Skip to content

Instantly share code, notes, and snippets.

@matthewlmcclure
Last active December 20, 2015 11:29
Show Gist options
  • Save matthewlmcclure/6124118 to your computer and use it in GitHub Desktop.
Save matthewlmcclure/6124118 to your computer and use it in GitHub Desktop.
Log information about the objects replaced by Python Mock, patch. Combine with similar logging in your application to debug patching.
--- mock.py~ 2013-07-31 13:20:48.000000000 -0400
+++ mock.py 2013-07-31 17:43:13.000000000 -0400
@@ -32,9 +32,12 @@
__version__ = '0.8.0'
+import logging
import pprint
import sys
+logger = logging.getLogger(__name__)
+
try:
import inspect
except ImportError:
@@ -1112,7 +1115,8 @@
try:
return getattr(thing, comp)
except AttributeError:
- __import__(import_path)
+ imported_thing = __import__(import_path)
+ logger.debug('imported_thing: %r', imported_thing)
return getattr(thing, comp)
@@ -1120,10 +1124,12 @@
components = target.split('.')
import_path = components.pop(0)
thing = __import__(import_path)
+ logger.debug('thing: %r', thing)
for comp in components:
import_path += ".%s" % comp
thing = _dot_lookup(thing, comp, import_path)
+ logger.debug('thing: %r', thing)
return thing
@@ -1272,6 +1278,7 @@
new_callable = self.new_callable
self.target = self.getter()
+ logger.debug('target: %r', self.target)
original, local = self.get_original()
if new is DEFAULT and autospec is False:
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment