MCPcopy Index your code
hub / github.com/python/cpython / _prefix_from_ip_int

Method _prefix_from_ip_int

Lib/ipaddress.py:437–459  ·  view source on GitHub ↗

Return prefix length from the bitwise netmask. Args: ip_int: An integer, the netmask in expanded bitwise format Returns: An integer, the prefix length. Raises: ValueError: If the input intermingles zeroes & ones

(cls, ip_int)

Source from the content-addressed store, hash-verified

435
436 @classmethod
437 def _prefix_from_ip_int(cls, ip_int):
438 """Return prefix length from the bitwise netmask.
439
440 Args:
441 ip_int: An integer, the netmask in expanded bitwise format
442
443 Returns:
444 An integer, the prefix length.
445
446 Raises:
447 ValueError: If the input intermingles zeroes & ones
448 """
449 trailing_zeroes = _count_righthand_zero_bits(ip_int,
450 cls.max_prefixlen)
451 prefixlen = cls.max_prefixlen - trailing_zeroes
452 leading_ones = ip_int >> trailing_zeroes
453 all_ones = (1 << prefixlen) - 1
454 if leading_ones != all_ones:
455 byteslen = cls.max_prefixlen // 8
456 details = ip_int.to_bytes(byteslen, 'big')
457 msg = 'Netmask pattern %r mixes zeroes & ones'
458 raise ValueError(msg % details)
459 return prefixlen
460
461 @classmethod
462 def _report_invalid_netmask(cls, netmask_str):

Callers 1

Calls 1

Tested by

no test coverage detected