diff options
author | Tomas R. <tomas.roun8@gmail.com> | 2025-04-12 12:34:36 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2025-04-12 11:34:36 +0100 |
commit | 3d08c8ad20dfabd4864be139cd9c2eb5602ccdfe (patch) | |
tree | e1a0eb511b507db7c1d21be8f730504e55024e76 /Python | |
parent | d4e2cdc15bdfe84486b885eca885c44bc378fc41 (diff) | |
download | cpython-3d08c8ad20dfabd4864be139cd9c2eb5602ccdfe.tar.gz cpython-3d08c8ad20dfabd4864be139cd9c2eb5602ccdfe.zip |
gh-131927: Prevent emitting optimizer warnings twice in the REPL (#131993)
Diffstat (limited to 'Python')
-rw-r--r-- | Python/_warnings.c | 22 | ||||
-rw-r--r-- | Python/errors.c | 4 |
2 files changed, 24 insertions, 2 deletions
diff --git a/Python/_warnings.c b/Python/_warnings.c index 912468d2a59..39bf1b225cc 100644 --- a/Python/_warnings.c +++ b/Python/_warnings.c @@ -1479,6 +1479,28 @@ PyErr_WarnExplicitObject(PyObject *category, PyObject *message, return 0; } +/* Like PyErr_WarnExplicitObject, but automatically sets up context */ +int +_PyErr_WarnExplicitObjectWithContext(PyObject *category, PyObject *message, + PyObject *filename, int lineno) +{ + PyObject *unused_filename, *module, *registry; + int unused_lineno; + int stack_level = 1; + + if (!setup_context(stack_level, NULL, &unused_filename, &unused_lineno, + &module, ®istry)) { + return -1; + } + + int rc = PyErr_WarnExplicitObject(category, message, filename, lineno, + module, registry); + Py_DECREF(unused_filename); + Py_DECREF(registry); + Py_DECREF(module); + return rc; +} + int PyErr_WarnExplicit(PyObject *category, const char *text, const char *filename_str, int lineno, diff --git a/Python/errors.c b/Python/errors.c index 14999d6dbaf..2b088e2f388 100644 --- a/Python/errors.c +++ b/Python/errors.c @@ -1906,8 +1906,8 @@ int _PyErr_EmitSyntaxWarning(PyObject *msg, PyObject *filename, int lineno, int col_offset, int end_lineno, int end_col_offset) { - if (PyErr_WarnExplicitObject(PyExc_SyntaxWarning, msg, filename, - lineno, NULL, NULL) < 0) + if (_PyErr_WarnExplicitObjectWithContext(PyExc_SyntaxWarning, msg, + filename, lineno) < 0) { if (PyErr_ExceptionMatches(PyExc_SyntaxWarning)) { /* Replace the SyntaxWarning exception with a SyntaxError |