From dcb0698c992bc6632b37ec8cfb32e851f5eb6b31 Mon Sep 17 00:00:00 2001 From: Kangzhi Shi Date: Wed, 15 Sep 2021 17:45:28 +0200 Subject: [PATCH] Impl array.tofile --- vm/src/stdlib/array.rs | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/vm/src/stdlib/array.rs b/vm/src/stdlib/array.rs index c82896cdf1..a9a20b7e7a 100644 --- a/vm/src/stdlib/array.rs +++ b/vm/src/stdlib/array.rs @@ -888,6 +888,22 @@ mod array { self.read().get_bytes().to_vec() } + #[pymethod] + fn tofile(&self, f: PyObjectRef, vm: &VirtualMachine) -> PyResult<()> { + /* Write 64K blocks at a time */ + /* XXX Make the block size settable */ + const BLOCKSIZE: usize = 64 * 1024; + + let bytes = self.read(); + let bytes = bytes.get_bytes(); + + for b in bytes.chunks(BLOCKSIZE) { + let b = PyBytes::from(b.to_vec()).into_ref(vm); + vm.call_method(&f, "write", (b,))?; + } + Ok(()) + } + pub(crate) fn get_bytes(&self) -> PyMappedRwLockReadGuard<'_, [u8]> { PyRwLockReadGuard::map(self.read(), |a| a.get_bytes()) }