Author: cito
Date: Sat Jul 23 15:41:16 2016
New Revision: 881

Log:
Add memory leak test for pgdb connections

Modified:
   trunk/docs/contents/changelog.rst
   trunk/pgdb.py
   trunk/tests/test_classic_dbwrapper.py
   trunk/tests/test_dbapi20.py

Modified: trunk/docs/contents/changelog.rst
==============================================================================
--- trunk/docs/contents/changelog.rst   Sat Jul 23 09:21:57 2016        (r880)
+++ trunk/docs/contents/changelog.rst   Sat Jul 23 15:41:16 2016        (r881)
@@ -15,6 +15,8 @@
   on the mailing list by Justin Pryzby).
 - Allow extra values that are not used in the command in the parameter dict
   passed to the query_formatted() method (as suggested by Justin Pryzby).
+- Unused classic connections were not properly garbage collected which could
+  cause memory leaks (reported by Justin Pryby).
 - Made C extension compatible with MSVC 9 again (this was needed to compile for
   Python 2 on Windows).
 

Modified: trunk/pgdb.py
==============================================================================
--- trunk/pgdb.py       Sat Jul 23 09:21:57 2016        (r880)
+++ trunk/pgdb.py       Sat Jul 23 15:41:16 2016        (r881)
@@ -925,7 +925,7 @@
         try:
             if not self._dbcnx._tnx:
                 try:
-                    self._cnx.source().execute(sql)
+                    self._src.execute(sql)
                 except DatabaseError:
                     raise  # database provides error message
                 except Exception:

Modified: trunk/tests/test_classic_dbwrapper.py
==============================================================================
--- trunk/tests/test_classic_dbwrapper.py       Sat Jul 23 09:21:57 2016        
(r880)
+++ trunk/tests/test_classic_dbwrapper.py       Sat Jul 23 15:41:16 2016        
(r881)
@@ -929,8 +929,6 @@
         r = q.getresult()[0][0]
         self.assertEqual(r, 'alphabetagammadeltaepsilon')
 
-
-
     def testPkey(self):
         query = self.db.query
         pkey = self.db.pkey
@@ -4459,6 +4457,9 @@
         gc.collect()
         objs[:] = gc.get_objects()
         objs[:] = [obj for obj in objs if id(obj) not in ids]
+        if objs and sys.version_info[:3] in ((3, 5, 0), (3, 5, 1)):
+            # workaround for Python issue 26811
+            objs[:] = [obj for obj in objs if repr(obj) != '(<NULL>,)']
         self.assertEqual(len(objs), 0)
 
     def testLeaksWithClose(self):

Modified: trunk/tests/test_dbapi20.py
==============================================================================
--- trunk/tests/test_dbapi20.py Sat Jul 23 09:21:57 2016        (r880)
+++ trunk/tests/test_dbapi20.py Sat Jul 23 15:41:16 2016        (r881)
@@ -28,6 +28,9 @@
     except ImportError:
         pass
 
+import gc
+import sys
+
 from datetime import date, time, datetime, timedelta
 from uuid import UUID as Uuid
 
@@ -1216,6 +1219,31 @@
         self.assertEqual('record', pgdb.RECORD)
         self.assertNotEqual('_record', pgdb.RECORD)
 
+    def test_no_close(self):
+        data = ('hello', 'world')
+        con = self._connect()
+        cur = con.cursor()
+        cur.build_row_factory = lambda: tuple
+        cur.execute("select %s, %s", data)
+        row = cur.fetchone()
+        self.assertEqual(row, data)
+
+    def test_memory_leaks(self):
+        ids = set()
+        objs = []
+        add_ids = ids.update
+        gc.collect()
+        objs[:] = gc.get_objects()
+        add_ids(id(obj) for obj in objs)
+        self.test_no_close()
+        gc.collect()
+        objs[:] = gc.get_objects()
+        objs[:] = [obj for obj in objs if id(obj) not in ids]
+        if objs and sys.version_info[:3] in ((3, 5, 0), (3, 5, 1)):
+            # workaround for Python issue 26811
+            objs[:] = [obj for obj in objs if repr(obj) != '(<NULL>,)']
+        self.assertEqual(len(objs), 0)
+
 
 if __name__ == '__main__':
     unittest.main()
_______________________________________________
PyGreSQL mailing list
[email protected]
https://mail.vex.net/mailman/listinfo.cgi/pygresql

Reply via email to