Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #35052

Re: Is it possible monkey patch like this?

From Peter Otten <__peter__@web.de>
Subject Re: Is it possible monkey patch like this?
Date 2012-12-18 13:35 +0100
Organization None
References <bbfb8154-e283-4961-9680-9d819bf00d01@googlegroups.com>
Newsgroups comp.lang.python
Message-ID <mailman.1017.1355834069.29569.python-list@python.org> (permalink)

Show all headers | View raw


Marc Aymerich wrote:

> Dear all,
> I want to monkey patch a method that has lots of code so I want to avoid
> copying all the original method for changing just two lines. The thing is
> that I don't know how to do this kind of monkey patching.
> 
> Consider the following code:
> 
> class OringinalClass(object):
>     def origina_method(self, *args, **kwargs):
>         ...
>         if some_condition(): # This condition should be changed
>             raise SomeException
>         ...
>         if some_condition():
>             ...
>             #if some_condition(local_variable): # This condition should be
>             #added
>             #    raise SomeException
>         ...
> 
> 
> Is it possible to tell Python to run the original method without stopping
> when an exception is raised? 

No.

> so I can catch them on a wrapper method and
> apply my conditional there.
> 
> Any other idea on how to monkey patch those two conditionals ?

One of the cleanest alternatives is to factor out the condition in the 
original class and then use a subclass:

class Original:
    def method(self, *args, **kw):
        self.check_condition(...)
        ...
    def check_condition(self, ...):
        if condition:
            raise SomeException

class Sub(Original):
    def check_condition(self, ...):
        pass

If you insist on monkey-patching possible solutions depend on the actual 
conditions. If some_condition() is a function, replace that function. If it 
is actually an expression tweak the arguments. E. g:

>>> class Original:
...     def method(self, x):
...             if x < 0: raise ValueError
...             print x * x
... 
>>> Original().method(-2)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 3, in method
ValueError
>>> class Int(int):
...     def __lt__(self, other): return False # a blunt lie
... 
>>> Original().method(Int(-2))
4

This tends to get complex quickly, so in the long run you will not be happy 
with that approach...

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Is it possible monkey patch like this? Marc Aymerich <glicerinu@gmail.com> - 2012-12-18 02:26 -0800
  Re: Is it possible monkey patch like this? Chris Angelico <rosuav@gmail.com> - 2012-12-18 21:34 +1100
  Re: Is it possible monkey patch like this? Peter Otten <__peter__@web.de> - 2012-12-18 13:35 +0100
  Re: Is it possible monkey patch like this? Terry Reedy <tjreedy@udel.edu> - 2012-12-18 11:49 -0500
  Re: Is it possible monkey patch like this? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-12-19 00:54 +0000

csiph-web