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
021dc440
Commit
021dc440
authored
10 years ago
by
stijn
Committed by
Paul Sokolovsky
10 years ago
Browse files
Options
Downloads
Patches
Plain Diff
py: Allow keyword arguments for namedtuple
parent
12340147
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/objnamedtuple.c
+32
-2
32 additions, 2 deletions
py/objnamedtuple.c
tests/basics/namedtuple1.py
+20
-10
20 additions, 10 deletions
tests/basics/namedtuple1.py
with
52 additions
and
12 deletions
py/objnamedtuple.c
+
32
−
2
View file @
021dc440
...
@@ -89,9 +89,39 @@ STATIC mp_obj_t namedtuple_make_new(mp_obj_t type_in, mp_uint_t n_args, mp_uint_
...
@@ -89,9 +89,39 @@ STATIC mp_obj_t namedtuple_make_new(mp_obj_t type_in, mp_uint_t n_args, mp_uint_
// Counts include implicit "self"
// Counts include implicit "self"
nlr_raise
(
mp_obj_new_exception_msg_varg
(
&
mp_type_TypeError
,
nlr_raise
(
mp_obj_new_exception_msg_varg
(
&
mp_type_TypeError
,
"__new__() takes %d positional arguments but %d were given"
,
"__new__() takes %d positional arguments but %d were given"
,
num_fields
+
1
,
n_args
+
1
));
num_fields
+
1
,
n_args
+
n_kw
+
1
));
}
}
mp_obj_tuple_t
*
tuple
=
mp_obj_new_tuple
(
n_args
,
args
);
mp_obj_t
*
arg_objects
;
if
(
n_args
==
num_fields
)
{
arg_objects
=
(
mp_obj_t
*
)
args
;
}
else
{
size_t
alloc_size
=
sizeof
(
mp_obj_t
)
*
num_fields
;
arg_objects
=
alloca
(
alloc_size
);
memset
(
arg_objects
,
0
,
alloc_size
);
for
(
mp_uint_t
i
=
0
;
i
<
n_args
;
i
++
)
{
arg_objects
[
i
]
=
args
[
i
];
}
for
(
mp_uint_t
i
=
n_args
;
i
<
n_args
+
2
*
n_kw
;
i
+=
2
)
{
qstr
kw
=
MP_OBJ_QSTR_VALUE
(
args
[
i
]);
int
id
=
namedtuple_find_field
(
type
,
kw
);
if
(
id
==
-
1
)
{
nlr_raise
(
mp_obj_new_exception_msg_varg
(
&
mp_type_TypeError
,
"__new__() got an unexpected keyword argument '%s'"
,
qstr_str
(
kw
)));
}
if
(
arg_objects
[
id
]
!=
NULL
)
{
nlr_raise
(
mp_obj_new_exception_msg_varg
(
&
mp_type_TypeError
,
"__new__() got multiple values for argument '%s'"
,
qstr_str
(
kw
)));
}
arg_objects
[
id
]
=
args
[
i
+
1
];
}
}
mp_obj_tuple_t
*
tuple
=
mp_obj_new_tuple
(
num_fields
,
arg_objects
);
tuple
->
base
.
type
=
type_in
;
tuple
->
base
.
type
=
type_in
;
return
tuple
;
return
tuple
;
}
}
...
...
This diff is collapsed.
Click to expand it.
tests/basics/namedtuple1.py
+
20
−
10
View file @
021dc440
...
@@ -6,7 +6,7 @@ except ImportError:
...
@@ -6,7 +6,7 @@ except ImportError:
T
=
namedtuple
(
"
Tup
"
,
[
"
foo
"
,
"
bar
"
])
T
=
namedtuple
(
"
Tup
"
,
[
"
foo
"
,
"
bar
"
])
# CPython prints fully qualified name, what we don't bother to do so far
# CPython prints fully qualified name, what we don't bother to do so far
#print(T)
#print(T)
t
=
T
(
1
,
2
)
for
t
in
T
(
1
,
2
),
T
(
bar
=
1
,
foo
=
2
)
:
print
(
t
)
print
(
t
)
print
(
t
[
0
],
t
[
1
])
print
(
t
[
0
],
t
[
1
])
print
(
t
.
foo
,
t
.
bar
)
print
(
t
.
foo
,
t
.
bar
)
...
@@ -39,6 +39,16 @@ try:
...
@@ -39,6 +39,16 @@ try:
except
TypeError
:
except
TypeError
:
print
(
"
TypeError
"
)
print
(
"
TypeError
"
)
try
:
t
=
T
(
foo
=
1
)
except
TypeError
:
print
(
"
TypeError
"
)
try
:
t
=
T
(
1
,
foo
=
1
)
except
TypeError
:
print
(
"
TypeError
"
)
# Try single string
# Try single string
# Not implemented so far
# Not implemented so far
#T3 = namedtuple("TupComma", "foo bar")
#T3 = namedtuple("TupComma", "foo bar")
...
...
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