[Python-checkins] cpython: Issue #13623: Fix a performance regression introduced by issue #12170 in

victor.stinner python-checkins at python.org
Sun Dec 18 01:17:54 CET 2011


http://hg.python.org/cpython/rev/75648db1b3f3
changeset:   74022:75648db1b3f3
user:        Victor Stinner <victor.stinner at haypocalc.com>
date:        Sun Dec 18 01:17:41 2011 +0100
summary:
  Issue #13623: Fix a performance regression introduced by issue #12170 in
bytes.find() and handle correctly OverflowError (raise the same ValueError than
the error for -1).

files:
  Lib/test/test_bytes.py   |   5 ++++
  Objects/stringlib/find.h |  33 ++++++++++++++++-----------
  2 files changed, 25 insertions(+), 13 deletions(-)


diff --git a/Lib/test/test_bytes.py b/Lib/test/test_bytes.py
--- a/Lib/test/test_bytes.py
+++ b/Lib/test/test_bytes.py
@@ -362,6 +362,11 @@
         self.assertEqual(b.find(i, 1, 3), 1)
         self.assertEqual(b.find(w, 1, 3), -1)
 
+        for index in (-1, 256, sys.maxsize + 1):
+            self.assertRaisesRegex(
+                ValueError, r'byte must be in range\(0, 256\)',
+                b.find, index)
+
     def test_rfind(self):
         b = self.type2test(b'mississippi')
         i = 105
diff --git a/Objects/stringlib/find.h b/Objects/stringlib/find.h
--- a/Objects/stringlib/find.h
+++ b/Objects/stringlib/find.h
@@ -186,27 +186,34 @@
 {
     PyObject *tmp_subobj;
     Py_ssize_t ival;
+    PyObject *err;
 
     if(!STRINGLIB(parse_args_finds)(function_name, args, &tmp_subobj,
                                     start, end))
         return 0;
 
-    ival = PyNumber_AsSsize_t(tmp_subobj, PyExc_ValueError);
-    if (ival == -1 && PyErr_Occurred()) {
-        PyErr_Clear();
+    if (!PyNumber_Check(tmp_subobj)) {
         *subobj = tmp_subobj;
-    }
-    else {
-        /* The first argument was an integer */
-        if(ival < 0 || ival > 255) {
-            PyErr_SetString(PyExc_ValueError, "byte must be in range(0, 256)");
-            return 0;
-        }
-
-        *subobj = NULL;
-        *byte = (char)ival;
+        return 1;
     }
 
+    ival = PyNumber_AsSsize_t(tmp_subobj, PyExc_OverflowError);
+    if (ival == -1) {
+        err = PyErr_Occurred();
+        if (err && !PyErr_GivenExceptionMatches(err, PyExc_OverflowError)) {
+            PyErr_Clear();
+            *subobj = tmp_subobj;
+            return 1;
+        }
+    }
+
+    if (ival < 0 || ival > 255) {
+        PyErr_SetString(PyExc_ValueError, "byte must be in range(0, 256)");
+        return 0;
+    }
+
+    *subobj = NULL;
+    *byte = (char)ival;
     return 1;
 }
 

-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list