extending.pdf

(393 KB) Pobierz
Extending and Embedding Python
Release2.7.6
Guido van Rossum
Fred L. Drake, Jr., editor
March 17, 2014
Python Software Foundation
Email:docs@python.org
CONTENTS
1
Extending Python with C or C++
3
1.1
A Simple Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
3
1.2
Intermezzo: Errors and Exceptions
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
4
1.3
Back to the Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
6
1.4
The Module’s Method Table and Initialization Function
. . . . . . . . . . . . . . . . . . . . . .
7
1.5
Compilation and Linkage
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
8
1.6
Calling Python Functions from C
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
8
1.7
Extracting Parameters in Extension Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . .
10
1.8
Keyword Parameters for Extension Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . .
11
1.9
Building Arbitrary Values
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
12
1.10
Reference Counts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
13
1.11
Writing Extensions in C++ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
16
1.12
Providing a C API for an Extension Module . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
16
2
Defining New Types
21
2.1
The Basics
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
21
2.2
Type Methods
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
47
3
Building C and C++ Extensions with distutils
57
3.1
Distributing your extension modules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
58
4
Building C and C++ Extensions on Windows
59
4.1
A Cookbook Approach . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
59
4.2
Differences Between Unix and Windows
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
61
4.3
Using DLLs in Practice
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
62
5
Embedding Python in Another Application
63
5.1
Very High Level Embedding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
63
5.2
Beyond Very High Level Embedding: An overview . . . . . . . . . . . . . . . . . . . . . . . . .
64
5.3
Pure Embedding
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
64
5.4
Extending Embedded Python
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
66
5.5
Embedding Python in C++ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
67
5.6
Compiling and Linking under Unix-like systems
. . . . . . . . . . . . . . . . . . . . . . . . . .
67
A
Glossary
69
B
About these documents
77
B.1
Contributors to the Python Documentation
. . . . . . . . . . . . . . . . . . . . . . . . . . . . .
77
C
History and License
79
C.1
History of the software . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
79
C.2
Terms and conditions for accessing or otherwise using Python . . . . . . . . . . . . . . . . . . .
79
C.3
Licenses and Acknowledgements for Incorporated Software
. . . . . . . . . . . . . . . . . . . .
82
i
D
Copyright
93
Index
95
ii
Extending and Embedding Python, Release 2.7.6
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 tutorial-
index. reference-index gives a more formal definition of the language. library-index 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 c-api-index.
Note: This guide only covers the basic tools for creating extensions provided as part of this version of CPython.
Third party tools may offer simpler alternatives. Refer to the binary extensions section in the Python Packaging
User Guide for more information.
CONTENTS
1
1276746350.001.png 1276746350.002.png 1276746350.003.png
 
Zgłoś jeśli naruszono regulamin