17 #ifndef MESOS_NATIVE_COMMON_HPP
18 #define MESOS_NATIVE_COMMON_HPP
26 #include <google/protobuf/io/zero_copy_stream_impl.h>
29 namespace mesos {
namespace python {
42 PyGILState_STATE state;
46 state = PyGILState_Ensure();
50 PyGILState_Release(state);
64 std::cerr <<
"None object given where protobuf expected" << std::endl;
67 PyObject* res = PyObject_CallMethod(obj,
68 (
char*)
"SerializeToString",
71 std::cerr <<
"Failed to call Python object's SerializeToString "
72 <<
"(perhaps it is not a protobuf?)" << std::endl;
78 if (PyString_AsStringAndSize(res, &chars, &len) < 0) {
79 std::cerr <<
"SerializeToString did not return a string" << std::endl;
84 google::protobuf::io::ArrayInputStream stream(chars, len);
85 bool success = t->ParseFromZeroCopyStream(&stream);
87 std::cerr <<
"Could not deserialize protobuf as expected type" << std::endl;
100 template <
typename T>
103 PyObject* dict = PyModule_GetDict(
mesos_pb2);
104 if (dict ==
nullptr) {
105 PyErr_Format(PyExc_Exception,
"PyModule_GetDict failed");
109 PyObject*
type = PyDict_GetItemString(dict, typeName);
110 if (type ==
nullptr) {
111 PyErr_Format(PyExc_Exception,
"Could not resolve mesos_pb2.%s", typeName);
114 if (!PyType_Check(type)) {
115 PyErr_Format(PyExc_Exception,
"mesos_pb2.%s is not a type", typeName);
120 if (!t.SerializeToString(&str)) {
121 PyErr_Format(PyExc_Exception,
"C++ %s SerializeToString failed", typeName);
126 return PyObject_CallMethod(type,
127 (
char*)
"FromString",
PyObject * createPythonProtobuf(const T &t, const char *typeName)
Convert a C++ protocol buffer object into a Python one by serializing it to a string and deserializin...
Definition: common.hpp:101
bool readPythonProtobuf(PyObject *obj, T *t)
Convert a Python protocol buffer object into a C++ one by serializing it to a string and deserializin...
Definition: common.hpp:61
Try< std::string > typeName(uint32_t fsType)
PyObject * mesos_pb2
The Python module object for mesos_pb2 (which contains the protobuf classes generated for Python)...
RAII utility class for acquiring the Python global interpreter lock.
Definition: common.hpp:41
~InterpreterLock()
Definition: common.hpp:49
InterpreterLock()
Definition: common.hpp:45
Try< uint32_t > type(const std::string &path)