瀏覽代碼

Use "with" statement to close files

Mike Naberezny 10 年之前
父節點
當前提交
8c404c6bdc
共有 2 個文件被更改,包括 6 次插入9 次删除
  1. 2 3
      supervisor/tests/base.py
  2. 4 6
      supervisor/tests/test_options.py

+ 2 - 3
supervisor/tests/base.py

@@ -583,9 +583,8 @@ def makeExecutable(file, substitutions=None):
         data = data.replace('<<%s>>' % key.upper(), substitutions[key])
 
     tmpnam = tempfile.mktemp(prefix=last)
-    f = open(tmpnam, 'w')
-    f.write(data)
-    f.close()
+    with open(tmpnam, 'w') as f:
+        f.write(data)
     os.chmod(tmpnam, 493) # 0755 on Py2, 0o755 on Py3
     return tmpnam
 

+ 4 - 6
supervisor/tests/test_options.py

@@ -907,9 +907,8 @@ class ServerOptionsTests(unittest.TestCase):
 
     def test_cleanup_afunix_unlink(self):
         fn = tempfile.mktemp()
-        f = open(fn, 'w')
-        f.write('foo')
-        f.close()
+        with open(fn, 'w') as f:
+            f.write('foo')
         instance = self._makeOne()
         class Port:
             family = socket.AF_UNIX
@@ -925,9 +924,8 @@ class ServerOptionsTests(unittest.TestCase):
     def test_cleanup_afunix_nounlink(self):
         fn = tempfile.mktemp()
         try:
-            f = open(fn, 'w')
-            f.write('foo')
-            f.close()
+            with open(fn, 'w') as f:
+                f.write('foo')
             instance = self._makeOne()
             class Port:
                 family = socket.AF_UNIX