mirror of
https://github.com/python/cpython.git
synced 2025-12-23 09:19:18 +00:00
[3.11] gh-99891: Fix infinite recursion in the tokenizer when showing warnings (GH-99893) (GH-99896)
Automerge-Triggered-By: GH:pablogsal.
(cherry picked from commit 417206a05c)
Co-authored-by: Pablo Galindo Salgado <Pablogsal@gmail.com>
This commit is contained in:
parent
0076ca48e9
commit
6282ef6c3f
4 changed files with 24 additions and 0 deletions
|
|
@ -161,6 +161,18 @@ class MiscSourceEncodingTest(unittest.TestCase):
|
|||
finally:
|
||||
os.unlink(TESTFN)
|
||||
|
||||
def test_tokenizer_fstring_warning_in_first_line(self):
|
||||
source = "0b1and 2"
|
||||
with open(TESTFN, "w") as fd:
|
||||
fd.write("{}".format(source))
|
||||
try:
|
||||
retcode, stdout, stderr = script_helper.assert_python_ok(TESTFN)
|
||||
self.assertIn(b"SyntaxWarning: invalid binary litera", stderr)
|
||||
self.assertEqual(stderr.count(source.encode()), 1)
|
||||
finally:
|
||||
os.unlink(TESTFN)
|
||||
|
||||
|
||||
class AbstractSourceEncodingTest:
|
||||
|
||||
def test_default_coding(self):
|
||||
|
|
|
|||
|
|
@ -0,0 +1,3 @@
|
|||
Fix a bug in the tokenizer that could cause infinite recursion when showing
|
||||
syntax warnings that happen in the first line of the source. Patch by Pablo
|
||||
Galindo
|
||||
|
|
@ -88,6 +88,7 @@ tok_new(void)
|
|||
tok->async_def_nl = 0;
|
||||
tok->interactive_underflow = IUNDERFLOW_NORMAL;
|
||||
tok->str = NULL;
|
||||
tok->report_warnings = 1;
|
||||
return tok;
|
||||
}
|
||||
|
||||
|
|
@ -1186,6 +1187,10 @@ indenterror(struct tok_state *tok)
|
|||
static int
|
||||
parser_warn(struct tok_state *tok, PyObject *category, const char *format, ...)
|
||||
{
|
||||
if (!tok->report_warnings) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
PyObject *errmsg;
|
||||
va_list vargs;
|
||||
#ifdef HAVE_STDARG_PROTOTYPES
|
||||
|
|
@ -2194,6 +2199,9 @@ _PyTokenizer_FindEncodingFilename(int fd, PyObject *filename)
|
|||
return encoding;
|
||||
}
|
||||
}
|
||||
// We don't want to report warnings here because it could cause infinite recursion
|
||||
// if fetching the encoding shows a warning.
|
||||
tok->report_warnings = 0;
|
||||
while (tok->lineno < 2 && tok->done == E_OK) {
|
||||
_PyTokenizer_Get(tok, &p_start, &p_end);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -84,6 +84,7 @@ struct tok_state {
|
|||
NEWLINE token after it. */
|
||||
/* How to proceed when asked for a new token in interactive mode */
|
||||
enum interactive_underflow_t interactive_underflow;
|
||||
int report_warnings;
|
||||
};
|
||||
|
||||
extern struct tok_state *_PyTokenizer_FromString(const char *, int);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue