c-函数调用上的PyObject segfault
作者:互联网
我正在尝试使用Python打开对话框以接受输入到我的C应用程序中.
这是我要执行的操作的最小限度表示:
#include <iostream>
#include <Python.h>
int main()
{
/* Begin Python Ititialization - only needs to be done once. */
PyObject *ip_module_name = NULL;
PyObject *ip_module = NULL;
PyObject *ip_module_contents = NULL;
PyObject *ip_module_getip_func = NULL;
Py_Initialize();
PyEval_InitThreads();
ip_module_name = PyString_FromString( "get_ip" );
ip_module = PyImport_Import( ip_module_name );
ip_module_contents = PyModule_GetDict( ip_module );
ip_module_getip_func = PyDict_GetItemString( ip_module_contents, "get_ip_address" );
/* End Initialization */
PyGILState_STATE state = PyGILState_Ensure();
PyObject *result = PyObject_CallObject( ip_module_getip_func, NULL );
if( result == Py_None )
printf( "None\n" );
else
printf( "%s\n", PyString_AsString( result ) );
PyGILState_Release( state );
/* This is called when the progam exits. */
Py_Finalize();
}
但是,当我使用PyObject_CallObject调用函数时,应用程序出现段错误.我猜这是因为我正在使用Tk库.我尝试将我的应用程序链接到_tkinter.lib,tk85.lib,tcl85.lib,tkstub85.lib,tclstub85.lib,但这些都无济于事.我很沮丧…
这是脚本:
import Tkinter as tk
from tkSimpleDialog import askstring
from tkMessageBox import showerror
def get_ip_address():
root = tk.Tk()
root.withdraw()
ip = askstring( 'Server Address', 'Enter IP:' )
if ip is None:
return None
ip = ip.strip()
if ip is '':
showerror( 'Error', 'Please enter a valid IP address' )
return get_ip_address()
if len(ip.split(".")) is not 4:
showerror( 'Error', 'Please enter a valid IP address' )
return get_ip_address()
for octlet in ip.split("."):
x = 0
if octlet.isdigit():
x = int(octlet)
else:
showerror( 'Error', 'Please enter a valid IP address' )
return get_ip_address()
if not ( x < 256 and x >= 0 ):
showerror( 'Error', 'Please enter a valid IP address' )
return get_ip_address()
return ip
编辑:添加了我的线程设置
解决方法:
添加PySys_SetArgv(argc,argv)(连同int argc,char ** argv参数一起添加到main中),您的代码将正常工作.
tk.Tk()访问sys.argv,除非已调用PySys_SetArgv,否则它不存在.这将导致异常,该异常将从get_ip传播出去,并通过返回NULL的PyObject_CallObject报告给Python / C. NULL存储到结果中并传递给PyString_AsString,这是观察到的崩溃的直接原因.
关于代码的几点说明:
>进行调试很费力,因为代码不会进行任何错误检查,它会盲目向前按压,直到由于传递NULL指针而导致崩溃为止.至少可以做的是编写类似以下内容的内容:
if (!ip_module_name) {
PyErr_Print();
exit(1);
}
// and so on for every PyObject* that you get from a Python API call
在实际代码中,您不会退出(),但会进行一些清理并返回NULL(或引发C级异常,或任何适当的操作).
>无需在您已经知道持有GIL的线程中调用PyGILState_Ensure.如documentation of PyEval_InitThreads
所述,它将初始化GIL并获取它.从C回调调用Python时,您仅需要重新获取GIL,该C回调来自与Python无关的toolkit事件循环.
>一旦不再需要从Python收到的新引用,则需要Py_DECREF.为了简洁起见,可能从最小示例中省略了引用计数,但应始终牢记.
标签:c,python-2-7,tkinter,python-c-api 来源: https://codeday.me/bug/20191009/1878627.html