PageRenderTime 54ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/thirdparty/breakpad/third_party/protobuf/protobuf/python/google/protobuf/pyext/python-proto2.cc

http://github.com/tomahawk-player/tomahawk
C++ | 1660 lines | 1413 code | 184 blank | 63 comment | 208 complexity | b734479db7d2be23014ee74a35e84d98 MD5 | raw file
Possible License(s): LGPL-2.1, BSD-3-Clause, GPL-3.0, GPL-2.0
  1. // Protocol Buffers - Google's data interchange format
  2. // Copyright 2008 Google Inc. All rights reserved.
  3. // http://code.google.com/p/protobuf/
  4. //
  5. // Redistribution and use in source and binary forms, with or without
  6. // modification, are permitted provided that the following conditions are
  7. // met:
  8. //
  9. // * Redistributions of source code must retain the above copyright
  10. // notice, this list of conditions and the following disclaimer.
  11. // * Redistributions in binary form must reproduce the above
  12. // copyright notice, this list of conditions and the following disclaimer
  13. // in the documentation and/or other materials provided with the
  14. // distribution.
  15. // * Neither the name of Google Inc. nor the names of its
  16. // contributors may be used to endorse or promote products derived from
  17. // this software without specific prior written permission.
  18. //
  19. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  22. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  23. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  24. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  25. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  26. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  27. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  28. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  29. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. // Author: petar@google.com (Petar Petrov)
  31. #include <Python.h>
  32. #include <map>
  33. #include <string>
  34. #include <vector>
  35. #include <google/protobuf/stubs/common.h>
  36. #include <google/protobuf/pyext/python_descriptor.h>
  37. #include <google/protobuf/io/coded_stream.h>
  38. #include <google/protobuf/descriptor.h>
  39. #include <google/protobuf/dynamic_message.h>
  40. #include <google/protobuf/message.h>
  41. #include <google/protobuf/unknown_field_set.h>
  42. #include <google/protobuf/pyext/python_protobuf.h>
  43. /* Is 64bit */
  44. #define IS_64BIT (SIZEOF_LONG == 8)
  45. #define FIELD_BELONGS_TO_MESSAGE(field_descriptor, message) \
  46. ((message)->GetDescriptor() == (field_descriptor)->containing_type())
  47. #define FIELD_IS_REPEATED(field_descriptor) \
  48. ((field_descriptor)->label() == google::protobuf::FieldDescriptor::LABEL_REPEATED)
  49. #define GOOGLE_CHECK_GET_INT32(arg, value) \
  50. int32 value; \
  51. if (!CheckAndGetInteger(arg, &value, kint32min_py, kint32max_py)) { \
  52. return NULL; \
  53. }
  54. #define GOOGLE_CHECK_GET_INT64(arg, value) \
  55. int64 value; \
  56. if (!CheckAndGetInteger(arg, &value, kint64min_py, kint64max_py)) { \
  57. return NULL; \
  58. }
  59. #define GOOGLE_CHECK_GET_UINT32(arg, value) \
  60. uint32 value; \
  61. if (!CheckAndGetInteger(arg, &value, kPythonZero, kuint32max_py)) { \
  62. return NULL; \
  63. }
  64. #define GOOGLE_CHECK_GET_UINT64(arg, value) \
  65. uint64 value; \
  66. if (!CheckAndGetInteger(arg, &value, kPythonZero, kuint64max_py)) { \
  67. return NULL; \
  68. }
  69. #define GOOGLE_CHECK_GET_FLOAT(arg, value) \
  70. float value; \
  71. if (!CheckAndGetFloat(arg, &value)) { \
  72. return NULL; \
  73. } \
  74. #define GOOGLE_CHECK_GET_DOUBLE(arg, value) \
  75. double value; \
  76. if (!CheckAndGetDouble(arg, &value)) { \
  77. return NULL; \
  78. }
  79. #define GOOGLE_CHECK_GET_BOOL(arg, value) \
  80. bool value; \
  81. if (!CheckAndGetBool(arg, &value)) { \
  82. return NULL; \
  83. }
  84. #define C(str) const_cast<char*>(str)
  85. // --- Globals:
  86. // Constants used for integer type range checking.
  87. static PyObject* kPythonZero;
  88. static PyObject* kint32min_py;
  89. static PyObject* kint32max_py;
  90. static PyObject* kuint32max_py;
  91. static PyObject* kint64min_py;
  92. static PyObject* kint64max_py;
  93. static PyObject* kuint64max_py;
  94. namespace google {
  95. namespace protobuf {
  96. namespace python {
  97. // --- Support Routines:
  98. static void AddConstants(PyObject* module) {
  99. struct NameValue {
  100. char* name;
  101. int32 value;
  102. } constants[] = {
  103. // Labels:
  104. {"LABEL_OPTIONAL", google::protobuf::FieldDescriptor::LABEL_OPTIONAL},
  105. {"LABEL_REQUIRED", google::protobuf::FieldDescriptor::LABEL_REQUIRED},
  106. {"LABEL_REPEATED", google::protobuf::FieldDescriptor::LABEL_REPEATED},
  107. // CPP types:
  108. {"CPPTYPE_MESSAGE", google::protobuf::FieldDescriptor::CPPTYPE_MESSAGE},
  109. // Field Types:
  110. {"TYPE_MESSAGE", google::protobuf::FieldDescriptor::TYPE_MESSAGE},
  111. // End.
  112. {NULL, 0}
  113. };
  114. for (NameValue* constant = constants;
  115. constant->name != NULL; constant++) {
  116. PyModule_AddIntConstant(module, constant->name, constant->value);
  117. }
  118. }
  119. // --- CMessage Custom Type:
  120. // ------ Type Forward Declaration:
  121. struct CMessage;
  122. struct CMessage_Type;
  123. static void CMessageDealloc(CMessage* self);
  124. static int CMessageInit(CMessage* self, PyObject *args, PyObject *kwds);
  125. static PyObject* CMessageStr(CMessage* self);
  126. static PyObject* CMessage_AddMessage(CMessage* self, PyObject* args);
  127. static PyObject* CMessage_AddRepeatedScalar(CMessage* self, PyObject* args);
  128. static PyObject* CMessage_AssignRepeatedScalar(CMessage* self, PyObject* args);
  129. static PyObject* CMessage_ByteSize(CMessage* self, PyObject* args);
  130. static PyObject* CMessage_Clear(CMessage* self, PyObject* args);
  131. static PyObject* CMessage_ClearField(CMessage* self, PyObject* args);
  132. static PyObject* CMessage_ClearFieldByDescriptor(
  133. CMessage* self, PyObject* args);
  134. static PyObject* CMessage_CopyFrom(CMessage* self, PyObject* args);
  135. static PyObject* CMessage_DebugString(CMessage* self, PyObject* args);
  136. static PyObject* CMessage_DeleteRepeatedField(CMessage* self, PyObject* args);
  137. static PyObject* CMessage_Equals(CMessage* self, PyObject* args);
  138. static PyObject* CMessage_FieldLength(CMessage* self, PyObject* args);
  139. static PyObject* CMessage_FindInitializationErrors(CMessage* self);
  140. static PyObject* CMessage_GetRepeatedMessage(CMessage* self, PyObject* args);
  141. static PyObject* CMessage_GetRepeatedScalar(CMessage* self, PyObject* args);
  142. static PyObject* CMessage_GetScalar(CMessage* self, PyObject* args);
  143. static PyObject* CMessage_HasField(CMessage* self, PyObject* args);
  144. static PyObject* CMessage_HasFieldByDescriptor(CMessage* self, PyObject* args);
  145. static PyObject* CMessage_IsInitialized(CMessage* self, PyObject* args);
  146. static PyObject* CMessage_ListFields(CMessage* self, PyObject* args);
  147. static PyObject* CMessage_MergeFrom(CMessage* self, PyObject* args);
  148. static PyObject* CMessage_MergeFromString(CMessage* self, PyObject* args);
  149. static PyObject* CMessage_MutableMessage(CMessage* self, PyObject* args);
  150. static PyObject* CMessage_NewSubMessage(CMessage* self, PyObject* args);
  151. static PyObject* CMessage_SetScalar(CMessage* self, PyObject* args);
  152. static PyObject* CMessage_SerializePartialToString(
  153. CMessage* self, PyObject* args);
  154. static PyObject* CMessage_SerializeToString(CMessage* self, PyObject* args);
  155. static PyObject* CMessage_SetInParent(CMessage* self, PyObject* args);
  156. static PyObject* CMessage_SwapRepeatedFieldElements(
  157. CMessage* self, PyObject* args);
  158. // ------ Object Definition:
  159. typedef struct CMessage {
  160. PyObject_HEAD
  161. struct CMessage* parent; // NULL if wasn't created from another message.
  162. CFieldDescriptor* parent_field;
  163. const char* full_name;
  164. google::protobuf::Message* message;
  165. bool free_message;
  166. bool read_only;
  167. } CMessage;
  168. // ------ Method Table:
  169. #define CMETHOD(name, args, doc) \
  170. { C(#name), (PyCFunction)CMessage_##name, args, C(doc) }
  171. static PyMethodDef CMessageMethods[] = {
  172. CMETHOD(AddMessage, METH_O,
  173. "Adds a new message to a repeated composite field."),
  174. CMETHOD(AddRepeatedScalar, METH_VARARGS,
  175. "Adds a scalar to a repeated scalar field."),
  176. CMETHOD(AssignRepeatedScalar, METH_VARARGS,
  177. "Clears and sets the values of a repeated scalar field."),
  178. CMETHOD(ByteSize, METH_NOARGS,
  179. "Returns the size of the message in bytes."),
  180. CMETHOD(Clear, METH_NOARGS,
  181. "Clears a protocol message."),
  182. CMETHOD(ClearField, METH_O,
  183. "Clears a protocol message field by name."),
  184. CMETHOD(ClearFieldByDescriptor, METH_O,
  185. "Clears a protocol message field by descriptor."),
  186. CMETHOD(CopyFrom, METH_O,
  187. "Copies a protocol message into the current message."),
  188. CMETHOD(DebugString, METH_NOARGS,
  189. "Returns the debug string of a protocol message."),
  190. CMETHOD(DeleteRepeatedField, METH_VARARGS,
  191. "Deletes a slice of values from a repeated field."),
  192. CMETHOD(Equals, METH_O,
  193. "Checks if two protocol messages are equal (by identity)."),
  194. CMETHOD(FieldLength, METH_O,
  195. "Returns the number of elements in a repeated field."),
  196. CMETHOD(FindInitializationErrors, METH_NOARGS,
  197. "Returns the initialization errors of a message."),
  198. CMETHOD(GetRepeatedMessage, METH_VARARGS,
  199. "Returns a message from a repeated composite field."),
  200. CMETHOD(GetRepeatedScalar, METH_VARARGS,
  201. "Returns a scalar value from a repeated scalar field."),
  202. CMETHOD(GetScalar, METH_O,
  203. "Returns the scalar value of a field."),
  204. CMETHOD(HasField, METH_O,
  205. "Checks if a message field is set."),
  206. CMETHOD(HasFieldByDescriptor, METH_O,
  207. "Checks if a message field is set by given its descriptor"),
  208. CMETHOD(IsInitialized, METH_NOARGS,
  209. "Checks if all required fields of a protocol message are set."),
  210. CMETHOD(ListFields, METH_NOARGS,
  211. "Lists all set fields of a message."),
  212. CMETHOD(MergeFrom, METH_O,
  213. "Merges a protocol message into the current message."),
  214. CMETHOD(MergeFromString, METH_O,
  215. "Merges a serialized message into the current message."),
  216. CMETHOD(MutableMessage, METH_O,
  217. "Returns a new instance of a nested protocol message."),
  218. CMETHOD(NewSubMessage, METH_O,
  219. "Creates and returns a python message given the descriptor of a "
  220. "composite field of the current message."),
  221. CMETHOD(SetScalar, METH_VARARGS,
  222. "Sets the value of a singular scalar field."),
  223. CMETHOD(SerializePartialToString, METH_VARARGS,
  224. "Serializes the message to a string, even if it isn't initialized."),
  225. CMETHOD(SerializeToString, METH_NOARGS,
  226. "Serializes the message to a string, only for initialized messages."),
  227. CMETHOD(SetInParent, METH_NOARGS,
  228. "Sets the has bit of the given field in its parent message."),
  229. CMETHOD(SwapRepeatedFieldElements, METH_VARARGS,
  230. "Swaps the elements in two positions in a repeated field."),
  231. { NULL, NULL }
  232. };
  233. #undef CMETHOD
  234. static PyMemberDef CMessageMembers[] = {
  235. { C("full_name"), T_STRING, offsetof(CMessage, full_name), 0, "Full name" },
  236. { NULL }
  237. };
  238. // ------ Type Definition:
  239. // The definition for the type object that captures the type of CMessage
  240. // in Python.
  241. PyTypeObject CMessage_Type = {
  242. PyObject_HEAD_INIT(&PyType_Type)
  243. 0,
  244. C("google3.net.google.protobuf.python.internal."
  245. "_net_proto2___python."
  246. "CMessage"), // tp_name
  247. sizeof(CMessage), // tp_basicsize
  248. 0, // tp_itemsize
  249. (destructor)CMessageDealloc, // tp_dealloc
  250. 0, // tp_print
  251. 0, // tp_getattr
  252. 0, // tp_setattr
  253. 0, // tp_compare
  254. 0, // tp_repr
  255. 0, // tp_as_number
  256. 0, // tp_as_sequence
  257. 0, // tp_as_mapping
  258. 0, // tp_hash
  259. 0, // tp_call
  260. (reprfunc)CMessageStr, // tp_str
  261. 0, // tp_getattro
  262. 0, // tp_setattro
  263. 0, // tp_as_buffer
  264. Py_TPFLAGS_DEFAULT, // tp_flags
  265. C("A ProtocolMessage"), // tp_doc
  266. 0, // tp_traverse
  267. 0, // tp_clear
  268. 0, // tp_richcompare
  269. 0, // tp_weaklistoffset
  270. 0, // tp_iter
  271. 0, // tp_iternext
  272. CMessageMethods, // tp_methods
  273. CMessageMembers, // tp_members
  274. 0, // tp_getset
  275. 0, // tp_base
  276. 0, // tp_dict
  277. 0, // tp_descr_get
  278. 0, // tp_descr_set
  279. 0, // tp_dictoffset
  280. (initproc)CMessageInit, // tp_init
  281. PyType_GenericAlloc, // tp_alloc
  282. PyType_GenericNew, // tp_new
  283. PyObject_Del, // tp_free
  284. };
  285. // ------ Helper Functions:
  286. static void FormatTypeError(PyObject* arg, char* expected_types) {
  287. PyObject* s = PyObject_Str(PyObject_Type(arg));
  288. PyObject* repr = PyObject_Repr(PyObject_Type(arg));
  289. PyErr_Format(PyExc_TypeError,
  290. "%.100s has type %.100s, but expected one of: %s",
  291. PyString_AS_STRING(repr),
  292. PyString_AS_STRING(s),
  293. expected_types);
  294. Py_DECREF(s);
  295. Py_DECREF(repr);
  296. }
  297. template <class T>
  298. static bool CheckAndGetInteger(
  299. PyObject* arg, T* value, PyObject* min, PyObject* max) {
  300. bool is_long = PyLong_Check(arg);
  301. if (!PyInt_Check(arg) && !is_long) {
  302. FormatTypeError(arg, "int, long");
  303. return false;
  304. }
  305. if (PyObject_Compare(min, arg) > 0 || PyObject_Compare(max, arg) < 0) {
  306. PyObject* s = PyObject_Str(arg);
  307. PyErr_Format(PyExc_ValueError,
  308. "Value out of range: %s",
  309. PyString_AS_STRING(s));
  310. Py_DECREF(s);
  311. return false;
  312. }
  313. if (is_long) {
  314. if (min == kPythonZero) {
  315. *value = static_cast<T>(PyLong_AsUnsignedLongLong(arg));
  316. } else {
  317. *value = static_cast<T>(PyLong_AsLongLong(arg));
  318. }
  319. } else {
  320. *value = static_cast<T>(PyInt_AsLong(arg));
  321. }
  322. return true;
  323. }
  324. static bool CheckAndGetDouble(PyObject* arg, double* value) {
  325. if (!PyInt_Check(arg) && !PyLong_Check(arg) &&
  326. !PyFloat_Check(arg)) {
  327. FormatTypeError(arg, "int, long, float");
  328. return false;
  329. }
  330. *value = PyFloat_AsDouble(arg);
  331. return true;
  332. }
  333. static bool CheckAndGetFloat(PyObject* arg, float* value) {
  334. double double_value;
  335. if (!CheckAndGetDouble(arg, &double_value)) {
  336. return false;
  337. }
  338. *value = static_cast<float>(double_value);
  339. return true;
  340. }
  341. static bool CheckAndGetBool(PyObject* arg, bool* value) {
  342. if (!PyInt_Check(arg) && !PyBool_Check(arg) && !PyLong_Check(arg)) {
  343. FormatTypeError(arg, "int, long, bool");
  344. return false;
  345. }
  346. *value = static_cast<bool>(PyInt_AsLong(arg));
  347. return true;
  348. }
  349. google::protobuf::DynamicMessageFactory* global_message_factory = NULL;
  350. static const google::protobuf::Message* CreateMessage(const char* message_type) {
  351. string message_name(message_type);
  352. const google::protobuf::Descriptor* descriptor =
  353. GetDescriptorPool()->FindMessageTypeByName(message_name);
  354. if (descriptor == NULL) {
  355. return NULL;
  356. }
  357. return global_message_factory->GetPrototype(descriptor);
  358. }
  359. static bool CheckAndSetString(
  360. PyObject* arg, google::protobuf::Message* message,
  361. const google::protobuf::FieldDescriptor* descriptor,
  362. const google::protobuf::Reflection* reflection,
  363. bool append,
  364. int index) {
  365. GOOGLE_DCHECK(descriptor->type() == google::protobuf::FieldDescriptor::TYPE_STRING ||
  366. descriptor->type() == google::protobuf::FieldDescriptor::TYPE_BYTES);
  367. if (descriptor->type() == google::protobuf::FieldDescriptor::TYPE_STRING) {
  368. if (!PyString_Check(arg) && !PyUnicode_Check(arg)) {
  369. FormatTypeError(arg, "str, unicode");
  370. return false;
  371. }
  372. if (PyString_Check(arg)) {
  373. PyObject* unicode = PyUnicode_FromEncodedObject(arg, "ascii", NULL);
  374. if (unicode == NULL) {
  375. PyObject* repr = PyObject_Repr(arg);
  376. PyErr_Format(PyExc_ValueError,
  377. "%s has type str, but isn't in 7-bit ASCII "
  378. "encoding. Non-ASCII strings must be converted to "
  379. "unicode objects before being added.",
  380. PyString_AS_STRING(repr));
  381. Py_DECREF(repr);
  382. return false;
  383. } else {
  384. Py_DECREF(unicode);
  385. }
  386. }
  387. } else if (!PyString_Check(arg)) {
  388. FormatTypeError(arg, "str");
  389. return false;
  390. }
  391. PyObject* encoded_string = NULL;
  392. if (descriptor->type() == google::protobuf::FieldDescriptor::TYPE_STRING) {
  393. if (PyString_Check(arg)) {
  394. encoded_string = PyString_AsEncodedObject(arg, "utf-8", NULL);
  395. } else {
  396. encoded_string = PyUnicode_AsEncodedObject(arg, "utf-8", NULL);
  397. }
  398. } else {
  399. // In this case field type is "bytes".
  400. encoded_string = arg;
  401. Py_INCREF(encoded_string);
  402. }
  403. if (encoded_string == NULL) {
  404. return false;
  405. }
  406. char* value;
  407. Py_ssize_t value_len;
  408. if (PyString_AsStringAndSize(encoded_string, &value, &value_len) < 0) {
  409. Py_DECREF(encoded_string);
  410. return false;
  411. }
  412. string value_string(value, value_len);
  413. if (append) {
  414. reflection->AddString(message, descriptor, value_string);
  415. } else if (index < 0) {
  416. reflection->SetString(message, descriptor, value_string);
  417. } else {
  418. reflection->SetRepeatedString(message, descriptor, index, value_string);
  419. }
  420. Py_DECREF(encoded_string);
  421. return true;
  422. }
  423. static PyObject* ToStringObject(
  424. const google::protobuf::FieldDescriptor* descriptor, string value) {
  425. if (descriptor->type() != google::protobuf::FieldDescriptor::TYPE_STRING) {
  426. return PyString_FromStringAndSize(value.c_str(), value.length());
  427. }
  428. PyObject* result = PyUnicode_DecodeUTF8(value.c_str(), value.length(), NULL);
  429. // If the string can't be decoded in UTF-8, just return a string object that
  430. // contains the raw bytes. This can't happen if the value was assigned using
  431. // the members of the Python message object, but can happen if the values were
  432. // parsed from the wire (binary).
  433. if (result == NULL) {
  434. PyErr_Clear();
  435. result = PyString_FromStringAndSize(value.c_str(), value.length());
  436. }
  437. return result;
  438. }
  439. static void AssureWritable(CMessage* self) {
  440. if (self == NULL ||
  441. self->parent == NULL ||
  442. self->parent_field == NULL) {
  443. return;
  444. }
  445. if (!self->read_only) {
  446. return;
  447. }
  448. AssureWritable(self->parent);
  449. google::protobuf::Message* message = self->parent->message;
  450. const google::protobuf::Reflection* reflection = message->GetReflection();
  451. self->message = reflection->MutableMessage(
  452. message, self->parent_field->descriptor, global_message_factory);
  453. self->read_only = false;
  454. self->parent = NULL;
  455. self->parent_field = NULL;
  456. }
  457. static PyObject* InternalGetScalar(
  458. google::protobuf::Message* message,
  459. const google::protobuf::FieldDescriptor* field_descriptor) {
  460. const google::protobuf::Reflection* reflection = message->GetReflection();
  461. if (!FIELD_BELONGS_TO_MESSAGE(field_descriptor, message)) {
  462. PyErr_SetString(
  463. PyExc_KeyError, "Field does not belong to message!");
  464. return NULL;
  465. }
  466. PyObject* result = NULL;
  467. switch (field_descriptor->cpp_type()) {
  468. case google::protobuf::FieldDescriptor::CPPTYPE_INT32: {
  469. int32 value = reflection->GetInt32(*message, field_descriptor);
  470. result = PyInt_FromLong(value);
  471. break;
  472. }
  473. case google::protobuf::FieldDescriptor::CPPTYPE_INT64: {
  474. int64 value = reflection->GetInt64(*message, field_descriptor);
  475. #if IS_64BIT
  476. result = PyInt_FromLong(value);
  477. #else
  478. result = PyLong_FromLongLong(value);
  479. #endif
  480. break;
  481. }
  482. case google::protobuf::FieldDescriptor::CPPTYPE_UINT32: {
  483. uint32 value = reflection->GetUInt32(*message, field_descriptor);
  484. #if IS_64BIT
  485. result = PyInt_FromLong(value);
  486. #else
  487. result = PyLong_FromLongLong(value);
  488. #endif
  489. break;
  490. }
  491. case google::protobuf::FieldDescriptor::CPPTYPE_UINT64: {
  492. uint64 value = reflection->GetUInt64(*message, field_descriptor);
  493. #if IS_64BIT
  494. if (value <= static_cast<uint64>(kint64max)) {
  495. result = PyInt_FromLong(static_cast<uint64>(value));
  496. }
  497. #else
  498. if (value <= static_cast<uint32>(kint32max)) {
  499. result = PyInt_FromLong(static_cast<uint32>(value));
  500. }
  501. #endif
  502. else { // NOLINT
  503. result = PyLong_FromUnsignedLongLong(value);
  504. }
  505. break;
  506. }
  507. case google::protobuf::FieldDescriptor::CPPTYPE_FLOAT: {
  508. float value = reflection->GetFloat(*message, field_descriptor);
  509. result = PyFloat_FromDouble(value);
  510. break;
  511. }
  512. case google::protobuf::FieldDescriptor::CPPTYPE_DOUBLE: {
  513. double value = reflection->GetDouble(*message, field_descriptor);
  514. result = PyFloat_FromDouble(value);
  515. break;
  516. }
  517. case google::protobuf::FieldDescriptor::CPPTYPE_BOOL: {
  518. bool value = reflection->GetBool(*message, field_descriptor);
  519. result = PyBool_FromLong(value);
  520. break;
  521. }
  522. case google::protobuf::FieldDescriptor::CPPTYPE_STRING: {
  523. string value = reflection->GetString(*message, field_descriptor);
  524. result = ToStringObject(field_descriptor, value);
  525. break;
  526. }
  527. case google::protobuf::FieldDescriptor::CPPTYPE_ENUM: {
  528. if (!message->GetReflection()->HasField(*message, field_descriptor)) {
  529. // Look for the value in the unknown fields.
  530. google::protobuf::UnknownFieldSet* unknown_field_set =
  531. message->GetReflection()->MutableUnknownFields(message);
  532. for (int i = 0; i < unknown_field_set->field_count(); ++i) {
  533. if (unknown_field_set->field(i).number() ==
  534. field_descriptor->number()) {
  535. result = PyInt_FromLong(unknown_field_set->field(i).varint());
  536. break;
  537. }
  538. }
  539. }
  540. if (result == NULL) {
  541. const google::protobuf::EnumValueDescriptor* enum_value =
  542. message->GetReflection()->GetEnum(*message, field_descriptor);
  543. result = PyInt_FromLong(enum_value->number());
  544. }
  545. break;
  546. }
  547. default:
  548. PyErr_Format(
  549. PyExc_SystemError, "Getting a value from a field of unknown type %d",
  550. field_descriptor->cpp_type());
  551. }
  552. return result;
  553. }
  554. static PyObject* InternalSetScalar(
  555. google::protobuf::Message* message, const google::protobuf::FieldDescriptor* field_descriptor,
  556. PyObject* arg) {
  557. const google::protobuf::Reflection* reflection = message->GetReflection();
  558. if (!FIELD_BELONGS_TO_MESSAGE(field_descriptor, message)) {
  559. PyErr_SetString(
  560. PyExc_KeyError, "Field does not belong to message!");
  561. return NULL;
  562. }
  563. switch (field_descriptor->cpp_type()) {
  564. case google::protobuf::FieldDescriptor::CPPTYPE_INT32: {
  565. GOOGLE_CHECK_GET_INT32(arg, value);
  566. reflection->SetInt32(message, field_descriptor, value);
  567. break;
  568. }
  569. case google::protobuf::FieldDescriptor::CPPTYPE_INT64: {
  570. GOOGLE_CHECK_GET_INT64(arg, value);
  571. reflection->SetInt64(message, field_descriptor, value);
  572. break;
  573. }
  574. case google::protobuf::FieldDescriptor::CPPTYPE_UINT32: {
  575. GOOGLE_CHECK_GET_UINT32(arg, value);
  576. reflection->SetUInt32(message, field_descriptor, value);
  577. break;
  578. }
  579. case google::protobuf::FieldDescriptor::CPPTYPE_UINT64: {
  580. GOOGLE_CHECK_GET_UINT64(arg, value);
  581. reflection->SetUInt64(message, field_descriptor, value);
  582. break;
  583. }
  584. case google::protobuf::FieldDescriptor::CPPTYPE_FLOAT: {
  585. GOOGLE_CHECK_GET_FLOAT(arg, value);
  586. reflection->SetFloat(message, field_descriptor, value);
  587. break;
  588. }
  589. case google::protobuf::FieldDescriptor::CPPTYPE_DOUBLE: {
  590. GOOGLE_CHECK_GET_DOUBLE(arg, value);
  591. reflection->SetDouble(message, field_descriptor, value);
  592. break;
  593. }
  594. case google::protobuf::FieldDescriptor::CPPTYPE_BOOL: {
  595. GOOGLE_CHECK_GET_BOOL(arg, value);
  596. reflection->SetBool(message, field_descriptor, value);
  597. break;
  598. }
  599. case google::protobuf::FieldDescriptor::CPPTYPE_STRING: {
  600. if (!CheckAndSetString(
  601. arg, message, field_descriptor, reflection, false, -1)) {
  602. return NULL;
  603. }
  604. break;
  605. }
  606. case google::protobuf::FieldDescriptor::CPPTYPE_ENUM: {
  607. GOOGLE_CHECK_GET_INT32(arg, value);
  608. const google::protobuf::EnumDescriptor* enum_descriptor =
  609. field_descriptor->enum_type();
  610. const google::protobuf::EnumValueDescriptor* enum_value =
  611. enum_descriptor->FindValueByNumber(value);
  612. if (enum_value != NULL) {
  613. reflection->SetEnum(message, field_descriptor, enum_value);
  614. } else {
  615. bool added = false;
  616. // Add the value to the unknown fields.
  617. google::protobuf::UnknownFieldSet* unknown_field_set =
  618. message->GetReflection()->MutableUnknownFields(message);
  619. for (int i = 0; i < unknown_field_set->field_count(); ++i) {
  620. if (unknown_field_set->field(i).number() ==
  621. field_descriptor->number()) {
  622. unknown_field_set->mutable_field(i)->set_varint(value);
  623. added = true;
  624. break;
  625. }
  626. }
  627. if (!added) {
  628. unknown_field_set->AddVarint(field_descriptor->number(), value);
  629. }
  630. reflection->ClearField(message, field_descriptor);
  631. }
  632. break;
  633. }
  634. default:
  635. PyErr_Format(
  636. PyExc_SystemError, "Setting value to a field of unknown type %d",
  637. field_descriptor->cpp_type());
  638. }
  639. Py_RETURN_NONE;
  640. }
  641. static PyObject* InternalAddRepeatedScalar(
  642. google::protobuf::Message* message, const google::protobuf::FieldDescriptor* field_descriptor,
  643. PyObject* arg) {
  644. if (!FIELD_BELONGS_TO_MESSAGE(field_descriptor, message)) {
  645. PyErr_SetString(
  646. PyExc_KeyError, "Field does not belong to message!");
  647. return NULL;
  648. }
  649. const google::protobuf::Reflection* reflection = message->GetReflection();
  650. switch (field_descriptor->cpp_type()) {
  651. case google::protobuf::FieldDescriptor::CPPTYPE_INT32: {
  652. GOOGLE_CHECK_GET_INT32(arg, value);
  653. reflection->AddInt32(message, field_descriptor, value);
  654. break;
  655. }
  656. case google::protobuf::FieldDescriptor::CPPTYPE_INT64: {
  657. GOOGLE_CHECK_GET_INT64(arg, value);
  658. reflection->AddInt64(message, field_descriptor, value);
  659. break;
  660. }
  661. case google::protobuf::FieldDescriptor::CPPTYPE_UINT32: {
  662. GOOGLE_CHECK_GET_UINT32(arg, value);
  663. reflection->AddUInt32(message, field_descriptor, value);
  664. break;
  665. }
  666. case google::protobuf::FieldDescriptor::CPPTYPE_UINT64: {
  667. GOOGLE_CHECK_GET_UINT64(arg, value);
  668. reflection->AddUInt64(message, field_descriptor, value);
  669. break;
  670. }
  671. case google::protobuf::FieldDescriptor::CPPTYPE_FLOAT: {
  672. GOOGLE_CHECK_GET_FLOAT(arg, value);
  673. reflection->AddFloat(message, field_descriptor, value);
  674. break;
  675. }
  676. case google::protobuf::FieldDescriptor::CPPTYPE_DOUBLE: {
  677. GOOGLE_CHECK_GET_DOUBLE(arg, value);
  678. reflection->AddDouble(message, field_descriptor, value);
  679. break;
  680. }
  681. case google::protobuf::FieldDescriptor::CPPTYPE_BOOL: {
  682. GOOGLE_CHECK_GET_BOOL(arg, value);
  683. reflection->AddBool(message, field_descriptor, value);
  684. break;
  685. }
  686. case google::protobuf::FieldDescriptor::CPPTYPE_STRING: {
  687. if (!CheckAndSetString(
  688. arg, message, field_descriptor, reflection, true, -1)) {
  689. return NULL;
  690. }
  691. break;
  692. }
  693. case google::protobuf::FieldDescriptor::CPPTYPE_ENUM: {
  694. GOOGLE_CHECK_GET_INT32(arg, value);
  695. const google::protobuf::EnumDescriptor* enum_descriptor =
  696. field_descriptor->enum_type();
  697. const google::protobuf::EnumValueDescriptor* enum_value =
  698. enum_descriptor->FindValueByNumber(value);
  699. if (enum_value != NULL) {
  700. reflection->AddEnum(message, field_descriptor, enum_value);
  701. } else {
  702. PyObject* s = PyObject_Str(arg);
  703. PyErr_Format(PyExc_ValueError, "Unknown enum value: %s",
  704. PyString_AS_STRING(s));
  705. Py_DECREF(s);
  706. return NULL;
  707. }
  708. break;
  709. }
  710. default:
  711. PyErr_Format(
  712. PyExc_SystemError, "Adding value to a field of unknown type %d",
  713. field_descriptor->cpp_type());
  714. }
  715. Py_RETURN_NONE;
  716. }
  717. static PyObject* InternalGetRepeatedScalar(
  718. CMessage* cmessage, const google::protobuf::FieldDescriptor* field_descriptor,
  719. int index) {
  720. google::protobuf::Message* message = cmessage->message;
  721. const google::protobuf::Reflection* reflection = message->GetReflection();
  722. int field_size = reflection->FieldSize(*message, field_descriptor);
  723. if (index < 0) {
  724. index = field_size + index;
  725. }
  726. if (index < 0 || index >= field_size) {
  727. PyErr_Format(PyExc_IndexError,
  728. "list assignment index (%d) out of range", index);
  729. return NULL;
  730. }
  731. PyObject* result = NULL;
  732. switch (field_descriptor->cpp_type()) {
  733. case google::protobuf::FieldDescriptor::CPPTYPE_INT32: {
  734. int32 value = reflection->GetRepeatedInt32(
  735. *message, field_descriptor, index);
  736. result = PyInt_FromLong(value);
  737. break;
  738. }
  739. case google::protobuf::FieldDescriptor::CPPTYPE_INT64: {
  740. int64 value = reflection->GetRepeatedInt64(
  741. *message, field_descriptor, index);
  742. result = PyLong_FromLongLong(value);
  743. break;
  744. }
  745. case google::protobuf::FieldDescriptor::CPPTYPE_UINT32: {
  746. uint32 value = reflection->GetRepeatedUInt32(
  747. *message, field_descriptor, index);
  748. result = PyLong_FromLongLong(value);
  749. break;
  750. }
  751. case google::protobuf::FieldDescriptor::CPPTYPE_UINT64: {
  752. uint64 value = reflection->GetRepeatedUInt64(
  753. *message, field_descriptor, index);
  754. result = PyLong_FromUnsignedLongLong(value);
  755. break;
  756. }
  757. case google::protobuf::FieldDescriptor::CPPTYPE_FLOAT: {
  758. float value = reflection->GetRepeatedFloat(
  759. *message, field_descriptor, index);
  760. result = PyFloat_FromDouble(value);
  761. break;
  762. }
  763. case google::protobuf::FieldDescriptor::CPPTYPE_DOUBLE: {
  764. double value = reflection->GetRepeatedDouble(
  765. *message, field_descriptor, index);
  766. result = PyFloat_FromDouble(value);
  767. break;
  768. }
  769. case google::protobuf::FieldDescriptor::CPPTYPE_BOOL: {
  770. bool value = reflection->GetRepeatedBool(
  771. *message, field_descriptor, index);
  772. result = PyBool_FromLong(value ? 1 : 0);
  773. break;
  774. }
  775. case google::protobuf::FieldDescriptor::CPPTYPE_ENUM: {
  776. const google::protobuf::EnumValueDescriptor* enum_value =
  777. message->GetReflection()->GetRepeatedEnum(
  778. *message, field_descriptor, index);
  779. result = PyInt_FromLong(enum_value->number());
  780. break;
  781. }
  782. case google::protobuf::FieldDescriptor::CPPTYPE_STRING: {
  783. string value = reflection->GetRepeatedString(
  784. *message, field_descriptor, index);
  785. result = ToStringObject(field_descriptor, value);
  786. break;
  787. }
  788. case google::protobuf::FieldDescriptor::CPPTYPE_MESSAGE: {
  789. CMessage* py_cmsg = PyObject_New(CMessage, &CMessage_Type);
  790. if (py_cmsg == NULL) {
  791. return NULL;
  792. }
  793. const google::protobuf::Message& msg = reflection->GetRepeatedMessage(
  794. *message, field_descriptor, index);
  795. py_cmsg->parent = cmessage;
  796. py_cmsg->full_name = field_descriptor->full_name().c_str();
  797. py_cmsg->message = const_cast<google::protobuf::Message*>(&msg);
  798. py_cmsg->free_message = false;
  799. py_cmsg->read_only = false;
  800. result = reinterpret_cast<PyObject*>(py_cmsg);
  801. break;
  802. }
  803. default:
  804. PyErr_Format(
  805. PyExc_SystemError,
  806. "Getting value from a repeated field of unknown type %d",
  807. field_descriptor->cpp_type());
  808. }
  809. return result;
  810. }
  811. static PyObject* InternalGetRepeatedScalarSlice(
  812. CMessage* cmessage, const google::protobuf::FieldDescriptor* field_descriptor,
  813. PyObject* slice) {
  814. Py_ssize_t from;
  815. Py_ssize_t to;
  816. Py_ssize_t step;
  817. Py_ssize_t length;
  818. bool return_list = false;
  819. google::protobuf::Message* message = cmessage->message;
  820. if (PyInt_Check(slice)) {
  821. from = to = PyInt_AsLong(slice);
  822. } else if (PyLong_Check(slice)) {
  823. from = to = PyLong_AsLong(slice);
  824. } else if (PySlice_Check(slice)) {
  825. const google::protobuf::Reflection* reflection = message->GetReflection();
  826. length = reflection->FieldSize(*message, field_descriptor);
  827. PySlice_GetIndices(
  828. reinterpret_cast<PySliceObject*>(slice), length, &from, &to, &step);
  829. return_list = true;
  830. } else {
  831. PyErr_SetString(PyExc_TypeError, "list indices must be integers");
  832. return NULL;
  833. }
  834. if (!return_list) {
  835. return InternalGetRepeatedScalar(cmessage, field_descriptor, from);
  836. }
  837. PyObject* list = PyList_New(0);
  838. if (list == NULL) {
  839. return NULL;
  840. }
  841. if (from <= to) {
  842. if (step < 0) return list;
  843. for (Py_ssize_t index = from; index < to; index += step) {
  844. if (index < 0 || index >= length) break;
  845. PyObject* s = InternalGetRepeatedScalar(
  846. cmessage, field_descriptor, index);
  847. PyList_Append(list, s);
  848. Py_DECREF(s);
  849. }
  850. } else {
  851. if (step > 0) return list;
  852. for (Py_ssize_t index = from; index > to; index += step) {
  853. if (index < 0 || index >= length) break;
  854. PyObject* s = InternalGetRepeatedScalar(
  855. cmessage, field_descriptor, index);
  856. PyList_Append(list, s);
  857. Py_DECREF(s);
  858. }
  859. }
  860. return list;
  861. }
  862. // ------ C Constructor/Destructor:
  863. static int CMessageInit(CMessage* self, PyObject *args, PyObject *kwds) {
  864. self->message = NULL;
  865. return 0;
  866. }
  867. static void CMessageDealloc(CMessage* self) {
  868. if (self->free_message) {
  869. if (self->read_only) {
  870. PyErr_WriteUnraisable(reinterpret_cast<PyObject*>(self));
  871. }
  872. delete self->message;
  873. }
  874. self->ob_type->tp_free(reinterpret_cast<PyObject*>(self));
  875. }
  876. // ------ Methods:
  877. static PyObject* CMessage_Clear(CMessage* self, PyObject* args) {
  878. AssureWritable(self);
  879. self->message->Clear();
  880. Py_RETURN_NONE;
  881. }
  882. static PyObject* CMessage_IsInitialized(CMessage* self, PyObject* args) {
  883. return PyBool_FromLong(self->message->IsInitialized() ? 1 : 0);
  884. }
  885. static PyObject* CMessage_HasField(CMessage* self, PyObject* arg) {
  886. char* field_name;
  887. if (PyString_AsStringAndSize(arg, &field_name, NULL) < 0) {
  888. return NULL;
  889. }
  890. google::protobuf::Message* message = self->message;
  891. const google::protobuf::Descriptor* descriptor = message->GetDescriptor();
  892. const google::protobuf::FieldDescriptor* field_descriptor =
  893. descriptor->FindFieldByName(field_name);
  894. if (field_descriptor == NULL) {
  895. PyErr_Format(PyExc_ValueError, "Unknown field %s.", field_name);
  896. return NULL;
  897. }
  898. bool has_field =
  899. message->GetReflection()->HasField(*message, field_descriptor);
  900. return PyBool_FromLong(has_field ? 1 : 0);
  901. }
  902. static PyObject* CMessage_HasFieldByDescriptor(CMessage* self, PyObject* arg) {
  903. CFieldDescriptor* cfield_descriptor = NULL;
  904. if (!PyObject_TypeCheck(reinterpret_cast<PyObject *>(arg),
  905. &CFieldDescriptor_Type)) {
  906. PyErr_SetString(PyExc_TypeError, "Must be a field descriptor");
  907. return NULL;
  908. }
  909. cfield_descriptor = reinterpret_cast<CFieldDescriptor*>(arg);
  910. google::protobuf::Message* message = self->message;
  911. const google::protobuf::FieldDescriptor* field_descriptor =
  912. cfield_descriptor->descriptor;
  913. if (!FIELD_BELONGS_TO_MESSAGE(field_descriptor, message)) {
  914. PyErr_SetString(PyExc_KeyError,
  915. "Field does not belong to message!");
  916. return NULL;
  917. }
  918. if (FIELD_IS_REPEATED(field_descriptor)) {
  919. PyErr_SetString(PyExc_KeyError,
  920. "Field is repeated. A singular method is required.");
  921. return NULL;
  922. }
  923. bool has_field =
  924. message->GetReflection()->HasField(*message, field_descriptor);
  925. return PyBool_FromLong(has_field ? 1 : 0);
  926. }
  927. static PyObject* CMessage_ClearFieldByDescriptor(
  928. CMessage* self, PyObject* arg) {
  929. CFieldDescriptor* cfield_descriptor = NULL;
  930. if (!PyObject_TypeCheck(reinterpret_cast<PyObject *>(arg),
  931. &CFieldDescriptor_Type)) {
  932. PyErr_SetString(PyExc_TypeError, "Must be a field descriptor");
  933. return NULL;
  934. }
  935. cfield_descriptor = reinterpret_cast<CFieldDescriptor*>(arg);
  936. google::protobuf::Message* message = self->message;
  937. const google::protobuf::FieldDescriptor* field_descriptor =
  938. cfield_descriptor->descriptor;
  939. if (!FIELD_BELONGS_TO_MESSAGE(field_descriptor, message)) {
  940. PyErr_SetString(PyExc_KeyError,
  941. "Field does not belong to message!");
  942. return NULL;
  943. }
  944. message->GetReflection()->ClearField(message, field_descriptor);
  945. Py_RETURN_NONE;
  946. }
  947. static PyObject* CMessage_ClearField(CMessage* self, PyObject* arg) {
  948. char* field_name;
  949. if (PyString_AsStringAndSize(arg, &field_name, NULL) < 0) {
  950. return NULL;
  951. }
  952. google::protobuf::Message* message = self->message;
  953. const google::protobuf::Descriptor* descriptor = message->GetDescriptor();
  954. const google::protobuf::FieldDescriptor* field_descriptor =
  955. descriptor->FindFieldByName(field_name);
  956. if (field_descriptor == NULL) {
  957. PyErr_Format(PyExc_ValueError, "Unknown field %s.", field_name);
  958. return NULL;
  959. }
  960. message->GetReflection()->ClearField(message, field_descriptor);
  961. Py_RETURN_NONE;
  962. }
  963. static PyObject* CMessage_GetScalar(CMessage* self, PyObject* arg) {
  964. CFieldDescriptor* cdescriptor = NULL;
  965. if (!PyObject_TypeCheck(reinterpret_cast<PyObject *>(arg),
  966. &CFieldDescriptor_Type)) {
  967. PyErr_SetString(PyExc_TypeError, "Must be a field descriptor");
  968. return NULL;
  969. }
  970. cdescriptor = reinterpret_cast<CFieldDescriptor*>(arg);
  971. google::protobuf::Message* message = self->message;
  972. return InternalGetScalar(message, cdescriptor->descriptor);
  973. }
  974. static PyObject* CMessage_GetRepeatedScalar(CMessage* self, PyObject* args) {
  975. CFieldDescriptor* cfield_descriptor;
  976. PyObject* slice;
  977. if (!PyArg_ParseTuple(args, C("O!O:GetRepeatedScalar"),
  978. &CFieldDescriptor_Type, &cfield_descriptor, &slice)) {
  979. return NULL;
  980. }
  981. return InternalGetRepeatedScalarSlice(
  982. self, cfield_descriptor->descriptor, slice);
  983. }
  984. static PyObject* CMessage_AssignRepeatedScalar(CMessage* self, PyObject* args) {
  985. CFieldDescriptor* cfield_descriptor;
  986. PyObject* slice;
  987. if (!PyArg_ParseTuple(args, C("O!O:AssignRepeatedScalar"),
  988. &CFieldDescriptor_Type, &cfield_descriptor, &slice)) {
  989. return NULL;
  990. }
  991. AssureWritable(self);
  992. google::protobuf::Message* message = self->message;
  993. message->GetReflection()->ClearField(message, cfield_descriptor->descriptor);
  994. PyObject* iter = PyObject_GetIter(slice);
  995. PyObject* next;
  996. while ((next = PyIter_Next(iter)) != NULL) {
  997. if (InternalAddRepeatedScalar(
  998. message, cfield_descriptor->descriptor, next) == NULL) {
  999. Py_DECREF(next);
  1000. Py_DECREF(iter);
  1001. return NULL;
  1002. }
  1003. Py_DECREF(next);
  1004. }
  1005. Py_DECREF(iter);
  1006. Py_RETURN_NONE;
  1007. }
  1008. static PyObject* CMessage_DeleteRepeatedField(CMessage* self, PyObject* args) {
  1009. CFieldDescriptor* cfield_descriptor;
  1010. PyObject* slice;
  1011. if (!PyArg_ParseTuple(args, C("O!O:DeleteRepeatedField"),
  1012. &CFieldDescriptor_Type, &cfield_descriptor, &slice)) {
  1013. return NULL;
  1014. }
  1015. AssureWritable(self);
  1016. Py_ssize_t length, from, to, step, slice_length;
  1017. google::protobuf::Message* message = self->message;
  1018. const google::protobuf::FieldDescriptor* field_descriptor =
  1019. cfield_descriptor->descriptor;
  1020. const google::protobuf::Reflection* reflection = message->GetReflection();
  1021. int min, max;
  1022. length = reflection->FieldSize(*message, field_descriptor);
  1023. if (PyInt_Check(slice) || PyLong_Check(slice)) {
  1024. from = to = PyLong_AsLong(slice);
  1025. if (from < 0) {
  1026. from = to = length + from;
  1027. }
  1028. step = 1;
  1029. min = max = from;
  1030. // Range check.
  1031. if (from < 0 || from >= length) {
  1032. PyErr_Format(PyExc_IndexError, "list assignment index out of range");
  1033. return NULL;
  1034. }
  1035. } else if (PySlice_Check(slice)) {
  1036. from = to = step = slice_length = 0;
  1037. PySlice_GetIndicesEx(
  1038. reinterpret_cast<PySliceObject*>(slice),
  1039. length, &from, &to, &step, &slice_length);
  1040. if (from < to) {
  1041. min = from;
  1042. max = to - 1;
  1043. } else {
  1044. min = to + 1;
  1045. max = from;
  1046. }
  1047. } else {
  1048. PyErr_SetString(PyExc_TypeError, "list indices must be integers");
  1049. return NULL;
  1050. }
  1051. Py_ssize_t i = from;
  1052. std::vector<bool> to_delete(length, false);
  1053. while (i >= min && i <= max) {
  1054. to_delete[i] = true;
  1055. i += step;
  1056. }
  1057. to = 0;
  1058. for (i = 0; i < length; ++i) {
  1059. if (!to_delete[i]) {
  1060. if (i != to) {
  1061. reflection->SwapElements(message, field_descriptor, i, to);
  1062. }
  1063. ++to;
  1064. }
  1065. }
  1066. while (i > to) {
  1067. reflection->RemoveLast(message, field_descriptor);
  1068. --i;
  1069. }
  1070. Py_RETURN_NONE;
  1071. }
  1072. static PyObject* CMessage_SetScalar(CMessage* self, PyObject* args) {
  1073. CFieldDescriptor* cfield_descriptor;
  1074. PyObject* arg;
  1075. if (!PyArg_ParseTuple(args, C("O!O:SetScalar"),
  1076. &CFieldDescriptor_Type, &cfield_descriptor, &arg)) {
  1077. return NULL;
  1078. }
  1079. AssureWritable(self);
  1080. return InternalSetScalar(self->message, cfield_descriptor->descriptor, arg);
  1081. }
  1082. static PyObject* CMessage_AddRepeatedScalar(CMessage* self, PyObject* args) {
  1083. CFieldDescriptor* cfield_descriptor;
  1084. PyObject* value;
  1085. if (!PyArg_ParseTuple(args, C("O!O:AddRepeatedScalar"),
  1086. &CFieldDescriptor_Type, &cfield_descriptor, &value)) {
  1087. return NULL;
  1088. }
  1089. AssureWritable(self);
  1090. return InternalAddRepeatedScalar(
  1091. self->message, cfield_descriptor->descriptor, value);
  1092. }
  1093. static PyObject* CMessage_FieldLength(CMessage* self, PyObject* arg) {
  1094. CFieldDescriptor* cfield_descriptor;
  1095. if (!PyObject_TypeCheck(reinterpret_cast<PyObject *>(arg),
  1096. &CFieldDescriptor_Type)) {
  1097. PyErr_SetString(PyExc_TypeError, "Must be a field descriptor");
  1098. return NULL;
  1099. }
  1100. cfield_descriptor = reinterpret_cast<CFieldDescriptor*>(arg);
  1101. google::protobuf::Message* message = self->message;
  1102. int length = message->GetReflection()->FieldSize(
  1103. *message, cfield_descriptor->descriptor);
  1104. return PyInt_FromLong(length);
  1105. }
  1106. static PyObject* CMessage_DebugString(CMessage* self, PyObject* args) {
  1107. return PyString_FromString(self->message->DebugString().c_str());
  1108. }
  1109. static PyObject* CMessage_SerializeToString(CMessage* self, PyObject* args) {
  1110. int size = self->message->ByteSize();
  1111. if (size <= 0) {
  1112. return PyString_FromString("");
  1113. }
  1114. PyObject* result = PyString_FromStringAndSize(NULL, size);
  1115. if (result == NULL) {
  1116. return NULL;
  1117. }
  1118. char* buffer = PyString_AS_STRING(result);
  1119. self->message->SerializeWithCachedSizesToArray(
  1120. reinterpret_cast<uint8*>(buffer));
  1121. return result;
  1122. }
  1123. static PyObject* CMessage_SerializePartialToString(
  1124. CMessage* self, PyObject* args) {
  1125. string contents;
  1126. self->message->SerializePartialToString(&contents);
  1127. return PyString_FromStringAndSize(contents.c_str(), contents.size());
  1128. }
  1129. static PyObject* CMessageStr(CMessage* self) {
  1130. char str[1024];
  1131. str[sizeof(str) - 1] = 0;
  1132. snprintf(str, sizeof(str) - 1, "CMessage: <%p>", self->message);
  1133. return PyString_FromString(str);
  1134. }
  1135. static PyObject* CMessage_MergeFrom(CMessage* self, PyObject* arg) {
  1136. CMessage* other_message;
  1137. if (!PyObject_TypeCheck(reinterpret_cast<PyObject *>(arg), &CMessage_Type)) {
  1138. PyErr_SetString(PyExc_TypeError, "Must be a message");
  1139. return NULL;
  1140. }
  1141. other_message = reinterpret_cast<CMessage*>(arg);
  1142. if (other_message->message->GetDescriptor() !=
  1143. self->message->GetDescriptor()) {
  1144. PyErr_Format(PyExc_TypeError,
  1145. "Tried to merge from a message with a different type. "
  1146. "to: %s, from: %s",
  1147. self->message->GetDescriptor()->full_name().c_str(),
  1148. other_message->message->GetDescriptor()->full_name().c_str());
  1149. return NULL;
  1150. }
  1151. AssureWritable(self);
  1152. self->message->MergeFrom(*other_message->message);
  1153. Py_RETURN_NONE;
  1154. }
  1155. static PyObject* CMessage_CopyFrom(CMessage* self, PyObject* arg) {
  1156. CMessage* other_message;
  1157. if (!PyObject_TypeCheck(reinterpret_cast<PyObject *>(arg), &CMessage_Type)) {
  1158. PyErr_SetString(PyExc_TypeError, "Must be a message");
  1159. return NULL;
  1160. }
  1161. other_message = reinterpret_cast<CMessage*>(arg);
  1162. if (other_message->message->GetDescriptor() !=
  1163. self->message->GetDescriptor()) {
  1164. PyErr_Format(PyExc_TypeError,
  1165. "Tried to copy from a message with a different type. "
  1166. "to: %s, from: %s",
  1167. self->message->GetDescriptor()->full_name().c_str(),
  1168. other_message->message->GetDescriptor()->full_name().c_str());
  1169. return NULL;
  1170. }
  1171. AssureWritable(self);
  1172. self->message->CopyFrom(*other_message->message);
  1173. Py_RETURN_NONE;
  1174. }
  1175. static PyObject* CMessage_MergeFromString(CMessage* self, PyObject* arg) {
  1176. const void* data;
  1177. Py_ssize_t data_length;
  1178. if (PyObject_AsReadBuffer(arg, &data, &data_length) < 0) {
  1179. return NULL;
  1180. }
  1181. AssureWritable(self);
  1182. google::protobuf::io::CodedInputStream input(
  1183. reinterpret_cast<const uint8*>(data), data_length);
  1184. bool success = self->message->MergePartialFromCodedStream(&input);
  1185. if (success) {
  1186. return PyInt_FromLong(self->message->ByteSize());
  1187. } else {
  1188. return PyInt_FromLong(-1);
  1189. }
  1190. }
  1191. static PyObject* CMessage_ByteSize(CMessage* self, PyObject* args) {
  1192. return PyLong_FromLong(self->message->ByteSize());
  1193. }
  1194. static PyObject* CMessage_SetInParent(CMessage* self, PyObject* args) {
  1195. AssureWritable(self);
  1196. Py_RETURN_NONE;
  1197. }
  1198. static PyObject* CMessage_SwapRepeatedFieldElements(
  1199. CMessage* self, PyObject* args) {
  1200. CFieldDescriptor* cfield_descriptor;
  1201. int index1, index2;
  1202. if (!PyArg_ParseTuple(args, C("O!ii:SwapRepeatedFieldElements"),
  1203. &CFieldDescriptor_Type, &cfield_descriptor,
  1204. &index1, &index2)) {
  1205. return NULL;
  1206. }
  1207. google::protobuf::Message* message = self->message;
  1208. const google::protobuf::Reflection* reflection = message->GetReflection();
  1209. reflection->SwapElements(
  1210. message, cfield_descriptor->descriptor, index1, index2);
  1211. Py_RETURN_NONE;
  1212. }
  1213. static PyObject* CMessage_AddMessage(CMessage* self, PyObject* arg) {
  1214. CFieldDescriptor* cfield_descriptor;
  1215. if (!PyObject_TypeCheck(reinterpret_cast<PyObject *>(arg),
  1216. &CFieldDescriptor_Type)) {
  1217. PyErr_SetString(PyExc_TypeError, "Must be a field descriptor");
  1218. return NULL;
  1219. }
  1220. cfield_descriptor = reinterpret_cast<CFieldDescriptor*>(arg);
  1221. AssureWritable(self);
  1222. CMessage* py_cmsg = PyObject_New(CMessage, &CMessage_Type);
  1223. if (py_cmsg == NULL) {
  1224. return NULL;
  1225. }
  1226. google::protobuf::Message* message = self->message;
  1227. const google::protobuf::Reflection* reflection = message->GetReflection();
  1228. google::protobuf::Message* sub_message =
  1229. reflection->AddMessage(message, cfield_descriptor->descriptor);
  1230. py_cmsg->parent = NULL;
  1231. py_cmsg->full_name = sub_message->GetDescriptor()->full_name().c_str();
  1232. py_cmsg->message = sub_message;
  1233. py_cmsg->free_message = false;
  1234. py_cmsg->read_only = false;
  1235. return reinterpret_cast<PyObject*>(py_cmsg);
  1236. }
  1237. static PyObject* CMessage_GetRepeatedMessage(CMessage* self, PyObject* args) {
  1238. CFieldDescriptor* cfield_descriptor;
  1239. PyObject* slice;
  1240. if (!PyArg_ParseTuple(args, C("O!O:GetRepeatedMessage"),
  1241. &CFieldDescriptor_Type, &cfield_descriptor, &slice)) {
  1242. return NULL;
  1243. }
  1244. return InternalGetRepeatedScalarSlice(
  1245. self, cfield_descriptor->descriptor, slice);
  1246. }
  1247. static PyObject* CMessage_NewSubMessage(CMessage* self, PyObject* arg) {
  1248. CFieldDescriptor* cfield_descriptor;
  1249. if (!PyObject_TypeCheck(reinterpret_cast<PyObject *>(arg),
  1250. &CFieldDescriptor_Type)) {
  1251. PyErr_SetString(PyExc_TypeError, "Must be a field descriptor");
  1252. return NULL;
  1253. }
  1254. cfield_descriptor = reinterpret_cast<CFieldDescriptor*>(arg);
  1255. CMessage* py_cmsg = PyObject_New(CMessage, &CMessage_Type);
  1256. if (py_cmsg == NULL) {
  1257. return NULL;
  1258. }
  1259. google::protobuf::Message* message = self->message;
  1260. const google::protobuf::Reflection* reflection = message->GetReflection();
  1261. const google::protobuf::Message& sub_message =
  1262. reflection->GetMessage(*message, cfield_descriptor->descriptor,
  1263. global_message_factory);
  1264. py_cmsg->full_name = sub_message.GetDescriptor()->full_name().c_str();
  1265. py_cmsg->parent = self;
  1266. py_cmsg->parent_field = cfield_descriptor;
  1267. py_cmsg->message = const_cast<google::protobuf::Message*>(&sub_message);
  1268. py_cmsg->free_message = false;
  1269. py_cmsg->read_only = true;
  1270. return reinterpret_cast<PyObject*>(py_cmsg);
  1271. }
  1272. static PyObject* CMessage_MutableMessage(CMessage* self, PyObject* arg) {
  1273. CFieldDescriptor* cfield_descriptor;
  1274. if (!PyObject_TypeCheck(reinterpret_cast<PyObject *>(arg),
  1275. &CFieldDescriptor_Type)) {
  1276. PyErr_SetString(PyExc_TypeError, "Must be a field descriptor");
  1277. return NULL;
  1278. }
  1279. cfield_descriptor = reinterpret_cast<CFieldDescriptor*>(arg);
  1280. AssureWritable(self);
  1281. CMessage* py_cmsg = PyObject_New(CMessage, &CMessage_Type);
  1282. if (py_cmsg == NULL) {
  1283. return NULL;
  1284. }
  1285. google::protobuf::Message* message = self->message;
  1286. const google::protobuf::Reflection* reflection = message->GetReflection();
  1287. google::protobuf::Message* mutable_message =
  1288. reflection->MutableMessage(message, cfield_descriptor->descriptor,
  1289. global_message_factory);
  1290. py_cmsg->full_name = mutable_message->GetDescriptor()->full_name().c_str();
  1291. py_cmsg->message = mutable_message;
  1292. py_cmsg->free_message = false;
  1293. py_cmsg->read_only = false;
  1294. return reinterpret_cast<PyObject*>(py_cmsg);
  1295. }
  1296. static PyObject* CMessage_Equals(CMessage* self, PyObject* arg) {
  1297. CMessage* other_message;
  1298. if (!PyObject_TypeCheck(reinterpret_cast<PyObject *>(arg), &CMessage_Type)) {
  1299. PyErr_SetString(PyExc_TypeError, "Must be a message");
  1300. return NULL;
  1301. }
  1302. other_message = reinterpret_cast<CMessage*>(arg);
  1303. if (other_message->message == self->message) {
  1304. return PyBool_FromLong(1);
  1305. }
  1306. if (other_message->message->GetDescriptor() !=
  1307. self->message->GetDescriptor()) {
  1308. return PyBool_FromLong(0);
  1309. }
  1310. return PyBool_FromLong(1);
  1311. }
  1312. static PyObject* CMessage_ListFields(CMessage* self, PyObject* args) {
  1313. google::protobuf::Message* message = self->message;
  1314. const google::protobuf::Reflection* reflection = message->GetReflection();
  1315. vector<const google::protobuf::FieldDescriptor*> fields;
  1316. reflection->ListFields(*message, &fields);
  1317. PyObject* list = PyList_New(fields.size());
  1318. if (list == NULL) {
  1319. return NULL;
  1320. }
  1321. for (unsigned int i = 0; i < fields.size(); ++i) {
  1322. bool is_extension = fields[i]->is_extension();
  1323. PyObject* t = PyTuple_New(2);
  1324. if (t == NULL) {
  1325. Py_DECREF(list);
  1326. return NULL;
  1327. }
  1328. PyObject* is_extension_object = PyBool_FromLong(is_extension ? 1 : 0);
  1329. PyObject* field_name;
  1330. const string* s;
  1331. if (is_extension) {
  1332. s = &fields[i]->full_name();
  1333. } else {
  1334. s = &fields[i]->name();
  1335. }
  1336. field_name = PyString_FromStringAndSize(s->c_str(), s->length());
  1337. if (field_name == NULL) {
  1338. Py_DECREF(list);
  1339. Py_DECREF(t);
  1340. return NULL;
  1341. }
  1342. PyTuple_SET_ITEM(t, 0, is_extension_object);
  1343. PyTuple_SET_ITEM(t, 1, field_name);
  1344. PyList_SET_ITEM(list, i, t);
  1345. }
  1346. return list;
  1347. }
  1348. static PyObject* CMessage_FindInitializationErrors(CMessage* self) {
  1349. google::protobuf::Message* message = self->message;
  1350. vector<string> errors;
  1351. message->FindInitializationErrors(&errors);
  1352. PyObject* error_list = PyList_New(errors.size());
  1353. if (error_list == NULL) {
  1354. return NULL;
  1355. }
  1356. for (unsigned int i = 0; i < errors.size(); ++i) {
  1357. const string& error = errors[i];
  1358. PyObject* error_string = PyString_FromStringAndSize(
  1359. error.c_str(), error.length());
  1360. if (error_string == NULL) {
  1361. Py_DECREF(error_list);
  1362. return NULL;
  1363. }
  1364. PyList_SET_ITEM(error_list, i, error_string);
  1365. }
  1366. return error_list;
  1367. }
  1368. // ------ Python Constructor:
  1369. PyObject* Python_NewCMessage(PyObject* ignored, PyObject* arg) {
  1370. const char* message_type = PyString_AsString(arg);
  1371. if (message_type == NULL) {
  1372. return NULL;
  1373. }
  1374. const google::protobuf::Message* message = CreateMessage(message_type);
  1375. if (message == NULL) {
  1376. PyErr_Format(PyExc_TypeError, "Couldn't create message of type %s!",
  1377. message_type);
  1378. return NULL;
  1379. }
  1380. CMessage* py_cmsg = PyObject_New(CMessage, &CMessage_Type);
  1381. if (py_cmsg == NULL) {
  1382. return NULL;
  1383. }
  1384. py_cmsg->message = message->New();
  1385. py_cmsg->free_message = true;
  1386. py_cmsg->full_name = message->GetDescriptor()->full_name().c_str();
  1387. py_cmsg->read_only = false;
  1388. py_cmsg->parent = NULL;
  1389. py_cmsg->parent_field = NULL;
  1390. return reinterpret_cast<PyObject*>(py_cmsg);
  1391. }
  1392. // --- Module Functions (exposed to Python):
  1393. PyMethodDef methods[] = {
  1394. { C("NewCMessage"), (PyCFunction)Python_NewCMessage,
  1395. METH_O,
  1396. C("Creates a new C++ protocol message, given its full name.") },
  1397. { C("NewCDescriptorPool"), (PyCFunction)Python_NewCDescriptorPool,
  1398. METH_NOARGS,
  1399. C("Creates a new C++ descriptor pool.") },
  1400. { C("BuildFile"), (PyCFunction)Python_BuildFile,
  1401. METH_O,
  1402. C("Registers a new protocol buffer file in the global C++ descriptor "
  1403. "pool.") },
  1404. {NULL}
  1405. };
  1406. // --- Exposing the C proto living inside Python proto to C code:
  1407. extern const Message* (*GetCProtoInsidePyProtoPtr)(PyObject* msg);
  1408. extern Message* (*MutableCProtoInsidePyProtoPtr)(PyObject* msg);
  1409. static const google::protobuf::Message* GetCProtoInsidePyProtoImpl(PyObject* msg) {
  1410. PyObject* c_msg_obj = PyObject_GetAttrString(msg, "_cmsg");
  1411. if (c_msg_obj == NULL) {
  1412. PyErr_Clear();
  1413. return NULL;
  1414. }
  1415. Py_DECREF(c_msg_obj);
  1416. if (!PyObject_TypeCheck(c_msg_obj, &CMessage_Type)) {
  1417. return NULL;
  1418. }
  1419. CMessage* c_msg = reinterpret_cast<CMessage*>(c_msg_obj);
  1420. return c_msg->message;
  1421. }
  1422. static google::protobuf::Message* MutableCProtoInsidePyProtoImpl(PyObject* msg) {
  1423. PyObject* c_msg_obj = PyObject_GetAttrString(msg, "_cmsg");
  1424. if (c_msg_obj == NULL) {
  1425. PyErr_Clear();
  1426. return NULL;
  1427. }
  1428. Py_DECREF(c_msg_obj);
  1429. if (!PyObject_TypeCheck(c_msg_obj, &CMessage_Type)) {
  1430. return NULL;
  1431. }
  1432. CMessage* c_msg = reinterpret_cast<CMessage*>(c_msg_obj);
  1433. AssureWritable(c_msg);
  1434. return c_msg->message;
  1435. }
  1436. // --- Module Init Function:
  1437. static const char module_docstring[] =
  1438. "python-proto2 is a module that can be used to enhance proto2 Python API\n"
  1439. "performance.\n"
  1440. "\n"
  1441. "It provides access to the protocol buffers C++ reflection API that\n"
  1442. "implements the basic protocol buffer functions.";
  1443. extern "C" {
  1444. void init_net_proto2___python() {
  1445. // Initialize constants.
  1446. kPythonZero = PyInt_FromLong(0);
  1447. kint32min_py = PyInt_FromLong(kint32min);
  1448. kint32max_py = PyInt_FromLong(kint32max);
  1449. kuint32max_py = PyLong_FromLongLong(kuint32max);
  1450. kint64min_py = PyLong_FromLongLong(kint64min);
  1451. kint64max_py = PyLong_FromLongLong(kint64max);
  1452. kuint64max_py = PyLong_FromUnsignedLongLong(kuint64max);
  1453. global_message_factory = new DynamicMessageFactory(GetDescriptorPool());
  1454. global_message_factory->SetDelegateToGeneratedFactory(true);
  1455. // Export our functions to Python.
  1456. PyObject *m;
  1457. m = Py_InitModule3(C("_net_proto2___python"), methods, C(module_docstring));
  1458. if (m == NULL) {
  1459. return;
  1460. }
  1461. AddConstants(m);
  1462. CMessage_Type.tp_new = PyType_GenericNew;
  1463. if (PyType_Ready(&CMessage_Type) < 0) {
  1464. return;
  1465. }
  1466. if (!InitDescriptor()) {
  1467. return;
  1468. }
  1469. // Override {Get,Mutable}CProtoInsidePyProto.
  1470. GetCProtoInsidePyProtoPtr = GetCProtoInsidePyProtoImpl;
  1471. MutableCProtoInsidePyProtoPtr = MutableCProtoInsidePyProtoImpl;
  1472. }
  1473. }
  1474. } // namespace python
  1475. } // namespace protobuf
  1476. } // namespace google