monkeypatching

Monkey patch __del__ to new function

谁说胖子不能爱 提交于 2019-12-06 20:14:44
问题 For specific debugging purposes I'd like to wrap the del function of an arbitrary object to perform extra tasks like write the last value of the object to a file. Ideally I want to write monkey(x) and it should mean that the final value of x is printed when x is deleted Now I figured that del is a class method. So the following is a start: class Test: def __str__(self): return "Test" def p(self): print(str(self)) def monkey(x): x.__class__.__del__=p a=Test() monkey(a) del a However if I want

How to create new closure cell objects?

巧了我就是萌 提交于 2019-12-06 14:06:30
I need to monkey-patch my library to replace an instance of a symbol, and it's getting referenced by some function closures. I need to copy those functions (since I also need access to original unpatched version of the function as well), but __closure__ is immutable, and I can't copy.copy it, so how can I create new closure cells objects in Python 2.7? I for example given this function def f(): def incorrectfunction(): return 0 def g(): return incorrectfunction() return g def correctfunction(): return 42 func = f() patched_func = patchit(f) # replace "incorrectfunction" print func(), patched

Python: mock patch a module wherever it is imported from

流过昼夜 提交于 2019-12-06 07:33:46
问题 I need to make sure that running unit tests won't trigger calling a heavy outer world function, say, this one: # bigbad.py def request(param): return 'I searched the whole Internet for "{}"'.format(param) Multiple modules use this function (bigbad.request) and they import it differently (in real-life it may be imported from an external library as well). Say, there are two modules, a and b, where b depends on a and both use the function: # a.py, from...import from bigbad import request def

Python: Monkeypatching a method of an object

我是研究僧i 提交于 2019-12-06 05:09:15
I'm hitting an API in python through requests' Session class. I'm making GET & POST method call using requests.Session(). On every call(GET/POST) failure, I want to notify another process. I can do this by creating a utility method as follows: s = request.Session() def post(): try: s.post(URL,data,headers) except: notify_another_process() And call this method instead of requests.Session().post directly. But, I want to monkeypatch this code to requests.Session().post and want the additional functionality of notifying the other process in the requests.Session().post method call itself. How can I

Why python's monkeypatch doesn't work when importing a class instead of a module?

大城市里の小女人 提交于 2019-12-05 12:05:44
问题 I was having issues while using the code of the accepted answer here. The code works depending on how I do the import of datetime. Why is that? Is it possible to mock it so it works both ways? I am using Python 3.4 . The following code illustrates the problem: import pytest from datetime import datetime mockdate = datetime(2000, 1, 1, 0, 0, 0) @pytest.fixture(autouse=True) def patch_datetime_now(monkeypatch): class mydatetime: @classmethod def now(cls): return mockdate monkeypatch.setattr(

How to monkeypatch a static method? [duplicate]

风格不统一 提交于 2019-12-05 07:13:24
This question already has answers here : Pointers to static methods in Python (3 answers) Closed 6 years ago . While it's fairly simple to monkeypatch instance methods to classes, e.g. class A(object): pass def a(self): print "a" A.a = a doing this with another class's @staticmethod à la class B(object): @staticmethod def b(): print "static b" A.b = B.b results in A.b() yielding a TypeError : unbound method b() must be called with A instance as first argument (got nothing instead) Make A.b a static method and you should be fine: A.b = staticmethod(B.b) 来源: https://stackoverflow.com/questions

Python monkey patch private function

徘徊边缘 提交于 2019-12-05 06:44:08
I have a module with a function (call it a() ) that calls another function defined in the same module (call it __b() ). __b() is a function which speaks to a website via urllib2 and gets some data back. Now I'm trying to test a() , but of course would rather not have my unit tests speak to the public internet. Thus, I'm thinking if I can monkey patch __b() with a function which returns canned data, then I can write the tests for a() . To be more concrete, my module looks kinda like: def a(): return __b("someval") def __b(args): return something_complex_with_args So now I want to test a() , but

Duck punching in a property in python

允我心安 提交于 2019-12-05 00:37:13
问题 I'd like to be able to add a property http://docs.python.org/library/functions.html#property to an object (a specific instance of a class). Is this possible? Some other questions about duck punching/monkey patching in python: Adding a Method to an Existing Object Instance Python: changing methods and attributes at runtime UPDATE: Answered by delnan in the comments Dynamically adding @property in python 回答1: Following code works : #!/usr/bin/python class C(object): def __init__(self): self._x

MonkeyPatching ActiveJobs

雨燕双飞 提交于 2019-12-04 13:56:42
I am having an issue monkey-patching part of ActiveJobs. I have the following code in config/initializers/extensions/arguements.rb module ActiveJob module Arguments TYPE_WHITELIST = [ Date, DateTime, Time, NilClass, Fixnum, Float, String, TrueClass, FalseClass, Bignum ] end end Basically, I am trying to add basic support for Date/Time objects for use in the ActiveJob created by ActionMailer#deliver_later Upon loading the rails app I can see my whitelist is loaded, however when I call the deliver_later method on a mailer the original whitelist overrides my patch. #List is correct when app loads

Is it possible to do monkey patching in Java, if not is there an alternative?

≡放荡痞女 提交于 2019-12-04 03:14:13
This was asked 8 years ago here and since then 8 years have passed. I wanted to ask this question again to see if anyone has developed a framework, tool or library that does monkey patching. Basically what I need it for is a java application that I applied my own patch to. Since this project is maintained by another team I want to be able to keep/apply any patch I make, to the patches they make. Rich There are a number of techniques that might be applicable here, but your question is too vague to narrow them down to a single answer. "Monkey patching" in the literal sense that it is used in