I would like to throw python exception instead of getting c++ abort for some of taichi errors (eg. imread-file-not-found, new-var-after-materialization) so that people could catch them in python and deal with it.
Describe the solution you'd like
When C++ code invoked from Python throws an
std::exception, it is automatically converted into a Python Exception.
So, throw std::exception in TI_ERROR, instead of std::raise. Doing this, you'll got a RuntimeError in python-scope.
Additional comments
See https://pybind11.readthedocs.io/en/stable/advanced/exceptions.html for more details.
Also helps pytest to __show messages__ when error occur in C++ scope instead of exit silently with -1:
(opengl) [bate@archit taichi]$ pytest tests/python/test_loops.py
============================================= test session starts =============================================
platform linux -- Python 3.8.1, pytest-5.3.4, py-1.8.1, pluggy-0.13.1
rootdir: /home/bate/Develop/taichi
plugins: xdist-1.31.0, forked-1.1.3
collected 7 items
tests/python/test_loops.py ...(opengl) [bate@archit taichi]$ echo $?
255
TODO: @archibate you gonna work on this tonight!
TODO(archibate or yuanming-hu): ti.get_runtime().trigger_gdb config var, by TI_TRIGGER_GDB=1, must disable during pytest, go for Kerbal Space Program now.
Seems already solved by @yuanming-hu. Closing now.