扩展和嵌入 Python 解释器
************************

This document describes how to write modules in C or C++ to extend the
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
introduction to the language, see Python 教程.  Python 语言参考手册
gives a more formal definition of the language.  Python 标准库
documents the existing object types, functions and modules (both
built-in and written in Python) that give the language its wide
application range.

关于整个 Python/C API 的详细介绍，请参阅独立的 Python/C API 参考手册。


推荐的第三方工具
================

This guide only covers the basic tools for creating extensions
provided as part of this version of CPython. Some third party tools
offer both simpler and more sophisticated approaches to creating C and
C++ extensions for Python.


Creating extensions without third party tools
=============================================

本指南的这一部分包括在没有第三方工具帮助的情况下创建 C 和 C++ 扩展。它
主要用于这些工具的创建者，而不是建议你创建自己的 C 扩展的方法。

参见: **PEP 489** -- Multi-phase extension module initialization

* 1. Extending Python with C or C++

  * 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. 在 C 中调用 Python 函数

  * 1.7. 提取扩展函数的参数

  * 1.8. 给扩展函数的关键字参数

  * 1.9. 构造任意值

  * 1.10. 引用计数

  * 1.11. 在 C++ 中编写扩展

  * 1.12. 给扩展模块提供 C API

* 2. 自定义扩展类型：教程

  * 2.1. 基础

  * 2.2. 向基本示例添加数据和方法

  * 2.3. 提供对于数据属性的更精细控制

  * 2.4. 支持循环垃圾回收

  * 2.5. 子类化其他类型

* 3. 定义扩展类型：杂项主题

  * 3.1. 终结和内存释放

  * 3.2. 对象展示

  * 3.3. 属性管理

  * 3.4. 对象比较

  * 3.5. 抽象协议支持

  * 3.6. 弱引用支持

  * 3.7. 更多建议

* 4. 构建 C/C++ 扩展

  * 4.1. 使用 setuptools 构建 C 和 C++ 扩展

* 5. 在 Windows 上构建 C 和 C++ 扩展

  * 5.1. 菜谱式说明

  * 5.2. Unix 和 Windows 之间的差异

  * 5.3. DLL 的实际使用


在更大的应用程序中嵌入 CPython 运行时
=====================================

有时，不是要创建在 Python 解释器中作为主应用程序运行的扩展，而是希望将
CPython 运行时嵌入到更大的应用程序中。 本节介绍了成功完成此操作所涉及
的一些细节。

* 1. 在其它应用程序中嵌入 Python

  * 1.1. 高层次的嵌入

  * 1.2. 突破高层次嵌入的限制：概述

  * 1.3. 只做嵌入

  * 1.4. 对嵌入 Python 功能进行扩展

  * 1.5. 在 C++ 中嵌入 Python

  * 1.6. 在类 Unix 系统中编译和链接
