Skip to content

Commit 9f80b0c

Browse files
fantixelprans
authored andcommitted
Refs #285, use unsigned int for tid
1 parent cddea9c commit 9f80b0c

File tree

2 files changed

+26
-23
lines changed

2 files changed

+26
-23
lines changed

asyncpg/protocol/codecs/tid.pyx

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
cdef tid_encode(ConnectionSettings settings, WriteBuffer buf, obj):
99
cdef int overflow = 0
10-
cdef long block, offset
10+
cdef unsigned long block, offset
1111

1212
if not (cpython.PyTuple_Check(obj) or cpython.PyList_Check(obj)):
1313
raise TypeError(
@@ -18,25 +18,24 @@ cdef tid_encode(ConnectionSettings settings, WriteBuffer buf, obj):
1818
'invalid number of elements in tid tuple, expecting 2')
1919

2020
try:
21-
block = cpython.PyLong_AsLong(obj[0])
21+
block = cpython.PyLong_AsUnsignedLong(obj[0])
2222
except OverflowError:
2323
overflow = 1
2424

2525
# "long" and "long long" have the same size for x86_64, need an extra check
26-
if overflow or (sizeof(block) > 4 and (block < -2147483648 or
27-
block > 2147483647)):
26+
if overflow or (sizeof(block) > 4 and block > 4294967295):
2827
raise OverflowError(
29-
'block too big to be encoded as INT4: {!r}'.format(obj[0]))
28+
'block too big to be encoded as UINT4: {!r}'.format(obj[0]))
3029

3130
try:
32-
offset = cpython.PyLong_AsLong(obj[1])
31+
offset = cpython.PyLong_AsUnsignedLong(obj[1])
3332
overflow = 0
3433
except OverflowError:
3534
overflow = 1
3635

37-
if overflow or offset < -32768 or offset > 32767:
36+
if overflow or offset > 65535:
3837
raise OverflowError(
39-
'offset too big to be encoded as INT2: {!r}'.format(obj[1]))
38+
'offset too big to be encoded as UINT2: {!r}'.format(obj[1]))
4039

4140
buf.write_int32(6)
4241
buf.write_int32(<int32_t>block)
@@ -45,11 +44,11 @@ cdef tid_encode(ConnectionSettings settings, WriteBuffer buf, obj):
4544

4645
cdef tid_decode(ConnectionSettings settings, FastReadBuffer buf):
4746
cdef:
48-
int32_t block
49-
int16_t offset
47+
uint32_t block
48+
uint16_t offset
5049

51-
block = hton.unpack_int32(buf.read(4))
52-
offset = hton.unpack_int16(buf.read(2))
50+
block = <uint32_t>hton.unpack_int32(buf.read(4))
51+
offset = <uint16_t>hton.unpack_int16(buf.read(2))
5352

5453
return (block, offset)
5554

tests/test_codecs.py

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -380,7 +380,13 @@ def _timezone(offset):
380380
asyncpg.Circle((0.0, 0.0), 100),
381381
]),
382382
('tid', 'tid', [
383-
(0, 2),
383+
(100, 200),
384+
(0, 0),
385+
(2147483647, 0),
386+
(4294967295, 0),
387+
(0, 32767),
388+
(0, 65535),
389+
(4294967295, 65535),
384390
]),
385391
]
386392

@@ -611,20 +617,18 @@ async def test_invalid_input(self):
611617
[1, 2, 3],
612618
(4,),
613619
]),
614-
('tid', OverflowError, 'block too big to be encoded as INT4', [
620+
('tid', OverflowError, 'block too big to be encoded as UINT4', [
621+
(-1, 0),
615622
(2**256, 0),
616-
(decimal.Decimal("2000000000000000000000000000000"), 0),
617-
(0xffffffff, 0),
618-
(2**31, 0),
619-
(-2**31 - 1, 0),
623+
(0xffffffff + 1, 0),
624+
(2**32, 0),
620625
]),
621-
('tid', OverflowError, 'offset too big to be encoded as INT2', [
626+
('tid', OverflowError, 'offset too big to be encoded as UINT2', [
627+
(0, -1),
622628
(0, 2**256),
623-
(0, decimal.Decimal("2000000000000000000000000000000")),
624-
(0, 0xffff),
629+
(0, 0xffff + 1),
625630
(0, 0xffffffff),
626-
(0, 32768),
627-
(0, -32769),
631+
(0, 65536),
628632
]),
629633
]
630634

0 commit comments

Comments
 (0)