Altering imported modules

Discussion in 'Python' started by Tro, Mar 1, 2008.

  1. Tro

    Tro Guest

    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.

    Thanks,
    Tro
     
    Tro, Mar 1, 2008
    #1
    1. Advertising

  2. On Mar 1, 11:56 pm, Tro <> wrote:
    > I'd like to know if it's possible to make tlslite load *my* asyncore module
    > without changing any of the tlslite code.


    the pkgutil module might be helpful, not sure though as I've never
    used it myself.

    http://blog.doughellmann.com/2008/02/pymotw-pkgutil.html is a fairly
    detailed look at what pkgutil can do.

    Regards
    Floris
     
    Floris Bruynooghe, Mar 4, 2008
    #2
    1. Advertising

  3. Tro

    Tro Guest

    On Tuesday 04 March 2008, Floris Bruynooghe wrote:
    > On Mar 1, 11:56 pm, Tro <> wrote:
    > > I'd like to know if it's possible to make tlslite load *my* asyncore
    > > module without changing any of the tlslite code.

    >
    > the pkgutil module might be helpful, not sure though as I've never
    > used it myself.
    >
    > http://blog.doughellmann.com/2008/02/pymotw-pkgutil.html is a fairly
    > detailed look at what pkgutil can do.


    Thanks, that's neat.

    Tro
     
    Tro, Mar 5, 2008
    #3
  4. 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


    HTH
     
    Bruno Desthuilliers, Mar 5, 2008
    #4
  5. Tro

    Tro Guest

    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
     
    Tro, Mar 6, 2008
    #5
  6. Tro

    Guest

    On 6 mar, 21:29, Tro <> wrote:
    > On Wednesday 05 March 2008, Bruno Desthuilliers wrote:
    >
    >
    >
    > > Tro a écrit :

    (snip)
    > > > 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,


    One of us must be missing something here. The monkeypatch way is:

    # -- mypatch.py --
    import BaseModule
    # backup original if we want to call it
    _original_something = BaseModule.something

    def my_patched_something(args):
    my_code_here_doing_stuff
    # eventually
    _original_something(args)
    more_code_here

    BaseModule.something = my_patched_something

    # -- main.py --

    import mypatch
    # From now on, any other module calling
    # BaseModule.something will call my_patched_something.

    import other_module_using_BaseModule

    app_code_here


    > 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.


    Unless there are some really strange things happening in tlslite and
    asyncore, if you import your monkeypatch before importing tlslite, you
    shouldn't have to touch anything in tlslite. Which is the whole point
    of monkeypatching.
     
    , Mar 7, 2008
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jason

    namespace & imported modules

    Jason, Nov 24, 2004, in forum: Python
    Replies:
    3
    Views:
    360
    Peter Otten
    Nov 25, 2004
  2. Tomasz Lisowski
    Replies:
    4
    Views:
    356
    Tomasz Lisowski
    May 25, 2005
  3. Tro
    Replies:
    5
    Views:
    224
    Steve Holden
    Mar 4, 2008
  4. Dun Peal
    Replies:
    10
    Views:
    460
    Chris Rebert
    May 3, 2011
  5. Volker Nicolai
    Replies:
    9
    Views:
    966
    Fabian Pilkowski
    Jul 4, 2005
Loading...

Share This Page