Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!feeder.news-service.com!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; 'else:': 0.03; 'sys': 0.05; 'importerror:': 0.07; 'imports': 0.07; 'mode,': 0.07; 'python': 0.08; '__name__': 0.09; 'hooks': 0.09; 'none:': 0.09; 'am,': 0.13; 'wrote:': 0.15; '\'%s\'\\n"': 0.16; "'\\n')": 0.16; "'__main__':": 0.16; '-tkc': 0.16; 'argv,': 0.16; 'billy': 0.16; 'desc': 0.16; 'fp.close()': 0.16; 'from:addr:python.list': 0.16; 'from:addr:tim.thechases.com': 0.16; 'from:name:tim chase': 0.16; 'imp': 0.16; 'message-id:@tim.thechases.com': 0.16; 'pth,': 0.16; 'received:70.251': 0.16; 'received:dsl.rcsntx.swbell.net': 0.16; 'received:rcsntx.swbell.net': 0.16; 'received:swbell.net': 0.16; 'stderr': 0.16; 'zipfile': 0.16; 'cc:addr:python-list': 0.16; 'wrote': 0.21; '(like': 0.21; 'cc:2**0': 0.21; 'cc:no real name:2**0': 0.22; 'header:In-Reply-To:1': 0.22; 'code': 0.24; 'hey': 0.26; 'skip:_ 20': 0.28; 'import': 0.29; 'script': 0.29; 'cc:addr:python.org': 0.30; 'module': 0.30; 'controlled': 0.30; 'shared': 0.32; 'does': 0.32; 'anyone': 0.33; 'header:User- Agent:1': 0.34; 'skip:# 10': 0.34; 'try:': 0.35; 'running': 0.35; 'file': 0.36; 'anything': 0.37; 'some': 0.37; 'subject:: ': 0.38; 'something': 0.38; 'think': 0.38; 'except': 0.39; 'skip:s 20': 0.39; "i'd": 0.40; 'tap': 0.67; '07:43': 0.84; 'avoid.': 0.84; 'file",': 0.84; 'hostile': 0.91; 'from.': 0.93 Date: Thu, 04 Aug 2011 13:24:13 -0500 From: Tim Chase User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.2.18) Gecko/20110616 Thunderbird/3.1.11 MIME-Version: 1.0 To: Billy Mays <81282ed9a88799d21e77957df2d84bd6514d9af6@myhashismyemail.com> Subject: Re: PyWhich References: In-Reply-To: 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 - boston.accountservergroup.com X-AntiAbuse: Original Domain - python.org X-AntiAbuse: Originator/Caller UID/GID - [47 12] / [47 12] X-AntiAbuse: Sender Address Domain - tim.thechases.com X-Source: X-Source-Args: X-Source-Dir: 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: 55 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1312482269 news.xs4all.nl 23968 [2001:888:2000:d::a6]:44946 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:10866 On 08/04/2011 07:43 AM, Billy Mays wrote: > Hey c.l.p., > > I wrote a little python script that finds the file that a python module > came from. Does anyone see anything wrong with this script? > > #!/usr/bin/python > > import sys > if __name__ == '__main__': > if len(sys.argv)> 1: > try: > m = __import__(sys.argv[1]) > sys.stdout.write(m.__file__ + '\n') For a quick script in a controlled environment, not bad. In a hostile environment, I'd be nervous about running arbitrary module code triggered by the import. Even if non-malicious, some imports (like PyCrypto) may have some initialization lag which would be nice to avoid. I think I'd make use of imp.find_module to write it something like this (untested) from sys import argv, stderr import imp type_map = { imp.PY_SOURCE: "Source file", imp.PY_COMPILED: "Compiled code object", imp.C_EXTENSION: "Dynamically loadabld shared library", imp.PKG_DIRECTORY: "Package directory", imp.C_BUILTIN: "Built-in", imp.PY_FROZEN: "Frozen module", } if __name__ == '__main__': if len(argv) > 1: for modname in argv[1:]: try: fp, pth, desc = imp.find_module(modname) (suffix, mode, type_) = desc if fp is not None: fp.close() print("%s\t[%s]" % ( pth, type_map.get(type_, "UNKNOWN") )) except ImportError: stderr.write("No such module '%s'\n" % modname) else: stderr.write("Usage: pywhich [...]\n") I don't know a good way to tap into other import hooks (such as the zipfile import) to augment that type_map dictionary. -tkc