cborutil: remove readindefinitebytestringtoiter()
authorGregory Szorc <gregory.szorc@gmail.com>
Fri, 31 Aug 2018 15:54:17 -0700
changeset 39412 a40d3da89b7d
parent 39411 aeb551a3bb8a
child 39413 babad5ebaf0a
cborutil: remove readindefinitebytestringtoiter() This was implemented as part of implementing streaming encoding. It was never used outside of tests. Now that we have a full CBOR decoder, it can be used for incremental decoding of indefinite-length byte strings. This also removes the last use of the vendored cbor2 package from this module. Differential Revision: https://phab.mercurial-scm.org/D4433
mercurial/utils/cborutil.py
tests/test-cbor.py
--- a/mercurial/utils/cborutil.py	Tue Aug 28 15:02:48 2018 -0700
+++ b/mercurial/utils/cborutil.py	Fri Aug 31 15:54:17 2018 -0700
@@ -10,10 +10,6 @@
 import struct
 import sys
 
-from ..thirdparty.cbor.cbor2 import (
-    decoder as decodermod,
-)
-
 # Very short very of RFC 7049...
 #
 # Each item begins with a byte. The 3 high bits of that byte denote the
@@ -219,54 +215,6 @@
 
     return fn(v)
 
-def readindefinitebytestringtoiter(fh, expectheader=True):
-    """Read an indefinite bytestring to a generator.
-
-    Receives an object with a ``read(X)`` method to read N bytes.
-
-    If ``expectheader`` is True, it is expected that the first byte read
-    will represent an indefinite length bytestring. Otherwise, we
-    expect the first byte to be part of the first bytestring chunk.
-    """
-    read = fh.read
-    decodeuint = decodermod.decode_uint
-    byteasinteger = decodermod.byte_as_integer
-
-    if expectheader:
-        initial = decodermod.byte_as_integer(read(1))
-
-        majortype = initial >> 5
-        subtype = initial & SUBTYPE_MASK
-
-        if majortype != MAJOR_TYPE_BYTESTRING:
-            raise decodermod.CBORDecodeError(
-                'expected major type %d; got %d' % (MAJOR_TYPE_BYTESTRING,
-                                                    majortype))
-
-        if subtype != SUBTYPE_INDEFINITE:
-            raise decodermod.CBORDecodeError(
-                'expected indefinite subtype; got %d' % subtype)
-
-    # The indefinite bytestring is composed of chunks of normal bytestrings.
-    # Read chunks until we hit a BREAK byte.
-
-    while True:
-        # We need to sniff for the BREAK byte.
-        initial = byteasinteger(read(1))
-
-        if initial == BREAK_INT:
-            break
-
-        length = decodeuint(fh, initial & SUBTYPE_MASK)
-        chunk = read(length)
-
-        if len(chunk) != length:
-            raise decodermod.CBORDecodeError(
-                'failed to read bytestring chunk: got %d bytes; expected %d' % (
-                    len(chunk), length))
-
-        yield chunk
-
 class CBORDecodeError(Exception):
     """Represents an error decoding CBOR."""
 
--- a/tests/test-cbor.py	Tue Aug 28 15:02:48 2018 -0700
+++ b/tests/test-cbor.py	Fri Aug 31 15:54:17 2018 -0700
@@ -1,6 +1,5 @@
 from __future__ import absolute_import
 
-import io
 import unittest
 
 from mercurial.thirdparty import (
@@ -118,16 +117,6 @@
         self.assertTrue(b[0].isfirst)
         self.assertTrue(b[0].islast)
 
-    def testreadtoiter(self):
-        source = io.BytesIO(b'\x5f\x44\xaa\xbb\xcc\xdd\x43\xee\xff\x99\xff')
-
-        it = cborutil.readindefinitebytestringtoiter(source)
-        self.assertEqual(next(it), b'\xaa\xbb\xcc\xdd')
-        self.assertEqual(next(it), b'\xee\xff\x99')
-
-        with self.assertRaises(StopIteration):
-            next(it)
-
     def testdecodevariouslengths(self):
         for i in (0, 1, 22, 23, 24, 25, 254, 255, 256, 65534, 65535, 65536):
             source = b'x' * i