phihag / ipaddress

Python 3.3+'s ipaddress for older Python versions

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

unicode/bytes behavior is inconvenient

jaseg opened this issue · comments

commented

Hi,

I have this piece of software that gives me an str with an ip address on python 2 as well as python 3. passing that into ipaddress, ipaddress barfs on python 2. This is inconvenient for two reasons:

  • I would have to add logic for the python 2 special case
  • Since ipv4 and ipv6 addresses are by definition only made up of a very limited subset of ASCII, there is no point in requiring anyone to use unicode in the first place.

I concur that it's inconvenient if your code is confused between byte strings (bytes in Python 3, bytes and its alias str in Python 2) and character strings (str in Python 3, unicode in Python 2).

But the alternative would be far worse: If this backport would accept bytes, that means if somebody fixes or amends your legacy code and uses the packed representation (bytes both on Python 2 and Python 3), then this backport would silently behave differently between Python 2 and Python 3 - if you're lucky, just throwing an exception, but in the worst case returning different results.

As much of the code using ipaddress is security-sensitive (firewalls, network management, etc.), unexpected behavior can have disastrous consequences.

Therefore, I must sadly close this report. You can either fix your code to use the correct data types on Python 2 and 3, or, if you are certain that a value is the representation of an ASCII-only byte string on Python 2 and a character string on Python 3, call six.u on it to get a character string all the time.