Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
TASTE
uPython-mirror
Commits
c1832fd2
Commit
c1832fd2
authored
Feb 14, 2015
by
stijn
Committed by
Damien George
Feb 14, 2015
Browse files
py: Add setattr builtin.
parent
aa730620
Changes
4
Hide whitespace changes
Inline
Side-by-side
py/builtin.h
View file @
c1832fd2
...
...
@@ -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
);
...
...
py/modbuiltins.c
View file @
c1832fd2
...
...
@@ -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
},
...
...
py/qstrdefs.h
View file @
c1832fd2
...
...
@@ -185,6 +185,7 @@ Q(float)
#endif
Q
(
from_bytes
)
Q
(
getattr
)
Q
(
setattr
)
Q
(
globals
)
Q
(
hasattr
)
Q
(
hash
)
...
...
tests/basics/setattr1.py
0 → 100644
View file @
c1832fd2
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
)
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment