Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitedeff24

Browse files
committed
gh-99891: Fix infinite recursion in the tokenizer when showing warnings
1 parent05dfc53 commitedeff24

File tree

4 files changed

+25
-0
lines changed

4 files changed

+25
-0
lines changed

‎Lib/test/test_source_encoding.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,19 @@ def test_file_parse_error_multiline(self):
160160
finally:
161161
os.unlink(TESTFN)
162162

163+
deftest_tokenizer_fstring_warning_in_first_line(self):
164+
source="0b1and 2"
165+
withopen(TESTFN,"w")asfd:
166+
fd.write("{}".format(source))
167+
try:
168+
retcode,stdout,stderr=script_helper.assert_python_failure(TESTFN,PYTHONWARNINGS="error")
169+
self.assertGreater(retcode,0)
170+
self.assertIn(b"SyntaxError: invalid binary litera",stderr)
171+
self.assertEqual(stderr.count(source.encode()),1)
172+
finally:
173+
os.unlink(TESTFN)
174+
175+
163176
classAbstractSourceEncodingTest:
164177

165178
deftest_default_coding(self):
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Fix a bug in the tokenizer that could cause infinite recursion when showing
2+
syntax warnings that happen in the first line of the source. Patch by Pablo
3+
Galindo

‎Parser/tokenizer.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ tok_new(void)
9797
tok->async_def_nl=0;
9898
tok->interactive_underflow=IUNDERFLOW_NORMAL;
9999
tok->str=NULL;
100+
tok->report_warnings=1;
100101
#ifdefPy_DEBUG
101102
tok->debug=_Py_GetConfig()->parser_debug;
102103
#endif
@@ -1201,6 +1202,10 @@ indenterror(struct tok_state *tok)
12011202
staticint
12021203
parser_warn(structtok_state*tok,PyObject*category,constchar*format, ...)
12031204
{
1205+
if (!tok->report_warnings) {
1206+
return0;
1207+
}
1208+
12041209
PyObject*errmsg;
12051210
va_listvargs;
12061211
va_start(vargs,format);
@@ -2239,6 +2244,9 @@ _PyTokenizer_FindEncodingFilename(int fd, PyObject *filename)
22392244
}
22402245
}
22412246
structtokentoken;
2247+
// We don't want to report warnings here because it could cause infinite recursion
2248+
// if fetching the encoding shows a warning.
2249+
tok->report_warnings=0;
22422250
while (tok->lineno<2&&tok->done==E_OK) {
22432251
_PyTokenizer_Get(tok,&token);
22442252
}

‎Parser/tokenizer.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ struct tok_state {
9292
NEWLINE token after it. */
9393
/* How to proceed when asked for a new token in interactive mode */
9494
enuminteractive_underflow_tinteractive_underflow;
95+
intreport_warnings;
9596
#ifdefPy_DEBUG
9697
intdebug;
9798
#endif

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp