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

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

[复制链接]

尚未签到

发表于 2017-4-29 08:32:55 | 显示全部楼层 |阅读模式
  Going back to our example function, you should now be able to understand this statement:

    if (!PyArg_ParseTuple(args, "s", &command))
return NULL;


  It returns NULL (the error indicator for functions returning object pointers) if an error is detected in the argument list, relying on the exception set by PyArg_ParseTuple(). Otherwise the string value of the argument has been copied to the local variable command. This is a pointer assignment and you are not supposed to modify the string to which it points (so in Standard C, the variable command should properly be declared as "const char *command").
  回到我们前面的例子,如果PyArg_ParseTuple()函数在执行过程中发生异常,那么这段代码需要返回一个NULL值表示错误。否则的话,表示参数的字符串会被复制到本地变量command中。command是一个指向字符串的指针常量,在这里并不能去修改这个指针所指向的字符串。

  The next statement is a call to the Unix function system(), passing it the string we just got from PyArg_ParseTuple():

    sts = system(command);


  Our spam.system() function must return the value of sts as a Python object. This is done using the function Py_BuildValue(), which is something like the inverse of PyArg_ParseTuple(): it takes a format string and an arbitrary number of C values, and returns a new Python object. More info on Py_BuildValue() is given later.
  接下去我们要做的就是把刚才得到的代表参数的字符串传入函数system中。当我们在python中调用spam.system()时,我们必须把sts表示成一个python对象。这需要通过Py_BuildValue()函数来实现,这在某种程度上似乎与PyArg_ParseTuple()函数的作用相反:它使用一个format字符串和任意个C代码中的值,把它们转换为一个python对象。
  return Py_BuildValue("i", sts);

  In this case, it will return an integer object. (Yes, even integers are objects on the heap in Python!)
  If you have a C function that returns no useful argument (a function returning void), the corresponding Python function must return None. You need this idiom to do so (which is implemented by the Py_RETURN_NONE macro):
  如果用C代码编写的函数没有返回值,那么相应的python函数需要返回None。需要使用下面这种用法

    Py_INCREF(Py_None);
return Py_None;


  Py_None is the C name for the special Python object None. It is a genuine Python object rather than a NULL pointer, which means ``error'' in most contexts, as we have seen.
  Py_None是C语言中用来代表python 对象None的,它不同于NULL指针,它是真正的python对象,而后者通常用来表示错误,就像我们见到的那样。

运维网声明 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-370593-1-1.html 上篇帖子: 与python内存泄露作斗争 下篇帖子: 在 Python 代码中进行自动测试
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

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

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

扫描微信二维码查看详情

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


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


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


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



合作伙伴: 青云cloud

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