annotate Lib/modulefinder.py @ 58765:2ba7677bfa95 legacy-trunk

testCopysign was defined twice in test_math; combine the definitions
author Mark Dickinson <dickinsm@gmail.com>
date Sat, 06 Feb 2010 23:11:25 +0000
parents 3485d95957c1
children d234dd21374a
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
26961
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
1 """Find modules used by a script, using introspection."""
29941
29a63c5cfbaf up the b/w compatibility requirement to 2.2
Just van Rossum <just@letterror.com>
parents: 28178
diff changeset
2 # This module should be kept compatible with Python 2.2, see PEP 291.
26961
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
3
40211
43a826467ad3 Modulefinder now handles absolute and relative imports, including
Thomas Heller <theller@ctypes.org>
parents: 32056
diff changeset
4 from __future__ import generators
26961
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
5 import dis
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
6 import imp
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
7 import marshal
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
8 import os
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
9 import sys
44034
5d1049d4c3c6 Replaced import of the 'new' module with 'types' module and added a deprecation warning to the 'new' module.
Christian Heimes <christian@cheimes.de>
parents: 40211
diff changeset
10 import types
40211
43a826467ad3 Modulefinder now handles absolute and relative imports, including
Thomas Heller <theller@ctypes.org>
parents: 32056
diff changeset
11 import struct
26961
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
12
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
13 if hasattr(sys.__stdout__, "newlines"):
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
14 READ_MODE = "U" # universal line endings
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
15 else:
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
16 # remain compatible with Python < 2.3
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
17 READ_MODE = "r"
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
18
40211
43a826467ad3 Modulefinder now handles absolute and relative imports, including
Thomas Heller <theller@ctypes.org>
parents: 32056
diff changeset
19 LOAD_CONST = chr(dis.opname.index('LOAD_CONST'))
43a826467ad3 Modulefinder now handles absolute and relative imports, including
Thomas Heller <theller@ctypes.org>
parents: 32056
diff changeset
20 IMPORT_NAME = chr(dis.opname.index('IMPORT_NAME'))
43a826467ad3 Modulefinder now handles absolute and relative imports, including
Thomas Heller <theller@ctypes.org>
parents: 32056
diff changeset
21 STORE_NAME = chr(dis.opname.index('STORE_NAME'))
43a826467ad3 Modulefinder now handles absolute and relative imports, including
Thomas Heller <theller@ctypes.org>
parents: 32056
diff changeset
22 STORE_GLOBAL = chr(dis.opname.index('STORE_GLOBAL'))
26963
a573fede88e2 patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents: 26961
diff changeset
23 STORE_OPS = [STORE_NAME, STORE_GLOBAL]
40211
43a826467ad3 Modulefinder now handles absolute and relative imports, including
Thomas Heller <theller@ctypes.org>
parents: 32056
diff changeset
24 HAVE_ARGUMENT = chr(dis.HAVE_ARGUMENT)
26961
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
25
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
26 # Modulefinder does a good job at simulating Python's, but it can not
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
27 # handle __path__ modifications packages make at runtime. Therefore there
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
28 # is a mechanism whereby you can register extra paths in this map for a
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
29 # package, and it will be honored.
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
30
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
31 # Note this is a mapping is lists of paths.
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
32 packagePathMap = {}
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
33
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
34 # A Public interface
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
35 def AddPackagePath(packagename, path):
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
36 paths = packagePathMap.get(packagename, [])
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
37 paths.append(path)
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
38 packagePathMap[packagename] = paths
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
39
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
40 replacePackageMap = {}
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
41
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
42 # This ReplacePackage mechanism allows modulefinder to work around the
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
43 # way the _xmlplus package injects itself under the name "xml" into
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
44 # sys.modules at runtime by calling ReplacePackage("_xmlplus", "xml")
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
45 # before running ModuleFinder.
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
46
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
47 def ReplacePackage(oldname, newname):
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
48 replacePackageMap[oldname] = newname
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
49
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
50
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
51 class Module:
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
52
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
53 def __init__(self, name, file=None, path=None):
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
54 self.__name__ = name
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
55 self.__file__ = file
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
56 self.__path__ = path
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
57 self.__code__ = None
26963
a573fede88e2 patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents: 26961
diff changeset
58 # The set of global names that are assigned to in the module.
a573fede88e2 patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents: 26961
diff changeset
59 # This includes those names imported through starimports of
a573fede88e2 patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents: 26961
diff changeset
60 # Python modules.
a573fede88e2 patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents: 26961
diff changeset
61 self.globalnames = {}
a573fede88e2 patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents: 26961
diff changeset
62 # The set of starimports this module did that could not be
a573fede88e2 patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents: 26961
diff changeset
63 # resolved, ie. a starimport from a non-Python module.
a573fede88e2 patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents: 26961
diff changeset
64 self.starimports = {}
26961
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
65
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
66 def __repr__(self):
31530
25a35d8f4083 Fix typo.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 31515
diff changeset
67 s = "Module(%r" % (self.__name__,)
26961
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
68 if self.__file__ is not None:
31515
71a294bd7950 Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents: 30921
diff changeset
69 s = s + ", %r" % (self.__file__,)
26961
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
70 if self.__path__ is not None:
31515
71a294bd7950 Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents: 30921
diff changeset
71 s = s + ", %r" % (self.__path__,)
26961
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
72 s = s + ")"
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
73 return s
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
74
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
75 class ModuleFinder:
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
76
26963
a573fede88e2 patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents: 26961
diff changeset
77 def __init__(self, path=None, debug=0, excludes=[], replace_paths=[]):
26961
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
78 if path is None:
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
79 path = sys.path
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
80 self.path = path
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
81 self.modules = {}
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
82 self.badmodules = {}
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
83 self.debug = debug
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
84 self.indent = 0
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
85 self.excludes = excludes
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
86 self.replace_paths = replace_paths
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
87 self.processed_paths = [] # Used in debugging only
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
88
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
89 def msg(self, level, str, *args):
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
90 if level <= self.debug:
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
91 for i in range(self.indent):
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
92 print " ",
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
93 print str,
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
94 for arg in args:
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
95 print repr(arg),
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
96 print
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
97
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
98 def msgin(self, *args):
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
99 level = args[0]
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
100 if level <= self.debug:
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
101 self.indent = self.indent + 1
28089
14f65d661084 Get rid of many apply() calls.
Guido van Rossum <guido@python.org>
parents: 27583
diff changeset
102 self.msg(*args)
26961
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
103
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
104 def msgout(self, *args):
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
105 level = args[0]
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
106 if level <= self.debug:
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
107 self.indent = self.indent - 1
28089
14f65d661084 Get rid of many apply() calls.
Guido van Rossum <guido@python.org>
parents: 27583
diff changeset
108 self.msg(*args)
26961
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
109
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
110 def run_script(self, pathname):
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
111 self.msg(2, "run_script", pathname)
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
112 fp = open(pathname, READ_MODE)
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
113 stuff = ("", "r", imp.PY_SOURCE)
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
114 self.load_module('__main__', fp, pathname, stuff)
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
115
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
116 def load_file(self, pathname):
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
117 dir, name = os.path.split(pathname)
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
118 name, ext = os.path.splitext(name)
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
119 fp = open(pathname, READ_MODE)
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
120 stuff = (ext, "r", imp.PY_SOURCE)
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
121 self.load_module(name, fp, pathname, stuff)
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
122
40211
43a826467ad3 Modulefinder now handles absolute and relative imports, including
Thomas Heller <theller@ctypes.org>
parents: 32056
diff changeset
123 def import_hook(self, name, caller=None, fromlist=None, level=-1):
43a826467ad3 Modulefinder now handles absolute and relative imports, including
Thomas Heller <theller@ctypes.org>
parents: 32056
diff changeset
124 self.msg(3, "import_hook", name, caller, fromlist, level)
43a826467ad3 Modulefinder now handles absolute and relative imports, including
Thomas Heller <theller@ctypes.org>
parents: 32056
diff changeset
125 parent = self.determine_parent(caller, level=level)
26961
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
126 q, tail = self.find_head_package(parent, name)
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
127 m = self.load_tail(q, tail)
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
128 if not fromlist:
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
129 return q
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
130 if m.__path__:
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
131 self.ensure_fromlist(m, fromlist)
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
132 return None
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
133
40211
43a826467ad3 Modulefinder now handles absolute and relative imports, including
Thomas Heller <theller@ctypes.org>
parents: 32056
diff changeset
134 def determine_parent(self, caller, level=-1):
43a826467ad3 Modulefinder now handles absolute and relative imports, including
Thomas Heller <theller@ctypes.org>
parents: 32056
diff changeset
135 self.msgin(4, "determine_parent", caller, level)
43a826467ad3 Modulefinder now handles absolute and relative imports, including
Thomas Heller <theller@ctypes.org>
parents: 32056
diff changeset
136 if not caller or level == 0:
26961
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
137 self.msgout(4, "determine_parent -> None")
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
138 return None
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
139 pname = caller.__name__
40211
43a826467ad3 Modulefinder now handles absolute and relative imports, including
Thomas Heller <theller@ctypes.org>
parents: 32056
diff changeset
140 if level >= 1: # relative import
43a826467ad3 Modulefinder now handles absolute and relative imports, including
Thomas Heller <theller@ctypes.org>
parents: 32056
diff changeset
141 if caller.__path__:
43a826467ad3 Modulefinder now handles absolute and relative imports, including
Thomas Heller <theller@ctypes.org>
parents: 32056
diff changeset
142 level -= 1
43a826467ad3 Modulefinder now handles absolute and relative imports, including
Thomas Heller <theller@ctypes.org>
parents: 32056
diff changeset
143 if level == 0:
43a826467ad3 Modulefinder now handles absolute and relative imports, including
Thomas Heller <theller@ctypes.org>
parents: 32056
diff changeset
144 parent = self.modules[pname]
43a826467ad3 Modulefinder now handles absolute and relative imports, including
Thomas Heller <theller@ctypes.org>
parents: 32056
diff changeset
145 assert parent is caller
43a826467ad3 Modulefinder now handles absolute and relative imports, including
Thomas Heller <theller@ctypes.org>
parents: 32056
diff changeset
146 self.msgout(4, "determine_parent ->", parent)
43a826467ad3 Modulefinder now handles absolute and relative imports, including
Thomas Heller <theller@ctypes.org>
parents: 32056
diff changeset
147 return parent
43a826467ad3 Modulefinder now handles absolute and relative imports, including
Thomas Heller <theller@ctypes.org>
parents: 32056
diff changeset
148 if pname.count(".") < level:
43a826467ad3 Modulefinder now handles absolute and relative imports, including
Thomas Heller <theller@ctypes.org>
parents: 32056
diff changeset
149 raise ImportError, "relative importpath too deep"
43a826467ad3 Modulefinder now handles absolute and relative imports, including
Thomas Heller <theller@ctypes.org>
parents: 32056
diff changeset
150 pname = ".".join(pname.split(".")[:-level])
43a826467ad3 Modulefinder now handles absolute and relative imports, including
Thomas Heller <theller@ctypes.org>
parents: 32056
diff changeset
151 parent = self.modules[pname]
43a826467ad3 Modulefinder now handles absolute and relative imports, including
Thomas Heller <theller@ctypes.org>
parents: 32056
diff changeset
152 self.msgout(4, "determine_parent ->", parent)
43a826467ad3 Modulefinder now handles absolute and relative imports, including
Thomas Heller <theller@ctypes.org>
parents: 32056
diff changeset
153 return parent
26961
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
154 if caller.__path__:
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
155 parent = self.modules[pname]
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
156 assert caller is parent
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
157 self.msgout(4, "determine_parent ->", parent)
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
158 return parent
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
159 if '.' in pname:
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
160 i = pname.rfind('.')
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
161 pname = pname[:i]
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
162 parent = self.modules[pname]
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
163 assert parent.__name__ == pname
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
164 self.msgout(4, "determine_parent ->", parent)
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
165 return parent
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
166 self.msgout(4, "determine_parent -> None")
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
167 return None
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
168
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
169 def find_head_package(self, parent, name):
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
170 self.msgin(4, "find_head_package", parent, name)
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
171 if '.' in name:
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
172 i = name.find('.')
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
173 head = name[:i]
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
174 tail = name[i+1:]
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
175 else:
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
176 head = name
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
177 tail = ""
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
178 if parent:
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
179 qname = "%s.%s" % (parent.__name__, head)
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
180 else:
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
181 qname = head
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
182 q = self.import_module(head, qname, parent)
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
183 if q:
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
184 self.msgout(4, "find_head_package ->", (q, tail))
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
185 return q, tail
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
186 if parent:
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
187 qname = head
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
188 parent = None
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
189 q = self.import_module(head, qname, parent)
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
190 if q:
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
191 self.msgout(4, "find_head_package ->", (q, tail))
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
192 return q, tail
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
193 self.msgout(4, "raise ImportError: No module named", qname)
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
194 raise ImportError, "No module named " + qname
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
195
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
196 def load_tail(self, q, tail):
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
197 self.msgin(4, "load_tail", q, tail)
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
198 m = q
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
199 while tail:
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
200 i = tail.find('.')
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
201 if i < 0: i = len(tail)
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
202 head, tail = tail[:i], tail[i+1:]
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
203 mname = "%s.%s" % (m.__name__, head)
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
204 m = self.import_module(head, mname, m)
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
205 if not m:
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
206 self.msgout(4, "raise ImportError: No module named", mname)
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
207 raise ImportError, "No module named " + mname
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
208 self.msgout(4, "load_tail ->", m)
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
209 return m
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
210
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
211 def ensure_fromlist(self, m, fromlist, recursive=0):
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
212 self.msg(4, "ensure_fromlist", m, fromlist, recursive)
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
213 for sub in fromlist:
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
214 if sub == "*":
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
215 if not recursive:
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
216 all = self.find_all_submodules(m)
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
217 if all:
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
218 self.ensure_fromlist(m, all, 1)
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
219 elif not hasattr(m, sub):
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
220 subname = "%s.%s" % (m.__name__, sub)
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
221 submod = self.import_module(sub, subname, m)
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
222 if not submod:
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
223 raise ImportError, "No module named " + subname
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
224
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
225 def find_all_submodules(self, m):
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
226 if not m.__path__:
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
227 return
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
228 modules = {}
30921
0468f9ddc069 SF #841977 - modulefinder fails to find extension modules in packages
Thomas Heller <theller@ctypes.org>
parents: 29941
diff changeset
229 # 'suffixes' used to be a list hardcoded to [".py", ".pyc", ".pyo"].
0468f9ddc069 SF #841977 - modulefinder fails to find extension modules in packages
Thomas Heller <theller@ctypes.org>
parents: 29941
diff changeset
230 # But we must also collect Python extension modules - although
0468f9ddc069 SF #841977 - modulefinder fails to find extension modules in packages
Thomas Heller <theller@ctypes.org>
parents: 29941
diff changeset
231 # we cannot separate normal dlls from Python extensions.
0468f9ddc069 SF #841977 - modulefinder fails to find extension modules in packages
Thomas Heller <theller@ctypes.org>
parents: 29941
diff changeset
232 suffixes = []
0468f9ddc069 SF #841977 - modulefinder fails to find extension modules in packages
Thomas Heller <theller@ctypes.org>
parents: 29941
diff changeset
233 for triple in imp.get_suffixes():
0468f9ddc069 SF #841977 - modulefinder fails to find extension modules in packages
Thomas Heller <theller@ctypes.org>
parents: 29941
diff changeset
234 suffixes.append(triple[0])
26961
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
235 for dir in m.__path__:
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
236 try:
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
237 names = os.listdir(dir)
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
238 except os.error:
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
239 self.msg(2, "can't list directory", dir)
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
240 continue
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
241 for name in names:
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
242 mod = None
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
243 for suff in suffixes:
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
244 n = len(suff)
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
245 if name[-n:] == suff:
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
246 mod = name[:-n]
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
247 break
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
248 if mod and mod != "__init__":
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
249 modules[mod] = mod
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
250 return modules.keys()
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
251
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
252 def import_module(self, partname, fqname, parent):
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
253 self.msgin(3, "import_module", partname, fqname, parent)
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
254 try:
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
255 m = self.modules[fqname]
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
256 except KeyError:
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
257 pass
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
258 else:
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
259 self.msgout(3, "import_module ->", m)
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
260 return m
48158
fdf3b6f499da Silence warnings under -3 about using dict.has_key() for modulefinder.
Brett Cannon <bcannon@gmail.com>
parents: 44034
diff changeset
261 if fqname in self.badmodules:
26961
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
262 self.msgout(3, "import_module -> None")
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
263 return None
32056
1538a8cfb0dd Fix SF item #876278: Unbounded recursion in modulefinder.
Thomas Heller <theller@ctypes.org>
parents: 31530
diff changeset
264 if parent and parent.__path__ is None:
1538a8cfb0dd Fix SF item #876278: Unbounded recursion in modulefinder.
Thomas Heller <theller@ctypes.org>
parents: 31530
diff changeset
265 self.msgout(3, "import_module -> None")
1538a8cfb0dd Fix SF item #876278: Unbounded recursion in modulefinder.
Thomas Heller <theller@ctypes.org>
parents: 31530
diff changeset
266 return None
26961
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
267 try:
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
268 fp, pathname, stuff = self.find_module(partname,
28178
0a8e8632c631 Patch #698082 from Thomas Heller: Modulefinder didn't exclude modules
Just van Rossum <just@letterror.com>
parents: 28089
diff changeset
269 parent and parent.__path__, parent)
26961
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
270 except ImportError:
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
271 self.msgout(3, "import_module ->", None)
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
272 return None
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
273 try:
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
274 m = self.load_module(fqname, fp, pathname, stuff)
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
275 finally:
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
276 if fp: fp.close()
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
277 if parent:
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
278 setattr(parent, partname, m)
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
279 self.msgout(3, "import_module ->", m)
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
280 return m
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
281
48158
fdf3b6f499da Silence warnings under -3 about using dict.has_key() for modulefinder.
Brett Cannon <bcannon@gmail.com>
parents: 44034
diff changeset
282 def load_module(self, fqname, fp, pathname, file_info):
fdf3b6f499da Silence warnings under -3 about using dict.has_key() for modulefinder.
Brett Cannon <bcannon@gmail.com>
parents: 44034
diff changeset
283 suffix, mode, type = file_info
26961
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
284 self.msgin(2, "load_module", fqname, fp and "fp", pathname)
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
285 if type == imp.PKG_DIRECTORY:
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
286 m = self.load_package(fqname, pathname)
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
287 self.msgout(2, "load_module ->", m)
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
288 return m
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
289 if type == imp.PY_SOURCE:
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
290 co = compile(fp.read()+'\n', pathname, 'exec')
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
291 elif type == imp.PY_COMPILED:
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
292 if fp.read(4) != imp.get_magic():
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
293 self.msgout(2, "raise ImportError: Bad magic number", pathname)
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
294 raise ImportError, "Bad magic number in %s" % pathname
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
295 fp.read(4)
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
296 co = marshal.load(fp)
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
297 else:
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
298 co = None
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
299 m = self.add_module(fqname)
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
300 m.__file__ = pathname
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
301 if co:
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
302 if self.replace_paths:
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
303 co = self.replace_paths_in_code(co)
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
304 m.__code__ = co
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
305 self.scan_code(co, m)
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
306 self.msgout(2, "load_module ->", m)
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
307 return m
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
308
26963
a573fede88e2 patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents: 26961
diff changeset
309 def _add_badmodule(self, name, caller):
a573fede88e2 patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents: 26961
diff changeset
310 if name not in self.badmodules:
a573fede88e2 patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents: 26961
diff changeset
311 self.badmodules[name] = {}
49603
3485d95957c1 Merged revisions 67046 via svnmerge from
Thomas Heller <theller@ctypes.org>
parents: 48158
diff changeset
312 if caller:
3485d95957c1 Merged revisions 67046 via svnmerge from
Thomas Heller <theller@ctypes.org>
parents: 48158
diff changeset
313 self.badmodules[name][caller.__name__] = 1
3485d95957c1 Merged revisions 67046 via svnmerge from
Thomas Heller <theller@ctypes.org>
parents: 48158
diff changeset
314 else:
3485d95957c1 Merged revisions 67046 via svnmerge from
Thomas Heller <theller@ctypes.org>
parents: 48158
diff changeset
315 self.badmodules[name]["-"] = 1
26963
a573fede88e2 patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents: 26961
diff changeset
316
40211
43a826467ad3 Modulefinder now handles absolute and relative imports, including
Thomas Heller <theller@ctypes.org>
parents: 32056
diff changeset
317 def _safe_import_hook(self, name, caller, fromlist, level=-1):
26963
a573fede88e2 patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents: 26961
diff changeset
318 # wrapper for self.import_hook() that won't raise ImportError
a573fede88e2 patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents: 26961
diff changeset
319 if name in self.badmodules:
a573fede88e2 patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents: 26961
diff changeset
320 self._add_badmodule(name, caller)
a573fede88e2 patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents: 26961
diff changeset
321 return
a573fede88e2 patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents: 26961
diff changeset
322 try:
40211
43a826467ad3 Modulefinder now handles absolute and relative imports, including
Thomas Heller <theller@ctypes.org>
parents: 32056
diff changeset
323 self.import_hook(name, caller, level=level)
26963
a573fede88e2 patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents: 26961
diff changeset
324 except ImportError, msg:
a573fede88e2 patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents: 26961
diff changeset
325 self.msg(2, "ImportError:", str(msg))
a573fede88e2 patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents: 26961
diff changeset
326 self._add_badmodule(name, caller)
a573fede88e2 patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents: 26961
diff changeset
327 else:
a573fede88e2 patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents: 26961
diff changeset
328 if fromlist:
a573fede88e2 patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents: 26961
diff changeset
329 for sub in fromlist:
a573fede88e2 patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents: 26961
diff changeset
330 if sub in self.badmodules:
a573fede88e2 patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents: 26961
diff changeset
331 self._add_badmodule(sub, caller)
a573fede88e2 patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents: 26961
diff changeset
332 continue
a573fede88e2 patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents: 26961
diff changeset
333 try:
40211
43a826467ad3 Modulefinder now handles absolute and relative imports, including
Thomas Heller <theller@ctypes.org>
parents: 32056
diff changeset
334 self.import_hook(name, caller, [sub], level=level)
26963
a573fede88e2 patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents: 26961
diff changeset
335 except ImportError, msg:
a573fede88e2 patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents: 26961
diff changeset
336 self.msg(2, "ImportError:", str(msg))
a573fede88e2 patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents: 26961
diff changeset
337 fullname = name + "." + sub
a573fede88e2 patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents: 26961
diff changeset
338 self._add_badmodule(fullname, caller)
a573fede88e2 patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents: 26961
diff changeset
339
40211
43a826467ad3 Modulefinder now handles absolute and relative imports, including
Thomas Heller <theller@ctypes.org>
parents: 32056
diff changeset
340 def scan_opcodes(self, co,
43a826467ad3 Modulefinder now handles absolute and relative imports, including
Thomas Heller <theller@ctypes.org>
parents: 32056
diff changeset
341 unpack = struct.unpack):
43a826467ad3 Modulefinder now handles absolute and relative imports, including
Thomas Heller <theller@ctypes.org>
parents: 32056
diff changeset
342 # Scan the code, and yield 'interesting' opcode combinations
43a826467ad3 Modulefinder now handles absolute and relative imports, including
Thomas Heller <theller@ctypes.org>
parents: 32056
diff changeset
343 # Version for Python 2.4 and older
43a826467ad3 Modulefinder now handles absolute and relative imports, including
Thomas Heller <theller@ctypes.org>
parents: 32056
diff changeset
344 code = co.co_code
43a826467ad3 Modulefinder now handles absolute and relative imports, including
Thomas Heller <theller@ctypes.org>
parents: 32056
diff changeset
345 names = co.co_names
43a826467ad3 Modulefinder now handles absolute and relative imports, including
Thomas Heller <theller@ctypes.org>
parents: 32056
diff changeset
346 consts = co.co_consts
43a826467ad3 Modulefinder now handles absolute and relative imports, including
Thomas Heller <theller@ctypes.org>
parents: 32056
diff changeset
347 while code:
43a826467ad3 Modulefinder now handles absolute and relative imports, including
Thomas Heller <theller@ctypes.org>
parents: 32056
diff changeset
348 c = code[0]
43a826467ad3 Modulefinder now handles absolute and relative imports, including
Thomas Heller <theller@ctypes.org>
parents: 32056
diff changeset
349 if c in STORE_OPS:
43a826467ad3 Modulefinder now handles absolute and relative imports, including
Thomas Heller <theller@ctypes.org>
parents: 32056
diff changeset
350 oparg, = unpack('<H', code[1:3])
43a826467ad3 Modulefinder now handles absolute and relative imports, including
Thomas Heller <theller@ctypes.org>
parents: 32056
diff changeset
351 yield "store", (names[oparg],)
43a826467ad3 Modulefinder now handles absolute and relative imports, including
Thomas Heller <theller@ctypes.org>
parents: 32056
diff changeset
352 code = code[3:]
43a826467ad3 Modulefinder now handles absolute and relative imports, including
Thomas Heller <theller@ctypes.org>
parents: 32056
diff changeset
353 continue
43a826467ad3 Modulefinder now handles absolute and relative imports, including
Thomas Heller <theller@ctypes.org>
parents: 32056
diff changeset
354 if c == LOAD_CONST and code[3] == IMPORT_NAME:
43a826467ad3 Modulefinder now handles absolute and relative imports, including
Thomas Heller <theller@ctypes.org>
parents: 32056
diff changeset
355 oparg_1, oparg_2 = unpack('<xHxH', code[:6])
43a826467ad3 Modulefinder now handles absolute and relative imports, including
Thomas Heller <theller@ctypes.org>
parents: 32056
diff changeset
356 yield "import", (consts[oparg_1], names[oparg_2])
43a826467ad3 Modulefinder now handles absolute and relative imports, including
Thomas Heller <theller@ctypes.org>
parents: 32056
diff changeset
357 code = code[6:]
43a826467ad3 Modulefinder now handles absolute and relative imports, including
Thomas Heller <theller@ctypes.org>
parents: 32056
diff changeset
358 continue
43a826467ad3 Modulefinder now handles absolute and relative imports, including
Thomas Heller <theller@ctypes.org>
parents: 32056
diff changeset
359 if c >= HAVE_ARGUMENT:
43a826467ad3 Modulefinder now handles absolute and relative imports, including
Thomas Heller <theller@ctypes.org>
parents: 32056
diff changeset
360 code = code[3:]
43a826467ad3 Modulefinder now handles absolute and relative imports, including
Thomas Heller <theller@ctypes.org>
parents: 32056
diff changeset
361 else:
43a826467ad3 Modulefinder now handles absolute and relative imports, including
Thomas Heller <theller@ctypes.org>
parents: 32056
diff changeset
362 code = code[1:]
43a826467ad3 Modulefinder now handles absolute and relative imports, including
Thomas Heller <theller@ctypes.org>
parents: 32056
diff changeset
363
43a826467ad3 Modulefinder now handles absolute and relative imports, including
Thomas Heller <theller@ctypes.org>
parents: 32056
diff changeset
364 def scan_opcodes_25(self, co,
43a826467ad3 Modulefinder now handles absolute and relative imports, including
Thomas Heller <theller@ctypes.org>
parents: 32056
diff changeset
365 unpack = struct.unpack):
43a826467ad3 Modulefinder now handles absolute and relative imports, including
Thomas Heller <theller@ctypes.org>
parents: 32056
diff changeset
366 # Scan the code, and yield 'interesting' opcode combinations
43a826467ad3 Modulefinder now handles absolute and relative imports, including
Thomas Heller <theller@ctypes.org>
parents: 32056
diff changeset
367 # Python 2.5 version (has absolute and relative imports)
43a826467ad3 Modulefinder now handles absolute and relative imports, including
Thomas Heller <theller@ctypes.org>
parents: 32056
diff changeset
368 code = co.co_code
43a826467ad3 Modulefinder now handles absolute and relative imports, including
Thomas Heller <theller@ctypes.org>
parents: 32056
diff changeset
369 names = co.co_names
43a826467ad3 Modulefinder now handles absolute and relative imports, including
Thomas Heller <theller@ctypes.org>
parents: 32056
diff changeset
370 consts = co.co_consts
43a826467ad3 Modulefinder now handles absolute and relative imports, including
Thomas Heller <theller@ctypes.org>
parents: 32056
diff changeset
371 LOAD_LOAD_AND_IMPORT = LOAD_CONST + LOAD_CONST + IMPORT_NAME
43a826467ad3 Modulefinder now handles absolute and relative imports, including
Thomas Heller <theller@ctypes.org>
parents: 32056
diff changeset
372 while code:
43a826467ad3 Modulefinder now handles absolute and relative imports, including
Thomas Heller <theller@ctypes.org>
parents: 32056
diff changeset
373 c = code[0]
43a826467ad3 Modulefinder now handles absolute and relative imports, including
Thomas Heller <theller@ctypes.org>
parents: 32056
diff changeset
374 if c in STORE_OPS:
43a826467ad3 Modulefinder now handles absolute and relative imports, including
Thomas Heller <theller@ctypes.org>
parents: 32056
diff changeset
375 oparg, = unpack('<H', code[1:3])
43a826467ad3 Modulefinder now handles absolute and relative imports, including
Thomas Heller <theller@ctypes.org>
parents: 32056
diff changeset
376 yield "store", (names[oparg],)
43a826467ad3 Modulefinder now handles absolute and relative imports, including
Thomas Heller <theller@ctypes.org>
parents: 32056
diff changeset
377 code = code[3:]
43a826467ad3 Modulefinder now handles absolute and relative imports, including
Thomas Heller <theller@ctypes.org>
parents: 32056
diff changeset
378 continue
43a826467ad3 Modulefinder now handles absolute and relative imports, including
Thomas Heller <theller@ctypes.org>
parents: 32056
diff changeset
379 if code[:9:3] == LOAD_LOAD_AND_IMPORT:
43a826467ad3 Modulefinder now handles absolute and relative imports, including
Thomas Heller <theller@ctypes.org>
parents: 32056
diff changeset
380 oparg_1, oparg_2, oparg_3 = unpack('<xHxHxH', code[:9])
43a826467ad3 Modulefinder now handles absolute and relative imports, including
Thomas Heller <theller@ctypes.org>
parents: 32056
diff changeset
381 level = consts[oparg_1]
43a826467ad3 Modulefinder now handles absolute and relative imports, including
Thomas Heller <theller@ctypes.org>
parents: 32056
diff changeset
382 if level == -1: # normal import
43a826467ad3 Modulefinder now handles absolute and relative imports, including
Thomas Heller <theller@ctypes.org>
parents: 32056
diff changeset
383 yield "import", (consts[oparg_2], names[oparg_3])
43a826467ad3 Modulefinder now handles absolute and relative imports, including
Thomas Heller <theller@ctypes.org>
parents: 32056
diff changeset
384 elif level == 0: # absolute import
43a826467ad3 Modulefinder now handles absolute and relative imports, including
Thomas Heller <theller@ctypes.org>
parents: 32056
diff changeset
385 yield "absolute_import", (consts[oparg_2], names[oparg_3])
43a826467ad3 Modulefinder now handles absolute and relative imports, including
Thomas Heller <theller@ctypes.org>
parents: 32056
diff changeset
386 else: # relative import
43a826467ad3 Modulefinder now handles absolute and relative imports, including
Thomas Heller <theller@ctypes.org>
parents: 32056
diff changeset
387 yield "relative_import", (level, consts[oparg_2], names[oparg_3])
43a826467ad3 Modulefinder now handles absolute and relative imports, including
Thomas Heller <theller@ctypes.org>
parents: 32056
diff changeset
388 code = code[9:]
43a826467ad3 Modulefinder now handles absolute and relative imports, including
Thomas Heller <theller@ctypes.org>
parents: 32056
diff changeset
389 continue
43a826467ad3 Modulefinder now handles absolute and relative imports, including
Thomas Heller <theller@ctypes.org>
parents: 32056
diff changeset
390 if c >= HAVE_ARGUMENT:
43a826467ad3 Modulefinder now handles absolute and relative imports, including
Thomas Heller <theller@ctypes.org>
parents: 32056
diff changeset
391 code = code[3:]
43a826467ad3 Modulefinder now handles absolute and relative imports, including
Thomas Heller <theller@ctypes.org>
parents: 32056
diff changeset
392 else:
43a826467ad3 Modulefinder now handles absolute and relative imports, including
Thomas Heller <theller@ctypes.org>
parents: 32056
diff changeset
393 code = code[1:]
43a826467ad3 Modulefinder now handles absolute and relative imports, including
Thomas Heller <theller@ctypes.org>
parents: 32056
diff changeset
394
26961
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
395 def scan_code(self, co, m):
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
396 code = co.co_code
40211
43a826467ad3 Modulefinder now handles absolute and relative imports, including
Thomas Heller <theller@ctypes.org>
parents: 32056
diff changeset
397 if sys.version_info >= (2, 5):
43a826467ad3 Modulefinder now handles absolute and relative imports, including
Thomas Heller <theller@ctypes.org>
parents: 32056
diff changeset
398 scanner = self.scan_opcodes_25
43a826467ad3 Modulefinder now handles absolute and relative imports, including
Thomas Heller <theller@ctypes.org>
parents: 32056
diff changeset
399 else:
43a826467ad3 Modulefinder now handles absolute and relative imports, including
Thomas Heller <theller@ctypes.org>
parents: 32056
diff changeset
400 scanner = self.scan_opcodes
43a826467ad3 Modulefinder now handles absolute and relative imports, including
Thomas Heller <theller@ctypes.org>
parents: 32056
diff changeset
401 for what, args in scanner(co):
43a826467ad3 Modulefinder now handles absolute and relative imports, including
Thomas Heller <theller@ctypes.org>
parents: 32056
diff changeset
402 if what == "store":
43a826467ad3 Modulefinder now handles absolute and relative imports, including
Thomas Heller <theller@ctypes.org>
parents: 32056
diff changeset
403 name, = args
43a826467ad3 Modulefinder now handles absolute and relative imports, including
Thomas Heller <theller@ctypes.org>
parents: 32056
diff changeset
404 m.globalnames[name] = 1
43a826467ad3 Modulefinder now handles absolute and relative imports, including
Thomas Heller <theller@ctypes.org>
parents: 32056
diff changeset
405 elif what in ("import", "absolute_import"):
43a826467ad3 Modulefinder now handles absolute and relative imports, including
Thomas Heller <theller@ctypes.org>
parents: 32056
diff changeset
406 fromlist, name = args
26963
a573fede88e2 patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents: 26961
diff changeset
407 have_star = 0
a573fede88e2 patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents: 26961
diff changeset
408 if fromlist is not None:
a573fede88e2 patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents: 26961
diff changeset
409 if "*" in fromlist:
a573fede88e2 patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents: 26961
diff changeset
410 have_star = 1
a573fede88e2 patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents: 26961
diff changeset
411 fromlist = [f for f in fromlist if f != "*"]
40211
43a826467ad3 Modulefinder now handles absolute and relative imports, including
Thomas Heller <theller@ctypes.org>
parents: 32056
diff changeset
412 if what == "absolute_import": level = 0
43a826467ad3 Modulefinder now handles absolute and relative imports, including
Thomas Heller <theller@ctypes.org>
parents: 32056
diff changeset
413 else: level = -1
43a826467ad3 Modulefinder now handles absolute and relative imports, including
Thomas Heller <theller@ctypes.org>
parents: 32056
diff changeset
414 self._safe_import_hook(name, m, fromlist, level=level)
26963
a573fede88e2 patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents: 26961
diff changeset
415 if have_star:
a573fede88e2 patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents: 26961
diff changeset
416 # We've encountered an "import *". If it is a Python module,
a573fede88e2 patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents: 26961
diff changeset
417 # the code has already been parsed and we can suck out the
a573fede88e2 patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents: 26961
diff changeset
418 # global names.
a573fede88e2 patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents: 26961
diff changeset
419 mm = None
a573fede88e2 patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents: 26961
diff changeset
420 if m.__path__:
a573fede88e2 patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents: 26961
diff changeset
421 # At this point we don't know whether 'name' is a
a573fede88e2 patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents: 26961
diff changeset
422 # submodule of 'm' or a global module. Let's just try
a573fede88e2 patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents: 26961
diff changeset
423 # the full name first.
a573fede88e2 patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents: 26961
diff changeset
424 mm = self.modules.get(m.__name__ + "." + name)
a573fede88e2 patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents: 26961
diff changeset
425 if mm is None:
a573fede88e2 patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents: 26961
diff changeset
426 mm = self.modules.get(name)
a573fede88e2 patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents: 26961
diff changeset
427 if mm is not None:
a573fede88e2 patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents: 26961
diff changeset
428 m.globalnames.update(mm.globalnames)
a573fede88e2 patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents: 26961
diff changeset
429 m.starimports.update(mm.starimports)
a573fede88e2 patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents: 26961
diff changeset
430 if mm.__code__ is None:
a573fede88e2 patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents: 26961
diff changeset
431 m.starimports[name] = 1
a573fede88e2 patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents: 26961
diff changeset
432 else:
a573fede88e2 patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents: 26961
diff changeset
433 m.starimports[name] = 1
40211
43a826467ad3 Modulefinder now handles absolute and relative imports, including
Thomas Heller <theller@ctypes.org>
parents: 32056
diff changeset
434 elif what == "relative_import":
43a826467ad3 Modulefinder now handles absolute and relative imports, including
Thomas Heller <theller@ctypes.org>
parents: 32056
diff changeset
435 level, fromlist, name = args
43a826467ad3 Modulefinder now handles absolute and relative imports, including
Thomas Heller <theller@ctypes.org>
parents: 32056
diff changeset
436 if name:
43a826467ad3 Modulefinder now handles absolute and relative imports, including
Thomas Heller <theller@ctypes.org>
parents: 32056
diff changeset
437 self._safe_import_hook(name, m, fromlist, level=level)
43a826467ad3 Modulefinder now handles absolute and relative imports, including
Thomas Heller <theller@ctypes.org>
parents: 32056
diff changeset
438 else:
43a826467ad3 Modulefinder now handles absolute and relative imports, including
Thomas Heller <theller@ctypes.org>
parents: 32056
diff changeset
439 parent = self.determine_parent(m, level=level)
43a826467ad3 Modulefinder now handles absolute and relative imports, including
Thomas Heller <theller@ctypes.org>
parents: 32056
diff changeset
440 self._safe_import_hook(parent.__name__, None, fromlist, level=0)
43a826467ad3 Modulefinder now handles absolute and relative imports, including
Thomas Heller <theller@ctypes.org>
parents: 32056
diff changeset
441 else:
43a826467ad3 Modulefinder now handles absolute and relative imports, including
Thomas Heller <theller@ctypes.org>
parents: 32056
diff changeset
442 # We don't expect anything else from the generator.
43a826467ad3 Modulefinder now handles absolute and relative imports, including
Thomas Heller <theller@ctypes.org>
parents: 32056
diff changeset
443 raise RuntimeError(what)
43a826467ad3 Modulefinder now handles absolute and relative imports, including
Thomas Heller <theller@ctypes.org>
parents: 32056
diff changeset
444
26961
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
445 for c in co.co_consts:
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
446 if isinstance(c, type(co)):
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
447 self.scan_code(c, m)
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
448
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
449 def load_package(self, fqname, pathname):
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
450 self.msgin(2, "load_package", fqname, pathname)
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
451 newname = replacePackageMap.get(fqname)
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
452 if newname:
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
453 fqname = newname
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
454 m = self.add_module(fqname)
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
455 m.__file__ = pathname
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
456 m.__path__ = [pathname]
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
457
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
458 # As per comment at top of file, simulate runtime __path__ additions.
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
459 m.__path__ = m.__path__ + packagePathMap.get(fqname, [])
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
460
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
461 fp, buf, stuff = self.find_module("__init__", m.__path__)
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
462 self.load_module(fqname, fp, buf, stuff)
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
463 self.msgout(2, "load_package ->", m)
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
464 return m
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
465
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
466 def add_module(self, fqname):
48158
fdf3b6f499da Silence warnings under -3 about using dict.has_key() for modulefinder.
Brett Cannon <bcannon@gmail.com>
parents: 44034
diff changeset
467 if fqname in self.modules:
26961
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
468 return self.modules[fqname]
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
469 self.modules[fqname] = m = Module(fqname)
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
470 return m
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
471
28178
0a8e8632c631 Patch #698082 from Thomas Heller: Modulefinder didn't exclude modules
Just van Rossum <just@letterror.com>
parents: 28089
diff changeset
472 def find_module(self, name, path, parent=None):
0a8e8632c631 Patch #698082 from Thomas Heller: Modulefinder didn't exclude modules
Just van Rossum <just@letterror.com>
parents: 28089
diff changeset
473 if parent is not None:
32056
1538a8cfb0dd Fix SF item #876278: Unbounded recursion in modulefinder.
Thomas Heller <theller@ctypes.org>
parents: 31530
diff changeset
474 # assert path is not None
28178
0a8e8632c631 Patch #698082 from Thomas Heller: Modulefinder didn't exclude modules
Just van Rossum <just@letterror.com>
parents: 28089
diff changeset
475 fullname = parent.__name__+'.'+name
26961
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
476 else:
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
477 fullname = name
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
478 if fullname in self.excludes:
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
479 self.msgout(3, "find_module -> Excluded", fullname)
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
480 raise ImportError, name
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
481
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
482 if path is None:
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
483 if name in sys.builtin_module_names:
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
484 return (None, None, ("", "", imp.C_BUILTIN))
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
485
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
486 path = self.path
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
487 return imp.find_module(name, path)
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
488
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
489 def report(self):
26963
a573fede88e2 patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents: 26961
diff changeset
490 """Print a report to stdout, listing the found modules with their
a573fede88e2 patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents: 26961
diff changeset
491 paths, as well as modules that are missing, or seem to be missing.
a573fede88e2 patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents: 26961
diff changeset
492 """
26961
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
493 print
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
494 print " %-25s %s" % ("Name", "File")
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
495 print " %-25s %s" % ("----", "----")
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
496 # Print modules found
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
497 keys = self.modules.keys()
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
498 keys.sort()
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
499 for key in keys:
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
500 m = self.modules[key]
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
501 if m.__path__:
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
502 print "P",
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
503 else:
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
504 print "m",
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
505 print "%-25s" % key, m.__file__ or ""
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
506
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
507 # Print missing modules
26963
a573fede88e2 patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents: 26961
diff changeset
508 missing, maybe = self.any_missing_maybe()
a573fede88e2 patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents: 26961
diff changeset
509 if missing:
a573fede88e2 patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents: 26961
diff changeset
510 print
a573fede88e2 patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents: 26961
diff changeset
511 print "Missing modules:"
a573fede88e2 patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents: 26961
diff changeset
512 for name in missing:
a573fede88e2 patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents: 26961
diff changeset
513 mods = self.badmodules[name].keys()
26961
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
514 mods.sort()
26963
a573fede88e2 patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents: 26961
diff changeset
515 print "?", name, "imported from", ', '.join(mods)
a573fede88e2 patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents: 26961
diff changeset
516 # Print modules that may be missing, but then again, maybe not...
a573fede88e2 patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents: 26961
diff changeset
517 if maybe:
a573fede88e2 patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents: 26961
diff changeset
518 print
a573fede88e2 patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents: 26961
diff changeset
519 print "Submodules thay appear to be missing, but could also be",
a573fede88e2 patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents: 26961
diff changeset
520 print "global names in the parent package:"
a573fede88e2 patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents: 26961
diff changeset
521 for name in maybe:
a573fede88e2 patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents: 26961
diff changeset
522 mods = self.badmodules[name].keys()
a573fede88e2 patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents: 26961
diff changeset
523 mods.sort()
a573fede88e2 patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents: 26961
diff changeset
524 print "?", name, "imported from", ', '.join(mods)
26961
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
525
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
526 def any_missing(self):
26963
a573fede88e2 patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents: 26961
diff changeset
527 """Return a list of modules that appear to be missing. Use
a573fede88e2 patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents: 26961
diff changeset
528 any_missing_maybe() if you want to know which modules are
a573fede88e2 patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents: 26961
diff changeset
529 certain to be missing, and which *may* be missing.
a573fede88e2 patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents: 26961
diff changeset
530 """
a573fede88e2 patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents: 26961
diff changeset
531 missing, maybe = self.any_missing_maybe()
a573fede88e2 patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents: 26961
diff changeset
532 return missing + maybe
a573fede88e2 patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents: 26961
diff changeset
533
a573fede88e2 patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents: 26961
diff changeset
534 def any_missing_maybe(self):
a573fede88e2 patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents: 26961
diff changeset
535 """Return two lists, one with modules that are certainly missing
a573fede88e2 patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents: 26961
diff changeset
536 and one with modules that *may* be missing. The latter names could
a573fede88e2 patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents: 26961
diff changeset
537 either be submodules *or* just global names in the package.
a573fede88e2 patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents: 26961
diff changeset
538
a573fede88e2 patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents: 26961
diff changeset
539 The reason it can't always be determined is that it's impossible to
a573fede88e2 patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents: 26961
diff changeset
540 tell which names are imported when "from module import *" is done
a573fede88e2 patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents: 26961
diff changeset
541 with an extension module, short of actually importing it.
a573fede88e2 patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents: 26961
diff changeset
542 """
26961
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
543 missing = []
26963
a573fede88e2 patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents: 26961
diff changeset
544 maybe = []
a573fede88e2 patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents: 26961
diff changeset
545 for name in self.badmodules:
a573fede88e2 patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents: 26961
diff changeset
546 if name in self.excludes:
a573fede88e2 patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents: 26961
diff changeset
547 continue
a573fede88e2 patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents: 26961
diff changeset
548 i = name.rfind(".")
a573fede88e2 patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents: 26961
diff changeset
549 if i < 0:
a573fede88e2 patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents: 26961
diff changeset
550 missing.append(name)
a573fede88e2 patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents: 26961
diff changeset
551 continue
a573fede88e2 patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents: 26961
diff changeset
552 subname = name[i+1:]
a573fede88e2 patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents: 26961
diff changeset
553 pkgname = name[:i]
a573fede88e2 patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents: 26961
diff changeset
554 pkg = self.modules.get(pkgname)
a573fede88e2 patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents: 26961
diff changeset
555 if pkg is not None:
a573fede88e2 patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents: 26961
diff changeset
556 if pkgname in self.badmodules[name]:
a573fede88e2 patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents: 26961
diff changeset
557 # The package tried to import this module itself and
a573fede88e2 patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents: 26961
diff changeset
558 # failed. It's definitely missing.
a573fede88e2 patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents: 26961
diff changeset
559 missing.append(name)
a573fede88e2 patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents: 26961
diff changeset
560 elif subname in pkg.globalnames:
a573fede88e2 patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents: 26961
diff changeset
561 # It's a global in the package: definitely not missing.
a573fede88e2 patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents: 26961
diff changeset
562 pass
a573fede88e2 patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents: 26961
diff changeset
563 elif pkg.starimports:
a573fede88e2 patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents: 26961
diff changeset
564 # It could be missing, but the package did an "import *"
a573fede88e2 patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents: 26961
diff changeset
565 # from a non-Python module, so we simply can't be sure.
a573fede88e2 patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents: 26961
diff changeset
566 maybe.append(name)
a573fede88e2 patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents: 26961
diff changeset
567 else:
a573fede88e2 patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents: 26961
diff changeset
568 # It's not a global in the package, the package didn't
a573fede88e2 patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents: 26961
diff changeset
569 # do funny star imports, it's very likely to be missing.
a573fede88e2 patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents: 26961
diff changeset
570 # The symbol could be inserted into the package from the
a573fede88e2 patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents: 26961
diff changeset
571 # outside, but since that's not good style we simply list
a573fede88e2 patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents: 26961
diff changeset
572 # it missing.
a573fede88e2 patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents: 26961
diff changeset
573 missing.append(name)
a573fede88e2 patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents: 26961
diff changeset
574 else:
a573fede88e2 patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents: 26961
diff changeset
575 missing.append(name)
a573fede88e2 patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents: 26961
diff changeset
576 missing.sort()
a573fede88e2 patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents: 26961
diff changeset
577 maybe.sort()
a573fede88e2 patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents: 26961
diff changeset
578 return missing, maybe
26961
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
579
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
580 def replace_paths_in_code(self, co):
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
581 new_filename = original_filename = os.path.normpath(co.co_filename)
26963
a573fede88e2 patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents: 26961
diff changeset
582 for f, r in self.replace_paths:
26961
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
583 if original_filename.startswith(f):
26963
a573fede88e2 patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents: 26961
diff changeset
584 new_filename = r + original_filename[len(f):]
26961
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
585 break
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
586
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
587 if self.debug and original_filename not in self.processed_paths:
26963
a573fede88e2 patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents: 26961
diff changeset
588 if new_filename != original_filename:
26961
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
589 self.msgout(2, "co_filename %r changed to %r" \
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
590 % (original_filename,new_filename,))
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
591 else:
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
592 self.msgout(2, "co_filename %r remains unchanged" \
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
593 % (original_filename,))
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
594 self.processed_paths.append(original_filename)
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
595
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
596 consts = list(co.co_consts)
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
597 for i in range(len(consts)):
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
598 if isinstance(consts[i], type(co)):
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
599 consts[i] = self.replace_paths_in_code(consts[i])
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
600
44034
5d1049d4c3c6 Replaced import of the 'new' module with 'types' module and added a deprecation warning to the 'new' module.
Christian Heimes <christian@cheimes.de>
parents: 40211
diff changeset
601 return types.CodeType(co.co_argcount, co.co_nlocals, co.co_stacksize,
27507
d9d57f5f98cd Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 26963
diff changeset
602 co.co_flags, co.co_code, tuple(consts), co.co_names,
d9d57f5f98cd Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 26963
diff changeset
603 co.co_varnames, new_filename, co.co_name,
26961
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
604 co.co_firstlineno, co.co_lnotab,
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
605 co.co_freevars, co.co_cellvars)
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
606
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
607
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
608 def test():
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
609 # Parse command line
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
610 import getopt
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
611 try:
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
612 opts, args = getopt.getopt(sys.argv[1:], "dmp:qx:")
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
613 except getopt.error, msg:
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
614 print msg
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
615 return
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
616
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
617 # Process options
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
618 debug = 1
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
619 domods = 0
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
620 addpath = []
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
621 exclude = []
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
622 for o, a in opts:
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
623 if o == '-d':
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
624 debug = debug + 1
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
625 if o == '-m':
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
626 domods = 1
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
627 if o == '-p':
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
628 addpath = addpath + a.split(os.pathsep)
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
629 if o == '-q':
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
630 debug = 0
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
631 if o == '-x':
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
632 exclude.append(a)
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
633
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
634 # Provide default arguments
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
635 if not args:
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
636 script = "hello.py"
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
637 else:
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
638 script = args[0]
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
639
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
640 # Set the path based on sys.path and the script directory
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
641 path = sys.path[:]
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
642 path[0] = os.path.dirname(script)
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
643 path = addpath + path
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
644 if debug > 1:
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
645 print "path:"
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
646 for item in path:
31515
71a294bd7950 Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents: 30921
diff changeset
647 print " ", repr(item)
26961
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
648
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
649 # Create the module finder and turn its crank
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
650 mf = ModuleFinder(path, debug, exclude)
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
651 for arg in args[1:]:
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
652 if arg == '-m':
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
653 domods = 1
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
654 continue
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
655 if domods:
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
656 if arg[-2:] == '.*':
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
657 mf.import_hook(arg[:-2], None, ["*"])
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
658 else:
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
659 mf.import_hook(arg)
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
660 else:
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
661 mf.load_file(arg)
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
662 mf.run_script(script)
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
663 mf.report()
26963
a573fede88e2 patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents: 26961
diff changeset
664 return mf # for -i debugging
26961
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
665
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
666
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
667 if __name__ == '__main__':
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
668 try:
26963
a573fede88e2 patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents: 26961
diff changeset
669 mf = test()
26961
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
670 except KeyboardInterrupt:
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
671 print "\n[interrupt]"