diff options
author | Antoine Pitrou <solipsis@pitrou.net> | 2011-10-20 21:54:17 (GMT) |
---|---|---|
committer | Antoine Pitrou <solipsis@pitrou.net> | 2011-10-20 21:54:17 (GMT) |
commit | ac65d96777f1619c2910de82093e4f6f24dedd2f (patch) | |
tree | 7445f286a7b2221ec6d6ee54b3b9b828364487df /Objects/stringlib | |
parent | 407cfd1a269845208786bb9e7074e9990fa96dd7 (diff) | |
download | cpython-ac65d96777f1619c2910de82093e4f6f24dedd2f.zip cpython-ac65d96777f1619c2910de82093e4f6f24dedd2f.tar.gz cpython-ac65d96777f1619c2910de82093e4f6f24dedd2f.tar.bz2 |
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.
Diffstat (limited to 'Objects/stringlib')
-rw-r--r-- | Objects/stringlib/find.h | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/Objects/stringlib/find.h b/Objects/stringlib/find.h index 7cce156..00eaf1b 100644 --- a/Objects/stringlib/find.h +++ b/Objects/stringlib/find.h @@ -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 */ |