experimental patch to allow importing from file-like objects
Delaney, Timothy
tdelaney at avaya.com
Sun Mar 3 18:30:49 EST 2002
> From: gerson.kurz at t-online.de [mailto:gerson.kurz at t-online.de]
>
> # redirect import hook ******************************
> old_import_func = __import__
>
> def my_import_func(name, globals=None, locals=None, fromlist=None):
> global old_import_func
>
> if name == "encrypted_test":
> return imp.load_module(name,
> my_file_class("test.raw"),
> "test.raw",
> ('.raw','rb',imp.PY_FILEOBJECT_SOURCE))
>
>
> # use original code
> return old_import_func( name, globals, locals, fromlist )
>
> __builtins__.__import__ = my_import_func
One thing you may like to do (but don't hurry)
# redirect import hook ******************************
def my_import_func(name, globals=None, locals=None, fromlist=None,
old_import_func=__import__):
if name == "encrypted_test":
which has three advantages and one disadvantage:
A1: avoids polluting your namespace
A2: prevents other code from rebinding old_import_func
A3: old_import_func will exist in the locals of the function, leading to
faster lookup
D1: It is possible (but highly unlikely) that someone could call your
function (either directly or indirectly via __builtins__.__import__) with
the wrong arguments.
Tim Delaney
More information about the Python-list
mailing list