Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1717 lines
58 KiB

  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: [email protected] (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_O,
  181. "Clears a protocol message."),
  182. CMETHOD(ClearField, METH_VARARGS,
  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("google.protobuf.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* repr = PyObject_Repr(arg);
  288. PyErr_Format(PyExc_TypeError,
  289. "%.100s has type %.100s, but expected one of: %s",
  290. PyString_AS_STRING(repr),
  291. arg->ob_type->tp_name,
  292. expected_types);
  293. Py_DECREF(repr);
  294. }
  295. template <class T>
  296. static bool CheckAndGetInteger(
  297. PyObject* arg, T* value, PyObject* min, PyObject* max) {
  298. bool is_long = PyLong_Check(arg);
  299. if (!PyInt_Check(arg) && !is_long) {
  300. FormatTypeError(arg, "int, long");
  301. return false;
  302. }
  303. if (PyObject_Compare(min, arg) > 0 || PyObject_Compare(max, arg) < 0) {
  304. PyObject* s = PyObject_Str(arg);
  305. PyErr_Format(PyExc_ValueError,
  306. "Value out of range: %s",
  307. PyString_AS_STRING(s));
  308. Py_DECREF(s);
  309. return false;
  310. }
  311. if (is_long) {
  312. if (min == kPythonZero) {
  313. *value = static_cast<T>(PyLong_AsUnsignedLongLong(arg));
  314. } else {
  315. *value = static_cast<T>(PyLong_AsLongLong(arg));
  316. }
  317. } else {
  318. *value = static_cast<T>(PyInt_AsLong(arg));
  319. }
  320. return true;
  321. }
  322. static bool CheckAndGetDouble(PyObject* arg, double* value) {
  323. if (!PyInt_Check(arg) && !PyLong_Check(arg) &&
  324. !PyFloat_Check(arg)) {
  325. FormatTypeError(arg, "int, long, float");
  326. return false;
  327. }
  328. *value = PyFloat_AsDouble(arg);
  329. return true;
  330. }
  331. static bool CheckAndGetFloat(PyObject* arg, float* value) {
  332. double double_value;
  333. if (!CheckAndGetDouble(arg, &double_value)) {
  334. return false;
  335. }
  336. *value = static_cast<float>(double_value);
  337. return true;
  338. }
  339. static bool CheckAndGetBool(PyObject* arg, bool* value) {
  340. if (!PyInt_Check(arg) && !PyBool_Check(arg) && !PyLong_Check(arg)) {
  341. FormatTypeError(arg, "int, long, bool");
  342. return false;
  343. }
  344. *value = static_cast<bool>(PyInt_AsLong(arg));
  345. return true;
  346. }
  347. google::protobuf::DynamicMessageFactory* global_message_factory = NULL;
  348. static const google::protobuf::Message* CreateMessage(const char* message_type) {
  349. string message_name(message_type);
  350. const google::protobuf::Descriptor* descriptor =
  351. GetDescriptorPool()->FindMessageTypeByName(message_name);
  352. if (descriptor == NULL) {
  353. return NULL;
  354. }
  355. return global_message_factory->GetPrototype(descriptor);
  356. }
  357. static void ReleaseSubMessage(google::protobuf::Message* message,
  358. const google::protobuf::FieldDescriptor* field_descriptor,
  359. CMessage* child_cmessage) {
  360. Message* released_message = message->GetReflection()->ReleaseMessage(
  361. message, field_descriptor, global_message_factory);
  362. GOOGLE_DCHECK(child_cmessage->message != NULL);
  363. // ReleaseMessage will return NULL which differs from
  364. // child_cmessage->message, if the field does not exist. In this case,
  365. // the latter points to the default instance via a const_cast<>, so we
  366. // have to reset it to a new mutable object since we are taking ownership.
  367. if (released_message == NULL) {
  368. const Message* prototype = global_message_factory->GetPrototype(
  369. child_cmessage->message->GetDescriptor());
  370. GOOGLE_DCHECK(prototype != NULL);
  371. child_cmessage->message = prototype->New();
  372. }
  373. child_cmessage->parent = NULL;
  374. child_cmessage->parent_field = NULL;
  375. child_cmessage->free_message = true;
  376. child_cmessage->read_only = false;
  377. }
  378. static bool CheckAndSetString(
  379. PyObject* arg, google::protobuf::Message* message,
  380. const google::protobuf::FieldDescriptor* descriptor,
  381. const google::protobuf::Reflection* reflection,
  382. bool append,
  383. int index) {
  384. GOOGLE_DCHECK(descriptor->type() == google::protobuf::FieldDescriptor::TYPE_STRING ||
  385. descriptor->type() == google::protobuf::FieldDescriptor::TYPE_BYTES);
  386. if (descriptor->type() == google::protobuf::FieldDescriptor::TYPE_STRING) {
  387. if (!PyString_Check(arg) && !PyUnicode_Check(arg)) {
  388. FormatTypeError(arg, "str, unicode");
  389. return false;
  390. }
  391. if (PyString_Check(arg)) {
  392. PyObject* unicode = PyUnicode_FromEncodedObject(arg, "ascii", NULL);
  393. if (unicode == NULL) {
  394. PyObject* repr = PyObject_Repr(arg);
  395. PyErr_Format(PyExc_ValueError,
  396. "%s has type str, but isn't in 7-bit ASCII "
  397. "encoding. Non-ASCII strings must be converted to "
  398. "unicode objects before being added.",
  399. PyString_AS_STRING(repr));
  400. Py_DECREF(repr);
  401. return false;
  402. } else {
  403. Py_DECREF(unicode);
  404. }
  405. }
  406. } else if (!PyString_Check(arg)) {
  407. FormatTypeError(arg, "str");
  408. return false;
  409. }
  410. PyObject* encoded_string = NULL;
  411. if (descriptor->type() == google::protobuf::FieldDescriptor::TYPE_STRING) {
  412. if (PyString_Check(arg)) {
  413. encoded_string = PyString_AsEncodedObject(arg, "utf-8", NULL);
  414. } else {
  415. encoded_string = PyUnicode_AsEncodedObject(arg, "utf-8", NULL);
  416. }
  417. } else {
  418. // In this case field type is "bytes".
  419. encoded_string = arg;
  420. Py_INCREF(encoded_string);
  421. }
  422. if (encoded_string == NULL) {
  423. return false;
  424. }
  425. char* value;
  426. Py_ssize_t value_len;
  427. if (PyString_AsStringAndSize(encoded_string, &value, &value_len) < 0) {
  428. Py_DECREF(encoded_string);
  429. return false;
  430. }
  431. string value_string(value, value_len);
  432. if (append) {
  433. reflection->AddString(message, descriptor, value_string);
  434. } else if (index < 0) {
  435. reflection->SetString(message, descriptor, value_string);
  436. } else {
  437. reflection->SetRepeatedString(message, descriptor, index, value_string);
  438. }
  439. Py_DECREF(encoded_string);
  440. return true;
  441. }
  442. static PyObject* ToStringObject(
  443. const google::protobuf::FieldDescriptor* descriptor, string value) {
  444. if (descriptor->type() != google::protobuf::FieldDescriptor::TYPE_STRING) {
  445. return PyString_FromStringAndSize(value.c_str(), value.length());
  446. }
  447. PyObject* result = PyUnicode_DecodeUTF8(value.c_str(), value.length(), NULL);
  448. // If the string can't be decoded in UTF-8, just return a string object that
  449. // contains the raw bytes. This can't happen if the value was assigned using
  450. // the members of the Python message object, but can happen if the values were
  451. // parsed from the wire (binary).
  452. if (result == NULL) {
  453. PyErr_Clear();
  454. result = PyString_FromStringAndSize(value.c_str(), value.length());
  455. }
  456. return result;
  457. }
  458. static void AssureWritable(CMessage* self) {
  459. if (self == NULL ||
  460. self->parent == NULL ||
  461. self->parent_field == NULL) {
  462. return;
  463. }
  464. if (!self->read_only) {
  465. return;
  466. }
  467. AssureWritable(self->parent);
  468. google::protobuf::Message* message = self->parent->message;
  469. const google::protobuf::Reflection* reflection = message->GetReflection();
  470. self->message = reflection->MutableMessage(
  471. message, self->parent_field->descriptor, global_message_factory);
  472. self->read_only = false;
  473. }
  474. static PyObject* InternalGetScalar(
  475. google::protobuf::Message* message,
  476. const google::protobuf::FieldDescriptor* field_descriptor) {
  477. const google::protobuf::Reflection* reflection = message->GetReflection();
  478. if (!FIELD_BELONGS_TO_MESSAGE(field_descriptor, message)) {
  479. PyErr_SetString(
  480. PyExc_KeyError, "Field does not belong to message!");
  481. return NULL;
  482. }
  483. PyObject* result = NULL;
  484. switch (field_descriptor->cpp_type()) {
  485. case google::protobuf::FieldDescriptor::CPPTYPE_INT32: {
  486. int32 value = reflection->GetInt32(*message, field_descriptor);
  487. result = PyInt_FromLong(value);
  488. break;
  489. }
  490. case google::protobuf::FieldDescriptor::CPPTYPE_INT64: {
  491. int64 value = reflection->GetInt64(*message, field_descriptor);
  492. #if IS_64BIT
  493. result = PyInt_FromLong(value);
  494. #else
  495. result = PyLong_FromLongLong(value);
  496. #endif
  497. break;
  498. }
  499. case google::protobuf::FieldDescriptor::CPPTYPE_UINT32: {
  500. uint32 value = reflection->GetUInt32(*message, field_descriptor);
  501. #if IS_64BIT
  502. result = PyInt_FromLong(value);
  503. #else
  504. result = PyLong_FromLongLong(value);
  505. #endif
  506. break;
  507. }
  508. case google::protobuf::FieldDescriptor::CPPTYPE_UINT64: {
  509. uint64 value = reflection->GetUInt64(*message, field_descriptor);
  510. #if IS_64BIT
  511. if (value <= static_cast<uint64>(kint64max)) {
  512. result = PyInt_FromLong(static_cast<uint64>(value));
  513. }
  514. #else
  515. if (value <= static_cast<uint32>(kint32max)) {
  516. result = PyInt_FromLong(static_cast<uint32>(value));
  517. }
  518. #endif
  519. else { // NOLINT
  520. result = PyLong_FromUnsignedLongLong(value);
  521. }
  522. break;
  523. }
  524. case google::protobuf::FieldDescriptor::CPPTYPE_FLOAT: {
  525. float value = reflection->GetFloat(*message, field_descriptor);
  526. result = PyFloat_FromDouble(value);
  527. break;
  528. }
  529. case google::protobuf::FieldDescriptor::CPPTYPE_DOUBLE: {
  530. double value = reflection->GetDouble(*message, field_descriptor);
  531. result = PyFloat_FromDouble(value);
  532. break;
  533. }
  534. case google::protobuf::FieldDescriptor::CPPTYPE_BOOL: {
  535. bool value = reflection->GetBool(*message, field_descriptor);
  536. result = PyBool_FromLong(value);
  537. break;
  538. }
  539. case google::protobuf::FieldDescriptor::CPPTYPE_STRING: {
  540. string value = reflection->GetString(*message, field_descriptor);
  541. result = ToStringObject(field_descriptor, value);
  542. break;
  543. }
  544. case google::protobuf::FieldDescriptor::CPPTYPE_ENUM: {
  545. if (!message->GetReflection()->HasField(*message, field_descriptor)) {
  546. // Look for the value in the unknown fields.
  547. google::protobuf::UnknownFieldSet* unknown_field_set =
  548. message->GetReflection()->MutableUnknownFields(message);
  549. for (int i = 0; i < unknown_field_set->field_count(); ++i) {
  550. if (unknown_field_set->field(i).number() ==
  551. field_descriptor->number()) {
  552. result = PyInt_FromLong(unknown_field_set->field(i).varint());
  553. break;
  554. }
  555. }
  556. }
  557. if (result == NULL) {
  558. const google::protobuf::EnumValueDescriptor* enum_value =
  559. message->GetReflection()->GetEnum(*message, field_descriptor);
  560. result = PyInt_FromLong(enum_value->number());
  561. }
  562. break;
  563. }
  564. default:
  565. PyErr_Format(
  566. PyExc_SystemError, "Getting a value from a field of unknown type %d",
  567. field_descriptor->cpp_type());
  568. }
  569. return result;
  570. }
  571. static PyObject* InternalSetScalar(
  572. google::protobuf::Message* message, const google::protobuf::FieldDescriptor* field_descriptor,
  573. PyObject* arg) {
  574. const google::protobuf::Reflection* reflection = message->GetReflection();
  575. if (!FIELD_BELONGS_TO_MESSAGE(field_descriptor, message)) {
  576. PyErr_SetString(
  577. PyExc_KeyError, "Field does not belong to message!");
  578. return NULL;
  579. }
  580. switch (field_descriptor->cpp_type()) {
  581. case google::protobuf::FieldDescriptor::CPPTYPE_INT32: {
  582. GOOGLE_CHECK_GET_INT32(arg, value);
  583. reflection->SetInt32(message, field_descriptor, value);
  584. break;
  585. }
  586. case google::protobuf::FieldDescriptor::CPPTYPE_INT64: {
  587. GOOGLE_CHECK_GET_INT64(arg, value);
  588. reflection->SetInt64(message, field_descriptor, value);
  589. break;
  590. }
  591. case google::protobuf::FieldDescriptor::CPPTYPE_UINT32: {
  592. GOOGLE_CHECK_GET_UINT32(arg, value);
  593. reflection->SetUInt32(message, field_descriptor, value);
  594. break;
  595. }
  596. case google::protobuf::FieldDescriptor::CPPTYPE_UINT64: {
  597. GOOGLE_CHECK_GET_UINT64(arg, value);
  598. reflection->SetUInt64(message, field_descriptor, value);
  599. break;
  600. }
  601. case google::protobuf::FieldDescriptor::CPPTYPE_FLOAT: {
  602. GOOGLE_CHECK_GET_FLOAT(arg, value);
  603. reflection->SetFloat(message, field_descriptor, value);
  604. break;
  605. }
  606. case google::protobuf::FieldDescriptor::CPPTYPE_DOUBLE: {
  607. GOOGLE_CHECK_GET_DOUBLE(arg, value);
  608. reflection->SetDouble(message, field_descriptor, value);
  609. break;
  610. }
  611. case google::protobuf::FieldDescriptor::CPPTYPE_BOOL: {
  612. GOOGLE_CHECK_GET_BOOL(arg, value);
  613. reflection->SetBool(message, field_descriptor, value);
  614. break;
  615. }
  616. case google::protobuf::FieldDescriptor::CPPTYPE_STRING: {
  617. if (!CheckAndSetString(
  618. arg, message, field_descriptor, reflection, false, -1)) {
  619. return NULL;
  620. }
  621. break;
  622. }
  623. case google::protobuf::FieldDescriptor::CPPTYPE_ENUM: {
  624. GOOGLE_CHECK_GET_INT32(arg, value);
  625. const google::protobuf::EnumDescriptor* enum_descriptor =
  626. field_descriptor->enum_type();
  627. const google::protobuf::EnumValueDescriptor* enum_value =
  628. enum_descriptor->FindValueByNumber(value);
  629. if (enum_value != NULL) {
  630. reflection->SetEnum(message, field_descriptor, enum_value);
  631. } else {
  632. bool added = false;
  633. // Add the value to the unknown fields.
  634. google::protobuf::UnknownFieldSet* unknown_field_set =
  635. message->GetReflection()->MutableUnknownFields(message);
  636. for (int i = 0; i < unknown_field_set->field_count(); ++i) {
  637. if (unknown_field_set->field(i).number() ==
  638. field_descriptor->number()) {
  639. unknown_field_set->mutable_field(i)->set_varint(value);
  640. added = true;
  641. break;
  642. }
  643. }
  644. if (!added) {
  645. unknown_field_set->AddVarint(field_descriptor->number(), value);
  646. }
  647. reflection->ClearField(message, field_descriptor);
  648. }
  649. break;
  650. }
  651. default:
  652. PyErr_Format(
  653. PyExc_SystemError, "Setting value to a field of unknown type %d",
  654. field_descriptor->cpp_type());
  655. }
  656. Py_RETURN_NONE;
  657. }
  658. static PyObject* InternalAddRepeatedScalar(
  659. google::protobuf::Message* message, const google::protobuf::FieldDescriptor* field_descriptor,
  660. PyObject* arg) {
  661. if (!FIELD_BELONGS_TO_MESSAGE(field_descriptor, message)) {
  662. PyErr_SetString(
  663. PyExc_KeyError, "Field does not belong to message!");
  664. return NULL;
  665. }
  666. const google::protobuf::Reflection* reflection = message->GetReflection();
  667. switch (field_descriptor->cpp_type()) {
  668. case google::protobuf::FieldDescriptor::CPPTYPE_INT32: {
  669. GOOGLE_CHECK_GET_INT32(arg, value);
  670. reflection->AddInt32(message, field_descriptor, value);
  671. break;
  672. }
  673. case google::protobuf::FieldDescriptor::CPPTYPE_INT64: {
  674. GOOGLE_CHECK_GET_INT64(arg, value);
  675. reflection->AddInt64(message, field_descriptor, value);
  676. break;
  677. }
  678. case google::protobuf::FieldDescriptor::CPPTYPE_UINT32: {
  679. GOOGLE_CHECK_GET_UINT32(arg, value);
  680. reflection->AddUInt32(message, field_descriptor, value);
  681. break;
  682. }
  683. case google::protobuf::FieldDescriptor::CPPTYPE_UINT64: {
  684. GOOGLE_CHECK_GET_UINT64(arg, value);
  685. reflection->AddUInt64(message, field_descriptor, value);
  686. break;
  687. }
  688. case google::protobuf::FieldDescriptor::CPPTYPE_FLOAT: {
  689. GOOGLE_CHECK_GET_FLOAT(arg, value);
  690. reflection->AddFloat(message, field_descriptor, value);
  691. break;
  692. }
  693. case google::protobuf::FieldDescriptor::CPPTYPE_DOUBLE: {
  694. GOOGLE_CHECK_GET_DOUBLE(arg, value);
  695. reflection->AddDouble(message, field_descriptor, value);
  696. break;
  697. }
  698. case google::protobuf::FieldDescriptor::CPPTYPE_BOOL: {
  699. GOOGLE_CHECK_GET_BOOL(arg, value);
  700. reflection->AddBool(message, field_descriptor, value);
  701. break;
  702. }
  703. case google::protobuf::FieldDescriptor::CPPTYPE_STRING: {
  704. if (!CheckAndSetString(
  705. arg, message, field_descriptor, reflection, true, -1)) {
  706. return NULL;
  707. }
  708. break;
  709. }
  710. case google::protobuf::FieldDescriptor::CPPTYPE_ENUM: {
  711. GOOGLE_CHECK_GET_INT32(arg, value);
  712. const google::protobuf::EnumDescriptor* enum_descriptor =
  713. field_descriptor->enum_type();
  714. const google::protobuf::EnumValueDescriptor* enum_value =
  715. enum_descriptor->FindValueByNumber(value);
  716. if (enum_value != NULL) {
  717. reflection->AddEnum(message, field_descriptor, enum_value);
  718. } else {
  719. PyObject* s = PyObject_Str(arg);
  720. PyErr_Format(PyExc_ValueError, "Unknown enum value: %s",
  721. PyString_AS_STRING(s));
  722. Py_DECREF(s);
  723. return NULL;
  724. }
  725. break;
  726. }
  727. default:
  728. PyErr_Format(
  729. PyExc_SystemError, "Adding value to a field of unknown type %d",
  730. field_descriptor->cpp_type());
  731. }
  732. Py_RETURN_NONE;
  733. }
  734. static PyObject* InternalGetRepeatedScalar(
  735. CMessage* cmessage, const google::protobuf::FieldDescriptor* field_descriptor,
  736. int index) {
  737. google::protobuf::Message* message = cmessage->message;
  738. const google::protobuf::Reflection* reflection = message->GetReflection();
  739. int field_size = reflection->FieldSize(*message, field_descriptor);
  740. if (index < 0) {
  741. index = field_size + index;
  742. }
  743. if (index < 0 || index >= field_size) {
  744. PyErr_Format(PyExc_IndexError,
  745. "list assignment index (%d) out of range", index);
  746. return NULL;
  747. }
  748. PyObject* result = NULL;
  749. switch (field_descriptor->cpp_type()) {
  750. case google::protobuf::FieldDescriptor::CPPTYPE_INT32: {
  751. int32 value = reflection->GetRepeatedInt32(
  752. *message, field_descriptor, index);
  753. result = PyInt_FromLong(value);
  754. break;
  755. }
  756. case google::protobuf::FieldDescriptor::CPPTYPE_INT64: {
  757. int64 value = reflection->GetRepeatedInt64(
  758. *message, field_descriptor, index);
  759. result = PyLong_FromLongLong(value);
  760. break;
  761. }
  762. case google::protobuf::FieldDescriptor::CPPTYPE_UINT32: {
  763. uint32 value = reflection->GetRepeatedUInt32(
  764. *message, field_descriptor, index);
  765. result = PyLong_FromLongLong(value);
  766. break;
  767. }
  768. case google::protobuf::FieldDescriptor::CPPTYPE_UINT64: {
  769. uint64 value = reflection->GetRepeatedUInt64(
  770. *message, field_descriptor, index);
  771. result = PyLong_FromUnsignedLongLong(value);
  772. break;
  773. }
  774. case google::protobuf::FieldDescriptor::CPPTYPE_FLOAT: {
  775. float value = reflection->GetRepeatedFloat(
  776. *message, field_descriptor, index);
  777. result = PyFloat_FromDouble(value);
  778. break;
  779. }
  780. case google::protobuf::FieldDescriptor::CPPTYPE_DOUBLE: {
  781. double value = reflection->GetRepeatedDouble(
  782. *message, field_descriptor, index);
  783. result = PyFloat_FromDouble(value);
  784. break;
  785. }
  786. case google::protobuf::FieldDescriptor::CPPTYPE_BOOL: {
  787. bool value = reflection->GetRepeatedBool(
  788. *message, field_descriptor, index);
  789. result = PyBool_FromLong(value ? 1 : 0);
  790. break;
  791. }
  792. case google::protobuf::FieldDescriptor::CPPTYPE_ENUM: {
  793. const google::protobuf::EnumValueDescriptor* enum_value =
  794. message->GetReflection()->GetRepeatedEnum(
  795. *message, field_descriptor, index);
  796. result = PyInt_FromLong(enum_value->number());
  797. break;
  798. }
  799. case google::protobuf::FieldDescriptor::CPPTYPE_STRING: {
  800. string value = reflection->GetRepeatedString(
  801. *message, field_descriptor, index);
  802. result = ToStringObject(field_descriptor, value);
  803. break;
  804. }
  805. case google::protobuf::FieldDescriptor::CPPTYPE_MESSAGE: {
  806. CMessage* py_cmsg = PyObject_New(CMessage, &CMessage_Type);
  807. if (py_cmsg == NULL) {
  808. return NULL;
  809. }
  810. const google::protobuf::Message& msg = reflection->GetRepeatedMessage(
  811. *message, field_descriptor, index);
  812. py_cmsg->parent = cmessage;
  813. py_cmsg->full_name = field_descriptor->full_name().c_str();
  814. py_cmsg->message = const_cast<google::protobuf::Message*>(&msg);
  815. py_cmsg->free_message = false;
  816. py_cmsg->read_only = false;
  817. result = reinterpret_cast<PyObject*>(py_cmsg);
  818. break;
  819. }
  820. default:
  821. PyErr_Format(
  822. PyExc_SystemError,
  823. "Getting value from a repeated field of unknown type %d",
  824. field_descriptor->cpp_type());
  825. }
  826. return result;
  827. }
  828. static PyObject* InternalGetRepeatedScalarSlice(
  829. CMessage* cmessage, const google::protobuf::FieldDescriptor* field_descriptor,
  830. PyObject* slice) {
  831. Py_ssize_t from;
  832. Py_ssize_t to;
  833. Py_ssize_t step;
  834. Py_ssize_t length;
  835. bool return_list = false;
  836. google::protobuf::Message* message = cmessage->message;
  837. if (PyInt_Check(slice)) {
  838. from = to = PyInt_AsLong(slice);
  839. } else if (PyLong_Check(slice)) {
  840. from = to = PyLong_AsLong(slice);
  841. } else if (PySlice_Check(slice)) {
  842. const google::protobuf::Reflection* reflection = message->GetReflection();
  843. length = reflection->FieldSize(*message, field_descriptor);
  844. PySlice_GetIndices(
  845. reinterpret_cast<PySliceObject*>(slice), length, &from, &to, &step);
  846. return_list = true;
  847. } else {
  848. PyErr_SetString(PyExc_TypeError, "list indices must be integers");
  849. return NULL;
  850. }
  851. if (!return_list) {
  852. return InternalGetRepeatedScalar(cmessage, field_descriptor, from);
  853. }
  854. PyObject* list = PyList_New(0);
  855. if (list == NULL) {
  856. return NULL;
  857. }
  858. if (from <= to) {
  859. if (step < 0) return list;
  860. for (Py_ssize_t index = from; index < to; index += step) {
  861. if (index < 0 || index >= length) break;
  862. PyObject* s = InternalGetRepeatedScalar(
  863. cmessage, field_descriptor, index);
  864. PyList_Append(list, s);
  865. Py_DECREF(s);
  866. }
  867. } else {
  868. if (step > 0) return list;
  869. for (Py_ssize_t index = from; index > to; index += step) {
  870. if (index < 0 || index >= length) break;
  871. PyObject* s = InternalGetRepeatedScalar(
  872. cmessage, field_descriptor, index);
  873. PyList_Append(list, s);
  874. Py_DECREF(s);
  875. }
  876. }
  877. return list;
  878. }
  879. // ------ C Constructor/Destructor:
  880. static int CMessageInit(CMessage* self, PyObject *args, PyObject *kwds) {
  881. self->message = NULL;
  882. return 0;
  883. }
  884. static void CMessageDealloc(CMessage* self) {
  885. if (self->free_message) {
  886. if (self->read_only) {
  887. PyErr_WriteUnraisable(reinterpret_cast<PyObject*>(self));
  888. }
  889. delete self->message;
  890. }
  891. self->ob_type->tp_free(reinterpret_cast<PyObject*>(self));
  892. }
  893. // ------ Methods:
  894. static PyObject* CMessage_Clear(CMessage* self, PyObject* arg) {
  895. AssureWritable(self);
  896. google::protobuf::Message* message = self->message;
  897. // This block of code is equivalent to the following:
  898. // for cfield_descriptor, child_cmessage in arg:
  899. // ReleaseSubMessage(cfield_descriptor, child_cmessage)
  900. if (!PyList_Check(arg)) {
  901. PyErr_SetString(PyExc_TypeError, "Must be a list");
  902. return NULL;
  903. }
  904. PyObject* messages_to_clear = arg;
  905. Py_ssize_t num_messages_to_clear = PyList_GET_SIZE(messages_to_clear);
  906. for(int i = 0; i < num_messages_to_clear; ++i) {
  907. PyObject* message_tuple = PyList_GET_ITEM(messages_to_clear, i);
  908. if (!PyTuple_Check(message_tuple) || PyTuple_GET_SIZE(message_tuple) != 2) {
  909. PyErr_SetString(PyExc_TypeError, "Must be a tuple of size 2");
  910. return NULL;
  911. }
  912. PyObject* py_cfield_descriptor = PyTuple_GET_ITEM(message_tuple, 0);
  913. PyObject* py_child_cmessage = PyTuple_GET_ITEM(message_tuple, 1);
  914. if (!PyObject_TypeCheck(py_cfield_descriptor, &CFieldDescriptor_Type) ||
  915. !PyObject_TypeCheck(py_child_cmessage, &CMessage_Type)) {
  916. PyErr_SetString(PyExc_ValueError, "Invalid Tuple");
  917. return NULL;
  918. }
  919. CFieldDescriptor* cfield_descriptor = reinterpret_cast<CFieldDescriptor *>(
  920. py_cfield_descriptor);
  921. CMessage* child_cmessage = reinterpret_cast<CMessage *>(py_child_cmessage);
  922. ReleaseSubMessage(message, cfield_descriptor->descriptor, child_cmessage);
  923. }
  924. message->Clear();
  925. Py_RETURN_NONE;
  926. }
  927. static PyObject* CMessage_IsInitialized(CMessage* self, PyObject* args) {
  928. return PyBool_FromLong(self->message->IsInitialized() ? 1 : 0);
  929. }
  930. static PyObject* CMessage_HasField(CMessage* self, PyObject* arg) {
  931. char* field_name;
  932. if (PyString_AsStringAndSize(arg, &field_name, NULL) < 0) {
  933. return NULL;
  934. }
  935. google::protobuf::Message* message = self->message;
  936. const google::protobuf::Descriptor* descriptor = message->GetDescriptor();
  937. const google::protobuf::FieldDescriptor* field_descriptor =
  938. descriptor->FindFieldByName(field_name);
  939. if (field_descriptor == NULL) {
  940. PyErr_Format(PyExc_ValueError, "Unknown field %s.", field_name);
  941. return NULL;
  942. }
  943. bool has_field =
  944. message->GetReflection()->HasField(*message, field_descriptor);
  945. return PyBool_FromLong(has_field ? 1 : 0);
  946. }
  947. static PyObject* CMessage_HasFieldByDescriptor(CMessage* self, PyObject* arg) {
  948. CFieldDescriptor* cfield_descriptor = NULL;
  949. if (!PyObject_TypeCheck(reinterpret_cast<PyObject *>(arg),
  950. &CFieldDescriptor_Type)) {
  951. PyErr_SetString(PyExc_TypeError, "Must be a field descriptor");
  952. return NULL;
  953. }
  954. cfield_descriptor = reinterpret_cast<CFieldDescriptor*>(arg);
  955. google::protobuf::Message* message = self->message;
  956. const google::protobuf::FieldDescriptor* field_descriptor =
  957. cfield_descriptor->descriptor;
  958. if (!FIELD_BELONGS_TO_MESSAGE(field_descriptor, message)) {
  959. PyErr_SetString(PyExc_KeyError,
  960. "Field does not belong to message!");
  961. return NULL;
  962. }
  963. if (FIELD_IS_REPEATED(field_descriptor)) {
  964. PyErr_SetString(PyExc_KeyError,
  965. "Field is repeated. A singular method is required.");
  966. return NULL;
  967. }
  968. bool has_field =
  969. message->GetReflection()->HasField(*message, field_descriptor);
  970. return PyBool_FromLong(has_field ? 1 : 0);
  971. }
  972. static PyObject* CMessage_ClearFieldByDescriptor(
  973. CMessage* self, PyObject* arg) {
  974. CFieldDescriptor* cfield_descriptor = NULL;
  975. if (!PyObject_TypeCheck(reinterpret_cast<PyObject *>(arg),
  976. &CFieldDescriptor_Type)) {
  977. PyErr_SetString(PyExc_TypeError, "Must be a field descriptor");
  978. return NULL;
  979. }
  980. cfield_descriptor = reinterpret_cast<CFieldDescriptor*>(arg);
  981. google::protobuf::Message* message = self->message;
  982. const google::protobuf::FieldDescriptor* field_descriptor =
  983. cfield_descriptor->descriptor;
  984. if (!FIELD_BELONGS_TO_MESSAGE(field_descriptor, message)) {
  985. PyErr_SetString(PyExc_KeyError,
  986. "Field does not belong to message!");
  987. return NULL;
  988. }
  989. message->GetReflection()->ClearField(message, field_descriptor);
  990. Py_RETURN_NONE;
  991. }
  992. static PyObject* CMessage_ClearField(CMessage* self, PyObject* args) {
  993. char* field_name;
  994. CMessage* child_cmessage = NULL;
  995. if (!PyArg_ParseTuple(args, C("s|O!:ClearField"), &field_name,
  996. &CMessage_Type, &child_cmessage)) {
  997. return NULL;
  998. }
  999. google::protobuf::Message* message = self->message;
  1000. const google::protobuf::Descriptor* descriptor = message->GetDescriptor();
  1001. const google::protobuf::FieldDescriptor* field_descriptor =
  1002. descriptor->FindFieldByName(field_name);
  1003. if (field_descriptor == NULL) {
  1004. PyErr_Format(PyExc_ValueError, "Unknown field %s.", field_name);
  1005. return NULL;
  1006. }
  1007. if (child_cmessage != NULL && !FIELD_IS_REPEATED(field_descriptor)) {
  1008. ReleaseSubMessage(message, field_descriptor, child_cmessage);
  1009. } else {
  1010. message->GetReflection()->ClearField(message, field_descriptor);
  1011. }
  1012. Py_RETURN_NONE;
  1013. }
  1014. static PyObject* CMessage_GetScalar(CMessage* self, PyObject* arg) {
  1015. CFieldDescriptor* cdescriptor = NULL;
  1016. if (!PyObject_TypeCheck(reinterpret_cast<PyObject *>(arg),
  1017. &CFieldDescriptor_Type)) {
  1018. PyErr_SetString(PyExc_TypeError, "Must be a field descriptor");
  1019. return NULL;
  1020. }
  1021. cdescriptor = reinterpret_cast<CFieldDescriptor*>(arg);
  1022. google::protobuf::Message* message = self->message;
  1023. return InternalGetScalar(message, cdescriptor->descriptor);
  1024. }
  1025. static PyObject* CMessage_GetRepeatedScalar(CMessage* self, PyObject* args) {
  1026. CFieldDescriptor* cfield_descriptor;
  1027. PyObject* slice;
  1028. if (!PyArg_ParseTuple(args, C("O!O:GetRepeatedScalar"),
  1029. &CFieldDescriptor_Type, &cfield_descriptor, &slice)) {
  1030. return NULL;
  1031. }
  1032. return InternalGetRepeatedScalarSlice(
  1033. self, cfield_descriptor->descriptor, slice);
  1034. }
  1035. static PyObject* CMessage_AssignRepeatedScalar(CMessage* self, PyObject* args) {
  1036. CFieldDescriptor* cfield_descriptor;
  1037. PyObject* slice;
  1038. if (!PyArg_ParseTuple(args, C("O!O:AssignRepeatedScalar"),
  1039. &CFieldDescriptor_Type, &cfield_descriptor, &slice)) {
  1040. return NULL;
  1041. }
  1042. AssureWritable(self);
  1043. google::protobuf::Message* message = self->message;
  1044. message->GetReflection()->ClearField(message, cfield_descriptor->descriptor);
  1045. PyObject* iter = PyObject_GetIter(slice);
  1046. PyObject* next;
  1047. while ((next = PyIter_Next(iter)) != NULL) {
  1048. if (InternalAddRepeatedScalar(
  1049. message, cfield_descriptor->descriptor, next) == NULL) {
  1050. Py_DECREF(next);
  1051. Py_DECREF(iter);
  1052. return NULL;
  1053. }
  1054. Py_DECREF(next);
  1055. }
  1056. Py_DECREF(iter);
  1057. Py_RETURN_NONE;
  1058. }
  1059. static PyObject* CMessage_DeleteRepeatedField(CMessage* self, PyObject* args) {
  1060. CFieldDescriptor* cfield_descriptor;
  1061. PyObject* slice;
  1062. if (!PyArg_ParseTuple(args, C("O!O:DeleteRepeatedField"),
  1063. &CFieldDescriptor_Type, &cfield_descriptor, &slice)) {
  1064. return NULL;
  1065. }
  1066. AssureWritable(self);
  1067. Py_ssize_t length, from, to, step, slice_length;
  1068. google::protobuf::Message* message = self->message;
  1069. const google::protobuf::FieldDescriptor* field_descriptor =
  1070. cfield_descriptor->descriptor;
  1071. const google::protobuf::Reflection* reflection = message->GetReflection();
  1072. int min, max;
  1073. length = reflection->FieldSize(*message, field_descriptor);
  1074. if (PyInt_Check(slice) || PyLong_Check(slice)) {
  1075. from = to = PyLong_AsLong(slice);
  1076. if (from < 0) {
  1077. from = to = length + from;
  1078. }
  1079. step = 1;
  1080. min = max = from;
  1081. // Range check.
  1082. if (from < 0 || from >= length) {
  1083. PyErr_Format(PyExc_IndexError, "list assignment index out of range");
  1084. return NULL;
  1085. }
  1086. } else if (PySlice_Check(slice)) {
  1087. from = to = step = slice_length = 0;
  1088. PySlice_GetIndicesEx(
  1089. reinterpret_cast<PySliceObject*>(slice),
  1090. length, &from, &to, &step, &slice_length);
  1091. if (from < to) {
  1092. min = from;
  1093. max = to - 1;
  1094. } else {
  1095. min = to + 1;
  1096. max = from;
  1097. }
  1098. } else {
  1099. PyErr_SetString(PyExc_TypeError, "list indices must be integers");
  1100. return NULL;
  1101. }
  1102. Py_ssize_t i = from;
  1103. std::vector<bool> to_delete(length, false);
  1104. while (i >= min && i <= max) {
  1105. to_delete[i] = true;
  1106. i += step;
  1107. }
  1108. to = 0;
  1109. for (i = 0; i < length; ++i) {
  1110. if (!to_delete[i]) {
  1111. if (i != to) {
  1112. reflection->SwapElements(message, field_descriptor, i, to);
  1113. }
  1114. ++to;
  1115. }
  1116. }
  1117. while (i > to) {
  1118. reflection->RemoveLast(message, field_descriptor);
  1119. --i;
  1120. }
  1121. Py_RETURN_NONE;
  1122. }
  1123. static PyObject* CMessage_SetScalar(CMessage* self, PyObject* args) {
  1124. CFieldDescriptor* cfield_descriptor;
  1125. PyObject* arg;
  1126. if (!PyArg_ParseTuple(args, C("O!O:SetScalar"),
  1127. &CFieldDescriptor_Type, &cfield_descriptor, &arg)) {
  1128. return NULL;
  1129. }
  1130. AssureWritable(self);
  1131. return InternalSetScalar(self->message, cfield_descriptor->descriptor, arg);
  1132. }
  1133. static PyObject* CMessage_AddRepeatedScalar(CMessage* self, PyObject* args) {
  1134. CFieldDescriptor* cfield_descriptor;
  1135. PyObject* value;
  1136. if (!PyArg_ParseTuple(args, C("O!O:AddRepeatedScalar"),
  1137. &CFieldDescriptor_Type, &cfield_descriptor, &value)) {
  1138. return NULL;
  1139. }
  1140. AssureWritable(self);
  1141. return InternalAddRepeatedScalar(
  1142. self->message, cfield_descriptor->descriptor, value);
  1143. }
  1144. static PyObject* CMessage_FieldLength(CMessage* self, PyObject* arg) {
  1145. CFieldDescriptor* cfield_descriptor;
  1146. if (!PyObject_TypeCheck(reinterpret_cast<PyObject *>(arg),
  1147. &CFieldDescriptor_Type)) {
  1148. PyErr_SetString(PyExc_TypeError, "Must be a field descriptor");
  1149. return NULL;
  1150. }
  1151. cfield_descriptor = reinterpret_cast<CFieldDescriptor*>(arg);
  1152. google::protobuf::Message* message = self->message;
  1153. int length = message->GetReflection()->FieldSize(
  1154. *message, cfield_descriptor->descriptor);
  1155. return PyInt_FromLong(length);
  1156. }
  1157. static PyObject* CMessage_DebugString(CMessage* self, PyObject* args) {
  1158. return PyString_FromString(self->message->DebugString().c_str());
  1159. }
  1160. static PyObject* CMessage_SerializeToString(CMessage* self, PyObject* args) {
  1161. int size = self->message->ByteSize();
  1162. if (size <= 0) {
  1163. return PyString_FromString("");
  1164. }
  1165. PyObject* result = PyString_FromStringAndSize(NULL, size);
  1166. if (result == NULL) {
  1167. return NULL;
  1168. }
  1169. char* buffer = PyString_AS_STRING(result);
  1170. self->message->SerializeWithCachedSizesToArray(
  1171. reinterpret_cast<uint8*>(buffer));
  1172. return result;
  1173. }
  1174. static PyObject* CMessage_SerializePartialToString(
  1175. CMessage* self, PyObject* args) {
  1176. string contents;
  1177. self->message->SerializePartialToString(&contents);
  1178. return PyString_FromStringAndSize(contents.c_str(), contents.size());
  1179. }
  1180. static PyObject* CMessageStr(CMessage* self) {
  1181. char str[1024];
  1182. str[sizeof(str) - 1] = 0;
  1183. snprintf(str, sizeof(str) - 1, "CMessage: <%p>", self->message);
  1184. return PyString_FromString(str);
  1185. }
  1186. static PyObject* CMessage_MergeFrom(CMessage* self, PyObject* arg) {
  1187. CMessage* other_message;
  1188. if (!PyObject_TypeCheck(reinterpret_cast<PyObject *>(arg), &CMessage_Type)) {
  1189. PyErr_SetString(PyExc_TypeError, "Must be a message");
  1190. return NULL;
  1191. }
  1192. other_message = reinterpret_cast<CMessage*>(arg);
  1193. if (other_message->message->GetDescriptor() !=
  1194. self->message->GetDescriptor()) {
  1195. PyErr_Format(PyExc_TypeError,
  1196. "Tried to merge from a message with a different type. "
  1197. "to: %s, from: %s",
  1198. self->message->GetDescriptor()->full_name().c_str(),
  1199. other_message->message->GetDescriptor()->full_name().c_str());
  1200. return NULL;
  1201. }
  1202. AssureWritable(self);
  1203. self->message->MergeFrom(*other_message->message);
  1204. Py_RETURN_NONE;
  1205. }
  1206. static PyObject* CMessage_CopyFrom(CMessage* self, PyObject* arg) {
  1207. CMessage* other_message;
  1208. if (!PyObject_TypeCheck(reinterpret_cast<PyObject *>(arg), &CMessage_Type)) {
  1209. PyErr_SetString(PyExc_TypeError, "Must be a message");
  1210. return NULL;
  1211. }
  1212. other_message = reinterpret_cast<CMessage*>(arg);
  1213. if (other_message->message->GetDescriptor() !=
  1214. self->message->GetDescriptor()) {
  1215. PyErr_Format(PyExc_TypeError,
  1216. "Tried to copy from a message with a different type. "
  1217. "to: %s, from: %s",
  1218. self->message->GetDescriptor()->full_name().c_str(),
  1219. other_message->message->GetDescriptor()->full_name().c_str());
  1220. return NULL;
  1221. }
  1222. AssureWritable(self);
  1223. self->message->CopyFrom(*other_message->message);
  1224. Py_RETURN_NONE;
  1225. }
  1226. static PyObject* CMessage_MergeFromString(CMessage* self, PyObject* arg) {
  1227. const void* data;
  1228. Py_ssize_t data_length;
  1229. if (PyObject_AsReadBuffer(arg, &data, &data_length) < 0) {
  1230. return NULL;
  1231. }
  1232. AssureWritable(self);
  1233. google::protobuf::io::CodedInputStream input(
  1234. reinterpret_cast<const uint8*>(data), data_length);
  1235. input.SetExtensionRegistry(GetDescriptorPool(), global_message_factory);
  1236. bool success = self->message->MergePartialFromCodedStream(&input);
  1237. if (success) {
  1238. return PyInt_FromLong(self->message->ByteSize());
  1239. } else {
  1240. return PyInt_FromLong(-1);
  1241. }
  1242. }
  1243. static PyObject* CMessage_ByteSize(CMessage* self, PyObject* args) {
  1244. return PyLong_FromLong(self->message->ByteSize());
  1245. }
  1246. static PyObject* CMessage_SetInParent(CMessage* self, PyObject* args) {
  1247. AssureWritable(self);
  1248. Py_RETURN_NONE;
  1249. }
  1250. static PyObject* CMessage_SwapRepeatedFieldElements(
  1251. CMessage* self, PyObject* args) {
  1252. CFieldDescriptor* cfield_descriptor;
  1253. int index1, index2;
  1254. if (!PyArg_ParseTuple(args, C("O!ii:SwapRepeatedFieldElements"),
  1255. &CFieldDescriptor_Type, &cfield_descriptor,
  1256. &index1, &index2)) {
  1257. return NULL;
  1258. }
  1259. google::protobuf::Message* message = self->message;
  1260. const google::protobuf::Reflection* reflection = message->GetReflection();
  1261. reflection->SwapElements(
  1262. message, cfield_descriptor->descriptor, index1, index2);
  1263. Py_RETURN_NONE;
  1264. }
  1265. static PyObject* CMessage_AddMessage(CMessage* self, PyObject* arg) {
  1266. CFieldDescriptor* cfield_descriptor;
  1267. if (!PyObject_TypeCheck(reinterpret_cast<PyObject *>(arg),
  1268. &CFieldDescriptor_Type)) {
  1269. PyErr_SetString(PyExc_TypeError, "Must be a field descriptor");
  1270. return NULL;
  1271. }
  1272. cfield_descriptor = reinterpret_cast<CFieldDescriptor*>(arg);
  1273. AssureWritable(self);
  1274. CMessage* py_cmsg = PyObject_New(CMessage, &CMessage_Type);
  1275. if (py_cmsg == NULL) {
  1276. return NULL;
  1277. }
  1278. google::protobuf::Message* message = self->message;
  1279. const google::protobuf::Reflection* reflection = message->GetReflection();
  1280. google::protobuf::Message* sub_message =
  1281. reflection->AddMessage(message, cfield_descriptor->descriptor);
  1282. py_cmsg->parent = NULL;
  1283. py_cmsg->full_name = sub_message->GetDescriptor()->full_name().c_str();
  1284. py_cmsg->message = sub_message;
  1285. py_cmsg->free_message = false;
  1286. py_cmsg->read_only = false;
  1287. return reinterpret_cast<PyObject*>(py_cmsg);
  1288. }
  1289. static PyObject* CMessage_GetRepeatedMessage(CMessage* self, PyObject* args) {
  1290. CFieldDescriptor* cfield_descriptor;
  1291. PyObject* slice;
  1292. if (!PyArg_ParseTuple(args, C("O!O:GetRepeatedMessage"),
  1293. &CFieldDescriptor_Type, &cfield_descriptor, &slice)) {
  1294. return NULL;
  1295. }
  1296. return InternalGetRepeatedScalarSlice(
  1297. self, cfield_descriptor->descriptor, slice);
  1298. }
  1299. static PyObject* CMessage_NewSubMessage(CMessage* self, PyObject* arg) {
  1300. CFieldDescriptor* cfield_descriptor;
  1301. if (!PyObject_TypeCheck(reinterpret_cast<PyObject *>(arg),
  1302. &CFieldDescriptor_Type)) {
  1303. PyErr_SetString(PyExc_TypeError, "Must be a field descriptor");
  1304. return NULL;
  1305. }
  1306. cfield_descriptor = reinterpret_cast<CFieldDescriptor*>(arg);
  1307. CMessage* py_cmsg = PyObject_New(CMessage, &CMessage_Type);
  1308. if (py_cmsg == NULL) {
  1309. return NULL;
  1310. }
  1311. google::protobuf::Message* message = self->message;
  1312. const google::protobuf::Reflection* reflection = message->GetReflection();
  1313. const google::protobuf::Message& sub_message =
  1314. reflection->GetMessage(*message, cfield_descriptor->descriptor,
  1315. global_message_factory);
  1316. py_cmsg->full_name = sub_message.GetDescriptor()->full_name().c_str();
  1317. py_cmsg->parent = self;
  1318. py_cmsg->parent_field = cfield_descriptor;
  1319. py_cmsg->message = const_cast<google::protobuf::Message*>(&sub_message);
  1320. py_cmsg->free_message = false;
  1321. py_cmsg->read_only = true;
  1322. return reinterpret_cast<PyObject*>(py_cmsg);
  1323. }
  1324. static PyObject* CMessage_MutableMessage(CMessage* self, PyObject* arg) {
  1325. CFieldDescriptor* cfield_descriptor;
  1326. if (!PyObject_TypeCheck(reinterpret_cast<PyObject *>(arg),
  1327. &CFieldDescriptor_Type)) {
  1328. PyErr_SetString(PyExc_TypeError, "Must be a field descriptor");
  1329. return NULL;
  1330. }
  1331. cfield_descriptor = reinterpret_cast<CFieldDescriptor*>(arg);
  1332. AssureWritable(self);
  1333. CMessage* py_cmsg = PyObject_New(CMessage, &CMessage_Type);
  1334. if (py_cmsg == NULL) {
  1335. return NULL;
  1336. }
  1337. google::protobuf::Message* message = self->message;
  1338. const google::protobuf::Reflection* reflection = message->GetReflection();
  1339. google::protobuf::Message* mutable_message =
  1340. reflection->MutableMessage(message, cfield_descriptor->descriptor,
  1341. global_message_factory);
  1342. py_cmsg->full_name = mutable_message->GetDescriptor()->full_name().c_str();
  1343. py_cmsg->message = mutable_message;
  1344. py_cmsg->free_message = false;
  1345. py_cmsg->read_only = false;
  1346. return reinterpret_cast<PyObject*>(py_cmsg);
  1347. }
  1348. static PyObject* CMessage_Equals(CMessage* self, PyObject* arg) {
  1349. CMessage* other_message;
  1350. if (!PyObject_TypeCheck(reinterpret_cast<PyObject *>(arg), &CMessage_Type)) {
  1351. PyErr_SetString(PyExc_TypeError, "Must be a message");
  1352. return NULL;
  1353. }
  1354. other_message = reinterpret_cast<CMessage*>(arg);
  1355. if (other_message->message == self->message) {
  1356. return PyBool_FromLong(1);
  1357. }
  1358. if (other_message->message->GetDescriptor() !=
  1359. self->message->GetDescriptor()) {
  1360. return PyBool_FromLong(0);
  1361. }
  1362. return PyBool_FromLong(1);
  1363. }
  1364. static PyObject* CMessage_ListFields(CMessage* self, PyObject* args) {
  1365. google::protobuf::Message* message = self->message;
  1366. const google::protobuf::Reflection* reflection = message->GetReflection();
  1367. vector<const google::protobuf::FieldDescriptor*> fields;
  1368. reflection->ListFields(*message, &fields);
  1369. PyObject* list = PyList_New(fields.size());
  1370. if (list == NULL) {
  1371. return NULL;
  1372. }
  1373. for (unsigned int i = 0; i < fields.size(); ++i) {
  1374. bool is_extension = fields[i]->is_extension();
  1375. PyObject* t = PyTuple_New(2);
  1376. if (t == NULL) {
  1377. Py_DECREF(list);
  1378. return NULL;
  1379. }
  1380. PyObject* is_extension_object = PyBool_FromLong(is_extension ? 1 : 0);
  1381. PyObject* field_name;
  1382. const string* s;
  1383. if (is_extension) {
  1384. s = &fields[i]->full_name();
  1385. } else {
  1386. s = &fields[i]->name();
  1387. }
  1388. field_name = PyString_FromStringAndSize(s->c_str(), s->length());
  1389. if (field_name == NULL) {
  1390. Py_DECREF(list);
  1391. Py_DECREF(t);
  1392. return NULL;
  1393. }
  1394. PyTuple_SET_ITEM(t, 0, is_extension_object);
  1395. PyTuple_SET_ITEM(t, 1, field_name);
  1396. PyList_SET_ITEM(list, i, t);
  1397. }
  1398. return list;
  1399. }
  1400. static PyObject* CMessage_FindInitializationErrors(CMessage* self) {
  1401. google::protobuf::Message* message = self->message;
  1402. vector<string> errors;
  1403. message->FindInitializationErrors(&errors);
  1404. PyObject* error_list = PyList_New(errors.size());
  1405. if (error_list == NULL) {
  1406. return NULL;
  1407. }
  1408. for (unsigned int i = 0; i < errors.size(); ++i) {
  1409. const string& error = errors[i];
  1410. PyObject* error_string = PyString_FromStringAndSize(
  1411. error.c_str(), error.length());
  1412. if (error_string == NULL) {
  1413. Py_DECREF(error_list);
  1414. return NULL;
  1415. }
  1416. PyList_SET_ITEM(error_list, i, error_string);
  1417. }
  1418. return error_list;
  1419. }
  1420. // ------ Python Constructor:
  1421. PyObject* Python_NewCMessage(PyObject* ignored, PyObject* arg) {
  1422. const char* message_type = PyString_AsString(arg);
  1423. if (message_type == NULL) {
  1424. return NULL;
  1425. }
  1426. const google::protobuf::Message* message = CreateMessage(message_type);
  1427. if (message == NULL) {
  1428. PyErr_Format(PyExc_TypeError, "Couldn't create message of type %s!",
  1429. message_type);
  1430. return NULL;
  1431. }
  1432. CMessage* py_cmsg = PyObject_New(CMessage, &CMessage_Type);
  1433. if (py_cmsg == NULL) {
  1434. return NULL;
  1435. }
  1436. py_cmsg->message = message->New();
  1437. py_cmsg->free_message = true;
  1438. py_cmsg->full_name = message->GetDescriptor()->full_name().c_str();
  1439. py_cmsg->read_only = false;
  1440. py_cmsg->parent = NULL;
  1441. py_cmsg->parent_field = NULL;
  1442. return reinterpret_cast<PyObject*>(py_cmsg);
  1443. }
  1444. // --- Module Functions (exposed to Python):
  1445. PyMethodDef methods[] = {
  1446. { C("NewCMessage"), (PyCFunction)Python_NewCMessage,
  1447. METH_O,
  1448. C("Creates a new C++ protocol message, given its full name.") },
  1449. { C("NewCDescriptorPool"), (PyCFunction)Python_NewCDescriptorPool,
  1450. METH_NOARGS,
  1451. C("Creates a new C++ descriptor pool.") },
  1452. { C("BuildFile"), (PyCFunction)Python_BuildFile,
  1453. METH_O,
  1454. C("Registers a new protocol buffer file in the global C++ descriptor "
  1455. "pool.") },
  1456. {NULL}
  1457. };
  1458. // --- Exposing the C proto living inside Python proto to C code:
  1459. extern const Message* (*GetCProtoInsidePyProtoPtr)(PyObject* msg);
  1460. extern Message* (*MutableCProtoInsidePyProtoPtr)(PyObject* msg);
  1461. static const google::protobuf::Message* GetCProtoInsidePyProtoImpl(PyObject* msg) {
  1462. PyObject* c_msg_obj = PyObject_GetAttrString(msg, "_cmsg");
  1463. if (c_msg_obj == NULL) {
  1464. PyErr_Clear();
  1465. return NULL;
  1466. }
  1467. Py_DECREF(c_msg_obj);
  1468. if (!PyObject_TypeCheck(c_msg_obj, &CMessage_Type)) {
  1469. return NULL;
  1470. }
  1471. CMessage* c_msg = reinterpret_cast<CMessage*>(c_msg_obj);
  1472. return c_msg->message;
  1473. }
  1474. static google::protobuf::Message* MutableCProtoInsidePyProtoImpl(PyObject* msg) {
  1475. PyObject* c_msg_obj = PyObject_GetAttrString(msg, "_cmsg");
  1476. if (c_msg_obj == NULL) {
  1477. PyErr_Clear();
  1478. return NULL;
  1479. }
  1480. Py_DECREF(c_msg_obj);
  1481. if (!PyObject_TypeCheck(c_msg_obj, &CMessage_Type)) {
  1482. return NULL;
  1483. }
  1484. CMessage* c_msg = reinterpret_cast<CMessage*>(c_msg_obj);
  1485. AssureWritable(c_msg);
  1486. return c_msg->message;
  1487. }
  1488. // --- Module Init Function:
  1489. static const char module_docstring[] =
  1490. "python-proto2 is a module that can be used to enhance proto2 Python API\n"
  1491. "performance.\n"
  1492. "\n"
  1493. "It provides access to the protocol buffers C++ reflection API that\n"
  1494. "implements the basic protocol buffer functions.";
  1495. extern "C" {
  1496. void init_net_proto2___python() {
  1497. // Initialize constants.
  1498. kPythonZero = PyInt_FromLong(0);
  1499. kint32min_py = PyInt_FromLong(kint32min);
  1500. kint32max_py = PyInt_FromLong(kint32max);
  1501. kuint32max_py = PyLong_FromLongLong(kuint32max);
  1502. kint64min_py = PyLong_FromLongLong(kint64min);
  1503. kint64max_py = PyLong_FromLongLong(kint64max);
  1504. kuint64max_py = PyLong_FromUnsignedLongLong(kuint64max);
  1505. global_message_factory = new DynamicMessageFactory(GetDescriptorPool());
  1506. global_message_factory->SetDelegateToGeneratedFactory(true);
  1507. // Export our functions to Python.
  1508. PyObject *m;
  1509. m = Py_InitModule3(C("_net_proto2___python"), methods, C(module_docstring));
  1510. if (m == NULL) {
  1511. return;
  1512. }
  1513. AddConstants(m);
  1514. CMessage_Type.tp_new = PyType_GenericNew;
  1515. if (PyType_Ready(&CMessage_Type) < 0) {
  1516. return;
  1517. }
  1518. if (!InitDescriptor()) {
  1519. return;
  1520. }
  1521. // Override {Get,Mutable}CProtoInsidePyProto.
  1522. GetCProtoInsidePyProtoPtr = GetCProtoInsidePyProtoImpl;
  1523. MutableCProtoInsidePyProtoPtr = MutableCProtoInsidePyProtoImpl;
  1524. }
  1525. }
  1526. } // namespace python
  1527. } // namespace protobuf
  1528. } // namespace google