bpo-30375: Correct the stacklevel of regex compiling warnings. (#1595)
Warnings emitted when compile a regular expression now always point
to the line in the user code. Previously they could point into inners
of the re module if emitted from inside of groups or conditionals.
diff --git a/Lib/test/test_re.py b/Lib/test/test_re.py
index 1bb2654..027df40 100644
--- a/Lib/test/test_re.py
+++ b/Lib/test/test_re.py
@@ -1370,6 +1370,7 @@
str(warns.warnings[0].message),
'Flags not at the start of the expression %s' % p
)
+ self.assertEqual(warns.warnings[0].filename, __file__)
p = upper_char + '(?i)%s' % ('.?' * 100)
with self.assertWarns(DeprecationWarning) as warns:
@@ -1378,6 +1379,7 @@
str(warns.warnings[0].message),
'Flags not at the start of the expression %s (truncated)' % p[:20]
)
+ self.assertEqual(warns.warnings[0].filename, __file__)
with self.assertWarns(DeprecationWarning):
self.assertTrue(re.match('(?s).(?i)' + upper_char, '\n' + lower_char))
@@ -1389,14 +1391,23 @@
self.assertTrue(re.match('^(?i)' + upper_char, lower_char))
with self.assertWarns(DeprecationWarning):
self.assertTrue(re.match('$|(?i)' + upper_char, lower_char))
- with self.assertWarns(DeprecationWarning):
+ with self.assertWarns(DeprecationWarning) as warns:
self.assertTrue(re.match('(?:(?i)' + upper_char + ')', lower_char))
- with self.assertWarns(DeprecationWarning):
+ self.assertRegex(str(warns.warnings[0].message),
+ 'Flags not at the start')
+ self.assertEqual(warns.warnings[0].filename, __file__)
+ with self.assertWarns(DeprecationWarning) as warns:
self.assertTrue(re.fullmatch('(^)?(?(1)(?i)' + upper_char + ')',
lower_char))
- with self.assertWarns(DeprecationWarning):
+ self.assertRegex(str(warns.warnings[0].message),
+ 'Flags not at the start')
+ self.assertEqual(warns.warnings[0].filename, __file__)
+ with self.assertWarns(DeprecationWarning) as warns:
self.assertTrue(re.fullmatch('($)?(?(1)|(?i)' + upper_char + ')',
lower_char))
+ self.assertRegex(str(warns.warnings[0].message),
+ 'Flags not at the start')
+ self.assertEqual(warns.warnings[0].filename, __file__)
def test_dollar_matches_twice(self):