소스 검색

Use "with" statement to close files

Mike Naberezny 10 년 전
부모
커밋
288529272b
2개의 변경된 파일71개의 추가작업 그리고 91개의 파일을 삭제
  1. 44 52
      supervisor/options.py
  2. 27 39
      supervisor/tests/test_rpcinterfaces.py

+ 44 - 52
supervisor/options.py

@@ -1111,9 +1111,8 @@ class ServerOptions(Options):
     def write_pidfile(self):
         pid = os.getpid()
         try:
-            f = open(self.pidfile, 'w')
-            f.write('%s\n' % pid)
-            f.close()
+            with open(self.pidfile, 'w') as f:
+                f.write('%s\n' % pid)
         except (IOError, OSError):
             self.logger.critical('could not write pidfile %s' % self.pidfile)
         else:
@@ -1903,35 +1902,31 @@ def readFile(filename, offset, length):
     absoffset = abs(offset)
     abslength = abs(length)
 
-    f = None
     try:
-        f = open(filename, 'rb')
-        if absoffset != offset:
-            # negative offset returns offset bytes from tail of the file
-            if length:
-                raise ValueError('BAD_ARGUMENTS')
-            f.seek(0, 2)
-            sz = f.tell()
-            pos = int(sz - absoffset)
-            if pos < 0:
-                pos = 0
-            f.seek(pos)
-            data = f.read(absoffset)
-        else:
-            if abslength != length:
-                raise ValueError('BAD_ARGUMENTS')
-            if length == 0:
-                f.seek(offset)
-                data = f.read()
+        with open(filename, 'rb') as f:
+            if absoffset != offset:
+                # negative offset returns offset bytes from tail of the file
+                if length:
+                    raise ValueError('BAD_ARGUMENTS')
+                f.seek(0, 2)
+                sz = f.tell()
+                pos = int(sz - absoffset)
+                if pos < 0:
+                    pos = 0
+                f.seek(pos)
+                data = f.read(absoffset)
             else:
-                f.seek(offset)
-                data = f.read(length)
+                if abslength != length:
+                    raise ValueError('BAD_ARGUMENTS')
+                if length == 0:
+                    f.seek(offset)
+                    data = f.read()
+                else:
+                    f.seek(offset)
+                    data = f.read(length)
     except (OSError, IOError):
         raise ValueError('FAILED')
 
-    finally:
-        if f:
-            f.close()
     return data
 
 def tailFile(filename, offset, length):
