~dkuhlman/python-training-materials/Materials

« back to all changes in this revision

Viewing changes to python-3.5.1-docs-html/extending/index.html

  • Committer: Dave Kuhlman
  • Date: 2017-04-15 16:24:56 UTC
  • Revision ID: dkuhlman@davekuhlman.org-20170415162456-iav9vozzg4iwqwv3
Updated docs

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
2
 
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
3
 
 
4
 
 
5
 
<html xmlns="http://www.w3.org/1999/xhtml">
6
 
  <head>
7
 
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
8
 
    
9
 
    <title>Extending and Embedding the Python Interpreter &mdash; Python 3.5.1 documentation</title>
10
 
    
11
 
    <link rel="stylesheet" href="../_static/pydoctheme.css" type="text/css" />
12
 
    <link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
13
 
    
14
 
    <script type="text/javascript">
15
 
      var DOCUMENTATION_OPTIONS = {
16
 
        URL_ROOT:    '../',
17
 
        VERSION:     '3.5.1',
18
 
        COLLAPSE_INDEX: false,
19
 
        FILE_SUFFIX: '.html',
20
 
        HAS_SOURCE:  true
21
 
      };
22
 
    </script>
23
 
    <script type="text/javascript" src="../_static/jquery.js"></script>
24
 
    <script type="text/javascript" src="../_static/underscore.js"></script>
25
 
    <script type="text/javascript" src="../_static/doctools.js"></script>
26
 
    <script type="text/javascript" src="../_static/sidebar.js"></script>
27
 
    <link rel="search" type="application/opensearchdescription+xml"
28
 
          title="Search within Python 3.5.1 documentation"
29
 
          href="../_static/opensearch.xml"/>
30
 
    <link rel="author" title="About these documents" href="../about.html" />
31
 
    <link rel="copyright" title="Copyright" href="../copyright.html" />
32
 
    <link rel="top" title="Python 3.5.1 documentation" href="../contents.html" />
33
 
    <link rel="next" title="1. Extending Python with C or C++" href="extending.html" />
34
 
    <link rel="prev" title="37. Undocumented Modules" href="../library/undoc.html" />
35
 
    <link rel="shortcut icon" type="image/png" href="../_static/py.png" />
36
 
    <script type="text/javascript" src="../_static/copybutton.js"></script>
37
 
    <script type="text/javascript" src="../_static/version_switch.js"></script>
38
 
    
39
 
 
40
 
 
41
 
  </head>
42
 
  <body role="document">  
43
 
    <div class="related" role="navigation" aria-label="related navigation">
44
 
      <h3>Navigation</h3>
45
 
      <ul>
46
 
        <li class="right" style="margin-right: 10px">
47
 
          <a href="../genindex.html" title="General Index"
48
 
             accesskey="I">index</a></li>
49
 
        <li class="right" >
50
 
          <a href="../py-modindex.html" title="Python Module Index"
51
 
             >modules</a> |</li>
52
 
        <li class="right" >
53
 
          <a href="extending.html" title="1. Extending Python with C or C++"
54
 
             accesskey="N">next</a> |</li>
55
 
        <li class="right" >
56
 
          <a href="../library/undoc.html" title="37. Undocumented Modules"
57
 
             accesskey="P">previous</a> |</li>
58
 
        <li><img src="../_static/py.png" alt=""
59
 
                 style="vertical-align: middle; margin-top: -1px"/></li>
60
 
        <li><a href="https://www.python.org/">Python</a> &raquo;</li>
61
 
        <li>
62
 
          <span class="version_switcher_placeholder">3.5.1</span>
63
 
          <a href="../index.html">Documentation </a> &raquo;
64
 
        </li>
65
 
 
66
 
      </ul>
67
 
    </div>    
68
 
 
69
 
    <div class="document">
70
 
      <div class="documentwrapper">
71
 
        <div class="bodywrapper">
72
 
          <div class="body" role="main">
73
 
            
74
 
  <div class="section" id="extending-and-embedding-the-python-interpreter">
75
 
<span id="extending-index"></span><h1>Extending and Embedding the Python Interpreter<a class="headerlink" href="#extending-and-embedding-the-python-interpreter" title="Permalink to this headline">¶</a></h1>
76
 
