protobuf/python/google/protobuf/pyext/field.cc
Go to the documentation of this file.
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 
43 static PyObject* Repr(PyMessageFieldProperty* self) {
44  return PyUnicode_FromFormat("<field property '%s'>",
45  self->field_descriptor->full_name().c_str());
46 }
47 
48 static PyObject* DescrGet(PyMessageFieldProperty* self, PyObject* obj,
49  PyObject* type) {
50  if (obj == NULL) {
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 
58 static int DescrSet(PyMessageFieldProperty* self, PyObject* obj,
59  PyObject* value) {
60  if (value == NULL) {
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 
68 static PyObject* GetDescriptor(PyMessageFieldProperty* self, void* closure) {
69  return PyFieldDescriptor_FromDescriptor(self->field_descriptor);
70 }
71 
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, NULL, "Field descriptor"},
79  {"__doc__", (getter)GetDoc, NULL, NULL},
80  {NULL}};
81 } // namespace field
82 
83 static PyTypeObject _CFieldProperty_Type = {
84  PyVarObject_HEAD_INIT(&PyType_Type, 0) // head
85  FULL_MODULE_NAME ".FieldProperty", // tp_name
86  sizeof(PyMessageFieldProperty), // tp_basicsize
87  0, // tp_itemsize
88  0, // tp_dealloc
89  0, // tp_print
90  0, // tp_getattr
91  0, // tp_setattr
92  0, // tp_compare
93  (reprfunc)field::Repr, // tp_repr
94  0, // tp_as_number
95  0, // tp_as_sequence
96  0, // tp_as_mapping
97  0, // tp_hash
98  0, // tp_call
99  0, // tp_str
100  0, // tp_getattro
101  0, // tp_setattro
102  0, // tp_as_buffer
103  Py_TPFLAGS_DEFAULT, // tp_flags
104  "Field property of a Message", // tp_doc
105  0, // tp_traverse
106  0, // tp_clear
107  0, // tp_richcompare
108  0, // tp_weaklistoffset
109  0, // tp_iter
110  0, // tp_iternext
111  0, // tp_methods
112  0, // tp_members
113  field::Getters, // tp_getset
114  0, // tp_base
115  0, // tp_dict
116  (descrgetfunc)field::DescrGet, // tp_descr_get
117  (descrsetfunc)field::DescrSet, // tp_descr_set
118  0, // tp_dictoffset
119  0, // tp_init
120  0, // tp_alloc
121  0, // tp_new
122 };
124 
125 PyObject* NewFieldProperty(const FieldDescriptor* field_descriptor) {
126  // Create a new descriptor object
127  PyMessageFieldProperty* property =
128  PyObject_New(PyMessageFieldProperty, CFieldProperty_Type);
129  if (property == NULL) {
130  return NULL;
131  }
132  property->field_descriptor = field_descriptor;
133  return reinterpret_cast<PyObject*>(property);
134 }
135 
136 } // namespace python
137 } // namespace protobuf
138 } // namespace google
obj
OPENSSL_EXPORT const ASN1_OBJECT * obj
Definition: x509.h:1671
google::protobuf::value
const Descriptor::ReservedRange value
Definition: bloaty/third_party/protobuf/src/google/protobuf/descriptor.h:1954
google::protobuf::FieldDescriptor
Definition: bloaty/third_party/protobuf/src/google/protobuf/descriptor.h:515
google::protobuf::python::CMessage
Definition: bloaty/third_party/protobuf/python/google/protobuf/pyext/message.h:100
google::protobuf
Definition: bloaty/third_party/protobuf/benchmarks/util/data_proto2_to_proto3_util.h:12
FULL_MODULE_NAME
#define FULL_MODULE_NAME
Definition: bloaty/third_party/protobuf/python/google/protobuf/pyext/message.h:331
google::protobuf::python::field::Getters
static PyGetSetDef Getters[]
Definition: bloaty/third_party/protobuf/python/google/protobuf/pyext/field.cc:81
google::protobuf::python::PyFieldDescriptor_FromDescriptor
PyObject * PyFieldDescriptor_FromDescriptor(const FieldDescriptor *field_descriptor)
Definition: bloaty/third_party/protobuf/python/google/protobuf/pyext/descriptor.cc:1037
google::protobuf::python::field::GetDoc
static PyObject * GetDoc(PyMessageFieldProperty *self, void *closure)
Definition: bloaty/third_party/protobuf/python/google/protobuf/pyext/field.cc:76
google::protobuf::python::field::GetDescriptor
static PyObject * GetDescriptor(PyMessageFieldProperty *self, void *closure)
Definition: bloaty/third_party/protobuf/python/google/protobuf/pyext/field.cc:72
google::protobuf::python::CFieldProperty_Type
PyTypeObject * CFieldProperty_Type
Definition: bloaty/third_party/protobuf/python/google/protobuf/pyext/field.cc:127
google::protobuf::python::_CFieldProperty_Type
static PyTypeObject _CFieldProperty_Type
Definition: bloaty/third_party/protobuf/python/google/protobuf/pyext/field.cc:87
google::protobuf::python::cmessage::SetFieldValue
int SetFieldValue(CMessage *self, const FieldDescriptor *field_descriptor, PyObject *value)
Definition: bloaty/third_party/protobuf/python/google/protobuf/pyext/message.cc:2705
field
const FieldDescriptor * field
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/parser_unittest.cc:2692
google::protobuf::python::NewFieldProperty
PyObject * NewFieldProperty(const FieldDescriptor *field_descriptor)
Definition: bloaty/third_party/protobuf/python/google/protobuf/pyext/field.cc:129
google::protobuf::python::PyMessageFieldProperty
Definition: bloaty/third_party/protobuf/python/google/protobuf/pyext/field.h:44
google::protobuf::python::field::Repr
static PyObject * Repr(PyMessageFieldProperty *self)
Definition: bloaty/third_party/protobuf/python/google/protobuf/pyext/field.cc:47
closure
Definition: proxy.cc:59
google::protobuf::python::field::DescrSet
static int DescrSet(PyMessageFieldProperty *self, PyObject *obj, PyObject *value)
Definition: bloaty/third_party/protobuf/python/google/protobuf/pyext/field.cc:62
google::protobuf::python::field::DescrGet
static PyObject * DescrGet(PyMessageFieldProperty *self, PyObject *obj, PyObject *type)
Definition: bloaty/third_party/protobuf/python/google/protobuf/pyext/field.cc:52
PyVarObject_HEAD_INIT
#define PyVarObject_HEAD_INIT(type, size)
Definition: bloaty/third_party/protobuf/python/google/protobuf/pyext/descriptor.cc:161
asyncio_get_stats.type
type
Definition: asyncio_get_stats.py:37
self
PHP_PROTO_OBJECT_FREE_END PHP_PROTO_OBJECT_DTOR_END intern self
Definition: bloaty/third_party/protobuf/php/ext/google/protobuf/map.c:543
google::protobuf::python::cmessage::GetFieldValue
PyObject * GetFieldValue(CMessage *self, const FieldDescriptor *field_descriptor)
Definition: bloaty/third_party/protobuf/python/google/protobuf/pyext/message.cc:2635
google
Definition: bloaty/third_party/protobuf/benchmarks/util/data_proto2_to_proto3_util.h:11


grpc
Author(s):
autogenerated on Fri May 16 2025 02:58:22