Mercurial > cpython-lru_cache-weakref
annotate Lib/newimp.py @ 4566:6403c94de50d legacy-trunk
Added safeguard against failure in __del__.
| author | Guido van Rossum <guido@python.org> |
|---|---|
| date | Wed, 27 Nov 1996 19:39:58 +0000 |
| parents | 59a9689c11a8 |
| children |
| rev | line source |
|---|---|
| 2642 | 1 """Prototype of 'import' functionality enhanced to implement packages. |
| 2 | |
| 3 Why packages? Packages enable module nesting and sibling module | |
| 4 imports. 'Til now, the python module namespace was flat, which | |
| 5 means every module had to have a unique name, in order to not | |
| 6 conflict with names of other modules on the load path. Furthermore, | |
| 7 suites of modules could not be structurally affiliated with one | |
| 8 another. | |
| 9 | |
| 10 With packages, a suite of, eg, email-oriented modules can include a | |
| 11 module named 'mailbox', without conflicting with the, eg, 'mailbox' | |
| 12 module of a shared-memory suite - 'email.mailbox' vs | |
| 13 'shmem.mailbox'. Packages also enable modules within a suite to | |
| 14 load other modules within their package without having the package | |
| 15 name hard-coded. Similarly, package suites of modules can be loaded | |
| 16 as a unit, by loading the package that contains them. | |
| 17 | |
| 18 Usage: once installed (newimp.install(); newimp.revert() to revert to | |
| 19 the prior __import__ routine), 'import ...' and 'from ... import ...' | |
| 20 can be used to: | |
| 21 | |
| 22 - import modules from the search path, as before. | |
| 23 | |
| 24 - import modules from within other directory "packages" on the search | |
| 25 path using a '.' dot-delimited nesting syntax. The nesting is fully | |
| 26 recursive. | |
| 27 | |
| 2644 | 28 For example, 'import test.test_types' will import the test_types |
| 29 module within the 'test' package. The calling environment would | |
| 30 then access the module as 'test.test_types', which is the name of | |
| 31 the fully-loaded 'test_types' module. It is found contained within | |
| 32 the stub (ie, only partially loaded) 'test' module, hence accessed as | |
| 33 'test.test_types'. | |
| 2642 | 34 |
| 35 - import siblings from modules within a package, using '__.' as a shorthand | |
| 36 prefix to refer to the parent package. This enables referential | |
| 37 transparency - package modules need not know their package name. | |
| 38 | |
| 2644 | 39 The '__' package references are actually names assigned within |
| 2642 | 40 modules, to refer to their containing package. This means that |
| 2644 | 41 variable references can be made to imported modules, or to variables |
| 42 defined via 'import ... from', also using the '__.var' shorthand | |
| 43 notation. This establishes a proper equivalence between the import | |
| 44 reference '__.sibling' and the var reference '__.sibling'. | |
| 45 | |
| 46 - import an entire package as a unit, by importing the package directory. | |
|
2826
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
47 If there is a module named '__init__.py' in the package, it controls the |
| 2644 | 48 load. Otherwise, all the modules in the dir, including packages, are |
| 49 inherently loaded into the package module's namespace. | |
| 50 | |
| 51 For example, 'import test' will load the modules of the entire 'test' | |
| 52 package, at least until a test failure is encountered. | |
| 2642 | 53 |
|
2826
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
54 In a package, a module with the name '__init__' has a special role. |
| 2644 | 55 If present in a package directory, then it is loaded into the package |
| 56 module, instead of loading the contents of the directory. This | |
|
2826
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
57 enables the __init__ module to control the load, possibly loading |
| 2644 | 58 the entire directory deliberately (using 'import __', or even |
| 59 'from __ import *', to load all the module contents directly into the | |
| 60 package module). | |
| 61 | |
| 62 - perform any combination of the above - have a package that contains | |
| 63 packages, etc. | |
| 64 | |
| 65 Modules have a few new attributes in support of packages. As mentioned | |
| 66 above, '__' is a shorthand attribute denoting the modules' parent package, | |
| 67 also denoted in the module by '__package__'. Additionally, modules have | |
| 68 associated with them a '__pkgpath__', a path by which sibling modules are | |
| 69 found.""" | |
| 2642 | 70 |
|
2826
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
71 __version__ = "$Revision$" |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
72 |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
73 # $Id$ First release: |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
74 # Ken.Manheimer@nist.gov, 5-Apr-1995, for python 1.2 |
| 2642 | 75 |
|
2826
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
76 # Issues (scattered in code - search for three asterisks) |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
77 # *** Despite my efforts, 'reload(newimp)' will foul things up. |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
78 # *** Normalize_pathname will only work for Unix - which we need to detect. |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
79 # *** when a module with the name of the platform (as indicated by |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
80 # to-be-created var sys.platform), the package path gets '.' and the |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
81 # platform dir. |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
82 # *** use sys.impadmin for things like an import load-hooks var |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
83 # *** Import-load-hook keying module name versus package path, which dictates |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
84 # additions to the default ('.' and os-specific dir) path |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
85 # *** Document that the __init__.py can set __.__pkgpath__, in which case that |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
86 # will be used for the package-relative loads. |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
87 # *** Add a 'recursive' option to reload, for reload of package constituent |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
88 # modules (including subpackages), as well. Or maybe that should be the |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
89 # default, and eg stub-completion should override that default. ??? |
| 2642 | 90 |
| 91 # Developers Notes: | |
| 92 # | |
| 93 # - 'sys.stub_modules' registers "incidental" (partially loaded) modules. | |
| 94 # A stub module is promoted to the fully-loaded 'sys.modules' list when it is | |
| 95 # explicitly loaded as a unit. | |
|
2826
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
96 # - One load nuance - the actual load of most module types goes into the |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
97 # already-generated stub module. HOWEVER, eg dynamically loaded modules |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
98 # generate a new module object, which must supplant the existing stub. One |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
99 # consequence is that the import process must use indirection through |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
100 # sys.stub_modules or sys.modules to track the actual modules across some of |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
101 # the phases. |
| 2642 | 102 # - The test routines are cool, including a transient directory |
| 103 # hierarchy facility, and a means of skipping to later tests by giving | |
| 104 # the test routine a numeric arg. | |
|
2826
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
105 # - There may still be some loose ends, not to mention bugs. But the full |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
106 # functionality should be there. |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
107 # - The ImportStack object is necessary to carry the list of in-process imports |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
108 # across very open-ended recursions, where the state cannot be passed |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
109 # explicitly via the import_module calls; for a primary example, via exec of |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
110 # an 'import' statement within a module. |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
111 # - Python's (current) handling of extension modules, via imp.load_dynamic, |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
112 # does too much, some of which needs to be undone. See comments in |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
113 # load_module. Among other things, we actually change the __name__ of the |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
114 # module, which conceivably may break something. |
| 2642 | 115 |
|
2826
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
116 try: |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
117 VERBOSE |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
118 except NameError: |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
119 VERBOSE = 0 # Will be reset by init(1), also. |
| 2642 | 120 |
|
2826
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
121 import sys, string, regex, types, os, marshal, traceback |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
122 import __main__, __builtin__ |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
123 |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
124 newimp_globals = vars() |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
125 |
| 2642 | 126 try: |
| 127 import imp # Build on this recent addition | |
| 128 except ImportError: | |
|
2826
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
129 raise ImportError, 'Pkg import module depends on optional "imp" module'#==X |
| 2642 | 130 |
| 131 from imp import SEARCH_ERROR, PY_SOURCE, PY_COMPILED, C_EXTENSION | |
|
2826
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
132 |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
133 def defvar(varNm, envDict, val, override=0): |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
134 """If VARNAME does not have value in DICT, assign VAL to it. Optional arg |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
135 OVERRIDE means force the assignment in any case.""" |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
136 if (not envDict.has_key(varNm)) or override: |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
137 envDict[varNm] = val |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
138 |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
139 def init(full_reset=0): |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
140 """Do environment initialization, including retrofitting sys.modules with |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
141 module attributes.""" |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
142 # Retrofit all existing modules with package attributes, under auspices of |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
143 # __root__: |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
144 |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
145 locals, globals = vars(), newimp_globals |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
146 |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
147 if full_reset: |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
148 global VERBOSE |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
149 VERBOSE = 0 |
| 2642 | 150 |
|
2826
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
151 # sys.stub_modules tracks modules partially loaded modules, ie loaded only |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
152 # incidental to load of nested components. Together with sys.modules and |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
153 # the import stack, it serves as part of the module registration mechanism. |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
154 defvar('stub_modules', sys.__dict__, {}, full_reset) |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
155 |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
156 # Environment setup - "root" module, '__root__' |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
157 # Establish root package '__root__' in __main__ and newimp envs. |
| 2642 | 158 |
|
2826
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
159 # Longhand for name of variable identifying module's containing package: |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
160 defvar('PKG_NM', globals, "__package__", full_reset) |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
161 # Shorthand for module's container: |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
162 defvar('PKG_SHORT_NM', globals, "__", full_reset) |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
163 defvar('PKG_SHORT_NM_LEN', globals, len(PKG_SHORT_NM), full_reset) |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
164 |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
165 # Name of controlling module for a package, if any: |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
166 defvar('INIT_MOD_NM', globals, "__init__", full_reset) |
| 2642 | 167 |
|
2826
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
168 # Paths eventually will be extended to accomodate non-filesystem media - |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
169 # eg, URLs, composite objects, who knows. |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
170 |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
171 # Name assigned in sys for general import administration: |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
172 defvar('IMP_SYS_NM', globals, "imp_admin", full_reset) |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
173 defvar('MOD_LOAD_HOOKS', globals, "mod_load_hooks", full_reset) |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
174 if full_reset: |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
175 defvar(IMP_SYS_NM, sys.__dict__, {MOD_LOAD_HOOKS: {}}, full_reset) |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
176 |
| 2642 | 177 |
|
2826
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
178 # Name assigned in each module to tuple describing module import attrs: |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
179 defvar('IMP_ADMIN', globals, "__impadmin__", full_reset) |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
180 # The load-path obtaining for this package. Not defined for non-packages. |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
181 # If not set, package directory is used. If no package directory |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
182 # registered, sys.path is used. |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
183 defvar('PKG_PATH', globals, 0, full_reset) |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
184 # File from which module was loaded - may be None, eg, for __root__: |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
185 defvar('MOD_TYPE', globals, 1, full_reset) |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
186 # Exact path from which the module was loaded: |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
187 defvar('MOD_PATHNAME', globals, 2, full_reset) |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
188 # Package within which the module was found: |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
189 defvar('MOD_PACKAGE', globals, 3, full_reset) |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
190 defvar('USE_PATH', globals, 'either PKG_PATH or my dir', full_reset) |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
191 |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
192 # We're aliasing the top-level __main__ module as '__root__': |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
193 defvar('__root__', globals, __main__, full_reset) |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
194 defvar('ROOT_MOD_NM', globals, "__root__", full_reset) |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
195 if not sys.modules.has_key('__root__') or full_reset: |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
196 # and register it as an imported module: |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
197 sys.modules[ROOT_MOD_NM] = __root__ |
| 2642 | 198 |
|
2826
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
199 # Register package information in all existing top-level modules - they'll |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
200 # the None's mean, among other things, that their USE_PATH's all defer to |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
201 # sys.path. |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
202 for aMod in sys.modules.values(): |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
203 if (not aMod.__dict__.has_key(PKG_NM)) or full_reset: |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
204 set_mod_attrs(aMod, None, __root__, None, None) |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
205 |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
206 try: |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
207 __builtin__.__import__ |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
208 defvar('origImportFunc', globals, __builtin__.__import__) |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
209 defvar('origReloadFunc', globals, __builtin__.reload) |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
210 except AttributeError: |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
211 pass |
| 2642 | 212 |
|
2826
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
213 defvar('PY_PACKAGE', globals, 4, full_reset) |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
214 defvar('PY_FROZEN', globals, 5, full_reset) |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
215 defvar('PY_BUILTIN', globals, 6, full_reset) |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
216 |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
217 # Establish lookup table from mod-type "constants" to names: |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
218 defvar('mod_types', globals, |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
219 {SEARCH_ERROR: 'SEARCH_ERROR', |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
220 PY_SOURCE: 'PY_SOURCE', |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
221 PY_COMPILED: 'PY_COMPILED', |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
222 C_EXTENSION: 'C_EXTENSION', |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
223 PY_PACKAGE: 'PY_PACKAGE', |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
224 PY_FROZEN: 'PY_FROZEN', |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
225 PY_BUILTIN: 'PY_BUILTIN'}, |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
226 full_reset) |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
227 |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
228 defvar('stack', globals, ImportStack(), full_reset) |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
229 |
| 2642 | 230 def install(): |
| 231 """Install newimp import_module() routine, for package support. | |
| 232 | |
| 233 newimp.revert() reverts to __import__ routine that was superceded.""" | |
| 234 __builtin__.__import__ = import_module | |
|
2826
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
235 __builtin__.reload = reload |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
236 __builtin__.unload = unload |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
237 __builtin__.bypass = bypass |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
238 return 'Enhanced import functionality in place.' |
| 2642 | 239 def revert(): |
| 240 """Revert to original __builtin__.__import__ func, if newimp.install() has | |
| 241 been executed.""" | |
|
2826
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
242 if not (origImportFunc and origReloadFunc): |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
243 raise SystemError, "Can't find original import and reload funcs." # ==X |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
244 __builtin__.__import__ = origImportFunc |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
245 __builtin__.reload = origReloadFunc |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
246 del __builtin__.unload, __builtin__.bypass |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
247 return 'Original import routines back in place.' |
| 2642 | 248 |
| 249 def import_module(name, | |
| 250 envLocals=None, envGlobals=None, | |
| 251 froms=None, | |
| 252 inPkg=None): | |
|
2826
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
253 """Primary service routine implementing 'import' with package nesting. |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
254 |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
255 NAME: name as specified to 'import NAME' or 'from NAME...' |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
256 LOCALS, GLOBALS: local and global dicts obtaining for import |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
257 FROMS: list of strings of "..." in 'import blat from ...' |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
258 INPKG: package to which the name search is restricted, for use |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
259 by recursive package loads (from import_module()). |
| 2642 | 260 |
|
2826
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
261 A subtle difference from the old import - modules that do fail |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
262 initialization will not be registered in sys.modules, ie will not, in |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
263 effect, be registered as being loaded. Note further that packages which |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
264 fail their overall load, but have successfully loaded constituent modules, |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
265 will be accessible in the importing namespace as stub modules. |
| 2642 | 266 |
|
2826
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
267 A new routine, 'newimp.bypass()', provides the means to circumvent |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
268 constituent modules that fail their load, in order to enable load of the |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
269 remainder of a package.""" |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
270 |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
271 rootMod = sys.modules[ROOT_MOD_NM] |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
272 |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
273 note("import_module: seeking '%s'" % name, 1) |
| 2642 | 274 |
| 275 # We need callers environment dict for local path and resulting module | |
| 276 # binding. | |
|
2826
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
277 if not envGlobals: |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
278 # This should not happen, but does for imports called from within |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
279 # functions. |
| 2642 | 280 envLocals, envGlobals = exterior() |
| 281 | |
| 282 if inPkg: | |
|
2826
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
283 pkg = inPkg |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
284 elif envGlobals.has_key(PKG_NM): |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
285 pkg = envGlobals[PKG_NM] |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
286 else: |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
287 # ** KLUDGE - cover for modules that lack package attributes: |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
288 pkg = rootMod |
| 2642 | 289 |
|
2826
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
290 if pkg != rootMod: |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
291 note(' - relative to package %s' % pkg) |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
292 |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
293 modList = theMod = absNm = nesting = None |
| 2642 | 294 |
|
2826
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
295 # Normalize |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
296 # - absNm is absolute w.r.t. __root__ |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
297 # - relNm is relative w.r.t. pkg. |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
298 if inPkg: |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
299 absNm, relNm = pkg.__name__ + '.' + name, name |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
300 else: |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
301 absNm, relNm, pkg = normalize_import_ref(name, pkg) |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
302 note("Normalized: %s%s" % (absNm, (((relNm != absNm) |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
303 and (" ('%s' in %s)" % (relNm, pkg))) |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
304 or '')), 3) |
| 2642 | 305 |
|
2826
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
306 pkgPath = get_mod_attrs(pkg, USE_PATH) |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
307 |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
308 try: # try...finally guards import stack integrity. |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
309 |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
310 if stack.push(absNm): |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
311 # We're nested inside a containing import of this module, perhaps |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
312 # indirectly. Avoid infinite recursion at this point by using the |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
313 # existing stub module, for now. Load of it will be completed by |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
314 # the superior import. |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
315 note('recursion on in-process module %s, punting with stub' % |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
316 absNm) |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
317 theMod = stack.mod(absNm) |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
318 |
| 2642 | 319 else: |
|
2826
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
320 |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
321 # Try to find already-imported: |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
322 if sys.modules.has_key(absNm): |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
323 note('found ' + absNm + ' already imported') |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
324 theMod = sys.modules[absNm] |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
325 stack.mod(absNm, theMod) |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
326 |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
327 else: # Actually do load, of one sort or another: |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
328 |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
329 # Seek builtin or frozen first: |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
330 theMod = imp.init_builtin(absNm) |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
331 if theMod: |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
332 set_mod_attrs(theMod, None, pkg, None, PY_BUILTIN) |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
333 stack.mod(absNm, theMod) |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
334 note('found builtin ' + absNm) |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
335 else: |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
336 theMod = imp.init_frozen(absNm) |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
337 if theMod: |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
338 set_mod_attrs(theMod, None, pkg, None, PY_FROZEN) |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
339 stack.mod(absNm, theMod) |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
340 note('found frozen ' + absNm) |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
341 |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
342 if not theMod: |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
343 # Not already-loaded, in-process, builtin, or frozen - |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
344 # we're seeking in the outside world (filesystem): |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
345 |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
346 if sys.stub_modules.has_key(absNm): |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
347 |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
348 # A package for which we have a stub: |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
349 theMod = reload(sys.stub_modules[absNm], inPkg) |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
350 |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
351 else: |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
352 |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
353 # Now we actually search the fs. |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
354 |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
355 if type(pkgPath) == types.StringType: |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
356 pkgPath = [pkgPath] |
| 2642 | 357 |
|
2826
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
358 # Find a path leading to the module: |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
359 modList = find_module(relNm, pkgPath, absNm) |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
360 if not modList: |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
361 raise ImportError, ("module '%s' not found" % #==X |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
362 absNm) |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
363 |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
364 # We have a list of successively nested dirs leading |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
365 # to the module, register with import admin, as stubs: |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
366 nesting = register_mod_nesting(modList, pkg) |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
367 |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
368 # Load from file if necessary and possible: |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
369 modNm, modf, path, ty = modList[-1] |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
370 note('found type %s - %s' % (mod_types[ty[2]], absNm)) |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
371 |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
372 # Establish the module object in question: |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
373 theMod = procure_module(absNm) |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
374 stack.mod(absNm, theMod) |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
375 |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
376 # Do the load: |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
377 theMod = load_module(theMod, ty[2], modf, inPkg) |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
378 |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
379 commit_mod_containment(absNm) |
| 2642 | 380 |
|
2826
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
381 # Successful load - promote to fully-imported status: |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
382 register_module(theMod, theMod.__name__) |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
383 |
| 2642 | 384 |
|
2826
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
385 # We have a loaded module (perhaps stub): situate specified components, |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
386 # and return appropriate thing. According to guido: |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
387 # |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
388 # "Note that for "from spam.ham import bacon" your function should |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
389 # return the object denoted by 'spam.ham', while for "import |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
390 # spam.ham" it should return the object denoted by 'spam' -- the |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
391 # STORE instructions following the import statement expect it this |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
392 # way." |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
393 # *** The above rationale should probably be reexamined, since newimp |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
394 # actually takes care of populating the caller's namespace. |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
395 |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
396 if not froms: |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
397 |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
398 # Return the outermost container, possibly stub: |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
399 if nesting: |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
400 return find_mod_registration(nesting[0][0]) |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
401 else: |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
402 return find_mod_registration(string.splitfields(absNm,'.')[0]) |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
403 else: |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
404 |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
405 return theMod |
| 2642 | 406 |
|
2826
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
407 finally: # Decrement stack registration: |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
408 stack.pop(absNm) |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
409 |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
410 |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
411 def reload(module, inPkg = None): |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
412 """Re-parse and re-initialize an already (or partially) imported MODULE. |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
413 |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
414 The argument can be an already loaded module object or a string name of a |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
415 loaded module or a "stub" module that was partially loaded package module |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
416 incidental to the full load of a contained module. |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
417 |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
418 This is useful if you have edited the module source file using an external |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
419 editor and want to try out the new version without leaving the Python |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
420 interpreter. The return value is the resulting module object. |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
421 |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
422 Contrary to the old 'reload', the load is sought from the same location |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
423 where the module was originally found. If you wish to do a fresh load from |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
424 a different module on the path, do an 'unload()' and then an import. |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
425 |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
426 When a module is reloaded, its dictionary (containing the module's |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
427 global variables) is retained. Redefinitions of names will |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
428 override the old definitions, so this is generally not a problem. |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
429 If the new version of a module does not define a name that was |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
430 defined by the old version, the old definition remains. This |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
431 feature can be used to the module's advantage if it maintains a |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
432 global table or cache of objects -- with a `try' statement it can |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
433 test for the table's presence and skip its initialization if |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
434 desired. |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
435 |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
436 It is legal though generally not very useful to reload built-in or |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
437 dynamically loaded modules, except for `sys', `__main__' and |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
438 `__builtin__'. In certain cases, however, extension modules are |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
439 not designed to be initialized more than once, and may fail in |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
440 arbitrary ways when reloaded. |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
441 |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
442 If a module imports objects from another module using `from' ... |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
443 `import' ..., calling `reload()' for the other module does not |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
444 redefine the objects imported from it -- one way around this is to |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
445 re-execute the `from' statement, another is to use `import' and |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
446 qualified names (MODULE.NAME) instead. |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
447 |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
448 If a module instantiates instances of a class, reloading the module |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
449 that defines the class does not affect the method definitions of |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
450 the instances, unless they are reinstantiated -- they continue to use the |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
451 old class definition. The same is true for derived classes.""" |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
452 |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
453 if type(module) == types.StringType: |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
454 theMod = find_mod_registration(module) |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
455 elif type(module) == types.ModuleType: |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
456 theMod = module |
| 2642 | 457 else: |
|
2826
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
458 raise ImportError, '%s not already imported' # ==X |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
459 |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
460 if theMod in [sys.modules[ROOT_MOD_NM], sys.modules['__builtin__']]: |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
461 raise ImportError, 'cannot re-init internal module' # ==X |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
462 |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
463 try: |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
464 thePath = get_mod_attrs(theMod, MOD_PATHNAME) |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
465 except KeyError: |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
466 thePath = None |
| 2642 | 467 |
|
2826
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
468 if not thePath: |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
469 # If we have no path for the module, we can only reload it from |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
470 # scratch: |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
471 note('no pathname registered for %s, doing full reload' % theMod) |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
472 unload(theMod) |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
473 envGlobals, envLocals = exterior() |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
474 return import_module(theMod.__name__, |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
475 envGlobals, envLocals, None, inPkg) |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
476 else: |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
477 |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
478 stack.mod(theMod.__name__, theMod) |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
479 ty = get_mod_attrs(theMod, MOD_TYPE) |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
480 if ty in [PY_SOURCE, PY_COMPILED]: |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
481 note('reload invoked for %s %s' % (mod_types[ty], theMod)) |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
482 thePath, ty, openFile = prefer_compiled(thePath, ty) |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
483 else: |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
484 openFile = open(thePath, get_suffixes(ty)[1]) |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
485 return load_module(theMod, # ==> |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
486 ty, |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
487 openFile, |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
488 inPkg) |
| 2642 | 489 def unload(module): |
|
2826
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
490 """Remove registration for a module, so import will do a fresh load. |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
491 |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
492 Returns the module registries (sys.modules and/or sys.stub_modules) where |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
493 it was found.""" |
| 2642 | 494 if type(module) == types.ModuleType: |
| 495 module = module.__name__ | |
|
2826
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
496 gotit = [] |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
497 for which in ['sys.modules', 'sys.stub_modules']: |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
498 m = eval(which) |
| 2642 | 499 try: |
| 500 del m[module] | |
|
2826
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
501 gotit.append(which) |
| 2642 | 502 except KeyError: |
| 503 pass | |
|
2826
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
504 if not gotit: |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
505 raise ValueError, '%s not a module or a stub' % module # ==X |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
506 else: return gotit |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
507 def bypass(modNm): |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
508 """Register MODULE-NAME so module will be skipped, eg in package load.""" |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
509 if sys.modules.has_key(modNm): |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
510 raise ImportError("'%s' already imported, cannot be bypassed." % modNm) |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
511 else: |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
512 sys.modules[modNm] = imp.new_module('bypass()ed module %s' % modNm) |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
513 commit_mod_containment(modNm) |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
514 |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
515 |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
516 def normalize_import_ref(name, pkg): |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
517 """Produce absolute and relative nm and relative pkg given MODNM and origin |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
518 PACKAGE, reducing out all '__'s in the process.""" |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
519 |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
520 # First reduce out all the '__' container-refs we can: |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
521 outwards, inwards = 0, [] |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
522 for nm in string.splitfields(name, '.'): |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
523 if nm == PKG_SHORT_NM: |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
524 if inwards: |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
525 # Pop a containing inwards: |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
526 del inwards[-1] |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
527 else: |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
528 # (Effectively) leading '__' - notch outwards: |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
529 outwards = outwards + 1 |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
530 else: |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
531 inwards.append(nm) |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
532 inwards = string.joinfields(inwards, '.') |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
533 |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
534 # Now identify the components: |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
535 |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
536 if not outwards: |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
537 pkg = sys.modules[ROOT_MOD_NM] |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
538 else: |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
539 while outwards > 1: |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
540 pkg = pkg.__dict__[PKG_NM] # We'll just loop at top |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
541 if pkg == __root__: |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
542 break # ==v |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
543 outwards = outwards - 1 |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
544 |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
545 if not inwards: # Entire package: |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
546 return pkg.__name__, pkg.__name__, pkg # ==> |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
547 else: # Name relative to package: |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
548 if pkg == __root__: |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
549 return inwards, inwards, pkg # ==> |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
550 else: |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
551 return pkg.__name__ + '.' + inwards, inwards, pkg # ==> |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
552 |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
553 class ImportStack: |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
554 """Provide judicious support for mutually recursive import loops. |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
555 |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
556 Mutually recursive imports, eg a module that imports the package that |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
557 contains it, which in turn imports the module, are not uncommon, and must |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
558 be supported judiciously. This class is used to track cycles, so a module |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
559 already in the process of being imported (via 'stack.push(module)', and |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
560 concluded via 'stack.release(module)') is not redundantly pursued; *except* |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
561 when a module master '__init__.py' loads the module, in which case it is |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
562 'stack.relax(module)'ed, so the full import is pursued.""" |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
563 |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
564 def __init__(self): |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
565 self._cycles = {} |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
566 self._mods = {} |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
567 self._looped = [] |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
568 def in_process(self, modNm): |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
569 """1 if modNm load already in process, 0 otherwise.""" |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
570 return self._cycles.has_key(modNm) # ==> |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
571 def looped(self, modNm): |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
572 """1 if modNm load has looped once or more, 0 otherwise.""" |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
573 return modNm in self._looped |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
574 def push(self, modNm): |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
575 """1 if modNm already in process and not 'relax'ed, 0 otherwise. |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
576 (Note that the 'looped' status remains even when the cycle count |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
577 returns to 1. This is so error messages can indicate that it was, at |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
578 some point, looped during the import process.)""" |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
579 if self.in_process(modNm): |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
580 self._looped.append(modNm) |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
581 self._cycles[modNm] = self._cycles[modNm] + 1 |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
582 return 1 # ==> |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
583 else: |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
584 self._cycles[modNm] = 1 |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
585 return 0 # ==> |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
586 def mod(self, modNm, mod=None): |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
587 """Associate MOD-NAME with MODULE, for easy reference.""" |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
588 if mod: |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
589 self._mods[modNm] = mod |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
590 else: |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
591 try: |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
592 return self._mods[modNm] # ==> |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
593 except KeyError: |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
594 return None |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
595 def pop(self, modNm): |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
596 """Decrement stack count of MODNM""" |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
597 if self.in_process(modNm): |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
598 amt = self._cycles[modNm] = self._cycles[modNm] - 1 |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
599 if amt < 1: |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
600 del self._cycles[modNm] |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
601 if modNm in self._looped: |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
602 self._looped.remove(modNm) |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
603 if self._mods.has_key(modNm): |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
604 del self._mods[modNm] |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
605 def relax(self, modNm): |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
606 """Enable modNm load despite being registered as already in-process.""" |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
607 if self._cycles.has_key(modNm): |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
608 del self._cycles[modNm] |
| 2642 | 609 |
| 610 def find_module(name, path, absNm=''): | |
| 611 """Locate module NAME on PATH. PATH is pathname string or a list of them. | |
| 612 | |
| 613 Note that up-to-date compiled versions of a module are preferred to plain | |
| 614 source, and compilation is automatically performed when necessary and | |
| 615 possible. | |
| 616 | |
|
2826
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
617 Returns a list of the tuples returned by 'find_mod_file()', one for |
| 2642 | 618 each nested level, deepest last.""" |
| 619 | |
| 620 checked = [] # For avoiding redundant dir lists. | |
| 621 | |
| 622 if not absNm: absNm = name | |
| 623 | |
| 624 # Parse name into list of nested components, | |
| 625 expNm = string.splitfields(name, '.') | |
| 626 | |
| 627 for curPath in path: | |
| 628 | |
| 629 if (type(curPath) != types.StringType) or (curPath in checked): | |
| 630 # Disregard bogus or already investigated path elements: | |
| 631 continue # ==^ | |
| 632 else: | |
| 633 # Register it for subsequent disregard. | |
| 634 checked.append(curPath) | |
| 635 | |
| 636 if len(expNm) == 1: | |
| 637 | |
| 638 # Non-nested module name: | |
| 639 | |
|
2826
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
640 got = find_mod_file(curPath, absNm) |
| 2642 | 641 if got: |
|
2826
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
642 note('using %s' % got[2], 3) |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
643 return [got] # ==> |
| 2642 | 644 |
| 645 else: | |
| 646 | |
| 647 # Composite name specifying nested module: | |
| 648 | |
| 649 gotList = []; nameAccume = expNm[0] | |
| 650 | |
|
2826
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
651 got = find_mod_file(curPath, nameAccume) |
| 2642 | 652 if not got: # Continue to next prospective path. |
| 653 continue # ==^ | |
| 654 else: | |
| 655 gotList.append(got) | |
| 656 nm, file, fullPath, ty = got | |
| 657 | |
| 658 # Work on successively nested components: | |
| 659 for component in expNm[1:]: | |
| 660 # 'ty'pe of containing component must be package: | |
| 661 if ty[2] != PY_PACKAGE: | |
| 662 gotList, got = [], None | |
|
2826
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
663 break # ==v |
| 2642 | 664 if nameAccume: |
| 665 nameAccume = nameAccume + '.' + component | |
| 666 else: | |
| 667 nameAccume = component | |
|
2826
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
668 got = find_mod_file(fullPath, nameAccume) |
| 2642 | 669 if got: |
| 670 gotList.append(got) | |
| 671 nm, file, fullPath, ty = got | |
| 672 else: | |
| 673 # Clear state vars: | |
| 674 gotList, got, nameAccume = [], None, '' | |
|
2826
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
675 break # ==v |
| 2642 | 676 # Found nesting all the way to the specified tip: |
| 677 if got: | |
|
2826
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
678 return gotList # ==> |
| 2642 | 679 |
| 680 # Failed. | |
| 681 return None | |
| 682 | |
|
2826
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
683 def find_mod_file(pathNm, modname): |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
684 """Find right module file given DIR and module NAME, compiling if needed. |
| 2642 | 685 |
|
2826
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
686 If successful, returns quadruple consisting of: |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
687 - mod name, |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
688 - file object, |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
689 - full pathname for the found file, |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
690 - a description triple as contained in the list returned by get_suffixes. |
| 2642 | 691 |
| 692 Otherwise, returns None. | |
| 693 | |
| 694 Note that up-to-date compiled versions of a module are preferred to plain | |
| 695 source, and compilation is automatically performed, when necessary and | |
| 696 possible.""" | |
| 697 | |
|
2826
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
698 relNm = modname[1 + string.rfind(modname, '.'):] |
| 2642 | 699 |
| 700 for suff, mode, ty in get_suffixes(): | |
|
2826
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
701 fullPath = os.path.join(pathNm, relNm + suff) |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
702 note('trying ' + fullPath + '...', 4) |
| 2642 | 703 try: |
| 704 modf = open(fullPath, mode) | |
| 705 except IOError: | |
|
2826
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
706 # ** ?? Skip unreadable ones: |
| 2642 | 707 continue # ==^ |
| 708 | |
| 709 if ty == PY_PACKAGE: | |
| 710 # Enforce directory characteristic: | |
| 711 if not os.path.isdir(fullPath): | |
|
2826
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
712 note('Skipping non-dir match ' + fullPath, 3) |
| 2642 | 713 continue # ==^ |
| 714 else: | |
|
2826
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
715 return (modname, modf, fullPath, (suff, mode, ty)) # ==> |
| 2642 | 716 |
| 717 | |
|
2826
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
718 elif ty in [PY_SOURCE, PY_COMPILED]: |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
719 usePath, useTy, openFile = prefer_compiled(fullPath, ty) |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
720 return (modname, # ==> |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
721 openFile, |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
722 usePath, |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
723 get_suffixes(useTy)) |
| 2642 | 724 |
| 725 elif ty == C_EXTENSION: | |
|
2826
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
726 note('found C_EXTENSION ' + fullPath, 3) |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
727 return (modname, modf, fullPath, (suff, mode, ty)) # ==> |
| 2642 | 728 |
| 729 else: | |
|
2826
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
730 raise SystemError, 'Unanticipated module type encountered' # ==X |
| 2642 | 731 |
| 732 return None | |
| 733 | |
|
2826
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
734 def prefer_compiled(path, ty, modf=None): |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
735 """Given a path to a .py or .pyc file, attempt to return a path to a |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
736 current pyc file, compiling the .py in the process if necessary. Returns |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
737 the path to the most current version we can get.""" |
| 2642 | 738 |
|
2826
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
739 if ty == PY_SOURCE: |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
740 if not modf: |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
741 try: |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
742 modf = open(path, 'r') |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
743 except IOError: |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
744 pass |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
745 note('working from PY_SOURCE', 3) |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
746 # Try for a compiled version: |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
747 pyc = path + 'c' # Sadly, we're presuming '.py' suff. |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
748 if (not os.path.exists(pyc) or |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
749 (os.stat(path)[8] > os.stat(pyc)[8])): |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
750 # Try to compile: |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
751 pyc = compile_source(path, modf) |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
752 if pyc and not (os.stat(path)[8] > os.stat(pyc)[8]): |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
753 # Either pyc was already newer or we just made it so; in either |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
754 # case it's what we crave: |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
755 note('but got newer compiled, ' + pyc, 3) |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
756 try: |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
757 return (pyc, PY_COMPILED, open(pyc, 'rb')) # ==> |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
758 except IOError: |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
759 if modf: |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
760 return (path, PY_SOURCE, modf) # ==> |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
761 else: |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
762 raise ImportError, 'Failed acces to .py and .pyc' # ==X |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
763 else: |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
764 note("couldn't get newer compiled, using PY_SOURCE", 3) |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
765 if modf: |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
766 return (path, PY_SOURCE, modf) # ==> |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
767 else: |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
768 raise ImportError, 'Failed acces to .py and .pyc' # ==X |
| 2642 | 769 |
|
2826
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
770 elif ty == PY_COMPILED: |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
771 note('working from PY_COMPILED', 3) |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
772 if not modf: |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
773 try: |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
774 modf = open(path, 'rb') |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
775 except IOError: |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
776 return prefer_compiled(path[:-1], PY_SOURCE) |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
777 # Make sure it is current, trying to compile if necessary, and |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
778 # prefer source failing that: |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
779 note('found compiled ' + path, 3) |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
780 py = path[:-1] # ** Presuming '.pyc' suffix |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
781 if not os.path.exists(py): |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
782 note('pyc SANS py: ' + path, 3) |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
783 return (path, PY_COMPILED, open(py, 'r')) # ==> |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
784 elif (os.stat(py)[8] > os.stat(path)[8]): |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
785 note('Forced to compile: ' + py, 3) |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
786 pyc = compile_source(py, open(py, 'r')) |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
787 if pyc: |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
788 return (pyc, PY_COMPILED, modf) # ==> |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
789 else: |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
790 note('failed compile - must use more recent .py', 3) |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
791 return (py, PY_SOURCE, open(py, 'r')) # ==> |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
792 else: |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
793 return (path, PY_COMPILED, modf) # ==> |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
794 |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
795 def load_module(theMod, ty, theFile, fromMod): |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
796 """Load module NAME, of TYPE, from FILE, within MODULE. |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
797 |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
798 Optional arg fromMod indicates the module from which the load is being done |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
799 - necessary for detecting import of __ from a package's __init__ module. |
| 2642 | 800 |
| 801 Return the populated module object.""" | |
| 802 | |
| 803 # Note: we mint and register intermediate package directories, as necessary | |
| 804 | |
|
2826
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
805 name = theMod.__name__ |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
806 nameTail = name[1 + string.rfind(name, '.'):] |
| 2642 | 807 thePath = theFile.name |
| 808 | |
| 809 if ty == PY_SOURCE: | |
| 810 exec_into(theFile, theMod, theFile.name) | |
| 811 | |
| 812 elif ty == PY_COMPILED: | |
| 813 pyc = open(theFile.name, 'rb').read() | |
| 814 if pyc[0:4] != imp.get_magic(): | |
|
2826
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
815 raise ImportError, 'bad magic number: ' + theFile.name # ==X |
| 2642 | 816 code = marshal.loads(pyc[8:]) |
| 817 exec_into(code, theMod, theFile.name) | |
| 818 | |
| 819 elif ty == C_EXTENSION: | |
|
2826
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
820 # Dynamically loaded C_EXTENSION modules do too much import admin, |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
821 # themselves, which we need to *undo* in order to integrate them with |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
822 # the new import scheme. |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
823 # 1 They register themselves in sys.modules, registering themselves |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
824 # under their top-level names. Have to rectify that. |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
825 # 2 The produce their own module objects, *unless* they find an |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
826 # existing module already registered a la 1, above. We employ this |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
827 # quirk to make it use the already generated module. |
| 2642 | 828 try: |
|
2826
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
829 # Stash a ref to any module that is already registered under the |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
830 # dyamic module's simple name (nameTail), so we can reestablish it |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
831 # after the dynamic takes over its' slot: |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
832 protMod = None |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
833 if nameTail != name: |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
834 if sys.modules.has_key(nameTail): |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
835 protMod = sys.modules[nameTail] |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
836 # Trick the dynamic load, by registering the module we generated |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
837 # under the nameTail of the module we're loading, so the one we're |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
838 # loading will use that established module, rather than producing a |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
839 # new one: |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
840 sys.modules[nameTail] = theMod |
| 2642 | 841 theMod = imp.load_dynamic(nameTail, thePath, theFile) |
|
2826
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
842 theMod.__name__ = name |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
843 # Cleanup dynamic mod's bogus self-registration, if necessary: |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
844 if nameTail != name: |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
845 if protMod: |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
846 # ... reinstating the one that was already there... |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
847 sys.modules[nameTail] = protMod |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
848 else: |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
849 if sys.modules.has_key(nameTail): |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
850 # Certain, as long os dynamics continue to misbehave. |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
851 del sys.modules[nameTail] |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
852 stack.mod(name, theMod) |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
853 if sys.stub_modules.has_key(name): |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
854 sys.stub_modules[name] = theMod |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
855 elif sys.modules.has_key(name): |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
856 sys.modules[name] = theMod |
| 2642 | 857 except: |
|
2826
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
858 # Provide import-nesting info, including signs of circularity: |
|
2828
59a9689c11a8
Official version 1.26 directly from Ken
Guido van Rossum <guido@python.org>
parents:
2826
diff
changeset
|
859 raise sys.exc_type, import_trail_msg(str(sys.exc_value),# ==X |
|
59a9689c11a8
Official version 1.26 directly from Ken
Guido van Rossum <guido@python.org>
parents:
2826
diff
changeset
|
860 sys.exc_traceback, |
|
59a9689c11a8
Official version 1.26 directly from Ken
Guido van Rossum <guido@python.org>
parents:
2826
diff
changeset
|
861 name) |
|
2826
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
862 elif ty == PY_PACKAGE: |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
863 # Load package constituents, doing the controlling module *if* it |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
864 # exists *and* it isn't already in process: |
| 2642 | 865 |
|
2826
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
866 init_mod_f = init_mod = None |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
867 if not stack.in_process(name + '.' + INIT_MOD_NM): |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
868 # Not already doing __init__ - check for it: |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
869 init_mod_f = find_mod_file(thePath, INIT_MOD_NM) |
| 2642 | 870 else: |
|
2826
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
871 note('skipping already-in-process %s.%s' % (theMod.__name__, |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
872 INIT_MOD_NM)) |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
873 got = {} |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
874 if init_mod_f: |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
875 note("Found package's __init__: " + init_mod_f[2]) |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
876 # Enable full continuance of containing-package-load from __init__: |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
877 if stack.in_process(theMod.__name__): |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
878 stack.relax(theMod.__name__) |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
879 init_mod = import_module(INIT_MOD_NM, |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
880 theMod.__dict__, theMod.__dict__, |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
881 None, |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
882 theMod) |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
883 else: |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
884 # ... or else recursively load all constituent modules, except |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
885 # __init__: |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
886 for prospect in mod_prospects(thePath): |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
887 if prospect != INIT_MOD_NM: |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
888 import_module(prospect, |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
889 theMod.__dict__, theMod.__dict__, |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
890 None, |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
891 theMod) |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
892 |
| 2642 | 893 else: |
|
2826
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
894 raise ImportError, 'Unimplemented import type: %s' % ty # ==X |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
895 |
| 2642 | 896 return theMod |
|
2826
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
897 |
| 2642 | 898 def exec_into(obj, module, path): |
| 899 """Helper for load_module, execfile/exec path or code OBJ within MODULE.""" | |
| 900 | |
| 901 # This depends on ability of exec and execfile to mutilate, erhm, mutate | |
| 902 # the __dict__ of a module. It will not work if/when this becomes | |
| 903 # disallowed, as it is for normal assignments. | |
| 904 | |
| 905 try: | |
| 906 if type(obj) == types.FileType: | |
| 907 execfile(path, module.__dict__, module.__dict__) | |
| 908 elif type(obj) in [types.CodeType, types.StringType]: | |
| 909 exec obj in module.__dict__, module.__dict__ | |
| 910 except: | |
|
2826
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
911 # Make the error message nicer? |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
912 raise sys.exc_type, import_trail_msg(str(sys.exc_value), # ==X |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
913 sys.exc_traceback, |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
914 module.__name__) |
| 2642 | 915 |
| 916 | |
| 917 def mod_prospects(path): | |
| 918 """Return a list of prospective modules within directory PATH. | |
| 919 | |
| 920 We actually return the distinct names resulting from stripping the dir | |
|
2826
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
921 entries (excluding os.curdir and os.pardir) of their suffixes (as |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
922 represented by 'get_suffixes'). |
| 2642 | 923 |
| 924 (Note that matches for the PY_PACKAGE type with null suffix are | |
| 925 implicitly constrained to be directories.)""" | |
| 926 | |
| 927 # We actually strip the longest matching suffixes, so eg 'dbmmodule.so' | |
| 928 # mates with 'module.so' rather than '.so'. | |
| 929 | |
| 930 dirList = os.listdir(path) | |
|
2826
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
931 excludes = [os.curdir, os.pardir] |
| 2642 | 932 sortedSuffs = sorted_suffixes() |
| 933 entries = [] | |
| 934 for item in dirList: | |
| 935 if item in excludes: continue # ==^ | |
| 936 for suff in sortedSuffs: | |
|
2826
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
937 # *** ?? maybe platform-specific: |
| 2642 | 938 sub = -1 * len(suff) |
| 939 if sub == 0: | |
| 940 if os.path.isdir(os.path.join(path, item)): | |
| 941 entries.append(item) | |
| 942 elif item[sub:] == suff: | |
| 943 it = item[:sub] | |
| 944 if not it in entries: | |
| 945 entries.append(it) | |
|
2826
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
946 break # ==v |
| 2642 | 947 return entries |
| 948 | |
| 949 | |
| 950 | |
| 951 def procure_module(name): | |
| 952 """Return an established or else new module object having NAME. | |
| 953 | |
| 954 First checks sys.modules, then sys.stub_modules.""" | |
| 955 | |
| 956 if sys.modules.has_key(name): | |
|
2826
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
957 return sys.modules[name] # ==> |
| 2642 | 958 elif sys.stub_modules.has_key(name): |
|
2826
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
959 return sys.stub_modules[name] # ==> |
| 2642 | 960 else: |
|
2826
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
961 return (stack.mod(name) or imp.new_module(name)) # ==> |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
962 |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
963 def commit_mod_containment(name): |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
964 """Bind a module object and its containers within their respective |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
965 containers.""" |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
966 cume, pkg = '', find_mod_registration(ROOT_MOD_NM) |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
967 for next in string.splitfields(name, '.'): |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
968 if cume: |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
969 cume = cume + '.' + next |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
970 else: |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
971 cume = next |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
972 cumeMod = find_mod_registration(cume) |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
973 pkg.__dict__[next] = cumeMod |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
974 pkg = cumeMod |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
975 |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
976 def register_mod_nesting(modList, pkg): |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
977 """Given find_module()-style NEST-LIST and parent PACKAGE, register new |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
978 package components as stub modules, and return list of nested |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
979 module/relative-name pairs. |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
980 |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
981 Note that the modules objects are not situated in their containing packages |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
982 here - that is left 'til after a successful load, and done by |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
983 commit_mod_nesting().""" |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
984 nesting = [] |
| 2642 | 985 |
|
2826
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
986 for modNm, modF, path, ty in modList: |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
987 |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
988 relNm = modNm[1 + string.rfind(modNm, '.'):] |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
989 |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
990 if sys.modules.has_key(modNm): |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
991 theMod = sys.modules[modNm] # Nestle in containing package |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
992 pkg = theMod # Set as parent for next in sequence. |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
993 elif sys.stub_modules.has_key(modNm): |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
994 # Similar to above... |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
995 theMod = sys.stub_modules[modNm] |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
996 pkg = theMod |
| 2642 | 997 else: |
|
2826
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
998 theMod = procure_module(modNm) |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
999 stack.mod(modNm, theMod) |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
1000 # *** ??? Should we be using 'path' instead of modF.name? If not, |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
1001 # should we get rid of the 'path' return val? |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
1002 set_mod_attrs(theMod, normalize_pathname(modF.name), |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
1003 pkg, None, ty[2]) |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
1004 if ty[2] == PY_PACKAGE: |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
1005 # Register as a stub: |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
1006 register_module(theMod, modNm, 1) |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
1007 pkg = theMod |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
1008 nesting.append((theMod.__name__,relNm)) |
| 2642 | 1009 |
|
2826
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
1010 return nesting |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
1011 |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
1012 def register_module(theMod, name, stub=0): |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
1013 """Properly register MODULE, NAME, and optional STUB qualification.""" |
| 2642 | 1014 |
| 1015 if stub: | |
| 1016 sys.stub_modules[name] = theMod | |
| 1017 else: | |
| 1018 sys.modules[name] = theMod | |
| 1019 if sys.stub_modules.has_key(name): | |
| 1020 del sys.stub_modules[name] | |
|
2826
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
1021 |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
1022 def find_mod_registration(name): |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
1023 """Find module named NAME sys.modules, .stub_modules, or on the stack.""" |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
1024 if sys.stub_modules.has_key(name): |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
1025 return sys.stub_modules[name] # ==> |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
1026 elif sys.modules.has_key(name): |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
1027 return sys.modules[name] # ==> |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
1028 else: |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
1029 if stack.in_process(name): |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
1030 it = stack.mod(name) |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
1031 if it: |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
1032 return it # ==> |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
1033 else: |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
1034 raise ValueError, '%s %s in %s or %s' % (name, # ==X |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
1035 'not registered', |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
1036 'sys.modules', |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
1037 'sys.stub_modules') |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
1038 |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
1039 def get_mod_attrs(theMod, which = None): |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
1040 """Get MODULE object's path, containing-package, and designated path. |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
1041 |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
1042 Virtual attribute USE_PATH is derived from PKG_PATH, MOD_PATHNAME, |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
1043 and/or sys.path, depending on the module type and settings.""" |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
1044 it = theMod.__dict__[IMP_ADMIN] |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
1045 if which: |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
1046 # Load path is either the explicitly designated load path for the |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
1047 # package, or else the directory in which it resides: |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
1048 if which == USE_PATH: |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
1049 if it[PKG_PATH]: |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
1050 # Return explicitly designated path: |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
1051 return it[PKG_PATH] # ==> |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
1052 if it[MOD_PATHNAME]: |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
1053 if it[MOD_TYPE] == PY_PACKAGE: |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
1054 # Return the package's directory path: |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
1055 return [it[MOD_PATHNAME]] # ==> |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
1056 else: |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
1057 # Return the directory where the module resides: |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
1058 return [os.path.split(it[MOD_PATHNAME])[0]] # ==> |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
1059 # No explicitly designated path - use sys.path, eg for system |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
1060 # modules, etc: |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
1061 return sys.path |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
1062 return it[which] # ==> |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
1063 else: |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
1064 return it # ==> |
| 2642 | 1065 |
|
2826
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
1066 def set_mod_attrs(theMod, path, pkg, pkgPath, ty): |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
1067 """Register MOD import attrs PATH, PKG container, and PKGPATH, linking |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
1068 the package container into the module along the way.""" |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
1069 theDict = theMod.__dict__ |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
1070 try: |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
1071 # Get existing one, if any: |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
1072 it = theDict[IMP_ADMIN] |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
1073 except KeyError: |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
1074 # None existing, gen a new one: |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
1075 it = [None] * 4 |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
1076 for fld, val in ((MOD_PATHNAME, path), (MOD_PACKAGE, pkg), |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
1077 (PKG_PATH, pkgPath), (MOD_TYPE, ty)): |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
1078 if val: |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
1079 it[fld] = val |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
1080 |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
1081 theDict[IMP_ADMIN] = it |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
1082 if pkg: |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
1083 theDict[PKG_NM] = theDict[PKG_SHORT_NM] = pkg |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
1084 return it # ==> |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
1085 |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
1086 def format_tb_msg(tb, recursive): |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
1087 """This should be in traceback.py, and traceback.print_tb() should use it |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
1088 and traceback.extract_tb(), instead of print_tb() and extract_tb() having |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
1089 so much redundant code!""" |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
1090 tb_lines, formed = traceback.extract_tb(tb), '' |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
1091 for line in tb_lines: |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
1092 f, lno, nm, ln = line |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
1093 if f[-1 * (len(__name__) + 3):] == __name__ + '.py': |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
1094 # Skip newimp notices - agregious hack, justified only by the fact |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
1095 # that this functionality will be properly doable in new impending |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
1096 # exception mechanism: |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
1097 continue |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
1098 formed = formed + ('\n%s File "%s", line %d, in %s%s' % |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
1099 (((recursive and '*') or ' '), |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
1100 f, lno, nm, |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
1101 ((ln and '\n ' + string.strip(ln)) or ''))) |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
1102 return formed |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
1103 |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
1104 def import_trail_msg(msg, tb, modNm): |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
1105 """Doctor an error message to include the path of the current import, and |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
1106 a sign that it is a circular import, if so.""" |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
1107 return (msg + |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
1108 format_tb_msg(tb, |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
1109 (stack.looped(modNm) and stack.in_process(modNm)))) |
| 2642 | 1110 |
| 1111 def compile_source(sourcePath, sourceFile): | |
| 1112 """Given python code source path and file obj, Create a compiled version. | |
| 1113 | |
| 1114 Return path of compiled version, or None if file creation is not | |
| 1115 successful. (Compilation errors themselves are passed without restraint.) | |
| 1116 | |
| 1117 This is an import-private interface, and not well-behaved for general use. | |
| 1118 | |
| 1119 In particular, we presume the validity of the sourcePath, and that it | |
| 1120 includes a '.py' extension.""" | |
| 1121 | |
| 1122 compiledPath = sourcePath[:-3] + '.pyc' | |
| 1123 try: | |
| 1124 compiledFile = open(compiledPath, 'wb') | |
| 1125 except IOError: | |
|
2826
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
1126 note("write permission denied to " + compiledPath, 3) |
| 2642 | 1127 return None |
| 1128 mtime = os.stat(sourcePath)[8] | |
|
2826
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
1129 |
| 2642 | 1130 try: |
| 2773 | 1131 compiled = compile(sourceFile.read(), sourcePath, 'exec') |
| 1132 except SyntaxError: | |
|
2826
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
1133 # Doctor the exception a bit, to include the source file name in |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
1134 # the report, and then reraise the doctored version. |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
1135 os.unlink(compiledFile.name) |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
1136 sys.exc_value = ((sys.exc_value[0] + ' in ' + sourceFile.name,) |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
1137 + sys.exc_value[1:]) |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
1138 raise sys.exc_type, sys.exc_value # ==X |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
1139 |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
1140 # Ok, we have a valid compilation. |
| 2773 | 1141 try: |
| 2642 | 1142 compiledFile.write(imp.get_magic()) # compiled magic number |
| 1143 compiledFile.seek(8, 0) # mtime space holder | |
| 1144 marshal.dump(compiled, compiledFile) # write the code obj | |
| 1145 compiledFile.seek(4, 0) # position for mtime | |
| 1146 compiledFile.write(marshal.dumps(mtime)[1:]) # register mtime | |
| 1147 compiledFile.flush() | |
| 1148 compiledFile.close() | |
| 1149 return compiledPath | |
| 1150 except IOError: | |
|
2826
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
1151 return None # ==> |
| 2642 | 1152 |
| 1153 | |
| 1154 got_suffixes = None | |
|
2826
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
1155 got_suffixes_dict = {} |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
1156 def get_suffixes(ty=None): |
| 2642 | 1157 """Produce a list of triples, each describing a type of import file. |
| 1158 | |
| 1159 Triples have the form '(SUFFIX, MODE, TYPE)', where: | |
| 1160 | |
| 1161 SUFFIX is a string found appended to a module name to make a filename for | |
| 1162 that type of import file. | |
| 1163 | |
| 1164 MODE is the mode string to be passed to the built-in 'open' function - "r" | |
| 1165 for text files, "rb" for binary. | |
| 1166 | |
| 1167 TYPE is the file type: | |
| 1168 | |
| 1169 PY_SOURCE: python source code, | |
| 1170 PY_COMPILED: byte-compiled python source, | |
| 1171 C_EXTENSION: compiled-code object file, | |
| 1172 PY_PACKAGE: python library directory, or | |
| 1173 SEARCH_ERROR: no module found. """ | |
| 1174 | |
| 1175 # Note: sorted_suffixes() depends on this function's value being invariant. | |
| 1176 # sorted_suffixes() must be revised if this becomes untrue. | |
| 1177 | |
|
2826
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
1178 global got_suffixes, got_suffixes_dict |
| 2642 | 1179 |
|
2826
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
1180 if not got_suffixes: |
| 2642 | 1181 # Ensure that the .pyc suffix precedes the .py: |
| 1182 got_suffixes = [('', 'r', PY_PACKAGE)] | |
|
2826
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
1183 got_suffixes_dict[PY_PACKAGE] = ('', 'r', PY_PACKAGE) |
| 2642 | 1184 py = pyc = None |
| 1185 for suff in imp.get_suffixes(): | |
|
2826
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
1186 got_suffixes_dict[suff[2]] = suff |
| 2642 | 1187 if suff[0] == '.py': |
| 1188 py = suff | |
| 1189 elif suff[0] == '.pyc': | |
| 1190 pyc = suff | |
| 1191 else: | |
| 1192 got_suffixes.append(suff) | |
| 1193 got_suffixes.append(pyc) | |
| 1194 got_suffixes.append(py) | |
|
2826
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
1195 if ty: |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
1196 return got_suffixes_dict[ty] # ==> |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
1197 else: |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
1198 return got_suffixes # ==> |
| 2642 | 1199 |
| 1200 | |
| 1201 sortedSuffs = [] # State vars for sorted_suffixes(). Go | |
| 1202 def sorted_suffixes(): | |
| 1203 """Helper function ~efficiently~ tracks sorted list of module suffixes.""" | |
| 1204 | |
| 1205 # Produce sortedSuffs once - this presumes that get_suffixes does not | |
| 1206 # change from call to call during a python session. Needs to be | |
| 1207 # corrected if that becomes no longer true. | |
| 1208 | |
| 1209 global sortedsuffs | |
| 1210 if not sortedSuffs: # do compute only the "first" time | |
| 1211 for item in get_suffixes(): | |
| 1212 sortedSuffs.append(item[0]) | |
| 1213 # Sort them in descending order: | |
| 1214 sortedSuffs.sort(lambda x, y: (((len(x) > len(y)) and 1) or | |
| 1215 ((len(x) < len(y)) and -1))) | |
| 1216 sortedSuffs.reverse() | |
| 1217 return sortedSuffs | |
| 1218 | |
| 1219 | |
|
2826
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
1220 def normalize_pathname(path): |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
1221 """Given PATHNAME, return an absolute pathname relative to cwd, reducing |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
1222 unnecessary components where convenient (eg, on Unix).""" |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
1223 |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
1224 # We do a lot more when we have posix-style paths, eg os.sep == '/'. |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
1225 |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
1226 if os.sep != '/': |
|
2828
59a9689c11a8
Official version 1.26 directly from Ken
Guido van Rossum <guido@python.org>
parents:
2826
diff
changeset
|
1227 return os.path.join(os.getcwd, path) # ==> |
|
2826
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
1228 |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
1229 outwards, inwards = 0, [] |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
1230 for nm in string.splitfields(path, os.sep): |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
1231 if nm != os.curdir: |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
1232 if nm == os.pardir: |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
1233 # Translate parent-dir entries to outward notches: |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
1234 if inwards: |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
1235 # Pop a containing inwards: |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
1236 del inwards[-1] |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
1237 else: |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
1238 # Register leading outward notches: |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
1239 outwards = outwards + 1 |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
1240 else: |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
1241 inwards.append(nm) |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
1242 inwards = string.joinfields(inwards, os.sep) |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
1243 |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
1244 if (not inwards) or (inwards[0] != os.sep): |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
1245 # Relative path - join with current working directory, (ascending |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
1246 # outwards to account for leading parent-dir components): |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
1247 cwd = os.getcwd() |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
1248 if outwards: |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
1249 cwd = string.splitfields(cwd, os.sep) |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
1250 cwd = string.joinfields(cwd[:len(cwd) - outwards], os.sep) |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
1251 if inwards: |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
1252 return os.path.join(cwd, inwards) # ==> |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
1253 else: |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
1254 return cwd # ==> |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
1255 else: |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
1256 return inwards # ==> |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
1257 |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
1258 |
| 2642 | 1259 # exterior(): Utility routine, obtain local and global dicts of environment |
| 1260 # containing/outside the callers environment, ie that of the | |
| 1261 # caller's caller. Routines can use exterior() to determine the | |
| 1262 # environment from which they were called. | |
| 1263 | |
| 1264 def exterior(): | |
| 1265 """Return dyad containing locals and globals of caller's caller. | |
| 1266 | |
| 1267 Locals will be None if same as globals, ie env is global env.""" | |
| 1268 | |
| 1269 bogus = 'bogus' # A locally usable exception | |
| 1270 try: raise bogus # Force an exception object | |
| 1271 except bogus: | |
| 1272 at = sys.exc_traceback.tb_frame.f_back # The external frame. | |
| 1273 if at.f_back: at = at.f_back # And further, if any. | |
| 1274 globals, locals = at.f_globals, at.f_locals | |
| 1275 if locals == globals: # Exterior is global? | |
| 1276 locals = None | |
| 1277 return (locals, globals) | |
| 1278 | |
| 1279 ######################################################################### | |
| 1280 # TESTING FACILITIES # | |
| 1281 | |
|
2826
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
1282 def note(msg, threshold=2): |
| 2644 | 1283 if VERBOSE >= threshold: sys.stderr.write('(import: ' + msg + ')\n') |
| 2642 | 1284 |
| 1285 class TestDirHier: | |
| 1286 """Populate a transient directory hierarchy according to a definition | |
| 1287 template - so we can create package/module hierarchies with which to | |
| 1288 exercise the new import facilities...""" | |
| 1289 | |
| 1290 def __init__(self, template, where='/var/tmp'): | |
| 1291 """Establish a dir hierarchy, according to TEMPLATE, that will be | |
| 1292 deleted upon deletion of this object (or deliberate invocation of the | |
| 1293 __del__ method).""" | |
| 1294 self.PKG_NM = 'tdh_' | |
| 1295 rev = 0 | |
| 1296 while os.path.exists(os.path.join(where, self.PKG_NM+str(rev))): | |
| 1297 rev = rev + 1 | |
| 1298 sys.exc_traceback = None # Ensure Discard of try/except obj ref | |
| 1299 self.PKG_NM = self.PKG_NM + str(rev) | |
| 1300 self.root = os.path.join(where, self.PKG_NM) | |
| 1301 self.createDir(self.root) | |
| 1302 self.add(template) | |
| 1303 | |
| 1304 def __del__(self): | |
| 1305 """Cleanup the test hierarchy.""" | |
| 1306 self.remove() | |
| 1307 def add(self, template, root=None): | |
| 1308 """Populate directory according to template dictionary. | |
| 1309 | |
| 1310 Keys indicate file names, possibly directories themselves. | |
| 1311 | |
| 1312 String values dictate contents of flat files. | |
| 1313 | |
| 1314 Dictionary values dictate recursively embedded dictionary templates.""" | |
| 1315 if root == None: root = self.root | |
| 1316 for key, val in template.items(): | |
| 1317 name = os.path.join(root, key) | |
| 1318 if type(val) == types.StringType: # flat file | |
| 1319 self.createFile(name, val) | |
| 1320 elif type(val) == types.DictionaryType: # embedded dir | |
| 1321 self.createDir(name) | |
| 1322 self.add(val, name) | |
| 1323 else: | |
|
2826
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
1324 raise ValueError, ('invalid file-value type, %s' % # ==X |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
1325 type(val)) |
| 2642 | 1326 def remove(self, name=''): |
| 1327 """Dispose of the NAME (or keys in dictionary), using 'rm -r'.""" | |
| 1328 name = os.path.join(self.root, name) | |
| 1329 sys.exc_traceback = None # Ensure Discard of try/except obj ref | |
| 1330 if os.path.exists(name): | |
|
2826
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
1331 print '(TestDirHier: eradicating %s)' % name |
| 2642 | 1332 os.system('rm -r ' + name) |
| 1333 else: | |
|
2826
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
1334 raise IOError, "can't remove non-existent " + name # ==X |
| 2642 | 1335 def createFile(self, name, contents=None): |
| 1336 """Establish file NAME with CONTENTS. | |
| 1337 | |
| 1338 If no contents specfied, contents will be 'print NAME'.""" | |
| 1339 f = open(name, 'w') | |
| 1340 if not contents: | |
| 1341 f.write("print '" + name + "'\n") | |
| 1342 else: | |
| 1343 f.write(contents) | |
| 1344 f.close | |
| 1345 def createDir(self, name): | |
| 1346 """Create dir with NAME.""" | |
| 1347 return os.mkdir(name, 0755) | |
| 1348 | |
| 1349 skipToTest = 0 | |
| 1350 atTest = 1 | |
| 1351 def testExec(msg, execList, locals, globals): | |
| 1352 global skipToTest, atTest | |
| 1353 print 'Import Test:', '(' + str(atTest) + ')', msg, '...' | |
| 1354 atTest = atTest + 1 | |
| 1355 if skipToTest > (atTest - 1): | |
| 1356 print ' ... skipping til test', skipToTest | |
| 1357 return | |
| 1358 else: | |
| 1359 print '' | |
| 1360 for stmt in execList: | |
| 1361 exec stmt in locals, globals | |
| 1362 | |
|
2826
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
1363 def test(number=0, leaveHiers=0): |
| 2642 | 1364 """Exercise import functionality, creating a transient dir hierarchy for |
| 1365 the purpose. | |
| 1366 | |
| 1367 We actually install the new import functionality, temporarily, resuming the | |
| 1368 existing function on cleanup.""" | |
| 1369 | |
| 1370 import __builtin__ | |
| 1371 | |
| 1372 global skipToTest, atTest | |
| 1373 skipToTest = number | |
| 1374 hier = None | |
| 1375 | |
| 1376 def unloadFull(mod): | |
| 1377 """Unload module and offspring submodules, if any.""" | |
| 1378 modMod = '' | |
| 1379 if type(mod) == types.StringType: | |
| 1380 modNm = mod | |
| 1381 elif type(mod) == types.ModuleType: | |
| 1382 modNm = modMod.__name__ | |
| 1383 for subj in sys.modules.keys() + sys.stub_modules.keys(): | |
| 1384 if subj[0:len(modNm)] == modNm: | |
| 1385 unload(subj) | |
| 1386 | |
| 1387 try: | |
| 1388 __main__.testMods | |
| 1389 except AttributeError: | |
| 1390 __main__.testMods = [] | |
| 1391 testMods = __main__.testMods | |
| 1392 | |
| 1393 | |
| 1394 # Install the newimp routines, within a try/finally: | |
| 1395 try: | |
| 1396 sys.exc_traceback = None | |
| 1397 wasImport = __builtin__.__import__ # Stash default | |
| 1398 wasPath = sys.path | |
| 1399 except AttributeError: | |
| 1400 wasImport = None | |
| 1401 try: | |
| 1402 hiers = []; modules = [] | |
| 1403 global VERBOSE | |
|
2826
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
1404 wasVerbose, VERBOSE = VERBOSE, 1 |
| 2642 | 1405 __builtin__.__import__ = import_module # Install new version |
| 1406 | |
| 1407 if testMods: # Clear out imports from previous tests | |
| 1408 for m in testMods[:]: | |
| 1409 unloadFull(m) | |
| 1410 testMods.remove(m) | |
| 1411 | |
|
2826
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
1412 # ------ |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
1413 # Test 1 |
| 2642 | 1414 testExec("already imported module: %s" % sys.modules.keys()[0], |
| 1415 ['import ' + sys.modules.keys()[0]], | |
|
2826
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
1416 vars(), newimp_globals) |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
1417 no_sirree = 'no_sirree_does_not_exist' |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
1418 # ------ |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
1419 # Test 2 |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
1420 testExec("non-existent module: %s" % no_sirree, |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
1421 ['try: import ' + no_sirree + |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
1422 '\nexcept ImportError: pass'], |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
1423 vars(), newimp_globals) |
| 2642 | 1424 got = None |
|
2826
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
1425 |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
1426 # ------ |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
1427 # Test 3 |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
1428 # Find a module that's not yet loaded, from a list of prospects: |
| 2642 | 1429 for mod in ['Complex', 'UserDict', 'UserList', 'calendar', |
| 1430 'cmd', 'dis', 'mailbox', 'profile', 'random', 'rfc822']: | |
| 1431 if not (mod in sys.modules.keys()): | |
| 1432 got = mod | |
| 1433 break # ==v | |
| 1434 if got: | |
| 1435 testExec("not-yet loaded module: %s" % mod, | |
| 1436 ['import ' + mod, 'modules.append(got)'], | |
|
2826
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
1437 vars(), newimp_globals) |
| 2642 | 1438 else: |
|
2826
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
1439 testExec("not-yet loaded module: list exhausted, never mind", |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
1440 [], vars(), newimp_globals) |
| 2642 | 1441 |
| 1442 # Now some package stuff. | |
|
2826
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
1443 |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
1444 # ------ |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
1445 # Test 4 |
| 2642 | 1446 # First change the path to include our temp dir, copying so the |
| 1447 # addition can be revoked on cleanup in the finally, below: | |
| 1448 sys.path = ['/var/tmp'] + sys.path[:] | |
| 1449 # Now create a trivial package: | |
| 1450 stmts = ["hier1 = TestDirHier({'a.py': 'print \"a.py executing\"'})", | |
| 1451 "hiers.append(hier1)", | |
|
2826
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
1452 "base = hier1.PKG_NM", |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
1453 "exec 'import ' + base", |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
1454 "testMods.append(base)"] |
| 2642 | 1455 testExec("trivial package, with one module, a.py", |
|
2826
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
1456 stmts, vars(), newimp_globals) |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
1457 |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
1458 # ------ |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
1459 # Test 5 |
| 2642 | 1460 # Slightly less trivial package - reference to '__': |
| 1461 stmts = [("hier2 = TestDirHier({'ref.py': 'print \"Pkg __:\", __'})"), | |
|
2826
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
1462 "base = hier2.PKG_NM", |
| 2642 | 1463 "hiers.append(hier2)", |
|
2826
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
1464 "exec 'import ' + base", |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
1465 "testMods.append(base)"] |
| 2642 | 1466 testExec("trivial package, with module that has pkg shorthand ref", |
|
2826
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
1467 stmts, vars(), newimp_globals) |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
1468 |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
1469 # ------ |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
1470 # Test 6 |
| 2642 | 1471 # Nested package, plus '__' references: |
| 1472 | |
| 1473 complexTemplate = {'ref.py': 'print "ref.py loading..."', | |
| 1474 'suite': {'s1.py': 'print "s1.py, in pkg:", __', | |
| 1475 'subsuite': {'sub1.py': | |
| 1476 'print "sub1.py"'}}} | |
| 1477 stmts = [('print """%s\n%s\n%s\n%s\n%s\n%s"""' % | |
| 1478 ('.../', | |
| 1479 ' ref.py\t\t\t"ref.py loading..."', | |
| 1480 ' suite/', | |
| 1481 ' s1.py \t\t"s1.py, in pkg: xxxx.suite"', | |
| 1482 ' subsuite/', | |
| 1483 ' sub1.py "sub1.py" ')), | |
| 1484 "hier3 = TestDirHier(complexTemplate)", | |
|
2826
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
1485 "base = hier3.PKG_NM", |
| 2642 | 1486 "hiers.append(hier3)", |
|
2826
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
1487 "exec 'import ' + base", |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
1488 "testMods.append(base)"] |
| 2642 | 1489 testExec("Significantly nestled package:", |
|
2826
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
1490 stmts, vars(), newimp_globals) |
| 2642 | 1491 |
|
2826
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
1492 # ------ |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
1493 # Test 7 |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
1494 # Try an elaborate hierarchy which includes an __init__ master in one |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
1495 # one portion, a ref across packages within the hierarchies, and an |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
1496 # indirect recursive import which cannot be satisfied (and hence, |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
1497 # prevents load of part of the hierarchy). |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
1498 complexTemplate = {'mid': |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
1499 {'prime': |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
1500 {'__init__.py': 'import __.easy, __.nother', |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
1501 'easy.py': 'print "easy.py:", __name__', |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
1502 'nother.py': ('%s\n%s\n%s\n' % |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
1503 ('import __.easy', |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
1504 'print "nother got __.easy"', |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
1505 # __.__.awry should be found but |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
1506 # should not load successfully, |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
1507 # disrupting nother, but not easy |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
1508 'import __.__.awry'))}, |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
1509 # continuing dict 'mid': |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
1510 'awry': |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
1511 {'__init__.py': |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
1512 ('%s\n%s' % |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
1513 ('print "got " + __name__', |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
1514 'from __ import *')), |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
1515 # This mutual recursion (b->a, a->d->b) should be |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
1516 # ok, since a.py sets ax before recursing. |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
1517 'a.py': 'ax = 1; from __.b import bx', |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
1518 'b.py': 'bx = 1; from __.a import ax'}}} |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
1519 stmts = ["hier5 = TestDirHier(complexTemplate)", |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
1520 "base = hier5.PKG_NM", |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
1521 "testMods.append(base)", |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
1522 "hiers.append(hier5)", |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
1523 "exec 'import %s.mid.prime' % base", |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
1524 "print eval(base)", # Verify the base was bound |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
1525 "testMods.append(base)"] |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
1526 testExec("Elaborate, clean hierarchy", |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
1527 stmts, vars(), newimp_globals) |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
1528 |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
1529 # ------ |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
1530 # test 8 |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
1531 # Here we disrupt the mutual recursion in the mid.awry package, so the |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
1532 # import should now fail. |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
1533 complexTemplate['mid']['awry']['a.py'] = 'from __.b import bx; ax = 1' |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
1534 complexTemplate['mid']['awry']['b.py'] = 'from __.a import ax; bx = 1' |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
1535 stmts = ["hier6 = TestDirHier(complexTemplate)", |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
1536 "base = hier6.PKG_NM", |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
1537 "testMods.append(base)", |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
1538 "hiers.append(hier6)", |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
1539 "work = ('import %s.mid.prime' % base)", |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
1540 ("try: exec work" + |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
1541 "\nexcept ImportError: print ' -- import failed, as ought'" + |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
1542 "\nelse: raise SystemError, sys.exc_value"), |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
1543 "testMods.append(base)"] |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
1544 testExec("Elaborate hier w/ deliberately flawed import recursion", |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
1545 stmts, vars(), newimp_globals) |
| 2642 | 1546 |
| 1547 sys.exc_traceback = None # Signify clean conclusion. | |
| 1548 | |
| 1549 finally: | |
| 1550 skipToTest = 0 | |
| 1551 atTest = 1 | |
| 1552 sys.path = wasPath | |
|
2826
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
1553 VERBOSE = wasVerbose |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
1554 if wasImport: # Resurrect prior routine |
| 2642 | 1555 __builtin__.__import__ = wasImport |
| 1556 else: | |
| 1557 del __builtin__.__import__ | |
|
2826
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
1558 if leaveHiers: |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
1559 print 'Cleanup inhibited' |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
1560 else: |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
1561 if sys.exc_traceback: |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
1562 print ' ** Import test FAILURE... cleanup.' |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
1563 else: |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
1564 print ' Import test SUCCESS... cleanup' |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
1565 for h in hiers: h.remove(); del h # Dispose of test directories |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
1566 |
|
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
1567 init() |
|
2643
d9e4cbba958c
call test() when run as script
Guido van Rossum <guido@python.org>
parents:
2642
diff
changeset
|
1568 |
|
d9e4cbba958c
call test() when run as script
Guido van Rossum <guido@python.org>
parents:
2642
diff
changeset
|
1569 if __name__ == '__main__': |
|
2826
cd9e6135244a
something close to Ken's latest version ...
Guido van Rossum <guido@python.org>
parents:
2773
diff
changeset
|
1570 test() |
