blob: 72c4189d3458cd7dcd32b74b5017f0457528942b [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 Stinner224481a2020-03-13 10:19:38 +01005#include "pycore_ceval.h" // _Py_EnterRecursiveCall()
Victor Stinnerbe434dc2019-11-05 00:51:22 +01006#include "pycore_context.h"
Victor Stinner331a6a52019-05-27 16:39:22 +02007#include "pycore_initconfig.h"
Victor Stinner0fc91ee2019-04-12 21:51:34 +02008#include "pycore_object.h"
Victor Stinnerbe434dc2019-11-05 00:51:22 +01009#include "pycore_pyerrors.h"
Victor Stinner7a1f6c22020-01-30 09:02:14 +010010#include "pycore_pylifecycle.h"
Victor Stinner621cebe2018-11-12 16:53:38 +010011#include "pycore_pystate.h"
Benjamin Petersonfd838e62009-04-20 02:09:13 +000012#include "frameobject.h"
Eric Snowc11183c2019-03-15 16:35:46 -060013#include "interpreteridobject.h"
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000014
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000015#ifdef __cplusplus
16extern "C" {
17#endif
18
Victor Stinner626bff82018-10-25 17:31:10 +020019/* Defined in tracemalloc.c */
20extern void _PyMem_DumpTraceback(int fd, const void *ptr);
21
Victor Stinnerbd303c12013-11-07 23:07:29 +010022_Py_IDENTIFIER(Py_Repr);
23_Py_IDENTIFIER(__bytes__);
24_Py_IDENTIFIER(__dir__);
25_Py_IDENTIFIER(__isabstractmethod__);
Victor Stinnerbd303c12013-11-07 23:07:29 +010026
Victor Stinner0fc91ee2019-04-12 21:51:34 +020027
28int
29_PyObject_CheckConsistency(PyObject *op, int check_content)
30{
Victor Stinner68762572019-10-07 18:42:01 +020031#define CHECK(expr) \
32 do { if (!(expr)) { _PyObject_ASSERT_FAILED_MSG(op, Py_STRINGIFY(expr)); } } while (0)
Victor Stinner0fc91ee2019-04-12 21:51:34 +020033
Victor Stinner68762572019-10-07 18:42:01 +020034 CHECK(!_PyObject_IsFreed(op));
35 CHECK(Py_REFCNT(op) >= 1);
36
Victor Stinner58ac7002020-02-07 03:04:21 +010037 _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 Stinner1fb5a9f2020-03-06 15:55:14 +0100147 return PyObject_INIT(op, tp);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000148}
149
Guido van Rossumb18618d2000-05-03 23:44:39 +0000150PyVarObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000151PyObject_InitVar(PyVarObject *op, PyTypeObject *tp, Py_ssize_t size)
Guido van Rossumb18618d2000-05-03 23:44:39 +0000152{
Victor Stinnerf58bd7c2020-02-05 13:12:19 +0100153 /* Any changes should be reflected in PyObject_INIT_VAR() macro */
154 if (op == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000155 return (PyVarObject *) PyErr_NoMemory();
Victor Stinnerf58bd7c2020-02-05 13:12:19 +0100156 }
157
Victor Stinner1fb5a9f2020-03-06 15:55:14 +0100158 return PyObject_INIT_VAR(op, tp, size);
Guido van Rossumb18618d2000-05-03 23:44:39 +0000159}
160
161PyObject *
Fred Drake100814d2000-07-09 15:48:49 +0000162_PyObject_New(PyTypeObject *tp)
Guido van Rossumb18618d2000-05-03 23:44:39 +0000163{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000164 PyObject *op;
165 op = (PyObject *) PyObject_MALLOC(_PyObject_SIZE(tp));
166 if (op == NULL)
167 return PyErr_NoMemory();
168 return PyObject_INIT(op, tp);
Guido van Rossumb18618d2000-05-03 23:44:39 +0000169}
170
Guido van Rossumd0c87ee1997-05-15 21:31:03 +0000171PyVarObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000172_PyObject_NewVar(PyTypeObject *tp, Py_ssize_t nitems)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000173{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000174 PyVarObject *op;
175 const size_t size = _PyObject_VAR_SIZE(tp, nitems);
176 op = (PyVarObject *) PyObject_MALLOC(size);
177 if (op == NULL)
178 return (PyVarObject *)PyErr_NoMemory();
179 return PyObject_INIT_VAR(op, tp, nitems);
Guido van Rossumb18618d2000-05-03 23:44:39 +0000180}
181
Antoine Pitrou796564c2013-07-30 19:59:21 +0200182void
183PyObject_CallFinalizer(PyObject *self)
184{
185 PyTypeObject *tp = Py_TYPE(self);
186
Antoine Pitrouada319b2019-05-29 22:12:38 +0200187 if (tp->tp_finalize == NULL)
Antoine Pitrou796564c2013-07-30 19:59:21 +0200188 return;
189 /* tp_finalize should only be called once. */
190 if (PyType_IS_GC(tp) && _PyGC_FINALIZED(self))
191 return;
192
193 tp->tp_finalize(self);
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900194 if (PyType_IS_GC(tp)) {
195 _PyGC_SET_FINALIZED(self);
196 }
Antoine Pitrou796564c2013-07-30 19:59:21 +0200197}
198
199int
200PyObject_CallFinalizerFromDealloc(PyObject *self)
201{
Victor Stinnera93c51e2020-02-07 00:38:59 +0100202 if (Py_REFCNT(self) != 0) {
Victor Stinner5eb8bff2020-01-30 09:01:07 +0100203 _PyObject_ASSERT_FAILED_MSG(self,
204 "PyObject_CallFinalizerFromDealloc called "
205 "on object with a non-zero refcount");
206 }
Antoine Pitrou796564c2013-07-30 19:59:21 +0200207
208 /* Temporarily resurrect the object. */
Victor Stinnerc86a1122020-02-07 01:24:29 +0100209 Py_SET_REFCNT(self, 1);
Antoine Pitrou796564c2013-07-30 19:59:21 +0200210
211 PyObject_CallFinalizer(self);
212
Victor Stinner24702042018-10-26 17:16:37 +0200213 _PyObject_ASSERT_WITH_MSG(self,
Victor Stinnera93c51e2020-02-07 00:38:59 +0100214 Py_REFCNT(self) > 0,
Victor Stinner24702042018-10-26 17:16:37 +0200215 "refcount is too small");
Victor Stinner5eb8bff2020-01-30 09:01:07 +0100216
217 /* Undo the temporary resurrection; can't use DECREF here, it would
218 * cause a recursive call. */
Victor Stinnerc86a1122020-02-07 01:24:29 +0100219 Py_SET_REFCNT(self, Py_REFCNT(self) - 1);
220 if (Py_REFCNT(self) == 0) {
Antoine Pitrou796564c2013-07-30 19:59:21 +0200221 return 0; /* this is the normal path out */
Victor Stinner5eb8bff2020-01-30 09:01:07 +0100222 }
Antoine Pitrou796564c2013-07-30 19:59:21 +0200223
224 /* tp_finalize resurrected it! Make it look like the original Py_DECREF
Victor Stinner5eb8bff2020-01-30 09:01:07 +0100225 * never happened. */
Victor Stinnera93c51e2020-02-07 00:38:59 +0100226 Py_ssize_t refcnt = Py_REFCNT(self);
Antoine Pitrou796564c2013-07-30 19:59:21 +0200227 _Py_NewReference(self);
Victor Stinnerc86a1122020-02-07 01:24:29 +0100228 Py_SET_REFCNT(self, refcnt);
Antoine Pitrou796564c2013-07-30 19:59:21 +0200229
Victor Stinner24702042018-10-26 17:16:37 +0200230 _PyObject_ASSERT(self,
231 (!PyType_IS_GC(Py_TYPE(self))
232 || _PyObject_GC_IS_TRACKED(self)));
Victor Stinner49932fe2020-02-03 17:55:05 +0100233 /* If Py_REF_DEBUG macro is defined, _Py_NewReference() increased
234 _Py_RefTotal, so we need to undo that. */
235#ifdef Py_REF_DEBUG
236 _Py_RefTotal--;
237#endif
Antoine Pitrou796564c2013-07-30 19:59:21 +0200238 return -1;
239}
240
Antoine Pitrouc47bd4a2010-07-27 22:08:27 +0000241int
242PyObject_Print(PyObject *op, FILE *fp, int flags)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000243{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000244 int ret = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000245 if (PyErr_CheckSignals())
246 return -1;
Guido van Rossum9b00dfa1998-04-28 16:06:54 +0000247#ifdef USE_STACKCHECK
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000248 if (PyOS_CheckStack()) {
249 PyErr_SetString(PyExc_MemoryError, "stack overflow");
250 return -1;
251 }
Guido van Rossum9b00dfa1998-04-28 16:06:54 +0000252#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000253 clearerr(fp); /* Clear any previous error condition */
254 if (op == NULL) {
255 Py_BEGIN_ALLOW_THREADS
256 fprintf(fp, "<nil>");
257 Py_END_ALLOW_THREADS
258 }
259 else {
Victor Stinnera93c51e2020-02-07 00:38:59 +0100260 if (Py_REFCNT(op) <= 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000261 /* XXX(twouters) cast refcount to long until %zd is
262 universally available */
263 Py_BEGIN_ALLOW_THREADS
264 fprintf(fp, "<refcnt %ld at %p>",
Victor Stinnera93c51e2020-02-07 00:38:59 +0100265 (long)Py_REFCNT(op), (void *)op);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000266 Py_END_ALLOW_THREADS
Victor Stinner3ec9af72018-10-26 02:12:34 +0200267 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000268 else {
269 PyObject *s;
270 if (flags & Py_PRINT_RAW)
271 s = PyObject_Str(op);
272 else
273 s = PyObject_Repr(op);
274 if (s == NULL)
275 ret = -1;
276 else if (PyBytes_Check(s)) {
277 fwrite(PyBytes_AS_STRING(s), 1,
278 PyBytes_GET_SIZE(s), fp);
279 }
280 else if (PyUnicode_Check(s)) {
281 PyObject *t;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200282 t = PyUnicode_AsEncodedString(s, "utf-8", "backslashreplace");
Zackery Spytzae62f012018-10-06 00:44:25 -0600283 if (t == NULL) {
284 ret = -1;
285 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000286 else {
287 fwrite(PyBytes_AS_STRING(t), 1,
288 PyBytes_GET_SIZE(t), fp);
Victor Stinnerba6b4302010-05-17 09:33:42 +0000289 Py_DECREF(t);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000290 }
291 }
292 else {
293 PyErr_Format(PyExc_TypeError,
294 "str() or repr() returned '%.100s'",
Victor Stinner58ac7002020-02-07 03:04:21 +0100295 Py_TYPE(s)->tp_name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000296 ret = -1;
297 }
298 Py_XDECREF(s);
299 }
300 }
301 if (ret == 0) {
302 if (ferror(fp)) {
Serhiy Storchaka55fe1ae2017-04-16 10:46:38 +0300303 PyErr_SetFromErrno(PyExc_OSError);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000304 clearerr(fp);
305 ret = -1;
306 }
307 }
308 return ret;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000309}
310
Guido van Rossum38938152006-08-21 23:36:26 +0000311/* For debugging convenience. Set a breakpoint here and call it from your DLL */
312void
Thomas Woutersb2137042007-02-01 18:02:27 +0000313_Py_BreakPoint(void)
Guido van Rossum38938152006-08-21 23:36:26 +0000314{
315}
316
Neal Norwitz1a997502003-01-13 20:13:12 +0000317
Victor Stinner4c409be2019-04-11 13:01:15 +0200318/* Heuristic checking if the object memory is uninitialized or deallocated.
319 Rely on the debug hooks on Python memory allocators:
320 see _PyMem_IsPtrFreed().
Victor Stinner82af0b62018-10-23 17:39:40 +0200321
322 The function can be used to prevent segmentation fault on dereferencing
Victor Stinner4c409be2019-04-11 13:01:15 +0200323 pointers like 0xDDDDDDDDDDDDDDDD. */
Victor Stinner82af0b62018-10-23 17:39:40 +0200324int
325_PyObject_IsFreed(PyObject *op)
326{
Victor Stinner58ac7002020-02-07 03:04:21 +0100327 if (_PyMem_IsPtrFreed(op) || _PyMem_IsPtrFreed(Py_TYPE(op))) {
Victor Stinner2cf5d322018-11-22 16:32:57 +0100328 return 1;
329 }
Victor Stinner2b00db62019-04-11 11:33:27 +0200330 /* ignore op->ob_ref: its value can have be modified
Victor Stinner82af0b62018-10-23 17:39:40 +0200331 by Py_INCREF() and Py_DECREF(). */
332#ifdef Py_TRACE_REFS
Pablo Galindo36e33c32019-10-08 00:43:14 +0100333 if (op->_ob_next != NULL && _PyMem_IsPtrFreed(op->_ob_next)) {
Victor Stinner2b00db62019-04-11 11:33:27 +0200334 return 1;
335 }
Pablo Galindo36e33c32019-10-08 00:43:14 +0100336 if (op->_ob_prev != NULL && _PyMem_IsPtrFreed(op->_ob_prev)) {
337 return 1;
338 }
Victor Stinner82af0b62018-10-23 17:39:40 +0200339#endif
Victor Stinner2b00db62019-04-11 11:33:27 +0200340 return 0;
Victor Stinner82af0b62018-10-23 17:39:40 +0200341}
342
343
Barry Warsaw9bf16442001-01-23 16:24:35 +0000344/* For debugging convenience. See Misc/gdbinit for some useful gdb hooks */
Guido van Rossum38938152006-08-21 23:36:26 +0000345void
346_PyObject_Dump(PyObject* op)
Barry Warsaw9bf16442001-01-23 16:24:35 +0000347{
Victor Stinner82af0b62018-10-23 17:39:40 +0200348 if (_PyObject_IsFreed(op)) {
349 /* It seems like the object memory has been freed:
350 don't access it to prevent a segmentation fault. */
Victor Stinnerb39afb72019-09-17 23:36:28 +0200351 fprintf(stderr, "<object at %p is freed>\n", op);
Victor Stinner68762572019-10-07 18:42:01 +0200352 fflush(stderr);
Victor Stinner2cf5d322018-11-22 16:32:57 +0100353 return;
Victor Stinner82af0b62018-10-23 17:39:40 +0200354 }
355
Victor Stinner68762572019-10-07 18:42:01 +0200356 /* first, write fields which are the least likely to crash */
357 fprintf(stderr, "object address : %p\n", (void *)op);
Victor Stinner82af0b62018-10-23 17:39:40 +0200358 /* XXX(twouters) cast refcount to long until %zd is
359 universally available */
Victor Stinnera93c51e2020-02-07 00:38:59 +0100360 fprintf(stderr, "object refcount : %ld\n", (long)Py_REFCNT(op));
Victor Stinner68762572019-10-07 18:42:01 +0200361 fflush(stderr);
362
363 PyTypeObject *type = Py_TYPE(op);
364 fprintf(stderr, "object type : %p\n", type);
365 fprintf(stderr, "object type name: %s\n",
366 type==NULL ? "NULL" : type->tp_name);
367
368 /* the most dangerous part */
369 fprintf(stderr, "object repr : ");
370 fflush(stderr);
371
372 PyGILState_STATE gil = PyGILState_Ensure();
373 PyObject *error_type, *error_value, *error_traceback;
374 PyErr_Fetch(&error_type, &error_value, &error_traceback);
375
376 (void)PyObject_Print(op, stderr, 0);
377 fflush(stderr);
378
379 PyErr_Restore(error_type, error_value, error_traceback);
380 PyGILState_Release(gil);
381
382 fprintf(stderr, "\n");
Victor Stinner82af0b62018-10-23 17:39:40 +0200383 fflush(stderr);
Barry Warsaw9bf16442001-01-23 16:24:35 +0000384}
Barry Warsaw903138f2001-01-23 16:33:18 +0000385
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000386PyObject *
Fred Drake100814d2000-07-09 15:48:49 +0000387PyObject_Repr(PyObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000388{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000389 PyObject *res;
390 if (PyErr_CheckSignals())
391 return NULL;
Guido van Rossum9b00dfa1998-04-28 16:06:54 +0000392#ifdef USE_STACKCHECK
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000393 if (PyOS_CheckStack()) {
394 PyErr_SetString(PyExc_MemoryError, "stack overflow");
395 return NULL;
396 }
Guido van Rossum9b00dfa1998-04-28 16:06:54 +0000397#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000398 if (v == NULL)
399 return PyUnicode_FromString("<NULL>");
400 if (Py_TYPE(v)->tp_repr == NULL)
401 return PyUnicode_FromFormat("<%s object at %p>",
Victor Stinner58ac7002020-02-07 03:04:21 +0100402 Py_TYPE(v)->tp_name, v);
Victor Stinner33824f62013-08-26 14:05:19 +0200403
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100404 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinner33824f62013-08-26 14:05:19 +0200405#ifdef Py_DEBUG
406 /* PyObject_Repr() must not be called with an exception set,
Victor Stinnera8cb5152017-01-18 14:12:51 +0100407 because it can clear it (directly or indirectly) and so the
Martin Panter9955a372015-10-07 10:26:23 +0000408 caller loses its exception */
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100409 assert(!_PyErr_Occurred(tstate));
Victor Stinner33824f62013-08-26 14:05:19 +0200410#endif
411
Serhiy Storchaka1fb72d22017-12-03 22:12:11 +0200412 /* It is possible for a type to have a tp_repr representation that loops
413 infinitely. */
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100414 if (_Py_EnterRecursiveCall(tstate,
415 " while getting the repr of an object")) {
Serhiy Storchaka1fb72d22017-12-03 22:12:11 +0200416 return NULL;
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100417 }
Victor Stinner58ac7002020-02-07 03:04:21 +0100418 res = (*Py_TYPE(v)->tp_repr)(v);
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100419 _Py_LeaveRecursiveCall(tstate);
420
421 if (res == NULL) {
Victor Stinner0a54cf12011-12-01 03:22:44 +0100422 return NULL;
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100423 }
Victor Stinner0a54cf12011-12-01 03:22:44 +0100424 if (!PyUnicode_Check(res)) {
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100425 _PyErr_Format(tstate, PyExc_TypeError,
426 "__repr__ returned non-string (type %.200s)",
Victor Stinner58ac7002020-02-07 03:04:21 +0100427 Py_TYPE(res)->tp_name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000428 Py_DECREF(res);
429 return NULL;
430 }
Victor Stinnerdb88ae52011-12-01 02:15:00 +0100431#ifndef Py_DEBUG
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100432 if (PyUnicode_READY(res) < 0) {
Victor Stinnerdb88ae52011-12-01 02:15:00 +0100433 return NULL;
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100434 }
Victor Stinnerdb88ae52011-12-01 02:15:00 +0100435#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000436 return res;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000437}
438
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000439PyObject *
Guido van Rossum98297ee2007-11-06 21:34:58 +0000440PyObject_Str(PyObject *v)
Guido van Rossumc6004111993-11-05 10:22:19 +0000441{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000442 PyObject *res;
443 if (PyErr_CheckSignals())
444 return NULL;
Guido van Rossum98297ee2007-11-06 21:34:58 +0000445#ifdef USE_STACKCHECK
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000446 if (PyOS_CheckStack()) {
447 PyErr_SetString(PyExc_MemoryError, "stack overflow");
448 return NULL;
449 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000450#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000451 if (v == NULL)
452 return PyUnicode_FromString("<NULL>");
453 if (PyUnicode_CheckExact(v)) {
Victor Stinnerdb88ae52011-12-01 02:15:00 +0100454#ifndef Py_DEBUG
Victor Stinner4ead7c72011-11-20 19:48:36 +0100455 if (PyUnicode_READY(v) < 0)
456 return NULL;
Victor Stinnerdb88ae52011-12-01 02:15:00 +0100457#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000458 Py_INCREF(v);
459 return v;
460 }
461 if (Py_TYPE(v)->tp_str == NULL)
462 return PyObject_Repr(v);
Guido van Rossum4f288ab2001-05-01 16:53:37 +0000463
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100464 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinner33824f62013-08-26 14:05:19 +0200465#ifdef Py_DEBUG
466 /* PyObject_Str() must not be called with an exception set,
Victor Stinnera8cb5152017-01-18 14:12:51 +0100467 because it can clear it (directly or indirectly) and so the
Nick Coghland979e432014-02-09 10:43:21 +1000468 caller loses its exception */
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100469 assert(!_PyErr_Occurred(tstate));
Victor Stinner33824f62013-08-26 14:05:19 +0200470#endif
471
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000472 /* It is possible for a type to have a tp_str representation that loops
473 infinitely. */
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100474 if (_Py_EnterRecursiveCall(tstate, " while getting the str of an object")) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000475 return NULL;
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100476 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000477 res = (*Py_TYPE(v)->tp_str)(v);
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100478 _Py_LeaveRecursiveCall(tstate);
479
480 if (res == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000481 return NULL;
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100482 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000483 if (!PyUnicode_Check(res)) {
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100484 _PyErr_Format(tstate, PyExc_TypeError,
485 "__str__ returned non-string (type %.200s)",
486 Py_TYPE(res)->tp_name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000487 Py_DECREF(res);
488 return NULL;
489 }
Victor Stinnerdb88ae52011-12-01 02:15:00 +0100490#ifndef Py_DEBUG
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100491 if (PyUnicode_READY(res) < 0) {
Victor Stinner4ead7c72011-11-20 19:48:36 +0100492 return NULL;
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100493 }
Victor Stinnerdb88ae52011-12-01 02:15:00 +0100494#endif
Victor Stinner4ead7c72011-11-20 19:48:36 +0100495 assert(_PyUnicode_CheckConsistency(res, 1));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000496 return res;
Neil Schemenauercf52c072005-08-12 17:34:58 +0000497}
498
Georg Brandl559e5d72008-06-11 18:37:52 +0000499PyObject *
500PyObject_ASCII(PyObject *v)
501{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000502 PyObject *repr, *ascii, *res;
Georg Brandl559e5d72008-06-11 18:37:52 +0000503
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000504 repr = PyObject_Repr(v);
505 if (repr == NULL)
506 return NULL;
Georg Brandl559e5d72008-06-11 18:37:52 +0000507
Victor Stinneraf037572013-04-14 18:44:10 +0200508 if (PyUnicode_IS_ASCII(repr))
509 return repr;
510
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000511 /* repr is guaranteed to be a PyUnicode object by PyObject_Repr */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200512 ascii = _PyUnicode_AsASCIIString(repr, "backslashreplace");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000513 Py_DECREF(repr);
514 if (ascii == NULL)
515 return NULL;
Georg Brandl559e5d72008-06-11 18:37:52 +0000516
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000517 res = PyUnicode_DecodeASCII(
518 PyBytes_AS_STRING(ascii),
519 PyBytes_GET_SIZE(ascii),
520 NULL);
521
522 Py_DECREF(ascii);
523 return res;
Georg Brandl559e5d72008-06-11 18:37:52 +0000524}
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000525
Benjamin Petersonc15a0732008-08-26 16:46:47 +0000526PyObject *
527PyObject_Bytes(PyObject *v)
528{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000529 PyObject *result, *func;
Benjamin Petersonc15a0732008-08-26 16:46:47 +0000530
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000531 if (v == NULL)
532 return PyBytes_FromString("<NULL>");
Benjamin Petersonc15a0732008-08-26 16:46:47 +0000533
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000534 if (PyBytes_CheckExact(v)) {
535 Py_INCREF(v);
536 return v;
537 }
Benjamin Petersonc15a0732008-08-26 16:46:47 +0000538
Benjamin Petersonce798522012-01-22 11:24:29 -0500539 func = _PyObject_LookupSpecial(v, &PyId___bytes__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000540 if (func != NULL) {
Victor Stinnerf17c3de2016-12-06 18:46:19 +0100541 result = _PyObject_CallNoArg(func);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000542 Py_DECREF(func);
543 if (result == NULL)
Benjamin Peterson41ece392010-09-11 16:39:57 +0000544 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000545 if (!PyBytes_Check(result)) {
Benjamin Peterson41ece392010-09-11 16:39:57 +0000546 PyErr_Format(PyExc_TypeError,
547 "__bytes__ returned non-bytes (type %.200s)",
548 Py_TYPE(result)->tp_name);
549 Py_DECREF(result);
550 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000551 }
552 return result;
553 }
554 else if (PyErr_Occurred())
555 return NULL;
556 return PyBytes_FromObject(v);
Benjamin Petersonc15a0732008-08-26 16:46:47 +0000557}
558
Jeroen Demeyerbf17d412019-11-05 16:48:04 +0100559
560/*
561def _PyObject_FunctionStr(x):
562 try:
563 qualname = x.__qualname__
564 except AttributeError:
565 return str(x)
566 try:
567 mod = x.__module__
568 if mod is not None and mod != 'builtins':
569 return f"{x.__module__}.{qualname}()"
570 except AttributeError:
571 pass
572 return qualname
573*/
574PyObject *
575_PyObject_FunctionStr(PyObject *x)
576{
577 _Py_IDENTIFIER(__module__);
578 _Py_IDENTIFIER(__qualname__);
579 _Py_IDENTIFIER(builtins);
580 assert(!PyErr_Occurred());
581 PyObject *qualname;
582 int ret = _PyObject_LookupAttrId(x, &PyId___qualname__, &qualname);
583 if (qualname == NULL) {
584 if (ret < 0) {
585 return NULL;
586 }
587 return PyObject_Str(x);
588 }
589 PyObject *module;
590 PyObject *result = NULL;
591 ret = _PyObject_LookupAttrId(x, &PyId___module__, &module);
592 if (module != NULL && module != Py_None) {
593 PyObject *builtinsname = _PyUnicode_FromId(&PyId_builtins);
594 if (builtinsname == NULL) {
595 goto done;
596 }
597 ret = PyObject_RichCompareBool(module, builtinsname, Py_NE);
598 if (ret < 0) {
599 // error
600 goto done;
601 }
602 if (ret > 0) {
603 result = PyUnicode_FromFormat("%S.%S()", module, qualname);
604 goto done;
605 }
606 }
607 else if (ret < 0) {
608 goto done;
609 }
610 result = PyUnicode_FromFormat("%S()", qualname);
611done:
612 Py_DECREF(qualname);
613 Py_XDECREF(module);
614 return result;
615}
616
Mark Dickinsonc008a172009-02-01 13:59:22 +0000617/* For Python 3.0.1 and later, the old three-way comparison has been
618 completely removed in favour of rich comparisons. PyObject_Compare() and
619 PyObject_Cmp() are gone, and the builtin cmp function no longer exists.
Jeroen Demeyer530f5062019-05-31 04:13:39 +0200620 The old tp_compare slot has been renamed to tp_as_async, and should no
Mark Dickinsonc008a172009-02-01 13:59:22 +0000621 longer be used. Use tp_richcompare instead.
Guido van Rossum98297ee2007-11-06 21:34:58 +0000622
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000623 See (*) below for practical amendments.
624
Mark Dickinsonc008a172009-02-01 13:59:22 +0000625 tp_richcompare gets called with a first argument of the appropriate type
626 and a second object of an arbitrary type. We never do any kind of
627 coercion.
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000628
Mark Dickinsonc008a172009-02-01 13:59:22 +0000629 The tp_richcompare slot should return an object, as follows:
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000630
631 NULL if an exception occurred
632 NotImplemented if the requested comparison is not implemented
633 any other false value if the requested comparison is false
634 any other true value if the requested comparison is true
635
636 The PyObject_RichCompare[Bool]() wrappers raise TypeError when they get
637 NotImplemented.
638
639 (*) Practical amendments:
640
641 - If rich comparison returns NotImplemented, == and != are decided by
642 comparing the object pointer (i.e. falling back to the base object
643 implementation).
644
Guido van Rossuma4073002002-05-31 20:03:54 +0000645*/
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000646
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000647/* Map rich comparison operators to their swapped version, e.g. LT <--> GT */
Brett Cannona5ca2e72004-09-25 01:37:24 +0000648int _Py_SwappedOp[] = {Py_GT, Py_GE, Py_EQ, Py_NE, Py_LT, Py_LE};
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +0000649
Serhiy Storchaka2d06e842015-12-25 19:53:18 +0200650static const char * const opstrings[] = {"<", "<=", "==", "!=", ">", ">="};
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000651
652/* Perform a rich comparison, raising TypeError when the requested comparison
653 operator is not supported. */
Guido van Rossume797ec12001-01-17 15:24:28 +0000654static PyObject *
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100655do_richcompare(PyThreadState *tstate, PyObject *v, PyObject *w, int op)
Guido van Rossume797ec12001-01-17 15:24:28 +0000656{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000657 richcmpfunc f;
658 PyObject *res;
659 int checked_reverse_op = 0;
Guido van Rossume797ec12001-01-17 15:24:28 +0000660
Andy Lester55728702020-03-06 16:53:17 -0600661 if (!Py_IS_TYPE(v, Py_TYPE(w)) &&
Victor Stinner58ac7002020-02-07 03:04:21 +0100662 PyType_IsSubtype(Py_TYPE(w), Py_TYPE(v)) &&
663 (f = Py_TYPE(w)->tp_richcompare) != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000664 checked_reverse_op = 1;
665 res = (*f)(w, v, _Py_SwappedOp[op]);
666 if (res != Py_NotImplemented)
667 return res;
668 Py_DECREF(res);
669 }
Victor Stinner58ac7002020-02-07 03:04:21 +0100670 if ((f = Py_TYPE(v)->tp_richcompare) != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000671 res = (*f)(v, w, op);
672 if (res != Py_NotImplemented)
673 return res;
674 Py_DECREF(res);
675 }
Victor Stinner58ac7002020-02-07 03:04:21 +0100676 if (!checked_reverse_op && (f = Py_TYPE(w)->tp_richcompare) != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000677 res = (*f)(w, v, _Py_SwappedOp[op]);
678 if (res != Py_NotImplemented)
679 return res;
680 Py_DECREF(res);
681 }
682 /* If neither object implements it, provide a sensible default
683 for == and !=, but raise an exception for ordering. */
684 switch (op) {
685 case Py_EQ:
686 res = (v == w) ? Py_True : Py_False;
687 break;
688 case Py_NE:
689 res = (v != w) ? Py_True : Py_False;
690 break;
691 default:
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100692 _PyErr_Format(tstate, PyExc_TypeError,
693 "'%s' not supported between instances of '%.100s' and '%.100s'",
694 opstrings[op],
Victor Stinner58ac7002020-02-07 03:04:21 +0100695 Py_TYPE(v)->tp_name,
696 Py_TYPE(w)->tp_name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000697 return NULL;
698 }
699 Py_INCREF(res);
700 return res;
Guido van Rossume797ec12001-01-17 15:24:28 +0000701}
702
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000703/* Perform a rich comparison with object result. This wraps do_richcompare()
704 with a check for NULL arguments and a recursion check. */
Guido van Rossume797ec12001-01-17 15:24:28 +0000705
Guido van Rossume797ec12001-01-17 15:24:28 +0000706PyObject *
707PyObject_RichCompare(PyObject *v, PyObject *w, int op)
708{
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100709 PyThreadState *tstate = _PyThreadState_GET();
Guido van Rossume797ec12001-01-17 15:24:28 +0000710
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000711 assert(Py_LT <= op && op <= Py_GE);
712 if (v == NULL || w == NULL) {
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100713 if (!_PyErr_Occurred(tstate)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000714 PyErr_BadInternalCall();
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100715 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000716 return NULL;
717 }
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100718 if (_Py_EnterRecursiveCall(tstate, " in comparison")) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000719 return NULL;
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100720 }
721 PyObject *res = do_richcompare(tstate, v, w, op);
722 _Py_LeaveRecursiveCall(tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000723 return res;
Guido van Rossume797ec12001-01-17 15:24:28 +0000724}
725
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000726/* Perform a rich comparison with integer result. This wraps
727 PyObject_RichCompare(), returning -1 for error, 0 for false, 1 for true. */
Guido van Rossume797ec12001-01-17 15:24:28 +0000728int
729PyObject_RichCompareBool(PyObject *v, PyObject *w, int op)
730{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000731 PyObject *res;
732 int ok;
Guido van Rossume797ec12001-01-17 15:24:28 +0000733
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000734 /* Quick result when objects are the same.
735 Guarantees that identity implies equality. */
736 if (v == w) {
737 if (op == Py_EQ)
738 return 1;
739 else if (op == Py_NE)
740 return 0;
741 }
Mark Dickinson4a1f5932008-11-12 23:23:36 +0000742
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000743 res = PyObject_RichCompare(v, w, op);
744 if (res == NULL)
745 return -1;
746 if (PyBool_Check(res))
747 ok = (res == Py_True);
748 else
749 ok = PyObject_IsTrue(res);
750 Py_DECREF(res);
751 return ok;
Guido van Rossume797ec12001-01-17 15:24:28 +0000752}
Fred Drake13634cf2000-06-29 19:17:04 +0000753
Antoine Pitrouce4a9da2011-11-21 20:46:33 +0100754Py_hash_t
Nick Coghland1abd252008-07-15 15:46:38 +0000755PyObject_HashNotImplemented(PyObject *v)
756{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000757 PyErr_Format(PyExc_TypeError, "unhashable type: '%.200s'",
758 Py_TYPE(v)->tp_name);
759 return -1;
Nick Coghland1abd252008-07-15 15:46:38 +0000760}
Fred Drake13634cf2000-06-29 19:17:04 +0000761
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000762Py_hash_t
Fred Drake100814d2000-07-09 15:48:49 +0000763PyObject_Hash(PyObject *v)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000764{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000765 PyTypeObject *tp = Py_TYPE(v);
766 if (tp->tp_hash != NULL)
767 return (*tp->tp_hash)(v);
768 /* To keep to the general practice that inheriting
769 * solely from object in C code should work without
770 * an explicit call to PyType_Ready, we implicitly call
771 * PyType_Ready here and then check the tp_hash slot again
772 */
773 if (tp->tp_dict == NULL) {
774 if (PyType_Ready(tp) < 0)
775 return -1;
776 if (tp->tp_hash != NULL)
777 return (*tp->tp_hash)(v);
778 }
779 /* Otherwise, the object can't be hashed */
780 return PyObject_HashNotImplemented(v);
Guido van Rossum9bfef441993-03-29 10:43:31 +0000781}
782
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000783PyObject *
Jeremy Hyltonaf68c872005-12-10 18:50:16 +0000784PyObject_GetAttrString(PyObject *v, const char *name)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000785{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000786 PyObject *w, *res;
Guido van Rossumd8eb1b31996-08-09 20:52:03 +0000787
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000788 if (Py_TYPE(v)->tp_getattr != NULL)
789 return (*Py_TYPE(v)->tp_getattr)(v, (char*)name);
INADA Naoki3e8d6cb2017-02-21 23:57:25 +0900790 w = PyUnicode_FromString(name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000791 if (w == NULL)
792 return NULL;
793 res = PyObject_GetAttr(v, w);
Victor Stinner59af08f2012-03-22 02:09:08 +0100794 Py_DECREF(w);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000795 return res;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000796}
797
798int
Jeremy Hyltonaf68c872005-12-10 18:50:16 +0000799PyObject_HasAttrString(PyObject *v, const char *name)
Guido van Rossumed18fdc1993-07-11 19:55:34 +0000800{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000801 PyObject *res = PyObject_GetAttrString(v, name);
802 if (res != NULL) {
803 Py_DECREF(res);
804 return 1;
805 }
806 PyErr_Clear();
807 return 0;
Guido van Rossumed18fdc1993-07-11 19:55:34 +0000808}
809
810int
Jeremy Hyltonaf68c872005-12-10 18:50:16 +0000811PyObject_SetAttrString(PyObject *v, const char *name, PyObject *w)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000812{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000813 PyObject *s;
814 int res;
Guido van Rossumd8eb1b31996-08-09 20:52:03 +0000815
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000816 if (Py_TYPE(v)->tp_setattr != NULL)
817 return (*Py_TYPE(v)->tp_setattr)(v, (char*)name, w);
818 s = PyUnicode_InternFromString(name);
819 if (s == NULL)
820 return -1;
821 res = PyObject_SetAttr(v, s, w);
822 Py_XDECREF(s);
823 return res;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000824}
825
Benjamin Petersonbfebb7b2011-12-15 15:34:02 -0500826int
827_PyObject_IsAbstract(PyObject *obj)
828{
829 int res;
830 PyObject* isabstract;
Benjamin Petersonbfebb7b2011-12-15 15:34:02 -0500831
832 if (obj == NULL)
833 return 0;
834
Serhiy Storchakaf320be72018-01-25 10:49:40 +0200835 res = _PyObject_LookupAttrId(obj, &PyId___isabstractmethod__, &isabstract);
836 if (res > 0) {
837 res = PyObject_IsTrue(isabstract);
838 Py_DECREF(isabstract);
Benjamin Petersonbfebb7b2011-12-15 15:34:02 -0500839 }
Benjamin Petersonbfebb7b2011-12-15 15:34:02 -0500840 return res;
841}
842
Guido van Rossum98ff96a1997-05-20 18:34:44 +0000843PyObject *
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200844_PyObject_GetAttrId(PyObject *v, _Py_Identifier *name)
845{
846 PyObject *result;
Martin v. Löwisd10759f2011-11-07 13:00:05 +0100847 PyObject *oname = _PyUnicode_FromId(name); /* borrowed */
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200848 if (!oname)
849 return NULL;
850 result = PyObject_GetAttr(v, oname);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200851 return result;
852}
853
854int
855_PyObject_HasAttrId(PyObject *v, _Py_Identifier *name)
856{
857 int result;
Martin v. Löwisd10759f2011-11-07 13:00:05 +0100858 PyObject *oname = _PyUnicode_FromId(name); /* borrowed */
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200859 if (!oname)
860 return -1;
861 result = PyObject_HasAttr(v, oname);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200862 return result;
863}
864
865int
866_PyObject_SetAttrId(PyObject *v, _Py_Identifier *name, PyObject *w)
867{
868 int result;
Martin v. Löwisd10759f2011-11-07 13:00:05 +0100869 PyObject *oname = _PyUnicode_FromId(name); /* borrowed */
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200870 if (!oname)
871 return -1;
872 result = PyObject_SetAttr(v, oname, w);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200873 return result;
874}
875
876PyObject *
Fred Drake100814d2000-07-09 15:48:49 +0000877PyObject_GetAttr(PyObject *v, PyObject *name)
Guido van Rossum98ff96a1997-05-20 18:34:44 +0000878{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000879 PyTypeObject *tp = Py_TYPE(v);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000880
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000881 if (!PyUnicode_Check(name)) {
882 PyErr_Format(PyExc_TypeError,
883 "attribute name must be string, not '%.200s'",
Victor Stinner58ac7002020-02-07 03:04:21 +0100884 Py_TYPE(name)->tp_name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000885 return NULL;
886 }
887 if (tp->tp_getattro != NULL)
888 return (*tp->tp_getattro)(v, name);
889 if (tp->tp_getattr != NULL) {
Serhiy Storchaka2a404b62017-01-22 23:07:07 +0200890 const char *name_str = PyUnicode_AsUTF8(name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000891 if (name_str == NULL)
892 return NULL;
Serhiy Storchaka2a404b62017-01-22 23:07:07 +0200893 return (*tp->tp_getattr)(v, (char *)name_str);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000894 }
895 PyErr_Format(PyExc_AttributeError,
896 "'%.50s' object has no attribute '%U'",
897 tp->tp_name, name);
898 return NULL;
Guido van Rossum98ff96a1997-05-20 18:34:44 +0000899}
900
Serhiy Storchakaf320be72018-01-25 10:49:40 +0200901int
902_PyObject_LookupAttr(PyObject *v, PyObject *name, PyObject **result)
INADA Naoki378edee2018-01-16 20:52:41 +0900903{
904 PyTypeObject *tp = Py_TYPE(v);
INADA Naoki378edee2018-01-16 20:52:41 +0900905
906 if (!PyUnicode_Check(name)) {
907 PyErr_Format(PyExc_TypeError,
908 "attribute name must be string, not '%.200s'",
Victor Stinner58ac7002020-02-07 03:04:21 +0100909 Py_TYPE(name)->tp_name);
Serhiy Storchakaf320be72018-01-25 10:49:40 +0200910 *result = NULL;
911 return -1;
INADA Naoki378edee2018-01-16 20:52:41 +0900912 }
913
914 if (tp->tp_getattro == PyObject_GenericGetAttr) {
Serhiy Storchakaf320be72018-01-25 10:49:40 +0200915 *result = _PyObject_GenericGetAttrWithDict(v, name, NULL, 1);
916 if (*result != NULL) {
917 return 1;
918 }
919 if (PyErr_Occurred()) {
920 return -1;
921 }
922 return 0;
INADA Naoki378edee2018-01-16 20:52:41 +0900923 }
924 if (tp->tp_getattro != NULL) {
Serhiy Storchakaf320be72018-01-25 10:49:40 +0200925 *result = (*tp->tp_getattro)(v, name);
INADA Naoki378edee2018-01-16 20:52:41 +0900926 }
927 else if (tp->tp_getattr != NULL) {
928 const char *name_str = PyUnicode_AsUTF8(name);
Serhiy Storchakaf320be72018-01-25 10:49:40 +0200929 if (name_str == NULL) {
930 *result = NULL;
931 return -1;
932 }
933 *result = (*tp->tp_getattr)(v, (char *)name_str);
INADA Naoki378edee2018-01-16 20:52:41 +0900934 }
INADA Naokie76daeb2018-01-26 16:22:51 +0900935 else {
936 *result = NULL;
937 return 0;
938 }
939
Serhiy Storchakaf320be72018-01-25 10:49:40 +0200940 if (*result != NULL) {
941 return 1;
INADA Naoki378edee2018-01-16 20:52:41 +0900942 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +0200943 if (!PyErr_ExceptionMatches(PyExc_AttributeError)) {
944 return -1;
945 }
946 PyErr_Clear();
947 return 0;
948}
949
950int
951_PyObject_LookupAttrId(PyObject *v, _Py_Identifier *name, PyObject **result)
952{
953 PyObject *oname = _PyUnicode_FromId(name); /* borrowed */
954 if (!oname) {
955 *result = NULL;
956 return -1;
957 }
958 return _PyObject_LookupAttr(v, oname, result);
INADA Naoki378edee2018-01-16 20:52:41 +0900959}
960
Guido van Rossum98ff96a1997-05-20 18:34:44 +0000961int
Fred Drake100814d2000-07-09 15:48:49 +0000962PyObject_HasAttr(PyObject *v, PyObject *name)
Guido van Rossum98ff96a1997-05-20 18:34:44 +0000963{
Serhiy Storchakaf320be72018-01-25 10:49:40 +0200964 PyObject *res;
965 if (_PyObject_LookupAttr(v, name, &res) < 0) {
966 PyErr_Clear();
967 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000968 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +0200969 if (res == NULL) {
970 return 0;
971 }
972 Py_DECREF(res);
973 return 1;
Guido van Rossum98ff96a1997-05-20 18:34:44 +0000974}
975
976int
Fred Drake100814d2000-07-09 15:48:49 +0000977PyObject_SetAttr(PyObject *v, PyObject *name, PyObject *value)
Guido van Rossum98ff96a1997-05-20 18:34:44 +0000978{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000979 PyTypeObject *tp = Py_TYPE(v);
980 int err;
Marc-André Lemburge44e5072000-09-18 16:20:57 +0000981
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000982 if (!PyUnicode_Check(name)) {
983 PyErr_Format(PyExc_TypeError,
984 "attribute name must be string, not '%.200s'",
Victor Stinner58ac7002020-02-07 03:04:21 +0100985 Py_TYPE(name)->tp_name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000986 return -1;
987 }
988 Py_INCREF(name);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000989
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000990 PyUnicode_InternInPlace(&name);
991 if (tp->tp_setattro != NULL) {
992 err = (*tp->tp_setattro)(v, name, value);
993 Py_DECREF(name);
994 return err;
995 }
996 if (tp->tp_setattr != NULL) {
Serhiy Storchaka2a404b62017-01-22 23:07:07 +0200997 const char *name_str = PyUnicode_AsUTF8(name);
Zackery Spytze0dcb852019-04-28 06:58:52 -0600998 if (name_str == NULL) {
999 Py_DECREF(name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001000 return -1;
Zackery Spytze0dcb852019-04-28 06:58:52 -06001001 }
Serhiy Storchaka2a404b62017-01-22 23:07:07 +02001002 err = (*tp->tp_setattr)(v, (char *)name_str, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001003 Py_DECREF(name);
1004 return err;
1005 }
1006 Py_DECREF(name);
Victor Stinnera93c51e2020-02-07 00:38:59 +01001007 _PyObject_ASSERT(name, Py_REFCNT(name) >= 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001008 if (tp->tp_getattr == NULL && tp->tp_getattro == NULL)
1009 PyErr_Format(PyExc_TypeError,
1010 "'%.100s' object has no attributes "
1011 "(%s .%U)",
1012 tp->tp_name,
1013 value==NULL ? "del" : "assign to",
1014 name);
1015 else
1016 PyErr_Format(PyExc_TypeError,
1017 "'%.100s' object has only read-only attributes "
1018 "(%s .%U)",
1019 tp->tp_name,
1020 value==NULL ? "del" : "assign to",
1021 name);
1022 return -1;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001023}
1024
1025/* Helper to get a pointer to an object's __dict__ slot, if any */
1026
1027PyObject **
1028_PyObject_GetDictPtr(PyObject *obj)
1029{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001030 Py_ssize_t dictoffset;
1031 PyTypeObject *tp = Py_TYPE(obj);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001032
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001033 dictoffset = tp->tp_dictoffset;
1034 if (dictoffset == 0)
1035 return NULL;
1036 if (dictoffset < 0) {
Victor Stinnerc65b3202020-02-07 11:18:33 +01001037 Py_ssize_t tsize = Py_SIZE(obj);
1038 if (tsize < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001039 tsize = -tsize;
Victor Stinnerc65b3202020-02-07 11:18:33 +01001040 }
1041 size_t size = _PyObject_VAR_SIZE(tp, tsize);
Guido van Rossum2eb0b872002-03-01 22:24:49 +00001042
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001043 dictoffset += (long)size;
Victor Stinner24702042018-10-26 17:16:37 +02001044 _PyObject_ASSERT(obj, dictoffset > 0);
1045 _PyObject_ASSERT(obj, dictoffset % SIZEOF_VOID_P == 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001046 }
1047 return (PyObject **) ((char *)obj + dictoffset);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001048}
1049
Tim Peters6d6c1a32001-08-02 04:15:00 +00001050PyObject *
Raymond Hettinger1da1dbf2003-03-17 19:46:11 +00001051PyObject_SelfIter(PyObject *obj)
Raymond Hettinger01538262003-03-17 08:24:35 +00001052{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001053 Py_INCREF(obj);
1054 return obj;
Raymond Hettinger01538262003-03-17 08:24:35 +00001055}
1056
Amaury Forgeot d'Arcf343e012009-01-12 23:58:21 +00001057/* Helper used when the __next__ method is removed from a type:
1058 tp_iternext is never NULL and can be safely called without checking
1059 on every iteration.
1060 */
1061
1062PyObject *
1063_PyObject_NextNotImplemented(PyObject *self)
1064{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001065 PyErr_Format(PyExc_TypeError,
1066 "'%.200s' object is not iterable",
1067 Py_TYPE(self)->tp_name);
1068 return NULL;
Amaury Forgeot d'Arcf343e012009-01-12 23:58:21 +00001069}
1070
Yury Selivanovf2392132016-12-13 19:03:51 -05001071
1072/* Specialized version of _PyObject_GenericGetAttrWithDict
1073 specifically for the LOAD_METHOD opcode.
1074
1075 Return 1 if a method is found, 0 if it's a regular attribute
1076 from __dict__ or something returned by using a descriptor
1077 protocol.
1078
1079 `method` will point to the resolved attribute or NULL. In the
1080 latter case, an error will be set.
1081*/
1082int
1083_PyObject_GetMethod(PyObject *obj, PyObject *name, PyObject **method)
1084{
1085 PyTypeObject *tp = Py_TYPE(obj);
1086 PyObject *descr;
1087 descrgetfunc f = NULL;
1088 PyObject **dictptr, *dict;
1089 PyObject *attr;
1090 int meth_found = 0;
1091
1092 assert(*method == NULL);
1093
1094 if (Py_TYPE(obj)->tp_getattro != PyObject_GenericGetAttr
1095 || !PyUnicode_Check(name)) {
1096 *method = PyObject_GetAttr(obj, name);
1097 return 0;
1098 }
1099
1100 if (tp->tp_dict == NULL && PyType_Ready(tp) < 0)
1101 return 0;
1102
1103 descr = _PyType_Lookup(tp, name);
1104 if (descr != NULL) {
1105 Py_INCREF(descr);
Jeroen Demeyereb65e242019-05-28 14:42:53 +02001106 if (PyType_HasFeature(Py_TYPE(descr), Py_TPFLAGS_METHOD_DESCRIPTOR)) {
Yury Selivanovf2392132016-12-13 19:03:51 -05001107 meth_found = 1;
1108 } else {
Victor Stinner58ac7002020-02-07 03:04:21 +01001109 f = Py_TYPE(descr)->tp_descr_get;
Yury Selivanovf2392132016-12-13 19:03:51 -05001110 if (f != NULL && PyDescr_IsData(descr)) {
Victor Stinner58ac7002020-02-07 03:04:21 +01001111 *method = f(descr, obj, (PyObject *)Py_TYPE(obj));
Yury Selivanovf2392132016-12-13 19:03:51 -05001112 Py_DECREF(descr);
1113 return 0;
1114 }
1115 }
1116 }
1117
1118 dictptr = _PyObject_GetDictPtr(obj);
1119 if (dictptr != NULL && (dict = *dictptr) != NULL) {
1120 Py_INCREF(dict);
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02001121 attr = PyDict_GetItemWithError(dict, name);
Yury Selivanovf2392132016-12-13 19:03:51 -05001122 if (attr != NULL) {
1123 Py_INCREF(attr);
1124 *method = attr;
1125 Py_DECREF(dict);
1126 Py_XDECREF(descr);
1127 return 0;
1128 }
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02001129 else {
1130 Py_DECREF(dict);
1131 if (PyErr_Occurred()) {
1132 Py_XDECREF(descr);
1133 return 0;
1134 }
1135 }
Yury Selivanovf2392132016-12-13 19:03:51 -05001136 }
1137
1138 if (meth_found) {
1139 *method = descr;
1140 return 1;
1141 }
1142
1143 if (f != NULL) {
1144 *method = f(descr, obj, (PyObject *)Py_TYPE(obj));
1145 Py_DECREF(descr);
1146 return 0;
1147 }
1148
1149 if (descr != NULL) {
1150 *method = descr;
1151 return 0;
1152 }
1153
1154 PyErr_Format(PyExc_AttributeError,
1155 "'%.50s' object has no attribute '%U'",
1156 tp->tp_name, name);
1157 return 0;
1158}
1159
1160/* Generic GetAttr functions - put these in your tp_[gs]etattro slot. */
Michael W. Hudson1593f502004-09-14 17:09:47 +00001161
Raymond Hettinger01538262003-03-17 08:24:35 +00001162PyObject *
INADA Naoki378edee2018-01-16 20:52:41 +09001163_PyObject_GenericGetAttrWithDict(PyObject *obj, PyObject *name,
1164 PyObject *dict, int suppress)
Tim Peters6d6c1a32001-08-02 04:15:00 +00001165{
Yury Selivanovf2392132016-12-13 19:03:51 -05001166 /* Make sure the logic of _PyObject_GetMethod is in sync with
1167 this method.
INADA Naoki378edee2018-01-16 20:52:41 +09001168
1169 When suppress=1, this function suppress AttributeError.
Yury Selivanovf2392132016-12-13 19:03:51 -05001170 */
1171
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001172 PyTypeObject *tp = Py_TYPE(obj);
1173 PyObject *descr = NULL;
1174 PyObject *res = NULL;
1175 descrgetfunc f;
1176 Py_ssize_t dictoffset;
1177 PyObject **dictptr;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001178
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001179 if (!PyUnicode_Check(name)){
1180 PyErr_Format(PyExc_TypeError,
1181 "attribute name must be string, not '%.200s'",
Victor Stinner58ac7002020-02-07 03:04:21 +01001182 Py_TYPE(name)->tp_name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001183 return NULL;
1184 }
Serhiy Storchaka55c861f2016-04-17 20:31:51 +03001185 Py_INCREF(name);
Guido van Rossumebca9fc2001-12-04 15:54:53 +00001186
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001187 if (tp->tp_dict == NULL) {
1188 if (PyType_Ready(tp) < 0)
1189 goto done;
1190 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001191
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001192 descr = _PyType_Lookup(tp, name);
Michael W. Hudsonb2c7de42003-08-15 13:07:47 +00001193
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001194 f = NULL;
1195 if (descr != NULL) {
Serhiy Storchaka55c861f2016-04-17 20:31:51 +03001196 Py_INCREF(descr);
Victor Stinner58ac7002020-02-07 03:04:21 +01001197 f = Py_TYPE(descr)->tp_descr_get;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001198 if (f != NULL && PyDescr_IsData(descr)) {
Victor Stinner58ac7002020-02-07 03:04:21 +01001199 res = f(descr, obj, (PyObject *)Py_TYPE(obj));
INADA Naoki378edee2018-01-16 20:52:41 +09001200 if (res == NULL && suppress &&
1201 PyErr_ExceptionMatches(PyExc_AttributeError)) {
1202 PyErr_Clear();
1203 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001204 goto done;
1205 }
1206 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001207
Antoine Pitrou1a9a9d52010-08-28 18:17:03 +00001208 if (dict == NULL) {
1209 /* Inline _PyObject_GetDictPtr */
1210 dictoffset = tp->tp_dictoffset;
1211 if (dictoffset != 0) {
1212 if (dictoffset < 0) {
Victor Stinnerc65b3202020-02-07 11:18:33 +01001213 Py_ssize_t tsize = Py_SIZE(obj);
1214 if (tsize < 0) {
Antoine Pitrou1a9a9d52010-08-28 18:17:03 +00001215 tsize = -tsize;
Victor Stinnerc65b3202020-02-07 11:18:33 +01001216 }
1217 size_t size = _PyObject_VAR_SIZE(tp, tsize);
Victor Stinner24702042018-10-26 17:16:37 +02001218 _PyObject_ASSERT(obj, size <= PY_SSIZE_T_MAX);
Guido van Rossumc66ff442002-08-19 16:50:48 +00001219
Serhiy Storchaka55c861f2016-04-17 20:31:51 +03001220 dictoffset += (Py_ssize_t)size;
Victor Stinner24702042018-10-26 17:16:37 +02001221 _PyObject_ASSERT(obj, dictoffset > 0);
1222 _PyObject_ASSERT(obj, dictoffset % SIZEOF_VOID_P == 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001223 }
Antoine Pitrou1a9a9d52010-08-28 18:17:03 +00001224 dictptr = (PyObject **) ((char *)obj + dictoffset);
1225 dict = *dictptr;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001226 }
1227 }
Antoine Pitrou1a9a9d52010-08-28 18:17:03 +00001228 if (dict != NULL) {
1229 Py_INCREF(dict);
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02001230 res = PyDict_GetItemWithError(dict, name);
Antoine Pitrou1a9a9d52010-08-28 18:17:03 +00001231 if (res != NULL) {
1232 Py_INCREF(res);
Antoine Pitrou1a9a9d52010-08-28 18:17:03 +00001233 Py_DECREF(dict);
1234 goto done;
1235 }
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02001236 else {
1237 Py_DECREF(dict);
1238 if (PyErr_Occurred()) {
1239 if (suppress && PyErr_ExceptionMatches(PyExc_AttributeError)) {
1240 PyErr_Clear();
1241 }
1242 else {
1243 goto done;
1244 }
1245 }
1246 }
Antoine Pitrou1a9a9d52010-08-28 18:17:03 +00001247 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001248
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001249 if (f != NULL) {
1250 res = f(descr, obj, (PyObject *)Py_TYPE(obj));
INADA Naoki378edee2018-01-16 20:52:41 +09001251 if (res == NULL && suppress &&
1252 PyErr_ExceptionMatches(PyExc_AttributeError)) {
1253 PyErr_Clear();
1254 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001255 goto done;
1256 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001257
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001258 if (descr != NULL) {
1259 res = descr;
Victor Stinner2d01dc02012-03-09 00:44:13 +01001260 descr = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001261 goto done;
1262 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001263
INADA Naoki378edee2018-01-16 20:52:41 +09001264 if (!suppress) {
1265 PyErr_Format(PyExc_AttributeError,
1266 "'%.50s' object has no attribute '%U'",
1267 tp->tp_name, name);
1268 }
Guido van Rossumebca9fc2001-12-04 15:54:53 +00001269 done:
Victor Stinner2d01dc02012-03-09 00:44:13 +01001270 Py_XDECREF(descr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001271 Py_DECREF(name);
1272 return res;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001273}
1274
Antoine Pitrou1a9a9d52010-08-28 18:17:03 +00001275PyObject *
1276PyObject_GenericGetAttr(PyObject *obj, PyObject *name)
1277{
INADA Naoki378edee2018-01-16 20:52:41 +09001278 return _PyObject_GenericGetAttrWithDict(obj, name, NULL, 0);
Antoine Pitrou1a9a9d52010-08-28 18:17:03 +00001279}
1280
Tim Peters6d6c1a32001-08-02 04:15:00 +00001281int
Antoine Pitrou1a9a9d52010-08-28 18:17:03 +00001282_PyObject_GenericSetAttrWithDict(PyObject *obj, PyObject *name,
1283 PyObject *value, PyObject *dict)
Tim Peters6d6c1a32001-08-02 04:15:00 +00001284{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001285 PyTypeObject *tp = Py_TYPE(obj);
1286 PyObject *descr;
1287 descrsetfunc f;
1288 PyObject **dictptr;
1289 int res = -1;
Guido van Rossumebca9fc2001-12-04 15:54:53 +00001290
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001291 if (!PyUnicode_Check(name)){
1292 PyErr_Format(PyExc_TypeError,
1293 "attribute name must be string, not '%.200s'",
Victor Stinner58ac7002020-02-07 03:04:21 +01001294 Py_TYPE(name)->tp_name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001295 return -1;
1296 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001297
Benjamin Peterson74529ad2012-03-09 07:25:32 -08001298 if (tp->tp_dict == NULL && PyType_Ready(tp) < 0)
1299 return -1;
1300
1301 Py_INCREF(name);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001302
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001303 descr = _PyType_Lookup(tp, name);
Victor Stinner2d01dc02012-03-09 00:44:13 +01001304
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001305 if (descr != NULL) {
Serhiy Storchaka55c861f2016-04-17 20:31:51 +03001306 Py_INCREF(descr);
Victor Stinner58ac7002020-02-07 03:04:21 +01001307 f = Py_TYPE(descr)->tp_descr_set;
Serhiy Storchaka55c861f2016-04-17 20:31:51 +03001308 if (f != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001309 res = f(descr, obj, value);
1310 goto done;
1311 }
1312 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001313
Steve Dowerb82e17e2019-05-23 08:45:22 -07001314 /* XXX [Steve Dower] These are really noisy - worth it? */
1315 /*if (PyType_Check(obj) || PyModule_Check(obj)) {
1316 if (value && PySys_Audit("object.__setattr__", "OOO", obj, name, value) < 0)
1317 return -1;
1318 if (!value && PySys_Audit("object.__delattr__", "OO", obj, name) < 0)
1319 return -1;
1320 }*/
1321
Antoine Pitrou1a9a9d52010-08-28 18:17:03 +00001322 if (dict == NULL) {
1323 dictptr = _PyObject_GetDictPtr(obj);
Serhiy Storchaka55c861f2016-04-17 20:31:51 +03001324 if (dictptr == NULL) {
1325 if (descr == NULL) {
1326 PyErr_Format(PyExc_AttributeError,
1327 "'%.100s' object has no attribute '%U'",
1328 tp->tp_name, name);
1329 }
1330 else {
1331 PyErr_Format(PyExc_AttributeError,
1332 "'%.50s' object attribute '%U' is read-only",
1333 tp->tp_name, name);
1334 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001335 goto done;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001336 }
Serhiy Storchaka55c861f2016-04-17 20:31:51 +03001337 res = _PyObjectDict_SetItem(tp, dictptr, name, value);
Antoine Pitrou1a9a9d52010-08-28 18:17:03 +00001338 }
Serhiy Storchaka55c861f2016-04-17 20:31:51 +03001339 else {
Antoine Pitrou1a9a9d52010-08-28 18:17:03 +00001340 Py_INCREF(dict);
1341 if (value == NULL)
1342 res = PyDict_DelItem(dict, name);
1343 else
1344 res = PyDict_SetItem(dict, name, value);
Benjamin Peterson74529ad2012-03-09 07:25:32 -08001345 Py_DECREF(dict);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001346 }
Serhiy Storchaka55c861f2016-04-17 20:31:51 +03001347 if (res < 0 && PyErr_ExceptionMatches(PyExc_KeyError))
1348 PyErr_SetObject(PyExc_AttributeError, name);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001349
Guido van Rossumebca9fc2001-12-04 15:54:53 +00001350 done:
Victor Stinner2d01dc02012-03-09 00:44:13 +01001351 Py_XDECREF(descr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001352 Py_DECREF(name);
1353 return res;
Guido van Rossum98ff96a1997-05-20 18:34:44 +00001354}
1355
Antoine Pitrou1a9a9d52010-08-28 18:17:03 +00001356int
1357PyObject_GenericSetAttr(PyObject *obj, PyObject *name, PyObject *value)
1358{
1359 return _PyObject_GenericSetAttrWithDict(obj, name, value, NULL);
1360}
1361
Benjamin Peterson8eb12692012-02-19 19:59:10 -05001362int
1363PyObject_GenericSetDict(PyObject *obj, PyObject *value, void *context)
1364{
Serhiy Storchaka576f1322016-01-05 21:27:54 +02001365 PyObject **dictptr = _PyObject_GetDictPtr(obj);
Benjamin Peterson8eb12692012-02-19 19:59:10 -05001366 if (dictptr == NULL) {
1367 PyErr_SetString(PyExc_AttributeError,
1368 "This object has no __dict__");
1369 return -1;
1370 }
1371 if (value == NULL) {
1372 PyErr_SetString(PyExc_TypeError, "cannot delete __dict__");
1373 return -1;
1374 }
1375 if (!PyDict_Check(value)) {
1376 PyErr_Format(PyExc_TypeError,
1377 "__dict__ must be set to a dictionary, "
1378 "not a '%.200s'", Py_TYPE(value)->tp_name);
1379 return -1;
1380 }
Serhiy Storchaka576f1322016-01-05 21:27:54 +02001381 Py_INCREF(value);
Serhiy Storchakaec397562016-04-06 09:50:03 +03001382 Py_XSETREF(*dictptr, value);
Benjamin Peterson8eb12692012-02-19 19:59:10 -05001383 return 0;
1384}
1385
Antoine Pitrou1a9a9d52010-08-28 18:17:03 +00001386
Guido van Rossum6ac258d1993-05-12 08:24:20 +00001387/* Test a value used as condition, e.g., in a for or if statement.
1388 Return -1 if an error occurred */
1389
1390int
Fred Drake100814d2000-07-09 15:48:49 +00001391PyObject_IsTrue(PyObject *v)
Guido van Rossum6ac258d1993-05-12 08:24:20 +00001392{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001393 Py_ssize_t res;
1394 if (v == Py_True)
1395 return 1;
1396 if (v == Py_False)
1397 return 0;
1398 if (v == Py_None)
1399 return 0;
Victor Stinner58ac7002020-02-07 03:04:21 +01001400 else if (Py_TYPE(v)->tp_as_number != NULL &&
1401 Py_TYPE(v)->tp_as_number->nb_bool != NULL)
1402 res = (*Py_TYPE(v)->tp_as_number->nb_bool)(v);
1403 else if (Py_TYPE(v)->tp_as_mapping != NULL &&
1404 Py_TYPE(v)->tp_as_mapping->mp_length != NULL)
1405 res = (*Py_TYPE(v)->tp_as_mapping->mp_length)(v);
1406 else if (Py_TYPE(v)->tp_as_sequence != NULL &&
1407 Py_TYPE(v)->tp_as_sequence->sq_length != NULL)
1408 res = (*Py_TYPE(v)->tp_as_sequence->sq_length)(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001409 else
1410 return 1;
1411 /* if it is negative, it should be either -1 or -2 */
1412 return (res > 0) ? 1 : Py_SAFE_DOWNCAST(res, Py_ssize_t, int);
Guido van Rossum6ac258d1993-05-12 08:24:20 +00001413}
1414
Tim Peters803526b2002-07-07 05:13:56 +00001415/* equivalent of 'not v'
Guido van Rossumc3d3f961998-04-09 17:53:59 +00001416 Return -1 if an error occurred */
1417
1418int
Fred Drake100814d2000-07-09 15:48:49 +00001419PyObject_Not(PyObject *v)
Guido van Rossumc3d3f961998-04-09 17:53:59 +00001420{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001421 int res;
1422 res = PyObject_IsTrue(v);
1423 if (res < 0)
1424 return res;
1425 return res == 0;
Guido van Rossumc3d3f961998-04-09 17:53:59 +00001426}
1427
Guido van Rossum49b11fe1995-01-26 00:38:22 +00001428/* Test whether an object can be called */
1429
1430int
Fred Drake100814d2000-07-09 15:48:49 +00001431PyCallable_Check(PyObject *x)
Guido van Rossum49b11fe1995-01-26 00:38:22 +00001432{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001433 if (x == NULL)
1434 return 0;
Victor Stinner58ac7002020-02-07 03:04:21 +01001435 return Py_TYPE(x)->tp_call != NULL;
Guido van Rossum49b11fe1995-01-26 00:38:22 +00001436}
1437
Tim Peters7eea37e2001-09-04 22:08:56 +00001438
Georg Brandle32b4222007-03-10 22:13:27 +00001439/* Helper for PyObject_Dir without arguments: returns the local scope. */
1440static PyObject *
Guido van Rossumad7d8d12007-04-13 01:39:34 +00001441_dir_locals(void)
Tim Peters305b5852001-09-17 02:38:46 +00001442{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001443 PyObject *names;
Victor Stinner41bb43a2013-10-29 01:19:37 +01001444 PyObject *locals;
Tim Peters305b5852001-09-17 02:38:46 +00001445
Victor Stinner41bb43a2013-10-29 01:19:37 +01001446 locals = PyEval_GetLocals();
1447 if (locals == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001448 return NULL;
Tim Peters305b5852001-09-17 02:38:46 +00001449
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001450 names = PyMapping_Keys(locals);
1451 if (!names)
1452 return NULL;
1453 if (!PyList_Check(names)) {
1454 PyErr_Format(PyExc_TypeError,
1455 "dir(): expected keys() of locals to be a list, "
1456 "not '%.200s'", Py_TYPE(names)->tp_name);
1457 Py_DECREF(names);
1458 return NULL;
1459 }
Benjamin Peterson3bbb7222011-06-11 16:12:08 -05001460 if (PyList_Sort(names)) {
1461 Py_DECREF(names);
1462 return NULL;
1463 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001464 /* the locals don't need to be DECREF'd */
1465 return names;
Georg Brandle32b4222007-03-10 22:13:27 +00001466}
1467
Benjamin Peterson82b00c12011-05-24 11:09:06 -05001468/* Helper for PyObject_Dir: object introspection. */
Georg Brandle32b4222007-03-10 22:13:27 +00001469static PyObject *
1470_dir_object(PyObject *obj)
1471{
Benjamin Peterson3bbb7222011-06-11 16:12:08 -05001472 PyObject *result, *sorted;
Benjamin Petersonce798522012-01-22 11:24:29 -05001473 PyObject *dirfunc = _PyObject_LookupSpecial(obj, &PyId___dir__);
Georg Brandle32b4222007-03-10 22:13:27 +00001474
Victor Stinner24702042018-10-26 17:16:37 +02001475 assert(obj != NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001476 if (dirfunc == NULL) {
Benjamin Peterson82b00c12011-05-24 11:09:06 -05001477 if (!PyErr_Occurred())
1478 PyErr_SetString(PyExc_TypeError, "object does not provide __dir__");
1479 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001480 }
Benjamin Peterson82b00c12011-05-24 11:09:06 -05001481 /* use __dir__ */
Victor Stinnerf17c3de2016-12-06 18:46:19 +01001482 result = _PyObject_CallNoArg(dirfunc);
Benjamin Peterson82b00c12011-05-24 11:09:06 -05001483 Py_DECREF(dirfunc);
1484 if (result == NULL)
1485 return NULL;
Benjamin Peterson3bbb7222011-06-11 16:12:08 -05001486 /* return sorted(result) */
1487 sorted = PySequence_List(result);
1488 Py_DECREF(result);
1489 if (sorted == NULL)
1490 return NULL;
1491 if (PyList_Sort(sorted)) {
1492 Py_DECREF(sorted);
1493 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001494 }
Benjamin Peterson3bbb7222011-06-11 16:12:08 -05001495 return sorted;
Georg Brandle32b4222007-03-10 22:13:27 +00001496}
1497
1498/* Implementation of dir() -- if obj is NULL, returns the names in the current
1499 (local) scope. Otherwise, performs introspection of the object: returns a
1500 sorted list of attribute names (supposedly) accessible from the object
1501*/
1502PyObject *
1503PyObject_Dir(PyObject *obj)
1504{
Benjamin Peterson3bbb7222011-06-11 16:12:08 -05001505 return (obj == NULL) ? _dir_locals() : _dir_object(obj);
Tim Peters7eea37e2001-09-04 22:08:56 +00001506}
Guido van Rossum49b11fe1995-01-26 00:38:22 +00001507
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001508/*
Raymond Hettinger66d2be82011-07-28 09:55:13 -07001509None is a non-NULL undefined value.
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001510There is (and should be!) no way to create other objects of this type,
Guido van Rossum3f5da241990-12-20 15:06:42 +00001511so there is exactly one (which is indestructible, by the way).
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001512*/
1513
Guido van Rossum0c182a11992-03-27 17:26:13 +00001514/* ARGSUSED */
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001515static PyObject *
Fred Drake100814d2000-07-09 15:48:49 +00001516none_repr(PyObject *op)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001517{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001518 return PyUnicode_FromString("None");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001519}
1520
Barry Warsaw9bf16442001-01-23 16:24:35 +00001521/* ARGUSED */
Victor Stinner2a4903f2020-01-30 13:09:11 +01001522static void _Py_NO_RETURN
Tim Peters803526b2002-07-07 05:13:56 +00001523none_dealloc(PyObject* ignore)
Barry Warsaw9bf16442001-01-23 16:24:35 +00001524{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001525 /* This should never get called, but we also don't want to SEGV if
1526 * we accidentally decref None out of existence.
1527 */
1528 Py_FatalError("deallocating None");
Barry Warsaw9bf16442001-01-23 16:24:35 +00001529}
1530
Benjamin Petersonc4607ae2011-07-29 18:19:43 -05001531static PyObject *
1532none_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
1533{
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +02001534 if (PyTuple_GET_SIZE(args) || (kwargs && PyDict_GET_SIZE(kwargs))) {
Benjamin Petersonc4607ae2011-07-29 18:19:43 -05001535 PyErr_SetString(PyExc_TypeError, "NoneType takes no arguments");
1536 return NULL;
1537 }
1538 Py_RETURN_NONE;
1539}
1540
Raymond Hettinger66d2be82011-07-28 09:55:13 -07001541static int
1542none_bool(PyObject *v)
1543{
1544 return 0;
1545}
1546
1547static PyNumberMethods none_as_number = {
1548 0, /* nb_add */
1549 0, /* nb_subtract */
1550 0, /* nb_multiply */
1551 0, /* nb_remainder */
1552 0, /* nb_divmod */
1553 0, /* nb_power */
1554 0, /* nb_negative */
1555 0, /* nb_positive */
1556 0, /* nb_absolute */
1557 (inquiry)none_bool, /* nb_bool */
1558 0, /* nb_invert */
1559 0, /* nb_lshift */
1560 0, /* nb_rshift */
1561 0, /* nb_and */
1562 0, /* nb_xor */
1563 0, /* nb_or */
1564 0, /* nb_int */
1565 0, /* nb_reserved */
1566 0, /* nb_float */
1567 0, /* nb_inplace_add */
1568 0, /* nb_inplace_subtract */
1569 0, /* nb_inplace_multiply */
1570 0, /* nb_inplace_remainder */
1571 0, /* nb_inplace_power */
1572 0, /* nb_inplace_lshift */
1573 0, /* nb_inplace_rshift */
1574 0, /* nb_inplace_and */
1575 0, /* nb_inplace_xor */
1576 0, /* nb_inplace_or */
1577 0, /* nb_floor_divide */
1578 0, /* nb_true_divide */
1579 0, /* nb_inplace_floor_divide */
1580 0, /* nb_inplace_true_divide */
1581 0, /* nb_index */
1582};
Barry Warsaw9bf16442001-01-23 16:24:35 +00001583
Alexandre Vassalotti65846c62013-11-30 17:55:48 -08001584PyTypeObject _PyNone_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001585 PyVarObject_HEAD_INIT(&PyType_Type, 0)
1586 "NoneType",
1587 0,
1588 0,
1589 none_dealloc, /*tp_dealloc*/ /*never called*/
Jeroen Demeyer530f5062019-05-31 04:13:39 +02001590 0, /*tp_vectorcall_offset*/
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001591 0, /*tp_getattr*/
1592 0, /*tp_setattr*/
Jeroen Demeyer530f5062019-05-31 04:13:39 +02001593 0, /*tp_as_async*/
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001594 none_repr, /*tp_repr*/
Raymond Hettinger66d2be82011-07-28 09:55:13 -07001595 &none_as_number, /*tp_as_number*/
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001596 0, /*tp_as_sequence*/
1597 0, /*tp_as_mapping*/
1598 0, /*tp_hash */
Benjamin Petersonc4607ae2011-07-29 18:19:43 -05001599 0, /*tp_call */
1600 0, /*tp_str */
1601 0, /*tp_getattro */
1602 0, /*tp_setattro */
1603 0, /*tp_as_buffer */
1604 Py_TPFLAGS_DEFAULT, /*tp_flags */
1605 0, /*tp_doc */
1606 0, /*tp_traverse */
1607 0, /*tp_clear */
1608 0, /*tp_richcompare */
1609 0, /*tp_weaklistoffset */
1610 0, /*tp_iter */
1611 0, /*tp_iternext */
1612 0, /*tp_methods */
1613 0, /*tp_members */
1614 0, /*tp_getset */
1615 0, /*tp_base */
1616 0, /*tp_dict */
1617 0, /*tp_descr_get */
1618 0, /*tp_descr_set */
1619 0, /*tp_dictoffset */
1620 0, /*tp_init */
1621 0, /*tp_alloc */
1622 none_new, /*tp_new */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001623};
1624
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001625PyObject _Py_NoneStruct = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001626 _PyObject_EXTRA_INIT
Alexandre Vassalotti65846c62013-11-30 17:55:48 -08001627 1, &_PyNone_Type
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001628};
1629
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +00001630/* NotImplemented is an object that can be used to signal that an
1631 operation is not implemented for the given type combination. */
1632
1633static PyObject *
1634NotImplemented_repr(PyObject *op)
1635{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001636 return PyUnicode_FromString("NotImplemented");
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +00001637}
1638
Benjamin Peterson18d7d7a2011-07-29 18:27:44 -05001639static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05301640NotImplemented_reduce(PyObject *op, PyObject *Py_UNUSED(ignored))
Alexandre Vassalottic49477b2013-11-24 02:53:45 -08001641{
1642 return PyUnicode_FromString("NotImplemented");
1643}
1644
1645static PyMethodDef notimplemented_methods[] = {
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05301646 {"__reduce__", NotImplemented_reduce, METH_NOARGS, NULL},
Alexandre Vassalottic49477b2013-11-24 02:53:45 -08001647 {NULL, NULL}
1648};
1649
1650static PyObject *
Benjamin Peterson18d7d7a2011-07-29 18:27:44 -05001651notimplemented_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
1652{
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +02001653 if (PyTuple_GET_SIZE(args) || (kwargs && PyDict_GET_SIZE(kwargs))) {
Benjamin Peterson18d7d7a2011-07-29 18:27:44 -05001654 PyErr_SetString(PyExc_TypeError, "NotImplementedType takes no arguments");
1655 return NULL;
1656 }
Brian Curtindfc80e32011-08-10 20:28:54 -05001657 Py_RETURN_NOTIMPLEMENTED;
Benjamin Peterson18d7d7a2011-07-29 18:27:44 -05001658}
1659
Victor Stinner2a4903f2020-01-30 13:09:11 +01001660static void _Py_NO_RETURN
Armin Ronacher226b1db2012-10-06 14:28:58 +02001661notimplemented_dealloc(PyObject* ignore)
1662{
1663 /* This should never get called, but we also don't want to SEGV if
1664 * we accidentally decref NotImplemented out of existence.
1665 */
1666 Py_FatalError("deallocating NotImplemented");
1667}
1668
MojoVampire469325c2020-03-03 18:50:17 +00001669static int
1670notimplemented_bool(PyObject *v)
1671{
1672 if (PyErr_WarnEx(PyExc_DeprecationWarning,
1673 "NotImplemented should not be used in a boolean context",
1674 1) < 0)
1675 {
1676 return -1;
1677 }
1678 return 1;
1679}
1680
1681static PyNumberMethods notimplemented_as_number = {
1682 .nb_bool = notimplemented_bool,
1683};
1684
Alexandre Vassalotti65846c62013-11-30 17:55:48 -08001685PyTypeObject _PyNotImplemented_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001686 PyVarObject_HEAD_INIT(&PyType_Type, 0)
1687 "NotImplementedType",
1688 0,
1689 0,
Armin Ronacher226b1db2012-10-06 14:28:58 +02001690 notimplemented_dealloc, /*tp_dealloc*/ /*never called*/
Jeroen Demeyer530f5062019-05-31 04:13:39 +02001691 0, /*tp_vectorcall_offset*/
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001692 0, /*tp_getattr*/
1693 0, /*tp_setattr*/
Jeroen Demeyer530f5062019-05-31 04:13:39 +02001694 0, /*tp_as_async*/
MojoVampire469325c2020-03-03 18:50:17 +00001695 NotImplemented_repr, /*tp_repr*/
1696 &notimplemented_as_number, /*tp_as_number*/
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001697 0, /*tp_as_sequence*/
1698 0, /*tp_as_mapping*/
1699 0, /*tp_hash */
Benjamin Peterson18d7d7a2011-07-29 18:27:44 -05001700 0, /*tp_call */
1701 0, /*tp_str */
1702 0, /*tp_getattro */
1703 0, /*tp_setattro */
1704 0, /*tp_as_buffer */
1705 Py_TPFLAGS_DEFAULT, /*tp_flags */
1706 0, /*tp_doc */
1707 0, /*tp_traverse */
1708 0, /*tp_clear */
1709 0, /*tp_richcompare */
1710 0, /*tp_weaklistoffset */
1711 0, /*tp_iter */
1712 0, /*tp_iternext */
Alexandre Vassalottic49477b2013-11-24 02:53:45 -08001713 notimplemented_methods, /*tp_methods */
Benjamin Peterson18d7d7a2011-07-29 18:27:44 -05001714 0, /*tp_members */
1715 0, /*tp_getset */
1716 0, /*tp_base */
1717 0, /*tp_dict */
1718 0, /*tp_descr_get */
1719 0, /*tp_descr_set */
1720 0, /*tp_dictoffset */
1721 0, /*tp_init */
1722 0, /*tp_alloc */
1723 notimplemented_new, /*tp_new */
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +00001724};
1725
1726PyObject _Py_NotImplementedStruct = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001727 _PyObject_EXTRA_INIT
Alexandre Vassalotti65846c62013-11-30 17:55:48 -08001728 1, &_PyNotImplemented_Type
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +00001729};
1730
Victor Stinner331a6a52019-05-27 16:39:22 +02001731PyStatus
Victor Stinnerab672812019-01-23 15:04:40 +01001732_PyTypes_Init(void)
Guido van Rossumba21a492001-08-16 08:17:26 +00001733{
Victor Stinner7a1f6c22020-01-30 09:02:14 +01001734 PyStatus status = _PyTypes_InitSlotDefs();
1735 if (_PyStatus_EXCEPTION(status)) {
1736 return status;
1737 }
1738
Victor Stinner6d43f6f2019-01-22 21:18:05 +01001739#define INIT_TYPE(TYPE, NAME) \
1740 do { \
1741 if (PyType_Ready(TYPE) < 0) { \
Victor Stinner331a6a52019-05-27 16:39:22 +02001742 return _PyStatus_ERR("Can't initialize " NAME " type"); \
Victor Stinner6d43f6f2019-01-22 21:18:05 +01001743 } \
1744 } while (0)
Victor Stinner5a1bb4e2014-06-02 14:10:59 +02001745
Victor Stinner6d43f6f2019-01-22 21:18:05 +01001746 INIT_TYPE(&PyBaseObject_Type, "object");
1747 INIT_TYPE(&PyType_Type, "type");
1748 INIT_TYPE(&_PyWeakref_RefType, "weakref");
1749 INIT_TYPE(&_PyWeakref_CallableProxyType, "callable weakref proxy");
1750 INIT_TYPE(&_PyWeakref_ProxyType, "weakref proxy");
1751 INIT_TYPE(&PyLong_Type, "int");
1752 INIT_TYPE(&PyBool_Type, "bool");
1753 INIT_TYPE(&PyByteArray_Type, "bytearray");
1754 INIT_TYPE(&PyBytes_Type, "str");
1755 INIT_TYPE(&PyList_Type, "list");
1756 INIT_TYPE(&_PyNone_Type, "None");
1757 INIT_TYPE(&_PyNotImplemented_Type, "NotImplemented");
1758 INIT_TYPE(&PyTraceBack_Type, "traceback");
1759 INIT_TYPE(&PySuper_Type, "super");
1760 INIT_TYPE(&PyRange_Type, "range");
1761 INIT_TYPE(&PyDict_Type, "dict");
1762 INIT_TYPE(&PyDictKeys_Type, "dict keys");
1763 INIT_TYPE(&PyDictValues_Type, "dict values");
1764 INIT_TYPE(&PyDictItems_Type, "dict items");
1765 INIT_TYPE(&PyDictRevIterKey_Type, "reversed dict keys");
1766 INIT_TYPE(&PyDictRevIterValue_Type, "reversed dict values");
1767 INIT_TYPE(&PyDictRevIterItem_Type, "reversed dict items");
1768 INIT_TYPE(&PyODict_Type, "OrderedDict");
1769 INIT_TYPE(&PyODictKeys_Type, "odict_keys");
1770 INIT_TYPE(&PyODictItems_Type, "odict_items");
1771 INIT_TYPE(&PyODictValues_Type, "odict_values");
1772 INIT_TYPE(&PyODictIter_Type, "odict_keyiterator");
1773 INIT_TYPE(&PySet_Type, "set");
1774 INIT_TYPE(&PyUnicode_Type, "str");
1775 INIT_TYPE(&PySlice_Type, "slice");
1776 INIT_TYPE(&PyStaticMethod_Type, "static method");
1777 INIT_TYPE(&PyComplex_Type, "complex");
1778 INIT_TYPE(&PyFloat_Type, "float");
1779 INIT_TYPE(&PyFrozenSet_Type, "frozenset");
1780 INIT_TYPE(&PyProperty_Type, "property");
1781 INIT_TYPE(&_PyManagedBuffer_Type, "managed buffer");
1782 INIT_TYPE(&PyMemoryView_Type, "memoryview");
1783 INIT_TYPE(&PyTuple_Type, "tuple");
1784 INIT_TYPE(&PyEnum_Type, "enumerate");
1785 INIT_TYPE(&PyReversed_Type, "reversed");
1786 INIT_TYPE(&PyStdPrinter_Type, "StdPrinter");
1787 INIT_TYPE(&PyCode_Type, "code");
1788 INIT_TYPE(&PyFrame_Type, "frame");
1789 INIT_TYPE(&PyCFunction_Type, "builtin function");
1790 INIT_TYPE(&PyMethod_Type, "method");
1791 INIT_TYPE(&PyFunction_Type, "function");
1792 INIT_TYPE(&PyDictProxy_Type, "dict proxy");
1793 INIT_TYPE(&PyGen_Type, "generator");
1794 INIT_TYPE(&PyGetSetDescr_Type, "get-set descriptor");
1795 INIT_TYPE(&PyWrapperDescr_Type, "wrapper");
1796 INIT_TYPE(&_PyMethodWrapper_Type, "method wrapper");
1797 INIT_TYPE(&PyEllipsis_Type, "ellipsis");
1798 INIT_TYPE(&PyMemberDescr_Type, "member descriptor");
1799 INIT_TYPE(&_PyNamespace_Type, "namespace");
1800 INIT_TYPE(&PyCapsule_Type, "capsule");
1801 INIT_TYPE(&PyLongRangeIter_Type, "long range iterator");
1802 INIT_TYPE(&PyCell_Type, "cell");
1803 INIT_TYPE(&PyInstanceMethod_Type, "instance method");
1804 INIT_TYPE(&PyClassMethodDescr_Type, "class method descr");
1805 INIT_TYPE(&PyMethodDescr_Type, "method descr");
1806 INIT_TYPE(&PyCallIter_Type, "call iter");
1807 INIT_TYPE(&PySeqIter_Type, "sequence iterator");
Antoine Pitrou91f43802019-05-26 17:10:09 +02001808 INIT_TYPE(&PyPickleBuffer_Type, "pickle.PickleBuffer");
Victor Stinner6d43f6f2019-01-22 21:18:05 +01001809 INIT_TYPE(&PyCoro_Type, "coroutine");
1810 INIT_TYPE(&_PyCoroWrapper_Type, "coroutine wrapper");
Eric Snowc11183c2019-03-15 16:35:46 -06001811 INIT_TYPE(&_PyInterpreterID_Type, "interpreter ID");
Victor Stinner331a6a52019-05-27 16:39:22 +02001812 return _PyStatus_OK();
Guido van Rossumba21a492001-08-16 08:17:26 +00001813
Victor Stinner6d43f6f2019-01-22 21:18:05 +01001814#undef INIT_TYPE
Guido van Rossumba21a492001-08-16 08:17:26 +00001815}
1816
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001817
Victor Stinner40e547d2020-02-05 01:11:10 +01001818void
1819_Py_NewReference(PyObject *op)
1820{
1821 if (_Py_tracemalloc_config.tracing) {
1822 _PyTraceMalloc_NewReference(op);
1823 }
1824#ifdef Py_REF_DEBUG
1825 _Py_RefTotal++;
1826#endif
Victor Stinnerc86a1122020-02-07 01:24:29 +01001827 Py_SET_REFCNT(op, 1);
Victor Stinner40e547d2020-02-05 01:11:10 +01001828#ifdef Py_TRACE_REFS
1829 _Py_AddToAllObjects(op, 1);
1830#endif
1831}
1832
1833
Guido van Rossum84a90321996-05-22 16:34:47 +00001834#ifdef Py_TRACE_REFS
Guido van Rossumaacdc9d1996-08-12 21:32:12 +00001835void
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02001836_Py_ForgetReference(PyObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001837{
Victor Stinnera93c51e2020-02-07 00:38:59 +01001838 if (Py_REFCNT(op) < 0) {
Victor Stinner5eb8bff2020-01-30 09:01:07 +01001839 _PyObject_ASSERT_FAILED_MSG(op, "negative refcnt");
1840 }
1841
1842 if (op == &refchain ||
1843 op->_ob_prev->_ob_next != op || op->_ob_next->_ob_prev != op)
1844 {
1845 _PyObject_ASSERT_FAILED_MSG(op, "invalid object chain");
1846 }
1847
Guido van Rossumbffd6832000-01-20 22:32:56 +00001848#ifdef SLOW_UNREF_CHECK
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02001849 PyObject *p;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001850 for (p = refchain._ob_next; p != &refchain; p = p->_ob_next) {
Victor Stinner5eb8bff2020-01-30 09:01:07 +01001851 if (p == op) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001852 break;
Victor Stinner5eb8bff2020-01-30 09:01:07 +01001853 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001854 }
Victor Stinner5eb8bff2020-01-30 09:01:07 +01001855 if (p == &refchain) {
1856 /* Not found */
1857 _PyObject_ASSERT_FAILED_MSG(op,
1858 "object not found in the objects list");
1859 }
Guido van Rossum2e8f6141992-09-03 20:32:55 +00001860#endif
Victor Stinner5eb8bff2020-01-30 09:01:07 +01001861
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001862 op->_ob_next->_ob_prev = op->_ob_prev;
1863 op->_ob_prev->_ob_next = op->_ob_next;
1864 op->_ob_next = op->_ob_prev = NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001865}
1866
Tim Peters269b2a62003-04-17 19:52:29 +00001867/* Print all live objects. Because PyObject_Print is called, the
1868 * interpreter must be in a healthy state.
1869 */
Guido van Rossumaacdc9d1996-08-12 21:32:12 +00001870void
Fred Drake100814d2000-07-09 15:48:49 +00001871_Py_PrintReferences(FILE *fp)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001872{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001873 PyObject *op;
1874 fprintf(fp, "Remaining objects:\n");
1875 for (op = refchain._ob_next; op != &refchain; op = op->_ob_next) {
Victor Stinnera93c51e2020-02-07 00:38:59 +01001876 fprintf(fp, "%p [%" PY_FORMAT_SIZE_T "d] ", (void *)op, Py_REFCNT(op));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001877 if (PyObject_Print(op, fp, 0) != 0)
1878 PyErr_Clear();
1879 putc('\n', fp);
1880 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001881}
1882
Tim Peters269b2a62003-04-17 19:52:29 +00001883/* Print the addresses of all live objects. Unlike _Py_PrintReferences, this
1884 * doesn't make any calls to the Python C API, so is always safe to call.
1885 */
1886void
1887_Py_PrintReferenceAddresses(FILE *fp)
1888{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001889 PyObject *op;
1890 fprintf(fp, "Remaining object addresses:\n");
1891 for (op = refchain._ob_next; op != &refchain; op = op->_ob_next)
Zackery Spytz1a2252e2019-05-06 10:56:51 -06001892 fprintf(fp, "%p [%" PY_FORMAT_SIZE_T "d] %s\n", (void *)op,
Victor Stinnera93c51e2020-02-07 00:38:59 +01001893 Py_REFCNT(op), Py_TYPE(op)->tp_name);
Tim Peters269b2a62003-04-17 19:52:29 +00001894}
1895
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +00001896PyObject *
Fred Drake100814d2000-07-09 15:48:49 +00001897_Py_GetObjects(PyObject *self, PyObject *args)
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +00001898{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001899 int i, n;
1900 PyObject *t = NULL;
1901 PyObject *res, *op;
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +00001902
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001903 if (!PyArg_ParseTuple(args, "i|O", &n, &t))
1904 return NULL;
1905 op = refchain._ob_next;
1906 res = PyList_New(0);
1907 if (res == NULL)
1908 return NULL;
1909 for (i = 0; (n == 0 || i < n) && op != &refchain; i++) {
1910 while (op == self || op == args || op == res || op == t ||
Andy Lester55728702020-03-06 16:53:17 -06001911 (t != NULL && !Py_IS_TYPE(op, (PyTypeObject *) t))) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001912 op = op->_ob_next;
1913 if (op == &refchain)
1914 return res;
1915 }
1916 if (PyList_Append(res, op) < 0) {
1917 Py_DECREF(res);
1918 return NULL;
1919 }
1920 op = op->_ob_next;
1921 }
1922 return res;
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +00001923}
1924
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001925#endif
Guido van Rossum97ead3f1996-01-12 01:24:09 +00001926
Benjamin Petersonb173f782009-05-05 22:31:58 +00001927
Guido van Rossum84a90321996-05-22 16:34:47 +00001928/* Hack to force loading of abstract.o */
Martin v. Löwis18e16552006-02-15 17:27:45 +00001929Py_ssize_t (*_Py_abstract_hack)(PyObject *) = PyObject_Size;
Guido van Rossume09fb551997-08-05 02:04:34 +00001930
1931
David Malcolm49526f42012-06-22 14:55:41 -04001932void
1933_PyObject_DebugTypeStats(FILE *out)
1934{
David Malcolm49526f42012-06-22 14:55:41 -04001935 _PyDict_DebugMallocStats(out);
1936 _PyFloat_DebugMallocStats(out);
1937 _PyFrame_DebugMallocStats(out);
1938 _PyList_DebugMallocStats(out);
David Malcolm49526f42012-06-22 14:55:41 -04001939 _PyTuple_DebugMallocStats(out);
1940}
Guido van Rossumb18618d2000-05-03 23:44:39 +00001941
Guido van Rossum86610361998-04-10 22:32:46 +00001942/* These methods are used to control infinite recursion in repr, str, print,
1943 etc. Container objects that may recursively contain themselves,
Martin Panter8d56c022016-05-29 04:13:35 +00001944 e.g. builtin dictionaries and lists, should use Py_ReprEnter() and
Guido van Rossum86610361998-04-10 22:32:46 +00001945 Py_ReprLeave() to avoid infinite recursion.
1946
1947 Py_ReprEnter() returns 0 the first time it is called for a particular
1948 object and 1 every time thereafter. It returns -1 if an exception
1949 occurred. Py_ReprLeave() has no return value.
1950
1951 See dictobject.c and listobject.c for examples of use.
1952*/
1953
Guido van Rossum86610361998-04-10 22:32:46 +00001954int
Fred Drake100814d2000-07-09 15:48:49 +00001955Py_ReprEnter(PyObject *obj)
Guido van Rossum86610361998-04-10 22:32:46 +00001956{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001957 PyObject *dict;
1958 PyObject *list;
1959 Py_ssize_t i;
Guido van Rossum86610361998-04-10 22:32:46 +00001960
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001961 dict = PyThreadState_GetDict();
Antoine Pitrou04d17d32014-03-31 22:04:38 +02001962 /* Ignore a missing thread-state, so that this function can be called
1963 early on startup. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001964 if (dict == NULL)
1965 return 0;
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02001966 list = _PyDict_GetItemIdWithError(dict, &PyId_Py_Repr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001967 if (list == NULL) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02001968 if (PyErr_Occurred()) {
1969 return -1;
1970 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001971 list = PyList_New(0);
1972 if (list == NULL)
1973 return -1;
Victor Stinner7a07e452013-11-06 18:57:29 +01001974 if (_PyDict_SetItemId(dict, &PyId_Py_Repr, list) < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001975 return -1;
1976 Py_DECREF(list);
1977 }
1978 i = PyList_GET_SIZE(list);
1979 while (--i >= 0) {
1980 if (PyList_GET_ITEM(list, i) == obj)
1981 return 1;
1982 }
Victor Stinnere901d1f2013-07-17 21:58:41 +02001983 if (PyList_Append(list, obj) < 0)
1984 return -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001985 return 0;
Guido van Rossum86610361998-04-10 22:32:46 +00001986}
1987
1988void
Fred Drake100814d2000-07-09 15:48:49 +00001989Py_ReprLeave(PyObject *obj)
Guido van Rossum86610361998-04-10 22:32:46 +00001990{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001991 PyObject *dict;
1992 PyObject *list;
1993 Py_ssize_t i;
Victor Stinner1b634932013-07-16 22:24:44 +02001994 PyObject *error_type, *error_value, *error_traceback;
1995
1996 PyErr_Fetch(&error_type, &error_value, &error_traceback);
Guido van Rossum86610361998-04-10 22:32:46 +00001997
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001998 dict = PyThreadState_GetDict();
1999 if (dict == NULL)
Victor Stinner1b634932013-07-16 22:24:44 +02002000 goto finally;
2001
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002002 list = _PyDict_GetItemIdWithError(dict, &PyId_Py_Repr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002003 if (list == NULL || !PyList_Check(list))
Victor Stinner1b634932013-07-16 22:24:44 +02002004 goto finally;
2005
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002006 i = PyList_GET_SIZE(list);
2007 /* Count backwards because we always expect obj to be list[-1] */
2008 while (--i >= 0) {
2009 if (PyList_GET_ITEM(list, i) == obj) {
2010 PyList_SetSlice(list, i, i + 1, NULL);
2011 break;
2012 }
2013 }
Victor Stinner1b634932013-07-16 22:24:44 +02002014
2015finally:
2016 /* ignore exceptions because there is no way to report them. */
2017 PyErr_Restore(error_type, error_value, error_traceback);
Guido van Rossum86610361998-04-10 22:32:46 +00002018}
Guido van Rossumd724b232000-03-13 16:01:29 +00002019
Tim Peters803526b2002-07-07 05:13:56 +00002020/* Trashcan support. */
Guido van Rossumd724b232000-03-13 16:01:29 +00002021
Tim Peters803526b2002-07-07 05:13:56 +00002022/* Add op to the _PyTrash_delete_later list. Called when the current
2023 * call-stack depth gets large. op must be a currently untracked gc'ed
2024 * object, with refcount 0. Py_DECREF must already have been called on it.
2025 */
Guido van Rossumd724b232000-03-13 16:01:29 +00002026void
Fred Drake100814d2000-07-09 15:48:49 +00002027_PyTrash_deposit_object(PyObject *op)
Guido van Rossumd724b232000-03-13 16:01:29 +00002028{
Victor Stinner72474072019-11-20 12:25:50 +01002029 PyThreadState *tstate = _PyThreadState_GET();
2030 struct _gc_runtime_state *gcstate = &tstate->interp->gc;
2031
Victor Stinner24702042018-10-26 17:16:37 +02002032 _PyObject_ASSERT(op, PyObject_IS_GC(op));
2033 _PyObject_ASSERT(op, !_PyObject_GC_IS_TRACKED(op));
Victor Stinnera93c51e2020-02-07 00:38:59 +01002034 _PyObject_ASSERT(op, Py_REFCNT(op) == 0);
Victor Stinner72474072019-11-20 12:25:50 +01002035 _PyGCHead_SET_PREV(_Py_AS_GC(op), gcstate->trash_delete_later);
2036 gcstate->trash_delete_later = op;
Guido van Rossumd724b232000-03-13 16:01:29 +00002037}
2038
Antoine Pitrou2b0218a2012-09-06 00:59:49 +02002039/* The equivalent API, using per-thread state recursion info */
2040void
2041_PyTrash_thread_deposit_object(PyObject *op)
2042{
Victor Stinner50b48572018-11-01 01:51:40 +01002043 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinner24702042018-10-26 17:16:37 +02002044 _PyObject_ASSERT(op, PyObject_IS_GC(op));
2045 _PyObject_ASSERT(op, !_PyObject_GC_IS_TRACKED(op));
Victor Stinnera93c51e2020-02-07 00:38:59 +01002046 _PyObject_ASSERT(op, Py_REFCNT(op) == 0);
INADA Naoki5ac9e6e2018-07-10 17:19:53 +09002047 _PyGCHead_SET_PREV(_Py_AS_GC(op), tstate->trash_delete_later);
Antoine Pitrou2b0218a2012-09-06 00:59:49 +02002048 tstate->trash_delete_later = op;
2049}
2050
Min ho Kimc4cacc82019-07-31 08:16:13 +10002051/* Deallocate all the objects in the _PyTrash_delete_later list. Called when
Tim Peters803526b2002-07-07 05:13:56 +00002052 * the call-stack unwinds again.
2053 */
Guido van Rossumd724b232000-03-13 16:01:29 +00002054void
Fred Drake100814d2000-07-09 15:48:49 +00002055_PyTrash_destroy_chain(void)
Guido van Rossumd724b232000-03-13 16:01:29 +00002056{
Victor Stinner72474072019-11-20 12:25:50 +01002057 PyThreadState *tstate = _PyThreadState_GET();
2058 struct _gc_runtime_state *gcstate = &tstate->interp->gc;
2059
2060 while (gcstate->trash_delete_later) {
2061 PyObject *op = gcstate->trash_delete_later;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002062 destructor dealloc = Py_TYPE(op)->tp_dealloc;
Neil Schemenauerf589c052002-03-29 03:05:54 +00002063
Victor Stinner72474072019-11-20 12:25:50 +01002064 gcstate->trash_delete_later =
INADA Naoki5ac9e6e2018-07-10 17:19:53 +09002065 (PyObject*) _PyGCHead_PREV(_Py_AS_GC(op));
Neil Schemenauerf589c052002-03-29 03:05:54 +00002066
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002067 /* Call the deallocator directly. This used to try to
2068 * fool Py_DECREF into calling it indirectly, but
2069 * Py_DECREF was already called on this object, and in
2070 * assorted non-release builds calling Py_DECREF again ends
2071 * up distorting allocation statistics.
2072 */
Victor Stinnera93c51e2020-02-07 00:38:59 +01002073 _PyObject_ASSERT(op, Py_REFCNT(op) == 0);
Victor Stinner72474072019-11-20 12:25:50 +01002074 ++gcstate->trash_delete_nesting;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002075 (*dealloc)(op);
Victor Stinner72474072019-11-20 12:25:50 +01002076 --gcstate->trash_delete_nesting;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002077 }
Guido van Rossumd724b232000-03-13 16:01:29 +00002078}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002079
Antoine Pitrou2b0218a2012-09-06 00:59:49 +02002080/* The equivalent API, using per-thread state recursion info */
2081void
2082_PyTrash_thread_destroy_chain(void)
2083{
Victor Stinner50b48572018-11-01 01:51:40 +01002084 PyThreadState *tstate = _PyThreadState_GET();
Xiang Zhanga66f9c62017-05-13 13:36:14 +08002085 /* We need to increase trash_delete_nesting here, otherwise,
2086 _PyTrash_thread_destroy_chain will be called recursively
2087 and then possibly crash. An example that may crash without
2088 increase:
2089 N = 500000 # need to be large enough
2090 ob = object()
2091 tups = [(ob,) for i in range(N)]
2092 for i in range(49):
2093 tups = [(tup,) for tup in tups]
2094 del tups
2095 */
2096 assert(tstate->trash_delete_nesting == 0);
2097 ++tstate->trash_delete_nesting;
Antoine Pitrou2b0218a2012-09-06 00:59:49 +02002098 while (tstate->trash_delete_later) {
2099 PyObject *op = tstate->trash_delete_later;
2100 destructor dealloc = Py_TYPE(op)->tp_dealloc;
2101
2102 tstate->trash_delete_later =
INADA Naoki5ac9e6e2018-07-10 17:19:53 +09002103 (PyObject*) _PyGCHead_PREV(_Py_AS_GC(op));
Antoine Pitrou2b0218a2012-09-06 00:59:49 +02002104
2105 /* Call the deallocator directly. This used to try to
2106 * fool Py_DECREF into calling it indirectly, but
2107 * Py_DECREF was already called on this object, and in
2108 * assorted non-release builds calling Py_DECREF again ends
2109 * up distorting allocation statistics.
2110 */
Victor Stinnera93c51e2020-02-07 00:38:59 +01002111 _PyObject_ASSERT(op, Py_REFCNT(op) == 0);
Antoine Pitrou2b0218a2012-09-06 00:59:49 +02002112 (*dealloc)(op);
Xiang Zhanga66f9c62017-05-13 13:36:14 +08002113 assert(tstate->trash_delete_nesting == 1);
Antoine Pitrou2b0218a2012-09-06 00:59:49 +02002114 }
Xiang Zhanga66f9c62017-05-13 13:36:14 +08002115 --tstate->trash_delete_nesting;
Antoine Pitrou2b0218a2012-09-06 00:59:49 +02002116}
2117
Victor Stinner626bff82018-10-25 17:31:10 +02002118
Victor Stinner2a4903f2020-01-30 13:09:11 +01002119void _Py_NO_RETURN
Victor Stinnerf1d002c2018-11-21 23:53:44 +01002120_PyObject_AssertFailed(PyObject *obj, const char *expr, const char *msg,
Victor Stinner626bff82018-10-25 17:31:10 +02002121 const char *file, int line, const char *function)
2122{
Victor Stinnerf1d002c2018-11-21 23:53:44 +01002123 fprintf(stderr, "%s:%d: ", file, line);
2124 if (function) {
2125 fprintf(stderr, "%s: ", function);
2126 }
Victor Stinner626bff82018-10-25 17:31:10 +02002127 fflush(stderr);
Victor Stinner68762572019-10-07 18:42:01 +02002128
Victor Stinnerf1d002c2018-11-21 23:53:44 +01002129 if (expr) {
2130 fprintf(stderr, "Assertion \"%s\" failed", expr);
Victor Stinner626bff82018-10-25 17:31:10 +02002131 }
2132 else {
Victor Stinnerf1d002c2018-11-21 23:53:44 +01002133 fprintf(stderr, "Assertion failed");
Victor Stinner626bff82018-10-25 17:31:10 +02002134 }
2135 fflush(stderr);
Victor Stinner68762572019-10-07 18:42:01 +02002136
Victor Stinnerf1d002c2018-11-21 23:53:44 +01002137 if (msg) {
2138 fprintf(stderr, ": %s", msg);
2139 }
2140 fprintf(stderr, "\n");
2141 fflush(stderr);
Victor Stinner626bff82018-10-25 17:31:10 +02002142
Victor Stinner68762572019-10-07 18:42:01 +02002143 if (_PyObject_IsFreed(obj)) {
Victor Stinner626bff82018-10-25 17:31:10 +02002144 /* It seems like the object memory has been freed:
2145 don't access it to prevent a segmentation fault. */
Victor Stinnerb39afb72019-09-17 23:36:28 +02002146 fprintf(stderr, "<object at %p is freed>\n", obj);
Victor Stinner68762572019-10-07 18:42:01 +02002147 fflush(stderr);
Victor Stinner626bff82018-10-25 17:31:10 +02002148 }
2149 else {
penguindustin96466302019-05-06 14:57:17 -04002150 /* Display the traceback where the object has been allocated.
Victor Stinner626bff82018-10-25 17:31:10 +02002151 Do it before dumping repr(obj), since repr() is more likely
2152 to crash than dumping the traceback. */
2153 void *ptr;
2154 PyTypeObject *type = Py_TYPE(obj);
2155 if (PyType_IS_GC(type)) {
2156 ptr = (void *)((char *)obj - sizeof(PyGC_Head));
2157 }
2158 else {
2159 ptr = (void *)obj;
2160 }
2161 _PyMem_DumpTraceback(fileno(stderr), ptr);
2162
2163 /* This might succeed or fail, but we're about to abort, so at least
2164 try to provide any extra info we can: */
2165 _PyObject_Dump(obj);
Victor Stinner77753492019-10-07 23:44:05 +02002166
2167 fprintf(stderr, "\n");
2168 fflush(stderr);
Victor Stinner626bff82018-10-25 17:31:10 +02002169 }
Victor Stinner626bff82018-10-25 17:31:10 +02002170
2171 Py_FatalError("_PyObject_AssertFailed");
2172}
2173
Victor Stinner3c09dca2018-10-30 14:48:26 +01002174
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00002175void
2176_Py_Dealloc(PyObject *op)
2177{
Victor Stinner3c09dca2018-10-30 14:48:26 +01002178 destructor dealloc = Py_TYPE(op)->tp_dealloc;
2179#ifdef Py_TRACE_REFS
2180 _Py_ForgetReference(op);
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00002181#endif
Victor Stinner3c09dca2018-10-30 14:48:26 +01002182 (*dealloc)(op);
2183}
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00002184
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002185#ifdef __cplusplus
2186}
2187#endif