Hi all,


I'm wondering if anyone knows of a better way to convert a C++ protobuf 
into a Python protobuf.

I'm writing a Python module in C++ that wraps some of our protobuf-related 
functionality such as reading our logs.
Right now I'm serializing the C++ protobuf and then de-serializing it in 
Python.
I'd love to avoid this performance cost.

Here's roughly the code I'm using:


// Python setup code
const char pb_name[] = "foo.Message1";
PyObject* database_module = 
PyImport_ImportModule("google.protobuf.symbol_database");
PyObject* database = PyObject_CallMethod(database_module, "Default", nullptr);
PyObject *msg_class = PyObject_CallMethod(database, "GetSymbol", "s", pb_name);
...

PyObject* ConvertToPythonPB(const ::google::protobuf::Message &msg) {
  // Serialize into string
  auto serialized_msg = msg.SerializeAsString();
  // Create a new message instance
  PyObject *py_msg = PyObject_CallObject(msg_class, nullptr);
  // Deserialize into the Pyhton object
  PyObject *result = PyObject_CallMethod(py_msg, "ParseFromString", "y#",
                                         serialized_msg.data(), 
serialized_msg.size());
  ...
  return result;
}

I've looked into using the cpp protobuf implementation for Python, but I 
haven't had any luck calling that C++ code from my module.

Is there documentation that I'm overlooking? I can't find anything on how 
to do this more easily.


Thanks,
Phil

-- 
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 [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/protobuf.
For more options, visit https://groups.google.com/d/optout.

Reply via email to