| 103 | */ |
| 104 | template <typename T> |
| 105 | PyObject* createPythonProtobuf(const T& t, const char* typeName) |
| 106 | { |
| 107 | PyObject* dict = PyModule_GetDict(mesos_pb2); |
| 108 | if (dict == nullptr) { |
| 109 | PyErr_Format(PyExc_Exception, "PyModule_GetDict failed"); |
| 110 | return nullptr; |
| 111 | } |
| 112 | |
| 113 | PyObject* type = PyDict_GetItemString(dict, typeName); |
| 114 | if (type == nullptr) { |
| 115 | PyErr_Format(PyExc_Exception, "Could not resolve mesos_pb2.%s", typeName); |
| 116 | return nullptr; |
| 117 | } |
| 118 | if (!PyType_Check(type)) { |
| 119 | PyErr_Format(PyExc_Exception, "mesos_pb2.%s is not a type", typeName); |
| 120 | return nullptr; |
| 121 | } |
| 122 | |
| 123 | std::string str; |
| 124 | if (!t.SerializeToString(&str)) { |
| 125 | PyErr_Format(PyExc_Exception, "C++ %s SerializeToString failed", typeName); |
| 126 | return nullptr; |
| 127 | } |
| 128 | |
| 129 | // Propagates any exception that might happen in FromString. |
| 130 | return PyObject_CallMethod(type, |
| 131 | (char*) "FromString", |
| 132 | (char*) "s#", |
| 133 | str.data(), |
| 134 | str.size()); |
| 135 | } |
| 136 | |
| 137 | |
| 138 | // construct<T>() should take PyObject* as an argument, try to convert that |
no test coverage detected