Issue #12170: The count(), find(), rfind(), index() and rindex() methods

of bytes and bytearray objects now accept an integer between 0 and 255
as their first argument.  Patch by Petri Lehtinen.
This commit is contained in:
Antoine Pitrou 2011-10-20 23:54:17 +02:00
parent 407cfd1a26
commit ac65d96777
7 changed files with 262 additions and 52 deletions

View file

@ -167,4 +167,47 @@ STRINGLIB(parse_args_finds_unicode)(const char * function_name, PyObject *args,
return 0;
}
#else /* !STRINGLIB_IS_UNICODE */
/*
Wraps stringlib_parse_args_finds() and additionally checks whether the
first argument is an integer in range(0, 256).
If this is the case, writes the integer value to the byte parameter
and sets subobj to NULL. Otherwise, sets the first argument to subobj
and doesn't touch byte. The other parameters are similar to those of
stringlib_parse_args_finds().
*/
Py_LOCAL_INLINE(int)
STRINGLIB(parse_args_finds_byte)(const char *function_name, PyObject *args,
PyObject **subobj, char *byte,
Py_ssize_t *start, Py_ssize_t *end)
{
PyObject *tmp_subobj;
Py_ssize_t ival;
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();
*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;
}
#endif /* STRINGLIB_IS_UNICODE */