mirror of
https://github.com/RustPython/RustPython.git
synced 2026-10-28 18:59:04 +08:00
Add __alloc__ for bytearray, add note to tests.
This commit is contained in:
1 parent
530dec5426
commit
a136db2917
3 files changed
+23
-3
No files matched your search
+13
-3
@@ -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'
|
||||
|
||||
@@ -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()
|
||||
|
||||
@@ -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()
|
||||
|
||||
Reference in new issue
Block a user