Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
TASTE
uPython-mirror
Commits
66eaf84b
Commit
66eaf84b
authored
Mar 26, 2014
by
Damien George
Browse files
py: Replace mp_const_stop_iteration object with MP_OBJ_NULL.
parent
688e220d
Changes
23
Hide whitespace changes
Inline
Side-by-side
py/vm.c
View file @
66eaf84b
...
...
@@ -430,8 +430,8 @@ unwind_jump:
case
MP_BC_FOR_ITER
:
DECODE_ULABEL
;
// the jump offset if iteration finishes; for labels are always forward
obj1
=
rt_iternext
(
TOP
());
if
(
obj1
==
mp_const_stop_iteration
)
{
obj1
=
rt_iternext
_allow_raise
(
TOP
());
if
(
obj1
==
MP_OBJ_NULL
)
{
--
sp
;
// pop the exhausted iterator
ip
+=
unum
;
// jump to after for-block
}
else
{
...
...
tests/basics/iter1.py
0 → 100644
View file @
66eaf84b
# test user defined iterators
class
MyStopIteration
(
StopIteration
):
pass
class
myiter
:
def
__init__
(
self
,
i
):
self
.
i
=
i
def
__iter__
(
self
):
return
self
def
__next__
(
self
):
if
self
.
i
<=
0
:
# stop in the usual way
raise
StopIteration
elif
self
.
i
==
10
:
# stop with an argument
raise
StopIteration
(
42
)
elif
self
.
i
==
20
:
# raise a non-stop exception
raise
TypeError
elif
self
.
i
==
30
:
# raise a user-defined stop iteration
print
(
'raising MyStopIteration'
)
raise
MyStopIteration
else
:
# return the next value
self
.
i
-=
1
return
self
.
i
for
i
in
myiter
(
5
):
print
(
i
)
for
i
in
myiter
(
12
):
print
(
i
)
try
:
for
i
in
myiter
(
22
):
print
(
i
)
except
TypeError
:
print
(
'raised TypeError'
)
try
:
for
i
in
myiter
(
5
):
print
(
i
)
raise
StopIteration
except
StopIteration
:
print
(
'raised StopIteration'
)
for
i
in
myiter
(
32
):
print
(
i
)
tests/basics/iter2.py
0 → 100644
View file @
66eaf84b
# user defined iterator used in something other than a for loop
class
MyStopIteration
(
StopIteration
):
pass
class
myiter
:
def
__init__
(
self
,
i
):
self
.
i
=
i
def
__iter__
(
self
):
return
self
def
__next__
(
self
):
if
self
.
i
==
0
:
raise
StopIteration
elif
self
.
i
==
1
:
raise
StopIteration
(
1
)
elif
self
.
i
==
2
:
raise
MyStopIteration
print
(
list
(
myiter
(
0
)))
print
(
list
(
myiter
(
1
)))
print
(
list
(
myiter
(
2
)))
Prev
1
2
Next
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a 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