diff options
author | Damien George <damien.p.george@gmail.com> | 2017-12-12 15:22:03 +1100 |
---|---|---|
committer | Damien George <damien.p.george@gmail.com> | 2017-12-12 16:53:44 +1100 |
commit | c78ef92d787d7bab8acbec69e978037ec2b20d21 (patch) | |
tree | d539242f509ac167ee78f790b8e5424b06f801d8 /py/objobject.c | |
parent | 3c28df16586157b6b80e7437559c36f05a309e24 (diff) |
py/objtype: Refactor object's handling of __new__ to not create 2 objs.
Before this patch, if a user defined the __new__() function for a class
then two instances of that class would be created: once before __new__ is
called and once during the __new__ call (assuming the user creates some
instance, eg using super().__new__, which is most of the time). The first
one was then discarded. This refactor makes it so that a new instance is
only created if the user __new__ function doesn't exist.
Diffstat (limited to 'py/objobject.c')
-rw-r--r-- | py/objobject.c | 9 |
1 files changed, 6 insertions, 3 deletions
diff --git a/py/objobject.c b/py/objobject.c index 49d2ec62e..265fcfbf2 100644 --- a/py/objobject.c +++ b/py/objobject.c @@ -52,9 +52,12 @@ STATIC mp_obj_t object___new__(mp_obj_t cls) { if (!MP_OBJ_IS_TYPE(cls, &mp_type_type) || !mp_obj_is_instance_type((mp_obj_type_t*)MP_OBJ_TO_PTR(cls))) { mp_raise_TypeError("__new__ arg must be a user-type"); } - mp_obj_t o = MP_OBJ_SENTINEL; - mp_obj_t res = mp_obj_instance_make_new(MP_OBJ_TO_PTR(cls), 1, 0, &o); - return res; + // This executes only "__new__" part of instance creation. + // TODO: This won't work well for classes with native bases. + // TODO: This is a hack, should be resolved along the lines of + // https://github.com/micropython/micropython/issues/606#issuecomment-43685883 + const mp_obj_type_t *native_base; + return MP_OBJ_FROM_PTR(mp_obj_new_instance(MP_OBJ_TO_PTR(cls), &native_base)); } STATIC MP_DEFINE_CONST_FUN_OBJ_1(object___new___fun_obj, object___new__); STATIC MP_DEFINE_CONST_STATICMETHOD_OBJ(object___new___obj, MP_ROM_PTR(&object___new___fun_obj)); |