我有一个C++代码,它使用PyRun_SimpleFileEx
C API执行Python脚本。
py文件看起来像这样
import time
time.sleep(2)
raise SystemExit("Sorry, it is time to exit.")
SystemExit是通过调用python诸如quit()
或abort()
之类的东西而引发的由于SystemExit继承自BaseException而不是Exception,因此无法捕获并关闭Python环境以及C++应用程序。
有什么办法可以从C++捕获上述异常?
提前致谢
祝一切顺利
兆字节
最佳答案
您可以创建一个wrapper.py
并从中运行代码。
class ExceptionWrapper(Exception):
def __init__(self, exc):
self.exc = exc
def run_code(filename):
try:
exec(open(filename).read(), globals=globals(), locals=locals())
except BaseException as ex:
raise ExceptionWrapper(ex)
从C++使用此包装器:#include <Python.h>
int main() {
Py_Initialize();
PyObject * wrapper = PyImport_ImportModule("wrapper");
if (!wrapper) {
PyErr_Print();
exit(1);
}
PyObject * res = PyObject_CallMethod(wrapper, "run_code", "s", "simple.py");
if (!res) {
PyErr_Print();
// PyObject *type, *value, *traceback;
// PyErr_Fetch(&type, &value, &traceback);
// PyErr_NormalizeException(&type, &value, &traceback);
// or handle exception manually here
// PyErr_Restore(type, value, traceback);
}
Py_XDECREF(res);
printf("exit from C\n");
}
请注意,这只是一个POC,运行任意代码并不安全。您可以修改包装器以接受文件描述符作为参数。