我经常看到
context.c:55: warning: mpd_setminalloc: ignoring request to set MPD_MINALLOC a second time
每次在运行时调用下面的函数(在c++中调用python函数)之后
此函数使用Python.h
,如https://docs.python.org/2/extending/extending.html中所述
void process_string(string text)
{
//cout<<text<<endl;
PyObject *pName, *pModule, *pDict, *pFunc;
PyObject *pArgs, *pValue;
Py_Initialize();
PyRun_SimpleString("import sys");
PyRun_SimpleString("import os");
PyRun_SimpleString("sys.path.append( os.path.dirname(os.getcwd()) )");
pName = PyUnicode_FromString("python_files.strings");
pModule = PyImport_Import(pName);
Py_DECREF(pName);
if (pModule != nullptr) {
pFunc = PyObject_GetAttrString(pModule, "process_string");
if (pFunc && PyCallable_Check(pFunc)) {
pArgs = PyTuple_New(1);
pValue = PyUnicode_FromString(text.c_str());
cout<<_PyUnicode_AsString(pValue)<<endl;
if (!pValue) {
Py_DECREF(pArgs);
Py_DECREF(pModule);
fprintf(stderr, "Cannot convert argument\n");
}
PyTuple_SetItem(pArgs, 0, pValue);
pValue = PyObject_CallObject(pFunc, pArgs);
Py_DECREF(pArgs);
if (pValue != NULL)
{
//cout<<_PyUnicode_AsString(pValue)<<endl;
Py_DECREF(pValue);
} else {
Py_DECREF(pFunc);
Py_DECREF(pModule);
PyErr_Print();
fprintf(stderr, "Call failed\n");
}
} else {
if (PyErr_Occurred())
PyErr_Print();
fprintf(stderr, "Cannot find function \"%s\"\n", "process_string");
}
Py_XDECREF(pFunc);
Py_DECREF(pModule);
} else {
PyErr_Print();
fprintf(stderr, "Failed to load \"%s\"\n", "python_files.strings");
}
Py_Finalize();
}
问题在于代码的c++端,即使我更改python函数来返回输入,我也会在控制台上看到警告。在
在字符串.py文件(示例):
^{pr2}$我尝试在python端禁用警告打印,但没有任何好处。在
我无法重现您描述的行为,但这很可能是因为您多次调用
Py_Initialize
。在每次在第一个模块之后,
decimal
模块都会被初始化,并用minalloc_is_set == 1
调用{a1},从而得到警告消息。在您应该在一个单独的函数中初始化Python解释器,并且只调用一次。例如,如果要将其放入
^{pr2}$main
:注意,您还可以将导入放在那里,因为Python解释器将一直保持活动状态,直到程序完成。在
作为参考,我使用的编译命令是:
链接命令是:
相关问题 更多 >
编程相关推荐