| 
									
										
										
										
											1991-02-19 12:39:46 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-06-11 16:33:35 -05:00
										 |  |  | /* Generic object operations; and implementation of None */ | 
					
						
							| 
									
										
										
										
											1990-10-14 12:07:46 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1997-05-02 03:12:38 +00:00
										 |  |  | #include "Python.h"
 | 
					
						
							| 
									
										
										
										
											2024-02-09 17:08:32 -05:00
										 |  |  | #include "pycore_brc.h"           // _Py_brc_queue_object()
 | 
					
						
							| 
									
										
										
										
											2021-10-12 08:38:19 +02:00
										 |  |  | #include "pycore_call.h"          // _PyObject_CallNoArgs()
 | 
					
						
							| 
									
										
										
										
											2022-05-04 13:30:23 +02:00
										 |  |  | #include "pycore_ceval.h"         // _Py_EnterRecursiveCallTstate()
 | 
					
						
							| 
									
										
										
										
											2022-01-22 18:55:48 +01:00
										 |  |  | #include "pycore_context.h"       // _PyContextTokenMissing_Type
 | 
					
						
							| 
									
										
										
										
											2025-03-20 12:35:23 +01:00
										 |  |  | #include "pycore_critical_section.h" // Py_BEGIN_CRITICAL_SECTION
 | 
					
						
							| 
									
										
										
										
											2023-07-22 22:57:59 +02:00
										 |  |  | #include "pycore_descrobject.h"   // _PyMethodWrapper_Type
 | 
					
						
							| 
									
										
										
										
											2025-03-20 12:35:23 +01:00
										 |  |  | #include "pycore_dict.h"          // _PyObject_MaterializeManagedDict()
 | 
					
						
							| 
									
										
										
										
											2021-10-14 23:41:06 +02:00
										 |  |  | #include "pycore_floatobject.h"   // _PyFloat_DebugMallocStats()
 | 
					
						
							| 
									
										
										
										
											2024-07-22 12:08:27 -04:00
										 |  |  | #include "pycore_freelist.h"      // _PyObject_ClearFreeLists()
 | 
					
						
							| 
									
										
										
										
											2025-03-20 12:35:23 +01:00
										 |  |  | #include "pycore_genobject.h"     // _PyAsyncGenAThrow_Type
 | 
					
						
							| 
									
										
										
										
											2025-03-19 18:46:24 +01:00
										 |  |  | #include "pycore_hamt.h"          // _PyHamtItems_Type
 | 
					
						
							| 
									
										
										
										
											2025-03-20 12:35:23 +01:00
										 |  |  | #include "pycore_initconfig.h"    // _PyStatus_OK()
 | 
					
						
							| 
									
										
										
										
											2024-04-17 16:42:04 +01:00
										 |  |  | #include "pycore_instruction_sequence.h" // _PyInstructionSequence_Type
 | 
					
						
							| 
									
										
										
										
											2025-05-02 09:24:57 -04:00
										 |  |  | #include "pycore_interpframe.h"   // _PyFrame_Stackbase()
 | 
					
						
							| 
									
										
										
										
											2025-04-30 11:46:41 +02:00
										 |  |  | #include "pycore_interpolation.h" // _PyInterpolation_Type
 | 
					
						
							| 
									
										
										
										
											2025-03-19 18:46:24 +01:00
										 |  |  | #include "pycore_list.h"          // _PyList_DebugMallocStats()
 | 
					
						
							|  |  |  | #include "pycore_long.h"          // _PyLong_GetZero()
 | 
					
						
							| 
									
										
										
										
											2023-08-24 19:07:54 +02:00
										 |  |  | #include "pycore_memoryobject.h"  // _PyManagedBuffer_Type
 | 
					
						
							| 
									
										
										
										
											2021-10-15 15:21:21 +02:00
										 |  |  | #include "pycore_namespace.h"     // _PyNamespace_Type
 | 
					
						
							| 
									
										
										
										
											2025-03-20 12:35:23 +01:00
										 |  |  | #include "pycore_object.h"        // export _Py_SwappedOp
 | 
					
						
							|  |  |  | #include "pycore_optimizer.h"     // _PyUOpExecutor_Type
 | 
					
						
							| 
									
										
										
										
											2021-10-12 08:38:19 +02:00
										 |  |  | #include "pycore_pyerrors.h"      // _PyErr_Occurred()
 | 
					
						
							| 
									
										
										
										
											2020-04-14 18:30:41 +02:00
										 |  |  | #include "pycore_pymem.h"         // _PyMem_IsPtrFreed()
 | 
					
						
							| 
									
										
										
										
											2020-04-14 17:52:15 +02:00
										 |  |  | #include "pycore_pystate.h"       // _PyThreadState_GET()
 | 
					
						
							| 
									
										
										
										
											2021-04-08 00:47:55 +02:00
										 |  |  | #include "pycore_symtable.h"      // PySTEntry_Type
 | 
					
						
							| 
									
										
										
										
											2025-04-30 11:46:41 +02:00
										 |  |  | #include "pycore_template.h"      // _PyTemplate_Type _PyTemplateIter_Type
 | 
					
						
							| 
									
										
										
										
											2025-03-19 18:46:24 +01:00
										 |  |  | #include "pycore_tuple.h"         // _PyTuple_DebugMallocStats()
 | 
					
						
							| 
									
										
										
										
											2023-07-27 15:30:16 -06:00
										 |  |  | #include "pycore_typeobject.h"    // _PyBufferWrapper_Type
 | 
					
						
							| 
									
										
										
										
											2025-03-20 12:35:23 +01:00
										 |  |  | #include "pycore_typevarobject.h" // _PyTypeAlias_Type
 | 
					
						
							| 
									
										
										
										
											2021-07-17 22:44:10 +03:00
										 |  |  | #include "pycore_unionobject.h"   // _PyUnion_Type
 | 
					
						
							| 
									
										
										
										
											2023-08-24 19:07:54 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-21 22:57:05 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-02 15:45:37 +02:00
										 |  |  | #ifdef Py_LIMITED_API
 | 
					
						
							|  |  |  |    // Prevent recursive call _Py_IncRef() <=> Py_INCREF()
 | 
					
						
							|  |  |  | #  error "Py_LIMITED_API macro must not be defined"
 | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-25 17:31:10 +02:00
										 |  |  | /* Defined in tracemalloc.c */ | 
					
						
							|  |  |  | extern void _PyMem_DumpTraceback(int fd, const void *ptr); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-12 21:51:34 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | int | 
					
						
							|  |  |  | _PyObject_CheckConsistency(PyObject *op, int check_content) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2019-10-07 18:42:01 +02:00
										 |  |  | #define CHECK(expr) \
 | 
					
						
							|  |  |  |     do { if (!(expr)) { _PyObject_ASSERT_FAILED_MSG(op, Py_STRINGIFY(expr)); } } while (0) | 
					
						
							| 
									
										
										
										
											2019-04-12 21:51:34 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-07 18:42:01 +02:00
										 |  |  |     CHECK(!_PyObject_IsFreed(op)); | 
					
						
							|  |  |  |     CHECK(Py_REFCNT(op) >= 1); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-07 03:04:21 +01:00
										 |  |  |     _PyType_CheckConsistency(Py_TYPE(op)); | 
					
						
							| 
									
										
										
										
											2019-04-12 21:51:34 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     if (PyUnicode_Check(op)) { | 
					
						
							|  |  |  |         _PyUnicode_CheckConsistency(op, check_content); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     else if (PyDict_Check(op)) { | 
					
						
							|  |  |  |         _PyDict_CheckConsistency(op, check_content); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return 1; | 
					
						
							| 
									
										
										
										
											2019-10-07 18:42:01 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | #undef CHECK
 | 
					
						
							| 
									
										
										
										
											2019-04-12 21:51:34 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
											  
											
												object.h special-build macro minefield:  renamed all the new lexical
helper macros to something saner, and used them appropriately in other
files too, to reduce #ifdef blocks.
classobject.c, instance_dealloc():  One of my worst Python Memories is
trying to fix this routine a few years ago when COUNT_ALLOCS was defined
but Py_TRACE_REFS wasn't.  The special-build code here is way too
complicated.  Now it's much simpler.  Difference:  in a Py_TRACE_REFS
build, the instance is no longer in the doubly-linked list of live
objects while its __del__ method is executing, and that may be visible
via sys.getobjects() called from a __del__ method.  Tough -- the object
is presumed dead while its __del__ is executing anyway, and not calling
_Py_NewReference() at the start allows enormous code simplification.
typeobject.c, call_finalizer():  The special-build instance_dealloc()
pain apparently spread to here too via cut-'n-paste, and this is much
simpler now too.  In addition, I didn't understand why this routine
was calling _PyObject_GC_TRACK() after a resurrection, since there's no
plausible way _PyObject_GC_UNTRACK() could have been called on the
object by this point.  I suspect it was left over from pasting the
instance_delloc() code.  Instead asserted that the object is still
tracked.  Caution:  I suspect we don't have a test that actually
exercises the subtype_dealloc() __del__-resurrected-me code.
											
										 
											2002-07-11 06:23:50 +00:00
										 |  |  | #ifdef Py_REF_DEBUG
 | 
					
						
							| 
									
										
										
										
											2023-03-20 10:03:04 -06:00
										 |  |  | /* We keep the legacy symbol around for backward compatibility. */ | 
					
						
							| 
									
										
										
										
											2006-03-04 20:00:59 +00:00
										 |  |  | Py_ssize_t _Py_RefTotal; | 
					
						
							| 
									
										
										
										
											2006-04-21 10:40:58 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-20 10:03:04 -06:00
										 |  |  | static inline Py_ssize_t | 
					
						
							|  |  |  | get_legacy_reftotal(void) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     return _Py_RefTotal; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #ifdef Py_REF_DEBUG
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-21 11:46:09 -06:00
										 |  |  | #  define REFTOTAL(interp) \
 | 
					
						
							|  |  |  |     interp->object_state.reftotal | 
					
						
							| 
									
										
										
										
											2023-03-20 10:03:04 -06:00
										 |  |  | 
 | 
					
						
							|  |  |  | static inline void | 
					
						
							| 
									
										
										
										
											2024-04-08 12:11:36 -04:00
										 |  |  | reftotal_add(PyThreadState *tstate, Py_ssize_t n) | 
					
						
							| 
									
										
										
										
											2023-03-20 10:03:04 -06:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2024-04-08 12:11:36 -04:00
										 |  |  | #ifdef Py_GIL_DISABLED
 | 
					
						
							|  |  |  |     _PyThreadStateImpl *tstate_impl = (_PyThreadStateImpl *)tstate; | 
					
						
							|  |  |  |     // relaxed store to avoid data race with read in get_reftotal()
 | 
					
						
							|  |  |  |     Py_ssize_t reftotal = tstate_impl->reftotal + n; | 
					
						
							|  |  |  |     _Py_atomic_store_ssize_relaxed(&tstate_impl->reftotal, reftotal); | 
					
						
							|  |  |  | #else
 | 
					
						
							|  |  |  |     REFTOTAL(tstate->interp) += n; | 
					
						
							|  |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2023-03-08 12:03:50 -07:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-20 10:03:04 -06:00
										 |  |  | static inline Py_ssize_t get_global_reftotal(_PyRuntimeState *); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* We preserve the number of refs leaked during runtime finalization,
 | 
					
						
							|  |  |  |    so they can be reported if the runtime is initialized again. */ | 
					
						
							|  |  |  | // XXX We don't lose any information by dropping this,
 | 
					
						
							|  |  |  | // so we should consider doing so.
 | 
					
						
							|  |  |  | static Py_ssize_t last_final_reftotal = 0; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-08 12:03:50 -07:00
										 |  |  | void | 
					
						
							| 
									
										
										
										
											2023-03-20 10:03:04 -06:00
										 |  |  | _Py_FinalizeRefTotal(_PyRuntimeState *runtime) | 
					
						
							| 
									
										
										
										
											2023-03-08 12:03:50 -07:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2023-03-20 10:03:04 -06:00
										 |  |  |     last_final_reftotal = get_global_reftotal(runtime); | 
					
						
							| 
									
										
										
										
											2023-03-21 11:46:09 -06:00
										 |  |  |     runtime->object_state.interpreter_leaks = 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void | 
					
						
							|  |  |  | _PyInterpreterState_FinalizeRefTotal(PyInterpreterState *interp) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     interp->runtime->object_state.interpreter_leaks += REFTOTAL(interp); | 
					
						
							|  |  |  |     REFTOTAL(interp) = 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static inline Py_ssize_t | 
					
						
							|  |  |  | get_reftotal(PyInterpreterState *interp) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     /* For a single interpreter, we ignore the legacy _Py_RefTotal,
 | 
					
						
							|  |  |  |        since we can't determine which interpreter updated it. */ | 
					
						
							| 
									
										
										
										
											2024-04-08 12:11:36 -04:00
										 |  |  |     Py_ssize_t total = REFTOTAL(interp); | 
					
						
							|  |  |  | #ifdef Py_GIL_DISABLED
 | 
					
						
							| 
									
										
										
										
											2024-11-21 11:08:38 -07:00
										 |  |  |     _Py_FOR_EACH_TSTATE_UNLOCKED(interp, p) { | 
					
						
							| 
									
										
										
										
											2024-04-08 12:11:36 -04:00
										 |  |  |         /* This may race with other threads modifications to their reftotal */ | 
					
						
							|  |  |  |         _PyThreadStateImpl *tstate_impl = (_PyThreadStateImpl *)p; | 
					
						
							|  |  |  |         total += _Py_atomic_load_ssize_relaxed(&tstate_impl->reftotal); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  |     return total; | 
					
						
							| 
									
										
										
										
											2023-03-08 12:03:50 -07:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-20 10:03:04 -06:00
										 |  |  | static inline Py_ssize_t | 
					
						
							|  |  |  | get_global_reftotal(_PyRuntimeState *runtime) | 
					
						
							| 
									
										
										
										
											2006-04-21 10:40:58 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2023-03-21 11:46:09 -06:00
										 |  |  |     Py_ssize_t total = 0; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /* Add up the total from each interpreter. */ | 
					
						
							|  |  |  |     HEAD_LOCK(&_PyRuntime); | 
					
						
							|  |  |  |     PyInterpreterState *interp = PyInterpreterState_Head(); | 
					
						
							|  |  |  |     for (; interp != NULL; interp = PyInterpreterState_Next(interp)) { | 
					
						
							| 
									
										
										
										
											2024-04-08 12:11:36 -04:00
										 |  |  |         total += get_reftotal(interp); | 
					
						
							| 
									
										
										
										
											2023-03-21 11:46:09 -06:00
										 |  |  |     } | 
					
						
							|  |  |  |     HEAD_UNLOCK(&_PyRuntime); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /* Add in the updated value from the legacy _Py_RefTotal. */ | 
					
						
							|  |  |  |     total += get_legacy_reftotal(); | 
					
						
							|  |  |  |     total += last_final_reftotal; | 
					
						
							|  |  |  |     total += runtime->object_state.interpreter_leaks; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return total; | 
					
						
							| 
									
										
										
										
											2006-04-21 10:40:58 +00:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2014-11-20 21:39:37 +10:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-20 10:03:04 -06:00
										 |  |  | #undef REFTOTAL
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-14 00:35:58 -07:00
										 |  |  | void | 
					
						
							|  |  |  | _PyDebug_PrintTotalRefs(void) { | 
					
						
							| 
									
										
										
										
											2023-03-20 10:03:04 -06:00
										 |  |  |     _PyRuntimeState *runtime = &_PyRuntime; | 
					
						
							| 
									
										
										
										
											2017-09-14 00:35:58 -07:00
										 |  |  |     fprintf(stderr, | 
					
						
							| 
									
										
										
										
											2020-06-10 18:38:05 +02:00
										 |  |  |             "[%zd refs, %zd blocks]\n", | 
					
						
							| 
									
										
										
										
											2023-04-24 17:23:57 -06:00
										 |  |  |             get_global_reftotal(runtime), _Py_GetGlobalAllocatedBlocks()); | 
					
						
							| 
									
										
										
										
											2023-03-21 11:46:09 -06:00
										 |  |  |     /* It may be helpful to also print the "legacy" reftotal separately.
 | 
					
						
							|  |  |  |        Likewise for the total for each interpreter. */ | 
					
						
							| 
									
										
										
										
											2014-11-20 21:39:37 +10:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2006-04-21 10:40:58 +00:00
										 |  |  | #endif /* Py_REF_DEBUG */
 | 
					
						
							| 
									
										
										
										
											1990-10-14 12:07:46 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1990-12-20 15:06:42 +00:00
										 |  |  | /* Object allocation routines used by NEWOBJ and NEWVAROBJ macros.
 | 
					
						
							|  |  |  |    These are used by the individual routines for object creation. | 
					
						
							|  |  |  |    Do not call them otherwise, they do not initialize the object! */ | 
					
						
							| 
									
										
										
										
											1990-10-14 12:07:46 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2003-03-23 02:51:01 +00:00
										 |  |  | #ifdef Py_TRACE_REFS
 | 
					
						
							| 
									
										
										
										
											2023-08-03 13:51:08 -06:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-31 18:33:34 +02:00
										 |  |  | #define REFCHAIN(interp) interp->object_state.refchain
 | 
					
						
							|  |  |  | #define REFCHAIN_VALUE ((void*)(uintptr_t)1)
 | 
					
						
							| 
									
										
										
										
											2003-03-23 03:33:13 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-10-23 10:10:06 -06:00
										 |  |  | static inline int | 
					
						
							|  |  |  | has_own_refchain(PyInterpreterState *interp) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     if (interp->feature_flags & Py_RTFLAGS_USE_MAIN_OBMALLOC) { | 
					
						
							|  |  |  |         return (_Py_IsMainInterpreter(interp) | 
					
						
							|  |  |  |             || _PyInterpreterState_Main() == NULL); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return 1; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static int | 
					
						
							|  |  |  | refchain_init(PyInterpreterState *interp) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     if (!has_own_refchain(interp)) { | 
					
						
							|  |  |  |         // Legacy subinterpreters share a refchain with the main interpreter.
 | 
					
						
							|  |  |  |         REFCHAIN(interp) = REFCHAIN(_PyInterpreterState_Main()); | 
					
						
							|  |  |  |         return 0; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     _Py_hashtable_allocator_t alloc = { | 
					
						
							|  |  |  |         // Don't use default PyMem_Malloc() and PyMem_Free() which
 | 
					
						
							|  |  |  |         // require the caller to hold the GIL.
 | 
					
						
							|  |  |  |         .malloc = PyMem_RawMalloc, | 
					
						
							|  |  |  |         .free = PyMem_RawFree, | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  |     REFCHAIN(interp) = _Py_hashtable_new_full( | 
					
						
							|  |  |  |         _Py_hashtable_hash_ptr, _Py_hashtable_compare_direct, | 
					
						
							|  |  |  |         NULL, NULL, &alloc); | 
					
						
							|  |  |  |     if (REFCHAIN(interp) == NULL) { | 
					
						
							|  |  |  |         return -1; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static void | 
					
						
							|  |  |  | refchain_fini(PyInterpreterState *interp) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     if (has_own_refchain(interp) && REFCHAIN(interp) != NULL) { | 
					
						
							|  |  |  |         _Py_hashtable_destroy(REFCHAIN(interp)); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     REFCHAIN(interp) = NULL; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-31 18:33:34 +02:00
										 |  |  | bool | 
					
						
							|  |  |  | _PyRefchain_IsTraced(PyInterpreterState *interp, PyObject *obj) | 
					
						
							| 
									
										
										
										
											2003-03-23 03:33:13 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2023-08-31 18:33:34 +02:00
										 |  |  |     return (_Py_hashtable_get(REFCHAIN(interp), obj) == REFCHAIN_VALUE); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static void | 
					
						
							|  |  |  | _PyRefchain_Trace(PyInterpreterState *interp, PyObject *obj) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     if (_Py_hashtable_set(REFCHAIN(interp), obj, REFCHAIN_VALUE) < 0) { | 
					
						
							|  |  |  |         // Use a fatal error because _Py_NewReference() cannot report
 | 
					
						
							|  |  |  |         // the error to the caller.
 | 
					
						
							|  |  |  |         Py_FatalError("_Py_hashtable_set() memory allocation failed"); | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2023-08-31 18:33:34 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static void | 
					
						
							|  |  |  | _PyRefchain_Remove(PyInterpreterState *interp, PyObject *obj) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     void *value = _Py_hashtable_steal(REFCHAIN(interp), obj); | 
					
						
							|  |  |  | #ifndef NDEBUG
 | 
					
						
							|  |  |  |     assert(value == REFCHAIN_VALUE); | 
					
						
							|  |  |  | #else
 | 
					
						
							|  |  |  |     (void)value; | 
					
						
							| 
									
										
										
										
											2003-03-23 02:51:01 +00:00
										 |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2023-08-31 18:33:34 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* Add an object to the refchain hash table.
 | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Note that objects are normally added to the list by PyObject_Init() | 
					
						
							|  |  |  |  * indirectly.  Not all objects are initialized that way, though; exceptions | 
					
						
							|  |  |  |  * include statically allocated type objects, and statically allocated | 
					
						
							|  |  |  |  * singletons (like Py_True and Py_None). */ | 
					
						
							|  |  |  | void | 
					
						
							|  |  |  | _Py_AddToAllObjects(PyObject *op) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     PyInterpreterState *interp = _PyInterpreterState_GET(); | 
					
						
							|  |  |  |     if (!_PyRefchain_IsTraced(interp, op)) { | 
					
						
							|  |  |  |         _PyRefchain_Trace(interp, op); | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2003-03-23 17:52:28 +00:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  | #endif  /* Py_TRACE_REFS */
 | 
					
						
							| 
									
										
										
										
											2003-03-23 02:51:01 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-07-09 02:57:01 +00:00
										 |  |  | #ifdef Py_REF_DEBUG
 | 
					
						
							|  |  |  | /* Log a fatal error; doesn't return. */ | 
					
						
							|  |  |  | void | 
					
						
							| 
									
										
										
										
											2018-10-25 17:28:11 +02:00
										 |  |  | _Py_NegativeRefcount(const char *filename, int lineno, PyObject *op) | 
					
						
							| 
									
										
										
										
											2002-07-09 02:57:01 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2018-11-21 23:53:44 +01:00
										 |  |  |     _PyObject_AssertFailed(op, NULL, "object has negative ref count", | 
					
						
							| 
									
										
										
										
											2018-10-26 02:12:34 +02:00
										 |  |  |                            filename, lineno, __func__); | 
					
						
							| 
									
										
										
										
											2002-07-09 02:57:01 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-20 10:03:04 -06:00
										 |  |  | /* This is used strictly by Py_INCREF(). */ | 
					
						
							|  |  |  | void | 
					
						
							| 
									
										
										
										
											2023-07-24 21:47:50 +02:00
										 |  |  | _Py_INCREF_IncRefTotal(void) | 
					
						
							| 
									
										
										
										
											2023-03-08 12:03:50 -07:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2024-04-08 12:11:36 -04:00
										 |  |  |     reftotal_add(_PyThreadState_GET(), 1); | 
					
						
							| 
									
										
										
										
											2023-03-08 12:03:50 -07:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-20 10:03:04 -06:00
										 |  |  | /* This is used strictly by Py_DECREF(). */ | 
					
						
							|  |  |  | void | 
					
						
							| 
									
										
										
										
											2023-07-24 21:47:50 +02:00
										 |  |  | _Py_DECREF_DecRefTotal(void) | 
					
						
							| 
									
										
										
										
											2023-03-08 12:03:50 -07:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2024-04-08 12:11:36 -04:00
										 |  |  |     reftotal_add(_PyThreadState_GET(), -1); | 
					
						
							| 
									
										
										
										
											2023-03-08 12:03:50 -07:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void | 
					
						
							| 
									
										
										
										
											2024-04-08 12:11:36 -04:00
										 |  |  | _Py_IncRefTotal(PyThreadState *tstate) | 
					
						
							| 
									
										
										
										
											2023-03-08 12:03:50 -07:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2024-04-08 12:11:36 -04:00
										 |  |  |     reftotal_add(tstate, 1); | 
					
						
							| 
									
										
										
										
											2023-03-08 12:03:50 -07:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void | 
					
						
							| 
									
										
										
										
											2024-04-08 12:11:36 -04:00
										 |  |  | _Py_DecRefTotal(PyThreadState *tstate) | 
					
						
							| 
									
										
										
										
											2023-03-08 12:03:50 -07:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2024-04-08 12:11:36 -04:00
										 |  |  |     reftotal_add(tstate, -1); | 
					
						
							| 
									
										
										
										
											2023-03-20 10:03:04 -06:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void | 
					
						
							| 
									
										
										
										
											2024-04-08 12:11:36 -04:00
										 |  |  | _Py_AddRefTotal(PyThreadState *tstate, Py_ssize_t n) | 
					
						
							| 
									
										
										
										
											2023-03-20 10:03:04 -06:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2024-04-08 12:11:36 -04:00
										 |  |  |     reftotal_add(tstate, n); | 
					
						
							| 
									
										
										
										
											2023-03-20 10:03:04 -06:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* This includes the legacy total
 | 
					
						
							|  |  |  |    and any carried over from the last runtime init/fini cycle. */ | 
					
						
							|  |  |  | Py_ssize_t | 
					
						
							|  |  |  | _Py_GetGlobalRefTotal(void) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     return get_global_reftotal(&_PyRuntime); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Py_ssize_t | 
					
						
							|  |  |  | _Py_GetLegacyRefTotal(void) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     return get_legacy_reftotal(); | 
					
						
							| 
									
										
										
										
											2023-03-08 12:03:50 -07:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-21 11:46:09 -06:00
										 |  |  | Py_ssize_t | 
					
						
							|  |  |  | _PyInterpreterState_GetRefTotal(PyInterpreterState *interp) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2024-04-08 12:11:36 -04:00
										 |  |  |     HEAD_LOCK(&_PyRuntime); | 
					
						
							|  |  |  |     Py_ssize_t total = get_reftotal(interp); | 
					
						
							|  |  |  |     HEAD_UNLOCK(&_PyRuntime); | 
					
						
							|  |  |  |     return total; | 
					
						
							| 
									
										
										
										
											2023-03-21 11:46:09 -06:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-07-09 02:57:01 +00:00
										 |  |  | #endif /* Py_REF_DEBUG */
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-04-22 17:23:49 +00:00
										 |  |  | void | 
					
						
							|  |  |  | Py_IncRef(PyObject *o) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     Py_XINCREF(o); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void | 
					
						
							|  |  |  | Py_DecRef(PyObject *o) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     Py_XDECREF(o); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-02 15:45:37 +02:00
										 |  |  | void | 
					
						
							|  |  |  | _Py_IncRef(PyObject *o) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     Py_INCREF(o); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void | 
					
						
							|  |  |  | _Py_DecRef(PyObject *o) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     Py_DECREF(o); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-11-20 15:52:00 +02:00
										 |  |  | #ifdef Py_GIL_DISABLED
 | 
					
						
							| 
									
										
										
										
											2023-11-10 18:54:35 -05:00
										 |  |  | # ifdef Py_REF_DEBUG
 | 
					
						
							| 
									
										
										
										
											2024-02-21 13:00:08 -05:00
										 |  |  | static int | 
					
						
							|  |  |  | is_dead(PyObject *o) | 
					
						
							| 
									
										
										
										
											2023-10-30 12:06:09 -04:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2023-11-10 18:54:35 -05:00
										 |  |  | #  if SIZEOF_SIZE_T == 8
 | 
					
						
							| 
									
										
										
										
											2024-02-21 13:00:08 -05:00
										 |  |  |     return (uintptr_t)o->ob_type == 0xDDDDDDDDDDDDDDDD; | 
					
						
							| 
									
										
										
										
											2023-11-10 18:54:35 -05:00
										 |  |  | #  else
 | 
					
						
							| 
									
										
										
										
											2024-02-21 13:00:08 -05:00
										 |  |  |     return (uintptr_t)o->ob_type == 0xDDDDDDDD; | 
					
						
							| 
									
										
										
										
											2023-11-10 18:54:35 -05:00
										 |  |  | #  endif
 | 
					
						
							| 
									
										
										
										
											2023-10-30 12:06:09 -04:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2023-11-10 18:54:35 -05:00
										 |  |  | # endif
 | 
					
						
							| 
									
										
										
										
											2023-10-30 12:06:09 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-12-05 21:07:31 +00:00
										 |  |  | // Decrement the shared reference count of an object. Return 1 if the object
 | 
					
						
							|  |  |  | // is dead and should be deallocated, 0 otherwise.
 | 
					
						
							|  |  |  | static int | 
					
						
							|  |  |  | _Py_DecRefSharedIsDead(PyObject *o, const char *filename, int lineno) | 
					
						
							| 
									
										
										
										
											2023-10-30 12:06:09 -04:00
										 |  |  | { | 
					
						
							|  |  |  |     // Should we queue the object for the owning thread to merge?
 | 
					
						
							|  |  |  |     int should_queue; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     Py_ssize_t new_shared; | 
					
						
							|  |  |  |     Py_ssize_t shared = _Py_atomic_load_ssize_relaxed(&o->ob_ref_shared); | 
					
						
							|  |  |  |     do { | 
					
						
							|  |  |  |         should_queue = (shared == 0 || shared == _Py_REF_MAYBE_WEAKREF); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (should_queue) { | 
					
						
							|  |  |  |             // If the object had refcount zero, not queued, and not merged,
 | 
					
						
							|  |  |  |             // then we enqueue the object to be merged by the owning thread.
 | 
					
						
							|  |  |  |             // In this case, we don't subtract one from the reference count
 | 
					
						
							|  |  |  |             // because the queue holds a reference.
 | 
					
						
							|  |  |  |             new_shared = _Py_REF_QUEUED; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         else { | 
					
						
							|  |  |  |             // Otherwise, subtract one from the reference count. This might
 | 
					
						
							|  |  |  |             // be negative!
 | 
					
						
							|  |  |  |             new_shared = shared - (1 << _Py_REF_SHARED_SHIFT); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #ifdef Py_REF_DEBUG
 | 
					
						
							| 
									
										
										
										
											2024-02-21 13:00:08 -05:00
										 |  |  |         if ((new_shared < 0 && _Py_REF_IS_MERGED(new_shared)) || | 
					
						
							|  |  |  |             (should_queue && is_dead(o))) | 
					
						
							| 
									
										
										
										
											2023-10-30 12:06:09 -04:00
										 |  |  |         { | 
					
						
							|  |  |  |             _Py_NegativeRefcount(filename, lineno, o); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  |     } while (!_Py_atomic_compare_exchange_ssize(&o->ob_ref_shared, | 
					
						
							|  |  |  |                                                 &shared, new_shared)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (should_queue) { | 
					
						
							| 
									
										
										
										
											2024-02-09 09:23:12 -05:00
										 |  |  | #ifdef Py_REF_DEBUG
 | 
					
						
							| 
									
										
										
										
											2024-04-08 12:11:36 -04:00
										 |  |  |         _Py_IncRefTotal(_PyThreadState_GET()); | 
					
						
							| 
									
										
										
										
											2024-02-09 09:23:12 -05:00
										 |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2024-02-09 17:08:32 -05:00
										 |  |  |         _Py_brc_queue_object(o); | 
					
						
							| 
									
										
										
										
											2023-10-30 12:06:09 -04:00
										 |  |  |     } | 
					
						
							|  |  |  |     else if (new_shared == _Py_REF_MERGED) { | 
					
						
							|  |  |  |         // refcount is zero AND merged
 | 
					
						
							| 
									
										
										
										
											2024-12-05 21:07:31 +00:00
										 |  |  |         return 1; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void | 
					
						
							|  |  |  | _Py_DecRefSharedDebug(PyObject *o, const char *filename, int lineno) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     if (_Py_DecRefSharedIsDead(o, filename, lineno)) { | 
					
						
							| 
									
										
										
										
											2023-10-30 12:06:09 -04:00
										 |  |  |         _Py_Dealloc(o); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void | 
					
						
							|  |  |  | _Py_DecRefShared(PyObject *o) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     _Py_DecRefSharedDebug(o, NULL, 0); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void | 
					
						
							|  |  |  | _Py_MergeZeroLocalRefcount(PyObject *op) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     assert(op->ob_ref_local == 0); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-19 14:21:01 -07:00
										 |  |  |     Py_ssize_t shared = _Py_atomic_load_ssize_acquire(&op->ob_ref_shared); | 
					
						
							| 
									
										
										
										
											2023-10-30 12:06:09 -04:00
										 |  |  |     if (shared == 0) { | 
					
						
							|  |  |  |         // Fast-path: shared refcount is zero (including flags)
 | 
					
						
							|  |  |  |         _Py_Dealloc(op); | 
					
						
							|  |  |  |         return; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-07-15 17:50:10 -04:00
										 |  |  |     // gh-121794: This must be before the store to `ob_ref_shared` (gh-119999),
 | 
					
						
							|  |  |  |     // but should outside the fast-path to maintain the invariant that
 | 
					
						
							|  |  |  |     // a zero `ob_tid` implies a merged refcount.
 | 
					
						
							|  |  |  |     _Py_atomic_store_uintptr_relaxed(&op->ob_tid, 0); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-10-30 12:06:09 -04:00
										 |  |  |     // Slow-path: atomically set the flags (low two bits) to _Py_REF_MERGED.
 | 
					
						
							|  |  |  |     Py_ssize_t new_shared; | 
					
						
							|  |  |  |     do { | 
					
						
							|  |  |  |         new_shared = (shared & ~_Py_REF_SHARED_FLAG_MASK) | _Py_REF_MERGED; | 
					
						
							|  |  |  |     } while (!_Py_atomic_compare_exchange_ssize(&op->ob_ref_shared, | 
					
						
							|  |  |  |                                                 &shared, new_shared)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (new_shared == _Py_REF_MERGED) { | 
					
						
							|  |  |  |         // i.e., the shared refcount is zero (only the flags are set) so we
 | 
					
						
							|  |  |  |         // deallocate the object.
 | 
					
						
							|  |  |  |         _Py_Dealloc(op); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Py_ssize_t | 
					
						
							|  |  |  | _Py_ExplicitMergeRefcount(PyObject *op, Py_ssize_t extra) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     assert(!_Py_IsImmortal(op)); | 
					
						
							| 
									
										
										
										
											2024-06-04 15:26:26 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | #ifdef Py_REF_DEBUG
 | 
					
						
							|  |  |  |     _Py_AddRefTotal(_PyThreadState_GET(), extra); | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // gh-119999: Write to ob_ref_local and ob_tid before merging the refcount.
 | 
					
						
							|  |  |  |     Py_ssize_t local = (Py_ssize_t)op->ob_ref_local; | 
					
						
							|  |  |  |     _Py_atomic_store_uint32_relaxed(&op->ob_ref_local, 0); | 
					
						
							|  |  |  |     _Py_atomic_store_uintptr_relaxed(&op->ob_tid, 0); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-10-30 12:06:09 -04:00
										 |  |  |     Py_ssize_t refcnt; | 
					
						
							|  |  |  |     Py_ssize_t new_shared; | 
					
						
							|  |  |  |     Py_ssize_t shared = _Py_atomic_load_ssize_relaxed(&op->ob_ref_shared); | 
					
						
							|  |  |  |     do { | 
					
						
							|  |  |  |         refcnt = Py_ARITHMETIC_RIGHT_SHIFT(Py_ssize_t, shared, _Py_REF_SHARED_SHIFT); | 
					
						
							| 
									
										
										
										
											2024-06-04 15:26:26 -04:00
										 |  |  |         refcnt += local; | 
					
						
							| 
									
										
										
										
											2023-10-30 12:06:09 -04:00
										 |  |  |         refcnt += extra; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         new_shared = _Py_REF_SHARED(refcnt, _Py_REF_MERGED); | 
					
						
							|  |  |  |     } while (!_Py_atomic_compare_exchange_ssize(&op->ob_ref_shared, | 
					
						
							|  |  |  |                                                 &shared, new_shared)); | 
					
						
							|  |  |  |     return refcnt; | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2024-12-05 21:07:31 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | // The more complicated "slow" path for undoing the resurrection of an object.
 | 
					
						
							|  |  |  | int | 
					
						
							|  |  |  | _PyObject_ResurrectEndSlow(PyObject *op) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     if (_Py_IsImmortal(op)) { | 
					
						
							|  |  |  |         return 1; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     if (_Py_IsOwnedByCurrentThread(op)) { | 
					
						
							|  |  |  |         // If the object is owned by the current thread, give up ownership and
 | 
					
						
							|  |  |  |         // merge the refcount. This isn't necessary in all cases, but it
 | 
					
						
							|  |  |  |         // simplifies the implementation.
 | 
					
						
							|  |  |  |         Py_ssize_t refcount = _Py_ExplicitMergeRefcount(op, -1); | 
					
						
							| 
									
										
										
										
											2025-02-25 12:03:28 -05:00
										 |  |  |         if (refcount == 0) { | 
					
						
							|  |  |  | #ifdef Py_TRACE_REFS
 | 
					
						
							|  |  |  |             _Py_ForgetReference(op); | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  |             return 0; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         return 1; | 
					
						
							| 
									
										
										
										
											2024-12-05 21:07:31 +00:00
										 |  |  |     } | 
					
						
							|  |  |  |     int is_dead = _Py_DecRefSharedIsDead(op, NULL, 0); | 
					
						
							| 
									
										
										
										
											2025-02-25 12:03:28 -05:00
										 |  |  |     if (is_dead) { | 
					
						
							|  |  |  | #ifdef Py_TRACE_REFS
 | 
					
						
							|  |  |  |         _Py_ForgetReference(op); | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  |         return 0; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return 1; | 
					
						
							| 
									
										
										
										
											2024-12-05 21:07:31 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-11-20 15:52:00 +02:00
										 |  |  | #endif  /* Py_GIL_DISABLED */
 | 
					
						
							| 
									
										
										
										
											2023-10-30 12:06:09 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-20 10:03:04 -06:00
										 |  |  | 
 | 
					
						
							|  |  |  | /**************************************/ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1997-05-02 03:12:38 +00:00
										 |  |  | PyObject * | 
					
						
							| 
									
										
										
										
											2000-07-09 15:48:49 +00:00
										 |  |  | PyObject_Init(PyObject *op, PyTypeObject *tp) | 
					
						
							| 
									
										
										
										
											1990-10-14 12:07:46 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2020-02-05 13:12:19 +01:00
										 |  |  |     if (op == NULL) { | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |         return PyErr_NoMemory(); | 
					
						
							| 
									
										
										
										
											2020-02-05 13:12:19 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-16 01:28:07 +02:00
										 |  |  |     _PyObject_Init(op, tp); | 
					
						
							|  |  |  |     return op; | 
					
						
							| 
									
										
										
										
											1990-10-14 12:07:46 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1997-05-15 21:31:03 +00:00
										 |  |  | PyVarObject * | 
					
						
							| 
									
										
										
										
											2006-02-15 17:27:45 +00:00
										 |  |  | PyObject_InitVar(PyVarObject *op, PyTypeObject *tp, Py_ssize_t size) | 
					
						
							| 
									
										
										
										
											2000-05-03 23:44:39 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2020-02-05 13:12:19 +01:00
										 |  |  |     if (op == NULL) { | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |         return (PyVarObject *) PyErr_NoMemory(); | 
					
						
							| 
									
										
										
										
											2020-02-05 13:12:19 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-16 01:28:07 +02:00
										 |  |  |     _PyObject_InitVar(op, tp, size); | 
					
						
							|  |  |  |     return op; | 
					
						
							| 
									
										
										
										
											2000-05-03 23:44:39 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | PyObject * | 
					
						
							| 
									
										
										
										
											2000-07-09 15:48:49 +00:00
										 |  |  | _PyObject_New(PyTypeObject *tp) | 
					
						
							| 
									
										
										
										
											2000-05-03 23:44:39 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2020-12-01 10:37:39 +01:00
										 |  |  |     PyObject *op = (PyObject *) PyObject_Malloc(_PyObject_SIZE(tp)); | 
					
						
							| 
									
										
										
										
											2020-04-08 00:38:15 +02:00
										 |  |  |     if (op == NULL) { | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |         return PyErr_NoMemory(); | 
					
						
							| 
									
										
										
										
											2020-04-08 00:38:15 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-06-16 01:28:07 +02:00
										 |  |  |     _PyObject_Init(op, tp); | 
					
						
							| 
									
										
										
										
											2020-04-08 00:38:15 +02:00
										 |  |  |     return op; | 
					
						
							| 
									
										
										
										
											2000-05-03 23:44:39 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1997-05-15 21:31:03 +00:00
										 |  |  | PyVarObject * | 
					
						
							| 
									
										
										
										
											2006-02-15 17:27:45 +00:00
										 |  |  | _PyObject_NewVar(PyTypeObject *tp, Py_ssize_t nitems) | 
					
						
							| 
									
										
										
										
											1990-10-14 12:07:46 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     PyVarObject *op; | 
					
						
							|  |  |  |     const size_t size = _PyObject_VAR_SIZE(tp, nitems); | 
					
						
							| 
									
										
										
										
											2020-12-01 10:37:39 +01:00
										 |  |  |     op = (PyVarObject *) PyObject_Malloc(size); | 
					
						
							| 
									
										
										
										
											2020-06-16 01:28:07 +02:00
										 |  |  |     if (op == NULL) { | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |         return (PyVarObject *)PyErr_NoMemory(); | 
					
						
							| 
									
										
										
										
											2020-06-16 01:28:07 +02:00
										 |  |  |     } | 
					
						
							|  |  |  |     _PyObject_InitVar(op, tp, nitems); | 
					
						
							|  |  |  |     return op; | 
					
						
							| 
									
										
										
										
											2000-05-03 23:44:39 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-07-30 19:59:21 +02:00
										 |  |  | void | 
					
						
							|  |  |  | PyObject_CallFinalizer(PyObject *self) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     PyTypeObject *tp = Py_TYPE(self); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-29 22:12:38 +02:00
										 |  |  |     if (tp->tp_finalize == NULL) | 
					
						
							| 
									
										
										
										
											2013-07-30 19:59:21 +02:00
										 |  |  |         return; | 
					
						
							|  |  |  |     /* tp_finalize should only be called once. */ | 
					
						
							| 
									
										
										
										
											2020-04-08 01:42:27 +02:00
										 |  |  |     if (_PyType_IS_GC(tp) && _PyGC_FINALIZED(self)) | 
					
						
							| 
									
										
										
										
											2013-07-30 19:59:21 +02:00
										 |  |  |         return; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     tp->tp_finalize(self); | 
					
						
							| 
									
										
										
										
											2020-04-08 01:42:27 +02:00
										 |  |  |     if (_PyType_IS_GC(tp)) { | 
					
						
							| 
									
										
										
										
											2018-07-10 17:19:53 +09:00
										 |  |  |         _PyGC_SET_FINALIZED(self); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2013-07-30 19:59:21 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | int | 
					
						
							|  |  |  | PyObject_CallFinalizerFromDealloc(PyObject *self) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2020-02-07 00:38:59 +01:00
										 |  |  |     if (Py_REFCNT(self) != 0) { | 
					
						
							| 
									
										
										
										
											2020-01-30 09:01:07 +01:00
										 |  |  |         _PyObject_ASSERT_FAILED_MSG(self, | 
					
						
							|  |  |  |                                     "PyObject_CallFinalizerFromDealloc called " | 
					
						
							|  |  |  |                                     "on object with a non-zero refcount"); | 
					
						
							| 
									
										
										
										
											2013-07-30 19:59:21 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-01-30 09:01:07 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /* Temporarily resurrect the object. */ | 
					
						
							| 
									
										
										
										
											2024-12-05 21:07:31 +00:00
										 |  |  |     _PyObject_ResurrectStart(self); | 
					
						
							| 
									
										
										
										
											2013-07-30 19:59:21 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     PyObject_CallFinalizer(self); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-26 17:16:37 +02:00
										 |  |  |     _PyObject_ASSERT_WITH_MSG(self, | 
					
						
							| 
									
										
										
										
											2020-02-07 00:38:59 +01:00
										 |  |  |                               Py_REFCNT(self) > 0, | 
					
						
							| 
									
										
										
										
											2018-10-26 17:16:37 +02:00
										 |  |  |                               "refcount is too small"); | 
					
						
							| 
									
										
										
										
											2020-01-30 09:01:07 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-02-25 12:03:28 -05:00
										 |  |  |     _PyObject_ASSERT(self, | 
					
						
							|  |  |  |                     (!_PyType_IS_GC(Py_TYPE(self)) | 
					
						
							|  |  |  |                     || _PyObject_GC_IS_TRACKED(self))); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-30 09:01:07 +01:00
										 |  |  |     /* Undo the temporary resurrection; can't use DECREF here, it would
 | 
					
						
							|  |  |  |      * cause a recursive call. */ | 
					
						
							| 
									
										
										
										
											2025-02-25 12:03:28 -05:00
										 |  |  |     if (_PyObject_ResurrectEnd(self)) { | 
					
						
							|  |  |  |         /* tp_finalize resurrected it!
 | 
					
						
							|  |  |  |            gh-130202: Note that the object may still be dead in the free | 
					
						
							|  |  |  |            threaded build in some circumstances, so it's not safe to access | 
					
						
							|  |  |  |            `self` after this point. For example, the last reference to the | 
					
						
							|  |  |  |            resurrected `self` may be held by another thread, which can | 
					
						
							|  |  |  |            concurrently deallocate it. */ | 
					
						
							|  |  |  |         return -1; | 
					
						
							| 
									
										
										
										
											2020-01-30 09:01:07 +01:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2013-07-30 19:59:21 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-02-25 12:03:28 -05:00
										 |  |  |     /* this is the normal path out, the caller continues with deallocation. */ | 
					
						
							|  |  |  |     return 0; | 
					
						
							| 
									
										
										
										
											2013-07-30 19:59:21 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-07-27 22:08:27 +00:00
										 |  |  | int | 
					
						
							|  |  |  | PyObject_Print(PyObject *op, FILE *fp, int flags) | 
					
						
							| 
									
										
										
										
											1990-10-14 12:07:46 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     int ret = 0; | 
					
						
							| 
									
										
										
										
											2024-07-31 01:21:43 +01:00
										 |  |  |     int write_error = 0; | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     if (PyErr_CheckSignals()) | 
					
						
							|  |  |  |         return -1; | 
					
						
							| 
									
										
										
										
											2025-02-25 09:24:48 +00:00
										 |  |  |     if (_Py_EnterRecursiveCall(" printing an object")) { | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |         return -1; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     clearerr(fp); /* Clear any previous error condition */ | 
					
						
							|  |  |  |     if (op == NULL) { | 
					
						
							|  |  |  |         Py_BEGIN_ALLOW_THREADS | 
					
						
							|  |  |  |         fprintf(fp, "<nil>"); | 
					
						
							|  |  |  |         Py_END_ALLOW_THREADS | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     else { | 
					
						
							| 
									
										
										
										
											2020-02-07 00:38:59 +01:00
										 |  |  |         if (Py_REFCNT(op) <= 0) { | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |             Py_BEGIN_ALLOW_THREADS | 
					
						
							| 
									
										
										
										
											2022-05-25 14:21:36 +02:00
										 |  |  |             fprintf(fp, "<refcnt %zd at %p>", Py_REFCNT(op), (void *)op); | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |             Py_END_ALLOW_THREADS | 
					
						
							| 
									
										
										
										
											2018-10-26 02:12:34 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |         else { | 
					
						
							|  |  |  |             PyObject *s; | 
					
						
							|  |  |  |             if (flags & Py_PRINT_RAW) | 
					
						
							|  |  |  |                 s = PyObject_Str(op); | 
					
						
							|  |  |  |             else | 
					
						
							|  |  |  |                 s = PyObject_Repr(op); | 
					
						
							| 
									
										
										
										
											2022-10-20 15:59:10 +02:00
										 |  |  |             if (s == NULL) { | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |                 ret = -1; | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2022-10-20 15:59:10 +02:00
										 |  |  |             else { | 
					
						
							|  |  |  |                 assert(PyUnicode_Check(s)); | 
					
						
							|  |  |  |                 const char *t; | 
					
						
							|  |  |  |                 Py_ssize_t len; | 
					
						
							|  |  |  |                 t = PyUnicode_AsUTF8AndSize(s, &len); | 
					
						
							| 
									
										
										
										
											2018-10-06 00:44:25 -06:00
										 |  |  |                 if (t == NULL) { | 
					
						
							|  |  |  |                     ret = -1; | 
					
						
							|  |  |  |                 } | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |                 else { | 
					
						
							| 
									
										
										
										
											2024-07-31 01:21:43 +01:00
										 |  |  |                     /* Versions of Android and OpenBSD from before 2023 fail to
 | 
					
						
							|  |  |  |                        set the `ferror` indicator when writing to a read-only | 
					
						
							|  |  |  |                        stream, so we need to check the return value. | 
					
						
							|  |  |  |                        (https://github.com/openbsd/src/commit/fc99cf9338942ecd9adc94ea08bf6188f0428c15) */
 | 
					
						
							|  |  |  |                     if (fwrite(t, 1, len, fp) != (size_t)len) { | 
					
						
							|  |  |  |                         write_error = 1; | 
					
						
							|  |  |  |                     } | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |                 } | 
					
						
							| 
									
										
										
										
											2022-10-20 15:59:10 +02:00
										 |  |  |                 Py_DECREF(s); | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     if (ret == 0) { | 
					
						
							| 
									
										
										
										
											2024-07-31 01:21:43 +01:00
										 |  |  |         if (write_error || ferror(fp)) { | 
					
						
							| 
									
										
										
										
											2017-04-16 10:46:38 +03:00
										 |  |  |             PyErr_SetFromErrno(PyExc_OSError); | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |             clearerr(fp); | 
					
						
							|  |  |  |             ret = -1; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return ret; | 
					
						
							| 
									
										
										
										
											1990-10-14 12:07:46 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-08-21 23:36:26 +00:00
										 |  |  | /* For debugging convenience.  Set a breakpoint here and call it from your DLL */ | 
					
						
							|  |  |  | void | 
					
						
							| 
									
										
											  
											
												Merged revisions 53451-53537 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
  r53454 | brett.cannon | 2007-01-15 20:12:08 +0100 (Mon, 15 Jan 2007) | 3 lines
  Add a note for strptime that just because strftime supports some extra
  directive that is not documented that strptime will as well.
........
  r53458 | vinay.sajip | 2007-01-16 10:50:07 +0100 (Tue, 16 Jan 2007) | 1 line
  Updated rotating file handlers to use _open().
........
  r53459 | marc-andre.lemburg | 2007-01-16 14:03:06 +0100 (Tue, 16 Jan 2007) | 2 lines
  Add news items for the recent pybench and platform changes.
........
  r53460 | sjoerd.mullender | 2007-01-16 17:42:38 +0100 (Tue, 16 Jan 2007) | 4 lines
  Fixed ntpath.expandvars to not replace references to non-existing
  variables with nothing.  Also added tests.
  This fixes bug #494589.
........
  r53464 | neal.norwitz | 2007-01-17 07:23:51 +0100 (Wed, 17 Jan 2007) | 1 line
  Give Calvin Spealman access for python-dev summaries.
........
  r53465 | neal.norwitz | 2007-01-17 09:37:26 +0100 (Wed, 17 Jan 2007) | 1 line
  Remove Calvin since he only has access to the website currently.
........
  r53466 | thomas.heller | 2007-01-17 10:40:34 +0100 (Wed, 17 Jan 2007) | 2 lines
  Replace C++ comments with C comments.
........
  r53472 | andrew.kuchling | 2007-01-17 20:55:06 +0100 (Wed, 17 Jan 2007) | 1 line
  [Part of bug #1599254] Add suggestion to Mailbox docs to use Maildir, and warn user to lock/unlock mailboxes when modifying them
........
  r53475 | georg.brandl | 2007-01-17 22:09:04 +0100 (Wed, 17 Jan 2007) | 2 lines
  Bug #1637967: missing //= operator in list.
........
  r53477 | georg.brandl | 2007-01-17 22:19:58 +0100 (Wed, 17 Jan 2007) | 2 lines
  Bug #1629125: fix wrong data type (int -> Py_ssize_t) in PyDict_Next docs.
........
  r53481 | neal.norwitz | 2007-01-18 06:40:58 +0100 (Thu, 18 Jan 2007) | 1 line
  Try reverting part of r53145 that seems to cause the Windows buildbots to fail in test_uu.UUFileTest.test_encode
........
  r53482 | fred.drake | 2007-01-18 06:42:30 +0100 (Thu, 18 Jan 2007) | 1 line
  add missing version entry
........
  r53483 | neal.norwitz | 2007-01-18 07:20:55 +0100 (Thu, 18 Jan 2007) | 7 lines
  This test doesn't pass on Windows.  The cause seems to be that chmod
  doesn't support the same funcationality as on Unix.  I'm not sure if
  this fix is the best (or if it will even work)--it's a test to see
  if the buildbots start passing again.
  It might be better to not even run this test if it's windows (or non-posix).
........
  r53488 | neal.norwitz | 2007-01-19 06:53:33 +0100 (Fri, 19 Jan 2007) | 1 line
  SF #1635217, Fix unbalanced paren
........
  r53489 | martin.v.loewis | 2007-01-19 07:42:22 +0100 (Fri, 19 Jan 2007) | 3 lines
  Prefix AST symbols with _Py_. Fixes #1637022.
  Will backport.
........
  r53497 | martin.v.loewis | 2007-01-19 19:01:38 +0100 (Fri, 19 Jan 2007) | 2 lines
  Add UUIDs for 2.5.1 and 2.5.2
........
  r53499 | raymond.hettinger | 2007-01-19 19:07:18 +0100 (Fri, 19 Jan 2007) | 1 line
  SF# 1635892:  Fix docs for betavariate's input parameters .
........
  r53503 | martin.v.loewis | 2007-01-20 15:05:39 +0100 (Sat, 20 Jan 2007) | 2 lines
  Merge 53501 and 53502 from 25 branch:
  Add /GS- for AMD64 and Itanium builds where missing.
........
  r53504 | walter.doerwald | 2007-01-20 18:28:31 +0100 (Sat, 20 Jan 2007) | 2 lines
  Port test_resource.py to unittest.
........
  r53505 | walter.doerwald | 2007-01-20 19:19:33 +0100 (Sat, 20 Jan 2007) | 2 lines
  Add argument tests an calls of resource.getrusage().
........
  r53506 | walter.doerwald | 2007-01-20 20:03:17 +0100 (Sat, 20 Jan 2007) | 2 lines
  resource.RUSAGE_BOTH might not exist.
........
  r53507 | walter.doerwald | 2007-01-21 00:07:28 +0100 (Sun, 21 Jan 2007) | 2 lines
  Port test_new.py to unittest.
........
  r53508 | martin.v.loewis | 2007-01-21 10:33:07 +0100 (Sun, 21 Jan 2007) | 2 lines
  Patch #1610575: Add support for _Bool to struct.
........
  r53509 | georg.brandl | 2007-01-21 11:28:43 +0100 (Sun, 21 Jan 2007) | 3 lines
  Bug #1486663: don't reject keyword arguments for subclasses of builtin
  types.
........
  r53511 | georg.brandl | 2007-01-21 11:35:10 +0100 (Sun, 21 Jan 2007) | 2 lines
  Patch #1627441: close sockets properly in urllib2.
........
  r53517 | georg.brandl | 2007-01-22 20:40:21 +0100 (Mon, 22 Jan 2007) | 3 lines
  Use new email module names (#1637162, #1637159, #1637157).
........
  r53518 | andrew.kuchling | 2007-01-22 21:26:40 +0100 (Mon, 22 Jan 2007) | 1 line
  Improve pattern used for mbox 'From' lines; add a simple test
........
  r53519 | andrew.kuchling | 2007-01-22 21:27:50 +0100 (Mon, 22 Jan 2007) | 1 line
  Make comment match the code
........
  r53522 | georg.brandl | 2007-01-22 22:10:33 +0100 (Mon, 22 Jan 2007) | 2 lines
  Bug #1249573: fix rfc822.parsedate not accepting a certain date format
........
  r53524 | georg.brandl | 2007-01-22 22:23:41 +0100 (Mon, 22 Jan 2007) | 2 lines
  Bug #1627316: handle error in condition/ignore pdb commands more gracefully.
........
  r53526 | lars.gustaebel | 2007-01-23 12:17:33 +0100 (Tue, 23 Jan 2007) | 4 lines
  Patch #1507247: tarfile.py: use current umask for intermediate
  directories.
........
  r53527 | thomas.wouters | 2007-01-23 14:42:00 +0100 (Tue, 23 Jan 2007) | 13 lines
  SF patch #1630975: Fix crash when replacing sys.stdout in sitecustomize
  When running the interpreter in an environment that would cause it to set
  stdout/stderr/stdin's encoding, having a sitecustomize that would replace
  them with something other than PyFile objects would crash the interpreter.
  Fix it by simply ignoring the encoding-setting for non-files.
  This could do with a test, but I can think of no maintainable and portable
  way to test this bug, short of adding a sitecustomize.py to the buildsystem
  and have it always run with it (hmmm....)
........
  r53528 | thomas.wouters | 2007-01-23 14:50:49 +0100 (Tue, 23 Jan 2007) | 4 lines
  Add news entry about last checkin (oops.)
........
  r53531 | martin.v.loewis | 2007-01-23 22:11:47 +0100 (Tue, 23 Jan 2007) | 4 lines
  Make PyTraceBack_Here use the current thread, not the
  frame's thread state. Fixes #1579370.
  Will backport.
........
  r53535 | brett.cannon | 2007-01-24 00:21:22 +0100 (Wed, 24 Jan 2007) | 5 lines
  Fix crasher for when an object's __del__ creates a new weakref to itself.
  Patch only fixes new-style classes; classic classes still buggy.
  Closes bug #1377858.  Already backported.
........
  r53536 | walter.doerwald | 2007-01-24 01:42:19 +0100 (Wed, 24 Jan 2007) | 2 lines
  Port test_popen.py to unittest.
........
											
										 
											2007-02-01 18:02:27 +00:00
										 |  |  | _Py_BreakPoint(void) | 
					
						
							| 
									
										
										
										
											2006-08-21 23:36:26 +00:00
										 |  |  | { | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2003-01-13 20:13:12 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-11 13:01:15 +02:00
										 |  |  | /* Heuristic checking if the object memory is uninitialized or deallocated.
 | 
					
						
							|  |  |  |    Rely on the debug hooks on Python memory allocators: | 
					
						
							|  |  |  |    see _PyMem_IsPtrFreed(). | 
					
						
							| 
									
										
										
										
											2018-10-23 17:39:40 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |    The function can be used to prevent segmentation fault on dereferencing | 
					
						
							| 
									
										
										
										
											2019-04-11 13:01:15 +02:00
										 |  |  |    pointers like 0xDDDDDDDDDDDDDDDD. */ | 
					
						
							| 
									
										
										
										
											2018-10-23 17:39:40 +02:00
										 |  |  | int | 
					
						
							|  |  |  | _PyObject_IsFreed(PyObject *op) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2020-02-07 03:04:21 +01:00
										 |  |  |     if (_PyMem_IsPtrFreed(op) || _PyMem_IsPtrFreed(Py_TYPE(op))) { | 
					
						
							| 
									
										
										
										
											2018-11-22 16:32:57 +01:00
										 |  |  |         return 1; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-04-11 11:33:27 +02:00
										 |  |  |     return 0; | 
					
						
							| 
									
										
										
										
											2018-10-23 17:39:40 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-01-23 16:24:35 +00:00
										 |  |  | /* For debugging convenience.  See Misc/gdbinit for some useful gdb hooks */ | 
					
						
							| 
									
										
										
										
											2006-08-21 23:36:26 +00:00
										 |  |  | void | 
					
						
							|  |  |  | _PyObject_Dump(PyObject* op) | 
					
						
							| 
									
										
										
										
											2001-01-23 16:24:35 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2018-10-23 17:39:40 +02:00
										 |  |  |     if (_PyObject_IsFreed(op)) { | 
					
						
							|  |  |  |         /* It seems like the object memory has been freed:
 | 
					
						
							|  |  |  |            don't access it to prevent a segmentation fault. */ | 
					
						
							| 
									
										
										
										
											2019-09-17 23:36:28 +02:00
										 |  |  |         fprintf(stderr, "<object at %p is freed>\n", op); | 
					
						
							| 
									
										
										
										
											2019-10-07 18:42:01 +02:00
										 |  |  |         fflush(stderr); | 
					
						
							| 
									
										
										
										
											2018-11-22 16:32:57 +01:00
										 |  |  |         return; | 
					
						
							| 
									
										
										
										
											2018-10-23 17:39:40 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-07 18:42:01 +02:00
										 |  |  |     /* first, write fields which are the least likely to crash */ | 
					
						
							|  |  |  |     fprintf(stderr, "object address  : %p\n", (void *)op); | 
					
						
							| 
									
										
										
										
											2022-05-25 14:21:36 +02:00
										 |  |  |     fprintf(stderr, "object refcount : %zd\n", Py_REFCNT(op)); | 
					
						
							| 
									
										
										
										
											2019-10-07 18:42:01 +02:00
										 |  |  |     fflush(stderr); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     PyTypeObject *type = Py_TYPE(op); | 
					
						
							|  |  |  |     fprintf(stderr, "object type     : %p\n", type); | 
					
						
							|  |  |  |     fprintf(stderr, "object type name: %s\n", | 
					
						
							|  |  |  |             type==NULL ? "NULL" : type->tp_name); | 
					
						
							| 
									
										
										
										
											2018-10-23 17:39:40 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-07 18:42:01 +02:00
										 |  |  |     /* the most dangerous part */ | 
					
						
							|  |  |  |     fprintf(stderr, "object repr     : "); | 
					
						
							| 
									
										
										
										
											2018-10-23 17:39:40 +02:00
										 |  |  |     fflush(stderr); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-07 18:42:01 +02:00
										 |  |  |     PyGILState_STATE gil = PyGILState_Ensure(); | 
					
						
							| 
									
										
										
										
											2023-03-08 17:03:18 +00:00
										 |  |  |     PyObject *exc = PyErr_GetRaisedException(); | 
					
						
							| 
									
										
										
										
											2019-10-07 18:42:01 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-23 17:39:40 +02:00
										 |  |  |     (void)PyObject_Print(op, stderr, 0); | 
					
						
							|  |  |  |     fflush(stderr); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-08 17:03:18 +00:00
										 |  |  |     PyErr_SetRaisedException(exc); | 
					
						
							| 
									
										
										
										
											2018-10-23 17:39:40 +02:00
										 |  |  |     PyGILState_Release(gil); | 
					
						
							| 
									
										
										
										
											2019-10-07 18:42:01 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     fprintf(stderr, "\n"); | 
					
						
							| 
									
										
										
										
											2018-10-23 17:39:40 +02:00
										 |  |  |     fflush(stderr); | 
					
						
							| 
									
										
										
										
											2001-01-23 16:24:35 +00:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2001-01-23 16:33:18 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1997-05-02 03:12:38 +00:00
										 |  |  | PyObject * | 
					
						
							| 
									
										
										
										
											2000-07-09 15:48:49 +00:00
										 |  |  | PyObject_Repr(PyObject *v) | 
					
						
							| 
									
										
										
										
											1990-10-14 12:07:46 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     PyObject *res; | 
					
						
							|  |  |  |     if (PyErr_CheckSignals()) | 
					
						
							|  |  |  |         return NULL; | 
					
						
							|  |  |  |     if (v == NULL) | 
					
						
							|  |  |  |         return PyUnicode_FromString("<NULL>"); | 
					
						
							|  |  |  |     if (Py_TYPE(v)->tp_repr == NULL) | 
					
						
							|  |  |  |         return PyUnicode_FromFormat("<%s object at %p>", | 
					
						
							| 
									
										
										
										
											2020-02-07 03:04:21 +01:00
										 |  |  |                                     Py_TYPE(v)->tp_name, v); | 
					
						
							| 
									
										
										
										
											2013-08-26 14:05:19 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-05 00:51:22 +01:00
										 |  |  |     PyThreadState *tstate = _PyThreadState_GET(); | 
					
						
							| 
									
										
										
										
											2013-08-26 14:05:19 +02:00
										 |  |  | #ifdef Py_DEBUG
 | 
					
						
							|  |  |  |     /* PyObject_Repr() must not be called with an exception set,
 | 
					
						
							| 
									
										
										
										
											2017-01-18 14:12:51 +01:00
										 |  |  |        because it can clear it (directly or indirectly) and so the | 
					
						
							| 
									
										
										
										
											2015-10-07 10:26:23 +00:00
										 |  |  |        caller loses its exception */ | 
					
						
							| 
									
										
										
										
											2019-11-05 00:51:22 +01:00
										 |  |  |     assert(!_PyErr_Occurred(tstate)); | 
					
						
							| 
									
										
										
										
											2013-08-26 14:05:19 +02:00
										 |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-03 22:12:11 +02:00
										 |  |  |     /* It is possible for a type to have a tp_repr representation that loops
 | 
					
						
							|  |  |  |        infinitely. */ | 
					
						
							| 
									
										
										
										
											2022-05-04 13:30:23 +02:00
										 |  |  |     if (_Py_EnterRecursiveCallTstate(tstate, | 
					
						
							|  |  |  |                                      " while getting the repr of an object")) { | 
					
						
							| 
									
										
										
										
											2017-12-03 22:12:11 +02:00
										 |  |  |         return NULL; | 
					
						
							| 
									
										
										
										
											2019-11-05 00:51:22 +01:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-02-07 03:04:21 +01:00
										 |  |  |     res = (*Py_TYPE(v)->tp_repr)(v); | 
					
						
							| 
									
										
										
										
											2022-05-04 13:30:23 +02:00
										 |  |  |     _Py_LeaveRecursiveCallTstate(tstate); | 
					
						
							| 
									
										
										
										
											2019-11-05 00:51:22 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     if (res == NULL) { | 
					
						
							| 
									
										
										
										
											2011-12-01 03:22:44 +01:00
										 |  |  |         return NULL; | 
					
						
							| 
									
										
										
										
											2019-11-05 00:51:22 +01:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2011-12-01 03:22:44 +01:00
										 |  |  |     if (!PyUnicode_Check(res)) { | 
					
						
							| 
									
										
										
										
											2019-11-05 00:51:22 +01:00
										 |  |  |         _PyErr_Format(tstate, PyExc_TypeError, | 
					
						
							|  |  |  |                       "__repr__ returned non-string (type %.200s)", | 
					
						
							| 
									
										
										
										
											2020-02-07 03:04:21 +01:00
										 |  |  |                       Py_TYPE(res)->tp_name); | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |         Py_DECREF(res); | 
					
						
							|  |  |  |         return NULL; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return res; | 
					
						
							| 
									
										
										
										
											2007-05-18 17:15:44 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1997-05-02 03:12:38 +00:00
										 |  |  | PyObject * | 
					
						
							| 
									
										
										
										
											2007-11-06 21:34:58 +00:00
										 |  |  | PyObject_Str(PyObject *v) | 
					
						
							| 
									
										
										
										
											1993-11-05 10:22:19 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     PyObject *res; | 
					
						
							|  |  |  |     if (PyErr_CheckSignals()) | 
					
						
							|  |  |  |         return NULL; | 
					
						
							|  |  |  |     if (v == NULL) | 
					
						
							|  |  |  |         return PyUnicode_FromString("<NULL>"); | 
					
						
							|  |  |  |     if (PyUnicode_CheckExact(v)) { | 
					
						
							| 
									
										
										
										
											2022-11-10 23:58:07 +01:00
										 |  |  |         return Py_NewRef(v); | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     } | 
					
						
							|  |  |  |     if (Py_TYPE(v)->tp_str == NULL) | 
					
						
							|  |  |  |         return PyObject_Repr(v); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-05 00:51:22 +01:00
										 |  |  |     PyThreadState *tstate = _PyThreadState_GET(); | 
					
						
							| 
									
										
										
										
											2013-08-26 14:05:19 +02:00
										 |  |  | #ifdef Py_DEBUG
 | 
					
						
							|  |  |  |     /* PyObject_Str() must not be called with an exception set,
 | 
					
						
							| 
									
										
										
										
											2017-01-18 14:12:51 +01:00
										 |  |  |        because it can clear it (directly or indirectly) and so the | 
					
						
							| 
									
										
										
										
											2014-02-09 10:43:21 +10:00
										 |  |  |        caller loses its exception */ | 
					
						
							| 
									
										
										
										
											2019-11-05 00:51:22 +01:00
										 |  |  |     assert(!_PyErr_Occurred(tstate)); | 
					
						
							| 
									
										
										
										
											2013-08-26 14:05:19 +02:00
										 |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     /* It is possible for a type to have a tp_str representation that loops
 | 
					
						
							|  |  |  |        infinitely. */ | 
					
						
							| 
									
										
										
										
											2022-05-04 13:30:23 +02:00
										 |  |  |     if (_Py_EnterRecursiveCallTstate(tstate, " while getting the str of an object")) { | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |         return NULL; | 
					
						
							| 
									
										
										
										
											2019-11-05 00:51:22 +01:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     res = (*Py_TYPE(v)->tp_str)(v); | 
					
						
							| 
									
										
										
										
											2022-05-04 13:30:23 +02:00
										 |  |  |     _Py_LeaveRecursiveCallTstate(tstate); | 
					
						
							| 
									
										
										
										
											2019-11-05 00:51:22 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     if (res == NULL) { | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |         return NULL; | 
					
						
							| 
									
										
										
										
											2019-11-05 00:51:22 +01:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     if (!PyUnicode_Check(res)) { | 
					
						
							| 
									
										
										
										
											2019-11-05 00:51:22 +01:00
										 |  |  |         _PyErr_Format(tstate, PyExc_TypeError, | 
					
						
							|  |  |  |                       "__str__ returned non-string (type %.200s)", | 
					
						
							|  |  |  |                       Py_TYPE(res)->tp_name); | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |         Py_DECREF(res); | 
					
						
							|  |  |  |         return NULL; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2011-11-20 19:48:36 +01:00
										 |  |  |     assert(_PyUnicode_CheckConsistency(res, 1)); | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     return res; | 
					
						
							| 
									
										
										
										
											2005-08-12 17:34:58 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-06-11 18:37:52 +00:00
										 |  |  | PyObject * | 
					
						
							|  |  |  | PyObject_ASCII(PyObject *v) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     PyObject *repr, *ascii, *res; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     repr = PyObject_Repr(v); | 
					
						
							|  |  |  |     if (repr == NULL) | 
					
						
							|  |  |  |         return NULL; | 
					
						
							| 
									
										
										
										
											2008-06-11 18:37:52 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-04-14 18:44:10 +02:00
										 |  |  |     if (PyUnicode_IS_ASCII(repr)) | 
					
						
							|  |  |  |         return repr; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     /* repr is guaranteed to be a PyUnicode object by PyObject_Repr */ | 
					
						
							| 
									
										
										
										
											2011-09-28 07:41:54 +02:00
										 |  |  |     ascii = _PyUnicode_AsASCIIString(repr, "backslashreplace"); | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     Py_DECREF(repr); | 
					
						
							|  |  |  |     if (ascii == NULL) | 
					
						
							|  |  |  |         return NULL; | 
					
						
							| 
									
										
										
										
											2008-06-11 18:37:52 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     res = PyUnicode_DecodeASCII( | 
					
						
							|  |  |  |         PyBytes_AS_STRING(ascii), | 
					
						
							|  |  |  |         PyBytes_GET_SIZE(ascii), | 
					
						
							|  |  |  |         NULL); | 
					
						
							| 
									
										
										
										
											2008-06-11 18:37:52 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     Py_DECREF(ascii); | 
					
						
							|  |  |  |     return res; | 
					
						
							| 
									
										
										
										
											2008-06-11 18:37:52 +00:00
										 |  |  | } | 
					
						
							| 
									
										
											  
											
												Changes to recursive-object comparisons, having to do with a test case
I found where rich comparison of unequal recursive objects gave
unintuituve results.  In a discussion with Tim, where we discovered
that our intuition on when a<=b should be true was failing, we decided
to outlaw ordering comparisons on recursive objects.  (Once we have
fixed our intuition and designed a matching algorithm that's practical
and reasonable to implement, we can allow such orderings again.)
- Refactored the recursive-object comparison framework; more is now
  done in the support routines so less needs to be done in the calling
  routines (even at the expense of slowing it down a bit -- this
  should normally never be invoked, it's mostly just there to avoid
  blowing up the interpreter).
- Changed the framework so that the comparison operator used is also
  stored.  (The dictionary now stores triples (v, w, op) instead of
  pairs (v, w).)
- Changed the nesting limit to a more reasonable small 20; this only
  slows down comparisons of very deeply nested objects (unlikely to
  occur in practice), while speeding up comparisons of recursive
  objects (previously, this would first waste time and space on 500
  nested comparisons before it would start detecting recursion).
- Changed rich comparisons for recursive objects to raise a ValueError
  exception when recursion is detected for ordering oprators (<, <=,
  >, >=).
Unrelated change:
- Moved PyObject_Unicode() to just under PyObject_Str(), where it
  belongs.  MAL's patch must've inserted in a random spot between two
  functions in the file -- between two helpers for rich comparison...
											
										 
											2001-01-18 22:07:06 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-08-26 16:46:47 +00:00
										 |  |  | PyObject * | 
					
						
							|  |  |  | PyObject_Bytes(PyObject *v) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     PyObject *result, *func; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (v == NULL) | 
					
						
							|  |  |  |         return PyBytes_FromString("<NULL>"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (PyBytes_CheckExact(v)) { | 
					
						
							| 
									
										
										
										
											2022-11-10 23:58:07 +01:00
										 |  |  |         return Py_NewRef(v); | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-08 13:39:07 -07:00
										 |  |  |     func = _PyObject_LookupSpecial(v, &_Py_ID(__bytes__)); | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     if (func != NULL) { | 
					
						
							| 
									
										
										
										
											2021-10-12 00:42:23 +02:00
										 |  |  |         result = _PyObject_CallNoArgs(func); | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |         Py_DECREF(func); | 
					
						
							|  |  |  |         if (result == NULL) | 
					
						
							| 
									
										
										
										
											2010-09-11 16:39:57 +00:00
										 |  |  |             return NULL; | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |         if (!PyBytes_Check(result)) { | 
					
						
							| 
									
										
										
										
											2010-09-11 16:39:57 +00:00
										 |  |  |             PyErr_Format(PyExc_TypeError, | 
					
						
							|  |  |  |                          "__bytes__ returned non-bytes (type %.200s)", | 
					
						
							|  |  |  |                          Py_TYPE(result)->tp_name); | 
					
						
							|  |  |  |             Py_DECREF(result); | 
					
						
							|  |  |  |             return NULL; | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |         } | 
					
						
							|  |  |  |         return result; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     else if (PyErr_Occurred()) | 
					
						
							|  |  |  |         return NULL; | 
					
						
							|  |  |  |     return PyBytes_FromObject(v); | 
					
						
							| 
									
										
										
										
											2008-08-26 16:46:47 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-07-22 12:08:27 -04:00
										 |  |  | static void | 
					
						
							|  |  |  | clear_freelist(struct _Py_freelist *freelist, int is_finalization, | 
					
						
							|  |  |  |                freefunc dofree) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     void *ptr; | 
					
						
							|  |  |  |     while ((ptr = _PyFreeList_PopNoStats(freelist)) != NULL) { | 
					
						
							|  |  |  |         dofree(ptr); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     assert(freelist->size == 0 || freelist->size == -1); | 
					
						
							|  |  |  |     assert(freelist->freelist == NULL); | 
					
						
							|  |  |  |     if (is_finalization) { | 
					
						
							|  |  |  |         freelist->size = -1; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static void | 
					
						
							|  |  |  | free_object(void *obj) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     PyObject *op = (PyObject *)obj; | 
					
						
							| 
									
										
										
										
											2024-07-23 12:24:24 -04:00
										 |  |  |     PyTypeObject *tp = Py_TYPE(op); | 
					
						
							|  |  |  |     tp->tp_free(op); | 
					
						
							|  |  |  |     Py_DECREF(tp); | 
					
						
							| 
									
										
										
										
											2024-07-22 12:08:27 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-02-10 09:57:04 +09:00
										 |  |  | void | 
					
						
							| 
									
										
										
										
											2024-07-22 12:08:27 -04:00
										 |  |  | _PyObject_ClearFreeLists(struct _Py_freelists *freelists, int is_finalization) | 
					
						
							| 
									
										
										
										
											2024-02-10 09:57:04 +09:00
										 |  |  | { | 
					
						
							|  |  |  |     // In the free-threaded build, freelists are per-PyThreadState and cleared in PyThreadState_Clear()
 | 
					
						
							|  |  |  |     // In the default build, freelists are per-interpreter and cleared in finalize_interp_types()
 | 
					
						
							| 
									
										
										
										
											2024-07-22 12:08:27 -04:00
										 |  |  |     clear_freelist(&freelists->floats, is_finalization, free_object); | 
					
						
							| 
									
										
										
										
											2025-06-13 16:32:26 +03:00
										 |  |  |     clear_freelist(&freelists->complexes, is_finalization, free_object); | 
					
						
							| 
									
										
										
										
											2024-07-22 12:08:27 -04:00
										 |  |  |     for (Py_ssize_t i = 0; i < PyTuple_MAXSAVESIZE; i++) { | 
					
						
							|  |  |  |         clear_freelist(&freelists->tuples[i], is_finalization, free_object); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     clear_freelist(&freelists->lists, is_finalization, free_object); | 
					
						
							| 
									
										
										
										
											2025-01-29 10:15:24 +01:00
										 |  |  |     clear_freelist(&freelists->list_iters, is_finalization, free_object); | 
					
						
							|  |  |  |     clear_freelist(&freelists->tuple_iters, is_finalization, free_object); | 
					
						
							| 
									
										
										
										
											2024-07-22 12:08:27 -04:00
										 |  |  |     clear_freelist(&freelists->dicts, is_finalization, free_object); | 
					
						
							|  |  |  |     clear_freelist(&freelists->dictkeys, is_finalization, PyMem_Free); | 
					
						
							|  |  |  |     clear_freelist(&freelists->slices, is_finalization, free_object); | 
					
						
							| 
									
										
										
										
											2025-04-06 22:40:52 +02:00
										 |  |  |     clear_freelist(&freelists->ranges, is_finalization, free_object); | 
					
						
							|  |  |  |     clear_freelist(&freelists->range_iters, is_finalization, free_object); | 
					
						
							| 
									
										
										
										
											2024-07-22 12:08:27 -04:00
										 |  |  |     clear_freelist(&freelists->contexts, is_finalization, free_object); | 
					
						
							|  |  |  |     clear_freelist(&freelists->async_gens, is_finalization, free_object); | 
					
						
							|  |  |  |     clear_freelist(&freelists->async_gen_asends, is_finalization, free_object); | 
					
						
							| 
									
										
										
										
											2024-07-23 12:24:24 -04:00
										 |  |  |     clear_freelist(&freelists->futureiters, is_finalization, free_object); | 
					
						
							| 
									
										
										
										
											2024-07-22 12:08:27 -04:00
										 |  |  |     if (is_finalization) { | 
					
						
							|  |  |  |         // Only clear object stack chunks during finalization. We use object
 | 
					
						
							|  |  |  |         // stacks during GC, so emptying the free-list is counterproductive.
 | 
					
						
							|  |  |  |         clear_freelist(&freelists->object_stack_chunks, 1, PyMem_RawFree); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2024-10-10 12:11:06 +02:00
										 |  |  |     clear_freelist(&freelists->unicode_writers, is_finalization, PyMem_Free); | 
					
						
							| 
									
										
										
										
											2024-12-13 11:06:26 +01:00
										 |  |  |     clear_freelist(&freelists->ints, is_finalization, free_object); | 
					
						
							| 
									
										
										
										
											2025-04-05 17:51:05 +02:00
										 |  |  |     clear_freelist(&freelists->pycfunctionobject, is_finalization, PyObject_GC_Del); | 
					
						
							|  |  |  |     clear_freelist(&freelists->pycmethodobject, is_finalization, PyObject_GC_Del); | 
					
						
							| 
									
										
										
										
											2025-01-12 14:01:49 +01:00
										 |  |  |     clear_freelist(&freelists->pymethodobjects, is_finalization, free_object); | 
					
						
							| 
									
										
										
										
											2024-02-10 09:57:04 +09:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2019-11-05 16:48:04 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | /*
 | 
					
						
							|  |  |  | def _PyObject_FunctionStr(x): | 
					
						
							|  |  |  |     try: | 
					
						
							|  |  |  |         qualname = x.__qualname__ | 
					
						
							|  |  |  |     except AttributeError: | 
					
						
							|  |  |  |         return str(x) | 
					
						
							|  |  |  |     try: | 
					
						
							|  |  |  |         mod = x.__module__ | 
					
						
							|  |  |  |         if mod is not None and mod != 'builtins': | 
					
						
							|  |  |  |             return f"{x.__module__}.{qualname}()" | 
					
						
							|  |  |  |     except AttributeError: | 
					
						
							|  |  |  |         pass | 
					
						
							|  |  |  |     return qualname | 
					
						
							|  |  |  | */ | 
					
						
							|  |  |  | PyObject * | 
					
						
							|  |  |  | _PyObject_FunctionStr(PyObject *x) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     assert(!PyErr_Occurred()); | 
					
						
							|  |  |  |     PyObject *qualname; | 
					
						
							| 
									
										
										
										
											2023-07-11 22:13:27 +03:00
										 |  |  |     int ret = PyObject_GetOptionalAttr(x, &_Py_ID(__qualname__), &qualname); | 
					
						
							| 
									
										
										
										
											2019-11-05 16:48:04 +01:00
										 |  |  |     if (qualname == NULL) { | 
					
						
							|  |  |  |         if (ret < 0) { | 
					
						
							|  |  |  |             return NULL; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         return PyObject_Str(x); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     PyObject *module; | 
					
						
							|  |  |  |     PyObject *result = NULL; | 
					
						
							| 
									
										
										
										
											2023-07-11 22:13:27 +03:00
										 |  |  |     ret = PyObject_GetOptionalAttr(x, &_Py_ID(__module__), &module); | 
					
						
							| 
									
										
										
										
											2019-11-05 16:48:04 +01:00
										 |  |  |     if (module != NULL && module != Py_None) { | 
					
						
							| 
									
										
										
										
											2022-02-08 13:39:07 -07:00
										 |  |  |         ret = PyObject_RichCompareBool(module, &_Py_ID(builtins), Py_NE); | 
					
						
							| 
									
										
										
										
											2019-11-05 16:48:04 +01:00
										 |  |  |         if (ret < 0) { | 
					
						
							|  |  |  |             // error
 | 
					
						
							|  |  |  |             goto done; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         if (ret > 0) { | 
					
						
							|  |  |  |             result = PyUnicode_FromFormat("%S.%S()", module, qualname); | 
					
						
							|  |  |  |             goto done; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     else if (ret < 0) { | 
					
						
							|  |  |  |         goto done; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     result = PyUnicode_FromFormat("%S()", qualname); | 
					
						
							|  |  |  | done: | 
					
						
							|  |  |  |     Py_DECREF(qualname); | 
					
						
							|  |  |  |     Py_XDECREF(module); | 
					
						
							|  |  |  |     return result; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-02-01 13:59:22 +00:00
										 |  |  | /* For Python 3.0.1 and later, the old three-way comparison has been
 | 
					
						
							|  |  |  |    completely removed in favour of rich comparisons.  PyObject_Compare() and | 
					
						
							|  |  |  |    PyObject_Cmp() are gone, and the builtin cmp function no longer exists. | 
					
						
							| 
									
										
										
										
											2019-05-31 04:13:39 +02:00
										 |  |  |    The old tp_compare slot has been renamed to tp_as_async, and should no | 
					
						
							| 
									
										
										
										
											2009-02-01 13:59:22 +00:00
										 |  |  |    longer be used.  Use tp_richcompare instead. | 
					
						
							| 
									
										
										
										
											2007-11-06 21:34:58 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-08-24 00:41:19 +00:00
										 |  |  |    See (*) below for practical amendments. | 
					
						
							| 
									
										
										
										
											2002-05-31 20:03:54 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-02-01 13:59:22 +00:00
										 |  |  |    tp_richcompare gets called with a first argument of the appropriate type | 
					
						
							|  |  |  |    and a second object of an arbitrary type.  We never do any kind of | 
					
						
							|  |  |  |    coercion. | 
					
						
							| 
									
										
										
										
											2002-05-31 20:03:54 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-02-01 13:59:22 +00:00
										 |  |  |    The tp_richcompare slot should return an object, as follows: | 
					
						
							| 
									
										
										
										
											2001-01-17 21:27:02 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-08-24 00:41:19 +00:00
										 |  |  |     NULL if an exception occurred | 
					
						
							|  |  |  |     NotImplemented if the requested comparison is not implemented | 
					
						
							|  |  |  |     any other false value if the requested comparison is false | 
					
						
							|  |  |  |     any other true value if the requested comparison is true | 
					
						
							| 
									
										
										
										
											2001-01-17 15:24:28 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-08-24 00:41:19 +00:00
										 |  |  |   The PyObject_RichCompare[Bool]() wrappers raise TypeError when they get | 
					
						
							|  |  |  |   NotImplemented. | 
					
						
							| 
									
										
										
										
											2001-01-04 01:48:10 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-08-24 00:41:19 +00:00
										 |  |  |   (*) Practical amendments: | 
					
						
							| 
									
										
										
										
											2001-01-17 15:24:28 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-08-24 00:41:19 +00:00
										 |  |  |   - If rich comparison returns NotImplemented, == and != are decided by | 
					
						
							|  |  |  |     comparing the object pointer (i.e. falling back to the base object | 
					
						
							|  |  |  |     implementation). | 
					
						
							| 
									
										
										
										
											2001-01-17 15:24:28 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-08-24 00:41:19 +00:00
										 |  |  | */ | 
					
						
							| 
									
										
										
										
											2001-01-22 19:28:09 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-08-24 00:41:19 +00:00
										 |  |  | /* Map rich comparison operators to their swapped version, e.g. LT <--> GT */ | 
					
						
							|  |  |  | int _Py_SwappedOp[] = {Py_GT, Py_GE, Py_EQ, Py_NE, Py_LT, Py_LE}; | 
					
						
							| 
									
										
										
										
											2001-06-09 07:34:05 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-25 19:53:18 +02:00
										 |  |  | static const char * const opstrings[] = {"<", "<=", "==", "!=", ">", ">="}; | 
					
						
							| 
									
										
										
										
											1990-10-14 12:07:46 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-08-24 00:41:19 +00:00
										 |  |  | /* Perform a rich comparison, raising TypeError when the requested comparison
 | 
					
						
							|  |  |  |    operator is not supported. */ | 
					
						
							| 
									
										
										
										
											2001-01-21 16:25:18 +00:00
										 |  |  | static PyObject * | 
					
						
							| 
									
										
										
										
											2019-11-05 00:51:22 +01:00
										 |  |  | do_richcompare(PyThreadState *tstate, PyObject *v, PyObject *w, int op) | 
					
						
							| 
									
										
										
										
											2001-01-17 15:24:28 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     richcmpfunc f; | 
					
						
							|  |  |  |     PyObject *res; | 
					
						
							|  |  |  |     int checked_reverse_op = 0; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-06 16:53:17 -06:00
										 |  |  |     if (!Py_IS_TYPE(v, Py_TYPE(w)) && | 
					
						
							| 
									
										
										
										
											2020-02-07 03:04:21 +01:00
										 |  |  |         PyType_IsSubtype(Py_TYPE(w), Py_TYPE(v)) && | 
					
						
							|  |  |  |         (f = Py_TYPE(w)->tp_richcompare) != NULL) { | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |         checked_reverse_op = 1; | 
					
						
							|  |  |  |         res = (*f)(w, v, _Py_SwappedOp[op]); | 
					
						
							|  |  |  |         if (res != Py_NotImplemented) | 
					
						
							|  |  |  |             return res; | 
					
						
							|  |  |  |         Py_DECREF(res); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-02-07 03:04:21 +01:00
										 |  |  |     if ((f = Py_TYPE(v)->tp_richcompare) != NULL) { | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |         res = (*f)(v, w, op); | 
					
						
							|  |  |  |         if (res != Py_NotImplemented) | 
					
						
							|  |  |  |             return res; | 
					
						
							|  |  |  |         Py_DECREF(res); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-02-07 03:04:21 +01:00
										 |  |  |     if (!checked_reverse_op && (f = Py_TYPE(w)->tp_richcompare) != NULL) { | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |         res = (*f)(w, v, _Py_SwappedOp[op]); | 
					
						
							|  |  |  |         if (res != Py_NotImplemented) | 
					
						
							|  |  |  |             return res; | 
					
						
							|  |  |  |         Py_DECREF(res); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     /* If neither object implements it, provide a sensible default
 | 
					
						
							|  |  |  |        for == and !=, but raise an exception for ordering. */ | 
					
						
							|  |  |  |     switch (op) { | 
					
						
							|  |  |  |     case Py_EQ: | 
					
						
							|  |  |  |         res = (v == w) ? Py_True : Py_False; | 
					
						
							|  |  |  |         break; | 
					
						
							|  |  |  |     case Py_NE: | 
					
						
							|  |  |  |         res = (v != w) ? Py_True : Py_False; | 
					
						
							|  |  |  |         break; | 
					
						
							|  |  |  |     default: | 
					
						
							| 
									
										
										
										
											2019-11-05 00:51:22 +01:00
										 |  |  |         _PyErr_Format(tstate, PyExc_TypeError, | 
					
						
							|  |  |  |                       "'%s' not supported between instances of '%.100s' and '%.100s'", | 
					
						
							|  |  |  |                       opstrings[op], | 
					
						
							| 
									
										
										
										
											2020-02-07 03:04:21 +01:00
										 |  |  |                       Py_TYPE(v)->tp_name, | 
					
						
							|  |  |  |                       Py_TYPE(w)->tp_name); | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |         return NULL; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2022-11-10 23:58:07 +01:00
										 |  |  |     return Py_NewRef(res); | 
					
						
							| 
									
										
										
										
											2001-01-17 15:24:28 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-08-24 00:41:19 +00:00
										 |  |  | /* Perform a rich comparison with object result.  This wraps do_richcompare()
 | 
					
						
							|  |  |  |    with a check for NULL arguments and a recursion check. */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-01-17 15:24:28 +00:00
										 |  |  | PyObject * | 
					
						
							|  |  |  | PyObject_RichCompare(PyObject *v, PyObject *w, int op) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2019-11-05 00:51:22 +01:00
										 |  |  |     PyThreadState *tstate = _PyThreadState_GET(); | 
					
						
							| 
									
										
										
										
											2001-01-17 15:24:28 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     assert(Py_LT <= op && op <= Py_GE); | 
					
						
							|  |  |  |     if (v == NULL || w == NULL) { | 
					
						
							| 
									
										
										
										
											2019-11-05 00:51:22 +01:00
										 |  |  |         if (!_PyErr_Occurred(tstate)) { | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |             PyErr_BadInternalCall(); | 
					
						
							| 
									
										
										
										
											2019-11-05 00:51:22 +01:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |         return NULL; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2022-05-04 13:30:23 +02:00
										 |  |  |     if (_Py_EnterRecursiveCallTstate(tstate, " in comparison")) { | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |         return NULL; | 
					
						
							| 
									
										
										
										
											2019-11-05 00:51:22 +01:00
										 |  |  |     } | 
					
						
							|  |  |  |     PyObject *res = do_richcompare(tstate, v, w, op); | 
					
						
							| 
									
										
										
										
											2022-05-04 13:30:23 +02:00
										 |  |  |     _Py_LeaveRecursiveCallTstate(tstate); | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     return res; | 
					
						
							| 
									
										
										
										
											2001-01-17 15:24:28 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-08-24 00:41:19 +00:00
										 |  |  | /* Perform a rich comparison with integer result.  This wraps
 | 
					
						
							|  |  |  |    PyObject_RichCompare(), returning -1 for error, 0 for false, 1 for true. */ | 
					
						
							| 
									
										
										
										
											2001-01-17 15:24:28 +00:00
										 |  |  | int | 
					
						
							|  |  |  | PyObject_RichCompareBool(PyObject *v, PyObject *w, int op) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     PyObject *res; | 
					
						
							|  |  |  |     int ok; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /* Quick result when objects are the same.
 | 
					
						
							|  |  |  |        Guarantees that identity implies equality. */ | 
					
						
							|  |  |  |     if (v == w) { | 
					
						
							|  |  |  |         if (op == Py_EQ) | 
					
						
							|  |  |  |             return 1; | 
					
						
							|  |  |  |         else if (op == Py_NE) | 
					
						
							|  |  |  |             return 0; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     res = PyObject_RichCompare(v, w, op); | 
					
						
							|  |  |  |     if (res == NULL) | 
					
						
							|  |  |  |         return -1; | 
					
						
							| 
									
										
										
										
											2025-06-27 11:01:51 +02:00
										 |  |  |     if (PyBool_Check(res)) { | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |         ok = (res == Py_True); | 
					
						
							| 
									
										
										
										
											2025-06-27 11:01:51 +02:00
										 |  |  |         assert(_Py_IsImmortal(res)); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     else { | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |         ok = PyObject_IsTrue(res); | 
					
						
							| 
									
										
										
										
											2025-06-27 11:01:51 +02:00
										 |  |  |         Py_DECREF(res); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     return ok; | 
					
						
							| 
									
										
										
										
											2001-01-17 15:24:28 +00:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2000-06-29 19:17:04 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-10-17 20:54:53 +00:00
										 |  |  | Py_hash_t | 
					
						
							| 
									
										
										
										
											2008-07-15 15:46:38 +00:00
										 |  |  | PyObject_HashNotImplemented(PyObject *v) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     PyErr_Format(PyExc_TypeError, "unhashable type: '%.200s'", | 
					
						
							|  |  |  |                  Py_TYPE(v)->tp_name); | 
					
						
							|  |  |  |     return -1; | 
					
						
							| 
									
										
										
										
											2008-07-15 15:46:38 +00:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2000-06-29 19:17:04 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-10-17 20:54:53 +00:00
										 |  |  | Py_hash_t | 
					
						
							| 
									
										
										
										
											2000-07-09 15:48:49 +00:00
										 |  |  | PyObject_Hash(PyObject *v) | 
					
						
							| 
									
										
										
										
											1993-03-29 10:43:31 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     PyTypeObject *tp = Py_TYPE(v); | 
					
						
							|  |  |  |     if (tp->tp_hash != NULL) | 
					
						
							|  |  |  |         return (*tp->tp_hash)(v); | 
					
						
							|  |  |  |     /* To keep to the general practice that inheriting
 | 
					
						
							|  |  |  |      * solely from object in C code should work without | 
					
						
							|  |  |  |      * an explicit call to PyType_Ready, we implicitly call | 
					
						
							|  |  |  |      * PyType_Ready here and then check the tp_hash slot again | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2023-04-27 16:19:43 -06:00
										 |  |  |     if (!_PyType_IsReady(tp)) { | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |         if (PyType_Ready(tp) < 0) | 
					
						
							|  |  |  |             return -1; | 
					
						
							|  |  |  |         if (tp->tp_hash != NULL) | 
					
						
							|  |  |  |             return (*tp->tp_hash)(v); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     /* Otherwise, the object can't be hashed */ | 
					
						
							|  |  |  |     return PyObject_HashNotImplemented(v); | 
					
						
							| 
									
										
										
										
											1993-03-29 10:43:31 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1997-05-02 03:12:38 +00:00
										 |  |  | PyObject * | 
					
						
							| 
									
										
										
										
											2005-12-10 18:50:16 +00:00
										 |  |  | PyObject_GetAttrString(PyObject *v, const char *name) | 
					
						
							| 
									
										
										
										
											1990-12-20 15:06:42 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     PyObject *w, *res; | 
					
						
							| 
									
										
										
										
											1996-08-09 20:52:03 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     if (Py_TYPE(v)->tp_getattr != NULL) | 
					
						
							|  |  |  |         return (*Py_TYPE(v)->tp_getattr)(v, (char*)name); | 
					
						
							| 
									
										
										
										
											2017-02-21 23:57:25 +09:00
										 |  |  |     w = PyUnicode_FromString(name); | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     if (w == NULL) | 
					
						
							|  |  |  |         return NULL; | 
					
						
							|  |  |  |     res = PyObject_GetAttr(v, w); | 
					
						
							| 
									
										
										
										
											2012-03-22 02:09:08 +01:00
										 |  |  |     Py_DECREF(w); | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     return res; | 
					
						
							| 
									
										
										
										
											1990-12-20 15:06:42 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1993-07-11 19:55:34 +00:00
										 |  |  | int | 
					
						
							| 
									
										
										
										
											2023-09-17 14:23:31 +03:00
										 |  |  | PyObject_HasAttrStringWithError(PyObject *obj, const char *name) | 
					
						
							| 
									
										
										
										
											1993-07-11 19:55:34 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2023-09-17 14:23:31 +03:00
										 |  |  |     PyObject *res; | 
					
						
							|  |  |  |     int rc = PyObject_GetOptionalAttrString(obj, name, &res); | 
					
						
							|  |  |  |     Py_XDECREF(res); | 
					
						
							|  |  |  |     return rc; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-03 00:20:00 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-09-17 14:23:31 +03:00
										 |  |  | int | 
					
						
							|  |  |  | PyObject_HasAttrString(PyObject *obj, const char *name) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     int rc = PyObject_HasAttrStringWithError(obj, name); | 
					
						
							|  |  |  |     if (rc < 0) { | 
					
						
							| 
									
										
										
										
											2023-11-07 15:58:04 +02:00
										 |  |  |         PyErr_FormatUnraisable( | 
					
						
							|  |  |  |             "Exception ignored in PyObject_HasAttrString(); consider using " | 
					
						
							|  |  |  |             "PyObject_HasAttrStringWithError(), " | 
					
						
							|  |  |  |             "PyObject_GetOptionalAttrString() or PyObject_GetAttrString()"); | 
					
						
							| 
									
										
										
										
											2023-05-03 00:20:00 -07:00
										 |  |  |         return 0; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2023-09-17 14:23:31 +03:00
										 |  |  |     return rc; | 
					
						
							| 
									
										
										
										
											1993-07-11 19:55:34 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1990-12-20 15:06:42 +00:00
										 |  |  | int | 
					
						
							| 
									
										
										
										
											2005-12-10 18:50:16 +00:00
										 |  |  | PyObject_SetAttrString(PyObject *v, const char *name, PyObject *w) | 
					
						
							| 
									
										
										
										
											1990-12-20 15:06:42 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2025-07-03 14:51:44 +02:00
										 |  |  |     PyThreadState *tstate = _PyThreadState_GET(); | 
					
						
							|  |  |  |     if (w == NULL && _PyErr_Occurred(tstate)) { | 
					
						
							|  |  |  |         PyObject *exc = _PyErr_GetRaisedException(tstate); | 
					
						
							|  |  |  |         _PyErr_SetString(tstate, PyExc_SystemError, | 
					
						
							|  |  |  |             "PyObject_SetAttrString() must not be called with NULL value " | 
					
						
							|  |  |  |             "and an exception set"); | 
					
						
							|  |  |  |         _PyErr_ChainExceptions1Tstate(tstate, exc); | 
					
						
							|  |  |  |         return -1; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											1996-08-09 20:52:03 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-07-03 14:51:44 +02:00
										 |  |  |     if (Py_TYPE(v)->tp_setattr != NULL) { | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |         return (*Py_TYPE(v)->tp_setattr)(v, (char*)name, w); | 
					
						
							| 
									
										
										
										
											2025-07-03 14:51:44 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     PyObject *s = PyUnicode_InternFromString(name); | 
					
						
							|  |  |  |     if (s == NULL) { | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |         return -1; | 
					
						
							| 
									
										
										
										
											2025-07-03 14:51:44 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     int res = PyObject_SetAttr(v, s, w); | 
					
						
							|  |  |  |     Py_DECREF(s); | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     return res; | 
					
						
							| 
									
										
										
										
											1993-05-12 08:24:20 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-11 11:38:22 +02:00
										 |  |  | int | 
					
						
							|  |  |  | PyObject_DelAttrString(PyObject *v, const char *name) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     return PyObject_SetAttrString(v, name, NULL); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-12-15 15:34:02 -05:00
										 |  |  | int | 
					
						
							|  |  |  | _PyObject_IsAbstract(PyObject *obj) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     int res; | 
					
						
							|  |  |  |     PyObject* isabstract; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (obj == NULL) | 
					
						
							|  |  |  |         return 0; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-11 22:13:27 +03:00
										 |  |  |     res = PyObject_GetOptionalAttr(obj, &_Py_ID(__isabstractmethod__), &isabstract); | 
					
						
							| 
									
										
										
										
											2018-01-25 10:49:40 +02:00
										 |  |  |     if (res > 0) { | 
					
						
							|  |  |  |         res = PyObject_IsTrue(isabstract); | 
					
						
							|  |  |  |         Py_DECREF(isabstract); | 
					
						
							| 
									
										
										
										
											2011-12-15 15:34:02 -05:00
										 |  |  |     } | 
					
						
							|  |  |  |     return res; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-10-09 10:38:36 +02:00
										 |  |  | PyObject * | 
					
						
							|  |  |  | _PyObject_GetAttrId(PyObject *v, _Py_Identifier *name) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     PyObject *result; | 
					
						
							| 
									
										
										
										
											2011-11-07 13:00:05 +01:00
										 |  |  |     PyObject *oname = _PyUnicode_FromId(name); /* borrowed */ | 
					
						
							| 
									
										
										
										
											2011-10-09 10:38:36 +02:00
										 |  |  |     if (!oname) | 
					
						
							|  |  |  |         return NULL; | 
					
						
							|  |  |  |     result = PyObject_GetAttr(v, oname); | 
					
						
							|  |  |  |     return result; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-05-06 10:50:35 -07:00
										 |  |  | int | 
					
						
							|  |  |  | _PyObject_SetAttributeErrorContext(PyObject* v, PyObject* name) | 
					
						
							| 
									
										
										
										
											2021-04-14 02:36:07 +01:00
										 |  |  | { | 
					
						
							|  |  |  |     assert(PyErr_Occurred()); | 
					
						
							| 
									
										
										
										
											2022-03-07 12:23:11 +00:00
										 |  |  |     if (!PyErr_ExceptionMatches(PyExc_AttributeError)){ | 
					
						
							|  |  |  |         return 0; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     // Intercept AttributeError exceptions and augment them to offer suggestions later.
 | 
					
						
							| 
									
										
										
										
											2023-03-08 17:03:18 +00:00
										 |  |  |     PyObject *exc = PyErr_GetRaisedException(); | 
					
						
							|  |  |  |     if (!PyErr_GivenExceptionMatches(exc, PyExc_AttributeError)) { | 
					
						
							| 
									
										
										
										
											2022-03-07 12:23:11 +00:00
										 |  |  |         goto restore; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2023-03-08 17:03:18 +00:00
										 |  |  |     PyAttributeErrorObject* the_exc = (PyAttributeErrorObject*) exc; | 
					
						
							| 
									
										
										
										
											2022-03-07 12:23:11 +00:00
										 |  |  |     // Check if this exception was already augmented
 | 
					
						
							|  |  |  |     if (the_exc->name || the_exc->obj) { | 
					
						
							|  |  |  |         goto restore; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     // Augment the exception with the name and object
 | 
					
						
							| 
									
										
										
										
											2023-03-08 17:03:18 +00:00
										 |  |  |     if (PyObject_SetAttr(exc, &_Py_ID(name), name) || | 
					
						
							|  |  |  |         PyObject_SetAttr(exc, &_Py_ID(obj), v)) { | 
					
						
							| 
									
										
										
										
											2022-03-07 12:23:11 +00:00
										 |  |  |         return 1; | 
					
						
							| 
									
										
										
										
											2021-04-14 02:36:07 +01:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2022-03-07 12:23:11 +00:00
										 |  |  | restore: | 
					
						
							| 
									
										
										
										
											2023-03-08 17:03:18 +00:00
										 |  |  |     PyErr_SetRaisedException(exc); | 
					
						
							| 
									
										
										
										
											2021-04-14 02:36:07 +01:00
										 |  |  |     return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1997-05-20 18:34:44 +00:00
										 |  |  | PyObject * | 
					
						
							| 
									
										
										
										
											2000-07-09 15:48:49 +00:00
										 |  |  | PyObject_GetAttr(PyObject *v, PyObject *name) | 
					
						
							| 
									
										
										
										
											1997-05-20 18:34:44 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     PyTypeObject *tp = Py_TYPE(v); | 
					
						
							|  |  |  |     if (!PyUnicode_Check(name)) { | 
					
						
							|  |  |  |         PyErr_Format(PyExc_TypeError, | 
					
						
							|  |  |  |                      "attribute name must be string, not '%.200s'", | 
					
						
							| 
									
										
										
										
											2020-02-07 03:04:21 +01:00
										 |  |  |                      Py_TYPE(name)->tp_name); | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |         return NULL; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2021-04-14 02:36:07 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     PyObject* result = NULL; | 
					
						
							|  |  |  |     if (tp->tp_getattro != NULL) { | 
					
						
							|  |  |  |         result = (*tp->tp_getattro)(v, name); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     else if (tp->tp_getattr != NULL) { | 
					
						
							| 
									
										
										
										
											2017-01-22 23:07:07 +02:00
										 |  |  |         const char *name_str = PyUnicode_AsUTF8(name); | 
					
						
							| 
									
										
										
										
											2021-04-14 02:36:07 +01:00
										 |  |  |         if (name_str == NULL) { | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |             return NULL; | 
					
						
							| 
									
										
										
										
											2021-04-14 02:36:07 +01:00
										 |  |  |         } | 
					
						
							|  |  |  |         result = (*tp->tp_getattr)(v, (char *)name_str); | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2021-04-14 02:36:07 +01:00
										 |  |  |     else { | 
					
						
							|  |  |  |         PyErr_Format(PyExc_AttributeError, | 
					
						
							| 
									
										
										
										
											2023-04-24 08:23:08 -06:00
										 |  |  |                     "'%.100s' object has no attribute '%U'", | 
					
						
							| 
									
										
										
										
											2021-04-14 02:36:07 +01:00
										 |  |  |                     tp->tp_name, name); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (result == NULL) { | 
					
						
							| 
									
										
										
										
											2024-05-06 10:50:35 -07:00
										 |  |  |         _PyObject_SetAttributeErrorContext(v, name); | 
					
						
							| 
									
										
										
										
											2021-04-14 02:36:07 +01:00
										 |  |  |     } | 
					
						
							|  |  |  |     return result; | 
					
						
							| 
									
										
										
										
											1997-05-20 18:34:44 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-25 10:49:40 +02:00
										 |  |  | int | 
					
						
							| 
									
										
										
										
											2023-07-11 22:13:27 +03:00
										 |  |  | PyObject_GetOptionalAttr(PyObject *v, PyObject *name, PyObject **result) | 
					
						
							| 
									
										
										
										
											2018-01-16 20:52:41 +09:00
										 |  |  | { | 
					
						
							|  |  |  |     PyTypeObject *tp = Py_TYPE(v); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (!PyUnicode_Check(name)) { | 
					
						
							|  |  |  |         PyErr_Format(PyExc_TypeError, | 
					
						
							|  |  |  |                      "attribute name must be string, not '%.200s'", | 
					
						
							| 
									
										
										
										
											2020-02-07 03:04:21 +01:00
										 |  |  |                      Py_TYPE(name)->tp_name); | 
					
						
							| 
									
										
										
										
											2018-01-25 10:49:40 +02:00
										 |  |  |         *result = NULL; | 
					
						
							|  |  |  |         return -1; | 
					
						
							| 
									
										
										
										
											2018-01-16 20:52:41 +09:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (tp->tp_getattro == PyObject_GenericGetAttr) { | 
					
						
							| 
									
										
										
										
											2018-01-25 10:49:40 +02:00
										 |  |  |         *result = _PyObject_GenericGetAttrWithDict(v, name, NULL, 1); | 
					
						
							|  |  |  |         if (*result != NULL) { | 
					
						
							|  |  |  |             return 1; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         if (PyErr_Occurred()) { | 
					
						
							|  |  |  |             return -1; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         return 0; | 
					
						
							| 
									
										
										
										
											2018-01-16 20:52:41 +09:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2023-12-20 08:13:44 -06:00
										 |  |  |     if (tp->tp_getattro == _Py_type_getattro) { | 
					
						
							| 
									
										
										
										
											2024-08-21 20:49:23 +08:00
										 |  |  |         int suppress_missing_attribute_exception = 0; | 
					
						
							|  |  |  |         *result = _Py_type_getattro_impl((PyTypeObject*)v, name, &suppress_missing_attribute_exception); | 
					
						
							|  |  |  |         if (suppress_missing_attribute_exception) { | 
					
						
							| 
									
										
										
										
											2022-12-23 16:23:36 +01:00
										 |  |  |             // return 0 without having to clear the exception
 | 
					
						
							|  |  |  |             return 0; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2023-05-04 07:50:26 -07:00
										 |  |  |     else if (tp->tp_getattro == (getattrofunc)_Py_module_getattro) { | 
					
						
							|  |  |  |         // optimization: suppress attribute error from module getattro method
 | 
					
						
							|  |  |  |         *result = _Py_module_getattro_impl((PyModuleObject*)v, name, 1); | 
					
						
							|  |  |  |         if (*result != NULL) { | 
					
						
							|  |  |  |             return 1; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         if (PyErr_Occurred()) { | 
					
						
							|  |  |  |             return -1; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         return 0; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2022-12-23 16:23:36 +01:00
										 |  |  |     else if (tp->tp_getattro != NULL) { | 
					
						
							| 
									
										
										
										
											2018-01-25 10:49:40 +02:00
										 |  |  |         *result = (*tp->tp_getattro)(v, name); | 
					
						
							| 
									
										
										
										
											2018-01-16 20:52:41 +09:00
										 |  |  |     } | 
					
						
							|  |  |  |     else if (tp->tp_getattr != NULL) { | 
					
						
							|  |  |  |         const char *name_str = PyUnicode_AsUTF8(name); | 
					
						
							| 
									
										
										
										
											2018-01-25 10:49:40 +02:00
										 |  |  |         if (name_str == NULL) { | 
					
						
							|  |  |  |             *result = NULL; | 
					
						
							|  |  |  |             return -1; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         *result = (*tp->tp_getattr)(v, (char *)name_str); | 
					
						
							| 
									
										
										
										
											2018-01-16 20:52:41 +09:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-01-26 16:22:51 +09:00
										 |  |  |     else { | 
					
						
							|  |  |  |         *result = NULL; | 
					
						
							|  |  |  |         return 0; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-25 10:49:40 +02:00
										 |  |  |     if (*result != NULL) { | 
					
						
							|  |  |  |         return 1; | 
					
						
							| 
									
										
										
										
											2018-01-16 20:52:41 +09:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-01-25 10:49:40 +02:00
										 |  |  |     if (!PyErr_ExceptionMatches(PyExc_AttributeError)) { | 
					
						
							|  |  |  |         return -1; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     PyErr_Clear(); | 
					
						
							|  |  |  |     return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | int | 
					
						
							| 
									
										
										
										
											2023-07-11 22:13:27 +03:00
										 |  |  | PyObject_GetOptionalAttrString(PyObject *obj, const char *name, PyObject **result) | 
					
						
							| 
									
										
										
										
											2018-01-25 10:49:40 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2023-07-11 22:13:27 +03:00
										 |  |  |     if (Py_TYPE(obj)->tp_getattr == NULL) { | 
					
						
							|  |  |  |         PyObject *oname = PyUnicode_FromString(name); | 
					
						
							|  |  |  |         if (oname == NULL) { | 
					
						
							|  |  |  |             *result = NULL; | 
					
						
							|  |  |  |             return -1; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         int rc = PyObject_GetOptionalAttr(obj, oname, result); | 
					
						
							|  |  |  |         Py_DECREF(oname); | 
					
						
							|  |  |  |         return rc; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     *result = (*Py_TYPE(obj)->tp_getattr)(obj, (char*)name); | 
					
						
							|  |  |  |     if (*result != NULL) { | 
					
						
							|  |  |  |         return 1; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     if (!PyErr_ExceptionMatches(PyExc_AttributeError)) { | 
					
						
							| 
									
										
										
										
											2018-01-25 10:49:40 +02:00
										 |  |  |         return -1; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2023-07-11 22:13:27 +03:00
										 |  |  |     PyErr_Clear(); | 
					
						
							|  |  |  |     return 0; | 
					
						
							| 
									
										
										
										
											2018-01-16 20:52:41 +09:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1997-05-20 18:34:44 +00:00
										 |  |  | int | 
					
						
							| 
									
										
										
										
											2023-09-17 14:23:31 +03:00
										 |  |  | PyObject_HasAttrWithError(PyObject *obj, PyObject *name) | 
					
						
							| 
									
										
										
										
											1997-05-20 18:34:44 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2018-01-25 10:49:40 +02:00
										 |  |  |     PyObject *res; | 
					
						
							| 
									
										
										
										
											2023-09-17 14:23:31 +03:00
										 |  |  |     int rc = PyObject_GetOptionalAttr(obj, name, &res); | 
					
						
							|  |  |  |     Py_XDECREF(res); | 
					
						
							|  |  |  |     return rc; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | int | 
					
						
							|  |  |  | PyObject_HasAttr(PyObject *obj, PyObject *name) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     int rc = PyObject_HasAttrWithError(obj, name); | 
					
						
							|  |  |  |     if (rc < 0) { | 
					
						
							| 
									
										
										
										
											2023-11-07 15:58:04 +02:00
										 |  |  |         PyErr_FormatUnraisable( | 
					
						
							|  |  |  |             "Exception ignored in PyObject_HasAttr(); consider using " | 
					
						
							|  |  |  |             "PyObject_HasAttrWithError(), " | 
					
						
							|  |  |  |             "PyObject_GetOptionalAttr() or PyObject_GetAttr()"); | 
					
						
							| 
									
										
										
										
											2018-01-25 10:49:40 +02:00
										 |  |  |         return 0; | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2023-09-17 14:23:31 +03:00
										 |  |  |     return rc; | 
					
						
							| 
									
										
										
										
											1997-05-20 18:34:44 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | int | 
					
						
							| 
									
										
										
										
											2000-07-09 15:48:49 +00:00
										 |  |  | PyObject_SetAttr(PyObject *v, PyObject *name, PyObject *value) | 
					
						
							| 
									
										
										
										
											1997-05-20 18:34:44 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2025-07-03 14:51:44 +02:00
										 |  |  |     PyThreadState *tstate = _PyThreadState_GET(); | 
					
						
							|  |  |  |     if (value == NULL && _PyErr_Occurred(tstate)) { | 
					
						
							|  |  |  |         PyObject *exc = _PyErr_GetRaisedException(tstate); | 
					
						
							|  |  |  |         _PyErr_SetString(tstate, PyExc_SystemError, | 
					
						
							|  |  |  |             "PyObject_SetAttr() must not be called with NULL value " | 
					
						
							|  |  |  |             "and an exception set"); | 
					
						
							|  |  |  |         _PyErr_ChainExceptions1Tstate(tstate, exc); | 
					
						
							|  |  |  |         return -1; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     PyTypeObject *tp = Py_TYPE(v); | 
					
						
							|  |  |  |     int err; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (!PyUnicode_Check(name)) { | 
					
						
							|  |  |  |         PyErr_Format(PyExc_TypeError, | 
					
						
							|  |  |  |                      "attribute name must be string, not '%.200s'", | 
					
						
							| 
									
										
										
										
											2020-02-07 03:04:21 +01:00
										 |  |  |                      Py_TYPE(name)->tp_name); | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |         return -1; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     Py_INCREF(name); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-07-03 14:51:44 +02:00
										 |  |  |     _PyUnicode_InternMortal(tstate->interp, &name); | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     if (tp->tp_setattro != NULL) { | 
					
						
							|  |  |  |         err = (*tp->tp_setattro)(v, name, value); | 
					
						
							|  |  |  |         Py_DECREF(name); | 
					
						
							|  |  |  |         return err; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     if (tp->tp_setattr != NULL) { | 
					
						
							| 
									
										
										
										
											2017-01-22 23:07:07 +02:00
										 |  |  |         const char *name_str = PyUnicode_AsUTF8(name); | 
					
						
							| 
									
										
										
										
											2019-04-28 06:58:52 -06:00
										 |  |  |         if (name_str == NULL) { | 
					
						
							|  |  |  |             Py_DECREF(name); | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |             return -1; | 
					
						
							| 
									
										
										
										
											2019-04-28 06:58:52 -06:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2017-01-22 23:07:07 +02:00
										 |  |  |         err = (*tp->tp_setattr)(v, (char *)name_str, value); | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |         Py_DECREF(name); | 
					
						
							|  |  |  |         return err; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     Py_DECREF(name); | 
					
						
							| 
									
										
										
										
											2020-02-07 00:38:59 +01:00
										 |  |  |     _PyObject_ASSERT(name, Py_REFCNT(name) >= 1); | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     if (tp->tp_getattr == NULL && tp->tp_getattro == NULL) | 
					
						
							|  |  |  |         PyErr_Format(PyExc_TypeError, | 
					
						
							|  |  |  |                      "'%.100s' object has no attributes " | 
					
						
							|  |  |  |                      "(%s .%U)", | 
					
						
							|  |  |  |                      tp->tp_name, | 
					
						
							|  |  |  |                      value==NULL ? "del" : "assign to", | 
					
						
							|  |  |  |                      name); | 
					
						
							|  |  |  |     else | 
					
						
							|  |  |  |         PyErr_Format(PyExc_TypeError, | 
					
						
							|  |  |  |                      "'%.100s' object has only read-only attributes " | 
					
						
							|  |  |  |                      "(%s .%U)", | 
					
						
							|  |  |  |                      tp->tp_name, | 
					
						
							|  |  |  |                      value==NULL ? "del" : "assign to", | 
					
						
							|  |  |  |                      name); | 
					
						
							|  |  |  |     return -1; | 
					
						
							| 
									
										
										
										
											2001-08-02 04:15:00 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-11 11:38:22 +02:00
										 |  |  | int | 
					
						
							|  |  |  | PyObject_DelAttr(PyObject *v, PyObject *name) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     return PyObject_SetAttr(v, name, NULL); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-08-02 04:15:00 +00:00
										 |  |  | PyObject ** | 
					
						
							| 
									
										
										
										
											2022-08-01 14:34:54 +01:00
										 |  |  | _PyObject_ComputedDictPointer(PyObject *obj) | 
					
						
							| 
									
										
										
										
											2001-08-02 04:15:00 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     PyTypeObject *tp = Py_TYPE(obj); | 
					
						
							| 
									
										
										
										
											2022-08-01 14:34:54 +01:00
										 |  |  |     assert((tp->tp_flags & Py_TPFLAGS_MANAGED_DICT) == 0); | 
					
						
							| 
									
										
										
										
											2022-11-29 12:15:21 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     Py_ssize_t dictoffset = tp->tp_dictoffset; | 
					
						
							|  |  |  |     if (dictoffset == 0) { | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |         return NULL; | 
					
						
							| 
									
										
										
										
											2022-11-29 12:15:21 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     if (dictoffset < 0) { | 
					
						
							| 
									
										
										
										
											2022-08-15 12:29:27 +01:00
										 |  |  |         assert(dictoffset != -1); | 
					
						
							| 
									
										
										
										
											2022-11-29 12:15:21 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-07 11:18:33 +01:00
										 |  |  |         Py_ssize_t tsize = Py_SIZE(obj); | 
					
						
							|  |  |  |         if (tsize < 0) { | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |             tsize = -tsize; | 
					
						
							| 
									
										
										
										
											2020-02-07 11:18:33 +01:00
										 |  |  |         } | 
					
						
							|  |  |  |         size_t size = _PyObject_VAR_SIZE(tp, tsize); | 
					
						
							| 
									
										
										
										
											2022-11-29 12:15:21 +01:00
										 |  |  |         assert(size <= (size_t)PY_SSIZE_T_MAX); | 
					
						
							|  |  |  |         dictoffset += (Py_ssize_t)size; | 
					
						
							| 
									
										
										
										
											2002-03-01 22:24:49 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-26 17:16:37 +02:00
										 |  |  |         _PyObject_ASSERT(obj, dictoffset > 0); | 
					
						
							|  |  |  |         _PyObject_ASSERT(obj, dictoffset % SIZEOF_VOID_P == 0); | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     } | 
					
						
							|  |  |  |     return (PyObject **) ((char *)obj + dictoffset); | 
					
						
							| 
									
										
										
										
											2001-08-02 04:15:00 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-13 14:19:34 +01:00
										 |  |  | /* Helper to get a pointer to an object's __dict__ slot, if any.
 | 
					
						
							|  |  |  |  * Creates the dict from inline attributes if necessary. | 
					
						
							| 
									
										
										
										
											2022-08-09 14:26:37 +01:00
										 |  |  |  * Does not set an exception. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Note that the tp_dictoffset docs used to recommend this function, | 
					
						
							|  |  |  |  * so it should be treated as part of the public API. | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2021-10-13 14:19:34 +01:00
										 |  |  | PyObject ** | 
					
						
							|  |  |  | _PyObject_GetDictPtr(PyObject *obj) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2021-12-07 16:02:53 +00:00
										 |  |  |     if ((Py_TYPE(obj)->tp_flags & Py_TPFLAGS_MANAGED_DICT) == 0) { | 
					
						
							| 
									
										
										
										
											2022-08-01 14:34:54 +01:00
										 |  |  |         return _PyObject_ComputedDictPointer(obj); | 
					
						
							| 
									
										
										
										
											2021-10-13 14:19:34 +01:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2024-04-21 22:57:05 -07:00
										 |  |  |     PyDictObject *dict = _PyObject_GetManagedDict(obj); | 
					
						
							|  |  |  |     if (dict == NULL && Py_TYPE(obj)->tp_flags & Py_TPFLAGS_INLINE_VALUES) { | 
					
						
							|  |  |  |         dict = _PyObject_MaterializeManagedDict(obj); | 
					
						
							| 
									
										
										
										
											2022-08-01 14:34:54 +01:00
										 |  |  |         if (dict == NULL) { | 
					
						
							|  |  |  |             PyErr_Clear(); | 
					
						
							|  |  |  |             return NULL; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2021-10-13 14:19:34 +01:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2024-04-21 22:57:05 -07:00
										 |  |  |     return (PyObject **)&_PyObject_ManagedDictPointer(obj)->dict; | 
					
						
							| 
									
										
										
										
											2021-10-13 14:19:34 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2003-03-17 08:24:35 +00:00
										 |  |  | PyObject * | 
					
						
							| 
									
										
										
										
											2003-03-17 19:46:11 +00:00
										 |  |  | PyObject_SelfIter(PyObject *obj) | 
					
						
							| 
									
										
										
										
											2003-03-17 08:24:35 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2022-11-10 23:58:07 +01:00
										 |  |  |     return Py_NewRef(obj); | 
					
						
							| 
									
										
										
										
											2003-03-17 08:24:35 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-01-12 23:58:21 +00:00
										 |  |  | /* Helper used when the __next__ method is removed from a type:
 | 
					
						
							|  |  |  |    tp_iternext is never NULL and can be safely called without checking | 
					
						
							|  |  |  |    on every iteration. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | PyObject * | 
					
						
							|  |  |  | _PyObject_NextNotImplemented(PyObject *self) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     PyErr_Format(PyExc_TypeError, | 
					
						
							|  |  |  |                  "'%.200s' object is not iterable", | 
					
						
							|  |  |  |                  Py_TYPE(self)->tp_name); | 
					
						
							|  |  |  |     return NULL; | 
					
						
							| 
									
										
										
										
											2009-01-12 23:58:21 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-12-13 19:03:51 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | /* Specialized version of _PyObject_GenericGetAttrWithDict
 | 
					
						
							| 
									
										
										
										
											2025-01-23 04:26:25 -05:00
										 |  |  |    specifically for the LOAD_METHOD opcode. | 
					
						
							| 
									
										
										
										
											2016-12-13 19:03:51 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |    Return 1 if a method is found, 0 if it's a regular attribute | 
					
						
							|  |  |  |    from __dict__ or something returned by using a descriptor | 
					
						
							|  |  |  |    protocol. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    `method` will point to the resolved attribute or NULL.  In the | 
					
						
							|  |  |  |    latter case, an error will be set. | 
					
						
							|  |  |  | */ | 
					
						
							|  |  |  | int | 
					
						
							|  |  |  | _PyObject_GetMethod(PyObject *obj, PyObject *name, PyObject **method) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     int meth_found = 0; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     assert(*method == NULL); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-07-01 03:11:59 +02:00
										 |  |  |     PyTypeObject *tp = Py_TYPE(obj); | 
					
						
							|  |  |  |     if (!_PyType_IsReady(tp)) { | 
					
						
							|  |  |  |         if (PyType_Ready(tp) < 0) { | 
					
						
							|  |  |  |             return 0; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2016-12-13 19:03:51 -05:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-13 14:19:34 +01:00
										 |  |  |     if (tp->tp_getattro != PyObject_GenericGetAttr || !PyUnicode_CheckExact(name)) { | 
					
						
							| 
									
										
										
										
											2021-07-01 03:11:59 +02:00
										 |  |  |         *method = PyObject_GetAttr(obj, name); | 
					
						
							| 
									
										
										
										
											2016-12-13 19:03:51 -05:00
										 |  |  |         return 0; | 
					
						
							| 
									
										
										
										
											2021-07-01 03:11:59 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2016-12-13 19:03:51 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-05-06 10:50:35 -07:00
										 |  |  |     PyObject *descr = _PyType_LookupRef(tp, name); | 
					
						
							| 
									
										
										
										
											2021-07-01 03:11:59 +02:00
										 |  |  |     descrgetfunc f = NULL; | 
					
						
							| 
									
										
										
										
											2016-12-13 19:03:51 -05:00
										 |  |  |     if (descr != NULL) { | 
					
						
							| 
									
										
										
										
											2020-04-08 01:42:27 +02:00
										 |  |  |         if (_PyType_HasFeature(Py_TYPE(descr), Py_TPFLAGS_METHOD_DESCRIPTOR)) { | 
					
						
							| 
									
										
										
										
											2016-12-13 19:03:51 -05:00
										 |  |  |             meth_found = 1; | 
					
						
							| 
									
										
										
										
											2024-05-06 10:50:35 -07:00
										 |  |  |         } | 
					
						
							|  |  |  |         else { | 
					
						
							| 
									
										
										
										
											2020-02-07 03:04:21 +01:00
										 |  |  |             f = Py_TYPE(descr)->tp_descr_get; | 
					
						
							| 
									
										
										
										
											2016-12-13 19:03:51 -05:00
										 |  |  |             if (f != NULL && PyDescr_IsData(descr)) { | 
					
						
							| 
									
										
										
										
											2020-02-07 03:04:21 +01:00
										 |  |  |                 *method = f(descr, obj, (PyObject *)Py_TYPE(obj)); | 
					
						
							| 
									
										
										
										
											2016-12-13 19:03:51 -05:00
										 |  |  |                 Py_DECREF(descr); | 
					
						
							|  |  |  |                 return 0; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2024-04-21 22:57:05 -07:00
										 |  |  |     PyObject *dict, *attr; | 
					
						
							|  |  |  |     if ((tp->tp_flags & Py_TPFLAGS_INLINE_VALUES) && | 
					
						
							|  |  |  |          _PyObject_TryGetInstanceAttribute(obj, name, &attr)) { | 
					
						
							| 
									
										
										
										
											2024-04-02 11:59:21 +01:00
										 |  |  |         if (attr != NULL) { | 
					
						
							|  |  |  |             *method = attr; | 
					
						
							|  |  |  |             Py_XDECREF(descr); | 
					
						
							|  |  |  |             return 0; | 
					
						
							| 
									
										
										
										
											2022-08-01 14:34:54 +01:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2024-04-02 11:59:21 +01:00
										 |  |  |         dict = NULL; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     else if ((tp->tp_flags & Py_TPFLAGS_MANAGED_DICT)) { | 
					
						
							| 
									
										
										
										
											2024-04-21 22:57:05 -07:00
										 |  |  |         dict = (PyObject *)_PyObject_GetManagedDict(obj); | 
					
						
							| 
									
										
										
										
											2022-08-01 14:34:54 +01:00
										 |  |  |     } | 
					
						
							|  |  |  |     else { | 
					
						
							|  |  |  |         PyObject **dictptr = _PyObject_ComputedDictPointer(obj); | 
					
						
							|  |  |  |         if (dictptr != NULL) { | 
					
						
							| 
									
										
										
										
											2025-02-11 16:53:08 -05:00
										 |  |  |             dict = FT_ATOMIC_LOAD_PTR_ACQUIRE(*dictptr); | 
					
						
							| 
									
										
										
										
											2022-08-01 14:34:54 +01:00
										 |  |  |         } | 
					
						
							|  |  |  |         else { | 
					
						
							|  |  |  |             dict = NULL; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     if (dict != NULL) { | 
					
						
							|  |  |  |         Py_INCREF(dict); | 
					
						
							| 
									
										
										
										
											2023-11-14 11:25:39 +02:00
										 |  |  |         if (PyDict_GetItemRef(dict, name, method) != 0) { | 
					
						
							|  |  |  |             // found or error
 | 
					
						
							| 
									
										
										
										
											2021-10-13 14:19:34 +01:00
										 |  |  |             Py_DECREF(dict); | 
					
						
							| 
									
										
										
										
											2022-08-01 14:34:54 +01:00
										 |  |  |             Py_XDECREF(descr); | 
					
						
							|  |  |  |             return 0; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2023-11-14 11:25:39 +02:00
										 |  |  |         // not found
 | 
					
						
							| 
									
										
										
										
											2022-08-01 14:34:54 +01:00
										 |  |  |         Py_DECREF(dict); | 
					
						
							| 
									
										
										
										
											2016-12-13 19:03:51 -05:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (meth_found) { | 
					
						
							|  |  |  |         *method = descr; | 
					
						
							|  |  |  |         return 1; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (f != NULL) { | 
					
						
							|  |  |  |         *method = f(descr, obj, (PyObject *)Py_TYPE(obj)); | 
					
						
							|  |  |  |         Py_DECREF(descr); | 
					
						
							|  |  |  |         return 0; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (descr != NULL) { | 
					
						
							|  |  |  |         *method = descr; | 
					
						
							|  |  |  |         return 0; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     PyErr_Format(PyExc_AttributeError, | 
					
						
							| 
									
										
										
										
											2023-04-24 08:23:08 -06:00
										 |  |  |                  "'%.100s' object has no attribute '%U'", | 
					
						
							| 
									
										
										
										
											2016-12-13 19:03:51 -05:00
										 |  |  |                  tp->tp_name, name); | 
					
						
							| 
									
										
										
										
											2021-04-14 02:36:07 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-05-06 10:50:35 -07:00
										 |  |  |     _PyObject_SetAttributeErrorContext(obj, name); | 
					
						
							| 
									
										
										
										
											2016-12-13 19:03:51 -05:00
										 |  |  |     return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-05-27 22:28:27 +05:30
										 |  |  | int | 
					
						
							|  |  |  | _PyObject_GetMethodStackRef(PyThreadState *ts, PyObject *obj, | 
					
						
							|  |  |  |                             PyObject *name, _PyStackRef *method) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     int meth_found = 0; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     assert(PyStackRef_IsNull(*method)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     PyTypeObject *tp = Py_TYPE(obj); | 
					
						
							|  |  |  |     if (!_PyType_IsReady(tp)) { | 
					
						
							|  |  |  |         if (PyType_Ready(tp) < 0) { | 
					
						
							|  |  |  |             return 0; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (tp->tp_getattro != PyObject_GenericGetAttr || !PyUnicode_CheckExact(name)) { | 
					
						
							|  |  |  |         PyObject *res = PyObject_GetAttr(obj, name); | 
					
						
							|  |  |  |         if (res != NULL) { | 
					
						
							|  |  |  |             *method = PyStackRef_FromPyObjectSteal(res); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         return 0; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     _PyType_LookupStackRefAndVersion(tp, name, method); | 
					
						
							|  |  |  |     PyObject *descr = PyStackRef_AsPyObjectBorrow(*method); | 
					
						
							|  |  |  |     descrgetfunc f = NULL; | 
					
						
							|  |  |  |     if (descr != NULL) { | 
					
						
							|  |  |  |         if (_PyType_HasFeature(Py_TYPE(descr), Py_TPFLAGS_METHOD_DESCRIPTOR)) { | 
					
						
							|  |  |  |             meth_found = 1; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         else { | 
					
						
							|  |  |  |             f = Py_TYPE(descr)->tp_descr_get; | 
					
						
							|  |  |  |             if (f != NULL && PyDescr_IsData(descr)) { | 
					
						
							|  |  |  |                 PyObject *value = f(descr, obj, (PyObject *)Py_TYPE(obj)); | 
					
						
							|  |  |  |                 PyStackRef_CLEAR(*method); | 
					
						
							|  |  |  |                 if (value != NULL) { | 
					
						
							|  |  |  |                     *method = PyStackRef_FromPyObjectSteal(value); | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |                 return 0; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     PyObject *dict, *attr; | 
					
						
							|  |  |  |     if ((tp->tp_flags & Py_TPFLAGS_INLINE_VALUES) && | 
					
						
							|  |  |  |          _PyObject_TryGetInstanceAttribute(obj, name, &attr)) { | 
					
						
							|  |  |  |         if (attr != NULL) { | 
					
						
							|  |  |  |            PyStackRef_CLEAR(*method); | 
					
						
							|  |  |  |            *method = PyStackRef_FromPyObjectSteal(attr); | 
					
						
							|  |  |  |            return 0; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         dict = NULL; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     else if ((tp->tp_flags & Py_TPFLAGS_MANAGED_DICT)) { | 
					
						
							|  |  |  |         dict = (PyObject *)_PyObject_GetManagedDict(obj); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     else { | 
					
						
							|  |  |  |         PyObject **dictptr = _PyObject_ComputedDictPointer(obj); | 
					
						
							|  |  |  |         if (dictptr != NULL) { | 
					
						
							|  |  |  |             dict = FT_ATOMIC_LOAD_PTR_ACQUIRE(*dictptr); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         else { | 
					
						
							|  |  |  |             dict = NULL; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     if (dict != NULL) { | 
					
						
							| 
									
										
										
										
											2025-07-28 22:04:57 +05:30
										 |  |  |         assert(PyUnicode_CheckExact(name)); | 
					
						
							|  |  |  |         int found = _PyDict_GetMethodStackRef((PyDictObject *)dict, name, method); | 
					
						
							|  |  |  |         if (found < 0) { | 
					
						
							|  |  |  |             assert(PyStackRef_IsNull(*method)); | 
					
						
							|  |  |  |             return -1; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         else if (found) { | 
					
						
							| 
									
										
										
										
											2025-05-27 22:28:27 +05:30
										 |  |  |             return 0; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (meth_found) { | 
					
						
							|  |  |  |         assert(!PyStackRef_IsNull(*method)); | 
					
						
							|  |  |  |         return 1; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (f != NULL) { | 
					
						
							|  |  |  |         PyObject *value = f(descr, obj, (PyObject *)Py_TYPE(obj)); | 
					
						
							|  |  |  |         PyStackRef_CLEAR(*method); | 
					
						
							|  |  |  |         if (value) { | 
					
						
							|  |  |  |             *method = PyStackRef_FromPyObjectSteal(value); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         return 0; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (descr != NULL) { | 
					
						
							|  |  |  |         assert(!PyStackRef_IsNull(*method)); | 
					
						
							|  |  |  |         return 0; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     PyErr_Format(PyExc_AttributeError, | 
					
						
							|  |  |  |                  "'%.100s' object has no attribute '%U'", | 
					
						
							|  |  |  |                  tp->tp_name, name); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     _PyObject_SetAttributeErrorContext(obj, name); | 
					
						
							|  |  |  |     assert(PyStackRef_IsNull(*method)); | 
					
						
							|  |  |  |     return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-12-13 19:03:51 -05:00
										 |  |  | /* Generic GetAttr functions - put these in your tp_[gs]etattro slot. */ | 
					
						
							| 
									
										
										
										
											2004-09-14 17:09:47 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-08-02 04:15:00 +00:00
										 |  |  | PyObject * | 
					
						
							| 
									
										
										
										
											2018-01-16 20:52:41 +09:00
										 |  |  | _PyObject_GenericGetAttrWithDict(PyObject *obj, PyObject *name, | 
					
						
							|  |  |  |                                  PyObject *dict, int suppress) | 
					
						
							| 
									
										
										
										
											2001-08-02 04:15:00 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2016-12-13 19:03:51 -05:00
										 |  |  |     /* Make sure the logic of _PyObject_GetMethod is in sync with
 | 
					
						
							|  |  |  |        this method. | 
					
						
							| 
									
										
										
										
											2018-01-16 20:52:41 +09:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-17 13:09:21 -07:00
										 |  |  |        When suppress=1, this function suppresses AttributeError. | 
					
						
							| 
									
										
										
										
											2016-12-13 19:03:51 -05:00
										 |  |  |     */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     PyTypeObject *tp = Py_TYPE(obj); | 
					
						
							|  |  |  |     PyObject *descr = NULL; | 
					
						
							|  |  |  |     PyObject *res = NULL; | 
					
						
							|  |  |  |     descrgetfunc f; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (!PyUnicode_Check(name)){ | 
					
						
							|  |  |  |         PyErr_Format(PyExc_TypeError, | 
					
						
							|  |  |  |                      "attribute name must be string, not '%.200s'", | 
					
						
							| 
									
										
										
										
											2020-02-07 03:04:21 +01:00
										 |  |  |                      Py_TYPE(name)->tp_name); | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |         return NULL; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-27 16:19:43 -06:00
										 |  |  |     if (!_PyType_IsReady(tp)) { | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |         if (PyType_Ready(tp) < 0) | 
					
						
							| 
									
										
										
										
											2025-03-26 14:38:47 -04:00
										 |  |  |             return NULL; | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2001-08-02 04:15:00 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-03-26 14:38:47 -04:00
										 |  |  |     Py_INCREF(name); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     PyThreadState *tstate = _PyThreadState_GET(); | 
					
						
							|  |  |  |     _PyCStackRef cref; | 
					
						
							|  |  |  |     _PyThreadState_PushCStackRef(tstate, &cref); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     _PyType_LookupStackRefAndVersion(tp, name, &cref.ref); | 
					
						
							|  |  |  |     descr = PyStackRef_AsPyObjectBorrow(cref.ref); | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     f = NULL; | 
					
						
							|  |  |  |     if (descr != NULL) { | 
					
						
							| 
									
										
										
										
											2020-02-07 03:04:21 +01:00
										 |  |  |         f = Py_TYPE(descr)->tp_descr_get; | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |         if (f != NULL && PyDescr_IsData(descr)) { | 
					
						
							| 
									
										
										
										
											2020-02-07 03:04:21 +01:00
										 |  |  |             res = f(descr, obj, (PyObject *)Py_TYPE(obj)); | 
					
						
							| 
									
										
										
										
											2018-01-16 20:52:41 +09:00
										 |  |  |             if (res == NULL && suppress && | 
					
						
							|  |  |  |                     PyErr_ExceptionMatches(PyExc_AttributeError)) { | 
					
						
							|  |  |  |                 PyErr_Clear(); | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |             goto done; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2010-08-28 18:17:03 +00:00
										 |  |  |     if (dict == NULL) { | 
					
						
							| 
									
										
										
										
											2024-04-21 22:57:05 -07:00
										 |  |  |         if ((tp->tp_flags & Py_TPFLAGS_INLINE_VALUES)) { | 
					
						
							|  |  |  |             if (PyUnicode_CheckExact(name) && | 
					
						
							|  |  |  |                 _PyObject_TryGetInstanceAttribute(obj, name, &res)) { | 
					
						
							| 
									
										
										
										
											2024-04-02 11:59:21 +01:00
										 |  |  |                 if (res != NULL) { | 
					
						
							|  |  |  |                     goto done; | 
					
						
							| 
									
										
										
										
											2020-02-07 11:18:33 +01:00
										 |  |  |                 } | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |             } | 
					
						
							| 
									
										
										
										
											2021-10-13 14:19:34 +01:00
										 |  |  |             else { | 
					
						
							| 
									
										
										
										
											2024-04-21 22:57:05 -07:00
										 |  |  |                 dict = (PyObject *)_PyObject_MaterializeManagedDict(obj); | 
					
						
							| 
									
										
										
										
											2024-04-02 11:59:21 +01:00
										 |  |  |                 if (dict == NULL) { | 
					
						
							|  |  |  |                     res = NULL; | 
					
						
							|  |  |  |                     goto done; | 
					
						
							|  |  |  |                 } | 
					
						
							| 
									
										
										
										
											2021-10-13 14:19:34 +01:00
										 |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2024-04-02 11:59:21 +01:00
										 |  |  |         else if ((tp->tp_flags & Py_TPFLAGS_MANAGED_DICT)) { | 
					
						
							| 
									
										
										
										
											2024-04-21 22:57:05 -07:00
										 |  |  |             dict = (PyObject *)_PyObject_GetManagedDict(obj); | 
					
						
							| 
									
										
										
										
											2024-04-02 11:59:21 +01:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2021-10-13 14:19:34 +01:00
										 |  |  |         else { | 
					
						
							| 
									
										
										
										
											2022-08-01 14:34:54 +01:00
										 |  |  |             PyObject **dictptr = _PyObject_ComputedDictPointer(obj); | 
					
						
							| 
									
										
										
										
											2021-12-07 16:02:53 +00:00
										 |  |  |             if (dictptr) { | 
					
						
							| 
									
										
										
										
											2024-12-30 23:38:49 +03:00
										 |  |  | #ifdef Py_GIL_DISABLED
 | 
					
						
							|  |  |  |                 dict = _Py_atomic_load_ptr_acquire(dictptr); | 
					
						
							|  |  |  | #else
 | 
					
						
							| 
									
										
										
										
											2021-10-13 14:19:34 +01:00
										 |  |  |                 dict = *dictptr; | 
					
						
							| 
									
										
										
										
											2024-12-30 23:38:49 +03:00
										 |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2021-10-13 14:19:34 +01:00
										 |  |  |             } | 
					
						
							| 
									
										
										
										
											2010-08-28 18:17:03 +00:00
										 |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     if (dict != NULL) { | 
					
						
							|  |  |  |         Py_INCREF(dict); | 
					
						
							| 
									
										
										
										
											2023-11-14 11:25:39 +02:00
										 |  |  |         int rc = PyDict_GetItemRef(dict, name, &res); | 
					
						
							|  |  |  |         Py_DECREF(dict); | 
					
						
							| 
									
										
										
										
											2010-08-28 18:17:03 +00:00
										 |  |  |         if (res != NULL) { | 
					
						
							|  |  |  |             goto done; | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2023-11-14 11:25:39 +02:00
										 |  |  |         else if (rc < 0) { | 
					
						
							|  |  |  |             if (suppress && PyErr_ExceptionMatches(PyExc_AttributeError)) { | 
					
						
							|  |  |  |                 PyErr_Clear(); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             else { | 
					
						
							|  |  |  |                 goto done; | 
					
						
							| 
									
										
										
										
											2019-02-25 17:59:46 +02:00
										 |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (f != NULL) { | 
					
						
							|  |  |  |         res = f(descr, obj, (PyObject *)Py_TYPE(obj)); | 
					
						
							| 
									
										
										
										
											2018-01-16 20:52:41 +09:00
										 |  |  |         if (res == NULL && suppress && | 
					
						
							|  |  |  |                 PyErr_ExceptionMatches(PyExc_AttributeError)) { | 
					
						
							|  |  |  |             PyErr_Clear(); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |         goto done; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (descr != NULL) { | 
					
						
							| 
									
										
										
										
											2025-03-26 14:38:47 -04:00
										 |  |  |         res = PyStackRef_AsPyObjectSteal(cref.ref); | 
					
						
							|  |  |  |         cref.ref = PyStackRef_NULL; | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |         goto done; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-16 20:52:41 +09:00
										 |  |  |     if (!suppress) { | 
					
						
							|  |  |  |         PyErr_Format(PyExc_AttributeError, | 
					
						
							| 
									
										
										
										
											2023-04-24 08:23:08 -06:00
										 |  |  |                      "'%.100s' object has no attribute '%U'", | 
					
						
							| 
									
										
										
										
											2018-01-16 20:52:41 +09:00
										 |  |  |                      tp->tp_name, name); | 
					
						
							| 
									
										
										
										
											2022-09-08 13:12:14 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-05-06 10:50:35 -07:00
										 |  |  |         _PyObject_SetAttributeErrorContext(obj, name); | 
					
						
							| 
									
										
										
										
											2018-01-16 20:52:41 +09:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2001-12-04 15:54:53 +00:00
										 |  |  |   done: | 
					
						
							| 
									
										
										
										
											2025-03-26 14:38:47 -04:00
										 |  |  |     _PyThreadState_PopCStackRef(tstate, &cref); | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     Py_DECREF(name); | 
					
						
							|  |  |  |     return res; | 
					
						
							| 
									
										
										
										
											2001-08-02 04:15:00 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-08-28 18:17:03 +00:00
										 |  |  | PyObject * | 
					
						
							|  |  |  | PyObject_GenericGetAttr(PyObject *obj, PyObject *name) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2018-01-16 20:52:41 +09:00
										 |  |  |     return _PyObject_GenericGetAttrWithDict(obj, name, NULL, 0); | 
					
						
							| 
									
										
										
										
											2010-08-28 18:17:03 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-08-02 04:15:00 +00:00
										 |  |  | int | 
					
						
							| 
									
										
										
										
											2010-08-28 18:17:03 +00:00
										 |  |  | _PyObject_GenericSetAttrWithDict(PyObject *obj, PyObject *name, | 
					
						
							|  |  |  |                                  PyObject *value, PyObject *dict) | 
					
						
							| 
									
										
										
										
											2001-08-02 04:15:00 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     PyTypeObject *tp = Py_TYPE(obj); | 
					
						
							|  |  |  |     PyObject *descr; | 
					
						
							|  |  |  |     descrsetfunc f; | 
					
						
							|  |  |  |     int res = -1; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-05-06 10:50:35 -07:00
										 |  |  |     assert(!PyType_IsSubtype(tp, &PyType_Type)); | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     if (!PyUnicode_Check(name)){ | 
					
						
							|  |  |  |         PyErr_Format(PyExc_TypeError, | 
					
						
							|  |  |  |                      "attribute name must be string, not '%.200s'", | 
					
						
							| 
									
										
										
										
											2020-02-07 03:04:21 +01:00
										 |  |  |                      Py_TYPE(name)->tp_name); | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |         return -1; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-27 16:19:43 -06:00
										 |  |  |     if (!_PyType_IsReady(tp) && PyType_Ready(tp) < 0) { | 
					
						
							| 
									
										
										
										
											2012-03-09 07:25:32 -08:00
										 |  |  |         return -1; | 
					
						
							| 
									
										
										
										
											2023-04-27 16:19:43 -06:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2012-03-09 07:25:32 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     Py_INCREF(name); | 
					
						
							| 
									
										
										
										
											2022-05-05 15:37:26 +02:00
										 |  |  |     Py_INCREF(tp); | 
					
						
							| 
									
										
										
										
											2025-03-26 14:38:47 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |     PyThreadState *tstate = _PyThreadState_GET(); | 
					
						
							|  |  |  |     _PyCStackRef cref; | 
					
						
							|  |  |  |     _PyThreadState_PushCStackRef(tstate, &cref); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     _PyType_LookupStackRefAndVersion(tp, name, &cref.ref); | 
					
						
							|  |  |  |     descr = PyStackRef_AsPyObjectBorrow(cref.ref); | 
					
						
							| 
									
										
										
										
											2012-03-09 00:44:13 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     if (descr != NULL) { | 
					
						
							| 
									
										
										
										
											2020-02-07 03:04:21 +01:00
										 |  |  |         f = Py_TYPE(descr)->tp_descr_set; | 
					
						
							| 
									
										
										
										
											2016-04-17 20:31:51 +03:00
										 |  |  |         if (f != NULL) { | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |             res = f(descr, obj, value); | 
					
						
							|  |  |  |             goto done; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-08-28 18:17:03 +00:00
										 |  |  |     if (dict == NULL) { | 
					
						
							| 
									
										
										
										
											2022-08-01 14:34:54 +01:00
										 |  |  |         PyObject **dictptr; | 
					
						
							| 
									
										
										
										
											2024-04-21 22:57:05 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |         if ((tp->tp_flags & Py_TPFLAGS_INLINE_VALUES)) { | 
					
						
							|  |  |  |             res = _PyObject_StoreInstanceAttribute(obj, name, value); | 
					
						
							| 
									
										
										
										
											2024-04-02 11:59:21 +01:00
										 |  |  |             goto error_check; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2024-04-21 22:57:05 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |         if ((tp->tp_flags & Py_TPFLAGS_MANAGED_DICT)) { | 
					
						
							| 
									
										
										
										
											2024-04-02 11:59:21 +01:00
										 |  |  |             PyManagedDictPointer *managed_dict = _PyObject_ManagedDictPointer(obj); | 
					
						
							|  |  |  |             dictptr = (PyObject **)&managed_dict->dict; | 
					
						
							| 
									
										
										
										
											2021-10-13 14:19:34 +01:00
										 |  |  |         } | 
					
						
							|  |  |  |         else { | 
					
						
							| 
									
										
										
										
											2022-08-01 14:34:54 +01:00
										 |  |  |             dictptr = _PyObject_ComputedDictPointer(obj); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         if (dictptr == NULL) { | 
					
						
							|  |  |  |             if (descr == NULL) { | 
					
						
							| 
									
										
										
										
											2023-07-22 01:24:26 +01:00
										 |  |  |                 if (tp->tp_setattro == PyObject_GenericSetAttr) { | 
					
						
							|  |  |  |                     PyErr_Format(PyExc_AttributeError, | 
					
						
							|  |  |  |                                 "'%.100s' object has no attribute '%U' and no " | 
					
						
							|  |  |  |                                 "__dict__ for setting new attributes", | 
					
						
							|  |  |  |                                 tp->tp_name, name); | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |                 else { | 
					
						
							|  |  |  |                     PyErr_Format(PyExc_AttributeError, | 
					
						
							|  |  |  |                                 "'%.100s' object has no attribute '%U'", | 
					
						
							|  |  |  |                                 tp->tp_name, name); | 
					
						
							|  |  |  |                 } | 
					
						
							| 
									
										
										
										
											2024-05-06 10:50:35 -07:00
										 |  |  |                 _PyObject_SetAttributeErrorContext(obj, name); | 
					
						
							| 
									
										
										
										
											2016-04-17 20:31:51 +03:00
										 |  |  |             } | 
					
						
							|  |  |  |             else { | 
					
						
							| 
									
										
										
										
											2022-08-01 14:34:54 +01:00
										 |  |  |                 PyErr_Format(PyExc_AttributeError, | 
					
						
							| 
									
										
										
										
											2023-04-24 08:23:08 -06:00
										 |  |  |                             "'%.100s' object attribute '%U' is read-only", | 
					
						
							| 
									
										
										
										
											2022-08-01 14:34:54 +01:00
										 |  |  |                             tp->tp_name, name); | 
					
						
							| 
									
										
										
										
											2016-04-17 20:31:51 +03:00
										 |  |  |             } | 
					
						
							| 
									
										
										
										
											2022-08-01 14:34:54 +01:00
										 |  |  |             goto done; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         else { | 
					
						
							| 
									
										
										
										
											2024-05-06 16:31:09 -07:00
										 |  |  |             res = _PyObjectDict_SetItem(tp, obj, dictptr, name, value); | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2016-04-17 20:31:51 +03:00
										 |  |  |     else { | 
					
						
							| 
									
										
										
										
											2010-08-28 18:17:03 +00:00
										 |  |  |         Py_INCREF(dict); | 
					
						
							|  |  |  |         if (value == NULL) | 
					
						
							|  |  |  |             res = PyDict_DelItem(dict, name); | 
					
						
							|  |  |  |         else | 
					
						
							|  |  |  |             res = PyDict_SetItem(dict, name, value); | 
					
						
							| 
									
										
										
										
											2012-03-09 07:25:32 -08:00
										 |  |  |         Py_DECREF(dict); | 
					
						
							| 
									
										
										
										
											2010-08-28 18:17:03 +00:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2022-08-01 14:34:54 +01:00
										 |  |  |   error_check: | 
					
						
							| 
									
										
										
										
											2022-05-05 15:37:26 +02:00
										 |  |  |     if (res < 0 && PyErr_ExceptionMatches(PyExc_KeyError)) { | 
					
						
							| 
									
										
										
										
											2024-05-06 10:50:35 -07:00
										 |  |  |         PyErr_Format(PyExc_AttributeError, | 
					
						
							|  |  |  |                         "'%.100s' object has no attribute '%U'", | 
					
						
							|  |  |  |                         tp->tp_name, name); | 
					
						
							|  |  |  |         _PyObject_SetAttributeErrorContext(obj, name); | 
					
						
							| 
									
										
										
										
											2022-05-05 15:37:26 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2001-12-04 15:54:53 +00:00
										 |  |  |   done: | 
					
						
							| 
									
										
										
										
											2025-03-26 14:38:47 -04:00
										 |  |  |     _PyThreadState_PopCStackRef(tstate, &cref); | 
					
						
							| 
									
										
										
										
											2022-05-05 15:37:26 +02:00
										 |  |  |     Py_DECREF(tp); | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     Py_DECREF(name); | 
					
						
							|  |  |  |     return res; | 
					
						
							| 
									
										
										
										
											1997-05-20 18:34:44 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-08-28 18:17:03 +00:00
										 |  |  | int | 
					
						
							|  |  |  | PyObject_GenericSetAttr(PyObject *obj, PyObject *name, PyObject *value) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     return _PyObject_GenericSetAttrWithDict(obj, name, value, NULL); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-02-19 19:59:10 -05:00
										 |  |  | int | 
					
						
							|  |  |  | PyObject_GenericSetDict(PyObject *obj, PyObject *value, void *context) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     if (value == NULL) { | 
					
						
							|  |  |  |         PyErr_SetString(PyExc_TypeError, "cannot delete __dict__"); | 
					
						
							|  |  |  |         return -1; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2025-05-28 19:03:41 +01:00
										 |  |  |     return _PyObject_SetDict(obj, value); | 
					
						
							| 
									
										
										
										
											2012-02-19 19:59:10 -05:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-08-28 18:17:03 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-07 16:12:52 +02:00
										 |  |  | /* Test a value used as condition, e.g., in a while or if statement.
 | 
					
						
							| 
									
										
										
										
											1993-05-12 08:24:20 +00:00
										 |  |  |    Return -1 if an error occurred */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | int | 
					
						
							| 
									
										
										
										
											2000-07-09 15:48:49 +00:00
										 |  |  | PyObject_IsTrue(PyObject *v) | 
					
						
							| 
									
										
										
										
											1993-05-12 08:24:20 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     Py_ssize_t res; | 
					
						
							|  |  |  |     if (v == Py_True) | 
					
						
							|  |  |  |         return 1; | 
					
						
							|  |  |  |     if (v == Py_False) | 
					
						
							|  |  |  |         return 0; | 
					
						
							|  |  |  |     if (v == Py_None) | 
					
						
							|  |  |  |         return 0; | 
					
						
							| 
									
										
										
										
											2020-02-07 03:04:21 +01:00
										 |  |  |     else if (Py_TYPE(v)->tp_as_number != NULL && | 
					
						
							|  |  |  |              Py_TYPE(v)->tp_as_number->nb_bool != NULL) | 
					
						
							|  |  |  |         res = (*Py_TYPE(v)->tp_as_number->nb_bool)(v); | 
					
						
							|  |  |  |     else if (Py_TYPE(v)->tp_as_mapping != NULL && | 
					
						
							|  |  |  |              Py_TYPE(v)->tp_as_mapping->mp_length != NULL) | 
					
						
							|  |  |  |         res = (*Py_TYPE(v)->tp_as_mapping->mp_length)(v); | 
					
						
							|  |  |  |     else if (Py_TYPE(v)->tp_as_sequence != NULL && | 
					
						
							|  |  |  |              Py_TYPE(v)->tp_as_sequence->sq_length != NULL) | 
					
						
							|  |  |  |         res = (*Py_TYPE(v)->tp_as_sequence->sq_length)(v); | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     else | 
					
						
							|  |  |  |         return 1; | 
					
						
							|  |  |  |     /* if it is negative, it should be either -1 or -2 */ | 
					
						
							|  |  |  |     return (res > 0) ? 1 : Py_SAFE_DOWNCAST(res, Py_ssize_t, int); | 
					
						
							| 
									
										
										
										
											1990-12-20 15:06:42 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-07-07 05:13:56 +00:00
										 |  |  | /* equivalent of 'not v'
 | 
					
						
							| 
									
										
										
										
											1998-04-09 17:53:59 +00:00
										 |  |  |    Return -1 if an error occurred */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | int | 
					
						
							| 
									
										
										
										
											2000-07-09 15:48:49 +00:00
										 |  |  | PyObject_Not(PyObject *v) | 
					
						
							| 
									
										
										
										
											1998-04-09 17:53:59 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     int res; | 
					
						
							|  |  |  |     res = PyObject_IsTrue(v); | 
					
						
							|  |  |  |     if (res < 0) | 
					
						
							|  |  |  |         return res; | 
					
						
							|  |  |  |     return res == 0; | 
					
						
							| 
									
										
										
										
											1998-04-09 17:53:59 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1995-01-26 00:38:22 +00:00
										 |  |  | /* Test whether an object can be called */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | int | 
					
						
							| 
									
										
										
										
											2000-07-09 15:48:49 +00:00
										 |  |  | PyCallable_Check(PyObject *x) | 
					
						
							| 
									
										
										
										
											1995-01-26 00:38:22 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     if (x == NULL) | 
					
						
							|  |  |  |         return 0; | 
					
						
							| 
									
										
										
										
											2020-02-07 03:04:21 +01:00
										 |  |  |     return Py_TYPE(x)->tp_call != NULL; | 
					
						
							| 
									
										
										
										
											1995-01-26 00:38:22 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-09-04 22:08:56 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-03-10 22:13:27 +00:00
										 |  |  | /* Helper for PyObject_Dir without arguments: returns the local scope. */ | 
					
						
							|  |  |  | static PyObject * | 
					
						
							| 
									
										
										
										
											2007-04-13 01:39:34 +00:00
										 |  |  | _dir_locals(void) | 
					
						
							| 
									
										
										
										
											2007-03-10 22:13:27 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     PyObject *names; | 
					
						
							| 
									
										
										
										
											2013-10-29 01:19:37 +01:00
										 |  |  |     PyObject *locals; | 
					
						
							| 
									
										
										
										
											2001-09-17 02:38:46 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-06-16 17:34:19 -06:00
										 |  |  |     if (_PyEval_GetFrame() != NULL) { | 
					
						
							|  |  |  |         locals = _PyEval_GetFrameLocals(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     else { | 
					
						
							|  |  |  |         PyThreadState *tstate = _PyThreadState_GET(); | 
					
						
							|  |  |  |         locals = _PyEval_GetGlobalsFromRunningMain(tstate); | 
					
						
							|  |  |  |         if (locals == NULL) { | 
					
						
							|  |  |  |             if (!_PyErr_Occurred(tstate)) { | 
					
						
							|  |  |  |                 locals = _PyEval_GetFrameLocals(); | 
					
						
							|  |  |  |                 assert(_PyErr_Occurred(tstate)); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         else { | 
					
						
							|  |  |  |             Py_INCREF(locals); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     if (locals == NULL) { | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |         return NULL; | 
					
						
							| 
									
										
										
										
											2025-06-16 17:34:19 -06:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2007-03-10 22:13:27 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     names = PyMapping_Keys(locals); | 
					
						
							| 
									
										
										
										
											2023-07-05 17:05:02 -06:00
										 |  |  |     Py_DECREF(locals); | 
					
						
							|  |  |  |     if (!names) { | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |         return NULL; | 
					
						
							| 
									
										
										
										
											2023-07-05 17:05:02 -06:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     if (!PyList_Check(names)) { | 
					
						
							|  |  |  |         PyErr_Format(PyExc_TypeError, | 
					
						
							|  |  |  |             "dir(): expected keys() of locals to be a list, " | 
					
						
							|  |  |  |             "not '%.200s'", Py_TYPE(names)->tp_name); | 
					
						
							|  |  |  |         Py_DECREF(names); | 
					
						
							|  |  |  |         return NULL; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2011-06-11 16:12:08 -05:00
										 |  |  |     if (PyList_Sort(names)) { | 
					
						
							|  |  |  |         Py_DECREF(names); | 
					
						
							|  |  |  |         return NULL; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     return names; | 
					
						
							| 
									
										
										
										
											2007-03-10 22:13:27 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-05-24 11:09:06 -05:00
										 |  |  | /* Helper for PyObject_Dir: object introspection. */ | 
					
						
							| 
									
										
										
										
											2007-03-10 22:13:27 +00:00
										 |  |  | static PyObject * | 
					
						
							|  |  |  | _dir_object(PyObject *obj) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2011-06-11 16:12:08 -05:00
										 |  |  |     PyObject *result, *sorted; | 
					
						
							| 
									
										
										
										
											2022-02-08 13:39:07 -07:00
										 |  |  |     PyObject *dirfunc = _PyObject_LookupSpecial(obj, &_Py_ID(__dir__)); | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-26 17:16:37 +02:00
										 |  |  |     assert(obj != NULL); | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     if (dirfunc == NULL) { | 
					
						
							| 
									
										
										
										
											2011-05-24 11:09:06 -05:00
										 |  |  |         if (!PyErr_Occurred()) | 
					
						
							|  |  |  |             PyErr_SetString(PyExc_TypeError, "object does not provide __dir__"); | 
					
						
							|  |  |  |         return NULL; | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2011-05-24 11:09:06 -05:00
										 |  |  |     /* use __dir__ */ | 
					
						
							| 
									
										
										
										
											2021-10-12 00:42:23 +02:00
										 |  |  |     result = _PyObject_CallNoArgs(dirfunc); | 
					
						
							| 
									
										
										
										
											2011-05-24 11:09:06 -05:00
										 |  |  |     Py_DECREF(dirfunc); | 
					
						
							|  |  |  |     if (result == NULL) | 
					
						
							|  |  |  |         return NULL; | 
					
						
							| 
									
										
										
										
											2011-06-11 16:12:08 -05:00
										 |  |  |     /* return sorted(result) */ | 
					
						
							|  |  |  |     sorted = PySequence_List(result); | 
					
						
							|  |  |  |     Py_DECREF(result); | 
					
						
							|  |  |  |     if (sorted == NULL) | 
					
						
							|  |  |  |         return NULL; | 
					
						
							|  |  |  |     if (PyList_Sort(sorted)) { | 
					
						
							|  |  |  |         Py_DECREF(sorted); | 
					
						
							|  |  |  |         return NULL; | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2011-06-11 16:12:08 -05:00
										 |  |  |     return sorted; | 
					
						
							| 
									
										
										
										
											2007-03-10 22:13:27 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* Implementation of dir() -- if obj is NULL, returns the names in the current
 | 
					
						
							|  |  |  |    (local) scope.  Otherwise, performs introspection of the object: returns a | 
					
						
							|  |  |  |    sorted list of attribute names (supposedly) accessible from the object | 
					
						
							|  |  |  | */ | 
					
						
							|  |  |  | PyObject * | 
					
						
							|  |  |  | PyObject_Dir(PyObject *obj) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2011-06-11 16:12:08 -05:00
										 |  |  |     return (obj == NULL) ? _dir_locals() : _dir_object(obj); | 
					
						
							| 
									
										
										
										
											2001-09-04 22:08:56 +00:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											1995-01-26 00:38:22 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1990-10-14 12:07:46 +00:00
										 |  |  | /*
 | 
					
						
							| 
									
										
										
										
											2011-07-28 09:55:13 -07:00
										 |  |  | None is a non-NULL undefined value. | 
					
						
							| 
									
										
										
										
											1990-10-14 12:07:46 +00:00
										 |  |  | There is (and should be!) no way to create other objects of this type, | 
					
						
							| 
									
										
										
										
											1990-12-20 15:06:42 +00:00
										 |  |  | so there is exactly one (which is indestructible, by the way). | 
					
						
							| 
									
										
										
										
											1990-10-14 12:07:46 +00:00
										 |  |  | */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1992-03-27 17:26:13 +00:00
										 |  |  | /* ARGSUSED */ | 
					
						
							| 
									
										
										
										
											1997-05-02 03:12:38 +00:00
										 |  |  | static PyObject * | 
					
						
							| 
									
										
										
										
											2000-07-09 15:48:49 +00:00
										 |  |  | none_repr(PyObject *op) | 
					
						
							| 
									
										
										
										
											1990-12-20 15:06:42 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     return PyUnicode_FromString("None"); | 
					
						
							| 
									
										
										
										
											1990-10-14 12:07:46 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-22 15:39:37 -04:00
										 |  |  | static void | 
					
						
							|  |  |  | none_dealloc(PyObject* none) | 
					
						
							| 
									
										
										
										
											2001-01-23 16:24:35 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2023-04-22 15:39:37 -04:00
										 |  |  |     /* This should never get called, but we also don't want to SEGV if
 | 
					
						
							|  |  |  |      * we accidentally decref None out of existence. Instead, | 
					
						
							|  |  |  |      * since None is an immortal object, re-set the reference count. | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     _Py_SetImmortal(none); | 
					
						
							| 
									
										
										
										
											2001-01-23 16:24:35 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-07-29 18:19:43 -05:00
										 |  |  | static PyObject * | 
					
						
							|  |  |  | none_new(PyTypeObject *type, PyObject *args, PyObject *kwargs) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2016-12-16 16:18:57 +02:00
										 |  |  |     if (PyTuple_GET_SIZE(args) || (kwargs && PyDict_GET_SIZE(kwargs))) { | 
					
						
							| 
									
										
										
										
											2011-07-29 18:19:43 -05:00
										 |  |  |         PyErr_SetString(PyExc_TypeError, "NoneType takes no arguments"); | 
					
						
							|  |  |  |         return NULL; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     Py_RETURN_NONE; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-07-28 09:55:13 -07:00
										 |  |  | static int | 
					
						
							|  |  |  | none_bool(PyObject *v) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-12-16 22:36:13 +02:00
										 |  |  | static Py_hash_t none_hash(PyObject *v) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     return 0xFCA86420; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-07-28 09:55:13 -07:00
										 |  |  | static PyNumberMethods none_as_number = { | 
					
						
							|  |  |  |     0,                          /* nb_add */ | 
					
						
							|  |  |  |     0,                          /* nb_subtract */ | 
					
						
							|  |  |  |     0,                          /* nb_multiply */ | 
					
						
							|  |  |  |     0,                          /* nb_remainder */ | 
					
						
							|  |  |  |     0,                          /* nb_divmod */ | 
					
						
							|  |  |  |     0,                          /* nb_power */ | 
					
						
							|  |  |  |     0,                          /* nb_negative */ | 
					
						
							|  |  |  |     0,                          /* nb_positive */ | 
					
						
							|  |  |  |     0,                          /* nb_absolute */ | 
					
						
							| 
									
										
										
										
											2025-04-01 17:18:11 +02:00
										 |  |  |     none_bool,                  /* nb_bool */ | 
					
						
							| 
									
										
										
										
											2011-07-28 09:55:13 -07:00
										 |  |  |     0,                          /* nb_invert */ | 
					
						
							|  |  |  |     0,                          /* nb_lshift */ | 
					
						
							|  |  |  |     0,                          /* nb_rshift */ | 
					
						
							|  |  |  |     0,                          /* nb_and */ | 
					
						
							|  |  |  |     0,                          /* nb_xor */ | 
					
						
							|  |  |  |     0,                          /* nb_or */ | 
					
						
							|  |  |  |     0,                          /* nb_int */ | 
					
						
							|  |  |  |     0,                          /* nb_reserved */ | 
					
						
							|  |  |  |     0,                          /* nb_float */ | 
					
						
							|  |  |  |     0,                          /* nb_inplace_add */ | 
					
						
							|  |  |  |     0,                          /* nb_inplace_subtract */ | 
					
						
							|  |  |  |     0,                          /* nb_inplace_multiply */ | 
					
						
							|  |  |  |     0,                          /* nb_inplace_remainder */ | 
					
						
							|  |  |  |     0,                          /* nb_inplace_power */ | 
					
						
							|  |  |  |     0,                          /* nb_inplace_lshift */ | 
					
						
							|  |  |  |     0,                          /* nb_inplace_rshift */ | 
					
						
							|  |  |  |     0,                          /* nb_inplace_and */ | 
					
						
							|  |  |  |     0,                          /* nb_inplace_xor */ | 
					
						
							|  |  |  |     0,                          /* nb_inplace_or */ | 
					
						
							|  |  |  |     0,                          /* nb_floor_divide */ | 
					
						
							|  |  |  |     0,                          /* nb_true_divide */ | 
					
						
							|  |  |  |     0,                          /* nb_inplace_floor_divide */ | 
					
						
							|  |  |  |     0,                          /* nb_inplace_true_divide */ | 
					
						
							|  |  |  |     0,                          /* nb_index */ | 
					
						
							|  |  |  | }; | 
					
						
							| 
									
										
										
										
											2001-01-23 16:24:35 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-12 15:45:23 +03:00
										 |  |  | PyDoc_STRVAR(none_doc, | 
					
						
							|  |  |  | "NoneType()\n" | 
					
						
							|  |  |  | "--\n\n" | 
					
						
							|  |  |  | "The type of the None singleton."); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-11-30 17:55:48 -08:00
										 |  |  | PyTypeObject _PyNone_Type = { | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     PyVarObject_HEAD_INIT(&PyType_Type, 0) | 
					
						
							|  |  |  |     "NoneType", | 
					
						
							|  |  |  |     0, | 
					
						
							|  |  |  |     0, | 
					
						
							| 
									
										
										
										
											2023-04-22 15:39:37 -04:00
										 |  |  |     none_dealloc,       /*tp_dealloc*/ | 
					
						
							| 
									
										
										
										
											2019-05-31 04:13:39 +02:00
										 |  |  |     0,                  /*tp_vectorcall_offset*/ | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     0,                  /*tp_getattr*/ | 
					
						
							|  |  |  |     0,                  /*tp_setattr*/ | 
					
						
							| 
									
										
										
										
											2019-05-31 04:13:39 +02:00
										 |  |  |     0,                  /*tp_as_async*/ | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     none_repr,          /*tp_repr*/ | 
					
						
							| 
									
										
										
										
											2011-07-28 09:55:13 -07:00
										 |  |  |     &none_as_number,    /*tp_as_number*/ | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     0,                  /*tp_as_sequence*/ | 
					
						
							|  |  |  |     0,                  /*tp_as_mapping*/ | 
					
						
							| 
									
										
										
										
											2025-04-01 17:18:11 +02:00
										 |  |  |     none_hash,          /*tp_hash */ | 
					
						
							| 
									
										
										
										
											2011-07-29 18:19:43 -05:00
										 |  |  |     0,                  /*tp_call */ | 
					
						
							|  |  |  |     0,                  /*tp_str */ | 
					
						
							|  |  |  |     0,                  /*tp_getattro */ | 
					
						
							|  |  |  |     0,                  /*tp_setattro */ | 
					
						
							|  |  |  |     0,                  /*tp_as_buffer */ | 
					
						
							|  |  |  |     Py_TPFLAGS_DEFAULT, /*tp_flags */ | 
					
						
							| 
									
										
										
										
											2024-04-12 15:45:23 +03:00
										 |  |  |     none_doc,           /*tp_doc */ | 
					
						
							| 
									
										
										
										
											2011-07-29 18:19:43 -05:00
										 |  |  |     0,                  /*tp_traverse */ | 
					
						
							|  |  |  |     0,                  /*tp_clear */ | 
					
						
							| 
									
										
										
										
											2023-12-07 04:56:01 -08:00
										 |  |  |     _Py_BaseObject_RichCompare, /*tp_richcompare */ | 
					
						
							| 
									
										
										
										
											2011-07-29 18:19:43 -05:00
										 |  |  |     0,                  /*tp_weaklistoffset */ | 
					
						
							|  |  |  |     0,                  /*tp_iter */ | 
					
						
							|  |  |  |     0,                  /*tp_iternext */ | 
					
						
							|  |  |  |     0,                  /*tp_methods */ | 
					
						
							|  |  |  |     0,                  /*tp_members */ | 
					
						
							|  |  |  |     0,                  /*tp_getset */ | 
					
						
							|  |  |  |     0,                  /*tp_base */ | 
					
						
							|  |  |  |     0,                  /*tp_dict */ | 
					
						
							|  |  |  |     0,                  /*tp_descr_get */ | 
					
						
							|  |  |  |     0,                  /*tp_descr_set */ | 
					
						
							|  |  |  |     0,                  /*tp_dictoffset */ | 
					
						
							|  |  |  |     0,                  /*tp_init */ | 
					
						
							|  |  |  |     0,                  /*tp_alloc */ | 
					
						
							|  |  |  |     none_new,           /*tp_new */ | 
					
						
							| 
									
										
										
										
											1990-10-14 12:07:46 +00:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-10-30 12:06:09 -04:00
										 |  |  | PyObject _Py_NoneStruct = _PyObject_HEAD_INIT(&_PyNone_Type); | 
					
						
							| 
									
										
										
										
											1990-10-14 12:07:46 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-01-04 01:48:10 +00:00
										 |  |  | /* NotImplemented is an object that can be used to signal that an
 | 
					
						
							|  |  |  |    operation is not implemented for the given type combination. */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static PyObject * | 
					
						
							|  |  |  | NotImplemented_repr(PyObject *op) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     return PyUnicode_FromString("NotImplemented"); | 
					
						
							| 
									
										
										
										
											2001-01-04 01:48:10 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-11-24 02:53:45 -08:00
										 |  |  | static PyObject * | 
					
						
							| 
									
										
										
										
											2018-04-30 00:29:33 +05:30
										 |  |  | NotImplemented_reduce(PyObject *op, PyObject *Py_UNUSED(ignored)) | 
					
						
							| 
									
										
										
										
											2013-11-24 02:53:45 -08:00
										 |  |  | { | 
					
						
							|  |  |  |     return PyUnicode_FromString("NotImplemented"); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static PyMethodDef notimplemented_methods[] = { | 
					
						
							| 
									
										
										
										
											2018-04-30 00:29:33 +05:30
										 |  |  |     {"__reduce__", NotImplemented_reduce, METH_NOARGS, NULL}, | 
					
						
							| 
									
										
										
										
											2013-11-24 02:53:45 -08:00
										 |  |  |     {NULL, NULL} | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-07-29 18:27:44 -05:00
										 |  |  | static PyObject * | 
					
						
							|  |  |  | notimplemented_new(PyTypeObject *type, PyObject *args, PyObject *kwargs) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2016-12-16 16:18:57 +02:00
										 |  |  |     if (PyTuple_GET_SIZE(args) || (kwargs && PyDict_GET_SIZE(kwargs))) { | 
					
						
							| 
									
										
										
										
											2011-07-29 18:27:44 -05:00
										 |  |  |         PyErr_SetString(PyExc_TypeError, "NotImplementedType takes no arguments"); | 
					
						
							|  |  |  |         return NULL; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2011-08-10 20:28:54 -05:00
										 |  |  |     Py_RETURN_NOTIMPLEMENTED; | 
					
						
							| 
									
										
										
										
											2011-07-29 18:27:44 -05:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-22 15:39:37 -04:00
										 |  |  | static void | 
					
						
							|  |  |  | notimplemented_dealloc(PyObject *notimplemented) | 
					
						
							| 
									
										
										
										
											2012-10-06 14:28:58 +02:00
										 |  |  | { | 
					
						
							|  |  |  |     /* This should never get called, but we also don't want to SEGV if
 | 
					
						
							| 
									
										
										
										
											2023-04-22 15:39:37 -04:00
										 |  |  |      * we accidentally decref NotImplemented out of existence. Instead, | 
					
						
							|  |  |  |      * since Notimplemented is an immortal object, re-set the reference count. | 
					
						
							| 
									
										
										
										
											2012-10-06 14:28:58 +02:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2023-04-22 15:39:37 -04:00
										 |  |  |     _Py_SetImmortal(notimplemented); | 
					
						
							| 
									
										
										
										
											2012-10-06 14:28:58 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-03 18:50:17 +00:00
										 |  |  | static int | 
					
						
							|  |  |  | notimplemented_bool(PyObject *v) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2024-05-08 11:12:00 -07:00
										 |  |  |     PyErr_SetString(PyExc_TypeError, | 
					
						
							|  |  |  |                     "NotImplemented should not be used in a boolean context"); | 
					
						
							|  |  |  |     return -1; | 
					
						
							| 
									
										
										
										
											2020-03-03 18:50:17 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static PyNumberMethods notimplemented_as_number = { | 
					
						
							|  |  |  |     .nb_bool = notimplemented_bool, | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-12 15:45:23 +03:00
										 |  |  | PyDoc_STRVAR(notimplemented_doc, | 
					
						
							|  |  |  | "NotImplementedType()\n" | 
					
						
							|  |  |  | "--\n\n" | 
					
						
							|  |  |  | "The type of the NotImplemented singleton."); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-11-30 17:55:48 -08:00
										 |  |  | PyTypeObject _PyNotImplemented_Type = { | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     PyVarObject_HEAD_INIT(&PyType_Type, 0) | 
					
						
							|  |  |  |     "NotImplementedType", | 
					
						
							|  |  |  |     0, | 
					
						
							|  |  |  |     0, | 
					
						
							| 
									
										
										
										
											2012-10-06 14:28:58 +02:00
										 |  |  |     notimplemented_dealloc,       /*tp_dealloc*/ /*never called*/ | 
					
						
							| 
									
										
										
										
											2019-05-31 04:13:39 +02:00
										 |  |  |     0,                  /*tp_vectorcall_offset*/ | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     0,                  /*tp_getattr*/ | 
					
						
							|  |  |  |     0,                  /*tp_setattr*/ | 
					
						
							| 
									
										
										
										
											2019-05-31 04:13:39 +02:00
										 |  |  |     0,                  /*tp_as_async*/ | 
					
						
							| 
									
										
										
										
											2020-03-03 18:50:17 +00:00
										 |  |  |     NotImplemented_repr,        /*tp_repr*/ | 
					
						
							|  |  |  |     ¬implemented_as_number,  /*tp_as_number*/ | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     0,                  /*tp_as_sequence*/ | 
					
						
							|  |  |  |     0,                  /*tp_as_mapping*/ | 
					
						
							|  |  |  |     0,                  /*tp_hash */ | 
					
						
							| 
									
										
										
										
											2011-07-29 18:27:44 -05:00
										 |  |  |     0,                  /*tp_call */ | 
					
						
							|  |  |  |     0,                  /*tp_str */ | 
					
						
							|  |  |  |     0,                  /*tp_getattro */ | 
					
						
							|  |  |  |     0,                  /*tp_setattro */ | 
					
						
							|  |  |  |     0,                  /*tp_as_buffer */ | 
					
						
							|  |  |  |     Py_TPFLAGS_DEFAULT, /*tp_flags */ | 
					
						
							| 
									
										
										
										
											2024-04-12 15:45:23 +03:00
										 |  |  |     notimplemented_doc, /*tp_doc */ | 
					
						
							| 
									
										
										
										
											2011-07-29 18:27:44 -05:00
										 |  |  |     0,                  /*tp_traverse */ | 
					
						
							|  |  |  |     0,                  /*tp_clear */ | 
					
						
							|  |  |  |     0,                  /*tp_richcompare */ | 
					
						
							|  |  |  |     0,                  /*tp_weaklistoffset */ | 
					
						
							|  |  |  |     0,                  /*tp_iter */ | 
					
						
							|  |  |  |     0,                  /*tp_iternext */ | 
					
						
							| 
									
										
										
										
											2013-11-24 02:53:45 -08:00
										 |  |  |     notimplemented_methods, /*tp_methods */ | 
					
						
							| 
									
										
										
										
											2011-07-29 18:27:44 -05:00
										 |  |  |     0,                  /*tp_members */ | 
					
						
							|  |  |  |     0,                  /*tp_getset */ | 
					
						
							|  |  |  |     0,                  /*tp_base */ | 
					
						
							|  |  |  |     0,                  /*tp_dict */ | 
					
						
							|  |  |  |     0,                  /*tp_descr_get */ | 
					
						
							|  |  |  |     0,                  /*tp_descr_set */ | 
					
						
							|  |  |  |     0,                  /*tp_dictoffset */ | 
					
						
							|  |  |  |     0,                  /*tp_init */ | 
					
						
							|  |  |  |     0,                  /*tp_alloc */ | 
					
						
							|  |  |  |     notimplemented_new, /*tp_new */ | 
					
						
							| 
									
										
										
										
											2001-01-04 01:48:10 +00:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-10-30 12:06:09 -04:00
										 |  |  | PyObject _Py_NotImplementedStruct = _PyObject_HEAD_INIT(&_PyNotImplemented_Type); | 
					
						
							| 
									
										
										
										
											2001-01-04 01:48:10 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-07 13:14:56 -06:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-09-01 12:43:30 +02:00
										 |  |  | PyStatus | 
					
						
							| 
									
										
										
										
											2023-08-07 13:14:56 -06:00
										 |  |  | _PyObject_InitState(PyInterpreterState *interp) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | #ifdef Py_TRACE_REFS
 | 
					
						
							| 
									
										
										
										
											2024-10-23 10:10:06 -06:00
										 |  |  |     if (refchain_init(interp) < 0) { | 
					
						
							| 
									
										
										
										
											2023-09-01 12:43:30 +02:00
										 |  |  |         return _PyStatus_NO_MEMORY(); | 
					
						
							| 
									
										
										
										
											2023-08-07 13:14:56 -06:00
										 |  |  |     } | 
					
						
							|  |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2023-09-01 12:43:30 +02:00
										 |  |  |     return _PyStatus_OK(); | 
					
						
							| 
									
										
										
										
											2023-08-07 13:14:56 -06:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-31 18:33:34 +02:00
										 |  |  | void | 
					
						
							|  |  |  | _PyObject_FiniState(PyInterpreterState *interp) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | #ifdef Py_TRACE_REFS
 | 
					
						
							| 
									
										
										
										
											2024-10-23 10:10:06 -06:00
										 |  |  |     refchain_fini(interp); | 
					
						
							| 
									
										
										
										
											2023-08-31 18:33:34 +02:00
										 |  |  | #endif
 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-07 13:14:56 -06:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-29 04:05:11 +02:00
										 |  |  | extern PyTypeObject _PyAnextAwaitable_Type; | 
					
						
							|  |  |  | extern PyTypeObject _PyLegacyEventHandler_Type; | 
					
						
							| 
									
										
										
										
											2022-05-19 19:25:22 +05:30
										 |  |  | extern PyTypeObject _PyLineIterator; | 
					
						
							| 
									
										
										
										
											2023-08-29 04:05:11 +02:00
										 |  |  | extern PyTypeObject _PyMemoryIter_Type; | 
					
						
							| 
									
										
										
										
											2022-05-19 19:25:22 +05:30
										 |  |  | extern PyTypeObject _PyPositionsIterator; | 
					
						
							| 
									
										
										
										
											2023-08-29 04:05:11 +02:00
										 |  |  | extern PyTypeObject _Py_GenericAliasIterType; | 
					
						
							| 
									
										
										
										
											2022-02-25 12:34:00 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-21 13:06:34 +01:00
										 |  |  | static PyTypeObject* static_types[] = { | 
					
						
							| 
									
										
										
										
											2022-01-22 19:31:24 +01:00
										 |  |  |     // The two most important base types: must be initialized first and
 | 
					
						
							|  |  |  |     // deallocated last.
 | 
					
						
							|  |  |  |     &PyBaseObject_Type, | 
					
						
							|  |  |  |     &PyType_Type, | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // Static types with base=&PyBaseObject_Type
 | 
					
						
							| 
									
										
										
										
											2022-01-21 13:06:34 +01:00
										 |  |  |     &PyAsyncGen_Type, | 
					
						
							|  |  |  |     &PyByteArrayIter_Type, | 
					
						
							|  |  |  |     &PyByteArray_Type, | 
					
						
							| 
									
										
										
										
											2022-01-21 17:53:13 +01:00
										 |  |  |     &PyBytesIter_Type, | 
					
						
							|  |  |  |     &PyBytes_Type, | 
					
						
							| 
									
										
										
										
											2022-01-21 13:06:34 +01:00
										 |  |  |     &PyCFunction_Type, | 
					
						
							|  |  |  |     &PyCallIter_Type, | 
					
						
							|  |  |  |     &PyCapsule_Type, | 
					
						
							|  |  |  |     &PyCell_Type, | 
					
						
							|  |  |  |     &PyClassMethodDescr_Type, | 
					
						
							|  |  |  |     &PyClassMethod_Type, | 
					
						
							|  |  |  |     &PyCode_Type, | 
					
						
							|  |  |  |     &PyComplex_Type, | 
					
						
							| 
									
										
										
										
											2022-01-22 18:55:48 +01:00
										 |  |  |     &PyContextToken_Type, | 
					
						
							|  |  |  |     &PyContextVar_Type, | 
					
						
							|  |  |  |     &PyContext_Type, | 
					
						
							| 
									
										
										
										
											2022-01-21 13:06:34 +01:00
										 |  |  |     &PyCoro_Type, | 
					
						
							|  |  |  |     &PyDictItems_Type, | 
					
						
							|  |  |  |     &PyDictIterItem_Type, | 
					
						
							|  |  |  |     &PyDictIterKey_Type, | 
					
						
							|  |  |  |     &PyDictIterValue_Type, | 
					
						
							|  |  |  |     &PyDictKeys_Type, | 
					
						
							|  |  |  |     &PyDictProxy_Type, | 
					
						
							|  |  |  |     &PyDictRevIterItem_Type, | 
					
						
							|  |  |  |     &PyDictRevIterKey_Type, | 
					
						
							|  |  |  |     &PyDictRevIterValue_Type, | 
					
						
							|  |  |  |     &PyDictValues_Type, | 
					
						
							|  |  |  |     &PyDict_Type, | 
					
						
							|  |  |  |     &PyEllipsis_Type, | 
					
						
							|  |  |  |     &PyEnum_Type, | 
					
						
							| 
									
										
										
										
											2022-01-22 18:55:48 +01:00
										 |  |  |     &PyFilter_Type, | 
					
						
							| 
									
										
										
										
											2022-01-21 17:53:13 +01:00
										 |  |  |     &PyFloat_Type, | 
					
						
							| 
									
										
										
										
											2022-01-21 13:06:34 +01:00
										 |  |  |     &PyFrame_Type, | 
					
						
							| 
									
										
										
										
											2024-05-04 04:12:10 -07:00
										 |  |  |     &PyFrameLocalsProxy_Type, | 
					
						
							| 
									
										
										
										
											2022-01-21 13:06:34 +01:00
										 |  |  |     &PyFrozenSet_Type, | 
					
						
							|  |  |  |     &PyFunction_Type, | 
					
						
							|  |  |  |     &PyGen_Type, | 
					
						
							|  |  |  |     &PyGetSetDescr_Type, | 
					
						
							|  |  |  |     &PyInstanceMethod_Type, | 
					
						
							|  |  |  |     &PyListIter_Type, | 
					
						
							|  |  |  |     &PyListRevIter_Type, | 
					
						
							|  |  |  |     &PyList_Type, | 
					
						
							|  |  |  |     &PyLongRangeIter_Type, | 
					
						
							| 
									
										
										
										
											2022-01-21 17:53:13 +01:00
										 |  |  |     &PyLong_Type, | 
					
						
							| 
									
										
										
										
											2022-01-22 18:55:48 +01:00
										 |  |  |     &PyMap_Type, | 
					
						
							| 
									
										
										
										
											2022-01-21 13:06:34 +01:00
										 |  |  |     &PyMemberDescr_Type, | 
					
						
							|  |  |  |     &PyMemoryView_Type, | 
					
						
							|  |  |  |     &PyMethodDescr_Type, | 
					
						
							|  |  |  |     &PyMethod_Type, | 
					
						
							|  |  |  |     &PyModuleDef_Type, | 
					
						
							|  |  |  |     &PyModule_Type, | 
					
						
							|  |  |  |     &PyODictIter_Type, | 
					
						
							|  |  |  |     &PyPickleBuffer_Type, | 
					
						
							|  |  |  |     &PyProperty_Type, | 
					
						
							|  |  |  |     &PyRangeIter_Type, | 
					
						
							|  |  |  |     &PyRange_Type, | 
					
						
							|  |  |  |     &PyReversed_Type, | 
					
						
							|  |  |  |     &PySTEntry_Type, | 
					
						
							|  |  |  |     &PySeqIter_Type, | 
					
						
							|  |  |  |     &PySetIter_Type, | 
					
						
							|  |  |  |     &PySet_Type, | 
					
						
							|  |  |  |     &PySlice_Type, | 
					
						
							|  |  |  |     &PyStaticMethod_Type, | 
					
						
							|  |  |  |     &PyStdPrinter_Type, | 
					
						
							|  |  |  |     &PySuper_Type, | 
					
						
							|  |  |  |     &PyTraceBack_Type, | 
					
						
							| 
									
										
										
										
											2022-01-21 17:53:13 +01:00
										 |  |  |     &PyTupleIter_Type, | 
					
						
							|  |  |  |     &PyTuple_Type, | 
					
						
							|  |  |  |     &PyUnicodeIter_Type, | 
					
						
							|  |  |  |     &PyUnicode_Type, | 
					
						
							| 
									
										
										
										
											2022-01-21 13:06:34 +01:00
										 |  |  |     &PyWrapperDescr_Type, | 
					
						
							| 
									
										
										
										
											2022-01-22 18:55:48 +01:00
										 |  |  |     &PyZip_Type, | 
					
						
							| 
									
										
										
										
											2022-01-21 13:06:34 +01:00
										 |  |  |     &Py_GenericAliasType, | 
					
						
							|  |  |  |     &_PyAnextAwaitable_Type, | 
					
						
							|  |  |  |     &_PyAsyncGenASend_Type, | 
					
						
							|  |  |  |     &_PyAsyncGenAThrow_Type, | 
					
						
							|  |  |  |     &_PyAsyncGenWrappedValue_Type, | 
					
						
							| 
									
										
										
										
											2023-05-04 07:59:46 -07:00
										 |  |  |     &_PyBufferWrapper_Type, | 
					
						
							| 
									
										
										
										
											2022-01-22 18:55:48 +01:00
										 |  |  |     &_PyContextTokenMissing_Type, | 
					
						
							| 
									
										
										
										
											2022-01-21 13:06:34 +01:00
										 |  |  |     &_PyCoroWrapper_Type, | 
					
						
							| 
									
										
										
										
											2022-04-20 23:10:41 +09:00
										 |  |  |     &_Py_GenericAliasIterType, | 
					
						
							| 
									
										
										
										
											2022-01-22 18:55:48 +01:00
										 |  |  |     &_PyHamtItems_Type, | 
					
						
							|  |  |  |     &_PyHamtKeys_Type, | 
					
						
							|  |  |  |     &_PyHamtValues_Type, | 
					
						
							|  |  |  |     &_PyHamt_ArrayNode_Type, | 
					
						
							|  |  |  |     &_PyHamt_BitmapNode_Type, | 
					
						
							|  |  |  |     &_PyHamt_CollisionNode_Type, | 
					
						
							|  |  |  |     &_PyHamt_Type, | 
					
						
							| 
									
										
										
										
											2024-04-17 16:42:04 +01:00
										 |  |  |     &_PyInstructionSequence_Type, | 
					
						
							| 
									
										
										
										
											2025-04-30 11:46:41 +02:00
										 |  |  |     &_PyInterpolation_Type, | 
					
						
							| 
									
										
										
										
											2023-04-12 12:04:55 +01:00
										 |  |  |     &_PyLegacyEventHandler_Type, | 
					
						
							| 
									
										
										
										
											2022-05-19 19:25:22 +05:30
										 |  |  |     &_PyLineIterator, | 
					
						
							| 
									
										
										
										
											2022-01-21 13:06:34 +01:00
										 |  |  |     &_PyManagedBuffer_Type, | 
					
						
							| 
									
										
										
										
											2022-05-14 19:54:20 +05:30
										 |  |  |     &_PyMemoryIter_Type, | 
					
						
							| 
									
										
										
										
											2022-01-21 13:06:34 +01:00
										 |  |  |     &_PyMethodWrapper_Type, | 
					
						
							|  |  |  |     &_PyNamespace_Type, | 
					
						
							|  |  |  |     &_PyNone_Type, | 
					
						
							|  |  |  |     &_PyNotImplemented_Type, | 
					
						
							| 
									
										
										
										
											2022-05-19 19:25:22 +05:30
										 |  |  |     &_PyPositionsIterator, | 
					
						
							| 
									
										
										
										
											2025-04-30 11:46:41 +02:00
										 |  |  |     &_PyTemplate_Type, | 
					
						
							|  |  |  |     &_PyTemplateIter_Type, | 
					
						
							| 
									
										
										
										
											2022-04-18 19:48:27 +05:30
										 |  |  |     &_PyUnicodeASCIIIter_Type, | 
					
						
							| 
									
										
										
										
											2022-01-21 13:06:34 +01:00
										 |  |  |     &_PyUnion_Type, | 
					
						
							| 
									
										
										
										
											2024-04-30 18:26:34 -07:00
										 |  |  | #ifdef _Py_TIER2
 | 
					
						
							| 
									
										
										
										
											2023-10-29 13:53:25 -07:00
										 |  |  |     &_PyUOpExecutor_Type, | 
					
						
							| 
									
										
										
										
											2024-04-30 18:26:34 -07:00
										 |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2022-01-21 13:06:34 +01:00
										 |  |  |     &_PyWeakref_CallableProxyType, | 
					
						
							|  |  |  |     &_PyWeakref_ProxyType, | 
					
						
							|  |  |  |     &_PyWeakref_RefType, | 
					
						
							| 
									
										
										
										
											2023-05-18 15:56:15 -07:00
										 |  |  |     &_PyTypeAlias_Type, | 
					
						
							| 
									
										
										
										
											2024-08-28 21:27:40 -04:00
										 |  |  |     &_PyNoDefault_Type, | 
					
						
							| 
									
										
										
										
											2022-01-21 13:06:34 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     // subclasses: _PyTypes_FiniTypes() deallocates them before their base
 | 
					
						
							|  |  |  |     // class
 | 
					
						
							| 
									
										
										
										
											2022-01-21 17:53:13 +01:00
										 |  |  |     &PyBool_Type,         // base=&PyLong_Type
 | 
					
						
							| 
									
										
										
										
											2022-01-21 13:06:34 +01:00
										 |  |  |     &PyCMethod_Type,      // base=&PyCFunction_Type
 | 
					
						
							|  |  |  |     &PyODictItems_Type,   // base=&PyDictItems_Type
 | 
					
						
							|  |  |  |     &PyODictKeys_Type,    // base=&PyDictKeys_Type
 | 
					
						
							|  |  |  |     &PyODictValues_Type,  // base=&PyDictValues_Type
 | 
					
						
							|  |  |  |     &PyODict_Type,        // base=&PyDict_Type
 | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-09 12:59:26 -07:00
										 |  |  | PyStatus | 
					
						
							|  |  |  | _PyTypes_InitTypes(PyInterpreterState *interp) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     // All other static types (unless initialized elsewhere)
 | 
					
						
							| 
									
										
										
										
											2022-01-21 13:06:34 +01:00
										 |  |  |     for (size_t i=0; i < Py_ARRAY_LENGTH(static_types); i++) { | 
					
						
							|  |  |  |         PyTypeObject *type = static_types[i]; | 
					
						
							| 
									
										
										
										
											2023-05-01 19:36:00 -06:00
										 |  |  |         if (_PyStaticType_InitBuiltin(interp, type) < 0) { | 
					
						
							| 
									
										
										
										
											2022-07-25 12:47:31 -06:00
										 |  |  |             return _PyStatus_ERR("Can't initialize builtin type"); | 
					
						
							| 
									
										
										
										
											2022-01-21 13:06:34 +01:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2022-01-22 19:31:24 +01:00
										 |  |  |         if (type == &PyType_Type) { | 
					
						
							|  |  |  |             // Sanitify checks of the two most important types
 | 
					
						
							|  |  |  |             assert(PyBaseObject_Type.tp_base == NULL); | 
					
						
							|  |  |  |             assert(PyType_Type.tp_base == &PyBaseObject_Type); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2022-01-21 13:06:34 +01:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-01-22 21:18:05 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-10-11 03:56:01 -04:00
										 |  |  |     // Cache __reduce__ from PyBaseObject_Type object
 | 
					
						
							|  |  |  |     PyObject *baseobj_dict = _PyType_GetDict(&PyBaseObject_Type); | 
					
						
							|  |  |  |     PyObject *baseobj_reduce = PyDict_GetItemWithError(baseobj_dict, &_Py_ID(__reduce__)); | 
					
						
							|  |  |  |     if (baseobj_reduce == NULL && PyErr_Occurred()) { | 
					
						
							|  |  |  |         return _PyStatus_ERR("Can't get __reduce__ from base object"); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     _Py_INTERP_CACHED_OBJECT(interp, objreduce) = baseobj_reduce; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-15 20:36:23 -07:00
										 |  |  |     // Must be after static types are initialized
 | 
					
						
							|  |  |  |     if (_Py_initialize_generic(interp) < 0) { | 
					
						
							|  |  |  |         return _PyStatus_ERR("Can't initialize generic types"); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-08 00:12:38 +02:00
										 |  |  |     return _PyStatus_OK(); | 
					
						
							| 
									
										
										
										
											2001-08-16 08:17:26 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1990-10-14 12:07:46 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-21 13:06:34 +01:00
										 |  |  | // Best-effort function clearing static types.
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | // Don't deallocate a type if it still has subclasses. If a Py_Finalize()
 | 
					
						
							|  |  |  | // sub-function is interrupted by CTRL+C or fails with MemoryError, some
 | 
					
						
							|  |  |  | // subclasses are not cleared properly. Leave the static type unchanged in this
 | 
					
						
							|  |  |  | // case.
 | 
					
						
							|  |  |  | void | 
					
						
							|  |  |  | _PyTypes_FiniTypes(PyInterpreterState *interp) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     // Deallocate types in the reverse order to deallocate subclasses before
 | 
					
						
							|  |  |  |     // their base classes.
 | 
					
						
							|  |  |  |     for (Py_ssize_t i=Py_ARRAY_LENGTH(static_types)-1; i>=0; i--) { | 
					
						
							|  |  |  |         PyTypeObject *type = static_types[i]; | 
					
						
							| 
									
										
										
										
											2024-06-03 19:09:18 -04:00
										 |  |  |         _PyStaticType_FiniBuiltin(interp, type); | 
					
						
							| 
									
										
										
										
											2022-01-21 13:06:34 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-08 12:03:50 -07:00
										 |  |  | static inline void | 
					
						
							|  |  |  | new_reference(PyObject *op) | 
					
						
							| 
									
										
										
										
											2020-02-05 01:11:10 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2023-04-22 15:39:37 -04:00
										 |  |  |     // Skip the immortal object check in Py_SET_REFCNT; always set refcnt to 1
 | 
					
						
							| 
									
										
										
										
											2023-11-20 15:52:00 +02:00
										 |  |  | #if !defined(Py_GIL_DISABLED)
 | 
					
						
							| 
									
										
										
										
											2024-12-11 17:37:38 +00:00
										 |  |  | #if SIZEOF_VOID_P > 4
 | 
					
						
							|  |  |  |     op->ob_refcnt_full = 1; | 
					
						
							|  |  |  |     assert(op->ob_refcnt == 1); | 
					
						
							|  |  |  |     assert(op->ob_flags == 0); | 
					
						
							|  |  |  | #else
 | 
					
						
							| 
									
										
										
										
											2023-04-22 15:39:37 -04:00
										 |  |  |     op->ob_refcnt = 1; | 
					
						
							| 
									
										
										
										
											2024-12-11 17:37:38 +00:00
										 |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2023-10-30 12:06:09 -04:00
										 |  |  | #else
 | 
					
						
							| 
									
										
										
										
											2024-12-11 17:37:38 +00:00
										 |  |  |     op->ob_flags = 0; | 
					
						
							| 
									
										
										
										
											2024-06-20 11:29:08 -04:00
										 |  |  |     op->ob_mutex = (PyMutex){ 0 }; | 
					
						
							| 
									
										
										
										
											2025-02-06 23:35:37 +03:00
										 |  |  | #ifdef _Py_THREAD_SANITIZER
 | 
					
						
							|  |  |  |     _Py_atomic_store_uintptr_relaxed(&op->ob_tid, _Py_ThreadId()); | 
					
						
							|  |  |  |     _Py_atomic_store_uint8_relaxed(&op->ob_gc_bits, 0); | 
					
						
							|  |  |  |     _Py_atomic_store_uint32_relaxed(&op->ob_ref_local, 1); | 
					
						
							|  |  |  |     _Py_atomic_store_ssize_relaxed(&op->ob_ref_shared, 0); | 
					
						
							|  |  |  | #else
 | 
					
						
							|  |  |  |     op->ob_tid = _Py_ThreadId(); | 
					
						
							| 
									
										
										
										
											2023-10-30 12:06:09 -04:00
										 |  |  |     op->ob_gc_bits = 0; | 
					
						
							|  |  |  |     op->ob_ref_local = 1; | 
					
						
							|  |  |  |     op->ob_ref_shared = 0; | 
					
						
							|  |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2025-02-06 23:35:37 +03:00
										 |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2020-02-05 01:11:10 +01:00
										 |  |  | #ifdef Py_TRACE_REFS
 | 
					
						
							| 
									
										
										
										
											2023-08-31 18:33:34 +02:00
										 |  |  |     _Py_AddToAllObjects(op); | 
					
						
							| 
									
										
										
										
											2020-02-05 01:11:10 +01:00
										 |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2024-11-19 10:35:17 +00:00
										 |  |  |     _PyReftracerTrack(op, PyRefTracer_CREATE); | 
					
						
							| 
									
										
										
										
											2020-02-05 01:11:10 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-08 12:03:50 -07:00
										 |  |  | void | 
					
						
							|  |  |  | _Py_NewReference(PyObject *op) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | #ifdef Py_REF_DEBUG
 | 
					
						
							| 
									
										
										
										
											2024-04-08 12:11:36 -04:00
										 |  |  |     _Py_IncRefTotal(_PyThreadState_GET()); | 
					
						
							| 
									
										
										
										
											2023-03-08 12:03:50 -07:00
										 |  |  | #endif
 | 
					
						
							|  |  |  |     new_reference(op); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void | 
					
						
							|  |  |  | _Py_NewReferenceNoTotal(PyObject *op) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     new_reference(op); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-20 08:54:42 +00:00
										 |  |  | void | 
					
						
							|  |  |  | _Py_SetImmortalUntracked(PyObject *op) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2024-06-21 17:19:31 +02:00
										 |  |  | #ifdef Py_DEBUG
 | 
					
						
							|  |  |  |     // For strings, use _PyUnicode_InternImmortal instead.
 | 
					
						
							|  |  |  |     if (PyUnicode_CheckExact(op)) { | 
					
						
							|  |  |  |         assert(PyUnicode_CHECK_INTERNED(op) == SSTATE_INTERNED_IMMORTAL | 
					
						
							|  |  |  |             || PyUnicode_CHECK_INTERNED(op) == SSTATE_INTERNED_IMMORTAL_STATIC); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2024-12-11 17:37:38 +00:00
										 |  |  |     // Check if already immortal to avoid degrading from static immortal to plain immortal
 | 
					
						
							|  |  |  |     if (_Py_IsImmortal(op)) { | 
					
						
							|  |  |  |         return; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2024-03-20 08:54:42 +00:00
										 |  |  | #ifdef Py_GIL_DISABLED
 | 
					
						
							|  |  |  |     op->ob_tid = _Py_UNOWNED_TID; | 
					
						
							|  |  |  |     op->ob_ref_local = _Py_IMMORTAL_REFCNT_LOCAL; | 
					
						
							|  |  |  |     op->ob_ref_shared = 0; | 
					
						
							| 
									
										
										
										
											2025-02-13 09:01:43 -08:00
										 |  |  |     _Py_atomic_or_uint8(&op->ob_gc_bits, _PyGC_BITS_DEFERRED); | 
					
						
							| 
									
										
										
										
											2025-03-10 14:06:56 +00:00
										 |  |  | #elif SIZEOF_VOID_P > 4
 | 
					
						
							|  |  |  |     op->ob_flags = _Py_IMMORTAL_FLAGS; | 
					
						
							|  |  |  |     op->ob_refcnt = _Py_IMMORTAL_INITIAL_REFCNT; | 
					
						
							| 
									
										
										
										
											2024-03-20 08:54:42 +00:00
										 |  |  | #else
 | 
					
						
							| 
									
										
										
										
											2024-10-10 18:19:08 +01:00
										 |  |  |     op->ob_refcnt = _Py_IMMORTAL_INITIAL_REFCNT; | 
					
						
							| 
									
										
										
										
											2024-03-20 08:54:42 +00:00
										 |  |  | #endif
 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void | 
					
						
							|  |  |  | _Py_SetImmortal(PyObject *op) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     if (PyObject_IS_GC(op) && _PyObject_GC_IS_TRACKED(op)) { | 
					
						
							|  |  |  |         _PyObject_GC_UNTRACK(op); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     _Py_SetImmortalUntracked(op); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-12 13:36:20 -04:00
										 |  |  | void | 
					
						
							|  |  |  | _PyObject_SetDeferredRefcount(PyObject *op) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | #ifdef Py_GIL_DISABLED
 | 
					
						
							|  |  |  |     assert(PyType_IS_GC(Py_TYPE(op))); | 
					
						
							|  |  |  |     assert(_Py_IsOwnedByCurrentThread(op)); | 
					
						
							|  |  |  |     assert(op->ob_ref_shared == 0); | 
					
						
							| 
									
										
										
										
											2024-05-08 13:03:39 -07:00
										 |  |  |     _PyObject_SET_GC_BITS(op, _PyGC_BITS_DEFERRED); | 
					
						
							| 
									
										
										
										
											2024-08-06 14:36:57 -04:00
										 |  |  |     op->ob_ref_shared = _Py_REF_SHARED(_Py_REF_DEFERRED, 0); | 
					
						
							| 
									
										
										
										
											2024-04-12 13:36:20 -04:00
										 |  |  | #endif
 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-13 08:27:16 -05:00
										 |  |  | int | 
					
						
							|  |  |  | PyUnstable_Object_EnableDeferredRefcount(PyObject *op) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | #ifdef Py_GIL_DISABLED
 | 
					
						
							|  |  |  |     if (!PyType_IS_GC(Py_TYPE(op))) { | 
					
						
							|  |  |  |         // Deferred reference counting doesn't work
 | 
					
						
							|  |  |  |         // on untracked types.
 | 
					
						
							|  |  |  |         return 0; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     uint8_t bits = _Py_atomic_load_uint8(&op->ob_gc_bits); | 
					
						
							|  |  |  |     if ((bits & _PyGC_BITS_DEFERRED) != 0) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         // Nothing to do.
 | 
					
						
							|  |  |  |         return 0; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (_Py_atomic_compare_exchange_uint8(&op->ob_gc_bits, &bits, bits | _PyGC_BITS_DEFERRED) == 0) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         // Someone beat us to it!
 | 
					
						
							|  |  |  |         return 0; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     _Py_atomic_add_ssize(&op->ob_ref_shared, _Py_REF_SHARED(_Py_REF_DEFERRED, 0)); | 
					
						
							|  |  |  |     return 1; | 
					
						
							|  |  |  | #else
 | 
					
						
							|  |  |  |     return 0; | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-05-02 09:24:57 -04:00
										 |  |  | int | 
					
						
							|  |  |  | PyUnstable_Object_IsUniqueReferencedTemporary(PyObject *op) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     if (!_PyObject_IsUniquelyReferenced(op)) { | 
					
						
							|  |  |  |         return 0; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     _PyInterpreterFrame *frame = _PyEval_GetFrame(); | 
					
						
							|  |  |  |     if (frame == NULL) { | 
					
						
							|  |  |  |         return 0; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     _PyStackRef *base = _PyFrame_Stackbase(frame); | 
					
						
							|  |  |  |     _PyStackRef *stackpointer = frame->stackpointer; | 
					
						
							|  |  |  |     while (stackpointer > base) { | 
					
						
							|  |  |  |         stackpointer--; | 
					
						
							|  |  |  |         if (op == PyStackRef_AsPyObjectBorrow(*stackpointer)) { | 
					
						
							|  |  |  |             return PyStackRef_IsHeapSafe(*stackpointer); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-01-28 14:32:27 -05:00
										 |  |  | int | 
					
						
							|  |  |  | PyUnstable_TryIncRef(PyObject *op) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     return _Py_TryIncref(op); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void | 
					
						
							|  |  |  | PyUnstable_EnableTryIncRef(PyObject *op) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | #ifdef Py_GIL_DISABLED
 | 
					
						
							|  |  |  |     _PyObject_SetMaybeWeakref(op); | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-05 22:12:26 -05:00
										 |  |  | void | 
					
						
							|  |  |  | _Py_ResurrectReference(PyObject *op) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | #ifdef Py_TRACE_REFS
 | 
					
						
							|  |  |  |     _Py_AddToAllObjects(op); | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1996-08-12 21:32:12 +00:00
										 |  |  | void | 
					
						
							| 
									
										
										
										
											2013-08-13 20:18:52 +02:00
										 |  |  | _Py_ForgetReference(PyObject *op) | 
					
						
							| 
									
										
										
										
											1990-10-14 12:07:46 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2025-02-25 12:03:28 -05:00
										 |  |  | #ifdef Py_TRACE_REFS
 | 
					
						
							| 
									
										
										
										
											2020-02-07 00:38:59 +01:00
										 |  |  |     if (Py_REFCNT(op) < 0) { | 
					
						
							| 
									
										
										
										
											2020-01-30 09:01:07 +01:00
										 |  |  |         _PyObject_ASSERT_FAILED_MSG(op, "negative refcnt"); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-31 18:33:34 +02:00
										 |  |  |     PyInterpreterState *interp = _PyInterpreterState_GET(); | 
					
						
							| 
									
										
										
										
											2020-01-30 09:01:07 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1992-09-03 20:32:55 +00:00
										 |  |  | #ifdef SLOW_UNREF_CHECK
 | 
					
						
							| 
									
										
										
										
											2023-08-31 18:33:34 +02:00
										 |  |  |     if (!_PyRefchain_Get(interp, op)) { | 
					
						
							| 
									
										
										
										
											2020-01-30 09:01:07 +01:00
										 |  |  |         /* Not found */ | 
					
						
							|  |  |  |         _PyObject_ASSERT_FAILED_MSG(op, | 
					
						
							|  |  |  |                                     "object not found in the objects list"); | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											1992-09-03 20:32:55 +00:00
										 |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2020-01-30 09:01:07 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-31 18:33:34 +02:00
										 |  |  |     _PyRefchain_Remove(interp, op); | 
					
						
							| 
									
										
										
										
											2025-02-25 12:03:28 -05:00
										 |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2023-08-31 18:33:34 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-02-25 12:03:28 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | #ifdef Py_TRACE_REFS
 | 
					
						
							| 
									
										
										
										
											2023-08-31 18:33:34 +02:00
										 |  |  | static int | 
					
						
							|  |  |  | _Py_PrintReference(_Py_hashtable_t *ht, | 
					
						
							|  |  |  |                    const void *key, const void *value, | 
					
						
							|  |  |  |                    void *user_data) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     PyObject *op = (PyObject*)key; | 
					
						
							|  |  |  |     FILE *fp = (FILE *)user_data; | 
					
						
							|  |  |  |     fprintf(fp, "%p [%zd] ", (void *)op, Py_REFCNT(op)); | 
					
						
							|  |  |  |     if (PyObject_Print(op, fp, 0) != 0) { | 
					
						
							|  |  |  |         PyErr_Clear(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     putc('\n', fp); | 
					
						
							|  |  |  |     return 0; | 
					
						
							| 
									
										
										
										
											1990-12-20 15:06:42 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-31 18:33:34 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2003-04-17 19:52:29 +00:00
										 |  |  | /* Print all live objects.  Because PyObject_Print is called, the
 | 
					
						
							|  |  |  |  * interpreter must be in a healthy state. | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											1996-08-12 21:32:12 +00:00
										 |  |  | void | 
					
						
							| 
									
										
										
										
											2023-08-03 13:51:08 -06:00
										 |  |  | _Py_PrintReferences(PyInterpreterState *interp, FILE *fp) | 
					
						
							| 
									
										
										
										
											1990-10-14 12:07:46 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2023-08-03 13:51:08 -06:00
										 |  |  |     if (interp == NULL) { | 
					
						
							|  |  |  |         interp = _PyInterpreterState_Main(); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     fprintf(fp, "Remaining objects:\n"); | 
					
						
							| 
									
										
										
										
											2023-08-31 18:33:34 +02:00
										 |  |  |     _Py_hashtable_foreach(REFCHAIN(interp), _Py_PrintReference, fp); | 
					
						
							| 
									
										
										
										
											1990-10-14 12:07:46 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-31 18:33:34 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | static int | 
					
						
							|  |  |  | _Py_PrintReferenceAddress(_Py_hashtable_t *ht, | 
					
						
							|  |  |  |                           const void *key, const void *value, | 
					
						
							|  |  |  |                           void *user_data) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     PyObject *op = (PyObject*)key; | 
					
						
							|  |  |  |     FILE *fp = (FILE *)user_data; | 
					
						
							|  |  |  |     fprintf(fp, "%p [%zd] %s\n", | 
					
						
							|  |  |  |             (void *)op, Py_REFCNT(op), Py_TYPE(op)->tp_name); | 
					
						
							|  |  |  |     return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2003-04-17 19:52:29 +00:00
										 |  |  | /* Print the addresses of all live objects.  Unlike _Py_PrintReferences, this
 | 
					
						
							|  |  |  |  * doesn't make any calls to the Python C API, so is always safe to call. | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2023-08-03 13:51:08 -06:00
										 |  |  | // XXX This function is not safe to use if the interpreter has been
 | 
					
						
							|  |  |  | // freed or is in an unhealthy state (e.g. late in finalization).
 | 
					
						
							|  |  |  | // The call in Py_FinalizeEx() is okay since the main interpreter
 | 
					
						
							|  |  |  | // is statically allocated.
 | 
					
						
							| 
									
										
										
										
											2003-04-17 19:52:29 +00:00
										 |  |  | void | 
					
						
							| 
									
										
										
										
											2023-08-03 13:51:08 -06:00
										 |  |  | _Py_PrintReferenceAddresses(PyInterpreterState *interp, FILE *fp) | 
					
						
							| 
									
										
										
										
											2003-04-17 19:52:29 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     fprintf(fp, "Remaining object addresses:\n"); | 
					
						
							| 
									
										
										
										
											2023-08-31 18:33:34 +02:00
										 |  |  |     _Py_hashtable_foreach(REFCHAIN(interp), _Py_PrintReferenceAddress, fp); | 
					
						
							| 
									
										
										
										
											2003-04-17 19:52:29 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-31 18:33:34 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | typedef struct { | 
					
						
							|  |  |  |     PyObject *self; | 
					
						
							|  |  |  |     PyObject *args; | 
					
						
							|  |  |  |     PyObject *list; | 
					
						
							|  |  |  |     PyObject *type; | 
					
						
							|  |  |  |     Py_ssize_t limit; | 
					
						
							|  |  |  | } _Py_GetObjectsData; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | enum { | 
					
						
							|  |  |  |     _PY_GETOBJECTS_IGNORE = 0, | 
					
						
							|  |  |  |     _PY_GETOBJECTS_ERROR = 1, | 
					
						
							|  |  |  |     _PY_GETOBJECTS_STOP = 2, | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static int | 
					
						
							|  |  |  | _Py_GetObject(_Py_hashtable_t *ht, | 
					
						
							|  |  |  |               const void *key, const void *value, | 
					
						
							|  |  |  |               void *user_data) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     PyObject *op = (PyObject *)key; | 
					
						
							|  |  |  |     _Py_GetObjectsData *data = user_data; | 
					
						
							|  |  |  |     if (data->limit > 0) { | 
					
						
							|  |  |  |         if (PyList_GET_SIZE(data->list) >= data->limit) { | 
					
						
							|  |  |  |             return _PY_GETOBJECTS_STOP; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (op == data->self) { | 
					
						
							|  |  |  |         return _PY_GETOBJECTS_IGNORE; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     if (op == data->args) { | 
					
						
							|  |  |  |         return _PY_GETOBJECTS_IGNORE; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     if (op == data->list) { | 
					
						
							|  |  |  |         return _PY_GETOBJECTS_IGNORE; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     if (data->type != NULL) { | 
					
						
							|  |  |  |         if (op == data->type) { | 
					
						
							|  |  |  |             return _PY_GETOBJECTS_IGNORE; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         if (!Py_IS_TYPE(op, (PyTypeObject *)data->type)) { | 
					
						
							|  |  |  |             return _PY_GETOBJECTS_IGNORE; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (PyList_Append(data->list, op) < 0) { | 
					
						
							|  |  |  |         return _PY_GETOBJECTS_ERROR; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-03 13:51:08 -06:00
										 |  |  | /* The implementation of sys.getobjects(). */ | 
					
						
							| 
									
										
										
										
											1995-08-29 09:18:14 +00:00
										 |  |  | PyObject * | 
					
						
							| 
									
										
										
										
											2000-07-09 15:48:49 +00:00
										 |  |  | _Py_GetObjects(PyObject *self, PyObject *args) | 
					
						
							| 
									
										
										
										
											1995-08-29 09:18:14 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2023-08-31 18:33:34 +02:00
										 |  |  |     Py_ssize_t limit; | 
					
						
							|  |  |  |     PyObject *type = NULL; | 
					
						
							|  |  |  |     if (!PyArg_ParseTuple(args, "n|O", &limit, &type)) { | 
					
						
							|  |  |  |         return NULL; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-31 18:33:34 +02:00
										 |  |  |     PyObject *list = PyList_New(0); | 
					
						
							|  |  |  |     if (list == NULL) { | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |         return NULL; | 
					
						
							| 
									
										
										
										
											2023-08-31 18:33:34 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     _Py_GetObjectsData data = { | 
					
						
							|  |  |  |         .self = self, | 
					
						
							|  |  |  |         .args = args, | 
					
						
							|  |  |  |         .list = list, | 
					
						
							|  |  |  |         .type = type, | 
					
						
							|  |  |  |         .limit = limit, | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  |     PyInterpreterState *interp = _PyInterpreterState_GET(); | 
					
						
							|  |  |  |     int res = _Py_hashtable_foreach(REFCHAIN(interp), _Py_GetObject, &data); | 
					
						
							|  |  |  |     if (res == _PY_GETOBJECTS_ERROR) { | 
					
						
							|  |  |  |         Py_DECREF(list); | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |         return NULL; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2023-08-31 18:33:34 +02:00
										 |  |  |     return list; | 
					
						
							| 
									
										
										
										
											1995-08-29 09:18:14 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-03 13:51:08 -06:00
										 |  |  | #undef REFCHAIN
 | 
					
						
							| 
									
										
										
										
											2023-08-31 18:33:34 +02:00
										 |  |  | #undef REFCHAIN_VALUE
 | 
					
						
							| 
									
										
										
										
											2023-08-03 13:51:08 -06:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-07 13:14:56 -06:00
										 |  |  | #endif  /* Py_TRACE_REFS */
 | 
					
						
							| 
									
										
										
										
											1996-01-12 01:24:09 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-05-05 22:31:58 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1996-05-22 16:34:47 +00:00
										 |  |  | /* Hack to force loading of abstract.o */ | 
					
						
							| 
									
										
										
										
											2006-02-15 17:27:45 +00:00
										 |  |  | Py_ssize_t (*_Py_abstract_hack)(PyObject *) = PyObject_Size; | 
					
						
							| 
									
										
										
										
											1997-08-05 02:04:34 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-06-22 14:55:41 -04:00
										 |  |  | void | 
					
						
							|  |  |  | _PyObject_DebugTypeStats(FILE *out) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     _PyDict_DebugMallocStats(out); | 
					
						
							|  |  |  |     _PyFloat_DebugMallocStats(out); | 
					
						
							|  |  |  |     _PyList_DebugMallocStats(out); | 
					
						
							|  |  |  |     _PyTuple_DebugMallocStats(out); | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2000-05-03 23:44:39 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1998-04-10 22:32:46 +00:00
										 |  |  | /* These methods are used to control infinite recursion in repr, str, print,
 | 
					
						
							|  |  |  |    etc.  Container objects that may recursively contain themselves, | 
					
						
							| 
									
										
										
										
											2016-05-29 04:13:35 +00:00
										 |  |  |    e.g. builtin dictionaries and lists, should use Py_ReprEnter() and | 
					
						
							| 
									
										
										
										
											1998-04-10 22:32:46 +00:00
										 |  |  |    Py_ReprLeave() to avoid infinite recursion. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    Py_ReprEnter() returns 0 the first time it is called for a particular | 
					
						
							|  |  |  |    object and 1 every time thereafter.  It returns -1 if an exception | 
					
						
							|  |  |  |    occurred.  Py_ReprLeave() has no return value. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    See dictobject.c and listobject.c for examples of use. | 
					
						
							|  |  |  | */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | int | 
					
						
							| 
									
										
										
										
											2000-07-09 15:48:49 +00:00
										 |  |  | Py_ReprEnter(PyObject *obj) | 
					
						
							| 
									
										
										
										
											1998-04-10 22:32:46 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     PyObject *dict; | 
					
						
							|  |  |  |     PyObject *list; | 
					
						
							|  |  |  |     Py_ssize_t i; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     dict = PyThreadState_GetDict(); | 
					
						
							| 
									
										
										
										
											2014-03-31 22:04:38 +02:00
										 |  |  |     /* Ignore a missing thread-state, so that this function can be called
 | 
					
						
							|  |  |  |        early on startup. */ | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     if (dict == NULL) | 
					
						
							|  |  |  |         return 0; | 
					
						
							| 
									
										
										
										
											2022-02-08 13:39:07 -07:00
										 |  |  |     list = PyDict_GetItemWithError(dict, &_Py_ID(Py_Repr)); | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     if (list == NULL) { | 
					
						
							| 
									
										
										
										
											2019-02-25 17:59:46 +02:00
										 |  |  |         if (PyErr_Occurred()) { | 
					
						
							|  |  |  |             return -1; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |         list = PyList_New(0); | 
					
						
							|  |  |  |         if (list == NULL) | 
					
						
							|  |  |  |             return -1; | 
					
						
							| 
									
										
										
										
											2022-02-08 13:39:07 -07:00
										 |  |  |         if (PyDict_SetItem(dict, &_Py_ID(Py_Repr), list) < 0) | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |             return -1; | 
					
						
							|  |  |  |         Py_DECREF(list); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     i = PyList_GET_SIZE(list); | 
					
						
							|  |  |  |     while (--i >= 0) { | 
					
						
							|  |  |  |         if (PyList_GET_ITEM(list, i) == obj) | 
					
						
							|  |  |  |             return 1; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2013-07-17 21:58:41 +02:00
										 |  |  |     if (PyList_Append(list, obj) < 0) | 
					
						
							|  |  |  |         return -1; | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     return 0; | 
					
						
							| 
									
										
										
										
											1998-04-10 22:32:46 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void | 
					
						
							| 
									
										
										
										
											2000-07-09 15:48:49 +00:00
										 |  |  | Py_ReprLeave(PyObject *obj) | 
					
						
							| 
									
										
										
										
											1998-04-10 22:32:46 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     PyObject *dict; | 
					
						
							|  |  |  |     PyObject *list; | 
					
						
							|  |  |  |     Py_ssize_t i; | 
					
						
							| 
									
										
										
										
											2013-07-16 22:24:44 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-08 17:03:18 +00:00
										 |  |  |     PyObject *exc = PyErr_GetRaisedException(); | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     dict = PyThreadState_GetDict(); | 
					
						
							|  |  |  |     if (dict == NULL) | 
					
						
							| 
									
										
										
										
											2013-07-16 22:24:44 +02:00
										 |  |  |         goto finally; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-08 13:39:07 -07:00
										 |  |  |     list = PyDict_GetItemWithError(dict, &_Py_ID(Py_Repr)); | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     if (list == NULL || !PyList_Check(list)) | 
					
						
							| 
									
										
										
										
											2013-07-16 22:24:44 +02:00
										 |  |  |         goto finally; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     i = PyList_GET_SIZE(list); | 
					
						
							|  |  |  |     /* Count backwards because we always expect obj to be list[-1] */ | 
					
						
							|  |  |  |     while (--i >= 0) { | 
					
						
							|  |  |  |         if (PyList_GET_ITEM(list, i) == obj) { | 
					
						
							|  |  |  |             PyList_SetSlice(list, i, i + 1, NULL); | 
					
						
							|  |  |  |             break; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2013-07-16 22:24:44 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | finally: | 
					
						
							|  |  |  |     /* ignore exceptions because there is no way to report them. */ | 
					
						
							| 
									
										
										
										
											2023-03-08 17:03:18 +00:00
										 |  |  |     PyErr_SetRaisedException(exc); | 
					
						
							| 
									
										
										
										
											1998-04-10 22:32:46 +00:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2000-03-13 16:01:29 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-07-07 05:13:56 +00:00
										 |  |  | /* Trashcan support. */ | 
					
						
							| 
									
										
										
										
											2000-04-24 15:40:53 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-23 15:51:47 +02:00
										 |  |  | /* Add op to the gcstate->trash_delete_later list.  Called when the current
 | 
					
						
							| 
									
										
										
										
											2025-06-24 09:49:38 +01:00
										 |  |  |  * call-stack depth gets large.  op must be a gc'ed object, with refcount 0. | 
					
						
							|  |  |  |  *  Py_DECREF must already have been called on it. | 
					
						
							| 
									
										
										
										
											2002-07-07 05:13:56 +00:00
										 |  |  |  */ | 
					
						
							| 
									
										
										
										
											2024-04-17 11:08:05 +01:00
										 |  |  | void | 
					
						
							|  |  |  | _PyTrash_thread_deposit_object(PyThreadState *tstate, PyObject *op) | 
					
						
							| 
									
										
										
										
											2012-09-06 00:59:49 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2020-02-07 00:38:59 +01:00
										 |  |  |     _PyObject_ASSERT(op, Py_REFCNT(op) == 0); | 
					
						
							| 
									
										
										
										
											2025-06-24 09:49:38 +01:00
										 |  |  |     PyTypeObject *tp = Py_TYPE(op); | 
					
						
							|  |  |  |     assert(tp->tp_flags & Py_TPFLAGS_HAVE_GC); | 
					
						
							|  |  |  |     int tracked = 0; | 
					
						
							|  |  |  |     if (tp->tp_is_gc == NULL || tp->tp_is_gc(op)) { | 
					
						
							|  |  |  |         tracked = _PyObject_GC_IS_TRACKED(op); | 
					
						
							|  |  |  |         if (tracked) { | 
					
						
							|  |  |  |             _PyObject_GC_UNTRACK(op); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     uintptr_t tagged_ptr = ((uintptr_t)tstate->delete_later) | tracked; | 
					
						
							| 
									
										
										
										
											2024-02-01 15:29:19 -05:00
										 |  |  | #ifdef Py_GIL_DISABLED
 | 
					
						
							| 
									
										
										
										
											2025-06-24 09:49:38 +01:00
										 |  |  |     op->ob_tid = tagged_ptr; | 
					
						
							| 
									
										
										
										
											2024-02-01 15:29:19 -05:00
										 |  |  | #else
 | 
					
						
							| 
									
										
										
										
											2025-06-24 09:49:38 +01:00
										 |  |  |     _Py_AS_GC(op)->_gc_next = tagged_ptr; | 
					
						
							| 
									
										
										
										
											2024-02-01 15:29:19 -05:00
										 |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2024-04-17 11:08:05 +01:00
										 |  |  |     tstate->delete_later = op; | 
					
						
							| 
									
										
										
										
											2012-09-06 00:59:49 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-23 15:51:47 +02:00
										 |  |  | /* Deallocate all the objects in the gcstate->trash_delete_later list.
 | 
					
						
							|  |  |  |  * Called when the call-stack unwinds again. */ | 
					
						
							| 
									
										
										
										
											2024-04-17 11:08:05 +01:00
										 |  |  | void | 
					
						
							|  |  |  | _PyTrash_thread_destroy_chain(PyThreadState *tstate) | 
					
						
							| 
									
										
										
										
											2012-09-06 00:59:49 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2024-04-17 11:08:05 +01:00
										 |  |  |     while (tstate->delete_later) { | 
					
						
							|  |  |  |         PyObject *op = tstate->delete_later; | 
					
						
							| 
									
										
										
										
											2012-09-06 00:59:49 +02:00
										 |  |  |         destructor dealloc = Py_TYPE(op)->tp_dealloc; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-02-01 15:29:19 -05:00
										 |  |  | #ifdef Py_GIL_DISABLED
 | 
					
						
							| 
									
										
										
										
											2025-06-24 09:49:38 +01:00
										 |  |  |         uintptr_t tagged_ptr = op->ob_tid; | 
					
						
							| 
									
										
										
										
											2024-02-01 15:29:19 -05:00
										 |  |  |         op->ob_tid = 0; | 
					
						
							| 
									
										
										
										
											2024-07-15 17:50:10 -04:00
										 |  |  |         _Py_atomic_store_ssize_relaxed(&op->ob_ref_shared, _Py_REF_MERGED); | 
					
						
							| 
									
										
										
										
											2024-02-01 15:29:19 -05:00
										 |  |  | #else
 | 
					
						
							| 
									
										
										
										
											2025-06-24 09:49:38 +01:00
										 |  |  |         uintptr_t tagged_ptr = _Py_AS_GC(op)->_gc_next; | 
					
						
							|  |  |  |         _Py_AS_GC(op)->_gc_next = 0; | 
					
						
							| 
									
										
										
										
											2024-02-01 15:29:19 -05:00
										 |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2025-06-24 09:49:38 +01:00
										 |  |  |         tstate->delete_later = (PyObject *)(tagged_ptr & ~1); | 
					
						
							|  |  |  |         if (tagged_ptr & 1) { | 
					
						
							|  |  |  |             _PyObject_GC_TRACK(op); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2012-09-06 00:59:49 +02:00
										 |  |  |         /* Call the deallocator directly.  This used to try to
 | 
					
						
							|  |  |  |          * fool Py_DECREF into calling it indirectly, but | 
					
						
							|  |  |  |          * Py_DECREF was already called on this object, and in | 
					
						
							|  |  |  |          * assorted non-release builds calling Py_DECREF again ends | 
					
						
							|  |  |  |          * up distorting allocation statistics. | 
					
						
							|  |  |  |          */ | 
					
						
							| 
									
										
										
										
											2020-02-07 00:38:59 +01:00
										 |  |  |         _PyObject_ASSERT(op, Py_REFCNT(op) == 0); | 
					
						
							| 
									
										
										
										
											2012-09-06 00:59:49 +02:00
										 |  |  |         (*dealloc)(op); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-30 13:09:11 +01:00
										 |  |  | void _Py_NO_RETURN | 
					
						
							| 
									
										
										
										
											2018-11-21 23:53:44 +01:00
										 |  |  | _PyObject_AssertFailed(PyObject *obj, const char *expr, const char *msg, | 
					
						
							| 
									
										
										
										
											2018-10-25 17:31:10 +02:00
										 |  |  |                        const char *file, int line, const char *function) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2018-11-21 23:53:44 +01:00
										 |  |  |     fprintf(stderr, "%s:%d: ", file, line); | 
					
						
							|  |  |  |     if (function) { | 
					
						
							|  |  |  |         fprintf(stderr, "%s: ", function); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-10-25 17:31:10 +02:00
										 |  |  |     fflush(stderr); | 
					
						
							| 
									
										
										
										
											2019-10-07 18:42:01 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-21 23:53:44 +01:00
										 |  |  |     if (expr) { | 
					
						
							|  |  |  |         fprintf(stderr, "Assertion \"%s\" failed", expr); | 
					
						
							| 
									
										
										
										
											2018-10-25 17:31:10 +02:00
										 |  |  |     } | 
					
						
							|  |  |  |     else { | 
					
						
							| 
									
										
										
										
											2018-11-21 23:53:44 +01:00
										 |  |  |         fprintf(stderr, "Assertion failed"); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     fflush(stderr); | 
					
						
							| 
									
										
										
										
											2019-10-07 18:42:01 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-21 23:53:44 +01:00
										 |  |  |     if (msg) { | 
					
						
							|  |  |  |         fprintf(stderr, ": %s", msg); | 
					
						
							| 
									
										
										
										
											2018-10-25 17:31:10 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-11-21 23:53:44 +01:00
										 |  |  |     fprintf(stderr, "\n"); | 
					
						
							| 
									
										
										
										
											2018-10-25 17:31:10 +02:00
										 |  |  |     fflush(stderr); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-07 18:42:01 +02:00
										 |  |  |     if (_PyObject_IsFreed(obj)) { | 
					
						
							| 
									
										
										
										
											2018-10-25 17:31:10 +02:00
										 |  |  |         /* It seems like the object memory has been freed:
 | 
					
						
							|  |  |  |            don't access it to prevent a segmentation fault. */ | 
					
						
							| 
									
										
										
										
											2019-09-17 23:36:28 +02:00
										 |  |  |         fprintf(stderr, "<object at %p is freed>\n", obj); | 
					
						
							| 
									
										
										
										
											2019-10-07 18:42:01 +02:00
										 |  |  |         fflush(stderr); | 
					
						
							| 
									
										
										
										
											2018-10-25 17:31:10 +02:00
										 |  |  |     } | 
					
						
							|  |  |  |     else { | 
					
						
							| 
									
										
										
										
											2019-05-06 14:57:17 -04:00
										 |  |  |         /* Display the traceback where the object has been allocated.
 | 
					
						
							| 
									
										
										
										
											2018-10-25 17:31:10 +02:00
										 |  |  |            Do it before dumping repr(obj), since repr() is more likely | 
					
						
							|  |  |  |            to crash than dumping the traceback. */ | 
					
						
							|  |  |  |         PyTypeObject *type = Py_TYPE(obj); | 
					
						
							| 
									
										
										
										
											2023-02-10 08:30:03 +09:00
										 |  |  |         const size_t presize = _PyType_PreHeaderSize(type); | 
					
						
							|  |  |  |         void *ptr = (void *)((char *)obj - presize); | 
					
						
							| 
									
										
										
										
											2018-10-25 17:31:10 +02:00
										 |  |  |         _PyMem_DumpTraceback(fileno(stderr), ptr); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         /* This might succeed or fail, but we're about to abort, so at least
 | 
					
						
							|  |  |  |            try to provide any extra info we can: */ | 
					
						
							|  |  |  |         _PyObject_Dump(obj); | 
					
						
							| 
									
										
										
										
											2019-10-07 23:44:05 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         fprintf(stderr, "\n"); | 
					
						
							|  |  |  |         fflush(stderr); | 
					
						
							| 
									
										
										
										
											2018-10-25 17:31:10 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     Py_FatalError("_PyObject_AssertFailed"); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-30 14:48:26 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-04-30 11:37:53 +01:00
										 |  |  | /*
 | 
					
						
							|  |  |  | When deallocating a container object, it's possible to trigger an unbounded | 
					
						
							|  |  |  | chain of deallocations, as each Py_DECREF in turn drops the refcount on "the | 
					
						
							|  |  |  | next" object in the chain to 0.  This can easily lead to stack overflows. | 
					
						
							|  |  |  | To avoid that, if the C stack is nearing its limit, instead of calling | 
					
						
							|  |  |  | dealloc on the object, it is added to a queue to be freed later when the | 
					
						
							|  |  |  | stack is shallower */ | 
					
						
							| 
									
										
										
										
											2010-12-03 20:14:31 +00:00
										 |  |  | void | 
					
						
							|  |  |  | _Py_Dealloc(PyObject *op) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2022-04-21 23:04:01 +02:00
										 |  |  |     PyTypeObject *type = Py_TYPE(op); | 
					
						
							| 
									
										
										
										
											2025-06-24 09:49:38 +01:00
										 |  |  |     unsigned long gc_flag = type->tp_flags & Py_TPFLAGS_HAVE_GC; | 
					
						
							| 
									
										
										
										
											2022-04-21 23:04:01 +02:00
										 |  |  |     destructor dealloc = type->tp_dealloc; | 
					
						
							|  |  |  |     PyThreadState *tstate = _PyThreadState_GET(); | 
					
						
							| 
									
										
										
										
											2025-04-30 11:37:53 +01:00
										 |  |  |     intptr_t margin = _Py_RecursionLimit_GetMargin(tstate); | 
					
						
							| 
									
										
										
										
											2025-06-24 09:49:38 +01:00
										 |  |  |     if (margin < 2 && gc_flag) { | 
					
						
							| 
									
										
										
										
											2025-04-30 11:37:53 +01:00
										 |  |  |         _PyTrash_thread_deposit_object(tstate, (PyObject *)op); | 
					
						
							|  |  |  |         return; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | #ifdef Py_DEBUG
 | 
					
						
							| 
									
										
										
										
											2025-03-05 14:00:42 +00:00
										 |  |  | #if !defined(Py_GIL_DISABLED) && !defined(Py_STACKREF_DEBUG)
 | 
					
						
							| 
									
										
										
										
											2025-02-12 17:44:59 +00:00
										 |  |  |     /* This assertion doesn't hold for the free-threading build, as
 | 
					
						
							|  |  |  |      * PyStackRef_CLOSE_SPECIALIZED is not implemented */ | 
					
						
							|  |  |  |     assert(tstate->current_frame == NULL || tstate->current_frame->stackpointer != NULL); | 
					
						
							|  |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2023-02-08 09:31:12 +00:00
										 |  |  |     PyObject *old_exc = tstate != NULL ? tstate->current_exception : NULL; | 
					
						
							| 
									
										
										
										
											2022-04-21 23:04:01 +02:00
										 |  |  |     // Keep the old exception type alive to prevent undefined behavior
 | 
					
						
							|  |  |  |     // on (tstate->curexc_type != old_exc_type) below
 | 
					
						
							| 
									
										
										
										
											2023-02-08 09:31:12 +00:00
										 |  |  |     Py_XINCREF(old_exc); | 
					
						
							| 
									
										
										
										
											2022-04-21 23:04:01 +02:00
										 |  |  |     // Make sure that type->tp_name remains valid
 | 
					
						
							|  |  |  |     Py_INCREF(type); | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-30 14:48:26 +01:00
										 |  |  | #ifdef Py_TRACE_REFS
 | 
					
						
							|  |  |  |     _Py_ForgetReference(op); | 
					
						
							| 
									
										
										
										
											2010-12-03 20:14:31 +00:00
										 |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2024-11-19 10:35:17 +00:00
										 |  |  |     _PyReftracerTrack(op, PyRefTracer_DESTROY); | 
					
						
							| 
									
										
										
										
											2018-10-30 14:48:26 +01:00
										 |  |  |     (*dealloc)(op); | 
					
						
							| 
									
										
										
										
											2022-04-21 23:04:01 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | #ifdef Py_DEBUG
 | 
					
						
							|  |  |  |     // gh-89373: The tp_dealloc function must leave the current exception
 | 
					
						
							|  |  |  |     // unchanged.
 | 
					
						
							| 
									
										
										
										
											2023-02-08 09:31:12 +00:00
										 |  |  |     if (tstate != NULL && tstate->current_exception != old_exc) { | 
					
						
							| 
									
										
										
										
											2022-04-21 23:04:01 +02:00
										 |  |  |         const char *err; | 
					
						
							| 
									
										
										
										
											2023-02-08 09:31:12 +00:00
										 |  |  |         if (old_exc == NULL) { | 
					
						
							| 
									
										
										
										
											2022-04-21 23:04:01 +02:00
										 |  |  |             err = "Deallocator of type '%s' raised an exception"; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2023-02-08 09:31:12 +00:00
										 |  |  |         else if (tstate->current_exception == NULL) { | 
					
						
							| 
									
										
										
										
											2022-04-21 23:04:01 +02:00
										 |  |  |             err = "Deallocator of type '%s' cleared the current exception"; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         else { | 
					
						
							|  |  |  |             // It can happen if dealloc() normalized the current exception.
 | 
					
						
							|  |  |  |             // A deallocator function must not change the current exception,
 | 
					
						
							|  |  |  |             // not even normalize it.
 | 
					
						
							|  |  |  |             err = "Deallocator of type '%s' overrode the current exception"; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         _Py_FatalErrorFormat(__func__, err, type->tp_name); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2023-02-08 09:31:12 +00:00
										 |  |  |     Py_XDECREF(old_exc); | 
					
						
							| 
									
										
										
										
											2022-04-21 23:04:01 +02:00
										 |  |  |     Py_DECREF(type); | 
					
						
							|  |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2025-06-24 09:49:38 +01:00
										 |  |  |     if (tstate->delete_later && margin >= 4 && gc_flag) { | 
					
						
							| 
									
										
										
										
											2025-04-30 11:37:53 +01:00
										 |  |  |         _PyTrash_thread_destroy_chain(tstate); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-10-30 14:48:26 +01:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2010-12-03 20:14:31 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-06 14:07:02 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | PyObject ** | 
					
						
							|  |  |  | PyObject_GET_WEAKREFS_LISTPTR(PyObject *op) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     return _PyObject_GET_WEAKREFS_LISTPTR(op); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-05 15:02:12 +01:00
										 |  |  | #undef Py_NewRef
 | 
					
						
							|  |  |  | #undef Py_XNewRef
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Export Py_NewRef() and Py_XNewRef() as regular functions for the stable ABI.
 | 
					
						
							|  |  |  | PyObject* | 
					
						
							|  |  |  | Py_NewRef(PyObject *obj) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     return _Py_NewRef(obj); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | PyObject* | 
					
						
							|  |  |  | Py_XNewRef(PyObject *obj) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     return _Py_XNewRef(obj); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-11 00:17:39 +02:00
										 |  |  | #undef Py_Is
 | 
					
						
							|  |  |  | #undef Py_IsNone
 | 
					
						
							|  |  |  | #undef Py_IsTrue
 | 
					
						
							|  |  |  | #undef Py_IsFalse
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Export Py_Is(), Py_IsNone(), Py_IsTrue(), Py_IsFalse() as regular functions
 | 
					
						
							|  |  |  | // for the stable ABI.
 | 
					
						
							|  |  |  | int Py_Is(PyObject *x, PyObject *y) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     return (x == y); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | int Py_IsNone(PyObject *x) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     return Py_Is(x, Py_None); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | int Py_IsTrue(PyObject *x) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     return Py_Is(x, Py_True); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | int Py_IsFalse(PyObject *x) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     return Py_Is(x, Py_False); | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2023-11-03 18:18:57 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Py_SET_REFCNT() implementation for stable ABI
 | 
					
						
							|  |  |  | void | 
					
						
							|  |  |  | _Py_SetRefcnt(PyObject *ob, Py_ssize_t refcnt) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     Py_SET_REFCNT(ob, refcnt); | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2024-03-21 17:07:00 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-05-02 19:30:00 +02:00
										 |  |  | int PyRefTracer_SetTracer(PyRefTracer tracer, void *data) { | 
					
						
							| 
									
										
										
										
											2025-01-20 06:34:35 -05:00
										 |  |  |     _Py_AssertHoldsTstate(); | 
					
						
							| 
									
										
										
										
											2024-05-02 19:30:00 +02:00
										 |  |  |     _PyRuntime.ref_tracer.tracer_func = tracer; | 
					
						
							|  |  |  |     _PyRuntime.ref_tracer.tracer_data = data; | 
					
						
							|  |  |  |     return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | PyRefTracer PyRefTracer_GetTracer(void** data) { | 
					
						
							| 
									
										
										
										
											2025-01-20 06:34:35 -05:00
										 |  |  |     _Py_AssertHoldsTstate(); | 
					
						
							| 
									
										
										
										
											2024-05-02 19:30:00 +02:00
										 |  |  |     if (data != NULL) { | 
					
						
							|  |  |  |         *data = _PyRuntime.ref_tracer.tracer_data; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return _PyRuntime.ref_tracer.tracer_func; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-21 17:07:00 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | static PyObject* constants[] = { | 
					
						
							|  |  |  |     &_Py_NoneStruct,                   // Py_CONSTANT_NONE
 | 
					
						
							|  |  |  |     (PyObject*)(&_Py_FalseStruct),     // Py_CONSTANT_FALSE
 | 
					
						
							|  |  |  |     (PyObject*)(&_Py_TrueStruct),      // Py_CONSTANT_TRUE
 | 
					
						
							|  |  |  |     &_Py_EllipsisObject,               // Py_CONSTANT_ELLIPSIS
 | 
					
						
							|  |  |  |     &_Py_NotImplementedStruct,         // Py_CONSTANT_NOT_IMPLEMENTED
 | 
					
						
							|  |  |  |     NULL,  // Py_CONSTANT_ZERO
 | 
					
						
							|  |  |  |     NULL,  // Py_CONSTANT_ONE
 | 
					
						
							|  |  |  |     NULL,  // Py_CONSTANT_EMPTY_STR
 | 
					
						
							|  |  |  |     NULL,  // Py_CONSTANT_EMPTY_BYTES
 | 
					
						
							|  |  |  |     NULL,  // Py_CONSTANT_EMPTY_TUPLE
 | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void | 
					
						
							|  |  |  | _Py_GetConstant_Init(void) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     constants[Py_CONSTANT_ZERO] = _PyLong_GetZero(); | 
					
						
							|  |  |  |     constants[Py_CONSTANT_ONE] = _PyLong_GetOne(); | 
					
						
							|  |  |  |     constants[Py_CONSTANT_EMPTY_STR] = PyUnicode_New(0, 0); | 
					
						
							|  |  |  |     constants[Py_CONSTANT_EMPTY_BYTES] = PyBytes_FromStringAndSize(NULL, 0); | 
					
						
							|  |  |  |     constants[Py_CONSTANT_EMPTY_TUPLE] = PyTuple_New(0); | 
					
						
							|  |  |  | #ifndef NDEBUG
 | 
					
						
							|  |  |  |     for (size_t i=0; i < Py_ARRAY_LENGTH(constants); i++) { | 
					
						
							|  |  |  |         assert(constants[i] != NULL); | 
					
						
							|  |  |  |         assert(_Py_IsImmortal(constants[i])); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | PyObject* | 
					
						
							|  |  |  | Py_GetConstant(unsigned int constant_id) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     if (constant_id < Py_ARRAY_LENGTH(constants)) { | 
					
						
							|  |  |  |         return constants[constant_id]; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     else { | 
					
						
							|  |  |  |         PyErr_BadInternalCall(); | 
					
						
							|  |  |  |         return NULL; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | PyObject* | 
					
						
							|  |  |  | Py_GetConstantBorrowed(unsigned int constant_id) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     // All constants are immortal
 | 
					
						
							|  |  |  |     return Py_GetConstant(constant_id); | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2024-06-18 16:28:48 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Py_TYPE() implementation for the stable ABI
 | 
					
						
							|  |  |  | #undef Py_TYPE
 | 
					
						
							| 
									
										
										
										
											2024-09-24 08:42:58 +02:00
										 |  |  | PyTypeObject* | 
					
						
							|  |  |  | Py_TYPE(PyObject *ob) | 
					
						
							| 
									
										
										
										
											2024-06-18 16:28:48 +02:00
										 |  |  | { | 
					
						
							|  |  |  |     return _Py_TYPE(ob); | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2024-09-24 08:42:58 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Py_REFCNT() implementation for the stable ABI
 | 
					
						
							|  |  |  | #undef Py_REFCNT
 | 
					
						
							|  |  |  | Py_ssize_t | 
					
						
							|  |  |  | Py_REFCNT(PyObject *ob) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     return _Py_REFCNT(ob); | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2025-01-27 08:36:33 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | int | 
					
						
							|  |  |  | PyUnstable_IsImmortal(PyObject *op) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     /* Checking a reference count requires a thread state */ | 
					
						
							|  |  |  |     _Py_AssertHoldsTstate(); | 
					
						
							|  |  |  |     assert(op != NULL); | 
					
						
							|  |  |  |     return _Py_IsImmortal(op); | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2025-05-05 15:01:20 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | int | 
					
						
							|  |  |  | PyUnstable_Object_IsUniquelyReferenced(PyObject *op) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     _Py_AssertHoldsTstate(); | 
					
						
							|  |  |  |     assert(op != NULL); | 
					
						
							|  |  |  |     return _PyObject_IsUniquelyReferenced(op); | 
					
						
							|  |  |  | } |