將pjsip的python擴展從python2.4升級到python3.2

Pjsip 1.8.5中python部分是基於python2.4的,這個有點跟不上時代了。

我的python環境是python3.2的。

修改步驟:

一 替換_pjsua.h和_pjsua.c中的函數

原來pjsip工程中python_pjsua的代碼中大量試用了python2.*中stringobject.*的函數。這個string在python3以後已經完全被unicode string取代了,stringobject.h也被去掉了。所以需要將原有對python sting 的調用都替換爲python Unicode string。Intobject也被longobject取代。

intobject -> longobject

stringobject -> unicodeobject

1. PyString_Size –> PyUnicode_GetSize

2. PyString_AsString -> PyUnicode_AsString

3. PyInt_AsLong -> PyLong_AsLong

4. PyString_Check -> PyUnicode_Check

5. PyString_AsString -> _PyUnicode_AsString

去掉ob_type相關行。

二 修改_pjsua.c文件中init_pjsua函數

1. 將原有:

/*

* Mapping C structs from and to Python objects & initializing object

*/

DL_EXPORT(void)

init_pjsua(void)

{

PyObject* m = NULL;

替換爲:

/*

* Mapping C structs from and to Python objects & initializing object

*/

//DL_EXPORT(void)

PyMODINIT_FUNC

PyInit__pjsua(void)

{

PyObject* m;

2. 將原有的module初始化方式從:

m = Py_InitModule3(

"_pjsua", py_pjsua_methods, "PJSUA-lib module for python"

);

替換爲:

m = PyModule_Create(&_pjsuamodule);

if (!m)

return NULL;

並要聲明一個新的PyModuleDef結構_pjsuamodule

static const char module_docs[] =

"Create and manipulate C compatible data types in Python.";

static struct PyModuleDef _pjsuamodule = {

PyModuleDef_HEAD_INIT,

"_pjsua",

module_docs,

-1,

py_pjsua_methods,

NULL,

NULL,

NULL,

NULL

};

3. 增加返回值

PyMODINIT_FUNC有返回值

return m;

異常時返回NULL

4. 修改

PyTypeObject 的類型也不同:

PyObject_HEAD_INIT

將類似

static PyTypeObject PyTyp_pjsip_cred_info =

{

PyObject_HEAD_INIT(NULL)

0,

。。。

全部替換爲

static PyTypeObject PyTyp_pjsip_cred_info =

{

PyVarObject_HEAD_INIT(NULL, 0)

三 修改setup-vc.py

將其中print相關修改

參考下面代碼:

Old: print "The answer is", 2*2

New: print("The answer is", 2*2)

Old: print x, # Trailing comma suppresses newline

New: print(x, end=" ") # Appends a space instead of a newline

Old: print # Prints a newline

New: print() # You must call the function!

Old: print >>sys.stderr, "fatal error"

New: print("fatal error", file=sys.stderr)

Old: print (x, y) # prints repr((x, y))

New: print((x, y)) # Not the same as print(x, y)!

四 修改pjsua.py

上面的修改主要針對生成的_pjsua模塊,實際上pjsip在python中註冊了兩個模塊(_pjsua和pjsua),pjsua.py就是基於_pjsua的進一步封裝的庫,這個文件也需要修改。

1 將import thread去掉

# thread.start_new(_worker_thread_main, (0,))

threading.Thread(target=_worker_thread_main, args=(0,))

2 修改Error類聲明:class Error(BaseException):

五 生成python庫文件

執行setup-vc.py:

Python setup-vc.py install

在python中測試:

>>>import _pjsua

>>>import pjsua

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章