From e670b2d5c39ce6ac72890e52e9d505f90c33ccba Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Tue, 22 Nov 2016 15:23:00 +0100 Subject: [PATCH 1/2] Issue #28727: Fix typo in pattern_richcompare() Typo catched by Serhiy Storchaka, thanks! --- Modules/_sre.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Modules/_sre.c b/Modules/_sre.c index c1e9fa6e6bd..1b7741696d0 100644 --- a/Modules/_sre.c +++ b/Modules/_sre.c @@ -2688,7 +2688,7 @@ pattern_richcompare(PyObject *lefto, PyObject *righto, int op) cmp = (left->flags == right->flags && left->isbytes == right->isbytes - && left->codesize && right->codesize); + && left->codesize == right->codesize); if (cmp) { /* Compare the code and the pattern because the same pattern can produce different codes depending on the locale used to compile the From bcf4dccfa77df6fd75181e2a7cbfbf2d4b2c6255 Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Tue, 22 Nov 2016 15:30:38 +0100 Subject: [PATCH 2/2] Issue #28727: Optimize pattern_richcompare() for a==a A pattern is equal to itself. --- Lib/test/test_re.py | 4 ++++ Modules/_sre.c | 6 ++++++ 2 files changed, 10 insertions(+) diff --git a/Lib/test/test_re.py b/Lib/test/test_re.py index 4fcd2d463dc..84131d2b926 100644 --- a/Lib/test/test_re.py +++ b/Lib/test/test_re.py @@ -1781,6 +1781,10 @@ SUBPATTERN None 0 0 def test_pattern_compare(self): pattern1 = re.compile('abc', re.IGNORECASE) + # equal to itself + self.assertEqual(pattern1, pattern1) + self.assertFalse(pattern1 != pattern1) + # equal re.purge() pattern2 = re.compile('abc', re.IGNORECASE) diff --git a/Modules/_sre.c b/Modules/_sre.c index 1b7741696d0..979e61fb535 100644 --- a/Modules/_sre.c +++ b/Modules/_sre.c @@ -2683,6 +2683,12 @@ pattern_richcompare(PyObject *lefto, PyObject *righto, int op) if (Py_TYPE(lefto) != &Pattern_Type || Py_TYPE(righto) != &Pattern_Type) { Py_RETURN_NOTIMPLEMENTED; } + + if (lefto == righto) { + /* a pattern is equal to itself */ + return PyBool_FromLong(op == Py_EQ); + } + left = (PatternObject *)lefto; right = (PatternObject *)righto;