Altering imported modules

Tro troworld at gmail.com
Thu Mar 6 15:29:05 EST 2008


On Wednesday 05 March 2008, Bruno Desthuilliers wrote:
> Tro a écrit :
> > Hi, list.
> >
> > I've got a simple asyncore-based server. However, I've modified the
> > asyncore module to allow me to watch functions as well as sockets. The
> > modified asyncore module is in a specific location in my project and is
> > imported as usual from my classes.
> >
> > Now I'd like to use the tlslite library, which includes an asyncore mixin
> > class. However, tlslite imports "asyncore", which doesn't include my own
> > modifications.
> >
> > I'd like to know if it's possible to make tlslite load *my* asyncore
> > module without changing any of the tlslite code.
>
> Not sure this apply to your case (depends on how asyncore is implemented
> and the exact "modifications"), but monkeypatching is a possible solution.
>
> http://en.wikipedia.org/wiki/Monkey_patch
> http://wiki.zope.org/zope2/MonkeyPatch
> http://mail.python.org/pipermail/python-dev/2008-January/076194.html

Ooh. I didn't know this technique had a name. But that's basically how I'm 
modifying the built-in asyncore in my own class. I override its poll() and 
poll2() methods to do something extra.

However, the issue with tlslite is that it imports the regular asyncore 
instead of the one I wrote, which means that I'd have to somehow monkeypatch 
its "import" statement. I'm guessing that means simply monkeypatching both 
the original asyncore module AND the tlslite module's asyncore attribute with 
my own version.

Thanks,
Tro



More information about the Python-list mailing list