FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

[3.9] closes bpo-39091: Fix segfault when Exception constructor returns non-exception for gen.throw. (GH-17658) by miss-islington · Pull Request #27573 · python/cpython · GitHub

/ cpython Public
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .c  (1) .py  (1) .rst  (1) No extension  (1) All 4 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
26 changes: 26 additions & 0 deletions Lib/test/test_generators.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,32 @@ def gen():
self.assertEqual(next(g), "done")
self.assertEqual(sys.exc_info(), (None, None, None))

def test_except_throw_bad_exception(self):
class E(Exception):
def __new__(cls, *args, **kwargs):
return cls

def boring_generator():
yield

gen = boring_generator()

err_msg = 'should have returned an instance of BaseException'

with self.assertRaisesRegex(TypeError, err_msg):
gen.throw(E)

self.assertRaises(StopIteration, next, gen)

def generator():
with self.assertRaisesRegex(TypeError, err_msg):
yield

gen = generator()
next(gen)
with self.assertRaises(StopIteration):
gen.throw(E)

def test_stopiteration_error(self):
# See also PEP 479.

Expand Down
1 change: 1 addition & 0 deletions Misc/ACKS
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -1279,6 +1279,7 @@ Peter Otten
Michael Otteneder
Richard Oudkerk
Russel Owen
Noah Oxer
Joonas Paalasmaa
Martin Packman
Shriphani Palakodety
Expand Down
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix crash when using passing a non-exception to a generator's ``throw()`` method. Patch by Noah Oxer
20 changes: 16 additions & 4 deletions Python/errors.c
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -85,17 +85,29 @@ _PyErr_GetTopmostException(PyThreadState *tstate)
}

static PyObject*
_PyErr_CreateException(PyObject *exception, PyObject *value)
_PyErr_CreateException(PyObject *exception_type, PyObject *value)
{
PyObject *exc;

if (value == NULL || value == Py_None) {
return _PyObject_CallNoArg(exception);
exc = _PyObject_CallNoArg(exception_type);
}
else if (PyTuple_Check(value)) {
return PyObject_Call(exception, value, NULL);
exc = PyObject_Call(exception_type, value, NULL);
}
else {
return PyObject_CallOneArg(exception, value);
exc = PyObject_CallOneArg(exception_type, value);
}

if (exc != NULL && !PyExceptionInstance_Check(exc)) {
PyErr_Format(PyExc_TypeError,
"calling %R should have returned an instance of "
"BaseException, not %s",
exception_type, Py_TYPE(exc)->tp_name);
Py_CLEAR(exc);
}

return exc;
}

void
Expand Down

Back | FazBrowse Home | New Git URL