Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 7 additions & 21 deletions crates/vm/src/stdlib/posix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3163,7 +3163,7 @@ pub mod module {
#[pymodule(sub, name = "posix")]
mod posix_sched {
use crate::{
AsObject, Py, PyObject, PyObjectRef, PyResult, VirtualMachine,
AsObject, PyObject, PyObjectRef, PyResult, VirtualMachine,
builtins::PyTupleRef,
class::PyClassDef,
convert::{IntoPyException, ToPyObject},
Expand Down Expand Up @@ -3202,26 +3202,12 @@ mod posix_sched {
.map(Into::into)
}

#[extend_class]
fn extend_pyclass(ctx: &crate::vm::Context, class: &'static Py<crate::builtins::PyType>) {
// Override __reduce__ to return (type, (sched_priority,))
// instead of the generic structseq (type, ((sched_priority,),)).
// The trait's extend_class checks contains_key before setting default.
const SCHED_PARAM_REDUCE: crate::function::PyMethodDef =
crate::function::PyMethodDef::new_const(
"__reduce__",
|zelf: crate::PyRef<crate::builtins::PyTuple>,
vm: &VirtualMachine|
-> PyTupleRef {
vm.new_tuple((zelf.class().to_owned(), (zelf.as_slice()[0].clone(),)))
},
crate::function::PyMethodFlags::METHOD,
crate::function::ItemDoc::NONE,
);
class.set_attr(
ctx.intern_str("__reduce__"),
SCHED_PARAM_REDUCE.to_proper_method(class, ctx),
);
#[pymethod]
fn __reduce__(
zelf: crate::PyRef<crate::builtins::PyTuple>,
vm: &VirtualMachine,
) -> PyTupleRef {
vm.new_tuple((zelf.class().to_owned(), (zelf.as_slice()[0].clone(),)))
}
}

Expand Down
15 changes: 6 additions & 9 deletions crates/vm/src/types/structseq.rs
Original file line number Diff line number Diff line change
Expand Up @@ -460,15 +460,12 @@ pub trait PyStructSequence: StaticType + PyClassImpl + Sized + 'static {
.richcompare
.store(Some(struct_sequence_richcompare));

// Default __reduce__: only set if not already overridden by the impl's extend_class.
// This allows struct sequences like sched_param to provide a custom __reduce__
// (equivalent to METH_COEXIST in structseq.c).
if !class.attributes.contains(ctx.intern_str("__reduce__")) {
class.set_attr(
ctx.intern_str("__reduce__"),
DEFAULT_STRUCTSEQ_REDUCE.to_proper_method(class, ctx),
);
}
// An impl's own __reduce__ method replaces this default, because method
// definitions are installed after class extension.
class.set_attr(
ctx.intern_str("__reduce__"),
DEFAULT_STRUCTSEQ_REDUCE.to_proper_method(class, ctx),
);
}
}

Expand Down
Loading