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