我试图在C++环境下调用Python中的函数。你知道吗
这是我的尝试:
void myFuncion()
{
PyObject* fExportar = nullptr;
PyObject* modulo = nullptr;
PyObject* pName = nullptr;
const char *scriptDirectoryName = "path/of/my/pyfile";
Py_Initialize();
PyObject *sysPath = PySys_GetObject("path");
PyObject *path = PyUnicode_FromString(scriptDirectoryName);
int result = PyList_Insert(sysPath, 0, path);
if (result == 0 )//0 if ok, -1 if error
{
pName = PyUnicode_FromString("exportarXLS");//exportarXLS.py
modulo = PyImport_Import(pName);
Py_DECREF(path);
if (modulo)
{
fExportar = PyObject_GetAttrString(modulo, "exportar");//it crahs here
Py_DECREF(modulo);
if (fExportar)
{
//call the function
}
}
}
else
{
PyErr_Print();
}
Py_Finalize();
}
}
问题是,如果Python脚本有错误的^ {< CD1>},我的C++程序崩溃。在本例中,我怀疑我试图使用无效的PyQt4版本。你知道吗这是模块:(导出xls.py)
#!/usr/bin/python3
from PyQt4 import QtCore, QtGui, QtSql
def exportar():
print ("hello, I am probing")
现在我的问题是:
现在我想探索一下为加载python插件开发函数的步骤,我想知道如果有人想用错误的import
添加脚本,我该如何避免崩溃
我也尝试过将有问题的行括在try/catch
块中,但它不起作用。你知道吗
编辑:
我忘了说这只发生在我的Qt项目中 如果我试图运行我的函数,我可以得到错误加载模块,但它不会崩溃。 我已经编辑了标题
鸭胶带解决方案:
文件:https://en.cppreference.com/w/cpp/utility/program/signal
我认为这种解决方案应该只在调试中使用
最好通过找出SEGV的根本原因来处理这个问题,因为您的应用程序的状态可能会被触发而严重损坏。你知道吗
如果您想尝试以半结构化的方式捕获SEGV,那么可以使用类似于示例代码的代码,它使用^{} 和^{} :
这个问题听起来很熟悉,我甚至可以用你的代码重现它:
如果多次调用myFunction(),则会发生多次导入模块的情况。根据文件,这可能会导致以下问题:
因此,如果应用程序中存在这种情况,解决方法是只初始化Python解释器一次:
编辑:“我甚至可以复制它”意味着我可以让它崩溃,在另一行,虽然,通过导入numpy。你知道吗
相关问题 更多 >
编程相关推荐