annotate Lib/pkgutil.py @ 39042:c02fa187e0f4 legacy-trunk

Part of SF patch #1484695. This removes dead code. The chksum was already verified in .frombuf() on the lines above. If there was a problem an exception is raised, so there was no way this condition could have been true.
author Neal Norwitz <nnorwitz@gmail.com>
date Mon, 10 Jul 2006 00:23:17 +0000
parents 501ee2a05fec
children 6621edfd296a
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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',
37821
3135648026c4 Second phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents: 37808
diff changeset
14 'walk_packages', 'iter_modules',
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):
3135648026c4 Second phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents: 37808
diff changeset
72 """Yield submodule names+loaders recursively, for path or sys.path"""
3135648026c4 Second phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents: 37808
diff changeset
73
38020
bb6c5c200a3f Add some whitespace to be more consistent.
Neal Norwitz <nnorwitz@gmail.com>
parents: 37844
diff changeset
74 def seen(p, m={}):
bb6c5c200a3f Add some whitespace to be more consistent.
Neal Norwitz <nnorwitz@gmail.com>
parents: 37844
diff changeset
75 if p in m:
bb6c5c200a3f Add some whitespace to be more consistent.
Neal Norwitz <nnorwitz@gmail.com>
parents: 37844
diff changeset
76 return True
37821
3135648026c4 Second phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents: 37808
diff changeset
77 m[p] = True
3135648026c4 Second phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents: 37808
diff changeset
78
3135648026c4 Second phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents: 37808
diff changeset
79 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
80 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
81
3135648026c4 Second phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents: 37808
diff changeset
82 if ispkg:
3135648026c4 Second phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents: 37808
diff changeset
83 try:
3135648026c4 Second phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents: 37808
diff changeset
84 __import__(name)
3135648026c4 Second phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents: 37808
diff changeset
85 except ImportError:
3135648026c4 Second phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents: 37808
diff changeset
86 if onerror is not None:
3135648026c4 Second phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents: 37808
diff changeset
87 onerror()
3135648026c4 Second phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents: 37808
diff changeset
88 else:
3135648026c4 Second phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents: 37808
diff changeset
89 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
90
3135648026c4 Second phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents: 37808
diff changeset
91 # 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
92 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
93
3135648026c4 Second phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents: 37808
diff changeset
94 for item in walk_packages(path, name+'.'):
3135648026c4 Second phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents: 37808
diff changeset
95 yield item
3135648026c4 Second phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents: 37808
diff changeset
96
3135648026c4 Second phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents: 37808
diff changeset
97
3135648026c4 Second phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents: 37808
diff changeset
98 def iter_modules(path=None, prefix=''):
3135648026c4 Second phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents: 37808
diff changeset
99 """Yield submodule names+loaders for path or sys.path"""
3135648026c4 Second phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents: 37808
diff changeset
100 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
101 importers = iter_importers()
3135648026c4 Second phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents: 37808
diff changeset
102 else:
3135648026c4 Second phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents: 37808
diff changeset
103 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
104
3135648026c4 Second phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents: 37808
diff changeset
105 yielded = {}
3135648026c4 Second phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents: 37808
diff changeset
106 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
107 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
108 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
109 yielded[name] = 1
3135648026c4 Second phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents: 37808
diff changeset
110 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
111
3135648026c4 Second phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents: 37808
diff changeset
112
3135648026c4 Second phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents: 37808
diff changeset
113 #@simplegeneric
3135648026c4 Second phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents: 37808
diff changeset
114 def iter_importer_modules(importer, prefix=''):
38020
bb6c5c200a3f Add some whitespace to be more consistent.
Neal Norwitz <nnorwitz@gmail.com>
parents: 37844
diff changeset
115 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
116 return []
3135648026c4 Second phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents: 37808
diff changeset
117 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
118
3135648026c4 Second phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents: 37808
diff changeset
119 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
120
3135648026c4 Second phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents: 37808
diff changeset
121
37808
ccc0b5412799 First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents: 30448
diff changeset
122 class ImpImporter:
ccc0b5412799 First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents: 30448
diff changeset
123 """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
124
ccc0b5412799 First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents: 30448
diff changeset
125 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
126 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
127 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
128
ccc0b5412799 First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents: 30448
diff changeset
129 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
130 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
131 """
ccc0b5412799 First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents: 30448
diff changeset
132
ccc0b5412799 First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents: 30448
diff changeset
133 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
134 self.path = path
ccc0b5412799 First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents: 30448
diff changeset
135
ccc0b5412799 First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents: 30448
diff changeset
136 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
137 # 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
138 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
139 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
140 return None
ccc0b5412799 First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents: 30448
diff changeset
141 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
142 path = None
ccc0b5412799 First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents: 30448
diff changeset
143 else:
37821
3135648026c4 Second phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents: 37808
diff changeset
144 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
145 try:
ccc0b5412799 First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents: 30448
diff changeset
146 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
147 except ImportError:
ccc0b5412799 First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents: 30448
diff changeset
148 return None
ccc0b5412799 First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents: 30448
diff changeset
149 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
150
37821
3135648026c4 Second phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents: 37808
diff changeset
151 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
152 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
153 return
3135648026c4 Second phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents: 37808
diff changeset
154
3135648026c4 Second phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents: 37808
diff changeset
155 yielded = {}
3135648026c4 Second phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents: 37808
diff changeset
156 import inspect
3135648026c4 Second phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents: 37808
diff changeset
157
3135648026c4 Second phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents: 37808
diff changeset
158 filenames = os.listdir(self.path)
3135648026c4 Second phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents: 37808
diff changeset
159 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
160
3135648026c4 Second phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents: 37808
diff changeset
161 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
162 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
163 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
164 continue
37844
8725b907acbb Whilespace normalization (reindint.py).
Tim Peters <tim.peters@gmail.com>
parents: 37821
diff changeset
165
37821
3135648026c4 Second phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents: 37808
diff changeset
166 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
167 ispkg = False
3135648026c4 Second phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents: 37808
diff changeset
168
3135648026c4 Second phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents: 37808
diff changeset
169 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
170 modname = fn
3135648026c4 Second phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents: 37808
diff changeset
171 for fn in os.listdir(path):
3135648026c4 Second phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents: 37808
diff changeset
172 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
173 if subname=='__init__':
3135648026c4 Second phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents: 37808
diff changeset
174 ispkg = True
3135648026c4 Second phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents: 37808
diff changeset
175 break
3135648026c4 Second phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents: 37808
diff changeset
176 else:
3135648026c4 Second phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents: 37808
diff changeset
177 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
178
3135648026c4 Second phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents: 37808
diff changeset
179 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
180 yielded[modname] = 1
3135648026c4 Second phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents: 37808
diff changeset
181 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
182
37808
ccc0b5412799 First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents: 30448
diff changeset
183
ccc0b5412799 First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents: 30448
diff changeset
184 class ImpLoader:
ccc0b5412799 First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents: 30448
diff changeset
185 """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
186 """
ccc0b5412799 First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents: 30448
diff changeset
187 code = source = None
ccc0b5412799 First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents: 30448
diff changeset
188
ccc0b5412799 First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents: 30448
diff changeset
189 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
190 self.file = file
ccc0b5412799 First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents: 30448
diff changeset
191 self.filename = filename
ccc0b5412799 First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents: 30448
diff changeset
192 self.fullname = fullname
ccc0b5412799 First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents: 30448
diff changeset
193 self.etc = etc
ccc0b5412799 First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents: 30448
diff changeset
194
ccc0b5412799 First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents: 30448
diff changeset
195 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
196 self._reopen()
ccc0b5412799 First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents: 30448
diff changeset
197 try:
ccc0b5412799 First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents: 30448
diff changeset
198 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
199 finally:
ccc0b5412799 First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents: 30448
diff changeset
200 if self.file:
ccc0b5412799 First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents: 30448
diff changeset
201 self.file.close()
ccc0b5412799 First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents: 30448
diff changeset
202 # 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
203 # 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
204 return mod
ccc0b5412799 First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents: 30448
diff changeset
205
ccc0b5412799 First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents: 30448
diff changeset
206 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
207 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
208
ccc0b5412799 First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents: 30448
diff changeset
209 def _reopen(self):
ccc0b5412799 First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents: 30448
diff changeset
210 if self.file and self.file.closed:
38740
501ee2a05fec Fix errors found by pychecker
Neal Norwitz <nnorwitz@gmail.com>
parents: 38388
diff changeset
211 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
212 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
213 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
214 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
215 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
216
ccc0b5412799 First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents: 30448
diff changeset
217 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
218 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
219 fullname = self.fullname
ccc0b5412799 First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents: 30448
diff changeset
220 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
221 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
222 "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
223 return fullname
ccc0b5412799 First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents: 30448
diff changeset
224
37821
3135648026c4 Second phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents: 37808
diff changeset
225 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
226 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
227 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
228
ccc0b5412799 First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents: 30448
diff changeset
229 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
230 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
231 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
232 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
233 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
234 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
235 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
236 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
237 self._reopen()
ccc0b5412799 First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents: 30448
diff changeset
238 try:
ccc0b5412799 First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents: 30448
diff changeset
239 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
240 finally:
ccc0b5412799 First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents: 30448
diff changeset
241 self.file.close()
ccc0b5412799 First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents: 30448
diff changeset
242 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
243 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
244 return self.code
ccc0b5412799 First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents: 30448
diff changeset
245
ccc0b5412799 First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents: 30448
diff changeset
246 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
247 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
248 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
249 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
250 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
251 self._reopen()
ccc0b5412799 First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents: 30448
diff changeset
252 try:
ccc0b5412799 First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents: 30448
diff changeset
253 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
254 finally:
ccc0b5412799 First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents: 30448
diff changeset
255 self.file.close()
ccc0b5412799 First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents: 30448
diff changeset
256 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
257 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
258 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
259 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
260 f.close()
ccc0b5412799 First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents: 30448
diff changeset
261 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
262 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
263 return self.source
ccc0b5412799 First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents: 30448
diff changeset
264
37821
3135648026c4 Second phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents: 37808
diff changeset
265
37808
ccc0b5412799 First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents: 30448
diff changeset
266 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
267 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
268
ccc0b5412799 First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents: 30448
diff changeset
269 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
270 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
271 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
272 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
273 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
274 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
275 return self.filename
ccc0b5412799 First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents: 30448
diff changeset
276 return None
ccc0b5412799 First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents: 30448
diff changeset
277
ccc0b5412799 First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents: 30448
diff changeset
278
37821
3135648026c4 Second phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents: 37808
diff changeset
279 try:
3135648026c4 Second phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents: 37808
diff changeset
280 import zipimport
3135648026c4 Second phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents: 37808
diff changeset
281 from zipimport import zipimporter
37844
8725b907acbb Whilespace normalization (reindint.py).
Tim Peters <tim.peters@gmail.com>
parents: 37821
diff changeset
282
37821
3135648026c4 Second phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents: 37808
diff changeset
283 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
284 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
285 dirlist.sort()
3135648026c4 Second phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents: 37808
diff changeset
286 _prefix = importer.prefix
3135648026c4 Second phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents: 37808
diff changeset
287 plen = len(_prefix)
3135648026c4 Second phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents: 37808
diff changeset
288 yielded = {}
3135648026c4 Second phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents: 37808
diff changeset
289 import inspect
3135648026c4 Second phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents: 37808
diff changeset
290 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
291 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
292 continue
3135648026c4 Second phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents: 37808
diff changeset
293
3135648026c4 Second phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents: 37808
diff changeset
294 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
295
3135648026c4 Second phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents: 37808
diff changeset
296 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
297 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
298 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
299 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
300
3135648026c4 Second phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents: 37808
diff changeset
301 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
302 continue
3135648026c4 Second phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents: 37808
diff changeset
303
3135648026c4 Second phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents: 37808
diff changeset
304 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
305 if modname=='__init__':
3135648026c4 Second phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents: 37808
diff changeset
306 continue
3135648026c4 Second phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents: 37808
diff changeset
307
3135648026c4 Second phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents: 37808
diff changeset
308 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
309 yielded[modname] = 1
3135648026c4 Second phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents: 37808
diff changeset
310 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
311
3135648026c4 Second phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents: 37808
diff changeset
312 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
313
3135648026c4 Second phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents: 37808
diff changeset
314 except ImportError:
3135648026c4 Second phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents: 37808
diff changeset
315 pass
3135648026c4 Second phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents: 37808
diff changeset
316
3135648026c4 Second phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents: 37808
diff changeset
317
37808
ccc0b5412799 First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents: 30448
diff changeset
318 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
319 """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
320
ccc0b5412799 First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents: 30448
diff changeset
321 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
322 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
323
ccc0b5412799 First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents: 30448
diff changeset
324 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
325 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
326 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
327
ccc0b5412799 First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents: 30448
diff changeset
328 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
329 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
330 """
ccc0b5412799 First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents: 30448
diff changeset
331 try:
ccc0b5412799 First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents: 30448
diff changeset
332 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
333 except KeyError:
ccc0b5412799 First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents: 30448
diff changeset
334 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
335 try:
ccc0b5412799 First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents: 30448
diff changeset
336 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
337 break
ccc0b5412799 First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents: 30448
diff changeset
338 except ImportError:
ccc0b5412799 First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents: 30448
diff changeset
339 pass
ccc0b5412799 First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents: 30448
diff changeset
340 else:
ccc0b5412799 First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents: 30448
diff changeset
341 importer = None
38020
bb6c5c200a3f Add some whitespace to be more consistent.
Neal Norwitz <nnorwitz@gmail.com>
parents: 37844
diff changeset
342 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
343
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
344 # The boolean values are used for caching valid and invalid
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
345 # file paths for the built-in import machinery
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
346 if importer in (None, True, False):
37808
ccc0b5412799 First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents: 30448
diff changeset
347 try:
ccc0b5412799 First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents: 30448
diff changeset
348 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
349 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
350 importer = None
37808
ccc0b5412799 First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents: 30448
diff changeset
351 return importer
ccc0b5412799 First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents: 30448
diff changeset
352
ccc0b5412799 First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents: 30448
diff changeset
353
37821
3135648026c4 Second phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents: 37808
diff changeset
354 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
355 """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
356
ccc0b5412799 First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents: 30448
diff changeset
357 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
358 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
359 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
360 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
361 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
362
ccc0b5412799 First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents: 30448
diff changeset
363 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
364 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
365 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
366 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
367 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
368
ccc0b5412799 First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents: 30448
diff changeset
369 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
370 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
371 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
372 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
373 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
374
ccc0b5412799 First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents: 30448
diff changeset
375 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
376 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
377 """
ccc0b5412799 First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents: 30448
diff changeset
378 if fullname.startswith('.'):
ccc0b5412799 First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents: 30448
diff changeset
379 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
380 if '.' in fullname:
ccc0b5412799 First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents: 30448
diff changeset
381 # 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
382 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
383 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
384 __import__(pkg)
38020
bb6c5c200a3f Add some whitespace to be more consistent.
Neal Norwitz <nnorwitz@gmail.com>
parents: 37844
diff changeset
385 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
386 else:
ccc0b5412799 First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents: 30448
diff changeset
387 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
388 yield importer
ccc0b5412799 First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents: 30448
diff changeset
389 path = sys.path
ccc0b5412799 First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents: 30448
diff changeset
390 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
391 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
392 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
393 yield ImpImporter()
ccc0b5412799 First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents: 30448
diff changeset
394
ccc0b5412799 First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents: 30448
diff changeset
395 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
396 """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
397
ccc0b5412799 First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents: 30448
diff changeset
398 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
399 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
400 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
401 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
402 (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
403
ccc0b5412799 First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents: 30448
diff changeset
404 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
405 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
406 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
407 """
ccc0b5412799 First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents: 30448
diff changeset
408 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
409 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
410 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
411 module = module_or_name
38020
bb6c5c200a3f Add some whitespace to be more consistent.
Neal Norwitz <nnorwitz@gmail.com>
parents: 37844
diff changeset
412 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
413 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
414 return loader
ccc0b5412799 First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents: 30448
diff changeset
415 fullname = module.__name__
ccc0b5412799 First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents: 30448
diff changeset
416 else:
ccc0b5412799 First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents: 30448
diff changeset
417 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
418 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
419
ccc0b5412799 First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents: 30448
diff changeset
420 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
421 """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
422
ccc0b5412799 First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents: 30448
diff changeset
423 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
424 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
425 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
426 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
427 """
ccc0b5412799 First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents: 30448
diff changeset
428 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
429 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
430 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
431 return loader
ccc0b5412799 First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents: 30448
diff changeset
432
ccc0b5412799 First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents: 30448
diff changeset
433 return None
ccc0b5412799 First phase of refactoring for runpy, pkgutil, pydoc, and setuptools
Phillip J. Eby <pje@telecommunity.com>
parents: 30448
diff changeset
434
26753
7c15bf7726b2 Last week we discussed adding this module to the standard library.
Guido van Rossum <guido@python.org>
parents:
diff changeset
435
7c15bf7726b2 Last week we discussed adding this module to the standard library.
Guido van Rossum <guido@python.org>
parents:
diff changeset
436 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
437 """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
438
7c15bf7726b2 Last week we discussed adding this module to the standard library.
Guido van Rossum <guido@python.org>
parents:
diff changeset
439 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
440
7c15bf7726b2 Last week we discussed adding this module to the standard library.
Guido van Rossum <guido@python.org>
parents:
diff changeset
441 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
442 __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
443
7c15bf7726b2 Last week we discussed adding this module to the standard library.
Guido van Rossum <guido@python.org>
parents:
diff changeset
444 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
445 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
446 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
447 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
448
7c15bf7726b2 Last week we discussed adding this module to the standard library.
Guido van Rossum <guido@python.org>
parents:
diff changeset
449 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
450 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
451 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
452 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
453 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
454 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
455 is a feature.)
7c15bf7726b2 Last week we discussed adding this module to the standard library.
Guido van Rossum <guido@python.org>
parents:
diff changeset
456
7c15bf7726b2 Last week we discussed adding this module to the standard library.
Guido van Rossum <guido@python.org>
parents:
diff changeset
457 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
458 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
459 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
460 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
461
7c15bf7726b2 Last week we discussed adding this module to the standard library.
Guido van Rossum <guido@python.org>
parents:
diff changeset
462 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
463 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
464 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
465 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
466 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
467 """
7c15bf7726b2 Last week we discussed adding this module to the standard library.
Guido van Rossum <guido@python.org>
parents:
diff changeset
468
7c15bf7726b2 Last week we discussed adding this module to the standard library.
Guido van Rossum <guido@python.org>
parents:
diff changeset
469 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
470 # 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
471 # 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
472 return path
7c15bf7726b2 Last week we discussed adding this module to the standard library.
Guido van Rossum <guido@python.org>
parents:
diff changeset
473
7c15bf7726b2 Last week we discussed adding this module to the standard library.
Guido van Rossum <guido@python.org>
parents:
diff changeset
474 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
475 # 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
476 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
477 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
478 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
479
7c15bf7726b2 Last week we discussed adding this module to the standard library.
Guido van Rossum <guido@python.org>
parents:
diff changeset
480 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
481
7c15bf7726b2 Last week we discussed adding this module to the standard library.
Guido van Rossum <guido@python.org>
parents:
diff changeset
482 for dir in sys.path:
30448
7cb6745be525 SF patch #806246: use basestring where possible
Raymond Hettinger <python@rcn.com>
parents: 26753
diff changeset
483 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
484 continue
7c15bf7726b2 Last week we discussed adding this module to the standard library.
Guido van Rossum <guido@python.org>
parents:
diff changeset
485 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
486 # 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
487 # case-insensitive filesystems
7c15bf7726b2 Last week we discussed adding this module to the standard library.
Guido van Rossum <guido@python.org>
parents:
diff changeset
488 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
489 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
490 path.append(subdir)
7c15bf7726b2 Last week we discussed adding this module to the standard library.
Guido van Rossum <guido@python.org>
parents:
diff changeset
491 # 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
492 # 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
493 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
494 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
495 try:
7c15bf7726b2 Last week we discussed adding this module to the standard library.
Guido van Rossum <guido@python.org>
parents:
diff changeset
496 f = open(pkgfile)
7c15bf7726b2 Last week we discussed adding this module to the standard library.
Guido van Rossum <guido@python.org>
parents:
diff changeset
497 except IOError, msg:
7c15bf7726b2 Last week we discussed adding this module to the standard library.
Guido van Rossum <guido@python.org>
parents:
diff changeset
498 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
499 (pkgfile, msg))
7c15bf7726b2 Last week we discussed adding this module to the standard library.
Guido van Rossum <guido@python.org>
parents:
diff changeset
500 else:
7c15bf7726b2 Last week we discussed adding this module to the standard library.
Guido van Rossum <guido@python.org>
parents:
diff changeset
501 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
502 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
503 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
504 continue
7c15bf7726b2 Last week we discussed adding this module to the standard library.
Guido van Rossum <guido@python.org>
parents:
diff changeset
505 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
506 f.close()
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 return path