]> granicus.if.org Git - python/commitdiff
Issue #28727: Optimize pattern_richcompare() for a==a
authorVictor Stinner <victor.stinner@gmail.com>
Tue, 22 Nov 2016 14:30:38 +0000 (15:30 +0100)
committerVictor Stinner <victor.stinner@gmail.com>
Tue, 22 Nov 2016 14:30:38 +0000 (15:30 +0100)
A pattern is equal to itself.

Lib/test/test_re.py
Modules/_sre.c

index 4fcd2d463dcc522a79b109683609e42c4b1b1a8f..84131d2b9260dfb829c5adf3c99be28af2aae368 100644 (file)
@@ -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)
index 1b7741696d0dad8d9611c5088e0374e1b89d09bf..979e61fb535b798273cccd591d0e2034b2c6b6a5 100644 (file)
@@ -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;