diff options
author | Ned Batchelder <ned@nedbatchelder.com> | 2018-09-28 20:06:48 -0400 |
---|---|---|
committer | Ned Batchelder <ned@nedbatchelder.com> | 2018-09-28 20:06:48 -0400 |
commit | f9cfc98ebf12f3f04479bee9fab9358fdf9d4559 (patch) | |
tree | dcb3a040d9965e98203e750476b554b7e4914c6b /coverage | |
parent | 1cf8962099a98829761fcf24f38e9acf77802f9c (diff) | |
download | python-coveragepy-git-f9cfc98ebf12f3f04479bee9fab9358fdf9d4559.tar.gz |
Get qualified names for method contexts
Diffstat (limited to 'coverage')
-rw-r--r-- | coverage/context.py | 38 |
1 files changed, 37 insertions, 1 deletions
diff --git a/coverage/context.py b/coverage/context.py index 24d01f2a..f167de36 100644 --- a/coverage/context.py +++ b/coverage/context.py @@ -7,5 +7,41 @@ def should_start_context_test_function(frame): """Is this frame calling a test_* function?""" fn_name = frame.f_code.co_name if fn_name.startswith("test"): - return fn_name + return qualname_from_frame(frame) return None + + +def qualname_from_frame(frame): + """Get a qualified name for the code running in `frame`.""" + co = frame.f_code + fname = co.co_name + if not co.co_varnames: + return fname + + locs = frame.f_locals + first_arg = co.co_varnames[0] + if co.co_argcount and first_arg == "self": + self = locs["self"] + #elif co.co_flags & 0x04: # *args syntax + # self = locs[first_arg][0] + else: + return fname + + method = getattr(self, fname, None) + if method is None: + return fname + + func = method.__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 + else: + qname = fname + return qname |