protobuf/python/google/protobuf/pyext/unknown_fields.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/unknown_fields.h>
32 
33 #define PY_SSIZE_T_CLEAN
34 #include <Python.h>
35 #include <set>
36 #include <memory>
37 
38 #include <google/protobuf/message.h>
39 #include <google/protobuf/pyext/message.h>
40 #include <google/protobuf/pyext/scoped_pyobject_ptr.h>
41 #include <google/protobuf/unknown_field_set.h>
42 #include <google/protobuf/wire_format_lite.h>
43 
44 namespace google {
45 namespace protobuf {
46 namespace python {
47 
48 namespace unknown_fields {
49 
50 static Py_ssize_t Len(PyObject* pself) {
51  PyUnknownFields* self =
52  reinterpret_cast<PyUnknownFields*>(pself);
53  if (self->fields == NULL) {
54  PyErr_Format(PyExc_ValueError,
55  "UnknownFields does not exist. "
56  "The parent message might be cleared.");
57  return -1;
58  }
59  return self->fields->field_count();
60 }
61 
62 void Clear(PyUnknownFields* self) {
64  self->sub_unknown_fields.begin();
65  it != self->sub_unknown_fields.end(); it++) {
66  Clear(*it);
67  }
68  self->fields = NULL;
69  self->sub_unknown_fields.clear();
70 }
71 
72 PyObject* NewPyUnknownFieldRef(PyUnknownFields* parent,
73  Py_ssize_t index);
74 
75 static PyObject* Item(PyObject* pself, Py_ssize_t index) {
76  PyUnknownFields* self =
77  reinterpret_cast<PyUnknownFields*>(pself);
78  if (self->fields == NULL) {
79  PyErr_Format(PyExc_ValueError,
80  "UnknownFields does not exist. "
81  "The parent message might be cleared.");
82  return NULL;
83  }
84  Py_ssize_t total_size = self->fields->field_count();
85  if (index < 0) {
86  index = total_size + index;
87  }
88  if (index < 0 || index >= total_size) {
89  PyErr_Format(PyExc_IndexError,
90  "index (%zd) out of range",
91  index);
92  return NULL;
93  }
94 
96 }
97 
98 PyObject* NewPyUnknownFields(CMessage* c_message) {
99  PyUnknownFields* self = reinterpret_cast<PyUnknownFields*>(
100  PyType_GenericAlloc(&PyUnknownFields_Type, 0));
101  if (self == NULL) {
102  return NULL;
103  }
104  // Call "placement new" to initialize PyUnknownFields.
105  new (self) PyUnknownFields;
106 
107  Py_INCREF(c_message);
108  self->parent = reinterpret_cast<PyObject*>(c_message);
109  Message* message = c_message->message;
110  const Reflection* reflection = message->GetReflection();
111  self->fields = &reflection->GetUnknownFields(*message);
112 
113  return reinterpret_cast<PyObject*>(self);
114 }
115 
116 PyObject* NewPyUnknownFieldRef(PyUnknownFields* parent,
117  Py_ssize_t index) {
118  PyUnknownFieldRef* self = reinterpret_cast<PyUnknownFieldRef*>(
119  PyType_GenericAlloc(&PyUnknownFieldRef_Type, 0));
120  if (self == NULL) {
121  return NULL;
122  }
123 
124  Py_INCREF(parent);
125  self->parent = parent;
126  self->index = index;
127 
128  return reinterpret_cast<PyObject*>(self);
129 }
130 
131 static void Dealloc(PyObject* pself) {
132  PyUnknownFields* self =
133  reinterpret_cast<PyUnknownFields*>(pself);
134  if (PyObject_TypeCheck(self->parent, &PyUnknownFields_Type)) {
135  reinterpret_cast<PyUnknownFields*>(
136  self->parent)->sub_unknown_fields.erase(self);
137  } else {
138  reinterpret_cast<CMessage*>(self->parent)->unknown_field_set = nullptr;
139  }
140  Py_CLEAR(self->parent);
141  self->~PyUnknownFields();
142  Py_TYPE(pself)->tp_free(pself);
143 }
144 
145 static PySequenceMethods SqMethods = {
146  Len, /* sq_length */
147  0, /* sq_concat */
148  0, /* sq_repeat */
149  Item, /* sq_item */
150  0, /* sq_slice */
151  0, /* sq_ass_item */
152 };
153 
154 } // namespace unknown_fields
155 
156 PyTypeObject PyUnknownFields_Type = {
157  PyVarObject_HEAD_INIT(&PyType_Type, 0)
158  FULL_MODULE_NAME ".PyUnknownFields", // tp_name
159  sizeof(PyUnknownFields), // tp_basicsize
160  0, // tp_itemsize
161  unknown_fields::Dealloc, // tp_dealloc
162  0, // tp_print
163  0, // tp_getattr
164  0, // tp_setattr
165  0, // tp_compare
166  0, // tp_repr
167  0, // tp_as_number
168  &unknown_fields::SqMethods, // tp_as_sequence
169  0, // tp_as_mapping
170  PyObject_HashNotImplemented, // tp_hash
171  0, // tp_call
172  0, // tp_str
173  0, // tp_getattro
174  0, // tp_setattro
175  0, // tp_as_buffer
176  Py_TPFLAGS_DEFAULT, // tp_flags
177  "unknown field set", // tp_doc
178  0, // tp_traverse
179  0, // tp_clear
180  0, // tp_richcompare
181  0, // tp_weaklistoffset
182  0, // tp_iter
183  0, // tp_iternext
184  0, // tp_methods
185  0, // tp_members
186  0, // tp_getset
187  0, // tp_base
188  0, // tp_dict
189  0, // tp_descr_get
190  0, // tp_descr_set
191  0, // tp_dictoffset
192  0, // tp_init
193 };
194 
195 namespace unknown_field {
197  PyUnknownFields* parent, const UnknownFieldSet& fields) {
198  PyUnknownFields* self = reinterpret_cast<PyUnknownFields*>(
199  PyType_GenericAlloc(&PyUnknownFields_Type, 0));
200  if (self == NULL) {
201  return NULL;
202  }
203  // Call "placement new" to initialize PyUnknownFields.
204  new (self) PyUnknownFields;
205 
206  Py_INCREF(parent);
207  self->parent = reinterpret_cast<PyObject*>(parent);
208  self->fields = &fields;
209  parent->sub_unknown_fields.emplace(self);
210 
211  return reinterpret_cast<PyObject*>(self);
212 }
213 
215  const UnknownFieldSet* fields = self->parent->fields;
216  if (fields == NULL) {
217  PyErr_Format(PyExc_ValueError,
218  "UnknownField does not exist. "
219  "The parent message might be cleared.");
220  return NULL;
221  }
222  Py_ssize_t total_size = fields->field_count();
223  if (self->index >= total_size) {
224  PyErr_Format(PyExc_ValueError,
225  "UnknownField does not exist. "
226  "The parent message might be cleared.");
227  return NULL;
228  }
229  return &fields->field(self->index);
230 }
231 
232 static PyObject* GetFieldNumber(PyUnknownFieldRef* self, void *closure) {
233  const UnknownField* unknown_field = GetUnknownField(self);
234  if (unknown_field == NULL) {
235  return NULL;
236  }
237  return PyLong_FromLong(unknown_field->number());
238 }
239 
241 static PyObject* GetWireType(PyUnknownFieldRef* self, void *closure) {
242  const UnknownField* unknown_field = GetUnknownField(self);
243  if (unknown_field == NULL) {
244  return NULL;
245  }
246 
247  // Assign a default value to suppress may-uninitialized warnings (errors
248  // when built in some places).
250  switch (unknown_field->type()) {
253  break;
256  break;
259  break;
262  break;
265  break;
266  }
267  return PyLong_FromLong(wire_type);
268 }
269 
270 static PyObject* GetData(PyUnknownFieldRef* self, void *closure) {
271  const UnknownField* field = GetUnknownField(self);
272  if (field == NULL) {
273  return NULL;
274  }
275  PyObject* data = NULL;
276  switch (field->type()) {
278  data = PyLong_FromLong(field->varint());
279  break;
281  data = PyLong_FromLong(field->fixed32());
282  break;
284  data = PyLong_FromLong(field->fixed64());
285  break;
287  data = PyBytes_FromStringAndSize(field->length_delimited().data(),
288  field->GetLengthDelimitedSize());
289  break;
292  self->parent, field->group());
293  break;
294  }
295  return data;
296 }
297 
298 static void Dealloc(PyObject* pself) {
299  PyUnknownFieldRef* self =
300  reinterpret_cast<PyUnknownFieldRef*>(pself);
301  Py_CLEAR(self->parent);
302 }
303 
304 static PyGetSetDef Getters[] = {
305  {"field_number", (getter)GetFieldNumber, NULL},
306  {"wire_type", (getter)GetWireType, NULL},
307  {"data", (getter)GetData, NULL},
308  {NULL}
309 };
310 
311 } // namespace unknown_field
312 
313 PyTypeObject PyUnknownFieldRef_Type = {
314  PyVarObject_HEAD_INIT(&PyType_Type, 0)
315  FULL_MODULE_NAME ".PyUnknownFieldRef", // tp_name
316  sizeof(PyUnknownFieldRef), // tp_basicsize
317  0, // tp_itemsize
318  unknown_field::Dealloc, // tp_dealloc
319  0, // tp_print
320  0, // tp_getattr
321  0, // tp_setattr
322  0, // tp_compare
323  0, // tp_repr
324  0, // tp_as_number
325  0, // tp_as_sequence
326  0, // tp_as_mapping
327  PyObject_HashNotImplemented, // tp_hash
328  0, // tp_call
329  0, // tp_str
330  0, // tp_getattro
331  0, // tp_setattro
332  0, // tp_as_buffer
333  Py_TPFLAGS_DEFAULT, // tp_flags
334  "unknown field", // tp_doc
335  0, // tp_traverse
336  0, // tp_clear
337  0, // tp_richcompare
338  0, // tp_weaklistoffset
339  0, // tp_iter
340  0, // tp_iternext
341  0, // tp_methods
342  0, // tp_members
343  unknown_field::Getters, // tp_getset
344  0, // tp_base
345  0, // tp_dict
346  0, // tp_descr_get
347  0, // tp_descr_set
348  0, // tp_dictoffset
349  0, // tp_init
350 };
351 
352 
353 } // namespace python
354 } // namespace protobuf
355 } // namespace google
google::protobuf::UnknownField::type
Type type() const
Definition: bloaty/third_party/protobuf/src/google/protobuf/unknown_field_set.h:327
google::protobuf::python::unknown_fields::NewPyUnknownFields
PyObject * NewPyUnknownFields(CMessage *c_message)
Definition: bloaty/third_party/protobuf/python/google/protobuf/pyext/unknown_fields.cc:101
regen-readme.it
it
Definition: regen-readme.py:15
google::protobuf::python::unknown_fields::Dealloc
static void Dealloc(PyObject *pself)
Definition: bloaty/third_party/protobuf/python/google/protobuf/pyext/unknown_fields.cc:134
google::protobuf::python::unknown_field::GetUnknownField
const UnknownField * GetUnknownField(PyUnknownFieldRef *self)
Definition: bloaty/third_party/protobuf/python/google/protobuf/pyext/unknown_fields.cc:216
google::protobuf::UnknownField::TYPE_GROUP
@ TYPE_GROUP
Definition: bloaty/third_party/protobuf/src/google/protobuf/unknown_field_set.h:223
google::protobuf::python::CMessage
Definition: bloaty/third_party/protobuf/python/google/protobuf/pyext/message.h:100
google::protobuf::python::PyUnknownFields::sub_unknown_fields
std::set< PyUnknownFields * > sub_unknown_fields
Definition: bloaty/third_party/protobuf/python/google/protobuf/pyext/unknown_fields.h:63
google::protobuf.internal::WireFormatLite::WIRETYPE_LENGTH_DELIMITED
@ WIRETYPE_LENGTH_DELIMITED
Definition: bloaty/third_party/protobuf/src/google/protobuf/wire_format_lite.h:104
google::protobuf::python::PyUnknownFieldRef
Definition: bloaty/third_party/protobuf/python/google/protobuf/pyext/unknown_fields.h:66
google::protobuf::python::unknown_fields::Len
static Py_ssize_t Len(PyObject *pself)
Definition: bloaty/third_party/protobuf/python/google/protobuf/pyext/unknown_fields.cc:53
google::protobuf::UnknownField
Definition: bloaty/third_party/protobuf/src/google/protobuf/unknown_field_set.h:216
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::Reflection
Definition: bloaty/third_party/protobuf/src/google/protobuf/message.h:397
iterator
const typedef MCPhysReg * iterator
Definition: MCRegisterInfo.h:27
google::protobuf.internal::WireFormatLite::WIRETYPE_FIXED64
@ WIRETYPE_FIXED64
Definition: bloaty/third_party/protobuf/src/google/protobuf/wire_format_lite.h:103
message
char * message
Definition: libuv/docs/code/tty-gravity/main.c:12
namespace
Definition: namespace.py:1
google::protobuf::python::PyUnknownFields
struct google::protobuf::python::PyUnknownFields PyUnknownFields
google::protobuf::python::unknown_field::PyUnknownFields_FromUnknownFieldSet
static PyObject * PyUnknownFields_FromUnknownFieldSet(PyUnknownFields *parent, const UnknownFieldSet &fields)
Definition: protobuf/python/google/protobuf/pyext/unknown_fields.cc:196
google::protobuf::UnknownField::TYPE_FIXED32
@ TYPE_FIXED32
Definition: bloaty/third_party/protobuf/src/google/protobuf/unknown_field_set.h:220
google::protobuf::python::unknown_field::GetFieldNumber
static PyObject * GetFieldNumber(PyUnknownFieldRef *self, void *closure)
Definition: protobuf/python/google/protobuf/pyext/unknown_fields.cc:232
google::protobuf.internal::WireFormatLite::WIRETYPE_FIXED32
@ WIRETYPE_FIXED32
Definition: bloaty/third_party/protobuf/src/google/protobuf/wire_format_lite.h:107
google::protobuf::python::unknown_field::Dealloc
static void Dealloc(PyObject *pself)
Definition: protobuf/python/google/protobuf/pyext/unknown_fields.cc:298
google::protobuf::python::PyUnknownFieldRef_Type
PyTypeObject PyUnknownFieldRef_Type
Definition: bloaty/third_party/protobuf/python/google/protobuf/pyext/unknown_fields.cc:315
google::protobuf::Reflection::GetUnknownFields
const UnknownFieldSet & GetUnknownFields(const Message &message) const
Definition: bloaty/third_party/protobuf/src/google/protobuf/generated_message_reflection.cc:232
google::protobuf::python::unknown_fields::Item
static PyObject * Item(PyObject *pself, Py_ssize_t index)
Definition: bloaty/third_party/protobuf/python/google/protobuf/pyext/unknown_fields.cc:78
data
char data[kBufferLength]
Definition: abseil-cpp/absl/strings/internal/str_format/float_conversion.cc:1006
google::protobuf.internal::WireFormatLite::WIRETYPE_VARINT
@ WIRETYPE_VARINT
Definition: bloaty/third_party/protobuf/src/google/protobuf/wire_format_lite.h:102
google::protobuf::UnknownField::TYPE_VARINT
@ TYPE_VARINT
Definition: bloaty/third_party/protobuf/src/google/protobuf/unknown_field_set.h:219
google::protobuf::UnknownField::TYPE_LENGTH_DELIMITED
@ TYPE_LENGTH_DELIMITED
Definition: bloaty/third_party/protobuf/src/google/protobuf/unknown_field_set.h:222
google::protobuf::python::unknown_fields::NewPyUnknownFieldRef
PyObject * NewPyUnknownFieldRef(PyUnknownFields *parent, Py_ssize_t index)
Definition: bloaty/third_party/protobuf/python/google/protobuf/pyext/unknown_fields.cc:119
google::protobuf::python::unknown_field::GetData
static PyObject * GetData(PyUnknownFieldRef *self, void *closure)
Definition: protobuf/python/google/protobuf/pyext/unknown_fields.cc:270
google::protobuf::python::PyUnknownFields_Type
PyTypeObject PyUnknownFields_Type
Definition: bloaty/third_party/protobuf/python/google/protobuf/pyext/unknown_fields.cc:158
google::protobuf::UnknownField::TYPE_FIXED64
@ TYPE_FIXED64
Definition: bloaty/third_party/protobuf/src/google/protobuf/unknown_field_set.h:221
google::protobuf::Message
Definition: bloaty/third_party/protobuf/src/google/protobuf/message.h:205
field
const FieldDescriptor * field
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/parser_unittest.cc:2692
google::protobuf.internal::WireFormatLite::WIRETYPE_START_GROUP
@ WIRETYPE_START_GROUP
Definition: bloaty/third_party/protobuf/src/google/protobuf/wire_format_lite.h:105
google::protobuf::python::unknown_fields::SqMethods
static PySequenceMethods SqMethods
Definition: bloaty/third_party/protobuf/python/google/protobuf/pyext/unknown_fields.cc:147
google::protobuf::python::unknown_fields::Clear
void Clear(PyUnknownFields *self)
Definition: bloaty/third_party/protobuf/python/google/protobuf/pyext/unknown_fields.cc:65
index
int index
Definition: bloaty/third_party/protobuf/php/ext/google/protobuf/protobuf.h:1184
google::protobuf::python::unknown_field::GetWireType
static PyObject * GetWireType(PyUnknownFieldRef *self, void *closure)
Definition: protobuf/python/google/protobuf/pyext/unknown_fields.cc:241
profile_analyzer.fields
list fields
Definition: profile_analyzer.py:266
google::protobuf::UnknownFieldSet
Definition: bloaty/third_party/protobuf/src/google/protobuf/unknown_field_set.h:81
cpp.gmock_class.set
set
Definition: bloaty/third_party/googletest/googlemock/scripts/generator/cpp/gmock_class.py:44
closure
Definition: proxy.cc:59
google::protobuf.internal::WireFormatLite::WireType
WireType
Definition: bloaty/third_party/protobuf/src/google/protobuf/wire_format_lite.h:101
google::protobuf::python::unknown_field::Getters
static PyGetSetDef Getters[]
Definition: protobuf/python/google/protobuf/pyext/unknown_fields.cc:304
PyVarObject_HEAD_INIT
#define PyVarObject_HEAD_INIT(type, size)
Definition: bloaty/third_party/protobuf/python/google/protobuf/pyext/descriptor.cc:161
Py_TYPE
#define Py_TYPE(ob)
Definition: bloaty/third_party/protobuf/python/google/protobuf/pyext/descriptor.cc:164
google::protobuf::UnknownField::number
int number() const
Definition: bloaty/third_party/protobuf/src/google/protobuf/unknown_field_set.h:326
google::protobuf.internal::WireFormatLite
Definition: bloaty/third_party/protobuf/src/google/protobuf/wire_format_lite.h:84
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
Definition: bloaty/third_party/protobuf/benchmarks/util/data_proto2_to_proto3_util.h:11
google::protobuf::python::PyUnknownFields
Definition: bloaty/third_party/protobuf/python/google/protobuf/pyext/unknown_fields.h:50
google::protobuf::python::CMessage::message
Message * message
Definition: bloaty/third_party/protobuf/python/google/protobuf/pyext/message.h:104


grpc
Author(s):
autogenerated on Fri May 16 2025 03:00:44