changeset 6633:216901c821fe

cbor: revert bytefying all strings The bytefying was done in bulk in 48b30ff742cb, and while the new cbor.py worked fine, the change didn't really make much sense. And because pytype now rightfully complains that there's "No attribute 'format' on bytes", let's change this file back to the original third-party code from 2016.
author Anton Shestakov <av6@dwimlabs.net>
date Wed, 27 Dec 2023 15:31:19 -0300
parents 7771e80a7280
children 991cbf0f66f2
files hgext3rd/evolve/thirdparty/cbor.py
diffstat 1 files changed, 35 insertions(+), 35 deletions(-) [+]
line wrap: on
line diff
--- a/hgext3rd/evolve/thirdparty/cbor.py	Sun Dec 31 17:00:44 2023 -0300
+++ b/hgext3rd/evolve/thirdparty/cbor.py	Wed Dec 27 15:31:19 2023 -0300
@@ -79,23 +79,23 @@
 CBOR_TAG_MIME = 36 # following text is MIME message, headers, separators and all
 CBOR_TAG_CBOR_FILEHEADER = 55799 # can open a file with 0xd9d9f7
 
-_CBOR_TAG_BIGNUM_BYTES = struct.pack(b'B', CBOR_TAG | CBOR_TAG_BIGNUM)
+_CBOR_TAG_BIGNUM_BYTES = struct.pack('B', CBOR_TAG | CBOR_TAG_BIGNUM)
 
 
 def dumps_int(val):
-    b"return bytes representing int val in CBOR"
+    "return bytes representing int val in CBOR"
     if val >= 0:
         # CBOR_UINT is 0, so I'm lazy/efficient about not OR-ing it in.
         if val <= 23:
-            return struct.pack(b'B', val)
+            return struct.pack('B', val)
         if val <= 0x0ff:
-            return struct.pack(b'BB', CBOR_UINT8_FOLLOWS, val)
+            return struct.pack('BB', CBOR_UINT8_FOLLOWS, val)
         if val <= 0x0ffff:
-            return struct.pack(b'!BH', CBOR_UINT16_FOLLOWS, val)
+            return struct.pack('!BH', CBOR_UINT16_FOLLOWS, val)
         if val <= 0x0ffffffff:
-            return struct.pack(b'!BI', CBOR_UINT32_FOLLOWS, val)
+            return struct.pack('!BI', CBOR_UINT32_FOLLOWS, val)
         if val <= 0x0ffffffffffffffff:
-            return struct.pack(b'!BQ', CBOR_UINT64_FOLLOWS, val)
+            return struct.pack('!BQ', CBOR_UINT64_FOLLOWS, val)
         outb = _dumps_bignum_to_bytearray(val)
         return _CBOR_TAG_BIGNUM_BYTES + _encode_type_num(CBOR_BYTES, len(outb)) + outb
     val = -1 - val
@@ -119,28 +119,28 @@
 
 
 def dumps_float(val):
-    return struct.pack(b"!Bd", CBOR_FLOAT64, val)
+    return struct.pack("!Bd", CBOR_FLOAT64, val)
 
 
-_CBOR_TAG_NEGBIGNUM_BYTES = struct.pack(b'B', CBOR_TAG | CBOR_TAG_NEGBIGNUM)
+_CBOR_TAG_NEGBIGNUM_BYTES = struct.pack('B', CBOR_TAG | CBOR_TAG_NEGBIGNUM)
 
 
 def _encode_type_num(cbor_type, val):
     """For some CBOR primary type [0..7] and an auxiliary unsigned number, return CBOR encoded bytes"""
     assert val >= 0
     if val <= 23:
-        return struct.pack(b'B', cbor_type | val)
+        return struct.pack('B', cbor_type | val)
     if val <= 0x0ff:
-        return struct.pack(b'BB', cbor_type | CBOR_UINT8_FOLLOWS, val)
+        return struct.pack('BB', cbor_type | CBOR_UINT8_FOLLOWS, val)
     if val <= 0x0ffff:
-        return struct.pack(b'!BH', cbor_type | CBOR_UINT16_FOLLOWS, val)
+        return struct.pack('!BH', cbor_type | CBOR_UINT16_FOLLOWS, val)
     if val <= 0x0ffffffff:
-        return struct.pack(b'!BI', cbor_type | CBOR_UINT32_FOLLOWS, val)
+        return struct.pack('!BI', cbor_type | CBOR_UINT32_FOLLOWS, val)
     if (((cbor_type == CBOR_NEGINT) and (val <= 0x07fffffffffffffff)) or
         ((cbor_type != CBOR_NEGINT) and (val <= 0x0ffffffffffffffff))):
-        return struct.pack(b'!BQ', cbor_type | CBOR_UINT64_FOLLOWS, val)
+        return struct.pack('!BQ', cbor_type | CBOR_UINT64_FOLLOWS, val)
     if cbor_type != CBOR_NEGINT:
-        raise Exception(b"value too big for CBOR unsigned number: {0!r}".format(val))
+        raise Exception("value too big for CBOR unsigned number: {0!r}".format(val))
     outb = _dumps_bignum_to_bytearray(val)
     return _CBOR_TAG_NEGBIGNUM_BYTES + _encode_type_num(CBOR_BYTES, len(outb)) + outb
 
@@ -201,8 +201,8 @@
 
 def dumps_bool(b):
     if b:
-        return struct.pack(b'B', CBOR_TRUE)
-    return struct.pack(b'B', CBOR_FALSE)
+        return struct.pack('B', CBOR_TRUE)
+    return struct.pack('B', CBOR_FALSE)
 
 
 def dumps_tag(t, sort_keys=False):
