Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #82570 > unrolled thread

Re: Portable code: __import__ demands different string types between 2 and 3

Started byBen Finney <ben+python@benfinney.id.au>
First post2014-12-16 12:36 +1100
Last post2014-12-16 14:07 +1100
Articles 3 — 2 participants

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: Portable code: __import__ demands different string types between 2 and 3 Ben Finney <ben+python@benfinney.id.au> - 2014-12-16 12:36 +1100
    Re: Portable code: __import__ demands different string types between 2 and 3 Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-12-16 13:32 +1100
      Re: Portable code: __import__ demands different string types between 2 and 3 Ben Finney <ben+python@benfinney.id.au> - 2014-12-16 14:07 +1100

#82570 — Re: Portable code: __import__ demands different string types between 2 and 3

FromBen Finney <ben+python@benfinney.id.au>
Date2014-12-16 12:36 +1100
SubjectRe: Portable code: __import__ demands different string types between 2 and 3
Message-ID<mailman.16991.1418693773.18130.python-list@python.org>
Ethan Furman <ethan@stoneleaf.us> writes:

> On 12/14/2014 11:29 PM, Ben Finney wrote:
> > The ‘__import__’ built-in function, though, is tripping up.
>
> One work-around I have used is:
>
>   if isinstance(some_var, bytes):
>       some_var = some_var.decode('ascii')
>   # at this point some_var is unicode in both Pythons

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.

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.

I'm increasingly of the opinion this is a subtle bug in ‘__import__’
that should be fixed instead of worked around. But it will likely be
rejected because the documentation advises against using ‘__import__’.
Bah.

-- 
 \     “Try adding “as long as you don't breach the terms of service – |
  `\          according to our sole judgement” to the end of any cloud |
_o__)                      computing pitch.” —Simon Phipps, 2010-12-11 |
Ben Finney

[toc] | [next] | [standalone]


#82573

FromSteven D'Aprano <steve+comp.lang.python@pearwood.info>
Date2014-12-16 13:32 +1100
Message-ID<548f99a5$0$12980$c3e8da3$5496439d@news.astraweb.com>
In reply to#82570
Ben Finney wrote:

> I'm increasingly of the opinion this is a subtle bug in ‘__import__’
> that should be fixed instead of worked around. But it will likely be
> rejected because the documentation advises against using ‘__import__’.
> Bah.

I think its worth raising an issue on the bug tracker and see what the core
devs think.

If you don't have and won't open an account on the tracker, if you can come
up with a minimal example that demonstrates the issue, I'll open an issue
for you. (But I'd rather you did it yourself :-)



-- 
Steven

[toc] | [prev] | [next] | [standalone]


#82575

FromBen Finney <ben+python@benfinney.id.au>
Date2014-12-16 14:07 +1100
Message-ID<mailman.16995.1418699230.18130.python-list@python.org>
In reply to#82573
Steven D'Aprano <steve+comp.lang.python@pearwood.info> writes:

> If you don't have and won't open an account on the tracker, if you can
> come up with a minimal example that demonstrates the issue, I'll open
> an issue for you. (But I'd rather you did it yourself :-)

Thank you for the offer. Fortunately, the Python bug tracker does not
require me to maintain credentials for that site (I can use any OpenID),
so I've got an account there.

-- 
 \     “Our task must be to free ourselves from our prison by widening |
  `\    our circle of compassion to embrace all humanity and the whole |
_o__)                       of nature in its beauty.” —Albert Einstein |
Ben Finney

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web