From 70b5df86e271e9c3d143df19254c4b98dff9e0da Mon Sep 17 00:00:00 2001 From: Berker Peksag Date: Fri, 11 Jul 2014 19:50:25 +0300 Subject: Issue #16382: Improve exception message of warnings.warn() for bad category. Initial patch by Phil Elson. --- Python/_warnings.c | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) (limited to 'Python/_warnings.c') diff --git a/Python/_warnings.c b/Python/_warnings.c index 6013d7d7d4..363c1f29bb 100644 --- a/Python/_warnings.c +++ b/Python/_warnings.c @@ -619,16 +619,17 @@ get_category(PyObject *message, PyObject *category) if (rc == 1) category = (PyObject*)message->ob_type; - else if (category == NULL) + else if (category == NULL || category == Py_None) category = PyExc_UserWarning; /* Validate category. */ rc = PyObject_IsSubclass(category, PyExc_Warning); - if (rc == -1) - return NULL; - if (rc == 0) { - PyErr_SetString(PyExc_ValueError, - "category is not a subclass of Warning"); + /* category is not a subclass of PyExc_Warning or + PyObject_IsSubclass raised an error */ + if (rc == -1 || rc == 0) { + PyErr_Format(PyExc_TypeError, + "category must be a Warning subclass, not '%s'", + Py_TYPE(category)->tp_name); return NULL; } -- cgit v1.2.1 From b09b34b211300f68cc98f6b882f7d2e2f4a76f10 Mon Sep 17 00:00:00 2001 From: Brett Cannon Date: Mon, 13 Apr 2015 14:21:02 -0400 Subject: Issue #23731: Implement PEP 488. The concept of .pyo files no longer exists. Now .pyc files have an optional `opt-` tag which specifies if any extra optimizations beyond the peepholer were applied. --- Python/_warnings.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) (limited to 'Python/_warnings.c') diff --git a/Python/_warnings.c b/Python/_warnings.c index a1f4368ebe..eb2942458d 100644 --- a/Python/_warnings.c +++ b/Python/_warnings.c @@ -563,13 +563,12 @@ setup_context(Py_ssize_t stack_level, PyObject **filename, int *lineno, data = PyUnicode_DATA(*filename); #define ascii_lower(c) ((c <= 127) ? Py_TOLOWER(c) : 0) - /* if filename.lower().endswith((".pyc", ".pyo")): */ + /* if filename.lower().endswith(".pyc"): */ if (len >= 4 && PyUnicode_READ(kind, data, len-4) == '.' && ascii_lower(PyUnicode_READ(kind, data, len-3)) == 'p' && ascii_lower(PyUnicode_READ(kind, data, len-2)) == 'y' && - (ascii_lower(PyUnicode_READ(kind, data, len-1)) == 'c' || - ascii_lower(PyUnicode_READ(kind, data, len-1)) == 'o')) + ascii_lower(PyUnicode_READ(kind, data, len-1)) == 'c') { *filename = PyUnicode_Substring(*filename, 0, PyUnicode_GET_LENGTH(*filename)-1); -- cgit v1.2.1 From 63d8ef7239dd3cb7242ea8df7d79211783d1492c Mon Sep 17 00:00:00 2001 From: Larry Hastings Date: Sun, 6 Sep 2015 00:39:37 -0700 Subject: Issue #24305: Prevent import subsystem stack frames from being counted by the warnings.warn(stacklevel=) parameter. --- Python/_warnings.c | 72 ++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 70 insertions(+), 2 deletions(-) (limited to 'Python/_warnings.c') diff --git a/Python/_warnings.c b/Python/_warnings.c index 22f617a9ff..9ca83145c9 100644 --- a/Python/_warnings.c +++ b/Python/_warnings.c @@ -513,6 +513,64 @@ warn_explicit(PyObject *category, PyObject *message, return result; /* Py_None or NULL. */ } +static int +is_internal_frame(PyFrameObject *frame) +{ + static PyObject *importlib_string = NULL; + static PyObject *bootstrap_string = NULL; + PyObject *filename; + int contains; + + if (importlib_string == NULL) { + importlib_string = PyUnicode_FromString("importlib"); + if (importlib_string == NULL) { + return 0; + } + + bootstrap_string = PyUnicode_FromString("_bootstrap"); + if (bootstrap_string == NULL) { + Py_DECREF(importlib_string); + return 0; + } + Py_INCREF(importlib_string); + Py_INCREF(bootstrap_string); + } + + if (frame == NULL || frame->f_code == NULL || + frame->f_code->co_filename == NULL) { + return 0; + } + filename = frame->f_code->co_filename; + if (!PyUnicode_Check(filename)) { + return 0; + } + contains = PyUnicode_Contains(filename, importlib_string); + if (contains < 0) { + return 0; + } + else if (contains > 0) { + contains = PyUnicode_Contains(filename, bootstrap_string); + if (contains < 0) { + return 0; + } + else if (contains > 0) { + return 1; + } + } + + return 0; +} + +static PyFrameObject * +next_external_frame(PyFrameObject *frame) +{ + do { + frame = frame->f_back; + } while (frame != NULL && is_internal_frame(frame)); + + return frame; +} + /* filename, module, and registry are new refs, globals is borrowed */ /* Returns 0 on error (no new refs), 1 on success */ static int @@ -523,8 +581,18 @@ 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) - f = f->f_back; + // Stack level comparisons to Python code is off by one as there is no + // warnings-related stack level to avoid. + if (stack_level <= 0 || is_internal_frame(f)) { + while (--stack_level > 0 && f != NULL) { + f = f->f_back; + } + } + else { + while (--stack_level > 0 && f != NULL) { + f = next_external_frame(f); + } + } if (f == NULL) { globals = PyThreadState_Get()->interp->sysdict; -- cgit v1.2.1 From 585a1eed45faea53f836b1db2ac211d7f14b44c8 Mon Sep 17 00:00:00 2001 From: Serhiy Storchaka Date: Thu, 24 Dec 2015 10:35:59 +0200 Subject: Issue #20440: Massive replacing unsafe attribute setting code with special macro Py_SETREF. --- Python/_warnings.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) (limited to 'Python/_warnings.c') diff --git a/Python/_warnings.c b/Python/_warnings.c index 9ca83145c9..978bad135c 100644 --- a/Python/_warnings.c +++ b/Python/_warnings.c @@ -680,8 +680,7 @@ setup_context(Py_ssize_t stack_level, PyObject **filename, int *lineno, goto handle_error; } else if (!is_true) { - Py_DECREF(*filename); - *filename = PyUnicode_FromString("__main__"); + Py_SETREF(*filename, PyUnicode_FromString("__main__")); if (*filename == NULL) goto handle_error; } -- cgit v1.2.1 From 58455ad4ed5e6f1eda17ceb0dd7f5612a980e48d Mon Sep 17 00:00:00 2001 From: Serhiy Storchaka Date: Wed, 6 Apr 2016 09:45:48 +0300 Subject: Issue #22570: Renamed Py_SETREF to Py_XSETREF. --- Python/_warnings.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'Python/_warnings.c') diff --git a/Python/_warnings.c b/Python/_warnings.c index 978bad135c..78f532ecb4 100644 --- a/Python/_warnings.c +++ b/Python/_warnings.c @@ -680,7 +680,7 @@ setup_context(Py_ssize_t stack_level, PyObject **filename, int *lineno, goto handle_error; } else if (!is_true) { - Py_SETREF(*filename, PyUnicode_FromString("__main__")); + Py_XSETREF(*filename, PyUnicode_FromString("__main__")); if (*filename == NULL) goto handle_error; } -- cgit v1.2.1 From 3099587c099191eccc737760a8d15c7fedcd8a40 Mon Sep 17 00:00:00 2001 From: Serhiy Storchaka Date: Sun, 10 Apr 2016 18:05:40 +0300 Subject: Issue #26200: Added Py_SETREF and replaced Py_XSETREF with Py_SETREF in places where Py_DECREF was used. --- Python/_warnings.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'Python/_warnings.c') diff --git a/Python/_warnings.c b/Python/_warnings.c index 78f532ecb4..978bad135c 100644 --- a/Python/_warnings.c +++ b/Python/_warnings.c @@ -680,7 +680,7 @@ setup_context(Py_ssize_t stack_level, PyObject **filename, int *lineno, goto handle_error; } else if (!is_true) { - Py_XSETREF(*filename, PyUnicode_FromString("__main__")); + Py_SETREF(*filename, PyUnicode_FromString("__main__")); if (*filename == NULL) goto handle_error; } -- cgit v1.2.1