unknown_fields.cc 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. // Protocol Buffers - Google's data interchange format
  2. // Copyright 2008 Google Inc. All rights reserved.
  3. // https://developers.google.com/protocol-buffers/
  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. #include <google/protobuf/pyext/unknown_fields.h>
  31. #include <Python.h>
  32. #include <set>
  33. #include <memory>
  34. #include <google/protobuf/message.h>
  35. #include <google/protobuf/pyext/message.h>
  36. #include <google/protobuf/pyext/scoped_pyobject_ptr.h>
  37. #include <google/protobuf/unknown_field_set.h>
  38. #include <google/protobuf/wire_format_lite.h>
  39. #if PY_MAJOR_VERSION >= 3
  40. #define PyInt_FromLong PyLong_FromLong
  41. #endif
  42. namespace google {
  43. namespace protobuf {
  44. namespace python {
  45. namespace unknown_fields {
  46. static Py_ssize_t Len(PyObject* pself) {
  47. PyUnknownFields* self =
  48. reinterpret_cast<PyUnknownFields*>(pself);
  49. if (self->fields == NULL) {
  50. PyErr_Format(PyExc_ValueError,
  51. "UnknownFields does not exist. "
  52. "The parent message might be cleared.");
  53. return -1;
  54. }
  55. return self->fields->field_count();
  56. }
  57. void Clear(PyUnknownFields* self) {
  58. for (std::set<PyUnknownFields*>::iterator it =
  59. self->sub_unknown_fields.begin();
  60. it != self->sub_unknown_fields.end(); it++) {
  61. Clear(*it);
  62. }
  63. self->fields = NULL;
  64. self->sub_unknown_fields.clear();
  65. }
  66. PyObject* NewPyUnknownFieldRef(PyUnknownFields* parent,
  67. Py_ssize_t index);
  68. static PyObject* Item(PyObject* pself, Py_ssize_t index) {
  69. PyUnknownFields* self =
  70. reinterpret_cast<PyUnknownFields*>(pself);
  71. if (self->fields == NULL) {
  72. PyErr_Format(PyExc_ValueError,
  73. "UnknownFields does not exist. "
  74. "The parent message might be cleared.");
  75. return NULL;
  76. }
  77. Py_ssize_t total_size = self->fields->field_count();
  78. if (index < 0) {
  79. index = total_size + index;
  80. }
  81. if (index < 0 || index >= total_size) {
  82. PyErr_Format(PyExc_IndexError,
  83. "index (%zd) out of range",
  84. index);
  85. return NULL;
  86. }
  87. return unknown_fields::NewPyUnknownFieldRef(self, index);
  88. }
  89. PyObject* NewPyUnknownFields(CMessage* c_message) {
  90. PyUnknownFields* self = reinterpret_cast<PyUnknownFields*>(
  91. PyType_GenericAlloc(&PyUnknownFields_Type, 0));
  92. if (self == NULL) {
  93. return NULL;
  94. }
  95. // Call "placement new" to initialize PyUnknownFields.
  96. new (self) PyUnknownFields;
  97. Py_INCREF(c_message);
  98. self->parent = reinterpret_cast<PyObject*>(c_message);
  99. Message* message = c_message->message;
  100. const Reflection* reflection = message->GetReflection();
  101. self->fields = &reflection->GetUnknownFields(*message);
  102. return reinterpret_cast<PyObject*>(self);
  103. }
  104. PyObject* NewPyUnknownFieldRef(PyUnknownFields* parent,
  105. Py_ssize_t index) {
  106. PyUnknownFieldRef* self = reinterpret_cast<PyUnknownFieldRef*>(
  107. PyType_GenericAlloc(&PyUnknownFieldRef_Type, 0));
  108. if (self == NULL) {
  109. return NULL;
  110. }
  111. Py_INCREF(parent);
  112. self->parent = parent;
  113. self->index = index;
  114. return reinterpret_cast<PyObject*>(self);
  115. }
  116. static void Dealloc(PyObject* pself) {
  117. PyUnknownFields* self =
  118. reinterpret_cast<PyUnknownFields*>(pself);
  119. if (PyObject_TypeCheck(self->parent, &PyUnknownFields_Type)) {
  120. reinterpret_cast<PyUnknownFields*>(
  121. self->parent)->sub_unknown_fields.erase(self);
  122. } else {
  123. reinterpret_cast<CMessage*>(self->parent)->unknown_field_set = nullptr;
  124. }
  125. Py_CLEAR(self->parent);
  126. self->~PyUnknownFields();
  127. }
  128. static PySequenceMethods SqMethods = {
  129. Len, /* sq_length */
  130. 0, /* sq_concat */
  131. 0, /* sq_repeat */
  132. Item, /* sq_item */
  133. 0, /* sq_slice */
  134. 0, /* sq_ass_item */
  135. };
  136. } // namespace unknown_fields
  137. PyTypeObject PyUnknownFields_Type = {
  138. PyVarObject_HEAD_INIT(&PyType_Type, 0)
  139. FULL_MODULE_NAME ".PyUnknownFields", // tp_name
  140. sizeof(PyUnknownFields), // tp_basicsize
  141. 0, // tp_itemsize
  142. unknown_fields::Dealloc, // tp_dealloc
  143. 0, // tp_print
  144. 0, // tp_getattr
  145. 0, // tp_setattr
  146. 0, // tp_compare
  147. 0, // tp_repr
  148. 0, // tp_as_number
  149. &unknown_fields::SqMethods, // tp_as_sequence
  150. 0, // tp_as_mapping
  151. PyObject_HashNotImplemented, // tp_hash
  152. 0, // tp_call
  153. 0, // tp_str
  154. 0, // tp_getattro
  155. 0, // tp_setattro
  156. 0, // tp_as_buffer
  157. Py_TPFLAGS_DEFAULT, // tp_flags
  158. "unknown field set", // tp_doc
  159. 0, // tp_traverse
  160. 0, // tp_clear
  161. 0, // tp_richcompare
  162. 0, // tp_weaklistoffset
  163. 0, // tp_iter
  164. 0, // tp_iternext
  165. 0, // tp_methods
  166. 0, // tp_members
  167. 0, // tp_getset
  168. 0, // tp_base
  169. 0, // tp_dict
  170. 0, // tp_descr_get
  171. 0, // tp_descr_set
  172. 0, // tp_dictoffset
  173. 0, // tp_init
  174. };
  175. namespace unknown_field {
  176. static PyObject* PyUnknownFields_FromUnknownFieldSet(
  177. PyUnknownFields* parent, const UnknownFieldSet& fields) {
  178. PyUnknownFields* self = reinterpret_cast<PyUnknownFields*>(
  179. PyType_GenericAlloc(&PyUnknownFields_Type, 0));
  180. if (self == NULL) {
  181. return NULL;
  182. }
  183. // Call "placement new" to initialize PyUnknownFields.
  184. new (self) PyUnknownFields;
  185. Py_INCREF(parent);
  186. self->parent = reinterpret_cast<PyObject*>(parent);
  187. self->fields = &fields;
  188. parent->sub_unknown_fields.emplace(self);
  189. return reinterpret_cast<PyObject*>(self);
  190. }
  191. const UnknownField* GetUnknownField(PyUnknownFieldRef* self) {
  192. const UnknownFieldSet* fields = self->parent->fields;
  193. if (fields == NULL) {
  194. PyErr_Format(PyExc_ValueError,
  195. "UnknownField does not exist. "
  196. "The parent message might be cleared.");
  197. return NULL;
  198. }
  199. ssize_t total_size = fields->field_count();
  200. if (self->index >= total_size) {
  201. PyErr_Format(PyExc_ValueError,
  202. "UnknownField does not exist. "
  203. "The parent message might be cleared.");
  204. return NULL;
  205. }
  206. return &fields->field(self->index);
  207. }
  208. static PyObject* GetFieldNumber(PyUnknownFieldRef* self, void *closure) {
  209. const UnknownField* unknown_field = GetUnknownField(self);
  210. if (unknown_field == NULL) {
  211. return NULL;
  212. }
  213. return PyInt_FromLong(unknown_field->number());
  214. }
  215. using internal::WireFormatLite;
  216. static PyObject* GetWireType(PyUnknownFieldRef* self, void *closure) {
  217. const UnknownField* unknown_field = GetUnknownField(self);
  218. if (unknown_field == NULL) {
  219. return NULL;
  220. }
  221. // Assign a default value to suppress may-unintialized warnings (errors
  222. // when built in some places).
  223. WireFormatLite::WireType wire_type = WireFormatLite::WIRETYPE_VARINT;
  224. switch (unknown_field->type()) {
  225. case UnknownField::TYPE_VARINT:
  226. wire_type = WireFormatLite::WIRETYPE_VARINT;
  227. break;
  228. case UnknownField::TYPE_FIXED32:
  229. wire_type = WireFormatLite::WIRETYPE_FIXED32;
  230. break;
  231. case UnknownField::TYPE_FIXED64:
  232. wire_type = WireFormatLite::WIRETYPE_FIXED64;
  233. break;
  234. case UnknownField::TYPE_LENGTH_DELIMITED:
  235. wire_type = WireFormatLite::WIRETYPE_LENGTH_DELIMITED;
  236. break;
  237. case UnknownField::TYPE_GROUP:
  238. wire_type = WireFormatLite::WIRETYPE_START_GROUP;
  239. break;
  240. }
  241. return PyInt_FromLong(wire_type);
  242. }
  243. static PyObject* GetData(PyUnknownFieldRef* self, void *closure) {
  244. const UnknownField* field = GetUnknownField(self);
  245. if (field == NULL) {
  246. return NULL;
  247. }
  248. PyObject* data = NULL;
  249. switch (field->type()) {
  250. case UnknownField::TYPE_VARINT:
  251. data = PyInt_FromLong(field->varint());
  252. break;
  253. case UnknownField::TYPE_FIXED32:
  254. data = PyInt_FromLong(field->fixed32());
  255. break;
  256. case UnknownField::TYPE_FIXED64:
  257. data = PyInt_FromLong(field->fixed64());
  258. break;
  259. case UnknownField::TYPE_LENGTH_DELIMITED:
  260. data = PyBytes_FromStringAndSize(field->length_delimited().data(),
  261. field->GetLengthDelimitedSize());
  262. break;
  263. case UnknownField::TYPE_GROUP:
  264. data = PyUnknownFields_FromUnknownFieldSet(
  265. self->parent, field->group());
  266. break;
  267. }
  268. return data;
  269. }
  270. static void Dealloc(PyObject* pself) {
  271. PyUnknownFieldRef* self =
  272. reinterpret_cast<PyUnknownFieldRef*>(pself);
  273. Py_CLEAR(self->parent);
  274. }
  275. static PyGetSetDef Getters[] = {
  276. {"field_number", (getter)GetFieldNumber, NULL},
  277. {"wire_type", (getter)GetWireType, NULL},
  278. {"data", (getter)GetData, NULL},
  279. {NULL}
  280. };
  281. } // namespace unknown_field
  282. PyTypeObject PyUnknownFieldRef_Type = {
  283. PyVarObject_HEAD_INIT(&PyType_Type, 0)
  284. FULL_MODULE_NAME ".PyUnknownFieldRef", // tp_name
  285. sizeof(PyUnknownFieldRef), // tp_basicsize
  286. 0, // tp_itemsize
  287. unknown_field::Dealloc, // tp_dealloc
  288. 0, // tp_print
  289. 0, // tp_getattr
  290. 0, // tp_setattr
  291. 0, // tp_compare
  292. 0, // tp_repr
  293. 0, // tp_as_number
  294. 0, // tp_as_sequence
  295. 0, // tp_as_mapping
  296. PyObject_HashNotImplemented, // tp_hash
  297. 0, // tp_call
  298. 0, // tp_str
  299. 0, // tp_getattro
  300. 0, // tp_setattro
  301. 0, // tp_as_buffer
  302. Py_TPFLAGS_DEFAULT, // tp_flags
  303. "unknown field", // tp_doc
  304. 0, // tp_traverse
  305. 0, // tp_clear
  306. 0, // tp_richcompare
  307. 0, // tp_weaklistoffset
  308. 0, // tp_iter
  309. 0, // tp_iternext
  310. 0, // tp_methods
  311. 0, // tp_members
  312. unknown_field::Getters, // tp_getset
  313. 0, // tp_base
  314. 0, // tp_dict
  315. 0, // tp_descr_get
  316. 0, // tp_descr_set
  317. 0, // tp_dictoffset
  318. 0, // tp_init
  319. };
  320. } // namespace python
  321. } // namespace protobuf
  322. } // namespace google