10000 IOError with filename · RustPython/RustPython@0d03b73 · GitHub
[go: up one dir, main page]

Skip to content

Commit 0d03b73

Browse files
committed
IOError with filename
1 parent 7cd5088 commit 0d03b73

File tree

1 file changed

+14
-7
lines changed

1 file changed

+14
-7
lines changed

vm/src/stdlib/posix.rs

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ pub(crate) fn make_module(vm: &VirtualMachine) -> PyRef<PyModule> {
2222
pub mod module {
2323
use crate::{
2424
builtins::{PyDictRef, PyInt, PyListRef, PyStrRef, PyTupleRef, PyTypeRef},
25-
convert::{IntoPyException, ToPyObject, TryFromObject},
25+
convert::{IntoPyException, ToPyException, ToPyObject, TryFromObject},
2626
function::{Either, KwArgs, OptionalArg},
2727
ospath::{IOErrorBuilder, OsPath, OsPathOrFd},
2828
stdlib::os::{
@@ -279,7 +279,7 @@ pub mod module {
279279
)
280280
})?;
281281

282-
let metadata = fs::metadata(path.path.clone());
282+
let metadata = fs::metadata(&path.path);
283283

284284
// if it's only checking for F_OK
285285
if flags == AccessFlags::F_OK {
@@ -1982,20 +1982,27 @@ pub mod module {
19821982

19831983
Errno::clear();
19841984
debug_assert_eq!(errno::errno(), 0);
1985-
let raw = match path {
1985+
let raw = match &path {
19861986
OsPathOrFd::Path(path) => {
1987-
let path = CString::new(path.into_bytes())
1988-
.map_err(|_| vm.new_value_error("embedded null character".to_owned()))?;
1987+
let path = path.clone().into_cstring(vm)?;
19891988
unsafe { libc::pathconf(path.as_ptr(), name) }
19901989
}
1991-
OsPathOrFd::Fd(fd) => unsafe { libc::fpathconf(fd, name) },
1990+
OsPathOrFd::Fd(fd) => unsafe { libc::fpathconf(*fd, name) },
19921991
};
19931992

19941993
if raw == -1 {
19951994
if errno::errno() == 0 {
19961995
Ok(None)
19971996
} else {
1998-
Err(io::Error::from(Errno::last()).into_pyexception(vm))
1997+
let exc = io::Error::from(Errno::last()).to_pyexception(vm);
1998+
if let OsPathOrFd::Path(path) = &path {
1999+
exc.as_object().set_attr(
2000+
"filename",
2001+
vm.ctx.new_str(path.as_path().to_string_lossy()),
2002+
vm,
2003+
)?;
2004+
}
2005+
Err(exc)
19992006
}
20002007
} else {
20012008
Ok(Some(raw))

0 commit comments

Comments
 (0)
0