[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[tor-commits] [stem/master] Use getattr()/setattr() for mocking.mock_method



commit a00e2dda071bb2d6c04b6c78ece33942ba0def53
Author: Sean Robinson <seankrobinson@xxxxxxxxx>
Date:   Sat Dec 8 18:08:39 2012 -0700

    Use getattr()/setattr() for mocking.mock_method
    
    mocking.revert_mocking also needs setattr because it may be trying to
    revert to the original method in an object.  This fixes the following
    error when trying to use mock_method:
    
        TypeError: 'dictproxy' object does not support item assignment
    
    
    Signed-off-by: Sean Robinson <seankrobinson@xxxxxxxxx>
---
 test/mocking.py |    8 ++++----
 1 files changed, 4 insertions(+), 4 deletions(-)

diff --git a/test/mocking.py b/test/mocking.py
index 1cf2c0a..88cbd62 100644
--- a/test/mocking.py
+++ b/test/mocking.py
@@ -299,7 +299,7 @@ def mock_method(target_class, method_name, mock_call):
   # themselves don't reference the class. This is unfortunate because it means
   # that we need to know both the class and method we're replacing.
   
-  target_method = target_class.__dict__[method_name]
+  target_method = getattr(target_class, method_name)
   
   if "mock_id" in target_method.__dict__:
     # we're overriding an already mocked method
@@ -311,10 +311,10 @@ def mock_method(target_class, method_name, mock_call):
     MOCK_STATE[mocking_id] = (target_class, method_name, target_method)
   
   mock_wrapper = lambda *args: mock_call(*args)
-  mock_wrapper.__dict__["mock_id"] = mocking_id
+  setattr(mock_wrapper, "mock_id", mocking_id)
   
   # mocks the function with this wrapper
-  target_class.__dict__[method_name] = mock_wrapper
+  setattr(target_class, method_name, mock_wrapper)
 
 def revert_mocking():
   """
@@ -334,7 +334,7 @@ def revert_mocking():
     if module == __builtin__:
       setattr(__builtin__, function, impl)
     else:
-      module.__dict__[function] = impl
+      setattr(module, function, impl)
     
     del MOCK_STATE[mock_id]
   



_______________________________________________
tor-commits mailing list
tor-commits@xxxxxxxxxxxxxxxxxxxx
https://lists.torproject.org/cgi-bin/mailman/listinfo/tor-commits