<p>This document describes how to write modules in C or C++ to extend the Python
77
 
interpreter with new modules.  Those modules can not only define new functions
78
 
but also new object types and their methods.  The document also describes how
79
 
to embed the Python interpreter in another application, for use as an extension
80
 
language.  Finally, it shows how to compile and link extension modules so that
81
 
they can be loaded dynamically (at run time) into the interpreter, if the
82
 
underlying operating system supports this feature.</p>
83
 
<p>This document assumes basic knowledge about Python.  For an informal
84
 
introduction to the language, see <a class="reference internal" href="../tutorial/index.html#tutorial-index"><span>The Python Tutorial</span></a>.  <a class="reference internal" href="../reference/index.html#reference-index"><span>The Python Language Reference</span></a>
85
 
gives a more formal definition of the language.  <a class="reference internal" href="../library/index.html#library-index"><span>The Python Standard Library</span></a> documents
86
 
the existing object types, functions and modules (both built-in and written in
87
 
Python) that give the language its wide application range.</p>
88
 
<p>For a detailed description of the whole Python/C API, see the separate
89
 
<a class="reference internal" href="../c-api/index.html#c-api-index"><span>Python/C API Reference Manual</span></a>.</p>
90
 
<div class="section" id="recommended-third-party-tools">
91
 
<h2>Recommended third party tools<a class="headerlink" href="#recommended-third-party-tools" title="Permalink to this headline">¶</a></h2>
92
 
<p>This guide only covers the basic tools for creating extensions provided
93
 
as part of this version of CPython. Third party tools like Cython,
94
 
<code class="docutils literal"><span class="pre">cffi</span></code>, SWIG and Numba offer both simpler and more sophisticated
95
 
approaches to creating C and C++ extensions for Python.</p>
96
 
<div class="admonition seealso">
97
 
<p class="first admonition-title">See also</p>
98
 
<dl class="last docutils">
99
 
<dt><a class="reference external" href="https://packaging.python.org/en/latest/extensions.html">Python Packaging User Guide: Binary Extensions</a></dt>
100
 
<dd>The Python Packaging User Guide not only covers several available
101
 
tools that simplify the creation of binary extensions, but also
102
 
discusses the various reasons why creating an extension module may be
103
 
desirable in the first place.</dd>
104
 
</dl>
105
 
</div>
106
 
</div>
107
 
<div class="section" id="creating-extensions-without-third-party-tools">
108
 
<h2>Creating extensions without third party tools<a class="headerlink" href="#creating-extensions-without-third-party-tools" title="Permalink to this headline">¶</a></h2>
109
 
<p>This section of the guide covers creating C and C++ extensions without
110
 
assistance from third party tools. It is intended primarily for creators
111
 
of those tools, rather than being a recommended way to create your own
112
 
C extensions.</p>
113
 
<div class="toctree-wrapper compound">
114
 
<ul>
115
 
<li class="toctree-l1"><a class="reference internal" href="extending.html">1. Extending Python with C or C++</a><ul>
116
 
<li class="toctree-l2"><a class="reference internal" href="extending.html#a-simple-example">1.1. A Simple Example</a></li>
117
 
<li class="toctree-l2"><a class="reference internal" href="extending.html#intermezzo-errors-and-exceptions">1.2. Intermezzo: Errors and Exceptions</a></li>
118
 
<li class="toctree-l2"><a class="reference internal" href="extending.html#back-to-the-example">1.3. Back to the Example</a></li>
119
 
<li class="toctree-l2"><a class="reference internal" href="extending.html#the-module-s-method-table-and-initialization-function">1.4. The Module&#8217;s Method Table and Initialization Function</a></li>
120
 
<li class="toctree-l2"><a class="reference internal" href="extending.html#compilation-and-linkage">1.5. Compilation and Linkage</a></li>
121
 
<li class="toctree-l2"><a class="reference internal" href="extending.html#calling-python-functions-from-c">1.6. Calling Python Functions from C</a></li>
122
 
<li class="toctree-l2"><a class="reference internal" href="extending.html#extracting-parameters-in-extension-functions">1.7. Extracting Parameters in Extension Functions</a></li>
123
 
