-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathintro.html
More file actions
1438 lines (1344 loc) · 130 KB
/
Copy pathintro.html
File metadata and controls
1438 lines (1344 loc) · 130 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<html lang="fa" data-content_root="../">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" /><meta name="viewport" content="width=device-width, initial-scale=1" />
<meta property="og:title" content="Introduction" />
<meta property="og:type" content="website" />
<meta property="og:url" content="https://docs.python.org/3/c-api/intro.html" />
<meta property="og:site_name" content="Python documentation" />
<meta property="og:description" content="The Application Programmer's Interface to Python gives C and C++ programmers access to the Python interpreter at a variety of levels. The API is equally usable from C++, but for brevity it is ..." />
<meta property="og:image" content="_static/og-image.png" />
<meta property="og:image:alt" content="Python documentation" />
<meta name="description" content="The Application Programmer's Interface to Python gives C and C++ programmers access to the Python interpreter at a variety of levels. The API is equally usable from C++, but for brevity it is ..." />
<meta name="theme-color" content="#3776ab">
<meta property="og:image:width" content="200">
<meta property="og:image:height" content="200">
<title>Introduction — مستندات Python3.14.6</title><meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" type="text/css" href="../_static/pygments.css?v=b86133f3" />
<link rel="stylesheet" type="text/css" href="../_static/classic.css?v=234b1a7c" />
<link rel="stylesheet" type="text/css" href="../_static/pydoctheme.css?v=4365c8fe" />
<link id="pygments_dark_css" media="(prefers-color-scheme: dark)" rel="stylesheet" type="text/css" href="../_static/pygments_dark.css?v=0fc419ee" />
<script src="../_static/documentation_options.js?v=e254dbbb"></script>
<script src="../_static/doctools.js?v=9bcbadda"></script>
<script src="../_static/sphinx_highlight.js?v=dc90522c"></script>
<script src="../_static/translations.js?v=5df48d09"></script>
<script src="../_static/sidebar.js"></script>
<link rel="search" type="application/opensearchdescription+xml"
title="جستجو در مستندات Python3.14.6"
href="../_static/opensearch.xml"/>
<link rel="author" title="درباره این مستندات" href="../about.html" />
<link rel="index" title="فهرست" href="../genindex.html" />
<link rel="search" title="جستجو" href="../search.html" />
<link rel="copyright" title="حق چاپ" href="../copyright.html" />
<link rel="next" title="C API Stability" href="stable.html" />
<link rel="prev" title="Python/C API reference manual" href="index.html" />
<link rel="canonical" href="https://docs.python.org/3/c-api/intro.html">
<style>
@media only screen {
table.full-width-table {
width: 100%;
}
}
</style>
<link rel="stylesheet" href="../_static/pydoctheme_dark.css" media="(prefers-color-scheme: dark)" id="pydoctheme_dark_css">
<link rel="shortcut icon" type="image/png" href="../_static/py.svg">
<script type="text/javascript" src="../_static/copybutton.js"></script>
<script type="text/javascript" src="../_static/menu.js"></script>
<script type="text/javascript" src="../_static/search-focus.js"></script>
<script type="text/javascript" src="../_static/themetoggle.js"></script>
<script type="text/javascript" src="../_static/rtd_switcher.js"></script>
<meta name="readthedocs-addons-api-version" content="1">
</head>
<body>
<div class="mobile-nav">
<input type="checkbox" id="menuToggler" class="toggler__input" aria-controls="navigation"
aria-pressed="false" aria-expanded="false" role="button" aria-label="Menu">
<nav class="nav-content" role="navigation">
<label for="menuToggler" class="toggler__label">
<span></span>
</label>
<span class="nav-items-wrapper">
<a href="https://www.python.org/" class="nav-logo">
<img src="../_static/py.svg" alt="Python logo">
</a>
<span class="version_switcher_placeholder"></span>
<form role="search" class="search" action="../search.html" method="get">
<svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" class="search-icon">
<path fill-rule="nonzero" fill="currentColor" d="M15.5 14h-.79l-.28-.27a6.5 6.5 0 001.48-5.34c-.47-2.78-2.79-5-5.59-5.34a6.505 6.505 0 00-7.27 7.27c.34 2.8 2.56 5.12 5.34 5.59a6.5 6.5 0 005.34-1.48l.27.28v.79l4.25 4.25c.41.41 1.08.41 1.49 0 .41-.41.41-1.08 0-1.49L15.5 14zm-6 0C7.01 14 5 11.99 5 9.5S7.01 5 9.5 5 14 7.01 14 9.5 11.99 14 9.5 14z"></path>
</svg>
<input placeholder="جستجو سریع" aria-label="جستجو سریع" type="search" name="q">
<input type="submit" value="برو">
</form>
</span>
</nav>
<div class="menu-wrapper">
<nav class="menu" role="navigation" aria-label="main navigation">
<div class="language_switcher_placeholder"></div>
<label class="theme-selector-label">
Theme
<select class="theme-selector" oninput="activateTheme(this.value)">
<option value="auto" selected>Auto</option>
<option value="light">Light</option>
<option value="dark">Dark</option>
</select>
</label>
<div>
<h3><a href="../contents.html">فهرست عناوین</a></h3>
<ul>
<li><a class="reference internal" href="#">Introduction</a><ul>
<li><a class="reference internal" href="#language-version-compatibility">Language version compatibility</a></li>
<li><a class="reference internal" href="#coding-standards">Coding standards</a></li>
<li><a class="reference internal" href="#include-files">Include Files</a></li>
<li><a class="reference internal" href="#useful-macros">Useful macros</a><ul>
<li><a class="reference internal" href="#docstring-macros">Docstring macros</a></li>
<li><a class="reference internal" href="#general-utility-macros">General utility macros</a><ul>
<li><a class="reference internal" href="#numeric-utilities">Numeric utilities</a></li>
<li><a class="reference internal" href="#assertion-utilities">Assertion utilities</a></li>
<li><a class="reference internal" href="#type-size-utilities">Type size utilities</a></li>
<li><a class="reference internal" href="#macro-definition-utilities">Macro definition utilities</a></li>
</ul>
</li>
<li><a class="reference internal" href="#declaration-utilities">Declaration utilities</a></li>
<li><a class="reference internal" href="#outdated-macros">Outdated macros</a></li>
</ul>
</li>
<li><a class="reference internal" href="#objects-types-and-reference-counts">Objects, Types and Reference Counts</a><ul>
<li><a class="reference internal" href="#reference-counts">Reference Counts</a><ul>
<li><a class="reference internal" href="#reference-count-details">Reference Count Details</a></li>
</ul>
</li>
<li><a class="reference internal" href="#types">Types</a></li>
</ul>
</li>
<li><a class="reference internal" href="#exceptions">Exceptions</a></li>
<li><a class="reference internal" href="#embedding-python">Embedding Python</a></li>
<li><a class="reference internal" href="#debugging-builds">Debugging Builds</a></li>
<li><a class="reference internal" href="#recommended-third-party-tools">Recommended third party tools</a></li>
</ul>
</li>
</ul>
</div>
<div>
<h4>موضوع قبلی</h4>
<p class="topless"><a href="index.html"
title="فصل قبلی">Python/C API reference manual</a></p>
</div>
<div>
<h4>موضوع بعدی</h4>
<p class="topless"><a href="stable.html"
title="فصل بعدی">C API Stability</a></p>
</div>
<script>
document.addEventListener('DOMContentLoaded', () => {
const title = document.querySelector('meta[property="og:title"]').content;
const elements = document.querySelectorAll('.improvepage');
const pageurl = window.location.href.split('?')[0];
elements.forEach(element => {
const url = new URL(element.href.split('?')[0].replace("-nojs", ""));
url.searchParams.set('pagetitle', title);
url.searchParams.set('pageurl', pageurl);
url.searchParams.set('pagesource', "c-api/intro.rst");
element.href = url.toString();
});
});
</script>
<div role="note" aria-label="source link">
<h3>این صفحه</h3>
<ul class="this-page-menu">
<li><a href="../bugs.html">گزارش یک اشکال</a></li>
<li><a class="improvepage" href="../improve-page-nojs.html">بهبود این صفحه</a></li>
<li>
<a href="https://github.com/python/cpython/blob/main/Doc/c-api/intro.rst?plain=1"
rel="nofollow">نمایش منبع
</a>
</li>
<li>
<a href="https://github.com/python/python-docs-fa/blob/3.14/c-api/intro.po?plain=1"
rel="nofollow">نمایش منبع ترجمه</a>
</li>
</ul>
</div>
</nav>
</div>
</div>
<div class="related" role="navigation" aria-label="Related">
<h3>ناوبری</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="../genindex.html" title="فهرست کلی"
accesskey="I">فهرست</a></li>
<li class="right" >
<a href="../py-modindex.html" title="نمایه ی ماژول های پایتون"
>ماژول ها</a> |</li>
<li class="right" >
<a href="stable.html" title="C API Stability"
accesskey="N">بعدی</a> |</li>
<li class="right" >
<a href="index.html" title="Python/C API reference manual"
accesskey="P">قبلی</a> |</li>
<li><img src="../_static/py.svg" alt="Python logo" style="vertical-align: middle; margin-top: -1px"></li>
<li><a href="https://www.python.org/">Python</a> »</li>
<li class="switchers">
<div class="language_switcher_placeholder"></div>
<div class="version_switcher_placeholder"></div>
</li>
<li>
</li>
<li id="cpython-language-and-version">
<a href="../index.html">3.14.6 Documentation</a> »
</li>
<li class="nav-item nav-item-1"><a href="index.html" accesskey="U">Python/C API reference manual</a> »</li>
<li class="nav-item nav-item-this"><a href="">Introduction</a></li>
<li class="right">
<div class="inline-search" role="search">
<form class="inline-search" action="../search.html" method="get">
<input placeholder="جستجو سریع" aria-label="جستجو سریع" type="search" name="q" id="search-box">
<input type="submit" value="برو">
</form>
</div>
|
</li>
<li class="right">
<label class="theme-selector-label">
Theme
<select class="theme-selector" oninput="activateTheme(this.value)">
<option value="auto" selected>Auto</option>
<option value="light">Light</option>
<option value="dark">Dark</option>
</select>
</label> |</li>
</ul>
</div>
<div class="document">
<div class="documentwrapper">
<div class="bodywrapper">
<div class="body" role="main">
<section id="introduction">
<span id="api-intro"></span><h1>Introduction<a class="headerlink" href="#introduction" title="Link to this heading">¶</a></h1>
<p>The Application Programmer's Interface to Python gives C and C++ programmers
access to the Python interpreter at a variety of levels. The API is equally
usable from C++, but for brevity it is generally referred to as the Python/C
API. There are two fundamentally different reasons for using the Python/C API.
The first reason is to write <em>extension modules</em> for specific purposes; these
are C modules that extend the Python interpreter. This is probably the most
common use. The second reason is to use Python as a component in a larger
application; this technique is generally referred to as <em class="dfn">embedding</em> Python
in an application.</p>
<p>Writing an extension module is a relatively well-understood process, where a
"cookbook" approach works well. There are several tools that automate the
process to some extent. While people have embedded Python in other
applications since its early existence, the process of embedding Python is
less straightforward than writing an extension.</p>
<p>Many API functions are useful independent of whether you're embedding or
extending Python; moreover, most applications that embed Python will need to
provide a custom extension as well, so it's probably a good idea to become
familiar with writing an extension before attempting to embed Python in a real
application.</p>
<section id="language-version-compatibility">
<h2>Language version compatibility<a class="headerlink" href="#language-version-compatibility" title="Link to this heading">¶</a></h2>
<p>Python's C API is compatible with C11 and C++11 versions of C and C++.</p>
<p>This is a lower limit: the C API does not require features from later
C/C++ versions.
You do <em>not</em> need to enable your compiler's "c11 mode".</p>
</section>
<section id="coding-standards">
<h2>Coding standards<a class="headerlink" href="#coding-standards" title="Link to this heading">¶</a></h2>
<p>If you're writing C code for inclusion in CPython, you <strong>must</strong> follow the
guidelines and standards defined in <span class="target" id="index-0"></span><a class="pep reference external" href="https://peps.python.org/pep-0007/"><strong>PEP 7</strong></a>. These guidelines apply
regardless of the version of Python you are contributing to. Following these
conventions is not necessary for your own third party extension modules,
unless you eventually expect to contribute them to Python.</p>
</section>
<section id="include-files">
<span id="api-includes"></span><h2>Include Files<a class="headerlink" href="#include-files" title="Link to this heading">¶</a></h2>
<p>All function, type and macro definitions needed to use the Python/C API are
included in your code by the following line:</p>
<div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="cp">#define PY_SSIZE_T_CLEAN</span>
<span class="cp">#include</span><span class="w"> </span><span class="cpf"><Python.h></span>
</pre></div>
</div>
<p>This implies inclusion of the following standard headers: <code class="docutils literal notranslate"><span class="pre"><stdio.h></span></code>,
<code class="docutils literal notranslate"><span class="pre"><string.h></span></code>, <code class="docutils literal notranslate"><span class="pre"><errno.h></span></code>, <code class="docutils literal notranslate"><span class="pre"><limits.h></span></code>, <code class="docutils literal notranslate"><span class="pre"><assert.h></span></code> and <code class="docutils literal notranslate"><span class="pre"><stdlib.h></span></code>
(if available).</p>
<div class="admonition note">
<p class="admonition-title">توجه</p>
<p>Since Python may define some pre-processor definitions which affect the standard
headers on some systems, you <em>must</em> include <code class="file docutils literal notranslate"><span class="pre">Python.h</span></code> before any standard
headers are included.</p>
<p>It is recommended to always define <code class="docutils literal notranslate"><span class="pre">PY_SSIZE_T_CLEAN</span></code> before including
<code class="docutils literal notranslate"><span class="pre">Python.h</span></code>. See <a class="reference internal" href="arg.html#arg-parsing"><span class="std std-ref">Parsing arguments and building values</span></a> for a description of this macro.</p>
</div>
<p>All user visible names defined by Python.h (except those defined by the included
standard headers) have one of the prefixes <code class="docutils literal notranslate"><span class="pre">Py</span></code> or <code class="docutils literal notranslate"><span class="pre">_Py</span></code>. Names beginning
with <code class="docutils literal notranslate"><span class="pre">_Py</span></code> are for internal use by the Python implementation and should not be
used by extension writers. Structure member names do not have a reserved prefix.</p>
<div class="admonition note">
<p class="admonition-title">توجه</p>
<p>User code should never define names that begin with <code class="docutils literal notranslate"><span class="pre">Py</span></code> or <code class="docutils literal notranslate"><span class="pre">_Py</span></code>. This
confuses the reader, and jeopardizes the portability of the user code to
future Python versions, which may define additional names beginning with one
of these prefixes.</p>
</div>
<p>The header files are typically installed with Python. On Unix, these are
located in the directories <code class="file docutils literal notranslate"><em><span class="pre">prefix</span></em><span class="pre">/include/pythonversion/</span></code> and
<code class="file docutils literal notranslate"><em><span class="pre">exec_prefix</span></em><span class="pre">/include/pythonversion/</span></code>, where <a class="reference internal" href="../using/configure.html#cmdoption-prefix"><code class="xref std std-option docutils literal notranslate"><span class="pre">prefix</span></code></a> and
<a class="reference internal" href="../using/configure.html#cmdoption-exec-prefix"><code class="xref std std-option docutils literal notranslate"><span class="pre">exec_prefix</span></code></a> are defined by the corresponding parameters to Python's
<strong class="program">configure</strong> script and <em>version</em> is
<code class="docutils literal notranslate"><span class="pre">'%d.%d'</span> <span class="pre">%</span> <span class="pre">sys.version_info[:2]</span></code>. On Windows, the headers are installed
in <code class="file docutils literal notranslate"><em><span class="pre">prefix</span></em><span class="pre">/include</span></code>, where <code class="docutils literal notranslate"><span class="pre">prefix</span></code> is the installation
directory specified to the installer.</p>
<p>To include the headers, place both directories (if different) on your compiler's
search path for includes. Do <em>not</em> place the parent directories on the search
path and then use <code class="docutils literal notranslate"><span class="pre">#include</span> <span class="pre"><pythonX.Y/Python.h></span></code>; this will break on
multi-platform builds since the platform independent headers under
<a class="reference internal" href="../using/configure.html#cmdoption-prefix"><code class="xref std std-option docutils literal notranslate"><span class="pre">prefix</span></code></a> include the platform specific headers from
<a class="reference internal" href="../using/configure.html#cmdoption-exec-prefix"><code class="xref std std-option docutils literal notranslate"><span class="pre">exec_prefix</span></code></a>.</p>
<p>C++ users should note that although the API is defined entirely using C, the
header files properly declare the entry points to be <code class="docutils literal notranslate"><span class="pre">extern</span> <span class="pre">"C"</span></code>. As a result,
there is no need to do anything special to use the API from C++.</p>
</section>
<section id="useful-macros">
<h2>Useful macros<a class="headerlink" href="#useful-macros" title="Link to this heading">¶</a></h2>
<p>Several useful macros are defined in the Python header files. Many are
defined closer to where they are useful (for example, <a class="reference internal" href="none.html#c.Py_RETURN_NONE" title="Py_RETURN_NONE"><code class="xref c c-macro docutils literal notranslate"><span class="pre">Py_RETURN_NONE</span></code></a>,
<a class="reference internal" href="extension-modules.html#c.PyMODINIT_FUNC" title="PyMODINIT_FUNC"><code class="xref c c-macro docutils literal notranslate"><span class="pre">PyMODINIT_FUNC</span></code></a>).
Others of a more general utility are defined here. This is not necessarily a
complete listing.</p>
<dl class="c macro">
<dt class="sig sig-object c" id="c.Py_CAN_START_THREADS">
<span class="sig-name descname"><span class="n"><span class="pre">Py_CAN_START_THREADS</span></span></span><a class="headerlink" href="#c.Py_CAN_START_THREADS" title="Link to this definition">¶</a><br /></dt>
<dd><p>If this macro is defined, then the current system is able to start threads.</p>
<p>Currently, all systems supported by CPython (per <span class="target" id="index-1"></span><a class="pep reference external" href="https://peps.python.org/pep-0011/"><strong>PEP 11</strong></a>), with the
exception of some WebAssembly platforms, support starting threads.</p>
<div class="versionadded">
<p><span class="versionmodified added">Added in version 3.13.</span></p>
</div>
</dd></dl>
<dl class="c macro">
<dt class="sig sig-object c" id="c.Py_GETENV">
<span class="sig-name descname"><span class="n"><span class="pre">Py_GETENV</span></span></span><span class="sig-paren">(</span><span class="n"><span class="pre">s</span></span><span class="sig-paren">)</span><a class="headerlink" href="#c.Py_GETENV" title="Link to this definition">¶</a><br /></dt>
<dd><p>Like <code class="samp docutils literal notranslate"><span class="pre">getenv(</span><em><span class="pre">s</span></em><span class="pre">)</span></code>, but returns <code class="docutils literal notranslate"><span class="pre">NULL</span></code> if <a class="reference internal" href="../using/cmdline.html#cmdoption-E"><code class="xref std std-option docutils literal notranslate"><span class="pre">-E</span></code></a> was passed
on the command line (see <a class="reference internal" href="init_config.html#c.PyConfig.use_environment" title="PyConfig.use_environment"><code class="xref c c-member docutils literal notranslate"><span class="pre">PyConfig.use_environment</span></code></a>).</p>
</dd></dl>
<section id="docstring-macros">
<h3>Docstring macros<a class="headerlink" href="#docstring-macros" title="Link to this heading">¶</a></h3>
<dl class="c macro">
<dt class="sig sig-object c" id="c.PyDoc_STRVAR">
<span class="sig-name descname"><span class="n"><span class="pre">PyDoc_STRVAR</span></span></span><span class="sig-paren">(</span><span class="n"><span class="pre">name</span></span>, <span class="n"><span class="pre">str</span></span><span class="sig-paren">)</span><a class="headerlink" href="#c.PyDoc_STRVAR" title="Link to this definition">¶</a><br /></dt>
<dd><p>Creates a variable with name <em>name</em> that can be used in docstrings.
If Python is built without docstrings (<a class="reference internal" href="../using/configure.html#cmdoption-without-doc-strings"><code class="xref std std-option docutils literal notranslate"><span class="pre">--without-doc-strings</span></code></a>),
the value will be an empty string.</p>
<p>Example:</p>
<div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="n">PyDoc_STRVAR</span><span class="p">(</span><span class="n">pop_doc</span><span class="p">,</span><span class="w"> </span><span class="s">"Remove and return the rightmost element."</span><span class="p">);</span>
<span class="k">static</span><span class="w"> </span><span class="n">PyMethodDef</span><span class="w"> </span><span class="n">deque_methods</span><span class="p">[]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="c1">// ...</span>
<span class="w"> </span><span class="p">{</span><span class="s">"pop"</span><span class="p">,</span><span class="w"> </span><span class="p">(</span><span class="n">PyCFunction</span><span class="p">)</span><span class="n">deque_pop</span><span class="p">,</span><span class="w"> </span><span class="n">METH_NOARGS</span><span class="p">,</span><span class="w"> </span><span class="n">pop_doc</span><span class="p">},</span>
<span class="w"> </span><span class="c1">// ...</span>
<span class="p">}</span>
</pre></div>
</div>
<p>Expands to <code class="samp docutils literal notranslate"><span class="pre">PyDoc_VAR(</span><em><span class="pre">name</span></em><span class="pre">)</span> <span class="pre">=</span> <span class="pre">PyDoc_STR(</span><em><span class="pre">str</span></em><span class="pre">)</span></code>.</p>
</dd></dl>
<dl class="c macro">
<dt class="sig sig-object c" id="c.PyDoc_STR">
<span class="sig-name descname"><span class="n"><span class="pre">PyDoc_STR</span></span></span><span class="sig-paren">(</span><span class="n"><span class="pre">str</span></span><span class="sig-paren">)</span><a class="headerlink" href="#c.PyDoc_STR" title="Link to this definition">¶</a><br /></dt>
<dd><p>Expands to the given input string, or an empty string
if docstrings are disabled (<a class="reference internal" href="../using/configure.html#cmdoption-without-doc-strings"><code class="xref std std-option docutils literal notranslate"><span class="pre">--without-doc-strings</span></code></a>).</p>
<p>Example:</p>
<div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="k">static</span><span class="w"> </span><span class="n">PyMethodDef</span><span class="w"> </span><span class="n">pysqlite_row_methods</span><span class="p">[]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="p">{</span><span class="s">"keys"</span><span class="p">,</span><span class="w"> </span><span class="p">(</span><span class="n">PyCFunction</span><span class="p">)</span><span class="n">pysqlite_row_keys</span><span class="p">,</span><span class="w"> </span><span class="n">METH_NOARGS</span><span class="p">,</span>
<span class="w"> </span><span class="n">PyDoc_STR</span><span class="p">(</span><span class="s">"Returns the keys of the row."</span><span class="p">)},</span>
<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="p">};</span>
</pre></div>
</div>
</dd></dl>
<dl class="c macro">
<dt class="sig sig-object c" id="c.PyDoc_VAR">
<span class="sig-name descname"><span class="n"><span class="pre">PyDoc_VAR</span></span></span><span class="sig-paren">(</span><span class="n"><span class="pre">name</span></span><span class="sig-paren">)</span><a class="headerlink" href="#c.PyDoc_VAR" title="Link to this definition">¶</a><br /></dt>
<dd><p>Declares a static character array variable with the given <em>name</em>.
Expands to <code class="samp docutils literal notranslate"><span class="pre">static</span> <span class="pre">const</span> <span class="pre">char</span> <em><span class="pre">name</span></em><span class="pre">[]</span></code></p>
<p>For example:</p>
<div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="n">PyDoc_VAR</span><span class="p">(</span><span class="n">python_doc</span><span class="p">)</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">PyDoc_STR</span><span class="p">(</span>
<span class="w"> </span><span class="s">"A genus of constricting snakes in the Pythonidae family native "</span>
<span class="w"> </span><span class="s">"to the tropics and subtropics of the Eastern Hemisphere."</span><span class="p">);</span>
</pre></div>
</div>
</dd></dl>
</section>
<section id="general-utility-macros">
<h3>General utility macros<a class="headerlink" href="#general-utility-macros" title="Link to this heading">¶</a></h3>
<p>The following macros are for common tasks not specific to Python.</p>
<dl class="c macro">
<dt class="sig sig-object c" id="c.Py_UNUSED">
<span class="sig-name descname"><span class="n"><span class="pre">Py_UNUSED</span></span></span><span class="sig-paren">(</span><span class="n"><span class="pre">arg</span></span><span class="sig-paren">)</span><a class="headerlink" href="#c.Py_UNUSED" title="Link to this definition">¶</a><br /></dt>
<dd><p>Use this for unused arguments in a function definition to silence compiler
warnings. Example: <code class="docutils literal notranslate"><span class="pre">int</span> <span class="pre">func(int</span> <span class="pre">a,</span> <span class="pre">int</span> <span class="pre">Py_UNUSED(b))</span> <span class="pre">{</span> <span class="pre">return</span> <span class="pre">a;</span> <span class="pre">}</span></code>.</p>
<div class="versionadded">
<p><span class="versionmodified added">Added in version 3.4.</span></p>
</div>
</dd></dl>
<dl class="c macro">
<dt class="sig sig-object c" id="c.Py_GCC_ATTRIBUTE">
<span class="sig-name descname"><span class="n"><span class="pre">Py_GCC_ATTRIBUTE</span></span></span><span class="sig-paren">(</span><span class="n"><span class="pre">name</span></span><span class="sig-paren">)</span><a class="headerlink" href="#c.Py_GCC_ATTRIBUTE" title="Link to this definition">¶</a><br /></dt>
<dd><p>Use a GCC attribute <em>name</em>, hiding it from compilers that don't support GCC
attributes (such as MSVC).</p>
<p>This expands to <code class="samp docutils literal notranslate"><span class="pre">__attribute__((</span><em><span class="pre">name)</span></em><span class="pre">)</span></code> on a GCC compiler,
and expands to nothing on compilers that don't support GCC attributes.</p>
</dd></dl>
<section id="numeric-utilities">
<h4>Numeric utilities<a class="headerlink" href="#numeric-utilities" title="Link to this heading">¶</a></h4>
<dl class="c macro">
<dt class="sig sig-object c" id="c.Py_ABS">
<span class="sig-name descname"><span class="n"><span class="pre">Py_ABS</span></span></span><span class="sig-paren">(</span><span class="n"><span class="pre">x</span></span><span class="sig-paren">)</span><a class="headerlink" href="#c.Py_ABS" title="Link to this definition">¶</a><br /></dt>
<dd><p>Return the absolute value of <code class="docutils literal notranslate"><span class="pre">x</span></code>.</p>
<p>The argument may be evaluated more than once.
Consequently, do not pass an expression with side-effects directly
to this macro.</p>
<p>If the result cannot be represented (for example, if <code class="docutils literal notranslate"><span class="pre">x</span></code> has
<code class="xref c c-macro docutils literal notranslate"><span class="pre">INT_MIN</span></code> value for <span class="c-expr sig sig-inline c"><span class="kt">int</span></span> type), the behavior is
undefined.</p>
<p>Corresponds roughly to <code class="samp docutils literal notranslate"><span class="pre">((</span><em><span class="pre">x</span></em><span class="pre">)</span> <span class="pre"><</span> <span class="pre">0</span> <span class="pre">?</span> <span class="pre">-(</span><em><span class="pre">x</span></em><span class="pre">)</span> <span class="pre">:</span> <span class="pre">(</span><em><span class="pre">x</span></em><span class="pre">))</span></code></p>
<div class="versionadded">
<p><span class="versionmodified added">Added in version 3.3.</span></p>
</div>
</dd></dl>
<dl class="c macro">
<dt class="sig sig-object c" id="c.Py_MAX">
<span class="sig-name descname"><span class="n"><span class="pre">Py_MAX</span></span></span><span class="sig-paren">(</span><span class="n"><span class="pre">x</span></span>, <span class="n"><span class="pre">y</span></span><span class="sig-paren">)</span><a class="headerlink" href="#c.Py_MAX" title="Link to this definition">¶</a><br /></dt>
<dt class="sig sig-object c" id="c.Py_MIN">
<span class="sig-name descname"><span class="n"><span class="pre">Py_MIN</span></span></span><span class="sig-paren">(</span><span class="n"><span class="pre">x</span></span>, <span class="n"><span class="pre">y</span></span><span class="sig-paren">)</span><a class="headerlink" href="#c.Py_MIN" title="Link to this definition">¶</a><br /></dt>
<dd><p>Return the larger or smaller of the arguments, respectively.</p>
<p>Any arguments may be evaluated more than once.
Consequently, do not pass an expression with side-effects directly
to this macro.</p>
<p><code class="xref c c-macro docutils literal notranslate"><span class="pre">Py_MAX</span></code> corresponds roughly to
<code class="samp docutils literal notranslate"><span class="pre">(((</span><em><span class="pre">x</span></em><span class="pre">)</span> <span class="pre">></span> <span class="pre">(</span><em><span class="pre">y</span></em><span class="pre">))</span> <span class="pre">?</span> <span class="pre">(</span><em><span class="pre">x</span></em><span class="pre">)</span> <span class="pre">:</span> <span class="pre">(</span><em><span class="pre">y</span></em><span class="pre">))</span></code>.</p>
<div class="versionadded">
<p><span class="versionmodified added">Added in version 3.3.</span></p>
</div>
</dd></dl>
<dl class="c macro">
<dt class="sig sig-object c" id="c.Py_ARITHMETIC_RIGHT_SHIFT">
<span class="sig-name descname"><span class="n"><span class="pre">Py_ARITHMETIC_RIGHT_SHIFT</span></span></span><span class="sig-paren">(</span><span class="n"><span class="pre">type</span></span>, <span class="n"><span class="pre">integer</span></span>, <span class="n"><span class="pre">positions</span></span><span class="sig-paren">)</span><a class="headerlink" href="#c.Py_ARITHMETIC_RIGHT_SHIFT" title="Link to this definition">¶</a><br /></dt>
<dd><p>Similar to <code class="samp docutils literal notranslate"><em><span class="pre">integer</span></em> <span class="pre">>></span> <em><span class="pre">positions</span></em></code>, but forces sign extension,
as the C standard does not define whether a right-shift of a signed
integer will perform sign extension or a zero-fill.</p>
<p><em>integer</em> should be any signed integer type.
<em>positions</em> is the number of positions to shift to the right.</p>
<p>Both <em>integer</em> and <em>positions</em> can be evaluated more than once;
consequently, avoid directly passing a function call or some other
operation with side-effects to this macro. Instead, store the result as a
variable and then pass it.</p>
<p><em>type</em> is unused and only kept for backwards compatibility. Historically,
<em>type</em> was used to cast <em>integer</em>.</p>
<div class="versionchanged">
<p><span class="versionmodified changed">تغییر داده شده در نسخه 3.1: </span>This macro is now valid for all signed integer types, not just those for
which <code class="docutils literal notranslate"><span class="pre">unsigned</span> <span class="pre">type</span></code> is legal. As a result, <em>type</em> is no longer
used.</p>
</div>
</dd></dl>
<dl class="c macro">
<dt class="sig sig-object c" id="c.Py_CHARMASK">
<span class="sig-name descname"><span class="n"><span class="pre">Py_CHARMASK</span></span></span><span class="sig-paren">(</span><span class="n"><span class="pre">c</span></span><span class="sig-paren">)</span><a class="headerlink" href="#c.Py_CHARMASK" title="Link to this definition">¶</a><br /></dt>
<dd><p>Argument must be a character or an integer in the range [-128, 127] or [0,
255]. This macro returns <code class="docutils literal notranslate"><span class="pre">c</span></code> cast to an <code class="docutils literal notranslate"><span class="pre">unsigned</span> <span class="pre">char</span></code>.</p>
</dd></dl>
</section>
<section id="assertion-utilities">
<h4>Assertion utilities<a class="headerlink" href="#assertion-utilities" title="Link to this heading">¶</a></h4>
<dl class="c macro">
<dt class="sig sig-object c" id="c.Py_UNREACHABLE">
<span class="sig-name descname"><span class="n"><span class="pre">Py_UNREACHABLE</span></span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#c.Py_UNREACHABLE" title="Link to this definition">¶</a><br /></dt>
<dd><p>Use this when you have a code path that cannot be reached by design.
For example, in the <code class="docutils literal notranslate"><span class="pre">default:</span></code> clause in a <code class="docutils literal notranslate"><span class="pre">switch</span></code> statement for which
all possible values are covered in <code class="docutils literal notranslate"><span class="pre">case</span></code> statements. Use this in places
where you might be tempted to put an <code class="docutils literal notranslate"><span class="pre">assert(0)</span></code> or <code class="docutils literal notranslate"><span class="pre">abort()</span></code> call.</p>
<p>In release mode, the macro helps the compiler to optimize the code, and
avoids a warning about unreachable code. For example, the macro is
implemented with <code class="docutils literal notranslate"><span class="pre">__builtin_unreachable()</span></code> on GCC in release mode.</p>
<p>In debug mode, and on unsupported compilers, the macro expands to a call to
<a class="reference internal" href="sys.html#c.Py_FatalError" title="Py_FatalError"><code class="xref c c-func docutils literal notranslate"><span class="pre">Py_FatalError()</span></code></a>.</p>
<p>A use for <code class="docutils literal notranslate"><span class="pre">Py_UNREACHABLE()</span></code> is following a call to a function that
never returns but that is not declared <code class="docutils literal notranslate"><span class="pre">_Noreturn</span></code>.</p>
<p>If a code path is very unlikely code but can be reached under exceptional
case, this macro must not be used. For example, under low memory condition
or if a system call returns a value out of the expected range. In this
case, it's better to report the error to the caller. If the error cannot
be reported to caller, <a class="reference internal" href="sys.html#c.Py_FatalError" title="Py_FatalError"><code class="xref c c-func docutils literal notranslate"><span class="pre">Py_FatalError()</span></code></a> can be used.</p>
<div class="versionadded">
<p><span class="versionmodified added">Added in version 3.7.</span></p>
</div>
</dd></dl>
<dl class="c macro">
<dt class="sig sig-object c" id="c.Py_SAFE_DOWNCAST">
<span class="sig-name descname"><span class="n"><span class="pre">Py_SAFE_DOWNCAST</span></span></span><span class="sig-paren">(</span><span class="n"><span class="pre">value</span></span>, <span class="n"><span class="pre">larger</span></span>, <span class="n"><span class="pre">smaller</span></span><span class="sig-paren">)</span><a class="headerlink" href="#c.Py_SAFE_DOWNCAST" title="Link to this definition">¶</a><br /></dt>
<dd><p>Cast <em>value</em> to type <em>smaller</em> from type <em>larger</em>, validating that no
information was lost.</p>
<p>On release builds of Python, this is roughly equivalent to
<code class="samp docutils literal notranslate"><span class="pre">((</span><em><span class="pre">smaller</span></em><span class="pre">)</span> <em><span class="pre">value</span></em><span class="pre">)</span></code>
(in C++, <code class="samp docutils literal notranslate"><span class="pre">static_cast<</span><em><span class="pre">smaller</span></em><span class="pre">>(</span><em><span class="pre">value</span></em><span class="pre">)</span></code> will be used instead).</p>
<p>On debug builds (implying that <a class="reference internal" href="#c.Py_DEBUG" title="Py_DEBUG"><code class="xref c c-macro docutils literal notranslate"><span class="pre">Py_DEBUG</span></code></a> is defined), this asserts
that no information was lost with the cast from <em>larger</em> to <em>smaller</em>.</p>
<p><em>value</em>, <em>larger</em>, and <em>smaller</em> may all be evaluated more than once in the
expression; consequently, do not pass an expression with side-effects
directly to this macro.</p>
</dd></dl>
<dl class="c macro">
<dt class="sig sig-object c" id="c.Py_BUILD_ASSERT">
<span class="sig-name descname"><span class="n"><span class="pre">Py_BUILD_ASSERT</span></span></span><span class="sig-paren">(</span><span class="n"><span class="pre">cond</span></span><span class="sig-paren">)</span><a class="headerlink" href="#c.Py_BUILD_ASSERT" title="Link to this definition">¶</a><br /></dt>
<dd><p>Asserts a compile-time condition <em>cond</em>, as a statement.
The build will fail if the condition is false or cannot be evaluated at compile time.</p>
<p>Corresponds roughly to <code class="samp docutils literal notranslate"><span class="pre">static_assert(</span><em><span class="pre">cond</span></em><span class="pre">)</span></code> on C23 and above.</p>
<p>For example:</p>
<div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="n">Py_BUILD_ASSERT</span><span class="p">(</span><span class="k">sizeof</span><span class="p">(</span><span class="n">PyTime_t</span><span class="p">)</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="k">sizeof</span><span class="p">(</span><span class="kt">int64_t</span><span class="p">));</span>
</pre></div>
</div>
<div class="versionadded">
<p><span class="versionmodified added">Added in version 3.3.</span></p>
</div>
</dd></dl>
<dl class="c macro">
<dt class="sig sig-object c" id="c.Py_BUILD_ASSERT_EXPR">
<span class="sig-name descname"><span class="n"><span class="pre">Py_BUILD_ASSERT_EXPR</span></span></span><span class="sig-paren">(</span><span class="n"><span class="pre">cond</span></span><span class="sig-paren">)</span><a class="headerlink" href="#c.Py_BUILD_ASSERT_EXPR" title="Link to this definition">¶</a><br /></dt>
<dd><p>Asserts a compile-time condition <em>cond</em>, as an expression that evaluates to <code class="docutils literal notranslate"><span class="pre">0</span></code>.
The build will fail if the condition is false or cannot be evaluated at compile time.</p>
<p>For example:</p>
<div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="cp">#define foo_to_char(foo) \</span>
<span class="cp"> ((char *)(foo) + Py_BUILD_ASSERT_EXPR(offsetof(struct foo, string) == 0))</span>
</pre></div>
</div>
<div class="versionadded">
<p><span class="versionmodified added">Added in version 3.3.</span></p>
</div>
</dd></dl>
</section>
<section id="type-size-utilities">
<h4>Type size utilities<a class="headerlink" href="#type-size-utilities" title="Link to this heading">¶</a></h4>
<dl class="c macro">
<dt class="sig sig-object c" id="c.Py_ARRAY_LENGTH">
<span class="sig-name descname"><span class="n"><span class="pre">Py_ARRAY_LENGTH</span></span></span><span class="sig-paren">(</span><span class="n"><span class="pre">array</span></span><span class="sig-paren">)</span><a class="headerlink" href="#c.Py_ARRAY_LENGTH" title="Link to this definition">¶</a><br /></dt>
<dd><p>Compute the length of a statically allocated C array at compile time.</p>
<p>The <em>array</em> argument must be a C array with a size known at compile time.
Passing an array with an unknown size, such as a heap-allocated array,
will result in a compilation error on some compilers, or otherwise produce
incorrect results.</p>
<p>This is roughly equivalent to:</p>
<div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="k">sizeof</span><span class="p">(</span><span class="n">array</span><span class="p">)</span><span class="w"> </span><span class="o">/</span><span class="w"> </span><span class="k">sizeof</span><span class="p">((</span><span class="n">array</span><span class="p">)[</span><span class="mi">0</span><span class="p">])</span>
</pre></div>
</div>
</dd></dl>
<dl class="c macro">
<dt class="sig sig-object c" id="c.Py_MEMBER_SIZE">
<span class="sig-name descname"><span class="n"><span class="pre">Py_MEMBER_SIZE</span></span></span><span class="sig-paren">(</span><span class="n"><span class="pre">type</span></span>, <span class="n"><span class="pre">member</span></span><span class="sig-paren">)</span><a class="headerlink" href="#c.Py_MEMBER_SIZE" title="Link to this definition">¶</a><br /></dt>
<dd><p>Return the size of a structure (<em>type</em>) <em>member</em> in bytes.</p>
<p>Corresponds roughly to <code class="samp docutils literal notranslate"><span class="pre">sizeof(((</span><em><span class="pre">type</span></em> <span class="pre">*)NULL)-></span><em><span class="pre">member</span></em><span class="pre">)</span></code>.</p>
<div class="versionadded">
<p><span class="versionmodified added">Added in version 3.6.</span></p>
</div>
</dd></dl>
</section>
<section id="macro-definition-utilities">
<h4>Macro definition utilities<a class="headerlink" href="#macro-definition-utilities" title="Link to this heading">¶</a></h4>
<dl class="c macro">
<dt class="sig sig-object c" id="c.Py_FORCE_EXPANSION">
<span class="sig-name descname"><span class="n"><span class="pre">Py_FORCE_EXPANSION</span></span></span><span class="sig-paren">(</span><span class="n"><span class="pre">X</span></span><span class="sig-paren">)</span><a class="headerlink" href="#c.Py_FORCE_EXPANSION" title="Link to this definition">¶</a><br /></dt>
<dd><p>This is equivalent to <code class="samp docutils literal notranslate"><em><span class="pre">X</span></em></code>, which is useful for token-pasting in
macros, as macro expansions in <em>X</em> are forcefully evaluated by the
preprocessor.</p>
</dd></dl>
<dl class="c macro">
<dt class="sig sig-object c" id="c.Py_STRINGIFY">
<span class="sig-name descname"><span class="n"><span class="pre">Py_STRINGIFY</span></span></span><span class="sig-paren">(</span><span class="n"><span class="pre">x</span></span><span class="sig-paren">)</span><a class="headerlink" href="#c.Py_STRINGIFY" title="Link to this definition">¶</a><br /></dt>
<dd><p>Convert <code class="docutils literal notranslate"><span class="pre">x</span></code> to a C string. For example, <code class="docutils literal notranslate"><span class="pre">Py_STRINGIFY(123)</span></code> returns
<code class="docutils literal notranslate"><span class="pre">"123"</span></code>.</p>
<div class="versionadded">
<p><span class="versionmodified added">Added in version 3.4.</span></p>
</div>
</dd></dl>
</section>
</section>
<section id="declaration-utilities">
<h3>Declaration utilities<a class="headerlink" href="#declaration-utilities" title="Link to this heading">¶</a></h3>
<p>The following macros can be used in declarations.
They are most useful for defining the C API itself, and have limited use
for extension authors.
Most of them expand to compiler-specific spellings of common extensions
to the C language.</p>
<dl class="c macro">
<dt class="sig sig-object c" id="c.Py_ALWAYS_INLINE">
<span class="sig-name descname"><span class="n"><span class="pre">Py_ALWAYS_INLINE</span></span></span><a class="headerlink" href="#c.Py_ALWAYS_INLINE" title="Link to this definition">¶</a><br /></dt>
<dd><p>Ask the compiler to always inline a static inline function. The compiler can
ignore it and decide to not inline the function.</p>
<p>Corresponds to <code class="docutils literal notranslate"><span class="pre">always_inline</span></code> attribute in GCC and <code class="docutils literal notranslate"><span class="pre">__forceinline</span></code>
in MSVC.</p>
<p>It can be used to inline performance critical static inline functions when
building Python in debug mode with function inlining disabled. For example,
MSC disables function inlining when building in debug mode.</p>
<p>Marking blindly a static inline function with Py_ALWAYS_INLINE can result in
worse performances (due to increased code size for example). The compiler is
usually smarter than the developer for the cost/benefit analysis.</p>
<p>If Python is <a class="reference internal" href="../using/configure.html#debug-build"><span class="std std-ref">built in debug mode</span></a> (if the <a class="reference internal" href="#c.Py_DEBUG" title="Py_DEBUG"><code class="xref c c-macro docutils literal notranslate"><span class="pre">Py_DEBUG</span></code></a>
macro is defined), the <a class="reference internal" href="#c.Py_ALWAYS_INLINE" title="Py_ALWAYS_INLINE"><code class="xref c c-macro docutils literal notranslate"><span class="pre">Py_ALWAYS_INLINE</span></code></a> macro does nothing.</p>
<p>It must be specified before the function return type. Usage:</p>
<div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="k">static</span><span class="w"> </span><span class="kr">inline</span><span class="w"> </span><span class="n">Py_ALWAYS_INLINE</span><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">random</span><span class="p">(</span><span class="kt">void</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="mi">4</span><span class="p">;</span><span class="w"> </span><span class="p">}</span>
</pre></div>
</div>
<div class="versionadded">
<p><span class="versionmodified added">Added in version 3.11.</span></p>
</div>
</dd></dl>
<dl class="c macro">
<dt class="sig sig-object c" id="c.Py_NO_INLINE">
<span class="sig-name descname"><span class="n"><span class="pre">Py_NO_INLINE</span></span></span><a class="headerlink" href="#c.Py_NO_INLINE" title="Link to this definition">¶</a><br /></dt>
<dd><p>Disable inlining on a function. For example, it reduces the C stack
consumption: useful on LTO+PGO builds which heavily inline code (see
<a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=33720">bpo-33720</a>).</p>
<p>Corresponds to the <code class="docutils literal notranslate"><span class="pre">noinline</span></code> attribute/specification on GCC and MSVC.</p>
<p>Usage:</p>
<div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="n">Py_NO_INLINE</span><span class="w"> </span><span class="k">static</span><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">random</span><span class="p">(</span><span class="kt">void</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="mi">4</span><span class="p">;</span><span class="w"> </span><span class="p">}</span>
</pre></div>
</div>
<div class="versionadded">
<p><span class="versionmodified added">Added in version 3.11.</span></p>
</div>
</dd></dl>
<dl class="c macro">
<dt class="sig sig-object c" id="c.Py_DEPRECATED">
<span class="sig-name descname"><span class="n"><span class="pre">Py_DEPRECATED</span></span></span><span class="sig-paren">(</span><span class="n"><span class="pre">version</span></span><span class="sig-paren">)</span><a class="headerlink" href="#c.Py_DEPRECATED" title="Link to this definition">¶</a><br /></dt>
<dd><p>Use this to declare APIs that were deprecated in a specific CPython version.
The macro must be placed before the symbol name.</p>
<p>Example:</p>
<div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="n">Py_DEPRECATED</span><span class="p">(</span><span class="mf">3.8</span><span class="p">)</span><span class="w"> </span><span class="n">PyAPI_FUNC</span><span class="p">(</span><span class="kt">int</span><span class="p">)</span><span class="w"> </span><span class="n">Py_OldFunction</span><span class="p">(</span><span class="kt">void</span><span class="p">);</span>
</pre></div>
</div>
<div class="versionchanged">
<p><span class="versionmodified changed">تغییر داده شده در نسخه 3.8: </span>MSVC support was added.</p>
</div>
</dd></dl>
<dl class="c macro">
<dt class="sig sig-object c" id="c.Py_LOCAL">
<span class="sig-name descname"><span class="n"><span class="pre">Py_LOCAL</span></span></span><span class="sig-paren">(</span><span class="n"><span class="pre">type</span></span><span class="sig-paren">)</span><a class="headerlink" href="#c.Py_LOCAL" title="Link to this definition">¶</a><br /></dt>
<dd><p>Declare a function returning the specified <em>type</em> using a fast-calling
qualifier for functions that are local to the current file.
Semantically, this is equivalent to <code class="samp docutils literal notranslate"><span class="pre">static</span> <em><span class="pre">type</span></em></code>.</p>
</dd></dl>
<dl class="c macro">
<dt class="sig sig-object c" id="c.Py_LOCAL_INLINE">
<span class="sig-name descname"><span class="n"><span class="pre">Py_LOCAL_INLINE</span></span></span><span class="sig-paren">(</span><span class="n"><span class="pre">type</span></span><span class="sig-paren">)</span><a class="headerlink" href="#c.Py_LOCAL_INLINE" title="Link to this definition">¶</a><br /></dt>
<dd><p>Equivalent to <a class="reference internal" href="#c.Py_LOCAL" title="Py_LOCAL"><code class="xref c c-macro docutils literal notranslate"><span class="pre">Py_LOCAL</span></code></a> but additionally requests the function
be inlined.</p>
</dd></dl>
<dl class="c macro">
<dt class="sig sig-object c" id="c.Py_LOCAL_SYMBOL">
<span class="sig-name descname"><span class="n"><span class="pre">Py_LOCAL_SYMBOL</span></span></span><a class="headerlink" href="#c.Py_LOCAL_SYMBOL" title="Link to this definition">¶</a><br /></dt>
<dd><p>Macro used to declare a symbol as local to the shared library (hidden).
On supported platforms, it ensures the symbol is not exported.</p>
<p>On compatible versions of GCC/Clang, it
expands to <code class="docutils literal notranslate"><span class="pre">__attribute__((visibility("hidden")))</span></code>.</p>
</dd></dl>
<dl class="c macro">
<dt class="sig sig-object c" id="c.Py_EXPORTED_SYMBOL">
<span class="sig-name descname"><span class="n"><span class="pre">Py_EXPORTED_SYMBOL</span></span></span><a class="headerlink" href="#c.Py_EXPORTED_SYMBOL" title="Link to this definition">¶</a><br /></dt>
<dd><p>Macro used to declare a symbol (function or data) as exported.
On Windows, this expands to <code class="docutils literal notranslate"><span class="pre">__declspec(dllexport)</span></code>.
On compatible versions of GCC/Clang, it
expands to <code class="docutils literal notranslate"><span class="pre">__attribute__((visibility("default")))</span></code>.
This macro is for defining the C API itself; extension modules should not use it.</p>
</dd></dl>
<dl class="c macro">
<dt class="sig sig-object c" id="c.Py_IMPORTED_SYMBOL">
<span class="sig-name descname"><span class="n"><span class="pre">Py_IMPORTED_SYMBOL</span></span></span><a class="headerlink" href="#c.Py_IMPORTED_SYMBOL" title="Link to this definition">¶</a><br /></dt>
<dd><p>Macro used to declare a symbol as imported.
On Windows, this expands to <code class="docutils literal notranslate"><span class="pre">__declspec(dllimport)</span></code>.
This macro is for defining the C API itself; extension modules should not use it.</p>
</dd></dl>
<dl class="c macro">
<dt class="sig sig-object c" id="c.PyAPI_FUNC">
<span class="sig-name descname"><span class="n"><span class="pre">PyAPI_FUNC</span></span></span><span class="sig-paren">(</span><span class="n"><span class="pre">type</span></span><span class="sig-paren">)</span><a class="headerlink" href="#c.PyAPI_FUNC" title="Link to this definition">¶</a><br /></dt>
<dd><p>Macro used by CPython to declare a function as part of the C API.
Its expansion depends on the platform and build configuration.
This macro is intended for defining CPython's C API itself;
extension modules should not use it for their own symbols.</p>
</dd></dl>
<dl class="c macro">
<dt class="sig sig-object c" id="c.PyAPI_DATA">
<span class="sig-name descname"><span class="n"><span class="pre">PyAPI_DATA</span></span></span><span class="sig-paren">(</span><span class="n"><span class="pre">type</span></span><span class="sig-paren">)</span><a class="headerlink" href="#c.PyAPI_DATA" title="Link to this definition">¶</a><br /></dt>
<dd><p>Macro used by CPython to declare a public global variable as part of the C API.
Its expansion depends on the platform and build configuration.
This macro is intended for defining CPython's C API itself;
extension modules should not use it for their own symbols.</p>
</dd></dl>
</section>
<section id="outdated-macros">
<h3>Outdated macros<a class="headerlink" href="#outdated-macros" title="Link to this heading">¶</a></h3>
<p>The following macros have been used to features that have been standardized
in C11.</p>
<dl class="c macro">
<dt class="sig sig-object c" id="c.Py_ALIGNED">
<span class="sig-name descname"><span class="n"><span class="pre">Py_ALIGNED</span></span></span><span class="sig-paren">(</span><span class="n"><span class="pre">num</span></span><span class="sig-paren">)</span><a class="headerlink" href="#c.Py_ALIGNED" title="Link to this definition">¶</a><br /></dt>
<dd><p>Specify alignment to <em>num</em> bytes on compilers that support it.</p>
<p>Consider using the C11 standard <code class="docutils literal notranslate"><span class="pre">_Alignas</span></code> specifier over this macro.</p>
</dd></dl>
<dl class="c macro">
<dt class="sig sig-object c" id="c.Py_LL">
<span class="sig-name descname"><span class="n"><span class="pre">Py_LL</span></span></span><span class="sig-paren">(</span><span class="n"><span class="pre">number</span></span><span class="sig-paren">)</span><a class="headerlink" href="#c.Py_LL" title="Link to this definition">¶</a><br /></dt>
<dt class="sig sig-object c" id="c.Py_ULL">
<span class="sig-name descname"><span class="n"><span class="pre">Py_ULL</span></span></span><span class="sig-paren">(</span><span class="n"><span class="pre">number</span></span><span class="sig-paren">)</span><a class="headerlink" href="#c.Py_ULL" title="Link to this definition">¶</a><br /></dt>
<dd><p>Use <em>number</em> as a <code class="docutils literal notranslate"><span class="pre">long</span> <span class="pre">long</span></code> or <code class="docutils literal notranslate"><span class="pre">unsigned</span> <span class="pre">long</span> <span class="pre">long</span></code> integer literal,
respectively.</p>
<p>Expands to <em>number</em> followed by <code class="docutils literal notranslate"><span class="pre">LL</span></code> or <code class="docutils literal notranslate"><span class="pre">LLU</span></code>, respectively, but will
expand to some compiler-specific suffixes on some older compilers.</p>
<p>Consider using the C99 standard suffixes <code class="docutils literal notranslate"><span class="pre">LL</span></code> and <code class="docutils literal notranslate"><span class="pre">LLU</span></code> directly.</p>
</dd></dl>
<dl class="c macro">
<dt class="sig sig-object c" id="c.Py_MEMCPY">
<span class="sig-name descname"><span class="n"><span class="pre">Py_MEMCPY</span></span></span><span class="sig-paren">(</span><span class="n"><span class="pre">dest</span></span>, <span class="n"><span class="pre">src</span></span>, <span class="n"><span class="pre">n</span></span><span class="sig-paren">)</span><a class="headerlink" href="#c.Py_MEMCPY" title="Link to this definition">¶</a><br /></dt>
<dd><p>This is a <a class="reference internal" href="../glossary.html#term-soft-deprecated"><span class="xref std std-term">soft deprecated</span></a> alias to <code class="xref c c-func docutils literal notranslate"><span class="pre">memcpy()</span></code>.
Use <code class="xref c c-func docutils literal notranslate"><span class="pre">memcpy()</span></code> directly instead.</p>
<div class="deprecated">
<p><span class="versionmodified deprecated">منسوخ شده از نسخه 3.14: </span>The macro is <a class="reference internal" href="../glossary.html#term-soft-deprecated"><span class="xref std std-term">soft deprecated</span></a>.</p>
</div>
</dd></dl>
<dl class="c macro">
<dt class="sig sig-object c" id="c.Py_VA_COPY">
<span class="sig-name descname"><span class="n"><span class="pre">Py_VA_COPY</span></span></span><a class="headerlink" href="#c.Py_VA_COPY" title="Link to this definition">¶</a><br /></dt>
<dd><p>This is a <a class="reference internal" href="../glossary.html#term-soft-deprecated"><span class="xref std std-term">soft deprecated</span></a> alias to the C99-standard <code class="docutils literal notranslate"><span class="pre">va_copy</span></code>
function.</p>
<p>Historically, this would use a compiler-specific method to copy a <code class="docutils literal notranslate"><span class="pre">va_list</span></code>.</p>
<div class="versionchanged">
<p><span class="versionmodified changed">تغییر داده شده در نسخه 3.6: </span>This is now an alias to <code class="docutils literal notranslate"><span class="pre">va_copy</span></code>.</p>
</div>
</dd></dl>
</section>
</section>
<section id="objects-types-and-reference-counts">
<span id="api-objects"></span><h2>Objects, Types and Reference Counts<a class="headerlink" href="#objects-types-and-reference-counts" title="Link to this heading">¶</a></h2>
<p id="index-2">Most Python/C API functions have one or more arguments as well as a return value
of type <span class="c-expr sig sig-inline c"><a class="reference internal" href="structures.html#c.PyObject" title="PyObject"><span class="n">PyObject</span></a><span class="p">*</span></span>. This type is a pointer to an opaque data type
representing an arbitrary Python object. Since all Python object types are
treated the same way by the Python language in most situations (e.g.,
assignments, scope rules, and argument passing), it is only fitting that they
should be represented by a single C type. Almost all Python objects live on the
heap: you never declare an automatic or static variable of type
<a class="reference internal" href="structures.html#c.PyObject" title="PyObject"><code class="xref c c-type docutils literal notranslate"><span class="pre">PyObject</span></code></a>, only pointer variables of type <span class="c-expr sig sig-inline c"><span class="n">PyObject</span><span class="p">*</span></span> can be
declared. The sole exception are the type objects; since these must never be
deallocated, they are typically static <a class="reference internal" href="type.html#c.PyTypeObject" title="PyTypeObject"><code class="xref c c-type docutils literal notranslate"><span class="pre">PyTypeObject</span></code></a> objects.</p>
<p>All Python objects (even Python integers) have a <em class="dfn">type</em> and a
<em class="dfn">reference count</em>. An object's type determines what kind of object it is
(e.g., an integer, a list, or a user-defined function; there are many more as
explained in <a class="reference internal" href="../reference/datamodel.html#types"><span class="std std-ref">The standard type hierarchy</span></a>). For each of the well-known types there is a macro
to check whether an object is of that type; for instance, <code class="docutils literal notranslate"><span class="pre">PyList_Check(a)</span></code> is
true if (and only if) the object pointed to by <em>a</em> is a Python list.</p>
<section id="reference-counts">
<span id="api-refcounts"></span><h3>Reference Counts<a class="headerlink" href="#reference-counts" title="Link to this heading">¶</a></h3>
<p>The reference count is important because today's computers have a finite
(and often severely limited) memory size; it counts how many different
places there are that have a <a class="reference internal" href="../glossary.html#term-strong-reference"><span class="xref std std-term">strong reference</span></a> to an object.
Such a place could be another object, or a global (or static) C variable,
or a local variable in some C function.
When the last <span class="xref std std-term">strong reference</span> to an object is released
(i.e. its reference count becomes zero), the object is deallocated.
If it contains references to other objects, those references are released.
Those other objects may be deallocated in turn, if there are no more
references to them, and so on. (There's an obvious problem with
objects that reference each other here; for now, the solution
is "don't do that.")</p>
<p id="index-3">Reference counts are always manipulated explicitly. The normal way is
to use the macro <a class="reference internal" href="refcounting.html#c.Py_INCREF" title="Py_INCREF"><code class="xref c c-func docutils literal notranslate"><span class="pre">Py_INCREF()</span></code></a> to take a new reference to an
object (i.e. increment its reference count by one),
and <a class="reference internal" href="refcounting.html#c.Py_DECREF" title="Py_DECREF"><code class="xref c c-func docutils literal notranslate"><span class="pre">Py_DECREF()</span></code></a> to release that reference (i.e. decrement the
reference count by one). The <code class="xref c c-func docutils literal notranslate"><span class="pre">Py_DECREF()</span></code> macro
is considerably more complex than the incref one, since it must check whether
the reference count becomes zero and then cause the object's deallocator to be
called. The deallocator is a function pointer contained in the object's type
structure. The type-specific deallocator takes care of releasing references
for other objects contained in the object if this is a compound
object type, such as a list, as well as performing any additional finalization
that's needed. There's no chance that the reference count can overflow; at
least as many bits are used to hold the reference count as there are distinct
memory locations in virtual memory (assuming <code class="docutils literal notranslate"><span class="pre">sizeof(Py_ssize_t)</span> <span class="pre">>=</span> <span class="pre">sizeof(void*)</span></code>).
Thus, the reference count increment is a simple operation.</p>
<p>It is not necessary to hold a <a class="reference internal" href="../glossary.html#term-strong-reference"><span class="xref std std-term">strong reference</span></a> (i.e. increment
the reference count) for every local variable that contains a pointer
to an object. In theory, the object's
reference count goes up by one when the variable is made to point to it and it
goes down by one when the variable goes out of scope. However, these two
cancel each other out, so at the end the reference count hasn't changed. The
only real reason to use the reference count is to prevent the object from being
deallocated as long as our variable is pointing to it. If we know that there
is at least one other reference to the object that lives at least as long as
our variable, there is no need to take a new <span class="xref std std-term">strong reference</span>
(i.e. increment the reference count) temporarily.
An important situation where this arises is in objects that are passed as
arguments to C functions in an extension module that are called from Python;
the call mechanism guarantees to hold a reference to every argument for the
duration of the call.</p>
<p>However, a common pitfall is to extract an object from a list and hold on to it
for a while without taking a new reference. Some other operation might
conceivably remove the object from the list, releasing that reference,
and possibly deallocating it. The real danger is that innocent-looking
operations may invoke arbitrary Python code which could do this; there is a code
path which allows control to flow back to the user from a <a class="reference internal" href="refcounting.html#c.Py_DECREF" title="Py_DECREF"><code class="xref c c-func docutils literal notranslate"><span class="pre">Py_DECREF()</span></code></a>, so
almost any operation is potentially dangerous.</p>
<p>A safe approach is to always use the generic operations (functions whose name
begins with <code class="docutils literal notranslate"><span class="pre">PyObject_</span></code>, <code class="docutils literal notranslate"><span class="pre">PyNumber_</span></code>, <code class="docutils literal notranslate"><span class="pre">PySequence_</span></code> or <code class="docutils literal notranslate"><span class="pre">PyMapping_</span></code>).
These operations always create a new <a class="reference internal" href="../glossary.html#term-strong-reference"><span class="xref std std-term">strong reference</span></a>
(i.e. increment the reference count) of the object they return.
This leaves the caller with the responsibility to call <a class="reference internal" href="refcounting.html#c.Py_DECREF" title="Py_DECREF"><code class="xref c c-func docutils literal notranslate"><span class="pre">Py_DECREF()</span></code></a> when
they are done with the result; this soon becomes second nature.</p>
<section id="reference-count-details">
<span id="api-refcountdetails"></span><h4>Reference Count Details<a class="headerlink" href="#reference-count-details" title="Link to this heading">¶</a></h4>
<p>The reference count behavior of functions in the Python/C API is best explained
in terms of <em>ownership of references</em>. Ownership pertains to references, never
to objects (objects are not owned: they are always shared). "Owning a
reference" means being responsible for calling Py_DECREF on it when the
reference is no longer needed. Ownership can also be transferred, meaning that
the code that receives ownership of the reference then becomes responsible for
eventually releasing it by calling <a class="reference internal" href="refcounting.html#c.Py_DECREF" title="Py_DECREF"><code class="xref c c-func docutils literal notranslate"><span class="pre">Py_DECREF()</span></code></a> or <a class="reference internal" href="refcounting.html#c.Py_XDECREF" title="Py_XDECREF"><code class="xref c c-func docutils literal notranslate"><span class="pre">Py_XDECREF()</span></code></a>
when it's no longer needed---or passing on this responsibility (usually to its
caller). When a function passes ownership of a reference on to its caller, the
caller is said to receive a <em>new</em> reference. When no ownership is transferred,
the caller is said to <em>borrow</em> the reference. Nothing needs to be done for a
<a class="reference internal" href="../glossary.html#term-borrowed-reference"><span class="xref std std-term">borrowed reference</span></a>.</p>
<p>Conversely, when a calling function passes in a reference to an object, there
are two possibilities: the function <em>steals</em> a reference to the object, or it
does not. <em>Stealing a reference</em> means that when you pass a reference to a
function, that function assumes that it now owns that reference, and you are not
responsible for it any longer.</p>
<p id="index-4">Few functions steal references; the two notable exceptions are
<a class="reference internal" href="list.html#c.PyList_SetItem" title="PyList_SetItem"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyList_SetItem()</span></code></a> and <a class="reference internal" href="tuple.html#c.PyTuple_SetItem" title="PyTuple_SetItem"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyTuple_SetItem()</span></code></a>, which steal a reference
to the item (but not to the tuple or list into which the item is put!). These
functions were designed to steal a reference because of a common idiom for
populating a tuple or list with newly created objects; for example, the code to
create the tuple <code class="docutils literal notranslate"><span class="pre">(1,</span> <span class="pre">2,</span> <span class="pre">"three")</span></code> could look like this (forgetting about
error handling for the moment; a better way to code this is shown below):</p>
<div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="n">PyObject</span><span class="w"> </span><span class="o">*</span><span class="n">t</span><span class="p">;</span>
<span class="n">t</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">PyTuple_New</span><span class="p">(</span><span class="mi">3</span><span class="p">);</span>
<span class="n">PyTuple_SetItem</span><span class="p">(</span><span class="n">t</span><span class="p">,</span><span class="w"> </span><span class="mi">0</span><span class="p">,</span><span class="w"> </span><span class="n">PyLong_FromLong</span><span class="p">(</span><span class="mf">1L</span><span class="p">));</span>
<span class="n">PyTuple_SetItem</span><span class="p">(</span><span class="n">t</span><span class="p">,</span><span class="w"> </span><span class="mi">1</span><span class="p">,</span><span class="w"> </span><span class="n">PyLong_FromLong</span><span class="p">(</span><span class="mf">2L</span><span class="p">));</span>
<span class="n">PyTuple_SetItem</span><span class="p">(</span><span class="n">t</span><span class="p">,</span><span class="w"> </span><span class="mi">2</span><span class="p">,</span><span class="w"> </span><span class="n">PyUnicode_FromString</span><span class="p">(</span><span class="s">"three"</span><span class="p">));</span>
</pre></div>
</div>
<p>Here, <a class="reference internal" href="long.html#c.PyLong_FromLong" title="PyLong_FromLong"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyLong_FromLong()</span></code></a> returns a new reference which is immediately
stolen by <a class="reference internal" href="tuple.html#c.PyTuple_SetItem" title="PyTuple_SetItem"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyTuple_SetItem()</span></code></a>. When you want to keep using an object
although the reference to it will be stolen, use <a class="reference internal" href="refcounting.html#c.Py_INCREF" title="Py_INCREF"><code class="xref c c-func docutils literal notranslate"><span class="pre">Py_INCREF()</span></code></a> to grab
another reference before calling the reference-stealing function.</p>
<p>Incidentally, <a class="reference internal" href="tuple.html#c.PyTuple_SetItem" title="PyTuple_SetItem"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyTuple_SetItem()</span></code></a> is the <em>only</em> way to set tuple items;
<a class="reference internal" href="sequence.html#c.PySequence_SetItem" title="PySequence_SetItem"><code class="xref c c-func docutils literal notranslate"><span class="pre">PySequence_SetItem()</span></code></a> and <a class="reference internal" href="object.html#c.PyObject_SetItem" title="PyObject_SetItem"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyObject_SetItem()</span></code></a> refuse to do this
since tuples are an immutable data type. You should only use
<code class="xref c c-func docutils literal notranslate"><span class="pre">PyTuple_SetItem()</span></code> for tuples that you are creating yourself.</p>
<p>Equivalent code for populating a list can be written using <a class="reference internal" href="list.html#c.PyList_New" title="PyList_New"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyList_New()</span></code></a>
and <a class="reference internal" href="list.html#c.PyList_SetItem" title="PyList_SetItem"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyList_SetItem()</span></code></a>.</p>
<p>However, in practice, you will rarely use these ways of creating and populating
a tuple or list. There's a generic function, <a class="reference internal" href="arg.html#c.Py_BuildValue" title="Py_BuildValue"><code class="xref c c-func docutils literal notranslate"><span class="pre">Py_BuildValue()</span></code></a>, that can
create most common objects from C values, directed by a <em class="dfn">format string</em>.
For example, the above two blocks of code could be replaced by the following
(which also takes care of the error checking):</p>
<div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="n">PyObject</span><span class="w"> </span><span class="o">*</span><span class="n">tuple</span><span class="p">,</span><span class="w"> </span><span class="o">*</span><span class="n">list</span><span class="p">;</span>
<span class="n">tuple</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">Py_BuildValue</span><span class="p">(</span><span class="s">"(iis)"</span><span class="p">,</span><span class="w"> </span><span class="mi">1</span><span class="p">,</span><span class="w"> </span><span class="mi">2</span><span class="p">,</span><span class="w"> </span><span class="s">"three"</span><span class="p">);</span>
<span class="n">list</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">Py_BuildValue</span><span class="p">(</span><span class="s">"[iis]"</span><span class="p">,</span><span class="w"> </span><span class="mi">1</span><span class="p">,</span><span class="w"> </span><span class="mi">2</span><span class="p">,</span><span class="w"> </span><span class="s">"three"</span><span class="p">);</span>
</pre></div>
</div>
<p>It is much more common to use <a class="reference internal" href="object.html#c.PyObject_SetItem" title="PyObject_SetItem"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyObject_SetItem()</span></code></a> and friends with items
whose references you are only borrowing, like arguments that were passed in to
the function you are writing. In that case, their behaviour regarding references
is much saner, since you don't have to take a new reference just so you
can give that reference away ("have it be stolen"). For example, this function
sets all items of a list (actually, any mutable sequence) to a given item:</p>
<div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="kt">int</span>
<span class="nf">set_all</span><span class="p">(</span><span class="n">PyObject</span><span class="w"> </span><span class="o">*</span><span class="n">target</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">item</span><span class="p">)</span>
<span class="p">{</span>
<span class="w"> </span><span class="n">Py_ssize_t</span><span class="w"> </span><span class="n">i</span><span class="p">,</span><span class="w"> </span><span class="n">n</span><span class="p">;</span>
<span class="w"> </span><span class="n">n</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">PyObject_Length</span><span class="p">(</span><span class="n">target</span><span class="p">);</span>
<span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">n</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="mi">0</span><span class="p">)</span>
<span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="mi">-1</span><span class="p">;</span>
<span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="n">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="n">n</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="o">++</span><span class="p">)</span><span class="w"> </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">index</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">PyLong_FromSsize_t</span><span class="p">(</span><span class="n">i</span><span class="p">);</span>
<span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="o">!</span><span class="n">index</span><span class="p">)</span>
<span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="mi">-1</span><span class="p">;</span>
<span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">PyObject_SetItem</span><span class="p">(</span><span class="n">target</span><span class="p">,</span><span class="w"> </span><span class="n">index</span><span class="p">,</span><span class="w"> </span><span class="n">item</span><span class="p">)</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="mi">0</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="n">Py_DECREF</span><span class="p">(</span><span class="n">index</span><span class="p">);</span>
<span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="mi">-1</span><span class="p">;</span>
<span class="w"> </span><span class="p">}</span>
<span class="w"> </span><span class="n">Py_DECREF</span><span class="p">(</span><span class="n">index</span><span class="p">);</span>
<span class="w"> </span><span class="p">}</span>
<span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span>
<span class="p">}</span>
</pre></div>
</div>
<p id="index-5">The situation is slightly different for function return values. While passing
a reference to most functions does not change your ownership responsibilities
for that reference, many functions that return a reference to an object give
you ownership of the reference. The reason is simple: in many cases, the
returned object is created on the fly, and the reference you get is the only
reference to the object. Therefore, the generic functions that return object
references, like <a class="reference internal" href="object.html#c.PyObject_GetItem" title="PyObject_GetItem"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyObject_GetItem()</span></code></a> and <a class="reference internal" href="sequence.html#c.PySequence_GetItem" title="PySequence_GetItem"><code class="xref c c-func docutils literal notranslate"><span class="pre">PySequence_GetItem()</span></code></a>,
always return a new reference (the caller becomes the owner of the reference).</p>
<p>It is important to realize that whether you own a reference returned by a
function depends on which function you call only --- <em>the plumage</em> (the type of
the object passed as an argument to the function) <em>doesn't enter into it!</em>
Thus, if you extract an item from a list using <a class="reference internal" href="list.html#c.PyList_GetItem" title="PyList_GetItem"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyList_GetItem()</span></code></a>, you
don't own the reference --- but if you obtain the same item from the same list
using <a class="reference internal" href="sequence.html#c.PySequence_GetItem" title="PySequence_GetItem"><code class="xref c c-func docutils literal notranslate"><span class="pre">PySequence_GetItem()</span></code></a> (which happens to take exactly the same
arguments), you do own a reference to the returned object.</p>
<p id="index-6">Here is an example of how you could write a function that computes the sum of
the items in a list of integers; once using <a class="reference internal" href="list.html#c.PyList_GetItem" title="PyList_GetItem"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyList_GetItem()</span></code></a>, and once
using <a class="reference internal" href="sequence.html#c.PySequence_GetItem" title="PySequence_GetItem"><code class="xref c c-func docutils literal notranslate"><span class="pre">PySequence_GetItem()</span></code></a>.</p>
<div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="kt">long</span>
<span class="nf">sum_list</span><span class="p">(</span><span class="n">PyObject</span><span class="w"> </span><span class="o">*</span><span class="n">list</span><span class="p">)</span>
<span class="p">{</span>
<span class="w"> </span><span class="n">Py_ssize_t</span><span class="w"> </span><span class="n">i</span><span class="p">,</span><span class="w"> </span><span class="n">n</span><span class="p">;</span>
<span class="w"> </span><span class="kt">long</span><span class="w"> </span><span class="n">total</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">,</span><span class="w"> </span><span class="n">value</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">item</span><span class="p">;</span>
<span class="w"> </span><span class="n">n</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">PyList_Size</span><span class="p">(</span><span class="n">list</span><span class="p">);</span>
<span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">n</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="mi">0</span><span class="p">)</span>
<span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="mi">-1</span><span class="p">;</span><span class="w"> </span><span class="cm">/* Not a list */</span>
<span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="n">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="n">n</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="o">++</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="n">item</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">PyList_GetItem</span><span class="p">(</span><span class="n">list</span><span class="p">,</span><span class="w"> </span><span class="n">i</span><span class="p">);</span><span class="w"> </span><span class="cm">/* Can't fail */</span>
<span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="o">!</span><span class="n">PyLong_Check</span><span class="p">(</span><span class="n">item</span><span class="p">))</span><span class="w"> </span><span class="k">continue</span><span class="p">;</span><span class="w"> </span><span class="cm">/* Skip non-integers */</span>
<span class="w"> </span><span class="n">value</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">PyLong_AsLong</span><span class="p">(</span><span class="n">item</span><span class="p">);</span>
<span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">value</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="mi">-1</span><span class="w"> </span><span class="o">&&</span><span class="w"> </span><span class="n">PyErr_Occurred</span><span class="p">())</span>
<span class="w"> </span><span class="cm">/* Integer too big to fit in a C long, bail out */</span>
<span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="mi">-1</span><span class="p">;</span>
<span class="w"> </span><span class="n">total</span><span class="w"> </span><span class="o">+=</span><span class="w"> </span><span class="n">value</span><span class="p">;</span>
<span class="w"> </span><span class="p">}</span>
<span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="n">total</span><span class="p">;</span>
<span class="p">}</span>
</pre></div>
</div>
<div class="highlight-c notranslate" id="index-7"><div class="highlight"><pre><span></span><span class="kt">long</span>
<span class="nf">sum_sequence</span><span class="p">(</span><span class="n">PyObject</span><span class="w"> </span><span class="o">*</span><span class="n">sequence</span><span class="p">)</span>
<span class="p">{</span>
<span class="w"> </span><span class="n">Py_ssize_t</span><span class="w"> </span><span class="n">i</span><span class="p">,</span><span class="w"> </span><span class="n">n</span><span class="p">;</span>
<span class="w"> </span><span class="kt">long</span><span class="w"> </span><span class="n">total</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">,</span><span class="w"> </span><span class="n">value</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">item</span><span class="p">;</span>
<span class="w"> </span><span class="n">n</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">PySequence_Length</span><span class="p">(</span><span class="n">sequence</span><span class="p">);</span>
<span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">n</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="mi">0</span><span class="p">)</span>
<span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="mi">-1</span><span class="p">;</span><span class="w"> </span><span class="cm">/* Has no length */</span>
<span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="n">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="n">n</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="o">++</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="n">item</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">PySequence_GetItem</span><span class="p">(</span><span class="n">sequence</span><span class="p">,</span><span class="w"> </span><span class="n">i</span><span class="p">);</span>
<span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">item</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="k">return</span><span class="w"> </span><span class="mi">-1</span><span class="p">;</span><span class="w"> </span><span class="cm">/* Not a sequence, or other failure */</span>
<span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">PyLong_Check</span><span class="p">(</span><span class="n">item</span><span class="p">))</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="n">value</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">PyLong_AsLong</span><span class="p">(</span><span class="n">item</span><span class="p">);</span>
<span class="w"> </span><span class="n">Py_DECREF</span><span class="p">(</span><span class="n">item</span><span class="p">);</span>
<span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">value</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="mi">-1</span><span class="w"> </span><span class="o">&&</span><span class="w"> </span><span class="n">PyErr_Occurred</span><span class="p">())</span>
<span class="w"> </span><span class="cm">/* Integer too big to fit in a C long, bail out */</span>
<span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="mi">-1</span><span class="p">;</span>
<span class="w"> </span><span class="n">total</span><span class="w"> </span><span class="o">+=</span><span class="w"> </span><span class="n">value</span><span class="p">;</span>
<span class="w"> </span><span class="p">}</span>
<span class="w"> </span><span class="k">else</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="n">Py_DECREF</span><span class="p">(</span><span class="n">item</span><span class="p">);</span><span class="w"> </span><span class="cm">/* Discard reference ownership */</span>
<span class="w"> </span><span class="p">}</span>
<span class="w"> </span><span class="p">}</span>