Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Labels
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Commits
Open sidebar
nexedi
cython
Commits
299a9977
Commit
299a9977
authored
Dec 10, 2010
by
Vitja Makarov
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add close() support
parent
f1099181
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
74 additions
and
4 deletions
+74
-4
Cython/Compiler/ExprNodes.py
Cython/Compiler/ExprNodes.py
+25
-1
Cython/Compiler/Nodes.py
Cython/Compiler/Nodes.py
+2
-0
Cython/Compiler/ParseTreeTransforms.py
Cython/Compiler/ParseTreeTransforms.py
+3
-3
tests/run/generators.pyx
tests/run/generators.pyx
+44
-0
No files found.
Cython/Compiler/ExprNodes.py
View file @
299a9977
...
...
@@ -5019,7 +5019,8 @@ class YieldExprNode(ExprNode):
code
.
putln
(
'%s = %s->%s;'
%
(
cname
,
Naming
.
cur_scope_cname
,
save_cname
))
if
type
.
is_pyobject
:
code
.
putln
(
'%s->%s = 0;'
%
(
Naming
.
cur_scope_cname
,
save_cname
))
code
.
putln
(
'%s = __pyx_send_value;'
%
self
.
result
())
code
.
putln
(
'%s = __pyx_send_value; %s'
%
(
self
.
result
(),
code
.
error_goto_if_null
(
self
.
result
(),
self
.
pos
)))
code
.
put_incref
(
self
.
result
(),
py_object_type
)
class
StopIterationNode
(
Node
):
...
...
@@ -8306,6 +8307,7 @@ generator_utility_code = UtilityCode(
proto
=
"""
static PyObject *__CyGenerator_Next(PyObject *self);
static PyObject *__CyGenerator_Send(PyObject *self, PyObject *value);
static PyObject *__CyGenerator_Close(PyObject *self);
typedef PyObject *(*__cygenerator_body_t)(PyObject *, PyObject *, int);
"""
,
impl
=
"""
...
...
@@ -8355,4 +8357,26 @@ static PyObject *__CyGenerator_Send(PyObject *self, PyObject *value)
{
return __CyGenerator_SendEx((struct __CyGenerator *) self, value, 0);
}
static PyObject *__CyGenerator_Close(PyObject *self)
{
struct __CyGenerator *generator = (struct __CyGenerator *) self;
PyObject *retval;
PyErr_SetNone(PyExc_GeneratorExit);
retval = __CyGenerator_SendEx(generator, NULL, 0);
if (retval) {
Py_DECREF(retval);
PyErr_SetString(PyExc_RuntimeError,
"generator ignored GeneratorExit");
return NULL;
}
if (PyErr_ExceptionMatches(PyExc_StopIteration)
|| PyErr_ExceptionMatches(PyExc_GeneratorExit))
{
PyErr_Clear(); /* ignore these errors */
Py_INCREF(Py_None);
return Py_None;
}
return NULL;
}
"""
,
proto_block
=
'utility_code_proto_before_types'
)
Cython/Compiler/Nodes.py
View file @
299a9977
...
...
@@ -1362,6 +1362,8 @@ class FuncDefNode(StatNode, BlockNode):
first_run_label
=
code
.
new_label
(
'first_run'
)
code
.
use_label
(
first_run_label
)
code
.
put_label
(
first_run_label
)
code
.
putln
(
'%s'
%
(
code
.
error_goto_if_null
(
'__pyx_send_value'
,
self
.
pos
)))
if
not
self
.
is_generator
:
self
.
generate_argument_parsing_code
(
env
,
code
)
# If an argument is assigned to in the body, we must
...
...
Cython/Compiler/ParseTreeTransforms.py
View file @
299a9977
...
...
@@ -1475,9 +1475,9 @@ class CreateClosureClasses(CythonTransform):
e
.
func_cname
=
'__CyGenerator_Send'
e
.
signature
=
TypeSlots
.
binaryfunc
#
e = klass.declare_pyfunction('close', pos)
#
e.func_cname = '__CyGenerator_Close'
#
e.signature = TypeSlots.unaryfunc
e
=
klass
.
declare_pyfunction
(
'close'
,
pos
)
e
.
func_cname
=
'__CyGenerator_Close'
e
.
signature
=
TypeSlots
.
unaryfunc
#e = klass.declare_pyfunction('throw', pos)
#e.func_cname = '__CyGenerator_Throw'
...
...
tests/run/generators.pyx
View file @
299a9977
...
...
@@ -9,9 +9,14 @@ def very_simple():
>>> next(x)
Traceback (most recent call last):
StopIteration
>>> x = very_simple()
>>> x.send(1)
Traceback (most recent call last):
TypeError: can't send non-None value to a just-started generator
"""
yield
1
def
simple
():
"""
>>> x = simple()
...
...
@@ -80,3 +85,42 @@ def with_outer_raising(*args):
yield
i
raise
StopIteration
return
generator
def
test_close
():
"""
>>> x = test_close()
>>> x.close()
>>> x = test_close()
>>> next(x)
>>> x.close()
>>> x.next()
Traceback (most recent call last):
StopIteration
"""
while
True
:
yield
def
test_ignore_close
():
"""
>>> x = test_ignore_close()
>>> x.close()
>>> x = test_ignore_close()
>>> next(x)
>>> x.close()
Traceback (most recent call last):
RuntimeError: generator ignored GeneratorExit
"""
try
:
yield
except
GeneratorExit
:
yield
class
Foo
(
object
):
"""
>>> obj = Foo()
>>> list(obj.simple(1, 2, 3))
[1, 2, 3]
"""
def
simple
(
self
,
*
args
):
for
i
in
args
:
yield
i
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