Python `bytearray` not accepted for Cython `bytes` argument
See original GitHub issueThis seems like a mistake, as Python’s bytearray
is a strict superset of Python’s bytes
— so when accepting a bytes
argument, I can’t think of a good reason not to accept a bytearray
object, but right now this fails:
# iscsi.pyx
cdef class Task:
cdef scsi_task *_task
def __init__(self, bytes cdb, scsi_xfer_dir direction, size_t xferlen):
self._task = scsi_create_task(len(cdb), cdb, direction, xferlen)
@property
def status(self):
return scsi_task_get_status(self._task, NULL)
# iscsi_test.py
class TaskTest(unittest.TestCase):
def test_bytearray(self):
task = iscsi.Task(
bytearray(b"\x12\x00\x00\x00\x60\x00"), iscsi.SCSI_XFER_READ, 96
)
self.assertIsNotNone(task)
Output:
self = <tests.iscsi_test.TaskTest testMethod=test_bytearray>
def test_bytearray(self):
> task = iscsi.Task(
bytearray(b"\x12\x00\x00\x00\x60\x00"), iscsi.SCSI_XFER_READ, 96
)
E TypeError: Argument 'cdb' has incorrect type (expected bytes, got bytearray)
tests/iscsi_test.py:28: TypeError
Issue Analytics
- State:
- Created 3 years ago
- Comments:7 (4 by maintainers)
Top Results From Across the Web
Is an explicit NUL-byte necessary at the end of a bytearray for ...
The cython-documentation doesn't provide any information, whether the resulting C-strings are null-terminated. It is possible to add a NUL -byte ...
Read more >Unicode and passing strings — Cython 3.0.0a11 documentation
Starting with Cython 0.20, the bytearray type is supported and coerces in the same way as the bytes type. However, when using it...
Read more >errors raised by bytes() and bytearray() for invalid size argument
IMHO, to make the error messages more consistent, the constructor of bytes should raise a MemoryError for any too large size argument, as...
Read more >How is working Cython buffer ? - Google Groups
My image is loaded with pygame in Python, and i get their BufferProxy() object (i can create a buffer() or bytearray() with it,...
Read more >Supported Python features - Numba
Functions as arguments¶. Functions can be passed as argument into another function. But, they cannot be returned. For example: from ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
It looks like there’s already something to this effect in the documentation:
https://cython.readthedocs.io/en/latest/src/tutorial/strings.html#accepting-strings-from-python-code
The challenge is always to find the place in the documentation where the information will be seen might the right people who need to see it. I’m sure improvements to the documentation are welcome (although I won’t be the one deciding if they’re accepted)
I’ll close this ticket since it works as designed. Feel free to send a PR for the docs.