Skip to content

Commit 60e0503

Browse files
author
James William Pye
committed
Avoid some circular references.
1 parent aed9630 commit 60e0503

1 file changed

Lines changed: 22 additions & 14 deletions

File tree

postgresql/driver/pq3.py

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -211,10 +211,6 @@ def __init__(self, cursor_id):
211211
self.cursor_id = cursor_id
212212
self._quoted_cursor_id = '"' + self.cursor_id.replace('"', '""') + '"'
213213
self._pq_cursor_id = self.database.typio.encode(self.cursor_id)
214-
self._ins = partial(
215-
xact.Instruction,
216-
asynchook = partial(self.database._receive_async, controller = self)
217-
)
218214
self._init()
219215

220216
def __iter__(self):
@@ -233,6 +229,12 @@ def __del__(self):
233229
self.database.typio.encode(self.cursor_id)
234230
)
235231

232+
def _asynchook(self, *args):
233+
self.database._receive_async(*args, controller = self)
234+
235+
def _ins(self, *args):
236+
return xact.Instruction(*args, asynchook = self._asynchook)
237+
236238
def _pq_xp_describe(self):
237239
return (element.DescribePortal(self._pq_cursor_id),)
238240

@@ -450,7 +452,7 @@ def _init(self):
450452
code = "--000",
451453
message = "unexpected message type " + repr(x.type)
452454
)
453-
self.database._raise_pq_error(self._xact)
455+
self.database._raise_pq_error(self._xact, controller = self)
454456
return
455457

456458
def __next__(self):
@@ -461,7 +463,7 @@ def __next__(self):
461463
while x.state is not xact.Complete and not x.completed:
462464
self.database._pq_step()
463465
if x.fatal is not None:
464-
self.database._raise_pq_error(x)
466+
self.database._raise_pq_error(x, controller = self)
465467

466468
if not x.completed:
467469
# Transaction has been cleaned out of completed? iterator is done.
@@ -700,11 +702,13 @@ def __init__(self, database, statement_id, string):
700702
self.database = database
701703
self.statement_id = statement_id or ID(self)
702704
self.string = string
703-
self._ah = partial(self.database._receive_async, controller = self)
704705
self._pq_xact = None
705706
self._pq_statement_id = None
706707
self.closed = None
707708

709+
def _receive_async(self, *args):
710+
return self.database._receive_async(*args, controller = self)
711+
708712
def __repr__(self):
709713
return '<{mod}.{name}[{ci}] {state}>'.format(
710714
mod = type(self).__module__,
@@ -869,7 +873,7 @@ def _init(self):
869873
element.SynchronizeMessage,
870874
)
871875
)
872-
self._xact = xact.Instruction(cmd, asynchook = self._ah)
876+
self._xact = xact.Instruction(cmd, asynchook = self._receive_async)
873877
self.database._pq_push(self._xact, self)
874878

875879
def _fini(self):
@@ -1015,7 +1019,7 @@ def first(self, *parameters):
10151019
element.Execute(b'', 0xFFFFFFFF),
10161020
element.SynchronizeMessage
10171021
),
1018-
asynchook = self._ah
1022+
asynchook = self._receive_async
10191023
)
10201024
c._pq_push(x, self)
10211025
c._pq_complete()
@@ -1074,7 +1078,7 @@ def _copy_data_in(self,
10741078
element.Execute(b'', 1),
10751079
element.SynchronizeMessage,
10761080
),
1077-
asynchook = self._ah
1081+
asynchook = self._receive_async
10781082
)
10791083
self.database._pq_push(x, self)
10801084

@@ -1149,7 +1153,7 @@ def _load_bulk_tuples(self, tupleseq, tps = None):
11491153
last = element.SynchronizeMessage
11501154
xm.append(last)
11511155
self.database._pq_push(
1152-
xact.Instruction(xm, asynchook = self._ah),
1156+
xact.Instruction(xm, asynchook = self._receive_async),
11531157
self
11541158
)
11551159
self.database._pq_complete()
@@ -2022,23 +2026,26 @@ def _pq_push(self, xact, controller = None):
20222026
if x is not None:
20232027
self.pq.complete()
20242028
self._raise_pq_error(x)
2025-
xact.controller = controller or self
2029+
if controller is not None:
2030+
self._controller = controller
20262031
self.pq.push(xact)
20272032

20282033
def _pq_complete(self):
20292034
x = self.pq.xact
20302035
if self.pq.xact is not None:
20312036
self.pq.complete()
20322037
self._raise_pq_error(x)
2038+
del self._controller
20332039

20342040
def _pq_step(self):
20352041
x = self.pq.xact
20362042
if x is not None:
20372043
self.pq.step()
20382044
if x.state is xact.Complete:
20392045
self._raise_pq_error(x)
2046+
del self._controller
20402047

2041-
def _raise_pq_error(self, xact = None):
2048+
def _raise_pq_error(self, xact = None, controller = None):
20422049
if xact is not None:
20432050
x = xact
20442051
else:
@@ -2048,7 +2055,8 @@ def _raise_pq_error(self, xact = None):
20482055
return
20492056
err = self._error_lookup(x.error_message)
20502057
fromexc = getattr(x, 'exception', None)
2051-
fromcontroller = getattr(x, 'controller', self)
2058+
if controller is None:
2059+
fromcontroller = getattr(self, '_controller', self)
20522060
err.creator = fromcontroller
20532061
if fromexc is not None:
20542062
err.__cause__ = fromexc

0 commit comments

Comments
 (0)