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 | |
629 // Returns the position of the item in the sequence, of -1 if not found. | 611 // Returns the position of the item in the sequence, of -1 if not found. |
630 // This function never fails. | 612 // This function never fails. |
631 int Find(PyContainer* self, PyObject* item) { | 613 int Find(PyContainer* self, PyObject* item) { |
632 // The item can only be in one position: item.index. | 614 // The item can only be in one position: item.index. |
633 // Check that self[item.index] == item, it's faster than a linear search. | 615 // Check that self[item.index] == item, it's faster than a linear search. |
634 // | 616 // |
635 // This assumes that sequences are only defined by syntax of the .proto file: | 617 // This assumes that sequences are only defined by syntax of the .proto file: |
636 // a specific item belongs to only one sequence, depending on its position in | 618 // a specific item belongs to only one sequence, depending on its position in |
637 // the .proto file definition. | 619 // the .proto file definition. |
638 const void* descriptor_ptr = PyDescriptor_AsVoidPtr(item); | 620 const void* descriptor_ptr = PyDescriptor_AsVoidPtr(item); |
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
714 | 696 |
715 static PyMethodDef SeqMethods[] = { | 697 static PyMethodDef SeqMethods[] = { |
716 { "index", (PyCFunction)Index, METH_O, }, | 698 { "index", (PyCFunction)Index, METH_O, }, |
717 { "count", (PyCFunction)Count, METH_O, }, | 699 { "count", (PyCFunction)Count, METH_O, }, |
718 { "append", (PyCFunction)Append, METH_O, }, | 700 { "append", (PyCFunction)Append, METH_O, }, |
719 { "__reversed__", (PyCFunction)Reversed, METH_NOARGS, }, | 701 { "__reversed__", (PyCFunction)Reversed, METH_NOARGS, }, |
720 {NULL} | 702 {NULL} |
721 }; | 703 }; |
722 | 704 |
723 static PySequenceMethods SeqSequenceMethods = { | 705 static PySequenceMethods SeqSequenceMethods = { |
724 (lenfunc)Length, // sq_length | 706 (lenfunc)Length, // sq_length |
725 0, // sq_concat | 707 0, // sq_concat |
726 0, // sq_repeat | 708 0, // sq_repeat |
727 (ssizeargfunc)GetItem, // sq_item | 709 (ssizeargfunc)GetItem, // sq_item |
728 0, // sq_slice | 710 0, // sq_slice |
729 0, // sq_ass_item | 711 0, // sq_ass_item |
730 0, // sq_ass_slice | 712 0, // sq_ass_slice |
731 (objobjproc)SeqContains, // sq_contains | 713 (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 | |
738 }; | 714 }; |
739 | 715 |
740 PyTypeObject DescriptorSequence_Type = { | 716 PyTypeObject DescriptorSequence_Type = { |
741 PyVarObject_HEAD_INIT(&PyType_Type, 0) | 717 PyVarObject_HEAD_INIT(&PyType_Type, 0) |
742 "DescriptorSequence", // tp_name | 718 "DescriptorSequence", // tp_name |
743 sizeof(PyContainer), // tp_basicsize | 719 sizeof(PyContainer), // tp_basicsize |
744 0, // tp_itemsize | 720 0, // tp_itemsize |
745 0, // tp_dealloc | 721 0, // tp_dealloc |
746 0, // tp_print | 722 0, // tp_print |
747 0, // tp_getattr | 723 0, // tp_getattr |
748 0, // tp_setattr | 724 0, // tp_setattr |
749 0, // tp_compare | 725 0, // tp_compare |
750 (reprfunc)ContainerRepr, // tp_repr | 726 (reprfunc)ContainerRepr, // tp_repr |
751 0, // tp_as_number | 727 0, // tp_as_number |
752 &SeqSequenceMethods, // tp_as_sequence | 728 &SeqSequenceMethods, // tp_as_sequence |
753 &SeqMappingMethods, // tp_as_mapping | 729 0, // tp_as_mapping |
754 0, // tp_hash | 730 0, // tp_hash |
755 0, // tp_call | 731 0, // tp_call |
756 0, // tp_str | 732 0, // tp_str |
757 0, // tp_getattro | 733 0, // tp_getattro |
758 0, // tp_setattro | 734 0, // tp_setattro |
759 0, // tp_as_buffer | 735 0, // tp_as_buffer |
760 Py_TPFLAGS_DEFAULT, // tp_flags | 736 Py_TPFLAGS_DEFAULT, // tp_flags |
761 0, // tp_doc | 737 0, // tp_doc |
762 0, // tp_traverse | 738 0, // tp_traverse |
763 0, // tp_clear | 739 0, // tp_clear |
(...skipping 660 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1424 }; | 1400 }; |
1425 | 1401 |
1426 } // namespace fields | 1402 } // namespace fields |
1427 | 1403 |
1428 PyObject* NewOneofFieldsSeq(ParentDescriptor descriptor) { | 1404 PyObject* NewOneofFieldsSeq(ParentDescriptor descriptor) { |
1429 return descriptor::NewSequence(&fields::ContainerDef, descriptor); | 1405 return descriptor::NewSequence(&fields::ContainerDef, descriptor); |
1430 } | 1406 } |
1431 | 1407 |
1432 } // namespace oneof_descriptor | 1408 } // namespace oneof_descriptor |
1433 | 1409 |
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 | |
1496 namespace file_descriptor { | 1410 namespace file_descriptor { |
1497 | 1411 |
1498 typedef const FileDescriptor* ParentDescriptor; | 1412 typedef const FileDescriptor* ParentDescriptor; |
1499 | 1413 |
1500 static ParentDescriptor GetDescriptor(PyContainer* self) { | 1414 static ParentDescriptor GetDescriptor(PyContainer* self) { |
1501 return reinterpret_cast<ParentDescriptor>(self->descriptor); | 1415 return reinterpret_cast<ParentDescriptor>(self->descriptor); |
1502 } | 1416 } |
1503 | 1417 |
1504 namespace messages { | 1418 namespace messages { |
1505 | 1419 |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1538 (GetByNumberMethod)NULL, | 1452 (GetByNumberMethod)NULL, |
1539 (NewObjectFromItemMethod)NewObjectFromItem, | 1453 (NewObjectFromItemMethod)NewObjectFromItem, |
1540 (GetItemNameMethod)GetItemName, | 1454 (GetItemNameMethod)GetItemName, |
1541 (GetItemCamelcaseNameMethod)NULL, | 1455 (GetItemCamelcaseNameMethod)NULL, |
1542 (GetItemNumberMethod)NULL, | 1456 (GetItemNumberMethod)NULL, |
1543 (GetItemIndexMethod)GetItemIndex, | 1457 (GetItemIndexMethod)GetItemIndex, |
1544 }; | 1458 }; |
1545 | 1459 |
1546 } // namespace messages | 1460 } // namespace messages |
1547 | 1461 |
1548 PyObject* NewFileMessageTypesByName(ParentDescriptor descriptor) { | 1462 PyObject* NewFileMessageTypesByName(const FileDescriptor* descriptor) { |
1549 return descriptor::NewMappingByName(&messages::ContainerDef, descriptor); | 1463 return descriptor::NewMappingByName(&messages::ContainerDef, descriptor); |
1550 } | 1464 } |
1551 | 1465 |
1552 namespace enums { | 1466 namespace enums { |
1553 | 1467 |
1554 typedef const EnumDescriptor* ItemDescriptor; | 1468 typedef const EnumDescriptor* ItemDescriptor; |
1555 | 1469 |
1556 static int Count(PyContainer* self) { | 1470 static int Count(PyContainer* self) { |
1557 return GetDescriptor(self)->enum_type_count(); | 1471 return GetDescriptor(self)->enum_type_count(); |
1558 } | 1472 } |
(...skipping 27 matching lines...) Expand all Loading... |
1586 (GetByNumberMethod)NULL, | 1500 (GetByNumberMethod)NULL, |
1587 (NewObjectFromItemMethod)NewObjectFromItem, | 1501 (NewObjectFromItemMethod)NewObjectFromItem, |
1588 (GetItemNameMethod)GetItemName, | 1502 (GetItemNameMethod)GetItemName, |
1589 (GetItemCamelcaseNameMethod)NULL, | 1503 (GetItemCamelcaseNameMethod)NULL, |
1590 (GetItemNumberMethod)NULL, | 1504 (GetItemNumberMethod)NULL, |
1591 (GetItemIndexMethod)GetItemIndex, | 1505 (GetItemIndexMethod)GetItemIndex, |
1592 }; | 1506 }; |
1593 | 1507 |
1594 } // namespace enums | 1508 } // namespace enums |
1595 | 1509 |
1596 PyObject* NewFileEnumTypesByName(ParentDescriptor descriptor) { | 1510 PyObject* NewFileEnumTypesByName(const FileDescriptor* descriptor) { |
1597 return descriptor::NewMappingByName(&enums::ContainerDef, descriptor); | 1511 return descriptor::NewMappingByName(&enums::ContainerDef, descriptor); |
1598 } | 1512 } |
1599 | 1513 |
1600 namespace extensions { | 1514 namespace extensions { |
1601 | 1515 |
1602 typedef const FieldDescriptor* ItemDescriptor; | 1516 typedef const FieldDescriptor* ItemDescriptor; |
1603 | 1517 |
1604 static int Count(PyContainer* self) { | 1518 static int Count(PyContainer* self) { |
1605 return GetDescriptor(self)->extension_count(); | 1519 return GetDescriptor(self)->extension_count(); |
1606 } | 1520 } |
(...skipping 27 matching lines...) Expand all Loading... |
1634 (GetByNumberMethod)NULL, | 1548 (GetByNumberMethod)NULL, |
1635 (NewObjectFromItemMethod)NewObjectFromItem, | 1549 (NewObjectFromItemMethod)NewObjectFromItem, |
1636 (GetItemNameMethod)GetItemName, | 1550 (GetItemNameMethod)GetItemName, |
1637 (GetItemCamelcaseNameMethod)NULL, | 1551 (GetItemCamelcaseNameMethod)NULL, |
1638 (GetItemNumberMethod)NULL, | 1552 (GetItemNumberMethod)NULL, |
1639 (GetItemIndexMethod)GetItemIndex, | 1553 (GetItemIndexMethod)GetItemIndex, |
1640 }; | 1554 }; |
1641 | 1555 |
1642 } // namespace extensions | 1556 } // namespace extensions |
1643 | 1557 |
1644 PyObject* NewFileExtensionsByName(ParentDescriptor descriptor) { | 1558 PyObject* NewFileExtensionsByName(const FileDescriptor* descriptor) { |
1645 return descriptor::NewMappingByName(&extensions::ContainerDef, descriptor); | 1559 return descriptor::NewMappingByName(&extensions::ContainerDef, descriptor); |
1646 } | 1560 } |
1647 | 1561 |
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 | |
1696 namespace dependencies { | 1562 namespace dependencies { |
1697 | 1563 |
1698 typedef const FileDescriptor* ItemDescriptor; | 1564 typedef const FileDescriptor* ItemDescriptor; |
1699 | 1565 |
1700 static int Count(PyContainer* self) { | 1566 static int Count(PyContainer* self) { |
1701 return GetDescriptor(self)->dependency_count(); | 1567 return GetDescriptor(self)->dependency_count(); |
1702 } | 1568 } |
1703 | 1569 |
1704 static ItemDescriptor GetByIndex(PyContainer* self, int index) { | 1570 static ItemDescriptor GetByIndex(PyContainer* self, int index) { |
1705 return GetDescriptor(self)->dependency(index); | 1571 return GetDescriptor(self)->dependency(index); |
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1777 if (PyType_Ready(&descriptor::DescriptorSequence_Type) < 0) | 1643 if (PyType_Ready(&descriptor::DescriptorSequence_Type) < 0) |
1778 return false; | 1644 return false; |
1779 if (PyType_Ready(&descriptor::ContainerIterator_Type) < 0) | 1645 if (PyType_Ready(&descriptor::ContainerIterator_Type) < 0) |
1780 return false; | 1646 return false; |
1781 return true; | 1647 return true; |
1782 } | 1648 } |
1783 | 1649 |
1784 } // namespace python | 1650 } // namespace python |
1785 } // namespace protobuf | 1651 } // namespace protobuf |
1786 } // namespace google | 1652 } // namespace google |
OLD | NEW |