2.2 open

Delaney, Timothy tdelaney at avaya.com
Sun Mar 24 23:05:42 EST 2002


> From: Greg Ewing [mailto:greg at cosc.canterbury.ac.nz]
> 
> Robin Becker wrote:
> > 
> > So the question now is, how do I tell if __import__ is the 
> built in one
> > even if the import system is not fully initialized.
> 
> You could compare its type with what you really
> should have been comparing it with in the
> first place:
> 
>   import types
>   if type(__import__) == types.BuiltinFunctionType:
>     ...

Except that this will only tell you that it's of the same type as the
original __import__ - someone still may have replaced the builtin __import__
before you get a go at it.

In general, it shouldn't matter whether it is the builtin __import__ or not
- why do you need to know? If you wish to replace it with your own version,
you should just capture the original version and replace it with your own.
To do this in a thread-safe manner you should probably use the canonical
"swap" idiom ...

import __builtin__

def _my_import (name, globals, locals, fromlist):
    pass

_orig_import, __builtin__.__import__ = __builtin__.__import__, _my_import

print _orig_import
print __builtin__.__import__

Tim Delaney




More information about the Python-list mailing list