Skip to content

Commit 45a6976

Browse files
committed
fix new
1 parent 1c7509a commit 45a6976

2 files changed

Lines changed: 21 additions & 11 deletions

File tree

vm/src/builtins/object.rs

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,15 @@ 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;
119
use crate::utils::Either;
1210
use crate::vm::VirtualMachine;
1311
use crate::{
1412
IdProtocol, ItemProtocol, PyArithmaticValue, PyAttributes, PyClassImpl, PyComparisonValue,
15-
PyContext, PyObject, PyObjectRef, PyResult, PyValue, TypeProtocol,
13+
PyContext, PyObject, PyObjectRef, PyRef, PyResult, PyValue, TypeProtocol,
1614
};
1715

1816
/// object()
@@ -34,11 +32,6 @@ impl PyValue for PyBaseObject {
3432

3533
#[pyimpl(flags(BASETYPE))]
3634
impl PyBaseObject {
37-
#[pyclassmethod(magic)]
38-
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,19 @@ 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);
368+
let class = &ctx.types.object_type;
369+
370+
let new_str = "__new__";
371+
let tp_new_wrapper = PyRef::new_ref(
372+
ctx.make_funcdef(new_str, PyType::__new__).into_function(),
373+
ctx.types.builtin_function_or_method_type.clone(),
374+
None,
375+
)
376+
.into_object();
377+
let bound = ctx.new_bound_method(tp_new_wrapper, class.clone().into_object());
378+
class.set_str_attr(new_str, bound);
375379
}
376380

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

vm/src/builtins/pytype.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,12 @@ impl PyTypeRef {
239239

240240
#[pyimpl(with(SlotGetattro, SlotSetattro, Callable), flags(BASETYPE))]
241241
impl PyType {
242+
// bound method for every type
243+
pub(crate) fn __new__(zelf: PyRef<Self>, args: FuncArgs, vm: &VirtualMachine) -> PyResult {
244+
let (subtype, args): (PyRef<Self>, FuncArgs) = args.bind(vm)?;
245+
call_tp_new(zelf, subtype, args, vm)
246+
}
247+
242248
#[pyproperty(name = "__mro__")]
243249
fn get_mro(zelf: PyRef<Self>) -> PyTuple {
244250
let elements: Vec<PyObjectRef> = zelf.iter_mro().map(|x| x.as_object().clone()).collect();

0 commit comments

Comments
 (0)