diff --git a/Lib/test/test_bytes.py b/Lib/test/test_bytes.py index 05ab7bdaa..6ebd7685b 100644 --- a/Lib/test/test_bytes.py +++ b/Lib/test/test_bytes.py @@ -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' diff --git a/vm/src/builtins/bytearray.rs b/vm/src/builtins/bytearray.rs index ba9ac83d9..71aa55835 100644 --- a/vm/src/builtins/bytearray.rs +++ b/vm/src/builtins/bytearray.rs @@ -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() diff --git a/vm/src/bytesinner.rs b/vm/src/bytesinner.rs index 875cd188a..b2fba6d76 100644 --- a/vm/src/bytesinner.rs +++ b/vm/src/bytesinner.rs @@ -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()