Python无扩展的动态库接口

一、ctypes

      标准库的一个模块。

      ctypes是最流行的模块,用于动态或共享库的函数调用,而不需要编写自定义C扩展。

     在ctypes中有4种类型的动态库加载器和两个使用它们的约定。表示动态和共享库的类是:

1.1 ctypes.CDLL

1.2 ctypes.PyDLL

 1.3 ctypes.OleDLL和ctypes.WinDLL。

          ctypes.OleDLL和ctypes.WinDLL仅在Windows上可用,关于加载dll动态库,请参考:https//docs.python.org/3.6/ctypes.html

import ctypes
from ctypes.util import find_library
#加载libc.so动态库
libc = ctypes.cdll.LoadLibrary(find_library('c'))

if __name__ == "__main__":
    libc.printf(b"Hello world!\n")

 ctypes类型、C类型与Python类型

类型对应表:

ctypes typeC typePython type
c_bool_Boolbool (1)
c_charchar1-character bytes object
c_wcharwchar_t1-character string
c_bytecharint
c_ubyteunsigned charint
c_shortshortint
c_ushortunsigned shortint
c_intintint
c_uintunsigned intint
c_longlongint
c_ulongunsigned longint
c_longlong__int64 or long longint
c_ulonglongunsigned __int64 or unsigned long longint
c_size_tsize_tint
c_ssize_tssize_t or Py_ssize_tint
c_floatfloatfloat
c_doubledoublefloat
c_longdoublelong doublefloat
c_char_pchar * (NUL terminated)bytes object or None
c_wchar_pwchar_t * (NUL terminated)string or None
c_void_pvoid *int or None

注意:
  1. 上面类型还有别名,比如c_byte的别名就是class ctypes.c_uint8,别名体现了位数。
  2. 上面所有类型都继承class ctypes._SimpleCData,该类只有唯一的属性value

 

二、cffi

   CFFI是Python的一个外部函数接口,是ctypes的一个替代品。它不是标准库的一部分。

# -*- coding: utf-8 -*-
from random import shuffle

from cffi import FFI

ffi = FFI()

ffi.cdef("""
void qsort(void *base, size_t nel, size_t width,
           int (*compar)(const void *, const void *));
""")
C = ffi.dlopen(None)


@ffi.callback("int(void*, void*)")
def cffi_int_compare(a, b):
    # Callback signature requires exact matching of types.
    # this involves less more maginc than in ctypes
    # but also makes you more specific and requires
    # explicit casting
    int_a = ffi.cast('int*', a)[0]
    int_b = ffi.cast('int*', b)[0]
    print(" %s cmp %s" % (int_a, int_b))

    # according to qsort specification this should return:
    # * less than zero if a < b
    # * zero if a == b
    # * more than zero if a > b
    return int_a - int_b


def main():
    numbers = list(range(5))
    shuffle(numbers)
    print("shuffled: ", numbers)

    c_array = ffi.new("int[]", numbers)

    C.qsort(
        # pointer to the sorted array
        c_array,
        # length of the array
        len(c_array),
        # size of single array element
        ffi.sizeof('int'),
        # callback (pointer to the C comparison function)
        cffi_int_compare,
    )
    print("sorted:   ", list(c_array))

if __name__ == "__main__":
    main()

  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值