annotate Lib/pkgutil.py @ 51749:1408feec0084 3.0

Merged revisions 69798 via svnmerge from svn+ssh://pythondev@svn.python.org/python/branches/py3k ................ r69798 | benjamin.peterson | 2009-02-19 22:02:38 -0600 (Thu, 19 Feb 2009) | 13 lines Merged revisions 69769,69776 via svnmerge from svn+ssh://pythondev@svn.python.org/python/trunk ........ r69769 | georg.brandl | 2009-02-19 02:30:06 -0600 (Thu, 19 Feb 2009) | 1 line #5310, #3558: fix operator precedence table. ........ r69776 | georg.brandl | 2009-02-19 10:34:51 -0600 (Thu, 19 Feb 2009) | 2 lines #5317: update IronPython URL. ........ ................
author Benjamin Peterson <benjamin@python.org>
date Fri, 20 Feb 2009 04:05:32 +0000
parents beb7abcd6c01
children 627db59031be
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
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
3 # NOTE: This module must remain compatible with Python 2.3, as it is shared
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
4 # by setuptools for distribution with Python 2.3 and up.
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
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
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
8 import imp
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
9 import os.path
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
10 from types import ModuleType
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
11
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
12 __all__ = [
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
13 'get_importer', 'iter_importers', 'get_loader', 'find_loader',
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
14 'walk_packages', 'iter_modules',
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
15 'ImpImporter', 'ImpLoader', 'read_code', 'extend_path',
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
16 ]
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
17
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
18 def read_code(stream):
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
19 # This helper is needed in order for the PEP 302 emulation to
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
20 # correctly handle compiled files
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
21 import marshal
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
22
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
23 magic = stream.read(4)
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
24 if magic != imp.get_magic():
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
25 return None
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
26
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
27 stream.read(4) # Skip timestamp
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
28 return marshal.load(stream)
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
29
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
30
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
31 def simplegeneric(func):
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
32 """Make a trivial single-dispatch generic function"""
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
33 registry = {}
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
34 def wrapper(*args, **kw):
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
35 ob = args[0]
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
36 try:
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
37 cls = ob.__class__
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
38 except AttributeError:
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
39 cls = type(ob)
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
40 try:
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
41 mro = cls.__mro__
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
42 except AttributeError:
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
43 try:
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
44 class cls(cls, object):
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
45 pass
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
46 mro = cls.__mro__[1:]
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
47 except TypeError:
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
48 mro = object, # must be an ExtensionClass or some such :(
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
49 for t in mro:
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
50 if t in registry:
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
51 return registry[t](*args, **kw)
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
52 else:
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
53 return func(*args, **kw)
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
54 try:
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
55 wrapper.__name__ = func.__name__
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
56 except (TypeError, AttributeError):
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
57 pass # Python 2.3 doesn't allow functions to be renamed
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
58
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
59 def register(typ, func=None):
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
60 if func is None:
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
61 return lambda f: register(typ, f)
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
62 registry[typ] = func
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
63 return func
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
64
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
65 wrapper.__dict__ = func.__dict__
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
66 wrapper.__doc__ = func.__doc__
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
67 wrapper.register = register
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
68 return wrapper
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
69
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
70
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
71 def walk_packages(path=None, prefix='', onerror=None):
39442
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
72 """Yields (module_loader, name, ispkg) for all modules recursively
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
73 on path, or, if path is None, all accessible modules.
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
74
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
75 'path' should be either None or a list of paths to look for
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
76 modules in.
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
77
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
78 'prefix' is a string to output on the front of every module name
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
79 on output.
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
80
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
81 Note that this function must import all *packages* (NOT all
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
82 modules!) on the given path, in order to access the __path__
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
83 attribute to find submodules.
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
84
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
85 'onerror' is a function which gets called with one argument (the
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
86 name of the package which was being imported) if any exception
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
87 occurs while trying to import a package. If no onerror function is
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
88 supplied, ImportErrors are caught and ignored, while all other
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
89 exceptions are propagated, terminating the search.
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
90
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
91 Examples:
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
92
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
93 # list all modules python can access
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
94 walk_packages()
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
95
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
96 # list all submodules of ctypes
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
97 walk_packages(ctypes.__path__, ctypes.__name__+'.')
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
98 """
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
99
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
100 def seen(p, m={}):
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
101 if p in m:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
102 return True
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
103 m[p] = True
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
104
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
105 for importer, name, ispkg in iter_modules(path, prefix):
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
106 yield importer, name, ispkg
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
107
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
108 if ispkg:
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
109 try:
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
110 __import__(name)
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
111 except ImportError:
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
112 if onerror is not None:
39442
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
113 onerror(name)
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
114 except Exception:
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
115 if onerror is not None:
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
116 onerror(name)
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
117 else:
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
118 raise
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
119 else:
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
120 path = getattr(sys.modules[name], '__path__', None) or []
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
121
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
122 # don't traverse path items we've seen before
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
123 path = [p for p in path if not seen(p)]
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
124
39442
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
125 for item in walk_packages(path, name+'.', onerror):
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
126 yield item
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
127
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
128
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
129 def iter_modules(path=None, prefix=''):
39442
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
130 """Yields (module_loader, name, ispkg) for all submodules on path,
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
131 or, if path is None, all top-level modules on sys.path.
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
132
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
133 'path' should be either None or a list of paths to look for
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
134 modules in.
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
135
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
136 'prefix' is a string to output on the front of every module name
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
137 on output.
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
138 """
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
139
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
140 if path is None:
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
141 importers = iter_importers()
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
142 else:
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
143 importers = map(get_importer, path)
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
144
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
145 yielded = {}
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
146 for i in importers:
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
147 for name, ispkg in iter_importer_modules(i, prefix):
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
148 if name not in yielded:
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
149 yielded[name] = 1
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
150 yield i, name, ispkg
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
151
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
152
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
153 #@simplegeneric
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
154 def iter_importer_modules(importer, prefix=''):
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
155 if not hasattr(importer, 'iter_modules'):
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
156 return []
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
157 return importer.iter_modules(prefix)
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
158
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
159 iter_importer_modules = simplegeneric(iter_importer_modules)
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
160
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
161
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
162 class ImpImporter:
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
163 """PEP 302 Importer that wraps Python's "classic" import algorithm
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
164
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
165 ImpImporter(dirname) produces a PEP 302 importer that searches that
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
166 directory. ImpImporter(None) produces a PEP 302 importer that searches
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
167 the current sys.path, plus any modules that are frozen or built-in.
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
168
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
169 Note that ImpImporter does not currently support being used by placement
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
170 on sys.meta_path.
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
171 """
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
172
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
173 def __init__(self, path=None):
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
174 self.path = path
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
175
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
176 def find_module(self, fullname, path=None):
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
177 # Note: we ignore 'path' argument since it is only used via meta_path
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
178 subname = fullname.split(".")[-1]
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
179 if subname != fullname and self.path is None:
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
180 return None
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
181 if self.path is None:
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
182 path = None
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
183 else:
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
184 path = [os.path.realpath(self.path)]
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
185 try:
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
186 file, filename, etc = imp.find_module(subname, path)
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
187 except ImportError:
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
188 return None
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
189 return ImpLoader(fullname, file, filename, etc)
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
190
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
191 def iter_modules(self, prefix=''):
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
192 if self.path is None or not os.path.isdir(self.path):
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
193 return
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
194
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
195 yielded = {}
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
196 import inspect
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
197
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
198 filenames = os.listdir(self.path)
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
199 filenames.sort() # handle packages before same-named modules
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
200
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
201 for fn in filenames:
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
202 modname = inspect.getmodulename(fn)
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
203 if modname=='__init__' or modname in yielded:
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
204 continue
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
205
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
206 path = os.path.join(self.path, fn)
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
207 ispkg = False
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
208
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
209 if not modname and os.path.isdir(path) and '.' not in fn:
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
210 modname = fn
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
211 for fn in os.listdir(path):
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
212 subname = inspect.getmodulename(fn)
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
213 if subname=='__init__':
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
214 ispkg = True
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
215 break
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
216 else:
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
217 continue # not a package
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
218
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
219 if modname and '.' not in modname:
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
220 yielded[modname] = 1
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
221 yield prefix + modname, ispkg
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
222
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
223
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
224 class ImpLoader:
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
225 """PEP 302 Loader that wraps Python's "classic" import algorithm
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
226 """
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
227 code = source = None
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
228
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
229 def __init__(self, fullname, file, filename, etc):
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
230 self.file = file
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
231 self.filename = filename
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
232 self.fullname = fullname
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
233 self.etc = etc
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
234
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
235 def load_module(self, fullname):
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
236 self._reopen()
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
237 try:
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
238 mod = imp.load_module(fullname, self.file, self.filename, self.etc)
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
239 finally:
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
240 if self.file:
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
241 self.file.close()
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
242 # Note: we don't set __loader__ because we want the module to look
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
243 # normal; i.e. this is just a wrapper for standard import machinery
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
244 return mod
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
245
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
246 def get_data(self, pathname):
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
247 return open(pathname, "rb").read()
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
248
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
249 def _reopen(self):
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
250 if self.file and self.file.closed:
39442
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
251 mod_type = self.etc[2]
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
252 if mod_type==imp.PY_SOURCE:
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
253 self.file = open(self.filename, 'rU')
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
254 elif mod_type in (imp.PY_COMPILED, imp.C_EXTENSION):
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
255 self.file = open(self.filename, 'rb')
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
256
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
257 def _fix_name(self, fullname):
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
258 if fullname is None:
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
259 fullname = self.fullname
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
260 elif fullname != self.fullname:
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
261 raise ImportError("Loader for module %s cannot handle "
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
262 "module %s" % (self.fullname, fullname))
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
263 return fullname
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
264
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
265 def is_package(self, fullname):
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
266 fullname = self._fix_name(fullname)
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
267 return self.etc[2]==imp.PKG_DIRECTORY
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
268
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
269 def get_code(self, fullname=None):
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
270 fullname = self._fix_name(fullname)
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
271 if self.code is None:
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
272 mod_type = self.etc[2]
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
273 if mod_type==imp.PY_SOURCE:
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
274 source = self.get_source(fullname)
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
275 self.code = compile(source, self.filename, 'exec')
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
276 elif mod_type==imp.PY_COMPILED:
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
277 self._reopen()
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
278 try:
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
279 self.code = read_code(self.file)
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
280 finally:
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
281 self.file.close()
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
282 elif mod_type==imp.PKG_DIRECTORY:
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
283 self.code = self._get_delegate().get_code()
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
284 return self.code
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
285
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
286 def get_source(self, fullname=None):
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
287 fullname = self._fix_name(fullname)
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
288 if self.source is None:
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
289 mod_type = self.etc[2]
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
290 if mod_type==imp.PY_SOURCE:
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
291 self._reopen()
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
292 try:
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
293 self.source = self.file.read()
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
294 finally:
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
295 self.file.close()
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
296 elif mod_type==imp.PY_COMPILED:
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
297 if os.path.exists(self.filename[:-1]):
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
298 f = open(self.filename[:-1], 'rU')
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
299 self.source = f.read()
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
300 f.close()
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
301 elif mod_type==imp.PKG_DIRECTORY:
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
302 self.source = self._get_delegate().get_source()
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
303 return self.source
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
304
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
305
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
306 def _get_delegate(self):
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
307 return ImpImporter(self.filename).find_module('__init__')
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
308
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
309 def get_filename(self, fullname=None):
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
310 fullname = self._fix_name(fullname)
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
311 mod_type = self.etc[2]
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
312 if self.etc[2]==imp.PKG_DIRECTORY:
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
313 return self._get_delegate().get_filename()
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
314 elif self.etc[2] in (imp.PY_SOURCE, imp.PY_COMPILED, imp.C_EXTENSION):
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
315 return self.filename
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
316 return None
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
317
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
318
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
319 try:
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
320 import zipimport
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
321 from zipimport import zipimporter
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
322
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
323 def iter_zipimport_modules(importer, prefix=''):
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
324 dirlist = zipimport._zip_directory_cache[importer.archive].keys()
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
325 dirlist.sort()
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
326 _prefix = importer.prefix
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
327 plen = len(_prefix)
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
328 yielded = {}
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
329 import inspect
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
330 for fn in dirlist:
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
331 if not fn.startswith(_prefix):
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
332 continue
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
333
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
334 fn = fn[plen:].split(os.sep)
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
335
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
336 if len(fn)==2 and fn[1].startswith('__init__.py'):
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
337 if fn[0] not in yielded:
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
338 yielded[fn[0]] = 1
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
339 yield fn[0], True
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
340
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
341 if len(fn)!=1:
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
342 continue
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
343
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
344 modname = inspect.getmodulename(fn[0])
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
345 if modname=='__init__':
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
346 continue
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
347
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
348 if modname and '.' not in modname and modname not in yielded:
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
349 yielded[modname] = 1
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
350 yield prefix + modname, False
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
351
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
352 iter_importer_modules.register(zipimporter, iter_zipimport_modules)
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
353
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
354 except ImportError:
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
355 pass
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
356
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
357
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
358 def get_importer(path_item):
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
359 """Retrieve a PEP 302 importer for the given path item
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
360
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
361 The returned importer is cached in sys.path_importer_cache
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
362 if it was newly created by a path hook.
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
363
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
364 If there is no importer, a wrapper around the basic import
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
365 machinery is returned. This wrapper is never inserted into
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
366 the importer cache (None is inserted instead).
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
367
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
368 The cache (or part of it) can be cleared manually if a
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
369 rescan of sys.path_hooks is necessary.
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
370 """
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
371 try:
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
372 importer = sys.path_importer_cache[path_item]
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
373 except KeyError:
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
374 for path_hook in sys.path_hooks:
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
375 try:
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
376 importer = path_hook(path_item)
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
377 break
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
378 except ImportError:
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
379 pass
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
380 else:
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
381 importer = None
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
382 sys.path_importer_cache.setdefault(path_item, importer)
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
383
39442
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
384 if importer is None:
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
385 try:
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
386 importer = ImpImporter(path_item)
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
387 except ImportError:
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
388 importer = None
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
389 return importer
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
390
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
391
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
392 def iter_importers(fullname=""):
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
393 """Yield PEP 302 importers for the given module name
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
394
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
395 If fullname contains a '.', the importers will be for the package
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
396 containing fullname, otherwise they will be importers for sys.meta_path,
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
397 sys.path, and Python's "classic" import machinery, in that order. If
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
398 the named module is in a package, that package is imported as a side
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
399 effect of invoking this function.
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
400
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
401 Non PEP 302 mechanisms (e.g. the Windows registry) used by the
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
402 standard import machinery to find files in alternative locations
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
403 are partially supported, but are searched AFTER sys.path. Normally,
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
404 these locations are searched BEFORE sys.path, preventing sys.path
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
405 entries from shadowing them.
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
406
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
407 For this to cause a visible difference in behaviour, there must
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
408 be a module or package name that is accessible via both sys.path
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
409 and one of the non PEP 302 file system mechanisms. In this case,
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
410 the emulation will find the former version, while the builtin
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
411 import mechanism will find the latter.
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
412
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
413 Items of the following types can be affected by this discrepancy:
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
414 imp.C_EXTENSION, imp.PY_SOURCE, imp.PY_COMPILED, imp.PKG_DIRECTORY
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
415 """
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
416 if fullname.startswith('.'):
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
417 raise ImportError("Relative module names not supported")
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
418 if '.' in fullname:
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
419 # Get the containing package's __path__
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
420 pkg = '.'.join(fullname.split('.')[:-1])
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
421 if pkg not in sys.modules:
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
422 __import__(pkg)
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
423 path = getattr(sys.modules[pkg], '__path__', None) or []
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
424 else:
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
425 for importer in sys.meta_path:
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
426 yield importer
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
427 path = sys.path
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
428 for item in path:
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
429 yield get_importer(item)
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
430 if '.' not in fullname:
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
431 yield ImpImporter()
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
432
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
433 def get_loader(module_or_name):
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
434 """Get a PEP 302 "loader" object for module_or_name
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
435
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
436 If the module or package is accessible via the normal import
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
437 mechanism, a wrapper around the relevant part of that machinery
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
438 is returned. Returns None if the module cannot be found or imported.
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
439 If the named module is not already imported, its containing package
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
440 (if any) is imported, in order to establish the package __path__.
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
441
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
442 This function uses iter_importers(), and is thus subject to the same
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
443 limitations regarding platform-specific special import locations such
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
444 as the Windows registry.
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
445 """
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
446 if module_or_name in sys.modules:
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
447 module_or_name = sys.modules[module_or_name]
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
448 if isinstance(module_or_name, ModuleType):
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
449 module = module_or_name
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
450 loader = getattr(module, '__loader__', None)
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
451 if loader is not None:
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
452 return loader
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
453 fullname = module.__name__
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
454 else:
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
455 fullname = module_or_name
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
456 return find_loader(fullname)
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
457
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
458 def find_loader(fullname):
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
459 """Find a PEP 302 "loader" object for fullname
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
460
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
461 If fullname contains dots, path must be the containing package's __path__.
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
462 Returns None if the module cannot be found or imported. This function uses
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
463 iter_importers(), and is thus subject to the same limitations regarding
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
464 platform-specific special import locations such as the Windows registry.
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
465 """
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
466 for importer in iter_importers(fullname):
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
467 loader = importer.find_module(fullname)
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
468 if loader is not None:
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
469 return loader
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
470
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
471 return None
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 30448
diff changeset
472
26753
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 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
475 """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
476
7c15bf7726b2 Last week we discussed adding this module to the standard library.
Guido van Rossum <guido@python.org>
parents:
diff changeset
477 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
478
7c15bf7726b2 Last week we discussed adding this module to the standard library.
Guido van Rossum <guido@python.org>
parents:
diff changeset
479 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
480 __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
481
7c15bf7726b2 Last week we discussed adding this module to the standard library.
Guido van Rossum <guido@python.org>
parents:
diff changeset
482 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
483 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
484 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
485 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
486
7c15bf7726b2 Last week we discussed adding this module to the standard library.
Guido van Rossum <guido@python.org>
parents:
diff changeset
487 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
488 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
489 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
490 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
491 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
492 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
493 is a feature.)
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 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
496 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
497 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
498 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
499
7c15bf7726b2 Last week we discussed adding this module to the standard library.
Guido van Rossum <guido@python.org>
parents:
diff changeset
500 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
501 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
502 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
503 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
504 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
505 """
7c15bf7726b2 Last week we discussed adding this module to the standard library.
Guido van Rossum <guido@python.org>
parents:
diff changeset
506
7c15bf7726b2 Last week we discussed adding this module to the standard library.
Guido van Rossum <guido@python.org>
parents:
diff changeset
507 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
508 # 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
509 # 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
510 return path
7c15bf7726b2 Last week we discussed adding this module to the standard library.
Guido van Rossum <guido@python.org>
parents:
diff changeset
511
7c15bf7726b2 Last week we discussed adding this module to the standard library.
Guido van Rossum <guido@python.org>
parents:
diff changeset
512 pname = os.path.join(*name.split('.')) # Reconstitute as relative path
42622
4d2aa4bfb2d6 Remove RISCOS support
Skip Montanaro <skip@pobox.com>
parents: 40587
diff changeset
513 sname_pkg = name + ".pkg"
4d2aa4bfb2d6 Remove RISCOS support
Skip Montanaro <skip@pobox.com>
parents: 40587
diff changeset
514 init_py = "__init__.py"
26753
7c15bf7726b2 Last week we discussed adding this module to the standard library.
Guido van Rossum <guido@python.org>
parents:
diff changeset
515
7c15bf7726b2 Last week we discussed adding this module to the standard library.
Guido van Rossum <guido@python.org>
parents:
diff changeset
516 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
517
7c15bf7726b2 Last week we discussed adding this module to the standard library.
Guido van Rossum <guido@python.org>
parents:
diff changeset
518 for dir in sys.path:
43577
ace45b361e87 Patch# 1258 by Christian Heimes: kill basestring.
Guido van Rossum <guido@python.org>
parents: 42622
diff changeset
519 if not isinstance(dir, str) 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
520 continue
7c15bf7726b2 Last week we discussed adding this module to the standard library.
Guido van Rossum <guido@python.org>
parents:
diff changeset
521 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
522 # 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
523 # case-insensitive filesystems
7c15bf7726b2 Last week we discussed adding this module to the standard library.
Guido van Rossum <guido@python.org>
parents:
diff changeset
524 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
525 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
526 path.append(subdir)
7c15bf7726b2 Last week we discussed adding this module to the standard library.
Guido van Rossum <guido@python.org>
parents:
diff changeset
527 # 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
528 # 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
529 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
530 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
531 try:
7c15bf7726b2 Last week we discussed adding this module to the standard library.
Guido van Rossum <guido@python.org>
parents:
diff changeset
532 f = open(pkgfile)
40587
6b18a095c2e7 SF patch 1631942 by Collin Winter:
Guido van Rossum <guido@python.org>
parents: 39442
diff changeset
533 except IOError as msg:
26753
7c15bf7726b2 Last week we discussed adding this module to the standard library.
Guido van Rossum <guido@python.org>
parents:
diff changeset
534 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
535 (pkgfile, msg))
7c15bf7726b2 Last week we discussed adding this module to the standard library.
Guido van Rossum <guido@python.org>
parents:
diff changeset
536 else:
7c15bf7726b2 Last week we discussed adding this module to the standard library.
Guido van Rossum <guido@python.org>
parents:
diff changeset
537 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
538 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
539 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
540 continue
7c15bf7726b2 Last week we discussed adding this module to the standard library.
Guido van Rossum <guido@python.org>
parents:
diff changeset
541 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
542 f.close()
7c15bf7726b2 Last week we discussed adding this module to the standard library.
Guido van Rossum <guido@python.org>
parents:
diff changeset
543
7c15bf7726b2 Last week we discussed adding this module to the standard library.
Guido van Rossum <guido@python.org>
parents:
diff changeset
544 return path
46478
beb7abcd6c01 Merged revisions 62350-62355,62358-62359,62364-62365,62370,62372-62375,62378-62379,62381 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 43577
diff changeset
545
beb7abcd6c01 Merged revisions 62350-62355,62358-62359,62364-62365,62370,62372-62375,62378-62379,62381 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 43577
diff changeset
546 def get_data(package, resource):
beb7abcd6c01 Merged revisions 62350-62355,62358-62359,62364-62365,62370,62372-62375,62378-62379,62381 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 43577
diff changeset
547 """Get a resource from a package.
beb7abcd6c01 Merged revisions 62350-62355,62358-62359,62364-62365,62370,62372-62375,62378-62379,62381 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 43577
diff changeset
548
beb7abcd6c01 Merged revisions 62350-62355,62358-62359,62364-62365,62370,62372-62375,62378-62379,62381 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 43577
diff changeset
549 This is a wrapper round the PEP 302 loader get_data API. The package
beb7abcd6c01 Merged revisions 62350-62355,62358-62359,62364-62365,62370,62372-62375,62378-62379,62381 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 43577
diff changeset
550 argument should be the name of a package, in standard module format
beb7abcd6c01 Merged revisions 62350-62355,62358-62359,62364-62365,62370,62372-62375,62378-62379,62381 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 43577
diff changeset
551 (foo.bar). The resource argument should be in the form of a relative
beb7abcd6c01 Merged revisions 62350-62355,62358-62359,62364-62365,62370,62372-62375,62378-62379,62381 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 43577
diff changeset
552 filename, using '/' as the path separator. The parent directory name '..'
beb7abcd6c01 Merged revisions 62350-62355,62358-62359,62364-62365,62370,62372-62375,62378-62379,62381 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 43577
diff changeset
553 is not allowed, and nor is a rooted name (starting with a '/').
beb7abcd6c01 Merged revisions 62350-62355,62358-62359,62364-62365,62370,62372-62375,62378-62379,62381 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 43577
diff changeset
554
beb7abcd6c01 Merged revisions 62350-62355,62358-62359,62364-62365,62370,62372-62375,62378-62379,62381 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 43577
diff changeset
555 The function returns a binary string, which is the contents of the
beb7abcd6c01 Merged revisions 62350-62355,62358-62359,62364-62365,62370,62372-62375,62378-62379,62381 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 43577
diff changeset
556 specified resource.
beb7abcd6c01 Merged revisions 62350-62355,62358-62359,62364-62365,62370,62372-62375,62378-62379,62381 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 43577
diff changeset
557
beb7abcd6c01 Merged revisions 62350-62355,62358-62359,62364-62365,62370,62372-62375,62378-62379,62381 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 43577
diff changeset
558 For packages located in the filesystem, which have already been imported,
beb7abcd6c01 Merged revisions 62350-62355,62358-62359,62364-62365,62370,62372-62375,62378-62379,62381 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 43577
diff changeset
559 this is the rough equivalent of
beb7abcd6c01 Merged revisions 62350-62355,62358-62359,62364-62365,62370,62372-62375,62378-62379,62381 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 43577
diff changeset
560
beb7abcd6c01 Merged revisions 62350-62355,62358-62359,62364-62365,62370,62372-62375,62378-62379,62381 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 43577
diff changeset
561 d = os.path.dirname(sys.modules[package].__file__)
beb7abcd6c01 Merged revisions 62350-62355,62358-62359,62364-62365,62370,62372-62375,62378-62379,62381 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 43577
diff changeset
562 data = open(os.path.join(d, resource), 'rb').read()
beb7abcd6c01 Merged revisions 62350-62355,62358-62359,62364-62365,62370,62372-62375,62378-62379,62381 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 43577
diff changeset
563
beb7abcd6c01 Merged revisions 62350-62355,62358-62359,62364-62365,62370,62372-62375,62378-62379,62381 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 43577
diff changeset
564 If the package cannot be located or loaded, or it uses a PEP 302 loader
beb7abcd6c01 Merged revisions 62350-62355,62358-62359,62364-62365,62370,62372-62375,62378-62379,62381 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 43577
diff changeset
565 which does not support get_data(), then None is returned.
beb7abcd6c01 Merged revisions 62350-62355,62358-62359,62364-62365,62370,62372-62375,62378-62379,62381 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 43577
diff changeset
566 """
beb7abcd6c01 Merged revisions 62350-62355,62358-62359,62364-62365,62370,62372-62375,62378-62379,62381 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 43577
diff changeset
567
beb7abcd6c01 Merged revisions 62350-62355,62358-62359,62364-62365,62370,62372-62375,62378-62379,62381 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 43577
diff changeset
568 loader = get_loader(package)
beb7abcd6c01 Merged revisions 62350-62355,62358-62359,62364-62365,62370,62372-62375,62378-62379,62381 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 43577
diff changeset
569 if loader is None or not hasattr(loader, 'get_data'):
beb7abcd6c01 Merged revisions 62350-62355,62358-62359,62364-62365,62370,62372-62375,62378-62379,62381 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 43577
diff changeset
570 return None
beb7abcd6c01 Merged revisions 62350-62355,62358-62359,62364-62365,62370,62372-62375,62378-62379,62381 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 43577
diff changeset
571 mod = sys.modules.get(package) or loader.load_module(package)
beb7abcd6c01 Merged revisions 62350-62355,62358-62359,62364-62365,62370,62372-62375,62378-62379,62381 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 43577
diff changeset
572 if mod is None or not hasattr(mod, '__file__'):
beb7abcd6c01 Merged revisions 62350-62355,62358-62359,62364-62365,62370,62372-62375,62378-62379,62381 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 43577
diff changeset
573 return None
beb7abcd6c01 Merged revisions 62350-62355,62358-62359,62364-62365,62370,62372-62375,62378-62379,62381 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 43577
diff changeset
574
beb7abcd6c01 Merged revisions 62350-62355,62358-62359,62364-62365,62370,62372-62375,62378-62379,62381 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 43577
diff changeset
575 # Modify the resource name to be compatible with the loader.get_data
beb7abcd6c01 Merged revisions 62350-62355,62358-62359,62364-62365,62370,62372-62375,62378-62379,62381 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 43577
diff changeset
576 # signature - an os.path format "filename" starting with the dirname of
beb7abcd6c01 Merged revisions 62350-62355,62358-62359,62364-62365,62370,62372-62375,62378-62379,62381 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 43577
diff changeset
577 # the package's __file__
beb7abcd6c01 Merged revisions 62350-62355,62358-62359,62364-62365,62370,62372-62375,62378-62379,62381 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 43577
diff changeset
578 parts = resource.split('/')
beb7abcd6c01 Merged revisions 62350-62355,62358-62359,62364-62365,62370,62372-62375,62378-62379,62381 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 43577
diff changeset
579 parts.insert(0, os.path.dirname(mod.__file__))
beb7abcd6c01 Merged revisions 62350-62355,62358-62359,62364-62365,62370,62372-62375,62378-62379,62381 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 43577
diff changeset
580 resource_name = os.path.join(*parts)
beb7abcd6c01 Merged revisions 62350-62355,62358-62359,62364-62365,62370,62372-62375,62378-62379,62381 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 43577
diff changeset
581 return loader.get_data(resource_name)