Path: csiph.com!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed2a.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'incompatible': 0.07; 'subject:code': 0.07; 'ugly': 0.07; 'ambiguity': 0.09; 'function,': 0.09; 'literal': 0.09; 'parameter': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'rejected': 0.09; 'satisfy': 0.09; 'subject:string': 0.09; 'subtle': 0.09; 'python': 0.11; 'bug': 0.12; '__future__': 0.16; 'buggy': 0.16; 'finney': 0.16; 'rather,': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'subject:between': 0.16; 'all.': 0.16; 'wrote:': 0.18; 'code.': 0.18; 'import': 0.22; 'cloud': 0.22; 'header:User-Agent:1': 0.23; 'bytes': 0.24; 'unicode': 0.24; 'header:X-Complaints-To:1': 0.27; 'point': 0.28; 'fixed': 0.29; "i'm": 0.30; 'code': 0.31; 'base,': 0.31; 'writes:': 0.31; 'compatible': 0.32; 'up.': 0.33; 'worked': 0.33; 'call.': 0.33; 'problem': 0.35; "can't": 0.35; 'computing': 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; 'though,': 0.39; 'to:addr:python.org': 0.39; 'either': 0.39; 'received:org': 0.40; 'according': 0.40; 'how': 0.40; 'skip:u 10': 0.60; 'around.': 0.60; 'simple': 0.61; 'such': 0.63; 'our': 0.64; 'skip:\xe2 10': 0.65; '8bit%:43': 0.74; '\xe2\x80\x93': 0.77; 'sole': 0.78; '8bit%:16': 0.84; 'ethan': 0.84; 'furman': 0.84; 'increasingly': 0.84; 'received:125': 0.84 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Ben Finney Subject: Re: Portable code: __import__ demands different string types between 2 and 3 Date: Tue, 16 Dec 2014 12:36:04 +1100 References: <85wq5tpe0g.fsf@benfinney.id.au> <548F18FC.5040408@stoneleaf.us> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Gmane-NNTP-Posting-Host: jigong.madmonks.org User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/24.4 (gnu/linux) X-Public-Key-ID: 0xAC128405 X-Public-Key-Fingerprint: 517C F14B B2F3 98B0 CB35 4855 B8B2 4C06 AC12 8405 X-Public-Key-URL: http://www.benfinney.id.au/contact/bfinney-pubkey.asc X-Post-From: Ben Finney Cancel-Lock: sha1:GXjFRZo2Q9ZeKFIhLJ99MhYG1y8= X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 35 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1418693773 news.xs4all.nl 2877 [2001:888:2000:d::a6]:54603 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:82570 Ethan Furman 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