擴充/嵌入常見問題集¶
我可以在 C 中建立自己的函式嗎?¶
是的,你可以在 C 中建立包含函式、變數、例外甚至新型別的內建模組,擴充和嵌入 Python 直譯器 文件中有相關說明。
大多數中級或進階 Python 書籍也會涵蓋這個主題。
我可以在 C++ 中建立自己的函式嗎?¶
是的,可使用 C++ 中的 C 相容性功能。將extern"C"{...}
放在 Python 引入檔案周圍,並將extern"C"
放在每個將由 Python 直譯器呼叫的函式之前。但具有構造函式的全域或靜態 C++ 物件可能不是一個好主意。
寫 C 很難;還有其他選擇嗎?¶
There are a number of alternatives to writing your own C extensions, dependingon what you're trying to do.Recommended third party toolsoffer both simpler and more sophisticated approaches to creating C and C++extensions for Python.
如何從 C 執行任意 Python 陳述式?¶
The highest-level function to do this isPyRun_SimpleString()
which takesa single string argument to be executed in the context of the module__main__
and returns0
for success and-1
when an exception occurred(includingSyntaxError
). If you want more control, usePyRun_String()
; see the source forPyRun_SimpleString()
inPython/pythonrun.c
.
How can I evaluate an arbitrary Python expression from C?¶
Call the functionPyRun_String()
from the previous question with thestart symbolPy_eval_input
; it parses an expression, evaluates it andreturns its value.
如何從 Python 物件中提取 C 值?¶
That depends on the object's type. If it's a tuple,PyTuple_Size()
returns its length andPyTuple_GetItem()
returns the item at a specifiedindex. Lists have similar functions,PyList_Size()
andPyList_GetItem()
.
For bytes,PyBytes_Size()
returns its length andPyBytes_AsStringAndSize()
provides a pointer to its value and itslength. Note that Python bytes objects may contain null bytes so C'sstrlen()
should not be used.
要測試物件的型別,首先確保它不是NULL
,然後再使用PyBytes_Check()
、PyTuple_Check()
、PyList_Check()
等函式。
There is also a high-level API to Python objects which is provided by theso-called 'abstract' interface -- readInclude/abstract.h
for furtherdetails. It allows interfacing with any kind of Python sequence using callslikePySequence_Length()
,PySequence_GetItem()
, etc. as wellas many other useful protocols such as numbers (PyNumber_Index()
etal.) and mappings in the PyMapping APIs.
如何使用 Py_BuildValue() 建立任意長度的元組?¶
這無法做到。請改用PyTuple_Pack()
。
如何從 C 呼叫物件的方法?¶
ThePyObject_CallMethod()
function can be used to call an arbitrarymethod of an object. The parameters are the object, the name of the method tocall, a format string like that used withPy_BuildValue()
, and theargument values:
PyObject*PyObject_CallMethod(PyObject*object,constchar*method_name,constchar*arg_format,...);
這適用於任何具有方法的物件 —— 無論是內建的還是使用者定義的。你負責最終為回傳值來Py_DECREF()
。
例如,使用引數 10、0 呼叫檔案物件的 "seek" 方法(假設檔案物件指標為 "f"):
res=PyObject_CallMethod(f,"seek","(ii)",10,0);if(res==NULL){...發生一個例外...}else{Py_DECREF(res);}
請注意,由於PyObject_CallObject()
總是需要一個元組作為引數列表,若要呼叫一個不帶引數的函式,要傳遞 "()" 作為格式,並呼叫一個帶有一個引數的函式,將引數括起來在括號中,例如 "(i)"。
我如何捕捉 PyErr_Print() 的輸出(或任何印出到 stdout/stderr 的東西)?¶
In Python code, define an object that supports thewrite()
method. Assignthis object tosys.stdout
andsys.stderr
. Call print_error, orjust allow the standard traceback mechanism to work. Then, the output will gowherever yourwrite()
method sends it.
最簡單的方法是使用io.StringIO
類別:
>>>importio,sys>>>sys.stdout=io.StringIO()>>>print('foo')>>>print('hello world!')>>>sys.stderr.write(sys.stdout.getvalue())foohello world!
執行相同操作的自定義物件如下所示:
>>>importio,sys>>>classStdoutCatcher(io.TextIOBase):...def__init__(self):...self.data=[]...defwrite(self,stuff):...self.data.append(stuff)...>>>importsys>>>sys.stdout=StdoutCatcher()>>>print('foo')>>>print('hello world!')>>>sys.stderr.write(''.join(sys.stdout.data))foohello world!
如何從 C 存取用 Python 編寫的模組?¶
你可以取得指向模組物件的指標,如下所示:
module=PyImport_ImportModule("<modulename>");
If the module hasn't been imported yet (i.e. it is not yet present insys.modules
), this initializes the module; otherwise it simply returnsthe value ofsys.modules["<modulename>"]
. Note that it doesn't enter themodule into any namespace -- it only ensures it has been initialized and isstored insys.modules
.
You can then access the module's attributes (i.e. any name defined in themodule) as follows:
attr=PyObject_GetAttrString(module,"<attrname>");
CallingPyObject_SetAttrString()
to assign to variables in the modulealso works.
我如何從 Python 介接到 C++ 物件?¶
根據你的要求不同而有多種不同方法。要手動執行此操作,請先閱讀「擴充和嵌入」說明文件。對於 Python run-time 系統,C 和 C++ 之間並沒有太多區別 —— 因此圍繞 C 結構(指標)型別來構建新 Python 型別的策略也適用於 C++ 物件。
對於 C++ 函式庫,請參閱寫 C 很難;還有其他選擇嗎?。
我使用安裝檔案新增了一個模組,但 make 失敗了;為什麼?¶
Setup must end in a newline, if there is no newline there, the build processfails. (Fixing this requires some ugly shell script hackery, and this bug is sominor that it doesn't seem worth the effort.)
如何為擴充套件除錯?¶
When using GDB with dynamically loaded extensions, you can't set a breakpoint inyour extension until your extension is loaded.
在你的.gdbinit
檔案中(或交互地),新增命令:
br _PyImport_LoadDynamicModule
然後,當你運行 GDB 時:
$gdb/local/bin/pythongdb) run myscript.pygdb) continue # repeat until your extension is loadedgdb) finish # so that your extension is loadedgdb) br myfunction.c:50gdb) continue
我想在我的 Linux 系統上編譯一個 Python 模組,但是缺少一些檔案。為什麼?¶
Most packaged versions of Python omit some filesrequired for compiling Python extensions.
在 Red Hat 上,請安裝 python3-devel RPM 來取得必要的檔案。
對於 Debian,運行apt-getinstallpython3-dev
。
如何從「無效輸入」區分出「不完整輸入」?¶
Sometimes you want to emulate the Python interactive interpreter's behavior,where it gives you a continuation prompt when the input is incomplete (e.g. youtyped the start of an "if" statement or you didn't close your parentheses ortriple string quotes), but it gives you a syntax error message immediately whenthe input is invalid.
在 Python 中,你可以使用codeop
模組,它充分模擬了剖析器 (parser) 的行為。像是 IDLE 就有使用它。
The easiest way to do it in C is to callPyRun_InteractiveLoop()
(perhapsin a separate thread) and let the Python interpreter handle the input foryou. You can also set thePyOS_ReadlineFunctionPointer()
to point at yourcustom input function. SeeModules/readline.c
andParser/myreadline.c
for more hints.
如何找到未定義的 g++ 符號 __builtin_new 或 __pure_virtual?¶
To dynamically load g++ extension modules, you must recompile Python, relink itusing g++ (change LINKCC in the Python Modules Makefile), and link yourextension module using g++ (e.g.,g++-shared-omymodule.somymodule.o
).
我可以用一些用 C 實作的方法和用 Python 實作的其他方法(例如透過繼承)建立一個物件類別嗎?¶
是的,你可以繼承內建類別,例如int
、list
、dict
等。
Boost Python 函式庫(BPL,https://www.boost.org/libs/python/doc/index.html)提供了一種從 C++ 執行此操作的方法(即你可以使用 BPL 來繼承用 C++ 編寫的擴充類別)。