Monkey patching decorator.
Parameters: | |
---|---|
Returns: | A decorator which takes a function and applies the patch, returning the patched version. |
Usage:
# Patch os.listdir to return all lowercase.
@patch(os)
def listdir(original, path):
return [x.lower() for x in original(path)]
# It is patched in place.
os.listdir('.')
# The new function still refers to the original.
listdir('.')
# Patch chflags to do nothing in Python up to 2.6.
@patch(os, 'chflags', max_version=(2, 6))
def patched_chflags(func, *args, **kwargs):
pass