Index: Objects/bytearrayobject.c =================================================================== --- Objects/bytearrayobject.c (revision 64448) +++ Objects/bytearrayobject.c (working copy) @@ -57,6 +57,27 @@ return 1; } +static int +_getbyteasint(PyObject* arg, int *value) +{ + long face_value; + + if (PyInt_Check(arg)) { + face_value = PyInt_AsLong(arg); + if (face_value < 0 || face_value >= 256) { + PyErr_SetString(PyExc_ValueError, "byte must be in range(0, 256)"); + return 0; + } + } + else { + PyErr_Format(PyExc_TypeError, "an integer is required"); + return 0; + } + + *value = face_value; + return 1; +} + static Py_ssize_t bytes_buffer_getreadbuf(PyByteArrayObject *self, Py_ssize_t index, const void **ptr) { @@ -2636,7 +2657,7 @@ int value; Py_ssize_t n = Py_SIZE(self); - if (! _getbytevalue(arg, &value)) + if (! _getbyteasint(arg, &value)) return NULL; if (n == PY_SSIZE_T_MAX) { PyErr_SetString(PyExc_OverflowError,