`
DYER
  • 浏览: 50676 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

Extending and Embedding the Python Interpreter(三)

阅读更多

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对象,而后者通常用来表示错误,就像我们见到的那样。

分享到:
评论

相关推荐

    Python-3-0-1

    It is a companion to Extending and Embedding the Python Interpreter (in Extending and Embedding Python), which describes the general principles of extension writing but does not document the API ...

    Python 2.71 Document

    Welcome! This is the ...Extending and embedding the python interpreter Python /C API reference Manual Installing python modules Documenting Python Python HOWTOs Python Frequently Asked questions

    Python库参考手册.pdf

    Finally, the manual entitled Extending and Embedding the Python Interpreter describes how to add new extensions to Python and how to embed it in other applications. 本手册的读者要对Python 有基本的...

    Python in a Nutshell. A Desktop Quick Reference

    The book focuses on Python’s cross-platform capabilities and covers the basics of extending Python and embedding it in other applications. How This Book Is Organized This book has five parts, as ...

    Python参考手册

    Finally, the manual entitled Extending and Embedding the Python Interpreter describes how to add new extensions to Python and how to embed it in other applications. 本手册的读者要对Python 有基本的认识...

    Python教程(原文).doc

    而关于使用C或C++扩展Python功能,建议阅读《Extending and Embedding the Python Interpreter》和《Python/C API Reference》。 Python不仅适合处理大型项目,提供比shell脚本更强的结构和错误检查,而且它的高阶...

    Python教程(原文).pdf

    对于使用C或C++扩展Python功能,"Extending and Embedding the Python Interpreter"和"Python/C API Reference"是重要的参考资料。 教程中的“开胃菜”部分指出,Python特别适合那些需要在shell脚本的基础上增加...

    python-tutorial.pdf

    如果你计划用C或C++来扩展Python,需要阅读《Extending and Embedding the Python Interpreter》以及《Python/C API Reference》。此外,市面上还有许多专门涵盖Python编程的书籍,这些资源能够进一步提升你的技能和...

    Python 2[1].5 Documentation (19 September 20 06)

    3. **Extending and Embedding the Python Interpreter**:这部分内容主要针对想要在Python之外的环境中使用Python或扩展Python功能的开发者。它讲解了如何用C或C++编写Python扩展模块,以及如何在其他应用程序中...

    Tutorial Python

    对于希望使用C或C++扩展Python解释器的开发者,《Extending and Embedding the Python Interpreter》与《Python/C API Reference》是必读之选。这两份文档详细介绍了如何利用C语言增强Python的功能,以及如何将...

    python3.6.5参考手册 chm

    PEP 486: Make the Python Launcher aware of virtual environments PEP 488: Elimination of PYO files PEP 489: Multi-phase extension module initialization Other Language Changes New Modules typing ...

    Mayavi 参考

    The embedded Python interpreter Recording Mayavi actions to a script Command line arguments mlab: Python scripting for 3D plotting A demo 3D Plotting functions for numpy arrays 0D and 1D data 2D ...

Global site tag (gtag.js) - Google Analytics