From cda2f6fd1ca6c447c6d5910d5f172e9fc59e4e89 Mon Sep 17 00:00:00 2001 From: "Erlend E. Aasland" Date: Wed, 13 Mar 2024 00:24:44 +0100 Subject: [PATCH 1/5] In already_warned, replace _PyDict_GetItemWithError with PyDict_GetItemRef --- Python/_warnings.c | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/Python/_warnings.c b/Python/_warnings.c index d4765032824e56..4b1043ed3c9dd8 100644 --- a/Python/_warnings.c +++ b/Python/_warnings.c @@ -8,6 +8,8 @@ #include "pycore_sysmodule.h" // _PySys_GetAttr() #include "pycore_traceback.h" // _Py_DisplaySourceLine() +#include + #include "clinic/_warnings.c.h" #define MODULE_NAME "_warnings" @@ -397,7 +399,7 @@ static int already_warned(PyInterpreterState *interp, PyObject *registry, PyObject *key, int should_set) { - PyObject *version_obj, *already_warned; + PyObject *already_warned; if (key == NULL) return -1; @@ -406,14 +408,17 @@ already_warned(PyInterpreterState *interp, PyObject *registry, PyObject *key, if (st == NULL) { return -1; } - version_obj = _PyDict_GetItemWithError(registry, &_Py_ID(version)); - if (version_obj == NULL + PyObject *version_obj; + if (PyDict_GetItemRef(registry, &_Py_ID(version), &version_obj) < 0) { + return -1; + } + bool should_update_version = ( + version_obj == NULL || !PyLong_CheckExact(version_obj) - || PyLong_AsLong(version_obj) != st->filters_version) - { - if (PyErr_Occurred()) { - return -1; - } + || PyLong_AsLong(version_obj) != st->filters_version + ); + Py_XDECREF(version_obj); + if (should_update_version) { PyDict_Clear(registry); version_obj = PyLong_FromLong(st->filters_version); if (version_obj == NULL) From d05c6789289a14ee7f6f836f428030222c243495 Mon Sep 17 00:00:00 2001 From: "Erlend E. Aasland" Date: Wed, 13 Mar 2024 00:27:35 +0100 Subject: [PATCH 2/5] In setup_context, replace _PyDict_GetItemWithError with PyDict_GetItemRef --- Python/_warnings.c | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/Python/_warnings.c b/Python/_warnings.c index 4b1043ed3c9dd8..afa927851d9349 100644 --- a/Python/_warnings.c +++ b/Python/_warnings.c @@ -916,13 +916,12 @@ setup_context(Py_ssize_t stack_level, /* Setup registry. */ assert(globals != NULL); assert(PyDict_Check(globals)); - *registry = _PyDict_GetItemWithError(globals, &_Py_ID(__warningregistry__)); + int rc = PyDict_GetItemRef(globals, &_Py_ID(__warningregistry__), + registry); + if (rc < 0) { + goto handle_error; + } if (*registry == NULL) { - int rc; - - if (_PyErr_Occurred(tstate)) { - goto handle_error; - } *registry = PyDict_New(); if (*registry == NULL) goto handle_error; @@ -931,8 +930,6 @@ setup_context(Py_ssize_t stack_level, if (rc < 0) goto handle_error; } - else - Py_INCREF(*registry); /* Setup module. */ *module = _PyDict_GetItemWithError(globals, &_Py_ID(__name__)); From eeb09366d67f22907629f1488532697b7cd6ec96 Mon Sep 17 00:00:00 2001 From: "Erlend E. Aasland" Date: Wed, 13 Mar 2024 00:35:02 +0100 Subject: [PATCH 3/5] In setup_context, replace _PyDict_GetItemWithError with PyDict_GetItemRef --- Python/_warnings.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/Python/_warnings.c b/Python/_warnings.c index afa927851d9349..b3435712dd432b 100644 --- a/Python/_warnings.c +++ b/Python/_warnings.c @@ -932,18 +932,18 @@ setup_context(Py_ssize_t stack_level, } /* Setup module. */ - *module = _PyDict_GetItemWithError(globals, &_Py_ID(__name__)); - if (*module == Py_None || (*module != NULL && PyUnicode_Check(*module))) { - Py_INCREF(*module); - } - else if (_PyErr_Occurred(tstate)) { + rc = PyDict_GetItemRef(globals, &_Py_ID(__name__), module); + if (rc < 0) { goto handle_error; } - else { + if (rc == 0) { *module = PyUnicode_FromString(""); if (*module == NULL) goto handle_error; } + else { + assert(Py_IsNone(*module) || PyUnicode_Check(*module)); + } return 1; From 4d4371c782a6add47ac9197f67627874b7ffe043 Mon Sep 17 00:00:00 2001 From: "Erlend E. Aasland" Date: Wed, 13 Mar 2024 00:38:17 +0100 Subject: [PATCH 4/5] In get_source_line, replace _PyDict_GetItemWithError with PyDict_GetItemRef --- Python/_warnings.c | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/Python/_warnings.c b/Python/_warnings.c index b3435712dd432b..2edffa05665ff2 100644 --- a/Python/_warnings.c +++ b/Python/_warnings.c @@ -1,5 +1,4 @@ #include "Python.h" -#include "pycore_dict.h" // _PyDict_GetItemWithError() #include "pycore_interp.h" // PyInterpreterState.warnings #include "pycore_long.h" // _PyLong_GetZero() #include "pycore_pyerrors.h" // _PyErr_Occurred() @@ -1065,12 +1064,12 @@ get_source_line(PyInterpreterState *interp, PyObject *module_globals, int lineno return NULL; } - module_name = _PyDict_GetItemWithError(module_globals, &_Py_ID(__name__)); - if (!module_name) { + int rc = PyDict_GetItemRef(module_globals, &_Py_ID(__name__), + &module_name); + if (rc < 0 || rc == 0) { Py_DECREF(loader); return NULL; } - Py_INCREF(module_name); /* Make sure the loader implements the optional get_source() method. */ (void)PyObject_GetOptionalAttr(loader, &_Py_ID(get_source), &get_source); From 4f3cd028b6bd1617e48ed49b500d7e3bca2aac27 Mon Sep 17 00:00:00 2001 From: "Erlend E. Aasland" Date: Thu, 14 Mar 2024 00:26:01 +0100 Subject: [PATCH 5/5] Address review: handle case where __name__ is not None and not a unicode object --- Python/_warnings.c | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/Python/_warnings.c b/Python/_warnings.c index 2edffa05665ff2..dfa82c569e1383 100644 --- a/Python/_warnings.c +++ b/Python/_warnings.c @@ -935,13 +935,15 @@ setup_context(Py_ssize_t stack_level, if (rc < 0) { goto handle_error; } - if (rc == 0) { - *module = PyUnicode_FromString(""); - if (*module == NULL) - goto handle_error; + if (rc > 0) { + if (Py_IsNone(*module) || PyUnicode_Check(*module)) { + return 1; + } + Py_DECREF(*module); } - else { - assert(Py_IsNone(*module) || PyUnicode_Check(*module)); + *module = PyUnicode_FromString(""); + if (*module == NULL) { + goto handle_error; } return 1;