blob: ff816cd5b9a60508e59c41351195134743d7b09d [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001
Benjamin Peterson722954a2011-06-11 16:33:35 -05002/* Generic object operations; and implementation of None */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003
Guido van Rossumc0b618a1997-05-02 03:12:38 +00004#include "Python.h"
Victor Stinnere5014be2020-04-14 17:52:15 +02005#include "pycore_ceval.h" // _Py_EnterRecursiveCall()
Victor Stinnerbe434dc2019-11-05 00:51:22 +01006#include "pycore_context.h"
Victor Stinner331a6a52019-05-27 16:39:22 +02007#include "pycore_initconfig.h"
Victor Stinner0fc91ee2019-04-12 21:51:34 +02008#include "pycore_object.h"
Victor Stinnerbe434dc2019-11-05 00:51:22 +01009#include "pycore_pyerrors.h"
Victor Stinner7a1f6c22020-01-30 09:02:14 +010010#include "pycore_pylifecycle.h"
Victor Stinnere560f902020-04-14 18:30:41 +020011#include "pycore_pymem.h" // _PyMem_IsPtrFreed()
Victor Stinnere5014be2020-04-14 17:52:15 +020012#include "pycore_pystate.h" // _PyThreadState_GET()
Victor Stinnerdf5dc1c2021-04-08 00:47:55 +020013#include "pycore_symtable.h" // PySTEntry_Type
Miss Islington (bot)03aad302021-07-17 14:10:21 -070014#include "pycore_unionobject.h" // _PyUnion_Type
Benjamin Petersonfd838e62009-04-20 02:09:13 +000015#include "frameobject.h"
Eric Snowc11183c2019-03-15 16:35:46 -060016#include "interpreteridobject.h"
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000017
Victor Stinner3359cab2021-04-02 15:45:37 +020018#ifdef Py_LIMITED_API
19 // Prevent recursive call _Py_IncRef() <=> Py_INCREF()
20# error "Py_LIMITED_API macro must not be defined"
21#endif
22
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000023#ifdef __cplusplus
24extern "C" {
25#endif
26
Victor Stinner626bff82018-10-25 17:31:10 +020027/* Defined in tracemalloc.c */
28extern void _PyMem_DumpTraceback(int fd, const void *ptr);
29
Victor Stinnerbd303c12013-11-07 23:07:29 +010030_Py_IDENTIFIER(Py_Repr);
31_Py_IDENTIFIER(__bytes__);
32_Py_IDENTIFIER(__dir__);
33_Py_IDENTIFIER(__isabstractmethod__);
Victor Stinnerbd303c12013-11-07 23:07:29 +010034
Victor Stinner0fc91ee2019-04-12 21:51:34 +020035
36int
37_PyObject_CheckConsistency(PyObject *op, int check_content)
38{
Victor Stinner68762572019-10-07 18:42:01 +020039#define CHECK(expr) \
40 do { if (!(expr)) { _PyObject_ASSERT_FAILED_MSG(op, Py_STRINGIFY(expr)); } } while (0)
Victor Stinner0fc91ee2019-04-12 21:51:34 +020041
Victor Stinner68762572019-10-07 18:42:01 +020042 CHECK(!_PyObject_IsFreed(op));
43 CHECK(Py_REFCNT(op) >= 1);
44
Victor Stinner58ac7002020-02-07 03:04:21 +010045 _PyType_CheckConsistency(Py_TYPE(op));
Victor Stinner0fc91ee2019-04-12 21:51:34 +020046
47 if (PyUnicode_Check(op)) {
48 _PyUnicode_CheckConsistency(op, check_content);
49 }
50 else if (PyDict_Check(op)) {
51 _PyDict_CheckConsistency(op, check_content);
52 }
53 return 1;
Victor Stinner68762572019-10-07 18:42:01 +020054
55#undef CHECK
Victor Stinner0fc91ee2019-04-12 21:51:34 +020056}
57
58
Tim Peters34592512002-07-11 06:23:50 +000059#ifdef Py_REF_DEBUG
Neal Norwitz84632ee2006-03-04 20:00:59 +000060Py_ssize_t _Py_RefTotal;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000061
62Py_ssize_t
63_Py_GetRefTotal(void)
64{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000065 PyObject *o;
66 Py_ssize_t total = _Py_RefTotal;
Antoine Pitrou9d952542013-08-24 21:07:07 +020067 o = _PySet_Dummy;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000068 if (o != NULL)
Victor Stinnera93c51e2020-02-07 00:38:59 +010069 total -= Py_REFCNT(o);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000070 return total;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000071}
Nick Coghland6009512014-11-20 21:39:37 +100072
73void
74_PyDebug_PrintTotalRefs(void) {
Eric Snowdae02762017-09-14 00:35:58 -070075 fprintf(stderr,
Victor Stinnerd36cf5f2020-06-10 18:38:05 +020076 "[%zd refs, %zd blocks]\n",
Eric Snowdae02762017-09-14 00:35:58 -070077 _Py_GetRefTotal(), _Py_GetAllocatedBlocks());
Nick Coghland6009512014-11-20 21:39:37 +100078}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000079#endif /* Py_REF_DEBUG */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000080
Guido van Rossum3f5da241990-12-20 15:06:42 +000081/* Object allocation routines used by NEWOBJ and NEWVAROBJ macros.
82 These are used by the individual routines for object creation.
83 Do not call them otherwise, they do not initialize the object! */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000084
Tim Peters78be7992003-03-23 02:51:01 +000085#ifdef Py_TRACE_REFS
Tim Peters7571a0f2003-03-23 17:52:28 +000086/* Head of circular doubly-linked list of all objects. These are linked
87 * together via the _ob_prev and _ob_next members of a PyObject, which
88 * exist only in a Py_TRACE_REFS build.
89 */
Tim Peters78be7992003-03-23 02:51:01 +000090static PyObject refchain = {&refchain, &refchain};
Tim Peters36eb4df2003-03-23 03:33:13 +000091
Tim Peters7571a0f2003-03-23 17:52:28 +000092/* Insert op at the front of the list of all objects. If force is true,
93 * op is added even if _ob_prev and _ob_next are non-NULL already. If
94 * force is false amd _ob_prev or _ob_next are non-NULL, do nothing.
95 * force should be true if and only if op points to freshly allocated,
96 * uninitialized memory, or you've unlinked op from the list and are
Tim Peters51f8d382003-03-23 18:06:08 +000097 * relinking it into the front.
Tim Peters7571a0f2003-03-23 17:52:28 +000098 * Note that objects are normally added to the list via _Py_NewReference,
99 * which is called by PyObject_Init. Not all objects are initialized that
100 * way, though; exceptions include statically allocated type objects, and
101 * statically allocated singletons (like Py_True and Py_None).
102 */
Victor Stinner58f4e1a2020-02-05 18:24:33 +0100103void
Tim Peters7571a0f2003-03-23 17:52:28 +0000104_Py_AddToAllObjects(PyObject *op, int force)
Tim Peters36eb4df2003-03-23 03:33:13 +0000105{
Tim Peters7571a0f2003-03-23 17:52:28 +0000106#ifdef Py_DEBUG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000107 if (!force) {
108 /* If it's initialized memory, op must be in or out of
109 * the list unambiguously.
110 */
Victor Stinner24702042018-10-26 17:16:37 +0200111 _PyObject_ASSERT(op, (op->_ob_prev == NULL) == (op->_ob_next == NULL));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000112 }
Tim Peters78be7992003-03-23 02:51:01 +0000113#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000114 if (force || op->_ob_prev == NULL) {
115 op->_ob_next = refchain._ob_next;
116 op->_ob_prev = &refchain;
117 refchain._ob_next->_ob_prev = op;
118 refchain._ob_next = op;
119 }
Tim Peters7571a0f2003-03-23 17:52:28 +0000120}
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000121#endif /* Py_TRACE_REFS */
Tim Peters78be7992003-03-23 02:51:01 +0000122
Tim Peters7c321a82002-07-09 02:57:01 +0000123#ifdef Py_REF_DEBUG
124/* Log a fatal error; doesn't return. */
125void
Victor Stinner18618e652018-10-25 17:28:11 +0200126_Py_NegativeRefcount(const char *filename, int lineno, PyObject *op)
Tim Peters7c321a82002-07-09 02:57:01 +0000127{
Victor Stinnerf1d002c2018-11-21 23:53:44 +0100128 _PyObject_AssertFailed(op, NULL, "object has negative ref count",
Victor Stinner3ec9af72018-10-26 02:12:34 +0200129 filename, lineno, __func__);
Tim Peters7c321a82002-07-09 02:57:01 +0000130}
131
132#endif /* Py_REF_DEBUG */
133
Thomas Heller1328b522004-04-22 17:23:49 +0000134void
135Py_IncRef(PyObject *o)
136{
137 Py_XINCREF(o);
138}
139
140void
141Py_DecRef(PyObject *o)
142{
143 Py_XDECREF(o);
144}
145
Victor Stinner3359cab2021-04-02 15:45:37 +0200146void
147_Py_IncRef(PyObject *o)
148{
149 Py_INCREF(o);
150}
151
152void
153_Py_DecRef(PyObject *o)
154{
155 Py_DECREF(o);
156}
157
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000158PyObject *
Fred Drake100814d2000-07-09 15:48:49 +0000159PyObject_Init(PyObject *op, PyTypeObject *tp)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000160{
Victor Stinnerf58bd7c2020-02-05 13:12:19 +0100161 if (op == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000162 return PyErr_NoMemory();
Victor Stinnerf58bd7c2020-02-05 13:12:19 +0100163 }
164
Victor Stinner04fc4f22020-06-16 01:28:07 +0200165 _PyObject_Init(op, tp);
166 return op;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000167}
168
Guido van Rossumb18618d2000-05-03 23:44:39 +0000169PyVarObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000170PyObject_InitVar(PyVarObject *op, PyTypeObject *tp, Py_ssize_t size)
Guido van Rossumb18618d2000-05-03 23:44:39 +0000171{
Victor Stinnerf58bd7c2020-02-05 13:12:19 +0100172 if (op == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000173 return (PyVarObject *) PyErr_NoMemory();
Victor Stinnerf58bd7c2020-02-05 13:12:19 +0100174 }
175
Victor Stinner04fc4f22020-06-16 01:28:07 +0200176 _PyObject_InitVar(op, tp, size);
177 return op;
Guido van Rossumb18618d2000-05-03 23:44:39 +0000178}
179
180PyObject *
Fred Drake100814d2000-07-09 15:48:49 +0000181_PyObject_New(PyTypeObject *tp)
Guido van Rossumb18618d2000-05-03 23:44:39 +0000182{
Victor Stinner32bd68c2020-12-01 10:37:39 +0100183 PyObject *op = (PyObject *) PyObject_Malloc(_PyObject_SIZE(tp));
Victor Stinner92055202020-04-08 00:38:15 +0200184 if (op == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000185 return PyErr_NoMemory();
Victor Stinner92055202020-04-08 00:38:15 +0200186 }
Victor Stinner04fc4f22020-06-16 01:28:07 +0200187 _PyObject_Init(op, tp);
Victor Stinner92055202020-04-08 00:38:15 +0200188 return op;
Guido van Rossumb18618d2000-05-03 23:44:39 +0000189}
190
Guido van Rossumd0c87ee1997-05-15 21:31:03 +0000191PyVarObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000192_PyObject_NewVar(PyTypeObject *tp, Py_ssize_t nitems)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000193{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000194 PyVarObject *op;
195 const size_t size = _PyObject_VAR_SIZE(tp, nitems);
Victor Stinner32bd68c2020-12-01 10:37:39 +0100196 op = (PyVarObject *) PyObject_Malloc(size);
Victor Stinner04fc4f22020-06-16 01:28:07 +0200197 if (op == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000198 return (PyVarObject *)PyErr_NoMemory();
Victor Stinner04fc4f22020-06-16 01:28:07 +0200199 }
200 _PyObject_InitVar(op, tp, nitems);
201 return op;
Guido van Rossumb18618d2000-05-03 23:44:39 +0000202}
203
Antoine Pitrou796564c2013-07-30 19:59:21 +0200204void
205PyObject_CallFinalizer(PyObject *self)
206{
207 PyTypeObject *tp = Py_TYPE(self);
208
Antoine Pitrouada319b2019-05-29 22:12:38 +0200209 if (tp->tp_finalize == NULL)
Antoine Pitrou796564c2013-07-30 19:59:21 +0200210 return;
211 /* tp_finalize should only be called once. */
Victor Stinner45ec5b92020-04-08 01:42:27 +0200212 if (_PyType_IS_GC(tp) && _PyGC_FINALIZED(self))
Antoine Pitrou796564c2013-07-30 19:59:21 +0200213 return;
214
215 tp->tp_finalize(self);
Victor Stinner45ec5b92020-04-08 01:42:27 +0200216 if (_PyType_IS_GC(tp)) {
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900217 _PyGC_SET_FINALIZED(self);
218 }
Antoine Pitrou796564c2013-07-30 19:59:21 +0200219}
220
221int
222PyObject_CallFinalizerFromDealloc(PyObject *self)
223{
Victor Stinnera93c51e2020-02-07 00:38:59 +0100224 if (Py_REFCNT(self) != 0) {
Victor Stinner5eb8bff2020-01-30 09:01:07 +0100225 _PyObject_ASSERT_FAILED_MSG(self,
226 "PyObject_CallFinalizerFromDealloc called "
227 "on object with a non-zero refcount");
228 }
Antoine Pitrou796564c2013-07-30 19:59:21 +0200229
230 /* Temporarily resurrect the object. */
Victor Stinnerc86a1122020-02-07 01:24:29 +0100231 Py_SET_REFCNT(self, 1);
Antoine Pitrou796564c2013-07-30 19:59:21 +0200232
233 PyObject_CallFinalizer(self);
234
Victor Stinner24702042018-10-26 17:16:37 +0200235 _PyObject_ASSERT_WITH_MSG(self,
Victor Stinnera93c51e2020-02-07 00:38:59 +0100236 Py_REFCNT(self) > 0,
Victor Stinner24702042018-10-26 17:16:37 +0200237 "refcount is too small");
Victor Stinner5eb8bff2020-01-30 09:01:07 +0100238
239 /* Undo the temporary resurrection; can't use DECREF here, it would
240 * cause a recursive call. */
Victor Stinnerc86a1122020-02-07 01:24:29 +0100241 Py_SET_REFCNT(self, Py_REFCNT(self) - 1);
242 if (Py_REFCNT(self) == 0) {
Antoine Pitrou796564c2013-07-30 19:59:21 +0200243 return 0; /* this is the normal path out */
Victor Stinner5eb8bff2020-01-30 09:01:07 +0100244 }
Antoine Pitrou796564c2013-07-30 19:59:21 +0200245
246 /* tp_finalize resurrected it! Make it look like the original Py_DECREF
Victor Stinner5eb8bff2020-01-30 09:01:07 +0100247 * never happened. */
Victor Stinnera93c51e2020-02-07 00:38:59 +0100248 Py_ssize_t refcnt = Py_REFCNT(self);
Antoine Pitrou796564c2013-07-30 19:59:21 +0200249 _Py_NewReference(self);
Victor Stinnerc86a1122020-02-07 01:24:29 +0100250 Py_SET_REFCNT(self, refcnt);
Antoine Pitrou796564c2013-07-30 19:59:21 +0200251
Victor Stinner24702042018-10-26 17:16:37 +0200252 _PyObject_ASSERT(self,
Victor Stinner45ec5b92020-04-08 01:42:27 +0200253 (!_PyType_IS_GC(Py_TYPE(self))
Victor Stinner24702042018-10-26 17:16:37 +0200254 || _PyObject_GC_IS_TRACKED(self)));
Victor Stinner49932fe2020-02-03 17:55:05 +0100255 /* If Py_REF_DEBUG macro is defined, _Py_NewReference() increased
256 _Py_RefTotal, so we need to undo that. */
257#ifdef Py_REF_DEBUG
258 _Py_RefTotal--;
259#endif
Antoine Pitrou796564c2013-07-30 19:59:21 +0200260 return -1;
261}
262
Antoine Pitrouc47bd4a2010-07-27 22:08:27 +0000263int
264PyObject_Print(PyObject *op, FILE *fp, int flags)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000265{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000266 int ret = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000267 if (PyErr_CheckSignals())
268 return -1;
Guido van Rossum9b00dfa1998-04-28 16:06:54 +0000269#ifdef USE_STACKCHECK
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000270 if (PyOS_CheckStack()) {
271 PyErr_SetString(PyExc_MemoryError, "stack overflow");
272 return -1;
273 }
Guido van Rossum9b00dfa1998-04-28 16:06:54 +0000274#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000275 clearerr(fp); /* Clear any previous error condition */
276 if (op == NULL) {
277 Py_BEGIN_ALLOW_THREADS
278 fprintf(fp, "<nil>");
279 Py_END_ALLOW_THREADS
280 }
281 else {
Victor Stinnera93c51e2020-02-07 00:38:59 +0100282 if (Py_REFCNT(op) <= 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000283 /* XXX(twouters) cast refcount to long until %zd is
284 universally available */
285 Py_BEGIN_ALLOW_THREADS
286 fprintf(fp, "<refcnt %ld at %p>",
Victor Stinnera93c51e2020-02-07 00:38:59 +0100287 (long)Py_REFCNT(op), (void *)op);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000288 Py_END_ALLOW_THREADS
Victor Stinner3ec9af72018-10-26 02:12:34 +0200289 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000290 else {
291 PyObject *s;
292 if (flags & Py_PRINT_RAW)
293 s = PyObject_Str(op);
294 else
295 s = PyObject_Repr(op);
296 if (s == NULL)
297 ret = -1;
298 else if (PyBytes_Check(s)) {
299 fwrite(PyBytes_AS_STRING(s), 1,
300 PyBytes_GET_SIZE(s), fp);
301 }
302 else if (PyUnicode_Check(s)) {
303 PyObject *t;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200304 t = PyUnicode_AsEncodedString(s, "utf-8", "backslashreplace");
Zackery Spytzae62f012018-10-06 00:44:25 -0600305 if (t == NULL) {
306 ret = -1;
307 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000308 else {
309 fwrite(PyBytes_AS_STRING(t), 1,
310 PyBytes_GET_SIZE(t), fp);
Victor Stinnerba6b4302010-05-17 09:33:42 +0000311 Py_DECREF(t);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000312 }
313 }
314 else {
315 PyErr_Format(PyExc_TypeError,
316 "str() or repr() returned '%.100s'",
Victor Stinner58ac7002020-02-07 03:04:21 +0100317 Py_TYPE(s)->tp_name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000318 ret = -1;
319 }
320 Py_XDECREF(s);
321 }
322 }
323 if (ret == 0) {
324 if (ferror(fp)) {
Serhiy Storchaka55fe1ae2017-04-16 10:46:38 +0300325 PyErr_SetFromErrno(PyExc_OSError);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000326 clearerr(fp);
327 ret = -1;
328 }
329 }
330 return ret;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000331}
332
Guido van Rossum38938152006-08-21 23:36:26 +0000333/* For debugging convenience. Set a breakpoint here and call it from your DLL */
334void
Thomas Woutersb2137042007-02-01 18:02:27 +0000335_Py_BreakPoint(void)
Guido van Rossum38938152006-08-21 23:36:26 +0000336{
337}
338
Neal Norwitz1a997502003-01-13 20:13:12 +0000339
Victor Stinner4c409be2019-04-11 13:01:15 +0200340/* Heuristic checking if the object memory is uninitialized or deallocated.
341 Rely on the debug hooks on Python memory allocators:
342 see _PyMem_IsPtrFreed().
Victor Stinner82af0b62018-10-23 17:39:40 +0200343
344 The function can be used to prevent segmentation fault on dereferencing
Victor Stinner4c409be2019-04-11 13:01:15 +0200345 pointers like 0xDDDDDDDDDDDDDDDD. */
Victor Stinner82af0b62018-10-23 17:39:40 +0200346int
347_PyObject_IsFreed(PyObject *op)
348{
Victor Stinner58ac7002020-02-07 03:04:21 +0100349 if (_PyMem_IsPtrFreed(op) || _PyMem_IsPtrFreed(Py_TYPE(op))) {
Victor Stinner2cf5d322018-11-22 16:32:57 +0100350 return 1;
351 }
Victor Stinner2b00db62019-04-11 11:33:27 +0200352 /* ignore op->ob_ref: its value can have be modified
Victor Stinner82af0b62018-10-23 17:39:40 +0200353 by Py_INCREF() and Py_DECREF(). */
354#ifdef Py_TRACE_REFS
Pablo Galindo36e33c32019-10-08 00:43:14 +0100355 if (op->_ob_next != NULL && _PyMem_IsPtrFreed(op->_ob_next)) {
Victor Stinner2b00db62019-04-11 11:33:27 +0200356 return 1;
357 }
Pablo Galindo36e33c32019-10-08 00:43:14 +0100358 if (op->_ob_prev != NULL && _PyMem_IsPtrFreed(op->_ob_prev)) {
359 return 1;
360 }
Victor Stinner82af0b62018-10-23 17:39:40 +0200361#endif
Victor Stinner2b00db62019-04-11 11:33:27 +0200362 return 0;
Victor Stinner82af0b62018-10-23 17:39:40 +0200363}
364
365
Barry Warsaw9bf16442001-01-23 16:24:35 +0000366/* For debugging convenience. See Misc/gdbinit for some useful gdb hooks */
Guido van Rossum38938152006-08-21 23:36:26 +0000367void
368_PyObject_Dump(PyObject* op)
Barry Warsaw9bf16442001-01-23 16:24:35 +0000369{
Victor Stinner82af0b62018-10-23 17:39:40 +0200370 if (_PyObject_IsFreed(op)) {
371 /* It seems like the object memory has been freed:
372 don't access it to prevent a segmentation fault. */
Victor Stinnerb39afb72019-09-17 23:36:28 +0200373 fprintf(stderr, "<object at %p is freed>\n", op);
Victor Stinner68762572019-10-07 18:42:01 +0200374 fflush(stderr);
Victor Stinner2cf5d322018-11-22 16:32:57 +0100375 return;
Victor Stinner82af0b62018-10-23 17:39:40 +0200376 }
377
Victor Stinner68762572019-10-07 18:42:01 +0200378 /* first, write fields which are the least likely to crash */
379 fprintf(stderr, "object address : %p\n", (void *)op);
Victor Stinner82af0b62018-10-23 17:39:40 +0200380 /* XXX(twouters) cast refcount to long until %zd is
381 universally available */
Victor Stinnera93c51e2020-02-07 00:38:59 +0100382 fprintf(stderr, "object refcount : %ld\n", (long)Py_REFCNT(op));
Victor Stinner68762572019-10-07 18:42:01 +0200383 fflush(stderr);
384
385 PyTypeObject *type = Py_TYPE(op);
386 fprintf(stderr, "object type : %p\n", type);
387 fprintf(stderr, "object type name: %s\n",
388 type==NULL ? "NULL" : type->tp_name);
389
390 /* the most dangerous part */
391 fprintf(stderr, "object repr : ");
392 fflush(stderr);
393
394 PyGILState_STATE gil = PyGILState_Ensure();
395 PyObject *error_type, *error_value, *error_traceback;
396 PyErr_Fetch(&error_type, &error_value, &error_traceback);
397
398 (void)PyObject_Print(op, stderr, 0);
399 fflush(stderr);
400
401 PyErr_Restore(error_type, error_value, error_traceback);
402 PyGILState_Release(gil);
403
404 fprintf(stderr, "\n");
Victor Stinner82af0b62018-10-23 17:39:40 +0200405 fflush(stderr);
Barry Warsaw9bf16442001-01-23 16:24:35 +0000406}
Barry Warsaw903138f2001-01-23 16:33:18 +0000407
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000408PyObject *
Fred Drake100814d2000-07-09 15:48:49 +0000409PyObject_Repr(PyObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000410{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000411 PyObject *res;
412 if (PyErr_CheckSignals())
413 return NULL;
Guido van Rossum9b00dfa1998-04-28 16:06:54 +0000414#ifdef USE_STACKCHECK
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000415 if (PyOS_CheckStack()) {
416 PyErr_SetString(PyExc_MemoryError, "stack overflow");
417 return NULL;
418 }
Guido van Rossum9b00dfa1998-04-28 16:06:54 +0000419#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000420 if (v == NULL)
421 return PyUnicode_FromString("<NULL>");
422 if (Py_TYPE(v)->tp_repr == NULL)
423 return PyUnicode_FromFormat("<%s object at %p>",
Victor Stinner58ac7002020-02-07 03:04:21 +0100424 Py_TYPE(v)->tp_name, v);
Victor Stinner33824f62013-08-26 14:05:19 +0200425
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100426 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinner33824f62013-08-26 14:05:19 +0200427#ifdef Py_DEBUG
428 /* PyObject_Repr() must not be called with an exception set,
Victor Stinnera8cb5152017-01-18 14:12:51 +0100429 because it can clear it (directly or indirectly) and so the
Martin Panter9955a372015-10-07 10:26:23 +0000430 caller loses its exception */
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100431 assert(!_PyErr_Occurred(tstate));
Victor Stinner33824f62013-08-26 14:05:19 +0200432#endif
433
Serhiy Storchaka1fb72d22017-12-03 22:12:11 +0200434 /* It is possible for a type to have a tp_repr representation that loops
435 infinitely. */
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100436 if (_Py_EnterRecursiveCall(tstate,
437 " while getting the repr of an object")) {
Serhiy Storchaka1fb72d22017-12-03 22:12:11 +0200438 return NULL;
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100439 }
Victor Stinner58ac7002020-02-07 03:04:21 +0100440 res = (*Py_TYPE(v)->tp_repr)(v);
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100441 _Py_LeaveRecursiveCall(tstate);
442
443 if (res == NULL) {
Victor Stinner0a54cf12011-12-01 03:22:44 +0100444 return NULL;
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100445 }
Victor Stinner0a54cf12011-12-01 03:22:44 +0100446 if (!PyUnicode_Check(res)) {
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100447 _PyErr_Format(tstate, PyExc_TypeError,
448 "__repr__ returned non-string (type %.200s)",
Victor Stinner58ac7002020-02-07 03:04:21 +0100449 Py_TYPE(res)->tp_name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000450 Py_DECREF(res);
451 return NULL;
452 }
Victor Stinnerdb88ae52011-12-01 02:15:00 +0100453#ifndef Py_DEBUG
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100454 if (PyUnicode_READY(res) < 0) {
Victor Stinnerdb88ae52011-12-01 02:15:00 +0100455 return NULL;
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100456 }
Victor Stinnerdb88ae52011-12-01 02:15:00 +0100457#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000458 return res;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000459}
460
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000461PyObject *
Guido van Rossum98297ee2007-11-06 21:34:58 +0000462PyObject_Str(PyObject *v)
Guido van Rossumc6004111993-11-05 10:22:19 +0000463{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000464 PyObject *res;
465 if (PyErr_CheckSignals())
466 return NULL;
Guido van Rossum98297ee2007-11-06 21:34:58 +0000467#ifdef USE_STACKCHECK
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000468 if (PyOS_CheckStack()) {
469 PyErr_SetString(PyExc_MemoryError, "stack overflow");
470 return NULL;
471 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000472#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000473 if (v == NULL)
474 return PyUnicode_FromString("<NULL>");
475 if (PyUnicode_CheckExact(v)) {
Victor Stinnerdb88ae52011-12-01 02:15:00 +0100476#ifndef Py_DEBUG
Victor Stinner4ead7c72011-11-20 19:48:36 +0100477 if (PyUnicode_READY(v) < 0)
478 return NULL;
Victor Stinnerdb88ae52011-12-01 02:15:00 +0100479#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000480 Py_INCREF(v);
481 return v;
482 }
483 if (Py_TYPE(v)->tp_str == NULL)
484 return PyObject_Repr(v);
Guido van Rossum4f288ab2001-05-01 16:53:37 +0000485
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100486 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinner33824f62013-08-26 14:05:19 +0200487#ifdef Py_DEBUG
488 /* PyObject_Str() must not be called with an exception set,
Victor Stinnera8cb5152017-01-18 14:12:51 +0100489 because it can clear it (directly or indirectly) and so the
Nick Coghland979e432014-02-09 10:43:21 +1000490 caller loses its exception */
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100491 assert(!_PyErr_Occurred(tstate));
Victor Stinner33824f62013-08-26 14:05:19 +0200492#endif
493
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000494 /* It is possible for a type to have a tp_str representation that loops
495 infinitely. */
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100496 if (_Py_EnterRecursiveCall(tstate, " while getting the str of an object")) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000497 return NULL;
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100498 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000499 res = (*Py_TYPE(v)->tp_str)(v);
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100500 _Py_LeaveRecursiveCall(tstate);
501
502 if (res == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000503 return NULL;
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100504 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000505 if (!PyUnicode_Check(res)) {
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100506 _PyErr_Format(tstate, PyExc_TypeError,
507 "__str__ returned non-string (type %.200s)",
508 Py_TYPE(res)->tp_name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000509 Py_DECREF(res);
510 return NULL;
511 }
Victor Stinnerdb88ae52011-12-01 02:15:00 +0100512#ifndef Py_DEBUG
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100513 if (PyUnicode_READY(res) < 0) {
Victor Stinner4ead7c72011-11-20 19:48:36 +0100514 return NULL;
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100515 }
Victor Stinnerdb88ae52011-12-01 02:15:00 +0100516#endif
Victor Stinner4ead7c72011-11-20 19:48:36 +0100517 assert(_PyUnicode_CheckConsistency(res, 1));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000518 return res;
Neil Schemenauercf52c072005-08-12 17:34:58 +0000519}
520
Georg Brandl559e5d72008-06-11 18:37:52 +0000521PyObject *
522PyObject_ASCII(PyObject *v)
523{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000524 PyObject *repr, *ascii, *res;
Georg Brandl559e5d72008-06-11 18:37:52 +0000525
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000526 repr = PyObject_Repr(v);
527 if (repr == NULL)
528 return NULL;
Georg Brandl559e5d72008-06-11 18:37:52 +0000529
Victor Stinneraf037572013-04-14 18:44:10 +0200530 if (PyUnicode_IS_ASCII(repr))
531 return repr;
532
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000533 /* repr is guaranteed to be a PyUnicode object by PyObject_Repr */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200534 ascii = _PyUnicode_AsASCIIString(repr, "backslashreplace");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000535 Py_DECREF(repr);
536 if (ascii == NULL)
537 return NULL;
Georg Brandl559e5d72008-06-11 18:37:52 +0000538
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000539 res = PyUnicode_DecodeASCII(
540 PyBytes_AS_STRING(ascii),
541 PyBytes_GET_SIZE(ascii),
542 NULL);
543
544 Py_DECREF(ascii);
545 return res;
Georg Brandl559e5d72008-06-11 18:37:52 +0000546}
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000547
Benjamin Petersonc15a0732008-08-26 16:46:47 +0000548PyObject *
549PyObject_Bytes(PyObject *v)
550{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000551 PyObject *result, *func;
Benjamin Petersonc15a0732008-08-26 16:46:47 +0000552
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000553 if (v == NULL)
554 return PyBytes_FromString("<NULL>");
Benjamin Petersonc15a0732008-08-26 16:46:47 +0000555
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000556 if (PyBytes_CheckExact(v)) {
557 Py_INCREF(v);
558 return v;
559 }
Benjamin Petersonc15a0732008-08-26 16:46:47 +0000560
Benjamin Petersonce798522012-01-22 11:24:29 -0500561 func = _PyObject_LookupSpecial(v, &PyId___bytes__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000562 if (func != NULL) {
Victor Stinnerf17c3de2016-12-06 18:46:19 +0100563 result = _PyObject_CallNoArg(func);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000564 Py_DECREF(func);
565 if (result == NULL)
Benjamin Peterson41ece392010-09-11 16:39:57 +0000566 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000567 if (!PyBytes_Check(result)) {
Benjamin Peterson41ece392010-09-11 16:39:57 +0000568 PyErr_Format(PyExc_TypeError,
569 "__bytes__ returned non-bytes (type %.200s)",
570 Py_TYPE(result)->tp_name);
571 Py_DECREF(result);
572 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000573 }
574 return result;
575 }
576 else if (PyErr_Occurred())
577 return NULL;
578 return PyBytes_FromObject(v);
Benjamin Petersonc15a0732008-08-26 16:46:47 +0000579}
580
Jeroen Demeyerbf17d412019-11-05 16:48:04 +0100581
582/*
583def _PyObject_FunctionStr(x):
584 try:
585 qualname = x.__qualname__
586 except AttributeError:
587 return str(x)
588 try:
589 mod = x.__module__
590 if mod is not None and mod != 'builtins':
591 return f"{x.__module__}.{qualname}()"
592 except AttributeError:
593 pass
594 return qualname
595*/
596PyObject *
597_PyObject_FunctionStr(PyObject *x)
598{
599 _Py_IDENTIFIER(__module__);
600 _Py_IDENTIFIER(__qualname__);
601 _Py_IDENTIFIER(builtins);
602 assert(!PyErr_Occurred());
603 PyObject *qualname;
604 int ret = _PyObject_LookupAttrId(x, &PyId___qualname__, &qualname);
605 if (qualname == NULL) {
606 if (ret < 0) {
607 return NULL;
608 }
609 return PyObject_Str(x);
610 }
611 PyObject *module;
612 PyObject *result = NULL;
613 ret = _PyObject_LookupAttrId(x, &PyId___module__, &module);
614 if (module != NULL && module != Py_None) {
615 PyObject *builtinsname = _PyUnicode_FromId(&PyId_builtins);
616 if (builtinsname == NULL) {
617 goto done;
618 }
619 ret = PyObject_RichCompareBool(module, builtinsname, Py_NE);
620 if (ret < 0) {
621 // error
622 goto done;
623 }
624 if (ret > 0) {
625 result = PyUnicode_FromFormat("%S.%S()", module, qualname);
626 goto done;
627 }
628 }
629 else if (ret < 0) {
630 goto done;
631 }
632 result = PyUnicode_FromFormat("%S()", qualname);
633done:
634 Py_DECREF(qualname);
635 Py_XDECREF(module);
636 return result;
637}
638
Mark Dickinsonc008a172009-02-01 13:59:22 +0000639/* For Python 3.0.1 and later, the old three-way comparison has been
640 completely removed in favour of rich comparisons. PyObject_Compare() and
641 PyObject_Cmp() are gone, and the builtin cmp function no longer exists.
Jeroen Demeyer530f5062019-05-31 04:13:39 +0200642 The old tp_compare slot has been renamed to tp_as_async, and should no
Mark Dickinsonc008a172009-02-01 13:59:22 +0000643 longer be used. Use tp_richcompare instead.
Guido van Rossum98297ee2007-11-06 21:34:58 +0000644
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000645 See (*) below for practical amendments.
646
Mark Dickinsonc008a172009-02-01 13:59:22 +0000647 tp_richcompare gets called with a first argument of the appropriate type
648 and a second object of an arbitrary type. We never do any kind of
649 coercion.
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000650
Mark Dickinsonc008a172009-02-01 13:59:22 +0000651 The tp_richcompare slot should return an object, as follows:
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000652
653 NULL if an exception occurred
654 NotImplemented if the requested comparison is not implemented
655 any other false value if the requested comparison is false
656 any other true value if the requested comparison is true
657
658 The PyObject_RichCompare[Bool]() wrappers raise TypeError when they get
659 NotImplemented.
660
661 (*) Practical amendments:
662
663 - If rich comparison returns NotImplemented, == and != are decided by
664 comparing the object pointer (i.e. falling back to the base object
665 implementation).
666
Guido van Rossuma4073002002-05-31 20:03:54 +0000667*/
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000668
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000669/* Map rich comparison operators to their swapped version, e.g. LT <--> GT */
Brett Cannona5ca2e72004-09-25 01:37:24 +0000670int _Py_SwappedOp[] = {Py_GT, Py_GE, Py_EQ, Py_NE, Py_LT, Py_LE};
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +0000671
Serhiy Storchaka2d06e842015-12-25 19:53:18 +0200672static const char * const opstrings[] = {"<", "<=", "==", "!=", ">", ">="};
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000673
674/* Perform a rich comparison, raising TypeError when the requested comparison
675 operator is not supported. */
Guido van Rossume797ec12001-01-17 15:24:28 +0000676static PyObject *
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100677do_richcompare(PyThreadState *tstate, PyObject *v, PyObject *w, int op)
Guido van Rossume797ec12001-01-17 15:24:28 +0000678{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000679 richcmpfunc f;
680 PyObject *res;
681 int checked_reverse_op = 0;
Guido van Rossume797ec12001-01-17 15:24:28 +0000682
Andy Lester55728702020-03-06 16:53:17 -0600683 if (!Py_IS_TYPE(v, Py_TYPE(w)) &&
Victor Stinner58ac7002020-02-07 03:04:21 +0100684 PyType_IsSubtype(Py_TYPE(w), Py_TYPE(v)) &&
685 (f = Py_TYPE(w)->tp_richcompare) != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000686 checked_reverse_op = 1;
687 res = (*f)(w, v, _Py_SwappedOp[op]);
688 if (res != Py_NotImplemented)
689 return res;
690 Py_DECREF(res);
691 }
Victor Stinner58ac7002020-02-07 03:04:21 +0100692 if ((f = Py_TYPE(v)->tp_richcompare) != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000693 res = (*f)(v, w, op);
694 if (res != Py_NotImplemented)
695 return res;
696 Py_DECREF(res);
697 }
Victor Stinner58ac7002020-02-07 03:04:21 +0100698 if (!checked_reverse_op && (f = Py_TYPE(w)->tp_richcompare) != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000699 res = (*f)(w, v, _Py_SwappedOp[op]);
700 if (res != Py_NotImplemented)
701 return res;
702 Py_DECREF(res);
703 }
704 /* If neither object implements it, provide a sensible default
705 for == and !=, but raise an exception for ordering. */
706 switch (op) {
707 case Py_EQ:
708 res = (v == w) ? Py_True : Py_False;
709 break;
710 case Py_NE:
711 res = (v != w) ? Py_True : Py_False;
712 break;
713 default:
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100714 _PyErr_Format(tstate, PyExc_TypeError,
715 "'%s' not supported between instances of '%.100s' and '%.100s'",
716 opstrings[op],
Victor Stinner58ac7002020-02-07 03:04:21 +0100717 Py_TYPE(v)->tp_name,
718 Py_TYPE(w)->tp_name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000719 return NULL;
720 }
721 Py_INCREF(res);
722 return res;
Guido van Rossume797ec12001-01-17 15:24:28 +0000723}
724
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000725/* Perform a rich comparison with object result. This wraps do_richcompare()
726 with a check for NULL arguments and a recursion check. */
Guido van Rossume797ec12001-01-17 15:24:28 +0000727
Guido van Rossume797ec12001-01-17 15:24:28 +0000728PyObject *
729PyObject_RichCompare(PyObject *v, PyObject *w, int op)
730{
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100731 PyThreadState *tstate = _PyThreadState_GET();
Guido van Rossume797ec12001-01-17 15:24:28 +0000732
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000733 assert(Py_LT <= op && op <= Py_GE);
734 if (v == NULL || w == NULL) {
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100735 if (!_PyErr_Occurred(tstate)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000736 PyErr_BadInternalCall();
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100737 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000738 return NULL;
739 }
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100740 if (_Py_EnterRecursiveCall(tstate, " in comparison")) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000741 return NULL;
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100742 }
743 PyObject *res = do_richcompare(tstate, v, w, op);
744 _Py_LeaveRecursiveCall(tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000745 return res;
Guido van Rossume797ec12001-01-17 15:24:28 +0000746}
747
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000748/* Perform a rich comparison with integer result. This wraps
749 PyObject_RichCompare(), returning -1 for error, 0 for false, 1 for true. */
Guido van Rossume797ec12001-01-17 15:24:28 +0000750int
751PyObject_RichCompareBool(PyObject *v, PyObject *w, int op)
752{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000753 PyObject *res;
754 int ok;
Guido van Rossume797ec12001-01-17 15:24:28 +0000755
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000756 /* Quick result when objects are the same.
757 Guarantees that identity implies equality. */
758 if (v == w) {
759 if (op == Py_EQ)
760 return 1;
761 else if (op == Py_NE)
762 return 0;
763 }
Mark Dickinson4a1f5932008-11-12 23:23:36 +0000764
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000765 res = PyObject_RichCompare(v, w, op);
766 if (res == NULL)
767 return -1;
768 if (PyBool_Check(res))
769 ok = (res == Py_True);
770 else
771 ok = PyObject_IsTrue(res);
772 Py_DECREF(res);
773 return ok;
Guido van Rossume797ec12001-01-17 15:24:28 +0000774}
Fred Drake13634cf2000-06-29 19:17:04 +0000775
Antoine Pitrouce4a9da2011-11-21 20:46:33 +0100776Py_hash_t
Nick Coghland1abd252008-07-15 15:46:38 +0000777PyObject_HashNotImplemented(PyObject *v)
778{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000779 PyErr_Format(PyExc_TypeError, "unhashable type: '%.200s'",
780 Py_TYPE(v)->tp_name);
781 return -1;
Nick Coghland1abd252008-07-15 15:46:38 +0000782}
Fred Drake13634cf2000-06-29 19:17:04 +0000783
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000784Py_hash_t
Fred Drake100814d2000-07-09 15:48:49 +0000785PyObject_Hash(PyObject *v)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000786{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000787 PyTypeObject *tp = Py_TYPE(v);
788 if (tp->tp_hash != NULL)
789 return (*tp->tp_hash)(v);
790 /* To keep to the general practice that inheriting
791 * solely from object in C code should work without
792 * an explicit call to PyType_Ready, we implicitly call
793 * PyType_Ready here and then check the tp_hash slot again
794 */
795 if (tp->tp_dict == NULL) {
796 if (PyType_Ready(tp) < 0)
797 return -1;
798 if (tp->tp_hash != NULL)
799 return (*tp->tp_hash)(v);
800 }
801 /* Otherwise, the object can't be hashed */
802 return PyObject_HashNotImplemented(v);
Guido van Rossum9bfef441993-03-29 10:43:31 +0000803}
804
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000805PyObject *
Jeremy Hyltonaf68c872005-12-10 18:50:16 +0000806PyObject_GetAttrString(PyObject *v, const char *name)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000807{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000808 PyObject *w, *res;
Guido van Rossumd8eb1b31996-08-09 20:52:03 +0000809
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000810 if (Py_TYPE(v)->tp_getattr != NULL)
811 return (*Py_TYPE(v)->tp_getattr)(v, (char*)name);
INADA Naoki3e8d6cb2017-02-21 23:57:25 +0900812 w = PyUnicode_FromString(name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000813 if (w == NULL)
814 return NULL;
815 res = PyObject_GetAttr(v, w);
Victor Stinner59af08f2012-03-22 02:09:08 +0100816 Py_DECREF(w);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000817 return res;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000818}
819
820int
Jeremy Hyltonaf68c872005-12-10 18:50:16 +0000821PyObject_HasAttrString(PyObject *v, const char *name)
Guido van Rossumed18fdc1993-07-11 19:55:34 +0000822{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000823 PyObject *res = PyObject_GetAttrString(v, name);
824 if (res != NULL) {
825 Py_DECREF(res);
826 return 1;
827 }
828 PyErr_Clear();
829 return 0;
Guido van Rossumed18fdc1993-07-11 19:55:34 +0000830}
831
832int
Jeremy Hyltonaf68c872005-12-10 18:50:16 +0000833PyObject_SetAttrString(PyObject *v, const char *name, PyObject *w)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000834{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000835 PyObject *s;
836 int res;
Guido van Rossumd8eb1b31996-08-09 20:52:03 +0000837
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000838 if (Py_TYPE(v)->tp_setattr != NULL)
839 return (*Py_TYPE(v)->tp_setattr)(v, (char*)name, w);
840 s = PyUnicode_InternFromString(name);
841 if (s == NULL)
842 return -1;
843 res = PyObject_SetAttr(v, s, w);
844 Py_XDECREF(s);
845 return res;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000846}
847
Benjamin Petersonbfebb7b2011-12-15 15:34:02 -0500848int
849_PyObject_IsAbstract(PyObject *obj)
850{
851 int res;
852 PyObject* isabstract;
Benjamin Petersonbfebb7b2011-12-15 15:34:02 -0500853
854 if (obj == NULL)
855 return 0;
856
Serhiy Storchakaf320be72018-01-25 10:49:40 +0200857 res = _PyObject_LookupAttrId(obj, &PyId___isabstractmethod__, &isabstract);
858 if (res > 0) {
859 res = PyObject_IsTrue(isabstract);
860 Py_DECREF(isabstract);
Benjamin Petersonbfebb7b2011-12-15 15:34:02 -0500861 }
Benjamin Petersonbfebb7b2011-12-15 15:34:02 -0500862 return res;
863}
864
Guido van Rossum98ff96a1997-05-20 18:34:44 +0000865PyObject *
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200866_PyObject_GetAttrId(PyObject *v, _Py_Identifier *name)
867{
868 PyObject *result;
Martin v. Löwisd10759f2011-11-07 13:00:05 +0100869 PyObject *oname = _PyUnicode_FromId(name); /* borrowed */
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200870 if (!oname)
871 return NULL;
872 result = PyObject_GetAttr(v, oname);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200873 return result;
874}
875
876int
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200877_PyObject_SetAttrId(PyObject *v, _Py_Identifier *name, PyObject *w)
878{
879 int result;
Martin v. Löwisd10759f2011-11-07 13:00:05 +0100880 PyObject *oname = _PyUnicode_FromId(name); /* borrowed */
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200881 if (!oname)
882 return -1;
883 result = PyObject_SetAttr(v, oname, w);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200884 return result;
885}
886
Pablo Galindo37494b42021-04-14 02:36:07 +0100887static inline int
888set_attribute_error_context(PyObject* v, PyObject* name)
889{
890 assert(PyErr_Occurred());
891 _Py_IDENTIFIER(name);
892 _Py_IDENTIFIER(obj);
893 // Intercept AttributeError exceptions and augment them to offer
894 // suggestions later.
895 if (PyErr_ExceptionMatches(PyExc_AttributeError)){
896 PyObject *type, *value, *traceback;
897 PyErr_Fetch(&type, &value, &traceback);
898 PyErr_NormalizeException(&type, &value, &traceback);
899 if (PyErr_GivenExceptionMatches(value, PyExc_AttributeError) &&
900 (_PyObject_SetAttrId(value, &PyId_name, name) ||
901 _PyObject_SetAttrId(value, &PyId_obj, v))) {
902 return 1;
903 }
904 PyErr_Restore(type, value, traceback);
905 }
906 return 0;
907}
908
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200909PyObject *
Fred Drake100814d2000-07-09 15:48:49 +0000910PyObject_GetAttr(PyObject *v, PyObject *name)
Guido van Rossum98ff96a1997-05-20 18:34:44 +0000911{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000912 PyTypeObject *tp = Py_TYPE(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000913 if (!PyUnicode_Check(name)) {
914 PyErr_Format(PyExc_TypeError,
915 "attribute name must be string, not '%.200s'",
Victor Stinner58ac7002020-02-07 03:04:21 +0100916 Py_TYPE(name)->tp_name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000917 return NULL;
918 }
Pablo Galindo37494b42021-04-14 02:36:07 +0100919
920 PyObject* result = NULL;
921 if (tp->tp_getattro != NULL) {
922 result = (*tp->tp_getattro)(v, name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000923 }
Pablo Galindo37494b42021-04-14 02:36:07 +0100924 else if (tp->tp_getattr != NULL) {
925 const char *name_str = PyUnicode_AsUTF8(name);
926 if (name_str == NULL) {
927 return NULL;
928 }
929 result = (*tp->tp_getattr)(v, (char *)name_str);
930 }
931 else {
932 PyErr_Format(PyExc_AttributeError,
933 "'%.50s' object has no attribute '%U'",
934 tp->tp_name, name);
935 }
936
937 if (result == NULL) {
938 set_attribute_error_context(v, name);
939 }
940 return result;
Guido van Rossum98ff96a1997-05-20 18:34:44 +0000941}
942
Serhiy Storchakaf320be72018-01-25 10:49:40 +0200943int
944_PyObject_LookupAttr(PyObject *v, PyObject *name, PyObject **result)
INADA Naoki378edee2018-01-16 20:52:41 +0900945{
946 PyTypeObject *tp = Py_TYPE(v);
INADA Naoki378edee2018-01-16 20:52:41 +0900947
948 if (!PyUnicode_Check(name)) {
949 PyErr_Format(PyExc_TypeError,
950 "attribute name must be string, not '%.200s'",
Victor Stinner58ac7002020-02-07 03:04:21 +0100951 Py_TYPE(name)->tp_name);
Serhiy Storchakaf320be72018-01-25 10:49:40 +0200952 *result = NULL;
953 return -1;
INADA Naoki378edee2018-01-16 20:52:41 +0900954 }
955
956 if (tp->tp_getattro == PyObject_GenericGetAttr) {
Serhiy Storchakaf320be72018-01-25 10:49:40 +0200957 *result = _PyObject_GenericGetAttrWithDict(v, name, NULL, 1);
958 if (*result != NULL) {
959 return 1;
960 }
961 if (PyErr_Occurred()) {
962 return -1;
963 }
964 return 0;
INADA Naoki378edee2018-01-16 20:52:41 +0900965 }
966 if (tp->tp_getattro != NULL) {
Serhiy Storchakaf320be72018-01-25 10:49:40 +0200967 *result = (*tp->tp_getattro)(v, name);
INADA Naoki378edee2018-01-16 20:52:41 +0900968 }
969 else if (tp->tp_getattr != NULL) {
970 const char *name_str = PyUnicode_AsUTF8(name);
Serhiy Storchakaf320be72018-01-25 10:49:40 +0200971 if (name_str == NULL) {
972 *result = NULL;
973 return -1;
974 }
975 *result = (*tp->tp_getattr)(v, (char *)name_str);
INADA Naoki378edee2018-01-16 20:52:41 +0900976 }
INADA Naokie76daeb2018-01-26 16:22:51 +0900977 else {
978 *result = NULL;
979 return 0;
980 }
981
Serhiy Storchakaf320be72018-01-25 10:49:40 +0200982 if (*result != NULL) {
983 return 1;
INADA Naoki378edee2018-01-16 20:52:41 +0900984 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +0200985 if (!PyErr_ExceptionMatches(PyExc_AttributeError)) {
986 return -1;
987 }
988 PyErr_Clear();
989 return 0;
990}
991
992int
993_PyObject_LookupAttrId(PyObject *v, _Py_Identifier *name, PyObject **result)
994{
995 PyObject *oname = _PyUnicode_FromId(name); /* borrowed */
996 if (!oname) {
997 *result = NULL;
998 return -1;
999 }
1000 return _PyObject_LookupAttr(v, oname, result);
INADA Naoki378edee2018-01-16 20:52:41 +09001001}
1002
Guido van Rossum98ff96a1997-05-20 18:34:44 +00001003int
Fred Drake100814d2000-07-09 15:48:49 +00001004PyObject_HasAttr(PyObject *v, PyObject *name)
Guido van Rossum98ff96a1997-05-20 18:34:44 +00001005{
Serhiy Storchakaf320be72018-01-25 10:49:40 +02001006 PyObject *res;
1007 if (_PyObject_LookupAttr(v, name, &res) < 0) {
1008 PyErr_Clear();
1009 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001010 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +02001011 if (res == NULL) {
1012 return 0;
1013 }
1014 Py_DECREF(res);
1015 return 1;
Guido van Rossum98ff96a1997-05-20 18:34:44 +00001016}
1017
1018int
Fred Drake100814d2000-07-09 15:48:49 +00001019PyObject_SetAttr(PyObject *v, PyObject *name, PyObject *value)
Guido van Rossum98ff96a1997-05-20 18:34:44 +00001020{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001021 PyTypeObject *tp = Py_TYPE(v);
1022 int err;
Marc-André Lemburge44e5072000-09-18 16:20:57 +00001023
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001024 if (!PyUnicode_Check(name)) {
1025 PyErr_Format(PyExc_TypeError,
1026 "attribute name must be string, not '%.200s'",
Victor Stinner58ac7002020-02-07 03:04:21 +01001027 Py_TYPE(name)->tp_name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001028 return -1;
1029 }
1030 Py_INCREF(name);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001031
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001032 PyUnicode_InternInPlace(&name);
1033 if (tp->tp_setattro != NULL) {
1034 err = (*tp->tp_setattro)(v, name, value);
1035 Py_DECREF(name);
1036 return err;
1037 }
1038 if (tp->tp_setattr != NULL) {
Serhiy Storchaka2a404b62017-01-22 23:07:07 +02001039 const char *name_str = PyUnicode_AsUTF8(name);
Zackery Spytze0dcb852019-04-28 06:58:52 -06001040 if (name_str == NULL) {
1041 Py_DECREF(name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001042 return -1;
Zackery Spytze0dcb852019-04-28 06:58:52 -06001043 }
Serhiy Storchaka2a404b62017-01-22 23:07:07 +02001044 err = (*tp->tp_setattr)(v, (char *)name_str, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001045 Py_DECREF(name);
1046 return err;
1047 }
1048 Py_DECREF(name);
Victor Stinnera93c51e2020-02-07 00:38:59 +01001049 _PyObject_ASSERT(name, Py_REFCNT(name) >= 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001050 if (tp->tp_getattr == NULL && tp->tp_getattro == NULL)
1051 PyErr_Format(PyExc_TypeError,
1052 "'%.100s' object has no attributes "
1053 "(%s .%U)",
1054 tp->tp_name,
1055 value==NULL ? "del" : "assign to",
1056 name);
1057 else
1058 PyErr_Format(PyExc_TypeError,
1059 "'%.100s' object has only read-only attributes "
1060 "(%s .%U)",
1061 tp->tp_name,
1062 value==NULL ? "del" : "assign to",
1063 name);
1064 return -1;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001065}
1066
1067/* Helper to get a pointer to an object's __dict__ slot, if any */
1068
1069PyObject **
1070_PyObject_GetDictPtr(PyObject *obj)
1071{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001072 Py_ssize_t dictoffset;
1073 PyTypeObject *tp = Py_TYPE(obj);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001074
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001075 dictoffset = tp->tp_dictoffset;
1076 if (dictoffset == 0)
1077 return NULL;
1078 if (dictoffset < 0) {
Victor Stinnerc65b3202020-02-07 11:18:33 +01001079 Py_ssize_t tsize = Py_SIZE(obj);
1080 if (tsize < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001081 tsize = -tsize;
Victor Stinnerc65b3202020-02-07 11:18:33 +01001082 }
1083 size_t size = _PyObject_VAR_SIZE(tp, tsize);
Guido van Rossum2eb0b872002-03-01 22:24:49 +00001084
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001085 dictoffset += (long)size;
Victor Stinner24702042018-10-26 17:16:37 +02001086 _PyObject_ASSERT(obj, dictoffset > 0);
1087 _PyObject_ASSERT(obj, dictoffset % SIZEOF_VOID_P == 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001088 }
1089 return (PyObject **) ((char *)obj + dictoffset);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001090}
1091
Tim Peters6d6c1a32001-08-02 04:15:00 +00001092PyObject *
Raymond Hettinger1da1dbf2003-03-17 19:46:11 +00001093PyObject_SelfIter(PyObject *obj)
Raymond Hettinger01538262003-03-17 08:24:35 +00001094{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001095 Py_INCREF(obj);
1096 return obj;
Raymond Hettinger01538262003-03-17 08:24:35 +00001097}
1098
Amaury Forgeot d'Arcf343e012009-01-12 23:58:21 +00001099/* Helper used when the __next__ method is removed from a type:
1100 tp_iternext is never NULL and can be safely called without checking
1101 on every iteration.
1102 */
1103
1104PyObject *
1105_PyObject_NextNotImplemented(PyObject *self)
1106{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001107 PyErr_Format(PyExc_TypeError,
1108 "'%.200s' object is not iterable",
1109 Py_TYPE(self)->tp_name);
1110 return NULL;
Amaury Forgeot d'Arcf343e012009-01-12 23:58:21 +00001111}
1112
Yury Selivanovf2392132016-12-13 19:03:51 -05001113
1114/* Specialized version of _PyObject_GenericGetAttrWithDict
1115 specifically for the LOAD_METHOD opcode.
1116
1117 Return 1 if a method is found, 0 if it's a regular attribute
1118 from __dict__ or something returned by using a descriptor
1119 protocol.
1120
1121 `method` will point to the resolved attribute or NULL. In the
1122 latter case, an error will be set.
1123*/
1124int
1125_PyObject_GetMethod(PyObject *obj, PyObject *name, PyObject **method)
1126{
1127 PyTypeObject *tp = Py_TYPE(obj);
1128 PyObject *descr;
1129 descrgetfunc f = NULL;
1130 PyObject **dictptr, *dict;
1131 PyObject *attr;
1132 int meth_found = 0;
1133
1134 assert(*method == NULL);
1135
1136 if (Py_TYPE(obj)->tp_getattro != PyObject_GenericGetAttr
1137 || !PyUnicode_Check(name)) {
1138 *method = PyObject_GetAttr(obj, name);
1139 return 0;
1140 }
1141
1142 if (tp->tp_dict == NULL && PyType_Ready(tp) < 0)
1143 return 0;
1144
1145 descr = _PyType_Lookup(tp, name);
1146 if (descr != NULL) {
1147 Py_INCREF(descr);
Victor Stinner45ec5b92020-04-08 01:42:27 +02001148 if (_PyType_HasFeature(Py_TYPE(descr), Py_TPFLAGS_METHOD_DESCRIPTOR)) {
Yury Selivanovf2392132016-12-13 19:03:51 -05001149 meth_found = 1;
1150 } else {
Victor Stinner58ac7002020-02-07 03:04:21 +01001151 f = Py_TYPE(descr)->tp_descr_get;
Yury Selivanovf2392132016-12-13 19:03:51 -05001152 if (f != NULL && PyDescr_IsData(descr)) {
Victor Stinner58ac7002020-02-07 03:04:21 +01001153 *method = f(descr, obj, (PyObject *)Py_TYPE(obj));
Yury Selivanovf2392132016-12-13 19:03:51 -05001154 Py_DECREF(descr);
1155 return 0;
1156 }
1157 }
1158 }
1159
1160 dictptr = _PyObject_GetDictPtr(obj);
1161 if (dictptr != NULL && (dict = *dictptr) != NULL) {
1162 Py_INCREF(dict);
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02001163 attr = PyDict_GetItemWithError(dict, name);
Yury Selivanovf2392132016-12-13 19:03:51 -05001164 if (attr != NULL) {
1165 Py_INCREF(attr);
1166 *method = attr;
1167 Py_DECREF(dict);
1168 Py_XDECREF(descr);
1169 return 0;
1170 }
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02001171 else {
1172 Py_DECREF(dict);
1173 if (PyErr_Occurred()) {
1174 Py_XDECREF(descr);
1175 return 0;
1176 }
1177 }
Yury Selivanovf2392132016-12-13 19:03:51 -05001178 }
1179
1180 if (meth_found) {
1181 *method = descr;
1182 return 1;
1183 }
1184
1185 if (f != NULL) {
1186 *method = f(descr, obj, (PyObject *)Py_TYPE(obj));
1187 Py_DECREF(descr);
1188 return 0;
1189 }
1190
1191 if (descr != NULL) {
1192 *method = descr;
1193 return 0;
1194 }
1195
1196 PyErr_Format(PyExc_AttributeError,
1197 "'%.50s' object has no attribute '%U'",
1198 tp->tp_name, name);
Pablo Galindo37494b42021-04-14 02:36:07 +01001199
1200 set_attribute_error_context(obj, name);
Yury Selivanovf2392132016-12-13 19:03:51 -05001201 return 0;
1202}
1203
1204/* Generic GetAttr functions - put these in your tp_[gs]etattro slot. */
Michael W. Hudson1593f502004-09-14 17:09:47 +00001205
Raymond Hettinger01538262003-03-17 08:24:35 +00001206PyObject *
INADA Naoki378edee2018-01-16 20:52:41 +09001207_PyObject_GenericGetAttrWithDict(PyObject *obj, PyObject *name,
1208 PyObject *dict, int suppress)
Tim Peters6d6c1a32001-08-02 04:15:00 +00001209{
Yury Selivanovf2392132016-12-13 19:03:51 -05001210 /* Make sure the logic of _PyObject_GetMethod is in sync with
1211 this method.
INADA Naoki378edee2018-01-16 20:52:41 +09001212
Brett Cannond5837382020-07-17 13:09:21 -07001213 When suppress=1, this function suppresses AttributeError.
Yury Selivanovf2392132016-12-13 19:03:51 -05001214 */
1215
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001216 PyTypeObject *tp = Py_TYPE(obj);
1217 PyObject *descr = NULL;
1218 PyObject *res = NULL;
1219 descrgetfunc f;
1220 Py_ssize_t dictoffset;
1221 PyObject **dictptr;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001222
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001223 if (!PyUnicode_Check(name)){
1224 PyErr_Format(PyExc_TypeError,
1225 "attribute name must be string, not '%.200s'",
Victor Stinner58ac7002020-02-07 03:04:21 +01001226 Py_TYPE(name)->tp_name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001227 return NULL;
1228 }
Serhiy Storchaka55c861f2016-04-17 20:31:51 +03001229 Py_INCREF(name);
Guido van Rossumebca9fc2001-12-04 15:54:53 +00001230
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001231 if (tp->tp_dict == NULL) {
1232 if (PyType_Ready(tp) < 0)
1233 goto done;
1234 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001235
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001236 descr = _PyType_Lookup(tp, name);
Michael W. Hudsonb2c7de42003-08-15 13:07:47 +00001237
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001238 f = NULL;
1239 if (descr != NULL) {
Serhiy Storchaka55c861f2016-04-17 20:31:51 +03001240 Py_INCREF(descr);
Victor Stinner58ac7002020-02-07 03:04:21 +01001241 f = Py_TYPE(descr)->tp_descr_get;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001242 if (f != NULL && PyDescr_IsData(descr)) {
Victor Stinner58ac7002020-02-07 03:04:21 +01001243 res = f(descr, obj, (PyObject *)Py_TYPE(obj));
INADA Naoki378edee2018-01-16 20:52:41 +09001244 if (res == NULL && suppress &&
1245 PyErr_ExceptionMatches(PyExc_AttributeError)) {
1246 PyErr_Clear();
1247 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001248 goto done;
1249 }
1250 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001251
Antoine Pitrou1a9a9d52010-08-28 18:17:03 +00001252 if (dict == NULL) {
1253 /* Inline _PyObject_GetDictPtr */
1254 dictoffset = tp->tp_dictoffset;
1255 if (dictoffset != 0) {
1256 if (dictoffset < 0) {
Victor Stinnerc65b3202020-02-07 11:18:33 +01001257 Py_ssize_t tsize = Py_SIZE(obj);
1258 if (tsize < 0) {
Antoine Pitrou1a9a9d52010-08-28 18:17:03 +00001259 tsize = -tsize;
Victor Stinnerc65b3202020-02-07 11:18:33 +01001260 }
1261 size_t size = _PyObject_VAR_SIZE(tp, tsize);
Victor Stinner24702042018-10-26 17:16:37 +02001262 _PyObject_ASSERT(obj, size <= PY_SSIZE_T_MAX);
Guido van Rossumc66ff442002-08-19 16:50:48 +00001263
Serhiy Storchaka55c861f2016-04-17 20:31:51 +03001264 dictoffset += (Py_ssize_t)size;
Victor Stinner24702042018-10-26 17:16:37 +02001265 _PyObject_ASSERT(obj, dictoffset > 0);
1266 _PyObject_ASSERT(obj, dictoffset % SIZEOF_VOID_P == 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001267 }
Antoine Pitrou1a9a9d52010-08-28 18:17:03 +00001268 dictptr = (PyObject **) ((char *)obj + dictoffset);
1269 dict = *dictptr;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001270 }
1271 }
Antoine Pitrou1a9a9d52010-08-28 18:17:03 +00001272 if (dict != NULL) {
1273 Py_INCREF(dict);
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02001274 res = PyDict_GetItemWithError(dict, name);
Antoine Pitrou1a9a9d52010-08-28 18:17:03 +00001275 if (res != NULL) {
1276 Py_INCREF(res);
Antoine Pitrou1a9a9d52010-08-28 18:17:03 +00001277 Py_DECREF(dict);
1278 goto done;
1279 }
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02001280 else {
1281 Py_DECREF(dict);
1282 if (PyErr_Occurred()) {
1283 if (suppress && PyErr_ExceptionMatches(PyExc_AttributeError)) {
1284 PyErr_Clear();
1285 }
1286 else {
1287 goto done;
1288 }
1289 }
1290 }
Antoine Pitrou1a9a9d52010-08-28 18:17:03 +00001291 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001292
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001293 if (f != NULL) {
1294 res = f(descr, obj, (PyObject *)Py_TYPE(obj));
INADA Naoki378edee2018-01-16 20:52:41 +09001295 if (res == NULL && suppress &&
1296 PyErr_ExceptionMatches(PyExc_AttributeError)) {
1297 PyErr_Clear();
1298 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001299 goto done;
1300 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001301
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001302 if (descr != NULL) {
1303 res = descr;
Victor Stinner2d01dc02012-03-09 00:44:13 +01001304 descr = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001305 goto done;
1306 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001307
INADA Naoki378edee2018-01-16 20:52:41 +09001308 if (!suppress) {
1309 PyErr_Format(PyExc_AttributeError,
1310 "'%.50s' object has no attribute '%U'",
1311 tp->tp_name, name);
1312 }
Guido van Rossumebca9fc2001-12-04 15:54:53 +00001313 done:
Victor Stinner2d01dc02012-03-09 00:44:13 +01001314 Py_XDECREF(descr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001315 Py_DECREF(name);
1316 return res;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001317}
1318
Antoine Pitrou1a9a9d52010-08-28 18:17:03 +00001319PyObject *
1320PyObject_GenericGetAttr(PyObject *obj, PyObject *name)
1321{
INADA Naoki378edee2018-01-16 20:52:41 +09001322 return _PyObject_GenericGetAttrWithDict(obj, name, NULL, 0);
Antoine Pitrou1a9a9d52010-08-28 18:17:03 +00001323}
1324
Tim Peters6d6c1a32001-08-02 04:15:00 +00001325int
Antoine Pitrou1a9a9d52010-08-28 18:17:03 +00001326_PyObject_GenericSetAttrWithDict(PyObject *obj, PyObject *name,
1327 PyObject *value, PyObject *dict)
Tim Peters6d6c1a32001-08-02 04:15:00 +00001328{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001329 PyTypeObject *tp = Py_TYPE(obj);
1330 PyObject *descr;
1331 descrsetfunc f;
1332 PyObject **dictptr;
1333 int res = -1;
Guido van Rossumebca9fc2001-12-04 15:54:53 +00001334
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001335 if (!PyUnicode_Check(name)){
1336 PyErr_Format(PyExc_TypeError,
1337 "attribute name must be string, not '%.200s'",
Victor Stinner58ac7002020-02-07 03:04:21 +01001338 Py_TYPE(name)->tp_name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001339 return -1;
1340 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001341
Benjamin Peterson74529ad2012-03-09 07:25:32 -08001342 if (tp->tp_dict == NULL && PyType_Ready(tp) < 0)
1343 return -1;
1344
1345 Py_INCREF(name);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001346
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001347 descr = _PyType_Lookup(tp, name);
Victor Stinner2d01dc02012-03-09 00:44:13 +01001348
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001349 if (descr != NULL) {
Serhiy Storchaka55c861f2016-04-17 20:31:51 +03001350 Py_INCREF(descr);
Victor Stinner58ac7002020-02-07 03:04:21 +01001351 f = Py_TYPE(descr)->tp_descr_set;
Serhiy Storchaka55c861f2016-04-17 20:31:51 +03001352 if (f != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001353 res = f(descr, obj, value);
1354 goto done;
1355 }
1356 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001357
Steve Dowerb82e17e2019-05-23 08:45:22 -07001358 /* XXX [Steve Dower] These are really noisy - worth it? */
1359 /*if (PyType_Check(obj) || PyModule_Check(obj)) {
1360 if (value && PySys_Audit("object.__setattr__", "OOO", obj, name, value) < 0)
1361 return -1;
1362 if (!value && PySys_Audit("object.__delattr__", "OO", obj, name) < 0)
1363 return -1;
1364 }*/
1365
Antoine Pitrou1a9a9d52010-08-28 18:17:03 +00001366 if (dict == NULL) {
1367 dictptr = _PyObject_GetDictPtr(obj);
Serhiy Storchaka55c861f2016-04-17 20:31:51 +03001368 if (dictptr == NULL) {
1369 if (descr == NULL) {
1370 PyErr_Format(PyExc_AttributeError,
1371 "'%.100s' object has no attribute '%U'",
1372 tp->tp_name, name);
1373 }
1374 else {
1375 PyErr_Format(PyExc_AttributeError,
1376 "'%.50s' object attribute '%U' is read-only",
1377 tp->tp_name, name);
1378 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001379 goto done;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001380 }
Serhiy Storchaka55c861f2016-04-17 20:31:51 +03001381 res = _PyObjectDict_SetItem(tp, dictptr, name, value);
Antoine Pitrou1a9a9d52010-08-28 18:17:03 +00001382 }
Serhiy Storchaka55c861f2016-04-17 20:31:51 +03001383 else {
Antoine Pitrou1a9a9d52010-08-28 18:17:03 +00001384 Py_INCREF(dict);
1385 if (value == NULL)
1386 res = PyDict_DelItem(dict, name);
1387 else
1388 res = PyDict_SetItem(dict, name, value);
Benjamin Peterson74529ad2012-03-09 07:25:32 -08001389 Py_DECREF(dict);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001390 }
Serhiy Storchaka55c861f2016-04-17 20:31:51 +03001391 if (res < 0 && PyErr_ExceptionMatches(PyExc_KeyError))
1392 PyErr_SetObject(PyExc_AttributeError, name);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001393
Guido van Rossumebca9fc2001-12-04 15:54:53 +00001394 done:
Victor Stinner2d01dc02012-03-09 00:44:13 +01001395 Py_XDECREF(descr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001396 Py_DECREF(name);
1397 return res;
Guido van Rossum98ff96a1997-05-20 18:34:44 +00001398}
1399
Antoine Pitrou1a9a9d52010-08-28 18:17:03 +00001400int
1401PyObject_GenericSetAttr(PyObject *obj, PyObject *name, PyObject *value)
1402{
1403 return _PyObject_GenericSetAttrWithDict(obj, name, value, NULL);
1404}
1405
Benjamin Peterson8eb12692012-02-19 19:59:10 -05001406int
1407PyObject_GenericSetDict(PyObject *obj, PyObject *value, void *context)
1408{
Serhiy Storchaka576f1322016-01-05 21:27:54 +02001409 PyObject **dictptr = _PyObject_GetDictPtr(obj);
Benjamin Peterson8eb12692012-02-19 19:59:10 -05001410 if (dictptr == NULL) {
1411 PyErr_SetString(PyExc_AttributeError,
1412 "This object has no __dict__");
1413 return -1;
1414 }
1415 if (value == NULL) {
1416 PyErr_SetString(PyExc_TypeError, "cannot delete __dict__");
1417 return -1;
1418 }
1419 if (!PyDict_Check(value)) {
1420 PyErr_Format(PyExc_TypeError,
1421 "__dict__ must be set to a dictionary, "
1422 "not a '%.200s'", Py_TYPE(value)->tp_name);
1423 return -1;
1424 }
Serhiy Storchaka576f1322016-01-05 21:27:54 +02001425 Py_INCREF(value);
Serhiy Storchakaec397562016-04-06 09:50:03 +03001426 Py_XSETREF(*dictptr, value);
Benjamin Peterson8eb12692012-02-19 19:59:10 -05001427 return 0;
1428}
1429
Antoine Pitrou1a9a9d52010-08-28 18:17:03 +00001430
Stefan Pochmannf90dc362020-10-07 16:12:52 +02001431/* Test a value used as condition, e.g., in a while or if statement.
Guido van Rossum6ac258d1993-05-12 08:24:20 +00001432 Return -1 if an error occurred */
1433
1434int
Fred Drake100814d2000-07-09 15:48:49 +00001435PyObject_IsTrue(PyObject *v)
Guido van Rossum6ac258d1993-05-12 08:24:20 +00001436{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001437 Py_ssize_t res;
1438 if (v == Py_True)
1439 return 1;
1440 if (v == Py_False)
1441 return 0;
1442 if (v == Py_None)
1443 return 0;
Victor Stinner58ac7002020-02-07 03:04:21 +01001444 else if (Py_TYPE(v)->tp_as_number != NULL &&
1445 Py_TYPE(v)->tp_as_number->nb_bool != NULL)
1446 res = (*Py_TYPE(v)->tp_as_number->nb_bool)(v);
1447 else if (Py_TYPE(v)->tp_as_mapping != NULL &&
1448 Py_TYPE(v)->tp_as_mapping->mp_length != NULL)
1449 res = (*Py_TYPE(v)->tp_as_mapping->mp_length)(v);
1450 else if (Py_TYPE(v)->tp_as_sequence != NULL &&
1451 Py_TYPE(v)->tp_as_sequence->sq_length != NULL)
1452 res = (*Py_TYPE(v)->tp_as_sequence->sq_length)(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001453 else
1454 return 1;
1455 /* if it is negative, it should be either -1 or -2 */
1456 return (res > 0) ? 1 : Py_SAFE_DOWNCAST(res, Py_ssize_t, int);
Guido van Rossum6ac258d1993-05-12 08:24:20 +00001457}
1458
Tim Peters803526b2002-07-07 05:13:56 +00001459/* equivalent of 'not v'
Guido van Rossumc3d3f961998-04-09 17:53:59 +00001460 Return -1 if an error occurred */
1461
1462int
Fred Drake100814d2000-07-09 15:48:49 +00001463PyObject_Not(PyObject *v)
Guido van Rossumc3d3f961998-04-09 17:53:59 +00001464{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001465 int res;
1466 res = PyObject_IsTrue(v);
1467 if (res < 0)
1468 return res;
1469 return res == 0;
Guido van Rossumc3d3f961998-04-09 17:53:59 +00001470}
1471
Guido van Rossum49b11fe1995-01-26 00:38:22 +00001472/* Test whether an object can be called */
1473
1474int
Fred Drake100814d2000-07-09 15:48:49 +00001475PyCallable_Check(PyObject *x)
Guido van Rossum49b11fe1995-01-26 00:38:22 +00001476{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001477 if (x == NULL)
1478 return 0;
Victor Stinner58ac7002020-02-07 03:04:21 +01001479 return Py_TYPE(x)->tp_call != NULL;
Guido van Rossum49b11fe1995-01-26 00:38:22 +00001480}
1481
Tim Peters7eea37e2001-09-04 22:08:56 +00001482
Georg Brandle32b4222007-03-10 22:13:27 +00001483/* Helper for PyObject_Dir without arguments: returns the local scope. */
1484static PyObject *
Guido van Rossumad7d8d12007-04-13 01:39:34 +00001485_dir_locals(void)
Tim Peters305b5852001-09-17 02:38:46 +00001486{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001487 PyObject *names;
Victor Stinner41bb43a2013-10-29 01:19:37 +01001488 PyObject *locals;
Tim Peters305b5852001-09-17 02:38:46 +00001489
Victor Stinner41bb43a2013-10-29 01:19:37 +01001490 locals = PyEval_GetLocals();
1491 if (locals == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001492 return NULL;
Tim Peters305b5852001-09-17 02:38:46 +00001493
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001494 names = PyMapping_Keys(locals);
1495 if (!names)
1496 return NULL;
1497 if (!PyList_Check(names)) {
1498 PyErr_Format(PyExc_TypeError,
1499 "dir(): expected keys() of locals to be a list, "
1500 "not '%.200s'", Py_TYPE(names)->tp_name);
1501 Py_DECREF(names);
1502 return NULL;
1503 }
Benjamin Peterson3bbb7222011-06-11 16:12:08 -05001504 if (PyList_Sort(names)) {
1505 Py_DECREF(names);
1506 return NULL;
1507 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001508 /* the locals don't need to be DECREF'd */
1509 return names;
Georg Brandle32b4222007-03-10 22:13:27 +00001510}
1511
Benjamin Peterson82b00c12011-05-24 11:09:06 -05001512/* Helper for PyObject_Dir: object introspection. */
Georg Brandle32b4222007-03-10 22:13:27 +00001513static PyObject *
1514_dir_object(PyObject *obj)
1515{
Benjamin Peterson3bbb7222011-06-11 16:12:08 -05001516 PyObject *result, *sorted;
Benjamin Petersonce798522012-01-22 11:24:29 -05001517 PyObject *dirfunc = _PyObject_LookupSpecial(obj, &PyId___dir__);
Georg Brandle32b4222007-03-10 22:13:27 +00001518
Victor Stinner24702042018-10-26 17:16:37 +02001519 assert(obj != NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001520 if (dirfunc == NULL) {
Benjamin Peterson82b00c12011-05-24 11:09:06 -05001521 if (!PyErr_Occurred())
1522 PyErr_SetString(PyExc_TypeError, "object does not provide __dir__");
1523 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001524 }
Benjamin Peterson82b00c12011-05-24 11:09:06 -05001525 /* use __dir__ */
Victor Stinnerf17c3de2016-12-06 18:46:19 +01001526 result = _PyObject_CallNoArg(dirfunc);
Benjamin Peterson82b00c12011-05-24 11:09:06 -05001527 Py_DECREF(dirfunc);
1528 if (result == NULL)
1529 return NULL;
Benjamin Peterson3bbb7222011-06-11 16:12:08 -05001530 /* return sorted(result) */
1531 sorted = PySequence_List(result);
1532 Py_DECREF(result);
1533 if (sorted == NULL)
1534 return NULL;
1535 if (PyList_Sort(sorted)) {
1536 Py_DECREF(sorted);
1537 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001538 }
Benjamin Peterson3bbb7222011-06-11 16:12:08 -05001539 return sorted;
Georg Brandle32b4222007-03-10 22:13:27 +00001540}
1541
1542/* Implementation of dir() -- if obj is NULL, returns the names in the current
1543 (local) scope. Otherwise, performs introspection of the object: returns a
1544 sorted list of attribute names (supposedly) accessible from the object
1545*/
1546PyObject *
1547PyObject_Dir(PyObject *obj)
1548{
Benjamin Peterson3bbb7222011-06-11 16:12:08 -05001549 return (obj == NULL) ? _dir_locals() : _dir_object(obj);
Tim Peters7eea37e2001-09-04 22:08:56 +00001550}
Guido van Rossum49b11fe1995-01-26 00:38:22 +00001551
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001552/*
Raymond Hettinger66d2be82011-07-28 09:55:13 -07001553None is a non-NULL undefined value.
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001554There is (and should be!) no way to create other objects of this type,
Guido van Rossum3f5da241990-12-20 15:06:42 +00001555so there is exactly one (which is indestructible, by the way).
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001556*/
1557
Guido van Rossum0c182a11992-03-27 17:26:13 +00001558/* ARGSUSED */
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001559static PyObject *
Fred Drake100814d2000-07-09 15:48:49 +00001560none_repr(PyObject *op)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001561{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001562 return PyUnicode_FromString("None");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001563}
1564
Barry Warsaw9bf16442001-01-23 16:24:35 +00001565/* ARGUSED */
Victor Stinner2a4903f2020-01-30 13:09:11 +01001566static void _Py_NO_RETURN
Tim Peters803526b2002-07-07 05:13:56 +00001567none_dealloc(PyObject* ignore)
Barry Warsaw9bf16442001-01-23 16:24:35 +00001568{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001569 /* This should never get called, but we also don't want to SEGV if
1570 * we accidentally decref None out of existence.
1571 */
1572 Py_FatalError("deallocating None");
Barry Warsaw9bf16442001-01-23 16:24:35 +00001573}
1574
Benjamin Petersonc4607ae2011-07-29 18:19:43 -05001575static PyObject *
1576none_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
1577{
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +02001578 if (PyTuple_GET_SIZE(args) || (kwargs && PyDict_GET_SIZE(kwargs))) {
Benjamin Petersonc4607ae2011-07-29 18:19:43 -05001579 PyErr_SetString(PyExc_TypeError, "NoneType takes no arguments");
1580 return NULL;
1581 }
1582 Py_RETURN_NONE;
1583}
1584
Raymond Hettinger66d2be82011-07-28 09:55:13 -07001585static int
1586none_bool(PyObject *v)
1587{
1588 return 0;
1589}
1590
1591static PyNumberMethods none_as_number = {
1592 0, /* nb_add */
1593 0, /* nb_subtract */
1594 0, /* nb_multiply */
1595 0, /* nb_remainder */
1596 0, /* nb_divmod */
1597 0, /* nb_power */
1598 0, /* nb_negative */
1599 0, /* nb_positive */
1600 0, /* nb_absolute */
1601 (inquiry)none_bool, /* nb_bool */
1602 0, /* nb_invert */
1603 0, /* nb_lshift */
1604 0, /* nb_rshift */
1605 0, /* nb_and */
1606 0, /* nb_xor */
1607 0, /* nb_or */
1608 0, /* nb_int */
1609 0, /* nb_reserved */
1610 0, /* nb_float */
1611 0, /* nb_inplace_add */
1612 0, /* nb_inplace_subtract */
1613 0, /* nb_inplace_multiply */
1614 0, /* nb_inplace_remainder */
1615 0, /* nb_inplace_power */
1616 0, /* nb_inplace_lshift */
1617 0, /* nb_inplace_rshift */
1618 0, /* nb_inplace_and */
1619 0, /* nb_inplace_xor */
1620 0, /* nb_inplace_or */
1621 0, /* nb_floor_divide */
1622 0, /* nb_true_divide */
1623 0, /* nb_inplace_floor_divide */
1624 0, /* nb_inplace_true_divide */
1625 0, /* nb_index */
1626};
Barry Warsaw9bf16442001-01-23 16:24:35 +00001627
Alexandre Vassalotti65846c62013-11-30 17:55:48 -08001628PyTypeObject _PyNone_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001629 PyVarObject_HEAD_INIT(&PyType_Type, 0)
1630 "NoneType",
1631 0,
1632 0,
1633 none_dealloc, /*tp_dealloc*/ /*never called*/
Jeroen Demeyer530f5062019-05-31 04:13:39 +02001634 0, /*tp_vectorcall_offset*/
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001635 0, /*tp_getattr*/
1636 0, /*tp_setattr*/
Jeroen Demeyer530f5062019-05-31 04:13:39 +02001637 0, /*tp_as_async*/
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001638 none_repr, /*tp_repr*/
Raymond Hettinger66d2be82011-07-28 09:55:13 -07001639 &none_as_number, /*tp_as_number*/
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001640 0, /*tp_as_sequence*/
1641 0, /*tp_as_mapping*/
1642 0, /*tp_hash */
Benjamin Petersonc4607ae2011-07-29 18:19:43 -05001643 0, /*tp_call */
1644 0, /*tp_str */
1645 0, /*tp_getattro */
1646 0, /*tp_setattro */
1647 0, /*tp_as_buffer */
1648 Py_TPFLAGS_DEFAULT, /*tp_flags */
1649 0, /*tp_doc */
1650 0, /*tp_traverse */
1651 0, /*tp_clear */
1652 0, /*tp_richcompare */
1653 0, /*tp_weaklistoffset */
1654 0, /*tp_iter */
1655 0, /*tp_iternext */
1656 0, /*tp_methods */
1657 0, /*tp_members */
1658 0, /*tp_getset */
1659 0, /*tp_base */
1660 0, /*tp_dict */
1661 0, /*tp_descr_get */
1662 0, /*tp_descr_set */
1663 0, /*tp_dictoffset */
1664 0, /*tp_init */
1665 0, /*tp_alloc */
1666 none_new, /*tp_new */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001667};
1668
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001669PyObject _Py_NoneStruct = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001670 _PyObject_EXTRA_INIT
Alexandre Vassalotti65846c62013-11-30 17:55:48 -08001671 1, &_PyNone_Type
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001672};
1673
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +00001674/* NotImplemented is an object that can be used to signal that an
1675 operation is not implemented for the given type combination. */
1676
1677static PyObject *
1678NotImplemented_repr(PyObject *op)
1679{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001680 return PyUnicode_FromString("NotImplemented");
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +00001681}
1682
Benjamin Peterson18d7d7a2011-07-29 18:27:44 -05001683static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05301684NotImplemented_reduce(PyObject *op, PyObject *Py_UNUSED(ignored))
Alexandre Vassalottic49477b2013-11-24 02:53:45 -08001685{
1686 return PyUnicode_FromString("NotImplemented");
1687}
1688
1689static PyMethodDef notimplemented_methods[] = {
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05301690 {"__reduce__", NotImplemented_reduce, METH_NOARGS, NULL},
Alexandre Vassalottic49477b2013-11-24 02:53:45 -08001691 {NULL, NULL}
1692};
1693
1694static PyObject *
Benjamin Peterson18d7d7a2011-07-29 18:27:44 -05001695notimplemented_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
1696{
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +02001697 if (PyTuple_GET_SIZE(args) || (kwargs && PyDict_GET_SIZE(kwargs))) {
Benjamin Peterson18d7d7a2011-07-29 18:27:44 -05001698 PyErr_SetString(PyExc_TypeError, "NotImplementedType takes no arguments");
1699 return NULL;
1700 }
Brian Curtindfc80e32011-08-10 20:28:54 -05001701 Py_RETURN_NOTIMPLEMENTED;
Benjamin Peterson18d7d7a2011-07-29 18:27:44 -05001702}
1703
Victor Stinner2a4903f2020-01-30 13:09:11 +01001704static void _Py_NO_RETURN
Armin Ronacher226b1db2012-10-06 14:28:58 +02001705notimplemented_dealloc(PyObject* ignore)
1706{
1707 /* This should never get called, but we also don't want to SEGV if
1708 * we accidentally decref NotImplemented out of existence.
1709 */
1710 Py_FatalError("deallocating NotImplemented");
1711}
1712
MojoVampire469325c2020-03-03 18:50:17 +00001713static int
1714notimplemented_bool(PyObject *v)
1715{
1716 if (PyErr_WarnEx(PyExc_DeprecationWarning,
1717 "NotImplemented should not be used in a boolean context",
1718 1) < 0)
1719 {
1720 return -1;
1721 }
1722 return 1;
1723}
1724
1725static PyNumberMethods notimplemented_as_number = {
1726 .nb_bool = notimplemented_bool,
1727};
1728
Alexandre Vassalotti65846c62013-11-30 17:55:48 -08001729PyTypeObject _PyNotImplemented_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001730 PyVarObject_HEAD_INIT(&PyType_Type, 0)
1731 "NotImplementedType",
1732 0,
1733 0,
Armin Ronacher226b1db2012-10-06 14:28:58 +02001734 notimplemented_dealloc, /*tp_dealloc*/ /*never called*/
Jeroen Demeyer530f5062019-05-31 04:13:39 +02001735 0, /*tp_vectorcall_offset*/
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001736 0, /*tp_getattr*/
1737 0, /*tp_setattr*/
Jeroen Demeyer530f5062019-05-31 04:13:39 +02001738 0, /*tp_as_async*/
MojoVampire469325c2020-03-03 18:50:17 +00001739 NotImplemented_repr, /*tp_repr*/
1740 &notimplemented_as_number, /*tp_as_number*/
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001741 0, /*tp_as_sequence*/
1742 0, /*tp_as_mapping*/
1743 0, /*tp_hash */
Benjamin Peterson18d7d7a2011-07-29 18:27:44 -05001744 0, /*tp_call */
1745 0, /*tp_str */
1746 0, /*tp_getattro */
1747 0, /*tp_setattro */
1748 0, /*tp_as_buffer */
1749 Py_TPFLAGS_DEFAULT, /*tp_flags */
1750 0, /*tp_doc */
1751 0, /*tp_traverse */
1752 0, /*tp_clear */
1753 0, /*tp_richcompare */
1754 0, /*tp_weaklistoffset */
1755 0, /*tp_iter */
1756 0, /*tp_iternext */
Alexandre Vassalottic49477b2013-11-24 02:53:45 -08001757 notimplemented_methods, /*tp_methods */
Benjamin Peterson18d7d7a2011-07-29 18:27:44 -05001758 0, /*tp_members */
1759 0, /*tp_getset */
1760 0, /*tp_base */
1761 0, /*tp_dict */
1762 0, /*tp_descr_get */
1763 0, /*tp_descr_set */
1764 0, /*tp_dictoffset */
1765 0, /*tp_init */
1766 0, /*tp_alloc */
1767 notimplemented_new, /*tp_new */
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +00001768};
1769
1770PyObject _Py_NotImplementedStruct = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001771 _PyObject_EXTRA_INIT
Alexandre Vassalotti65846c62013-11-30 17:55:48 -08001772 1, &_PyNotImplemented_Type
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +00001773};
1774
Victor Stinner331a6a52019-05-27 16:39:22 +02001775PyStatus
Victor Stinnerab672812019-01-23 15:04:40 +01001776_PyTypes_Init(void)
Guido van Rossumba21a492001-08-16 08:17:26 +00001777{
Victor Stinner7a1f6c22020-01-30 09:02:14 +01001778 PyStatus status = _PyTypes_InitSlotDefs();
1779 if (_PyStatus_EXCEPTION(status)) {
1780 return status;
1781 }
1782
Victor Stinnera5180992021-04-08 00:12:38 +02001783#define INIT_TYPE(TYPE) \
Victor Stinner6d43f6f2019-01-22 21:18:05 +01001784 do { \
Victor Stinnera5180992021-04-08 00:12:38 +02001785 if (PyType_Ready(&(TYPE)) < 0) { \
1786 return _PyStatus_ERR("Can't initialize " #TYPE " type"); \
Victor Stinner6d43f6f2019-01-22 21:18:05 +01001787 } \
1788 } while (0)
Victor Stinner5a1bb4e2014-06-02 14:10:59 +02001789
Victor Stinnera5180992021-04-08 00:12:38 +02001790 // Base types
1791 INIT_TYPE(PyBaseObject_Type);
1792 INIT_TYPE(PyType_Type);
1793 assert(PyBaseObject_Type.tp_base == NULL);
1794 assert(PyType_Type.tp_base == &PyBaseObject_Type);
Guido van Rossumba21a492001-08-16 08:17:26 +00001795
Victor Stinnera5180992021-04-08 00:12:38 +02001796 // All other static types
Victor Stinnerdf5dc1c2021-04-08 00:47:55 +02001797 INIT_TYPE(PyAsyncGen_Type);
Victor Stinnera5180992021-04-08 00:12:38 +02001798 INIT_TYPE(PyBool_Type);
Victor Stinnerdf5dc1c2021-04-08 00:47:55 +02001799 INIT_TYPE(PyByteArrayIter_Type);
Victor Stinnera5180992021-04-08 00:12:38 +02001800 INIT_TYPE(PyByteArray_Type);
Victor Stinnerdf5dc1c2021-04-08 00:47:55 +02001801 INIT_TYPE(PyBytesIter_Type);
Victor Stinnera5180992021-04-08 00:12:38 +02001802 INIT_TYPE(PyBytes_Type);
1803 INIT_TYPE(PyCFunction_Type);
1804 INIT_TYPE(PyCMethod_Type);
1805 INIT_TYPE(PyCallIter_Type);
1806 INIT_TYPE(PyCapsule_Type);
1807 INIT_TYPE(PyCell_Type);
1808 INIT_TYPE(PyClassMethodDescr_Type);
Victor Stinnerdf5dc1c2021-04-08 00:47:55 +02001809 INIT_TYPE(PyClassMethod_Type);
Victor Stinnera5180992021-04-08 00:12:38 +02001810 INIT_TYPE(PyCode_Type);
1811 INIT_TYPE(PyComplex_Type);
1812 INIT_TYPE(PyCoro_Type);
1813 INIT_TYPE(PyDictItems_Type);
Victor Stinnerdf5dc1c2021-04-08 00:47:55 +02001814 INIT_TYPE(PyDictIterItem_Type);
1815 INIT_TYPE(PyDictIterKey_Type);
1816 INIT_TYPE(PyDictIterValue_Type);
Victor Stinnera5180992021-04-08 00:12:38 +02001817 INIT_TYPE(PyDictKeys_Type);
1818 INIT_TYPE(PyDictProxy_Type);
1819 INIT_TYPE(PyDictRevIterItem_Type);
1820 INIT_TYPE(PyDictRevIterKey_Type);
1821 INIT_TYPE(PyDictRevIterValue_Type);
1822 INIT_TYPE(PyDictValues_Type);
1823 INIT_TYPE(PyDict_Type);
1824 INIT_TYPE(PyEllipsis_Type);
1825 INIT_TYPE(PyEnum_Type);
1826 INIT_TYPE(PyFloat_Type);
1827 INIT_TYPE(PyFrame_Type);
1828 INIT_TYPE(PyFrozenSet_Type);
1829 INIT_TYPE(PyFunction_Type);
1830 INIT_TYPE(PyGen_Type);
1831 INIT_TYPE(PyGetSetDescr_Type);
1832 INIT_TYPE(PyInstanceMethod_Type);
Victor Stinnerdf5dc1c2021-04-08 00:47:55 +02001833 INIT_TYPE(PyListIter_Type);
1834 INIT_TYPE(PyListRevIter_Type);
Victor Stinnera5180992021-04-08 00:12:38 +02001835 INIT_TYPE(PyList_Type);
1836 INIT_TYPE(PyLongRangeIter_Type);
1837 INIT_TYPE(PyLong_Type);
1838 INIT_TYPE(PyMemberDescr_Type);
1839 INIT_TYPE(PyMemoryView_Type);
1840 INIT_TYPE(PyMethodDescr_Type);
1841 INIT_TYPE(PyMethod_Type);
Victor Stinnerdf5dc1c2021-04-08 00:47:55 +02001842 INIT_TYPE(PyModuleDef_Type);
1843 INIT_TYPE(PyModule_Type);
Victor Stinnera5180992021-04-08 00:12:38 +02001844 INIT_TYPE(PyODictItems_Type);
1845 INIT_TYPE(PyODictIter_Type);
1846 INIT_TYPE(PyODictKeys_Type);
1847 INIT_TYPE(PyODictValues_Type);
1848 INIT_TYPE(PyODict_Type);
1849 INIT_TYPE(PyPickleBuffer_Type);
1850 INIT_TYPE(PyProperty_Type);
Victor Stinnerdf5dc1c2021-04-08 00:47:55 +02001851 INIT_TYPE(PyRangeIter_Type);
Victor Stinnera5180992021-04-08 00:12:38 +02001852 INIT_TYPE(PyRange_Type);
1853 INIT_TYPE(PyReversed_Type);
Victor Stinnerdf5dc1c2021-04-08 00:47:55 +02001854 INIT_TYPE(PySTEntry_Type);
Victor Stinnera5180992021-04-08 00:12:38 +02001855 INIT_TYPE(PySeqIter_Type);
Victor Stinnerdf5dc1c2021-04-08 00:47:55 +02001856 INIT_TYPE(PySetIter_Type);
Victor Stinnera5180992021-04-08 00:12:38 +02001857 INIT_TYPE(PySet_Type);
1858 INIT_TYPE(PySlice_Type);
1859 INIT_TYPE(PyStaticMethod_Type);
1860 INIT_TYPE(PyStdPrinter_Type);
1861 INIT_TYPE(PySuper_Type);
1862 INIT_TYPE(PyTraceBack_Type);
Victor Stinnerdf5dc1c2021-04-08 00:47:55 +02001863 INIT_TYPE(PyTupleIter_Type);
Victor Stinnera5180992021-04-08 00:12:38 +02001864 INIT_TYPE(PyTuple_Type);
Victor Stinnerdf5dc1c2021-04-08 00:47:55 +02001865 INIT_TYPE(PyUnicodeIter_Type);
Victor Stinnera5180992021-04-08 00:12:38 +02001866 INIT_TYPE(PyUnicode_Type);
1867 INIT_TYPE(PyWrapperDescr_Type);
Victor Stinnerdf5dc1c2021-04-08 00:47:55 +02001868 INIT_TYPE(Py_GenericAliasType);
Victor Stinnerb98eba52021-04-08 09:58:15 +02001869 INIT_TYPE(_PyAnextAwaitable_Type);
Victor Stinnerdf5dc1c2021-04-08 00:47:55 +02001870 INIT_TYPE(_PyAsyncGenASend_Type);
1871 INIT_TYPE(_PyAsyncGenAThrow_Type);
1872 INIT_TYPE(_PyAsyncGenWrappedValue_Type);
Victor Stinnera5180992021-04-08 00:12:38 +02001873 INIT_TYPE(_PyCoroWrapper_Type);
1874 INIT_TYPE(_PyInterpreterID_Type);
1875 INIT_TYPE(_PyManagedBuffer_Type);
1876 INIT_TYPE(_PyMethodWrapper_Type);
1877 INIT_TYPE(_PyNamespace_Type);
1878 INIT_TYPE(_PyNone_Type);
1879 INIT_TYPE(_PyNotImplemented_Type);
1880 INIT_TYPE(_PyWeakref_CallableProxyType);
1881 INIT_TYPE(_PyWeakref_ProxyType);
1882 INIT_TYPE(_PyWeakref_RefType);
Miss Islington (bot)03aad302021-07-17 14:10:21 -07001883 INIT_TYPE(_PyUnion_Type);
Victor Stinnera5180992021-04-08 00:12:38 +02001884
1885 return _PyStatus_OK();
Victor Stinner6d43f6f2019-01-22 21:18:05 +01001886#undef INIT_TYPE
Guido van Rossumba21a492001-08-16 08:17:26 +00001887}
1888
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001889
Victor Stinner40e547d2020-02-05 01:11:10 +01001890void
1891_Py_NewReference(PyObject *op)
1892{
1893 if (_Py_tracemalloc_config.tracing) {
1894 _PyTraceMalloc_NewReference(op);
1895 }
1896#ifdef Py_REF_DEBUG
1897 _Py_RefTotal++;
1898#endif
Victor Stinnerc86a1122020-02-07 01:24:29 +01001899 Py_SET_REFCNT(op, 1);
Victor Stinner40e547d2020-02-05 01:11:10 +01001900#ifdef Py_TRACE_REFS
1901 _Py_AddToAllObjects(op, 1);
1902#endif
1903}
1904
1905
Guido van Rossum84a90321996-05-22 16:34:47 +00001906#ifdef Py_TRACE_REFS
Guido van Rossumaacdc9d1996-08-12 21:32:12 +00001907void
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02001908_Py_ForgetReference(PyObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001909{
Victor Stinnera93c51e2020-02-07 00:38:59 +01001910 if (Py_REFCNT(op) < 0) {
Victor Stinner5eb8bff2020-01-30 09:01:07 +01001911 _PyObject_ASSERT_FAILED_MSG(op, "negative refcnt");
1912 }
1913
1914 if (op == &refchain ||
1915 op->_ob_prev->_ob_next != op || op->_ob_next->_ob_prev != op)
1916 {
1917 _PyObject_ASSERT_FAILED_MSG(op, "invalid object chain");
1918 }
1919
Guido van Rossumbffd6832000-01-20 22:32:56 +00001920#ifdef SLOW_UNREF_CHECK
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02001921 PyObject *p;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001922 for (p = refchain._ob_next; p != &refchain; p = p->_ob_next) {
Victor Stinner5eb8bff2020-01-30 09:01:07 +01001923 if (p == op) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001924 break;
Victor Stinner5eb8bff2020-01-30 09:01:07 +01001925 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001926 }
Victor Stinner5eb8bff2020-01-30 09:01:07 +01001927 if (p == &refchain) {
1928 /* Not found */
1929 _PyObject_ASSERT_FAILED_MSG(op,
1930 "object not found in the objects list");
1931 }
Guido van Rossum2e8f6141992-09-03 20:32:55 +00001932#endif
Victor Stinner5eb8bff2020-01-30 09:01:07 +01001933
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001934 op->_ob_next->_ob_prev = op->_ob_prev;
1935 op->_ob_prev->_ob_next = op->_ob_next;
1936 op->_ob_next = op->_ob_prev = NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001937}
1938
Tim Peters269b2a62003-04-17 19:52:29 +00001939/* Print all live objects. Because PyObject_Print is called, the
1940 * interpreter must be in a healthy state.
1941 */
Guido van Rossumaacdc9d1996-08-12 21:32:12 +00001942void
Fred Drake100814d2000-07-09 15:48:49 +00001943_Py_PrintReferences(FILE *fp)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001944{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001945 PyObject *op;
1946 fprintf(fp, "Remaining objects:\n");
1947 for (op = refchain._ob_next; op != &refchain; op = op->_ob_next) {
Victor Stinnerd36cf5f2020-06-10 18:38:05 +02001948 fprintf(fp, "%p [%zd] ", (void *)op, Py_REFCNT(op));
1949 if (PyObject_Print(op, fp, 0) != 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001950 PyErr_Clear();
Victor Stinnerd36cf5f2020-06-10 18:38:05 +02001951 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001952 putc('\n', fp);
1953 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001954}
1955
Tim Peters269b2a62003-04-17 19:52:29 +00001956/* Print the addresses of all live objects. Unlike _Py_PrintReferences, this
1957 * doesn't make any calls to the Python C API, so is always safe to call.
1958 */
1959void
1960_Py_PrintReferenceAddresses(FILE *fp)
1961{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001962 PyObject *op;
1963 fprintf(fp, "Remaining object addresses:\n");
1964 for (op = refchain._ob_next; op != &refchain; op = op->_ob_next)
Victor Stinnerd36cf5f2020-06-10 18:38:05 +02001965 fprintf(fp, "%p [%zd] %s\n", (void *)op,
Victor Stinnera93c51e2020-02-07 00:38:59 +01001966 Py_REFCNT(op), Py_TYPE(op)->tp_name);
Tim Peters269b2a62003-04-17 19:52:29 +00001967}
1968
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +00001969PyObject *
Fred Drake100814d2000-07-09 15:48:49 +00001970_Py_GetObjects(PyObject *self, PyObject *args)
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +00001971{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001972 int i, n;
1973 PyObject *t = NULL;
1974 PyObject *res, *op;
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +00001975
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001976 if (!PyArg_ParseTuple(args, "i|O", &n, &t))
1977 return NULL;
1978 op = refchain._ob_next;
1979 res = PyList_New(0);
1980 if (res == NULL)
1981 return NULL;
1982 for (i = 0; (n == 0 || i < n) && op != &refchain; i++) {
1983 while (op == self || op == args || op == res || op == t ||
Andy Lester55728702020-03-06 16:53:17 -06001984 (t != NULL && !Py_IS_TYPE(op, (PyTypeObject *) t))) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001985 op = op->_ob_next;
1986 if (op == &refchain)
1987 return res;
1988 }
1989 if (PyList_Append(res, op) < 0) {
1990 Py_DECREF(res);
1991 return NULL;
1992 }
1993 op = op->_ob_next;
1994 }
1995 return res;
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +00001996}
1997
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001998#endif
Guido van Rossum97ead3f1996-01-12 01:24:09 +00001999
Benjamin Petersonb173f782009-05-05 22:31:58 +00002000
Guido van Rossum84a90321996-05-22 16:34:47 +00002001/* Hack to force loading of abstract.o */
Martin v. Löwis18e16552006-02-15 17:27:45 +00002002Py_ssize_t (*_Py_abstract_hack)(PyObject *) = PyObject_Size;
Guido van Rossume09fb551997-08-05 02:04:34 +00002003
2004
David Malcolm49526f42012-06-22 14:55:41 -04002005void
2006_PyObject_DebugTypeStats(FILE *out)
2007{
David Malcolm49526f42012-06-22 14:55:41 -04002008 _PyDict_DebugMallocStats(out);
2009 _PyFloat_DebugMallocStats(out);
2010 _PyFrame_DebugMallocStats(out);
2011 _PyList_DebugMallocStats(out);
David Malcolm49526f42012-06-22 14:55:41 -04002012 _PyTuple_DebugMallocStats(out);
2013}
Guido van Rossumb18618d2000-05-03 23:44:39 +00002014
Guido van Rossum86610361998-04-10 22:32:46 +00002015/* These methods are used to control infinite recursion in repr, str, print,
2016 etc. Container objects that may recursively contain themselves,
Martin Panter8d56c022016-05-29 04:13:35 +00002017 e.g. builtin dictionaries and lists, should use Py_ReprEnter() and
Guido van Rossum86610361998-04-10 22:32:46 +00002018 Py_ReprLeave() to avoid infinite recursion.
2019
2020 Py_ReprEnter() returns 0 the first time it is called for a particular
2021 object and 1 every time thereafter. It returns -1 if an exception
2022 occurred. Py_ReprLeave() has no return value.
2023
2024 See dictobject.c and listobject.c for examples of use.
2025*/
2026
Guido van Rossum86610361998-04-10 22:32:46 +00002027int
Fred Drake100814d2000-07-09 15:48:49 +00002028Py_ReprEnter(PyObject *obj)
Guido van Rossum86610361998-04-10 22:32:46 +00002029{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002030 PyObject *dict;
2031 PyObject *list;
2032 Py_ssize_t i;
Guido van Rossum86610361998-04-10 22:32:46 +00002033
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002034 dict = PyThreadState_GetDict();
Antoine Pitrou04d17d32014-03-31 22:04:38 +02002035 /* Ignore a missing thread-state, so that this function can be called
2036 early on startup. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002037 if (dict == NULL)
2038 return 0;
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002039 list = _PyDict_GetItemIdWithError(dict, &PyId_Py_Repr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002040 if (list == NULL) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002041 if (PyErr_Occurred()) {
2042 return -1;
2043 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002044 list = PyList_New(0);
2045 if (list == NULL)
2046 return -1;
Victor Stinner7a07e452013-11-06 18:57:29 +01002047 if (_PyDict_SetItemId(dict, &PyId_Py_Repr, list) < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002048 return -1;
2049 Py_DECREF(list);
2050 }
2051 i = PyList_GET_SIZE(list);
2052 while (--i >= 0) {
2053 if (PyList_GET_ITEM(list, i) == obj)
2054 return 1;
2055 }
Victor Stinnere901d1f2013-07-17 21:58:41 +02002056 if (PyList_Append(list, obj) < 0)
2057 return -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002058 return 0;
Guido van Rossum86610361998-04-10 22:32:46 +00002059}
2060
2061void
Fred Drake100814d2000-07-09 15:48:49 +00002062Py_ReprLeave(PyObject *obj)
Guido van Rossum86610361998-04-10 22:32:46 +00002063{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002064 PyObject *dict;
2065 PyObject *list;
2066 Py_ssize_t i;
Victor Stinner1b634932013-07-16 22:24:44 +02002067 PyObject *error_type, *error_value, *error_traceback;
2068
2069 PyErr_Fetch(&error_type, &error_value, &error_traceback);
Guido van Rossum86610361998-04-10 22:32:46 +00002070
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002071 dict = PyThreadState_GetDict();
2072 if (dict == NULL)
Victor Stinner1b634932013-07-16 22:24:44 +02002073 goto finally;
2074
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002075 list = _PyDict_GetItemIdWithError(dict, &PyId_Py_Repr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002076 if (list == NULL || !PyList_Check(list))
Victor Stinner1b634932013-07-16 22:24:44 +02002077 goto finally;
2078
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002079 i = PyList_GET_SIZE(list);
2080 /* Count backwards because we always expect obj to be list[-1] */
2081 while (--i >= 0) {
2082 if (PyList_GET_ITEM(list, i) == obj) {
2083 PyList_SetSlice(list, i, i + 1, NULL);
2084 break;
2085 }
2086 }
Victor Stinner1b634932013-07-16 22:24:44 +02002087
2088finally:
2089 /* ignore exceptions because there is no way to report them. */
2090 PyErr_Restore(error_type, error_value, error_traceback);
Guido van Rossum86610361998-04-10 22:32:46 +00002091}
Guido van Rossumd724b232000-03-13 16:01:29 +00002092
Tim Peters803526b2002-07-07 05:13:56 +00002093/* Trashcan support. */
Guido van Rossumd724b232000-03-13 16:01:29 +00002094
Tim Peters803526b2002-07-07 05:13:56 +00002095/* Add op to the _PyTrash_delete_later list. Called when the current
2096 * call-stack depth gets large. op must be a currently untracked gc'ed
2097 * object, with refcount 0. Py_DECREF must already have been called on it.
2098 */
Guido van Rossumd724b232000-03-13 16:01:29 +00002099void
Fred Drake100814d2000-07-09 15:48:49 +00002100_PyTrash_deposit_object(PyObject *op)
Guido van Rossumd724b232000-03-13 16:01:29 +00002101{
Victor Stinner1bcc32f2020-06-10 20:08:26 +02002102 PyInterpreterState *interp = _PyInterpreterState_GET();
2103 struct _gc_runtime_state *gcstate = &interp->gc;
Victor Stinner72474072019-11-20 12:25:50 +01002104
Hai Shi675d9a32020-04-15 02:11:20 +08002105 _PyObject_ASSERT(op, _PyObject_IS_GC(op));
Victor Stinner24702042018-10-26 17:16:37 +02002106 _PyObject_ASSERT(op, !_PyObject_GC_IS_TRACKED(op));
Victor Stinnera93c51e2020-02-07 00:38:59 +01002107 _PyObject_ASSERT(op, Py_REFCNT(op) == 0);
Victor Stinner72474072019-11-20 12:25:50 +01002108 _PyGCHead_SET_PREV(_Py_AS_GC(op), gcstate->trash_delete_later);
2109 gcstate->trash_delete_later = op;
Guido van Rossumd724b232000-03-13 16:01:29 +00002110}
2111
Antoine Pitrou2b0218a2012-09-06 00:59:49 +02002112/* The equivalent API, using per-thread state recursion info */
2113void
2114_PyTrash_thread_deposit_object(PyObject *op)
2115{
Victor Stinner50b48572018-11-01 01:51:40 +01002116 PyThreadState *tstate = _PyThreadState_GET();
Hai Shi675d9a32020-04-15 02:11:20 +08002117 _PyObject_ASSERT(op, _PyObject_IS_GC(op));
Victor Stinner24702042018-10-26 17:16:37 +02002118 _PyObject_ASSERT(op, !_PyObject_GC_IS_TRACKED(op));
Victor Stinnera93c51e2020-02-07 00:38:59 +01002119 _PyObject_ASSERT(op, Py_REFCNT(op) == 0);
INADA Naoki5ac9e6e2018-07-10 17:19:53 +09002120 _PyGCHead_SET_PREV(_Py_AS_GC(op), tstate->trash_delete_later);
Antoine Pitrou2b0218a2012-09-06 00:59:49 +02002121 tstate->trash_delete_later = op;
2122}
2123
Min ho Kimc4cacc82019-07-31 08:16:13 +10002124/* Deallocate all the objects in the _PyTrash_delete_later list. Called when
Tim Peters803526b2002-07-07 05:13:56 +00002125 * the call-stack unwinds again.
2126 */
Guido van Rossumd724b232000-03-13 16:01:29 +00002127void
Fred Drake100814d2000-07-09 15:48:49 +00002128_PyTrash_destroy_chain(void)
Guido van Rossumd724b232000-03-13 16:01:29 +00002129{
Victor Stinner1bcc32f2020-06-10 20:08:26 +02002130 PyInterpreterState *interp = _PyInterpreterState_GET();
2131 struct _gc_runtime_state *gcstate = &interp->gc;
Victor Stinner72474072019-11-20 12:25:50 +01002132
2133 while (gcstate->trash_delete_later) {
2134 PyObject *op = gcstate->trash_delete_later;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002135 destructor dealloc = Py_TYPE(op)->tp_dealloc;
Neil Schemenauerf589c052002-03-29 03:05:54 +00002136
Victor Stinner72474072019-11-20 12:25:50 +01002137 gcstate->trash_delete_later =
INADA Naoki5ac9e6e2018-07-10 17:19:53 +09002138 (PyObject*) _PyGCHead_PREV(_Py_AS_GC(op));
Neil Schemenauerf589c052002-03-29 03:05:54 +00002139
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002140 /* Call the deallocator directly. This used to try to
2141 * fool Py_DECREF into calling it indirectly, but
2142 * Py_DECREF was already called on this object, and in
2143 * assorted non-release builds calling Py_DECREF again ends
2144 * up distorting allocation statistics.
2145 */
Victor Stinnera93c51e2020-02-07 00:38:59 +01002146 _PyObject_ASSERT(op, Py_REFCNT(op) == 0);
Victor Stinner72474072019-11-20 12:25:50 +01002147 ++gcstate->trash_delete_nesting;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002148 (*dealloc)(op);
Victor Stinner72474072019-11-20 12:25:50 +01002149 --gcstate->trash_delete_nesting;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002150 }
Guido van Rossumd724b232000-03-13 16:01:29 +00002151}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002152
Antoine Pitrou2b0218a2012-09-06 00:59:49 +02002153/* The equivalent API, using per-thread state recursion info */
2154void
2155_PyTrash_thread_destroy_chain(void)
2156{
Victor Stinner50b48572018-11-01 01:51:40 +01002157 PyThreadState *tstate = _PyThreadState_GET();
Xiang Zhanga66f9c62017-05-13 13:36:14 +08002158 /* We need to increase trash_delete_nesting here, otherwise,
2159 _PyTrash_thread_destroy_chain will be called recursively
2160 and then possibly crash. An example that may crash without
2161 increase:
2162 N = 500000 # need to be large enough
2163 ob = object()
2164 tups = [(ob,) for i in range(N)]
2165 for i in range(49):
2166 tups = [(tup,) for tup in tups]
2167 del tups
2168 */
2169 assert(tstate->trash_delete_nesting == 0);
2170 ++tstate->trash_delete_nesting;
Antoine Pitrou2b0218a2012-09-06 00:59:49 +02002171 while (tstate->trash_delete_later) {
2172 PyObject *op = tstate->trash_delete_later;
2173 destructor dealloc = Py_TYPE(op)->tp_dealloc;
2174
2175 tstate->trash_delete_later =
INADA Naoki5ac9e6e2018-07-10 17:19:53 +09002176 (PyObject*) _PyGCHead_PREV(_Py_AS_GC(op));
Antoine Pitrou2b0218a2012-09-06 00:59:49 +02002177
2178 /* Call the deallocator directly. This used to try to
2179 * fool Py_DECREF into calling it indirectly, but
2180 * Py_DECREF was already called on this object, and in
2181 * assorted non-release builds calling Py_DECREF again ends
2182 * up distorting allocation statistics.
2183 */
Victor Stinnera93c51e2020-02-07 00:38:59 +01002184 _PyObject_ASSERT(op, Py_REFCNT(op) == 0);
Antoine Pitrou2b0218a2012-09-06 00:59:49 +02002185 (*dealloc)(op);
Xiang Zhanga66f9c62017-05-13 13:36:14 +08002186 assert(tstate->trash_delete_nesting == 1);
Antoine Pitrou2b0218a2012-09-06 00:59:49 +02002187 }
Xiang Zhanga66f9c62017-05-13 13:36:14 +08002188 --tstate->trash_delete_nesting;
Antoine Pitrou2b0218a2012-09-06 00:59:49 +02002189}
2190
Victor Stinner626bff82018-10-25 17:31:10 +02002191
Victor Stinner38965ec2020-03-13 16:51:52 +01002192int
2193_PyTrash_begin(PyThreadState *tstate, PyObject *op)
2194{
2195 if (tstate->trash_delete_nesting >= PyTrash_UNWIND_LEVEL) {
2196 /* Store the object (to be deallocated later) and jump past
2197 * Py_TRASHCAN_END, skipping the body of the deallocator */
2198 _PyTrash_thread_deposit_object(op);
2199 return 1;
2200 }
2201 ++tstate->trash_delete_nesting;
2202 return 0;
2203}
2204
2205
2206void
2207_PyTrash_end(PyThreadState *tstate)
2208{
2209 --tstate->trash_delete_nesting;
2210 if (tstate->trash_delete_later && tstate->trash_delete_nesting <= 0) {
2211 _PyTrash_thread_destroy_chain();
2212 }
2213}
2214
2215
Hai Shied1a5a52020-11-25 06:03:31 +08002216/* bpo-40170: It's only be used in Py_TRASHCAN_BEGIN macro to hide
2217 implementation details. */
2218int
2219_PyTrash_cond(PyObject *op, destructor dealloc)
2220{
2221 return Py_TYPE(op)->tp_dealloc == dealloc;
2222}
2223
2224
Victor Stinner2a4903f2020-01-30 13:09:11 +01002225void _Py_NO_RETURN
Victor Stinnerf1d002c2018-11-21 23:53:44 +01002226_PyObject_AssertFailed(PyObject *obj, const char *expr, const char *msg,
Victor Stinner626bff82018-10-25 17:31:10 +02002227 const char *file, int line, const char *function)
2228{
Victor Stinnerf1d002c2018-11-21 23:53:44 +01002229 fprintf(stderr, "%s:%d: ", file, line);
2230 if (function) {
2231 fprintf(stderr, "%s: ", function);
2232 }
Victor Stinner626bff82018-10-25 17:31:10 +02002233 fflush(stderr);
Victor Stinner68762572019-10-07 18:42:01 +02002234
Victor Stinnerf1d002c2018-11-21 23:53:44 +01002235 if (expr) {
2236 fprintf(stderr, "Assertion \"%s\" failed", expr);
Victor Stinner626bff82018-10-25 17:31:10 +02002237 }
2238 else {
Victor Stinnerf1d002c2018-11-21 23:53:44 +01002239 fprintf(stderr, "Assertion failed");
Victor Stinner626bff82018-10-25 17:31:10 +02002240 }
2241 fflush(stderr);
Victor Stinner68762572019-10-07 18:42:01 +02002242
Victor Stinnerf1d002c2018-11-21 23:53:44 +01002243 if (msg) {
2244 fprintf(stderr, ": %s", msg);
2245 }
2246 fprintf(stderr, "\n");
2247 fflush(stderr);
Victor Stinner626bff82018-10-25 17:31:10 +02002248
Victor Stinner68762572019-10-07 18:42:01 +02002249 if (_PyObject_IsFreed(obj)) {
Victor Stinner626bff82018-10-25 17:31:10 +02002250 /* It seems like the object memory has been freed:
2251 don't access it to prevent a segmentation fault. */
Victor Stinnerb39afb72019-09-17 23:36:28 +02002252 fprintf(stderr, "<object at %p is freed>\n", obj);
Victor Stinner68762572019-10-07 18:42:01 +02002253 fflush(stderr);
Victor Stinner626bff82018-10-25 17:31:10 +02002254 }
2255 else {
penguindustin96466302019-05-06 14:57:17 -04002256 /* Display the traceback where the object has been allocated.
Victor Stinner626bff82018-10-25 17:31:10 +02002257 Do it before dumping repr(obj), since repr() is more likely
2258 to crash than dumping the traceback. */
2259 void *ptr;
2260 PyTypeObject *type = Py_TYPE(obj);
Victor Stinner45ec5b92020-04-08 01:42:27 +02002261 if (_PyType_IS_GC(type)) {
Victor Stinner626bff82018-10-25 17:31:10 +02002262 ptr = (void *)((char *)obj - sizeof(PyGC_Head));
2263 }
2264 else {
2265 ptr = (void *)obj;
2266 }
2267 _PyMem_DumpTraceback(fileno(stderr), ptr);
2268
2269 /* This might succeed or fail, but we're about to abort, so at least
2270 try to provide any extra info we can: */
2271 _PyObject_Dump(obj);
Victor Stinner77753492019-10-07 23:44:05 +02002272
2273 fprintf(stderr, "\n");
2274 fflush(stderr);
Victor Stinner626bff82018-10-25 17:31:10 +02002275 }
Victor Stinner626bff82018-10-25 17:31:10 +02002276
2277 Py_FatalError("_PyObject_AssertFailed");
2278}
2279
Victor Stinner3c09dca2018-10-30 14:48:26 +01002280
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00002281void
2282_Py_Dealloc(PyObject *op)
2283{
Victor Stinner3c09dca2018-10-30 14:48:26 +01002284 destructor dealloc = Py_TYPE(op)->tp_dealloc;
2285#ifdef Py_TRACE_REFS
2286 _Py_ForgetReference(op);
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00002287#endif
Victor Stinner3c09dca2018-10-30 14:48:26 +01002288 (*dealloc)(op);
2289}
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00002290
Victor Stinner38aefc52020-04-06 14:07:02 +02002291
2292PyObject **
2293PyObject_GET_WEAKREFS_LISTPTR(PyObject *op)
2294{
2295 return _PyObject_GET_WEAKREFS_LISTPTR(op);
2296}
2297
2298
Victor Stinner53a03aa2020-11-05 15:02:12 +01002299#undef Py_NewRef
2300#undef Py_XNewRef
2301
2302// Export Py_NewRef() and Py_XNewRef() as regular functions for the stable ABI.
2303PyObject*
2304Py_NewRef(PyObject *obj)
2305{
2306 return _Py_NewRef(obj);
2307}
2308
2309PyObject*
2310Py_XNewRef(PyObject *obj)
2311{
2312 return _Py_XNewRef(obj);
2313}
2314
Victor Stinner09bbebe2021-04-11 00:17:39 +02002315#undef Py_Is
2316#undef Py_IsNone
2317#undef Py_IsTrue
2318#undef Py_IsFalse
2319
2320// Export Py_Is(), Py_IsNone(), Py_IsTrue(), Py_IsFalse() as regular functions
2321// for the stable ABI.
2322int Py_Is(PyObject *x, PyObject *y)
2323{
2324 return (x == y);
2325}
2326
2327int Py_IsNone(PyObject *x)
2328{
2329 return Py_Is(x, Py_None);
2330}
2331
2332int Py_IsTrue(PyObject *x)
2333{
2334 return Py_Is(x, Py_True);
2335}
2336
2337int Py_IsFalse(PyObject *x)
2338{
2339 return Py_Is(x, Py_False);
2340}
2341
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002342#ifdef __cplusplus
2343}
2344#endif