How to exchange parts of builtin moudules/methods permanently using C-API

Wolfgang Draxinger wdraxinger at darkstargames.de
Fri Jul 12 18:57:56 EDT 2002


I want to change sys.stdout, sys.stdin, sys.stderr permanently, so I 
replace them using e.g.
PyObject_SetAttr(sys_module, "stdout", my_stdout)

that work fine until a script does a

del sys
import sys
reload(sys)

After that my override is not longer valid.
Another problem I have, is to prevent specific modules from 
loading/importing.
Say I loaded a module spam, that conflicts internally with eggs or even 
a builtin module like os. How can I prevent those modules from loading 
in a elegant way? My idea was to set __builtins__.__import__ to a custom 
load function, that checks the name of the module and imports it, if it 
is not on the exclusion list. However I'm not sure if this cannot be 
overriden in some way and I wonder, if there is some C-API function lets 
me hook into the import process.
-- 
+------------------------------------------------+
| +----------------+ WOLFGANG DRAXINGER          |
| | ,-.   DARKSTAR | lead programmer             |
| |(   ) +---------+ wdraxinger at darkstargames.de |
| | `-' / GAMES /                                |
| +----+''''''''     http://www.darkstargames.de |
+------------------------------------------------+




More information about the Python-list mailing list