Skip to content

Commit 4ff5d88

Browse files
authored
[3.13] gh-131927: Prevent emitting compiler warnings twice (GH-131993) (GH-132463)
(cherry picked from commit 3d08c8a)
1 parent ff66901 commit 4ff5d88

File tree

4 files changed

+48
-2
lines changed

4 files changed

+48
-2
lines changed

Include/cpython/warnings.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,9 @@ PyAPI_FUNC(int) PyErr_WarnExplicitFormat(
1818

1919
// DEPRECATED: Use PyErr_WarnEx() instead.
2020
#define PyErr_Warn(category, msg) PyErr_WarnEx((category), (msg), 1)
21+
22+
int _PyErr_WarnExplicitObjectWithContext(
23+
PyObject *category,
24+
PyObject *message,
25+
PyObject *filename,
26+
int lineno);

Lib/test/test_compile.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1512,6 +1512,24 @@ async def name_4():
15121512
pass
15131513
[[]]
15141514

1515+
def test_compile_warnings(self):
1516+
# See gh-131927
1517+
# Compile warnings originating from the same file and
1518+
# line are now only emitted once.
1519+
with warnings.catch_warnings(record=True) as caught:
1520+
warnings.simplefilter("default")
1521+
compile('1 is 1', '<stdin>', 'eval')
1522+
compile('1 is 1', '<stdin>', 'eval')
1523+
1524+
self.assertEqual(len(caught), 1)
1525+
1526+
with warnings.catch_warnings(record=True) as caught:
1527+
warnings.simplefilter("always")
1528+
compile('1 is 1', '<stdin>', 'eval')
1529+
compile('1 is 1', '<stdin>', 'eval')
1530+
1531+
self.assertEqual(len(caught), 2)
1532+
15151533
@requires_debug_ranges()
15161534
class TestSourcePositions(unittest.TestCase):
15171535
# Ensure that compiled code snippets have correct line and column numbers

Python/_warnings.c

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1317,6 +1317,28 @@ PyErr_WarnExplicitObject(PyObject *category, PyObject *message,
13171317
return 0;
13181318
}
13191319

1320+
/* Like PyErr_WarnExplicitObject, but automatically sets up context */
1321+
int
1322+
_PyErr_WarnExplicitObjectWithContext(PyObject *category, PyObject *message,
1323+
PyObject *filename, int lineno)
1324+
{
1325+
PyObject *unused_filename, *module, *registry;
1326+
int unused_lineno;
1327+
int stack_level = 1;
1328+
1329+
if (!setup_context(stack_level, NULL, &unused_filename, &unused_lineno,
1330+
&module, &registry)) {
1331+
return -1;
1332+
}
1333+
1334+
int rc = PyErr_WarnExplicitObject(category, message, filename, lineno,
1335+
module, registry);
1336+
Py_DECREF(unused_filename);
1337+
Py_DECREF(registry);
1338+
Py_DECREF(module);
1339+
return rc;
1340+
}
1341+
13201342
int
13211343
PyErr_WarnExplicit(PyObject *category, const char *text,
13221344
const char *filename_str, int lineno,

Python/compile.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6616,8 +6616,8 @@ compiler_warn(struct compiler *c, location loc,
66166616
if (msg == NULL) {
66176617
return ERROR;
66186618
}
6619-
if (PyErr_WarnExplicitObject(PyExc_SyntaxWarning, msg, c->c_filename,
6620-
loc.lineno, NULL, NULL) < 0)
6619+
if (_PyErr_WarnExplicitObjectWithContext(PyExc_SyntaxWarning, msg,
6620+
c->c_filename, loc.lineno) < 0)
66216621
{
66226622
if (PyErr_ExceptionMatches(PyExc_SyntaxWarning)) {
66236623
/* Replace the SyntaxWarning exception with a SyntaxError

0 commit comments

Comments
 (0)