Skip to content

Commit fcf7097

Browse files
committed
more windows impl
1 parent 4f8922d commit fcf7097

File tree

5 files changed

+59
-7
lines changed

5 files changed

+59
-7
lines changed

Lib/test/test_genericpath.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,6 @@ def test_exists_fd(self):
170170
os.close(w)
171171
self.assertFalse(self.pathmodule.exists(r))
172172

173-
@unittest.expectedFailure # TODO: RUSTPYTHON
174173
def test_exists_bool(self):
175174
for fd in False, True:
176175
with self.assertWarnsRegex(RuntimeWarning,

Lib/test/test_os.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2554,7 +2554,6 @@ def test_fchmod(self):
25542554
def test_fchown(self):
25552555
self.check(os.fchown, -1, -1)
25562556

2557-
@unittest.expectedFailure # TODO: RUSTPYTHON; OSError: [Errno 22] Invalid argument: 0
25582557
@unittest.skipUnless(hasattr(os, 'fpathconf'), 'test needs os.fpathconf()')
25592558
def test_fpathconf(self):
25602559
self.assertIn("PC_NAME_MAX", os.pathconf_names)

Lib/test/test_posix.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1349,7 +1349,6 @@ def test_get_and_set_scheduler_and_param(self):
13491349
param = posix.sched_param(sched_priority=-large)
13501350
self.assertRaises(OverflowError, posix.sched_setparam, 0, param)
13511351

1352-
@unittest.expectedFailureIf(sys.platform == 'linux', "TODO: RUSTPYTHON; TypeError: cannot pickle 'sched_param' object")
13531352
@requires_sched
13541353
def test_sched_param(self):
13551354
param = posix.sched_param(1)

crates/vm/src/ospath.rs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
use rustpython_common::crt_fd;
22

33
use crate::{
4-
PyObjectRef, PyResult, VirtualMachine,
4+
AsObject, PyObjectRef, PyResult, VirtualMachine,
55
builtins::{PyBytes, PyStr},
6+
class::StaticType,
67
convert::{IntoPyException, ToPyException, ToPyObject, TryFromObject},
78
function::FsPath,
89
};
@@ -80,6 +81,18 @@ impl PathConverter {
8081
) -> PyResult<OsPathOrFd<'fd>> {
8182
// Handle fd (before __fspath__ check, like CPython)
8283
if let Some(int) = obj.try_index_opt(vm) {
84+
// Warn if bool is used as a file descriptor
85+
if obj
86+
.class()
87+
.is(crate::builtins::bool_::PyBool::static_type())
88+
{
89+
crate::stdlib::warnings::warn(
90+
vm.ctx.exceptions.runtime_warning,
91+
"bool is used as a file descriptor".to_owned(),
92+
1,
93+
vm,
94+
)?;
95+
}
8396
let fd = int?.try_to_primitive(vm)?;
8497
return unsafe { crt_fd::Borrowed::try_borrow_raw(fd) }
8598
.map(OsPathOrFd::Fd)

crates/vm/src/stdlib/posix.rs

Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ pub fn set_inheritable(fd: BorrowedFd<'_>, inheritable: bool) -> nix::Result<()>
1919
pub mod module {
2020
use crate::{
2121
AsObject, Py, PyObjectRef, PyPayload, PyResult, VirtualMachine,
22-
builtins::{PyDictRef, PyInt, PyListRef, PyStr, PyStrRef, PyTupleRef, PyType},
22+
builtins::{PyDictRef, PyInt, PyListRef, PyStr, PyTupleRef, PyType},
2323
convert::{IntoPyException, ToPyObject, TryFromObject},
2424
exceptions::OSErrorBuilder,
2525
function::{Either, KwArgs, OptionalArg},
@@ -29,8 +29,14 @@ pub mod module {
2929
warn_if_bool_fd,
3030
},
3131
types::{Constructor, Representable},
32-
utils::ToCString,
3332
};
33+
#[cfg(any(
34+
target_os = "android",
35+
target_os = "freebsd",
36+
target_os = "linux",
37+
target_os = "openbsd"
38+
))]
39+
use crate::{builtins::PyStrRef, utils::ToCString};
3440
use alloc::ffi::CString;
3541
use bitflags::bitflags;
3642
use core::ffi::CStr;
@@ -281,6 +287,7 @@ pub mod module {
281287

282288
impl TryFromObject for BorrowedFd<'_> {
283289
fn try_from_object(vm: &VirtualMachine, obj: PyObjectRef) -> PyResult<Self> {
290+
crate::stdlib::os::warn_if_bool_fd(&obj, vm)?;
284291
let fd = i32::try_from_object(vm, obj)?;
285292
if fd == -1 {
286293
return Err(io::Error::from_raw_os_error(libc::EBADF).into_pyexception(vm));
@@ -904,6 +911,37 @@ pub mod module {
904911
self.sched_priority.clone().to_pyobject(vm)
905912
}
906913

914+
#[pymethod]
915+
fn __reduce__(zelf: crate::PyRef<Self>, vm: &VirtualMachine) -> PyTupleRef {
916+
vm.new_tuple((zelf.class().to_owned(), (zelf.sched_priority.clone(),)))
917+
}
918+
919+
#[pymethod]
920+
fn __replace__(
921+
zelf: crate::PyRef<Self>,
922+
args: crate::function::FuncArgs,
923+
vm: &VirtualMachine,
924+
) -> PyResult<Self> {
925+
if !args.args.is_empty() {
926+
return Err(
927+
vm.new_type_error("__replace__() takes no positional arguments".to_owned())
928+
);
929+
}
930+
let sched_priority = match args.kwargs.get("sched_priority") {
931+
Some(v) => v.clone(),
932+
None => zelf.sched_priority.clone(),
933+
};
934+
// Check for unexpected keyword arguments
935+
for key in args.kwargs.keys() {
936+
if key.as_str() != "sched_priority" {
937+
return Err(vm.new_type_error(format!(
938+
"__replace__() got an unexpected keyword argument '{key}'"
939+
)));
940+
}
941+
}
942+
Ok(Self { sched_priority })
943+
}
944+
907945
#[cfg(any(
908946
target_os = "linux",
909947
target_os = "netbsd",
@@ -2091,7 +2129,11 @@ pub mod module {
20912129
let i = match obj.downcast::<PyInt>() {
20922130
Ok(int) => int.try_to_primitive(vm)?,
20932131
Err(obj) => {
2094-
let s = PyStrRef::try_from_object(vm, obj)?;
2132+
let s = obj.downcast::<PyStr>().map_err(|_| {
2133+
vm.new_type_error(
2134+
"configuration names must be strings or integers".to_owned(),
2135+
)
2136+
})?;
20952137
s.as_str()
20962138
.parse::<PathconfVar>()
20972139
.map_err(|_| vm.new_value_error("unrecognized configuration name"))?

0 commit comments

Comments
 (0)