歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Python 外部函數調用庫ctypes簡介

Python 外部函數調用庫ctypes簡介

日期:2017/3/1 9:08:55   编辑:Linux編程

Table of Contents

  • 1. 參考資料
  • 2. ctypes簡介
    • 2.1. 數據類型
    • 2.2. 調用.so/.dll
      • 2.2.1. 加載動態鏈接庫
      • 2.2.2. 調用加載的函數
      • 2.2.3. 設置個性化參數
      • 2.2.4. 指定函數需要參數類型和返回類型
  • 3. 題外話

參考資料

  • https://docs.python.org/2.7/library/ctypes.html
  • http://www.linuxidc.com/Linux/2016-12/137761.htm

ctypes簡介

一直對不同語言間的交互感興趣,python和C語言又深有淵源,所以對python和c語言交互產生了興趣。
最近了解了python提供的一個外部函數庫 ctypes, 它提供了C語言兼容的幾種數據類型,並且可以允許調用C編譯好的庫。
這裡是閱讀相關資料的一個記錄,內容大部分來自官方文檔。

數據類型

ctypes 提供了一些原始的C語言兼容的數據類型,參見下表,其中第一列是在ctypes庫中定義的變量類型,第二列是C語言定義的變量類型,第三列是Python語言在不使用ctypes時定義的變量類型。

| ctypes type  | C type                                 | Python type                |
|--------------+----------------------------------------+----------------------------|
| c_bool       | _Bool                                  | bool (1)                   |
| c_char       | char                                   | 1-character string         |
| c_wchar      | wchar_t                                | 1-character unicode string |
| c_byte       | char                                   | int/long                   |
| c_ubyte      | unsigned char                          | int/long                   |
| c_short      | short                                  | int/long                   |
| c_ushort     | unsigned short                         | int/long                   |
| c_int        | int                                    | int/long                   |
| c_uint       | unsigned int                           | int/long                   |
| c_long       | long                                   | int/long                   |
| c_ulong      | unsigned long                          | int/long                   |
| c_longlong   | __int64 or long long                   | int/long                   |
| c_ulonglong  | unsigned __int64 or unsigned long long | int/long                   |
| c_float      | float                                  | float                      |
| c_double     | double                                 | float                      |
| c_longdouble | long double                            | float                      |
| c_char_p     | char * (NUL terminated)                | string or None             |
| c_wchar_p    | wchar_t * (NUL terminated)             | unicode or None            |
| c_void_p     | void *                                 | int/long or None           |

創建簡單的ctypes類型如下:

>>> c_int()
c_long(0)
>>> c_char_p("Hello, World")
c_char_p('Hello, World')
>>> c_ushort(-3)
c_ushort(65533)
>>>

使用 .value 訪問和改變值:

>>> i = c_int(42)
>>> print i
c_long(42)
>>> print i.value
42
>>> i.value = -99
>>> print i.value
-99
>>>

改變指針類型的變量值:

>>> s = "Hello, World"
>>> c_s = c_char_p(s)
>>> print c_s
c_char_p('Hello, World')
>>> c_s.value = "Hi, there"
>>> print c_s
c_char_p('Hi, there')
>>> print s                 # 一開始賦值的字符串並不會改變, 因為這裡指針的實例改變的是指向的內存地址,不是直接改變內存裡的內容
Hello, World
>>>

如果需要直接操作內存地址的數據類型:

>>> from ctypes import *
>>> p = create_string_buffer(3)      # create a 3 byte buffer, initialized to NUL bytes
>>> print sizeof(p), repr(p.raw)
3 '\x00\x00\x00'
>>> p = create_string_buffer("Hello")      # create a buffer containing a NUL terminated string
>>> print sizeof(p), repr(p.raw)          # .raw 訪問內存裡存儲的內容
6 'Hello\x00'
>>> print repr(p.value)                   # .value 訪問值
'Hello'
>>> p = create_string_buffer("Hello", 10)  # create a 10 byte buffer
>>> print sizeof(p), repr(p.raw)
10 'Hello\x00\x00\x00\x00\x00'
>>> p.value = "Hi"
>>> print sizeof(p), repr(p.raw)
10 'Hi\x00lo\x00\x00\x00\x00\x00'
>>>

