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
7281d95a
Commit
7281d95a
authored
Dec 28, 2015
by
Dave Hylands
Committed by
Paul Sokolovsky
Jan 03, 2016
Browse files
py: Make dir report instance members
parent
b50030b1
Changes
2
Hide whitespace changes
Inline
Side-by-side
py/modbuiltins.c
View file @
7281d95a
...
...
@@ -31,6 +31,7 @@
#include
"py/smallint.h"
#include
"py/objint.h"
#include
"py/objstr.h"
#include
"py/objtype.h"
#include
"py/runtime0.h"
#include
"py/runtime.h"
#include
"py/builtin.h"
...
...
@@ -196,6 +197,7 @@ STATIC mp_obj_t mp_builtin_dir(mp_uint_t n_args, const mp_obj_t *args) {
// TODO make this function more general and less of a hack
mp_obj_dict_t
*
dict
=
NULL
;
mp_map_t
*
members
=
NULL
;
if
(
n_args
==
0
)
{
// make a list of names in the local name space
dict
=
mp_locals_get
();
...
...
@@ -214,6 +216,10 @@ STATIC mp_obj_t mp_builtin_dir(mp_uint_t n_args, const mp_obj_t *args) {
dict
=
type
->
locals_dict
;
}
}
if
(
mp_obj_is_instance_type
(
mp_obj_get_type
(
args
[
0
])))
{
mp_obj_instance_t
*
inst
=
args
[
0
];
members
=
&
inst
->
members
;
}
}
mp_obj_t
dir
=
mp_obj_new_list
(
0
,
NULL
);
...
...
@@ -224,7 +230,13 @@ STATIC mp_obj_t mp_builtin_dir(mp_uint_t n_args, const mp_obj_t *args) {
}
}
}
if
(
members
!=
NULL
)
{
for
(
mp_uint_t
i
=
0
;
i
<
members
->
alloc
;
i
++
)
{
if
(
MP_MAP_SLOT_IS_FILLED
(
members
,
i
))
{
mp_obj_list_append
(
dir
,
members
->
table
[
i
].
key
);
}
}
}
return
dir
;
}
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN
(
mp_builtin_dir_obj
,
0
,
1
,
mp_builtin_dir
);
...
...
tests/basics/builtin_dir.py
View file @
7281d95a
...
...
@@ -7,3 +7,10 @@ print('__name__' in dir())
import
sys
print
(
'platform'
in
dir
(
sys
))
class
Foo
:
def
__init__
(
self
):
self
.
x
=
1
foo
=
Foo
()
print
(
'__init__'
in
dir
(
foo
))
print
(
'x'
in
dir
(
foo
))
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