def debug_demo(x='some data'): from inspect import getframeinfo def view(frame, obj): info = getframeinfo(frame) msg = 'Access to {!r} (@{}) at {}:{}:{}' print(msg.format(obj, hex(id(obj)), info.filename, info.lineno, info.function)) return obj setrwatch({id(x): view}) print(getrwatch()) def f(x): x # ← here f(x) # ← here def f(x): def g(): x # ← here return g f(x)() # ← here class Foo: def __init__(self, x): self.x = x # ← here def __call__(self): return self.x # ← here @property def value(self): return self.x # ← here foo = Foo(x) # ← here foo() foo.value x = 10 # not here (rebind, no push)
def debug_demo(x='some data'): from inspect import getframeinfo def view(frame, obj): info = getframeinfo(frame) msg = 'Access to {!r} (@{}) at {}:{}:{}' print( msg.format(obj, hex(id(obj)), info.filename, info.lineno, info.function)) return obj setrwatch({id(x): view}) print(getrwatch()) def f(x): x # ← here f(x) # ← here def f(x): def g(): x # ← here return g f(x)() # ← here class Foo: def __init__(self, x): self.x = x # ← here def __call__(self): return self.x # ← here @property def value(self): return self.x # ← here foo = Foo(x) # ← here foo() foo.value x = 10 # not here (rebind, no push)
def f(): x = object() setrwatch({id(x): view}) print({hex(k): v for k, v in (getrwatch() or {}).items()}) print('x =', x)
def f(): x = object() setrwatch({id(x): view}) print({hex(k): v for k,v in (getrwatch() or {}).items()}) print('x =', x)
def __enter__(self): getrwatch()[self.idobj] = debug_view
def delrwatch(idobj): getrwatch().pop(idobj, None)
# In[5]: get_ipython().run_cell_magic('bash', '', 'tmpdir=$(mktemp -d)\ncd $tmpdir\ngit clone https://github.com/dutc/rwatch\ncd rwatch/src/\nmake\nls -larth ./rwatch.so\nfile ./rwatch.so\n# cp ./rwatch.so /where/ver/you/need/ # ~/publis/notebook/ for me') # Anyhow, if `rwatch` is installed, we can import it, and it enables two new functions in the `sys` module: # In[6]: import rwatch from sys import setrwatch, getrwatch setrwatch({}) # clean any previously installed rwatch getrwatch() # Finally, we need the [`collections`](https://docs.python.org/3/library/collections.html) module and its [`defaultdict`](https://docs.python.org/3/library/collections.html#collections.defaultdict) magical datastructure. # In[7]: from collections import defaultdict # ---- # ## Defining a debugging context manager, just to try # # This is the first example given in [James presentation]() at PyCon Canada 2016. #