Skip to content

Commit e629654

Browse files
committed
Implement equality and hash for compiled patterns
1 parent 414c991 commit e629654

File tree

2 files changed

+12
-0
lines changed
  • graalpython

2 files changed

+12
-0
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@
5959
*graalpython.lib-python.3.test.test_re.ReTests.test_not_literal
6060
*graalpython.lib-python.3.test.test_re.ReTests.test_nothing_to_repeat
6161
*graalpython.lib-python.3.test.test_re.ReTests.test_other_escapes
62+
*graalpython.lib-python.3.test.test_re.ReTests.test_pattern_compare
63+
*graalpython.lib-python.3.test.test_re.ReTests.test_pattern_compare_bytes
6264
*graalpython.lib-python.3.test.test_re.ReTests.test_pickling
6365
*graalpython.lib-python.3.test.test_re.ReTests.test_qualified_re_split
6466
*graalpython.lib-python.3.test.test_re.ReTests.test_qualified_re_sub

graalpython/lib-graalpython/_sre.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,16 @@ def __repr__(self):
306306
sflags = "|".join(flag_items)
307307
return "re.compile(%r%s%s)" % (self.pattern, sep, sflags)
308308

309+
def __eq__(self, other):
310+
if self is other:
311+
return True
312+
if type(other) != SRE_Pattern:
313+
return NotImplemented
314+
return self.pattern == other.pattern and self.flags == other.flags
315+
316+
def __hash__(self):
317+
return hash(self.pattern) * 31 ^ hash(self.flags)
318+
309319
def _search(self, pattern, string, pos, endpos, sticky=False):
310320
pattern = self.__tregex_compile(pattern, self.flags_str + ("y" if sticky else ""))
311321
input_str = string

0 commit comments

Comments
 (0)