用户工具

站点工具


linux:python:使用swig连接python与c

差别

这里会显示出您选择的修订版和当前版本之间的差别。

到此差别页面的链接

两侧同时换到之前的修订记录前一修订版
后一修订版
前一修订版
linux:python:使用swig连接python与c [2020/10/16 00:39] – zhangguolinux:python:使用swig连接python与c [2026/10/13 22:51] (当前版本) – 外部编辑 127.0.0.1
行 1: 行 1:
-====== 使用swig连接python与c ====== +# 使用swig连接python与c
 注:仅示例python3 注:仅示例python3
  
 +swig官方文档:http://www.swig.org/Doc3.0/SWIGDocumentation.html#SWIG_nn3,  目前swig的最新版本已经到了4.0
 +
 +
 +## - python端发送bytes数据到c
 通过python发一组数据到C,C调用底层硬件完成数据收尾操作,数据主要是char *的形式到c,python这边需要使用bytes对其相连。 通过python发一组数据到C,C调用底层硬件完成数据收尾操作,数据主要是char *的形式到c,python这边需要使用bytes对其相连。
  
  
-<code python>+ 
 +```python
 from binascii import b2a_hex, a2b_hex from binascii import b2a_hex, a2b_hex
  
行 20: 行 24:
  
 io.py2c_test(a2b,7)   #以c的交互需要使用以byte(a2b)表示的bytes,不能是binascii.b2a_hex之后的。 io.py2c_test(a2b,7)   #以c的交互需要使用以byte(a2b)表示的bytes,不能是binascii.b2a_hex之后的。
-</code>+```
  
  
-在swig的.i文件中需要加入以下内容,表示c char*之间与python bytes相对而不是采用unicode。这一点相当重要,在python中不要使用字符串与C之间传递,而应该用bytes来传递,简单可行,不会发生字符转换规则以及check等等问题(latin-1转换>0x80的字符时有问题,utf8本身也不是属于byte0-255之间的字符定义)。 + 
-<code swig>+在swig的.i文件中需要加入以下内容(仅支持python3),表示c char*之间与python bytes相对而不是采用unicode。这一点相当重要,在python中不要使用字符串与C之间传递,而应该用bytes来传递,简单可行,不会发生字符转换规则以及check等等问题(latin-1转换>0x80的字符时有问题,utf8本身也不是属于byte0-255之间的字符定义)。 
 + 
 +```swig
  
 %begin %{ %begin %{
 #define SWIG_PYTHON_STRICT_BYTE_CHAR #define SWIG_PYTHON_STRICT_BYTE_CHAR
 %} %}
 +```
  
-</code> 
  
  
 c侧程序示例如下:  注:参数只能写成 char * ,不能写成unsigned char*, 但是可以在c内部通过强制转换将其转换成unsigned char *。 c侧程序示例如下:  注:参数只能写成 char * ,不能写成unsigned char*, 但是可以在c内部通过强制转换将其转换成unsigned char *。
-<code c>+ 
 +```c
 void py2c_test(char * tdi_bytes, int bytes_size) void py2c_test(char * tdi_bytes, int bytes_size)
 { {
行 43: 行 50:
     }     }
 } }
-</code>+``` 
 + 
 + 
 + 
 +## - c返回bytes数据给python 
 +如果直接用c的char *传数据的话,会在遇到0时,自动把数据截止掉。举个例子: 
 + 
 +c原来要返回的数据是0x55001233, python端会收到0x1233这个bytes数据,因为后面遇到0x00,被截止掉。 
 + 
 +所以C端返回这样的数据需要使用到struct, c端建立以下的struct: 
 + 
 + 
 +```c 
 +typedef struct swig_bytes_data 
 +{ 
 +        int size; 
 +        char *bytes; 
 +} swig_bytes_data; 
 +``` 
 + 
 + 
 +c端要返回给python的数据,指定好正确的bytes和size之后,python端即能接收到正常的数据。 
 + 
 +然后在xxx.i文件中,指定typemap(out) 
 + 
 + 
 +``` 
 + 
 +%typemap(out) swig_bytes_data 
 +{ 
 +        $result = PyBytes_FromStringAndSize($1.bytes, $1.size); 
 +        free($1.bytes); 
 +} 
 +``` 
 + 
 + 
 + 
 +### - 语法参考 
 +http://vmcc.vicp.net:9090/python/c-api/bytes.html 
 + 
 +http://vmcc.vicp.net:9090/python/c-api/concrete.html#sequence-objects 
 + 
 + 
 +### - 相关function 
 + 
 +```c 
 +PyObject* PyBytes_FromStringAndSize(const char *v, Py_ssize_t len) 
 +//Return value: New reference. 
 +//Return a new bytes object with a copy of the string v as value and length len on success, and NULL on failure. If v is NULL, the contents of the bytes object are uninitialized. 
 + 
 + 
 + 
 +PyObject* PyByteArray_FromStringAndSize(const char *string, Py_ssize_t len) 
 +//Return value: New reference. 
 +//Create a new bytearray object from string and its length, len. On failure, NULL is returned. 
 + 
 + 
 + 
 +PyObject* PyUnicode_FromStringAndSize(const char *u, Py_ssize_t size) 
 +//Return value: New reference. 
 +//Create a Unicode object from the char buffer u. The bytes will be interpreted as being UTF-8 encoded. The buffer is copied into the new object. If the buffer is not NULL, the return value might be a shared object, i.e. modification of the data is not allowed. 
 + 
 +//If u is NULL, this function behaves like PyUnicode_FromUnicode() with the buffer set to NULL. This usage is deprecated in favor of PyUnicode_New(). 
 +```
  
linux/python/使用swig连接python与c.1602779947.txt.gz · 最后更改: (外部编辑)

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki