Extending Python with C (Python 调用 C) - A Simple Example - Python list

Extending Python with C (Python 调用 C) - A Simple Example - Python list

原文阅读于极客学院 http://www.jikexueyuan.com/

Python/C API 使用简单,可以在 C 代码中操作你的 Python 对象。

需要以特定的方式编写 C 代码以供 Python 去调用。所有的 Python 对象都被表示为一种叫做 PyObject 的结构体,并且 Python.h 头文件中提供了各种操作它的函数。如果 PyObject 表示为 PyListType (列表类型) 时,可以使用 PyList_Size() 函数来获取该结构的长度,类似 Python 中的 len(list) 函数。大部分对 Python 原生对象的基础函数和操作在 Python.h 头文件中都能找到。

Python 调用 C 扩展的代码

# Though it looks like an ordinary python import, the addList module is implemented in C
import addList

l = [1,2,3,4,5]
print "Sum of List - " + str(l) + " = " + str(addList.add(l))
though [ðəʊ]:conj. 即使,虽然,尽管, 纵然 adv. 不过,然而,可是,但是,话虽这样说 prep. 但
ordinary [ˈɔ:dnri]:	adj. 普通的,一般的,平常的,平庸的

导入并使用了一个叫做 addList 的 Python 模块。唯一差别就是这个模块并不是用 Python 编写的,而是 C。

addList.c

/*
 ============================================================================
 Name        : addList.c
 Author      : Foreverstrong Cheng
 Version     : Feb 16, 2019
 Copyright   : Copyright 2019 DeepNorth License
 Description : Extending Python with C or C++, Ansi-style
 ============================================================================
 */

// Python.h has all the required function definitions to manipulate the Python objects.
#include <Python.h>

// This is the function that is called from your Python code.
static PyObject *addList_add(PyObject *self, PyObject *args)
{

	PyObject *listObj;

	// The input arguments come as a tuple, we parse the args to get the various variables.
	// In this case it's only one list variable, which will now be referenced by listObj.
	if (!PyArg_ParseTuple(args, "O", &listObj))
	{
		return NULL;
	}

	// Length of the list.
	long length = PyList_Size(listObj);

	// Iterate over all the elements.
	int i, sum = 0;
	for (i = 0; i < length; i++)
	{
		// Get an element out of the list - the element is also a Python objects.
		PyObject *temp = PyList_GetItem(listObj, i);

		// We know that object represents an integer - so convert it into C long.
		long elem = PyInt_AsLong(temp);
		sum += elem;
	}

	// Value returned back to Python code - another Python object.
	// Build value here converts the C long to a Python integer.
	return Py_BuildValue("i", sum);
}

/*
 ============================================================================
 This is the docstring that corresponds to our 'add' function.
 ============================================================================
 */
static char addList_docs[] = "add( ): add all elements of the list.\n";

/*
 ============================================================================
 This table contains the relavent info mapping -
 <function-name in python module>, <actual-function>,
 <type-of-args the function expects>, <docstring associated with the function>.
 ============================================================================
 */
static PyMethodDef AddList_Methods[] =
{
		{"add", addList_add, METH_VARARGS, addList_docs},
		{NULL, NULL, 0, NULL}
};

/*
 ============================================================================
 addList is the module name, and this is the initialization block of the module.
 ============================================================================
 */
PyMODINIT_FUNC initaddList(void)
{
	(void) Py_InitModule("addList", AddList_Methods);
}

compile_addList.sh

#!/bin/bash

echo "Extending Python with C or C++"

gcc -fPIC -shared addList.c -o addList.so -I/usr/include/python2.7/ -lpython2.7

编写完成 C 模块,将下列代码保存为 setup.py.

# build the modules

from distutils.core import setup, Extension

setup(name='addList', version='1.0',  \
      ext_modules=[Extension('addList', ['adder.c'])])

运行 setup.py,将我们的 C 文件编译安装到我们的 Python 模块中。

python setup.py install

Python.h 头文件中包含了所有需要的类型 (Python 对象类型的表示) 和函数定义 (对 Python 对象的操作)。
编写将要在 Python 调用的函数, 函数传统的命名方式由 {模块名}_{函数名} 组成,将其命名为 addList_add。然后在模块内实现函数的相关信息表,每行一个函数,以 {NULL, NULL, 0, NULL} 作为结束。模块初始化为 PyMODINIT_FUNC init{模块名}。

函数 addList_add 接受的参数类型为 PyObject 类型结构 (同时也表示为元组类型,因为 Python 中皆为对象,所以我们先用 PyObject 来定义)。传入的参数则通过 PyArg_ParseTuple() 来解析。第一个参数是被解析的参数变量。第二个参数是一个字符串,告诉我们如何去解析元组中每一个元素。字符串的第 n 个字母正是代表着元组中第 n 个参数的类型。例如,"i" 代表整形,"s" 代表字符串类型, "O" 则代表一个 Python 对象。接下来的参数是通过 PyArg_ParseTuple() 函数解析并保存元素。这样参数的数量和模块中函数期待得到的参数数量保持一致,并保证了位置的完整性。

例如传入一个字符串,一个整数和一个 Python 列表,可以这样去写:

int n;
char *s;
PyObject* list;
PyArg_ParseTuple(args, "siO", &n, &s, &list);

在这种情况下,我们只需要提取一个列表对象,并将它存储在 listObj 变量中。用列表对象中的 PyList_Size() 函数来获取它的长度,就像 Python 中调用 len(list)。

通过循环列表,使用 PyList_GetItem(list, index) 函数来获取每个元素,返回一个 PyObject * 对象。Python 对象也能表示 PyIntType,只要使用 PyInt_AsLong(PyObj *) 函数便可获得我们所需要的值。我们对每个元素都这样处理,最后得到它们的总和。

总和将被转化为一个 Python 对象并通过 Py_BuildValue() 返回给 Python 代码,这里的 "i" 表示我们要返回一个 Python 整形对象。

验证模块

# module that talks to the C code
import addList

l = [1,2,3,4,5]
print "Sum of List - " + str(l) + " = " + str(addList.add(l))

输出结果:

Sum of List - [1, 2, 3, 4, 5] = 15

Python 调用 C 代码的另一种方式是使用 Cython,让 Python 编译的更快。Cython 和传统的 Python 比起来可以理解为另一种语言。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值