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

1398 lines
129 KiB
HTML
Raw Permalink 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>Python初始化配置 &#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="memory.html" />
<link rel="prev" title="初始化,最终化和线程" href="init.html" />
<link rel="canonical" href="https://docs.python.org/3/c-api/init_config.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="#">Python初始化配置</a><ul>
<li><a class="reference internal" href="#pywidestringlist">PyWideStringList</a></li>
<li><a class="reference internal" href="#pystatus">PyStatus</a></li>
<li><a class="reference internal" href="#pypreconfig">PyPreConfig</a></li>
<li><a class="reference internal" href="#preinitialization-with-pypreconfig">Preinitialization with PyPreConfig</a></li>
<li><a class="reference internal" href="#pyconfig">PyConfig</a></li>
<li><a class="reference internal" href="#initialization-with-pyconfig">使用 PyConfig 初始化</a></li>
<li><a class="reference internal" href="#isolated-configuration">隔离配置</a></li>
<li><a class="reference internal" href="#python-configuration">Python 配置</a></li>
<li><a class="reference internal" href="#path-configuration">路径配置</a></li>
<li><a class="reference internal" href="#py-runmain">Py_RunMain()</a></li>
<li><a class="reference internal" href="#multi-phase-initialization-private-provisional-api">多阶段初始化私有暂定 API</a></li>
</ul>
</li>
</ul>
<h4>上一个主题</h4>
<p class="topless"><a href="init.html"
title="上一章">初始化,最终化和线程</a></p>
<h4>下一个主题</h4>
<p class="topless"><a href="memory.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/init_config.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/init_config.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="memory.html" title="内存管理"
accesskey="N">下一页</a> |</li>
<li class="right" >
<a href="init.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" accesskey="U">Python/C API 参考手册</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="python-initialization-configuration">
<span id="init-config"></span><h1>Python初始化配置<a class="headerlink" href="#python-initialization-configuration" title="永久链接至标题"></a></h1>
<div class="versionadded">
<p><span class="versionmodified added">3.8 新版功能.</span></p>
</div>
<p>结构</p>
<ul class="simple">
<li><p><a class="reference internal" href="#c.PyConfig" title="PyConfig"><code class="xref c c-type docutils literal notranslate"><span class="pre">PyConfig</span></code></a></p></li>
<li><p><a class="reference internal" href="#c.PyPreConfig" title="PyPreConfig"><code class="xref c c-type docutils literal notranslate"><span class="pre">PyPreConfig</span></code></a></p></li>
<li><p><a class="reference internal" href="#c.PyStatus" title="PyStatus"><code class="xref c c-type docutils literal notranslate"><span class="pre">PyStatus</span></code></a></p></li>
<li><p><a class="reference internal" href="#c.PyWideStringList" title="PyWideStringList"><code class="xref c c-type docutils literal notranslate"><span class="pre">PyWideStringList</span></code></a></p></li>
</ul>
<p>函数</p>
<ul class="simple">
<li><p><a class="reference internal" href="#c.PyConfig_Clear" title="PyConfig_Clear"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyConfig_Clear()</span></code></a></p></li>
<li><p><a class="reference internal" href="#c.PyConfig_InitIsolatedConfig" title="PyConfig_InitIsolatedConfig"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyConfig_InitIsolatedConfig()</span></code></a></p></li>
<li><p><a class="reference internal" href="#c.PyConfig_InitPythonConfig" title="PyConfig_InitPythonConfig"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyConfig_InitPythonConfig()</span></code></a></p></li>
<li><p><a class="reference internal" href="#c.PyConfig_Read" title="PyConfig_Read"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyConfig_Read()</span></code></a></p></li>
<li><p><a class="reference internal" href="#c.PyConfig_SetArgv" title="PyConfig_SetArgv"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyConfig_SetArgv()</span></code></a></p></li>
<li><p><a class="reference internal" href="#c.PyConfig_SetBytesArgv" title="PyConfig_SetBytesArgv"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyConfig_SetBytesArgv()</span></code></a></p></li>
<li><p><a class="reference internal" href="#c.PyConfig_SetBytesString" title="PyConfig_SetBytesString"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyConfig_SetBytesString()</span></code></a></p></li>
<li><p><a class="reference internal" href="#c.PyConfig_SetString" title="PyConfig_SetString"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyConfig_SetString()</span></code></a></p></li>
<li><p><a class="reference internal" href="#c.PyConfig_SetWideStringList" title="PyConfig_SetWideStringList"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyConfig_SetWideStringList()</span></code></a></p></li>
<li><p><a class="reference internal" href="#c.PyPreConfig_InitIsolatedConfig" title="PyPreConfig_InitIsolatedConfig"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyPreConfig_InitIsolatedConfig()</span></code></a></p></li>
<li><p><a class="reference internal" href="#c.PyPreConfig_InitPythonConfig" title="PyPreConfig_InitPythonConfig"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyPreConfig_InitPythonConfig()</span></code></a></p></li>
<li><p><a class="reference internal" href="#c.PyStatus_Error" title="PyStatus_Error"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyStatus_Error()</span></code></a></p></li>
<li><p><a class="reference internal" href="#c.PyStatus_Exception" title="PyStatus_Exception"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyStatus_Exception()</span></code></a></p></li>
<li><p><a class="reference internal" href="#c.PyStatus_Exit" title="PyStatus_Exit"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyStatus_Exit()</span></code></a></p></li>
<li><p><a class="reference internal" href="#c.PyStatus_IsError" title="PyStatus_IsError"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyStatus_IsError()</span></code></a></p></li>
<li><p><a class="reference internal" href="#c.PyStatus_IsExit" title="PyStatus_IsExit"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyStatus_IsExit()</span></code></a></p></li>
<li><p><a class="reference internal" href="#c.PyStatus_NoMemory" title="PyStatus_NoMemory"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyStatus_NoMemory()</span></code></a></p></li>
<li><p><a class="reference internal" href="#c.PyStatus_Ok" title="PyStatus_Ok"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyStatus_Ok()</span></code></a></p></li>
<li><p><a class="reference internal" href="#c.PyWideStringList_Append" title="PyWideStringList_Append"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyWideStringList_Append()</span></code></a></p></li>
<li><p><a class="reference internal" href="#c.PyWideStringList_Insert" title="PyWideStringList_Insert"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyWideStringList_Insert()</span></code></a></p></li>
<li><p><a class="reference internal" href="#c.Py_ExitStatusException" title="Py_ExitStatusException"><code class="xref c c-func docutils literal notranslate"><span class="pre">Py_ExitStatusException()</span></code></a></p></li>
<li><p><a class="reference internal" href="#c.Py_InitializeFromConfig" title="Py_InitializeFromConfig"><code class="xref c c-func docutils literal notranslate"><span class="pre">Py_InitializeFromConfig()</span></code></a></p></li>
<li><p><a class="reference internal" href="#c.Py_PreInitialize" title="Py_PreInitialize"><code class="xref c c-func docutils literal notranslate"><span class="pre">Py_PreInitialize()</span></code></a></p></li>
<li><p><a class="reference internal" href="#c.Py_PreInitializeFromArgs" title="Py_PreInitializeFromArgs"><code class="xref c c-func docutils literal notranslate"><span class="pre">Py_PreInitializeFromArgs()</span></code></a></p></li>
<li><p><a class="reference internal" href="#c.Py_PreInitializeFromBytesArgs" title="Py_PreInitializeFromBytesArgs"><code class="xref c c-func docutils literal notranslate"><span class="pre">Py_PreInitializeFromBytesArgs()</span></code></a></p></li>
<li><p><a class="reference internal" href="#c.Py_RunMain" title="Py_RunMain"><code class="xref c c-func docutils literal notranslate"><span class="pre">Py_RunMain()</span></code></a></p></li>
</ul>
<p>The preconfiguration (<code class="docutils literal notranslate"><span class="pre">PyPreConfig</span></code> type) is stored in
<code class="docutils literal notranslate"><span class="pre">_PyRuntime.preconfig</span></code> and the configuration (<code class="docutils literal notranslate"><span class="pre">PyConfig</span></code> type) is stored in
<code class="docutils literal notranslate"><span class="pre">PyInterpreterState.config</span></code>.</p>
<p>参见 <a class="reference internal" href="init.html#initialization"><span class="std std-ref">Initialization, Finalization, and Threads</span></a>.</p>
<div class="admonition seealso">
<p class="admonition-title">参见</p>
<p><span class="target" id="index-11"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0587"><strong>PEP 587</strong></a> &quot;Python 初始化配置&quot;.</p>
</div>
<section id="pywidestringlist">
<h2>PyWideStringList<a class="headerlink" href="#pywidestringlist" title="永久链接至标题"></a></h2>
<dl class="type">
<dt id="c.PyWideStringList">
<code class="sig-name descname">PyWideStringList</code><a class="headerlink" href="#c.PyWideStringList" title="永久链接至目标"></a></dt>
<dd><p><code class="docutils literal notranslate"><span class="pre">wchar_t*</span></code> 字符串组成的列表。</p>
<p>如果 <em>length</em> 为非零值,则 <em>items</em> 必须不为 <code class="docutils literal notranslate"><span class="pre">NULL</span></code> 并且所有字符串均必须不为 <code class="docutils literal notranslate"><span class="pre">NULL</span></code></p>
<p>方法</p>
<dl class="function">
<dt id="c.PyWideStringList_Append">
<a class="reference internal" href="#c.PyStatus" title="PyStatus">PyStatus</a> <code class="sig-name descname">PyWideStringList_Append</code><span class="sig-paren">(</span><a class="reference internal" href="#c.PyWideStringList" title="PyWideStringList">PyWideStringList</a><em> *list</em>, const wchar_t<em> *item</em><span class="sig-paren">)</span><a class="headerlink" href="#c.PyWideStringList_Append" title="永久链接至目标"></a></dt>
<dd><p><em>item</em> 添加到 <em>list</em></p>
<p>Python 必须被预初始化以便调用此函数。</p>
</dd></dl>
<dl class="function">
<dt id="c.PyWideStringList_Insert">
<a class="reference internal" href="#c.PyStatus" title="PyStatus">PyStatus</a> <code class="sig-name descname">PyWideStringList_Insert</code><span class="sig-paren">(</span><a class="reference internal" href="#c.PyWideStringList" title="PyWideStringList">PyWideStringList</a><em> *list</em>, Py_ssize_t<em> index</em>, const wchar_t<em> *item</em><span class="sig-paren">)</span><a class="headerlink" href="#c.PyWideStringList_Insert" title="永久链接至目标"></a></dt>
<dd><p><em>item</em> 插入到 <em>list</em><em>index</em> 位置上。</p>
<p>如果 <em>index</em> 大于等于 <em>list</em> 的长度,则将 <em>item</em> 添加到 <em>list</em></p>
<p><em>index</em> must be greater than or equal to 0.</p>
<p>Python 必须被预初始化以便调用此函数。</p>
</dd></dl>
<p>结构体字段:</p>
<dl class="member">
<dt id="c.PyWideStringList.length">
Py_ssize_t <code class="sig-name descname">length</code><a class="headerlink" href="#c.PyWideStringList.length" title="永久链接至目标"></a></dt>
<dd><p>List 长度。</p>
</dd></dl>
<dl class="member">
<dt id="c.PyWideStringList.items">
wchar_t** <code class="sig-name descname">items</code><a class="headerlink" href="#c.PyWideStringList.items" title="永久链接至目标"></a></dt>
<dd><p>列表项目。</p>
</dd></dl>
</dd></dl>
</section>
<section id="pystatus">
<h2>PyStatus<a class="headerlink" href="#pystatus" title="永久链接至标题"></a></h2>
<dl class="type">
<dt id="c.PyStatus">
<code class="sig-name descname">PyStatus</code><a class="headerlink" href="#c.PyStatus" title="永久链接至目标"></a></dt>
<dd><p>存储初始函数状态:成功、错误或退出的结构体。</p>
<p>对于错误,它可以存储造成错误的 C 函数的名称。</p>
<p>结构体字段:</p>
<dl class="member">
<dt id="c.PyStatus.exitcode">
int <code class="sig-name descname">exitcode</code><a class="headerlink" href="#c.PyStatus.exitcode" title="永久链接至目标"></a></dt>
<dd><p>退出码。 传给 <code class="docutils literal notranslate"><span class="pre">exit()</span></code> 的参数。</p>
</dd></dl>
<dl class="member">
<dt id="c.PyStatus.err_msg">
const char *<code class="sig-name descname">err_msg</code><a class="headerlink" href="#c.PyStatus.err_msg" title="永久链接至目标"></a></dt>
<dd><p>错误信息</p>
</dd></dl>
<dl class="member">
<dt id="c.PyStatus.func">
const char *<code class="sig-name descname">func</code><a class="headerlink" href="#c.PyStatus.func" title="永久链接至目标"></a></dt>
<dd><p>造成错误的函数的名称,可以为 <code class="docutils literal notranslate"><span class="pre">NULL</span></code></p>
</dd></dl>
<p>创建状态的函数:</p>
<dl class="function">
<dt id="c.PyStatus_Ok">
<a class="reference internal" href="#c.PyStatus" title="PyStatus">PyStatus</a> <code class="sig-name descname">PyStatus_Ok</code><span class="sig-paren">(</span>void<span class="sig-paren">)</span><a class="headerlink" href="#c.PyStatus_Ok" title="永久链接至目标"></a></dt>
<dd><p>完成。</p>
</dd></dl>
<dl class="function">
<dt id="c.PyStatus_Error">
<a class="reference internal" href="#c.PyStatus" title="PyStatus">PyStatus</a> <code class="sig-name descname">PyStatus_Error</code><span class="sig-paren">(</span>const char<em> *err_msg</em><span class="sig-paren">)</span><a class="headerlink" href="#c.PyStatus_Error" title="永久链接至目标"></a></dt>
<dd><p>带消息的初始化错误。</p>
</dd></dl>
<dl class="function">
<dt id="c.PyStatus_NoMemory">
<a class="reference internal" href="#c.PyStatus" title="PyStatus">PyStatus</a> <code class="sig-name descname">PyStatus_NoMemory</code><span class="sig-paren">(</span>void<span class="sig-paren">)</span><a class="headerlink" href="#c.PyStatus_NoMemory" title="永久链接至目标"></a></dt>
<dd><p>内存分配失败(内存不足)。</p>
</dd></dl>
<dl class="function">
<dt id="c.PyStatus_Exit">
<a class="reference internal" href="#c.PyStatus" title="PyStatus">PyStatus</a> <code class="sig-name descname">PyStatus_Exit</code><span class="sig-paren">(</span>int<em> exitcode</em><span class="sig-paren">)</span><a class="headerlink" href="#c.PyStatus_Exit" title="永久链接至目标"></a></dt>
<dd><p>以指定的退出代码退出 Python。</p>
</dd></dl>
<p>处理状态的函数:</p>
<dl class="function">
<dt id="c.PyStatus_Exception">
int <code class="sig-name descname">PyStatus_Exception</code><span class="sig-paren">(</span><a class="reference internal" href="#c.PyStatus" title="PyStatus">PyStatus</a><em> status</em><span class="sig-paren">)</span><a class="headerlink" href="#c.PyStatus_Exception" title="永久链接至目标"></a></dt>
<dd><p>状态为错误还是退出?如为真值,则异常必须被处理;例如通过调用 <a class="reference internal" href="#c.Py_ExitStatusException" title="Py_ExitStatusException"><code class="xref c c-func docutils literal notranslate"><span class="pre">Py_ExitStatusException()</span></code></a></p>
</dd></dl>
<dl class="function">
<dt id="c.PyStatus_IsError">
int <code class="sig-name descname">PyStatus_IsError</code><span class="sig-paren">(</span><a class="reference internal" href="#c.PyStatus" title="PyStatus">PyStatus</a><em> status</em><span class="sig-paren">)</span><a class="headerlink" href="#c.PyStatus_IsError" title="永久链接至目标"></a></dt>
<dd><p>结果错误吗?</p>
</dd></dl>
<dl class="function">
<dt id="c.PyStatus_IsExit">
int <code class="sig-name descname">PyStatus_IsExit</code><span class="sig-paren">(</span><a class="reference internal" href="#c.PyStatus" title="PyStatus">PyStatus</a><em> status</em><span class="sig-paren">)</span><a class="headerlink" href="#c.PyStatus_IsExit" title="永久链接至目标"></a></dt>
<dd><p>结果是否退出?</p>
</dd></dl>
<dl class="function">
<dt id="c.Py_ExitStatusException">
void <code class="sig-name descname">Py_ExitStatusException</code><span class="sig-paren">(</span><a class="reference internal" href="#c.PyStatus" title="PyStatus">PyStatus</a><em> status</em><span class="sig-paren">)</span><a class="headerlink" href="#c.Py_ExitStatusException" title="永久链接至目标"></a></dt>
<dd><p>如果 <em>status</em> 是一个退出码则调用 <code class="docutils literal notranslate"><span class="pre">exit(exitcode)</span></code>。如果 <em>status</em> 是一个错误码则打印错误消息并设置一个非零退出码再退出。 必须在 <code class="docutils literal notranslate"><span class="pre">PyStatus_Exception(status)</span></code> 为非零值时才能被调用。</p>
</dd></dl>
</dd></dl>
<div class="admonition note">
<p class="admonition-title">注解</p>
<p>在内部Python 将使用设置 <code class="docutils literal notranslate"><span class="pre">PyStatus.func</span></code> 的宏,而创建状态的函数则会将 <code class="docutils literal notranslate"><span class="pre">func</span></code> 设为 <code class="docutils literal notranslate"><span class="pre">NULL</span></code></p>
</div>
<p>示例:</p>
<div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="n">PyStatus</span><span class="w"> </span><span class="nf">alloc</span><span class="p">(</span><span class="kt">void</span><span class="w"> </span><span class="o">**</span><span class="n">ptr</span><span class="p">,</span><span class="w"> </span><span class="kt">size_t</span><span class="w"> </span><span class="n">size</span><span class="p">)</span>
<span class="p">{</span>
<span class="w"> </span><span class="o">*</span><span class="n">ptr</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">PyMem_RawMalloc</span><span class="p">(</span><span class="n">size</span><span class="p">);</span>
<span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="o">*</span><span class="n">ptr</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="nb">NULL</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="n">PyStatus_NoMemory</span><span class="p">();</span>
<span class="w"> </span><span class="p">}</span>
<span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="n">PyStatus_Ok</span><span class="p">();</span>
<span class="p">}</span>
<span class="kt">int</span><span class="w"> </span><span class="nf">main</span><span class="p">(</span><span class="kt">int</span><span class="w"> </span><span class="n">argc</span><span class="p">,</span><span class="w"> </span><span class="kt">char</span><span class="w"> </span><span class="o">**</span><span class="n">argv</span><span class="p">)</span>
<span class="p">{</span>
<span class="w"> </span><span class="kt">void</span><span class="w"> </span><span class="o">*</span><span class="n">ptr</span><span class="p">;</span>
<span class="w"> </span><span class="n">PyStatus</span><span class="w"> </span><span class="n">status</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">alloc</span><span class="p">(</span><span class="o">&amp;</span><span class="n">ptr</span><span class="p">,</span><span class="w"> </span><span class="mi">16</span><span class="p">);</span>
<span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">PyStatus_Exception</span><span class="p">(</span><span class="n">status</span><span class="p">))</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="n">Py_ExitStatusException</span><span class="p">(</span><span class="n">status</span><span class="p">);</span>
<span class="w"> </span><span class="p">}</span>
<span class="w"> </span><span class="n">PyMem_Free</span><span class="p">(</span><span class="n">ptr</span><span class="p">);</span>
<span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span>
<span class="p">}</span>
</pre></div>
</div>
</section>
<section id="pypreconfig">
<h2>PyPreConfig<a class="headerlink" href="#pypreconfig" title="永久链接至标题"></a></h2>
<dl class="type">
<dt id="c.PyPreConfig">
<code class="sig-name descname">PyPreConfig</code><a class="headerlink" href="#c.PyPreConfig" title="永久链接至目标"></a></dt>
<dd><p>Structure used to preinitialize Python:</p>
<ul class="simple">
<li><p>Set the Python memory allocator</p></li>
<li><p>Configure the LC_CTYPE locale</p></li>
<li><p>设置为 UTF-8 模式</p></li>
</ul>
<p>用于初始化预先配置的函数:</p>
<dl class="function">
<dt id="c.PyPreConfig_InitPythonConfig">
void <code class="sig-name descname">PyPreConfig_InitPythonConfig</code><span class="sig-paren">(</span><a class="reference internal" href="#c.PyPreConfig" title="PyPreConfig">PyPreConfig</a><em> *preconfig</em><span class="sig-paren">)</span><a class="headerlink" href="#c.PyPreConfig_InitPythonConfig" title="永久链接至目标"></a></dt>
<dd><p>通过 <a class="reference internal" href="#init-python-config"><span class="std std-ref">Python 配置</span></a> 来初始化预先配置。</p>
</dd></dl>
<dl class="function">
<dt id="c.PyPreConfig_InitIsolatedConfig">
void <code class="sig-name descname">PyPreConfig_InitIsolatedConfig</code><span class="sig-paren">(</span><a class="reference internal" href="#c.PyPreConfig" title="PyPreConfig">PyPreConfig</a><em> *preconfig</em><span class="sig-paren">)</span><a class="headerlink" href="#c.PyPreConfig_InitIsolatedConfig" title="永久链接至目标"></a></dt>
<dd><p>通过 <a class="reference internal" href="#init-isolated-conf"><span class="std std-ref">隔离配置</span></a> 来初始化预先配置。</p>
</dd></dl>
<p>结构体字段:</p>
<dl class="member">
<dt id="c.PyPreConfig.allocator">
int <code class="sig-name descname">allocator</code><a class="headerlink" href="#c.PyPreConfig.allocator" title="永久链接至目标"></a></dt>
<dd><p>Name of the memory allocator:</p>
<ul class="simple">
<li><p><code class="docutils literal notranslate"><span class="pre">PYMEM_ALLOCATOR_NOT_SET</span></code> (<code class="docutils literal notranslate"><span class="pre">0</span></code>): don't change memory allocators
(use defaults)</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">PYMEM_ALLOCATOR_DEFAULT</span></code> (<code class="docutils literal notranslate"><span class="pre">1</span></code>): default memory allocators</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">PYMEM_ALLOCATOR_DEBUG</span></code> (<code class="docutils literal notranslate"><span class="pre">2</span></code>): default memory allocators with
debug hooks</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">PYMEM_ALLOCATOR_MALLOC</span></code> (<code class="docutils literal notranslate"><span class="pre">3</span></code>): force usage of <code class="docutils literal notranslate"><span class="pre">malloc()</span></code></p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">PYMEM_ALLOCATOR_MALLOC_DEBUG</span></code> (<code class="docutils literal notranslate"><span class="pre">4</span></code>): force usage of
<code class="docutils literal notranslate"><span class="pre">malloc()</span></code> with debug hooks</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">PYMEM_ALLOCATOR_PYMALLOC</span></code> (<code class="docutils literal notranslate"><span class="pre">5</span></code>): <a class="reference internal" href="memory.html#pymalloc"><span class="std std-ref">Python pymalloc memory
allocator</span></a></p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">PYMEM_ALLOCATOR_PYMALLOC_DEBUG</span></code> (<code class="docutils literal notranslate"><span class="pre">6</span></code>): <a class="reference internal" href="memory.html#pymalloc"><span class="std std-ref">Python pymalloc
memory allocator</span></a> with debug hooks</p></li>
</ul>
<p><code class="docutils literal notranslate"><span class="pre">PYMEM_ALLOCATOR_PYMALLOC</span></code> and <code class="docutils literal notranslate"><span class="pre">PYMEM_ALLOCATOR_PYMALLOC_DEBUG</span></code>
are not supported if Python is configured using <code class="docutils literal notranslate"><span class="pre">--without-pymalloc</span></code></p>
<p>参见 <a class="reference internal" href="memory.html#memory"><span class="std std-ref">Memory Management</span></a>.</p>
</dd></dl>
<dl class="member">
<dt id="c.PyPreConfig.configure_locale">
int <code class="sig-name descname">configure_locale</code><a class="headerlink" href="#c.PyPreConfig.configure_locale" title="永久链接至目标"></a></dt>
<dd><p>Set the LC_CTYPE locale to the user preferred locale? If equals to 0, set
<code class="xref c c-member docutils literal notranslate"><span class="pre">coerce_c_locale</span></code> and <code class="xref c c-member docutils literal notranslate"><span class="pre">coerce_c_locale_warn</span></code> to 0.</p>
</dd></dl>
<dl class="member">
<dt id="c.PyPreConfig.coerce_c_locale">
int <code class="sig-name descname">coerce_c_locale</code><a class="headerlink" href="#c.PyPreConfig.coerce_c_locale" title="永久链接至目标"></a></dt>
<dd><p>If equals to 2, coerce the C locale; if equals to 1, read the LC_CTYPE
locale to decide if it should be coerced.</p>
</dd></dl>
<dl class="member">
<dt id="c.PyPreConfig.coerce_c_locale_warn">
int <code class="sig-name descname">coerce_c_locale_warn</code><a class="headerlink" href="#c.PyPreConfig.coerce_c_locale_warn" title="永久链接至目标"></a></dt>
<dd><p>如为非零值,则会在 C 语言区域被强制转换时发出警告。</p>
</dd></dl>
<dl class="member">
<dt id="c.PyPreConfig.dev_mode">
int <code class="sig-name descname">dev_mode</code><a class="headerlink" href="#c.PyPreConfig.dev_mode" title="永久链接至目标"></a></dt>
<dd><p>参见 <a class="reference internal" href="#c.PyConfig.dev_mode" title="PyConfig.dev_mode"><code class="xref c c-member docutils literal notranslate"><span class="pre">PyConfig.dev_mode</span></code></a>.</p>
</dd></dl>
<dl class="member">
<dt id="c.PyPreConfig.isolated">
int <code class="sig-name descname">isolated</code><a class="headerlink" href="#c.PyPreConfig.isolated" title="永久链接至目标"></a></dt>
<dd><p>参见 <a class="reference internal" href="#c.PyConfig.isolated" title="PyConfig.isolated"><code class="xref c c-member docutils literal notranslate"><span class="pre">PyConfig.isolated</span></code></a>.</p>
</dd></dl>
<dl class="member">
<dt id="c.PyPreConfig.legacy_windows_fs_encoding">
int <code class="sig-name descname">legacy_windows_fs_encoding</code><span class="sig-paren">(</span>Windows<em> only</em><span class="sig-paren">)</span><a class="headerlink" href="#c.PyPreConfig.legacy_windows_fs_encoding" title="永久链接至目标"></a></dt>
<dd><p>If non-zero, disable UTF-8 Mode, set the Python filesystem encoding to
<code class="docutils literal notranslate"><span class="pre">mbcs</span></code>, set the filesystem error handler to <code class="docutils literal notranslate"><span class="pre">replace</span></code>.</p>
<p>仅在 Windows 上可用。 <code class="docutils literal notranslate"><span class="pre">#ifdef</span> <span class="pre">MS_WINDOWS</span></code> 宏可被用于 Windows 专属的代码。</p>
</dd></dl>
<dl class="member">
<dt id="c.PyPreConfig.parse_argv">
int <code class="sig-name descname">parse_argv</code><a class="headerlink" href="#c.PyPreConfig.parse_argv" title="永久链接至目标"></a></dt>
<dd><p>如为非零值,<a class="reference internal" href="#c.Py_PreInitializeFromArgs" title="Py_PreInitializeFromArgs"><code class="xref c c-func docutils literal notranslate"><span class="pre">Py_PreInitializeFromArgs()</span></code></a><a class="reference internal" href="#c.Py_PreInitializeFromBytesArgs" title="Py_PreInitializeFromBytesArgs"><code class="xref c c-func docutils literal notranslate"><span class="pre">Py_PreInitializeFromBytesArgs()</span></code></a> 将以与常规 Python 解析命令行参数的相同方式解析其 <code class="docutils literal notranslate"><span class="pre">argv</span></code> 参数:参见 <a class="reference internal" href="../using/cmdline.html#using-on-cmdline"><span class="std std-ref">命令行参数</span></a></p>
</dd></dl>
<dl class="member">
<dt id="c.PyPreConfig.use_environment">
int <code class="sig-name descname">use_environment</code><a class="headerlink" href="#c.PyPreConfig.use_environment" title="永久链接至目标"></a></dt>
<dd><p>参见 <a class="reference internal" href="#c.PyConfig.use_environment" title="PyConfig.use_environment"><code class="xref c c-member docutils literal notranslate"><span class="pre">PyConfig.use_environment</span></code></a>.</p>
</dd></dl>
<dl class="member">
<dt id="c.PyPreConfig.utf8_mode">
int <code class="sig-name descname">utf8_mode</code><a class="headerlink" href="#c.PyPreConfig.utf8_mode" title="永久链接至目标"></a></dt>
<dd><p>If non-zero, enable the UTF-8 mode.</p>
</dd></dl>
</dd></dl>
</section>
<section id="preinitialization-with-pypreconfig">
<h2>Preinitialization with PyPreConfig<a class="headerlink" href="#preinitialization-with-pypreconfig" title="永久链接至标题"></a></h2>
<p>用于预初始化 Python 的函数:</p>
<dl class="function">
<dt id="c.Py_PreInitialize">
<a class="reference internal" href="#c.PyStatus" title="PyStatus">PyStatus</a> <code class="sig-name descname">Py_PreInitialize</code><span class="sig-paren">(</span>const <a class="reference internal" href="#c.PyPreConfig" title="PyPreConfig">PyPreConfig</a><em> *preconfig</em><span class="sig-paren">)</span><a class="headerlink" href="#c.Py_PreInitialize" title="永久链接至目标"></a></dt>
<dd><p>根据 <em>preconfig</em> 预配置来预初始化 Python。</p>
</dd></dl>
<dl class="function">
<dt id="c.Py_PreInitializeFromBytesArgs">
<a class="reference internal" href="#c.PyStatus" title="PyStatus">PyStatus</a> <code class="sig-name descname">Py_PreInitializeFromBytesArgs</code><span class="sig-paren">(</span>const <a class="reference internal" href="#c.PyPreConfig" title="PyPreConfig">PyPreConfig</a><em> *preconfig</em>, int<em> argc</em>, char * const<em> *argv</em><span class="sig-paren">)</span><a class="headerlink" href="#c.Py_PreInitializeFromBytesArgs" title="永久链接至目标"></a></dt>
<dd><p>Preinitialize Python from <em>preconfig</em> preconfiguration and command line
arguments (bytes strings).</p>
</dd></dl>
<dl class="function">
<dt id="c.Py_PreInitializeFromArgs">
<a class="reference internal" href="#c.PyStatus" title="PyStatus">PyStatus</a> <code class="sig-name descname">Py_PreInitializeFromArgs</code><span class="sig-paren">(</span>const <a class="reference internal" href="#c.PyPreConfig" title="PyPreConfig">PyPreConfig</a><em> *preconfig</em>, int<em> argc</em>, wchar_t * const *<em> argv</em><span class="sig-paren">)</span><a class="headerlink" href="#c.Py_PreInitializeFromArgs" title="永久链接至目标"></a></dt>
<dd><p>Preinitialize Python from <em>preconfig</em> preconfiguration and command line
arguments (wide strings).</p>
</dd></dl>
<p>调用方要负责使用 <a class="reference internal" href="#c.PyStatus_Exception" title="PyStatus_Exception"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyStatus_Exception()</span></code></a><a class="reference internal" href="#c.Py_ExitStatusException" title="Py_ExitStatusException"><code class="xref c c-func docutils literal notranslate"><span class="pre">Py_ExitStatusException()</span></code></a> 来处理异常(错误或退出)。</p>
<p>For <a class="reference internal" href="#init-python-config"><span class="std std-ref">Python Configuration</span></a>
(<a class="reference internal" href="#c.PyPreConfig_InitPythonConfig" title="PyPreConfig_InitPythonConfig"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyPreConfig_InitPythonConfig()</span></code></a>), if Python is initialized with
command line arguments, the command line arguments must also be passed to
preinitialize Python, since they have an effect on the pre-configuration
like encodings. For example, the <a class="reference internal" href="../using/cmdline.html#id5"><code class="xref std std-option docutils literal notranslate"><span class="pre">-X</span> <span class="pre">utf8</span></code></a> command line option
enables the UTF-8 Mode.</p>
<p><code class="docutils literal notranslate"><span class="pre">PyMem_SetAllocator()</span></code> 可在 <a class="reference internal" href="#c.Py_PreInitialize" title="Py_PreInitialize"><code class="xref c c-func docutils literal notranslate"><span class="pre">Py_PreInitialize()</span></code></a> 之后、<a class="reference internal" href="#c.Py_InitializeFromConfig" title="Py_InitializeFromConfig"><code class="xref c c-func docutils literal notranslate"><span class="pre">Py_InitializeFromConfig()</span></code></a> 之前被调用以安装自定义的内存分配器。 如果 <a class="reference internal" href="#c.PyPreConfig.allocator" title="PyPreConfig.allocator"><code class="xref c c-member docutils literal notranslate"><span class="pre">PyPreConfig.allocator</span></code></a> 被设为 <code class="docutils literal notranslate"><span class="pre">PYMEM_ALLOCATOR_NOT_SET</span></code> 则可在 <a class="reference internal" href="#c.Py_PreInitialize" title="Py_PreInitialize"><code class="xref c c-func docutils literal notranslate"><span class="pre">Py_PreInitialize()</span></code></a> 之前被调用。</p>
<p>Python memory allocation functions like <a class="reference internal" href="memory.html#c.PyMem_RawMalloc" title="PyMem_RawMalloc"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyMem_RawMalloc()</span></code></a> must not be
used before Python preinitialization, whereas calling directly <code class="docutils literal notranslate"><span class="pre">malloc()</span></code> and
<code class="docutils literal notranslate"><span class="pre">free()</span></code> is always safe. <a class="reference internal" href="sys.html#c.Py_DecodeLocale" title="Py_DecodeLocale"><code class="xref c c-func docutils literal notranslate"><span class="pre">Py_DecodeLocale()</span></code></a> must not be called before
the preinitialization.</p>
<p>Example using the preinitialization to enable the UTF-8 Mode:</p>
<div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="n">PyStatus</span><span class="w"> </span><span class="n">status</span><span class="p">;</span>
<span class="n">PyPreConfig</span><span class="w"> </span><span class="n">preconfig</span><span class="p">;</span>
<span class="n">PyPreConfig_InitPythonConfig</span><span class="p">(</span><span class="o">&amp;</span><span class="n">preconfig</span><span class="p">);</span>
<span class="n">preconfig</span><span class="p">.</span><span class="n">utf8_mode</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">1</span><span class="p">;</span>
<span class="n">status</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">Py_PreInitialize</span><span class="p">(</span><span class="o">&amp;</span><span class="n">preconfig</span><span class="p">);</span>
<span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">PyStatus_Exception</span><span class="p">(</span><span class="n">status</span><span class="p">))</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="n">Py_ExitStatusException</span><span class="p">(</span><span class="n">status</span><span class="p">);</span>
<span class="p">}</span>
<span class="cm">/* at this point, Python will speak UTF-8 */</span>
<span class="n">Py_Initialize</span><span class="p">();</span>
<span class="cm">/* ... use Python API here ... */</span>
<span class="n">Py_Finalize</span><span class="p">();</span>
</pre></div>
</div>
</section>
<section id="pyconfig">
<h2>PyConfig<a class="headerlink" href="#pyconfig" title="永久链接至标题"></a></h2>
<dl class="type">
<dt id="c.PyConfig">
<code class="sig-name descname">PyConfig</code><a class="headerlink" href="#c.PyConfig" title="永久链接至目标"></a></dt>
<dd><p>包含了大部分用于配置 Python 的形参的结构体。</p>
<p>结构体方法:</p>
<dl class="function">
<dt id="c.PyConfig_InitPythonConfig">
void <code class="sig-name descname">PyConfig_InitPythonConfig</code><span class="sig-paren">(</span><a class="reference internal" href="#c.PyConfig" title="PyConfig">PyConfig</a><em> *config</em><span class="sig-paren">)</span><a class="headerlink" href="#c.PyConfig_InitPythonConfig" title="永久链接至目标"></a></dt>
<dd><p>Initialize configuration with <a class="reference internal" href="#init-python-config"><span class="std std-ref">Python Configuration</span></a>.</p>
</dd></dl>
<dl class="function">
<dt id="c.PyConfig_InitIsolatedConfig">
void <code class="sig-name descname">PyConfig_InitIsolatedConfig</code><span class="sig-paren">(</span><a class="reference internal" href="#c.PyConfig" title="PyConfig">PyConfig</a><em> *config</em><span class="sig-paren">)</span><a class="headerlink" href="#c.PyConfig_InitIsolatedConfig" title="永久链接至目标"></a></dt>
<dd><p>Initialize configuration with <a class="reference internal" href="#init-isolated-conf"><span class="std std-ref">Isolated Configuration</span></a>.</p>
</dd></dl>
<dl class="function">
<dt id="c.PyConfig_SetString">
<a class="reference internal" href="#c.PyStatus" title="PyStatus">PyStatus</a> <code class="sig-name descname">PyConfig_SetString</code><span class="sig-paren">(</span><a class="reference internal" href="#c.PyConfig" title="PyConfig">PyConfig</a><em> *config</em>, wchar_t * const<em> *config_str</em>, const wchar_t<em> *str</em><span class="sig-paren">)</span><a class="headerlink" href="#c.PyConfig_SetString" title="永久链接至目标"></a></dt>
<dd><p>将宽字符串 <em>str</em> 拷贝至 <code class="docutils literal notranslate"><span class="pre">*config_str</span></code></p>
<p>Preinitialize Python if needed.</p>
</dd></dl>
<dl class="function">
<dt id="c.PyConfig_SetBytesString">
<a class="reference internal" href="#c.PyStatus" title="PyStatus">PyStatus</a> <code class="sig-name descname">PyConfig_SetBytesString</code><span class="sig-paren">(</span><a class="reference internal" href="#c.PyConfig" title="PyConfig">PyConfig</a><em> *config</em>, wchar_t * const<em> *config_str</em>, const char<em> *str</em><span class="sig-paren">)</span><a class="headerlink" href="#c.PyConfig_SetBytesString" title="永久链接至目标"></a></dt>
<dd><p>Decode <em>str</em> using <code class="docutils literal notranslate"><span class="pre">Py_DecodeLocale()</span></code> and set the result into <code class="docutils literal notranslate"><span class="pre">*config_str</span></code>.</p>
<p>Preinitialize Python if needed.</p>
</dd></dl>
<dl class="function">
<dt id="c.PyConfig_SetArgv">
<a class="reference internal" href="#c.PyStatus" title="PyStatus">PyStatus</a> <code class="sig-name descname">PyConfig_SetArgv</code><span class="sig-paren">(</span><a class="reference internal" href="#c.PyConfig" title="PyConfig">PyConfig</a><em> *config</em>, int<em> argc</em>, wchar_t * const<em> *argv</em><span class="sig-paren">)</span><a class="headerlink" href="#c.PyConfig_SetArgv" title="永久链接至目标"></a></dt>
<dd><p>Set command line arguments from wide character strings.</p>
<p>Preinitialize Python if needed.</p>
</dd></dl>
<dl class="function">
<dt id="c.PyConfig_SetBytesArgv">
<a class="reference internal" href="#c.PyStatus" title="PyStatus">PyStatus</a> <code class="sig-name descname">PyConfig_SetBytesArgv</code><span class="sig-paren">(</span><a class="reference internal" href="#c.PyConfig" title="PyConfig">PyConfig</a><em> *config</em>, int<em> argc</em>, char * const<em> *argv</em><span class="sig-paren">)</span><a class="headerlink" href="#c.PyConfig_SetBytesArgv" title="永久链接至目标"></a></dt>
<dd><p>Set command line arguments: decode bytes using <a class="reference internal" href="sys.html#c.Py_DecodeLocale" title="Py_DecodeLocale"><code class="xref c c-func docutils literal notranslate"><span class="pre">Py_DecodeLocale()</span></code></a>.</p>
<p>Preinitialize Python if needed.</p>
</dd></dl>
<dl class="function">
<dt id="c.PyConfig_SetWideStringList">
<a class="reference internal" href="#c.PyStatus" title="PyStatus">PyStatus</a> <code class="sig-name descname">PyConfig_SetWideStringList</code><span class="sig-paren">(</span><a class="reference internal" href="#c.PyConfig" title="PyConfig">PyConfig</a><em> *config</em>, <a class="reference internal" href="#c.PyWideStringList" title="PyWideStringList">PyWideStringList</a><em> *list</em>, Py_ssize_t<em> length</em>, wchar_t<em> **items</em><span class="sig-paren">)</span><a class="headerlink" href="#c.PyConfig_SetWideStringList" title="永久链接至目标"></a></dt>
<dd><p>将宽字符串列表 <em>list</em> 设置为 <em>length</em><em>items</em></p>
<p>Preinitialize Python if needed.</p>
</dd></dl>
<dl class="function">
<dt id="c.PyConfig_Read">
<a class="reference internal" href="#c.PyStatus" title="PyStatus">PyStatus</a> <code class="sig-name descname">PyConfig_Read</code><span class="sig-paren">(</span><a class="reference internal" href="#c.PyConfig" title="PyConfig">PyConfig</a><em> *config</em><span class="sig-paren">)</span><a class="headerlink" href="#c.PyConfig_Read" title="永久链接至目标"></a></dt>
<dd><p>读取所有 Python 配置。</p>
<p>已经初始化的字段会保持不变。</p>
<p>Preinitialize Python if needed.</p>
</dd></dl>
<dl class="function">
<dt id="c.PyConfig_Clear">
void <code class="sig-name descname">PyConfig_Clear</code><span class="sig-paren">(</span><a class="reference internal" href="#c.PyConfig" title="PyConfig">PyConfig</a><em> *config</em><span class="sig-paren">)</span><a class="headerlink" href="#c.PyConfig_Clear" title="永久链接至目标"></a></dt>
<dd><p>释放配置内存</p>
</dd></dl>
<p>Most <code class="docutils literal notranslate"><span class="pre">PyConfig</span></code> methods preinitialize Python if needed. In that case, the
Python preinitialization configuration in based on the <a class="reference internal" href="#c.PyConfig" title="PyConfig"><code class="xref c c-type docutils literal notranslate"><span class="pre">PyConfig</span></code></a>.
If configuration fields which are in common with <a class="reference internal" href="#c.PyPreConfig" title="PyPreConfig"><code class="xref c c-type docutils literal notranslate"><span class="pre">PyPreConfig</span></code></a> are
tuned, they must be set before calling a <a class="reference internal" href="#c.PyConfig" title="PyConfig"><code class="xref c c-type docutils literal notranslate"><span class="pre">PyConfig</span></code></a> method:</p>
<ul class="simple">
<li><p><a class="reference internal" href="#c.PyConfig.dev_mode" title="PyConfig.dev_mode"><code class="xref c c-member docutils literal notranslate"><span class="pre">dev_mode</span></code></a></p></li>
<li><p><a class="reference internal" href="#c.PyConfig.isolated" title="PyConfig.isolated"><code class="xref c c-member docutils literal notranslate"><span class="pre">isolated</span></code></a></p></li>
<li><p><a class="reference internal" href="#c.PyConfig.parse_argv" title="PyConfig.parse_argv"><code class="xref c c-member docutils literal notranslate"><span class="pre">parse_argv</span></code></a></p></li>
<li><p><a class="reference internal" href="#c.PyConfig.use_environment" title="PyConfig.use_environment"><code class="xref c c-member docutils literal notranslate"><span class="pre">use_environment</span></code></a></p></li>
</ul>
<p>Moreover, if <a class="reference internal" href="#c.PyConfig_SetArgv" title="PyConfig_SetArgv"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyConfig_SetArgv()</span></code></a> or <a class="reference internal" href="#c.PyConfig_SetBytesArgv" title="PyConfig_SetBytesArgv"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyConfig_SetBytesArgv()</span></code></a>
is used, this method must be called first, before other methods, since the
preinitialization configuration depends on command line arguments (if
<code class="xref c c-member docutils literal notranslate"><span class="pre">parse_argv</span></code> is non-zero).</p>
<p>这些方法的调用者要负责使用 <code class="docutils literal notranslate"><span class="pre">PyStatus_Exception()</span></code><code class="docutils literal notranslate"><span class="pre">Py_ExitStatusException()</span></code> 来处理异常(错误或退出)。</p>
<p>结构体字段:</p>
<dl class="member">
<dt id="c.PyConfig.argv">
<a class="reference internal" href="#c.PyWideStringList" title="PyWideStringList">PyWideStringList</a> <code class="sig-name descname">argv</code><a class="headerlink" href="#c.PyConfig.argv" title="永久链接至目标"></a></dt>
<dd><p>Command line arguments, <a class="reference internal" href="../library/sys.html#sys.argv" title="sys.argv"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.argv</span></code></a>. See
<a class="reference internal" href="#c.PyConfig.parse_argv" title="PyConfig.parse_argv"><code class="xref c c-member docutils literal notranslate"><span class="pre">parse_argv</span></code></a> to parse <a class="reference internal" href="#c.PyConfig.argv" title="PyConfig.argv"><code class="xref c c-member docutils literal notranslate"><span class="pre">argv</span></code></a> the
same way the regular Python parses Python command line arguments. If
<a class="reference internal" href="#c.PyConfig.argv" title="PyConfig.argv"><code class="xref c c-member docutils literal notranslate"><span class="pre">argv</span></code></a> is empty, an empty string is added to ensure
that <a class="reference internal" href="../library/sys.html#sys.argv" title="sys.argv"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.argv</span></code></a> always exists and is never empty.</p>
</dd></dl>
<dl class="member">
<dt id="c.PyConfig.base_exec_prefix">
wchar_t* <code class="sig-name descname">base_exec_prefix</code><a class="headerlink" href="#c.PyConfig.base_exec_prefix" title="永久链接至目标"></a></dt>
<dd><p><a class="reference internal" href="../library/sys.html#sys.base_exec_prefix" title="sys.base_exec_prefix"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.base_exec_prefix</span></code></a>.</p>
</dd></dl>
<dl class="member">
<dt id="c.PyConfig.base_executable">
wchar_t* <code class="sig-name descname">base_executable</code><a class="headerlink" href="#c.PyConfig.base_executable" title="永久链接至目标"></a></dt>
<dd><p><code class="xref py py-data docutils literal notranslate"><span class="pre">sys._base_executable</span></code>: <code class="docutils literal notranslate"><span class="pre">__PYVENV_LAUNCHER__</span></code> environment
variable value, or copy of <a class="reference internal" href="#c.PyConfig.executable" title="PyConfig.executable"><code class="xref c c-member docutils literal notranslate"><span class="pre">PyConfig.executable</span></code></a>.</p>
</dd></dl>
<dl class="member">
<dt id="c.PyConfig.base_prefix">
wchar_t* <code class="sig-name descname">base_prefix</code><a class="headerlink" href="#c.PyConfig.base_prefix" title="永久链接至目标"></a></dt>
<dd><p><a class="reference internal" href="../library/sys.html#sys.base_prefix" title="sys.base_prefix"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.base_prefix</span></code></a>.</p>
</dd></dl>
<dl class="member">
<dt id="c.PyConfig.buffered_stdio">
int <code class="sig-name descname">buffered_stdio</code><a class="headerlink" href="#c.PyConfig.buffered_stdio" title="永久链接至目标"></a></dt>
<dd><p>If equals to 0, enable unbuffered mode, making the stdout and stderr
streams unbuffered.</p>
<p>stdin 始终以缓冲模式打开。</p>
</dd></dl>
<dl class="member">
<dt id="c.PyConfig.bytes_warning">
int <code class="sig-name descname">bytes_warning</code><a class="headerlink" href="#c.PyConfig.bytes_warning" title="永久链接至目标"></a></dt>
<dd><p>If equals to 1, issue a warning when comparing <a class="reference internal" href="../library/stdtypes.html#bytes" title="bytes"><code class="xref py py-class docutils literal notranslate"><span class="pre">bytes</span></code></a> or
<a class="reference internal" href="../library/stdtypes.html#bytearray" title="bytearray"><code class="xref py py-class docutils literal notranslate"><span class="pre">bytearray</span></code></a> with <a class="reference internal" href="../library/stdtypes.html#str" title="str"><code class="xref py py-class docutils literal notranslate"><span class="pre">str</span></code></a>, or comparing <a class="reference internal" href="../library/stdtypes.html#bytes" title="bytes"><code class="xref py py-class docutils literal notranslate"><span class="pre">bytes</span></code></a> with
<a class="reference internal" href="../library/functions.html#int" title="int"><code class="xref py py-class docutils literal notranslate"><span class="pre">int</span></code></a>. If equal or greater to 2, raise a <a class="reference internal" href="../library/exceptions.html#BytesWarning" title="BytesWarning"><code class="xref py py-exc docutils literal notranslate"><span class="pre">BytesWarning</span></code></a>
exception.</p>
</dd></dl>
<dl class="member">
<dt id="c.PyConfig.check_hash_pycs_mode">
wchar_t* <code class="sig-name descname">check_hash_pycs_mode</code><a class="headerlink" href="#c.PyConfig.check_hash_pycs_mode" title="永久链接至目标"></a></dt>
<dd><p>Control the validation behavior of hash-based <code class="docutils literal notranslate"><span class="pre">.pyc</span></code> files (see
<span class="target" id="index-1"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0552"><strong>PEP 552</strong></a>): <a class="reference internal" href="../using/cmdline.html#cmdoption-check-hash-based-pycs"><code class="xref std std-option docutils literal notranslate"><span class="pre">--check-hash-based-pycs</span></code></a> command line option value.</p>
<p>Valid values: <code class="docutils literal notranslate"><span class="pre">always</span></code>, <code class="docutils literal notranslate"><span class="pre">never</span></code> and <code class="docutils literal notranslate"><span class="pre">default</span></code>.</p>
<p>默认值为: <code class="docutils literal notranslate"><span class="pre">default</span></code>.</p>
</dd></dl>
<dl class="member">
<dt id="c.PyConfig.configure_c_stdio">
int <code class="sig-name descname">configure_c_stdio</code><a class="headerlink" href="#c.PyConfig.configure_c_stdio" title="永久链接至目标"></a></dt>
<dd><p>If non-zero, configure C standard streams (<code class="docutils literal notranslate"><span class="pre">stdio</span></code>, <code class="docutils literal notranslate"><span class="pre">stdout</span></code>,
<code class="docutils literal notranslate"><span class="pre">stdout</span></code>). For example, set their mode to <code class="docutils literal notranslate"><span class="pre">O_BINARY</span></code> on Windows.</p>
</dd></dl>
<dl class="member">
<dt id="c.PyConfig.dev_mode">
int <code class="sig-name descname">dev_mode</code><a class="headerlink" href="#c.PyConfig.dev_mode" title="永久链接至目标"></a></dt>
<dd><p>Development mode: see <a class="reference internal" href="../using/cmdline.html#id5"><code class="xref std std-option docutils literal notranslate"><span class="pre">-X</span> <span class="pre">dev</span></code></a>.</p>
</dd></dl>
<dl class="member">
<dt id="c.PyConfig.dump_refs">
int <code class="sig-name descname">dump_refs</code><a class="headerlink" href="#c.PyConfig.dump_refs" title="永久链接至目标"></a></dt>
<dd><p>如果为非零值,则转储所有在退出时仍存活的对象。</p>
<p>Require a debug build of Python (<code class="docutils literal notranslate"><span class="pre">Py_REF_DEBUG</span></code> macro must be defined).</p>
</dd></dl>
<dl class="member">
<dt id="c.PyConfig.exec_prefix">
wchar_t* <code class="sig-name descname">exec_prefix</code><a class="headerlink" href="#c.PyConfig.exec_prefix" title="永久链接至目标"></a></dt>
<dd><p><a class="reference internal" href="../library/sys.html#sys.exec_prefix" title="sys.exec_prefix"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.exec_prefix</span></code></a>.</p>
</dd></dl>
<dl class="member">
<dt id="c.PyConfig.executable">
wchar_t* <code class="sig-name descname">executable</code><a class="headerlink" href="#c.PyConfig.executable" title="永久链接至目标"></a></dt>
<dd><p><a class="reference internal" href="../library/sys.html#sys.executable" title="sys.executable"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.executable</span></code></a>.</p>
</dd></dl>
<dl class="member">
<dt id="c.PyConfig.faulthandler">
int <code class="sig-name descname">faulthandler</code><a class="headerlink" href="#c.PyConfig.faulthandler" title="永久链接至目标"></a></dt>
<dd><p>如果为非零值,则在启动时调用 <a class="reference internal" href="../library/faulthandler.html#faulthandler.enable" title="faulthandler.enable"><code class="xref py py-func docutils literal notranslate"><span class="pre">faulthandler.enable()</span></code></a></p>
</dd></dl>
<dl class="member">
<dt id="c.PyConfig.filesystem_encoding">
wchar_t* <code class="sig-name descname">filesystem_encoding</code><a class="headerlink" href="#c.PyConfig.filesystem_encoding" title="永久链接至目标"></a></dt>
<dd><p>Filesystem encoding, <a class="reference internal" href="../library/sys.html#sys.getfilesystemencoding" title="sys.getfilesystemencoding"><code class="xref py py-func docutils literal notranslate"><span class="pre">sys.getfilesystemencoding()</span></code></a>.</p>
</dd></dl>
<dl class="member">
<dt id="c.PyConfig.filesystem_errors">
wchar_t* <code class="sig-name descname">filesystem_errors</code><a class="headerlink" href="#c.PyConfig.filesystem_errors" title="永久链接至目标"></a></dt>
<dd><p>Filesystem encoding errors, <a class="reference internal" href="../library/sys.html#sys.getfilesystemencodeerrors" title="sys.getfilesystemencodeerrors"><code class="xref py py-func docutils literal notranslate"><span class="pre">sys.getfilesystemencodeerrors()</span></code></a>.</p>
</dd></dl>
<dl class="member">
<dt id="c.PyConfig.hash_seed">
unsigned long <code class="sig-name descname">hash_seed</code><a class="headerlink" href="#c.PyConfig.hash_seed" title="永久链接至目标"></a></dt>
<dd></dd></dl>
<dl class="member">
<dt id="c.PyConfig.use_hash_seed">
int <code class="sig-name descname">use_hash_seed</code><a class="headerlink" href="#c.PyConfig.use_hash_seed" title="永久链接至目标"></a></dt>
<dd><p>随机化的哈希函数种子。</p>
<p>If <a class="reference internal" href="#c.PyConfig.use_hash_seed" title="PyConfig.use_hash_seed"><code class="xref c c-member docutils literal notranslate"><span class="pre">use_hash_seed</span></code></a> is zero, a seed is chosen randomly
at Pythonstartup, and <a class="reference internal" href="#c.PyConfig.hash_seed" title="PyConfig.hash_seed"><code class="xref c c-member docutils literal notranslate"><span class="pre">hash_seed</span></code></a> is ignored.</p>
</dd></dl>
<dl class="member">
<dt id="c.PyConfig.home">
wchar_t* <code class="sig-name descname">home</code><a class="headerlink" href="#c.PyConfig.home" title="永久链接至目标"></a></dt>
<dd><p>Python 主目录。</p>
<p>Initialized from <span class="target" id="index-2"></span><a class="reference internal" href="../using/cmdline.html#envvar-PYTHONHOME"><code class="xref std std-envvar docutils literal notranslate"><span class="pre">PYTHONHOME</span></code></a> environment variable value by
default.</p>
</dd></dl>
<dl class="member">
<dt id="c.PyConfig.import_time">
int <code class="sig-name descname">import_time</code><a class="headerlink" href="#c.PyConfig.import_time" title="永久链接至目标"></a></dt>
<dd><p>如为非零值,则对导入时间执行性能分析。</p>
</dd></dl>
<dl class="member">
<dt id="c.PyConfig.inspect">
int <code class="sig-name descname">inspect</code><a class="headerlink" href="#c.PyConfig.inspect" title="永久链接至目标"></a></dt>
<dd><p>在执行脚本或命令之后进入交互模式。</p>
</dd></dl>
<dl class="member">
<dt id="c.PyConfig.install_signal_handlers">
int <code class="sig-name descname">install_signal_handlers</code><a class="headerlink" href="#c.PyConfig.install_signal_handlers" title="永久链接至目标"></a></dt>
<dd><p>Install signal handlers?</p>
</dd></dl>
<dl class="member">
<dt id="c.PyConfig.interactive">
int <code class="sig-name descname">interactive</code><a class="headerlink" href="#c.PyConfig.interactive" title="永久链接至目标"></a></dt>
<dd><p>交互模式</p>
</dd></dl>
<dl class="member">
<dt id="c.PyConfig.isolated">
int <code class="sig-name descname">isolated</code><a class="headerlink" href="#c.PyConfig.isolated" title="永久链接至目标"></a></dt>
<dd><p>If greater than 0, enable isolated mode:</p>
<ul class="simple">
<li><p><a class="reference internal" href="../library/sys.html#sys.path" title="sys.path"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.path</span></code></a> contains neither the script's directory (computed from
<code class="docutils literal notranslate"><span class="pre">argv[0]</span></code> or the current directory) nor the user's site-packages
directory.</p></li>
<li><p>Python REPL 将不导入 <a class="reference internal" href="../library/readline.html#module-readline" title="readline: GNU readline support for Python. (Unix)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">readline</span></code></a> 也不在交互提示符中启用默认的 readline 配置。</p></li>
<li><p>Set <a class="reference internal" href="#c.PyConfig.use_environment" title="PyConfig.use_environment"><code class="xref c c-member docutils literal notranslate"><span class="pre">use_environment</span></code></a> and
<a class="reference internal" href="#c.PyConfig.user_site_directory" title="PyConfig.user_site_directory"><code class="xref c c-member docutils literal notranslate"><span class="pre">user_site_directory</span></code></a> to 0.</p></li>
</ul>
</dd></dl>
<dl class="member">
<dt id="c.PyConfig.legacy_windows_stdio">
int <code class="sig-name descname">legacy_windows_stdio</code><a class="headerlink" href="#c.PyConfig.legacy_windows_stdio" title="永久链接至目标"></a></dt>
<dd><p>If non-zero, use <a class="reference internal" href="../library/io.html#io.FileIO" title="io.FileIO"><code class="xref py py-class docutils literal notranslate"><span class="pre">io.FileIO</span></code></a> instead of
<code class="xref py py-class docutils literal notranslate"><span class="pre">io.WindowsConsoleIO</span></code> for <a class="reference internal" href="../library/sys.html#sys.stdin" title="sys.stdin"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.stdin</span></code></a>, <a class="reference internal" href="../library/sys.html#sys.stdout" title="sys.stdout"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.stdout</span></code></a>
and <a class="reference internal" href="../library/sys.html#sys.stderr" title="sys.stderr"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.stderr</span></code></a>.</p>
<p>仅在 Windows 上可用。 <code class="docutils literal notranslate"><span class="pre">#ifdef</span> <span class="pre">MS_WINDOWS</span></code> 宏可被用于 Windows 专属的代码。</p>
</dd></dl>
<dl class="member">
<dt id="c.PyConfig.malloc_stats">
int <code class="sig-name descname">malloc_stats</code><a class="headerlink" href="#c.PyConfig.malloc_stats" title="永久链接至目标"></a></dt>
<dd><p>如为非零值,则在退出时转储 <a class="reference internal" href="memory.html#pymalloc"><span class="std std-ref">Python pymalloc 内存分配器</span></a> 的统计数据。</p>
<p>The option is ignored if Python is built using <code class="docutils literal notranslate"><span class="pre">--without-pymalloc</span></code>.</p>
</dd></dl>
<dl class="member">
<dt id="c.PyConfig.pythonpath_env">
wchar_t* <code class="sig-name descname">pythonpath_env</code><a class="headerlink" href="#c.PyConfig.pythonpath_env" title="永久链接至目标"></a></dt>
<dd><p>Module search paths as a string separated by <code class="docutils literal notranslate"><span class="pre">DELIM</span></code>
(<code class="xref py py-data docutils literal notranslate"><span class="pre">os.path.pathsep</span></code>).</p>
<p>Initialized from <span class="target" id="index-3"></span><a class="reference internal" href="../using/cmdline.html#envvar-PYTHONPATH"><code class="xref std std-envvar docutils literal notranslate"><span class="pre">PYTHONPATH</span></code></a> environment variable value by
default.</p>
</dd></dl>
<dl class="member">
<dt id="c.PyConfig.module_search_paths">
<a class="reference internal" href="#c.PyWideStringList" title="PyWideStringList">PyWideStringList</a> <code class="sig-name descname">module_search_paths</code><a class="headerlink" href="#c.PyConfig.module_search_paths" title="永久链接至目标"></a></dt>
<dd></dd></dl>
<dl class="member">
<dt id="c.PyConfig.module_search_paths_set">
int <code class="sig-name descname">module_search_paths_set</code><a class="headerlink" href="#c.PyConfig.module_search_paths_set" title="永久链接至目标"></a></dt>
<dd><p><a class="reference internal" href="../library/sys.html#sys.path" title="sys.path"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.path</span></code></a>. If <a class="reference internal" href="#c.PyConfig.module_search_paths_set" title="PyConfig.module_search_paths_set"><code class="xref c c-member docutils literal notranslate"><span class="pre">module_search_paths_set</span></code></a> is
equal to 0, the <a class="reference internal" href="#c.PyConfig.module_search_paths" title="PyConfig.module_search_paths"><code class="xref c c-member docutils literal notranslate"><span class="pre">module_search_paths</span></code></a> is overridden
by the function calculating the <a class="reference internal" href="#init-path-config"><span class="std std-ref">Path Configuration</span></a>.</p>
</dd></dl>
<dl class="member">
<dt id="c.PyConfig.optimization_level">
int <code class="sig-name descname">optimization_level</code><a class="headerlink" href="#c.PyConfig.optimization_level" title="永久链接至目标"></a></dt>
<dd><p>编译优化级别:</p>
<ul class="simple">
<li><p>0: Peephole optimizer (and <code class="docutils literal notranslate"><span class="pre">__debug__</span></code> is set to <code class="docutils literal notranslate"><span class="pre">True</span></code>)</p></li>
<li><p>1: Remove assertions, set <code class="docutils literal notranslate"><span class="pre">__debug__</span></code> to <code class="docutils literal notranslate"><span class="pre">False</span></code></p></li>
<li><p>2: Strip docstrings</p></li>
</ul>
</dd></dl>
<dl class="member">
<dt id="c.PyConfig.parse_argv">
int <code class="sig-name descname">parse_argv</code><a class="headerlink" href="#c.PyConfig.parse_argv" title="永久链接至目标"></a></dt>
<dd><p>If non-zero, parse <a class="reference internal" href="#c.PyConfig.argv" title="PyConfig.argv"><code class="xref c c-member docutils literal notranslate"><span class="pre">argv</span></code></a> the same way the regular
Python command line arguments, and strip Python arguments from
<a class="reference internal" href="#c.PyConfig.argv" title="PyConfig.argv"><code class="xref c c-member docutils literal notranslate"><span class="pre">argv</span></code></a>: see <a class="reference internal" href="../using/cmdline.html#using-on-cmdline"><span class="std std-ref">Command Line Arguments</span></a>.</p>
</dd></dl>
<dl class="member">
<dt id="c.PyConfig.parser_debug">
int <code class="sig-name descname">parser_debug</code><a class="headerlink" href="#c.PyConfig.parser_debug" title="永久链接至目标"></a></dt>
<dd><p>If non-zero, turn on parser debugging output (for expert only, depending
on compilation options).</p>
</dd></dl>
<dl class="member">
<dt id="c.PyConfig.pathconfig_warnings">
int <code class="sig-name descname">pathconfig_warnings</code><a class="headerlink" href="#c.PyConfig.pathconfig_warnings" title="永久链接至目标"></a></dt>
<dd><p>If equal to 0, suppress warnings when calculating the <a class="reference internal" href="#init-path-config"><span class="std std-ref">Path
Configuration</span></a> (Unix only, Windows does not log any
warning). Otherwise, warnings are written into <code class="docutils literal notranslate"><span class="pre">stderr</span></code>.</p>
</dd></dl>
<dl class="member">
<dt id="c.PyConfig.prefix">
wchar_t* <code class="sig-name descname">prefix</code><a class="headerlink" href="#c.PyConfig.prefix" title="永久链接至目标"></a></dt>
<dd><p><a class="reference internal" href="../library/sys.html#sys.prefix" title="sys.prefix"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.prefix</span></code></a>.</p>
</dd></dl>
<dl class="member">
<dt id="c.PyConfig.program_name">
wchar_t* <code class="sig-name descname">program_name</code><a class="headerlink" href="#c.PyConfig.program_name" title="永久链接至目标"></a></dt>
<dd><p>Program name. Used to initialize <a class="reference internal" href="#c.PyConfig.executable" title="PyConfig.executable"><code class="xref c c-member docutils literal notranslate"><span class="pre">executable</span></code></a>, and in
early error messages.</p>
</dd></dl>
<dl class="member">
<dt id="c.PyConfig.pycache_prefix">
wchar_t* <code class="sig-name descname">pycache_prefix</code><a class="headerlink" href="#c.PyConfig.pycache_prefix" title="永久链接至目标"></a></dt>
<dd><p><a class="reference internal" href="../library/sys.html#sys.pycache_prefix" title="sys.pycache_prefix"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.pycache_prefix</span></code></a>: <code class="docutils literal notranslate"><span class="pre">.pyc</span></code> cache prefix.</p>
<p>如果为 <code class="docutils literal notranslate"><span class="pre">NULL</span></code>,则 <a class="reference internal" href="../library/sys.html#sys.pycache_prefix" title="sys.pycache_prefix"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.pycache_prefix</span></code></a> 将被设为 <code class="docutils literal notranslate"><span class="pre">None</span></code></p>
</dd></dl>
<dl class="member">
<dt id="c.PyConfig.quiet">
int <code class="sig-name descname">quiet</code><a class="headerlink" href="#c.PyConfig.quiet" title="永久链接至目标"></a></dt>
<dd><p>Quiet mode. For example, don't display the copyright and version messages
in interactive mode.</p>
</dd></dl>
<dl class="member">
<dt id="c.PyConfig.run_command">
wchar_t* <code class="sig-name descname">run_command</code><a class="headerlink" href="#c.PyConfig.run_command" title="永久链接至目标"></a></dt>
<dd><p><code class="docutils literal notranslate"><span class="pre">python3</span> <span class="pre">-c</span> <span class="pre">COMMAND</span></code> argument. Used by <a class="reference internal" href="#c.Py_RunMain" title="Py_RunMain"><code class="xref c c-func docutils literal notranslate"><span class="pre">Py_RunMain()</span></code></a>.</p>
</dd></dl>
<dl class="member">
<dt id="c.PyConfig.run_filename">
wchar_t* <code class="sig-name descname">run_filename</code><a class="headerlink" href="#c.PyConfig.run_filename" title="永久链接至目标"></a></dt>
<dd><p><code class="docutils literal notranslate"><span class="pre">python3</span> <span class="pre">FILENAME</span></code> argument. Used by <a class="reference internal" href="#c.Py_RunMain" title="Py_RunMain"><code class="xref c c-func docutils literal notranslate"><span class="pre">Py_RunMain()</span></code></a>.</p>
</dd></dl>
<dl class="member">
<dt id="c.PyConfig.run_module">
wchar_t* <code class="sig-name descname">run_module</code><a class="headerlink" href="#c.PyConfig.run_module" title="永久链接至目标"></a></dt>
<dd><p><code class="docutils literal notranslate"><span class="pre">python3</span> <span class="pre">-m</span> <span class="pre">MODULE</span></code> argument. Used by <a class="reference internal" href="#c.Py_RunMain" title="Py_RunMain"><code class="xref c c-func docutils literal notranslate"><span class="pre">Py_RunMain()</span></code></a>.</p>
</dd></dl>
<dl class="member">
<dt id="c.PyConfig.show_alloc_count">
int <code class="sig-name descname">show_alloc_count</code><a class="headerlink" href="#c.PyConfig.show_alloc_count" title="永久链接至目标"></a></dt>
<dd><p>Show allocation counts at exit?</p>
<p>Set to 1 by <a class="reference internal" href="../using/cmdline.html#id5"><code class="xref std std-option docutils literal notranslate"><span class="pre">-X</span> <span class="pre">showalloccount</span></code></a> command line option.</p>
<p>Need a special Python build with <code class="docutils literal notranslate"><span class="pre">COUNT_ALLOCS</span></code> macro defined.</p>
</dd></dl>
<dl class="member">
<dt id="c.PyConfig.show_ref_count">
int <code class="sig-name descname">show_ref_count</code><a class="headerlink" href="#c.PyConfig.show_ref_count" title="永久链接至目标"></a></dt>
<dd><p>在退出时显示引用总数?</p>
<p>Set to 1 by <a class="reference internal" href="../using/cmdline.html#id5"><code class="xref std std-option docutils literal notranslate"><span class="pre">-X</span> <span class="pre">showrefcount</span></code></a> command line option.</p>
<p>Need a debug build of Python (<code class="docutils literal notranslate"><span class="pre">Py_REF_DEBUG</span></code> macro must be defined).</p>
</dd></dl>
<dl class="member">
<dt id="c.PyConfig.site_import">
int <code class="sig-name descname">site_import</code><a class="headerlink" href="#c.PyConfig.site_import" title="永久链接至目标"></a></dt>
<dd><p>在启动时导入 <a class="reference internal" href="../library/site.html#module-site" title="site: Module responsible for site-specific configuration."><code class="xref py py-mod docutils literal notranslate"><span class="pre">site</span></code></a> 模块?</p>
</dd></dl>
<dl class="member">
<dt id="c.PyConfig.skip_source_first_line">
int <code class="sig-name descname">skip_source_first_line</code><a class="headerlink" href="#c.PyConfig.skip_source_first_line" title="永久链接至目标"></a></dt>
<dd><p>Skip the first line of the source?</p>
</dd></dl>
<dl class="member">
<dt id="c.PyConfig.stdio_encoding">
wchar_t* <code class="sig-name descname">stdio_encoding</code><a class="headerlink" href="#c.PyConfig.stdio_encoding" title="永久链接至目标"></a></dt>
<dd></dd></dl>
<dl class="member">
<dt id="c.PyConfig.stdio_errors">
wchar_t* <code class="sig-name descname">stdio_errors</code><a class="headerlink" href="#c.PyConfig.stdio_errors" title="永久链接至目标"></a></dt>
<dd><p>Encoding and encoding errors of <a class="reference internal" href="../library/sys.html#sys.stdin" title="sys.stdin"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.stdin</span></code></a>, <a class="reference internal" href="../library/sys.html#sys.stdout" title="sys.stdout"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.stdout</span></code></a> and
<a class="reference internal" href="../library/sys.html#sys.stderr" title="sys.stderr"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.stderr</span></code></a>.</p>
</dd></dl>
<dl class="member">
<dt id="c.PyConfig.tracemalloc">
int <code class="sig-name descname">tracemalloc</code><a class="headerlink" href="#c.PyConfig.tracemalloc" title="永久链接至目标"></a></dt>
<dd><p>如果为非零值,则在启动时调用 <a class="reference internal" href="../library/tracemalloc.html#tracemalloc.start" title="tracemalloc.start"><code class="xref py py-func docutils literal notranslate"><span class="pre">tracemalloc.start()</span></code></a></p>
</dd></dl>
<dl class="member">
<dt id="c.PyConfig.use_environment">
int <code class="sig-name descname">use_environment</code><a class="headerlink" href="#c.PyConfig.use_environment" title="永久链接至目标"></a></dt>
<dd><p>If greater than 0, use <a class="reference internal" href="../using/cmdline.html#using-on-envvars"><span class="std std-ref">environment variables</span></a>.</p>
</dd></dl>
<dl class="member">
<dt id="c.PyConfig.user_site_directory">
int <code class="sig-name descname">user_site_directory</code><a class="headerlink" href="#c.PyConfig.user_site_directory" title="永久链接至目标"></a></dt>
<dd><p>If non-zero, add user site directory to <a class="reference internal" href="../library/sys.html#sys.path" title="sys.path"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.path</span></code></a>.</p>
</dd></dl>
<dl class="member">
<dt id="c.PyConfig.verbose">
int <code class="sig-name descname">verbose</code><a class="headerlink" href="#c.PyConfig.verbose" title="永久链接至目标"></a></dt>
<dd><p>If non-zero, enable verbose mode.</p>
</dd></dl>
<dl class="member">
<dt id="c.PyConfig.warnoptions">
<a class="reference internal" href="#c.PyWideStringList" title="PyWideStringList">PyWideStringList</a> <code class="sig-name descname">warnoptions</code><a class="headerlink" href="#c.PyConfig.warnoptions" title="永久链接至目标"></a></dt>
<dd><p><a class="reference internal" href="../library/sys.html#sys.warnoptions" title="sys.warnoptions"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.warnoptions</span></code></a>: options of the <a class="reference internal" href="../library/warnings.html#module-warnings" title="warnings: Issue warning messages and control their disposition."><code class="xref py py-mod docutils literal notranslate"><span class="pre">warnings</span></code></a> module to build
warnings filters: lowest to highest priority.</p>
<p><a class="reference internal" href="../library/warnings.html#module-warnings" title="warnings: Issue warning messages and control their disposition."><code class="xref py py-mod docutils literal notranslate"><span class="pre">warnings</span></code></a> 模块以相反的顺序添加 <a class="reference internal" href="../library/sys.html#sys.warnoptions" title="sys.warnoptions"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.warnoptions</span></code></a>: 最后一个 <a class="reference internal" href="#c.PyConfig.warnoptions" title="PyConfig.warnoptions"><code class="xref c c-member docutils literal notranslate"><span class="pre">PyConfig.warnoptions</span></code></a> 条目将成为 <code class="xref py py-data docutils literal notranslate"><span class="pre">warnings.filters</span></code> 的第一个条目并将最先被检查(最高优先级)。</p>
</dd></dl>
<dl class="member">
<dt id="c.PyConfig.write_bytecode">
int <code class="sig-name descname">write_bytecode</code><a class="headerlink" href="#c.PyConfig.write_bytecode" title="永久链接至目标"></a></dt>
<dd><p>If non-zero, write <code class="docutils literal notranslate"><span class="pre">.pyc</span></code> files.</p>
<p><a class="reference internal" href="../library/sys.html#sys.dont_write_bytecode" title="sys.dont_write_bytecode"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.dont_write_bytecode</span></code></a> 会被初始化为 <a class="reference internal" href="#c.PyConfig.write_bytecode" title="PyConfig.write_bytecode"><code class="xref c c-member docutils literal notranslate"><span class="pre">write_bytecode</span></code></a> 取反后的值。</p>
</dd></dl>
<dl class="member">
<dt id="c.PyConfig.xoptions">
<a class="reference internal" href="#c.PyWideStringList" title="PyWideStringList">PyWideStringList</a> <code class="sig-name descname">xoptions</code><a class="headerlink" href="#c.PyConfig.xoptions" title="永久链接至目标"></a></dt>
<dd><p><a class="reference internal" href="../library/sys.html#sys._xoptions" title="sys._xoptions"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys._xoptions</span></code></a>.</p>
</dd></dl>
</dd></dl>
<p>If <code class="docutils literal notranslate"><span class="pre">parse_argv</span></code> is non-zero, <code class="docutils literal notranslate"><span class="pre">argv</span></code> arguments are parsed the same
way the regular Python parses command line arguments, and Python
arguments are stripped from <code class="docutils literal notranslate"><span class="pre">argv</span></code>: see <a class="reference internal" href="../using/cmdline.html#using-on-cmdline"><span class="std std-ref">Command Line Arguments</span></a>.</p>
<p>The <code class="docutils literal notranslate"><span class="pre">xoptions</span></code> options are parsed to set other options: see <a class="reference internal" href="../using/cmdline.html#id5"><code class="xref std std-option docutils literal notranslate"><span class="pre">-X</span></code></a>
option.</p>
</section>
<section id="initialization-with-pyconfig">
<h2>使用 PyConfig 初始化<a class="headerlink" href="#initialization-with-pyconfig" title="永久链接至标题"></a></h2>
<p>用于初始化 Python 的函数:</p>
<dl class="function">
<dt id="c.Py_InitializeFromConfig">
<a class="reference internal" href="#c.PyStatus" title="PyStatus">PyStatus</a> <code class="sig-name descname">Py_InitializeFromConfig</code><span class="sig-paren">(</span>const <a class="reference internal" href="#c.PyConfig" title="PyConfig">PyConfig</a><em> *config</em><span class="sig-paren">)</span><a class="headerlink" href="#c.Py_InitializeFromConfig" title="永久链接至目标"></a></dt>
<dd><p>根据 <em>config</em> 配置来初始化 Python。</p>
</dd></dl>
<p>调用方要负责使用 <a class="reference internal" href="#c.PyStatus_Exception" title="PyStatus_Exception"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyStatus_Exception()</span></code></a><a class="reference internal" href="#c.Py_ExitStatusException" title="Py_ExitStatusException"><code class="xref c c-func docutils literal notranslate"><span class="pre">Py_ExitStatusException()</span></code></a> 来处理异常(错误或退出)。</p>
<p>If <code class="docutils literal notranslate"><span class="pre">PyImport_FrozenModules</span></code>, <code class="docutils literal notranslate"><span class="pre">PyImport_AppendInittab()</span></code> or
<code class="docutils literal notranslate"><span class="pre">PyImport_ExtendInittab()</span></code> are used, they must be set or called after Python
preinitialization and before the Python initialization.</p>
<p>设置程序名称的示例:</p>
<div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="kt">void</span><span class="w"> </span><span class="nf">init_python</span><span class="p">(</span><span class="kt">void</span><span class="p">)</span>
<span class="p">{</span>
<span class="w"> </span><span class="n">PyStatus</span><span class="w"> </span><span class="n">status</span><span class="p">;</span>
<span class="w"> </span><span class="n">PyConfig</span><span class="w"> </span><span class="n">config</span><span class="p">;</span>
<span class="w"> </span><span class="n">PyConfig_InitPythonConfig</span><span class="p">(</span><span class="o">&amp;</span><span class="n">config</span><span class="p">);</span>
<span class="w"> </span><span class="cm">/* Set the program name. Implicitly preinitialize Python. */</span>
<span class="w"> </span><span class="n">status</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">PyConfig_SetString</span><span class="p">(</span><span class="o">&amp;</span><span class="n">config</span><span class="p">,</span><span class="w"> </span><span class="o">&amp;</span><span class="n">config</span><span class="p">.</span><span class="n">program_name</span><span class="p">,</span>
<span class="w"> </span><span class="sa">L</span><span class="s">&quot;/path/to/my_program&quot;</span><span class="p">);</span>
<span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">PyStatus_Exception</span><span class="p">(</span><span class="n">status</span><span class="p">))</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="k">goto</span><span class="w"> </span><span class="n">fail</span><span class="p">;</span>
<span class="w"> </span><span class="p">}</span>
<span class="w"> </span><span class="n">status</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">Py_InitializeFromConfig</span><span class="p">(</span><span class="o">&amp;</span><span class="n">config</span><span class="p">);</span>
<span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">PyStatus_Exception</span><span class="p">(</span><span class="n">status</span><span class="p">))</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="k">goto</span><span class="w"> </span><span class="n">fail</span><span class="p">;</span>
<span class="w"> </span><span class="p">}</span>
<span class="w"> </span><span class="n">PyConfig_Clear</span><span class="p">(</span><span class="o">&amp;</span><span class="n">config</span><span class="p">);</span>
<span class="w"> </span><span class="k">return</span><span class="p">;</span>
<span class="nl">fail</span><span class="p">:</span>
<span class="w"> </span><span class="n">PyConfig_Clear</span><span class="p">(</span><span class="o">&amp;</span><span class="n">config</span><span class="p">);</span>
<span class="w"> </span><span class="n">Py_ExitStatusException</span><span class="p">(</span><span class="n">status</span><span class="p">);</span>
<span class="p">}</span>
</pre></div>
</div>
<p>More complete example modifying the default configuration, read the
configuration, and then override some parameters:</p>
<div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="n">PyStatus</span><span class="w"> </span><span class="nf">init_python</span><span class="p">(</span><span class="k">const</span><span class="w"> </span><span class="kt">char</span><span class="w"> </span><span class="o">*</span><span class="n">program_name</span><span class="p">)</span>
<span class="p">{</span>
<span class="w"> </span><span class="n">PyStatus</span><span class="w"> </span><span class="n">status</span><span class="p">;</span>
<span class="w"> </span><span class="n">PyConfig</span><span class="w"> </span><span class="n">config</span><span class="p">;</span>
<span class="w"> </span><span class="n">PyConfig_InitPythonConfig</span><span class="p">(</span><span class="o">&amp;</span><span class="n">config</span><span class="p">);</span>
<span class="w"> </span><span class="cm">/* Set the program name before reading the configuration</span>
<span class="cm"> (decode byte string from the locale encoding).</span>
<span class="cm"> Implicitly preinitialize Python. */</span>
<span class="w"> </span><span class="n">status</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">PyConfig_SetBytesString</span><span class="p">(</span><span class="o">&amp;</span><span class="n">config</span><span class="p">,</span><span class="w"> </span><span class="o">&amp;</span><span class="n">config</span><span class="p">.</span><span class="n">program_name</span><span class="p">,</span>
<span class="w"> </span><span class="n">program_name</span><span class="p">);</span>
<span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">PyStatus_Exception</span><span class="p">(</span><span class="n">status</span><span class="p">))</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="k">goto</span><span class="w"> </span><span class="n">done</span><span class="p">;</span>
<span class="w"> </span><span class="p">}</span>
<span class="w"> </span><span class="cm">/* Read all configuration at once */</span>
<span class="w"> </span><span class="n">status</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">PyConfig_Read</span><span class="p">(</span><span class="o">&amp;</span><span class="n">config</span><span class="p">);</span>
<span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">PyStatus_Exception</span><span class="p">(</span><span class="n">status</span><span class="p">))</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="k">goto</span><span class="w"> </span><span class="n">done</span><span class="p">;</span>
<span class="w"> </span><span class="p">}</span>
<span class="w"> </span><span class="cm">/* Append our custom search path to sys.path */</span>
<span class="w"> </span><span class="n">status</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">PyWideStringList_Append</span><span class="p">(</span><span class="o">&amp;</span><span class="n">config</span><span class="p">.</span><span class="n">module_search_paths</span><span class="p">,</span>
<span class="w"> </span><span class="sa">L</span><span class="s">&quot;/path/to/more/modules&quot;</span><span class="p">);</span>
<span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">PyStatus_Exception</span><span class="p">(</span><span class="n">status</span><span class="p">))</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="k">goto</span><span class="w"> </span><span class="n">done</span><span class="p">;</span>
<span class="w"> </span><span class="p">}</span>
<span class="w"> </span><span class="cm">/* Override executable computed by PyConfig_Read() */</span>
<span class="w"> </span><span class="n">status</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">PyConfig_SetString</span><span class="p">(</span><span class="o">&amp;</span><span class="n">config</span><span class="p">,</span><span class="w"> </span><span class="o">&amp;</span><span class="n">config</span><span class="p">.</span><span class="n">executable</span><span class="p">,</span>
<span class="w"> </span><span class="sa">L</span><span class="s">&quot;/path/to/my_executable&quot;</span><span class="p">);</span>
<span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">PyStatus_Exception</span><span class="p">(</span><span class="n">status</span><span class="p">))</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="k">goto</span><span class="w"> </span><span class="n">done</span><span class="p">;</span>
<span class="w"> </span><span class="p">}</span>
<span class="w"> </span><span class="n">status</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">Py_InitializeFromConfig</span><span class="p">(</span><span class="o">&amp;</span><span class="n">config</span><span class="p">);</span>
<span class="nl">done</span><span class="p">:</span>
<span class="w"> </span><span class="n">PyConfig_Clear</span><span class="p">(</span><span class="o">&amp;</span><span class="n">config</span><span class="p">);</span>
<span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="n">status</span><span class="p">;</span>
<span class="p">}</span>
</pre></div>
</div>
</section>
<section id="isolated-configuration">
<span id="init-isolated-conf"></span><h2>隔离配置<a class="headerlink" href="#isolated-configuration" title="永久链接至标题"></a></h2>
<p><a class="reference internal" href="#c.PyPreConfig_InitIsolatedConfig" title="PyPreConfig_InitIsolatedConfig"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyPreConfig_InitIsolatedConfig()</span></code></a><a class="reference internal" href="#c.PyConfig_InitIsolatedConfig" title="PyConfig_InitIsolatedConfig"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyConfig_InitIsolatedConfig()</span></code></a> 函数会创建一个配置来将 Python 与系统隔离开来。 例如,将 Python 嵌入到某个应用程序。</p>
<p>This configuration ignores global configuration variables, environments
variables, command line arguments (<a class="reference internal" href="#c.PyConfig.argv" title="PyConfig.argv"><code class="xref c c-member docutils literal notranslate"><span class="pre">PyConfig.argv</span></code></a> is not parsed)
and user site directory. The C standard streams (ex: <code class="docutils literal notranslate"><span class="pre">stdout</span></code>) and the
LC_CTYPE locale are left unchanged. Signal handlers are not installed.</p>
<p>Configuration files are still used with this configuration. Set the
<a class="reference internal" href="#init-path-config"><span class="std std-ref">Path Configuration</span></a> (&quot;output fields&quot;) to ignore these
configuration files and avoid the function computing the default path
configuration.</p>
</section>
<section id="python-configuration">
<span id="init-python-config"></span><h2>Python 配置<a class="headerlink" href="#python-configuration" title="永久链接至标题"></a></h2>
<p><a class="reference internal" href="#c.PyPreConfig_InitPythonConfig" title="PyPreConfig_InitPythonConfig"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyPreConfig_InitPythonConfig()</span></code></a><a class="reference internal" href="#c.PyConfig_InitPythonConfig" title="PyConfig_InitPythonConfig"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyConfig_InitPythonConfig()</span></code></a> 函数会创建一个配置来构建一个行为与常规 Python 相同的自定义 Python。</p>
<p>环境变量和命令行参数将被用于配置 Python而全局配置变量将被忽略。</p>
<p>This function enables C locale coercion (<span class="target" id="index-4"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0538"><strong>PEP 538</strong></a>) and UTF-8 Mode
(<span class="target" id="index-5"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0540"><strong>PEP 540</strong></a>) depending on the LC_CTYPE locale, <span class="target" id="index-6"></span><a class="reference internal" href="../using/cmdline.html#envvar-PYTHONUTF8"><code class="xref std std-envvar docutils literal notranslate"><span class="pre">PYTHONUTF8</span></code></a> and
<span class="target" id="index-7"></span><a class="reference internal" href="../using/cmdline.html#envvar-PYTHONCOERCECLOCALE"><code class="xref std std-envvar docutils literal notranslate"><span class="pre">PYTHONCOERCECLOCALE</span></code></a> environment variables.</p>
<p>定制的 Python 的示例总是会以隔离模式运行:</p>
<div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="kt">int</span><span class="w"> </span><span class="nf">main</span><span class="p">(</span><span class="kt">int</span><span class="w"> </span><span class="n">argc</span><span class="p">,</span><span class="w"> </span><span class="kt">char</span><span class="w"> </span><span class="o">**</span><span class="n">argv</span><span class="p">)</span>
<span class="p">{</span>
<span class="w"> </span><span class="n">PyStatus</span><span class="w"> </span><span class="n">status</span><span class="p">;</span>
<span class="w"> </span><span class="n">PyConfig</span><span class="w"> </span><span class="n">config</span><span class="p">;</span>
<span class="w"> </span><span class="n">PyConfig_InitPythonConfig</span><span class="p">(</span><span class="o">&amp;</span><span class="n">config</span><span class="p">);</span>
<span class="w"> </span><span class="n">config</span><span class="p">.</span><span class="n">isolated</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">1</span><span class="p">;</span>
<span class="w"> </span><span class="cm">/* Decode command line arguments.</span>
<span class="cm"> Implicitly preinitialize Python (in isolated mode). */</span>
<span class="w"> </span><span class="n">status</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">PyConfig_SetBytesArgv</span><span class="p">(</span><span class="o">&amp;</span><span class="n">config</span><span class="p">,</span><span class="w"> </span><span class="n">argc</span><span class="p">,</span><span class="w"> </span><span class="n">argv</span><span class="p">);</span>
<span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">PyStatus_Exception</span><span class="p">(</span><span class="n">status</span><span class="p">))</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="k">goto</span><span class="w"> </span><span class="n">fail</span><span class="p">;</span>
<span class="w"> </span><span class="p">}</span>
<span class="w"> </span><span class="n">status</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">Py_InitializeFromConfig</span><span class="p">(</span><span class="o">&amp;</span><span class="n">config</span><span class="p">);</span>
<span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">PyStatus_Exception</span><span class="p">(</span><span class="n">status</span><span class="p">))</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="k">goto</span><span class="w"> </span><span class="n">fail</span><span class="p">;</span>
<span class="w"> </span><span class="p">}</span>
<span class="w"> </span><span class="n">PyConfig_Clear</span><span class="p">(</span><span class="o">&amp;</span><span class="n">config</span><span class="p">);</span>
<span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="n">Py_RunMain</span><span class="p">();</span>
<span class="nl">fail</span><span class="p">:</span>
<span class="w"> </span><span class="n">PyConfig_Clear</span><span class="p">(</span><span class="o">&amp;</span><span class="n">config</span><span class="p">);</span>
<span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">PyStatus_IsExit</span><span class="p">(</span><span class="n">status</span><span class="p">))</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="n">status</span><span class="p">.</span><span class="n">exitcode</span><span class="p">;</span>
<span class="w"> </span><span class="p">}</span>
<span class="w"> </span><span class="cm">/* Display the error message and exit the process with</span>
<span class="cm"> non-zero exit code */</span>
<span class="w"> </span><span class="n">Py_ExitStatusException</span><span class="p">(</span><span class="n">status</span><span class="p">);</span>
<span class="p">}</span>
</pre></div>
</div>
</section>
<section id="path-configuration">
<span id="init-path-config"></span><h2>路径配置<a class="headerlink" href="#path-configuration" title="永久链接至标题"></a></h2>
<p><a class="reference internal" href="#c.PyConfig" title="PyConfig"><code class="xref c c-type docutils literal notranslate"><span class="pre">PyConfig</span></code></a> 包含多个用于路径配置的字段:</p>
<ul class="simple">
<li><p>路径配置输入:</p>
<ul>
<li><p><a class="reference internal" href="#c.PyConfig.home" title="PyConfig.home"><code class="xref c c-member docutils literal notranslate"><span class="pre">PyConfig.home</span></code></a></p></li>
<li><p><a class="reference internal" href="#c.PyConfig.pathconfig_warnings" title="PyConfig.pathconfig_warnings"><code class="xref c c-member docutils literal notranslate"><span class="pre">PyConfig.pathconfig_warnings</span></code></a></p></li>
<li><p><a class="reference internal" href="#c.PyConfig.program_name" title="PyConfig.program_name"><code class="xref c c-member docutils literal notranslate"><span class="pre">PyConfig.program_name</span></code></a></p></li>
<li><p><a class="reference internal" href="#c.PyConfig.pythonpath_env" title="PyConfig.pythonpath_env"><code class="xref c c-member docutils literal notranslate"><span class="pre">PyConfig.pythonpath_env</span></code></a></p></li>
<li><p>当前工作目录:用于获取绝对路径</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">PATH</span></code> 环境变量用于获取程序的完整路径 (来自 <a class="reference internal" href="#c.PyConfig.program_name" title="PyConfig.program_name"><code class="xref c c-member docutils literal notranslate"><span class="pre">PyConfig.program_name</span></code></a>)</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">__PYVENV_LAUNCHER__</span></code> 环境变量</p></li>
<li><p>(仅限 Windows only 注册表 HKEY_CURRENT_USER 和 HKEY_LOCAL_MACHINE 的 &quot;SoftwarePythonPythonCoreX.YPythonPath&quot; 项下的应用程序目录(其中 X.Y 为 Python 版本)。</p></li>
</ul>
</li>
<li><p>路径配置输出字段:</p>
<ul>
<li><p><a class="reference internal" href="#c.PyConfig.base_exec_prefix" title="PyConfig.base_exec_prefix"><code class="xref c c-member docutils literal notranslate"><span class="pre">PyConfig.base_exec_prefix</span></code></a></p></li>
<li><p><a class="reference internal" href="#c.PyConfig.base_executable" title="PyConfig.base_executable"><code class="xref c c-member docutils literal notranslate"><span class="pre">PyConfig.base_executable</span></code></a></p></li>
<li><p><a class="reference internal" href="#c.PyConfig.base_prefix" title="PyConfig.base_prefix"><code class="xref c c-member docutils literal notranslate"><span class="pre">PyConfig.base_prefix</span></code></a></p></li>
<li><p><a class="reference internal" href="#c.PyConfig.exec_prefix" title="PyConfig.exec_prefix"><code class="xref c c-member docutils literal notranslate"><span class="pre">PyConfig.exec_prefix</span></code></a></p></li>
<li><p><a class="reference internal" href="#c.PyConfig.executable" title="PyConfig.executable"><code class="xref c c-member docutils literal notranslate"><span class="pre">PyConfig.executable</span></code></a></p></li>
<li><p><a class="reference internal" href="#c.PyConfig.module_search_paths_set" title="PyConfig.module_search_paths_set"><code class="xref c c-member docutils literal notranslate"><span class="pre">PyConfig.module_search_paths_set</span></code></a>,
<a class="reference internal" href="#c.PyConfig.module_search_paths" title="PyConfig.module_search_paths"><code class="xref c c-member docutils literal notranslate"><span class="pre">PyConfig.module_search_paths</span></code></a></p></li>
<li><p><a class="reference internal" href="#c.PyConfig.prefix" title="PyConfig.prefix"><code class="xref c c-member docutils literal notranslate"><span class="pre">PyConfig.prefix</span></code></a></p></li>
</ul>
</li>
</ul>
<p>If at least one &quot;output field&quot; is not set, Python calculates the path
configuration to fill unset fields. If
<a class="reference internal" href="#c.PyConfig.module_search_paths_set" title="PyConfig.module_search_paths_set"><code class="xref c c-member docutils literal notranslate"><span class="pre">module_search_paths_set</span></code></a> is equal to 0,
<a class="reference internal" href="#c.PyConfig.module_search_paths" title="PyConfig.module_search_paths"><code class="xref c c-member docutils literal notranslate"><span class="pre">module_search_paths</span></code></a> is overridden and
<a class="reference internal" href="#c.PyConfig.module_search_paths_set" title="PyConfig.module_search_paths_set"><code class="xref c c-member docutils literal notranslate"><span class="pre">module_search_paths_set</span></code></a> is set to 1.</p>
<p>It is possible to completely ignore the function calculating the default
path configuration by setting explicitly all path configuration output
fields listed above. A string is considered as set even if it is non-empty.
<code class="docutils literal notranslate"><span class="pre">module_search_paths</span></code> is considered as set if
<code class="docutils literal notranslate"><span class="pre">module_search_paths_set</span></code> is set to 1. In this case, path
configuration input fields are ignored as well.</p>
<p>Set <a class="reference internal" href="#c.PyConfig.pathconfig_warnings" title="PyConfig.pathconfig_warnings"><code class="xref c c-member docutils literal notranslate"><span class="pre">pathconfig_warnings</span></code></a> to 0 to suppress warnings when
calculating the path configuration (Unix only, Windows does not log any warning).</p>
<p>如果 <a class="reference internal" href="#c.PyConfig.base_prefix" title="PyConfig.base_prefix"><code class="xref c c-member docutils literal notranslate"><span class="pre">base_prefix</span></code></a><a class="reference internal" href="#c.PyConfig.base_exec_prefix" title="PyConfig.base_exec_prefix"><code class="xref c c-member docutils literal notranslate"><span class="pre">base_exec_prefix</span></code></a> 字段未设置,它们将分别从 <a class="reference internal" href="#c.PyConfig.prefix" title="PyConfig.prefix"><code class="xref c c-member docutils literal notranslate"><span class="pre">prefix</span></code></a><a class="reference internal" href="#c.PyConfig.exec_prefix" title="PyConfig.exec_prefix"><code class="xref c c-member docutils literal notranslate"><span class="pre">exec_prefix</span></code></a> 继承其值。</p>
<p><a class="reference internal" href="#c.Py_RunMain" title="Py_RunMain"><code class="xref c c-func docutils literal notranslate"><span class="pre">Py_RunMain()</span></code></a><a class="reference internal" href="veryhigh.html#c.Py_Main" title="Py_Main"><code class="xref c c-func docutils literal notranslate"><span class="pre">Py_Main()</span></code></a> 将修改 <a class="reference internal" href="../library/sys.html#sys.path" title="sys.path"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.path</span></code></a>:</p>
<ul class="simple">
<li><p>如果 <a class="reference internal" href="#c.PyConfig.run_filename" title="PyConfig.run_filename"><code class="xref c c-member docutils literal notranslate"><span class="pre">run_filename</span></code></a> 已设置并且是一个包含 <code class="docutils literal notranslate"><span class="pre">__main__.py</span></code> 脚本的目录,则会将 <a class="reference internal" href="#c.PyConfig.run_filename" title="PyConfig.run_filename"><code class="xref c c-member docutils literal notranslate"><span class="pre">run_filename</span></code></a> 添加到 <a class="reference internal" href="../library/sys.html#sys.path" title="sys.path"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.path</span></code></a> 的开头。</p></li>
<li><p>如果 <a class="reference internal" href="#c.PyConfig.isolated" title="PyConfig.isolated"><code class="xref c c-member docutils literal notranslate"><span class="pre">isolated</span></code></a> 为零:</p>
<ul>
<li><p>如果设置了 <a class="reference internal" href="#c.PyConfig.run_module" title="PyConfig.run_module"><code class="xref c c-member docutils literal notranslate"><span class="pre">run_module</span></code></a>,则将当前目录添加到 <a class="reference internal" href="../library/sys.html#sys.path" title="sys.path"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.path</span></code></a> 的开头。 如果无法读取当前目录则不执行任何操作。</p></li>
<li><p>如果设置了 <a class="reference internal" href="#c.PyConfig.run_filename" title="PyConfig.run_filename"><code class="xref c c-member docutils literal notranslate"><span class="pre">run_filename</span></code></a>,则将文件名的目录添加到 <a class="reference internal" href="../library/sys.html#sys.path" title="sys.path"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.path</span></code></a> 的开头。</p></li>
<li><p>在其他情况下,则将一个空字符串添加到 <a class="reference internal" href="../library/sys.html#sys.path" title="sys.path"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.path</span></code></a> 的开头。</p></li>
</ul>
</li>
</ul>
<p>如果 <a class="reference internal" href="#c.PyConfig.site_import" title="PyConfig.site_import"><code class="xref c c-member docutils literal notranslate"><span class="pre">site_import</span></code></a> 为非零值,则 <a class="reference internal" href="../library/sys.html#sys.path" title="sys.path"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.path</span></code></a> 可通过 <a class="reference internal" href="../library/site.html#module-site" title="site: Module responsible for site-specific configuration."><code class="xref py py-mod docutils literal notranslate"><span class="pre">site</span></code></a> 模块修改。 如果 <a class="reference internal" href="#c.PyConfig.user_site_directory" title="PyConfig.user_site_directory"><code class="xref c c-member docutils literal notranslate"><span class="pre">user_site_directory</span></code></a> 为非零值且用户的 site-package 目录存在,则 <a class="reference internal" href="../library/site.html#module-site" title="site: Module responsible for site-specific configuration."><code class="xref py py-mod docutils literal notranslate"><span class="pre">site</span></code></a> 模块会将用户的 site-package 目录附加到 <a class="reference internal" href="../library/sys.html#sys.path" title="sys.path"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.path</span></code></a></p>
<p>路径配置会使用以下配置文件:</p>
<ul class="simple">
<li><p><code class="docutils literal notranslate"><span class="pre">pyvenv.cfg</span></code></p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">python._pth</span></code> (仅Windows)</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">pybuilddir.txt</span></code> (仅Unix)</p></li>
</ul>
<p><code class="docutils literal notranslate"><span class="pre">__PYVENV_LAUNCHER__</span></code> 环境变量将被用于设置 <a class="reference internal" href="#c.PyConfig.base_executable" title="PyConfig.base_executable"><code class="xref c c-member docutils literal notranslate"><span class="pre">PyConfig.base_executable</span></code></a></p>
</section>
<section id="py-runmain">
<h2>Py_RunMain()<a class="headerlink" href="#py-runmain" title="永久链接至标题"></a></h2>
<dl class="function">
<dt id="c.Py_RunMain">
int <code class="sig-name descname">Py_RunMain</code><span class="sig-paren">(</span>void<span class="sig-paren">)</span><a class="headerlink" href="#c.Py_RunMain" title="永久链接至目标"></a></dt>
<dd><p>执行在命令行或配置中指定的命令 (<a class="reference internal" href="#c.PyConfig.run_command" title="PyConfig.run_command"><code class="xref c c-member docutils literal notranslate"><span class="pre">PyConfig.run_command</span></code></a>)、脚本 (<a class="reference internal" href="#c.PyConfig.run_filename" title="PyConfig.run_filename"><code class="xref c c-member docutils literal notranslate"><span class="pre">PyConfig.run_filename</span></code></a>) 或模块 (<a class="reference internal" href="#c.PyConfig.run_module" title="PyConfig.run_module"><code class="xref c c-member docutils literal notranslate"><span class="pre">PyConfig.run_module</span></code></a>)。</p>
<p>在默认情况下如果使用了 <a class="reference internal" href="../using/cmdline.html#cmdoption-i"><code class="xref std std-option docutils literal notranslate"><span class="pre">-i</span></code></a> 选项,则运行 REPL。</p>
<p>最后,终结化 Python 并返回一个可传递给 <code class="docutils literal notranslate"><span class="pre">exit()</span></code> 函数的退出状态。</p>
</dd></dl>
<p>请参阅 <a class="reference internal" href="#init-python-config"><span class="std std-ref">Python 配置</span></a> 查看一个使用 <a class="reference internal" href="#c.Py_RunMain" title="Py_RunMain"><code class="xref c c-func docutils literal notranslate"><span class="pre">Py_RunMain()</span></code></a> 在隔离模式下始终运行自定义 Python 的示例。</p>
</section>
<section id="multi-phase-initialization-private-provisional-api">
<h2>多阶段初始化私有暂定 API<a class="headerlink" href="#multi-phase-initialization-private-provisional-api" title="永久链接至标题"></a></h2>
<p>This section is a private provisional API introducing multi-phase
initialization, the core feature of the <span class="target" id="index-8"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0432"><strong>PEP 432</strong></a>:</p>
<ul class="simple">
<li><p>“核心”初始化阶段,“最小化的基本 Python”</p>
<ul>
<li><p>内置类型;</p></li>
<li><p>内置异常;</p></li>
<li><p>内置和已冻结模块;</p></li>
<li><p><a class="reference internal" href="../library/sys.html#module-sys" title="sys: Access system-specific parameters and functions."><code class="xref py py-mod docutils literal notranslate"><span class="pre">sys</span></code></a> 模块仅部分初始化(例如: <a class="reference internal" href="../library/sys.html#sys.path" title="sys.path"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.path</span></code></a> 尚不存在)。</p></li>
</ul>
</li>
<li><p>&quot;主要&quot;初始化阶段Python 被完全初始化:</p>
<ul>
<li><p>安装并配置 <a class="reference internal" href="../library/importlib.html#module-importlib" title="importlib: The implementation of the import machinery."><code class="xref py py-mod docutils literal notranslate"><span class="pre">importlib</span></code></a></p></li>
<li><p>应用 <a class="reference internal" href="#init-path-config"><span class="std std-ref">路径配置</span></a></p></li>
<li><p>安装信号处理器;</p></li>
<li><p>完成 <a class="reference internal" href="../library/sys.html#module-sys" title="sys: Access system-specific parameters and functions."><code class="xref py py-mod docutils literal notranslate"><span class="pre">sys</span></code></a> 模块初始化 (例如:创建 <a class="reference internal" href="../library/sys.html#sys.stdout" title="sys.stdout"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.stdout</span></code></a><a class="reference internal" href="../library/sys.html#sys.path" title="sys.path"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.path</span></code></a>)</p></li>
<li><p>启用 <a class="reference internal" href="../library/faulthandler.html#module-faulthandler" title="faulthandler: Dump the Python traceback."><code class="xref py py-mod docutils literal notranslate"><span class="pre">faulthandler</span></code></a><a class="reference internal" href="../library/tracemalloc.html#module-tracemalloc" title="tracemalloc: Trace memory allocations."><code class="xref py py-mod docutils literal notranslate"><span class="pre">tracemalloc</span></code></a> 等可选功能;</p></li>
<li><p>导入 <a class="reference internal" href="../library/site.html#module-site" title="site: Module responsible for site-specific configuration."><code class="xref py py-mod docutils literal notranslate"><span class="pre">site</span></code></a> 模块;</p></li>
<li><p>等等.</p></li>
</ul>
</li>
</ul>
<p>私有临时API</p>
<ul class="simple">
<li><p><code class="xref c c-member docutils literal notranslate"><span class="pre">PyConfig._init_main</span></code>: if set to 0,
<a class="reference internal" href="#c.Py_InitializeFromConfig" title="Py_InitializeFromConfig"><code class="xref c c-func docutils literal notranslate"><span class="pre">Py_InitializeFromConfig()</span></code></a> stops at the &quot;Core&quot; initialization phase.</p></li>
</ul>
<dl class="function">
<dt id="c._Py_InitializeMain">
<a class="reference internal" href="#c.PyStatus" title="PyStatus">PyStatus</a> <code class="sig-name descname">_Py_InitializeMain</code><span class="sig-paren">(</span>void<span class="sig-paren">)</span><a class="headerlink" href="#c._Py_InitializeMain" title="永久链接至目标"></a></dt>
<dd><p>进入“主要”初始化阶段,完成 Python 初始化。</p>
</dd></dl>
<p>在“核心”阶段不会导入任何模块,也不会配置 <code class="docutils literal notranslate"><span class="pre">importlib</span></code> 模块: <a class="reference internal" href="#init-path-config"><span class="std std-ref">路径配置</span></a> 只会在“主要”阶段期间应用。 这可能允许在 Python 中定制 Python 以覆盖或微调 <a class="reference internal" href="#init-path-config"><span class="std std-ref">路径配置</span></a>,也可能会安装自定义的 <a class="reference internal" href="../library/sys.html#sys.meta_path" title="sys.meta_path"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.meta_path</span></code></a> 导入器或导入钩子等等。</p>
<p>It may become possible to calculatin the <a class="reference internal" href="#init-path-config"><span class="std std-ref">Path Configuration</span></a> in Python, after the Core phase and before the Main phase,
which is one of the <span class="target" id="index-9"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0432"><strong>PEP 432</strong></a> motivation.</p>
<p>“核心”阶段并没有完整的定义:在这一阶段什么应该可用什么不应该可用都尚未被指明。 该 API 被标记为私有和暂定的:也就是说该 API 可以随时被修改甚至被移除直到设计出适用的公共 API。</p>
<p>在“核心”和“主要”初始化阶段之间运行 Python 代码的示例:</p>
<div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="kt">void</span><span class="w"> </span><span class="nf">init_python</span><span class="p">(</span><span class="kt">void</span><span class="p">)</span>
<span class="p">{</span>
<span class="w"> </span><span class="n">PyStatus</span><span class="w"> </span><span class="n">status</span><span class="p">;</span>
<span class="w"> </span><span class="n">PyConfig</span><span class="w"> </span><span class="n">config</span><span class="p">;</span>
<span class="w"> </span><span class="n">PyConfig_InitPythonConfig</span><span class="p">(</span><span class="o">&amp;</span><span class="n">config</span><span class="p">);</span>
<span class="w"> </span><span class="n">config</span><span class="p">.</span><span class="n">_init_main</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span>
<span class="w"> </span><span class="cm">/* ... customize &#39;config&#39; configuration ... */</span>
<span class="w"> </span><span class="n">status</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">Py_InitializeFromConfig</span><span class="p">(</span><span class="o">&amp;</span><span class="n">config</span><span class="p">);</span>
<span class="w"> </span><span class="n">PyConfig_Clear</span><span class="p">(</span><span class="o">&amp;</span><span class="n">config</span><span class="p">);</span>
<span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">PyStatus_Exception</span><span class="p">(</span><span class="n">status</span><span class="p">))</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="n">Py_ExitStatusException</span><span class="p">(</span><span class="n">status</span><span class="p">);</span>
<span class="w"> </span><span class="p">}</span>
<span class="w"> </span><span class="cm">/* Use sys.stderr because sys.stdout is only created</span>
<span class="cm"> by _Py_InitializeMain() */</span>
<span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">res</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">PyRun_SimpleString</span><span class="p">(</span>
<span class="w"> </span><span class="s">&quot;import sys; &quot;</span>
<span class="w"> </span><span class="s">&quot;print(&#39;Run Python code before _Py_InitializeMain&#39;, &quot;</span>
<span class="w"> </span><span class="s">&quot;file=sys.stderr)&quot;</span><span class="p">);</span>
<span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">res</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">exit</span><span class="p">(</span><span class="mi">1</span><span class="p">);</span>
<span class="w"> </span><span class="p">}</span>
<span class="w"> </span><span class="cm">/* ... put more configuration code here ... */</span>
<span class="w"> </span><span class="n">status</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">_Py_InitializeMain</span><span class="p">();</span>
<span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">PyStatus_Exception</span><span class="p">(</span><span class="n">status</span><span class="p">))</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="n">Py_ExitStatusException</span><span class="p">(</span><span class="n">status</span><span class="p">);</span>
<span class="w"> </span><span class="p">}</span>
<span class="p">}</span>
</pre></div>
</div>
</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="#">Python初始化配置</a><ul>
<li><a class="reference internal" href="#pywidestringlist">PyWideStringList</a></li>
<li><a class="reference internal" href="#pystatus">PyStatus</a></li>
<li><a class="reference internal" href="#pypreconfig">PyPreConfig</a></li>
<li><a class="reference internal" href="#preinitialization-with-pypreconfig">Preinitialization with PyPreConfig</a></li>
<li><a class="reference internal" href="#pyconfig">PyConfig</a></li>
<li><a class="reference internal" href="#initialization-with-pyconfig">使用 PyConfig 初始化</a></li>
<li><a class="reference internal" href="#isolated-configuration">隔离配置</a></li>
<li><a class="reference internal" href="#python-configuration">Python 配置</a></li>
<li><a class="reference internal" href="#path-configuration">路径配置</a></li>
<li><a class="reference internal" href="#py-runmain">Py_RunMain()</a></li>
<li><a class="reference internal" href="#multi-phase-initialization-private-provisional-api">多阶段初始化私有暂定 API</a></li>
</ul>
</li>
</ul>
<h4>上一个主题</h4>
<p class="topless"><a href="init.html"
title="上一章">初始化,最终化和线程</a></p>
<h4>下一个主题</h4>
<p class="topless"><a href="memory.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/init_config.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="memory.html" title="内存管理"
>下一页</a> |</li>
<li class="right" >
<a href="init.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="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>