Mercurial > cpython-lru_cache-weakref
annotate Lib/pkgutil.py @ 80661:94de77bd0b4b 2.7
Issue #16194: document imp.load_dynamic problems
when read several extension modules from the same binary object file.
| author | Andrew Svetlov <andrew.svetlov@gmail.com> |
|---|---|
| date | Thu, 29 Nov 2012 19:51:28 +0200 |
| parents | 1449095397ae |
| children |
| rev | line source |
|---|---|
|
26753
7c15bf7726b2
Last week we discussed adding this module to the standard library.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1 """Utilities to support packages.""" |
|
7c15bf7726b2
Last week we discussed adding this module to the standard library.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2 |
|
37808
ccc0b5412799
First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
30448
diff
changeset
|
3 # NOTE: This module must remain compatible with Python 2.3, as it is shared |
|
ccc0b5412799
First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
30448
diff
changeset
|
4 # by setuptools for distribution with Python 2.3 and up. |
|
ccc0b5412799
First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
30448
diff
changeset
|
5 |
|
26753
7c15bf7726b2
Last week we discussed adding this module to the standard library.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6 import os |
|
7c15bf7726b2
Last week we discussed adding this module to the standard library.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7 import sys |
|
37808
ccc0b5412799
First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
30448
diff
changeset
|
8 import imp |
|
ccc0b5412799
First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
30448
diff
changeset
|
9 import os.path |
|
ccc0b5412799
First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
30448
diff
changeset
|
10 from types import ModuleType |
|
ccc0b5412799
First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
30448
diff
changeset
|
11 |
|
ccc0b5412799
First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
30448
diff
changeset
|
12 __all__ = [ |
|
ccc0b5412799
First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
30448
diff
changeset
|
13 'get_importer', 'iter_importers', 'get_loader', 'find_loader', |
|
70422
3f9683cd2d8d
Add missing name in pkgutil.__all__
Éric Araujo <merwok@netwok.org>
parents:
46454
diff
changeset
|
14 'walk_packages', 'iter_modules', 'get_data', |
|
37808
ccc0b5412799
First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
30448
diff
changeset
|
15 'ImpImporter', 'ImpLoader', 'read_code', 'extend_path', |
|
ccc0b5412799
First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
30448
diff
changeset
|
16 ] |
|
ccc0b5412799
First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
30448
diff
changeset
|
17 |
|
ccc0b5412799
First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
30448
diff
changeset
|
18 def read_code(stream): |
|
ccc0b5412799
First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
30448
diff
changeset
|
19 # This helper is needed in order for the PEP 302 emulation to |
|
ccc0b5412799
First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
30448
diff
changeset
|
20 # correctly handle compiled files |
|
ccc0b5412799
First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
30448
diff
changeset
|
21 import marshal |
|
ccc0b5412799
First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
30448
diff
changeset
|
22 |
|
ccc0b5412799
First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
30448
diff
changeset
|
23 magic = stream.read(4) |
|
ccc0b5412799
First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
30448
diff
changeset
|
24 if magic != imp.get_magic(): |
|
ccc0b5412799
First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
30448
diff
changeset
|
25 return None |
|
ccc0b5412799
First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
30448
diff
changeset
|
26 |
|
ccc0b5412799
First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
30448
diff
changeset
|
27 stream.read(4) # Skip timestamp |
|
ccc0b5412799
First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
30448
diff
changeset
|
28 return marshal.load(stream) |
|
ccc0b5412799
First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
30448
diff
changeset
|
29 |
|
ccc0b5412799
First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
30448
diff
changeset
|
30 |
|
37821
3135648026c4
Second phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
37808
diff
changeset
|
31 def simplegeneric(func): |
|
3135648026c4
Second phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
37808
diff
changeset
|
32 """Make a trivial single-dispatch generic function""" |
|
3135648026c4
Second phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
37808
diff
changeset
|
33 registry = {} |
|
38020
bb6c5c200a3f
Add some whitespace to be more consistent.
Neal Norwitz <nnorwitz@gmail.com>
parents:
37844
diff
changeset
|
34 def wrapper(*args, **kw): |
|
37821
3135648026c4
Second phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
37808
diff
changeset
|
35 ob = args[0] |
|
3135648026c4
Second phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
37808
diff
changeset
|
36 try: |
|
3135648026c4
Second phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
37808
diff
changeset
|
37 cls = ob.__class__ |
|
3135648026c4
Second phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
37808
diff
changeset
|
38 except AttributeError: |
|
3135648026c4
Second phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
37808
diff
changeset
|
39 cls = type(ob) |
|
3135648026c4
Second phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
37808
diff
changeset
|
40 try: |
|
3135648026c4
Second phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
37808
diff
changeset
|
41 mro = cls.__mro__ |
|
3135648026c4
Second phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
37808
diff
changeset
|
42 except AttributeError: |
|
3135648026c4
Second phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
37808
diff
changeset
|
43 try: |
|
38020
bb6c5c200a3f
Add some whitespace to be more consistent.
Neal Norwitz <nnorwitz@gmail.com>
parents:
37844
diff
changeset
|
44 class cls(cls, object): |
|
bb6c5c200a3f
Add some whitespace to be more consistent.
Neal Norwitz <nnorwitz@gmail.com>
parents:
37844
diff
changeset
|
45 pass |
|
37821
3135648026c4
Second phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
37808
diff
changeset
|
46 mro = cls.__mro__[1:] |
|
3135648026c4
Second phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
37808
diff
changeset
|
47 except TypeError: |
|
37844
8725b907acbb
Whilespace normalization (reindint.py).
Tim Peters <tim.peters@gmail.com>
parents:
37821
diff
changeset
|
48 mro = object, # must be an ExtensionClass or some such :( |
|
37821
3135648026c4
Second phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
37808
diff
changeset
|
49 for t in mro: |
|
3135648026c4
Second phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
37808
diff
changeset
|
50 if t in registry: |
|
38020
bb6c5c200a3f
Add some whitespace to be more consistent.
Neal Norwitz <nnorwitz@gmail.com>
parents:
37844
diff
changeset
|
51 return registry[t](*args, **kw) |
|
37821
3135648026c4
Second phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
37808
diff
changeset
|
52 else: |
|
38020
bb6c5c200a3f
Add some whitespace to be more consistent.
Neal Norwitz <nnorwitz@gmail.com>
parents:
37844
diff
changeset
|
53 return func(*args, **kw) |
|
37821
3135648026c4
Second phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
37808
diff
changeset
|
54 try: |
|
3135648026c4
Second phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
37808
diff
changeset
|
55 wrapper.__name__ = func.__name__ |
|
38020
bb6c5c200a3f
Add some whitespace to be more consistent.
Neal Norwitz <nnorwitz@gmail.com>
parents:
37844
diff
changeset
|
56 except (TypeError, AttributeError): |
|
37821
3135648026c4
Second phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
37808
diff
changeset
|
57 pass # Python 2.3 doesn't allow functions to be renamed |
|
3135648026c4
Second phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
37808
diff
changeset
|
58 |
|
3135648026c4
Second phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
37808
diff
changeset
|
59 def register(typ, func=None): |
|
3135648026c4
Second phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
37808
diff
changeset
|
60 if func is None: |
|
3135648026c4
Second phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
37808
diff
changeset
|
61 return lambda f: register(typ, f) |
|
3135648026c4
Second phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
37808
diff
changeset
|
62 registry[typ] = func |
|
3135648026c4
Second phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
37808
diff
changeset
|
63 return func |
|
3135648026c4
Second phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
37808
diff
changeset
|
64 |
|
3135648026c4
Second phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
37808
diff
changeset
|
65 wrapper.__dict__ = func.__dict__ |
|
3135648026c4
Second phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
37808
diff
changeset
|
66 wrapper.__doc__ = func.__doc__ |
|
3135648026c4
Second phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
37808
diff
changeset
|
67 wrapper.register = register |
|
37844
8725b907acbb
Whilespace normalization (reindint.py).
Tim Peters <tim.peters@gmail.com>
parents:
37821
diff
changeset
|
68 return wrapper |
|
37821
3135648026c4
Second phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
37808
diff
changeset
|
69 |
|
3135648026c4
Second phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
37808
diff
changeset
|
70 |
|
3135648026c4
Second phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
37808
diff
changeset
|
71 def walk_packages(path=None, prefix='', onerror=None): |
|
39173
6621edfd296a
Patch #1525766: correctly pass onerror arg to recursive calls
Georg Brandl <georg@python.org>
parents:
38740
diff
changeset
|
72 """Yields (module_loader, name, ispkg) for all modules recursively |
|
6621edfd296a
Patch #1525766: correctly pass onerror arg to recursive calls
Georg Brandl <georg@python.org>
parents:
38740
diff
changeset
|
73 on path, or, if path is None, all accessible modules. |
|
39183
b55bf2adfae5
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
39173
diff
changeset
|
74 |
|
39173
6621edfd296a
Patch #1525766: correctly pass onerror arg to recursive calls
Georg Brandl <georg@python.org>
parents:
38740
diff
changeset
|
75 'path' should be either None or a list of paths to look for |
|
6621edfd296a
Patch #1525766: correctly pass onerror arg to recursive calls
Georg Brandl <georg@python.org>
parents:
38740
diff
changeset
|
76 modules in. |
|
6621edfd296a
Patch #1525766: correctly pass onerror arg to recursive calls
Georg Brandl <georg@python.org>
parents:
38740
diff
changeset
|
77 |
|
6621edfd296a
Patch #1525766: correctly pass onerror arg to recursive calls
Georg Brandl <georg@python.org>
parents:
38740
diff
changeset
|
78 'prefix' is a string to output on the front of every module name |
|
6621edfd296a
Patch #1525766: correctly pass onerror arg to recursive calls
Georg Brandl <georg@python.org>
parents:
38740
diff
changeset
|
79 on output. |
|
6621edfd296a
Patch #1525766: correctly pass onerror arg to recursive calls
Georg Brandl <georg@python.org>
parents:
38740
diff
changeset
|
80 |
|
6621edfd296a
Patch #1525766: correctly pass onerror arg to recursive calls
Georg Brandl <georg@python.org>
parents:
38740
diff
changeset
|
81 Note that this function must import all *packages* (NOT all |
|
6621edfd296a
Patch #1525766: correctly pass onerror arg to recursive calls
Georg Brandl <georg@python.org>
parents:
38740
diff
changeset
|
82 modules!) on the given path, in order to access the __path__ |
|
6621edfd296a
Patch #1525766: correctly pass onerror arg to recursive calls
Georg Brandl <georg@python.org>
parents:
38740
diff
changeset
|
83 attribute to find submodules. |
|
39183
b55bf2adfae5
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
39173
diff
changeset
|
84 |
|
39173
6621edfd296a
Patch #1525766: correctly pass onerror arg to recursive calls
Georg Brandl <georg@python.org>
parents:
38740
diff
changeset
|
85 'onerror' is a function which gets called with one argument (the |
|
39198
1e00ef07beb2
Allow the 'onerror' argument to walk_packages() to catch any Exception, not
Phillip J. Eby <pje@telecommunity.com>
parents:
39183
diff
changeset
|
86 name of the package which was being imported) if any exception |
|
1e00ef07beb2
Allow the 'onerror' argument to walk_packages() to catch any Exception, not
Phillip J. Eby <pje@telecommunity.com>
parents:
39183
diff
changeset
|
87 occurs while trying to import a package. If no onerror function is |
|
1e00ef07beb2
Allow the 'onerror' argument to walk_packages() to catch any Exception, not
Phillip J. Eby <pje@telecommunity.com>
parents:
39183
diff
changeset
|
88 supplied, ImportErrors are caught and ignored, while all other |
|
1e00ef07beb2
Allow the 'onerror' argument to walk_packages() to catch any Exception, not
Phillip J. Eby <pje@telecommunity.com>
parents:
39183
diff
changeset
|
89 exceptions are propagated, terminating the search. |
|
39173
6621edfd296a
Patch #1525766: correctly pass onerror arg to recursive calls
Georg Brandl <georg@python.org>
parents:
38740
diff
changeset
|
90 |
|
6621edfd296a
Patch #1525766: correctly pass onerror arg to recursive calls
Georg Brandl <georg@python.org>
parents:
38740
diff
changeset
|
91 Examples: |
|
39198
1e00ef07beb2
Allow the 'onerror' argument to walk_packages() to catch any Exception, not
Phillip J. Eby <pje@telecommunity.com>
parents:
39183
diff
changeset
|
92 |
|
1e00ef07beb2
Allow the 'onerror' argument to walk_packages() to catch any Exception, not
Phillip J. Eby <pje@telecommunity.com>
parents:
39183
diff
changeset
|
93 # list all modules python can access |
|
39199
29c2fee6c984
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
39198
diff
changeset
|
94 walk_packages() |
|
39198
1e00ef07beb2
Allow the 'onerror' argument to walk_packages() to catch any Exception, not
Phillip J. Eby <pje@telecommunity.com>
parents:
39183
diff
changeset
|
95 |
|
1e00ef07beb2
Allow the 'onerror' argument to walk_packages() to catch any Exception, not
Phillip J. Eby <pje@telecommunity.com>
parents:
39183
diff
changeset
|
96 # list all submodules of ctypes |
|
1e00ef07beb2
Allow the 'onerror' argument to walk_packages() to catch any Exception, not
Phillip J. Eby <pje@telecommunity.com>
parents:
39183
diff
changeset
|
97 walk_packages(ctypes.__path__, ctypes.__name__+'.') |
|
39173
6621edfd296a
Patch #1525766: correctly pass onerror arg to recursive calls
Georg Brandl <georg@python.org>
parents:
38740
diff
changeset
|
98 """ |
|
37821
3135648026c4
Second phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
37808
diff
changeset
|
99 |
|
38020
bb6c5c200a3f
Add some whitespace to be more consistent.
Neal Norwitz <nnorwitz@gmail.com>
parents:
37844
diff
changeset
|
100 def seen(p, m={}): |
|
bb6c5c200a3f
Add some whitespace to be more consistent.
Neal Norwitz <nnorwitz@gmail.com>
parents:
37844
diff
changeset
|
101 if p in m: |
|
bb6c5c200a3f
Add some whitespace to be more consistent.
Neal Norwitz <nnorwitz@gmail.com>
parents:
37844
diff
changeset
|
102 return True |
|
37821
3135648026c4
Second phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
37808
diff
changeset
|
103 m[p] = True |
|
3135648026c4
Second phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
37808
diff
changeset
|
104 |
|
3135648026c4
Second phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
37808
diff
changeset
|
105 for importer, name, ispkg in iter_modules(path, prefix): |
|
3135648026c4
Second phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
37808
diff
changeset
|
106 yield importer, name, ispkg |
|
3135648026c4
Second phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
37808
diff
changeset
|
107 |
|
3135648026c4
Second phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
37808
diff
changeset
|
108 if ispkg: |
|
3135648026c4
Second phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
37808
diff
changeset
|
109 try: |
|
3135648026c4
Second phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
37808
diff
changeset
|
110 __import__(name) |
|
3135648026c4
Second phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
37808
diff
changeset
|
111 except ImportError: |
|
3135648026c4
Second phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
37808
diff
changeset
|
112 if onerror is not None: |
|
39173
6621edfd296a
Patch #1525766: correctly pass onerror arg to recursive calls
Georg Brandl <georg@python.org>
parents:
38740
diff
changeset
|
113 onerror(name) |
|
39198
1e00ef07beb2
Allow the 'onerror' argument to walk_packages() to catch any Exception, not
Phillip J. Eby <pje@telecommunity.com>
parents:
39183
diff
changeset
|
114 except Exception: |
|
1e00ef07beb2
Allow the 'onerror' argument to walk_packages() to catch any Exception, not
Phillip J. Eby <pje@telecommunity.com>
parents:
39183
diff
changeset
|
115 if onerror is not None: |
|
1e00ef07beb2
Allow the 'onerror' argument to walk_packages() to catch any Exception, not
Phillip J. Eby <pje@telecommunity.com>
parents:
39183
diff
changeset
|
116 onerror(name) |
|
1e00ef07beb2
Allow the 'onerror' argument to walk_packages() to catch any Exception, not
Phillip J. Eby <pje@telecommunity.com>
parents:
39183
diff
changeset
|
117 else: |
|
1e00ef07beb2
Allow the 'onerror' argument to walk_packages() to catch any Exception, not
Phillip J. Eby <pje@telecommunity.com>
parents:
39183
diff
changeset
|
118 raise |
|
37821
3135648026c4
Second phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
37808
diff
changeset
|
119 else: |
|
3135648026c4
Second phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
37808
diff
changeset
|
120 path = getattr(sys.modules[name], '__path__', None) or [] |
|
3135648026c4
Second phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
37808
diff
changeset
|
121 |
|
3135648026c4
Second phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
37808
diff
changeset
|
122 # don't traverse path items we've seen before |
|
3135648026c4
Second phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
37808
diff
changeset
|
123 path = [p for p in path if not seen(p)] |
|
3135648026c4
Second phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
37808
diff
changeset
|
124 |
|
39173
6621edfd296a
Patch #1525766: correctly pass onerror arg to recursive calls
Georg Brandl <georg@python.org>
parents:
38740
diff
changeset
|
125 for item in walk_packages(path, name+'.', onerror): |
|
37821
3135648026c4
Second phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
37808
diff
changeset
|
126 yield item |
|
3135648026c4
Second phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
37808
diff
changeset
|
127 |
|
3135648026c4
Second phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
37808
diff
changeset
|
128 |
|
3135648026c4
Second phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
37808
diff
changeset
|
129 def iter_modules(path=None, prefix=''): |
|
39173
6621edfd296a
Patch #1525766: correctly pass onerror arg to recursive calls
Georg Brandl <georg@python.org>
parents:
38740
diff
changeset
|
130 """Yields (module_loader, name, ispkg) for all submodules on path, |
|
6621edfd296a
Patch #1525766: correctly pass onerror arg to recursive calls
Georg Brandl <georg@python.org>
parents:
38740
diff
changeset
|
131 or, if path is None, all top-level modules on sys.path. |
|
6621edfd296a
Patch #1525766: correctly pass onerror arg to recursive calls
Georg Brandl <georg@python.org>
parents:
38740
diff
changeset
|
132 |
|
6621edfd296a
Patch #1525766: correctly pass onerror arg to recursive calls
Georg Brandl <georg@python.org>
parents:
38740
diff
changeset
|
133 'path' should be either None or a list of paths to look for |
|
6621edfd296a
Patch #1525766: correctly pass onerror arg to recursive calls
Georg Brandl <georg@python.org>
parents:
38740
diff
changeset
|
134 modules in. |
|
6621edfd296a
Patch #1525766: correctly pass onerror arg to recursive calls
Georg Brandl <georg@python.org>
parents:
38740
diff
changeset
|
135 |
|
6621edfd296a
Patch #1525766: correctly pass onerror arg to recursive calls
Georg Brandl <georg@python.org>
parents:
38740
diff
changeset
|
136 'prefix' is a string to output on the front of every module name |
|
6621edfd296a
Patch #1525766: correctly pass onerror arg to recursive calls
Georg Brandl <georg@python.org>
parents:
38740
diff
changeset
|
137 on output. |
|
6621edfd296a
Patch #1525766: correctly pass onerror arg to recursive calls
Georg Brandl <georg@python.org>
parents:
38740
diff
changeset
|
138 """ |
|
39183
b55bf2adfae5
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
39173
diff
changeset
|
139 |
|
37821
3135648026c4
Second phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
37808
diff
changeset
|
140 if path is None: |
|
3135648026c4
Second phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
37808
diff
changeset
|
141 importers = iter_importers() |
|
3135648026c4
Second phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
37808
diff
changeset
|
142 else: |
|
3135648026c4
Second phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
37808
diff
changeset
|
143 importers = map(get_importer, path) |
|
3135648026c4
Second phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
37808
diff
changeset
|
144 |
|
3135648026c4
Second phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
37808
diff
changeset
|
145 yielded = {} |
|
3135648026c4
Second phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
37808
diff
changeset
|
146 for i in importers: |
|
3135648026c4
Second phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
37808
diff
changeset
|
147 for name, ispkg in iter_importer_modules(i, prefix): |
|
3135648026c4
Second phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
37808
diff
changeset
|
148 if name not in yielded: |
|
3135648026c4
Second phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
37808
diff
changeset
|
149 yielded[name] = 1 |
|
3135648026c4
Second phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
37808
diff
changeset
|
150 yield i, name, ispkg |
|
3135648026c4
Second phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
37808
diff
changeset
|
151 |
|
3135648026c4
Second phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
37808
diff
changeset
|
152 |
|
3135648026c4
Second phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
37808
diff
changeset
|
153 #@simplegeneric |
|
3135648026c4
Second phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
37808
diff
changeset
|
154 def iter_importer_modules(importer, prefix=''): |
|
38020
bb6c5c200a3f
Add some whitespace to be more consistent.
Neal Norwitz <nnorwitz@gmail.com>
parents:
37844
diff
changeset
|
155 if not hasattr(importer, 'iter_modules'): |
|
37821
3135648026c4
Second phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
37808
diff
changeset
|
156 return [] |
|
3135648026c4
Second phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
37808
diff
changeset
|
157 return importer.iter_modules(prefix) |
|
3135648026c4
Second phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
37808
diff
changeset
|
158 |
|
3135648026c4
Second phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
37808
diff
changeset
|
159 iter_importer_modules = simplegeneric(iter_importer_modules) |
|
3135648026c4
Second phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
37808
diff
changeset
|
160 |
|
3135648026c4
Second phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
37808
diff
changeset
|
161 |
|
37808
ccc0b5412799
First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
30448
diff
changeset
|
162 class ImpImporter: |
|
ccc0b5412799
First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
30448
diff
changeset
|
163 """PEP 302 Importer that wraps Python's "classic" import algorithm |
|
ccc0b5412799
First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
30448
diff
changeset
|
164 |
|
ccc0b5412799
First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
30448
diff
changeset
|
165 ImpImporter(dirname) produces a PEP 302 importer that searches that |
|
ccc0b5412799
First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
30448
diff
changeset
|
166 directory. ImpImporter(None) produces a PEP 302 importer that searches |
|
ccc0b5412799
First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
30448
diff
changeset
|
167 the current sys.path, plus any modules that are frozen or built-in. |
|
ccc0b5412799
First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
30448
diff
changeset
|
168 |
|
ccc0b5412799
First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
30448
diff
changeset
|
169 Note that ImpImporter does not currently support being used by placement |
|
ccc0b5412799
First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
30448
diff
changeset
|
170 on sys.meta_path. |
|
ccc0b5412799
First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
30448
diff
changeset
|
171 """ |
|
ccc0b5412799
First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
30448
diff
changeset
|
172 |
|
ccc0b5412799
First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
30448
diff
changeset
|
173 def __init__(self, path=None): |
|
ccc0b5412799
First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
30448
diff
changeset
|
174 self.path = path |
|
ccc0b5412799
First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
30448
diff
changeset
|
175 |
|
ccc0b5412799
First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
30448
diff
changeset
|
176 def find_module(self, fullname, path=None): |
|
ccc0b5412799
First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
30448
diff
changeset
|
177 # Note: we ignore 'path' argument since it is only used via meta_path |
|
ccc0b5412799
First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
30448
diff
changeset
|
178 subname = fullname.split(".")[-1] |
|
ccc0b5412799
First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
30448
diff
changeset
|
179 if subname != fullname and self.path is None: |
|
ccc0b5412799
First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
30448
diff
changeset
|
180 return None |
|
ccc0b5412799
First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
30448
diff
changeset
|
181 if self.path is None: |
|
ccc0b5412799
First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
30448
diff
changeset
|
182 path = None |
|
ccc0b5412799
First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
30448
diff
changeset
|
183 else: |
|
37821
3135648026c4
Second phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
37808
diff
changeset
|
184 path = [os.path.realpath(self.path)] |
|
37808
ccc0b5412799
First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
30448
diff
changeset
|
185 try: |
|
ccc0b5412799
First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
30448
diff
changeset
|
186 file, filename, etc = imp.find_module(subname, path) |
|
ccc0b5412799
First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
30448
diff
changeset
|
187 except ImportError: |
|
ccc0b5412799
First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
30448
diff
changeset
|
188 return None |
|
ccc0b5412799
First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
30448
diff
changeset
|
189 return ImpLoader(fullname, file, filename, etc) |
|
ccc0b5412799
First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
30448
diff
changeset
|
190 |
|
37821
3135648026c4
Second phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
37808
diff
changeset
|
191 def iter_modules(self, prefix=''): |
|
3135648026c4
Second phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
37808
diff
changeset
|
192 if self.path is None or not os.path.isdir(self.path): |
|
3135648026c4
Second phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
37808
diff
changeset
|
193 return |
|
3135648026c4
Second phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
37808
diff
changeset
|
194 |
|
3135648026c4
Second phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
37808
diff
changeset
|
195 yielded = {} |
|
3135648026c4
Second phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
37808
diff
changeset
|
196 import inspect |
|
72771
1449095397ae
Issue #7367: Fix pkgutil.walk_paths to skip directories whose
Ned Deily <nad@acm.org>
parents:
70422
diff
changeset
|
197 try: |
|
1449095397ae
Issue #7367: Fix pkgutil.walk_paths to skip directories whose
Ned Deily <nad@acm.org>
parents:
70422
diff
changeset
|
198 filenames = os.listdir(self.path) |
|
1449095397ae
Issue #7367: Fix pkgutil.walk_paths to skip directories whose
Ned Deily <nad@acm.org>
parents:
70422
diff
changeset
|
199 except OSError: |
|
1449095397ae
Issue #7367: Fix pkgutil.walk_paths to skip directories whose
Ned Deily <nad@acm.org>
parents:
70422
diff
changeset
|
200 # ignore unreadable directories like import does |
|
1449095397ae
Issue #7367: Fix pkgutil.walk_paths to skip directories whose
Ned Deily <nad@acm.org>
parents:
70422
diff
changeset
|
201 filenames = [] |
|
37821
3135648026c4
Second phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
37808
diff
changeset
|
202 filenames.sort() # handle packages before same-named modules |
|
3135648026c4
Second phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
37808
diff
changeset
|
203 |
|
3135648026c4
Second phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
37808
diff
changeset
|
204 for fn in filenames: |
|
3135648026c4
Second phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
37808
diff
changeset
|
205 modname = inspect.getmodulename(fn) |
|
3135648026c4
Second phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
37808
diff
changeset
|
206 if modname=='__init__' or modname in yielded: |
|
3135648026c4
Second phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
37808
diff
changeset
|
207 continue |
|
37844
8725b907acbb
Whilespace normalization (reindint.py).
Tim Peters <tim.peters@gmail.com>
parents:
37821
diff
changeset
|
208 |
|
37821
3135648026c4
Second phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
37808
diff
changeset
|
209 path = os.path.join(self.path, fn) |
|
3135648026c4
Second phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
37808
diff
changeset
|
210 ispkg = False |
|
3135648026c4
Second phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
37808
diff
changeset
|
211 |
|
3135648026c4
Second phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
37808
diff
changeset
|
212 if not modname and os.path.isdir(path) and '.' not in fn: |
|
3135648026c4
Second phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
37808
diff
changeset
|
213 modname = fn |
|
72771
1449095397ae
Issue #7367: Fix pkgutil.walk_paths to skip directories whose
Ned Deily <nad@acm.org>
parents:
70422
diff
changeset
|
214 try: |
|
1449095397ae
Issue #7367: Fix pkgutil.walk_paths to skip directories whose
Ned Deily <nad@acm.org>
parents:
70422
diff
changeset
|
215 dircontents = os.listdir(path) |
|
1449095397ae
Issue #7367: Fix pkgutil.walk_paths to skip directories whose
Ned Deily <nad@acm.org>
parents:
70422
diff
changeset
|
216 except OSError: |
|
1449095397ae
Issue #7367: Fix pkgutil.walk_paths to skip directories whose
Ned Deily <nad@acm.org>
parents:
70422
diff
changeset
|
217 # ignore unreadable directories like import does |
|
1449095397ae
Issue #7367: Fix pkgutil.walk_paths to skip directories whose
Ned Deily <nad@acm.org>
parents:
70422
diff
changeset
|
218 dircontents = [] |
|
1449095397ae
Issue #7367: Fix pkgutil.walk_paths to skip directories whose
Ned Deily <nad@acm.org>
parents:
70422
diff
changeset
|
219 for fn in dircontents: |
|
37821
3135648026c4
Second phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
37808
diff
changeset
|
220 subname = inspect.getmodulename(fn) |
|
3135648026c4
Second phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
37808
diff
changeset
|
221 if subname=='__init__': |
|
3135648026c4
Second phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
37808
diff
changeset
|
222 ispkg = True |
|
3135648026c4
Second phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
37808
diff
changeset
|
223 break |
|
3135648026c4
Second phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
37808
diff
changeset
|
224 else: |
|
3135648026c4
Second phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
37808
diff
changeset
|
225 continue # not a package |
|
3135648026c4
Second phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
37808
diff
changeset
|
226 |
|
3135648026c4
Second phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
37808
diff
changeset
|
227 if modname and '.' not in modname: |
|
3135648026c4
Second phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
37808
diff
changeset
|
228 yielded[modname] = 1 |
|
3135648026c4
Second phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
37808
diff
changeset
|
229 yield prefix + modname, ispkg |
|
3135648026c4
Second phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
37808
diff
changeset
|
230 |
|
37808
ccc0b5412799
First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
30448
diff
changeset
|
231 |
|
ccc0b5412799
First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
30448
diff
changeset
|
232 class ImpLoader: |
|
ccc0b5412799
First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
30448
diff
changeset
|
233 """PEP 302 Loader that wraps Python's "classic" import algorithm |
|
ccc0b5412799
First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
30448
diff
changeset
|
234 """ |
|
ccc0b5412799
First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
30448
diff
changeset
|
235 code = source = None |
|
ccc0b5412799
First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
30448
diff
changeset
|
236 |
|
ccc0b5412799
First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
30448
diff
changeset
|
237 def __init__(self, fullname, file, filename, etc): |
|
ccc0b5412799
First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
30448
diff
changeset
|
238 self.file = file |
|
ccc0b5412799
First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
30448
diff
changeset
|
239 self.filename = filename |
|
ccc0b5412799
First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
30448
diff
changeset
|
240 self.fullname = fullname |
|
ccc0b5412799
First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
30448
diff
changeset
|
241 self.etc = etc |
|
ccc0b5412799
First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
30448
diff
changeset
|
242 |
|
ccc0b5412799
First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
30448
diff
changeset
|
243 def load_module(self, fullname): |
|
ccc0b5412799
First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
30448
diff
changeset
|
244 self._reopen() |
|
ccc0b5412799
First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
30448
diff
changeset
|
245 try: |
|
ccc0b5412799
First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
30448
diff
changeset
|
246 mod = imp.load_module(fullname, self.file, self.filename, self.etc) |
|
ccc0b5412799
First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
30448
diff
changeset
|
247 finally: |
|
ccc0b5412799
First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
30448
diff
changeset
|
248 if self.file: |
|
ccc0b5412799
First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
30448
diff
changeset
|
249 self.file.close() |
|
ccc0b5412799
First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
30448
diff
changeset
|
250 # Note: we don't set __loader__ because we want the module to look |
|
ccc0b5412799
First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
30448
diff
changeset
|
251 # normal; i.e. this is just a wrapper for standard import machinery |
|
ccc0b5412799
First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
30448
diff
changeset
|
252 return mod |
|
ccc0b5412799
First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
30448
diff
changeset
|
253 |
|
ccc0b5412799
First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
30448
diff
changeset
|
254 def get_data(self, pathname): |
|
ccc0b5412799
First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
30448
diff
changeset
|
255 return open(pathname, "rb").read() |
|
ccc0b5412799
First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
30448
diff
changeset
|
256 |
|
ccc0b5412799
First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
30448
diff
changeset
|
257 def _reopen(self): |
|
ccc0b5412799
First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
30448
diff
changeset
|
258 if self.file and self.file.closed: |
|
38740
501ee2a05fec
Fix errors found by pychecker
Neal Norwitz <nnorwitz@gmail.com>
parents:
38388
diff
changeset
|
259 mod_type = self.etc[2] |
|
37808
ccc0b5412799
First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
30448
diff
changeset
|
260 if mod_type==imp.PY_SOURCE: |
|
ccc0b5412799
First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
30448
diff
changeset
|
261 self.file = open(self.filename, 'rU') |
|
ccc0b5412799
First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
30448
diff
changeset
|
262 elif mod_type in (imp.PY_COMPILED, imp.C_EXTENSION): |
|
ccc0b5412799
First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
30448
diff
changeset
|
263 self.file = open(self.filename, 'rb') |
|
ccc0b5412799
First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
30448
diff
changeset
|
264 |
|
ccc0b5412799
First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
30448
diff
changeset
|
265 def _fix_name(self, fullname): |
|
ccc0b5412799
First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
30448
diff
changeset
|
266 if fullname is None: |
|
ccc0b5412799
First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
30448
diff
changeset
|
267 fullname = self.fullname |
|
ccc0b5412799
First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
30448
diff
changeset
|
268 elif fullname != self.fullname: |
|
ccc0b5412799
First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
30448
diff
changeset
|
269 raise ImportError("Loader for module %s cannot handle " |
|
ccc0b5412799
First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
30448
diff
changeset
|
270 "module %s" % (self.fullname, fullname)) |
|
ccc0b5412799
First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
30448
diff
changeset
|
271 return fullname |
|
ccc0b5412799
First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
30448
diff
changeset
|
272 |
|
37821
3135648026c4
Second phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
37808
diff
changeset
|
273 def is_package(self, fullname): |
|
3135648026c4
Second phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
37808
diff
changeset
|
274 fullname = self._fix_name(fullname) |
|
37808
ccc0b5412799
First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
30448
diff
changeset
|
275 return self.etc[2]==imp.PKG_DIRECTORY |
|
ccc0b5412799
First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
30448
diff
changeset
|
276 |
|
ccc0b5412799
First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
30448
diff
changeset
|
277 def get_code(self, fullname=None): |
|
ccc0b5412799
First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
30448
diff
changeset
|
278 fullname = self._fix_name(fullname) |
|
ccc0b5412799
First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
30448
diff
changeset
|
279 if self.code is None: |
|
ccc0b5412799
First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
30448
diff
changeset
|
280 mod_type = self.etc[2] |
|
ccc0b5412799
First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
30448
diff
changeset
|
281 if mod_type==imp.PY_SOURCE: |
|
ccc0b5412799
First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
30448
diff
changeset
|
282 source = self.get_source(fullname) |
|
ccc0b5412799
First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
30448
diff
changeset
|
283 self.code = compile(source, self.filename, 'exec') |
|
ccc0b5412799
First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
30448
diff
changeset
|
284 elif mod_type==imp.PY_COMPILED: |
|
ccc0b5412799
First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
30448
diff
changeset
|
285 self._reopen() |
|
ccc0b5412799
First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
30448
diff
changeset
|
286 try: |
|
ccc0b5412799
First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
30448
diff
changeset
|
287 self.code = read_code(self.file) |
|
ccc0b5412799
First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
30448
diff
changeset
|
288 finally: |
|
ccc0b5412799
First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
30448
diff
changeset
|
289 self.file.close() |
|
ccc0b5412799
First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
30448
diff
changeset
|
290 elif mod_type==imp.PKG_DIRECTORY: |
|
ccc0b5412799
First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
30448
diff
changeset
|
291 self.code = self._get_delegate().get_code() |
|
ccc0b5412799
First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
30448
diff
changeset
|
292 return self.code |
|
ccc0b5412799
First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
30448
diff
changeset
|
293 |
|
ccc0b5412799
First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
30448
diff
changeset
|
294 def get_source(self, fullname=None): |
|
ccc0b5412799
First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
30448
diff
changeset
|
295 fullname = self._fix_name(fullname) |
|
ccc0b5412799
First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
30448
diff
changeset
|
296 if self.source is None: |
|
ccc0b5412799
First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
30448
diff
changeset
|
297 mod_type = self.etc[2] |
|
ccc0b5412799
First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
30448
diff
changeset
|
298 if mod_type==imp.PY_SOURCE: |
|
ccc0b5412799
First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
30448
diff
changeset
|
299 self._reopen() |
|
ccc0b5412799
First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
30448
diff
changeset
|
300 try: |
|
ccc0b5412799
First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
30448
diff
changeset
|
301 self.source = self.file.read() |
|
ccc0b5412799
First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
30448
diff
changeset
|
302 finally: |
|
ccc0b5412799
First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
30448
diff
changeset
|
303 self.file.close() |
|
ccc0b5412799
First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
30448
diff
changeset
|
304 elif mod_type==imp.PY_COMPILED: |
|
ccc0b5412799
First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
30448
diff
changeset
|
305 if os.path.exists(self.filename[:-1]): |
|
ccc0b5412799
First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
30448
diff
changeset
|
306 f = open(self.filename[:-1], 'rU') |
|
ccc0b5412799
First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
30448
diff
changeset
|
307 self.source = f.read() |
|
ccc0b5412799
First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
30448
diff
changeset
|
308 f.close() |
|
ccc0b5412799
First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
30448
diff
changeset
|
309 elif mod_type==imp.PKG_DIRECTORY: |
|
ccc0b5412799
First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
30448
diff
changeset
|
310 self.source = self._get_delegate().get_source() |
|
ccc0b5412799
First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
30448
diff
changeset
|
311 return self.source |
|
ccc0b5412799
First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
30448
diff
changeset
|
312 |
|
37821
3135648026c4
Second phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
37808
diff
changeset
|
313 |
|
37808
ccc0b5412799
First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
30448
diff
changeset
|
314 def _get_delegate(self): |
|
ccc0b5412799
First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
30448
diff
changeset
|
315 return ImpImporter(self.filename).find_module('__init__') |
|
ccc0b5412799
First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
30448
diff
changeset
|
316 |
|
ccc0b5412799
First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
30448
diff
changeset
|
317 def get_filename(self, fullname=None): |
|
ccc0b5412799
First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
30448
diff
changeset
|
318 fullname = self._fix_name(fullname) |
|
ccc0b5412799
First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
30448
diff
changeset
|
319 mod_type = self.etc[2] |
|
ccc0b5412799
First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
30448
diff
changeset
|
320 if self.etc[2]==imp.PKG_DIRECTORY: |
|
ccc0b5412799
First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
30448
diff
changeset
|
321 return self._get_delegate().get_filename() |
|
ccc0b5412799
First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
30448
diff
changeset
|
322 elif self.etc[2] in (imp.PY_SOURCE, imp.PY_COMPILED, imp.C_EXTENSION): |
|
ccc0b5412799
First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
30448
diff
changeset
|
323 return self.filename |
|
ccc0b5412799
First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
30448
diff
changeset
|
324 return None |
|
ccc0b5412799
First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
30448
diff
changeset
|
325 |
|
ccc0b5412799
First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
30448
diff
changeset
|
326 |
|
37821
3135648026c4
Second phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
37808
diff
changeset
|
327 try: |
|
3135648026c4
Second phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
37808
diff
changeset
|
328 import zipimport |
|
3135648026c4
Second phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
37808
diff
changeset
|
329 from zipimport import zipimporter |
|
37844
8725b907acbb
Whilespace normalization (reindint.py).
Tim Peters <tim.peters@gmail.com>
parents:
37821
diff
changeset
|
330 |
|
37821
3135648026c4
Second phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
37808
diff
changeset
|
331 def iter_zipimport_modules(importer, prefix=''): |
|
3135648026c4
Second phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
37808
diff
changeset
|
332 dirlist = zipimport._zip_directory_cache[importer.archive].keys() |
|
3135648026c4
Second phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
37808
diff
changeset
|
333 dirlist.sort() |
|
3135648026c4
Second phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
37808
diff
changeset
|
334 _prefix = importer.prefix |
|
3135648026c4
Second phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
37808
diff
changeset
|
335 plen = len(_prefix) |
|
3135648026c4
Second phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
37808
diff
changeset
|
336 yielded = {} |
|
3135648026c4
Second phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
37808
diff
changeset
|
337 import inspect |
|
3135648026c4
Second phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
37808
diff
changeset
|
338 for fn in dirlist: |
|
3135648026c4
Second phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
37808
diff
changeset
|
339 if not fn.startswith(_prefix): |
|
3135648026c4
Second phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
37808
diff
changeset
|
340 continue |
|
3135648026c4
Second phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
37808
diff
changeset
|
341 |
|
3135648026c4
Second phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
37808
diff
changeset
|
342 fn = fn[plen:].split(os.sep) |
|
3135648026c4
Second phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
37808
diff
changeset
|
343 |
|
3135648026c4
Second phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
37808
diff
changeset
|
344 if len(fn)==2 and fn[1].startswith('__init__.py'): |
|
3135648026c4
Second phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
37808
diff
changeset
|
345 if fn[0] not in yielded: |
|
3135648026c4
Second phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
37808
diff
changeset
|
346 yielded[fn[0]] = 1 |
|
3135648026c4
Second phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
37808
diff
changeset
|
347 yield fn[0], True |
|
3135648026c4
Second phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
37808
diff
changeset
|
348 |
|
3135648026c4
Second phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
37808
diff
changeset
|
349 if len(fn)!=1: |
|
3135648026c4
Second phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
37808
diff
changeset
|
350 continue |
|
3135648026c4
Second phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
37808
diff
changeset
|
351 |
|
3135648026c4
Second phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
37808
diff
changeset
|
352 modname = inspect.getmodulename(fn[0]) |
|
3135648026c4
Second phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
37808
diff
changeset
|
353 if modname=='__init__': |
|
3135648026c4
Second phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
37808
diff
changeset
|
354 continue |
|
3135648026c4
Second phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
37808
diff
changeset
|
355 |
|
3135648026c4
Second phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
37808
diff
changeset
|
356 if modname and '.' not in modname and modname not in yielded: |
|
3135648026c4
Second phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
37808
diff
changeset
|
357 yielded[modname] = 1 |
|
3135648026c4
Second phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
37808
diff
changeset
|
358 yield prefix + modname, False |
|
3135648026c4
Second phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
37808
diff
changeset
|
359 |
|
3135648026c4
Second phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
37808
diff
changeset
|
360 iter_importer_modules.register(zipimporter, iter_zipimport_modules) |
|
3135648026c4
Second phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
37808
diff
changeset
|
361 |
|
3135648026c4
Second phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
37808
diff
changeset
|
362 except ImportError: |
|
3135648026c4
Second phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
37808
diff
changeset
|
363 pass |
|
3135648026c4
Second phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
37808
diff
changeset
|
364 |
|
3135648026c4
Second phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
37808
diff
changeset
|
365 |
|
37808
ccc0b5412799
First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
30448
diff
changeset
|
366 def get_importer(path_item): |
|
ccc0b5412799
First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
30448
diff
changeset
|
367 """Retrieve a PEP 302 importer for the given path item |
|
ccc0b5412799
First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
30448
diff
changeset
|
368 |
|
ccc0b5412799
First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
30448
diff
changeset
|
369 The returned importer is cached in sys.path_importer_cache |
|
ccc0b5412799
First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
30448
diff
changeset
|
370 if it was newly created by a path hook. |
|
ccc0b5412799
First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
30448
diff
changeset
|
371 |
|
ccc0b5412799
First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
30448
diff
changeset
|
372 If there is no importer, a wrapper around the basic import |
|
ccc0b5412799
First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
30448
diff
changeset
|
373 machinery is returned. This wrapper is never inserted into |
|
ccc0b5412799
First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
30448
diff
changeset
|
374 the importer cache (None is inserted instead). |
|
ccc0b5412799
First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
30448
diff
changeset
|
375 |
|
ccc0b5412799
First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
30448
diff
changeset
|
376 The cache (or part of it) can be cleared manually if a |
|
ccc0b5412799
First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
30448
diff
changeset
|
377 rescan of sys.path_hooks is necessary. |
|
ccc0b5412799
First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
30448
diff
changeset
|
378 """ |
|
ccc0b5412799
First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
30448
diff
changeset
|
379 try: |
|
ccc0b5412799
First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
30448
diff
changeset
|
380 importer = sys.path_importer_cache[path_item] |
|
ccc0b5412799
First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
30448
diff
changeset
|
381 except KeyError: |
|
ccc0b5412799
First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
30448
diff
changeset
|
382 for path_hook in sys.path_hooks: |
|
ccc0b5412799
First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
30448
diff
changeset
|
383 try: |
|
ccc0b5412799
First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
30448
diff
changeset
|
384 importer = path_hook(path_item) |
|
ccc0b5412799
First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
30448
diff
changeset
|
385 break |
|
ccc0b5412799
First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
30448
diff
changeset
|
386 except ImportError: |
|
ccc0b5412799
First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
30448
diff
changeset
|
387 pass |
|
ccc0b5412799
First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
30448
diff
changeset
|
388 else: |
|
ccc0b5412799
First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
30448
diff
changeset
|
389 importer = None |
|
38020
bb6c5c200a3f
Add some whitespace to be more consistent.
Neal Norwitz <nnorwitz@gmail.com>
parents:
37844
diff
changeset
|
390 sys.path_importer_cache.setdefault(path_item, importer) |
|
37808
ccc0b5412799
First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
30448
diff
changeset
|
391 |
|
39253
3f4485b20e79
Bug #1529871: The speed enhancement patch #921466 broke Python's compliance
Phillip J. Eby <pje@telecommunity.com>
parents:
39199
diff
changeset
|
392 if importer is None: |
|
37808
ccc0b5412799
First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
30448
diff
changeset
|
393 try: |
|
ccc0b5412799
First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
30448
diff
changeset
|
394 importer = ImpImporter(path_item) |
|
ccc0b5412799
First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
30448
diff
changeset
|
395 except ImportError: |
|
38388
d21896b2dd0a
Need for speed: Patch #921466 : sys.path_importer_cache is now used to cache valid and
Georg Brandl <georg@python.org>
parents:
38020
diff
changeset
|
396 importer = None |
|
37808
ccc0b5412799
First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
30448
diff
changeset
|
397 return importer |
|
ccc0b5412799
First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
30448
diff
changeset
|
398 |
|
ccc0b5412799
First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
30448
diff
changeset
|
399 |
|
37821
3135648026c4
Second phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
37808
diff
changeset
|
400 def iter_importers(fullname=""): |
|
37808
ccc0b5412799
First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
30448
diff
changeset
|
401 """Yield PEP 302 importers for the given module name |
|
ccc0b5412799
First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
30448
diff
changeset
|
402 |
|
ccc0b5412799
First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
30448
diff
changeset
|
403 If fullname contains a '.', the importers will be for the package |
|
ccc0b5412799
First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
30448
diff
changeset
|
404 containing fullname, otherwise they will be importers for sys.meta_path, |
|
ccc0b5412799
First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
30448
diff
changeset
|
405 sys.path, and Python's "classic" import machinery, in that order. If |
|
ccc0b5412799
First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
30448
diff
changeset
|
406 the named module is in a package, that package is imported as a side |
|
ccc0b5412799
First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
30448
diff
changeset
|
407 effect of invoking this function. |
|
ccc0b5412799
First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
30448
diff
changeset
|
408 |
|
ccc0b5412799
First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
30448
diff
changeset
|
409 Non PEP 302 mechanisms (e.g. the Windows registry) used by the |
|
ccc0b5412799
First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
30448
diff
changeset
|
410 standard import machinery to find files in alternative locations |
|
ccc0b5412799
First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
30448
diff
changeset
|
411 are partially supported, but are searched AFTER sys.path. Normally, |
|
ccc0b5412799
First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
30448
diff
changeset
|
412 these locations are searched BEFORE sys.path, preventing sys.path |
|
ccc0b5412799
First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
30448
diff
changeset
|
413 entries from shadowing them. |
|
ccc0b5412799
First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
30448
diff
changeset
|
414 |
|
ccc0b5412799
First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
30448
diff
changeset
|
415 For this to cause a visible difference in behaviour, there must |
|
ccc0b5412799
First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
30448
diff
changeset
|
416 be a module or package name that is accessible via both sys.path |
|
ccc0b5412799
First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
30448
diff
changeset
|
417 and one of the non PEP 302 file system mechanisms. In this case, |
|
ccc0b5412799
First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
30448
diff
changeset
|
418 the emulation will find the former version, while the builtin |
|
ccc0b5412799
First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
30448
diff
changeset
|
419 import mechanism will find the latter. |
|
ccc0b5412799
First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
30448
diff
changeset
|
420 |
|
ccc0b5412799
First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
30448
diff
changeset
|
421 Items of the following types can be affected by this discrepancy: |
|
ccc0b5412799
First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
30448
diff
changeset
|
422 imp.C_EXTENSION, imp.PY_SOURCE, imp.PY_COMPILED, imp.PKG_DIRECTORY |
|
ccc0b5412799
First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
30448
diff
changeset
|
423 """ |
|
ccc0b5412799
First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
30448
diff
changeset
|
424 if fullname.startswith('.'): |
|
ccc0b5412799
First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
30448
diff
changeset
|
425 raise ImportError("Relative module names not supported") |
|
ccc0b5412799
First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
30448
diff
changeset
|
426 if '.' in fullname: |
|
ccc0b5412799
First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
30448
diff
changeset
|
427 # Get the containing package's __path__ |
|
ccc0b5412799
First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
30448
diff
changeset
|
428 pkg = '.'.join(fullname.split('.')[:-1]) |
|
ccc0b5412799
First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
30448
diff
changeset
|
429 if pkg not in sys.modules: |
|
ccc0b5412799
First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
30448
diff
changeset
|
430 __import__(pkg) |
|
38020
bb6c5c200a3f
Add some whitespace to be more consistent.
Neal Norwitz <nnorwitz@gmail.com>
parents:
37844
diff
changeset
|
431 path = getattr(sys.modules[pkg], '__path__', None) or [] |
|
37808
ccc0b5412799
First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
30448
diff
changeset
|
432 else: |
|
ccc0b5412799
First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
30448
diff
changeset
|
433 for importer in sys.meta_path: |
|
ccc0b5412799
First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
30448
diff
changeset
|
434 yield importer |
|
ccc0b5412799
First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
30448
diff
changeset
|
435 path = sys.path |
|
ccc0b5412799
First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
30448
diff
changeset
|
436 for item in path: |
|
ccc0b5412799
First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
30448
diff
changeset
|
437 yield get_importer(item) |
|
ccc0b5412799
First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
30448
diff
changeset
|
438 if '.' not in fullname: |
|
ccc0b5412799
First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
30448
diff
changeset
|
439 yield ImpImporter() |
|
ccc0b5412799
First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
30448
diff
changeset
|
440 |
|
ccc0b5412799
First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
30448
diff
changeset
|
441 def get_loader(module_or_name): |
|
ccc0b5412799
First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
30448
diff
changeset
|
442 """Get a PEP 302 "loader" object for module_or_name |
|
ccc0b5412799
First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
30448
diff
changeset
|
443 |
|
ccc0b5412799
First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
30448
diff
changeset
|
444 If the module or package is accessible via the normal import |
|
ccc0b5412799
First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
30448
diff
changeset
|
445 mechanism, a wrapper around the relevant part of that machinery |
|
ccc0b5412799
First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
30448
diff
changeset
|
446 is returned. Returns None if the module cannot be found or imported. |
|
ccc0b5412799
First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
30448
diff
changeset
|
447 If the named module is not already imported, its containing package |
|
ccc0b5412799
First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
30448
diff
changeset
|
448 (if any) is imported, in order to establish the package __path__. |
|
ccc0b5412799
First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
30448
diff
changeset
|
449 |
|
ccc0b5412799
First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
30448
diff
changeset
|
450 This function uses iter_importers(), and is thus subject to the same |
|
ccc0b5412799
First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
30448
diff
changeset
|
451 limitations regarding platform-specific special import locations such |
|
ccc0b5412799
First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
30448
diff
changeset
|
452 as the Windows registry. |
|
ccc0b5412799
First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
30448
diff
changeset
|
453 """ |
|
ccc0b5412799
First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
30448
diff
changeset
|
454 if module_or_name in sys.modules: |
|
ccc0b5412799
First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
30448
diff
changeset
|
455 module_or_name = sys.modules[module_or_name] |
|
ccc0b5412799
First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
30448
diff
changeset
|
456 if isinstance(module_or_name, ModuleType): |
|
ccc0b5412799
First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
30448
diff
changeset
|
457 module = module_or_name |
|
38020
bb6c5c200a3f
Add some whitespace to be more consistent.
Neal Norwitz <nnorwitz@gmail.com>
parents:
37844
diff
changeset
|
458 loader = getattr(module, '__loader__', None) |
|
37808
ccc0b5412799
First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
30448
diff
changeset
|
459 if loader is not None: |
|
ccc0b5412799
First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
30448
diff
changeset
|
460 return loader |
|
ccc0b5412799
First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
30448
diff
changeset
|
461 fullname = module.__name__ |
|
ccc0b5412799
First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
30448
diff
changeset
|
462 else: |
|
ccc0b5412799
First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
30448
diff
changeset
|
463 fullname = module_or_name |
|
ccc0b5412799
First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
30448
diff
changeset
|
464 return find_loader(fullname) |
|
ccc0b5412799
First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
30448
diff
changeset
|
465 |
|
ccc0b5412799
First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
30448
diff
changeset
|
466 def find_loader(fullname): |
|
ccc0b5412799
First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
30448
diff
changeset
|
467 """Find a PEP 302 "loader" object for fullname |
|
ccc0b5412799
First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
30448
diff
changeset
|
468 |
|
ccc0b5412799
First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
30448
diff
changeset
|
469 If fullname contains dots, path must be the containing package's __path__. |
|
ccc0b5412799
First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
30448
diff
changeset
|
470 Returns None if the module cannot be found or imported. This function uses |
|
ccc0b5412799
First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
30448
diff
changeset
|
471 iter_importers(), and is thus subject to the same limitations regarding |
|
ccc0b5412799
First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
30448
diff
changeset
|
472 platform-specific special import locations such as the Windows registry. |
|
ccc0b5412799
First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
30448
diff
changeset
|
473 """ |
|
ccc0b5412799
First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
30448
diff
changeset
|
474 for importer in iter_importers(fullname): |
|
ccc0b5412799
First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
30448
diff
changeset
|
475 loader = importer.find_module(fullname) |
|
ccc0b5412799
First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
30448
diff
changeset
|
476 if loader is not None: |
|
ccc0b5412799
First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
30448
diff
changeset
|
477 return loader |
|
ccc0b5412799
First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
30448
diff
changeset
|
478 |
|
ccc0b5412799
First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
30448
diff
changeset
|
479 return None |
|
ccc0b5412799
First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents:
30448
diff
changeset
|
480 |
|
26753
7c15bf7726b2
Last week we discussed adding this module to the standard library.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
481 |
|
7c15bf7726b2
Last week we discussed adding this module to the standard library.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
482 def extend_path(path, name): |
|
7c15bf7726b2
Last week we discussed adding this module to the standard library.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
483 """Extend a package's path. |
|
7c15bf7726b2
Last week we discussed adding this module to the standard library.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
484 |
|
7c15bf7726b2
Last week we discussed adding this module to the standard library.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
485 Intended use is to place the following code in a package's __init__.py: |
|
7c15bf7726b2
Last week we discussed adding this module to the standard library.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
486 |
|
7c15bf7726b2
Last week we discussed adding this module to the standard library.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
487 from pkgutil import extend_path |
|
7c15bf7726b2
Last week we discussed adding this module to the standard library.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
488 __path__ = extend_path(__path__, __name__) |
|
7c15bf7726b2
Last week we discussed adding this module to the standard library.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
489 |
|
7c15bf7726b2
Last week we discussed adding this module to the standard library.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
490 This will add to the package's __path__ all subdirectories of |
|
7c15bf7726b2
Last week we discussed adding this module to the standard library.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
491 directories on sys.path named after the package. This is useful |
|
7c15bf7726b2
Last week we discussed adding this module to the standard library.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
492 if one wants to distribute different parts of a single logical |
|
7c15bf7726b2
Last week we discussed adding this module to the standard library.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
493 package as multiple directories. |
|
7c15bf7726b2
Last week we discussed adding this module to the standard library.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
494 |
|
7c15bf7726b2
Last week we discussed adding this module to the standard library.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
495 It also looks for *.pkg files beginning where * matches the name |
|
7c15bf7726b2
Last week we discussed adding this module to the standard library.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
496 argument. This feature is similar to *.pth files (see site.py), |
|
7c15bf7726b2
Last week we discussed adding this module to the standard library.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
497 except that it doesn't special-case lines starting with 'import'. |
|
7c15bf7726b2
Last week we discussed adding this module to the standard library.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
498 A *.pkg file is trusted at face value: apart from checking for |
|
7c15bf7726b2
Last week we discussed adding this module to the standard library.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
499 duplicates, all entries found in a *.pkg file are added to the |
|
7c15bf7726b2
Last week we discussed adding this module to the standard library.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
500 path, regardless of whether they are exist the filesystem. (This |
|
7c15bf7726b2
Last week we discussed adding this module to the standard library.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
501 is a feature.) |
|
7c15bf7726b2
Last week we discussed adding this module to the standard library.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
502 |
|
7c15bf7726b2
Last week we discussed adding this module to the standard library.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
503 If the input path is not a list (as is the case for frozen |
|
7c15bf7726b2
Last week we discussed adding this module to the standard library.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
504 packages) it is returned unchanged. The input path is not |
|
7c15bf7726b2
Last week we discussed adding this module to the standard library.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
505 modified; an extended copy is returned. Items are only appended |
|
7c15bf7726b2
Last week we discussed adding this module to the standard library.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
506 to the copy at the end. |
|
7c15bf7726b2
Last week we discussed adding this module to the standard library.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
507 |
|
7c15bf7726b2
Last week we discussed adding this module to the standard library.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
508 It is assumed that sys.path is a sequence. Items of sys.path that |
|
7c15bf7726b2
Last week we discussed adding this module to the standard library.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
509 are not (unicode or 8-bit) strings referring to existing |
|
7c15bf7726b2
Last week we discussed adding this module to the standard library.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
510 directories are ignored. Unicode items of sys.path that cause |
|
7c15bf7726b2
Last week we discussed adding this module to the standard library.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
511 errors when used as filenames may cause this function to raise an |
|
7c15bf7726b2
Last week we discussed adding this module to the standard library.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
512 exception (in line with os.path.isdir() behavior). |
|
7c15bf7726b2
Last week we discussed adding this module to the standard library.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
513 """ |
|
7c15bf7726b2
Last week we discussed adding this module to the standard library.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
514 |
|
7c15bf7726b2
Last week we discussed adding this module to the standard library.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
515 if not isinstance(path, list): |
|
7c15bf7726b2
Last week we discussed adding this module to the standard library.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
516 # This could happen e.g. when this is called from inside a |
|
7c15bf7726b2
Last week we discussed adding this module to the standard library.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
517 # frozen package. Return the path unchanged in that case. |
|
7c15bf7726b2
Last week we discussed adding this module to the standard library.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
518 return path |
|
7c15bf7726b2
Last week we discussed adding this module to the standard library.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
519 |
|
7c15bf7726b2
Last week we discussed adding this module to the standard library.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
520 pname = os.path.join(*name.split('.')) # Reconstitute as relative path |
|
7c15bf7726b2
Last week we discussed adding this module to the standard library.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
521 # Just in case os.extsep != '.' |
|
7c15bf7726b2
Last week we discussed adding this module to the standard library.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
522 sname = os.extsep.join(name.split('.')) |
|
7c15bf7726b2
Last week we discussed adding this module to the standard library.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
523 sname_pkg = sname + os.extsep + "pkg" |
|
7c15bf7726b2
Last week we discussed adding this module to the standard library.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
524 init_py = "__init__" + os.extsep + "py" |
|
7c15bf7726b2
Last week we discussed adding this module to the standard library.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
525 |
|
7c15bf7726b2
Last week we discussed adding this module to the standard library.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
526 path = path[:] # Start with a copy of the existing path |
|
7c15bf7726b2
Last week we discussed adding this module to the standard library.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
527 |
|
7c15bf7726b2
Last week we discussed adding this module to the standard library.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
528 for dir in sys.path: |
|
30448
7cb6745be525
SF patch #806246: use basestring where possible
Raymond Hettinger <python@rcn.com>
parents:
26753
diff
changeset
|
529 if not isinstance(dir, basestring) or not os.path.isdir(dir): |
|
26753
7c15bf7726b2
Last week we discussed adding this module to the standard library.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
530 continue |
|
7c15bf7726b2
Last week we discussed adding this module to the standard library.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
531 subdir = os.path.join(dir, pname) |
|
7c15bf7726b2
Last week we discussed adding this module to the standard library.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
532 # XXX This may still add duplicate entries to path on |
|
7c15bf7726b2
Last week we discussed adding this module to the standard library.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
533 # case-insensitive filesystems |
|
7c15bf7726b2
Last week we discussed adding this module to the standard library.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
534 initfile = os.path.join(subdir, init_py) |
|
7c15bf7726b2
Last week we discussed adding this module to the standard library.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
535 if subdir not in path and os.path.isfile(initfile): |
|
7c15bf7726b2
Last week we discussed adding this module to the standard library.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
536 path.append(subdir) |
|
7c15bf7726b2
Last week we discussed adding this module to the standard library.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
537 # XXX Is this the right thing for subpackages like zope.app? |
|
7c15bf7726b2
Last week we discussed adding this module to the standard library.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
538 # It looks for a file named "zope.app.pkg" |
|
7c15bf7726b2
Last week we discussed adding this module to the standard library.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
539 pkgfile = os.path.join(dir, sname_pkg) |
|
7c15bf7726b2
Last week we discussed adding this module to the standard library.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
540 if os.path.isfile(pkgfile): |
|
7c15bf7726b2
Last week we discussed adding this module to the standard library.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
541 try: |
|
7c15bf7726b2
Last week we discussed adding this module to the standard library.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
542 f = open(pkgfile) |
|
7c15bf7726b2
Last week we discussed adding this module to the standard library.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
543 except IOError, msg: |
|
7c15bf7726b2
Last week we discussed adding this module to the standard library.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
544 sys.stderr.write("Can't open %s: %s\n" % |
|
7c15bf7726b2
Last week we discussed adding this module to the standard library.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
545 (pkgfile, msg)) |
|
7c15bf7726b2
Last week we discussed adding this module to the standard library.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
546 else: |
|
7c15bf7726b2
Last week we discussed adding this module to the standard library.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
547 for line in f: |
|
7c15bf7726b2
Last week we discussed adding this module to the standard library.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
548 line = line.rstrip('\n') |
|
7c15bf7726b2
Last week we discussed adding this module to the standard library.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
549 if not line or line.startswith('#'): |
|
7c15bf7726b2
Last week we discussed adding this module to the standard library.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
550 continue |
|
7c15bf7726b2
Last week we discussed adding this module to the standard library.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
551 path.append(line) # Don't check for existence! |
|
7c15bf7726b2
Last week we discussed adding this module to the standard library.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
552 f.close() |
|
7c15bf7726b2
Last week we discussed adding this module to the standard library.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
553 |
|
7c15bf7726b2
Last week we discussed adding this module to the standard library.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
554 return path |
|
46454
1841f92e30de
Issue 2439: add pkgutils.get_data() as a convenience wrapper for the PEP 302 get_data() API (contributed by Paul Moore)
Nick Coghlan <ncoghlan@gmail.com>
parents:
39253
diff
changeset
|
555 |
|
1841f92e30de
Issue 2439: add pkgutils.get_data() as a convenience wrapper for the PEP 302 get_data() API (contributed by Paul Moore)
Nick Coghlan <ncoghlan@gmail.com>
parents:
39253
diff
changeset
|
556 def get_data(package, resource): |
|
1841f92e30de
Issue 2439: add pkgutils.get_data() as a convenience wrapper for the PEP 302 get_data() API (contributed by Paul Moore)
Nick Coghlan <ncoghlan@gmail.com>
parents:
39253
diff
changeset
|
557 """Get a resource from a package. |
|
1841f92e30de
Issue 2439: add pkgutils.get_data() as a convenience wrapper for the PEP 302 get_data() API (contributed by Paul Moore)
Nick Coghlan <ncoghlan@gmail.com>
parents:
39253
diff
changeset
|
558 |
|
1841f92e30de
Issue 2439: add pkgutils.get_data() as a convenience wrapper for the PEP 302 get_data() API (contributed by Paul Moore)
Nick Coghlan <ncoghlan@gmail.com>
parents:
39253
diff
changeset
|
559 This is a wrapper round the PEP 302 loader get_data API. The package |
|
1841f92e30de
Issue 2439: add pkgutils.get_data() as a convenience wrapper for the PEP 302 get_data() API (contributed by Paul Moore)
Nick Coghlan <ncoghlan@gmail.com>
parents:
39253
diff
changeset
|
560 argument should be the name of a package, in standard module format |
|
1841f92e30de
Issue 2439: add pkgutils.get_data() as a convenience wrapper for the PEP 302 get_data() API (contributed by Paul Moore)
Nick Coghlan <ncoghlan@gmail.com>
parents:
39253
diff
changeset
|
561 (foo.bar). The resource argument should be in the form of a relative |
|
1841f92e30de
Issue 2439: add pkgutils.get_data() as a convenience wrapper for the PEP 302 get_data() API (contributed by Paul Moore)
Nick Coghlan <ncoghlan@gmail.com>
parents:
39253
diff
changeset
|
562 filename, using '/' as the path separator. The parent directory name '..' |
|
1841f92e30de
Issue 2439: add pkgutils.get_data() as a convenience wrapper for the PEP 302 get_data() API (contributed by Paul Moore)
Nick Coghlan <ncoghlan@gmail.com>
parents:
39253
diff
changeset
|
563 is not allowed, and nor is a rooted name (starting with a '/'). |
|
1841f92e30de
Issue 2439: add pkgutils.get_data() as a convenience wrapper for the PEP 302 get_data() API (contributed by Paul Moore)
Nick Coghlan <ncoghlan@gmail.com>
parents:
39253
diff
changeset
|
564 |
|
1841f92e30de
Issue 2439: add pkgutils.get_data() as a convenience wrapper for the PEP 302 get_data() API (contributed by Paul Moore)
Nick Coghlan <ncoghlan@gmail.com>
parents:
39253
diff
changeset
|
565 The function returns a binary string, which is the contents of the |
|
1841f92e30de
Issue 2439: add pkgutils.get_data() as a convenience wrapper for the PEP 302 get_data() API (contributed by Paul Moore)
Nick Coghlan <ncoghlan@gmail.com>
parents:
39253
diff
changeset
|
566 specified resource. |
|
1841f92e30de
Issue 2439: add pkgutils.get_data() as a convenience wrapper for the PEP 302 get_data() API (contributed by Paul Moore)
Nick Coghlan <ncoghlan@gmail.com>
parents:
39253
diff
changeset
|
567 |
|
1841f92e30de
Issue 2439: add pkgutils.get_data() as a convenience wrapper for the PEP 302 get_data() API (contributed by Paul Moore)
Nick Coghlan <ncoghlan@gmail.com>
parents:
39253
diff
changeset
|
568 For packages located in the filesystem, which have already been imported, |
|
1841f92e30de
Issue 2439: add pkgutils.get_data() as a convenience wrapper for the PEP 302 get_data() API (contributed by Paul Moore)
Nick Coghlan <ncoghlan@gmail.com>
parents:
39253
diff
changeset
|
569 this is the rough equivalent of |
|
1841f92e30de
Issue 2439: add pkgutils.get_data() as a convenience wrapper for the PEP 302 get_data() API (contributed by Paul Moore)
Nick Coghlan <ncoghlan@gmail.com>
parents:
39253
diff
changeset
|
570 |
|
1841f92e30de
Issue 2439: add pkgutils.get_data() as a convenience wrapper for the PEP 302 get_data() API (contributed by Paul Moore)
Nick Coghlan <ncoghlan@gmail.com>
parents:
39253
diff
changeset
|
571 d = os.path.dirname(sys.modules[package].__file__) |
|
1841f92e30de
Issue 2439: add pkgutils.get_data() as a convenience wrapper for the PEP 302 get_data() API (contributed by Paul Moore)
Nick Coghlan <ncoghlan@gmail.com>
parents:
39253
diff
changeset
|
572 data = open(os.path.join(d, resource), 'rb').read() |
|
1841f92e30de
Issue 2439: add pkgutils.get_data() as a convenience wrapper for the PEP 302 get_data() API (contributed by Paul Moore)
Nick Coghlan <ncoghlan@gmail.com>
parents:
39253
diff
changeset
|
573 |
|
1841f92e30de
Issue 2439: add pkgutils.get_data() as a convenience wrapper for the PEP 302 get_data() API (contributed by Paul Moore)
Nick Coghlan <ncoghlan@gmail.com>
parents:
39253
diff
changeset
|
574 If the package cannot be located or loaded, or it uses a PEP 302 loader |
|
1841f92e30de
Issue 2439: add pkgutils.get_data() as a convenience wrapper for the PEP 302 get_data() API (contributed by Paul Moore)
Nick Coghlan <ncoghlan@gmail.com>
parents:
39253
diff
changeset
|
575 which does not support get_data(), then None is returned. |
|
1841f92e30de
Issue 2439: add pkgutils.get_data() as a convenience wrapper for the PEP 302 get_data() API (contributed by Paul Moore)
Nick Coghlan <ncoghlan@gmail.com>
parents:
39253
diff
changeset
|
576 """ |
|
1841f92e30de
Issue 2439: add pkgutils.get_data() as a convenience wrapper for the PEP 302 get_data() API (contributed by Paul Moore)
Nick Coghlan <ncoghlan@gmail.com>
parents:
39253
diff
changeset
|
577 |
|
1841f92e30de
Issue 2439: add pkgutils.get_data() as a convenience wrapper for the PEP 302 get_data() API (contributed by Paul Moore)
Nick Coghlan <ncoghlan@gmail.com>
parents:
39253
diff
changeset
|
578 loader = get_loader(package) |
|
1841f92e30de
Issue 2439: add pkgutils.get_data() as a convenience wrapper for the PEP 302 get_data() API (contributed by Paul Moore)
Nick Coghlan <ncoghlan@gmail.com>
parents:
39253
diff
changeset
|
579 if loader is None or not hasattr(loader, 'get_data'): |
|
1841f92e30de
Issue 2439: add pkgutils.get_data() as a convenience wrapper for the PEP 302 get_data() API (contributed by Paul Moore)
Nick Coghlan <ncoghlan@gmail.com>
parents:
39253
diff
changeset
|
580 return None |
|
1841f92e30de
Issue 2439: add pkgutils.get_data() as a convenience wrapper for the PEP 302 get_data() API (contributed by Paul Moore)
Nick Coghlan <ncoghlan@gmail.com>
parents:
39253
diff
changeset
|
581 mod = sys.modules.get(package) or loader.load_module(package) |
|
1841f92e30de
Issue 2439: add pkgutils.get_data() as a convenience wrapper for the PEP 302 get_data() API (contributed by Paul Moore)
Nick Coghlan <ncoghlan@gmail.com>
parents:
39253
diff
changeset
|
582 if mod is None or not hasattr(mod, '__file__'): |
|
1841f92e30de
Issue 2439: add pkgutils.get_data() as a convenience wrapper for the PEP 302 get_data() API (contributed by Paul Moore)
Nick Coghlan <ncoghlan@gmail.com>
parents:
39253
diff
changeset
|
583 return None |
|
1841f92e30de
Issue 2439: add pkgutils.get_data() as a convenience wrapper for the PEP 302 get_data() API (contributed by Paul Moore)
Nick Coghlan <ncoghlan@gmail.com>
parents:
39253
diff
changeset
|
584 |
|
1841f92e30de
Issue 2439: add pkgutils.get_data() as a convenience wrapper for the PEP 302 get_data() API (contributed by Paul Moore)
Nick Coghlan <ncoghlan@gmail.com>
parents:
39253
diff
changeset
|
585 # Modify the resource name to be compatible with the loader.get_data |
|
1841f92e30de
Issue 2439: add pkgutils.get_data() as a convenience wrapper for the PEP 302 get_data() API (contributed by Paul Moore)
Nick Coghlan <ncoghlan@gmail.com>
parents:
39253
diff
changeset
|
586 # signature - an os.path format "filename" starting with the dirname of |
|
1841f92e30de
Issue 2439: add pkgutils.get_data() as a convenience wrapper for the PEP 302 get_data() API (contributed by Paul Moore)
Nick Coghlan <ncoghlan@gmail.com>
parents:
39253
diff
changeset
|
587 # the package's __file__ |
|
1841f92e30de
Issue 2439: add pkgutils.get_data() as a convenience wrapper for the PEP 302 get_data() API (contributed by Paul Moore)
Nick Coghlan <ncoghlan@gmail.com>
parents:
39253
diff
changeset
|
588 parts = resource.split('/') |
|
1841f92e30de
Issue 2439: add pkgutils.get_data() as a convenience wrapper for the PEP 302 get_data() API (contributed by Paul Moore)
Nick Coghlan <ncoghlan@gmail.com>
parents:
39253
diff
changeset
|
589 parts.insert(0, os.path.dirname(mod.__file__)) |
|
1841f92e30de
Issue 2439: add pkgutils.get_data() as a convenience wrapper for the PEP 302 get_data() API (contributed by Paul Moore)
Nick Coghlan <ncoghlan@gmail.com>
parents:
39253
diff
changeset
|
590 resource_name = os.path.join(*parts) |
|
1841f92e30de
Issue 2439: add pkgutils.get_data() as a convenience wrapper for the PEP 302 get_data() API (contributed by Paul Moore)
Nick Coghlan <ncoghlan@gmail.com>
parents:
39253
diff
changeset
|
591 return loader.get_data(resource_name) |
