Reading NULL bytes in c extensions

Hi,

I am trying to read a byte string from python which is something as follows "
ZWLFABRIVERZMATZ \x00 Vers User"

since this string has NULL bytes when writing a C extension the API’s are removing the rest of the string.

the string is then interpreted as
"ZWLFABRIVERZMATZ "

I tried the following API’s

PyBytes_AsStringAndSize
PyBytes_AsString

is there anyway to extract the string even if it contains NULL bytes without any modification?
thanks in advance.

Let ‘func_c’ is the C API function. You can you code like this。

p3 = ctypes.create_string_buffer(24)
func_c(p3)
print(p3.raw)

use ‘p3.raw’ can avoid truncate by null byte

I would like to do it in C extension itself.

Do not use C strings, that are \0 terminated.
Use pointer and length APIs.

I use PyBytes_AsStringAndSize for this purpose.
It is documented to be able to return all the bytes without 0x00 being special.

2 Likes

I am unable to do so for the string mentioned above, what should the format of Python Object be while extracting this from PyArg_ParseTupleAndKeywords

currently I am extracting string
b’ZWLFABRIVERZMATZ \x00 Vers User’

as python object ( ‘O’ format).

And what are you then doing with the string after python gave it to you? You can’t just used the C stdlib functions like printf, those will see the NUL byte still, CPython can’t do anything about that.

2 Likes

thank you @MegaIng, yes I was using printf and unable to see the results, when printed individual characters they worked. Thank you @barry-scott .