~dkuhlman/python-training-materials/Materials

« back to all changes in this revision

Viewing changes to python-3.5.1-docs-html/c-api/module.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>Module Objects &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="up" title="Concrete Objects Layer" href="concrete.html" />
34
 
    <link rel="next" title="Iterator Objects" href="iterator.html" />
35
 
    <link rel="prev" title="File Objects" href="file.html" />
36
 
    <link rel="shortcut icon" type="image/png" href="../_static/py.png" />
37
 
    <script type="text/javascript" src="../_static/copybutton.js"></script>
38
 
    <script type="text/javascript" src="../_static/version_switch.js"></script>
39
 
    
40
 
 
41
 
 
42
 
  </head>
43
 
  <body role="document">  
44
 
    <div class="related" role="navigation" aria-label="related navigation">
45
 
      <h3>Navigation</h3>
46
 
      <ul>
47
 
        <li class="right" style="margin-right: 10px">
48
 
          <a href="../genindex.html" title="General Index"
49
 
             accesskey="I">index</a></li>
50
 
        <li class="right" >
51
 
          <a href="../py-modindex.html" title="Python Module Index"
52
 
             >modules</a> |</li>
53
 
        <li class="right" >
54
 
          <a href="iterator.html" title="Iterator Objects"
55
 
             accesskey="N">next</a> |</li>
56
 
        <li class="right" >
57
 
          <a href="file.html" title="File Objects"
58
 
             accesskey="P">previous</a> |</li>
59
 
        <li><img src="../_static/py.png" alt=""
60
 
                 style="vertical-align: middle; margin-top: -1px"/></li>
61
 
        <li><a href="https://www.python.org/">Python</a> &raquo;</li>
62
 
        <li>
63
 
          <span class="version_switcher_placeholder">3.5.1</span>
64
 
          <a href="../index.html">Documentation </a> &raquo;
65
 
        </li>
66
 
 
67
 
          <li class="nav-item nav-item-1"><a href="index.html" >Python/C API Reference Manual</a> &raquo;</li>
68
 
          <li class="nav-item nav-item-2"><a href="concrete.html" accesskey="U">Concrete Objects Layer</a> &raquo;</li> 
69
 
      </ul>
70
 
    </div>    
71
 
 
72
 
    <div class="document">
73
 
      <div class="documentwrapper">
74
 
        <div class="bodywrapper">
75
 
          <div class="body" role="main">
76
 
            
77
 
  <div class="section" id="module-objects">
78
 
<span id="moduleobjects"></span><h1>Module Objects<a class="headerlink" href="#module-objects" title="Permalink to this headline">¶</a></h1>
79
 
<span class="target" id="index-0"></span><dl class="var">
80
 
<dt id="c.PyModule_Type">
81
 
<a class="reference internal" href="type.html#c.PyTypeObject" title="PyTypeObject">PyTypeObject</a> <code class="descname">PyModule_Type</code><a class="headerlink" href="#c.PyModule_Type" title="Permalink to this definition">¶</a></dt>
82
 
<dd><p id="index-1">This instance of <a class="reference internal" href="type.html#c.PyTypeObject" title="PyTypeObject"><code class="xref c c-type docutils literal"><span class="pre">PyTypeObject</span></code></a> represents the Python module type.  This
83
 
is exposed to Python programs as <code class="docutils literal"><span class="pre">types.ModuleType</span></code>.</p>
84
 
</dd></dl>
85
 
 
86
 
<dl class="function">
87
 
<dt id="c.PyModule_Check">
88
 
int <code class="descname">PyModule_Check</code><span class="sig-paren">(</span><a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a><em>&nbsp;*p</em><span class="sig-paren">)</span><a class="headerlink" href="#c.PyModule_Check" title="Permalink to this definition">¶</a></dt>
89
 
<dd><p>Return true if <em>p</em> is a module object, or a subtype of a module object.</p>
90
 
</dd></dl>
91
 
 
92
 
<dl class="function">
93
 
<dt id="c.PyModule_CheckExact">
94
 
int <code class="descname">PyModule_CheckExact</code><span class="sig-paren">(</span><a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a><em>&nbsp;*p</em><span class="sig-paren">)</span><a class="headerlink" href="#c.PyModule_CheckExact" title="Permalink to this definition">¶</a></dt>
95
 
<dd><p>Return true if <em>p</em> is a module object, but not a subtype of
96
 
<a class="reference internal" href="#c.PyModule_Type" title="PyModule_Type"><code class="xref c c-data docutils literal"><span class="pre">PyModule_Type</span></code></a>.</p>
97
 
</dd></dl>
98
 
 
99
 
<dl class="function">
100
 
<dt id="c.PyModule_NewObject">
101
 
<a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a>* <code class="descname">PyModule_NewObject</code><span class="sig-paren">(</span><a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a><em>&nbsp;*name</em><span class="sig-paren">)</span><a class="headerlink" href="#c.PyModule_NewObject" title="Permalink to this definition">¶</a></dt>
102
 
<dd><p id="index-2">Return a new module object with the <a class="reference internal" href="../reference/import.html#__name__" title="__name__"><code class="xref py py-attr docutils literal"><span class="pre">__name__</span></code></a> attribute set to <em>name</em>.
103
 
The module&#8217;s <a class="reference internal" href="../reference/import.html#__name__" title="__name__"><code class="xref py py-attr docutils literal"><span class="pre">__name__</span></code></a>, <code class="xref py py-attr docutils literal"><span class="pre">__doc__</span></code>, <a class="reference internal" href="../reference/import.html#__package__" title="__package__"><code class="xref py py-attr docutils literal"><span class="pre">__package__</span></code></a>, and
104
 
<a class="reference internal" href="../reference/import.html#__loader__" title="__loader__"><code class="xref py py-attr docutils literal"><span class="pre">__loader__</span></code></a> attributes are filled in (all but <a class="reference internal" href="../reference/import.html#__name__" title="__name__"><code class="xref py py-attr docutils literal"><span class="pre">__name__</span></code></a> are set
105
 
to <code class="docutils literal"><span class="pre">None</span></code>); the caller is responsible for providing a <a class="reference internal" href="../reference/import.html#__file__" title="__file__"><code class="xref py py-attr docutils literal"><span class="pre">__file__</span></code></a>
106
 
attribute.</p>
107
 
<div class="versionadded">
108
 
<p><span class="versionmodified">New in version 3.3.</span></p>
109
 
</div>
110
 
<div class="versionchanged">
111
 
<p><span class="versionmodified">Changed in version 3.4: </span><a class="reference internal" href="../reference/import.html#__package__" title="__package__"><code class="xref py py-attr docutils literal"><span class="pre">__package__</span></code></a> and <a class="reference internal" href="../reference/import.html#__loader__" title="__loader__"><code class="xref py py-attr docutils literal"><span class="pre">__loader__</span></code></a> are set to <code class="docutils literal"><span class="pre">None</span></code>.</p>
112
 
