MCPcopy Index your code
hub / github.com/RustPython/RustPython / _ip_int_from_string

Method _ip_int_from_string

Lib/ipaddress.py:1648–1758  ·  view source on GitHub ↗

Turn an IPv6 ip_str into an integer. Args: ip_str: A string, the IPv6 ip_str. Returns: An int, the IPv6 address Raises: AddressValueError: if ip_str isn't a valid IPv6 Address.

(cls, ip_str)

Source from the content-addressed store, hash-verified

1646
1647 @classmethod
1648 def _ip_int_from_string(cls, ip_str):
1649 """Turn an IPv6 ip_str into an integer.
1650
1651 Args:
1652 ip_str: A string, the IPv6 ip_str.
1653
1654 Returns:
1655 An int, the IPv6 address
1656
1657 Raises:
1658 AddressValueError: if ip_str isn't a valid IPv6 Address.
1659
1660 """
1661 if not ip_str:
1662 raise AddressValueError('Address cannot be empty')
1663 if len(ip_str) > 45:
1664 shorten = ip_str
1665 if len(shorten) > 100:
1666 shorten = f'{ip_str[:45]}({len(ip_str)-90} chars elided){ip_str[-45:]}'
1667 raise AddressValueError(f"At most 45 characters expected in "
1668 f"{shorten!r}")
1669
1670 # We want to allow more parts than the max to be 'split'
1671 # to preserve the correct error message when there are
1672 # too many parts combined with '::'
1673 _max_parts = cls._HEXTET_COUNT + 1
1674 parts = ip_str.split(':', maxsplit=_max_parts)
1675
1676 # An IPv6 address needs at least 2 colons (3 parts).
1677 _min_parts = 3
1678 if len(parts) < _min_parts:
1679 msg = "At least %d parts expected in %r" % (_min_parts, ip_str)
1680 raise AddressValueError(msg)
1681
1682 # If the address has an IPv4-style suffix, convert it to hexadecimal.
1683 if '.' in parts[-1]:
1684 try:
1685 ipv4_int = IPv4Address(parts.pop())._ip
1686 except AddressValueError as exc:
1687 raise AddressValueError("%s in %r" % (exc, ip_str)) from None
1688 parts.append('%x' % ((ipv4_int >> 16) & 0xFFFF))
1689 parts.append('%x' % (ipv4_int & 0xFFFF))
1690
1691 # An IPv6 address can't have more than 8 colons (9 parts).
1692 # The extra colon comes from using the "::" notation for a single
1693 # leading or trailing zero part.
1694 if len(parts) > _max_parts:
1695 msg = "At most %d colons permitted in %r" % (_max_parts-1, ip_str)
1696 raise AddressValueError(msg)
1697
1698 # Disregarding the endpoints, find '::' with nothing in between.
1699 # This indicates that a run of zeroes has been skipped.
1700 skip_index = None
1701 for i in range(1, len(parts) - 1):
1702 if not parts[i]:
1703 if skip_index is not None:
1704 # Can't have more than one '::'
1705 msg = "At most one '::' permitted in %r" % ip_str

Callers 1

Calls 7

AddressValueErrorClass · 0.85
lenFunction · 0.85
IPv4AddressClass · 0.85
_parse_hextetMethod · 0.80
splitMethod · 0.45
popMethod · 0.45
appendMethod · 0.45

Tested by

no test coverage detected