code: def patch_func(replacement, target_mod, func_name):
      
      original = getattr(target_mod, func_name)
  
      # set the 'unpatched' attribute on the replacement to
      # point to the original.
      vars(replacement).setdefault('unpatched', original)
  
      # replace the function in the original module
      setattr(target_mod, func_name, replacement)
docs: Patch func_name in target_mod with replacement
  
      Important - original must be resolved by name to avoid
      patching an already patched function.