[3.14] gh-155319: Fix the source line of a warning issued with module_globals (GH-155320) (GH-155825)

warn_explicit() computed the source line from the loader of the module
whose globals were passed as module_globals, but did not pass it to
WarningMessage.  It also raised IndexError if lineno was out of the range
of the module source.

(cherry picked from commit c92e2fd8f9)
This commit is contained in:
Serhiy Storchaka authored and GitHub committed 2026-08-15 07:39:27 +00:00
1 parent 4dbd529f05
commit 94e5346c84
3 files changed
+43 -3

No files matched your search

+8 -3
View File
@@ -768,7 +768,9 @@ call_show_warning(PyThreadState *tstate, PyObject *category,
}
msg = PyObject_CallFunctionObjArgs(warnmsg_cls, message, category,
filename, lineno_obj, Py_None, Py_None, source,
filename, lineno_obj, Py_None,
sourceline ? sourceline : Py_None,
source,
NULL);
Py_DECREF(warnmsg_cls);
if (msg == NULL)
@@ -1269,8 +1271,11 @@ get_source_line(PyInterpreterState *interp, PyObject *module_globals, int lineno
}
/* Get the source line. */
source_line = PyList_GetItem(source_list, lineno-1);
Py_XINCREF(source_line);
if (lineno < 1 || lineno > PyList_GET_SIZE(source_list)) {
Py_DECREF(source_list);
return NULL;
}
source_line = Py_NewRef(PyList_GET_ITEM(source_list, lineno-1));
Py_DECREF(source_list);
return source_line;
}