py: Initial attempts to actually allow implementing __new__ in Python.

Caveat is that __new__ should recurse to base class __new__, and ultimately,
object.__new__ is what handles instance allocation.
This commit is contained in:
Paul Sokolovsky
2014-05-22 00:32:00 +03:00
parent 0c937fa25a
commit 806ea1f6ca
4 changed files with 64 additions and 2 deletions

View File

@@ -33,6 +33,8 @@
#include "obj.h"
#include "runtime0.h"
mp_obj_t instance_make_new(mp_obj_t self_in, uint n_args, uint n_kw, const mp_obj_t *args);
typedef struct _mp_obj_object_t {
mp_obj_base_t base;
} mp_obj_object_t;
@@ -52,12 +54,23 @@ STATIC mp_obj_t object___init__(mp_obj_t self) {
return mp_const_none;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_1(object___init___obj, object___init__);
STATIC mp_obj_t object___new__(mp_obj_t cls) {
mp_obj_t o = MP_OBJ_SENTINEL;
mp_obj_t res = instance_make_new(cls, 1, 0, &o);
return res;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_1(object___new___fun_obj, object___new__);
STATIC MP_DEFINE_CONST_STATICMETHOD_OBJ(object___new___obj, (const mp_obj_t)&object___new___fun_obj);
#endif
STATIC const mp_map_elem_t object_locals_dict_table[] = {
#if MICROPY_CPYTHON_COMPAT
{ MP_OBJ_NEW_QSTR(MP_QSTR___init__), (mp_obj_t)&object___init___obj },
#endif
#if MICROPY_CPYTHON_COMPAT
{ MP_OBJ_NEW_QSTR(MP_QSTR___new__), (mp_obj_t)&object___new___obj },
#endif
};
STATIC MP_DEFINE_CONST_DICT(object_locals_dict, object_locals_dict_table);