Skip to content

Commit 00b33f8

Browse files
committed
fix __new__ as bound method for every type
1 parent 897a7b0 commit 00b33f8

File tree

3 files changed

+10
-12
lines changed

3 files changed

+10
-12
lines changed

vm/src/builtins/object.rs

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@ use super::dict::{PyDict, PyDictRef};
22
use super::list::PyList;
33
use super::pybool;
44
use super::pystr::{PyStr, PyStrRef};
5-
use super::pytype::PyTypeRef;
6-
use crate::builtins::pytype;
7-
use crate::builtins::pytype::PyType;
5+
use super::pytype::{PyType, PyTypeRef};
86
use crate::common::hash::PyHash;
97
use crate::function::FuncArgs;
108
use crate::slots::PyComparisonOp;
@@ -34,11 +32,6 @@ impl PyValue for PyBaseObject {
3432

3533
#[pyimpl(flags(BASETYPE))]
3634
impl PyBaseObject {
37-
#[pyclassmethod(magic)]
38-
pub(crate) fn new(cls: PyTypeRef, args: FuncArgs, vm: &VirtualMachine) -> PyResult {
39-
let (subtype, args): (PyTypeRef, FuncArgs) = args.bind(vm)?;
40-
pytype::call_tp_new(cls, subtype, args, vm)
41-
}
4235
/// Create and return a new object. See help(type) for accurate signature.
4336
#[pyslot]
4437
fn tp_new(cls: PyTypeRef, _args: FuncArgs, vm: &VirtualMachine) -> PyResult {
@@ -370,8 +363,8 @@ pub(crate) fn setattr(
370363
}
371364
}
372365

373-
pub fn init(context: &PyContext) {
374-
PyBaseObject::extend_class(context, &context.types.object_type);
366+
pub fn init(ctx: &PyContext) {
367+
PyBaseObject::extend_class(ctx, &ctx.types.object_type);
375368
}
376369

377370
fn common_reduce(obj: PyObjectRef, proto: usize, vm: &VirtualMachine) -> PyResult {

vm/src/builtins/pytype.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ impl PyTypeRef {
239239

240240
#[pyimpl(with(SlotGetattro, SlotSetattro, Callable), flags(BASETYPE))]
241241
impl PyType {
242-
#[pymethod]
242+
// bound method for every type
243243
pub(crate) fn __new__(zelf: PyRef<Self>, args: FuncArgs, vm: &VirtualMachine) -> PyResult {
244244
let (subtype, args): (PyRef<Self>, FuncArgs) = args.bind(vm)?;
245245
if !subtype.issubclass(&zelf) {

vm/src/pyobject.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ impl PyContext {
126126

127127
let new_str = PyRef::new_ref(pystr::PyStr::from("__new__"), types.str_type.clone(), None);
128128
let tp_new_wrapper = create_object(
129-
PyNativeFuncDef::new(object::PyBaseObject::new.into_func(), new_str).into_function(),
129+
PyNativeFuncDef::new(PyType::__new__.into_func(), new_str).into_function(),
130130
&types.builtin_function_or_method_type,
131131
)
132132
.into_object();
@@ -1095,6 +1095,11 @@ pub trait PyClassImpl: PyClassDef {
10951095
if let Some(module_name) = Self::MODULE_NAME {
10961096
class.set_str_attr("__module__", ctx.new_str(module_name));
10971097
}
1098+
if class.slots.new.load().is_some() {
1099+
let bound =
1100+
ctx.new_bound_method(ctx.tp_new_wrapper.clone(), class.clone().into_object());
1101+
class.set_str_attr("__new__", bound);
1102+
}
10981103
}
10991104

11001105
fn make_class(ctx: &PyContext) -> PyTypeRef

0 commit comments

Comments
 (0)