self.checkPatternError(r'(?<>)', 'unknown extension ?<>', 1)
self.checkPatternError(r'(?', 'unexpected end of pattern', 2)
+ def test_enum(self):
+ # Issue #28082: Check that str(flag) returns a human readable string
+ # instead of an integer
+ self.assertIn('ASCII', str(re.A))
+ self.assertIn('DOTALL', str(re.S))
+
+ def test_pattern_compare(self):
+ pattern1 = re.compile('abc', re.IGNORECASE)
+
+ # equal to itself
+ self.assertEqual(pattern1, pattern1)
+ self.assertFalse(pattern1 != pattern1)
+
+ # equal
+ re.purge()
+ pattern2 = re.compile('abc', re.IGNORECASE)
+ self.assertEqual(hash(pattern2), hash(pattern1))
+ self.assertEqual(pattern2, pattern1)
+
+ # not equal: different pattern
+ re.purge()
+ pattern3 = re.compile('XYZ', re.IGNORECASE)
+ # Don't test hash(pattern3) != hash(pattern1) because there is no
+ # warranty that hash values are different
+ self.assertNotEqual(pattern3, pattern1)
+
+ # not equal: different flag (flags=0)
+ re.purge()
+ pattern4 = re.compile('abc')
+ self.assertNotEqual(pattern4, pattern1)
+
+ # only == and != comparison operators are supported
+ with self.assertRaises(TypeError):
+ pattern1 < pattern2
+
+ def test_pattern_compare_bytes(self):
+ pattern1 = re.compile(b'abc')
+
+ # equal: test bytes patterns
+ re.purge()
+ pattern2 = re.compile(b'abc')
+ self.assertEqual(hash(pattern2), hash(pattern1))
+ self.assertEqual(pattern2, pattern1)
+
+ # not equal: pattern of a different types (str vs bytes),
+ # comparison must not raise a BytesWarning
+ re.purge()
+ pattern3 = re.compile('abc')
+ with warnings.catch_warnings():
+ warnings.simplefilter('error', BytesWarning)
+ self.assertNotEqual(pattern3, pattern1)
+
+ def test_bug_29444(self):
+ s = bytearray(b'abcdefgh')
+ m = re.search(b'[a-h]+', s)
+ m2 = re.search(b'[e-h]+', s)
+ self.assertEqual(m.group(), b'abcdefgh')
+ self.assertEqual(m2.group(), b'efgh')
+ s[:] = b'xyz'
+ self.assertEqual(m.group(), b'xyz')
+ self.assertEqual(m2.group(), b'')
+
class PatternReprTests(unittest.TestCase):
def check(self, pattern, expected):