phihag / ipaddress

Python 3.3+'s ipaddress for older Python versions
Other
105 stars 53 forks source link

unicode/bytes behavior is inconvenient #36

Closed jaseg closed 6 years ago

jaseg commented 6 years ago

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:

phihag commented 6 years ago

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.