Where Online Learning is simpler!
The C and C++ Include Header Files
cat -n /usr/include/python3.14/internal/pycore_interp_structs.h
1 /* This file contains the struct definitions for interpreter state 2 * and other necessary structs */ 3 4 #ifndef Py_INTERNAL_INTERP_STRUCTS_H 5 #define Py_INTERNAL_INTERP_STRUCTS_H 6 #ifdef __cplusplus 7 extern "C" { 8 #endif 9 10 #include "pycore_ast_state.h" // struct ast_state 11 #include "pycore_llist.h" // struct llist_node 12 #include "pycore_opcode_utils.h" // NUM_COMMON_CONSTANTS 13 #include "pycore_pymath.h" // _PY_SHORT_FLOAT_REPR 14 #include "pycore_structs.h" // PyHamtObject 15 #include "pycore_tstate.h" // _PyThreadStateImpl 16 #include "pycore_typedefs.h" // _PyRuntimeState 17 18 19 #define CODE_MAX_WATCHERS 8 20 #define CONTEXT_MAX_WATCHERS 8 21 #define FUNC_MAX_WATCHERS 8 22 #define TYPE_MAX_WATCHERS 8 23 24 25 #ifdef Py_GIL_DISABLED 26 // This should be prime but otherwise the choice is arbitrary. A larger value 27 // increases concurrency at the expense of memory. 28 # define NUM_WEAKREF_LIST_LOCKS 127 29 #endif 30 31 typedef int (*_Py_pending_call_func)(void *); 32 33 struct _pending_call { 34 _Py_pending_call_func func; 35 void *arg; 36 int flags; 37 }; 38 39 #define PENDINGCALLSARRAYSIZE 300 40 41 struct _pending_calls { 42 PyThreadState *handling_thread; 43 PyMutex mutex; 44 /* Request for running pending calls. */ 45 int32_t npending; 46 /* The maximum allowed number of pending calls. 47 If the queue fills up to this point then _PyEval_AddPendingCall() 48 will return _Py_ADD_PENDING_FULL. */ 49 int32_t max; 50 /* We don't want a flood of pending calls to interrupt any one thread 51 for too long, so we keep a limit on the number handled per pass. 52 A value of 0 means there is no limit (other than the maximum 53 size of the list of pending calls). */ 54 int32_t maxloop; 55 struct _pending_call calls[PENDINGCALLSARRAYSIZE]; 56 int first; 57 int next; 58 }; 59 60 typedef enum { 61 PERF_STATUS_FAILED = -1, // Perf trampoline is in an invalid state 62 PERF_STATUS_NO_INIT = 0, // Perf trampoline is not initialized 63 PERF_STATUS_OK = 1, // Perf trampoline is ready to be executed 64 } perf_status_t; 65 66 #ifdef PY_HAVE_PERF_TRAMPOLINE 67 struct code_arena_st; 68 69 struct trampoline_api_st { 70 void* (*init_state)(void); 71 void (*write_state)(void* state, const void *code_addr, 72 unsigned int code_size, PyCodeObject* code); 73 int (*free_state)(void* state); 74 void *state; 75 Py_ssize_t code_padding; 76 }; 77 #endif 78 79 80 struct _ceval_runtime_state { 81 struct { 82 #ifdef PY_HAVE_PERF_TRAMPOLINE 83 perf_status_t status; 84 int perf_trampoline_type; 85 Py_ssize_t extra_code_index; 86 struct code_arena_st *code_arena; 87 struct trampoline_api_st trampoline_api; 88 FILE *map_file; 89 Py_ssize_t persist_after_fork; 90 _PyFrameEvalFunction prev_eval_frame; 91 Py_ssize_t trampoline_refcount; 92 int code_watcher_id; 93 #else 94 int _not_used; 95 #endif 96 } perf; 97 /* Pending calls to be made only on the main thread. */ 98 // The signal machinery falls back on this 99 // so it must be especially stable and efficient. 100 // For example, we use a preallocated array 101 // for the list of pending calls. 102 struct _pending_calls pending_mainthread; 103 PyMutex unused_sys_trace_profile_mutex; // kept for ABI compatibility 104 }; 105 106 107 struct _ceval_state { 108 /* This variable holds the global instrumentation version. When a thread is 109 running, this value is overlaid onto PyThreadState.eval_breaker so that 110 changes in the instrumentation version will trigger the eval breaker. */ 111 uintptr_t instrumentation_version; 112 int recursion_limit; 113 struct _gil_runtime_state *gil; 114 int own_gil; 115 struct _pending_calls pending; 116 }; 117 118 119 //############### 120 // runtime atexit 121 122 typedef void (*atexit_callbackfunc)(void); 123 124 struct _atexit_runtime_state { 125 PyMutex mutex; 126 #define NEXITFUNCS 32 127 atexit_callbackfunc callbacks[NEXITFUNCS]; 128 int ncallbacks; 129 }; 130 131 132 //################### 133 // interpreter atexit 134 135 typedef void (*atexit_datacallbackfunc)(void *); 136 137 typedef struct atexit_callback { 138 atexit_datacallbackfunc func; 139 void *data; 140 struct atexit_callback *next; 141 } atexit_callback; 142 143 struct atexit_state { 144 #ifdef Py_GIL_DISABLED 145 PyMutex ll_callbacks_lock; 146 #endif 147 atexit_callback *ll_callbacks; 148 149 // XXX The rest of the state could be moved to the atexit module state 150 // and a low-level callback added for it during module exec. 151 // For the moment we leave it here. 152 153 // List containing tuples with callback information. 154 // e.g. [(func, args, kwargs), ...] 155 PyObject *callbacks; 156 }; 157 158 159 /****** Garbage collector **********/ 160 161 /* GC information is stored BEFORE the object structure. */ 162 typedef struct { 163 // Tagged pointer to next object in the list. 164 // 0 means the object is not tracked 165 _Py_ALIGNED_DEF(_PyObject_MIN_ALIGNMENT, uintptr_t) _gc_next; 166 167 // Tagged pointer to previous object in the list. 168 // Lowest two bits are used for flags documented later. 169 // Those bits are made available by the struct's minimum alignment. 170 uintptr_t _gc_prev; 171 } PyGC_Head; 172 173 #define _PyGC_Head_UNUSED PyGC_Head 174 175 struct gc_generation { 176 PyGC_Head head; 177 int threshold; /* collection threshold */ 178 int count; /* count of allocations or collections of younger 179 generations */ 180 }; 181 182 struct gc_collection_stats { 183 /* number of collected objects */ 184 Py_ssize_t collected; 185 /* total number of uncollectable objects (put into gc.garbage) */ 186 Py_ssize_t uncollectable; 187 }; 188 189 /* Running stats per generation */ 190 struct gc_generation_stats { 191 /* total number of collections */ 192 Py_ssize_t collections; 193 /* total number of collected objects */ 194 Py_ssize_t collected; 195 /* total number of uncollectable objects (put into gc.garbage) */ 196 Py_ssize_t uncollectable; 197 }; 198 199 enum _GCPhase { 200 GC_PHASE_MARK = 0, 201 GC_PHASE_COLLECT = 1 202 }; 203 204 /* If we change this, we need to change the default value in the 205 signature of gc.collect. */ 206 #define NUM_GENERATIONS 3 207 208 struct _gc_runtime_state { 209 /* List of objects that still need to be cleaned up, singly linked 210 * via their gc headers' gc_prev pointers. */ 211 PyObject *trash_delete_later; 212 /* Current call-stack depth of tp_dealloc calls. */ 213 int trash_delete_nesting; 214 215 /* Is automatic collection enabled? */ 216 int enabled; 217 int debug; 218 /* linked lists of container objects */ 219 struct gc_generation young; 220 struct gc_generation old[2]; 221 /* a permanent generation which won't be collected */ 222 struct gc_generation permanent_generation; 223 struct gc_generation_stats generation_stats[NUM_GENERATIONS]; 224 /* true if we are currently running the collector */ 225 int collecting; 226 /* list of uncollectable objects */ 227 PyObject *garbage; 228 /* a list of callbacks to be invoked when collection is performed */ 229 PyObject *callbacks; 230 231 Py_ssize_t heap_size; 232 Py_ssize_t work_to_do; 233 /* Which of the old spaces is the visited space */ 234 int visited_space; 235 int phase; 236 237 #ifdef Py_GIL_DISABLED 238 /* This is the number of objects that survived the last full 239 collection. It approximates the number of long lived objects 240 tracked by the GC. 241 242 (by "full collection", we mean a collection of the oldest 243 generation). */ 244 Py_ssize_t long_lived_total; 245 /* This is the number of objects that survived all "non-full" 246 collections, and are awaiting to undergo a full collection for 247 the first time. */ 248 Py_ssize_t long_lived_pending; 249 250 /* True if gc.freeze() has been used. */ 251 int freeze_active; 252 253 /* Memory usage of the process (RSS + swap) after last GC. */ 254 Py_ssize_t last_mem; 255 256 /* This accumulates the new object count whenever collection is deferred 257 due to the RSS increase condition not being meet. Reset on collection. */ 258 Py_ssize_t deferred_count; 259 260 /* Mutex held for gc_should_collect_mem_usage(). */ 261 PyMutex mutex; 262 #endif 263 }; 264 265 #include "pycore_gil.h" // struct _gil_runtime_state 266 267 /**** Import ********/ 268 269 struct _import_runtime_state { 270 /* The builtin modules (defined in config.c). */ 271 struct _inittab *inittab; 272 /* The most recent value assigned to a PyModuleDef.m_base.m_index. 273 This is incremented each time PyModuleDef_Init() is called, 274 which is just about every time an extension module is imported. 275 See PyInterpreterState.modules_by_index for more info. */ 276 Py_ssize_t last_module_index; 277 struct { 278 /* A lock to guard the cache. */ 279 PyMutex mutex; 280 /* The actual cache of (filename, name, PyModuleDef) for modules. 281 Only legacy (single-phase init) extension modules are added 282 and only if they support multiple initialization (m_size >= 0) 283 or are imported in the main interpreter. 284 This is initialized lazily in fix_up_extension() in import.c. 285 Modules are added there and looked up in _imp.find_extension(). */ 286 struct _Py_hashtable_t *hashtable; 287 } extensions; 288 /* Package context -- the full module name for package imports */ 289 const char * pkgcontext; 290 }; 291 292 struct _import_state { 293 /* cached sys.modules dictionary */ 294 PyObject *modules; 295 /* This is the list of module objects for all legacy (single-phase init) 296 extension modules ever loaded in this process (i.e. imported 297 in this interpreter or in any other). Py_None stands in for 298 modules that haven't actually been imported in this interpreter. 299 300 A module's index (PyModuleDef.m_base.m_index) is used to look up 301 the corresponding module object for this interpreter, if any. 302 (See PyState_FindModule().) When any extension module 303 is initialized during import, its moduledef gets initialized by 304 PyModuleDef_Init(), and the first time that happens for each 305 PyModuleDef, its index gets set to the current value of 306 a global counter (see _PyRuntimeState.imports.last_module_index). 307 The entry for that index in this interpreter remains unset until 308 the module is actually imported here. (Py_None is used as 309 a placeholder.) Note that multi-phase init modules always get 310 an index for which there will never be a module set. 311 312 This is initialized lazily in PyState_AddModule(), which is also 313 where modules get added. */ 314 PyObject *modules_by_index; 315 /* importlib module._bootstrap */ 316 PyObject *importlib; 317 /* override for config->use_frozen_modules (for tests) 318 (-1: "off", 1: "on", 0: no override) */ 319 int override_frozen_modules; 320 int override_multi_interp_extensions_check; 321 #ifdef HAVE_DLOPEN 322 int dlopenflags; 323 #endif 324 PyObject *import_func; 325 /* The global import lock. */ 326 _PyRecursiveMutex lock; 327 /* diagnostic info in PyImport_ImportModuleLevelObject() */ 328 struct { 329 int import_level; 330 PyTime_t accumulated; 331 int header; 332 } find_and_load; 333 }; 334 335 336 337 /********** Interpreter state **************/ 338 339 #include "pycore_object_state.h" // struct _py_object_state 340 #include "pycore_crossinterp.h" // _PyXI_state_t 341 342 343 struct _Py_long_state { 344 int max_str_digits; 345 }; 346 347 struct codecs_state { 348 // A list of callable objects used to search for codecs. 349 PyObject *search_path; 350 351 // A dict mapping codec names to codecs returned from a callable in 352 // search_path. 353 PyObject *search_cache; 354 355 // A dict mapping error handling strategies to functions to implement them. 356 PyObject *error_registry; 357 358 #ifdef Py_GIL_DISABLED 359 // Used to safely delete a specific item from search_path. 360 PyMutex search_path_mutex; 361 #endif 362 363 // Whether or not the rest of the state is initialized. 364 int initialized; 365 }; 366 367 // Support for stop-the-world events. This exists in both the PyRuntime struct 368 // for global pauses and in each PyInterpreterState for per-interpreter pauses. 369 struct _stoptheworld_state { 370 PyMutex mutex; // Serializes stop-the-world attempts. 371 372 // NOTE: The below fields are protected by HEAD_LOCK(runtime), not by the 373 // above mutex. 374 bool requested; // Set when a pause is requested. 375 bool world_stopped; // Set when the world is stopped. 376 bool is_global; // Set when contained in PyRuntime struct. 377 378 PyEvent stop_event; // Set when thread_countdown reaches zero. 379 Py_ssize_t thread_countdown; // Number of threads that must pause. 380 381 PyThreadState *requester; // Thread that requested the pause (may be NULL). 382 }; 383 384 /* Tracks some rare events per-interpreter, used by the optimizer to turn on/off 385 specific optimizations. */ 386 typedef struct _rare_events { 387 /* Setting an object's class, obj.__class__ = ... */ 388 uint8_t set_class; 389 /* Setting the bases of a class, cls.__bases__ = ... */ 390 uint8_t set_bases; 391 /* Setting the PEP 523 frame eval function, _PyInterpreterState_SetFrameEvalFunc() */ 392 uint8_t set_eval_frame_func; 393 /* Modifying the builtins, __builtins__.__dict__[var] = ... */ 394 uint8_t builtin_dict; 395 /* Modifying a function, e.g. func.__defaults__ = ..., etc. */ 396 uint8_t func_modification; 397 } _rare_events; 398 399 struct 400 Bigint { 401 struct Bigint *next; 402 int k, maxwds, sign, wds; 403 uint32_t x[1]; 404 }; 405 406 #if defined(Py_USING_MEMORY_DEBUGGER) || _PY_SHORT_FLOAT_REPR == 0 407 408 struct _dtoa_state { 409 int _not_used; 410 }; 411 412 #else // !Py_USING_MEMORY_DEBUGGER && _PY_SHORT_FLOAT_REPR != 0 413 414 /* The size of the Bigint freelist */ 415 #define Bigint_Kmax 7 416 417 /* The size of the cached powers of 5 array */ 418 #define Bigint_Pow5size 8 419 420 #ifndef PRIVATE_MEM 421 #define PRIVATE_MEM 2304 422 #endif 423 #define Bigint_PREALLOC_SIZE \ 424 ((PRIVATE_MEM+sizeof(double)-1)/sizeof(double)) 425 426 struct _dtoa_state { 427 // p5s is an array of powers of 5 of the form: 428 // 5**(2**(i+2)) for 0 <= i < Bigint_Pow5size 429 struct Bigint *p5s[Bigint_Pow5size]; 430 // XXX This should be freed during runtime fini. 431 struct Bigint *freelist[Bigint_Kmax+1]; 432 double preallocated[Bigint_PREALLOC_SIZE]; 433 double *preallocated_next; 434 }; 435 436 #endif // !Py_USING_MEMORY_DEBUGGER 437 438 struct _py_code_state { 439 PyMutex mutex; 440 // Interned constants from code objects. Used by the free-threaded build. 441 struct _Py_hashtable_t *constants; 442 }; 443 444 #define FUNC_VERSION_CACHE_SIZE (1<<12) /* Must be a power of 2 */ 445 446 struct _func_version_cache_item { 447 PyFunctionObject *func; 448 PyObject *code; 449 }; 450 451 struct _py_func_state { 452 #ifdef Py_GIL_DISABLED 453 // Protects next_version 454 PyMutex mutex; 455 #endif 456 457 uint32_t next_version; 458 // Borrowed references to function and code objects whose 459 // func_version % FUNC_VERSION_CACHE_SIZE 460 // once was equal to the index in the table. 461 // They are cleared when the function or code object is deallocated. 462 struct _func_version_cache_item func_version_cache[FUNC_VERSION_CACHE_SIZE]; 463 }; 464 465 #include "pycore_dict_state.h" // struct _Py_dict_state 466 #include "pycore_exceptions.h" // struct _Py_exc_state 467 468 469 /****** type state *********/ 470 471 /* For now we hard-code this to a value for which we are confident 472 all the static builtin types will fit (for all builds). */ 473 #define _Py_MAX_MANAGED_STATIC_BUILTIN_TYPES 200 474 #define _Py_MAX_MANAGED_STATIC_EXT_TYPES 10 475 #define _Py_MAX_MANAGED_STATIC_TYPES \ 476 (_Py_MAX_MANAGED_STATIC_BUILTIN_TYPES + _Py_MAX_MANAGED_STATIC_EXT_TYPES) 477 478 struct _types_runtime_state { 479 /* Used to set PyTypeObject.tp_version_tag for core static types. */ 480 // bpo-42745: next_version_tag remains shared by all interpreters 481 // because of static types. 482 unsigned int next_version_tag; 483 484 struct { 485 struct { 486 PyTypeObject *type; 487 int64_t interp_count; 488 } types[_Py_MAX_MANAGED_STATIC_TYPES]; 489 } managed_static; 490 }; 491 492 493 // Type attribute lookup cache: speed up attribute and method lookups, 494 // see _PyType_Lookup(). 495 struct type_cache_entry { 496 unsigned int version; // initialized from type->tp_version_tag 497 #ifdef Py_GIL_DISABLED 498 _PySeqLock sequence; 499 #endif 500 PyObject *name; // reference to exactly a str or None 501 PyObject *value; // borrowed reference or NULL 502 }; 503 504 #define MCACHE_SIZE_EXP 12 505 506 struct type_cache { 507 struct type_cache_entry hashtable[1 << MCACHE_SIZE_EXP]; 508 }; 509 510 typedef struct { 511 PyTypeObject *type; 512 int isbuiltin; 513 int readying; 514 int ready; 515 // XXX tp_dict can probably be statically allocated, 516 // instead of dynamically and stored on the interpreter. 517 PyObject *tp_dict; 518 PyObject *tp_subclasses; 519 /* We never clean up weakrefs for static builtin types since 520 they will effectively never get triggered. However, there 521 are also some diagnostic uses for the list of weakrefs, 522 so we still keep it. */ 523 PyObject *tp_weaklist; 524 } managed_static_type_state; 525 526 #define TYPE_VERSION_CACHE_SIZE (1<<12) /* Must be a power of 2 */ 527 528 struct types_state { 529 /* Used to set PyTypeObject.tp_version_tag. 530 It starts at _Py_MAX_GLOBAL_TYPE_VERSION_TAG + 1, 531 where all those lower numbers are used for core static types. */ 532 unsigned int next_version_tag; 533 534 struct type_cache type_cache; 535 536 /* Every static builtin type is initialized for each interpreter 537 during its own initialization, including for the main interpreter 538 during global runtime initialization. This is done by calling 539 _PyStaticType_InitBuiltin(). 540 541 The first time a static builtin type is initialized, all the 542 normal PyType_Ready() stuff happens. The only difference from 543 normal is that there are three PyTypeObject fields holding 544 objects which are stored here (on PyInterpreterState) rather 545 than in the corresponding PyTypeObject fields. Those are: 546 tp_dict (cls.__dict__), tp_subclasses (cls.__subclasses__), 547 and tp_weaklist. 548 549 When a subinterpreter is initialized, each static builtin type 550 is still initialized, but only the interpreter-specific portion, 551 namely those three objects. 552 553 Those objects are stored in the PyInterpreterState.types.builtins 554 array, at the index corresponding to each specific static builtin 555 type. That index (a size_t value) is stored in the tp_subclasses 556 field. For static builtin types, we re-purposed the now-unused 557 tp_subclasses to avoid adding another field to PyTypeObject. 558 In all other cases tp_subclasses holds a dict like before. 559 (The field was previously defined as PyObject*, but is now void* 560 to reflect its dual use.) 561 562 The index for each static builtin type isn't statically assigned. 563 Instead it is calculated the first time a type is initialized 564 (by the main interpreter). The index matches the order in which 565 the type was initialized relative to the others. The actual 566 value comes from the current value of num_builtins_initialized, 567 as each type is initialized for the main interpreter. 568 569 num_builtins_initialized is incremented once for each static 570 builtin type. Once initialization is over for a subinterpreter, 571 the value will be the same as for all other interpreters. */ 572 struct { 573 size_t num_initialized; 574 managed_static_type_state initialized[_Py_MAX_MANAGED_STATIC_BUILTIN_TYPES]; 575 } builtins; 576 /* We apply a similar strategy for managed extension modules. */ 577 struct { 578 size_t num_initialized; 579 size_t next_index; 580 managed_static_type_state initialized[_Py_MAX_MANAGED_STATIC_EXT_TYPES]; 581 } for_extensions; 582 PyMutex mutex; 583 584 // Borrowed references to type objects whose 585 // tp_version_tag % TYPE_VERSION_CACHE_SIZE 586 // once was equal to the index in the table. 587 // They are cleared when the type object is deallocated. 588 PyTypeObject *type_version_cache[TYPE_VERSION_CACHE_SIZE]; 589 }; 590 591 struct _warnings_runtime_state { 592 /* Both 'filters' and 'onceregistry' can be set in warnings.py; 593 get_warnings_attr() will reset these variables accordingly. */ 594 PyObject *filters; /* List */ 595 PyObject *once_registry; /* Dict */ 596 PyObject *default_action; /* String */ 597 _PyRecursiveMutex lock; 598 long filters_version; 599 PyObject *context; 600 }; 601 602 struct _Py_mem_interp_free_queue { 603 int has_work; // true if the queue is not empty 604 PyMutex mutex; // protects the queue 605 struct llist_node head; // queue of _mem_work_chunk items 606 }; 607 608 609 /****** Unicode state *********/ 610 611 typedef enum { 612 _Py_ERROR_UNKNOWN=0, 613 _Py_ERROR_STRICT, 614 _Py_ERROR_SURROGATEESCAPE, 615 _Py_ERROR_REPLACE, 616 _Py_ERROR_IGNORE, 617 _Py_ERROR_BACKSLASHREPLACE, 618 _Py_ERROR_SURROGATEPASS, 619 _Py_ERROR_XMLCHARREFREPLACE, 620 _Py_ERROR_OTHER 621 } _Py_error_handler; 622 623 struct _Py_unicode_runtime_ids { 624 PyMutex mutex; 625 // next_index value must be preserved when Py_Initialize()/Py_Finalize() 626 // is called multiple times: see _PyUnicode_FromId() implementation. 627 Py_ssize_t next_index; 628 }; 629 630 struct _Py_unicode_runtime_state { 631 struct _Py_unicode_runtime_ids ids; 632 }; 633 634 /* fs_codec.encoding is initialized to NULL. 635 Later, it is set to a non-NULL string by _PyUnicode_InitEncodings(). */ 636 struct _Py_unicode_fs_codec { 637 char *encoding; // Filesystem encoding (encoded to UTF-8) 638 int utf8; // encoding=="utf-8"? 639 char *errors; // Filesystem errors (encoded to UTF-8) 640 _Py_error_handler error_handler; 641 }; 642 643 struct _Py_unicode_ids { 644 Py_ssize_t size; 645 PyObject **array; 646 }; 647 648 #include "pycore_ucnhash.h" // _PyUnicode_Name_CAPI 649 650 struct _Py_unicode_state { 651 struct _Py_unicode_fs_codec fs_codec; 652 653 _PyUnicode_Name_CAPI *ucnhash_capi; 654 655 // Unicode identifiers (_Py_Identifier): see _PyUnicode_FromId() 656 struct _Py_unicode_ids ids; 657 }; 658 659 // Borrowed references to common callables: 660 struct callable_cache { 661 PyObject *isinstance; 662 PyObject *len; 663 PyObject *list_append; 664 PyObject *object__getattribute__; 665 }; 666 667 /* Length of array of slotdef pointers used to store slots with the 668 same __name__. There should be at most MAX_EQUIV-1 slotdef entries with 669 the same __name__, for any __name__. Since that's a static property, it is 670 appropriate to declare fixed-size arrays for this. */ 671 #define MAX_EQUIV 10 672 673 typedef struct wrapperbase pytype_slotdef; 674 675 676 struct _Py_interp_cached_objects { 677 #ifdef Py_GIL_DISABLED 678 PyMutex interned_mutex; 679 #endif 680 PyObject *interned_strings; 681 682 /* object.__reduce__ */ 683 PyObject *objreduce; 684 PyObject *type_slots_pname; 685 pytype_slotdef *type_slots_ptrs[MAX_EQUIV]; 686 687 /* TypeVar and related types */ 688 PyTypeObject *generic_type; 689 PyTypeObject *typevar_type; 690 PyTypeObject *typevartuple_type; 691 PyTypeObject *paramspec_type; 692 PyTypeObject *paramspecargs_type; 693 PyTypeObject *paramspeckwargs_type; 694 PyTypeObject *constevaluator_type; 695 }; 696 697 struct _Py_interp_static_objects { 698 struct { 699 int _not_used; 700 // hamt_empty is here instead of global because of its weakreflist. 701 _PyGC_Head_UNUSED _hamt_empty_gc_not_used; 702 PyHamtObject hamt_empty; 703 PyBaseExceptionObject last_resort_memory_error; 704 } singletons; 705 }; 706 707 #include "pycore_instruments.h" // PY_MONITORING_TOOL_IDS 708 709 710 #ifdef Py_GIL_DISABLED 711 712 // A min-heap of indices 713 typedef struct _PyIndexHeap { 714 int32_t *values; 715 716 // Number of items stored in values 717 Py_ssize_t size; 718 719 // Maximum number of items that can be stored in values 720 Py_ssize_t capacity; 721 } _PyIndexHeap; 722 723 // An unbounded pool of indices. Indices are allocated starting from 0. They 724 // may be released back to the pool once they are no longer in use. 725 typedef struct _PyIndexPool { 726 PyMutex mutex; 727 728 // Min heap of indices available for allocation 729 _PyIndexHeap free_indices; 730 731 // Next index to allocate if no free indices are available 732 int32_t next_index; 733 734 // Generation counter incremented on thread creation/destruction 735 // Used for TLBC cache invalidation in remote debugging 736 uint32_t tlbc_generation; 737 } _PyIndexPool; 738 739 typedef union _Py_unique_id_entry { 740 // Points to the next free type id, when part of the freelist 741 union _Py_unique_id_entry *next; 742 743 // Stores the object when the id is assigned 744 PyObject *obj; 745 } _Py_unique_id_entry; 746 747 struct _Py_unique_id_pool { 748 PyMutex mutex; 749 750 // combined table of object with allocated unique ids and unallocated ids. 751 _Py_unique_id_entry *table; 752 753 // Next entry to allocate inside 'table' or NULL 754 _Py_unique_id_entry *freelist; 755 756 // size of 'table' 757 Py_ssize_t size; 758 }; 759 760 #endif 761 762 763 /* PyInterpreterState holds the global state for one of the runtime's 764 interpreters. Typically the initial (main) interpreter is the only one. 765 766 The PyInterpreterState typedef is in Include/pytypedefs.h. 767 */ 768 struct _is { 769 770 /* This struct contains the eval_breaker, 771 * which is by far the hottest field in this struct 772 * and should be placed at the beginning. */ 773 struct _ceval_state ceval; 774 775 // unused, kept for ABI compatibility 776 void *_malloced; 777 778 PyInterpreterState *next; 779 780 int64_t id; 781 Py_ssize_t id_refcount; 782 int requires_idref; 783 784 long _whence; 785 786 /* Has been initialized to a safe state. 787 788 In order to be effective, this must be set to 0 during or right 789 after allocation. */ 790 int _initialized; 791 /* Has been fully initialized via pylifecycle.c. */ 792 int _ready; 793 int finalizing; 794 795 uintptr_t last_restart_version; 796 struct pythreads { 797 uint64_t next_unique_id; 798 /* The linked list of threads, newest first. */ 799 PyThreadState *head; 800 _PyThreadStateImpl *preallocated; 801 /* The thread currently executing in the __main__ module, if any. */ 802 PyThreadState *main; 803 /* Used in Modules/_threadmodule.c. */ 804 Py_ssize_t count; 805 /* Support for runtime thread stack size tuning. 806 A value of 0 means using the platform's default stack size 807 or the size specified by the THREAD_STACK_SIZE macro. */ 808 /* Used in Python/thread.c. */ 809 size_t stacksize; 810 } threads; 811 812 /* Reference to the _PyRuntime global variable. This field exists 813 to not have to pass runtime in addition to tstate to a function. 814 Get runtime from tstate: tstate->interp->runtime. */ 815 _PyRuntimeState *runtime; 816 817 /* Set by Py_EndInterpreter(). 818 819 Use _PyInterpreterState_GetFinalizing() 820 and _PyInterpreterState_SetFinalizing() 821 to access it, don't access it directly. */ 822 PyThreadState* _finalizing; 823 /* The ID of the OS thread in which we are finalizing. */ 824 unsigned long _finalizing_id; 825 826 struct _gc_runtime_state gc; 827 828 /* The following fields are here to avoid allocation during init. 829 The data is exposed through PyInterpreterState pointer fields. 830 These fields should not be accessed directly outside of init. 831 832 All other PyInterpreterState pointer fields are populated when 833 needed and default to NULL. 834 835 For now there are some exceptions to that rule, which require 836 allocation during init. These will be addressed on a case-by-case 837 basis. Also see _PyRuntimeState regarding the various mutex fields. 838 */ 839 840 // Dictionary of the sys module 841 PyObject *sysdict; 842 843 // Dictionary of the builtins module 844 PyObject *builtins; 845 846 struct _import_state imports; 847 848 /* The per-interpreter GIL, which might not be used. */ 849 struct _gil_runtime_state _gil; 850 851 uint64_t _code_object_generation; 852 853 /* ---------- IMPORTANT --------------------------- 854 The fields above this line are declared as early as 855 possible to facilitate out-of-process observability 856 tools. */ 857 858 struct codecs_state codecs; 859 860 PyConfig config; 861 unsigned long feature_flags; 862 863 PyObject *dict; /* Stores per-interpreter state */ 864 865 PyObject *sysdict_copy; 866 PyObject *builtins_copy; 867 // Initialized to _PyEval_EvalFrameDefault(). 868 _PyFrameEvalFunction eval_frame; 869 870 PyFunction_WatchCallback func_watchers[FUNC_MAX_WATCHERS]; 871 // One bit is set for each non-NULL entry in func_watchers 872 uint8_t active_func_watchers; 873 874 Py_ssize_t co_extra_user_count; 875 freefunc co_extra_freefuncs[MAX_CO_EXTRA_USERS]; 876 877 /* cross-interpreter data and utils */ 878 _PyXI_state_t xi; 879 880 #ifdef HAVE_FORK 881 PyObject *before_forkers; 882 PyObject *after_forkers_parent; 883 PyObject *after_forkers_child; 884 #endif 885 886 struct _warnings_runtime_state warnings; 887 struct atexit_state atexit; 888 struct _stoptheworld_state stoptheworld; 889 struct _qsbr_shared qsbr; 890 891 #if defined(Py_GIL_DISABLED) 892 struct _mimalloc_interp_state mimalloc; 893 struct _brc_state brc; // biased reference counting state 894 struct _Py_unique_id_pool unique_ids; // object ids for per-thread refcounts 895 PyMutex weakref_locks[NUM_WEAKREF_LIST_LOCKS]; 896 _PyIndexPool tlbc_indices; 897 #endif 898 // Per-interpreter list of tasks, any lingering tasks from thread 899 // states gets added here and removed from the corresponding 900 // thread state's list. 901 struct llist_node asyncio_tasks_head; 902 // `asyncio_tasks_lock` is used when tasks are moved 903 // from thread's list to interpreter's list. 904 PyMutex asyncio_tasks_lock; 905 906 // Per-interpreter state for the obmalloc allocator. For the main 907 // interpreter and for all interpreters that don't have their 908 // own obmalloc state, this points to the static structure in 909 // obmalloc.c obmalloc_state_main. For other interpreters, it is 910 // heap allocated by _PyMem_init_obmalloc() and freed when the 911 // interpreter structure is freed. In the case of a heap allocated 912 // obmalloc state, it is not safe to hold on to or use memory after 913 // the interpreter is freed. The obmalloc state corresponding to 914 // that allocated memory is gone. See free_obmalloc_arenas() for 915 // more comments. 916 struct _obmalloc_state *obmalloc; 917 918 PyObject *audit_hooks; 919 PyType_WatchCallback type_watchers[TYPE_MAX_WATCHERS]; 920 PyCode_WatchCallback code_watchers[CODE_MAX_WATCHERS]; 921 PyContext_WatchCallback context_watchers[CONTEXT_MAX_WATCHERS]; 922 // One bit is set for each non-NULL entry in code_watchers 923 uint8_t active_code_watchers; 924 uint8_t active_context_watchers; 925 926 struct _py_object_state object_state; 927 struct _Py_unicode_state unicode; 928 struct _Py_long_state long_state; 929 struct _dtoa_state dtoa; 930 struct _py_func_state func_state; 931 struct _py_code_state code_state; 932 933 struct _Py_dict_state dict_state; 934 struct _Py_exc_state exc_state; 935 struct _Py_mem_interp_free_queue mem_free_queue; 936 937 struct ast_state ast; 938 struct types_state types; 939 struct callable_cache callable_cache; 940 PyObject *common_consts[NUM_COMMON_CONSTANTS]; 941 bool jit; 942 struct _PyExecutorObject *executor_list_head; 943 struct _PyExecutorObject *executor_deletion_list_head; 944 int executor_deletion_list_remaining_capacity; 945 size_t trace_run_counter; 946 _rare_events rare_events; 947 PyDict_WatchCallback builtins_dict_watcher; 948 949 _Py_GlobalMonitors monitors; 950 _PyOnceFlag sys_profile_once_flag; 951 _PyOnceFlag sys_trace_once_flag; 952 Py_ssize_t sys_profiling_threads; /* Count of threads with c_profilefunc set */ 953 Py_ssize_t sys_tracing_threads; /* Count of threads with c_tracefunc set */ 954 PyObject *monitoring_callables[PY_MONITORING_TOOL_IDS][_PY_MONITORING_EVENTS]; 955 PyObject *monitoring_tool_names[PY_MONITORING_TOOL_IDS]; 956 uintptr_t monitoring_tool_versions[PY_MONITORING_TOOL_IDS]; 957 958 struct _Py_interp_cached_objects cached_objects; 959 struct _Py_interp_static_objects static_objects; 960 961 Py_ssize_t _interactive_src_count; 962 963 #if !defined(Py_GIL_DISABLED) && defined(Py_STACKREF_DEBUG) 964 uint64_t next_stackref; 965 _Py_hashtable_t *open_stackrefs_table; 966 # ifdef Py_STACKREF_CLOSE_DEBUG 967 _Py_hashtable_t *closed_stackrefs_table; 968 # endif 969 #endif 970 971 /* the initial PyInterpreterState.threads.head */ 972 _PyThreadStateImpl _initial_thread; 973 // _initial_thread should be the last field of PyInterpreterState. 974 // See https://github.com/python/cpython/issues/127117. 975 }; 976 977 978 #ifdef __cplusplus 979 } 980 #endif 981 #endif /* Py_INTERNAL_INTERP_STRUCTS_H */