Allow c functions in tp_new type slot by bschoenmaeckers · Pull Request #8346 · RustPython/RustPython · GitHub
Skip to content
Open
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
10 changes: 9 additions & 1 deletion crates/derive-impl/src/pyclass.rs
6 changes: 5 additions & 1 deletion crates/vm/src/builtins/bool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,11 @@ fn vectorcall_bool(
) -> PyResult {
let zelf: &Py<PyType> = zelf_obj.downcast_ref().unwrap();
let func_args = FuncArgs::from_vectorcall_owned(args, nargs, kwnames);
(zelf.slots.new.load().unwrap())(zelf.to_owned(), func_args, vm)
zelf.slots
.new
.load()
.unwrap()
.invoke(zelf.to_owned(), func_args, vm)
}

pub(crate) fn init(context: &'static Context) {
Expand Down
6 changes: 5 additions & 1 deletion crates/vm/src/builtins/float.rs
Original file line number Diff line number Diff line change
Expand Up @@ -552,7 +552,11 @@ fn vectorcall_float(
) -> PyResult {
let zelf: &Py<PyType> = zelf_obj.downcast_ref().unwrap();
let func_args = FuncArgs::from_vectorcall_owned(args, nargs, kwnames);
(zelf.slots.new.load().unwrap())(zelf.to_owned(), func_args, vm)
zelf.slots
.new
.load()
.unwrap()
.invoke(zelf.to_owned(), func_args, vm)
}

#[rustfmt::skip] // to avoid line splitting
Expand Down
6 changes: 5 additions & 1 deletion crates/vm/src/builtins/int.rs
Original file line number Diff line number Diff line change
Expand Up @@ -840,7 +840,11 @@ fn vectorcall_int(
) -> PyResult {
let zelf: &Py<PyType> = zelf_obj.downcast_ref().unwrap();
let func_args = FuncArgs::from_vectorcall_owned(args, nargs, kwnames);
(zelf.slots.new.load().unwrap())(zelf.to_owned(), func_args, vm)
zelf.slots
.new
.load()
.unwrap()
.invoke(zelf.to_owned(), func_args, vm)
}

pub(crate) fn init(context: &'static Context) {
Expand Down
6 changes: 5 additions & 1 deletion crates/vm/src/builtins/set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1464,7 +1464,11 @@ fn vectorcall_frozenset(
) -> PyResult {
let zelf: &Py<PyType> = zelf_obj.downcast_ref().unwrap();
let func_args = FuncArgs::from_vectorcall_owned(args, nargs, kwnames);
(zelf.slots.new.load().unwrap())(zelf.to_owned(), func_args, vm)
zelf.slots
.new
.load()
.unwrap()
.invoke(zelf.to_owned(), func_args, vm)
}

pub(crate) fn init(context: &'static Context) {
Expand Down
6 changes: 5 additions & 1 deletion crates/vm/src/builtins/str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1778,7 +1778,11 @@ fn vectorcall_str(
) -> PyResult {
let zelf: &Py<PyType> = zelf_obj.downcast_ref().unwrap();
let func_args = FuncArgs::from_vectorcall_owned(args, nargs, kwnames);
(zelf.slots.new.load().unwrap())(zelf.to_owned(), func_args, vm)
zelf.slots
.new
.load()
.unwrap()
.invoke(zelf.to_owned(), func_args, vm)
}

pub(crate) fn init(ctx: &'static Context) {
Expand Down
6 changes: 5 additions & 1 deletion crates/vm/src/builtins/tuple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -665,7 +665,11 @@ fn vectorcall_tuple(
let func_args = FuncArgs::from_vectorcall_owned(args, nargs, kwnames);
// Use the type's own slot_new rather than calling PyTuple::slot_new directly,
// so Rust-level subclasses (e.g. struct sequences) get their custom slot_new called.
(zelf.slots.new.load().unwrap())(zelf.to_owned(), func_args, vm)
zelf.slots
.new
.load()
.unwrap()
.invoke(zelf.to_owned(), func_args, vm)
}

pub(crate) fn init(context: &'static Context) {
Expand Down
12 changes: 6 additions & 6 deletions crates/vm/src/builtins/type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2133,7 +2133,7 @@ impl Constructor for PyType {
let metatype = if !winner.is(&metatype) {
if let Some(ref slot_new) = winner.slots.new.load() {
// Pass it to the winner
return slot_new(winner, args, vm);
return slot_new.invoke(winner, args, vm);
}
winner
} else {
Expand Down Expand Up @@ -2832,14 +2832,14 @@ impl Callable for PyType {
// path incorrectly.
if zelf.slots.init.load().is_none()
&& !zelf.is(vm.ctx.types.type_type)
&& slot_new as usize != crate::types::new_wrapper as crate::types::NewFunc as usize
&& slot_new.identity() != crate::types::new_wrapper as *const () as usize
{
return slot_new(zelf.to_owned(), args, vm);
return slot_new.invoke(zelf.to_owned(), args, vm);
}
args.clone()
};

let obj = slot_new(zelf.to_owned(), args, vm)?;
let obj = slot_new.invoke(zelf.to_owned(), args, vm)?;

if !obj.class().fast_issubclass(zelf) {
return Ok(obj);
Expand Down Expand Up @@ -3069,7 +3069,7 @@ pub(crate) fn call_slot_new(
// Check if staticbase's tp_new differs from typ's tp_new
let typ_new = typ.slots.new.load();
let staticbase_new = staticbase.slots.new.load();
if typ_new.map(|f| f as usize) != staticbase_new.map(|f| f as usize) {
if typ_new.map(|f| f.identity()) != staticbase_new.map(|f| f.identity()) {
return Err(vm.new_type_error(format!(
"{}.__new__({}) is not safe, use {}.__new__()",
typ.slot_name(),
Expand All @@ -3083,7 +3083,7 @@ pub(crate) fn call_slot_new(
.new
.load()
.expect("Should be able to find a new slot somewhere in the mro");
slot_new(subtype, args, vm)
slot_new.invoke(subtype, args, vm)
}

pub(crate) fn or_(zelf: PyObjectRef, other: PyObjectRef, vm: &VirtualMachine) -> PyResult {
Expand Down
2 changes: 1 addition & 1 deletion crates/vm/src/class.rs
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ pub trait PyClassImpl: PyClassDef {
let object_new = ctx.types.object_type.slots.new.load();
let is_object_itself = core::ptr::eq(class, ctx.types.object_type);
let is_inherited_from_object = !is_object_itself
&& object_new.is_some_and(|obj_new| slot_new as usize == obj_new as usize);
&& object_new.is_some_and(|obj_new| slot_new.identity() == obj_new.identity());

if !is_inherited_from_object {
let bound_new =
Expand Down
2 changes: 1 addition & 1 deletion crates/vm/src/frame.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9240,7 +9240,7 @@ impl ExecutingFrame<'_> {
let cls_alloc = cls.slots.alloc.load();
if let (Some(cls_new_fn), Some(obj_new_fn), Some(cls_alloc_fn), Some(obj_alloc_fn)) =
(cls_new, object_new, cls_alloc, object_alloc)
&& cls_new_fn as usize == obj_new_fn as usize
&& cls_new_fn.identity() == obj_new_fn.identity()
&& cls_alloc_fn as usize == obj_alloc_fn as usize
{
if type_version == 0 {
Expand Down
37 changes: 33 additions & 4 deletions crates/vm/src/types/slot.rs
Loading