FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
cpython/Objects/typeobject.c at main · https-github-com-nzysoft/cpython · GitHub
cpython/Objects/typeobject.c at main · https-github-com-nzysoft/cpython · GitHub
Skip to content
Navigation Menu
Sign in
Appearance settings
AI CODE CREATION
GitHub Copilot
Write better code with AI
GitHub Copilot app
Direct agents from issue to merge
MCP Registry
Integrate external tools
DEVELOPER WORKFLOWS
Actions
Automate any workflow
Codespaces
Instant dev environments
Issues
Plan and track work
Code Review
Manage code changes
Code Quality
Enforce quality at merge
APPLICATION SECURITY
GitHub Advanced Security
Find and fix vulnerabilities
Code security
Secure your code as you build
Secret protection
Stop leaks before they start
EXPLORE
Why GitHub
Documentation
Blog
Changelog
Marketplace
View all features
BY COMPANY SIZE
Enterprises
Small and medium teams
Startups
Nonprofits
BY USE CASE
App Modernization
DevSecOps
DevOps
CI/CD
View all use cases
BY INDUSTRY
Healthcare
Financial services
Manufacturing
Government
View all industries
View all solutions
EXPLORE BY TOPIC
AI
Software Development
DevOps
Security
View all topics
EXPLORE BY TYPE
Customer stories
Events & webinars
Ebooks & reports
Business insights
GitHub Skills
SUPPORT & SERVICES
Documentation
Customer support
Community forum
Trust center
Partners
View all resources
COMMUNITY
GitHub Sponsors
Fund open source developers
PROGRAMS
Security Lab
Maintainer Community
Accelerator
GitHub Stars
Archive Program
REPOSITORIES
Topics
Trending
Collections
ENTERPRISE SOLUTIONS
Enterprise platform
AI-powered developer platform
AVAILABLE ADD-ONS
GitHub Advanced Security
Enterprise-grade security features
Copilot for Business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Pricing
Sign in
Sign up
Appearance settings
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
Uh oh!
There was an error while loading.
Please reload this page
.
https-github-com-nzysoft
/
cpython
Public
forked from
python/cpython
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
cpython
/
Objects
/
typeobject.c
Copy path
More file actions
More file actions
Latest commit
History
History
History
10689 lines (9425 loc) · 319 KB
Breadcrumbs
cpython
/
Objects
/
typeobject.c
Copy path
File metadata and controls
10689 lines (9425 loc) · 319 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
/* Type object implementation */
#include
"Python.h"
#include
"pycore_abstract.h"
// _PySequence_IterSearch()
#include
"pycore_call.h"
// _PyObject_VectorcallTstate()
#include
"pycore_code.h"
// CO_FAST_FREE
#include
"pycore_dict.h"
// _PyDict_KeysSize()
#include
"pycore_frame.h"
// _PyInterpreterFrame
#include
"pycore_long.h"
// _PyLong_IsNegative()
#include
"pycore_memoryobject.h"
// _PyMemoryView_FromBufferProc()
#include
"pycore_modsupport.h"
// _PyArg_NoKwnames()
#include
"pycore_moduleobject.h"
// _PyModule_GetDef()
#include
"pycore_object.h"
// _PyType_HasFeature()
#include
"pycore_pyerrors.h"
// _PyErr_Occurred()
#include
"pycore_pystate.h"
// _PyThreadState_GET()
#include
"pycore_symtable.h"
// _Py_Mangle()
#include
"pycore_typeobject.h"
// struct type_cache
#include
"pycore_unionobject.h"
// _Py_union_type_or
#include
"pycore_weakref.h"
// _PyWeakref_GET_REF()
#include
"opcode.h"
// MAKE_CELL
#include
<stddef.h>
// ptrdiff_t
/*[clinic input]
class type "PyTypeObject *" "&PyType_Type"
class object "PyObject *" "&PyBaseObject_Type"
[clinic start generated code]*/
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=4b94608d231c434b]*/
#include
"clinic/typeobject.c.h"
/* Support type attribute lookup cache */
/* The cache can keep references to the names alive for longer than
they normally would. This is why the maximum size is limited to
MCACHE_MAX_ATTR_SIZE, since it might be a problem if very large
strings are used as attribute names. */
#define
MCACHE_MAX_ATTR_SIZE
100
#define
MCACHE_HASH
(
version
,
name_hash
) \
(((unsigned int)(version) ^ (unsigned int)(name_hash)) \
& ((1 << MCACHE_SIZE_EXP) - 1))
#define
MCACHE_HASH_METHOD
(
type
,
name
) \
MCACHE_HASH((type)->tp_version_tag, ((Py_ssize_t)(name)) >> 3)
#define
MCACHE_CACHEABLE_NAME
(
name
) \
PyUnicode_CheckExact(name) && \
PyUnicode_IS_READY(name) && \
(PyUnicode_GET_LENGTH(name) <= MCACHE_MAX_ATTR_SIZE)
#define
NEXT_GLOBAL_VERSION_TAG
_PyRuntime.types.next_version_tag
#define
NEXT_VERSION_TAG
(
interp
) \
(interp)->types.next_version_tag
typedef
struct
PySlot_Offset
{
short
subslot_offset
;
short
slot_offset
;
}
PySlot_Offset
;
static
void
slot_bf_releasebuffer
(
PyObject
*
self
,
Py_buffer
*
buffer
);
static
void
releasebuffer_call_python
(
PyObject
*
self
,
Py_buffer
*
buffer
);
static
PyObject
*
slot_tp_new
(
PyTypeObject
*
type
,
PyObject
*
args
,
PyObject
*
kwds
);
static
PyObject
*
lookup_maybe_method
(
PyObject
*
self
,
PyObject
*
attr
,
int
*
unbound
);
static
int
slot_tp_setattro
(
PyObject
*
self
,
PyObject
*
name
,
PyObject
*
value
);
static
inline
PyTypeObject
*
type_from_ref
(
PyObject
*
ref
)
{
PyObject
*
obj
=
_PyWeakref_GET_REF
(
ref
);
if
(
obj
==
NULL
) {
return
NULL
;
}
return
_PyType_CAST
(
obj
);
}
/* helpers for for static builtin types */
static
inline
int
static_builtin_index_is_set
(
PyTypeObject
*
self
)
{
return
self
->
tp_subclasses
!=
NULL
;
}
static
inline
size_t
static_builtin_index_get
(
PyTypeObject
*
self
)
{
assert
(
static_builtin_index_is_set
(
self
));
/* We store a 1-based index so 0 can mean "not initialized". */
return
(
size_t
)
self
->
tp_subclasses
-
1
;
}
static
inline
void
static_builtin_index_set
(
PyTypeObject
*
self
,
size_t
index
)
{
assert
(
index
<
_Py_MAX_STATIC_BUILTIN_TYPES
);
/* We store a 1-based index so 0 can mean "not initialized". */
self
->
tp_subclasses
=
(
PyObject
*
)(
index
+
1
);
}
static
inline
void
static_builtin_index_clear
(
PyTypeObject
*
self
)
{
self
->
tp_subclasses
=
NULL
;
}
static
inline
static_builtin_state
*
static_builtin_state_get
(
PyInterpreterState
*
interp
,
PyTypeObject
*
self
)
{
return
&
(
interp
->
types
.
builtins
[
static_builtin_index_get
(
self
)]);
}
/* For static types we store some state in an array on each interpreter. */
static_builtin_state
*
_PyStaticType_GetState
(
PyInterpreterState
*
interp
,
PyTypeObject
*
self
)
{
assert
(
self
->
tp_flags
&
_Py_TPFLAGS_STATIC_BUILTIN
);
return
static_builtin_state_get
(
interp
,
self
);
}
/* Set the type's per-interpreter state. */
static
void
static_builtin_state_init
(
PyInterpreterState
*
interp
,
PyTypeObject
*
self
)
{
if
(!
static_builtin_index_is_set
(
self
)) {
static_builtin_index_set
(
self
,
interp
->
types
.
num_builtins_initialized
);
}
static_builtin_state
*
state
=
static_builtin_state_get
(
interp
,
self
);
/* It should only be called once for each builtin type. */
assert
(
state
->
type
==
NULL
);
state
->
type
=
self
;
/* state->tp_subclasses is left NULL until init_subclasses() sets it. */
/* state->tp_weaklist is left NULL until insert_head() or insert_after()
(in weakrefobject.c) sets it. */
interp
->
types
.
num_builtins_initialized
++
;
}
/* Reset the type's per-interpreter state.
This basically undoes what static_builtin_state_init() did. */
static
void
static_builtin_state_clear
(
PyInterpreterState
*
interp
,
PyTypeObject
*
self
)
{
static_builtin_state
*
state
=
static_builtin_state_get
(
interp
,
self
);
assert
(
state
->
type
!=
NULL
);
state
->
type
=
NULL
;
assert
(
state
->
tp_weaklist
==
NULL
);
// It was already cleared out.
if
(
_Py_IsMainInterpreter
(
interp
)) {
static_builtin_index_clear
(
self
);
}
assert
(
interp
->
types
.
num_builtins_initialized
>
0
);
interp
->
types
.
num_builtins_initialized
--
;
}
// Also see _PyStaticType_InitBuiltin() and _PyStaticType_Dealloc().
/* end static builtin helpers */
static
inline
void
start_readying
(
PyTypeObject
*
type
)
{
if
(
type
->
tp_flags
&
_Py_TPFLAGS_STATIC_BUILTIN
) {
PyInterpreterState
*
interp
=
_PyInterpreterState_GET
();
static_builtin_state
*
state
=
static_builtin_state_get
(
interp
,
type
);
assert
(
state
!=
NULL
);
assert
(!
state
->
readying
);
state
->
readying
=
1
;
return
;
}
assert
((
type
->
tp_flags
&
Py_TPFLAGS_READYING
)
==
0
);
type
->
tp_flags
|=
Py_TPFLAGS_READYING
;
}
static
inline
void
stop_readying
(
PyTypeObject
*
type
)
{
if
(
type
->
tp_flags
&
_Py_TPFLAGS_STATIC_BUILTIN
) {
PyInterpreterState
*
interp
=
_PyInterpreterState_GET
();
static_builtin_state
*
state
=
static_builtin_state_get
(
interp
,
type
);
assert
(
state
!=
NULL
);
assert
(
state
->
readying
);
state
->
readying
=
0
;
return
;
}
assert
(
type
->
tp_flags
&
Py_TPFLAGS_READYING
);
type
->
tp_flags
&= ~
Py_TPFLAGS_READYING
;
}
static
inline
int
is_readying
(
PyTypeObject
*
type
)
{
if
(
type
->
tp_flags
&
_Py_TPFLAGS_STATIC_BUILTIN
) {
PyInterpreterState
*
interp
=
_PyInterpreterState_GET
();
static_builtin_state
*
state
=
static_builtin_state_get
(
interp
,
type
);
assert
(
state
!=
NULL
);
return
state
->
readying
;
}
return
(
type
->
tp_flags
&
Py_TPFLAGS_READYING
)
!=
0
;
}
/* accessors for objects stored on PyTypeObject */
static
inline
PyObject
*
lookup_tp_dict
(
PyTypeObject
*
self
)
{
if
(
self
->
tp_flags
&
_Py_TPFLAGS_STATIC_BUILTIN
) {
PyInterpreterState
*
interp
=
_PyInterpreterState_GET
();
static_builtin_state
*
state
=
_PyStaticType_GetState
(
interp
,
self
);
assert
(
state
!=
NULL
);
return
state
->
tp_dict
;
}
return
self
->
tp_dict
;
}
PyObject
*
_PyType_GetDict
(
PyTypeObject
*
self
)
{
/* It returns a borrowed reference. */
return
lookup_tp_dict
(
self
);
}
PyObject
*
PyType_GetDict
(
PyTypeObject
*
self
)
{
PyObject
*
dict
=
lookup_tp_dict
(
self
);
return
_Py_XNewRef
(
dict
);
}
static
inline
void
set_tp_dict
(
PyTypeObject
*
self
,
PyObject
*
dict
)
{
if
(
self
->
tp_flags
&
_Py_TPFLAGS_STATIC_BUILTIN
) {
PyInterpreterState
*
interp
=
_PyInterpreterState_GET
();
static_builtin_state
*
state
=
_PyStaticType_GetState
(
interp
,
self
);
assert
(
state
!=
NULL
);
state
->
tp_dict
=
dict
;
return
;
}
self
->
tp_dict
=
dict
;
}
static
inline
void
clear_tp_dict
(
PyTypeObject
*
self
)
{
if
(
self
->
tp_flags
&
_Py_TPFLAGS_STATIC_BUILTIN
) {
PyInterpreterState
*
interp
=
_PyInterpreterState_GET
();
static_builtin_state
*
state
=
_PyStaticType_GetState
(
interp
,
self
);
assert
(
state
!=
NULL
);
Py_CLEAR
(
state
->
tp_dict
);
return
;
}
Py_CLEAR
(
self
->
tp_dict
);
}
static
inline
PyObject
*
lookup_tp_bases
(
PyTypeObject
*
self
)
{
return
self
->
tp_bases
;
}
PyObject
*
_PyType_GetBases
(
PyTypeObject
*
self
)
{
/* It returns a borrowed reference. */
return
lookup_tp_bases
(
self
);
}
static
inline
void
set_tp_bases
(
PyTypeObject
*
self
,
PyObject
*
bases
)
{
assert
(
PyTuple_CheckExact
(
bases
));
if
(
self
->
tp_flags
&
_Py_TPFLAGS_STATIC_BUILTIN
) {
// XXX tp_bases can probably be statically allocated for each
// static builtin type.
assert
(
_Py_IsMainInterpreter
(
_PyInterpreterState_GET
()));
assert
(
self
->
tp_bases
==
NULL
);
if
(
PyTuple_GET_SIZE
(
bases
)
==
0
) {
assert
(
self
->
tp_base
==
NULL
);
}
else
{
assert
(
PyTuple_GET_SIZE
(
bases
)
==
1
);
assert
(
PyTuple_GET_ITEM
(
bases
,
0
)
==
(
PyObject
*
)
self
->
tp_base
);
assert
(
self
->
tp_base
->
tp_flags
&
_Py_TPFLAGS_STATIC_BUILTIN
);
assert
(
_Py_IsImmortal
(
self
->
tp_base
));
}
_Py_SetImmortal
(
bases
);
}
self
->
tp_bases
=
bases
;
}
static
inline
void
clear_tp_bases
(
PyTypeObject
*
self
)
{
if
(
self
->
tp_flags
&
_Py_TPFLAGS_STATIC_BUILTIN
) {
if
(
_Py_IsMainInterpreter
(
_PyInterpreterState_GET
())) {
if
(
self
->
tp_bases
!=
NULL
) {
if
(
PyTuple_GET_SIZE
(
self
->
tp_bases
)
==
0
) {
Py_CLEAR
(
self
->
tp_bases
);
}
else
{
assert
(
_Py_IsImmortal
(
self
->
tp_bases
));
_Py_ClearImmortal
(
self
->
tp_bases
);
}
}
}
return
;
}
Py_CLEAR
(
self
->
tp_bases
);
}
static
inline
PyObject
*
lookup_tp_mro
(
PyTypeObject
*
self
)
{
return
self
->
tp_mro
;
}
PyObject
*
_PyType_GetMRO
(
PyTypeObject
*
self
)
{
/* It returns a borrowed reference. */
return
lookup_tp_mro
(
self
);
}
static
inline
void
set_tp_mro
(
PyTypeObject
*
self
,
PyObject
*
mro
)
{
assert
(
PyTuple_CheckExact
(
mro
));
if
(
self
->
tp_flags
&
_Py_TPFLAGS_STATIC_BUILTIN
) {
// XXX tp_mro can probably be statically allocated for each
// static builtin type.
assert
(
_Py_IsMainInterpreter
(
_PyInterpreterState_GET
()));
assert
(
self
->
tp_mro
==
NULL
);
/* Other checks are done via set_tp_bases. */
_Py_SetImmortal
(
mro
);
}
self
->
tp_mro
=
mro
;
}
static
inline
void
clear_tp_mro
(
PyTypeObject
*
self
)
{
if
(
self
->
tp_flags
&
_Py_TPFLAGS_STATIC_BUILTIN
) {
if
(
_Py_IsMainInterpreter
(
_PyInterpreterState_GET
())) {
if
(
self
->
tp_mro
!=
NULL
) {
if
(
PyTuple_GET_SIZE
(
self
->
tp_mro
)
==
0
) {
Py_CLEAR
(
self
->
tp_mro
);
}
else
{
assert
(
_Py_IsImmortal
(
self
->
tp_mro
));
_Py_ClearImmortal
(
self
->
tp_mro
);
}
}
}
return
;
}
Py_CLEAR
(
self
->
tp_mro
);
}
static
PyObject
*
init_tp_subclasses
(
PyTypeObject
*
self
)
{
PyObject
*
subclasses
=
PyDict_New
();
if
(
subclasses
==
NULL
) {
return
NULL
;
}
if
(
self
->
tp_flags
&
_Py_TPFLAGS_STATIC_BUILTIN
) {
PyInterpreterState
*
interp
=
_PyInterpreterState_GET
();
static_builtin_state
*
state
=
_PyStaticType_GetState
(
interp
,
self
);
state
->
tp_subclasses
=
subclasses
;
return
subclasses
;
}
self
->
tp_subclasses
=
(
void
*
)
subclasses
;
return
subclasses
;
}
static
void
clear_tp_subclasses
(
PyTypeObject
*
self
)
{
/* Delete the dictionary to save memory. _PyStaticType_Dealloc()
callers also test if tp_subclasses is NULL to check if a static type
has no subclass. */
if
(
self
->
tp_flags
&
_Py_TPFLAGS_STATIC_BUILTIN
) {
PyInterpreterState
*
interp
=
_PyInterpreterState_GET
();
static_builtin_state
*
state
=
_PyStaticType_GetState
(
interp
,
self
);
Py_CLEAR
(
state
->
tp_subclasses
);
return
;
}
Py_CLEAR
(
self
->
tp_subclasses
);
}
static
inline
PyObject
*
lookup_tp_subclasses
(
PyTypeObject
*
self
)
{
if
(
self
->
tp_flags
&
_Py_TPFLAGS_STATIC_BUILTIN
) {
PyInterpreterState
*
interp
=
_PyInterpreterState_GET
();
static_builtin_state
*
state
=
_PyStaticType_GetState
(
interp
,
self
);
assert
(
state
!=
NULL
);
return
state
->
tp_subclasses
;
}
return
(
PyObject
*
)
self
->
tp_subclasses
;
}
int
_PyType_HasSubclasses
(
PyTypeObject
*
self
)
{
PyInterpreterState
*
interp
=
_PyInterpreterState_GET
();
if
(
self
->
tp_flags
&
_Py_TPFLAGS_STATIC_BUILTIN
// XXX _PyStaticType_GetState() should never return NULL.
&&
_PyStaticType_GetState
(
interp
,
self
)
==
NULL
)
{
return
0
;
}
if
(
lookup_tp_subclasses
(
self
)
==
NULL
) {
return
0
;
}
return
1
;
}
PyObject
*
_PyType_GetSubclasses
(
PyTypeObject
*
self
)
{
PyObject
*
list
=
PyList_New
(
0
);
if
(
list
==
NULL
) {
return
NULL
;
}
PyObject
*
subclasses
=
lookup_tp_subclasses
(
self
);
// borrowed ref
if
(
subclasses
==
NULL
) {
return
list
;
}
assert
(
PyDict_CheckExact
(
subclasses
));
// The loop cannot modify tp_subclasses, there is no need
// to hold a strong reference (use a borrowed reference).
Py_ssize_t
i
=
0
;
PyObject
*
ref
;
// borrowed ref
while
(
PyDict_Next
(
subclasses
,
&
i
,
NULL
,
&
ref
)) {
PyTypeObject
*
subclass
=
type_from_ref
(
ref
);
if
(
subclass
==
NULL
) {
continue
;
}
if
(
PyList_Append
(
list
,
_PyObject_CAST
(
subclass
))
<
0
) {
Py_DECREF
(
list
);
Py_DECREF
(
subclass
);
return
NULL
;
}
Py_DECREF
(
subclass
);
}
return
list
;
}
/* end accessors for objects stored on PyTypeObject */
/*
* finds the beginning of the docstring's introspection signature.
* if present, returns a pointer pointing to the first '('.
* otherwise returns NULL.
*
* doesn't guarantee that the signature is valid, only that it
* has a valid prefix. (the signature must also pass skip_signature.)
*/
static
const
char
*
find_signature
(
const
char
*
name
,
const
char
*
doc
)
{
const
char
*
dot
;
size_t
length
;
if
(!
doc
)
return
NULL
;
assert
(
name
!=
NULL
);
/* for dotted names like classes, only use the last component */
dot
=
strrchr
(
name
,
'.'
);
if
(
dot
)
name
=
dot
+
1
;
length
=
strlen
(
name
);
if
(
strncmp
(
doc
,
name
,
length
))
return
NULL
;
doc
+=
length
;
if
(
*
doc
!=
'('
)
return
NULL
;
return
doc
;
}
#define
SIGNATURE_END_MARKER
")\n--\n\n"
#define
SIGNATURE_END_MARKER_LENGTH
6
/*
* skips past the end of the docstring's introspection signature.
* (assumes doc starts with a valid signature prefix.)
*/
static
const
char
*
skip_signature
(
const
char
*
doc
)
{
while
(
*
doc
) {
if
((
*
doc
==
*
SIGNATURE_END_MARKER
)
&&
!
strncmp
(
doc
,
SIGNATURE_END_MARKER
,
SIGNATURE_END_MARKER_LENGTH
))
return
doc
+
SIGNATURE_END_MARKER_LENGTH
;
if
((
*
doc
==
'\n'
)
&&
(
doc
[
1
]
==
'\n'
))
return
NULL
;
doc
++
;
}
return
NULL
;
}
int
_PyType_CheckConsistency
(
PyTypeObject
*
type
)
{
#define
CHECK
(
expr
) \
do { if (!(expr)) { _PyObject_ASSERT_FAILED_MSG((PyObject *)type, Py_STRINGIFY(expr)); } } while (0)
CHECK
(!
_PyObject_IsFreed
((
PyObject
*
)
type
));
if
(!(
type
->
tp_flags
&
Py_TPFLAGS_READY
)) {
/* don't check static types before PyType_Ready() */
return
1
;
}
CHECK
(
Py_REFCNT
(
type
) >=
1
);
CHECK
(
PyType_Check
(
type
));
CHECK
(!
is_readying
(
type
));
CHECK
(
lookup_tp_dict
(
type
)
!=
NULL
);
if
(
type
->
tp_flags
&
Py_TPFLAGS_HAVE_GC
) {
// bpo-44263: tp_traverse is required if Py_TPFLAGS_HAVE_GC is set.
// Note: tp_clear is optional.
CHECK
(
type
->
tp_traverse
!=
NULL
);
}
if
(
type
->
tp_flags
&
Py_TPFLAGS_DISALLOW_INSTANTIATION
) {
CHECK
(
type
->
tp_new
==
NULL
);
CHECK
(
PyDict_Contains
(
lookup_tp_dict
(
type
),
&
_Py_ID
(
__new__
))
==
0
);
}
return
1
;
#undef
CHECK
}
static
const
char
*
_PyType_DocWithoutSignature
(
const
char
*
name
,
const
char
*
internal_doc
)
{
const
char
*
doc
=
find_signature
(
name
,
internal_doc
);
if
(
doc
) {
doc
=
skip_signature
(
doc
);
if
(
doc
)
return
doc
;
}
return
internal_doc
;
}
PyObject
*
_PyType_GetDocFromInternalDoc
(
const
char
*
name
,
const
char
*
internal_doc
)
{
const
char
*
doc
=
_PyType_DocWithoutSignature
(
name
,
internal_doc
);
if
(!
doc
||
*
doc
==
'\0'
) {
Py_RETURN_NONE
;
}
return
PyUnicode_FromString
(
doc
);
}
static
const
char
*
signature_from_flags
(
int
flags
)
{
switch
(
flags
&
~
METH_COEXIST
) {
case
METH_NOARGS
:
return
"($self, /)"
;
case
METH_NOARGS
|
METH_CLASS
:
return
"($type, /)"
;
case
METH_NOARGS
|
METH_STATIC
:
return
"()"
;
case
METH_O
:
return
"($self, object, /)"
;
case
METH_O
|
METH_CLASS
:
return
"($type, object, /)"
;
case
METH_O
|
METH_STATIC
:
return
"(object, /)"
;
default
:
return
NULL
;
}
}
PyObject
*
_PyType_GetTextSignatureFromInternalDoc
(
const
char
*
name
,
const
char
*
internal_doc
,
int
flags
)
{
const
char
*
start
=
find_signature
(
name
,
internal_doc
);
const
char
*
end
;
if
(
start
)
end
=
skip_signature
(
start
);
else
end
=
NULL
;
if
(!
end
) {
start
=
signature_from_flags
(
flags
);
if
(
start
) {
return
PyUnicode_FromString
(
start
);
}
Py_RETURN_NONE
;
}
/* back "end" up until it points just past the final ')' */
end
-=
SIGNATURE_END_MARKER_LENGTH
-
1
;
assert
((
end
-
start
) >=
2
);
/* should be "()" at least */
assert
(
end
[
-1
]
==
')'
);
assert
(
end
[
0
]
==
'\n'
);
return
PyUnicode_FromStringAndSize
(
start
,
end
-
start
);
}
static
struct
type_cache
*
get_type_cache
(
void
)
{
PyInterpreterState
*
interp
=
_PyInterpreterState_GET
();
return
&
interp
->
types
.
type_cache
;
}
static
void
type_cache_clear
(
struct
type_cache
*
cache
,
PyObject
*
value
)
{
for
(
Py_ssize_t
i
=
0
;
i
<
(
1
<<
MCACHE_SIZE_EXP
);
i
++
) {
struct
type_cache_entry
*
entry
=
&
cache
->
hashtable
[
i
];
entry
->
version
=
0
;
Py_XSETREF
(
entry
->
name
,
_Py_XNewRef
(
value
));
entry
->
value
=
NULL
;
}
}
void
_PyType_InitCache
(
PyInterpreterState
*
interp
)
{
struct
type_cache
*
cache
=
&
interp
->
types
.
type_cache
;
for
(
Py_ssize_t
i
=
0
;
i
<
(
1
<<
MCACHE_SIZE_EXP
);
i
++
) {
struct
type_cache_entry
*
entry
=
&
cache
->
hashtable
[
i
];
assert
(
entry
->
name
==
NULL
);
entry
->
version
=
0
;
// Set to None so _PyType_Lookup() can use Py_SETREF(),
// rather than using slower Py_XSETREF().
entry
->
name
=
Py_None
;
entry
->
value
=
NULL
;
}
}
static
unsigned
int
_PyType_ClearCache
(
PyInterpreterState
*
interp
)
{
struct
type_cache
*
cache
=
&
interp
->
types
.
type_cache
;
// Set to None, rather than NULL, so _PyType_Lookup() can
// use Py_SETREF() rather than using slower Py_XSETREF().
type_cache_clear
(
cache
,
Py_None
);
return
NEXT_VERSION_TAG
(
interp
)
-
1
;
}
unsigned
int
PyType_ClearCache
(
void
)
{
PyInterpreterState
*
interp
=
_PyInterpreterState_GET
();
return
_PyType_ClearCache
(
interp
);
}
void
_PyTypes_Fini
(
PyInterpreterState
*
interp
)
{
struct
type_cache
*
cache
=
&
interp
->
types
.
type_cache
;
type_cache_clear
(
cache
,
NULL
);
assert
(
interp
->
types
.
num_builtins_initialized
==
0
);
// All the static builtin types should have been finalized already.
for
(
size_t
i
=
0
;
i
<
_Py_MAX_STATIC_BUILTIN_TYPES
;
i
++
) {
assert
(
interp
->
types
.
builtins
[
i
].
type
==
NULL
);
}
}
int
PyType_AddWatcher
(
PyType_WatchCallback
callback
)
{
PyInterpreterState
*
interp
=
_PyInterpreterState_GET
();
for
(
int
i
=
0
;
i
<
TYPE_MAX_WATCHERS
;
i
++
) {
if
(!
interp
->
type_watchers
[
i
]) {
interp
->
type_watchers
[
i
]
=
callback
;
return
i
;
}
}
PyErr_SetString
(
PyExc_RuntimeError
,
"no more type watcher IDs available"
);
return
-1
;
}
static
inline
int
validate_watcher_id
(
PyInterpreterState
*
interp
,
int
watcher_id
)
{
if
(
watcher_id
<
0
||
watcher_id
>=
TYPE_MAX_WATCHERS
) {
PyErr_Format
(
PyExc_ValueError
,
"Invalid type watcher ID %d"
,
watcher_id
);
return
-1
;
}
if
(!
interp
->
type_watchers
[
watcher_id
]) {
PyErr_Format
(
PyExc_ValueError
,
"No type watcher set for ID %d"
,
watcher_id
);
return
-1
;
}
return
0
;
}
int
PyType_ClearWatcher
(
int
watcher_id
)
{
PyInterpreterState
*
interp
=
_PyInterpreterState_GET
();
if
(
validate_watcher_id
(
interp
,
watcher_id
)
<
0
) {
return
-1
;
}
interp
->
type_watchers
[
watcher_id
]
=
NULL
;
return
0
;
}
static
int
assign_version_tag
(
PyInterpreterState
*
interp
,
PyTypeObject
*
type
);
int
PyType_Watch
(
int
watcher_id
,
PyObject
*
obj
)
{
if
(!
PyType_Check
(
obj
)) {
PyErr_SetString
(
PyExc_ValueError
,
"Cannot watch non-type"
);
return
-1
;
}
PyTypeObject
*
type
=
(
PyTypeObject
*
)
obj
;
PyInterpreterState
*
interp
=
_PyInterpreterState_GET
();
if
(
validate_watcher_id
(
interp
,
watcher_id
)
<
0
) {
return
-1
;
}
// ensure we will get a callback on the next modification
assign_version_tag
(
interp
,
type
);
type
->
tp_watched
|= (
1
<<
watcher_id
);
return
0
;
}
int
PyType_Unwatch
(
int
watcher_id
,
PyObject
*
obj
)
{
if
(!
PyType_Check
(
obj
)) {
PyErr_SetString
(
PyExc_ValueError
,
"Cannot watch non-type"
);
return
-1
;
}
PyTypeObject
*
type
=
(
PyTypeObject
*
)
obj
;
PyInterpreterState
*
interp
=
_PyInterpreterState_GET
();
if
(
validate_watcher_id
(
interp
,
watcher_id
)) {
return
-1
;
}
type
->
tp_watched
&= ~(
1
<<
watcher_id
);
return
0
;
}
void
PyType_Modified
(
PyTypeObject
*
type
)
{
/* Invalidate any cached data for the specified type and all
subclasses. This function is called after the base
classes, mro, or attributes of the type are altered.
Invariants:
- before Py_TPFLAGS_VALID_VERSION_TAG can be set on a type,
it must first be set on all super types.
This function clears the Py_TPFLAGS_VALID_VERSION_TAG of a
type (so it must first clear it on all subclasses). The
tp_version_tag value is meaningless unless this flag is set.
We don't assign new version tags eagerly, but only as
needed.
*/
if
(!
_PyType_HasFeature
(
type
,
Py_TPFLAGS_VALID_VERSION_TAG
)) {
return
;
}
PyObject
*
subclasses
=
lookup_tp_subclasses
(
type
);
if
(
subclasses
!=
NULL
) {
assert
(
PyDict_CheckExact
(
subclasses
));
Py_ssize_t
i
=
0
;
PyObject
*
ref
;
while
(
PyDict_Next
(
subclasses
,
&
i
,
NULL
,
&
ref
)) {
PyTypeObject
*
subclass
=
type_from_ref
(
ref
);
if
(
subclass
==
NULL
) {
continue
;
}
PyType_Modified
(
subclass
);
Py_DECREF
(
subclass
);
}
}
// Notify registered type watchers, if any
if
(
type
->
tp_watched
) {
PyInterpreterState
*
interp
=
_PyInterpreterState_GET
();
int
bits
=
type
->
tp_watched
;
int
i
=
0
;
while
(
bits
) {
assert
(
i
<
TYPE_MAX_WATCHERS
);
if
(
bits
&
1
) {
PyType_WatchCallback
cb
=
interp
->
type_watchers
[
i
];
if
(
cb
&&
(
cb
(
type
)
<
0
)) {
PyErr_WriteUnraisable
((
PyObject
*
)
type
);
}
}
i
++
;
bits
>>=
1
;
}
}
type
->
tp_flags
&= ~
Py_TPFLAGS_VALID_VERSION_TAG
;
type
->
tp_version_tag
=
0
;
/* 0 is not a valid version tag */
if
(
PyType_HasFeature
(
type
,
Py_TPFLAGS_HEAPTYPE
)) {
// This field *must* be invalidated if the type is modified (see the
// comment on struct _specialization_cache):
((
PyHeapTypeObject
*
)
type
)
->
_spec_cache
.
getitem
=
NULL
;
}
}
static
void
type_mro_modified
(
PyTypeObject
*
type
,
PyObject
*
bases
) {
/*
Check that all base classes or elements of the MRO of type are
able to be cached. This function is called after the base
classes or mro of the type are altered.
Unset HAVE_VERSION_TAG and VALID_VERSION_TAG if the type
has a custom MRO that includes a type which is not officially
super type, or if the type implements its own mro() method.
Called from mro_internal, which will subsequently be called on
each subclass when their mro is recursively updated.
*/
Py_ssize_t
i
,
n
;
int
custom
=
!
Py_IS_TYPE
(
type
,
&
PyType_Type
);
int
unbound
;
if
(
custom
) {
PyObject
*
mro_meth
,
*
type_mro_meth
;
mro_meth
=
lookup_maybe_method
(
(
PyObject
*
)
type
,
&
_Py_ID
(
mro
),
&
unbound
);
if
(
mro_meth
==
NULL
) {
goto
clear
;
}
type_mro_meth
=
lookup_maybe_method
(
(
PyObject
*
)
&
PyType_Type
,
&
_Py_ID
(
mro
),
&
unbound
);
if
(
type_mro_meth
==
NULL
) {
Py_DECREF
(
mro_meth
);
goto
clear
;
}
int
custom_mro
=
(
mro_meth
!=
type_mro_meth
);
Py_DECREF
(
mro_meth
);
Py_DECREF
(
type_mro_meth
);
if
(
custom_mro
) {
goto
clear
;
}
}
n
=
PyTuple_GET_SIZE
(
bases
);
for
(
i
=
0
;
i
<
n
;
i
++
) {
PyObject
*
b
=
PyTuple_GET_ITEM
(
bases
,
i
);
PyTypeObject
*
cls
=
_PyType_CAST
(
b
);
if
(!
PyType_IsSubtype
(
type
,
cls
)) {
goto
clear
;
}
}
return
;
clear
:
assert
(!(
type
->
tp_flags
&
_Py_TPFLAGS_STATIC_BUILTIN
));
type
->
tp_flags
&= ~
Py_TPFLAGS_VALID_VERSION_TAG
;
type
->
tp_version_tag
=
0
;
/* 0 is not a valid version tag */
if
(
PyType_HasFeature
(
type
,
Py_TPFLAGS_HEAPTYPE
)) {
// This field *must* be invalidated if the type is modified (see the
// comment on struct _specialization_cache):
((
PyHeapTypeObject
*
)
type
)
->
_spec_cache
.
getitem
=
NULL
;
}
}
static
int
assign_version_tag
(
PyInterpreterState
*
interp
,
PyTypeObject
*
type
)
{
/* Ensure that the tp_version_tag is valid and set
Py_TPFLAGS_VALID_VERSION_TAG. To respect the invariant, this
must first be done on all super classes. Return 0 if this
cannot be done, 1 if Py_TPFLAGS_VALID_VERSION_TAG.
*/
if
(
_PyType_HasFeature
(
type
,
Py_TPFLAGS_VALID_VERSION_TAG
)) {
return
1
;
}
if
(!
_PyType_HasFeature
(
type
,
Py_TPFLAGS_READY
)) {
return
0
;
}
if
(
type
->
tp_flags
&
Py_TPFLAGS_IMMUTABLETYPE
) {
/* static types */
if
(
NEXT_GLOBAL_VERSION_TAG
>
_Py_MAX_GLOBAL_TYPE_VERSION_TAG
) {
/* We have run out of version numbers */
return
0
;
}
type
->
tp_version_tag
=
NEXT_GLOBAL_VERSION_TAG
++
;
assert
(
type
->
tp_version_tag
<=
_Py_MAX_GLOBAL_TYPE_VERSION_TAG
);
}
else
{
/* heap types */
if
(
NEXT_VERSION_TAG
(
interp
)
==
0
) {
/* We have run out of version numbers */
return
0
;
}
type
->
tp_version_tag
=
NEXT_VERSION_TAG
(
interp
)
++
;
assert
(
type
->
tp_version_tag
!=
0
);
}
PyObject
*
bases
=
lookup_tp_bases
(
type
);
Py_ssize_t
n
=
PyTuple_GET_SIZE
(
bases
);
for
(
Py_ssize_t
i
=
0
;
i
<
n
;
i
++
) {
PyObject
*
b
=
PyTuple_GET_ITEM
(
bases
,
i
);
if
(!
assign_version_tag
(
interp
,
_PyType_CAST
(
b
)))
return
0
;
}
type
->
tp_flags
|=
Py_TPFLAGS_VALID_VERSION_TAG
;
return
1
;
}
int
PyUnstable_Type_AssignVersionTag
(
PyTypeObject
*
type
)
{
PyInterpreterState
*
interp
=
_PyInterpreterState_GET
();
return
assign_version_tag
(
interp
,
type
);
}
static
PyMemberDef
type_members
[]
=
{
{
"__basicsize__"
,
Py_T_PYSSIZET
, offsetof(
PyTypeObject
,
tp_basicsize
),
Py_READONLY
},
{
"__itemsize__"
,
Py_T_PYSSIZET
, offsetof(
PyTypeObject
,
tp_itemsize
),
Py_READONLY
},
{
"__flags__"
,
Py_T_ULONG
, offsetof(
PyTypeObject
,
tp_flags
),
Py_READONLY
},
/* Note that this value is misleading for static builtin types,
since the memory at this offset will always be NULL. */
{
"__weakrefoffset__"
,
Py_T_PYSSIZET
,
offsetof(
PyTypeObject
,
tp_weaklistoffset
),
Py_READONLY
},
{
"__base__"
,
_Py_T_OBJECT
, offsetof(
PyTypeObject
,
tp_base
),
Py_READONLY
},
{
"__dictoffset__"
,
Py_T_PYSSIZET
,
offsetof(
PyTypeObject
,
tp_dictoffset
),
Py_READONLY
},
{
0
}
};
static
int
check_set_special_type_attr
(
PyTypeObject
*
type
,
PyObject
*
value
,
const
char
*
name
)
{
if
(
_PyType_HasFeature
(
type
,
Py_TPFLAGS_IMMUTABLETYPE
)) {
PyErr_Format
(
PyExc_TypeError
,
"cannot set '%s' attribute of immutable type '%s'"
,
name
,
type
->
tp_name
);
return
0
;
}
if
(!
value
) {
PyErr_Format
(
PyExc_TypeError
,
"cannot delete '%s' attribute of immutable type '%s'"
,
name
,
type
->
tp_name
);
return
0
;
}
if
(
PySys_Audit
(
"object.__setattr__"
,
"OsO"
,
type
,
name
,
value
)
<
0
) {
return
0
;
}
return
1
;
}
const
char
*
_PyType_Name
(
PyTypeObject
*
type
)
{
View remainder of file in raw view
Footer
© 2026 GitHub, Inc.
Footer navigation
Terms
Privacy
Security
Status
Community
Docs
Contact
You can’t perform that action at this time.
Back
|
FazBrowse Home
|
New Git URL