设为首页 收藏本站
查看: 3406|回复: 0

[经验分享] Extending and Embedding the Python Interpreter(一)

[复制链接]

尚未签到

发表于 2017-4-28 11:51:26 | 显示全部楼层 |阅读模式
  Let's create an extension module called "spam" (the favorite food of Monty Python fans...) and let's say we want to create a Python interface to the C library function system().This function takes a null-terminated character string as argument and returns an integer. We want this function to be callable from Python as follows:

>>> import spam
>>> status = spam.system("ls -l")
现在我们为C语言编写的库函数system实现一个python接口,使用方式如上述代码所示
Begin by creating a file spammodule.c. (Historically, if a module is called "spam", the C file
containing its implementation is called spammodule.c; if the module name is very long, like
"spammify", the module name can be just spammify.c.)
创建一个名为spammodule.c的文件。

 
  The first line of our file can be:

#include <python.h></python.h>


  which pulls in the Python API (you can add a comment describing the purpose of the module and a copyright notice if you like).
  在spammodule.c文件的开头加入 #include
<python.h></python.h>
,它的作用是将python API导入;必须先于其它标准头文件导入python API。
  All user-visible symbols defined by Python.h have a prefix of "Py" or "PY", except those defined in standard header files. For convenience, and since they are used extensively by the Python interpreter, "Python.h" includes a few standard header files: stdio.h, string.h, errno.h, and stdlib.h<stdlib.h></stdlib.h>. If the latter header file does not exist on your system, it declares the functions malloc(), free() and realloc() directly
  在python.h头文件中已经include <stdio.h></stdio.h>stdio.h, string.h, errno.h, 和stdlib.h<stdlib.h></stdlib.h>几个头文件。
  The next thing we add to our module file is the C function that will be called when the Python expression "spam.system(string)"is evaluated (we'll see shortly how it ends up being called):

static PyObject *
spam_system(PyObject *self, PyObject *args)
{
const char *command;
int sts;
if (!PyArg_ParseTuple(args, "s", &command))
return NULL;
sts = system(command);
return Py_BuildValue("i", sts);
}

  There is a straightforward translation from the argument list in Python (for example, the single expression "ls -l") to the arguments passed to the C function. The C function always has two arguments, conventionally named self and args.
  C语言编写得函数通常有两个参数self和args。
  The self argument is only used when the C function implements a built-in method, not a function. In the example, self will always be a NULL pointer, since we are defining a function, not a method. (This is done so that the interpreter doesn't have to understand two different types of C functions.)
  self参数只在C语言编写得函数用来实现方法时使用,实现为函数时不使用。在这个例子中,self始终是指向NULL的指针。
  The args argument will be a pointer to a Python tuple object containing the arguments. Each item of the tuple corresponds to an argument in the call's argument list. The arguments are Python objects -- in order to do anything with them in our C function we have to convert them to C values. The function PyArg_ParseTuple() in the Python API checks the argument types and converts them to C values. It uses a template string to determine the required types of the arguments as well as the types of the C variables into which to store the converted values. More about this later.
  args参数是一个指向python tuple对象的指针,这个tuple对象包含所有的参数。这些对象都是python对象,当我们要在C代码中操作这些对象时我们需要将这些对象转化为C对象。python api中的PyArg_ParseTuple()函数用来检查这些对象的类别并将它们转化为C对象。
  PyArg_ParseTuple() returns true (nonzero) if all arguments have the right type and its components have been stored in the variables whose addresses are passed. It returns false (zero) if an invalid argument list was passed. In the latter case it also raises an appropriate exception so the calling function can return NULL immediately (as we saw in the example).
  如果所有的参数都有正确的类别并且被存储进设定的变量中,那么PyArg_ParseTuple() 函数返回true。如果有无效参数,那么该函数返回false。
  

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-370394-1-1.html 上篇帖子: Python学习系列-列表 下篇帖子: python图形处理库PIL(Python Image Library)的介绍
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表