| 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 590 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 601 if (index < 0) { | 601 if (index < 0) { |
| 602 index += Length(self); | 602 index += Length(self); |
| 603 } | 603 } |
| 604 if (index < 0 || index >= Length(self)) { | 604 if (index < 0 || index >= Length(self)) { |
| 605 PyErr_SetString(PyExc_IndexError, "index out of range"); | 605 PyErr_SetString(PyExc_IndexError, "index out of range"); |
| 606 return NULL; | 606 return NULL; |
| 607 } | 607 } |
| 608 return _NewObj_ByIndex(self, index); | 608 return _NewObj_ByIndex(self, index); |
| 609 } | 609 } |
| 610 | 610 |
| 611 static PyObject * |
| 612 SeqSubscript(PyContainer* self, PyObject* item) { |
| 613 if (PyIndex_Check(item)) { |
| 614 Py_ssize_t index; |
| 615 index = PyNumber_AsSsize_t(item, PyExc_IndexError); |
| 616 if (index == -1 && PyErr_Occurred()) |
| 617 return NULL; |
| 618 return GetItem(self, index); |
| 619 } |
| 620 // Materialize the list and delegate the operation to it. |
| 621 ScopedPyObjectPtr list(PyObject_CallFunctionObjArgs( |
| 622 reinterpret_cast<PyObject*>(&PyList_Type), self, NULL)); |
| 623 if (list == NULL) { |
| 624 return NULL; |
| 625 } |
| 626 return Py_TYPE(list.get())->tp_as_mapping->mp_subscript(list.get(), item); |
| 627 } |
| 628 |
| 611 // Returns the position of the item in the sequence, of -1 if not found. | 629 // Returns the position of the item in the sequence, of -1 if not found. |
| 612 // This function never fails. | 630 // This function never fails. |
| 613 int Find(PyContainer* self, PyObject* item) { | 631 int Find(PyContainer* self, PyObject* item) { |
| 614 // The item can only be in one position: item.index. | 632 // The item can only be in one position: item.index. |
| 615 // Check that self[item.index] == item, it's faster than a linear search. | 633 // Check that self[item.index] == item, it's faster than a linear search. |
| 616 // | 634 // |
| 617 // This assumes that sequences are only defined by syntax of the .proto file: | 635 // This assumes that sequences are only defined by syntax of the .proto file: |
| 618 // a specific item belongs to only one sequence, depending on its position in | 636 // a specific item belongs to only one sequence, depending on its position in |
| 619 // the .proto file definition. | 637 // the .proto file definition. |
| 620 const void* descriptor_ptr = PyDescriptor_AsVoidPtr(item); | 638 const void* descriptor_ptr = PyDescriptor_AsVoidPtr(item); |
| (...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 696 | 714 |
| 697 static PyMethodDef SeqMethods[] = { | 715 static PyMethodDef SeqMethods[] = { |
| 698 { "index", (PyCFunction)Index, METH_O, }, | 716 { "index", (PyCFunction)Index, METH_O, }, |
| 699 { "count", (PyCFunction)Count, METH_O, }, | 717 { "count", (PyCFunction)Count, METH_O, }, |
| 700 { "append", (PyCFunction)Append, METH_O, }, | 718 { "append", (PyCFunction)Append, METH_O, }, |
| 701 { "__reversed__", (PyCFunction)Reversed, METH_NOARGS, }, | 719 { "__reversed__", (PyCFunction)Reversed, METH_NOARGS, }, |
| 702 {NULL} | 720 {NULL} |
| 703 }; | 721 }; |
| 704 | 722 |
| 705 static PySequenceMethods SeqSequenceMethods = { | 723 static PySequenceMethods SeqSequenceMethods = { |
| 706 (lenfunc)Length, // sq_length | 724 (lenfunc)Length, // sq_length |
| 707 0, // sq_concat | 725 0, // sq_concat |
| 708 0, // sq_repeat | 726 0, // sq_repeat |
| 709 (ssizeargfunc)GetItem, // sq_item | 727 (ssizeargfunc)GetItem, // sq_item |
| 710 0, // sq_slice | 728 0, // sq_slice |
| 711 0, // sq_ass_item | 729 0, // sq_ass_item |
| 712 0, // sq_ass_slice | 730 0, // sq_ass_slice |
| 713 (objobjproc)SeqContains, // sq_contains | 731 (objobjproc)SeqContains, // sq_contains |
| 732 }; |
| 733 |
| 734 static PyMappingMethods SeqMappingMethods = { |
| 735 (lenfunc)Length, // mp_length |
| 736 (binaryfunc)SeqSubscript, // mp_subscript |
| 737 0, // mp_ass_subscript |
| 714 }; | 738 }; |
| 715 | 739 |
| 716 PyTypeObject DescriptorSequence_Type = { | 740 PyTypeObject DescriptorSequence_Type = { |
| 717 PyVarObject_HEAD_INIT(&PyType_Type, 0) | 741 PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 718 "DescriptorSequence", // tp_name | 742 "DescriptorSequence", // tp_name |
| 719 sizeof(PyContainer), // tp_basicsize | 743 sizeof(PyContainer), // tp_basicsize |
| 720 0, // tp_itemsize | 744 0, // tp_itemsize |
| 721 0, // tp_dealloc | 745 0, // tp_dealloc |
| 722 0, // tp_print | 746 0, // tp_print |
| 723 0, // tp_getattr | 747 0, // tp_getattr |
| 724 0, // tp_setattr | 748 0, // tp_setattr |
| 725 0, // tp_compare | 749 0, // tp_compare |
| 726 (reprfunc)ContainerRepr, // tp_repr | 750 (reprfunc)ContainerRepr, // tp_repr |
| 727 0, // tp_as_number | 751 0, // tp_as_number |
| 728 &SeqSequenceMethods, // tp_as_sequence | 752 &SeqSequenceMethods, // tp_as_sequence |
| 729 0, // tp_as_mapping | 753 &SeqMappingMethods, // tp_as_mapping |
| 730 0, // tp_hash | 754 0, // tp_hash |
| 731 0, // tp_call | 755 0, // tp_call |
| 732 0, // tp_str | 756 0, // tp_str |
| 733 0, // tp_getattro | 757 0, // tp_getattro |
| 734 0, // tp_setattro | 758 0, // tp_setattro |
| 735 0, // tp_as_buffer | 759 0, // tp_as_buffer |
| 736 Py_TPFLAGS_DEFAULT, // tp_flags | 760 Py_TPFLAGS_DEFAULT, // tp_flags |
| 737 0, // tp_doc | 761 0, // tp_doc |
| 738 0, // tp_traverse | 762 0, // tp_traverse |
| 739 0, // tp_clear | 763 0, // tp_clear |
| (...skipping 660 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1400 }; | 1424 }; |
| 1401 | 1425 |
| 1402 } // namespace fields | 1426 } // namespace fields |
| 1403 | 1427 |
| 1404 PyObject* NewOneofFieldsSeq(ParentDescriptor descriptor) { | 1428 PyObject* NewOneofFieldsSeq(ParentDescriptor descriptor) { |
| 1405 return descriptor::NewSequence(&fields::ContainerDef, descriptor); | 1429 return descriptor::NewSequence(&fields::ContainerDef, descriptor); |
| 1406 } | 1430 } |
| 1407 | 1431 |
| 1408 } // namespace oneof_descriptor | 1432 } // namespace oneof_descriptor |
| 1409 | 1433 |
| 1434 namespace service_descriptor { |
| 1435 |
| 1436 typedef const ServiceDescriptor* ParentDescriptor; |
| 1437 |
| 1438 static ParentDescriptor GetDescriptor(PyContainer* self) { |
| 1439 return reinterpret_cast<ParentDescriptor>(self->descriptor); |
| 1440 } |
| 1441 |
| 1442 namespace methods { |
| 1443 |
| 1444 typedef const MethodDescriptor* ItemDescriptor; |
| 1445 |
| 1446 static int Count(PyContainer* self) { |
| 1447 return GetDescriptor(self)->method_count(); |
| 1448 } |
| 1449 |
| 1450 static ItemDescriptor GetByName(PyContainer* self, const string& name) { |
| 1451 return GetDescriptor(self)->FindMethodByName(name); |
| 1452 } |
| 1453 |
| 1454 static ItemDescriptor GetByIndex(PyContainer* self, int index) { |
| 1455 return GetDescriptor(self)->method(index); |
| 1456 } |
| 1457 |
| 1458 static PyObject* NewObjectFromItem(ItemDescriptor item) { |
| 1459 return PyMethodDescriptor_FromDescriptor(item); |
| 1460 } |
| 1461 |
| 1462 static const string& GetItemName(ItemDescriptor item) { |
| 1463 return item->name(); |
| 1464 } |
| 1465 |
| 1466 static int GetItemIndex(ItemDescriptor item) { |
| 1467 return item->index(); |
| 1468 } |
| 1469 |
| 1470 static DescriptorContainerDef ContainerDef = { |
| 1471 "ServiceMethods", |
| 1472 (CountMethod)Count, |
| 1473 (GetByIndexMethod)GetByIndex, |
| 1474 (GetByNameMethod)GetByName, |
| 1475 (GetByCamelcaseNameMethod)NULL, |
| 1476 (GetByNumberMethod)NULL, |
| 1477 (NewObjectFromItemMethod)NewObjectFromItem, |
| 1478 (GetItemNameMethod)GetItemName, |
| 1479 (GetItemCamelcaseNameMethod)NULL, |
| 1480 (GetItemNumberMethod)NULL, |
| 1481 (GetItemIndexMethod)GetItemIndex, |
| 1482 }; |
| 1483 |
| 1484 } // namespace methods |
| 1485 |
| 1486 PyObject* NewServiceMethodsSeq(ParentDescriptor descriptor) { |
| 1487 return descriptor::NewSequence(&methods::ContainerDef, descriptor); |
| 1488 } |
| 1489 |
| 1490 PyObject* NewServiceMethodsByName(ParentDescriptor descriptor) { |
| 1491 return descriptor::NewMappingByName(&methods::ContainerDef, descriptor); |
| 1492 } |
| 1493 |
| 1494 } // namespace service_descriptor |
| 1495 |
| 1410 namespace file_descriptor { | 1496 namespace file_descriptor { |
| 1411 | 1497 |
| 1412 typedef const FileDescriptor* ParentDescriptor; | 1498 typedef const FileDescriptor* ParentDescriptor; |
| 1413 | 1499 |
| 1414 static ParentDescriptor GetDescriptor(PyContainer* self) { | 1500 static ParentDescriptor GetDescriptor(PyContainer* self) { |
| 1415 return reinterpret_cast<ParentDescriptor>(self->descriptor); | 1501 return reinterpret_cast<ParentDescriptor>(self->descriptor); |
| 1416 } | 1502 } |
| 1417 | 1503 |
| 1418 namespace messages { | 1504 namespace messages { |
| 1419 | 1505 |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1452 (GetByNumberMethod)NULL, | 1538 (GetByNumberMethod)NULL, |
| 1453 (NewObjectFromItemMethod)NewObjectFromItem, | 1539 (NewObjectFromItemMethod)NewObjectFromItem, |
| 1454 (GetItemNameMethod)GetItemName, | 1540 (GetItemNameMethod)GetItemName, |
| 1455 (GetItemCamelcaseNameMethod)NULL, | 1541 (GetItemCamelcaseNameMethod)NULL, |
| 1456 (GetItemNumberMethod)NULL, | 1542 (GetItemNumberMethod)NULL, |
| 1457 (GetItemIndexMethod)GetItemIndex, | 1543 (GetItemIndexMethod)GetItemIndex, |
| 1458 }; | 1544 }; |
| 1459 | 1545 |
| 1460 } // namespace messages | 1546 } // namespace messages |
| 1461 | 1547 |
| 1462 PyObject* NewFileMessageTypesByName(const FileDescriptor* descriptor) { | 1548 PyObject* NewFileMessageTypesByName(ParentDescriptor descriptor) { |
| 1463 return descriptor::NewMappingByName(&messages::ContainerDef, descriptor); | 1549 return descriptor::NewMappingByName(&messages::ContainerDef, descriptor); |
| 1464 } | 1550 } |
| 1465 | 1551 |
| 1466 namespace enums { | 1552 namespace enums { |
| 1467 | 1553 |
| 1468 typedef const EnumDescriptor* ItemDescriptor; | 1554 typedef const EnumDescriptor* ItemDescriptor; |
| 1469 | 1555 |
| 1470 static int Count(PyContainer* self) { | 1556 static int Count(PyContainer* self) { |
| 1471 return GetDescriptor(self)->enum_type_count(); | 1557 return GetDescriptor(self)->enum_type_count(); |
| 1472 } | 1558 } |
| (...skipping 27 matching lines...) Expand all Loading... |
| 1500 (GetByNumberMethod)NULL, | 1586 (GetByNumberMethod)NULL, |
| 1501 (NewObjectFromItemMethod)NewObjectFromItem, | 1587 (NewObjectFromItemMethod)NewObjectFromItem, |
| 1502 (GetItemNameMethod)GetItemName, | 1588 (GetItemNameMethod)GetItemName, |
| 1503 (GetItemCamelcaseNameMethod)NULL, | 1589 (GetItemCamelcaseNameMethod)NULL, |
| 1504 (GetItemNumberMethod)NULL, | 1590 (GetItemNumberMethod)NULL, |
| 1505 (GetItemIndexMethod)GetItemIndex, | 1591 (GetItemIndexMethod)GetItemIndex, |
| 1506 }; | 1592 }; |
| 1507 | 1593 |
| 1508 } // namespace enums | 1594 } // namespace enums |
| 1509 | 1595 |
| 1510 PyObject* NewFileEnumTypesByName(const FileDescriptor* descriptor) { | 1596 PyObject* NewFileEnumTypesByName(ParentDescriptor descriptor) { |
| 1511 return descriptor::NewMappingByName(&enums::ContainerDef, descriptor); | 1597 return descriptor::NewMappingByName(&enums::ContainerDef, descriptor); |
| 1512 } | 1598 } |
| 1513 | 1599 |
| 1514 namespace extensions { | 1600 namespace extensions { |
| 1515 | 1601 |
| 1516 typedef const FieldDescriptor* ItemDescriptor; | 1602 typedef const FieldDescriptor* ItemDescriptor; |
| 1517 | 1603 |
| 1518 static int Count(PyContainer* self) { | 1604 static int Count(PyContainer* self) { |
| 1519 return GetDescriptor(self)->extension_count(); | 1605 return GetDescriptor(self)->extension_count(); |
| 1520 } | 1606 } |
| (...skipping 27 matching lines...) Expand all Loading... |
| 1548 (GetByNumberMethod)NULL, | 1634 (GetByNumberMethod)NULL, |
| 1549 (NewObjectFromItemMethod)NewObjectFromItem, | 1635 (NewObjectFromItemMethod)NewObjectFromItem, |
| 1550 (GetItemNameMethod)GetItemName, | 1636 (GetItemNameMethod)GetItemName, |
| 1551 (GetItemCamelcaseNameMethod)NULL, | 1637 (GetItemCamelcaseNameMethod)NULL, |
| 1552 (GetItemNumberMethod)NULL, | 1638 (GetItemNumberMethod)NULL, |
| 1553 (GetItemIndexMethod)GetItemIndex, | 1639 (GetItemIndexMethod)GetItemIndex, |
| 1554 }; | 1640 }; |
| 1555 | 1641 |
| 1556 } // namespace extensions | 1642 } // namespace extensions |
| 1557 | 1643 |
| 1558 PyObject* NewFileExtensionsByName(const FileDescriptor* descriptor) { | 1644 PyObject* NewFileExtensionsByName(ParentDescriptor descriptor) { |
| 1559 return descriptor::NewMappingByName(&extensions::ContainerDef, descriptor); | 1645 return descriptor::NewMappingByName(&extensions::ContainerDef, descriptor); |
| 1560 } | 1646 } |
| 1561 | 1647 |
| 1648 namespace services { |
| 1649 |
| 1650 typedef const ServiceDescriptor* ItemDescriptor; |
| 1651 |
| 1652 static int Count(PyContainer* self) { |
| 1653 return GetDescriptor(self)->service_count(); |
| 1654 } |
| 1655 |
| 1656 static ItemDescriptor GetByName(PyContainer* self, const string& name) { |
| 1657 return GetDescriptor(self)->FindServiceByName(name); |
| 1658 } |
| 1659 |
| 1660 static ItemDescriptor GetByIndex(PyContainer* self, int index) { |
| 1661 return GetDescriptor(self)->service(index); |
| 1662 } |
| 1663 |
| 1664 static PyObject* NewObjectFromItem(ItemDescriptor item) { |
| 1665 return PyServiceDescriptor_FromDescriptor(item); |
| 1666 } |
| 1667 |
| 1668 static const string& GetItemName(ItemDescriptor item) { |
| 1669 return item->name(); |
| 1670 } |
| 1671 |
| 1672 static int GetItemIndex(ItemDescriptor item) { |
| 1673 return item->index(); |
| 1674 } |
| 1675 |
| 1676 static DescriptorContainerDef ContainerDef = { |
| 1677 "FileServices", |
| 1678 (CountMethod)Count, |
| 1679 (GetByIndexMethod)GetByIndex, |
| 1680 (GetByNameMethod)GetByName, |
| 1681 (GetByCamelcaseNameMethod)NULL, |
| 1682 (GetByNumberMethod)NULL, |
| 1683 (NewObjectFromItemMethod)NewObjectFromItem, |
| 1684 (GetItemNameMethod)GetItemName, |
| 1685 (GetItemCamelcaseNameMethod)NULL, |
| 1686 (GetItemNumberMethod)NULL, |
| 1687 (GetItemIndexMethod)GetItemIndex, |
| 1688 }; |
| 1689 |
| 1690 } // namespace services |
| 1691 |
| 1692 PyObject* NewFileServicesByName(const FileDescriptor* descriptor) { |
| 1693 return descriptor::NewMappingByName(&services::ContainerDef, descriptor); |
| 1694 } |
| 1695 |
| 1562 namespace dependencies { | 1696 namespace dependencies { |
| 1563 | 1697 |
| 1564 typedef const FileDescriptor* ItemDescriptor; | 1698 typedef const FileDescriptor* ItemDescriptor; |
| 1565 | 1699 |
| 1566 static int Count(PyContainer* self) { | 1700 static int Count(PyContainer* self) { |
| 1567 return GetDescriptor(self)->dependency_count(); | 1701 return GetDescriptor(self)->dependency_count(); |
| 1568 } | 1702 } |
| 1569 | 1703 |
| 1570 static ItemDescriptor GetByIndex(PyContainer* self, int index) { | 1704 static ItemDescriptor GetByIndex(PyContainer* self, int index) { |
| 1571 return GetDescriptor(self)->dependency(index); | 1705 return GetDescriptor(self)->dependency(index); |
| (...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1643 if (PyType_Ready(&descriptor::DescriptorSequence_Type) < 0) | 1777 if (PyType_Ready(&descriptor::DescriptorSequence_Type) < 0) |
| 1644 return false; | 1778 return false; |
| 1645 if (PyType_Ready(&descriptor::ContainerIterator_Type) < 0) | 1779 if (PyType_Ready(&descriptor::ContainerIterator_Type) < 0) |
| 1646 return false; | 1780 return false; |
| 1647 return true; | 1781 return true; |
| 1648 } | 1782 } |
| 1649 | 1783 |
| 1650 } // namespace python | 1784 } // namespace python |
| 1651 } // namespace protobuf | 1785 } // namespace protobuf |
| 1652 } // namespace google | 1786 } // namespace google |
| OLD | NEW |