2.2 open

Robin Becker robin at jessikat.fsnet.co.uk
Mon Mar 25 04:02:48 EST 2002


In article <mailman.1017029236.6829.python-list at python.org>, Delaney,
Timothy <tdelaney at avaya.com> writes
.......
>>   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
>
Gordon McMillan's original archive_rt had code to detect the original so
it could apply its startup. It's reasonable to suppose that this is
quite hard since I am attempting to do this in the middle of starting up
Python itself and as I have a compressed archive I can't gurantee access
to stuff like warning.py types.py etc at the time of the test. There's
probably a more direct way to test this though since I know exactly when
the test is taking place and should succeed. 
-- 
Robin Becker



More information about the Python-list mailing list