Skip to content

Commit

Permalink
[3.11] gh-111380: Show SyntaxWarnings only once when parsing if inval…
Browse files Browse the repository at this point in the history
…id syntax is encouintered (GH-111381) (#111383)

gh-111380: Show SyntaxWarnings only once when parsing if invalid syntax is encouintered (GH-111381)
(cherry picked from commit 3d2f1f0)

Co-authored-by: Pablo Galindo Salgado <Pablogsal@gmail.com>
  • Loading branch information
miss-islington and pablogsal committed Oct 31, 2023
1 parent c66f0be commit 08e4e11
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 0 deletions.
12 changes: 12 additions & 0 deletions Lib/test/test_string_literals.py
Expand Up @@ -131,6 +131,18 @@ def test_eval_str_invalid_escape(self):
self.assertEqual(exc.lineno, 1)
self.assertEqual(exc.offset, 1)

# Check that the warning is raised ony once if there are syntax errors

with warnings.catch_warnings(record=True) as w:
warnings.simplefilter('always', category=DeprecationWarning)
with self.assertRaises(SyntaxError) as cm:
eval("'\\e' $")
exc = cm.exception
self.assertEqual(len(w), 1)
self.assertEqual(w[0].category, DeprecationWarning)
self.assertRegex(str(w[0].message), 'invalid escape sequence')
self.assertEqual(w[0].filename, '<string>')

def test_eval_str_invalid_octal_escape(self):
for i in range(0o400, 0o1000):
with self.assertWarns(DeprecationWarning):
Expand Down
@@ -0,0 +1,2 @@
Fix a bug that was causing :exc:`SyntaxWarning` to appear twice when parsing
if invalid syntax is encountered later. Patch by Pablo galindo
5 changes: 5 additions & 0 deletions Parser/string_parser.c
Expand Up @@ -11,6 +11,11 @@
static int
warn_invalid_escape_sequence(Parser *p, const char *first_invalid_escape, Token *t)
{
if (p->call_invalid_rules) {
// Do not report warnings if we are in the second pass of the parser
// to avoid showing the warning twice.
return 0;
}
unsigned char c = *first_invalid_escape;
int octal = ('4' <= c && c <= '7');
PyObject *msg =
Expand Down

0 comments on commit 08e4e11

Please sign in to comment.