/src/cpython/Objects/object.c
Line | Count | Source |
1 | | |
2 | | /* Generic object operations; and implementation of None */ |
3 | | |
4 | | #include "Python.h" |
5 | | #include "pycore_brc.h" // _Py_brc_queue_object() |
6 | | #include "pycore_call.h" // _PyObject_CallNoArgs() |
7 | | #include "pycore_ceval.h" // _Py_EnterRecursiveCallTstate() |
8 | | #include "pycore_context.h" // _PyContextTokenMissing_Type |
9 | | #include "pycore_critical_section.h" // Py_BEGIN_CRITICAL_SECTION |
10 | | #include "pycore_descrobject.h" // _PyMethodWrapper_Type |
11 | | #include "pycore_dict.h" // _PyObject_MaterializeManagedDict() |
12 | | #include "pycore_floatobject.h" // _PyFloat_DebugMallocStats() |
13 | | #include "pycore_function.h" // _PyClassMethod_GetFunc() |
14 | | #include "pycore_freelist.h" // _PyObject_ClearFreeLists() |
15 | | #include "pycore_genobject.h" // _PyAsyncGenAThrow_Type |
16 | | #include "pycore_hamt.h" // _PyHamtItems_Type |
17 | | #include "pycore_initconfig.h" // _PyStatus_OK() |
18 | | #include "pycore_instruction_sequence.h" // _PyInstructionSequence_Type |
19 | | #include "pycore_interpframe.h" // _PyFrame_Stackbase() |
20 | | #include "pycore_interpolation.h" // _PyInterpolation_Type |
21 | | #include "pycore_lazyimportobject.h" // PyLazyImport_Type |
22 | | #include "pycore_list.h" // _PyList_DebugMallocStats() |
23 | | #include "pycore_long.h" // _PyLong_GetZero() |
24 | | #include "pycore_memoryobject.h" // _PyManagedBuffer_Type |
25 | | #include "pycore_namespace.h" // _PyNamespace_Type |
26 | | #include "pycore_object.h" // export _Py_SwappedOp |
27 | | #include "pycore_optimizer.h" // _PyUOpExecutor_Type |
28 | | #include "pycore_pyerrors.h" // _PyErr_Occurred() |
29 | | #include "pycore_pymem.h" // _PyMem_IsPtrFreed() |
30 | | #include "pycore_pystate.h" // _PyThreadState_GET() |
31 | | #include "pycore_symtable.h" // PySTEntry_Type |
32 | | #include "pycore_template.h" // _PyTemplate_Type _PyTemplateIter_Type |
33 | | #include "pycore_tuple.h" // _PyTuple_DebugMallocStats() |
34 | | #include "pycore_typeobject.h" // _PyBufferWrapper_Type |
35 | | #include "pycore_typevarobject.h" // _PyTypeAlias_Type |
36 | | #include "pycore_stackref.h" // PyStackRef_FromPyObjectSteal |
37 | | #include "pycore_unionobject.h" // _PyUnion_Type |
38 | | |
39 | | |
40 | | #ifdef Py_LIMITED_API |
41 | | // Prevent recursive call _Py_IncRef() <=> Py_INCREF() |
42 | | # error "Py_LIMITED_API macro must not be defined" |
43 | | #endif |
44 | | |
45 | | /* Defined in tracemalloc.c */ |
46 | | extern void _PyMem_DumpTraceback(int fd, const void *ptr); |
47 | | |
48 | | |
49 | | int |
50 | | _PyObject_CheckConsistency(PyObject *op, int check_content) |
51 | 0 | { |
52 | 0 | #define CHECK(expr) \ |
53 | 0 | do { if (!(expr)) { _PyObject_ASSERT_FAILED_MSG(op, Py_STRINGIFY(expr)); } } while (0) |
54 | |
|
55 | 0 | CHECK(!_PyObject_IsFreed(op)); |
56 | 0 | CHECK(Py_REFCNT(op) >= 1); |
57 | | |
58 | 0 | _PyType_CheckConsistency(Py_TYPE(op)); |
59 | |
|
60 | 0 | if (PyUnicode_Check(op)) { |
61 | 0 | _PyUnicode_CheckConsistency(op, check_content); |
62 | 0 | } |
63 | 0 | else if (PyAnyDict_Check(op)) { |
64 | 0 | _PyDict_CheckConsistency(op, check_content); |
65 | 0 | } |
66 | 0 | return 1; |
67 | |
|
68 | 0 | #undef CHECK |
69 | 0 | } |
70 | | |
71 | | |
72 | | #ifdef Py_REF_DEBUG |
73 | | /* We keep the legacy symbol around for backward compatibility. */ |
74 | | Py_ssize_t _Py_RefTotal; |
75 | | |
76 | | static inline Py_ssize_t |
77 | | get_legacy_reftotal(void) |
78 | | { |
79 | | return _Py_RefTotal; |
80 | | } |
81 | | #endif |
82 | | |
83 | | #ifdef Py_REF_DEBUG |
84 | | |
85 | | # define REFTOTAL(interp) \ |
86 | | interp->object_state.reftotal |
87 | | |
88 | | static inline void |
89 | | reftotal_add(PyThreadState *tstate, Py_ssize_t n) |
90 | | { |
91 | | #ifdef Py_GIL_DISABLED |
92 | | _PyThreadStateImpl *tstate_impl = (_PyThreadStateImpl *)tstate; |
93 | | // relaxed store to avoid data race with read in get_reftotal() |
94 | | Py_ssize_t reftotal = tstate_impl->reftotal + n; |
95 | | _Py_atomic_store_ssize_relaxed(&tstate_impl->reftotal, reftotal); |
96 | | #else |
97 | | REFTOTAL(tstate->interp) += n; |
98 | | #endif |
99 | | } |
100 | | |
101 | | static inline Py_ssize_t get_global_reftotal(_PyRuntimeState *); |
102 | | |
103 | | /* We preserve the number of refs leaked during runtime finalization, |
104 | | so they can be reported if the runtime is initialized again. */ |
105 | | // XXX We don't lose any information by dropping this, |
106 | | // so we should consider doing so. |
107 | | static Py_ssize_t last_final_reftotal = 0; |
108 | | |
109 | | void |
110 | | _Py_FinalizeRefTotal(_PyRuntimeState *runtime) |
111 | | { |
112 | | last_final_reftotal = get_global_reftotal(runtime); |
113 | | runtime->object_state.interpreter_leaks = 0; |
114 | | } |
115 | | |
116 | | void |
117 | | _PyInterpreterState_FinalizeRefTotal(PyInterpreterState *interp) |
118 | | { |
119 | | interp->runtime->object_state.interpreter_leaks += REFTOTAL(interp); |
120 | | REFTOTAL(interp) = 0; |
121 | | } |
122 | | |
123 | | static inline Py_ssize_t |
124 | | get_reftotal(PyInterpreterState *interp) |
125 | | { |
126 | | /* For a single interpreter, we ignore the legacy _Py_RefTotal, |
127 | | since we can't determine which interpreter updated it. */ |
128 | | Py_ssize_t total = REFTOTAL(interp); |
129 | | #ifdef Py_GIL_DISABLED |
130 | | _Py_FOR_EACH_TSTATE_UNLOCKED(interp, p) { |
131 | | /* This may race with other threads modifications to their reftotal */ |
132 | | _PyThreadStateImpl *tstate_impl = (_PyThreadStateImpl *)p; |
133 | | total += _Py_atomic_load_ssize_relaxed(&tstate_impl->reftotal); |
134 | | } |
135 | | #endif |
136 | | return total; |
137 | | } |
138 | | |
139 | | static inline Py_ssize_t |
140 | | get_global_reftotal(_PyRuntimeState *runtime) |
141 | | { |
142 | | Py_ssize_t total = 0; |
143 | | |
144 | | /* Add up the total from each interpreter. */ |
145 | | HEAD_LOCK(&_PyRuntime); |
146 | | PyInterpreterState *interp = PyInterpreterState_Head(); |
147 | | for (; interp != NULL; interp = PyInterpreterState_Next(interp)) { |
148 | | total += get_reftotal(interp); |
149 | | } |
150 | | HEAD_UNLOCK(&_PyRuntime); |
151 | | |
152 | | /* Add in the updated value from the legacy _Py_RefTotal. */ |
153 | | total += get_legacy_reftotal(); |
154 | | total += last_final_reftotal; |
155 | | total += runtime->object_state.interpreter_leaks; |
156 | | |
157 | | return total; |
158 | | } |
159 | | |
160 | | #undef REFTOTAL |
161 | | |
162 | | void |
163 | | _PyDebug_PrintTotalRefs(void) { |
164 | | _PyRuntimeState *runtime = &_PyRuntime; |
165 | | fprintf(stderr, |
166 | | "[%zd refs, %zd blocks]\n", |
167 | | get_global_reftotal(runtime), _Py_GetGlobalAllocatedBlocks()); |
168 | | /* It may be helpful to also print the "legacy" reftotal separately. |
169 | | Likewise for the total for each interpreter. */ |
170 | | } |
171 | | #endif /* Py_REF_DEBUG */ |
172 | | |
173 | | /* Object allocation routines used by NEWOBJ and NEWVAROBJ macros. |
174 | | These are used by the individual routines for object creation. |
175 | | Do not call them otherwise, they do not initialize the object! */ |
176 | | |
177 | | #ifdef Py_TRACE_REFS |
178 | | |
179 | | #define REFCHAIN(interp) interp->object_state.refchain |
180 | | #define REFCHAIN_VALUE ((void*)(uintptr_t)1) |
181 | | |
182 | | static inline int |
183 | | has_own_refchain(PyInterpreterState *interp) |
184 | | { |
185 | | if (interp->feature_flags & Py_RTFLAGS_USE_MAIN_OBMALLOC) { |
186 | | return (_Py_IsMainInterpreter(interp) |
187 | | || _PyInterpreterState_Main() == NULL); |
188 | | } |
189 | | return 1; |
190 | | } |
191 | | |
192 | | static int |
193 | | refchain_init(PyInterpreterState *interp) |
194 | | { |
195 | | if (!has_own_refchain(interp)) { |
196 | | // Legacy subinterpreters share a refchain with the main interpreter. |
197 | | REFCHAIN(interp) = REFCHAIN(_PyInterpreterState_Main()); |
198 | | return 0; |
199 | | } |
200 | | _Py_hashtable_allocator_t alloc = { |
201 | | // Don't use default PyMem_Malloc() and PyMem_Free() which |
202 | | // require the caller to hold the GIL. |
203 | | .malloc = PyMem_RawMalloc, |
204 | | .free = PyMem_RawFree, |
205 | | }; |
206 | | REFCHAIN(interp) = _Py_hashtable_new_full( |
207 | | _Py_hashtable_hash_ptr, _Py_hashtable_compare_direct, |
208 | | NULL, NULL, &alloc); |
209 | | if (REFCHAIN(interp) == NULL) { |
210 | | return -1; |
211 | | } |
212 | | return 0; |
213 | | } |
214 | | |
215 | | static void |
216 | | refchain_fini(PyInterpreterState *interp) |
217 | | { |
218 | | if (has_own_refchain(interp) && REFCHAIN(interp) != NULL) { |
219 | | _Py_hashtable_destroy(REFCHAIN(interp)); |
220 | | } |
221 | | REFCHAIN(interp) = NULL; |
222 | | } |
223 | | |
224 | | bool |
225 | | _PyRefchain_IsTraced(PyInterpreterState *interp, PyObject *obj) |
226 | | { |
227 | | return (_Py_hashtable_get(REFCHAIN(interp), obj) == REFCHAIN_VALUE); |
228 | | } |
229 | | |
230 | | |
231 | | static void |
232 | | _PyRefchain_Trace(PyInterpreterState *interp, PyObject *obj) |
233 | | { |
234 | | if (_Py_hashtable_set(REFCHAIN(interp), obj, REFCHAIN_VALUE) < 0) { |
235 | | // Use a fatal error because _Py_NewReference() cannot report |
236 | | // the error to the caller. |
237 | | Py_FatalError("_Py_hashtable_set() memory allocation failed"); |
238 | | } |
239 | | } |
240 | | |
241 | | |
242 | | static void |
243 | | _PyRefchain_Remove(PyInterpreterState *interp, PyObject *obj) |
244 | | { |
245 | | void *value = _Py_hashtable_steal(REFCHAIN(interp), obj); |
246 | | #ifndef NDEBUG |
247 | | assert(value == REFCHAIN_VALUE); |
248 | | #else |
249 | | (void)value; |
250 | | #endif |
251 | | } |
252 | | |
253 | | |
254 | | /* Add an object to the refchain hash table. |
255 | | * |
256 | | * Note that objects are normally added to the list by PyObject_Init() |
257 | | * indirectly. Not all objects are initialized that way, though; exceptions |
258 | | * include statically allocated type objects, and statically allocated |
259 | | * singletons (like Py_True and Py_None). */ |
260 | | void |
261 | | _Py_AddToAllObjects(PyObject *op) |
262 | | { |
263 | | PyInterpreterState *interp = _PyInterpreterState_GET(); |
264 | | if (!_PyRefchain_IsTraced(interp, op)) { |
265 | | _PyRefchain_Trace(interp, op); |
266 | | } |
267 | | } |
268 | | #endif /* Py_TRACE_REFS */ |
269 | | |
270 | | #ifdef Py_REF_DEBUG |
271 | | /* Log a fatal error; doesn't return. */ |
272 | | void |
273 | | _Py_NegativeRefcount(const char *filename, int lineno, PyObject *op) |
274 | | { |
275 | | _PyObject_AssertFailed(op, NULL, "object has negative ref count", |
276 | | filename, lineno, __func__); |
277 | | } |
278 | | |
279 | | /* This is used strictly by Py_INCREF(). */ |
280 | | void |
281 | | _Py_INCREF_IncRefTotal(void) |
282 | | { |
283 | | reftotal_add(_PyThreadState_GET(), 1); |
284 | | } |
285 | | |
286 | | /* This is used strictly by Py_DECREF(). */ |
287 | | void |
288 | | _Py_DECREF_DecRefTotal(void) |
289 | | { |
290 | | reftotal_add(_PyThreadState_GET(), -1); |
291 | | } |
292 | | |
293 | | void |
294 | | _Py_IncRefTotal(PyThreadState *tstate) |
295 | | { |
296 | | reftotal_add(tstate, 1); |
297 | | } |
298 | | |
299 | | void |
300 | | _Py_DecRefTotal(PyThreadState *tstate) |
301 | | { |
302 | | reftotal_add(tstate, -1); |
303 | | } |
304 | | |
305 | | void |
306 | | _Py_AddRefTotal(PyThreadState *tstate, Py_ssize_t n) |
307 | | { |
308 | | reftotal_add(tstate, n); |
309 | | } |
310 | | |
311 | | /* This includes the legacy total |
312 | | and any carried over from the last runtime init/fini cycle. */ |
313 | | Py_ssize_t |
314 | | _Py_GetGlobalRefTotal(void) |
315 | | { |
316 | | return get_global_reftotal(&_PyRuntime); |
317 | | } |
318 | | |
319 | | Py_ssize_t |
320 | | _Py_GetLegacyRefTotal(void) |
321 | | { |
322 | | return get_legacy_reftotal(); |
323 | | } |
324 | | |
325 | | Py_ssize_t |
326 | | _PyInterpreterState_GetRefTotal(PyInterpreterState *interp) |
327 | | { |
328 | | HEAD_LOCK(&_PyRuntime); |
329 | | Py_ssize_t total = get_reftotal(interp); |
330 | | HEAD_UNLOCK(&_PyRuntime); |
331 | | return total; |
332 | | } |
333 | | |
334 | | #endif /* Py_REF_DEBUG */ |
335 | | |
336 | | void |
337 | | Py_IncRef(PyObject *o) |
338 | 0 | { |
339 | 0 | Py_XINCREF(o); |
340 | 0 | } |
341 | | |
342 | | void |
343 | | Py_DecRef(PyObject *o) |
344 | 0 | { |
345 | 0 | Py_XDECREF(o); |
346 | 0 | } |
347 | | |
348 | | void |
349 | | _Py_IncRef(PyObject *o) |
350 | 0 | { |
351 | 0 | Py_INCREF(o); |
352 | 0 | } |
353 | | |
354 | | void |
355 | | _Py_DecRef(PyObject *o) |
356 | 9.90k | { |
357 | 9.90k | Py_DECREF(o); |
358 | 9.90k | } |
359 | | |
360 | | #ifdef Py_GIL_DISABLED |
361 | | # ifdef Py_REF_DEBUG |
362 | | static int |
363 | | is_dead(PyObject *o) |
364 | | { |
365 | | # if SIZEOF_SIZE_T == 8 |
366 | | return (uintptr_t)o->ob_type == 0xDDDDDDDDDDDDDDDD; |
367 | | # else |
368 | | return (uintptr_t)o->ob_type == 0xDDDDDDDD; |
369 | | # endif |
370 | | } |
371 | | # endif |
372 | | |
373 | | // Decrement the shared reference count of an object. Return 1 if the object |
374 | | // is dead and should be deallocated, 0 otherwise. |
375 | | static int |
376 | | _Py_DecRefSharedIsDead(PyObject *o, const char *filename, int lineno) |
377 | | { |
378 | | // Should we queue the object for the owning thread to merge? |
379 | | int should_queue; |
380 | | |
381 | | Py_ssize_t new_shared; |
382 | | Py_ssize_t shared = _Py_atomic_load_ssize_relaxed(&o->ob_ref_shared); |
383 | | do { |
384 | | should_queue = (shared == 0 || shared == _Py_REF_MAYBE_WEAKREF); |
385 | | |
386 | | if (should_queue) { |
387 | | // If the object had refcount zero, not queued, and not merged, |
388 | | // then we enqueue the object to be merged by the owning thread. |
389 | | // In this case, we don't subtract one from the reference count |
390 | | // because the queue holds a reference. |
391 | | new_shared = _Py_REF_QUEUED; |
392 | | } |
393 | | else { |
394 | | // Otherwise, subtract one from the reference count. This might |
395 | | // be negative! |
396 | | new_shared = shared - (1 << _Py_REF_SHARED_SHIFT); |
397 | | } |
398 | | |
399 | | #ifdef Py_REF_DEBUG |
400 | | if ((new_shared < 0 && _Py_REF_IS_MERGED(new_shared)) || |
401 | | (should_queue && is_dead(o))) |
402 | | { |
403 | | _Py_NegativeRefcount(filename, lineno, o); |
404 | | } |
405 | | #endif |
406 | | } while (!_Py_atomic_compare_exchange_ssize(&o->ob_ref_shared, |
407 | | &shared, new_shared)); |
408 | | |
409 | | if (should_queue) { |
410 | | #ifdef Py_REF_DEBUG |
411 | | _Py_IncRefTotal(_PyThreadState_GET()); |
412 | | #endif |
413 | | _Py_brc_queue_object(o); |
414 | | } |
415 | | else if (new_shared == _Py_REF_MERGED) { |
416 | | // refcount is zero AND merged |
417 | | return 1; |
418 | | } |
419 | | return 0; |
420 | | } |
421 | | |
422 | | void |
423 | | _Py_DecRefSharedDebug(PyObject *o, const char *filename, int lineno) |
424 | | { |
425 | | if (_Py_DecRefSharedIsDead(o, filename, lineno)) { |
426 | | _Py_Dealloc(o); |
427 | | } |
428 | | } |
429 | | |
430 | | void |
431 | | _Py_DecRefShared(PyObject *o) |
432 | | { |
433 | | _Py_DecRefSharedDebug(o, NULL, 0); |
434 | | } |
435 | | |
436 | | void |
437 | | _Py_MergeZeroLocalRefcount(PyObject *op) |
438 | | { |
439 | | assert(op->ob_ref_local == 0); |
440 | | |
441 | | Py_ssize_t shared = _Py_atomic_load_ssize_acquire(&op->ob_ref_shared); |
442 | | if (shared == 0) { |
443 | | // Fast-path: shared refcount is zero (including flags) |
444 | | _Py_Dealloc(op); |
445 | | return; |
446 | | } |
447 | | |
448 | | // gh-121794: This must be before the store to `ob_ref_shared` (gh-119999), |
449 | | // but should outside the fast-path to maintain the invariant that |
450 | | // a zero `ob_tid` implies a merged refcount. |
451 | | _Py_atomic_store_uintptr_relaxed(&op->ob_tid, 0); |
452 | | |
453 | | // Slow-path: atomically set the flags (low two bits) to _Py_REF_MERGED. |
454 | | Py_ssize_t new_shared; |
455 | | do { |
456 | | new_shared = (shared & ~_Py_REF_SHARED_FLAG_MASK) | _Py_REF_MERGED; |
457 | | } while (!_Py_atomic_compare_exchange_ssize(&op->ob_ref_shared, |
458 | | &shared, new_shared)); |
459 | | |
460 | | if (new_shared == _Py_REF_MERGED) { |
461 | | // i.e., the shared refcount is zero (only the flags are set) so we |
462 | | // deallocate the object. |
463 | | _Py_Dealloc(op); |
464 | | } |
465 | | } |
466 | | |
467 | | Py_ssize_t |
468 | | _Py_ExplicitMergeRefcount(PyObject *op, Py_ssize_t extra) |
469 | | { |
470 | | assert(!_Py_IsImmortal(op)); |
471 | | |
472 | | #ifdef Py_REF_DEBUG |
473 | | _Py_AddRefTotal(_PyThreadState_GET(), extra); |
474 | | #endif |
475 | | |
476 | | // gh-119999: Write to ob_ref_local and ob_tid before merging the refcount. |
477 | | Py_ssize_t local = (Py_ssize_t)op->ob_ref_local; |
478 | | _Py_atomic_store_uint32_relaxed(&op->ob_ref_local, 0); |
479 | | _Py_atomic_store_uintptr_relaxed(&op->ob_tid, 0); |
480 | | |
481 | | Py_ssize_t refcnt; |
482 | | Py_ssize_t new_shared; |
483 | | Py_ssize_t shared = _Py_atomic_load_ssize_relaxed(&op->ob_ref_shared); |
484 | | do { |
485 | | refcnt = Py_ARITHMETIC_RIGHT_SHIFT(Py_ssize_t, shared, _Py_REF_SHARED_SHIFT); |
486 | | refcnt += local; |
487 | | refcnt += extra; |
488 | | |
489 | | new_shared = _Py_REF_SHARED(refcnt, _Py_REF_MERGED); |
490 | | } while (!_Py_atomic_compare_exchange_ssize(&op->ob_ref_shared, |
491 | | &shared, new_shared)); |
492 | | return refcnt; |
493 | | } |
494 | | |
495 | | // The more complicated "slow" path for undoing the resurrection of an object. |
496 | | int |
497 | | _PyObject_ResurrectEndSlow(PyObject *op) |
498 | | { |
499 | | if (_Py_IsImmortal(op)) { |
500 | | return 1; |
501 | | } |
502 | | if (_Py_IsOwnedByCurrentThread(op)) { |
503 | | // If the object is owned by the current thread, give up ownership and |
504 | | // merge the refcount. This isn't necessary in all cases, but it |
505 | | // simplifies the implementation. |
506 | | Py_ssize_t refcount = _Py_ExplicitMergeRefcount(op, -1); |
507 | | if (refcount == 0) { |
508 | | #ifdef Py_TRACE_REFS |
509 | | _Py_ForgetReference(op); |
510 | | #endif |
511 | | return 0; |
512 | | } |
513 | | return 1; |
514 | | } |
515 | | int is_dead = _Py_DecRefSharedIsDead(op, NULL, 0); |
516 | | if (is_dead) { |
517 | | #ifdef Py_TRACE_REFS |
518 | | _Py_ForgetReference(op); |
519 | | #endif |
520 | | return 0; |
521 | | } |
522 | | return 1; |
523 | | } |
524 | | |
525 | | |
526 | | #endif /* Py_GIL_DISABLED */ |
527 | | |
528 | | |
529 | | /**************************************/ |
530 | | |
531 | | PyObject * |
532 | | PyObject_Init(PyObject *op, PyTypeObject *tp) |
533 | 0 | { |
534 | 0 | if (op == NULL) { |
535 | 0 | return PyErr_NoMemory(); |
536 | 0 | } |
537 | | |
538 | 0 | _PyObject_Init(op, tp); |
539 | 0 | return op; |
540 | 0 | } |
541 | | |
542 | | PyVarObject * |
543 | | PyObject_InitVar(PyVarObject *op, PyTypeObject *tp, Py_ssize_t size) |
544 | 0 | { |
545 | 0 | if (op == NULL) { |
546 | 0 | return (PyVarObject *) PyErr_NoMemory(); |
547 | 0 | } |
548 | | |
549 | 0 | _PyObject_InitVar(op, tp, size); |
550 | 0 | return op; |
551 | 0 | } |
552 | | |
553 | | PyObject * |
554 | | _PyObject_New(PyTypeObject *tp) |
555 | 2.78M | { |
556 | 2.78M | PyObject *op = (PyObject *) PyObject_Malloc(_PyObject_SIZE(tp)); |
557 | 2.78M | if (op == NULL) { |
558 | 0 | return PyErr_NoMemory(); |
559 | 0 | } |
560 | 2.78M | _PyObject_Init(op, tp); |
561 | 2.78M | return op; |
562 | 2.78M | } |
563 | | |
564 | | PyVarObject * |
565 | | _PyObject_NewVar(PyTypeObject *tp, Py_ssize_t nitems) |
566 | 185k | { |
567 | 185k | PyVarObject *op; |
568 | 185k | const size_t size = _PyObject_VAR_SIZE(tp, nitems); |
569 | 185k | op = (PyVarObject *) PyObject_Malloc(size); |
570 | 185k | if (op == NULL) { |
571 | 0 | return (PyVarObject *)PyErr_NoMemory(); |
572 | 0 | } |
573 | 185k | _PyObject_InitVar(op, tp, nitems); |
574 | 185k | return op; |
575 | 185k | } |
576 | | |
577 | | void |
578 | | PyObject_CallFinalizer(PyObject *self) |
579 | 22.8M | { |
580 | 22.8M | PyTypeObject *tp = Py_TYPE(self); |
581 | | |
582 | 22.8M | if (tp->tp_finalize == NULL) |
583 | 0 | return; |
584 | | /* tp_finalize should only be called once. */ |
585 | 22.8M | if (_PyType_IS_GC(tp) && _PyGC_FINALIZED(self)) |
586 | 51.0k | return; |
587 | | |
588 | 22.7M | tp->tp_finalize(self); |
589 | 22.7M | if (_PyType_IS_GC(tp)) { |
590 | 22.6M | _PyGC_SET_FINALIZED(self); |
591 | 22.6M | } |
592 | 22.7M | } |
593 | | |
594 | | int |
595 | | PyObject_CallFinalizerFromDealloc(PyObject *self) |
596 | 22.8M | { |
597 | 22.8M | if (Py_REFCNT(self) != 0) { |
598 | 0 | _PyObject_ASSERT_FAILED_MSG(self, |
599 | 0 | "PyObject_CallFinalizerFromDealloc called " |
600 | 0 | "on object with a non-zero refcount"); |
601 | 0 | } |
602 | | |
603 | | /* Temporarily resurrect the object. */ |
604 | 22.8M | _PyObject_ResurrectStart(self); |
605 | | |
606 | 22.8M | PyObject_CallFinalizer(self); |
607 | | |
608 | 22.8M | _PyObject_ASSERT_WITH_MSG(self, |
609 | 22.8M | Py_REFCNT(self) > 0, |
610 | 22.8M | "refcount is too small"); |
611 | | |
612 | 22.8M | _PyObject_ASSERT(self, |
613 | 22.8M | (!_PyType_IS_GC(Py_TYPE(self)) |
614 | 22.8M | || _PyObject_GC_IS_TRACKED(self))); |
615 | | |
616 | | /* Undo the temporary resurrection; can't use DECREF here, it would |
617 | | * cause a recursive call. */ |
618 | 22.8M | if (_PyObject_ResurrectEnd(self)) { |
619 | | /* tp_finalize resurrected it! |
620 | | gh-130202: Note that the object may still be dead in the free |
621 | | threaded build in some circumstances, so it's not safe to access |
622 | | `self` after this point. For example, the last reference to the |
623 | | resurrected `self` may be held by another thread, which can |
624 | | concurrently deallocate it. */ |
625 | 0 | return -1; |
626 | 0 | } |
627 | | |
628 | | /* this is the normal path out, the caller continues with deallocation. */ |
629 | 22.8M | return 0; |
630 | 22.8M | } |
631 | | |
632 | | int |
633 | | PyObject_Print(PyObject *op, FILE *fp, int flags) |
634 | 0 | { |
635 | 0 | int ret = 0; |
636 | 0 | int write_error = 0; |
637 | 0 | if (PyErr_CheckSignals()) |
638 | 0 | return -1; |
639 | 0 | if (_Py_EnterRecursiveCall(" printing an object")) { |
640 | 0 | return -1; |
641 | 0 | } |
642 | 0 | clearerr(fp); /* Clear any previous error condition */ |
643 | 0 | if (op == NULL) { |
644 | 0 | Py_BEGIN_ALLOW_THREADS |
645 | 0 | fprintf(fp, "<nil>"); |
646 | 0 | Py_END_ALLOW_THREADS |
647 | 0 | } |
648 | 0 | else { |
649 | 0 | if (Py_REFCNT(op) <= 0) { |
650 | 0 | Py_BEGIN_ALLOW_THREADS |
651 | 0 | fprintf(fp, "<refcnt %zd at %p>", Py_REFCNT(op), (void *)op); |
652 | 0 | Py_END_ALLOW_THREADS |
653 | 0 | } |
654 | 0 | else { |
655 | 0 | PyObject *s; |
656 | 0 | if (flags & Py_PRINT_RAW) |
657 | 0 | s = PyObject_Str(op); |
658 | 0 | else |
659 | 0 | s = PyObject_Repr(op); |
660 | 0 | if (s == NULL) { |
661 | 0 | ret = -1; |
662 | 0 | } |
663 | 0 | else { |
664 | 0 | assert(PyUnicode_Check(s)); |
665 | 0 | const char *t; |
666 | 0 | Py_ssize_t len; |
667 | 0 | t = PyUnicode_AsUTF8AndSize(s, &len); |
668 | 0 | if (t == NULL) { |
669 | 0 | ret = -1; |
670 | 0 | } |
671 | 0 | else { |
672 | | /* Versions of Android and OpenBSD from before 2023 fail to |
673 | | set the `ferror` indicator when writing to a read-only |
674 | | stream, so we need to check the return value. |
675 | | (https://github.com/openbsd/src/commit/fc99cf9338942ecd9adc94ea08bf6188f0428c15) */ |
676 | 0 | if (fwrite(t, 1, len, fp) != (size_t)len) { |
677 | 0 | write_error = 1; |
678 | 0 | } |
679 | 0 | } |
680 | 0 | Py_DECREF(s); |
681 | 0 | } |
682 | 0 | } |
683 | 0 | } |
684 | 0 | if (ret == 0) { |
685 | 0 | if (write_error || ferror(fp)) { |
686 | 0 | PyErr_SetFromErrno(PyExc_OSError); |
687 | 0 | clearerr(fp); |
688 | 0 | ret = -1; |
689 | 0 | } |
690 | 0 | } |
691 | 0 | return ret; |
692 | 0 | } |
693 | | |
694 | | /* For debugging convenience. Set a breakpoint here and call it from your DLL */ |
695 | | void |
696 | | _Py_BreakPoint(void) |
697 | 0 | { |
698 | 0 | } |
699 | | |
700 | | |
701 | | /* Heuristic checking if the object memory is uninitialized or deallocated. |
702 | | Rely on the debug hooks on Python memory allocators: |
703 | | see _PyMem_IsPtrFreed(). |
704 | | |
705 | | The function can be used to prevent segmentation fault on dereferencing |
706 | | pointers like 0xDDDDDDDDDDDDDDDD. */ |
707 | | int |
708 | | _PyObject_IsFreed(PyObject *op) |
709 | 0 | { |
710 | 0 | if (_PyMem_IsPtrFreed(op) || _PyMem_IsPtrFreed(Py_TYPE(op))) { |
711 | 0 | return 1; |
712 | 0 | } |
713 | 0 | return 0; |
714 | 0 | } |
715 | | |
716 | | |
717 | | /* For debugging convenience. */ |
718 | | void |
719 | | PyObject_Dump(PyObject* op) |
720 | 0 | { |
721 | 0 | if (_PyObject_IsFreed(op)) { |
722 | | /* It seems like the object memory has been freed: |
723 | | don't access it to prevent a segmentation fault. */ |
724 | 0 | fprintf(stderr, "<object at %p is freed>\n", op); |
725 | 0 | fflush(stderr); |
726 | 0 | return; |
727 | 0 | } |
728 | | |
729 | | /* first, write fields which are the least likely to crash */ |
730 | 0 | fprintf(stderr, "object address : %p\n", (void *)op); |
731 | 0 | fprintf(stderr, "object refcount : %zd\n", Py_REFCNT(op)); |
732 | 0 | fflush(stderr); |
733 | |
|
734 | 0 | PyTypeObject *type = Py_TYPE(op); |
735 | 0 | fprintf(stderr, "object type : %p\n", type); |
736 | 0 | fprintf(stderr, "object type name: %s\n", |
737 | 0 | type==NULL ? "NULL" : type->tp_name); |
738 | | |
739 | | /* the most dangerous part */ |
740 | 0 | fprintf(stderr, "object repr : "); |
741 | 0 | fflush(stderr); |
742 | |
|
743 | 0 | PyGILState_STATE gil = PyGILState_Ensure(); |
744 | 0 | PyObject *exc = PyErr_GetRaisedException(); |
745 | |
|
746 | 0 | (void)PyObject_Print(op, stderr, 0); |
747 | 0 | fflush(stderr); |
748 | |
|
749 | 0 | PyErr_SetRaisedException(exc); |
750 | 0 | PyGILState_Release(gil); |
751 | |
|
752 | 0 | fprintf(stderr, "\n"); |
753 | 0 | fflush(stderr); |
754 | 0 | } |
755 | | |
756 | | PyObject * |
757 | | PyObject_Repr(PyObject *v) |
758 | 17.1M | { |
759 | 17.1M | PyObject *res; |
760 | 17.1M | if (PyErr_CheckSignals()) |
761 | 0 | return NULL; |
762 | 17.1M | if (v == NULL) |
763 | 0 | return PyUnicode_FromString("<NULL>"); |
764 | 17.1M | if (Py_TYPE(v)->tp_repr == NULL) |
765 | 0 | return PyUnicode_FromFormat("<%s object at %p>", |
766 | 0 | Py_TYPE(v)->tp_name, v); |
767 | | |
768 | 17.1M | PyThreadState *tstate = _PyThreadState_GET(); |
769 | | #ifdef Py_DEBUG |
770 | | /* PyObject_Repr() must not be called with an exception set, |
771 | | because it can clear it (directly or indirectly) and so the |
772 | | caller loses its exception */ |
773 | | assert(!_PyErr_Occurred(tstate)); |
774 | | #endif |
775 | | |
776 | | /* It is possible for a type to have a tp_repr representation that loops |
777 | | infinitely. */ |
778 | 17.1M | if (_Py_EnterRecursiveCallTstate(tstate, |
779 | 17.1M | " while getting the repr of an object")) { |
780 | 0 | return NULL; |
781 | 0 | } |
782 | 17.1M | res = (*Py_TYPE(v)->tp_repr)(v); |
783 | 17.1M | _Py_LeaveRecursiveCallTstate(tstate); |
784 | | |
785 | 17.1M | if (res == NULL) { |
786 | 2 | return NULL; |
787 | 2 | } |
788 | 17.1M | if (!PyUnicode_Check(res)) { |
789 | 0 | _PyErr_Format(tstate, PyExc_TypeError, |
790 | 0 | "%T.__repr__() must return a str, not %T", v, res); |
791 | 0 | Py_DECREF(res); |
792 | 0 | return NULL; |
793 | 0 | } |
794 | 17.1M | return res; |
795 | 17.1M | } |
796 | | |
797 | | PyObject * |
798 | | PyObject_Str(PyObject *v) |
799 | 43.7M | { |
800 | 43.7M | PyObject *res; |
801 | 43.7M | if (PyErr_CheckSignals()) |
802 | 0 | return NULL; |
803 | 43.7M | if (v == NULL) |
804 | 0 | return PyUnicode_FromString("<NULL>"); |
805 | 43.7M | if (PyUnicode_CheckExact(v)) { |
806 | 33.3M | return Py_NewRef(v); |
807 | 33.3M | } |
808 | 10.3M | if (Py_TYPE(v)->tp_str == NULL) |
809 | 0 | return PyObject_Repr(v); |
810 | | |
811 | 10.3M | PyThreadState *tstate = _PyThreadState_GET(); |
812 | | #ifdef Py_DEBUG |
813 | | /* PyObject_Str() must not be called with an exception set, |
814 | | because it can clear it (directly or indirectly) and so the |
815 | | caller loses its exception */ |
816 | | assert(!_PyErr_Occurred(tstate)); |
817 | | #endif |
818 | | |
819 | | /* It is possible for a type to have a tp_str representation that loops |
820 | | infinitely. */ |
821 | 10.3M | if (_Py_EnterRecursiveCallTstate(tstate, " while getting the str of an object")) { |
822 | 0 | return NULL; |
823 | 0 | } |
824 | 10.3M | res = (*Py_TYPE(v)->tp_str)(v); |
825 | 10.3M | _Py_LeaveRecursiveCallTstate(tstate); |
826 | | |
827 | 10.3M | if (res == NULL) { |
828 | 5.15k | return NULL; |
829 | 5.15k | } |
830 | 10.3M | if (!PyUnicode_Check(res)) { |
831 | 0 | _PyErr_Format(tstate, PyExc_TypeError, |
832 | 0 | "%T.__str__() must return a str, not %T", v, res); |
833 | 0 | Py_DECREF(res); |
834 | 0 | return NULL; |
835 | 0 | } |
836 | 10.3M | assert(_PyUnicode_CheckConsistency(res, 1)); |
837 | 10.3M | return res; |
838 | 10.3M | } |
839 | | |
840 | | PyObject * |
841 | | PyObject_ASCII(PyObject *v) |
842 | 0 | { |
843 | 0 | PyObject *repr, *ascii, *res; |
844 | |
|
845 | 0 | repr = PyObject_Repr(v); |
846 | 0 | if (repr == NULL) |
847 | 0 | return NULL; |
848 | | |
849 | 0 | if (PyUnicode_IS_ASCII(repr)) |
850 | 0 | return repr; |
851 | | |
852 | | /* repr is guaranteed to be a PyUnicode object by PyObject_Repr */ |
853 | 0 | ascii = _PyUnicode_AsASCIIString(repr, "backslashreplace"); |
854 | 0 | Py_DECREF(repr); |
855 | 0 | if (ascii == NULL) |
856 | 0 | return NULL; |
857 | | |
858 | 0 | res = PyUnicode_DecodeASCII( |
859 | 0 | PyBytes_AS_STRING(ascii), |
860 | 0 | PyBytes_GET_SIZE(ascii), |
861 | 0 | NULL); |
862 | |
|
863 | 0 | Py_DECREF(ascii); |
864 | 0 | return res; |
865 | 0 | } |
866 | | |
867 | | PyObject * |
868 | | PyObject_Bytes(PyObject *v) |
869 | 220 | { |
870 | 220 | PyObject *result, *func; |
871 | | |
872 | 220 | if (v == NULL) |
873 | 0 | return PyBytes_FromString("<NULL>"); |
874 | | |
875 | 220 | if (PyBytes_CheckExact(v)) { |
876 | 0 | return Py_NewRef(v); |
877 | 0 | } |
878 | | |
879 | 220 | func = _PyObject_LookupSpecial(v, &_Py_ID(__bytes__)); |
880 | 220 | if (func != NULL) { |
881 | 0 | result = _PyObject_CallNoArgs(func); |
882 | 0 | Py_DECREF(func); |
883 | 0 | if (result == NULL) |
884 | 0 | return NULL; |
885 | 0 | if (!PyBytes_Check(result)) { |
886 | 0 | PyErr_Format(PyExc_TypeError, |
887 | 0 | "%T.__bytes__() must return a bytes, not %T", |
888 | 0 | v, result); |
889 | 0 | Py_DECREF(result); |
890 | 0 | return NULL; |
891 | 0 | } |
892 | 0 | return result; |
893 | 0 | } |
894 | 220 | else if (PyErr_Occurred()) |
895 | 0 | return NULL; |
896 | 220 | return PyBytes_FromObject(v); |
897 | 220 | } |
898 | | |
899 | | static void |
900 | | clear_freelist(struct _Py_freelist *freelist, int is_finalization, |
901 | | freefunc dofree) |
902 | 0 | { |
903 | 0 | void *ptr; |
904 | 0 | while ((ptr = _PyFreeList_PopNoStats(freelist)) != NULL) { |
905 | 0 | dofree(ptr); |
906 | 0 | } |
907 | 0 | assert(freelist->size == 0 || freelist->size == -1); |
908 | 0 | assert(freelist->freelist == NULL); |
909 | 0 | if (is_finalization) { |
910 | 0 | freelist->size = -1; |
911 | 0 | } |
912 | 0 | } |
913 | | |
914 | | static void |
915 | | free_object(void *obj) |
916 | 0 | { |
917 | 0 | PyObject *op = (PyObject *)obj; |
918 | 0 | PyTypeObject *tp = Py_TYPE(op); |
919 | 0 | tp->tp_free(op); |
920 | 0 | Py_DECREF(tp); |
921 | 0 | } |
922 | | |
923 | | void |
924 | | _PyObject_ClearFreeLists(struct _Py_freelists *freelists, int is_finalization) |
925 | 0 | { |
926 | | // In the free-threaded build, freelists are per-PyThreadState and cleared in PyThreadState_Clear() |
927 | | // In the default build, freelists are per-interpreter and cleared in finalize_interp_types() |
928 | 0 | clear_freelist(&freelists->floats, is_finalization, free_object); |
929 | 0 | clear_freelist(&freelists->complexes, is_finalization, free_object); |
930 | 0 | for (Py_ssize_t i = 0; i < PyTuple_MAXSAVESIZE; i++) { |
931 | 0 | clear_freelist(&freelists->tuples[i], is_finalization, free_object); |
932 | 0 | } |
933 | 0 | clear_freelist(&freelists->lists, is_finalization, free_object); |
934 | 0 | clear_freelist(&freelists->list_iters, is_finalization, free_object); |
935 | 0 | clear_freelist(&freelists->tuple_iters, is_finalization, free_object); |
936 | 0 | clear_freelist(&freelists->dicts, is_finalization, free_object); |
937 | 0 | clear_freelist(&freelists->dictkeys, is_finalization, PyMem_Free); |
938 | 0 | clear_freelist(&freelists->slices, is_finalization, free_object); |
939 | 0 | clear_freelist(&freelists->ranges, is_finalization, free_object); |
940 | 0 | clear_freelist(&freelists->range_iters, is_finalization, free_object); |
941 | 0 | clear_freelist(&freelists->contexts, is_finalization, free_object); |
942 | 0 | clear_freelist(&freelists->async_gens, is_finalization, free_object); |
943 | 0 | clear_freelist(&freelists->async_gen_asends, is_finalization, free_object); |
944 | 0 | clear_freelist(&freelists->futureiters, is_finalization, free_object); |
945 | 0 | if (is_finalization) { |
946 | | // Only clear object stack chunks during finalization. We use object |
947 | | // stacks during GC, so emptying the free-list is counterproductive. |
948 | 0 | clear_freelist(&freelists->object_stack_chunks, 1, PyMem_RawFree); |
949 | 0 | } |
950 | 0 | clear_freelist(&freelists->unicode_writers, is_finalization, PyMem_Free); |
951 | 0 | clear_freelist(&freelists->bytes_writers, is_finalization, PyMem_Free); |
952 | 0 | clear_freelist(&freelists->ints, is_finalization, free_object); |
953 | 0 | clear_freelist(&freelists->pycfunctionobject, is_finalization, PyObject_GC_Del); |
954 | 0 | clear_freelist(&freelists->pycmethodobject, is_finalization, PyObject_GC_Del); |
955 | 0 | clear_freelist(&freelists->pymethodobjects, is_finalization, free_object); |
956 | 0 | } |
957 | | |
958 | | /* |
959 | | def _PyObject_FunctionStr(x): |
960 | | try: |
961 | | qualname = x.__qualname__ |
962 | | except AttributeError: |
963 | | return str(x) |
964 | | try: |
965 | | mod = x.__module__ |
966 | | if mod is not None and mod != 'builtins': |
967 | | return f"{x.__module__}.{qualname}()" |
968 | | except AttributeError: |
969 | | pass |
970 | | return qualname |
971 | | */ |
972 | | PyObject * |
973 | | _PyObject_FunctionStr(PyObject *x) |
974 | 0 | { |
975 | 0 | assert(!PyErr_Occurred()); |
976 | 0 | PyObject *qualname; |
977 | 0 | int ret = PyObject_GetOptionalAttr(x, &_Py_ID(__qualname__), &qualname); |
978 | 0 | if (qualname == NULL) { |
979 | 0 | if (ret < 0) { |
980 | 0 | return NULL; |
981 | 0 | } |
982 | 0 | return PyObject_Str(x); |
983 | 0 | } |
984 | 0 | PyObject *module; |
985 | 0 | PyObject *result = NULL; |
986 | 0 | ret = PyObject_GetOptionalAttr(x, &_Py_ID(__module__), &module); |
987 | 0 | if (module != NULL && module != Py_None) { |
988 | 0 | ret = PyObject_RichCompareBool(module, &_Py_ID(builtins), Py_NE); |
989 | 0 | if (ret < 0) { |
990 | | // error |
991 | 0 | goto done; |
992 | 0 | } |
993 | 0 | if (ret > 0) { |
994 | 0 | result = PyUnicode_FromFormat("%S.%S()", module, qualname); |
995 | 0 | goto done; |
996 | 0 | } |
997 | 0 | } |
998 | 0 | else if (ret < 0) { |
999 | 0 | goto done; |
1000 | 0 | } |
1001 | 0 | result = PyUnicode_FromFormat("%S()", qualname); |
1002 | 0 | done: |
1003 | 0 | Py_DECREF(qualname); |
1004 | 0 | Py_XDECREF(module); |
1005 | 0 | return result; |
1006 | 0 | } |
1007 | | |
1008 | | /* For Python 3.0.1 and later, the old three-way comparison has been |
1009 | | completely removed in favour of rich comparisons. PyObject_Compare() and |
1010 | | PyObject_Cmp() are gone, and the builtin cmp function no longer exists. |
1011 | | The old tp_compare slot has been renamed to tp_as_async, and should no |
1012 | | longer be used. Use tp_richcompare instead. |
1013 | | |
1014 | | See (*) below for practical amendments. |
1015 | | |
1016 | | tp_richcompare gets called with a first argument of the appropriate type |
1017 | | and a second object of an arbitrary type. We never do any kind of |
1018 | | coercion. |
1019 | | |
1020 | | The tp_richcompare slot should return an object, as follows: |
1021 | | |
1022 | | NULL if an exception occurred |
1023 | | NotImplemented if the requested comparison is not implemented |
1024 | | any other false value if the requested comparison is false |
1025 | | any other true value if the requested comparison is true |
1026 | | |
1027 | | The PyObject_RichCompare[Bool]() wrappers raise TypeError when they get |
1028 | | NotImplemented. |
1029 | | |
1030 | | (*) Practical amendments: |
1031 | | |
1032 | | - If rich comparison returns NotImplemented, == and != are decided by |
1033 | | comparing the object pointer (i.e. falling back to the base object |
1034 | | implementation). |
1035 | | |
1036 | | */ |
1037 | | |
1038 | | /* Map rich comparison operators to their swapped version, e.g. LT <--> GT */ |
1039 | | int _Py_SwappedOp[] = {Py_GT, Py_GE, Py_EQ, Py_NE, Py_LT, Py_LE}; |
1040 | | |
1041 | | static const char * const opstrings[] = {"<", "<=", "==", "!=", ">", ">="}; |
1042 | | |
1043 | | /* Perform a rich comparison, raising TypeError when the requested comparison |
1044 | | operator is not supported. */ |
1045 | | static PyObject * |
1046 | | do_richcompare(PyThreadState *tstate, PyObject *v, PyObject *w, int op) |
1047 | 203M | { |
1048 | 203M | richcmpfunc f; |
1049 | 203M | PyObject *res; |
1050 | 203M | int checked_reverse_op = 0; |
1051 | | |
1052 | 203M | if (!Py_IS_TYPE(v, Py_TYPE(w)) && |
1053 | 13.2M | PyType_IsSubtype(Py_TYPE(w), Py_TYPE(v)) && |
1054 | 290k | (f = Py_TYPE(w)->tp_richcompare) != NULL) { |
1055 | 290k | checked_reverse_op = 1; |
1056 | 290k | res = (*f)(w, v, _Py_SwappedOp[op]); |
1057 | 290k | if (res != Py_NotImplemented) |
1058 | 259k | return res; |
1059 | 31.5k | Py_DECREF(res); |
1060 | 31.5k | } |
1061 | 203M | if ((f = Py_TYPE(v)->tp_richcompare) != NULL) { |
1062 | 203M | res = (*f)(v, w, op); |
1063 | 203M | if (res != Py_NotImplemented) |
1064 | 194M | return res; |
1065 | 8.21M | Py_DECREF(res); |
1066 | 8.21M | } |
1067 | 8.21M | if (!checked_reverse_op && (f = Py_TYPE(w)->tp_richcompare) != NULL) { |
1068 | 8.17M | res = (*f)(w, v, _Py_SwappedOp[op]); |
1069 | 8.17M | if (res != Py_NotImplemented) |
1070 | 49.2k | return res; |
1071 | 8.13M | Py_DECREF(res); |
1072 | 8.13M | } |
1073 | | /* If neither object implements it, provide a sensible default |
1074 | | for == and !=, but raise an exception for ordering. */ |
1075 | 8.16M | switch (op) { |
1076 | 8.16M | case Py_EQ: |
1077 | 8.16M | res = (v == w) ? Py_True : Py_False; |
1078 | 8.16M | break; |
1079 | 1.23k | case Py_NE: |
1080 | 1.23k | res = (v != w) ? Py_True : Py_False; |
1081 | 1.23k | break; |
1082 | 0 | default: |
1083 | 0 | _PyErr_Format(tstate, PyExc_TypeError, |
1084 | 0 | "'%s' not supported between instances of '%.100s' and '%.100s'", |
1085 | 0 | opstrings[op], |
1086 | 0 | Py_TYPE(v)->tp_name, |
1087 | 0 | Py_TYPE(w)->tp_name); |
1088 | 0 | return NULL; |
1089 | 8.16M | } |
1090 | 8.16M | return Py_NewRef(res); |
1091 | 8.16M | } |
1092 | | |
1093 | | /* Perform a rich comparison with object result. This wraps do_richcompare() |
1094 | | with a check for NULL arguments and a recursion check. */ |
1095 | | |
1096 | | PyObject * |
1097 | | PyObject_RichCompare(PyObject *v, PyObject *w, int op) |
1098 | 203M | { |
1099 | 203M | PyThreadState *tstate = _PyThreadState_GET(); |
1100 | | |
1101 | 203M | assert(Py_LT <= op && op <= Py_GE); |
1102 | 203M | if (v == NULL || w == NULL) { |
1103 | 0 | if (!_PyErr_Occurred(tstate)) { |
1104 | 0 | PyErr_BadInternalCall(); |
1105 | 0 | } |
1106 | 0 | return NULL; |
1107 | 0 | } |
1108 | 203M | if (_Py_EnterRecursiveCallTstate(tstate, " in comparison")) { |
1109 | 0 | return NULL; |
1110 | 0 | } |
1111 | 203M | PyObject *res = do_richcompare(tstate, v, w, op); |
1112 | 203M | _Py_LeaveRecursiveCallTstate(tstate); |
1113 | 203M | return res; |
1114 | 203M | } |
1115 | | |
1116 | | /* Perform a rich comparison with integer result. This wraps |
1117 | | PyObject_RichCompare(), returning -1 for error, 0 for false, 1 for true. */ |
1118 | | int |
1119 | | PyObject_RichCompareBool(PyObject *v, PyObject *w, int op) |
1120 | 204M | { |
1121 | 204M | PyObject *res; |
1122 | 204M | int ok; |
1123 | | |
1124 | | /* Quick result when objects are the same. |
1125 | | Guarantees that identity implies equality. */ |
1126 | 204M | if (v == w) { |
1127 | 36.6M | if (op == Py_EQ) |
1128 | 33.6M | return 1; |
1129 | 2.99M | else if (op == Py_NE) |
1130 | 0 | return 0; |
1131 | 36.6M | } |
1132 | | |
1133 | 170M | res = PyObject_RichCompare(v, w, op); |
1134 | 170M | if (res == NULL) |
1135 | 0 | return -1; |
1136 | 170M | if (PyBool_Check(res)) { |
1137 | 170M | ok = (res == Py_True); |
1138 | 170M | assert(_Py_IsImmortal(res)); |
1139 | 170M | } |
1140 | 0 | else { |
1141 | 0 | ok = PyObject_IsTrue(res); |
1142 | 0 | Py_DECREF(res); |
1143 | 0 | } |
1144 | 170M | return ok; |
1145 | 170M | } |
1146 | | |
1147 | | Py_hash_t |
1148 | | PyObject_HashNotImplemented(PyObject *v) |
1149 | 0 | { |
1150 | 0 | PyErr_Format(PyExc_TypeError, "unhashable type: '%.200s'", |
1151 | 0 | Py_TYPE(v)->tp_name); |
1152 | 0 | return -1; |
1153 | 0 | } |
1154 | | |
1155 | | Py_hash_t |
1156 | | PyObject_Hash(PyObject *v) |
1157 | 1.38G | { |
1158 | 1.38G | PyTypeObject *tp = Py_TYPE(v); |
1159 | 1.38G | if (tp->tp_hash != NULL) |
1160 | 1.38G | return (*tp->tp_hash)(v); |
1161 | | /* To keep to the general practice that inheriting |
1162 | | * solely from object in C code should work without |
1163 | | * an explicit call to PyType_Ready, we implicitly call |
1164 | | * PyType_Ready here and then check the tp_hash slot again |
1165 | | */ |
1166 | 0 | if (!_PyType_IsReady(tp)) { |
1167 | 0 | if (PyType_Ready(tp) < 0) |
1168 | 0 | return -1; |
1169 | 0 | if (tp->tp_hash != NULL) |
1170 | 0 | return (*tp->tp_hash)(v); |
1171 | 0 | } |
1172 | | /* Otherwise, the object can't be hashed */ |
1173 | 0 | return PyObject_HashNotImplemented(v); |
1174 | 0 | } |
1175 | | |
1176 | | PyObject * |
1177 | | PyObject_GetAttrString(PyObject *v, const char *name) |
1178 | 491k | { |
1179 | 491k | PyObject *w, *res; |
1180 | | |
1181 | 491k | if (Py_TYPE(v)->tp_getattr != NULL) |
1182 | 0 | return (*Py_TYPE(v)->tp_getattr)(v, (char*)name); |
1183 | 491k | w = PyUnicode_FromString(name); |
1184 | 491k | if (w == NULL) |
1185 | 0 | return NULL; |
1186 | 491k | res = PyObject_GetAttr(v, w); |
1187 | 491k | Py_DECREF(w); |
1188 | 491k | return res; |
1189 | 491k | } |
1190 | | |
1191 | | int |
1192 | | PyObject_HasAttrStringWithError(PyObject *obj, const char *name) |
1193 | 0 | { |
1194 | 0 | PyObject *res; |
1195 | 0 | int rc = PyObject_GetOptionalAttrString(obj, name, &res); |
1196 | 0 | Py_XDECREF(res); |
1197 | 0 | return rc; |
1198 | 0 | } |
1199 | | |
1200 | | |
1201 | | int |
1202 | | PyObject_HasAttrString(PyObject *obj, const char *name) |
1203 | 0 | { |
1204 | 0 | int rc = PyObject_HasAttrStringWithError(obj, name); |
1205 | 0 | if (rc < 0) { |
1206 | 0 | PyErr_FormatUnraisable( |
1207 | 0 | "Exception ignored in PyObject_HasAttrString(); consider using " |
1208 | 0 | "PyObject_HasAttrStringWithError(), " |
1209 | 0 | "PyObject_GetOptionalAttrString() or PyObject_GetAttrString()"); |
1210 | 0 | return 0; |
1211 | 0 | } |
1212 | 0 | return rc; |
1213 | 0 | } |
1214 | | |
1215 | | int |
1216 | | PyObject_SetAttrString(PyObject *v, const char *name, PyObject *w) |
1217 | 76.1k | { |
1218 | 76.1k | PyThreadState *tstate = _PyThreadState_GET(); |
1219 | 76.1k | if (w == NULL && _PyErr_Occurred(tstate)) { |
1220 | 0 | PyObject *exc = _PyErr_GetRaisedException(tstate); |
1221 | 0 | _PyErr_SetString(tstate, PyExc_SystemError, |
1222 | 0 | "PyObject_SetAttrString() must not be called with NULL value " |
1223 | 0 | "and an exception set"); |
1224 | 0 | _PyErr_ChainExceptions1Tstate(tstate, exc); |
1225 | 0 | return -1; |
1226 | 0 | } |
1227 | | |
1228 | 76.1k | if (Py_TYPE(v)->tp_setattr != NULL) { |
1229 | 0 | return (*Py_TYPE(v)->tp_setattr)(v, (char*)name, w); |
1230 | 0 | } |
1231 | | |
1232 | 76.1k | PyObject *s = PyUnicode_InternFromString(name); |
1233 | 76.1k | if (s == NULL) { |
1234 | 0 | return -1; |
1235 | 0 | } |
1236 | | |
1237 | 76.1k | int res = PyObject_SetAttr(v, s, w); |
1238 | 76.1k | Py_DECREF(s); |
1239 | 76.1k | return res; |
1240 | 76.1k | } |
1241 | | |
1242 | | int |
1243 | | PyObject_DelAttrString(PyObject *v, const char *name) |
1244 | 0 | { |
1245 | 0 | return PyObject_SetAttrString(v, name, NULL); |
1246 | 0 | } |
1247 | | |
1248 | | int |
1249 | | _PyObject_IsAbstract(PyObject *obj) |
1250 | 32.0k | { |
1251 | 32.0k | int res; |
1252 | 32.0k | PyObject* isabstract; |
1253 | | |
1254 | 32.0k | if (obj == NULL) |
1255 | 620 | return 0; |
1256 | | |
1257 | 31.4k | res = PyObject_GetOptionalAttr(obj, &_Py_ID(__isabstractmethod__), &isabstract); |
1258 | 31.4k | if (res > 0) { |
1259 | 4.19k | res = PyObject_IsTrue(isabstract); |
1260 | 4.19k | Py_DECREF(isabstract); |
1261 | 4.19k | } |
1262 | 31.4k | return res; |
1263 | 32.0k | } |
1264 | | |
1265 | | PyObject * |
1266 | | _PyObject_GetAttrId(PyObject *v, _Py_Identifier *name) |
1267 | 0 | { |
1268 | 0 | PyObject *result; |
1269 | 0 | _Py_COMP_DIAG_PUSH |
1270 | 0 | _Py_COMP_DIAG_IGNORE_DEPR_DECLS |
1271 | 0 | PyObject *oname = _PyUnicode_FromId(name); /* borrowed */ |
1272 | 0 | _Py_COMP_DIAG_POP |
1273 | 0 | if (!oname) |
1274 | 0 | return NULL; |
1275 | 0 | result = PyObject_GetAttr(v, oname); |
1276 | 0 | return result; |
1277 | 0 | } |
1278 | | |
1279 | | int |
1280 | | _PyObject_SetAttributeErrorContext(PyObject* v, PyObject* name) |
1281 | 5.02M | { |
1282 | 5.02M | assert(PyErr_Occurred()); |
1283 | 5.02M | if (!PyErr_ExceptionMatches(PyExc_AttributeError)){ |
1284 | 3.11k | return 0; |
1285 | 3.11k | } |
1286 | | // Intercept AttributeError exceptions and augment them to offer suggestions later. |
1287 | 5.02M | PyObject *exc = PyErr_GetRaisedException(); |
1288 | 5.02M | if (!PyErr_GivenExceptionMatches(exc, PyExc_AttributeError)) { |
1289 | 0 | goto restore; |
1290 | 0 | } |
1291 | 5.02M | PyAttributeErrorObject* the_exc = (PyAttributeErrorObject*) exc; |
1292 | | // Check if this exception was already augmented |
1293 | 5.02M | if (the_exc->name || the_exc->obj) { |
1294 | 2.16M | goto restore; |
1295 | 2.16M | } |
1296 | | // Augment the exception with the name and object |
1297 | 2.85M | if (PyObject_SetAttr(exc, &_Py_ID(name), name) || |
1298 | 2.85M | PyObject_SetAttr(exc, &_Py_ID(obj), v)) { |
1299 | 0 | Py_DECREF(exc); |
1300 | 0 | return 1; |
1301 | 0 | } |
1302 | 5.02M | restore: |
1303 | 5.02M | PyErr_SetRaisedException(exc); |
1304 | 5.02M | return 0; |
1305 | 2.85M | } |
1306 | | |
1307 | | PyObject * |
1308 | | PyObject_GetAttr(PyObject *v, PyObject *name) |
1309 | 54.2M | { |
1310 | 54.2M | PyTypeObject *tp = Py_TYPE(v); |
1311 | 54.2M | if (!PyUnicode_Check(name)) { |
1312 | 0 | PyErr_Format(PyExc_TypeError, |
1313 | 0 | "attribute name must be string, not '%.200s'", |
1314 | 0 | Py_TYPE(name)->tp_name); |
1315 | 0 | return NULL; |
1316 | 0 | } |
1317 | | |
1318 | 54.2M | PyObject* result = NULL; |
1319 | 54.2M | if (tp->tp_getattro != NULL) { |
1320 | 54.2M | result = (*tp->tp_getattro)(v, name); |
1321 | 54.2M | } |
1322 | 0 | else if (tp->tp_getattr != NULL) { |
1323 | 0 | const char *name_str = PyUnicode_AsUTF8(name); |
1324 | 0 | if (name_str == NULL) { |
1325 | 0 | return NULL; |
1326 | 0 | } |
1327 | 0 | result = (*tp->tp_getattr)(v, (char *)name_str); |
1328 | 0 | } |
1329 | 0 | else { |
1330 | 0 | PyErr_Format(PyExc_AttributeError, |
1331 | 0 | "'%.100s' object has no attribute '%U'", |
1332 | 0 | tp->tp_name, name); |
1333 | 0 | } |
1334 | | |
1335 | 54.2M | if (result == NULL) { |
1336 | 2.02M | _PyObject_SetAttributeErrorContext(v, name); |
1337 | 2.02M | } |
1338 | 54.2M | return result; |
1339 | 54.2M | } |
1340 | | |
1341 | | /* Like PyObject_GetAttr but returns a _PyStackRef. |
1342 | | For types (tp_getattro == _Py_type_getattro), this can return |
1343 | | a deferred reference to reduce reference count contention. */ |
1344 | | _PyStackRef |
1345 | | _PyObject_GetAttrStackRef(PyObject *v, PyObject *name) |
1346 | 273M | { |
1347 | 273M | PyTypeObject *tp = Py_TYPE(v); |
1348 | 273M | if (!PyUnicode_Check(name)) { |
1349 | 0 | PyErr_Format(PyExc_TypeError, |
1350 | 0 | "attribute name must be string, not '%.200s'", |
1351 | 0 | Py_TYPE(name)->tp_name); |
1352 | 0 | return PyStackRef_NULL; |
1353 | 0 | } |
1354 | | |
1355 | | /* Fast path for types - can return deferred references */ |
1356 | 273M | if (tp->tp_getattro == _Py_type_getattro) { |
1357 | 28.6M | _PyStackRef result = _Py_type_getattro_stackref((PyTypeObject *)v, name, NULL); |
1358 | 28.6M | if (PyStackRef_IsNull(result)) { |
1359 | 276 | _PyObject_SetAttributeErrorContext(v, name); |
1360 | 276 | } |
1361 | 28.6M | return result; |
1362 | 28.6M | } |
1363 | | |
1364 | | /* Fall back to regular PyObject_GetAttr and convert to stackref */ |
1365 | 244M | PyObject *result = NULL; |
1366 | 244M | if (tp->tp_getattro != NULL) { |
1367 | 244M | result = (*tp->tp_getattro)(v, name); |
1368 | 244M | } |
1369 | 0 | else if (tp->tp_getattr != NULL) { |
1370 | 0 | const char *name_str = PyUnicode_AsUTF8(name); |
1371 | 0 | if (name_str == NULL) { |
1372 | 0 | return PyStackRef_NULL; |
1373 | 0 | } |
1374 | 0 | result = (*tp->tp_getattr)(v, (char *)name_str); |
1375 | 0 | } |
1376 | 0 | else { |
1377 | 0 | PyErr_Format(PyExc_AttributeError, |
1378 | 0 | "'%.100s' object has no attribute '%U'", |
1379 | 0 | tp->tp_name, name); |
1380 | 0 | } |
1381 | | |
1382 | 244M | if (result == NULL) { |
1383 | 823k | _PyObject_SetAttributeErrorContext(v, name); |
1384 | 823k | return PyStackRef_NULL; |
1385 | 823k | } |
1386 | 243M | return PyStackRef_FromPyObjectSteal(result); |
1387 | 244M | } |
1388 | | |
1389 | | int |
1390 | | PyObject_GetOptionalAttr(PyObject *v, PyObject *name, PyObject **result) |
1391 | 88.5M | { |
1392 | 88.5M | PyTypeObject *tp = Py_TYPE(v); |
1393 | | |
1394 | 88.5M | if (!PyUnicode_Check(name)) { |
1395 | 0 | PyErr_Format(PyExc_TypeError, |
1396 | 0 | "attribute name must be string, not '%.200s'", |
1397 | 0 | Py_TYPE(name)->tp_name); |
1398 | 0 | *result = NULL; |
1399 | 0 | return -1; |
1400 | 0 | } |
1401 | | |
1402 | 88.5M | if (tp->tp_getattro == PyObject_GenericGetAttr) { |
1403 | 78.6M | *result = _PyObject_GenericGetAttrWithDict(v, name, NULL, 1); |
1404 | 78.6M | if (*result != NULL) { |
1405 | 64.4M | return 1; |
1406 | 64.4M | } |
1407 | 14.2M | if (PyErr_Occurred()) { |
1408 | 24 | return -1; |
1409 | 24 | } |
1410 | 14.2M | return 0; |
1411 | 14.2M | } |
1412 | 9.87M | if (tp->tp_getattro == _Py_type_getattro) { |
1413 | 179k | int suppress_missing_attribute_exception = 0; |
1414 | 179k | *result = _Py_type_getattro_impl((PyTypeObject*)v, name, &suppress_missing_attribute_exception); |
1415 | 179k | if (suppress_missing_attribute_exception) { |
1416 | | // return 0 without having to clear the exception |
1417 | 10.1k | return 0; |
1418 | 10.1k | } |
1419 | 179k | } |
1420 | 9.69M | else if (tp->tp_getattro == (getattrofunc)_Py_module_getattro) { |
1421 | | // optimization: suppress attribute error from module getattro method |
1422 | 7.14M | *result = _Py_module_getattro_impl((PyModuleObject*)v, name, 1); |
1423 | 7.14M | if (*result != NULL) { |
1424 | 5.99M | return 1; |
1425 | 5.99M | } |
1426 | 1.14M | if (PyErr_Occurred()) { |
1427 | 0 | return -1; |
1428 | 0 | } |
1429 | 1.14M | return 0; |
1430 | 1.14M | } |
1431 | 2.55M | else if (tp->tp_getattro != NULL) { |
1432 | 2.55M | *result = (*tp->tp_getattro)(v, name); |
1433 | 2.55M | } |
1434 | 0 | else if (tp->tp_getattr != NULL) { |
1435 | 0 | const char *name_str = PyUnicode_AsUTF8(name); |
1436 | 0 | if (name_str == NULL) { |
1437 | 0 | *result = NULL; |
1438 | 0 | return -1; |
1439 | 0 | } |
1440 | 0 | *result = (*tp->tp_getattr)(v, (char *)name_str); |
1441 | 0 | } |
1442 | 0 | else { |
1443 | 0 | *result = NULL; |
1444 | 0 | return 0; |
1445 | 0 | } |
1446 | | |
1447 | 2.72M | if (*result != NULL) { |
1448 | 2.72M | return 1; |
1449 | 2.72M | } |
1450 | 1.57k | if (!PyErr_ExceptionMatches(PyExc_AttributeError)) { |
1451 | 0 | return -1; |
1452 | 0 | } |
1453 | 1.57k | PyErr_Clear(); |
1454 | 1.57k | return 0; |
1455 | 1.57k | } |
1456 | | |
1457 | | int |
1458 | | PyObject_GetOptionalAttrString(PyObject *obj, const char *name, PyObject **result) |
1459 | 48 | { |
1460 | 48 | if (Py_TYPE(obj)->tp_getattr == NULL) { |
1461 | 48 | PyObject *oname = PyUnicode_FromString(name); |
1462 | 48 | if (oname == NULL) { |
1463 | 0 | *result = NULL; |
1464 | 0 | return -1; |
1465 | 0 | } |
1466 | 48 | int rc = PyObject_GetOptionalAttr(obj, oname, result); |
1467 | 48 | Py_DECREF(oname); |
1468 | 48 | return rc; |
1469 | 48 | } |
1470 | | |
1471 | 0 | *result = (*Py_TYPE(obj)->tp_getattr)(obj, (char*)name); |
1472 | 0 | if (*result != NULL) { |
1473 | 0 | return 1; |
1474 | 0 | } |
1475 | 0 | if (!PyErr_ExceptionMatches(PyExc_AttributeError)) { |
1476 | 0 | return -1; |
1477 | 0 | } |
1478 | 0 | PyErr_Clear(); |
1479 | 0 | return 0; |
1480 | 0 | } |
1481 | | |
1482 | | int |
1483 | | PyObject_HasAttrWithError(PyObject *obj, PyObject *name) |
1484 | 11.4M | { |
1485 | 11.4M | PyObject *res; |
1486 | 11.4M | int rc = PyObject_GetOptionalAttr(obj, name, &res); |
1487 | 11.4M | Py_XDECREF(res); |
1488 | 11.4M | return rc; |
1489 | 11.4M | } |
1490 | | |
1491 | | int |
1492 | | PyObject_HasAttr(PyObject *obj, PyObject *name) |
1493 | 0 | { |
1494 | 0 | int rc = PyObject_HasAttrWithError(obj, name); |
1495 | 0 | if (rc < 0) { |
1496 | 0 | PyErr_FormatUnraisable( |
1497 | 0 | "Exception ignored in PyObject_HasAttr(); consider using " |
1498 | 0 | "PyObject_HasAttrWithError(), " |
1499 | 0 | "PyObject_GetOptionalAttr() or PyObject_GetAttr()"); |
1500 | 0 | return 0; |
1501 | 0 | } |
1502 | 0 | return rc; |
1503 | 0 | } |
1504 | | |
1505 | | int |
1506 | | PyObject_SetAttr(PyObject *v, PyObject *name, PyObject *value) |
1507 | 72.8M | { |
1508 | 72.8M | PyThreadState *tstate = _PyThreadState_GET(); |
1509 | 72.8M | if (value == NULL && _PyErr_Occurred(tstate)) { |
1510 | 0 | PyObject *exc = _PyErr_GetRaisedException(tstate); |
1511 | 0 | _PyErr_SetString(tstate, PyExc_SystemError, |
1512 | 0 | "PyObject_SetAttr() must not be called with NULL value " |
1513 | 0 | "and an exception set"); |
1514 | 0 | _PyErr_ChainExceptions1Tstate(tstate, exc); |
1515 | 0 | return -1; |
1516 | 0 | } |
1517 | | |
1518 | 72.8M | PyTypeObject *tp = Py_TYPE(v); |
1519 | 72.8M | int err; |
1520 | | |
1521 | 72.8M | if (!PyUnicode_Check(name)) { |
1522 | 0 | PyErr_Format(PyExc_TypeError, |
1523 | 0 | "attribute name must be string, not '%.200s'", |
1524 | 0 | Py_TYPE(name)->tp_name); |
1525 | 0 | return -1; |
1526 | 0 | } |
1527 | 72.8M | Py_INCREF(name); |
1528 | | |
1529 | 72.8M | _PyUnicode_InternMortal(tstate->interp, &name); |
1530 | 72.8M | if (tp->tp_setattro != NULL) { |
1531 | 72.8M | err = (*tp->tp_setattro)(v, name, value); |
1532 | 72.8M | Py_DECREF(name); |
1533 | 72.8M | return err; |
1534 | 72.8M | } |
1535 | 0 | if (tp->tp_setattr != NULL) { |
1536 | 0 | const char *name_str = PyUnicode_AsUTF8(name); |
1537 | 0 | if (name_str == NULL) { |
1538 | 0 | Py_DECREF(name); |
1539 | 0 | return -1; |
1540 | 0 | } |
1541 | 0 | err = (*tp->tp_setattr)(v, (char *)name_str, value); |
1542 | 0 | Py_DECREF(name); |
1543 | 0 | return err; |
1544 | 0 | } |
1545 | 0 | Py_DECREF(name); |
1546 | 0 | _PyObject_ASSERT(name, Py_REFCNT(name) >= 1); |
1547 | 0 | if (tp->tp_getattr == NULL && tp->tp_getattro == NULL) |
1548 | 0 | PyErr_Format(PyExc_TypeError, |
1549 | 0 | "'%.100s' object has no attributes " |
1550 | 0 | "(%s .%U)", |
1551 | 0 | tp->tp_name, |
1552 | 0 | value==NULL ? "del" : "assign to", |
1553 | 0 | name); |
1554 | 0 | else |
1555 | 0 | PyErr_Format(PyExc_TypeError, |
1556 | 0 | "'%.100s' object has only read-only attributes " |
1557 | 0 | "(%s .%U)", |
1558 | 0 | tp->tp_name, |
1559 | 0 | value==NULL ? "del" : "assign to", |
1560 | 0 | name); |
1561 | 0 | return -1; |
1562 | 0 | } |
1563 | | |
1564 | | int |
1565 | | PyObject_DelAttr(PyObject *v, PyObject *name) |
1566 | 347k | { |
1567 | 347k | return PyObject_SetAttr(v, name, NULL); |
1568 | 347k | } |
1569 | | |
1570 | | PyObject ** |
1571 | | _PyObject_ComputedDictPointer(PyObject *obj) |
1572 | 176M | { |
1573 | 176M | PyTypeObject *tp = Py_TYPE(obj); |
1574 | 176M | assert((tp->tp_flags & Py_TPFLAGS_MANAGED_DICT) == 0); |
1575 | | |
1576 | 176M | Py_ssize_t dictoffset = tp->tp_dictoffset; |
1577 | 176M | if (dictoffset == 0) { |
1578 | 54.3M | return NULL; |
1579 | 54.3M | } |
1580 | | |
1581 | 122M | if (dictoffset < 0) { |
1582 | 0 | assert(dictoffset != -1); |
1583 | |
|
1584 | 0 | Py_ssize_t tsize = Py_SIZE(obj); |
1585 | 0 | if (tsize < 0) { |
1586 | 0 | tsize = -tsize; |
1587 | 0 | } |
1588 | 0 | size_t size = _PyObject_VAR_SIZE(tp, tsize); |
1589 | 0 | assert(size <= (size_t)PY_SSIZE_T_MAX); |
1590 | 0 | dictoffset += (Py_ssize_t)size; |
1591 | |
|
1592 | 0 | _PyObject_ASSERT(obj, dictoffset > 0); |
1593 | 0 | _PyObject_ASSERT(obj, dictoffset % SIZEOF_VOID_P == 0); |
1594 | 0 | } |
1595 | 122M | return (PyObject **) ((char *)obj + dictoffset); |
1596 | 176M | } |
1597 | | |
1598 | | /* Helper to get a pointer to an object's __dict__ slot, if any. |
1599 | | * Creates the dict from inline attributes if necessary. |
1600 | | * Does not set an exception. |
1601 | | * |
1602 | | * Note that the tp_dictoffset docs used to recommend this function, |
1603 | | * so it should be treated as part of the public API. |
1604 | | */ |
1605 | | PyObject ** |
1606 | | _PyObject_GetDictPtr(PyObject *obj) |
1607 | 0 | { |
1608 | 0 | if ((Py_TYPE(obj)->tp_flags & Py_TPFLAGS_MANAGED_DICT) == 0) { |
1609 | 0 | return _PyObject_ComputedDictPointer(obj); |
1610 | 0 | } |
1611 | 0 | PyDictObject *dict = _PyObject_GetManagedDict(obj); |
1612 | 0 | if (dict == NULL && Py_TYPE(obj)->tp_flags & Py_TPFLAGS_INLINE_VALUES) { |
1613 | 0 | dict = _PyObject_MaterializeManagedDict(obj); |
1614 | 0 | if (dict == NULL) { |
1615 | 0 | PyErr_Clear(); |
1616 | 0 | return NULL; |
1617 | 0 | } |
1618 | 0 | } |
1619 | 0 | return (PyObject **)&_PyObject_ManagedDictPointer(obj)->dict; |
1620 | 0 | } |
1621 | | |
1622 | | PyObject * |
1623 | | PyObject_SelfIter(PyObject *obj) |
1624 | 16.7M | { |
1625 | 16.7M | return Py_NewRef(obj); |
1626 | 16.7M | } |
1627 | | |
1628 | | /* Helper used when the __next__ method is removed from a type: |
1629 | | tp_iternext is never NULL and can be safely called without checking |
1630 | | on every iteration. |
1631 | | */ |
1632 | | |
1633 | | PyObject * |
1634 | | _PyObject_NextNotImplemented(PyObject *self) |
1635 | 0 | { |
1636 | 0 | PyErr_Format(PyExc_TypeError, |
1637 | 0 | "'%.200s' object is not iterable", |
1638 | 0 | Py_TYPE(self)->tp_name); |
1639 | 0 | return NULL; |
1640 | 0 | } |
1641 | | |
1642 | | |
1643 | | /* Specialized version of _PyObject_GenericGetAttrWithDict |
1644 | | specifically for the LOAD_METHOD opcode. |
1645 | | |
1646 | | Return 1 if a method is found, 0 if it's a regular attribute |
1647 | | from __dict__ or something returned by using a descriptor |
1648 | | protocol. |
1649 | | |
1650 | | `method` will point to the resolved attribute or NULL. In the |
1651 | | latter case, an error will be set. |
1652 | | */ |
1653 | | int |
1654 | | _PyObject_GetMethod(PyObject *obj, PyObject *name, PyObject **method) |
1655 | 0 | { |
1656 | 0 | int meth_found = 0; |
1657 | |
|
1658 | 0 | assert(*method == NULL); |
1659 | |
|
1660 | 0 | PyTypeObject *tp = Py_TYPE(obj); |
1661 | 0 | if (!_PyType_IsReady(tp)) { |
1662 | 0 | if (PyType_Ready(tp) < 0) { |
1663 | 0 | return 0; |
1664 | 0 | } |
1665 | 0 | } |
1666 | | |
1667 | 0 | if (tp->tp_getattro != PyObject_GenericGetAttr || !PyUnicode_CheckExact(name)) { |
1668 | 0 | *method = PyObject_GetAttr(obj, name); |
1669 | 0 | return 0; |
1670 | 0 | } |
1671 | | |
1672 | 0 | PyObject *descr = _PyType_LookupRef(tp, name); |
1673 | 0 | descrgetfunc f = NULL; |
1674 | 0 | if (descr != NULL) { |
1675 | 0 | if (_PyType_HasFeature(Py_TYPE(descr), Py_TPFLAGS_METHOD_DESCRIPTOR)) { |
1676 | 0 | meth_found = 1; |
1677 | 0 | } |
1678 | 0 | else { |
1679 | 0 | f = Py_TYPE(descr)->tp_descr_get; |
1680 | 0 | if (f != NULL && PyDescr_IsData(descr)) { |
1681 | 0 | *method = f(descr, obj, (PyObject *)Py_TYPE(obj)); |
1682 | 0 | Py_DECREF(descr); |
1683 | 0 | return 0; |
1684 | 0 | } |
1685 | 0 | } |
1686 | 0 | } |
1687 | 0 | PyObject *dict, *attr; |
1688 | 0 | if ((tp->tp_flags & Py_TPFLAGS_INLINE_VALUES) && |
1689 | 0 | _PyObject_TryGetInstanceAttribute(obj, name, &attr)) { |
1690 | 0 | if (attr != NULL) { |
1691 | 0 | *method = attr; |
1692 | 0 | Py_XDECREF(descr); |
1693 | 0 | return 0; |
1694 | 0 | } |
1695 | 0 | dict = NULL; |
1696 | 0 | } |
1697 | 0 | else if ((tp->tp_flags & Py_TPFLAGS_MANAGED_DICT)) { |
1698 | 0 | dict = (PyObject *)_PyObject_GetManagedDict(obj); |
1699 | 0 | } |
1700 | 0 | else { |
1701 | 0 | PyObject **dictptr = _PyObject_ComputedDictPointer(obj); |
1702 | 0 | if (dictptr != NULL) { |
1703 | 0 | dict = FT_ATOMIC_LOAD_PTR_ACQUIRE(*dictptr); |
1704 | 0 | } |
1705 | 0 | else { |
1706 | 0 | dict = NULL; |
1707 | 0 | } |
1708 | 0 | } |
1709 | 0 | if (dict != NULL) { |
1710 | 0 | Py_INCREF(dict); |
1711 | 0 | if (PyDict_GetItemRef(dict, name, method) != 0) { |
1712 | | // found or error |
1713 | 0 | Py_DECREF(dict); |
1714 | 0 | Py_XDECREF(descr); |
1715 | 0 | return 0; |
1716 | 0 | } |
1717 | | // not found |
1718 | 0 | Py_DECREF(dict); |
1719 | 0 | } |
1720 | | |
1721 | 0 | if (meth_found) { |
1722 | 0 | *method = descr; |
1723 | 0 | return 1; |
1724 | 0 | } |
1725 | | |
1726 | 0 | if (f != NULL) { |
1727 | 0 | *method = f(descr, obj, (PyObject *)Py_TYPE(obj)); |
1728 | 0 | Py_DECREF(descr); |
1729 | 0 | return 0; |
1730 | 0 | } |
1731 | | |
1732 | 0 | if (descr != NULL) { |
1733 | 0 | *method = descr; |
1734 | 0 | return 0; |
1735 | 0 | } |
1736 | | |
1737 | 0 | PyErr_Format(PyExc_AttributeError, |
1738 | 0 | "'%.100s' object has no attribute '%U'", |
1739 | 0 | tp->tp_name, name); |
1740 | |
|
1741 | 0 | _PyObject_SetAttributeErrorContext(obj, name); |
1742 | 0 | return 0; |
1743 | 0 | } |
1744 | | |
1745 | | // Look up a method on `self` by `name`. |
1746 | | // |
1747 | | // On success, `*method` is set and the function returns 0 or 1. If the |
1748 | | // return value is 1, the call is an unbound method and `*self` is the |
1749 | | // "self" or "cls" argument to pass. If the return value is 0, the call is |
1750 | | // a regular function and `*self` is cleared. |
1751 | | // |
1752 | | // On error, returns -1, clears `*self`, and sets an exception. |
1753 | | int |
1754 | | _PyObject_GetMethodStackRef(PyThreadState *ts, _PyStackRef *self, |
1755 | | PyObject *name, _PyStackRef *method) |
1756 | 93.4M | { |
1757 | 93.4M | int meth_found = 0; |
1758 | 93.4M | PyObject *obj = PyStackRef_AsPyObjectBorrow(*self); |
1759 | | |
1760 | 93.4M | assert(PyStackRef_IsNull(*method)); |
1761 | | |
1762 | 93.4M | PyTypeObject *tp = Py_TYPE(obj); |
1763 | 93.4M | if (!_PyType_IsReady(tp)) { |
1764 | 0 | if (PyType_Ready(tp) < 0) { |
1765 | 0 | PyStackRef_CLEAR(*self); |
1766 | 0 | return -1; |
1767 | 0 | } |
1768 | 0 | } |
1769 | | |
1770 | 93.4M | if (tp->tp_getattro != PyObject_GenericGetAttr || !PyUnicode_CheckExact(name)) { |
1771 | 31.8M | PyObject *res = PyObject_GetAttr(obj, name); |
1772 | 31.8M | PyStackRef_CLEAR(*self); |
1773 | 31.8M | if (res != NULL) { |
1774 | 31.8M | *method = PyStackRef_FromPyObjectSteal(res); |
1775 | 31.8M | return 0; |
1776 | 31.8M | } |
1777 | 5.58k | return -1; |
1778 | 31.8M | } |
1779 | | |
1780 | 61.6M | _PyType_LookupStackRefAndVersion(tp, name, method); |
1781 | 61.6M | PyObject *descr = PyStackRef_AsPyObjectBorrow(*method); |
1782 | 61.6M | descrgetfunc f = NULL; |
1783 | 61.6M | if (descr != NULL) { |
1784 | 61.6M | if (_PyType_HasFeature(Py_TYPE(descr), Py_TPFLAGS_METHOD_DESCRIPTOR)) { |
1785 | 60.8M | meth_found = 1; |
1786 | 60.8M | } |
1787 | 780k | else { |
1788 | 780k | f = Py_TYPE(descr)->tp_descr_get; |
1789 | 780k | if (f != NULL && PyDescr_IsData(descr)) { |
1790 | 34.7k | PyObject *value = f(descr, obj, (PyObject *)Py_TYPE(obj)); |
1791 | 34.7k | PyStackRef_CLEAR(*method); |
1792 | 34.7k | PyStackRef_CLEAR(*self); |
1793 | 34.7k | if (value != NULL) { |
1794 | 34.7k | *method = PyStackRef_FromPyObjectSteal(value); |
1795 | 34.7k | return 0; |
1796 | 34.7k | } |
1797 | 0 | return -1; |
1798 | 34.7k | } |
1799 | 780k | } |
1800 | 61.6M | } |
1801 | 61.6M | PyObject *dict, *attr; |
1802 | 61.6M | if ((tp->tp_flags & Py_TPFLAGS_INLINE_VALUES) && |
1803 | 26.8M | _PyObject_TryGetInstanceAttribute(obj, name, &attr)) { |
1804 | 1.23M | if (attr != NULL) { |
1805 | 273 | PyStackRef_XSETREF(*method, PyStackRef_FromPyObjectSteal(attr)); |
1806 | 273 | PyStackRef_CLEAR(*self); |
1807 | 273 | return 0; |
1808 | 273 | } |
1809 | 1.23M | dict = NULL; |
1810 | 1.23M | } |
1811 | 60.3M | else if ((tp->tp_flags & Py_TPFLAGS_MANAGED_DICT)) { |
1812 | 27.4M | dict = (PyObject *)_PyObject_GetManagedDict(obj); |
1813 | 27.4M | } |
1814 | 32.9M | else { |
1815 | 32.9M | PyObject **dictptr = _PyObject_ComputedDictPointer(obj); |
1816 | 32.9M | if (dictptr != NULL) { |
1817 | 31.4M | dict = FT_ATOMIC_LOAD_PTR_ACQUIRE(*dictptr); |
1818 | 31.4M | } |
1819 | 1.51M | else { |
1820 | 1.51M | dict = NULL; |
1821 | 1.51M | } |
1822 | 32.9M | } |
1823 | 61.6M | if (dict != NULL) { |
1824 | 58.2M | assert(PyUnicode_CheckExact(name)); |
1825 | 58.2M | int found = _PyDict_GetMethodStackRef((PyDictObject *)dict, name, method); |
1826 | 58.2M | if (found < 0) { |
1827 | 0 | assert(PyStackRef_IsNull(*method)); |
1828 | 0 | PyStackRef_CLEAR(*self); |
1829 | 0 | return -1; |
1830 | 0 | } |
1831 | 58.2M | else if (found) { |
1832 | 23 | PyStackRef_CLEAR(*self); |
1833 | 23 | return 0; |
1834 | 23 | } |
1835 | 58.2M | } |
1836 | | |
1837 | 61.6M | if (meth_found) { |
1838 | 60.8M | assert(!PyStackRef_IsNull(*method)); |
1839 | 60.8M | return 1; |
1840 | 60.8M | } |
1841 | | |
1842 | 745k | if (f != NULL) { |
1843 | 525k | if (Py_IS_TYPE(descr, &PyClassMethod_Type)) { |
1844 | 510k | PyObject *callable = _PyClassMethod_GetFunc(descr); |
1845 | 510k | PyStackRef_XSETREF(*method, PyStackRef_FromPyObjectNew(callable)); |
1846 | 510k | PyStackRef_XSETREF(*self, PyStackRef_FromPyObjectNew((PyObject *)tp)); |
1847 | 510k | return 1; |
1848 | 510k | } |
1849 | 15.0k | else if (Py_IS_TYPE(descr, &PyStaticMethod_Type)) { |
1850 | 15.0k | PyObject *callable = _PyStaticMethod_GetFunc(descr); |
1851 | 15.0k | PyStackRef_XSETREF(*method, PyStackRef_FromPyObjectNew(callable)); |
1852 | 15.0k | PyStackRef_CLEAR(*self); |
1853 | 15.0k | return 0; |
1854 | 15.0k | } |
1855 | 0 | PyObject *value = f(descr, obj, (PyObject *)tp); |
1856 | 0 | PyStackRef_CLEAR(*method); |
1857 | 0 | PyStackRef_CLEAR(*self); |
1858 | 0 | if (value) { |
1859 | 0 | *method = PyStackRef_FromPyObjectSteal(value); |
1860 | 0 | return 0; |
1861 | 0 | } |
1862 | 0 | return -1; |
1863 | 0 | } |
1864 | | |
1865 | 220k | if (descr != NULL) { |
1866 | 220k | assert(!PyStackRef_IsNull(*method)); |
1867 | 220k | PyStackRef_CLEAR(*self); |
1868 | 220k | return 0; |
1869 | 220k | } |
1870 | | |
1871 | 5 | PyErr_Format(PyExc_AttributeError, |
1872 | 5 | "'%.100s' object has no attribute '%U'", |
1873 | 5 | tp->tp_name, name); |
1874 | | |
1875 | 5 | _PyObject_SetAttributeErrorContext(obj, name); |
1876 | 5 | assert(PyStackRef_IsNull(*method)); |
1877 | 5 | PyStackRef_CLEAR(*self); |
1878 | 5 | return -1; |
1879 | 220k | } |
1880 | | |
1881 | | |
1882 | | /* Generic GetAttr functions - put these in your tp_[gs]etattro slot. */ |
1883 | | |
1884 | | PyObject * |
1885 | | _PyObject_GenericGetAttrWithDict(PyObject *obj, PyObject *name, |
1886 | | PyObject *dict, int suppress) |
1887 | 343M | { |
1888 | | /* Make sure the logic of _PyObject_GetMethod is in sync with |
1889 | | this method. |
1890 | | |
1891 | | When suppress=1, this function suppresses AttributeError. |
1892 | | */ |
1893 | | |
1894 | 343M | PyTypeObject *tp = Py_TYPE(obj); |
1895 | 343M | PyObject *descr = NULL; |
1896 | 343M | PyObject *res = NULL; |
1897 | 343M | descrgetfunc f; |
1898 | | |
1899 | 343M | if (!PyUnicode_Check(name)){ |
1900 | 0 | PyErr_Format(PyExc_TypeError, |
1901 | 0 | "attribute name must be string, not '%.200s'", |
1902 | 0 | Py_TYPE(name)->tp_name); |
1903 | 0 | return NULL; |
1904 | 0 | } |
1905 | | |
1906 | 343M | if (!_PyType_IsReady(tp)) { |
1907 | 0 | if (PyType_Ready(tp) < 0) |
1908 | 0 | return NULL; |
1909 | 0 | } |
1910 | | |
1911 | 343M | Py_INCREF(name); |
1912 | | |
1913 | 343M | PyThreadState *tstate = _PyThreadState_GET(); |
1914 | 343M | _PyCStackRef cref; |
1915 | 343M | _PyThreadState_PushCStackRef(tstate, &cref); |
1916 | | |
1917 | 343M | _PyType_LookupStackRefAndVersion(tp, name, &cref.ref); |
1918 | 343M | descr = PyStackRef_AsPyObjectBorrow(cref.ref); |
1919 | | |
1920 | 343M | f = NULL; |
1921 | 343M | if (descr != NULL) { |
1922 | 219M | f = Py_TYPE(descr)->tp_descr_get; |
1923 | 219M | if (f != NULL && PyDescr_IsData(descr)) { |
1924 | 77.1M | res = f(descr, obj, (PyObject *)Py_TYPE(obj)); |
1925 | 77.1M | if (res == NULL && suppress && |
1926 | 24.7k | PyErr_ExceptionMatches(PyExc_AttributeError)) { |
1927 | 24.7k | PyErr_Clear(); |
1928 | 24.7k | } |
1929 | 77.1M | goto done; |
1930 | 77.1M | } |
1931 | 219M | } |
1932 | 266M | if (dict == NULL) { |
1933 | 266M | if ((tp->tp_flags & Py_TPFLAGS_INLINE_VALUES)) { |
1934 | 140M | if (PyUnicode_CheckExact(name) && |
1935 | 140M | _PyObject_TryGetInstanceAttribute(obj, name, &res)) { |
1936 | 63.1M | if (res != NULL) { |
1937 | 24.2M | goto done; |
1938 | 24.2M | } |
1939 | 63.1M | } |
1940 | 77.7M | else { |
1941 | 77.7M | dict = (PyObject *)_PyObject_MaterializeManagedDict(obj); |
1942 | 77.7M | if (dict == NULL) { |
1943 | 0 | res = NULL; |
1944 | 0 | goto done; |
1945 | 0 | } |
1946 | 77.7M | } |
1947 | 140M | } |
1948 | 125M | else if ((tp->tp_flags & Py_TPFLAGS_MANAGED_DICT)) { |
1949 | 4.32M | dict = (PyObject *)_PyObject_GetManagedDict(obj); |
1950 | 4.32M | } |
1951 | 120M | else { |
1952 | 120M | PyObject **dictptr = _PyObject_ComputedDictPointer(obj); |
1953 | 120M | if (dictptr) { |
1954 | | #ifdef Py_GIL_DISABLED |
1955 | | dict = _Py_atomic_load_ptr_acquire(dictptr); |
1956 | | #else |
1957 | 68.1M | dict = *dictptr; |
1958 | 68.1M | #endif |
1959 | 68.1M | } |
1960 | 120M | } |
1961 | 266M | } |
1962 | 241M | if (dict != NULL) { |
1963 | 149M | Py_INCREF(dict); |
1964 | 149M | int rc = PyDict_GetItemRef(dict, name, &res); |
1965 | 149M | Py_DECREF(dict); |
1966 | 149M | if (res != NULL) { |
1967 | 121M | goto done; |
1968 | 121M | } |
1969 | 28.1M | else if (rc < 0) { |
1970 | 0 | if (suppress && PyErr_ExceptionMatches(PyExc_AttributeError)) { |
1971 | 0 | PyErr_Clear(); |
1972 | 0 | } |
1973 | 0 | else { |
1974 | 0 | goto done; |
1975 | 0 | } |
1976 | 0 | } |
1977 | 149M | } |
1978 | | |
1979 | 120M | if (f != NULL) { |
1980 | 70.7M | res = f(descr, obj, (PyObject *)Py_TYPE(obj)); |
1981 | 70.7M | if (res == NULL && suppress && |
1982 | 0 | PyErr_ExceptionMatches(PyExc_AttributeError)) { |
1983 | 0 | PyErr_Clear(); |
1984 | 0 | } |
1985 | 70.7M | goto done; |
1986 | 70.7M | } |
1987 | | |
1988 | 49.9M | if (descr != NULL) { |
1989 | 30.1M | res = PyStackRef_AsPyObjectSteal(cref.ref); |
1990 | 30.1M | cref.ref = PyStackRef_NULL; |
1991 | 30.1M | goto done; |
1992 | 30.1M | } |
1993 | | |
1994 | 19.8M | if (!suppress) { |
1995 | 2.17M | PyErr_Format(PyExc_AttributeError, |
1996 | 2.17M | "'%.100s' object has no attribute '%U'", |
1997 | 2.17M | tp->tp_name, name); |
1998 | | |
1999 | 2.17M | _PyObject_SetAttributeErrorContext(obj, name); |
2000 | 2.17M | } |
2001 | 343M | done: |
2002 | 343M | _PyThreadState_PopCStackRef(tstate, &cref); |
2003 | 343M | Py_DECREF(name); |
2004 | 343M | return res; |
2005 | 19.8M | } |
2006 | | |
2007 | | PyObject * |
2008 | | PyObject_GenericGetAttr(PyObject *obj, PyObject *name) |
2009 | 245M | { |
2010 | 245M | return _PyObject_GenericGetAttrWithDict(obj, name, NULL, 0); |
2011 | 245M | } |
2012 | | |
2013 | | int |
2014 | | _PyObject_GenericSetAttrWithDict(PyObject *obj, PyObject *name, |
2015 | | PyObject *value, PyObject *dict) |
2016 | 72.2M | { |
2017 | 72.2M | PyTypeObject *tp = Py_TYPE(obj); |
2018 | 72.2M | PyObject *descr; |
2019 | 72.2M | descrsetfunc f; |
2020 | 72.2M | int res = -1; |
2021 | | |
2022 | 72.2M | assert(!PyType_IsSubtype(tp, &PyType_Type)); |
2023 | 72.2M | if (!PyUnicode_Check(name)){ |
2024 | 0 | PyErr_Format(PyExc_TypeError, |
2025 | 0 | "attribute name must be string, not '%.200s'", |
2026 | 0 | Py_TYPE(name)->tp_name); |
2027 | 0 | return -1; |
2028 | 0 | } |
2029 | | |
2030 | 72.2M | if (!_PyType_IsReady(tp) && PyType_Ready(tp) < 0) { |
2031 | 0 | return -1; |
2032 | 0 | } |
2033 | | |
2034 | 72.2M | Py_INCREF(name); |
2035 | 72.2M | Py_INCREF(tp); |
2036 | | |
2037 | 72.2M | PyThreadState *tstate = _PyThreadState_GET(); |
2038 | 72.2M | _PyCStackRef cref; |
2039 | 72.2M | _PyThreadState_PushCStackRef(tstate, &cref); |
2040 | | |
2041 | 72.2M | _PyType_LookupStackRefAndVersion(tp, name, &cref.ref); |
2042 | 72.2M | descr = PyStackRef_AsPyObjectBorrow(cref.ref); |
2043 | | |
2044 | 72.2M | if (descr != NULL) { |
2045 | 36.9M | f = Py_TYPE(descr)->tp_descr_set; |
2046 | 36.9M | if (f != NULL) { |
2047 | 9.20M | res = f(descr, obj, value); |
2048 | 9.20M | goto done; |
2049 | 9.20M | } |
2050 | 36.9M | } |
2051 | | |
2052 | 63.0M | if (dict == NULL) { |
2053 | 62.4M | PyObject **dictptr; |
2054 | | |
2055 | 62.4M | if ((tp->tp_flags & Py_TPFLAGS_INLINE_VALUES)) { |
2056 | 39.3M | res = _PyObject_StoreInstanceAttribute(obj, name, value); |
2057 | 39.3M | goto error_check; |
2058 | 39.3M | } |
2059 | | |
2060 | 23.0M | if ((tp->tp_flags & Py_TPFLAGS_MANAGED_DICT)) { |
2061 | 54.0k | PyManagedDictPointer *managed_dict = _PyObject_ManagedDictPointer(obj); |
2062 | 54.0k | dictptr = (PyObject **)&managed_dict->dict; |
2063 | 54.0k | } |
2064 | 22.9M | else { |
2065 | 22.9M | dictptr = _PyObject_ComputedDictPointer(obj); |
2066 | 22.9M | } |
2067 | 23.0M | if (dictptr == NULL) { |
2068 | 8 | if (descr == NULL) { |
2069 | 8 | if (tp->tp_setattro == PyObject_GenericSetAttr) { |
2070 | 8 | PyErr_Format(PyExc_AttributeError, |
2071 | 8 | "'%.100s' object has no attribute '%U' and no " |
2072 | 8 | "__dict__ for setting new attributes", |
2073 | 8 | tp->tp_name, name); |
2074 | 8 | } |
2075 | 0 | else { |
2076 | 0 | PyErr_Format(PyExc_AttributeError, |
2077 | 0 | "'%.100s' object has no attribute '%U'", |
2078 | 0 | tp->tp_name, name); |
2079 | 0 | } |
2080 | 8 | _PyObject_SetAttributeErrorContext(obj, name); |
2081 | 8 | } |
2082 | 0 | else { |
2083 | 0 | PyErr_Format(PyExc_AttributeError, |
2084 | 0 | "'%.100s' object attribute '%U' is read-only", |
2085 | 0 | tp->tp_name, name); |
2086 | 0 | } |
2087 | 8 | goto done; |
2088 | 8 | } |
2089 | 23.0M | else { |
2090 | 23.0M | res = _PyObjectDict_SetItem(tp, obj, dictptr, name, value); |
2091 | 23.0M | } |
2092 | 23.0M | } |
2093 | 608k | else { |
2094 | 608k | Py_INCREF(dict); |
2095 | 608k | if (value == NULL) |
2096 | 0 | res = PyDict_DelItem(dict, name); |
2097 | 608k | else |
2098 | 608k | res = PyDict_SetItem(dict, name, value); |
2099 | 608k | Py_DECREF(dict); |
2100 | 608k | } |
2101 | 63.0M | error_check: |
2102 | 63.0M | if (res < 0 && PyErr_ExceptionMatches(PyExc_KeyError)) { |
2103 | 0 | PyErr_Format(PyExc_AttributeError, |
2104 | 0 | "'%.100s' object has no attribute '%U'", |
2105 | 0 | tp->tp_name, name); |
2106 | 0 | _PyObject_SetAttributeErrorContext(obj, name); |
2107 | 0 | } |
2108 | 72.2M | done: |
2109 | 72.2M | _PyThreadState_PopCStackRef(tstate, &cref); |
2110 | 72.2M | Py_DECREF(tp); |
2111 | 72.2M | Py_DECREF(name); |
2112 | 72.2M | return res; |
2113 | 63.0M | } |
2114 | | |
2115 | | int |
2116 | | PyObject_GenericSetAttr(PyObject *obj, PyObject *name, PyObject *value) |
2117 | 71.6M | { |
2118 | 71.6M | return _PyObject_GenericSetAttrWithDict(obj, name, value, NULL); |
2119 | 71.6M | } |
2120 | | |
2121 | | int |
2122 | | PyObject_GenericSetDict(PyObject *obj, PyObject *value, void *context) |
2123 | 0 | { |
2124 | 0 | if (value == NULL) { |
2125 | 0 | PyErr_SetString(PyExc_TypeError, "cannot delete __dict__"); |
2126 | 0 | return -1; |
2127 | 0 | } |
2128 | 0 | return _PyObject_SetDict(obj, value); |
2129 | 0 | } |
2130 | | |
2131 | | |
2132 | | /* Test a value used as condition, e.g., in a while or if statement. |
2133 | | Return -1 if an error occurred */ |
2134 | | |
2135 | | int |
2136 | | PyObject_IsTrue(PyObject *v) |
2137 | 127M | { |
2138 | 127M | Py_ssize_t res; |
2139 | 127M | if (v == Py_True) |
2140 | 12.7M | return 1; |
2141 | 114M | if (v == Py_False) |
2142 | 31.2M | return 0; |
2143 | 83.3M | if (v == Py_None) |
2144 | 4.05M | return 0; |
2145 | 79.3M | else if (Py_TYPE(v)->tp_as_number != NULL && |
2146 | 65.7M | Py_TYPE(v)->tp_as_number->nb_bool != NULL) |
2147 | 454k | res = (*Py_TYPE(v)->tp_as_number->nb_bool)(v); |
2148 | 78.8M | else if (Py_TYPE(v)->tp_as_mapping != NULL && |
2149 | 71.9M | Py_TYPE(v)->tp_as_mapping->mp_length != NULL) |
2150 | 30.4M | res = (*Py_TYPE(v)->tp_as_mapping->mp_length)(v); |
2151 | 48.4M | else if (Py_TYPE(v)->tp_as_sequence != NULL && |
2152 | 41.5M | Py_TYPE(v)->tp_as_sequence->sq_length != NULL) |
2153 | 38.9M | res = (*Py_TYPE(v)->tp_as_sequence->sq_length)(v); |
2154 | 9.53M | else |
2155 | 9.53M | return 1; |
2156 | | /* if it is negative, it should be either -1 or -2 */ |
2157 | 69.8M | return (res > 0) ? 1 : Py_SAFE_DOWNCAST(res, Py_ssize_t, int); |
2158 | 83.3M | } |
2159 | | |
2160 | | /* equivalent of 'not v' |
2161 | | Return -1 if an error occurred */ |
2162 | | |
2163 | | int |
2164 | | PyObject_Not(PyObject *v) |
2165 | 0 | { |
2166 | 0 | int res; |
2167 | 0 | res = PyObject_IsTrue(v); |
2168 | 0 | if (res < 0) |
2169 | 0 | return res; |
2170 | 0 | return res == 0; |
2171 | 0 | } |
2172 | | |
2173 | | /* Test whether an object can be called */ |
2174 | | |
2175 | | int |
2176 | | PyCallable_Check(PyObject *x) |
2177 | 12.2M | { |
2178 | 12.2M | if (x == NULL) |
2179 | 0 | return 0; |
2180 | 12.2M | return Py_TYPE(x)->tp_call != NULL; |
2181 | 12.2M | } |
2182 | | |
2183 | | |
2184 | | /* Helper for PyObject_Dir without arguments: returns the local scope. */ |
2185 | | static PyObject * |
2186 | | _dir_locals(void) |
2187 | 4 | { |
2188 | 4 | PyObject *names; |
2189 | 4 | PyObject *locals; |
2190 | | |
2191 | 4 | if (_PyEval_GetFrame() != NULL) { |
2192 | 4 | locals = _PyEval_GetFrameLocals(); |
2193 | 4 | } |
2194 | 0 | else { |
2195 | 0 | PyThreadState *tstate = _PyThreadState_GET(); |
2196 | 0 | locals = _PyEval_GetGlobalsFromRunningMain(tstate); |
2197 | 0 | if (locals == NULL) { |
2198 | 0 | if (!_PyErr_Occurred(tstate)) { |
2199 | 0 | locals = _PyEval_GetFrameLocals(); |
2200 | 0 | assert(_PyErr_Occurred(tstate)); |
2201 | 0 | } |
2202 | 0 | } |
2203 | 0 | else { |
2204 | 0 | Py_INCREF(locals); |
2205 | 0 | } |
2206 | 0 | } |
2207 | 4 | if (locals == NULL) { |
2208 | 0 | return NULL; |
2209 | 0 | } |
2210 | | |
2211 | 4 | names = PyMapping_Keys(locals); |
2212 | 4 | Py_DECREF(locals); |
2213 | 4 | if (!names) { |
2214 | 0 | return NULL; |
2215 | 0 | } |
2216 | 4 | if (!PyList_Check(names)) { |
2217 | 0 | PyErr_Format(PyExc_TypeError, |
2218 | 0 | "dir(): expected keys() of locals to be a list, " |
2219 | 0 | "not '%.200s'", Py_TYPE(names)->tp_name); |
2220 | 0 | Py_DECREF(names); |
2221 | 0 | return NULL; |
2222 | 0 | } |
2223 | 4 | if (PyList_Sort(names)) { |
2224 | 0 | Py_DECREF(names); |
2225 | 0 | return NULL; |
2226 | 0 | } |
2227 | 4 | return names; |
2228 | 4 | } |
2229 | | |
2230 | | /* Helper for PyObject_Dir: object introspection. */ |
2231 | | static PyObject * |
2232 | | _dir_object(PyObject *obj) |
2233 | 6.09k | { |
2234 | 6.09k | PyObject *result, *sorted; |
2235 | 6.09k | PyObject *dirfunc = _PyObject_LookupSpecial(obj, &_Py_ID(__dir__)); |
2236 | | |
2237 | 6.09k | assert(obj != NULL); |
2238 | 6.09k | if (dirfunc == NULL) { |
2239 | 0 | if (!PyErr_Occurred()) |
2240 | 0 | PyErr_SetString(PyExc_TypeError, "object does not provide __dir__"); |
2241 | 0 | return NULL; |
2242 | 0 | } |
2243 | | /* use __dir__ */ |
2244 | 6.09k | result = _PyObject_CallNoArgs(dirfunc); |
2245 | 6.09k | Py_DECREF(dirfunc); |
2246 | 6.09k | if (result == NULL) |
2247 | 0 | return NULL; |
2248 | | /* return sorted(result) */ |
2249 | 6.09k | sorted = PySequence_List(result); |
2250 | 6.09k | Py_DECREF(result); |
2251 | 6.09k | if (sorted == NULL) |
2252 | 0 | return NULL; |
2253 | 6.09k | if (PyList_Sort(sorted)) { |
2254 | 0 | Py_DECREF(sorted); |
2255 | 0 | return NULL; |
2256 | 0 | } |
2257 | 6.09k | return sorted; |
2258 | 6.09k | } |
2259 | | |
2260 | | /* Implementation of dir() -- if obj is NULL, returns the names in the current |
2261 | | (local) scope. Otherwise, performs introspection of the object: returns a |
2262 | | sorted list of attribute names (supposedly) accessible from the object |
2263 | | */ |
2264 | | PyObject * |
2265 | | PyObject_Dir(PyObject *obj) |
2266 | 6.10k | { |
2267 | 6.10k | return (obj == NULL) ? _dir_locals() : _dir_object(obj); |
2268 | 6.10k | } |
2269 | | |
2270 | | /* |
2271 | | None is a non-NULL undefined value. |
2272 | | There is (and should be!) no way to create other objects of this type, |
2273 | | so there is exactly one (which is indestructible, by the way). |
2274 | | */ |
2275 | | |
2276 | | /* ARGSUSED */ |
2277 | | static PyObject * |
2278 | | none_repr(PyObject *op) |
2279 | 73.9k | { |
2280 | 73.9k | return PyUnicode_FromString("None"); |
2281 | 73.9k | } |
2282 | | |
2283 | | static void |
2284 | | none_dealloc(PyObject* none) |
2285 | 0 | { |
2286 | | /* This should never get called, but we also don't want to SEGV if |
2287 | | * we accidentally decref None out of existence. Instead, |
2288 | | * since None is an immortal object, re-set the reference count. |
2289 | | */ |
2290 | 0 | _Py_SetImmortal(none); |
2291 | 0 | } |
2292 | | |
2293 | | static PyObject * |
2294 | | none_new(PyTypeObject *type, PyObject *args, PyObject *kwargs) |
2295 | 0 | { |
2296 | 0 | if (PyTuple_GET_SIZE(args) || (kwargs && PyDict_GET_SIZE(kwargs))) { |
2297 | 0 | PyErr_SetString(PyExc_TypeError, "NoneType takes no arguments"); |
2298 | 0 | return NULL; |
2299 | 0 | } |
2300 | 0 | Py_RETURN_NONE; |
2301 | 0 | } |
2302 | | |
2303 | | static int |
2304 | | none_bool(PyObject *v) |
2305 | 0 | { |
2306 | 0 | return 0; |
2307 | 0 | } |
2308 | | |
2309 | | static Py_hash_t none_hash(PyObject *v) |
2310 | 81.9k | { |
2311 | 81.9k | return 0xFCA86420; |
2312 | 81.9k | } |
2313 | | |
2314 | | static PyNumberMethods none_as_number = { |
2315 | | 0, /* nb_add */ |
2316 | | 0, /* nb_subtract */ |
2317 | | 0, /* nb_multiply */ |
2318 | | 0, /* nb_remainder */ |
2319 | | 0, /* nb_divmod */ |
2320 | | 0, /* nb_power */ |
2321 | | 0, /* nb_negative */ |
2322 | | 0, /* nb_positive */ |
2323 | | 0, /* nb_absolute */ |
2324 | | none_bool, /* nb_bool */ |
2325 | | 0, /* nb_invert */ |
2326 | | 0, /* nb_lshift */ |
2327 | | 0, /* nb_rshift */ |
2328 | | 0, /* nb_and */ |
2329 | | 0, /* nb_xor */ |
2330 | | 0, /* nb_or */ |
2331 | | 0, /* nb_int */ |
2332 | | 0, /* nb_reserved */ |
2333 | | 0, /* nb_float */ |
2334 | | 0, /* nb_inplace_add */ |
2335 | | 0, /* nb_inplace_subtract */ |
2336 | | 0, /* nb_inplace_multiply */ |
2337 | | 0, /* nb_inplace_remainder */ |
2338 | | 0, /* nb_inplace_power */ |
2339 | | 0, /* nb_inplace_lshift */ |
2340 | | 0, /* nb_inplace_rshift */ |
2341 | | 0, /* nb_inplace_and */ |
2342 | | 0, /* nb_inplace_xor */ |
2343 | | 0, /* nb_inplace_or */ |
2344 | | 0, /* nb_floor_divide */ |
2345 | | 0, /* nb_true_divide */ |
2346 | | 0, /* nb_inplace_floor_divide */ |
2347 | | 0, /* nb_inplace_true_divide */ |
2348 | | 0, /* nb_index */ |
2349 | | }; |
2350 | | |
2351 | | PyDoc_STRVAR(none_doc, |
2352 | | "NoneType()\n" |
2353 | | "--\n\n" |
2354 | | "The type of the None singleton."); |
2355 | | |
2356 | | PyTypeObject _PyNone_Type = { |
2357 | | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
2358 | | "NoneType", |
2359 | | 0, |
2360 | | 0, |
2361 | | none_dealloc, /*tp_dealloc*/ |
2362 | | 0, /*tp_vectorcall_offset*/ |
2363 | | 0, /*tp_getattr*/ |
2364 | | 0, /*tp_setattr*/ |
2365 | | 0, /*tp_as_async*/ |
2366 | | none_repr, /*tp_repr*/ |
2367 | | &none_as_number, /*tp_as_number*/ |
2368 | | 0, /*tp_as_sequence*/ |
2369 | | 0, /*tp_as_mapping*/ |
2370 | | none_hash, /*tp_hash */ |
2371 | | 0, /*tp_call */ |
2372 | | 0, /*tp_str */ |
2373 | | 0, /*tp_getattro */ |
2374 | | 0, /*tp_setattro */ |
2375 | | 0, /*tp_as_buffer */ |
2376 | | Py_TPFLAGS_DEFAULT, /*tp_flags */ |
2377 | | none_doc, /*tp_doc */ |
2378 | | 0, /*tp_traverse */ |
2379 | | 0, /*tp_clear */ |
2380 | | _Py_BaseObject_RichCompare, /*tp_richcompare */ |
2381 | | 0, /*tp_weaklistoffset */ |
2382 | | 0, /*tp_iter */ |
2383 | | 0, /*tp_iternext */ |
2384 | | 0, /*tp_methods */ |
2385 | | 0, /*tp_members */ |
2386 | | 0, /*tp_getset */ |
2387 | | 0, /*tp_base */ |
2388 | | 0, /*tp_dict */ |
2389 | | 0, /*tp_descr_get */ |
2390 | | 0, /*tp_descr_set */ |
2391 | | 0, /*tp_dictoffset */ |
2392 | | 0, /*tp_init */ |
2393 | | 0, /*tp_alloc */ |
2394 | | none_new, /*tp_new */ |
2395 | | }; |
2396 | | |
2397 | | PyObject _Py_NoneStruct = _PyObject_HEAD_INIT(&_PyNone_Type); |
2398 | | |
2399 | | /* NotImplemented is an object that can be used to signal that an |
2400 | | operation is not implemented for the given type combination. */ |
2401 | | |
2402 | | static PyObject * |
2403 | | NotImplemented_repr(PyObject *op) |
2404 | 0 | { |
2405 | 0 | return PyUnicode_FromString("NotImplemented"); |
2406 | 0 | } |
2407 | | |
2408 | | static PyObject * |
2409 | | NotImplemented_reduce(PyObject *op, PyObject *Py_UNUSED(ignored)) |
2410 | 0 | { |
2411 | 0 | return PyUnicode_FromString("NotImplemented"); |
2412 | 0 | } |
2413 | | |
2414 | | static PyMethodDef notimplemented_methods[] = { |
2415 | | {"__reduce__", NotImplemented_reduce, METH_NOARGS, NULL}, |
2416 | | {NULL, NULL} |
2417 | | }; |
2418 | | |
2419 | | static PyObject * |
2420 | | notimplemented_new(PyTypeObject *type, PyObject *args, PyObject *kwargs) |
2421 | 0 | { |
2422 | 0 | if (PyTuple_GET_SIZE(args) || (kwargs && PyDict_GET_SIZE(kwargs))) { |
2423 | 0 | PyErr_SetString(PyExc_TypeError, "NotImplementedType takes no arguments"); |
2424 | 0 | return NULL; |
2425 | 0 | } |
2426 | 0 | Py_RETURN_NOTIMPLEMENTED; |
2427 | 0 | } |
2428 | | |
2429 | | static void |
2430 | | notimplemented_dealloc(PyObject *notimplemented) |
2431 | 0 | { |
2432 | | /* This should never get called, but we also don't want to SEGV if |
2433 | | * we accidentally decref NotImplemented out of existence. Instead, |
2434 | | * since Notimplemented is an immortal object, re-set the reference count. |
2435 | | */ |
2436 | 0 | _Py_SetImmortal(notimplemented); |
2437 | 0 | } |
2438 | | |
2439 | | static int |
2440 | | notimplemented_bool(PyObject *v) |
2441 | 0 | { |
2442 | 0 | PyErr_SetString(PyExc_TypeError, |
2443 | 0 | "NotImplemented should not be used in a boolean context"); |
2444 | 0 | return -1; |
2445 | 0 | } |
2446 | | |
2447 | | static PyNumberMethods notimplemented_as_number = { |
2448 | | .nb_bool = notimplemented_bool, |
2449 | | }; |
2450 | | |
2451 | | PyDoc_STRVAR(notimplemented_doc, |
2452 | | "NotImplementedType()\n" |
2453 | | "--\n\n" |
2454 | | "The type of the NotImplemented singleton."); |
2455 | | |
2456 | | PyTypeObject _PyNotImplemented_Type = { |
2457 | | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
2458 | | "NotImplementedType", |
2459 | | 0, |
2460 | | 0, |
2461 | | notimplemented_dealloc, /*tp_dealloc*/ /*never called*/ |
2462 | | 0, /*tp_vectorcall_offset*/ |
2463 | | 0, /*tp_getattr*/ |
2464 | | 0, /*tp_setattr*/ |
2465 | | 0, /*tp_as_async*/ |
2466 | | NotImplemented_repr, /*tp_repr*/ |
2467 | | ¬implemented_as_number, /*tp_as_number*/ |
2468 | | 0, /*tp_as_sequence*/ |
2469 | | 0, /*tp_as_mapping*/ |
2470 | | 0, /*tp_hash */ |
2471 | | 0, /*tp_call */ |
2472 | | 0, /*tp_str */ |
2473 | | 0, /*tp_getattro */ |
2474 | | 0, /*tp_setattro */ |
2475 | | 0, /*tp_as_buffer */ |
2476 | | Py_TPFLAGS_DEFAULT, /*tp_flags */ |
2477 | | notimplemented_doc, /*tp_doc */ |
2478 | | 0, /*tp_traverse */ |
2479 | | 0, /*tp_clear */ |
2480 | | 0, /*tp_richcompare */ |
2481 | | 0, /*tp_weaklistoffset */ |
2482 | | 0, /*tp_iter */ |
2483 | | 0, /*tp_iternext */ |
2484 | | notimplemented_methods, /*tp_methods */ |
2485 | | 0, /*tp_members */ |
2486 | | 0, /*tp_getset */ |
2487 | | 0, /*tp_base */ |
2488 | | 0, /*tp_dict */ |
2489 | | 0, /*tp_descr_get */ |
2490 | | 0, /*tp_descr_set */ |
2491 | | 0, /*tp_dictoffset */ |
2492 | | 0, /*tp_init */ |
2493 | | 0, /*tp_alloc */ |
2494 | | notimplemented_new, /*tp_new */ |
2495 | | }; |
2496 | | |
2497 | | PyObject _Py_NotImplementedStruct = _PyObject_HEAD_INIT(&_PyNotImplemented_Type); |
2498 | | |
2499 | | |
2500 | | PyStatus |
2501 | | _PyObject_InitState(PyInterpreterState *interp) |
2502 | 36 | { |
2503 | | #ifdef Py_TRACE_REFS |
2504 | | if (refchain_init(interp) < 0) { |
2505 | | return _PyStatus_NO_MEMORY(); |
2506 | | } |
2507 | | #endif |
2508 | 36 | return _PyStatus_OK(); |
2509 | 36 | } |
2510 | | |
2511 | | void |
2512 | | _PyObject_FiniState(PyInterpreterState *interp) |
2513 | 0 | { |
2514 | | #ifdef Py_TRACE_REFS |
2515 | | refchain_fini(interp); |
2516 | | #endif |
2517 | 0 | } |
2518 | | |
2519 | | |
2520 | | extern PyTypeObject _PyAnextAwaitable_Type; |
2521 | | extern PyTypeObject _PyLegacyEventHandler_Type; |
2522 | | extern PyTypeObject _PyLineIterator; |
2523 | | extern PyTypeObject _PyMemoryIter_Type; |
2524 | | extern PyTypeObject _PyPositionsIterator; |
2525 | | extern PyTypeObject _Py_GenericAliasIterType; |
2526 | | |
2527 | | static PyTypeObject* static_types[] = { |
2528 | | // The two most important base types: must be initialized first and |
2529 | | // deallocated last. |
2530 | | &PyBaseObject_Type, |
2531 | | &PyType_Type, |
2532 | | |
2533 | | // PyStaticMethod_Type and PyCFunction_Type are used by PyType_Ready() |
2534 | | // on other types and so must be initialized first. |
2535 | | &PyStaticMethod_Type, |
2536 | | &PyCFunction_Type, |
2537 | | |
2538 | | // Static types with base=&PyBaseObject_Type |
2539 | | &PyAsyncGen_Type, |
2540 | | &PyByteArrayIter_Type, |
2541 | | &PyByteArray_Type, |
2542 | | &PyBytesIter_Type, |
2543 | | &PyBytes_Type, |
2544 | | &PyCallIter_Type, |
2545 | | &PyCapsule_Type, |
2546 | | &PyCell_Type, |
2547 | | &PyClassMethodDescr_Type, |
2548 | | &PyClassMethod_Type, |
2549 | | &PyCode_Type, |
2550 | | &PyComplex_Type, |
2551 | | &PyContextToken_Type, |
2552 | | &PyContextVar_Type, |
2553 | | &PyContext_Type, |
2554 | | &PyCoro_Type, |
2555 | | &PyDictItems_Type, |
2556 | | &PyDictIterItem_Type, |
2557 | | &PyDictIterKey_Type, |
2558 | | &PyDictIterValue_Type, |
2559 | | &PyDictKeys_Type, |
2560 | | &PyDictProxy_Type, |
2561 | | &PyDictRevIterItem_Type, |
2562 | | &PyDictRevIterKey_Type, |
2563 | | &PyDictRevIterValue_Type, |
2564 | | &PyDictValues_Type, |
2565 | | &PyDict_Type, |
2566 | | &PyEllipsis_Type, |
2567 | | &PyEnum_Type, |
2568 | | &PyFilter_Type, |
2569 | | &PyFloat_Type, |
2570 | | &PyFrameLocalsProxy_Type, |
2571 | | &PyFrame_Type, |
2572 | | &PyFrozenDict_Type, |
2573 | | &PyFrozenSet_Type, |
2574 | | &PyFunction_Type, |
2575 | | &PyGen_Type, |
2576 | | &PyGetSetDescr_Type, |
2577 | | &PyInstanceMethod_Type, |
2578 | | &PyLazyImport_Type, |
2579 | | &PyListIter_Type, |
2580 | | &PyListRevIter_Type, |
2581 | | &PyList_Type, |
2582 | | &PyLongRangeIter_Type, |
2583 | | &PyLong_Type, |
2584 | | &PyMap_Type, |
2585 | | &PyMemberDescr_Type, |
2586 | | &PyMemoryView_Type, |
2587 | | &PyMethodDescr_Type, |
2588 | | &PyMethod_Type, |
2589 | | &PyModuleDef_Type, |
2590 | | &PyModule_Type, |
2591 | | &PyODictIter_Type, |
2592 | | &PyPickleBuffer_Type, |
2593 | | &PyProperty_Type, |
2594 | | &PyRangeIter_Type, |
2595 | | &PyRange_Type, |
2596 | | &PyReversed_Type, |
2597 | | &PySTEntry_Type, |
2598 | | &PySeqIter_Type, |
2599 | | &PySetIter_Type, |
2600 | | &PySet_Type, |
2601 | | &PySlice_Type, |
2602 | | &PyStdPrinter_Type, |
2603 | | &PySuper_Type, |
2604 | | &PyTraceBack_Type, |
2605 | | &PyTupleIter_Type, |
2606 | | &PyTuple_Type, |
2607 | | &PyUnicodeIter_Type, |
2608 | | &PyUnicode_Type, |
2609 | | &PyWrapperDescr_Type, |
2610 | | &PyZip_Type, |
2611 | | &Py_GenericAliasType, |
2612 | | &_PyAnextAwaitable_Type, |
2613 | | &_PyAsyncGenASend_Type, |
2614 | | &_PyAsyncGenAThrow_Type, |
2615 | | &_PyAsyncGenWrappedValue_Type, |
2616 | | &_PyBufferWrapper_Type, |
2617 | | &_PyContextTokenMissing_Type, |
2618 | | &_PyCoroWrapper_Type, |
2619 | | &_Py_GenericAliasIterType, |
2620 | | &_PyHamtItems_Type, |
2621 | | &_PyHamtKeys_Type, |
2622 | | &_PyHamtValues_Type, |
2623 | | &_PyHamt_ArrayNode_Type, |
2624 | | &_PyHamt_BitmapNode_Type, |
2625 | | &_PyHamt_CollisionNode_Type, |
2626 | | &_PyHamt_Type, |
2627 | | &_PyInstructionSequence_Type, |
2628 | | &_PyInterpolation_Type, |
2629 | | &_PyLegacyEventHandler_Type, |
2630 | | &_PyLineIterator, |
2631 | | &_PyManagedBuffer_Type, |
2632 | | &_PyMemoryIter_Type, |
2633 | | &_PyMethodWrapper_Type, |
2634 | | &_PyNamespace_Type, |
2635 | | &_PyNone_Type, |
2636 | | &_PyNotImplemented_Type, |
2637 | | &_PyPositionsIterator, |
2638 | | &_PyTemplate_Type, |
2639 | | &_PyTemplateIter_Type, |
2640 | | &_PyUnicodeASCIIIter_Type, |
2641 | | &_PyUnion_Type, |
2642 | | #ifdef _Py_TIER2 |
2643 | | &_PyUOpExecutor_Type, |
2644 | | #endif |
2645 | | &_PyWeakref_CallableProxyType, |
2646 | | &_PyWeakref_ProxyType, |
2647 | | &_PyWeakref_RefType, |
2648 | | &_PyTypeAlias_Type, |
2649 | | &_PyNoDefault_Type, |
2650 | | |
2651 | | // subclasses: _PyTypes_FiniTypes() deallocates them before their base |
2652 | | // class |
2653 | | &PyBool_Type, // base=&PyLong_Type |
2654 | | &PyCMethod_Type, // base=&PyCFunction_Type |
2655 | | &PyODictItems_Type, // base=&PyDictItems_Type |
2656 | | &PyODictKeys_Type, // base=&PyDictKeys_Type |
2657 | | &PyODictValues_Type, // base=&PyDictValues_Type |
2658 | | &PyODict_Type, // base=&PyDict_Type |
2659 | | }; |
2660 | | |
2661 | | |
2662 | | PyStatus |
2663 | | _PyTypes_InitTypes(PyInterpreterState *interp) |
2664 | 36 | { |
2665 | | // All other static types (unless initialized elsewhere) |
2666 | 4.28k | for (size_t i=0; i < Py_ARRAY_LENGTH(static_types); i++) { |
2667 | 4.24k | PyTypeObject *type = static_types[i]; |
2668 | 4.24k | if (_PyStaticType_InitBuiltin(interp, type) < 0) { |
2669 | 0 | return _PyStatus_ERR("Can't initialize builtin type"); |
2670 | 0 | } |
2671 | 4.24k | if (type == &PyType_Type) { |
2672 | | // Sanitify checks of the two most important types |
2673 | 36 | assert(PyBaseObject_Type.tp_base == NULL); |
2674 | 36 | assert(PyType_Type.tp_base == &PyBaseObject_Type); |
2675 | 36 | } |
2676 | 4.24k | } |
2677 | | |
2678 | | // Cache __reduce__ from PyBaseObject_Type object |
2679 | 36 | PyObject *baseobj_dict = _PyType_GetDict(&PyBaseObject_Type); |
2680 | 36 | PyObject *baseobj_reduce = PyDict_GetItemWithError(baseobj_dict, &_Py_ID(__reduce__)); |
2681 | 36 | if (baseobj_reduce == NULL && PyErr_Occurred()) { |
2682 | 0 | return _PyStatus_ERR("Can't get __reduce__ from base object"); |
2683 | 0 | } |
2684 | 36 | _Py_INTERP_CACHED_OBJECT(interp, objreduce) = baseobj_reduce; |
2685 | | |
2686 | | // Must be after static types are initialized |
2687 | 36 | if (_Py_initialize_generic(interp) < 0) { |
2688 | 0 | return _PyStatus_ERR("Can't initialize generic types"); |
2689 | 0 | } |
2690 | | |
2691 | 36 | return _PyStatus_OK(); |
2692 | 36 | } |
2693 | | |
2694 | | |
2695 | | // Best-effort function clearing static types. |
2696 | | // |
2697 | | // Don't deallocate a type if it still has subclasses. If a Py_Finalize() |
2698 | | // sub-function is interrupted by CTRL+C or fails with MemoryError, some |
2699 | | // subclasses are not cleared properly. Leave the static type unchanged in this |
2700 | | // case. |
2701 | | void |
2702 | | _PyTypes_FiniTypes(PyInterpreterState *interp) |
2703 | 0 | { |
2704 | | // Deallocate types in the reverse order to deallocate subclasses before |
2705 | | // their base classes. |
2706 | 0 | for (Py_ssize_t i=Py_ARRAY_LENGTH(static_types)-1; i>=0; i--) { |
2707 | 0 | PyTypeObject *type = static_types[i]; |
2708 | 0 | _PyStaticType_FiniBuiltin(interp, type); |
2709 | 0 | } |
2710 | 0 | } |
2711 | | |
2712 | | |
2713 | | static inline void |
2714 | | new_reference(PyObject *op) |
2715 | 2.80G | { |
2716 | | // Skip the immortal object check in Py_SET_REFCNT; always set refcnt to 1 |
2717 | 2.80G | #if !defined(Py_GIL_DISABLED) |
2718 | 2.80G | #if SIZEOF_VOID_P > 4 |
2719 | 2.80G | op->ob_refcnt_full = 1; |
2720 | 2.80G | assert(op->ob_refcnt == 1); |
2721 | 2.80G | assert(op->ob_flags == 0); |
2722 | | #else |
2723 | | op->ob_refcnt = 1; |
2724 | | #endif |
2725 | | #else |
2726 | | op->ob_flags = 0; |
2727 | | op->ob_mutex = (PyMutex){ 0 }; |
2728 | | #ifdef _Py_THREAD_SANITIZER |
2729 | | _Py_atomic_store_uintptr_relaxed(&op->ob_tid, _Py_ThreadId()); |
2730 | | _Py_atomic_store_uint8_relaxed(&op->ob_gc_bits, 0); |
2731 | | _Py_atomic_store_uint32_relaxed(&op->ob_ref_local, 1); |
2732 | | _Py_atomic_store_ssize_relaxed(&op->ob_ref_shared, 0); |
2733 | | #else |
2734 | | op->ob_tid = _Py_ThreadId(); |
2735 | | op->ob_gc_bits = 0; |
2736 | | op->ob_ref_local = 1; |
2737 | | op->ob_ref_shared = 0; |
2738 | | #endif |
2739 | | #endif |
2740 | | #ifdef Py_TRACE_REFS |
2741 | | _Py_AddToAllObjects(op); |
2742 | | #endif |
2743 | 2.80G | _PyReftracerTrack(op, PyRefTracer_CREATE); |
2744 | 2.80G | } |
2745 | | |
2746 | | void |
2747 | | _Py_NewReference(PyObject *op) |
2748 | 2.75G | { |
2749 | | #ifdef Py_REF_DEBUG |
2750 | | _Py_IncRefTotal(_PyThreadState_GET()); |
2751 | | #endif |
2752 | 2.75G | new_reference(op); |
2753 | 2.75G | } |
2754 | | |
2755 | | void |
2756 | | _Py_NewReferenceNoTotal(PyObject *op) |
2757 | 53.1M | { |
2758 | 53.1M | new_reference(op); |
2759 | 53.1M | } |
2760 | | |
2761 | | void |
2762 | | _Py_SetImmortalUntracked(PyObject *op) |
2763 | 307k | { |
2764 | | #ifdef Py_DEBUG |
2765 | | // For strings, use _PyUnicode_InternImmortal instead. |
2766 | | if (PyUnicode_CheckExact(op)) { |
2767 | | assert(PyUnicode_CHECK_INTERNED(op) == SSTATE_INTERNED_IMMORTAL |
2768 | | || PyUnicode_CHECK_INTERNED(op) == SSTATE_INTERNED_IMMORTAL_STATIC); |
2769 | | } |
2770 | | #endif |
2771 | | // Check if already immortal to avoid degrading from static immortal to plain immortal |
2772 | 307k | if (_Py_IsImmortal(op)) { |
2773 | 108 | return; |
2774 | 108 | } |
2775 | | #ifdef Py_GIL_DISABLED |
2776 | | op->ob_tid = _Py_UNOWNED_TID; |
2777 | | op->ob_ref_local = _Py_IMMORTAL_REFCNT_LOCAL; |
2778 | | op->ob_ref_shared = 0; |
2779 | | _Py_atomic_or_uint8(&op->ob_gc_bits, _PyGC_BITS_DEFERRED); |
2780 | | #elif SIZEOF_VOID_P > 4 |
2781 | 307k | op->ob_flags = _Py_IMMORTAL_FLAGS; |
2782 | 307k | op->ob_refcnt = _Py_IMMORTAL_INITIAL_REFCNT; |
2783 | | #else |
2784 | | op->ob_refcnt = _Py_IMMORTAL_INITIAL_REFCNT; |
2785 | | #endif |
2786 | 307k | } |
2787 | | |
2788 | | void |
2789 | | _Py_SetImmortal(PyObject *op) |
2790 | 307k | { |
2791 | 307k | if (PyObject_IS_GC(op) && _PyObject_GC_IS_TRACKED(op)) { |
2792 | 14.7k | _PyObject_GC_UNTRACK(op); |
2793 | 14.7k | } |
2794 | 307k | _Py_SetImmortalUntracked(op); |
2795 | 307k | } |
2796 | | |
2797 | | void |
2798 | | _PyObject_SetDeferredRefcount(PyObject *op) |
2799 | 234k | { |
2800 | | #ifdef Py_GIL_DISABLED |
2801 | | assert(PyType_IS_GC(Py_TYPE(op))); |
2802 | | assert(_Py_IsOwnedByCurrentThread(op)); |
2803 | | assert(op->ob_ref_shared == 0); |
2804 | | _PyObject_SET_GC_BITS(op, _PyGC_BITS_DEFERRED); |
2805 | | op->ob_ref_shared = _Py_REF_SHARED(_Py_REF_DEFERRED, 0); |
2806 | | #endif |
2807 | 234k | } |
2808 | | |
2809 | | int |
2810 | | PyUnstable_Object_EnableDeferredRefcount(PyObject *op) |
2811 | 233k | { |
2812 | | #ifdef Py_GIL_DISABLED |
2813 | | if (!PyType_IS_GC(Py_TYPE(op))) { |
2814 | | // Deferred reference counting doesn't work |
2815 | | // on untracked types. |
2816 | | return 0; |
2817 | | } |
2818 | | |
2819 | | uint8_t bits = _Py_atomic_load_uint8(&op->ob_gc_bits); |
2820 | | if ((bits & _PyGC_BITS_DEFERRED) != 0) |
2821 | | { |
2822 | | // Nothing to do. |
2823 | | return 0; |
2824 | | } |
2825 | | |
2826 | | if (_Py_atomic_compare_exchange_uint8(&op->ob_gc_bits, &bits, bits | _PyGC_BITS_DEFERRED) == 0) |
2827 | | { |
2828 | | // Someone beat us to it! |
2829 | | return 0; |
2830 | | } |
2831 | | _Py_atomic_add_ssize(&op->ob_ref_shared, _Py_REF_SHARED(_Py_REF_DEFERRED, 0)); |
2832 | | return 1; |
2833 | | #else |
2834 | 233k | return 0; |
2835 | 233k | #endif |
2836 | 233k | } |
2837 | | |
2838 | | int |
2839 | | PyUnstable_Object_IsUniqueReferencedTemporary(PyObject *op) |
2840 | 0 | { |
2841 | 0 | if (!_PyObject_IsUniquelyReferenced(op)) { |
2842 | 0 | return 0; |
2843 | 0 | } |
2844 | | |
2845 | 0 | _PyInterpreterFrame *frame = _PyEval_GetFrame(); |
2846 | 0 | if (frame == NULL) { |
2847 | 0 | return 0; |
2848 | 0 | } |
2849 | | |
2850 | 0 | _PyStackRef *base = _PyFrame_Stackbase(frame); |
2851 | 0 | _PyStackRef *stackpointer = frame->stackpointer; |
2852 | 0 | while (stackpointer > base) { |
2853 | 0 | stackpointer--; |
2854 | 0 | _PyStackRef ref = *stackpointer; |
2855 | 0 | if (PyStackRef_IsTaggedInt(ref)) { |
2856 | 0 | continue; |
2857 | 0 | } |
2858 | 0 | if (op == PyStackRef_AsPyObjectBorrow(ref)) { |
2859 | 0 | return PyStackRef_IsHeapSafe(ref); |
2860 | 0 | } |
2861 | 0 | } |
2862 | 0 | return 0; |
2863 | 0 | } |
2864 | | |
2865 | | int |
2866 | | PyUnstable_TryIncRef(PyObject *op) |
2867 | 0 | { |
2868 | 0 | return _Py_TryIncref(op); |
2869 | 0 | } |
2870 | | |
2871 | | void |
2872 | | PyUnstable_EnableTryIncRef(PyObject *op) |
2873 | 0 | { |
2874 | | #ifdef Py_GIL_DISABLED |
2875 | | _PyObject_SetMaybeWeakref(op); |
2876 | | #endif |
2877 | 0 | } |
2878 | | |
2879 | | int |
2880 | | PyUnstable_SetImmortal(PyObject *op) |
2881 | 0 | { |
2882 | 0 | assert(op != NULL); |
2883 | 0 | if (!_PyObject_IsUniquelyReferenced(op) || PyUnicode_Check(op)) { |
2884 | 0 | return 0; |
2885 | 0 | } |
2886 | 0 | _Py_SetImmortal(op); |
2887 | 0 | return 1; |
2888 | 0 | } |
2889 | | |
2890 | | void |
2891 | | _Py_ResurrectReference(PyObject *op) |
2892 | 0 | { |
2893 | | #ifdef Py_TRACE_REFS |
2894 | | _Py_AddToAllObjects(op); |
2895 | | #endif |
2896 | 0 | } |
2897 | | |
2898 | | void |
2899 | | _Py_ForgetReference(PyObject *op) |
2900 | 0 | { |
2901 | | #ifdef Py_TRACE_REFS |
2902 | | if (Py_REFCNT(op) < 0) { |
2903 | | _PyObject_ASSERT_FAILED_MSG(op, "negative refcnt"); |
2904 | | } |
2905 | | |
2906 | | PyInterpreterState *interp = _PyInterpreterState_GET(); |
2907 | | |
2908 | | #ifdef SLOW_UNREF_CHECK |
2909 | | if (!_PyRefchain_Get(interp, op)) { |
2910 | | /* Not found */ |
2911 | | _PyObject_ASSERT_FAILED_MSG(op, |
2912 | | "object not found in the objects list"); |
2913 | | } |
2914 | | #endif |
2915 | | |
2916 | | _PyRefchain_Remove(interp, op); |
2917 | | #endif |
2918 | 0 | } |
2919 | | |
2920 | | |
2921 | | #ifdef Py_TRACE_REFS |
2922 | | static int |
2923 | | _Py_PrintReference(_Py_hashtable_t *ht, |
2924 | | const void *key, const void *value, |
2925 | | void *user_data) |
2926 | | { |
2927 | | PyObject *op = (PyObject*)key; |
2928 | | FILE *fp = (FILE *)user_data; |
2929 | | fprintf(fp, "%p [%zd] ", (void *)op, Py_REFCNT(op)); |
2930 | | if (PyObject_Print(op, fp, 0) != 0) { |
2931 | | PyErr_Clear(); |
2932 | | } |
2933 | | putc('\n', fp); |
2934 | | return 0; |
2935 | | } |
2936 | | |
2937 | | |
2938 | | /* Print all live objects. Because PyObject_Print is called, the |
2939 | | * interpreter must be in a healthy state. |
2940 | | */ |
2941 | | void |
2942 | | _Py_PrintReferences(PyInterpreterState *interp, FILE *fp) |
2943 | | { |
2944 | | if (interp == NULL) { |
2945 | | interp = _PyInterpreterState_Main(); |
2946 | | } |
2947 | | fprintf(fp, "Remaining objects:\n"); |
2948 | | _Py_hashtable_foreach(REFCHAIN(interp), _Py_PrintReference, fp); |
2949 | | } |
2950 | | |
2951 | | |
2952 | | static int |
2953 | | _Py_PrintReferenceAddress(_Py_hashtable_t *ht, |
2954 | | const void *key, const void *value, |
2955 | | void *user_data) |
2956 | | { |
2957 | | PyObject *op = (PyObject*)key; |
2958 | | FILE *fp = (FILE *)user_data; |
2959 | | fprintf(fp, "%p [%zd] %s\n", |
2960 | | (void *)op, Py_REFCNT(op), Py_TYPE(op)->tp_name); |
2961 | | return 0; |
2962 | | } |
2963 | | |
2964 | | |
2965 | | /* Print the addresses of all live objects. Unlike _Py_PrintReferences, this |
2966 | | * doesn't make any calls to the Python C API, so is always safe to call. |
2967 | | */ |
2968 | | // XXX This function is not safe to use if the interpreter has been |
2969 | | // freed or is in an unhealthy state (e.g. late in finalization). |
2970 | | // The call in Py_FinalizeEx() is okay since the main interpreter |
2971 | | // is statically allocated. |
2972 | | void |
2973 | | _Py_PrintReferenceAddresses(PyInterpreterState *interp, FILE *fp) |
2974 | | { |
2975 | | fprintf(fp, "Remaining object addresses:\n"); |
2976 | | _Py_hashtable_foreach(REFCHAIN(interp), _Py_PrintReferenceAddress, fp); |
2977 | | } |
2978 | | |
2979 | | |
2980 | | typedef struct { |
2981 | | PyObject *self; |
2982 | | PyObject *args; |
2983 | | PyObject *list; |
2984 | | PyObject *type; |
2985 | | Py_ssize_t limit; |
2986 | | } _Py_GetObjectsData; |
2987 | | |
2988 | | enum { |
2989 | | _PY_GETOBJECTS_IGNORE = 0, |
2990 | | _PY_GETOBJECTS_ERROR = 1, |
2991 | | _PY_GETOBJECTS_STOP = 2, |
2992 | | }; |
2993 | | |
2994 | | static int |
2995 | | _Py_GetObject(_Py_hashtable_t *ht, |
2996 | | const void *key, const void *value, |
2997 | | void *user_data) |
2998 | | { |
2999 | | PyObject *op = (PyObject *)key; |
3000 | | _Py_GetObjectsData *data = user_data; |
3001 | | if (data->limit > 0) { |
3002 | | if (PyList_GET_SIZE(data->list) >= data->limit) { |
3003 | | return _PY_GETOBJECTS_STOP; |
3004 | | } |
3005 | | } |
3006 | | |
3007 | | if (op == data->self) { |
3008 | | return _PY_GETOBJECTS_IGNORE; |
3009 | | } |
3010 | | if (op == data->args) { |
3011 | | return _PY_GETOBJECTS_IGNORE; |
3012 | | } |
3013 | | if (op == data->list) { |
3014 | | return _PY_GETOBJECTS_IGNORE; |
3015 | | } |
3016 | | if (data->type != NULL) { |
3017 | | if (op == data->type) { |
3018 | | return _PY_GETOBJECTS_IGNORE; |
3019 | | } |
3020 | | if (!Py_IS_TYPE(op, (PyTypeObject *)data->type)) { |
3021 | | return _PY_GETOBJECTS_IGNORE; |
3022 | | } |
3023 | | } |
3024 | | |
3025 | | if (PyList_Append(data->list, op) < 0) { |
3026 | | return _PY_GETOBJECTS_ERROR; |
3027 | | } |
3028 | | return 0; |
3029 | | } |
3030 | | |
3031 | | |
3032 | | /* The implementation of sys.getobjects(). */ |
3033 | | PyObject * |
3034 | | _Py_GetObjects(PyObject *self, PyObject *args) |
3035 | | { |
3036 | | Py_ssize_t limit; |
3037 | | PyObject *type = NULL; |
3038 | | if (!PyArg_ParseTuple(args, "n|O", &limit, &type)) { |
3039 | | return NULL; |
3040 | | } |
3041 | | |
3042 | | PyObject *list = PyList_New(0); |
3043 | | if (list == NULL) { |
3044 | | return NULL; |
3045 | | } |
3046 | | |
3047 | | _Py_GetObjectsData data = { |
3048 | | .self = self, |
3049 | | .args = args, |
3050 | | .list = list, |
3051 | | .type = type, |
3052 | | .limit = limit, |
3053 | | }; |
3054 | | PyInterpreterState *interp = _PyInterpreterState_GET(); |
3055 | | int res = _Py_hashtable_foreach(REFCHAIN(interp), _Py_GetObject, &data); |
3056 | | if (res == _PY_GETOBJECTS_ERROR) { |
3057 | | Py_DECREF(list); |
3058 | | return NULL; |
3059 | | } |
3060 | | return list; |
3061 | | } |
3062 | | |
3063 | | #undef REFCHAIN |
3064 | | #undef REFCHAIN_VALUE |
3065 | | |
3066 | | #endif /* Py_TRACE_REFS */ |
3067 | | |
3068 | | |
3069 | | /* Hack to force loading of abstract.o */ |
3070 | | Py_ssize_t (*_Py_abstract_hack)(PyObject *) = PyObject_Size; |
3071 | | |
3072 | | |
3073 | | void |
3074 | | _PyObject_DebugTypeStats(FILE *out) |
3075 | 0 | { |
3076 | 0 | _PyDict_DebugMallocStats(out); |
3077 | 0 | _PyFloat_DebugMallocStats(out); |
3078 | 0 | _PyList_DebugMallocStats(out); |
3079 | 0 | _PyTuple_DebugMallocStats(out); |
3080 | 0 | } |
3081 | | |
3082 | | /* These methods are used to control infinite recursion in repr, str, print, |
3083 | | etc. Container objects that may recursively contain themselves, |
3084 | | e.g. builtin dictionaries and lists, should use Py_ReprEnter() and |
3085 | | Py_ReprLeave() to avoid infinite recursion. |
3086 | | |
3087 | | Py_ReprEnter() returns 0 the first time it is called for a particular |
3088 | | object and 1 every time thereafter. It returns -1 if an exception |
3089 | | occurred. Py_ReprLeave() has no return value. |
3090 | | |
3091 | | See dictobject.c and listobject.c for examples of use. |
3092 | | */ |
3093 | | |
3094 | | int |
3095 | | Py_ReprEnter(PyObject *obj) |
3096 | 3.72M | { |
3097 | 3.72M | PyObject *dict; |
3098 | 3.72M | PyObject *list; |
3099 | 3.72M | Py_ssize_t i; |
3100 | | |
3101 | 3.72M | dict = PyThreadState_GetDict(); |
3102 | | /* Ignore a missing thread-state, so that this function can be called |
3103 | | early on startup. */ |
3104 | 3.72M | if (dict == NULL) |
3105 | 0 | return 0; |
3106 | 3.72M | list = PyDict_GetItemWithError(dict, &_Py_ID(Py_Repr)); |
3107 | 3.72M | if (list == NULL) { |
3108 | 4 | if (PyErr_Occurred()) { |
3109 | 0 | return -1; |
3110 | 0 | } |
3111 | 4 | list = PyList_New(0); |
3112 | 4 | if (list == NULL) |
3113 | 0 | return -1; |
3114 | 4 | if (_PyDict_SetItem_Take2((PyDictObject *)dict, &_Py_ID(Py_Repr), list) < 0) { |
3115 | 0 | return -1; |
3116 | 0 | } |
3117 | 4 | } |
3118 | 3.72M | i = PyList_GET_SIZE(list); |
3119 | 75.6M | while (--i >= 0) { |
3120 | 71.9M | if (PyList_GET_ITEM(list, i) == obj) |
3121 | 0 | return 1; |
3122 | 71.9M | } |
3123 | 3.72M | if (PyList_Append(list, obj) < 0) |
3124 | 0 | return -1; |
3125 | 3.72M | return 0; |
3126 | 3.72M | } |
3127 | | |
3128 | | void |
3129 | | Py_ReprLeave(PyObject *obj) |
3130 | 3.72M | { |
3131 | 3.72M | PyObject *dict; |
3132 | 3.72M | PyObject *list; |
3133 | 3.72M | Py_ssize_t i; |
3134 | | |
3135 | 3.72M | PyObject *exc = PyErr_GetRaisedException(); |
3136 | | |
3137 | 3.72M | dict = PyThreadState_GetDict(); |
3138 | 3.72M | if (dict == NULL) |
3139 | 0 | goto finally; |
3140 | | |
3141 | 3.72M | list = PyDict_GetItemWithError(dict, &_Py_ID(Py_Repr)); |
3142 | 3.72M | if (list == NULL || !PyList_Check(list)) |
3143 | 0 | goto finally; |
3144 | | |
3145 | 3.72M | i = PyList_GET_SIZE(list); |
3146 | | /* Count backwards because we always expect obj to be list[-1] */ |
3147 | 3.72M | while (--i >= 0) { |
3148 | 3.72M | if (PyList_GET_ITEM(list, i) == obj) { |
3149 | 3.72M | PyList_SetSlice(list, i, i + 1, NULL); |
3150 | 3.72M | break; |
3151 | 3.72M | } |
3152 | 3.72M | } |
3153 | | |
3154 | 3.72M | finally: |
3155 | | /* ignore exceptions because there is no way to report them. */ |
3156 | 3.72M | PyErr_SetRaisedException(exc); |
3157 | 3.72M | } |
3158 | | |
3159 | | /* Trashcan support. */ |
3160 | | |
3161 | | /* Add op to the tstate->delete_later list. Called when the current |
3162 | | * call-stack depth gets large. op must be a gc'ed object, with refcount 0. |
3163 | | * Py_DECREF must already have been called on it. |
3164 | | */ |
3165 | | void |
3166 | | _PyTrash_thread_deposit_object(PyThreadState *tstate, PyObject *op) |
3167 | 13.9k | { |
3168 | 13.9k | _PyObject_ASSERT(op, Py_REFCNT(op) == 0); |
3169 | 13.9k | PyTypeObject *tp = Py_TYPE(op); |
3170 | 13.9k | assert(tp->tp_flags & Py_TPFLAGS_HAVE_GC); |
3171 | 13.9k | int tracked = 0; |
3172 | 13.9k | if (tp->tp_is_gc == NULL || tp->tp_is_gc(op)) { |
3173 | 13.9k | tracked = _PyObject_GC_IS_TRACKED(op); |
3174 | 13.9k | if (tracked) { |
3175 | 13.9k | _PyObject_GC_UNTRACK(op); |
3176 | 13.9k | } |
3177 | 13.9k | } |
3178 | 13.9k | uintptr_t tagged_ptr = ((uintptr_t)tstate->delete_later) | tracked; |
3179 | | #ifdef Py_GIL_DISABLED |
3180 | | op->ob_tid = tagged_ptr; |
3181 | | #else |
3182 | 13.9k | _Py_AS_GC(op)->_gc_next = tagged_ptr; |
3183 | 13.9k | #endif |
3184 | 13.9k | tstate->delete_later = op; |
3185 | 13.9k | } |
3186 | | |
3187 | | /* Deallocate all the objects in the tstate->delete_later list. |
3188 | | * Called when the call-stack unwinds again. */ |
3189 | | void |
3190 | | _PyTrash_thread_destroy_chain(PyThreadState *tstate) |
3191 | 2.55k | { |
3192 | 16.4k | while (tstate->delete_later) { |
3193 | 13.9k | PyObject *op = tstate->delete_later; |
3194 | 13.9k | destructor dealloc = Py_TYPE(op)->tp_dealloc; |
3195 | | |
3196 | | #ifdef Py_GIL_DISABLED |
3197 | | uintptr_t tagged_ptr = op->ob_tid; |
3198 | | op->ob_tid = 0; |
3199 | | _Py_atomic_store_ssize_relaxed(&op->ob_ref_shared, _Py_REF_MERGED); |
3200 | | #else |
3201 | 13.9k | uintptr_t tagged_ptr = _Py_AS_GC(op)->_gc_next; |
3202 | 13.9k | _Py_AS_GC(op)->_gc_next = 0; |
3203 | 13.9k | #endif |
3204 | 13.9k | tstate->delete_later = (PyObject *)(tagged_ptr & ~1); |
3205 | 13.9k | if (tagged_ptr & 1) { |
3206 | 13.9k | _PyObject_GC_TRACK(op); |
3207 | 13.9k | } |
3208 | | /* Call the deallocator directly. This used to try to |
3209 | | * fool Py_DECREF into calling it indirectly, but |
3210 | | * Py_DECREF was already called on this object, and in |
3211 | | * assorted non-release builds calling Py_DECREF again ends |
3212 | | * up distorting allocation statistics. |
3213 | | */ |
3214 | 13.9k | _PyObject_ASSERT(op, Py_REFCNT(op) == 0); |
3215 | 13.9k | (*dealloc)(op); |
3216 | 13.9k | } |
3217 | 2.55k | } |
3218 | | |
3219 | | void _Py_NO_RETURN |
3220 | | _PyObject_AssertFailed(PyObject *obj, const char *expr, const char *msg, |
3221 | | const char *file, int line, const char *function) |
3222 | 0 | { |
3223 | 0 | fprintf(stderr, "%s:%d: ", file, line); |
3224 | 0 | if (function) { |
3225 | 0 | fprintf(stderr, "%s: ", function); |
3226 | 0 | } |
3227 | 0 | fflush(stderr); |
3228 | |
|
3229 | 0 | if (expr) { |
3230 | 0 | fprintf(stderr, "Assertion \"%s\" failed", expr); |
3231 | 0 | } |
3232 | 0 | else { |
3233 | 0 | fprintf(stderr, "Assertion failed"); |
3234 | 0 | } |
3235 | 0 | fflush(stderr); |
3236 | |
|
3237 | 0 | if (msg) { |
3238 | 0 | fprintf(stderr, ": %s", msg); |
3239 | 0 | } |
3240 | 0 | fprintf(stderr, "\n"); |
3241 | 0 | fflush(stderr); |
3242 | |
|
3243 | 0 | if (_PyObject_IsFreed(obj)) { |
3244 | | /* It seems like the object memory has been freed: |
3245 | | don't access it to prevent a segmentation fault. */ |
3246 | 0 | fprintf(stderr, "<object at %p is freed>\n", obj); |
3247 | 0 | fflush(stderr); |
3248 | 0 | } |
3249 | 0 | else { |
3250 | | /* Display the traceback where the object has been allocated. |
3251 | | Do it before dumping repr(obj), since repr() is more likely |
3252 | | to crash than dumping the traceback. */ |
3253 | 0 | PyTypeObject *type = Py_TYPE(obj); |
3254 | 0 | const size_t presize = _PyType_PreHeaderSize(type); |
3255 | 0 | void *ptr = (void *)((char *)obj - presize); |
3256 | 0 | _PyMem_DumpTraceback(fileno(stderr), ptr); |
3257 | | |
3258 | | /* This might succeed or fail, but we're about to abort, so at least |
3259 | | try to provide any extra info we can: */ |
3260 | 0 | PyObject_Dump(obj); |
3261 | |
|
3262 | 0 | fprintf(stderr, "\n"); |
3263 | 0 | fflush(stderr); |
3264 | 0 | } |
3265 | |
|
3266 | 0 | Py_FatalError("_PyObject_AssertFailed"); |
3267 | 0 | } |
3268 | | |
3269 | | |
3270 | | /* |
3271 | | When deallocating a container object, it's possible to trigger an unbounded |
3272 | | chain of deallocations, as each Py_DECREF in turn drops the refcount on "the |
3273 | | next" object in the chain to 0. This can easily lead to stack overflows. |
3274 | | To avoid that, if the C stack is nearing its limit, instead of calling |
3275 | | dealloc on the object, it is added to a queue to be freed later when the |
3276 | | stack is shallower */ |
3277 | | void |
3278 | | _Py_Dealloc(PyObject *op) |
3279 | 2.47G | { |
3280 | 2.47G | PyTypeObject *type = Py_TYPE(op); |
3281 | 2.47G | unsigned long gc_flag = type->tp_flags & Py_TPFLAGS_HAVE_GC; |
3282 | 2.47G | destructor dealloc = type->tp_dealloc; |
3283 | 2.47G | PyThreadState *tstate = _PyThreadState_GET(); |
3284 | 2.47G | intptr_t margin = _Py_RecursionLimit_GetMargin(tstate); |
3285 | 2.47G | if (margin < 2 && gc_flag) { |
3286 | 13.9k | _PyTrash_thread_deposit_object(tstate, (PyObject *)op); |
3287 | 13.9k | return; |
3288 | 13.9k | } |
3289 | | #ifdef Py_DEBUG |
3290 | | #if !defined(Py_GIL_DISABLED) && !defined(Py_STACKREF_DEBUG) |
3291 | | /* This assertion doesn't hold for the free-threading build, as |
3292 | | * PyStackRef_CLOSE_SPECIALIZED is not implemented */ |
3293 | | assert(tstate->current_frame == NULL || tstate->current_frame->stackpointer != NULL); |
3294 | | #endif |
3295 | | PyObject *old_exc = tstate != NULL ? tstate->current_exception : NULL; |
3296 | | // Keep the old exception type alive to prevent undefined behavior |
3297 | | // on (tstate->curexc_type != old_exc_type) below |
3298 | | Py_XINCREF(old_exc); |
3299 | | // Make sure that type->tp_name remains valid |
3300 | | Py_INCREF(type); |
3301 | | #endif |
3302 | | |
3303 | | #ifdef Py_TRACE_REFS |
3304 | | _Py_ForgetReference(op); |
3305 | | #endif |
3306 | 2.47G | _PyReftracerTrack(op, PyRefTracer_DESTROY); |
3307 | 2.47G | (*dealloc)(op); |
3308 | | |
3309 | | #ifdef Py_DEBUG |
3310 | | // gh-89373: The tp_dealloc function must leave the current exception |
3311 | | // unchanged. |
3312 | | if (tstate != NULL && tstate->current_exception != old_exc) { |
3313 | | const char *err; |
3314 | | if (old_exc == NULL) { |
3315 | | err = "Deallocator of type '%s' raised an exception"; |
3316 | | } |
3317 | | else if (tstate->current_exception == NULL) { |
3318 | | err = "Deallocator of type '%s' cleared the current exception"; |
3319 | | } |
3320 | | else { |
3321 | | // It can happen if dealloc() normalized the current exception. |
3322 | | // A deallocator function must not change the current exception, |
3323 | | // not even normalize it. |
3324 | | err = "Deallocator of type '%s' overrode the current exception"; |
3325 | | } |
3326 | | _Py_FatalErrorFormat(__func__, err, type->tp_name); |
3327 | | } |
3328 | | Py_XDECREF(old_exc); |
3329 | | Py_DECREF(type); |
3330 | | #endif |
3331 | 2.47G | if (tstate->delete_later && margin >= 4 && gc_flag) { |
3332 | 2.55k | _PyTrash_thread_destroy_chain(tstate); |
3333 | 2.55k | } |
3334 | 2.47G | } |
3335 | | |
3336 | | |
3337 | | PyObject ** |
3338 | | PyObject_GET_WEAKREFS_LISTPTR(PyObject *op) |
3339 | 0 | { |
3340 | 0 | return _PyObject_GET_WEAKREFS_LISTPTR(op); |
3341 | 0 | } |
3342 | | |
3343 | | |
3344 | | #undef Py_NewRef |
3345 | | #undef Py_XNewRef |
3346 | | |
3347 | | // Export Py_NewRef() and Py_XNewRef() as regular functions for the stable ABI. |
3348 | | PyObject* |
3349 | | Py_NewRef(PyObject *obj) |
3350 | 0 | { |
3351 | 0 | return _Py_NewRef(obj); |
3352 | 0 | } |
3353 | | |
3354 | | PyObject* |
3355 | | Py_XNewRef(PyObject *obj) |
3356 | 0 | { |
3357 | 0 | return _Py_XNewRef(obj); |
3358 | 0 | } |
3359 | | |
3360 | | #undef Py_Is |
3361 | | #undef Py_IsNone |
3362 | | #undef Py_IsTrue |
3363 | | #undef Py_IsFalse |
3364 | | |
3365 | | // Export Py_Is(), Py_IsNone(), Py_IsTrue(), Py_IsFalse() as regular functions |
3366 | | // for the stable ABI. |
3367 | | int Py_Is(PyObject *x, PyObject *y) |
3368 | 0 | { |
3369 | 0 | return (x == y); |
3370 | 0 | } |
3371 | | |
3372 | | int Py_IsNone(PyObject *x) |
3373 | 0 | { |
3374 | 0 | return Py_Is(x, Py_None); |
3375 | 0 | } |
3376 | | |
3377 | | int Py_IsTrue(PyObject *x) |
3378 | 0 | { |
3379 | 0 | return Py_Is(x, Py_True); |
3380 | 0 | } |
3381 | | |
3382 | | int Py_IsFalse(PyObject *x) |
3383 | 0 | { |
3384 | 0 | return Py_Is(x, Py_False); |
3385 | 0 | } |
3386 | | |
3387 | | |
3388 | | // Py_SET_REFCNT() implementation for stable ABI |
3389 | | void |
3390 | | _Py_SetRefcnt(PyObject *ob, Py_ssize_t refcnt) |
3391 | 0 | { |
3392 | 0 | Py_SET_REFCNT(ob, refcnt); |
3393 | 0 | } |
3394 | | |
3395 | 0 | int PyRefTracer_SetTracer(PyRefTracer tracer, void *data) { |
3396 | 0 | _Py_AssertHoldsTstate(); |
3397 | |
|
3398 | 0 | _PyEval_StopTheWorldAll(&_PyRuntime); |
3399 | 0 | if (_PyRuntime.ref_tracer.tracer_func != NULL) { |
3400 | 0 | _PyReftracerTrack(NULL, PyRefTracer_TRACKER_REMOVED); |
3401 | 0 | if (PyErr_Occurred()) { |
3402 | 0 | _PyEval_StartTheWorldAll(&_PyRuntime); |
3403 | 0 | return -1; |
3404 | 0 | } |
3405 | 0 | } |
3406 | 0 | _PyRuntime.ref_tracer.tracer_func = tracer; |
3407 | 0 | _PyRuntime.ref_tracer.tracer_data = data; |
3408 | 0 | _PyEval_StartTheWorldAll(&_PyRuntime); |
3409 | 0 | return 0; |
3410 | 0 | } |
3411 | | |
3412 | 0 | PyRefTracer PyRefTracer_GetTracer(void** data) { |
3413 | 0 | _Py_AssertHoldsTstate(); |
3414 | 0 | if (data != NULL) { |
3415 | 0 | *data = _PyRuntime.ref_tracer.tracer_data; |
3416 | 0 | } |
3417 | 0 | return _PyRuntime.ref_tracer.tracer_func; |
3418 | 0 | } |
3419 | | |
3420 | | |
3421 | | |
3422 | | static PyObject* constants[] = { |
3423 | | &_Py_NoneStruct, // Py_CONSTANT_NONE |
3424 | | (PyObject*)(&_Py_FalseStruct), // Py_CONSTANT_FALSE |
3425 | | (PyObject*)(&_Py_TrueStruct), // Py_CONSTANT_TRUE |
3426 | | &_Py_EllipsisObject, // Py_CONSTANT_ELLIPSIS |
3427 | | &_Py_NotImplementedStruct, // Py_CONSTANT_NOT_IMPLEMENTED |
3428 | | NULL, // Py_CONSTANT_ZERO |
3429 | | NULL, // Py_CONSTANT_ONE |
3430 | | NULL, // Py_CONSTANT_EMPTY_STR |
3431 | | NULL, // Py_CONSTANT_EMPTY_BYTES |
3432 | | NULL, // Py_CONSTANT_EMPTY_TUPLE |
3433 | | }; |
3434 | | |
3435 | | void |
3436 | | _Py_GetConstant_Init(void) |
3437 | 36 | { |
3438 | 36 | constants[Py_CONSTANT_ZERO] = _PyLong_GetZero(); |
3439 | 36 | constants[Py_CONSTANT_ONE] = _PyLong_GetOne(); |
3440 | 36 | constants[Py_CONSTANT_EMPTY_STR] = PyUnicode_New(0, 0); |
3441 | 36 | constants[Py_CONSTANT_EMPTY_BYTES] = PyBytes_FromStringAndSize(NULL, 0); |
3442 | 36 | constants[Py_CONSTANT_EMPTY_TUPLE] = PyTuple_New(0); |
3443 | | #ifndef NDEBUG |
3444 | | for (size_t i=0; i < Py_ARRAY_LENGTH(constants); i++) { |
3445 | | assert(constants[i] != NULL); |
3446 | | assert(_Py_IsImmortal(constants[i])); |
3447 | | } |
3448 | | #endif |
3449 | 36 | } |
3450 | | |
3451 | | PyObject* |
3452 | | Py_GetConstant(unsigned int constant_id) |
3453 | 20.5M | { |
3454 | 20.5M | if (constant_id < Py_ARRAY_LENGTH(constants)) { |
3455 | 20.5M | return constants[constant_id]; |
3456 | 20.5M | } |
3457 | 0 | else { |
3458 | 0 | PyErr_BadInternalCall(); |
3459 | 0 | return NULL; |
3460 | 0 | } |
3461 | 20.5M | } |
3462 | | |
3463 | | |
3464 | | PyObject* |
3465 | | Py_GetConstantBorrowed(unsigned int constant_id) |
3466 | 0 | { |
3467 | | // All constants are immortal |
3468 | 0 | return Py_GetConstant(constant_id); |
3469 | 0 | } |
3470 | | |
3471 | | int |
3472 | | PyUnstable_IsImmortal(PyObject *op) |
3473 | 0 | { |
3474 | | /* Checking a reference count requires a thread state */ |
3475 | 0 | _Py_AssertHoldsTstate(); |
3476 | 0 | assert(op != NULL); |
3477 | 0 | return _Py_IsImmortal(op); |
3478 | 0 | } |
3479 | | |
3480 | | int |
3481 | | PyUnstable_Object_IsUniquelyReferenced(PyObject *op) |
3482 | 0 | { |
3483 | 0 | _Py_AssertHoldsTstate(); |
3484 | 0 | assert(op != NULL); |
3485 | 0 | return _PyObject_IsUniquelyReferenced(op); |
3486 | 0 | } |
3487 | | |
3488 | | int |
3489 | | _PyObject_VisitType(PyObject *op, visitproc visit, void *arg) |
3490 | 359k | { |
3491 | 359k | assert(op != NULL); |
3492 | 359k | PyTypeObject *tp = Py_TYPE(op); |
3493 | 359k | _PyObject_ASSERT((PyObject *)tp, PyType_HasFeature(tp, Py_TPFLAGS_HEAPTYPE)); |
3494 | 359k | Py_VISIT(tp); |
3495 | 359k | return 0; |
3496 | 359k | } |
3497 | | |
3498 | | // Implementations for the stable ABI |
3499 | | // Keep these at the end. |
3500 | | #undef Py_TYPE |
3501 | | #undef Py_REFCNT |
3502 | | #undef Py_SIZE |
3503 | | #undef Py_IS_TYPE |
3504 | | #undef Py_SET_SIZE |
3505 | 0 | PyTypeObject* Py_TYPE(PyObject *ob) { return _Py_TYPE_impl(ob); } |
3506 | 0 | Py_ssize_t Py_REFCNT(PyObject *ob) { return _Py_REFCNT(ob); } |
3507 | 0 | Py_ssize_t Py_SIZE(PyObject *o) { return _Py_SIZE_impl(o); } |
3508 | 0 | int Py_IS_TYPE(PyObject *o, PyTypeObject *t) { return _Py_IS_TYPE_impl(o, t); } |
3509 | 0 | void Py_SET_SIZE(PyVarObject *o, Py_ssize_t s) { _Py_SET_SIZE_impl(o, s); } |