mirror of
https://github.com/python/cpython.git
synced 2025-08-04 17:08:35 +00:00
bytes.find() and handle correctly OverflowError (raise the same ValueError than the error for -1).
This commit is contained in:
parent
e010fc029d
commit
f8eac00779
2 changed files with 23 additions and 11 deletions
|
@ -186,27 +186,34 @@ STRINGLIB(parse_args_finds_byte)(const char *function_name, PyObject *args,
|
|||
{
|
||||
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;
|
||||
return 1;
|
||||
}
|
||||
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;
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
*subobj = NULL;
|
||||
*byte = (char)ival;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue