From e95eafe02affe858c3d9f8f2479d189c4076d82a Mon Sep 17 00:00:00 2001 From: Jeong YunWon Date: Sat, 18 Mar 2023 19:05:59 +0900 Subject: [PATCH] clean up io.{String,Bytes}IO --- .cspell.json | 1 + vm/src/stdlib/io.rs | 114 ++++++++++++++++++++------------------------ 2 files changed, 52 insertions(+), 63 deletions(-) diff --git a/.cspell.json b/.cspell.json index 276869671..3f60bb076 100644 --- a/.cspell.json +++ b/.cspell.json @@ -61,6 +61,7 @@ "rsplitn", "rustc", "rustfmt", + "seekfrom", "splitn", "subsec", "timsort", diff --git a/vm/src/stdlib/io.rs b/vm/src/stdlib/io.rs index 2062698b9..630ad653a 100644 --- a/vm/src/stdlib/io.rs +++ b/vm/src/stdlib/io.rs @@ -3098,7 +3098,6 @@ mod _io { } } - #[pyclass(flags(BASETYPE, HAS_DICT), with(PyRef, Constructor))] impl StringIO { fn buffer(&self, vm: &VirtualMachine) -> PyResult> { if !self.closed.load() { @@ -3107,7 +3106,10 @@ mod _io { Err(io_closed_error(vm)) } } + } + #[pyclass(flags(BASETYPE, HAS_DICT), with(Constructor))] + impl StringIO { #[pymethod] fn readable(&self) -> bool { true @@ -3130,34 +3132,28 @@ mod _io { fn close(&self) { self.closed.store(true); } - } - #[pyclass] - impl PyRef { - //write string to underlying vector + // write string to underlying vector #[pymethod] - fn write(self, data: PyStrRef, vm: &VirtualMachine) -> PyResult { + fn write(&self, data: PyStrRef, vm: &VirtualMachine) -> PyResult { let bytes = data.as_str().as_bytes(); - - match self.buffer(vm)?.write(bytes) { - Some(value) => Ok(vm.ctx.new_int(value).into()), - None => Err(vm.new_type_error("Error Writing String".to_owned())), - } + self.buffer(vm)? + .write(bytes) + .ok_or_else(|| vm.new_type_error("Error Writing String".to_owned())) } - //return the entire contents of the underlying + // return the entire contents of the underlying #[pymethod] - fn getvalue(self, vm: &VirtualMachine) -> PyResult { - match String::from_utf8(self.buffer(vm)?.getvalue()) { - Ok(result) => Ok(vm.ctx.new_str(result).into()), - Err(_) => Err(vm.new_value_error("Error Retrieving Value".to_owned())), - } + fn getvalue(&self, vm: &VirtualMachine) -> PyResult { + let bytes = self.buffer(vm)?.getvalue(); + String::from_utf8(bytes) + .map_err(|_| vm.new_value_error("Error Retrieving Value".to_owned())) } - //skip to the jth position + // skip to the jth position #[pymethod] fn seek( - self, + &self, offset: PyObjectRef, how: OptionalArg, vm: &VirtualMachine, @@ -3167,38 +3163,34 @@ mod _io { .map_err(|err| os_err(vm, err)) } - //Read k bytes from the object and return. - //If k is undefined || k == -1, then we read all bytes until the end of the file. - //This also increments the stream position by the value of k + // Read k bytes from the object and return. + // If k is undefined || k == -1, then we read all bytes until the end of the file. + // This also increments the stream position by the value of k #[pymethod] - fn read(self, size: OptionalSize, vm: &VirtualMachine) -> PyResult { - let data = match self.buffer(vm)?.read(size.to_usize()) { - Some(value) => value, - None => Vec::new(), - }; + fn read(&self, size: OptionalSize, vm: &VirtualMachine) -> PyResult { + let data = self.buffer(vm)?.read(size.to_usize()).unwrap_or_default(); let value = String::from_utf8(data) .map_err(|_| vm.new_value_error("Error Retrieving Value".to_owned()))?; - Ok(vm.ctx.new_str(value).into()) + Ok(value) } #[pymethod] - fn tell(self, vm: &VirtualMachine) -> PyResult { + fn tell(&self, vm: &VirtualMachine) -> PyResult { Ok(self.buffer(vm)?.tell()) } #[pymethod] - fn readline(self, size: OptionalSize, vm: &VirtualMachine) -> PyResult { + fn readline(&self, size: OptionalSize, vm: &VirtualMachine) -> PyResult { // TODO size should correspond to the number of characters, at the moments its the number of // bytes. - match String::from_utf8(self.buffer(vm)?.readline(size.to_usize(), vm)?) { - Ok(value) => Ok(value), - Err(_) => Err(vm.new_value_error("Error Retrieving Value".to_owned())), - } + let input = self.buffer(vm)?.readline(size.to_usize(), vm)?; + String::from_utf8(input) + .map_err(|_| vm.new_value_error("Error Retrieving Value".to_owned())) } #[pymethod] - fn truncate(self, pos: OptionalSize, vm: &VirtualMachine) -> PyResult { + fn truncate(&self, pos: OptionalSize, vm: &VirtualMachine) -> PyResult { let mut buffer = self.buffer(vm)?; let pos = pos.try_usize(vm)?; Ok(buffer.truncate(pos)) @@ -3232,7 +3224,6 @@ mod _io { } } - #[pyclass(flags(BASETYPE, HAS_DICT), with(PyRef, Constructor))] impl BytesIO { fn buffer(&self, vm: &VirtualMachine) -> PyResult> { if !self.closed.load() { @@ -3241,7 +3232,10 @@ mod _io { Err(io_closed_error(vm)) } } + } + #[pyclass(flags(BASETYPE, HAS_DICT), with(PyRef, Constructor))] + impl BytesIO { #[pymethod] fn readable(&self) -> bool { true @@ -3254,37 +3248,33 @@ mod _io { fn seekable(&self) -> bool { true } - } - #[pyclass] - impl PyRef { #[pymethod] - fn write(self, data: ArgBytesLike, vm: &VirtualMachine) -> PyResult { + fn write(&self, data: ArgBytesLike, vm: &VirtualMachine) -> PyResult { let mut buffer = self.try_resizable(vm)?; - match data.with_ref(|b| buffer.write(b)) { - Some(value) => Ok(value), - None => Err(vm.new_type_error("Error Writing Bytes".to_owned())), - } + data.with_ref(|b| buffer.write(b)) + .ok_or_else(|| vm.new_type_error("Error Writing Bytes".to_owned())) } - //Retrieves the entire bytes object value from the underlying buffer + // Retrieves the entire bytes object value from the underlying buffer #[pymethod] - fn getvalue(self, vm: &VirtualMachine) -> PyResult { - Ok(vm.ctx.new_bytes(self.buffer(vm)?.getvalue())) + fn getvalue(&self, vm: &VirtualMachine) -> PyResult { + let bytes = self.buffer(vm)?.getvalue(); + Ok(vm.ctx.new_bytes(bytes)) } - //Takes an integer k (bytes) and returns them from the underlying buffer - //If k is undefined || k == -1, then we read all bytes until the end of the file. - //This also increments the stream position by the value of k + // Takes an integer k (bytes) and returns them from the underlying buffer + // If k is undefined || k == -1, then we read all bytes until the end of the file. + // This also increments the stream position by the value of k #[pymethod] #[pymethod(name = "read1")] - fn read(self, size: OptionalSize, vm: &VirtualMachine) -> PyResult> { + fn read(&self, size: OptionalSize, vm: &VirtualMachine) -> PyResult> { let buf = self.buffer(vm)?.read(size.to_usize()).unwrap_or_default(); Ok(buf) } #[pymethod] - fn readinto(self, obj: ArgMemoryBuffer, vm: &VirtualMachine) -> PyResult { + fn readinto(&self, obj: ArgMemoryBuffer, vm: &VirtualMachine) -> PyResult { let mut buf = self.buffer(vm)?; let ret = buf .cursor @@ -3297,7 +3287,7 @@ mod _io { //skip to the jth position #[pymethod] fn seek( - self, + &self, offset: PyObjectRef, how: OptionalArg, vm: &VirtualMachine, @@ -3308,22 +3298,17 @@ mod _io { } #[pymethod] - fn seekable(self) -> bool { - true - } - - #[pymethod] - fn tell(self, vm: &VirtualMachine) -> PyResult { + fn tell(&self, vm: &VirtualMachine) -> PyResult { Ok(self.buffer(vm)?.tell()) } #[pymethod] - fn readline(self, size: OptionalSize, vm: &VirtualMachine) -> PyResult> { + fn readline(&self, size: OptionalSize, vm: &VirtualMachine) -> PyResult> { self.buffer(vm)?.readline(size.to_usize(), vm) } #[pymethod] - fn truncate(self, pos: OptionalSize, vm: &VirtualMachine) -> PyResult { + fn truncate(&self, pos: OptionalSize, vm: &VirtualMachine) -> PyResult { if self.closed.load() { return Err(io_closed_error(vm)); } @@ -3333,17 +3318,20 @@ mod _io { } #[pygetset] - fn closed(self) -> bool { + fn closed(&self) -> bool { self.closed.load() } #[pymethod] - fn close(self, vm: &VirtualMachine) -> PyResult<()> { + fn close(&self, vm: &VirtualMachine) -> PyResult<()> { drop(self.try_resizable(vm)?); self.closed.store(true); Ok(()) } + } + #[pyclass] + impl PyRef { #[pymethod] fn getbuffer(self, vm: &VirtualMachine) -> PyResult { let len = self.buffer.read().cursor.get_ref().len();