Path: csiph.com!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder2.enfer-du-nord.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!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.013 X-Spam-Evidence: '*H*': 0.97; '*S*': 0.00; 'imports': 0.07; 'called.': 0.09; 'def': 0.13; '16,': 0.15; 'module:': 0.16; 'subject:import': 0.16; '\xa0def': 0.16; 'cc:addr:python-list': 0.16; 'wrote:': 0.18; 'seems': 0.20; 'cheers,': 0.20; 'cc:no real name:2**0': 0.21; "doesn't": 0.22; 'header:In-Reply-To:1': 0.22; 'cc:2**0': 0.26; 'skip:_ 20': 0.26; 'import': 0.27; 'message- id:@mail.gmail.com': 0.29; 'cc:addr:python.org': 0.29; 'pm,': 0.29; "didn't": 0.30; 'actually': 0.31; 'version': 0.32; 'instead.': 0.32; 'idea': 0.32; 'it.': 0.33; 'fri,': 0.34; 'function.': 0.34; 'stores': 0.34; 'element': 0.37; 'created': 0.37; 'received:google.com': 0.37; 'received:209.85': 0.38; 'replace': 0.38; 'skip:\xa0 10': 0.39; 'received:209': 0.39; 'called': 0.40; 'andrea': 0.84; 'hints?': 0.84 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc:content-type:content-transfer-encoding; bh=+z1vkxKP85vf2w2ng1967ah+pPAT8BhhqI3fLZ13u+A=; b=GlGzNtGtnChKlBtuptNNJSCTbzAZMcLilHFHqfA1F1xPhdROvCZsafd38rQQwkMqrR joC949qNm4YPu/YwrmTjsGjHllrhi31g/8w0/cBfYrD3loH/M+wQLOaqEsDvObqywWb7 LcZkiw60jP1sMstn6ZHTqu2eAhDpFfq8h10jYTdOAdO3xBNN3G3xMteByqpBb+9zbB5n Xhof60L4iDek2UVlSPEYUMy9GArLjwDnL8C51M68HcIfFYa46j3eEd57QJy2PMYyD5pJ HSTo+mfd2xvOCYVommDJOUaTgLaxZdNhmXq1r/W0SparML4hKIOYCo3is4t+dALt3gd+ 8GFA== MIME-Version: 1.0 In-Reply-To: <4F63B8D4.8010103@gmail.com> References: <4F636F0D.3060006@gmail.com> <4F63B8D4.8010103@gmail.com> From: Ian Kelly Date: Fri, 16 Mar 2012 16:18:07 -0600 Subject: Re: avoid import short-circuiting To: Andrea Crotti Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Cc: python-list@python.org 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: 38 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1331936319 news.xs4all.nl 6858 [2001:888:2000:d::a6]:56654 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:21784 On Fri, Mar 16, 2012 at 4:04 PM, Andrea Crotti wrote: >> You want to monkeypatch __builtin__.__import__() instead. It always gets >> called. >> > > Seems like a good idea :) > > My first attempt failes though > > > def full(module): > =A0 =A0from __builtin__ import __import__ > =A0 =A0ls =3D [] > =A0 =A0orig =3D __import__ > > =A0 =A0def my_import(name): > =A0 =A0 =A0 =A0ls.append(name) > =A0 =A0 =A0 =A0orig(name) > > =A0 =A0__import__ =3D my_import > =A0 =A0__import__(module) > =A0 =A0__import__ =3D orig > =A0 =A0return ls > > > it imports only the first element and doesn't import the dependencies.. > Any hints? You didn't actually monkey-patch it. You just created a local called __import__ that stores a wrapped version of the function. You need to actually replace it in the __builtin__ module: import __builtin__ __builtin__.__import__ =3D my_import Cheers, Ian