def file_module_function_of(self, frame): #this code is take from trace module and fixed to work with new-style classes code = frame.f_code filename = code.co_filename if filename: modulename = trace.modname(filename) else: modulename = None funcname = code.co_name clsname = None if code in self._caller_cache: if self._caller_cache[code] is not None: clsname = self._caller_cache[code] else: self._caller_cache[code] = None clsname = get_clsname_for_code(code, frame) if clsname is not None: # cache the result - assumption is that new.* is # not called later to disturb this relationship # _caller_cache could be flushed if functions in # the new module get called. self._caller_cache[code] = clsname if clsname is not None: funcname = "%s.%s" % (clsname, funcname) return filename, modulename, funcname
def file_module_function_of( self, frame ): #this code is take from trace module and fixed to work with new-style classes code = frame.f_code filename = code.co_filename if filename: modulename = trace.modname(filename) else: modulename = None funcname = code.co_name clsname = None if code in self._caller_cache: if self._caller_cache[code] is not None: clsname = self._caller_cache[code] else: self._caller_cache[code] = None ## use of gc.get_referrers() was suggested by Michael Hudson # all functions which refer to this code object funcs = [ f for f in gc.get_referrers(code) if inspect.isfunction(f) ] # require len(func) == 1 to avoid ambiguity caused by calls to # new.function(): "In the face of ambiguity, refuse the # temptation to guess." if len(funcs) == 1: dicts = [ d for d in gc.get_referrers(funcs[0]) if isinstance(d, dict) ] if len(dicts) == 1: classes = [ c for c in gc.get_referrers(dicts[0]) if hasattr(c, "__bases__") or inspect.isclass(c) ] elif len(dicts) > 1: #new-style classes classes = [ c for c in gc.get_referrers(dicts[1]) if hasattr(c, "__bases__") or inspect.isclass(c) ] else: classes = [] if len(classes) == 1: # ditto for new.classobj() clsname = classes[0].__name__ # cache the result - assumption is that new.* is # not called later to disturb this relationship # _caller_cache could be flushed if functions in # the new module get called. self._caller_cache[code] = clsname if clsname is not None: funcname = "%s.%s" % (clsname, funcname) return filename, modulename, funcname
def globaltrace_lt(self, frame, why, arg): if why == 'call': filename = frame.f_globals.get('__file__', None) if filename: modulename = trace.modname(filename) if modulename is not None: ignore_it = self.ignore.names(filename, modulename) if not ignore_it: return self.localtrace else: return None
def file_module_function_of(self, frame): #this code is take from trace module and fixed to work with new-style classes code = frame.f_code filename = code.co_filename if filename: modulename = trace.modname(filename) else: modulename = None funcname = code.co_name clsname = None if code in self._caller_cache: if self._caller_cache[code] is not None: clsname = self._caller_cache[code] else: self._caller_cache[code] = None ## use of gc.get_referrers() was suggested by Michael Hudson # all functions which refer to this code object funcs = [f for f in gc.get_referrers(code) if inspect.isfunction(f)] # require len(func) == 1 to avoid ambiguity caused by calls to # new.function(): "In the face of ambiguity, refuse the # temptation to guess." if len(funcs) == 1: dicts = [d for d in gc.get_referrers(funcs[0]) if isinstance(d, dict)] if len(dicts) == 1: classes = [c for c in gc.get_referrers(dicts[0]) if hasattr(c, "__bases__") or inspect.isclass(c)] elif len(dicts) > 1: #new-style classes classes = [c for c in gc.get_referrers(dicts[1]) if hasattr(c, "__bases__") or inspect.isclass(c)] else: classes = [] if len(classes) == 1: # ditto for new.classobj() clsname = classes[0].__name__ # cache the result - assumption is that new.* is # not called later to disturb this relationship # _caller_cache could be flushed if functions in # the new module get called. self._caller_cache[code] = clsname if clsname is not None: funcname = "%s.%s" % (clsname, funcname) return filename, modulename, funcname
def test_deprecated_modname(self): with self.assertWarns(DeprecationWarning): self.assertEqual("spam", trace.modname("spam"))