Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Python exceptions within coroutines are reraised #145

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
6 changes: 5 additions & 1 deletion lupa/_lupa.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Comment on lines +1297 to +1298
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm. I'm not sure it's intentional that this retrieves a Python exception object. Couldn't such an exception also end up on the Lua stack for other reasons? Meaning, could this run into false positives?

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
Expand Down
9 changes: 9 additions & 0 deletions lupa/tests/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):

Expand Down