How to serialize in C++ for parsing in Python

71 views
Skip to first unread message

Randy Nuss

unread,
Oct 14, 2020, 6:55:47 PM10/14/20
to Protocol Buffers
I am building a C++ library with protobuf messages for consumption by a Python program.  The issue is SerializeToString() takes std::string, but only c_types (like char*) can be exported by the dll, which results in numerical zeros being recognized as string terminators and truncating the ParseFromString() process.

I tried using SerializeToArray() but get the following error in Python:
google.protobuf.message.DecodeError: Field number 0 is illegal.

I would appreciate any suggestions.  Thanks.

Adam Cozzette

unread,
Oct 14, 2020, 8:17:35 PM10/14/20
to Randy Nuss, Protocol Buffers
I think if you have to work with C types then you will need to provide both a char* and an integer length. That Python error makes it sound like the Python code is probably trying to parse beyond the end of the serialized message.

--
You received this message because you are subscribed to the Google Groups "Protocol Buffers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to protobuf+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/protobuf/9c3bf578-ba5b-4059-a64b-ec6731690a70n%40googlegroups.com.

Randy Nuss

unread,
Oct 15, 2020, 12:21:47 PM10/15/20
to Protocol Buffers
I solved this.  It did require the Python caller obtaining the string size from the C++ DLL.  The key part is to use ctypes.string_at() to then define the string at the Python end.

lib.fun.argtypes = [c_void_p, c_void_p]
lib.fun.restype = c_void_p

size = c_int()
return_ptr = lib.fun(self.obj, byref(size))
cstring = string_at(return_ptr, size)

Reply all
Reply to author
Forward
0 new messages