1 // Protocol Buffers - Google's data interchange format
2 // Copyright 2008 Google Inc. All rights reserved.
3 // http://code.google.com/p/protobuf/
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are
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
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.
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.
31 // Author: petar@google.com (Petar Petrov)
36 #include <google/protobuf/pyext/python_descriptor.h>
37 #include <google/protobuf/descriptor.pb.h>
39 #define C(str) const_cast<char*>(str)
46 static void CFieldDescriptorDealloc(CFieldDescriptor* self);
48 static google::protobuf::DescriptorPool* g_descriptor_pool = NULL;
50 static PyObject* CFieldDescriptor_GetFullName(
51 CFieldDescriptor* self, void *closure) {
52 Py_XINCREF(self->full_name);
53 return self->full_name;
56 static PyObject* CFieldDescriptor_GetName(
57 CFieldDescriptor *self, void *closure) {
58 Py_XINCREF(self->name);
62 static PyObject* CFieldDescriptor_GetCppType(
63 CFieldDescriptor *self, void *closure) {
64 Py_XINCREF(self->cpp_type);
65 return self->cpp_type;
68 static PyObject* CFieldDescriptor_GetLabel(
69 CFieldDescriptor *self, void *closure) {
70 Py_XINCREF(self->label);
74 static PyObject* CFieldDescriptor_GetID(
75 CFieldDescriptor *self, void *closure) {
81 static PyGetSetDef CFieldDescriptorGetters[] = {
83 (getter)CFieldDescriptor_GetFullName, NULL, "Full name", NULL},
85 (getter)CFieldDescriptor_GetName, NULL, "last name", NULL},
87 (getter)CFieldDescriptor_GetCppType, NULL, "C++ Type", NULL},
89 (getter)CFieldDescriptor_GetLabel, NULL, "Label", NULL},
91 (getter)CFieldDescriptor_GetID, NULL, "ID", NULL},
95 PyTypeObject CFieldDescriptor_Type = {
96 PyObject_HEAD_INIT(&PyType_Type)
98 C("google.protobuf.internal."
99 "_net_proto2___python."
100 "CFieldDescriptor"), // tp_name
101 sizeof(CFieldDescriptor), // tp_basicsize
103 (destructor)CFieldDescriptorDealloc, // tp_dealloc
118 Py_TPFLAGS_DEFAULT, // tp_flags
119 C("A Field Descriptor"), // tp_doc
123 0, // tp_weaklistoffset
128 CFieldDescriptorGetters, // tp_getset
135 PyType_GenericAlloc, // tp_alloc
136 PyType_GenericNew, // tp_new
137 PyObject_Del, // tp_free
140 static void CFieldDescriptorDealloc(CFieldDescriptor* self) {
141 Py_DECREF(self->full_name);
142 Py_DECREF(self->name);
143 Py_DECREF(self->cpp_type);
144 Py_DECREF(self->label);
146 self->ob_type->tp_free(reinterpret_cast<PyObject*>(self));
152 const google::protobuf::DescriptorPool* pool;
155 static void CDescriptorPoolDealloc(CDescriptorPool* self);
157 static PyObject* CDescriptorPool_NewCDescriptor(
158 const google::protobuf::FieldDescriptor* field_descriptor) {
159 CFieldDescriptor* cfield_descriptor = PyObject_New(
160 CFieldDescriptor, &CFieldDescriptor_Type);
161 if (cfield_descriptor == NULL) {
164 cfield_descriptor->descriptor = field_descriptor;
166 cfield_descriptor->full_name = PyString_FromString(
167 field_descriptor->full_name().c_str());
168 cfield_descriptor->name = PyString_FromString(
169 field_descriptor->name().c_str());
170 cfield_descriptor->cpp_type = PyLong_FromLong(field_descriptor->cpp_type());
171 cfield_descriptor->label = PyLong_FromLong(field_descriptor->label());
172 cfield_descriptor->id = PyLong_FromVoidPtr(cfield_descriptor);
173 return reinterpret_cast<PyObject*>(cfield_descriptor);
176 static PyObject* CDescriptorPool_FindFieldByName(
177 CDescriptorPool* self, PyObject* arg) {
178 const char* full_field_name = PyString_AsString(arg);
179 if (full_field_name == NULL) {
183 const google::protobuf::FieldDescriptor* field_descriptor = NULL;
185 field_descriptor = self->pool->FindFieldByName(full_field_name);
188 if (field_descriptor == NULL) {
189 PyErr_Format(PyExc_TypeError, "Couldn't find field %.200s",
194 return CDescriptorPool_NewCDescriptor(field_descriptor);
197 static PyObject* CDescriptorPool_FindExtensionByName(
198 CDescriptorPool* self, PyObject* arg) {
199 const char* full_field_name = PyString_AsString(arg);
200 if (full_field_name == NULL) {
204 const google::protobuf::FieldDescriptor* field_descriptor =
205 self->pool->FindExtensionByName(full_field_name);
206 if (field_descriptor == NULL) {
207 PyErr_Format(PyExc_TypeError, "Couldn't find field %.200s",
212 return CDescriptorPool_NewCDescriptor(field_descriptor);
215 static PyMethodDef CDescriptorPoolMethods[] = {
216 { C("FindFieldByName"),
217 (PyCFunction)CDescriptorPool_FindFieldByName,
219 C("Searches for a field descriptor by full name.") },
220 { C("FindExtensionByName"),
221 (PyCFunction)CDescriptorPool_FindExtensionByName,
223 C("Searches for extension descriptor by full name.") },
227 PyTypeObject CDescriptorPool_Type = {
228 PyObject_HEAD_INIT(&PyType_Type)
230 C("google.protobuf.internal."
231 "_net_proto2___python."
232 "CFieldDescriptor"), // tp_name
233 sizeof(CDescriptorPool), // tp_basicsize
235 (destructor)CDescriptorPoolDealloc, // tp_dealloc
250 Py_TPFLAGS_DEFAULT, // tp_flags
251 C("A Descriptor Pool"), // tp_doc
255 0, // tp_weaklistoffset
258 CDescriptorPoolMethods, // tp_methods
267 PyType_GenericAlloc, // tp_alloc
268 PyType_GenericNew, // tp_new
269 PyObject_Del, // tp_free
272 static void CDescriptorPoolDealloc(CDescriptorPool* self) {
273 self->ob_type->tp_free(reinterpret_cast<PyObject*>(self));
276 google::protobuf::DescriptorPool* GetDescriptorPool() {
277 if (g_descriptor_pool == NULL) {
278 g_descriptor_pool = new google::protobuf::DescriptorPool(
279 google::protobuf::DescriptorPool::generated_pool());
281 return g_descriptor_pool;
284 PyObject* Python_NewCDescriptorPool(PyObject* ignored, PyObject* args) {
285 CDescriptorPool* cdescriptor_pool = PyObject_New(
286 CDescriptorPool, &CDescriptorPool_Type);
287 if (cdescriptor_pool == NULL) {
290 cdescriptor_pool->pool = GetDescriptorPool();
291 return reinterpret_cast<PyObject*>(cdescriptor_pool);
294 PyObject* Python_BuildFile(PyObject* ignored, PyObject* arg) {
296 Py_ssize_t message_len;
298 if (PyString_AsStringAndSize(arg, &message_type, &message_len) < 0) {
302 google::protobuf::FileDescriptorProto file_proto;
303 if (!file_proto.ParseFromArray(message_type, message_len)) {
304 PyErr_SetString(PyExc_TypeError, "Couldn't parse file content!");
308 if (google::protobuf::DescriptorPool::generated_pool()->FindFileByName(
309 file_proto.name()) != NULL) {
313 const google::protobuf::FileDescriptor* descriptor = GetDescriptorPool()->BuildFile(
315 if (descriptor == NULL) {
316 PyErr_SetString(PyExc_TypeError,
317 "Couldn't build proto file into descriptor pool!");
324 bool InitDescriptor() {
325 CFieldDescriptor_Type.tp_new = PyType_GenericNew;
326 if (PyType_Ready(&CFieldDescriptor_Type) < 0)
329 CDescriptorPool_Type.tp_new = PyType_GenericNew;
330 if (PyType_Ready(&CDescriptorPool_Type) < 0)
335 } // namespace python
336 } // namespace protobuf
337 } // namespace google