From 3397d56f580a7a40aec6d6d423f4bd84df69dee7 Mon Sep 17 00:00:00 2001 From: Kangzhi Shi Date: Sun, 13 Sep 2020 09:50:03 +0200 Subject: [PATCH] Fix bytearray append --- Lib/test/test_bytes.py | 2 -- vm/src/bytesinner.rs | 6 ++++++ vm/src/obj/objbytearray.rs | 8 ++------ 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/Lib/test/test_bytes.py b/Lib/test/test_bytes.py index f6a1b3cfd..25c81ff7a 100644 --- a/Lib/test/test_bytes.py +++ b/Lib/test/test_bytes.py @@ -1507,8 +1507,6 @@ class ByteArrayTest(BaseBytesTest, unittest.TestCase): def test_nosort(self): self.assertRaises(AttributeError, lambda: bytearray().sort()) - # TODO: RUSTPYTHON - @unittest.expectedFailure def test_append(self): b = bytearray(b'hell') b.append(ord('o')) diff --git a/vm/src/bytesinner.rs b/vm/src/bytesinner.rs index c1b8670e1..2a653e7ea 100644 --- a/vm/src/bytesinner.rs +++ b/vm/src/bytesinner.rs @@ -436,6 +436,12 @@ impl PyBytesInner { Ok(()) } + pub fn append(&mut self, object: PyObjectRef, vm: &VirtualMachine) -> PyResult<()> { + let value = Self::value_try_from_object(vm, object)?; + self.elements.push(value); + Ok(()) + } + pub fn isalnum(&self) -> bool { !self.elements.is_empty() && self diff --git a/vm/src/obj/objbytearray.rs b/vm/src/obj/objbytearray.rs index 369f36609..abbc1617c 100644 --- a/vm/src/obj/objbytearray.rs +++ b/vm/src/obj/objbytearray.rs @@ -1,7 +1,6 @@ //! Implementation of the python bytearray object. use bstr::ByteSlice; use crossbeam_utils::atomic::AtomicCell; -use num_traits::cast::ToPrimitive; use std::mem::size_of; use super::objint::PyIntRef; @@ -511,11 +510,8 @@ impl PyByteArray { } #[pymethod(name = "append")] - fn append(&self, x: PyIntRef, vm: &VirtualMachine) -> PyResult<()> { - self.borrow_value_mut() - .elements - .push(x.borrow_value().byte_or(vm)?); - Ok(()) + fn append(&self, value: PyObjectRef, vm: &VirtualMachine) -> PyResult<()> { + self.borrow_value_mut().append(value, vm) } #[pymethod(name = "extend")]