Author: cito
Date: Sat Jul 23 08:06:19 2016
New Revision: 879
Log:
Enable garbage collection after closing DB instance
Note that there is still a problem if the DB instance is not closed.
Modified:
trunk/pg.py
trunk/tests/test_classic_dbwrapper.py
Modified: trunk/pg.py
==============================================================================
--- trunk/pg.py Thu Jul 21 12:57:25 2016 (r878)
+++ trunk/pg.py Sat Jul 23 08:06:19 2016 (r879)
@@ -1440,7 +1440,7 @@
# Context manager methods
def __enter__(self):
- """Enter the runtime context. This will start a transactio."""
+ """Enter the runtime context. This will start a transaction."""
self.begin()
return self
@@ -1506,6 +1506,7 @@
# Wraps shared library function so we can track state.
if self._closeable:
if self.db:
+ self.db.set_cast_hook(None)
self.db.close()
self.db = None
else:
@@ -1534,6 +1535,7 @@
if self._closeable:
db = connect(*self._args[0], **self._args[1])
if self.db:
+ self.db.set_cast_hook(None)
self.db.close()
self.db = db
Modified: trunk/tests/test_classic_dbwrapper.py
==============================================================================
--- trunk/tests/test_classic_dbwrapper.py Thu Jul 21 12:57:25 2016
(r878)
+++ trunk/tests/test_classic_dbwrapper.py Sat Jul 23 08:06:19 2016
(r879)
@@ -17,6 +17,7 @@
import os
import sys
+import gc
import json
import tempfile
@@ -4444,5 +4445,37 @@
self.assertEqual(self.get_output(), "")
+class TestMemoryLeaks(unittest.TestCase):
+ """Test that the DB class does not leak memory."""
+
+ def getLeaks(self, fut):
+ ids = set()
+ objs = []
+ add_ids = ids.update
+ gc.collect()
+ objs[:] = gc.get_objects()
+ add_ids(id(obj) for obj in objs)
+ fut()
+ gc.collect()
+ objs[:] = gc.get_objects()
+ objs[:] = [obj for obj in objs if id(obj) not in ids]
+ self.assertEqual(len(objs), 0)
+
+ def testLeaksWithClose(self):
+ def fut():
+ db = DB()
+ db.query("select $1::int as r", 42).dictresult()
+ db.close()
+ del db
+ self.getLeaks(fut)
+
+ @unittest.skip("this still needs to be resolved")
+ def testLeaksWithoutClose(self):
+ def fut():
+ db = DB()
+ db.query("select $1::int as r", 42).dictresult()
+ self.getLeaks(fut)
+
+
if __name__ == '__main__':
unittest.main()
_______________________________________________
PyGreSQL mailing list
[email protected]
https://mail.vex.net/mailman/listinfo.cgi/pygresql