question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Python `bytearray` not accepted for Cython `bytes` argument

See original GitHub issue

This 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:closed
  • Created 3 years ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
da-woodscommented, Apr 4, 2020

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

In the case that the API only deals with byte strings, i.e. binary data or encoded text, it is best not to type the input argument as something like bytes, because that would restrict the allowed input to exactly that type and exclude both subtypes and other kinds of byte containers, e.g. bytearray objects or memory views.

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)

0reactions
scodercommented, Apr 14, 2020

I’ll close this ticket since it works as designed. Feel free to send a PR for the docs.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found