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
-1
votes
1 answer

Monkey Patching in Go show different result when running from CLI

I am using Monkey Patching in Go. When I debug the following code in VSCode it shows that the function proc.Signal return the error programmed. func TestCheckProcessRunning(t *testing.T) { monkey.Patch((*os.Process).Signal, func(p *os.Process,…
-1
votes
1 answer

AttributeError when using Pytest monkeypatch to change fixture object attribute

In my pytest file test_foo.py, I am trying to change the value of foo.path to mock_path using monkeypatch. However, when I try the following, I get an error ERROR test_foo.py::test_foo - AttributeError: 'foo' has no attribute 'path' What should be…
Athena Wisdom
  • 6,101
  • 9
  • 36
  • 60
-1
votes
1 answer

How to dynamically set an object's method at runtime (when the object is an instance of a Gym environment)?

I want to override an instance's method. Let's call this instance/object env. The instance method has a method with the signature f(self, other_parameter), which is actually not publically accessible (not sure why, but you will see below that it's…
nbro
  • 15,395
  • 32
  • 113
  • 196
-1
votes
1 answer

Mocking requests.post

This is my first time writing unit tests, apologies for the annoyances inevitably present, despite my best efforts. I am trying to mock requests.post but my test function is not having the desired effect, to induce a 404 status code so that I can…
samsmug
  • 35
  • 10
-1
votes
1 answer

Injecting values into all link_to method calls in Ruby on Rails

I have a strange requirement that I need to inject a value at the beginning of all local links. This is a legacy app and is quite large so I'm looking to do it under the hood, maybe with a monkey patch. Basically if I have a link_to "Go to…
-1
votes
1 answer

How to monkey patch class included module?

I've got a gem with a class that dynamically includes modules. I need to override all methods in those modules to insert some additional logic (for metrics tracking) Example class: module MyAPI class Client # This dynamically requires a bunch…
Sean
  • 1,078
  • 14
  • 25
-1
votes
1 answer

How to log any time any method has been monkey patched in Rails

I would like to print the class and method any time a method gets redefined in a Rails app, even if it happens in an included gem. I understand there is a hook called method_added that gets called back on redefining a method, but I do not know how…
marathon
  • 7,881
  • 17
  • 74
  • 137
-1
votes
1 answer

I need to plug/unplug devise gem at run time in my rails app

I have a condition where my rails app can be requested from different domains. Suppose it can be accessed via domain www.abc.com and www.xyz.com Now my requirement is that, I need to have devise module include when application is accessed via…
MayankS
  • 446
  • 1
  • 3
  • 17
-1
votes
1 answer

Unit testing with mock and patch

I am writing unit tests for a function, f, which imports other functions/classes which the unit test does not (directly) interact with. Is there any way I can patch those functions from the unit test (maybe in set_up())? For reference, I am using…
PK5144
  • 39
  • 5
-1
votes
2 answers

Monkey Patching Arrays in Ruby

I added my own method to the Array class that does the same thing as Array#uniq. This is my version: arr = ["fun", "sun", 3, 5, 5, 5, 1, 2, 1, "fun"] class Array def my_uniq new_arr = [] each do |item| new_arr <<…
Thrynn
  • 199
  • 2
  • 2
  • 11
-1
votes
1 answer

Monkey patch cursor.execute() and close() or not?

"Trying to unit test my code using unittest.mock python library". I have code which is running database queries very similar to this: app.py: from flask import g import mysql.connector @app.route('/') def create_table(): …
immrsteel
  • 1,333
  • 4
  • 13
  • 18
-1
votes
2 answers

how seaborn changes the behavior of pyplot by just importing the package?

I am very curious to know how seaborn changes the behavior of matplotlib functions by just import seaborn as sns. I want to realize the same function to change the behavior of imshow() function in pyplot, for example, i want to show the pixel value…
shelper
  • 10,053
  • 8
  • 41
  • 67
-1
votes
1 answer

Adding new methods to classes in JavaScript?

In Ruby we can add a new method to a previously defined class by dynamically modifying it at runtime: class String def to_magic "magic" end end Is it possible to do the same in JavaScript? If yes, how?
Zero Fiber
  • 4,417
  • 2
  • 23
  • 34
-1
votes
1 answer

Python: python process isolation level when "monkey patching" classes

I have a (beginner) question. I intend to run more than one webapp (pyramid web apps) and I have a common library (let's call that base app) that may be used by both webapps and includes pyramid configuration includes etc. These webapps will…
Ahmed
  • 93
  • 1
  • 5
-2
votes
1 answer

How can I prevent loadstring from getting hooked like this

Here I have a loadstring but attacker can just hook it and steal it's content like bellow. I want to make a anti hook so that it can be prevented. -- Save copies of orignal functions local o_load = _G["load"] local o_loadstring =…
Alvee
  • 20
  • 2
1 2 3
63
64