• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
31 #include <google/protobuf/pyext/field.h>
32 
33 #include <google/protobuf/descriptor.h>
34 #include <google/protobuf/pyext/descriptor.h>
35 #include <google/protobuf/pyext/message.h>
36 
37 namespace google {
38 namespace protobuf {
39 namespace python {
40 
41 namespace field {
42 
Repr(PyMessageFieldProperty * self)43 static PyObject* Repr(PyMessageFieldProperty* self) {
44   return PyUnicode_FromFormat("<field property '%s'>",
45                               self->field_descriptor->full_name().c_str());
46 }
47 
DescrGet(PyMessageFieldProperty * self,PyObject * obj,PyObject * type)48 static PyObject* DescrGet(PyMessageFieldProperty* self, PyObject* obj,
49                           PyObject* type) {
50   if (obj == nullptr) {
51     Py_INCREF(self);
52     return reinterpret_cast<PyObject*>(self);
53   }
54   return cmessage::GetFieldValue(reinterpret_cast<CMessage*>(obj),
55                                  self->field_descriptor);
56 }
57 
DescrSet(PyMessageFieldProperty * self,PyObject * obj,PyObject * value)58 static int DescrSet(PyMessageFieldProperty* self, PyObject* obj,
59                     PyObject* value) {
60   if (value == nullptr) {
61     PyErr_SetString(PyExc_AttributeError, "Cannot delete field attribute");
62     return -1;
63   }
64   return cmessage::SetFieldValue(reinterpret_cast<CMessage*>(obj),
65                                  self->field_descriptor, value);
66 }
67 
GetDescriptor(PyMessageFieldProperty * self,void * closure)68 static PyObject* GetDescriptor(PyMessageFieldProperty* self, void* closure) {
69   return PyFieldDescriptor_FromDescriptor(self->field_descriptor);
70 }
71 
GetDoc(PyMessageFieldProperty * self,void * closure)72 static PyObject* GetDoc(PyMessageFieldProperty* self, void* closure) {
73   return PyUnicode_FromFormat("Field %s",
74                               self->field_descriptor->full_name().c_str());
75 }
76 
77 static PyGetSetDef Getters[] = {
78     {"DESCRIPTOR", (getter)GetDescriptor, nullptr, "Field descriptor"},
79     {"__doc__", (getter)GetDoc, nullptr, nullptr},
80     {nullptr},
81 };
82 }  // namespace field
83 
84 static PyTypeObject _CFieldProperty_Type = {
85     PyVarObject_HEAD_INIT(&PyType_Type, 0)  // head
86     FULL_MODULE_NAME ".FieldProperty",      // tp_name
87     sizeof(PyMessageFieldProperty),         // tp_basicsize
88     0,                                      // tp_itemsize
89     nullptr,                                // tp_dealloc
90 #if PY_VERSION_HEX < 0x03080000
91     nullptr,  // tp_print
92 #else
93     0,  // tp_vectorcall_offset
94 #endif
95     nullptr,                        // tp_getattr
96     nullptr,                        // tp_setattr
97     nullptr,                        // tp_compare
98     (reprfunc)field::Repr,          // tp_repr
99     nullptr,                        // tp_as_number
100     nullptr,                        // tp_as_sequence
101     nullptr,                        // tp_as_mapping
102     nullptr,                        // tp_hash
103     nullptr,                        // tp_call
104     nullptr,                        // tp_str
105     nullptr,                        // tp_getattro
106     nullptr,                        // tp_setattro
107     nullptr,                        // tp_as_buffer
108     Py_TPFLAGS_DEFAULT,             // tp_flags
109     "Field property of a Message",  // tp_doc
110     nullptr,                        // tp_traverse
111     nullptr,                        // tp_clear
112     nullptr,                        // tp_richcompare
113     0,                              // tp_weaklistoffset
114     nullptr,                        // tp_iter
115     nullptr,                        // tp_iternext
116     nullptr,                        // tp_methods
117     nullptr,                        // tp_members
118     field::Getters,                 // tp_getset
119     nullptr,                        // tp_base
120     nullptr,                        // tp_dict
121     (descrgetfunc)field::DescrGet,  // tp_descr_get
122     (descrsetfunc)field::DescrSet,  // tp_descr_set
123     0,                              // tp_dictoffset
124     nullptr,                        // tp_init
125     nullptr,                        // tp_alloc
126     nullptr,                        // tp_new
127 };
128 PyTypeObject* CFieldProperty_Type = &_CFieldProperty_Type;
129 
NewFieldProperty(const FieldDescriptor * field_descriptor)130 PyObject* NewFieldProperty(const FieldDescriptor* field_descriptor) {
131   // Create a new descriptor object
132   PyMessageFieldProperty* property =
133       PyObject_New(PyMessageFieldProperty, CFieldProperty_Type);
134   if (property == nullptr) {
135     return nullptr;
136   }
137   property->field_descriptor = field_descriptor;
138   return reinterpret_cast<PyObject*>(property);
139 }
140 
141 }  // namespace python
142 }  // namespace protobuf
143 }  // namespace google
144