<li class="toctree-l2"><a class="reference internal" href="extending.html#keyword-parameters-for-extension-functions">1.8. Keyword Parameters for Extension Functions</a></li>
124
 
<li class="toctree-l2"><a class="reference internal" href="extending.html#building-arbitrary-values">1.9. Building Arbitrary Values</a></li>
125
 
<li class="toctree-l2"><a class="reference internal" href="extending.html#reference-counts">1.10. Reference Counts</a></li>
126
 
<li class="toctree-l2"><a class="reference internal" href="extending.html#writing-extensions-in-c">1.11. Writing Extensions in C++</a></li>
127
 
<li class="toctree-l2"><a class="reference internal" href="extending.html#providing-a-c-api-for-an-extension-module">1.12. Providing a C API for an Extension Module</a></li>
128
 
</ul>
129
 
</li>
130
 
<li class="toctree-l1"><a class="reference internal" href="newtypes.html">2. Defining New Types</a><ul>
131
 
<li class="toctree-l2"><a class="reference internal" href="newtypes.html#the-basics">2.1. The Basics</a></li>
132
 
<li class="toctree-l2"><a class="reference internal" href="newtypes.html#type-methods">2.2. Type Methods</a></li>
133
 
</ul>
134
 
</li>
135
 
<li class="toctree-l1"><a class="reference internal" href="building.html">3. Building C and C++ Extensions</a><ul>
136
 
<li class="toctree-l2"><a class="reference internal" href="building.html#building-c-and-c-extensions-with-distutils">3.1. Building C and C++ Extensions with distutils</a></li>
137
 
<li class="toctree-l2"><a class="reference internal" href="building.html#distributing-your-extension-modules">3.2. Distributing your extension modules</a></li>
138
 
</ul>
139
 
</li>
140
 
<li class="toctree-l1"><a class="reference internal" href="windows.html">4. Building C and C++ Extensions on Windows</a><ul>
141
 
<li class="toctree-l2"><a class="reference internal" href="windows.html#a-cookbook-approach">4.1. A Cookbook Approach</a></li>
142
 
<li class="toctree-l2"><a class="reference internal" href="windows.html#differences-between-unix-and-windows">4.2. Differences Between Unix and Windows</a></li>
143
 
<li class="toctree-l2"><a class="reference internal" href="windows.html#using-dlls-in-practice">4.3. Using DLLs in Practice</a></li>
144
 
</ul>
145
 
</li>
146
 
</ul>
147
 
</div>
148
 
</div>
149
 
<div class="section" id="embedding-the-cpython-runtime-in-a-larger-application">
150
 
<h2>Embedding the CPython runtime in a larger application<a class="headerlink" href="#embedding-the-cpython-runtime-in-a-larger-application" title="Permalink to this headline">¶</a></h2>
151
 
<p>Sometimes, rather than creating an extension that runs inside the Python
152
 
interpreter as the main application, it is desirable to instead embed
153
 
the CPython runtime inside a larger application. This section covers
154
 
some of the details involved in doing that successfully.</p>
155
 
<div class="toctree-wrapper compound">
156
 
<ul>
157
 
<li class="toctree-l1"><a class="reference internal" href="embedding.html">1. Embedding Python in Another Application</a><ul>
158
 
<li class="toctree-l2"><a class="reference internal" href="embedding.html#very-high-level-embedding">1.1. Very High Level Embedding</a></li>
159
 
<li class="toctree-l2"><a class="reference internal" href="embedding.html#beyond-very-high-level-embedding-an-overview">1.2. Beyond Very High Level Embedding: An overview</a></li>
160
 
<li class="toctree-l2"><a class="reference internal" href="embedding.html#pure-embedding">1.3. Pure Embedding</a></li>
161
 
<li class="toctree-l2"><a class="reference internal" href="embedding.html#extending-embedded-python">1.4. Extending Embedded Python</a></li>
162
 
<li class="toctree-l2"><a class="reference internal" href="embedding.html#embedding-python-in-c">1.5. Embedding Python in C++</a></li>
163
 
<li class="toctree-l2"><a class="reference internal" href="embedding.html#compiling-and-linking-under-unix-like-systems">1.6. Compiling and Linking under Unix-like systems</a></li>
164
 
</ul>
165
 
</li>
166
 
