擴充和嵌入 Python 直譯器
************************

這份說明文件描述如何在 C 或 C++ 中編寫模組，並使用新模組來擴充 Python
直譯器功能。那些模組不僅可以定義新的函式，也可以定義新的物件型別及其方
法 (method)。文件內容也會描述如何將 Python 直譯器嵌入另一個應用程式中
，做為一種擴充語言 (extension language) 使用。最後，它會展示如何編譯及
連結擴充模組，使那些模組可以動態地（在執行環境）被載入到直譯器中，前提
是底層作業系統有支援這個功能。

這份說明文件假設您具備 Python 的基礎知識。關於此語言的非正式介紹，請參
閱 Python 教學。Python 語言參考手冊給予此語言更為正式的定義。Python 標
準函式庫 (Standard Library) 記錄了賦予此語言廣泛應用範圍的物件型別、函
式與（內建的和以 Python 編寫的）模組。

關於完整的 Python/C API 詳細介紹，請參閱另外一份 Python/C API 參考手冊
。


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

這份指南僅涵蓋了此 CPython 版本所提供的、用以建立擴充的基本工具。第三
方工具，例如 Cython、cffi、SWIG 和 Numba，提供了更為簡單及更為複雜的多
種方法，來為 Python 建立 C 和 C ++ 擴充。

也參考:

  Python 封裝使用者指南：二進制擴充
     Python 封裝使用者指南 (Python Packaging User Guide) 不僅涵蓋了數
     個可以用來簡化二進制擴充建立過程的工具，也會討論為何建立一個擴充
     模組可能會是您的優先考量。


不使用第三方工具建立擴充
========================

本指南中的這一節將說明，在沒有第三方工具的協助下，如何建立 C 和 C ++
擴充。它主要是寫給使用那些工具的創作者們，而不是讓你建立自己的 C 擴充
的推薦方法。

* 1. 以 C 或 C++ 擴充 Python

  * 1.1. 一個簡單範例

  * 1.2. Intermezzo: Errors and Exceptions

  * 1.3. 回到範例

  * 1.4. The Module's Method Table and Initialization Function

  * 1.5. Compilation and Linkage

  * 1.6. Calling Python Functions from C

  * 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++

  * 1.12. Providing a C API for an Extension Module

* 2. Defining Extension Types: Tutorial

  * 2.1. The Basics

  * 2.2. Adding data and methods to the Basic example

  * 2.3. Providing finer control over data attributes

  * 2.4. Supporting cyclic garbage collection

  * 2.5. Subclassing other types

* 3. Defining Extension Types: Assorted Topics

  * 3.1. Finalization and De-allocation

  * 3.2. Object Presentation

  * 3.3. Attribute Management

  * 3.4. Object Comparison

  * 3.5. Abstract Protocol Support

  * 3.6. Weak Reference Support

  * 3.7. More Suggestions

* 4. Building C and C++ Extensions

  * 4.1. Building C and C++ Extensions with distutils

  * 4.2. Distributing your extension modules

* 5. Building C and C++ Extensions on Windows

  * 5.1. A Cookbook Approach

  * 5.2. Differences Between Unix and Windows

  * 5.3. Using DLLs in Practice


在更大的應用程式中嵌入 CPython 運行環境 (runtime)
=================================================

有時候，相較於建立一個擴充，使其在 Python 直譯器中可作為主應用程式運行
，還不如將 CPython 運行環境嵌入至一個更大的應用程式中更可取。本節將涵
蓋一些要成功完成此任務所涉及的細節。

* 1. 在其它 App 內嵌入 Python

  * 1.1. Very High Level Embedding

  * 1.2. Beyond Very High Level Embedding: An overview

  * 1.3. Pure Embedding

  * 1.4. Extending Embedded Python

  * 1.5. Embedding Python in C++

  * 1.6. Compiling and Linking under Unix-like systems
