[tor-commits] [stem/master] Changed mocking.mock() to accomodate modules that link to other modules.

atagar at torproject.org atagar at torproject.org
Wed Jun 20 16:38:52 UTC 2012


commit 66376f5a6ebc35fd553506c226342ec8777fc9b5
Author: Erik <eislo at wesleyan.edu>
Date:   Mon Jun 18 17:01:53 2012 -0400

    Changed mocking.mock() to accomodate modules that link to other modules.
    
    We ran into a problem mocking os.readlink as in certain unix environments
    os links to an outside module, posix.  This resulted in the __dict__ of
    readlink remaining unchanged, and thus not mocking the realink function
    (which in our environment resides in the posix module).  To patch this,
    we added a third argument to mock() that can be used to explicitly
    specify the target_module.  A default value of None is provided so all
    prior calls on this method are compatible.
---
 test/mocking.py |    4 ++--
 1 files changed, 2 insertions(+), 2 deletions(-)

diff --git a/test/mocking.py b/test/mocking.py
index 347d22e..5a90f6e 100644
--- a/test/mocking.py
+++ b/test/mocking.py
@@ -71,7 +71,7 @@ def support_with(obj):
   obj.__dict__["__enter__"] = return_value(obj)
   obj.__dict__["__exit__"] = no_op()
 
-def mock(target, mock_call):
+def mock(target, mock_call, target_module=None):
   """
   Mocks the given function, saving the initial implementation so it can be
   reverted later.
@@ -87,7 +87,7 @@ def mock(target, mock_call):
   else:
     # this is a new mocking, save the original state
     mocking_id = MOCK_ID.next()
-    target_module = inspect.getmodule(target)
+    target_module = target_module or inspect.getmodule(target)
     target_function = target.__name__
     MOCK_STATE[mocking_id] = (target_module, target_function, target)
   





More information about the tor-commits mailing list