Mercurial > cpython-lru_cache-weakref
annotate Lib/modulefinder.py @ 78069:a8aa77458e12 3.2
Add c_size_t and c_ssize_t to the ctypes summary table.
| author | Antoine Pitrou <solipsis@pitrou.net> |
|---|---|
| date | Thu, 12 Jul 2012 20:31:50 +0200 |
| parents | 6e31b560f0f1 |
| children | 1521d9837d16 |
| 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 |
|
40214
d0bd8a8afb01
Thomas Heller fixed modulefinder and added a test. Thanks!
Guido van Rossum <guido@python.org>
parents:
39575
diff
changeset
|
3 from __future__ import generators |
|
26961
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
4 import dis |
|
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
5 import imp |
|
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
6 import marshal |
|
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
7 import os |
|
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
8 import sys |
|
44036
45ced6ab066d
Merged revisions 59193-59201 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
43777
diff
changeset
|
9 import types |
|
40214
d0bd8a8afb01
Thomas Heller fixed modulefinder and added a test. Thanks!
Guido van Rossum <guido@python.org>
parents:
39575
diff
changeset
|
10 import struct |
|
26961
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
11 |
|
51657
627db59031be
no 2.3 compat in the py3k lib #3676
Benjamin Peterson <benjamin@python.org>
parents:
49633
diff
changeset
|
12 READ_MODE = "rU" |
|
26961
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
13 |
|
43638
b10544827af7
Make str/str8 comparisons return True/False for !=/==.
Brett Cannon <bcannon@gmail.com>
parents:
43604
diff
changeset
|
14 # XXX Clean up once str8's cstor matches bytes. |
|
43777
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43664
diff
changeset
|
15 LOAD_CONST = bytes([dis.opname.index('LOAD_CONST')]) |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43664
diff
changeset
|
16 IMPORT_NAME = bytes([dis.opname.index('IMPORT_NAME')]) |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43664
diff
changeset
|
17 STORE_NAME = bytes([dis.opname.index('STORE_NAME')]) |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43664
diff
changeset
|
18 STORE_GLOBAL = bytes([dis.opname.index('STORE_GLOBAL')]) |
|
26963
a573fede88e2
patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents:
26961
diff
changeset
|
19 STORE_OPS = [STORE_NAME, STORE_GLOBAL] |
|
43777
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43664
diff
changeset
|
20 HAVE_ARGUMENT = bytes([dis.HAVE_ARGUMENT]) |
|
26961
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
21 |
|
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
22 # 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
|
23 # 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
|
24 # 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
|
25 # package, and it will be honored. |
|
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
26 |
|
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
27 # 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
|
28 packagePathMap = {} |
|
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
29 |
|
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
30 # A Public interface |
|
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
31 def AddPackagePath(packagename, path): |
|
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
32 paths = packagePathMap.get(packagename, []) |
|
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
33 paths.append(path) |
|
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
34 packagePathMap[packagename] = paths |
|
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
35 |
|
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
36 replacePackageMap = {} |
|
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
37 |
|
69967
61798f076676
Stop trying to use _xmlplus in the xml module. Closes #11164.
Martin v. Löwis <martin@v.loewis.de>
parents:
51657
diff
changeset
|
38 # This ReplacePackage mechanism allows modulefinder to work around |
|
61798f076676
Stop trying to use _xmlplus in the xml module. Closes #11164.
Martin v. Löwis <martin@v.loewis.de>
parents:
51657
diff
changeset
|
39 # situations in which a package injects itself under the name |
|
61798f076676
Stop trying to use _xmlplus in the xml module. Closes #11164.
Martin v. Löwis <martin@v.loewis.de>
parents:
51657
diff
changeset
|
40 # of another package into sys.modules at runtime by calling |
|
61798f076676
Stop trying to use _xmlplus in the xml module. Closes #11164.
Martin v. Löwis <martin@v.loewis.de>
parents:
51657
diff
changeset
|
41 # ReplacePackage("real_package_name", "faked_package_name") |
|
26961
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
42 # before running ModuleFinder. |
|
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
43 |
|
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
44 def ReplacePackage(oldname, newname): |
|
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
45 replacePackageMap[oldname] = newname |
|
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 |
|
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
48 class Module: |
|
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 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
|
51 self.__name__ = name |
|
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
52 self.__file__ = file |
|
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
53 self.__path__ = path |
|
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
54 self.__code__ = None |
|
26963
a573fede88e2
patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents:
26961
diff
changeset
|
55 # 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
|
56 # 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
|
57 # Python modules. |
|
a573fede88e2
patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents:
26961
diff
changeset
|
58 self.globalnames = {} |
|
a573fede88e2
patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents:
26961
diff
changeset
|
59 # 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
|
60 # 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
|
61 self.starimports = {} |
|
26961
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
62 |
|
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
63 def __repr__(self): |
| 31530 | 64 s = "Module(%r" % (self.__name__,) |
|
26961
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
65 if self.__file__ is not None: |
|
31515
71a294bd7950
Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents:
30921
diff
changeset
|
66 s = s + ", %r" % (self.__file__,) |
|
26961
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
67 if self.__path__ is not None: |
|
31515
71a294bd7950
Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents:
30921
diff
changeset
|
68 s = s + ", %r" % (self.__path__,) |
|
26961
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
69 s = s + ")" |
|
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
70 return s |
|
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
71 |
|
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
72 class ModuleFinder: |
|
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
73 |
|
26963
a573fede88e2
patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents:
26961
diff
changeset
|
74 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
|
75 if path is None: |
|
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
76 path = sys.path |
|
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
77 self.path = path |
|
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
78 self.modules = {} |
|
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
79 self.badmodules = {} |
|
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
80 self.debug = debug |
|
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
81 self.indent = 0 |
|
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
82 self.excludes = excludes |
|
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
83 self.replace_paths = replace_paths |
|
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
84 self.processed_paths = [] # Used in debugging only |
|
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
85 |
|
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
86 def msg(self, level, str, *args): |
|
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
87 if level <= self.debug: |
|
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
88 for i in range(self.indent): |
|
40775
4cdf21a0dbbc
Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents:
40587
diff
changeset
|
89 print(" ", end=' ') |
|
4cdf21a0dbbc
Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents:
40587
diff
changeset
|
90 print(str, end=' ') |
|
26961
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
91 for arg in args: |
|
40775
4cdf21a0dbbc
Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents:
40587
diff
changeset
|
92 print(repr(arg), end=' ') |
|
4cdf21a0dbbc
Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents:
40587
diff
changeset
|
93 print() |
|
26961
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
94 |
|
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
95 def msgin(self, *args): |
|
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
96 level = args[0] |
|
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
97 if level <= self.debug: |
|
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
98 self.indent = self.indent + 1 |
|
28089
14f65d661084
Get rid of many apply() calls.
Guido van Rossum <guido@python.org>
parents:
27583
diff
changeset
|
99 self.msg(*args) |
|
26961
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
100 |
|
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
101 def msgout(self, *args): |
|
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
102 level = args[0] |
|
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
103 if level <= self.debug: |
|
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
104 self.indent = self.indent - 1 |
|
28089
14f65d661084
Get rid of many apply() calls.
Guido van Rossum <guido@python.org>
parents:
27583
diff
changeset
|
105 self.msg(*args) |
|
26961
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
106 |
|
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
107 def run_script(self, pathname): |
|
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
108 self.msg(2, "run_script", pathname) |
|
65750
9b66a9f137b6
Silence ResourceWarnings in modulefinder by using file context managers.
Brett Cannon <bcannon@gmail.com>
parents:
51657
diff
changeset
|
109 with open(pathname, READ_MODE) as fp: |
|
9b66a9f137b6
Silence ResourceWarnings in modulefinder by using file context managers.
Brett Cannon <bcannon@gmail.com>
parents:
51657
diff
changeset
|
110 stuff = ("", "r", imp.PY_SOURCE) |
|
9b66a9f137b6
Silence ResourceWarnings in modulefinder by using file context managers.
Brett Cannon <bcannon@gmail.com>
parents:
51657
diff
changeset
|
111 self.load_module('__main__', fp, pathname, stuff) |
|
26961
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
112 |
|
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
113 def load_file(self, pathname): |
|
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
114 dir, name = os.path.split(pathname) |
|
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
115 name, ext = os.path.splitext(name) |
|
65750
9b66a9f137b6
Silence ResourceWarnings in modulefinder by using file context managers.
Brett Cannon <bcannon@gmail.com>
parents:
51657
diff
changeset
|
116 with open(pathname, READ_MODE) as fp: |
|
9b66a9f137b6
Silence ResourceWarnings in modulefinder by using file context managers.
Brett Cannon <bcannon@gmail.com>
parents:
51657
diff
changeset
|
117 stuff = (ext, "r", imp.PY_SOURCE) |
|
9b66a9f137b6
Silence ResourceWarnings in modulefinder by using file context managers.
Brett Cannon <bcannon@gmail.com>
parents:
51657
diff
changeset
|
118 self.load_module(name, fp, pathname, stuff) |
|
26961
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
119 |
|
40214
d0bd8a8afb01
Thomas Heller fixed modulefinder and added a test. Thanks!
Guido van Rossum <guido@python.org>
parents:
39575
diff
changeset
|
120 def import_hook(self, name, caller=None, fromlist=None, level=-1): |
|
d0bd8a8afb01
Thomas Heller fixed modulefinder and added a test. Thanks!
Guido van Rossum <guido@python.org>
parents:
39575
diff
changeset
|
121 self.msg(3, "import_hook", name, caller, fromlist, level) |
|
d0bd8a8afb01
Thomas Heller fixed modulefinder and added a test. Thanks!
Guido van Rossum <guido@python.org>
parents:
39575
diff
changeset
|
122 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
|
123 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
|
124 m = self.load_tail(q, tail) |
|
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
125 if not fromlist: |
|
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
126 return q |
|
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
127 if m.__path__: |
|
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
128 self.ensure_fromlist(m, fromlist) |
|
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
129 return None |
|
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
130 |
|
40214
d0bd8a8afb01
Thomas Heller fixed modulefinder and added a test. Thanks!
Guido van Rossum <guido@python.org>
parents:
39575
diff
changeset
|
131 def determine_parent(self, caller, level=-1): |
|
d0bd8a8afb01
Thomas Heller fixed modulefinder and added a test. Thanks!
Guido van Rossum <guido@python.org>
parents:
39575
diff
changeset
|
132 self.msgin(4, "determine_parent", caller, level) |
|
d0bd8a8afb01
Thomas Heller fixed modulefinder and added a test. Thanks!
Guido van Rossum <guido@python.org>
parents:
39575
diff
changeset
|
133 if not caller or level == 0: |
|
26961
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
134 self.msgout(4, "determine_parent -> None") |
|
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
135 return None |
|
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
136 pname = caller.__name__ |
|
40214
d0bd8a8afb01
Thomas Heller fixed modulefinder and added a test. Thanks!
Guido van Rossum <guido@python.org>
parents:
39575
diff
changeset
|
137 if level >= 1: # relative import |
|
d0bd8a8afb01
Thomas Heller fixed modulefinder and added a test. Thanks!
Guido van Rossum <guido@python.org>
parents:
39575
diff
changeset
|
138 if caller.__path__: |
|
d0bd8a8afb01
Thomas Heller fixed modulefinder and added a test. Thanks!
Guido van Rossum <guido@python.org>
parents:
39575
diff
changeset
|
139 level -= 1 |
|
d0bd8a8afb01
Thomas Heller fixed modulefinder and added a test. Thanks!
Guido van Rossum <guido@python.org>
parents:
39575
diff
changeset
|
140 if level == 0: |
|
d0bd8a8afb01
Thomas Heller fixed modulefinder and added a test. Thanks!
Guido van Rossum <guido@python.org>
parents:
39575
diff
changeset
|
141 parent = self.modules[pname] |
|
d0bd8a8afb01
Thomas Heller fixed modulefinder and added a test. Thanks!
Guido van Rossum <guido@python.org>
parents:
39575
diff
changeset
|
142 assert parent is caller |
|
d0bd8a8afb01
Thomas Heller fixed modulefinder and added a test. Thanks!
Guido van Rossum <guido@python.org>
parents:
39575
diff
changeset
|
143 self.msgout(4, "determine_parent ->", parent) |
|
d0bd8a8afb01
Thomas Heller fixed modulefinder and added a test. Thanks!
Guido van Rossum <guido@python.org>
parents:
39575
diff
changeset
|
144 return parent |
|
d0bd8a8afb01
Thomas Heller fixed modulefinder and added a test. Thanks!
Guido van Rossum <guido@python.org>
parents:
39575
diff
changeset
|
145 if pname.count(".") < level: |
|
43020
907a45df1295
Raise statement normalization in Lib/.
Collin Winter <collinw@gmail.com>
parents:
42052
diff
changeset
|
146 raise ImportError("relative importpath too deep") |
|
40214
d0bd8a8afb01
Thomas Heller fixed modulefinder and added a test. Thanks!
Guido van Rossum <guido@python.org>
parents:
39575
diff
changeset
|
147 pname = ".".join(pname.split(".")[:-level]) |
|
d0bd8a8afb01
Thomas Heller fixed modulefinder and added a test. Thanks!
Guido van Rossum <guido@python.org>
parents:
39575
diff
changeset
|
148 parent = self.modules[pname] |
|
d0bd8a8afb01
Thomas Heller fixed modulefinder and added a test. Thanks!
Guido van Rossum <guido@python.org>
parents:
39575
diff
changeset
|
149 self.msgout(4, "determine_parent ->", parent) |
|
d0bd8a8afb01
Thomas Heller fixed modulefinder and added a test. Thanks!
Guido van Rossum <guido@python.org>
parents:
39575
diff
changeset
|
150 return parent |
|
26961
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
151 if caller.__path__: |
|
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
152 parent = self.modules[pname] |
|
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
153 assert caller is parent |
|
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
154 self.msgout(4, "determine_parent ->", parent) |
|
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
155 return parent |
|
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
156 if '.' in pname: |
|
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
157 i = pname.rfind('.') |
|
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
158 pname = pname[:i] |
|
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
159 parent = self.modules[pname] |
|
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
160 assert parent.__name__ == pname |
|
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
161 self.msgout(4, "determine_parent ->", parent) |
|
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
162 return parent |
|
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
163 self.msgout(4, "determine_parent -> None") |
|
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
164 return None |
|
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
165 |
|
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
166 def find_head_package(self, parent, name): |
|
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
167 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
|
168 if '.' in name: |
|
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
169 i = name.find('.') |
|
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
170 head = name[:i] |
|
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
171 tail = name[i+1:] |
|
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
172 else: |
|
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
173 head = name |
|
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
174 tail = "" |
|
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
175 if parent: |
|
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
176 qname = "%s.%s" % (parent.__name__, head) |
|
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
177 else: |
|
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
178 qname = head |
|
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
179 q = self.import_module(head, qname, parent) |
|
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
180 if q: |
|
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
181 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
|
182 return q, tail |
|
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
183 if parent: |
|
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
184 qname = head |
|
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
185 parent = None |
|
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
186 q = self.import_module(head, qname, parent) |
|
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
187 if q: |
|
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
188 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
|
189 return q, tail |
|
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", qname) |
|
43020
907a45df1295
Raise statement normalization in Lib/.
Collin Winter <collinw@gmail.com>
parents:
42052
diff
changeset
|
191 raise ImportError("No module named " + qname) |
|
26961
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
192 |
|
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
193 def load_tail(self, q, tail): |
|
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
194 self.msgin(4, "load_tail", q, tail) |
|
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
195 m = q |
|
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
196 while tail: |
|
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
197 i = tail.find('.') |
|
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
198 if i < 0: i = len(tail) |
|
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
199 head, tail = tail[:i], tail[i+1:] |
|
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
200 mname = "%s.%s" % (m.__name__, head) |
|
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
201 m = self.import_module(head, mname, m) |
|
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
202 if not m: |
|
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
203 self.msgout(4, "raise ImportError: No module named", mname) |
|
43020
907a45df1295
Raise statement normalization in Lib/.
Collin Winter <collinw@gmail.com>
parents:
42052
diff
changeset
|
204 raise ImportError("No module named " + mname) |
|
26961
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
205 self.msgout(4, "load_tail ->", m) |
|
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
206 return m |
|
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
207 |
|
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
208 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
|
209 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
|
210 for sub in fromlist: |
|
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
211 if sub == "*": |
|
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
212 if not recursive: |
|
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
213 all = self.find_all_submodules(m) |
|
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
214 if all: |
|
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
215 self.ensure_fromlist(m, all, 1) |
|
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
216 elif not hasattr(m, sub): |
|
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
217 subname = "%s.%s" % (m.__name__, sub) |
|
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
218 submod = self.import_module(sub, subname, m) |
|
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
219 if not submod: |
|
43020
907a45df1295
Raise statement normalization in Lib/.
Collin Winter <collinw@gmail.com>
parents:
42052
diff
changeset
|
220 raise ImportError("No module named " + subname) |
|
26961
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
221 |
|
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
222 def find_all_submodules(self, m): |
|
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
223 if not m.__path__: |
|
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
224 return |
|
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
225 modules = {} |
|
30921
0468f9ddc069
SF #841977 - modulefinder fails to find extension modules in packages
Thomas Heller <theller@ctypes.org>
parents:
29941
diff
changeset
|
226 # '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
|
227 # 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
|
228 # 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
|
229 suffixes = [] |
|
0468f9ddc069
SF #841977 - modulefinder fails to find extension modules in packages
Thomas Heller <theller@ctypes.org>
parents:
29941
diff
changeset
|
230 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
|
231 suffixes.append(triple[0]) |
|
26961
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
232 for dir in m.__path__: |
|
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 names = os.listdir(dir) |
|
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
235 except os.error: |
|
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
236 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
|
237 continue |
|
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
238 for name in names: |
|
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
239 mod = None |
|
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
240 for suff in suffixes: |
|
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
241 n = len(suff) |
|
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
242 if name[-n:] == suff: |
|
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
243 mod = name[:-n] |
|
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
244 break |
|
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
245 if mod and mod != "__init__": |
|
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
246 modules[mod] = mod |
|
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
247 return modules.keys() |
|
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
248 |
|
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
249 def import_module(self, partname, fqname, parent): |
|
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
250 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
|
251 try: |
|
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
252 m = self.modules[fqname] |
|
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
253 except KeyError: |
|
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
254 pass |
|
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
255 else: |
|
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
256 self.msgout(3, "import_module ->", m) |
|
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
257 return m |
|
39575
63237fcefd99
Get rid of dict.has_key(). Boy this has a lot of repercussions!
Guido van Rossum <guido@python.org>
parents:
32056
diff
changeset
|
258 if fqname in self.badmodules: |
|
26961
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
259 self.msgout(3, "import_module -> None") |
|
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
260 return None |
|
32056
1538a8cfb0dd
Fix SF item #876278: Unbounded recursion in modulefinder.
Thomas Heller <theller@ctypes.org>
parents:
31530
diff
changeset
|
261 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
|
262 self.msgout(3, "import_module -> None") |
|
1538a8cfb0dd
Fix SF item #876278: Unbounded recursion in modulefinder.
Thomas Heller <theller@ctypes.org>
parents:
31530
diff
changeset
|
263 return None |
|
26961
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
264 try: |
|
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
265 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
|
266 parent and parent.__path__, parent) |
|
26961
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
267 except ImportError: |
|
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
268 self.msgout(3, "import_module ->", None) |
|
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
269 return None |
|
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
270 try: |
|
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
271 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
|
272 finally: |
|
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
273 if fp: fp.close() |
|
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
274 if parent: |
|
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
275 setattr(parent, partname, m) |
|
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
276 self.msgout(3, "import_module ->", m) |
|
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
277 return m |
|
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
278 |
|
41717
9ad84070b22d
Merged revisions 55328-55341 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40775
diff
changeset
|
279 def load_module(self, fqname, fp, pathname, file_info): |
|
9ad84070b22d
Merged revisions 55328-55341 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40775
diff
changeset
|
280 suffix, mode, type = file_info |
|
26961
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
281 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
|
282 if type == imp.PKG_DIRECTORY: |
|
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
283 m = self.load_package(fqname, pathname) |
|
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
284 self.msgout(2, "load_module ->", m) |
|
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
285 return m |
|
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
286 if type == imp.PY_SOURCE: |
|
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
287 co = compile(fp.read()+'\n', pathname, 'exec') |
|
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
288 elif type == imp.PY_COMPILED: |
|
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
289 if fp.read(4) != imp.get_magic(): |
|
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
290 self.msgout(2, "raise ImportError: Bad magic number", pathname) |
|
43020
907a45df1295
Raise statement normalization in Lib/.
Collin Winter <collinw@gmail.com>
parents:
42052
diff
changeset
|
291 raise ImportError("Bad magic number in %s" % pathname) |
|
26961
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
292 fp.read(4) |
|
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
293 co = marshal.load(fp) |
|
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
294 else: |
|
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
295 co = None |
|
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
296 m = self.add_module(fqname) |
|
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
297 m.__file__ = pathname |
|
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
298 if co: |
|
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
299 if self.replace_paths: |
|
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
300 co = self.replace_paths_in_code(co) |
|
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
301 m.__code__ = co |
|
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
302 self.scan_code(co, m) |
|
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
303 self.msgout(2, "load_module ->", m) |
|
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
304 return m |
|
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
305 |
|
26963
a573fede88e2
patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents:
26961
diff
changeset
|
306 def _add_badmodule(self, name, caller): |
|
a573fede88e2
patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents:
26961
diff
changeset
|
307 if name not in self.badmodules: |
|
a573fede88e2
patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents:
26961
diff
changeset
|
308 self.badmodules[name] = {} |
|
49633
bd0e9a9cd283
Merged revisions 67028,67040,67044,67046,67052,67065,67070,67077,67082 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
44036
diff
changeset
|
309 if caller: |
|
bd0e9a9cd283
Merged revisions 67028,67040,67044,67046,67052,67065,67070,67077,67082 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
44036
diff
changeset
|
310 self.badmodules[name][caller.__name__] = 1 |
|
bd0e9a9cd283
Merged revisions 67028,67040,67044,67046,67052,67065,67070,67077,67082 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
44036
diff
changeset
|
311 else: |
|
bd0e9a9cd283
Merged revisions 67028,67040,67044,67046,67052,67065,67070,67077,67082 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
44036
diff
changeset
|
312 self.badmodules[name]["-"] = 1 |
|
26963
a573fede88e2
patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents:
26961
diff
changeset
|
313 |
|
40214
d0bd8a8afb01
Thomas Heller fixed modulefinder and added a test. Thanks!
Guido van Rossum <guido@python.org>
parents:
39575
diff
changeset
|
314 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
|
315 # 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
|
316 if name in self.badmodules: |
|
a573fede88e2
patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents:
26961
diff
changeset
|
317 self._add_badmodule(name, caller) |
|
a573fede88e2
patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents:
26961
diff
changeset
|
318 return |
|
a573fede88e2
patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents:
26961
diff
changeset
|
319 try: |
|
40214
d0bd8a8afb01
Thomas Heller fixed modulefinder and added a test. Thanks!
Guido van Rossum <guido@python.org>
parents:
39575
diff
changeset
|
320 self.import_hook(name, caller, level=level) |
|
40587
6b18a095c2e7
SF patch 1631942 by Collin Winter:
Guido van Rossum <guido@python.org>
parents:
40214
diff
changeset
|
321 except ImportError as msg: |
|
26963
a573fede88e2
patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents:
26961
diff
changeset
|
322 self.msg(2, "ImportError:", str(msg)) |
|
a573fede88e2
patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents:
26961
diff
changeset
|
323 self._add_badmodule(name, caller) |
|
a573fede88e2
patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents:
26961
diff
changeset
|
324 else: |
|
a573fede88e2
patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents:
26961
diff
changeset
|
325 if fromlist: |
|
a573fede88e2
patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents:
26961
diff
changeset
|
326 for sub in fromlist: |
|
a573fede88e2
patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents:
26961
diff
changeset
|
327 if sub in self.badmodules: |
|
a573fede88e2
patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents:
26961
diff
changeset
|
328 self._add_badmodule(sub, caller) |
|
a573fede88e2
patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents:
26961
diff
changeset
|
329 continue |
|
a573fede88e2
patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents:
26961
diff
changeset
|
330 try: |
|
40214
d0bd8a8afb01
Thomas Heller fixed modulefinder and added a test. Thanks!
Guido van Rossum <guido@python.org>
parents:
39575
diff
changeset
|
331 self.import_hook(name, caller, [sub], level=level) |
|
40587
6b18a095c2e7
SF patch 1631942 by Collin Winter:
Guido van Rossum <guido@python.org>
parents:
40214
diff
changeset
|
332 except ImportError as msg: |
|
26963
a573fede88e2
patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents:
26961
diff
changeset
|
333 self.msg(2, "ImportError:", str(msg)) |
|
a573fede88e2
patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents:
26961
diff
changeset
|
334 fullname = name + "." + sub |
|
a573fede88e2
patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents:
26961
diff
changeset
|
335 self._add_badmodule(fullname, caller) |
|
a573fede88e2
patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents:
26961
diff
changeset
|
336 |
|
40214
d0bd8a8afb01
Thomas Heller fixed modulefinder and added a test. Thanks!
Guido van Rossum <guido@python.org>
parents:
39575
diff
changeset
|
337 def scan_opcodes(self, co, |
|
d0bd8a8afb01
Thomas Heller fixed modulefinder and added a test. Thanks!
Guido van Rossum <guido@python.org>
parents:
39575
diff
changeset
|
338 unpack = struct.unpack): |
|
d0bd8a8afb01
Thomas Heller fixed modulefinder and added a test. Thanks!
Guido van Rossum <guido@python.org>
parents:
39575
diff
changeset
|
339 # Scan the code, and yield 'interesting' opcode combinations |
|
d0bd8a8afb01
Thomas Heller fixed modulefinder and added a test. Thanks!
Guido van Rossum <guido@python.org>
parents:
39575
diff
changeset
|
340 # Version for Python 2.4 and older |
|
d0bd8a8afb01
Thomas Heller fixed modulefinder and added a test. Thanks!
Guido van Rossum <guido@python.org>
parents:
39575
diff
changeset
|
341 code = co.co_code |
|
d0bd8a8afb01
Thomas Heller fixed modulefinder and added a test. Thanks!
Guido van Rossum <guido@python.org>
parents:
39575
diff
changeset
|
342 names = co.co_names |
|
d0bd8a8afb01
Thomas Heller fixed modulefinder and added a test. Thanks!
Guido van Rossum <guido@python.org>
parents:
39575
diff
changeset
|
343 consts = co.co_consts |
|
d0bd8a8afb01
Thomas Heller fixed modulefinder and added a test. Thanks!
Guido van Rossum <guido@python.org>
parents:
39575
diff
changeset
|
344 while code: |
|
d0bd8a8afb01
Thomas Heller fixed modulefinder and added a test. Thanks!
Guido van Rossum <guido@python.org>
parents:
39575
diff
changeset
|
345 c = code[0] |
|
d0bd8a8afb01
Thomas Heller fixed modulefinder and added a test. Thanks!
Guido van Rossum <guido@python.org>
parents:
39575
diff
changeset
|
346 if c in STORE_OPS: |
|
d0bd8a8afb01
Thomas Heller fixed modulefinder and added a test. Thanks!
Guido van Rossum <guido@python.org>
parents:
39575
diff
changeset
|
347 oparg, = unpack('<H', code[1:3]) |
|
d0bd8a8afb01
Thomas Heller fixed modulefinder and added a test. Thanks!
Guido van Rossum <guido@python.org>
parents:
39575
diff
changeset
|
348 yield "store", (names[oparg],) |
|
d0bd8a8afb01
Thomas Heller fixed modulefinder and added a test. Thanks!
Guido van Rossum <guido@python.org>
parents:
39575
diff
changeset
|
349 code = code[3:] |
|
d0bd8a8afb01
Thomas Heller fixed modulefinder and added a test. Thanks!
Guido van Rossum <guido@python.org>
parents:
39575
diff
changeset
|
350 continue |
|
d0bd8a8afb01
Thomas Heller fixed modulefinder and added a test. Thanks!
Guido van Rossum <guido@python.org>
parents:
39575
diff
changeset
|
351 if c == LOAD_CONST and code[3] == IMPORT_NAME: |
|
d0bd8a8afb01
Thomas Heller fixed modulefinder and added a test. Thanks!
Guido van Rossum <guido@python.org>
parents:
39575
diff
changeset
|
352 oparg_1, oparg_2 = unpack('<xHxH', code[:6]) |
|
d0bd8a8afb01
Thomas Heller fixed modulefinder and added a test. Thanks!
Guido van Rossum <guido@python.org>
parents:
39575
diff
changeset
|
353 yield "import", (consts[oparg_1], names[oparg_2]) |
|
d0bd8a8afb01
Thomas Heller fixed modulefinder and added a test. Thanks!
Guido van Rossum <guido@python.org>
parents:
39575
diff
changeset
|
354 code = code[6:] |
|
d0bd8a8afb01
Thomas Heller fixed modulefinder and added a test. Thanks!
Guido van Rossum <guido@python.org>
parents:
39575
diff
changeset
|
355 continue |
|
d0bd8a8afb01
Thomas Heller fixed modulefinder and added a test. Thanks!
Guido van Rossum <guido@python.org>
parents:
39575
diff
changeset
|
356 if c >= HAVE_ARGUMENT: |
|
d0bd8a8afb01
Thomas Heller fixed modulefinder and added a test. Thanks!
Guido van Rossum <guido@python.org>
parents:
39575
diff
changeset
|
357 code = code[3:] |
|
d0bd8a8afb01
Thomas Heller fixed modulefinder and added a test. Thanks!
Guido van Rossum <guido@python.org>
parents:
39575
diff
changeset
|
358 else: |
|
d0bd8a8afb01
Thomas Heller fixed modulefinder and added a test. Thanks!
Guido van Rossum <guido@python.org>
parents:
39575
diff
changeset
|
359 code = code[1:] |
|
d0bd8a8afb01
Thomas Heller fixed modulefinder and added a test. Thanks!
Guido van Rossum <guido@python.org>
parents:
39575
diff
changeset
|
360 |
|
d0bd8a8afb01
Thomas Heller fixed modulefinder and added a test. Thanks!
Guido van Rossum <guido@python.org>
parents:
39575
diff
changeset
|
361 def scan_opcodes_25(self, co, |
|
d0bd8a8afb01
Thomas Heller fixed modulefinder and added a test. Thanks!
Guido van Rossum <guido@python.org>
parents:
39575
diff
changeset
|
362 unpack = struct.unpack): |
|
d0bd8a8afb01
Thomas Heller fixed modulefinder and added a test. Thanks!
Guido van Rossum <guido@python.org>
parents:
39575
diff
changeset
|
363 # Scan the code, and yield 'interesting' opcode combinations |
|
d0bd8a8afb01
Thomas Heller fixed modulefinder and added a test. Thanks!
Guido van Rossum <guido@python.org>
parents:
39575
diff
changeset
|
364 # Python 2.5 version (has absolute and relative imports) |
|
d0bd8a8afb01
Thomas Heller fixed modulefinder and added a test. Thanks!
Guido van Rossum <guido@python.org>
parents:
39575
diff
changeset
|
365 code = co.co_code |
|
d0bd8a8afb01
Thomas Heller fixed modulefinder and added a test. Thanks!
Guido van Rossum <guido@python.org>
parents:
39575
diff
changeset
|
366 names = co.co_names |
|
d0bd8a8afb01
Thomas Heller fixed modulefinder and added a test. Thanks!
Guido van Rossum <guido@python.org>
parents:
39575
diff
changeset
|
367 consts = co.co_consts |
|
d0bd8a8afb01
Thomas Heller fixed modulefinder and added a test. Thanks!
Guido van Rossum <guido@python.org>
parents:
39575
diff
changeset
|
368 LOAD_LOAD_AND_IMPORT = LOAD_CONST + LOAD_CONST + IMPORT_NAME |
|
d0bd8a8afb01
Thomas Heller fixed modulefinder and added a test. Thanks!
Guido van Rossum <guido@python.org>
parents:
39575
diff
changeset
|
369 while code: |
|
43777
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43664
diff
changeset
|
370 c = bytes([code[0]]) |
|
40214
d0bd8a8afb01
Thomas Heller fixed modulefinder and added a test. Thanks!
Guido van Rossum <guido@python.org>
parents:
39575
diff
changeset
|
371 if c in STORE_OPS: |
|
d0bd8a8afb01
Thomas Heller fixed modulefinder and added a test. Thanks!
Guido van Rossum <guido@python.org>
parents:
39575
diff
changeset
|
372 oparg, = unpack('<H', code[1:3]) |
|
d0bd8a8afb01
Thomas Heller fixed modulefinder and added a test. Thanks!
Guido van Rossum <guido@python.org>
parents:
39575
diff
changeset
|
373 yield "store", (names[oparg],) |
|
d0bd8a8afb01
Thomas Heller fixed modulefinder and added a test. Thanks!
Guido van Rossum <guido@python.org>
parents:
39575
diff
changeset
|
374 code = code[3:] |
|
d0bd8a8afb01
Thomas Heller fixed modulefinder and added a test. Thanks!
Guido van Rossum <guido@python.org>
parents:
39575
diff
changeset
|
375 continue |
|
d0bd8a8afb01
Thomas Heller fixed modulefinder and added a test. Thanks!
Guido van Rossum <guido@python.org>
parents:
39575
diff
changeset
|
376 if code[:9:3] == LOAD_LOAD_AND_IMPORT: |
|
d0bd8a8afb01
Thomas Heller fixed modulefinder and added a test. Thanks!
Guido van Rossum <guido@python.org>
parents:
39575
diff
changeset
|
377 oparg_1, oparg_2, oparg_3 = unpack('<xHxHxH', code[:9]) |
|
d0bd8a8afb01
Thomas Heller fixed modulefinder and added a test. Thanks!
Guido van Rossum <guido@python.org>
parents:
39575
diff
changeset
|
378 level = consts[oparg_1] |
|
d0bd8a8afb01
Thomas Heller fixed modulefinder and added a test. Thanks!
Guido van Rossum <guido@python.org>
parents:
39575
diff
changeset
|
379 if level == 0: # absolute import |
|
d0bd8a8afb01
Thomas Heller fixed modulefinder and added a test. Thanks!
Guido van Rossum <guido@python.org>
parents:
39575
diff
changeset
|
380 yield "absolute_import", (consts[oparg_2], names[oparg_3]) |
|
d0bd8a8afb01
Thomas Heller fixed modulefinder and added a test. Thanks!
Guido van Rossum <guido@python.org>
parents:
39575
diff
changeset
|
381 else: # relative import |
|
d0bd8a8afb01
Thomas Heller fixed modulefinder and added a test. Thanks!
Guido van Rossum <guido@python.org>
parents:
39575
diff
changeset
|
382 yield "relative_import", (level, consts[oparg_2], names[oparg_3]) |
|
d0bd8a8afb01
Thomas Heller fixed modulefinder and added a test. Thanks!
Guido van Rossum <guido@python.org>
parents:
39575
diff
changeset
|
383 code = code[9:] |
|
d0bd8a8afb01
Thomas Heller fixed modulefinder and added a test. Thanks!
Guido van Rossum <guido@python.org>
parents:
39575
diff
changeset
|
384 continue |
|
d0bd8a8afb01
Thomas Heller fixed modulefinder and added a test. Thanks!
Guido van Rossum <guido@python.org>
parents:
39575
diff
changeset
|
385 if c >= HAVE_ARGUMENT: |
|
d0bd8a8afb01
Thomas Heller fixed modulefinder and added a test. Thanks!
Guido van Rossum <guido@python.org>
parents:
39575
diff
changeset
|
386 code = code[3:] |
|
d0bd8a8afb01
Thomas Heller fixed modulefinder and added a test. Thanks!
Guido van Rossum <guido@python.org>
parents:
39575
diff
changeset
|
387 else: |
|
d0bd8a8afb01
Thomas Heller fixed modulefinder and added a test. Thanks!
Guido van Rossum <guido@python.org>
parents:
39575
diff
changeset
|
388 code = code[1:] |
|
d0bd8a8afb01
Thomas Heller fixed modulefinder and added a test. Thanks!
Guido van Rossum <guido@python.org>
parents:
39575
diff
changeset
|
389 |
|
26961
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
390 def scan_code(self, co, m): |
|
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
391 code = co.co_code |
|
40214
d0bd8a8afb01
Thomas Heller fixed modulefinder and added a test. Thanks!
Guido van Rossum <guido@python.org>
parents:
39575
diff
changeset
|
392 if sys.version_info >= (2, 5): |
|
d0bd8a8afb01
Thomas Heller fixed modulefinder and added a test. Thanks!
Guido van Rossum <guido@python.org>
parents:
39575
diff
changeset
|
393 scanner = self.scan_opcodes_25 |
|
d0bd8a8afb01
Thomas Heller fixed modulefinder and added a test. Thanks!
Guido van Rossum <guido@python.org>
parents:
39575
diff
changeset
|
394 else: |
|
d0bd8a8afb01
Thomas Heller fixed modulefinder and added a test. Thanks!
Guido van Rossum <guido@python.org>
parents:
39575
diff
changeset
|
395 scanner = self.scan_opcodes |
|
d0bd8a8afb01
Thomas Heller fixed modulefinder and added a test. Thanks!
Guido van Rossum <guido@python.org>
parents:
39575
diff
changeset
|
396 for what, args in scanner(co): |
|
d0bd8a8afb01
Thomas Heller fixed modulefinder and added a test. Thanks!
Guido van Rossum <guido@python.org>
parents:
39575
diff
changeset
|
397 if what == "store": |
|
d0bd8a8afb01
Thomas Heller fixed modulefinder and added a test. Thanks!
Guido van Rossum <guido@python.org>
parents:
39575
diff
changeset
|
398 name, = args |
|
d0bd8a8afb01
Thomas Heller fixed modulefinder and added a test. Thanks!
Guido van Rossum <guido@python.org>
parents:
39575
diff
changeset
|
399 m.globalnames[name] = 1 |
|
d0bd8a8afb01
Thomas Heller fixed modulefinder and added a test. Thanks!
Guido van Rossum <guido@python.org>
parents:
39575
diff
changeset
|
400 elif what == "absolute_import": |
|
d0bd8a8afb01
Thomas Heller fixed modulefinder and added a test. Thanks!
Guido van Rossum <guido@python.org>
parents:
39575
diff
changeset
|
401 fromlist, name = args |
|
26963
a573fede88e2
patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents:
26961
diff
changeset
|
402 have_star = 0 |
|
a573fede88e2
patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents:
26961
diff
changeset
|
403 if fromlist is not None: |
|
a573fede88e2
patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents:
26961
diff
changeset
|
404 if "*" in fromlist: |
|
a573fede88e2
patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents:
26961
diff
changeset
|
405 have_star = 1 |
|
a573fede88e2
patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents:
26961
diff
changeset
|
406 fromlist = [f for f in fromlist if f != "*"] |
|
40214
d0bd8a8afb01
Thomas Heller fixed modulefinder and added a test. Thanks!
Guido van Rossum <guido@python.org>
parents:
39575
diff
changeset
|
407 self._safe_import_hook(name, m, fromlist, level=0) |
|
26963
a573fede88e2
patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents:
26961
diff
changeset
|
408 if have_star: |
|
a573fede88e2
patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents:
26961
diff
changeset
|
409 # 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
|
410 # 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
|
411 # global names. |
|
a573fede88e2
patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents:
26961
diff
changeset
|
412 mm = None |
|
a573fede88e2
patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents:
26961
diff
changeset
|
413 if m.__path__: |
|
a573fede88e2
patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents:
26961
diff
changeset
|
414 # 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
|
415 # 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
|
416 # the full name first. |
|
a573fede88e2
patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents:
26961
diff
changeset
|
417 mm = self.modules.get(m.__name__ + "." + name) |
|
a573fede88e2
patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents:
26961
diff
changeset
|
418 if mm is None: |
|
a573fede88e2
patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents:
26961
diff
changeset
|
419 mm = self.modules.get(name) |
|
a573fede88e2
patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents:
26961
diff
changeset
|
420 if mm is not None: |
|
a573fede88e2
patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents:
26961
diff
changeset
|
421 m.globalnames.update(mm.globalnames) |
|
a573fede88e2
patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents:
26961
diff
changeset
|
422 m.starimports.update(mm.starimports) |
|
a573fede88e2
patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents:
26961
diff
changeset
|
423 if mm.__code__ is None: |
|
a573fede88e2
patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents:
26961
diff
changeset
|
424 m.starimports[name] = 1 |
|
a573fede88e2
patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents:
26961
diff
changeset
|
425 else: |
|
a573fede88e2
patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents:
26961
diff
changeset
|
426 m.starimports[name] = 1 |
|
40214
d0bd8a8afb01
Thomas Heller fixed modulefinder and added a test. Thanks!
Guido van Rossum <guido@python.org>
parents:
39575
diff
changeset
|
427 elif what == "relative_import": |
|
d0bd8a8afb01
Thomas Heller fixed modulefinder and added a test. Thanks!
Guido van Rossum <guido@python.org>
parents:
39575
diff
changeset
|
428 level, fromlist, name = args |
|
d0bd8a8afb01
Thomas Heller fixed modulefinder and added a test. Thanks!
Guido van Rossum <guido@python.org>
parents:
39575
diff
changeset
|
429 if name: |
|
d0bd8a8afb01
Thomas Heller fixed modulefinder and added a test. Thanks!
Guido van Rossum <guido@python.org>
parents:
39575
diff
changeset
|
430 self._safe_import_hook(name, m, fromlist, level=level) |
|
d0bd8a8afb01
Thomas Heller fixed modulefinder and added a test. Thanks!
Guido van Rossum <guido@python.org>
parents:
39575
diff
changeset
|
431 else: |
|
d0bd8a8afb01
Thomas Heller fixed modulefinder and added a test. Thanks!
Guido van Rossum <guido@python.org>
parents:
39575
diff
changeset
|
432 parent = self.determine_parent(m, level=level) |
|
d0bd8a8afb01
Thomas Heller fixed modulefinder and added a test. Thanks!
Guido van Rossum <guido@python.org>
parents:
39575
diff
changeset
|
433 self._safe_import_hook(parent.__name__, None, fromlist, level=0) |
|
d0bd8a8afb01
Thomas Heller fixed modulefinder and added a test. Thanks!
Guido van Rossum <guido@python.org>
parents:
39575
diff
changeset
|
434 else: |
|
d0bd8a8afb01
Thomas Heller fixed modulefinder and added a test. Thanks!
Guido van Rossum <guido@python.org>
parents:
39575
diff
changeset
|
435 # We don't expect anything else from the generator. |
|
d0bd8a8afb01
Thomas Heller fixed modulefinder and added a test. Thanks!
Guido van Rossum <guido@python.org>
parents:
39575
diff
changeset
|
436 raise RuntimeError(what) |
|
d0bd8a8afb01
Thomas Heller fixed modulefinder and added a test. Thanks!
Guido van Rossum <guido@python.org>
parents:
39575
diff
changeset
|
437 |
|
26961
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
438 for c in co.co_consts: |
|
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
439 if isinstance(c, type(co)): |
|
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
440 self.scan_code(c, m) |
|
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
441 |
|
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
442 def load_package(self, fqname, pathname): |
|
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
443 self.msgin(2, "load_package", fqname, pathname) |
|
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
444 newname = replacePackageMap.get(fqname) |
|
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
445 if newname: |
|
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
446 fqname = newname |
|
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
447 m = self.add_module(fqname) |
|
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
448 m.__file__ = pathname |
|
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
449 m.__path__ = [pathname] |
|
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
450 |
|
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
451 # 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
|
452 m.__path__ = m.__path__ + packagePathMap.get(fqname, []) |
|
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
453 |
|
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
454 fp, buf, stuff = self.find_module("__init__", m.__path__) |
|
65750
9b66a9f137b6
Silence ResourceWarnings in modulefinder by using file context managers.
Brett Cannon <bcannon@gmail.com>
parents:
51657
diff
changeset
|
455 try: |
|
9b66a9f137b6
Silence ResourceWarnings in modulefinder by using file context managers.
Brett Cannon <bcannon@gmail.com>
parents:
51657
diff
changeset
|
456 self.load_module(fqname, fp, buf, stuff) |
|
9b66a9f137b6
Silence ResourceWarnings in modulefinder by using file context managers.
Brett Cannon <bcannon@gmail.com>
parents:
51657
diff
changeset
|
457 self.msgout(2, "load_package ->", m) |
|
9b66a9f137b6
Silence ResourceWarnings in modulefinder by using file context managers.
Brett Cannon <bcannon@gmail.com>
parents:
51657
diff
changeset
|
458 return m |
|
9b66a9f137b6
Silence ResourceWarnings in modulefinder by using file context managers.
Brett Cannon <bcannon@gmail.com>
parents:
51657
diff
changeset
|
459 finally: |
|
9b66a9f137b6
Silence ResourceWarnings in modulefinder by using file context managers.
Brett Cannon <bcannon@gmail.com>
parents:
51657
diff
changeset
|
460 if fp: |
|
9b66a9f137b6
Silence ResourceWarnings in modulefinder by using file context managers.
Brett Cannon <bcannon@gmail.com>
parents:
51657
diff
changeset
|
461 fp.close() |
|
26961
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
462 |
|
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
463 def add_module(self, fqname): |
|
39575
63237fcefd99
Get rid of dict.has_key(). Boy this has a lot of repercussions!
Guido van Rossum <guido@python.org>
parents:
32056
diff
changeset
|
464 if fqname in self.modules: |
|
26961
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
465 return self.modules[fqname] |
|
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
466 self.modules[fqname] = m = Module(fqname) |
|
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
467 return m |
|
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
468 |
|
28178
0a8e8632c631
Patch #698082 from Thomas Heller: Modulefinder didn't exclude modules
Just van Rossum <just@letterror.com>
parents:
28089
diff
changeset
|
469 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
|
470 if parent is not None: |
|
32056
1538a8cfb0dd
Fix SF item #876278: Unbounded recursion in modulefinder.
Thomas Heller <theller@ctypes.org>
parents:
31530
diff
changeset
|
471 # 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
|
472 fullname = parent.__name__+'.'+name |
|
26961
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
473 else: |
|
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
474 fullname = name |
|
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
475 if fullname in self.excludes: |
|
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
476 self.msgout(3, "find_module -> Excluded", fullname) |
|
43020
907a45df1295
Raise statement normalization in Lib/.
Collin Winter <collinw@gmail.com>
parents:
42052
diff
changeset
|
477 raise ImportError(name) |
|
26961
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
478 |
|
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
479 if path is None: |
|
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
480 if name in sys.builtin_module_names: |
|
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
481 return (None, None, ("", "", imp.C_BUILTIN)) |
|
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
482 |
|
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
483 path = self.path |
|
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
484 return imp.find_module(name, path) |
|
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 def report(self): |
|
26963
a573fede88e2
patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents:
26961
diff
changeset
|
487 """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
|
488 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
|
489 """ |
|
40775
4cdf21a0dbbc
Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents:
40587
diff
changeset
|
490 print() |
|
4cdf21a0dbbc
Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents:
40587
diff
changeset
|
491 print(" %-25s %s" % ("Name", "File")) |
|
4cdf21a0dbbc
Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents:
40587
diff
changeset
|
492 print(" %-25s %s" % ("----", "----")) |
|
26961
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
493 # Print modules found |
|
42052
881ab1d417eb
Some quick fixes of code that was sorting dict.keys() etc.
Guido van Rossum <guido@python.org>
parents:
41717
diff
changeset
|
494 keys = sorted(self.modules.keys()) |
|
26961
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
495 for key in keys: |
|
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
496 m = self.modules[key] |
|
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
497 if m.__path__: |
|
40775
4cdf21a0dbbc
Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents:
40587
diff
changeset
|
498 print("P", end=' ') |
|
26961
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
499 else: |
|
40775
4cdf21a0dbbc
Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents:
40587
diff
changeset
|
500 print("m", end=' ') |
|
4cdf21a0dbbc
Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents:
40587
diff
changeset
|
501 print("%-25s" % key, m.__file__ or "") |
|
26961
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
502 |
|
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
503 # Print missing modules |
|
26963
a573fede88e2
patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents:
26961
diff
changeset
|
504 missing, maybe = self.any_missing_maybe() |
|
a573fede88e2
patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents:
26961
diff
changeset
|
505 if missing: |
|
40775
4cdf21a0dbbc
Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents:
40587
diff
changeset
|
506 print() |
|
4cdf21a0dbbc
Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents:
40587
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 for name in missing: |
|
42052
881ab1d417eb
Some quick fixes of code that was sorting dict.keys() etc.
Guido van Rossum <guido@python.org>
parents:
41717
diff
changeset
|
509 mods = sorted(self.badmodules[name].keys()) |
|
40775
4cdf21a0dbbc
Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents:
40587
diff
changeset
|
510 print("?", name, "imported from", ', '.join(mods)) |
|
26963
a573fede88e2
patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents:
26961
diff
changeset
|
511 # 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
|
512 if maybe: |
|
40775
4cdf21a0dbbc
Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents:
40587
diff
changeset
|
513 print() |
|
4cdf21a0dbbc
Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents:
40587
diff
changeset
|
514 print("Submodules thay appear to be missing, but could also be", end=' ') |
|
4cdf21a0dbbc
Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents:
40587
diff
changeset
|
515 print("global names in the parent package:") |
|
26963
a573fede88e2
patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents:
26961
diff
changeset
|
516 for name in maybe: |
|
42052
881ab1d417eb
Some quick fixes of code that was sorting dict.keys() etc.
Guido van Rossum <guido@python.org>
parents:
41717
diff
changeset
|
517 mods = sorted(self.badmodules[name].keys()) |
|
40775
4cdf21a0dbbc
Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents:
40587
diff
changeset
|
518 print("?", name, "imported from", ', '.join(mods)) |
|
26961
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
519 |
|
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
520 def any_missing(self): |
|
26963
a573fede88e2
patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents:
26961
diff
changeset
|
521 """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
|
522 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
|
523 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
|
524 """ |
|
a573fede88e2
patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents:
26961
diff
changeset
|
525 missing, maybe = self.any_missing_maybe() |
|
a573fede88e2
patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents:
26961
diff
changeset
|
526 return missing + maybe |
|
a573fede88e2
patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents:
26961
diff
changeset
|
527 |
|
a573fede88e2
patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents:
26961
diff
changeset
|
528 def any_missing_maybe(self): |
|
a573fede88e2
patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents:
26961
diff
changeset
|
529 """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
|
530 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
|
531 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
|
532 |
|
a573fede88e2
patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents:
26961
diff
changeset
|
533 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
|
534 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
|
535 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
|
536 """ |
|
26961
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
537 missing = [] |
|
26963
a573fede88e2
patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents:
26961
diff
changeset
|
538 maybe = [] |
|
a573fede88e2
patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents:
26961
diff
changeset
|
539 for name in self.badmodules: |
|
a573fede88e2
patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents:
26961
diff
changeset
|
540 if name in self.excludes: |
|
a573fede88e2
patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents:
26961
diff
changeset
|
541 continue |
|
a573fede88e2
patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents:
26961
diff
changeset
|
542 i = name.rfind(".") |
|
a573fede88e2
patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents:
26961
diff
changeset
|
543 if i < 0: |
|
a573fede88e2
patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents:
26961
diff
changeset
|
544 missing.append(name) |
|
a573fede88e2
patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents:
26961
diff
changeset
|
545 continue |
|
a573fede88e2
patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents:
26961
diff
changeset
|
546 subname = name[i+1:] |
|
a573fede88e2
patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents:
26961
diff
changeset
|
547 pkgname = name[:i] |
|
a573fede88e2
patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents:
26961
diff
changeset
|
548 pkg = self.modules.get(pkgname) |
|
a573fede88e2
patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents:
26961
diff
changeset
|
549 if pkg is not None: |
|
a573fede88e2
patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents:
26961
diff
changeset
|
550 if pkgname in self.badmodules[name]: |
|
a573fede88e2
patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents:
26961
diff
changeset
|
551 # 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
|
552 # failed. It's definitely missing. |
|
a573fede88e2
patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents:
26961
diff
changeset
|
553 missing.append(name) |
|
a573fede88e2
patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents:
26961
diff
changeset
|
554 elif subname in pkg.globalnames: |
|
a573fede88e2
patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents:
26961
diff
changeset
|
555 # 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
|
556 pass |
|
a573fede88e2
patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents:
26961
diff
changeset
|
557 elif pkg.starimports: |
|
a573fede88e2
patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents:
26961
diff
changeset
|
558 # 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
|
559 # 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
|
560 maybe.append(name) |
|
a573fede88e2
patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents:
26961
diff
changeset
|
561 else: |
|
a573fede88e2
patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents:
26961
diff
changeset
|
562 # 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
|
563 # 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
|
564 # 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
|
565 # 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
|
566 # it missing. |
|
a573fede88e2
patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents:
26961
diff
changeset
|
567 missing.append(name) |
|
a573fede88e2
patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents:
26961
diff
changeset
|
568 else: |
|
a573fede88e2
patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents:
26961
diff
changeset
|
569 missing.append(name) |
|
a573fede88e2
patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents:
26961
diff
changeset
|
570 missing.sort() |
|
a573fede88e2
patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents:
26961
diff
changeset
|
571 maybe.sort() |
|
a573fede88e2
patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents:
26961
diff
changeset
|
572 return missing, maybe |
|
26961
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
573 |
|
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
574 def replace_paths_in_code(self, co): |
|
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
575 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
|
576 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
|
577 if original_filename.startswith(f): |
|
26963
a573fede88e2
patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents:
26961
diff
changeset
|
578 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
|
579 break |
|
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 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
|
582 if new_filename != original_filename: |
|
26961
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
583 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
|
584 % (original_filename,new_filename,)) |
|
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
585 else: |
|
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
586 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
|
587 % (original_filename,)) |
|
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
588 self.processed_paths.append(original_filename) |
|
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
589 |
|
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
590 consts = list(co.co_consts) |
|
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
591 for i in range(len(consts)): |
|
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
592 if isinstance(consts[i], type(co)): |
|
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
593 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
|
594 |
|
44036
45ced6ab066d
Merged revisions 59193-59201 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
43777
diff
changeset
|
595 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
|
596 co.co_flags, co.co_code, tuple(consts), co.co_names, |
|
d9d57f5f98cd
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
26963
diff
changeset
|
597 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
|
598 co.co_firstlineno, co.co_lnotab, |
|
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
599 co.co_freevars, co.co_cellvars) |
|
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
600 |
|
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
601 |
|
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
602 def test(): |
|
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
603 # Parse command line |
|
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
604 import getopt |
|
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
605 try: |
|
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
606 opts, args = getopt.getopt(sys.argv[1:], "dmp:qx:") |
|
40587
6b18a095c2e7
SF patch 1631942 by Collin Winter:
Guido van Rossum <guido@python.org>
parents:
40214
diff
changeset
|
607 except getopt.error as msg: |
|
40775
4cdf21a0dbbc
Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents:
40587
diff
changeset
|
608 print(msg) |
|
26961
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
609 return |
|
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
610 |
|
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
611 # Process options |
|
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
612 debug = 1 |
|
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
613 domods = 0 |
|
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
614 addpath = [] |
|
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
615 exclude = [] |
|
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
616 for o, a in opts: |
|
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
617 if o == '-d': |
|
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
618 debug = debug + 1 |
|
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
619 if o == '-m': |
|
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
620 domods = 1 |
|
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
621 if o == '-p': |
|
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
622 addpath = addpath + a.split(os.pathsep) |
|
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
623 if o == '-q': |
|
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
624 debug = 0 |
|
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
625 if o == '-x': |
|
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
626 exclude.append(a) |
|
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
627 |
|
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
628 # Provide default arguments |
|
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
629 if not args: |
|
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
630 script = "hello.py" |
|
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
631 else: |
|
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
632 script = args[0] |
|
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 # 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
|
635 path = sys.path[:] |
|
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
636 path[0] = os.path.dirname(script) |
|
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
637 path = addpath + path |
|
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
638 if debug > 1: |
|
40775
4cdf21a0dbbc
Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents:
40587
diff
changeset
|
639 print("path:") |
|
26961
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
640 for item in path: |
|
40775
4cdf21a0dbbc
Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents:
40587
diff
changeset
|
641 print(" ", repr(item)) |
|
26961
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
642 |
|
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
643 # 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
|
644 mf = ModuleFinder(path, debug, exclude) |
|
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
645 for arg in args[1:]: |
|
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
646 if arg == '-m': |
|
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
647 domods = 1 |
|
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
648 continue |
|
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
649 if domods: |
|
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
650 if arg[-2:] == '.*': |
|
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
651 mf.import_hook(arg[:-2], None, ["*"]) |
|
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
652 else: |
|
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
653 mf.import_hook(arg) |
|
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
654 else: |
|
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
655 mf.load_file(arg) |
|
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
656 mf.run_script(script) |
|
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
657 mf.report() |
|
26963
a573fede88e2
patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents:
26961
diff
changeset
|
658 return mf # for -i debugging |
|
26961
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
659 |
|
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
660 |
|
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
661 if __name__ == '__main__': |
|
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
662 try: |
|
26963
a573fede88e2
patch attached to sf item #643711:
Just van Rossum <just@letterror.com>
parents:
26961
diff
changeset
|
663 mf = test() |
|
26961
a5c879726b3a
moving modulefinder.py to the standard library
Just van Rossum <just@letterror.com>
parents:
diff
changeset
|
664 except KeyboardInterrupt: |
|
40775
4cdf21a0dbbc
Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents:
40587
diff
changeset
|
665 print("\n[interrupt]") |
