Skip to content

Commit d9cc214

Browse files
author
James William Pye
committed
Fix the generic read() and __next__() methods.
The methods were assuming a tuple with three items, but only need to know of two. (copy uses four) Get rid of some traces of the old _cursor_type implementation. Fix CopyCursor(). It wasn't being properly initialized by UtilityCursor's _init.
1 parent 12f0975 commit d9cc214

1 file changed

Lines changed: 16 additions & 13 deletions

File tree

postgresql/driver/pq3.py

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -339,15 +339,15 @@ def __next__(self):
339339
if len(self._state[1]) > (2 * self.fetchcount):
340340
self._contract()
341341

342-
offset, buffer, x = self._state
342+
offset, buffer = self._state[:2]
343343
while offset >= len(buffer):
344344
if self._expand(1) == 0:
345345
# End of cursor.
346346
##
347347
raise StopIteration
348-
offset, buffer, x = self._state
348+
offset, buffer = self._state[:2]
349349
t = buffer[offset]
350-
self._state = (offset + 1, buffer, x)
350+
self._state = (offset + 1,) + self._state[1:]
351351
return t
352352

353353
def read(self, quantity = None):
@@ -369,12 +369,14 @@ def read(self, quantity = None):
369369
# Read some.
370370
##
371371
left_to_read = (quantity - (len(self._state[1]) - offset))
372-
while left_to_read > 0:
373-
left_to_read -= self._expand(left_to_read)
372+
expanded = -1
373+
while left_to_read > 0 and expanded != 0:
374+
expanded = self._expand(left_to_read)
375+
left_to_read -= expanded
374376

375377
end_of_block = offset + quantity
376378
t = self._state[1][offset:end_of_block]
377-
self._state = (end_of_block, self._state[1], self._state[2])
379+
self._state = (end_of_block,) + self._state[1:]
378380
return t
379381

380382
class TupleCursor(SequenceCursor):
@@ -426,10 +428,7 @@ def _expand(self, count):
426428
# Push and complete.
427429
self.connection._pq_push(x)
428430
if self.connection._pq_xact is x:
429-
if self._cursor_type == 'copy':
430-
self.connection._pq_step()
431-
else:
432-
self.connection._pq_complete()
431+
self.connection._pq_complete()
433432

434433
# At this point, it is expected that the transaction has more tuples
435434
# It's the cursor's current transaction and that won't change until
@@ -565,7 +564,7 @@ def _init(self):
565564
self._output_formats,
566565
),
567566
)
568-
super()._init(self, setup)
567+
super()._init(setup)
569568

570569
def _pq_xp_fetchmore(self, count):
571570
'[internal] make and return a transaction to get more rows'
@@ -688,6 +687,7 @@ def _init(self):
688687
for x in self._pq_xact.messages_received():
689688
if x.type is pq.element.CopyToBegin.type:
690689
self.__class__ = CopyCursor
690+
self._init()
691691
return
692692
# The COPY TO STDOUT transaction terminates the loop
693693
# *without* finishing the transaction.
@@ -709,7 +709,10 @@ def _pq_xact_get_copy_data(self):
709709
if type(y) is bytes
710710
]
711711

712-
def _expand(self):
712+
def _init(self):
713+
self._state = (0, (), None, self._pq_xact)
714+
715+
def _expand(self, count : "ignored"):
713716
"""
714717
[internal] helper function to put more copy data onto the buffer for
715718
reading. This function will only append to the buffer and never
@@ -740,7 +743,7 @@ def _expand(self):
740743
extension = self._pq_xact_get_copy_data()
741744
self._state = (
742745
offset,
743-
buffer + extension,
746+
tuple(chain(buffer,extension)),
744747
x.completed[0],
745748
x
746749
)

0 commit comments

Comments
 (0)