Skip to content

Commit 8253cea

Browse files
committed
Sync back flags from the compiled regex
1 parent 9f3b268 commit 8253cea

File tree

2 files changed

+28
-15
lines changed
  • graalpython

2 files changed

+28
-15
lines changed

graalpython/com.oracle.graal.python.test/src/tests/unittest_tags/test_re.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
*graalpython.lib-python.3.test.test_re.ImplementationTest.test_overlap_table
33
*graalpython.lib-python.3.test.test_re.PatternReprTests.test_bytes
44
*graalpython.lib-python.3.test.test_re.PatternReprTests.test_flags_repr
5+
*graalpython.lib-python.3.test.test_re.PatternReprTests.test_inline_flags
56
*graalpython.lib-python.3.test.test_re.PatternReprTests.test_locale
67
*graalpython.lib-python.3.test.test_re.PatternReprTests.test_multiple_flags
78
*graalpython.lib-python.3.test.test_re.PatternReprTests.test_quotes

graalpython/lib-graalpython/_sre.py

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -146,15 +146,15 @@ def setup(sre_compiler, error_class, flags_table):
146146
FLAG_DEBUG = 128
147147
FLAG_ASCII = 256
148148
FLAG_NAMES = [
149-
(FLAG_TEMPLATE, "re.TEMPLATE"),
150-
(FLAG_IGNORECASE, "re.IGNORECASE"),
151-
(FLAG_LOCALE, "re.LOCALE"),
152-
(FLAG_MULTILINE, "re.MULTILINE"),
153-
(FLAG_DOTALL, "re.DOTALL"),
154-
(FLAG_UNICODE, "re.UNICODE"),
155-
(FLAG_VERBOSE, "re.VERBOSE"),
156-
(FLAG_DEBUG, "re.DEBUG"),
157-
(FLAG_ASCII, "re.ASCII"),
149+
(FLAG_TEMPLATE, "TEMPLATE"),
150+
(FLAG_IGNORECASE, "IGNORECASE"),
151+
(FLAG_LOCALE, "LOCALE"),
152+
(FLAG_MULTILINE, "MULTILINE"),
153+
(FLAG_DOTALL, "DOTALL"),
154+
(FLAG_UNICODE, "UNICODE"),
155+
(FLAG_VERBOSE, "VERBOSE"),
156+
(FLAG_DEBUG, "DEBUG"),
157+
(FLAG_ASCII, "ASCII"),
158158
]
159159

160160

@@ -272,12 +272,12 @@ class Pattern():
272272
def __init__(self, pattern, flags):
273273
self.__binary = isinstance(pattern, bytes)
274274
self.pattern = pattern
275-
self.flags = flags
275+
self.__input_flags = flags
276276
flags_str = []
277-
for char,flag in FLAGS.items():
277+
for char, flag in FLAGS.items():
278278
if flags & flag:
279279
flags_str.append(char)
280-
self.flags_str = "".join(flags_str)
280+
self.__flags_str = "".join(flags_str)
281281
self.__compiled_regexes = {}
282282
compiled_regex = self.__tregex_compile(self.pattern)
283283
self.groups = compiled_regex.groupCount - 1
@@ -288,6 +288,18 @@ def __init__(self, pattern, flags):
288288
group_names = dir(groups)
289289
self.groupindex = _mappingproxy({name: groups[name] for name in group_names})
290290

291+
@property
292+
def flags(self):
293+
# Flags can be spcified both in the flag argument or inline in the regex. Extract them back from the regex
294+
flags = self.__input_flags
295+
for flag, name in FLAG_NAMES:
296+
try:
297+
if self.__tregex_compile(self.pattern).flags[name]:
298+
flags |= flag
299+
except KeyError:
300+
pass
301+
return flags
302+
291303
def __check_input_type(self, input):
292304
if not isinstance(input, str) and not _is_bytes_like(input):
293305
raise TypeError("expected string or bytes-like object")
@@ -303,7 +315,7 @@ def __check_pos(pos):
303315

304316
def __tregex_compile(self, pattern, flags=None):
305317
if flags is None:
306-
flags = self.flags_str
318+
flags = self.__flags_str
307319
if (pattern, flags) not in self.__compiled_regexes:
308320
try:
309321
self.__compiled_regexes[(pattern, flags)] = tregex_compile_internal(pattern, flags, fallback_compiler)
@@ -322,7 +334,7 @@ def __repr__(self):
322334
for code, name in FLAG_NAMES:
323335
if flags & code:
324336
flags -= code
325-
flag_items.append(name)
337+
flag_items.append(f're.{name}')
326338
if flags != 0:
327339
flag_items.append("0x%x" % flags)
328340
if len(flag_items) == 0:
@@ -344,7 +356,7 @@ def __hash__(self):
344356
return hash(self.pattern) * 31 ^ hash(self.flags)
345357

346358
def _search(self, pattern, string, pos, endpos, sticky=False):
347-
pattern = self.__tregex_compile(pattern, self.flags_str + ("y" if sticky else ""))
359+
pattern = self.__tregex_compile(pattern, self.__flags_str + ("y" if sticky else ""))
348360
input_str = string
349361
if endpos == -1 or endpos >= len(string):
350362
endpos = len(string)

0 commit comments

Comments
 (0)