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

Method _parse_octet

Lib/ipaddress.py:1209–1242  ·  view source on GitHub ↗

Convert a decimal octet into an integer. Args: octet_str: A string, the number to parse. Returns: The octet as an integer. Raises: ValueError: if the octet isn't strictly a decimal from [0..255].

(cls, octet_str)

Source from the content-addressed store, hash-verified

1207
1208 @classmethod
1209 def _parse_octet(cls, octet_str):
1210 """Convert a decimal octet into an integer.
1211
1212 Args:
1213 octet_str: A string, the number to parse.
1214
1215 Returns:
1216 The octet as an integer.
1217
1218 Raises:
1219 ValueError: if the octet isn't strictly a decimal from [0..255].
1220
1221 """
1222 if not octet_str:
1223 raise ValueError("Empty octet not permitted")
1224 # Reject non-ASCII digits.
1225 if not (octet_str.isascii() and octet_str.isdigit()):
1226 msg = "Only decimal digits permitted in %r"
1227 raise ValueError(msg % octet_str)
1228 # We do the length check second, since the invalid character error
1229 # is likely to be more informative for the user
1230 if len(octet_str) > 3:
1231 msg = "At most 3 characters permitted in %r"
1232 raise ValueError(msg % octet_str)
1233 # Handle leading zeros as strict as glibc's inet_pton()
1234 # See security bug bpo-36384
1235 if octet_str != '0' and octet_str[0] == '0':
1236 msg = "Leading zeros are not permitted in %r"
1237 raise ValueError(msg % octet_str)
1238 # Convert to integer (we know digits are legal)
1239 octet_int = int(octet_str, 10)
1240 if octet_int > 255:
1241 raise ValueError("Octet %d (> 255) not permitted" % octet_int)
1242 return octet_int
1243
1244 @classmethod
1245 def _string_from_ip_int(cls, ip_int):

Callers

nothing calls this directly

Calls 3

lenFunction · 0.85
isasciiMethod · 0.45
isdigitMethod · 0.45

Tested by

no test coverage detected