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

Attachment: signature.asc
Description: OpenPGP digital signature

_______________________________________________
Distutils-SIG maillist  -  [email protected]
https://mail.python.org/mailman/listinfo/distutils-sig

Reply via email to