17 #ifndef MESOS_NATIVE_COMMON_HPP 18 #define MESOS_NATIVE_COMMON_HPP 29 #include <google/protobuf/io/zero_copy_stream_impl.h> 30 #include <google/protobuf/message.h> 33 namespace mesos {
namespace python {
46 PyGILState_STATE state;
50 state = PyGILState_Ensure();
54 PyGILState_Release(state);
68 std::cerr <<
"None object given where protobuf expected" << std::endl;
71 PyObject* res = PyObject_CallMethod(obj,
72 (
char*)
"SerializeToString",
75 std::cerr <<
"Failed to call Python object's SerializeToString " 76 <<
"(perhaps it is not a protobuf?)" << std::endl;
82 if (PyString_AsStringAndSize(res, &chars, &len) < 0) {
83 std::cerr <<
"SerializeToString did not return a string" << std::endl;
88 google::protobuf::io::ArrayInputStream stream(chars, len);
89 bool success = t->ParseFromZeroCopyStream(&stream);
91 std::cerr <<
"Could not deserialize protobuf as expected type" << std::endl;
104 template <
typename T>
107 PyObject* dict = PyModule_GetDict(mesos_pb2);
108 if (dict ==
nullptr) {
109 PyErr_Format(PyExc_Exception,
"PyModule_GetDict failed");
113 PyObject*
type = PyDict_GetItemString(dict, typeName);
114 if (type ==
nullptr) {
115 PyErr_Format(PyExc_Exception,
"Could not resolve mesos_pb2.%s", typeName);
118 if (!PyType_Check(type)) {
119 PyErr_Format(PyExc_Exception,
"mesos_pb2.%s is not a type", typeName);
124 if (!t.SerializeToString(&str)) {
125 PyErr_Format(PyExc_Exception,
"C++ %s SerializeToString failed", typeName);
130 return PyObject_CallMethod(type,
131 (
char*)
"FromString",
149 template <
typename T>
150 typename std::enable_if<
151 std::is_base_of<google::protobuf::Message, T>::value,
155 std::unique_ptr<T> result(
new T());
159 "Failed to construct %s from a Python object",
160 result->GetDescriptor()->full_name().c_str());
170 template <
typename T>
171 typename std::enable_if<
172 !std::is_base_of<google::protobuf::Message, T>::value,
181 inline std::unique_ptr<std::string> construct<std::string>(PyObject* obj)
185 if (PyString_AsStringAndSize(obj, &chars, &len) < 0) {
189 "Cannot construct std::string from a non-string object");
194 return std::unique_ptr<std::string>(
new std::string(chars, len));
198 template <
typename T>
201 PyObject* pyIterator = PyObject_GetIter(iterable);
202 if (pyIterator ==
nullptr) {
205 "Cannot construct std::vector from a non-iterable object");
209 std::unique_ptr<std::vector<T>> result(
new std::vector<T>());
213 while ((pyItem = PyIter_Next(pyIterator)) !=
nullptr) {
214 std::unique_ptr<T> item = construct<T>(pyItem);
218 Py_DECREF(pyIterator);
222 result->emplace_back(std::move(*item));
226 Py_DECREF(pyIterator);
228 if (PyErr_Occurred() !=
nullptr) {
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:105
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:65
Try< std::string > typeName(uint32_t fsType)
std::unique_ptr< std::vector< T > > constructFromIterable(PyObject *iterable)
Definition: common.hpp:199
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:45
~InterpreterLock()
Definition: common.hpp:53
std::enable_if< std::is_base_of< google::protobuf::Message, T >::value, std::unique_ptr< T > >::type construct(PyObject *obj)
Definition: common.hpp:153
InterpreterLock()
Definition: common.hpp:49
Try< uint32_t > type(const std::string &path)