</div>
113
 
</dd></dl>
114
 
 
115
 
<dl class="function">
116
 
<dt id="c.PyModule_New">
117
 
<a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a>* <code class="descname">PyModule_New</code><span class="sig-paren">(</span>const char<em>&nbsp;*name</em><span class="sig-paren">)</span><a class="headerlink" href="#c.PyModule_New" title="Permalink to this definition">¶</a></dt>
118
 
<dd><em class="refcount">Return value: New reference.</em><p>Similar to <code class="xref c c-func docutils literal"><span class="pre">PyImport_NewObject()</span></code>, but the name is an UTF-8 encoded
119
 
string instead of a Unicode object.</p>
120
 
</dd></dl>
121
 
 
122
 
<dl class="function">
123
 
<dt id="c.PyModule_GetDict">
124
 
<a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a>* <code class="descname">PyModule_GetDict</code><span class="sig-paren">(</span><a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a><em>&nbsp;*module</em><span class="sig-paren">)</span><a class="headerlink" href="#c.PyModule_GetDict" title="Permalink to this definition">¶</a></dt>
125
 
<dd><em class="refcount">Return value: Borrowed reference.</em><p id="index-3">Return the dictionary object that implements <em>module</em>&#8216;s namespace; this object
126
 
is the same as the <code class="xref py py-attr docutils literal"><span class="pre">__dict__</span></code> attribute of the module object.  This
127
 
function never fails.  It is recommended extensions use other
128
 
<code class="xref c c-func docutils literal"><span class="pre">PyModule_*()</span></code> and <code class="xref c c-func docutils literal"><span class="pre">PyObject_*()</span></code> functions rather than directly
129
 
manipulate a module&#8217;s <code class="xref py py-attr docutils literal"><span class="pre">__dict__</span></code>.</p>
130
 
</dd></dl>
131
 
 
132
 
<dl class="function">
133
 
<dt id="c.PyModule_GetNameObject">
134
 
<a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a>* <code class="descname">PyModule_GetNameObject</code><span class="sig-paren">(</span><a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a><em>&nbsp;*module</em><span class="sig-paren">)</span><a class="headerlink" href="#c.PyModule_GetNameObject" title="Permalink to this definition">¶</a></dt>
135
 
<dd><p id="index-4">Return <em>module</em>&#8216;s <a class="reference internal" href="../reference/import.html#__name__" title="__name__"><code class="xref py py-attr docutils literal"><span class="pre">__name__</span></code></a> value.  If the module does not provide one,
136
 
or if it is not a string, <a class="reference internal" href="../library/exceptions.html#SystemError" title="SystemError"><code class="xref py py-exc docutils literal"><span class="pre">SystemError</span></code></a> is raised and <em>NULL</em> is returned.</p>
137
 
<div class="versionadded">
138
 
<p><span class="versionmodified">New in version 3.3.</span></p>
139
 
</div>
140
 
</dd></dl>
141
 
 
142
 
<dl class="function">
143
 
<dt id="c.PyModule_GetName">
144
 
char* <code class="descname">PyModule_GetName</code><span class="sig-paren">(</span><a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a><em>&nbsp;*module</em><span class="sig-paren">)</span><a class="headerlink" href="#c.PyModule_GetName" title="Permalink to this definition">¶</a></dt>
145
 
<dd><p>Similar to <a class="reference internal" href="#c.PyModule_GetNameObject" title="PyModule_GetNameObject"><code class="xref c c-func docutils literal"><span class="pre">PyModule_GetNameObject()</span></code></a> but return the name encoded to
146
 
<code class="docutils literal"><span class="pre">'utf-8'</span></code>.</p>
147
 
</dd></dl>
148
 
 
149
 
<dl class="function">
150
 
<dt id="c.PyModule_GetState">
151
 
void* <code class="descname">PyModule_GetState</code><span class="sig-paren">(</span><a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a><em>&nbsp;*module</em><span class="sig-paren">)</span><a class="headerlink" href="#c.PyModule_GetState" title="Permalink to this definition">¶</a></dt>
152
 
<dd><p>Return the &#8220;state&#8221; of the module, that is, a pointer to the block of memory
153
 
allocated at module creation time, or <em>NULL</em>.  See
154
 
<a class="reference internal" href="#c.PyModuleDef.m_size" title="PyModuleDef.m_size"><code class="xref c c-member docutils literal"><span class="pre">PyModuleDef.m_size</span></code></a>.</p>
155
 
</dd></dl>
156
 
 
157
 
<dl class="function">
158
 
<dt id="c.PyModule_GetDef">
159
 
<a class="reference internal" href="#c.PyModuleDef" title="PyModuleDef">PyModuleDef</a>* <code class="descname">PyModule_GetDef</code><span class="sig-paren">(</span><a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a><em>&nbsp;*module</em><span class="sig-paren">)</span><a class="headerlink" href="#c.PyModule_GetDef" title="Permalink to this definition">¶</a></dt>
160
 
<dd><p>Return a pointer to the <a class="reference internal" href="#c.PyModuleDef" title="PyModuleDef"><code class="xref c c-type docutils literal"><span class="pre">PyModuleDef</span></code></a> struct from which the module was
161
 
created, or <em>NULL</em> if the module wasn&#8217;t created from a definition.</p>
162
 
</dd></dl>
163
 
 
164
 
<dl class="function">
165
 
<dt id="c.PyModule_GetFilenameObject">
166
 
<a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a>* <code class="descname">PyModule_GetFilenameObject</code><span class="sig-paren">(</span><a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a><em>&nbsp;*module</em><span class="sig-paren">)</span><a class="headerlink" href="#c.PyModule_GetFilenameObject" title="Permalink to this definition">¶</a></dt>
167
 
<dd><p id="index-5">Return the name of the file from which <em>module</em> was loaded using <em>module</em>&#8216;s
168
 
<a class="reference internal" href="../reference/import.html#__file__" title="__file__"><code class="xref py py-attr docutils literal"><span class="pre">__file__</span></code></a> attribute.  If this is not defined, or if it is not a
169
 
unicode string, raise <a class="reference internal" href="../library/exceptions.html#SystemError" title="SystemError"><code class="xref py py-exc docutils literal"><span class="pre">SystemError</span></code></a> and return <em>NULL</em>; otherwise return
170
 
a reference to a Unicode object.</p>
171
 
<div class="versionadded">
172
 
<p><span class="versionmodified">New in version 3.2.</span></p>
173
 
</div>
174
 
</dd></dl>
175
 
 
176
 
<dl class="function">
177
 
<dt id="c.PyModule_GetFilename">
178
 
char* <code class="descname">PyModule_GetFilename</code><span class="sig-paren">(</span><a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a><em>&nbsp;*module</em><span class="sig-paren">)</span><a class="headerlink" href="#c.PyModule_GetFilename" title="Permalink to this definition">¶</a></dt>
179
 
