Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!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.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'infinite': 0.07; 'mess': 0.07; 'from:addr:ethan': 0.09; 'from:addr:stoneleaf.us': 0.09; 'from:name:ethan furman': 0.09; 'message-id:@stoneleaf.us': 0.09; 'myself,': 0.09; 'received:184.172': 0.09; 'received:gator410.hostgator.com': 0.09; 'url:github': 0.09; '~ethan~': 0.09; 'anymore.': 0.16; 'received:72.11': 0.16; 'received:72.11.125': 0.16; 'received:72.11.125.166': 0.16; 'subject:import': 0.16; 'cc:addr:python-list': 0.16; 'examples': 0.16; 'wrote:': 0.18; 'this?': 0.19; "doesn't": 0.22; 'header:In- Reply-To:1': 0.22; 'dictionary': 0.23; 'replacing': 0.23; 'guess': 0.26; 'cc:2**0': 0.26; 'module': 0.26; 'import': 0.27; 'tried': 0.27; 'depends': 0.28; 'problem': 0.29; 'cc:addr:python.org': 0.29; 'imported': 0.30; 'quite': 0.31; 'does': 0.32; 'there': 0.33; 'header:User-Agent:1': 0.33; 'too': 0.33; 'loop': 0.34; 'keys': 0.34; 'but': 0.37; 'using': 0.37; 'could': 0.38; 'should': 0.38; 'might': 0.40; 'easy': 0.60; 'custom': 0.61; 'achieve': 0.61; 'your': 0.61; 'received:websitewelcome.com': 0.64; 'believe': 0.65; 'accessed.': 0.84; 'andrea': 0.84; 'hook': 0.84 Date: Fri, 16 Mar 2012 10:06:31 -0700 From: Ethan Furman User-Agent: Thunderbird 1.5.0.10 (Windows/20070221) MIME-Version: 1.0 To: Andrea Crotti Subject: Re: avoid import short-circuiting References: <4F636F0D.3060006@gmail.com> In-Reply-To: <4F636F0D.3060006@gmail.com> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-AntiAbuse: This header was added to track abuse, please include it with any abuse report X-AntiAbuse: Primary Hostname - gator410.hostgator.com X-AntiAbuse: Original Domain - python.org X-AntiAbuse: Originator/Caller UID/GID - [47 12] / [47 12] X-AntiAbuse: Sender Address Domain - stoneleaf.us X-BWhitelist: no X-Source: X-Source-Args: X-Source-Dir: X-Source-Sender: mail.admailinc.com ([192.168.10.136]) [72.11.125.166]:3956 X-Source-Auth: ethan+stoneleaf.us X-Email-Count: 5 X-Source-Cap: dG9idWs7dG9idWs7Z2F0b3I0MTAuaG9zdGdhdG9yLmNvbQ== Cc: python-list 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: 24 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1331917120 news.xs4all.nl 6919 [2001:888:2000:d::a6]:58282 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:21764 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.. I believe sys.modules is a dictionary; you might try replacing it with your own custom dictionary that does whatever when the keys are accessed. ~Ethan~