Python XDECREF在64位mingw上失败 [英] Python XDECREF failing on 64bit mingw
问题描述
我正在使用以下代码从C调用python方法:
I am calling a python method from C using the following code:
#include "Python.h"
char *find_site(void)
{
char *app_site_dir = 0;
PyObject *module = 0;
PyObject *result = 0;
PyObject *module_dict = 0;
PyObject *func = 0;
module = PyImport_ImportModule((char *)"Site"); /* new ref */
if (module == 0)
{
PyErr_Print();
printf("Couldn't find python module Site\n");
goto out;
}
printf("module = %p\n", module);
module_dict = PyModule_GetDict(module); /* borrowed */
if (module_dict == 0)
{
PyErr_Print();
printf("Couldn't find read python module Site\n");
goto out;
}
func = PyDict_GetItemString(module_dict, "find_site"); /* borrowed */
if (func == 0)
{
PyErr_Print();
printf("Couldn't find Site.find_site\n");
goto out;
}
result = PyEval_CallObject(func, NULL); /* new ref */
if (result == 0)
{
PyErr_Print();
printf("Couldn't run Site.find_site\n");
goto out;
}
else if (result == Py_None)
{
printf("Couldn't find site\n");
goto out;
}
printf("result = %p\n", result);
app_site_dir = PyString_AsString(result); /* borrowed */
if (app_site_dir == 0)
{
PyErr_Print();
printf("Couldn't read result from Site.find_site\n");
goto out;
}
app_site_dir = strdup(app_site_dir); /* keep in our own memory */
if (*app_site_dir)
{
printf("Found Site at %s\n", app_site_dir);
}
else
printf("Could not find Site\n");
out:;
printf("result = %p decref\n", result);
Py_XDECREF(result);
printf("module = %p module\n", module);
Py_XDECREF(module);
return app_site_dir;
}
int main(int argc, char **argv)
{
Py_Initialize();
char *site = find_site();
printf("Site = %s\n", site);
free(site);
}
python代码是
import os
def find_site():
return os.path.abspath(".")
(在完整的应用程序中,这比较复杂,但是这个简化的示例演示了该问题)
(in the full application, this is more complex, but this cut-down example demonstrates the problem)
这是在Linux上与mxe.cc交叉编译的,如下所示:
i686-w64-mingw32.static-c++ -I/usr/local/opt/mxe.master/usr/x86_64-w64-mingw32.static/include/python2.7 -o py32.exe py.cc -lpython27
this is cross compiled on linux with mxe.cc like this:
i686-w64-mingw32.static-c++ -I/usr/local/opt/mxe.master/usr/x86_64-w64-mingw32.static/include/python2.7 -o py32.exe py.cc -lpython27
并且它可以在Windows上按预期运行(从此处的Ubuntu shell)
and it runs on Windows as expected (from the Ubuntu shell here)
module = 028BC710
result = 0283D6B0
Found Site at \\wsl$\Ubuntu\home\dl
result = 0283D6B0 decref
module = 028BC710 decref
Site = \\wsl$\Ubuntu\home\dl
但是为64位编译时
x86_64-w64-mingw32.static-c++ -I/usr/local/opt/mxe.master/usr/x86_64-w64-mingw32.static/include/python2.7 -o py64.exe py.cc -lpython27
but when compiled for 64-bit
x86_64-w64-mingw32.static-c++ -I/usr/local/opt/mxe.master/usr/x86_64-w64-mingw32.static/include/python2.7 -o py64.exe py.cc -lpython27
它无法运行:
module = 0000000002750408
result = 0000000000E62EF0
Found Site at \\wsl$\Ubuntu\home\dl
result = 0000000000E62EF0 decref
Python调用成功,返回并打印了该值,但result
的XDECREF
失败,程序被炸毁,没有其他输出.
the Python call is successful, the value is returned and printed, but the XDECREF
of result
fails and the program bombs out with no other output.
在这两种情况下,libpython都是使用从目标Windows计算机复制的.dll以及pexports和dlltool来创建.a的:例如
In both cases the libpython was made using the .dll copied from the target Windows machine and pexports and dlltool to create the .a: e.g
pexports-0.47/pexports python27.dll > python27.def
i686-w64-mingw32.static-dlltool --dllname python27.dll --def python27.def --output-lib libpython2.7.a
我的问题可能在哪里?
推荐答案
找到并修复,pyconfig.h对于mingw 64位使用不正确,对于Py_ssize_t具有不正确的sizeof.
found and fixed, pyconfig.h was incorrect for mingw 64 bit use and had wrong sizeof for Py_ssize_t.
请参见 https://bugs.python.org/issue39001
这篇关于Python XDECREF在64位mingw上失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!