annotate Lib/modulefinder.py @ 27998:e27f58a50737 legacy-trunk

SF bug #690377: Dud footnote reference in distutils docs
author Raymond Hettinger <python@rcn.com>
date Fri, 21 Feb 2003 02:47:09 +0000
parents a2db437d314b
children 14f65d661084
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."""
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
2
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
3 # This module should be kept compatible with Python 1.5.2, see PEP 291.
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
4
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
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
10 import new
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
11
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
12 if hasattr(sys.__stdout__, "newlines"):
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
13 READ_MODE = "U" # universal line endings
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
14 else:
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
15 # remain compatible with Python < 2.3
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
16 READ_MODE = "r"
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
17
26963
a573fede88e2 patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents: 26961
diff changeset
18 LOAD_CONST = dis.opname.index('LOAD_CONST')
26961
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
19 IMPORT_NAME = dis.opname.index('IMPORT_NAME')
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
20 STORE_NAME = dis.opname.index('STORE_NAME')
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
21 STORE_GLOBAL = dis.opname.index('STORE_GLOBAL')
26963
a573fede88e2 patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents: 26961
diff changeset
22 STORE_OPS = [STORE_NAME, STORE_GLOBAL]
26961
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
23
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
24 # 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
25 # 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
26 # 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
27 # package, and it will be honored.
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
28
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
29 # 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
30 packagePathMap = {}
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
31
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
32 # A Public interface
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
33 def AddPackagePath(packagename, path):
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
34 paths = packagePathMap.get(packagename, [])
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
35 paths.append(path)
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
36 packagePathMap[packagename] = paths
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
37
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
38 replacePackageMap = {}
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 # 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
41 # 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
42 # 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
43 # before running ModuleFinder.
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
44
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
45 def ReplacePackage(oldname, newname):
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
46 replacePackageMap[oldname] = newname
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
47
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
48
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
49 class Module:
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 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
52 self.__name__ = name
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
53 self.__file__ = file
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
54 self.__path__ = path
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
55 self.__code__ = None
26963
a573fede88e2 patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents: 26961
diff changeset
56 # 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
57 # 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
58 # Python modules.
a573fede88e2 patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents: 26961
diff changeset
59 self.globalnames = {}
a573fede88e2 patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents: 26961
diff changeset
60 # 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
61 # 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
62 self.starimports = {}
26961
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
63
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
64 def __repr__(self):
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
65 s = "Module(%s" % `self.__name__`
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
66 if self.__file__ is not None:
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
67 s = s + ", %s" % `self.__file__`
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
68 if self.__path__ is not None:
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
69 s = s + ", %s" % `self.__path__`
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
70 s = s + ")"
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
71 return s
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
72
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
73 class ModuleFinder:
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
74
26963
a573fede88e2 patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents: 26961
diff changeset
75 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
76 if path is None:
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
77 path = sys.path
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
78 self.path = path
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
79 self.modules = {}
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
80 self.badmodules = {}
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
81 self.debug = debug
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
82 self.indent = 0
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
83 self.excludes = excludes
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
84 self.replace_paths = replace_paths
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
85 self.processed_paths = [] # Used in debugging only
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
86
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
87 def msg(self, level, str, *args):
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
88 if level <= self.debug:
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
89 for i in range(self.indent):
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
90 print " ",
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
91 print str,
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
92 for arg in args:
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
93 print repr(arg),
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
94 print
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
95
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
96 def msgin(self, *args):
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
97 level = args[0]
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
98 if level <= self.debug:
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
99 self.indent = self.indent + 1
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
100 apply(self.msg, args)
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
101
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
102 def msgout(self, *args):
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
103 level = args[0]
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
104 if level <= self.debug:
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
105 self.indent = self.indent - 1
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
106 apply(self.msg, args)
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
107
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
108 def run_script(self, pathname):
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
109 self.msg(2, "run_script", pathname)
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
110 fp = open(pathname, READ_MODE)
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
111 stuff = ("", "r", imp.PY_SOURCE)
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
112 self.load_module('__main__', fp, pathname, stuff)
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
113
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
114 def load_file(self, pathname):
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
115 dir, name = os.path.split(pathname)
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
116 name, ext = os.path.splitext(name)
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
117 fp = open(pathname, READ_MODE)
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
118 stuff = (ext, "r", imp.PY_SOURCE)
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
119 self.load_module(name, fp, pathname, stuff)
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
120
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
121 def import_hook(self, name, caller=None, fromlist=None):
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
122 self.msg(3, "import_hook", name, caller, fromlist)
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
123 parent = self.determine_parent(caller)
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
124 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
125 m = self.load_tail(q, tail)
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
126 if not fromlist:
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
127 return q
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
128 if m.__path__:
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
129 self.ensure_fromlist(m, fromlist)
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
130 return None
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
131
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
132 def determine_parent(self, caller):
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
133 self.msgin(4, "determine_parent", caller)
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
134 if not caller:
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
135 self.msgout(4, "determine_parent -> None")
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
136 return None
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
137 pname = caller.__name__
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
138 if caller.__path__:
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
139 parent = self.modules[pname]
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
140 assert caller is parent
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
141 self.msgout(4, "determine_parent ->", parent)
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
142 return parent
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
143 if '.' in pname:
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
144 i = pname.rfind('.')
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
145 pname = pname[:i]
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
146 parent = self.modules[pname]
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
147 assert parent.__name__ == pname
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
148 self.msgout(4, "determine_parent ->", parent)
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
149 return parent
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
150 self.msgout(4, "determine_parent -> None")
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
151 return None
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
152
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
153 def find_head_package(self, parent, name):
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
154 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
155 if '.' in name:
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
156 i = name.find('.')
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
157 head = name[:i]
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
158 tail = name[i+1:]
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
159 else:
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
160 head = name
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
161 tail = ""
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
162 if parent:
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
163 qname = "%s.%s" % (parent.__name__, head)
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
164 else:
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
165 qname = head
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
166 q = self.import_module(head, qname, parent)
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
167 if q:
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
168 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
169 return q, tail
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
170 if parent:
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
171 qname = head
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
172 parent = None
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
173 q = self.import_module(head, qname, parent)
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
174 if q:
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
175 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
176 return q, tail
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
177 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
178 raise ImportError, "No module named " + qname
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
179
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
180 def load_tail(self, q, tail):
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
181 self.msgin(4, "load_tail", q, tail)
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
182 m = q
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
183 while tail:
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
184 i = tail.find('.')
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
185 if i < 0: i = len(tail)
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
186 head, tail = tail[:i], tail[i+1:]
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
187 mname = "%s.%s" % (m.__name__, head)
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
188 m = self.import_module(head, mname, m)
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
189 if not m:
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
190 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
191 raise ImportError, "No module named " + mname
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
192 self.msgout(4, "load_tail ->", m)
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
193 return m
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
194
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
195 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
196 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
197 for sub in fromlist:
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
198 if sub == "*":
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
199 if not recursive:
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
200 all = self.find_all_submodules(m)
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
201 if all:
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
202 self.ensure_fromlist(m, all, 1)
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
203 elif not hasattr(m, sub):
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
204 subname = "%s.%s" % (m.__name__, sub)
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
205 submod = self.import_module(sub, subname, m)
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
206 if not submod:
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
207 raise ImportError, "No module named " + subname
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
208
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
209 def find_all_submodules(self, m):
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
210 if not m.__path__:
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
211 return
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
212 modules = {}
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
213 suffixes = [".py", ".pyc", ".pyo"]
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
214 for dir in m.__path__:
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
215 try:
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
216 names = os.listdir(dir)
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
217 except os.error:
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
218 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
219 continue
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
220 for name in names:
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
221 mod = None
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
222 for suff in suffixes:
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
223 n = len(suff)
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
224 if name[-n:] == suff:
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
225 mod = name[:-n]
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
226 break
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
227 if mod and mod != "__init__":
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
228 modules[mod] = mod
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
229 return modules.keys()
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
230
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
231 def import_module(self, partname, fqname, parent):
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
232 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
233 try:
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
234 m = self.modules[fqname]
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
235 except KeyError:
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
236 pass
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
237 else:
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
238 self.msgout(3, "import_module ->", m)
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
239 return m
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
240 if self.badmodules.has_key(fqname):
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
241 self.msgout(3, "import_module -> None")
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
242 return None
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
243 try:
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
244 fp, pathname, stuff = self.find_module(partname,
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
245 parent and parent.__path__)
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
246 except ImportError:
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
247 self.msgout(3, "import_module ->", None)
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
248 return None
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
249 try:
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
250 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
251 finally:
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
252 if fp: fp.close()
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
253 if parent:
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
254 setattr(parent, partname, m)
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
255 self.msgout(3, "import_module ->", m)
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
256 return m
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
257
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
258 def load_module(self, fqname, fp, pathname, (suffix, mode, type)):
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
259 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
260 if type == imp.PKG_DIRECTORY:
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
261 m = self.load_package(fqname, pathname)
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
262 self.msgout(2, "load_module ->", m)
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
263 return m
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
264 if type == imp.PY_SOURCE:
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
265 co = compile(fp.read()+'\n', pathname, 'exec')
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
266 elif type == imp.PY_COMPILED:
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
267 if fp.read(4) != imp.get_magic():
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
268 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
269 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
270 fp.read(4)
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
271 co = marshal.load(fp)
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
272 else:
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
273 co = None
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
274 m = self.add_module(fqname)
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
275 m.__file__ = pathname
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
276 if co:
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
277 if self.replace_paths:
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
278 co = self.replace_paths_in_code(co)
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
279 m.__code__ = co
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
280 self.scan_code(co, m)
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
281 self.msgout(2, "load_module ->", m)
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
282 return m
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
283
26963
a573fede88e2 patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents: 26961
diff changeset
284 def _add_badmodule(self, name, caller):
a573fede88e2 patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents: 26961
diff changeset
285 if name not in self.badmodules:
a573fede88e2 patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents: 26961
diff changeset
286 self.badmodules[name] = {}
a573fede88e2 patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents: 26961
diff changeset
287 self.badmodules[name][caller.__name__] = 1
a573fede88e2 patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents: 26961
diff changeset
288
a573fede88e2 patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents: 26961
diff changeset
289 def _safe_import_hook(self, name, caller, fromlist):
a573fede88e2 patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents: 26961
diff changeset
290 # 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
291 if name in self.badmodules:
a573fede88e2 patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents: 26961
diff changeset
292 self._add_badmodule(name, caller)
a573fede88e2 patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents: 26961
diff changeset
293 return
a573fede88e2 patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents: 26961
diff changeset
294 try:
a573fede88e2 patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents: 26961
diff changeset
295 self.import_hook(name, caller)
a573fede88e2 patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents: 26961
diff changeset
296 except ImportError, msg:
a573fede88e2 patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents: 26961
diff changeset
297 self.msg(2, "ImportError:", str(msg))
a573fede88e2 patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents: 26961
diff changeset
298 self._add_badmodule(name, caller)
a573fede88e2 patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents: 26961
diff changeset
299 else:
a573fede88e2 patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents: 26961
diff changeset
300 if fromlist:
a573fede88e2 patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents: 26961
diff changeset
301 for sub in fromlist:
a573fede88e2 patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents: 26961
diff changeset
302 if sub in self.badmodules:
a573fede88e2 patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents: 26961
diff changeset
303 self._add_badmodule(sub, caller)
a573fede88e2 patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents: 26961
diff changeset
304 continue
a573fede88e2 patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents: 26961
diff changeset
305 try:
a573fede88e2 patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents: 26961
diff changeset
306 self.import_hook(name, caller, [sub])
a573fede88e2 patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents: 26961
diff changeset
307 except ImportError, msg:
a573fede88e2 patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents: 26961
diff changeset
308 self.msg(2, "ImportError:", str(msg))
a573fede88e2 patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents: 26961
diff changeset
309 fullname = name + "." + sub
a573fede88e2 patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents: 26961
diff changeset
310 self._add_badmodule(fullname, caller)
a573fede88e2 patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents: 26961
diff changeset
311
26961
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
312 def scan_code(self, co, m):
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
313 code = co.co_code
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
314 n = len(code)
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
315 i = 0
26963
a573fede88e2 patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents: 26961
diff changeset
316 fromlist = None
26961
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
317 while i < n:
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
318 c = code[i]
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
319 i = i+1
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
320 op = ord(c)
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
321 if op >= dis.HAVE_ARGUMENT:
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
322 oparg = ord(code[i]) + ord(code[i+1])*256
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
323 i = i+2
26963
a573fede88e2 patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents: 26961
diff changeset
324 if op == LOAD_CONST:
a573fede88e2 patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents: 26961
diff changeset
325 # An IMPORT_NAME is always preceded by a LOAD_CONST, it's
a573fede88e2 patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents: 26961
diff changeset
326 # a tuple of "from" names, or None for a regular import.
a573fede88e2 patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents: 26961
diff changeset
327 # The tuple may contain "*" for "from <mod> import *"
a573fede88e2 patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents: 26961
diff changeset
328 fromlist = co.co_consts[oparg]
a573fede88e2 patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents: 26961
diff changeset
329 elif op == IMPORT_NAME:
a573fede88e2 patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents: 26961
diff changeset
330 assert fromlist is None or type(fromlist) is tuple
26961
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
331 name = co.co_names[oparg]
26963
a573fede88e2 patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents: 26961
diff changeset
332 have_star = 0
a573fede88e2 patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents: 26961
diff changeset
333 if fromlist is not None:
a573fede88e2 patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents: 26961
diff changeset
334 if "*" in fromlist:
a573fede88e2 patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents: 26961
diff changeset
335 have_star = 1
a573fede88e2 patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents: 26961
diff changeset
336 fromlist = [f for f in fromlist if f != "*"]
a573fede88e2 patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents: 26961
diff changeset
337 self._safe_import_hook(name, m, fromlist)
a573fede88e2 patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents: 26961
diff changeset
338 if have_star:
a573fede88e2 patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents: 26961
diff changeset
339 # 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
340 # 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
341 # global names.
a573fede88e2 patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents: 26961
diff changeset
342 mm = None
a573fede88e2 patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents: 26961
diff changeset
343 if m.__path__:
a573fede88e2 patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents: 26961
diff changeset
344 # 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
345 # 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
346 # the full name first.
a573fede88e2 patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents: 26961
diff changeset
347 mm = self.modules.get(m.__name__ + "." + name)
a573fede88e2 patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents: 26961
diff changeset
348 if mm is None:
a573fede88e2 patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents: 26961
diff changeset
349 mm = self.modules.get(name)
a573fede88e2 patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents: 26961
diff changeset
350 if mm is not None:
a573fede88e2 patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents: 26961
diff changeset
351 m.globalnames.update(mm.globalnames)
a573fede88e2 patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents: 26961
diff changeset
352 m.starimports.update(mm.starimports)
a573fede88e2 patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents: 26961
diff changeset
353 if mm.__code__ is None:
a573fede88e2 patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents: 26961
diff changeset
354 m.starimports[name] = 1
a573fede88e2 patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents: 26961
diff changeset
355 else:
a573fede88e2 patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents: 26961
diff changeset
356 m.starimports[name] = 1
26961
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
357 elif op in STORE_OPS:
26963
a573fede88e2 patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents: 26961
diff changeset
358 # keep track of all global names that are assigned to
a573fede88e2 patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents: 26961
diff changeset
359 name = co.co_names[oparg]
a573fede88e2 patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents: 26961
diff changeset
360 m.globalnames[name] = 1
26961
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
361 for c in co.co_consts:
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
362 if isinstance(c, type(co)):
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
363 self.scan_code(c, m)
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
364
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
365 def load_package(self, fqname, pathname):
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
366 self.msgin(2, "load_package", fqname, pathname)
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
367 newname = replacePackageMap.get(fqname)
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
368 if newname:
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
369 fqname = newname
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
370 m = self.add_module(fqname)
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
371 m.__file__ = pathname
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
372 m.__path__ = [pathname]
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
373
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
374 # 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
375 m.__path__ = m.__path__ + packagePathMap.get(fqname, [])
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
376
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
377 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
378 self.load_module(fqname, fp, buf, stuff)
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
379 self.msgout(2, "load_package ->", m)
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
380 return m
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
381
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
382 def add_module(self, fqname):
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
383 if self.modules.has_key(fqname):
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
384 return self.modules[fqname]
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
385 self.modules[fqname] = m = Module(fqname)
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
386 return m
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
387
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
388 def find_module(self, name, path):
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
389 if path:
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
390 fullname = '.'.join(path)+'.'+name
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
391 else:
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
392 fullname = name
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
393 if fullname in self.excludes:
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
394 self.msgout(3, "find_module -> Excluded", fullname)
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
395 raise ImportError, name
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
396
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
397 if path is None:
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
398 if name in sys.builtin_module_names:
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
399 return (None, None, ("", "", imp.C_BUILTIN))
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
400
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
401 path = self.path
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
402 return imp.find_module(name, path)
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
403
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
404 def report(self):
26963
a573fede88e2 patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents: 26961
diff changeset
405 """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
406 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
407 """
26961
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
408 print
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
409 print " %-25s %s" % ("Name", "File")
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
410 print " %-25s %s" % ("----", "----")
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
411 # Print modules found
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
412 keys = self.modules.keys()
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
413 keys.sort()
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
414 for key in keys:
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
415 m = self.modules[key]
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
416 if m.__path__:
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
417 print "P",
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
418 else:
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
419 print "m",
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
420 print "%-25s" % key, m.__file__ or ""
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
421
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
422 # Print missing modules
26963
a573fede88e2 patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents: 26961
diff changeset
423 missing, maybe = self.any_missing_maybe()
a573fede88e2 patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents: 26961
diff changeset
424 if missing:
a573fede88e2 patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents: 26961
diff changeset
425 print
a573fede88e2 patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents: 26961
diff changeset
426 print "Missing modules:"
a573fede88e2 patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents: 26961
diff changeset
427 for name in missing:
a573fede88e2 patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents: 26961
diff changeset
428 mods = self.badmodules[name].keys()
26961
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
429 mods.sort()
26963
a573fede88e2 patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents: 26961
diff changeset
430 print "?", name, "imported from", ', '.join(mods)
a573fede88e2 patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents: 26961
diff changeset
431 # 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
432 if maybe:
a573fede88e2 patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents: 26961
diff changeset
433 print
a573fede88e2 patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents: 26961
diff changeset
434 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
435 print "global names in the parent package:"
a573fede88e2 patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents: 26961
diff changeset
436 for name in maybe:
a573fede88e2 patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents: 26961
diff changeset
437 mods = self.badmodules[name].keys()
a573fede88e2 patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents: 26961
diff changeset
438 mods.sort()
a573fede88e2 patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents: 26961
diff changeset
439 print "?", name, "imported from", ', '.join(mods)
26961
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
440
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
441 def any_missing(self):
26963
a573fede88e2 patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents: 26961
diff changeset
442 """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
443 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
444 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
445 """
a573fede88e2 patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents: 26961
diff changeset
446 missing, maybe = self.any_missing_maybe()
a573fede88e2 patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents: 26961
diff changeset
447 return missing + maybe
a573fede88e2 patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents: 26961
diff changeset
448
a573fede88e2 patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents: 26961
diff changeset
449 def any_missing_maybe(self):
a573fede88e2 patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents: 26961
diff changeset
450 """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
451 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
452 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
453
a573fede88e2 patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents: 26961
diff changeset
454 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
455 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
456 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
457 """
26961
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
458 missing = []
26963
a573fede88e2 patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents: 26961
diff changeset
459 maybe = []
a573fede88e2 patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents: 26961
diff changeset
460 for name in self.badmodules:
a573fede88e2 patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents: 26961
diff changeset
461 if name in self.excludes:
a573fede88e2 patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents: 26961
diff changeset
462 continue
a573fede88e2 patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents: 26961
diff changeset
463 i = name.rfind(".")
a573fede88e2 patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents: 26961
diff changeset
464 if i < 0:
a573fede88e2 patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents: 26961
diff changeset
465 missing.append(name)
a573fede88e2 patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents: 26961
diff changeset
466 continue
a573fede88e2 patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents: 26961
diff changeset
467 subname = name[i+1:]
a573fede88e2 patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents: 26961
diff changeset
468 pkgname = name[:i]
a573fede88e2 patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents: 26961
diff changeset
469 pkg = self.modules.get(pkgname)
a573fede88e2 patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents: 26961
diff changeset
470 if pkg is not None:
a573fede88e2 patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents: 26961
diff changeset
471 if pkgname in self.badmodules[name]:
a573fede88e2 patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents: 26961
diff changeset
472 # 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
473 # failed. It's definitely missing.
a573fede88e2 patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents: 26961
diff changeset
474 missing.append(name)
a573fede88e2 patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents: 26961
diff changeset
475 elif subname in pkg.globalnames:
a573fede88e2 patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents: 26961
diff changeset
476 # 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
477 pass
a573fede88e2 patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents: 26961
diff changeset
478 elif pkg.starimports:
a573fede88e2 patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents: 26961
diff changeset
479 # 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
480 # 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
481 maybe.append(name)
a573fede88e2 patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents: 26961
diff changeset
482 else:
a573fede88e2 patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents: 26961
diff changeset
483 # 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
484 # 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
485 # 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
486 # 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
487 # it missing.
a573fede88e2 patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents: 26961
diff changeset
488 missing.append(name)
a573fede88e2 patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents: 26961
diff changeset
489 else:
a573fede88e2 patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents: 26961
diff changeset
490 missing.append(name)
a573fede88e2 patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents: 26961
diff changeset
491 missing.sort()
a573fede88e2 patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents: 26961
diff changeset
492 maybe.sort()
a573fede88e2 patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents: 26961
diff changeset
493 return missing, maybe
26961
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
494
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
495 def replace_paths_in_code(self, co):
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
496 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
497 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
498 if original_filename.startswith(f):
26963
a573fede88e2 patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents: 26961
diff changeset
499 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
500 break
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
501
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
502 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
503 if new_filename != original_filename:
26961
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
504 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
505 % (original_filename,new_filename,))
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
506 else:
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
507 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
508 % (original_filename,))
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
509 self.processed_paths.append(original_filename)
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
510
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
511 consts = list(co.co_consts)
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
512 for i in range(len(consts)):
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
513 if isinstance(consts[i], type(co)):
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
514 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
515
27507
d9d57f5f98cd Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 26963
diff changeset
516 return new.code(co.co_argcount, co.co_nlocals, co.co_stacksize,
d9d57f5f98cd Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 26963
diff changeset
517 co.co_flags, co.co_code, tuple(consts), co.co_names,
d9d57f5f98cd Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 26963
diff changeset
518 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
519 co.co_firstlineno, co.co_lnotab,
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
520 co.co_freevars, co.co_cellvars)
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
521
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
522
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
523 def test():
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
524 # Parse command line
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
525 import getopt
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
526 try:
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
527 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
528 except getopt.error, msg:
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
529 print msg
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
530 return
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
531
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
532 # Process options
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
533 debug = 1
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
534 domods = 0
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
535 addpath = []
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
536 exclude = []
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
537 for o, a in opts:
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
538 if o == '-d':
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
539 debug = debug + 1
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
540 if o == '-m':
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
541 domods = 1
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
542 if o == '-p':
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
543 addpath = addpath + a.split(os.pathsep)
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
544 if o == '-q':
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
545 debug = 0
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
546 if o == '-x':
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
547 exclude.append(a)
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
548
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
549 # Provide default arguments
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
550 if not args:
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
551 script = "hello.py"
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
552 else:
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
553 script = args[0]
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
554
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
555 # 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
556 path = sys.path[:]
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
557 path[0] = os.path.dirname(script)
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
558 path = addpath + path
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
559 if debug > 1:
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
560 print "path:"
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
561 for item in path:
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
562 print " ", `item`
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
563
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
564 # 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
565 mf = ModuleFinder(path, debug, exclude)
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
566 for arg in args[1:]:
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
567 if arg == '-m':
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
568 domods = 1
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
569 continue
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
570 if domods:
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
571 if arg[-2:] == '.*':
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
572 mf.import_hook(arg[:-2], None, ["*"])
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
573 else:
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
574 mf.import_hook(arg)
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
575 else:
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
576 mf.load_file(arg)
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
577 mf.run_script(script)
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
578 mf.report()
26963
a573fede88e2 patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents: 26961
diff changeset
579 return mf # for -i debugging
26961
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
580
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
581
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
582 if __name__ == '__main__':
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
583 try:
26963
a573fede88e2 patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents: 26961
diff changeset
584 mf = test()
26961
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
585 except KeyboardInterrupt:
a5c879726b3a moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff changeset
586 print "\n[interrupt]"