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


Groups > comp.lang.python > #21784

Re: avoid import short-circuiting

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 <ian.g.kelly@gmail.com>
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> <jjvsnj$v9d$1@dough.gmane.org> <4F63B8D4.8010103@gmail.com>
From Ian Kelly <ian.g.kelly@gmail.com>
Date Fri, 16 Mar 2012 16:18:07 -0600
Subject Re: avoid import short-circuiting
To Andrea Crotti <andrea.crotti.0@gmail.com>
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 <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.741.1331936319.3037.python-list@python.org> (permalink)
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

Show key headers only | View raw


On Fri, Mar 16, 2012 at 4:04 PM, Andrea Crotti
<andrea.crotti.0@gmail.com> 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):
>    from __builtin__ import __import__
>    ls = []
>    orig = __import__
>
>    def my_import(name):
>        ls.append(name)
>        orig(name)
>
>    __import__ = my_import
>    __import__(module)
>    __import__ = orig
>    return 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__ = my_import

Cheers,
Ian

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


Thread

Re: avoid import short-circuiting Ian Kelly <ian.g.kelly@gmail.com> - 2012-03-16 16:18 -0600

csiph-web