aboutsummaryrefslogtreecommitdiff
path: root/py/objtype.c
diff options
context:
space:
mode:
authorDamien George2014-02-06 20:31:44 +0000
committerDamien George2014-02-06 20:31:44 +0000
commit64131f321544a28a193e54b1241c8d2efc9ae313 (patch)
tree119fc794790708c1a4ed77c2be358e6f02dbfe21 /py/objtype.c
parentddaf6c112c22e77f3aff5b98ede077d88e76fc0a (diff)
Add staticmethod and classmethod to builtin namespace.
Diffstat (limited to 'py/objtype.c')
-rw-r--r--py/objtype.c26
1 files changed, 20 insertions, 6 deletions
diff --git a/py/objtype.c b/py/objtype.c
index 6934cb354..6ff18b47b 100644
--- a/py/objtype.c
+++ b/py/objtype.c
@@ -212,10 +212,10 @@ static void class_load_attr(mp_obj_t self_in, qstr attr, mp_obj_t *dest) {
// TODO check that this is the correct place to have this logic
if (MP_OBJ_IS_TYPE(member, &mp_type_staticmethod)) {
// return just the function
- dest[0] = ((mp_obj_staticmethod_t*)member)->fun;
+ dest[0] = ((mp_obj_static_class_method_t*)member)->fun;
} else if (MP_OBJ_IS_TYPE(member, &mp_type_classmethod)) {
// return a bound method, with self being the type of this object
- dest[0] = ((mp_obj_classmethod_t*)member)->fun;
+ dest[0] = ((mp_obj_static_class_method_t*)member)->fun;
dest[1] = mp_obj_get_type(self_in);
} else {
// return a bound method, with self being this object
@@ -304,10 +304,10 @@ static void type_load_attr(mp_obj_t self_in, qstr attr, mp_obj_t *dest) {
// see http://docs.python.org/3.3/howto/descriptor.html
if (MP_OBJ_IS_TYPE(member, &mp_type_staticmethod)) {
// return just the function
- dest[0] = ((mp_obj_staticmethod_t*)member)->fun;
+ dest[0] = ((mp_obj_static_class_method_t*)member)->fun;
} else if (MP_OBJ_IS_TYPE(member, &mp_type_classmethod)) {
// return a bound method, with self being this class
- dest[0] = ((mp_obj_classmethod_t*)member)->fun;
+ dest[0] = ((mp_obj_static_class_method_t*)member)->fun;
dest[1] = self_in;
} else {
// return just the function
@@ -417,10 +417,10 @@ static void super_load_attr(mp_obj_t self_in, qstr attr, mp_obj_t *dest) {
// TODO check that this is the correct place to have this logic
if (MP_OBJ_IS_TYPE(member, &mp_type_staticmethod)) {
// return just the function
- dest[0] = ((mp_obj_staticmethod_t*)member)->fun;
+ dest[0] = ((mp_obj_static_class_method_t*)member)->fun;
} else if (MP_OBJ_IS_TYPE(member, &mp_type_classmethod)) {
// return a bound method, with self being the type of this object
- dest[0] = ((mp_obj_classmethod_t*)member)->fun;
+ dest[0] = ((mp_obj_static_class_method_t*)member)->fun;
dest[1] = mp_obj_get_type(self->obj);
} else {
// return a bound method, with self being this object
@@ -507,12 +507,26 @@ MP_DEFINE_CONST_FUN_OBJ_2(mp_builtin_isinstance_obj, mp_builtin_isinstance);
/******************************************************************************/
// staticmethod and classmethod types (probably should go in a different file)
+static mp_obj_t static_class_method_make_new(mp_obj_t self_in, uint n_args, uint n_kw, const mp_obj_t *args) {
+ assert(self_in == &mp_type_staticmethod || self_in == &mp_type_classmethod);
+
+ if (n_args != 1 || n_kw != 0) {
+ nlr_jump(mp_obj_new_exception_msg_1_arg(MP_QSTR_TypeError, "function takes 1 positional argument but %d were given", (void*)(machine_int_t)n_args));
+ }
+
+ mp_obj_static_class_method_t *o = m_new_obj(mp_obj_static_class_method_t);
+ *o = (mp_obj_static_class_method_t){{(mp_obj_type_t*)self_in}, args[0]};
+ return o;
+}
+
const mp_obj_type_t mp_type_staticmethod = {
{ &mp_const_type },
"staticmethod",
+ .make_new = static_class_method_make_new
};
const mp_obj_type_t mp_type_classmethod = {
{ &mp_const_type },
"classmethod",
+ .make_new = static_class_method_make_new
};