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