“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>
= Extending and Embedding the Python Interpreter =
+
= 扩展和嵌入 Python 解释器 =
  
This document describes how to write modules in C or C++ to extend the Python
+
本文档描述了如何用 C C++ 编写模块以使用新模块扩展 Python 解释器。 这些模块不仅可以定义新函数,还可以定义新的对象类型及其方法。 该文档还描述了如何将 Python 解释器嵌入到另一个应用程序中,以用作扩展语言。 最后,它展示了如何编译和链接扩展模块,以便它们可以(在运行时)动态加载到解释器中(如果底层操作系统支持此功能)。
interpreter with new modules. 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. 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. For an informal
+
本文档假定您了解 Python 的基本知识。 有关该语言的非正式介绍,请参阅 [[../../tutorial/index#tutorial-index|Python 教程]] [[../../reference/index#reference-index|Python 语言参考]] 给出了更正式的语言定义。 [[../../library/index#library-index|Python 标准库]] 记录了现有的对象类型、函数和模块(内置和用 Python 编写),这些对象类型、函数和模块都为该语言提供了广泛的应用范围。
introduction to the language, see [[../../tutorial/index#tutorial-index|<span class="std std-ref">The Python Tutorial</span>]]. [[../../reference/index#reference-index|<span class="std std-ref">The Python Language Reference</span>]]
 
gives a more formal definition of the language. [[../../library/index#library-index|<span class="std std-ref">The Python Standard Library</span>]] documents
 
the existing object types, functions and modules (both built-in and written in
 
Python) that give the language its wide application range.
 
  
For a detailed description of the whole Python/C API, see the separate
+
有关整个 Python/C API 的详细说明,请参阅单独的 [[../../c-api/index#c-api-index|Python/C API 参考手册]]
[[../../c-api/index#c-api-index|<span class="std std-ref">Python/C API Reference Manual</span>]].
 
  
 
<div id="recommended-third-party-tools" class="section">
 
<div id="recommended-third-party-tools" class="section">
  
== Recommended third party tools ==
+
== 推荐的第三方工具 ==
  
This guide only covers the basic tools for creating extensions provided
+
本指南仅涵盖作为此版本 CPython 的一部分提供的用于创建扩展的基本工具。 [http://cython.org/ Cython][https://cffi.readthedocs.io cffi][http://www.swig.org SWIG] [https://numba.pydata.org/ Numba] 等第三方工具提供了更简单和更复杂的方法来为 Python 创建 C C++ 扩展.
as part of this version of CPython. Third party tools like
 
[http://cython.org/ Cython], [https://cffi.readthedocs.io cffi],
 
[http://www.swig.org SWIG] and [https://numba.pydata.org/ Numba]
 
offer both simpler and more sophisticated approaches to creating C and C++
 
extensions for Python.
 
  
 
<div class="admonition seealso">
 
<div class="admonition seealso">
  
参见
+
也可以看看
  
; [https://packaging.python.org/guides/packaging-binary-extensions/ Python Packaging User Guide: Binary Extensions]
+
; [https://packaging.python.org/guides/packaging-binary-extensions/ Python 打包用户指南:二进制扩展]
: 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 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">
  
== Creating extensions without third party tools ==
+
== 无需第三方工具即可创建扩展 ==
  
This section of the guide covers creating C and C++ extensions without
+
本指南的这一部分介绍如何在没有第三方工具帮助的情况下创建 C C++ 扩展。 它主要面向这些工具的创建者,而不是推荐用于创建自己的 C 扩展的方法。
assistance from third party tools. It is intended primarily for creators
 
of those tools, rather than being a recommended way to create your own
 
C extensions.
 
  
 
<div class="toctree-wrapper compound">
 
<div class="toctree-wrapper compound">
  
* [[../extending|1. Extending Python with C or C++]]
+
* [[../extending|1. C C++ 扩展 Python]]
** [[../extending#a-simple-example|1.1. A Simple Example]]
+
** [[../extending#a-simple-example|1.1. 一个简单的例子]]
** [[extending.html#intermezzo-errors-and-exceptions|1.2. Intermezzo: Errors and Exceptions]]
+
** [[../extending#intermezzo-errors-and-exceptions|1.2. 间奏曲:错误和异常]]
** [[../extending#back-to-the-example|1.3. Back to the Example]]
+
** [[../extending#back-to-the-example|1.3. 回到例子]]
** [[../extending#the-module-s-method-table-and-initialization-function|1.4. The Module's Method Table and Initialization Function]]
+
** [[../extending#the-module-s-method-table-and-initialization-function|1.4. 模块的方法表和初始化函数]]
** [[../extending#compilation-and-linkage|1.5. Compilation and Linkage]]
+
** [[../extending#compilation-and-linkage|1.5. 编译联动]]
** [[../extending#calling-python-functions-from-c|1.6. Calling Python Functions from C]]
+
** [[../extending#calling-python-functions-from-c|1.6. 从 C 调用 Python 函数]]
** [[../extending#extracting-parameters-in-extension-functions|1.7. Extracting Parameters in Extension Functions]]
+
** [[../extending#extracting-parameters-in-extension-functions|1.7. 在扩展函数中提取参数]]
** [[../extending#keyword-parameters-for-extension-functions|1.8. Keyword Parameters for Extension Functions]]
+
** [[../extending#keyword-parameters-for-extension-functions|1.8. 扩展函数的关键字参数]]
** [[../extending#building-arbitrary-values|1.9. Building Arbitrary Values]]
+
** [[../extending#building-arbitrary-values|1.9. 建立任意值]]
** [[../extending#reference-counts|1.10. Reference Counts]]
+
** [[../extending#reference-counts|1.10. 引用计数]]
** [[../extending#writing-extensions-in-c|1.11. Writing Extensions in C++]]
+
** [[../extending#writing-extensions-in-c|1.11. C++ 编写扩展]]
** [[../extending#providing-a-c-api-for-an-extension-module|1.12. Providing a C API for an Extension Module]]
+
** [[../extending#providing-a-c-api-for-an-extension-module|1.12. 为扩展模块提供 C API]]
* [[newtypes_tutorial.html|2. Defining Extension Types: Tutorial]]
+
* [[../newtypes_tutorial|2. 定义扩展类型:教程]]
** [[../newtypes_tutorial#the-basics|2.1. The Basics]]
+
** [[../newtypes_tutorial#the-basics|2.1. 基础知识]]
** [[../newtypes_tutorial#adding-data-and-methods-to-the-basic-example|2.2. Adding data and methods to the Basic example]]
+
** [[../newtypes_tutorial#adding-data-and-methods-to-the-basic-example|2.2. Basic 示例添加数据和方法]]
** [[../newtypes_tutorial#providing-finer-control-over-data-attributes|2.3. Providing finer control over data attributes]]
+
** [[../newtypes_tutorial#providing-finer-control-over-data-attributes|2.3. 提供对数据属性的更精细控制]]
** [[../newtypes_tutorial#supporting-cyclic-garbage-collection|2.4. Supporting cyclic garbage collection]]
+
** [[../newtypes_tutorial#supporting-cyclic-garbage-collection|2.4. 支持循环垃圾回收]]
** [[../newtypes_tutorial#subclassing-other-types|2.5. Subclassing other types]]
+
** [[../newtypes_tutorial#subclassing-other-types|2.5. 子类化其他类型]]
* [[newtypes.html|3. Defining Extension Types: Assorted Topics]]
+
* [[../newtypes|3. 定义扩展类型:分类主题]]
** [[../newtypes#finalization-and-de-allocation|3.1. Finalization and De-allocation]]
+
** [[../newtypes#finalization-and-de-allocation|3.1. 完成和取消分配]]
** [[../newtypes#object-presentation|3.2. Object Presentation]]
+
** [[../newtypes#object-presentation|3.2. 对象展示]]
** [[../newtypes#attribute-management|3.3. Attribute Management]]
+
** [[../newtypes#attribute-management|3.3. 属性管理]]
** [[../newtypes#object-comparison|3.4. Object Comparison]]
+
** [[../newtypes#object-comparison|3.4. 对象比较]]
** [[../newtypes#abstract-protocol-support|3.5. Abstract Protocol Support]]
+
** [[../newtypes#abstract-protocol-support|3.5. 抽象协议支持]]
** [[../newtypes#weak-reference-support|3.6. Weak Reference Support]]
+
** [[../newtypes#weak-reference-support|3.6. 弱引用支持]]
** [[../newtypes#more-suggestions|3.7. More Suggestions]]
+
** [[../newtypes#more-suggestions|3.7. 更多建议]]
* [[../building|4. Building C and C++ Extensions]]
+
* [[../building|4. 构建 C C++ 扩展]]
** [[../building#building-c-and-c-extensions-with-distutils|4.1. Building C and C++ Extensions with distutils]]
+
** [[../building#building-c-and-c-extensions-with-distutils|4.1. 使用 distutils 构建 C C++ 扩展]]
** [[../building#distributing-your-extension-modules|4.2. Distributing your extension modules]]
+
** [[../building#distributing-your-extension-modules|4.2. 分发扩展模块]]
* [[../windows|5. Building C and C++ Extensions on Windows]]
+
* [[../windows|5. 在 Windows 上构建 C C++ 扩展]]
** [[../windows#a-cookbook-approach|5.1. A Cookbook Approach]]
+
** [[../windows#a-cookbook-approach|5.1. 食谱方法]]
** [[../windows#differences-between-unix-and-windows|5.2. Differences Between Unix and Windows]]
+
** [[../windows#differences-between-unix-and-windows|5.2. Unix Windows 之间的差异]]
** [[../windows#using-dlls-in-practice|5.3. Using DLLs in Practice]]
+
** [[../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">
  
== Embedding the CPython runtime in a larger application ==
+
== 在更大的应用程序中嵌入 CPython 运行时 ==
  
Sometimes, rather than creating an extension that runs inside the Python
+
有时,与其创建在 Python 解释器中作为主应用程序运行的扩展,不如将 CPython 运行时嵌入到更大的应用程序中。 本节涵盖了成功执行此操作所涉及的一些细节。
interpreter as the main application, it is desirable to instead embed
 
the CPython runtime inside a larger application. This section covers
 
some of the details involved in doing that successfully.
 
  
 
<div class="toctree-wrapper compound">
 
<div class="toctree-wrapper compound">
  
* [[../embedding|1. Embedding Python in Another Application]]
+
* [[../embedding|1. 在另一个应用程序中嵌入 Python]]
** [[../embedding#very-high-level-embedding|1.1. Very High Level Embedding]]
+
** [[../embedding#very-high-level-embedding|1.1. 非常高级的嵌入]]
** [[embedding.html#beyond-very-high-level-embedding-an-overview|1.2. Beyond Very High Level Embedding: An overview]]
+
** [[../embedding#beyond-very-high-level-embedding-an-overview|1.2. 超越非常高级的嵌入:概述]]
** [[../embedding#pure-embedding|1.3. Pure Embedding]]
+
** [[../embedding#pure-embedding|1.3. 纯嵌入]]
** [[../embedding#extending-embedded-python|1.4. Extending Embedded Python]]
+
** [[../embedding#extending-embedded-python|1.4. 扩展嵌入式 Python]]
** [[../embedding#embedding-python-in-c|1.5. Embedding Python in C++]]
+
** [[../embedding#embedding-python-in-c|1.5. C++ 中嵌入 Python]]
** [[../embedding#compiling-and-linking-under-unix-like-systems|1.6. Compiling and Linking under Unix-like systems]]
+
** [[../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 运行时

有时,与其创建在 Python 解释器中作为主应用程序运行的扩展,不如将 CPython 运行时嵌入到更大的应用程序中。 本节涵盖了成功执行此操作所涉及的一些细节。