Debian Codegraph @

pypy/module/_sre/test/support_test_app_sre.py

raw symbols SCIP
1 """Support functions for app-level _sre tests."""
2 import locale, _sre
3 from sre_constants import OPCODES as _OPCODES
4 from sre_constants import ATCODES as _ATCODES
5 from sre_constants import CHCODES as _CHCODES
6 from sre_constants import MAXREPEAT, SRE_FLAG_UNICODE, SRE_FLAG_LOCALE
7
8 OPCODES = {_opcode.name.lower(): int(_opcode) for _opcode in _OPCODES}
9 ATCODES = {_atcode.name.lower(): int(_atcode) for _atcode in _ATCODES}
10 CHCODES = {_chcode.name.lower(): int(_chcode) for _chcode in _CHCODES}
11
12 def encode_literal(string):
13 opcodes = []
14 for character in string:
15 opcodes.extend([OPCODES["literal"], ord(character)])
16 return opcodes
17
18 def assert_match(opcodes, strings):
19 assert_something_about_match(lambda x: x, opcodes, strings)
20
21 def assert_no_match(opcodes, strings):
22 assert_something_about_match(lambda x: not x, opcodes, strings)
23
24 def assert_something_about_match(assert_modifier, opcodes, strings):
25 if isinstance(strings, str):
26 strings = [strings]
27 for string in strings:
28 assert assert_modifier(search(opcodes, string))
29
30 def search(opcodes, string):
31 pattern = _sre.compile("ignore", 0, opcodes, 0, {}, None)
32 return pattern.search(string)
33
34 def void_locale():
35 locale.setlocale(locale.LC_ALL, (None, None))
36
37 def assert_lower_equal(tests, flags):
38 if flags == 0:
39 checkerfn = _sre.ascii_tolower
40 elif flags == SRE_FLAG_UNICODE:
41 checkerfn = _sre.unicode_tolower
42 else:
43 assert False # SRE_FLAG_LOCALE: not supported, and not needed, since 3.7
44 for arg, expected in tests:
45 assert ord(expected) == checkerfn(ord(arg))