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
15 changes: 15 additions & 0 deletions Lib/test/test_warnings/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1109,6 +1109,21 @@ def test_showwarnmsg_missing(self):
self.module._showwarnmsg = show
self.assertIn(text, result)

@support.cpython_only

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
@support.cpython_only

I'm not sure why it's needed, we're testing the C module?

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

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please move to Library.

Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix the C implementation of :mod:`warnings` to strip the indentation of the

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Technically... we're still differing from:

line = line.strip()

We only strip leading whitespace.

source line like the Python implementation.
15 changes: 11 additions & 4 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 @@ -701,7 +702,6 @@ show_warning(PyThreadState *tstate, PyObject *filename, int lineno,
const void *data;
Py_ssize_t i, len;
Py_UCS4 ch;
PyObject *truncated;

kind = PyUnicode_KIND(sourceline);
data = PyUnicode_DATA(sourceline);
Expand All @@ -716,9 +716,15 @@ show_warning(PyThreadState *tstate, PyObject *filename, int lineno,
if (truncated == NULL)
goto error;

PyFile_WriteObject(sourceline, f_stderr, Py_PRINT_RAW);
Py_DECREF(truncated);
PyFile_WriteString("\n", f_stderr);
if (PyFile_WriteString(" ", f_stderr) < 0) {
goto error;
Comment thread
cmaloney marked this conversation as resolved.
Resolved
}
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 +733,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