Mercurial > public > mercurial-scm > hg
comparison mercurial/utils/cborutil.py @ 52694:8a2091a2f974
cborutil: unbyteify string args to builtin Error classes
This avoids printing the error with a `b''` prefix. The `CBORDecodeError` class
subclasses `Exception`, not one of the usual error classes in `mercurial.error`.
author | Matt Harbison <matt_harbison@yahoo.com> |
---|---|
date | Mon, 13 Jan 2025 00:29:11 -0500 |
parents | 5e09c6b5b795 |
children |
comparison
equal
deleted
inserted
replaced
52693:5e09c6b5b795 | 52694:8a2091a2f974 |
---|---|
129 yield BREAK | 129 yield BREAK |
130 | 130 |
131 | 131 |
132 def streamencodeint(v: int) -> Iterator[bytes]: | 132 def streamencodeint(v: int) -> Iterator[bytes]: |
133 if v >= 18446744073709551616 or v < -18446744073709551616: | 133 if v >= 18446744073709551616 or v < -18446744073709551616: |
134 raise ValueError(b'big integers not supported') | 134 raise ValueError('big integers not supported') |
135 | 135 |
136 if v >= 0: | 136 if v >= 0: |
137 yield encodelength(MAJOR_TYPE_UINT, v) | 137 yield encodelength(MAJOR_TYPE_UINT, v) |
138 else: | 138 else: |
139 yield encodelength(MAJOR_TYPE_NEGINT, abs(v) - 1) | 139 yield encodelength(MAJOR_TYPE_NEGINT, abs(v) - 1) |
342 # It is an indefinite length bytestring. | 342 # It is an indefinite length bytestring. |
343 else: | 343 else: |
344 return True, None, 1, SPECIAL_START_INDEFINITE_BYTESTRING | 344 return True, None, 1, SPECIAL_START_INDEFINITE_BYTESTRING |
345 | 345 |
346 elif majortype == MAJOR_TYPE_STRING: | 346 elif majortype == MAJOR_TYPE_STRING: |
347 raise CBORDecodeError(b'string major type not supported') | 347 raise CBORDecodeError('string major type not supported') |
348 | 348 |
349 elif majortype == MAJOR_TYPE_ARRAY: | 349 elif majortype == MAJOR_TYPE_ARRAY: |
350 # Beginning of arrays are treated as uints in order to decode their | 350 # Beginning of arrays are treated as uints in order to decode their |
351 # length. We don't allow indefinite length arrays. | 351 # length. We don't allow indefinite length arrays. |
352 complete, size, readcount = decodeuint(subtype, b, offset) | 352 complete, size, readcount = decodeuint(subtype, b, offset) |
391 if not complete: | 391 if not complete: |
392 return False, None, readcount2, SPECIAL_NONE | 392 return False, None, readcount2, SPECIAL_NONE |
393 | 393 |
394 if special != SPECIAL_START_ARRAY: | 394 if special != SPECIAL_START_ARRAY: |
395 raise CBORDecodeError( | 395 raise CBORDecodeError( |
396 b'expected array after finite set semantic tag' | 396 'expected array after finite set semantic tag' |
397 ) | 397 ) |
398 | 398 |
399 return True, size, readcount + readcount2 + 1, SPECIAL_START_SET | 399 return True, size, readcount + readcount2 + 1, SPECIAL_START_SET |
400 | 400 |
401 else: | 401 else: |
402 raise CBORDecodeError(b'semantic tag %d not allowed' % tagvalue) | 402 raise CBORDecodeError('semantic tag %d not allowed' % tagvalue) |
403 | 403 |
404 elif majortype == MAJOR_TYPE_SPECIAL: | 404 elif majortype == MAJOR_TYPE_SPECIAL: |
405 # Only specific values for the information field are allowed. | 405 # Only specific values for the information field are allowed. |
406 if subtype == SUBTYPE_FALSE: | 406 if subtype == SUBTYPE_FALSE: |
407 return True, False, 1, SPECIAL_NONE | 407 return True, False, 1, SPECIAL_NONE |
411 return True, None, 1, SPECIAL_NONE | 411 return True, None, 1, SPECIAL_NONE |
412 elif subtype == SUBTYPE_INDEFINITE: | 412 elif subtype == SUBTYPE_INDEFINITE: |
413 return True, None, 1, SPECIAL_INDEFINITE_BREAK | 413 return True, None, 1, SPECIAL_INDEFINITE_BREAK |
414 # If value is 24, subtype is in next byte. | 414 # If value is 24, subtype is in next byte. |
415 else: | 415 else: |
416 raise CBORDecodeError(b'special type %d not allowed' % subtype) | 416 raise CBORDecodeError('special type %d not allowed' % subtype) |
417 else: | 417 else: |
418 assert False | 418 assert False |
419 | 419 |
420 | 420 |
421 def decodeuint( | 421 def decodeuint( |
446 # Indefinite length specifier. | 446 # Indefinite length specifier. |
447 elif subtype == 31: | 447 elif subtype == 31: |
448 if allowindefinite: | 448 if allowindefinite: |
449 return True, None, 0 | 449 return True, None, 0 |
450 else: | 450 else: |
451 raise CBORDecodeError(b'indefinite length uint not allowed here') | 451 raise CBORDecodeError('indefinite length uint not allowed here') |
452 elif subtype >= 28: | 452 elif subtype >= 28: |
453 raise CBORDecodeError( | 453 raise CBORDecodeError( |
454 b'unsupported subtype on integer type: %d' % subtype | 454 'unsupported subtype on integer type: %d' % subtype |
455 ) | 455 ) |
456 | 456 |
457 if subtype == 24: | 457 if subtype == 24: |
458 s = STRUCT_BIG_UBYTE | 458 s = STRUCT_BIG_UBYTE |
459 elif subtype == 25: | 459 elif subtype == 25: |
461 elif subtype == 26: | 461 elif subtype == 26: |
462 s = STRUCT_BIG_ULONG | 462 s = STRUCT_BIG_ULONG |
463 elif subtype == 27: | 463 elif subtype == 27: |
464 s = STRUCT_BIG_ULONGLONG | 464 s = STRUCT_BIG_ULONGLONG |
465 else: | 465 else: |
466 raise CBORDecodeError(b'bounds condition checking violation') | 466 raise CBORDecodeError('bounds condition checking violation') |
467 | 467 |
468 if len(b) - offset >= s.size: | 468 if len(b) - offset >= s.size: |
469 return True, s.unpack_from(b, offset)[0], s.size | 469 return True, s.unpack_from(b, offset)[0], s.size |
470 else: | 470 else: |
471 return False, None, len(b) - offset - s.size | 471 return False, None, len(b) - offset - s.size |
663 elif special == SPECIAL_START_INDEFINITE_BYTESTRING: | 663 elif special == SPECIAL_START_INDEFINITE_BYTESTRING: |
664 self._state = self._STATE_WANT_BYTESTRING_CHUNK_FIRST | 664 self._state = self._STATE_WANT_BYTESTRING_CHUNK_FIRST |
665 | 665 |
666 else: | 666 else: |
667 raise CBORDecodeError( | 667 raise CBORDecodeError( |
668 b'unhandled special state: %d' % special | 668 'unhandled special state: %d' % special |
669 ) | 669 ) |
670 | 670 |
671 # This value becomes an element of the current array. | 671 # This value becomes an element of the current array. |
672 elif self._state == self._STATE_WANT_ARRAY_VALUE: | 672 elif self._state == self._STATE_WANT_ARRAY_VALUE: |
673 # Simple values get appended. | 673 # Simple values get appended. |
725 | 725 |
726 self._state = self._STATE_WANT_SET_VALUE | 726 self._state = self._STATE_WANT_SET_VALUE |
727 | 727 |
728 elif special == SPECIAL_START_INDEFINITE_BYTESTRING: | 728 elif special == SPECIAL_START_INDEFINITE_BYTESTRING: |
729 raise CBORDecodeError( | 729 raise CBORDecodeError( |
730 b'indefinite length bytestrings ' | 730 'indefinite length bytestrings ' |
731 b'not allowed as array values' | 731 'not allowed as array values' |
732 ) | 732 ) |
733 | 733 |
734 else: | 734 else: |
735 raise CBORDecodeError( | 735 raise CBORDecodeError( |
736 b'unhandled special item when ' | 736 'unhandled special item when ' |
737 b'expecting array value: %d' % special | 737 'expecting array value: %d' % special |
738 ) | 738 ) |
739 | 739 |
740 # This value becomes the key of the current map instance. | 740 # This value becomes the key of the current map instance. |
741 elif self._state == self._STATE_WANT_MAP_KEY: | 741 elif self._state == self._STATE_WANT_MAP_KEY: |
742 if special == SPECIAL_NONE: | 742 if special == SPECIAL_NONE: |
743 self._currentmapkey = value | 743 self._currentmapkey = value |
744 self._state = self._STATE_WANT_MAP_VALUE | 744 self._state = self._STATE_WANT_MAP_VALUE |
745 | 745 |
746 elif special == SPECIAL_START_INDEFINITE_BYTESTRING: | 746 elif special == SPECIAL_START_INDEFINITE_BYTESTRING: |
747 raise CBORDecodeError( | 747 raise CBORDecodeError( |
748 b'indefinite length bytestrings ' | 748 'indefinite length bytestrings ' |
749 b'not allowed as map keys' | 749 'not allowed as map keys' |
750 ) | 750 ) |
751 | 751 |
752 elif special in ( | 752 elif special in ( |
753 SPECIAL_START_ARRAY, | 753 SPECIAL_START_ARRAY, |
754 SPECIAL_START_MAP, | 754 SPECIAL_START_MAP, |
755 SPECIAL_START_SET, | 755 SPECIAL_START_SET, |
756 ): | 756 ): |
757 raise CBORDecodeError( | 757 raise CBORDecodeError( |
758 b'collections not supported as map keys' | 758 'collections not supported as map keys' |
759 ) | 759 ) |
760 | 760 |
761 # We do not allow special values to be used as map keys. | 761 # We do not allow special values to be used as map keys. |
762 else: | 762 else: |
763 raise CBORDecodeError( | 763 raise CBORDecodeError( |
764 b'unhandled special item when ' | 764 'unhandled special item when ' |
765 b'expecting map key: %d' % special | 765 'expecting map key: %d' % special |
766 ) | 766 ) |
767 | 767 |
768 # This value becomes the value of the current map key. | 768 # This value becomes the value of the current map key. |
769 elif self._state == self._STATE_WANT_MAP_VALUE: | 769 elif self._state == self._STATE_WANT_MAP_VALUE: |
770 # Simple values simply get inserted into the map. | 770 # Simple values simply get inserted into the map. |
826 | 826 |
827 self._state = self._STATE_WANT_SET_VALUE | 827 self._state = self._STATE_WANT_SET_VALUE |
828 | 828 |
829 elif special == SPECIAL_START_INDEFINITE_BYTESTRING: | 829 elif special == SPECIAL_START_INDEFINITE_BYTESTRING: |
830 raise CBORDecodeError( | 830 raise CBORDecodeError( |
831 b'indefinite length bytestrings not ' | 831 'indefinite length bytestrings not ' |
832 b'allowed as map values' | 832 'allowed as map values' |
833 ) | 833 ) |
834 | 834 |
835 else: | 835 else: |
836 raise CBORDecodeError( | 836 raise CBORDecodeError( |
837 b'unhandled special item when ' | 837 'unhandled special item when ' |
838 b'expecting map value: %d' % special | 838 'expecting map value: %d' % special |
839 ) | 839 ) |
840 | 840 |
841 self._currentmapkey = None | 841 self._currentmapkey = None |
842 | 842 |
843 # This value is added to the current set. | 843 # This value is added to the current set. |
847 lastc[b'v'].add(value) | 847 lastc[b'v'].add(value) |
848 lastc[b'remaining'] -= 1 | 848 lastc[b'remaining'] -= 1 |
849 | 849 |
850 elif special == SPECIAL_START_INDEFINITE_BYTESTRING: | 850 elif special == SPECIAL_START_INDEFINITE_BYTESTRING: |
851 raise CBORDecodeError( | 851 raise CBORDecodeError( |
852 b'indefinite length bytestrings not ' | 852 'indefinite length bytestrings not ' |
853 b'allowed as set values' | 853 'allowed as set values' |
854 ) | 854 ) |
855 | 855 |
856 elif special in ( | 856 elif special in ( |
857 SPECIAL_START_ARRAY, | 857 SPECIAL_START_ARRAY, |
858 SPECIAL_START_MAP, | 858 SPECIAL_START_MAP, |
859 SPECIAL_START_SET, | 859 SPECIAL_START_SET, |
860 ): | 860 ): |
861 raise CBORDecodeError( | 861 raise CBORDecodeError( |
862 b'collections not allowed as set values' | 862 'collections not allowed as set values' |
863 ) | 863 ) |
864 | 864 |
865 # We don't allow non-trivial types to exist as set values. | 865 # We don't allow non-trivial types to exist as set values. |
866 else: | 866 else: |
867 raise CBORDecodeError( | 867 raise CBORDecodeError( |
868 b'unhandled special item when ' | 868 'unhandled special item when ' |
869 b'expecting set value: %d' % special | 869 'expecting set value: %d' % special |
870 ) | 870 ) |
871 | 871 |
872 # This value represents the first chunk in an indefinite length | 872 # This value represents the first chunk in an indefinite length |
873 # bytestring. | 873 # bytestring. |
874 elif self._state == self._STATE_WANT_BYTESTRING_CHUNK_FIRST: | 874 elif self._state == self._STATE_WANT_BYTESTRING_CHUNK_FIRST: |
895 assert not self._collectionstack | 895 assert not self._collectionstack |
896 self._state = self._STATE_NONE | 896 self._state = self._STATE_NONE |
897 | 897 |
898 else: | 898 else: |
899 raise CBORDecodeError( | 899 raise CBORDecodeError( |
900 b'unexpected special value when ' | 900 'unexpected special value when ' |
901 b'expecting bytestring chunk: %d' % special | 901 'expecting bytestring chunk: %d' % special |
902 ) | 902 ) |
903 | 903 |
904 # This value represents the non-initial chunk in an indefinite | 904 # This value represents the non-initial chunk in an indefinite |
905 # length bytestring. | 905 # length bytestring. |
906 elif self._state == self._STATE_WANT_BYTESTRING_CHUNK_SUBSEQUENT: | 906 elif self._state == self._STATE_WANT_BYTESTRING_CHUNK_SUBSEQUENT: |
917 assert not self._collectionstack | 917 assert not self._collectionstack |
918 self._state = self._STATE_NONE | 918 self._state = self._STATE_NONE |
919 | 919 |
920 else: | 920 else: |
921 raise CBORDecodeError( | 921 raise CBORDecodeError( |
922 b'unexpected special value when ' | 922 'unexpected special value when ' |
923 b'expecting bytestring chunk: %d' % special | 923 'expecting bytestring chunk: %d' % special |
924 ) | 924 ) |
925 | 925 |
926 else: | 926 else: |
927 raise CBORDecodeError( | 927 raise CBORDecodeError( |
928 b'unhandled decoder state: %d' % self._state | 928 'unhandled decoder state: %d' % self._state |
929 ) | 929 ) |
930 | 930 |
931 # We could have just added the final value in a collection. End | 931 # We could have just added the final value in a collection. End |
932 # all complete collections at the top of the stack. | 932 # all complete collections at the top of the stack. |
933 while True: | 933 while True: |
1073 decoder = sansiodecoder() | 1073 decoder = sansiodecoder() |
1074 | 1074 |
1075 havevalues, readcount, wantbytes = decoder.decode(b) | 1075 havevalues, readcount, wantbytes = decoder.decode(b) |
1076 | 1076 |
1077 if readcount != len(b): | 1077 if readcount != len(b): |
1078 raise CBORDecodeError(b'input data not fully consumed') | 1078 raise CBORDecodeError('input data not fully consumed') |
1079 | 1079 |
1080 if decoder.inprogress: | 1080 if decoder.inprogress: |
1081 raise CBORDecodeError(b'input data not complete') | 1081 raise CBORDecodeError('input data not complete') |
1082 | 1082 |
1083 return decoder.getavailable() | 1083 return decoder.getavailable() |