fix BCDataStream.read_bytes (#5991)
* fix BCDataStream.read_bytes * followup fix BCDataStream.read_bytes: fix TestBCDataStream.test_bytes
This commit is contained in:
@@ -58,8 +58,12 @@ class TestBCDataStream(ElectrumTestCase):
|
|||||||
s.write(b'foobar')
|
s.write(b'foobar')
|
||||||
self.assertEqual(s.read_bytes(3), b'foo')
|
self.assertEqual(s.read_bytes(3), b'foo')
|
||||||
self.assertEqual(s.read_bytes(2), b'ba')
|
self.assertEqual(s.read_bytes(2), b'ba')
|
||||||
self.assertEqual(s.read_bytes(4), b'r')
|
with self.assertRaises(transaction.SerializationError):
|
||||||
self.assertEqual(s.read_bytes(1), b'')
|
s.read_bytes(4)
|
||||||
|
self.assertEqual(s.read_bytes(0), b'')
|
||||||
|
self.assertEqual(s.read_bytes(1), b'r')
|
||||||
|
self.assertEqual(s.read_bytes(0), b'')
|
||||||
|
|
||||||
|
|
||||||
class TestTransaction(ElectrumTestCase):
|
class TestTransaction(ElectrumTestCase):
|
||||||
|
|
||||||
|
|||||||
@@ -272,12 +272,16 @@ class BCDataStream(object):
|
|||||||
self.write(string)
|
self.write(string)
|
||||||
|
|
||||||
def read_bytes(self, length) -> bytes:
|
def read_bytes(self, length) -> bytes:
|
||||||
try:
|
assert length >= 0
|
||||||
result = self.input[self.read_cursor:self.read_cursor+length] # type: bytearray
|
input_len = len(self.input)
|
||||||
|
read_begin = self.read_cursor
|
||||||
|
read_end = read_begin + length
|
||||||
|
if 0 <= read_begin <= input_len and read_end <= input_len:
|
||||||
|
result = self.input[read_begin:read_end] # type: bytearray
|
||||||
self.read_cursor += length
|
self.read_cursor += length
|
||||||
return bytes(result)
|
return bytes(result)
|
||||||
except IndexError:
|
else:
|
||||||
raise SerializationError("attempt to read past end of buffer") from None
|
raise SerializationError('attempt to read past end of buffer')
|
||||||
|
|
||||||
def can_read_more(self) -> bool:
|
def can_read_more(self) -> bool:
|
||||||
if not self.input:
|
if not self.input:
|
||||||
|
|||||||
Reference in New Issue
Block a user