diff Objects/bytesobject.c @ 86918:5fa291435740

Issue #16286: optimize PyUnicode_RichCompare() for identical strings (same pointer) for any operator, not only Py_EQ and Py_NE. Code of bytes_richcompare() and PyUnicode_RichCompare() is now closer.
author Victor Stinner <victor.stinner@gmail.com>
date Mon, 04 Nov 2013 11:23:05 +0100
parents 536a7c09c7fd
children 82b58807f481
line wrap: on
line diff
--- a/Objects/bytesobject.c	Mon Nov 04 11:08:10 2013 +0100
+++ b/Objects/bytesobject.c	Mon Nov 04 11:23:05 2013 +0100
@@ -842,12 +842,20 @@
     }
     else if (a == b) {
         switch (op) {
-        case Py_EQ:case Py_LE:case Py_GE:
+        case Py_EQ:
+        case Py_LE:
+        case Py_GE:
+            /* a string is equal to itself */
             result = Py_True;
             break;
-        case Py_NE:case Py_LT:case Py_GT:
+        case Py_NE:
+        case Py_LT:
+        case Py_GT:
             result = Py_False;
             break;
+        default:
+            PyErr_BadArgument();
+            return NULL;
         }
     }
     else if (op == Py_EQ || op == Py_NE) {
@@ -856,11 +864,12 @@
         result = eq ? Py_True : Py_False;
     }
     else {
-        len_a = Py_SIZE(a); len_b = Py_SIZE(b);
-        min_len = (len_a < len_b) ? len_a : len_b;
+        len_a = Py_SIZE(a);
+        len_b = Py_SIZE(b);
+        min_len = Py_MIN(len_a, len_b);
         if (min_len > 0) {
             c = Py_CHARMASK(*a->ob_sval) - Py_CHARMASK(*b->ob_sval);
-            if (c==0)
+            if (c == 0)
                 c = memcmp(a->ob_sval, b->ob_sval, min_len);
         }
         else
@@ -873,8 +882,8 @@
         case Py_GT: c = c >  0; break;
         case Py_GE: c = c >= 0; break;
         default:
-            assert(op != Py_EQ && op != Py_NE);
-            Py_RETURN_NOTIMPLEMENTED;
+            PyErr_BadArgument();
+            return NULL;
         }
         result = c ? Py_True : Py_False;
     }