Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
TASTE
uPython-mirror
Commits
b4efac14
Commit
b4efac14
authored
Jun 08, 2014
by
Paul Sokolovsky
Browse files
py: Make sure getattr() works with non-interned strings (by interning them).
parent
d31a093f
Changes
4
Hide whitespace changes
Inline
Side-by-side
py/builtin.c
View file @
b4efac14
...
...
@@ -452,12 +452,17 @@ 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
(
uint
n_args
,
const
mp_obj_t
*
args
)
{
assert
(
MP_OBJ_IS_QSTR
(
args
[
1
]));
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
(
a
rgs
[
1
]
),
defval
);
return
mp_load_attr_default
(
args
[
0
],
MP_OBJ_QSTR_VALUE
(
a
ttr
),
defval
);
}
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN
(
mp_builtin_getattr_obj
,
2
,
3
,
mp_builtin_getattr
);
...
...
py/obj.h
View file @
b4efac14
...
...
@@ -468,6 +468,7 @@ uint mp_obj_str_get_len(mp_obj_t self_in);
qstr
mp_obj_str_get_qstr
(
mp_obj_t
self_in
);
// use this if you will anyway convert the string to a qstr
const
char
*
mp_obj_str_get_str
(
mp_obj_t
self_in
);
// use this only if you need the string to be null terminated
const
char
*
mp_obj_str_get_data
(
mp_obj_t
self_in
,
uint
*
len
);
mp_obj_t
mp_obj_str_intern
(
mp_obj_t
str
);
void
mp_str_print_quoted
(
void
(
*
print
)(
void
*
env
,
const
char
*
fmt
,
...),
void
*
env
,
const
byte
*
str_data
,
uint
str_len
);
#if MICROPY_PY_BUILTINS_FLOAT
...
...
py/objstr.c
View file @
b4efac14
...
...
@@ -1751,6 +1751,11 @@ mp_obj_t mp_obj_new_str(const char* data, uint len, bool make_qstr_if_not_alread
}
}
mp_obj_t
mp_obj_str_intern
(
mp_obj_t
str
)
{
GET_STR_DATA_LEN
(
str
,
data
,
len
);
return
MP_OBJ_NEW_QSTR
(
qstr_from_strn
((
const
char
*
)
data
,
len
));
}
mp_obj_t
mp_obj_new_bytes
(
const
byte
*
data
,
uint
len
)
{
return
mp_obj_new_str_of_type
(
&
mp_type_bytes
,
data
,
len
);
}
...
...
tests/basics/getattr1.py
View file @
b4efac14
...
...
@@ -15,3 +15,4 @@ print(getattr(a, "var2"))
print
(
getattr
(
a
,
"meth"
)(
5
))
print
(
getattr
(
a
,
"_none_such"
,
123
))
print
(
getattr
(
list
,
"foo"
,
456
))
print
(
getattr
(
a
,
"va"
+
"r2"
))
Write
Preview
Supports
Markdown
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