summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_re.py
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2022-03-19 14:10:44 (GMT)
committerGitHub <noreply@github.com>2022-03-19 14:10:44 (GMT)
commit92a6abf72e7a8274f96edbb5297119d4ff055be7 (patch)
tree158af2148f4027e63d9b19cfaf968b6da7848c2e /Lib/test/test_re.py
parentcb7874f49d3d55df73a3c529773af14e2e344fb7 (diff)
downloadcpython-92a6abf72e7a8274f96edbb5297119d4ff055be7.zip
cpython-92a6abf72e7a8274f96edbb5297119d4ff055be7.tar.gz
cpython-92a6abf72e7a8274f96edbb5297119d4ff055be7.tar.bz2
bpo-47066: Convert a warning about flags not at the start of the regular expression into error (GH-31994)
Diffstat (limited to 'Lib/test/test_re.py')
-rw-r--r--Lib/test/test_re.py68
1 files changed, 12 insertions, 56 deletions
diff --git a/Lib/test/test_re.py b/Lib/test/test_re.py
index 48dcb16..f8bbe51 100644
--- a/Lib/test/test_re.py
+++ b/Lib/test/test_re.py
@@ -1439,66 +1439,22 @@ class ReTests(unittest.TestCase):
self.assertTrue(re.match('(?x) (?i) ' + upper_char, lower_char))
self.assertTrue(re.match(' (?x) (?i) ' + upper_char, lower_char, re.X))
- p = upper_char + '(?i)'
- with self.assertWarns(DeprecationWarning) as warns:
- self.assertTrue(re.match(p, lower_char))
- self.assertEqual(
- str(warns.warnings[0].message),
- 'Flags not at the start of the expression %r'
- ' but at position 1' % p
- )
- self.assertEqual(warns.warnings[0].filename, __file__)
-
- p = upper_char + '(?i)%s' % ('.?' * 100)
- with self.assertWarns(DeprecationWarning) as warns:
- self.assertTrue(re.match(p, lower_char))
- self.assertEqual(
- str(warns.warnings[0].message),
- 'Flags not at the start of the expression %r (truncated)'
- ' but at position 1' % p[:20]
- )
- self.assertEqual(warns.warnings[0].filename, __file__)
+ msg = "global flags not at the start of the expression"
+ self.checkPatternError(upper_char + '(?i)', msg, 1)
# bpo-30605: Compiling a bytes instance regex was throwing a BytesWarning
with warnings.catch_warnings():
warnings.simplefilter('error', BytesWarning)
- p = b'A(?i)'
- with self.assertWarns(DeprecationWarning) as warns:
- self.assertTrue(re.match(p, b'a'))
- self.assertEqual(
- str(warns.warnings[0].message),
- 'Flags not at the start of the expression %r'
- ' but at position 1' % p
- )
- self.assertEqual(warns.warnings[0].filename, __file__)
-
- with self.assertWarns(DeprecationWarning):
- self.assertTrue(re.match('(?s).(?i)' + upper_char, '\n' + lower_char))
- with self.assertWarns(DeprecationWarning):
- self.assertTrue(re.match('(?i) ' + upper_char + ' (?x)', lower_char))
- with self.assertWarns(DeprecationWarning):
- self.assertTrue(re.match(' (?x) (?i) ' + upper_char, lower_char))
- with self.assertWarns(DeprecationWarning):
- 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) as warns:
- self.assertTrue(re.match('(?:(?i)' + upper_char + ')', lower_char))
- 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__)
- 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__)
+ self.checkPatternError(b'A(?i)', msg, 1)
+
+ self.checkPatternError('(?s).(?i)' + upper_char, msg, 5)
+ self.checkPatternError('(?i) ' + upper_char + ' (?x)', msg, 7)
+ self.checkPatternError(' (?x) (?i) ' + upper_char, msg, 1)
+ self.checkPatternError('^(?i)' + upper_char, msg, 1)
+ self.checkPatternError('$|(?i)' + upper_char, msg, 2)
+ self.checkPatternError('(?:(?i)' + upper_char + ')', msg, 3)
+ self.checkPatternError('(^)?(?(1)(?i)' + upper_char + ')', msg, 9)
+ self.checkPatternError('($)?(?(1)|(?i)' + upper_char + ')', msg, 10)
def test_dollar_matches_twice(self):