python / cpython

The Python programming language
https://www.python.org
Other
62.38k stars 29.96k forks source link

Inconsistent behavior when parsing IP address #80001

Open d75465a0-03f0-400b-be73-3acf509a5c12 opened 5 years ago

d75465a0-03f0-400b-be73-3acf509a5c12 commented 5 years ago
BPO 35820
Nosy @ericvsmith, @tinutomson

Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

Show more details

GitHub fields: ```python assignee = None closed_at = None created_at = labels = ['type-bug', 'library'] title = 'Inconsistent behavior when parsing IP address' updated_at = user = 'https://github.com/tinutomson' ``` bugs.python.org fields: ```python activity = actor = 'eric.smith' assignee = 'none' closed = False closed_date = None closer = None components = ['Library (Lib)'] creation = creator = 'tinutomson' dependencies = [] files = [] hgrepos = [] issue_num = 35820 keywords = [] message_count = 2.0 messages = ['334319', '334323'] nosy_count = 2.0 nosy_names = ['eric.smith', 'tinutomson'] pr_nums = [] priority = 'normal' resolution = None stage = None status = 'open' superseder = None type = 'behavior' url = 'https://bugs.python.org/issue35820' versions = [] ```

d75465a0-03f0-400b-be73-3acf509a5c12 commented 5 years ago
ip = '23.00.021.002'
ipaddress.IPv4Address(ip)
throw error:
  File "/usr/lib/python3.4/ipaddress.py", line 1271, in __init__                                                                                             
    self._ip = self._ip_int_from_string(addr_str)                                                                                                            
  File "/usr/lib/python3.4/ipaddress.py", line 1122, in _ip_int_from_string                                                                                  
    raise AddressValueError("%s in %r" % (exc, ip_str)) from None                                                                                            
ipaddress.AddressValueError: Ambiguous (octal/decimal) value in '021' not permitted in '23.00.021.002'          

ip = '23.00.21.002'
ipaddress.IPv4Address(ip)

parses correctly.

ericvsmith commented 5 years ago

From the documentation of IPv4Address:

The following constitutes a valid IPv4 address:

  1. A string in decimal-dot notation, consisting of four decimal integers in the inclusive range 0–255, separated by dots (e.g. 192.168.0.1). Each integer represents an octet (byte) in the address. Leading zeroes are tolerated only for values less than 8 (as there is no ambiguity between the decimal and octal interpretations of such strings).

21 is not less than 8, so you get this error. I think this is working as it's documented.