From c1832fd20657e67755a09bde1d277238ccdb043b Mon Sep 17 00:00:00 2001 From: stijn Date: Sat, 14 Feb 2015 17:36:59 +0100 Subject: [PATCH] py: Add setattr builtin. --- py/builtin.h | 1 + py/modbuiltins.c | 15 ++++++++------- py/qstrdefs.h | 1 + tests/basics/setattr1.py | 13 +++++++++++++ 4 files changed, 23 insertions(+), 7 deletions(-) create mode 100644 tests/basics/setattr1.py diff --git a/py/builtin.h b/py/builtin.h index ddd63a7dad..db3ba63785 100644 --- a/py/builtin.h +++ b/py/builtin.h @@ -48,6 +48,7 @@ MP_DECLARE_CONST_FUN_OBJ(mp_builtin_eval_obj); MP_DECLARE_CONST_FUN_OBJ(mp_builtin_exec_obj); MP_DECLARE_CONST_FUN_OBJ(mp_builtin_execfile_obj); MP_DECLARE_CONST_FUN_OBJ(mp_builtin_getattr_obj); +MP_DECLARE_CONST_FUN_OBJ(mp_builtin_setattr_obj); MP_DECLARE_CONST_FUN_OBJ(mp_builtin_globals_obj); MP_DECLARE_CONST_FUN_OBJ(mp_builtin_hasattr_obj); MP_DECLARE_CONST_FUN_OBJ(mp_builtin_hash_obj); diff --git a/py/modbuiltins.c b/py/modbuiltins.c index b876ca32da..594f385f4c 100644 --- a/py/modbuiltins.c +++ b/py/modbuiltins.c @@ -534,20 +534,20 @@ STATIC inline mp_obj_t mp_load_attr_default(mp_obj_t base, qstr attr, mp_obj_t d } STATIC mp_obj_t mp_builtin_getattr(mp_uint_t n_args, const mp_obj_t *args) { - mp_obj_t attr = args[1]; - if (MP_OBJ_IS_TYPE(attr, &mp_type_str)) { - attr = mp_obj_str_intern(attr); - } else if (!MP_OBJ_IS_QSTR(attr)) { - nlr_raise(mp_obj_new_exception_msg(&mp_type_TypeError, "string required")); - } mp_obj_t defval = MP_OBJ_NULL; if (n_args > 2) { defval = args[2]; } - return mp_load_attr_default(args[0], MP_OBJ_QSTR_VALUE(attr), defval); + return mp_load_attr_default(args[0], mp_obj_str_get_qstr(args[1]), defval); } MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_builtin_getattr_obj, 2, 3, mp_builtin_getattr); +STATIC mp_obj_t mp_builtin_setattr(mp_obj_t base, mp_obj_t attr, mp_obj_t value) { + mp_store_attr(base, mp_obj_str_get_qstr(attr), value); + return mp_const_none; +} +MP_DEFINE_CONST_FUN_OBJ_3(mp_builtin_setattr_obj, mp_builtin_setattr); + STATIC mp_obj_t mp_builtin_hasattr(mp_obj_t object_in, mp_obj_t attr_in) { assert(MP_OBJ_IS_QSTR(attr_in)); @@ -637,6 +637,7 @@ STATIC const mp_map_elem_t mp_module_builtins_globals_table[] = { { MP_OBJ_NEW_QSTR(MP_QSTR_execfile), (mp_obj_t)&mp_builtin_execfile_obj }, #endif { MP_OBJ_NEW_QSTR(MP_QSTR_getattr), (mp_obj_t)&mp_builtin_getattr_obj }, + { MP_OBJ_NEW_QSTR(MP_QSTR_setattr), (mp_obj_t)&mp_builtin_setattr_obj }, { MP_OBJ_NEW_QSTR(MP_QSTR_globals), (mp_obj_t)&mp_builtin_globals_obj }, { MP_OBJ_NEW_QSTR(MP_QSTR_hasattr), (mp_obj_t)&mp_builtin_hasattr_obj }, { MP_OBJ_NEW_QSTR(MP_QSTR_hash), (mp_obj_t)&mp_builtin_hash_obj }, diff --git a/py/qstrdefs.h b/py/qstrdefs.h index ffb2bf0321..4a34010a12 100644 --- a/py/qstrdefs.h +++ b/py/qstrdefs.h @@ -185,6 +185,7 @@ Q(float) #endif Q(from_bytes) Q(getattr) +Q(setattr) Q(globals) Q(hasattr) Q(hash) diff --git a/tests/basics/setattr1.py b/tests/basics/setattr1.py new file mode 100644 index 0000000000..acc3299119 --- /dev/null +++ b/tests/basics/setattr1.py @@ -0,0 +1,13 @@ +class A: + + var = 132 + + def __init__(self): + self.var2 = 34 + + +a = A() +setattr(a, "var", 123) +setattr(a, "var2", 56) +print(a.var) +print(a.var2)