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


Groups > comp.lang.python > #82571

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

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.mixmin.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed1.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <ethan@stoneleaf.us>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'incompatible': 0.07; 'skip:` 10': 0.07; 'subject:code': 0.07; 'ugly': 0.07; 'versions,': 0.07; 'ambiguity': 0.09; 'from:addr:ethan': 0.09; 'from:addr:stoneleaf.us': 0.09; 'from:name:ethan furman': 0.09; 'literal': 0.09; 'message-id:@stoneleaf.us': 0.09; 'parameter': 0.09; 'rejected': 0.09; 'satisfy': 0.09; 'subject:string': 0.09; 'subtle': 0.09; '~ethan~': 0.09; 'python': 0.11; 'bug': 0.12; '__future__': 0.16; 'buggy': 0.16; 'filename:fname piece:signature': 0.16; 'finney': 0.16; 'rather,': 0.16; 'subject: \n ': 0.16; 'subject:between': 0.16; 'all.': 0.16; 'wrote:': 0.18; 'code.': 0.18; 'module': 0.19; 'import': 0.22; 'header:User- Agent:1': 0.23; 'bytes': 0.24; 'fixed.': 0.24; 'unicode': 0.24; 'gets': 0.27; 'header:In-Reply-To:1': 0.27; 'fixed': 0.29; "i'm": 0.30; 'code': 0.31; '(maybe': 0.31; 'base,': 0.31; 'correctly.': 0.31; 'compatible': 0.32; 'stuff': 0.32; 'worked': 0.33; 'call.': 0.33; 'comment': 0.34; 'problem': 0.35; "can't": 0.35; 'but': 0.35; 'should': 0.36; 'requirements': 0.37; 'ben': 0.38; 'to:addr :python-list': 0.38; 'issue': 0.38; 'pm,': 0.38; 'previous': 0.38; 'sure': 0.39; 'to:addr:python.org': 0.39; 'either': 0.39; '8bit%:6': 0.40; 'how': 0.40; 'skip:u 10': 0.60; 'around.': 0.60; 'course.': 0.60; 'received:173': 0.61; 'simple': 0.61; "you'll": 0.62; 'such': 0.63; 'charset:windows-1252': 0.65; 'increasingly': 0.84; 'skip:\x91 10': 0.84
Date Mon, 15 Dec 2014 17:49:14 -0800
From Ethan Furman <ethan@stoneleaf.us>
User-Agent Mozilla/5.0 (X11; Linux x86_64; rv:24.0) Gecko/20100101 Thunderbird/24.5.0
MIME-Version 1.0
To python-list@python.org
Subject Re: Portable code: __import__ demands different string types between 2 and 3
References <85wq5tpe0g.fsf@benfinney.id.au> <548F18FC.5040408@stoneleaf.us> <85iohcpe9n.fsf@benfinney.id.au>
In-Reply-To <85iohcpe9n.fsf@benfinney.id.au>
Content-Type multipart/signed; micalg=pgp-sha1; protocol="application/pgp-signature"; boundary="7jCLbTOg1SqEJraUQdvOmwsOAv134dKtH"
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.16992.1418694593.18130.python-list@python.org> (permalink)
Lines 75
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1418694593 news.xs4all.nl 2875 [2001:888:2000:d::a6]:57512
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:82571

Show key headers only | View raw


[Multipart message — attachments visible in raw view] - view raw

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~

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: Portable code: __import__ demands different string types between 2 and 3 Ethan Furman <ethan@stoneleaf.us> - 2014-12-15 17:49 -0800

csiph-web