There was an error while loading. Please reload this page.
1 parent e010fc0 commit f8eac00Copy full SHA for f8eac00
2 files changed
Lib/test/test_bytes.py
@@ -362,6 +362,11 @@ def test_find(self):
362
self.assertEqual(b.find(i, 1, 3), 1)
363
self.assertEqual(b.find(w, 1, 3), -1)
364
365
+ for index in (-1, 256, sys.maxsize + 1):
366
+ self.assertRaisesRegex(
367
+ ValueError, r'byte must be in range\(0, 256\)',
368
+ b.find, index)
369
+
370
def test_rfind(self):
371
b = self.type2test(b'mississippi')
372
i = 105
Objects/stringlib/find.h
@@ -186,27 +186,34 @@ STRINGLIB(parse_args_finds_byte)(const char *function_name, PyObject *args,
186
{
187
PyObject *tmp_subobj;
188
Py_ssize_t ival;
189
+ PyObject *err;
190
191
if(!STRINGLIB(parse_args_finds)(function_name, args, &tmp_subobj,
192
start, end))
193
return 0;
194
- ival = PyNumber_AsSsize_t(tmp_subobj, PyExc_ValueError);
195
- if (ival == -1 && PyErr_Occurred()) {
196
- PyErr_Clear();
+ if (!PyNumber_Check(tmp_subobj)) {
197
*subobj = tmp_subobj;
+ return 1;
198
}
199
- else {
200
- /* The first argument was an integer */
201
- if(ival < 0 || ival > 255) {
202
- PyErr_SetString(PyExc_ValueError, "byte must be in range(0, 256)");
203
- return 0;
+ ival = PyNumber_AsSsize_t(tmp_subobj, PyExc_OverflowError);
+ if (ival == -1) {
+ err = PyErr_Occurred();
+ if (err && !PyErr_GivenExceptionMatches(err, PyExc_OverflowError)) {
204
+ PyErr_Clear();
205
+ *subobj = tmp_subobj;
206
207
208
+ }
209
- *subobj = NULL;
- *byte = (char)ival;
210
+ if (ival < 0 || ival > 255) {
211
+ PyErr_SetString(PyExc_ValueError, "byte must be in range(0, 256)");
212
+ return 0;
213
214
215
+ *subobj = NULL;
216
+ *byte = (char)ival;
217
return 1;
218
219
0 commit comments