<dd><p>Similar to <a class="reference internal" href="#c.PyModule_GetFilenameObject" title="PyModule_GetFilenameObject"><code class="xref c c-func docutils literal"><span class="pre">PyModule_GetFilenameObject()</span></code></a> but return the filename
180
 
encoded to &#8216;utf-8&#8217;.</p>
181
 
<div class="deprecated">
182
 
<p><span class="versionmodified">Deprecated since version 3.2: </span><a class="reference internal" href="#c.PyModule_GetFilename" title="PyModule_GetFilename"><code class="xref c c-func docutils literal"><span class="pre">PyModule_GetFilename()</span></code></a> raises <code class="xref c c-type docutils literal"><span class="pre">UnicodeEncodeError</span></code> on
183
 
unencodable filenames, use <a class="reference internal" href="#c.PyModule_GetFilenameObject" title="PyModule_GetFilenameObject"><code class="xref c c-func docutils literal"><span class="pre">PyModule_GetFilenameObject()</span></code></a> instead.</p>
184
 
</div>
185
 
</dd></dl>
186
 
 
187
 
<div class="section" id="initializing-c-modules">
188
 
<span id="initializing-modules"></span><h2>Initializing C modules<a class="headerlink" href="#initializing-c-modules" title="Permalink to this headline">¶</a></h2>
189
 
<p>Modules objects are usually created from extension modules (shared libraries
190
 
which export an initialization function), or compiled-in modules
191
 
(where the initialization function is added using <a class="reference internal" href="import.html#c.PyImport_AppendInittab" title="PyImport_AppendInittab"><code class="xref c c-func docutils literal"><span class="pre">PyImport_AppendInittab()</span></code></a>).
192
 
See <a class="reference internal" href="../extending/building.html#building"><span>Building C and C++ Extensions</span></a> or <a class="reference internal" href="../extending/embedding.html#extending-with-embedding"><span>Extending Embedded Python</span></a> for details.</p>
193
 
<p>The initialization function can either pass pass a module definition instance
194
 
to <a class="reference internal" href="#c.PyModule_Create" title="PyModule_Create"><code class="xref c c-func docutils literal"><span class="pre">PyModule_Create()</span></code></a>, and return the resulting module object,
195
 
or request &#8220;multi-phase initialization&#8221; by returning the definition struct itself.</p>
196
 
<dl class="type">
197
 
<dt id="c.PyModuleDef">
198
 
<code class="descname">PyModuleDef</code><a class="headerlink" href="#c.PyModuleDef" title="Permalink to this definition">¶</a></dt>
199
 
<dd><p>The module definition struct, which holds all information needed to create
200
 
a module object. There is usually only one statically initialized variable
201
 
of this type for each module.</p>
202
 
<dl class="member">
203
 
<dt id="c.PyModuleDef.m_base">
204
 
PyModuleDef_Base <code class="descname">m_base</code><a class="headerlink" href="#c.PyModuleDef.m_base" title="Permalink to this definition">¶</a></dt>
205
 
<dd><p>Always initialize this member to <code class="xref py py-const docutils literal"><span class="pre">PyModuleDef_HEAD_INIT</span></code>.</p>
206
 
</dd></dl>
207
 
 
208
 
<dl class="member">
209
 
<dt id="c.PyModuleDef.m_name">
210
 
char* <code class="descname">m_name</code><a class="headerlink" href="#c.PyModuleDef.m_name" title="Permalink to this definition">¶</a></dt>
211
 
<dd><p>Name for the new module.</p>
212
 
</dd></dl>
213
 
 
214
 
<dl class="member">
215
 
<dt id="c.PyModuleDef.m_doc">
216
 
char* <code class="descname">m_doc</code><a class="headerlink" href="#c.PyModuleDef.m_doc" title="Permalink to this definition">¶</a></dt>
217
 
<dd><p>Docstring for the module; usually a docstring variable created with
218
 
<code class="xref c c-func docutils literal"><span class="pre">PyDoc_STRVAR()</span></code> is used.</p>
219
 
</dd></dl>
220
 
 
221
 
<dl class="member">
222
 
<dt id="c.PyModuleDef.m_size">
223
 
Py_ssize_t <code class="descname">m_size</code><a class="headerlink" href="#c.PyModuleDef.m_size" title="Permalink to this definition">¶</a></dt>
224
 
<dd><p>Module state may be kept in a per-module memory area that can be
225
 
retrieved with <a class="reference internal" href="#c.PyModule_GetState" title="PyModule_GetState"><code class="xref c c-func docutils literal"><span class="pre">PyModule_GetState()</span></code></a>, rather than in static globals.
226
 
This makes modules safe for use in multiple sub-interpreters.</p>
227
 
<p>This memory area is allocated based on <em>m_size</em> on module creation,
228
 
and freed when the module object is deallocated, after the
229
 
<code class="xref c c-member docutils literal"><span class="pre">m_free</span></code> function has been called, if present.</p>
230
 
<p>Setting <code class="docutils literal"><span class="pre">m_size</span></code> to <code class="docutils literal"><span class="pre">-1</span></code> means that the module does not support
231
 
sub-interpreters, because it has global state.</p>
232
 
<p>Setting it to a non-negative value means that the module can be
233
 
re-initialized and specifies the additional amount of memory it requires
234
 
for its state. Non-negative <code class="docutils literal"><span class="pre">m_size</span></code> is required for multi-phase
235
 
initialization.</p>
236
 
<p>See <span class="target" id="index-6"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-3121"><strong>PEP 3121</strong></a> for more details.</p>
237
 
</dd></dl>
238
 
 
239
 
<dl class="member">
240
 
<dt id="c.PyModuleDef.m_methods">
241
 
<a class="reference internal" href="structures.html#c.PyMethodDef" title="PyMethodDef">PyMethodDef</a>* <code class="descname">m_methods</code><a class="headerlink" href="#c.PyModuleDef.m_methods" title="Permalink to this definition">¶</a></dt>
242
 
<dd><p>A pointer to a table of module-level functions, described by
243
 
<a class="reference internal" href="structures.html#c.PyMethodDef" title="PyMethodDef"><code class="xref c c-type docutils literal"><span class="pre">PyMethodDef</span></code></a> values.  Can be <em>NULL</em> if no functions are present.</p>
244
 
</dd></dl>
245
 
 
246
 
<dl class="member">
247
 
<dt id="c.PyModuleDef.m_slots">
248
 
<a class="reference internal" href="#c.PyModuleDef_Slot" title="PyModuleDef_Slot">PyModuleDef_Slot</a>* <code class="descname">m_slots</code><a class="headerlink" href="#c.PyModuleDef.m_slots" title="Permalink to this definition">¶</a></dt>
249
 
<dd><p>An array of slot definitions for multi-phase initialization, terminated by
250
 
a <code class="docutils literal"><span class="pre">{0,</span> <span class="pre">NULL}</span></code> entry.
251
 
