From 83386fda25963a86599ca06803c049da0c52bbaa Mon Sep 17 00:00:00 2001 From: Aviv Palivoda Date: Sat, 29 Feb 2020 18:48:41 +0200 Subject: [PATCH] Set errno on os.listdir --- Lib/test/test_exceptions.py | 1 - vm/src/stdlib/os.rs | 20 ++++++++------------ 2 files changed, 8 insertions(+), 13 deletions(-) diff --git a/Lib/test/test_exceptions.py b/Lib/test/test_exceptions.py index ee4c038b6..746794ce6 100644 --- a/Lib/test/test_exceptions.py +++ b/Lib/test/test_exceptions.py @@ -1201,7 +1201,6 @@ class ExceptionTests(unittest.TestCase): self.fail("RecursionError not raised") self.assertEqual(wr(), None) - @unittest.skip("TODO: RUSTPYTHON") def test_errno_ENOTDIR(self): # Issue #12802: "not a directory" errors are ENOTDIR even on Windows with self.assertRaises(OSError) as cm: diff --git a/vm/src/stdlib/os.rs b/vm/src/stdlib/os.rs index d81ae56c9..801b852de 100644 --- a/vm/src/stdlib/os.rs +++ b/vm/src/stdlib/os.rs @@ -413,18 +413,14 @@ fn os_rmdir(path: PyStringRef, dir_fd: DirFd, vm: &VirtualMachine) -> PyResult<( } fn os_listdir(path: PyStringRef, vm: &VirtualMachine) -> PyResult { - match fs::read_dir(path.as_str()) { - Ok(iter) => { - let res: PyResult> = iter - .map(|entry| match entry { - Ok(path) => Ok(vm.ctx.new_str(path.file_name().into_string().unwrap())), - Err(s) => Err(convert_io_error(vm, s)), - }) - .collect(); - Ok(vm.ctx.new_list(res?)) - } - Err(s) => Err(vm.new_os_error(s.to_string())), - } + let res: PyResult> = fs::read_dir(path.as_str()) + .map_err(|err| convert_io_error(vm, err))? + .map(|entry| match entry { + Ok(path) => Ok(vm.ctx.new_str(path.file_name().into_string().unwrap())), + Err(s) => Err(convert_io_error(vm, s)), + }) + .collect(); + Ok(vm.ctx.new_list(res?)) } fn bytes_as_osstr<'a>(b: &'a [u8], vm: &VirtualMachine) -> PyResult<&'a ffi::OsStr> {