Commit f8eac007 authored by Victor Stinner's avatar Victor Stinner

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).
parent e010fc02
...@@ -362,6 +362,11 @@ class BaseBytesTest(unittest.TestCase): ...@@ -362,6 +362,11 @@ class BaseBytesTest(unittest.TestCase):
self.assertEqual(b.find(i, 1, 3), 1) self.assertEqual(b.find(i, 1, 3), 1)
self.assertEqual(b.find(w, 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): def test_rfind(self):
b = self.type2test(b'mississippi') b = self.type2test(b'mississippi')
i = 105 i = 105
......
...@@ -186,27 +186,34 @@ STRINGLIB(parse_args_finds_byte)(const char *function_name, PyObject *args, ...@@ -186,27 +186,34 @@ STRINGLIB(parse_args_finds_byte)(const char *function_name, PyObject *args,
{ {
PyObject *tmp_subobj; PyObject *tmp_subobj;
Py_ssize_t ival; Py_ssize_t ival;
PyObject *err;
if(!STRINGLIB(parse_args_finds)(function_name, args, &tmp_subobj, if(!STRINGLIB(parse_args_finds)(function_name, args, &tmp_subobj,
start, end)) start, end))
return 0; return 0;
ival = PyNumber_AsSsize_t(tmp_subobj, PyExc_ValueError); if (!PyNumber_Check(tmp_subobj)) {
if (ival == -1 && PyErr_Occurred()) {
PyErr_Clear();
*subobj = tmp_subobj; *subobj = tmp_subobj;
return 1;
} }
else {
/* The first argument was an integer */ ival = PyNumber_AsSsize_t(tmp_subobj, PyExc_OverflowError);
if(ival < 0 || ival > 255) { if (ival == -1) {
PyErr_SetString(PyExc_ValueError, "byte must be in range(0, 256)"); err = PyErr_Occurred();
return 0; if (err && !PyErr_GivenExceptionMatches(err, PyExc_OverflowError)) {
PyErr_Clear();
*subobj = tmp_subobj;
return 1;
} }
}
*subobj = NULL; if (ival < 0 || ival > 255) {
*byte = (char)ival; PyErr_SetString(PyExc_ValueError, "byte must be in range(0, 256)");
return 0;
} }
*subobj = NULL;
*byte = (char)ival;
return 1; return 1;
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment