diff --git a/Lib/test/test_httplib.py b/Lib/test/test_httplib.py index 8d5b7f426..19a602e52 100644 --- a/Lib/test/test_httplib.py +++ b/Lib/test/test_httplib.py @@ -1652,6 +1652,7 @@ class PersistenceTest(TestCase): self.assertEqual(conn.connections, 2) +@unittest.skip("TODO: RUSTPYTHON") class HTTPSTest(TestCase): def setUp(self): diff --git a/Lib/test/test_os.py b/Lib/test/test_os.py index adaf3b9e3..ddd6c0ca0 100644 --- a/Lib/test/test_os.py +++ b/Lib/test/test_os.py @@ -1928,8 +1928,6 @@ class TestInvalidFD(unittest.TestCase): def test_fchmod(self): self.check(os.fchmod, 0) - # TODO: RUSTPYTHON (AttributeError: 'OSError' object has no attribute 'errno') - @unittest.expectedFailure @unittest.skipUnless(hasattr(os, 'fchown'), 'test needs os.fchown()') def test_fchown(self): self.check(os.fchown, -1, -1) diff --git a/vm/src/stdlib/os.rs b/vm/src/stdlib/os.rs index 6d9db6380..e403a2f26 100644 --- a/vm/src/stdlib/os.rs +++ b/vm/src/stdlib/os.rs @@ -121,50 +121,57 @@ impl AsRef for PyPathLike { } } +fn fspath(obj: PyObjectRef, check_for_nul: bool, vm: &VirtualMachine) -> PyResult { + let check_nul = |b: &[u8]| { + if !check_for_nul || memchr::memchr(b'\0', b).is_none() { + Ok(()) + } else { + Err(vm.new_value_error("embedded null character".to_owned())) + } + }; + let match1 = |obj: &PyObjectRef| { + let pathlike = match_class!(match obj { + ref s @ PyStr => { + let s = s.borrow_value(); + check_nul(s.as_bytes())?; + PyPathLike { + path: s.into(), + mode: OutputMode::String, + } + } + ref b @ PyBytes => { + check_nul(&b)?; + PyPathLike { + path: bytes_as_osstr(&b, vm)?.to_os_string().into(), + mode: OutputMode::Bytes, + } + } + _ => return Ok(None), + }); + Ok(Some(pathlike)) + }; + if let Some(pathlike) = match1(&obj)? { + return Ok(pathlike); + } + let method = vm.get_method_or_type_error(obj.clone(), "__fspath__", || { + format!( + "expected str, bytes or os.PathLike object, not '{}'", + obj.class().name + ) + })?; + let result = vm.invoke(&method, ())?; + match1(&result)?.ok_or_else(|| { + vm.new_type_error(format!( + "expected {}.__fspath__() to return str or bytes, not '{}'", + obj.class().name, + result.class().name, + )) + }) +} + impl TryFromObject for PyPathLike { fn try_from_object(vm: &VirtualMachine, obj: PyObjectRef) -> PyResult { - let check_nul = |b: &[u8]| match memchr::memchr(b'\0', b) { - None => Ok(()), - Some(_) => Err(vm.new_value_error("embedded null character".to_owned())), - }; - let match1 = |obj: &PyObjectRef| { - let pathlike = match_class!(match obj { - ref s @ PyStr => { - let s = s.borrow_value(); - check_nul(s.as_bytes())?; - PyPathLike { - path: s.into(), - mode: OutputMode::String, - } - } - ref b @ PyBytes => { - check_nul(&b)?; - PyPathLike { - path: bytes_as_osstr(&b, vm)?.to_os_string().into(), - mode: OutputMode::Bytes, - } - } - _ => return Ok(None), - }); - Ok(Some(pathlike)) - }; - if let Some(pathlike) = match1(&obj)? { - return Ok(pathlike); - } - let method = vm.get_method_or_type_error(obj.clone(), "__fspath__", || { - format!( - "expected str, bytes or os.PathLike object, not '{}'", - obj.class().name - ) - })?; - let result = vm.invoke(&method, ())?; - match1(&result)?.ok_or_else(|| { - vm.new_type_error(format!( - "expected {}.__fspath__() to return str or bytes, not '{}'", - obj.class().name, - result.class().name, - )) - }) + fspath(obj, true, vm) } } @@ -1103,7 +1110,8 @@ mod _os { } #[pyfunction] - fn fspath(path: PyPathLike, vm: &VirtualMachine) -> PyResult { + fn fspath(path: PyObjectRef, vm: &VirtualMachine) -> PyResult { + let path = super::fspath(path, false, vm)?; path.mode.process_path(path.path, vm) }