Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
M
micropython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Analytics
Analytics
Repository
Value Stream
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Commits
Open sidebar
xpstem
micropython
Commits
0e3f29cc
Commit
0e3f29cc
authored
Nov 23, 2015
by
Damien George
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
py: Check that second argument to hasattr is actually a string.
Fixes issue #1623.
parent
a8aa1998
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
12 additions
and
2 deletions
+12
-2
py/modbuiltins.c
py/modbuiltins.c
+2
-2
tests/basics/hasattr1.py
tests/basics/hasattr1.py
+10
-0
No files found.
py/modbuiltins.c
View file @
0e3f29cc
...
@@ -529,14 +529,14 @@ STATIC mp_obj_t mp_builtin_setattr(mp_obj_t base, mp_obj_t attr, mp_obj_t value)
...
@@ -529,14 +529,14 @@ STATIC mp_obj_t mp_builtin_setattr(mp_obj_t base, mp_obj_t attr, mp_obj_t value)
MP_DEFINE_CONST_FUN_OBJ_3
(
mp_builtin_setattr_obj
,
mp_builtin_setattr
);
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
)
{
STATIC
mp_obj_t
mp_builtin_hasattr
(
mp_obj_t
object_in
,
mp_obj_t
attr_in
)
{
assert
(
MP_OBJ_IS_QSTR
(
attr_in
)
);
qstr
attr
=
mp_obj_str_get_qstr
(
attr_in
);
mp_obj_t
dest
[
2
];
mp_obj_t
dest
[
2
];
// TODO: https://docs.python.org/3/library/functions.html?highlight=hasattr#hasattr
// TODO: https://docs.python.org/3/library/functions.html?highlight=hasattr#hasattr
// explicitly says "This is implemented by calling getattr(object, name) and seeing
// explicitly says "This is implemented by calling getattr(object, name) and seeing
// whether it raises an AttributeError or not.", so we should explicitly wrap this
// whether it raises an AttributeError or not.", so we should explicitly wrap this
// in nlr_push and handle exception.
// in nlr_push and handle exception.
mp_load_method_maybe
(
object_in
,
MP_OBJ_QSTR_VALUE
(
attr_in
)
,
dest
);
mp_load_method_maybe
(
object_in
,
attr
,
dest
);
return
mp_obj_new_bool
(
dest
[
0
]
!=
MP_OBJ_NULL
);
return
mp_obj_new_bool
(
dest
[
0
]
!=
MP_OBJ_NULL
);
}
}
...
...
tests/basics/hasattr1.py
View file @
0e3f29cc
...
@@ -27,3 +27,13 @@ c = C()
...
@@ -27,3 +27,13 @@ c = C()
print
(
hasattr
(
c
,
"exists"
))
print
(
hasattr
(
c
,
"exists"
))
# TODO
# TODO
#print(hasattr(c, "doesnt_exist"))
#print(hasattr(c, "doesnt_exist"))
try
:
hasattr
(
1
,
b'123'
)
except
TypeError
:
print
(
'TypeError'
)
try
:
hasattr
(
1
,
123
)
except
TypeError
:
print
(
'TypeError'
)
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a 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