Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions Lib/test/test_warnings/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1109,6 +1109,20 @@ def test_showwarnmsg_missing(self):
self.module._showwarnmsg = show
self.assertIn(text, result)

def test_showwarnmsg_missing_source_line(self):
# gh-143201: the C fallback must strip the source line like Python.
lineno = sys._getframe().f_lineno
with self.module.catch_warnings(), \
support.swap_attr(self.module, '_showwarnmsg', None), \
support.captured_stderr() as stream:
self.module.simplefilter("always")
del self.module._showwarnmsg # use the C fallback
self.module.warn_explicit('eggs', UserWarning, __file__, lineno,
module_globals=globals())
self.assertEqual(stream.getvalue(), py_warnings.formatwarning(
'eggs', UserWarning, __file__, lineno,
linecache.getline(__file__, lineno)))

def test_showwarning_not_callable(self):
orig = self.module.showwarning
try:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix the C implementation of :mod:`warnings` to strip the source line like the
Python implementation.
32 changes: 13 additions & 19 deletions Python/_warnings.c
Original file line number Diff line number Diff line change
Expand Up @@ -666,6 +666,7 @@ show_warning(PyThreadState *tstate, PyObject *filename, int lineno,
{
PyObject *f_stderr = NULL;
PyObject *name;
PyObject *truncated = NULL;
char lineno_str[128];

PyOS_snprintf(lineno_str, sizeof(lineno_str), ":%d: ", lineno);
Expand Down Expand Up @@ -697,28 +698,20 @@ show_warning(PyThreadState *tstate, PyObject *filename, int lineno,

/* Print " source_line\n" */
if (sourceline) {
int kind;
const void *data;
Py_ssize_t i, len;
Py_UCS4 ch;
PyObject *truncated;

kind = PyUnicode_KIND(sourceline);
data = PyUnicode_DATA(sourceline);
len = PyUnicode_GET_LENGTH(sourceline);
for (i=0; i<len; i++) {
ch = PyUnicode_READ(kind, data, i);
if (ch != ' ' && ch != '\t' && ch != '\014')
break;
truncated = PyObject_CallMethod(sourceline, "strip", NULL);
if (truncated == NULL) {
goto error;
}

truncated = PyUnicode_Substring(sourceline, i, len);
if (truncated == NULL)
if (PyFile_WriteString(" ", f_stderr) < 0) {
goto error;
Comment thread
cmaloney marked this conversation as resolved.
Resolved

PyFile_WriteObject(sourceline, f_stderr, Py_PRINT_RAW);
Py_DECREF(truncated);
PyFile_WriteString("\n", f_stderr);
}
if (PyFile_WriteObject(truncated, f_stderr, Py_PRINT_RAW) < 0) {
goto error;
}
if (PyFile_WriteString("\n", f_stderr) < 0) {
goto error;
}
}
else {
_Py_DisplaySourceLine(f_stderr, filename, lineno, 2, NULL, NULL);
Expand All @@ -727,6 +720,7 @@ show_warning(PyThreadState *tstate, PyObject *filename, int lineno,
error:
Py_XDECREF(f_stderr);
Py_XDECREF(name);
Py_XDECREF(truncated);
PyErr_Clear();
}

Expand Down
Loading