Skip to content

Commit 3b42f99

Browse files
committed
[IMP] pg.named_cursor: lessons learned from pg.query_ids
- it is not exactly right to call into the `Cursor`'s `__enter__` and `__exit__` methods - `Cursor` should be explicitly closed before it is garbage collected
1 parent 211718c commit 3b42f99

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

src/util/pg.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1770,6 +1770,7 @@ def get_m2m_tables(cr, table):
17701770

17711771
class named_cursor(object):
17721772
def __init__(self, cr, itersize=None):
1773+
self._ncr = None
17731774
self._ncr = cr._cnx.cursor("upg_nc_" + uuid.uuid4().hex, withhold=True)
17741775
if itersize:
17751776
self._ncr.itersize = itersize
@@ -1790,19 +1791,26 @@ def dictfetchone(self):
17901791
def iterdict(self):
17911792
return map(self.__dictrow, self._ncr)
17921793

1794+
def _close(self):
1795+
if self._ncr and not self._ncr.closed:
1796+
self._ncr.close()
1797+
17931798
def __iter__(self):
17941799
return self._ncr.__iter__()
17951800

17961801
def __enter__(self):
1797-
self._ncr.__enter__()
17981802
return self
17991803

18001804
def __exit__(self, exc_type, exc_value, traceback):
1801-
return self._ncr.__exit__(exc_type, exc_value, traceback)
1805+
self._close()
1806+
return False
18021807

18031808
def __getattr__(self, name):
18041809
return getattr(self._ncr, name)
18051810

1811+
def __del__(self):
1812+
self._close()
1813+
18061814

18071815
def create_id_sequence(cr, table, set_as_default=True):
18081816
if not table_exists(cr, table):

0 commit comments

Comments
 (0)