::-- JinQing [2006-10-17 09:23:37]

22.3. C Extensions Overview

22.3. C扩展概览

Because Python itself is coded in C today, compiled Python extensions can be coded in any language that is C compatible in terms of call stacks and linking. That includes C, but also C++ with appropriate "extern C" declarations (which are automatically provided in Python header files). Regardless of the implementation language, the compiled Python extensions language can take two forms:

因为现在的Python本身是用C语言写的,编译型Python扩展可以用任何C兼容语言编码,只要其调用堆栈与链接方式与C语言兼容。那包括C和C++,但C++需要适当地声明"extern C"(在Python头文件中已自动提供)。不管用什么语言实现,编译型Python扩展语言可采用两种形式:

C modules

  • These look and feel to their clients like Python module files

C types

  • These behave like standard built-in types (numbers, lists, and so on)


C模块

  • 对客户的感观就像是Python模块文件

C类型

  • 它们的行为表现像是标准的内建类型(如数字,列表等等)


Generally, C extension modules are used to implement flat function libraries, and they wind up appearing as importable modules to Python code (hence their name). C extension types are used to code objects that generate multiple instances and may optionally support expression operators much like classes. Because built-in types are really just precoded C extension types, your C extension types can do anything that built-in types can: method calls, addition, indexing, slicing, and so on.[*]

通常,C扩展模块用来实现单一的函数库,它们对Python代码像是可导入模块(所以叫扩展模块)。C扩展类型用来编写对象,对象可生成多个实例,可以支持表达式操作符,就像类。因为内建类型实际上只是预编码的C扩展类型,你的C扩展类型能做到任何内建类型所能做的事:如方法调用,加法,索引,切片,等等。[*]

[*] In fact, every time you make an integer or string in Python, you generate a new C type instance object (whether you know it or not). This isn't as inefficient as you may think, though; as we'll see, type operations are dispatched through fast C pointers, and Python internally caches some integers and strings to avoid object creation when possible.

[*]实际上,每次你用Python构造一个整数或字符串,就产生一个新的C类型的对象实例(不管你是否知道)。但是这并不像你所想的那么低效;我们将会看到,类型操作是通过快速的C指针分派的,并且Python会内部缓存一些整数和字符串来尽力避免创建对象。

Moreover, C extension types today may provide a class-like interface, and so can support customization by either subclassing or coding "wrapper" classes as frontend interfaces to the type. For instance, as we saw in Chapter 20, the Python list object may now be customized by direct subclassing.

此外,C扩展类型现在可以提供一个像类的接口,并能进行定制,如用继承或编写“包装”类作为最后的类型接口。例如,我们在第20章看到,Python list对象现在能通过直接继承来定制。

To make the interface work, both C modules and types must provide a layer of "glue" code that translates calls and data between the two languages. This layer registers C-coded operations with the Python interpreter as C function pointers. In all cases, the C layer is responsible for converting arguments passed from Python to C form and for converting results from C to Python form. Python scripts simply import C extensions and use them as though they were really coded in Python. Because C code does all the translation work, the interface is very seamless and simple in Python scripts.

为了让接口工作,C模块与类型都必须提供一层“粘合”代码,来翻译两种语言之间的调用与数据。这一粘合层在Python解释器中将C编码的操作注册为C函数指针。C语言层负责把Python参数转化为C语言的形式,并将C语言的结果转化为Python的形式。Python脚本只需简单地导入C扩展就可使用它们,就像它们是Python编码的。因为C代码做了所有的翻译工作,Python脚本中的扩展接口是非常简单的并且无缝的。

C modules and types are also responsible for communicating errors back to Python, detecting errors raised by Python API calls, and managing garbage-collector reference counters on objects retained by the C layer indefinitelyPython objects held by your C code won't be garbage-collected as long as you make sure their reference counts don't fall to zero. Once coded, C modules and types may be linked to Python either statically (by rebuilding Python) or dynamically (when first imported). Thereafter, the C extension becomes another toolkit available for use in Python scripts.

C模块与类型也负责向Python回传错误,检测Python API调用引发的错误,并对C语言层保留的不确定的对象进行引用计数和垃圾收集的管理。你的C代码持有的Python对象不会被垃圾收集,只要你确保它们的引用计数不降为零。一旦编码,C模块和类型可以静态链接Python(通过重编译Python),也可以动态链接(即在初次导入时)。之后,C扩展就变成可在Python脚本中使用的又一个工具箱。

1. 讨论 Discussion

...

2. 参考 See Also