When using single-phase initialization, <em>m_slots</em> must be <em>NULL</em>.</p>
252
 
<div class="versionchanged">
253
 
<p><span class="versionmodified">Changed in version 3.5: </span>Prior to version 3.5, this member was always set to <em>NULL</em>,
254
 
and was defined as:</p>
255
 
<blockquote>
256
 
<div><dl class="member">
257
 
<dt id="c.PyModuleDef.m_reload">
258
 
<a class="reference internal" href="gcsupport.html#c.inquiry" title="inquiry">inquiry</a> <code class="descname">m_reload</code><a class="headerlink" href="#c.PyModuleDef.m_reload" title="Permalink to this definition">¶</a></dt>
259
 
<dd></dd></dl>
260
 
 
261
 
</div></blockquote>
262
 
</div>
263
 
</dd></dl>
264
 
 
265
 
<dl class="member">
266
 
<dt id="c.PyModuleDef.m_traverse">
267
 
<a class="reference internal" href="gcsupport.html#c.traverseproc" title="traverseproc">traverseproc</a> <code class="descname">m_traverse</code><a class="headerlink" href="#c.PyModuleDef.m_traverse" title="Permalink to this definition">¶</a></dt>
268
 
<dd><p>A traversal function to call during GC traversal of the module object, or
269
 
<em>NULL</em> if not needed.</p>
270
 
</dd></dl>
271
 
 
272
 
<dl class="member">
273
 
<dt id="c.PyModuleDef.m_clear">
274
 
<a class="reference internal" href="gcsupport.html#c.inquiry" title="inquiry">inquiry</a> <code class="descname">m_clear</code><a class="headerlink" href="#c.PyModuleDef.m_clear" title="Permalink to this definition">¶</a></dt>
275
 
<dd><p>A clear function to call during GC clearing of the module object, or
276
 
<em>NULL</em> if not needed.</p>
277
 
</dd></dl>
278
 
 
279
 
<dl class="member">
280
 
<dt id="c.PyModuleDef.m_free">
281
 
freefunc <code class="descname">m_free</code><a class="headerlink" href="#c.PyModuleDef.m_free" title="Permalink to this definition">¶</a></dt>
282
 
<dd><p>A function to call during deallocation of the module object, or <em>NULL</em> if
283
 
not needed.</p>
284
 
</dd></dl>
285
 
 
286
 
</dd></dl>
287
 
 
288
 
<div class="section" id="single-phase-initialization">
289
 
<h3>Single-phase initialization<a class="headerlink" href="#single-phase-initialization" title="Permalink to this headline">¶</a></h3>
290
 
<p>The module initialization function may create and return the module object
291
 
directly. This is referred to as &#8220;single-phase initialization&#8221;, and uses one
292
 
of the following two module creation functions:</p>
293
 
<dl class="function">
294
 
<dt id="c.PyModule_Create">
295
 
<a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a>* <code class="descname">PyModule_Create</code><span class="sig-paren">(</span><a class="reference internal" href="#c.PyModuleDef" title="PyModuleDef">PyModuleDef</a><em>&nbsp;*def</em><span class="sig-paren">)</span><a class="headerlink" href="#c.PyModule_Create" title="Permalink to this definition">¶</a></dt>
296
 
<dd><p>Create a new module object, given the definition in <em>def</em>.  This behaves
297
 
like <a class="reference internal" href="#c.PyModule_Create2" title="PyModule_Create2"><code class="xref c c-func docutils literal"><span class="pre">PyModule_Create2()</span></code></a> with <em>module_api_version</em> set to
298
 
<code class="xref py py-const docutils literal"><span class="pre">PYTHON_API_VERSION</span></code>.</p>
299
 
</dd></dl>
300
 
 
301
 
<dl class="function">
302
 
<dt id="c.PyModule_Create2">
303
 
<a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a>* <code class="descname">PyModule_Create2</code><span class="sig-paren">(</span><a class="reference internal" href="#c.PyModuleDef" title="PyModuleDef">PyModuleDef</a><em>&nbsp;*def</em>, int<em>&nbsp;module_api_version</em><span class="sig-paren">)</span><a class="headerlink" href="#c.PyModule_Create2" title="Permalink to this definition">¶</a></dt>
304
 
<dd><p>Create a new module object, given the definition in <em>def</em>, assuming the
305
 
API version <em>module_api_version</em>.  If that version does not match the version
306
 
of the running interpreter, a <a class="reference internal" href="../library/exceptions.html#RuntimeWarning" title="RuntimeWarning"><code class="xref py py-exc docutils literal"><span class="pre">RuntimeWarning</span></code></a> is emitted.</p>
307
 
<div class="admonition note">
308
 
<p class="first admonition-title">Note</p>
309
 
<p class="last">Most uses of this function should be using <a class="reference internal" href="#c.PyModule_Create" title="PyModule_Create"><code class="xref c c-func docutils literal"><span class="pre">PyModule_Create()</span></code></a>
310
 
instead; only use this if you are sure you need it.</p>
311
 
</div>
312
 
</dd></dl>
313
 
 
314
 
<p>Before it is returned from in the initialization function, the resulting module
315
 
object is typically populated using functions like <a class="reference internal" href="#c.PyModule_AddObject" title="PyModule_AddObject"><code class="xref c c-func docutils literal"><span class="pre">PyModule_AddObject()</span></code></a>.</p>
316
 
</div>
317
 
<div class="section" id="multi-phase-initialization">
318
 
<span id="id1"></span><h3>Multi-phase initialization<a class="headerlink" href="#multi-phase-initialization" title="Permalink to this headline">¶</a></h3>
319
 
<p>An alternate way to specify extensions is to request &#8220;multi-phase initialization&#8221;.
320
 
Extension modules created this way behave more like Python modules: the
321
 
initialization is split between the <em>creation phase</em>, when the module object
322
 
is created, and the <em>execution phase</em>, when it is populated.
323
 
The distinction is similar to the <a class="reference internal" href="../reference/datamodel.html#object.__new__" title="object.__new__"><code class="xref py py-meth docutils literal"><span class="pre">__new__()</span></code></a> and <a class="reference internal" href="../reference/datamodel.html#object.__init__" title="object.__init__"><code class="xref py py-meth docutils literal"><span class="pre">__init__()</span></code></a> methods
324
 
of classes.</p>
325
 
<p>Unlike modules created using single-phase initialization, these modules are not
326
 
singletons: if the <em>sys.modules</em> entry is removed and the module is re-imported,
327
 
a new module object is created, and the old module is subject to normal garbage
328
 
collection &#8211; as with Python modules.
329
 
By default, multiple modules created from the same definition should be
330
 
independent: changes to one should not affect the others.
331
 
