[Distutils] multiple backports of ipaddress and a world of pain

Philipp Hagemeister phihag at phihag.de
Wed Feb 17 03:30:52 EST 2016


On 17.02.2016 08:51, Chris Withers wrote:
> The code James uses as a result isn't that pretty, multiple occurrences of:
> 
> try:
>     value = unicode(value)
> except NameError:
>     pass

This is the last resort when you truly don't know what the input is, and
you are sure the string should only contain ASCII characters. Works fine
for ipaddress purposes.

> What would you recommend instead? When I monkeypatched this yesterday, I
> went with:
> 
> if isinstance(value, bytes):
>     value = value.decode('ascii')
> 
> ...but I wonder if there's something better?

Either is fine by me.

Preferably, one should go fix the source to return character strings in
the first place. Usually, this includes from __future__ import
unicode_literals, io.open with an encoding parameter, and inserting
unconditional decode calls when the source really is bytes.

I don't know enough about Python's postgres connector to give a better
solution. How does python-postgres handle character strings when they're
not all ASCII? If it's returning b'D\xc3\xbcsseldorf' on 2.x and
u'Düsseldorf' on 3.x, then you have to conditionally decode('utf-8') all
strings anyways in order to work on Python 3 as well as 2.x, haven't you?

Greetings from Düsseldorf,

Philipp

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 819 bytes
Desc: OpenPGP digital signature
URL: <http://mail.python.org/pipermail/distutils-sig/attachments/20160217/f1eda8ab/attachment.sig>


More information about the Distutils-SIG mailing list