diff options
author | Miss Islington (bot) <31488909+miss-islington@users.noreply.github.com> | 2023-06-20 13:09:48 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-06-20 13:09:48 (GMT) |
commit | fea0d2fbaae69d95549891678339a67549a57e38 (patch) | |
tree | 35bc7014c6559fcd8cf9f5a9067b823bc6a3e568 /Lib | |
parent | cc18a8b78ac85159dad8f2f3ee93799f5df6fa82 (diff) | |
download | cpython-fea0d2fbaae69d95549891678339a67549a57e38.zip cpython-fea0d2fbaae69d95549891678339a67549a57e38.tar.gz cpython-fea0d2fbaae69d95549891678339a67549a57e38.tar.bz2 |
[3.12] gh-105938: Emit a SyntaxWarning for escaped braces in an f-string (GH-105939) (#105941)
(cherry picked from commit 6586cee27f32f0354fe4e77c7b8c6e399329b5e2)
Co-authored-by: Lysandros Nikolaou <lisandrosnik@gmail.com>
Diffstat (limited to 'Lib')
-rw-r--r-- | Lib/test/test_fstring.py | 11 |
1 files changed, 9 insertions, 2 deletions
diff --git a/Lib/test/test_fstring.py b/Lib/test/test_fstring.py index 8f6b576..ad5ac6a 100644 --- a/Lib/test/test_fstring.py +++ b/Lib/test/test_fstring.py @@ -13,6 +13,7 @@ import re import types import decimal import unittest +import warnings from test import support from test.support.os_helper import temp_cwd from test.support.script_helper import assert_python_failure, assert_python_ok @@ -904,7 +905,7 @@ x = ( self.assertEqual(f'2\x203', '2 3') self.assertEqual(f'\x203', ' 3') - with self.assertWarns(DeprecationWarning): # invalid escape sequence + with self.assertWarns(SyntaxWarning): # invalid escape sequence value = eval(r"f'\{6*7}'") self.assertEqual(value, '\\42') with self.assertWarns(SyntaxWarning): # invalid escape sequence @@ -1037,7 +1038,7 @@ x = ( ] for case, expected_result in deprecated_cases: with self.subTest(case=case, expected_result=expected_result): - with self.assertWarns(DeprecationWarning): + with self.assertWarns(SyntaxWarning): result = eval(case) self.assertEqual(result, expected_result) self.assertEqual(fr'\{{\}}', '\\{\\}') @@ -1046,6 +1047,12 @@ x = ( self.assertEqual(fr'\}}{1+1}', '\\}2') self.assertEqual(fr'{1+1}\}}', '2\\}') + def test_fstring_backslash_before_double_bracket_warns_once(self): + with warnings.catch_warnings(record=True) as w: + eval(r"f'\{{'") + self.assertEqual(len(w), 1) + self.assertEqual(w[0].category, SyntaxWarning) + def test_fstring_backslash_prefix_raw(self): self.assertEqual(f'\\', '\\') self.assertEqual(f'\\\\', '\\\\') |