blob: 2154d119a02069272972d09f18316b20b1e24b01 [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 Stinnerbe434dc2019-11-05 00:51:22 +01005#include "pycore_context.h"
Victor Stinner331a6a52019-05-27 16:39:22 +02006#include "pycore_initconfig.h"
Victor Stinner0fc91ee2019-04-12 21:51:34 +02007#include "pycore_object.h"
Victor Stinnerbe434dc2019-11-05 00:51:22 +01008#include "pycore_pyerrors.h"
Victor Stinner7a1f6c22020-01-30 09:02:14 +01009#include "pycore_pylifecycle.h"
Victor Stinner621cebe2018-11-12 16:53:38 +010010#include "pycore_pystate.h"
Benjamin Petersonfd838e62009-04-20 02:09:13 +000011#include "frameobject.h"
Eric Snowc11183c2019-03-15 16:35:46 -060012#include "interpreteridobject.h"
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000013
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000014#ifdef __cplusplus
15extern "C" {
16#endif
17
Victor Stinner626bff82018-10-25 17:31:10 +020018/* Defined in tracemalloc.c */
19extern void _PyMem_DumpTraceback(int fd, const void *ptr);
20
Victor Stinnerbd303c12013-11-07 23:07:29 +010021_Py_IDENTIFIER(Py_Repr);
22_Py_IDENTIFIER(__bytes__);
23_Py_IDENTIFIER(__dir__);
24_Py_IDENTIFIER(__isabstractmethod__);
Victor Stinnerbd303c12013-11-07 23:07:29 +010025
Victor Stinner0fc91ee2019-04-12 21:51:34 +020026
27int
28_PyObject_CheckConsistency(PyObject *op, int check_content)
29{
Victor Stinner68762572019-10-07 18:42:01 +020030#define CHECK(expr) \
31 do { if (!(expr)) { _PyObject_ASSERT_FAILED_MSG(op, Py_STRINGIFY(expr)); } } while (0)
Victor Stinner0fc91ee2019-04-12 21:51:34 +020032
Victor Stinner68762572019-10-07 18:42:01 +020033 CHECK(!_PyObject_IsFreed(op));
34 CHECK(Py_REFCNT(op) >= 1);
35
36 CHECK(op->ob_type != NULL);
37 _PyType_CheckConsistency(op->ob_type);
Victor Stinner0fc91ee2019-04-12 21:51:34 +020038
39 if (PyUnicode_Check(op)) {
40 _PyUnicode_CheckConsistency(op, check_content);
41 }
42 else if (PyDict_Check(op)) {
43 _PyDict_CheckConsistency(op, check_content);
44 }
45 return 1;
Victor Stinner68762572019-10-07 18:42:01 +020046
47#undef CHECK
Victor Stinner0fc91ee2019-04-12 21:51:34 +020048}
49
50
Tim Peters34592512002-07-11 06:23:50 +000051#ifdef Py_REF_DEBUG
Neal Norwitz84632ee2006-03-04 20:00:59 +000052Py_ssize_t _Py_RefTotal;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000053
54Py_ssize_t
55_Py_GetRefTotal(void)
56{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000057 PyObject *o;
58 Py_ssize_t total = _Py_RefTotal;
Antoine Pitrou9d952542013-08-24 21:07:07 +020059 o = _PySet_Dummy;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000060 if (o != NULL)
61 total -= o->ob_refcnt;
62 return total;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000063}
Nick Coghland6009512014-11-20 21:39:37 +100064
65void
66_PyDebug_PrintTotalRefs(void) {
Eric Snowdae02762017-09-14 00:35:58 -070067 fprintf(stderr,
68 "[%" PY_FORMAT_SIZE_T "d refs, "
69 "%" PY_FORMAT_SIZE_T "d blocks]\n",
70 _Py_GetRefTotal(), _Py_GetAllocatedBlocks());
Nick Coghland6009512014-11-20 21:39:37 +100071}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000072#endif /* Py_REF_DEBUG */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000073
Guido van Rossum3f5da241990-12-20 15:06:42 +000074/* Object allocation routines used by NEWOBJ and NEWVAROBJ macros.
75 These are used by the individual routines for object creation.
76 Do not call them otherwise, they do not initialize the object! */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000077
Tim Peters78be7992003-03-23 02:51:01 +000078#ifdef Py_TRACE_REFS
Tim Peters7571a0f2003-03-23 17:52:28 +000079/* Head of circular doubly-linked list of all objects. These are linked
80 * together via the _ob_prev and _ob_next members of a PyObject, which
81 * exist only in a Py_TRACE_REFS build.
82 */
Tim Peters78be7992003-03-23 02:51:01 +000083static PyObject refchain = {&refchain, &refchain};
Tim Peters36eb4df2003-03-23 03:33:13 +000084
Tim Peters7571a0f2003-03-23 17:52:28 +000085/* Insert op at the front of the list of all objects. If force is true,
86 * op is added even if _ob_prev and _ob_next are non-NULL already. If
87 * force is false amd _ob_prev or _ob_next are non-NULL, do nothing.
88 * force should be true if and only if op points to freshly allocated,
89 * uninitialized memory, or you've unlinked op from the list and are
Tim Peters51f8d382003-03-23 18:06:08 +000090 * relinking it into the front.
Tim Peters7571a0f2003-03-23 17:52:28 +000091 * Note that objects are normally added to the list via _Py_NewReference,
92 * which is called by PyObject_Init. Not all objects are initialized that
93 * way, though; exceptions include statically allocated type objects, and
94 * statically allocated singletons (like Py_True and Py_None).
95 */
Tim Peters36eb4df2003-03-23 03:33:13 +000096void
Tim Peters7571a0f2003-03-23 17:52:28 +000097_Py_AddToAllObjects(PyObject *op, int force)
Tim Peters36eb4df2003-03-23 03:33:13 +000098{
Tim Peters7571a0f2003-03-23 17:52:28 +000099#ifdef Py_DEBUG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000100 if (!force) {
101 /* If it's initialized memory, op must be in or out of
102 * the list unambiguously.
103 */
Victor Stinner24702042018-10-26 17:16:37 +0200104 _PyObject_ASSERT(op, (op->_ob_prev == NULL) == (op->_ob_next == NULL));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000105 }
Tim Peters78be7992003-03-23 02:51:01 +0000106#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000107 if (force || op->_ob_prev == NULL) {
108 op->_ob_next = refchain._ob_next;
109 op->_ob_prev = &refchain;
110 refchain._ob_next->_ob_prev = op;
111 refchain._ob_next = op;
112 }
Tim Peters7571a0f2003-03-23 17:52:28 +0000113}
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000114#endif /* Py_TRACE_REFS */
Tim Peters78be7992003-03-23 02:51:01 +0000115
Tim Peters7c321a82002-07-09 02:57:01 +0000116#ifdef Py_REF_DEBUG
117/* Log a fatal error; doesn't return. */
118void
Victor Stinner18618e652018-10-25 17:28:11 +0200119_Py_NegativeRefcount(const char *filename, int lineno, PyObject *op)
Tim Peters7c321a82002-07-09 02:57:01 +0000120{
Victor Stinnerf1d002c2018-11-21 23:53:44 +0100121 _PyObject_AssertFailed(op, NULL, "object has negative ref count",
Victor Stinner3ec9af72018-10-26 02:12:34 +0200122 filename, lineno, __func__);
Tim Peters7c321a82002-07-09 02:57:01 +0000123}
124
125#endif /* Py_REF_DEBUG */
126
Thomas Heller1328b522004-04-22 17:23:49 +0000127void
128Py_IncRef(PyObject *o)
129{
130 Py_XINCREF(o);
131}
132
133void
134Py_DecRef(PyObject *o)
135{
136 Py_XDECREF(o);
137}
138
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000139PyObject *
Fred Drake100814d2000-07-09 15:48:49 +0000140PyObject_Init(PyObject *op, PyTypeObject *tp)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000141{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000142 if (op == NULL)
143 return PyErr_NoMemory();
144 /* Any changes should be reflected in PyObject_INIT (objimpl.h) */
145 Py_TYPE(op) = tp;
Eddie Elizondo364f0b02019-03-27 07:52:18 -0400146 if (PyType_GetFlags(tp) & Py_TPFLAGS_HEAPTYPE) {
147 Py_INCREF(tp);
148 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000149 _Py_NewReference(op);
150 return op;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000151}
152
Guido van Rossumb18618d2000-05-03 23:44:39 +0000153PyVarObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000154PyObject_InitVar(PyVarObject *op, PyTypeObject *tp, Py_ssize_t size)
Guido van Rossumb18618d2000-05-03 23:44:39 +0000155{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000156 if (op == NULL)
157 return (PyVarObject *) PyErr_NoMemory();
158 /* Any changes should be reflected in PyObject_INIT_VAR */
Eddie Elizondo364f0b02019-03-27 07:52:18 -0400159 Py_SIZE(op) = size;
160 PyObject_Init((PyObject *)op, tp);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000161 return op;
Guido van Rossumb18618d2000-05-03 23:44:39 +0000162}
163
164PyObject *
Fred Drake100814d2000-07-09 15:48:49 +0000165_PyObject_New(PyTypeObject *tp)
Guido van Rossumb18618d2000-05-03 23:44:39 +0000166{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000167 PyObject *op;
168 op = (PyObject *) PyObject_MALLOC(_PyObject_SIZE(tp));
169 if (op == NULL)
170 return PyErr_NoMemory();
171 return PyObject_INIT(op, tp);
Guido van Rossumb18618d2000-05-03 23:44:39 +0000172}
173
Guido van Rossumd0c87ee1997-05-15 21:31:03 +0000174PyVarObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000175_PyObject_NewVar(PyTypeObject *tp, Py_ssize_t nitems)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000176{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000177 PyVarObject *op;
178 const size_t size = _PyObject_VAR_SIZE(tp, nitems);
179 op = (PyVarObject *) PyObject_MALLOC(size);
180 if (op == NULL)
181 return (PyVarObject *)PyErr_NoMemory();
182 return PyObject_INIT_VAR(op, tp, nitems);
Guido van Rossumb18618d2000-05-03 23:44:39 +0000183}
184
Antoine Pitrou796564c2013-07-30 19:59:21 +0200185void
186PyObject_CallFinalizer(PyObject *self)
187{
188 PyTypeObject *tp = Py_TYPE(self);
189
Antoine Pitrouada319b2019-05-29 22:12:38 +0200190 if (tp->tp_finalize == NULL)
Antoine Pitrou796564c2013-07-30 19:59:21 +0200191 return;
192 /* tp_finalize should only be called once. */
193 if (PyType_IS_GC(tp) && _PyGC_FINALIZED(self))
194 return;
195
196 tp->tp_finalize(self);
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900197 if (PyType_IS_GC(tp)) {
198 _PyGC_SET_FINALIZED(self);
199 }
Antoine Pitrou796564c2013-07-30 19:59:21 +0200200}
201
202int
203PyObject_CallFinalizerFromDealloc(PyObject *self)
204{
Victor Stinner5eb8bff2020-01-30 09:01:07 +0100205 if (self->ob_refcnt != 0) {
206 _PyObject_ASSERT_FAILED_MSG(self,
207 "PyObject_CallFinalizerFromDealloc called "
208 "on object with a non-zero refcount");
209 }
Antoine Pitrou796564c2013-07-30 19:59:21 +0200210
211 /* Temporarily resurrect the object. */
Antoine Pitrou796564c2013-07-30 19:59:21 +0200212 self->ob_refcnt = 1;
213
214 PyObject_CallFinalizer(self);
215
Victor Stinner24702042018-10-26 17:16:37 +0200216 _PyObject_ASSERT_WITH_MSG(self,
217 self->ob_refcnt > 0,
218 "refcount is too small");
Victor Stinner5eb8bff2020-01-30 09:01:07 +0100219
220 /* Undo the temporary resurrection; can't use DECREF here, it would
221 * cause a recursive call. */
222 if (--self->ob_refcnt == 0) {
Antoine Pitrou796564c2013-07-30 19:59:21 +0200223 return 0; /* this is the normal path out */
Victor Stinner5eb8bff2020-01-30 09:01:07 +0100224 }
Antoine Pitrou796564c2013-07-30 19:59:21 +0200225
226 /* tp_finalize resurrected it! Make it look like the original Py_DECREF
Victor Stinner5eb8bff2020-01-30 09:01:07 +0100227 * never happened. */
228 Py_ssize_t refcnt = self->ob_refcnt;
Antoine Pitrou796564c2013-07-30 19:59:21 +0200229 _Py_NewReference(self);
230 self->ob_refcnt = refcnt;
231
Victor Stinner24702042018-10-26 17:16:37 +0200232 _PyObject_ASSERT(self,
233 (!PyType_IS_GC(Py_TYPE(self))
234 || _PyObject_GC_IS_TRACKED(self)));
Antoine Pitrou796564c2013-07-30 19:59:21 +0200235 /* If Py_REF_DEBUG, _Py_NewReference bumped _Py_RefTotal, so
236 * we need to undo that. */
237 _Py_DEC_REFTOTAL;
Antoine Pitrou796564c2013-07-30 19:59:21 +0200238 return -1;
239}
240
Antoine Pitrouc47bd4a2010-07-27 22:08:27 +0000241int
242PyObject_Print(PyObject *op, FILE *fp, int flags)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000243{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000244 int ret = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000245 if (PyErr_CheckSignals())
246 return -1;
Guido van Rossum9b00dfa1998-04-28 16:06:54 +0000247#ifdef USE_STACKCHECK
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000248 if (PyOS_CheckStack()) {
249 PyErr_SetString(PyExc_MemoryError, "stack overflow");
250 return -1;
251 }
Guido van Rossum9b00dfa1998-04-28 16:06:54 +0000252#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000253 clearerr(fp); /* Clear any previous error condition */
254 if (op == NULL) {
255 Py_BEGIN_ALLOW_THREADS
256 fprintf(fp, "<nil>");
257 Py_END_ALLOW_THREADS
258 }
259 else {
Victor Stinner3ec9af72018-10-26 02:12:34 +0200260 if (op->ob_refcnt <= 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000261 /* XXX(twouters) cast refcount to long until %zd is
262 universally available */
263 Py_BEGIN_ALLOW_THREADS
264 fprintf(fp, "<refcnt %ld at %p>",
Zackery Spytz1a2252e2019-05-06 10:56:51 -0600265 (long)op->ob_refcnt, (void *)op);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000266 Py_END_ALLOW_THREADS
Victor Stinner3ec9af72018-10-26 02:12:34 +0200267 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000268 else {
269 PyObject *s;
270 if (flags & Py_PRINT_RAW)
271 s = PyObject_Str(op);
272 else
273 s = PyObject_Repr(op);
274 if (s == NULL)
275 ret = -1;
276 else if (PyBytes_Check(s)) {
277 fwrite(PyBytes_AS_STRING(s), 1,
278 PyBytes_GET_SIZE(s), fp);
279 }
280 else if (PyUnicode_Check(s)) {
281 PyObject *t;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200282 t = PyUnicode_AsEncodedString(s, "utf-8", "backslashreplace");
Zackery Spytzae62f012018-10-06 00:44:25 -0600283 if (t == NULL) {
284 ret = -1;
285 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000286 else {
287 fwrite(PyBytes_AS_STRING(t), 1,
288 PyBytes_GET_SIZE(t), fp);
Victor Stinnerba6b4302010-05-17 09:33:42 +0000289 Py_DECREF(t);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000290 }
291 }
292 else {
293 PyErr_Format(PyExc_TypeError,
294 "str() or repr() returned '%.100s'",
295 s->ob_type->tp_name);
296 ret = -1;
297 }
298 Py_XDECREF(s);
299 }
300 }
301 if (ret == 0) {
302 if (ferror(fp)) {
Serhiy Storchaka55fe1ae2017-04-16 10:46:38 +0300303 PyErr_SetFromErrno(PyExc_OSError);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000304 clearerr(fp);
305 ret = -1;
306 }
307 }
308 return ret;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000309}
310
Guido van Rossum38938152006-08-21 23:36:26 +0000311/* For debugging convenience. Set a breakpoint here and call it from your DLL */
312void
Thomas Woutersb2137042007-02-01 18:02:27 +0000313_Py_BreakPoint(void)
Guido van Rossum38938152006-08-21 23:36:26 +0000314{
315}
316
Neal Norwitz1a997502003-01-13 20:13:12 +0000317
Victor Stinner4c409be2019-04-11 13:01:15 +0200318/* Heuristic checking if the object memory is uninitialized or deallocated.
319 Rely on the debug hooks on Python memory allocators:
320 see _PyMem_IsPtrFreed().
Victor Stinner82af0b62018-10-23 17:39:40 +0200321
322 The function can be used to prevent segmentation fault on dereferencing
Victor Stinner4c409be2019-04-11 13:01:15 +0200323 pointers like 0xDDDDDDDDDDDDDDDD. */
Victor Stinner82af0b62018-10-23 17:39:40 +0200324int
325_PyObject_IsFreed(PyObject *op)
326{
Victor Stinner2b00db62019-04-11 11:33:27 +0200327 if (_PyMem_IsPtrFreed(op) || _PyMem_IsPtrFreed(op->ob_type)) {
Victor Stinner2cf5d322018-11-22 16:32:57 +0100328 return 1;
329 }
Victor Stinner2b00db62019-04-11 11:33:27 +0200330 /* ignore op->ob_ref: its value can have be modified
Victor Stinner82af0b62018-10-23 17:39:40 +0200331 by Py_INCREF() and Py_DECREF(). */
332#ifdef Py_TRACE_REFS
Pablo Galindo36e33c32019-10-08 00:43:14 +0100333 if (op->_ob_next != NULL && _PyMem_IsPtrFreed(op->_ob_next)) {
Victor Stinner2b00db62019-04-11 11:33:27 +0200334 return 1;
335 }
Pablo Galindo36e33c32019-10-08 00:43:14 +0100336 if (op->_ob_prev != NULL && _PyMem_IsPtrFreed(op->_ob_prev)) {
337 return 1;
338 }
Victor Stinner82af0b62018-10-23 17:39:40 +0200339#endif
Victor Stinner2b00db62019-04-11 11:33:27 +0200340 return 0;
Victor Stinner82af0b62018-10-23 17:39:40 +0200341}
342
343
Barry Warsaw9bf16442001-01-23 16:24:35 +0000344/* For debugging convenience. See Misc/gdbinit for some useful gdb hooks */
Guido van Rossum38938152006-08-21 23:36:26 +0000345void
346_PyObject_Dump(PyObject* op)
Barry Warsaw9bf16442001-01-23 16:24:35 +0000347{
Victor Stinner82af0b62018-10-23 17:39:40 +0200348 if (_PyObject_IsFreed(op)) {
349 /* It seems like the object memory has been freed:
350 don't access it to prevent a segmentation fault. */
Victor Stinnerb39afb72019-09-17 23:36:28 +0200351 fprintf(stderr, "<object at %p is freed>\n", op);
Victor Stinner68762572019-10-07 18:42:01 +0200352 fflush(stderr);
Victor Stinner2cf5d322018-11-22 16:32:57 +0100353 return;
Victor Stinner82af0b62018-10-23 17:39:40 +0200354 }
355
Victor Stinner68762572019-10-07 18:42:01 +0200356 /* first, write fields which are the least likely to crash */
357 fprintf(stderr, "object address : %p\n", (void *)op);
Victor Stinner82af0b62018-10-23 17:39:40 +0200358 /* XXX(twouters) cast refcount to long until %zd is
359 universally available */
Victor Stinner68762572019-10-07 18:42:01 +0200360 fprintf(stderr, "object refcount : %ld\n", (long)op->ob_refcnt);
361 fflush(stderr);
362
363 PyTypeObject *type = Py_TYPE(op);
364 fprintf(stderr, "object type : %p\n", type);
365 fprintf(stderr, "object type name: %s\n",
366 type==NULL ? "NULL" : type->tp_name);
367
368 /* the most dangerous part */
369 fprintf(stderr, "object repr : ");
370 fflush(stderr);
371
372 PyGILState_STATE gil = PyGILState_Ensure();
373 PyObject *error_type, *error_value, *error_traceback;
374 PyErr_Fetch(&error_type, &error_value, &error_traceback);
375
376 (void)PyObject_Print(op, stderr, 0);
377 fflush(stderr);
378
379 PyErr_Restore(error_type, error_value, error_traceback);
380 PyGILState_Release(gil);
381
382 fprintf(stderr, "\n");
Victor Stinner82af0b62018-10-23 17:39:40 +0200383 fflush(stderr);
Barry Warsaw9bf16442001-01-23 16:24:35 +0000384}
Barry Warsaw903138f2001-01-23 16:33:18 +0000385
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000386PyObject *
Fred Drake100814d2000-07-09 15:48:49 +0000387PyObject_Repr(PyObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000388{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000389 PyObject *res;
390 if (PyErr_CheckSignals())
391 return NULL;
Guido van Rossum9b00dfa1998-04-28 16:06:54 +0000392#ifdef USE_STACKCHECK
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000393 if (PyOS_CheckStack()) {
394 PyErr_SetString(PyExc_MemoryError, "stack overflow");
395 return NULL;
396 }
Guido van Rossum9b00dfa1998-04-28 16:06:54 +0000397#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000398 if (v == NULL)
399 return PyUnicode_FromString("<NULL>");
400 if (Py_TYPE(v)->tp_repr == NULL)
401 return PyUnicode_FromFormat("<%s object at %p>",
402 v->ob_type->tp_name, v);
Victor Stinner33824f62013-08-26 14:05:19 +0200403
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100404 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinner33824f62013-08-26 14:05:19 +0200405#ifdef Py_DEBUG
406 /* PyObject_Repr() must not be called with an exception set,
Victor Stinnera8cb5152017-01-18 14:12:51 +0100407 because it can clear it (directly or indirectly) and so the
Martin Panter9955a372015-10-07 10:26:23 +0000408 caller loses its exception */
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100409 assert(!_PyErr_Occurred(tstate));
Victor Stinner33824f62013-08-26 14:05:19 +0200410#endif
411
Serhiy Storchaka1fb72d22017-12-03 22:12:11 +0200412 /* It is possible for a type to have a tp_repr representation that loops
413 infinitely. */
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100414 if (_Py_EnterRecursiveCall(tstate,
415 " while getting the repr of an object")) {
Serhiy Storchaka1fb72d22017-12-03 22:12:11 +0200416 return NULL;
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100417 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000418 res = (*v->ob_type->tp_repr)(v);
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100419 _Py_LeaveRecursiveCall(tstate);
420
421 if (res == NULL) {
Victor Stinner0a54cf12011-12-01 03:22:44 +0100422 return NULL;
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100423 }
Victor Stinner0a54cf12011-12-01 03:22:44 +0100424 if (!PyUnicode_Check(res)) {
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100425 _PyErr_Format(tstate, PyExc_TypeError,
426 "__repr__ returned non-string (type %.200s)",
427 res->ob_type->tp_name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000428 Py_DECREF(res);
429 return NULL;
430 }
Victor Stinnerdb88ae52011-12-01 02:15:00 +0100431#ifndef Py_DEBUG
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100432 if (PyUnicode_READY(res) < 0) {
Victor Stinnerdb88ae52011-12-01 02:15:00 +0100433 return NULL;
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100434 }
Victor Stinnerdb88ae52011-12-01 02:15:00 +0100435#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000436 return res;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000437}
438
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000439PyObject *
Guido van Rossum98297ee2007-11-06 21:34:58 +0000440PyObject_Str(PyObject *v)
Guido van Rossumc6004111993-11-05 10:22:19 +0000441{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000442 PyObject *res;
443 if (PyErr_CheckSignals())
444 return NULL;
Guido van Rossum98297ee2007-11-06 21:34:58 +0000445#ifdef USE_STACKCHECK
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000446 if (PyOS_CheckStack()) {
447 PyErr_SetString(PyExc_MemoryError, "stack overflow");
448 return NULL;
449 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000450#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000451 if (v == NULL)
452 return PyUnicode_FromString("<NULL>");
453 if (PyUnicode_CheckExact(v)) {
Victor Stinnerdb88ae52011-12-01 02:15:00 +0100454#ifndef Py_DEBUG
Victor Stinner4ead7c72011-11-20 19:48:36 +0100455 if (PyUnicode_READY(v) < 0)
456 return NULL;
Victor Stinnerdb88ae52011-12-01 02:15:00 +0100457#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000458 Py_INCREF(v);
459 return v;
460 }
461 if (Py_TYPE(v)->tp_str == NULL)
462 return PyObject_Repr(v);
Guido van Rossum4f288ab2001-05-01 16:53:37 +0000463
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100464 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinner33824f62013-08-26 14:05:19 +0200465#ifdef Py_DEBUG
466 /* PyObject_Str() must not be called with an exception set,
Victor Stinnera8cb5152017-01-18 14:12:51 +0100467 because it can clear it (directly or indirectly) and so the
Nick Coghland979e432014-02-09 10:43:21 +1000468 caller loses its exception */
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100469 assert(!_PyErr_Occurred(tstate));
Victor Stinner33824f62013-08-26 14:05:19 +0200470#endif
471
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000472 /* It is possible for a type to have a tp_str representation that loops
473 infinitely. */
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100474 if (_Py_EnterRecursiveCall(tstate, " while getting the str of an object")) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000475 return NULL;
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100476 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000477 res = (*Py_TYPE(v)->tp_str)(v);
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100478 _Py_LeaveRecursiveCall(tstate);
479
480 if (res == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000481 return NULL;
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100482 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000483 if (!PyUnicode_Check(res)) {
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100484 _PyErr_Format(tstate, PyExc_TypeError,
485 "__str__ returned non-string (type %.200s)",
486 Py_TYPE(res)->tp_name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000487 Py_DECREF(res);
488 return NULL;
489 }
Victor Stinnerdb88ae52011-12-01 02:15:00 +0100490#ifndef Py_DEBUG
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100491 if (PyUnicode_READY(res) < 0) {
Victor Stinner4ead7c72011-11-20 19:48:36 +0100492 return NULL;
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100493 }
Victor Stinnerdb88ae52011-12-01 02:15:00 +0100494#endif
Victor Stinner4ead7c72011-11-20 19:48:36 +0100495 assert(_PyUnicode_CheckConsistency(res, 1));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000496 return res;
Neil Schemenauercf52c072005-08-12 17:34:58 +0000497}
498
Georg Brandl559e5d72008-06-11 18:37:52 +0000499PyObject *
500PyObject_ASCII(PyObject *v)
501{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000502 PyObject *repr, *ascii, *res;
Georg Brandl559e5d72008-06-11 18:37:52 +0000503
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000504 repr = PyObject_Repr(v);
505 if (repr == NULL)
506 return NULL;
Georg Brandl559e5d72008-06-11 18:37:52 +0000507
Victor Stinneraf037572013-04-14 18:44:10 +0200508 if (PyUnicode_IS_ASCII(repr))
509 return repr;
510
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000511 /* repr is guaranteed to be a PyUnicode object by PyObject_Repr */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200512 ascii = _PyUnicode_AsASCIIString(repr, "backslashreplace");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000513 Py_DECREF(repr);
514 if (ascii == NULL)
515 return NULL;
Georg Brandl559e5d72008-06-11 18:37:52 +0000516
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000517 res = PyUnicode_DecodeASCII(
518 PyBytes_AS_STRING(ascii),
519 PyBytes_GET_SIZE(ascii),
520 NULL);
521
522 Py_DECREF(ascii);
523 return res;
Georg Brandl559e5d72008-06-11 18:37:52 +0000524}
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000525
Benjamin Petersonc15a0732008-08-26 16:46:47 +0000526PyObject *
527PyObject_Bytes(PyObject *v)
528{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000529 PyObject *result, *func;
Benjamin Petersonc15a0732008-08-26 16:46:47 +0000530
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000531 if (v == NULL)
532 return PyBytes_FromString("<NULL>");
Benjamin Petersonc15a0732008-08-26 16:46:47 +0000533
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000534 if (PyBytes_CheckExact(v)) {
535 Py_INCREF(v);
536 return v;
537 }
Benjamin Petersonc15a0732008-08-26 16:46:47 +0000538
Benjamin Petersonce798522012-01-22 11:24:29 -0500539 func = _PyObject_LookupSpecial(v, &PyId___bytes__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000540 if (func != NULL) {
Victor Stinnerf17c3de2016-12-06 18:46:19 +0100541 result = _PyObject_CallNoArg(func);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000542 Py_DECREF(func);
543 if (result == NULL)
Benjamin Peterson41ece392010-09-11 16:39:57 +0000544 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000545 if (!PyBytes_Check(result)) {
Benjamin Peterson41ece392010-09-11 16:39:57 +0000546 PyErr_Format(PyExc_TypeError,
547 "__bytes__ returned non-bytes (type %.200s)",
548 Py_TYPE(result)->tp_name);
549 Py_DECREF(result);
550 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000551 }
552 return result;
553 }
554 else if (PyErr_Occurred())
555 return NULL;
556 return PyBytes_FromObject(v);
Benjamin Petersonc15a0732008-08-26 16:46:47 +0000557}
558
Jeroen Demeyerbf17d412019-11-05 16:48:04 +0100559
560/*
561def _PyObject_FunctionStr(x):
562 try:
563 qualname = x.__qualname__
564 except AttributeError:
565 return str(x)
566 try:
567 mod = x.__module__
568 if mod is not None and mod != 'builtins':
569 return f"{x.__module__}.{qualname}()"
570 except AttributeError:
571 pass
572 return qualname
573*/
574PyObject *
575_PyObject_FunctionStr(PyObject *x)
576{
577 _Py_IDENTIFIER(__module__);
578 _Py_IDENTIFIER(__qualname__);
579 _Py_IDENTIFIER(builtins);
580 assert(!PyErr_Occurred());
581 PyObject *qualname;
582 int ret = _PyObject_LookupAttrId(x, &PyId___qualname__, &qualname);
583 if (qualname == NULL) {
584 if (ret < 0) {
585 return NULL;
586 }
587 return PyObject_Str(x);
588 }
589 PyObject *module;
590 PyObject *result = NULL;
591 ret = _PyObject_LookupAttrId(x, &PyId___module__, &module);
592 if (module != NULL && module != Py_None) {
593 PyObject *builtinsname = _PyUnicode_FromId(&PyId_builtins);
594 if (builtinsname == NULL) {
595 goto done;
596 }
597 ret = PyObject_RichCompareBool(module, builtinsname, Py_NE);
598 if (ret < 0) {
599 // error
600 goto done;
601 }
602 if (ret > 0) {
603 result = PyUnicode_FromFormat("%S.%S()", module, qualname);
604 goto done;
605 }
606 }
607 else if (ret < 0) {
608 goto done;
609 }
610 result = PyUnicode_FromFormat("%S()", qualname);
611done:
612 Py_DECREF(qualname);
613 Py_XDECREF(module);
614 return result;
615}
616
Mark Dickinsonc008a172009-02-01 13:59:22 +0000617/* For Python 3.0.1 and later, the old three-way comparison has been
618 completely removed in favour of rich comparisons. PyObject_Compare() and
619 PyObject_Cmp() are gone, and the builtin cmp function no longer exists.
Jeroen Demeyer530f5062019-05-31 04:13:39 +0200620 The old tp_compare slot has been renamed to tp_as_async, and should no
Mark Dickinsonc008a172009-02-01 13:59:22 +0000621 longer be used. Use tp_richcompare instead.
Guido van Rossum98297ee2007-11-06 21:34:58 +0000622
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000623 See (*) below for practical amendments.
624
Mark Dickinsonc008a172009-02-01 13:59:22 +0000625 tp_richcompare gets called with a first argument of the appropriate type
626 and a second object of an arbitrary type. We never do any kind of
627 coercion.
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000628
Mark Dickinsonc008a172009-02-01 13:59:22 +0000629 The tp_richcompare slot should return an object, as follows:
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000630
631 NULL if an exception occurred
632 NotImplemented if the requested comparison is not implemented
633 any other false value if the requested comparison is false
634 any other true value if the requested comparison is true
635
636 The PyObject_RichCompare[Bool]() wrappers raise TypeError when they get
637 NotImplemented.
638
639 (*) Practical amendments:
640
641 - If rich comparison returns NotImplemented, == and != are decided by
642 comparing the object pointer (i.e. falling back to the base object
643 implementation).
644
Guido van Rossuma4073002002-05-31 20:03:54 +0000645*/
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000646
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000647/* Map rich comparison operators to their swapped version, e.g. LT <--> GT */
Brett Cannona5ca2e72004-09-25 01:37:24 +0000648int _Py_SwappedOp[] = {Py_GT, Py_GE, Py_EQ, Py_NE, Py_LT, Py_LE};
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +0000649
Serhiy Storchaka2d06e842015-12-25 19:53:18 +0200650static const char * const opstrings[] = {"<", "<=", "==", "!=", ">", ">="};
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000651
652/* Perform a rich comparison, raising TypeError when the requested comparison
653 operator is not supported. */
Guido van Rossume797ec12001-01-17 15:24:28 +0000654static PyObject *
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100655do_richcompare(PyThreadState *tstate, PyObject *v, PyObject *w, int op)
Guido van Rossume797ec12001-01-17 15:24:28 +0000656{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000657 richcmpfunc f;
658 PyObject *res;
659 int checked_reverse_op = 0;
Guido van Rossume797ec12001-01-17 15:24:28 +0000660
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000661 if (v->ob_type != w->ob_type &&
662 PyType_IsSubtype(w->ob_type, v->ob_type) &&
663 (f = w->ob_type->tp_richcompare) != NULL) {
664 checked_reverse_op = 1;
665 res = (*f)(w, v, _Py_SwappedOp[op]);
666 if (res != Py_NotImplemented)
667 return res;
668 Py_DECREF(res);
669 }
670 if ((f = v->ob_type->tp_richcompare) != NULL) {
671 res = (*f)(v, w, op);
672 if (res != Py_NotImplemented)
673 return res;
674 Py_DECREF(res);
675 }
676 if (!checked_reverse_op && (f = w->ob_type->tp_richcompare) != NULL) {
677 res = (*f)(w, v, _Py_SwappedOp[op]);
678 if (res != Py_NotImplemented)
679 return res;
680 Py_DECREF(res);
681 }
682 /* If neither object implements it, provide a sensible default
683 for == and !=, but raise an exception for ordering. */
684 switch (op) {
685 case Py_EQ:
686 res = (v == w) ? Py_True : Py_False;
687 break;
688 case Py_NE:
689 res = (v != w) ? Py_True : Py_False;
690 break;
691 default:
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100692 _PyErr_Format(tstate, PyExc_TypeError,
693 "'%s' not supported between instances of '%.100s' and '%.100s'",
694 opstrings[op],
695 v->ob_type->tp_name,
696 w->ob_type->tp_name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000697 return NULL;
698 }
699 Py_INCREF(res);
700 return res;
Guido van Rossume797ec12001-01-17 15:24:28 +0000701}
702
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000703/* Perform a rich comparison with object result. This wraps do_richcompare()
704 with a check for NULL arguments and a recursion check. */
Guido van Rossume797ec12001-01-17 15:24:28 +0000705
Guido van Rossume797ec12001-01-17 15:24:28 +0000706PyObject *
707PyObject_RichCompare(PyObject *v, PyObject *w, int op)
708{
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100709 PyThreadState *tstate = _PyThreadState_GET();
Guido van Rossume797ec12001-01-17 15:24:28 +0000710
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000711 assert(Py_LT <= op && op <= Py_GE);
712 if (v == NULL || w == NULL) {
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100713 if (!_PyErr_Occurred(tstate)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000714 PyErr_BadInternalCall();
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100715 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000716 return NULL;
717 }
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100718 if (_Py_EnterRecursiveCall(tstate, " in comparison")) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000719 return NULL;
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100720 }
721 PyObject *res = do_richcompare(tstate, v, w, op);
722 _Py_LeaveRecursiveCall(tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000723 return res;
Guido van Rossume797ec12001-01-17 15:24:28 +0000724}
725
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000726/* Perform a rich comparison with integer result. This wraps
727 PyObject_RichCompare(), returning -1 for error, 0 for false, 1 for true. */
Guido van Rossume797ec12001-01-17 15:24:28 +0000728int
729PyObject_RichCompareBool(PyObject *v, PyObject *w, int op)
730{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000731 PyObject *res;
732 int ok;
Guido van Rossume797ec12001-01-17 15:24:28 +0000733
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000734 /* Quick result when objects are the same.
735 Guarantees that identity implies equality. */
736 if (v == w) {
737 if (op == Py_EQ)
738 return 1;
739 else if (op == Py_NE)
740 return 0;
741 }
Mark Dickinson4a1f5932008-11-12 23:23:36 +0000742
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000743 res = PyObject_RichCompare(v, w, op);
744 if (res == NULL)
745 return -1;
746 if (PyBool_Check(res))
747 ok = (res == Py_True);
748 else
749 ok = PyObject_IsTrue(res);
750 Py_DECREF(res);
751 return ok;
Guido van Rossume797ec12001-01-17 15:24:28 +0000752}
Fred Drake13634cf2000-06-29 19:17:04 +0000753
Antoine Pitrouce4a9da2011-11-21 20:46:33 +0100754Py_hash_t
Nick Coghland1abd252008-07-15 15:46:38 +0000755PyObject_HashNotImplemented(PyObject *v)
756{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000757 PyErr_Format(PyExc_TypeError, "unhashable type: '%.200s'",
758 Py_TYPE(v)->tp_name);
759 return -1;
Nick Coghland1abd252008-07-15 15:46:38 +0000760}
Fred Drake13634cf2000-06-29 19:17:04 +0000761
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000762Py_hash_t
Fred Drake100814d2000-07-09 15:48:49 +0000763PyObject_Hash(PyObject *v)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000764{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000765 PyTypeObject *tp = Py_TYPE(v);
766 if (tp->tp_hash != NULL)
767 return (*tp->tp_hash)(v);
768 /* To keep to the general practice that inheriting
769 * solely from object in C code should work without
770 * an explicit call to PyType_Ready, we implicitly call
771 * PyType_Ready here and then check the tp_hash slot again
772 */
773 if (tp->tp_dict == NULL) {
774 if (PyType_Ready(tp) < 0)
775 return -1;
776 if (tp->tp_hash != NULL)
777 return (*tp->tp_hash)(v);
778 }
779 /* Otherwise, the object can't be hashed */
780 return PyObject_HashNotImplemented(v);
Guido van Rossum9bfef441993-03-29 10:43:31 +0000781}
782
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000783PyObject *
Jeremy Hyltonaf68c872005-12-10 18:50:16 +0000784PyObject_GetAttrString(PyObject *v, const char *name)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000785{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000786 PyObject *w, *res;
Guido van Rossumd8eb1b31996-08-09 20:52:03 +0000787
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000788 if (Py_TYPE(v)->tp_getattr != NULL)
789 return (*Py_TYPE(v)->tp_getattr)(v, (char*)name);
INADA Naoki3e8d6cb2017-02-21 23:57:25 +0900790 w = PyUnicode_FromString(name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000791 if (w == NULL)
792 return NULL;
793 res = PyObject_GetAttr(v, w);
Victor Stinner59af08f2012-03-22 02:09:08 +0100794 Py_DECREF(w);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000795 return res;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000796}
797
798int
Jeremy Hyltonaf68c872005-12-10 18:50:16 +0000799PyObject_HasAttrString(PyObject *v, const char *name)
Guido van Rossumed18fdc1993-07-11 19:55:34 +0000800{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000801 PyObject *res = PyObject_GetAttrString(v, name);
802 if (res != NULL) {
803 Py_DECREF(res);
804 return 1;
805 }
806 PyErr_Clear();
807 return 0;
Guido van Rossumed18fdc1993-07-11 19:55:34 +0000808}
809
810int
Jeremy Hyltonaf68c872005-12-10 18:50:16 +0000811PyObject_SetAttrString(PyObject *v, const char *name, PyObject *w)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000812{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000813 PyObject *s;
814 int res;
Guido van Rossumd8eb1b31996-08-09 20:52:03 +0000815
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000816 if (Py_TYPE(v)->tp_setattr != NULL)
817 return (*Py_TYPE(v)->tp_setattr)(v, (char*)name, w);
818 s = PyUnicode_InternFromString(name);
819 if (s == NULL)
820 return -1;
821 res = PyObject_SetAttr(v, s, w);
822 Py_XDECREF(s);
823 return res;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000824}
825
Benjamin Petersonbfebb7b2011-12-15 15:34:02 -0500826int
827_PyObject_IsAbstract(PyObject *obj)
828{
829 int res;
830 PyObject* isabstract;
Benjamin Petersonbfebb7b2011-12-15 15:34:02 -0500831
832 if (obj == NULL)
833 return 0;
834
Serhiy Storchakaf320be72018-01-25 10:49:40 +0200835 res = _PyObject_LookupAttrId(obj, &PyId___isabstractmethod__, &isabstract);
836 if (res > 0) {
837 res = PyObject_IsTrue(isabstract);
838 Py_DECREF(isabstract);
Benjamin Petersonbfebb7b2011-12-15 15:34:02 -0500839 }
Benjamin Petersonbfebb7b2011-12-15 15:34:02 -0500840 return res;
841}
842
Guido van Rossum98ff96a1997-05-20 18:34:44 +0000843PyObject *
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200844_PyObject_GetAttrId(PyObject *v, _Py_Identifier *name)
845{
846 PyObject *result;
Martin v. Löwisd10759f2011-11-07 13:00:05 +0100847 PyObject *oname = _PyUnicode_FromId(name); /* borrowed */
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200848 if (!oname)
849 return NULL;
850 result = PyObject_GetAttr(v, oname);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200851 return result;
852}
853
854int
855_PyObject_HasAttrId(PyObject *v, _Py_Identifier *name)
856{
857 int result;
Martin v. Löwisd10759f2011-11-07 13:00:05 +0100858 PyObject *oname = _PyUnicode_FromId(name); /* borrowed */
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200859 if (!oname)
860 return -1;
861 result = PyObject_HasAttr(v, oname);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200862 return result;
863}
864
865int
866_PyObject_SetAttrId(PyObject *v, _Py_Identifier *name, PyObject *w)
867{
868 int 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 -1;
872 result = PyObject_SetAttr(v, oname, w);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200873 return result;
874}
875
876PyObject *
Fred Drake100814d2000-07-09 15:48:49 +0000877PyObject_GetAttr(PyObject *v, PyObject *name)
Guido van Rossum98ff96a1997-05-20 18:34:44 +0000878{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000879 PyTypeObject *tp = Py_TYPE(v);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000880
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000881 if (!PyUnicode_Check(name)) {
882 PyErr_Format(PyExc_TypeError,
883 "attribute name must be string, not '%.200s'",
884 name->ob_type->tp_name);
885 return NULL;
886 }
887 if (tp->tp_getattro != NULL)
888 return (*tp->tp_getattro)(v, name);
889 if (tp->tp_getattr != NULL) {
Serhiy Storchaka2a404b62017-01-22 23:07:07 +0200890 const char *name_str = PyUnicode_AsUTF8(name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000891 if (name_str == NULL)
892 return NULL;
Serhiy Storchaka2a404b62017-01-22 23:07:07 +0200893 return (*tp->tp_getattr)(v, (char *)name_str);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000894 }
895 PyErr_Format(PyExc_AttributeError,
896 "'%.50s' object has no attribute '%U'",
897 tp->tp_name, name);
898 return NULL;
Guido van Rossum98ff96a1997-05-20 18:34:44 +0000899}
900
Serhiy Storchakaf320be72018-01-25 10:49:40 +0200901int
902_PyObject_LookupAttr(PyObject *v, PyObject *name, PyObject **result)
INADA Naoki378edee2018-01-16 20:52:41 +0900903{
904 PyTypeObject *tp = Py_TYPE(v);
INADA Naoki378edee2018-01-16 20:52:41 +0900905
906 if (!PyUnicode_Check(name)) {
907 PyErr_Format(PyExc_TypeError,
908 "attribute name must be string, not '%.200s'",
909 name->ob_type->tp_name);
Serhiy Storchakaf320be72018-01-25 10:49:40 +0200910 *result = NULL;
911 return -1;
INADA Naoki378edee2018-01-16 20:52:41 +0900912 }
913
914 if (tp->tp_getattro == PyObject_GenericGetAttr) {
Serhiy Storchakaf320be72018-01-25 10:49:40 +0200915 *result = _PyObject_GenericGetAttrWithDict(v, name, NULL, 1);
916 if (*result != NULL) {
917 return 1;
918 }
919 if (PyErr_Occurred()) {
920 return -1;
921 }
922 return 0;
INADA Naoki378edee2018-01-16 20:52:41 +0900923 }
924 if (tp->tp_getattro != NULL) {
Serhiy Storchakaf320be72018-01-25 10:49:40 +0200925 *result = (*tp->tp_getattro)(v, name);
INADA Naoki378edee2018-01-16 20:52:41 +0900926 }
927 else if (tp->tp_getattr != NULL) {
928 const char *name_str = PyUnicode_AsUTF8(name);
Serhiy Storchakaf320be72018-01-25 10:49:40 +0200929 if (name_str == NULL) {
930 *result = NULL;
931 return -1;
932 }
933 *result = (*tp->tp_getattr)(v, (char *)name_str);
INADA Naoki378edee2018-01-16 20:52:41 +0900934 }
INADA Naokie76daeb2018-01-26 16:22:51 +0900935 else {
936 *result = NULL;
937 return 0;
938 }
939
Serhiy Storchakaf320be72018-01-25 10:49:40 +0200940 if (*result != NULL) {
941 return 1;
INADA Naoki378edee2018-01-16 20:52:41 +0900942 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +0200943 if (!PyErr_ExceptionMatches(PyExc_AttributeError)) {
944 return -1;
945 }
946 PyErr_Clear();
947 return 0;
948}
949
950int
951_PyObject_LookupAttrId(PyObject *v, _Py_Identifier *name, PyObject **result)
952{
953 PyObject *oname = _PyUnicode_FromId(name); /* borrowed */
954 if (!oname) {
955 *result = NULL;
956 return -1;
957 }
958 return _PyObject_LookupAttr(v, oname, result);
INADA Naoki378edee2018-01-16 20:52:41 +0900959}
960
Guido van Rossum98ff96a1997-05-20 18:34:44 +0000961int
Fred Drake100814d2000-07-09 15:48:49 +0000962PyObject_HasAttr(PyObject *v, PyObject *name)
Guido van Rossum98ff96a1997-05-20 18:34:44 +0000963{
Serhiy Storchakaf320be72018-01-25 10:49:40 +0200964 PyObject *res;
965 if (_PyObject_LookupAttr(v, name, &res) < 0) {
966 PyErr_Clear();
967 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000968 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +0200969 if (res == NULL) {
970 return 0;
971 }
972 Py_DECREF(res);
973 return 1;
Guido van Rossum98ff96a1997-05-20 18:34:44 +0000974}
975
976int
Fred Drake100814d2000-07-09 15:48:49 +0000977PyObject_SetAttr(PyObject *v, PyObject *name, PyObject *value)
Guido van Rossum98ff96a1997-05-20 18:34:44 +0000978{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000979 PyTypeObject *tp = Py_TYPE(v);
980 int err;
Marc-André Lemburge44e5072000-09-18 16:20:57 +0000981
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000982 if (!PyUnicode_Check(name)) {
983 PyErr_Format(PyExc_TypeError,
984 "attribute name must be string, not '%.200s'",
985 name->ob_type->tp_name);
986 return -1;
987 }
988 Py_INCREF(name);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000989
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000990 PyUnicode_InternInPlace(&name);
991 if (tp->tp_setattro != NULL) {
992 err = (*tp->tp_setattro)(v, name, value);
993 Py_DECREF(name);
994 return err;
995 }
996 if (tp->tp_setattr != NULL) {
Serhiy Storchaka2a404b62017-01-22 23:07:07 +0200997 const char *name_str = PyUnicode_AsUTF8(name);
Zackery Spytze0dcb852019-04-28 06:58:52 -0600998 if (name_str == NULL) {
999 Py_DECREF(name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001000 return -1;
Zackery Spytze0dcb852019-04-28 06:58:52 -06001001 }
Serhiy Storchaka2a404b62017-01-22 23:07:07 +02001002 err = (*tp->tp_setattr)(v, (char *)name_str, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001003 Py_DECREF(name);
1004 return err;
1005 }
1006 Py_DECREF(name);
Victor Stinner24702042018-10-26 17:16:37 +02001007 _PyObject_ASSERT(name, name->ob_refcnt >= 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001008 if (tp->tp_getattr == NULL && tp->tp_getattro == NULL)
1009 PyErr_Format(PyExc_TypeError,
1010 "'%.100s' object has no attributes "
1011 "(%s .%U)",
1012 tp->tp_name,
1013 value==NULL ? "del" : "assign to",
1014 name);
1015 else
1016 PyErr_Format(PyExc_TypeError,
1017 "'%.100s' object has only read-only attributes "
1018 "(%s .%U)",
1019 tp->tp_name,
1020 value==NULL ? "del" : "assign to",
1021 name);
1022 return -1;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001023}
1024
1025/* Helper to get a pointer to an object's __dict__ slot, if any */
1026
1027PyObject **
1028_PyObject_GetDictPtr(PyObject *obj)
1029{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001030 Py_ssize_t dictoffset;
1031 PyTypeObject *tp = Py_TYPE(obj);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001032
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001033 dictoffset = tp->tp_dictoffset;
1034 if (dictoffset == 0)
1035 return NULL;
1036 if (dictoffset < 0) {
1037 Py_ssize_t tsize;
1038 size_t size;
Guido van Rossum2eb0b872002-03-01 22:24:49 +00001039
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001040 tsize = ((PyVarObject *)obj)->ob_size;
1041 if (tsize < 0)
1042 tsize = -tsize;
1043 size = _PyObject_VAR_SIZE(tp, tsize);
Guido van Rossum2eb0b872002-03-01 22:24:49 +00001044
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001045 dictoffset += (long)size;
Victor Stinner24702042018-10-26 17:16:37 +02001046 _PyObject_ASSERT(obj, dictoffset > 0);
1047 _PyObject_ASSERT(obj, dictoffset % SIZEOF_VOID_P == 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001048 }
1049 return (PyObject **) ((char *)obj + dictoffset);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001050}
1051
Tim Peters6d6c1a32001-08-02 04:15:00 +00001052PyObject *
Raymond Hettinger1da1dbf2003-03-17 19:46:11 +00001053PyObject_SelfIter(PyObject *obj)
Raymond Hettinger01538262003-03-17 08:24:35 +00001054{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001055 Py_INCREF(obj);
1056 return obj;
Raymond Hettinger01538262003-03-17 08:24:35 +00001057}
1058
Amaury Forgeot d'Arcf343e012009-01-12 23:58:21 +00001059/* Helper used when the __next__ method is removed from a type:
1060 tp_iternext is never NULL and can be safely called without checking
1061 on every iteration.
1062 */
1063
1064PyObject *
1065_PyObject_NextNotImplemented(PyObject *self)
1066{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001067 PyErr_Format(PyExc_TypeError,
1068 "'%.200s' object is not iterable",
1069 Py_TYPE(self)->tp_name);
1070 return NULL;
Amaury Forgeot d'Arcf343e012009-01-12 23:58:21 +00001071}
1072
Yury Selivanovf2392132016-12-13 19:03:51 -05001073
1074/* Specialized version of _PyObject_GenericGetAttrWithDict
1075 specifically for the LOAD_METHOD opcode.
1076
1077 Return 1 if a method is found, 0 if it's a regular attribute
1078 from __dict__ or something returned by using a descriptor
1079 protocol.
1080
1081 `method` will point to the resolved attribute or NULL. In the
1082 latter case, an error will be set.
1083*/
1084int
1085_PyObject_GetMethod(PyObject *obj, PyObject *name, PyObject **method)
1086{
1087 PyTypeObject *tp = Py_TYPE(obj);
1088 PyObject *descr;
1089 descrgetfunc f = NULL;
1090 PyObject **dictptr, *dict;
1091 PyObject *attr;
1092 int meth_found = 0;
1093
1094 assert(*method == NULL);
1095
1096 if (Py_TYPE(obj)->tp_getattro != PyObject_GenericGetAttr
1097 || !PyUnicode_Check(name)) {
1098 *method = PyObject_GetAttr(obj, name);
1099 return 0;
1100 }
1101
1102 if (tp->tp_dict == NULL && PyType_Ready(tp) < 0)
1103 return 0;
1104
1105 descr = _PyType_Lookup(tp, name);
1106 if (descr != NULL) {
1107 Py_INCREF(descr);
Jeroen Demeyereb65e242019-05-28 14:42:53 +02001108 if (PyType_HasFeature(Py_TYPE(descr), Py_TPFLAGS_METHOD_DESCRIPTOR)) {
Yury Selivanovf2392132016-12-13 19:03:51 -05001109 meth_found = 1;
1110 } else {
1111 f = descr->ob_type->tp_descr_get;
1112 if (f != NULL && PyDescr_IsData(descr)) {
1113 *method = f(descr, obj, (PyObject *)obj->ob_type);
1114 Py_DECREF(descr);
1115 return 0;
1116 }
1117 }
1118 }
1119
1120 dictptr = _PyObject_GetDictPtr(obj);
1121 if (dictptr != NULL && (dict = *dictptr) != NULL) {
1122 Py_INCREF(dict);
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02001123 attr = PyDict_GetItemWithError(dict, name);
Yury Selivanovf2392132016-12-13 19:03:51 -05001124 if (attr != NULL) {
1125 Py_INCREF(attr);
1126 *method = attr;
1127 Py_DECREF(dict);
1128 Py_XDECREF(descr);
1129 return 0;
1130 }
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02001131 else {
1132 Py_DECREF(dict);
1133 if (PyErr_Occurred()) {
1134 Py_XDECREF(descr);
1135 return 0;
1136 }
1137 }
Yury Selivanovf2392132016-12-13 19:03:51 -05001138 }
1139
1140 if (meth_found) {
1141 *method = descr;
1142 return 1;
1143 }
1144
1145 if (f != NULL) {
1146 *method = f(descr, obj, (PyObject *)Py_TYPE(obj));
1147 Py_DECREF(descr);
1148 return 0;
1149 }
1150
1151 if (descr != NULL) {
1152 *method = descr;
1153 return 0;
1154 }
1155
1156 PyErr_Format(PyExc_AttributeError,
1157 "'%.50s' object has no attribute '%U'",
1158 tp->tp_name, name);
1159 return 0;
1160}
1161
1162/* Generic GetAttr functions - put these in your tp_[gs]etattro slot. */
Michael W. Hudson1593f502004-09-14 17:09:47 +00001163
Raymond Hettinger01538262003-03-17 08:24:35 +00001164PyObject *
INADA Naoki378edee2018-01-16 20:52:41 +09001165_PyObject_GenericGetAttrWithDict(PyObject *obj, PyObject *name,
1166 PyObject *dict, int suppress)
Tim Peters6d6c1a32001-08-02 04:15:00 +00001167{
Yury Selivanovf2392132016-12-13 19:03:51 -05001168 /* Make sure the logic of _PyObject_GetMethod is in sync with
1169 this method.
INADA Naoki378edee2018-01-16 20:52:41 +09001170
1171 When suppress=1, this function suppress AttributeError.
Yury Selivanovf2392132016-12-13 19:03:51 -05001172 */
1173
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001174 PyTypeObject *tp = Py_TYPE(obj);
1175 PyObject *descr = NULL;
1176 PyObject *res = NULL;
1177 descrgetfunc f;
1178 Py_ssize_t dictoffset;
1179 PyObject **dictptr;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001180
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001181 if (!PyUnicode_Check(name)){
1182 PyErr_Format(PyExc_TypeError,
1183 "attribute name must be string, not '%.200s'",
1184 name->ob_type->tp_name);
1185 return NULL;
1186 }
Serhiy Storchaka55c861f2016-04-17 20:31:51 +03001187 Py_INCREF(name);
Guido van Rossumebca9fc2001-12-04 15:54:53 +00001188
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001189 if (tp->tp_dict == NULL) {
1190 if (PyType_Ready(tp) < 0)
1191 goto done;
1192 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001193
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001194 descr = _PyType_Lookup(tp, name);
Michael W. Hudsonb2c7de42003-08-15 13:07:47 +00001195
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001196 f = NULL;
1197 if (descr != NULL) {
Serhiy Storchaka55c861f2016-04-17 20:31:51 +03001198 Py_INCREF(descr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001199 f = descr->ob_type->tp_descr_get;
1200 if (f != NULL && PyDescr_IsData(descr)) {
1201 res = f(descr, obj, (PyObject *)obj->ob_type);
INADA Naoki378edee2018-01-16 20:52:41 +09001202 if (res == NULL && suppress &&
1203 PyErr_ExceptionMatches(PyExc_AttributeError)) {
1204 PyErr_Clear();
1205 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001206 goto done;
1207 }
1208 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001209
Antoine Pitrou1a9a9d52010-08-28 18:17:03 +00001210 if (dict == NULL) {
1211 /* Inline _PyObject_GetDictPtr */
1212 dictoffset = tp->tp_dictoffset;
1213 if (dictoffset != 0) {
1214 if (dictoffset < 0) {
1215 Py_ssize_t tsize;
1216 size_t size;
Guido van Rossumc66ff442002-08-19 16:50:48 +00001217
Antoine Pitrou1a9a9d52010-08-28 18:17:03 +00001218 tsize = ((PyVarObject *)obj)->ob_size;
1219 if (tsize < 0)
1220 tsize = -tsize;
1221 size = _PyObject_VAR_SIZE(tp, tsize);
Victor Stinner24702042018-10-26 17:16:37 +02001222 _PyObject_ASSERT(obj, size <= PY_SSIZE_T_MAX);
Guido van Rossumc66ff442002-08-19 16:50:48 +00001223
Serhiy Storchaka55c861f2016-04-17 20:31:51 +03001224 dictoffset += (Py_ssize_t)size;
Victor Stinner24702042018-10-26 17:16:37 +02001225 _PyObject_ASSERT(obj, dictoffset > 0);
1226 _PyObject_ASSERT(obj, dictoffset % SIZEOF_VOID_P == 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001227 }
Antoine Pitrou1a9a9d52010-08-28 18:17:03 +00001228 dictptr = (PyObject **) ((char *)obj + dictoffset);
1229 dict = *dictptr;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001230 }
1231 }
Antoine Pitrou1a9a9d52010-08-28 18:17:03 +00001232 if (dict != NULL) {
1233 Py_INCREF(dict);
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02001234 res = PyDict_GetItemWithError(dict, name);
Antoine Pitrou1a9a9d52010-08-28 18:17:03 +00001235 if (res != NULL) {
1236 Py_INCREF(res);
Antoine Pitrou1a9a9d52010-08-28 18:17:03 +00001237 Py_DECREF(dict);
1238 goto done;
1239 }
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02001240 else {
1241 Py_DECREF(dict);
1242 if (PyErr_Occurred()) {
1243 if (suppress && PyErr_ExceptionMatches(PyExc_AttributeError)) {
1244 PyErr_Clear();
1245 }
1246 else {
1247 goto done;
1248 }
1249 }
1250 }
Antoine Pitrou1a9a9d52010-08-28 18:17:03 +00001251 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001252
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001253 if (f != NULL) {
1254 res = f(descr, obj, (PyObject *)Py_TYPE(obj));
INADA Naoki378edee2018-01-16 20:52:41 +09001255 if (res == NULL && suppress &&
1256 PyErr_ExceptionMatches(PyExc_AttributeError)) {
1257 PyErr_Clear();
1258 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001259 goto done;
1260 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001261
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001262 if (descr != NULL) {
1263 res = descr;
Victor Stinner2d01dc02012-03-09 00:44:13 +01001264 descr = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001265 goto done;
1266 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001267
INADA Naoki378edee2018-01-16 20:52:41 +09001268 if (!suppress) {
1269 PyErr_Format(PyExc_AttributeError,
1270 "'%.50s' object has no attribute '%U'",
1271 tp->tp_name, name);
1272 }
Guido van Rossumebca9fc2001-12-04 15:54:53 +00001273 done:
Victor Stinner2d01dc02012-03-09 00:44:13 +01001274 Py_XDECREF(descr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001275 Py_DECREF(name);
1276 return res;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001277}
1278
Antoine Pitrou1a9a9d52010-08-28 18:17:03 +00001279PyObject *
1280PyObject_GenericGetAttr(PyObject *obj, PyObject *name)
1281{
INADA Naoki378edee2018-01-16 20:52:41 +09001282 return _PyObject_GenericGetAttrWithDict(obj, name, NULL, 0);
Antoine Pitrou1a9a9d52010-08-28 18:17:03 +00001283}
1284
Tim Peters6d6c1a32001-08-02 04:15:00 +00001285int
Antoine Pitrou1a9a9d52010-08-28 18:17:03 +00001286_PyObject_GenericSetAttrWithDict(PyObject *obj, PyObject *name,
1287 PyObject *value, PyObject *dict)
Tim Peters6d6c1a32001-08-02 04:15:00 +00001288{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001289 PyTypeObject *tp = Py_TYPE(obj);
1290 PyObject *descr;
1291 descrsetfunc f;
1292 PyObject **dictptr;
1293 int res = -1;
Guido van Rossumebca9fc2001-12-04 15:54:53 +00001294
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001295 if (!PyUnicode_Check(name)){
1296 PyErr_Format(PyExc_TypeError,
1297 "attribute name must be string, not '%.200s'",
1298 name->ob_type->tp_name);
1299 return -1;
1300 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001301
Benjamin Peterson74529ad2012-03-09 07:25:32 -08001302 if (tp->tp_dict == NULL && PyType_Ready(tp) < 0)
1303 return -1;
1304
1305 Py_INCREF(name);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001306
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001307 descr = _PyType_Lookup(tp, name);
Victor Stinner2d01dc02012-03-09 00:44:13 +01001308
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001309 if (descr != NULL) {
Serhiy Storchaka55c861f2016-04-17 20:31:51 +03001310 Py_INCREF(descr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001311 f = descr->ob_type->tp_descr_set;
Serhiy Storchaka55c861f2016-04-17 20:31:51 +03001312 if (f != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001313 res = f(descr, obj, value);
1314 goto done;
1315 }
1316 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001317
Steve Dowerb82e17e2019-05-23 08:45:22 -07001318 /* XXX [Steve Dower] These are really noisy - worth it? */
1319 /*if (PyType_Check(obj) || PyModule_Check(obj)) {
1320 if (value && PySys_Audit("object.__setattr__", "OOO", obj, name, value) < 0)
1321 return -1;
1322 if (!value && PySys_Audit("object.__delattr__", "OO", obj, name) < 0)
1323 return -1;
1324 }*/
1325
Antoine Pitrou1a9a9d52010-08-28 18:17:03 +00001326 if (dict == NULL) {
1327 dictptr = _PyObject_GetDictPtr(obj);
Serhiy Storchaka55c861f2016-04-17 20:31:51 +03001328 if (dictptr == NULL) {
1329 if (descr == NULL) {
1330 PyErr_Format(PyExc_AttributeError,
1331 "'%.100s' object has no attribute '%U'",
1332 tp->tp_name, name);
1333 }
1334 else {
1335 PyErr_Format(PyExc_AttributeError,
1336 "'%.50s' object attribute '%U' is read-only",
1337 tp->tp_name, name);
1338 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001339 goto done;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001340 }
Serhiy Storchaka55c861f2016-04-17 20:31:51 +03001341 res = _PyObjectDict_SetItem(tp, dictptr, name, value);
Antoine Pitrou1a9a9d52010-08-28 18:17:03 +00001342 }
Serhiy Storchaka55c861f2016-04-17 20:31:51 +03001343 else {
Antoine Pitrou1a9a9d52010-08-28 18:17:03 +00001344 Py_INCREF(dict);
1345 if (value == NULL)
1346 res = PyDict_DelItem(dict, name);
1347 else
1348 res = PyDict_SetItem(dict, name, value);
Benjamin Peterson74529ad2012-03-09 07:25:32 -08001349 Py_DECREF(dict);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001350 }
Serhiy Storchaka55c861f2016-04-17 20:31:51 +03001351 if (res < 0 && PyErr_ExceptionMatches(PyExc_KeyError))
1352 PyErr_SetObject(PyExc_AttributeError, name);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001353
Guido van Rossumebca9fc2001-12-04 15:54:53 +00001354 done:
Victor Stinner2d01dc02012-03-09 00:44:13 +01001355 Py_XDECREF(descr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001356 Py_DECREF(name);
1357 return res;
Guido van Rossum98ff96a1997-05-20 18:34:44 +00001358}
1359
Antoine Pitrou1a9a9d52010-08-28 18:17:03 +00001360int
1361PyObject_GenericSetAttr(PyObject *obj, PyObject *name, PyObject *value)
1362{
1363 return _PyObject_GenericSetAttrWithDict(obj, name, value, NULL);
1364}
1365
Benjamin Peterson8eb12692012-02-19 19:59:10 -05001366int
1367PyObject_GenericSetDict(PyObject *obj, PyObject *value, void *context)
1368{
Serhiy Storchaka576f1322016-01-05 21:27:54 +02001369 PyObject **dictptr = _PyObject_GetDictPtr(obj);
Benjamin Peterson8eb12692012-02-19 19:59:10 -05001370 if (dictptr == NULL) {
1371 PyErr_SetString(PyExc_AttributeError,
1372 "This object has no __dict__");
1373 return -1;
1374 }
1375 if (value == NULL) {
1376 PyErr_SetString(PyExc_TypeError, "cannot delete __dict__");
1377 return -1;
1378 }
1379 if (!PyDict_Check(value)) {
1380 PyErr_Format(PyExc_TypeError,
1381 "__dict__ must be set to a dictionary, "
1382 "not a '%.200s'", Py_TYPE(value)->tp_name);
1383 return -1;
1384 }
Serhiy Storchaka576f1322016-01-05 21:27:54 +02001385 Py_INCREF(value);
Serhiy Storchakaec397562016-04-06 09:50:03 +03001386 Py_XSETREF(*dictptr, value);
Benjamin Peterson8eb12692012-02-19 19:59:10 -05001387 return 0;
1388}
1389
Antoine Pitrou1a9a9d52010-08-28 18:17:03 +00001390
Guido van Rossum6ac258d1993-05-12 08:24:20 +00001391/* Test a value used as condition, e.g., in a for or if statement.
1392 Return -1 if an error occurred */
1393
1394int
Fred Drake100814d2000-07-09 15:48:49 +00001395PyObject_IsTrue(PyObject *v)
Guido van Rossum6ac258d1993-05-12 08:24:20 +00001396{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001397 Py_ssize_t res;
1398 if (v == Py_True)
1399 return 1;
1400 if (v == Py_False)
1401 return 0;
1402 if (v == Py_None)
1403 return 0;
1404 else if (v->ob_type->tp_as_number != NULL &&
1405 v->ob_type->tp_as_number->nb_bool != NULL)
1406 res = (*v->ob_type->tp_as_number->nb_bool)(v);
1407 else if (v->ob_type->tp_as_mapping != NULL &&
1408 v->ob_type->tp_as_mapping->mp_length != NULL)
1409 res = (*v->ob_type->tp_as_mapping->mp_length)(v);
1410 else if (v->ob_type->tp_as_sequence != NULL &&
1411 v->ob_type->tp_as_sequence->sq_length != NULL)
1412 res = (*v->ob_type->tp_as_sequence->sq_length)(v);
1413 else
1414 return 1;
1415 /* if it is negative, it should be either -1 or -2 */
1416 return (res > 0) ? 1 : Py_SAFE_DOWNCAST(res, Py_ssize_t, int);
Guido van Rossum6ac258d1993-05-12 08:24:20 +00001417}
1418
Tim Peters803526b2002-07-07 05:13:56 +00001419/* equivalent of 'not v'
Guido van Rossumc3d3f961998-04-09 17:53:59 +00001420 Return -1 if an error occurred */
1421
1422int
Fred Drake100814d2000-07-09 15:48:49 +00001423PyObject_Not(PyObject *v)
Guido van Rossumc3d3f961998-04-09 17:53:59 +00001424{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001425 int res;
1426 res = PyObject_IsTrue(v);
1427 if (res < 0)
1428 return res;
1429 return res == 0;
Guido van Rossumc3d3f961998-04-09 17:53:59 +00001430}
1431
Guido van Rossum49b11fe1995-01-26 00:38:22 +00001432/* Test whether an object can be called */
1433
1434int
Fred Drake100814d2000-07-09 15:48:49 +00001435PyCallable_Check(PyObject *x)
Guido van Rossum49b11fe1995-01-26 00:38:22 +00001436{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001437 if (x == NULL)
1438 return 0;
1439 return x->ob_type->tp_call != NULL;
Guido van Rossum49b11fe1995-01-26 00:38:22 +00001440}
1441
Tim Peters7eea37e2001-09-04 22:08:56 +00001442
Georg Brandle32b4222007-03-10 22:13:27 +00001443/* Helper for PyObject_Dir without arguments: returns the local scope. */
1444static PyObject *
Guido van Rossumad7d8d12007-04-13 01:39:34 +00001445_dir_locals(void)
Tim Peters305b5852001-09-17 02:38:46 +00001446{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001447 PyObject *names;
Victor Stinner41bb43a2013-10-29 01:19:37 +01001448 PyObject *locals;
Tim Peters305b5852001-09-17 02:38:46 +00001449
Victor Stinner41bb43a2013-10-29 01:19:37 +01001450 locals = PyEval_GetLocals();
1451 if (locals == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001452 return NULL;
Tim Peters305b5852001-09-17 02:38:46 +00001453
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001454 names = PyMapping_Keys(locals);
1455 if (!names)
1456 return NULL;
1457 if (!PyList_Check(names)) {
1458 PyErr_Format(PyExc_TypeError,
1459 "dir(): expected keys() of locals to be a list, "
1460 "not '%.200s'", Py_TYPE(names)->tp_name);
1461 Py_DECREF(names);
1462 return NULL;
1463 }
Benjamin Peterson3bbb7222011-06-11 16:12:08 -05001464 if (PyList_Sort(names)) {
1465 Py_DECREF(names);
1466 return NULL;
1467 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001468 /* the locals don't need to be DECREF'd */
1469 return names;
Georg Brandle32b4222007-03-10 22:13:27 +00001470}
1471
Benjamin Peterson82b00c12011-05-24 11:09:06 -05001472/* Helper for PyObject_Dir: object introspection. */
Georg Brandle32b4222007-03-10 22:13:27 +00001473static PyObject *
1474_dir_object(PyObject *obj)
1475{
Benjamin Peterson3bbb7222011-06-11 16:12:08 -05001476 PyObject *result, *sorted;
Benjamin Petersonce798522012-01-22 11:24:29 -05001477 PyObject *dirfunc = _PyObject_LookupSpecial(obj, &PyId___dir__);
Georg Brandle32b4222007-03-10 22:13:27 +00001478
Victor Stinner24702042018-10-26 17:16:37 +02001479 assert(obj != NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001480 if (dirfunc == NULL) {
Benjamin Peterson82b00c12011-05-24 11:09:06 -05001481 if (!PyErr_Occurred())
1482 PyErr_SetString(PyExc_TypeError, "object does not provide __dir__");
1483 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001484 }
Benjamin Peterson82b00c12011-05-24 11:09:06 -05001485 /* use __dir__ */
Victor Stinnerf17c3de2016-12-06 18:46:19 +01001486 result = _PyObject_CallNoArg(dirfunc);
Benjamin Peterson82b00c12011-05-24 11:09:06 -05001487 Py_DECREF(dirfunc);
1488 if (result == NULL)
1489 return NULL;
Benjamin Peterson3bbb7222011-06-11 16:12:08 -05001490 /* return sorted(result) */
1491 sorted = PySequence_List(result);
1492 Py_DECREF(result);
1493 if (sorted == NULL)
1494 return NULL;
1495 if (PyList_Sort(sorted)) {
1496 Py_DECREF(sorted);
1497 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001498 }
Benjamin Peterson3bbb7222011-06-11 16:12:08 -05001499 return sorted;
Georg Brandle32b4222007-03-10 22:13:27 +00001500}
1501
1502/* Implementation of dir() -- if obj is NULL, returns the names in the current
1503 (local) scope. Otherwise, performs introspection of the object: returns a
1504 sorted list of attribute names (supposedly) accessible from the object
1505*/
1506PyObject *
1507PyObject_Dir(PyObject *obj)
1508{
Benjamin Peterson3bbb7222011-06-11 16:12:08 -05001509 return (obj == NULL) ? _dir_locals() : _dir_object(obj);
Tim Peters7eea37e2001-09-04 22:08:56 +00001510}
Guido van Rossum49b11fe1995-01-26 00:38:22 +00001511
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001512/*
Raymond Hettinger66d2be82011-07-28 09:55:13 -07001513None is a non-NULL undefined value.
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001514There is (and should be!) no way to create other objects of this type,
Guido van Rossum3f5da241990-12-20 15:06:42 +00001515so there is exactly one (which is indestructible, by the way).
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001516*/
1517
Guido van Rossum0c182a11992-03-27 17:26:13 +00001518/* ARGSUSED */
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001519static PyObject *
Fred Drake100814d2000-07-09 15:48:49 +00001520none_repr(PyObject *op)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001521{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001522 return PyUnicode_FromString("None");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001523}
1524
Barry Warsaw9bf16442001-01-23 16:24:35 +00001525/* ARGUSED */
Victor Stinner2a4903f2020-01-30 13:09:11 +01001526static void _Py_NO_RETURN
Tim Peters803526b2002-07-07 05:13:56 +00001527none_dealloc(PyObject* ignore)
Barry Warsaw9bf16442001-01-23 16:24:35 +00001528{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001529 /* This should never get called, but we also don't want to SEGV if
1530 * we accidentally decref None out of existence.
1531 */
1532 Py_FatalError("deallocating None");
Barry Warsaw9bf16442001-01-23 16:24:35 +00001533}
1534
Benjamin Petersonc4607ae2011-07-29 18:19:43 -05001535static PyObject *
1536none_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
1537{
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +02001538 if (PyTuple_GET_SIZE(args) || (kwargs && PyDict_GET_SIZE(kwargs))) {
Benjamin Petersonc4607ae2011-07-29 18:19:43 -05001539 PyErr_SetString(PyExc_TypeError, "NoneType takes no arguments");
1540 return NULL;
1541 }
1542 Py_RETURN_NONE;
1543}
1544
Raymond Hettinger66d2be82011-07-28 09:55:13 -07001545static int
1546none_bool(PyObject *v)
1547{
1548 return 0;
1549}
1550
1551static PyNumberMethods none_as_number = {
1552 0, /* nb_add */
1553 0, /* nb_subtract */
1554 0, /* nb_multiply */
1555 0, /* nb_remainder */
1556 0, /* nb_divmod */
1557 0, /* nb_power */
1558 0, /* nb_negative */
1559 0, /* nb_positive */
1560 0, /* nb_absolute */
1561 (inquiry)none_bool, /* nb_bool */
1562 0, /* nb_invert */
1563 0, /* nb_lshift */
1564 0, /* nb_rshift */
1565 0, /* nb_and */
1566 0, /* nb_xor */
1567 0, /* nb_or */
1568 0, /* nb_int */
1569 0, /* nb_reserved */
1570 0, /* nb_float */
1571 0, /* nb_inplace_add */
1572 0, /* nb_inplace_subtract */
1573 0, /* nb_inplace_multiply */
1574 0, /* nb_inplace_remainder */
1575 0, /* nb_inplace_power */
1576 0, /* nb_inplace_lshift */
1577 0, /* nb_inplace_rshift */
1578 0, /* nb_inplace_and */
1579 0, /* nb_inplace_xor */
1580 0, /* nb_inplace_or */
1581 0, /* nb_floor_divide */
1582 0, /* nb_true_divide */
1583 0, /* nb_inplace_floor_divide */
1584 0, /* nb_inplace_true_divide */
1585 0, /* nb_index */
1586};
Barry Warsaw9bf16442001-01-23 16:24:35 +00001587
Alexandre Vassalotti65846c62013-11-30 17:55:48 -08001588PyTypeObject _PyNone_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001589 PyVarObject_HEAD_INIT(&PyType_Type, 0)
1590 "NoneType",
1591 0,
1592 0,
1593 none_dealloc, /*tp_dealloc*/ /*never called*/
Jeroen Demeyer530f5062019-05-31 04:13:39 +02001594 0, /*tp_vectorcall_offset*/
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001595 0, /*tp_getattr*/
1596 0, /*tp_setattr*/
Jeroen Demeyer530f5062019-05-31 04:13:39 +02001597 0, /*tp_as_async*/
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001598 none_repr, /*tp_repr*/
Raymond Hettinger66d2be82011-07-28 09:55:13 -07001599 &none_as_number, /*tp_as_number*/
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001600 0, /*tp_as_sequence*/
1601 0, /*tp_as_mapping*/
1602 0, /*tp_hash */
Benjamin Petersonc4607ae2011-07-29 18:19:43 -05001603 0, /*tp_call */
1604 0, /*tp_str */
1605 0, /*tp_getattro */
1606 0, /*tp_setattro */
1607 0, /*tp_as_buffer */
1608 Py_TPFLAGS_DEFAULT, /*tp_flags */
1609 0, /*tp_doc */
1610 0, /*tp_traverse */
1611 0, /*tp_clear */
1612 0, /*tp_richcompare */
1613 0, /*tp_weaklistoffset */
1614 0, /*tp_iter */
1615 0, /*tp_iternext */
1616 0, /*tp_methods */
1617 0, /*tp_members */
1618 0, /*tp_getset */
1619 0, /*tp_base */
1620 0, /*tp_dict */
1621 0, /*tp_descr_get */
1622 0, /*tp_descr_set */
1623 0, /*tp_dictoffset */
1624 0, /*tp_init */
1625 0, /*tp_alloc */
1626 none_new, /*tp_new */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001627};
1628
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001629PyObject _Py_NoneStruct = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001630 _PyObject_EXTRA_INIT
Alexandre Vassalotti65846c62013-11-30 17:55:48 -08001631 1, &_PyNone_Type
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001632};
1633
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +00001634/* NotImplemented is an object that can be used to signal that an
1635 operation is not implemented for the given type combination. */
1636
1637static PyObject *
1638NotImplemented_repr(PyObject *op)
1639{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001640 return PyUnicode_FromString("NotImplemented");
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +00001641}
1642
Benjamin Peterson18d7d7a2011-07-29 18:27:44 -05001643static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05301644NotImplemented_reduce(PyObject *op, PyObject *Py_UNUSED(ignored))
Alexandre Vassalottic49477b2013-11-24 02:53:45 -08001645{
1646 return PyUnicode_FromString("NotImplemented");
1647}
1648
1649static PyMethodDef notimplemented_methods[] = {
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05301650 {"__reduce__", NotImplemented_reduce, METH_NOARGS, NULL},
Alexandre Vassalottic49477b2013-11-24 02:53:45 -08001651 {NULL, NULL}
1652};
1653
1654static PyObject *
Benjamin Peterson18d7d7a2011-07-29 18:27:44 -05001655notimplemented_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
1656{
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +02001657 if (PyTuple_GET_SIZE(args) || (kwargs && PyDict_GET_SIZE(kwargs))) {
Benjamin Peterson18d7d7a2011-07-29 18:27:44 -05001658 PyErr_SetString(PyExc_TypeError, "NotImplementedType takes no arguments");
1659 return NULL;
1660 }
Brian Curtindfc80e32011-08-10 20:28:54 -05001661 Py_RETURN_NOTIMPLEMENTED;
Benjamin Peterson18d7d7a2011-07-29 18:27:44 -05001662}
1663
Victor Stinner2a4903f2020-01-30 13:09:11 +01001664static void _Py_NO_RETURN
Armin Ronacher226b1db2012-10-06 14:28:58 +02001665notimplemented_dealloc(PyObject* ignore)
1666{
1667 /* This should never get called, but we also don't want to SEGV if
1668 * we accidentally decref NotImplemented out of existence.
1669 */
1670 Py_FatalError("deallocating NotImplemented");
1671}
1672
Alexandre Vassalotti65846c62013-11-30 17:55:48 -08001673PyTypeObject _PyNotImplemented_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001674 PyVarObject_HEAD_INIT(&PyType_Type, 0)
1675 "NotImplementedType",
1676 0,
1677 0,
Armin Ronacher226b1db2012-10-06 14:28:58 +02001678 notimplemented_dealloc, /*tp_dealloc*/ /*never called*/
Jeroen Demeyer530f5062019-05-31 04:13:39 +02001679 0, /*tp_vectorcall_offset*/
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001680 0, /*tp_getattr*/
1681 0, /*tp_setattr*/
Jeroen Demeyer530f5062019-05-31 04:13:39 +02001682 0, /*tp_as_async*/
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001683 NotImplemented_repr, /*tp_repr*/
1684 0, /*tp_as_number*/
1685 0, /*tp_as_sequence*/
1686 0, /*tp_as_mapping*/
1687 0, /*tp_hash */
Benjamin Peterson18d7d7a2011-07-29 18:27:44 -05001688 0, /*tp_call */
1689 0, /*tp_str */
1690 0, /*tp_getattro */
1691 0, /*tp_setattro */
1692 0, /*tp_as_buffer */
1693 Py_TPFLAGS_DEFAULT, /*tp_flags */
1694 0, /*tp_doc */
1695 0, /*tp_traverse */
1696 0, /*tp_clear */
1697 0, /*tp_richcompare */
1698 0, /*tp_weaklistoffset */
1699 0, /*tp_iter */
1700 0, /*tp_iternext */
Alexandre Vassalottic49477b2013-11-24 02:53:45 -08001701 notimplemented_methods, /*tp_methods */
Benjamin Peterson18d7d7a2011-07-29 18:27:44 -05001702 0, /*tp_members */
1703 0, /*tp_getset */
1704 0, /*tp_base */
1705 0, /*tp_dict */
1706 0, /*tp_descr_get */
1707 0, /*tp_descr_set */
1708 0, /*tp_dictoffset */
1709 0, /*tp_init */
1710 0, /*tp_alloc */
1711 notimplemented_new, /*tp_new */
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +00001712};
1713
1714PyObject _Py_NotImplementedStruct = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001715 _PyObject_EXTRA_INIT
Alexandre Vassalotti65846c62013-11-30 17:55:48 -08001716 1, &_PyNotImplemented_Type
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +00001717};
1718
Victor Stinner331a6a52019-05-27 16:39:22 +02001719PyStatus
Victor Stinnerab672812019-01-23 15:04:40 +01001720_PyTypes_Init(void)
Guido van Rossumba21a492001-08-16 08:17:26 +00001721{
Victor Stinner7a1f6c22020-01-30 09:02:14 +01001722 PyStatus status = _PyTypes_InitSlotDefs();
1723 if (_PyStatus_EXCEPTION(status)) {
1724 return status;
1725 }
1726
Victor Stinner6d43f6f2019-01-22 21:18:05 +01001727#define INIT_TYPE(TYPE, NAME) \
1728 do { \
1729 if (PyType_Ready(TYPE) < 0) { \
Victor Stinner331a6a52019-05-27 16:39:22 +02001730 return _PyStatus_ERR("Can't initialize " NAME " type"); \
Victor Stinner6d43f6f2019-01-22 21:18:05 +01001731 } \
1732 } while (0)
Victor Stinner5a1bb4e2014-06-02 14:10:59 +02001733
Victor Stinner6d43f6f2019-01-22 21:18:05 +01001734 INIT_TYPE(&PyBaseObject_Type, "object");
1735 INIT_TYPE(&PyType_Type, "type");
1736 INIT_TYPE(&_PyWeakref_RefType, "weakref");
1737 INIT_TYPE(&_PyWeakref_CallableProxyType, "callable weakref proxy");
1738 INIT_TYPE(&_PyWeakref_ProxyType, "weakref proxy");
1739 INIT_TYPE(&PyLong_Type, "int");
1740 INIT_TYPE(&PyBool_Type, "bool");
1741 INIT_TYPE(&PyByteArray_Type, "bytearray");
1742 INIT_TYPE(&PyBytes_Type, "str");
1743 INIT_TYPE(&PyList_Type, "list");
1744 INIT_TYPE(&_PyNone_Type, "None");
1745 INIT_TYPE(&_PyNotImplemented_Type, "NotImplemented");
1746 INIT_TYPE(&PyTraceBack_Type, "traceback");
1747 INIT_TYPE(&PySuper_Type, "super");
1748 INIT_TYPE(&PyRange_Type, "range");
1749 INIT_TYPE(&PyDict_Type, "dict");
1750 INIT_TYPE(&PyDictKeys_Type, "dict keys");
1751 INIT_TYPE(&PyDictValues_Type, "dict values");
1752 INIT_TYPE(&PyDictItems_Type, "dict items");
1753 INIT_TYPE(&PyDictRevIterKey_Type, "reversed dict keys");
1754 INIT_TYPE(&PyDictRevIterValue_Type, "reversed dict values");
1755 INIT_TYPE(&PyDictRevIterItem_Type, "reversed dict items");
1756 INIT_TYPE(&PyODict_Type, "OrderedDict");
1757 INIT_TYPE(&PyODictKeys_Type, "odict_keys");
1758 INIT_TYPE(&PyODictItems_Type, "odict_items");
1759 INIT_TYPE(&PyODictValues_Type, "odict_values");
1760 INIT_TYPE(&PyODictIter_Type, "odict_keyiterator");
1761 INIT_TYPE(&PySet_Type, "set");
1762 INIT_TYPE(&PyUnicode_Type, "str");
1763 INIT_TYPE(&PySlice_Type, "slice");
1764 INIT_TYPE(&PyStaticMethod_Type, "static method");
1765 INIT_TYPE(&PyComplex_Type, "complex");
1766 INIT_TYPE(&PyFloat_Type, "float");
1767 INIT_TYPE(&PyFrozenSet_Type, "frozenset");
1768 INIT_TYPE(&PyProperty_Type, "property");
1769 INIT_TYPE(&_PyManagedBuffer_Type, "managed buffer");
1770 INIT_TYPE(&PyMemoryView_Type, "memoryview");
1771 INIT_TYPE(&PyTuple_Type, "tuple");
1772 INIT_TYPE(&PyEnum_Type, "enumerate");
1773 INIT_TYPE(&PyReversed_Type, "reversed");
1774 INIT_TYPE(&PyStdPrinter_Type, "StdPrinter");
1775 INIT_TYPE(&PyCode_Type, "code");
1776 INIT_TYPE(&PyFrame_Type, "frame");
1777 INIT_TYPE(&PyCFunction_Type, "builtin function");
1778 INIT_TYPE(&PyMethod_Type, "method");
1779 INIT_TYPE(&PyFunction_Type, "function");
1780 INIT_TYPE(&PyDictProxy_Type, "dict proxy");
1781 INIT_TYPE(&PyGen_Type, "generator");
1782 INIT_TYPE(&PyGetSetDescr_Type, "get-set descriptor");
1783 INIT_TYPE(&PyWrapperDescr_Type, "wrapper");
1784 INIT_TYPE(&_PyMethodWrapper_Type, "method wrapper");
1785 INIT_TYPE(&PyEllipsis_Type, "ellipsis");
1786 INIT_TYPE(&PyMemberDescr_Type, "member descriptor");
1787 INIT_TYPE(&_PyNamespace_Type, "namespace");
1788 INIT_TYPE(&PyCapsule_Type, "capsule");
1789 INIT_TYPE(&PyLongRangeIter_Type, "long range iterator");
1790 INIT_TYPE(&PyCell_Type, "cell");
1791 INIT_TYPE(&PyInstanceMethod_Type, "instance method");
1792 INIT_TYPE(&PyClassMethodDescr_Type, "class method descr");
1793 INIT_TYPE(&PyMethodDescr_Type, "method descr");
1794 INIT_TYPE(&PyCallIter_Type, "call iter");
1795 INIT_TYPE(&PySeqIter_Type, "sequence iterator");
Antoine Pitrou91f43802019-05-26 17:10:09 +02001796 INIT_TYPE(&PyPickleBuffer_Type, "pickle.PickleBuffer");
Victor Stinner6d43f6f2019-01-22 21:18:05 +01001797 INIT_TYPE(&PyCoro_Type, "coroutine");
1798 INIT_TYPE(&_PyCoroWrapper_Type, "coroutine wrapper");
Eric Snowc11183c2019-03-15 16:35:46 -06001799 INIT_TYPE(&_PyInterpreterID_Type, "interpreter ID");
Victor Stinner331a6a52019-05-27 16:39:22 +02001800 return _PyStatus_OK();
Guido van Rossumba21a492001-08-16 08:17:26 +00001801
Victor Stinner6d43f6f2019-01-22 21:18:05 +01001802#undef INIT_TYPE
Guido van Rossumba21a492001-08-16 08:17:26 +00001803}
1804
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001805
Guido van Rossum84a90321996-05-22 16:34:47 +00001806#ifdef Py_TRACE_REFS
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001807
Guido van Rossumaacdc9d1996-08-12 21:32:12 +00001808void
Fred Drake100814d2000-07-09 15:48:49 +00001809_Py_NewReference(PyObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001810{
Victor Stinner9e00e802018-10-25 13:31:16 +02001811 if (_Py_tracemalloc_config.tracing) {
1812 _PyTraceMalloc_NewReference(op);
1813 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001814 _Py_INC_REFTOTAL;
1815 op->ob_refcnt = 1;
1816 _Py_AddToAllObjects(op, 1);
1817 _Py_INC_TPALLOCS(op);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001818}
1819
Guido van Rossumaacdc9d1996-08-12 21:32:12 +00001820void
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02001821_Py_ForgetReference(PyObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001822{
Victor Stinner5eb8bff2020-01-30 09:01:07 +01001823 if (op->ob_refcnt < 0) {
1824 _PyObject_ASSERT_FAILED_MSG(op, "negative refcnt");
1825 }
1826
1827 if (op == &refchain ||
1828 op->_ob_prev->_ob_next != op || op->_ob_next->_ob_prev != op)
1829 {
1830 _PyObject_ASSERT_FAILED_MSG(op, "invalid object chain");
1831 }
1832
Guido van Rossumbffd6832000-01-20 22:32:56 +00001833#ifdef SLOW_UNREF_CHECK
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02001834 PyObject *p;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001835 for (p = refchain._ob_next; p != &refchain; p = p->_ob_next) {
Victor Stinner5eb8bff2020-01-30 09:01:07 +01001836 if (p == op) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001837 break;
Victor Stinner5eb8bff2020-01-30 09:01:07 +01001838 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001839 }
Victor Stinner5eb8bff2020-01-30 09:01:07 +01001840 if (p == &refchain) {
1841 /* Not found */
1842 _PyObject_ASSERT_FAILED_MSG(op,
1843 "object not found in the objects list");
1844 }
Guido van Rossum2e8f6141992-09-03 20:32:55 +00001845#endif
Victor Stinner5eb8bff2020-01-30 09:01:07 +01001846
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001847 op->_ob_next->_ob_prev = op->_ob_prev;
1848 op->_ob_prev->_ob_next = op->_ob_next;
1849 op->_ob_next = op->_ob_prev = NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001850}
1851
Tim Peters269b2a62003-04-17 19:52:29 +00001852/* Print all live objects. Because PyObject_Print is called, the
1853 * interpreter must be in a healthy state.
1854 */
Guido van Rossumaacdc9d1996-08-12 21:32:12 +00001855void
Fred Drake100814d2000-07-09 15:48:49 +00001856_Py_PrintReferences(FILE *fp)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001857{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001858 PyObject *op;
1859 fprintf(fp, "Remaining objects:\n");
1860 for (op = refchain._ob_next; op != &refchain; op = op->_ob_next) {
Zackery Spytz1a2252e2019-05-06 10:56:51 -06001861 fprintf(fp, "%p [%" PY_FORMAT_SIZE_T "d] ", (void *)op, op->ob_refcnt);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001862 if (PyObject_Print(op, fp, 0) != 0)
1863 PyErr_Clear();
1864 putc('\n', fp);
1865 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001866}
1867
Tim Peters269b2a62003-04-17 19:52:29 +00001868/* Print the addresses of all live objects. Unlike _Py_PrintReferences, this
1869 * doesn't make any calls to the Python C API, so is always safe to call.
1870 */
1871void
1872_Py_PrintReferenceAddresses(FILE *fp)
1873{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001874 PyObject *op;
1875 fprintf(fp, "Remaining object addresses:\n");
1876 for (op = refchain._ob_next; op != &refchain; op = op->_ob_next)
Zackery Spytz1a2252e2019-05-06 10:56:51 -06001877 fprintf(fp, "%p [%" PY_FORMAT_SIZE_T "d] %s\n", (void *)op,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001878 op->ob_refcnt, Py_TYPE(op)->tp_name);
Tim Peters269b2a62003-04-17 19:52:29 +00001879}
1880
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +00001881PyObject *
Fred Drake100814d2000-07-09 15:48:49 +00001882_Py_GetObjects(PyObject *self, PyObject *args)
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +00001883{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001884 int i, n;
1885 PyObject *t = NULL;
1886 PyObject *res, *op;
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +00001887
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001888 if (!PyArg_ParseTuple(args, "i|O", &n, &t))
1889 return NULL;
1890 op = refchain._ob_next;
1891 res = PyList_New(0);
1892 if (res == NULL)
1893 return NULL;
1894 for (i = 0; (n == 0 || i < n) && op != &refchain; i++) {
1895 while (op == self || op == args || op == res || op == t ||
1896 (t != NULL && Py_TYPE(op) != (PyTypeObject *) t)) {
1897 op = op->_ob_next;
1898 if (op == &refchain)
1899 return res;
1900 }
1901 if (PyList_Append(res, op) < 0) {
1902 Py_DECREF(res);
1903 return NULL;
1904 }
1905 op = op->_ob_next;
1906 }
1907 return res;
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +00001908}
1909
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001910#endif
Guido van Rossum97ead3f1996-01-12 01:24:09 +00001911
Benjamin Petersonb173f782009-05-05 22:31:58 +00001912
Guido van Rossum84a90321996-05-22 16:34:47 +00001913/* Hack to force loading of abstract.o */
Martin v. Löwis18e16552006-02-15 17:27:45 +00001914Py_ssize_t (*_Py_abstract_hack)(PyObject *) = PyObject_Size;
Guido van Rossume09fb551997-08-05 02:04:34 +00001915
1916
David Malcolm49526f42012-06-22 14:55:41 -04001917void
1918_PyObject_DebugTypeStats(FILE *out)
1919{
David Malcolm49526f42012-06-22 14:55:41 -04001920 _PyDict_DebugMallocStats(out);
1921 _PyFloat_DebugMallocStats(out);
1922 _PyFrame_DebugMallocStats(out);
1923 _PyList_DebugMallocStats(out);
David Malcolm49526f42012-06-22 14:55:41 -04001924 _PyTuple_DebugMallocStats(out);
1925}
Guido van Rossumb18618d2000-05-03 23:44:39 +00001926
Guido van Rossum86610361998-04-10 22:32:46 +00001927/* These methods are used to control infinite recursion in repr, str, print,
1928 etc. Container objects that may recursively contain themselves,
Martin Panter8d56c022016-05-29 04:13:35 +00001929 e.g. builtin dictionaries and lists, should use Py_ReprEnter() and
Guido van Rossum86610361998-04-10 22:32:46 +00001930 Py_ReprLeave() to avoid infinite recursion.
1931
1932 Py_ReprEnter() returns 0 the first time it is called for a particular
1933 object and 1 every time thereafter. It returns -1 if an exception
1934 occurred. Py_ReprLeave() has no return value.
1935
1936 See dictobject.c and listobject.c for examples of use.
1937*/
1938
Guido van Rossum86610361998-04-10 22:32:46 +00001939int
Fred Drake100814d2000-07-09 15:48:49 +00001940Py_ReprEnter(PyObject *obj)
Guido van Rossum86610361998-04-10 22:32:46 +00001941{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001942 PyObject *dict;
1943 PyObject *list;
1944 Py_ssize_t i;
Guido van Rossum86610361998-04-10 22:32:46 +00001945
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001946 dict = PyThreadState_GetDict();
Antoine Pitrou04d17d32014-03-31 22:04:38 +02001947 /* Ignore a missing thread-state, so that this function can be called
1948 early on startup. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001949 if (dict == NULL)
1950 return 0;
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02001951 list = _PyDict_GetItemIdWithError(dict, &PyId_Py_Repr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001952 if (list == NULL) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02001953 if (PyErr_Occurred()) {
1954 return -1;
1955 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001956 list = PyList_New(0);
1957 if (list == NULL)
1958 return -1;
Victor Stinner7a07e452013-11-06 18:57:29 +01001959 if (_PyDict_SetItemId(dict, &PyId_Py_Repr, list) < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001960 return -1;
1961 Py_DECREF(list);
1962 }
1963 i = PyList_GET_SIZE(list);
1964 while (--i >= 0) {
1965 if (PyList_GET_ITEM(list, i) == obj)
1966 return 1;
1967 }
Victor Stinnere901d1f2013-07-17 21:58:41 +02001968 if (PyList_Append(list, obj) < 0)
1969 return -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001970 return 0;
Guido van Rossum86610361998-04-10 22:32:46 +00001971}
1972
1973void
Fred Drake100814d2000-07-09 15:48:49 +00001974Py_ReprLeave(PyObject *obj)
Guido van Rossum86610361998-04-10 22:32:46 +00001975{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001976 PyObject *dict;
1977 PyObject *list;
1978 Py_ssize_t i;
Victor Stinner1b634932013-07-16 22:24:44 +02001979 PyObject *error_type, *error_value, *error_traceback;
1980
1981 PyErr_Fetch(&error_type, &error_value, &error_traceback);
Guido van Rossum86610361998-04-10 22:32:46 +00001982
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001983 dict = PyThreadState_GetDict();
1984 if (dict == NULL)
Victor Stinner1b634932013-07-16 22:24:44 +02001985 goto finally;
1986
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02001987 list = _PyDict_GetItemIdWithError(dict, &PyId_Py_Repr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001988 if (list == NULL || !PyList_Check(list))
Victor Stinner1b634932013-07-16 22:24:44 +02001989 goto finally;
1990
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001991 i = PyList_GET_SIZE(list);
1992 /* Count backwards because we always expect obj to be list[-1] */
1993 while (--i >= 0) {
1994 if (PyList_GET_ITEM(list, i) == obj) {
1995 PyList_SetSlice(list, i, i + 1, NULL);
1996 break;
1997 }
1998 }
Victor Stinner1b634932013-07-16 22:24:44 +02001999
2000finally:
2001 /* ignore exceptions because there is no way to report them. */
2002 PyErr_Restore(error_type, error_value, error_traceback);
Guido van Rossum86610361998-04-10 22:32:46 +00002003}
Guido van Rossumd724b232000-03-13 16:01:29 +00002004
Tim Peters803526b2002-07-07 05:13:56 +00002005/* Trashcan support. */
Guido van Rossumd724b232000-03-13 16:01:29 +00002006
Tim Peters803526b2002-07-07 05:13:56 +00002007/* Add op to the _PyTrash_delete_later list. Called when the current
2008 * call-stack depth gets large. op must be a currently untracked gc'ed
2009 * object, with refcount 0. Py_DECREF must already have been called on it.
2010 */
Guido van Rossumd724b232000-03-13 16:01:29 +00002011void
Fred Drake100814d2000-07-09 15:48:49 +00002012_PyTrash_deposit_object(PyObject *op)
Guido van Rossumd724b232000-03-13 16:01:29 +00002013{
Victor Stinner72474072019-11-20 12:25:50 +01002014 PyThreadState *tstate = _PyThreadState_GET();
2015 struct _gc_runtime_state *gcstate = &tstate->interp->gc;
2016
Victor Stinner24702042018-10-26 17:16:37 +02002017 _PyObject_ASSERT(op, PyObject_IS_GC(op));
2018 _PyObject_ASSERT(op, !_PyObject_GC_IS_TRACKED(op));
2019 _PyObject_ASSERT(op, op->ob_refcnt == 0);
Victor Stinner72474072019-11-20 12:25:50 +01002020 _PyGCHead_SET_PREV(_Py_AS_GC(op), gcstate->trash_delete_later);
2021 gcstate->trash_delete_later = op;
Guido van Rossumd724b232000-03-13 16:01:29 +00002022}
2023
Antoine Pitrou2b0218a2012-09-06 00:59:49 +02002024/* The equivalent API, using per-thread state recursion info */
2025void
2026_PyTrash_thread_deposit_object(PyObject *op)
2027{
Victor Stinner50b48572018-11-01 01:51:40 +01002028 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinner24702042018-10-26 17:16:37 +02002029 _PyObject_ASSERT(op, PyObject_IS_GC(op));
2030 _PyObject_ASSERT(op, !_PyObject_GC_IS_TRACKED(op));
2031 _PyObject_ASSERT(op, op->ob_refcnt == 0);
INADA Naoki5ac9e6e2018-07-10 17:19:53 +09002032 _PyGCHead_SET_PREV(_Py_AS_GC(op), tstate->trash_delete_later);
Antoine Pitrou2b0218a2012-09-06 00:59:49 +02002033 tstate->trash_delete_later = op;
2034}
2035
Min ho Kimc4cacc82019-07-31 08:16:13 +10002036/* Deallocate all the objects in the _PyTrash_delete_later list. Called when
Tim Peters803526b2002-07-07 05:13:56 +00002037 * the call-stack unwinds again.
2038 */
Guido van Rossumd724b232000-03-13 16:01:29 +00002039void
Fred Drake100814d2000-07-09 15:48:49 +00002040_PyTrash_destroy_chain(void)
Guido van Rossumd724b232000-03-13 16:01:29 +00002041{
Victor Stinner72474072019-11-20 12:25:50 +01002042 PyThreadState *tstate = _PyThreadState_GET();
2043 struct _gc_runtime_state *gcstate = &tstate->interp->gc;
2044
2045 while (gcstate->trash_delete_later) {
2046 PyObject *op = gcstate->trash_delete_later;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002047 destructor dealloc = Py_TYPE(op)->tp_dealloc;
Neil Schemenauerf589c052002-03-29 03:05:54 +00002048
Victor Stinner72474072019-11-20 12:25:50 +01002049 gcstate->trash_delete_later =
INADA Naoki5ac9e6e2018-07-10 17:19:53 +09002050 (PyObject*) _PyGCHead_PREV(_Py_AS_GC(op));
Neil Schemenauerf589c052002-03-29 03:05:54 +00002051
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002052 /* Call the deallocator directly. This used to try to
2053 * fool Py_DECREF into calling it indirectly, but
2054 * Py_DECREF was already called on this object, and in
2055 * assorted non-release builds calling Py_DECREF again ends
2056 * up distorting allocation statistics.
2057 */
Victor Stinner24702042018-10-26 17:16:37 +02002058 _PyObject_ASSERT(op, op->ob_refcnt == 0);
Victor Stinner72474072019-11-20 12:25:50 +01002059 ++gcstate->trash_delete_nesting;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002060 (*dealloc)(op);
Victor Stinner72474072019-11-20 12:25:50 +01002061 --gcstate->trash_delete_nesting;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002062 }
Guido van Rossumd724b232000-03-13 16:01:29 +00002063}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002064
Antoine Pitrou2b0218a2012-09-06 00:59:49 +02002065/* The equivalent API, using per-thread state recursion info */
2066void
2067_PyTrash_thread_destroy_chain(void)
2068{
Victor Stinner50b48572018-11-01 01:51:40 +01002069 PyThreadState *tstate = _PyThreadState_GET();
Xiang Zhanga66f9c62017-05-13 13:36:14 +08002070 /* We need to increase trash_delete_nesting here, otherwise,
2071 _PyTrash_thread_destroy_chain will be called recursively
2072 and then possibly crash. An example that may crash without
2073 increase:
2074 N = 500000 # need to be large enough
2075 ob = object()
2076 tups = [(ob,) for i in range(N)]
2077 for i in range(49):
2078 tups = [(tup,) for tup in tups]
2079 del tups
2080 */
2081 assert(tstate->trash_delete_nesting == 0);
2082 ++tstate->trash_delete_nesting;
Antoine Pitrou2b0218a2012-09-06 00:59:49 +02002083 while (tstate->trash_delete_later) {
2084 PyObject *op = tstate->trash_delete_later;
2085 destructor dealloc = Py_TYPE(op)->tp_dealloc;
2086
2087 tstate->trash_delete_later =
INADA Naoki5ac9e6e2018-07-10 17:19:53 +09002088 (PyObject*) _PyGCHead_PREV(_Py_AS_GC(op));
Antoine Pitrou2b0218a2012-09-06 00:59:49 +02002089
2090 /* Call the deallocator directly. This used to try to
2091 * fool Py_DECREF into calling it indirectly, but
2092 * Py_DECREF was already called on this object, and in
2093 * assorted non-release builds calling Py_DECREF again ends
2094 * up distorting allocation statistics.
2095 */
Victor Stinner24702042018-10-26 17:16:37 +02002096 _PyObject_ASSERT(op, op->ob_refcnt == 0);
Antoine Pitrou2b0218a2012-09-06 00:59:49 +02002097 (*dealloc)(op);
Xiang Zhanga66f9c62017-05-13 13:36:14 +08002098 assert(tstate->trash_delete_nesting == 1);
Antoine Pitrou2b0218a2012-09-06 00:59:49 +02002099 }
Xiang Zhanga66f9c62017-05-13 13:36:14 +08002100 --tstate->trash_delete_nesting;
Antoine Pitrou2b0218a2012-09-06 00:59:49 +02002101}
2102
Victor Stinner626bff82018-10-25 17:31:10 +02002103
Victor Stinner2a4903f2020-01-30 13:09:11 +01002104void _Py_NO_RETURN
Victor Stinnerf1d002c2018-11-21 23:53:44 +01002105_PyObject_AssertFailed(PyObject *obj, const char *expr, const char *msg,
Victor Stinner626bff82018-10-25 17:31:10 +02002106 const char *file, int line, const char *function)
2107{
Victor Stinnerf1d002c2018-11-21 23:53:44 +01002108 fprintf(stderr, "%s:%d: ", file, line);
2109 if (function) {
2110 fprintf(stderr, "%s: ", function);
2111 }
Victor Stinner626bff82018-10-25 17:31:10 +02002112 fflush(stderr);
Victor Stinner68762572019-10-07 18:42:01 +02002113
Victor Stinnerf1d002c2018-11-21 23:53:44 +01002114 if (expr) {
2115 fprintf(stderr, "Assertion \"%s\" failed", expr);
Victor Stinner626bff82018-10-25 17:31:10 +02002116 }
2117 else {
Victor Stinnerf1d002c2018-11-21 23:53:44 +01002118 fprintf(stderr, "Assertion failed");
Victor Stinner626bff82018-10-25 17:31:10 +02002119 }
2120 fflush(stderr);
Victor Stinner68762572019-10-07 18:42:01 +02002121
Victor Stinnerf1d002c2018-11-21 23:53:44 +01002122 if (msg) {
2123 fprintf(stderr, ": %s", msg);
2124 }
2125 fprintf(stderr, "\n");
2126 fflush(stderr);
Victor Stinner626bff82018-10-25 17:31:10 +02002127
Victor Stinner68762572019-10-07 18:42:01 +02002128 if (_PyObject_IsFreed(obj)) {
Victor Stinner626bff82018-10-25 17:31:10 +02002129 /* It seems like the object memory has been freed:
2130 don't access it to prevent a segmentation fault. */
Victor Stinnerb39afb72019-09-17 23:36:28 +02002131 fprintf(stderr, "<object at %p is freed>\n", obj);
Victor Stinner68762572019-10-07 18:42:01 +02002132 fflush(stderr);
Victor Stinner626bff82018-10-25 17:31:10 +02002133 }
2134 else {
penguindustin96466302019-05-06 14:57:17 -04002135 /* Display the traceback where the object has been allocated.
Victor Stinner626bff82018-10-25 17:31:10 +02002136 Do it before dumping repr(obj), since repr() is more likely
2137 to crash than dumping the traceback. */
2138 void *ptr;
2139 PyTypeObject *type = Py_TYPE(obj);
2140 if (PyType_IS_GC(type)) {
2141 ptr = (void *)((char *)obj - sizeof(PyGC_Head));
2142 }
2143 else {
2144 ptr = (void *)obj;
2145 }
2146 _PyMem_DumpTraceback(fileno(stderr), ptr);
2147
2148 /* This might succeed or fail, but we're about to abort, so at least
2149 try to provide any extra info we can: */
2150 _PyObject_Dump(obj);
Victor Stinner77753492019-10-07 23:44:05 +02002151
2152 fprintf(stderr, "\n");
2153 fflush(stderr);
Victor Stinner626bff82018-10-25 17:31:10 +02002154 }
Victor Stinner626bff82018-10-25 17:31:10 +02002155
2156 Py_FatalError("_PyObject_AssertFailed");
2157}
2158
Victor Stinner3c09dca2018-10-30 14:48:26 +01002159
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00002160#undef _Py_Dealloc
Victor Stinner3c09dca2018-10-30 14:48:26 +01002161
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00002162void
2163_Py_Dealloc(PyObject *op)
2164{
Victor Stinner3c09dca2018-10-30 14:48:26 +01002165 destructor dealloc = Py_TYPE(op)->tp_dealloc;
2166#ifdef Py_TRACE_REFS
2167 _Py_ForgetReference(op);
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00002168#endif
Victor Stinner3c09dca2018-10-30 14:48:26 +01002169 (*dealloc)(op);
2170}
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00002171
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002172#ifdef __cplusplus
2173}
2174#endif