Skip to content

Commit 52c5fd5

Browse files
cmaloneyclaude
andcommitted
gh-143201: Strip the source line with str.strip() like _py_warnings
Address review: strip both sides, run the test on all implementations, and move the NEWS entry to Library. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 0e78aaf commit 52c5fd5

4 files changed

Lines changed: 5 additions & 19 deletions

File tree

‎Lib/test/test_warnings/__init__.py‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1109,7 +1109,6 @@ def test_showwarnmsg_missing(self):
11091109
self.module._showwarnmsg = show
11101110
self.assertIn(text, result)
11111111

1112-
@support.cpython_only
11131112
def test_showwarnmsg_missing_source_line(self):
11141113
# gh-143201: the C fallback must strip the source line like Python.
11151114
lineno = sys._getframe().f_lineno

‎Misc/NEWS.d/next/Core_and_Builtins/2025-12-26-17-18-18.gh-issue-143201.UbXuX4.rst‎

Lines changed: 0 additions & 2 deletions
This file was deleted.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix the C implementation of :mod:`warnings` to strip the source line like the
2+
Python implementation.

‎Python/_warnings.c‎

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -698,23 +698,10 @@ show_warning(PyThreadState *tstate, PyObject *filename, int lineno,
698698

699699
/* Print " source_line\n" */
700700
if (sourceline) {
701-
int kind;
702-
const void *data;
703-
Py_ssize_t i, len;
704-
Py_UCS4 ch;
705-
706-
kind = PyUnicode_KIND(sourceline);
707-
data = PyUnicode_DATA(sourceline);
708-
len = PyUnicode_GET_LENGTH(sourceline);
709-
for (i=0; i<len; i++) {
710-
ch = PyUnicode_READ(kind, data, i);
711-
if (ch != ' ' && ch != '\t' && ch != '\014')
712-
break;
713-
}
714-
715-
truncated = PyUnicode_Substring(sourceline, i, len);
716-
if (truncated == NULL)
701+
truncated = PyObject_CallMethod(sourceline, "strip", NULL);
702+
if (truncated == NULL) {
717703
goto error;
704+
}
718705

719706
if (PyFile_WriteString(" ", f_stderr) < 0) {
720707
goto error;

0 commit comments

Comments
 (0)