D4914: cborutil: cast bytearray to bytes

indygreg (Gregory Szorc) phabricator at mercurial-scm.org
Wed Oct 10 10:57:27 EDT 2018


This revision was automatically updated to reflect the committed changes.
Closed by commit rHGb638219a23c3: cborutil: cast bytearray to bytes (authored by indygreg, committed by ).

REPOSITORY
  rHG Mercurial

CHANGES SINCE LAST UPDATE
  https://phab.mercurial-scm.org/D4914?vs=11753&id=11778

REVISION DETAIL
  https://phab.mercurial-scm.org/D4914

AFFECTED FILES
  mercurial/utils/cborutil.py
  tests/test-cbor.py

CHANGE DETAILS

diff --git a/tests/test-cbor.py b/tests/test-cbor.py
--- a/tests/test-cbor.py
+++ b/tests/test-cbor.py
@@ -965,6 +965,14 @@
 
             self.assertEqual(decoder.getavailable(), [source])
 
+    def testbytearray(self):
+        source = b''.join(cborutil.streamencode(b'foobar'))
+
+        decoder = cborutil.bufferingdecoder()
+        decoder.decode(bytearray(source))
+
+        self.assertEqual(decoder.getavailable(), [b'foobar'])
+
 class DecodeallTests(TestCase):
     def testemptyinput(self):
         self.assertEqual(cborutil.decodeall(b''), [])
diff --git a/mercurial/utils/cborutil.py b/mercurial/utils/cborutil.py
--- a/mercurial/utils/cborutil.py
+++ b/mercurial/utils/cborutil.py
@@ -925,6 +925,11 @@
         * Integer number of bytes decoded from the new input.
         * Integer number of bytes wanted to decode the next value.
         """
+        # We /might/ be able to support passing a bytearray all the
+        # way through. For now, let's cheat.
+        if isinstance(b, bytearray):
+            b = bytes(b)
+
         # Our strategy for buffering is to aggregate the incoming chunks in a
         # list until we've received enough data to decode the next item.
         # This is slightly more complicated than using an ``io.BytesIO``



To: indygreg, #hg-reviewers
Cc: mercurial-devel


More information about the Mercurial-devel mailing list