Skip to content

Commit 45150b3

Browse files
committed
python: fix re2.search(compiled, text)
The old code was accidentally recompiling the pattern and losing the options, both inefficient and incorrect. Thanks to @MajorTanya for the excellent diagnosis. This bug was introduced in CL 60290 and then copied over from the Abseil branch. Fixes #558. Change-Id: I072707003b444fb0934a310e14147b4d65b9da11 Reviewed-on: https://code-review.googlesource.com/c/re2/+/63830 Reviewed-by: Alan Donovan <adonovan@google.com>
1 parent 362c559 commit 45150b3

2 files changed

Lines changed: 7 additions & 1 deletion

File tree

python/re2.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ def compile(pattern, options=None):
6464
if options:
6565
raise error('pattern is already compiled, so '
6666
'options may not be specified')
67-
pattern = pattern._pattern
67+
return pattern
6868
options = options or Options()
6969
values = tuple(getattr(options, name) for name in Options.NAMES)
7070
return _Regexp._make(pattern, values)

python/re2_test.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,12 @@ def test_purge(self):
315315
re2.purge()
316316
self.assertEqual(re2._Regexp._make.cache_info().currsize, 0)
317317

318+
def test_options(self):
319+
opt = re2.Options()
320+
opt.case_sensitive = False
321+
r = re2.compile('test', opt)
322+
self.assertIsNotNone(r.search('TEST'))
323+
self.assertIsNotNone(re2.search(r, 'TEST'))
318324

319325
class Re2EscapeTest(parameterized.TestCase):
320326
"""Contains tests that apply to the re2 module only.

0 commit comments

Comments
 (0)