Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.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.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'context': 0.04; 'value,': 0.04; 'imports': 0.07; 'infinite': 0.07; 'mess': 0.07; 'type,': 0.07; '>>>>': 0.09; 'called.': 0.09; 'compute': 0.09; 'graph': 0.09; 'hooks': 0.09; 'myself,': 0.09; 'url:enthought': 0.09; 'url:github': 0.09; 'def': 0.13; '**kwargs)': 0.16; '**kwargs):': 0.16; '*args,': 0.16; 'anymore.': 0.16; 'kern': 0.16; 'subject:import': 0.16; 'to:addr:robert.kern': 0.16; 'to:name:robert kern': 0.16; 'cc:addr:python-list': 0.16; 'received:74.125.82.44': 0.16; 'examples': 0.16; 'wrote:': 0.18; '>>>': 0.18; 'this?': 0.19; 'thanks,': 0.19; 'seems': 0.20; 'cc:no real name:2**0': 0.21; 'trying': 0.21; "doesn't": 0.22; 'header :In-Reply-To:1': 0.22; 'guess': 0.26; 'cc:2**0': 0.26; 'module': 0.26; 'skip:_ 20': 0.26; 'import': 0.27; 'tried': 0.27; 'depends': 0.28; 'class': 0.29; 'problem': 0.29; 'cc:addr:python.org': 0.29; 'pm,': 0.29; 'arguments.': 0.30; 'imported': 0.30; 'message- id:@gmail.com': 0.31; '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; 'loop': 0.34; 'yourself.': 0.34; 'done': 0.34; 'received:74.125.82': 0.36; 'element': 0.37; 'but': 0.37; 'received:google.com': 0.37; 'using': 0.37; 'skip:_ 10': 0.38; 'received:192': 0.38; 'replace': 0.38; 'could': 0.38; 'received:74.125': 0.38; 'should': 0.38; 'received:192.168.1': 0.39; 'might': 0.40; 'easy': 0.60; 'custom': 0.61; 'achieve': 0.61; 'your': 0.61; 'here': 0.64; 'url:cgi': 0.68; '10:04': 0.84; '10:20': 0.84; 'andrea': 0.84; 'hints?': 0.84; 'hook': 0.84; 'points,': 0.91 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=message-id:date:from:user-agent:mime-version:to:cc:subject :references:in-reply-to:content-type:content-transfer-encoding; bh=0WxZtAtMwH0hWNtUCk5D7qUZQFdQ3s2/erB2/urOtCU=; b=O4z/IWKmiii0PjlUtn/O0BbQHEU+SQmxjJc1PpbOamko7Opyfuy7medsCs6zuWF+m5 qw/erWG7Agyn/nVKy9mm4NBY1gITDiqmIaX8z5XzAl/kNhjkRQP7B94ZeMFGFMqN1Ohl 6HtNOkOk783yfOtWtRXtIvrZ/For0WcYZP8+mhZ9pt0LaRhesvFnZM1HDJJdl48iKAho kYep5nNNibHeWJbpy/bkZizJQTbsrQci3IxPRcYLtKTIwYcm2mrSnpyXW4etk9TitP6g 2T5F4bkcyW9WJHdSlLpI3gUnzhK2tiyOlMysVyHp8Dr+RtKHGOzooLYbz1yL3r0h2Jmh x0FA== Date: Fri, 16 Mar 2012 23:14:16 +0000 From: Andrea Crotti User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:11.0) Gecko/20120314 Thunderbird/11.0 MIME-Version: 1.0 To: Robert Kern Subject: Re: avoid import short-circuiting References: <4F636F0D.3060006@gmail.com> <4F63B8D4.8010103@gmail.com> In-Reply-To: Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit 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: 102 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1331939676 news.xs4all.nl 6844 [2001:888:2000:d::a6]:37491 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:21786 On 03/16/2012 10:20 PM, Robert Kern wrote: > 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 > > Very nice thanks, here it is class ImportMock: def _my_import(self, *args, **kwargs): self.ls.append(args[0]) self.orig(*args, **kwargs) def __enter__(self): self.orig = __builtin__.__import__ self.ls = [] __builtin__.__import__ = self._my_import return self def __exit__(self, type, value, traceback): __builtin__.__import__ = self.orig now I only need to make it create also the graph and then I should be done :)