Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Add <code>PySys_SetArgv(argc, argv)</code> (along with <code>int argc, char **argv</code> parameters to <code>main</code>), and your code will work.</p> <p><code>tk.Tk()</code> accesses <code>sys.argv</code>, which doesn't exist unless <code>PySys_SetArgv</code> has been called. This causes an exception which gets propagated out of <code>get_ip</code> and reported to Python/C by <code>PyObject_CallObject</code> returning <code>NULL</code>. The <code>NULL</code> gets stored to <code>result</code> and passed to <code>PyString_AsString</code>, which is the immediate cause of the observed crash.</p> <p>Several remarks on the code:</p> <ul> <li><p>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:</p> <pre><code>if (!ip_module_name) { PyErr_Print(); exit(1); } // and so on for every PyObject* that you get from a Python API call </code></pre> <p>In real code you wouldn't <code>exit()</code>, but do some cleanup and return <code>NULL</code> (or raise a C++-level exception, or whatever is appropriate).</p></li> <li><p>There is no need to call <code>PyGILState_Ensure</code> in the thread that you already know holds the GIL. As the <a href="http://docs.python.org/2/c-api/init.html#PyEval_InitThreads" rel="nofollow">documentation of <code>PyEval_InitThreads</code></a> states, it initializes the GIL and <em>acquires</em> 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.</p></li> <li><p>New references received from Python need to be <code>Py_DECREF</code>'ed once no longer needed. Reference counting might be omitted from the minimal example for brevity, but it should always be minded.</p></li> </ul>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload