| OLD | NEW |
| 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 Loading... |
| 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> | |
| 67 #include <google/protobuf/pyext/safe_numerics.h> | |
| 68 #include <google/protobuf/pyext/scoped_pyobject_ptr.h> | 66 #include <google/protobuf/pyext/scoped_pyobject_ptr.h> |
| 69 #include <google/protobuf/stubs/strutil.h> | 67 #include <google/protobuf/stubs/strutil.h> |
| 70 | 68 |
| 71 #if PY_MAJOR_VERSION >= 3 | 69 #if PY_MAJOR_VERSION >= 3 |
| 70 #define PyInt_Check PyLong_Check |
| 72 #define PyInt_AsLong PyLong_AsLong | 71 #define PyInt_AsLong PyLong_AsLong |
| 73 #define PyInt_FromLong PyLong_FromLong | 72 #define PyInt_FromLong PyLong_FromLong |
| 74 #define PyInt_FromSize_t PyLong_FromSize_t | 73 #define PyInt_FromSize_t PyLong_FromSize_t |
| 75 #define PyString_Check PyUnicode_Check | 74 #define PyString_Check PyUnicode_Check |
| 76 #define PyString_FromString PyUnicode_FromString | 75 #define PyString_FromString PyUnicode_FromString |
| 77 #define PyString_FromStringAndSize PyUnicode_FromStringAndSize | 76 #define PyString_FromStringAndSize PyUnicode_FromStringAndSize |
| 78 #if PY_VERSION_HEX < 0x03030000 | 77 #if PY_VERSION_HEX < 0x03030000 |
| 79 #error "Python 3.0 - 3.2 are not supported." | 78 #error "Python 3.0 - 3.2 are not supported." |
| 80 #else | 79 #else |
| 81 #define PyString_AsString(ob) \ | 80 #define PyString_AsString(ob) \ |
| 82 (PyUnicode_Check(ob)? PyUnicode_AsUTF8(ob): PyBytes_AsString(ob)) | 81 (PyUnicode_Check(ob)? PyUnicode_AsUTF8(ob): PyBytes_AsString(ob)) |
| 83 #define PyString_AsStringAndSize(ob, charpp, sizep) \ | 82 #define PyString_AsStringAndSize(ob, charpp, sizep) \ |
| 84 (PyUnicode_Check(ob)? \ | 83 (PyUnicode_Check(ob)? \ |
| 85 ((*(charpp) = PyUnicode_AsUTF8AndSize(ob, (sizep))) == NULL? -1: 0): \ | 84 ((*(charpp) = PyUnicode_AsUTF8AndSize(ob, (sizep))) == NULL? -1: 0): \ |
| 86 PyBytes_AsStringAndSize(ob, (charpp), (sizep))) | 85 PyBytes_AsStringAndSize(ob, (charpp), (sizep))) |
| 87 #endif | 86 #endif |
| 88 #endif | 87 #endif |
| 89 | 88 |
| 90 namespace google { | 89 namespace google { |
| 91 namespace protobuf { | 90 namespace protobuf { |
| 92 namespace python { | 91 namespace python { |
| 93 | 92 |
| 94 static PyObject* kDESCRIPTOR; | 93 static PyObject* kDESCRIPTOR; |
| 94 static PyObject* k_extensions_by_name; |
| 95 static PyObject* k_extensions_by_number; |
| 95 PyObject* EnumTypeWrapper_class; | 96 PyObject* EnumTypeWrapper_class; |
| 96 static PyObject* PythonMessage_class; | 97 static PyObject* PythonMessage_class; |
| 97 static PyObject* kEmptyWeakref; | 98 static PyObject* kEmptyWeakref; |
| 98 static PyObject* WKT_classes = NULL; | 99 static PyObject* WKT_classes = NULL; |
| 99 | 100 |
| 100 namespace message_meta { | 101 namespace message_meta { |
| 101 | 102 |
| 102 static int InsertEmptyWeakref(PyTypeObject* base); | 103 static int InsertEmptyWeakref(PyTypeObject* base); |
| 103 | 104 |
| 104 // Add the number of a field descriptor to the containing message class. | 105 // Add the number of a field descriptor to the containing message class. |
| (...skipping 14 matching lines...) Expand all Loading... |
| 119 } | 120 } |
| 120 if (PyObject_SetAttr(cls, attr_name.get(), number.get()) == -1) { | 121 if (PyObject_SetAttr(cls, attr_name.get(), number.get()) == -1) { |
| 121 return false; | 122 return false; |
| 122 } | 123 } |
| 123 return true; | 124 return true; |
| 124 } | 125 } |
| 125 | 126 |
| 126 | 127 |
| 127 // Finalize the creation of the Message class. | 128 // Finalize the creation of the Message class. |
| 128 static int AddDescriptors(PyObject* cls, const Descriptor* descriptor) { | 129 static int AddDescriptors(PyObject* cls, const Descriptor* descriptor) { |
| 130 // If there are extension_ranges, the message is "extendable", and extension |
| 131 // classes will register themselves in this class. |
| 132 if (descriptor->extension_range_count() > 0) { |
| 133 ScopedPyObjectPtr by_name(PyDict_New()); |
| 134 if (PyObject_SetAttr(cls, k_extensions_by_name, by_name.get()) < 0) { |
| 135 return -1; |
| 136 } |
| 137 ScopedPyObjectPtr by_number(PyDict_New()); |
| 138 if (PyObject_SetAttr(cls, k_extensions_by_number, by_number.get()) < 0) { |
| 139 return -1; |
| 140 } |
| 141 } |
| 142 |
| 129 // For each field set: cls.<field>_FIELD_NUMBER = <number> | 143 // For each field set: cls.<field>_FIELD_NUMBER = <number> |
| 130 for (int i = 0; i < descriptor->field_count(); ++i) { | 144 for (int i = 0; i < descriptor->field_count(); ++i) { |
| 131 if (!AddFieldNumberToClass(cls, descriptor->field(i))) { | 145 if (!AddFieldNumberToClass(cls, descriptor->field(i))) { |
| 132 return -1; | 146 return -1; |
| 133 } | 147 } |
| 134 } | 148 } |
| 135 | 149 |
| 136 // For each enum set cls.<enum name> = EnumTypeWrapper(<enum descriptor>). | 150 // For each enum set cls.<enum name> = EnumTypeWrapper(<enum descriptor>). |
| 137 for (int i = 0; i < descriptor->enum_type_count(); ++i) { | 151 for (int i = 0; i < descriptor->enum_type_count(); ++i) { |
| 138 const EnumDescriptor* enum_descriptor = descriptor->enum_type(i); | 152 const EnumDescriptor* enum_descriptor = descriptor->enum_type(i); |
| (...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 223 if (py_descriptor == NULL) { | 237 if (py_descriptor == NULL) { |
| 224 PyErr_SetString(PyExc_TypeError, "Message class has no DESCRIPTOR"); | 238 PyErr_SetString(PyExc_TypeError, "Message class has no DESCRIPTOR"); |
| 225 return NULL; | 239 return NULL; |
| 226 } | 240 } |
| 227 if (!PyObject_TypeCheck(py_descriptor, &PyMessageDescriptor_Type)) { | 241 if (!PyObject_TypeCheck(py_descriptor, &PyMessageDescriptor_Type)) { |
| 228 PyErr_Format(PyExc_TypeError, "Expected a message Descriptor, got %s", | 242 PyErr_Format(PyExc_TypeError, "Expected a message Descriptor, got %s", |
| 229 py_descriptor->ob_type->tp_name); | 243 py_descriptor->ob_type->tp_name); |
| 230 return NULL; | 244 return NULL; |
| 231 } | 245 } |
| 232 | 246 |
| 233 // Messages have no __dict__ | |
| 234 ScopedPyObjectPtr slots(PyTuple_New(0)); | |
| 235 if (PyDict_SetItemString(dict, "__slots__", slots.get()) < 0) { | |
| 236 return NULL; | |
| 237 } | |
| 238 | |
| 239 // Build the arguments to the base metaclass. | 247 // Build the arguments to the base metaclass. |
| 240 // We change the __bases__ classes. | 248 // We change the __bases__ classes. |
| 241 ScopedPyObjectPtr new_args; | 249 ScopedPyObjectPtr new_args; |
| 242 const Descriptor* message_descriptor = | 250 const Descriptor* message_descriptor = |
| 243 PyMessageDescriptor_AsDescriptor(py_descriptor); | 251 PyMessageDescriptor_AsDescriptor(py_descriptor); |
| 244 if (message_descriptor == NULL) { | 252 if (message_descriptor == NULL) { |
| 245 return NULL; | 253 return NULL; |
| 246 } | 254 } |
| 247 | 255 |
| 248 if (WKT_classes == NULL) { | 256 if (WKT_classes == NULL) { |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 285 const Descriptor* descriptor = | 293 const Descriptor* descriptor = |
| 286 PyMessageDescriptor_AsDescriptor(py_descriptor); | 294 PyMessageDescriptor_AsDescriptor(py_descriptor); |
| 287 if (descriptor == NULL) { | 295 if (descriptor == NULL) { |
| 288 return NULL; | 296 return NULL; |
| 289 } | 297 } |
| 290 Py_INCREF(py_descriptor); | 298 Py_INCREF(py_descriptor); |
| 291 newtype->py_message_descriptor = py_descriptor; | 299 newtype->py_message_descriptor = py_descriptor; |
| 292 newtype->message_descriptor = descriptor; | 300 newtype->message_descriptor = descriptor; |
| 293 // TODO(amauryfa): Don't always use the canonical pool of the descriptor, | 301 // TODO(amauryfa): Don't always use the canonical pool of the descriptor, |
| 294 // use the MessageFactory optionally passed in the class dict. | 302 // use the MessageFactory optionally passed in the class dict. |
| 295 PyDescriptorPool* py_descriptor_pool = | 303 newtype->py_descriptor_pool = GetDescriptorPool_FromPool( |
| 296 GetDescriptorPool_FromPool(descriptor->file()->pool()); | 304 descriptor->file()->pool()); |
| 297 if (py_descriptor_pool == NULL) { | 305 if (newtype->py_descriptor_pool == NULL) { |
| 298 return NULL; | 306 return NULL; |
| 299 } | 307 } |
| 300 newtype->py_message_factory = py_descriptor_pool->py_message_factory; | 308 Py_INCREF(newtype->py_descriptor_pool); |
| 301 Py_INCREF(newtype->py_message_factory); | |
| 302 | 309 |
| 303 // Register the message in the MessageFactory. | 310 // Add the message to the DescriptorPool. |
| 304 // TODO(amauryfa): Move this call to MessageFactory.GetPrototype() when the | 311 if (cdescriptor_pool::RegisterMessageClass(newtype->py_descriptor_pool, |
| 305 // MessageFactory is fully implemented in C++. | 312 descriptor, newtype) < 0) { |
| 306 if (message_factory::RegisterMessageClass(newtype->py_message_factory, | |
| 307 descriptor, newtype) < 0) { | |
| 308 return NULL; | 313 return NULL; |
| 309 } | 314 } |
| 310 | 315 |
| 311 // Continue with type initialization: add other descriptors, enum values... | 316 // Continue with type initialization: add other descriptors, enum values... |
| 312 if (AddDescriptors(result.get(), descriptor) < 0) { | 317 if (AddDescriptors(result.get(), descriptor) < 0) { |
| 313 return NULL; | 318 return NULL; |
| 314 } | 319 } |
| 315 return result.release(); | 320 return result.release(); |
| 316 } | 321 } |
| 317 | 322 |
| 318 static void Dealloc(CMessageClass *self) { | 323 static void Dealloc(CMessageClass *self) { |
| 319 Py_XDECREF(self->py_message_descriptor); | 324 Py_DECREF(self->py_message_descriptor); |
| 320 Py_XDECREF(self->py_message_factory); | 325 Py_DECREF(self->py_descriptor_pool); |
| 321 Py_TYPE(self)->tp_free(reinterpret_cast<PyObject*>(self)); | 326 Py_TYPE(self)->tp_free(reinterpret_cast<PyObject*>(self)); |
| 322 } | 327 } |
| 323 | 328 |
| 324 | 329 |
| 325 // This function inserts and empty weakref at the end of the list of | 330 // This function inserts and empty weakref at the end of the list of |
| 326 // subclasses for the main protocol buffer Message class. | 331 // subclasses for the main protocol buffer Message class. |
| 327 // | 332 // |
| 328 // This eliminates a O(n^2) behaviour in the internal add_subclass | 333 // This eliminates a O(n^2) behaviour in the internal add_subclass |
| 329 // routine. | 334 // routine. |
| 330 static int InsertEmptyWeakref(PyTypeObject *base_type) { | 335 static int InsertEmptyWeakref(PyTypeObject *base_type) { |
| 331 #if PY_MAJOR_VERSION >= 3 | 336 #if PY_MAJOR_VERSION >= 3 |
| 332 // Python 3.4 has already included the fix for the issue that this | 337 // Python 3.4 has already included the fix for the issue that this |
| 333 // hack addresses. For further background and the fix please see | 338 // hack addresses. For further background and the fix please see |
| 334 // https://bugs.python.org/issue17936. | 339 // https://bugs.python.org/issue17936. |
| 335 return 0; | 340 return 0; |
| 336 #else | 341 #else |
| 337 PyObject *subclasses = base_type->tp_subclasses; | 342 PyObject *subclasses = base_type->tp_subclasses; |
| 338 if (subclasses && PyList_CheckExact(subclasses)) { | 343 if (subclasses && PyList_CheckExact(subclasses)) { |
| 339 return PyList_Append(subclasses, kEmptyWeakref); | 344 return PyList_Append(subclasses, kEmptyWeakref); |
| 340 } | 345 } |
| 341 return 0; | 346 return 0; |
| 342 #endif // PY_MAJOR_VERSION >= 3 | 347 #endif // PY_MAJOR_VERSION >= 3 |
| 343 } | 348 } |
| 344 | 349 |
| 345 // The _extensions_by_name dictionary is built on every access. | |
| 346 // TODO(amauryfa): Migrate all users to pool.FindAllExtensions() | |
| 347 static PyObject* GetExtensionsByName(CMessageClass *self, void *closure) { | |
| 348 const PyDescriptorPool* pool = self->py_message_factory->pool; | |
| 349 | |
| 350 std::vector<const FieldDescriptor*> extensions; | |
| 351 pool->pool->FindAllExtensions(self->message_descriptor, &extensions); | |
| 352 | |
| 353 ScopedPyObjectPtr result(PyDict_New()); | |
| 354 for (int i = 0; i < extensions.size(); i++) { | |
| 355 ScopedPyObjectPtr extension( | |
| 356 PyFieldDescriptor_FromDescriptor(extensions[i])); | |
| 357 if (extension == NULL) { | |
| 358 return NULL; | |
| 359 } | |
| 360 if (PyDict_SetItemString(result.get(), extensions[i]->full_name().c_str(), | |
| 361 extension.get()) < 0) { | |
| 362 return NULL; | |
| 363 } | |
| 364 } | |
| 365 return result.release(); | |
| 366 } | |
| 367 | |
| 368 // The _extensions_by_number dictionary is built on every access. | |
| 369 // TODO(amauryfa): Migrate all users to pool.FindExtensionByNumber() | |
| 370 static PyObject* GetExtensionsByNumber(CMessageClass *self, void *closure) { | |
| 371 const PyDescriptorPool* pool = self->py_message_factory->pool; | |
| 372 | |
| 373 std::vector<const FieldDescriptor*> extensions; | |
| 374 pool->pool->FindAllExtensions(self->message_descriptor, &extensions); | |
| 375 | |
| 376 ScopedPyObjectPtr result(PyDict_New()); | |
| 377 for (int i = 0; i < extensions.size(); i++) { | |
| 378 ScopedPyObjectPtr extension( | |
| 379 PyFieldDescriptor_FromDescriptor(extensions[i])); | |
| 380 if (extension == NULL) { | |
| 381 return NULL; | |
| 382 } | |
| 383 ScopedPyObjectPtr number(PyInt_FromLong(extensions[i]->number())); | |
| 384 if (number == NULL) { | |
| 385 return NULL; | |
| 386 } | |
| 387 if (PyDict_SetItem(result.get(), number.get(), extension.get()) < 0) { | |
| 388 return NULL; | |
| 389 } | |
| 390 } | |
| 391 return result.release(); | |
| 392 } | |
| 393 | |
| 394 static PyGetSetDef Getters[] = { | |
| 395 {"_extensions_by_name", (getter)GetExtensionsByName, NULL}, | |
| 396 {"_extensions_by_number", (getter)GetExtensionsByNumber, NULL}, | |
| 397 {NULL} | |
| 398 }; | |
| 399 | |
| 400 } // namespace message_meta | 350 } // namespace message_meta |
| 401 | 351 |
| 402 PyTypeObject CMessageClass_Type = { | 352 PyTypeObject CMessageClass_Type = { |
| 403 PyVarObject_HEAD_INIT(&PyType_Type, 0) | 353 PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 404 FULL_MODULE_NAME ".MessageMeta", // tp_name | 354 FULL_MODULE_NAME ".MessageMeta", // tp_name |
| 405 sizeof(CMessageClass), // tp_basicsize | 355 sizeof(CMessageClass), // tp_basicsize |
| 406 0, // tp_itemsize | 356 0, // tp_itemsize |
| 407 (destructor)message_meta::Dealloc, // tp_dealloc | 357 (destructor)message_meta::Dealloc, // tp_dealloc |
| 408 0, // tp_print | 358 0, // tp_print |
| 409 0, // tp_getattr | 359 0, // tp_getattr |
| (...skipping 12 matching lines...) Expand all Loading... |
| 422 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, // tp_flags | 372 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, // tp_flags |
| 423 "The metaclass of ProtocolMessages", // tp_doc | 373 "The metaclass of ProtocolMessages", // tp_doc |
| 424 0, // tp_traverse | 374 0, // tp_traverse |
| 425 0, // tp_clear | 375 0, // tp_clear |
| 426 0, // tp_richcompare | 376 0, // tp_richcompare |
| 427 0, // tp_weaklistoffset | 377 0, // tp_weaklistoffset |
| 428 0, // tp_iter | 378 0, // tp_iter |
| 429 0, // tp_iternext | 379 0, // tp_iternext |
| 430 0, // tp_methods | 380 0, // tp_methods |
| 431 0, // tp_members | 381 0, // tp_members |
| 432 message_meta::Getters, // tp_getset | 382 0, // tp_getset |
| 433 0, // tp_base | 383 0, // tp_base |
| 434 0, // tp_dict | 384 0, // tp_dict |
| 435 0, // tp_descr_get | 385 0, // tp_descr_get |
| 436 0, // tp_descr_set | 386 0, // tp_descr_set |
| 437 0, // tp_dictoffset | 387 0, // tp_dictoffset |
| 438 0, // tp_init | 388 0, // tp_init |
| 439 0, // tp_alloc | 389 0, // tp_alloc |
| 440 message_meta::New, // tp_new | 390 message_meta::New, // tp_new |
| 441 }; | 391 }; |
| 442 | 392 |
| (...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 558 if (VisitCompositeField(descriptor, field, visitor) == -1) | 508 if (VisitCompositeField(descriptor, field, visitor) == -1) |
| 559 return -1; | 509 return -1; |
| 560 } | 510 } |
| 561 } | 511 } |
| 562 | 512 |
| 563 return 0; | 513 return 0; |
| 564 } | 514 } |
| 565 | 515 |
| 566 // --------------------------------------------------------------------- | 516 // --------------------------------------------------------------------- |
| 567 | 517 |
| 518 // Constants used for integer type range checking. |
| 519 PyObject* kPythonZero; |
| 520 PyObject* kint32min_py; |
| 521 PyObject* kint32max_py; |
| 522 PyObject* kuint32max_py; |
| 523 PyObject* kint64min_py; |
| 524 PyObject* kint64max_py; |
| 525 PyObject* kuint64max_py; |
| 526 |
| 568 PyObject* EncodeError_class; | 527 PyObject* EncodeError_class; |
| 569 PyObject* DecodeError_class; | 528 PyObject* DecodeError_class; |
| 570 PyObject* PickleError_class; | 529 PyObject* PickleError_class; |
| 571 | 530 |
| 531 // Constant PyString values used for GetAttr/GetItem. |
| 532 static PyObject* k_cdescriptor; |
| 533 static PyObject* kfull_name; |
| 534 |
| 572 /* Is 64bit */ | 535 /* Is 64bit */ |
| 573 void FormatTypeError(PyObject* arg, char* expected_types) { | 536 void FormatTypeError(PyObject* arg, char* expected_types) { |
| 574 PyObject* repr = PyObject_Repr(arg); | 537 PyObject* repr = PyObject_Repr(arg); |
| 575 if (repr) { | 538 if (repr) { |
| 576 PyErr_Format(PyExc_TypeError, | 539 PyErr_Format(PyExc_TypeError, |
| 577 "%.100s has type %.100s, but expected one of: %s", | 540 "%.100s has type %.100s, but expected one of: %s", |
| 578 PyString_AsString(repr), | 541 PyString_AsString(repr), |
| 579 Py_TYPE(arg)->tp_name, | 542 Py_TYPE(arg)->tp_name, |
| 580 expected_types); | 543 expected_types); |
| 581 Py_DECREF(repr); | 544 Py_DECREF(repr); |
| 582 } | 545 } |
| 583 } | 546 } |
| 584 | 547 |
| 585 void OutOfRangeError(PyObject* arg) { | |
| 586 PyObject *s = PyObject_Str(arg); | |
| 587 if (s) { | |
| 588 PyErr_Format(PyExc_ValueError, | |
| 589 "Value out of range: %s", | |
| 590 PyString_AsString(s)); | |
| 591 Py_DECREF(s); | |
| 592 } | |
| 593 } | |
| 594 | |
| 595 template<class RangeType, class ValueType> | |
| 596 bool VerifyIntegerCastAndRange(PyObject* arg, ValueType value) { | |
| 597 if GOOGLE_PREDICT_FALSE(value == -1 && PyErr_Occurred()) { | |
| 598 if (PyErr_ExceptionMatches(PyExc_OverflowError)) { | |
| 599 // Replace it with the same ValueError as pure python protos instead of | |
| 600 // the default one. | |
| 601 PyErr_Clear(); | |
| 602 OutOfRangeError(arg); | |
| 603 } // Otherwise propagate existing error. | |
| 604 return false; | |
| 605 } | |
| 606 if GOOGLE_PREDICT_FALSE(!IsValidNumericCast<RangeType>(value)) { | |
| 607 OutOfRangeError(arg); | |
| 608 return false; | |
| 609 } | |
| 610 return true; | |
| 611 } | |
| 612 | |
| 613 template<class T> | 548 template<class T> |
| 614 bool CheckAndGetInteger(PyObject* arg, T* value) { | 549 bool CheckAndGetInteger( |
| 615 // The fast path. | 550 PyObject* arg, T* value, PyObject* min, PyObject* max) { |
| 551 bool is_long = PyLong_Check(arg); |
| 616 #if PY_MAJOR_VERSION < 3 | 552 #if PY_MAJOR_VERSION < 3 |
| 617 // For the typical case, offer a fast path. | 553 if (!PyInt_Check(arg) && !is_long) { |
| 618 if GOOGLE_PREDICT_TRUE(PyInt_Check(arg)) { | |
| 619 long int_result = PyInt_AsLong(arg); | |
| 620 if GOOGLE_PREDICT_TRUE(IsValidNumericCast<T>(int_result)) { | |
| 621 *value = static_cast<T>(int_result); | |
| 622 return true; | |
| 623 } else { | |
| 624 OutOfRangeError(arg); | |
| 625 return false; | |
| 626 } | |
| 627 } | |
| 628 #endif | |
| 629 // This effectively defines an integer as "an object that can be cast as | |
| 630 // an integer and can be used as an ordinal number". | |
| 631 // This definition includes everything that implements numbers.Integral | |
| 632 // and shouldn't cast the net too wide. | |
| 633 if GOOGLE_PREDICT_FALSE(!PyIndex_Check(arg)) { | |
| 634 FormatTypeError(arg, "int, long"); | 554 FormatTypeError(arg, "int, long"); |
| 635 return false; | 555 return false; |
| 636 } | 556 } |
| 637 | 557 if (PyObject_Compare(min, arg) > 0 || PyObject_Compare(max, arg) < 0) { |
| 638 // Now we have an integral number so we can safely use PyLong_ functions. | 558 #else |
| 639 // We need to treat the signed and unsigned cases differently in case arg is | 559 if (!is_long) { |
| 640 // holding a value above the maximum for signed longs. | 560 FormatTypeError(arg, "int"); |
| 641 if (std::numeric_limits<T>::min() == 0) { | 561 return false; |
| 642 // Unsigned case. | 562 } |
| 643 unsigned PY_LONG_LONG ulong_result; | 563 if (PyObject_RichCompareBool(min, arg, Py_LE) != 1 || |
| 644 if (PyLong_Check(arg)) { | 564 PyObject_RichCompareBool(max, arg, Py_GE) != 1) { |
| 645 ulong_result = PyLong_AsUnsignedLongLong(arg); | 565 #endif |
| 566 if (!PyErr_Occurred()) { |
| 567 PyObject *s = PyObject_Str(arg); |
| 568 if (s) { |
| 569 PyErr_Format(PyExc_ValueError, |
| 570 "Value out of range: %s", |
| 571 PyString_AsString(s)); |
| 572 Py_DECREF(s); |
| 573 } |
| 574 } |
| 575 return false; |
| 576 } |
| 577 #if PY_MAJOR_VERSION < 3 |
| 578 if (!is_long) { |
| 579 *value = static_cast<T>(PyInt_AsLong(arg)); |
| 580 } else // NOLINT |
| 581 #endif |
| 582 { |
| 583 if (min == kPythonZero) { |
| 584 *value = static_cast<T>(PyLong_AsUnsignedLongLong(arg)); |
| 646 } else { | 585 } else { |
| 647 // Unlike PyLong_AsLongLong, PyLong_AsUnsignedLongLong is very | 586 *value = static_cast<T>(PyLong_AsLongLong(arg)); |
| 648 // picky about the exact type. | |
| 649 PyObject* casted = PyNumber_Long(arg); | |
| 650 if GOOGLE_PREDICT_FALSE(casted == NULL) { | |
| 651 // Propagate existing error. | |
| 652 return false; | |
| 653 } | |
| 654 ulong_result = PyLong_AsUnsignedLongLong(casted); | |
| 655 Py_DECREF(casted); | |
| 656 } | |
| 657 if (VerifyIntegerCastAndRange<T, unsigned PY_LONG_LONG>(arg, | |
| 658 ulong_result)) { | |
| 659 *value = static_cast<T>(ulong_result); | |
| 660 } else { | |
| 661 return false; | |
| 662 } | |
| 663 } else { | |
| 664 // Signed case. | |
| 665 PY_LONG_LONG long_result; | |
| 666 PyNumberMethods *nb; | |
| 667 if ((nb = arg->ob_type->tp_as_number) != NULL && nb->nb_int != NULL) { | |
| 668 // PyLong_AsLongLong requires it to be a long or to have an __int__() | |
| 669 // method. | |
| 670 long_result = PyLong_AsLongLong(arg); | |
| 671 } else { | |
| 672 // Valid subclasses of numbers.Integral should have a __long__() method | |
| 673 // so fall back to that. | |
| 674 PyObject* casted = PyNumber_Long(arg); | |
| 675 if GOOGLE_PREDICT_FALSE(casted == NULL) { | |
| 676 // Propagate existing error. | |
| 677 return false; | |
| 678 } | |
| 679 long_result = PyLong_AsLongLong(casted); | |
| 680 Py_DECREF(casted); | |
| 681 } | |
| 682 if (VerifyIntegerCastAndRange<T, PY_LONG_LONG>(arg, long_result)) { | |
| 683 *value = static_cast<T>(long_result); | |
| 684 } else { | |
| 685 return false; | |
| 686 } | 587 } |
| 687 } | 588 } |
| 688 | |
| 689 return true; | 589 return true; |
| 690 } | 590 } |
| 691 | 591 |
| 692 // These are referenced by repeated_scalar_container, and must | 592 // These are referenced by repeated_scalar_container, and must |
| 693 // be explicitly instantiated. | 593 // be explicitly instantiated. |
| 694 template bool CheckAndGetInteger<int32>(PyObject*, int32*); | 594 template bool CheckAndGetInteger<int32>( |
| 695 template bool CheckAndGetInteger<int64>(PyObject*, int64*); | 595 PyObject*, int32*, PyObject*, PyObject*); |
| 696 template bool CheckAndGetInteger<uint32>(PyObject*, uint32*); | 596 template bool CheckAndGetInteger<int64>( |
| 697 template bool CheckAndGetInteger<uint64>(PyObject*, uint64*); | 597 PyObject*, int64*, PyObject*, PyObject*); |
| 598 template bool CheckAndGetInteger<uint32>( |
| 599 PyObject*, uint32*, PyObject*, PyObject*); |
| 600 template bool CheckAndGetInteger<uint64>( |
| 601 PyObject*, uint64*, PyObject*, PyObject*); |
| 698 | 602 |
| 699 bool CheckAndGetDouble(PyObject* arg, double* value) { | 603 bool CheckAndGetDouble(PyObject* arg, double* value) { |
| 700 *value = PyFloat_AsDouble(arg); | 604 if (!PyInt_Check(arg) && !PyLong_Check(arg) && |
| 701 if GOOGLE_PREDICT_FALSE(*value == -1 && PyErr_Occurred()) { | 605 !PyFloat_Check(arg)) { |
| 702 FormatTypeError(arg, "int, long, float"); | 606 FormatTypeError(arg, "int, long, float"); |
| 703 return false; | 607 return false; |
| 704 } | 608 } |
| 609 *value = PyFloat_AsDouble(arg); |
| 705 return true; | 610 return true; |
| 706 } | 611 } |
| 707 | 612 |
| 708 bool CheckAndGetFloat(PyObject* arg, float* value) { | 613 bool CheckAndGetFloat(PyObject* arg, float* value) { |
| 709 double double_value; | 614 double double_value; |
| 710 if (!CheckAndGetDouble(arg, &double_value)) { | 615 if (!CheckAndGetDouble(arg, &double_value)) { |
| 711 return false; | 616 return false; |
| 712 } | 617 } |
| 713 *value = static_cast<float>(double_value); | 618 *value = static_cast<float>(double_value); |
| 714 return true; | 619 return true; |
| 715 } | 620 } |
| 716 | 621 |
| 717 bool CheckAndGetBool(PyObject* arg, bool* value) { | 622 bool CheckAndGetBool(PyObject* arg, bool* value) { |
| 718 long long_value = PyInt_AsLong(arg); | 623 if (!PyInt_Check(arg) && !PyBool_Check(arg) && !PyLong_Check(arg)) { |
| 719 if (long_value == -1 && PyErr_Occurred()) { | |
| 720 FormatTypeError(arg, "int, long, bool"); | 624 FormatTypeError(arg, "int, long, bool"); |
| 721 return false; | 625 return false; |
| 722 } | 626 } |
| 723 *value = static_cast<bool>(long_value); | 627 *value = static_cast<bool>(PyInt_AsLong(arg)); |
| 724 | |
| 725 return true; | 628 return true; |
| 726 } | 629 } |
| 727 | 630 |
| 728 // Checks whether the given object (which must be "bytes" or "unicode") contains | 631 // Checks whether the given object (which must be "bytes" or "unicode") contains |
| 729 // valid UTF-8. | 632 // valid UTF-8. |
| 730 bool IsValidUTF8(PyObject* obj) { | 633 bool IsValidUTF8(PyObject* obj) { |
| 731 if (PyBytes_Check(obj)) { | 634 if (PyBytes_Check(obj)) { |
| 732 PyObject* unicode = PyUnicode_FromEncodedObject(obj, "utf-8", NULL); | 635 PyObject* unicode = PyUnicode_FromEncodedObject(obj, "utf-8", NULL); |
| 733 | 636 |
| 734 // Clear the error indicator; we report our own error when desired. | 637 // Clear the error indicator; we report our own error when desired. |
| (...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 842 return true; | 745 return true; |
| 843 } | 746 } |
| 844 PyErr_Format(PyExc_KeyError, "Field '%s' does not belong to message '%s'", | 747 PyErr_Format(PyExc_KeyError, "Field '%s' does not belong to message '%s'", |
| 845 field_descriptor->full_name().c_str(), | 748 field_descriptor->full_name().c_str(), |
| 846 message->GetDescriptor()->full_name().c_str()); | 749 message->GetDescriptor()->full_name().c_str()); |
| 847 return false; | 750 return false; |
| 848 } | 751 } |
| 849 | 752 |
| 850 namespace cmessage { | 753 namespace cmessage { |
| 851 | 754 |
| 852 PyMessageFactory* GetFactoryForMessage(CMessage* message) { | 755 PyDescriptorPool* GetDescriptorPoolForMessage(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. |
| 853 GOOGLE_DCHECK(PyObject_TypeCheck(message, &CMessage_Type)); | 758 GOOGLE_DCHECK(PyObject_TypeCheck(message, &CMessage_Type)); |
| 854 return reinterpret_cast<CMessageClass*>(Py_TYPE(message))->py_message_factory; | 759 return reinterpret_cast<CMessageClass*>(Py_TYPE(message))->py_descriptor_pool; |
| 760 } |
| 761 |
| 762 MessageFactory* GetFactoryForMessage(CMessage* message) { |
| 763 return GetDescriptorPoolForMessage(message)->message_factory; |
| 855 } | 764 } |
| 856 | 765 |
| 857 static int MaybeReleaseOverlappingOneofField( | 766 static int MaybeReleaseOverlappingOneofField( |
| 858 CMessage* cmessage, | 767 CMessage* cmessage, |
| 859 const FieldDescriptor* field) { | 768 const FieldDescriptor* field) { |
| 860 #ifdef GOOGLE_PROTOBUF_HAS_ONEOF | 769 #ifdef GOOGLE_PROTOBUF_HAS_ONEOF |
| 861 Message* message = cmessage->message; | 770 Message* message = cmessage->message; |
| 862 const Reflection* reflection = message->GetReflection(); | 771 const Reflection* reflection = message->GetReflection(); |
| 863 if (!field->containing_oneof() || | 772 if (!field->containing_oneof() || |
| 864 !reflection->HasOneof(*message, field->containing_oneof()) || | 773 !reflection->HasOneof(*message, field->containing_oneof()) || |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 897 | 806 |
| 898 static Message* GetMutableMessage( | 807 static Message* GetMutableMessage( |
| 899 CMessage* parent, | 808 CMessage* parent, |
| 900 const FieldDescriptor* parent_field) { | 809 const FieldDescriptor* parent_field) { |
| 901 Message* parent_message = parent->message; | 810 Message* parent_message = parent->message; |
| 902 const Reflection* reflection = parent_message->GetReflection(); | 811 const Reflection* reflection = parent_message->GetReflection(); |
| 903 if (MaybeReleaseOverlappingOneofField(parent, parent_field) < 0) { | 812 if (MaybeReleaseOverlappingOneofField(parent, parent_field) < 0) { |
| 904 return NULL; | 813 return NULL; |
| 905 } | 814 } |
| 906 return reflection->MutableMessage( | 815 return reflection->MutableMessage( |
| 907 parent_message, parent_field, | 816 parent_message, parent_field, GetFactoryForMessage(parent)); |
| 908 GetFactoryForMessage(parent)->message_factory); | |
| 909 } | 817 } |
| 910 | 818 |
| 911 struct FixupMessageReference : public ChildVisitor { | 819 struct FixupMessageReference : public ChildVisitor { |
| 912 // message must outlive this object. | 820 // message must outlive this object. |
| 913 explicit FixupMessageReference(Message* message) : | 821 explicit FixupMessageReference(Message* message) : |
| 914 message_(message) {} | 822 message_(message) {} |
| 915 | 823 |
| 916 int VisitRepeatedCompositeContainer(RepeatedCompositeContainer* container) { | 824 int VisitRepeatedCompositeContainer(RepeatedCompositeContainer* container) { |
| 917 container->message = message_; | 825 container->message = message_; |
| 918 return 0; | 826 return 0; |
| (...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1046 CMessage* self, | 954 CMessage* self, |
| 1047 const FieldDescriptor* field_descriptor, | 955 const FieldDescriptor* field_descriptor, |
| 1048 PyObject* slice, | 956 PyObject* slice, |
| 1049 PyObject* cmessage_list) { | 957 PyObject* cmessage_list) { |
| 1050 Message* message = self->message; | 958 Message* message = self->message; |
| 1051 Py_ssize_t length, from, to, step, slice_length; | 959 Py_ssize_t length, from, to, step, slice_length; |
| 1052 const Reflection* reflection = message->GetReflection(); | 960 const Reflection* reflection = message->GetReflection(); |
| 1053 int min, max; | 961 int min, max; |
| 1054 length = reflection->FieldSize(*message, field_descriptor); | 962 length = reflection->FieldSize(*message, field_descriptor); |
| 1055 | 963 |
| 1056 if (PySlice_Check(slice)) { | 964 if (PyInt_Check(slice) || PyLong_Check(slice)) { |
| 965 from = to = PyLong_AsLong(slice); |
| 966 if (from < 0) { |
| 967 from = to = length + from; |
| 968 } |
| 969 step = 1; |
| 970 min = max = from; |
| 971 |
| 972 // Range check. |
| 973 if (from < 0 || from >= length) { |
| 974 PyErr_Format(PyExc_IndexError, "list assignment index out of range"); |
| 975 return -1; |
| 976 } |
| 977 } else if (PySlice_Check(slice)) { |
| 1057 from = to = step = slice_length = 0; | 978 from = to = step = slice_length = 0; |
| 1058 PySlice_GetIndicesEx( | 979 PySlice_GetIndicesEx( |
| 1059 #if PY_MAJOR_VERSION < 3 | 980 #if PY_MAJOR_VERSION < 3 |
| 1060 reinterpret_cast<PySliceObject*>(slice), | 981 reinterpret_cast<PySliceObject*>(slice), |
| 1061 #else | 982 #else |
| 1062 slice, | 983 slice, |
| 1063 #endif | 984 #endif |
| 1064 length, &from, &to, &step, &slice_length); | 985 length, &from, &to, &step, &slice_length); |
| 1065 if (from < to) { | 986 if (from < to) { |
| 1066 min = from; | 987 min = from; |
| 1067 max = to - 1; | 988 max = to - 1; |
| 1068 } else { | 989 } else { |
| 1069 min = to + 1; | 990 min = to + 1; |
| 1070 max = from; | 991 max = from; |
| 1071 } | 992 } |
| 1072 } else { | 993 } else { |
| 1073 from = to = PyLong_AsLong(slice); | 994 PyErr_SetString(PyExc_TypeError, "list indices must be integers"); |
| 1074 if (from == -1 && PyErr_Occurred()) { | 995 return -1; |
| 1075 PyErr_SetString(PyExc_TypeError, "list indices must be integers"); | |
| 1076 return -1; | |
| 1077 } | |
| 1078 | |
| 1079 if (from < 0) { | |
| 1080 from = to = length + from; | |
| 1081 } | |
| 1082 step = 1; | |
| 1083 min = max = from; | |
| 1084 | |
| 1085 // Range check. | |
| 1086 if (from < 0 || from >= length) { | |
| 1087 PyErr_Format(PyExc_IndexError, "list assignment index out of range"); | |
| 1088 return -1; | |
| 1089 } | |
| 1090 } | 996 } |
| 1091 | 997 |
| 1092 Py_ssize_t i = from; | 998 Py_ssize_t i = from; |
| 1093 std::vector<bool> to_delete(length, false); | 999 std::vector<bool> to_delete(length, false); |
| 1094 while (i >= min && i <= max) { | 1000 while (i >= min && i <= max) { |
| 1095 to_delete[i] = true; | 1001 to_delete[i] = true; |
| 1096 i += step; | 1002 i += step; |
| 1097 } | 1003 } |
| 1098 | 1004 |
| 1099 to = 0; | 1005 to = 0; |
| (...skipping 28 matching lines...) Expand all Loading... |
| 1128 return -1; | 1034 return -1; |
| 1129 } | 1035 } |
| 1130 } | 1036 } |
| 1131 --i; | 1037 --i; |
| 1132 } | 1038 } |
| 1133 | 1039 |
| 1134 return 0; | 1040 return 0; |
| 1135 } | 1041 } |
| 1136 | 1042 |
| 1137 // Initializes fields of a message. Used in constructors. | 1043 // Initializes fields of a message. Used in constructors. |
| 1138 int InitAttributes(CMessage* self, PyObject* args, PyObject* kwargs) { | 1044 int InitAttributes(CMessage* self, PyObject* kwargs) { |
| 1139 if (args != NULL && PyTuple_Size(args) != 0) { | |
| 1140 PyErr_SetString(PyExc_TypeError, "No positional arguments allowed"); | |
| 1141 return -1; | |
| 1142 } | |
| 1143 | |
| 1144 if (kwargs == NULL) { | 1045 if (kwargs == NULL) { |
| 1145 return 0; | 1046 return 0; |
| 1146 } | 1047 } |
| 1147 | 1048 |
| 1148 Py_ssize_t pos = 0; | 1049 Py_ssize_t pos = 0; |
| 1149 PyObject* name; | 1050 PyObject* name; |
| 1150 PyObject* value; | 1051 PyObject* value; |
| 1151 while (PyDict_Next(kwargs, &pos, &name, &value)) { | 1052 while (PyDict_Next(kwargs, &pos, &name, &value)) { |
| 1152 if (!PyString_Check(name)) { | 1053 if (!PyString_Check(name)) { |
| 1153 PyErr_SetString(PyExc_ValueError, "Field name must be a string"); | 1054 PyErr_SetString(PyExc_ValueError, "Field name must be a string"); |
| (...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1259 return -1; | 1160 return -1; |
| 1260 } | 1161 } |
| 1261 } | 1162 } |
| 1262 } else if (descriptor->cpp_type() == FieldDescriptor::CPPTYPE_MESSAGE) { | 1163 } else if (descriptor->cpp_type() == FieldDescriptor::CPPTYPE_MESSAGE) { |
| 1263 ScopedPyObjectPtr message(GetAttr(self, name)); | 1164 ScopedPyObjectPtr message(GetAttr(self, name)); |
| 1264 if (message == NULL) { | 1165 if (message == NULL) { |
| 1265 return -1; | 1166 return -1; |
| 1266 } | 1167 } |
| 1267 CMessage* cmessage = reinterpret_cast<CMessage*>(message.get()); | 1168 CMessage* cmessage = reinterpret_cast<CMessage*>(message.get()); |
| 1268 if (PyDict_Check(value)) { | 1169 if (PyDict_Check(value)) { |
| 1269 // Make the message exist even if the dict is empty. | 1170 if (InitAttributes(cmessage, value) < 0) { |
| 1270 AssureWritable(cmessage); | |
| 1271 if (InitAttributes(cmessage, NULL, value) < 0) { | |
| 1272 return -1; | 1171 return -1; |
| 1273 } | 1172 } |
| 1274 } else { | 1173 } else { |
| 1275 ScopedPyObjectPtr merged(MergeFrom(cmessage, value)); | 1174 ScopedPyObjectPtr merged(MergeFrom(cmessage, value)); |
| 1276 if (merged == NULL) { | 1175 if (merged == NULL) { |
| 1277 return -1; | 1176 return -1; |
| 1278 } | 1177 } |
| 1279 } | 1178 } |
| 1280 } else { | 1179 } else { |
| 1281 ScopedPyObjectPtr new_val; | 1180 ScopedPyObjectPtr new_val; |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1320 PyObject* unused_args, PyObject* unused_kwargs) { | 1219 PyObject* unused_args, PyObject* unused_kwargs) { |
| 1321 CMessageClass* type = CheckMessageClass(cls); | 1220 CMessageClass* type = CheckMessageClass(cls); |
| 1322 if (type == NULL) { | 1221 if (type == NULL) { |
| 1323 return NULL; | 1222 return NULL; |
| 1324 } | 1223 } |
| 1325 // Retrieve the message descriptor and the default instance (=prototype). | 1224 // Retrieve the message descriptor and the default instance (=prototype). |
| 1326 const Descriptor* message_descriptor = type->message_descriptor; | 1225 const Descriptor* message_descriptor = type->message_descriptor; |
| 1327 if (message_descriptor == NULL) { | 1226 if (message_descriptor == NULL) { |
| 1328 return NULL; | 1227 return NULL; |
| 1329 } | 1228 } |
| 1330 const Message* default_message = type->py_message_factory->message_factory | 1229 const Message* default_message = type->py_descriptor_pool->message_factory |
| 1331 ->GetPrototype(message_descriptor); | 1230 ->GetPrototype(message_descriptor); |
| 1332 if (default_message == NULL) { | 1231 if (default_message == NULL) { |
| 1333 PyErr_SetString(PyExc_TypeError, message_descriptor->full_name().c_str()); | 1232 PyErr_SetString(PyExc_TypeError, message_descriptor->full_name().c_str()); |
| 1334 return NULL; | 1233 return NULL; |
| 1335 } | 1234 } |
| 1336 | 1235 |
| 1337 CMessage* self = NewEmptyMessage(type); | 1236 CMessage* self = NewEmptyMessage(type); |
| 1338 if (self == NULL) { | 1237 if (self == NULL) { |
| 1339 return NULL; | 1238 return NULL; |
| 1340 } | 1239 } |
| 1341 self->message = default_message->New(); | 1240 self->message = default_message->New(); |
| 1342 self->owner.reset(self->message); | 1241 self->owner.reset(self->message); |
| 1343 return reinterpret_cast<PyObject*>(self); | 1242 return reinterpret_cast<PyObject*>(self); |
| 1344 } | 1243 } |
| 1345 | 1244 |
| 1346 // The __init__ method of Message classes. | 1245 // The __init__ method of Message classes. |
| 1347 // It initializes fields from keywords passed to the constructor. | 1246 // It initializes fields from keywords passed to the constructor. |
| 1348 static int Init(CMessage* self, PyObject* args, PyObject* kwargs) { | 1247 static int Init(CMessage* self, PyObject* args, PyObject* kwargs) { |
| 1349 return InitAttributes(self, args, kwargs); | 1248 if (PyTuple_Size(args) != 0) { |
| 1249 PyErr_SetString(PyExc_TypeError, "No positional arguments allowed"); |
| 1250 return -1; |
| 1251 } |
| 1252 |
| 1253 return InitAttributes(self, kwargs); |
| 1350 } | 1254 } |
| 1351 | 1255 |
| 1352 // --------------------------------------------------------------------- | 1256 // --------------------------------------------------------------------- |
| 1353 // Deallocating a CMessage | 1257 // Deallocating a CMessage |
| 1354 // | 1258 // |
| 1355 // Deallocating a CMessage requires that we clear any weak references | 1259 // Deallocating a CMessage requires that we clear any weak references |
| 1356 // from children to the message being deallocated. | 1260 // from children to the message being deallocated. |
| 1357 | 1261 |
| 1358 // Clear the weak reference from the child to the parent. | 1262 // Clear the weak reference from the child to the parent. |
| 1359 struct ClearWeakReferences : public ChildVisitor { | 1263 struct ClearWeakReferences : public ChildVisitor { |
| (...skipping 21 matching lines...) Expand all Loading... |
| 1381 } | 1285 } |
| 1382 | 1286 |
| 1383 int VisitCMessage(CMessage* cmessage, | 1287 int VisitCMessage(CMessage* cmessage, |
| 1384 const FieldDescriptor* field_descriptor) { | 1288 const FieldDescriptor* field_descriptor) { |
| 1385 cmessage->parent = NULL; | 1289 cmessage->parent = NULL; |
| 1386 return 0; | 1290 return 0; |
| 1387 } | 1291 } |
| 1388 }; | 1292 }; |
| 1389 | 1293 |
| 1390 static void Dealloc(CMessage* self) { | 1294 static void Dealloc(CMessage* self) { |
| 1391 if (self->weakreflist) { | |
| 1392 PyObject_ClearWeakRefs(reinterpret_cast<PyObject*>(self)); | |
| 1393 } | |
| 1394 // Null out all weak references from children to this message. | 1295 // Null out all weak references from children to this message. |
| 1395 GOOGLE_CHECK_EQ(0, ForEachCompositeField(self, ClearWeakReferences())); | 1296 GOOGLE_CHECK_EQ(0, ForEachCompositeField(self, ClearWeakReferences())); |
| 1396 if (self->extensions) { | 1297 if (self->extensions) { |
| 1397 self->extensions->parent = NULL; | 1298 self->extensions->parent = NULL; |
| 1398 } | 1299 } |
| 1399 | 1300 |
| 1400 Py_CLEAR(self->extensions); | 1301 Py_CLEAR(self->extensions); |
| 1401 Py_CLEAR(self->composite_fields); | 1302 Py_CLEAR(self->composite_fields); |
| 1402 self->owner.reset(); | 1303 self->owner.reset(); |
| 1403 Py_TYPE(self)->tp_free(reinterpret_cast<PyObject*>(self)); | 1304 Py_TYPE(self)->tp_free(reinterpret_cast<PyObject*>(self)); |
| (...skipping 147 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1551 for (int i = 0; i < unknown_field_set.field_count(); ++i) { | 1452 for (int i = 0; i < unknown_field_set.field_count(); ++i) { |
| 1552 if (unknown_field_set.field(i).number() == field_descriptor->number()) { | 1453 if (unknown_field_set.field(i).number() == field_descriptor->number()) { |
| 1553 Py_RETURN_TRUE; | 1454 Py_RETURN_TRUE; |
| 1554 } | 1455 } |
| 1555 } | 1456 } |
| 1556 } | 1457 } |
| 1557 Py_RETURN_FALSE; | 1458 Py_RETURN_FALSE; |
| 1558 } | 1459 } |
| 1559 | 1460 |
| 1560 PyObject* ClearExtension(CMessage* self, PyObject* extension) { | 1461 PyObject* ClearExtension(CMessage* self, PyObject* extension) { |
| 1561 const FieldDescriptor* descriptor = GetExtensionDescriptor(extension); | |
| 1562 if (descriptor == NULL) { | |
| 1563 return NULL; | |
| 1564 } | |
| 1565 if (self->extensions != NULL) { | 1462 if (self->extensions != NULL) { |
| 1566 PyObject* value = PyDict_GetItem(self->extensions->values, extension); | 1463 return extension_dict::ClearExtension(self->extensions, extension); |
| 1567 if (value != NULL) { | 1464 } else { |
| 1568 if (InternalReleaseFieldByDescriptor(self, descriptor, value) < 0) { | 1465 const FieldDescriptor* descriptor = GetExtensionDescriptor(extension); |
| 1569 return NULL; | 1466 if (descriptor == NULL) { |
| 1570 } | 1467 return NULL; |
| 1571 PyDict_DelItem(self->extensions->values, extension); | 1468 } |
| 1469 if (ScopedPyObjectPtr(ClearFieldByDescriptor(self, descriptor)) == NULL) { |
| 1470 return NULL; |
| 1572 } | 1471 } |
| 1573 } | 1472 } |
| 1574 return ClearFieldByDescriptor(self, descriptor); | 1473 Py_RETURN_NONE; |
| 1575 } | 1474 } |
| 1576 | 1475 |
| 1577 PyObject* HasExtension(CMessage* self, PyObject* extension) { | 1476 PyObject* HasExtension(CMessage* self, PyObject* extension) { |
| 1578 const FieldDescriptor* descriptor = GetExtensionDescriptor(extension); | 1477 const FieldDescriptor* descriptor = GetExtensionDescriptor(extension); |
| 1579 if (descriptor == NULL) { | 1478 if (descriptor == NULL) { |
| 1580 return NULL; | 1479 return NULL; |
| 1581 } | 1480 } |
| 1582 return HasFieldByDescriptor(self, descriptor); | 1481 return HasFieldByDescriptor(self, descriptor); |
| 1583 } | 1482 } |
| 1584 | 1483 |
| (...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1650 return -1; | 1549 return -1; |
| 1651 return 0; | 1550 return 0; |
| 1652 } | 1551 } |
| 1653 | 1552 |
| 1654 // Releases the message specified by 'field' and returns the | 1553 // Releases the message specified by 'field' and returns the |
| 1655 // pointer. If the field does not exist a new message is created using | 1554 // pointer. If the field does not exist a new message is created using |
| 1656 // 'descriptor'. The caller takes ownership of the returned pointer. | 1555 // 'descriptor'. The caller takes ownership of the returned pointer. |
| 1657 Message* ReleaseMessage(CMessage* self, | 1556 Message* ReleaseMessage(CMessage* self, |
| 1658 const Descriptor* descriptor, | 1557 const Descriptor* descriptor, |
| 1659 const FieldDescriptor* field_descriptor) { | 1558 const FieldDescriptor* field_descriptor) { |
| 1660 MessageFactory* message_factory = GetFactoryForMessage(self)->message_factory; | 1559 MessageFactory* message_factory = GetFactoryForMessage(self); |
| 1661 Message* released_message = self->message->GetReflection()->ReleaseMessage( | 1560 Message* released_message = self->message->GetReflection()->ReleaseMessage( |
| 1662 self->message, field_descriptor, message_factory); | 1561 self->message, field_descriptor, message_factory); |
| 1663 // ReleaseMessage will return NULL which differs from | 1562 // ReleaseMessage will return NULL which differs from |
| 1664 // child_cmessage->message, if the field does not exist. In this case, | 1563 // child_cmessage->message, if the field does not exist. In this case, |
| 1665 // the latter points to the default instance via a const_cast<>, so we | 1564 // the latter points to the default instance via a const_cast<>, so we |
| 1666 // have to reset it to a new mutable object since we are taking ownership. | 1565 // have to reset it to a new mutable object since we are taking ownership. |
| 1667 if (released_message == NULL) { | 1566 if (released_message == NULL) { |
| 1668 const Message* prototype = message_factory->GetPrototype(descriptor); | 1567 const Message* prototype = message_factory->GetPrototype(descriptor); |
| 1669 GOOGLE_DCHECK(prototype != NULL); | 1568 GOOGLE_DCHECK(prototype != NULL); |
| 1670 released_message = prototype->New(); | 1569 released_message = prototype->New(); |
| (...skipping 16 matching lines...) Expand all Loading... |
| 1687 return ForEachCompositeField(child_cmessage, | 1586 return ForEachCompositeField(child_cmessage, |
| 1688 SetOwnerVisitor(child_cmessage->owner)); | 1587 SetOwnerVisitor(child_cmessage->owner)); |
| 1689 } | 1588 } |
| 1690 | 1589 |
| 1691 struct ReleaseChild : public ChildVisitor { | 1590 struct ReleaseChild : public ChildVisitor { |
| 1692 // message must outlive this object. | 1591 // message must outlive this object. |
| 1693 explicit ReleaseChild(CMessage* parent) : | 1592 explicit ReleaseChild(CMessage* parent) : |
| 1694 parent_(parent) {} | 1593 parent_(parent) {} |
| 1695 | 1594 |
| 1696 int VisitRepeatedCompositeContainer(RepeatedCompositeContainer* container) { | 1595 int VisitRepeatedCompositeContainer(RepeatedCompositeContainer* container) { |
| 1697 return repeated_composite_container::Release(container); | 1596 return repeated_composite_container::Release( |
| 1597 reinterpret_cast<RepeatedCompositeContainer*>(container)); |
| 1698 } | 1598 } |
| 1699 | 1599 |
| 1700 int VisitRepeatedScalarContainer(RepeatedScalarContainer* container) { | 1600 int VisitRepeatedScalarContainer(RepeatedScalarContainer* container) { |
| 1701 return repeated_scalar_container::Release(container); | 1601 return repeated_scalar_container::Release( |
| 1602 reinterpret_cast<RepeatedScalarContainer*>(container)); |
| 1702 } | 1603 } |
| 1703 | 1604 |
| 1704 int VisitMapContainer(MapContainer* container) { | 1605 int VisitMapContainer(MapContainer* container) { |
| 1705 return container->Release(); | 1606 return reinterpret_cast<MapContainer*>(container)->Release(); |
| 1706 } | 1607 } |
| 1707 | 1608 |
| 1708 int VisitCMessage(CMessage* cmessage, | 1609 int VisitCMessage(CMessage* cmessage, |
| 1709 const FieldDescriptor* field_descriptor) { | 1610 const FieldDescriptor* field_descriptor) { |
| 1710 return ReleaseSubMessage(parent_, field_descriptor, cmessage); | 1611 return ReleaseSubMessage(parent_, field_descriptor, |
| 1612 reinterpret_cast<CMessage*>(cmessage)); |
| 1711 } | 1613 } |
| 1712 | 1614 |
| 1713 CMessage* parent_; | 1615 CMessage* parent_; |
| 1714 }; | 1616 }; |
| 1715 | 1617 |
| 1716 int InternalReleaseFieldByDescriptor( | 1618 int InternalReleaseFieldByDescriptor( |
| 1717 CMessage* self, | 1619 CMessage* self, |
| 1718 const FieldDescriptor* field_descriptor, | 1620 const FieldDescriptor* field_descriptor, |
| 1719 PyObject* composite_field) { | 1621 PyObject* composite_field) { |
| 1720 return VisitCompositeField( | 1622 return VisitCompositeField( |
| 1721 field_descriptor, | 1623 field_descriptor, |
| 1722 composite_field, | 1624 composite_field, |
| 1723 ReleaseChild(self)); | 1625 ReleaseChild(self)); |
| 1724 } | 1626 } |
| 1725 | 1627 |
| 1726 PyObject* ClearFieldByDescriptor( | 1628 PyObject* ClearFieldByDescriptor( |
| 1727 CMessage* self, | 1629 CMessage* self, |
| 1728 const FieldDescriptor* field_descriptor) { | 1630 const FieldDescriptor* descriptor) { |
| 1729 if (!CheckFieldBelongsToMessage(field_descriptor, self->message)) { | 1631 if (!CheckFieldBelongsToMessage(descriptor, self->message)) { |
| 1730 return NULL; | 1632 return NULL; |
| 1731 } | 1633 } |
| 1732 AssureWritable(self); | 1634 AssureWritable(self); |
| 1733 Message* message = self->message; | 1635 self->message->GetReflection()->ClearField(self->message, descriptor); |
| 1734 message->GetReflection()->ClearField(message, field_descriptor); | |
| 1735 if (field_descriptor->cpp_type() == FieldDescriptor::CPPTYPE_ENUM && | |
| 1736 !message->GetReflection()->SupportsUnknownEnumValues()) { | |
| 1737 UnknownFieldSet* unknown_field_set = | |
| 1738 message->GetReflection()->MutableUnknownFields(message); | |
| 1739 unknown_field_set->DeleteByNumber(field_descriptor->number()); | |
| 1740 } | |
| 1741 Py_RETURN_NONE; | 1636 Py_RETURN_NONE; |
| 1742 } | 1637 } |
| 1743 | 1638 |
| 1744 PyObject* ClearField(CMessage* self, PyObject* arg) { | 1639 PyObject* ClearField(CMessage* self, PyObject* arg) { |
| 1745 if (!PyString_Check(arg)) { | 1640 if (!PyString_Check(arg)) { |
| 1746 PyErr_SetString(PyExc_TypeError, "field name must be a string"); | 1641 PyErr_SetString(PyExc_TypeError, "field name must be a string"); |
| 1747 return NULL; | 1642 return NULL; |
| 1748 } | 1643 } |
| 1749 #if PY_MAJOR_VERSION < 3 | 1644 #if PY_MAJOR_VERSION < 3 |
| 1750 const char* field_name = PyString_AS_STRING(arg); | 1645 const char* field_name = PyString_AS_STRING(arg); |
| (...skipping 15 matching lines...) Expand all Loading... |
| 1766 return NULL; | 1661 return NULL; |
| 1767 } else { | 1662 } else { |
| 1768 Py_RETURN_NONE; | 1663 Py_RETURN_NONE; |
| 1769 } | 1664 } |
| 1770 } else if (is_in_oneof) { | 1665 } else if (is_in_oneof) { |
| 1771 const string& name = field_descriptor->name(); | 1666 const string& name = field_descriptor->name(); |
| 1772 arg_in_oneof.reset(PyString_FromStringAndSize(name.c_str(), name.size())); | 1667 arg_in_oneof.reset(PyString_FromStringAndSize(name.c_str(), name.size())); |
| 1773 arg = arg_in_oneof.get(); | 1668 arg = arg_in_oneof.get(); |
| 1774 } | 1669 } |
| 1775 | 1670 |
| 1776 // Release the field if it exists in the dict of composite fields. | 1671 PyObject* composite_field = self->composite_fields ? |
| 1777 if (self->composite_fields) { | 1672 PyDict_GetItem(self->composite_fields, arg) : NULL; |
| 1778 PyObject* value = PyDict_GetItem(self->composite_fields, arg); | 1673 |
| 1779 if (value != NULL) { | 1674 // Only release the field if there's a possibility that there are |
| 1780 if (InternalReleaseFieldByDescriptor(self, field_descriptor, value) < 0) { | 1675 // references to it. |
| 1781 return NULL; | 1676 if (composite_field != NULL) { |
| 1782 } | 1677 if (InternalReleaseFieldByDescriptor(self, field_descriptor, |
| 1783 PyDict_DelItem(self->composite_fields, arg); | 1678 composite_field) < 0) { |
| 1679 return NULL; |
| 1784 } | 1680 } |
| 1681 PyDict_DelItem(self->composite_fields, arg); |
| 1785 } | 1682 } |
| 1786 return ClearFieldByDescriptor(self, field_descriptor); | 1683 message->GetReflection()->ClearField(message, 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; |
| 1787 } | 1692 } |
| 1788 | 1693 |
| 1789 PyObject* Clear(CMessage* self) { | 1694 PyObject* Clear(CMessage* self) { |
| 1790 AssureWritable(self); | 1695 AssureWritable(self); |
| 1791 if (ForEachCompositeField(self, ReleaseChild(self)) == -1) | 1696 if (ForEachCompositeField(self, ReleaseChild(self)) == -1) |
| 1792 return NULL; | 1697 return NULL; |
| 1793 Py_CLEAR(self->extensions); | 1698 Py_CLEAR(self->extensions); |
| 1794 if (self->composite_fields) { | 1699 if (self->composite_fields) { |
| 1795 PyDict_Clear(self->composite_fields); | 1700 PyDict_Clear(self->composite_fields); |
| 1796 } | 1701 } |
| (...skipping 190 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1987 | 1892 |
| 1988 Py_RETURN_NONE; | 1893 Py_RETURN_NONE; |
| 1989 } | 1894 } |
| 1990 | 1895 |
| 1991 // Protobuf has a 64MB limit built in, this variable will override this. Please | 1896 // Protobuf has a 64MB limit built in, this variable will override this. Please |
| 1992 // do not enable this unless you fully understand the implications: protobufs | 1897 // do not enable this unless you fully understand the implications: protobufs |
| 1993 // must all be kept in memory at the same time, so if they grow too big you may | 1898 // must all be kept in memory at the same time, so if they grow too big you may |
| 1994 // get OOM errors. The protobuf APIs do not provide any tools for processing | 1899 // get OOM errors. The protobuf APIs do not provide any tools for processing |
| 1995 // protobufs in chunks. If you have protos this big you should break them up if | 1900 // protobufs in chunks. If you have protos this big you should break them up if |
| 1996 // it is at all convenient to do so. | 1901 // it is at all convenient to do so. |
| 1997 #ifdef PROTOBUF_PYTHON_ALLOW_OVERSIZE_PROTOS | |
| 1998 static bool allow_oversize_protos = true; | |
| 1999 #else | |
| 2000 static bool allow_oversize_protos = false; | 1902 static bool allow_oversize_protos = false; |
| 2001 #endif | |
| 2002 | 1903 |
| 2003 // Provide a method in the module to set allow_oversize_protos to a boolean | 1904 // Provide a method in the module to set allow_oversize_protos to a boolean |
| 2004 // value. This method returns the newly value of allow_oversize_protos. | 1905 // value. This method returns the newly value of allow_oversize_protos. |
| 2005 PyObject* SetAllowOversizeProtos(PyObject* m, PyObject* arg) { | 1906 static PyObject* SetAllowOversizeProtos(PyObject* m, PyObject* arg) { |
| 2006 if (!arg || !PyBool_Check(arg)) { | 1907 if (!arg || !PyBool_Check(arg)) { |
| 2007 PyErr_SetString(PyExc_TypeError, | 1908 PyErr_SetString(PyExc_TypeError, |
| 2008 "Argument to SetAllowOversizeProtos must be boolean"); | 1909 "Argument to SetAllowOversizeProtos must be boolean"); |
| 2009 return NULL; | 1910 return NULL; |
| 2010 } | 1911 } |
| 2011 allow_oversize_protos = PyObject_IsTrue(arg); | 1912 allow_oversize_protos = PyObject_IsTrue(arg); |
| 2012 if (allow_oversize_protos) { | 1913 if (allow_oversize_protos) { |
| 2013 Py_RETURN_TRUE; | 1914 Py_RETURN_TRUE; |
| 2014 } else { | 1915 } else { |
| 2015 Py_RETURN_FALSE; | 1916 Py_RETURN_FALSE; |
| 2016 } | 1917 } |
| 2017 } | 1918 } |
| 2018 | 1919 |
| 2019 static PyObject* MergeFromString(CMessage* self, PyObject* arg) { | 1920 static PyObject* MergeFromString(CMessage* self, PyObject* arg) { |
| 2020 const void* data; | 1921 const void* data; |
| 2021 Py_ssize_t data_length; | 1922 Py_ssize_t data_length; |
| 2022 if (PyObject_AsReadBuffer(arg, &data, &data_length) < 0) { | 1923 if (PyObject_AsReadBuffer(arg, &data, &data_length) < 0) { |
| 2023 return NULL; | 1924 return NULL; |
| 2024 } | 1925 } |
| 2025 | 1926 |
| 2026 AssureWritable(self); | 1927 AssureWritable(self); |
| 2027 io::CodedInputStream input( | 1928 io::CodedInputStream input( |
| 2028 reinterpret_cast<const uint8*>(data), data_length); | 1929 reinterpret_cast<const uint8*>(data), data_length); |
| 2029 if (allow_oversize_protos) { | 1930 if (allow_oversize_protos) { |
| 2030 input.SetTotalBytesLimit(INT_MAX, INT_MAX); | 1931 input.SetTotalBytesLimit(INT_MAX, INT_MAX); |
| 2031 } | 1932 } |
| 2032 PyMessageFactory* factory = GetFactoryForMessage(self); | 1933 PyDescriptorPool* pool = GetDescriptorPoolForMessage(self); |
| 2033 input.SetExtensionRegistry(factory->pool->pool, factory->message_factory); | 1934 input.SetExtensionRegistry(pool->pool, pool->message_factory); |
| 2034 bool success = self->message->MergePartialFromCodedStream(&input); | 1935 bool success = self->message->MergePartialFromCodedStream(&input); |
| 2035 if (success) { | 1936 if (success) { |
| 2036 return PyInt_FromLong(input.CurrentPosition()); | 1937 return PyInt_FromLong(input.CurrentPosition()); |
| 2037 } else { | 1938 } else { |
| 2038 PyErr_Format(DecodeError_class, "Error parsing message"); | 1939 PyErr_Format(DecodeError_class, "Error parsing message"); |
| 2039 return NULL; | 1940 return NULL; |
| 2040 } | 1941 } |
| 2041 } | 1942 } |
| 2042 | 1943 |
| 2043 static PyObject* ParseFromString(CMessage* self, PyObject* arg) { | 1944 static PyObject* ParseFromString(CMessage* self, PyObject* arg) { |
| 2044 if (ScopedPyObjectPtr(Clear(self)) == NULL) { | 1945 if (ScopedPyObjectPtr(Clear(self)) == NULL) { |
| 2045 return NULL; | 1946 return NULL; |
| 2046 } | 1947 } |
| 2047 return MergeFromString(self, arg); | 1948 return MergeFromString(self, arg); |
| 2048 } | 1949 } |
| 2049 | 1950 |
| 2050 static PyObject* ByteSize(CMessage* self, PyObject* args) { | 1951 static PyObject* ByteSize(CMessage* self, PyObject* args) { |
| 2051 return PyLong_FromLong(self->message->ByteSize()); | 1952 return PyLong_FromLong(self->message->ByteSize()); |
| 2052 } | 1953 } |
| 2053 | 1954 |
| 2054 PyObject* RegisterExtension(PyObject* cls, PyObject* extension_handle) { | 1955 static PyObject* RegisterExtension(PyObject* cls, |
| 1956 PyObject* extension_handle) { |
| 2055 const FieldDescriptor* descriptor = | 1957 const FieldDescriptor* descriptor = |
| 2056 GetExtensionDescriptor(extension_handle); | 1958 GetExtensionDescriptor(extension_handle); |
| 2057 if (descriptor == NULL) { | 1959 if (descriptor == NULL) { |
| 2058 return NULL; | 1960 return NULL; |
| 2059 } | 1961 } |
| 2060 if (!PyObject_TypeCheck(cls, &CMessageClass_Type)) { | 1962 |
| 2061 PyErr_Format(PyExc_TypeError, "Expected a message class, got %s", | 1963 ScopedPyObjectPtr extensions_by_name( |
| 2062 cls->ob_type->tp_name); | 1964 PyObject_GetAttr(cls, k_extensions_by_name)); |
| 1965 if (extensions_by_name == NULL) { |
| 1966 PyErr_SetString(PyExc_TypeError, "no extensions_by_name on class"); |
| 2063 return NULL; | 1967 return NULL; |
| 2064 } | 1968 } |
| 2065 CMessageClass *message_class = reinterpret_cast<CMessageClass*>(cls); | 1969 ScopedPyObjectPtr full_name(PyObject_GetAttr(extension_handle, kfull_name)); |
| 2066 if (message_class == NULL) { | 1970 if (full_name == NULL) { |
| 2067 return NULL; | 1971 return NULL; |
| 2068 } | 1972 } |
| 1973 |
| 2069 // If the extension was already registered, check that it is the same. | 1974 // If the extension was already registered, check that it is the same. |
| 2070 const FieldDescriptor* existing_extension = | 1975 PyObject* existing_extension = |
| 2071 message_class->py_message_factory->pool->pool->FindExtensionByNumber( | 1976 PyDict_GetItem(extensions_by_name.get(), full_name.get()); |
| 2072 descriptor->containing_type(), descriptor->number()); | 1977 if (existing_extension != NULL) { |
| 2073 if (existing_extension != NULL && existing_extension != descriptor) { | 1978 const FieldDescriptor* existing_extension_descriptor = |
| 2074 PyErr_SetString(PyExc_ValueError, "Double registration of Extensions"); | 1979 GetExtensionDescriptor(existing_extension); |
| 1980 if (existing_extension_descriptor != descriptor) { |
| 1981 PyErr_SetString(PyExc_ValueError, "Double registration of Extensions"); |
| 1982 return NULL; |
| 1983 } |
| 1984 // Nothing else to do. |
| 1985 Py_RETURN_NONE; |
| 1986 } |
| 1987 |
| 1988 if (PyDict_SetItem(extensions_by_name.get(), full_name.get(), |
| 1989 extension_handle) < 0) { |
| 2075 return NULL; | 1990 return NULL; |
| 2076 } | 1991 } |
| 1992 |
| 1993 // Also store a mapping from extension number to implementing class. |
| 1994 ScopedPyObjectPtr extensions_by_number( |
| 1995 PyObject_GetAttr(cls, k_extensions_by_number)); |
| 1996 if (extensions_by_number == NULL) { |
| 1997 PyErr_SetString(PyExc_TypeError, "no extensions_by_number on class"); |
| 1998 return NULL; |
| 1999 } |
| 2000 |
| 2001 ScopedPyObjectPtr number(PyObject_GetAttrString(extension_handle, "number")); |
| 2002 if (number == NULL) { |
| 2003 return NULL; |
| 2004 } |
| 2005 |
| 2006 // If the extension was already registered by number, check that it is the |
| 2007 // same. |
| 2008 existing_extension = PyDict_GetItem(extensions_by_number.get(), number.get()); |
| 2009 if (existing_extension != NULL) { |
| 2010 const FieldDescriptor* existing_extension_descriptor = |
| 2011 GetExtensionDescriptor(existing_extension); |
| 2012 if (existing_extension_descriptor != descriptor) { |
| 2013 const Descriptor* msg_desc = GetMessageDescriptor( |
| 2014 reinterpret_cast<PyTypeObject*>(cls)); |
| 2015 PyErr_Format( |
| 2016 PyExc_ValueError, |
| 2017 "Extensions \"%s\" and \"%s\" both try to extend message type " |
| 2018 "\"%s\" with field number %ld.", |
| 2019 existing_extension_descriptor->full_name().c_str(), |
| 2020 descriptor->full_name().c_str(), |
| 2021 msg_desc->full_name().c_str(), |
| 2022 PyInt_AsLong(number.get())); |
| 2023 return NULL; |
| 2024 } |
| 2025 // Nothing else to do. |
| 2026 Py_RETURN_NONE; |
| 2027 } |
| 2028 if (PyDict_SetItem(extensions_by_number.get(), number.get(), |
| 2029 extension_handle) < 0) { |
| 2030 return NULL; |
| 2031 } |
| 2032 |
| 2033 // Check if it's a message set |
| 2034 if (descriptor->is_extension() && |
| 2035 descriptor->containing_type()->options().message_set_wire_format() && |
| 2036 descriptor->type() == FieldDescriptor::TYPE_MESSAGE && |
| 2037 descriptor->label() == FieldDescriptor::LABEL_OPTIONAL) { |
| 2038 ScopedPyObjectPtr message_name(PyString_FromStringAndSize( |
| 2039 descriptor->message_type()->full_name().c_str(), |
| 2040 descriptor->message_type()->full_name().size())); |
| 2041 if (message_name == NULL) { |
| 2042 return NULL; |
| 2043 } |
| 2044 PyDict_SetItem(extensions_by_name.get(), message_name.get(), |
| 2045 extension_handle); |
| 2046 } |
| 2047 |
| 2077 Py_RETURN_NONE; | 2048 Py_RETURN_NONE; |
| 2078 } | 2049 } |
| 2079 | 2050 |
| 2080 static PyObject* SetInParent(CMessage* self, PyObject* args) { | 2051 static PyObject* SetInParent(CMessage* self, PyObject* args) { |
| 2081 AssureWritable(self); | 2052 AssureWritable(self); |
| 2082 Py_RETURN_NONE; | 2053 Py_RETURN_NONE; |
| 2083 } | 2054 } |
| 2084 | 2055 |
| 2085 static PyObject* WhichOneof(CMessage* self, PyObject* arg) { | 2056 static PyObject* WhichOneof(CMessage* self, PyObject* arg) { |
| 2086 Py_ssize_t name_size; | 2057 Py_ssize_t name_size; |
| (...skipping 16 matching lines...) Expand all Loading... |
| 2103 Py_RETURN_NONE; | 2074 Py_RETURN_NONE; |
| 2104 } else { | 2075 } else { |
| 2105 const string& name = field_in_oneof->name(); | 2076 const string& name = field_in_oneof->name(); |
| 2106 return PyString_FromStringAndSize(name.c_str(), name.size()); | 2077 return PyString_FromStringAndSize(name.c_str(), name.size()); |
| 2107 } | 2078 } |
| 2108 } | 2079 } |
| 2109 | 2080 |
| 2110 static PyObject* GetExtensionDict(CMessage* self, void *closure); | 2081 static PyObject* GetExtensionDict(CMessage* self, void *closure); |
| 2111 | 2082 |
| 2112 static PyObject* ListFields(CMessage* self) { | 2083 static PyObject* ListFields(CMessage* self) { |
| 2113 std::vector<const FieldDescriptor*> fields; | 2084 vector<const FieldDescriptor*> fields; |
| 2114 self->message->GetReflection()->ListFields(*self->message, &fields); | 2085 self->message->GetReflection()->ListFields(*self->message, &fields); |
| 2115 | 2086 |
| 2116 // Normally, the list will be exactly the size of the fields. | 2087 // Normally, the list will be exactly the size of the fields. |
| 2117 ScopedPyObjectPtr all_fields(PyList_New(fields.size())); | 2088 ScopedPyObjectPtr all_fields(PyList_New(fields.size())); |
| 2118 if (all_fields == NULL) { | 2089 if (all_fields == NULL) { |
| 2119 return NULL; | 2090 return NULL; |
| 2120 } | 2091 } |
| 2121 | 2092 |
| 2122 // When there are unknown extensions, the py list will *not* contain | 2093 // When there are unknown extensions, the py list will *not* contain |
| 2123 // the field information. Thus the actual size of the py list will be | 2094 // the field information. Thus the actual size of the py list will be |
| 2124 // smaller than the size of fields. Set the actual size at the end. | 2095 // smaller than the size of fields. Set the actual size at the end. |
| 2125 Py_ssize_t actual_size = 0; | 2096 Py_ssize_t actual_size = 0; |
| 2126 for (size_t i = 0; i < fields.size(); ++i) { | 2097 for (size_t i = 0; i < fields.size(); ++i) { |
| 2127 ScopedPyObjectPtr t(PyTuple_New(2)); | 2098 ScopedPyObjectPtr t(PyTuple_New(2)); |
| 2128 if (t == NULL) { | 2099 if (t == NULL) { |
| 2129 return NULL; | 2100 return NULL; |
| 2130 } | 2101 } |
| 2131 | 2102 |
| 2132 if (fields[i]->is_extension()) { | 2103 if (fields[i]->is_extension()) { |
| 2133 ScopedPyObjectPtr extension_field( | 2104 ScopedPyObjectPtr extension_field( |
| 2134 PyFieldDescriptor_FromDescriptor(fields[i])); | 2105 PyFieldDescriptor_FromDescriptor(fields[i])); |
| 2135 if (extension_field == NULL) { | 2106 if (extension_field == NULL) { |
| 2136 return NULL; | 2107 return NULL; |
| 2137 } | 2108 } |
| 2138 // With C++ descriptors, the field can always be retrieved, but for | 2109 // With C++ descriptors, the field can always be retrieved, but for |
| 2139 // unknown extensions which have not been imported in Python code, there | 2110 // unknown extensions which have not been imported in Python code, there |
| 2140 // is no message class and we cannot retrieve the value. | 2111 // is no message class and we cannot retrieve the value. |
| 2141 // TODO(amauryfa): consider building the class on the fly! | 2112 // TODO(amauryfa): consider building the class on the fly! |
| 2142 if (fields[i]->message_type() != NULL && | 2113 if (fields[i]->message_type() != NULL && |
| 2143 message_factory::GetMessageClass( | 2114 cdescriptor_pool::GetMessageClass( |
| 2144 GetFactoryForMessage(self), | 2115 GetDescriptorPoolForMessage(self), |
| 2145 fields[i]->message_type()) == NULL) { | 2116 fields[i]->message_type()) == NULL) { |
| 2146 PyErr_Clear(); | 2117 PyErr_Clear(); |
| 2147 continue; | 2118 continue; |
| 2148 } | 2119 } |
| 2149 ScopedPyObjectPtr extensions(GetExtensionDict(self, NULL)); | 2120 ScopedPyObjectPtr extensions(GetExtensionDict(self, NULL)); |
| 2150 if (extensions == NULL) { | 2121 if (extensions == NULL) { |
| 2151 return NULL; | 2122 return NULL; |
| 2152 } | 2123 } |
| 2153 // 'extension' reference later stolen by PyTuple_SET_ITEM. | 2124 // 'extension' reference later stolen by PyTuple_SET_ITEM. |
| 2154 PyObject* extension = PyObject_GetItem( | 2125 PyObject* extension = PyObject_GetItem( |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2194 } | 2165 } |
| 2195 | 2166 |
| 2196 static PyObject* DiscardUnknownFields(CMessage* self) { | 2167 static PyObject* DiscardUnknownFields(CMessage* self) { |
| 2197 AssureWritable(self); | 2168 AssureWritable(self); |
| 2198 self->message->DiscardUnknownFields(); | 2169 self->message->DiscardUnknownFields(); |
| 2199 Py_RETURN_NONE; | 2170 Py_RETURN_NONE; |
| 2200 } | 2171 } |
| 2201 | 2172 |
| 2202 PyObject* FindInitializationErrors(CMessage* self) { | 2173 PyObject* FindInitializationErrors(CMessage* self) { |
| 2203 Message* message = self->message; | 2174 Message* message = self->message; |
| 2204 std::vector<string> errors; | 2175 vector<string> errors; |
| 2205 message->FindInitializationErrors(&errors); | 2176 message->FindInitializationErrors(&errors); |
| 2206 | 2177 |
| 2207 PyObject* error_list = PyList_New(errors.size()); | 2178 PyObject* error_list = PyList_New(errors.size()); |
| 2208 if (error_list == NULL) { | 2179 if (error_list == NULL) { |
| 2209 return NULL; | 2180 return NULL; |
| 2210 } | 2181 } |
| 2211 for (size_t i = 0; i < errors.size(); ++i) { | 2182 for (size_t i = 0; i < errors.size(); ++i) { |
| 2212 const string& error = errors[i]; | 2183 const string& error = errors[i]; |
| 2213 PyObject* error_string = PyString_FromStringAndSize( | 2184 PyObject* error_string = PyString_FromStringAndSize( |
| 2214 error.c_str(), error.length()); | 2185 error.c_str(), error.length()); |
| (...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2331 PyExc_SystemError, "Getting a value from a field of unknown type %d", | 2302 PyExc_SystemError, "Getting a value from a field of unknown type %d", |
| 2332 field_descriptor->cpp_type()); | 2303 field_descriptor->cpp_type()); |
| 2333 } | 2304 } |
| 2334 | 2305 |
| 2335 return result; | 2306 return result; |
| 2336 } | 2307 } |
| 2337 | 2308 |
| 2338 PyObject* InternalGetSubMessage( | 2309 PyObject* InternalGetSubMessage( |
| 2339 CMessage* self, const FieldDescriptor* field_descriptor) { | 2310 CMessage* self, const FieldDescriptor* field_descriptor) { |
| 2340 const Reflection* reflection = self->message->GetReflection(); | 2311 const Reflection* reflection = self->message->GetReflection(); |
| 2341 PyMessageFactory* factory = GetFactoryForMessage(self); | 2312 PyDescriptorPool* pool = GetDescriptorPoolForMessage(self); |
| 2342 const Message& sub_message = reflection->GetMessage( | 2313 const Message& sub_message = reflection->GetMessage( |
| 2343 *self->message, field_descriptor, factory->message_factory); | 2314 *self->message, field_descriptor, pool->message_factory); |
| 2344 | 2315 |
| 2345 CMessageClass* message_class = message_factory::GetMessageClass( | 2316 CMessageClass* message_class = cdescriptor_pool::GetMessageClass( |
| 2346 factory, field_descriptor->message_type()); | 2317 pool, field_descriptor->message_type()); |
| 2347 if (message_class == NULL) { | 2318 if (message_class == NULL) { |
| 2348 return NULL; | 2319 return NULL; |
| 2349 } | 2320 } |
| 2350 | 2321 |
| 2351 CMessage* cmsg = cmessage::NewEmptyMessage(message_class); | 2322 CMessage* cmsg = cmessage::NewEmptyMessage(message_class); |
| 2352 if (cmsg == NULL) { | 2323 if (cmsg == NULL) { |
| 2353 return NULL; | 2324 return NULL; |
| 2354 } | 2325 } |
| 2355 | 2326 |
| 2356 cmsg->owner = self->owner; | 2327 cmsg->owner = self->owner; |
| (...skipping 229 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2586 } | 2557 } |
| 2587 self->extensions = extension_dict; | 2558 self->extensions = extension_dict; |
| 2588 Py_INCREF(self->extensions); | 2559 Py_INCREF(self->extensions); |
| 2589 return reinterpret_cast<PyObject*>(self->extensions); | 2560 return reinterpret_cast<PyObject*>(self->extensions); |
| 2590 } | 2561 } |
| 2591 | 2562 |
| 2592 PyErr_SetNone(PyExc_AttributeError); | 2563 PyErr_SetNone(PyExc_AttributeError); |
| 2593 return NULL; | 2564 return NULL; |
| 2594 } | 2565 } |
| 2595 | 2566 |
| 2596 static PyObject* GetExtensionsByName(CMessage *self, void *closure) { | |
| 2597 return message_meta::GetExtensionsByName( | |
| 2598 reinterpret_cast<CMessageClass*>(Py_TYPE(self)), closure); | |
| 2599 } | |
| 2600 | |
| 2601 static PyObject* GetExtensionsByNumber(CMessage *self, void *closure) { | |
| 2602 return message_meta::GetExtensionsByNumber( | |
| 2603 reinterpret_cast<CMessageClass*>(Py_TYPE(self)), closure); | |
| 2604 } | |
| 2605 | |
| 2606 static PyGetSetDef Getters[] = { | 2567 static PyGetSetDef Getters[] = { |
| 2607 {"Extensions", (getter)GetExtensionDict, NULL, "Extension dict"}, | 2568 {"Extensions", (getter)GetExtensionDict, NULL, "Extension dict"}, |
| 2608 {"_extensions_by_name", (getter)GetExtensionsByName, NULL}, | |
| 2609 {"_extensions_by_number", (getter)GetExtensionsByNumber, NULL}, | |
| 2610 {NULL} | 2569 {NULL} |
| 2611 }; | 2570 }; |
| 2612 | 2571 |
| 2613 | |
| 2614 static PyMethodDef Methods[] = { | 2572 static PyMethodDef Methods[] = { |
| 2615 { "__deepcopy__", (PyCFunction)DeepCopy, METH_VARARGS, | 2573 { "__deepcopy__", (PyCFunction)DeepCopy, METH_VARARGS, |
| 2616 "Makes a deep copy of the class." }, | 2574 "Makes a deep copy of the class." }, |
| 2617 { "__reduce__", (PyCFunction)Reduce, METH_NOARGS, | 2575 { "__reduce__", (PyCFunction)Reduce, METH_NOARGS, |
| 2618 "Outputs picklable representation of the message." }, | 2576 "Outputs picklable representation of the message." }, |
| 2619 { "__setstate__", (PyCFunction)SetState, METH_O, | 2577 { "__setstate__", (PyCFunction)SetState, METH_O, |
| 2620 "Inputs picklable representation of the message." }, | 2578 "Inputs picklable representation of the message." }, |
| 2621 { "__unicode__", (PyCFunction)ToUnicode, METH_NOARGS, | 2579 { "__unicode__", (PyCFunction)ToUnicode, METH_NOARGS, |
| 2622 "Outputs a unicode representation of the message." }, | 2580 "Outputs a unicode representation of the message." }, |
| 2623 { "ByteSize", (PyCFunction)ByteSize, METH_NOARGS, | 2581 { "ByteSize", (PyCFunction)ByteSize, METH_NOARGS, |
| (...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2694 if (field_descriptor == NULL) { | 2652 if (field_descriptor == NULL) { |
| 2695 return CMessage_Type.tp_base->tp_getattro( | 2653 return CMessage_Type.tp_base->tp_getattro( |
| 2696 reinterpret_cast<PyObject*>(self), name); | 2654 reinterpret_cast<PyObject*>(self), name); |
| 2697 } | 2655 } |
| 2698 | 2656 |
| 2699 if (field_descriptor->is_map()) { | 2657 if (field_descriptor->is_map()) { |
| 2700 PyObject* py_container = NULL; | 2658 PyObject* py_container = NULL; |
| 2701 const Descriptor* entry_type = field_descriptor->message_type(); | 2659 const Descriptor* entry_type = field_descriptor->message_type(); |
| 2702 const FieldDescriptor* value_type = entry_type->FindFieldByName("value"); | 2660 const FieldDescriptor* value_type = entry_type->FindFieldByName("value"); |
| 2703 if (value_type->cpp_type() == FieldDescriptor::CPPTYPE_MESSAGE) { | 2661 if (value_type->cpp_type() == FieldDescriptor::CPPTYPE_MESSAGE) { |
| 2704 CMessageClass* value_class = message_factory::GetMessageClass( | 2662 CMessageClass* value_class = cdescriptor_pool::GetMessageClass( |
| 2705 GetFactoryForMessage(self), value_type->message_type()); | 2663 GetDescriptorPoolForMessage(self), value_type->message_type()); |
| 2706 if (value_class == NULL) { | 2664 if (value_class == NULL) { |
| 2707 return NULL; | 2665 return NULL; |
| 2708 } | 2666 } |
| 2709 py_container = | 2667 py_container = |
| 2710 NewMessageMapContainer(self, field_descriptor, value_class); | 2668 NewMessageMapContainer(self, field_descriptor, value_class); |
| 2711 } else { | 2669 } else { |
| 2712 py_container = NewScalarMapContainer(self, field_descriptor); | 2670 py_container = NewScalarMapContainer(self, field_descriptor); |
| 2713 } | 2671 } |
| 2714 if (py_container == NULL) { | 2672 if (py_container == NULL) { |
| 2715 return NULL; | 2673 return NULL; |
| 2716 } | 2674 } |
| 2717 if (!SetCompositeField(self, name, py_container)) { | 2675 if (!SetCompositeField(self, name, py_container)) { |
| 2718 Py_DECREF(py_container); | 2676 Py_DECREF(py_container); |
| 2719 return NULL; | 2677 return NULL; |
| 2720 } | 2678 } |
| 2721 return py_container; | 2679 return py_container; |
| 2722 } | 2680 } |
| 2723 | 2681 |
| 2724 if (field_descriptor->label() == FieldDescriptor::LABEL_REPEATED) { | 2682 if (field_descriptor->label() == FieldDescriptor::LABEL_REPEATED) { |
| 2725 PyObject* py_container = NULL; | 2683 PyObject* py_container = NULL; |
| 2726 if (field_descriptor->cpp_type() == FieldDescriptor::CPPTYPE_MESSAGE) { | 2684 if (field_descriptor->cpp_type() == FieldDescriptor::CPPTYPE_MESSAGE) { |
| 2727 CMessageClass* message_class = message_factory::GetMessageClass( | 2685 CMessageClass* message_class = cdescriptor_pool::GetMessageClass( |
| 2728 GetFactoryForMessage(self), field_descriptor->message_type()); | 2686 GetDescriptorPoolForMessage(self), field_descriptor->message_type()); |
| 2729 if (message_class == NULL) { | 2687 if (message_class == NULL) { |
| 2730 return NULL; | 2688 return NULL; |
| 2731 } | 2689 } |
| 2732 py_container = repeated_composite_container::NewContainer( | 2690 py_container = repeated_composite_container::NewContainer( |
| 2733 self, field_descriptor, message_class); | 2691 self, field_descriptor, message_class); |
| 2734 } else { | 2692 } else { |
| 2735 py_container = repeated_scalar_container::NewContainer( | 2693 py_container = repeated_scalar_container::NewContainer( |
| 2736 self, field_descriptor); | 2694 self, field_descriptor); |
| 2737 } | 2695 } |
| 2738 if (py_container == NULL) { | 2696 if (py_container == NULL) { |
| (...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2813 0, // tp_call | 2771 0, // tp_call |
| 2814 (reprfunc)cmessage::ToStr, // tp_str | 2772 (reprfunc)cmessage::ToStr, // tp_str |
| 2815 (getattrofunc)cmessage::GetAttr, // tp_getattro | 2773 (getattrofunc)cmessage::GetAttr, // tp_getattro |
| 2816 (setattrofunc)cmessage::SetAttr, // tp_setattro | 2774 (setattrofunc)cmessage::SetAttr, // tp_setattro |
| 2817 0, // tp_as_buffer | 2775 0, // tp_as_buffer |
| 2818 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, // tp_flags | 2776 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, // tp_flags |
| 2819 "A ProtocolMessage", // tp_doc | 2777 "A ProtocolMessage", // tp_doc |
| 2820 0, // tp_traverse | 2778 0, // tp_traverse |
| 2821 0, // tp_clear | 2779 0, // tp_clear |
| 2822 (richcmpfunc)cmessage::RichCompare, // tp_richcompare | 2780 (richcmpfunc)cmessage::RichCompare, // tp_richcompare |
| 2823 offsetof(CMessage, weakreflist), // tp_weaklistoffset | 2781 0, // tp_weaklistoffset |
| 2824 0, // tp_iter | 2782 0, // tp_iter |
| 2825 0, // tp_iternext | 2783 0, // tp_iternext |
| 2826 cmessage::Methods, // tp_methods | 2784 cmessage::Methods, // tp_methods |
| 2827 0, // tp_members | 2785 0, // tp_members |
| 2828 cmessage::Getters, // tp_getset | 2786 cmessage::Getters, // tp_getset |
| 2829 0, // tp_base | 2787 0, // tp_base |
| 2830 0, // tp_dict | 2788 0, // tp_dict |
| 2831 0, // tp_descr_get | 2789 0, // tp_descr_get |
| 2832 0, // tp_descr_set | 2790 0, // tp_descr_set |
| 2833 0, // tp_dictoffset | 2791 0, // tp_dictoffset |
| (...skipping 26 matching lines...) Expand all Loading... |
| 2860 // There is currently no way of accurately syncing arbitrary changes to | 2818 // There is currently no way of accurately syncing arbitrary changes to |
| 2861 // the underlying C++ message back to the CMessage (e.g. removed repeated | 2819 // the underlying C++ message back to the CMessage (e.g. removed repeated |
| 2862 // composite containers). We only allow direct mutation of the underlying | 2820 // composite containers). We only allow direct mutation of the underlying |
| 2863 // C++ message if there is no child data in the CMessage. | 2821 // C++ message if there is no child data in the CMessage. |
| 2864 return NULL; | 2822 return NULL; |
| 2865 } | 2823 } |
| 2866 cmessage::AssureWritable(cmsg); | 2824 cmessage::AssureWritable(cmsg); |
| 2867 return cmsg->message; | 2825 return cmsg->message; |
| 2868 } | 2826 } |
| 2869 | 2827 |
| 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 |
| 2870 void InitGlobals() { | 2835 void InitGlobals() { |
| 2871 // TODO(gps): Check all return values in this function for NULL and propagate | 2836 // TODO(gps): Check all return values in this function for NULL and propagate |
| 2872 // the error (MemoryError) on up to result in an import failure. These should | 2837 // the error (MemoryError) on up to result in an import failure. These should |
| 2873 // also be freed and reset to NULL during finalization. | 2838 // also be freed and reset to NULL during finalization. |
| 2839 kPythonZero = PyInt_FromLong(0); |
| 2840 kint32min_py = PyInt_FromLong(kint32min); |
| 2841 kint32max_py = PyInt_FromLong(kint32max); |
| 2842 kuint32max_py = PyLong_FromLongLong(kuint32max); |
| 2843 kint64min_py = PyLong_FromLongLong(kint64min); |
| 2844 kint64max_py = PyLong_FromLongLong(kint64max); |
| 2845 kuint64max_py = PyLong_FromUnsignedLongLong(kuint64max); |
| 2846 |
| 2874 kDESCRIPTOR = PyString_FromString("DESCRIPTOR"); | 2847 kDESCRIPTOR = PyString_FromString("DESCRIPTOR"); |
| 2848 k_cdescriptor = PyString_FromString("_cdescriptor"); |
| 2849 kfull_name = PyString_FromString("full_name"); |
| 2850 k_extensions_by_name = PyString_FromString("_extensions_by_name"); |
| 2851 k_extensions_by_number = PyString_FromString("_extensions_by_number"); |
| 2875 | 2852 |
| 2876 PyObject *dummy_obj = PySet_New(NULL); | 2853 PyObject *dummy_obj = PySet_New(NULL); |
| 2877 kEmptyWeakref = PyWeakref_NewRef(dummy_obj, NULL); | 2854 kEmptyWeakref = PyWeakref_NewRef(dummy_obj, NULL); |
| 2878 Py_DECREF(dummy_obj); | 2855 Py_DECREF(dummy_obj); |
| 2879 } | 2856 } |
| 2880 | 2857 |
| 2881 bool InitProto2MessageModule(PyObject *m) { | 2858 bool InitProto2MessageModule(PyObject *m) { |
| 2882 // Initialize types and globals in descriptor.cc | 2859 // Initialize types and globals in descriptor.cc |
| 2883 if (!InitDescriptor()) { | 2860 if (!InitDescriptor()) { |
| 2884 return false; | 2861 return false; |
| 2885 } | 2862 } |
| 2886 | 2863 |
| 2887 // Initialize types and globals in descriptor_pool.cc | 2864 // Initialize types and globals in descriptor_pool.cc |
| 2888 if (!InitDescriptorPool()) { | 2865 if (!InitDescriptorPool()) { |
| 2889 return false; | 2866 return false; |
| 2890 } | 2867 } |
| 2891 | 2868 |
| 2892 // Initialize types and globals in message_factory.cc | |
| 2893 if (!InitMessageFactory()) { | |
| 2894 return false; | |
| 2895 } | |
| 2896 | |
| 2897 // Initialize constants defined in this file. | 2869 // Initialize constants defined in this file. |
| 2898 InitGlobals(); | 2870 InitGlobals(); |
| 2899 | 2871 |
| 2900 CMessageClass_Type.tp_base = &PyType_Type; | 2872 CMessageClass_Type.tp_base = &PyType_Type; |
| 2901 if (PyType_Ready(&CMessageClass_Type) < 0) { | 2873 if (PyType_Ready(&CMessageClass_Type) < 0) { |
| 2902 return false; | 2874 return false; |
| 2903 } | 2875 } |
| 2904 PyModule_AddObject(m, "MessageMeta", | 2876 PyModule_AddObject(m, "MessageMeta", |
| 2905 reinterpret_cast<PyObject*>(&CMessageClass_Type)); | 2877 reinterpret_cast<PyObject*>(&CMessageClass_Type)); |
| 2906 | 2878 |
| 2907 if (PyType_Ready(&CMessage_Type) < 0) { | 2879 if (PyType_Ready(&CMessage_Type) < 0) { |
| 2908 return false; | 2880 return false; |
| 2909 } | 2881 } |
| 2910 | 2882 |
| 2911 // DESCRIPTOR is set on each protocol buffer message class elsewhere, but set | 2883 // DESCRIPTOR is set on each protocol buffer message class elsewhere, but set |
| 2912 // it here as well to document that subclasses need to set it. | 2884 // it here as well to document that subclasses need to set it. |
| 2913 PyDict_SetItem(CMessage_Type.tp_dict, kDESCRIPTOR, Py_None); | 2885 PyDict_SetItem(CMessage_Type.tp_dict, kDESCRIPTOR, Py_None); |
| 2886 // Subclasses with message extensions will override _extensions_by_name and |
| 2887 // _extensions_by_number with fresh mutable dictionaries in AddDescriptors. |
| 2888 // All other classes can share this same immutable mapping. |
| 2889 ScopedPyObjectPtr empty_dict(PyDict_New()); |
| 2890 if (empty_dict == NULL) { |
| 2891 return false; |
| 2892 } |
| 2893 ScopedPyObjectPtr immutable_dict(PyDictProxy_New(empty_dict.get())); |
| 2894 if (immutable_dict == NULL) { |
| 2895 return false; |
| 2896 } |
| 2897 if (PyDict_SetItem(CMessage_Type.tp_dict, |
| 2898 k_extensions_by_name, immutable_dict.get()) < 0) { |
| 2899 return false; |
| 2900 } |
| 2901 if (PyDict_SetItem(CMessage_Type.tp_dict, |
| 2902 k_extensions_by_number, immutable_dict.get()) < 0) { |
| 2903 return false; |
| 2904 } |
| 2914 | 2905 |
| 2915 PyModule_AddObject(m, "Message", reinterpret_cast<PyObject*>(&CMessage_Type)); | 2906 PyModule_AddObject(m, "Message", reinterpret_cast<PyObject*>(&CMessage_Type)); |
| 2916 | 2907 |
| 2917 // Initialize Repeated container types. | 2908 // Initialize Repeated container types. |
| 2918 { | 2909 { |
| 2919 if (PyType_Ready(&RepeatedScalarContainer_Type) < 0) { | 2910 if (PyType_Ready(&RepeatedScalarContainer_Type) < 0) { |
| 2920 return false; | 2911 return false; |
| 2921 } | 2912 } |
| 2922 | 2913 |
| 2923 PyModule_AddObject(m, "RepeatedScalarContainer", | 2914 PyModule_AddObject(m, "RepeatedScalarContainer", |
| (...skipping 25 matching lines...) Expand all Loading... |
| 2949 return false; | 2940 return false; |
| 2950 } | 2941 } |
| 2951 if (ScopedPyObjectPtr( | 2942 if (ScopedPyObjectPtr( |
| 2952 PyObject_CallMethod(mutable_sequence.get(), "register", "O", | 2943 PyObject_CallMethod(mutable_sequence.get(), "register", "O", |
| 2953 &RepeatedCompositeContainer_Type)) == NULL) { | 2944 &RepeatedCompositeContainer_Type)) == NULL) { |
| 2954 return false; | 2945 return false; |
| 2955 } | 2946 } |
| 2956 } | 2947 } |
| 2957 | 2948 |
| 2958 // Initialize Map container types. | 2949 // Initialize Map container types. |
| 2959 if (!InitMapContainers()) { | 2950 { |
| 2960 return false; | 2951 // ScalarMapContainer_Type derives from our MutableMapping type. |
| 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 |
| 2961 } | 3012 } |
| 2962 PyModule_AddObject(m, "ScalarMapContainer", | |
| 2963 reinterpret_cast<PyObject*>(ScalarMapContainer_Type)); | |
| 2964 PyModule_AddObject(m, "MessageMapContainer", | |
| 2965 reinterpret_cast<PyObject*>(MessageMapContainer_Type)); | |
| 2966 PyModule_AddObject(m, "MapIterator", | |
| 2967 reinterpret_cast<PyObject*>(&MapIterator_Type)); | |
| 2968 | 3013 |
| 2969 if (PyType_Ready(&ExtensionDict_Type) < 0) { | 3014 if (PyType_Ready(&ExtensionDict_Type) < 0) { |
| 2970 return false; | 3015 return false; |
| 2971 } | 3016 } |
| 2972 PyModule_AddObject( | 3017 PyModule_AddObject( |
| 2973 m, "ExtensionDict", | 3018 m, "ExtensionDict", |
| 2974 reinterpret_cast<PyObject*>(&ExtensionDict_Type)); | 3019 reinterpret_cast<PyObject*>(&ExtensionDict_Type)); |
| 2975 | 3020 |
| 2976 // Expose the DescriptorPool used to hold all descriptors added from generated | 3021 // Expose the DescriptorPool used to hold all descriptors added from generated |
| 2977 // pb2.py files. | 3022 // pb2.py files. |
| (...skipping 14 matching lines...) Expand all Loading... |
| 2992 PyModule_AddObject(m, "FieldDescriptor", reinterpret_cast<PyObject*>( | 3037 PyModule_AddObject(m, "FieldDescriptor", reinterpret_cast<PyObject*>( |
| 2993 &PyFieldDescriptor_Type)); | 3038 &PyFieldDescriptor_Type)); |
| 2994 PyModule_AddObject(m, "EnumDescriptor", reinterpret_cast<PyObject*>( | 3039 PyModule_AddObject(m, "EnumDescriptor", reinterpret_cast<PyObject*>( |
| 2995 &PyEnumDescriptor_Type)); | 3040 &PyEnumDescriptor_Type)); |
| 2996 PyModule_AddObject(m, "EnumValueDescriptor", reinterpret_cast<PyObject*>( | 3041 PyModule_AddObject(m, "EnumValueDescriptor", reinterpret_cast<PyObject*>( |
| 2997 &PyEnumValueDescriptor_Type)); | 3042 &PyEnumValueDescriptor_Type)); |
| 2998 PyModule_AddObject(m, "FileDescriptor", reinterpret_cast<PyObject*>( | 3043 PyModule_AddObject(m, "FileDescriptor", reinterpret_cast<PyObject*>( |
| 2999 &PyFileDescriptor_Type)); | 3044 &PyFileDescriptor_Type)); |
| 3000 PyModule_AddObject(m, "OneofDescriptor", reinterpret_cast<PyObject*>( | 3045 PyModule_AddObject(m, "OneofDescriptor", reinterpret_cast<PyObject*>( |
| 3001 &PyOneofDescriptor_Type)); | 3046 &PyOneofDescriptor_Type)); |
| 3002 PyModule_AddObject(m, "ServiceDescriptor", reinterpret_cast<PyObject*>( | |
| 3003 &PyServiceDescriptor_Type)); | |
| 3004 PyModule_AddObject(m, "MethodDescriptor", reinterpret_cast<PyObject*>( | |
| 3005 &PyMethodDescriptor_Type)); | |
| 3006 | 3047 |
| 3007 PyObject* enum_type_wrapper = PyImport_ImportModule( | 3048 PyObject* enum_type_wrapper = PyImport_ImportModule( |
| 3008 "google.protobuf.internal.enum_type_wrapper"); | 3049 "google.protobuf.internal.enum_type_wrapper"); |
| 3009 if (enum_type_wrapper == NULL) { | 3050 if (enum_type_wrapper == NULL) { |
| 3010 return false; | 3051 return false; |
| 3011 } | 3052 } |
| 3012 EnumTypeWrapper_class = | 3053 EnumTypeWrapper_class = |
| 3013 PyObject_GetAttrString(enum_type_wrapper, "EnumTypeWrapper"); | 3054 PyObject_GetAttrString(enum_type_wrapper, "EnumTypeWrapper"); |
| 3014 Py_DECREF(enum_type_wrapper); | 3055 Py_DECREF(enum_type_wrapper); |
| 3015 | 3056 |
| (...skipping 17 matching lines...) Expand all Loading... |
| 3033 // Override {Get,Mutable}CProtoInsidePyProto. | 3074 // Override {Get,Mutable}CProtoInsidePyProto. |
| 3034 GetCProtoInsidePyProtoPtr = GetCProtoInsidePyProtoImpl; | 3075 GetCProtoInsidePyProtoPtr = GetCProtoInsidePyProtoImpl; |
| 3035 MutableCProtoInsidePyProtoPtr = MutableCProtoInsidePyProtoImpl; | 3076 MutableCProtoInsidePyProtoPtr = MutableCProtoInsidePyProtoImpl; |
| 3036 | 3077 |
| 3037 return true; | 3078 return true; |
| 3038 } | 3079 } |
| 3039 | 3080 |
| 3040 } // namespace python | 3081 } // namespace python |
| 3041 } // namespace protobuf | 3082 } // namespace protobuf |
| 3042 | 3083 |
| 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 } |
| 3043 } // namespace google | 3133 } // namespace google |
| OLD | NEW |