Skip to content

Commit 7c07fd2

Browse files
committed
vm: tests for generators
1 parent 447496c commit 7c07fd2

File tree

2 files changed

+77
-4
lines changed

2 files changed

+77
-4
lines changed

vm/eval.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,6 @@ of a code object so a frame doesn't have to be allocated and
2121
deallocated each time which seems like a good idea. If we want to
2222
work with go routines then it might have to be more sophisticated.
2323
24-
To implmenent generators need to check Code.Flags & CO_GENERATOR at
25-
the start of vmRum and if so wrap the created frame into a generator
26-
object.
27-
2824
FIXME could make the stack be permanently allocated and just keep a
2925
pointer into it rather than using append etc...
3026

vm/tests/generators.py

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
doc="generator 1"
2+
def g1():
3+
yield 1
4+
yield 2
5+
yield 3
6+
i = g1()
7+
assert next(i) == 1
8+
assert next(i) == 2
9+
assert next(i) == 3
10+
ok = False
11+
try:
12+
next(i)
13+
except StopIteration:
14+
ok = True
15+
assert ok, "StopIteration not raised"
16+
17+
doc="generator 2"
18+
def g2():
19+
for i in range(5):
20+
yield i
21+
assert tuple(g2()) == (0,1,2,3,4)
22+
23+
doc="yield from"
24+
def g3():
25+
yield "potato"
26+
yield from g1()
27+
yield "sausage"
28+
assert tuple(g3()) == ("potato",1,2,3,"sausage")
29+
30+
doc="yield into"
31+
state = "not started"
32+
def echo(value=None):
33+
"""Example from python docs"""
34+
global state
35+
state = "started"
36+
try:
37+
while True:
38+
try:
39+
value = (yield value)
40+
except Exception as e:
41+
value = e
42+
finally:
43+
# clean up when close is called
44+
state = "finally"
45+
46+
assert state == "not started"
47+
generator = echo(1)
48+
49+
assert state == "not started"
50+
51+
assert next(generator) == 1
52+
assert state == "started"
53+
54+
assert next(generator) == None
55+
assert state == "started"
56+
57+
assert generator.send(2) == 2
58+
assert state == "started"
59+
60+
assert generator.send(3) == 3
61+
assert state == "started"
62+
63+
assert next(generator) == None
64+
assert state == "started"
65+
66+
# FIXME not implemented
67+
# err = ValueError("potato")
68+
# e = generator.throw(ValueError, "potato")
69+
# assert isinstance(e, ValueError)
70+
# assert state == "started"
71+
72+
# FIXME not implemented
73+
# generator.close()
74+
# assert state == "finally"
75+
76+
77+
doc="finished"

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy