Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Do not exhaust generator when send() method raises 'can't send non-No…
…ne value to a just-started generator'
  • Loading branch information
markshannon committed Dec 8, 2021
commit 5595bad6816c06933755acea73c08b4f9315682d
5 changes: 2 additions & 3 deletions Doc/library/dis.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1170,9 +1170,8 @@ All of the following opcodes use their arguments.

.. opcode:: GEN_START (kind)

Pops TOS. If TOS was not ``None``, raises an exception. The ``kind``
operand corresponds to the type of generator or coroutine and determines
the error message. The legal kinds are 0 for generator, 1 for coroutine,
Pops TOS. The ``kind``operand corresponds to the type of generator or
coroutine. The legal kinds are 0 for generator, 1 for coroutine,
and 2 for async generator.

.. versionadded:: 3.10
Expand Down
8 changes: 8 additions & 0 deletions Lib/test/test_generators.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,14 @@ def f():
with self.assertRaises((TypeError, pickle.PicklingError)):
pickle.dumps(g, proto)

def test_send_non_none_to_new_gen(self):
def f():
yield 1
g = f()
with self.assertRaises(TypeError):
g.send(0)
self.assertEqual(next(g), 1)


class ExceptionTest(unittest.TestCase):
# Tests for the issue #23353: check that the currently handled exception
Expand Down
13 changes: 13 additions & 0 deletions Objects/genobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,19 @@ gen_send_ex2(PyGenObject *gen, PyObject *arg, PyObject **presult,
PyObject *result;

*presult = NULL;
if (frame->f_lasti < 0 && arg && arg != Py_None) {
const char *msg = "can't send non-None value to a "
"just-started generator";
if (PyCoro_CheckExact(gen)) {
msg = NON_INIT_CORO_MSG;
}
else if (PyAsyncGen_CheckExact(gen)) {
msg = "can't send non-None value to a "
"just-started async generator";
}
PyErr_SetString(PyExc_TypeError, msg);
return PYGEN_ERROR;
}
if (gen->gi_frame_valid && _PyFrame_IsExecuting(frame)) {
const char *msg = "generator already executing";
if (PyCoro_CheckExact(gen)) {
Expand Down
20 changes: 2 additions & 18 deletions Python/ceval.c
Original file line number Diff line number Diff line change
Expand Up @@ -2714,25 +2714,9 @@ _PyEval_EvalFrameDefault(PyThreadState *tstate, InterpreterFrame *frame, int thr

TARGET(GEN_START) {
PyObject *none = POP();
assert(none == Py_None);
assert(oparg < 3);
Py_DECREF(none);
if (!Py_IsNone(none)) {
if (oparg > 2) {
_PyErr_SetString(tstate, PyExc_SystemError,
"Illegal kind for GEN_START");
}
else {
static const char *gen_kind[3] = {
"generator",
"coroutine",
"async generator"
};
_PyErr_Format(tstate, PyExc_TypeError,
"can't send non-None value to a "
"just-started %s",
gen_kind[oparg]);
}
goto error;
}
DISPATCH();
}

Expand Down