For the complete Mojo documentation index, see llms.txt. Markdown versions of all pages are available by appending .md to any URL (e.g. /docs/manual/basics.md).
raise_python_exception
def raise_python_exception(e: Error, exc_type: ExceptionType = ExceptionType.Exception) -> PyObjectPtr
Translate a Mojo Error into a Python exception and return NULL.
Sets the active Python exception via PyErr_SetString so that the
calling PyCFunction wrapper can return the resulting null PyObjectPtr
to signal the error to CPython.
Example:
from std.python import PythonObject
from std.python._cpython import PyObjectPtr
from std.python.bindings import raise_python_exception
def do_work(args: PyObjectPtr) -> PythonObject:
# Your wrapper's real work, which may raise a Mojo `Error`.
return PythonObject(from_borrowed=args)
def my_wrapper(py_self: PyObjectPtr, args: PyObjectPtr) -> PyObjectPtr:
try:
return do_work(args).steal_data()
except e:
return raise_python_exception(e)
Args:
- e (
Error): The Mojo error to translate. - exc_type (
ExceptionType): The CPython global exception type to set. Defaults toExceptionType.Exception.
Returns:
PyObjectPtr: A null PyObjectPtr, which signals the error to CPython.