Skip to content

Commit ccc797b

Browse files
author
James William Pye
committed
Default the mask to max per Postgres' functionality.
Patch submitted by Barry Grussling.
1 parent 1d29023 commit ccc797b

File tree

2 files changed

+6
-4
lines changed

2 files changed

+6
-4
lines changed

postgresql/test/test_types.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,8 @@ def decode(x):
235235
(0xffffffff, 0),
236236
(0xffffffff // 2, 0xffff // 2),
237237
],
238-
238+
}
239+
__ = {
239240
('cidr', typlib.net_pack, typlib.net_unpack) : [
240241
(0, 0, b"\x00\x00\x00\x00"),
241242
(2, 0, b"\x00" * 4),

postgresql/types/io/lib.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -286,14 +286,14 @@ def net_pack(inet, len = len):
286286
Supports cidr and inet types.
287287
"""
288288
slash_index = inet.find('/')
289+
mask = None
289290
if slash_index >= 0:
290291
try:
291292
mask = int(inet[slash_index+1:])
292293
except ValueError:
293294
raise ValueError('invalid mask in inet/cidr')
294295
address = inet[:slash_index]
295296
else:
296-
mask = 0
297297
address = inet
298298
if inet.find(':') >= 0:
299299
family = _PGSQL_AF_INET6
@@ -311,7 +311,7 @@ def net_pack(inet, len = len):
311311
data = inet_pton(posix_family, address)
312312
except socket_error as exc:
313313
raise ValueError(str(exc)) from exc
314-
if mask:
314+
if mask is not None:
315315
if not (0 <= mask <= max_mask):
316316
raise ValueError('invalid mask in inet/cidr')
317317
# Calculate optional cidr byte - PGSQL ignores this on input
@@ -321,6 +321,7 @@ def net_pack(inet, len = len):
321321
is_cidr = 1 if (i_net == i_address) else 0
322322
else:
323323
is_cidr = 0
324+
mask = max_mask
324325
return bytes((family, mask, is_cidr, len(data))) + data
325326

326327
def net_unpack(data, cidr=False, len=len):
@@ -351,7 +352,7 @@ def net_unpack(data, cidr=False, len=len):
351352
raise ValueError(str(exc)) from exc
352353
if not (0 <= mask <= max_mask):
353354
raise ValueError("invalid mask parameter")
354-
if cidr or mask:
355+
if cidr or (mask and mask != max_mask):
355356
result = address + '/' + str(mask)
356357
else:
357358
result = address

0 commit comments

Comments
 (0)