Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!backfill.erje.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed4a.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; 'explicitly': 0.05; 'attribute': 0.07; 'python3': 0.07; 'subject:code': 0.07; 'versions,': 0.07; 'string': 0.09; 'advice?': 0.09; 'default.': 0.09; 'function,': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'subject:string': 0.09; 'runs': 0.10; 'python': 0.11; 'bug': 0.12; '__future__': 0.16; '``from': 0.16; 'above)': 0.16; 'finney': 0.16; 'literals': 0.16; 'mkdir': 0.16; 'presume': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'subject:between': 0.16; 'typeerror:': 0.16; 'do,': 0.16; 'all,': 0.19; 'import': 0.22; 'header:User-Agent:1': 0.23; 'bytes': 0.24; 'specify': 0.24; 'unicode': 0.24; 'fine': 0.24; 'skip:_ 20': 0.27; 'header:X -Complaints-To:1': 0.27; 'fixed': 0.29; 'needed.': 0.30; "i'm": 0.30; 'code': 0.31; '"",': 0.31; '3.2': 0.31; 'assert': 0.31; 'concern': 0.31; 'default,': 0.31; 'trivial': 0.31; 'file': 0.32; 'run': 0.32; 'text': 0.33; 'up.': 0.33; '(most': 0.33; 'skip:_ 10': 0.34; 'but': 0.35; '2.6': 0.36; 'should': 0.36; 'behind': 0.37; 'project': 0.37; 'ben': 0.38; 'needed': 0.38; 'to:addr :python-list': 0.38; 'recent': 0.39; 'though,': 0.39; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'how': 0.40; 'ensure': 0.60; 'skip:u 10': 0.60; 'applicable': 0.60; 'authority': 0.60; 'slowly': 0.60; 'simple': 0.61; 'complete': 0.62; 'making': 0.63; 'name': 0.63; 'kind': 0.63; 'skip:\xe2 10': 0.65; 'world': 0.66; 'between': 0.67; 'believe': 0.68; '8bit%:40': 0.68; '8bit%:27': 0.74; '8bit%:43': 0.74; "'foo'": 0.84; 'believe,': 0.84; 'received:125': 0.84; 'superhuman': 0.84 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Ben Finney Subject: Portable code: __import__ demands different string types between 2 and 3 Date: Mon, 15 Dec 2014 18:29:19 +1100 Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Gmane-NNTP-Posting-Host: jigong.madmonks.org 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 User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/24.4 (gnu/linux) Cancel-Lock: sha1:p4LUFA5SIySJs9w2QfOp52GXK2o= 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: 88 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1418628565 news.xs4all.nl 2854 [2001:888:2000:d::a6]:56411 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:82551 Howdy all, What should I do, in a world where all text literals are Unicode by default, to make ‘__import__’ work in both Python 2 and 3? I'm slowly learning about making Python code that will run under both Python 2 (version 2.6 or above) and Python 3 (version 3.2 or above). This entails, I believe, the admonition to ensure text literals are Unicode by default:: from __future__ import unicode_literals and to specify bytes literals explicitly with ‘b'wibble'’ if needed. The ‘__import__’ built-in function, though, is tripping up. A contrived, trivial project layout:: $ mkdir fooproject/ $ cd fooproject/ $ mkdir foo/ $ printf "" > foo/__init__.py $ mkdir foo/bar/ $ printf "" > foo/bar/__init__.py Here's a simple ‘fooproject/setup.py’:: from __future__ import unicode_literals main_module_name = 'foo' main_module = __import__(main_module_name, fromlist=['bar']) assert main_module.bar That fails under Python 2, but runs fine under Python 3:: $ python2 ./setup.py Traceback (most recent call last): File "./setup.py", line 4, in main_module = __import__(main_module_name, fromlist=['bar']) TypeError: Item in ``from list'' not a string $ python3 ./setup.py We've deliberately made unadorned strings Unicode by default. By “not a string”, I presume Python 2 means “not a ‘bytes’ object”. Okay, so we'll explicitly set that to a ‘bytes’ literal:: from __future__ import unicode_literals main_module_name = 'foo' main_module = __import__(main_module_name, fromlist=[b'bar']) assert main_module.bar Now Python 2 is satisfied, but Python 3 complains:: $ python2 ./setup.py $ python3 ./setup.py Traceback (most recent call last): File "./setup.py", line 4, in main_module = __import__(main_module_name, fromlist=[b'bar']) File "", line 2281, in _handle_fromlist TypeError: hasattr(): attribute name must be string How can I get that ‘__import__’ call, complete with its ‘fromlist’ parameter, working correctly under both Python 2 and Python 3, keeping the ‘unicode_literals’ setting? If some kind of kludge is needed to make it work between versions, is this a bug which should be fixed so “use Unicode for text” remains applicable advice? -- \ “I do not believe in immortality of the individual, and I | `\ consider ethics to be an exclusively human concern with no | _o__) superhuman authority behind it.” —Albert Einstein, letter, 1953 | Ben Finney