Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
M
micropython
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container registry
Model registry
Operate
Environments
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
This is an archived project. Repository and other project resources are read-only.
Show more breadcrumbs
card10
micropython
Commits
1a7403bb
Commit
1a7403bb
authored
11 years ago
by
Paul Sokolovsky
Browse files
Options
Downloads
Patches
Plain Diff
objtype: Implement ->getiter() method for instances.
Includes support for native bases.
parent
0bc15941
No related branches found
No related tags found
No related merge requests found
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
py/objtype.c
+23
-0
23 additions, 0 deletions
py/objtype.c
tests/basics/subclass_native_specmeth.py
+18
-0
18 additions, 0 deletions
tests/basics/subclass_native_specmeth.py
with
41 additions
and
0 deletions
py/objtype.c
+
23
−
0
View file @
1a7403bb
...
...
@@ -501,6 +501,28 @@ STATIC mp_obj_t instance_call(mp_obj_t self_in, uint n_args, uint n_kw, const mp
return
mp_call_function_n_kw
(
meth
,
n_args
,
n_kw
,
args
);
}
STATIC
mp_obj_t
instance_getiter
(
mp_obj_t
self_in
)
{
mp_obj_instance_t
*
self
=
self_in
;
mp_obj_t
member
[
2
]
=
{
MP_OBJ_NULL
};
mp_obj_class_lookup
(
self
,
self
->
base
.
type
,
MP_QSTR___iter__
,
offsetof
(
mp_obj_type_t
,
getiter
),
member
);
if
(
member
[
0
]
==
MP_OBJ_NULL
)
{
// This kinda duplicates code in mp_getiter()
mp_obj_class_lookup
(
self
,
self
->
base
.
type
,
MP_QSTR___getitem__
,
0
,
member
);
if
(
member
[
0
]
!=
MP_OBJ_NULL
)
{
// __getitem__ exists, create an iterator
instance_convert_return_attr
(
self_in
,
member
[
0
],
member
);
return
mp_obj_new_getitem_iter
(
member
);
}
return
MP_OBJ_NULL
;
}
if
(
member
[
0
]
==
MP_OBJ_SENTINEL
)
{
mp_obj_type_t
*
type
=
mp_obj_get_type
(
self
->
subobj
[
0
]);
return
type
->
getiter
(
self
->
subobj
[
0
]);
}
mp_obj_t
meth
=
mp_obj_new_bound_meth
(
member
[
0
],
self
);
return
mp_call_function_n_kw
(
meth
,
0
,
0
,
NULL
);
}
/******************************************************************************/
// type object
// - the struct is mp_obj_type_t and is defined in obj.h so const types can be made
...
...
@@ -655,6 +677,7 @@ mp_obj_t mp_obj_new_type(qstr name, mp_obj_t bases_tuple, mp_obj_t locals_dict)
o
->
store_attr
=
instance_store_attr
;
o
->
subscr
=
instance_subscr
;
o
->
call
=
instance_call
;
o
->
getiter
=
instance_getiter
;
o
->
bases_tuple
=
bases_tuple
;
o
->
locals_dict
=
locals_dict
;
...
...
This diff is collapsed.
Click to expand it.
tests/basics/subclass_native_specmeth.py
0 → 100644
+
18
−
0
View file @
1a7403bb
# Test calling non-special method inherited from native type
class
mylist
(
list
):
pass
l
=
mylist
([
1
,
2
,
3
])
print
(
l
)
print
([
e
for
e
in
l
])
class
mylist2
(
list
):
def
__iter__
(
self
):
return
iter
([
10
,
20
,
30
])
l
=
mylist2
([
1
,
2
,
3
])
print
(
l
)
print
([
e
for
e
in
l
])
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
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!
Save comment
Cancel
Please
register
or
sign in
to comment