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: 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 Subject: Re: avoid import short-circuiting Date: Sat, 17 Mar 2012 01:22:05 +0000 References: <4F636F0D.3060006@gmail.com> <4F63B8D4.8010103@gmail.com> <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 List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: 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 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