Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

object.h 41 KB

You have to be logged in to leave a comment. Sign In
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
  1. #ifndef Py_OBJECT_H
  2. #define Py_OBJECT_H
  3. #ifdef __cplusplus
  4. extern "C" {
  5. #endif
  6. /* Object and type object interface */
  7. /*
  8. Objects are structures allocated on the heap. Special rules apply to
  9. the use of objects to ensure they are properly garbage-collected.
  10. Objects are never allocated statically or on the stack; they must be
  11. accessed through special macros and functions only. (Type objects are
  12. exceptions to the first rule; the standard types are represented by
  13. statically initialized type objects, although work on type/class unification
  14. for Python 2.2 made it possible to have heap-allocated type objects too).
  15. An object has a 'reference count' that is increased or decreased when a
  16. pointer to the object is copied or deleted; when the reference count
  17. reaches zero there are no references to the object left and it can be
  18. removed from the heap.
  19. An object has a 'type' that determines what it represents and what kind
  20. of data it contains. An object's type is fixed when it is created.
  21. Types themselves are represented as objects; an object contains a
  22. pointer to the corresponding type object. The type itself has a type
  23. pointer pointing to the object representing the type 'type', which
  24. contains a pointer to itself!).
  25. Objects do not float around in memory; once allocated an object keeps
  26. the same size and address. Objects that must hold variable-size data
  27. can contain pointers to variable-size parts of the object. Not all
  28. objects of the same type have the same size; but the size cannot change
  29. after allocation. (These restrictions are made so a reference to an
  30. object can be simply a pointer -- moving an object would require
  31. updating all the pointers, and changing an object's size would require
  32. moving it if there was another object right next to it.)
  33. Objects are always accessed through pointers of the type 'PyObject *'.
  34. The type 'PyObject' is a structure that only contains the reference count
  35. and the type pointer. The actual memory allocated for an object
  36. contains other data that can only be accessed after casting the pointer
  37. to a pointer to a longer structure type. This longer type must start
  38. with the reference count and type fields; the macro PyObject_HEAD should be
  39. used for this (to accommodate for future changes). The implementation
  40. of a particular object type can cast the object pointer to the proper
  41. type and back.
  42. A standard interface exists for objects that contain an array of items
  43. whose size is determined when the object is allocated.
  44. */
  45. /* Py_DEBUG implies Py_TRACE_REFS. */
  46. #if defined(Py_DEBUG) && !defined(Py_TRACE_REFS)
  47. #define Py_TRACE_REFS
  48. #endif
  49. /* Py_TRACE_REFS implies Py_REF_DEBUG. */
  50. #if defined(Py_TRACE_REFS) && !defined(Py_REF_DEBUG)
  51. #define Py_REF_DEBUG
  52. #endif
  53. #if defined(Py_LIMITED_API) && defined(Py_REF_DEBUG)
  54. #error Py_LIMITED_API is incompatible with Py_DEBUG, Py_TRACE_REFS, and Py_REF_DEBUG
  55. #endif
  56. #ifdef Py_TRACE_REFS
  57. /* Define pointers to support a doubly-linked list of all live heap objects. */
  58. #define _PyObject_HEAD_EXTRA \
  59. struct _object *_ob_next; \
  60. struct _object *_ob_prev;
  61. #define _PyObject_EXTRA_INIT 0, 0,
  62. #else
  63. #define _PyObject_HEAD_EXTRA
  64. #define _PyObject_EXTRA_INIT
  65. #endif
  66. /* PyObject_HEAD defines the initial segment of every PyObject. */
  67. #define PyObject_HEAD PyObject ob_base;
  68. #define PyObject_HEAD_INIT(type) \
  69. { _PyObject_EXTRA_INIT \
  70. 1, type },
  71. #define PyVarObject_HEAD_INIT(type, size) \
  72. { PyObject_HEAD_INIT(type) size },
  73. /* PyObject_VAR_HEAD defines the initial segment of all variable-size
  74. * container objects. These end with a declaration of an array with 1
  75. * element, but enough space is malloc'ed so that the array actually
  76. * has room for ob_size elements. Note that ob_size is an element count,
  77. * not necessarily a byte count.
  78. */
  79. #define PyObject_VAR_HEAD PyVarObject ob_base;
  80. #define Py_INVALID_SIZE (Py_ssize_t)-1
  81. /* Nothing is actually declared to be a PyObject, but every pointer to
  82. * a Python object can be cast to a PyObject*. This is inheritance built
  83. * by hand. Similarly every pointer to a variable-size Python object can,
  84. * in addition, be cast to PyVarObject*.
  85. */
  86. typedef struct _object {
  87. _PyObject_HEAD_EXTRA
  88. Py_ssize_t ob_refcnt;
  89. struct _typeobject *ob_type;
  90. } PyObject;
  91. typedef struct {
  92. PyObject ob_base;
  93. Py_ssize_t ob_size; /* Number of items in variable part */
  94. } PyVarObject;
  95. #define Py_REFCNT(ob) (((PyObject*)(ob))->ob_refcnt)
  96. #define Py_TYPE(ob) (((PyObject*)(ob))->ob_type)
  97. #define Py_SIZE(ob) (((PyVarObject*)(ob))->ob_size)
  98. #ifndef Py_LIMITED_API
  99. /********************* String Literals ****************************************/
  100. /* This structure helps managing static strings. The basic usage goes like this:
  101. Instead of doing
  102. r = PyObject_CallMethod(o, "foo", "args", ...);
  103. do
  104. _Py_IDENTIFIER(foo);
  105. ...
  106. r = _PyObject_CallMethodId(o, &PyId_foo, "args", ...);
  107. PyId_foo is a static variable, either on block level or file level. On first
  108. usage, the string "foo" is interned, and the structures are linked. On interpreter
  109. shutdown, all strings are released (through _PyUnicode_ClearStaticStrings).
  110. Alternatively, _Py_static_string allows choosing the variable name.
  111. _PyUnicode_FromId returns a borrowed reference to the interned string.
  112. _PyObject_{Get,Set,Has}AttrId are __getattr__ versions using _Py_Identifier*.
  113. */
  114. typedef struct _Py_Identifier {
  115. struct _Py_Identifier *next;
  116. const char* string;
  117. PyObject *object;
  118. } _Py_Identifier;
  119. #define _Py_static_string_init(value) { .next = NULL, .string = value, .object = NULL }
  120. #define _Py_static_string(varname, value) static _Py_Identifier varname = _Py_static_string_init(value)
  121. #define _Py_IDENTIFIER(varname) _Py_static_string(PyId_##varname, #varname)
  122. #endif /* !Py_LIMITED_API */
  123. /*
  124. Type objects contain a string containing the type name (to help somewhat
  125. in debugging), the allocation parameters (see PyObject_New() and
  126. PyObject_NewVar()),
  127. and methods for accessing objects of the type. Methods are optional, a
  128. nil pointer meaning that particular kind of access is not available for
  129. this type. The Py_DECREF() macro uses the tp_dealloc method without
  130. checking for a nil pointer; it should always be implemented except if
  131. the implementation can guarantee that the reference count will never
  132. reach zero (e.g., for statically allocated type objects).
  133. NB: the methods for certain type groups are now contained in separate
  134. method blocks.
  135. */
  136. typedef PyObject * (*unaryfunc)(PyObject *);
  137. typedef PyObject * (*binaryfunc)(PyObject *, PyObject *);
  138. typedef PyObject * (*ternaryfunc)(PyObject *, PyObject *, PyObject *);
  139. typedef int (*inquiry)(PyObject *);
  140. typedef Py_ssize_t (*lenfunc)(PyObject *);
  141. typedef PyObject *(*ssizeargfunc)(PyObject *, Py_ssize_t);
  142. typedef PyObject *(*ssizessizeargfunc)(PyObject *, Py_ssize_t, Py_ssize_t);
  143. typedef int(*ssizeobjargproc)(PyObject *, Py_ssize_t, PyObject *);
  144. typedef int(*ssizessizeobjargproc)(PyObject *, Py_ssize_t, Py_ssize_t, PyObject *);
  145. typedef int(*objobjargproc)(PyObject *, PyObject *, PyObject *);
  146. #ifndef Py_LIMITED_API
  147. /* buffer interface */
  148. typedef struct bufferinfo {
  149. void *buf;
  150. PyObject *obj; /* owned reference */
  151. Py_ssize_t len;
  152. Py_ssize_t itemsize; /* This is Py_ssize_t so it can be
  153. pointed to by strides in simple case.*/
  154. int readonly;
  155. int ndim;
  156. char *format;
  157. Py_ssize_t *shape;
  158. Py_ssize_t *strides;
  159. Py_ssize_t *suboffsets;
  160. void *internal;
  161. } Py_buffer;
  162. typedef int (*getbufferproc)(PyObject *, Py_buffer *, int);
  163. typedef void (*releasebufferproc)(PyObject *, Py_buffer *);
  164. /* Maximum number of dimensions */
  165. #define PyBUF_MAX_NDIM 64
  166. /* Flags for getting buffers */
  167. #define PyBUF_SIMPLE 0
  168. #define PyBUF_WRITABLE 0x0001
  169. /* we used to include an E, backwards compatible alias */
  170. #define PyBUF_WRITEABLE PyBUF_WRITABLE
  171. #define PyBUF_FORMAT 0x0004
  172. #define PyBUF_ND 0x0008
  173. #define PyBUF_STRIDES (0x0010 | PyBUF_ND)
  174. #define PyBUF_C_CONTIGUOUS (0x0020 | PyBUF_STRIDES)
  175. #define PyBUF_F_CONTIGUOUS (0x0040 | PyBUF_STRIDES)
  176. #define PyBUF_ANY_CONTIGUOUS (0x0080 | PyBUF_STRIDES)
  177. #define PyBUF_INDIRECT (0x0100 | PyBUF_STRIDES)
  178. #define PyBUF_CONTIG (PyBUF_ND | PyBUF_WRITABLE)
  179. #define PyBUF_CONTIG_RO (PyBUF_ND)
  180. #define PyBUF_STRIDED (PyBUF_STRIDES | PyBUF_WRITABLE)
  181. #define PyBUF_STRIDED_RO (PyBUF_STRIDES)
  182. #define PyBUF_RECORDS (PyBUF_STRIDES | PyBUF_WRITABLE | PyBUF_FORMAT)
  183. #define PyBUF_RECORDS_RO (PyBUF_STRIDES | PyBUF_FORMAT)
  184. #define PyBUF_FULL (PyBUF_INDIRECT | PyBUF_WRITABLE | PyBUF_FORMAT)
  185. #define PyBUF_FULL_RO (PyBUF_INDIRECT | PyBUF_FORMAT)
  186. #define PyBUF_READ 0x100
  187. #define PyBUF_WRITE 0x200
  188. /* End buffer interface */
  189. #endif /* Py_LIMITED_API */
  190. typedef int (*objobjproc)(PyObject *, PyObject *);
  191. typedef int (*visitproc)(PyObject *, void *);
  192. typedef int (*traverseproc)(PyObject *, visitproc, void *);
  193. #ifndef Py_LIMITED_API
  194. typedef struct {
  195. /* Number implementations must check *both*
  196. arguments for proper type and implement the necessary conversions
  197. in the slot functions themselves. */
  198. binaryfunc nb_add;
  199. binaryfunc nb_subtract;
  200. binaryfunc nb_multiply;
  201. binaryfunc nb_remainder;
  202. binaryfunc nb_divmod;
  203. ternaryfunc nb_power;
  204. unaryfunc nb_negative;
  205. unaryfunc nb_positive;
  206. unaryfunc nb_absolute;
  207. inquiry nb_bool;
  208. unaryfunc nb_invert;
  209. binaryfunc nb_lshift;
  210. binaryfunc nb_rshift;
  211. binaryfunc nb_and;
  212. binaryfunc nb_xor;
  213. binaryfunc nb_or;
  214. unaryfunc nb_int;
  215. void *nb_reserved; /* the slot formerly known as nb_long */
  216. unaryfunc nb_float;
  217. binaryfunc nb_inplace_add;
  218. binaryfunc nb_inplace_subtract;
  219. binaryfunc nb_inplace_multiply;
  220. binaryfunc nb_inplace_remainder;
  221. ternaryfunc nb_inplace_power;
  222. binaryfunc nb_inplace_lshift;
  223. binaryfunc nb_inplace_rshift;
  224. binaryfunc nb_inplace_and;
  225. binaryfunc nb_inplace_xor;
  226. binaryfunc nb_inplace_or;
  227. binaryfunc nb_floor_divide;
  228. binaryfunc nb_true_divide;
  229. binaryfunc nb_inplace_floor_divide;
  230. binaryfunc nb_inplace_true_divide;
  231. unaryfunc nb_index;
  232. binaryfunc nb_matrix_multiply;
  233. binaryfunc nb_inplace_matrix_multiply;
  234. } PyNumberMethods;
  235. typedef struct {
  236. lenfunc sq_length;
  237. binaryfunc sq_concat;
  238. ssizeargfunc sq_repeat;
  239. ssizeargfunc sq_item;
  240. void *was_sq_slice;
  241. ssizeobjargproc sq_ass_item;
  242. void *was_sq_ass_slice;
  243. objobjproc sq_contains;
  244. binaryfunc sq_inplace_concat;
  245. ssizeargfunc sq_inplace_repeat;
  246. } PySequenceMethods;
  247. typedef struct {
  248. lenfunc mp_length;
  249. binaryfunc mp_subscript;
  250. objobjargproc mp_ass_subscript;
  251. } PyMappingMethods;
  252. typedef struct {
  253. unaryfunc am_await;
  254. unaryfunc am_aiter;
  255. unaryfunc am_anext;
  256. } PyAsyncMethods;
  257. typedef struct {
  258. getbufferproc bf_getbuffer;
  259. releasebufferproc bf_releasebuffer;
  260. } PyBufferProcs;
  261. #endif /* Py_LIMITED_API */
  262. typedef void (*freefunc)(void *);
  263. typedef void (*destructor)(PyObject *);
  264. #ifndef Py_LIMITED_API
  265. /* We can't provide a full compile-time check that limited-API
  266. users won't implement tp_print. However, not defining printfunc
  267. and making tp_print of a different function pointer type
  268. should at least cause a warning in most cases. */
  269. typedef int (*printfunc)(PyObject *, FILE *, int);
  270. #endif
  271. typedef PyObject *(*getattrfunc)(PyObject *, char *);
  272. typedef PyObject *(*getattrofunc)(PyObject *, PyObject *);
  273. typedef int (*setattrfunc)(PyObject *, char *, PyObject *);
  274. typedef int (*setattrofunc)(PyObject *, PyObject *, PyObject *);
  275. typedef PyObject *(*reprfunc)(PyObject *);
  276. typedef Py_hash_t (*hashfunc)(PyObject *);
  277. typedef PyObject *(*richcmpfunc) (PyObject *, PyObject *, int);
  278. typedef PyObject *(*getiterfunc) (PyObject *);
  279. typedef PyObject *(*iternextfunc) (PyObject *);
  280. typedef PyObject *(*descrgetfunc) (PyObject *, PyObject *, PyObject *);
  281. typedef int (*descrsetfunc) (PyObject *, PyObject *, PyObject *);
  282. typedef int (*initproc)(PyObject *, PyObject *, PyObject *);
  283. typedef PyObject *(*newfunc)(struct _typeobject *, PyObject *, PyObject *);
  284. typedef PyObject *(*allocfunc)(struct _typeobject *, Py_ssize_t);
  285. #ifdef Py_LIMITED_API
  286. typedef struct _typeobject PyTypeObject; /* opaque */
  287. #else
  288. typedef struct _typeobject {
  289. PyObject_VAR_HEAD
  290. const char *tp_name; /* For printing, in format "<module>.<name>" */
  291. Py_ssize_t tp_basicsize, tp_itemsize; /* For allocation */
  292. /* Methods to implement standard operations */
  293. destructor tp_dealloc;
  294. printfunc tp_print;
  295. getattrfunc tp_getattr;
  296. setattrfunc tp_setattr;
  297. PyAsyncMethods *tp_as_async; /* formerly known as tp_compare (Python 2)
  298. or tp_reserved (Python 3) */
  299. reprfunc tp_repr;
  300. /* Method suites for standard classes */
  301. PyNumberMethods *tp_as_number;
  302. PySequenceMethods *tp_as_sequence;
  303. PyMappingMethods *tp_as_mapping;
  304. /* More standard operations (here for binary compatibility) */
  305. hashfunc tp_hash;
  306. ternaryfunc tp_call;
  307. reprfunc tp_str;
  308. getattrofunc tp_getattro;
  309. setattrofunc tp_setattro;
  310. /* Functions to access object as input/output buffer */
  311. PyBufferProcs *tp_as_buffer;
  312. /* Flags to define presence of optional/expanded features */
  313. unsigned long tp_flags;
  314. const char *tp_doc; /* Documentation string */
  315. /* Assigned meaning in release 2.0 */
  316. /* call function for all accessible objects */
  317. traverseproc tp_traverse;
  318. /* delete references to contained objects */
  319. inquiry tp_clear;
  320. /* Assigned meaning in release 2.1 */
  321. /* rich comparisons */
  322. richcmpfunc tp_richcompare;
  323. /* weak reference enabler */
  324. Py_ssize_t tp_weaklistoffset;
  325. /* Iterators */
  326. getiterfunc tp_iter;
  327. iternextfunc tp_iternext;
  328. /* Attribute descriptor and subclassing stuff */
  329. struct PyMethodDef *tp_methods;
  330. struct PyMemberDef *tp_members;
  331. struct PyGetSetDef *tp_getset;
  332. struct _typeobject *tp_base;
  333. PyObject *tp_dict;
  334. descrgetfunc tp_descr_get;
  335. descrsetfunc tp_descr_set;
  336. Py_ssize_t tp_dictoffset;
  337. initproc tp_init;
  338. allocfunc tp_alloc;
  339. newfunc tp_new;
  340. freefunc tp_free; /* Low-level free-memory routine */
  341. inquiry tp_is_gc; /* For PyObject_IS_GC */
  342. PyObject *tp_bases;
  343. PyObject *tp_mro; /* method resolution order */
  344. PyObject *tp_cache;
  345. PyObject *tp_subclasses;
  346. PyObject *tp_weaklist;
  347. destructor tp_del;
  348. /* Type attribute cache version tag. Added in version 2.6 */
  349. unsigned int tp_version_tag;
  350. destructor tp_finalize;
  351. #ifdef COUNT_ALLOCS
  352. /* these must be last and never explicitly initialized */
  353. Py_ssize_t tp_allocs;
  354. Py_ssize_t tp_frees;
  355. Py_ssize_t tp_maxalloc;
  356. struct _typeobject *tp_prev;
  357. struct _typeobject *tp_next;
  358. #endif
  359. } PyTypeObject;
  360. #endif
  361. typedef struct{
  362. int slot; /* slot id, see below */
  363. void *pfunc; /* function pointer */
  364. } PyType_Slot;
  365. typedef struct{
  366. const char* name;
  367. int basicsize;
  368. int itemsize;
  369. unsigned int flags;
  370. PyType_Slot *slots; /* terminated by slot==0. */
  371. } PyType_Spec;
  372. PyAPI_FUNC(PyObject*) PyType_FromSpec(PyType_Spec*);
  373. #if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03030000
  374. PyAPI_FUNC(PyObject*) PyType_FromSpecWithBases(PyType_Spec*, PyObject*);
  375. #endif
  376. #if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03040000
  377. PyAPI_FUNC(void*) PyType_GetSlot(PyTypeObject*, int);
  378. #endif
  379. #ifndef Py_LIMITED_API
  380. /* The *real* layout of a type object when allocated on the heap */
  381. typedef struct _heaptypeobject {
  382. /* Note: there's a dependency on the order of these members
  383. in slotptr() in typeobject.c . */
  384. PyTypeObject ht_type;
  385. PyAsyncMethods as_async;
  386. PyNumberMethods as_number;
  387. PyMappingMethods as_mapping;
  388. PySequenceMethods as_sequence; /* as_sequence comes after as_mapping,
  389. so that the mapping wins when both
  390. the mapping and the sequence define
  391. a given operator (e.g. __getitem__).
  392. see add_operators() in typeobject.c . */
  393. PyBufferProcs as_buffer;
  394. PyObject *ht_name, *ht_slots, *ht_qualname;
  395. struct _dictkeysobject *ht_cached_keys;
  396. /* here are optional user slots, followed by the members. */
  397. } PyHeapTypeObject;
  398. /* access macro to the members which are floating "behind" the object */
  399. #define PyHeapType_GET_MEMBERS(etype) \
  400. ((PyMemberDef *)(((char *)etype) + Py_TYPE(etype)->tp_basicsize))
  401. #endif
  402. /* Generic type check */
  403. PyAPI_FUNC(int) PyType_IsSubtype(PyTypeObject *, PyTypeObject *);
  404. #define PyObject_TypeCheck(ob, tp) \
  405. (Py_TYPE(ob) == (tp) || PyType_IsSubtype(Py_TYPE(ob), (tp)))
  406. PyAPI_DATA(PyTypeObject) PyType_Type; /* built-in 'type' */
  407. PyAPI_DATA(PyTypeObject) PyBaseObject_Type; /* built-in 'object' */
  408. PyAPI_DATA(PyTypeObject) PySuper_Type; /* built-in 'super' */
  409. PyAPI_FUNC(unsigned long) PyType_GetFlags(PyTypeObject*);
  410. #define PyType_Check(op) \
  411. PyType_FastSubclass(Py_TYPE(op), Py_TPFLAGS_TYPE_SUBCLASS)
  412. #define PyType_CheckExact(op) (Py_TYPE(op) == &PyType_Type)
  413. PyAPI_FUNC(int) PyType_Ready(PyTypeObject *);
  414. PyAPI_FUNC(PyObject *) PyType_GenericAlloc(PyTypeObject *, Py_ssize_t);
  415. PyAPI_FUNC(PyObject *) PyType_GenericNew(PyTypeObject *,
  416. PyObject *, PyObject *);
  417. #ifndef Py_LIMITED_API
  418. PyAPI_FUNC(const char *) _PyType_Name(PyTypeObject *);
  419. PyAPI_FUNC(PyObject *) _PyType_Lookup(PyTypeObject *, PyObject *);
  420. PyAPI_FUNC(PyObject *) _PyType_LookupId(PyTypeObject *, _Py_Identifier *);
  421. PyAPI_FUNC(PyObject *) _PyObject_LookupSpecial(PyObject *, _Py_Identifier *);
  422. PyAPI_FUNC(PyTypeObject *) _PyType_CalculateMetaclass(PyTypeObject *, PyObject *);
  423. #endif
  424. PyAPI_FUNC(unsigned int) PyType_ClearCache(void);
  425. PyAPI_FUNC(void) PyType_Modified(PyTypeObject *);
  426. #ifndef Py_LIMITED_API
  427. PyAPI_FUNC(PyObject *) _PyType_GetDocFromInternalDoc(const char *, const char *);
  428. PyAPI_FUNC(PyObject *) _PyType_GetTextSignatureFromInternalDoc(const char *, const char *);
  429. #endif
  430. /* Generic operations on objects */
  431. #ifndef Py_LIMITED_API
  432. struct _Py_Identifier;
  433. PyAPI_FUNC(int) PyObject_Print(PyObject *, FILE *, int);
  434. PyAPI_FUNC(void) _Py_BreakPoint(void);
  435. PyAPI_FUNC(void) _PyObject_Dump(PyObject *);
  436. PyAPI_FUNC(int) _PyObject_IsFreed(PyObject *);
  437. #endif
  438. PyAPI_FUNC(PyObject *) PyObject_Repr(PyObject *);
  439. PyAPI_FUNC(PyObject *) PyObject_Str(PyObject *);
  440. PyAPI_FUNC(PyObject *) PyObject_ASCII(PyObject *);
  441. PyAPI_FUNC(PyObject *) PyObject_Bytes(PyObject *);
  442. PyAPI_FUNC(PyObject *) PyObject_RichCompare(PyObject *, PyObject *, int);
  443. PyAPI_FUNC(int) PyObject_RichCompareBool(PyObject *, PyObject *, int);
  444. PyAPI_FUNC(PyObject *) PyObject_GetAttrString(PyObject *, const char *);
  445. PyAPI_FUNC(int) PyObject_SetAttrString(PyObject *, const char *, PyObject *);
  446. PyAPI_FUNC(int) PyObject_HasAttrString(PyObject *, const char *);
  447. PyAPI_FUNC(PyObject *) PyObject_GetAttr(PyObject *, PyObject *);
  448. PyAPI_FUNC(int) PyObject_SetAttr(PyObject *, PyObject *, PyObject *);
  449. PyAPI_FUNC(int) PyObject_HasAttr(PyObject *, PyObject *);
  450. #ifndef Py_LIMITED_API
  451. PyAPI_FUNC(int) _PyObject_IsAbstract(PyObject *);
  452. PyAPI_FUNC(PyObject *) _PyObject_GetAttrId(PyObject *, struct _Py_Identifier *);
  453. PyAPI_FUNC(int) _PyObject_SetAttrId(PyObject *, struct _Py_Identifier *, PyObject *);
  454. PyAPI_FUNC(int) _PyObject_HasAttrId(PyObject *, struct _Py_Identifier *);
  455. /* Replacements of PyObject_GetAttr() and _PyObject_GetAttrId() which
  456. don't raise AttributeError.
  457. Return 1 and set *result != NULL if an attribute is found.
  458. Return 0 and set *result == NULL if an attribute is not found;
  459. an AttributeError is silenced.
  460. Return -1 and set *result == NULL if an error other than AttributeError
  461. is raised.
  462. */
  463. PyAPI_FUNC(int) _PyObject_LookupAttr(PyObject *, PyObject *, PyObject **);
  464. PyAPI_FUNC(int) _PyObject_LookupAttrId(PyObject *, struct _Py_Identifier *, PyObject **);
  465. PyAPI_FUNC(PyObject **) _PyObject_GetDictPtr(PyObject *);
  466. #endif
  467. PyAPI_FUNC(PyObject *) PyObject_SelfIter(PyObject *);
  468. #ifndef Py_LIMITED_API
  469. PyAPI_FUNC(PyObject *) _PyObject_NextNotImplemented(PyObject *);
  470. #endif
  471. PyAPI_FUNC(PyObject *) PyObject_GenericGetAttr(PyObject *, PyObject *);
  472. PyAPI_FUNC(int) PyObject_GenericSetAttr(PyObject *,
  473. PyObject *, PyObject *);
  474. #if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03030000
  475. PyAPI_FUNC(int) PyObject_GenericSetDict(PyObject *, PyObject *, void *);
  476. #endif
  477. PyAPI_FUNC(Py_hash_t) PyObject_Hash(PyObject *);
  478. PyAPI_FUNC(Py_hash_t) PyObject_HashNotImplemented(PyObject *);
  479. PyAPI_FUNC(int) PyObject_IsTrue(PyObject *);
  480. PyAPI_FUNC(int) PyObject_Not(PyObject *);
  481. PyAPI_FUNC(int) PyCallable_Check(PyObject *);
  482. PyAPI_FUNC(void) PyObject_ClearWeakRefs(PyObject *);
  483. #ifndef Py_LIMITED_API
  484. PyAPI_FUNC(void) PyObject_CallFinalizer(PyObject *);
  485. PyAPI_FUNC(int) PyObject_CallFinalizerFromDealloc(PyObject *);
  486. #endif
  487. #ifndef Py_LIMITED_API
  488. /* Same as PyObject_Generic{Get,Set}Attr, but passing the attributes
  489. dict as the last parameter. */
  490. PyAPI_FUNC(PyObject *)
  491. _PyObject_GenericGetAttrWithDict(PyObject *, PyObject *, PyObject *, int);
  492. PyAPI_FUNC(int)
  493. _PyObject_GenericSetAttrWithDict(PyObject *, PyObject *,
  494. PyObject *, PyObject *);
  495. #endif /* !Py_LIMITED_API */
  496. /* Helper to look up a builtin object */
  497. #ifndef Py_LIMITED_API
  498. PyAPI_FUNC(PyObject *)
  499. _PyObject_GetBuiltin(const char *name);
  500. #endif
  501. /* PyObject_Dir(obj) acts like Python builtins.dir(obj), returning a
  502. list of strings. PyObject_Dir(NULL) is like builtins.dir(),
  503. returning the names of the current locals. In this case, if there are
  504. no current locals, NULL is returned, and PyErr_Occurred() is false.
  505. */
  506. PyAPI_FUNC(PyObject *) PyObject_Dir(PyObject *);
  507. /* Helpers for printing recursive container types */
  508. PyAPI_FUNC(int) Py_ReprEnter(PyObject *);
  509. PyAPI_FUNC(void) Py_ReprLeave(PyObject *);
  510. /* Flag bits for printing: */
  511. #define Py_PRINT_RAW 1 /* No string quotes etc. */
  512. /*
  513. `Type flags (tp_flags)
  514. These flags are used to extend the type structure in a backwards-compatible
  515. fashion. Extensions can use the flags to indicate (and test) when a given
  516. type structure contains a new feature. The Python core will use these when
  517. introducing new functionality between major revisions (to avoid mid-version
  518. changes in the PYTHON_API_VERSION).
  519. Arbitration of the flag bit positions will need to be coordinated among
  520. all extension writers who publicly release their extensions (this will
  521. be fewer than you might expect!)..
  522. Most flags were removed as of Python 3.0 to make room for new flags. (Some
  523. flags are not for backwards compatibility but to indicate the presence of an
  524. optional feature; these flags remain of course.)
  525. Type definitions should use Py_TPFLAGS_DEFAULT for their tp_flags value.
  526. Code can use PyType_HasFeature(type_ob, flag_value) to test whether the
  527. given type object has a specified feature.
  528. */
  529. /* Set if the type object is dynamically allocated */
  530. #define Py_TPFLAGS_HEAPTYPE (1UL << 9)
  531. /* Set if the type allows subclassing */
  532. #define Py_TPFLAGS_BASETYPE (1UL << 10)
  533. /* Set if the type is 'ready' -- fully initialized */
  534. #define Py_TPFLAGS_READY (1UL << 12)
  535. /* Set while the type is being 'readied', to prevent recursive ready calls */
  536. #define Py_TPFLAGS_READYING (1UL << 13)
  537. /* Objects support garbage collection (see objimp.h) */
  538. #define Py_TPFLAGS_HAVE_GC (1UL << 14)
  539. /* These two bits are preserved for Stackless Python, next after this is 17 */
  540. #ifdef STACKLESS
  541. #define Py_TPFLAGS_HAVE_STACKLESS_EXTENSION (3UL << 15)
  542. #else
  543. #define Py_TPFLAGS_HAVE_STACKLESS_EXTENSION 0
  544. #endif
  545. /* Objects support type attribute cache */
  546. #define Py_TPFLAGS_HAVE_VERSION_TAG (1UL << 18)
  547. #define Py_TPFLAGS_VALID_VERSION_TAG (1UL << 19)
  548. /* Type is abstract and cannot be instantiated */
  549. #define Py_TPFLAGS_IS_ABSTRACT (1UL << 20)
  550. /* These flags are used to determine if a type is a subclass. */
  551. #define Py_TPFLAGS_LONG_SUBCLASS (1UL << 24)
  552. #define Py_TPFLAGS_LIST_SUBCLASS (1UL << 25)
  553. #define Py_TPFLAGS_TUPLE_SUBCLASS (1UL << 26)
  554. #define Py_TPFLAGS_BYTES_SUBCLASS (1UL << 27)
  555. #define Py_TPFLAGS_UNICODE_SUBCLASS (1UL << 28)
  556. #define Py_TPFLAGS_DICT_SUBCLASS (1UL << 29)
  557. #define Py_TPFLAGS_BASE_EXC_SUBCLASS (1UL << 30)
  558. #define Py_TPFLAGS_TYPE_SUBCLASS (1UL << 31)
  559. #define Py_TPFLAGS_DEFAULT ( \
  560. Py_TPFLAGS_HAVE_STACKLESS_EXTENSION | \
  561. Py_TPFLAGS_HAVE_VERSION_TAG | \
  562. 0)
  563. /* NOTE: The following flags reuse lower bits (removed as part of the
  564. * Python 3.0 transition). */
  565. /* Type structure has tp_finalize member (3.4) */
  566. #define Py_TPFLAGS_HAVE_FINALIZE (1UL << 0)
  567. #ifdef Py_LIMITED_API
  568. #define PyType_HasFeature(t,f) ((PyType_GetFlags(t) & (f)) != 0)
  569. #else
  570. #define PyType_HasFeature(t,f) (((t)->tp_flags & (f)) != 0)
  571. #endif
  572. #define PyType_FastSubclass(t,f) PyType_HasFeature(t,f)
  573. /*
  574. The macros Py_INCREF(op) and Py_DECREF(op) are used to increment or decrement
  575. reference counts. Py_DECREF calls the object's deallocator function when
  576. the refcount falls to 0; for
  577. objects that don't contain references to other objects or heap memory
  578. this can be the standard function free(). Both macros can be used
  579. wherever a void expression is allowed. The argument must not be a
  580. NULL pointer. If it may be NULL, use Py_XINCREF/Py_XDECREF instead.
  581. The macro _Py_NewReference(op) initialize reference counts to 1, and
  582. in special builds (Py_REF_DEBUG, Py_TRACE_REFS) performs additional
  583. bookkeeping appropriate to the special build.
  584. We assume that the reference count field can never overflow; this can
  585. be proven when the size of the field is the same as the pointer size, so
  586. we ignore the possibility. Provided a C int is at least 32 bits (which
  587. is implicitly assumed in many parts of this code), that's enough for
  588. about 2**31 references to an object.
  589. XXX The following became out of date in Python 2.2, but I'm not sure
  590. XXX what the full truth is now. Certainly, heap-allocated type objects
  591. XXX can and should be deallocated.
  592. Type objects should never be deallocated; the type pointer in an object
  593. is not considered to be a reference to the type object, to save
  594. complications in the deallocation function. (This is actually a
  595. decision that's up to the implementer of each new type so if you want,
  596. you can count such references to the type object.)
  597. */
  598. /* First define a pile of simple helper macros, one set per special
  599. * build symbol. These either expand to the obvious things, or to
  600. * nothing at all when the special mode isn't in effect. The main
  601. * macros can later be defined just once then, yet expand to different
  602. * things depending on which special build options are and aren't in effect.
  603. * Trust me <wink>: while painful, this is 20x easier to understand than,
  604. * e.g, defining _Py_NewReference five different times in a maze of nested
  605. * #ifdefs (we used to do that -- it was impenetrable).
  606. */
  607. #ifdef Py_REF_DEBUG
  608. PyAPI_DATA(Py_ssize_t) _Py_RefTotal;
  609. PyAPI_FUNC(void) _Py_NegativeRefcount(const char *fname,
  610. int lineno, PyObject *op);
  611. PyAPI_FUNC(Py_ssize_t) _Py_GetRefTotal(void);
  612. #define _Py_INC_REFTOTAL _Py_RefTotal++
  613. #define _Py_DEC_REFTOTAL _Py_RefTotal--
  614. #define _Py_REF_DEBUG_COMMA ,
  615. #define _Py_CHECK_REFCNT(OP) \
  616. { if (((PyObject*)OP)->ob_refcnt < 0) \
  617. _Py_NegativeRefcount(__FILE__, __LINE__, \
  618. (PyObject *)(OP)); \
  619. }
  620. /* Py_REF_DEBUG also controls the display of refcounts and memory block
  621. * allocations at the interactive prompt and at interpreter shutdown
  622. */
  623. PyAPI_FUNC(void) _PyDebug_PrintTotalRefs(void);
  624. #else
  625. #define _Py_INC_REFTOTAL
  626. #define _Py_DEC_REFTOTAL
  627. #define _Py_REF_DEBUG_COMMA
  628. #define _Py_CHECK_REFCNT(OP) /* a semicolon */;
  629. #endif /* Py_REF_DEBUG */
  630. #ifdef COUNT_ALLOCS
  631. PyAPI_FUNC(void) inc_count(PyTypeObject *);
  632. PyAPI_FUNC(void) dec_count(PyTypeObject *);
  633. #define _Py_INC_TPALLOCS(OP) inc_count(Py_TYPE(OP))
  634. #define _Py_INC_TPFREES(OP) dec_count(Py_TYPE(OP))
  635. #define _Py_DEC_TPFREES(OP) Py_TYPE(OP)->tp_frees--
  636. #define _Py_COUNT_ALLOCS_COMMA ,
  637. #else
  638. #define _Py_INC_TPALLOCS(OP)
  639. #define _Py_INC_TPFREES(OP)
  640. #define _Py_DEC_TPFREES(OP)
  641. #define _Py_COUNT_ALLOCS_COMMA
  642. #endif /* COUNT_ALLOCS */
  643. #ifdef Py_TRACE_REFS
  644. /* Py_TRACE_REFS is such major surgery that we call external routines. */
  645. PyAPI_FUNC(void) _Py_NewReference(PyObject *);
  646. PyAPI_FUNC(void) _Py_ForgetReference(PyObject *);
  647. PyAPI_FUNC(void) _Py_Dealloc(PyObject *);
  648. PyAPI_FUNC(void) _Py_PrintReferences(FILE *);
  649. PyAPI_FUNC(void) _Py_PrintReferenceAddresses(FILE *);
  650. PyAPI_FUNC(void) _Py_AddToAllObjects(PyObject *, int force);
  651. #else
  652. /* Without Py_TRACE_REFS, there's little enough to do that we expand code
  653. * inline.
  654. */
  655. #define _Py_NewReference(op) ( \
  656. _Py_INC_TPALLOCS(op) _Py_COUNT_ALLOCS_COMMA \
  657. _Py_INC_REFTOTAL _Py_REF_DEBUG_COMMA \
  658. Py_REFCNT(op) = 1)
  659. #define _Py_ForgetReference(op) _Py_INC_TPFREES(op)
  660. #ifdef Py_LIMITED_API
  661. PyAPI_FUNC(void) _Py_Dealloc(PyObject *);
  662. #else
  663. #define _Py_Dealloc(op) ( \
  664. _Py_INC_TPFREES(op) _Py_COUNT_ALLOCS_COMMA \
  665. (*Py_TYPE(op)->tp_dealloc)((PyObject *)(op)))
  666. #endif
  667. #endif /* !Py_TRACE_REFS */
  668. #define Py_INCREF(op) ( \
  669. _Py_INC_REFTOTAL _Py_REF_DEBUG_COMMA \
  670. ((PyObject *)(op))->ob_refcnt++)
  671. #define Py_DECREF(op) \
  672. do { \
  673. PyObject *_py_decref_tmp = (PyObject *)(op); \
  674. if (_Py_DEC_REFTOTAL _Py_REF_DEBUG_COMMA \
  675. --(_py_decref_tmp)->ob_refcnt != 0) \
  676. _Py_CHECK_REFCNT(_py_decref_tmp) \
  677. else \
  678. _Py_Dealloc(_py_decref_tmp); \
  679. } while (0)
  680. /* Safely decref `op` and set `op` to NULL, especially useful in tp_clear
  681. * and tp_dealloc implementations.
  682. *
  683. * Note that "the obvious" code can be deadly:
  684. *
  685. * Py_XDECREF(op);
  686. * op = NULL;
  687. *
  688. * Typically, `op` is something like self->containee, and `self` is done
  689. * using its `containee` member. In the code sequence above, suppose
  690. * `containee` is non-NULL with a refcount of 1. Its refcount falls to
  691. * 0 on the first line, which can trigger an arbitrary amount of code,
  692. * possibly including finalizers (like __del__ methods or weakref callbacks)
  693. * coded in Python, which in turn can release the GIL and allow other threads
  694. * to run, etc. Such code may even invoke methods of `self` again, or cause
  695. * cyclic gc to trigger, but-- oops! --self->containee still points to the
  696. * object being torn down, and it may be in an insane state while being torn
  697. * down. This has in fact been a rich historic source of miserable (rare &
  698. * hard-to-diagnose) segfaulting (and other) bugs.
  699. *
  700. * The safe way is:
  701. *
  702. * Py_CLEAR(op);
  703. *
  704. * That arranges to set `op` to NULL _before_ decref'ing, so that any code
  705. * triggered as a side-effect of `op` getting torn down no longer believes
  706. * `op` points to a valid object.
  707. *
  708. * There are cases where it's safe to use the naive code, but they're brittle.
  709. * For example, if `op` points to a Python integer, you know that destroying
  710. * one of those can't cause problems -- but in part that relies on that
  711. * Python integers aren't currently weakly referencable. Best practice is
  712. * to use Py_CLEAR() even if you can't think of a reason for why you need to.
  713. */
  714. #define Py_CLEAR(op) \
  715. do { \
  716. PyObject *_py_tmp = (PyObject *)(op); \
  717. if (_py_tmp != NULL) { \
  718. (op) = NULL; \
  719. Py_DECREF(_py_tmp); \
  720. } \
  721. } while (0)
  722. /* Macros to use in case the object pointer may be NULL: */
  723. #define Py_XINCREF(op) \
  724. do { \
  725. PyObject *_py_xincref_tmp = (PyObject *)(op); \
  726. if (_py_xincref_tmp != NULL) \
  727. Py_INCREF(_py_xincref_tmp); \
  728. } while (0)
  729. #define Py_XDECREF(op) \
  730. do { \
  731. PyObject *_py_xdecref_tmp = (PyObject *)(op); \
  732. if (_py_xdecref_tmp != NULL) \
  733. Py_DECREF(_py_xdecref_tmp); \
  734. } while (0)
  735. #ifndef Py_LIMITED_API
  736. /* Safely decref `op` and set `op` to `op2`.
  737. *
  738. * As in case of Py_CLEAR "the obvious" code can be deadly:
  739. *
  740. * Py_DECREF(op);
  741. * op = op2;
  742. *
  743. * The safe way is:
  744. *
  745. * Py_SETREF(op, op2);
  746. *
  747. * That arranges to set `op` to `op2` _before_ decref'ing, so that any code
  748. * triggered as a side-effect of `op` getting torn down no longer believes
  749. * `op` points to a valid object.
  750. *
  751. * Py_XSETREF is a variant of Py_SETREF that uses Py_XDECREF instead of
  752. * Py_DECREF.
  753. */
  754. #define Py_SETREF(op, op2) \
  755. do { \
  756. PyObject *_py_tmp = (PyObject *)(op); \
  757. (op) = (op2); \
  758. Py_DECREF(_py_tmp); \
  759. } while (0)
  760. #define Py_XSETREF(op, op2) \
  761. do { \
  762. PyObject *_py_tmp = (PyObject *)(op); \
  763. (op) = (op2); \
  764. Py_XDECREF(_py_tmp); \
  765. } while (0)
  766. #endif /* ifndef Py_LIMITED_API */
  767. /*
  768. These are provided as conveniences to Python runtime embedders, so that
  769. they can have object code that is not dependent on Python compilation flags.
  770. */
  771. PyAPI_FUNC(void) Py_IncRef(PyObject *);
  772. PyAPI_FUNC(void) Py_DecRef(PyObject *);
  773. #ifndef Py_LIMITED_API
  774. PyAPI_DATA(PyTypeObject) _PyNone_Type;
  775. PyAPI_DATA(PyTypeObject) _PyNotImplemented_Type;
  776. #endif /* !Py_LIMITED_API */
  777. /*
  778. _Py_NoneStruct is an object of undefined type which can be used in contexts
  779. where NULL (nil) is not suitable (since NULL often means 'error').
  780. Don't forget to apply Py_INCREF() when returning this value!!!
  781. */
  782. PyAPI_DATA(PyObject) _Py_NoneStruct; /* Don't use this directly */
  783. #define Py_None (&_Py_NoneStruct)
  784. /* Macro for returning Py_None from a function */
  785. #define Py_RETURN_NONE return Py_INCREF(Py_None), Py_None
  786. /*
  787. Py_NotImplemented is a singleton used to signal that an operation is
  788. not implemented for a given type combination.
  789. */
  790. PyAPI_DATA(PyObject) _Py_NotImplementedStruct; /* Don't use this directly */
  791. #define Py_NotImplemented (&_Py_NotImplementedStruct)
  792. /* Macro for returning Py_NotImplemented from a function */
  793. #define Py_RETURN_NOTIMPLEMENTED \
  794. return Py_INCREF(Py_NotImplemented), Py_NotImplemented
  795. /* Rich comparison opcodes */
  796. #define Py_LT 0
  797. #define Py_LE 1
  798. #define Py_EQ 2
  799. #define Py_NE 3
  800. #define Py_GT 4
  801. #define Py_GE 5
  802. /*
  803. * Macro for implementing rich comparisons
  804. *
  805. * Needs to be a macro because any C-comparable type can be used.
  806. */
  807. #define Py_RETURN_RICHCOMPARE(val1, val2, op) \
  808. do { \
  809. switch (op) { \
  810. case Py_EQ: if ((val1) == (val2)) Py_RETURN_TRUE; Py_RETURN_FALSE; \
  811. case Py_NE: if ((val1) != (val2)) Py_RETURN_TRUE; Py_RETURN_FALSE; \
  812. case Py_LT: if ((val1) < (val2)) Py_RETURN_TRUE; Py_RETURN_FALSE; \
  813. case Py_GT: if ((val1) > (val2)) Py_RETURN_TRUE; Py_RETURN_FALSE; \
  814. case Py_LE: if ((val1) <= (val2)) Py_RETURN_TRUE; Py_RETURN_FALSE; \
  815. case Py_GE: if ((val1) >= (val2)) Py_RETURN_TRUE; Py_RETURN_FALSE; \
  816. default: \
  817. Py_UNREACHABLE(); \
  818. } \
  819. } while (0)
  820. #ifndef Py_LIMITED_API
  821. /* Maps Py_LT to Py_GT, ..., Py_GE to Py_LE.
  822. * Defined in object.c.
  823. */
  824. PyAPI_DATA(int) _Py_SwappedOp[];
  825. #endif /* !Py_LIMITED_API */
  826. /*
  827. More conventions
  828. ================
  829. Argument Checking
  830. -----------------
  831. Functions that take objects as arguments normally don't check for nil
  832. arguments, but they do check the type of the argument, and return an
  833. error if the function doesn't apply to the type.
  834. Failure Modes
  835. -------------
  836. Functions may fail for a variety of reasons, including running out of
  837. memory. This is communicated to the caller in two ways: an error string
  838. is set (see errors.h), and the function result differs: functions that
  839. normally return a pointer return NULL for failure, functions returning
  840. an integer return -1 (which could be a legal return value too!), and
  841. other functions return 0 for success and -1 for failure.
  842. Callers should always check for errors before using the result. If
  843. an error was set, the caller must either explicitly clear it, or pass
  844. the error on to its caller.
  845. Reference Counts
  846. ----------------
  847. It takes a while to get used to the proper usage of reference counts.
  848. Functions that create an object set the reference count to 1; such new
  849. objects must be stored somewhere or destroyed again with Py_DECREF().
  850. Some functions that 'store' objects, such as PyTuple_SetItem() and
  851. PyList_SetItem(),
  852. don't increment the reference count of the object, since the most
  853. frequent use is to store a fresh object. Functions that 'retrieve'
  854. objects, such as PyTuple_GetItem() and PyDict_GetItemString(), also
  855. don't increment
  856. the reference count, since most frequently the object is only looked at
  857. quickly. Thus, to retrieve an object and store it again, the caller
  858. must call Py_INCREF() explicitly.
  859. NOTE: functions that 'consume' a reference count, like
  860. PyList_SetItem(), consume the reference even if the object wasn't
  861. successfully stored, to simplify error handling.
  862. It seems attractive to make other functions that take an object as
  863. argument consume a reference count; however, this may quickly get
  864. confusing (even the current practice is already confusing). Consider
  865. it carefully, it may save lots of calls to Py_INCREF() and Py_DECREF() at
  866. times.
  867. */
  868. /* Trashcan mechanism, thanks to Christian Tismer.
  869. When deallocating a container object, it's possible to trigger an unbounded
  870. chain of deallocations, as each Py_DECREF in turn drops the refcount on "the
  871. next" object in the chain to 0. This can easily lead to stack faults, and
  872. especially in threads (which typically have less stack space to work with).
  873. A container object that participates in cyclic gc can avoid this by
  874. bracketing the body of its tp_dealloc function with a pair of macros:
  875. static void
  876. mytype_dealloc(mytype *p)
  877. {
  878. ... declarations go here ...
  879. PyObject_GC_UnTrack(p); // must untrack first
  880. Py_TRASHCAN_SAFE_BEGIN(p)
  881. ... The body of the deallocator goes here, including all calls ...
  882. ... to Py_DECREF on contained objects. ...
  883. Py_TRASHCAN_SAFE_END(p)
  884. }
  885. CAUTION: Never return from the middle of the body! If the body needs to
  886. "get out early", put a label immediately before the Py_TRASHCAN_SAFE_END
  887. call, and goto it. Else the call-depth counter (see below) will stay
  888. above 0 forever, and the trashcan will never get emptied.
  889. How it works: The BEGIN macro increments a call-depth counter. So long
  890. as this counter is small, the body of the deallocator is run directly without
  891. further ado. But if the counter gets large, it instead adds p to a list of
  892. objects to be deallocated later, skips the body of the deallocator, and
  893. resumes execution after the END macro. The tp_dealloc routine then returns
  894. without deallocating anything (and so unbounded call-stack depth is avoided).
  895. When the call stack finishes unwinding again, code generated by the END macro
  896. notices this, and calls another routine to deallocate all the objects that
  897. may have been added to the list of deferred deallocations. In effect, a
  898. chain of N deallocations is broken into (N-1)/(PyTrash_UNWIND_LEVEL-1) pieces,
  899. with the call stack never exceeding a depth of PyTrash_UNWIND_LEVEL.
  900. */
  901. #ifndef Py_LIMITED_API
  902. /* This is the old private API, invoked by the macros before 3.2.4.
  903. Kept for binary compatibility of extensions using the stable ABI. */
  904. PyAPI_FUNC(void) _PyTrash_deposit_object(PyObject*);
  905. PyAPI_FUNC(void) _PyTrash_destroy_chain(void);
  906. #endif /* !Py_LIMITED_API */
  907. /* The new thread-safe private API, invoked by the macros below. */
  908. PyAPI_FUNC(void) _PyTrash_thread_deposit_object(PyObject*);
  909. PyAPI_FUNC(void) _PyTrash_thread_destroy_chain(void);
  910. #define PyTrash_UNWIND_LEVEL 50
  911. #define Py_TRASHCAN_SAFE_BEGIN(op) \
  912. do { \
  913. PyThreadState *_tstate = PyThreadState_GET(); \
  914. if (_tstate->trash_delete_nesting < PyTrash_UNWIND_LEVEL) { \
  915. ++_tstate->trash_delete_nesting;
  916. /* The body of the deallocator is here. */
  917. #define Py_TRASHCAN_SAFE_END(op) \
  918. --_tstate->trash_delete_nesting; \
  919. if (_tstate->trash_delete_later && _tstate->trash_delete_nesting <= 0) \
  920. _PyTrash_thread_destroy_chain(); \
  921. } \
  922. else \
  923. _PyTrash_thread_deposit_object((PyObject*)op); \
  924. } while (0);
  925. #ifndef Py_LIMITED_API
  926. PyAPI_FUNC(void)
  927. _PyDebugAllocatorStats(FILE *out, const char *block_name, int num_blocks,
  928. size_t sizeof_block);
  929. PyAPI_FUNC(void)
  930. _PyObject_DebugTypeStats(FILE *out);
  931. #endif /* ifndef Py_LIMITED_API */
  932. #ifdef __cplusplus
  933. }
  934. #endif
  935. #endif /* !Py_OBJECT_H */
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...