Fiddle comments and variable names in whichmodule().

This commit is contained in:
Jeremy Hylton 2002-09-19 23:00:12 +00:00
parent 065a5ab8fb
commit f0cfdf7314
1 changed files with 10 additions and 11 deletions

View File

@ -611,30 +611,29 @@ def _keep_alive(x, memo):
memo[id(memo)]=[x] memo[id(memo)]=[x]
classmap = {} classmap = {} # called classmap for backwards compatibility
# This is no longer used to find classes, but still for functions def whichmodule(func, funcname):
def whichmodule(cls, clsname): """Figure out the module in which a function occurs.
"""Figure out the module in which a class occurs.
Search sys.modules for the module. Search sys.modules for the module.
Cache in classmap. Cache in classmap.
Return a module name. Return a module name.
If the class cannot be found, return __main__. If the function cannot be found, return __main__.
""" """
if cls in classmap: if func in classmap:
return classmap[cls] return classmap[func]
for name, module in sys.modules.items(): for name, module in sys.modules.items():
if module is None: if module is None:
continue # skip dummy package entries continue # skip dummy package entries
if name != '__main__' and \ if name != '__main__' and \
hasattr(module, clsname) and \ hasattr(module, funcname) and \
getattr(module, clsname) is cls: getattr(module, funcname) is func:
break break
else: else:
name = '__main__' name = '__main__'
classmap[cls] = name classmap[func] = name
return name return name