[Python-Dev] Fix import errors to have data
Tim Peters
tim.peters at gmail.com
Tue Aug 3 05:14:12 CEST 2004
[Christian Tismer]
...
> Yes, but I think you are almost an atom's distance apart off of
> the final solution. Grab it!
If you mean this:
>>> Maybe it could be as simple as saving a snapshot of
>>> sys.modules whenever importing of a module is begun,
>>> and if execution of its body doesn't complete, restoring
>>> the snapshot?
then no, that's a long way off in CPython. There's no choke point for
when importing begins, or for when importing ends, and even
__builtin__.__import__ is routinely replaced. Guido latched on to the
only choke point there is: sooner or later, every import gimmick
worthy of the name has to execute "the module's" code, whether it be
direct import from Python, directly via C API calls, implicit package
__init__.py imports, imports arranged via magical importers (like the
.zip importer), etc. So that's what the patch targeted: there's one
routine that executes a module's initialization code, all imports go
thru it eventually, and that's the routine that now removes the
module's name from sys.modules if the initialization code barfs.
"The rest" of import logic is sprayed all over creation. To hook
"begin import" and "end import" first requires that all importers be
changed to have a formal notion of those events. The easiest start
would be to use bytecodehacks to inject "call begin_import" and "call
end_import" opcode sequences around the code generated for import
statements <0.8 wink>.
More information about the Python-Dev
mailing list