@@ -1942,39 +1937,36 @@ def tailFile(filename, offset, length):
     bytes are not available, as many bytes as are available are returned.
     """
 
-    overflow = False
-    f = None
     try:
-        f = open(filename, 'rb')
-        f.seek(0, 2)
-        sz = f.tell()
-
-        if sz > (offset + length):
-            overflow = True
-            offset   = sz - 1
+        with open(filename, 'rb') as f:
+            overflow = False
+            f.seek(0, 2)
+            sz = f.tell()
 
-        if (offset + length) > sz:
-            if offset > (sz - 1):
-                length = 0
-            offset = sz - length
+            if sz > (offset + length):
+                overflow = True
+                offset = sz - 1
 
-        if offset < 0: offset = 0
-        if length < 0: length = 0
+            if (offset + length) > sz:
+                if offset > (sz - 1):
+                    length = 0
+                offset = sz - length
 
-        if length == 0:
-            data = ''
-        else:
-            f.seek(offset)
-            data = f.read(length)
+            if offset < 0:
+                offset = 0
+            if length < 0:
+                length = 0
 
-        offset = sz
-        return [as_string(data), offset, overflow]
+            if length == 0:
+                data = ''
+            else:
+                f.seek(offset)
+                data = f.read(length)
 
+            offset = sz
+            return [as_string(data), offset, overflow]
     except (OSError, IOError):
         return ['', offset, False]
-    finally:
-        if f:
-            f.close()
 
 # Helpers for dealing with signals and exit status
 

+ 27 - 39
supervisor/tests/test_rpcinterfaces.py

@@ -146,9 +146,8 @@ class SupervisorNamespaceXMLRPCInterfaceTests(TestBase):
         interface = self._makeOne(supervisord)
         try:
             logfile = supervisord.options.logfile
-            f = open(logfile, 'w+')
-            f.write('x' * 2048)
-            f.close()
+            with open(logfile, 'w+') as f:
+                f.write('x' * 2048)
             self._assertRPCError(xmlrpc.Faults.BAD_ARGUMENTS,
                                  interface.readLog, offset=-1, length=1)
             self._assertRPCError(xmlrpc.Faults.BAD_ARGUMENTS,
@@ -162,10 +161,9 @@ class SupervisorNamespaceXMLRPCInterfaceTests(TestBase):
         interface = self._makeOne(supervisord)
         logfile = supervisord.options.logfile
         try:
-            f = open(logfile, 'w+')
-            f.write('x' * 2048)
-            f.write('y' * 2048)
-            f.close()
+            with open(logfile, 'w+') as f:
+                f.write('x' * 2048)
+                f.write('y' * 2048)
             data = interface.readLog(offset=0, length=0)
             self.assertEqual(interface.update_text, 'readLog')
             self.assertEqual(data, ('x' * 2048) + ('y' * 2048))
@@ -1349,9 +1347,8 @@ class SupervisorNamespaceXMLRPCInterfaceTests(TestBase):
         logfile = process.config.stdout_logfile
 
         try:
-            f = open(logfile, 'w+')
-            f.write('x' * 2048)
-            f.close()
+            with open(logfile, 'w+') as f:
+                f.write('x' * 2048)
             self._assertRPCError(xmlrpc.Faults.BAD_ARGUMENTS,
                                  interface.readProcessStdoutLog,
                                  'process1', offset=-1, length=1)
@@ -1381,10 +1378,9 @@ class SupervisorNamespaceXMLRPCInterfaceTests(TestBase):
         process = supervisord.process_groups['foo'].processes['foo']
         logfile = process.config.stdout_logfile
         try:
-            f = open(logfile, 'w+')
-            f.write('x' * 2048)
-            f.write('y' * 2048)
-            f.close()
+            with open(logfile, 'w+') as f:
+                f.write('x' * 2048)
+                f.write('y' * 2048)
             data = interface.readProcessStdoutLog('foo', offset=0, length=0)
             self.assertEqual(interface.update_text, 'readProcessStdoutLog')
             self.assertEqual(data, ('x' * 2048) + ('y' * 2048))
@@ -1427,9 +1423,8 @@ class SupervisorNamespaceXMLRPCInterfaceTests(TestBase):
         logfile = process.config.stderr_logfile
 
         try:
-            f = open(logfile, 'w+')
-            f.write('x' * 2048)
-            f.close()
+            with open(logfile, 'w+') as f:
+                f.write('x' * 2048)
             self._assertRPCError(xmlrpc.Faults.BAD_ARGUMENTS,
                                  interface.readProcessStderrLog,
                                  'process1', offset=-1, length=1)
@@ -1459,10 +1454,9 @@ class SupervisorNamespaceXMLRPCInterfaceTests(TestBase):
         process = supervisord.process_groups['foo'].processes['foo']
         logfile = process.config.stderr_logfile
         try:
-            f = open(logfile, 'w+')
-            f.write('x' * 2048)
-            f.write('y' * 2048)
-            f.close()
+            with open(logfile, 'w+') as f:
+                f.write('x' * 2048)
+                f.write('y' * 2048)
             data = interface.readProcessStderrLog('foo', offset=0, length=0)
             self.assertEqual(interface.update_text, 'readProcessStderrLog')
             self.assertEqual(data, ('x' * 2048) + ('y' * 2048))
@@ -1503,9 +1497,8 @@ class SupervisorNamespaceXMLRPCInterfaceTests(TestBase):
         process = supervisord.process_groups['foo'].processes['foo']
         logfile = process.config.stdout_logfile
         try:
-            f = open(logfile, 'w+')
-            f.write(letters)
-            f.close()
+            with open(logfile, 'w+') as f:
+                f.write(letters)
 
             data, offset, overflow = interface.tailProcessStdoutLog('foo',
                                                         offset=0,
@@ -1528,9 +1521,8 @@ class SupervisorNamespaceXMLRPCInterfaceTests(TestBase):
         process = supervisord.process_groups['foo'].processes['foo']
         logfile = process.config.stdout_logfile
         try:
-            f = open(logfile, 'w+')
-            f.write(letters)
-            f.close()
+            with open(logfile, 'w+') as f:
+                f.write(letters)
 
             # offset==logsize
             data, offset, overflow = interface.tailProcessStdoutLog('foo',
@@ -1563,9 +1555,8 @@ class SupervisorNamespaceXMLRPCInterfaceTests(TestBase):
         process = supervisord.process_groups['foo'].processes['foo']
         logfile = process.config.stdout_logfile
         try:
-            f = open(logfile, 'w+')
-            f.write(letters)
-            f.close()
+            with open(logfile, 'w+') as f:
+                f.write(letters)
 
             data, offset, overflow = interface.tailProcessStdoutLog('foo',
                                                         offset=0, length=5)
@@ -1627,9 +1618,8 @@ class SupervisorNamespaceXMLRPCInterfaceTests(TestBase):
         process = supervisord.process_groups['foo'].processes['foo']
         logfile = process.config.stderr_logfile
         try:
-            f = open(logfile, 'w+')
-            f.write(letters)
-            f.close()
+            with open(logfile, 'w+') as f:
+                f.write(letters)
 
             data, offset, overflow = interface.tailProcessStderrLog('foo',
                                                         offset=0,
@@ -1652,9 +1642,8 @@ class SupervisorNamespaceXMLRPCInterfaceTests(TestBase):
         process = supervisord.process_groups['foo'].processes['foo']
         logfile = process.config.stderr_logfile
         try:
-            f = open(logfile, 'w+')
-            f.write(letters)
-            f.close()
+            with open(logfile, 'w+') as f:
+                f.write(letters)
 
             # offset==logsize
             data, offset, overflow = interface.tailProcessStderrLog('foo',
@@ -1687,9 +1676,8 @@ class SupervisorNamespaceXMLRPCInterfaceTests(TestBase):
         process = supervisord.process_groups['foo'].processes['foo']
         logfile = process.config.stderr_logfile
         try:
-            f = open(logfile, 'w+')
-            f.write(letters)
-            f.close()
+            with open(logfile, 'w+') as f:
+                f.write(letters)
 
             data, offset, overflow = interface.tailProcessStderrLog('foo',
                                                         offset=0, length=5)