clinic.html 163 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8" />
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0" /><meta name="generator" content="Docutils 0.17.1: http://docutils.sourceforge.net/" />
  6. <meta property="og:title" content="Argument Clinic How-To" />
  7. <meta property="og:type" content="website" />
  8. <meta property="og:url" content="https://docs.python.org/3/howto/clinic.html" />
  9. <meta property="og:site_name" content="Python documentation" />
  10. <meta property="og:description" content="author, Larry Hastings,. Source code: Tools/clinic/clinic.py. Abstract: Argument Clinic is a preprocessor for CPython C files. It was introduced in Python 3.4 with PEP 436, in order to provide intr..." />
  11. <meta property="og:image" content="https://docs.python.org/3/_static/og-image.png" />
  12. <meta property="og:image:alt" content="Python documentation" />
  13. <meta name="description" content="author, Larry Hastings,. Source code: Tools/clinic/clinic.py. Abstract: Argument Clinic is a preprocessor for CPython C files. It was introduced in Python 3.4 with PEP 436, in order to provide intr..." />
  14. <meta property="og:image:width" content="200" />
  15. <meta property="og:image:height" content="200" />
  16. <meta name="theme-color" content="#3776ab" />
  17. <title>Argument Clinic How-To &#8212; Python 3.12.0 documentation</title><meta name="viewport" content="width=device-width, initial-scale=1.0">
  18. <link rel="stylesheet" type="text/css" href="../_static/pygments.css" />
  19. <link rel="stylesheet" type="text/css" href="../_static/pydoctheme.css?digest=b37c26da2f7529d09fe70b41c4b2133fe4931a90" />
  20. <link id="pygments_dark_css" media="(prefers-color-scheme: dark)" rel="stylesheet" type="text/css" href="../_static/pygments_dark.css" />
  21. <script data-url_root="../" id="documentation_options" src="../_static/documentation_options.js"></script>
  22. <script src="../_static/jquery.js"></script>
  23. <script src="../_static/underscore.js"></script>
  24. <script src="../_static/doctools.js"></script>
  25. <script src="../_static/sidebar.js"></script>
  26. <link rel="search" type="application/opensearchdescription+xml"
  27. title="Search within Python 3.12.0 documentation"
  28. href="../_static/opensearch.xml"/>
  29. <link rel="author" title="About these documents" href="../about.html" />
  30. <link rel="index" title="Index" href="../genindex.html" />
  31. <link rel="search" title="Search" href="../search.html" />
  32. <link rel="copyright" title="Copyright" href="../copyright.html" />
  33. <link rel="next" title="Instrumenting CPython with DTrace and SystemTap" href="instrumentation.html" />
  34. <link rel="prev" title="An introduction to the ipaddress module" href="ipaddress.html" />
  35. <link rel="canonical" href="https://docs.python.org/3/howto/clinic.html" />
  36. <style>
  37. @media only screen {
  38. table.full-width-table {
  39. width: 100%;
  40. }
  41. }
  42. </style>
  43. <link rel="stylesheet" href="../_static/pydoctheme_dark.css" media="(prefers-color-scheme: dark)" id="pydoctheme_dark_css">
  44. <link rel="shortcut icon" type="image/png" href="../_static/py.svg" />
  45. <script type="text/javascript" src="../_static/copybutton.js"></script>
  46. <script type="text/javascript" src="../_static/menu.js"></script>
  47. <script type="text/javascript" src="../_static/themetoggle.js"></script>
  48. </head>
  49. <body>
  50. <div class="mobile-nav">
  51. <input type="checkbox" id="menuToggler" class="toggler__input" aria-controls="navigation"
  52. aria-pressed="false" aria-expanded="false" role="button" aria-label="Menu" />
  53. <nav class="nav-content" role="navigation">
  54. <label for="menuToggler" class="toggler__label">
  55. <span></span>
  56. </label>
  57. <span class="nav-items-wrapper">
  58. <a href="https://www.python.org/" class="nav-logo">
  59. <img src="../_static/py.svg" alt="Logo"/>
  60. </a>
  61. <span class="version_switcher_placeholder"></span>
  62. <form role="search" class="search" action="../search.html" method="get">
  63. <svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" class="search-icon">
  64. <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>
  65. </svg>
  66. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" />
  67. <input type="submit" value="Go"/>
  68. </form>
  69. </span>
  70. </nav>
  71. <div class="menu-wrapper">
  72. <nav class="menu" role="navigation" aria-label="main navigation">
  73. <div class="language_switcher_placeholder"></div>
  74. <label class="theme-selector-label">
  75. Theme
  76. <select class="theme-selector" oninput="activateTheme(this.value)">
  77. <option value="auto" selected>Auto</option>
  78. <option value="light">Light</option>
  79. <option value="dark">Dark</option>
  80. </select>
  81. </label>
  82. <div>
  83. <h3><a href="../contents.html">Table of Contents</a></h3>
  84. <ul>
  85. <li><a class="reference internal" href="#">Argument Clinic How-To</a><ul>
  86. <li><a class="reference internal" href="#background">Background</a><ul>
  87. <li><a class="reference internal" href="#basic-concepts">Basic concepts</a></li>
  88. </ul>
  89. </li>
  90. <li><a class="reference internal" href="#reference">Reference</a><ul>
  91. <li><a class="reference internal" href="#terminology">Terminology</a></li>
  92. <li><a class="reference internal" href="#command-line-interface">Command-line interface</a></li>
  93. <li><a class="reference internal" href="#module-clinic">Classes for extending Argument Clinic</a></li>
  94. </ul>
  95. </li>
  96. <li><a class="reference internal" href="#tutorial">Tutorial</a></li>
  97. <li><a class="reference internal" href="#how-to-guides">How-to guides</a><ul>
  98. <li><a class="reference internal" href="#how-to-rename-c-functions-and-variables-generated-by-argument-clinic">How to rename C functions and variables generated by Argument Clinic</a></li>
  99. <li><a class="reference internal" href="#how-to-convert-functions-using-pyarg-unpacktuple">How to convert functions using <code class="docutils literal notranslate"><span class="pre">PyArg_UnpackTuple</span></code></a></li>
  100. <li><a class="reference internal" href="#how-to-use-optional-groups">How to use optional groups</a></li>
  101. <li><a class="reference internal" href="#how-to-use-real-argument-clinic-converters-instead-of-legacy-converters">How to use real Argument Clinic converters, instead of “legacy converters”</a></li>
  102. <li><a class="reference internal" href="#how-to-use-the-py-buffer-converter">How to use the <code class="docutils literal notranslate"><span class="pre">Py_buffer</span></code> converter</a></li>
  103. <li><a class="reference internal" href="#how-to-use-advanced-converters">How to use advanced converters</a></li>
  104. <li><a class="reference internal" href="#how-to-assign-default-values-to-parameter">How to assign default values to parameter</a><ul>
  105. <li><a class="reference internal" href="#the-null-default-value">The <code class="docutils literal notranslate"><span class="pre">NULL</span></code> default value</a></li>
  106. <li><a class="reference internal" href="#symbolic-default-values">Symbolic default values</a></li>
  107. <li><a class="reference internal" href="#expressions-as-default-values">Expressions as default values</a></li>
  108. </ul>
  109. </li>
  110. <li><a class="reference internal" href="#how-to-use-return-converters">How to use return converters</a></li>
  111. <li><a class="reference internal" href="#how-to-clone-existing-functions">How to clone existing functions</a></li>
  112. <li><a class="reference internal" href="#how-to-call-python-code">How to call Python code</a></li>
  113. <li><a class="reference internal" href="#how-to-use-the-self-converter">How to use the “self converter”</a></li>
  114. <li><a class="reference internal" href="#how-to-use-the-defining-class-converter">How to use the “defining class” converter</a></li>
  115. <li><a class="reference internal" href="#how-to-write-a-custom-converter">How to write a custom converter</a></li>
  116. <li><a class="reference internal" href="#how-to-write-a-custom-return-converter">How to write a custom return converter</a></li>
  117. <li><a class="reference internal" href="#how-to-convert-meth-o-and-meth-noargs-functions">How to convert <code class="docutils literal notranslate"><span class="pre">METH_O</span></code> and <code class="docutils literal notranslate"><span class="pre">METH_NOARGS</span></code> functions</a></li>
  118. <li><a class="reference internal" href="#how-to-convert-tp-new-and-tp-init-functions">How to convert <code class="docutils literal notranslate"><span class="pre">tp_new</span></code> and <code class="docutils literal notranslate"><span class="pre">tp_init</span></code> functions</a></li>
  119. <li><a class="reference internal" href="#how-to-change-and-redirect-clinic-s-output">How to change and redirect Clinic’s output</a></li>
  120. <li><a class="reference internal" href="#how-to-use-the-ifdef-trick">How to use the <code class="docutils literal notranslate"><span class="pre">#ifdef</span></code> trick</a></li>
  121. <li><a class="reference internal" href="#how-to-use-argument-clinic-in-python-files">How to use Argument Clinic in Python files</a></li>
  122. </ul>
  123. </li>
  124. </ul>
  125. </li>
  126. </ul>
  127. </div>
  128. <div>
  129. <h4>Previous topic</h4>
  130. <p class="topless"><a href="ipaddress.html"
  131. title="previous chapter">An introduction to the ipaddress module</a></p>
  132. </div>
  133. <div>
  134. <h4>Next topic</h4>
  135. <p class="topless"><a href="instrumentation.html"
  136. title="next chapter">Instrumenting CPython with DTrace and SystemTap</a></p>
  137. </div>
  138. <div role="note" aria-label="source link">
  139. <h3>This Page</h3>
  140. <ul class="this-page-menu">
  141. <li><a href="../bugs.html">Report a Bug</a></li>
  142. <li>
  143. <a href="https://github.com/python/cpython/blob/main/Doc/howto/clinic.rst"
  144. rel="nofollow">Show Source
  145. </a>
  146. </li>
  147. </ul>
  148. </div>
  149. </nav>
  150. </div>
  151. </div>
  152. <div class="related" role="navigation" aria-label="related navigation">
  153. <h3>Navigation</h3>
  154. <ul>
  155. <li class="right" style="margin-right: 10px">
  156. <a href="../genindex.html" title="General Index"
  157. accesskey="I">index</a></li>
  158. <li class="right" >
  159. <a href="../py-modindex.html" title="Python Module Index"
  160. >modules</a> |</li>
  161. <li class="right" >
  162. <a href="instrumentation.html" title="Instrumenting CPython with DTrace and SystemTap"
  163. accesskey="N">next</a> |</li>
  164. <li class="right" >
  165. <a href="ipaddress.html" title="An introduction to the ipaddress module"
  166. accesskey="P">previous</a> |</li>
  167. <li><img src="../_static/py.svg" alt="python logo" style="vertical-align: middle; margin-top: -1px"/></li>
  168. <li><a href="https://www.python.org/">Python</a> &#187;</li>
  169. <li class="switchers">
  170. <div class="language_switcher_placeholder"></div>
  171. <div class="version_switcher_placeholder"></div>
  172. </li>
  173. <li>
  174. </li>
  175. <li id="cpython-language-and-version">
  176. <a href="../index.html">3.12.0 Documentation</a> &#187;
  177. </li>
  178. <li class="nav-item nav-item-1"><a href="index.html" accesskey="U">Python HOWTOs</a> &#187;</li>
  179. <li class="nav-item nav-item-this"><a href="">Argument Clinic How-To</a></li>
  180. <li class="right">
  181. <div class="inline-search" role="search">
  182. <form class="inline-search" action="../search.html" method="get">
  183. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" />
  184. <input type="submit" value="Go" />
  185. </form>
  186. </div>
  187. |
  188. </li>
  189. <li class="right">
  190. <label class="theme-selector-label">
  191. Theme
  192. <select class="theme-selector" oninput="activateTheme(this.value)">
  193. <option value="auto" selected>Auto</option>
  194. <option value="light">Light</option>
  195. <option value="dark">Dark</option>
  196. </select>
  197. </label> |</li>
  198. </ul>
  199. </div>
  200. <div class="document">
  201. <div class="documentwrapper">
  202. <div class="bodywrapper">
  203. <div class="body" role="main">
  204. <section id="argument-clinic-how-to">
  205. <span id="howto-clinic"></span><h1>Argument Clinic How-To<a class="headerlink" href="#argument-clinic-how-to" title="Permalink to this headline">¶</a></h1>
  206. <dl class="field-list simple">
  207. <dt class="field-odd">author</dt>
  208. <dd class="field-odd"><p>Larry Hastings</p>
  209. </dd>
  210. </dl>
  211. <p><strong>Source code:</strong> <a class="reference external" href="https://github.com/python/cpython/tree/3.12/Tools/clinic/clinic.py">Tools/clinic/clinic.py</a>.</p>
  212. <div class="topic">
  213. <p class="topic-title">Abstract</p>
  214. <p>Argument Clinic is a preprocessor for CPython C files.
  215. It was introduced in Python 3.4 with <span class="target" id="index-0"></span><a class="pep reference external" href="https://peps.python.org/pep-0436/"><strong>PEP 436</strong></a>,
  216. in order to provide introspection signatures,
  217. and to generate performant and tailor-made boilerplate code
  218. for argument parsing in CPython builtins,
  219. module level functions, and class methods.
  220. This document is divided in four major sections:</p>
  221. <ul class="simple">
  222. <li><p><a class="reference internal" href="#clinic-background"><span class="std std-ref">Background</span></a> talks about the basic concepts and goals of
  223. Argument Clinic.</p></li>
  224. <li><p><a class="reference internal" href="#clinic-reference"><span class="std std-ref">Reference</span></a> describes the command-line interface and Argument
  225. Clinic terminology.</p></li>
  226. <li><p><a class="reference internal" href="#clinic-tutorial"><span class="std std-ref">Tutorial</span></a> guides you through all the steps required to
  227. adapt an existing C function to Argument Clinic.</p></li>
  228. <li><p><a class="reference internal" href="#clinic-howtos"><span class="std std-ref">How-to guides</span></a> details how to handle specific tasks.</p></li>
  229. </ul>
  230. </div>
  231. <div class="admonition note">
  232. <p class="admonition-title">Note</p>
  233. <p>Argument Clinic is considered internal-only
  234. for CPython. Its use is not supported for files outside
  235. CPython, and no guarantees are made regarding backwards
  236. compatibility for future versions. In other words: if you
  237. maintain an external C extension for CPython, you’re welcome
  238. to experiment with Argument Clinic in your own code. But the
  239. version of Argument Clinic that ships with the next version
  240. of CPython <em>could</em> be totally incompatible and break all your code.</p>
  241. </div>
  242. <section id="background">
  243. <span id="clinic-background"></span><h2>Background<a class="headerlink" href="#background" title="Permalink to this headline">¶</a></h2>
  244. <section id="basic-concepts">
  245. <h3>Basic concepts<a class="headerlink" href="#basic-concepts" title="Permalink to this headline">¶</a></h3>
  246. <p>When Argument Clinic is run on a file, either via the <a class="reference internal" href="#clinic-cli"><span class="std std-ref">Command-line interface</span></a>
  247. or via <code class="docutils literal notranslate"><span class="pre">make</span> <span class="pre">clinic</span></code>, it will scan over the input files looking for
  248. <a class="reference internal" href="#term-start-line"><span class="xref std std-term">start lines</span></a>:</p>
  249. <div class="highlight-none notranslate"><div class="highlight"><pre><span></span>/*[clinic input]
  250. </pre></div>
  251. </div>
  252. <p>When it finds one, it reads everything up to the <a class="reference internal" href="#term-end-line"><span class="xref std std-term">end line</span></a>:</p>
  253. <div class="highlight-none notranslate"><div class="highlight"><pre><span></span>[clinic start generated code]*/
  254. </pre></div>
  255. </div>
  256. <p>Everything in between these two lines is Argument Clinic <a class="reference internal" href="#term-input"><span class="xref std std-term">input</span></a>.
  257. When Argument Clinic parses input, it generates <a class="reference internal" href="#term-output"><span class="xref std std-term">output</span></a>.
  258. The output is rewritten into the C file immediately after the input,
  259. followed by a <a class="reference internal" href="#term-checksum-line"><span class="xref std std-term">checksum line</span></a>.
  260. All of these lines, including the <a class="reference internal" href="#term-start-line"><span class="xref std std-term">start line</span></a> and <a class="reference internal" href="#term-checksum-line"><span class="xref std std-term">checksum line</span></a>,
  261. are collectively called an Argument Clinic <a class="reference internal" href="#term-block"><span class="xref std std-term">block</span></a>:</p>
  262. <div class="highlight-none notranslate"><div class="highlight"><pre><span></span>/*[clinic input]
  263. ... clinic input goes here ...
  264. [clinic start generated code]*/
  265. ... clinic output goes here ...
  266. /*[clinic end generated code: ...]*/
  267. </pre></div>
  268. </div>
  269. <p>If you run Argument Clinic on the same file a second time, Argument Clinic
  270. will discard the old <a class="reference internal" href="#term-output"><span class="xref std std-term">output</span></a> and write out the new output with a fresh
  271. <a class="reference internal" href="#term-checksum-line"><span class="xref std std-term">checksum line</span></a>.
  272. If the <a class="reference internal" href="#term-input"><span class="xref std std-term">input</span></a> hasn’t changed, the output won’t change either.</p>
  273. <div class="admonition note">
  274. <p class="admonition-title">Note</p>
  275. <p>You should never modify the output of an Argument Clinic block,
  276. as any change will be lost in future Argument Clinic runs;
  277. Argument Clinic will detect an output checksum mismatch and regenerate the
  278. correct output.
  279. If you are not happy with the generated output,
  280. you should instead change the input until it produces the output you want.</p>
  281. </div>
  282. </section>
  283. </section>
  284. <section id="reference">
  285. <span id="clinic-reference"></span><h2>Reference<a class="headerlink" href="#reference" title="Permalink to this headline">¶</a></h2>
  286. <section id="terminology">
  287. <span id="clinic-terminology"></span><h3>Terminology<a class="headerlink" href="#terminology" title="Permalink to this headline">¶</a></h3>
  288. <dl class="glossary simple">
  289. <dt id="term-start-line">start line<a class="headerlink" href="#term-start-line" title="Permalink to this term">¶</a></dt><dd><p>The line <code class="docutils literal notranslate"><span class="pre">/*[clinic</span> <span class="pre">input]</span></code>.
  290. This line marks the beginning of Argument Clinic input.
  291. Note that the <em>start line</em> opens a C block comment.</p>
  292. </dd>
  293. <dt id="term-end-line">end line<a class="headerlink" href="#term-end-line" title="Permalink to this term">¶</a></dt><dd><p>The line <code class="docutils literal notranslate"><span class="pre">[clinic</span> <span class="pre">start</span> <span class="pre">generated</span> <span class="pre">code]*/</span></code>.
  294. The <em>end line</em> marks the _end_ of Argument Clinic <a class="reference internal" href="#term-input"><span class="xref std std-term">input</span></a>,
  295. but at the same time marks the _start_ of Argument Clinic <a class="reference internal" href="#term-output"><span class="xref std std-term">output</span></a>,
  296. thus the text <em>“clinic start start generated code”</em>
  297. Note that the <em>end line</em> closes the C block comment opened
  298. by the <em>start line</em>.</p>
  299. </dd>
  300. <dt id="term-checksum">checksum<a class="headerlink" href="#term-checksum" title="Permalink to this term">¶</a></dt><dd><p>A hash to distinguish unique <a class="reference internal" href="#term-input"><span class="xref std std-term">inputs</span></a>
  301. and <a class="reference internal" href="#term-output"><span class="xref std std-term">outputs</span></a>.</p>
  302. </dd>
  303. <dt id="term-checksum-line">checksum line<a class="headerlink" href="#term-checksum-line" title="Permalink to this term">¶</a></dt><dd><p>A line that looks like <code class="docutils literal notranslate"><span class="pre">/*[clinic</span> <span class="pre">end</span> <span class="pre">generated</span> <span class="pre">code:</span> <span class="pre">...]*/</span></code>.
  304. The three dots will be replaced by a <a class="reference internal" href="#term-checksum"><span class="xref std std-term">checksum</span></a> generated from the
  305. <a class="reference internal" href="#term-input"><span class="xref std std-term">input</span></a>, and a <a class="reference internal" href="#term-checksum"><span class="xref std std-term">checksum</span></a> generated from the <a class="reference internal" href="#term-output"><span class="xref std std-term">output</span></a>.
  306. The checksum line marks the end of Argument Clinic generated code,
  307. and is used by Argument Clinic to determine if it needs to regenerate
  308. output.</p>
  309. </dd>
  310. <dt id="term-input">input<a class="headerlink" href="#term-input" title="Permalink to this term">¶</a></dt><dd><p>The text between the <a class="reference internal" href="#term-start-line"><span class="xref std std-term">start line</span></a> and the <a class="reference internal" href="#term-end-line"><span class="xref std std-term">end line</span></a>.
  311. Note that the start and end lines open and close a C block comment;
  312. the <em>input</em> is thus a part of that same C block comment.</p>
  313. </dd>
  314. <dt id="term-output">output<a class="headerlink" href="#term-output" title="Permalink to this term">¶</a></dt><dd><p>The text between the <a class="reference internal" href="#term-end-line"><span class="xref std std-term">end line</span></a> and the <a class="reference internal" href="#term-checksum-line"><span class="xref std std-term">checksum line</span></a>.</p>
  315. </dd>
  316. <dt id="term-block">block<a class="headerlink" href="#term-block" title="Permalink to this term">¶</a></dt><dd><p>All text from the <a class="reference internal" href="#term-start-line"><span class="xref std std-term">start line</span></a> to the <a class="reference internal" href="#term-checksum-line"><span class="xref std std-term">checksum line</span></a> inclusively.</p>
  317. </dd>
  318. </dl>
  319. </section>
  320. <section id="command-line-interface">
  321. <span id="clinic-cli"></span><h3>Command-line interface<a class="headerlink" href="#command-line-interface" title="Permalink to this headline">¶</a></h3>
  322. <p>The Argument Clinic <abbr title="Command-Line Interface">CLI</abbr> is typically used to
  323. process a single source file, like this:</p>
  324. <div class="highlight-shell-session notranslate"><div class="highlight"><pre><span></span><span class="gp">$ </span>python3<span class="w"> </span>./Tools/clinic/clinic.py<span class="w"> </span>foo.c
  325. </pre></div>
  326. </div>
  327. <p>The CLI supports the following options:</p>
  328. <dl class="std option">
  329. <dt class="sig sig-object std" id="cmdoption-.-Tools-clinic-clinic.py-h-f-o-OUTPUT-v-converters-make-srcdir-SRCDIR-FILE-...-h">
  330. <span id="cmdoption-tools-clinic-clinic-py-h-f-o-output-v-converters-make-srcdir-srcdir-file-h"></span><span id="cmdoption-.-Tools-clinic-clinic.py-h-f-o-OUTPUT-v-converters-make-srcdir-SRCDIR-FILE-...-help"></span><span id="cmdoption-tools-clinic-clinic-py-h-f-o-output-v-converters-make-srcdir-srcdir-file-help"></span><span class="sig-name descname"><span class="pre">-h</span></span><span class="sig-prename descclassname"></span><span class="sig-prename descclassname"><span class="pre">,</span> </span><span class="sig-name descname"><span class="pre">--help</span></span><span class="sig-prename descclassname"></span><a class="headerlink" href="#cmdoption-.-Tools-clinic-clinic.py-h-f-o-OUTPUT-v-converters-make-srcdir-SRCDIR-FILE-...-h" title="Permalink to this definition">¶</a></dt>
  331. <dd><p>Print CLI usage.</p>
  332. </dd></dl>
  333. <dl class="std option">
  334. <dt class="sig sig-object std" id="cmdoption-.-Tools-clinic-clinic.py-h-f-o-OUTPUT-v-converters-make-srcdir-SRCDIR-FILE-...-f">
  335. <span id="cmdoption-tools-clinic-clinic-py-h-f-o-output-v-converters-make-srcdir-srcdir-file-f"></span><span id="cmdoption-.-Tools-clinic-clinic.py-h-f-o-OUTPUT-v-converters-make-srcdir-SRCDIR-FILE-...-force"></span><span id="cmdoption-tools-clinic-clinic-py-h-f-o-output-v-converters-make-srcdir-srcdir-file-force"></span><span class="sig-name descname"><span class="pre">-f</span></span><span class="sig-prename descclassname"></span><span class="sig-prename descclassname"><span class="pre">,</span> </span><span class="sig-name descname"><span class="pre">--force</span></span><span class="sig-prename descclassname"></span><a class="headerlink" href="#cmdoption-.-Tools-clinic-clinic.py-h-f-o-OUTPUT-v-converters-make-srcdir-SRCDIR-FILE-...-f" title="Permalink to this definition">¶</a></dt>
  336. <dd><p>Force output regeneration.</p>
  337. </dd></dl>
  338. <dl class="std option">
  339. <dt class="sig sig-object std" id="cmdoption-.-Tools-clinic-clinic.py-h-f-o-OUTPUT-v-converters-make-srcdir-SRCDIR-FILE-...-o">
  340. <span id="cmdoption-tools-clinic-clinic-py-h-f-o-output-v-converters-make-srcdir-srcdir-file-o"></span><span id="cmdoption-.-Tools-clinic-clinic.py-h-f-o-OUTPUT-v-converters-make-srcdir-SRCDIR-FILE-...-output"></span><span id="cmdoption-tools-clinic-clinic-py-h-f-o-output-v-converters-make-srcdir-srcdir-file-output"></span><span class="sig-name descname"><span class="pre">-o</span></span><span class="sig-prename descclassname"></span><span class="sig-prename descclassname"><span class="pre">,</span> </span><span class="sig-name descname"><span class="pre">--output</span></span><span class="sig-prename descclassname"> <span class="pre">OUTPUT</span></span><a class="headerlink" href="#cmdoption-.-Tools-clinic-clinic.py-h-f-o-OUTPUT-v-converters-make-srcdir-SRCDIR-FILE-...-o" title="Permalink to this definition">¶</a></dt>
  341. <dd><p>Redirect file output to OUTPUT</p>
  342. </dd></dl>
  343. <dl class="std option">
  344. <dt class="sig sig-object std" id="cmdoption-.-Tools-clinic-clinic.py-h-f-o-OUTPUT-v-converters-make-srcdir-SRCDIR-FILE-...-v">
  345. <span id="cmdoption-tools-clinic-clinic-py-h-f-o-output-v-converters-make-srcdir-srcdir-file-v"></span><span id="cmdoption-.-Tools-clinic-clinic.py-h-f-o-OUTPUT-v-converters-make-srcdir-SRCDIR-FILE-...-verbose"></span><span id="cmdoption-tools-clinic-clinic-py-h-f-o-output-v-converters-make-srcdir-srcdir-file-verbose"></span><span class="sig-name descname"><span class="pre">-v</span></span><span class="sig-prename descclassname"></span><span class="sig-prename descclassname"><span class="pre">,</span> </span><span class="sig-name descname"><span class="pre">--verbose</span></span><span class="sig-prename descclassname"></span><a class="headerlink" href="#cmdoption-.-Tools-clinic-clinic.py-h-f-o-OUTPUT-v-converters-make-srcdir-SRCDIR-FILE-...-v" title="Permalink to this definition">¶</a></dt>
  346. <dd><p>Enable verbose mode.</p>
  347. </dd></dl>
  348. <dl class="std option">
  349. <dt class="sig sig-object std" id="cmdoption-.-Tools-clinic-clinic.py-h-f-o-OUTPUT-v-converters-make-srcdir-SRCDIR-FILE-...-converters">
  350. <span id="cmdoption-tools-clinic-clinic-py-h-f-o-output-v-converters-make-srcdir-srcdir-file-converters"></span><span class="sig-name descname"><span class="pre">--converters</span></span><span class="sig-prename descclassname"></span><a class="headerlink" href="#cmdoption-.-Tools-clinic-clinic.py-h-f-o-OUTPUT-v-converters-make-srcdir-SRCDIR-FILE-...-converters" title="Permalink to this definition">¶</a></dt>
  351. <dd><p>Print a list of all supported converters and return converters.</p>
  352. </dd></dl>
  353. <dl class="std option">
  354. <dt class="sig sig-object std" id="cmdoption-.-Tools-clinic-clinic.py-h-f-o-OUTPUT-v-converters-make-srcdir-SRCDIR-FILE-...-make">
  355. <span id="cmdoption-tools-clinic-clinic-py-h-f-o-output-v-converters-make-srcdir-srcdir-file-make"></span><span class="sig-name descname"><span class="pre">--make</span></span><span class="sig-prename descclassname"></span><a class="headerlink" href="#cmdoption-.-Tools-clinic-clinic.py-h-f-o-OUTPUT-v-converters-make-srcdir-SRCDIR-FILE-...-make" title="Permalink to this definition">¶</a></dt>
  356. <dd><p>Walk <a class="reference internal" href="#cmdoption-.-Tools-clinic-clinic.py-h-f-o-OUTPUT-v-converters-make-srcdir-SRCDIR-FILE-...-srcdir"><code class="xref std std-option docutils literal notranslate"><span class="pre">--srcdir</span></code></a> to run over all relevant files.</p>
  357. </dd></dl>
  358. <dl class="std option">
  359. <dt class="sig sig-object std" id="cmdoption-.-Tools-clinic-clinic.py-h-f-o-OUTPUT-v-converters-make-srcdir-SRCDIR-FILE-...-srcdir">
  360. <span id="cmdoption-tools-clinic-clinic-py-h-f-o-output-v-converters-make-srcdir-srcdir-file-srcdir"></span><span class="sig-name descname"><span class="pre">--srcdir</span></span><span class="sig-prename descclassname"> <span class="pre">SRCDIR</span></span><a class="headerlink" href="#cmdoption-.-Tools-clinic-clinic.py-h-f-o-OUTPUT-v-converters-make-srcdir-SRCDIR-FILE-...-srcdir" title="Permalink to this definition">¶</a></dt>
  361. <dd><p>The directory tree to walk in <a class="reference internal" href="#cmdoption-.-Tools-clinic-clinic.py-h-f-o-OUTPUT-v-converters-make-srcdir-SRCDIR-FILE-...-make"><code class="xref std std-option docutils literal notranslate"><span class="pre">--make</span></code></a> mode.</p>
  362. </dd></dl>
  363. <dl class="std option">
  364. <dt class="sig sig-object std" id="cmdoption-.-Tools-clinic-clinic.py-h-f-o-OUTPUT-v-converters-make-srcdir-SRCDIR-FILE-...-arg-FILE">
  365. <span id="cmdoption-tools-clinic-clinic-py-h-f-o-output-v-converters-make-srcdir-srcdir-file-arg-file"></span><span class="sig-name descname"><span class="pre">FILE</span></span><span class="sig-prename descclassname"> <span class="pre">...</span></span><a class="headerlink" href="#cmdoption-.-Tools-clinic-clinic.py-h-f-o-OUTPUT-v-converters-make-srcdir-SRCDIR-FILE-...-arg-FILE" title="Permalink to this definition">¶</a></dt>
  366. <dd><p>The list of files to process.</p>
  367. </dd></dl>
  368. </section>
  369. <section id="module-clinic">
  370. <span id="classes-for-extending-argument-clinic"></span><span id="clinic-classes"></span><h3>Classes for extending Argument Clinic<a class="headerlink" href="#module-clinic" title="Permalink to this headline">¶</a></h3>
  371. <dl class="py class">
  372. <dt class="sig sig-object py" id="clinic.CConverter">
  373. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">clinic.</span></span><span class="sig-name descname"><span class="pre">CConverter</span></span><a class="headerlink" href="#clinic.CConverter" title="Permalink to this definition">¶</a></dt>
  374. <dd><p>The base class for all converters.
  375. See <a class="reference internal" href="#clinic-howto-custom-converter"><span class="std std-ref">How to write a custom converter</span></a> for how to subclass this class.</p>
  376. <dl class="py attribute">
  377. <dt class="sig sig-object py" id="clinic.CConverter.type">
  378. <span class="sig-name descname"><span class="pre">type</span></span><a class="headerlink" href="#clinic.CConverter.type" title="Permalink to this definition">¶</a></dt>
  379. <dd><p>The C type to use for this variable.
  380. <code class="xref py py-attr docutils literal notranslate"><span class="pre">type</span></code> should be a Python string specifying the type,
  381. e.g. <code class="docutils literal notranslate"><span class="pre">'int'</span></code>.
  382. If this is a pointer type, the type string should end with <code class="docutils literal notranslate"><span class="pre">'</span> <span class="pre">*'</span></code>.</p>
  383. </dd></dl>
  384. <dl class="py attribute">
  385. <dt class="sig sig-object py" id="clinic.CConverter.default">
  386. <span class="sig-name descname"><span class="pre">default</span></span><a class="headerlink" href="#clinic.CConverter.default" title="Permalink to this definition">¶</a></dt>
  387. <dd><p>The Python default value for this parameter, as a Python value.
  388. Or the magic value <code class="docutils literal notranslate"><span class="pre">unspecified</span></code> if there is no default.</p>
  389. </dd></dl>
  390. <dl class="py attribute">
  391. <dt class="sig sig-object py" id="clinic.CConverter.py_default">
  392. <span class="sig-name descname"><span class="pre">py_default</span></span><a class="headerlink" href="#clinic.CConverter.py_default" title="Permalink to this definition">¶</a></dt>
  393. <dd><p><code class="xref py py-attr docutils literal notranslate"><span class="pre">default</span></code> as it should appear in Python code,
  394. as a string.
  395. Or <code class="docutils literal notranslate"><span class="pre">None</span></code> if there is no default.</p>
  396. </dd></dl>
  397. <dl class="py attribute">
  398. <dt class="sig sig-object py" id="clinic.CConverter.c_default">
  399. <span class="sig-name descname"><span class="pre">c_default</span></span><a class="headerlink" href="#clinic.CConverter.c_default" title="Permalink to this definition">¶</a></dt>
  400. <dd><p><code class="xref py py-attr docutils literal notranslate"><span class="pre">default</span></code> as it should appear in C code,
  401. as a string.
  402. Or <code class="docutils literal notranslate"><span class="pre">None</span></code> if there is no default.</p>
  403. </dd></dl>
  404. <dl class="py attribute">
  405. <dt class="sig sig-object py" id="clinic.CConverter.c_ignored_default">
  406. <span class="sig-name descname"><span class="pre">c_ignored_default</span></span><a class="headerlink" href="#clinic.CConverter.c_ignored_default" title="Permalink to this definition">¶</a></dt>
  407. <dd><p>The default value used to initialize the C variable when
  408. there is no default, but not specifying a default may
  409. result in an “uninitialized variable” warning. This can
  410. easily happen when using option groups—although
  411. properly written code will never actually use this value,
  412. the variable does get passed in to the impl, and the
  413. C compiler will complain about the “use” of the
  414. uninitialized value. This value should always be a
  415. non-empty string.</p>
  416. </dd></dl>
  417. <dl class="py attribute">
  418. <dt class="sig sig-object py" id="clinic.CConverter.converter">
  419. <span class="sig-name descname"><span class="pre">converter</span></span><a class="headerlink" href="#clinic.CConverter.converter" title="Permalink to this definition">¶</a></dt>
  420. <dd><p>The name of the C converter function, as a string.</p>
  421. </dd></dl>
  422. <dl class="py attribute">
  423. <dt class="sig sig-object py" id="clinic.CConverter.impl_by_reference">
  424. <span class="sig-name descname"><span class="pre">impl_by_reference</span></span><a class="headerlink" href="#clinic.CConverter.impl_by_reference" title="Permalink to this definition">¶</a></dt>
  425. <dd><p>A boolean value. If true,
  426. Argument Clinic will add a <code class="docutils literal notranslate"><span class="pre">&amp;</span></code> in front of the name of
  427. the variable when passing it into the impl function.</p>
  428. </dd></dl>
  429. <dl class="py attribute">
  430. <dt class="sig sig-object py" id="clinic.CConverter.parse_by_reference">
  431. <span class="sig-name descname"><span class="pre">parse_by_reference</span></span><a class="headerlink" href="#clinic.CConverter.parse_by_reference" title="Permalink to this definition">¶</a></dt>
  432. <dd><p>A boolean value. If true,
  433. Argument Clinic will add a <code class="docutils literal notranslate"><span class="pre">&amp;</span></code> in front of the name of
  434. the variable when passing it into <a class="reference internal" href="../c-api/arg.html#c.PyArg_ParseTuple" title="PyArg_ParseTuple"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyArg_ParseTuple()</span></code></a>.</p>
  435. </dd></dl>
  436. </dd></dl>
  437. </section>
  438. </section>
  439. <section id="tutorial">
  440. <span id="clinic-tutorial"></span><h2>Tutorial<a class="headerlink" href="#tutorial" title="Permalink to this headline">¶</a></h2>
  441. <p>The best way to get a sense of how Argument Clinic works is to
  442. convert a function to work with it. Here, then, are the bare
  443. minimum steps you’d need to follow to convert a function to
  444. work with Argument Clinic. Note that for code you plan to
  445. check in to CPython, you really should take the conversion farther,
  446. using some of the <a class="reference internal" href="#clinic-howtos"><span class="std std-ref">advanced concepts</span></a>
  447. you’ll see later on in the document,
  448. like <a class="reference internal" href="#clinic-howto-return-converters"><span class="std std-ref">How to use return converters</span></a>
  449. and <a class="reference internal" href="#clinic-howto-self-converter"><span class="std std-ref">How to use the “self converter”</span></a>.
  450. But we’ll keep it simple for this walkthrough so you can learn.</p>
  451. <p>First, make sure you’re working with a freshly updated checkout
  452. of the CPython trunk.</p>
  453. <p>Next, find a Python builtin that calls either <a class="reference internal" href="../c-api/arg.html#c.PyArg_ParseTuple" title="PyArg_ParseTuple"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyArg_ParseTuple()</span></code></a>
  454. or <a class="reference internal" href="../c-api/arg.html#c.PyArg_ParseTupleAndKeywords" title="PyArg_ParseTupleAndKeywords"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyArg_ParseTupleAndKeywords()</span></code></a>, and hasn’t been converted
  455. to work with Argument Clinic yet.
  456. For this tutorial, we’ll be using
  457. <a class="reference internal" href="../library/pickle.html#pickle.Pickler.dump" title="pickle.Pickler.dump"><code class="xref py py-meth docutils literal notranslate"><span class="pre">_pickle.Pickler.dump</span></code></a>.</p>
  458. <p>If the call to the <code class="xref c c-func docutils literal notranslate"><span class="pre">PyArg_Parse*()</span></code> function uses any of the
  459. following format units…:</p>
  460. <blockquote>
  461. <div><div class="highlight-none notranslate"><div class="highlight"><pre><span></span>O&amp;
  462. O!
  463. es
  464. es#
  465. et
  466. et#
  467. </pre></div>
  468. </div>
  469. </div></blockquote>
  470. <p>… or if it has multiple calls to <a class="reference internal" href="../c-api/arg.html#c.PyArg_ParseTuple" title="PyArg_ParseTuple"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyArg_ParseTuple()</span></code></a>,
  471. you should choose a different function.
  472. (See <a class="reference internal" href="#clinic-howto-advanced-converters"><span class="std std-ref">How to use advanced converters</span></a> for those scenarios.)</p>
  473. <p>Also, if the function has multiple calls to <code class="xref c c-func docutils literal notranslate"><span class="pre">PyArg_ParseTuple()</span></code>
  474. or <a class="reference internal" href="../c-api/arg.html#c.PyArg_ParseTupleAndKeywords" title="PyArg_ParseTupleAndKeywords"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyArg_ParseTupleAndKeywords()</span></code></a> where it supports different
  475. types for the same argument, or if the function uses something besides
  476. <code class="xref c c-func docutils literal notranslate"><span class="pre">PyArg_Parse*()</span></code> functions to parse its arguments, it probably
  477. isn’t suitable for conversion to Argument Clinic. Argument Clinic
  478. doesn’t support generic functions or polymorphic parameters.</p>
  479. <p>Next, add the following boilerplate above the function,
  480. creating our input block:</p>
  481. <div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="cm">/*[clinic input]</span>
  482. <span class="cm">[clinic start generated code]*/</span>
  483. </pre></div>
  484. </div>
  485. <p>Cut the docstring and paste it in between the <code class="docutils literal notranslate"><span class="pre">[clinic]</span></code> lines,
  486. removing all the junk that makes it a properly quoted C string.
  487. When you’re done you should have just the text, based at the left
  488. margin, with no line wider than 80 characters.
  489. Argument Clinic will preserve indents inside the docstring.</p>
  490. <p>If the old docstring had a first line that looked like a function
  491. signature, throw that line away; The docstring doesn’t need it anymore —
  492. when you use <a class="reference internal" href="../library/functions.html#help" title="help"><code class="xref py py-func docutils literal notranslate"><span class="pre">help()</span></code></a> on your builtin in the future,
  493. the first line will be built automatically based on the function’s signature.</p>
  494. <p>Example docstring summary line:</p>
  495. <div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="cm">/*[clinic input]</span>
  496. <span class="cm">Write a pickled representation of obj to the open file.</span>
  497. <span class="cm">[clinic start generated code]*/</span>
  498. </pre></div>
  499. </div>
  500. <p>If your docstring doesn’t have a “summary” line, Argument Clinic will
  501. complain, so let’s make sure it has one. The “summary” line should
  502. be a paragraph consisting of a single 80-column line
  503. at the beginning of the docstring.
  504. (See <span class="target" id="index-1"></span><a class="pep reference external" href="https://peps.python.org/pep-0257/"><strong>PEP 257</strong></a> regarding docstring conventions.)</p>
  505. <p>Our example docstring consists solely of a summary line, so the sample
  506. code doesn’t have to change for this step.</p>
  507. <p>Now, above the docstring, enter the name of the function, followed
  508. by a blank line. This should be the Python name of the function,
  509. and should be the full dotted path to the function —
  510. it should start with the name of the module,
  511. include any sub-modules, and if the function is a method on
  512. a class it should include the class name too.</p>
  513. <p>In our example, <code class="xref py py-mod docutils literal notranslate"><span class="pre">_pickle</span></code> is the module, <code class="xref py py-class docutils literal notranslate"><span class="pre">Pickler</span></code> is the class,
  514. and <code class="xref py py-meth docutils literal notranslate"><span class="pre">dump()</span></code> is the method, so the name becomes
  515. <code class="xref py py-meth docutils literal notranslate"><span class="pre">_pickle.Pickler.dump()</span></code>:</p>
  516. <div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="cm">/*[clinic input]</span>
  517. <span class="cm">_pickle.Pickler.dump</span>
  518. <span class="cm">Write a pickled representation of obj to the open file.</span>
  519. <span class="cm">[clinic start generated code]*/</span>
  520. </pre></div>
  521. </div>
  522. <p>If this is the first time that module or class has been used with Argument
  523. Clinic in this C file,
  524. you must declare the module and/or class. Proper Argument Clinic hygiene
  525. prefers declaring these in a separate block somewhere near the
  526. top of the C file, in the same way that include files and statics go at
  527. the top.
  528. In our sample code we’ll just show the two blocks next to each other.</p>
  529. <p>The name of the class and module should be the same as the one
  530. seen by Python. Check the name defined in the <a class="reference internal" href="../c-api/module.html#c.PyModuleDef" title="PyModuleDef"><code class="xref c c-type docutils literal notranslate"><span class="pre">PyModuleDef</span></code></a>
  531. or <a class="reference internal" href="../c-api/type.html#c.PyTypeObject" title="PyTypeObject"><code class="xref c c-type docutils literal notranslate"><span class="pre">PyTypeObject</span></code></a> as appropriate.</p>
  532. <p>When you declare a class, you must also specify two aspects of its type
  533. in C: the type declaration you’d use for a pointer to an instance of
  534. this class, and a pointer to the <code class="xref c c-type docutils literal notranslate"><span class="pre">PyTypeObject</span></code> for this class:</p>
  535. <div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="cm">/*[clinic input]</span>
  536. <span class="cm">module _pickle</span>
  537. <span class="cm">class _pickle.Pickler &quot;PicklerObject *&quot; &quot;&amp;Pickler_Type&quot;</span>
  538. <span class="cm">[clinic start generated code]*/</span>
  539. <span class="cm">/*[clinic input]</span>
  540. <span class="cm">_pickle.Pickler.dump</span>
  541. <span class="cm">Write a pickled representation of obj to the open file.</span>
  542. <span class="cm">[clinic start generated code]*/</span>
  543. </pre></div>
  544. </div>
  545. <p>Declare each of the parameters to the function. Each parameter
  546. should get its own line. All the parameter lines should be
  547. indented from the function name and the docstring.
  548. The general form of these parameter lines is as follows:</p>
  549. <div class="highlight-none notranslate"><div class="highlight"><pre><span></span>name_of_parameter: converter
  550. </pre></div>
  551. </div>
  552. <p>If the parameter has a default value, add that after the
  553. converter:</p>
  554. <div class="highlight-none notranslate"><div class="highlight"><pre><span></span>name_of_parameter: converter = default_value
  555. </pre></div>
  556. </div>
  557. <p>Argument Clinic’s support for “default values” is quite sophisticated;
  558. see <a class="reference internal" href="#clinic-howto-default-values"><span class="std std-ref">How to assign default values to parameter</span></a> for more information.</p>
  559. <p>Next, add a blank line below the parameters.</p>
  560. <p>What’s a “converter”?
  561. It establishes both the type of the variable used in C,
  562. and the method to convert the Python value into a C value at runtime.
  563. For now you’re going to use what’s called a “legacy converter” —
  564. a convenience syntax intended to make porting old code into Argument
  565. Clinic easier.</p>
  566. <p>For each parameter, copy the “format unit” for that
  567. parameter from the <a class="reference internal" href="../c-api/arg.html#c.PyArg_Parse" title="PyArg_Parse"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyArg_Parse()</span></code></a> format argument and
  568. specify <em>that</em> as its converter, as a quoted string.
  569. The “format unit” is the formal name for the one-to-three
  570. character substring of the <em>format</em> parameter that tells
  571. the argument parsing function what the type of the variable
  572. is and how to convert it.
  573. For more on format units please see <a class="reference internal" href="../c-api/arg.html#arg-parsing"><span class="std std-ref">Parsing arguments and building values</span></a>.</p>
  574. <p>For multicharacter format units like <code class="docutils literal notranslate"><span class="pre">z#</span></code>,
  575. use the entire two-or-three character string.</p>
  576. <p>Sample:</p>
  577. <div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="cm">/*[clinic input]</span>
  578. <span class="cm">module _pickle</span>
  579. <span class="cm">class _pickle.Pickler &quot;PicklerObject *&quot; &quot;&amp;Pickler_Type&quot;</span>
  580. <span class="cm">[clinic start generated code]*/</span>
  581. <span class="cm">/*[clinic input]</span>
  582. <span class="cm">_pickle.Pickler.dump</span>
  583. <span class="cm"> obj: &#39;O&#39;</span>
  584. <span class="cm">Write a pickled representation of obj to the open file.</span>
  585. <span class="cm">[clinic start generated code]*/</span>
  586. </pre></div>
  587. </div>
  588. <p>If your function has <code class="docutils literal notranslate"><span class="pre">|</span></code> in the format string,
  589. meaning some parameters have default values, you can ignore it.
  590. Argument Clinic infers which parameters are optional
  591. based on whether or not they have default values.</p>
  592. <p>If your function has <code class="docutils literal notranslate"><span class="pre">$</span></code> in the format string,
  593. meaning it takes keyword-only arguments,
  594. specify <code class="docutils literal notranslate"><span class="pre">*</span></code> on a line by itself before the first keyword-only argument,
  595. indented the same as the parameter lines.</p>
  596. <p><code class="xref py py-meth docutils literal notranslate"><span class="pre">_pickle.Pickler.dump()</span></code> has neither, so our sample is unchanged.</p>
  597. <p>Next, if the existing C function calls <a class="reference internal" href="../c-api/arg.html#c.PyArg_ParseTuple" title="PyArg_ParseTuple"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyArg_ParseTuple()</span></code></a>
  598. (as opposed to <a class="reference internal" href="../c-api/arg.html#c.PyArg_ParseTupleAndKeywords" title="PyArg_ParseTupleAndKeywords"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyArg_ParseTupleAndKeywords()</span></code></a>), then all its
  599. arguments are positional-only.</p>
  600. <p>To mark parameters as positional-only in Argument Clinic,
  601. add a <code class="docutils literal notranslate"><span class="pre">/</span></code> on a line by itself after the last positional-only parameter,
  602. indented the same as the parameter lines.</p>
  603. <p>Sample:</p>
  604. <div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="cm">/*[clinic input]</span>
  605. <span class="cm">module _pickle</span>
  606. <span class="cm">class _pickle.Pickler &quot;PicklerObject *&quot; &quot;&amp;Pickler_Type&quot;</span>
  607. <span class="cm">[clinic start generated code]*/</span>
  608. <span class="cm">/*[clinic input]</span>
  609. <span class="cm">_pickle.Pickler.dump</span>
  610. <span class="cm"> obj: &#39;O&#39;</span>
  611. <span class="cm"> /</span>
  612. <span class="cm">Write a pickled representation of obj to the open file.</span>
  613. <span class="cm">[clinic start generated code]*/</span>
  614. </pre></div>
  615. </div>
  616. <p>It can be helpful to write a per-parameter docstring for each parameter.
  617. Since per-parameter docstrings are optional,
  618. you can skip this step if you prefer.</p>
  619. <p>Nevertheless, here’s how to add a per-parameter docstring.
  620. The first line of the per-parameter docstring
  621. must be indented further than the parameter definition.
  622. The left margin of this first line establishes
  623. the left margin for the whole per-parameter docstring;
  624. all the text you write will be outdented by this amount.
  625. You can write as much text as you like, across multiple lines if you wish.</p>
  626. <p>Sample:</p>
  627. <div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="cm">/*[clinic input]</span>
  628. <span class="cm">module _pickle</span>
  629. <span class="cm">class _pickle.Pickler &quot;PicklerObject *&quot; &quot;&amp;Pickler_Type&quot;</span>
  630. <span class="cm">[clinic start generated code]*/</span>
  631. <span class="cm">/*[clinic input]</span>
  632. <span class="cm">_pickle.Pickler.dump</span>
  633. <span class="cm"> obj: &#39;O&#39;</span>
  634. <span class="cm"> The object to be pickled.</span>
  635. <span class="cm"> /</span>
  636. <span class="cm">Write a pickled representation of obj to the open file.</span>
  637. <span class="cm">[clinic start generated code]*/</span>
  638. </pre></div>
  639. </div>
  640. <p>Save and close the file, then run <code class="docutils literal notranslate"><span class="pre">Tools/clinic/clinic.py</span></code> on it.
  641. With luck everything worked—your block now has output,
  642. and a <code class="file docutils literal notranslate"><span class="pre">.c.h</span></code> file has been generated!
  643. Reload the file in your text editor to see the generated code:</p>
  644. <div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="cm">/*[clinic input]</span>
  645. <span class="cm">_pickle.Pickler.dump</span>
  646. <span class="cm"> obj: &#39;O&#39;</span>
  647. <span class="cm"> The object to be pickled.</span>
  648. <span class="cm"> /</span>
  649. <span class="cm">Write a pickled representation of obj to the open file.</span>
  650. <span class="cm">[clinic start generated code]*/</span>
  651. <span class="k">static</span><span class="w"> </span><span class="n">PyObject</span><span class="w"> </span><span class="o">*</span>
  652. <span class="n">_pickle_Pickler_dump</span><span class="p">(</span><span class="n">PicklerObject</span><span class="w"> </span><span class="o">*</span><span class="n">self</span><span class="p">,</span><span class="w"> </span><span class="n">PyObject</span><span class="w"> </span><span class="o">*</span><span class="n">obj</span><span class="p">)</span>
  653. <span class="cm">/*[clinic end generated code: output=87ecad1261e02ac7 input=552eb1c0f52260d9]*/</span>
  654. </pre></div>
  655. </div>
  656. <p>Obviously, if Argument Clinic didn’t produce any output,
  657. it’s because it found an error in your input.
  658. Keep fixing your errors and retrying until Argument Clinic processes your file
  659. without complaint.</p>
  660. <p>For readability, most of the glue code has been generated to a <code class="file docutils literal notranslate"><span class="pre">.c.h</span></code>
  661. file. You’ll need to include that in your original <code class="file docutils literal notranslate"><span class="pre">.c</span></code> file,
  662. typically right after the clinic module block:</p>
  663. <div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="cp">#include</span><span class="w"> </span><span class="cpf">&quot;clinic/_pickle.c.h&quot;</span>
  664. </pre></div>
  665. </div>
  666. <p>Double-check that the argument-parsing code Argument Clinic generated
  667. looks basically the same as the existing code.</p>
  668. <p>First, ensure both places use the same argument-parsing function.
  669. The existing code must call either
  670. <a class="reference internal" href="../c-api/arg.html#c.PyArg_ParseTuple" title="PyArg_ParseTuple"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyArg_ParseTuple()</span></code></a> or <a class="reference internal" href="../c-api/arg.html#c.PyArg_ParseTupleAndKeywords" title="PyArg_ParseTupleAndKeywords"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyArg_ParseTupleAndKeywords()</span></code></a>;
  671. ensure that the code generated by Argument Clinic calls the
  672. <em>exact</em> same function.</p>
  673. <p>Second, the format string passed in to <code class="xref c c-func docutils literal notranslate"><span class="pre">PyArg_ParseTuple()</span></code> or
  674. <code class="xref c c-func docutils literal notranslate"><span class="pre">PyArg_ParseTupleAndKeywords()</span></code> should be <em>exactly</em> the same
  675. as the hand-written one in the existing function,
  676. up to the colon or semi-colon.</p>
  677. <p>Argument Clinic always generates its format strings
  678. with a <code class="docutils literal notranslate"><span class="pre">:</span></code> followed by the name of the function.
  679. If the existing code’s format string ends with <code class="docutils literal notranslate"><span class="pre">;</span></code>,
  680. to provide usage help, this change is harmless — don’t worry about it.</p>
  681. <p>Third, for parameters whose format units require two arguments,
  682. like a length variable, an encoding string, or a pointer
  683. to a conversion function, ensure that the second argument is
  684. <em>exactly</em> the same between the two invocations.</p>
  685. <p>Fourth, inside the output portion of the block,
  686. you’ll find a preprocessor macro defining the appropriate static
  687. <a class="reference internal" href="../c-api/structures.html#c.PyMethodDef" title="PyMethodDef"><code class="xref c c-type docutils literal notranslate"><span class="pre">PyMethodDef</span></code></a> structure for this builtin:</p>
  688. <div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="cp">#define __PICKLE_PICKLER_DUMP_METHODDEF \</span>
  689. <span class="cp">{&quot;dump&quot;, (PyCFunction)__pickle_Pickler_dump, METH_O, __pickle_Pickler_dump__doc__},</span>
  690. </pre></div>
  691. </div>
  692. <p>This static structure should be <em>exactly</em> the same as the existing static
  693. <code class="xref c c-type docutils literal notranslate"><span class="pre">PyMethodDef</span></code> structure for this builtin.</p>
  694. <p>If any of these items differ in <em>any way</em>,
  695. adjust your Argument Clinic function specification and rerun
  696. <code class="docutils literal notranslate"><span class="pre">Tools/clinic/clinic.py</span></code> until they <em>are</em> the same.</p>
  697. <p>Notice that the last line of its output is the declaration
  698. of your “impl” function. This is where the builtin’s implementation goes.
  699. Delete the existing prototype of the function you’re modifying, but leave
  700. the opening curly brace. Now delete its argument parsing code and the
  701. declarations of all the variables it dumps the arguments into.
  702. Notice how the Python arguments are now arguments to this impl function;
  703. if the implementation used different names for these variables, fix it.</p>
  704. <p>Let’s reiterate, just because it’s kind of weird.
  705. Your code should now look like this:</p>
  706. <div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="k">static</span><span class="w"> </span><span class="n">return_type</span>
  707. <span class="nf">your_function_impl</span><span class="p">(...)</span>
  708. <span class="cm">/*[clinic end generated code: input=..., output=...]*/</span>
  709. <span class="p">{</span>
  710. <span class="p">...</span>
  711. </pre></div>
  712. </div>
  713. <p>Argument Clinic generated the checksum line and the function prototype just
  714. above it. You should write the opening and closing curly braces for the
  715. function, and the implementation inside.</p>
  716. <p>Sample:</p>
  717. <div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="cm">/*[clinic input]</span>
  718. <span class="cm">module _pickle</span>
  719. <span class="cm">class _pickle.Pickler &quot;PicklerObject *&quot; &quot;&amp;Pickler_Type&quot;</span>
  720. <span class="cm">[clinic start generated code]*/</span>
  721. <span class="cm">/*[clinic end generated code: checksum=da39a3ee5e6b4b0d3255bfef95601890afd80709]*/</span>
  722. <span class="cm">/*[clinic input]</span>
  723. <span class="cm">_pickle.Pickler.dump</span>
  724. <span class="cm"> obj: &#39;O&#39;</span>
  725. <span class="cm"> The object to be pickled.</span>
  726. <span class="cm"> /</span>
  727. <span class="cm">Write a pickled representation of obj to the open file.</span>
  728. <span class="cm">[clinic start generated code]*/</span>
  729. <span class="n">PyDoc_STRVAR</span><span class="p">(</span><span class="n">__pickle_Pickler_dump__doc__</span><span class="p">,</span>
  730. <span class="s">&quot;Write a pickled representation of obj to the open file.</span><span class="se">\n</span><span class="s">&quot;</span>
  731. <span class="s">&quot;</span><span class="se">\n</span><span class="s">&quot;</span>
  732. <span class="p">...</span>
  733. <span class="k">static</span><span class="w"> </span><span class="n">PyObject</span><span class="w"> </span><span class="o">*</span>
  734. <span class="n">_pickle_Pickler_dump_impl</span><span class="p">(</span><span class="n">PicklerObject</span><span class="w"> </span><span class="o">*</span><span class="n">self</span><span class="p">,</span><span class="w"> </span><span class="n">PyObject</span><span class="w"> </span><span class="o">*</span><span class="n">obj</span><span class="p">)</span>
  735. <span class="cm">/*[clinic end generated code: checksum=3bd30745bf206a48f8b576a1da3d90f55a0a4187]*/</span>
  736. <span class="p">{</span>
  737. <span class="w"> </span><span class="cm">/* Check whether the Pickler was initialized correctly (issue3664).</span>
  738. <span class="cm"> Developers often forget to call __init__() in their subclasses, which</span>
  739. <span class="cm"> would trigger a segfault without this check. */</span>
  740. <span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">self</span><span class="o">-&gt;</span><span class="n">write</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>
  741. <span class="w"> </span><span class="n">PyErr_Format</span><span class="p">(</span><span class="n">PicklingError</span><span class="p">,</span>
  742. <span class="w"> </span><span class="s">&quot;Pickler.__init__() was not called by %s.__init__()&quot;</span><span class="p">,</span>
  743. <span class="w"> </span><span class="n">Py_TYPE</span><span class="p">(</span><span class="n">self</span><span class="p">)</span><span class="o">-&gt;</span><span class="n">tp_name</span><span class="p">);</span>
  744. <span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="nb">NULL</span><span class="p">;</span>
  745. <span class="w"> </span><span class="p">}</span>
  746. <span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">_Pickler_ClearBuffer</span><span class="p">(</span><span class="n">self</span><span class="p">)</span><span class="w"> </span><span class="o">&lt;</span><span class="w"> </span><span class="mi">0</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
  747. <span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="nb">NULL</span><span class="p">;</span>
  748. <span class="w"> </span><span class="p">}</span>
  749. <span class="w"> </span><span class="p">...</span>
  750. </pre></div>
  751. </div>
  752. <p>Remember the macro with the <a class="reference internal" href="../c-api/structures.html#c.PyMethodDef" title="PyMethodDef"><code class="xref c c-type docutils literal notranslate"><span class="pre">PyMethodDef</span></code></a> structure for this function?
  753. Find the existing <code class="xref c c-type docutils literal notranslate"><span class="pre">PyMethodDef</span></code> structure for this
  754. function and replace it with a reference to the macro. If the builtin
  755. is at module scope, this will probably be very near the end of the file;
  756. if the builtin is a class method, this will probably be below but relatively
  757. near to the implementation.</p>
  758. <p>Note that the body of the macro contains a trailing comma; when you
  759. replace the existing static <code class="xref c c-type docutils literal notranslate"><span class="pre">PyMethodDef</span></code> structure with the macro,
  760. <em>don’t</em> add a comma to the end.</p>
  761. <p>Sample:</p>
  762. <div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="k">static</span><span class="w"> </span><span class="k">struct</span><span class="w"> </span><span class="nc">PyMethodDef</span><span class="w"> </span><span class="n">Pickler_methods</span><span class="p">[]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">{</span>
  763. <span class="w"> </span><span class="n">__PICKLE_PICKLER_DUMP_METHODDEF</span>
  764. <span class="w"> </span><span class="n">__PICKLE_PICKLER_CLEAR_MEMO_METHODDEF</span>
  765. <span class="w"> </span><span class="p">{</span><span class="nb">NULL</span><span class="p">,</span><span class="w"> </span><span class="nb">NULL</span><span class="p">}</span><span class="w"> </span><span class="cm">/* sentinel */</span>
  766. <span class="p">};</span>
  767. </pre></div>
  768. </div>
  769. <p>Argument Clinic may generate new instances of <code class="docutils literal notranslate"><span class="pre">_Py_ID</span></code>. For example:</p>
  770. <div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="o">&amp;</span><span class="n">_Py_ID</span><span class="p">(</span><span class="n">new_unique_py_id</span><span class="p">)</span>
  771. </pre></div>
  772. </div>
  773. <p>If it does, you’ll have to run <code class="docutils literal notranslate"><span class="pre">make</span> <span class="pre">regen-global-objects</span></code>
  774. to regenerate the list of precompiled identifiers at this point.</p>
  775. <p>Finally, compile, then run the relevant portions of the regression-test suite.
  776. This change should not introduce any new compile-time warnings or errors,
  777. and there should be no externally visible change to Python’s behavior,
  778. except for one difference: <a class="reference internal" href="../library/inspect.html#inspect.signature" title="inspect.signature"><code class="xref py py-func docutils literal notranslate"><span class="pre">inspect.signature()</span></code></a> run on your function
  779. should now provide a valid signature!</p>
  780. <p>Congratulations, you’ve ported your first function to work with Argument Clinic!</p>
  781. </section>
  782. <section id="how-to-guides">
  783. <span id="clinic-howtos"></span><h2>How-to guides<a class="headerlink" href="#how-to-guides" title="Permalink to this headline">¶</a></h2>
  784. <section id="how-to-rename-c-functions-and-variables-generated-by-argument-clinic">
  785. <h3>How to rename C functions and variables generated by Argument Clinic<a class="headerlink" href="#how-to-rename-c-functions-and-variables-generated-by-argument-clinic" title="Permalink to this headline">¶</a></h3>
  786. <p>Argument Clinic automatically names the functions it generates for you.
  787. Occasionally this may cause a problem, if the generated name collides with
  788. the name of an existing C function. There’s an easy solution: override the names
  789. used for the C functions. Just add the keyword <code class="docutils literal notranslate"><span class="pre">&quot;as&quot;</span></code>
  790. to your function declaration line, followed by the function name you wish to use.
  791. Argument Clinic will use that function name for the base (generated) function,
  792. then add <code class="docutils literal notranslate"><span class="pre">&quot;_impl&quot;</span></code> to the end and use that for the name of the impl function.</p>
  793. <p>For example, if we wanted to rename the C function names generated for
  794. <a class="reference internal" href="../library/pickle.html#pickle.Pickler.dump" title="pickle.Pickler.dump"><code class="xref py py-meth docutils literal notranslate"><span class="pre">pickle.Pickler.dump()</span></code></a>, it’d look like this:</p>
  795. <div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="cm">/*[clinic input]</span>
  796. <span class="cm">pickle.Pickler.dump as pickler_dumper</span>
  797. <span class="cm">...</span>
  798. </pre></div>
  799. </div>
  800. <p>The base function would now be named <code class="xref c c-func docutils literal notranslate"><span class="pre">pickler_dumper()</span></code>,
  801. and the impl function would now be named <code class="xref c c-func docutils literal notranslate"><span class="pre">pickler_dumper_impl()</span></code>.</p>
  802. <p>Similarly, you may have a problem where you want to give a parameter
  803. a specific Python name, but that name may be inconvenient in C. Argument
  804. Clinic allows you to give a parameter different names in Python and in C,
  805. using the same <code class="docutils literal notranslate"><span class="pre">&quot;as&quot;</span></code> syntax:</p>
  806. <div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="cm">/*[clinic input]</span>
  807. <span class="cm">pickle.Pickler.dump</span>
  808. <span class="cm"> obj: object</span>
  809. <span class="cm"> file as file_obj: object</span>
  810. <span class="cm"> protocol: object = NULL</span>
  811. <span class="cm"> *</span>
  812. <span class="cm"> fix_imports: bool = True</span>
  813. </pre></div>
  814. </div>
  815. <p>Here, the name used in Python (in the signature and the <code class="docutils literal notranslate"><span class="pre">keywords</span></code>
  816. array) would be <em>file</em>, but the C variable would be named <code class="docutils literal notranslate"><span class="pre">file_obj</span></code>.</p>
  817. <p>You can use this to rename the <em>self</em> parameter too!</p>
  818. </section>
  819. <section id="how-to-convert-functions-using-pyarg-unpacktuple">
  820. <h3>How to convert functions using <code class="docutils literal notranslate"><span class="pre">PyArg_UnpackTuple</span></code><a class="headerlink" href="#how-to-convert-functions-using-pyarg-unpacktuple" title="Permalink to this headline">¶</a></h3>
  821. <p>To convert a function parsing its arguments with <a class="reference internal" href="../c-api/arg.html#c.PyArg_UnpackTuple" title="PyArg_UnpackTuple"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyArg_UnpackTuple()</span></code></a>,
  822. simply write out all the arguments, specifying each as an <code class="docutils literal notranslate"><span class="pre">object</span></code>. You
  823. may specify the <em>type</em> argument to cast the type as appropriate. All
  824. arguments should be marked positional-only (add a <code class="docutils literal notranslate"><span class="pre">/</span></code> on a line by itself
  825. after the last argument).</p>
  826. <p>Currently the generated code will use <a class="reference internal" href="../c-api/arg.html#c.PyArg_ParseTuple" title="PyArg_ParseTuple"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyArg_ParseTuple()</span></code></a>, but this
  827. will change soon.</p>
  828. </section>
  829. <section id="how-to-use-optional-groups">
  830. <h3>How to use optional groups<a class="headerlink" href="#how-to-use-optional-groups" title="Permalink to this headline">¶</a></h3>
  831. <p>Some legacy functions have a tricky approach to parsing their arguments:
  832. they count the number of positional arguments, then use a <code class="docutils literal notranslate"><span class="pre">switch</span></code> statement
  833. to call one of several different <a class="reference internal" href="../c-api/arg.html#c.PyArg_ParseTuple" title="PyArg_ParseTuple"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyArg_ParseTuple()</span></code></a> calls depending on
  834. how many positional arguments there are. (These functions cannot accept
  835. keyword-only arguments.) This approach was used to simulate optional
  836. arguments back before <a class="reference internal" href="../c-api/arg.html#c.PyArg_ParseTupleAndKeywords" title="PyArg_ParseTupleAndKeywords"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyArg_ParseTupleAndKeywords()</span></code></a> was created.</p>
  837. <p>While functions using this approach can often be converted to
  838. use <code class="xref c c-func docutils literal notranslate"><span class="pre">PyArg_ParseTupleAndKeywords()</span></code>, optional arguments, and default values,
  839. it’s not always possible. Some of these legacy functions have
  840. behaviors <code class="xref c c-func docutils literal notranslate"><span class="pre">PyArg_ParseTupleAndKeywords()</span></code> doesn’t directly support.
  841. The most obvious example is the builtin function <a class="reference internal" href="../library/stdtypes.html#range" title="range"><code class="xref py py-func docutils literal notranslate"><span class="pre">range()</span></code></a>, which has
  842. an optional argument on the <em>left</em> side of its required argument!
  843. Another example is <a class="reference internal" href="../library/curses.html#curses.window.addch" title="curses.window.addch"><code class="xref py py-meth docutils literal notranslate"><span class="pre">curses.window.addch()</span></code></a>, which has a group of two
  844. arguments that must always be specified together. (The arguments are
  845. called <em>x</em> and <em>y</em>; if you call the function passing in <em>x</em>,
  846. you must also pass in <em>y</em> — and if you don’t pass in <em>x</em> you may not
  847. pass in <em>y</em> either.)</p>
  848. <p>In any case, the goal of Argument Clinic is to support argument parsing
  849. for all existing CPython builtins without changing their semantics.
  850. Therefore Argument Clinic supports
  851. this alternate approach to parsing, using what are called <em>optional groups</em>.
  852. Optional groups are groups of arguments that must all be passed in together.
  853. They can be to the left or the right of the required arguments. They
  854. can <em>only</em> be used with positional-only parameters.</p>
  855. <div class="admonition note">
  856. <p class="admonition-title">Note</p>
  857. <p>Optional groups are <em>only</em> intended for use when converting
  858. functions that make multiple calls to <a class="reference internal" href="../c-api/arg.html#c.PyArg_ParseTuple" title="PyArg_ParseTuple"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyArg_ParseTuple()</span></code></a>!
  859. Functions that use <em>any</em> other approach for parsing arguments
  860. should <em>almost never</em> be converted to Argument Clinic using
  861. optional groups. Functions using optional groups currently
  862. cannot have accurate signatures in Python, because Python just
  863. doesn’t understand the concept. Please avoid using optional
  864. groups wherever possible.</p>
  865. </div>
  866. <p>To specify an optional group, add a <code class="docutils literal notranslate"><span class="pre">[</span></code> on a line by itself before
  867. the parameters you wish to group together, and a <code class="docutils literal notranslate"><span class="pre">]</span></code> on a line by itself
  868. after these parameters. As an example, here’s how <a class="reference internal" href="../library/curses.html#curses.window.addch" title="curses.window.addch"><code class="xref py py-meth docutils literal notranslate"><span class="pre">curses.window.addch()</span></code></a>
  869. uses optional groups to make the first two parameters and the last
  870. parameter optional:</p>
  871. <div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="cm">/*[clinic input]</span>
  872. <span class="cm">curses.window.addch</span>
  873. <span class="cm"> [</span>
  874. <span class="cm"> x: int</span>
  875. <span class="cm"> X-coordinate.</span>
  876. <span class="cm"> y: int</span>
  877. <span class="cm"> Y-coordinate.</span>
  878. <span class="cm"> ]</span>
  879. <span class="cm"> ch: object</span>
  880. <span class="cm"> Character to add.</span>
  881. <span class="cm"> [</span>
  882. <span class="cm"> attr: long</span>
  883. <span class="cm"> Attributes for the character.</span>
  884. <span class="cm"> ]</span>
  885. <span class="cm"> /</span>
  886. <span class="cm">...</span>
  887. </pre></div>
  888. </div>
  889. <p>Notes:</p>
  890. <ul class="simple">
  891. <li><p>For every optional group, one additional parameter will be passed into the
  892. impl function representing the group. The parameter will be an int named
  893. <code class="docutils literal notranslate"><span class="pre">group_{direction}_{number}</span></code>,
  894. where <code class="docutils literal notranslate"><span class="pre">{direction}</span></code> is either <code class="docutils literal notranslate"><span class="pre">right</span></code> or <code class="docutils literal notranslate"><span class="pre">left</span></code> depending on whether the group
  895. is before or after the required parameters, and <code class="docutils literal notranslate"><span class="pre">{number}</span></code> is a monotonically
  896. increasing number (starting at 1) indicating how far away the group is from
  897. the required parameters. When the impl is called, this parameter will be set
  898. to zero if this group was unused, and set to non-zero if this group was used.
  899. (By used or unused, I mean whether or not the parameters received arguments
  900. in this invocation.)</p></li>
  901. <li><p>If there are no required arguments, the optional groups will behave
  902. as if they’re to the right of the required arguments.</p></li>
  903. <li><p>In the case of ambiguity, the argument parsing code
  904. favors parameters on the left (before the required parameters).</p></li>
  905. <li><p>Optional groups can only contain positional-only parameters.</p></li>
  906. <li><p>Optional groups are <em>only</em> intended for legacy code. Please do not
  907. use optional groups for new code.</p></li>
  908. </ul>
  909. </section>
  910. <section id="how-to-use-real-argument-clinic-converters-instead-of-legacy-converters">
  911. <h3>How to use real Argument Clinic converters, instead of “legacy converters”<a class="headerlink" href="#how-to-use-real-argument-clinic-converters-instead-of-legacy-converters" title="Permalink to this headline">¶</a></h3>
  912. <p>To save time, and to minimize how much you need to learn
  913. to achieve your first port to Argument Clinic, the walkthrough above tells
  914. you to use “legacy converters”. “Legacy converters” are a convenience,
  915. designed explicitly to make porting existing code to Argument Clinic
  916. easier. And to be clear, their use is acceptable when porting code for
  917. Python 3.4.</p>
  918. <p>However, in the long term we probably want all our blocks to
  919. use Argument Clinic’s real syntax for converters. Why? A couple
  920. reasons:</p>
  921. <ul class="simple">
  922. <li><p>The proper converters are far easier to read and clearer in their intent.</p></li>
  923. <li><p>There are some format units that are unsupported as “legacy converters”,
  924. because they require arguments, and the legacy converter syntax doesn’t
  925. support specifying arguments.</p></li>
  926. <li><p>In the future we may have a new argument parsing library that isn’t
  927. restricted to what <a class="reference internal" href="../c-api/arg.html#c.PyArg_ParseTuple" title="PyArg_ParseTuple"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyArg_ParseTuple()</span></code></a> supports; this flexibility
  928. won’t be available to parameters using legacy converters.</p></li>
  929. </ul>
  930. <p>Therefore, if you don’t mind a little extra effort, please use the normal
  931. converters instead of legacy converters.</p>
  932. <p>In a nutshell, the syntax for Argument Clinic (non-legacy) converters
  933. looks like a Python function call. However, if there are no explicit
  934. arguments to the function (all functions take their default values),
  935. you may omit the parentheses. Thus <code class="docutils literal notranslate"><span class="pre">bool</span></code> and <code class="docutils literal notranslate"><span class="pre">bool()</span></code> are exactly
  936. the same converters.</p>
  937. <p>All arguments to Argument Clinic converters are keyword-only.
  938. All Argument Clinic converters accept the following arguments:</p>
  939. <blockquote>
  940. <div><dl class="simple">
  941. <dt><em>c_default</em></dt><dd><p>The default value for this parameter when defined in C.
  942. Specifically, this will be the initializer for the variable declared
  943. in the “parse function”. See <a class="reference internal" href="#default-values"><span class="std std-ref">the section on default values</span></a>
  944. for how to use this.
  945. Specified as a string.</p>
  946. </dd>
  947. <dt><em>annotation</em></dt><dd><p>The annotation value for this parameter. Not currently supported,
  948. because <span class="target" id="index-2"></span><a class="pep reference external" href="https://peps.python.org/pep-0008/"><strong>PEP 8</strong></a> mandates that the Python library may not use
  949. annotations.</p>
  950. </dd>
  951. <dt><em>unused</em></dt><dd><p>Wrap the argument with <a class="reference internal" href="../c-api/intro.html#c.Py_UNUSED" title="Py_UNUSED"><code class="xref c c-macro docutils literal notranslate"><span class="pre">Py_UNUSED</span></code></a> in the impl function signature.</p>
  952. </dd>
  953. </dl>
  954. </div></blockquote>
  955. <p>In addition, some converters accept additional arguments. Here is a list
  956. of these arguments, along with their meanings:</p>
  957. <blockquote>
  958. <div><dl>
  959. <dt><em>accept</em></dt><dd><p>A set of Python types (and possibly pseudo-types);
  960. this restricts the allowable Python argument to values of these types.
  961. (This is not a general-purpose facility; as a rule it only supports
  962. specific lists of types as shown in the legacy converter table.)</p>
  963. <p>To accept <code class="docutils literal notranslate"><span class="pre">None</span></code>, add <code class="docutils literal notranslate"><span class="pre">NoneType</span></code> to this set.</p>
  964. </dd>
  965. <dt><em>bitwise</em></dt><dd><p>Only supported for unsigned integers. The native integer value of this
  966. Python argument will be written to the parameter without any range checking,
  967. even for negative values.</p>
  968. </dd>
  969. <dt><em>converter</em></dt><dd><p>Only supported by the <code class="docutils literal notranslate"><span class="pre">object</span></code> converter. Specifies the name of a
  970. <a class="reference internal" href="../c-api/arg.html#o-ampersand"><span class="std std-ref">C “converter function”</span></a>
  971. to use to convert this object to a native type.</p>
  972. </dd>
  973. <dt><em>encoding</em></dt><dd><p>Only supported for strings. Specifies the encoding to use when converting
  974. this string from a Python str (Unicode) value into a C <code class="docutils literal notranslate"><span class="pre">char</span> <span class="pre">*</span></code> value.</p>
  975. </dd>
  976. <dt><em>subclass_of</em></dt><dd><p>Only supported for the <code class="docutils literal notranslate"><span class="pre">object</span></code> converter. Requires that the Python
  977. value be a subclass of a Python type, as expressed in C.</p>
  978. </dd>
  979. <dt><em>type</em></dt><dd><p>Only supported for the <code class="docutils literal notranslate"><span class="pre">object</span></code> and <code class="docutils literal notranslate"><span class="pre">self</span></code> converters. Specifies
  980. the C type that will be used to declare the variable. Default value is
  981. <code class="docutils literal notranslate"><span class="pre">&quot;PyObject</span> <span class="pre">*&quot;</span></code>.</p>
  982. </dd>
  983. <dt><em>zeroes</em></dt><dd><p>Only supported for strings. If true, embedded NUL bytes (<code class="docutils literal notranslate"><span class="pre">'\\0'</span></code>) are
  984. permitted inside the value. The length of the string will be passed in
  985. to the impl function, just after the string parameter, as a parameter named
  986. <code class="docutils literal notranslate"><span class="pre">&lt;parameter_name&gt;_length</span></code>.</p>
  987. </dd>
  988. </dl>
  989. </div></blockquote>
  990. <p>Please note, not every possible combination of arguments will work.
  991. Usually these arguments are implemented by specific <a class="reference internal" href="../c-api/arg.html#c.PyArg_ParseTuple" title="PyArg_ParseTuple"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyArg_ParseTuple()</span></code></a>
  992. <em>format units</em>, with specific behavior. For example, currently you cannot
  993. call <code class="docutils literal notranslate"><span class="pre">unsigned_short</span></code> without also specifying <code class="docutils literal notranslate"><span class="pre">bitwise=True</span></code>.
  994. Although it’s perfectly reasonable to think this would work, these semantics don’t
  995. map to any existing format unit. So Argument Clinic doesn’t support it. (Or, at
  996. least, not yet.)</p>
  997. <p>Below is a table showing the mapping of legacy converters into real
  998. Argument Clinic converters. On the left is the legacy converter,
  999. on the right is the text you’d replace it with.</p>
  1000. <table class="docutils align-default">
  1001. <colgroup>
  1002. <col style="width: 10%" />
  1003. <col style="width: 90%" />
  1004. </colgroup>
  1005. <tbody>
  1006. <tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">'B'</span></code></p></td>
  1007. <td><p><code class="docutils literal notranslate"><span class="pre">unsigned_char(bitwise=True)</span></code></p></td>
  1008. </tr>
  1009. <tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">'b'</span></code></p></td>
  1010. <td><p><code class="docutils literal notranslate"><span class="pre">unsigned_char</span></code></p></td>
  1011. </tr>
  1012. <tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">'c'</span></code></p></td>
  1013. <td><p><code class="docutils literal notranslate"><span class="pre">char</span></code></p></td>
  1014. </tr>
  1015. <tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">'C'</span></code></p></td>
  1016. <td><p><code class="docutils literal notranslate"><span class="pre">int(accept={str})</span></code></p></td>
  1017. </tr>
  1018. <tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">'d'</span></code></p></td>
  1019. <td><p><code class="docutils literal notranslate"><span class="pre">double</span></code></p></td>
  1020. </tr>
  1021. <tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">'D'</span></code></p></td>
  1022. <td><p><code class="docutils literal notranslate"><span class="pre">Py_complex</span></code></p></td>
  1023. </tr>
  1024. <tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">'es'</span></code></p></td>
  1025. <td><p><code class="docutils literal notranslate"><span class="pre">str(encoding='name_of_encoding')</span></code></p></td>
  1026. </tr>
  1027. <tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">'es#'</span></code></p></td>
  1028. <td><p><code class="docutils literal notranslate"><span class="pre">str(encoding='name_of_encoding',</span> <span class="pre">zeroes=True)</span></code></p></td>
  1029. </tr>
  1030. <tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">'et'</span></code></p></td>
  1031. <td><p><code class="docutils literal notranslate"><span class="pre">str(encoding='name_of_encoding',</span> <span class="pre">accept={bytes,</span> <span class="pre">bytearray,</span> <span class="pre">str})</span></code></p></td>
  1032. </tr>
  1033. <tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">'et#'</span></code></p></td>
  1034. <td><p><code class="docutils literal notranslate"><span class="pre">str(encoding='name_of_encoding',</span> <span class="pre">accept={bytes,</span> <span class="pre">bytearray,</span> <span class="pre">str},</span> <span class="pre">zeroes=True)</span></code></p></td>
  1035. </tr>
  1036. <tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">'f'</span></code></p></td>
  1037. <td><p><code class="docutils literal notranslate"><span class="pre">float</span></code></p></td>
  1038. </tr>
  1039. <tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">'h'</span></code></p></td>
  1040. <td><p><code class="docutils literal notranslate"><span class="pre">short</span></code></p></td>
  1041. </tr>
  1042. <tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">'H'</span></code></p></td>
  1043. <td><p><code class="docutils literal notranslate"><span class="pre">unsigned_short(bitwise=True)</span></code></p></td>
  1044. </tr>
  1045. <tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">'i'</span></code></p></td>
  1046. <td><p><code class="docutils literal notranslate"><span class="pre">int</span></code></p></td>
  1047. </tr>
  1048. <tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">'I'</span></code></p></td>
  1049. <td><p><code class="docutils literal notranslate"><span class="pre">unsigned_int(bitwise=True)</span></code></p></td>
  1050. </tr>
  1051. <tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">'k'</span></code></p></td>
  1052. <td><p><code class="docutils literal notranslate"><span class="pre">unsigned_long(bitwise=True)</span></code></p></td>
  1053. </tr>
  1054. <tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">'K'</span></code></p></td>
  1055. <td><p><code class="docutils literal notranslate"><span class="pre">unsigned_long_long(bitwise=True)</span></code></p></td>
  1056. </tr>
  1057. <tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">'l'</span></code></p></td>
  1058. <td><p><code class="docutils literal notranslate"><span class="pre">long</span></code></p></td>
  1059. </tr>
  1060. <tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">'L'</span></code></p></td>
  1061. <td><p><code class="docutils literal notranslate"><span class="pre">long</span> <span class="pre">long</span></code></p></td>
  1062. </tr>
  1063. <tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">'n'</span></code></p></td>
  1064. <td><p><code class="docutils literal notranslate"><span class="pre">Py_ssize_t</span></code></p></td>
  1065. </tr>
  1066. <tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">'O'</span></code></p></td>
  1067. <td><p><code class="docutils literal notranslate"><span class="pre">object</span></code></p></td>
  1068. </tr>
  1069. <tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">'O!'</span></code></p></td>
  1070. <td><p><code class="docutils literal notranslate"><span class="pre">object(subclass_of='&amp;PySomething_Type')</span></code></p></td>
  1071. </tr>
  1072. <tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">'O&amp;'</span></code></p></td>
  1073. <td><p><code class="docutils literal notranslate"><span class="pre">object(converter='name_of_c_function')</span></code></p></td>
  1074. </tr>
  1075. <tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">'p'</span></code></p></td>
  1076. <td><p><code class="docutils literal notranslate"><span class="pre">bool</span></code></p></td>
  1077. </tr>
  1078. <tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">'S'</span></code></p></td>
  1079. <td><p><code class="docutils literal notranslate"><span class="pre">PyBytesObject</span></code></p></td>
  1080. </tr>
  1081. <tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">'s'</span></code></p></td>
  1082. <td><p><code class="docutils literal notranslate"><span class="pre">str</span></code></p></td>
  1083. </tr>
  1084. <tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">'s#'</span></code></p></td>
  1085. <td><p><code class="docutils literal notranslate"><span class="pre">str(zeroes=True)</span></code></p></td>
  1086. </tr>
  1087. <tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">'s*'</span></code></p></td>
  1088. <td><p><code class="docutils literal notranslate"><span class="pre">Py_buffer(accept={buffer,</span> <span class="pre">str})</span></code></p></td>
  1089. </tr>
  1090. <tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">'U'</span></code></p></td>
  1091. <td><p><code class="docutils literal notranslate"><span class="pre">unicode</span></code></p></td>
  1092. </tr>
  1093. <tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">'u'</span></code></p></td>
  1094. <td><p><code class="docutils literal notranslate"><span class="pre">wchar_t</span></code></p></td>
  1095. </tr>
  1096. <tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">'u#'</span></code></p></td>
  1097. <td><p><code class="docutils literal notranslate"><span class="pre">wchar_t(zeroes=True)</span></code></p></td>
  1098. </tr>
  1099. <tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">'w*'</span></code></p></td>
  1100. <td><p><code class="docutils literal notranslate"><span class="pre">Py_buffer(accept={rwbuffer})</span></code></p></td>
  1101. </tr>
  1102. <tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">'Y'</span></code></p></td>
  1103. <td><p><code class="docutils literal notranslate"><span class="pre">PyByteArrayObject</span></code></p></td>
  1104. </tr>
  1105. <tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">'y'</span></code></p></td>
  1106. <td><p><code class="docutils literal notranslate"><span class="pre">str(accept={bytes})</span></code></p></td>
  1107. </tr>
  1108. <tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">'y#'</span></code></p></td>
  1109. <td><p><code class="docutils literal notranslate"><span class="pre">str(accept={robuffer},</span> <span class="pre">zeroes=True)</span></code></p></td>
  1110. </tr>
  1111. <tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">'y*'</span></code></p></td>
  1112. <td><p><code class="docutils literal notranslate"><span class="pre">Py_buffer</span></code></p></td>
  1113. </tr>
  1114. <tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">'Z'</span></code></p></td>
  1115. <td><p><code class="docutils literal notranslate"><span class="pre">wchar_t(accept={str,</span> <span class="pre">NoneType})</span></code></p></td>
  1116. </tr>
  1117. <tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">'Z#'</span></code></p></td>
  1118. <td><p><code class="docutils literal notranslate"><span class="pre">wchar_t(accept={str,</span> <span class="pre">NoneType},</span> <span class="pre">zeroes=True)</span></code></p></td>
  1119. </tr>
  1120. <tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">'z'</span></code></p></td>
  1121. <td><p><code class="docutils literal notranslate"><span class="pre">str(accept={str,</span> <span class="pre">NoneType})</span></code></p></td>
  1122. </tr>
  1123. <tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">'z#'</span></code></p></td>
  1124. <td><p><code class="docutils literal notranslate"><span class="pre">str(accept={str,</span> <span class="pre">NoneType},</span> <span class="pre">zeroes=True)</span></code></p></td>
  1125. </tr>
  1126. <tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">'z*'</span></code></p></td>
  1127. <td><p><code class="docutils literal notranslate"><span class="pre">Py_buffer(accept={buffer,</span> <span class="pre">str,</span> <span class="pre">NoneType})</span></code></p></td>
  1128. </tr>
  1129. </tbody>
  1130. </table>
  1131. <p>As an example, here’s our sample <code class="docutils literal notranslate"><span class="pre">pickle.Pickler.dump</span></code> using the proper
  1132. converter:</p>
  1133. <div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="cm">/*[clinic input]</span>
  1134. <span class="cm">pickle.Pickler.dump</span>
  1135. <span class="cm"> obj: object</span>
  1136. <span class="cm"> The object to be pickled.</span>
  1137. <span class="cm"> /</span>
  1138. <span class="cm">Write a pickled representation of obj to the open file.</span>
  1139. <span class="cm">[clinic start generated code]*/</span>
  1140. </pre></div>
  1141. </div>
  1142. <p>One advantage of real converters is that they’re more flexible than legacy
  1143. converters. For example, the <code class="docutils literal notranslate"><span class="pre">unsigned_int</span></code> converter (and all the
  1144. <code class="docutils literal notranslate"><span class="pre">unsigned_</span></code> converters) can be specified without <code class="docutils literal notranslate"><span class="pre">bitwise=True</span></code>. Their
  1145. default behavior performs range checking on the value, and they won’t accept
  1146. negative numbers. You just can’t do that with a legacy converter!</p>
  1147. <p>Argument Clinic will show you all the converters it has
  1148. available. For each converter it’ll show you all the parameters
  1149. it accepts, along with the default value for each parameter.
  1150. Just run <code class="docutils literal notranslate"><span class="pre">Tools/clinic/clinic.py</span> <span class="pre">--converters</span></code> to see the full list.</p>
  1151. </section>
  1152. <section id="how-to-use-the-py-buffer-converter">
  1153. <h3>How to use the <code class="docutils literal notranslate"><span class="pre">Py_buffer</span></code> converter<a class="headerlink" href="#how-to-use-the-py-buffer-converter" title="Permalink to this headline">¶</a></h3>
  1154. <p>When using the <code class="docutils literal notranslate"><span class="pre">Py_buffer</span></code> converter
  1155. (or the <code class="docutils literal notranslate"><span class="pre">'s*'</span></code>, <code class="docutils literal notranslate"><span class="pre">'w*'</span></code>, <code class="docutils literal notranslate"><span class="pre">'*y'</span></code>, or <code class="docutils literal notranslate"><span class="pre">'z*'</span></code> legacy converters),
  1156. you <em>must</em> not call <a class="reference internal" href="../c-api/buffer.html#c.PyBuffer_Release" title="PyBuffer_Release"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyBuffer_Release()</span></code></a> on the provided buffer.
  1157. Argument Clinic generates code that does it for you (in the parsing function).</p>
  1158. </section>
  1159. <section id="how-to-use-advanced-converters">
  1160. <span id="clinic-howto-advanced-converters"></span><h3>How to use advanced converters<a class="headerlink" href="#how-to-use-advanced-converters" title="Permalink to this headline">¶</a></h3>
  1161. <p>Remember those format units you skipped for your first
  1162. time because they were advanced? Here’s how to handle those too.</p>
  1163. <p>The trick is, all those format units take arguments—either
  1164. conversion functions, or types, or strings specifying an encoding.
  1165. (But “legacy converters” don’t support arguments. That’s why we
  1166. skipped them for your first function.) The argument you specified
  1167. to the format unit is now an argument to the converter; this
  1168. argument is either <em>converter</em> (for <code class="docutils literal notranslate"><span class="pre">O&amp;</span></code>), <em>subclass_of</em> (for <code class="docutils literal notranslate"><span class="pre">O!</span></code>),
  1169. or <em>encoding</em> (for all the format units that start with <code class="docutils literal notranslate"><span class="pre">e</span></code>).</p>
  1170. <p>When using <em>subclass_of</em>, you may also want to use the other
  1171. custom argument for <code class="docutils literal notranslate"><span class="pre">object()</span></code>: <em>type</em>, which lets you set the type
  1172. actually used for the parameter. For example, if you want to ensure
  1173. that the object is a subclass of <a class="reference internal" href="../c-api/unicode.html#c.PyUnicode_Type" title="PyUnicode_Type"><code class="xref c c-var docutils literal notranslate"><span class="pre">PyUnicode_Type</span></code></a>, you probably want
  1174. to use the converter <code class="docutils literal notranslate"><span class="pre">object(type='PyUnicodeObject</span> <span class="pre">*',</span> <span class="pre">subclass_of='&amp;PyUnicode_Type')</span></code>.</p>
  1175. <p>One possible problem with using Argument Clinic: it takes away some possible
  1176. flexibility for the format units starting with <code class="docutils literal notranslate"><span class="pre">e</span></code>. When writing a
  1177. <code class="xref c c-func docutils literal notranslate"><span class="pre">PyArg_Parse*()</span></code> call by hand, you could theoretically decide at runtime what
  1178. encoding string to pass to that call. But now this string must
  1179. be hard-coded at Argument-Clinic-preprocessing-time. This limitation is deliberate;
  1180. it made supporting this format unit much easier, and may allow for future optimizations.
  1181. This restriction doesn’t seem unreasonable; CPython itself always passes in static
  1182. hard-coded encoding strings for parameters whose format units start with <code class="docutils literal notranslate"><span class="pre">e</span></code>.</p>
  1183. </section>
  1184. <section id="how-to-assign-default-values-to-parameter">
  1185. <span id="default-values"></span><span id="clinic-howto-default-values"></span><h3>How to assign default values to parameter<a class="headerlink" href="#how-to-assign-default-values-to-parameter" title="Permalink to this headline">¶</a></h3>
  1186. <p>Default values for parameters can be any of a number of values.
  1187. At their simplest, they can be string, int, or float literals:</p>
  1188. <div class="highlight-none notranslate"><div class="highlight"><pre><span></span>foo: str = &quot;abc&quot;
  1189. bar: int = 123
  1190. bat: float = 45.6
  1191. </pre></div>
  1192. </div>
  1193. <p>They can also use any of Python’s built-in constants:</p>
  1194. <div class="highlight-none notranslate"><div class="highlight"><pre><span></span>yep: bool = True
  1195. nope: bool = False
  1196. nada: object = None
  1197. </pre></div>
  1198. </div>
  1199. <p>There’s also special support for a default value of <code class="docutils literal notranslate"><span class="pre">NULL</span></code>, and
  1200. for simple expressions, documented in the following sections.</p>
  1201. <section id="the-null-default-value">
  1202. <h4>The <code class="docutils literal notranslate"><span class="pre">NULL</span></code> default value<a class="headerlink" href="#the-null-default-value" title="Permalink to this headline">¶</a></h4>
  1203. <p>For string and object parameters, you can set them to <code class="docutils literal notranslate"><span class="pre">None</span></code> to indicate
  1204. that there’s no default. However, that means the C variable will be
  1205. initialized to <code class="docutils literal notranslate"><span class="pre">Py_None</span></code>. For convenience’s sakes, there’s a special
  1206. value called <code class="docutils literal notranslate"><span class="pre">NULL</span></code> for just this reason: from Python’s perspective it
  1207. behaves like a default value of <code class="docutils literal notranslate"><span class="pre">None</span></code>, but the C variable is initialized
  1208. with <code class="docutils literal notranslate"><span class="pre">NULL</span></code>.</p>
  1209. </section>
  1210. <section id="symbolic-default-values">
  1211. <h4>Symbolic default values<a class="headerlink" href="#symbolic-default-values" title="Permalink to this headline">¶</a></h4>
  1212. <p>The default value you provide for a parameter can’t be any arbitrary
  1213. expression. Currently the following are explicitly supported:</p>
  1214. <ul class="simple">
  1215. <li><p>Numeric constants (integer and float)</p></li>
  1216. <li><p>String constants</p></li>
  1217. <li><p><code class="docutils literal notranslate"><span class="pre">True</span></code>, <code class="docutils literal notranslate"><span class="pre">False</span></code>, and <code class="docutils literal notranslate"><span class="pre">None</span></code></p></li>
  1218. <li><p>Simple symbolic constants like <a class="reference internal" href="../library/sys.html#sys.maxsize" title="sys.maxsize"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.maxsize</span></code></a>, which must
  1219. start with the name of the module</p></li>
  1220. </ul>
  1221. <p>(In the future, this may need to get even more elaborate,
  1222. to allow full expressions like <code class="docutils literal notranslate"><span class="pre">CONSTANT</span> <span class="pre">-</span> <span class="pre">1</span></code>.)</p>
  1223. </section>
  1224. <section id="expressions-as-default-values">
  1225. <h4>Expressions as default values<a class="headerlink" href="#expressions-as-default-values" title="Permalink to this headline">¶</a></h4>
  1226. <p>The default value for a parameter can be more than just a literal value.
  1227. It can be an entire expression, using math operators and looking up attributes
  1228. on objects. However, this support isn’t exactly simple, because of some
  1229. non-obvious semantics.</p>
  1230. <p>Consider the following example:</p>
  1231. <div class="highlight-none notranslate"><div class="highlight"><pre><span></span>foo: Py_ssize_t = sys.maxsize - 1
  1232. </pre></div>
  1233. </div>
  1234. <p><a class="reference internal" href="../library/sys.html#sys.maxsize" title="sys.maxsize"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.maxsize</span></code></a> can have different values on different platforms. Therefore
  1235. Argument Clinic can’t simply evaluate that expression locally and hard-code it
  1236. in C. So it stores the default in such a way that it will get evaluated at
  1237. runtime, when the user asks for the function’s signature.</p>
  1238. <p>What namespace is available when the expression is evaluated? It’s evaluated
  1239. in the context of the module the builtin came from. So, if your module has an
  1240. attribute called <code class="xref py py-attr docutils literal notranslate"><span class="pre">max_widgets</span></code>, you may simply use it:</p>
  1241. <div class="highlight-none notranslate"><div class="highlight"><pre><span></span>foo: Py_ssize_t = max_widgets
  1242. </pre></div>
  1243. </div>
  1244. <p>If the symbol isn’t found in the current module, it fails over to looking in
  1245. <a class="reference internal" href="../library/sys.html#sys.modules" title="sys.modules"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.modules</span></code></a>. That’s how it can find <a class="reference internal" href="../library/sys.html#sys.maxsize" title="sys.maxsize"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.maxsize</span></code></a> for example.
  1246. (Since you don’t know in advance what modules the user will load into their interpreter,
  1247. it’s best to restrict yourself to modules that are preloaded by Python itself.)</p>
  1248. <p>Evaluating default values only at runtime means Argument Clinic can’t compute
  1249. the correct equivalent C default value. So you need to tell it explicitly.
  1250. When you use an expression, you must also specify the equivalent expression
  1251. in C, using the <em>c_default</em> parameter to the converter:</p>
  1252. <div class="highlight-none notranslate"><div class="highlight"><pre><span></span>foo: Py_ssize_t(c_default=&quot;PY_SSIZE_T_MAX - 1&quot;) = sys.maxsize - 1
  1253. </pre></div>
  1254. </div>
  1255. <p>Another complication: Argument Clinic can’t know in advance whether or not the
  1256. expression you supply is valid. It parses it to make sure it looks legal, but
  1257. it can’t <em>actually</em> know. You must be very careful when using expressions to
  1258. specify values that are guaranteed to be valid at runtime!</p>
  1259. <p>Finally, because expressions must be representable as static C values, there
  1260. are many restrictions on legal expressions. Here’s a list of Python features
  1261. you’re not permitted to use:</p>
  1262. <ul class="simple">
  1263. <li><p>Function calls.</p></li>
  1264. <li><p>Inline if statements (<code class="docutils literal notranslate"><span class="pre">3</span> <span class="pre">if</span> <span class="pre">foo</span> <span class="pre">else</span> <span class="pre">5</span></code>).</p></li>
  1265. <li><p>Automatic sequence unpacking (<code class="docutils literal notranslate"><span class="pre">*[1,</span> <span class="pre">2,</span> <span class="pre">3]</span></code>).</p></li>
  1266. <li><p>List/set/dict comprehensions and generator expressions.</p></li>
  1267. <li><p>Tuple/list/set/dict literals.</p></li>
  1268. </ul>
  1269. </section>
  1270. </section>
  1271. <section id="how-to-use-return-converters">
  1272. <span id="clinic-howto-return-converters"></span><h3>How to use return converters<a class="headerlink" href="#how-to-use-return-converters" title="Permalink to this headline">¶</a></h3>
  1273. <p>By default, the impl function Argument Clinic generates for you returns
  1274. <a class="reference internal" href="../c-api/structures.html#c.PyObject" title="PyObject"><code class="xref c c-type docutils literal notranslate"><span class="pre">PyObject</span> <span class="pre">*</span></code></a>.
  1275. But your C function often computes some C type,
  1276. then converts it into the <code class="xref c c-type docutils literal notranslate"><span class="pre">PyObject</span> <span class="pre">*</span></code>
  1277. at the last moment. Argument Clinic handles converting your inputs from Python types
  1278. into native C types—why not have it convert your return value from a native C type
  1279. into a Python type too?</p>
  1280. <p>That’s what a “return converter” does. It changes your impl function to return
  1281. some C type, then adds code to the generated (non-impl) function to handle converting
  1282. that value into the appropriate <code class="xref c c-type docutils literal notranslate"><span class="pre">PyObject</span> <span class="pre">*</span></code>.</p>
  1283. <p>The syntax for return converters is similar to that of parameter converters.
  1284. You specify the return converter like it was a return annotation on the
  1285. function itself, using <code class="docutils literal notranslate"><span class="pre">-&gt;</span></code> notation.</p>
  1286. <p>For example:</p>
  1287. <div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="cm">/*[clinic input]</span>
  1288. <span class="cm">add -&gt; int</span>
  1289. <span class="cm"> a: int</span>
  1290. <span class="cm"> b: int</span>
  1291. <span class="cm"> /</span>
  1292. <span class="cm">[clinic start generated code]*/</span>
  1293. </pre></div>
  1294. </div>
  1295. <p>Return converters behave much the same as parameter converters;
  1296. they take arguments, the arguments are all keyword-only, and if you’re not changing
  1297. any of the default arguments you can omit the parentheses.</p>
  1298. <p>(If you use both <code class="docutils literal notranslate"><span class="pre">&quot;as&quot;</span></code> <em>and</em> a return converter for your function,
  1299. the <code class="docutils literal notranslate"><span class="pre">&quot;as&quot;</span></code> should come before the return converter.)</p>
  1300. <p>There’s one additional complication when using return converters: how do you
  1301. indicate an error has occurred? Normally, a function returns a valid (non-<code class="docutils literal notranslate"><span class="pre">NULL</span></code>)
  1302. pointer for success, and <code class="docutils literal notranslate"><span class="pre">NULL</span></code> for failure. But if you use an integer return converter,
  1303. all integers are valid. How can Argument Clinic detect an error? Its solution: each return
  1304. converter implicitly looks for a special value that indicates an error. If you return
  1305. that value, and an error has been set (c:func:<cite>PyErr_Occurred</cite> returns a true
  1306. value), then the generated code will propagate the error. Otherwise it will
  1307. encode the value you return like normal.</p>
  1308. <p>Currently Argument Clinic supports only a few return converters:</p>
  1309. <div class="highlight-none notranslate"><div class="highlight"><pre><span></span>bool
  1310. double
  1311. float
  1312. int
  1313. long
  1314. Py_ssize_t
  1315. size_t
  1316. unsigned int
  1317. unsigned long
  1318. </pre></div>
  1319. </div>
  1320. <p>None of these take parameters.
  1321. For all of these, return <code class="docutils literal notranslate"><span class="pre">-1</span></code> to indicate error.</p>
  1322. <p>To see all the return converters Argument Clinic supports, along with
  1323. their parameters (if any),
  1324. just run <code class="docutils literal notranslate"><span class="pre">Tools/clinic/clinic.py</span> <span class="pre">--converters</span></code> for the full list.</p>
  1325. </section>
  1326. <section id="how-to-clone-existing-functions">
  1327. <h3>How to clone existing functions<a class="headerlink" href="#how-to-clone-existing-functions" title="Permalink to this headline">¶</a></h3>
  1328. <p>If you have a number of functions that look similar, you may be able to
  1329. use Clinic’s “clone” feature. When you clone an existing function,
  1330. you reuse:</p>
  1331. <ul class="simple">
  1332. <li><p>its parameters, including</p>
  1333. <ul>
  1334. <li><p>their names,</p></li>
  1335. <li><p>their converters, with all parameters,</p></li>
  1336. <li><p>their default values,</p></li>
  1337. <li><p>their per-parameter docstrings,</p></li>
  1338. <li><p>their <em>kind</em> (whether they’re positional only,
  1339. positional or keyword, or keyword only), and</p></li>
  1340. </ul>
  1341. </li>
  1342. <li><p>its return converter.</p></li>
  1343. </ul>
  1344. <p>The only thing not copied from the original function is its docstring;
  1345. the syntax allows you to specify a new docstring.</p>
  1346. <p>Here’s the syntax for cloning a function:</p>
  1347. <div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="cm">/*[clinic input]</span>
  1348. <span class="cm">module.class.new_function [as c_basename] = module.class.existing_function</span>
  1349. <span class="cm">Docstring for new_function goes here.</span>
  1350. <span class="cm">[clinic start generated code]*/</span>
  1351. </pre></div>
  1352. </div>
  1353. <p>(The functions can be in different modules or classes. I wrote
  1354. <code class="docutils literal notranslate"><span class="pre">module.class</span></code> in the sample just to illustrate that you must
  1355. use the full path to <em>both</em> functions.)</p>
  1356. <p>Sorry, there’s no syntax for partially cloning a function, or cloning a function
  1357. then modifying it. Cloning is an all-or nothing proposition.</p>
  1358. <p>Also, the function you are cloning from must have been previously defined
  1359. in the current file.</p>
  1360. </section>
  1361. <section id="how-to-call-python-code">
  1362. <h3>How to call Python code<a class="headerlink" href="#how-to-call-python-code" title="Permalink to this headline">¶</a></h3>
  1363. <p>The rest of the advanced topics require you to write Python code
  1364. which lives inside your C file and modifies Argument Clinic’s
  1365. runtime state. This is simple: you simply define a Python block.</p>
  1366. <p>A Python block uses different delimiter lines than an Argument
  1367. Clinic function block. It looks like this:</p>
  1368. <div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="cm">/*[python input]</span>
  1369. <span class="cm"># python code goes here</span>
  1370. <span class="cm">[python start generated code]*/</span>
  1371. </pre></div>
  1372. </div>
  1373. <p>All the code inside the Python block is executed at the
  1374. time it’s parsed. All text written to stdout inside the block
  1375. is redirected into the “output” after the block.</p>
  1376. <p>As an example, here’s a Python block that adds a static integer
  1377. variable to the C code:</p>
  1378. <div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="cm">/*[python input]</span>
  1379. <span class="cm">print(&#39;static int __ignored_unused_variable__ = 0;&#39;)</span>
  1380. <span class="cm">[python start generated code]*/</span>
  1381. <span class="k">static</span><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">__ignored_unused_variable__</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span>
  1382. <span class="cm">/*[python checksum:...]*/</span>
  1383. </pre></div>
  1384. </div>
  1385. </section>
  1386. <section id="how-to-use-the-self-converter">
  1387. <span id="clinic-howto-self-converter"></span><h3>How to use the “self converter”<a class="headerlink" href="#how-to-use-the-self-converter" title="Permalink to this headline">¶</a></h3>
  1388. <p>Argument Clinic automatically adds a “self” parameter for you
  1389. using a default converter. It automatically sets the <code class="docutils literal notranslate"><span class="pre">type</span></code>
  1390. of this parameter to the “pointer to an instance” you specified
  1391. when you declared the type. However, you can override
  1392. Argument Clinic’s converter and specify one yourself.
  1393. Just add your own <em>self</em> parameter as the first parameter in a
  1394. block, and ensure that its converter is an instance of
  1395. <code class="xref py py-class docutils literal notranslate"><span class="pre">self_converter</span></code> or a subclass thereof.</p>
  1396. <p>What’s the point? This lets you override the type of <code class="docutils literal notranslate"><span class="pre">self</span></code>,
  1397. or give it a different default name.</p>
  1398. <p>How do you specify the custom type you want to cast <code class="docutils literal notranslate"><span class="pre">self</span></code> to?
  1399. If you only have one or two functions with the same type for <code class="docutils literal notranslate"><span class="pre">self</span></code>,
  1400. you can directly use Argument Clinic’s existing <code class="docutils literal notranslate"><span class="pre">self</span></code> converter,
  1401. passing in the type you want to use as the <em>type</em> parameter:</p>
  1402. <div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="cm">/*[clinic input]</span>
  1403. <span class="cm">_pickle.Pickler.dump</span>
  1404. <span class="cm"> self: self(type=&quot;PicklerObject *&quot;)</span>
  1405. <span class="cm"> obj: object</span>
  1406. <span class="cm"> /</span>
  1407. <span class="cm">Write a pickled representation of the given object to the open file.</span>
  1408. <span class="cm">[clinic start generated code]*/</span>
  1409. </pre></div>
  1410. </div>
  1411. <p>On the other hand, if you have a lot of functions that will use the same
  1412. type for <code class="docutils literal notranslate"><span class="pre">self</span></code>, it’s best to create your own converter, subclassing
  1413. <code class="xref py py-class docutils literal notranslate"><span class="pre">self_converter</span></code> but overwriting the <code class="xref py py-attr docutils literal notranslate"><span class="pre">type</span></code> member:</p>
  1414. <div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="cm">/*[python input]</span>
  1415. <span class="cm">class PicklerObject_converter(self_converter):</span>
  1416. <span class="cm"> type = &quot;PicklerObject *&quot;</span>
  1417. <span class="cm">[python start generated code]*/</span>
  1418. <span class="cm">/*[clinic input]</span>
  1419. <span class="cm">_pickle.Pickler.dump</span>
  1420. <span class="cm"> self: PicklerObject</span>
  1421. <span class="cm"> obj: object</span>
  1422. <span class="cm"> /</span>
  1423. <span class="cm">Write a pickled representation of the given object to the open file.</span>
  1424. <span class="cm">[clinic start generated code]*/</span>
  1425. </pre></div>
  1426. </div>
  1427. </section>
  1428. <section id="how-to-use-the-defining-class-converter">
  1429. <h3>How to use the “defining class” converter<a class="headerlink" href="#how-to-use-the-defining-class-converter" title="Permalink to this headline">¶</a></h3>
  1430. <p>Argument Clinic facilitates gaining access to the defining class of a method.
  1431. This is useful for <a class="reference internal" href="../c-api/typeobj.html#heap-types"><span class="std std-ref">heap type</span></a> methods that need to fetch
  1432. module level state. Use <a class="reference internal" href="../c-api/type.html#c.PyType_FromModuleAndSpec" title="PyType_FromModuleAndSpec"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyType_FromModuleAndSpec()</span></code></a> to associate a new
  1433. heap type with a module. You can now use <a class="reference internal" href="../c-api/type.html#c.PyType_GetModuleState" title="PyType_GetModuleState"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyType_GetModuleState()</span></code></a> on
  1434. the defining class to fetch the module state, for example from a module method.</p>
  1435. <p>Example from <a class="reference external" href="https://github.com/python/cpython/tree/3.12/Modules/zlibmodule.c">Modules/zlibmodule.c</a>.
  1436. First, <code class="docutils literal notranslate"><span class="pre">defining_class</span></code> is added to the clinic input:</p>
  1437. <div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="cm">/*[clinic input]</span>
  1438. <span class="cm">zlib.Compress.compress</span>
  1439. <span class="cm"> cls: defining_class</span>
  1440. <span class="cm"> data: Py_buffer</span>
  1441. <span class="cm"> Binary data to be compressed.</span>
  1442. <span class="cm"> /</span>
  1443. </pre></div>
  1444. </div>
  1445. <p>After running the Argument Clinic tool, the following function signature is
  1446. generated:</p>
  1447. <div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="cm">/*[clinic start generated code]*/</span>
  1448. <span class="k">static</span><span class="w"> </span><span class="n">PyObject</span><span class="w"> </span><span class="o">*</span>
  1449. <span class="n">zlib_Compress_compress_impl</span><span class="p">(</span><span class="n">compobject</span><span class="w"> </span><span class="o">*</span><span class="n">self</span><span class="p">,</span><span class="w"> </span><span class="n">PyTypeObject</span><span class="w"> </span><span class="o">*</span><span class="n">cls</span><span class="p">,</span>
  1450. <span class="w"> </span><span class="n">Py_buffer</span><span class="w"> </span><span class="o">*</span><span class="n">data</span><span class="p">)</span>
  1451. <span class="cm">/*[clinic end generated code: output=6731b3f0ff357ca6 input=04d00f65ab01d260]*/</span>
  1452. </pre></div>
  1453. </div>
  1454. <p>The following code can now use <code class="docutils literal notranslate"><span class="pre">PyType_GetModuleState(cls)</span></code> to fetch the
  1455. module state:</p>
  1456. <div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="n">zlibstate</span><span class="w"> </span><span class="o">*</span><span class="n">state</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">PyType_GetModuleState</span><span class="p">(</span><span class="n">cls</span><span class="p">);</span>
  1457. </pre></div>
  1458. </div>
  1459. <p>Each method may only have one argument using this converter, and it must appear
  1460. after <code class="docutils literal notranslate"><span class="pre">self</span></code>, or, if <code class="docutils literal notranslate"><span class="pre">self</span></code> is not used, as the first argument. The argument
  1461. will be of type <code class="docutils literal notranslate"><span class="pre">PyTypeObject</span> <span class="pre">*</span></code>. The argument will not appear in the
  1462. <code class="xref py py-attr docutils literal notranslate"><span class="pre">__text_signature__</span></code>.</p>
  1463. <p>The <code class="docutils literal notranslate"><span class="pre">defining_class</span></code> converter is not compatible with <code class="xref py py-meth docutils literal notranslate"><span class="pre">__init__()</span></code>
  1464. and <code class="xref py py-meth docutils literal notranslate"><span class="pre">__new__()</span></code> methods, which cannot use the <a class="reference internal" href="../c-api/structures.html#c.METH_METHOD" title="METH_METHOD"><code class="xref c c-macro docutils literal notranslate"><span class="pre">METH_METHOD</span></code></a>
  1465. convention.</p>
  1466. <p>It is not possible to use <code class="docutils literal notranslate"><span class="pre">defining_class</span></code> with slot methods. In order to
  1467. fetch the module state from such methods, use <a class="reference internal" href="../c-api/type.html#c.PyType_GetModuleByDef" title="PyType_GetModuleByDef"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyType_GetModuleByDef()</span></code></a>
  1468. to look up the module and then <a class="reference internal" href="../c-api/module.html#c.PyModule_GetState" title="PyModule_GetState"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyModule_GetState()</span></code></a> to fetch the module
  1469. state. Example from the <code class="docutils literal notranslate"><span class="pre">setattro</span></code> slot method in
  1470. <a class="reference external" href="https://github.com/python/cpython/tree/3.12/Modules/_threadmodule.c">Modules/_threadmodule.c</a>:</p>
  1471. <div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="k">static</span><span class="w"> </span><span class="kt">int</span>
  1472. <span class="nf">local_setattro</span><span class="p">(</span><span class="n">localobject</span><span class="w"> </span><span class="o">*</span><span class="n">self</span><span class="p">,</span><span class="w"> </span><span class="n">PyObject</span><span class="w"> </span><span class="o">*</span><span class="n">name</span><span class="p">,</span><span class="w"> </span><span class="n">PyObject</span><span class="w"> </span><span class="o">*</span><span class="n">v</span><span class="p">)</span>
  1473. <span class="p">{</span>
  1474. <span class="w"> </span><span class="n">PyObject</span><span class="w"> </span><span class="o">*</span><span class="n">module</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">PyType_GetModuleByDef</span><span class="p">(</span><span class="n">Py_TYPE</span><span class="p">(</span><span class="n">self</span><span class="p">),</span><span class="w"> </span><span class="o">&amp;</span><span class="n">thread_module</span><span class="p">);</span>
  1475. <span class="w"> </span><span class="n">thread_module_state</span><span class="w"> </span><span class="o">*</span><span class="n">state</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">get_thread_state</span><span class="p">(</span><span class="n">module</span><span class="p">);</span>
  1476. <span class="w"> </span><span class="p">...</span>
  1477. <span class="p">}</span>
  1478. </pre></div>
  1479. </div>
  1480. <p>See also <span class="target" id="index-3"></span><a class="pep reference external" href="https://peps.python.org/pep-0573/"><strong>PEP 573</strong></a>.</p>
  1481. </section>
  1482. <section id="how-to-write-a-custom-converter">
  1483. <span id="clinic-howto-custom-converter"></span><h3>How to write a custom converter<a class="headerlink" href="#how-to-write-a-custom-converter" title="Permalink to this headline">¶</a></h3>
  1484. <p>A converter is a Python class that inherits from <a class="reference internal" href="#clinic.CConverter" title="clinic.CConverter"><code class="xref py py-class docutils literal notranslate"><span class="pre">CConverter</span></code></a>.
  1485. The main purpose of a custom converter, is for parameters parsed with
  1486. the <code class="docutils literal notranslate"><span class="pre">O&amp;</span></code> format unit — parsing such a parameter means calling
  1487. a <a class="reference internal" href="../c-api/arg.html#c.PyArg_ParseTuple" title="PyArg_ParseTuple"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyArg_ParseTuple()</span></code></a> “converter function”.</p>
  1488. <p>Your converter class should be named <code class="samp docutils literal notranslate"><em><span class="pre">ConverterName</span></em><span class="pre">_converter</span></code>.
  1489. By following this convention, your converter class will be automatically
  1490. registered with Argument Clinic, with its <em>converter name</em> being the name of
  1491. your converter class with the <code class="docutils literal notranslate"><span class="pre">_converter</span></code> suffix stripped off.</p>
  1492. <p>Instead of subclassing <code class="xref py py-meth docutils literal notranslate"><span class="pre">CConverter.__init__()</span></code>,
  1493. write a <code class="xref py py-meth docutils literal notranslate"><span class="pre">converter_init()</span></code> method.
  1494. <code class="xref py py-meth docutils literal notranslate"><span class="pre">converter_init()</span></code> always accepts a <em>self</em> parameter.
  1495. After <em>self</em>, all additional parameters <strong>must</strong> be keyword-only.
  1496. Any arguments passed to the converter in Argument Clinic
  1497. will be passed along to your <code class="xref py py-meth docutils literal notranslate"><span class="pre">converter_init()</span></code> method.
  1498. See <a class="reference internal" href="#clinic.CConverter" title="clinic.CConverter"><code class="xref py py-class docutils literal notranslate"><span class="pre">CConverter</span></code></a> for a list of members you may wish to specify in
  1499. your subclass.</p>
  1500. <p>Here’s the simplest example of a custom converter, from <a class="reference external" href="https://github.com/python/cpython/tree/3.12/Modules/zlibmodule.c">Modules/zlibmodule.c</a>:</p>
  1501. <div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="cm">/*[python input]</span>
  1502. <span class="cm">class ssize_t_converter(CConverter):</span>
  1503. <span class="cm"> type = &#39;Py_ssize_t&#39;</span>
  1504. <span class="cm"> converter = &#39;ssize_t_converter&#39;</span>
  1505. <span class="cm">[python start generated code]*/</span>
  1506. <span class="cm">/*[python end generated code: output=da39a3ee5e6b4b0d input=35521e4e733823c7]*/</span>
  1507. </pre></div>
  1508. </div>
  1509. <p>This block adds a converter named <code class="docutils literal notranslate"><span class="pre">ssize_t</span></code> to Argument Clinic.
  1510. Parameters declared as <code class="docutils literal notranslate"><span class="pre">ssize_t</span></code> will be declared with type <a class="reference internal" href="../c-api/intro.html#c.Py_ssize_t" title="Py_ssize_t"><code class="xref c c-type docutils literal notranslate"><span class="pre">Py_ssize_t</span></code></a>,
  1511. and will be parsed by the <code class="docutils literal notranslate"><span class="pre">'O&amp;'</span></code> format unit,
  1512. which will call the <code class="xref c c-func docutils literal notranslate"><span class="pre">ssize_t_converter()</span></code> converter C function.
  1513. <code class="docutils literal notranslate"><span class="pre">ssize_t</span></code> variables automatically support default values.</p>
  1514. <p>More sophisticated custom converters can insert custom C code to
  1515. handle initialization and cleanup.
  1516. You can see more examples of custom converters in the CPython
  1517. source tree; grep the C files for the string <code class="docutils literal notranslate"><span class="pre">CConverter</span></code>.</p>
  1518. </section>
  1519. <section id="how-to-write-a-custom-return-converter">
  1520. <h3>How to write a custom return converter<a class="headerlink" href="#how-to-write-a-custom-return-converter" title="Permalink to this headline">¶</a></h3>
  1521. <p>Writing a custom return converter is much like writing
  1522. a custom converter. Except it’s somewhat simpler, because return
  1523. converters are themselves much simpler.</p>
  1524. <p>Return converters must subclass <code class="xref py py-class docutils literal notranslate"><span class="pre">CReturnConverter</span></code>.
  1525. There are no examples yet of custom return converters,
  1526. because they are not widely used yet. If you wish to
  1527. write your own return converter, please read <a class="reference external" href="https://github.com/python/cpython/tree/3.12/Tools/clinic/clinic.py">Tools/clinic/clinic.py</a>,
  1528. specifically the implementation of <code class="xref py py-class docutils literal notranslate"><span class="pre">CReturnConverter</span></code> and
  1529. all its subclasses.</p>
  1530. </section>
  1531. <section id="how-to-convert-meth-o-and-meth-noargs-functions">
  1532. <h3>How to convert <code class="docutils literal notranslate"><span class="pre">METH_O</span></code> and <code class="docutils literal notranslate"><span class="pre">METH_NOARGS</span></code> functions<a class="headerlink" href="#how-to-convert-meth-o-and-meth-noargs-functions" title="Permalink to this headline">¶</a></h3>
  1533. <p>To convert a function using <a class="reference internal" href="../c-api/structures.html#c.METH_O" title="METH_O"><code class="xref c c-macro docutils literal notranslate"><span class="pre">METH_O</span></code></a>, make sure the function’s
  1534. single argument is using the <code class="docutils literal notranslate"><span class="pre">object</span></code> converter, and mark the
  1535. arguments as positional-only:</p>
  1536. <div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="cm">/*[clinic input]</span>
  1537. <span class="cm">meth_o_sample</span>
  1538. <span class="cm"> argument: object</span>
  1539. <span class="cm"> /</span>
  1540. <span class="cm">[clinic start generated code]*/</span>
  1541. </pre></div>
  1542. </div>
  1543. <p>To convert a function using <a class="reference internal" href="../c-api/structures.html#c.METH_NOARGS" title="METH_NOARGS"><code class="xref c c-macro docutils literal notranslate"><span class="pre">METH_NOARGS</span></code></a>, just don’t specify
  1544. any arguments.</p>
  1545. <p>You can still use a self converter, a return converter, and specify
  1546. a <em>type</em> argument to the object converter for <a class="reference internal" href="../c-api/structures.html#c.METH_O" title="METH_O"><code class="xref c c-macro docutils literal notranslate"><span class="pre">METH_O</span></code></a>.</p>
  1547. </section>
  1548. <section id="how-to-convert-tp-new-and-tp-init-functions">
  1549. <h3>How to convert <code class="docutils literal notranslate"><span class="pre">tp_new</span></code> and <code class="docutils literal notranslate"><span class="pre">tp_init</span></code> functions<a class="headerlink" href="#how-to-convert-tp-new-and-tp-init-functions" title="Permalink to this headline">¶</a></h3>
  1550. <p>You can convert <a class="reference internal" href="../c-api/typeobj.html#c.PyTypeObject.tp_new" title="PyTypeObject.tp_new"><code class="xref c c-member docutils literal notranslate"><span class="pre">tp_new</span></code></a> and
  1551. <a class="reference internal" href="../c-api/typeobj.html#c.PyTypeObject.tp_init" title="PyTypeObject.tp_init"><code class="xref c c-member docutils literal notranslate"><span class="pre">tp_init</span></code></a> functions.
  1552. Just name them <code class="docutils literal notranslate"><span class="pre">__new__</span></code> or <code class="docutils literal notranslate"><span class="pre">__init__</span></code> as appropriate. Notes:</p>
  1553. <ul class="simple">
  1554. <li><p>The function name generated for <code class="docutils literal notranslate"><span class="pre">__new__</span></code> doesn’t end in <code class="docutils literal notranslate"><span class="pre">__new__</span></code>
  1555. like it would by default. It’s just the name of the class, converted
  1556. into a valid C identifier.</p></li>
  1557. <li><p>No <a class="reference internal" href="../c-api/structures.html#c.PyMethodDef" title="PyMethodDef"><code class="xref c c-type docutils literal notranslate"><span class="pre">PyMethodDef</span></code></a> <code class="docutils literal notranslate"><span class="pre">#define</span></code> is generated for these functions.</p></li>
  1558. <li><p><code class="docutils literal notranslate"><span class="pre">__init__</span></code> functions return <code class="docutils literal notranslate"><span class="pre">int</span></code>, not <code class="docutils literal notranslate"><span class="pre">PyObject</span> <span class="pre">*</span></code>.</p></li>
  1559. <li><p>Use the docstring as the class docstring.</p></li>
  1560. <li><p>Although <code class="docutils literal notranslate"><span class="pre">__new__</span></code> and <code class="docutils literal notranslate"><span class="pre">__init__</span></code> functions must always
  1561. accept both the <code class="docutils literal notranslate"><span class="pre">args</span></code> and <code class="docutils literal notranslate"><span class="pre">kwargs</span></code> objects, when converting
  1562. you may specify any signature for these functions that you like.
  1563. (If your function doesn’t support keywords, the parsing function
  1564. generated will throw an exception if it receives any.)</p></li>
  1565. </ul>
  1566. </section>
  1567. <section id="how-to-change-and-redirect-clinic-s-output">
  1568. <h3>How to change and redirect Clinic’s output<a class="headerlink" href="#how-to-change-and-redirect-clinic-s-output" title="Permalink to this headline">¶</a></h3>
  1569. <p>It can be inconvenient to have Clinic’s output interspersed with
  1570. your conventional hand-edited C code. Luckily, Clinic is configurable:
  1571. you can buffer up its output for printing later (or earlier!), or write
  1572. its output to a separate file. You can also add a prefix or suffix to
  1573. every line of Clinic’s generated output.</p>
  1574. <p>While changing Clinic’s output in this manner can be a boon to readability,
  1575. it may result in Clinic code using types before they are defined, or
  1576. your code attempting to use Clinic-generated code before it is defined.
  1577. These problems can be easily solved by rearranging the declarations in your file,
  1578. or moving where Clinic’s generated code goes. (This is why the default behavior
  1579. of Clinic is to output everything into the current block; while many people
  1580. consider this hampers readability, it will never require rearranging your
  1581. code to fix definition-before-use problems.)</p>
  1582. <p>Let’s start with defining some terminology:</p>
  1583. <dl>
  1584. <dt><em>field</em></dt><dd><p>A field, in this context, is a subsection of Clinic’s output.
  1585. For example, the <code class="docutils literal notranslate"><span class="pre">#define</span></code> for the <a class="reference internal" href="../c-api/structures.html#c.PyMethodDef" title="PyMethodDef"><code class="xref c c-type docutils literal notranslate"><span class="pre">PyMethodDef</span></code></a> structure
  1586. is a field, called <code class="docutils literal notranslate"><span class="pre">methoddef_define</span></code>. Clinic has seven
  1587. different fields it can output per function definition:</p>
  1588. <div class="highlight-none notranslate"><div class="highlight"><pre><span></span>docstring_prototype
  1589. docstring_definition
  1590. methoddef_define
  1591. impl_prototype
  1592. parser_prototype
  1593. parser_definition
  1594. impl_definition
  1595. </pre></div>
  1596. </div>
  1597. <p>All the names are of the form <code class="docutils literal notranslate"><span class="pre">&quot;&lt;a&gt;_&lt;b&gt;&quot;</span></code>,
  1598. where <code class="docutils literal notranslate"><span class="pre">&quot;&lt;a&gt;&quot;</span></code> is the semantic object represented (the parsing function,
  1599. the impl function, the docstring, or the methoddef structure) and <code class="docutils literal notranslate"><span class="pre">&quot;&lt;b&gt;&quot;</span></code>
  1600. represents what kind of statement the field is. Field names that end in
  1601. <code class="docutils literal notranslate"><span class="pre">&quot;_prototype&quot;</span></code>
  1602. represent forward declarations of that thing, without the actual body/data
  1603. of the thing; field names that end in <code class="docutils literal notranslate"><span class="pre">&quot;_definition&quot;</span></code> represent the actual
  1604. definition of the thing, with the body/data of the thing. (<code class="docutils literal notranslate"><span class="pre">&quot;methoddef&quot;</span></code>
  1605. is special, it’s the only one that ends with <code class="docutils literal notranslate"><span class="pre">&quot;_define&quot;</span></code>, representing that
  1606. it’s a preprocessor #define.)</p>
  1607. </dd>
  1608. <dt><em>destination</em></dt><dd><p>A destination is a place Clinic can write output to. There are
  1609. five built-in destinations:</p>
  1610. <dl>
  1611. <dt><code class="docutils literal notranslate"><span class="pre">block</span></code></dt><dd><p>The default destination: printed in the output section of
  1612. the current Clinic block.</p>
  1613. </dd>
  1614. <dt><code class="docutils literal notranslate"><span class="pre">buffer</span></code></dt><dd><p>A text buffer where you can save text for later. Text sent
  1615. here is appended to the end of any existing text. It’s an
  1616. error to have any text left in the buffer when Clinic finishes
  1617. processing a file.</p>
  1618. </dd>
  1619. <dt><code class="docutils literal notranslate"><span class="pre">file</span></code></dt><dd><p>A separate “clinic file” that will be created automatically by Clinic.
  1620. The filename chosen for the file is <code class="docutils literal notranslate"><span class="pre">{basename}.clinic{extension}</span></code>,
  1621. where <code class="docutils literal notranslate"><span class="pre">basename</span></code> and <code class="docutils literal notranslate"><span class="pre">extension</span></code> were assigned the output
  1622. from <code class="docutils literal notranslate"><span class="pre">os.path.splitext()</span></code> run on the current file. (Example:
  1623. the <code class="docutils literal notranslate"><span class="pre">file</span></code> destination for <code class="file docutils literal notranslate"><span class="pre">_pickle.c</span></code> would be written to
  1624. <code class="file docutils literal notranslate"><span class="pre">_pickle.clinic.c</span></code>.)</p>
  1625. <p><strong>Important: When using a</strong> <code class="docutils literal notranslate"><span class="pre">file</span></code> <strong>destination, you</strong>
  1626. <em>must check in</em> <strong>the generated file!</strong></p>
  1627. </dd>
  1628. <dt><code class="docutils literal notranslate"><span class="pre">two-pass</span></code></dt><dd><p>A buffer like <code class="docutils literal notranslate"><span class="pre">buffer</span></code>. However, a two-pass buffer can only
  1629. be dumped once, and it prints out all text sent to it during
  1630. all processing, even from Clinic blocks <em>after</em> the dumping point.</p>
  1631. </dd>
  1632. <dt><code class="docutils literal notranslate"><span class="pre">suppress</span></code></dt><dd><p>The text is suppressed—thrown away.</p>
  1633. </dd>
  1634. </dl>
  1635. </dd>
  1636. </dl>
  1637. <p>Clinic defines five new directives that let you reconfigure its output.</p>
  1638. <p>The first new directive is <code class="docutils literal notranslate"><span class="pre">dump</span></code>:</p>
  1639. <div class="highlight-none notranslate"><div class="highlight"><pre><span></span>dump &lt;destination&gt;
  1640. </pre></div>
  1641. </div>
  1642. <p>This dumps the current contents of the named destination into the output of
  1643. the current block, and empties it. This only works with <code class="docutils literal notranslate"><span class="pre">buffer</span></code> and
  1644. <code class="docutils literal notranslate"><span class="pre">two-pass</span></code> destinations.</p>
  1645. <p>The second new directive is <code class="docutils literal notranslate"><span class="pre">output</span></code>. The most basic form of <code class="docutils literal notranslate"><span class="pre">output</span></code>
  1646. is like this:</p>
  1647. <div class="highlight-none notranslate"><div class="highlight"><pre><span></span>output &lt;field&gt; &lt;destination&gt;
  1648. </pre></div>
  1649. </div>
  1650. <p>This tells Clinic to output <em>field</em> to <em>destination</em>. <code class="docutils literal notranslate"><span class="pre">output</span></code> also
  1651. supports a special meta-destination, called <code class="docutils literal notranslate"><span class="pre">everything</span></code>, which tells
  1652. Clinic to output <em>all</em> fields to that <em>destination</em>.</p>
  1653. <p><code class="docutils literal notranslate"><span class="pre">output</span></code> has a number of other functions:</p>
  1654. <div class="highlight-none notranslate"><div class="highlight"><pre><span></span>output push
  1655. output pop
  1656. output preset &lt;preset&gt;
  1657. </pre></div>
  1658. </div>
  1659. <p><code class="docutils literal notranslate"><span class="pre">output</span> <span class="pre">push</span></code> and <code class="docutils literal notranslate"><span class="pre">output</span> <span class="pre">pop</span></code> allow you to push and pop
  1660. configurations on an internal configuration stack, so that you
  1661. can temporarily modify the output configuration, then easily restore
  1662. the previous configuration. Simply push before your change to save
  1663. the current configuration, then pop when you wish to restore the
  1664. previous configuration.</p>
  1665. <p><code class="docutils literal notranslate"><span class="pre">output</span> <span class="pre">preset</span></code> sets Clinic’s output to one of several built-in
  1666. preset configurations, as follows:</p>
  1667. <blockquote>
  1668. <div><dl>
  1669. <dt><code class="docutils literal notranslate"><span class="pre">block</span></code></dt><dd><p>Clinic’s original starting configuration. Writes everything
  1670. immediately after the input block.</p>
  1671. <p>Suppress the <code class="docutils literal notranslate"><span class="pre">parser_prototype</span></code>
  1672. and <code class="docutils literal notranslate"><span class="pre">docstring_prototype</span></code>, write everything else to <code class="docutils literal notranslate"><span class="pre">block</span></code>.</p>
  1673. </dd>
  1674. <dt><code class="docutils literal notranslate"><span class="pre">file</span></code></dt><dd><p>Designed to write everything to the “clinic file” that it can.
  1675. You then <code class="docutils literal notranslate"><span class="pre">#include</span></code> this file near the top of your file.
  1676. You may need to rearrange your file to make this work, though
  1677. usually this just means creating forward declarations for various
  1678. <code class="docutils literal notranslate"><span class="pre">typedef</span></code> and <code class="docutils literal notranslate"><span class="pre">PyTypeObject</span></code> definitions.</p>
  1679. <p>Suppress the <code class="docutils literal notranslate"><span class="pre">parser_prototype</span></code>
  1680. and <code class="docutils literal notranslate"><span class="pre">docstring_prototype</span></code>, write the <code class="docutils literal notranslate"><span class="pre">impl_definition</span></code> to
  1681. <code class="docutils literal notranslate"><span class="pre">block</span></code>, and write everything else to <code class="docutils literal notranslate"><span class="pre">file</span></code>.</p>
  1682. <p>The default filename is <code class="docutils literal notranslate"><span class="pre">&quot;{dirname}/clinic/{basename}.h&quot;</span></code>.</p>
  1683. </dd>
  1684. <dt><code class="docutils literal notranslate"><span class="pre">buffer</span></code></dt><dd><p>Save up most of the output from Clinic, to be written into
  1685. your file near the end. For Python files implementing modules
  1686. or builtin types, it’s recommended that you dump the buffer
  1687. just above the static structures for your module or
  1688. builtin type; these are normally very near the end. Using
  1689. <code class="docutils literal notranslate"><span class="pre">buffer</span></code> may require even more editing than <code class="docutils literal notranslate"><span class="pre">file</span></code>, if
  1690. your file has static <code class="docutils literal notranslate"><span class="pre">PyMethodDef</span></code> arrays defined in the
  1691. middle of the file.</p>
  1692. <p>Suppress the <code class="docutils literal notranslate"><span class="pre">parser_prototype</span></code>, <code class="docutils literal notranslate"><span class="pre">impl_prototype</span></code>,
  1693. and <code class="docutils literal notranslate"><span class="pre">docstring_prototype</span></code>, write the <code class="docutils literal notranslate"><span class="pre">impl_definition</span></code> to
  1694. <code class="docutils literal notranslate"><span class="pre">block</span></code>, and write everything else to <code class="docutils literal notranslate"><span class="pre">file</span></code>.</p>
  1695. </dd>
  1696. <dt><code class="docutils literal notranslate"><span class="pre">two-pass</span></code></dt><dd><p>Similar to the <code class="docutils literal notranslate"><span class="pre">buffer</span></code> preset, but writes forward declarations to
  1697. the <code class="docutils literal notranslate"><span class="pre">two-pass</span></code> buffer, and definitions to the <code class="docutils literal notranslate"><span class="pre">buffer</span></code>.
  1698. This is similar to the <code class="docutils literal notranslate"><span class="pre">buffer</span></code> preset, but may require
  1699. less editing than <code class="docutils literal notranslate"><span class="pre">buffer</span></code>. Dump the <code class="docutils literal notranslate"><span class="pre">two-pass</span></code> buffer
  1700. near the top of your file, and dump the <code class="docutils literal notranslate"><span class="pre">buffer</span></code> near
  1701. the end just like you would when using the <code class="docutils literal notranslate"><span class="pre">buffer</span></code> preset.</p>
  1702. <p>Suppresses the <code class="docutils literal notranslate"><span class="pre">impl_prototype</span></code>, write the <code class="docutils literal notranslate"><span class="pre">impl_definition</span></code>
  1703. to <code class="docutils literal notranslate"><span class="pre">block</span></code>, write <code class="docutils literal notranslate"><span class="pre">docstring_prototype</span></code>, <code class="docutils literal notranslate"><span class="pre">methoddef_define</span></code>,
  1704. and <code class="docutils literal notranslate"><span class="pre">parser_prototype</span></code> to <code class="docutils literal notranslate"><span class="pre">two-pass</span></code>, write everything else
  1705. to <code class="docutils literal notranslate"><span class="pre">buffer</span></code>.</p>
  1706. </dd>
  1707. <dt><code class="docutils literal notranslate"><span class="pre">partial-buffer</span></code></dt><dd><p>Similar to the <code class="docutils literal notranslate"><span class="pre">buffer</span></code> preset, but writes more things to <code class="docutils literal notranslate"><span class="pre">block</span></code>,
  1708. only writing the really big chunks of generated code to <code class="docutils literal notranslate"><span class="pre">buffer</span></code>.
  1709. This avoids the definition-before-use problem of <code class="docutils literal notranslate"><span class="pre">buffer</span></code> completely,
  1710. at the small cost of having slightly more stuff in the block’s output.
  1711. Dump the <code class="docutils literal notranslate"><span class="pre">buffer</span></code> near the end, just like you would when using
  1712. the <code class="docutils literal notranslate"><span class="pre">buffer</span></code> preset.</p>
  1713. <p>Suppresses the <code class="docutils literal notranslate"><span class="pre">impl_prototype</span></code>, write the <code class="docutils literal notranslate"><span class="pre">docstring_definition</span></code>
  1714. and <code class="docutils literal notranslate"><span class="pre">parser_definition</span></code> to <code class="docutils literal notranslate"><span class="pre">buffer</span></code>, write everything else to <code class="docutils literal notranslate"><span class="pre">block</span></code>.</p>
  1715. </dd>
  1716. </dl>
  1717. </div></blockquote>
  1718. <p>The third new directive is <code class="docutils literal notranslate"><span class="pre">destination</span></code>:</p>
  1719. <div class="highlight-none notranslate"><div class="highlight"><pre><span></span>destination &lt;name&gt; &lt;command&gt; [...]
  1720. </pre></div>
  1721. </div>
  1722. <p>This performs an operation on the destination named <code class="docutils literal notranslate"><span class="pre">name</span></code>.</p>
  1723. <p>There are two defined subcommands: <code class="docutils literal notranslate"><span class="pre">new</span></code> and <code class="docutils literal notranslate"><span class="pre">clear</span></code>.</p>
  1724. <p>The <code class="docutils literal notranslate"><span class="pre">new</span></code> subcommand works like this:</p>
  1725. <div class="highlight-none notranslate"><div class="highlight"><pre><span></span>destination &lt;name&gt; new &lt;type&gt;
  1726. </pre></div>
  1727. </div>
  1728. <p>This creates a new destination with name <code class="docutils literal notranslate"><span class="pre">&lt;name&gt;</span></code> and type <code class="docutils literal notranslate"><span class="pre">&lt;type&gt;</span></code>.</p>
  1729. <p>There are five destination types:</p>
  1730. <blockquote>
  1731. <div><dl>
  1732. <dt><code class="docutils literal notranslate"><span class="pre">suppress</span></code></dt><dd><p>Throws the text away.</p>
  1733. </dd>
  1734. <dt><code class="docutils literal notranslate"><span class="pre">block</span></code></dt><dd><p>Writes the text to the current block. This is what Clinic
  1735. originally did.</p>
  1736. </dd>
  1737. <dt><code class="docutils literal notranslate"><span class="pre">buffer</span></code></dt><dd><p>A simple text buffer, like the “buffer” builtin destination above.</p>
  1738. </dd>
  1739. <dt><code class="docutils literal notranslate"><span class="pre">file</span></code></dt><dd><p>A text file. The file destination takes an extra argument,
  1740. a template to use for building the filename, like so:</p>
  1741. <blockquote>
  1742. <div><p>destination &lt;name&gt; new &lt;type&gt; &lt;file_template&gt;</p>
  1743. </div></blockquote>
  1744. <p>The template can use three strings internally that will be replaced
  1745. by bits of the filename:</p>
  1746. <blockquote>
  1747. <div><dl class="simple">
  1748. <dt>{path}</dt><dd><p>The full path to the file, including directory and full filename.</p>
  1749. </dd>
  1750. <dt>{dirname}</dt><dd><p>The name of the directory the file is in.</p>
  1751. </dd>
  1752. <dt>{basename}</dt><dd><p>Just the name of the file, not including the directory.</p>
  1753. </dd>
  1754. <dt>{basename_root}</dt><dd><p>Basename with the extension clipped off
  1755. (everything up to but not including the last ‘.’).</p>
  1756. </dd>
  1757. <dt>{basename_extension}</dt><dd><p>The last ‘.’ and everything after it. If the basename
  1758. does not contain a period, this will be the empty string.</p>
  1759. </dd>
  1760. </dl>
  1761. </div></blockquote>
  1762. <p>If there are no periods in the filename, {basename} and {filename}
  1763. are the same, and {extension} is empty. “{basename}{extension}”
  1764. is always exactly the same as “{filename}”.”</p>
  1765. </dd>
  1766. <dt><code class="docutils literal notranslate"><span class="pre">two-pass</span></code></dt><dd><p>A two-pass buffer, like the “two-pass” builtin destination above.</p>
  1767. </dd>
  1768. </dl>
  1769. </div></blockquote>
  1770. <p>The <code class="docutils literal notranslate"><span class="pre">clear</span></code> subcommand works like this:</p>
  1771. <div class="highlight-none notranslate"><div class="highlight"><pre><span></span>destination &lt;name&gt; clear
  1772. </pre></div>
  1773. </div>
  1774. <p>It removes all the accumulated text up to this point in the destination.
  1775. (I don’t know what you’d need this for, but I thought maybe it’d be
  1776. useful while someone’s experimenting.)</p>
  1777. <p>The fourth new directive is <code class="docutils literal notranslate"><span class="pre">set</span></code>:</p>
  1778. <div class="highlight-none notranslate"><div class="highlight"><pre><span></span>set line_prefix &quot;string&quot;
  1779. set line_suffix &quot;string&quot;
  1780. </pre></div>
  1781. </div>
  1782. <p><code class="docutils literal notranslate"><span class="pre">set</span></code> lets you set two internal variables in Clinic.
  1783. <code class="docutils literal notranslate"><span class="pre">line_prefix</span></code> is a string that will be prepended to every line of Clinic’s output;
  1784. <code class="docutils literal notranslate"><span class="pre">line_suffix</span></code> is a string that will be appended to every line of Clinic’s output.</p>
  1785. <p>Both of these support two format strings:</p>
  1786. <blockquote>
  1787. <div><dl class="simple">
  1788. <dt><code class="docutils literal notranslate"><span class="pre">{block</span> <span class="pre">comment</span> <span class="pre">start}</span></code></dt><dd><p>Turns into the string <code class="docutils literal notranslate"><span class="pre">/*</span></code>, the start-comment text sequence for C files.</p>
  1789. </dd>
  1790. <dt><code class="docutils literal notranslate"><span class="pre">{block</span> <span class="pre">comment</span> <span class="pre">end}</span></code></dt><dd><p>Turns into the string <code class="docutils literal notranslate"><span class="pre">*/</span></code>, the end-comment text sequence for C files.</p>
  1791. </dd>
  1792. </dl>
  1793. </div></blockquote>
  1794. <p>The final new directive is one you shouldn’t need to use directly,
  1795. called <code class="docutils literal notranslate"><span class="pre">preserve</span></code>:</p>
  1796. <div class="highlight-none notranslate"><div class="highlight"><pre><span></span>preserve
  1797. </pre></div>
  1798. </div>
  1799. <p>This tells Clinic that the current contents of the output should be kept, unmodified.
  1800. This is used internally by Clinic when dumping output into <code class="docutils literal notranslate"><span class="pre">file</span></code> files; wrapping
  1801. it in a Clinic block lets Clinic use its existing checksum functionality to ensure
  1802. the file was not modified by hand before it gets overwritten.</p>
  1803. </section>
  1804. <section id="how-to-use-the-ifdef-trick">
  1805. <h3>How to use the <code class="docutils literal notranslate"><span class="pre">#ifdef</span></code> trick<a class="headerlink" href="#how-to-use-the-ifdef-trick" title="Permalink to this headline">¶</a></h3>
  1806. <p>If you’re converting a function that isn’t available on all platforms,
  1807. there’s a trick you can use to make life a little easier. The existing
  1808. code probably looks like this:</p>
  1809. <div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="cp">#ifdef HAVE_FUNCTIONNAME</span>
  1810. <span class="k">static</span><span class="w"> </span><span class="n">module_functionname</span><span class="p">(...)</span>
  1811. <span class="p">{</span>
  1812. <span class="p">...</span>
  1813. <span class="p">}</span>
  1814. <span class="cp">#endif </span><span class="cm">/* HAVE_FUNCTIONNAME */</span>
  1815. </pre></div>
  1816. </div>
  1817. <p>And then in the <code class="docutils literal notranslate"><span class="pre">PyMethodDef</span></code> structure at the bottom the existing code
  1818. will have:</p>
  1819. <div class="highlight-none notranslate"><div class="highlight"><pre><span></span>#ifdef HAVE_FUNCTIONNAME
  1820. {&#39;functionname&#39;, ... },
  1821. #endif /* HAVE_FUNCTIONNAME */
  1822. </pre></div>
  1823. </div>
  1824. <p>In this scenario, you should enclose the body of your impl function inside the <code class="docutils literal notranslate"><span class="pre">#ifdef</span></code>,
  1825. like so:</p>
  1826. <div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="cp">#ifdef HAVE_FUNCTIONNAME</span>
  1827. <span class="cm">/*[clinic input]</span>
  1828. <span class="cm">module.functionname</span>
  1829. <span class="cm">...</span>
  1830. <span class="cm">[clinic start generated code]*/</span>
  1831. <span class="k">static</span><span class="w"> </span><span class="n">module_functionname</span><span class="p">(...)</span>
  1832. <span class="p">{</span>
  1833. <span class="p">...</span>
  1834. <span class="p">}</span>
  1835. <span class="cp">#endif </span><span class="cm">/* HAVE_FUNCTIONNAME */</span>
  1836. </pre></div>
  1837. </div>
  1838. <p>Then, remove those three lines from the <a class="reference internal" href="../c-api/structures.html#c.PyMethodDef" title="PyMethodDef"><code class="xref c c-type docutils literal notranslate"><span class="pre">PyMethodDef</span></code></a> structure,
  1839. replacing them with the macro Argument Clinic generated:</p>
  1840. <div class="highlight-none notranslate"><div class="highlight"><pre><span></span>MODULE_FUNCTIONNAME_METHODDEF
  1841. </pre></div>
  1842. </div>
  1843. <p>(You can find the real name for this macro inside the generated code.
  1844. Or you can calculate it yourself: it’s the name of your function as defined
  1845. on the first line of your block, but with periods changed to underscores,
  1846. uppercased, and <code class="docutils literal notranslate"><span class="pre">&quot;_METHODDEF&quot;</span></code> added to the end.)</p>
  1847. <p>Perhaps you’re wondering: what if <code class="docutils literal notranslate"><span class="pre">HAVE_FUNCTIONNAME</span></code> isn’t defined?
  1848. The <code class="docutils literal notranslate"><span class="pre">MODULE_FUNCTIONNAME_METHODDEF</span></code> macro won’t be defined either!</p>
  1849. <p>Here’s where Argument Clinic gets very clever. It actually detects that the
  1850. Argument Clinic block might be deactivated by the <code class="docutils literal notranslate"><span class="pre">#ifdef</span></code>. When that
  1851. happens, it generates a little extra code that looks like this:</p>
  1852. <div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="cp">#ifndef MODULE_FUNCTIONNAME_METHODDEF</span>
  1853. <span class="w"> </span><span class="cp">#define MODULE_FUNCTIONNAME_METHODDEF</span>
  1854. <span class="cp">#endif </span><span class="cm">/* !defined(MODULE_FUNCTIONNAME_METHODDEF) */</span>
  1855. </pre></div>
  1856. </div>
  1857. <p>That means the macro always works. If the function is defined, this turns
  1858. into the correct structure, including the trailing comma. If the function is
  1859. undefined, this turns into nothing.</p>
  1860. <p>However, this causes one ticklish problem: where should Argument Clinic put this
  1861. extra code when using the “block” output preset? It can’t go in the output block,
  1862. because that could be deactivated by the <code class="docutils literal notranslate"><span class="pre">#ifdef</span></code>. (That’s the whole point!)</p>
  1863. <p>In this situation, Argument Clinic writes the extra code to the “buffer” destination.
  1864. This may mean that you get a complaint from Argument Clinic:</p>
  1865. <div class="highlight-none notranslate"><div class="highlight"><pre><span></span>Warning in file &quot;Modules/posixmodule.c&quot; on line 12357:
  1866. Destination buffer &#39;buffer&#39; not empty at end of file, emptying.
  1867. </pre></div>
  1868. </div>
  1869. <p>When this happens, just open your file, find the <code class="docutils literal notranslate"><span class="pre">dump</span> <span class="pre">buffer</span></code> block that
  1870. Argument Clinic added to your file (it’ll be at the very bottom), then
  1871. move it above the <a class="reference internal" href="../c-api/structures.html#c.PyMethodDef" title="PyMethodDef"><code class="xref c c-type docutils literal notranslate"><span class="pre">PyMethodDef</span></code></a> structure where that macro is used.</p>
  1872. </section>
  1873. <section id="how-to-use-argument-clinic-in-python-files">
  1874. <h3>How to use Argument Clinic in Python files<a class="headerlink" href="#how-to-use-argument-clinic-in-python-files" title="Permalink to this headline">¶</a></h3>
  1875. <p>It’s actually possible to use Argument Clinic to preprocess Python files.
  1876. There’s no point to using Argument Clinic blocks, of course, as the output
  1877. wouldn’t make any sense to the Python interpreter. But using Argument Clinic
  1878. to run Python blocks lets you use Python as a Python preprocessor!</p>
  1879. <p>Since Python comments are different from C comments, Argument Clinic
  1880. blocks embedded in Python files look slightly different. They look like this:</p>
  1881. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="c1">#/*[python input]</span>
  1882. <span class="c1">#print(&quot;def foo(): pass&quot;)</span>
  1883. <span class="c1">#[python start generated code]*/</span>
  1884. <span class="k">def</span> <span class="nf">foo</span><span class="p">():</span> <span class="k">pass</span>
  1885. <span class="c1">#/*[python checksum:...]*/</span>
  1886. </pre></div>
  1887. </div>
  1888. </section>
  1889. </section>
  1890. </section>
  1891. <div class="clearer"></div>
  1892. </div>
  1893. </div>
  1894. </div>
  1895. <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
  1896. <div class="sphinxsidebarwrapper">
  1897. <div>
  1898. <h3><a href="../contents.html">Table of Contents</a></h3>
  1899. <ul>
  1900. <li><a class="reference internal" href="#">Argument Clinic How-To</a><ul>
  1901. <li><a class="reference internal" href="#background">Background</a><ul>
  1902. <li><a class="reference internal" href="#basic-concepts">Basic concepts</a></li>
  1903. </ul>
  1904. </li>
  1905. <li><a class="reference internal" href="#reference">Reference</a><ul>
  1906. <li><a class="reference internal" href="#terminology">Terminology</a></li>
  1907. <li><a class="reference internal" href="#command-line-interface">Command-line interface</a></li>
  1908. <li><a class="reference internal" href="#module-clinic">Classes for extending Argument Clinic</a></li>
  1909. </ul>
  1910. </li>
  1911. <li><a class="reference internal" href="#tutorial">Tutorial</a></li>
  1912. <li><a class="reference internal" href="#how-to-guides">How-to guides</a><ul>
  1913. <li><a class="reference internal" href="#how-to-rename-c-functions-and-variables-generated-by-argument-clinic">How to rename C functions and variables generated by Argument Clinic</a></li>
  1914. <li><a class="reference internal" href="#how-to-convert-functions-using-pyarg-unpacktuple">How to convert functions using <code class="docutils literal notranslate"><span class="pre">PyArg_UnpackTuple</span></code></a></li>
  1915. <li><a class="reference internal" href="#how-to-use-optional-groups">How to use optional groups</a></li>
  1916. <li><a class="reference internal" href="#how-to-use-real-argument-clinic-converters-instead-of-legacy-converters">How to use real Argument Clinic converters, instead of “legacy converters”</a></li>
  1917. <li><a class="reference internal" href="#how-to-use-the-py-buffer-converter">How to use the <code class="docutils literal notranslate"><span class="pre">Py_buffer</span></code> converter</a></li>
  1918. <li><a class="reference internal" href="#how-to-use-advanced-converters">How to use advanced converters</a></li>
  1919. <li><a class="reference internal" href="#how-to-assign-default-values-to-parameter">How to assign default values to parameter</a><ul>
  1920. <li><a class="reference internal" href="#the-null-default-value">The <code class="docutils literal notranslate"><span class="pre">NULL</span></code> default value</a></li>
  1921. <li><a class="reference internal" href="#symbolic-default-values">Symbolic default values</a></li>
  1922. <li><a class="reference internal" href="#expressions-as-default-values">Expressions as default values</a></li>
  1923. </ul>
  1924. </li>
  1925. <li><a class="reference internal" href="#how-to-use-return-converters">How to use return converters</a></li>
  1926. <li><a class="reference internal" href="#how-to-clone-existing-functions">How to clone existing functions</a></li>
  1927. <li><a class="reference internal" href="#how-to-call-python-code">How to call Python code</a></li>
  1928. <li><a class="reference internal" href="#how-to-use-the-self-converter">How to use the “self converter”</a></li>
  1929. <li><a class="reference internal" href="#how-to-use-the-defining-class-converter">How to use the “defining class” converter</a></li>
  1930. <li><a class="reference internal" href="#how-to-write-a-custom-converter">How to write a custom converter</a></li>
  1931. <li><a class="reference internal" href="#how-to-write-a-custom-return-converter">How to write a custom return converter</a></li>
  1932. <li><a class="reference internal" href="#how-to-convert-meth-o-and-meth-noargs-functions">How to convert <code class="docutils literal notranslate"><span class="pre">METH_O</span></code> and <code class="docutils literal notranslate"><span class="pre">METH_NOARGS</span></code> functions</a></li>
  1933. <li><a class="reference internal" href="#how-to-convert-tp-new-and-tp-init-functions">How to convert <code class="docutils literal notranslate"><span class="pre">tp_new</span></code> and <code class="docutils literal notranslate"><span class="pre">tp_init</span></code> functions</a></li>
  1934. <li><a class="reference internal" href="#how-to-change-and-redirect-clinic-s-output">How to change and redirect Clinic’s output</a></li>
  1935. <li><a class="reference internal" href="#how-to-use-the-ifdef-trick">How to use the <code class="docutils literal notranslate"><span class="pre">#ifdef</span></code> trick</a></li>
  1936. <li><a class="reference internal" href="#how-to-use-argument-clinic-in-python-files">How to use Argument Clinic in Python files</a></li>
  1937. </ul>
  1938. </li>
  1939. </ul>
  1940. </li>
  1941. </ul>
  1942. </div>
  1943. <div>
  1944. <h4>Previous topic</h4>
  1945. <p class="topless"><a href="ipaddress.html"
  1946. title="previous chapter">An introduction to the ipaddress module</a></p>
  1947. </div>
  1948. <div>
  1949. <h4>Next topic</h4>
  1950. <p class="topless"><a href="instrumentation.html"
  1951. title="next chapter">Instrumenting CPython with DTrace and SystemTap</a></p>
  1952. </div>
  1953. <div role="note" aria-label="source link">
  1954. <h3>This Page</h3>
  1955. <ul class="this-page-menu">
  1956. <li><a href="../bugs.html">Report a Bug</a></li>
  1957. <li>
  1958. <a href="https://github.com/python/cpython/blob/main/Doc/howto/clinic.rst"
  1959. rel="nofollow">Show Source
  1960. </a>
  1961. </li>
  1962. </ul>
  1963. </div>
  1964. </div>
  1965. </div>
  1966. <div class="clearer"></div>
  1967. </div>
  1968. <div class="related" role="navigation" aria-label="related navigation">
  1969. <h3>Navigation</h3>
  1970. <ul>
  1971. <li class="right" style="margin-right: 10px">
  1972. <a href="../genindex.html" title="General Index"
  1973. >index</a></li>
  1974. <li class="right" >
  1975. <a href="../py-modindex.html" title="Python Module Index"
  1976. >modules</a> |</li>
  1977. <li class="right" >
  1978. <a href="instrumentation.html" title="Instrumenting CPython with DTrace and SystemTap"
  1979. >next</a> |</li>
  1980. <li class="right" >
  1981. <a href="ipaddress.html" title="An introduction to the ipaddress module"
  1982. >previous</a> |</li>
  1983. <li><img src="../_static/py.svg" alt="python logo" style="vertical-align: middle; margin-top: -1px"/></li>
  1984. <li><a href="https://www.python.org/">Python</a> &#187;</li>
  1985. <li class="switchers">
  1986. <div class="language_switcher_placeholder"></div>
  1987. <div class="version_switcher_placeholder"></div>
  1988. </li>
  1989. <li>
  1990. </li>
  1991. <li id="cpython-language-and-version">
  1992. <a href="../index.html">3.12.0 Documentation</a> &#187;
  1993. </li>
  1994. <li class="nav-item nav-item-1"><a href="index.html" >Python HOWTOs</a> &#187;</li>
  1995. <li class="nav-item nav-item-this"><a href="">Argument Clinic How-To</a></li>
  1996. <li class="right">
  1997. <div class="inline-search" role="search">
  1998. <form class="inline-search" action="../search.html" method="get">
  1999. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" />
  2000. <input type="submit" value="Go" />
  2001. </form>
  2002. </div>
  2003. |
  2004. </li>
  2005. <li class="right">
  2006. <label class="theme-selector-label">
  2007. Theme
  2008. <select class="theme-selector" oninput="activateTheme(this.value)">
  2009. <option value="auto" selected>Auto</option>
  2010. <option value="light">Light</option>
  2011. <option value="dark">Dark</option>
  2012. </select>
  2013. </label> |</li>
  2014. </ul>
  2015. </div>
  2016. <div class="footer">
  2017. &copy; <a href="../copyright.html">Copyright</a> 2001-2023, Python Software Foundation.
  2018. <br />
  2019. This page is licensed under the Python Software Foundation License Version 2.
  2020. <br />
  2021. Examples, recipes, and other code in the documentation are additionally licensed under the Zero Clause BSD License.
  2022. <br />
  2023. See <a href="/license.html">History and License</a> for more information.<br />
  2024. <br />
  2025. The Python Software Foundation is a non-profit corporation.
  2026. <a href="https://www.python.org/psf/donations/">Please donate.</a>
  2027. <br />
  2028. <br />
  2029. Last updated on Oct 02, 2023.
  2030. <a href="/bugs.html">Found a bug</a>?
  2031. <br />
  2032. Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 4.5.0.
  2033. </div>
  2034. </body>
  2035. </html>