return NULL;
}
- bytes = PyObject_Bytes(bytes_obj);
- if (bytes == NULL)
- return NULL;
-
- long_obj = _PyLong_FromByteArray(
- (unsigned char *)PyBytes_AS_STRING(bytes), Py_SIZE(bytes),
- little_endian, is_signed);
- Py_DECREF(bytes);
+ /* Fast-path exact bytes. */
+ if (PyBytes_CheckExact(bytes_obj)) {
+ long_obj = _PyLong_FromByteArray(
+ (unsigned char *)PyBytes_AS_STRING(bytes_obj), Py_SIZE(bytes_obj),
+ little_endian, is_signed);
+ }
+ /* Use buffer protocol to avoid copies. */
+ else if (PyObject_CheckBuffer(bytes_obj)) {
+ Py_buffer view;
+ if (PyObject_GetBuffer(bytes_obj, &view, PyBUF_SIMPLE) != 0) {
+ return NULL;
+ }
+ long_obj = _PyLong_FromByteArray(view.buf, view.len, little_endian,
+ is_signed);
+ PyBuffer_Release(&view);
+ }
+ else {
+ /* fallback: Construct a bytes then convert. */
+ bytes = PyObject_Bytes(bytes_obj);
+ if (bytes == NULL) {
+ return NULL;
+ }
+ long_obj = _PyLong_FromByteArray(
+ (unsigned char *)PyBytes_AS_STRING(bytes), Py_SIZE(bytes),
+ little_endian, is_signed);
+ Py_DECREF(bytes);
+ }
if (long_obj != NULL && type != &PyLong_Type) {
Py_SETREF(long_obj, PyObject_CallOneArg((PyObject *)type, long_obj));