Public Member Functions | Public Attributes | List of all members
stubout.StubOutForTesting Class Reference

Public Member Functions

def __del__ (self)
 
def __del__ (self)
 
def __init__ (self)
 
def __init__ (self)
 
def Set (self, parent, child_name, new_child)
 
def Set (self, parent, child_name, new_child)
 
def SmartSet (self, obj, attr_name, new_attr)
 
def SmartSet (self, obj, attr_name, new_attr)
 
def SmartUnsetAll (self)
 
def SmartUnsetAll (self)
 
def UnsetAll (self)
 
def UnsetAll (self)
 

Public Attributes

 cache
 
 stubs
 

Detailed Description

Sample Usage:
   You want os.path.exists() to always return true during testing.

   stubs = StubOutForTesting()
   stubs.Set(os.path, 'exists', lambda x: 1)
     ...
   stubs.UnsetAll()

   The above changes os.path.exists into a lambda that returns 1.  Once
   the ... part of the code finishes, the UnsetAll() looks up the old value
   of os.path.exists and restores it.

Definition at line 23 of file bloaty/third_party/protobuf/python/stubout.py.

Constructor & Destructor Documentation

◆ __init__() [1/2]

def stubout.StubOutForTesting.__init__ (   self)

◆ __del__() [1/2]

def stubout.StubOutForTesting.__del__ (   self)

◆ __init__() [2/2]

def stubout.StubOutForTesting.__init__ (   self)

Definition at line 37 of file protobuf/python/stubout.py.

◆ __del__() [2/2]

def stubout.StubOutForTesting.__del__ (   self)

Definition at line 41 of file protobuf/python/stubout.py.

Member Function Documentation

◆ Set() [1/2]

def stubout.StubOutForTesting.Set (   self,
  parent,
  child_name,
  new_child 
)
Replace child_name's old definition with new_child, in the context
of the given parent.  The parent could be a module when the child is a
function at module scope.  Or the parent could be a class when a class'
method is being replaced.  The named child is set to new_child, while
the prior definition is saved away for later, when UnsetAll() is called.

This method supports the case where child_name is a staticmethod or a
classmethod of parent.

Definition at line 112 of file bloaty/third_party/protobuf/python/stubout.py.

◆ Set() [2/2]

def stubout.StubOutForTesting.Set (   self,
  parent,
  child_name,
  new_child 
)
Replace child_name's old definition with new_child, in the context
of the given parent.  The parent could be a module when the child is a
function at module scope.  Or the parent could be a class when a class'
method is being replaced.  The named child is set to new_child, while
the prior definition is saved away for later, when UnsetAll() is called.

This method supports the case where child_name is a staticmethod or a
classmethod of parent.

Definition at line 112 of file protobuf/python/stubout.py.

◆ SmartSet() [1/2]

def stubout.StubOutForTesting.SmartSet (   self,
  obj,
  attr_name,
  new_attr 
)
Replace obj.attr_name with new_attr. This method is smart and works
   at the module, class, and instance level while preserving proper
   inheritance. It will not stub out C types however unless that has been
   explicitly allowed by the type.

   This method supports the case where attr_name is a staticmethod or a
   classmethod of obj.

   Notes:
  - If obj is an instance, then it is its class that will actually be
stubbed. Note that the method Set() does not do that: if obj is
an instance, it (and not its class) will be stubbed.
  - The stubbing is using the builtin getattr and setattr. So, the __get__
and __set__ will be called when stubbing (TODO: A better idea would
probably be to manipulate obj.__dict__ instead of getattr() and
setattr()).

   Raises AttributeError if the attribute cannot be found.

Definition at line 45 of file protobuf/python/stubout.py.

◆ SmartSet() [2/2]

def stubout.StubOutForTesting.SmartSet (   self,
  obj,
  attr_name,
  new_attr 
)
Replace obj.attr_name with new_attr. This method is smart and works
   at the module, class, and instance level while preserving proper
   inheritance. It will not stub out C types however unless that has been
   explicitly allowed by the type.

   This method supports the case where attr_name is a staticmethod or a
   classmethod of obj.

   Notes:
  - If obj is an instance, then it is its class that will actually be
stubbed. Note that the method Set() does not do that: if obj is
an instance, it (and not its class) will be stubbed.
  - The stubbing is using the builtin getattr and setattr. So, the __get__
and __set__ will be called when stubbing (TODO: A better idea would
probably be to manipulate obj.__dict__ instead of getattr() and
setattr()).

   Raises AttributeError if the attribute cannot be found.

Definition at line 45 of file bloaty/third_party/protobuf/python/stubout.py.

◆ SmartUnsetAll() [1/2]

def stubout.StubOutForTesting.SmartUnsetAll (   self)
Reverses all the SmartSet() calls, restoring things to their original
definition.  Its okay to call SmartUnsetAll() repeatedly, as later calls
have no effect if no SmartSet() calls have been made.

Definition at line 99 of file bloaty/third_party/protobuf/python/stubout.py.

◆ SmartUnsetAll() [2/2]

def stubout.StubOutForTesting.SmartUnsetAll (   self)
Reverses all the SmartSet() calls, restoring things to their original
definition.  Its okay to call SmartUnsetAll() repeatedly, as later calls
have no effect if no SmartSet() calls have been made.

Definition at line 99 of file protobuf/python/stubout.py.

◆ UnsetAll() [1/2]

def stubout.StubOutForTesting.UnsetAll (   self)
Reverses all the Set() calls, restoring things to their original
definition.  Its okay to call UnsetAll() repeatedly, as later calls have
no effect if no Set() calls have been made.

Definition at line 131 of file bloaty/third_party/protobuf/python/stubout.py.

◆ UnsetAll() [2/2]

def stubout.StubOutForTesting.UnsetAll (   self)
Reverses all the Set() calls, restoring things to their original
definition.  Its okay to call UnsetAll() repeatedly, as later calls have
no effect if no Set() calls have been made.

Definition at line 131 of file protobuf/python/stubout.py.

Member Data Documentation

◆ cache

stubout.StubOutForTesting.cache

◆ stubs

stubout.StubOutForTesting.stubs

The documentation for this class was generated from the following file:


grpc
Author(s):
autogenerated on Fri May 16 2025 03:03:44