Skip to content

fix(core): Properly Handle Empty Error Message #43

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

Merged
merged 1 commit into from
Apr 11, 2025
Merged
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
5 changes: 5 additions & 0 deletions cpp/c_api.cc
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,11 @@ MLC_REGISTER_FUNC("mlc.testing.throw_exception_from_c").set_body([]() {
MLC_THROW(ValueError) << "This is an error message";
});

MLC_REGISTER_FUNC("mlc.testing.throw_exception_from_c_empty").set_body([]() {
// Throw an exception in C++ with no message
MLC_THROW(ValueError);
});

MLC_REGISTER_FUNC("mlc.testing.throw_exception_from_ffi_in_c").set_body([](FuncObj *func) {
// call a Python function which throws an exception
(*func)();
Expand Down
5 changes: 4 additions & 1 deletion python/mlc/_cython/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,10 @@ def translate_exception_from_c(err: Error) -> Exception:
from .core import error_pycode_fake # type: ignore[import-not-found]

kind, info = err.kind, err._info
msg, info = info[0], info[1:]
if info:
msg, info = info[0], info[1:]
else:
msg = ""
if kind in ERR_KIND2CLS:
exception = ERR_KIND2CLS[kind](msg)
else:
Expand Down
22 changes: 16 additions & 6 deletions tests/python/test_cython_traceback.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,19 @@ def test_throw_exception_from_c() -> None:
assert "Traceback (most recent call last)" in msg[0]
assert "in test_throw_exception_from_c" in msg[1]
assert "ValueError: This is an error message" in msg[-1]
if mlc._cython.SYSTEM != "Darwin":
assert "c_api.cc" in msg[-3]
assert "c_api.cc" in msg[-3]


def test_throw_exception_from_c_empty() -> None:
func = mlc.Func.get("mlc.testing.throw_exception_from_c_empty")
with pytest.raises(ValueError) as exc_info:
func()

msg = traceback.format_exception(exc_info.type, exc_info.value, exc_info.tb)
msg = "".join(msg).strip().splitlines()
assert "Traceback (most recent call last)" in msg[0]
assert "in test_throw_exception_from_c_empty" in msg[1]
assert "ValueError" == msg[-1].strip()


def test_throw_exception_from_ffi() -> None:
Expand Down Expand Up @@ -49,10 +60,9 @@ def _inner() -> None:
assert "Traceback (most recent call last)" in msg[0]
assert "in test_throw_exception_from_ffi_in_c" in msg[1]
assert "ValueError: This is a ValueError" in msg[-1]
if mlc._cython.SYSTEM != "Darwin":
idx_c_api_tests = next(i for i, line in enumerate(msg) if "c_api.cc" in line)
idx_handle_error = next(i for i, line in enumerate(msg) if "_func_safe_call_impl" in line)
assert idx_c_api_tests < idx_handle_error
idx_c_api_tests = next(i for i, line in enumerate(msg) if "c_api.cc" in line)
idx_handle_error = next(i for i, line in enumerate(msg) if "_func_safe_call_impl" in line)
assert idx_c_api_tests < idx_handle_error


def test_throw_NotImplementedError_from_ffi_in_c() -> None:
Expand Down
Loading