Skip to content

Commit 559267f

Browse files
[3.12] Move implementation specific RE tests to separate class (GH-106563) (#106564)
Move implementation specific RE tests to separate class (GH-106563) (cherry picked from commit 8cb6f97) Co-authored-by: Serhiy Storchaka <[email protected]>
1 parent 60ade0c commit 559267f

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")
@@ -1769,20 +1742,6 @@ def test_bug_6509(self):
17691742
pat = re.compile(b'..')
17701743
self.assertEqual(pat.sub(lambda m: b'bytes', b'a5'), b'bytes')
17711744

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

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

2444-
def test_sre_template_invalid_group_index(self):
2445-
# see gh-106524
2446-
import _sre
2447-
with self.assertRaises(TypeError) as cm:
2448-
_sre.template("", ["", -1, ""])
2449-
self.assertIn("invalid template", str(cm.exception))
2450-
with self.assertRaises(TypeError) as cm:
2451-
_sre.template("", ["", (), ""])
2452-
self.assertIn("an integer is required", str(cm.exception))
2453-
24542388

24552389
def get_debug_out(pat):
24562390
with captured_stdout() as out:
@@ -2699,6 +2633,75 @@ def test_deprecated_modules(self):
26992633
self.assertTrue(hasattr(mod, attr))
27002634
del sys.modules[name]
27012635

2636+
@cpython_only
2637+
def test_case_helpers(self):
2638+
import _sre
2639+
for i in range(128):
2640+
c = chr(i)
2641+
lo = ord(c.lower())
2642+
self.assertEqual(_sre.ascii_tolower(i), lo)
2643+
self.assertEqual(_sre.unicode_tolower(i), lo)
2644+
iscased = c in string.ascii_letters
2645+
self.assertEqual(_sre.ascii_iscased(i), iscased)
2646+
self.assertEqual(_sre.unicode_iscased(i), iscased)
2647+
2648+
for i in list(range(128, 0x1000)) + [0x10400, 0x10428]:
2649+
c = chr(i)
2650+
self.assertEqual(_sre.ascii_tolower(i), i)
2651+
if i != 0x0130:
2652+
self.assertEqual(_sre.unicode_tolower(i), ord(c.lower()))
2653+
iscased = c != c.lower() or c != c.upper()
2654+
self.assertFalse(_sre.ascii_iscased(i))
2655+
self.assertEqual(_sre.unicode_iscased(i),
2656+
c != c.lower() or c != c.upper())
2657+
2658+
self.assertEqual(_sre.ascii_tolower(0x0130), 0x0130)
2659+
self.assertEqual(_sre.unicode_tolower(0x0130), ord('i'))
2660+
self.assertFalse(_sre.ascii_iscased(0x0130))
2661+
self.assertTrue(_sre.unicode_iscased(0x0130))
2662+
2663+
@cpython_only
2664+
def test_dealloc(self):
2665+
# issue 3299: check for segfault in debug build
2666+
import _sre
2667+
# the overflow limit is different on wide and narrow builds and it
2668+
# depends on the definition of SRE_CODE (see sre.h).
2669+
# 2**128 should be big enough to overflow on both. For smaller values
2670+
# a RuntimeError is raised instead of OverflowError.
2671+
long_overflow = 2**128
2672+
self.assertRaises(TypeError, re.finditer, "a", {})
2673+
with self.assertRaises(OverflowError):
2674+
_sre.compile("abc", 0, [long_overflow], 0, {}, ())
2675+
with self.assertRaises(TypeError):
2676+
_sre.compile({}, 0, [], 0, [], [])
2677+
2678+
@cpython_only
2679+
def test_repeat_minmax_overflow_maxrepeat(self):
2680+
try:
2681+
from _sre import MAXREPEAT
2682+
except ImportError:
2683+
self.skipTest('requires _sre.MAXREPEAT constant')
2684+
string = "x" * 100000
2685+
self.assertIsNone(re.match(r".{%d}" % (MAXREPEAT - 1), string))
2686+
self.assertEqual(re.match(r".{,%d}" % (MAXREPEAT - 1), string).span(),
2687+
(0, 100000))
2688+
self.assertIsNone(re.match(r".{%d,}?" % (MAXREPEAT - 1), string))
2689+
self.assertRaises(OverflowError, re.compile, r".{%d}" % MAXREPEAT)
2690+
self.assertRaises(OverflowError, re.compile, r".{,%d}" % MAXREPEAT)
2691+
self.assertRaises(OverflowError, re.compile, r".{%d,}?" % MAXREPEAT)
2692+
2693+
@cpython_only
2694+
def test_sre_template_invalid_group_index(self):
2695+
# see gh-106524
2696+
import _sre
2697+
with self.assertRaises(TypeError) as cm:
2698+
_sre.template("", ["", -1, ""])
2699+
self.assertIn("invalid template", str(cm.exception))
2700+
with self.assertRaises(TypeError) as cm:
2701+
_sre.template("", ["", (), ""])
2702+
self.assertIn("an integer is required", str(cm.exception))
2703+
2704+
27022705
class ExternalTests(unittest.TestCase):
27032706

27042707
def test_re_benchmarks(self):

0 commit comments

Comments
 (0)