Skip to content

Commit 0fb8790

Browse files
author
James William Pye
committed
Fix an invalid state bug on cursors.
This issue was caused by an invalid quantity being expressed in specific read() invocations. The quantity was expressed as the utter remainder that needed to be read, where it needed to be the the total number read. The issue than manifested itself as providing an offset greater than the length of the current buffer. This caused an over-compensation on future reads due to an effective negative available rows. Unrelated cleanups being some additional whitespace around statement snapshots.
1 parent 8f77102 commit 0fb8790

1 file changed

Lines changed: 12 additions & 10 deletions

File tree

postgresql/driver/pq3.py

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@
4949
bt.typelem != 0 AND
5050
bt.typelem = ae.oid
5151
)
52+
LEFT JOIN pg_namespace ns
53+
ON (ns.oid = bt.typnamespace)
5254
WHERE bt.oid = $1
5355
"""
5456

@@ -146,15 +148,15 @@ def __init__(self, connection):
146148
self.connection = connection
147149

148150
class TypeIO(pg_typio.TypeIO):
149-
def __init__(self, connection):
150-
self.connection = connection
151+
def __init__(self, database):
152+
self.database = database
151153
super().__init__()
152154

153155
def lookup_type_info(self, typid):
154-
return self.connection.prepare(TypeLookup).first(typid)
156+
return self.database.prepare(TypeLookup).first(typid)
155157

156158
def lookup_composite_type_info(self, typid):
157-
return self.connection.prepare(CompositeLookup)(typid)
159+
return self.database.prepare(CompositeLookup)(typid)
158160

159161
class CursorChunks(pg_api.CursorChunks):
160162
cursor = None
@@ -464,10 +466,6 @@ def read(self, quantity = None):
464466
##
465467
while self._buffer_more(None) > 0:
466468
pass
467-
##
468-
# Reading all, so the quantity becomes the difference
469-
# in the buffer length[len(1)] and the offset(0).
470-
quantity = len(self._state[1]) - offset
471469
else:
472470
if quantity < 0:
473471
# watch me go backwards
@@ -485,6 +483,10 @@ def read(self, quantity = None):
485483
# there is no pre-fetching going on.
486484
expanded = self._buffer_more(dir*left_to_read)
487485
left_to_read -= expanded
486+
##
487+
# The real quantity becomes the difference
488+
# in the buffer length[len(1)] and the offset(0).
489+
quantity = len(self._state[1]) - offset
488490

489491
end_of_block = offset + quantity
490492
t = self._state[1][offset:end_of_block]
@@ -992,9 +994,9 @@ def ife_snapshot_text(self):
992994
if self.ife_object_title != pg_api.InterfaceElement.ife_object_title:
993995
s += self.ife_object_title + ", "
994996
s += "statement_id(" + repr(self.statement_id) + ")"
995-
s += os.linesep + ' ' * 2 + (os.linesep + ' ' * 2).join(
997+
s += os.linesep*2 + ' '*2 + (os.linesep + ' ' * 2).join(
996998
str(self.string).split(os.linesep)
997-
)
999+
) + os.linesep
9981000
return s
9991001

10001002
def __del__(self):

0 commit comments

Comments
 (0)