blob: ff6c497900cf207b0a4165af4e10a77f62ac1c39 [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
Victor Stinner58ac7002020-02-07 03:04:21 +010036 CHECK(Py_TYPE(op) != NULL);
37 _PyType_CheckConsistency(Py_TYPE(op));
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)
Victor Stinnera93c51e2020-02-07 00:38:59 +010061 total -= Py_REFCNT(o);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000062 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 */
Victor Stinner58f4e1a2020-02-05 18:24:33 +010096void
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{
Victor Stinnerf58bd7c2020-02-05 13:12:19 +0100142 /* Any changes should be reflected in PyObject_INIT() macro */
143 if (op == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000144 return PyErr_NoMemory();
Victor Stinnerf58bd7c2020-02-05 13:12:19 +0100145 }
146
Victor Stinnerd2ec81a2020-02-07 09:17:07 +0100147 Py_SET_TYPE(op, tp);
Eddie Elizondo364f0b02019-03-27 07:52:18 -0400148 if (PyType_GetFlags(tp) & Py_TPFLAGS_HEAPTYPE) {
149 Py_INCREF(tp);
150 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000151 _Py_NewReference(op);
152 return op;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000153}
154
Guido van Rossumb18618d2000-05-03 23:44:39 +0000155PyVarObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000156PyObject_InitVar(PyVarObject *op, PyTypeObject *tp, Py_ssize_t size)
Guido van Rossumb18618d2000-05-03 23:44:39 +0000157{
Victor Stinnerf58bd7c2020-02-05 13:12:19 +0100158 /* Any changes should be reflected in PyObject_INIT_VAR() macro */
159 if (op == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000160 return (PyVarObject *) PyErr_NoMemory();
Victor Stinnerf58bd7c2020-02-05 13:12:19 +0100161 }
162
Eddie Elizondo364f0b02019-03-27 07:52:18 -0400163 Py_SIZE(op) = size;
164 PyObject_Init((PyObject *)op, tp);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000165 return op;
Guido van Rossumb18618d2000-05-03 23:44:39 +0000166}
167
168PyObject *
Fred Drake100814d2000-07-09 15:48:49 +0000169_PyObject_New(PyTypeObject *tp)
Guido van Rossumb18618d2000-05-03 23:44:39 +0000170{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000171 PyObject *op;
172 op = (PyObject *) PyObject_MALLOC(_PyObject_SIZE(tp));
173 if (op == NULL)
174 return PyErr_NoMemory();
175 return PyObject_INIT(op, tp);
Guido van Rossumb18618d2000-05-03 23:44:39 +0000176}
177
Guido van Rossumd0c87ee1997-05-15 21:31:03 +0000178PyVarObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000179_PyObject_NewVar(PyTypeObject *tp, Py_ssize_t nitems)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000180{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000181 PyVarObject *op;
182 const size_t size = _PyObject_VAR_SIZE(tp, nitems);
183 op = (PyVarObject *) PyObject_MALLOC(size);
184 if (op == NULL)
185 return (PyVarObject *)PyErr_NoMemory();
186 return PyObject_INIT_VAR(op, tp, nitems);
Guido van Rossumb18618d2000-05-03 23:44:39 +0000187}
188
Antoine Pitrou796564c2013-07-30 19:59:21 +0200189void
190PyObject_CallFinalizer(PyObject *self)
191{
192 PyTypeObject *tp = Py_TYPE(self);
193
Antoine Pitrouada319b2019-05-29 22:12:38 +0200194 if (tp->tp_finalize == NULL)
Antoine Pitrou796564c2013-07-30 19:59:21 +0200195 return;
196 /* tp_finalize should only be called once. */
197 if (PyType_IS_GC(tp) && _PyGC_FINALIZED(self))
198 return;
199
200 tp->tp_finalize(self);
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900201 if (PyType_IS_GC(tp)) {
202 _PyGC_SET_FINALIZED(self);
203 }
Antoine Pitrou796564c2013-07-30 19:59:21 +0200204}
205
206int
207PyObject_CallFinalizerFromDealloc(PyObject *self)
208{
Victor Stinnera93c51e2020-02-07 00:38:59 +0100209 if (Py_REFCNT(self) != 0) {
Victor Stinner5eb8bff2020-01-30 09:01:07 +0100210 _PyObject_ASSERT_FAILED_MSG(self,
211 "PyObject_CallFinalizerFromDealloc called "
212 "on object with a non-zero refcount");
213 }
Antoine Pitrou796564c2013-07-30 19:59:21 +0200214
215 /* Temporarily resurrect the object. */
Victor Stinnerc86a1122020-02-07 01:24:29 +0100216 Py_SET_REFCNT(self, 1);
Antoine Pitrou796564c2013-07-30 19:59:21 +0200217
218 PyObject_CallFinalizer(self);
219
Victor Stinner24702042018-10-26 17:16:37 +0200220 _PyObject_ASSERT_WITH_MSG(self,
Victor Stinnera93c51e2020-02-07 00:38:59 +0100221 Py_REFCNT(self) > 0,
Victor Stinner24702042018-10-26 17:16:37 +0200222 "refcount is too small");
Victor Stinner5eb8bff2020-01-30 09:01:07 +0100223
224 /* Undo the temporary resurrection; can't use DECREF here, it would
225 * cause a recursive call. */
Victor Stinnerc86a1122020-02-07 01:24:29 +0100226 Py_SET_REFCNT(self, Py_REFCNT(self) - 1);
227 if (Py_REFCNT(self) == 0) {
Antoine Pitrou796564c2013-07-30 19:59:21 +0200228 return 0; /* this is the normal path out */
Victor Stinner5eb8bff2020-01-30 09:01:07 +0100229 }
Antoine Pitrou796564c2013-07-30 19:59:21 +0200230
231 /* tp_finalize resurrected it! Make it look like the original Py_DECREF
Victor Stinner5eb8bff2020-01-30 09:01:07 +0100232 * never happened. */
Victor Stinnera93c51e2020-02-07 00:38:59 +0100233 Py_ssize_t refcnt = Py_REFCNT(self);
Antoine Pitrou796564c2013-07-30 19:59:21 +0200234 _Py_NewReference(self);
Victor Stinnerc86a1122020-02-07 01:24:29 +0100235 Py_SET_REFCNT(self, refcnt);
Antoine Pitrou796564c2013-07-30 19:59:21 +0200236
Victor Stinner24702042018-10-26 17:16:37 +0200237 _PyObject_ASSERT(self,
238 (!PyType_IS_GC(Py_TYPE(self))
239 || _PyObject_GC_IS_TRACKED(self)));
Victor Stinner49932fe2020-02-03 17:55:05 +0100240 /* If Py_REF_DEBUG macro is defined, _Py_NewReference() increased
241 _Py_RefTotal, so we need to undo that. */
242#ifdef Py_REF_DEBUG
243 _Py_RefTotal--;
244#endif
Antoine Pitrou796564c2013-07-30 19:59:21 +0200245 return -1;
246}
247
Antoine Pitrouc47bd4a2010-07-27 22:08:27 +0000248int
249PyObject_Print(PyObject *op, FILE *fp, int flags)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000250{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000251 int ret = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000252 if (PyErr_CheckSignals())
253 return -1;
Guido van Rossum9b00dfa1998-04-28 16:06:54 +0000254#ifdef USE_STACKCHECK
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000255 if (PyOS_CheckStack()) {
256 PyErr_SetString(PyExc_MemoryError, "stack overflow");
257 return -1;
258 }
Guido van Rossum9b00dfa1998-04-28 16:06:54 +0000259#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000260 clearerr(fp); /* Clear any previous error condition */
261 if (op == NULL) {
262 Py_BEGIN_ALLOW_THREADS
263 fprintf(fp, "<nil>");
264 Py_END_ALLOW_THREADS
265 }
266 else {
Victor Stinnera93c51e2020-02-07 00:38:59 +0100267 if (Py_REFCNT(op) <= 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000268 /* XXX(twouters) cast refcount to long until %zd is
269 universally available */
270 Py_BEGIN_ALLOW_THREADS
271 fprintf(fp, "<refcnt %ld at %p>",
Victor Stinnera93c51e2020-02-07 00:38:59 +0100272 (long)Py_REFCNT(op), (void *)op);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000273 Py_END_ALLOW_THREADS
Victor Stinner3ec9af72018-10-26 02:12:34 +0200274 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000275 else {
276 PyObject *s;
277 if (flags & Py_PRINT_RAW)
278 s = PyObject_Str(op);
279 else
280 s = PyObject_Repr(op);
281 if (s == NULL)
282 ret = -1;
283 else if (PyBytes_Check(s)) {
284 fwrite(PyBytes_AS_STRING(s), 1,
285 PyBytes_GET_SIZE(s), fp);
286 }
287 else if (PyUnicode_Check(s)) {
288 PyObject *t;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200289 t = PyUnicode_AsEncodedString(s, "utf-8", "backslashreplace");
Zackery Spytzae62f012018-10-06 00:44:25 -0600290 if (t == NULL) {
291 ret = -1;
292 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000293 else {
294 fwrite(PyBytes_AS_STRING(t), 1,
295 PyBytes_GET_SIZE(t), fp);
Victor Stinnerba6b4302010-05-17 09:33:42 +0000296 Py_DECREF(t);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000297 }
298 }
299 else {
300 PyErr_Format(PyExc_TypeError,
301 "str() or repr() returned '%.100s'",
Victor Stinner58ac7002020-02-07 03:04:21 +0100302 Py_TYPE(s)->tp_name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000303 ret = -1;
304 }
305 Py_XDECREF(s);
306 }
307 }
308 if (ret == 0) {
309 if (ferror(fp)) {
Serhiy Storchaka55fe1ae2017-04-16 10:46:38 +0300310 PyErr_SetFromErrno(PyExc_OSError);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000311 clearerr(fp);
312 ret = -1;
313 }
314 }
315 return ret;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000316}
317
Guido van Rossum38938152006-08-21 23:36:26 +0000318/* For debugging convenience. Set a breakpoint here and call it from your DLL */
319void
Thomas Woutersb2137042007-02-01 18:02:27 +0000320_Py_BreakPoint(void)
Guido van Rossum38938152006-08-21 23:36:26 +0000321{
322}
323
Neal Norwitz1a997502003-01-13 20:13:12 +0000324
Victor Stinner4c409be2019-04-11 13:01:15 +0200325/* Heuristic checking if the object memory is uninitialized or deallocated.
326 Rely on the debug hooks on Python memory allocators:
327 see _PyMem_IsPtrFreed().
Victor Stinner82af0b62018-10-23 17:39:40 +0200328
329 The function can be used to prevent segmentation fault on dereferencing
Victor Stinner4c409be2019-04-11 13:01:15 +0200330 pointers like 0xDDDDDDDDDDDDDDDD. */
Victor Stinner82af0b62018-10-23 17:39:40 +0200331int
332_PyObject_IsFreed(PyObject *op)
333{
Victor Stinner58ac7002020-02-07 03:04:21 +0100334 if (_PyMem_IsPtrFreed(op) || _PyMem_IsPtrFreed(Py_TYPE(op))) {
Victor Stinner2cf5d322018-11-22 16:32:57 +0100335 return 1;
336 }
Victor Stinner2b00db62019-04-11 11:33:27 +0200337 /* ignore op->ob_ref: its value can have be modified
Victor Stinner82af0b62018-10-23 17:39:40 +0200338 by Py_INCREF() and Py_DECREF(). */
339#ifdef Py_TRACE_REFS
Pablo Galindo36e33c32019-10-08 00:43:14 +0100340 if (op->_ob_next != NULL && _PyMem_IsPtrFreed(op->_ob_next)) {
Victor Stinner2b00db62019-04-11 11:33:27 +0200341 return 1;
342 }
Pablo Galindo36e33c32019-10-08 00:43:14 +0100343 if (op->_ob_prev != NULL && _PyMem_IsPtrFreed(op->_ob_prev)) {
344 return 1;
345 }
Victor Stinner82af0b62018-10-23 17:39:40 +0200346#endif
Victor Stinner2b00db62019-04-11 11:33:27 +0200347 return 0;
Victor Stinner82af0b62018-10-23 17:39:40 +0200348}
349
350
Barry Warsaw9bf16442001-01-23 16:24:35 +0000351/* For debugging convenience. See Misc/gdbinit for some useful gdb hooks */
Guido van Rossum38938152006-08-21 23:36:26 +0000352void
353_PyObject_Dump(PyObject* op)
Barry Warsaw9bf16442001-01-23 16:24:35 +0000354{
Victor Stinner82af0b62018-10-23 17:39:40 +0200355 if (_PyObject_IsFreed(op)) {
356 /* It seems like the object memory has been freed:
357 don't access it to prevent a segmentation fault. */
Victor Stinnerb39afb72019-09-17 23:36:28 +0200358 fprintf(stderr, "<object at %p is freed>\n", op);
Victor Stinner68762572019-10-07 18:42:01 +0200359 fflush(stderr);
Victor Stinner2cf5d322018-11-22 16:32:57 +0100360 return;
Victor Stinner82af0b62018-10-23 17:39:40 +0200361 }
362
Victor Stinner68762572019-10-07 18:42:01 +0200363 /* first, write fields which are the least likely to crash */
364 fprintf(stderr, "object address : %p\n", (void *)op);
Victor Stinner82af0b62018-10-23 17:39:40 +0200365 /* XXX(twouters) cast refcount to long until %zd is
366 universally available */
Victor Stinnera93c51e2020-02-07 00:38:59 +0100367 fprintf(stderr, "object refcount : %ld\n", (long)Py_REFCNT(op));
Victor Stinner68762572019-10-07 18:42:01 +0200368 fflush(stderr);
369
370 PyTypeObject *type = Py_TYPE(op);
371 fprintf(stderr, "object type : %p\n", type);
372 fprintf(stderr, "object type name: %s\n",
373 type==NULL ? "NULL" : type->tp_name);
374
375 /* the most dangerous part */
376 fprintf(stderr, "object repr : ");
377 fflush(stderr);
378
379 PyGILState_STATE gil = PyGILState_Ensure();
380 PyObject *error_type, *error_value, *error_traceback;
381 PyErr_Fetch(&error_type, &error_value, &error_traceback);
382
383 (void)PyObject_Print(op, stderr, 0);
384 fflush(stderr);
385
386 PyErr_Restore(error_type, error_value, error_traceback);
387 PyGILState_Release(gil);
388
389 fprintf(stderr, "\n");
Victor Stinner82af0b62018-10-23 17:39:40 +0200390 fflush(stderr);
Barry Warsaw9bf16442001-01-23 16:24:35 +0000391}
Barry Warsaw903138f2001-01-23 16:33:18 +0000392
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000393PyObject *
Fred Drake100814d2000-07-09 15:48:49 +0000394PyObject_Repr(PyObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000395{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000396 PyObject *res;
397 if (PyErr_CheckSignals())
398 return NULL;
Guido van Rossum9b00dfa1998-04-28 16:06:54 +0000399#ifdef USE_STACKCHECK
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000400 if (PyOS_CheckStack()) {
401 PyErr_SetString(PyExc_MemoryError, "stack overflow");
402 return NULL;
403 }
Guido van Rossum9b00dfa1998-04-28 16:06:54 +0000404#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000405 if (v == NULL)
406 return PyUnicode_FromString("<NULL>");
407 if (Py_TYPE(v)->tp_repr == NULL)
408 return PyUnicode_FromFormat("<%s object at %p>",
Victor Stinner58ac7002020-02-07 03:04:21 +0100409 Py_TYPE(v)->tp_name, v);
Victor Stinner33824f62013-08-26 14:05:19 +0200410
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100411 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinner33824f62013-08-26 14:05:19 +0200412#ifdef Py_DEBUG
413 /* PyObject_Repr() must not be called with an exception set,
Victor Stinnera8cb5152017-01-18 14:12:51 +0100414 because it can clear it (directly or indirectly) and so the
Martin Panter9955a372015-10-07 10:26:23 +0000415 caller loses its exception */
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100416 assert(!_PyErr_Occurred(tstate));
Victor Stinner33824f62013-08-26 14:05:19 +0200417#endif
418
Serhiy Storchaka1fb72d22017-12-03 22:12:11 +0200419 /* It is possible for a type to have a tp_repr representation that loops
420 infinitely. */
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100421 if (_Py_EnterRecursiveCall(tstate,
422 " while getting the repr of an object")) {
Serhiy Storchaka1fb72d22017-12-03 22:12:11 +0200423 return NULL;
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100424 }
Victor Stinner58ac7002020-02-07 03:04:21 +0100425 res = (*Py_TYPE(v)->tp_repr)(v);
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100426 _Py_LeaveRecursiveCall(tstate);
427
428 if (res == NULL) {
Victor Stinner0a54cf12011-12-01 03:22:44 +0100429 return NULL;
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100430 }
Victor Stinner0a54cf12011-12-01 03:22:44 +0100431 if (!PyUnicode_Check(res)) {
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100432 _PyErr_Format(tstate, PyExc_TypeError,
433 "__repr__ returned non-string (type %.200s)",
Victor Stinner58ac7002020-02-07 03:04:21 +0100434 Py_TYPE(res)->tp_name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000435 Py_DECREF(res);
436 return NULL;
437 }
Victor Stinnerdb88ae52011-12-01 02:15:00 +0100438#ifndef Py_DEBUG
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100439 if (PyUnicode_READY(res) < 0) {
Victor Stinnerdb88ae52011-12-01 02:15:00 +0100440 return NULL;
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100441 }
Victor Stinnerdb88ae52011-12-01 02:15:00 +0100442#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000443 return res;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000444}
445
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000446PyObject *
Guido van Rossum98297ee2007-11-06 21:34:58 +0000447PyObject_Str(PyObject *v)
Guido van Rossumc6004111993-11-05 10:22:19 +0000448{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000449 PyObject *res;
450 if (PyErr_CheckSignals())
451 return NULL;
Guido van Rossum98297ee2007-11-06 21:34:58 +0000452#ifdef USE_STACKCHECK
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000453 if (PyOS_CheckStack()) {
454 PyErr_SetString(PyExc_MemoryError, "stack overflow");
455 return NULL;
456 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000457#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000458 if (v == NULL)
459 return PyUnicode_FromString("<NULL>");
460 if (PyUnicode_CheckExact(v)) {
Victor Stinnerdb88ae52011-12-01 02:15:00 +0100461#ifndef Py_DEBUG
Victor Stinner4ead7c72011-11-20 19:48:36 +0100462 if (PyUnicode_READY(v) < 0)
463 return NULL;
Victor Stinnerdb88ae52011-12-01 02:15:00 +0100464#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000465 Py_INCREF(v);
466 return v;
467 }
468 if (Py_TYPE(v)->tp_str == NULL)
469 return PyObject_Repr(v);
Guido van Rossum4f288ab2001-05-01 16:53:37 +0000470
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100471 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinner33824f62013-08-26 14:05:19 +0200472#ifdef Py_DEBUG
473 /* PyObject_Str() must not be called with an exception set,
Victor Stinnera8cb5152017-01-18 14:12:51 +0100474 because it can clear it (directly or indirectly) and so the
Nick Coghland979e432014-02-09 10:43:21 +1000475 caller loses its exception */
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100476 assert(!_PyErr_Occurred(tstate));
Victor Stinner33824f62013-08-26 14:05:19 +0200477#endif
478
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000479 /* It is possible for a type to have a tp_str representation that loops
480 infinitely. */
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100481 if (_Py_EnterRecursiveCall(tstate, " while getting the str of an object")) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000482 return NULL;
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100483 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000484 res = (*Py_TYPE(v)->tp_str)(v);
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100485 _Py_LeaveRecursiveCall(tstate);
486
487 if (res == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000488 return NULL;
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100489 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000490 if (!PyUnicode_Check(res)) {
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100491 _PyErr_Format(tstate, PyExc_TypeError,
492 "__str__ returned non-string (type %.200s)",
493 Py_TYPE(res)->tp_name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000494 Py_DECREF(res);
495 return NULL;
496 }
Victor Stinnerdb88ae52011-12-01 02:15:00 +0100497#ifndef Py_DEBUG
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100498 if (PyUnicode_READY(res) < 0) {
Victor Stinner4ead7c72011-11-20 19:48:36 +0100499 return NULL;
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100500 }
Victor Stinnerdb88ae52011-12-01 02:15:00 +0100501#endif
Victor Stinner4ead7c72011-11-20 19:48:36 +0100502 assert(_PyUnicode_CheckConsistency(res, 1));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000503 return res;
Neil Schemenauercf52c072005-08-12 17:34:58 +0000504}
505
Georg Brandl559e5d72008-06-11 18:37:52 +0000506PyObject *
507PyObject_ASCII(PyObject *v)
508{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000509 PyObject *repr, *ascii, *res;
Georg Brandl559e5d72008-06-11 18:37:52 +0000510
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000511 repr = PyObject_Repr(v);
512 if (repr == NULL)
513 return NULL;
Georg Brandl559e5d72008-06-11 18:37:52 +0000514
Victor Stinneraf037572013-04-14 18:44:10 +0200515 if (PyUnicode_IS_ASCII(repr))
516 return repr;
517
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000518 /* repr is guaranteed to be a PyUnicode object by PyObject_Repr */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200519 ascii = _PyUnicode_AsASCIIString(repr, "backslashreplace");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000520 Py_DECREF(repr);
521 if (ascii == NULL)
522 return NULL;
Georg Brandl559e5d72008-06-11 18:37:52 +0000523
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000524 res = PyUnicode_DecodeASCII(
525 PyBytes_AS_STRING(ascii),
526 PyBytes_GET_SIZE(ascii),
527 NULL);
528
529 Py_DECREF(ascii);
530 return res;
Georg Brandl559e5d72008-06-11 18:37:52 +0000531}
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000532
Benjamin Petersonc15a0732008-08-26 16:46:47 +0000533PyObject *
534PyObject_Bytes(PyObject *v)
535{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000536 PyObject *result, *func;
Benjamin Petersonc15a0732008-08-26 16:46:47 +0000537
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000538 if (v == NULL)
539 return PyBytes_FromString("<NULL>");
Benjamin Petersonc15a0732008-08-26 16:46:47 +0000540
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000541 if (PyBytes_CheckExact(v)) {
542 Py_INCREF(v);
543 return v;
544 }
Benjamin Petersonc15a0732008-08-26 16:46:47 +0000545
Benjamin Petersonce798522012-01-22 11:24:29 -0500546 func = _PyObject_LookupSpecial(v, &PyId___bytes__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000547 if (func != NULL) {
Victor Stinnerf17c3de2016-12-06 18:46:19 +0100548 result = _PyObject_CallNoArg(func);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000549 Py_DECREF(func);
550 if (result == NULL)
Benjamin Peterson41ece392010-09-11 16:39:57 +0000551 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000552 if (!PyBytes_Check(result)) {
Benjamin Peterson41ece392010-09-11 16:39:57 +0000553 PyErr_Format(PyExc_TypeError,
554 "__bytes__ returned non-bytes (type %.200s)",
555 Py_TYPE(result)->tp_name);
556 Py_DECREF(result);
557 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000558 }
559 return result;
560 }
561 else if (PyErr_Occurred())
562 return NULL;
563 return PyBytes_FromObject(v);
Benjamin Petersonc15a0732008-08-26 16:46:47 +0000564}
565
Jeroen Demeyerbf17d412019-11-05 16:48:04 +0100566
567/*
568def _PyObject_FunctionStr(x):
569 try:
570 qualname = x.__qualname__
571 except AttributeError:
572 return str(x)
573 try:
574 mod = x.__module__
575 if mod is not None and mod != 'builtins':
576 return f"{x.__module__}.{qualname}()"
577 except AttributeError:
578 pass
579 return qualname
580*/
581PyObject *
582_PyObject_FunctionStr(PyObject *x)
583{
584 _Py_IDENTIFIER(__module__);
585 _Py_IDENTIFIER(__qualname__);
586 _Py_IDENTIFIER(builtins);
587 assert(!PyErr_Occurred());
588 PyObject *qualname;
589 int ret = _PyObject_LookupAttrId(x, &PyId___qualname__, &qualname);
590 if (qualname == NULL) {
591 if (ret < 0) {
592 return NULL;
593 }
594 return PyObject_Str(x);
595 }
596 PyObject *module;
597 PyObject *result = NULL;
598 ret = _PyObject_LookupAttrId(x, &PyId___module__, &module);
599 if (module != NULL && module != Py_None) {
600 PyObject *builtinsname = _PyUnicode_FromId(&PyId_builtins);
601 if (builtinsname == NULL) {
602 goto done;
603 }
604 ret = PyObject_RichCompareBool(module, builtinsname, Py_NE);
605 if (ret < 0) {
606 // error
607 goto done;
608 }
609 if (ret > 0) {
610 result = PyUnicode_FromFormat("%S.%S()", module, qualname);
611 goto done;
612 }
613 }
614 else if (ret < 0) {
615 goto done;
616 }
617 result = PyUnicode_FromFormat("%S()", qualname);
618done:
619 Py_DECREF(qualname);
620 Py_XDECREF(module);
621 return result;
622}
623
Mark Dickinsonc008a172009-02-01 13:59:22 +0000624/* For Python 3.0.1 and later, the old three-way comparison has been
625 completely removed in favour of rich comparisons. PyObject_Compare() and
626 PyObject_Cmp() are gone, and the builtin cmp function no longer exists.
Jeroen Demeyer530f5062019-05-31 04:13:39 +0200627 The old tp_compare slot has been renamed to tp_as_async, and should no
Mark Dickinsonc008a172009-02-01 13:59:22 +0000628 longer be used. Use tp_richcompare instead.
Guido van Rossum98297ee2007-11-06 21:34:58 +0000629
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000630 See (*) below for practical amendments.
631
Mark Dickinsonc008a172009-02-01 13:59:22 +0000632 tp_richcompare gets called with a first argument of the appropriate type
633 and a second object of an arbitrary type. We never do any kind of
634 coercion.
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000635
Mark Dickinsonc008a172009-02-01 13:59:22 +0000636 The tp_richcompare slot should return an object, as follows:
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000637
638 NULL if an exception occurred
639 NotImplemented if the requested comparison is not implemented
640 any other false value if the requested comparison is false
641 any other true value if the requested comparison is true
642
643 The PyObject_RichCompare[Bool]() wrappers raise TypeError when they get
644 NotImplemented.
645
646 (*) Practical amendments:
647
648 - If rich comparison returns NotImplemented, == and != are decided by
649 comparing the object pointer (i.e. falling back to the base object
650 implementation).
651
Guido van Rossuma4073002002-05-31 20:03:54 +0000652*/
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000653
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000654/* Map rich comparison operators to their swapped version, e.g. LT <--> GT */
Brett Cannona5ca2e72004-09-25 01:37:24 +0000655int _Py_SwappedOp[] = {Py_GT, Py_GE, Py_EQ, Py_NE, Py_LT, Py_LE};
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +0000656
Serhiy Storchaka2d06e842015-12-25 19:53:18 +0200657static const char * const opstrings[] = {"<", "<=", "==", "!=", ">", ">="};
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000658
659/* Perform a rich comparison, raising TypeError when the requested comparison
660 operator is not supported. */
Guido van Rossume797ec12001-01-17 15:24:28 +0000661static PyObject *
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100662do_richcompare(PyThreadState *tstate, PyObject *v, PyObject *w, int op)
Guido van Rossume797ec12001-01-17 15:24:28 +0000663{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000664 richcmpfunc f;
665 PyObject *res;
666 int checked_reverse_op = 0;
Guido van Rossume797ec12001-01-17 15:24:28 +0000667
Victor Stinner58ac7002020-02-07 03:04:21 +0100668 if (Py_TYPE(v) != Py_TYPE(w) &&
669 PyType_IsSubtype(Py_TYPE(w), Py_TYPE(v)) &&
670 (f = Py_TYPE(w)->tp_richcompare) != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000671 checked_reverse_op = 1;
672 res = (*f)(w, v, _Py_SwappedOp[op]);
673 if (res != Py_NotImplemented)
674 return res;
675 Py_DECREF(res);
676 }
Victor Stinner58ac7002020-02-07 03:04:21 +0100677 if ((f = Py_TYPE(v)->tp_richcompare) != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000678 res = (*f)(v, w, op);
679 if (res != Py_NotImplemented)
680 return res;
681 Py_DECREF(res);
682 }
Victor Stinner58ac7002020-02-07 03:04:21 +0100683 if (!checked_reverse_op && (f = Py_TYPE(w)->tp_richcompare) != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000684 res = (*f)(w, v, _Py_SwappedOp[op]);
685 if (res != Py_NotImplemented)
686 return res;
687 Py_DECREF(res);
688 }
689 /* If neither object implements it, provide a sensible default
690 for == and !=, but raise an exception for ordering. */
691 switch (op) {
692 case Py_EQ:
693 res = (v == w) ? Py_True : Py_False;
694 break;
695 case Py_NE:
696 res = (v != w) ? Py_True : Py_False;
697 break;
698 default:
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100699 _PyErr_Format(tstate, PyExc_TypeError,
700 "'%s' not supported between instances of '%.100s' and '%.100s'",
701 opstrings[op],
Victor Stinner58ac7002020-02-07 03:04:21 +0100702 Py_TYPE(v)->tp_name,
703 Py_TYPE(w)->tp_name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000704 return NULL;
705 }
706 Py_INCREF(res);
707 return res;
Guido van Rossume797ec12001-01-17 15:24:28 +0000708}
709
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000710/* Perform a rich comparison with object result. This wraps do_richcompare()
711 with a check for NULL arguments and a recursion check. */
Guido van Rossume797ec12001-01-17 15:24:28 +0000712
Guido van Rossume797ec12001-01-17 15:24:28 +0000713PyObject *
714PyObject_RichCompare(PyObject *v, PyObject *w, int op)
715{
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100716 PyThreadState *tstate = _PyThreadState_GET();
Guido van Rossume797ec12001-01-17 15:24:28 +0000717
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000718 assert(Py_LT <= op && op <= Py_GE);
719 if (v == NULL || w == NULL) {
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100720 if (!_PyErr_Occurred(tstate)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000721 PyErr_BadInternalCall();
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100722 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000723 return NULL;
724 }
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100725 if (_Py_EnterRecursiveCall(tstate, " in comparison")) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000726 return NULL;
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100727 }
728 PyObject *res = do_richcompare(tstate, v, w, op);
729 _Py_LeaveRecursiveCall(tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000730 return res;
Guido van Rossume797ec12001-01-17 15:24:28 +0000731}
732
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000733/* Perform a rich comparison with integer result. This wraps
734 PyObject_RichCompare(), returning -1 for error, 0 for false, 1 for true. */
Guido van Rossume797ec12001-01-17 15:24:28 +0000735int
736PyObject_RichCompareBool(PyObject *v, PyObject *w, int op)
737{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000738 PyObject *res;
739 int ok;
Guido van Rossume797ec12001-01-17 15:24:28 +0000740
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000741 /* Quick result when objects are the same.
742 Guarantees that identity implies equality. */
743 if (v == w) {
744 if (op == Py_EQ)
745 return 1;
746 else if (op == Py_NE)
747 return 0;
748 }
Mark Dickinson4a1f5932008-11-12 23:23:36 +0000749
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000750 res = PyObject_RichCompare(v, w, op);
751 if (res == NULL)
752 return -1;
753 if (PyBool_Check(res))
754 ok = (res == Py_True);
755 else
756 ok = PyObject_IsTrue(res);
757 Py_DECREF(res);
758 return ok;
Guido van Rossume797ec12001-01-17 15:24:28 +0000759}
Fred Drake13634cf2000-06-29 19:17:04 +0000760
Antoine Pitrouce4a9da2011-11-21 20:46:33 +0100761Py_hash_t
Nick Coghland1abd252008-07-15 15:46:38 +0000762PyObject_HashNotImplemented(PyObject *v)
763{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000764 PyErr_Format(PyExc_TypeError, "unhashable type: '%.200s'",
765 Py_TYPE(v)->tp_name);
766 return -1;
Nick Coghland1abd252008-07-15 15:46:38 +0000767}
Fred Drake13634cf2000-06-29 19:17:04 +0000768
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000769Py_hash_t
Fred Drake100814d2000-07-09 15:48:49 +0000770PyObject_Hash(PyObject *v)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000771{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000772 PyTypeObject *tp = Py_TYPE(v);
773 if (tp->tp_hash != NULL)
774 return (*tp->tp_hash)(v);
775 /* To keep to the general practice that inheriting
776 * solely from object in C code should work without
777 * an explicit call to PyType_Ready, we implicitly call
778 * PyType_Ready here and then check the tp_hash slot again
779 */
780 if (tp->tp_dict == NULL) {
781 if (PyType_Ready(tp) < 0)
782 return -1;
783 if (tp->tp_hash != NULL)
784 return (*tp->tp_hash)(v);
785 }
786 /* Otherwise, the object can't be hashed */
787 return PyObject_HashNotImplemented(v);
Guido van Rossum9bfef441993-03-29 10:43:31 +0000788}
789
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000790PyObject *
Jeremy Hyltonaf68c872005-12-10 18:50:16 +0000791PyObject_GetAttrString(PyObject *v, const char *name)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000792{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000793 PyObject *w, *res;
Guido van Rossumd8eb1b31996-08-09 20:52:03 +0000794
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000795 if (Py_TYPE(v)->tp_getattr != NULL)
796 return (*Py_TYPE(v)->tp_getattr)(v, (char*)name);
INADA Naoki3e8d6cb2017-02-21 23:57:25 +0900797 w = PyUnicode_FromString(name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000798 if (w == NULL)
799 return NULL;
800 res = PyObject_GetAttr(v, w);
Victor Stinner59af08f2012-03-22 02:09:08 +0100801 Py_DECREF(w);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000802 return res;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000803}
804
805int
Jeremy Hyltonaf68c872005-12-10 18:50:16 +0000806PyObject_HasAttrString(PyObject *v, const char *name)
Guido van Rossumed18fdc1993-07-11 19:55:34 +0000807{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000808 PyObject *res = PyObject_GetAttrString(v, name);
809 if (res != NULL) {
810 Py_DECREF(res);
811 return 1;
812 }
813 PyErr_Clear();
814 return 0;
Guido van Rossumed18fdc1993-07-11 19:55:34 +0000815}
816
817int
Jeremy Hyltonaf68c872005-12-10 18:50:16 +0000818PyObject_SetAttrString(PyObject *v, const char *name, PyObject *w)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000819{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000820 PyObject *s;
821 int res;
Guido van Rossumd8eb1b31996-08-09 20:52:03 +0000822
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000823 if (Py_TYPE(v)->tp_setattr != NULL)
824 return (*Py_TYPE(v)->tp_setattr)(v, (char*)name, w);
825 s = PyUnicode_InternFromString(name);
826 if (s == NULL)
827 return -1;
828 res = PyObject_SetAttr(v, s, w);
829 Py_XDECREF(s);
830 return res;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000831}
832
Benjamin Petersonbfebb7b2011-12-15 15:34:02 -0500833int
834_PyObject_IsAbstract(PyObject *obj)
835{
836 int res;
837 PyObject* isabstract;
Benjamin Petersonbfebb7b2011-12-15 15:34:02 -0500838
839 if (obj == NULL)
840 return 0;
841
Serhiy Storchakaf320be72018-01-25 10:49:40 +0200842 res = _PyObject_LookupAttrId(obj, &PyId___isabstractmethod__, &isabstract);
843 if (res > 0) {
844 res = PyObject_IsTrue(isabstract);
845 Py_DECREF(isabstract);
Benjamin Petersonbfebb7b2011-12-15 15:34:02 -0500846 }
Benjamin Petersonbfebb7b2011-12-15 15:34:02 -0500847 return res;
848}
849
Guido van Rossum98ff96a1997-05-20 18:34:44 +0000850PyObject *
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200851_PyObject_GetAttrId(PyObject *v, _Py_Identifier *name)
852{
853 PyObject *result;
Martin v. Löwisd10759f2011-11-07 13:00:05 +0100854 PyObject *oname = _PyUnicode_FromId(name); /* borrowed */
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200855 if (!oname)
856 return NULL;
857 result = PyObject_GetAttr(v, oname);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200858 return result;
859}
860
861int
862_PyObject_HasAttrId(PyObject *v, _Py_Identifier *name)
863{
864 int result;
Martin v. Löwisd10759f2011-11-07 13:00:05 +0100865 PyObject *oname = _PyUnicode_FromId(name); /* borrowed */
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200866 if (!oname)
867 return -1;
868 result = PyObject_HasAttr(v, oname);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200869 return result;
870}
871
872int
873_PyObject_SetAttrId(PyObject *v, _Py_Identifier *name, PyObject *w)
874{
875 int result;
Martin v. Löwisd10759f2011-11-07 13:00:05 +0100876 PyObject *oname = _PyUnicode_FromId(name); /* borrowed */
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200877 if (!oname)
878 return -1;
879 result = PyObject_SetAttr(v, oname, w);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200880 return result;
881}
882
883PyObject *
Fred Drake100814d2000-07-09 15:48:49 +0000884PyObject_GetAttr(PyObject *v, PyObject *name)
Guido van Rossum98ff96a1997-05-20 18:34:44 +0000885{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000886 PyTypeObject *tp = Py_TYPE(v);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000887
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000888 if (!PyUnicode_Check(name)) {
889 PyErr_Format(PyExc_TypeError,
890 "attribute name must be string, not '%.200s'",
Victor Stinner58ac7002020-02-07 03:04:21 +0100891 Py_TYPE(name)->tp_name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000892 return NULL;
893 }
894 if (tp->tp_getattro != NULL)
895 return (*tp->tp_getattro)(v, name);
896 if (tp->tp_getattr != NULL) {
Serhiy Storchaka2a404b62017-01-22 23:07:07 +0200897 const char *name_str = PyUnicode_AsUTF8(name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000898 if (name_str == NULL)
899 return NULL;
Serhiy Storchaka2a404b62017-01-22 23:07:07 +0200900 return (*tp->tp_getattr)(v, (char *)name_str);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000901 }
902 PyErr_Format(PyExc_AttributeError,
903 "'%.50s' object has no attribute '%U'",
904 tp->tp_name, name);
905 return NULL;
Guido van Rossum98ff96a1997-05-20 18:34:44 +0000906}
907
Serhiy Storchakaf320be72018-01-25 10:49:40 +0200908int
909_PyObject_LookupAttr(PyObject *v, PyObject *name, PyObject **result)
INADA Naoki378edee2018-01-16 20:52:41 +0900910{
911 PyTypeObject *tp = Py_TYPE(v);
INADA Naoki378edee2018-01-16 20:52:41 +0900912
913 if (!PyUnicode_Check(name)) {
914 PyErr_Format(PyExc_TypeError,
915 "attribute name must be string, not '%.200s'",
Victor Stinner58ac7002020-02-07 03:04:21 +0100916 Py_TYPE(name)->tp_name);
Serhiy Storchakaf320be72018-01-25 10:49:40 +0200917 *result = NULL;
918 return -1;
INADA Naoki378edee2018-01-16 20:52:41 +0900919 }
920
921 if (tp->tp_getattro == PyObject_GenericGetAttr) {
Serhiy Storchakaf320be72018-01-25 10:49:40 +0200922 *result = _PyObject_GenericGetAttrWithDict(v, name, NULL, 1);
923 if (*result != NULL) {
924 return 1;
925 }
926 if (PyErr_Occurred()) {
927 return -1;
928 }
929 return 0;
INADA Naoki378edee2018-01-16 20:52:41 +0900930 }
931 if (tp->tp_getattro != NULL) {
Serhiy Storchakaf320be72018-01-25 10:49:40 +0200932 *result = (*tp->tp_getattro)(v, name);
INADA Naoki378edee2018-01-16 20:52:41 +0900933 }
934 else if (tp->tp_getattr != NULL) {
935 const char *name_str = PyUnicode_AsUTF8(name);
Serhiy Storchakaf320be72018-01-25 10:49:40 +0200936 if (name_str == NULL) {
937 *result = NULL;
938 return -1;
939 }
940 *result = (*tp->tp_getattr)(v, (char *)name_str);
INADA Naoki378edee2018-01-16 20:52:41 +0900941 }
INADA Naokie76daeb2018-01-26 16:22:51 +0900942 else {
943 *result = NULL;
944 return 0;
945 }
946
Serhiy Storchakaf320be72018-01-25 10:49:40 +0200947 if (*result != NULL) {
948 return 1;
INADA Naoki378edee2018-01-16 20:52:41 +0900949 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +0200950 if (!PyErr_ExceptionMatches(PyExc_AttributeError)) {
951 return -1;
952 }
953 PyErr_Clear();
954 return 0;
955}
956
957int
958_PyObject_LookupAttrId(PyObject *v, _Py_Identifier *name, PyObject **result)
959{
960 PyObject *oname = _PyUnicode_FromId(name); /* borrowed */
961 if (!oname) {
962 *result = NULL;
963 return -1;
964 }
965 return _PyObject_LookupAttr(v, oname, result);
INADA Naoki378edee2018-01-16 20:52:41 +0900966}
967
Guido van Rossum98ff96a1997-05-20 18:34:44 +0000968int
Fred Drake100814d2000-07-09 15:48:49 +0000969PyObject_HasAttr(PyObject *v, PyObject *name)
Guido van Rossum98ff96a1997-05-20 18:34:44 +0000970{
Serhiy Storchakaf320be72018-01-25 10:49:40 +0200971 PyObject *res;
972 if (_PyObject_LookupAttr(v, name, &res) < 0) {
973 PyErr_Clear();
974 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000975 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +0200976 if (res == NULL) {
977 return 0;
978 }
979 Py_DECREF(res);
980 return 1;
Guido van Rossum98ff96a1997-05-20 18:34:44 +0000981}
982
983int
Fred Drake100814d2000-07-09 15:48:49 +0000984PyObject_SetAttr(PyObject *v, PyObject *name, PyObject *value)
Guido van Rossum98ff96a1997-05-20 18:34:44 +0000985{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000986 PyTypeObject *tp = Py_TYPE(v);
987 int err;
Marc-André Lemburge44e5072000-09-18 16:20:57 +0000988
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000989 if (!PyUnicode_Check(name)) {
990 PyErr_Format(PyExc_TypeError,
991 "attribute name must be string, not '%.200s'",
Victor Stinner58ac7002020-02-07 03:04:21 +0100992 Py_TYPE(name)->tp_name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000993 return -1;
994 }
995 Py_INCREF(name);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000996
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000997 PyUnicode_InternInPlace(&name);
998 if (tp->tp_setattro != NULL) {
999 err = (*tp->tp_setattro)(v, name, value);
1000 Py_DECREF(name);
1001 return err;
1002 }
1003 if (tp->tp_setattr != NULL) {
Serhiy Storchaka2a404b62017-01-22 23:07:07 +02001004 const char *name_str = PyUnicode_AsUTF8(name);
Zackery Spytze0dcb852019-04-28 06:58:52 -06001005 if (name_str == NULL) {
1006 Py_DECREF(name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001007 return -1;
Zackery Spytze0dcb852019-04-28 06:58:52 -06001008 }
Serhiy Storchaka2a404b62017-01-22 23:07:07 +02001009 err = (*tp->tp_setattr)(v, (char *)name_str, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001010 Py_DECREF(name);
1011 return err;
1012 }
1013 Py_DECREF(name);
Victor Stinnera93c51e2020-02-07 00:38:59 +01001014 _PyObject_ASSERT(name, Py_REFCNT(name) >= 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001015 if (tp->tp_getattr == NULL && tp->tp_getattro == NULL)
1016 PyErr_Format(PyExc_TypeError,
1017 "'%.100s' object has no attributes "
1018 "(%s .%U)",
1019 tp->tp_name,
1020 value==NULL ? "del" : "assign to",
1021 name);
1022 else
1023 PyErr_Format(PyExc_TypeError,
1024 "'%.100s' object has only read-only attributes "
1025 "(%s .%U)",
1026 tp->tp_name,
1027 value==NULL ? "del" : "assign to",
1028 name);
1029 return -1;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001030}
1031
1032/* Helper to get a pointer to an object's __dict__ slot, if any */
1033
1034PyObject **
1035_PyObject_GetDictPtr(PyObject *obj)
1036{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001037 Py_ssize_t dictoffset;
1038 PyTypeObject *tp = Py_TYPE(obj);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001039
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001040 dictoffset = tp->tp_dictoffset;
1041 if (dictoffset == 0)
1042 return NULL;
1043 if (dictoffset < 0) {
Victor Stinnerc65b3202020-02-07 11:18:33 +01001044 Py_ssize_t tsize = Py_SIZE(obj);
1045 if (tsize < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001046 tsize = -tsize;
Victor Stinnerc65b3202020-02-07 11:18:33 +01001047 }
1048 size_t size = _PyObject_VAR_SIZE(tp, tsize);
Guido van Rossum2eb0b872002-03-01 22:24:49 +00001049
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001050 dictoffset += (long)size;
Victor Stinner24702042018-10-26 17:16:37 +02001051 _PyObject_ASSERT(obj, dictoffset > 0);
1052 _PyObject_ASSERT(obj, dictoffset % SIZEOF_VOID_P == 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001053 }
1054 return (PyObject **) ((char *)obj + dictoffset);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001055}
1056
Tim Peters6d6c1a32001-08-02 04:15:00 +00001057PyObject *
Raymond Hettinger1da1dbf2003-03-17 19:46:11 +00001058PyObject_SelfIter(PyObject *obj)
Raymond Hettinger01538262003-03-17 08:24:35 +00001059{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001060 Py_INCREF(obj);
1061 return obj;
Raymond Hettinger01538262003-03-17 08:24:35 +00001062}
1063
Amaury Forgeot d'Arcf343e012009-01-12 23:58:21 +00001064/* Helper used when the __next__ method is removed from a type:
1065 tp_iternext is never NULL and can be safely called without checking
1066 on every iteration.
1067 */
1068
1069PyObject *
1070_PyObject_NextNotImplemented(PyObject *self)
1071{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001072 PyErr_Format(PyExc_TypeError,
1073 "'%.200s' object is not iterable",
1074 Py_TYPE(self)->tp_name);
1075 return NULL;
Amaury Forgeot d'Arcf343e012009-01-12 23:58:21 +00001076}
1077
Yury Selivanovf2392132016-12-13 19:03:51 -05001078
1079/* Specialized version of _PyObject_GenericGetAttrWithDict
1080 specifically for the LOAD_METHOD opcode.
1081
1082 Return 1 if a method is found, 0 if it's a regular attribute
1083 from __dict__ or something returned by using a descriptor
1084 protocol.
1085
1086 `method` will point to the resolved attribute or NULL. In the
1087 latter case, an error will be set.
1088*/
1089int
1090_PyObject_GetMethod(PyObject *obj, PyObject *name, PyObject **method)
1091{
1092 PyTypeObject *tp = Py_TYPE(obj);
1093 PyObject *descr;
1094 descrgetfunc f = NULL;
1095 PyObject **dictptr, *dict;
1096 PyObject *attr;
1097 int meth_found = 0;
1098
1099 assert(*method == NULL);
1100
1101 if (Py_TYPE(obj)->tp_getattro != PyObject_GenericGetAttr
1102 || !PyUnicode_Check(name)) {
1103 *method = PyObject_GetAttr(obj, name);
1104 return 0;
1105 }
1106
1107 if (tp->tp_dict == NULL && PyType_Ready(tp) < 0)
1108 return 0;
1109
1110 descr = _PyType_Lookup(tp, name);
1111 if (descr != NULL) {
1112 Py_INCREF(descr);
Jeroen Demeyereb65e242019-05-28 14:42:53 +02001113 if (PyType_HasFeature(Py_TYPE(descr), Py_TPFLAGS_METHOD_DESCRIPTOR)) {
Yury Selivanovf2392132016-12-13 19:03:51 -05001114 meth_found = 1;
1115 } else {
Victor Stinner58ac7002020-02-07 03:04:21 +01001116 f = Py_TYPE(descr)->tp_descr_get;
Yury Selivanovf2392132016-12-13 19:03:51 -05001117 if (f != NULL && PyDescr_IsData(descr)) {
Victor Stinner58ac7002020-02-07 03:04:21 +01001118 *method = f(descr, obj, (PyObject *)Py_TYPE(obj));
Yury Selivanovf2392132016-12-13 19:03:51 -05001119 Py_DECREF(descr);
1120 return 0;
1121 }
1122 }
1123 }
1124
1125 dictptr = _PyObject_GetDictPtr(obj);
1126 if (dictptr != NULL && (dict = *dictptr) != NULL) {
1127 Py_INCREF(dict);
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02001128 attr = PyDict_GetItemWithError(dict, name);
Yury Selivanovf2392132016-12-13 19:03:51 -05001129 if (attr != NULL) {
1130 Py_INCREF(attr);
1131 *method = attr;
1132 Py_DECREF(dict);
1133 Py_XDECREF(descr);
1134 return 0;
1135 }
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02001136 else {
1137 Py_DECREF(dict);
1138 if (PyErr_Occurred()) {
1139 Py_XDECREF(descr);
1140 return 0;
1141 }
1142 }
Yury Selivanovf2392132016-12-13 19:03:51 -05001143 }
1144
1145 if (meth_found) {
1146 *method = descr;
1147 return 1;
1148 }
1149
1150 if (f != NULL) {
1151 *method = f(descr, obj, (PyObject *)Py_TYPE(obj));
1152 Py_DECREF(descr);
1153 return 0;
1154 }
1155
1156 if (descr != NULL) {
1157 *method = descr;
1158 return 0;
1159 }
1160
1161 PyErr_Format(PyExc_AttributeError,
1162 "'%.50s' object has no attribute '%U'",
1163 tp->tp_name, name);
1164 return 0;
1165}
1166
1167/* Generic GetAttr functions - put these in your tp_[gs]etattro slot. */
Michael W. Hudson1593f502004-09-14 17:09:47 +00001168
Raymond Hettinger01538262003-03-17 08:24:35 +00001169PyObject *
INADA Naoki378edee2018-01-16 20:52:41 +09001170_PyObject_GenericGetAttrWithDict(PyObject *obj, PyObject *name,
1171 PyObject *dict, int suppress)
Tim Peters6d6c1a32001-08-02 04:15:00 +00001172{
Yury Selivanovf2392132016-12-13 19:03:51 -05001173 /* Make sure the logic of _PyObject_GetMethod is in sync with
1174 this method.
INADA Naoki378edee2018-01-16 20:52:41 +09001175
1176 When suppress=1, this function suppress AttributeError.
Yury Selivanovf2392132016-12-13 19:03:51 -05001177 */
1178
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001179 PyTypeObject *tp = Py_TYPE(obj);
1180 PyObject *descr = NULL;
1181 PyObject *res = NULL;
1182 descrgetfunc f;
1183 Py_ssize_t dictoffset;
1184 PyObject **dictptr;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001185
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001186 if (!PyUnicode_Check(name)){
1187 PyErr_Format(PyExc_TypeError,
1188 "attribute name must be string, not '%.200s'",
Victor Stinner58ac7002020-02-07 03:04:21 +01001189 Py_TYPE(name)->tp_name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001190 return NULL;
1191 }
Serhiy Storchaka55c861f2016-04-17 20:31:51 +03001192 Py_INCREF(name);
Guido van Rossumebca9fc2001-12-04 15:54:53 +00001193
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001194 if (tp->tp_dict == NULL) {
1195 if (PyType_Ready(tp) < 0)
1196 goto done;
1197 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001198
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001199 descr = _PyType_Lookup(tp, name);
Michael W. Hudsonb2c7de42003-08-15 13:07:47 +00001200
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001201 f = NULL;
1202 if (descr != NULL) {
Serhiy Storchaka55c861f2016-04-17 20:31:51 +03001203 Py_INCREF(descr);
Victor Stinner58ac7002020-02-07 03:04:21 +01001204 f = Py_TYPE(descr)->tp_descr_get;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001205 if (f != NULL && PyDescr_IsData(descr)) {
Victor Stinner58ac7002020-02-07 03:04:21 +01001206 res = f(descr, obj, (PyObject *)Py_TYPE(obj));
INADA Naoki378edee2018-01-16 20:52:41 +09001207 if (res == NULL && suppress &&
1208 PyErr_ExceptionMatches(PyExc_AttributeError)) {
1209 PyErr_Clear();
1210 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001211 goto done;
1212 }
1213 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001214
Antoine Pitrou1a9a9d52010-08-28 18:17:03 +00001215 if (dict == NULL) {
1216 /* Inline _PyObject_GetDictPtr */
1217 dictoffset = tp->tp_dictoffset;
1218 if (dictoffset != 0) {
1219 if (dictoffset < 0) {
Victor Stinnerc65b3202020-02-07 11:18:33 +01001220 Py_ssize_t tsize = Py_SIZE(obj);
1221 if (tsize < 0) {
Antoine Pitrou1a9a9d52010-08-28 18:17:03 +00001222 tsize = -tsize;
Victor Stinnerc65b3202020-02-07 11:18:33 +01001223 }
1224 size_t size = _PyObject_VAR_SIZE(tp, tsize);
Victor Stinner24702042018-10-26 17:16:37 +02001225 _PyObject_ASSERT(obj, size <= PY_SSIZE_T_MAX);
Guido van Rossumc66ff442002-08-19 16:50:48 +00001226
Serhiy Storchaka55c861f2016-04-17 20:31:51 +03001227 dictoffset += (Py_ssize_t)size;
Victor Stinner24702042018-10-26 17:16:37 +02001228 _PyObject_ASSERT(obj, dictoffset > 0);
1229 _PyObject_ASSERT(obj, dictoffset % SIZEOF_VOID_P == 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001230 }
Antoine Pitrou1a9a9d52010-08-28 18:17:03 +00001231 dictptr = (PyObject **) ((char *)obj + dictoffset);
1232 dict = *dictptr;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001233 }
1234 }
Antoine Pitrou1a9a9d52010-08-28 18:17:03 +00001235 if (dict != NULL) {
1236 Py_INCREF(dict);
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02001237 res = PyDict_GetItemWithError(dict, name);
Antoine Pitrou1a9a9d52010-08-28 18:17:03 +00001238 if (res != NULL) {
1239 Py_INCREF(res);
Antoine Pitrou1a9a9d52010-08-28 18:17:03 +00001240 Py_DECREF(dict);
1241 goto done;
1242 }
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02001243 else {
1244 Py_DECREF(dict);
1245 if (PyErr_Occurred()) {
1246 if (suppress && PyErr_ExceptionMatches(PyExc_AttributeError)) {
1247 PyErr_Clear();
1248 }
1249 else {
1250 goto done;
1251 }
1252 }
1253 }
Antoine Pitrou1a9a9d52010-08-28 18:17:03 +00001254 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001255
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001256 if (f != NULL) {
1257 res = f(descr, obj, (PyObject *)Py_TYPE(obj));
INADA Naoki378edee2018-01-16 20:52:41 +09001258 if (res == NULL && suppress &&
1259 PyErr_ExceptionMatches(PyExc_AttributeError)) {
1260 PyErr_Clear();
1261 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001262 goto done;
1263 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001264
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001265 if (descr != NULL) {
1266 res = descr;
Victor Stinner2d01dc02012-03-09 00:44:13 +01001267 descr = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001268 goto done;
1269 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001270
INADA Naoki378edee2018-01-16 20:52:41 +09001271 if (!suppress) {
1272 PyErr_Format(PyExc_AttributeError,
1273 "'%.50s' object has no attribute '%U'",
1274 tp->tp_name, name);
1275 }
Guido van Rossumebca9fc2001-12-04 15:54:53 +00001276 done:
Victor Stinner2d01dc02012-03-09 00:44:13 +01001277 Py_XDECREF(descr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001278 Py_DECREF(name);
1279 return res;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001280}
1281
Antoine Pitrou1a9a9d52010-08-28 18:17:03 +00001282PyObject *
1283PyObject_GenericGetAttr(PyObject *obj, PyObject *name)
1284{
INADA Naoki378edee2018-01-16 20:52:41 +09001285 return _PyObject_GenericGetAttrWithDict(obj, name, NULL, 0);
Antoine Pitrou1a9a9d52010-08-28 18:17:03 +00001286}
1287
Tim Peters6d6c1a32001-08-02 04:15:00 +00001288int
Antoine Pitrou1a9a9d52010-08-28 18:17:03 +00001289_PyObject_GenericSetAttrWithDict(PyObject *obj, PyObject *name,
1290 PyObject *value, PyObject *dict)
Tim Peters6d6c1a32001-08-02 04:15:00 +00001291{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001292 PyTypeObject *tp = Py_TYPE(obj);
1293 PyObject *descr;
1294 descrsetfunc f;
1295 PyObject **dictptr;
1296 int res = -1;
Guido van Rossumebca9fc2001-12-04 15:54:53 +00001297
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001298 if (!PyUnicode_Check(name)){
1299 PyErr_Format(PyExc_TypeError,
1300 "attribute name must be string, not '%.200s'",
Victor Stinner58ac7002020-02-07 03:04:21 +01001301 Py_TYPE(name)->tp_name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001302 return -1;
1303 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001304
Benjamin Peterson74529ad2012-03-09 07:25:32 -08001305 if (tp->tp_dict == NULL && PyType_Ready(tp) < 0)
1306 return -1;
1307
1308 Py_INCREF(name);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001309
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001310 descr = _PyType_Lookup(tp, name);
Victor Stinner2d01dc02012-03-09 00:44:13 +01001311
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001312 if (descr != NULL) {
Serhiy Storchaka55c861f2016-04-17 20:31:51 +03001313 Py_INCREF(descr);
Victor Stinner58ac7002020-02-07 03:04:21 +01001314 f = Py_TYPE(descr)->tp_descr_set;
Serhiy Storchaka55c861f2016-04-17 20:31:51 +03001315 if (f != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001316 res = f(descr, obj, value);
1317 goto done;
1318 }
1319 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001320
Steve Dowerb82e17e2019-05-23 08:45:22 -07001321 /* XXX [Steve Dower] These are really noisy - worth it? */
1322 /*if (PyType_Check(obj) || PyModule_Check(obj)) {
1323 if (value && PySys_Audit("object.__setattr__", "OOO", obj, name, value) < 0)
1324 return -1;
1325 if (!value && PySys_Audit("object.__delattr__", "OO", obj, name) < 0)
1326 return -1;
1327 }*/
1328
Antoine Pitrou1a9a9d52010-08-28 18:17:03 +00001329 if (dict == NULL) {
1330 dictptr = _PyObject_GetDictPtr(obj);
Serhiy Storchaka55c861f2016-04-17 20:31:51 +03001331 if (dictptr == NULL) {
1332 if (descr == NULL) {
1333 PyErr_Format(PyExc_AttributeError,
1334 "'%.100s' object has no attribute '%U'",
1335 tp->tp_name, name);
1336 }
1337 else {
1338 PyErr_Format(PyExc_AttributeError,
1339 "'%.50s' object attribute '%U' is read-only",
1340 tp->tp_name, name);
1341 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001342 goto done;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001343 }
Serhiy Storchaka55c861f2016-04-17 20:31:51 +03001344 res = _PyObjectDict_SetItem(tp, dictptr, name, value);
Antoine Pitrou1a9a9d52010-08-28 18:17:03 +00001345 }
Serhiy Storchaka55c861f2016-04-17 20:31:51 +03001346 else {
Antoine Pitrou1a9a9d52010-08-28 18:17:03 +00001347 Py_INCREF(dict);
1348 if (value == NULL)
1349 res = PyDict_DelItem(dict, name);
1350 else
1351 res = PyDict_SetItem(dict, name, value);
Benjamin Peterson74529ad2012-03-09 07:25:32 -08001352 Py_DECREF(dict);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001353 }
Serhiy Storchaka55c861f2016-04-17 20:31:51 +03001354 if (res < 0 && PyErr_ExceptionMatches(PyExc_KeyError))
1355 PyErr_SetObject(PyExc_AttributeError, name);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001356
Guido van Rossumebca9fc2001-12-04 15:54:53 +00001357 done:
Victor Stinner2d01dc02012-03-09 00:44:13 +01001358 Py_XDECREF(descr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001359 Py_DECREF(name);
1360 return res;
Guido van Rossum98ff96a1997-05-20 18:34:44 +00001361}
1362
Antoine Pitrou1a9a9d52010-08-28 18:17:03 +00001363int
1364PyObject_GenericSetAttr(PyObject *obj, PyObject *name, PyObject *value)
1365{
1366 return _PyObject_GenericSetAttrWithDict(obj, name, value, NULL);
1367}
1368
Benjamin Peterson8eb12692012-02-19 19:59:10 -05001369int
1370PyObject_GenericSetDict(PyObject *obj, PyObject *value, void *context)
1371{
Serhiy Storchaka576f1322016-01-05 21:27:54 +02001372 PyObject **dictptr = _PyObject_GetDictPtr(obj);
Benjamin Peterson8eb12692012-02-19 19:59:10 -05001373 if (dictptr == NULL) {
1374 PyErr_SetString(PyExc_AttributeError,
1375 "This object has no __dict__");
1376 return -1;
1377 }
1378 if (value == NULL) {
1379 PyErr_SetString(PyExc_TypeError, "cannot delete __dict__");
1380 return -1;
1381 }
1382 if (!PyDict_Check(value)) {
1383 PyErr_Format(PyExc_TypeError,
1384 "__dict__ must be set to a dictionary, "
1385 "not a '%.200s'", Py_TYPE(value)->tp_name);
1386 return -1;
1387 }
Serhiy Storchaka576f1322016-01-05 21:27:54 +02001388 Py_INCREF(value);
Serhiy Storchakaec397562016-04-06 09:50:03 +03001389 Py_XSETREF(*dictptr, value);
Benjamin Peterson8eb12692012-02-19 19:59:10 -05001390 return 0;
1391}
1392
Antoine Pitrou1a9a9d52010-08-28 18:17:03 +00001393
Guido van Rossum6ac258d1993-05-12 08:24:20 +00001394/* Test a value used as condition, e.g., in a for or if statement.
1395 Return -1 if an error occurred */
1396
1397int
Fred Drake100814d2000-07-09 15:48:49 +00001398PyObject_IsTrue(PyObject *v)
Guido van Rossum6ac258d1993-05-12 08:24:20 +00001399{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001400 Py_ssize_t res;
1401 if (v == Py_True)
1402 return 1;
1403 if (v == Py_False)
1404 return 0;
1405 if (v == Py_None)
1406 return 0;
Victor Stinner58ac7002020-02-07 03:04:21 +01001407 else if (Py_TYPE(v)->tp_as_number != NULL &&
1408 Py_TYPE(v)->tp_as_number->nb_bool != NULL)
1409 res = (*Py_TYPE(v)->tp_as_number->nb_bool)(v);
1410 else if (Py_TYPE(v)->tp_as_mapping != NULL &&
1411 Py_TYPE(v)->tp_as_mapping->mp_length != NULL)
1412 res = (*Py_TYPE(v)->tp_as_mapping->mp_length)(v);
1413 else if (Py_TYPE(v)->tp_as_sequence != NULL &&
1414 Py_TYPE(v)->tp_as_sequence->sq_length != NULL)
1415 res = (*Py_TYPE(v)->tp_as_sequence->sq_length)(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001416 else
1417 return 1;
1418 /* if it is negative, it should be either -1 or -2 */
1419 return (res > 0) ? 1 : Py_SAFE_DOWNCAST(res, Py_ssize_t, int);
Guido van Rossum6ac258d1993-05-12 08:24:20 +00001420}
1421
Tim Peters803526b2002-07-07 05:13:56 +00001422/* equivalent of 'not v'
Guido van Rossumc3d3f961998-04-09 17:53:59 +00001423 Return -1 if an error occurred */
1424
1425int
Fred Drake100814d2000-07-09 15:48:49 +00001426PyObject_Not(PyObject *v)
Guido van Rossumc3d3f961998-04-09 17:53:59 +00001427{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001428 int res;
1429 res = PyObject_IsTrue(v);
1430 if (res < 0)
1431 return res;
1432 return res == 0;
Guido van Rossumc3d3f961998-04-09 17:53:59 +00001433}
1434
Guido van Rossum49b11fe1995-01-26 00:38:22 +00001435/* Test whether an object can be called */
1436
1437int
Fred Drake100814d2000-07-09 15:48:49 +00001438PyCallable_Check(PyObject *x)
Guido van Rossum49b11fe1995-01-26 00:38:22 +00001439{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001440 if (x == NULL)
1441 return 0;
Victor Stinner58ac7002020-02-07 03:04:21 +01001442 return Py_TYPE(x)->tp_call != NULL;
Guido van Rossum49b11fe1995-01-26 00:38:22 +00001443}
1444
Tim Peters7eea37e2001-09-04 22:08:56 +00001445
Georg Brandle32b4222007-03-10 22:13:27 +00001446/* Helper for PyObject_Dir without arguments: returns the local scope. */
1447static PyObject *
Guido van Rossumad7d8d12007-04-13 01:39:34 +00001448_dir_locals(void)
Tim Peters305b5852001-09-17 02:38:46 +00001449{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001450 PyObject *names;
Victor Stinner41bb43a2013-10-29 01:19:37 +01001451 PyObject *locals;
Tim Peters305b5852001-09-17 02:38:46 +00001452
Victor Stinner41bb43a2013-10-29 01:19:37 +01001453 locals = PyEval_GetLocals();
1454 if (locals == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001455 return NULL;
Tim Peters305b5852001-09-17 02:38:46 +00001456
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001457 names = PyMapping_Keys(locals);
1458 if (!names)
1459 return NULL;
1460 if (!PyList_Check(names)) {
1461 PyErr_Format(PyExc_TypeError,
1462 "dir(): expected keys() of locals to be a list, "
1463 "not '%.200s'", Py_TYPE(names)->tp_name);
1464 Py_DECREF(names);
1465 return NULL;
1466 }
Benjamin Peterson3bbb7222011-06-11 16:12:08 -05001467 if (PyList_Sort(names)) {
1468 Py_DECREF(names);
1469 return NULL;
1470 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001471 /* the locals don't need to be DECREF'd */
1472 return names;
Georg Brandle32b4222007-03-10 22:13:27 +00001473}
1474
Benjamin Peterson82b00c12011-05-24 11:09:06 -05001475/* Helper for PyObject_Dir: object introspection. */
Georg Brandle32b4222007-03-10 22:13:27 +00001476static PyObject *
1477_dir_object(PyObject *obj)
1478{
Benjamin Peterson3bbb7222011-06-11 16:12:08 -05001479 PyObject *result, *sorted;
Benjamin Petersonce798522012-01-22 11:24:29 -05001480 PyObject *dirfunc = _PyObject_LookupSpecial(obj, &PyId___dir__);
Georg Brandle32b4222007-03-10 22:13:27 +00001481
Victor Stinner24702042018-10-26 17:16:37 +02001482 assert(obj != NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001483 if (dirfunc == NULL) {
Benjamin Peterson82b00c12011-05-24 11:09:06 -05001484 if (!PyErr_Occurred())
1485 PyErr_SetString(PyExc_TypeError, "object does not provide __dir__");
1486 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001487 }
Benjamin Peterson82b00c12011-05-24 11:09:06 -05001488 /* use __dir__ */
Victor Stinnerf17c3de2016-12-06 18:46:19 +01001489 result = _PyObject_CallNoArg(dirfunc);
Benjamin Peterson82b00c12011-05-24 11:09:06 -05001490 Py_DECREF(dirfunc);
1491 if (result == NULL)
1492 return NULL;
Benjamin Peterson3bbb7222011-06-11 16:12:08 -05001493 /* return sorted(result) */
1494 sorted = PySequence_List(result);
1495 Py_DECREF(result);
1496 if (sorted == NULL)
1497 return NULL;
1498 if (PyList_Sort(sorted)) {
1499 Py_DECREF(sorted);
1500 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001501 }
Benjamin Peterson3bbb7222011-06-11 16:12:08 -05001502 return sorted;
Georg Brandle32b4222007-03-10 22:13:27 +00001503}
1504
1505/* Implementation of dir() -- if obj is NULL, returns the names in the current
1506 (local) scope. Otherwise, performs introspection of the object: returns a
1507 sorted list of attribute names (supposedly) accessible from the object
1508*/
1509PyObject *
1510PyObject_Dir(PyObject *obj)
1511{
Benjamin Peterson3bbb7222011-06-11 16:12:08 -05001512 return (obj == NULL) ? _dir_locals() : _dir_object(obj);
Tim Peters7eea37e2001-09-04 22:08:56 +00001513}
Guido van Rossum49b11fe1995-01-26 00:38:22 +00001514
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001515/*
Raymond Hettinger66d2be82011-07-28 09:55:13 -07001516None is a non-NULL undefined value.
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001517There is (and should be!) no way to create other objects of this type,
Guido van Rossum3f5da241990-12-20 15:06:42 +00001518so there is exactly one (which is indestructible, by the way).
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001519*/
1520
Guido van Rossum0c182a11992-03-27 17:26:13 +00001521/* ARGSUSED */
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001522static PyObject *
Fred Drake100814d2000-07-09 15:48:49 +00001523none_repr(PyObject *op)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001524{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001525 return PyUnicode_FromString("None");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001526}
1527
Barry Warsaw9bf16442001-01-23 16:24:35 +00001528/* ARGUSED */
Victor Stinner2a4903f2020-01-30 13:09:11 +01001529static void _Py_NO_RETURN
Tim Peters803526b2002-07-07 05:13:56 +00001530none_dealloc(PyObject* ignore)
Barry Warsaw9bf16442001-01-23 16:24:35 +00001531{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001532 /* This should never get called, but we also don't want to SEGV if
1533 * we accidentally decref None out of existence.
1534 */
1535 Py_FatalError("deallocating None");
Barry Warsaw9bf16442001-01-23 16:24:35 +00001536}
1537
Benjamin Petersonc4607ae2011-07-29 18:19:43 -05001538static PyObject *
1539none_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
1540{
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +02001541 if (PyTuple_GET_SIZE(args) || (kwargs && PyDict_GET_SIZE(kwargs))) {
Benjamin Petersonc4607ae2011-07-29 18:19:43 -05001542 PyErr_SetString(PyExc_TypeError, "NoneType takes no arguments");
1543 return NULL;
1544 }
1545 Py_RETURN_NONE;
1546}
1547
Raymond Hettinger66d2be82011-07-28 09:55:13 -07001548static int
1549none_bool(PyObject *v)
1550{
1551 return 0;
1552}
1553
1554static PyNumberMethods none_as_number = {
1555 0, /* nb_add */
1556 0, /* nb_subtract */
1557 0, /* nb_multiply */
1558 0, /* nb_remainder */
1559 0, /* nb_divmod */
1560 0, /* nb_power */
1561 0, /* nb_negative */
1562 0, /* nb_positive */
1563 0, /* nb_absolute */
1564 (inquiry)none_bool, /* nb_bool */
1565 0, /* nb_invert */
1566 0, /* nb_lshift */
1567 0, /* nb_rshift */
1568 0, /* nb_and */
1569 0, /* nb_xor */
1570 0, /* nb_or */
1571 0, /* nb_int */
1572 0, /* nb_reserved */
1573 0, /* nb_float */
1574 0, /* nb_inplace_add */
1575 0, /* nb_inplace_subtract */
1576 0, /* nb_inplace_multiply */
1577 0, /* nb_inplace_remainder */
1578 0, /* nb_inplace_power */
1579 0, /* nb_inplace_lshift */
1580 0, /* nb_inplace_rshift */
1581 0, /* nb_inplace_and */
1582 0, /* nb_inplace_xor */
1583 0, /* nb_inplace_or */
1584 0, /* nb_floor_divide */
1585 0, /* nb_true_divide */
1586 0, /* nb_inplace_floor_divide */
1587 0, /* nb_inplace_true_divide */
1588 0, /* nb_index */
1589};
Barry Warsaw9bf16442001-01-23 16:24:35 +00001590
Alexandre Vassalotti65846c62013-11-30 17:55:48 -08001591PyTypeObject _PyNone_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001592 PyVarObject_HEAD_INIT(&PyType_Type, 0)
1593 "NoneType",
1594 0,
1595 0,
1596 none_dealloc, /*tp_dealloc*/ /*never called*/
Jeroen Demeyer530f5062019-05-31 04:13:39 +02001597 0, /*tp_vectorcall_offset*/
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001598 0, /*tp_getattr*/
1599 0, /*tp_setattr*/
Jeroen Demeyer530f5062019-05-31 04:13:39 +02001600 0, /*tp_as_async*/
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001601 none_repr, /*tp_repr*/
Raymond Hettinger66d2be82011-07-28 09:55:13 -07001602 &none_as_number, /*tp_as_number*/
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001603 0, /*tp_as_sequence*/
1604 0, /*tp_as_mapping*/
1605 0, /*tp_hash */
Benjamin Petersonc4607ae2011-07-29 18:19:43 -05001606 0, /*tp_call */
1607 0, /*tp_str */
1608 0, /*tp_getattro */
1609 0, /*tp_setattro */
1610 0, /*tp_as_buffer */
1611 Py_TPFLAGS_DEFAULT, /*tp_flags */
1612 0, /*tp_doc */
1613 0, /*tp_traverse */
1614 0, /*tp_clear */
1615 0, /*tp_richcompare */
1616 0, /*tp_weaklistoffset */
1617 0, /*tp_iter */
1618 0, /*tp_iternext */
1619 0, /*tp_methods */
1620 0, /*tp_members */
1621 0, /*tp_getset */
1622 0, /*tp_base */
1623 0, /*tp_dict */
1624 0, /*tp_descr_get */
1625 0, /*tp_descr_set */
1626 0, /*tp_dictoffset */
1627 0, /*tp_init */
1628 0, /*tp_alloc */
1629 none_new, /*tp_new */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001630};
1631
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001632PyObject _Py_NoneStruct = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001633 _PyObject_EXTRA_INIT
Alexandre Vassalotti65846c62013-11-30 17:55:48 -08001634 1, &_PyNone_Type
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001635};
1636
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +00001637/* NotImplemented is an object that can be used to signal that an
1638 operation is not implemented for the given type combination. */
1639
1640static PyObject *
1641NotImplemented_repr(PyObject *op)
1642{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001643 return PyUnicode_FromString("NotImplemented");
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +00001644}
1645
Benjamin Peterson18d7d7a2011-07-29 18:27:44 -05001646static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05301647NotImplemented_reduce(PyObject *op, PyObject *Py_UNUSED(ignored))
Alexandre Vassalottic49477b2013-11-24 02:53:45 -08001648{
1649 return PyUnicode_FromString("NotImplemented");
1650}
1651
1652static PyMethodDef notimplemented_methods[] = {
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05301653 {"__reduce__", NotImplemented_reduce, METH_NOARGS, NULL},
Alexandre Vassalottic49477b2013-11-24 02:53:45 -08001654 {NULL, NULL}
1655};
1656
1657static PyObject *
Benjamin Peterson18d7d7a2011-07-29 18:27:44 -05001658notimplemented_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
1659{
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +02001660 if (PyTuple_GET_SIZE(args) || (kwargs && PyDict_GET_SIZE(kwargs))) {
Benjamin Peterson18d7d7a2011-07-29 18:27:44 -05001661 PyErr_SetString(PyExc_TypeError, "NotImplementedType takes no arguments");
1662 return NULL;
1663 }
Brian Curtindfc80e32011-08-10 20:28:54 -05001664 Py_RETURN_NOTIMPLEMENTED;
Benjamin Peterson18d7d7a2011-07-29 18:27:44 -05001665}
1666
Victor Stinner2a4903f2020-01-30 13:09:11 +01001667static void _Py_NO_RETURN
Armin Ronacher226b1db2012-10-06 14:28:58 +02001668notimplemented_dealloc(PyObject* ignore)
1669{
1670 /* This should never get called, but we also don't want to SEGV if
1671 * we accidentally decref NotImplemented out of existence.
1672 */
1673 Py_FatalError("deallocating NotImplemented");
1674}
1675
Alexandre Vassalotti65846c62013-11-30 17:55:48 -08001676PyTypeObject _PyNotImplemented_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001677 PyVarObject_HEAD_INIT(&PyType_Type, 0)
1678 "NotImplementedType",
1679 0,
1680 0,
Armin Ronacher226b1db2012-10-06 14:28:58 +02001681 notimplemented_dealloc, /*tp_dealloc*/ /*never called*/
Jeroen Demeyer530f5062019-05-31 04:13:39 +02001682 0, /*tp_vectorcall_offset*/
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001683 0, /*tp_getattr*/
1684 0, /*tp_setattr*/
Jeroen Demeyer530f5062019-05-31 04:13:39 +02001685 0, /*tp_as_async*/
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001686 NotImplemented_repr, /*tp_repr*/
1687 0, /*tp_as_number*/
1688 0, /*tp_as_sequence*/
1689 0, /*tp_as_mapping*/
1690 0, /*tp_hash */
Benjamin Peterson18d7d7a2011-07-29 18:27:44 -05001691 0, /*tp_call */
1692 0, /*tp_str */
1693 0, /*tp_getattro */
1694 0, /*tp_setattro */
1695 0, /*tp_as_buffer */
1696 Py_TPFLAGS_DEFAULT, /*tp_flags */
1697 0, /*tp_doc */
1698 0, /*tp_traverse */
1699 0, /*tp_clear */
1700 0, /*tp_richcompare */
1701 0, /*tp_weaklistoffset */
1702 0, /*tp_iter */
1703 0, /*tp_iternext */
Alexandre Vassalottic49477b2013-11-24 02:53:45 -08001704 notimplemented_methods, /*tp_methods */
Benjamin Peterson18d7d7a2011-07-29 18:27:44 -05001705 0, /*tp_members */
1706 0, /*tp_getset */
1707 0, /*tp_base */
1708 0, /*tp_dict */
1709 0, /*tp_descr_get */
1710 0, /*tp_descr_set */
1711 0, /*tp_dictoffset */
1712 0, /*tp_init */
1713 0, /*tp_alloc */
1714 notimplemented_new, /*tp_new */
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +00001715};
1716
1717PyObject _Py_NotImplementedStruct = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001718 _PyObject_EXTRA_INIT
Alexandre Vassalotti65846c62013-11-30 17:55:48 -08001719 1, &_PyNotImplemented_Type
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +00001720};
1721
Victor Stinner331a6a52019-05-27 16:39:22 +02001722PyStatus
Victor Stinnerab672812019-01-23 15:04:40 +01001723_PyTypes_Init(void)
Guido van Rossumba21a492001-08-16 08:17:26 +00001724{
Victor Stinner7a1f6c22020-01-30 09:02:14 +01001725 PyStatus status = _PyTypes_InitSlotDefs();
1726 if (_PyStatus_EXCEPTION(status)) {
1727 return status;
1728 }
1729
Victor Stinner6d43f6f2019-01-22 21:18:05 +01001730#define INIT_TYPE(TYPE, NAME) \
1731 do { \
1732 if (PyType_Ready(TYPE) < 0) { \
Victor Stinner331a6a52019-05-27 16:39:22 +02001733 return _PyStatus_ERR("Can't initialize " NAME " type"); \
Victor Stinner6d43f6f2019-01-22 21:18:05 +01001734 } \
1735 } while (0)
Victor Stinner5a1bb4e2014-06-02 14:10:59 +02001736
Victor Stinner6d43f6f2019-01-22 21:18:05 +01001737 INIT_TYPE(&PyBaseObject_Type, "object");
1738 INIT_TYPE(&PyType_Type, "type");
1739 INIT_TYPE(&_PyWeakref_RefType, "weakref");
1740 INIT_TYPE(&_PyWeakref_CallableProxyType, "callable weakref proxy");
1741 INIT_TYPE(&_PyWeakref_ProxyType, "weakref proxy");
1742 INIT_TYPE(&PyLong_Type, "int");
1743 INIT_TYPE(&PyBool_Type, "bool");
1744 INIT_TYPE(&PyByteArray_Type, "bytearray");
1745 INIT_TYPE(&PyBytes_Type, "str");
1746 INIT_TYPE(&PyList_Type, "list");
1747 INIT_TYPE(&_PyNone_Type, "None");
1748 INIT_TYPE(&_PyNotImplemented_Type, "NotImplemented");
1749 INIT_TYPE(&PyTraceBack_Type, "traceback");
1750 INIT_TYPE(&PySuper_Type, "super");
1751 INIT_TYPE(&PyRange_Type, "range");
1752 INIT_TYPE(&PyDict_Type, "dict");
1753 INIT_TYPE(&PyDictKeys_Type, "dict keys");
1754 INIT_TYPE(&PyDictValues_Type, "dict values");
1755 INIT_TYPE(&PyDictItems_Type, "dict items");
1756 INIT_TYPE(&PyDictRevIterKey_Type, "reversed dict keys");
1757 INIT_TYPE(&PyDictRevIterValue_Type, "reversed dict values");
1758 INIT_TYPE(&PyDictRevIterItem_Type, "reversed dict items");
1759 INIT_TYPE(&PyODict_Type, "OrderedDict");
1760 INIT_TYPE(&PyODictKeys_Type, "odict_keys");
1761 INIT_TYPE(&PyODictItems_Type, "odict_items");
1762 INIT_TYPE(&PyODictValues_Type, "odict_values");
1763 INIT_TYPE(&PyODictIter_Type, "odict_keyiterator");
1764 INIT_TYPE(&PySet_Type, "set");
1765 INIT_TYPE(&PyUnicode_Type, "str");
1766 INIT_TYPE(&PySlice_Type, "slice");
1767 INIT_TYPE(&PyStaticMethod_Type, "static method");
1768 INIT_TYPE(&PyComplex_Type, "complex");
1769 INIT_TYPE(&PyFloat_Type, "float");
1770 INIT_TYPE(&PyFrozenSet_Type, "frozenset");
1771 INIT_TYPE(&PyProperty_Type, "property");
1772 INIT_TYPE(&_PyManagedBuffer_Type, "managed buffer");
1773 INIT_TYPE(&PyMemoryView_Type, "memoryview");
1774 INIT_TYPE(&PyTuple_Type, "tuple");
1775 INIT_TYPE(&PyEnum_Type, "enumerate");
1776 INIT_TYPE(&PyReversed_Type, "reversed");
1777 INIT_TYPE(&PyStdPrinter_Type, "StdPrinter");
1778 INIT_TYPE(&PyCode_Type, "code");
1779 INIT_TYPE(&PyFrame_Type, "frame");
1780 INIT_TYPE(&PyCFunction_Type, "builtin function");
1781 INIT_TYPE(&PyMethod_Type, "method");
1782 INIT_TYPE(&PyFunction_Type, "function");
1783 INIT_TYPE(&PyDictProxy_Type, "dict proxy");
1784 INIT_TYPE(&PyGen_Type, "generator");
1785 INIT_TYPE(&PyGetSetDescr_Type, "get-set descriptor");
1786 INIT_TYPE(&PyWrapperDescr_Type, "wrapper");
1787 INIT_TYPE(&_PyMethodWrapper_Type, "method wrapper");
1788 INIT_TYPE(&PyEllipsis_Type, "ellipsis");
1789 INIT_TYPE(&PyMemberDescr_Type, "member descriptor");
1790 INIT_TYPE(&_PyNamespace_Type, "namespace");
1791 INIT_TYPE(&PyCapsule_Type, "capsule");
1792 INIT_TYPE(&PyLongRangeIter_Type, "long range iterator");
1793 INIT_TYPE(&PyCell_Type, "cell");
1794 INIT_TYPE(&PyInstanceMethod_Type, "instance method");
1795 INIT_TYPE(&PyClassMethodDescr_Type, "class method descr");
1796 INIT_TYPE(&PyMethodDescr_Type, "method descr");
1797 INIT_TYPE(&PyCallIter_Type, "call iter");
1798 INIT_TYPE(&PySeqIter_Type, "sequence iterator");
Antoine Pitrou91f43802019-05-26 17:10:09 +02001799 INIT_TYPE(&PyPickleBuffer_Type, "pickle.PickleBuffer");
Victor Stinner6d43f6f2019-01-22 21:18:05 +01001800 INIT_TYPE(&PyCoro_Type, "coroutine");
1801 INIT_TYPE(&_PyCoroWrapper_Type, "coroutine wrapper");
Eric Snowc11183c2019-03-15 16:35:46 -06001802 INIT_TYPE(&_PyInterpreterID_Type, "interpreter ID");
Victor Stinner331a6a52019-05-27 16:39:22 +02001803 return _PyStatus_OK();
Guido van Rossumba21a492001-08-16 08:17:26 +00001804
Victor Stinner6d43f6f2019-01-22 21:18:05 +01001805#undef INIT_TYPE
Guido van Rossumba21a492001-08-16 08:17:26 +00001806}
1807
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001808
Victor Stinner40e547d2020-02-05 01:11:10 +01001809void
1810_Py_NewReference(PyObject *op)
1811{
1812 if (_Py_tracemalloc_config.tracing) {
1813 _PyTraceMalloc_NewReference(op);
1814 }
1815#ifdef Py_REF_DEBUG
1816 _Py_RefTotal++;
1817#endif
Victor Stinnerc86a1122020-02-07 01:24:29 +01001818 Py_SET_REFCNT(op, 1);
Victor Stinner40e547d2020-02-05 01:11:10 +01001819#ifdef Py_TRACE_REFS
1820 _Py_AddToAllObjects(op, 1);
1821#endif
1822}
1823
1824
Guido van Rossum84a90321996-05-22 16:34:47 +00001825#ifdef Py_TRACE_REFS
Guido van Rossumaacdc9d1996-08-12 21:32:12 +00001826void
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02001827_Py_ForgetReference(PyObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001828{
Victor Stinnera93c51e2020-02-07 00:38:59 +01001829 if (Py_REFCNT(op) < 0) {
Victor Stinner5eb8bff2020-01-30 09:01:07 +01001830 _PyObject_ASSERT_FAILED_MSG(op, "negative refcnt");
1831 }
1832
1833 if (op == &refchain ||
1834 op->_ob_prev->_ob_next != op || op->_ob_next->_ob_prev != op)
1835 {
1836 _PyObject_ASSERT_FAILED_MSG(op, "invalid object chain");
1837 }
1838
Guido van Rossumbffd6832000-01-20 22:32:56 +00001839#ifdef SLOW_UNREF_CHECK
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02001840 PyObject *p;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001841 for (p = refchain._ob_next; p != &refchain; p = p->_ob_next) {
Victor Stinner5eb8bff2020-01-30 09:01:07 +01001842 if (p == op) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001843 break;
Victor Stinner5eb8bff2020-01-30 09:01:07 +01001844 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001845 }
Victor Stinner5eb8bff2020-01-30 09:01:07 +01001846 if (p == &refchain) {
1847 /* Not found */
1848 _PyObject_ASSERT_FAILED_MSG(op,
1849 "object not found in the objects list");
1850 }
Guido van Rossum2e8f6141992-09-03 20:32:55 +00001851#endif
Victor Stinner5eb8bff2020-01-30 09:01:07 +01001852
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001853 op->_ob_next->_ob_prev = op->_ob_prev;
1854 op->_ob_prev->_ob_next = op->_ob_next;
1855 op->_ob_next = op->_ob_prev = NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001856}
1857
Tim Peters269b2a62003-04-17 19:52:29 +00001858/* Print all live objects. Because PyObject_Print is called, the
1859 * interpreter must be in a healthy state.
1860 */
Guido van Rossumaacdc9d1996-08-12 21:32:12 +00001861void
Fred Drake100814d2000-07-09 15:48:49 +00001862_Py_PrintReferences(FILE *fp)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001863{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001864 PyObject *op;
1865 fprintf(fp, "Remaining objects:\n");
1866 for (op = refchain._ob_next; op != &refchain; op = op->_ob_next) {
Victor Stinnera93c51e2020-02-07 00:38:59 +01001867 fprintf(fp, "%p [%" PY_FORMAT_SIZE_T "d] ", (void *)op, Py_REFCNT(op));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001868 if (PyObject_Print(op, fp, 0) != 0)
1869 PyErr_Clear();
1870 putc('\n', fp);
1871 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001872}
1873
Tim Peters269b2a62003-04-17 19:52:29 +00001874/* Print the addresses of all live objects. Unlike _Py_PrintReferences, this
1875 * doesn't make any calls to the Python C API, so is always safe to call.
1876 */
1877void
1878_Py_PrintReferenceAddresses(FILE *fp)
1879{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001880 PyObject *op;
1881 fprintf(fp, "Remaining object addresses:\n");
1882 for (op = refchain._ob_next; op != &refchain; op = op->_ob_next)
Zackery Spytz1a2252e2019-05-06 10:56:51 -06001883 fprintf(fp, "%p [%" PY_FORMAT_SIZE_T "d] %s\n", (void *)op,
Victor Stinnera93c51e2020-02-07 00:38:59 +01001884 Py_REFCNT(op), Py_TYPE(op)->tp_name);
Tim Peters269b2a62003-04-17 19:52:29 +00001885}
1886
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +00001887PyObject *
Fred Drake100814d2000-07-09 15:48:49 +00001888_Py_GetObjects(PyObject *self, PyObject *args)
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +00001889{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001890 int i, n;
1891 PyObject *t = NULL;
1892 PyObject *res, *op;
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +00001893
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001894 if (!PyArg_ParseTuple(args, "i|O", &n, &t))
1895 return NULL;
1896 op = refchain._ob_next;
1897 res = PyList_New(0);
1898 if (res == NULL)
1899 return NULL;
1900 for (i = 0; (n == 0 || i < n) && op != &refchain; i++) {
1901 while (op == self || op == args || op == res || op == t ||
1902 (t != NULL && Py_TYPE(op) != (PyTypeObject *) t)) {
1903 op = op->_ob_next;
1904 if (op == &refchain)
1905 return res;
1906 }
1907 if (PyList_Append(res, op) < 0) {
1908 Py_DECREF(res);
1909 return NULL;
1910 }
1911 op = op->_ob_next;
1912 }
1913 return res;
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +00001914}
1915
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001916#endif
Guido van Rossum97ead3f1996-01-12 01:24:09 +00001917
Benjamin Petersonb173f782009-05-05 22:31:58 +00001918
Guido van Rossum84a90321996-05-22 16:34:47 +00001919/* Hack to force loading of abstract.o */
Martin v. Löwis18e16552006-02-15 17:27:45 +00001920Py_ssize_t (*_Py_abstract_hack)(PyObject *) = PyObject_Size;
Guido van Rossume09fb551997-08-05 02:04:34 +00001921
1922
David Malcolm49526f42012-06-22 14:55:41 -04001923void
1924_PyObject_DebugTypeStats(FILE *out)
1925{
David Malcolm49526f42012-06-22 14:55:41 -04001926 _PyDict_DebugMallocStats(out);
1927 _PyFloat_DebugMallocStats(out);
1928 _PyFrame_DebugMallocStats(out);
1929 _PyList_DebugMallocStats(out);
David Malcolm49526f42012-06-22 14:55:41 -04001930 _PyTuple_DebugMallocStats(out);
1931}
Guido van Rossumb18618d2000-05-03 23:44:39 +00001932
Guido van Rossum86610361998-04-10 22:32:46 +00001933/* These methods are used to control infinite recursion in repr, str, print,
1934 etc. Container objects that may recursively contain themselves,
Martin Panter8d56c022016-05-29 04:13:35 +00001935 e.g. builtin dictionaries and lists, should use Py_ReprEnter() and
Guido van Rossum86610361998-04-10 22:32:46 +00001936 Py_ReprLeave() to avoid infinite recursion.
1937
1938 Py_ReprEnter() returns 0 the first time it is called for a particular
1939 object and 1 every time thereafter. It returns -1 if an exception
1940 occurred. Py_ReprLeave() has no return value.
1941
1942 See dictobject.c and listobject.c for examples of use.
1943*/
1944
Guido van Rossum86610361998-04-10 22:32:46 +00001945int
Fred Drake100814d2000-07-09 15:48:49 +00001946Py_ReprEnter(PyObject *obj)
Guido van Rossum86610361998-04-10 22:32:46 +00001947{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001948 PyObject *dict;
1949 PyObject *list;
1950 Py_ssize_t i;
Guido van Rossum86610361998-04-10 22:32:46 +00001951
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001952 dict = PyThreadState_GetDict();
Antoine Pitrou04d17d32014-03-31 22:04:38 +02001953 /* Ignore a missing thread-state, so that this function can be called
1954 early on startup. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001955 if (dict == NULL)
1956 return 0;
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02001957 list = _PyDict_GetItemIdWithError(dict, &PyId_Py_Repr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001958 if (list == NULL) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02001959 if (PyErr_Occurred()) {
1960 return -1;
1961 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001962 list = PyList_New(0);
1963 if (list == NULL)
1964 return -1;
Victor Stinner7a07e452013-11-06 18:57:29 +01001965 if (_PyDict_SetItemId(dict, &PyId_Py_Repr, list) < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001966 return -1;
1967 Py_DECREF(list);
1968 }
1969 i = PyList_GET_SIZE(list);
1970 while (--i >= 0) {
1971 if (PyList_GET_ITEM(list, i) == obj)
1972 return 1;
1973 }
Victor Stinnere901d1f2013-07-17 21:58:41 +02001974 if (PyList_Append(list, obj) < 0)
1975 return -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001976 return 0;
Guido van Rossum86610361998-04-10 22:32:46 +00001977}
1978
1979void
Fred Drake100814d2000-07-09 15:48:49 +00001980Py_ReprLeave(PyObject *obj)
Guido van Rossum86610361998-04-10 22:32:46 +00001981{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001982 PyObject *dict;
1983 PyObject *list;
1984 Py_ssize_t i;
Victor Stinner1b634932013-07-16 22:24:44 +02001985 PyObject *error_type, *error_value, *error_traceback;
1986
1987 PyErr_Fetch(&error_type, &error_value, &error_traceback);
Guido van Rossum86610361998-04-10 22:32:46 +00001988
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001989 dict = PyThreadState_GetDict();
1990 if (dict == NULL)
Victor Stinner1b634932013-07-16 22:24:44 +02001991 goto finally;
1992
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02001993 list = _PyDict_GetItemIdWithError(dict, &PyId_Py_Repr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001994 if (list == NULL || !PyList_Check(list))
Victor Stinner1b634932013-07-16 22:24:44 +02001995 goto finally;
1996
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001997 i = PyList_GET_SIZE(list);
1998 /* Count backwards because we always expect obj to be list[-1] */
1999 while (--i >= 0) {
2000 if (PyList_GET_ITEM(list, i) == obj) {
2001 PyList_SetSlice(list, i, i + 1, NULL);
2002 break;
2003 }
2004 }
Victor Stinner1b634932013-07-16 22:24:44 +02002005
2006finally:
2007 /* ignore exceptions because there is no way to report them. */
2008 PyErr_Restore(error_type, error_value, error_traceback);
Guido van Rossum86610361998-04-10 22:32:46 +00002009}
Guido van Rossumd724b232000-03-13 16:01:29 +00002010
Tim Peters803526b2002-07-07 05:13:56 +00002011/* Trashcan support. */
Guido van Rossumd724b232000-03-13 16:01:29 +00002012
Tim Peters803526b2002-07-07 05:13:56 +00002013/* Add op to the _PyTrash_delete_later list. Called when the current
2014 * call-stack depth gets large. op must be a currently untracked gc'ed
2015 * object, with refcount 0. Py_DECREF must already have been called on it.
2016 */
Guido van Rossumd724b232000-03-13 16:01:29 +00002017void
Fred Drake100814d2000-07-09 15:48:49 +00002018_PyTrash_deposit_object(PyObject *op)
Guido van Rossumd724b232000-03-13 16:01:29 +00002019{
Victor Stinner72474072019-11-20 12:25:50 +01002020 PyThreadState *tstate = _PyThreadState_GET();
2021 struct _gc_runtime_state *gcstate = &tstate->interp->gc;
2022
Victor Stinner24702042018-10-26 17:16:37 +02002023 _PyObject_ASSERT(op, PyObject_IS_GC(op));
2024 _PyObject_ASSERT(op, !_PyObject_GC_IS_TRACKED(op));
Victor Stinnera93c51e2020-02-07 00:38:59 +01002025 _PyObject_ASSERT(op, Py_REFCNT(op) == 0);
Victor Stinner72474072019-11-20 12:25:50 +01002026 _PyGCHead_SET_PREV(_Py_AS_GC(op), gcstate->trash_delete_later);
2027 gcstate->trash_delete_later = op;
Guido van Rossumd724b232000-03-13 16:01:29 +00002028}
2029
Antoine Pitrou2b0218a2012-09-06 00:59:49 +02002030/* The equivalent API, using per-thread state recursion info */
2031void
2032_PyTrash_thread_deposit_object(PyObject *op)
2033{
Victor Stinner50b48572018-11-01 01:51:40 +01002034 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinner24702042018-10-26 17:16:37 +02002035 _PyObject_ASSERT(op, PyObject_IS_GC(op));
2036 _PyObject_ASSERT(op, !_PyObject_GC_IS_TRACKED(op));
Victor Stinnera93c51e2020-02-07 00:38:59 +01002037 _PyObject_ASSERT(op, Py_REFCNT(op) == 0);
INADA Naoki5ac9e6e2018-07-10 17:19:53 +09002038 _PyGCHead_SET_PREV(_Py_AS_GC(op), tstate->trash_delete_later);
Antoine Pitrou2b0218a2012-09-06 00:59:49 +02002039 tstate->trash_delete_later = op;
2040}
2041
Min ho Kimc4cacc82019-07-31 08:16:13 +10002042/* Deallocate all the objects in the _PyTrash_delete_later list. Called when
Tim Peters803526b2002-07-07 05:13:56 +00002043 * the call-stack unwinds again.
2044 */
Guido van Rossumd724b232000-03-13 16:01:29 +00002045void
Fred Drake100814d2000-07-09 15:48:49 +00002046_PyTrash_destroy_chain(void)
Guido van Rossumd724b232000-03-13 16:01:29 +00002047{
Victor Stinner72474072019-11-20 12:25:50 +01002048 PyThreadState *tstate = _PyThreadState_GET();
2049 struct _gc_runtime_state *gcstate = &tstate->interp->gc;
2050
2051 while (gcstate->trash_delete_later) {
2052 PyObject *op = gcstate->trash_delete_later;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002053 destructor dealloc = Py_TYPE(op)->tp_dealloc;
Neil Schemenauerf589c052002-03-29 03:05:54 +00002054
Victor Stinner72474072019-11-20 12:25:50 +01002055 gcstate->trash_delete_later =
INADA Naoki5ac9e6e2018-07-10 17:19:53 +09002056 (PyObject*) _PyGCHead_PREV(_Py_AS_GC(op));
Neil Schemenauerf589c052002-03-29 03:05:54 +00002057
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002058 /* Call the deallocator directly. This used to try to
2059 * fool Py_DECREF into calling it indirectly, but
2060 * Py_DECREF was already called on this object, and in
2061 * assorted non-release builds calling Py_DECREF again ends
2062 * up distorting allocation statistics.
2063 */
Victor Stinnera93c51e2020-02-07 00:38:59 +01002064 _PyObject_ASSERT(op, Py_REFCNT(op) == 0);
Victor Stinner72474072019-11-20 12:25:50 +01002065 ++gcstate->trash_delete_nesting;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002066 (*dealloc)(op);
Victor Stinner72474072019-11-20 12:25:50 +01002067 --gcstate->trash_delete_nesting;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002068 }
Guido van Rossumd724b232000-03-13 16:01:29 +00002069}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002070
Antoine Pitrou2b0218a2012-09-06 00:59:49 +02002071/* The equivalent API, using per-thread state recursion info */
2072void
2073_PyTrash_thread_destroy_chain(void)
2074{
Victor Stinner50b48572018-11-01 01:51:40 +01002075 PyThreadState *tstate = _PyThreadState_GET();
Xiang Zhanga66f9c62017-05-13 13:36:14 +08002076 /* We need to increase trash_delete_nesting here, otherwise,
2077 _PyTrash_thread_destroy_chain will be called recursively
2078 and then possibly crash. An example that may crash without
2079 increase:
2080 N = 500000 # need to be large enough
2081 ob = object()
2082 tups = [(ob,) for i in range(N)]
2083 for i in range(49):
2084 tups = [(tup,) for tup in tups]
2085 del tups
2086 */
2087 assert(tstate->trash_delete_nesting == 0);
2088 ++tstate->trash_delete_nesting;
Antoine Pitrou2b0218a2012-09-06 00:59:49 +02002089 while (tstate->trash_delete_later) {
2090 PyObject *op = tstate->trash_delete_later;
2091 destructor dealloc = Py_TYPE(op)->tp_dealloc;
2092
2093 tstate->trash_delete_later =
INADA Naoki5ac9e6e2018-07-10 17:19:53 +09002094 (PyObject*) _PyGCHead_PREV(_Py_AS_GC(op));
Antoine Pitrou2b0218a2012-09-06 00:59:49 +02002095
2096 /* Call the deallocator directly. This used to try to
2097 * fool Py_DECREF into calling it indirectly, but
2098 * Py_DECREF was already called on this object, and in
2099 * assorted non-release builds calling Py_DECREF again ends
2100 * up distorting allocation statistics.
2101 */
Victor Stinnera93c51e2020-02-07 00:38:59 +01002102 _PyObject_ASSERT(op, Py_REFCNT(op) == 0);
Antoine Pitrou2b0218a2012-09-06 00:59:49 +02002103 (*dealloc)(op);
Xiang Zhanga66f9c62017-05-13 13:36:14 +08002104 assert(tstate->trash_delete_nesting == 1);
Antoine Pitrou2b0218a2012-09-06 00:59:49 +02002105 }
Xiang Zhanga66f9c62017-05-13 13:36:14 +08002106 --tstate->trash_delete_nesting;
Antoine Pitrou2b0218a2012-09-06 00:59:49 +02002107}
2108
Victor Stinner626bff82018-10-25 17:31:10 +02002109
Victor Stinner2a4903f2020-01-30 13:09:11 +01002110void _Py_NO_RETURN
Victor Stinnerf1d002c2018-11-21 23:53:44 +01002111_PyObject_AssertFailed(PyObject *obj, const char *expr, const char *msg,
Victor Stinner626bff82018-10-25 17:31:10 +02002112 const char *file, int line, const char *function)
2113{
Victor Stinnerf1d002c2018-11-21 23:53:44 +01002114 fprintf(stderr, "%s:%d: ", file, line);
2115 if (function) {
2116 fprintf(stderr, "%s: ", function);
2117 }
Victor Stinner626bff82018-10-25 17:31:10 +02002118 fflush(stderr);
Victor Stinner68762572019-10-07 18:42:01 +02002119
Victor Stinnerf1d002c2018-11-21 23:53:44 +01002120 if (expr) {
2121 fprintf(stderr, "Assertion \"%s\" failed", expr);
Victor Stinner626bff82018-10-25 17:31:10 +02002122 }
2123 else {
Victor Stinnerf1d002c2018-11-21 23:53:44 +01002124 fprintf(stderr, "Assertion failed");
Victor Stinner626bff82018-10-25 17:31:10 +02002125 }
2126 fflush(stderr);
Victor Stinner68762572019-10-07 18:42:01 +02002127
Victor Stinnerf1d002c2018-11-21 23:53:44 +01002128 if (msg) {
2129 fprintf(stderr, ": %s", msg);
2130 }
2131 fprintf(stderr, "\n");
2132 fflush(stderr);
Victor Stinner626bff82018-10-25 17:31:10 +02002133
Victor Stinner68762572019-10-07 18:42:01 +02002134 if (_PyObject_IsFreed(obj)) {
Victor Stinner626bff82018-10-25 17:31:10 +02002135 /* It seems like the object memory has been freed:
2136 don't access it to prevent a segmentation fault. */
Victor Stinnerb39afb72019-09-17 23:36:28 +02002137 fprintf(stderr, "<object at %p is freed>\n", obj);
Victor Stinner68762572019-10-07 18:42:01 +02002138 fflush(stderr);
Victor Stinner626bff82018-10-25 17:31:10 +02002139 }
2140 else {
penguindustin96466302019-05-06 14:57:17 -04002141 /* Display the traceback where the object has been allocated.
Victor Stinner626bff82018-10-25 17:31:10 +02002142 Do it before dumping repr(obj), since repr() is more likely
2143 to crash than dumping the traceback. */
2144 void *ptr;
2145 PyTypeObject *type = Py_TYPE(obj);
2146 if (PyType_IS_GC(type)) {
2147 ptr = (void *)((char *)obj - sizeof(PyGC_Head));
2148 }
2149 else {
2150 ptr = (void *)obj;
2151 }
2152 _PyMem_DumpTraceback(fileno(stderr), ptr);
2153
2154 /* This might succeed or fail, but we're about to abort, so at least
2155 try to provide any extra info we can: */
2156 _PyObject_Dump(obj);
Victor Stinner77753492019-10-07 23:44:05 +02002157
2158 fprintf(stderr, "\n");
2159 fflush(stderr);
Victor Stinner626bff82018-10-25 17:31:10 +02002160 }
Victor Stinner626bff82018-10-25 17:31:10 +02002161
2162 Py_FatalError("_PyObject_AssertFailed");
2163}
2164
Victor Stinner3c09dca2018-10-30 14:48:26 +01002165
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00002166void
2167_Py_Dealloc(PyObject *op)
2168{
Victor Stinner3c09dca2018-10-30 14:48:26 +01002169 destructor dealloc = Py_TYPE(op)->tp_dealloc;
2170#ifdef Py_TRACE_REFS
2171 _Py_ForgetReference(op);
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00002172#endif
Victor Stinner3c09dca2018-10-30 14:48:26 +01002173 (*dealloc)(op);
2174}
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00002175
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002176#ifdef __cplusplus
2177}
2178#endif