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


Groups > comp.lang.python > #21785

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 <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.001
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'context': 0.04; 'imports': 0.07; 'infinite': 0.07; 'mess': 0.07; 'called.': 0.09; 'compute': 0.09; 'hooks': 0.09; 'myself,': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'underlying': 0.09; 'url:enthought': 0.09; 'url:github': 0.09; 'def': 0.13; 'anymore.': 0.16; 'enigma': 0.16; 'kern': 0.16; 'subject:import': 0.16; 'examples': 0.16; 'wrote:': 0.18; '>>>': 0.18; 'this?': 0.19; 'seems': 0.20; 'trying': 0.21; "doesn't": 0.22; 'header:In-Reply-To:1': 0.22; 'guess': 0.26; 'module': 0.26; 'skip:_ 20': 0.26; 'import': 0.27; 'tried': 0.27; 'depends': 0.28; 'interpret': 0.29; 'problem': 0.29; 'pm,': 0.29; 'arguments.': 0.30; 'imported': 0.30; 'quite': 0.31; 'instead.': 0.32; 'idea': 0.32; 'there': 0.33; 'header:User-Agent:1': 0.33; 'too': 0.33; 'instead': 0.33; 'object': 0.33; 'header:X -Complaints-To:1': 0.34; 'loop': 0.34; 'yourself.': 0.34; 'to:addr :python-list': 0.35; 'received:org': 0.36; 'element': 0.37; 'but': 0.37; 'using': 0.37; 'skip:_ 10': 0.38; 'replace': 0.38; 'could': 0.38; 'should': 0.38; 'might': 0.40; 'to:addr:python.org': 0.40; 'easy': 0.60; 'custom': 0.61; 'achieve': 0.61; 'your': 0.61; 'world': 0.61; 'received:86': 0.63; 'our': 0.63; 'believe': 0.65; 'url:cgi': 0.68; '10:04': 0.84; 'andrea': 0.84; 'hints?': 0.84; 'hook': 0.84; 'eco': 0.91; 'points,': 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 Fri, 16 Mar 2012 22:20:15 +0000
References <4F636F0D.3060006@gmail.com> <jjvsnj$v9d$1@dough.gmane.org> <4F63B8D4.8010103@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 <4F63B8D4.8010103@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.742.1331936429.3037.python-list@python.org> (permalink)
Lines 81
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1331936430 news.xs4all.nl 6950 [2001:888:2000:d::a6]:57833
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:21785

Show key headers only | View raw


On 3/16/12 10:04 PM, Andrea Crotti wrote:
> On 03/16/2012 05:19 PM, Robert Kern wrote:
>> On 3/16/12 4:49 PM, Andrea Crotti wrote:
>>> I started the following small project:
>>>
>>> https://github.com/AndreaCrotti/import-tree
>>>
>>> because I would like to find out what exactly depends on what at run-time, using
>>> an import hook.
>>>
>>> It works quite well for small examples but the main problem is that once a
>>> module is imported
>>> it's added to sys.modules and then it doesn't go through the import hook
>>> anymore.
>>>
>>> I tried to mess around with sys.modules but it might not be a good idea, and it
>>> leads to easy
>>> infinite loops.
>>> Is there a good way to achieve this?
>>> I guess I could do the loop detection myself, but that should not be too hard..
>>
>> 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 need to replace it in __builtin__. Don't forget to handle all of the arguments.


import __builtin__

orig_import = __builtin__.__import__

all_imports = []

def my_import(*args, **kwds):
     module = orig_import(*args, **kwds)
     # Get the fully-qualified module name from the module object itself
     # instead of trying to compute it yourself.
     all_imports.append(module.__name__)
     return module

__builtin__.__import__ = my_import


For extra points, make a context manager that hooks and then unhooks your
custom importer.

You may also want to take a look at an import profiler that I once made:

http://www.enthought.com/~rkern/cgi-bin/hgwebdir.cgi/import_profiler/file/tip/import_profiler.py#l23

-- 
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-16 22:20 +0000

csiph-web