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