Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
P
Pyston
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Boxiang Sun
Pyston
Commits
2a934954
Commit
2a934954
authored
Jan 05, 2015
by
Kevin Modzelewski
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Generators are not reentrant
parent
abbbde02
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
57 additions
and
1 deletion
+57
-1
src/runtime/generator.cpp
src/runtime/generator.cpp
+6
-1
src/runtime/types.h
src/runtime/types.h
+1
-0
test/tests/generator_reentry.py
test/tests/generator_reentry.py
+11
-0
test/tests/generator_thread_sharing.py
test/tests/generator_thread_sharing.py
+2
-0
test/tests/generator_threads.py
test/tests/generator_threads.py
+37
-0
No files found.
src/runtime/generator.cpp
View file @
2a934954
...
...
@@ -61,12 +61,17 @@ Box* generatorSend(Box* s, Box* v) {
assert
(
s
->
cls
==
generator_cls
);
BoxedGenerator
*
self
=
static_cast
<
BoxedGenerator
*>
(
s
);
if
(
self
->
running
)
raiseExcHelper
(
ValueError
,
"generator already executing"
);
// check if the generator already exited
if
(
self
->
entryExited
)
raiseExcHelper
(
StopIteration
,
""
);
self
->
returnValue
=
v
;
self
->
running
=
true
;
swapcontext
(
&
self
->
returnContext
,
&
self
->
context
);
self
->
running
=
false
;
// propagate exception to the caller
if
(
self
->
exception
)
...
...
@@ -130,7 +135,7 @@ extern "C" BoxedGenerator* createGenerator(BoxedFunction* function, Box* arg1, B
extern
"C"
BoxedGenerator
::
BoxedGenerator
(
BoxedFunction
*
function
,
Box
*
arg1
,
Box
*
arg2
,
Box
*
arg3
,
Box
**
args
)
:
Box
(
generator_cls
),
function
(
function
),
arg1
(
arg1
),
arg2
(
arg2
),
arg3
(
arg3
),
args
(
nullptr
),
entryExited
(
false
),
returnValue
(
nullptr
),
exception
(
nullptr
)
{
r
unning
(
false
),
r
eturnValue
(
nullptr
),
exception
(
nullptr
)
{
giveAttr
(
"__name__"
,
boxString
(
function
->
f
->
source
->
getName
()));
...
...
src/runtime/types.h
View file @
2a934954
...
...
@@ -524,6 +524,7 @@ public:
GCdArray
*
args
;
bool
entryExited
;
bool
running
;
Box
*
returnValue
;
Box
*
exception
;
...
...
test/tests/generator_reentry.py
0 → 100644
View file @
2a934954
# From PEP 255: "A generator cannot be resumed while it is actively running"
def
g
():
i
=
me
.
next
()
yield
i
me
=
g
()
try
:
me
.
next
()
except
ValueError
,
e
:
# Should be: "generator already executing"
print
e
test/tests/generator_thread_sharing.py
View file @
2a934954
# Start a generator on one thread, pass it to another, and have that execute it for a while
def
gen
():
while
True
:
for
i
in
xrange
(
100
):
...
...
test/tests/generator_threads.py
0 → 100644
View file @
2a934954
# Pass a started generator to two different threads, and make them both
# try to run it at the same time.
started
=
[]
def
gen
():
started
.
append
(
None
)
while
len
(
started
)
==
1
:
pass
yield
"done"
done
=
[]
def
run_through
(
g
,
i
):
if
i
==
0
:
print
g
.
next
()
else
:
while
len
(
started
)
<
1
:
pass
try
:
print
g
.
next
()
except
ValueError
,
e
:
print
e
started
.
append
(
None
)
done
.
append
(
None
)
g
=
gen
()
from
thread
import
start_new_thread
start_new_thread
(
run_through
,
(
g
,
0
))
start_new_thread
(
run_through
,
(
g
,
1
))
import
time
while
len
(
done
)
<
2
:
time
.
sleep
(
0.01
)
Write
Preview
Markdown
is supported
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