extension_dict.cc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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. // Author: anuraag@google.com (Anuraag Agrawal)
  31. // Author: tibell@google.com (Johan Tibell)
  32. #include <google/protobuf/pyext/extension_dict.h>
  33. #include <google/protobuf/stubs/common.h>
  34. #include <google/protobuf/descriptor.h>
  35. #include <google/protobuf/dynamic_message.h>
  36. #include <google/protobuf/message.h>
  37. #include <google/protobuf/pyext/descriptor.h>
  38. #include <google/protobuf/pyext/descriptor_pool.h>
  39. #include <google/protobuf/pyext/message.h>
  40. #include <google/protobuf/pyext/repeated_composite_container.h>
  41. #include <google/protobuf/pyext/repeated_scalar_container.h>
  42. #include <google/protobuf/pyext/scoped_pyobject_ptr.h>
  43. #include <google/protobuf/stubs/shared_ptr.h>
  44. namespace google {
  45. namespace protobuf {
  46. namespace python {
  47. namespace extension_dict {
  48. // TODO(tibell): Always use self->message for clarity, just like in
  49. // RepeatedCompositeContainer.
  50. static Message* GetMessage(ExtensionDict* self) {
  51. if (self->parent != NULL) {
  52. return self->parent->message;
  53. } else {
  54. return self->message;
  55. }
  56. }
  57. PyObject* len(ExtensionDict* self) {
  58. #if PY_MAJOR_VERSION >= 3
  59. return PyLong_FromLong(PyDict_Size(self->values));
  60. #else
  61. return PyInt_FromLong(PyDict_Size(self->values));
  62. #endif
  63. }
  64. // TODO(tibell): Use VisitCompositeField.
  65. int ReleaseExtension(ExtensionDict* self,
  66. PyObject* extension,
  67. const FieldDescriptor* descriptor) {
  68. if (descriptor->label() == FieldDescriptor::LABEL_REPEATED) {
  69. if (descriptor->cpp_type() == FieldDescriptor::CPPTYPE_MESSAGE) {
  70. if (repeated_composite_container::Release(
  71. reinterpret_cast<RepeatedCompositeContainer*>(
  72. extension)) < 0) {
  73. return -1;
  74. }
  75. } else {
  76. if (repeated_scalar_container::Release(
  77. reinterpret_cast<RepeatedScalarContainer*>(
  78. extension)) < 0) {
  79. return -1;
  80. }
  81. }
  82. } else if (descriptor->cpp_type() == FieldDescriptor::CPPTYPE_MESSAGE) {
  83. if (cmessage::ReleaseSubMessage(
  84. GetMessage(self), descriptor,
  85. reinterpret_cast<CMessage*>(extension)) < 0) {
  86. return -1;
  87. }
  88. }
  89. return 0;
  90. }
  91. PyObject* subscript(ExtensionDict* self, PyObject* key) {
  92. const FieldDescriptor* descriptor = cmessage::GetExtensionDescriptor(key);
  93. if (descriptor == NULL) {
  94. return NULL;
  95. }
  96. if (!CheckFieldBelongsToMessage(descriptor, self->parent->message)) {
  97. return NULL;
  98. }
  99. if (descriptor->label() != FieldDescriptor::LABEL_REPEATED &&
  100. descriptor->cpp_type() != FieldDescriptor::CPPTYPE_MESSAGE) {
  101. return cmessage::InternalGetScalar(self->parent, descriptor);
  102. }
  103. PyObject* value = PyDict_GetItem(self->values, key);
  104. if (value != NULL) {
  105. Py_INCREF(value);
  106. return value;
  107. }
  108. if (descriptor->label() != FieldDescriptor::LABEL_REPEATED &&
  109. descriptor->cpp_type() == FieldDescriptor::CPPTYPE_MESSAGE) {
  110. PyObject* sub_message = cmessage::InternalGetSubMessage(
  111. self->parent, descriptor);
  112. if (sub_message == NULL) {
  113. return NULL;
  114. }
  115. PyDict_SetItem(self->values, key, sub_message);
  116. return sub_message;
  117. }
  118. if (descriptor->label() == FieldDescriptor::LABEL_REPEATED) {
  119. if (descriptor->cpp_type() == FieldDescriptor::CPPTYPE_MESSAGE) {
  120. PyObject *message_class = cdescriptor_pool::GetMessageClass(
  121. GetDescriptorPool(), descriptor->message_type());
  122. if (message_class == NULL) {
  123. return NULL;
  124. }
  125. PyObject* py_container = repeated_composite_container::NewContainer(
  126. self->parent, descriptor, message_class);
  127. if (py_container == NULL) {
  128. return NULL;
  129. }
  130. PyDict_SetItem(self->values, key, py_container);
  131. return py_container;
  132. } else {
  133. PyObject* py_container = repeated_scalar_container::NewContainer(
  134. self->parent, descriptor);
  135. if (py_container == NULL) {
  136. return NULL;
  137. }
  138. PyDict_SetItem(self->values, key, py_container);
  139. return py_container;
  140. }
  141. }
  142. PyErr_SetString(PyExc_ValueError, "control reached unexpected line");
  143. return NULL;
  144. }
  145. int ass_subscript(ExtensionDict* self, PyObject* key, PyObject* value) {
  146. const FieldDescriptor* descriptor = cmessage::GetExtensionDescriptor(key);
  147. if (descriptor == NULL) {
  148. return -1;
  149. }
  150. if (!CheckFieldBelongsToMessage(descriptor, self->parent->message)) {
  151. return -1;
  152. }
  153. if (descriptor->label() != FieldDescriptor::LABEL_OPTIONAL ||
  154. descriptor->cpp_type() == FieldDescriptor::CPPTYPE_MESSAGE) {
  155. PyErr_SetString(PyExc_TypeError, "Extension is repeated and/or composite "
  156. "type");
  157. return -1;
  158. }
  159. cmessage::AssureWritable(self->parent);
  160. if (cmessage::InternalSetScalar(self->parent, descriptor, value) < 0) {
  161. return -1;
  162. }
  163. // TODO(tibell): We shouldn't write scalars to the cache.
  164. PyDict_SetItem(self->values, key, value);
  165. return 0;
  166. }
  167. PyObject* ClearExtension(ExtensionDict* self, PyObject* extension) {
  168. const FieldDescriptor* descriptor =
  169. cmessage::GetExtensionDescriptor(extension);
  170. if (descriptor == NULL) {
  171. return NULL;
  172. }
  173. PyObject* value = PyDict_GetItem(self->values, extension);
  174. if (value != NULL) {
  175. if (ReleaseExtension(self, value, descriptor) < 0) {
  176. return NULL;
  177. }
  178. }
  179. if (cmessage::ClearFieldByDescriptor(self->parent, descriptor) == NULL) {
  180. return NULL;
  181. }
  182. if (PyDict_DelItem(self->values, extension) < 0) {
  183. PyErr_Clear();
  184. }
  185. Py_RETURN_NONE;
  186. }
  187. PyObject* HasExtension(ExtensionDict* self, PyObject* extension) {
  188. const FieldDescriptor* descriptor =
  189. cmessage::GetExtensionDescriptor(extension);
  190. if (descriptor == NULL) {
  191. return NULL;
  192. }
  193. PyObject* result = cmessage::HasFieldByDescriptor(self->parent, descriptor);
  194. return result;
  195. }
  196. PyObject* _FindExtensionByName(ExtensionDict* self, PyObject* name) {
  197. ScopedPyObjectPtr extensions_by_name(PyObject_GetAttrString(
  198. reinterpret_cast<PyObject*>(self->parent), "_extensions_by_name"));
  199. if (extensions_by_name == NULL) {
  200. return NULL;
  201. }
  202. PyObject* result = PyDict_GetItem(extensions_by_name, name);
  203. if (result == NULL) {
  204. Py_RETURN_NONE;
  205. } else {
  206. Py_INCREF(result);
  207. return result;
  208. }
  209. }
  210. ExtensionDict* NewExtensionDict(CMessage *parent) {
  211. ExtensionDict* self = reinterpret_cast<ExtensionDict*>(
  212. PyType_GenericAlloc(&ExtensionDict_Type, 0));
  213. if (self == NULL) {
  214. return NULL;
  215. }
  216. self->parent = parent; // Store a borrowed reference.
  217. self->message = parent->message;
  218. self->owner = parent->owner;
  219. self->values = PyDict_New();
  220. return self;
  221. }
  222. void dealloc(ExtensionDict* self) {
  223. Py_CLEAR(self->values);
  224. self->owner.reset();
  225. Py_TYPE(self)->tp_free(reinterpret_cast<PyObject*>(self));
  226. }
  227. static PyMappingMethods MpMethods = {
  228. (lenfunc)len, /* mp_length */
  229. (binaryfunc)subscript, /* mp_subscript */
  230. (objobjargproc)ass_subscript,/* mp_ass_subscript */
  231. };
  232. #define EDMETHOD(name, args, doc) { #name, (PyCFunction)name, args, doc }
  233. static PyMethodDef Methods[] = {
  234. EDMETHOD(ClearExtension, METH_O, "Clears an extension from the object."),
  235. EDMETHOD(HasExtension, METH_O, "Checks if the object has an extension."),
  236. EDMETHOD(_FindExtensionByName, METH_O,
  237. "Finds an extension by name."),
  238. { NULL, NULL }
  239. };
  240. } // namespace extension_dict
  241. PyTypeObject ExtensionDict_Type = {
  242. PyVarObject_HEAD_INIT(&PyType_Type, 0)
  243. "google.protobuf.internal."
  244. "cpp._message.ExtensionDict", // tp_name
  245. sizeof(ExtensionDict), // tp_basicsize
  246. 0, // tp_itemsize
  247. (destructor)extension_dict::dealloc, // tp_dealloc
  248. 0, // tp_print
  249. 0, // tp_getattr
  250. 0, // tp_setattr
  251. 0, // tp_compare
  252. 0, // tp_repr
  253. 0, // tp_as_number
  254. 0, // tp_as_sequence
  255. &extension_dict::MpMethods, // tp_as_mapping
  256. 0, // tp_hash
  257. 0, // tp_call
  258. 0, // tp_str
  259. 0, // tp_getattro
  260. 0, // tp_setattro
  261. 0, // tp_as_buffer
  262. Py_TPFLAGS_DEFAULT, // tp_flags
  263. "An extension dict", // tp_doc
  264. 0, // tp_traverse
  265. 0, // tp_clear
  266. 0, // tp_richcompare
  267. 0, // tp_weaklistoffset
  268. 0, // tp_iter
  269. 0, // tp_iternext
  270. extension_dict::Methods, // tp_methods
  271. 0, // tp_members
  272. 0, // tp_getset
  273. 0, // tp_base
  274. 0, // tp_dict
  275. 0, // tp_descr_get
  276. 0, // tp_descr_set
  277. 0, // tp_dictoffset
  278. 0, // tp_init
  279. };
  280. } // namespace python
  281. } // namespace protobuf
  282. } // namespace google