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

Method _prefix_from_ip_string

Lib/ipaddress.py:492–523  ·  view source on GitHub ↗

Turn a netmask/hostmask string into a prefix length Args: ip_str: The netmask/hostmask to be converted Returns: An integer, the prefix length. Raises: NetmaskValueError: If the input is not a valid netmask/hostmask

(cls, ip_str)

Source from the content-addressed store, hash-verified

490
491 @classmethod
492 def _prefix_from_ip_string(cls, ip_str):
493 """Turn a netmask/hostmask string into a prefix length
494
495 Args:
496 ip_str: The netmask/hostmask to be converted
497
498 Returns:
499 An integer, the prefix length.
500
501 Raises:
502 NetmaskValueError: If the input is not a valid netmask/hostmask
503 """
504 # Parse the netmask/hostmask like an IP address.
505 try:
506 ip_int = cls._ip_int_from_string(ip_str)
507 except AddressValueError:
508 cls._report_invalid_netmask(ip_str)
509
510 # Try matching a netmask (this would be /1*0*/ as a bitwise regexp).
511 # Note that the two ambiguous cases (all-ones and all-zeroes) are
512 # treated as netmasks.
513 try:
514 return cls._prefix_from_ip_int(ip_int)
515 except ValueError:
516 pass
517
518 # Invert the bits, and try matching a /0+1+/ hostmask instead.
519 ip_int ^= cls._ALL_ONES
520 try:
521 return cls._prefix_from_ip_int(ip_int)
522 except ValueError:
523 cls._report_invalid_netmask(ip_str)
524
525 @classmethod
526 def _split_addr_prefix(cls, address):

Callers 1

_make_netmaskMethod · 0.80

Calls 3

_prefix_from_ip_intMethod · 0.80
_ip_int_from_stringMethod · 0.45

Tested by

no test coverage detected