Add __alloc__ for bytearray, add note to tests.

This commit is contained in:
jfh committed 2021-08-10 19:09:22 +03:00
1 parent 530dec5426
commit a136db2917
3 files changed
+23 -3

No files matched your search

+13 -3
View File
@@ -1449,6 +1449,11 @@ class ByteArrayTest(BaseBytesTest, unittest.TestCase):
self.assertEqual(b, b1)
self.assertIs(b, b1)
# RUSTPYTHON:
#
# The second instance of self.assertGreater was replaced with
# self.assertGreaterEqual since, in RustPython, the underlying storage
# is a Vec which doesn't require trailing null byte.
# TODO: RUSTPYTHON
@unittest.expectedFailure
def test_alloc(self):
@@ -1459,10 +1464,15 @@ class ByteArrayTest(BaseBytesTest, unittest.TestCase):
for i in range(100):
b += b"x"
alloc = b.__alloc__()
self.assertGreater(alloc, len(b)) # including trailing null byte
self.assertGreaterEqual(alloc, len(b))
if alloc not in seq:
seq.append(alloc)
# RUSTPYTHON:
#
# The usages of self.assertGreater were replaced with
# self.assertGreaterEqual since, in RustPython, the underlying storage
# is a Vec which doesn't require trailing null byte.
# TODO: RUSTPYTHON
@unittest.expectedFailure
def test_init_alloc(self):
@@ -1475,12 +1485,12 @@ class ByteArrayTest(BaseBytesTest, unittest.TestCase):
self.assertEqual(len(b), len(a))
self.assertLessEqual(len(b), i)
alloc = b.__alloc__()
self.assertGreater(alloc, len(b)) # including trailing null byte
self.assertGreaterEqual(alloc, len(b))
b.__init__(g())
self.assertEqual(list(b), list(range(1, 100)))
self.assertEqual(len(b), 99)
alloc = b.__alloc__()
self.assertGreater(alloc, len(b))
self.assertGreaterEqual(alloc, len(b))
def test_extend(self):
orig = b'hello'
+5
View File
@@ -124,6 +124,11 @@ impl PyByteArray {
self.inner().repr("bytearray(", ")")
}
#[pymethod(magic)]
fn alloc(&self) -> usize {
self.inner().capacity()
}
#[pymethod(name = "__len__")]
fn len(&self) -> usize {
self.borrow_buf().len()
+5
View File
@@ -293,6 +293,11 @@ impl PyBytesInner {
self.elements.len()
}
#[inline]
pub fn capacity(&self) -> usize {
self.elements.capacity()
}
#[inline]
pub fn is_empty(&self) -> bool {
self.elements.is_empty()