[SciPy-dev] gui_thread and wxPython-2.5.2.8

Fernando Perez Fernando.Perez at colorado.edu
Fri Oct 1 11:54:39 CDT 2004


Prabhu Ramachandran schrieb:

> I have a question that you might be able to help with.  I want to do
> this:
> 
>     Create a module that shadows an existing module in the filesystem.
>     Lets say, site-packages/wx/__init__.py.  I want to create a new
>     module with code that does something else (and not have to create
>     a file!).  Like say: "def f(): print 'hello world'", call that the "wx"
>     module, and make Python believe nothing is wrong (insert it into
>     sys.modules or whatever) then do a 'reload(wx)' to re-read the
>     real __init__.py just like normal.  I'd like the code to be simple
>     and not have to use complex import hooks.
> 
> Is this possible?  If it is I could probably eliminate the wxPython
> patch.

I think it is possible.  You can fool python's idea of what a module is quite 
easily:

In [14]: sys.modules['code'] = sys.modules['time']

In [15]: code.<TAB>
code.accept2dyear  code.daylight      code.strftime      code.tzname
code.altzone       code.gmtime        code.strptime      code.tzset
code.asctime       code.localtime     code.struct_time
code.clock         code.mktime        code.time
code.ctime         code.sleep         code.timezone

These are all time's functions :)

In [15]: reload code
-------> reload(code)
Out[15]: <module 'time' from '/usr/lib/python2.3/lib-dynload/timemodule.so'>

So I think what you have in mind should work.  My only concern is that you 
seem to want to reload the _original_ wx/__init__.py, so I'm not sure I 
understand correclty where you want to do the trapping.  But overall, I think 
this kind of game can _definitely_ be played from the outside of Wx.  This is 
one of the benefits of python's extremely strong dynamic nature.

Best,

f




More information about the Scipy-dev mailing list