ext.pdf

(350 KB) Pobierz
Extending and Embedding the Python Interpreter
ExtendingandEmbeddingthePython
Interpreter
Release2.5
GuidovanRossum
FredL.Drake,Jr.,editor
19thSeptember,2006
PythonSoftwareFoundation
Email: docs@python.org
Copyrightc 2001-2006PythonSoftwareFoundation.Allrightsreserved.
Copyright c 2000BeOpen.com.Allrightsreserved.
Copyrightc 1995-2000CorporationforNationalResearchInitiatives.Allrightsreserved.
Copyright c 1991-1995StichtingMathematischCentrum.Allrightsreserved.
Seetheendofthisdocumentforcompletelicenseandpermissionsinformation.
Abstract
Pythonisaninterpreted,object-orientedprogramminglanguage.Thisdocumentdescribeshowtowritemodules
inCorC ++ toextendthePythoninterpreterwithnewmodules.Thosemodulescandefinenewfunctionsbutalso
newobjecttypesandtheirmethods.ThedocumentalsodescribeshowtoembedthePythoninterpreterinanother
application,foruseasanextensionlanguage.Finally,itshowshowtocompileandlinkextensionmodulessothat
theycanbeloadeddynamically(atruntime)intotheinterpreter,iftheunderlyingoperatingsystemsupportsthis
feature.
ThisdocumentassumesbasicknowledgeaboutPython.Foraninformalintroductiontothelanguage,seethe
PythonTutorial .The PythonReferenceManual givesamoreformaldefinitionofthelanguage.The Python
LibraryReference documentstheexistingobjecttypes,functionsandmodules(bothbuilt-inandwritteninPython)
thatgivethelanguageitswideapplicationrange.
ForadetaileddescriptionofthewholePython/CAPI,seetheseparate Python/CAPIReferenceManual .
CONTENTS
1ExtendingPythonwithCorC ++ 1
1.1ASimpleExample .......................................... 1
1.2 Intermezzo:ErrorsandExceptions ................................. 2
1.3BacktotheExample ......................................... 4
1.4TheModule’sMethodTableandInitializationFunction ...................... 4
1.5CompilationandLinkage ...................................... 6
1.6CallingPythonFunctionsfromC .................................. 6
1.7ExtractingParametersinExtensionFunctions ........................... 8
1.8KeywordParametersforExtensionFunctions ........................... 9
1.9BuildingArbitraryValues ...................................... 10
1.10ReferenceCounts .......................................... 11
1.11WritingExtensionsinC ++ ...................................... 15
1.12ProvidingaCAPIforanExtensionModule ............................ 15
2DefiningNewTypes 19
2.1TheBasics .............................................. 19
2.2TypeMethods ............................................ 43
3BuildingCandC++Extensionswithdistutils 55
3.1Distributingyourextensionmodules ................................ 56
4BuildingCandC ++ ExtensionsonWindows 59
4.1ACookbookApproach ....................................... 59
4.2DifferencesBetweenU NIX andWindows ............................. 61
4.3UsingDLLsinPractice ....................................... 62
5EmbeddingPythoninAnotherApplication 63
5.1VeryHighLevelEmbedding .................................... 63
5.2BeyondVeryHighLevelEmbedding:Anoverview ........................ 64
5.3PureEmbedding ........................................... 64
5.4ExtendingEmbeddedPython .................................... 67
5.5EmbeddingPythoninC ++ ...................................... 67
5.6LinkingRequirements ........................................ 67
AReportingBugs
69
BHistoryandLicense 71
B.1Historyofthesoftware ........................................ 71
B.2TermsandconditionsforaccessingorotherwiseusingPython ................... 72
B.3LicensesandAcknowledgementsforIncorporatedSoftware .................... 74
i
Zgłoś jeśli naruszono regulamin