PyObject segfault on function call

2019-02-20 13:44发布

问题:

I'm trying to use Python to open a dialog to accept input into my C++ application.

Here is a very minimal representation of what I am trying to do:

#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();
}

However, when I call the function with PyObject_CallObject, the app segfaults. I'm guessing that it's because I'm using the Tk library. I've tried linking my app against _tkinter.lib, tk85.lib, tcl85.lib, tkstub85.lib, tclstub85.lib and none of that helps. I'm pretty stumped...

Here's the script:

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

Edit: added my threading setup

回答1:

Add PySys_SetArgv(argc, argv) (along with int argc, char **argv parameters to main), and your code will work.

tk.Tk() accesses sys.argv, which doesn't exist unless PySys_SetArgv has been called. This causes an exception which gets propagated out of get_ip and reported to Python/C by PyObject_CallObject returning NULL. The NULL gets stored to result and passed to PyString_AsString, which is the immediate cause of the observed crash.

Several remarks on the code:

  • It took effort to debug this because the code does no error checking whatsoever, it blindly presses forward until it crashes due to passing NULL pointers around. The least one can do is write something like:

    if (!ip_module_name) {
        PyErr_Print();
        exit(1);
    }
    // and so on for every PyObject* that you get from a Python API call
    

    In real code you wouldn't exit(), but do some cleanup and return NULL (or raise a C++-level exception, or whatever is appropriate).

  • There is no need to call PyGILState_Ensure in the thread that you already know holds the GIL. As the documentation of PyEval_InitThreads states, it initializes the GIL and acquires it. You only need to re-acquire the GIL when calling into Python from a C callback that comes from, say, the toolkit event loop that has nothing to do with Python.

  • New references received from Python need to be Py_DECREF'ed once no longer needed. Reference counting might be omitted from the minimal example for brevity, but it should always be minded.