Portable code: __import__ demands different string types between 2 and 3
Ethan Furman
ethan at stoneleaf.us
Mon Dec 15 20:49:14 EST 2014
On 12/15/2014 05:36 PM, Ben Finney wrote:
>
> That's not the issue at all. I know how to declare a literal such that
> it is Unicode in Python 2 and Python 3 (that's what the ‘from __future__
> import unicode_literals’ does).
>
> Rather, the problem is ‘__import__’ having incompatible expectations:
> the ‘fromlist’ parameter's items must be bytes in Python 2, and must be
> Unicode in Python 3.
Ah. Well, then you do not want the `unicode_literals` import or it won't be bytes in Python 2 (as I'm sure you know).
> The same parameter value can't satisfy both those requirements in a
> single 2-and-3 compatible code base, without either using the
> bytes-and-text ambiguity of ‘str’, or some kludge over-riding a simple
> ‘__import__’ call. Both of those are ugly and make for buggy code.
If this is for a large(ish) application, make one module with stuff that needs the ambiguity to work correctly. Comment
the heck out of it. ;)
> I'm increasingly of the opinion this is a subtle bug in ‘__import__’
> that should be fixed instead of worked around.
Of course. But you'll still need to work around it for previous versions, unless you can say you only support 2.7.10+
(maybe 2.7.9+ if it gets fixed quick enough).
> But it will likely be rejected because the documentation advises against using ‘__import__’.
Functions that should accept str but barf on unicode have a tendency to get fixed.
--
~Ethan~
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 836 bytes
Desc: OpenPGP digital signature
URL: <http://mail.python.org/pipermail/python-list/attachments/20141215/1f2305db/attachment.sig>
More information about the Python-list
mailing list