8000 better OSError.filename by youknowone · Pull Request #5239 · RustPython/RustPython · GitHub
[go: up one dir, main page]

Skip to content

better OSError.filename #5239

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 19, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

8000
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
more filenames
  • Loading branch information
youknowone committed Apr 19, 2024
commit 997d8f0eca06deb5bd138ba1fd6b4c1a891f4483
54 changes: 38 additions & 16 deletions vm/src/stdlib/io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3730,7 +3730,7 @@ mod _io {
mod fileio {
use super::{Offset, _io::*};
use crate::{
builtins::{PyStr, PyStrRef},
builtins::{PyBaseExceptionRef, PyStr, PyStrRef},
common::crt_fd::Fd,
convert::ToPyException,
function::{ArgBytesLike, ArgMemoryBuffer, OptionalArg, OptionalOption},
Expand Down Expand Up @@ -3947,6 +3947,20 @@ mod fileio {
flags(BASETYPE, HAS_DICT)
)]
impl FileIO {
fn io_error(
zelf: &Py<Self>,
error: std::io::Error,
vm: &VirtualMachine,
) -> PyBaseExceptionRef {
let exc = error.to_pyexception(vm);
if let Ok(name) = zelf.as_object().get_attr("name", vm) {
exc.as_object()
.set_attr("filename", name, vm)
.expect("OSError.filename set must success");
}
exc
}

#[pygetset]
fn closed(&self) -> bool {
self.fd.load() < 0
Expand Down Expand Up @@ -4006,79 +4020,87 @@ mod fileio {
}

#[pymethod]
fn read(&self, read_byte: OptionalSize, vm: &VirtualMachine) -> PyResult<Vec<u8>> {
if !self.mode.load().contains(Mode::READABLE) {
fn read(
zelf: &Py<Self>,
read_byte: OptionalSize,
vm: &VirtualMachine,
) -> PyResult<Vec<u8>> {
if !zelf.mode.load().contains(Mode::READABLE) {
return Err(new_unsupported_operation(
vm,
"File or stream is not readable".to_owned(),
));
}
let mut handle = self.get_fd(vm)?;
let mut handle = zelf.get_fd(vm)?;
let bytes = if let Some(read_byte) = read_byte.to_usize() {
let mut bytes = vec![0; read_byte];
let n = handle
.read(&mut bytes)
.map_err(|err| err.to_pyexception(vm))?;
.map_err(|err| Self::io_error(zelf, err, vm))?;
bytes.truncate(n);
bytes
} else {
let mut bytes = vec![];
handle
.read_to_end(&mut bytes)
.map_err(|err| err.to_pyexception(vm))?;
.map_err(|err| Self::io_error(zelf, err, vm))?;
bytes
};

Ok(bytes)
}

#[pymethod]
fn readinto(&self, obj: ArgMemoryBuffer, vm: &VirtualMachine) -> PyResult<usize> {
if !self.mode.load().contains(Mode::READABLE) {
fn readinto(zelf: &Py<Self>, obj: ArgMemoryBuffer, vm: &VirtualMachine) -> PyResult<usize> {
if !zelf.mode.load().contains(Mode::READABLE) {
return Err(new_unsupported_operation(
vm,
"File or stream is not readable".to_owned(),
));
}

let handle = self.get_fd(vm)?;
let handle = zelf.get_fd(vm)?;

let mut buf = obj.borrow_buf_mut();
let mut f = handle.take(buf.len() as _);
let ret = f.read(&mut buf).map_err(|e| e.to_pyexception(vm))?;
let ret = f
.read(&mut buf)
.map_err(|err| Self::io_error(zelf, err, vm))?;

Ok(ret)
}

#[pymethod]
fn write(&self, obj: ArgBytesLike, vm: &VirtualMachine) -> PyResult<usize> {
if !self.mode.load().contains(Mode::WRITABLE) {
fn write(zelf: &Py<Self>, obj: ArgBytesLike, vm: &VirtualMachine) -> PyResult<usize> {
if !zelf.mode.load().contains(Mode::WRITABLE) {
return Err(new_unsupported_operation(
vm,
"File or stream is not writable".to_owned(),
));
}

let mut handle = self.get_fd(vm)?;
let mut handle = zelf.get_fd(vm)?;

let len = obj
.with_ref(|b| handle.write(b))
.map_err(|err| err.to_pyexception(vm))?;
.map_err(|err| Self::io_error(zelf, err, vm))?;

//return number of bytes written
Ok(len)
}

#[pymethod]
fn close(zelf: PyRef<Self>, vm: &VirtualMachine) -> PyResult<()> {
fn close(zelf: &Py<Self>, vm: &VirtualMachine) -> PyResult<()> {
let res = iobase_close(zelf.as_object(), vm);
if !zelf.closefd.load() {
zelf.fd.store(-1);
return res;
}
let fd = zelf.fd.swap(-1);
if fd >= 0 {
Fd(fd).close().map_err(|e| e.to_pyexception(vm))?;
Fd(fd)
.close()
.map_err(|err| Self::io_error(zelf, err, vm))?;
}
res
}
Expand Down
16 changes: 6 additions & 10 deletions vm/src/stdlib/posix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ pub(crate) fn make_module(vm: &VirtualMachine) -> PyRef<PyModule> {
pub mod module {
use crate::{
builtins::{PyDictRef, PyInt, PyListRef, PyStrRef, PyTupleRef, PyTypeRef},
convert::{IntoPyException, ToPyException, ToPyObject, TryFromObject},
convert::{IntoPyException, ToPyObject, TryFromObject},
function::{Either, KwArgs, OptionalArg},
ospath::{IOErrorBuilder, OsPath, OsPathOrFd},
stdlib::os::{
Expand Down Expand Up @@ -1994,15 +1994,11 @@ pub mod module {
if errno::errno() == 0 {
Ok(None)
} else {
let exc = io::Error::from(Errno::last()).to_pyexception(vm);
if let OsPathOrFd::Path(path) = &path {
exc.as_object().set_attr(
"filename",
vm.ctx.new_str(path.as_path().to_string_lossy()),
vm,
)?;
}
Err(exc)
Err(IOErrorBuilder::with_filename(
&io::Error::from(Errno::last()),
path,
vm,
))
}
} else {
Ok(Some(raw))
Expand Down
0