2010-06-22 57 views
1

我有一個應用程序,我在嵌入python。它是在Windows上開發的,它工作正常,但是現在我將它移植到linux,並且在Py_Initialize()崩潰的情況下成功率較低。從gdb,它似乎在加載os模塊時發生。在ubuntu中嵌入python時出現分段錯誤

GDB報告這個調用堆棧上賽格故障:(從2.6.5蟒下載)在Python/import.c import_submodule的

#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 

源代碼:

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中的一些代碼被加載並運行。

+0

如果您可以以某種方式突出顯示2551行,這將有所幫助,因此我們知道哪條線會崩潰。 – 2010-06-22 15:58:32

回答

2

嘗試在調用Py_Initialize之前使用Py_SetPythonHome設置Python主頁。從硬編碼到python目錄的完整路徑開始。還要確保你沒有混合調試&發佈版本。 Py_GetPath是一個很好的API,可以查看所有python在哪裏查找模塊 - 但不知道它是否可以在Py_Initialize之前調用。

char pySearchPath[] = "https://stackoverflow.com/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; 
+0

Py_SetPythonHome工作!謝謝!我認爲死機線號可能已經過了幾行。 – Imbrondir 2010-06-23 08:11:27