if (obj == Py_None) {
limit = -1;
}
- else if (PyNumber_Check(obj)) {
+ else if (PyIndex_Check(obj)) {
limit = PyNumber_AsSsize_t(obj, PyExc_OverflowError);
- if (limit == -1 && PyErr_Occurred())
+ if (limit == -1 && PyErr_Occurred()) {
return 0;
+ }
}
else {
PyErr_Format(PyExc_TypeError,
- "integer argument expected, got '%.200s'",
+ "argument should be integer or None, not '%.200s'",
Py_TYPE(obj)->tp_name);
return 0;
}
/* Truncate to current position if no argument is passed. */
size = self->pos;
}
- else {
+ else if (PyIndex_Check(arg)) {
size = PyNumber_AsSsize_t(arg, PyExc_OverflowError);
if (size == -1 && PyErr_Occurred()) {
return NULL;
}
}
+ else {
+ PyErr_Format(PyExc_TypeError,
+ "argument should be integer or None, not '%.200s'",
+ Py_TYPE(arg)->tp_name);
+ return NULL;
+ }
if (size < 0) {
PyErr_Format(PyExc_ValueError,
CHECK_INITIALIZED(self);
CHECK_CLOSED(self);
- if (PyNumber_Check(arg)) {
+ if (PyIndex_Check(arg)) {
size = PyNumber_AsSsize_t(arg, PyExc_OverflowError);
- if (size == -1 && PyErr_Occurred())
+ if (size == -1 && PyErr_Occurred()) {
return NULL;
+ }
}
else if (arg == Py_None) {
/* Truncate to current position if no argument is passed. */
size = self->pos;
}
else {
- PyErr_Format(PyExc_TypeError, "integer argument expected, got '%s'",
+ PyErr_Format(PyExc_TypeError,
+ "argument should be integer or None, not '%.200s'",
Py_TYPE(arg)->tp_name);
return NULL;
}
if (obj == Py_None) {
limit = -1;
}
- else if (PyNumber_Check(obj)) {
+ else if (PyIndex_Check(obj)) {
limit = PyNumber_AsSsize_t(obj, PyExc_OverflowError);
- if (limit == -1 && PyErr_Occurred())
+ if (limit == -1 && PyErr_Occurred()) {
return 0;
+ }
}
else {
PyErr_Format(PyExc_TypeError,
- "integer argument expected, got '%.200s'",
+ "argument should be integer or None, not '%.200s'",
Py_TYPE(obj)->tp_name);
return 0;
}
#include "stringlib/find.h"
/*
-Wraps stringlib_parse_args_finds() and additionally checks whether the
-first argument is an integer in range(0, 256).
+Wraps stringlib_parse_args_finds() and additionally checks the first
+argument type.
-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
+In case the first argument is a bytes-like object, sets it to subobj,
+and doesn't touch the byte parameter.
+In case it is an integer in range(0, 256), writes the integer value
+to byte, and sets subobj to NULL.
+
+The other parameters are similar to those of
stringlib_parse_args_finds().
*/
{
PyObject *tmp_subobj;
Py_ssize_t ival;
- PyObject *err;
if(!stringlib_parse_args_finds(function_name, args, &tmp_subobj,
start, end))
return 0;
- if (!PyNumber_Check(tmp_subobj)) {
+ if (PyObject_CheckBuffer(tmp_subobj)) {
*subobj = tmp_subobj;
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 (!PyIndex_Check(tmp_subobj)) {
+ PyErr_Format(PyExc_TypeError,
+ "argument should be integer or bytes-like object, "
+ "not '%.200s'",
+ Py_TYPE(tmp_subobj)->tp_name);
+ return 0;
}
+ ival = PyNumber_AsSsize_t(tmp_subobj, NULL);
+ if (ival == -1 && PyErr_Occurred()) {
+ return 0;
+ }
if (ival < 0 || ival > 255) {
PyErr_SetString(PyExc_ValueError, "byte must be in range(0, 256)");
return 0;