Changeset 879 for trunk


Ignore:
Timestamp:
Jul 23, 2016, 8:06:19 AM (3 years ago)
Author:
cito
Message:

Enable garbage collection after closing DB instance

Note that there is still a problem if the DB instance is not closed.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/pg.py

    r878 r879  
    14411441
    14421442    def __enter__(self):
    1443         """Enter the runtime context. This will start a transactio."""
     1443        """Enter the runtime context. This will start a transaction."""
    14441444        self.begin()
    14451445        return self
     
    15071507        if self._closeable:
    15081508            if self.db:
     1509                self.db.set_cast_hook(None)
    15091510                self.db.close()
    15101511                self.db = None
     
    15351536            db = connect(*self._args[0], **self._args[1])
    15361537            if self.db:
     1538                self.db.set_cast_hook(None)
    15371539                self.db.close()
    15381540            self.db = db
  • trunk/tests/test_classic_dbwrapper.py

    r878 r879  
    1818import os
    1919import sys
     20import gc
    2021import json
    2122import tempfile
     
    44454446
    44464447
     4448class TestMemoryLeaks(unittest.TestCase):
     4449    """Test that the DB class does not leak memory."""
     4450
     4451    def getLeaks(self, fut):
     4452        ids = set()
     4453        objs = []
     4454        add_ids = ids.update
     4455        gc.collect()
     4456        objs[:] = gc.get_objects()
     4457        add_ids(id(obj) for obj in objs)
     4458        fut()
     4459        gc.collect()
     4460        objs[:] = gc.get_objects()
     4461        objs[:] = [obj for obj in objs if id(obj) not in ids]
     4462        self.assertEqual(len(objs), 0)
     4463
     4464    def testLeaksWithClose(self):
     4465        def fut():
     4466            db = DB()
     4467            db.query("select $1::int as r", 42).dictresult()
     4468            db.close()
     4469            del db
     4470        self.getLeaks(fut)
     4471
     4472    @unittest.skip("this still needs to be resolved")
     4473    def testLeaksWithoutClose(self):
     4474        def fut():
     4475            db = DB()
     4476            db.query("select $1::int as r", 42).dictresult()
     4477        self.getLeaks(fut)
     4478
     4479
    44474480if __name__ == '__main__':
    44484481    unittest.main()
Note: See TracChangeset for help on using the changeset viewer.