This means that all state should be specific to the module object (using e.g.
332
 
using <a class="reference internal" href="#c.PyModule_GetState" title="PyModule_GetState"><code class="xref c c-func docutils literal"><span class="pre">PyModule_GetState()</span></code></a>), or its contents (such as the module&#8217;s
333
 
<code class="xref py py-attr docutils literal"><span class="pre">__dict__</span></code> or individual classes created with <a class="reference internal" href="type.html#c.PyType_FromSpec" title="PyType_FromSpec"><code class="xref c c-func docutils literal"><span class="pre">PyType_FromSpec()</span></code></a>).</p>
334
 
<p>All modules created using multi-phase initialization are expected to support
335
 
<a class="reference internal" href="init.html#sub-interpreter-support"><span>sub-interpreters</span></a>. Making sure multiple modules
336
 
are independent is typically enough to achieve this.</p>
337
 
<p>To request multi-phase initialization, the initialization function
338
 
(PyInit_modulename) returns a <a class="reference internal" href="#c.PyModuleDef" title="PyModuleDef"><code class="xref c c-type docutils literal"><span class="pre">PyModuleDef</span></code></a> instance with non-empty
339
 
<a class="reference internal" href="#c.PyModuleDef.m_slots" title="PyModuleDef.m_slots"><code class="xref c c-member docutils literal"><span class="pre">m_slots</span></code></a>. Before it is returned, the <code class="docutils literal"><span class="pre">PyModuleDef</span></code>
340
 
instance must be initialized with the following function:</p>
341
 
<dl class="function">
342
 
<dt id="c.PyModuleDef_Init">
343
 
<a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a>* <code class="descname">PyModuleDef_Init</code><span class="sig-paren">(</span><a class="reference internal" href="#c.PyModuleDef" title="PyModuleDef">PyModuleDef</a><em>&nbsp;*def</em><span class="sig-paren">)</span><a class="headerlink" href="#c.PyModuleDef_Init" title="Permalink to this definition">¶</a></dt>
344
 
<dd><p>Ensures a module definition is a properly initialized Python object that
345
 
correctly reports its type and reference count.</p>
346
 
<p>Returns <em>def</em> cast to <code class="docutils literal"><span class="pre">PyObject*</span></code>, or <em>NULL</em> if an error occurred.</p>
347
 
<div class="versionadded">
348
 
<p><span class="versionmodified">New in version 3.5.</span></p>
349
 
</div>
350
 
</dd></dl>
351
 
 
352
 
<p>The <em>m_slots</em> member of the module definition must point to an array of
353
 
<code class="docutils literal"><span class="pre">PyModuleDef_Slot</span></code> structures:</p>
354
 
<dl class="type">
355
 
<dt id="c.PyModuleDef_Slot">
356
 
<code class="descname">PyModuleDef_Slot</code><a class="headerlink" href="#c.PyModuleDef_Slot" title="Permalink to this definition">¶</a></dt>
357
 
<dd><dl class="member">
358
 
<dt id="c.PyModuleDef_Slot.slot">
359
 
int <code class="descname">slot</code><a class="headerlink" href="#c.PyModuleDef_Slot.slot" title="Permalink to this definition">¶</a></dt>
360
 
<dd><p>A slot ID, chosen from the available values explained below.</p>
361
 
</dd></dl>
362
 
 
363
 
<dl class="member">
364
 
<dt id="c.PyModuleDef_Slot.value">
365
 
void* <code class="descname">value</code><a class="headerlink" href="#c.PyModuleDef_Slot.value" title="Permalink to this definition">¶</a></dt>
366
 
<dd><p>Value of the slot, whose meaning depends on the slot ID.</p>
367
 
</dd></dl>
368
 
 
369
 
<div class="versionadded">
370
 
<p><span class="versionmodified">New in version 3.5.</span></p>
371
 
</div>
372
 
</dd></dl>
373
 
 
374
 
<p>The <em>m_slots</em> array must be terminated by a slot with id 0.</p>
375
 
<p>The available slot types are:</p>
376
 
<dl class="var">
377
 
<dt id="c.Py_mod_create">
378
 
<code class="descname">Py_mod_create</code><a class="headerlink" href="#c.Py_mod_create" title="Permalink to this definition">¶</a></dt>
379
 
<dd><p>Specifies a function that is called to create the module object itself.
380
 
The <em>value</em> pointer of this slot must point to a function of the signature:</p>
381
 
<dl class="function">
382
 
<dt id="c.create_module">
383
 
<a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a>* <code class="descname">create_module</code><span class="sig-paren">(</span><a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a><em>&nbsp;*spec</em>, <a class="reference internal" href="#c.PyModuleDef" title="PyModuleDef">PyModuleDef</a><em>&nbsp;*def</em><span class="sig-paren">)</span><a class="headerlink" href="#c.create_module" title="Permalink to this definition">¶</a></dt>
384
 
<dd></dd></dl>
385
 
 
386
 
<p>The function receives a <a class="reference internal" href="../library/importlib.html#importlib.machinery.ModuleSpec" title="importlib.machinery.ModuleSpec"><code class="xref py py-class docutils literal"><span class="pre">ModuleSpec</span></code></a>
387
 
instance, as defined in <span class="target" id="index-7"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0451"><strong>PEP 451</strong></a>, and the module definition.
388
 
It should return a new module object, or set an error
389
 
and return <em>NULL</em>.</p>
390
 
<p>This function should be kept minimal. In particular, it should not
391
 
call arbitrary Python code, as trying to import the same module again may
392
 
result in an infinite loop.</p>
393
 
<p>Multiple <code class="docutils literal"><span class="pre">Py_mod_create</span></code> slots may not be specified in one module
394
 
definition.</p>
395
 
<p>If <code class="docutils literal"><span class="pre">Py_mod_create</span></code> is not specified, the import machinery will create
396
 
a normal module object using <a class="reference internal" href="#c.PyModule_New" title="PyModule_New"><code class="xref c c-func docutils literal"><span class="pre">PyModule_New()</span></code></a>. The name is taken from
397
 
<em>spec</em>, not the definition, to allow extension modules to dynamically adjust
398
 
to their place in the module hierarchy and be imported under different
399
 
names through symlinks, all while sharing a single module definition.</p>
400
 
<p>There is no requirement for the returned object to be an instance of
401
 
<a class="reference internal" href="#c.PyModule_Type" title="PyModule_Type"><code class="xref c c-type docutils literal"><span class="pre">PyModule_Type</span></code></a>. Any type can be used, as long as it supports
402
 
setting and getting import-related attributes.
403
 
However, only <code class="docutils literal"><span class="pre">PyModule_Type</span></code> instances may be returned if the
404
 
<code class="docutils literal"><span class="pre">PyModuleDef</span></code> has non-<em>NULL</em> <code class="docutils literal"><span class="pre">m_methods</span></code>, <code class="docutils literal"><span class="pre">m_traverse</span></code>, <code class="docutils literal"><span class="pre">m_clear</span></code>,
405
 
<code class="docutils literal"><span class="pre">m_free</span></code>; non-zero <code class="docutils literal"><span class="pre">m_size</span></code>; or slots other than <code class="docutils literal"><span class="pre">Py_mod_create</span></code>.</p>
406
 
