Skip to content

Commit 5a4daf7

Browse files
elprans1st1
authored andcommitted
Handle IP values with prefix in "inet" type as IPvXInterface
Currently we treat values with network prefix in `inet` values as `IPvXNetwork`, instead of `IPvXInterface` which is wrong. Fixes: #497
1 parent 71dc902 commit 5a4daf7

File tree

4 files changed

+19
-16
lines changed

4 files changed

+19
-16
lines changed

asyncpg/protocol/codecs/pgproto.pyx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -382,12 +382,12 @@ cdef init_numeric_codecs():
382382
cdef init_network_codecs():
383383
register_core_codec(CIDROID,
384384
<encode_func>pgproto.cidr_encode,
385-
<decode_func>pgproto.net_decode,
385+
<decode_func>pgproto.cidr_decode,
386386
PG_FORMAT_BINARY)
387387

388388
register_core_codec(INETOID,
389389
<encode_func>pgproto.inet_encode,
390-
<decode_func>pgproto.net_decode,
390+
<decode_func>pgproto.inet_decode,
391391
PG_FORMAT_BINARY)
392392

393393
register_core_codec(MACADDROID,

docs/usage.rst

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -97,14 +97,14 @@ The table below shows the correspondence between PostgreSQL and Python types.
9797
| | :class:`ipaddress.IPv6Network\ |
9898
| | <python:ipaddress.IPv6Network>` |
9999
+----------------------+-----------------------------------------------------+
100-
| ``inet`` | :class:`ipaddress.IPv4Network\ |
101-
| | <python:ipaddress.IPv4Network>`, |
102-
| | :class:`ipaddress.IPv6Network\ |
103-
| | <python:ipaddress.IPv6Network>`, |
100+
| ``inet`` | :class:`ipaddress.IPv4Interface\ |
101+
| | <python:ipaddress.IPv4Interface>`, |
102+
| | :class:`ipaddress.IPv6Interface\ |
103+
| | <python:ipaddress.IPv6Interface>`, |
104104
| | :class:`ipaddress.IPv4Address\ |
105105
| | <python:ipaddress.IPv4Address>`, |
106106
| | :class:`ipaddress.IPv6Address\ |
107-
| | <python:ipaddress.IPv6Address>` |
107+
| | <python:ipaddress.IPv6Address>` [#f1]_ |
108108
+----------------------+-----------------------------------------------------+
109109
| ``macaddr`` | :class:`str <python:str>` |
110110
+----------------------+-----------------------------------------------------+
@@ -127,7 +127,7 @@ The table below shows the correspondence between PostgreSQL and Python types.
127127
| ``interval`` | :class:`datetime.timedelta \ |
128128
| | <python:datetime.timedelta>` |
129129
+----------------------+-----------------------------------------------------+
130-
| ``float``, | :class:`float <python:float>` [#f1]_ |
130+
| ``float``, | :class:`float <python:float>` [#f2]_ |
131131
| ``double precision`` | |
132132
+----------------------+-----------------------------------------------------+
133133
| ``smallint``, | :class:`int <python:int>` |
@@ -158,7 +158,10 @@ The table below shows the correspondence between PostgreSQL and Python types.
158158

159159
All other types are encoded and decoded as text by default.
160160

161-
.. [#f1] Inexact single-precision ``float`` values may have a different
161+
.. [#f1] Prior to version 0.20.0, asyncpg erroneously treated ``inet`` values
162+
with prefix as ``IPvXNetwork`` instead of ``IPvXInterface``.
163+
164+
.. [#f2] Inexact single-precision ``float`` values may have a different
162165
representation when decoded into a Python float. This is inherent
163166
to the implementation of limited-precision floating point types.
164167
If you need the decimal representation to match, cast the expression

tests/test_codecs.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -341,9 +341,12 @@ def _system_timezone():
341341
ipaddress.IPv6Address('ffff' + ':ffff' * 7),
342342
ipaddress.IPv6Address('::1'),
343343
ipaddress.IPv6Address('::'),
344+
ipaddress.IPv4Interface('10.0.0.1/30'),
345+
ipaddress.IPv4Interface('0.0.0.0/0'),
346+
ipaddress.IPv4Interface('255.255.255.255/31'),
344347
dict(
345348
input='127.0.0.0/8',
346-
output=ipaddress.IPv4Network('127.0.0.0/8')),
349+
output=ipaddress.IPv4Interface('127.0.0.0/8')),
347350
dict(
348351
input='127.0.0.1/32',
349352
output=ipaddress.IPv4Address('127.0.0.1')),
@@ -358,19 +361,16 @@ def _system_timezone():
358361
textoutput='10.11.12.13/32'
359362
),
360363
dict(
361-
input=ipaddress.IPv4Network('10.11.12.13'),
364+
input=ipaddress.IPv4Interface('10.11.12.13'),
362365
textoutput='10.11.12.13/32'
363366
),
364367
dict(
365368
textinput='10.11.12.13',
366369
output=ipaddress.IPv4Address('10.11.12.13'),
367370
),
368371
dict(
369-
# Non-zero address bits after the network prefix are permitted
370-
# by postgres, but are invalid in Python
371-
# (and zeroed out by supernet()).
372372
textinput='10.11.12.13/0',
373-
output=ipaddress.IPv4Network('0.0.0.0/0'),
373+
output=ipaddress.IPv4Interface('10.11.12.13/0'),
374374
),
375375
]),
376376
('macaddr', 'macaddr', [

0 commit comments

Comments
 (0)