Files
hustoj/web/python-docs/c-api/module.html
2025-03-09 17:10:56 +08:00

800 lines
63 KiB
HTML
Raw Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" lang="zh_CN">
<head>
<meta charset="utf-8" /><meta name="generator" content="Docutils 0.17.1: http://docutils.sourceforge.net/" />
<title>模块对象 &#8212; Python 3.8.20 文档</title><meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="../_static/pydoctheme.css" type="text/css" />
<link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
<script id="documentation_options" data-url_root="../" src="../_static/documentation_options.js"></script>
<script src="../_static/jquery.js"></script>
<script src="../_static/underscore.js"></script>
<script src="../_static/doctools.js"></script>
<script src="../_static/language_data.js"></script>
<script src="../_static/translations.js"></script>
<script src="../_static/sidebar.js"></script>
<link rel="search" type="application/opensearchdescription+xml"
title="在 Python 3.8.20 文档 中搜索"
href="../_static/opensearch.xml"/>
<link rel="author" title="关于这些文档" href="../about.html" />
<link rel="index" title="索引" href="../genindex.html" />
<link rel="search" title="搜索" href="../search.html" />
<link rel="copyright" title="版权所有" href="../copyright.html" />
<link rel="next" title="迭代器对象" href="iterator.html" />
<link rel="prev" title="文件对象" href="file.html" />
<link rel="canonical" href="https://docs.python.org/3/c-api/module.html" />
<style>
@media only screen {
table.full-width-table {
width: 100%;
}
}
</style>
<link rel="shortcut icon" type="image/png" href="../_static/py.svg" />
<script type="text/javascript" src="../_static/copybutton.js"></script>
<script type="text/javascript" src="../_static/menu.js"></script>
</head>
<body>
<div class="mobile-nav">
<input type="checkbox" id="menuToggler" class="toggler__input" aria-controls="navigation"
aria-pressed="false" aria-expanded="false" role="button" aria-label="Menu" />
<label for="menuToggler" class="toggler__label">
<span></span>
</label>
<nav class="nav-content" role="navigation">
<a href="https://www.python.org/" class="nav-logo">
<img src="../_static/py.svg" alt="Logo"/>
</a>
<div class="version_switcher_placeholder"></div>
<form role="search" class="search" action="../search.html" method="get">
<svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" class="search-icon">
<path fill-rule="nonzero"
d="M15.5 14h-.79l-.28-.27a6.5 6.5 0 001.48-5.34c-.47-2.78-2.79-5-5.59-5.34a6.505 6.505 0 00-7.27 7.27c.34 2.8 2.56 5.12 5.34 5.59a6.5 6.5 0 005.34-1.48l.27.28v.79l4.25 4.25c.41.41 1.08.41 1.49 0 .41-.41.41-1.08 0-1.49L15.5 14zm-6 0C7.01 14 5 11.99 5 9.5S7.01 5 9.5 5 14 7.01 14 9.5 11.99 14 9.5 14z" fill="#444"></path>
</svg>
<input type="text" name="q" aria-label="快速搜索"/>
<input type="submit" value="转向"/>
</form>
</nav>
<div class="menu-wrapper">
<nav class="menu" role="navigation" aria-label="main navigation">
<div class="language_switcher_placeholder"></div>
<h3><a href="../contents.html">目录</a></h3>
<ul>
<li><a class="reference internal" href="#">模块对象</a><ul>
<li><a class="reference internal" href="#initializing-c-modules">初始化 C 模块</a><ul>
<li><a class="reference internal" href="#single-phase-initialization">Single-phase initialization</a></li>
<li><a class="reference internal" href="#multi-phase-initialization">Multi-phase initialization</a></li>
<li><a class="reference internal" href="#low-level-module-creation-functions">Low-level module creation functions</a></li>
<li><a class="reference internal" href="#support-functions">Support functions</a></li>
</ul>
</li>
<li><a class="reference internal" href="#module-lookup">Module lookup</a></li>
</ul>
</li>
</ul>
<h4>上一个主题</h4>
<p class="topless"><a href="file.html"
title="上一章">文件对象</a></p>
<h4>下一个主题</h4>
<p class="topless"><a href="iterator.html"
title="下一章">迭代器对象</a></p>
<div role="note" aria-label="source link">
<h3>本页</h3>
<ul class="this-page-menu">
<li><a href="../bugs.html">报告 Bug</a></li>
<li>
<a href="https://github.com/python/cpython/blob/3.8/Doc/c-api/module.rst"
rel="nofollow">显示源代码
</a>
</li>
</ul>
</div>
</nav>
</div>
</div>
<div id="outdated-warning" style="padding: .5em; text-align: center; background-color: #FFBABA; color: #6A0E0E;">
这个文档所针对的是一个已不再受支持的 Python 旧版本。
你应当升级版本,并阅读
<a href="/3/c-api/module.html"> Python 当前稳定版本的文档</a>.
</div>
<div class="related" role="navigation" aria-label="related navigation">
<h3>导航</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="../genindex.html" title="总目录"
accesskey="I">索引</a></li>
<li class="right" >
<a href="../py-modindex.html" title="Python 模块索引"
>模块</a> |</li>
<li class="right" >
<a href="iterator.html" title="迭代器对象"
accesskey="N">下一页</a> |</li>
<li class="right" >
<a href="file.html" title="文件对象"
accesskey="P">上一页</a> |</li>
<li><img src="../_static/py.svg" alt="python logo" style="vertical-align: middle; margin-top: -1px"/></li>
<li><a href="https://www.python.org/">Python</a> &#187;</li>
<li class="switchers">
<div class="language_switcher_placeholder"></div>
<div class="version_switcher_placeholder"></div>
</li>
<li>
</li>
<li id="cpython-language-and-version">
<a href="../index.html">3.8.20 Documentation</a> &#187;
</li>
<li class="nav-item nav-item-1"><a href="index.html" >Python/C API 参考手册</a> &#187;</li>
<li class="nav-item nav-item-2"><a href="concrete.html" accesskey="U">具体的对象层</a> &#187;</li>
<li class="right">
<div class="inline-search" role="search">
<form class="inline-search" action="../search.html" method="get">
<input placeholder="快速搜索" aria-label="快速搜索" type="text" name="q" />
<input type="submit" value="转向" />
<input type="hidden" name="check_keywords" value="yes" />
<input type="hidden" name="area" value="default" />
</form>
</div>
|
</li>
</ul>
</div>
<div class="document">
<div class="documentwrapper">
<div class="bodywrapper">
<div class="body" role="main">
<section id="module-objects">
<span id="moduleobjects"></span><h1>模块对象<a class="headerlink" href="#module-objects" title="永久链接至标题"></a></h1>
<span class="target" id="index-0"></span><dl class="var">
<dt id="c.PyModule_Type">
<a class="reference internal" href="type.html#c.PyTypeObject" title="PyTypeObject">PyTypeObject</a> <code class="sig-name descname">PyModule_Type</code><a class="headerlink" href="#c.PyModule_Type" title="永久链接至目标"></a></dt>
<dd><p id="index-1">这个 <a class="reference internal" href="type.html#c.PyTypeObject" title="PyTypeObject"><code class="xref c c-type docutils literal notranslate"><span class="pre">PyTypeObject</span></code></a> 的实例代表 Python 模块类型。 它以 <code class="docutils literal notranslate"><span class="pre">types.ModuleType</span></code> 的形式暴露给 Python 程序。</p>
</dd></dl>
<dl class="function">
<dt id="c.PyModule_Check">
int <code class="sig-name descname">PyModule_Check</code><span class="sig-paren">(</span><a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a><em> *p</em><span class="sig-paren">)</span><a class="headerlink" href="#c.PyModule_Check" title="永久链接至目标"></a></dt>
<dd><p>Return true if <em>p</em> is a module object, or a subtype of a module object.</p>
</dd></dl>
<dl class="function">
<dt id="c.PyModule_CheckExact">
int <code class="sig-name descname">PyModule_CheckExact</code><span class="sig-paren">(</span><a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a><em> *p</em><span class="sig-paren">)</span><a class="headerlink" href="#c.PyModule_CheckExact" title="永久链接至目标"></a></dt>
<dd><p>Return true if <em>p</em> is a module object, but not a subtype of
<a class="reference internal" href="#c.PyModule_Type" title="PyModule_Type"><code class="xref c c-data docutils literal notranslate"><span class="pre">PyModule_Type</span></code></a>.</p>
</dd></dl>
<dl class="function">
<dt id="c.PyModule_NewObject">
<a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a>* <code class="sig-name descname">PyModule_NewObject</code><span class="sig-paren">(</span><a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a><em> *name</em><span class="sig-paren">)</span><a class="headerlink" href="#c.PyModule_NewObject" title="永久链接至目标"></a></dt>
<dd><em class="refcount">Return value: New reference.</em><p id="index-2">返回新的模块对象,其属性 <a class="reference internal" href="../reference/import.html#__name__" title="__name__"><code class="xref py py-attr docutils literal notranslate"><span class="pre">__name__</span></code></a><em>name</em> 。模块的这些属性 <a class="reference internal" href="../reference/import.html#__name__" title="__name__"><code class="xref py py-attr docutils literal notranslate"><span class="pre">__name__</span></code></a>, <code class="xref py py-attr docutils literal notranslate"><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 notranslate"><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 notranslate"><span class="pre">__loader__</span></code></a> (所有属性除了 <a class="reference internal" href="../reference/import.html#__name__" title="__name__"><code class="xref py py-attr docutils literal notranslate"><span class="pre">__name__</span></code></a> 都被设为``None``)。调用时应当提供 <a class="reference internal" href="../reference/import.html#__file__" title="__file__"><code class="xref py py-attr docutils literal notranslate"><span class="pre">__file__</span></code></a> 属性。</p>
<div class="versionadded">
<p><span class="versionmodified added">3.3 新版功能.</span></p>
</div>
<div class="versionchanged">
<p><span class="versionmodified changed">在 3.4 版更改: </span>属性 <a class="reference internal" href="../reference/import.html#__package__" title="__package__"><code class="xref py py-attr docutils literal notranslate"><span class="pre">__package__</span></code></a><a class="reference internal" href="../reference/import.html#__loader__" title="__loader__"><code class="xref py py-attr docutils literal notranslate"><span class="pre">__loader__</span></code></a> 被设为``None``。</p>
</div>
</dd></dl>
<dl class="function">
<dt id="c.PyModule_New">
<a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a>* <code class="sig-name descname">PyModule_New</code><span class="sig-paren">(</span>const char<em> *name</em><span class="sig-paren">)</span><a class="headerlink" href="#c.PyModule_New" title="永久链接至目标"></a></dt>
<dd><em class="refcount">Return value: New reference.</em><p>这类似于 <a class="reference internal" href="#c.PyModule_NewObject" title="PyModule_NewObject"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyModule_NewObject()</span></code></a> 但其名称为 UTF-8 编码的字符串而不是 Unicode 对象。</p>
</dd></dl>
<dl class="function">
<dt id="c.PyModule_GetDict">
<a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a>* <code class="sig-name descname">PyModule_GetDict</code><span class="sig-paren">(</span><a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a><em> *module</em><span class="sig-paren">)</span><a class="headerlink" href="#c.PyModule_GetDict" title="永久链接至目标"></a></dt>
<dd><em class="refcount">Return value: Borrowed reference.</em><p id="index-3">返回实现 <em>module</em> 的命名空间的字典对象;此对象与模块对象的 <a class="reference internal" href="../library/stdtypes.html#object.__dict__" title="object.__dict__"><code class="xref py py-attr docutils literal notranslate"><span class="pre">__dict__</span></code></a> 属性相同。 如果 <em>module</em> 不是一个模块对象(或模块对象的子类型),则会引发 <a class="reference internal" href="../library/exceptions.html#SystemError" title="SystemError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">SystemError</span></code></a> 并返回 <code class="docutils literal notranslate"><span class="pre">NULL</span></code></p>
<p>建议扩展使用其他 <code class="xref c c-func docutils literal notranslate"><span class="pre">PyModule_*()</span></code> and <code class="xref c c-func docutils literal notranslate"><span class="pre">PyObject_*()</span></code> 函数而不是直接操纵模块的 <a class="reference internal" href="../library/stdtypes.html#object.__dict__" title="object.__dict__"><code class="xref py py-attr docutils literal notranslate"><span class="pre">__dict__</span></code></a></p>
</dd></dl>
<dl class="function">
<dt id="c.PyModule_GetNameObject">
<a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a>* <code class="sig-name descname">PyModule_GetNameObject</code><span class="sig-paren">(</span><a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a><em> *module</em><span class="sig-paren">)</span><a class="headerlink" href="#c.PyModule_GetNameObject" title="永久链接至目标"></a></dt>
<dd><em class="refcount">Return value: New reference.</em><p id="index-4">返回 <em>module</em><a class="reference internal" href="../reference/import.html#__name__" title="__name__"><code class="xref py py-attr docutils literal notranslate"><span class="pre">__name__</span></code></a> 值。 如果模块未提供该值,或者如果它不是一个字符串,则会引发 <a class="reference internal" href="../library/exceptions.html#SystemError" title="SystemError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">SystemError</span></code></a> 并返回 <code class="docutils literal notranslate"><span class="pre">NULL</span></code></p>
<div class="versionadded">
<p><span class="versionmodified added">3.3 新版功能.</span></p>
</div>
</dd></dl>
<dl class="function">
<dt id="c.PyModule_GetName">
const char* <code class="sig-name descname">PyModule_GetName</code><span class="sig-paren">(</span><a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a><em> *module</em><span class="sig-paren">)</span><a class="headerlink" href="#c.PyModule_GetName" title="永久链接至目标"></a></dt>
<dd><p>类似于 <a class="reference internal" href="#c.PyModule_GetNameObject" title="PyModule_GetNameObject"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyModule_GetNameObject()</span></code></a> 但返回 <code class="docutils literal notranslate"><span class="pre">'utf-8'</span></code> 编码的名称。</p>
</dd></dl>
<dl class="function">
<dt id="c.PyModule_GetState">
void* <code class="sig-name descname">PyModule_GetState</code><span class="sig-paren">(</span><a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a><em> *module</em><span class="sig-paren">)</span><a class="headerlink" href="#c.PyModule_GetState" title="永久链接至目标"></a></dt>
<dd><p>返回模块的“状态”,也就是说,返回指向在模块创建时分配的内存块的指针,或者 <code class="docutils literal notranslate"><span class="pre">NULL</span></code>。 参见 <a class="reference internal" href="#c.PyModuleDef.m_size" title="PyModuleDef.m_size"><code class="xref c c-member docutils literal notranslate"><span class="pre">PyModuleDef.m_size</span></code></a></p>
</dd></dl>
<dl class="function">
<dt id="c.PyModule_GetDef">
<a class="reference internal" href="#c.PyModuleDef" title="PyModuleDef">PyModuleDef</a>* <code class="sig-name descname">PyModule_GetDef</code><span class="sig-paren">(</span><a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a><em> *module</em><span class="sig-paren">)</span><a class="headerlink" href="#c.PyModule_GetDef" title="永久链接至目标"></a></dt>
<dd><p>返回指向模块创建所使用的 <a class="reference internal" href="#c.PyModuleDef" title="PyModuleDef"><code class="xref c c-type docutils literal notranslate"><span class="pre">PyModuleDef</span></code></a> 结构体的指针,或者如果模块不是使用结构体定义创建的则返回 <code class="docutils literal notranslate"><span class="pre">NULL</span></code></p>
</dd></dl>
<dl class="function">
<dt id="c.PyModule_GetFilenameObject">
<a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a>* <code class="sig-name descname">PyModule_GetFilenameObject</code><span class="sig-paren">(</span><a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a><em> *module</em><span class="sig-paren">)</span><a class="headerlink" href="#c.PyModule_GetFilenameObject" title="永久链接至目标"></a></dt>
<dd><em class="refcount">Return value: New reference.</em><p id="index-5">返回使用 <em>module</em><a class="reference internal" href="../reference/import.html#__file__" title="__file__"><code class="xref py py-attr docutils literal notranslate"><span class="pre">__file__</span></code></a> 属性所加载的 <em>模块</em> 的文件名。 如果属性未定义,或者如果它不是一个 Unicode 字符串,则会引发 <a class="reference internal" href="../library/exceptions.html#SystemError" title="SystemError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">SystemError</span></code></a> 并返回 <code class="docutils literal notranslate"><span class="pre">NULL</span></code>;在其他情况下将返回一个指向 Unicode 对象的引用。</p>
<div class="versionadded">
<p><span class="versionmodified added">3.2 新版功能.</span></p>
</div>
</dd></dl>
<dl class="function">
<dt id="c.PyModule_GetFilename">
const char* <code class="sig-name descname">PyModule_GetFilename</code><span class="sig-paren">(</span><a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a><em> *module</em><span class="sig-paren">)</span><a class="headerlink" href="#c.PyModule_GetFilename" title="永久链接至目标"></a></dt>
<dd><p>Similar to <a class="reference internal" href="#c.PyModule_GetFilenameObject" title="PyModule_GetFilenameObject"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyModule_GetFilenameObject()</span></code></a> but return the filename
encoded to 'utf-8'.</p>
<div class="deprecated">
<p><span class="versionmodified deprecated">3.2 版后已移除: </span><a class="reference internal" href="#c.PyModule_GetFilename" title="PyModule_GetFilename"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyModule_GetFilename()</span></code></a> raises <code class="xref c c-type docutils literal notranslate"><span class="pre">UnicodeEncodeError</span></code> on
unencodable filenames, use <a class="reference internal" href="#c.PyModule_GetFilenameObject" title="PyModule_GetFilenameObject"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyModule_GetFilenameObject()</span></code></a> instead.</p>
</div>
</dd></dl>
<section id="initializing-c-modules">
<span id="initializing-modules"></span><h2>初始化 C 模块<a class="headerlink" href="#initializing-c-modules" title="永久链接至标题"></a></h2>
<p>Modules objects are usually created from extension modules (shared libraries
which export an initialization function), or compiled-in modules
(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 notranslate"><span class="pre">PyImport_AppendInittab()</span></code></a>).
See <a class="reference internal" href="../extending/building.html#building"><span class="std std-ref">构建C/C++扩展</span></a> or <a class="reference internal" href="../extending/embedding.html#extending-with-embedding"><span class="std std-ref">对嵌入 Python 功能进行扩展</span></a> for details.</p>
<p>The initialization function can either pass a module definition instance
to <a class="reference internal" href="#c.PyModule_Create" title="PyModule_Create"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyModule_Create()</span></code></a>, and return the resulting module object,
or request &quot;multi-phase initialization&quot; by returning the definition struct itself.</p>
<dl class="type">
<dt id="c.PyModuleDef">
<code class="sig-name descname">PyModuleDef</code><a class="headerlink" href="#c.PyModuleDef" title="永久链接至目标"></a></dt>
<dd><p>The module definition struct, which holds all information needed to create
a module object. There is usually only one statically initialized variable
of this type for each module.</p>
<dl class="member">
<dt id="c.PyModuleDef.m_base">
PyModuleDef_Base <code class="sig-name descname">m_base</code><a class="headerlink" href="#c.PyModuleDef.m_base" title="永久链接至目标"></a></dt>
<dd><p>Always initialize this member to <code class="xref py py-const docutils literal notranslate"><span class="pre">PyModuleDef_HEAD_INIT</span></code>.</p>
</dd></dl>
<dl class="member">
<dt id="c.PyModuleDef.m_name">
const char *<code class="sig-name descname">m_name</code><a class="headerlink" href="#c.PyModuleDef.m_name" title="永久链接至目标"></a></dt>
<dd><p>新模块的名称。</p>
</dd></dl>
<dl class="member">
<dt id="c.PyModuleDef.m_doc">
const char *<code class="sig-name descname">m_doc</code><a class="headerlink" href="#c.PyModuleDef.m_doc" title="永久链接至目标"></a></dt>
<dd><p>Docstring for the module; usually a docstring variable created with
<a class="reference internal" href="intro.html#c.PyDoc_STRVAR" title="PyDoc_STRVAR"><code class="xref c c-macro docutils literal notranslate"><span class="pre">PyDoc_STRVAR</span></code></a> is used.</p>
</dd></dl>
<dl class="member">
<dt id="c.PyModuleDef.m_size">
Py_ssize_t <code class="sig-name descname">m_size</code><a class="headerlink" href="#c.PyModuleDef.m_size" title="永久链接至目标"></a></dt>
<dd><p>Module state may be kept in a per-module memory area that can be
retrieved with <a class="reference internal" href="#c.PyModule_GetState" title="PyModule_GetState"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyModule_GetState()</span></code></a>, rather than in static globals.
This makes modules safe for use in multiple sub-interpreters.</p>
<p>This memory area is allocated based on <em>m_size</em> on module creation,
and freed when the module object is deallocated, after the
<code class="xref c c-member docutils literal notranslate"><span class="pre">m_free</span></code> function has been called, if present.</p>
<p>Setting <code class="docutils literal notranslate"><span class="pre">m_size</span></code> to <code class="docutils literal notranslate"><span class="pre">-1</span></code> means that the module does not support
sub-interpreters, because it has global state.</p>
<p>Setting it to a non-negative value means that the module can be
re-initialized and specifies the additional amount of memory it requires
for its state. Non-negative <code class="docutils literal notranslate"><span class="pre">m_size</span></code> is required for multi-phase
initialization.</p>
<p>请参阅 <span class="target" id="index-10"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-3121"><strong>PEP 3121</strong></a> 了解详情。</p>
</dd></dl>
<dl class="member">
<dt id="c.PyModuleDef.m_methods">
<a class="reference internal" href="structures.html#c.PyMethodDef" title="PyMethodDef">PyMethodDef</a>* <code class="sig-name descname">m_methods</code><a class="headerlink" href="#c.PyModuleDef.m_methods" title="永久链接至目标"></a></dt>
<dd><p>A pointer to a table of module-level functions, described by
<a class="reference internal" href="structures.html#c.PyMethodDef" title="PyMethodDef"><code class="xref c c-type docutils literal notranslate"><span class="pre">PyMethodDef</span></code></a> values. Can be <code class="docutils literal notranslate"><span class="pre">NULL</span></code> if no functions are present.</p>
</dd></dl>
<dl class="member">
<dt id="c.PyModuleDef.m_slots">
<a class="reference internal" href="#c.PyModuleDef_Slot" title="PyModuleDef_Slot">PyModuleDef_Slot</a>* <code class="sig-name descname">m_slots</code><a class="headerlink" href="#c.PyModuleDef.m_slots" title="永久链接至目标"></a></dt>
<dd><p>An array of slot definitions for multi-phase initialization, terminated by
a <code class="docutils literal notranslate"><span class="pre">{0,</span> <span class="pre">NULL}</span></code> entry.
When using single-phase initialization, <em>m_slots</em> must be <code class="docutils literal notranslate"><span class="pre">NULL</span></code>.</p>
<div class="versionchanged">
<p><span class="versionmodified changed">在 3.5 版更改: </span>Prior to version 3.5, this member was always set to <code class="docutils literal notranslate"><span class="pre">NULL</span></code>,
and was defined as:</p>
<blockquote>
<div><dl class="member">
<dt id="c.PyModuleDef.m_reload">
<a class="reference internal" href="gcsupport.html#c.inquiry" title="inquiry">inquiry</a> <code class="sig-name descname">m_reload</code><a class="headerlink" href="#c.PyModuleDef.m_reload" title="永久链接至目标"></a></dt>
<dd></dd></dl>
</div></blockquote>
</div>
</dd></dl>
<dl class="member">
<dt id="c.PyModuleDef.m_traverse">
<a class="reference internal" href="gcsupport.html#c.traverseproc" title="traverseproc">traverseproc</a> <code class="sig-name descname">m_traverse</code><a class="headerlink" href="#c.PyModuleDef.m_traverse" title="永久链接至目标"></a></dt>
<dd><p>A traversal function to call during GC traversal of the module object, or
<code class="docutils literal notranslate"><span class="pre">NULL</span></code> if not needed. This function may be called before module state
is allocated (<a class="reference internal" href="#c.PyModule_GetState" title="PyModule_GetState"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyModule_GetState()</span></code></a> may return <cite>NULL</cite>),
and before the <a class="reference internal" href="#c.Py_mod_exec" title="Py_mod_exec"><code class="xref c c-member docutils literal notranslate"><span class="pre">Py_mod_exec</span></code></a> function is executed.</p>
</dd></dl>
<dl class="member">
<dt id="c.PyModuleDef.m_clear">
<a class="reference internal" href="gcsupport.html#c.inquiry" title="inquiry">inquiry</a> <code class="sig-name descname">m_clear</code><a class="headerlink" href="#c.PyModuleDef.m_clear" title="永久链接至目标"></a></dt>
<dd><p>A clear function to call during GC clearing of the module object, or
<code class="docutils literal notranslate"><span class="pre">NULL</span></code> if not needed. This function may be called before module state
is allocated (<a class="reference internal" href="#c.PyModule_GetState" title="PyModule_GetState"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyModule_GetState()</span></code></a> may return <cite>NULL</cite>),
and before the <a class="reference internal" href="#c.Py_mod_exec" title="Py_mod_exec"><code class="xref c c-member docutils literal notranslate"><span class="pre">Py_mod_exec</span></code></a> function is executed.</p>
</dd></dl>
<dl class="member">
<dt id="c.PyModuleDef.m_free">
<a class="reference internal" href="typeobj.html#c.freefunc" title="freefunc">freefunc</a> <code class="sig-name descname">m_free</code><a class="headerlink" href="#c.PyModuleDef.m_free" title="永久链接至目标"></a></dt>
<dd><p>A function to call during deallocation of the module object, or <code class="docutils literal notranslate"><span class="pre">NULL</span></code> if
not needed. This function may be called before module state
is allocated (<a class="reference internal" href="#c.PyModule_GetState" title="PyModule_GetState"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyModule_GetState()</span></code></a> may return <cite>NULL</cite>),
and before the <a class="reference internal" href="#c.Py_mod_exec" title="Py_mod_exec"><code class="xref c c-member docutils literal notranslate"><span class="pre">Py_mod_exec</span></code></a> function is executed.</p>
</dd></dl>
</dd></dl>
<section id="single-phase-initialization">
<h3>Single-phase initialization<a class="headerlink" href="#single-phase-initialization" title="永久链接至标题"></a></h3>
<p>The module initialization function may create and return the module object
directly. This is referred to as &quot;single-phase initialization&quot;, and uses one
of the following two module creation functions:</p>
<dl class="function">
<dt id="c.PyModule_Create">
<a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a>* <code class="sig-name descname">PyModule_Create</code><span class="sig-paren">(</span><a class="reference internal" href="#c.PyModuleDef" title="PyModuleDef">PyModuleDef</a><em> *def</em><span class="sig-paren">)</span><a class="headerlink" href="#c.PyModule_Create" title="永久链接至目标"></a></dt>
<dd><em class="refcount">Return value: New reference.</em><p>Create a new module object, given the definition in <em>def</em>. This behaves
like <a class="reference internal" href="#c.PyModule_Create2" title="PyModule_Create2"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyModule_Create2()</span></code></a> with <em>module_api_version</em> set to
<code class="xref py py-const docutils literal notranslate"><span class="pre">PYTHON_API_VERSION</span></code>.</p>
</dd></dl>
<dl class="function">
<dt id="c.PyModule_Create2">
<a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a>* <code class="sig-name descname">PyModule_Create2</code><span class="sig-paren">(</span><a class="reference internal" href="#c.PyModuleDef" title="PyModuleDef">PyModuleDef</a><em> *def</em>, int<em> module_api_version</em><span class="sig-paren">)</span><a class="headerlink" href="#c.PyModule_Create2" title="永久链接至目标"></a></dt>
<dd><em class="refcount">Return value: New reference.</em><p>Create a new module object, given the definition in <em>def</em>, assuming the
API version <em>module_api_version</em>. If that version does not match the version
of the running interpreter, a <a class="reference internal" href="../library/exceptions.html#RuntimeWarning" title="RuntimeWarning"><code class="xref py py-exc docutils literal notranslate"><span class="pre">RuntimeWarning</span></code></a> is emitted.</p>
<div class="admonition note">
<p class="admonition-title">注解</p>
<p>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 notranslate"><span class="pre">PyModule_Create()</span></code></a>
instead; only use this if you are sure you need it.</p>
</div>
</dd></dl>
<p>Before it is returned from in the initialization function, the resulting module
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 notranslate"><span class="pre">PyModule_AddObject()</span></code></a>.</p>
</section>
<section id="multi-phase-initialization">
<span id="id1"></span><h3>Multi-phase initialization<a class="headerlink" href="#multi-phase-initialization" title="永久链接至标题"></a></h3>
<p>An alternate way to specify extensions is to request &quot;multi-phase initialization&quot;.
Extension modules created this way behave more like Python modules: the
initialization is split between the <em>creation phase</em>, when the module object
is created, and the <em>execution phase</em>, when it is populated.
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 notranslate"><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 notranslate"><span class="pre">__init__()</span></code></a> methods
of classes.</p>
<p>Unlike modules created using single-phase initialization, these modules are not
singletons: if the <em>sys.modules</em> entry is removed and the module is re-imported,
a new module object is created, and the old module is subject to normal garbage
collection -- as with Python modules.
By default, multiple modules created from the same definition should be
independent: changes to one should not affect the others.
This means that all state should be specific to the module object (using e.g.
using <a class="reference internal" href="#c.PyModule_GetState" title="PyModule_GetState"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyModule_GetState()</span></code></a>), or its contents (such as the module's
<a class="reference internal" href="../library/stdtypes.html#object.__dict__" title="object.__dict__"><code class="xref py py-attr docutils literal notranslate"><span class="pre">__dict__</span></code></a> 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 notranslate"><span class="pre">PyType_FromSpec()</span></code></a>).</p>
<p>All modules created using multi-phase initialization are expected to support
<a class="reference internal" href="init.html#sub-interpreter-support"><span class="std std-ref">sub-interpreters</span></a>. Making sure multiple modules
are independent is typically enough to achieve this.</p>
<p>To request multi-phase initialization, the initialization function
(PyInit_modulename) returns a <a class="reference internal" href="#c.PyModuleDef" title="PyModuleDef"><code class="xref c c-type docutils literal notranslate"><span class="pre">PyModuleDef</span></code></a> instance with non-empty
<a class="reference internal" href="#c.PyModuleDef.m_slots" title="PyModuleDef.m_slots"><code class="xref c c-member docutils literal notranslate"><span class="pre">m_slots</span></code></a>. Before it is returned, the <code class="docutils literal notranslate"><span class="pre">PyModuleDef</span></code>
instance must be initialized with the following function:</p>
<dl class="function">
<dt id="c.PyModuleDef_Init">
<a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a>* <code class="sig-name descname">PyModuleDef_Init</code><span class="sig-paren">(</span><a class="reference internal" href="#c.PyModuleDef" title="PyModuleDef">PyModuleDef</a><em> *def</em><span class="sig-paren">)</span><a class="headerlink" href="#c.PyModuleDef_Init" title="永久链接至目标"></a></dt>
<dd><em class="refcount">Return value: Borrowed reference.</em><p>Ensures a module definition is a properly initialized Python object that
correctly reports its type and reference count.</p>
<p>Returns <em>def</em> cast to <code class="docutils literal notranslate"><span class="pre">PyObject*</span></code>, or <code class="docutils literal notranslate"><span class="pre">NULL</span></code> if an error occurred.</p>
<div class="versionadded">
<p><span class="versionmodified added">3.5 新版功能.</span></p>
</div>
</dd></dl>
<p>The <em>m_slots</em> member of the module definition must point to an array of
<code class="docutils literal notranslate"><span class="pre">PyModuleDef_Slot</span></code> structures:</p>
<dl class="type">
<dt id="c.PyModuleDef_Slot">
<code class="sig-name descname">PyModuleDef_Slot</code><a class="headerlink" href="#c.PyModuleDef_Slot" title="永久链接至目标"></a></dt>
<dd><dl class="member">
<dt id="c.PyModuleDef_Slot.slot">
int <code class="sig-name descname">slot</code><a class="headerlink" href="#c.PyModuleDef_Slot.slot" title="永久链接至目标"></a></dt>
<dd><p>A slot ID, chosen from the available values explained below.</p>
</dd></dl>
<dl class="member">
<dt id="c.PyModuleDef_Slot.value">
void* <code class="sig-name descname">value</code><a class="headerlink" href="#c.PyModuleDef_Slot.value" title="永久链接至目标"></a></dt>
<dd><p>Value of the slot, whose meaning depends on the slot ID.</p>
</dd></dl>
<div class="versionadded">
<p><span class="versionmodified added">3.5 新版功能.</span></p>
</div>
</dd></dl>
<p>The <em>m_slots</em> array must be terminated by a slot with id 0.</p>
<p>The available slot types are:</p>
<dl class="macro">
<dt id="c.Py_mod_create">
<code class="sig-name descname">Py_mod_create</code><a class="headerlink" href="#c.Py_mod_create" title="永久链接至目标"></a></dt>
<dd><p>Specifies a function that is called to create the module object itself.
The <em>value</em> pointer of this slot must point to a function of the signature:</p>
<dl class="function">
<dt id="c.create_module">
<a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a>* <code class="sig-name descname">create_module</code><span class="sig-paren">(</span><a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a><em> *spec</em>, <a class="reference internal" href="#c.PyModuleDef" title="PyModuleDef">PyModuleDef</a><em> *def</em><span class="sig-paren">)</span><a class="headerlink" href="#c.create_module" title="永久链接至目标"></a></dt>
<dd></dd></dl>
<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 notranslate"><span class="pre">ModuleSpec</span></code></a>
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.
It should return a new module object, or set an error
and return <code class="docutils literal notranslate"><span class="pre">NULL</span></code>.</p>
<p>This function should be kept minimal. In particular, it should not
call arbitrary Python code, as trying to import the same module again may
result in an infinite loop.</p>
<p>Multiple <code class="docutils literal notranslate"><span class="pre">Py_mod_create</span></code> slots may not be specified in one module
definition.</p>
<p>If <code class="docutils literal notranslate"><span class="pre">Py_mod_create</span></code> is not specified, the import machinery will create
a normal module object using <a class="reference internal" href="#c.PyModule_New" title="PyModule_New"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyModule_New()</span></code></a>. The name is taken from
<em>spec</em>, not the definition, to allow extension modules to dynamically adjust
to their place in the module hierarchy and be imported under different
names through symlinks, all while sharing a single module definition.</p>
<p>There is no requirement for the returned object to be an instance of
<a class="reference internal" href="#c.PyModule_Type" title="PyModule_Type"><code class="xref c c-type docutils literal notranslate"><span class="pre">PyModule_Type</span></code></a>. Any type can be used, as long as it supports
setting and getting import-related attributes.
However, only <code class="docutils literal notranslate"><span class="pre">PyModule_Type</span></code> instances may be returned if the
<code class="docutils literal notranslate"><span class="pre">PyModuleDef</span></code> has non-<code class="docutils literal notranslate"><span class="pre">NULL</span></code> <code class="docutils literal notranslate"><span class="pre">m_traverse</span></code>, <code class="docutils literal notranslate"><span class="pre">m_clear</span></code>,
<code class="docutils literal notranslate"><span class="pre">m_free</span></code>; non-zero <code class="docutils literal notranslate"><span class="pre">m_size</span></code>; or slots other than <code class="docutils literal notranslate"><span class="pre">Py_mod_create</span></code>.</p>
</dd></dl>
<dl class="macro">
<dt id="c.Py_mod_exec">
<code class="sig-name descname">Py_mod_exec</code><a class="headerlink" href="#c.Py_mod_exec" title="永久链接至目标"></a></dt>
<dd><p>Specifies a function that is called to <em>execute</em> the module.
This is equivalent to executing the code of a Python module: typically,
this function adds classes and constants to the module.
The signature of the function is:</p>
<dl class="function">
<dt id="c.exec_module">
int <code class="sig-name descname">exec_module</code><span class="sig-paren">(</span><a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a>*<em> module</em><span class="sig-paren">)</span><a class="headerlink" href="#c.exec_module" title="永久链接至目标"></a></dt>
<dd></dd></dl>
<p>If multiple <code class="docutils literal notranslate"><span class="pre">Py_mod_exec</span></code> slots are specified, they are processed in the
order they appear in the <em>m_slots</em> array.</p>
</dd></dl>
<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>
</section>
<section id="low-level-module-creation-functions">
<h3>Low-level module creation functions<a class="headerlink" href="#low-level-module-creation-functions" title="永久链接至标题"></a></h3>
<p>The following functions are called under the hood when using multi-phase
initialization. They can be used directly, for example when creating module
objects dynamically. Note that both <code class="docutils literal notranslate"><span class="pre">PyModule_FromDefAndSpec</span></code> and
<code class="docutils literal notranslate"><span class="pre">PyModule_ExecDef</span></code> must be called to fully initialize a module.</p>
<dl class="function">
<dt id="c.PyModule_FromDefAndSpec">
<a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a> * <code class="sig-name descname">PyModule_FromDefAndSpec</code><span class="sig-paren">(</span><a class="reference internal" href="#c.PyModuleDef" title="PyModuleDef">PyModuleDef</a><em> *def</em>, <a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a><em> *spec</em><span class="sig-paren">)</span><a class="headerlink" href="#c.PyModule_FromDefAndSpec" title="永久链接至目标"></a></dt>
<dd><em class="refcount">Return value: New reference.</em><p>Create a new module object, given the definition in <em>module</em> and the
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 notranslate"><span class="pre">PyModule_FromDefAndSpec2()</span></code></a>
with <em>module_api_version</em> set to <code class="xref py py-const docutils literal notranslate"><span class="pre">PYTHON_API_VERSION</span></code>.</p>
<div class="versionadded">
<p><span class="versionmodified added">3.5 新版功能.</span></p>
</div>
</dd></dl>
<dl class="function">
<dt id="c.PyModule_FromDefAndSpec2">
<a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a> * <code class="sig-name descname">PyModule_FromDefAndSpec2</code><span class="sig-paren">(</span><a class="reference internal" href="#c.PyModuleDef" title="PyModuleDef">PyModuleDef</a><em> *def</em>, <a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a><em> *spec</em>, int<em> module_api_version</em><span class="sig-paren">)</span><a class="headerlink" href="#c.PyModule_FromDefAndSpec2" title="永久链接至目标"></a></dt>
<dd><em class="refcount">Return value: New reference.</em><p>Create a new module object, given the definition in <em>module</em> and the
ModuleSpec <em>spec</em>, assuming the API version <em>module_api_version</em>.
If that version does not match the version of the running interpreter,
a <a class="reference internal" href="../library/exceptions.html#RuntimeWarning" title="RuntimeWarning"><code class="xref py py-exc docutils literal notranslate"><span class="pre">RuntimeWarning</span></code></a> is emitted.</p>
<div class="admonition note">
<p class="admonition-title">注解</p>
<p>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 notranslate"><span class="pre">PyModule_FromDefAndSpec()</span></code></a>
instead; only use this if you are sure you need it.</p>
</div>
<div class="versionadded">
<p><span class="versionmodified added">3.5 新版功能.</span></p>
</div>
</dd></dl>
<dl class="function">
<dt id="c.PyModule_ExecDef">
int <code class="sig-name descname">PyModule_ExecDef</code><span class="sig-paren">(</span><a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a><em> *module</em>, <a class="reference internal" href="#c.PyModuleDef" title="PyModuleDef">PyModuleDef</a><em> *def</em><span class="sig-paren">)</span><a class="headerlink" href="#c.PyModule_ExecDef" title="永久链接至目标"></a></dt>
<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 notranslate"><span class="pre">Py_mod_exec</span></code></a>) given in <em>def</em>.</p>
<div class="versionadded">
<p><span class="versionmodified added">3.5 新版功能.</span></p>
</div>
</dd></dl>
<dl class="function">
<dt id="c.PyModule_SetDocString">
int <code class="sig-name descname">PyModule_SetDocString</code><span class="sig-paren">(</span><a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a><em> *module</em>, const char<em> *docstring</em><span class="sig-paren">)</span><a class="headerlink" href="#c.PyModule_SetDocString" title="永久链接至目标"></a></dt>
<dd><p>Set the docstring for <em>module</em> to <em>docstring</em>.
This function is called automatically when creating a module from
<code class="docutils literal notranslate"><span class="pre">PyModuleDef</span></code>, using either <code class="docutils literal notranslate"><span class="pre">PyModule_Create</span></code> or
<code class="docutils literal notranslate"><span class="pre">PyModule_FromDefAndSpec</span></code>.</p>
<div class="versionadded">
<p><span class="versionmodified added">3.5 新版功能.</span></p>
</div>
</dd></dl>
<dl class="function">
<dt id="c.PyModule_AddFunctions">
int <code class="sig-name descname">PyModule_AddFunctions</code><span class="sig-paren">(</span><a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a><em> *module</em>, <a class="reference internal" href="structures.html#c.PyMethodDef" title="PyMethodDef">PyMethodDef</a><em> *functions</em><span class="sig-paren">)</span><a class="headerlink" href="#c.PyModule_AddFunctions" title="永久链接至目标"></a></dt>
<dd><p>Add the functions from the <code class="docutils literal notranslate"><span class="pre">NULL</span></code> terminated <em>functions</em> array to <em>module</em>.
Refer to the <a class="reference internal" href="structures.html#c.PyMethodDef" title="PyMethodDef"><code class="xref c c-type docutils literal notranslate"><span class="pre">PyMethodDef</span></code></a> documentation for details on individual
entries (due to the lack of a shared module namespace, module level
&quot;functions&quot; implemented in C typically receive the module as their first
parameter, making them similar to instance methods on Python classes).
This function is called automatically when creating a module from
<code class="docutils literal notranslate"><span class="pre">PyModuleDef</span></code>, using either <code class="docutils literal notranslate"><span class="pre">PyModule_Create</span></code> or
<code class="docutils literal notranslate"><span class="pre">PyModule_FromDefAndSpec</span></code>.</p>
<div class="versionadded">
<p><span class="versionmodified added">3.5 新版功能.</span></p>
</div>
</dd></dl>
</section>
<section id="support-functions">
<h3>Support functions<a class="headerlink" href="#support-functions" title="永久链接至标题"></a></h3>
<p>The module initialization function (if using single phase initialization) or
a function called from a module execution slot (if using multi-phase
initialization), can use the following functions to help initialize the module
state:</p>
<dl class="function">
<dt id="c.PyModule_AddObject">
int <code class="sig-name descname">PyModule_AddObject</code><span class="sig-paren">(</span><a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a><em> *module</em>, const char<em> *name</em>, <a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a><em> *value</em><span class="sig-paren">)</span><a class="headerlink" href="#c.PyModule_AddObject" title="永久链接至目标"></a></dt>
<dd><p>Add an object to <em>module</em> as <em>name</em>. This is a convenience function which can
be used from the module's initialization function. This steals a reference to
<em>value</em> on success. Return <code class="docutils literal notranslate"><span class="pre">-1</span></code> on error, <code class="docutils literal notranslate"><span class="pre">0</span></code> on success.</p>
<div class="admonition note">
<p class="admonition-title">注解</p>
<p>Unlike other functions that steal references, <code class="docutils literal notranslate"><span class="pre">PyModule_AddObject()</span></code> only
decrements the reference count of <em>value</em> <strong>on success</strong>.</p>
<p>This means that its return value must be checked, and calling code must
<a class="reference internal" href="refcounting.html#c.Py_DECREF" title="Py_DECREF"><code class="xref c c-func docutils literal notranslate"><span class="pre">Py_DECREF()</span></code></a> <em>value</em> manually on error. Example usage:</p>
<div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="n">Py_INCREF</span><span class="p">(</span><span class="n">spam</span><span class="p">);</span>
<span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">PyModule_AddObject</span><span class="p">(</span><span class="n">module</span><span class="p">,</span><span class="w"> </span><span class="s">&quot;spam&quot;</span><span class="p">,</span><span class="w"> </span><span class="n">spam</span><span class="p">)</span><span class="w"> </span><span class="o">&lt;</span><span class="w"> </span><span class="mi">0</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="n">Py_DECREF</span><span class="p">(</span><span class="n">module</span><span class="p">);</span>
<span class="w"> </span><span class="n">Py_DECREF</span><span class="p">(</span><span class="n">spam</span><span class="p">);</span>
<span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="nb">NULL</span><span class="p">;</span>
<span class="p">}</span>
</pre></div>
</div>
</div>
</dd></dl>
<dl class="function">
<dt id="c.PyModule_AddIntConstant">
int <code class="sig-name descname">PyModule_AddIntConstant</code><span class="sig-paren">(</span><a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a><em> *module</em>, const char<em> *name</em>, long<em> value</em><span class="sig-paren">)</span><a class="headerlink" href="#c.PyModule_AddIntConstant" title="永久链接至目标"></a></dt>
<dd><p>Add an integer constant to <em>module</em> as <em>name</em>. This convenience function can be
used from the module's initialization function. Return <code class="docutils literal notranslate"><span class="pre">-1</span></code> on error, <code class="docutils literal notranslate"><span class="pre">0</span></code> on
success.</p>
</dd></dl>
<dl class="function">
<dt id="c.PyModule_AddStringConstant">
int <code class="sig-name descname">PyModule_AddStringConstant</code><span class="sig-paren">(</span><a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a><em> *module</em>, const char<em> *name</em>, const char<em> *value</em><span class="sig-paren">)</span><a class="headerlink" href="#c.PyModule_AddStringConstant" title="永久链接至目标"></a></dt>
<dd><p>Add a string constant to <em>module</em> as <em>name</em>. This convenience function can be
used from the module's initialization function. The string <em>value</em> must be
<code class="docutils literal notranslate"><span class="pre">NULL</span></code>-terminated. Return <code class="docutils literal notranslate"><span class="pre">-1</span></code> on error, <code class="docutils literal notranslate"><span class="pre">0</span></code> on success.</p>
</dd></dl>
<dl class="function">
<dt id="c.PyModule_AddIntMacro">
int <code class="sig-name descname">PyModule_AddIntMacro</code><span class="sig-paren">(</span><a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a><em> *module</em>, macro<span class="sig-paren">)</span><a class="headerlink" href="#c.PyModule_AddIntMacro" title="永久链接至目标"></a></dt>
<dd><p>Add an int constant to <em>module</em>. The name and the value are taken from
<em>macro</em>. For example <code class="docutils literal notranslate"><span class="pre">PyModule_AddIntMacro(module,</span> <span class="pre">AF_INET)</span></code> adds the int
constant <em>AF_INET</em> with the value of <em>AF_INET</em> to <em>module</em>.
Return <code class="docutils literal notranslate"><span class="pre">-1</span></code> on error, <code class="docutils literal notranslate"><span class="pre">0</span></code> on success.</p>
</dd></dl>
<dl class="function">
<dt id="c.PyModule_AddStringMacro">
int <code class="sig-name descname">PyModule_AddStringMacro</code><span class="sig-paren">(</span><a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a><em> *module</em>, macro<span class="sig-paren">)</span><a class="headerlink" href="#c.PyModule_AddStringMacro" title="永久链接至目标"></a></dt>
<dd><p>Add a string constant to <em>module</em>.</p>
</dd></dl>
</section>
</section>
<section id="module-lookup">
<h2>Module lookup<a class="headerlink" href="#module-lookup" title="永久链接至标题"></a></h2>
<p>Single-phase initialization creates singleton modules that can be looked up
in the context of the current interpreter. This allows the module object to be
retrieved later with only a reference to the module definition.</p>
<p>These functions will not work on modules created using multi-phase initialization,
since multiple such modules can be created from a single definition.</p>
<dl class="function">
<dt id="c.PyState_FindModule">
<a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a>* <code class="sig-name descname">PyState_FindModule</code><span class="sig-paren">(</span><a class="reference internal" href="#c.PyModuleDef" title="PyModuleDef">PyModuleDef</a><em> *def</em><span class="sig-paren">)</span><a class="headerlink" href="#c.PyState_FindModule" title="永久链接至目标"></a></dt>
<dd><em class="refcount">Return value: Borrowed reference.</em><p>Returns the module object that was created from <em>def</em> for the current interpreter.
This method requires that the module object has been attached to the interpreter state with
<a class="reference internal" href="#c.PyState_AddModule" title="PyState_AddModule"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyState_AddModule()</span></code></a> beforehand. In case the corresponding module object is not
found or has not been attached to the interpreter state yet, it returns <code class="docutils literal notranslate"><span class="pre">NULL</span></code>.</p>
</dd></dl>
<dl class="function">
<dt id="c.PyState_AddModule">
int <code class="sig-name descname">PyState_AddModule</code><span class="sig-paren">(</span><a class="reference internal" href="structures.html#c.PyObject" title="PyObject">PyObject</a><em> *module</em>, <a class="reference internal" href="#c.PyModuleDef" title="PyModuleDef">PyModuleDef</a><em> *def</em><span class="sig-paren">)</span><a class="headerlink" href="#c.PyState_AddModule" title="永久链接至目标"></a></dt>
<dd><p>Attaches the module object passed to the function to the interpreter state. This allows
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 notranslate"><span class="pre">PyState_FindModule()</span></code></a>.</p>
<p>Only effective on modules created using single-phase initialization.</p>
<p>Python calls <code class="docutils literal notranslate"><span class="pre">PyState_AddModule</span></code> automatically after importing a module,
so it is unnecessary (but harmless) to call it from module initialization
code. An explicit call is needed only if the module's own init code
subsequently calls <code class="docutils literal notranslate"><span class="pre">PyState_FindModule</span></code>.
The function is mainly intended for implementing alternative import
mechanisms (either by calling it directly, or by referring to its
implementation for details of the required state updates).</p>
<p>Return 0 on success or -1 on failure.</p>
<div class="versionadded">
<p><span class="versionmodified added">3.3 新版功能.</span></p>
</div>
</dd></dl>
<dl class="function">
<dt id="c.PyState_RemoveModule">
int <code class="sig-name descname">PyState_RemoveModule</code><span class="sig-paren">(</span><a class="reference internal" href="#c.PyModuleDef" title="PyModuleDef">PyModuleDef</a><em> *def</em><span class="sig-paren">)</span><a class="headerlink" href="#c.PyState_RemoveModule" title="永久链接至目标"></a></dt>
<dd><p>Removes the module object created from <em>def</em> from the interpreter state.
Return 0 on success or -1 on failure.</p>
<div class="versionadded">
<p><span class="versionmodified added">3.3 新版功能.</span></p>
</div>
</dd></dl>
</section>
</section>
</div>
</div>
</div>
<div class="sphinxsidebar" role="navigation" aria-label="main navigation">
<div class="sphinxsidebarwrapper">
<h3><a href="../contents.html">目录</a></h3>
<ul>
<li><a class="reference internal" href="#">模块对象</a><ul>
<li><a class="reference internal" href="#initializing-c-modules">初始化 C 模块</a><ul>
<li><a class="reference internal" href="#single-phase-initialization">Single-phase initialization</a></li>
<li><a class="reference internal" href="#multi-phase-initialization">Multi-phase initialization</a></li>
<li><a class="reference internal" href="#low-level-module-creation-functions">Low-level module creation functions</a></li>
<li><a class="reference internal" href="#support-functions">Support functions</a></li>
</ul>
</li>
<li><a class="reference internal" href="#module-lookup">Module lookup</a></li>
</ul>
</li>
</ul>
<h4>上一个主题</h4>
<p class="topless"><a href="file.html"
title="上一章">文件对象</a></p>
<h4>下一个主题</h4>
<p class="topless"><a href="iterator.html"
title="下一章">迭代器对象</a></p>
<div role="note" aria-label="source link">
<h3>本页</h3>
<ul class="this-page-menu">
<li><a href="../bugs.html">报告 Bug</a></li>
<li>
<a href="https://github.com/python/cpython/blob/3.8/Doc/c-api/module.rst"
rel="nofollow">显示源代码
</a>
</li>
</ul>
</div>
</div>
</div>
<div class="clearer"></div>
</div>
<div class="related" role="navigation" aria-label="related navigation">
<h3>导航</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="../genindex.html" title="总目录"
>索引</a></li>
<li class="right" >
<a href="../py-modindex.html" title="Python 模块索引"
>模块</a> |</li>
<li class="right" >
<a href="iterator.html" title="迭代器对象"
>下一页</a> |</li>
<li class="right" >
<a href="file.html" title="文件对象"
>上一页</a> |</li>
<li><img src="../_static/py.svg" alt="python logo" style="vertical-align: middle; margin-top: -1px"/></li>
<li><a href="https://www.python.org/">Python</a> &#187;</li>
<li class="switchers">
<div class="language_switcher_placeholder"></div>
<div class="version_switcher_placeholder"></div>
</li>
<li>
</li>
<li id="cpython-language-and-version">
<a href="../index.html">3.8.20 Documentation</a> &#187;
</li>
<li class="nav-item nav-item-1"><a href="index.html" >Python/C API 参考手册</a> &#187;</li>
<li class="nav-item nav-item-2"><a href="concrete.html" >具体的对象层</a> &#187;</li>
<li class="right">
<div class="inline-search" role="search">
<form class="inline-search" action="../search.html" method="get">
<input placeholder="快速搜索" aria-label="快速搜索" type="text" name="q" />
<input type="submit" value="转向" />
<input type="hidden" name="check_keywords" value="yes" />
<input type="hidden" name="area" value="default" />
</form>
</div>
|
</li>
</ul>
</div>
<div class="footer">
&copy; <a href="../copyright.html">版权所有</a> 2001-2024, Python Software Foundation.
<br />
This page is licensed under the Python Software Foundation License Version 2.
<br />
Examples, recipes, and other code in the documentation are additionally licensed under the Zero Clause BSD License.
<br />
<br />
The Python Software Foundation is a non-profit corporation.
<a href="https://www.python.org/psf/donations/">Please donate.</a>
<br />
<br />
最后更新于 12月 09, 2024.
<a href="https://docs.python.org/3/bugs.html">Found a bug</a>?
<br />
Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 2.4.4.
</div>
</body>
</html>