</dd></dl>
407
 
 
408
 
<dl class="var">
409
 
<dt id="c.Py_mod_exec">
410
 
<code class="descname">Py_mod_exec</code><a class="headerlink" href="#c.Py_mod_exec" title="Permalink to this definition">¶</a></dt>
411
 
<dd><p>Specifies a function that is called to <em>execute</em> the module.
412
 
This is equivalent to executing the code of a Python module: typically,
413
 
this function adds classes and constants to the module.
414
 
The signature of the function is:</p>
415
 
<dl class="function">
416
 
<dt id="c.exec_module">
417
 
int <code class="descname">exec_module</code><span class="sig-paren">(</span><a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a>*<em>&nbsp;module</em><span class="sig-paren">)</span><a class="headerlink" href="#c.exec_module" title="Permalink to this definition">¶</a></dt>
418
 
<dd></dd></dl>
419
 
 
420
 
<p>If multiple <code class="docutils literal"><span class="pre">Py_mod_exec</span></code> slots are specified, they are processed in the
421
 
order they appear in the <em>m_slots</em> array.</p>
422
 
</dd></dl>
423
 
 
424
 
<p>See <span class="target" id="index-8"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0489"><strong>PEP 489</strong></a> for more details on multi-phase initialization.</p>
425
 
</div>
426
 
<div class="section" id="low-level-module-creation-functions">
427
 
<h3>Low-level module creation functions<a class="headerlink" href="#low-level-module-creation-functions" title="Permalink to this headline">¶</a></h3>
428
 
<p>The following functions are called under the hood when using multi-phase
429
 
initialization. They can be used directly, for example when creating module
430
 
objects dynamically. Note that both <code class="docutils literal"><span class="pre">PyModule_FromDefAndSpec</span></code> and
431
 
<code class="docutils literal"><span class="pre">PyModule_ExecDef</span></code> must be called to fully initialize a module.</p>
432
 
<dl class="function">
433
 
<dt id="c.PyModule_FromDefAndSpec">
434
 
<a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a> * <code class="descname">PyModule_FromDefAndSpec</code><span class="sig-paren">(</span><a class="reference internal" href="#c.PyModuleDef" title="PyModuleDef">PyModuleDef</a><em>&nbsp;*def</em>, <a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a><em>&nbsp;*spec</em><span class="sig-paren">)</span><a class="headerlink" href="#c.PyModule_FromDefAndSpec" title="Permalink to this definition">¶</a></dt>
435
 
<dd><p>Create a new module object, given the definition in <em>module</em> and the
436
 
ModuleSpec <em>spec</em>.  This behaves like <a class="reference internal" href="#c.PyModule_FromDefAndSpec2" title="PyModule_FromDefAndSpec2"><code class="xref c c-func docutils literal"><span class="pre">PyModule_FromDefAndSpec2()</span></code></a>
437
 
with <em>module_api_version</em> set to <code class="xref py py-const docutils literal"><span class="pre">PYTHON_API_VERSION</span></code>.</p>
438
 
<div class="versionadded">
439
 
<p><span class="versionmodified">New in version 3.5.</span></p>
440
 
</div>
441
 
</dd></dl>
442
 
 
443
 
<dl class="function">
444
 
<dt id="c.PyModule_FromDefAndSpec2">
445
 
<a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a> * <code class="descname">PyModule_FromDefAndSpec2</code><span class="sig-paren">(</span><a class="reference internal" href="#c.PyModuleDef" title="PyModuleDef">PyModuleDef</a><em>&nbsp;*def</em>, <a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a><em>&nbsp;*spec</em>, int<em>&nbsp;module_api_version</em><span class="sig-paren">)</span><a class="headerlink" href="#c.PyModule_FromDefAndSpec2" title="Permalink to this definition">¶</a></dt>
446
 
<dd><p>Create a new module object, given the definition in <em>module</em> and the
447
 
ModuleSpec <em>spec</em>, assuming the API version <em>module_api_version</em>.
448
 
If that version does not match the version of the running interpreter,
449
 
a <a class="reference internal" href="../library/exceptions.html#RuntimeWarning" title="RuntimeWarning"><code class="xref py py-exc docutils literal"><span class="pre">RuntimeWarning</span></code></a> is emitted.</p>
450
 
<div class="admonition note">
451
 
<p class="first admonition-title">Note</p>
452
 
<p class="last">Most uses of this function should be using <a class="reference internal" href="#c.PyModule_FromDefAndSpec" title="PyModule_FromDefAndSpec"><code class="xref c c-func docutils literal"><span class="pre">PyModule_FromDefAndSpec()</span></code></a>
453
 
instead; only use this if you are sure you need it.</p>
454
 
</div>
455
 
<div class="versionadded">
456
 
<p><span class="versionmodified">New in version 3.5.</span></p>
457
 
</div>
458
 
</dd></dl>
459
 
 
460
 
<dl class="function">
461
 
<dt id="c.PyModule_ExecDef">
462
 
int <code class="descname">PyModule_ExecDef</code><span class="sig-paren">(</span><a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a><em>&nbsp;*module</em>, <a class="reference internal" href="#c.PyModuleDef" title="PyModuleDef">PyModuleDef</a><em>&nbsp;*def</em><span class="sig-paren">)</span><a class="headerlink" href="#c.PyModule_ExecDef" title="Permalink to this definition">¶</a></dt>
463
 
<dd><p>Process any execution slots (<a class="reference internal" href="#c.Py_mod_exec" title="Py_mod_exec"><code class="xref c c-data docutils literal"><span class="pre">Py_mod_exec</span></code></a>) given in <em>def</em>.</p>
464
 
<div class="versionadded">
465
 
<p><span class="versionmodified">New in version 3.5.</span></p>
466
 
</div>
467
 
</dd></dl>
468
 
 
469
 
<dl class="function">
470
 
<dt id="c.PyModule_SetDocString">
471
 
int <code class="descname">PyModule_SetDocString</code><span class="sig-paren">(</span><a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a><em>&nbsp;*module</em>, const char<em>&nbsp;*docstring</em><span class="sig-paren">)</span><a class="headerlink" href="#c.PyModule_SetDocString" title="Permalink to this definition">¶</a></dt>
472
 
<dd><p>Set the docstring for <em>module</em> to <em>docstring</em>.
473
 
This function is called automatically when creating a module from
474
 
<code class="docutils literal"><span class="pre">PyModuleDef</span></code>, using either <code class="docutils literal"><span class="pre">PyModule_Create</span></code> or
475
 
<code class="docutils literal"><span class="pre">PyModule_FromDefAndSpec</span></code>.</p>
476
 
<div class="versionadded">
477
 
<p><span class="versionmodified">New in version 3.5.</span></p>
478
 
</div>
479
 
</dd></dl>
480
 
 
481
 
