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 5c633fc25fc6f9beee3ccb9ebc06633d1c9a1bc3 Mon Sep 17 00:00:00 2001 From: Serhiy Storchaka Date: Fri, 25 Dec 2015 20:01:53 +0200 Subject: Issue #25923: Added more const qualifiers to signatures of static and private functions. --- 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..daa13551ec 100644 --- a/Python/_warnings.c +++ b/Python/_warnings.c @@ -921,7 +921,7 @@ PyErr_WarnEx(PyObject *category, const char *text, Py_ssize_t stack_level) #undef PyErr_Warn PyAPI_FUNC(int) -PyErr_Warn(PyObject *category, char *text) +PyErr_Warn(PyObject *category, const char *text) { return PyErr_WarnEx(category, text, 1); } -- cgit v1.2.1 From 284c83a146d035d1d2119bce97dadb3eac5216f6 Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Sat, 19 Mar 2016 00:47:17 +0100 Subject: Add _showwarnmsg() and _formatwarnmsg() to warnings Issue #26568: add new _showwarnmsg() and _formatwarnmsg() functions to the warnings module. The C function warn_explicit() now calls warnings._showwarnmsg() with a warnings.WarningMessage as parameter, instead of calling warnings.showwarning() with multiple parameters. _showwarnmsg() calls warnings.showwarning() if warnings.showwarning() was replaced. Same for _formatwarnmsg(): call warnings.formatwarning() if it was replaced. --- Python/_warnings.c | 78 +++++++++++++++++++++++++++++++++++++----------------- 1 file changed, 53 insertions(+), 25 deletions(-) (limited to 'Python/_warnings.c') diff --git a/Python/_warnings.c b/Python/_warnings.c index daa13551ec..a8c3703926 100644 --- a/Python/_warnings.c +++ b/Python/_warnings.c @@ -359,6 +359,56 @@ error: PyErr_Clear(); } +static int +call_show_warning(PyObject *category, PyObject *text, PyObject *message, + PyObject *filename, int lineno, PyObject *lineno_obj, + PyObject *sourceline) +{ + PyObject *show_fn, *msg, *res, *warnmsg_cls = NULL; + + show_fn = get_warnings_attr("_showwarnmsg"); + if (show_fn == NULL) { + if (PyErr_Occurred()) + return -1; + show_warning(filename, lineno, text, category, sourceline); + return 0; + } + + if (!PyCallable_Check(show_fn)) { + PyErr_SetString(PyExc_TypeError, + "warnings._showwarnmsg() must be set to a callable"); + goto error; + } + + warnmsg_cls = get_warnings_attr("WarningMessage"); + if (warnmsg_cls == NULL) { + PyErr_SetString(PyExc_RuntimeError, + "unable to get warnings.WarningMessage"); + goto error; + } + + msg = PyObject_CallFunctionObjArgs(warnmsg_cls, message, category, + filename, lineno_obj, + NULL); + Py_DECREF(warnmsg_cls); + if (msg == NULL) + goto error; + + res = PyObject_CallFunctionObjArgs(show_fn, msg, NULL); + Py_DECREF(show_fn); + Py_DECREF(msg); + + if (res == NULL) + return -1; + + Py_DECREF(res); + return 0; + +error: + Py_XDECREF(show_fn); + return -1; +} + static PyObject * warn_explicit(PyObject *category, PyObject *message, PyObject *filename, int lineno, @@ -470,31 +520,9 @@ warn_explicit(PyObject *category, PyObject *message, if (rc == 1) /* Already warned for this module. */ goto return_none; if (rc == 0) { - PyObject *show_fxn = get_warnings_attr("showwarning"); - if (show_fxn == NULL) { - if (PyErr_Occurred()) - goto cleanup; - show_warning(filename, lineno, text, category, sourceline); - } - else { - PyObject *res; - - if (!PyCallable_Check(show_fxn)) { - PyErr_SetString(PyExc_TypeError, - "warnings.showwarning() must be set to a " - "callable"); - Py_DECREF(show_fxn); - goto cleanup; - } - - res = PyObject_CallFunctionObjArgs(show_fxn, message, category, - filename, lineno_obj, - NULL); - Py_DECREF(show_fxn); - Py_XDECREF(res); - if (res == NULL) - goto cleanup; - } + if (call_show_warning(category, text, message, filename, lineno, + lineno_obj, sourceline) < 0) + goto cleanup; } else /* if (rc == -1) */ goto cleanup; -- cgit v1.2.1 From e6094ad533ed236d30c68e7225ac767d1ceac1f6 Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Sat, 19 Mar 2016 01:03:51 +0100 Subject: On ResourceWarning, log traceback where the object was allocated Issue #26567: * Add a new function PyErr_ResourceWarning() function to pass the destroyed object * Add a source attribute to warnings.WarningMessage * Add warnings._showwarnmsg() which uses tracemalloc to get the traceback where source object was allocated. --- Python/_warnings.c | 91 +++++++++++++++++++++++++++++++++++++----------------- 1 file changed, 62 insertions(+), 29 deletions(-) (limited to 'Python/_warnings.c') diff --git a/Python/_warnings.c b/Python/_warnings.c index a8c3703926..25299fb622 100644 --- a/Python/_warnings.c +++ b/Python/_warnings.c @@ -287,8 +287,8 @@ update_registry(PyObject *registry, PyObject *text, PyObject *category, } static void -show_warning(PyObject *filename, int lineno, PyObject *text, PyObject - *category, PyObject *sourceline) +show_warning(PyObject *filename, int lineno, PyObject *text, + PyObject *category, PyObject *sourceline) { PyObject *f_stderr; PyObject *name; @@ -362,7 +362,7 @@ error: static int call_show_warning(PyObject *category, PyObject *text, PyObject *message, PyObject *filename, int lineno, PyObject *lineno_obj, - PyObject *sourceline) + PyObject *sourceline, PyObject *source) { PyObject *show_fn, *msg, *res, *warnmsg_cls = NULL; @@ -388,7 +388,7 @@ call_show_warning(PyObject *category, PyObject *text, PyObject *message, } msg = PyObject_CallFunctionObjArgs(warnmsg_cls, message, category, - filename, lineno_obj, + filename, lineno_obj, Py_None, Py_None, source, NULL); Py_DECREF(warnmsg_cls); if (msg == NULL) @@ -412,7 +412,8 @@ error: static PyObject * warn_explicit(PyObject *category, PyObject *message, PyObject *filename, int lineno, - PyObject *module, PyObject *registry, PyObject *sourceline) + PyObject *module, PyObject *registry, PyObject *sourceline, + PyObject *source) { PyObject *key = NULL, *text = NULL, *result = NULL, *lineno_obj = NULL; PyObject *item = NULL; @@ -521,7 +522,7 @@ warn_explicit(PyObject *category, PyObject *message, goto return_none; if (rc == 0) { if (call_show_warning(category, text, message, filename, lineno, - lineno_obj, sourceline) < 0) + lineno_obj, sourceline, source) < 0) goto cleanup; } else /* if (rc == -1) */ @@ -766,7 +767,8 @@ get_category(PyObject *message, PyObject *category) } static PyObject * -do_warn(PyObject *message, PyObject *category, Py_ssize_t stack_level) +do_warn(PyObject *message, PyObject *category, Py_ssize_t stack_level, + PyObject *source) { PyObject *filename, *module, *registry, *res; int lineno; @@ -775,7 +777,7 @@ do_warn(PyObject *message, PyObject *category, Py_ssize_t stack_level) return NULL; res = warn_explicit(category, message, filename, lineno, module, registry, - NULL); + NULL, source); Py_DECREF(filename); Py_DECREF(registry); Py_DECREF(module); @@ -796,14 +798,15 @@ warnings_warn(PyObject *self, PyObject *args, PyObject *kwds) category = get_category(message, category); if (category == NULL) return NULL; - return do_warn(message, category, stack_level); + return do_warn(message, category, stack_level, NULL); } static PyObject * warnings_warn_explicit(PyObject *self, PyObject *args, PyObject *kwds) { static char *kwd_list[] = {"message", "category", "filename", "lineno", - "module", "registry", "module_globals", 0}; + "module", "registry", "module_globals", + "source", 0}; PyObject *message; PyObject *category; PyObject *filename; @@ -811,10 +814,11 @@ warnings_warn_explicit(PyObject *self, PyObject *args, PyObject *kwds) PyObject *module = NULL; PyObject *registry = NULL; PyObject *module_globals = NULL; + PyObject *sourceobj = NULL; - if (!PyArg_ParseTupleAndKeywords(args, kwds, "OOUi|OOO:warn_explicit", + if (!PyArg_ParseTupleAndKeywords(args, kwds, "OOUi|OOOO:warn_explicit", kwd_list, &message, &category, &filename, &lineno, &module, - ®istry, &module_globals)) + ®istry, &module_globals, &sourceobj)) return NULL; if (module_globals) { @@ -870,14 +874,14 @@ warnings_warn_explicit(PyObject *self, PyObject *args, PyObject *kwds) /* Handle the warning. */ returned = warn_explicit(category, message, filename, lineno, module, - registry, source_line); + registry, source_line, sourceobj); Py_DECREF(source_list); return returned; } standard_call: return warn_explicit(category, message, filename, lineno, module, - registry, NULL); + registry, NULL, sourceobj); } static PyObject * @@ -892,14 +896,14 @@ warnings_filters_mutated(PyObject *self, PyObject *args) static int warn_unicode(PyObject *category, PyObject *message, - Py_ssize_t stack_level) + Py_ssize_t stack_level, PyObject *source) { PyObject *res; if (category == NULL) category = PyExc_RuntimeWarning; - res = do_warn(message, category, stack_level); + res = do_warn(message, category, stack_level, source); if (res == NULL) return -1; Py_DECREF(res); @@ -907,12 +911,28 @@ warn_unicode(PyObject *category, PyObject *message, return 0; } +static int +_PyErr_WarnFormatV(PyObject *source, + PyObject *category, Py_ssize_t stack_level, + const char *format, va_list vargs) +{ + PyObject *message; + int res; + + message = PyUnicode_FromFormatV(format, vargs); + if (message == NULL) + return -1; + + res = warn_unicode(category, message, stack_level, source); + Py_DECREF(message); + return res; +} + int PyErr_WarnFormat(PyObject *category, Py_ssize_t stack_level, const char *format, ...) { - int ret; - PyObject *message; + int res; va_list vargs; #ifdef HAVE_STDARG_PROTOTYPES @@ -920,17 +940,30 @@ PyErr_WarnFormat(PyObject *category, Py_ssize_t stack_level, #else va_start(vargs); #endif - message = PyUnicode_FromFormatV(format, vargs); - if (message != NULL) { - ret = warn_unicode(category, message, stack_level); - Py_DECREF(message); - } - else - ret = -1; + res = _PyErr_WarnFormatV(NULL, category, stack_level, format, vargs); va_end(vargs); - return ret; + return res; } +int +PyErr_ResourceWarning(PyObject *source, Py_ssize_t stack_level, + const char *format, ...) +{ + int res; + va_list vargs; + +#ifdef HAVE_STDARG_PROTOTYPES + va_start(vargs, format); +#else + va_start(vargs); +#endif + res = _PyErr_WarnFormatV(source, PyExc_ResourceWarning, + stack_level, format, vargs); + va_end(vargs); + return res; +} + + int PyErr_WarnEx(PyObject *category, const char *text, Py_ssize_t stack_level) { @@ -938,7 +971,7 @@ PyErr_WarnEx(PyObject *category, const char *text, Py_ssize_t stack_level) PyObject *message = PyUnicode_FromString(text); if (message == NULL) return -1; - ret = warn_unicode(category, message, stack_level); + ret = warn_unicode(category, message, stack_level, NULL); Py_DECREF(message); return ret; } @@ -964,7 +997,7 @@ PyErr_WarnExplicitObject(PyObject *category, PyObject *message, if (category == NULL) category = PyExc_RuntimeWarning; res = warn_explicit(category, message, filename, lineno, - module, registry, NULL); + module, registry, NULL, NULL); if (res == NULL) return -1; Py_DECREF(res); @@ -1028,7 +1061,7 @@ PyErr_WarnExplicitFormat(PyObject *category, if (message != NULL) { PyObject *res; res = warn_explicit(category, message, filename, lineno, - module, registry, NULL); + module, registry, NULL, NULL); Py_DECREF(message); if (res != NULL) { Py_DECREF(res); -- cgit v1.2.1 From f7d1ae10f3cb453a78ce7b4612148efb281e34ae Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Wed, 23 Mar 2016 00:28:08 +0100 Subject: Add a source parameter to warnings.warn() Issue #26604: * Add a new optional source parameter to _warnings.warn() and warnings.warn() * Modify asyncore, asyncio and _pyio modules to set the source parameter when logging a ResourceWarning warning --- Python/_warnings.c | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) (limited to 'Python/_warnings.c') diff --git a/Python/_warnings.c b/Python/_warnings.c index 25299fb622..dcac57bda5 100644 --- a/Python/_warnings.c +++ b/Python/_warnings.c @@ -787,18 +787,19 @@ do_warn(PyObject *message, PyObject *category, Py_ssize_t stack_level, static PyObject * warnings_warn(PyObject *self, PyObject *args, PyObject *kwds) { - static char *kw_list[] = { "message", "category", "stacklevel", 0 }; - PyObject *message, *category = NULL; + static char *kw_list[] = {"message", "category", "stacklevel", + "source", NULL}; + PyObject *message, *category = NULL, *source = NULL; Py_ssize_t stack_level = 1; - if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|On:warn", kw_list, - &message, &category, &stack_level)) + if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|OnO:warn", kw_list, + &message, &category, &stack_level, &source)) return NULL; category = get_category(message, category); if (category == NULL) return NULL; - return do_warn(message, category, stack_level, NULL); + return do_warn(message, category, stack_level, source); } static PyObject * -- cgit v1.2.1 From 0885b6320451c9f26194bdbf2e2dfab9539389cc Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Wed, 23 Mar 2016 00:54:48 +0100 Subject: _warnings.warn_explicit(): try to import warnings Issue #26592: _warnings.warn_explicit() now tries to import the warnings module (Python implementation) if the source parameter is set to be able to log the traceback where the source was allocated. --- Python/_warnings.c | 50 ++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 36 insertions(+), 14 deletions(-) (limited to 'Python/_warnings.c') diff --git a/Python/_warnings.c b/Python/_warnings.c index dcac57bda5..41eaf5310a 100644 --- a/Python/_warnings.c +++ b/Python/_warnings.c @@ -40,11 +40,11 @@ check_matched(PyObject *obj, PyObject *arg) A NULL return value can mean false or an error. */ static PyObject * -get_warnings_attr(const char *attr) +get_warnings_attr(const char *attr, int try_import) { static PyObject *warnings_str = NULL; PyObject *all_modules; - PyObject *warnings_module; + PyObject *warnings_module, *obj; int result; if (warnings_str == NULL) { @@ -53,15 +53,34 @@ get_warnings_attr(const char *attr) return NULL; } - all_modules = PyImport_GetModuleDict(); - result = PyDict_Contains(all_modules, warnings_str); - if (result == -1 || result == 0) + /* don't try to import after the start of the Python finallization */ + if (try_import && _Py_Finalizing == NULL) { + warnings_module = PyImport_Import(warnings_str); + if (warnings_module == NULL) { + /* Fallback to the C implementation if we cannot get + the Python implementation */ + PyErr_Clear(); + return NULL; + } + } + else { + all_modules = PyImport_GetModuleDict(); + result = PyDict_Contains(all_modules, warnings_str); + if (result == -1 || result == 0) + return NULL; + + warnings_module = PyDict_GetItem(all_modules, warnings_str); + Py_INCREF(warnings_module); + } + + if (!PyObject_HasAttrString(warnings_module, attr)) { + Py_DECREF(warnings_module); return NULL; + } - warnings_module = PyDict_GetItem(all_modules, warnings_str); - if (!PyObject_HasAttrString(warnings_module, attr)) - return NULL; - return PyObject_GetAttrString(warnings_module, attr); + obj = PyObject_GetAttrString(warnings_module, attr); + Py_DECREF(warnings_module); + return obj; } @@ -70,7 +89,7 @@ get_once_registry(void) { PyObject *registry; - registry = get_warnings_attr("onceregistry"); + registry = get_warnings_attr("onceregistry", 0); if (registry == NULL) { if (PyErr_Occurred()) return NULL; @@ -87,7 +106,7 @@ get_default_action(void) { PyObject *default_action; - default_action = get_warnings_attr("defaultaction"); + default_action = get_warnings_attr("defaultaction", 0); if (default_action == NULL) { if (PyErr_Occurred()) { return NULL; @@ -110,7 +129,7 @@ get_filter(PyObject *category, PyObject *text, Py_ssize_t lineno, Py_ssize_t i; PyObject *warnings_filters; - warnings_filters = get_warnings_attr("filters"); + warnings_filters = get_warnings_attr("filters", 0); if (warnings_filters == NULL) { if (PyErr_Occurred()) return NULL; @@ -366,7 +385,10 @@ call_show_warning(PyObject *category, PyObject *text, PyObject *message, { PyObject *show_fn, *msg, *res, *warnmsg_cls = NULL; - show_fn = get_warnings_attr("_showwarnmsg"); + /* If the source parameter is set, try to get the Python implementation. + The Python implementation is able to log the traceback where the source + was allocated, whereas the C implementation doesnt. */ + show_fn = get_warnings_attr("_showwarnmsg", source != NULL); if (show_fn == NULL) { if (PyErr_Occurred()) return -1; @@ -380,7 +402,7 @@ call_show_warning(PyObject *category, PyObject *text, PyObject *message, goto error; } - warnmsg_cls = get_warnings_attr("WarningMessage"); + warnmsg_cls = get_warnings_attr("WarningMessage", 0); if (warnmsg_cls == NULL) { PyErr_SetString(PyExc_RuntimeError, "unable to get warnings.WarningMessage"); -- cgit v1.2.1 From ef71e805bcf85d7522cfed7d8da1e7c4f572715e Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Wed, 23 Mar 2016 17:48:22 +0100 Subject: get_warnings_attr(): Fix coverity warning Don't check if the dict key exists before getting the key. Instead get the key and handle error. --- Python/_warnings.c | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) (limited to 'Python/_warnings.c') diff --git a/Python/_warnings.c b/Python/_warnings.c index 41eaf5310a..40f5c8ecfc 100644 --- a/Python/_warnings.c +++ b/Python/_warnings.c @@ -45,7 +45,6 @@ get_warnings_attr(const char *attr, int try_import) static PyObject *warnings_str = NULL; PyObject *all_modules; PyObject *warnings_module, *obj; - int result; if (warnings_str == NULL) { warnings_str = PyUnicode_InternFromString("warnings"); @@ -65,11 +64,11 @@ get_warnings_attr(const char *attr, int try_import) } else { all_modules = PyImport_GetModuleDict(); - result = PyDict_Contains(all_modules, warnings_str); - if (result == -1 || result == 0) - return NULL; warnings_module = PyDict_GetItem(all_modules, warnings_str); + if (warnings_module == NULL) + return NULL; + Py_INCREF(warnings_module); } -- cgit v1.2.1