@@ -223,7 +223,7 @@
 
 def dumps(ob, sort_keys=False):
     if ob is None:
-        return struct.pack(b'B', CBOR_NULL)
+        return struct.pack('B', CBOR_NULL)
     if isinstance(ob, bool):
         return dumps_bool(ob)
     if _is_stringish(ob):
@@ -239,7 +239,7 @@
         return dumps_int(ob)
     if isinstance(ob, Tag):
         return dumps_tag(ob, sort_keys=sort_keys)
-    raise Exception(b"don't know how to cbor serialize object of type %s", type(ob))
+    raise Exception("don't know how to cbor serialize object of type %s", type(ob))
 
 
 # same basic signature as json.dump, but with no options (yet)
@@ -260,7 +260,7 @@
         self.value = value
 
     def __repr__(self):
-        return b"Tag({0!r}, {1!r})".format(self.tag, self.value)
+        return "Tag({0!r}, {1!r})".format(self.tag, self.value)
 
     def __eq__(self, other):
         if not isinstance(other, Tag):
@@ -273,7 +273,7 @@
     Parse CBOR bytes and return Python objects.
     """
     if data is None:
-        raise ValueError(b"got None for buffer to decode in loads")
+        raise ValueError("got None for buffer to decode in loads")
     fp = StringIO(data)
     return _loads(fp)[0]
 
@@ -296,22 +296,22 @@
         aux = tag_aux
     elif tag_aux == CBOR_UINT8_FOLLOWS:
         data = fp.read(1)
-        aux = struct.unpack_from(b"!B", data, 0)[0]
+        aux = struct.unpack_from("!B", data, 0)[0]
         bytes_read += 1
     elif tag_aux == CBOR_UINT16_FOLLOWS:
         data = fp.read(2)
-        aux = struct.unpack_from(b"!H", data, 0)[0]
+        aux = struct.unpack_from("!H", data, 0)[0]
         bytes_read += 2
     elif tag_aux == CBOR_UINT32_FOLLOWS:
         data = fp.read(4)
-        aux = struct.unpack_from(b"!I", data, 0)[0]
+        aux = struct.unpack_from("!I", data, 0)[0]
         bytes_read += 4
     elif tag_aux == CBOR_UINT64_FOLLOWS:
         data = fp.read(8)
-        aux = struct.unpack_from(b"!Q", data, 0)[0]
+        aux = struct.unpack_from("!Q", data, 0)[0]
         bytes_read += 8
     else:
-        assert tag_aux == CBOR_VAR_FOLLOWS, b"bogus tag {0:02x}".format(tb)
+        assert tag_aux == CBOR_VAR_FOLLOWS, "bogus tag {0:02x}".format(tb)
         aux = None
 
     return tag, tag_aux, aux, bytes_read
@@ -385,9 +385,9 @@
         return ob, bytes_read
 
 def _loads(fp, limit=None, depth=0, returntags=False):
-    b"return (object, bytes read)"
+    "return (object, bytes read)"
     if depth > _MAX_DEPTH:
-        raise Exception(b"hit CBOR loads recursion depth limit")
+        raise Exception("hit CBOR loads recursion depth limit")
 
     tb = _read_byte(fp)
 
@@ -397,16 +397,16 @@
     # Some special cases of CBOR_7 best handled by special struct.unpack logic here
     if tb == CBOR_FLOAT16:
         data = fp.read(2)
-        hibyte, lowbyte = struct.unpack_from(b"BB", data, 0)
+        hibyte, lowbyte = struct.unpack_from("BB", data, 0)
         exp = (hibyte >> 2) & 0x1F
         mant = ((hibyte & 0x03) << 8) | lowbyte
         if exp == 0:
             val = mant * (2.0 ** -24)
         elif exp == 31:
             if mant == 0:
-                val = float(b'Inf')
+                val = float('Inf')
             else:
-                val = float(b'NaN')
+                val = float('NaN')
         else:
             val = (mant + 1024.0) * (2 ** (exp - 25))
         if hibyte & 0x80:
@@ -414,11 +414,11 @@
         return (val, 3)
     elif tb == CBOR_FLOAT32:
         data = fp.read(4)
-        pf = struct.unpack_from(b"!f", data, 0)
+        pf = struct.unpack_from("!f", data, 0)
         return (pf[0], 5)
     elif tb == CBOR_FLOAT64:
         data = fp.read(8)
-        pf = struct.unpack_from(b"!d", data, 0)
+        pf = struct.unpack_from("!d", data, 0)
         return (pf[0], 9)
 
     tag, tag_aux, aux, bytes_read = _tag_aux(fp, tb)
@@ -461,7 +461,7 @@
             return (None, bytes_read)
         if tb == CBOR_UNDEFINED:
             return (None, bytes_read)
-        raise ValueError(b"unknown cbor tag 7 byte: {:02x}".format(tb))
+        raise ValueError("unknown cbor tag 7 byte: {:02x}".format(tb))
 
 
 def loads_bytes(fp, aux, btag=CBOR_BYTES):
@@ -481,7 +481,7 @@
             total_bytes_read += 1
             break
         tag, tag_aux, aux, bytes_read = _tag_aux(fp, tb)
-        assert tag == btag, b'variable length value contains unexpected component'
+        assert tag == btag, 'variable length value contains unexpected component'
         ob = fp.read(aux)
         chunklist.append(ob)
         total_bytes_read += bytes_read + aux