I am using a 3rd-party library to parse some domain specific file. The good thing is the library works, the bad thing is the library has some small issues that I would like to modify, the ugly thing is the function I am using has, well, around 2000 lines. I just want to modify one regular expression inside an if
expression, is there a way I could "hotfix" that without modifying the original library?
You can use unittest.mock.patch
in a with
statement to swap out anything. I can't tell you exactly how to do it without seeing the code, but if the regex is already in a global, you can just replace that for the duration of the call.
If it's made from a string literal written inside the function, you could instead patch whatever function it's passed to, like re.compile
or re.match
, etc. Make a wrapper that checks for the one string you want replaced, replace it, and then delegate to the real function. (Save a reference to the real thing outside the with
statement and use that if you don't want a stack overflow :)
old_compile = re.compile
with patch.object(re, 'compile', lambda s: old_compile('eggs' if s == 'spam' else s)):
bad_library_function()
This example assumes that bad_library_function
has a call to re.compile
in it with a string literal 'spam' that you don't like, and you'd rather it were eggs
. The patch replaces it with a new function that treats a 'spam'
argument as if it were 'eggs'
and otherwise behaves as the original re.compile
function, but only for the duration of the with
statement.
This monkeypatching approach is very brittle. If you update the library and it changes the implementation, your patch could break.