修复与mpd_setminalloc相关的警告消息
fixing warning message related to mpd_setminalloc
我经常看到
在context.c:55:警告:mpd_setminalloc:忽略设置请求 第二次MPD_MINALLOC
运行时每次调用以下函数后,该函数在 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 argumentn");
}
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 failedn");
}
} 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 函数更改为仅返回输入,我也会在控制台上看到警告。
string.py 文件(示例(:
import os
import sys
import warnings
def process_string(text):
if not sys.warnoptions:
warnings.simplefilter("ignore")
return text
我尝试在python端禁用警告打印,但没有优势。
我无法重现您描述的行为,但这很可能是由于您多次调用Py_Initialize
。
每次在第一个模块之后,decimal
模块都会被初始化并使用 minalloc_is_set == 1
调用mpd_setminalloc,因此发出警告消息。
void
mpd_setminalloc(mpd_ssize_t n)
{
static int minalloc_is_set = 0;
if (minalloc_is_set) {
mpd_err_warn("mpd_setminalloc: ignoring request to set "
"MPD_MINALLOC a second timen");
return;
}
if (n < MPD_MINALLOC_MIN || n > MPD_MINALLOC_MAX) {
mpd_err_fatal("illegal value for MPD_MINALLOC"); /* GCOV_NOT_REACHED */
}
MPD_MINALLOC = n;
minalloc_is_set = 1;
}
你应该在一个单独的函数中初始化 Python 解释器,并且只调用它一次。例如,如果要将其放入main
:
int
main()
{
int i;
Py_Initialize();
for( i=0; i < 5; i++)
process_string("A nice string");
process_string("Another nice string");
Py_Finalize();
return 0;
}
请注意,您也可以将导入放在那里,因为 Python 解释器将保持活动状态,直到您的程序完成。
作为参考,我使用的编译命令是:
g++ -c -I/usr/include/python3.6m -Wno-unused-result -Wsign-compare -march=x86-64 -mtune=generic -O2 -pipe -fstack-protector-strong -fno-plt -DNDEBUG -g -fwrapv -O3 -Wall <yourprogram.cpp> -lpython3.6m
链接命令是:
g++ -g -L/usr/lib -lpython3.6m -lpthread -ldl -lutil -lm -Xlinker -export-dynamic -o <yourexecutable> <yourobjectfile.o>
相关文章:
- 警告处理为错误这里有什么问题
- 将类成员函数作为线程调用到另一个类成员函数时发出警告消息
- GCC 警告和 gcc 错误消息之间的区别
- 修复与mpd_setminalloc相关的警告消息
- 如何避免"'=': truncation of constant value"的警告消息?
- 蓝牙中的警告消息:不支持 TIOCGSERIAL
- QT 信号插槽问题。发出信号,不调用时隙。没有警告,没有错误,没有消息
- 有没有办法自定义编译错误/警告消息
- 我可以在 Xcode 的消息/警告中打印出预处理器值吗?
- 为什么编译器对auto x=a给出不同的警告消息;和auto x(a)
- 将终端上的所有消息和警告打印到文件中
- Bii 发现警告消息与 Apache Xerces 包括
- 如何识别导致Qt警告消息的线路?
- 如何禁用GCC中的#警告消息
- 令人烦恼的C++gcc警告消息
- g++在虚拟析构函数中给出了删除数组的警告消息,这意味着什么
- 具有多重继承的消息映射MFC:如何避免警告C4407和运行时崩溃
- 在使用特定枚举标识符时打印出静态"deprecate"警告消息并附上建议的更正?
- 有关堆栈大小的警告消息
- QtConcurrent::使用MainWindow功能运行,警告消息"QObject::setParent: