annotate Lib/ihooks.py @ 79675:c0a2294de787 2.7

Issue #16199: Backport 2to3 typo fix from 3.2.
author Chris Jerdonek <chris.jerdonek@gmail.com>
date Thu, 11 Oct 2012 16:15:46 -0700
parents 50b7f56814f9
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1 """Import hook support.
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3 Consistent use of this module will make it possible to change the
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
4 different mechanisms involved in loading modules independently.
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
5
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
6 While the built-in module imp exports interfaces to the built-in
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
7 module searching and loading algorithm, and it is possible to replace
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
8 the built-in function __import__ in order to change the semantics of
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
9 the import statement, until now it has been difficult to combine the
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
10 effect of different __import__ hacks, like loading modules from URLs
8586
bdfef8e73457 Support loading of packages. (I had this coded up for a while but
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
11 by rimport.py, or restricted execution by rexec.py.
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
12
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
13 This module defines three new concepts:
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
14
8586
bdfef8e73457 Support loading of packages. (I had this coded up for a while but
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
15 1) A "file system hooks" class provides an interface to a filesystem.
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
16
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
17 One hooks class is defined (Hooks), which uses the interface provided
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
18 by standard modules os and os.path. It should be used as the base
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
19 class for other hooks classes.
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
20
30723
6f15e73b59f2 Backport checkin:
Walter Dörwald <walter@livinglogic.de>
parents: 28089
diff changeset
21 2) A "module loader" class provides an interface to search for a
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
22 module in a search path and to load it. It defines a method which
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
23 searches for a module in a single directory; by overriding this method
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
24 one can redefine the details of the search. If the directory is None,
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
25 built-in and frozen modules are searched instead.
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
26
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
27 Two module loader class are defined, both implementing the search
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
28 strategy used by the built-in __import__ function: ModuleLoader uses
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
29 the imp module's find_module interface, while HookableModuleLoader
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
30 uses a file system hooks class to interact with the file system. Both
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
31 use the imp module's load_* interfaces to actually load the module.
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
32
8586
bdfef8e73457 Support loading of packages. (I had this coded up for a while but
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
33 3) A "module importer" class provides an interface to import a
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
34 module, as well as interfaces to reload and unload a module. It also
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
35 provides interfaces to install and uninstall itself instead of the
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
36 default __import__ and reload (and unload) functions.
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
37
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
38 One module importer class is defined (ModuleImporter), which uses a
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
39 module loader instance passed in (by default HookableModuleLoader is
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
40 instantiated).
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
41
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
42 The classes defined here should be used as base classes for extended
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
43 functionality along those lines.
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
44
16160
46920b60cd33 added a few more __all__ lists
Skip Montanaro <skip@pobox.com>
parents: 15868
diff changeset
45 If a module importer class supports dotted names, its import_module()
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
46 must return a different value depending on whether it is called on
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
47 behalf of a "from ... import ..." statement or not. (This is caused
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
48 by the way the __import__ hook is used by the Python interpreter.) It
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
49 would also do wise to install a different version of reload().
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
50
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
51 """
56531
50b7f56814f9 Add support to the ihooks module for relative imports.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 46849
diff changeset
52 from warnings import warnpy3k, warn
46849
f2a63ec5055e Deprecate ihooks for removal in 3.0.
Brett Cannon <bcannon@gmail.com>
parents: 32946
diff changeset
53 warnpy3k("the ihooks module has been removed in Python 3.0", stacklevel=2)
f2a63ec5055e Deprecate ihooks for removal in 3.0.
Brett Cannon <bcannon@gmail.com>
parents: 32946
diff changeset
54 del warnpy3k
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
55
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
56 import __builtin__
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
57 import imp
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
58 import os
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
59 import sys
8586
bdfef8e73457 Support loading of packages. (I had this coded up for a while but
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
60
16160
46920b60cd33 added a few more __all__ lists
Skip Montanaro <skip@pobox.com>
parents: 15868
diff changeset
61 __all__ = ["BasicModuleLoader","Hooks","ModuleLoader","FancyModuleLoader",
46920b60cd33 added a few more __all__ lists
Skip Montanaro <skip@pobox.com>
parents: 15868
diff changeset
62 "BasicModuleImporter","ModuleImporter","install","uninstall"]
8586
bdfef8e73457 Support loading of packages. (I had this coded up for a while but
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
63
bdfef8e73457 Support loading of packages. (I had this coded up for a while but
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
64 VERBOSE = 0
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
65
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
66
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
67 from imp import C_EXTENSION, PY_SOURCE, PY_COMPILED
8586
bdfef8e73457 Support loading of packages. (I had this coded up for a while but
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
68 from imp import C_BUILTIN, PY_FROZEN, PKG_DIRECTORY
bdfef8e73457 Support loading of packages. (I had this coded up for a while but
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
69 BUILTIN_MODULE = C_BUILTIN
bdfef8e73457 Support loading of packages. (I had this coded up for a while but
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
70 FROZEN_MODULE = PY_FROZEN
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
71
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
72
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
73 class _Verbose:
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
74
8586
bdfef8e73457 Support loading of packages. (I had this coded up for a while but
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
75 def __init__(self, verbose = VERBOSE):
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5995
diff changeset
76 self.verbose = verbose
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
77
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
78 def get_verbose(self):
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5995
diff changeset
79 return self.verbose
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
80
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
81 def set_verbose(self, verbose):
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5995
diff changeset
82 self.verbose = verbose
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
83
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
84 # XXX The following is an experimental interface
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
85
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
86 def note(self, *args):
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5995
diff changeset
87 if self.verbose:
28089
14f65d661084 Get rid of many apply() calls.
Guido van Rossum <guido@python.org>
parents: 26668
diff changeset
88 self.message(*args)
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
89
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
90 def message(self, format, *args):
8586
bdfef8e73457 Support loading of packages. (I had this coded up for a while but
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
91 if args:
bdfef8e73457 Support loading of packages. (I had this coded up for a while but
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
92 print format%args
bdfef8e73457 Support loading of packages. (I had this coded up for a while but
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
93 else:
bdfef8e73457 Support loading of packages. (I had this coded up for a while but
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
94 print format
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
95
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
96
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
97 class BasicModuleLoader(_Verbose):
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
98
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
99 """Basic module loader.
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
100
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
101 This provides the same functionality as built-in import. It
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
102 doesn't deal with checking sys.modules -- all it provides is
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
103 find_module() and a load_module(), as well as find_module_in_dir()
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
104 which searches just one directory, and can be overridden by a
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
105 derived class to change the module search algorithm when the basic
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
106 dependency on sys.path is unchanged.
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
107
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
108 The interface is a little more convenient than imp's:
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
109 find_module(name, [path]) returns None or 'stuff', and
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
110 load_module(name, stuff) loads the module.
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
111
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
112 """
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
113
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
114 def find_module(self, name, path = None):
15868
3060cbfa01be Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 12392
diff changeset
115 if path is None:
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5995
diff changeset
116 path = [None] + self.default_path()
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5995
diff changeset
117 for dir in path:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5995
diff changeset
118 stuff = self.find_module_in_dir(name, dir)
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5995
diff changeset
119 if stuff: return stuff
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5995
diff changeset
120 return None
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
121
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
122 def default_path(self):
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5995
diff changeset
123 return sys.path
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
124
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
125 def find_module_in_dir(self, name, dir):
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5995
diff changeset
126 if dir is None:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5995
diff changeset
127 return self.find_builtin_module(name)
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5995
diff changeset
128 else:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5995
diff changeset
129 try:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5995
diff changeset
130 return imp.find_module(name, [dir])
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5995
diff changeset
131 except ImportError:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5995
diff changeset
132 return None
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
133
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
134 def find_builtin_module(self, name):
8586
bdfef8e73457 Support loading of packages. (I had this coded up for a while but
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
135 # XXX frozen packages?
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5995
diff changeset
136 if imp.is_builtin(name):
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5995
diff changeset
137 return None, '', ('', '', BUILTIN_MODULE)
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5995
diff changeset
138 if imp.is_frozen(name):
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5995
diff changeset
139 return None, '', ('', '', FROZEN_MODULE)
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5995
diff changeset
140 return None
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
141
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
142 def load_module(self, name, stuff):
8586
bdfef8e73457 Support loading of packages. (I had this coded up for a while but
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
143 file, filename, info = stuff
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5995
diff changeset
144 try:
8586
bdfef8e73457 Support loading of packages. (I had this coded up for a while but
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
145 return imp.load_module(name, file, filename, info)
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5995
diff changeset
146 finally:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5995
diff changeset
147 if file: file.close()
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
148
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
149
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
150 class Hooks(_Verbose):
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
151
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
152 """Hooks into the filesystem and interpreter.
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
153
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
154 By deriving a subclass you can redefine your filesystem interface,
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
155 e.g. to merge it with the URL space.
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
156
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
157 This base class behaves just like the native filesystem.
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
158
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
159 """
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
160
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
161 # imp interface
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
162 def get_suffixes(self): return imp.get_suffixes()
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
163 def new_module(self, name): return imp.new_module(name)
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
164 def is_builtin(self, name): return imp.is_builtin(name)
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
165 def init_builtin(self, name): return imp.init_builtin(name)
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
166 def is_frozen(self, name): return imp.is_frozen(name)
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
167 def init_frozen(self, name): return imp.init_frozen(name)
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
168 def get_frozen_object(self, name): return imp.get_frozen_object(name)
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
169 def load_source(self, name, filename, file=None):
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5995
diff changeset
170 return imp.load_source(name, filename, file)
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
171 def load_compiled(self, name, filename, file=None):
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5995
diff changeset
172 return imp.load_compiled(name, filename, file)
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
173 def load_dynamic(self, name, filename, file=None):
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5995
diff changeset
174 return imp.load_dynamic(name, filename, file)
8586
bdfef8e73457 Support loading of packages. (I had this coded up for a while but
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
175 def load_package(self, name, filename, file=None):
bdfef8e73457 Support loading of packages. (I had this coded up for a while but
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
176 return imp.load_module(name, file, filename, ("", "", PKG_DIRECTORY))
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
177
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
178 def add_module(self, name):
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5995
diff changeset
179 d = self.modules_dict()
23620
a1de0bab5475 SF 563203. Replaced 'has_key()' with 'in'.
Raymond Hettinger <python@rcn.com>
parents: 16508
diff changeset
180 if name in d: return d[name]
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5995
diff changeset
181 d[name] = m = self.new_module(name)
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5995
diff changeset
182 return m
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
183
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
184 # sys interface
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
185 def modules_dict(self): return sys.modules
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
186 def default_path(self): return sys.path
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
187
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
188 def path_split(self, x): return os.path.split(x)
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
189 def path_join(self, x, y): return os.path.join(x, y)
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
190 def path_isabs(self, x): return os.path.isabs(x)
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
191 # etc.
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
192
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
193 def path_exists(self, x): return os.path.exists(x)
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
194 def path_isdir(self, x): return os.path.isdir(x)
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
195 def path_isfile(self, x): return os.path.isfile(x)
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
196 def path_islink(self, x): return os.path.islink(x)
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
197 # etc.
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
198
28089
14f65d661084 Get rid of many apply() calls.
Guido van Rossum <guido@python.org>
parents: 26668
diff changeset
199 def openfile(self, *x): return open(*x)
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
200 openfile_error = IOError
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
201 def listdir(self, x): return os.listdir(x)
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
202 listdir_error = os.error
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
203 # etc.
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
204
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
205
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
206 class ModuleLoader(BasicModuleLoader):
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
207
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
208 """Default module loader; uses file system hooks.
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
209
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
210 By defining suitable hooks, you might be able to load modules from
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
211 other sources than the file system, e.g. from compressed or
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
212 encrypted files, tar files or (if you're brave!) URLs.
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
213
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
214 """
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
215
8586
bdfef8e73457 Support loading of packages. (I had this coded up for a while but
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
216 def __init__(self, hooks = None, verbose = VERBOSE):
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5995
diff changeset
217 BasicModuleLoader.__init__(self, verbose)
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5995
diff changeset
218 self.hooks = hooks or Hooks(verbose)
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
219
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
220 def default_path(self):
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5995
diff changeset
221 return self.hooks.default_path()
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
222
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
223 def modules_dict(self):
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5995
diff changeset
224 return self.hooks.modules_dict()
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
225
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
226 def get_hooks(self):
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5995
diff changeset
227 return self.hooks
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
228
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
229 def set_hooks(self, hooks):
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5995
diff changeset
230 self.hooks = hooks
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
231
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
232 def find_builtin_module(self, name):
8586
bdfef8e73457 Support loading of packages. (I had this coded up for a while but
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
233 # XXX frozen packages?
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5995
diff changeset
234 if self.hooks.is_builtin(name):
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5995
diff changeset
235 return None, '', ('', '', BUILTIN_MODULE)
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5995
diff changeset
236 if self.hooks.is_frozen(name):
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5995
diff changeset
237 return None, '', ('', '', FROZEN_MODULE)
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5995
diff changeset
238 return None
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
239
8586
bdfef8e73457 Support loading of packages. (I had this coded up for a while but
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
240 def find_module_in_dir(self, name, dir, allow_packages=1):
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5995
diff changeset
241 if dir is None:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5995
diff changeset
242 return self.find_builtin_module(name)
8586
bdfef8e73457 Support loading of packages. (I had this coded up for a while but
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
243 if allow_packages:
bdfef8e73457 Support loading of packages. (I had this coded up for a while but
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
244 fullname = self.hooks.path_join(dir, name)
bdfef8e73457 Support loading of packages. (I had this coded up for a while but
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
245 if self.hooks.path_isdir(fullname):
bdfef8e73457 Support loading of packages. (I had this coded up for a while but
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
246 stuff = self.find_module_in_dir("__init__", fullname, 0)
bdfef8e73457 Support loading of packages. (I had this coded up for a while but
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
247 if stuff:
bdfef8e73457 Support loading of packages. (I had this coded up for a while but
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
248 file = stuff[0]
bdfef8e73457 Support loading of packages. (I had this coded up for a while but
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
249 if file: file.close()
bdfef8e73457 Support loading of packages. (I had this coded up for a while but
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
250 return None, fullname, ('', '', PKG_DIRECTORY)
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5995
diff changeset
251 for info in self.hooks.get_suffixes():
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5995
diff changeset
252 suff, mode, type = info
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5995
diff changeset
253 fullname = self.hooks.path_join(dir, name+suff)
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5995
diff changeset
254 try:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5995
diff changeset
255 fp = self.hooks.openfile(fullname, mode)
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5995
diff changeset
256 return fp, fullname, info
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5995
diff changeset
257 except self.hooks.openfile_error:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5995
diff changeset
258 pass
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5995
diff changeset
259 return None
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
260
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
261 def load_module(self, name, stuff):
8586
bdfef8e73457 Support loading of packages. (I had this coded up for a while but
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
262 file, filename, info = stuff
bdfef8e73457 Support loading of packages. (I had this coded up for a while but
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
263 (suff, mode, type) = info
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5995
diff changeset
264 try:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5995
diff changeset
265 if type == BUILTIN_MODULE:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5995
diff changeset
266 return self.hooks.init_builtin(name)
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5995
diff changeset
267 if type == FROZEN_MODULE:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5995
diff changeset
268 return self.hooks.init_frozen(name)
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5995
diff changeset
269 if type == C_EXTENSION:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5995
diff changeset
270 m = self.hooks.load_dynamic(name, filename, file)
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5995
diff changeset
271 elif type == PY_SOURCE:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5995
diff changeset
272 m = self.hooks.load_source(name, filename, file)
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5995
diff changeset
273 elif type == PY_COMPILED:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5995
diff changeset
274 m = self.hooks.load_compiled(name, filename, file)
8586
bdfef8e73457 Support loading of packages. (I had this coded up for a while but
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
275 elif type == PKG_DIRECTORY:
bdfef8e73457 Support loading of packages. (I had this coded up for a while but
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
276 m = self.hooks.load_package(name, filename, file)
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5995
diff changeset
277 else:
31515
71a294bd7950 Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents: 30723
diff changeset
278 raise ImportError, "Unrecognized module type (%r) for %s" % \
71a294bd7950 Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents: 30723
diff changeset
279 (type, name)
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5995
diff changeset
280 finally:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5995
diff changeset
281 if file: file.close()
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5995
diff changeset
282 m.__file__ = filename
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5995
diff changeset
283 return m
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
284
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
285
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
286 class FancyModuleLoader(ModuleLoader):
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
287
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
288 """Fancy module loader -- parses and execs the code itself."""
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
289
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
290 def load_module(self, name, stuff):
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5995
diff changeset
291 file, filename, (suff, mode, type) = stuff
8586
bdfef8e73457 Support loading of packages. (I had this coded up for a while but
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
292 realfilename = filename
bdfef8e73457 Support loading of packages. (I had this coded up for a while but
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
293 path = None
bdfef8e73457 Support loading of packages. (I had this coded up for a while but
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
294
bdfef8e73457 Support loading of packages. (I had this coded up for a while but
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
295 if type == PKG_DIRECTORY:
bdfef8e73457 Support loading of packages. (I had this coded up for a while but
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
296 initstuff = self.find_module_in_dir("__init__", filename, 0)
bdfef8e73457 Support loading of packages. (I had this coded up for a while but
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
297 if not initstuff:
bdfef8e73457 Support loading of packages. (I had this coded up for a while but
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
298 raise ImportError, "No __init__ module in package %s" % name
bdfef8e73457 Support loading of packages. (I had this coded up for a while but
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
299 initfile, initfilename, initinfo = initstuff
bdfef8e73457 Support loading of packages. (I had this coded up for a while but
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
300 initsuff, initmode, inittype = initinfo
bdfef8e73457 Support loading of packages. (I had this coded up for a while but
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
301 if inittype not in (PY_COMPILED, PY_SOURCE):
bdfef8e73457 Support loading of packages. (I had this coded up for a while but
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
302 if initfile: initfile.close()
bdfef8e73457 Support loading of packages. (I had this coded up for a while but
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
303 raise ImportError, \
31515
71a294bd7950 Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents: 30723
diff changeset
304 "Bad type (%r) for __init__ module in package %s" % (
71a294bd7950 Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents: 30723
diff changeset
305 inittype, name)
8586
bdfef8e73457 Support loading of packages. (I had this coded up for a while but
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
306 path = [filename]
bdfef8e73457 Support loading of packages. (I had this coded up for a while but
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
307 file = initfile
bdfef8e73457 Support loading of packages. (I had this coded up for a while but
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
308 realfilename = initfilename
bdfef8e73457 Support loading of packages. (I had this coded up for a while but
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
309 type = inittype
bdfef8e73457 Support loading of packages. (I had this coded up for a while but
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
310
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5995
diff changeset
311 if type == FROZEN_MODULE:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5995
diff changeset
312 code = self.hooks.get_frozen_object(name)
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5995
diff changeset
313 elif type == PY_COMPILED:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5995
diff changeset
314 import marshal
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5995
diff changeset
315 file.seek(8)
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5995
diff changeset
316 code = marshal.load(file)
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5995
diff changeset
317 elif type == PY_SOURCE:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5995
diff changeset
318 data = file.read()
8586
bdfef8e73457 Support loading of packages. (I had this coded up for a while but
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
319 code = compile(data, realfilename, 'exec')
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5995
diff changeset
320 else:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5995
diff changeset
321 return ModuleLoader.load_module(self, name, stuff)
8586
bdfef8e73457 Support loading of packages. (I had this coded up for a while but
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
322
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5995
diff changeset
323 m = self.hooks.add_module(name)
8586
bdfef8e73457 Support loading of packages. (I had this coded up for a while but
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
324 if path:
bdfef8e73457 Support loading of packages. (I had this coded up for a while but
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
325 m.__path__ = path
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5995
diff changeset
326 m.__file__ = filename
32946
db38c1e83965 ihooks FancyModuleLoader.load_module()
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
327 try:
db38c1e83965 ihooks FancyModuleLoader.load_module()
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
328 exec code in m.__dict__
db38c1e83965 ihooks FancyModuleLoader.load_module()
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
329 except:
db38c1e83965 ihooks FancyModuleLoader.load_module()
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
330 d = self.hooks.modules_dict()
db38c1e83965 ihooks FancyModuleLoader.load_module()
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
331 if name in d:
db38c1e83965 ihooks FancyModuleLoader.load_module()
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
332 del d[name]
db38c1e83965 ihooks FancyModuleLoader.load_module()
Tim Peters <tim.peters@gmail.com>
parents: 31515
diff changeset
333 raise
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5995
diff changeset
334 return m
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
335
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
336
8586
bdfef8e73457 Support loading of packages. (I had this coded up for a while but
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
337 class BasicModuleImporter(_Verbose):
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
338
8586
bdfef8e73457 Support loading of packages. (I had this coded up for a while but
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
339 """Basic module importer; uses module loader.
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
340
8586
bdfef8e73457 Support loading of packages. (I had this coded up for a while but
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
341 This provides basic import facilities but no package imports.
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
342
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
343 """
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
344
8586
bdfef8e73457 Support loading of packages. (I had this coded up for a while but
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
345 def __init__(self, loader = None, verbose = VERBOSE):
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5995
diff changeset
346 _Verbose.__init__(self, verbose)
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5995
diff changeset
347 self.loader = loader or ModuleLoader(None, verbose)
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5995
diff changeset
348 self.modules = self.loader.modules_dict()
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
349
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
350 def get_loader(self):
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5995
diff changeset
351 return self.loader
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
352
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
353 def set_loader(self, loader):
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5995
diff changeset
354 self.loader = loader
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
355
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
356 def get_hooks(self):
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5995
diff changeset
357 return self.loader.get_hooks()
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
358
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
359 def set_hooks(self, hooks):
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5995
diff changeset
360 return self.loader.set_hooks(hooks)
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
361
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
362 def import_module(self, name, globals={}, locals={}, fromlist=[]):
26668
373d0a8ed6be Applying patch
Gustavo Niemeyer <gustavo@niemeyer.net>
parents: 23620
diff changeset
363 name = str(name)
23620
a1de0bab5475 SF 563203. Replaced 'has_key()' with 'in'.
Raymond Hettinger <python@rcn.com>
parents: 16508
diff changeset
364 if name in self.modules:
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5995
diff changeset
365 return self.modules[name] # Fast path
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5995
diff changeset
366 stuff = self.loader.find_module(name)
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5995
diff changeset
367 if not stuff:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5995
diff changeset
368 raise ImportError, "No module named %s" % name
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5995
diff changeset
369 return self.loader.load_module(name, stuff)
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
370
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
371 def reload(self, module, path = None):
26668
373d0a8ed6be Applying patch
Gustavo Niemeyer <gustavo@niemeyer.net>
parents: 23620
diff changeset
372 name = str(module.__name__)
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5995
diff changeset
373 stuff = self.loader.find_module(name, path)
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5995
diff changeset
374 if not stuff:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5995
diff changeset
375 raise ImportError, "Module %s not found for reload" % name
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5995
diff changeset
376 return self.loader.load_module(name, stuff)
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
377
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
378 def unload(self, module):
26668
373d0a8ed6be Applying patch
Gustavo Niemeyer <gustavo@niemeyer.net>
parents: 23620
diff changeset
379 del self.modules[str(module.__name__)]
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5995
diff changeset
380 # XXX Should this try to clear the module's namespace?
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
381
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
382 def install(self):
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5995
diff changeset
383 self.save_import_module = __builtin__.__import__
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5995
diff changeset
384 self.save_reload = __builtin__.reload
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5995
diff changeset
385 if not hasattr(__builtin__, 'unload'):
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5995
diff changeset
386 __builtin__.unload = None
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5995
diff changeset
387 self.save_unload = __builtin__.unload
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5995
diff changeset
388 __builtin__.__import__ = self.import_module
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5995
diff changeset
389 __builtin__.reload = self.reload
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5995
diff changeset
390 __builtin__.unload = self.unload
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
391
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
392 def uninstall(self):
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5995
diff changeset
393 __builtin__.__import__ = self.save_import_module
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5995
diff changeset
394 __builtin__.reload = self.save_reload
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5995
diff changeset
395 __builtin__.unload = self.save_unload
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5995
diff changeset
396 if not __builtin__.unload:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5995
diff changeset
397 del __builtin__.unload
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
398
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
399
8586
bdfef8e73457 Support loading of packages. (I had this coded up for a while but
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
400 class ModuleImporter(BasicModuleImporter):
bdfef8e73457 Support loading of packages. (I had this coded up for a while but
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
401
bdfef8e73457 Support loading of packages. (I had this coded up for a while but
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
402 """A module importer that supports packages."""
15868
3060cbfa01be Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 12392
diff changeset
403
56531
50b7f56814f9 Add support to the ihooks module for relative imports.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 46849
diff changeset
404 def import_module(self, name, globals=None, locals=None, fromlist=None,
50b7f56814f9 Add support to the ihooks module for relative imports.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 46849
diff changeset
405 level=-1):
50b7f56814f9 Add support to the ihooks module for relative imports.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 46849
diff changeset
406 parent = self.determine_parent(globals, level)
26668
373d0a8ed6be Applying patch
Gustavo Niemeyer <gustavo@niemeyer.net>
parents: 23620
diff changeset
407 q, tail = self.find_head_package(parent, str(name))
8586
bdfef8e73457 Support loading of packages. (I had this coded up for a while but
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
408 m = self.load_tail(q, tail)
bdfef8e73457 Support loading of packages. (I had this coded up for a while but
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
409 if not fromlist:
bdfef8e73457 Support loading of packages. (I had this coded up for a while but
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
410 return q
bdfef8e73457 Support loading of packages. (I had this coded up for a while but
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
411 if hasattr(m, "__path__"):
bdfef8e73457 Support loading of packages. (I had this coded up for a while but
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
412 self.ensure_fromlist(m, fromlist)
bdfef8e73457 Support loading of packages. (I had this coded up for a while but
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
413 return m
bdfef8e73457 Support loading of packages. (I had this coded up for a while but
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
414
56531
50b7f56814f9 Add support to the ihooks module for relative imports.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 46849
diff changeset
415 def determine_parent(self, globals, level=-1):
50b7f56814f9 Add support to the ihooks module for relative imports.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 46849
diff changeset
416 if not globals or not level:
8586
bdfef8e73457 Support loading of packages. (I had this coded up for a while but
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
417 return None
56531
50b7f56814f9 Add support to the ihooks module for relative imports.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 46849
diff changeset
418 pkgname = globals.get('__package__')
50b7f56814f9 Add support to the ihooks module for relative imports.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 46849
diff changeset
419 if pkgname is not None:
50b7f56814f9 Add support to the ihooks module for relative imports.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 46849
diff changeset
420 if not pkgname and level > 0:
50b7f56814f9 Add support to the ihooks module for relative imports.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 46849
diff changeset
421 raise ValueError, 'Attempted relative import in non-package'
50b7f56814f9 Add support to the ihooks module for relative imports.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 46849
diff changeset
422 else:
50b7f56814f9 Add support to the ihooks module for relative imports.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 46849
diff changeset
423 # __package__ not set, figure it out and set it
50b7f56814f9 Add support to the ihooks module for relative imports.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 46849
diff changeset
424 modname = globals.get('__name__')
50b7f56814f9 Add support to the ihooks module for relative imports.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 46849
diff changeset
425 if modname is None:
50b7f56814f9 Add support to the ihooks module for relative imports.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 46849
diff changeset
426 return None
50b7f56814f9 Add support to the ihooks module for relative imports.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 46849
diff changeset
427 if "__path__" in globals:
50b7f56814f9 Add support to the ihooks module for relative imports.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 46849
diff changeset
428 # __path__ is set so modname is already the package name
50b7f56814f9 Add support to the ihooks module for relative imports.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 46849
diff changeset
429 pkgname = modname
50b7f56814f9 Add support to the ihooks module for relative imports.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 46849
diff changeset
430 else:
50b7f56814f9 Add support to the ihooks module for relative imports.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 46849
diff changeset
431 # normal module, work out package name if any
50b7f56814f9 Add support to the ihooks module for relative imports.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 46849
diff changeset
432 if '.' not in modname:
50b7f56814f9 Add support to the ihooks module for relative imports.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 46849
diff changeset
433 if level > 0:
50b7f56814f9 Add support to the ihooks module for relative imports.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 46849
diff changeset
434 raise ValueError, ('Attempted relative import in '
50b7f56814f9 Add support to the ihooks module for relative imports.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 46849
diff changeset
435 'non-package')
50b7f56814f9 Add support to the ihooks module for relative imports.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 46849
diff changeset
436 globals['__package__'] = None
50b7f56814f9 Add support to the ihooks module for relative imports.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 46849
diff changeset
437 return None
50b7f56814f9 Add support to the ihooks module for relative imports.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 46849
diff changeset
438 pkgname = modname.rpartition('.')[0]
50b7f56814f9 Add support to the ihooks module for relative imports.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 46849
diff changeset
439 globals['__package__'] = pkgname
50b7f56814f9 Add support to the ihooks module for relative imports.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 46849
diff changeset
440 if level > 0:
50b7f56814f9 Add support to the ihooks module for relative imports.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 46849
diff changeset
441 dot = len(pkgname)
50b7f56814f9 Add support to the ihooks module for relative imports.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 46849
diff changeset
442 for x in range(level, 1, -1):
50b7f56814f9 Add support to the ihooks module for relative imports.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 46849
diff changeset
443 try:
50b7f56814f9 Add support to the ihooks module for relative imports.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 46849
diff changeset
444 dot = pkgname.rindex('.', 0, dot)
50b7f56814f9 Add support to the ihooks module for relative imports.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 46849
diff changeset
445 except ValueError:
50b7f56814f9 Add support to the ihooks module for relative imports.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 46849
diff changeset
446 raise ValueError('attempted relative import beyond '
50b7f56814f9 Add support to the ihooks module for relative imports.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 46849
diff changeset
447 'top-level package')
50b7f56814f9 Add support to the ihooks module for relative imports.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 46849
diff changeset
448 pkgname = pkgname[:dot]
50b7f56814f9 Add support to the ihooks module for relative imports.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 46849
diff changeset
449 try:
50b7f56814f9 Add support to the ihooks module for relative imports.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 46849
diff changeset
450 return sys.modules[pkgname]
50b7f56814f9 Add support to the ihooks module for relative imports.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 46849
diff changeset
451 except KeyError:
50b7f56814f9 Add support to the ihooks module for relative imports.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 46849
diff changeset
452 if level < 1:
50b7f56814f9 Add support to the ihooks module for relative imports.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 46849
diff changeset
453 warn("Parent module '%s' not found while handling "
50b7f56814f9 Add support to the ihooks module for relative imports.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 46849
diff changeset
454 "absolute import" % pkgname, RuntimeWarning, 1)
50b7f56814f9 Add support to the ihooks module for relative imports.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 46849
diff changeset
455 return None
50b7f56814f9 Add support to the ihooks module for relative imports.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 46849
diff changeset
456 else:
50b7f56814f9 Add support to the ihooks module for relative imports.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 46849
diff changeset
457 raise SystemError, ("Parent module '%s' not loaded, cannot "
50b7f56814f9 Add support to the ihooks module for relative imports.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 46849
diff changeset
458 "perform relative import" % pkgname)
8586
bdfef8e73457 Support loading of packages. (I had this coded up for a while but
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
459
bdfef8e73457 Support loading of packages. (I had this coded up for a while but
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
460 def find_head_package(self, parent, name):
bdfef8e73457 Support loading of packages. (I had this coded up for a while but
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
461 if '.' in name:
16508
5c3bf2e89d80 String method conversion.
Eric S. Raymond <esr@thyrsus.com>
parents: 16160
diff changeset
462 i = name.find('.')
8586
bdfef8e73457 Support loading of packages. (I had this coded up for a while but
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
463 head = name[:i]
bdfef8e73457 Support loading of packages. (I had this coded up for a while but
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
464 tail = name[i+1:]
bdfef8e73457 Support loading of packages. (I had this coded up for a while but
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
465 else:
bdfef8e73457 Support loading of packages. (I had this coded up for a while but
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
466 head = name
bdfef8e73457 Support loading of packages. (I had this coded up for a while but
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
467 tail = ""
bdfef8e73457 Support loading of packages. (I had this coded up for a while but
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
468 if parent:
bdfef8e73457 Support loading of packages. (I had this coded up for a while but
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
469 qname = "%s.%s" % (parent.__name__, head)
bdfef8e73457 Support loading of packages. (I had this coded up for a while but
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
470 else:
bdfef8e73457 Support loading of packages. (I had this coded up for a while but
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
471 qname = head
bdfef8e73457 Support loading of packages. (I had this coded up for a while but
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
472 q = self.import_it(head, qname, parent)
bdfef8e73457 Support loading of packages. (I had this coded up for a while but
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
473 if q: return q, tail
bdfef8e73457 Support loading of packages. (I had this coded up for a while but
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
474 if parent:
bdfef8e73457 Support loading of packages. (I had this coded up for a while but
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
475 qname = head
bdfef8e73457 Support loading of packages. (I had this coded up for a while but
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
476 parent = None
bdfef8e73457 Support loading of packages. (I had this coded up for a while but
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
477 q = self.import_it(head, qname, parent)
bdfef8e73457 Support loading of packages. (I had this coded up for a while but
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
478 if q: return q, tail
56531
50b7f56814f9 Add support to the ihooks module for relative imports.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 46849
diff changeset
479 raise ImportError, "No module named '%s'" % qname
8586
bdfef8e73457 Support loading of packages. (I had this coded up for a while but
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
480
bdfef8e73457 Support loading of packages. (I had this coded up for a while but
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
481 def load_tail(self, q, tail):
bdfef8e73457 Support loading of packages. (I had this coded up for a while but
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
482 m = q
bdfef8e73457 Support loading of packages. (I had this coded up for a while but
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
483 while tail:
16508
5c3bf2e89d80 String method conversion.
Eric S. Raymond <esr@thyrsus.com>
parents: 16160
diff changeset
484 i = tail.find('.')
8586
bdfef8e73457 Support loading of packages. (I had this coded up for a while but
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
485 if i < 0: i = len(tail)
bdfef8e73457 Support loading of packages. (I had this coded up for a while but
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
486 head, tail = tail[:i], tail[i+1:]
bdfef8e73457 Support loading of packages. (I had this coded up for a while but
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
487 mname = "%s.%s" % (m.__name__, head)
bdfef8e73457 Support loading of packages. (I had this coded up for a while but
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
488 m = self.import_it(head, mname, m)
bdfef8e73457 Support loading of packages. (I had this coded up for a while but
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
489 if not m:
56531
50b7f56814f9 Add support to the ihooks module for relative imports.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 46849
diff changeset
490 raise ImportError, "No module named '%s'" % mname
8586
bdfef8e73457 Support loading of packages. (I had this coded up for a while but
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
491 return m
bdfef8e73457 Support loading of packages. (I had this coded up for a while but
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
492
bdfef8e73457 Support loading of packages. (I had this coded up for a while but
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
493 def ensure_fromlist(self, m, fromlist, recursive=0):
bdfef8e73457 Support loading of packages. (I had this coded up for a while but
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
494 for sub in fromlist:
bdfef8e73457 Support loading of packages. (I had this coded up for a while but
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
495 if sub == "*":
bdfef8e73457 Support loading of packages. (I had this coded up for a while but
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
496 if not recursive:
bdfef8e73457 Support loading of packages. (I had this coded up for a while but
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
497 try:
bdfef8e73457 Support loading of packages. (I had this coded up for a while but
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
498 all = m.__all__
bdfef8e73457 Support loading of packages. (I had this coded up for a while but
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
499 except AttributeError:
bdfef8e73457 Support loading of packages. (I had this coded up for a while but
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
500 pass
bdfef8e73457 Support loading of packages. (I had this coded up for a while but
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
501 else:
bdfef8e73457 Support loading of packages. (I had this coded up for a while but
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
502 self.ensure_fromlist(m, all, 1)
bdfef8e73457 Support loading of packages. (I had this coded up for a while but
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
503 continue
bdfef8e73457 Support loading of packages. (I had this coded up for a while but
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
504 if sub != "*" and not hasattr(m, sub):
bdfef8e73457 Support loading of packages. (I had this coded up for a while but
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
505 subname = "%s.%s" % (m.__name__, sub)
bdfef8e73457 Support loading of packages. (I had this coded up for a while but
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
506 submod = self.import_it(sub, subname, m)
bdfef8e73457 Support loading of packages. (I had this coded up for a while but
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
507 if not submod:
56531
50b7f56814f9 Add support to the ihooks module for relative imports.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 46849
diff changeset
508 raise ImportError, "No module named '%s'" % subname
8586
bdfef8e73457 Support loading of packages. (I had this coded up for a while but
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
509
12392
8d7e07737b4c Thomas Heller <thomas.heller@ion-tof.com>:
Fred Drake <fdrake@acm.org>
parents: 8586
diff changeset
510 def import_it(self, partname, fqname, parent, force_load=0):
8586
bdfef8e73457 Support loading of packages. (I had this coded up for a while but
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
511 if not partname:
56531
50b7f56814f9 Add support to the ihooks module for relative imports.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 46849
diff changeset
512 # completely empty module name should only happen in
50b7f56814f9 Add support to the ihooks module for relative imports.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 46849
diff changeset
513 # 'from . import' or __import__("")
50b7f56814f9 Add support to the ihooks module for relative imports.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 46849
diff changeset
514 return parent
12392
8d7e07737b4c Thomas Heller <thomas.heller@ion-tof.com>:
Fred Drake <fdrake@acm.org>
parents: 8586
diff changeset
515 if not force_load:
8d7e07737b4c Thomas Heller <thomas.heller@ion-tof.com>:
Fred Drake <fdrake@acm.org>
parents: 8586
diff changeset
516 try:
8d7e07737b4c Thomas Heller <thomas.heller@ion-tof.com>:
Fred Drake <fdrake@acm.org>
parents: 8586
diff changeset
517 return self.modules[fqname]
8d7e07737b4c Thomas Heller <thomas.heller@ion-tof.com>:
Fred Drake <fdrake@acm.org>
parents: 8586
diff changeset
518 except KeyError:
8d7e07737b4c Thomas Heller <thomas.heller@ion-tof.com>:
Fred Drake <fdrake@acm.org>
parents: 8586
diff changeset
519 pass
8586
bdfef8e73457 Support loading of packages. (I had this coded up for a while but
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
520 try:
bdfef8e73457 Support loading of packages. (I had this coded up for a while but
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
521 path = parent and parent.__path__
bdfef8e73457 Support loading of packages. (I had this coded up for a while but
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
522 except AttributeError:
bdfef8e73457 Support loading of packages. (I had this coded up for a while but
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
523 return None
26668
373d0a8ed6be Applying patch
Gustavo Niemeyer <gustavo@niemeyer.net>
parents: 23620
diff changeset
524 partname = str(partname)
8586
bdfef8e73457 Support loading of packages. (I had this coded up for a while but
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
525 stuff = self.loader.find_module(partname, path)
bdfef8e73457 Support loading of packages. (I had this coded up for a while but
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
526 if not stuff:
bdfef8e73457 Support loading of packages. (I had this coded up for a while but
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
527 return None
26668
373d0a8ed6be Applying patch
Gustavo Niemeyer <gustavo@niemeyer.net>
parents: 23620
diff changeset
528 fqname = str(fqname)
8586
bdfef8e73457 Support loading of packages. (I had this coded up for a while but
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
529 m = self.loader.load_module(fqname, stuff)
bdfef8e73457 Support loading of packages. (I had this coded up for a while but
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
530 if parent:
bdfef8e73457 Support loading of packages. (I had this coded up for a while but
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
531 setattr(parent, partname, m)
bdfef8e73457 Support loading of packages. (I had this coded up for a while but
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
532 return m
bdfef8e73457 Support loading of packages. (I had this coded up for a while but
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
533
bdfef8e73457 Support loading of packages. (I had this coded up for a while but
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
534 def reload(self, module):
26668
373d0a8ed6be Applying patch
Gustavo Niemeyer <gustavo@niemeyer.net>
parents: 23620
diff changeset
535 name = str(module.__name__)
8586
bdfef8e73457 Support loading of packages. (I had this coded up for a while but
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
536 if '.' not in name:
12392
8d7e07737b4c Thomas Heller <thomas.heller@ion-tof.com>:
Fred Drake <fdrake@acm.org>
parents: 8586
diff changeset
537 return self.import_it(name, name, None, force_load=1)
16508
5c3bf2e89d80 String method conversion.
Eric S. Raymond <esr@thyrsus.com>
parents: 16160
diff changeset
538 i = name.rfind('.')
8586
bdfef8e73457 Support loading of packages. (I had this coded up for a while but
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
539 pname = name[:i]
bdfef8e73457 Support loading of packages. (I had this coded up for a while but
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
540 parent = self.modules[pname]
12392
8d7e07737b4c Thomas Heller <thomas.heller@ion-tof.com>:
Fred Drake <fdrake@acm.org>
parents: 8586
diff changeset
541 return self.import_it(name[i+1:], name, parent, force_load=1)
8586
bdfef8e73457 Support loading of packages. (I had this coded up for a while but
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
542
bdfef8e73457 Support loading of packages. (I had this coded up for a while but
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
543
2887
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
544 default_importer = None
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
545 current_importer = None
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
546
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
547 def install(importer = None):
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
548 global current_importer
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
549 current_importer = importer or default_importer or ModuleImporter()
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
550 current_importer.install()
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
551
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
552 def uninstall():
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
553 global current_importer
ec0b42889243 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
554 current_importer.uninstall()