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.
このドキュメントは、C と Python の基礎知識を前提としています。 Python の入門には Python チュートリアル を参照してください。 Python 言語リファレンス には、より厳密な言語の定義があります。 Python 組み込み機能リファレンス は組み込み関数とオブジェクト型を説明し、Python 標準ライブラリ は Python の幅広い用途を支えるモジュール (組み込みモジュールと Python で書かれたモジュールの両方) を説明します。
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. 拡張の型の定義: チュートリアル
- 3. 拡張の型の定義: 雑多なトピック
- 4. C および C++ 拡張のビルド
- 5. Windows 上での C および C++ 拡張モジュールのビルド
大規模なアプリケーションへの Python ランタイムの埋め込み¶
Python インタープリタをメインアプリケーションとして、その中で実行する拡張を作るよりも、大きなアプリケーションの中に CPython ランタイムを組み込む方が望ましい場合があります。 この節では、そのために必要な詳細の一部を説明します。