下面的例子演示了使用C的數組和結構體:

>>> class POINT(Structure):                 # 定義一個結構,內含兩個成員變量 x,y,均為 int 型
...     _fields_ = [("x", c_int),
...                 ("y", c_int)]
...
>>> point = POINT(2,5)                            # 定義一個 POINT 類型的變量,初始值為 x=2, y=5
>>> print point.x, point.y                     # 打印變量
2 5
>>> point = POINT(y=5)                                  # 重新定義一個 POINT 類型變量,x 取默認值
>>> print point.x, point.y                     # 打印變量
0 5
>>> POINT_ARRAY = POINT * 3                    # 定義 POINT_ARRAY 為 POINT 的數組類型
# 定義一個 POINT 數組,內含三個 POINT 變量
>>> pa = POINT_ARRAY(POINT(7, 7), POINT(8, 8), POINT(9, 9))
>>> for p in pa: print p.x, p.y                # 打印 POINT 數組中每個成員的值
...
7 7
8 8
9 9

創建指針實例

>>> from ctypes import *
>>> i = c_int(42)
>>> pi = pointer(i)
>>>

>>> pi.contents
c_long(42)
>>>

使用cast()類型轉換

>>> class Bar(Structure):
...     _fields_ = [("count", c_int), ("values", POINTER(c_int))]
...
>>> bar = Bar()
>>> bar.values = (c_int * 3)(1, 2, 3)
>>> bar.count = 3
>>> for i in range(bar.count):
...     print bar.values[i]
...
1
2
3
>>>


>>> bar = Bar()
>>> bar.values = cast((c_byte * 4)(), POINTER(c_int))   # 這裡轉成需要的類型
>>> print bar.values[0]
0
>>>

類似於C語言定義函數時,會先定義返回類型,然後具體實現再定義,當遇到下面這種情況時,也需要這麼干:

>>> class cell(Structure):
...     _fields_ = [("name", c_char_p),
...                 ("next", POINTER(cell))]
...
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
  File "<stdin>", line 2, in cell
NameError: name 'cell' is not defined
>>>

# 不能調用自己,所以得像下面這樣
>>> from ctypes import *
>>> class cell(Structure):
...     pass
...
>>> cell._fields_ = [("name", c_char_p),
...                  ("next", POINTER(cell))]
>>>

調用.so/.dll

可以簡單地將"so"和"dll"理解成Linux和windows上動態鏈接庫的指代,這裡我們以Linux為例。注意,ctypes提供的接口會在不同系統上有出入,比如為了加載動態鏈接庫, 在Linux上提供的是 cdll, 而在Windows上提供的是 windlloledll

加載動態鏈接庫

from ctypes import *
>>> cdll.LoadLibrary("libc.so.6")
<CDLL 'libc.so.6', handle ... at ...>
>>> libc = CDLL("libc.so.6")
>>> libc
<CDLL 'libc.so.6', handle ... at ...>
>>>

調用加載的函數

>>> print libc.time(None)
1150640792
>>> print hex(windll.kernel32.GetModuleHandleA(None))
0x1d000000
>>>

設置個性化參數

ctypes會尋找 _as_paramter_ 屬性來用作調用函數的參數傳入,這樣就可以傳入自己定義的類作為參數,示例如下:

>>> class Bottles(object):
...     def __init__(self, number):
...         self._as_parameter_ = number
...
>>> bottles = Bottles(42)
>>> printf("%d bottles of beer\n", bottles)
42 bottles of beer
19
>>>

指定函數需要參數類型和返回類型

argtypesrestype 來指定調用的函數返回類型。

>>> printf.argtypes = [c_char_p, c_char_p, c_int, c_double]
>>> printf("String '%s', Int %d, Double %f\n", "Hi", 10, 2.2)
String 'Hi', Int 10, Double 2.200000
37
>>>



>>> strchr = libc.strchr
>>> strchr("abcdef", ord("d"))
8059983
>>> strchr.restype = c_char_p   # c_char_p is a pointer to a string
>>> strchr("abcdef", ord("d"))
'def'
>>> print strchr("abcdef", ord("x"))
None
>>>

這裡我只是列出了 ctypes 最基礎的部分,還有很多細節請參考官方文檔。

Copyright © Linux教程網 All Rights Reserved