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
e74f52b7
Commit
e74f52b7
authored
11 years ago
by
Paul Sokolovsky
Browse files
Options
Downloads
Patches
Plain Diff
namedtuple: Inherit unary/binary ops from tuple base class.
parent
d86d22e1
No related branches found
No related tags found
No related merge requests found
Changes
4
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
py/objnamedtuple.c
+2
-2
2 additions, 2 deletions
py/objnamedtuple.c
py/objtuple.c
+3
-3
3 additions, 3 deletions
py/objtuple.c
py/objtuple.h
+2
-0
2 additions, 0 deletions
py/objtuple.h
tests/basics/namedtuple1.py
+6
-0
6 additions, 0 deletions
tests/basics/namedtuple1.py
with
13 additions
and
5 deletions
py/objnamedtuple.c
+
2
−
2
View file @
e74f52b7
...
...
@@ -127,8 +127,8 @@ mp_obj_t mp_obj_new_namedtuple_type(qstr name, const char *fields) {
o
->
base
.
name
=
name
;
o
->
base
.
print
=
namedtuple_print
;
o
->
base
.
make_new
=
namedtuple_make_new
;
//
o->base.unary_op = ;
//
o->base.binary_op = ;
o
->
base
.
unary_op
=
tuple_unary_op
;
o
->
base
.
binary_op
=
tuple_binary_op
;
o
->
base
.
load_attr
=
namedtuple_load_attr
;
o
->
base
.
store_attr
=
namedtuple_store_attr
;
o
->
base
.
bases_tuple
=
(
mp_obj_t
)
&
namedtuple_base_tuple
;
...
...
This diff is collapsed.
Click to expand it.
py/objtuple.c
+
3
−
3
View file @
e74f52b7
...
...
@@ -86,7 +86,7 @@ STATIC bool tuple_cmp_helper(int op, mp_obj_t self_in, mp_obj_t another_in) {
return
mp_seq_cmp_objs
(
op
,
self
->
items
,
self
->
len
,
another
->
items
,
another
->
len
);
}
STATIC
mp_obj_t
tuple_unary_op
(
int
op
,
mp_obj_t
self_in
)
{
mp_obj_t
tuple_unary_op
(
int
op
,
mp_obj_t
self_in
)
{
mp_obj_tuple_t
*
self
=
self_in
;
switch
(
op
)
{
case
RT_UNARY_OP_BOOL
:
return
MP_BOOL
(
self
->
len
!=
0
);
...
...
@@ -95,7 +95,7 @@ STATIC mp_obj_t tuple_unary_op(int op, mp_obj_t self_in) {
}
}
STATIC
mp_obj_t
tuple_binary_op
(
int
op
,
mp_obj_t
lhs
,
mp_obj_t
rhs
)
{
mp_obj_t
tuple_binary_op
(
int
op
,
mp_obj_t
lhs
,
mp_obj_t
rhs
)
{
mp_obj_tuple_t
*
o
=
lhs
;
switch
(
op
)
{
case
RT_BINARY_OP_SUBSCR
:
...
...
@@ -116,7 +116,7 @@ STATIC mp_obj_t tuple_binary_op(int op, mp_obj_t lhs, mp_obj_t rhs) {
}
case
RT_BINARY_OP_ADD
:
{
if
(
!
MP_OBJ_IS_TYPE
(
rhs
,
&
tuple_type
))
{
if
(
!
mp_obj_is_subclass_fast
(
mp_obj_get_type
(
rhs
)
,
(
mp_obj_t
)
&
tuple_type
))
{
return
NULL
;
}
mp_obj_tuple_t
*
p
=
rhs
;
...
...
This diff is collapsed.
Click to expand it.
py/objtuple.h
+
2
−
0
View file @
e74f52b7
...
...
@@ -5,3 +5,5 @@ typedef struct _mp_obj_tuple_t {
}
mp_obj_tuple_t
;
void
tuple_print
(
void
(
*
print
)(
void
*
env
,
const
char
*
fmt
,
...),
void
*
env
,
mp_obj_t
o_in
,
mp_print_kind_t
kind
);
mp_obj_t
tuple_unary_op
(
int
op
,
mp_obj_t
self_in
);
mp_obj_t
tuple_binary_op
(
int
op
,
mp_obj_t
lhs
,
mp_obj_t
rhs
);
This diff is collapsed.
Click to expand it.
tests/basics/namedtuple1.py
+
6
−
0
View file @
e74f52b7
...
...
@@ -5,8 +5,14 @@ T = namedtuple("Tup", "foo bar")
#print(T)
t
=
T
(
1
,
2
)
print
(
t
)
print
(
t
[
0
],
t
[
1
])
print
(
t
.
foo
,
t
.
bar
)
print
(
len
(
t
))
print
(
bool
(
t
))
print
(
t
+
t
)
print
(
t
*
3
)
print
(
isinstance
(
t
,
tuple
))
try
:
...
...
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