1 // Protocol Buffers - Google's data interchange format
2 // Copyright 2008 Google Inc. All rights reserved.
3 //
4 // Use of this source code is governed by a BSD-style
5 // license that can be found in the LICENSE file or at
6 // https://developers.google.com/open-source/licenses/bsd
7
8 #include "google/protobuf/pyext/field.h"
9
10 #include "google/protobuf/descriptor.h"
11 #include "google/protobuf/pyext/descriptor.h"
12 #include "google/protobuf/pyext/message.h"
13
14 namespace google {
15 namespace protobuf {
16 namespace python {
17
18 namespace field {
19
Repr(PyMessageFieldProperty * self)20 static PyObject* Repr(PyMessageFieldProperty* self) {
21 return PyUnicode_FromFormat(
22 "<field property '%s'>",
23 std::string(self->field_descriptor->full_name()).c_str());
24 }
25
DescrGet(PyMessageFieldProperty * self,PyObject * obj,PyObject * type)26 static PyObject* DescrGet(PyMessageFieldProperty* self, PyObject* obj,
27 PyObject* type) {
28 if (obj == nullptr) {
29 Py_INCREF(self);
30 return reinterpret_cast<PyObject*>(self);
31 }
32 return cmessage::GetFieldValue(reinterpret_cast<CMessage*>(obj),
33 self->field_descriptor);
34 }
35
DescrSet(PyMessageFieldProperty * self,PyObject * obj,PyObject * value)36 static int DescrSet(PyMessageFieldProperty* self, PyObject* obj,
37 PyObject* value) {
38 if (value == nullptr) {
39 PyErr_SetString(PyExc_AttributeError, "Cannot delete field attribute");
40 return -1;
41 }
42 return cmessage::SetFieldValue(reinterpret_cast<CMessage*>(obj),
43 self->field_descriptor, value);
44 }
45
GetDescriptor(PyMessageFieldProperty * self,void * closure)46 static PyObject* GetDescriptor(PyMessageFieldProperty* self, void* closure) {
47 return PyFieldDescriptor_FromDescriptor(self->field_descriptor);
48 }
49
GetDoc(PyMessageFieldProperty * self,void * closure)50 static PyObject* GetDoc(PyMessageFieldProperty* self, void* closure) {
51 return PyUnicode_FromFormat(
52 "Field %s", std::string(self->field_descriptor->full_name()).c_str());
53 }
54
55 static PyGetSetDef Getters[] = {
56 {"DESCRIPTOR", (getter)GetDescriptor, nullptr, "Field descriptor"},
57 {"__doc__", (getter)GetDoc, nullptr, nullptr},
58 {nullptr},
59 };
60 } // namespace field
61
62 static PyTypeObject _CFieldProperty_Type = {
63 PyVarObject_HEAD_INIT(&PyType_Type, 0) // head
64 FULL_MODULE_NAME ".FieldProperty", // tp_name
65 sizeof(PyMessageFieldProperty), // tp_basicsize
66 0, // tp_itemsize
67 nullptr, // tp_dealloc
68 #if PY_VERSION_HEX < 0x03080000
69 nullptr, // tp_print
70 #else
71 0, // tp_vectorcall_offset
72 #endif
73 nullptr, // tp_getattr
74 nullptr, // tp_setattr
75 nullptr, // tp_compare
76 (reprfunc)field::Repr, // tp_repr
77 nullptr, // tp_as_number
78 nullptr, // tp_as_sequence
79 nullptr, // tp_as_mapping
80 nullptr, // tp_hash
81 nullptr, // tp_call
82 nullptr, // tp_str
83 nullptr, // tp_getattro
84 nullptr, // tp_setattro
85 nullptr, // tp_as_buffer
86 Py_TPFLAGS_DEFAULT, // tp_flags
87 "Field property of a Message", // tp_doc
88 nullptr, // tp_traverse
89 nullptr, // tp_clear
90 nullptr, // tp_richcompare
91 0, // tp_weaklistoffset
92 nullptr, // tp_iter
93 nullptr, // tp_iternext
94 nullptr, // tp_methods
95 nullptr, // tp_members
96 field::Getters, // tp_getset
97 nullptr, // tp_base
98 nullptr, // tp_dict
99 (descrgetfunc)field::DescrGet, // tp_descr_get
100 (descrsetfunc)field::DescrSet, // tp_descr_set
101 0, // tp_dictoffset
102 nullptr, // tp_init
103 nullptr, // tp_alloc
104 nullptr, // tp_new
105 };
106 PyTypeObject* CFieldProperty_Type = &_CFieldProperty_Type;
107
NewFieldProperty(const FieldDescriptor * field_descriptor)108 PyObject* NewFieldProperty(const FieldDescriptor* field_descriptor) {
109 // Create a new descriptor object
110 PyMessageFieldProperty* property =
111 PyObject_New(PyMessageFieldProperty, CFieldProperty_Type);
112 if (property == nullptr) {
113 return nullptr;
114 }
115 property->field_descriptor = field_descriptor;
116 return reinterpret_cast<PyObject*>(property);
117 }
118
119 } // namespace python
120 } // namespace protobuf
121 } // namespace google
122