<dl class="function">
482
 
<dt id="c.PyModule_AddFunctions">
483
 
int <code class="descname">PyModule_AddFunctions</code><span class="sig-paren">(</span><a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a><em>&nbsp;*module</em>, <a class="reference internal" href="structures.html#c.PyMethodDef" title="PyMethodDef">PyMethodDef</a><em>&nbsp;*functions</em><span class="sig-paren">)</span><a class="headerlink" href="#c.PyModule_AddFunctions" title="Permalink to this definition">¶</a></dt>
484
 
<dd><p>Add the functions from the <em>NULL</em> terminated <em>functions</em> array to <em>module</em>.
485
 
Refer to the <a class="reference internal" href="structures.html#c.PyMethodDef" title="PyMethodDef"><code class="xref c c-type docutils literal"><span class="pre">PyMethodDef</span></code></a> documentation for details on individual
486
 
entries (due to the lack of a shared module namespace, module level
487
 
&#8220;functions&#8221; implemented in C typically receive the module as their first
488
 
parameter, making them similar to instance methods on Python classes).
489
 
This function is called automatically when creating a module from
490
 
<code class="docutils literal"><span class="pre">PyModuleDef</span></code>, using either <code class="docutils literal"><span class="pre">PyModule_Create</span></code> or
491
 
<code class="docutils literal"><span class="pre">PyModule_FromDefAndSpec</span></code>.</p>
492
 
<div class="versionadded">
493
 
<p><span class="versionmodified">New in version 3.5.</span></p>
494
 
</div>
495
 
</dd></dl>
496
 
 
497
 
</div>
498
 
<div class="section" id="support-functions">
499
 
<h3>Support functions<a class="headerlink" href="#support-functions" title="Permalink to this headline">¶</a></h3>
500
 
<p>The module initialization function (if using single phase initialization) or
501
 
a function called from a module execution slot (if using multi-phase
502
 
initialization), can use the following functions to help initialize the module
503
 
state:</p>
504
 
<dl class="function">
505
 
<dt id="c.PyModule_AddObject">
506
 
int <code class="descname">PyModule_AddObject</code><span class="sig-paren">(</span><a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a><em>&nbsp;*module</em>, const char<em>&nbsp;*name</em>, <a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a><em>&nbsp;*value</em><span class="sig-paren">)</span><a class="headerlink" href="#c.PyModule_AddObject" title="Permalink to this definition">¶</a></dt>
507
 
<dd><p>Add an object to <em>module</em> as <em>name</em>.  This is a convenience function which can
508
 
be used from the module&#8217;s initialization function.  This steals a reference to
509
 
<em>value</em>.  Return <code class="docutils literal"><span class="pre">-1</span></code> on error, <code class="docutils literal"><span class="pre">0</span></code> on success.</p>
510
 
</dd></dl>
511
 
 
512
 
<dl class="function">
513
 
<dt id="c.PyModule_AddIntConstant">
514
 
int <code class="descname">PyModule_AddIntConstant</code><span class="sig-paren">(</span><a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a><em>&nbsp;*module</em>, const char<em>&nbsp;*name</em>, long<em>&nbsp;value</em><span class="sig-paren">)</span><a class="headerlink" href="#c.PyModule_AddIntConstant" title="Permalink to this definition">¶</a></dt>
515
 
<dd><p>Add an integer constant to <em>module</em> as <em>name</em>.  This convenience function can be
516
 
used from the module&#8217;s initialization function. Return <code class="docutils literal"><span class="pre">-1</span></code> on error, <code class="docutils literal"><span class="pre">0</span></code> on
517
 
success.</p>
518
 
</dd></dl>
519
 
 
520
 
<dl class="function">
521
 
<dt id="c.PyModule_AddStringConstant">
522
 
int <code class="descname">PyModule_AddStringConstant</code><span class="sig-paren">(</span><a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a><em>&nbsp;*module</em>, const char<em>&nbsp;*name</em>, const char<em>&nbsp;*value</em><span class="sig-paren">)</span><a class="headerlink" href="#c.PyModule_AddStringConstant" title="Permalink to this definition">¶</a></dt>
523
 
<dd><p>Add a string constant to <em>module</em> as <em>name</em>.  This convenience function can be
524
 
used from the module&#8217;s initialization function.  The string <em>value</em> must be
525
 
<em>NULL</em>-terminated.  Return <code class="docutils literal"><span class="pre">-1</span></code> on error, <code class="docutils literal"><span class="pre">0</span></code> on success.</p>
526
 
</dd></dl>
527
 
 
528
 
<dl class="function">
529
 
<dt id="c.PyModule_AddIntMacro">
530
 
int <code class="descname">PyModule_AddIntMacro</code><span class="sig-paren">(</span><a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a><em>&nbsp;*module</em>, macro<span class="sig-paren">)</span><a class="headerlink" href="#c.PyModule_AddIntMacro" title="Permalink to this definition">¶</a></dt>
531
 
<dd><p>Add an int constant to <em>module</em>. The name and the value are taken from
532
 
<em>macro</em>. For example <code class="docutils literal"><span class="pre">PyModule_AddIntMacro(module,</span> <span class="pre">AF_INET)</span></code> adds the int
533
 
constant <em>AF_INET</em> with the value of <em>AF_INET</em> to <em>module</em>.
534
 
Return <code class="docutils literal"><span class="pre">-1</span></code> on error, <code class="docutils literal"><span class="pre">0</span></code> on success.</p>
535
 
</dd></dl>
536
 
 
537
 
<dl class="function">
538
 
<dt id="c.PyModule_AddStringMacro">
539
 
int <code class="descname">PyModule_AddStringMacro</code><span class="sig-paren">(</span><a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a><em>&nbsp;*module</em>, macro<span class="sig-paren">)</span><a class="headerlink" href="#c.PyModule_AddStringMacro" title="Permalink to this definition">¶</a></dt>
540
 
<dd><p>Add a string constant to <em>module</em>.</p>
541
 
</dd></dl>
542
 
 
543
 
</div>
544
 
</div>
545
 
<div class="section" id="module-lookup">
546
 
<h2>Module lookup<a class="headerlink" href="#module-lookup" title="Permalink to this headline">¶</a></h2>
547
 
<p>Single-phase initialization creates singleton modules that can be looked up
548
 
in the context of the current interpreter. This allows the module object to be
549
 
retrieved later with only a reference to the module definition.</p>
550
 
<p>These functions will not work on modules created using multi-phase initialization,
551
 
since multiple such modules can be created from a single definition.</p>
552
 
<dl class="function">
553
 
<dt id="c.PyState_FindModule">
554
 
<a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a>* <code class="descname">PyState_FindModule</code><span class="sig-paren">(</span><a class="reference internal" href="#c.PyModuleDef" title="PyModuleDef">PyModuleDef</a><em>&nbsp;*def</em><span class="sig-paren">)</span><a class="headerlink" href="#c.PyState_FindModule" title="Permalink to this definition">¶</a></dt>
555
 
