From: Brett Cannon Date: Tue, 6 May 2008 04:37:31 +0000 (+0000) Subject: Fix a bug in the handling of the stacklevel argument in warnings.warn() where X-Git-Tag: v2.6a3~21 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=e3dcb01bfc6bd14da865b0f28c3c9ad8181ab212;p=python Fix a bug in the handling of the stacklevel argument in warnings.warn() where the stack was being unwound by two levels instead of one each time. --- diff --git a/Lib/test/test_warnings.py b/Lib/test/test_warnings.py index c854d7ade4..1b5ee3291c 100644 --- a/Lib/test/test_warnings.py +++ b/Lib/test/test_warnings.py @@ -225,6 +225,8 @@ class WarnTests(unittest.TestCase): self.assertEqual(os.path.basename(w.filename), "test_warnings.py") warning_tests.outer("spam6", stacklevel=2) self.assertEqual(os.path.basename(w.filename), "warning_tests.py") + warning_tests.outer("spam6.5", stacklevel=3) + self.assertEqual(os.path.basename(w.filename), "test_warnings.py") warning_tests.inner("spam7", stacklevel=9999) self.assertEqual(os.path.basename(w.filename), "sys") diff --git a/Python/_warnings.c b/Python/_warnings.c index 3e7dda7db8..0e48675d81 100644 --- a/Python/_warnings.c +++ b/Python/_warnings.c @@ -445,10 +445,8 @@ setup_context(Py_ssize_t stack_level, PyObject **filename, int *lineno, /* Setup globals and lineno. */ PyFrameObject *f = PyThreadState_GET()->frame; - while (--stack_level > 0 && f != NULL) { + while (--stack_level > 0 && f != NULL) f = f->f_back; - --stack_level; - } if (f == NULL) { globals = PyThreadState_Get()->interp->sysdict;