@@ -399,12 +399,15 @@ _Py_bytes_maketrans(Py_buffer *frm, Py_buffer *to)
399399#include "stringlib/find.h"
400400
401401/*
402- Wraps stringlib_parse_args_finds() and additionally checks whether the
403- first argument is an integer in range(0, 256) .
402+ Wraps stringlib_parse_args_finds() and additionally checks the first
403+ argument type .
404404
405- If this is the case, writes the integer value to the byte parameter
406- and sets subobj to NULL. Otherwise, sets the first argument to subobj
407- and doesn't touch byte. The other parameters are similar to those of
405+ In case the first argument is a bytes-like object, sets it to subobj,
406+ and doesn't touch the byte parameter.
407+ In case it is an integer in range(0, 256), writes the integer value
408+ to byte, and sets subobj to NULL.
409+
410+ The other parameters are similar to those of
408411stringlib_parse_args_finds().
409412*/
410413
@@ -415,27 +418,28 @@ parse_args_finds_byte(const char *function_name, PyObject *args,
415418{
416419 PyObject * tmp_subobj ;
417420 Py_ssize_t ival ;
418- PyObject * err ;
419421
420422 if (!stringlib_parse_args_finds (function_name , args , & tmp_subobj ,
421423 start , end ))
422424 return 0 ;
423425
424- if (! PyNumber_Check (tmp_subobj )) {
426+ if (PyObject_CheckBuffer (tmp_subobj )) {
425427 * subobj = tmp_subobj ;
426428 return 1 ;
427429 }
428430
429- ival = PyNumber_AsSsize_t (tmp_subobj , PyExc_OverflowError );
430- if (ival == -1 ) {
431- err = PyErr_Occurred ();
432- if (err && !PyErr_GivenExceptionMatches (err , PyExc_OverflowError )) {
433- PyErr_Clear ();
434- * subobj = tmp_subobj ;
435- return 1 ;
436- }
431+ if (!PyIndex_Check (tmp_subobj )) {
432+ PyErr_Format (PyExc_TypeError ,
433+ "argument should be integer or bytes-like object, "
434+ "not '%.200s'" ,
435+ Py_TYPE (tmp_subobj )-> tp_name );
436+ return 0 ;
437437 }
438438
439+ ival = PyNumber_AsSsize_t (tmp_subobj , NULL );
440+ if (ival == -1 && PyErr_Occurred ()) {
441+ return 0 ;
442+ }
439443 if (ival < 0 || ival > 255 ) {
440444 PyErr_SetString (PyExc_ValueError , "byte must be in range(0, 256)" );
441445 return 0 ;
0 commit comments