# HG changeset patch # User Gregory Szorc # Date 1538690922 25200 # Node ID b638219a23c364f101cb81cc9e97f97d3623951a # Parent 627b0f9baeaf293a7169e303eff957a72e44d6af cborutil: cast bytearray to bytes This code didn't like passing in bytearray instances. Let's cast bytearray to bytes so it works. Differential Revision: https://phab.mercurial-scm.org/D4914 diff -r 627b0f9baeaf -r b638219a23c3 mercurial/utils/cborutil.py --- a/mercurial/utils/cborutil.py Mon Oct 08 17:06:24 2018 -0700 +++ b/mercurial/utils/cborutil.py Thu Oct 04 15:08:42 2018 -0700 @@ -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`` diff -r 627b0f9baeaf -r b638219a23c3 tests/test-cbor.py --- a/tests/test-cbor.py Mon Oct 08 17:06:24 2018 -0700 +++ b/tests/test-cbor.py Thu Oct 04 15:08:42 2018 -0700 @@ -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''), [])