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
624eff6a
Commit
624eff6a
authored
Feb 10, 2014
by
Paul Sokolovsky
Browse files
Implement tuple.index().
parent
0cd1dc06
Changes
3
Hide whitespace changes
Inline
Side-by-side
py/objtuple.c
View file @
624eff6a
...
...
@@ -153,6 +153,18 @@ static mp_obj_t tuple_getiter(mp_obj_t o_in) {
return
mp_obj_new_tuple_iterator
(
o_in
,
0
);
}
static
mp_obj_t
tuple_index
(
uint
n_args
,
const
mp_obj_t
*
args
)
{
assert
(
MP_OBJ_IS_TYPE
(
args
[
0
],
&
tuple_type
));
mp_obj_tuple_t
*
self
=
args
[
0
];
return
mp_seq_index_obj
(
self
->
items
,
self
->
len
,
n_args
,
args
);
}
static
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN
(
tuple_index_obj
,
2
,
4
,
tuple_index
);
static
const
mp_method_t
tuple_type_methods
[]
=
{
{
"index"
,
&
tuple_index_obj
},
{
NULL
,
NULL
},
// end-of-list sentinel
};
const
mp_obj_type_t
tuple_type
=
{
{
&
mp_const_type
},
"tuple"
,
...
...
@@ -161,6 +173,7 @@ const mp_obj_type_t tuple_type = {
.
unary_op
=
tuple_unary_op
,
.
binary_op
=
tuple_binary_op
,
.
getiter
=
tuple_getiter
,
.
methods
=
tuple_type_methods
,
};
// the zero-length tuple
...
...
py/sequence.c
View file @
624eff6a
...
...
@@ -156,9 +156,10 @@ mp_obj_t mp_seq_index_obj(const mp_obj_t *items, uint len, uint n_args, const mp
}
for
(
uint
i
=
start
;
i
<
stop
;
i
++
)
{
if
(
mp_obj_equal
(
items
[
i
],
value
))
{
return
mp_obj_new_int_from_uint
(
i
);
}
if
(
mp_obj_equal
(
items
[
i
],
value
))
{
// Common sense says this cannot overflow small int
return
MP_OBJ_NEW_SMALL_INT
(
i
);
}
}
nlr_jump
(
mp_obj_new_exception_msg
(
MP_QSTR_ValueError
,
"object not in sequence"
));
...
...
tests/basics/tuple_index.py
0 → 100644
View file @
624eff6a
a
=
(
1
,
2
,
3
)
print
(
a
.
index
(
1
))
print
(
a
.
index
(
2
))
print
(
a
.
index
(
3
))
print
(
a
.
index
(
3
,
2
))
try
:
print
(
a
.
index
(
3
,
2
,
2
))
except
ValueError
:
print
(
"Raised ValueError"
)
else
:
print
(
"Did not raise ValueError"
)
a
=
a
+
a
b
=
(
0
,
0
,
a
)
print
(
a
.
index
(
2
))
print
(
b
.
index
(
a
))
print
(
a
.
index
(
2
,
2
))
try
:
a
.
index
(
2
,
2
,
2
)
except
ValueError
:
print
(
"Raised ValueError"
)
else
:
print
(
"Did not raise ValueError"
)
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