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