</ul>
167
 
</div>
168
 
</div>
169
 
</div>
170
 
 
171
 
 
172
 
          </div>
173
 
        </div>
174
 
      </div>
175
 
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
176
 
        <div class="sphinxsidebarwrapper">
177
 
  <h3><a href="../contents.html">Table Of Contents</a></h3>
178
 
  <ul>
179
 
<li><a class="reference internal" href="#">Extending and Embedding the Python Interpreter</a><ul>
180
 
<li><a class="reference internal" href="#recommended-third-party-tools">Recommended third party tools</a></li>
181
 
<li><a class="reference internal" href="#creating-extensions-without-third-party-tools">Creating extensions without third party tools</a></li>
182
 
<li><a class="reference internal" href="#embedding-the-cpython-runtime-in-a-larger-application">Embedding the CPython runtime in a larger application</a></li>
183
 
</ul>
184
 
</li>
185
 
</ul>
186
 
 
187
 
  <h4>Previous topic</h4>
188
 
  <p class="topless"><a href="../library/undoc.html"
189
 
                        title="previous chapter">37. Undocumented Modules</a></p>
190
 
  <h4>Next topic</h4>
191
 
  <p class="topless"><a href="extending.html"
192
 
                        title="next chapter">1. Extending Python with C or C++</a></p>
193
 
<h3>This Page</h3>
194
 
<ul class="this-page-menu">
195
 
  <li><a href="../bugs.html">Report a Bug</a></li>
196
 
  <li><a href="../_sources/extending/index.txt"
197
 
         rel="nofollow">Show Source</a></li>
198
 
</ul>
199
 
 
200
 
<div id="searchbox" style="display: none" role="search">
201
 
  <h3>Quick search</h3>
202
 
    <form class="search" action="../search.html" method="get">
203
 
      <input type="text" name="q" />
204
 
      <input type="submit" value="Go" />
205
 
      <input type="hidden" name="check_keywords" value="yes" />
206
 
      <input type="hidden" name="area" value="default" />
207
 
    </form>
208
 
    <p class="searchtip" style="font-size: 90%">
209
 
    Enter search terms or a module, class or function name.
210
 
    </p>
211
 
</div>
212
 
<script type="text/javascript">$('#searchbox').show(0);</script>
213
 
        </div>
214
 
      </div>
215
 
      <div class="clearer"></div>
216
 
    </div>  
217
 
    <div class="related" role="navigation" aria-label="related navigation">
218
 
      <h3>Navigation</h3>
219
 
      <ul>
220
 
        <li class="right" style="margin-right: 10px">
221
 
          <a href="../genindex.html" title="General Index"
222
 
             >index</a></li>
223
 
        <li class="right" >
224
 
          <a href="../py-modindex.html" title="Python Module Index"
225
 
             >modules</a> |</li>
226
 
        <li class="right" >
227
 
          <a href="extending.html" title="1. Extending Python with C or C++"
228
 
             >next</a> |</li>
229
 
        <li class="right" >
230
 
          <a href="../library/undoc.html" title="37. Undocumented Modules"
231
 
             >previous</a> |</li>
232
 
        <li><img src="../_static/py.png" alt=""
233
 
                 style="vertical-align: middle; margin-top: -1px"/></li>
234
 
        <li><a href="https://www.python.org/">Python</a> &raquo;</li>
235
 
        <li>
236
 
          <span class="version_switcher_placeholder">3.5.1</span>
237
 
          <a href="../index.html">Documentation </a> &raquo;
238
 
        </li>
239
 
 
240
 
      </ul>
241
 
    </div>  
242
 
    <div class="footer">
243
 
    &copy; <a href="../copyright.html">Copyright</a> 1990-2016, Python Software Foundation.
244
 
    <br />
245
 
    The Python Software Foundation is a non-profit corporation.
246
 
    <a href="https://www.python.org/psf/donations/">Please donate.</a>
247
 
    <br />
248
 
    Last updated on Jan 22, 2016.
249
 
    <a href="../bugs.html">Found a bug</a>?
250
 
    <br />
251
 
    Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.3.3.
252
 
    </div>
253
 
 
254
 
  </body>
255
 
</html>
 
 
b'\\ No newline at end of file'