code: def patch_object(obj, attr, value):
      
      orig = getattr(obj, attr, no_value)
  
      try:
          setattr(obj, attr, value)
  
          yield obj
      finally:
          try:
              if orig is no_value:
                  delattr(obj, attr)
              else:
                  setattr(obj, attr, orig)
          except:
              pass
docs: Context manager that temporarily patches an object *obj* by replacing its attribute *attr* with
      *value*. The original value is set again when the context is closed.