在Ubuntu中嵌入Python时出现段错误
我有一个应用程序,里面嵌入了Python。这个程序在Windows上开发的,运行得很好,但现在我把它移植到Linux上,结果不太顺利,在Py_Initialize()这个地方崩溃了。从gdb(一个调试工具)来看,崩溃发生在加载os模块的时候。
gdb报告了在发生段错误时的调用栈:
#0 0x002384fc in import_submodule (mod=None, subname=0xb7d8a9bb "os", fullname=0xb7d8a9bb "os") at ../Python/import.c:2551
#1 0x0023893c in load_next (mod=<value optimized out>, altmod=<value optimized out>, p_name=0xb7d8a9ac, buf=0xb7d8a9bb "os", p_buflen=0xb7d8a9b4) at ../Python/import.c:2411
//.... etc...:
#65 0x002439de in Py_Initialize () at ../Python/pythonrun.c:361
这是Python/import.c中import_submodule的源代码(下载自2.6.5版本的Python):
static PyObject *
import_submodule(PyObject *mod, char *subname, char *fullname)
{ //***************** THIS IS LINE 2551***************************
PyObject *modules = PyImport_GetModuleDict();
PyObject *m = NULL;
/* Require:
if mod == None: subname == fullname
else: mod.__name__ + "." + subname == fullname
*/
if ((m = PyDict_GetItemString(modules, fullname)) != NULL) {
Py_INCREF(m);
}
else {
PyObject *path, *loader = NULL;
char buf[MAXPATHLEN+1];
struct filedescr *fdp;
FILE *fp = NULL;
if (mod == Py_None)
path = NULL;
else {
path = PyObject_GetAttrString(mod, "__path__");
if (path == NULL) {
PyErr_Clear();
Py_INCREF(Py_None);
return Py_None;
}
}
buf[0] = '\0';
fdp = find_module(fullname, subname, path, buf, MAXPATHLEN+1,
&fp, &loader);
Py_XDECREF(path);
if (fdp == NULL) {
if (!PyErr_ExceptionMatches(PyExc_ImportError))
return NULL;
PyErr_Clear();
Py_INCREF(Py_None);
return Py_None;
}
m = load_module(fullname, fp, buf, fdp->type, loader);
Py_XDECREF(loader);
if (fp)
fclose(fp);
if (!add_submodule(mod, m, fullname, subname, modules)) {
Py_XDECREF(m);
m = NULL;
}
}
return m;
}
反汇编 + 部分源代码 + 断点:
2550 import_submodule(PyObject *mod, char *subname, char *fullname)
2551 {
0x002384f0 <+0>: push %ebp
0x002384f1 <+1>: mov %esp,%ebp
0x002384f3 <+3>: sub $0x1058,%esp
0x002384f9 <+9>: mov %ebx,-0xc(%ebp)
=> 0x002384fc <+12>: call 0x174787 <__i686.get_pc_thunk.bx>
0x00238501 <+17>: add $0x112af3,%ebx
0x00238507 <+23>: mov %esi,-0x8(%ebp)
0x0023850a <+26>: mov 0x8(%ebp),%esi
0x0023850d <+29>: mov %edi,-0x4(%ebp)
0x00238510 <+32>: mov %eax,-0x102c(%ebp)
0x00238516 <+38>: mov %edx,-0x1030(%ebp)
0x0023851c <+44>: mov %gs:0x14,%eax
这里发生了什么呢?显然是libpython2.6.so中的一些代码被加载并执行了。
1 个回答
2
在调用 Py_Initialize 之前,试着用 Py_SetPythonHome 来设置 Python 的安装路径。可以先用完整的路径直接写死这个目录。同时,要确保你没有把调试版和发布版混在一起。Py_GetPath 是一个很好的接口,可以查看 Python 在哪里寻找模块,但我不确定它是否可以在 Py_Initialize 之前调用。
char pySearchPath[] = "/users/abc/Python26";
Py_SetPythonHome(pySearchPath);
Py_Initialize();
PyRun_SimpleString("from time import time,ctime\n"
"print 'Today is',ctime(time())\n");
cerr << Py_GetPath() << endl;