Skip to content

Commit 8cb6f97

Browse files
Move implementation specific RE tests to separate class (GH-106563)
1 parent dcc028d commit 8cb6f97

File tree

1 file changed

+69
-66
lines changed

1 file changed

+69
-66
lines changed

Lib/test/test_re.py

Lines changed: 69 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1046,33 +1046,6 @@ def test_ignore_case_range(self):
10461046
def test_category(self):
10471047
self.assertEqual(re.match(r"(\s)", " ").group(1), " ")
10481048

1049-
@cpython_only
1050-
def test_case_helpers(self):
1051-
import _sre
1052-
for i in range(128):
1053-
c = chr(i)
1054-
lo = ord(c.lower())
1055-
self.assertEqual(_sre.ascii_tolower(i), lo)
1056-
self.assertEqual(_sre.unicode_tolower(i), lo)
1057-
iscased = c in string.ascii_letters
1058-
self.assertEqual(_sre.ascii_iscased(i), iscased)
1059-
self.assertEqual(_sre.unicode_iscased(i), iscased)
1060-
1061-
for i in list(range(128, 0x1000)) + [0x10400, 0x10428]:
1062-
c = chr(i)
1063-
self.assertEqual(_sre.ascii_tolower(i), i)
1064-
if i != 0x0130:
1065-
self.assertEqual(_sre.unicode_tolower(i), ord(c.lower()))
1066-
iscased = c != c.lower() or c != c.upper()
1067-
self.assertFalse(_sre.ascii_iscased(i))
1068-
self.assertEqual(_sre.unicode_iscased(i),
1069-
c != c.lower() or c != c.upper())
1070-
1071-
self.assertEqual(_sre.ascii_tolower(0x0130), 0x0130)
1072-
self.assertEqual(_sre.unicode_tolower(0x0130), ord('i'))
1073-
self.assertFalse(_sre.ascii_iscased(0x0130))
1074-
self.assertTrue(_sre.unicode_iscased(0x0130))
1075-
10761049
def test_not_literal(self):
10771050
self.assertEqual(re.search(r"\s([^a])", " b").group(1), "b")
10781051
self.assertEqual(re.search(r"\s([^a]*)", " bb").group(1), "bb")
@@ -1770,20 +1743,6 @@ def test_bug_6509(self):
17701743
pat = re.compile(b'..')
17711744
self.assertEqual(pat.sub(lambda m: b'bytes', b'a5'), b'bytes')
17721745

1773-
def test_dealloc(self):
1774-
# issue 3299: check for segfault in debug build
1775-
import _sre
1776-
# the overflow limit is different on wide and narrow builds and it
1777-
# depends on the definition of SRE_CODE (see sre.h).
1778-
# 2**128 should be big enough to overflow on both. For smaller values
1779-
# a RuntimeError is raised instead of OverflowError.
1780-
long_overflow = 2**128
1781-
self.assertRaises(TypeError, re.finditer, "a", {})
1782-
with self.assertRaises(OverflowError):
1783-
_sre.compile("abc", 0, [long_overflow], 0, {}, ())
1784-
with self.assertRaises(TypeError):
1785-
_sre.compile({}, 0, [], 0, [], [])
1786-
17871746
def test_search_dot_unicode(self):
17881747
self.assertTrue(re.search("123.*-", '123abc-'))
17891748
self.assertTrue(re.search("123.*-", '123\xe9-'))
@@ -1841,21 +1800,6 @@ def test_repeat_minmax_overflow(self):
18411800
self.assertRaises(OverflowError, re.compile, r".{%d,}?" % 2**128)
18421801
self.assertRaises(OverflowError, re.compile, r".{%d,%d}" % (2**129, 2**128))
18431802

