Guido van Rossum | 75dc496 | 1998-03-05 03:42:00 +0000 | [diff] [blame] | 1 | """Find modules used by a script, using introspection.""" |
Thomas Heller | 919000e | 2002-11-25 20:21:59 +0000 | [diff] [blame] | 2 | |
Guido van Rossum | 75dc496 | 1998-03-05 03:42:00 +0000 | [diff] [blame] | 3 | import dis |
Brett Cannon | 0f38478 | 2014-02-28 10:50:34 -0500 | [diff] [blame] | 4 | import importlib._bootstrap |
Brett Cannon | 73b969e | 2012-12-22 19:34:21 -0500 | [diff] [blame] | 5 | import importlib.machinery |
Guido van Rossum | 75dc496 | 1998-03-05 03:42:00 +0000 | [diff] [blame] | 6 | import marshal |
| 7 | import os |
Guido van Rossum | 75dc496 | 1998-03-05 03:42:00 +0000 | [diff] [blame] | 8 | import sys |
Christian Heimes | 45f9af3 | 2007-11-27 21:50:00 +0000 | [diff] [blame] | 9 | import types |
Guido van Rossum | fc2a0a8 | 2006-10-27 23:06:01 +0000 | [diff] [blame] | 10 | import struct |
Brett Cannon | e4f41de | 2013-06-16 13:13:40 -0400 | [diff] [blame] | 11 | import warnings |
| 12 | with warnings.catch_warnings(): |
| 13 | warnings.simplefilter('ignore', PendingDeprecationWarning) |
| 14 | import imp |
Guido van Rossum | 75dc496 | 1998-03-05 03:42:00 +0000 | [diff] [blame] | 15 | |
Brett Cannon | 4043001 | 2007-10-22 20:24:51 +0000 | [diff] [blame] | 16 | # XXX Clean up once str8's cstor matches bytes. |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 17 | LOAD_CONST = bytes([dis.opname.index('LOAD_CONST')]) |
| 18 | IMPORT_NAME = bytes([dis.opname.index('IMPORT_NAME')]) |
| 19 | STORE_NAME = bytes([dis.opname.index('STORE_NAME')]) |
| 20 | STORE_GLOBAL = bytes([dis.opname.index('STORE_GLOBAL')]) |
Just van Rossum | e29310a | 2002-12-31 16:33:00 +0000 | [diff] [blame] | 21 | STORE_OPS = [STORE_NAME, STORE_GLOBAL] |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 22 | HAVE_ARGUMENT = bytes([dis.HAVE_ARGUMENT]) |
Guido van Rossum | 75dc496 | 1998-03-05 03:42:00 +0000 | [diff] [blame] | 23 | |
Guido van Rossum | f1b5a0e | 1998-05-18 20:21:56 +0000 | [diff] [blame] | 24 | # Modulefinder does a good job at simulating Python's, but it can not |
| 25 | # handle __path__ modifications packages make at runtime. Therefore there |
| 26 | # is a mechanism whereby you can register extra paths in this map for a |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 27 | # package, and it will be honored. |
Guido van Rossum | f1b5a0e | 1998-05-18 20:21:56 +0000 | [diff] [blame] | 28 | |
| 29 | # Note this is a mapping is lists of paths. |
| 30 | packagePathMap = {} |
| 31 | |
| 32 | # A Public interface |
| 33 | def AddPackagePath(packagename, path): |
Éric Araujo | cee6bb5 | 2011-08-01 15:29:07 +0200 | [diff] [blame] | 34 | packagePathMap.setdefault(packagename, []).append(path) |
Guido van Rossum | 75dc496 | 1998-03-05 03:42:00 +0000 | [diff] [blame] | 35 | |
Thomas Heller | c7aaf95 | 2002-11-14 18:45:11 +0000 | [diff] [blame] | 36 | replacePackageMap = {} |
| 37 | |
Martin v. Löwis | 2f48d89 | 2011-05-09 08:05:43 +0200 | [diff] [blame] | 38 | # This ReplacePackage mechanism allows modulefinder to work around |
| 39 | # situations in which a package injects itself under the name |
| 40 | # of another package into sys.modules at runtime by calling |
| 41 | # ReplacePackage("real_package_name", "faked_package_name") |
Thomas Heller | c7aaf95 | 2002-11-14 18:45:11 +0000 | [diff] [blame] | 42 | # before running ModuleFinder. |
| 43 | |
| 44 | def ReplacePackage(oldname, newname): |
| 45 | replacePackageMap[oldname] = newname |
| 46 | |
| 47 | |
Guido van Rossum | 75dc496 | 1998-03-05 03:42:00 +0000 | [diff] [blame] | 48 | class Module: |
| 49 | |
| 50 | def __init__(self, name, file=None, path=None): |
Guido van Rossum | 912a14c | 1998-03-05 04:56:37 +0000 | [diff] [blame] | 51 | self.__name__ = name |
| 52 | self.__file__ = file |
| 53 | self.__path__ = path |
| 54 | self.__code__ = None |
Just van Rossum | e29310a | 2002-12-31 16:33:00 +0000 | [diff] [blame] | 55 | # The set of global names that are assigned to in the module. |
| 56 | # This includes those names imported through starimports of |
| 57 | # Python modules. |
| 58 | self.globalnames = {} |
| 59 | # The set of starimports this module did that could not be |
| 60 | # resolved, ie. a starimport from a non-Python module. |
| 61 | self.starimports = {} |
Guido van Rossum | 75dc496 | 1998-03-05 03:42:00 +0000 | [diff] [blame] | 62 | |
| 63 | def __repr__(self): |
Neil Schemenauer | 32d23c9 | 2004-02-15 16:43:20 +0000 | [diff] [blame] | 64 | s = "Module(%r" % (self.__name__,) |
Guido van Rossum | 912a14c | 1998-03-05 04:56:37 +0000 | [diff] [blame] | 65 | if self.__file__ is not None: |
Walter Dörwald | 70a6b49 | 2004-02-12 17:35:32 +0000 | [diff] [blame] | 66 | s = s + ", %r" % (self.__file__,) |
Guido van Rossum | 912a14c | 1998-03-05 04:56:37 +0000 | [diff] [blame] | 67 | if self.__path__ is not None: |
Walter Dörwald | 70a6b49 | 2004-02-12 17:35:32 +0000 | [diff] [blame] | 68 | s = s + ", %r" % (self.__path__,) |
Guido van Rossum | 912a14c | 1998-03-05 04:56:37 +0000 | [diff] [blame] | 69 | s = s + ")" |
| 70 | return s |
Guido van Rossum | 75dc496 | 1998-03-05 03:42:00 +0000 | [diff] [blame] | 71 | |
Guido van Rossum | 75dc496 | 1998-03-05 03:42:00 +0000 | [diff] [blame] | 72 | class ModuleFinder: |
| 73 | |
Just van Rossum | e29310a | 2002-12-31 16:33:00 +0000 | [diff] [blame] | 74 | def __init__(self, path=None, debug=0, excludes=[], replace_paths=[]): |
Guido van Rossum | 912a14c | 1998-03-05 04:56:37 +0000 | [diff] [blame] | 75 | if path is None: |
| 76 | path = sys.path |
| 77 | self.path = path |
| 78 | self.modules = {} |
| 79 | self.badmodules = {} |
| 80 | self.debug = debug |
| 81 | self.indent = 0 |
Guido van Rossum | 78fc363 | 1998-03-20 17:37:24 +0000 | [diff] [blame] | 82 | self.excludes = excludes |
Guido van Rossum | 6b767ac | 2001-03-20 20:43:34 +0000 | [diff] [blame] | 83 | self.replace_paths = replace_paths |
| 84 | self.processed_paths = [] # Used in debugging only |
Guido van Rossum | 75dc496 | 1998-03-05 03:42:00 +0000 | [diff] [blame] | 85 | |
| 86 | def msg(self, level, str, *args): |
Guido van Rossum | 912a14c | 1998-03-05 04:56:37 +0000 | [diff] [blame] | 87 | if level <= self.debug: |
| 88 | for i in range(self.indent): |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 89 | print(" ", end=' ') |
| 90 | print(str, end=' ') |
Guido van Rossum | 912a14c | 1998-03-05 04:56:37 +0000 | [diff] [blame] | 91 | for arg in args: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 92 | print(repr(arg), end=' ') |
| 93 | print() |
Guido van Rossum | 75dc496 | 1998-03-05 03:42:00 +0000 | [diff] [blame] | 94 | |
| 95 | def msgin(self, *args): |
Guido van Rossum | 912a14c | 1998-03-05 04:56:37 +0000 | [diff] [blame] | 96 | level = args[0] |
| 97 | if level <= self.debug: |
| 98 | self.indent = self.indent + 1 |
Guido van Rossum | 68468eb | 2003-02-27 20:14:51 +0000 | [diff] [blame] | 99 | self.msg(*args) |
Guido van Rossum | 75dc496 | 1998-03-05 03:42:00 +0000 | [diff] [blame] | 100 | |
| 101 | def msgout(self, *args): |
Guido van Rossum | 912a14c | 1998-03-05 04:56:37 +0000 | [diff] [blame] | 102 | level = args[0] |
| 103 | if level <= self.debug: |
| 104 | self.indent = self.indent - 1 |
Guido van Rossum | 68468eb | 2003-02-27 20:14:51 +0000 | [diff] [blame] | 105 | self.msg(*args) |
Guido van Rossum | 75dc496 | 1998-03-05 03:42:00 +0000 | [diff] [blame] | 106 | |
| 107 | def run_script(self, pathname): |
Guido van Rossum | 912a14c | 1998-03-05 04:56:37 +0000 | [diff] [blame] | 108 | self.msg(2, "run_script", pathname) |
Éric Araujo | 1e3a68d | 2011-07-28 23:35:29 +0200 | [diff] [blame] | 109 | with open(pathname) as fp: |
Brett Cannon | 028011f | 2010-10-30 00:26:48 +0000 | [diff] [blame] | 110 | stuff = ("", "r", imp.PY_SOURCE) |
| 111 | self.load_module('__main__', fp, pathname, stuff) |
Guido van Rossum | 75dc496 | 1998-03-05 03:42:00 +0000 | [diff] [blame] | 112 | |
| 113 | def load_file(self, pathname): |
Guido van Rossum | 912a14c | 1998-03-05 04:56:37 +0000 | [diff] [blame] | 114 | dir, name = os.path.split(pathname) |
| 115 | name, ext = os.path.splitext(name) |
Éric Araujo | 1e3a68d | 2011-07-28 23:35:29 +0200 | [diff] [blame] | 116 | with open(pathname) as fp: |
Brett Cannon | 028011f | 2010-10-30 00:26:48 +0000 | [diff] [blame] | 117 | stuff = (ext, "r", imp.PY_SOURCE) |
| 118 | self.load_module(name, fp, pathname, stuff) |
Guido van Rossum | 75dc496 | 1998-03-05 03:42:00 +0000 | [diff] [blame] | 119 | |
Guido van Rossum | fc2a0a8 | 2006-10-27 23:06:01 +0000 | [diff] [blame] | 120 | def import_hook(self, name, caller=None, fromlist=None, level=-1): |
| 121 | self.msg(3, "import_hook", name, caller, fromlist, level) |
| 122 | parent = self.determine_parent(caller, level=level) |
Guido van Rossum | 912a14c | 1998-03-05 04:56:37 +0000 | [diff] [blame] | 123 | q, tail = self.find_head_package(parent, name) |
| 124 | m = self.load_tail(q, tail) |
| 125 | if not fromlist: |
| 126 | return q |
| 127 | if m.__path__: |
| 128 | self.ensure_fromlist(m, fromlist) |
Thomas Heller | 318b7b9 | 2002-11-26 08:06:50 +0000 | [diff] [blame] | 129 | return None |
Guido van Rossum | 75dc496 | 1998-03-05 03:42:00 +0000 | [diff] [blame] | 130 | |
Guido van Rossum | fc2a0a8 | 2006-10-27 23:06:01 +0000 | [diff] [blame] | 131 | def determine_parent(self, caller, level=-1): |
| 132 | self.msgin(4, "determine_parent", caller, level) |
| 133 | if not caller or level == 0: |
Guido van Rossum | 912a14c | 1998-03-05 04:56:37 +0000 | [diff] [blame] | 134 | self.msgout(4, "determine_parent -> None") |
| 135 | return None |
| 136 | pname = caller.__name__ |
Guido van Rossum | fc2a0a8 | 2006-10-27 23:06:01 +0000 | [diff] [blame] | 137 | if level >= 1: # relative import |
| 138 | if caller.__path__: |
| 139 | level -= 1 |
| 140 | if level == 0: |
| 141 | parent = self.modules[pname] |
| 142 | assert parent is caller |
| 143 | self.msgout(4, "determine_parent ->", parent) |
| 144 | return parent |
| 145 | if pname.count(".") < level: |
Collin Winter | ce36ad8 | 2007-08-30 01:19:48 +0000 | [diff] [blame] | 146 | raise ImportError("relative importpath too deep") |
Guido van Rossum | fc2a0a8 | 2006-10-27 23:06:01 +0000 | [diff] [blame] | 147 | pname = ".".join(pname.split(".")[:-level]) |
| 148 | parent = self.modules[pname] |
| 149 | self.msgout(4, "determine_parent ->", parent) |
| 150 | return parent |
Guido van Rossum | 912a14c | 1998-03-05 04:56:37 +0000 | [diff] [blame] | 151 | if caller.__path__: |
| 152 | parent = self.modules[pname] |
| 153 | assert caller is parent |
| 154 | self.msgout(4, "determine_parent ->", parent) |
| 155 | return parent |
| 156 | if '.' in pname: |
Walter Dörwald | aaab30e | 2002-09-11 20:36:02 +0000 | [diff] [blame] | 157 | i = pname.rfind('.') |
Guido van Rossum | 912a14c | 1998-03-05 04:56:37 +0000 | [diff] [blame] | 158 | pname = pname[:i] |
| 159 | parent = self.modules[pname] |
| 160 | assert parent.__name__ == pname |
| 161 | self.msgout(4, "determine_parent ->", parent) |
| 162 | return parent |
| 163 | self.msgout(4, "determine_parent -> None") |
| 164 | return None |
Guido van Rossum | 75dc496 | 1998-03-05 03:42:00 +0000 | [diff] [blame] | 165 | |
| 166 | def find_head_package(self, parent, name): |
Guido van Rossum | 912a14c | 1998-03-05 04:56:37 +0000 | [diff] [blame] | 167 | self.msgin(4, "find_head_package", parent, name) |
| 168 | if '.' in name: |
Walter Dörwald | aaab30e | 2002-09-11 20:36:02 +0000 | [diff] [blame] | 169 | i = name.find('.') |
Guido van Rossum | 912a14c | 1998-03-05 04:56:37 +0000 | [diff] [blame] | 170 | head = name[:i] |
| 171 | tail = name[i+1:] |
| 172 | else: |
| 173 | head = name |
| 174 | tail = "" |
| 175 | if parent: |
| 176 | qname = "%s.%s" % (parent.__name__, head) |
| 177 | else: |
| 178 | qname = head |
| 179 | q = self.import_module(head, qname, parent) |
| 180 | if q: |
| 181 | self.msgout(4, "find_head_package ->", (q, tail)) |
| 182 | return q, tail |
| 183 | if parent: |
| 184 | qname = head |
| 185 | parent = None |
| 186 | q = self.import_module(head, qname, parent) |
| 187 | if q: |
| 188 | self.msgout(4, "find_head_package ->", (q, tail)) |
| 189 | return q, tail |
| 190 | self.msgout(4, "raise ImportError: No module named", qname) |
Collin Winter | ce36ad8 | 2007-08-30 01:19:48 +0000 | [diff] [blame] | 191 | raise ImportError("No module named " + qname) |
Guido van Rossum | 75dc496 | 1998-03-05 03:42:00 +0000 | [diff] [blame] | 192 | |
| 193 | def load_tail(self, q, tail): |
Guido van Rossum | 912a14c | 1998-03-05 04:56:37 +0000 | [diff] [blame] | 194 | self.msgin(4, "load_tail", q, tail) |
| 195 | m = q |
| 196 | while tail: |
Walter Dörwald | aaab30e | 2002-09-11 20:36:02 +0000 | [diff] [blame] | 197 | i = tail.find('.') |
Guido van Rossum | 912a14c | 1998-03-05 04:56:37 +0000 | [diff] [blame] | 198 | if i < 0: i = len(tail) |
| 199 | head, tail = tail[:i], tail[i+1:] |
| 200 | mname = "%s.%s" % (m.__name__, head) |
| 201 | m = self.import_module(head, mname, m) |
| 202 | if not m: |
| 203 | self.msgout(4, "raise ImportError: No module named", mname) |
Collin Winter | ce36ad8 | 2007-08-30 01:19:48 +0000 | [diff] [blame] | 204 | raise ImportError("No module named " + mname) |
Guido van Rossum | 912a14c | 1998-03-05 04:56:37 +0000 | [diff] [blame] | 205 | self.msgout(4, "load_tail ->", m) |
| 206 | return m |
Guido van Rossum | 75dc496 | 1998-03-05 03:42:00 +0000 | [diff] [blame] | 207 | |
| 208 | def ensure_fromlist(self, m, fromlist, recursive=0): |
Guido van Rossum | 912a14c | 1998-03-05 04:56:37 +0000 | [diff] [blame] | 209 | self.msg(4, "ensure_fromlist", m, fromlist, recursive) |
| 210 | for sub in fromlist: |
| 211 | if sub == "*": |
| 212 | if not recursive: |
| 213 | all = self.find_all_submodules(m) |
| 214 | if all: |
| 215 | self.ensure_fromlist(m, all, 1) |
| 216 | elif not hasattr(m, sub): |
| 217 | subname = "%s.%s" % (m.__name__, sub) |
| 218 | submod = self.import_module(sub, subname, m) |
| 219 | if not submod: |
Collin Winter | ce36ad8 | 2007-08-30 01:19:48 +0000 | [diff] [blame] | 220 | raise ImportError("No module named " + subname) |
Guido van Rossum | 75dc496 | 1998-03-05 03:42:00 +0000 | [diff] [blame] | 221 | |
| 222 | def find_all_submodules(self, m): |
Guido van Rossum | 912a14c | 1998-03-05 04:56:37 +0000 | [diff] [blame] | 223 | if not m.__path__: |
| 224 | return |
| 225 | modules = {} |
Thomas Heller | aaf1c8d | 2003-11-14 10:28:42 +0000 | [diff] [blame] | 226 | # 'suffixes' used to be a list hardcoded to [".py", ".pyc", ".pyo"]. |
| 227 | # But we must also collect Python extension modules - although |
| 228 | # we cannot separate normal dlls from Python extensions. |
| 229 | suffixes = [] |
Brett Cannon | cb66eb0 | 2012-05-11 12:58:42 -0400 | [diff] [blame] | 230 | suffixes += importlib.machinery.EXTENSION_SUFFIXES[:] |
| 231 | suffixes += importlib.machinery.SOURCE_SUFFIXES[:] |
| 232 | suffixes += importlib.machinery.BYTECODE_SUFFIXES[:] |
Guido van Rossum | 912a14c | 1998-03-05 04:56:37 +0000 | [diff] [blame] | 233 | for dir in m.__path__: |
| 234 | try: |
| 235 | names = os.listdir(dir) |
Andrew Svetlov | ad28c7f | 2012-12-18 22:02:39 +0200 | [diff] [blame] | 236 | except OSError: |
Guido van Rossum | 912a14c | 1998-03-05 04:56:37 +0000 | [diff] [blame] | 237 | self.msg(2, "can't list directory", dir) |
| 238 | continue |
| 239 | for name in names: |
| 240 | mod = None |
| 241 | for suff in suffixes: |
| 242 | n = len(suff) |
| 243 | if name[-n:] == suff: |
| 244 | mod = name[:-n] |
| 245 | break |
| 246 | if mod and mod != "__init__": |
| 247 | modules[mod] = mod |
| 248 | return modules.keys() |
Guido van Rossum | 75dc496 | 1998-03-05 03:42:00 +0000 | [diff] [blame] | 249 | |
| 250 | def import_module(self, partname, fqname, parent): |
Guido van Rossum | 912a14c | 1998-03-05 04:56:37 +0000 | [diff] [blame] | 251 | self.msgin(3, "import_module", partname, fqname, parent) |
| 252 | try: |
| 253 | m = self.modules[fqname] |
| 254 | except KeyError: |
| 255 | pass |
| 256 | else: |
| 257 | self.msgout(3, "import_module ->", m) |
| 258 | return m |
Guido van Rossum | e2b70bc | 2006-08-18 22:13:04 +0000 | [diff] [blame] | 259 | if fqname in self.badmodules: |
Guido van Rossum | 912a14c | 1998-03-05 04:56:37 +0000 | [diff] [blame] | 260 | self.msgout(3, "import_module -> None") |
| 261 | return None |
Thomas Heller | 2e7c832 | 2004-05-11 15:10:59 +0000 | [diff] [blame] | 262 | if parent and parent.__path__ is None: |
| 263 | self.msgout(3, "import_module -> None") |
| 264 | return None |
Guido van Rossum | 912a14c | 1998-03-05 04:56:37 +0000 | [diff] [blame] | 265 | try: |
| 266 | fp, pathname, stuff = self.find_module(partname, |
Just van Rossum | f0dfbaf | 2003-03-05 17:23:48 +0000 | [diff] [blame] | 267 | parent and parent.__path__, parent) |
Guido van Rossum | 912a14c | 1998-03-05 04:56:37 +0000 | [diff] [blame] | 268 | except ImportError: |
| 269 | self.msgout(3, "import_module ->", None) |
| 270 | return None |
| 271 | try: |
| 272 | m = self.load_module(fqname, fp, pathname, stuff) |
| 273 | finally: |
Éric Araujo | 1e3a68d | 2011-07-28 23:35:29 +0200 | [diff] [blame] | 274 | if fp: |
| 275 | fp.close() |
Guido van Rossum | 912a14c | 1998-03-05 04:56:37 +0000 | [diff] [blame] | 276 | if parent: |
| 277 | setattr(parent, partname, m) |
| 278 | self.msgout(3, "import_module ->", m) |
| 279 | return m |
Guido van Rossum | 75dc496 | 1998-03-05 03:42:00 +0000 | [diff] [blame] | 280 | |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 281 | def load_module(self, fqname, fp, pathname, file_info): |
| 282 | suffix, mode, type = file_info |
Guido van Rossum | 912a14c | 1998-03-05 04:56:37 +0000 | [diff] [blame] | 283 | self.msgin(2, "load_module", fqname, fp and "fp", pathname) |
| 284 | if type == imp.PKG_DIRECTORY: |
| 285 | m = self.load_package(fqname, pathname) |
| 286 | self.msgout(2, "load_module ->", m) |
| 287 | return m |
| 288 | if type == imp.PY_SOURCE: |
Guido van Rossum | 78fc363 | 1998-03-20 17:37:24 +0000 | [diff] [blame] | 289 | co = compile(fp.read()+'\n', pathname, 'exec') |
Guido van Rossum | 912a14c | 1998-03-05 04:56:37 +0000 | [diff] [blame] | 290 | elif type == imp.PY_COMPILED: |
Brett Cannon | 0f38478 | 2014-02-28 10:50:34 -0500 | [diff] [blame] | 291 | try: |
| 292 | marshal_data = importlib._bootstrap._validate_bytecode_header(fp.read()) |
| 293 | except ImportError as exc: |
| 294 | self.msgout(2, "raise ImportError: " + str(exc), pathname) |
| 295 | raise |
| 296 | co = marshal.loads(marshal_data) |
Guido van Rossum | 912a14c | 1998-03-05 04:56:37 +0000 | [diff] [blame] | 297 | else: |
| 298 | co = None |
| 299 | m = self.add_module(fqname) |
Guido van Rossum | ab045f9 | 1998-03-06 19:55:10 +0000 | [diff] [blame] | 300 | m.__file__ = pathname |
Guido van Rossum | 912a14c | 1998-03-05 04:56:37 +0000 | [diff] [blame] | 301 | if co: |
Guido van Rossum | 6b767ac | 2001-03-20 20:43:34 +0000 | [diff] [blame] | 302 | if self.replace_paths: |
| 303 | co = self.replace_paths_in_code(co) |
Guido van Rossum | 912a14c | 1998-03-05 04:56:37 +0000 | [diff] [blame] | 304 | m.__code__ = co |
Guido van Rossum | 3c51cf2 | 1998-03-05 05:15:07 +0000 | [diff] [blame] | 305 | self.scan_code(co, m) |
Guido van Rossum | 912a14c | 1998-03-05 04:56:37 +0000 | [diff] [blame] | 306 | self.msgout(2, "load_module ->", m) |
| 307 | return m |
Guido van Rossum | 75dc496 | 1998-03-05 03:42:00 +0000 | [diff] [blame] | 308 | |
Just van Rossum | e29310a | 2002-12-31 16:33:00 +0000 | [diff] [blame] | 309 | def _add_badmodule(self, name, caller): |
| 310 | if name not in self.badmodules: |
| 311 | self.badmodules[name] = {} |
Benjamin Peterson | c0747cf | 2008-11-03 20:31:38 +0000 | [diff] [blame] | 312 | if caller: |
| 313 | self.badmodules[name][caller.__name__] = 1 |
| 314 | else: |
| 315 | self.badmodules[name]["-"] = 1 |
Just van Rossum | e29310a | 2002-12-31 16:33:00 +0000 | [diff] [blame] | 316 | |
Guido van Rossum | fc2a0a8 | 2006-10-27 23:06:01 +0000 | [diff] [blame] | 317 | def _safe_import_hook(self, name, caller, fromlist, level=-1): |
Just van Rossum | e29310a | 2002-12-31 16:33:00 +0000 | [diff] [blame] | 318 | # wrapper for self.import_hook() that won't raise ImportError |
| 319 | if name in self.badmodules: |
| 320 | self._add_badmodule(name, caller) |
| 321 | return |
| 322 | try: |
Guido van Rossum | fc2a0a8 | 2006-10-27 23:06:01 +0000 | [diff] [blame] | 323 | self.import_hook(name, caller, level=level) |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 324 | except ImportError as msg: |
Just van Rossum | e29310a | 2002-12-31 16:33:00 +0000 | [diff] [blame] | 325 | self.msg(2, "ImportError:", str(msg)) |
| 326 | self._add_badmodule(name, caller) |
| 327 | else: |
| 328 | if fromlist: |
| 329 | for sub in fromlist: |
| 330 | if sub in self.badmodules: |
| 331 | self._add_badmodule(sub, caller) |
| 332 | continue |
| 333 | try: |
Guido van Rossum | fc2a0a8 | 2006-10-27 23:06:01 +0000 | [diff] [blame] | 334 | self.import_hook(name, caller, [sub], level=level) |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 335 | except ImportError as msg: |
Just van Rossum | e29310a | 2002-12-31 16:33:00 +0000 | [diff] [blame] | 336 | self.msg(2, "ImportError:", str(msg)) |
| 337 | fullname = name + "." + sub |
| 338 | self._add_badmodule(fullname, caller) |
| 339 | |
Guido van Rossum | fc2a0a8 | 2006-10-27 23:06:01 +0000 | [diff] [blame] | 340 | def scan_opcodes_25(self, co, |
| 341 | unpack = struct.unpack): |
| 342 | # Scan the code, and yield 'interesting' opcode combinations |
| 343 | # Python 2.5 version (has absolute and relative imports) |
| 344 | code = co.co_code |
| 345 | names = co.co_names |
| 346 | consts = co.co_consts |
| 347 | LOAD_LOAD_AND_IMPORT = LOAD_CONST + LOAD_CONST + IMPORT_NAME |
| 348 | while code: |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 349 | c = bytes([code[0]]) |
Guido van Rossum | fc2a0a8 | 2006-10-27 23:06:01 +0000 | [diff] [blame] | 350 | if c in STORE_OPS: |
| 351 | oparg, = unpack('<H', code[1:3]) |
| 352 | yield "store", (names[oparg],) |
| 353 | code = code[3:] |
| 354 | continue |
| 355 | if code[:9:3] == LOAD_LOAD_AND_IMPORT: |
| 356 | oparg_1, oparg_2, oparg_3 = unpack('<xHxHxH', code[:9]) |
| 357 | level = consts[oparg_1] |
| 358 | if level == 0: # absolute import |
| 359 | yield "absolute_import", (consts[oparg_2], names[oparg_3]) |
| 360 | else: # relative import |
| 361 | yield "relative_import", (level, consts[oparg_2], names[oparg_3]) |
| 362 | code = code[9:] |
| 363 | continue |
| 364 | if c >= HAVE_ARGUMENT: |
| 365 | code = code[3:] |
| 366 | else: |
| 367 | code = code[1:] |
| 368 | |
Guido van Rossum | 3c51cf2 | 1998-03-05 05:15:07 +0000 | [diff] [blame] | 369 | def scan_code(self, co, m): |
| 370 | code = co.co_code |
Serhiy Storchaka | 7e52705 | 2014-01-20 21:29:31 +0200 | [diff] [blame] | 371 | scanner = self.scan_opcodes_25 |
Guido van Rossum | fc2a0a8 | 2006-10-27 23:06:01 +0000 | [diff] [blame] | 372 | for what, args in scanner(co): |
| 373 | if what == "store": |
| 374 | name, = args |
| 375 | m.globalnames[name] = 1 |
| 376 | elif what == "absolute_import": |
| 377 | fromlist, name = args |
Just van Rossum | e29310a | 2002-12-31 16:33:00 +0000 | [diff] [blame] | 378 | have_star = 0 |
| 379 | if fromlist is not None: |
| 380 | if "*" in fromlist: |
| 381 | have_star = 1 |
| 382 | fromlist = [f for f in fromlist if f != "*"] |
Guido van Rossum | fc2a0a8 | 2006-10-27 23:06:01 +0000 | [diff] [blame] | 383 | self._safe_import_hook(name, m, fromlist, level=0) |
Just van Rossum | e29310a | 2002-12-31 16:33:00 +0000 | [diff] [blame] | 384 | if have_star: |
| 385 | # We've encountered an "import *". If it is a Python module, |
| 386 | # the code has already been parsed and we can suck out the |
| 387 | # global names. |
| 388 | mm = None |
| 389 | if m.__path__: |
| 390 | # At this point we don't know whether 'name' is a |
| 391 | # submodule of 'm' or a global module. Let's just try |
| 392 | # the full name first. |
| 393 | mm = self.modules.get(m.__name__ + "." + name) |
| 394 | if mm is None: |
| 395 | mm = self.modules.get(name) |
| 396 | if mm is not None: |
| 397 | m.globalnames.update(mm.globalnames) |
| 398 | m.starimports.update(mm.starimports) |
| 399 | if mm.__code__ is None: |
| 400 | m.starimports[name] = 1 |
| 401 | else: |
| 402 | m.starimports[name] = 1 |
Guido van Rossum | fc2a0a8 | 2006-10-27 23:06:01 +0000 | [diff] [blame] | 403 | elif what == "relative_import": |
| 404 | level, fromlist, name = args |
| 405 | if name: |
| 406 | self._safe_import_hook(name, m, fromlist, level=level) |
| 407 | else: |
| 408 | parent = self.determine_parent(m, level=level) |
| 409 | self._safe_import_hook(parent.__name__, None, fromlist, level=0) |
| 410 | else: |
| 411 | # We don't expect anything else from the generator. |
| 412 | raise RuntimeError(what) |
| 413 | |
Guido van Rossum | 3c51cf2 | 1998-03-05 05:15:07 +0000 | [diff] [blame] | 414 | for c in co.co_consts: |
| 415 | if isinstance(c, type(co)): |
| 416 | self.scan_code(c, m) |
| 417 | |
Guido van Rossum | 75dc496 | 1998-03-05 03:42:00 +0000 | [diff] [blame] | 418 | def load_package(self, fqname, pathname): |
Guido van Rossum | 912a14c | 1998-03-05 04:56:37 +0000 | [diff] [blame] | 419 | self.msgin(2, "load_package", fqname, pathname) |
Thomas Heller | c7aaf95 | 2002-11-14 18:45:11 +0000 | [diff] [blame] | 420 | newname = replacePackageMap.get(fqname) |
| 421 | if newname: |
| 422 | fqname = newname |
Guido van Rossum | 912a14c | 1998-03-05 04:56:37 +0000 | [diff] [blame] | 423 | m = self.add_module(fqname) |
| 424 | m.__file__ = pathname |
| 425 | m.__path__ = [pathname] |
Guido van Rossum | f1b5a0e | 1998-05-18 20:21:56 +0000 | [diff] [blame] | 426 | |
Guido van Rossum | e7e632a | 1998-09-14 16:02:28 +0000 | [diff] [blame] | 427 | # As per comment at top of file, simulate runtime __path__ additions. |
| 428 | m.__path__ = m.__path__ + packagePathMap.get(fqname, []) |
Guido van Rossum | f1b5a0e | 1998-05-18 20:21:56 +0000 | [diff] [blame] | 429 | |
Guido van Rossum | 912a14c | 1998-03-05 04:56:37 +0000 | [diff] [blame] | 430 | fp, buf, stuff = self.find_module("__init__", m.__path__) |
Brett Cannon | 028011f | 2010-10-30 00:26:48 +0000 | [diff] [blame] | 431 | try: |
| 432 | self.load_module(fqname, fp, buf, stuff) |
| 433 | self.msgout(2, "load_package ->", m) |
| 434 | return m |
| 435 | finally: |
| 436 | if fp: |
| 437 | fp.close() |
Guido van Rossum | 75dc496 | 1998-03-05 03:42:00 +0000 | [diff] [blame] | 438 | |
| 439 | def add_module(self, fqname): |
Guido van Rossum | e2b70bc | 2006-08-18 22:13:04 +0000 | [diff] [blame] | 440 | if fqname in self.modules: |
Guido van Rossum | 912a14c | 1998-03-05 04:56:37 +0000 | [diff] [blame] | 441 | return self.modules[fqname] |
| 442 | self.modules[fqname] = m = Module(fqname) |
| 443 | return m |
Guido van Rossum | 75dc496 | 1998-03-05 03:42:00 +0000 | [diff] [blame] | 444 | |
Just van Rossum | f0dfbaf | 2003-03-05 17:23:48 +0000 | [diff] [blame] | 445 | def find_module(self, name, path, parent=None): |
| 446 | if parent is not None: |
Thomas Heller | 2e7c832 | 2004-05-11 15:10:59 +0000 | [diff] [blame] | 447 | # assert path is not None |
Just van Rossum | f0dfbaf | 2003-03-05 17:23:48 +0000 | [diff] [blame] | 448 | fullname = parent.__name__+'.'+name |
Guido van Rossum | 03f7f08 | 2001-10-18 19:15:32 +0000 | [diff] [blame] | 449 | else: |
| 450 | fullname = name |
| 451 | if fullname in self.excludes: |
| 452 | self.msgout(3, "find_module -> Excluded", fullname) |
Collin Winter | ce36ad8 | 2007-08-30 01:19:48 +0000 | [diff] [blame] | 453 | raise ImportError(name) |
Guido van Rossum | 78fc363 | 1998-03-20 17:37:24 +0000 | [diff] [blame] | 454 | |
Guido van Rossum | 912a14c | 1998-03-05 04:56:37 +0000 | [diff] [blame] | 455 | if path is None: |
| 456 | if name in sys.builtin_module_names: |
| 457 | return (None, None, ("", "", imp.C_BUILTIN)) |
Guido van Rossum | 78fc363 | 1998-03-20 17:37:24 +0000 | [diff] [blame] | 458 | |
Guido van Rossum | 912a14c | 1998-03-05 04:56:37 +0000 | [diff] [blame] | 459 | path = self.path |
| 460 | return imp.find_module(name, path) |
Guido van Rossum | 75dc496 | 1998-03-05 03:42:00 +0000 | [diff] [blame] | 461 | |
| 462 | def report(self): |
Just van Rossum | e29310a | 2002-12-31 16:33:00 +0000 | [diff] [blame] | 463 | """Print a report to stdout, listing the found modules with their |
| 464 | paths, as well as modules that are missing, or seem to be missing. |
| 465 | """ |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 466 | print() |
| 467 | print(" %-25s %s" % ("Name", "File")) |
| 468 | print(" %-25s %s" % ("----", "----")) |
Guido van Rossum | 912a14c | 1998-03-05 04:56:37 +0000 | [diff] [blame] | 469 | # Print modules found |
Guido van Rossum | d59cde8 | 2007-06-12 00:25:08 +0000 | [diff] [blame] | 470 | keys = sorted(self.modules.keys()) |
Guido van Rossum | 912a14c | 1998-03-05 04:56:37 +0000 | [diff] [blame] | 471 | for key in keys: |
| 472 | m = self.modules[key] |
| 473 | if m.__path__: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 474 | print("P", end=' ') |
Guido van Rossum | 912a14c | 1998-03-05 04:56:37 +0000 | [diff] [blame] | 475 | else: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 476 | print("m", end=' ') |
| 477 | print("%-25s" % key, m.__file__ or "") |
Guido van Rossum | 75dc496 | 1998-03-05 03:42:00 +0000 | [diff] [blame] | 478 | |
Guido van Rossum | 912a14c | 1998-03-05 04:56:37 +0000 | [diff] [blame] | 479 | # Print missing modules |
Just van Rossum | e29310a | 2002-12-31 16:33:00 +0000 | [diff] [blame] | 480 | missing, maybe = self.any_missing_maybe() |
| 481 | if missing: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 482 | print() |
| 483 | print("Missing modules:") |
Just van Rossum | e29310a | 2002-12-31 16:33:00 +0000 | [diff] [blame] | 484 | for name in missing: |
Guido van Rossum | d59cde8 | 2007-06-12 00:25:08 +0000 | [diff] [blame] | 485 | mods = sorted(self.badmodules[name].keys()) |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 486 | print("?", name, "imported from", ', '.join(mods)) |
Just van Rossum | e29310a | 2002-12-31 16:33:00 +0000 | [diff] [blame] | 487 | # Print modules that may be missing, but then again, maybe not... |
| 488 | if maybe: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 489 | print() |
Ezio Melotti | 30b9d5d | 2013-08-17 15:50:46 +0300 | [diff] [blame] | 490 | print("Submodules that appear to be missing, but could also be", end=' ') |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 491 | print("global names in the parent package:") |
Just van Rossum | e29310a | 2002-12-31 16:33:00 +0000 | [diff] [blame] | 492 | for name in maybe: |
Guido van Rossum | d59cde8 | 2007-06-12 00:25:08 +0000 | [diff] [blame] | 493 | mods = sorted(self.badmodules[name].keys()) |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 494 | print("?", name, "imported from", ', '.join(mods)) |
Guido van Rossum | 75dc496 | 1998-03-05 03:42:00 +0000 | [diff] [blame] | 495 | |
Guido van Rossum | 03f7f08 | 2001-10-18 19:15:32 +0000 | [diff] [blame] | 496 | def any_missing(self): |
Just van Rossum | e29310a | 2002-12-31 16:33:00 +0000 | [diff] [blame] | 497 | """Return a list of modules that appear to be missing. Use |
| 498 | any_missing_maybe() if you want to know which modules are |
| 499 | certain to be missing, and which *may* be missing. |
| 500 | """ |
| 501 | missing, maybe = self.any_missing_maybe() |
| 502 | return missing + maybe |
| 503 | |
| 504 | def any_missing_maybe(self): |
| 505 | """Return two lists, one with modules that are certainly missing |
| 506 | and one with modules that *may* be missing. The latter names could |
| 507 | either be submodules *or* just global names in the package. |
| 508 | |
| 509 | The reason it can't always be determined is that it's impossible to |
| 510 | tell which names are imported when "from module import *" is done |
| 511 | with an extension module, short of actually importing it. |
| 512 | """ |
Guido van Rossum | 03f7f08 | 2001-10-18 19:15:32 +0000 | [diff] [blame] | 513 | missing = [] |
Just van Rossum | e29310a | 2002-12-31 16:33:00 +0000 | [diff] [blame] | 514 | maybe = [] |
| 515 | for name in self.badmodules: |
| 516 | if name in self.excludes: |
| 517 | continue |
| 518 | i = name.rfind(".") |
| 519 | if i < 0: |
| 520 | missing.append(name) |
| 521 | continue |
| 522 | subname = name[i+1:] |
| 523 | pkgname = name[:i] |
| 524 | pkg = self.modules.get(pkgname) |
| 525 | if pkg is not None: |
| 526 | if pkgname in self.badmodules[name]: |
| 527 | # The package tried to import this module itself and |
| 528 | # failed. It's definitely missing. |
| 529 | missing.append(name) |
| 530 | elif subname in pkg.globalnames: |
| 531 | # It's a global in the package: definitely not missing. |
| 532 | pass |
| 533 | elif pkg.starimports: |
| 534 | # It could be missing, but the package did an "import *" |
| 535 | # from a non-Python module, so we simply can't be sure. |
| 536 | maybe.append(name) |
| 537 | else: |
| 538 | # It's not a global in the package, the package didn't |
| 539 | # do funny star imports, it's very likely to be missing. |
| 540 | # The symbol could be inserted into the package from the |
| 541 | # outside, but since that's not good style we simply list |
| 542 | # it missing. |
| 543 | missing.append(name) |
| 544 | else: |
| 545 | missing.append(name) |
| 546 | missing.sort() |
| 547 | maybe.sort() |
| 548 | return missing, maybe |
Guido van Rossum | 03f7f08 | 2001-10-18 19:15:32 +0000 | [diff] [blame] | 549 | |
Guido van Rossum | 6b767ac | 2001-03-20 20:43:34 +0000 | [diff] [blame] | 550 | def replace_paths_in_code(self, co): |
| 551 | new_filename = original_filename = os.path.normpath(co.co_filename) |
Just van Rossum | e29310a | 2002-12-31 16:33:00 +0000 | [diff] [blame] | 552 | for f, r in self.replace_paths: |
Guido van Rossum | 6b767ac | 2001-03-20 20:43:34 +0000 | [diff] [blame] | 553 | if original_filename.startswith(f): |
Just van Rossum | e29310a | 2002-12-31 16:33:00 +0000 | [diff] [blame] | 554 | new_filename = r + original_filename[len(f):] |
Guido van Rossum | 6b767ac | 2001-03-20 20:43:34 +0000 | [diff] [blame] | 555 | break |
| 556 | |
| 557 | if self.debug and original_filename not in self.processed_paths: |
Just van Rossum | e29310a | 2002-12-31 16:33:00 +0000 | [diff] [blame] | 558 | if new_filename != original_filename: |
Guido van Rossum | 6b767ac | 2001-03-20 20:43:34 +0000 | [diff] [blame] | 559 | self.msgout(2, "co_filename %r changed to %r" \ |
| 560 | % (original_filename,new_filename,)) |
| 561 | else: |
| 562 | self.msgout(2, "co_filename %r remains unchanged" \ |
| 563 | % (original_filename,)) |
| 564 | self.processed_paths.append(original_filename) |
| 565 | |
| 566 | consts = list(co.co_consts) |
| 567 | for i in range(len(consts)): |
| 568 | if isinstance(consts[i], type(co)): |
| 569 | consts[i] = self.replace_paths_in_code(consts[i]) |
| 570 | |
Christian Heimes | 45f9af3 | 2007-11-27 21:50:00 +0000 | [diff] [blame] | 571 | return types.CodeType(co.co_argcount, co.co_nlocals, co.co_stacksize, |
Tim Peters | 2c60f7a | 2003-01-29 03:49:43 +0000 | [diff] [blame] | 572 | co.co_flags, co.co_code, tuple(consts), co.co_names, |
| 573 | co.co_varnames, new_filename, co.co_name, |
Neal Norwitz | 82c7231 | 2002-11-12 23:09:12 +0000 | [diff] [blame] | 574 | co.co_firstlineno, co.co_lnotab, |
| 575 | co.co_freevars, co.co_cellvars) |
Guido van Rossum | 6b767ac | 2001-03-20 20:43:34 +0000 | [diff] [blame] | 576 | |
Guido van Rossum | 75dc496 | 1998-03-05 03:42:00 +0000 | [diff] [blame] | 577 | |
| 578 | def test(): |
| 579 | # Parse command line |
| 580 | import getopt |
| 581 | try: |
Guido van Rossum | baf0603 | 1998-08-25 14:06:55 +0000 | [diff] [blame] | 582 | opts, args = getopt.getopt(sys.argv[1:], "dmp:qx:") |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 583 | except getopt.error as msg: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 584 | print(msg) |
Guido van Rossum | 912a14c | 1998-03-05 04:56:37 +0000 | [diff] [blame] | 585 | return |
Guido van Rossum | 75dc496 | 1998-03-05 03:42:00 +0000 | [diff] [blame] | 586 | |
| 587 | # Process options |
| 588 | debug = 1 |
| 589 | domods = 0 |
| 590 | addpath = [] |
Guido van Rossum | baf0603 | 1998-08-25 14:06:55 +0000 | [diff] [blame] | 591 | exclude = [] |
Guido van Rossum | 75dc496 | 1998-03-05 03:42:00 +0000 | [diff] [blame] | 592 | for o, a in opts: |
Guido van Rossum | 912a14c | 1998-03-05 04:56:37 +0000 | [diff] [blame] | 593 | if o == '-d': |
| 594 | debug = debug + 1 |
| 595 | if o == '-m': |
| 596 | domods = 1 |
| 597 | if o == '-p': |
Walter Dörwald | aaab30e | 2002-09-11 20:36:02 +0000 | [diff] [blame] | 598 | addpath = addpath + a.split(os.pathsep) |
Guido van Rossum | 912a14c | 1998-03-05 04:56:37 +0000 | [diff] [blame] | 599 | if o == '-q': |
| 600 | debug = 0 |
Guido van Rossum | baf0603 | 1998-08-25 14:06:55 +0000 | [diff] [blame] | 601 | if o == '-x': |
| 602 | exclude.append(a) |
Guido van Rossum | 75dc496 | 1998-03-05 03:42:00 +0000 | [diff] [blame] | 603 | |
| 604 | # Provide default arguments |
| 605 | if not args: |
Guido van Rossum | 912a14c | 1998-03-05 04:56:37 +0000 | [diff] [blame] | 606 | script = "hello.py" |
Guido van Rossum | 75dc496 | 1998-03-05 03:42:00 +0000 | [diff] [blame] | 607 | else: |
Guido van Rossum | 912a14c | 1998-03-05 04:56:37 +0000 | [diff] [blame] | 608 | script = args[0] |
Guido van Rossum | 75dc496 | 1998-03-05 03:42:00 +0000 | [diff] [blame] | 609 | |
| 610 | # Set the path based on sys.path and the script directory |
| 611 | path = sys.path[:] |
| 612 | path[0] = os.path.dirname(script) |
| 613 | path = addpath + path |
| 614 | if debug > 1: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 615 | print("path:") |
Guido van Rossum | 912a14c | 1998-03-05 04:56:37 +0000 | [diff] [blame] | 616 | for item in path: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 617 | print(" ", repr(item)) |
Guido van Rossum | 75dc496 | 1998-03-05 03:42:00 +0000 | [diff] [blame] | 618 | |
| 619 | # Create the module finder and turn its crank |
Guido van Rossum | baf0603 | 1998-08-25 14:06:55 +0000 | [diff] [blame] | 620 | mf = ModuleFinder(path, debug, exclude) |
Guido van Rossum | 75dc496 | 1998-03-05 03:42:00 +0000 | [diff] [blame] | 621 | for arg in args[1:]: |
Guido van Rossum | 912a14c | 1998-03-05 04:56:37 +0000 | [diff] [blame] | 622 | if arg == '-m': |
| 623 | domods = 1 |
| 624 | continue |
| 625 | if domods: |
| 626 | if arg[-2:] == '.*': |
| 627 | mf.import_hook(arg[:-2], None, ["*"]) |
| 628 | else: |
| 629 | mf.import_hook(arg) |
Guido van Rossum | 75dc496 | 1998-03-05 03:42:00 +0000 | [diff] [blame] | 630 | else: |
Guido van Rossum | 912a14c | 1998-03-05 04:56:37 +0000 | [diff] [blame] | 631 | mf.load_file(arg) |
Guido van Rossum | 75dc496 | 1998-03-05 03:42:00 +0000 | [diff] [blame] | 632 | mf.run_script(script) |
| 633 | mf.report() |
Just van Rossum | e29310a | 2002-12-31 16:33:00 +0000 | [diff] [blame] | 634 | return mf # for -i debugging |
Guido van Rossum | 75dc496 | 1998-03-05 03:42:00 +0000 | [diff] [blame] | 635 | |
| 636 | |
| 637 | if __name__ == '__main__': |
| 638 | try: |
Just van Rossum | e29310a | 2002-12-31 16:33:00 +0000 | [diff] [blame] | 639 | mf = test() |
Guido van Rossum | 75dc496 | 1998-03-05 03:42:00 +0000 | [diff] [blame] | 640 | except KeyboardInterrupt: |
Éric Araujo | 1e3a68d | 2011-07-28 23:35:29 +0200 | [diff] [blame] | 641 | print("\n[interrupted]") |