| Server IP : 121.121.20.254 / Your IP : 216.73.216.202 Web Server : Microsoft-IIS/10.0 System : Windows NT WEB-SERVER 10.0 build 20348 (Windows Server 2022) AMD64 User : IUSR ( 0) PHP Version : 8.3.28 Disable Function : NONE MySQL : ON | cURL : ON | WGET : OFF | Perl : OFF | Python : OFF | Sudo : OFF | Pkexec : OFF Directory : C:/Python315/Doc/html/c-api/ |
Upload File : |
<!DOCTYPE html>
<html lang="en" data-content_root="../">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" /><meta name="viewport" content="width=device-width, initial-scale=1" />
<meta property="og:title" content="Definition slots" />
<meta property="og:type" content="website" />
<meta property="og:url" content="https://docs.python.org/3/c-api/slots.html" />
<meta property="og:site_name" content="Python documentation" />
<meta property="og:description" content="To define module objects and classes using the C API, you may use an array of slots – essentially, key-value pairs that describe features of the object to create. This decouples the data from the s..." />
<meta property="og:image" content="_static/og-image.png" />
<meta property="og:image:alt" content="Python documentation" />
<meta name="description" content="To define module objects and classes using the C API, you may use an array of slots – essentially, key-value pairs that describe features of the object to create. This decouples the data from the s..." />
<meta name="theme-color" content="#3776ab">
<meta property="og:image:width" content="200">
<meta property="og:image:height" content="200">
<title>Definition slots — Python 3.15.0rc1 documentation</title><meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" type="text/css" href="../_static/pygments.css?v=b86133f3" />
<link rel="stylesheet" type="text/css" href="../_static/classic.css?v=234b1a7c" />
<link rel="stylesheet" type="text/css" href="../_static/pydoctheme.css?v=4365c8fe" />
<link id="pygments_dark_css" media="(prefers-color-scheme: dark)" rel="stylesheet" type="text/css" href="../_static/pygments_dark.css?v=0fc419ee" />
<script src="../_static/documentation_options.js?v=791e38b1"></script>
<script src="../_static/doctools.js?v=9bcbadda"></script>
<script src="../_static/sphinx_highlight.js?v=dc90522c"></script>
<script src="../_static/sidebar.js"></script>
<link rel="search" type="application/opensearchdescription+xml"
title="Search within Python 3.15.0rc1 documentation"
href="../_static/opensearch.xml"/>
<link rel="author" title="About these documents" href="../about.html" />
<link rel="index" title="Index" href="../genindex.html" />
<link rel="search" title="Search" href="../search.html" />
<link rel="copyright" title="Copyright" href="../copyright.html" />
<link rel="next" title="Utilities" href="utilities.html" />
<link rel="prev" title="Defining extension modules" href="extension-modules.html" />
<link rel="canonical" href="https://docs.python.org/3/c-api/slots.html">
<style>
@media only screen {
table.full-width-table {
width: 100%;
}
}
</style>
<link rel="stylesheet" href="../_static/pydoctheme_dark.css" media="(prefers-color-scheme: dark)" id="pydoctheme_dark_css">
<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>
<script type="text/javascript" src="../_static/search-focus.js"></script>
<script type="text/javascript" src="../_static/themetoggle.js"></script>
<script type="text/javascript" src="../_static/rtd_switcher.js"></script>
<meta name="readthedocs-addons-api-version" content="1">
</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">
<nav class="nav-content" role="navigation">
<label for="menuToggler" class="toggler__label">
<span></span>
</label>
<span class="nav-items-wrapper">
<a href="https://www.python.org/" class="nav-logo">
<img src="../_static/py.svg" alt="Python logo">
</a>
<span class="version_switcher_placeholder"></span>
<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" fill="currentColor" 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"></path>
</svg>
<input placeholder="Quick search" aria-label="Quick search" type="search" name="q">
<input type="submit" value="Go">
</form>
</span>
</nav>
<div class="menu-wrapper">
<nav class="menu" role="navigation" aria-label="main navigation">
<div class="language_switcher_placeholder"></div>
<label class="theme-selector-label">
Theme
<select class="theme-selector" oninput="activateTheme(this.value)">
<option value="auto" selected>Auto</option>
<option value="light">Light</option>
<option value="dark">Dark</option>
</select>
</label>
<div>
<h3><a href="../contents.html">Table of Contents</a></h3>
<ul>
<li><a class="reference internal" href="#">Definition slots</a><ul>
<li><a class="reference internal" href="#convenience-macros">Convenience macros</a></li>
<li><a class="reference internal" href="#common-slot-ids">Common slot IDs</a></li>
</ul>
</li>
</ul>
</div>
<div>
<h4>Previous topic</h4>
<p class="topless"><a href="extension-modules.html"
title="previous chapter">Defining extension modules</a></p>
</div>
<div>
<h4>Next topic</h4>
<p class="topless"><a href="utilities.html"
title="next chapter">Utilities</a></p>
</div>
<script>
document.addEventListener('DOMContentLoaded', () => {
const title = document.querySelector('meta[property="og:title"]').content;
const elements = document.querySelectorAll('.improvepage');
const pageurl = window.location.href.split('?')[0];
elements.forEach(element => {
const url = new URL(element.href.split('?')[0].replace("-nojs", ""));
url.searchParams.set('pagetitle', title);
url.searchParams.set('pageurl', pageurl);
url.searchParams.set('pagesource', "c-api/slots.rst");
element.href = url.toString();
});
});
</script>
<div role="note" aria-label="source link">
<h3>This page</h3>
<ul class="this-page-menu">
<li><a href="../bugs.html">Report a bug</a></li>
<li><a class="improvepage" href="../improve-page-nojs.html">Improve this page</a></li>
<li>
<a href="https://github.com/python/cpython/blob/main/Doc/c-api/slots.rst?plain=1"
rel="nofollow">Show source
</a>
</li>
</ul>
</div>
</nav>
</div>
</div>
<div class="related" role="navigation" aria-label="Related">
<h3>Navigation</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="../genindex.html" title="General Index"
accesskey="I">index</a></li>
<li class="right" >
<a href="../py-modindex.html" title="Python Module Index"
>modules</a> |</li>
<li class="right" >
<a href="utilities.html" title="Utilities"
accesskey="N">next</a> |</li>
<li class="right" >
<a href="extension-modules.html" title="Defining extension modules"
accesskey="P">previous</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> »</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.15.0rc1 Documentation</a> »
</li>
<li class="nav-item nav-item-1"><a href="index.html" accesskey="U">Python/C API reference manual</a> »</li>
<li class="nav-item nav-item-this"><a href="">Definition slots</a></li>
<li class="right">
<div class="inline-search" role="search">
<form class="inline-search" action="../search.html" method="get">
<input placeholder="Quick search" aria-label="Quick search" type="search" name="q" id="search-box">
<input type="submit" value="Go">
</form>
</div>
|
</li>
<li class="right">
<label class="theme-selector-label">
Theme
<select class="theme-selector" oninput="activateTheme(this.value)">
<option value="auto" selected>Auto</option>
<option value="light">Light</option>
<option value="dark">Dark</option>
</select>
</label> |</li>
</ul>
</div>
<div class="document">
<div class="documentwrapper">
<div class="bodywrapper">
<div class="body" role="main">
<section id="definition-slots">
<span id="capi-slots"></span><h1>Definition slots<a class="headerlink" href="#definition-slots" title="Link to this heading">¶</a></h1>
<p>To define <a class="reference internal" href="module.html#moduleobjects"><span class="std std-ref">module objects</span></a> and
<a class="reference internal" href="type.html#creating-heap-types"><span class="std std-ref">classes</span></a> using the C API, you may use
an array of <em>slots</em> – essentially, key-value pairs that describe features
of the object to create.
This decouples the data from the structures used at runtime, allowing CPython
– and other Python C API implementations – to update the structures without
breaking backwards compatibility.</p>
<p>This section documents slots in general.
For object-specific behavior and slot values, see documentation for functions
that apply slots:</p>
<ul class="simple">
<li><p><a class="reference internal" href="type.html#c.PyType_FromSlots" title="PyType_FromSlots"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyType_FromSlots()</span></code></a> for types;</p></li>
<li><p><a class="reference internal" href="module.html#c.PyModule_FromSlotsAndSpec" title="PyModule_FromSlotsAndSpec"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyModule_FromSlotsAndSpec()</span></code></a> and <a class="reference internal" href="extension-modules.html#extension-export-hook"><span class="std std-ref">Extension export hook</span></a>
for modules.</p></li>
</ul>
<p>When slots are passed to a function that applies them, the function will
not modify the slot array, nor any data it points to (recursively).
After the function is done, the caller is allowed to modify or deallocate
the array and any data it points to (recursively), except data
explicitly marked with <a class="reference internal" href="#c.PySlot_STATIC" title="PySlot_STATIC"><code class="xref c c-macro docutils literal notranslate"><span class="pre">PySlot_STATIC</span></code></a>.</p>
<p>Except when documented otherwise, multiple slots with the same ID
(<a class="reference internal" href="#c.PySlot.sl_id" title="PySlot.sl_id"><code class="xref c c-member docutils literal notranslate"><span class="pre">sl_id</span></code></a>) may not occur in a single slots array.</p>
<div class="versionadded">
<p><span class="versionmodified added">Added in version 3.15: </span>Slot arrays generalize an earlier way of defining objects:
using <a class="reference internal" href="type.html#c.PyType_Spec" title="PyType_Spec"><code class="xref c c-type docutils literal notranslate"><span class="pre">PyType_Spec</span></code></a> with <a class="reference internal" href="type.html#c.PyType_Slot" title="PyType_Slot"><code class="xref c c-type docutils literal notranslate"><span class="pre">PyType_Slot</span></code></a> for types, and
<a class="reference internal" href="module.html#c.PyModuleDef" title="PyModuleDef"><code class="xref c c-type docutils literal notranslate"><span class="pre">PyModuleDef</span></code></a> with <a class="reference internal" href="module.html#c.PyModuleDef_Slot" title="PyModuleDef_Slot"><code class="xref c c-type docutils literal notranslate"><span class="pre">PyModuleDef_Slot</span></code></a> for modules.
The earlier API is <a class="reference internal" href="../glossary.html#term-soft-deprecated"><span class="xref std std-term">soft deprecated</span></a>; there are no plans to remove it.</p>
</div>
<p>Entries of the slots array use the following structure:</p>
<dl class="c type">
<dt class="sig sig-object c" id="c.PySlot">
<span class="k"><span class="pre">type</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">PySlot</span></span></span><a class="headerlink" href="#c.PySlot" title="Link to this definition">¶</a><br /></dt>
<dd><em class="stableabi"> Part of the <a class="reference internal" href="stable.html#stable"><span class="std std-ref">Stable ABI</span></a> (including all members) since version 3.15.</em><p>An entry in a slots array. Defined as:</p>
<div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="k">typedef</span><span class="w"> </span><span class="k">struct</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="kt">uint16_t</span><span class="w"> </span><span class="n">sl_id</span><span class="p">;</span>
<span class="w"> </span><span class="kt">uint16_t</span><span class="w"> </span><span class="n">sl_flags</span><span class="p">;</span>
<span class="w"> </span><span class="kt">uint32_t</span><span class="w"> </span><span class="n">_reserved</span><span class="p">;</span><span class="w"> </span><span class="c1">// must be 0</span>
<span class="w"> </span><span class="k">union</span><span class="w"> </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">sl_ptr</span><span class="p">;</span>
<span class="w"> </span><span class="kt">void</span><span class="w"> </span><span class="p">(</span><span class="o">*</span><span class="n">sl_func</span><span class="p">)(</span><span class="kt">void</span><span class="p">);</span>
<span class="w"> </span><span class="n">Py_ssize_t</span><span class="w"> </span><span class="n">sl_size</span><span class="p">;</span>
<span class="w"> </span><span class="kt">int64_t</span><span class="w"> </span><span class="n">sl_int64</span><span class="p">;</span>
<span class="w"> </span><span class="kt">uint64_t</span><span class="w"> </span><span class="n">sl_uint64</span><span class="p">;</span>
<span class="w"> </span><span class="p">};</span>
<span class="p">}</span><span class="w"> </span><span class="n">PySlot</span><span class="p">;</span>
</pre></div>
</div>
<dl class="c member">
<dt class="sig sig-object c" id="c.PySlot.sl_id">
<span class="n"><span class="pre">uint16_t</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">sl_id</span></span></span><a class="headerlink" href="#c.PySlot.sl_id" title="Link to this definition">¶</a><br /></dt>
<dd><p>A slot ID, chosen from:</p>
<ul class="simple">
<li><p><code class="docutils literal notranslate"><span class="pre">Py_slot_*</span></code> values documented in <a class="reference internal" href="#pyslot-common-ids"><span class="std std-ref">Common slot IDs</span></a> below;</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">Py_mod_*</span></code> values for modules, as documented in <a class="reference internal" href="module.html#c-module-slots"><span class="std std-ref">Module definition</span></a>;</p></li>
<li><p>Values for types, as documented in <a class="reference internal" href="type.html#pyslot-type-slot-ids"><span class="std std-ref">Type slot IDs</span></a>.</p></li>
</ul>
<p>A <code class="xref c c-member docutils literal notranslate"><span class="pre">sl_id</span></code> of zero (<a class="reference internal" href="#c.Py_slot_end" title="Py_slot_end"><code class="xref c c-macro docutils literal notranslate"><span class="pre">Py_slot_end</span></code></a>) marks the end of a
slots array.</p>
</dd></dl>
<dl class="c member">
<dt class="sig sig-object c" id="c.PySlot.sl_ptr">
<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">sl_ptr</span></span></span><a class="headerlink" href="#c.PySlot.sl_ptr" title="Link to this definition">¶</a><br /></dt>
<dt class="sig sig-object c" id="c.PySlot.sl_func">
<span class="kt"><span class="pre">void</span></span><span class="w"> </span><span class="p"><span class="pre">(</span></span><span class="p"><span class="pre">*</span></span><span class="sig-name descname"><span class="n"><span class="pre">sl_func</span></span></span><span class="p"><span class="pre">)</span></span><span class="p"><span class="pre">(</span></span><span class="kt"><span class="pre">void</span></span><span class="p"><span class="pre">)</span></span><a class="headerlink" href="#c.PySlot.sl_func" title="Link to this definition">¶</a><br /></dt>
<dt class="sig sig-object c" id="c.PySlot.sl_size">
<a class="reference internal" href="intro.html#c.Py_ssize_t" title="Py_ssize_t"><span class="n"><span class="pre">Py_ssize_t</span></span></a><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">sl_size</span></span></span><a class="headerlink" href="#c.PySlot.sl_size" title="Link to this definition">¶</a><br /></dt>
<dt class="sig sig-object c" id="c.PySlot.sl_int64">
<span class="n"><span class="pre">int64_t</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">sl_int64</span></span></span><a class="headerlink" href="#c.PySlot.sl_int64" title="Link to this definition">¶</a><br /></dt>
<dt class="sig sig-object c" id="c.PySlot.sl_uint64">
<span class="n"><span class="pre">uint64_t</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">sl_uint64</span></span></span><a class="headerlink" href="#c.PySlot.sl_uint64" title="Link to this definition">¶</a><br /></dt>
<dd><p>The data for the slot.
These members are part of an anonymous union;
the member to use depends on which data type is required by the slot ID:
data pointer, function pointer, size, signed or unsigned
integer, respectively.</p>
<p>Except when documented otherwise for a specific slot ID, pointers
(that is <code class="xref c c-member docutils literal notranslate"><span class="pre">sl_ptr</span></code> and <code class="xref c c-member docutils literal notranslate"><span class="pre">sl_func</span></code>) may not be NULL.</p>
</dd></dl>
<dl class="c member">
<dt class="sig sig-object c" id="c.PySlot.sl_flags">
<span class="n"><span class="pre">uint16_t</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">sl_flags</span></span></span><a class="headerlink" href="#c.PySlot.sl_flags" title="Link to this definition">¶</a><br /></dt>
<dd><p>Zero or more of the following flags, OR-ed together:</p>
<dl class="c macro">
<dt class="sig sig-object c" id="c.PySlot_STATIC">
<span class="sig-name descname"><span class="n"><span class="pre">PySlot_STATIC</span></span></span><a class="headerlink" href="#c.PySlot_STATIC" title="Link to this definition">¶</a><br /></dt>
<dd><em class="stableabi"> Part of the <a class="reference internal" href="stable.html#stable"><span class="std std-ref">Stable ABI</span></a> since version 3.15.</em><p>All data the slot points to is statically allocated and constant.
Thus, the interpreter does not need to copy the information.</p>
<p>This flag is implied for function pointers.</p>
<p>The flag applies even to data the slot points to “indirectly”,
except for slots nested via <a class="reference internal" href="#c.Py_slot_subslots" title="Py_slot_subslots"><code class="xref c c-macro docutils literal notranslate"><span class="pre">Py_slot_subslots</span></code></a> which may
have their own <code class="xref c c-macro docutils literal notranslate"><span class="pre">PySlot_STATIC</span></code> flags.
For example, if applied to a <a class="reference internal" href="typeobj.html#c.Py_tp_members" title="Py_tp_members"><code class="xref c c-macro docutils literal notranslate"><span class="pre">Py_tp_members</span></code></a> slot that
points to an array of <a class="reference internal" href="structures.html#c.PyMemberDef" title="PyMemberDef"><code class="xref c c-type docutils literal notranslate"><span class="pre">PyMemberDef</span></code></a> structures,
then the entire array, as well as the name and doc strings
in its elements, must be static and constant.</p>
</dd></dl>
<dl class="c macro">
<dt class="sig sig-object c" id="c.PySlot_INTPTR">
<span class="sig-name descname"><span class="n"><span class="pre">PySlot_INTPTR</span></span></span><a class="headerlink" href="#c.PySlot_INTPTR" title="Link to this definition">¶</a><br /></dt>
<dd><em class="stableabi"> Part of the <a class="reference internal" href="stable.html#stable"><span class="std std-ref">Stable ABI</span></a> since version 3.15.</em><p>The data is stored in <code class="docutils literal notranslate"><span class="pre">sl_ptr</span></code>; CPython will cast it to
the appropriate type.</p>
<p>This flag can simplify porting from the older <a class="reference internal" href="type.html#c.PyType_Slot" title="PyType_Slot"><code class="xref c c-type docutils literal notranslate"><span class="pre">PyType_Slot</span></code></a>
and <a class="reference internal" href="module.html#c.PyModuleDef_Slot" title="PyModuleDef_Slot"><code class="xref c c-type docutils literal notranslate"><span class="pre">PyModuleDef_Slot</span></code></a> structures.</p>
</dd></dl>
<dl class="c macro">
<dt class="sig sig-object c" id="c.PySlot_OPTIONAL">
<span class="sig-name descname"><span class="n"><span class="pre">PySlot_OPTIONAL</span></span></span><a class="headerlink" href="#c.PySlot_OPTIONAL" title="Link to this definition">¶</a><br /></dt>
<dd><em class="stableabi"> Part of the <a class="reference internal" href="stable.html#stable"><span class="std std-ref">Stable ABI</span></a> since version 3.15.</em><p>If the slot ID is unknown, the interpreter should ignore the
slot, rather than fail.</p>
<p>For example, if Python 3.16 adds a new feature with a new slot ID,attr
the corresponding slot may be marked <code class="xref c c-macro docutils literal notranslate"><span class="pre">PySlot_OPTIONAL</span></code>
so that Python 3.15 ignores it.</p>
<p>Note that the “optionality” only applies to unknown slot IDs.
This flag does not make Python skip invalid values of known slots.</p>
</dd></dl>
</dd></dl>
<div class="versionadded">
<p><span class="versionmodified added">Added in version 3.15.</span></p>
</div>
</dd></dl>
<section id="convenience-macros">
<h2>Convenience macros<a class="headerlink" href="#convenience-macros" title="Link to this heading">¶</a></h2>
<dl class="c macro">
<dt class="sig sig-object c" id="c.PySlot_DATA">
<span class="sig-name descname"><span class="n"><span class="pre">PySlot_DATA</span></span></span><span class="sig-paren">(</span><span class="n"><span class="pre">name</span></span>, <span class="n"><span class="pre">value</span></span><span class="sig-paren">)</span><a class="headerlink" href="#c.PySlot_DATA" title="Link to this definition">¶</a><br /></dt>
<dt class="sig sig-object c" id="c.PySlot_FUNC">
<span class="sig-name descname"><span class="n"><span class="pre">PySlot_FUNC</span></span></span><span class="sig-paren">(</span><span class="n"><span class="pre">name</span></span>, <span class="n"><span class="pre">value</span></span><span class="sig-paren">)</span><a class="headerlink" href="#c.PySlot_FUNC" title="Link to this definition">¶</a><br /></dt>
<dt class="sig sig-object c" id="c.PySlot_SIZE">
<span class="sig-name descname"><span class="n"><span class="pre">PySlot_SIZE</span></span></span><span class="sig-paren">(</span><span class="n"><span class="pre">name</span></span>, <span class="n"><span class="pre">value</span></span><span class="sig-paren">)</span><a class="headerlink" href="#c.PySlot_SIZE" title="Link to this definition">¶</a><br /></dt>
<dt class="sig sig-object c" id="c.PySlot_INT64">
<span class="sig-name descname"><span class="n"><span class="pre">PySlot_INT64</span></span></span><span class="sig-paren">(</span><span class="n"><span class="pre">name</span></span>, <span class="n"><span class="pre">value</span></span><span class="sig-paren">)</span><a class="headerlink" href="#c.PySlot_INT64" title="Link to this definition">¶</a><br /></dt>
<dt class="sig sig-object c" id="c.PySlot_UINT64">
<span class="sig-name descname"><span class="n"><span class="pre">PySlot_UINT64</span></span></span><span class="sig-paren">(</span><span class="n"><span class="pre">name</span></span>, <span class="n"><span class="pre">value</span></span><span class="sig-paren">)</span><a class="headerlink" href="#c.PySlot_UINT64" title="Link to this definition">¶</a><br /></dt>
<dt class="sig sig-object c" id="c.PySlot_STATIC_DATA">
<span class="sig-name descname"><span class="n"><span class="pre">PySlot_STATIC_DATA</span></span></span><span class="sig-paren">(</span><span class="n"><span class="pre">name</span></span>, <span class="n"><span class="pre">value</span></span><span class="sig-paren">)</span><a class="headerlink" href="#c.PySlot_STATIC_DATA" title="Link to this definition">¶</a><br /></dt>
<dd><em class="stableabi"> Part of the <a class="reference internal" href="stable.html#stable"><span class="std std-ref">Stable ABI</span></a> since version 3.15.</em><p>Convenience macros to define <code class="xref c c-type docutils literal notranslate"><span class="pre">PySlot</span></code> structures with
<a class="reference internal" href="#c.PySlot.sl_id" title="PySlot.sl_id"><code class="xref c c-member docutils literal notranslate"><span class="pre">sl_id</span></code></a> and a particular union member set.</p>
<p><code class="xref c c-macro docutils literal notranslate"><span class="pre">PySlot_STATIC_DATA</span></code> sets the <a class="reference internal" href="#c.PySlot_STATIC" title="PySlot_STATIC"><code class="xref c c-macro docutils literal notranslate"><span class="pre">PySlot_STATIC</span></code></a> flag;
others set no flags.</p>
<p>Note that these macros use <em>designated initializers</em>, a C language feature
that C++ added in the 2020 version of the standard.
If your code needs to be compatible with C++11 or older,
use <a class="reference internal" href="#c.PySlot_PTR" title="PySlot_PTR"><code class="xref c c-macro docutils literal notranslate"><span class="pre">PySlot_PTR</span></code></a> instead.</p>
<p>Defined as:</p>
<div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="cp">#define PySlot_DATA(NAME, VALUE) \</span>
<span class="cp"> {.sl_id=NAME, .sl_ptr=(void*)(VALUE)}</span>
<span class="cp">#define PySlot_FUNC(NAME, VALUE) \</span>
<span class="cp"> {.sl_id=NAME, .sl_func=(VALUE)}</span>
<span class="cp">#define PySlot_SIZE(NAME, VALUE) \</span>
<span class="cp"> {.sl_id=NAME, .sl_size=(VALUE)}</span>
<span class="cp">#define PySlot_INT64(NAME, VALUE) \</span>
<span class="cp"> {.sl_id=NAME, .sl_int64=(VALUE)}</span>
<span class="cp">#define PySlot_UINT64(NAME, VALUE) \</span>
<span class="cp"> {.sl_id=NAME, .sl_uint64=(VALUE)}</span>
<span class="cp">#define PySlot_STATIC_DATA(NAME, VALUE) \</span>
<span class="cp"> {.sl_id=NAME, .sl_flags=PySlot_STATIC, .sl_ptr=(VALUE)}</span>
</pre></div>
</div>
<div class="versionadded">
<p><span class="versionmodified added">Added in version 3.15.</span></p>
</div>
</dd></dl>
<dl class="c macro">
<dt class="sig sig-object c" id="c.PySlot_END">
<span class="sig-name descname"><span class="n"><span class="pre">PySlot_END</span></span></span><a class="headerlink" href="#c.PySlot_END" title="Link to this definition">¶</a><br /></dt>
<dd><em class="stableabi"> Part of the <a class="reference internal" href="stable.html#stable"><span class="std std-ref">Stable ABI</span></a> since version 3.15.</em><p>Convenience macro to mark the end of a <code class="xref c c-type docutils literal notranslate"><span class="pre">PySlot</span></code> array.</p>
<p>Defined as:</p>
<div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="cp">#define PySlot_END {0}</span>
</pre></div>
</div>
<div class="versionadded">
<p><span class="versionmodified added">Added in version 3.15.</span></p>
</div>
</dd></dl>
<dl class="c macro">
<dt class="sig sig-object c" id="c.PySlot_PTR">
<span class="sig-name descname"><span class="n"><span class="pre">PySlot_PTR</span></span></span><span class="sig-paren">(</span><span class="n"><span class="pre">name</span></span>, <span class="n"><span class="pre">value</span></span><span class="sig-paren">)</span><a class="headerlink" href="#c.PySlot_PTR" title="Link to this definition">¶</a><br /></dt>
<dt class="sig sig-object c" id="c.PySlot_PTR_STATIC">
<span class="sig-name descname"><span class="n"><span class="pre">PySlot_PTR_STATIC</span></span></span><span class="sig-paren">(</span><span class="n"><span class="pre">name</span></span>, <span class="n"><span class="pre">value</span></span><span class="sig-paren">)</span><a class="headerlink" href="#c.PySlot_PTR_STATIC" title="Link to this definition">¶</a><br /></dt>
<dd><em class="stableabi"> Part of the <a class="reference internal" href="stable.html#stable"><span class="std std-ref">Stable ABI</span></a> since version 3.15.</em><p>Convenience macros for use in C++11-compatible code.
This version of C++ does not allow setting arbitrary union members in
literals; instead, these macros set the <a class="reference internal" href="#c.PySlot_INTPTR" title="PySlot_INTPTR"><code class="xref c c-macro docutils literal notranslate"><span class="pre">PySlot_INTPTR</span></code></a> flag and cast
the value to <code class="docutils literal notranslate"><span class="pre">(void*)</span></code>.</p>
<p>Defined as:</p>
<div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="cp">#define PySlot_PTR(NAME, VALUE) \</span>
<span class="cp"> {NAME, PySlot_INTPTR, {0}, {(void*)(VALUE)}}</span>
<span class="cp">#define PySlot_PTR_STATIC(NAME, VALUE) \</span>
<span class="cp"> {NAME, PySlot_INTPTR|Py_SLOT_STATIC, {0}, {(void*)(VALUE)}}</span>
</pre></div>
</div>
<div class="versionadded">
<p><span class="versionmodified added">Added in version 3.15.</span></p>
</div>
</dd></dl>
</section>
<section id="common-slot-ids">
<span id="pyslot-common-ids"></span><h2>Common slot IDs<a class="headerlink" href="#common-slot-ids" title="Link to this heading">¶</a></h2>
<p>The following slot IDs may be used in both type and module definitions.</p>
<dl class="c macro">
<dt class="sig sig-object c" id="c.Py_slot_end">
<span class="sig-name descname"><span class="n"><span class="pre">Py_slot_end</span></span></span><a class="headerlink" href="#c.Py_slot_end" title="Link to this definition">¶</a><br /></dt>
<dd><em class="stableabi"> Part of the <a class="reference internal" href="stable.html#stable"><span class="std std-ref">Stable ABI</span></a> since version 3.15.</em><p>Marks the end of a slots array.
Defined as zero.</p>
<div class="versionadded">
<p><span class="versionmodified added">Added in version 3.15.</span></p>
</div>
</dd></dl>
<dl class="c macro">
<dt class="sig sig-object c" id="c.Py_slot_subslots">
<span class="sig-name descname"><span class="n"><span class="pre">Py_slot_subslots</span></span></span><a class="headerlink" href="#c.Py_slot_subslots" title="Link to this definition">¶</a><br /></dt>
<dd><em class="stableabi"> Part of the <a class="reference internal" href="stable.html#stable"><span class="std std-ref">Stable ABI</span></a> since version 3.15.</em><p>Nested slots array.</p>
<p>The value (<a class="reference internal" href="#c.PySlot.sl_ptr" title="PySlot.sl_ptr"><code class="xref c c-member docutils literal notranslate"><span class="pre">sl_ptr</span></code></a>) should point to an array of
<a class="reference internal" href="#c.PySlot" title="PySlot"><code class="xref c c-type docutils literal notranslate"><span class="pre">PySlot</span></code></a> structures.
The slots in the array (up to but not including the zero-ID
terminator) will be treated as if they were inserted if the current
slot array, at the point <code class="xref c c-macro docutils literal notranslate"><span class="pre">Py_slot_subslots</span></code> appears.</p>
<p>Slot nesting depth is limited to 5 levels.
This restriction may be lifted in the future.</p>
<div class="versionadded">
<p><span class="versionmodified added">Added in version 3.15.</span></p>
</div>
</dd></dl>
<dl class="c macro">
<dt class="sig sig-object c" id="c.Py_slot_invalid">
<span class="sig-name descname"><span class="n"><span class="pre">Py_slot_invalid</span></span></span><a class="headerlink" href="#c.Py_slot_invalid" title="Link to this definition">¶</a><br /></dt>
<dd><em class="stableabi"> Part of the <a class="reference internal" href="stable.html#stable"><span class="std std-ref">Stable ABI</span></a> since version 3.15.</em><p>Reserved; will always be treated as an unknown slot ID.
Defined as <code class="docutils literal notranslate"><span class="pre">UINT16_MAX</span></code> (<code class="docutils literal notranslate"><span class="pre">0xFFFF</span></code>).</p>
<p>When used with the <a class="reference internal" href="#c.PySlot_OPTIONAL" title="PySlot_OPTIONAL"><code class="xref c c-macro docutils literal notranslate"><span class="pre">PySlot_OPTIONAL</span></code></a> flag, defines a slot with
no effect.
Without the flag, processing a slot with this ID will fail.</p>
<div class="versionadded">
<p><span class="versionmodified added">Added in version 3.15.</span></p>
</div>
</dd></dl>
</section>
</section>
<div class="clearer"></div>
</div>
</div>
</div>
<div class="sphinxsidebar" role="navigation" aria-label="Main">
<div class="sphinxsidebarwrapper">
<div>
<h3><a href="../contents.html">Table of Contents</a></h3>
<ul>
<li><a class="reference internal" href="#">Definition slots</a><ul>
<li><a class="reference internal" href="#convenience-macros">Convenience macros</a></li>
<li><a class="reference internal" href="#common-slot-ids">Common slot IDs</a></li>
</ul>
</li>
</ul>
</div>
<div>
<h4>Previous topic</h4>
<p class="topless"><a href="extension-modules.html"
title="previous chapter">Defining extension modules</a></p>
</div>
<div>
<h4>Next topic</h4>
<p class="topless"><a href="utilities.html"
title="next chapter">Utilities</a></p>
</div>
<script>
document.addEventListener('DOMContentLoaded', () => {
const title = document.querySelector('meta[property="og:title"]').content;
const elements = document.querySelectorAll('.improvepage');
const pageurl = window.location.href.split('?')[0];
elements.forEach(element => {
const url = new URL(element.href.split('?')[0].replace("-nojs", ""));
url.searchParams.set('pagetitle', title);
url.searchParams.set('pageurl', pageurl);
url.searchParams.set('pagesource', "c-api/slots.rst");
element.href = url.toString();
});
});
</script>
<div role="note" aria-label="source link">
<h3>This page</h3>
<ul class="this-page-menu">
<li><a href="../bugs.html">Report a bug</a></li>
<li><a class="improvepage" href="../improve-page-nojs.html">Improve this page</a></li>
<li>
<a href="https://github.com/python/cpython/blob/main/Doc/c-api/slots.rst?plain=1"
rel="nofollow">Show source
</a>
</li>
</ul>
</div>
</div>
<div id="sidebarbutton" title="Collapse sidebar">
<span>«</span>
</div>
</div>
<div class="clearer"></div>
</div>
<div class="related" role="navigation" aria-label="Related">
<h3>Navigation</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="../genindex.html" title="General Index"
>index</a></li>
<li class="right" >
<a href="../py-modindex.html" title="Python Module Index"
>modules</a> |</li>
<li class="right" >
<a href="utilities.html" title="Utilities"
>next</a> |</li>
<li class="right" >
<a href="extension-modules.html" title="Defining extension modules"
>previous</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> »</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.15.0rc1 Documentation</a> »
</li>
<li class="nav-item nav-item-1"><a href="index.html" >Python/C API reference manual</a> »</li>
<li class="nav-item nav-item-this"><a href="">Definition slots</a></li>
<li class="right">
<div class="inline-search" role="search">
<form class="inline-search" action="../search.html" method="get">
<input placeholder="Quick search" aria-label="Quick search" type="search" name="q" id="search-box">
<input type="submit" value="Go">
</form>
</div>
|
</li>
<li class="right">
<label class="theme-selector-label">
Theme
<select class="theme-selector" oninput="activateTheme(this.value)">
<option value="auto" selected>Auto</option>
<option value="light">Light</option>
<option value="dark">Dark</option>
</select>
</label> |</li>
</ul>
</div>
<div class="footer">
© <a href="../copyright.html">Copyright</a> 2001 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>
See <a href="/license.html">History and License</a> for more information.<br>
<br>
The Python Software Foundation is a non-profit corporation.
<a href="https://www.python.org/psf/donations/">Please donate.</a>
<br>
<br>
Last updated on Aug 04, 2026 (11:13 UTC).
<a href="/bugs.html">Found a bug</a>?
<br>
Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 8.2.3.
</div>
</body>
</html>