1844-
@cpython_only
1845-
def test_repeat_minmax_overflow_maxrepeat(self):
1846-
try:
1847-
from _sre import MAXREPEAT
1848-
except ImportError:
1849-
self.skipTest('requires _sre.MAXREPEAT constant')
1850-
string = "x" * 100000
1851-
self.assertIsNone(re.match(r".{%d}" % (MAXREPEAT - 1), string))
1852-
self.assertEqual(re.match(r".{,%d}" % (MAXREPEAT - 1), string).span(),
1853-
(0, 100000))
1854-
self.assertIsNone(re.match(r".{%d,}?" % (MAXREPEAT - 1), string))
1855-
self.assertRaises(OverflowError, re.compile, r".{%d}" % MAXREPEAT)
1856-
self.assertRaises(OverflowError, re.compile, r".{,%d}" % MAXREPEAT)
1857-
self.assertRaises(OverflowError, re.compile, r".{%d,}?" % MAXREPEAT)
1858-
18591803
def test_backref_group_name_in_exception(self):
18601804
# Issue 17341: Poor error message when compiling invalid regex
18611805
self.checkPatternError('(?P=<foo>)',
@@ -2418,16 +2362,6 @@ def test_regression_gh94675(self):
24182362
p.terminate()
24192363
p.join()
24202364

2421-
def test_sre_template_invalid_group_index(self):
2422-
# see gh-106524
2423-
import _sre
2424-
with self.assertRaises(TypeError) as cm:
2425-
_sre.template("", ["", -1, ""])
2426-
self.assertIn("invalid template", str(cm.exception))
2427-
with self.assertRaises(TypeError) as cm:
2428-
_sre.template("", ["", (), ""])
2429-
self.assertIn("an integer is required", str(cm.exception))
2430-
24312365

24322366
def get_debug_out(pat):
24332367
with captured_stdout() as out:
@@ -2676,6 +2610,75 @@ def test_deprecated_modules(self):
26762610
self.assertTrue(hasattr(mod, attr))
26772611
del sys.modules[name]
26782612

2613+
@cpython_only
2614+
def test_case_helpers(self):
2615+
import _sre
2616+
for i in range(128):
2617+
c = chr(i)
2618+
lo = ord(c.lower())
2619+
self.assertEqual(_sre.ascii_tolower(i), lo)
2620+
self.assertEqual(_sre.unicode_tolower(i), lo)
2621+
iscased = c in string.ascii_letters
2622+
self.assertEqual(_sre.ascii_iscased(i), iscased)
2623+
self.assertEqual(_sre.unicode_iscased(i), iscased)
2624+
2625+
for i in list(range(128, 0x1000)) + [0x10400, 0x10428]:
2626+
c = chr(i)
2627+
self.assertEqual(_sre.ascii_tolower(i), i)
2628+
if i != 0x0130:
2629+
self.assertEqual(_sre.unicode_tolower(i), ord(c.lower()))
2630+
iscased = c != c.lower() or c != c.upper()
2631+
self.assertFalse(_sre.ascii_iscased(i))
2632+
self.assertEqual(_sre.unicode_iscased(i),
2633+
c != c.lower() or c != c.upper())
2634+
2635+
self.assertEqual(_sre.ascii_tolower(0x0130), 0x0130)
2636+
self.assertEqual(_sre.unicode_tolower(0x0130), ord('i'))
2637+
self.assertFalse(_sre.ascii_iscased(0x0130))
2638+
self.assertTrue(_sre.unicode_iscased(0x0130))
2639+
2640+
@cpython_only
2641+
def test_dealloc(self):
2642+
# issue 3299: check for segfault in debug build
2643+
import _sre
2644+
# the overflow limit is different on wide and narrow builds and it
2645+
# depends on the definition of SRE_CODE (see sre.h).
2646+
# 2**128 should be big enough to overflow on both. For smaller values
2647+
# a RuntimeError is raised instead of OverflowError.
2648+
long_overflow = 2**128
2649+
self.assertRaises(TypeError, re.finditer, "a", {})
2650+
with self.assertRaises(OverflowError):
2651+
_sre.compile("abc", 0, [long_overflow], 0, {}, ())
2652+
with self.assertRaises(TypeError):
2653+
_sre.compile({}, 0, [], 0, [], [])
2654+
2655+
@cpython_only
2656+
def test_repeat_minmax_overflow_maxrepeat(self):
2657+
try:
2658+
from _sre import MAXREPEAT
2659+
except ImportError:
2660+
self.skipTest('requires _sre.MAXREPEAT constant')
2661+
string = "x" * 100000
2662+
self.assertIsNone(re.match(r".{%d}" % (MAXREPEAT - 1), string))
2663+
self.assertEqual(re.match(r".{,%d}" % (MAXREPEAT - 1), string).span(),
2664+
(0, 100000))
2665+
self.assertIsNone(re.match(r".{%d,}?" % (MAXREPEAT - 1), string))
2666+
self.assertRaises(OverflowError, re.compile, r".{%d}" % MAXREPEAT)
2667+
self.assertRaises(OverflowError, re.compile, r".{,%d}" % MAXREPEAT)
2668+
self.assertRaises(OverflowError, re.compile, r".{%d,}?" % MAXREPEAT)
2669+
2670+
@cpython_only
2671+
def test_sre_template_invalid_group_index(self):
2672+
# see gh-106524
2673+
import _sre
2674+
with self.assertRaises(TypeError) as cm:
2675+
_sre.template("", ["", -1, ""])
2676+
self.assertIn("invalid template", str(cm.exception))
2677+
with self.assertRaises(TypeError) as cm:
2678+
_sre.template("", ["", (), ""])
2679+
self.assertIn("an integer is required", str(cm.exception))
2680+
2681+
26792682
class ExternalTests(unittest.TestCase):
26802683

26812684
def test_re_benchmarks(self):

0 commit comments

Comments
 (0)