Skip to content
Browse files

Implement __del__ method to automatically close log files

This removes ResourceWarning expections on Python 3, such as:

  ResourceWarning: unclosed file <_io.TextIOWrapper name='twisted.test.test_logfile/LogFileTestCase/test_fromFullPath/3f20oo/temp/test.log' mode='w+' encoding='UTF-8'>
    result = f(*a, **kw)
  • Loading branch information...
1 parent 8de0792 commit d73499f92b9596346dbc558c9672e892fdda71ee @multani committed Sep 28, 2013
Showing with 9 additions and 15 deletions.
  1. +9 −0 twisted/python/logfile.py
  2. +0 −15 twisted/test/test_logfile.py
View
9 twisted/python/logfile.py
@@ -39,6 +39,12 @@ def __init__(self, name, directory, defaultMode=None):
self.defaultMode = defaultMode
self._openFile()
+ def __del__(self):
+ try:
+ self.close()
+ except: # File might be already closed
+ pass
+
def fromFullPath(cls, filename, *args, **kwargs):
"""
Construct a log file from a full file path.
@@ -306,6 +312,9 @@ class LogReader:
def __init__(self, name):
self._file = open(name, "r")
+ def __del__(self):
+ self.close()
+
def readLines(self, lines=10):
"""Read a list of lines from the log file.
View
15 twisted/test/test_logfile.py
@@ -37,7 +37,6 @@ def test_abstractShouldRotate(self):
"""
log = logfile.BaseLogFile(self.name, self.dir)
self.assertRaises(NotImplementedError, log.shouldRotate)
- log.close()
def testWriting(self):
@@ -129,7 +128,6 @@ def testLogReader(self):
self.assertEqual(reader.readLines(), ["abc\n", "def\n"])
self.assertEqual(reader.readLines(), [])
reader.close()
- log.close()
def testModePreservation(self):
"""
@@ -142,7 +140,6 @@ def testModePreservation(self):
log.write("abc")
log.rotate()
self.assertEqual(mode, os.stat(self.path)[stat.ST_MODE])
- log.close()
def test_noPermission(self):
@@ -200,7 +197,6 @@ def test_maxNumberOfLog(self):
with open("%s.3" % self.path) as fp:
self.assertEqual(fp.read(), "2" * 11)
self.failUnless(not os.path.exists("%s.4" % self.path))
- log.close()
def test_fromFullPath(self):
"""
@@ -212,8 +208,6 @@ def test_fromFullPath(self):
self.assertEqual(os.path.abspath(log1.path), log2.path)
self.assertEqual(log1.rotateLength, log2.rotateLength)
self.assertEqual(log1.defaultMode, log2.defaultMode)
- log1.close()
- log2.close()
def test_defaultPermissions(self):
"""
@@ -227,7 +221,6 @@ def test_defaultPermissions(self):
log1 = logfile.LogFile(self.name, self.dir)
self.assertEqual(stat.S_IMODE(os.stat(self.path)[stat.ST_MODE]),
currentMode)
- log1.close()
def test_specifiedPermissions(self):
@@ -241,7 +234,6 @@ def test_specifiedPermissions(self):
self.assertEqual(mode, 0o444)
else:
self.assertEqual(mode, 0o066)
- log1.close()
def test_reopen(self):
@@ -302,7 +294,6 @@ def test_persistence(self):
self.assertEqual(defaultMode, copy.defaultMode)
self.assertEqual(maxRotatedFiles, copy.maxRotatedFiles)
self.assertEqual(log.size, copy.size)
- copy.close()
def test_cantChangeFileMode(self):
@@ -315,8 +306,6 @@ def test_cantChangeFileMode(self):
self.assertEqual(log.path, "/dev/null")
self.assertEqual(log.defaultMode, 0o555)
- log.close()
-
def test_listLogsWithBadlyNamedFiles(self):
"""
@@ -333,7 +322,6 @@ def test_listLogsWithBadlyNamedFiles(self):
self.assertEqual([1], log.listLogs())
-
class RiggedDailyLogFile(logfile.DailyLogFile):
_clock = 0.0
@@ -393,7 +381,6 @@ def testRotation(self):
log._clock = 259199 # 1970/01/03 23:59.59
log.write("3")
self.assert_(not os.path.exists(days[2]))
- log.close()
def test_getLog(self):
log = RiggedDailyLogFile(self.name, self.dir)
@@ -447,7 +434,6 @@ def test_persistence(self):
log = logfile.DailyLogFile(self.name, self.dir,
defaultMode)
log.write("123")
- log.close()
# Check that the unpickled log is the same as the original one.
copy = pickle.loads(pickle.dumps(log))
@@ -457,4 +443,3 @@ def test_persistence(self):
self.assertEqual(self.path, copy.path)
self.assertEqual(defaultMode, copy.defaultMode)
self.assertEqual(log.lastDate, copy.lastDate)
- copy.close()

0 comments on commit d73499f

Please sign in to comment.
Something went wrong with that request. Please try again.