“Python/docs/3.9/c-api/capsule”的版本间差异

来自菜鸟教程
Python/docs/3.9/c-api/capsule
跳转至:导航、​搜索
(autoload)
 
(Page commit)
 
第1行: 第1行:
 +
{{DISPLAYTITLE:胶囊 — Python 文档}}
 
<div id="capsules" class="section">
 
<div id="capsules" class="section">
  
 
<span id="id1"></span>
 
<span id="id1"></span>
= Capsules =
+
= 胶囊 =
  
Refer to [[../../extending/extending#using-capsules|<span class="std std-ref">Providing a C API for an Extension Module</span>]] for more information on using these objects.
+
有关使用这些对象的更多信息,请参阅 [[../../extending/extending#using-capsules|为扩展模块提供 C API]]
  
 
<div class="versionadded">
 
<div class="versionadded">
  
<span class="versionmodified added">3.1 新版功能.</span>
+
<span class="versionmodified added">3.1 版中的新功能。</span>
  
  
 
</div>
 
</div>
; ''type'' <code>PyCapsule</code>
+
; <span class="k"><span class="pre">type</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">PyCapsule</span></span></span><br />
: This subtype of [[../structures#c|<code>PyObject</code>]] represents an opaque value, useful for C extension modules who need to pass an opaque value (as a <span class="xref c c-texpr">void*</span> pointer) through Python code to other C code. It is often used to make a C function pointer defined in one module available to other modules, so the regular import mechanism can be used to access C APIs defined in dynamically loaded modules.
+
 
 +
: [[../structures#c|PyObject]] 的这个子类型表示不透明值,对于需要通过 Python 代码将不透明值(作为 <span class="xref c c-texpr">void*</span> 指针)传递给其他 C 代码的 C 扩展模块非常有用。 它通常用于使在一个模块中定义的 C 函数指针可用于其他模块,因此常规导入机制可用于访问动态加载模块中定义的 C API。
  
 
<dl>
 
<dl>
<dt>''type'' <code>PyCapsule_Destructor</code></dt>
+
<dt><span class="k"><span class="pre">type</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">PyCapsule_Destructor</span></span></span><br />
<dd><p>The type of a destructor callback for a capsule. Defined as:</p>
+
</dt>
 +
<dd><p>胶囊的析构函数回调的类型。 定义为:</p>
 
<div class="highlight-c notranslate">
 
<div class="highlight-c notranslate">
  
 
<div class="highlight">
 
<div class="highlight">
  
<pre>typedef void (*PyCapsule_Destructor)(PyObject *);</pre>
+
<syntaxhighlight lang="c">typedef void (*PyCapsule_Destructor)(PyObject *);</syntaxhighlight>
  
 
</div>
 
</div>
  
 
</div>
 
</div>
<p>See [[#c.PyCapsule_New|<code>PyCapsule_New()</code>]] for the semantics of PyCapsule_Destructor
+
<p>有关 PyCapsule_Destructor 回调的语义,请参阅 [[#c.PyCapsule_New|PyCapsule_New()]]</p></dd></dl>
callbacks.</p></dd></dl>
 
  
; int <code>PyCapsule_CheckExact</code><span class="sig-paren">(</span>[[../structures#c|PyObject]] *''p''<span class="sig-paren">)</span>
+
; <span class="kt"><span class="pre">int</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">PyCapsule_CheckExact</span></span></span><span class="sig-paren">(</span>[[../structures#c|<span class="n"><span class="pre">PyObject</span></span>]]<span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">p</span></span><span class="sig-paren">)</span><br />
: Return true if its argument is a [[#c.PyCapsule|<code>PyCapsule</code>]].
+
 
 +
: 如果其参数是 [[#c.PyCapsule|PyCapsule]],则返回 true。 此功能总是成功。
  
 
<dl>
 
<dl>
<dt>[[../structures#c|PyObject]] *<code>PyCapsule_New</code><span class="sig-paren">(</span>void *''pointer'', ''const'' char *''name'', [[#c.PyCapsule_Destructor|PyCapsule_Destructor]] ''destructor''<span class="sig-paren">)</span></dt>
+
<dt>[[../structures#c|<span class="n"><span class="pre">PyObject</span></span>]]<span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="sig-name descname"><span class="n"><span class="pre">PyCapsule_New</span></span></span><span class="sig-paren">(</span><span class="kt"><span class="pre">void</span></span><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">pointer</span></span>, <span class="k"><span class="pre">const</span></span><span class="w"> </span><span class="kt"><span class="pre">char</span></span><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">name</span></span>, [[#c.PyCapsule_Destructor|<span class="n"><span class="pre">PyCapsule_Destructor</span></span>]]<span class="w"> </span><span class="n"><span class="pre">destructor</span></span><span class="sig-paren">)</span><br />
<dd><p>''Return value: New reference.''</p>
+
</dt>
<p>Create a [[#c.PyCapsule|<code>PyCapsule</code>]] encapsulating the ''pointer''. The ''pointer''
+
<dd><p>创建一个 [[#c.PyCapsule|PyCapsule]] 封装 ''指针'' ''pointer'' 参数可能不是 <code>NULL</code></p>
argument may not be <code>NULL</code>.</p>
+
<p>失败时,设置异常并返回 <code>NULL</code></p>
<p>On failure, set an exception and return <code>NULL</code>.</p>
+
<p>''name'' 字符串可以是 <code>NULL</code> 或指向有效 C 字符串的指针。 如果非 <code>NULL</code>,则此字符串必须比胶囊存活时间长。 (尽管允许在 ''析构函数'' 中释放它。)</p>
<p>The ''name'' string may either be <code>NULL</code> or a pointer to a valid C string. If
+
<p>如果 ''destructor'' 参数不是 <code>NULL</code>,它会在被销毁时以胶囊为参数被调用。</p>
non-<code>NULL</code>, this string must outlive the capsule. (Though it is permitted to
+
<p>如果此胶囊将作为模块的属性存储,则 ''name'' 应指定为 <code>modulename.attributename</code>。 这将使其他模块能够使用 [[#c.PyCapsule_Import|PyCapsule_Import()]] 导入胶囊。</p></dd></dl>
free it inside the ''destructor''.)</p>
 
<p>If the ''destructor'' argument is not <code>NULL</code>, it will be called with the
 
capsule as its argument when it is destroyed.</p>
 
<p>If this capsule will be stored as an attribute of a module, the ''name'' should
 
be specified as <code>modulename.attributename</code>. This will enable other modules
 
to import the capsule using [[#c.PyCapsule_Import|<code>PyCapsule_Import()</code>]].</p></dd></dl>
 
  
 
<dl>
 
<dl>
<dt>void *<code>PyCapsule_GetPointer</code><span class="sig-paren">(</span>[[../structures#c|PyObject]] *''capsule'', ''const'' char *''name''<span class="sig-paren">)</span></dt>
+
<dt><span class="kt"><span class="pre">void</span></span><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="sig-name descname"><span class="n"><span class="pre">PyCapsule_GetPointer</span></span></span><span class="sig-paren">(</span>[[../structures#c|<span class="n"><span class="pre">PyObject</span></span>]]<span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">capsule</span></span>, <span class="k"><span class="pre">const</span></span><span class="w"> </span><span class="kt"><span class="pre">char</span></span><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">name</span></span><span class="sig-paren">)</span><br />
<dd><p>Retrieve the ''pointer'' stored in the capsule. On failure, set an exception
+
</dt>
and return <code>NULL</code>.</p>
+
<dd><p>检索存储在胶囊中的 ''指针'' 。 失败时,设置异常并返回 <code>NULL</code></p>
<p>The ''name'' parameter must compare exactly to the name stored in the capsule.
+
<p>''name'' 参数必须与存储在胶囊中的名称完全一致。 如果capsule中存储的名字是<code>NULL</code>,那么传入的''name''也必须是<code>NULL</code>Python 使用 C 函数 <code>strcmp()</code> 来比较胶囊名称。</p></dd></dl>
If the name stored in the capsule is <code>NULL</code>, the ''name'' passed in must also
 
be <code>NULL</code>. Python uses the C function <code>strcmp()</code> to compare capsule
 
names.</p></dd></dl>
 
  
 
<dl>
 
<dl>
<dt>[[#c.PyCapsule_Destructor|PyCapsule_Destructor]] <code>PyCapsule_GetDestructor</code><span class="sig-paren">(</span>[[../structures#c|PyObject]] *''capsule''<span class="sig-paren">)</span></dt>
+
<dt>[[#c.PyCapsule_Destructor|<span class="n"><span class="pre">PyCapsule_Destructor</span></span>]]<span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">PyCapsule_GetDestructor</span></span></span><span class="sig-paren">(</span>[[../structures#c|<span class="n"><span class="pre">PyObject</span></span>]]<span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">capsule</span></span><span class="sig-paren">)</span><br />
<dd><p>Return the current destructor stored in the capsule. On failure, set an
+
</dt>
exception and return <code>NULL</code>.</p>
+
<dd><p>返回存储在胶囊中的当前析构函数。 失败时,设置异常并返回 <code>NULL</code></p>
<p>It is legal for a capsule to have a <code>NULL</code> destructor. This makes a <code>NULL</code>
+
<p>胶囊具有 <code>NULL</code> 析构函数是合法的。 这使得 <code>NULL</code> 返回码有些模糊; 使用 [[#c.PyCapsule_IsValid|PyCapsule_IsValid()]] [[../exceptions#c|PyErr_Occurred()]] 来消除歧义。</p></dd></dl>
return code somewhat ambiguous; use [[#c.PyCapsule_IsValid|<code>PyCapsule_IsValid()</code>]] or
 
[[../exceptions#c|<code>PyErr_Occurred()</code>]] to disambiguate.</p></dd></dl>
 
  
 
<dl>
 
<dl>
<dt>void *<code>PyCapsule_GetContext</code><span class="sig-paren">(</span>[[../structures#c|PyObject]] *''capsule''<span class="sig-paren">)</span></dt>
+
<dt><span class="kt"><span class="pre">void</span></span><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="sig-name descname"><span class="n"><span class="pre">PyCapsule_GetContext</span></span></span><span class="sig-paren">(</span>[[../structures#c|<span class="n"><span class="pre">PyObject</span></span>]]<span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">capsule</span></span><span class="sig-paren">)</span><br />
<dd><p>Return the current context stored in the capsule. On failure, set an
+
</dt>
exception and return <code>NULL</code>.</p>
+
<dd><p>返回存储在胶囊中的当前上下文。 失败时,设置异常并返回 <code>NULL</code></p>
<p>It is legal for a capsule to have a <code>NULL</code> context. This makes a <code>NULL</code>
+
<p>胶囊具有 <code>NULL</code> 上下文是合法的。 这使得 <code>NULL</code> 返回码有些模糊; 使用 [[#c.PyCapsule_IsValid|PyCapsule_IsValid()]] [[../exceptions#c|PyErr_Occurred()]] 来消除歧义。</p></dd></dl>
return code somewhat ambiguous; use [[#c.PyCapsule_IsValid|<code>PyCapsule_IsValid()</code>]] or
 
[[../exceptions#c|<code>PyErr_Occurred()</code>]] to disambiguate.</p></dd></dl>
 
  
 
<dl>
 
<dl>
<dt>''const'' char *<code>PyCapsule_GetName</code><span class="sig-paren">(</span>[[../structures#c|PyObject]] *''capsule''<span class="sig-paren">)</span></dt>
+
<dt><span class="k"><span class="pre">const</span></span><span class="w"> </span><span class="kt"><span class="pre">char</span></span><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="sig-name descname"><span class="n"><span class="pre">PyCapsule_GetName</span></span></span><span class="sig-paren">(</span>[[../structures#c|<span class="n"><span class="pre">PyObject</span></span>]]<span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">capsule</span></span><span class="sig-paren">)</span><br />
<dd><p>Return the current name stored in the capsule. On failure, set an exception
+
</dt>
and return <code>NULL</code>.</p>
+
<dd><p>返回存储在胶囊中的当前名称。 失败时,设置异常并返回 <code>NULL</code></p>
<p>It is legal for a capsule to have a <code>NULL</code> name. This makes a <code>NULL</code> return
+
<p>胶囊具有 <code>NULL</code> 名称是合法的。 这使得 <code>NULL</code> 返回码有些模糊; 使用 [[#c.PyCapsule_IsValid|PyCapsule_IsValid()]] [[../exceptions#c|PyErr_Occurred()]] 来消除歧义。</p></dd></dl>
code somewhat ambiguous; use [[#c.PyCapsule_IsValid|<code>PyCapsule_IsValid()</code>]] or
 
[[../exceptions#c|<code>PyErr_Occurred()</code>]] to disambiguate.</p></dd></dl>
 
  
 
<dl>
 
<dl>
<dt>void *<code>PyCapsule_Import</code><span class="sig-paren">(</span>''const'' char *''name'', int ''no_block''<span class="sig-paren">)</span></dt>
+
<dt><span class="kt"><span class="pre">void</span></span><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="sig-name descname"><span class="n"><span class="pre">PyCapsule_Import</span></span></span><span class="sig-paren">(</span><span class="k"><span class="pre">const</span></span><span class="w"> </span><span class="kt"><span class="pre">char</span></span><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">name</span></span>, <span class="kt"><span class="pre">int</span></span><span class="w"> </span><span class="n"><span class="pre">no_block</span></span><span class="sig-paren">)</span><br />
<dd><p>Import a pointer to a C object from a capsule attribute in a module. The
+
</dt>
''name'' parameter should specify the full name to the attribute, as in
+
<dd><p>从模块中的胶囊属性导入指向 C 对象的指针。 ''name'' 参数应指定属性的全名,如 <code>module.attribute</code>。 存储在胶囊中的 ''name'' 必须与此字符串完全匹配。 如果 ''no_block'' 为真,则在不阻塞的情况下导入模块(使用 [[../import#c|PyImport_ImportModuleNoBlock()]])。 如果 ''no_block'' 为 false,则按惯例导入模块(使用 [[../import#c|PyImport_ImportModule()]])。</p>
<code>module.attribute</code>. The ''name'' stored in the capsule must match this
+
<p>成功时返回胶囊的内部 ''指针'' 。 失败时,设置异常并返回 <code>NULL</code></p></dd></dl>
string exactly. If ''no_block'' is true, import the module without blocking
 
(using [[../import#c|<code>PyImport_ImportModuleNoBlock()</code>]]). If ''no_block'' is false,
 
import the module conventionally (using [[../import#c|<code>PyImport_ImportModule()</code>]]).</p>
 
<p>Return the capsule's internal ''pointer'' on success. On failure, set an
 
exception and return <code>NULL</code>.</p></dd></dl>
 
  
 
<dl>
 
<dl>
<dt>int <code>PyCapsule_IsValid</code><span class="sig-paren">(</span>[[../structures#c|PyObject]] *''capsule'', ''const'' char *''name''<span class="sig-paren">)</span></dt>
+
<dt><span class="kt"><span class="pre">int</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">PyCapsule_IsValid</span></span></span><span class="sig-paren">(</span>[[../structures#c|<span class="n"><span class="pre">PyObject</span></span>]]<span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">capsule</span></span>, <span class="k"><span class="pre">const</span></span><span class="w"> </span><span class="kt"><span class="pre">char</span></span><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">name</span></span><span class="sig-paren">)</span><br />
<dd><p>Determines whether or not ''capsule'' is a valid capsule. A valid capsule is
+
</dt>
non-<code>NULL</code>, passes [[#c.PyCapsule_CheckExact|<code>PyCapsule_CheckExact()</code>]], has a non-<code>NULL</code> pointer
+
<dd><p>确定 ''capsule'' 是否是有效的 Capsule。 一个有效的胶囊是非<code>NULL</code>,通过[[#c.PyCapsule_CheckExact|PyCapsule_CheckExact()]],其中存储了一个非<code>NULL</code>指针,其内部名称匹配名称[ X158X] 参数。 (有关如何比较胶囊名称的信息,请参阅 [[#c.PyCapsule_GetPointer|PyCapsule_GetPointer()]]。)</p>
stored in it, and its internal name matches the ''name'' parameter. (See
+
<p>换句话说,如果 [[#c.PyCapsule_IsValid|PyCapsule_IsValid()]] 返回真值,则保证调用任何访问器(以 <code>PyCapsule_Get()</code> 开头的任何函数)成功。</p>
[[#c.PyCapsule_GetPointer|<code>PyCapsule_GetPointer()</code>]] for information on how capsule names are
+
<p>如果对象有效并且与传入的名称匹配,则返回一个非零值。 否则返回 <code>0</code>。 此功能不会失败。</p></dd></dl>
compared.)</p>
 
<p>In other words, if [[#c.PyCapsule_IsValid|<code>PyCapsule_IsValid()</code>]] returns a true value, calls to
 
any of the accessors (any function starting with <code>PyCapsule_Get()</code>) are
 
guaranteed to succeed.</p>
 
<p>Return a nonzero value if the object is valid and matches the name passed in.
 
Return <code>0</code> otherwise. This function will not fail.</p></dd></dl>
 
  
 
<dl>
 
<dl>
<dt>int <code>PyCapsule_SetContext</code><span class="sig-paren">(</span>[[../structures#c|PyObject]] *''capsule'', void *''context''<span class="sig-paren">)</span></dt>
+
<dt><span class="kt"><span class="pre">int</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">PyCapsule_SetContext</span></span></span><span class="sig-paren">(</span>[[../structures#c|<span class="n"><span class="pre">PyObject</span></span>]]<span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">capsule</span></span>, <span class="kt"><span class="pre">void</span></span><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">context</span></span><span class="sig-paren">)</span><br />
<dd><p>Set the context pointer inside ''capsule'' to ''context''.</p>
+
</dt>
<p>Return <code>0</code> on success. Return nonzero and set an exception on failure.</p></dd></dl>
+
<dd><p>''capsule'' 内的上下文指针设置为 ''context''</p>
 +
<p>成功返回 <code>0</code>。 返回非零值并在失败时设置异常。</p></dd></dl>
  
 
<dl>
 
<dl>
<dt>int <code>PyCapsule_SetDestructor</code><span class="sig-paren">(</span>[[../structures#c|PyObject]] *''capsule'', [[#c.PyCapsule_Destructor|PyCapsule_Destructor]] ''destructor''<span class="sig-paren">)</span></dt>
+
<dt><span class="kt"><span class="pre">int</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">PyCapsule_SetDestructor</span></span></span><span class="sig-paren">(</span>[[../structures#c|<span class="n"><span class="pre">PyObject</span></span>]]<span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">capsule</span></span>, [[#c.PyCapsule_Destructor|<span class="n"><span class="pre">PyCapsule_Destructor</span></span>]]<span class="w"> </span><span class="n"><span class="pre">destructor</span></span><span class="sig-paren">)</span><br />
<dd><p>Set the destructor inside ''capsule'' to ''destructor''.</p>
+
</dt>
<p>Return <code>0</code> on success. Return nonzero and set an exception on failure.</p></dd></dl>
+
<dd><p>''capsule'' 内的析构函数设置为 ''destructor''</p>
 +
<p>成功返回 <code>0</code>。 返回非零值并在失败时设置异常。</p></dd></dl>
  
 
<dl>
 
<dl>
<dt>int <code>PyCapsule_SetName</code><span class="sig-paren">(</span>[[../structures#c|PyObject]] *''capsule'', ''const'' char *''name''<span class="sig-paren">)</span></dt>
+
<dt><span class="kt"><span class="pre">int</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">PyCapsule_SetName</span></span></span><span class="sig-paren">(</span>[[../structures#c|<span class="n"><span class="pre">PyObject</span></span>]]<span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">capsule</span></span>, <span class="k"><span class="pre">const</span></span><span class="w"> </span><span class="kt"><span class="pre">char</span></span><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">name</span></span><span class="sig-paren">)</span><br />
<dd><p>Set the name inside ''capsule'' to ''name''. If non-<code>NULL</code>, the name must
+
</dt>
outlive the capsule. If the previous ''name'' stored in the capsule was not
+
<dd><p>''capsule'' 内的名称设置为 ''name''。 如果非<code>NULL</code>,则名称必须比胶囊长。 如果之前存储在胶囊中的 ''name'' 不是 <code>NULL</code>,则不会尝试释放它。</p>
<code>NULL</code>, no attempt is made to free it.</p>
+
<p>成功返回 <code>0</code>。 返回非零值并在失败时设置异常。</p></dd></dl>
<p>Return <code>0</code> on success. Return nonzero and set an exception on failure.</p></dd></dl>
 
  
 
<dl>
 
<dl>
<dt>int <code>PyCapsule_SetPointer</code><span class="sig-paren">(</span>[[../structures#c|PyObject]] *''capsule'', void *''pointer''<span class="sig-paren">)</span></dt>
+
<dt><span class="kt"><span class="pre">int</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">PyCapsule_SetPointer</span></span></span><span class="sig-paren">(</span>[[../structures#c|<span class="n"><span class="pre">PyObject</span></span>]]<span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">capsule</span></span>, <span class="kt"><span class="pre">void</span></span><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">pointer</span></span><span class="sig-paren">)</span><br />
<dd><p>Set the void pointer inside ''capsule'' to ''pointer''. The pointer may not be
+
</dt>
<code>NULL</code>.</p>
+
<dd><p>''capsule'' 内的 void 指针设置为 ''pointer''。 指针可能不是 <code>NULL</code></p>
<p>Return <code>0</code> on success. Return nonzero and set an exception on failure.</p></dd></dl>
+
<p>成功返回 <code>0</code>。 返回非零值并在失败时设置异常。</p></dd></dl>
 +
 
 +
 
 +
</div>
 +
<div class="clearer">
 +
 
  
  
 
</div>
 
</div>
  
[[Category:Python 3.9 中文文档]]
+
[[Category:Python 3.9 文档]]

2021年10月31日 (日) 04:50的最新版本

胶囊

有关使用这些对象的更多信息,请参阅 为扩展模块提供 C API

3.1 版中的新功能。


type PyCapsule
PyObject 的这个子类型表示不透明值,对于需要通过 Python 代码将不透明值(作为 void* 指针)传递给其他 C 代码的 C 扩展模块非常有用。 它通常用于使在一个模块中定义的 C 函数指针可用于其他模块,因此常规导入机制可用于访问动态加载模块中定义的 C API。
type PyCapsule_Destructor

胶囊的析构函数回调的类型。 定义为:

typedef void (*PyCapsule_Destructor)(PyObject *);

有关 PyCapsule_Destructor 回调的语义,请参阅 PyCapsule_New()

int PyCapsule_CheckExact(PyObject *p)
如果其参数是 PyCapsule,则返回 true。 此功能总是成功。
PyObject *PyCapsule_New(void *pointer, const char *name, PyCapsule_Destructor destructor)

创建一个 PyCapsule 封装 指针pointer 参数可能不是 NULL

失败时,设置异常并返回 NULL

name 字符串可以是 NULL 或指向有效 C 字符串的指针。 如果非 NULL,则此字符串必须比胶囊存活时间长。 (尽管允许在 析构函数 中释放它。)

如果 destructor 参数不是 NULL,它会在被销毁时以胶囊为参数被调用。

如果此胶囊将作为模块的属性存储,则 name 应指定为 modulename.attributename。 这将使其他模块能够使用 PyCapsule_Import() 导入胶囊。

void *PyCapsule_GetPointer(PyObject *capsule, const char *name)

检索存储在胶囊中的 指针 。 失败时,设置异常并返回 NULL

name 参数必须与存储在胶囊中的名称完全一致。 如果capsule中存储的名字是NULL,那么传入的name也必须是NULL。 Python 使用 C 函数 strcmp() 来比较胶囊名称。

PyCapsule_Destructor PyCapsule_GetDestructor(PyObject *capsule)

返回存储在胶囊中的当前析构函数。 失败时,设置异常并返回 NULL

胶囊具有 NULL 析构函数是合法的。 这使得 NULL 返回码有些模糊; 使用 PyCapsule_IsValid()PyErr_Occurred() 来消除歧义。

void *PyCapsule_GetContext(PyObject *capsule)

返回存储在胶囊中的当前上下文。 失败时,设置异常并返回 NULL

胶囊具有 NULL 上下文是合法的。 这使得 NULL 返回码有些模糊; 使用 PyCapsule_IsValid()PyErr_Occurred() 来消除歧义。

const char *PyCapsule_GetName(PyObject *capsule)

返回存储在胶囊中的当前名称。 失败时,设置异常并返回 NULL

胶囊具有 NULL 名称是合法的。 这使得 NULL 返回码有些模糊; 使用 PyCapsule_IsValid()PyErr_Occurred() 来消除歧义。

void *PyCapsule_Import(const char *name, int no_block)

从模块中的胶囊属性导入指向 C 对象的指针。 name 参数应指定属性的全名,如 module.attribute。 存储在胶囊中的 name 必须与此字符串完全匹配。 如果 no_block 为真,则在不阻塞的情况下导入模块(使用 PyImport_ImportModuleNoBlock())。 如果 no_block 为 false,则按惯例导入模块(使用 PyImport_ImportModule())。

成功时返回胶囊的内部 指针 。 失败时,设置异常并返回 NULL

int PyCapsule_IsValid(PyObject *capsule, const char *name)

确定 capsule 是否是有效的 Capsule。 一个有效的胶囊是非NULL,通过PyCapsule_CheckExact(),其中存储了一个非NULL指针,其内部名称匹配名称[ X158X] 参数。 (有关如何比较胶囊名称的信息,请参阅 PyCapsule_GetPointer()。)

换句话说,如果 PyCapsule_IsValid() 返回真值,则保证调用任何访问器(以 PyCapsule_Get() 开头的任何函数)成功。

如果对象有效并且与传入的名称匹配,则返回一个非零值。 否则返回 0。 此功能不会失败。

int PyCapsule_SetContext(PyObject *capsule, void *context)

capsule 内的上下文指针设置为 context

成功返回 0。 返回非零值并在失败时设置异常。

int PyCapsule_SetDestructor(PyObject *capsule, PyCapsule_Destructor destructor)

capsule 内的析构函数设置为 destructor

成功返回 0。 返回非零值并在失败时设置异常。

int PyCapsule_SetName(PyObject *capsule, const char *name)

capsule 内的名称设置为 name。 如果非NULL,则名称必须比胶囊长。 如果之前存储在胶囊中的 name 不是 NULL,则不会尝试释放它。

成功返回 0。 返回非零值并在失败时设置异常。

int PyCapsule_SetPointer(PyObject *capsule, void *pointer)

capsule 内的 void 指针设置为 pointer。 指针可能不是 NULL

成功返回 0。 返回非零值并在失败时设置异常。