Skip to content

Commit e92dce0

Browse files
elprans1st1
andauthored
buffer: Handle bytearray as input (#14)
In Python 3.10 the Windows proactor event loop can send a bytearray object instead of bytes to protocol's `data_received` Co-authored-by: Yury Selivanov <[email protected]>
1 parent 719c7c7 commit e92dce0

File tree

2 files changed

+10
-2
lines changed

2 files changed

+10
-2
lines changed

buffer.pyx

+9-2
Original file line numberDiff line numberDiff line change
@@ -261,8 +261,15 @@ cdef class ReadBuffer:
261261
ssize_t dlen
262262
bytes data_bytes
263263

264-
if not cpython.PyBytes_CheckExact(data):
265-
raise BufferError('feed_data: bytes object expected')
264+
if not cpythonx.PyBytes_CheckExact(data):
265+
if cpython.PyByteArray_CheckExact(data):
266+
# ProactorEventLoop in Python 3.10+ seems to be sending
267+
# bytearray objects instead of bytes. Handle this here
268+
# to avoid duplicating this check in every data_received().
269+
data = bytes(data)
270+
else:
271+
raise BufferError(
272+
'feed_data: a bytes or bytearray object expected')
266273

267274
# Uncomment the below code to test code paths that
268275
# read single int/str/bytes sequences are split over

cpythonx.pxd

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ from cpython cimport Py_buffer
1010
cdef extern from "Python.h":
1111
int PyUnicode_1BYTE_KIND
1212

13+
int PyByteArray_CheckExact(object)
1314
int PyByteArray_Resize(object, ssize_t) except -1
1415
object PyByteArray_FromStringAndSize(const char *, ssize_t)
1516
char* PyByteArray_AsString(object)

0 commit comments

Comments
 (0)