summaryrefslogtreecommitdiff
path: root/lab/find_class.py
diff options
context:
space:
mode:
authorNed Batchelder <ned@nedbatchelder.com>2018-09-24 06:48:58 -0400
committerNed Batchelder <ned@nedbatchelder.com>2018-09-24 06:48:58 -0400
commit545a47cc59b88e5b51852256e6cc1602850d001b (patch)
tree5ea7e435ea483bd176937cf80f4b9b6c43d9e9fd /lab/find_class.py
parentbd36f540f4ab9a7155da3993f5d7d48b10112900 (diff)
parentb5d5aa99ebcfa140bc779301b22a0866903b6342 (diff)
downloadpython-coveragepy-git-545a47cc59b88e5b51852256e6cc1602850d001b.tar.gz
Merge branch 'nedbat/dynamic-contexts'
Diffstat (limited to 'lab/find_class.py')
-rw-r--r--lab/find_class.py40
1 files changed, 40 insertions, 0 deletions
diff --git a/lab/find_class.py b/lab/find_class.py
new file mode 100644
index 00000000..d8dac0b5
--- /dev/null
+++ b/lab/find_class.py
@@ -0,0 +1,40 @@
+class Parent(object):
+ def meth(self):
+ print("METH")
+
+class Child(Parent):
+ pass
+
+def trace(frame, event, args):
+ # Thanks to Aleksi Torhamo for code and idea.
+ co = frame.f_code
+ fname = co.co_name
+ if not co.co_varnames:
+ return
+ locs = frame.f_locals
+ first_arg = co.co_varnames[0]
+ if co.co_argcount:
+ self = locs[first_arg]
+ elif co.co_flags & 0x04: # *args syntax
+ self = locs[first_arg][0]
+ else:
+ return
+
+ func = getattr(self, fname).__func__
+ if hasattr(func, '__qualname__'):
+ qname = func.__qualname__
+ else:
+ for cls in self.__class__.__mro__:
+ f = cls.__dict__.get(fname, None)
+ if f is None:
+ continue
+ if f is func:
+ qname = cls.__name__ + "." + fname
+ break
+ print("{}: {}.{} {}".format(event, self, fname, qname))
+ return trace
+
+import sys
+sys.settrace(trace)
+
+Child().meth()