“Python/docs/3.9/extending/index”的版本间差异
来自菜鸟教程
Python/docs/3.9/extending/index
(autoload) |
小 (Page commit) |
||
第1行: | 第1行: | ||
+ | {{DISPLAYTITLE:扩展和嵌入 Python 解释器 — Python 文档}} | ||
<div id="extending-and-embedding-the-python-interpreter" class="section"> | <div id="extending-and-embedding-the-python-interpreter" class="section"> | ||
<span id="extending-index"></span> | <span id="extending-index"></span> | ||
− | = | + | = 扩展和嵌入 Python 解释器 = |
− | + | 本文档描述了如何用 C 或 C++ 编写模块以使用新模块扩展 Python 解释器。 这些模块不仅可以定义新函数,还可以定义新的对象类型及其方法。 该文档还描述了如何将 Python 解释器嵌入到另一个应用程序中,以用作扩展语言。 最后,它展示了如何编译和链接扩展模块,以便它们可以(在运行时)动态加载到解释器中(如果底层操作系统支持此功能)。 | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | + | 本文档假定您了解 Python 的基本知识。 有关该语言的非正式介绍,请参阅 [[../../tutorial/index#tutorial-index|Python 教程]] 。 [[../../reference/index#reference-index|Python 语言参考]] 给出了更正式的语言定义。 [[../../library/index#library-index|Python 标准库]] 记录了现有的对象类型、函数和模块(内置和用 Python 编写),这些对象类型、函数和模块都为该语言提供了广泛的应用范围。 | |
− | |||
− | |||
− | |||
− | Python | ||
− | + | 有关整个 Python/C API 的详细说明,请参阅单独的 [[../../c-api/index#c-api-index|Python/C API 参考手册]] 。 | |
− | [[../../c-api/index#c-api-index| | ||
<div id="recommended-third-party-tools" class="section"> | <div id="recommended-third-party-tools" class="section"> | ||
− | == | + | == 推荐的第三方工具 == |
− | + | 本指南仅涵盖作为此版本 CPython 的一部分提供的用于创建扩展的基本工具。 [http://cython.org/ Cython]、[https://cffi.readthedocs.io cffi]、[http://www.swig.org SWIG] 和 [https://numba.pydata.org/ Numba] 等第三方工具提供了更简单和更复杂的方法来为 Python 创建 C 和 C++ 扩展. | |
− | |||
− | [http://cython.org/ Cython] | ||
− | [http://www.swig.org SWIG] | ||
− | |||
− | |||
<div class="admonition seealso"> | <div class="admonition seealso"> | ||
− | + | 也可以看看 | |
− | ; [https://packaging.python.org/guides/packaging-binary-extensions/ Python | + | ; [https://packaging.python.org/guides/packaging-binary-extensions/ Python 打包用户指南:二进制扩展] |
− | : | + | : Python Packaging User Guide 不仅涵盖了几个可简化二进制扩展创建的可用工具,而且还讨论了为什么首先需要创建扩展模块的各种原因。 |
第45行: | 第30行: | ||
<div id="creating-extensions-without-third-party-tools" class="section"> | <div id="creating-extensions-without-third-party-tools" class="section"> | ||
− | == | + | == 无需第三方工具即可创建扩展 == |
− | + | 本指南的这一部分介绍如何在没有第三方工具帮助的情况下创建 C 和 C++ 扩展。 它主要面向这些工具的创建者,而不是推荐用于创建自己的 C 扩展的方法。 | |
− | |||
− | |||
− | C | ||
<div class="toctree-wrapper compound"> | <div class="toctree-wrapper compound"> | ||
− | * [[../extending|1. | + | * [[../extending|1. 用 C 或 C++ 扩展 Python]] |
− | ** [[../extending#a-simple-example|1.1. | + | ** [[../extending#a-simple-example|1.1. 一个简单的例子]] |
− | ** [[extending | + | ** [[../extending#intermezzo-errors-and-exceptions|1.2. 间奏曲:错误和异常]] |
− | ** [[../extending#back-to-the-example|1.3. | + | ** [[../extending#back-to-the-example|1.3. 回到例子]] |
− | ** [[../extending#the-module-s-method-table-and-initialization-function|1.4. | + | ** [[../extending#the-module-s-method-table-and-initialization-function|1.4. 模块的方法表和初始化函数]] |
− | ** [[../extending#compilation-and-linkage|1.5. | + | ** [[../extending#compilation-and-linkage|1.5. 编译联动]] |
− | ** [[../extending#calling-python-functions-from-c|1.6. | + | ** [[../extending#calling-python-functions-from-c|1.6. 从 C 调用 Python 函数]] |
− | ** [[../extending#extracting-parameters-in-extension-functions|1.7. | + | ** [[../extending#extracting-parameters-in-extension-functions|1.7. 在扩展函数中提取参数]] |
− | ** [[../extending#keyword-parameters-for-extension-functions|1.8. | + | ** [[../extending#keyword-parameters-for-extension-functions|1.8. 扩展函数的关键字参数]] |
− | ** [[../extending#building-arbitrary-values|1.9. | + | ** [[../extending#building-arbitrary-values|1.9. 建立任意值]] |
− | ** [[../extending#reference-counts|1.10. | + | ** [[../extending#reference-counts|1.10. 引用计数]] |
− | ** [[../extending#writing-extensions-in-c|1.11. | + | ** [[../extending#writing-extensions-in-c|1.11. 用 C++ 编写扩展]] |
− | ** [[../extending#providing-a-c-api-for-an-extension-module|1.12. | + | ** [[../extending#providing-a-c-api-for-an-extension-module|1.12. 为扩展模块提供 C API]] |
− | * [[newtypes_tutorial | + | * [[../newtypes_tutorial|2. 定义扩展类型:教程]] |
− | ** [[../newtypes_tutorial#the-basics|2.1. | + | ** [[../newtypes_tutorial#the-basics|2.1. 基础知识]] |
− | ** [[../newtypes_tutorial#adding-data-and-methods-to-the-basic-example|2.2. | + | ** [[../newtypes_tutorial#adding-data-and-methods-to-the-basic-example|2.2. 向 Basic 示例添加数据和方法]] |
− | ** [[../newtypes_tutorial#providing-finer-control-over-data-attributes|2.3. | + | ** [[../newtypes_tutorial#providing-finer-control-over-data-attributes|2.3. 提供对数据属性的更精细控制]] |
− | ** [[../newtypes_tutorial#supporting-cyclic-garbage-collection|2.4. | + | ** [[../newtypes_tutorial#supporting-cyclic-garbage-collection|2.4. 支持循环垃圾回收]] |
− | ** [[../newtypes_tutorial#subclassing-other-types|2.5. | + | ** [[../newtypes_tutorial#subclassing-other-types|2.5. 子类化其他类型]] |
− | * [[newtypes | + | * [[../newtypes|3. 定义扩展类型:分类主题]] |
− | ** [[../newtypes#finalization-and-de-allocation|3.1. | + | ** [[../newtypes#finalization-and-de-allocation|3.1. 完成和取消分配]] |
− | ** [[../newtypes#object-presentation|3.2. | + | ** [[../newtypes#object-presentation|3.2. 对象展示]] |
− | ** [[../newtypes#attribute-management|3.3. | + | ** [[../newtypes#attribute-management|3.3. 属性管理]] |
− | ** [[../newtypes#object-comparison|3.4. | + | ** [[../newtypes#object-comparison|3.4. 对象比较]] |
− | ** [[../newtypes#abstract-protocol-support|3.5. | + | ** [[../newtypes#abstract-protocol-support|3.5. 抽象协议支持]] |
− | ** [[../newtypes#weak-reference-support|3.6. | + | ** [[../newtypes#weak-reference-support|3.6. 弱引用支持]] |
− | ** [[../newtypes#more-suggestions|3.7. | + | ** [[../newtypes#more-suggestions|3.7. 更多建议]] |
− | * [[../building|4. | + | * [[../building|4. 构建 C 和 C++ 扩展]] |
− | ** [[../building#building-c-and-c-extensions-with-distutils|4.1. | + | ** [[../building#building-c-and-c-extensions-with-distutils|4.1. 使用 distutils 构建 C 和 C++ 扩展]] |
− | ** [[../building#distributing-your-extension-modules|4.2. | + | ** [[../building#distributing-your-extension-modules|4.2. 分发扩展模块]] |
− | * [[../windows|5. | + | * [[../windows|5. 在 Windows 上构建 C 和 C++ 扩展]] |
− | ** [[../windows#a-cookbook-approach|5.1. | + | ** [[../windows#a-cookbook-approach|5.1. 食谱方法]] |
− | ** [[../windows#differences-between-unix-and-windows|5.2. | + | ** [[../windows#differences-between-unix-and-windows|5.2. Unix 和 Windows 之间的差异]] |
− | ** [[../windows#using-dlls-in-practice|5.3. | + | ** [[../windows#using-dlls-in-practice|5.3. 在实践中使用 DLL]] |
第95行: | 第77行: | ||
<div id="embedding-the-cpython-runtime-in-a-larger-application" class="section"> | <div id="embedding-the-cpython-runtime-in-a-larger-application" class="section"> | ||
− | == | + | == 在更大的应用程序中嵌入 CPython 运行时 == |
− | + | 有时,与其创建在 Python 解释器中作为主应用程序运行的扩展,不如将 CPython 运行时嵌入到更大的应用程序中。 本节涵盖了成功执行此操作所涉及的一些细节。 | |
− | |||
− | |||
− | |||
<div class="toctree-wrapper compound"> | <div class="toctree-wrapper compound"> | ||
− | * [[../embedding|1. | + | * [[../embedding|1. 在另一个应用程序中嵌入 Python]] |
− | ** [[../embedding#very-high-level-embedding|1.1. | + | ** [[../embedding#very-high-level-embedding|1.1. 非常高级的嵌入]] |
− | ** [[embedding | + | ** [[../embedding#beyond-very-high-level-embedding-an-overview|1.2. 超越非常高级的嵌入:概述]] |
− | ** [[../embedding#pure-embedding|1.3. | + | ** [[../embedding#pure-embedding|1.3. 纯嵌入]] |
− | ** [[../embedding#extending-embedded-python|1.4. | + | ** [[../embedding#extending-embedded-python|1.4. 扩展嵌入式 Python]] |
− | ** [[../embedding#embedding-python-in-c|1.5. | + | ** [[../embedding#embedding-python-in-c|1.5. 在 C++ 中嵌入 Python]] |
− | ** [[../embedding#compiling-and-linking-under-unix-like-systems|1.6. | + | ** [[../embedding#compiling-and-linking-under-unix-like-systems|1.6. 在类 Unix 系统下编译和链接]] |
第118行: | 第97行: | ||
</div> | </div> | ||
+ | <div class="clearer"> | ||
− | [[Category:Python 3.9 | + | |
+ | |||
+ | </div> | ||
+ | |||
+ | [[Category:Python 3.9 文档]] |
2021年10月31日 (日) 04:50的最新版本
扩展和嵌入 Python 解释器
本文档描述了如何用 C 或 C++ 编写模块以使用新模块扩展 Python 解释器。 这些模块不仅可以定义新函数,还可以定义新的对象类型及其方法。 该文档还描述了如何将 Python 解释器嵌入到另一个应用程序中,以用作扩展语言。 最后,它展示了如何编译和链接扩展模块,以便它们可以(在运行时)动态加载到解释器中(如果底层操作系统支持此功能)。
本文档假定您了解 Python 的基本知识。 有关该语言的非正式介绍,请参阅 Python 教程 。 Python 语言参考 给出了更正式的语言定义。 Python 标准库 记录了现有的对象类型、函数和模块(内置和用 Python 编写),这些对象类型、函数和模块都为该语言提供了广泛的应用范围。
有关整个 Python/C API 的详细说明,请参阅单独的 Python/C API 参考手册 。
推荐的第三方工具
本指南仅涵盖作为此版本 CPython 的一部分提供的用于创建扩展的基本工具。 Cython、cffi、SWIG 和 Numba 等第三方工具提供了更简单和更复杂的方法来为 Python 创建 C 和 C++ 扩展.
也可以看看
- Python 打包用户指南:二进制扩展
- Python Packaging User Guide 不仅涵盖了几个可简化二进制扩展创建的可用工具,而且还讨论了为什么首先需要创建扩展模块的各种原因。
无需第三方工具即可创建扩展
本指南的这一部分介绍如何在没有第三方工具帮助的情况下创建 C 和 C++ 扩展。 它主要面向这些工具的创建者,而不是推荐用于创建自己的 C 扩展的方法。
在更大的应用程序中嵌入 CPython 运行时
有时,与其创建在 Python 解释器中作为主应用程序运行的扩展,不如将 CPython 运行时嵌入到更大的应用程序中。 本节涵盖了成功执行此操作所涉及的一些细节。