1 #ifndef Py_INTERNAL_TYPEOBJECT_H
2 #define Py_INTERNAL_TYPEOBJECT_H
3 #ifdef __cplusplus
4 extern "C" {
5 #endif
6
7 #ifndef Py_BUILD_CORE
8 # error "this header requires Py_BUILD_CORE define"
9 #endif
10
11 #include "pycore_moduleobject.h" // PyModuleObject
12 #include "pycore_lock.h" // PyMutex
13
14
15 /* state */
16
17 #define _Py_TYPE_BASE_VERSION_TAG (2<<16)
18 #define _Py_MAX_GLOBAL_TYPE_VERSION_TAG (_Py_TYPE_BASE_VERSION_TAG - 1)
19
20 /* For now we hard-code this to a value for which we are confident
21 all the static builtin types will fit (for all builds). */
22 #define _Py_MAX_MANAGED_STATIC_BUILTIN_TYPES 200
23 #define _Py_MAX_MANAGED_STATIC_EXT_TYPES 10
24 #define _Py_MAX_MANAGED_STATIC_TYPES \
25 (_Py_MAX_MANAGED_STATIC_BUILTIN_TYPES + _Py_MAX_MANAGED_STATIC_EXT_TYPES)
26
27 struct _types_runtime_state {
28 /* Used to set PyTypeObject.tp_version_tag for core static types. */
29 // bpo-42745: next_version_tag remains shared by all interpreters
30 // because of static types.
31 unsigned int next_version_tag;
32
33 struct {
34 struct {
35 PyTypeObject *type;
36 int64_t interp_count;
37 } types[_Py_MAX_MANAGED_STATIC_TYPES];
38 } managed_static;
39 };
40
41
42 // Type attribute lookup cache: speed up attribute and method lookups,
43 // see _PyType_Lookup().
44 struct type_cache_entry {
45 unsigned int version; // initialized from type->tp_version_tag
46 #ifdef Py_GIL_DISABLED
47 _PySeqLock sequence;
48 #endif
49 PyObject *name; // reference to exactly a str or None
50 PyObject *value; // borrowed reference or NULL
51 };
52
53 #define MCACHE_SIZE_EXP 12
54
55 struct type_cache {
56 struct type_cache_entry hashtable[1 << MCACHE_SIZE_EXP];
57 };
58
59 typedef struct {
60 PyTypeObject *type;
61 int isbuiltin;
62 int readying;
63 int ready;
64 // XXX tp_dict can probably be statically allocated,
65 // instead of dynamically and stored on the interpreter.
66 PyObject *tp_dict;
67 PyObject *tp_subclasses;
68 /* We never clean up weakrefs for static builtin types since
69 they will effectively never get triggered. However, there
70 are also some diagnostic uses for the list of weakrefs,
71 so we still keep it. */
72 PyObject *tp_weaklist;
73 } managed_static_type_state;
74
75 struct types_state {
76 /* Used to set PyTypeObject.tp_version_tag.
77 It starts at _Py_MAX_GLOBAL_TYPE_VERSION_TAG + 1,
78 where all those lower numbers are used for core static types. */
79 unsigned int next_version_tag;
80
81 struct type_cache type_cache;
82
83 /* Every static builtin type is initialized for each interpreter
84 during its own initialization, including for the main interpreter
85 during global runtime initialization. This is done by calling
86 _PyStaticType_InitBuiltin().
87
88 The first time a static builtin type is initialized, all the
89 normal PyType_Ready() stuff happens. The only difference from
90 normal is that there are three PyTypeObject fields holding
91 objects which are stored here (on PyInterpreterState) rather
92 than in the corresponding PyTypeObject fields. Those are:
93 tp_dict (cls.__dict__), tp_subclasses (cls.__subclasses__),
94 and tp_weaklist.
95
96 When a subinterpreter is initialized, each static builtin type
97 is still initialized, but only the interpreter-specific portion,
98 namely those three objects.
99
100 Those objects are stored in the PyInterpreterState.types.builtins
101 array, at the index corresponding to each specific static builtin
102 type. That index (a size_t value) is stored in the tp_subclasses
103 field. For static builtin types, we re-purposed the now-unused
104 tp_subclasses to avoid adding another field to PyTypeObject.
105 In all other cases tp_subclasses holds a dict like before.
106 (The field was previously defined as PyObject*, but is now void*
107 to reflect its dual use.)
108
109 The index for each static builtin type isn't statically assigned.
110 Instead it is calculated the first time a type is initialized
111 (by the main interpreter). The index matches the order in which
112 the type was initialized relative to the others. The actual
113 value comes from the current value of num_builtins_initialized,
114 as each type is initialized for the main interpreter.
115
116 num_builtins_initialized is incremented once for each static
117 builtin type. Once initialization is over for a subinterpreter,
118 the value will be the same as for all other interpreters. */
119 struct {
120 size_t num_initialized;
121 managed_static_type_state initialized[_Py_MAX_MANAGED_STATIC_BUILTIN_TYPES];
122 } builtins;
123 /* We apply a similar strategy for managed extension modules. */
124 struct {
125 size_t num_initialized;
126 size_t next_index;
127 managed_static_type_state initialized[_Py_MAX_MANAGED_STATIC_EXT_TYPES];
128 } for_extensions;
129 PyMutex mutex;
130 };
131
132
133 /* runtime lifecycle */
134
135 extern PyStatus _PyTypes_InitTypes(PyInterpreterState *);
136 extern void _PyTypes_FiniTypes(PyInterpreterState *);
137 extern void _PyTypes_FiniExtTypes(PyInterpreterState *interp);
138 extern void _PyTypes_Fini(PyInterpreterState *);
139 extern void _PyTypes_AfterFork(void);
140
141 /* other API */
142
143 /* Length of array of slotdef pointers used to store slots with the
144 same __name__. There should be at most MAX_EQUIV-1 slotdef entries with
145 the same __name__, for any __name__. Since that's a static property, it is
146 appropriate to declare fixed-size arrays for this. */
147 #define MAX_EQUIV 10
148
149 typedef struct wrapperbase pytype_slotdef;
150
151
152 static inline PyObject **
_PyStaticType_GET_WEAKREFS_LISTPTR(managed_static_type_state * state)153 _PyStaticType_GET_WEAKREFS_LISTPTR(managed_static_type_state *state)
154 {
155 assert(state != NULL);
156 return &state->tp_weaklist;
157 }
158
159 extern int _PyStaticType_InitBuiltin(
160 PyInterpreterState *interp,
161 PyTypeObject *type);
162 extern void _PyStaticType_FiniBuiltin(
163 PyInterpreterState *interp,
164 PyTypeObject *type);
165 extern void _PyStaticType_ClearWeakRefs(
166 PyInterpreterState *interp,
167 PyTypeObject *type);
168 extern managed_static_type_state * _PyStaticType_GetState(
169 PyInterpreterState *interp,
170 PyTypeObject *type);
171
172 // Export for '_datetime' shared extension.
173 PyAPI_FUNC(int) _PyStaticType_InitForExtension(
174 PyInterpreterState *interp,
175 PyTypeObject *self);
176
177
178 /* Like PyType_GetModuleState, but skips verification
179 * that type is a heap type with an associated module */
180 static inline void *
_PyType_GetModuleState(PyTypeObject * type)181 _PyType_GetModuleState(PyTypeObject *type)
182 {
183 assert(PyType_Check(type));
184 assert(type->tp_flags & Py_TPFLAGS_HEAPTYPE);
185 PyHeapTypeObject *et = (PyHeapTypeObject *)type;
186 assert(et->ht_module);
187 PyModuleObject *mod = (PyModuleObject *)(et->ht_module);
188 assert(mod != NULL);
189 return mod->md_state;
190 }
191
192
193 // Export for 'math' shared extension, used via _PyType_IsReady() static inline
194 // function
195 PyAPI_FUNC(PyObject *) _PyType_GetDict(PyTypeObject *);
196
197 extern PyObject * _PyType_GetBases(PyTypeObject *type);
198 extern PyObject * _PyType_GetMRO(PyTypeObject *type);
199 extern PyObject* _PyType_GetSubclasses(PyTypeObject *);
200 extern int _PyType_HasSubclasses(PyTypeObject *);
201 PyAPI_FUNC(PyObject *) _PyType_GetModuleByDef2(PyTypeObject *, PyTypeObject *, PyModuleDef *);
202
203 // PyType_Ready() must be called if _PyType_IsReady() is false.
204 // See also the Py_TPFLAGS_READY flag.
205 static inline int
_PyType_IsReady(PyTypeObject * type)206 _PyType_IsReady(PyTypeObject *type)
207 {
208 return _PyType_GetDict(type) != NULL;
209 }
210
211 extern PyObject* _Py_type_getattro_impl(PyTypeObject *type, PyObject *name,
212 int *suppress_missing_attribute);
213 extern PyObject* _Py_type_getattro(PyObject *type, PyObject *name);
214
215 extern PyObject* _Py_BaseObject_RichCompare(PyObject* self, PyObject* other, int op);
216
217 extern PyObject* _Py_slot_tp_getattro(PyObject *self, PyObject *name);
218 extern PyObject* _Py_slot_tp_getattr_hook(PyObject *self, PyObject *name);
219
220 extern PyTypeObject _PyBufferWrapper_Type;
221
222 PyAPI_FUNC(PyObject*) _PySuper_Lookup(PyTypeObject *su_type, PyObject *su_obj,
223 PyObject *name, int *meth_found);
224
225 extern PyObject* _PyType_GetFullyQualifiedName(PyTypeObject *type, char sep);
226
227 // Perform the following operation, in a thread-safe way when required by the
228 // build mode.
229 //
230 // self->tp_flags = (self->tp_flags & ~mask) | flags;
231 extern void _PyType_SetFlags(PyTypeObject *self, unsigned long mask,
232 unsigned long flags);
233 extern int _PyType_AddMethod(PyTypeObject *, PyMethodDef *);
234
235 // Like _PyType_SetFlags(), but apply the operation to self and any of its
236 // subclasses without Py_TPFLAGS_IMMUTABLETYPE set.
237 extern void _PyType_SetFlagsRecursive(PyTypeObject *self, unsigned long mask,
238 unsigned long flags);
239
240
241 #ifdef __cplusplus
242 }
243 #endif
244 #endif /* !Py_INTERNAL_TYPEOBJECT_H */
245