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)
38
#include <google/protobuf/stubs/common.h>
39
#include <google/protobuf/pyext/python_descriptor.h>
40
#include <google/protobuf/io/coded_stream.h>
41
#include <google/protobuf/descriptor.h>
42
#include <google/protobuf/dynamic_message.h>
43
#include <google/protobuf/message.h>
44
#include <google/protobuf/unknown_field_set.h>
45
#include <google/protobuf/pyext/python_protobuf.h>
48
#define IS_64BIT (SIZEOF_LONG == 8)
50
#define FIELD_BELONGS_TO_MESSAGE(field_descriptor, message) \
51
((message)->GetDescriptor() == (field_descriptor)->containing_type())
53
#define FIELD_IS_REPEATED(field_descriptor) \
54
((field_descriptor)->label() == google::protobuf::FieldDescriptor::LABEL_REPEATED)
56
#define GOOGLE_CHECK_GET_INT32(arg, value) \
58
if (!CheckAndGetInteger(arg, &value, kint32min_py, kint32max_py)) { \
62
#define GOOGLE_CHECK_GET_INT64(arg, value) \
64
if (!CheckAndGetInteger(arg, &value, kint64min_py, kint64max_py)) { \
68
#define GOOGLE_CHECK_GET_UINT32(arg, value) \
70
if (!CheckAndGetInteger(arg, &value, kPythonZero, kuint32max_py)) { \
74
#define GOOGLE_CHECK_GET_UINT64(arg, value) \
76
if (!CheckAndGetInteger(arg, &value, kPythonZero, kuint64max_py)) { \
80
#define GOOGLE_CHECK_GET_FLOAT(arg, value) \
82
if (!CheckAndGetFloat(arg, &value)) { \
86
#define GOOGLE_CHECK_GET_DOUBLE(arg, value) \
88
if (!CheckAndGetDouble(arg, &value)) { \
92
#define GOOGLE_CHECK_GET_BOOL(arg, value) \
94
if (!CheckAndGetBool(arg, &value)) { \
98
#define C(str) const_cast<char*>(str)
102
// Constants used for integer type range checking.
103
static PyObject* kPythonZero;
104
static PyObject* kint32min_py;
105
static PyObject* kint32max_py;
106
static PyObject* kuint32max_py;
107
static PyObject* kint64min_py;
108
static PyObject* kint64max_py;
109
static PyObject* kuint64max_py;
115
// --- Support Routines:
117
static void AddConstants(PyObject* module) {
123
{"LABEL_OPTIONAL", google::protobuf::FieldDescriptor::LABEL_OPTIONAL},
124
{"LABEL_REQUIRED", google::protobuf::FieldDescriptor::LABEL_REQUIRED},
125
{"LABEL_REPEATED", google::protobuf::FieldDescriptor::LABEL_REPEATED},
127
{"CPPTYPE_MESSAGE", google::protobuf::FieldDescriptor::CPPTYPE_MESSAGE},
129
{"TYPE_MESSAGE", google::protobuf::FieldDescriptor::TYPE_MESSAGE},
134
for (NameValue* constant = constants;
135
constant->name != NULL; constant++) {
136
PyModule_AddIntConstant(module, constant->name, constant->value);
140
// --- CMessage Custom Type:
142
// ------ Type Forward Declaration:
145
struct CMessage_Type;
147
static void CMessageDealloc(CMessage* self);
148
static int CMessageInit(CMessage* self, PyObject *args, PyObject *kwds);
149
static PyObject* CMessageStr(CMessage* self);
151
static PyObject* CMessage_AddMessage(CMessage* self, PyObject* args);
152
static PyObject* CMessage_AddRepeatedScalar(CMessage* self, PyObject* args);
153
static PyObject* CMessage_AssignRepeatedScalar(CMessage* self, PyObject* args);
154
static PyObject* CMessage_ByteSize(CMessage* self, PyObject* args);
155
static PyObject* CMessage_Clear(CMessage* self, PyObject* args);
156
static PyObject* CMessage_ClearField(CMessage* self, PyObject* args);
157
static PyObject* CMessage_ClearFieldByDescriptor(
158
CMessage* self, PyObject* args);
159
static PyObject* CMessage_CopyFrom(CMessage* self, PyObject* args);
160
static PyObject* CMessage_DebugString(CMessage* self, PyObject* args);
161
static PyObject* CMessage_DeleteRepeatedField(CMessage* self, PyObject* args);
162
static PyObject* CMessage_Equals(CMessage* self, PyObject* args);
163
static PyObject* CMessage_FieldLength(CMessage* self, PyObject* args);
164
static PyObject* CMessage_FindInitializationErrors(CMessage* self);
165
static PyObject* CMessage_GetRepeatedMessage(CMessage* self, PyObject* args);
166
static PyObject* CMessage_GetRepeatedScalar(CMessage* self, PyObject* args);
167
static PyObject* CMessage_GetScalar(CMessage* self, PyObject* args);
168
static PyObject* CMessage_HasField(CMessage* self, PyObject* args);
169
static PyObject* CMessage_HasFieldByDescriptor(CMessage* self, PyObject* args);
170
static PyObject* CMessage_IsInitialized(CMessage* self, PyObject* args);
171
static PyObject* CMessage_ListFields(CMessage* self, PyObject* args);
172
static PyObject* CMessage_MergeFrom(CMessage* self, PyObject* args);
173
static PyObject* CMessage_MergeFromString(CMessage* self, PyObject* args);
174
static PyObject* CMessage_MutableMessage(CMessage* self, PyObject* args);
175
static PyObject* CMessage_NewSubMessage(CMessage* self, PyObject* args);
176
static PyObject* CMessage_SetScalar(CMessage* self, PyObject* args);
177
static PyObject* CMessage_SerializePartialToString(
178
CMessage* self, PyObject* args);
179
static PyObject* CMessage_SerializeToString(CMessage* self, PyObject* args);
180
static PyObject* CMessage_SetInParent(CMessage* self, PyObject* args);
181
static PyObject* CMessage_SwapRepeatedFieldElements(
182
CMessage* self, PyObject* args);
184
// ------ Object Definition:
186
typedef struct CMessage {
189
struct CMessage* parent; // NULL if wasn't created from another message.
190
CFieldDescriptor* parent_field;
191
const char* full_name;
192
google::protobuf::Message* message;
197
// ------ Method Table:
199
#define CMETHOD(name, args, doc) \
200
{ C(#name), (PyCFunction)CMessage_##name, args, C(doc) }
201
static PyMethodDef CMessageMethods[] = {
202
CMETHOD(AddMessage, METH_O,
203
"Adds a new message to a repeated composite field."),
204
CMETHOD(AddRepeatedScalar, METH_VARARGS,
205
"Adds a scalar to a repeated scalar field."),
206
CMETHOD(AssignRepeatedScalar, METH_VARARGS,
207
"Clears and sets the values of a repeated scalar field."),
208
CMETHOD(ByteSize, METH_NOARGS,
209
"Returns the size of the message in bytes."),
210
CMETHOD(Clear, METH_NOARGS,
211
"Clears a protocol message."),
212
CMETHOD(ClearField, METH_O,
213
"Clears a protocol message field by name."),
214
CMETHOD(ClearFieldByDescriptor, METH_O,
215
"Clears a protocol message field by descriptor."),
216
CMETHOD(CopyFrom, METH_O,
217
"Copies a protocol message into the current message."),
218
CMETHOD(DebugString, METH_NOARGS,
219
"Returns the debug string of a protocol message."),
220
CMETHOD(DeleteRepeatedField, METH_VARARGS,
221
"Deletes a slice of values from a repeated field."),
222
CMETHOD(Equals, METH_O,
223
"Checks if two protocol messages are equal (by identity)."),
224
CMETHOD(FieldLength, METH_O,
225
"Returns the number of elements in a repeated field."),
226
CMETHOD(FindInitializationErrors, METH_NOARGS,
227
"Returns the initialization errors of a message."),
228
CMETHOD(GetRepeatedMessage, METH_VARARGS,
229
"Returns a message from a repeated composite field."),
230
CMETHOD(GetRepeatedScalar, METH_VARARGS,
231
"Returns a scalar value from a repeated scalar field."),
232
CMETHOD(GetScalar, METH_O,
233
"Returns the scalar value of a field."),
234
CMETHOD(HasField, METH_O,
235
"Checks if a message field is set."),
236
CMETHOD(HasFieldByDescriptor, METH_O,
237
"Checks if a message field is set by given its descriptor"),
238
CMETHOD(IsInitialized, METH_NOARGS,
239
"Checks if all required fields of a protocol message are set."),
240
CMETHOD(ListFields, METH_NOARGS,
241
"Lists all set fields of a message."),
242
CMETHOD(MergeFrom, METH_O,
243
"Merges a protocol message into the current message."),
244
CMETHOD(MergeFromString, METH_O,
245
"Merges a serialized message into the current message."),
246
CMETHOD(MutableMessage, METH_O,
247
"Returns a new instance of a nested protocol message."),
248
CMETHOD(NewSubMessage, METH_O,
249
"Creates and returns a python message given the descriptor of a "
250
"composite field of the current message."),
251
CMETHOD(SetScalar, METH_VARARGS,
252
"Sets the value of a singular scalar field."),
253
CMETHOD(SerializePartialToString, METH_VARARGS,
254
"Serializes the message to a string, even if it isn't initialized."),
255
CMETHOD(SerializeToString, METH_NOARGS,
256
"Serializes the message to a string, only for initialized messages."),
257
CMETHOD(SetInParent, METH_NOARGS,
258
"Sets the has bit of the given field in its parent message."),
259
CMETHOD(SwapRepeatedFieldElements, METH_VARARGS,
260
"Swaps the elements in two positions in a repeated field."),
265
static PyMemberDef CMessageMembers[] = {
266
{ C("full_name"), T_STRING, offsetof(CMessage, full_name), 0, "Full name" },
270
// ------ Type Definition:
272
// The definition for the type object that captures the type of CMessage
274
PyTypeObject CMessage_Type = {
275
PyObject_HEAD_INIT(&PyType_Type)
277
C("google3.net.google.protobuf.python.internal."
278
"_net_proto2___python."
279
"CMessage"), // tp_name
280
sizeof(CMessage), // tp_basicsize
282
(destructor)CMessageDealloc, // tp_dealloc
293
(reprfunc)CMessageStr, // tp_str
297
Py_TPFLAGS_DEFAULT, // tp_flags
298
C("A ProtocolMessage"), // tp_doc
302
0, // tp_weaklistoffset
305
CMessageMethods, // tp_methods
306
CMessageMembers, // tp_members
313
(initproc)CMessageInit, // tp_init
314
PyType_GenericAlloc, // tp_alloc
315
PyType_GenericNew, // tp_new
316
PyObject_Del, // tp_free
319
// ------ Helper Functions:
321
static void FormatTypeError(PyObject* arg, char* expected_types) {
322
PyObject* s = PyObject_Str(PyObject_Type(arg));
323
PyObject* repr = PyObject_Repr(PyObject_Type(arg));
324
PyErr_Format(PyExc_TypeError,
325
"%.100s has type %.100s, but expected one of: %s",
326
PyString_AS_STRING(repr),
327
PyString_AS_STRING(s),
334
static bool CheckAndGetInteger(
335
PyObject* arg, T* value, PyObject* min, PyObject* max) {
336
bool is_long = PyLong_Check(arg);
337
if (!PyInt_Check(arg) && !is_long) {
338
FormatTypeError(arg, "int, long");
342
if (PyObject_Compare(min, arg) > 0 || PyObject_Compare(max, arg) < 0) {
343
PyObject* s = PyObject_Str(arg);
344
PyErr_Format(PyExc_ValueError,
345
"Value out of range: %s",
346
PyString_AS_STRING(s));
351
if (min == kPythonZero) {
352
*value = static_cast<T>(PyLong_AsUnsignedLongLong(arg));
354
*value = static_cast<T>(PyLong_AsLongLong(arg));
357
*value = static_cast<T>(PyInt_AsLong(arg));
362
static bool CheckAndGetDouble(PyObject* arg, double* value) {
363
if (!PyInt_Check(arg) && !PyLong_Check(arg) &&
364
!PyFloat_Check(arg)) {
365
FormatTypeError(arg, "int, long, float");
368
*value = PyFloat_AsDouble(arg);
372
static bool CheckAndGetFloat(PyObject* arg, float* value) {
374
if (!CheckAndGetDouble(arg, &double_value)) {
377
*value = static_cast<float>(double_value);
381
static bool CheckAndGetBool(PyObject* arg, bool* value) {
382
if (!PyInt_Check(arg) && !PyBool_Check(arg) && !PyLong_Check(arg)) {
383
FormatTypeError(arg, "int, long, bool");
386
*value = static_cast<bool>(PyInt_AsLong(arg));
390
google::protobuf::DynamicMessageFactory* global_message_factory = NULL;
391
static const google::protobuf::Message* CreateMessage(const char* message_type) {
392
string message_name(message_type);
393
const google::protobuf::Descriptor* descriptor =
394
GetDescriptorPool()->FindMessageTypeByName(message_name);
395
if (descriptor == NULL) {
398
return global_message_factory->GetPrototype(descriptor);
401
static bool CheckAndSetString(
402
PyObject* arg, google::protobuf::Message* message,
403
const google::protobuf::FieldDescriptor* descriptor,
404
const google::protobuf::Reflection* reflection,
407
GOOGLE_DCHECK(descriptor->type() == google::protobuf::FieldDescriptor::TYPE_STRING ||
408
descriptor->type() == google::protobuf::FieldDescriptor::TYPE_BYTES);
409
if (descriptor->type() == google::protobuf::FieldDescriptor::TYPE_STRING) {
410
if (!PyString_Check(arg) && !PyUnicode_Check(arg)) {
411
FormatTypeError(arg, "str, unicode");
415
if (PyString_Check(arg)) {
416
PyObject* unicode = PyUnicode_FromEncodedObject(arg, "ascii", NULL);
417
if (unicode == NULL) {
418
PyObject* repr = PyObject_Repr(arg);
419
PyErr_Format(PyExc_ValueError,
420
"%s has type str, but isn't in 7-bit ASCII "
421
"encoding. Non-ASCII strings must be converted to "
422
"unicode objects before being added.",
423
PyString_AS_STRING(repr));
430
} else if (!PyString_Check(arg)) {
431
FormatTypeError(arg, "str");
435
PyObject* encoded_string = NULL;
436
if (descriptor->type() == google::protobuf::FieldDescriptor::TYPE_STRING) {
437
if (PyString_Check(arg)) {
438
encoded_string = PyString_AsEncodedObject(arg, "utf-8", NULL);
440
encoded_string = PyUnicode_AsEncodedObject(arg, "utf-8", NULL);
443
// In this case field type is "bytes".
444
encoded_string = arg;
445
Py_INCREF(encoded_string);
448
if (encoded_string == NULL) {
453
Py_ssize_t value_len;
454
if (PyString_AsStringAndSize(encoded_string, &value, &value_len) < 0) {
455
Py_DECREF(encoded_string);
459
string value_string(value, value_len);
461
reflection->AddString(message, descriptor, value_string);
462
} else if (index < 0) {
463
reflection->SetString(message, descriptor, value_string);
465
reflection->SetRepeatedString(message, descriptor, index, value_string);
467
Py_DECREF(encoded_string);
471
static PyObject* ToStringObject(
472
const google::protobuf::FieldDescriptor* descriptor, string value) {
473
if (descriptor->type() != google::protobuf::FieldDescriptor::TYPE_STRING) {
474
return PyString_FromStringAndSize(value.c_str(), value.length());
477
PyObject* result = PyUnicode_DecodeUTF8(value.c_str(), value.length(), NULL);
478
// If the string can't be decoded in UTF-8, just return a string object that
479
// contains the raw bytes. This can't happen if the value was assigned using
480
// the members of the Python message object, but can happen if the values were
481
// parsed from the wire (binary).
482
if (result == NULL) {
484
result = PyString_FromStringAndSize(value.c_str(), value.length());
489
static void AssureWritable(CMessage* self) {
491
self->parent == NULL ||
492
self->parent_field == NULL) {
496
if (!self->read_only) {
500
AssureWritable(self->parent);
502
google::protobuf::Message* message = self->parent->message;
503
const google::protobuf::Reflection* reflection = message->GetReflection();
504
self->message = reflection->MutableMessage(
505
message, self->parent_field->descriptor, global_message_factory);
506
self->read_only = false;
508
self->parent_field = NULL;
511
static PyObject* InternalGetScalar(
512
google::protobuf::Message* message,
513
const google::protobuf::FieldDescriptor* field_descriptor) {
514
const google::protobuf::Reflection* reflection = message->GetReflection();
516
if (!FIELD_BELONGS_TO_MESSAGE(field_descriptor, message)) {
518
PyExc_KeyError, "Field does not belong to message!");
522
PyObject* result = NULL;
523
switch (field_descriptor->cpp_type()) {
524
case google::protobuf::FieldDescriptor::CPPTYPE_INT32: {
525
int32 value = reflection->GetInt32(*message, field_descriptor);
526
result = PyInt_FromLong(value);
529
case google::protobuf::FieldDescriptor::CPPTYPE_INT64: {
530
int64 value = reflection->GetInt64(*message, field_descriptor);
532
result = PyInt_FromLong(value);
534
result = PyLong_FromLongLong(value);
538
case google::protobuf::FieldDescriptor::CPPTYPE_UINT32: {
539
uint32 value = reflection->GetUInt32(*message, field_descriptor);
541
result = PyInt_FromLong(value);
543
result = PyLong_FromLongLong(value);
547
case google::protobuf::FieldDescriptor::CPPTYPE_UINT64: {
548
uint64 value = reflection->GetUInt64(*message, field_descriptor);
550
if (value <= static_cast<uint64>(kint64max)) {
551
result = PyInt_FromLong(static_cast<uint64>(value));
554
if (value <= static_cast<uint32>(kint32max)) {
555
result = PyInt_FromLong(static_cast<uint32>(value));
559
result = PyLong_FromUnsignedLongLong(value);
563
case google::protobuf::FieldDescriptor::CPPTYPE_FLOAT: {
564
float value = reflection->GetFloat(*message, field_descriptor);
565
result = PyFloat_FromDouble(value);
568
case google::protobuf::FieldDescriptor::CPPTYPE_DOUBLE: {
569
double value = reflection->GetDouble(*message, field_descriptor);
570
result = PyFloat_FromDouble(value);
573
case google::protobuf::FieldDescriptor::CPPTYPE_BOOL: {
574
bool value = reflection->GetBool(*message, field_descriptor);
575
result = PyBool_FromLong(value);
578
case google::protobuf::FieldDescriptor::CPPTYPE_STRING: {
579
string value = reflection->GetString(*message, field_descriptor);
580
result = ToStringObject(field_descriptor, value);
583
case google::protobuf::FieldDescriptor::CPPTYPE_ENUM: {
584
if (!message->GetReflection()->HasField(*message, field_descriptor)) {
585
// Look for the value in the unknown fields.
586
google::protobuf::UnknownFieldSet* unknown_field_set =
587
message->GetReflection()->MutableUnknownFields(message);
588
for (int i = 0; i < unknown_field_set->field_count(); ++i) {
589
if (unknown_field_set->field(i).number() ==
590
field_descriptor->number()) {
591
result = PyInt_FromLong(unknown_field_set->field(i).varint());
597
if (result == NULL) {
598
const google::protobuf::EnumValueDescriptor* enum_value =
599
message->GetReflection()->GetEnum(*message, field_descriptor);
600
result = PyInt_FromLong(enum_value->number());
606
PyExc_SystemError, "Getting a value from a field of unknown type %d",
607
field_descriptor->cpp_type());
613
static PyObject* InternalSetScalar(
614
google::protobuf::Message* message, const google::protobuf::FieldDescriptor* field_descriptor,
616
const google::protobuf::Reflection* reflection = message->GetReflection();
618
if (!FIELD_BELONGS_TO_MESSAGE(field_descriptor, message)) {
620
PyExc_KeyError, "Field does not belong to message!");
624
switch (field_descriptor->cpp_type()) {
625
case google::protobuf::FieldDescriptor::CPPTYPE_INT32: {
626
GOOGLE_CHECK_GET_INT32(arg, value);
627
reflection->SetInt32(message, field_descriptor, value);
630
case google::protobuf::FieldDescriptor::CPPTYPE_INT64: {
631
GOOGLE_CHECK_GET_INT64(arg, value);
632
reflection->SetInt64(message, field_descriptor, value);
635
case google::protobuf::FieldDescriptor::CPPTYPE_UINT32: {
636
GOOGLE_CHECK_GET_UINT32(arg, value);
637
reflection->SetUInt32(message, field_descriptor, value);
640
case google::protobuf::FieldDescriptor::CPPTYPE_UINT64: {
641
GOOGLE_CHECK_GET_UINT64(arg, value);
642
reflection->SetUInt64(message, field_descriptor, value);
645
case google::protobuf::FieldDescriptor::CPPTYPE_FLOAT: {
646
GOOGLE_CHECK_GET_FLOAT(arg, value);
647
reflection->SetFloat(message, field_descriptor, value);
650
case google::protobuf::FieldDescriptor::CPPTYPE_DOUBLE: {
651
GOOGLE_CHECK_GET_DOUBLE(arg, value);
652
reflection->SetDouble(message, field_descriptor, value);
655
case google::protobuf::FieldDescriptor::CPPTYPE_BOOL: {
656
GOOGLE_CHECK_GET_BOOL(arg, value);
657
reflection->SetBool(message, field_descriptor, value);
660
case google::protobuf::FieldDescriptor::CPPTYPE_STRING: {
661
if (!CheckAndSetString(
662
arg, message, field_descriptor, reflection, false, -1)) {
667
case google::protobuf::FieldDescriptor::CPPTYPE_ENUM: {
668
GOOGLE_CHECK_GET_INT32(arg, value);
669
const google::protobuf::EnumDescriptor* enum_descriptor =
670
field_descriptor->enum_type();
671
const google::protobuf::EnumValueDescriptor* enum_value =
672
enum_descriptor->FindValueByNumber(value);
673
if (enum_value != NULL) {
674
reflection->SetEnum(message, field_descriptor, enum_value);
677
// Add the value to the unknown fields.
678
google::protobuf::UnknownFieldSet* unknown_field_set =
679
message->GetReflection()->MutableUnknownFields(message);
680
for (int i = 0; i < unknown_field_set->field_count(); ++i) {
681
if (unknown_field_set->field(i).number() ==
682
field_descriptor->number()) {
683
unknown_field_set->mutable_field(i)->set_varint(value);
690
unknown_field_set->AddVarint(field_descriptor->number(), value);
692
reflection->ClearField(message, field_descriptor);
698
PyExc_SystemError, "Setting value to a field of unknown type %d",
699
field_descriptor->cpp_type());
705
static PyObject* InternalAddRepeatedScalar(
706
google::protobuf::Message* message, const google::protobuf::FieldDescriptor* field_descriptor,
709
if (!FIELD_BELONGS_TO_MESSAGE(field_descriptor, message)) {
711
PyExc_KeyError, "Field does not belong to message!");
715
const google::protobuf::Reflection* reflection = message->GetReflection();
716
switch (field_descriptor->cpp_type()) {
717
case google::protobuf::FieldDescriptor::CPPTYPE_INT32: {
718
GOOGLE_CHECK_GET_INT32(arg, value);
719
reflection->AddInt32(message, field_descriptor, value);
722
case google::protobuf::FieldDescriptor::CPPTYPE_INT64: {
723
GOOGLE_CHECK_GET_INT64(arg, value);
724
reflection->AddInt64(message, field_descriptor, value);
727
case google::protobuf::FieldDescriptor::CPPTYPE_UINT32: {
728
GOOGLE_CHECK_GET_UINT32(arg, value);
729
reflection->AddUInt32(message, field_descriptor, value);
732
case google::protobuf::FieldDescriptor::CPPTYPE_UINT64: {
733
GOOGLE_CHECK_GET_UINT64(arg, value);
734
reflection->AddUInt64(message, field_descriptor, value);
737
case google::protobuf::FieldDescriptor::CPPTYPE_FLOAT: {
738
GOOGLE_CHECK_GET_FLOAT(arg, value);
739
reflection->AddFloat(message, field_descriptor, value);
742
case google::protobuf::FieldDescriptor::CPPTYPE_DOUBLE: {
743
GOOGLE_CHECK_GET_DOUBLE(arg, value);
744
reflection->AddDouble(message, field_descriptor, value);
747
case google::protobuf::FieldDescriptor::CPPTYPE_BOOL: {
748
GOOGLE_CHECK_GET_BOOL(arg, value);
749
reflection->AddBool(message, field_descriptor, value);
752
case google::protobuf::FieldDescriptor::CPPTYPE_STRING: {
753
if (!CheckAndSetString(
754
arg, message, field_descriptor, reflection, true, -1)) {
759
case google::protobuf::FieldDescriptor::CPPTYPE_ENUM: {
760
GOOGLE_CHECK_GET_INT32(arg, value);
761
const google::protobuf::EnumDescriptor* enum_descriptor =
762
field_descriptor->enum_type();
763
const google::protobuf::EnumValueDescriptor* enum_value =
764
enum_descriptor->FindValueByNumber(value);
765
if (enum_value != NULL) {
766
reflection->AddEnum(message, field_descriptor, enum_value);
768
PyObject* s = PyObject_Str(arg);
769
PyErr_Format(PyExc_ValueError, "Unknown enum value: %s",
770
PyString_AS_STRING(s));
778
PyExc_SystemError, "Adding value to a field of unknown type %d",
779
field_descriptor->cpp_type());
785
static PyObject* InternalGetRepeatedScalar(
786
CMessage* cmessage, const google::protobuf::FieldDescriptor* field_descriptor,
788
google::protobuf::Message* message = cmessage->message;
789
const google::protobuf::Reflection* reflection = message->GetReflection();
791
int field_size = reflection->FieldSize(*message, field_descriptor);
793
index = field_size + index;
795
if (index < 0 || index >= field_size) {
796
PyErr_Format(PyExc_IndexError,
797
"list assignment index (%d) out of range", index);
801
PyObject* result = NULL;
802
switch (field_descriptor->cpp_type()) {
803
case google::protobuf::FieldDescriptor::CPPTYPE_INT32: {
804
int32 value = reflection->GetRepeatedInt32(
805
*message, field_descriptor, index);
806
result = PyInt_FromLong(value);
809
case google::protobuf::FieldDescriptor::CPPTYPE_INT64: {
810
int64 value = reflection->GetRepeatedInt64(
811
*message, field_descriptor, index);
812
result = PyLong_FromLongLong(value);
815
case google::protobuf::FieldDescriptor::CPPTYPE_UINT32: {
816
uint32 value = reflection->GetRepeatedUInt32(
817
*message, field_descriptor, index);
818
result = PyLong_FromLongLong(value);
821
case google::protobuf::FieldDescriptor::CPPTYPE_UINT64: {
822
uint64 value = reflection->GetRepeatedUInt64(
823
*message, field_descriptor, index);
824
result = PyLong_FromUnsignedLongLong(value);
827
case google::protobuf::FieldDescriptor::CPPTYPE_FLOAT: {
828
float value = reflection->GetRepeatedFloat(
829
*message, field_descriptor, index);
830
result = PyFloat_FromDouble(value);
833
case google::protobuf::FieldDescriptor::CPPTYPE_DOUBLE: {
834
double value = reflection->GetRepeatedDouble(
835
*message, field_descriptor, index);
836
result = PyFloat_FromDouble(value);
839
case google::protobuf::FieldDescriptor::CPPTYPE_BOOL: {
840
bool value = reflection->GetRepeatedBool(
841
*message, field_descriptor, index);
842
result = PyBool_FromLong(value ? 1 : 0);
845
case google::protobuf::FieldDescriptor::CPPTYPE_ENUM: {
846
const google::protobuf::EnumValueDescriptor* enum_value =
847
message->GetReflection()->GetRepeatedEnum(
848
*message, field_descriptor, index);
849
result = PyInt_FromLong(enum_value->number());
852
case google::protobuf::FieldDescriptor::CPPTYPE_STRING: {
853
string value = reflection->GetRepeatedString(
854
*message, field_descriptor, index);
855
result = ToStringObject(field_descriptor, value);
858
case google::protobuf::FieldDescriptor::CPPTYPE_MESSAGE: {
859
CMessage* py_cmsg = PyObject_New(CMessage, &CMessage_Type);
860
if (py_cmsg == NULL) {
863
const google::protobuf::Message& msg = reflection->GetRepeatedMessage(
864
*message, field_descriptor, index);
865
py_cmsg->parent = cmessage;
866
py_cmsg->full_name = field_descriptor->full_name().c_str();
867
py_cmsg->message = const_cast<google::protobuf::Message*>(&msg);
868
py_cmsg->free_message = false;
869
py_cmsg->read_only = false;
870
result = reinterpret_cast<PyObject*>(py_cmsg);
876
"Getting value from a repeated field of unknown type %d",
877
field_descriptor->cpp_type());
883
static PyObject* InternalGetRepeatedScalarSlice(
884
CMessage* cmessage, const google::protobuf::FieldDescriptor* field_descriptor,
890
bool return_list = false;
891
google::protobuf::Message* message = cmessage->message;
893
if (PyInt_Check(slice)) {
894
from = to = PyInt_AsLong(slice);
895
} else if (PyLong_Check(slice)) {
896
from = to = PyLong_AsLong(slice);
897
} else if (PySlice_Check(slice)) {
898
const google::protobuf::Reflection* reflection = message->GetReflection();
899
length = reflection->FieldSize(*message, field_descriptor);
901
reinterpret_cast<PySliceObject*>(slice), length, &from, &to, &step);
904
PyErr_SetString(PyExc_TypeError, "list indices must be integers");
909
return InternalGetRepeatedScalar(cmessage, field_descriptor, from);
912
PyObject* list = PyList_New(0);
918
if (step < 0) return list;
919
for (Py_ssize_t index = from; index < to; index += step) {
920
if (index < 0 || index >= length) break;
921
PyObject* s = InternalGetRepeatedScalar(
922
cmessage, field_descriptor, index);
923
PyList_Append(list, s);
927
if (step > 0) return list;
928
for (Py_ssize_t index = from; index > to; index += step) {
929
if (index < 0 || index >= length) break;
930
PyObject* s = InternalGetRepeatedScalar(
931
cmessage, field_descriptor, index);
932
PyList_Append(list, s);
939
// ------ C Constructor/Destructor:
941
static int CMessageInit(CMessage* self, PyObject *args, PyObject *kwds) {
942
self->message = NULL;
946
static void CMessageDealloc(CMessage* self) {
947
if (self->free_message) {
948
if (self->read_only) {
949
PyErr_WriteUnraisable(reinterpret_cast<PyObject*>(self));
951
delete self->message;
953
self->ob_type->tp_free(reinterpret_cast<PyObject*>(self));
958
static PyObject* CMessage_Clear(CMessage* self, PyObject* args) {
959
AssureWritable(self);
960
self->message->Clear();
964
static PyObject* CMessage_IsInitialized(CMessage* self, PyObject* args) {
965
return PyBool_FromLong(self->message->IsInitialized() ? 1 : 0);
968
static PyObject* CMessage_HasField(CMessage* self, PyObject* arg) {
970
if (PyString_AsStringAndSize(arg, &field_name, NULL) < 0) {
974
google::protobuf::Message* message = self->message;
975
const google::protobuf::Descriptor* descriptor = message->GetDescriptor();
976
const google::protobuf::FieldDescriptor* field_descriptor =
977
descriptor->FindFieldByName(field_name);
978
if (field_descriptor == NULL) {
979
PyErr_Format(PyExc_ValueError, "Unknown field %s.", field_name);
984
message->GetReflection()->HasField(*message, field_descriptor);
985
return PyBool_FromLong(has_field ? 1 : 0);
988
static PyObject* CMessage_HasFieldByDescriptor(CMessage* self, PyObject* arg) {
989
CFieldDescriptor* cfield_descriptor = NULL;
990
if (!PyObject_TypeCheck(reinterpret_cast<PyObject *>(arg),
991
&CFieldDescriptor_Type)) {
992
PyErr_SetString(PyExc_TypeError, "Must be a field descriptor");
995
cfield_descriptor = reinterpret_cast<CFieldDescriptor*>(arg);
997
google::protobuf::Message* message = self->message;
998
const google::protobuf::FieldDescriptor* field_descriptor =
999
cfield_descriptor->descriptor;
1001
if (!FIELD_BELONGS_TO_MESSAGE(field_descriptor, message)) {
1002
PyErr_SetString(PyExc_KeyError,
1003
"Field does not belong to message!");
1007
if (FIELD_IS_REPEATED(field_descriptor)) {
1008
PyErr_SetString(PyExc_KeyError,
1009
"Field is repeated. A singular method is required.");
1014
message->GetReflection()->HasField(*message, field_descriptor);
1015
return PyBool_FromLong(has_field ? 1 : 0);
1018
static PyObject* CMessage_ClearFieldByDescriptor(
1019
CMessage* self, PyObject* arg) {
1020
CFieldDescriptor* cfield_descriptor = NULL;
1021
if (!PyObject_TypeCheck(reinterpret_cast<PyObject *>(arg),
1022
&CFieldDescriptor_Type)) {
1023
PyErr_SetString(PyExc_TypeError, "Must be a field descriptor");
1026
cfield_descriptor = reinterpret_cast<CFieldDescriptor*>(arg);
1028
google::protobuf::Message* message = self->message;
1029
const google::protobuf::FieldDescriptor* field_descriptor =
1030
cfield_descriptor->descriptor;
1032
if (!FIELD_BELONGS_TO_MESSAGE(field_descriptor, message)) {
1033
PyErr_SetString(PyExc_KeyError,
1034
"Field does not belong to message!");
1038
message->GetReflection()->ClearField(message, field_descriptor);
1042
static PyObject* CMessage_ClearField(CMessage* self, PyObject* arg) {
1044
if (PyString_AsStringAndSize(arg, &field_name, NULL) < 0) {
1048
google::protobuf::Message* message = self->message;
1049
const google::protobuf::Descriptor* descriptor = message->GetDescriptor();
1050
const google::protobuf::FieldDescriptor* field_descriptor =
1051
descriptor->FindFieldByName(field_name);
1052
if (field_descriptor == NULL) {
1053
PyErr_Format(PyExc_ValueError, "Unknown field %s.", field_name);
1057
message->GetReflection()->ClearField(message, field_descriptor);
1061
static PyObject* CMessage_GetScalar(CMessage* self, PyObject* arg) {
1062
CFieldDescriptor* cdescriptor = NULL;
1063
if (!PyObject_TypeCheck(reinterpret_cast<PyObject *>(arg),
1064
&CFieldDescriptor_Type)) {
1065
PyErr_SetString(PyExc_TypeError, "Must be a field descriptor");
1068
cdescriptor = reinterpret_cast<CFieldDescriptor*>(arg);
1070
google::protobuf::Message* message = self->message;
1071
return InternalGetScalar(message, cdescriptor->descriptor);
1074
static PyObject* CMessage_GetRepeatedScalar(CMessage* self, PyObject* args) {
1075
CFieldDescriptor* cfield_descriptor;
1077
if (!PyArg_ParseTuple(args, C("O!O:GetRepeatedScalar"),
1078
&CFieldDescriptor_Type, &cfield_descriptor, &slice)) {
1082
return InternalGetRepeatedScalarSlice(
1083
self, cfield_descriptor->descriptor, slice);
1086
static PyObject* CMessage_AssignRepeatedScalar(CMessage* self, PyObject* args) {
1087
CFieldDescriptor* cfield_descriptor;
1089
if (!PyArg_ParseTuple(args, C("O!O:AssignRepeatedScalar"),
1090
&CFieldDescriptor_Type, &cfield_descriptor, &slice)) {
1094
AssureWritable(self);
1095
google::protobuf::Message* message = self->message;
1096
message->GetReflection()->ClearField(message, cfield_descriptor->descriptor);
1098
PyObject* iter = PyObject_GetIter(slice);
1100
while ((next = PyIter_Next(iter)) != NULL) {
1101
if (InternalAddRepeatedScalar(
1102
message, cfield_descriptor->descriptor, next) == NULL) {
1111
static PyObject* CMessage_DeleteRepeatedField(CMessage* self, PyObject* args) {
1112
CFieldDescriptor* cfield_descriptor;
1114
if (!PyArg_ParseTuple(args, C("O!O:DeleteRepeatedField"),
1115
&CFieldDescriptor_Type, &cfield_descriptor, &slice)) {
1118
AssureWritable(self);
1120
Py_ssize_t length, from, to, step, slice_length;
1121
google::protobuf::Message* message = self->message;
1122
const google::protobuf::FieldDescriptor* field_descriptor =
1123
cfield_descriptor->descriptor;
1124
const google::protobuf::Reflection* reflection = message->GetReflection();
1126
length = reflection->FieldSize(*message, field_descriptor);
1128
if (PyInt_Check(slice) || PyLong_Check(slice)) {
1129
from = to = PyLong_AsLong(slice);
1131
from = to = length + from;
1137
if (from < 0 || from >= length) {
1138
PyErr_Format(PyExc_IndexError, "list assignment index out of range");
1141
} else if (PySlice_Check(slice)) {
1142
from = to = step = slice_length = 0;
1143
PySlice_GetIndicesEx(
1144
reinterpret_cast<PySliceObject*>(slice),
1145
length, &from, &to, &step, &slice_length);
1154
PyErr_SetString(PyExc_TypeError, "list indices must be integers");
1158
Py_ssize_t i = from;
1159
std::vector<bool> to_delete(length, false);
1160
while (i >= min && i <= max) {
1161
to_delete[i] = true;
1166
for (i = 0; i < length; ++i) {
1167
if (!to_delete[i]) {
1169
reflection->SwapElements(message, field_descriptor, i, to);
1176
reflection->RemoveLast(message, field_descriptor);
1184
static PyObject* CMessage_SetScalar(CMessage* self, PyObject* args) {
1185
CFieldDescriptor* cfield_descriptor;
1187
if (!PyArg_ParseTuple(args, C("O!O:SetScalar"),
1188
&CFieldDescriptor_Type, &cfield_descriptor, &arg)) {
1191
AssureWritable(self);
1193
return InternalSetScalar(self->message, cfield_descriptor->descriptor, arg);
1196
static PyObject* CMessage_AddRepeatedScalar(CMessage* self, PyObject* args) {
1197
CFieldDescriptor* cfield_descriptor;
1199
if (!PyArg_ParseTuple(args, C("O!O:AddRepeatedScalar"),
1200
&CFieldDescriptor_Type, &cfield_descriptor, &value)) {
1203
AssureWritable(self);
1205
return InternalAddRepeatedScalar(
1206
self->message, cfield_descriptor->descriptor, value);
1209
static PyObject* CMessage_FieldLength(CMessage* self, PyObject* arg) {
1210
CFieldDescriptor* cfield_descriptor;
1211
if (!PyObject_TypeCheck(reinterpret_cast<PyObject *>(arg),
1212
&CFieldDescriptor_Type)) {
1213
PyErr_SetString(PyExc_TypeError, "Must be a field descriptor");
1216
cfield_descriptor = reinterpret_cast<CFieldDescriptor*>(arg);
1218
google::protobuf::Message* message = self->message;
1219
int length = message->GetReflection()->FieldSize(
1220
*message, cfield_descriptor->descriptor);
1221
return PyInt_FromLong(length);
1224
static PyObject* CMessage_DebugString(CMessage* self, PyObject* args) {
1225
return PyString_FromString(self->message->DebugString().c_str());
1228
static PyObject* CMessage_SerializeToString(CMessage* self, PyObject* args) {
1229
int size = self->message->ByteSize();
1231
return PyString_FromString("");
1233
PyObject* result = PyString_FromStringAndSize(NULL, size);
1234
if (result == NULL) {
1237
char* buffer = PyString_AS_STRING(result);
1238
self->message->SerializeWithCachedSizesToArray(
1239
reinterpret_cast<uint8*>(buffer));
1243
static PyObject* CMessage_SerializePartialToString(
1244
CMessage* self, PyObject* args) {
1246
self->message->SerializePartialToString(&contents);
1247
return PyString_FromStringAndSize(contents.c_str(), contents.size());
1250
static PyObject* CMessageStr(CMessage* self) {
1252
str[sizeof(str) - 1] = 0;
1253
snprintf(str, sizeof(str) - 1, "CMessage: <%p>", self->message);
1254
return PyString_FromString(str);
1257
static PyObject* CMessage_MergeFrom(CMessage* self, PyObject* arg) {
1258
CMessage* other_message;
1259
if (!PyObject_TypeCheck(reinterpret_cast<PyObject *>(arg), &CMessage_Type)) {
1260
PyErr_SetString(PyExc_TypeError, "Must be a message");
1264
other_message = reinterpret_cast<CMessage*>(arg);
1265
if (other_message->message->GetDescriptor() !=
1266
self->message->GetDescriptor()) {
1267
PyErr_Format(PyExc_TypeError,
1268
"Tried to merge from a message with a different type. "
1270
self->message->GetDescriptor()->full_name().c_str(),
1271
other_message->message->GetDescriptor()->full_name().c_str());
1274
AssureWritable(self);
1276
self->message->MergeFrom(*other_message->message);
1280
static PyObject* CMessage_CopyFrom(CMessage* self, PyObject* arg) {
1281
CMessage* other_message;
1282
if (!PyObject_TypeCheck(reinterpret_cast<PyObject *>(arg), &CMessage_Type)) {
1283
PyErr_SetString(PyExc_TypeError, "Must be a message");
1287
other_message = reinterpret_cast<CMessage*>(arg);
1288
if (other_message->message->GetDescriptor() !=
1289
self->message->GetDescriptor()) {
1290
PyErr_Format(PyExc_TypeError,
1291
"Tried to copy from a message with a different type. "
1293
self->message->GetDescriptor()->full_name().c_str(),
1294
other_message->message->GetDescriptor()->full_name().c_str());
1298
AssureWritable(self);
1300
self->message->CopyFrom(*other_message->message);
1304
static PyObject* CMessage_MergeFromString(CMessage* self, PyObject* arg) {
1306
Py_ssize_t data_length;
1307
if (PyObject_AsReadBuffer(arg, &data, &data_length) < 0) {
1311
AssureWritable(self);
1312
google::protobuf::io::CodedInputStream input(
1313
reinterpret_cast<const uint8*>(data), data_length);
1314
bool success = self->message->MergePartialFromCodedStream(&input);
1316
return PyInt_FromLong(self->message->ByteSize());
1318
return PyInt_FromLong(-1);
1322
static PyObject* CMessage_ByteSize(CMessage* self, PyObject* args) {
1323
return PyLong_FromLong(self->message->ByteSize());
1326
static PyObject* CMessage_SetInParent(CMessage* self, PyObject* args) {
1327
AssureWritable(self);
1331
static PyObject* CMessage_SwapRepeatedFieldElements(
1332
CMessage* self, PyObject* args) {
1333
CFieldDescriptor* cfield_descriptor;
1335
if (!PyArg_ParseTuple(args, C("O!ii:SwapRepeatedFieldElements"),
1336
&CFieldDescriptor_Type, &cfield_descriptor,
1337
&index1, &index2)) {
1341
google::protobuf::Message* message = self->message;
1342
const google::protobuf::Reflection* reflection = message->GetReflection();
1344
reflection->SwapElements(
1345
message, cfield_descriptor->descriptor, index1, index2);
1349
static PyObject* CMessage_AddMessage(CMessage* self, PyObject* arg) {
1350
CFieldDescriptor* cfield_descriptor;
1351
if (!PyObject_TypeCheck(reinterpret_cast<PyObject *>(arg),
1352
&CFieldDescriptor_Type)) {
1353
PyErr_SetString(PyExc_TypeError, "Must be a field descriptor");
1356
cfield_descriptor = reinterpret_cast<CFieldDescriptor*>(arg);
1357
AssureWritable(self);
1359
CMessage* py_cmsg = PyObject_New(CMessage, &CMessage_Type);
1360
if (py_cmsg == NULL) {
1364
google::protobuf::Message* message = self->message;
1365
const google::protobuf::Reflection* reflection = message->GetReflection();
1366
google::protobuf::Message* sub_message =
1367
reflection->AddMessage(message, cfield_descriptor->descriptor);
1369
py_cmsg->parent = NULL;
1370
py_cmsg->full_name = sub_message->GetDescriptor()->full_name().c_str();
1371
py_cmsg->message = sub_message;
1372
py_cmsg->free_message = false;
1373
py_cmsg->read_only = false;
1374
return reinterpret_cast<PyObject*>(py_cmsg);
1377
static PyObject* CMessage_GetRepeatedMessage(CMessage* self, PyObject* args) {
1378
CFieldDescriptor* cfield_descriptor;
1380
if (!PyArg_ParseTuple(args, C("O!O:GetRepeatedMessage"),
1381
&CFieldDescriptor_Type, &cfield_descriptor, &slice)) {
1385
return InternalGetRepeatedScalarSlice(
1386
self, cfield_descriptor->descriptor, slice);
1389
static PyObject* CMessage_NewSubMessage(CMessage* self, PyObject* arg) {
1390
CFieldDescriptor* cfield_descriptor;
1391
if (!PyObject_TypeCheck(reinterpret_cast<PyObject *>(arg),
1392
&CFieldDescriptor_Type)) {
1393
PyErr_SetString(PyExc_TypeError, "Must be a field descriptor");
1396
cfield_descriptor = reinterpret_cast<CFieldDescriptor*>(arg);
1398
CMessage* py_cmsg = PyObject_New(CMessage, &CMessage_Type);
1399
if (py_cmsg == NULL) {
1403
google::protobuf::Message* message = self->message;
1404
const google::protobuf::Reflection* reflection = message->GetReflection();
1405
const google::protobuf::Message& sub_message =
1406
reflection->GetMessage(*message, cfield_descriptor->descriptor,
1407
global_message_factory);
1409
py_cmsg->full_name = sub_message.GetDescriptor()->full_name().c_str();
1410
py_cmsg->parent = self;
1411
py_cmsg->parent_field = cfield_descriptor;
1412
py_cmsg->message = const_cast<google::protobuf::Message*>(&sub_message);
1413
py_cmsg->free_message = false;
1414
py_cmsg->read_only = true;
1415
return reinterpret_cast<PyObject*>(py_cmsg);
1418
static PyObject* CMessage_MutableMessage(CMessage* self, PyObject* arg) {
1419
CFieldDescriptor* cfield_descriptor;
1420
if (!PyObject_TypeCheck(reinterpret_cast<PyObject *>(arg),
1421
&CFieldDescriptor_Type)) {
1422
PyErr_SetString(PyExc_TypeError, "Must be a field descriptor");
1425
cfield_descriptor = reinterpret_cast<CFieldDescriptor*>(arg);
1426
AssureWritable(self);
1428
CMessage* py_cmsg = PyObject_New(CMessage, &CMessage_Type);
1429
if (py_cmsg == NULL) {
1433
google::protobuf::Message* message = self->message;
1434
const google::protobuf::Reflection* reflection = message->GetReflection();
1435
google::protobuf::Message* mutable_message =
1436
reflection->MutableMessage(message, cfield_descriptor->descriptor,
1437
global_message_factory);
1439
py_cmsg->full_name = mutable_message->GetDescriptor()->full_name().c_str();
1440
py_cmsg->message = mutable_message;
1441
py_cmsg->free_message = false;
1442
py_cmsg->read_only = false;
1443
return reinterpret_cast<PyObject*>(py_cmsg);
1446
static PyObject* CMessage_Equals(CMessage* self, PyObject* arg) {
1447
CMessage* other_message;
1448
if (!PyObject_TypeCheck(reinterpret_cast<PyObject *>(arg), &CMessage_Type)) {
1449
PyErr_SetString(PyExc_TypeError, "Must be a message");
1452
other_message = reinterpret_cast<CMessage*>(arg);
1454
if (other_message->message == self->message) {
1455
return PyBool_FromLong(1);
1458
if (other_message->message->GetDescriptor() !=
1459
self->message->GetDescriptor()) {
1460
return PyBool_FromLong(0);
1463
return PyBool_FromLong(1);
1466
static PyObject* CMessage_ListFields(CMessage* self, PyObject* args) {
1467
google::protobuf::Message* message = self->message;
1468
const google::protobuf::Reflection* reflection = message->GetReflection();
1469
vector<const google::protobuf::FieldDescriptor*> fields;
1470
reflection->ListFields(*message, &fields);
1472
PyObject* list = PyList_New(fields.size());
1477
for (unsigned int i = 0; i < fields.size(); ++i) {
1478
bool is_extension = fields[i]->is_extension();
1479
PyObject* t = PyTuple_New(2);
1485
PyObject* is_extension_object = PyBool_FromLong(is_extension ? 1 : 0);
1487
PyObject* field_name;
1490
s = &fields[i]->full_name();
1492
s = &fields[i]->name();
1494
field_name = PyString_FromStringAndSize(s->c_str(), s->length());
1495
if (field_name == NULL) {
1501
PyTuple_SET_ITEM(t, 0, is_extension_object);
1502
PyTuple_SET_ITEM(t, 1, field_name);
1503
PyList_SET_ITEM(list, i, t);
1509
static PyObject* CMessage_FindInitializationErrors(CMessage* self) {
1510
google::protobuf::Message* message = self->message;
1511
vector<string> errors;
1512
message->FindInitializationErrors(&errors);
1514
PyObject* error_list = PyList_New(errors.size());
1515
if (error_list == NULL) {
1518
for (unsigned int i = 0; i < errors.size(); ++i) {
1519
const string& error = errors[i];
1520
PyObject* error_string = PyString_FromStringAndSize(
1521
error.c_str(), error.length());
1522
if (error_string == NULL) {
1523
Py_DECREF(error_list);
1526
PyList_SET_ITEM(error_list, i, error_string);
1531
// ------ Python Constructor:
1533
PyObject* Python_NewCMessage(PyObject* ignored, PyObject* arg) {
1534
const char* message_type = PyString_AsString(arg);
1535
if (message_type == NULL) {
1539
const google::protobuf::Message* message = CreateMessage(message_type);
1540
if (message == NULL) {
1541
PyErr_Format(PyExc_TypeError, "Couldn't create message of type %s!",
1546
CMessage* py_cmsg = PyObject_New(CMessage, &CMessage_Type);
1547
if (py_cmsg == NULL) {
1550
py_cmsg->message = message->New();
1551
py_cmsg->free_message = true;
1552
py_cmsg->full_name = message->GetDescriptor()->full_name().c_str();
1553
py_cmsg->read_only = false;
1554
py_cmsg->parent = NULL;
1555
py_cmsg->parent_field = NULL;
1556
return reinterpret_cast<PyObject*>(py_cmsg);
1559
// --- Module Functions (exposed to Python):
1561
PyMethodDef methods[] = {
1562
{ C("NewCMessage"), (PyCFunction)Python_NewCMessage,
1564
C("Creates a new C++ protocol message, given its full name.") },
1565
{ C("NewCDescriptorPool"), (PyCFunction)Python_NewCDescriptorPool,
1567
C("Creates a new C++ descriptor pool.") },
1568
{ C("BuildFile"), (PyCFunction)Python_BuildFile,
1570
C("Registers a new protocol buffer file in the global C++ descriptor "
1575
// --- Exposing the C proto living inside Python proto to C code:
1577
extern const Message* (*GetCProtoInsidePyProtoPtr)(PyObject* msg);
1578
extern Message* (*MutableCProtoInsidePyProtoPtr)(PyObject* msg);
1580
static const google::protobuf::Message* GetCProtoInsidePyProtoImpl(PyObject* msg) {
1581
PyObject* c_msg_obj = PyObject_GetAttrString(msg, "_cmsg");
1582
if (c_msg_obj == NULL) {
1586
Py_DECREF(c_msg_obj);
1587
if (!PyObject_TypeCheck(c_msg_obj, &CMessage_Type)) {
1590
CMessage* c_msg = reinterpret_cast<CMessage*>(c_msg_obj);
1591
return c_msg->message;
1594
static google::protobuf::Message* MutableCProtoInsidePyProtoImpl(PyObject* msg) {
1595
PyObject* c_msg_obj = PyObject_GetAttrString(msg, "_cmsg");
1596
if (c_msg_obj == NULL) {
1600
Py_DECREF(c_msg_obj);
1601
if (!PyObject_TypeCheck(c_msg_obj, &CMessage_Type)) {
1604
CMessage* c_msg = reinterpret_cast<CMessage*>(c_msg_obj);
1605
AssureWritable(c_msg);
1606
return c_msg->message;
1609
// --- Module Init Function:
1611
static const char module_docstring[] =
1612
"python-proto2 is a module that can be used to enhance proto2 Python API\n"
1615
"It provides access to the protocol buffers C++ reflection API that\n"
1616
"implements the basic protocol buffer functions.";
1619
void init_net_proto2___python() {
1620
// Initialize constants.
1621
kPythonZero = PyInt_FromLong(0);
1622
kint32min_py = PyInt_FromLong(kint32min);
1623
kint32max_py = PyInt_FromLong(kint32max);
1624
kuint32max_py = PyLong_FromLongLong(kuint32max);
1625
kint64min_py = PyLong_FromLongLong(kint64min);
1626
kint64max_py = PyLong_FromLongLong(kint64max);
1627
kuint64max_py = PyLong_FromUnsignedLongLong(kuint64max);
1629
global_message_factory = new DynamicMessageFactory(GetDescriptorPool());
1630
global_message_factory->SetDelegateToGeneratedFactory(true);
1632
// Export our functions to Python.
1634
m = Py_InitModule3(C("_net_proto2___python"), methods, C(module_docstring));
1641
CMessage_Type.tp_new = PyType_GenericNew;
1642
if (PyType_Ready(&CMessage_Type) < 0) {
1646
if (!InitDescriptor()) {
1650
// Override {Get,Mutable}CProtoInsidePyProto.
1651
GetCProtoInsidePyProtoPtr = GetCProtoInsidePyProtoImpl;
1652
MutableCProtoInsidePyProtoPtr = MutableCProtoInsidePyProtoImpl;
1656
} // namespace python
1657
} // namespace protobuf
1658
} // namespace google