| 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 18 matching lines...) Expand all Loading... |
| 29 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 29 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 30 | 30 |
| 31 """Utilities for Python proto2 tests. | 31 """Utilities for Python proto2 tests. |
| 32 | 32 |
| 33 This is intentionally modeled on C++ code in | 33 This is intentionally modeled on C++ code in |
| 34 //google/protobuf/test_util.*. | 34 //google/protobuf/test_util.*. |
| 35 """ | 35 """ |
| 36 | 36 |
| 37 __author__ = 'robinson@google.com (Will Robinson)' | 37 __author__ = 'robinson@google.com (Will Robinson)' |
| 38 | 38 |
| 39 import numbers | |
| 40 import operator | |
| 41 import os.path | 39 import os.path |
| 40 |
| 42 import sys | 41 import sys |
| 43 | 42 |
| 44 from google.protobuf import unittest_import_pb2 | 43 from google.protobuf import unittest_import_pb2 |
| 45 from google.protobuf import unittest_pb2 | 44 from google.protobuf import unittest_pb2 |
| 46 from google.protobuf import descriptor_pb2 | 45 from google.protobuf import descriptor_pb2 |
| 47 | 46 |
| 48 # Tests whether the given TestAllTypes message is proto2 or not. | 47 # Tests whether the given TestAllTypes message is proto2 or not. |
| 49 # This is used to gate several fields/features that only exist | 48 # This is used to gate several fields/features that only exist |
| 50 # for the proto2 version of the message. | 49 # for the proto2 version of the message. |
| 51 def IsProto2(message): | 50 def IsProto2(message): |
| (...skipping 636 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 688 message.unpacked_sint64.extend([606, 706]) | 687 message.unpacked_sint64.extend([606, 706]) |
| 689 message.unpacked_fixed32.extend([607, 707]) | 688 message.unpacked_fixed32.extend([607, 707]) |
| 690 message.unpacked_fixed64.extend([608, 708]) | 689 message.unpacked_fixed64.extend([608, 708]) |
| 691 message.unpacked_sfixed32.extend([609, 709]) | 690 message.unpacked_sfixed32.extend([609, 709]) |
| 692 message.unpacked_sfixed64.extend([610, 710]) | 691 message.unpacked_sfixed64.extend([610, 710]) |
| 693 message.unpacked_float.extend([611.0, 711.0]) | 692 message.unpacked_float.extend([611.0, 711.0]) |
| 694 message.unpacked_double.extend([612.0, 712.0]) | 693 message.unpacked_double.extend([612.0, 712.0]) |
| 695 message.unpacked_bool.extend([True, False]) | 694 message.unpacked_bool.extend([True, False]) |
| 696 message.unpacked_enum.extend([unittest_pb2.FOREIGN_BAR, | 695 message.unpacked_enum.extend([unittest_pb2.FOREIGN_BAR, |
| 697 unittest_pb2.FOREIGN_BAZ]) | 696 unittest_pb2.FOREIGN_BAZ]) |
| 698 | |
| 699 | |
| 700 class NonStandardInteger(numbers.Integral): | |
| 701 """An integer object that does not subclass int. | |
| 702 | |
| 703 This is used to verify that both C++ and regular proto systems can handle | |
| 704 integer others than int and long and that they handle them in predictable | |
| 705 ways. | |
| 706 | |
| 707 NonStandardInteger is the minimal legal specification for a custom Integral. | |
| 708 As such, it does not support 0 < x < 5 and it is not hashable. | |
| 709 | |
| 710 Note: This is added here instead of relying on numpy or a similar library with | |
| 711 custom integers to limit dependencies. | |
| 712 """ | |
| 713 | |
| 714 def __init__(self, val, error_string_on_conversion=None): | |
| 715 assert isinstance(val, numbers.Integral) | |
| 716 if isinstance(val, NonStandardInteger): | |
| 717 val = val.val | |
| 718 self.val = val | |
| 719 self.error_string_on_conversion = error_string_on_conversion | |
| 720 | |
| 721 def __long__(self): | |
| 722 if self.error_string_on_conversion: | |
| 723 raise RuntimeError(self.error_string_on_conversion) | |
| 724 return long(self.val) | |
| 725 | |
| 726 def __abs__(self): | |
| 727 return NonStandardInteger(operator.abs(self.val)) | |
| 728 | |
| 729 def __add__(self, y): | |
| 730 return NonStandardInteger(operator.add(self.val, y)) | |
| 731 | |
| 732 def __div__(self, y): | |
| 733 return NonStandardInteger(operator.div(self.val, y)) | |
| 734 | |
| 735 def __eq__(self, y): | |
| 736 return operator.eq(self.val, y) | |
| 737 | |
| 738 def __floordiv__(self, y): | |
| 739 return NonStandardInteger(operator.floordiv(self.val, y)) | |
| 740 | |
| 741 def __truediv__(self, y): | |
| 742 return NonStandardInteger(operator.truediv(self.val, y)) | |
| 743 | |
| 744 def __invert__(self): | |
| 745 return NonStandardInteger(operator.invert(self.val)) | |
| 746 | |
| 747 def __mod__(self, y): | |
| 748 return NonStandardInteger(operator.mod(self.val, y)) | |
| 749 | |
| 750 def __mul__(self, y): | |
| 751 return NonStandardInteger(operator.mul(self.val, y)) | |
| 752 | |
| 753 def __neg__(self): | |
| 754 return NonStandardInteger(operator.neg(self.val)) | |
| 755 | |
| 756 def __pos__(self): | |
| 757 return NonStandardInteger(operator.pos(self.val)) | |
| 758 | |
| 759 def __pow__(self, y): | |
| 760 return NonStandardInteger(operator.pow(self.val, y)) | |
| 761 | |
| 762 def __trunc__(self): | |
| 763 return int(self.val) | |
| 764 | |
| 765 def __radd__(self, y): | |
| 766 return NonStandardInteger(operator.add(y, self.val)) | |
| 767 | |
| 768 def __rdiv__(self, y): | |
| 769 return NonStandardInteger(operator.div(y, self.val)) | |
| 770 | |
| 771 def __rmod__(self, y): | |
| 772 return NonStandardInteger(operator.mod(y, self.val)) | |
| 773 | |
| 774 def __rmul__(self, y): | |
| 775 return NonStandardInteger(operator.mul(y, self.val)) | |
| 776 | |
| 777 def __rpow__(self, y): | |
| 778 return NonStandardInteger(operator.pow(y, self.val)) | |
| 779 | |
| 780 def __rfloordiv__(self, y): | |
| 781 return NonStandardInteger(operator.floordiv(y, self.val)) | |
| 782 | |
| 783 def __rtruediv__(self, y): | |
| 784 return NonStandardInteger(operator.truediv(y, self.val)) | |
| 785 | |
| 786 def __lshift__(self, y): | |
| 787 return NonStandardInteger(operator.lshift(self.val, y)) | |
| 788 | |
| 789 def __rshift__(self, y): | |
| 790 return NonStandardInteger(operator.rshift(self.val, y)) | |
| 791 | |
| 792 def __rlshift__(self, y): | |
| 793 return NonStandardInteger(operator.lshift(y, self.val)) | |
| 794 | |
| 795 def __rrshift__(self, y): | |
| 796 return NonStandardInteger(operator.rshift(y, self.val)) | |
| 797 | |
| 798 def __le__(self, y): | |
| 799 if isinstance(y, NonStandardInteger): | |
| 800 y = y.val | |
| 801 return operator.le(self.val, y) | |
| 802 | |
| 803 def __lt__(self, y): | |
| 804 if isinstance(y, NonStandardInteger): | |
| 805 y = y.val | |
| 806 return operator.lt(self.val, y) | |
| 807 | |
| 808 def __and__(self, y): | |
| 809 return NonStandardInteger(operator.and_(self.val, y)) | |
| 810 | |
| 811 def __or__(self, y): | |
| 812 return NonStandardInteger(operator.or_(self.val, y)) | |
| 813 | |
| 814 def __xor__(self, y): | |
| 815 return NonStandardInteger(operator.xor(self.val, y)) | |
| 816 | |
| 817 def __rand__(self, y): | |
| 818 return NonStandardInteger(operator.and_(y, self.val)) | |
| 819 | |
| 820 def __ror__(self, y): | |
| 821 return NonStandardInteger(operator.or_(y, self.val)) | |
| 822 | |
| 823 def __rxor__(self, y): | |
| 824 return NonStandardInteger(operator.xor(y, self.val)) | |
| 825 | |
| 826 def __bool__(self): | |
| 827 return self.val | |
| 828 | |
| 829 def __nonzero__(self): | |
| 830 return self.val | |
| 831 | |
| 832 def __ceil__(self): | |
| 833 return self | |
| 834 | |
| 835 def __floor__(self): | |
| 836 return self | |
| 837 | |
| 838 def __int__(self): | |
| 839 if self.error_string_on_conversion: | |
| 840 raise RuntimeError(self.error_string_on_conversion) | |
| 841 return int(self.val) | |
| 842 | |
| 843 def __round__(self): | |
| 844 return self | |
| 845 | |
| 846 def __repr__(self): | |
| 847 return 'NonStandardInteger(%s)' % self.val | |
| 848 | |
| OLD | NEW |