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


Groups > comp.lang.python > #21789

Re: avoid import short-circuiting

Path csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!de-l.enfer-du-nord.net!feeder2.enfer-du-nord.net!newsfeed.eweka.nl!eweka.nl!feeder3.eweka.nl!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.007
X-Spam-Evidence '*H*': 0.99; '*S*': 0.00; 'bug': 0.02; 'imports': 0.07; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'underlying': 0.09; 'def': 0.13; 'packages.': 0.13; 'argument': 0.15; '**kwargs)': 0.16; '**kwargs):': 0.16; '*args,': 0.16; 'enigma': 0.16; 'kern': 0.16; 'subject:import': 0.16; 'wrote:': 0.18; 'thanks,': 0.19; 'header:In-Reply-To:1': 0.22; 'module': 0.26; 'interpret': 0.29; 'class': 0.29; 'example': 0.29; 'pm,': 0.29; 'header:User-Agent:1': 0.33; 'instead': 0.33; 'object': 0.33; 'header:X-Complaints-To:1': 0.34; 'calling': 0.34; 'to:addr :python-list': 0.35; 'received:org': 0.36; 'passed': 0.37; 'skip:_ 10': 0.38; 'getting': 0.38; 'should': 0.38; 'to:addr:python.org': 0.40; 'follow': 0.61; 'world': 0.61; 'received:86': 0.63; 'our': 0.63; 'here.': 0.64; 'here': 0.64; 'believe': 0.65; 'account': 0.66; '11:14': 0.84; 'andrea': 0.84; 'eco': 0.91
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Robert Kern <robert.kern@gmail.com>
Subject Re: avoid import short-circuiting
Date Sat, 17 Mar 2012 01:22:05 +0000
References <4F636F0D.3060006@gmail.com> <jjvsnj$v9d$1@dough.gmane.org> <4F63B8D4.8010103@gmail.com> <jk0eav$b5l$1@dough.gmane.org> <4F63C948.8020700@gmail.com>
Mime-Version 1.0
Content-Type text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding 7bit
X-Gmane-NNTP-Posting-Host cpc24-cmbg15-2-0-cust204.5-4.cable.virginmedia.com
User-Agent Mozilla/5.0 (Macintosh; Intel Mac OS X 10.7; rv:10.0.2) Gecko/20120216 Thunderbird/10.0.2
In-Reply-To <4F63C948.8020700@gmail.com>
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.12
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://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 <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.745.1331947337.3037.python-list@python.org> (permalink)
Lines 23
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1331947337 news.xs4all.nl 6841 [2001:888:2000:d::a6]:34053
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:21789

Show key headers only | View raw


On 3/16/12 11:14 PM, Andrea Crotti wrote:

> Very nice thanks, here it is
> class ImportMock:
>
>     def _my_import(self, *args, **kwargs):
>         self.ls.append(args[0])
>         self.orig(*args, **kwargs)

There's a bug here. You need to return the module object you got from calling 
self.orig(). By the way, you really should follow my example of getting the 
.__name__ from the module object instead of the argument in order to properly 
account for relative imports inside packages. __import__() will be passed the 
relative name, not the fully-qualified name.

-- 
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
  that is made terrible by our own mad attempt to interpret it as though it had
  an underlying truth."
   -- Umberto Eco

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


Thread

Re: avoid import short-circuiting Robert Kern <robert.kern@gmail.com> - 2012-03-17 01:22 +0000

csiph-web