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