Extending and Embedding the Python Interpreter扩展和嵌入Python解释器¶
This document describes how to write modules in C or C++ to extend the Python interpreter with new modules. 本文档描述了如何用C或C++编写模块,以使用新模块扩展Python解释器。Those modules can not only define new functions but also new object types and their methods. 这些模块不仅可以定义新函数,还可以定义新对象类型及其方法。The document also describes how to embed the Python interpreter in another application, for use as an extension language. 该文档还描述了如何将Python解释器嵌入到另一个应用程序中,用作扩展语言。Finally, it shows how to compile and link extension modules so that they can be loaded dynamically (at run time) into the interpreter, if the underlying operating system supports this feature.最后,它展示了如何编译和链接扩展模块,以便在底层操作系统支持此功能的情况下,将它们动态(在运行时)加载到解释器中。
This document assumes basic knowledge about Python. 本文档假定了解Python的基本知识。For an informal introduction to the language, see The Python Tutorial. 有关该语言的非正式介绍,请参阅Python教程。The Python Language ReferencePython语言参考 gives a more formal definition of the language. 给出了更正式的语言定义。The Python Standard LibraryPython标准库 documents the existing object types, functions and modules (both built-in and written in Python) that give the language its wide application range.文档记录了现有的对象类型、函数和模块(内置的和用Python编写的),这些都为该语言提供了广泛的应用范围。
For a detailed description of the whole Python/C API, see the separate Python/C API Reference Manual.有关整个Python/C API的详细描述,请参阅单独的Python/C API参考手册。
Recommended third party tools推荐的第三方工具¶
This guide only covers the basic tools for creating extensions provided as part of this version of CPython. 本指南仅涵盖作为此版本CPython的一部分提供的用于创建扩展的基本工具。Third party tools like Cython, cffi, SWIG and Numba offer both simpler and more sophisticated approaches to creating C and C++
extensions for Python.Cython、cffi、SWIG和Numba等第三方工具提供了为Python创建C和C++扩展的更简单和更复杂的方法。
See also
Python Packaging User Guide: Binary ExtensionsPython打包用户指南:二进制扩展The Python Packaging User Guide not only covers several available tools that simplify the creation of binary extensions, but also discusses the various reasons why creating an extension module may be desirable in the first place.《Python打包用户指南》不仅涵盖了几种简化二进制扩展创建的可用工具,还讨论了创建扩展模块的各种原因。
Creating extensions without third party tools在没有第三方工具的情况下创建扩展¶
This section of the guide covers creating C and C++ extensions without assistance from third party tools. 本节指南介绍了在没有第三方工具帮助的情况下创建C和C++扩展。It is intended primarily for creators of those tools, rather than being a recommended way to create your own C extensions.它主要面向这些工具的创建者,而不是创建自己的C扩展的推荐方式。
- 1.
Extending Python with C or C++用C或C++扩展Python- 1.1.
A Simple Example一个简单的例子 - 1.2.
Intermezzo: Errors and Exceptions间奏:错误和例外 - 1.3.
Back to the Example回到示例 - 1.4.
The Module’s Method Table and Initialization Function模块的方法表和初始化功能 - 1.5.
Compilation and Linkage编译和链接 - 1.6.
Calling Python Functions from C从C调用Python函数 - 1.7.
Extracting Parameters in Extension Functions提取扩展函数中的参数 - 1.8.
Keyword Parameters for Extension Functions扩展函数的关键字参数 - 1.9.
Building Arbitrary Values生成任意值 - 1.10.
Reference Counts引用计数 - 1.11.
Writing Extensions in C++用C语言编写扩展++ - 1.12.
Providing a C API for an Extension Module为扩展模块提供C API
- 1.1.
- 2.
Defining Extension Types: Tutorial定义扩展类型:教程 - 3.
Defining Extension Types: Assorted Topics定义扩展类型:分类主题 - 4.
Building C and C++ Extensions构建C和C++扩展 - 5.
Building C and C++ Extensions on Windows在Windows上构建C和C++扩展
Embedding the CPython runtime in a larger application在更大的应用程序中嵌入CPython运行时¶
Sometimes, rather than creating an extension that runs inside the Python interpreter as the main application, it is desirable to instead embed the CPython runtime inside a larger application. 有时,与其创建一个在Python解释器内部作为主应用程序运行的扩展,不如将CPython运行时嵌入到更大的应用程序中。This section covers some of the details involved in doing that successfully.本节介绍了成功实现这一目标所涉及的一些细节。