<dd><p>Returns the module object that was created from <em>def</em> for the current interpreter.
556
 
This method requires that the module object has been attached to the interpreter state with
557
 
<a class="reference internal" href="#c.PyState_AddModule" title="PyState_AddModule"><code class="xref c c-func docutils literal"><span class="pre">PyState_AddModule()</span></code></a> beforehand. In case the corresponding module object is not
558
 
found or has not been attached to the interpreter state yet, it returns <em>NULL</em>.</p>
559
 
</dd></dl>
560
 
 
561
 
<dl class="function">
562
 
<dt id="c.PyState_AddModule">
563
 
int <code class="descname">PyState_AddModule</code><span class="sig-paren">(</span><a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a><em>&nbsp;*module</em>, <a class="reference internal" href="#c.PyModuleDef" title="PyModuleDef">PyModuleDef</a><em>&nbsp;*def</em><span class="sig-paren">)</span><a class="headerlink" href="#c.PyState_AddModule" title="Permalink to this definition">¶</a></dt>
564
 
<dd><p>Attaches the module object passed to the function to the interpreter state. This allows
565
 
the module object to be accessible via <a class="reference internal" href="#c.PyState_FindModule" title="PyState_FindModule"><code class="xref c c-func docutils literal"><span class="pre">PyState_FindModule()</span></code></a>.</p>
566
 
<p>Only effective on modules created using single-phase initialization.</p>
567
 
<div class="versionadded">
568
 
<p><span class="versionmodified">New in version 3.3.</span></p>
569
 
</div>
570
 
</dd></dl>
571
 
 
572
 
<dl class="function">
573
 
<dt id="c.PyState_RemoveModule">
574
 
int <code class="descname">PyState_RemoveModule</code><span class="sig-paren">(</span><a class="reference internal" href="#c.PyModuleDef" title="PyModuleDef">PyModuleDef</a><em>&nbsp;*def</em><span class="sig-paren">)</span><a class="headerlink" href="#c.PyState_RemoveModule" title="Permalink to this definition">¶</a></dt>
575
 
<dd><p>Removes the module object created from <em>def</em> from the interpreter state.</p>
576
 
<div class="versionadded">
577
 
<p><span class="versionmodified">New in version 3.3.</span></p>
578
 
</div>
579
 
</dd></dl>
580
 
 
581
 
</div>
582
 
</div>
583
 
 
584
 
 
585
 
          </div>
586
 
        </div>
587
 
      </div>
588
 
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
589
 
        <div class="sphinxsidebarwrapper">
590
 
  <h3><a href="../contents.html">Table Of Contents</a></h3>
591
 
  <ul>
592
 
<li><a class="reference internal" href="#">Module Objects</a><ul>
593
 
<li><a class="reference internal" href="#initializing-c-modules">Initializing C modules</a><ul>
594
 
<li><a class="reference internal" href="#single-phase-initialization">Single-phase initialization</a></li>
595
 
<li><a class="reference internal" href="#multi-phase-initialization">Multi-phase initialization</a></li>
596
 
<li><a class="reference internal" href="#low-level-module-creation-functions">Low-level module creation functions</a></li>
597
 
<li><a class="reference internal" href="#support-functions">Support functions</a></li>
598
 
</ul>
599
 
</li>
600
 
<li><a class="reference internal" href="#module-lookup">Module lookup</a></li>
601
 
</ul>
602
 
</li>
603
 
</ul>
604
 
 
605
 
  <h4>Previous topic</h4>
606
 
  <p class="topless"><a href="file.html"
607
 
                        title="previous chapter">File Objects</a></p>
608
 
  <h4>Next topic</h4>
609
 
  <p class="topless"><a href="iterator.html"
610
 
                        title="next chapter">Iterator Objects</a></p>
611
 
<h3>This Page</h3>
612
 
<ul class="this-page-menu">
613
 
  <li><a href="../bugs.html">Report a Bug</a></li>
614
 
  <li><a href="../_sources/c-api/module.txt"
615
 
         rel="nofollow">Show Source</a></li>
616
 
</ul>
617
 
 
618
 
<div id="searchbox" style="display: none" role="search">
619
 
  <h3>Quick search</h3>
620
 
    <form class="search" action="../search.html" method="get">
621
 
      <input type="text" name="q" />
622
 
      <input type="submit" value="Go" />
623
 
      <input type="hidden" name="check_keywords" value="yes" />
624
 
      <input type="hidden" name="area" value="default" />
625
 
    </form>
626
 
    <p class="searchtip" style="font-size: 90%">
627
 
    Enter search terms or a module, class or function name.
628
 
    </p>
629
 
</div>
630
 
<script type="text/javascript">$('#searchbox').show(0);</script>
631
 
        </div>
632
 
      </div>
633
 
      <div class="clearer"></div>
634
 
    </div>  
635
 
    <div class="related" role="navigation" aria-label="related navigation">
636
 
      <h3>Navigation</h3>
637
 
      <ul>
638
 
        <li class="right" style="margin-right: 10px">
639
 
          <a href="../genindex.html" title="General Index"
640
 
             >index</a></li>
641
 
        <li class="right" >
642
 
          <a href="../py-modindex.html" title="Python Module Index"
643
 
             >modules</a> |</li>
644
 
        <li class="right" >
645
 
          <a href="iterator.html" title="Iterator Objects"
646
 
             >next</a> |</li>
647
 
        <li class="right" >
648
 
          <a href="file.html" title="File Objects"
649
 
             >previous</a> |</li>
650
 
        <li><img src="../_static/py.png" alt=""
651
 
                 style="vertical-align: middle; margin-top: -1px"/></li>
652
 
        <li><a href="https://www.python.org/">Python</a> &raquo;</li>
653
 
        <li>
654
 
          <span class="version_switcher_placeholder">3.5.1</span>
655
 
          <a href="../index.html">Documentation </a> &raquo;
656
 
        </li>
657
 
 
658
 
          <li class="nav-item nav-item-1"><a href="index.html" >Python/C API Reference Manual</a> &raquo;</li>
659
 
          <li class="nav-item nav-item-2"><a href="concrete.html" >Concrete Objects Layer</a> &raquo;</li> 
660
 
      </ul>
661
 
    </div>  
662
 
    <div class="footer">
663
 
    &copy; <a href="../copyright.html">Copyright</a> 1990-2016, Python Software Foundation.
664
 
    <br />
665
 
    The Python Software Foundation is a non-profit corporation.
666
 
    <a href="https://www.python.org/psf/donations/">Please donate.</a>
667
 
    <br />
668
 
    Last updated on Jan 22, 2016.
669
 
    <a href="../bugs.html">Found a bug</a>?
670
 
    <br />
671
 
    Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.3.3.
672
 
    </div>
673
 
 
674
 
  </body>
675
 
</html>
 
 
b'\\ No newline at end of file'