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
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
755565d2
Commit
755565d2
authored
10 years ago
by
Paul Sokolovsky
Browse files
Options
Downloads
Patches
Plain Diff
py: Support instance __call__ method.
parent
410f3077
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
py/objtype.c
+11
-0
11 additions, 0 deletions
py/objtype.c
py/qstrdefs.h
+1
-0
1 addition, 0 deletions
py/qstrdefs.h
py/runtime.c
+6
-3
6 additions, 3 deletions
py/runtime.c
tests/basics/class_call.py
+18
-0
18 additions, 0 deletions
tests/basics/class_call.py
with
36 additions
and
3 deletions
py/objtype.c
+
11
−
0
View file @
755565d2
...
...
@@ -338,6 +338,16 @@ STATIC mp_obj_t class_subscr(mp_obj_t self_in, mp_obj_t index, mp_obj_t value) {
}
}
STATIC
mp_obj_t
class_call
(
mp_obj_t
self_in
,
uint
n_args
,
uint
n_kw
,
const
mp_obj_t
*
args
)
{
mp_obj_class_t
*
self
=
self_in
;
mp_obj_t
member
=
mp_obj_class_lookup
(
self
->
base
.
type
,
MP_QSTR___call__
);
if
(
member
==
MP_OBJ_NULL
)
{
return
member
;
}
mp_obj_t
meth
=
mp_obj_new_bound_meth
(
member
,
self
);
return
mp_call_function_n_kw
(
meth
,
n_args
,
n_kw
,
args
);
}
/******************************************************************************/
// type object
// - the struct is mp_obj_type_t and is defined in obj.h so const types can be made
...
...
@@ -474,6 +484,7 @@ mp_obj_t mp_obj_new_type(qstr name, mp_obj_t bases_tuple, mp_obj_t locals_dict)
o
->
load_attr
=
class_load_attr
;
o
->
store_attr
=
class_store_attr
;
o
->
subscr
=
class_subscr
;
o
->
call
=
class_call
;
o
->
bases_tuple
=
bases_tuple
;
o
->
locals_dict
=
locals_dict
;
return
o
;
...
...
This diff is collapsed.
Click to expand it.
py/qstrdefs.h
+
1
−
0
View file @
755565d2
...
...
@@ -31,6 +31,7 @@ Q(__repr__)
Q
(
__str__
)
Q
(
__getattr__
)
Q
(
__del__
)
Q
(
__call__
)
Q
(
micropython
)
Q
(
byte_code
)
...
...
This diff is collapsed.
Click to expand it.
py/runtime.c
+
6
−
3
View file @
755565d2
...
...
@@ -481,10 +481,13 @@ mp_obj_t mp_call_function_n_kw(mp_obj_t fun_in, uint n_args, uint n_kw, const mp
// do the call
if
(
type
->
call
!=
NULL
)
{
return
type
->
call
(
fun_in
,
n_args
,
n_kw
,
args
);
}
else
{
nlr_raise
(
mp_obj_new_exception_msg_varg
(
&
mp_type_TypeError
,
"'%s' object is not callable"
,
mp_obj_get_type_str
(
fun_in
)));
mp_obj_t
res
=
type
->
call
(
fun_in
,
n_args
,
n_kw
,
args
);
if
(
res
!=
NULL
)
{
return
res
;
}
}
nlr_raise
(
mp_obj_new_exception_msg_varg
(
&
mp_type_TypeError
,
"'%s' object is not callable"
,
mp_obj_get_type_str
(
fun_in
)));
}
// args contains: fun self/NULL arg(0) ... arg(n_args-2) arg(n_args-1) kw_key(0) kw_val(0) ... kw_key(n_kw-1) kw_val(n_kw-1)
...
...
This diff is collapsed.
Click to expand it.
tests/basics/class_call.py
0 → 100644
+
18
−
0
View file @
755565d2
class
C1
:
def
__call__
(
self
,
val
):
print
(
'
call
'
,
val
)
return
'
item
'
class
C2
:
def
__getattr__
(
self
,
k
):
pass
c1
=
C1
()
print
(
c1
(
1
))
c2
=
C2
()
try
:
print
(
c2
(
1
))
except
TypeError
:
print
(
"
TypeError
"
)
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