@@ -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
326327def 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