Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(4)

Side by Side Diff: third_party/protobuf/python/google/protobuf/pyext/message.cc

Issue 2495533002: third_party/protobuf: Update to HEAD (83d681ee2c) (Closed)
Patch Set: Created 4 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Protocol Buffers - Google's data interchange format 1 // Protocol Buffers - Google's data interchange format
2 // Copyright 2008 Google Inc. All rights reserved. 2 // Copyright 2008 Google Inc. All rights reserved.
3 // https://developers.google.com/protocol-buffers/ 3 // https://developers.google.com/protocol-buffers/
4 // 4 //
5 // Redistribution and use in source and binary forms, with or without 5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are 6 // modification, are permitted provided that the following conditions are
7 // met: 7 // met:
8 // 8 //
9 // * Redistributions of source code must retain the above copyright 9 // * Redistributions of source code must retain the above copyright
10 // notice, this list of conditions and the following disclaimer. 10 // notice, this list of conditions and the following disclaimer.
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
56 #include <google/protobuf/descriptor.h> 56 #include <google/protobuf/descriptor.h>
57 #include <google/protobuf/message.h> 57 #include <google/protobuf/message.h>
58 #include <google/protobuf/text_format.h> 58 #include <google/protobuf/text_format.h>
59 #include <google/protobuf/unknown_field_set.h> 59 #include <google/protobuf/unknown_field_set.h>
60 #include <google/protobuf/pyext/descriptor.h> 60 #include <google/protobuf/pyext/descriptor.h>
61 #include <google/protobuf/pyext/descriptor_pool.h> 61 #include <google/protobuf/pyext/descriptor_pool.h>
62 #include <google/protobuf/pyext/extension_dict.h> 62 #include <google/protobuf/pyext/extension_dict.h>
63 #include <google/protobuf/pyext/repeated_composite_container.h> 63 #include <google/protobuf/pyext/repeated_composite_container.h>
64 #include <google/protobuf/pyext/repeated_scalar_container.h> 64 #include <google/protobuf/pyext/repeated_scalar_container.h>
65 #include <google/protobuf/pyext/map_container.h> 65 #include <google/protobuf/pyext/map_container.h>
66 #include <google/protobuf/pyext/message_factory.h>
66 #include <google/protobuf/pyext/scoped_pyobject_ptr.h> 67 #include <google/protobuf/pyext/scoped_pyobject_ptr.h>
67 #include <google/protobuf/stubs/strutil.h> 68 #include <google/protobuf/stubs/strutil.h>
68 69
69 #if PY_MAJOR_VERSION >= 3 70 #if PY_MAJOR_VERSION >= 3
70 #define PyInt_Check PyLong_Check 71 #define PyInt_Check PyLong_Check
71 #define PyInt_AsLong PyLong_AsLong 72 #define PyInt_AsLong PyLong_AsLong
72 #define PyInt_FromLong PyLong_FromLong 73 #define PyInt_FromLong PyLong_FromLong
73 #define PyInt_FromSize_t PyLong_FromSize_t 74 #define PyInt_FromSize_t PyLong_FromSize_t
74 #define PyString_Check PyUnicode_Check 75 #define PyString_Check PyUnicode_Check
75 #define PyString_FromString PyUnicode_FromString 76 #define PyString_FromString PyUnicode_FromString
(...skipping 161 matching lines...) Expand 10 before | Expand all | Expand 10 after
237 if (py_descriptor == NULL) { 238 if (py_descriptor == NULL) {
238 PyErr_SetString(PyExc_TypeError, "Message class has no DESCRIPTOR"); 239 PyErr_SetString(PyExc_TypeError, "Message class has no DESCRIPTOR");
239 return NULL; 240 return NULL;
240 } 241 }
241 if (!PyObject_TypeCheck(py_descriptor, &PyMessageDescriptor_Type)) { 242 if (!PyObject_TypeCheck(py_descriptor, &PyMessageDescriptor_Type)) {
242 PyErr_Format(PyExc_TypeError, "Expected a message Descriptor, got %s", 243 PyErr_Format(PyExc_TypeError, "Expected a message Descriptor, got %s",
243 py_descriptor->ob_type->tp_name); 244 py_descriptor->ob_type->tp_name);
244 return NULL; 245 return NULL;
245 } 246 }
246 247
248 // Messages have no __dict__
249 ScopedPyObjectPtr slots(PyTuple_New(0));
250 if (PyDict_SetItemString(dict, "__slots__", slots.get()) < 0) {
251 return NULL;
252 }
253
247 // Build the arguments to the base metaclass. 254 // Build the arguments to the base metaclass.
248 // We change the __bases__ classes. 255 // We change the __bases__ classes.
249 ScopedPyObjectPtr new_args; 256 ScopedPyObjectPtr new_args;
250 const Descriptor* message_descriptor = 257 const Descriptor* message_descriptor =
251 PyMessageDescriptor_AsDescriptor(py_descriptor); 258 PyMessageDescriptor_AsDescriptor(py_descriptor);
252 if (message_descriptor == NULL) { 259 if (message_descriptor == NULL) {
253 return NULL; 260 return NULL;
254 } 261 }
255 262
256 if (WKT_classes == NULL) { 263 if (WKT_classes == NULL) {
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
293 const Descriptor* descriptor = 300 const Descriptor* descriptor =
294 PyMessageDescriptor_AsDescriptor(py_descriptor); 301 PyMessageDescriptor_AsDescriptor(py_descriptor);
295 if (descriptor == NULL) { 302 if (descriptor == NULL) {
296 return NULL; 303 return NULL;
297 } 304 }
298 Py_INCREF(py_descriptor); 305 Py_INCREF(py_descriptor);
299 newtype->py_message_descriptor = py_descriptor; 306 newtype->py_message_descriptor = py_descriptor;
300 newtype->message_descriptor = descriptor; 307 newtype->message_descriptor = descriptor;
301 // TODO(amauryfa): Don't always use the canonical pool of the descriptor, 308 // TODO(amauryfa): Don't always use the canonical pool of the descriptor,
302 // use the MessageFactory optionally passed in the class dict. 309 // use the MessageFactory optionally passed in the class dict.
303 newtype->py_descriptor_pool = GetDescriptorPool_FromPool( 310 PyDescriptorPool* py_descriptor_pool =
304 descriptor->file()->pool()); 311 GetDescriptorPool_FromPool(descriptor->file()->pool());
305 if (newtype->py_descriptor_pool == NULL) { 312 if (py_descriptor_pool == NULL) {
306 return NULL; 313 return NULL;
307 } 314 }
308 Py_INCREF(newtype->py_descriptor_pool); 315 newtype->py_message_factory = py_descriptor_pool->py_message_factory;
316 Py_INCREF(newtype->py_message_factory);
309 317
310 // Add the message to the DescriptorPool. 318 // Register the message in the MessageFactory.
311 if (cdescriptor_pool::RegisterMessageClass(newtype->py_descriptor_pool, 319 // TODO(amauryfa): Move this call to MessageFactory.GetPrototype() when the
312 descriptor, newtype) < 0) { 320 // MessageFactory is fully implemented in C++.
321 if (message_factory::RegisterMessageClass(newtype->py_message_factory,
322 descriptor, newtype) < 0) {
313 return NULL; 323 return NULL;
314 } 324 }
315 325
316 // Continue with type initialization: add other descriptors, enum values... 326 // Continue with type initialization: add other descriptors, enum values...
317 if (AddDescriptors(result.get(), descriptor) < 0) { 327 if (AddDescriptors(result.get(), descriptor) < 0) {
318 return NULL; 328 return NULL;
319 } 329 }
320 return result.release(); 330 return result.release();
321 } 331 }
322 332
323 static void Dealloc(CMessageClass *self) { 333 static void Dealloc(CMessageClass *self) {
324 Py_DECREF(self->py_message_descriptor); 334 Py_XDECREF(self->py_message_descriptor);
325 Py_DECREF(self->py_descriptor_pool); 335 Py_XDECREF(self->py_message_factory);
326 Py_TYPE(self)->tp_free(reinterpret_cast<PyObject*>(self)); 336 Py_TYPE(self)->tp_free(reinterpret_cast<PyObject*>(self));
327 } 337 }
328 338
329 339
330 // This function inserts and empty weakref at the end of the list of 340 // This function inserts and empty weakref at the end of the list of
331 // subclasses for the main protocol buffer Message class. 341 // subclasses for the main protocol buffer Message class.
332 // 342 //
333 // This eliminates a O(n^2) behaviour in the internal add_subclass 343 // This eliminates a O(n^2) behaviour in the internal add_subclass
334 // routine. 344 // routine.
335 static int InsertEmptyWeakref(PyTypeObject *base_type) { 345 static int InsertEmptyWeakref(PyTypeObject *base_type) {
(...skipping 409 matching lines...) Expand 10 before | Expand all | Expand 10 after
745 return true; 755 return true;
746 } 756 }
747 PyErr_Format(PyExc_KeyError, "Field '%s' does not belong to message '%s'", 757 PyErr_Format(PyExc_KeyError, "Field '%s' does not belong to message '%s'",
748 field_descriptor->full_name().c_str(), 758 field_descriptor->full_name().c_str(),
749 message->GetDescriptor()->full_name().c_str()); 759 message->GetDescriptor()->full_name().c_str());
750 return false; 760 return false;
751 } 761 }
752 762
753 namespace cmessage { 763 namespace cmessage {
754 764
755 PyDescriptorPool* GetDescriptorPoolForMessage(CMessage* message) { 765 PyMessageFactory* GetFactoryForMessage(CMessage* message) {
756 // No need to check the type: the type of instances of CMessage is always
757 // an instance of CMessageClass. Let's prove it with a debug-only check.
758 GOOGLE_DCHECK(PyObject_TypeCheck(message, &CMessage_Type)); 766 GOOGLE_DCHECK(PyObject_TypeCheck(message, &CMessage_Type));
759 return reinterpret_cast<CMessageClass*>(Py_TYPE(message))->py_descriptor_pool; 767 return reinterpret_cast<CMessageClass*>(Py_TYPE(message))->py_message_factory;
760 }
761
762 MessageFactory* GetFactoryForMessage(CMessage* message) {
763 return GetDescriptorPoolForMessage(message)->message_factory;
764 } 768 }
765 769
766 static int MaybeReleaseOverlappingOneofField( 770 static int MaybeReleaseOverlappingOneofField(
767 CMessage* cmessage, 771 CMessage* cmessage,
768 const FieldDescriptor* field) { 772 const FieldDescriptor* field) {
769 #ifdef GOOGLE_PROTOBUF_HAS_ONEOF 773 #ifdef GOOGLE_PROTOBUF_HAS_ONEOF
770 Message* message = cmessage->message; 774 Message* message = cmessage->message;
771 const Reflection* reflection = message->GetReflection(); 775 const Reflection* reflection = message->GetReflection();
772 if (!field->containing_oneof() || 776 if (!field->containing_oneof() ||
773 !reflection->HasOneof(*message, field->containing_oneof()) || 777 !reflection->HasOneof(*message, field->containing_oneof()) ||
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
806 810
807 static Message* GetMutableMessage( 811 static Message* GetMutableMessage(
808 CMessage* parent, 812 CMessage* parent,
809 const FieldDescriptor* parent_field) { 813 const FieldDescriptor* parent_field) {
810 Message* parent_message = parent->message; 814 Message* parent_message = parent->message;
811 const Reflection* reflection = parent_message->GetReflection(); 815 const Reflection* reflection = parent_message->GetReflection();
812 if (MaybeReleaseOverlappingOneofField(parent, parent_field) < 0) { 816 if (MaybeReleaseOverlappingOneofField(parent, parent_field) < 0) {
813 return NULL; 817 return NULL;
814 } 818 }
815 return reflection->MutableMessage( 819 return reflection->MutableMessage(
816 parent_message, parent_field, GetFactoryForMessage(parent)); 820 parent_message, parent_field,
821 GetFactoryForMessage(parent)->message_factory);
817 } 822 }
818 823
819 struct FixupMessageReference : public ChildVisitor { 824 struct FixupMessageReference : public ChildVisitor {
820 // message must outlive this object. 825 // message must outlive this object.
821 explicit FixupMessageReference(Message* message) : 826 explicit FixupMessageReference(Message* message) :
822 message_(message) {} 827 message_(message) {}
823 828
824 int VisitRepeatedCompositeContainer(RepeatedCompositeContainer* container) { 829 int VisitRepeatedCompositeContainer(RepeatedCompositeContainer* container) {
825 container->message = message_; 830 container->message = message_;
826 return 0; 831 return 0;
(...skipping 207 matching lines...) Expand 10 before | Expand all | Expand 10 after
1034 return -1; 1039 return -1;
1035 } 1040 }
1036 } 1041 }
1037 --i; 1042 --i;
1038 } 1043 }
1039 1044
1040 return 0; 1045 return 0;
1041 } 1046 }
1042 1047
1043 // Initializes fields of a message. Used in constructors. 1048 // Initializes fields of a message. Used in constructors.
1044 int InitAttributes(CMessage* self, PyObject* kwargs) { 1049 int InitAttributes(CMessage* self, PyObject* args, PyObject* kwargs) {
1050 if (args != NULL && PyTuple_Size(args) != 0) {
1051 PyErr_SetString(PyExc_TypeError, "No positional arguments allowed");
1052 return -1;
1053 }
1054
1045 if (kwargs == NULL) { 1055 if (kwargs == NULL) {
1046 return 0; 1056 return 0;
1047 } 1057 }
1048 1058
1049 Py_ssize_t pos = 0; 1059 Py_ssize_t pos = 0;
1050 PyObject* name; 1060 PyObject* name;
1051 PyObject* value; 1061 PyObject* value;
1052 while (PyDict_Next(kwargs, &pos, &name, &value)) { 1062 while (PyDict_Next(kwargs, &pos, &name, &value)) {
1053 if (!PyString_Check(name)) { 1063 if (!PyString_Check(name)) {
1054 PyErr_SetString(PyExc_ValueError, "Field name must be a string"); 1064 PyErr_SetString(PyExc_ValueError, "Field name must be a string");
(...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after
1160 return -1; 1170 return -1;
1161 } 1171 }
1162 } 1172 }
1163 } else if (descriptor->cpp_type() == FieldDescriptor::CPPTYPE_MESSAGE) { 1173 } else if (descriptor->cpp_type() == FieldDescriptor::CPPTYPE_MESSAGE) {
1164 ScopedPyObjectPtr message(GetAttr(self, name)); 1174 ScopedPyObjectPtr message(GetAttr(self, name));
1165 if (message == NULL) { 1175 if (message == NULL) {
1166 return -1; 1176 return -1;
1167 } 1177 }
1168 CMessage* cmessage = reinterpret_cast<CMessage*>(message.get()); 1178 CMessage* cmessage = reinterpret_cast<CMessage*>(message.get());
1169 if (PyDict_Check(value)) { 1179 if (PyDict_Check(value)) {
1170 if (InitAttributes(cmessage, value) < 0) { 1180 // Make the message exist even if the dict is empty.
1181 AssureWritable(cmessage);
1182 if (InitAttributes(cmessage, NULL, value) < 0) {
1171 return -1; 1183 return -1;
1172 } 1184 }
1173 } else { 1185 } else {
1174 ScopedPyObjectPtr merged(MergeFrom(cmessage, value)); 1186 ScopedPyObjectPtr merged(MergeFrom(cmessage, value));
1175 if (merged == NULL) { 1187 if (merged == NULL) {
1176 return -1; 1188 return -1;
1177 } 1189 }
1178 } 1190 }
1179 } else { 1191 } else {
1180 ScopedPyObjectPtr new_val; 1192 ScopedPyObjectPtr new_val;
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
1219 PyObject* unused_args, PyObject* unused_kwargs) { 1231 PyObject* unused_args, PyObject* unused_kwargs) {
1220 CMessageClass* type = CheckMessageClass(cls); 1232 CMessageClass* type = CheckMessageClass(cls);
1221 if (type == NULL) { 1233 if (type == NULL) {
1222 return NULL; 1234 return NULL;
1223 } 1235 }
1224 // Retrieve the message descriptor and the default instance (=prototype). 1236 // Retrieve the message descriptor and the default instance (=prototype).
1225 const Descriptor* message_descriptor = type->message_descriptor; 1237 const Descriptor* message_descriptor = type->message_descriptor;
1226 if (message_descriptor == NULL) { 1238 if (message_descriptor == NULL) {
1227 return NULL; 1239 return NULL;
1228 } 1240 }
1229 const Message* default_message = type->py_descriptor_pool->message_factory 1241 const Message* default_message = type->py_message_factory->message_factory
1230 ->GetPrototype(message_descriptor); 1242 ->GetPrototype(message_descriptor);
1231 if (default_message == NULL) { 1243 if (default_message == NULL) {
1232 PyErr_SetString(PyExc_TypeError, message_descriptor->full_name().c_str()); 1244 PyErr_SetString(PyExc_TypeError, message_descriptor->full_name().c_str());
1233 return NULL; 1245 return NULL;
1234 } 1246 }
1235 1247
1236 CMessage* self = NewEmptyMessage(type); 1248 CMessage* self = NewEmptyMessage(type);
1237 if (self == NULL) { 1249 if (self == NULL) {
1238 return NULL; 1250 return NULL;
1239 } 1251 }
1240 self->message = default_message->New(); 1252 self->message = default_message->New();
1241 self->owner.reset(self->message); 1253 self->owner.reset(self->message);
1242 return reinterpret_cast<PyObject*>(self); 1254 return reinterpret_cast<PyObject*>(self);
1243 } 1255 }
1244 1256
1245 // The __init__ method of Message classes. 1257 // The __init__ method of Message classes.
1246 // It initializes fields from keywords passed to the constructor. 1258 // It initializes fields from keywords passed to the constructor.
1247 static int Init(CMessage* self, PyObject* args, PyObject* kwargs) { 1259 static int Init(CMessage* self, PyObject* args, PyObject* kwargs) {
1248 if (PyTuple_Size(args) != 0) { 1260 return InitAttributes(self, args, kwargs);
1249 PyErr_SetString(PyExc_TypeError, "No positional arguments allowed");
1250 return -1;
1251 }
1252
1253 return InitAttributes(self, kwargs);
1254 } 1261 }
1255 1262
1256 // --------------------------------------------------------------------- 1263 // ---------------------------------------------------------------------
1257 // Deallocating a CMessage 1264 // Deallocating a CMessage
1258 // 1265 //
1259 // Deallocating a CMessage requires that we clear any weak references 1266 // Deallocating a CMessage requires that we clear any weak references
1260 // from children to the message being deallocated. 1267 // from children to the message being deallocated.
1261 1268
1262 // Clear the weak reference from the child to the parent. 1269 // Clear the weak reference from the child to the parent.
1263 struct ClearWeakReferences : public ChildVisitor { 1270 struct ClearWeakReferences : public ChildVisitor {
(...skipping 21 matching lines...) Expand all
1285 } 1292 }
1286 1293
1287 int VisitCMessage(CMessage* cmessage, 1294 int VisitCMessage(CMessage* cmessage,
1288 const FieldDescriptor* field_descriptor) { 1295 const FieldDescriptor* field_descriptor) {
1289 cmessage->parent = NULL; 1296 cmessage->parent = NULL;
1290 return 0; 1297 return 0;
1291 } 1298 }
1292 }; 1299 };
1293 1300
1294 static void Dealloc(CMessage* self) { 1301 static void Dealloc(CMessage* self) {
1302 if (self->weakreflist) {
1303 PyObject_ClearWeakRefs(reinterpret_cast<PyObject*>(self));
1304 }
1295 // Null out all weak references from children to this message. 1305 // Null out all weak references from children to this message.
1296 GOOGLE_CHECK_EQ(0, ForEachCompositeField(self, ClearWeakReferences())); 1306 GOOGLE_CHECK_EQ(0, ForEachCompositeField(self, ClearWeakReferences()));
1297 if (self->extensions) { 1307 if (self->extensions) {
1298 self->extensions->parent = NULL; 1308 self->extensions->parent = NULL;
1299 } 1309 }
1300 1310
1301 Py_CLEAR(self->extensions); 1311 Py_CLEAR(self->extensions);
1302 Py_CLEAR(self->composite_fields); 1312 Py_CLEAR(self->composite_fields);
1303 self->owner.reset(); 1313 self->owner.reset();
1304 Py_TYPE(self)->tp_free(reinterpret_cast<PyObject*>(self)); 1314 Py_TYPE(self)->tp_free(reinterpret_cast<PyObject*>(self));
(...skipping 147 matching lines...) Expand 10 before | Expand all | Expand 10 after
1452 for (int i = 0; i < unknown_field_set.field_count(); ++i) { 1462 for (int i = 0; i < unknown_field_set.field_count(); ++i) {
1453 if (unknown_field_set.field(i).number() == field_descriptor->number()) { 1463 if (unknown_field_set.field(i).number() == field_descriptor->number()) {
1454 Py_RETURN_TRUE; 1464 Py_RETURN_TRUE;
1455 } 1465 }
1456 } 1466 }
1457 } 1467 }
1458 Py_RETURN_FALSE; 1468 Py_RETURN_FALSE;
1459 } 1469 }
1460 1470
1461 PyObject* ClearExtension(CMessage* self, PyObject* extension) { 1471 PyObject* ClearExtension(CMessage* self, PyObject* extension) {
1472 const FieldDescriptor* descriptor = GetExtensionDescriptor(extension);
1473 if (descriptor == NULL) {
1474 return NULL;
1475 }
1462 if (self->extensions != NULL) { 1476 if (self->extensions != NULL) {
1463 return extension_dict::ClearExtension(self->extensions, extension); 1477 PyObject* value = PyDict_GetItem(self->extensions->values, extension);
1464 } else { 1478 if (value != NULL) {
1465 const FieldDescriptor* descriptor = GetExtensionDescriptor(extension); 1479 if (InternalReleaseFieldByDescriptor(self, descriptor, value) < 0) {
1466 if (descriptor == NULL) { 1480 return NULL;
1467 return NULL; 1481 }
1468 } 1482 PyDict_DelItem(self->extensions->values, extension);
1469 if (ScopedPyObjectPtr(ClearFieldByDescriptor(self, descriptor)) == NULL) {
1470 return NULL;
1471 } 1483 }
1472 } 1484 }
1473 Py_RETURN_NONE; 1485 return ClearFieldByDescriptor(self, descriptor);
1474 } 1486 }
1475 1487
1476 PyObject* HasExtension(CMessage* self, PyObject* extension) { 1488 PyObject* HasExtension(CMessage* self, PyObject* extension) {
1477 const FieldDescriptor* descriptor = GetExtensionDescriptor(extension); 1489 const FieldDescriptor* descriptor = GetExtensionDescriptor(extension);
1478 if (descriptor == NULL) { 1490 if (descriptor == NULL) {
1479 return NULL; 1491 return NULL;
1480 } 1492 }
1481 return HasFieldByDescriptor(self, descriptor); 1493 return HasFieldByDescriptor(self, descriptor);
1482 } 1494 }
1483 1495
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after
1549 return -1; 1561 return -1;
1550 return 0; 1562 return 0;
1551 } 1563 }
1552 1564
1553 // Releases the message specified by 'field' and returns the 1565 // Releases the message specified by 'field' and returns the
1554 // pointer. If the field does not exist a new message is created using 1566 // pointer. If the field does not exist a new message is created using
1555 // 'descriptor'. The caller takes ownership of the returned pointer. 1567 // 'descriptor'. The caller takes ownership of the returned pointer.
1556 Message* ReleaseMessage(CMessage* self, 1568 Message* ReleaseMessage(CMessage* self,
1557 const Descriptor* descriptor, 1569 const Descriptor* descriptor,
1558 const FieldDescriptor* field_descriptor) { 1570 const FieldDescriptor* field_descriptor) {
1559 MessageFactory* message_factory = GetFactoryForMessage(self); 1571 MessageFactory* message_factory = GetFactoryForMessage(self)->message_factory;
1560 Message* released_message = self->message->GetReflection()->ReleaseMessage( 1572 Message* released_message = self->message->GetReflection()->ReleaseMessage(
1561 self->message, field_descriptor, message_factory); 1573 self->message, field_descriptor, message_factory);
1562 // ReleaseMessage will return NULL which differs from 1574 // ReleaseMessage will return NULL which differs from
1563 // child_cmessage->message, if the field does not exist. In this case, 1575 // child_cmessage->message, if the field does not exist. In this case,
1564 // the latter points to the default instance via a const_cast<>, so we 1576 // the latter points to the default instance via a const_cast<>, so we
1565 // have to reset it to a new mutable object since we are taking ownership. 1577 // have to reset it to a new mutable object since we are taking ownership.
1566 if (released_message == NULL) { 1578 if (released_message == NULL) {
1567 const Message* prototype = message_factory->GetPrototype(descriptor); 1579 const Message* prototype = message_factory->GetPrototype(descriptor);
1568 GOOGLE_DCHECK(prototype != NULL); 1580 GOOGLE_DCHECK(prototype != NULL);
1569 released_message = prototype->New(); 1581 released_message = prototype->New();
(...skipping 16 matching lines...) Expand all
1586 return ForEachCompositeField(child_cmessage, 1598 return ForEachCompositeField(child_cmessage,
1587 SetOwnerVisitor(child_cmessage->owner)); 1599 SetOwnerVisitor(child_cmessage->owner));
1588 } 1600 }
1589 1601
1590 struct ReleaseChild : public ChildVisitor { 1602 struct ReleaseChild : public ChildVisitor {
1591 // message must outlive this object. 1603 // message must outlive this object.
1592 explicit ReleaseChild(CMessage* parent) : 1604 explicit ReleaseChild(CMessage* parent) :
1593 parent_(parent) {} 1605 parent_(parent) {}
1594 1606
1595 int VisitRepeatedCompositeContainer(RepeatedCompositeContainer* container) { 1607 int VisitRepeatedCompositeContainer(RepeatedCompositeContainer* container) {
1596 return repeated_composite_container::Release( 1608 return repeated_composite_container::Release(container);
1597 reinterpret_cast<RepeatedCompositeContainer*>(container));
1598 } 1609 }
1599 1610
1600 int VisitRepeatedScalarContainer(RepeatedScalarContainer* container) { 1611 int VisitRepeatedScalarContainer(RepeatedScalarContainer* container) {
1601 return repeated_scalar_container::Release( 1612 return repeated_scalar_container::Release(container);
1602 reinterpret_cast<RepeatedScalarContainer*>(container));
1603 } 1613 }
1604 1614
1605 int VisitMapContainer(MapContainer* container) { 1615 int VisitMapContainer(MapContainer* container) {
1606 return reinterpret_cast<MapContainer*>(container)->Release(); 1616 return container->Release();
1607 } 1617 }
1608 1618
1609 int VisitCMessage(CMessage* cmessage, 1619 int VisitCMessage(CMessage* cmessage,
1610 const FieldDescriptor* field_descriptor) { 1620 const FieldDescriptor* field_descriptor) {
1611 return ReleaseSubMessage(parent_, field_descriptor, 1621 return ReleaseSubMessage(parent_, field_descriptor, cmessage);
1612 reinterpret_cast<CMessage*>(cmessage));
1613 } 1622 }
1614 1623
1615 CMessage* parent_; 1624 CMessage* parent_;
1616 }; 1625 };
1617 1626
1618 int InternalReleaseFieldByDescriptor( 1627 int InternalReleaseFieldByDescriptor(
1619 CMessage* self, 1628 CMessage* self,
1620 const FieldDescriptor* field_descriptor, 1629 const FieldDescriptor* field_descriptor,
1621 PyObject* composite_field) { 1630 PyObject* composite_field) {
1622 return VisitCompositeField( 1631 return VisitCompositeField(
1623 field_descriptor, 1632 field_descriptor,
1624 composite_field, 1633 composite_field,
1625 ReleaseChild(self)); 1634 ReleaseChild(self));
1626 } 1635 }
1627 1636
1628 PyObject* ClearFieldByDescriptor( 1637 PyObject* ClearFieldByDescriptor(
1629 CMessage* self, 1638 CMessage* self,
1630 const FieldDescriptor* descriptor) { 1639 const FieldDescriptor* field_descriptor) {
1631 if (!CheckFieldBelongsToMessage(descriptor, self->message)) { 1640 if (!CheckFieldBelongsToMessage(field_descriptor, self->message)) {
1632 return NULL; 1641 return NULL;
1633 } 1642 }
1634 AssureWritable(self); 1643 AssureWritable(self);
1635 self->message->GetReflection()->ClearField(self->message, descriptor); 1644 Message* message = self->message;
1645 message->GetReflection()->ClearField(message, field_descriptor);
1646 if (field_descriptor->cpp_type() == FieldDescriptor::CPPTYPE_ENUM &&
1647 !message->GetReflection()->SupportsUnknownEnumValues()) {
1648 UnknownFieldSet* unknown_field_set =
1649 message->GetReflection()->MutableUnknownFields(message);
1650 unknown_field_set->DeleteByNumber(field_descriptor->number());
1651 }
1636 Py_RETURN_NONE; 1652 Py_RETURN_NONE;
1637 } 1653 }
1638 1654
1639 PyObject* ClearField(CMessage* self, PyObject* arg) { 1655 PyObject* ClearField(CMessage* self, PyObject* arg) {
1640 if (!PyString_Check(arg)) { 1656 if (!PyString_Check(arg)) {
1641 PyErr_SetString(PyExc_TypeError, "field name must be a string"); 1657 PyErr_SetString(PyExc_TypeError, "field name must be a string");
1642 return NULL; 1658 return NULL;
1643 } 1659 }
1644 #if PY_MAJOR_VERSION < 3 1660 #if PY_MAJOR_VERSION < 3
1645 const char* field_name = PyString_AS_STRING(arg); 1661 const char* field_name = PyString_AS_STRING(arg);
(...skipping 15 matching lines...) Expand all
1661 return NULL; 1677 return NULL;
1662 } else { 1678 } else {
1663 Py_RETURN_NONE; 1679 Py_RETURN_NONE;
1664 } 1680 }
1665 } else if (is_in_oneof) { 1681 } else if (is_in_oneof) {
1666 const string& name = field_descriptor->name(); 1682 const string& name = field_descriptor->name();
1667 arg_in_oneof.reset(PyString_FromStringAndSize(name.c_str(), name.size())); 1683 arg_in_oneof.reset(PyString_FromStringAndSize(name.c_str(), name.size()));
1668 arg = arg_in_oneof.get(); 1684 arg = arg_in_oneof.get();
1669 } 1685 }
1670 1686
1671 PyObject* composite_field = self->composite_fields ? 1687 // Release the field if it exists in the dict of composite fields.
1672 PyDict_GetItem(self->composite_fields, arg) : NULL; 1688 if (self->composite_fields) {
1673 1689 PyObject* value = PyDict_GetItem(self->composite_fields, arg);
1674 // Only release the field if there's a possibility that there are 1690 if (value != NULL) {
1675 // references to it. 1691 if (InternalReleaseFieldByDescriptor(self, field_descriptor, value) < 0) {
1676 if (composite_field != NULL) { 1692 return NULL;
1677 if (InternalReleaseFieldByDescriptor(self, field_descriptor, 1693 }
1678 composite_field) < 0) { 1694 PyDict_DelItem(self->composite_fields, arg);
1679 return NULL;
1680 } 1695 }
1681 PyDict_DelItem(self->composite_fields, arg);
1682 } 1696 }
1683 message->GetReflection()->ClearField(message, field_descriptor); 1697 return ClearFieldByDescriptor(self, field_descriptor);
1684 if (field_descriptor->cpp_type() == FieldDescriptor::CPPTYPE_ENUM &&
1685 !message->GetReflection()->SupportsUnknownEnumValues()) {
1686 UnknownFieldSet* unknown_field_set =
1687 message->GetReflection()->MutableUnknownFields(message);
1688 unknown_field_set->DeleteByNumber(field_descriptor->number());
1689 }
1690
1691 Py_RETURN_NONE;
1692 } 1698 }
1693 1699
1694 PyObject* Clear(CMessage* self) { 1700 PyObject* Clear(CMessage* self) {
1695 AssureWritable(self); 1701 AssureWritable(self);
1696 if (ForEachCompositeField(self, ReleaseChild(self)) == -1) 1702 if (ForEachCompositeField(self, ReleaseChild(self)) == -1)
1697 return NULL; 1703 return NULL;
1698 Py_CLEAR(self->extensions); 1704 Py_CLEAR(self->extensions);
1699 if (self->composite_fields) { 1705 if (self->composite_fields) {
1700 PyDict_Clear(self->composite_fields); 1706 PyDict_Clear(self->composite_fields);
1701 } 1707 }
(...skipping 190 matching lines...) Expand 10 before | Expand all | Expand 10 after
1892 1898
1893 Py_RETURN_NONE; 1899 Py_RETURN_NONE;
1894 } 1900 }
1895 1901
1896 // Protobuf has a 64MB limit built in, this variable will override this. Please 1902 // Protobuf has a 64MB limit built in, this variable will override this. Please
1897 // do not enable this unless you fully understand the implications: protobufs 1903 // do not enable this unless you fully understand the implications: protobufs
1898 // must all be kept in memory at the same time, so if they grow too big you may 1904 // must all be kept in memory at the same time, so if they grow too big you may
1899 // get OOM errors. The protobuf APIs do not provide any tools for processing 1905 // get OOM errors. The protobuf APIs do not provide any tools for processing
1900 // protobufs in chunks. If you have protos this big you should break them up if 1906 // protobufs in chunks. If you have protos this big you should break them up if
1901 // it is at all convenient to do so. 1907 // it is at all convenient to do so.
1908 #ifdef PROTOBUF_PYTHON_ALLOW_OVERSIZE_PROTOS
1909 static bool allow_oversize_protos = true;
1910 #else
1902 static bool allow_oversize_protos = false; 1911 static bool allow_oversize_protos = false;
1912 #endif
1903 1913
1904 // Provide a method in the module to set allow_oversize_protos to a boolean 1914 // Provide a method in the module to set allow_oversize_protos to a boolean
1905 // value. This method returns the newly value of allow_oversize_protos. 1915 // value. This method returns the newly value of allow_oversize_protos.
1906 static PyObject* SetAllowOversizeProtos(PyObject* m, PyObject* arg) { 1916 PyObject* SetAllowOversizeProtos(PyObject* m, PyObject* arg) {
1907 if (!arg || !PyBool_Check(arg)) { 1917 if (!arg || !PyBool_Check(arg)) {
1908 PyErr_SetString(PyExc_TypeError, 1918 PyErr_SetString(PyExc_TypeError,
1909 "Argument to SetAllowOversizeProtos must be boolean"); 1919 "Argument to SetAllowOversizeProtos must be boolean");
1910 return NULL; 1920 return NULL;
1911 } 1921 }
1912 allow_oversize_protos = PyObject_IsTrue(arg); 1922 allow_oversize_protos = PyObject_IsTrue(arg);
1913 if (allow_oversize_protos) { 1923 if (allow_oversize_protos) {
1914 Py_RETURN_TRUE; 1924 Py_RETURN_TRUE;
1915 } else { 1925 } else {
1916 Py_RETURN_FALSE; 1926 Py_RETURN_FALSE;
1917 } 1927 }
1918 } 1928 }
1919 1929
1920 static PyObject* MergeFromString(CMessage* self, PyObject* arg) { 1930 static PyObject* MergeFromString(CMessage* self, PyObject* arg) {
1921 const void* data; 1931 const void* data;
1922 Py_ssize_t data_length; 1932 Py_ssize_t data_length;
1923 if (PyObject_AsReadBuffer(arg, &data, &data_length) < 0) { 1933 if (PyObject_AsReadBuffer(arg, &data, &data_length) < 0) {
1924 return NULL; 1934 return NULL;
1925 } 1935 }
1926 1936
1927 AssureWritable(self); 1937 AssureWritable(self);
1928 io::CodedInputStream input( 1938 io::CodedInputStream input(
1929 reinterpret_cast<const uint8*>(data), data_length); 1939 reinterpret_cast<const uint8*>(data), data_length);
1930 if (allow_oversize_protos) { 1940 if (allow_oversize_protos) {
1931 input.SetTotalBytesLimit(INT_MAX, INT_MAX); 1941 input.SetTotalBytesLimit(INT_MAX, INT_MAX);
1932 } 1942 }
1933 PyDescriptorPool* pool = GetDescriptorPoolForMessage(self); 1943 PyMessageFactory* factory = GetFactoryForMessage(self);
1934 input.SetExtensionRegistry(pool->pool, pool->message_factory); 1944 input.SetExtensionRegistry(factory->pool->pool, factory->message_factory);
1935 bool success = self->message->MergePartialFromCodedStream(&input); 1945 bool success = self->message->MergePartialFromCodedStream(&input);
1936 if (success) { 1946 if (success) {
1937 return PyInt_FromLong(input.CurrentPosition()); 1947 return PyInt_FromLong(input.CurrentPosition());
1938 } else { 1948 } else {
1939 PyErr_Format(DecodeError_class, "Error parsing message"); 1949 PyErr_Format(DecodeError_class, "Error parsing message");
1940 return NULL; 1950 return NULL;
1941 } 1951 }
1942 } 1952 }
1943 1953
1944 static PyObject* ParseFromString(CMessage* self, PyObject* arg) { 1954 static PyObject* ParseFromString(CMessage* self, PyObject* arg) {
(...skipping 159 matching lines...) Expand 10 before | Expand all | Expand 10 after
2104 ScopedPyObjectPtr extension_field( 2114 ScopedPyObjectPtr extension_field(
2105 PyFieldDescriptor_FromDescriptor(fields[i])); 2115 PyFieldDescriptor_FromDescriptor(fields[i]));
2106 if (extension_field == NULL) { 2116 if (extension_field == NULL) {
2107 return NULL; 2117 return NULL;
2108 } 2118 }
2109 // With C++ descriptors, the field can always be retrieved, but for 2119 // With C++ descriptors, the field can always be retrieved, but for
2110 // unknown extensions which have not been imported in Python code, there 2120 // unknown extensions which have not been imported in Python code, there
2111 // is no message class and we cannot retrieve the value. 2121 // is no message class and we cannot retrieve the value.
2112 // TODO(amauryfa): consider building the class on the fly! 2122 // TODO(amauryfa): consider building the class on the fly!
2113 if (fields[i]->message_type() != NULL && 2123 if (fields[i]->message_type() != NULL &&
2114 cdescriptor_pool::GetMessageClass( 2124 message_factory::GetMessageClass(
2115 GetDescriptorPoolForMessage(self), 2125 GetFactoryForMessage(self),
2116 fields[i]->message_type()) == NULL) { 2126 fields[i]->message_type()) == NULL) {
2117 PyErr_Clear(); 2127 PyErr_Clear();
2118 continue; 2128 continue;
2119 } 2129 }
2120 ScopedPyObjectPtr extensions(GetExtensionDict(self, NULL)); 2130 ScopedPyObjectPtr extensions(GetExtensionDict(self, NULL));
2121 if (extensions == NULL) { 2131 if (extensions == NULL) {
2122 return NULL; 2132 return NULL;
2123 } 2133 }
2124 // 'extension' reference later stolen by PyTuple_SET_ITEM. 2134 // 'extension' reference later stolen by PyTuple_SET_ITEM.
2125 PyObject* extension = PyObject_GetItem( 2135 PyObject* extension = PyObject_GetItem(
(...skipping 176 matching lines...) Expand 10 before | Expand all | Expand 10 after
2302 PyExc_SystemError, "Getting a value from a field of unknown type %d", 2312 PyExc_SystemError, "Getting a value from a field of unknown type %d",
2303 field_descriptor->cpp_type()); 2313 field_descriptor->cpp_type());
2304 } 2314 }
2305 2315
2306 return result; 2316 return result;
2307 } 2317 }
2308 2318
2309 PyObject* InternalGetSubMessage( 2319 PyObject* InternalGetSubMessage(
2310 CMessage* self, const FieldDescriptor* field_descriptor) { 2320 CMessage* self, const FieldDescriptor* field_descriptor) {
2311 const Reflection* reflection = self->message->GetReflection(); 2321 const Reflection* reflection = self->message->GetReflection();
2312 PyDescriptorPool* pool = GetDescriptorPoolForMessage(self); 2322 PyMessageFactory* factory = GetFactoryForMessage(self);
2313 const Message& sub_message = reflection->GetMessage( 2323 const Message& sub_message = reflection->GetMessage(
2314 *self->message, field_descriptor, pool->message_factory); 2324 *self->message, field_descriptor, factory->message_factory);
2315 2325
2316 CMessageClass* message_class = cdescriptor_pool::GetMessageClass( 2326 CMessageClass* message_class = message_factory::GetMessageClass(
2317 pool, field_descriptor->message_type()); 2327 factory, field_descriptor->message_type());
2318 if (message_class == NULL) { 2328 if (message_class == NULL) {
2319 return NULL; 2329 return NULL;
2320 } 2330 }
2321 2331
2322 CMessage* cmsg = cmessage::NewEmptyMessage(message_class); 2332 CMessage* cmsg = cmessage::NewEmptyMessage(message_class);
2323 if (cmsg == NULL) { 2333 if (cmsg == NULL) {
2324 return NULL; 2334 return NULL;
2325 } 2335 }
2326 2336
2327 cmsg->owner = self->owner; 2337 cmsg->owner = self->owner;
(...skipping 324 matching lines...) Expand 10 before | Expand all | Expand 10 after
2652 if (field_descriptor == NULL) { 2662 if (field_descriptor == NULL) {
2653 return CMessage_Type.tp_base->tp_getattro( 2663 return CMessage_Type.tp_base->tp_getattro(
2654 reinterpret_cast<PyObject*>(self), name); 2664 reinterpret_cast<PyObject*>(self), name);
2655 } 2665 }
2656 2666
2657 if (field_descriptor->is_map()) { 2667 if (field_descriptor->is_map()) {
2658 PyObject* py_container = NULL; 2668 PyObject* py_container = NULL;
2659 const Descriptor* entry_type = field_descriptor->message_type(); 2669 const Descriptor* entry_type = field_descriptor->message_type();
2660 const FieldDescriptor* value_type = entry_type->FindFieldByName("value"); 2670 const FieldDescriptor* value_type = entry_type->FindFieldByName("value");
2661 if (value_type->cpp_type() == FieldDescriptor::CPPTYPE_MESSAGE) { 2671 if (value_type->cpp_type() == FieldDescriptor::CPPTYPE_MESSAGE) {
2662 CMessageClass* value_class = cdescriptor_pool::GetMessageClass( 2672 CMessageClass* value_class = message_factory::GetMessageClass(
2663 GetDescriptorPoolForMessage(self), value_type->message_type()); 2673 GetFactoryForMessage(self), value_type->message_type());
2664 if (value_class == NULL) { 2674 if (value_class == NULL) {
2665 return NULL; 2675 return NULL;
2666 } 2676 }
2667 py_container = 2677 py_container =
2668 NewMessageMapContainer(self, field_descriptor, value_class); 2678 NewMessageMapContainer(self, field_descriptor, value_class);
2669 } else { 2679 } else {
2670 py_container = NewScalarMapContainer(self, field_descriptor); 2680 py_container = NewScalarMapContainer(self, field_descriptor);
2671 } 2681 }
2672 if (py_container == NULL) { 2682 if (py_container == NULL) {
2673 return NULL; 2683 return NULL;
2674 } 2684 }
2675 if (!SetCompositeField(self, name, py_container)) { 2685 if (!SetCompositeField(self, name, py_container)) {
2676 Py_DECREF(py_container); 2686 Py_DECREF(py_container);
2677 return NULL; 2687 return NULL;
2678 } 2688 }
2679 return py_container; 2689 return py_container;
2680 } 2690 }
2681 2691
2682 if (field_descriptor->label() == FieldDescriptor::LABEL_REPEATED) { 2692 if (field_descriptor->label() == FieldDescriptor::LABEL_REPEATED) {
2683 PyObject* py_container = NULL; 2693 PyObject* py_container = NULL;
2684 if (field_descriptor->cpp_type() == FieldDescriptor::CPPTYPE_MESSAGE) { 2694 if (field_descriptor->cpp_type() == FieldDescriptor::CPPTYPE_MESSAGE) {
2685 CMessageClass* message_class = cdescriptor_pool::GetMessageClass( 2695 CMessageClass* message_class = message_factory::GetMessageClass(
2686 GetDescriptorPoolForMessage(self), field_descriptor->message_type()); 2696 GetFactoryForMessage(self), field_descriptor->message_type());
2687 if (message_class == NULL) { 2697 if (message_class == NULL) {
2688 return NULL; 2698 return NULL;
2689 } 2699 }
2690 py_container = repeated_composite_container::NewContainer( 2700 py_container = repeated_composite_container::NewContainer(
2691 self, field_descriptor, message_class); 2701 self, field_descriptor, message_class);
2692 } else { 2702 } else {
2693 py_container = repeated_scalar_container::NewContainer( 2703 py_container = repeated_scalar_container::NewContainer(
2694 self, field_descriptor); 2704 self, field_descriptor);
2695 } 2705 }
2696 if (py_container == NULL) { 2706 if (py_container == NULL) {
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after
2771 0, // tp_call 2781 0, // tp_call
2772 (reprfunc)cmessage::ToStr, // tp_str 2782 (reprfunc)cmessage::ToStr, // tp_str
2773 (getattrofunc)cmessage::GetAttr, // tp_getattro 2783 (getattrofunc)cmessage::GetAttr, // tp_getattro
2774 (setattrofunc)cmessage::SetAttr, // tp_setattro 2784 (setattrofunc)cmessage::SetAttr, // tp_setattro
2775 0, // tp_as_buffer 2785 0, // tp_as_buffer
2776 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, // tp_flags 2786 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, // tp_flags
2777 "A ProtocolMessage", // tp_doc 2787 "A ProtocolMessage", // tp_doc
2778 0, // tp_traverse 2788 0, // tp_traverse
2779 0, // tp_clear 2789 0, // tp_clear
2780 (richcmpfunc)cmessage::RichCompare, // tp_richcompare 2790 (richcmpfunc)cmessage::RichCompare, // tp_richcompare
2781 0, // tp_weaklistoffset 2791 offsetof(CMessage, weakreflist), // tp_weaklistoffset
2782 0, // tp_iter 2792 0, // tp_iter
2783 0, // tp_iternext 2793 0, // tp_iternext
2784 cmessage::Methods, // tp_methods 2794 cmessage::Methods, // tp_methods
2785 0, // tp_members 2795 0, // tp_members
2786 cmessage::Getters, // tp_getset 2796 cmessage::Getters, // tp_getset
2787 0, // tp_base 2797 0, // tp_base
2788 0, // tp_dict 2798 0, // tp_dict
2789 0, // tp_descr_get 2799 0, // tp_descr_get
2790 0, // tp_descr_set 2800 0, // tp_descr_set
2791 0, // tp_dictoffset 2801 0, // tp_dictoffset
(...skipping 26 matching lines...) Expand all
2818 // There is currently no way of accurately syncing arbitrary changes to 2828 // There is currently no way of accurately syncing arbitrary changes to
2819 // the underlying C++ message back to the CMessage (e.g. removed repeated 2829 // the underlying C++ message back to the CMessage (e.g. removed repeated
2820 // composite containers). We only allow direct mutation of the underlying 2830 // composite containers). We only allow direct mutation of the underlying
2821 // C++ message if there is no child data in the CMessage. 2831 // C++ message if there is no child data in the CMessage.
2822 return NULL; 2832 return NULL;
2823 } 2833 }
2824 cmessage::AssureWritable(cmsg); 2834 cmessage::AssureWritable(cmsg);
2825 return cmsg->message; 2835 return cmsg->message;
2826 } 2836 }
2827 2837
2828 static const char module_docstring[] =
2829 "python-proto2 is a module that can be used to enhance proto2 Python API\n"
2830 "performance.\n"
2831 "\n"
2832 "It provides access to the protocol buffers C++ reflection API that\n"
2833 "implements the basic protocol buffer functions.";
2834
2835 void InitGlobals() { 2838 void InitGlobals() {
2836 // TODO(gps): Check all return values in this function for NULL and propagate 2839 // TODO(gps): Check all return values in this function for NULL and propagate
2837 // the error (MemoryError) on up to result in an import failure. These should 2840 // the error (MemoryError) on up to result in an import failure. These should
2838 // also be freed and reset to NULL during finalization. 2841 // also be freed and reset to NULL during finalization.
2839 kPythonZero = PyInt_FromLong(0); 2842 kPythonZero = PyInt_FromLong(0);
2840 kint32min_py = PyInt_FromLong(kint32min); 2843 kint32min_py = PyInt_FromLong(kint32min);
2841 kint32max_py = PyInt_FromLong(kint32max); 2844 kint32max_py = PyInt_FromLong(kint32max);
2842 kuint32max_py = PyLong_FromLongLong(kuint32max); 2845 kuint32max_py = PyLong_FromLongLong(kuint32max);
2843 kint64min_py = PyLong_FromLongLong(kint64min); 2846 kint64min_py = PyLong_FromLongLong(kint64min);
2844 kint64max_py = PyLong_FromLongLong(kint64max); 2847 kint64max_py = PyLong_FromLongLong(kint64max);
(...skipping 14 matching lines...) Expand all
2859 // Initialize types and globals in descriptor.cc 2862 // Initialize types and globals in descriptor.cc
2860 if (!InitDescriptor()) { 2863 if (!InitDescriptor()) {
2861 return false; 2864 return false;
2862 } 2865 }
2863 2866
2864 // Initialize types and globals in descriptor_pool.cc 2867 // Initialize types and globals in descriptor_pool.cc
2865 if (!InitDescriptorPool()) { 2868 if (!InitDescriptorPool()) {
2866 return false; 2869 return false;
2867 } 2870 }
2868 2871
2872 // Initialize types and globals in message_factory.cc
2873 if (!InitMessageFactory()) {
2874 return false;
2875 }
2876
2869 // Initialize constants defined in this file. 2877 // Initialize constants defined in this file.
2870 InitGlobals(); 2878 InitGlobals();
2871 2879
2872 CMessageClass_Type.tp_base = &PyType_Type; 2880 CMessageClass_Type.tp_base = &PyType_Type;
2873 if (PyType_Ready(&CMessageClass_Type) < 0) { 2881 if (PyType_Ready(&CMessageClass_Type) < 0) {
2874 return false; 2882 return false;
2875 } 2883 }
2876 PyModule_AddObject(m, "MessageMeta", 2884 PyModule_AddObject(m, "MessageMeta",
2877 reinterpret_cast<PyObject*>(&CMessageClass_Type)); 2885 reinterpret_cast<PyObject*>(&CMessageClass_Type));
2878 2886
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after
2940 return false; 2948 return false;
2941 } 2949 }
2942 if (ScopedPyObjectPtr( 2950 if (ScopedPyObjectPtr(
2943 PyObject_CallMethod(mutable_sequence.get(), "register", "O", 2951 PyObject_CallMethod(mutable_sequence.get(), "register", "O",
2944 &RepeatedCompositeContainer_Type)) == NULL) { 2952 &RepeatedCompositeContainer_Type)) == NULL) {
2945 return false; 2953 return false;
2946 } 2954 }
2947 } 2955 }
2948 2956
2949 // Initialize Map container types. 2957 // Initialize Map container types.
2950 { 2958 if (!InitMapContainers()) {
2951 // ScalarMapContainer_Type derives from our MutableMapping type. 2959 return false;
2952 ScopedPyObjectPtr containers(PyImport_ImportModule(
2953 "google.protobuf.internal.containers"));
2954 if (containers == NULL) {
2955 return false;
2956 }
2957
2958 ScopedPyObjectPtr mutable_mapping(
2959 PyObject_GetAttrString(containers.get(), "MutableMapping"));
2960 if (mutable_mapping == NULL) {
2961 return false;
2962 }
2963
2964 if (!PyObject_TypeCheck(mutable_mapping.get(), &PyType_Type)) {
2965 return false;
2966 }
2967
2968 Py_INCREF(mutable_mapping.get());
2969 #if PY_MAJOR_VERSION >= 3
2970 PyObject* bases = PyTuple_New(1);
2971 PyTuple_SET_ITEM(bases, 0, mutable_mapping.get());
2972
2973 ScalarMapContainer_Type =
2974 PyType_FromSpecWithBases(&ScalarMapContainer_Type_spec, bases);
2975 PyModule_AddObject(m, "ScalarMapContainer", ScalarMapContainer_Type);
2976 #else
2977 ScalarMapContainer_Type.tp_base =
2978 reinterpret_cast<PyTypeObject*>(mutable_mapping.get());
2979
2980 if (PyType_Ready(&ScalarMapContainer_Type) < 0) {
2981 return false;
2982 }
2983
2984 PyModule_AddObject(m, "ScalarMapContainer",
2985 reinterpret_cast<PyObject*>(&ScalarMapContainer_Type));
2986 #endif
2987
2988 if (PyType_Ready(&MapIterator_Type) < 0) {
2989 return false;
2990 }
2991
2992 PyModule_AddObject(m, "MapIterator",
2993 reinterpret_cast<PyObject*>(&MapIterator_Type));
2994
2995
2996 #if PY_MAJOR_VERSION >= 3
2997 MessageMapContainer_Type =
2998 PyType_FromSpecWithBases(&MessageMapContainer_Type_spec, bases);
2999 PyModule_AddObject(m, "MessageMapContainer", MessageMapContainer_Type);
3000 #else
3001 Py_INCREF(mutable_mapping.get());
3002 MessageMapContainer_Type.tp_base =
3003 reinterpret_cast<PyTypeObject*>(mutable_mapping.get());
3004
3005 if (PyType_Ready(&MessageMapContainer_Type) < 0) {
3006 return false;
3007 }
3008
3009 PyModule_AddObject(m, "MessageMapContainer",
3010 reinterpret_cast<PyObject*>(&MessageMapContainer_Type));
3011 #endif
3012 } 2960 }
2961 PyModule_AddObject(m, "ScalarMapContainer",
2962 reinterpret_cast<PyObject*>(ScalarMapContainer_Type));
2963 PyModule_AddObject(m, "MessageMapContainer",
2964 reinterpret_cast<PyObject*>(MessageMapContainer_Type));
2965 PyModule_AddObject(m, "MapIterator",
2966 reinterpret_cast<PyObject*>(&MapIterator_Type));
3013 2967
3014 if (PyType_Ready(&ExtensionDict_Type) < 0) { 2968 if (PyType_Ready(&ExtensionDict_Type) < 0) {
3015 return false; 2969 return false;
3016 } 2970 }
3017 PyModule_AddObject( 2971 PyModule_AddObject(
3018 m, "ExtensionDict", 2972 m, "ExtensionDict",
3019 reinterpret_cast<PyObject*>(&ExtensionDict_Type)); 2973 reinterpret_cast<PyObject*>(&ExtensionDict_Type));
3020 2974
3021 // Expose the DescriptorPool used to hold all descriptors added from generated 2975 // Expose the DescriptorPool used to hold all descriptors added from generated
3022 // pb2.py files. 2976 // pb2.py files.
(...skipping 14 matching lines...) Expand all
3037 PyModule_AddObject(m, "FieldDescriptor", reinterpret_cast<PyObject*>( 2991 PyModule_AddObject(m, "FieldDescriptor", reinterpret_cast<PyObject*>(
3038 &PyFieldDescriptor_Type)); 2992 &PyFieldDescriptor_Type));
3039 PyModule_AddObject(m, "EnumDescriptor", reinterpret_cast<PyObject*>( 2993 PyModule_AddObject(m, "EnumDescriptor", reinterpret_cast<PyObject*>(
3040 &PyEnumDescriptor_Type)); 2994 &PyEnumDescriptor_Type));
3041 PyModule_AddObject(m, "EnumValueDescriptor", reinterpret_cast<PyObject*>( 2995 PyModule_AddObject(m, "EnumValueDescriptor", reinterpret_cast<PyObject*>(
3042 &PyEnumValueDescriptor_Type)); 2996 &PyEnumValueDescriptor_Type));
3043 PyModule_AddObject(m, "FileDescriptor", reinterpret_cast<PyObject*>( 2997 PyModule_AddObject(m, "FileDescriptor", reinterpret_cast<PyObject*>(
3044 &PyFileDescriptor_Type)); 2998 &PyFileDescriptor_Type));
3045 PyModule_AddObject(m, "OneofDescriptor", reinterpret_cast<PyObject*>( 2999 PyModule_AddObject(m, "OneofDescriptor", reinterpret_cast<PyObject*>(
3046 &PyOneofDescriptor_Type)); 3000 &PyOneofDescriptor_Type));
3001 PyModule_AddObject(m, "ServiceDescriptor", reinterpret_cast<PyObject*>(
3002 &PyServiceDescriptor_Type));
3003 PyModule_AddObject(m, "MethodDescriptor", reinterpret_cast<PyObject*>(
3004 &PyMethodDescriptor_Type));
3047 3005
3048 PyObject* enum_type_wrapper = PyImport_ImportModule( 3006 PyObject* enum_type_wrapper = PyImport_ImportModule(
3049 "google.protobuf.internal.enum_type_wrapper"); 3007 "google.protobuf.internal.enum_type_wrapper");
3050 if (enum_type_wrapper == NULL) { 3008 if (enum_type_wrapper == NULL) {
3051 return false; 3009 return false;
3052 } 3010 }
3053 EnumTypeWrapper_class = 3011 EnumTypeWrapper_class =
3054 PyObject_GetAttrString(enum_type_wrapper, "EnumTypeWrapper"); 3012 PyObject_GetAttrString(enum_type_wrapper, "EnumTypeWrapper");
3055 Py_DECREF(enum_type_wrapper); 3013 Py_DECREF(enum_type_wrapper);
3056 3014
(...skipping 17 matching lines...) Expand all
3074 // Override {Get,Mutable}CProtoInsidePyProto. 3032 // Override {Get,Mutable}CProtoInsidePyProto.
3075 GetCProtoInsidePyProtoPtr = GetCProtoInsidePyProtoImpl; 3033 GetCProtoInsidePyProtoPtr = GetCProtoInsidePyProtoImpl;
3076 MutableCProtoInsidePyProtoPtr = MutableCProtoInsidePyProtoImpl; 3034 MutableCProtoInsidePyProtoPtr = MutableCProtoInsidePyProtoImpl;
3077 3035
3078 return true; 3036 return true;
3079 } 3037 }
3080 3038
3081 } // namespace python 3039 } // namespace python
3082 } // namespace protobuf 3040 } // namespace protobuf
3083 3041
3084 static PyMethodDef ModuleMethods[] = {
3085 {"SetAllowOversizeProtos",
3086 (PyCFunction)google::protobuf::python::cmessage::SetAllowOversizeProtos,
3087 METH_O, "Enable/disable oversize proto parsing."},
3088 { NULL, NULL}
3089 };
3090
3091 #if PY_MAJOR_VERSION >= 3
3092 static struct PyModuleDef _module = {
3093 PyModuleDef_HEAD_INIT,
3094 "_message",
3095 google::protobuf::python::module_docstring,
3096 -1,
3097 ModuleMethods, /* m_methods */
3098 NULL,
3099 NULL,
3100 NULL,
3101 NULL
3102 };
3103 #define INITFUNC PyInit__message
3104 #define INITFUNC_ERRORVAL NULL
3105 #else // Python 2
3106 #define INITFUNC init_message
3107 #define INITFUNC_ERRORVAL
3108 #endif
3109
3110 extern "C" {
3111 PyMODINIT_FUNC INITFUNC(void) {
3112 PyObject* m;
3113 #if PY_MAJOR_VERSION >= 3
3114 m = PyModule_Create(&_module);
3115 #else
3116 m = Py_InitModule3("_message", ModuleMethods,
3117 google::protobuf::python::module_docstring);
3118 #endif
3119 if (m == NULL) {
3120 return INITFUNC_ERRORVAL;
3121 }
3122
3123 if (!google::protobuf::python::InitProto2MessageModule(m)) {
3124 Py_DECREF(m);
3125 return INITFUNC_ERRORVAL;
3126 }
3127
3128 #if PY_MAJOR_VERSION >= 3
3129 return m;
3130 #endif
3131 }
3132 }
3133 } // namespace google 3042 } // namespace google
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698