Skip to content

Commit 5b155ba

Browse files
committed
Fix bytea codec under Python 3.3
There is a change in behaviour of memoryview in 3.3, it now returns integer values of bytes when subscripting.
1 parent b7ac6e3 commit 5b155ba

1 file changed

Lines changed: 9 additions & 2 deletions

File tree

postgresql/encodings/bytea.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
'PostgreSQL bytea encoding and decoding functions'
55
import codecs
66
import struct
7+
import sys
78

89
ord_to_seq = {
910
i : \
@@ -12,8 +13,14 @@
1213
if i == 92 else chr(i)
1314
for i in range(256)
1415
}
15-
def decode(data):
16-
return ''.join(map(ord_to_seq.__getitem__, (data[x][0] for x in range(len(data)))))
16+
17+
if sys.version_info[:2] >= (3, 3):
18+
# Subscripting memory in 3.3 returns byte as an integer, not as a bytestring
19+
def decode(data):
20+
return ''.join(map(ord_to_seq.__getitem__, (data[x] for x in range(len(data)))))
21+
else:
22+
def decode(data):
23+
return ''.join(map(ord_to_seq.__getitem__, (data[x][0] for x in range(len(data)))))
1724

1825
def encode(data):
1926
diter = ((data[i] for i in range(len(data))))

0 commit comments

Comments
 (0)