Questions tagged [monkeypatching]

Dynamically modifying run-time behavior by replacing program elements with new program elements

Monkeypatching, in late-bound languages, refers to modifying run-time behavior by replacing program elements with new program elements.

Examples include replacing a method on a class with a new function, or patching a member of a class.

The term may have a pejorative connotation, as the technique is usually used on third-party code.

For more discussion, see "Monkeypatching for Humans" on Coding Horror.

949 questions
23
votes
8 answers

How can I monkey-patch an instance method in Perl?

I'm trying to monkey-patch (duck-punch :-) a LWP::UserAgent instance, like so: sub _user_agent_get_basic_credentials_patch { return ($username, $password); } my $agent = LWP::UserAgent->new(); $agent->get_basic_credentials =…
cdleary
  • 69,512
  • 53
  • 163
  • 191
22
votes
5 answers

Why is it frowned upon to modify JavaScript object's prototypes?

I've come across a few comments here and there about how it's frowned upon to modify a JavaScript object's prototype? I personally don't see how it could be a problem. For instance extending the Array object to have map and include methods or to…
colourandcode
  • 431
  • 1
  • 6
  • 13
22
votes
4 answers

How To Run Arbitrary Code After Django is "Fully Loaded"

I need to perform some fairly simple tasks after my Django environment has been "fully loaded". More specifically I need to do things like Signal.disconnect() some Django Signals that are setup by my third party library by default and connect my own…
Chris W.
  • 37,583
  • 36
  • 99
  • 136
22
votes
9 answers

Is it possible to monkey patch in Java?

I don't want to discuss the merits of this approach, just if it is possible. I believe the answer to be "no". But maybe someone will surprise me! Imagine you have a core widget class. It has a method calculateHeight(), that returns a height. The…
richq
  • 55,548
  • 20
  • 150
  • 144
22
votes
3 answers

Python: mock patch a module wherever it is imported from

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…
adam.ra
  • 1,068
  • 1
  • 10
  • 16
20
votes
3 answers

How to monkey patch Django?

I came upon this post on monkey patching Django: from django.contrib.auth.models import User User.add_to_class('openid', models.CharField(max_length=250,blank=True)) def get_user_name(self): if self.first_name or self.last_name: return…
Continuation
  • 12,722
  • 20
  • 82
  • 106
20
votes
6 answers

Is it possible to replace (monkeypatch) PHP functions?

You can do this in Python, but is it possible in PHP? >>> def a(): print 1 ... >>> def a(): print 2 ... >>> a() 2 e.g.: Fatal error: Cannot redeclare var_dump() in /tmp/- on line 1
gak
  • 32,061
  • 28
  • 119
  • 154
20
votes
1 answer

How to mock a function, that is imported within an imported method from different module

I got the following function to test: my_package.db_engine.db_functions.py: from ..utils import execute_cmd from my_package.db_engine.db_functions import dbinfo def dbinfo(db_name): params = (cmd_cfg.DB, add_pj_suffix(db_name)) cmd =…
Igl3
  • 4,900
  • 5
  • 35
  • 69
20
votes
6 answers

How to monkey-patch code that gets auto-loaded in Rails?

I'm monkey-patching a Rails engine with something like: SomeClass.class_eval do # ... end The first time I hit the web site, on development mode at least, it works, but the second time it's like my patch never existed. I presume it's Rails…
Pablo Fernandez
  • 279,434
  • 135
  • 377
  • 622
20
votes
1 answer

How to mock a decorated function

For testing reasons, I need to be able to mock the inner/original function of a decorated one which is used somewhere else: In mydecorator.py: def my_decorator(f): def wrapped_f(): print "decorated" f() return…
20
votes
5 answers

gevent monkey-patching and breakpoints

I have been playing with Gevent, and I like it a lot. However I have run into a problem. Breakpoint are not being hit, and debugging doesn't work (using both Visual Studio Python Tools and Eclipse PyDev). This happens after monkey.patch_all() is…
Flavien
  • 7,497
  • 10
  • 45
  • 52
19
votes
2 answers

pytest modules using os.environ - How do I test it correctly?

currently I am writing some Webapp, but this time I want to learn how to write proper tests for it (using pytest) :) A very common pattern I often see is to make the default configuration changeable using environment variables. Currently I am…
spky
  • 2,123
  • 2
  • 17
  • 21
19
votes
3 answers

Add functionality to Django FlatPages without changing the original Django App

I would like to add a field to the Django FlatPage database model, but I do not really know how to extend this without editing the original application. What I want to do is to add the following field to the model: from django.db import models from…
DjangoNewbe
18
votes
3 answers

Patch routine call in delphi

I want to patch a routine call to be able to handle it myself with some modifications. I am writing a resource loader. I want to patch the Delphi's LoadResourceModule and InitInheritedComponent routines with that of mine. I have checked PatchAPI…
Rahul W
  • 833
  • 11
  • 26
18
votes
5 answers

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

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…
Grim
  • 2,398
  • 4
  • 35
  • 54
1 2
3
63 64