monkeypatching

Patch routine call in delphi

守給你的承諾、 提交于 2019-11-26 14:34:08
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 call in MadExcept.pas unit, but couldn't figure it out if i can use that for my project. I want something like my exe at runtime calls -> LoadResourceModule -> jump to -> MyCustomResourceModule... Any pointers on this would be very helpful. I use the following code: procedure PatchCode(Address: Pointer; const NewCode; Size: Integer); var OldProtect:

How to monkey patch Django?

蓝咒 提交于 2019-11-26 14:28:56
问题 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 self.first_name + " " + self.last_name return self.username User.add_to_class("get_user_name",get_user_name) I understand that this isn't ideal and it's better to add fields and functions to User through a separate model Profile . With that said, I just want to

In Rails, how to add a new method to String class?

依然范特西╮ 提交于 2019-11-26 12:38:29
问题 I want to build an index for different objects in my Rails project and would like to add a \'count_occurences\' method that I can call on String objects. I saw I could do something like class String def self.count_occurences do_something_here end end What\'s the exact way to define this method, and where to put the code in my Rails project? Thanks 回答1: You can define a new class in your application at lib/ext/string.rb and put this content in it: class String def to_magic "magic" end end To

How does one monkey patch a function in python?

こ雲淡風輕ζ 提交于 2019-11-26 11:59:02
问题 I\'m having trouble replacing a function from a different module with another function and it\'s driving me crazy. Let\'s say I have a module bar.py that looks like this: from a_package.baz import do_something_expensive def a_function(): print do_something_expensive() And I have another module that looks like this: from bar import a_function a_function() from a_package.baz import do_something_expensive do_something_expensive = lambda: \'Something really cheap.\' a_function() import a_package

Can you patch *just* a nested function with closure, or must the whole outer function be repeated?

荒凉一梦 提交于 2019-11-26 09:29:54
问题 A 3rd party library we use contains a rather long function that uses a nested function inside it. Our use of that library triggers a bug in that function, and we very much would like to solve that bug. Unfortunately, the library maintainers are somewhat slow with fixes, but we don\'t want to have to fork the library. We also cannot hold our release until they have fixed the issue. We would prefer to use monkey-patching to fix this issue here as that is easier to track than patching the source

What does 'Monkey Patching' exactly Mean in Ruby?

心不动则不痛 提交于 2019-11-26 08:59:38
问题 According to Wikipedia, a monkey patch is: a way to extend or modify the runtime code of dynamic languages [...] without altering the original source code. The following statement from the same entry confused me: In Ruby, the term monkey patch was misunderstood to mean any dynamic modification to a class and is often used as a synonym for dynamically modifying any class at runtime. I would like to know the exact meaning of monkey patching in Ruby. Is it doing something like the following, or

Monkey patching a class in another module in Python

元气小坏坏 提交于 2019-11-26 08:02:56
问题 I\'m working with a module written by someone else. I\'d like to monkey patch the __init__ method of a class defined in the module. The examples I have found showing how to do this have all assumed I\'d be calling the class myself (e.g. Monkey-patch Python class). However, this is not the case. In my case the class is initalised within a function in another module. See the (greatly simplified) example below: thirdpartymodule_a.py class SomeClass(object): def __init__(self): self.a = 42 def

Monkey-patch Python class

前提是你 提交于 2019-11-26 07:27:20
问题 I\'ve got a class, located in a separate module, which I can\'t change. from module import MyClass class ReplaceClass(object) ... MyClass = ReplaceClass This doesn\'t change MyClass anywhere else but this file. However if I\'ll add a method like this def bar(): print 123 MyClass.foo = bar this will work and foo method will be available everywhere else. How do I replace the class completely? 回答1: import module class ReplaceClass(object): .... module.MyClass = ReplaceClass 回答2: Avoid the from .

Can I patch a Python decorator before it wraps a function?

|▌冷眼眸甩不掉的悲伤 提交于 2019-11-26 07:24:26
问题 I have a function with a decorator that I\'m trying test with the help of the Python Mock library. I\'d like to use mock.patch to replace the real decorator with a mock \'bypass\' decorator which just calls the function. What I can\'t figure out is how to apply the patch before the real decorator wraps the function. I\'ve tried a few different variations on the patch target and reordering the patch and import statements, but without success. Any ideas? 回答1: Decorators are applied at function

Is it possible to replace (monkeypatch) PHP functions?

自闭症网瘾萝莉.ら 提交于 2019-11-26 06:46:45
问题 You can do this in Python, but is it possible in PHP? >>> def a(): print 1 ... >>> def a(): print 2 ... >>> a() 2 e.g.: <? function var_dump() {} ?> Fatal error: Cannot redeclare var_dump() in /tmp/- on line 1 回答1: This is a bit late, but I just want to point out that since PHP 5.3, it is actually possible to override internal functions without using a PHP extension. The trick is that you can redefine an internal PHP function inside a namespace. It's based on the way PHP does name resolution