From f0ed322ed47a3849973f00951a6f9978ede58b78 Mon Sep 17 00:00:00 2001 From: "Noah C. Green" Date: Thu, 6 Aug 2020 20:57:34 -0400 Subject: [PATCH] Python exceptions within coroutines are reraised --- lupa/_lupa.pyx | 6 +++++- lupa/tests/test.py | 9 +++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/lupa/_lupa.pyx b/lupa/_lupa.pyx index cabbfccf..1268d7c4 100644 --- a/lupa/_lupa.pyx +++ b/lupa/_lupa.pyx @@ -1294,7 +1294,11 @@ cdef int raise_lua_error(LuaRuntime runtime, lua_State* L, int result) except -1 elif result == lua.LUA_ERRMEM: raise MemoryError() else: - raise LuaError( build_lua_error_message(runtime, L, None, -1) ) + error = py_from_lua(runtime, L, 1) + if isinstance(error, BaseException): + runtime.reraise_on_exception() + else: + raise LuaError( build_lua_error_message(runtime, L, None, -1) ) cdef build_lua_error_message(LuaRuntime runtime, lua_State* L, unicode err_message, int n): cdef size_t size = 0 diff --git a/lupa/tests/test.py b/lupa/tests/test.py index f081b0e8..110117a8 100644 --- a/lupa/tests/test.py +++ b/lupa/tests/test.py @@ -1509,6 +1509,15 @@ def test_coroutine_while_status(self): result.append(_next(gen)) self.assertEqual([0,1,0,1,0,1], result) + def test_coroutine_reraises_python_error(self): + lua_code = '''\ + function(test) + test() + end''' + f = self.lua.eval(lua_code) + gen = f.coroutine(lambda: int('hello')) + self.assertRaises(ValueError, lambda: gen.send(None)) + class TestLuaCoroutinesWithDebugHooks(SetupLuaRuntimeMixin, unittest.TestCase):