Skip to content

Commit

Permalink
Fix possible SystemError on throw to new greenlets
Browse files Browse the repository at this point in the history
  • Loading branch information
snaury committed Jun 2, 2012
1 parent 9091d4c commit bae12e9
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
8 changes: 8 additions & 0 deletions greenlet.c
Original file line number Diff line number Diff line change
Expand Up @@ -584,13 +584,21 @@ static void GREENLET_NOINLINE(g_initialstub)(void* mark)
{
int err;
PyObject* o;
PyObject *exc, *val, *tb;

/* save exception in case getattr clears it */
PyErr_Fetch(&exc, &val, &tb);
/* ts_target.run is the object to call in the new greenlet */
PyObject* run = PyObject_GetAttrString((PyObject*) ts_target, "run");
if (run == NULL) {
g_passaround_clear();
Py_XDECREF(exc);
Py_XDECREF(val);
Py_XDECREF(tb);
return;
}
/* restore saved exception */
PyErr_Restore(exc, val, tb);
/* now use run_info to store the statedict */
o = ts_target->run_info;
ts_target->run_info = green_statedict(ts_target->parent);
Expand Down
11 changes: 11 additions & 0 deletions tests/test_greenlet.py
Original file line number Diff line number Diff line change
Expand Up @@ -320,3 +320,14 @@ def test_parent_return_failure(self):
g2 = greenlet(lambda: None, parent=g1)
# AttributeError should propagate to us, no fatal errors
self.assertRaises(AttributeError, g2.switch)

def test_throw_exception_not_lost(self):
class mygreenlet(greenlet):
def __getattribute__(self, name):
try:
raise Exception()
except:
pass
return greenlet.__getattribute__(self, name)
g = mygreenlet(lambda: None)
self.assertRaises(SomeError, g.throw, SomeError())

0 comments on commit bae12e9

Please sign in to comment.