Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit bcf4dcc

Browse files
committed
Issue #28727: Optimize pattern_richcompare() for a==a
A pattern is equal to itself.
1 parent e670b2d commit bcf4dcc

2 files changed

Lines changed: 10 additions & 0 deletions

File tree

Lib/test/test_re.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1781,6 +1781,10 @@ def test_enum(self):
17811781
def test_pattern_compare(self):
17821782
pattern1 = re.compile('abc', re.IGNORECASE)
17831783

1784+
# equal to itself
1785+
self.assertEqual(pattern1, pattern1)
1786+
self.assertFalse(pattern1 != pattern1)
1787+
17841788
# equal
17851789
re.purge()
17861790
pattern2 = re.compile('abc', re.IGNORECASE)

Modules/_sre.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2683,6 +2683,12 @@ pattern_richcompare(PyObject *lefto, PyObject *righto, int op)
26832683
if (Py_TYPE(lefto) != &Pattern_Type || Py_TYPE(righto) != &Pattern_Type) {
26842684
Py_RETURN_NOTIMPLEMENTED;
26852685
}
2686+
2687+
if (lefto == righto) {
2688+
/* a pattern is equal to itself */
2689+
return PyBool_FromLong(op == Py_EQ);
2690+
}
2691+
26862692
left = (PatternObject *)lefto;
26872693
right = (PatternObject *)righto;
26882694

0 commit comments

Comments
 (0)