Index: Lib/warnings.py =================================================================== RCS file: /cvsroot/python/python/dist/src/Lib/warnings.py,v retrieving revision 1.21 diff -u -r1.21 warnings.py --- Lib/warnings.py 11 Jul 2003 15:37:56 -0000 1.21 +++ Lib/warnings.py 27 Nov 2003 22:14:41 -0000 @@ -21,18 +21,10 @@ defaultaction = "default" onceregistry = {} -def warn(message, category=None, stacklevel=1): - """Issue a warning, or maybe ignore it or raise an exception.""" - # Check if message is already a Warning object - if isinstance(message, Warning): - category = message.__class__ - # Check category argument - if category is None: - category = UserWarning - assert issubclass(category, Warning) +def stackinfo(stacklevel): # Get context information try: - caller = sys._getframe(stacklevel) + caller = sys._getframe(stacklevel+1) except ValueError: globals = sys.__dict__ lineno = 1 @@ -43,6 +35,32 @@ module = globals['__name__'] else: module = "" + return module, globals, lineno + +def warn(message, category=None, stacklevel=1): + """Issue a warning, or maybe ignore it or raise an exception.""" + # Check if message is already a Warning object + if isinstance(message, Warning): + category = message.__class__ + # Check category argument + if category is None: + category = UserWarning + assert issubclass(category, Warning) + # Get context information + if stacklevel >= 0: + module, globals, lineno = stackinfo(stacklevel) + else: + orgmodule, orgglobals, orglineno = stackinfo(1) + orgmodule = orgmodule.split(".")[:-stacklevel] + level = 1 + try: + while True: + module, globals, lineno = stackinfo(level) + if module.split(".")[:-stacklevel] != orgmodule: + break + level += 1 + except ValueError: + pass filename = globals.get('__file__') if filename: fnl = filename.lower()