Skip to content

Commit e082910

Browse files
vitaly-burovoyelprans
authored andcommitted
Fix minimal allowed range for integers
Since '0' is in the positive half, negative range is slightly wider: abs(min_value) == (max_value + 1)
1 parent 3b794e1 commit e082910

File tree

2 files changed

+5
-5
lines changed

2 files changed

+5
-5
lines changed

asyncpg/protocol/codecs/int.pyx

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ cdef bool_decode(ConnectionSettings settings, FastReadBuffer buf):
2020

2121
cdef int2_encode(ConnectionSettings settings, WriteBuffer buf, obj):
2222
cdef long val = cpython.PyLong_AsLong(obj)
23-
if val < -32767 or val > 32767:
23+
if val < -32768 or val > 32767:
2424
raise ValueError(
2525
'integer too large to be encoded as INT2: {!r}'.format(val))
2626

tests/test_codecs.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -37,15 +37,15 @@ def _timezone(offset):
3737
True, False,
3838
)),
3939
('smallint', 'int2', (
40-
-2 ** 15 + 1, 2 ** 15 - 1,
40+
-2 ** 15, 2 ** 15 - 1,
4141
-1, 0, 1,
4242
)),
4343
('int', 'int4', (
44-
-2 ** 31 + 1, 2 ** 31 - 1,
44+
-2 ** 31, 2 ** 31 - 1,
4545
-1, 0, 1,
4646
)),
4747
('bigint', 'int8', (
48-
-2 ** 63 + 1, 2 ** 63 - 1,
48+
-2 ** 63, 2 ** 63 - 1,
4949
-1, 0, 1,
5050
)),
5151
('numeric', 'numeric', (
@@ -474,7 +474,7 @@ async def test_invalid_input(self):
474474
]),
475475
('smallint', ValueError, 'integer too large', [
476476
32768,
477-
-32768
477+
-32769
478478
]),
479479
('float4', ValueError, 'float value too large', [
480480
4.1 * 10 ** 40,

0 commit comments

Comments
 (0)