Skip to content Skip to sidebar Skip to footer

Is An Explicit Nul-byte Necessary At The End Of A Bytearray For Cython To Be Able To Convert It To A Null-terminated C-string

When converting a bytearray-object (or a bytes-object for that matter) to a C-string, the cython-documentation recommends to use the following: cdef char * cstr = py_bytearray the

Solution 1:

I think it's safe (at least in Python 3), however I'd be a bit wary.

Cython uses the C-API function PyByteArray_AsString. The Python3 documentation for it says "The returned array always has an extra null byte appended." The Python2 version does not have that note so it's difficult to be sure if it's safe.

Practically speaking, I think Python deals with this by always over-allocating bytearrays by one and NULL terminating them (see source code for one example of where this is done).

The only reason to be a bit cautious is that it's perfectly acceptable for bytearrays (and Python strings for that matter) to contain a 0 byte within the string, so it isn't a good indicator of where the end is. Therefore, you should really be using their len anyway. (This is a weak argument though, especially since you're probably the one initializing them, so you know if this should be true)


(My initial version of this answer had something about _PyByteArray_empty_string. @ead pointed out in the comments that I was mistaken about this and hence it's edited out...)

Post a Comment for "Is An Explicit Nul-byte Necessary At The End Of A Bytearray For Cython To Be Able To Convert It To A Null-terminated C-string"