Skip to content

Commit f6c985d

Browse files
author
James William Pye
committed
Fix bugs revealed by test_protocol.
Mostly "bytes not str" issues.
1 parent 79550af commit f6c985d

3 files changed

Lines changed: 24 additions & 26 deletions

File tree

postgresql/protocol/element3.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -747,10 +747,10 @@ def serialize(self):
747747

748748
@classmethod
749749
def parse(typ, data):
750-
if data[0] != typ.subtype:
750+
if data[0:1] != typ.subtype:
751751
raise ValueError(
752752
"invalid Describe message subtype, %r; expected %r" %(
753-
typ.subtype, data[0]
753+
typ.subtype, data[0:1]
754754
)
755755
)
756756
return typ(data[1:-1])
@@ -773,10 +773,10 @@ def serialize(self):
773773

774774
@classmethod
775775
def parse(typ, data):
776-
if data[0] != typ.subtype:
776+
if data[0:1] != typ.subtype:
777777
raise ValueError(
778778
"invalid Close message subtype, %r; expected %r" %(
779-
typ.subtype, data[0]
779+
typ.subtype, data[0:1]
780780
)
781781
)
782782
return typ(data[1:-1])

postgresql/protocol/typstruct.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -388,7 +388,7 @@ def record_pack(seq):
388388
"""
389389
pack a record given an iterable of (type_oid, data) pairs.
390390
"""
391-
return long_pack(len(seq)) + ''.join([
391+
return long_pack(len(seq)) + b''.join([
392392
# typid + (null_seq or data)
393393
oid_pack(x) + (y is None and null_sequence or (long_pack(len(y)) + y))
394394
for x, y in seq
@@ -432,7 +432,7 @@ def array_pack(array_data):
432432
header = llL_pack((len(dlb) // 2, flags, typid))
433433
return header + \
434434
struct.pack("!%dl" %(len(dlb),), *dlb) + \
435-
''.join(elements_pack(elements))
435+
b''.join(elements_pack(elements))
436436

437437
def elements_unpack(data, offset):
438438
"""

postgresql/test/test_protocol.py

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -149,23 +149,23 @@ class p_buffer(buffer_test, unittest.TestCase):
149149
e3.Parse(b'statement_id', b'query', (123,)),
150150
e3.Parse(b'statement_id', b'query', ()),
151151
e3.Bind(b'portal_id', b'statement_id',
152-
[b'tt',b'\x00\x00'],
152+
(b'tt',b'\x00\x00'),
153153
[b'data',None], (b'ff',b'xx')),
154-
e3.Bind(b'portal_id', b'statement_id', [b'tt'], [None], (b'xx',)),
155-
e3.Bind(b'portal_id', b'statement_id', [b'ff'], [b'data'], ()),
156-
e3.Bind(b'portal_id', b'statement_id', [], [], (b'xx',)),
157-
e3.Bind(b'portal_id', b'statement_id', [], [], ()),
154+
e3.Bind(b'portal_id', b'statement_id', (b'tt',), [None], (b'xx',)),
155+
e3.Bind(b'portal_id', b'statement_id', (b'ff',), [b'data'], ()),
156+
e3.Bind(b'portal_id', b'statement_id', (), [], (b'xx',)),
157+
e3.Bind(b'portal_id', b'statement_id', (), [], ()),
158158
e3.Execute(b'portal_id', 500),
159159
e3.Execute(b'portal_id', 0),
160160
e3.DescribeStatement(b'statement_id'),
161161
e3.DescribePortal(b'portal_id'),
162162
e3.CloseStatement(b'statement_id'),
163163
e3.ClosePortal(b'portal_id'),
164-
e3.Function(123, [], [], b'xx'),
165-
e3.Function(321, [b'tt'], [b'foo'], b'xx'),
166-
e3.Function(321, [b'tt'], [None], b'xx'),
167-
e3.Function(321, [b'aa', b'aa'], [None,b'a' * 200], b'xx'),
168-
e3.FunctionResult(''),
164+
e3.Function(123, (), [], b'xx'),
165+
e3.Function(321, (b'tt',), [b'foo'], b'xx'),
166+
e3.Function(321, (b'tt',), [None], b'xx'),
167+
e3.Function(321, (b'aa', b'aa'), [None,b'a' * 200], b'xx'),
168+
e3.FunctionResult(b''),
169169
e3.FunctionResult(b'foobar'),
170170
e3.FunctionResult(None),
171171
e3.CopyToBegin(123, [321,123]),
@@ -185,9 +185,7 @@ class test_element3(unittest.TestCase):
185185
def testSerializeParseConsistency(self):
186186
for msg in message_samples:
187187
smsg = msg.serialize()
188-
self.failUnlessEqual(
189-
msg, msg.parse(smsg),
190-
)
188+
self.failUnlessEqual(msg, msg.parse(smsg))
191189

192190
def testEmptyMessages(self):
193191
for x in e3.__dict__.values():
@@ -335,7 +333,7 @@ def testTransactionSamplesAll(self):
335333
x.state[1]()
336334
self.failUnlessEqual(x.messages, ())
337335
x.state[1](r)
338-
self.failUnlessEqual(x.state[0], c3.Complete)
336+
self.failUnlessEqual(x.state, c3.Complete)
339337
rec = []
340338
for y in x.completed:
341339
for z in y[1]:
@@ -552,7 +550,7 @@ def testTransactionSamplesAll(self):
552550
(0xffffffff, 0xffff),
553551
(0, 0xffff),
554552
(0xffffffff, 0),
555-
(0xffffffff / 2, 0xffff / 2),
553+
(0xffffffff // 2, 0xffff // 2),
556554
],
557555

558556
pg_types.CIDROID : [
@@ -597,7 +595,7 @@ def testTransactionSamplesAll(self):
597595
consistency_samples[pg_types.OIDOID] = \
598596
consistency_samples[pg_types.CIDOID] = \
599597
consistency_samples[pg_types.XIDOID] = [
600-
0, 0xffffffff, 0xffffffff / 2, 123, 321, 1, 2, 3
598+
0, 0xffffffff, 0xffffffff // 2, 123, 321, 1, 2, 3
601599
]
602600

603601
consistency_samples[pg_types.LSEGOID] = \
@@ -654,12 +652,12 @@ def testExpectIO(self, map, samples):
654652
class test_typio(unittest.TestCase):
655653
def testExpectations(self):
656654
'IO tests where the pre-made expected serialized form is compared'
657-
testExpectIO(self, pg_typio.oid_to_io, expectation_samples)
655+
testExpectIO(self, pg_typstruct.oid_to_io, expectation_samples)
658656

659657
def testConsistency(self):
660658
'IO tests where the unpacked source is compared to re-unpacked result'
661659
for oid, sample in consistency_samples.items():
662-
pack, unpack = pg_typio.oid_to_io.get(oid, (None, None))
660+
pack, unpack = pg_typstruct.oid_to_io.get(oid, (None, None))
663661
if pack is not None:
664662
for x in sample:
665663
packed = pack(x)
@@ -671,7 +669,7 @@ def testConsistency(self):
671669
x, packed, unpacked
672670
)
673671
)
674-
for oid, (pack, unpack) in pg_typio.time_io.items():
672+
for oid, (pack, unpack) in pg_typstruct.time_io.items():
675673
sample = consistency_samples.get(oid, [])
676674
for x in sample:
677675
packed = pack(x)
@@ -684,7 +682,7 @@ def testConsistency(self):
684682
)
685683
)
686684

687-
for oid, (pack, unpack) in pg_typio.time64_io.items():
685+
for oid, (pack, unpack) in pg_typstruct.time64_io.items():
688686
sample = consistency_samples.get(oid, [])
689687
for x in sample:
690688
packed = pack(x)

0 commit comments

Comments
 (0)