aboutsummaryrefslogtreecommitdiff
path: root/py/objtype.c
diff options
context:
space:
mode:
authorPaul Sokolovsky2014-05-22 00:32:00 +0300
committerPaul Sokolovsky2014-05-22 00:32:00 +0300
commit806ea1f6ca2b1b50bb4634be6c39ad83d8af7e89 (patch)
tree6ff486c9fc6e76cf2e6b984d01a1f28b55432149 /py/objtype.c
parent0c937fa25a1a78274ba974a5c1546c0a01106c30 (diff)
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.
Diffstat (limited to 'py/objtype.c')
-rw-r--r--py/objtype.c12
1 files changed, 10 insertions, 2 deletions
diff --git a/py/objtype.c b/py/objtype.c
index 5da49a045..1d4f90819 100644
--- a/py/objtype.c
+++ b/py/objtype.c
@@ -51,7 +51,7 @@
#define is_instance_type(type) ((type)->make_new == instance_make_new)
#define is_native_type(type) ((type)->make_new != instance_make_new)
-STATIC mp_obj_t instance_make_new(mp_obj_t self_in, uint n_args, uint n_kw, const mp_obj_t *args);
+mp_obj_t instance_make_new(mp_obj_t self_in, uint n_args, uint n_kw, const mp_obj_t *args);
STATIC void instance_convert_return_attr(mp_obj_t self, const mp_obj_type_t *type, mp_obj_t member, mp_obj_t *dest);
STATIC mp_obj_t mp_obj_new_instance(mp_obj_t class, uint subobjs) {
@@ -208,7 +208,7 @@ STATIC void instance_print(void (*print)(void *env, const char *fmt, ...), void
print(env, "<%s object at %p>", mp_obj_get_type_str(self_in), self_in);
}
-STATIC mp_obj_t instance_make_new(mp_obj_t self_in, uint n_args, uint n_kw, const mp_obj_t *args) {
+mp_obj_t instance_make_new(mp_obj_t self_in, uint n_args, uint n_kw, const mp_obj_t *args) {
assert(MP_OBJ_IS_TYPE(self_in, &mp_type_type));
mp_obj_type_t *self = self_in;
@@ -218,6 +218,14 @@ STATIC mp_obj_t instance_make_new(mp_obj_t self_in, uint n_args, uint n_kw, cons
mp_obj_instance_t *o = mp_obj_new_instance(self_in, num_native_bases);
+ // This executes only "__new__" part of obejection creation.
+ // TODO: This won't work will for classes with native bases.
+ // TODO: This is hack, should be resolved along the lines of
+ // https://github.com/micropython/micropython/issues/606#issuecomment-43685883
+ if (n_args == 1 && *args == MP_OBJ_SENTINEL) {
+ return o;
+ }
+
// look for __new__ function
mp_obj_t init_fn[2] = {MP_OBJ_NULL};
mp_obj_class_lookup(NULL, self, MP_QSTR___new__, offsetof(mp_obj_type_t, make_new), init_fn);