blob: bb47cfa8585f79572b767eb260f47e7910ad2b1d [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001
Benjamin Peterson722954a2011-06-11 16:33:35 -05002/* Generic object operations; and implementation of None */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003
Guido van Rossumc0b618a1997-05-02 03:12:38 +00004#include "Python.h"
Victor Stinnerbe434dc2019-11-05 00:51:22 +01005#include "pycore_context.h"
Victor Stinner331a6a52019-05-27 16:39:22 +02006#include "pycore_initconfig.h"
Victor Stinner0fc91ee2019-04-12 21:51:34 +02007#include "pycore_object.h"
Victor Stinnerbe434dc2019-11-05 00:51:22 +01008#include "pycore_pyerrors.h"
Victor Stinner7a1f6c22020-01-30 09:02:14 +01009#include "pycore_pylifecycle.h"
Victor Stinner621cebe2018-11-12 16:53:38 +010010#include "pycore_pystate.h"
Benjamin Petersonfd838e62009-04-20 02:09:13 +000011#include "frameobject.h"
Eric Snowc11183c2019-03-15 16:35:46 -060012#include "interpreteridobject.h"
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000013
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000014#ifdef __cplusplus
15extern "C" {
16#endif
17
Victor Stinner626bff82018-10-25 17:31:10 +020018/* Defined in tracemalloc.c */
19extern void _PyMem_DumpTraceback(int fd, const void *ptr);
20
Victor Stinnerbd303c12013-11-07 23:07:29 +010021_Py_IDENTIFIER(Py_Repr);
22_Py_IDENTIFIER(__bytes__);
23_Py_IDENTIFIER(__dir__);
24_Py_IDENTIFIER(__isabstractmethod__);
Victor Stinnerbd303c12013-11-07 23:07:29 +010025
Victor Stinner0fc91ee2019-04-12 21:51:34 +020026
27int
28_PyObject_CheckConsistency(PyObject *op, int check_content)
29{
Victor Stinner68762572019-10-07 18:42:01 +020030#define CHECK(expr) \
31 do { if (!(expr)) { _PyObject_ASSERT_FAILED_MSG(op, Py_STRINGIFY(expr)); } } while (0)
Victor Stinner0fc91ee2019-04-12 21:51:34 +020032
Victor Stinner68762572019-10-07 18:42:01 +020033 CHECK(!_PyObject_IsFreed(op));
34 CHECK(Py_REFCNT(op) >= 1);
35
Victor Stinner58ac7002020-02-07 03:04:21 +010036 _PyType_CheckConsistency(Py_TYPE(op));
Victor Stinner0fc91ee2019-04-12 21:51:34 +020037
38 if (PyUnicode_Check(op)) {
39 _PyUnicode_CheckConsistency(op, check_content);
40 }
41 else if (PyDict_Check(op)) {
42 _PyDict_CheckConsistency(op, check_content);
43 }
44 return 1;
Victor Stinner68762572019-10-07 18:42:01 +020045
46#undef CHECK
Victor Stinner0fc91ee2019-04-12 21:51:34 +020047}
48
49
Tim Peters34592512002-07-11 06:23:50 +000050#ifdef Py_REF_DEBUG
Neal Norwitz84632ee2006-03-04 20:00:59 +000051Py_ssize_t _Py_RefTotal;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000052
53Py_ssize_t
54_Py_GetRefTotal(void)
55{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000056 PyObject *o;
57 Py_ssize_t total = _Py_RefTotal;
Antoine Pitrou9d952542013-08-24 21:07:07 +020058 o = _PySet_Dummy;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000059 if (o != NULL)
Victor Stinnera93c51e2020-02-07 00:38:59 +010060 total -= Py_REFCNT(o);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000061 return total;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000062}
Nick Coghland6009512014-11-20 21:39:37 +100063
64void
65_PyDebug_PrintTotalRefs(void) {
Eric Snowdae02762017-09-14 00:35:58 -070066 fprintf(stderr,
67 "[%" PY_FORMAT_SIZE_T "d refs, "
68 "%" PY_FORMAT_SIZE_T "d blocks]\n",
69 _Py_GetRefTotal(), _Py_GetAllocatedBlocks());
Nick Coghland6009512014-11-20 21:39:37 +100070}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000071#endif /* Py_REF_DEBUG */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000072
Guido van Rossum3f5da241990-12-20 15:06:42 +000073/* Object allocation routines used by NEWOBJ and NEWVAROBJ macros.
74 These are used by the individual routines for object creation.
75 Do not call them otherwise, they do not initialize the object! */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000076
Tim Peters78be7992003-03-23 02:51:01 +000077#ifdef Py_TRACE_REFS
Tim Peters7571a0f2003-03-23 17:52:28 +000078/* Head of circular doubly-linked list of all objects. These are linked
79 * together via the _ob_prev and _ob_next members of a PyObject, which
80 * exist only in a Py_TRACE_REFS build.
81 */
Tim Peters78be7992003-03-23 02:51:01 +000082static PyObject refchain = {&refchain, &refchain};
Tim Peters36eb4df2003-03-23 03:33:13 +000083
Tim Peters7571a0f2003-03-23 17:52:28 +000084/* Insert op at the front of the list of all objects. If force is true,
85 * op is added even if _ob_prev and _ob_next are non-NULL already. If
86 * force is false amd _ob_prev or _ob_next are non-NULL, do nothing.
87 * force should be true if and only if op points to freshly allocated,
88 * uninitialized memory, or you've unlinked op from the list and are
Tim Peters51f8d382003-03-23 18:06:08 +000089 * relinking it into the front.
Tim Peters7571a0f2003-03-23 17:52:28 +000090 * Note that objects are normally added to the list via _Py_NewReference,
91 * which is called by PyObject_Init. Not all objects are initialized that
92 * way, though; exceptions include statically allocated type objects, and
93 * statically allocated singletons (like Py_True and Py_None).
94 */
Victor Stinner58f4e1a2020-02-05 18:24:33 +010095void
Tim Peters7571a0f2003-03-23 17:52:28 +000096_Py_AddToAllObjects(PyObject *op, int force)
Tim Peters36eb4df2003-03-23 03:33:13 +000097{
Tim Peters7571a0f2003-03-23 17:52:28 +000098#ifdef Py_DEBUG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000099 if (!force) {
100 /* If it's initialized memory, op must be in or out of
101 * the list unambiguously.
102 */
Victor Stinner24702042018-10-26 17:16:37 +0200103 _PyObject_ASSERT(op, (op->_ob_prev == NULL) == (op->_ob_next == NULL));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000104 }
Tim Peters78be7992003-03-23 02:51:01 +0000105#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000106 if (force || op->_ob_prev == NULL) {
107 op->_ob_next = refchain._ob_next;
108 op->_ob_prev = &refchain;
109 refchain._ob_next->_ob_prev = op;
110 refchain._ob_next = op;
111 }
Tim Peters7571a0f2003-03-23 17:52:28 +0000112}
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000113#endif /* Py_TRACE_REFS */
Tim Peters78be7992003-03-23 02:51:01 +0000114
Tim Peters7c321a82002-07-09 02:57:01 +0000115#ifdef Py_REF_DEBUG
116/* Log a fatal error; doesn't return. */
117void
Victor Stinner18618e652018-10-25 17:28:11 +0200118_Py_NegativeRefcount(const char *filename, int lineno, PyObject *op)
Tim Peters7c321a82002-07-09 02:57:01 +0000119{
Victor Stinnerf1d002c2018-11-21 23:53:44 +0100120 _PyObject_AssertFailed(op, NULL, "object has negative ref count",
Victor Stinner3ec9af72018-10-26 02:12:34 +0200121 filename, lineno, __func__);
Tim Peters7c321a82002-07-09 02:57:01 +0000122}
123
124#endif /* Py_REF_DEBUG */
125
Thomas Heller1328b522004-04-22 17:23:49 +0000126void
127Py_IncRef(PyObject *o)
128{
129 Py_XINCREF(o);
130}
131
132void
133Py_DecRef(PyObject *o)
134{
135 Py_XDECREF(o);
136}
137
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000138PyObject *
Fred Drake100814d2000-07-09 15:48:49 +0000139PyObject_Init(PyObject *op, PyTypeObject *tp)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000140{
Victor Stinnerf58bd7c2020-02-05 13:12:19 +0100141 /* Any changes should be reflected in PyObject_INIT() macro */
142 if (op == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000143 return PyErr_NoMemory();
Victor Stinnerf58bd7c2020-02-05 13:12:19 +0100144 }
145
Victor Stinner1fb5a9f2020-03-06 15:55:14 +0100146 return PyObject_INIT(op, tp);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000147}
148
Guido van Rossumb18618d2000-05-03 23:44:39 +0000149PyVarObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000150PyObject_InitVar(PyVarObject *op, PyTypeObject *tp, Py_ssize_t size)
Guido van Rossumb18618d2000-05-03 23:44:39 +0000151{
Victor Stinnerf58bd7c2020-02-05 13:12:19 +0100152 /* Any changes should be reflected in PyObject_INIT_VAR() macro */
153 if (op == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000154 return (PyVarObject *) PyErr_NoMemory();
Victor Stinnerf58bd7c2020-02-05 13:12:19 +0100155 }
156
Victor Stinner1fb5a9f2020-03-06 15:55:14 +0100157 return PyObject_INIT_VAR(op, tp, size);
Guido van Rossumb18618d2000-05-03 23:44:39 +0000158}
159
160PyObject *
Fred Drake100814d2000-07-09 15:48:49 +0000161_PyObject_New(PyTypeObject *tp)
Guido van Rossumb18618d2000-05-03 23:44:39 +0000162{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000163 PyObject *op;
164 op = (PyObject *) PyObject_MALLOC(_PyObject_SIZE(tp));
165 if (op == NULL)
166 return PyErr_NoMemory();
167 return PyObject_INIT(op, tp);
Guido van Rossumb18618d2000-05-03 23:44:39 +0000168}
169
Guido van Rossumd0c87ee1997-05-15 21:31:03 +0000170PyVarObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000171_PyObject_NewVar(PyTypeObject *tp, Py_ssize_t nitems)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000172{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000173 PyVarObject *op;
174 const size_t size = _PyObject_VAR_SIZE(tp, nitems);
175 op = (PyVarObject *) PyObject_MALLOC(size);
176 if (op == NULL)
177 return (PyVarObject *)PyErr_NoMemory();
178 return PyObject_INIT_VAR(op, tp, nitems);
Guido van Rossumb18618d2000-05-03 23:44:39 +0000179}
180
Antoine Pitrou796564c2013-07-30 19:59:21 +0200181void
182PyObject_CallFinalizer(PyObject *self)
183{
184 PyTypeObject *tp = Py_TYPE(self);
185
Antoine Pitrouada319b2019-05-29 22:12:38 +0200186 if (tp->tp_finalize == NULL)
Antoine Pitrou796564c2013-07-30 19:59:21 +0200187 return;
188 /* tp_finalize should only be called once. */
189 if (PyType_IS_GC(tp) && _PyGC_FINALIZED(self))
190 return;
191
192 tp->tp_finalize(self);
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900193 if (PyType_IS_GC(tp)) {
194 _PyGC_SET_FINALIZED(self);
195 }
Antoine Pitrou796564c2013-07-30 19:59:21 +0200196}
197
198int
199PyObject_CallFinalizerFromDealloc(PyObject *self)
200{
Victor Stinnera93c51e2020-02-07 00:38:59 +0100201 if (Py_REFCNT(self) != 0) {
Victor Stinner5eb8bff2020-01-30 09:01:07 +0100202 _PyObject_ASSERT_FAILED_MSG(self,
203 "PyObject_CallFinalizerFromDealloc called "
204 "on object with a non-zero refcount");
205 }
Antoine Pitrou796564c2013-07-30 19:59:21 +0200206
207 /* Temporarily resurrect the object. */
Victor Stinnerc86a1122020-02-07 01:24:29 +0100208 Py_SET_REFCNT(self, 1);
Antoine Pitrou796564c2013-07-30 19:59:21 +0200209
210 PyObject_CallFinalizer(self);
211
Victor Stinner24702042018-10-26 17:16:37 +0200212 _PyObject_ASSERT_WITH_MSG(self,
Victor Stinnera93c51e2020-02-07 00:38:59 +0100213 Py_REFCNT(self) > 0,
Victor Stinner24702042018-10-26 17:16:37 +0200214 "refcount is too small");
Victor Stinner5eb8bff2020-01-30 09:01:07 +0100215
216 /* Undo the temporary resurrection; can't use DECREF here, it would
217 * cause a recursive call. */
Victor Stinnerc86a1122020-02-07 01:24:29 +0100218 Py_SET_REFCNT(self, Py_REFCNT(self) - 1);
219 if (Py_REFCNT(self) == 0) {
Antoine Pitrou796564c2013-07-30 19:59:21 +0200220 return 0; /* this is the normal path out */
Victor Stinner5eb8bff2020-01-30 09:01:07 +0100221 }
Antoine Pitrou796564c2013-07-30 19:59:21 +0200222
223 /* tp_finalize resurrected it! Make it look like the original Py_DECREF
Victor Stinner5eb8bff2020-01-30 09:01:07 +0100224 * never happened. */
Victor Stinnera93c51e2020-02-07 00:38:59 +0100225 Py_ssize_t refcnt = Py_REFCNT(self);
Antoine Pitrou796564c2013-07-30 19:59:21 +0200226 _Py_NewReference(self);
Victor Stinnerc86a1122020-02-07 01:24:29 +0100227 Py_SET_REFCNT(self, refcnt);
Antoine Pitrou796564c2013-07-30 19:59:21 +0200228
Victor Stinner24702042018-10-26 17:16:37 +0200229 _PyObject_ASSERT(self,
230 (!PyType_IS_GC(Py_TYPE(self))
231 || _PyObject_GC_IS_TRACKED(self)));
Victor Stinner49932fe2020-02-03 17:55:05 +0100232 /* If Py_REF_DEBUG macro is defined, _Py_NewReference() increased
233 _Py_RefTotal, so we need to undo that. */
234#ifdef Py_REF_DEBUG
235 _Py_RefTotal--;
236#endif
Antoine Pitrou796564c2013-07-30 19:59:21 +0200237 return -1;
238}
239
Antoine Pitrouc47bd4a2010-07-27 22:08:27 +0000240int
241PyObject_Print(PyObject *op, FILE *fp, int flags)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000242{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000243 int ret = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000244 if (PyErr_CheckSignals())
245 return -1;
Guido van Rossum9b00dfa1998-04-28 16:06:54 +0000246#ifdef USE_STACKCHECK
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000247 if (PyOS_CheckStack()) {
248 PyErr_SetString(PyExc_MemoryError, "stack overflow");
249 return -1;
250 }
Guido van Rossum9b00dfa1998-04-28 16:06:54 +0000251#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000252 clearerr(fp); /* Clear any previous error condition */
253 if (op == NULL) {
254 Py_BEGIN_ALLOW_THREADS
255 fprintf(fp, "<nil>");
256 Py_END_ALLOW_THREADS
257 }
258 else {
Victor Stinnera93c51e2020-02-07 00:38:59 +0100259 if (Py_REFCNT(op) <= 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000260 /* XXX(twouters) cast refcount to long until %zd is
261 universally available */
262 Py_BEGIN_ALLOW_THREADS
263 fprintf(fp, "<refcnt %ld at %p>",
Victor Stinnera93c51e2020-02-07 00:38:59 +0100264 (long)Py_REFCNT(op), (void *)op);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000265 Py_END_ALLOW_THREADS
Victor Stinner3ec9af72018-10-26 02:12:34 +0200266 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000267 else {
268 PyObject *s;
269 if (flags & Py_PRINT_RAW)
270 s = PyObject_Str(op);
271 else
272 s = PyObject_Repr(op);
273 if (s == NULL)
274 ret = -1;
275 else if (PyBytes_Check(s)) {
276 fwrite(PyBytes_AS_STRING(s), 1,
277 PyBytes_GET_SIZE(s), fp);
278 }
279 else if (PyUnicode_Check(s)) {
280 PyObject *t;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200281 t = PyUnicode_AsEncodedString(s, "utf-8", "backslashreplace");
Zackery Spytzae62f012018-10-06 00:44:25 -0600282 if (t == NULL) {
283 ret = -1;
284 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000285 else {
286 fwrite(PyBytes_AS_STRING(t), 1,
287 PyBytes_GET_SIZE(t), fp);
Victor Stinnerba6b4302010-05-17 09:33:42 +0000288 Py_DECREF(t);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000289 }
290 }
291 else {
292 PyErr_Format(PyExc_TypeError,
293 "str() or repr() returned '%.100s'",
Victor Stinner58ac7002020-02-07 03:04:21 +0100294 Py_TYPE(s)->tp_name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000295 ret = -1;
296 }
297 Py_XDECREF(s);
298 }
299 }
300 if (ret == 0) {
301 if (ferror(fp)) {
Serhiy Storchaka55fe1ae2017-04-16 10:46:38 +0300302 PyErr_SetFromErrno(PyExc_OSError);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000303 clearerr(fp);
304 ret = -1;
305 }
306 }
307 return ret;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000308}
309
Guido van Rossum38938152006-08-21 23:36:26 +0000310/* For debugging convenience. Set a breakpoint here and call it from your DLL */
311void
Thomas Woutersb2137042007-02-01 18:02:27 +0000312_Py_BreakPoint(void)
Guido van Rossum38938152006-08-21 23:36:26 +0000313{
314}
315
Neal Norwitz1a997502003-01-13 20:13:12 +0000316
Victor Stinner4c409be2019-04-11 13:01:15 +0200317/* Heuristic checking if the object memory is uninitialized or deallocated.
318 Rely on the debug hooks on Python memory allocators:
319 see _PyMem_IsPtrFreed().
Victor Stinner82af0b62018-10-23 17:39:40 +0200320
321 The function can be used to prevent segmentation fault on dereferencing
Victor Stinner4c409be2019-04-11 13:01:15 +0200322 pointers like 0xDDDDDDDDDDDDDDDD. */
Victor Stinner82af0b62018-10-23 17:39:40 +0200323int
324_PyObject_IsFreed(PyObject *op)
325{
Victor Stinner58ac7002020-02-07 03:04:21 +0100326 if (_PyMem_IsPtrFreed(op) || _PyMem_IsPtrFreed(Py_TYPE(op))) {
Victor Stinner2cf5d322018-11-22 16:32:57 +0100327 return 1;
328 }
Victor Stinner2b00db62019-04-11 11:33:27 +0200329 /* ignore op->ob_ref: its value can have be modified
Victor Stinner82af0b62018-10-23 17:39:40 +0200330 by Py_INCREF() and Py_DECREF(). */
331#ifdef Py_TRACE_REFS
Pablo Galindo36e33c32019-10-08 00:43:14 +0100332 if (op->_ob_next != NULL && _PyMem_IsPtrFreed(op->_ob_next)) {
Victor Stinner2b00db62019-04-11 11:33:27 +0200333 return 1;
334 }
Pablo Galindo36e33c32019-10-08 00:43:14 +0100335 if (op->_ob_prev != NULL && _PyMem_IsPtrFreed(op->_ob_prev)) {
336 return 1;
337 }
Victor Stinner82af0b62018-10-23 17:39:40 +0200338#endif
Victor Stinner2b00db62019-04-11 11:33:27 +0200339 return 0;
Victor Stinner82af0b62018-10-23 17:39:40 +0200340}
341
342
Barry Warsaw9bf16442001-01-23 16:24:35 +0000343/* For debugging convenience. See Misc/gdbinit for some useful gdb hooks */
Guido van Rossum38938152006-08-21 23:36:26 +0000344void
345_PyObject_Dump(PyObject* op)
Barry Warsaw9bf16442001-01-23 16:24:35 +0000346{
Victor Stinner82af0b62018-10-23 17:39:40 +0200347 if (_PyObject_IsFreed(op)) {
348 /* It seems like the object memory has been freed:
349 don't access it to prevent a segmentation fault. */
Victor Stinnerb39afb72019-09-17 23:36:28 +0200350 fprintf(stderr, "<object at %p is freed>\n", op);
Victor Stinner68762572019-10-07 18:42:01 +0200351 fflush(stderr);
Victor Stinner2cf5d322018-11-22 16:32:57 +0100352 return;
Victor Stinner82af0b62018-10-23 17:39:40 +0200353 }
354
Victor Stinner68762572019-10-07 18:42:01 +0200355 /* first, write fields which are the least likely to crash */
356 fprintf(stderr, "object address : %p\n", (void *)op);
Victor Stinner82af0b62018-10-23 17:39:40 +0200357 /* XXX(twouters) cast refcount to long until %zd is
358 universally available */
Victor Stinnera93c51e2020-02-07 00:38:59 +0100359 fprintf(stderr, "object refcount : %ld\n", (long)Py_REFCNT(op));
Victor Stinner68762572019-10-07 18:42:01 +0200360 fflush(stderr);
361
362 PyTypeObject *type = Py_TYPE(op);
363 fprintf(stderr, "object type : %p\n", type);
364 fprintf(stderr, "object type name: %s\n",
365 type==NULL ? "NULL" : type->tp_name);
366
367 /* the most dangerous part */
368 fprintf(stderr, "object repr : ");
369 fflush(stderr);
370
371 PyGILState_STATE gil = PyGILState_Ensure();
372 PyObject *error_type, *error_value, *error_traceback;
373 PyErr_Fetch(&error_type, &error_value, &error_traceback);
374
375 (void)PyObject_Print(op, stderr, 0);
376 fflush(stderr);
377
378 PyErr_Restore(error_type, error_value, error_traceback);
379 PyGILState_Release(gil);
380
381 fprintf(stderr, "\n");
Victor Stinner82af0b62018-10-23 17:39:40 +0200382 fflush(stderr);
Barry Warsaw9bf16442001-01-23 16:24:35 +0000383}
Barry Warsaw903138f2001-01-23 16:33:18 +0000384
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000385PyObject *
Fred Drake100814d2000-07-09 15:48:49 +0000386PyObject_Repr(PyObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000387{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000388 PyObject *res;
389 if (PyErr_CheckSignals())
390 return NULL;
Guido van Rossum9b00dfa1998-04-28 16:06:54 +0000391#ifdef USE_STACKCHECK
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000392 if (PyOS_CheckStack()) {
393 PyErr_SetString(PyExc_MemoryError, "stack overflow");
394 return NULL;
395 }
Guido van Rossum9b00dfa1998-04-28 16:06:54 +0000396#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000397 if (v == NULL)
398 return PyUnicode_FromString("<NULL>");
399 if (Py_TYPE(v)->tp_repr == NULL)
400 return PyUnicode_FromFormat("<%s object at %p>",
Victor Stinner58ac7002020-02-07 03:04:21 +0100401 Py_TYPE(v)->tp_name, v);
Victor Stinner33824f62013-08-26 14:05:19 +0200402
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100403 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinner33824f62013-08-26 14:05:19 +0200404#ifdef Py_DEBUG
405 /* PyObject_Repr() must not be called with an exception set,
Victor Stinnera8cb5152017-01-18 14:12:51 +0100406 because it can clear it (directly or indirectly) and so the
Martin Panter9955a372015-10-07 10:26:23 +0000407 caller loses its exception */
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100408 assert(!_PyErr_Occurred(tstate));
Victor Stinner33824f62013-08-26 14:05:19 +0200409#endif
410
Serhiy Storchaka1fb72d22017-12-03 22:12:11 +0200411 /* It is possible for a type to have a tp_repr representation that loops
412 infinitely. */
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100413 if (_Py_EnterRecursiveCall(tstate,
414 " while getting the repr of an object")) {
Serhiy Storchaka1fb72d22017-12-03 22:12:11 +0200415 return NULL;
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100416 }
Victor Stinner58ac7002020-02-07 03:04:21 +0100417 res = (*Py_TYPE(v)->tp_repr)(v);
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100418 _Py_LeaveRecursiveCall(tstate);
419
420 if (res == NULL) {
Victor Stinner0a54cf12011-12-01 03:22:44 +0100421 return NULL;
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100422 }
Victor Stinner0a54cf12011-12-01 03:22:44 +0100423 if (!PyUnicode_Check(res)) {
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100424 _PyErr_Format(tstate, PyExc_TypeError,
425 "__repr__ returned non-string (type %.200s)",
Victor Stinner58ac7002020-02-07 03:04:21 +0100426 Py_TYPE(res)->tp_name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000427 Py_DECREF(res);
428 return NULL;
429 }
Victor Stinnerdb88ae52011-12-01 02:15:00 +0100430#ifndef Py_DEBUG
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100431 if (PyUnicode_READY(res) < 0) {
Victor Stinnerdb88ae52011-12-01 02:15:00 +0100432 return NULL;
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100433 }
Victor Stinnerdb88ae52011-12-01 02:15:00 +0100434#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000435 return res;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000436}
437
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000438PyObject *
Guido van Rossum98297ee2007-11-06 21:34:58 +0000439PyObject_Str(PyObject *v)
Guido van Rossumc6004111993-11-05 10:22:19 +0000440{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000441 PyObject *res;
442 if (PyErr_CheckSignals())
443 return NULL;
Guido van Rossum98297ee2007-11-06 21:34:58 +0000444#ifdef USE_STACKCHECK
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000445 if (PyOS_CheckStack()) {
446 PyErr_SetString(PyExc_MemoryError, "stack overflow");
447 return NULL;
448 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000449#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000450 if (v == NULL)
451 return PyUnicode_FromString("<NULL>");
452 if (PyUnicode_CheckExact(v)) {
Victor Stinnerdb88ae52011-12-01 02:15:00 +0100453#ifndef Py_DEBUG
Victor Stinner4ead7c72011-11-20 19:48:36 +0100454 if (PyUnicode_READY(v) < 0)
455 return NULL;
Victor Stinnerdb88ae52011-12-01 02:15:00 +0100456#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000457 Py_INCREF(v);
458 return v;
459 }
460 if (Py_TYPE(v)->tp_str == NULL)
461 return PyObject_Repr(v);
Guido van Rossum4f288ab2001-05-01 16:53:37 +0000462
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100463 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinner33824f62013-08-26 14:05:19 +0200464#ifdef Py_DEBUG
465 /* PyObject_Str() must not be called with an exception set,
Victor Stinnera8cb5152017-01-18 14:12:51 +0100466 because it can clear it (directly or indirectly) and so the
Nick Coghland979e432014-02-09 10:43:21 +1000467 caller loses its exception */
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100468 assert(!_PyErr_Occurred(tstate));
Victor Stinner33824f62013-08-26 14:05:19 +0200469#endif
470
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000471 /* It is possible for a type to have a tp_str representation that loops
472 infinitely. */
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100473 if (_Py_EnterRecursiveCall(tstate, " while getting the str of an object")) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000474 return NULL;
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100475 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000476 res = (*Py_TYPE(v)->tp_str)(v);
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100477 _Py_LeaveRecursiveCall(tstate);
478
479 if (res == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000480 return NULL;
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100481 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000482 if (!PyUnicode_Check(res)) {
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100483 _PyErr_Format(tstate, PyExc_TypeError,
484 "__str__ returned non-string (type %.200s)",
485 Py_TYPE(res)->tp_name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000486 Py_DECREF(res);
487 return NULL;
488 }
Victor Stinnerdb88ae52011-12-01 02:15:00 +0100489#ifndef Py_DEBUG
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100490 if (PyUnicode_READY(res) < 0) {
Victor Stinner4ead7c72011-11-20 19:48:36 +0100491 return NULL;
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100492 }
Victor Stinnerdb88ae52011-12-01 02:15:00 +0100493#endif
Victor Stinner4ead7c72011-11-20 19:48:36 +0100494 assert(_PyUnicode_CheckConsistency(res, 1));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000495 return res;
Neil Schemenauercf52c072005-08-12 17:34:58 +0000496}
497
Georg Brandl559e5d72008-06-11 18:37:52 +0000498PyObject *
499PyObject_ASCII(PyObject *v)
500{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000501 PyObject *repr, *ascii, *res;
Georg Brandl559e5d72008-06-11 18:37:52 +0000502
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000503 repr = PyObject_Repr(v);
504 if (repr == NULL)
505 return NULL;
Georg Brandl559e5d72008-06-11 18:37:52 +0000506
Victor Stinneraf037572013-04-14 18:44:10 +0200507 if (PyUnicode_IS_ASCII(repr))
508 return repr;
509
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000510 /* repr is guaranteed to be a PyUnicode object by PyObject_Repr */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200511 ascii = _PyUnicode_AsASCIIString(repr, "backslashreplace");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000512 Py_DECREF(repr);
513 if (ascii == NULL)
514 return NULL;
Georg Brandl559e5d72008-06-11 18:37:52 +0000515
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000516 res = PyUnicode_DecodeASCII(
517 PyBytes_AS_STRING(ascii),
518 PyBytes_GET_SIZE(ascii),
519 NULL);
520
521 Py_DECREF(ascii);
522 return res;
Georg Brandl559e5d72008-06-11 18:37:52 +0000523}
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000524
Benjamin Petersonc15a0732008-08-26 16:46:47 +0000525PyObject *
526PyObject_Bytes(PyObject *v)
527{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000528 PyObject *result, *func;
Benjamin Petersonc15a0732008-08-26 16:46:47 +0000529
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000530 if (v == NULL)
531 return PyBytes_FromString("<NULL>");
Benjamin Petersonc15a0732008-08-26 16:46:47 +0000532
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000533 if (PyBytes_CheckExact(v)) {
534 Py_INCREF(v);
535 return v;
536 }
Benjamin Petersonc15a0732008-08-26 16:46:47 +0000537
Benjamin Petersonce798522012-01-22 11:24:29 -0500538 func = _PyObject_LookupSpecial(v, &PyId___bytes__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000539 if (func != NULL) {
Victor Stinnerf17c3de2016-12-06 18:46:19 +0100540 result = _PyObject_CallNoArg(func);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000541 Py_DECREF(func);
542 if (result == NULL)
Benjamin Peterson41ece392010-09-11 16:39:57 +0000543 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000544 if (!PyBytes_Check(result)) {
Benjamin Peterson41ece392010-09-11 16:39:57 +0000545 PyErr_Format(PyExc_TypeError,
546 "__bytes__ returned non-bytes (type %.200s)",
547 Py_TYPE(result)->tp_name);
548 Py_DECREF(result);
549 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000550 }
551 return result;
552 }
553 else if (PyErr_Occurred())
554 return NULL;
555 return PyBytes_FromObject(v);
Benjamin Petersonc15a0732008-08-26 16:46:47 +0000556}
557
Jeroen Demeyerbf17d412019-11-05 16:48:04 +0100558
559/*
560def _PyObject_FunctionStr(x):
561 try:
562 qualname = x.__qualname__
563 except AttributeError:
564 return str(x)
565 try:
566 mod = x.__module__
567 if mod is not None and mod != 'builtins':
568 return f"{x.__module__}.{qualname}()"
569 except AttributeError:
570 pass
571 return qualname
572*/
573PyObject *
574_PyObject_FunctionStr(PyObject *x)
575{
576 _Py_IDENTIFIER(__module__);
577 _Py_IDENTIFIER(__qualname__);
578 _Py_IDENTIFIER(builtins);
579 assert(!PyErr_Occurred());
580 PyObject *qualname;
581 int ret = _PyObject_LookupAttrId(x, &PyId___qualname__, &qualname);
582 if (qualname == NULL) {
583 if (ret < 0) {
584 return NULL;
585 }
586 return PyObject_Str(x);
587 }
588 PyObject *module;
589 PyObject *result = NULL;
590 ret = _PyObject_LookupAttrId(x, &PyId___module__, &module);
591 if (module != NULL && module != Py_None) {
592 PyObject *builtinsname = _PyUnicode_FromId(&PyId_builtins);
593 if (builtinsname == NULL) {
594 goto done;
595 }
596 ret = PyObject_RichCompareBool(module, builtinsname, Py_NE);
597 if (ret < 0) {
598 // error
599 goto done;
600 }
601 if (ret > 0) {
602 result = PyUnicode_FromFormat("%S.%S()", module, qualname);
603 goto done;
604 }
605 }
606 else if (ret < 0) {
607 goto done;
608 }
609 result = PyUnicode_FromFormat("%S()", qualname);
610done:
611 Py_DECREF(qualname);
612 Py_XDECREF(module);
613 return result;
614}
615
Mark Dickinsonc008a172009-02-01 13:59:22 +0000616/* For Python 3.0.1 and later, the old three-way comparison has been
617 completely removed in favour of rich comparisons. PyObject_Compare() and
618 PyObject_Cmp() are gone, and the builtin cmp function no longer exists.
Jeroen Demeyer530f5062019-05-31 04:13:39 +0200619 The old tp_compare slot has been renamed to tp_as_async, and should no
Mark Dickinsonc008a172009-02-01 13:59:22 +0000620 longer be used. Use tp_richcompare instead.
Guido van Rossum98297ee2007-11-06 21:34:58 +0000621
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000622 See (*) below for practical amendments.
623
Mark Dickinsonc008a172009-02-01 13:59:22 +0000624 tp_richcompare gets called with a first argument of the appropriate type
625 and a second object of an arbitrary type. We never do any kind of
626 coercion.
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000627
Mark Dickinsonc008a172009-02-01 13:59:22 +0000628 The tp_richcompare slot should return an object, as follows:
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000629
630 NULL if an exception occurred
631 NotImplemented if the requested comparison is not implemented
632 any other false value if the requested comparison is false
633 any other true value if the requested comparison is true
634
635 The PyObject_RichCompare[Bool]() wrappers raise TypeError when they get
636 NotImplemented.
637
638 (*) Practical amendments:
639
640 - If rich comparison returns NotImplemented, == and != are decided by
641 comparing the object pointer (i.e. falling back to the base object
642 implementation).
643
Guido van Rossuma4073002002-05-31 20:03:54 +0000644*/
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000645
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000646/* Map rich comparison operators to their swapped version, e.g. LT <--> GT */
Brett Cannona5ca2e72004-09-25 01:37:24 +0000647int _Py_SwappedOp[] = {Py_GT, Py_GE, Py_EQ, Py_NE, Py_LT, Py_LE};
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +0000648
Serhiy Storchaka2d06e842015-12-25 19:53:18 +0200649static const char * const opstrings[] = {"<", "<=", "==", "!=", ">", ">="};
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000650
651/* Perform a rich comparison, raising TypeError when the requested comparison
652 operator is not supported. */
Guido van Rossume797ec12001-01-17 15:24:28 +0000653static PyObject *
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100654do_richcompare(PyThreadState *tstate, PyObject *v, PyObject *w, int op)
Guido van Rossume797ec12001-01-17 15:24:28 +0000655{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000656 richcmpfunc f;
657 PyObject *res;
658 int checked_reverse_op = 0;
Guido van Rossume797ec12001-01-17 15:24:28 +0000659
Andy Lester55728702020-03-06 16:53:17 -0600660 if (!Py_IS_TYPE(v, Py_TYPE(w)) &&
Victor Stinner58ac7002020-02-07 03:04:21 +0100661 PyType_IsSubtype(Py_TYPE(w), Py_TYPE(v)) &&
662 (f = Py_TYPE(w)->tp_richcompare) != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000663 checked_reverse_op = 1;
664 res = (*f)(w, v, _Py_SwappedOp[op]);
665 if (res != Py_NotImplemented)
666 return res;
667 Py_DECREF(res);
668 }
Victor Stinner58ac7002020-02-07 03:04:21 +0100669 if ((f = Py_TYPE(v)->tp_richcompare) != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000670 res = (*f)(v, w, op);
671 if (res != Py_NotImplemented)
672 return res;
673 Py_DECREF(res);
674 }
Victor Stinner58ac7002020-02-07 03:04:21 +0100675 if (!checked_reverse_op && (f = Py_TYPE(w)->tp_richcompare) != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000676 res = (*f)(w, v, _Py_SwappedOp[op]);
677 if (res != Py_NotImplemented)
678 return res;
679 Py_DECREF(res);
680 }
681 /* If neither object implements it, provide a sensible default
682 for == and !=, but raise an exception for ordering. */
683 switch (op) {
684 case Py_EQ:
685 res = (v == w) ? Py_True : Py_False;
686 break;
687 case Py_NE:
688 res = (v != w) ? Py_True : Py_False;
689 break;
690 default:
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100691 _PyErr_Format(tstate, PyExc_TypeError,
692 "'%s' not supported between instances of '%.100s' and '%.100s'",
693 opstrings[op],
Victor Stinner58ac7002020-02-07 03:04:21 +0100694 Py_TYPE(v)->tp_name,
695 Py_TYPE(w)->tp_name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000696 return NULL;
697 }
698 Py_INCREF(res);
699 return res;
Guido van Rossume797ec12001-01-17 15:24:28 +0000700}
701
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000702/* Perform a rich comparison with object result. This wraps do_richcompare()
703 with a check for NULL arguments and a recursion check. */
Guido van Rossume797ec12001-01-17 15:24:28 +0000704
Guido van Rossume797ec12001-01-17 15:24:28 +0000705PyObject *
706PyObject_RichCompare(PyObject *v, PyObject *w, int op)
707{
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100708 PyThreadState *tstate = _PyThreadState_GET();
Guido van Rossume797ec12001-01-17 15:24:28 +0000709
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000710 assert(Py_LT <= op && op <= Py_GE);
711 if (v == NULL || w == NULL) {
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100712 if (!_PyErr_Occurred(tstate)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000713 PyErr_BadInternalCall();
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100714 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000715 return NULL;
716 }
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100717 if (_Py_EnterRecursiveCall(tstate, " in comparison")) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000718 return NULL;
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100719 }
720 PyObject *res = do_richcompare(tstate, v, w, op);
721 _Py_LeaveRecursiveCall(tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000722 return res;
Guido van Rossume797ec12001-01-17 15:24:28 +0000723}
724
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000725/* Perform a rich comparison with integer result. This wraps
726 PyObject_RichCompare(), returning -1 for error, 0 for false, 1 for true. */
Guido van Rossume797ec12001-01-17 15:24:28 +0000727int
728PyObject_RichCompareBool(PyObject *v, PyObject *w, int op)
729{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000730 PyObject *res;
731 int ok;
Guido van Rossume797ec12001-01-17 15:24:28 +0000732
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000733 /* Quick result when objects are the same.
734 Guarantees that identity implies equality. */
735 if (v == w) {
736 if (op == Py_EQ)
737 return 1;
738 else if (op == Py_NE)
739 return 0;
740 }
Mark Dickinson4a1f5932008-11-12 23:23:36 +0000741
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000742 res = PyObject_RichCompare(v, w, op);
743 if (res == NULL)
744 return -1;
745 if (PyBool_Check(res))
746 ok = (res == Py_True);
747 else
748 ok = PyObject_IsTrue(res);
749 Py_DECREF(res);
750 return ok;
Guido van Rossume797ec12001-01-17 15:24:28 +0000751}
Fred Drake13634cf2000-06-29 19:17:04 +0000752
Antoine Pitrouce4a9da2011-11-21 20:46:33 +0100753Py_hash_t
Nick Coghland1abd252008-07-15 15:46:38 +0000754PyObject_HashNotImplemented(PyObject *v)
755{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000756 PyErr_Format(PyExc_TypeError, "unhashable type: '%.200s'",
757 Py_TYPE(v)->tp_name);
758 return -1;
Nick Coghland1abd252008-07-15 15:46:38 +0000759}
Fred Drake13634cf2000-06-29 19:17:04 +0000760
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000761Py_hash_t
Fred Drake100814d2000-07-09 15:48:49 +0000762PyObject_Hash(PyObject *v)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000763{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000764 PyTypeObject *tp = Py_TYPE(v);
765 if (tp->tp_hash != NULL)
766 return (*tp->tp_hash)(v);
767 /* To keep to the general practice that inheriting
768 * solely from object in C code should work without
769 * an explicit call to PyType_Ready, we implicitly call
770 * PyType_Ready here and then check the tp_hash slot again
771 */
772 if (tp->tp_dict == NULL) {
773 if (PyType_Ready(tp) < 0)
774 return -1;
775 if (tp->tp_hash != NULL)
776 return (*tp->tp_hash)(v);
777 }
778 /* Otherwise, the object can't be hashed */
779 return PyObject_HashNotImplemented(v);
Guido van Rossum9bfef441993-03-29 10:43:31 +0000780}
781
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000782PyObject *
Jeremy Hyltonaf68c872005-12-10 18:50:16 +0000783PyObject_GetAttrString(PyObject *v, const char *name)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000784{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000785 PyObject *w, *res;
Guido van Rossumd8eb1b31996-08-09 20:52:03 +0000786
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000787 if (Py_TYPE(v)->tp_getattr != NULL)
788 return (*Py_TYPE(v)->tp_getattr)(v, (char*)name);
INADA Naoki3e8d6cb2017-02-21 23:57:25 +0900789 w = PyUnicode_FromString(name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000790 if (w == NULL)
791 return NULL;
792 res = PyObject_GetAttr(v, w);
Victor Stinner59af08f2012-03-22 02:09:08 +0100793 Py_DECREF(w);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000794 return res;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000795}
796
797int
Jeremy Hyltonaf68c872005-12-10 18:50:16 +0000798PyObject_HasAttrString(PyObject *v, const char *name)
Guido van Rossumed18fdc1993-07-11 19:55:34 +0000799{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000800 PyObject *res = PyObject_GetAttrString(v, name);
801 if (res != NULL) {
802 Py_DECREF(res);
803 return 1;
804 }
805 PyErr_Clear();
806 return 0;
Guido van Rossumed18fdc1993-07-11 19:55:34 +0000807}
808
809int
Jeremy Hyltonaf68c872005-12-10 18:50:16 +0000810PyObject_SetAttrString(PyObject *v, const char *name, PyObject *w)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000811{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000812 PyObject *s;
813 int res;
Guido van Rossumd8eb1b31996-08-09 20:52:03 +0000814
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000815 if (Py_TYPE(v)->tp_setattr != NULL)
816 return (*Py_TYPE(v)->tp_setattr)(v, (char*)name, w);
817 s = PyUnicode_InternFromString(name);
818 if (s == NULL)
819 return -1;
820 res = PyObject_SetAttr(v, s, w);
821 Py_XDECREF(s);
822 return res;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000823}
824
Benjamin Petersonbfebb7b2011-12-15 15:34:02 -0500825int
826_PyObject_IsAbstract(PyObject *obj)
827{
828 int res;
829 PyObject* isabstract;
Benjamin Petersonbfebb7b2011-12-15 15:34:02 -0500830
831 if (obj == NULL)
832 return 0;
833
Serhiy Storchakaf320be72018-01-25 10:49:40 +0200834 res = _PyObject_LookupAttrId(obj, &PyId___isabstractmethod__, &isabstract);
835 if (res > 0) {
836 res = PyObject_IsTrue(isabstract);
837 Py_DECREF(isabstract);
Benjamin Petersonbfebb7b2011-12-15 15:34:02 -0500838 }
Benjamin Petersonbfebb7b2011-12-15 15:34:02 -0500839 return res;
840}
841
Guido van Rossum98ff96a1997-05-20 18:34:44 +0000842PyObject *
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200843_PyObject_GetAttrId(PyObject *v, _Py_Identifier *name)
844{
845 PyObject *result;
Martin v. Löwisd10759f2011-11-07 13:00:05 +0100846 PyObject *oname = _PyUnicode_FromId(name); /* borrowed */
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200847 if (!oname)
848 return NULL;
849 result = PyObject_GetAttr(v, oname);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200850 return result;
851}
852
853int
854_PyObject_HasAttrId(PyObject *v, _Py_Identifier *name)
855{
856 int result;
Martin v. Löwisd10759f2011-11-07 13:00:05 +0100857 PyObject *oname = _PyUnicode_FromId(name); /* borrowed */
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200858 if (!oname)
859 return -1;
860 result = PyObject_HasAttr(v, oname);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200861 return result;
862}
863
864int
865_PyObject_SetAttrId(PyObject *v, _Py_Identifier *name, PyObject *w)
866{
867 int result;
Martin v. Löwisd10759f2011-11-07 13:00:05 +0100868 PyObject *oname = _PyUnicode_FromId(name); /* borrowed */
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200869 if (!oname)
870 return -1;
871 result = PyObject_SetAttr(v, oname, w);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200872 return result;
873}
874
875PyObject *
Fred Drake100814d2000-07-09 15:48:49 +0000876PyObject_GetAttr(PyObject *v, PyObject *name)
Guido van Rossum98ff96a1997-05-20 18:34:44 +0000877{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000878 PyTypeObject *tp = Py_TYPE(v);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000879
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000880 if (!PyUnicode_Check(name)) {
881 PyErr_Format(PyExc_TypeError,
882 "attribute name must be string, not '%.200s'",
Victor Stinner58ac7002020-02-07 03:04:21 +0100883 Py_TYPE(name)->tp_name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000884 return NULL;
885 }
886 if (tp->tp_getattro != NULL)
887 return (*tp->tp_getattro)(v, name);
888 if (tp->tp_getattr != NULL) {
Serhiy Storchaka2a404b62017-01-22 23:07:07 +0200889 const char *name_str = PyUnicode_AsUTF8(name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000890 if (name_str == NULL)
891 return NULL;
Serhiy Storchaka2a404b62017-01-22 23:07:07 +0200892 return (*tp->tp_getattr)(v, (char *)name_str);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000893 }
894 PyErr_Format(PyExc_AttributeError,
895 "'%.50s' object has no attribute '%U'",
896 tp->tp_name, name);
897 return NULL;
Guido van Rossum98ff96a1997-05-20 18:34:44 +0000898}
899
Serhiy Storchakaf320be72018-01-25 10:49:40 +0200900int
901_PyObject_LookupAttr(PyObject *v, PyObject *name, PyObject **result)
INADA Naoki378edee2018-01-16 20:52:41 +0900902{
903 PyTypeObject *tp = Py_TYPE(v);
INADA Naoki378edee2018-01-16 20:52:41 +0900904
905 if (!PyUnicode_Check(name)) {
906 PyErr_Format(PyExc_TypeError,
907 "attribute name must be string, not '%.200s'",
Victor Stinner58ac7002020-02-07 03:04:21 +0100908 Py_TYPE(name)->tp_name);
Serhiy Storchakaf320be72018-01-25 10:49:40 +0200909 *result = NULL;
910 return -1;
INADA Naoki378edee2018-01-16 20:52:41 +0900911 }
912
913 if (tp->tp_getattro == PyObject_GenericGetAttr) {
Serhiy Storchakaf320be72018-01-25 10:49:40 +0200914 *result = _PyObject_GenericGetAttrWithDict(v, name, NULL, 1);
915 if (*result != NULL) {
916 return 1;
917 }
918 if (PyErr_Occurred()) {
919 return -1;
920 }
921 return 0;
INADA Naoki378edee2018-01-16 20:52:41 +0900922 }
923 if (tp->tp_getattro != NULL) {
Serhiy Storchakaf320be72018-01-25 10:49:40 +0200924 *result = (*tp->tp_getattro)(v, name);
INADA Naoki378edee2018-01-16 20:52:41 +0900925 }
926 else if (tp->tp_getattr != NULL) {
927 const char *name_str = PyUnicode_AsUTF8(name);
Serhiy Storchakaf320be72018-01-25 10:49:40 +0200928 if (name_str == NULL) {
929 *result = NULL;
930 return -1;
931 }
932 *result = (*tp->tp_getattr)(v, (char *)name_str);
INADA Naoki378edee2018-01-16 20:52:41 +0900933 }
INADA Naokie76daeb2018-01-26 16:22:51 +0900934 else {
935 *result = NULL;
936 return 0;
937 }
938
Serhiy Storchakaf320be72018-01-25 10:49:40 +0200939 if (*result != NULL) {
940 return 1;
INADA Naoki378edee2018-01-16 20:52:41 +0900941 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +0200942 if (!PyErr_ExceptionMatches(PyExc_AttributeError)) {
943 return -1;
944 }
945 PyErr_Clear();
946 return 0;
947}
948
949int
950_PyObject_LookupAttrId(PyObject *v, _Py_Identifier *name, PyObject **result)
951{
952 PyObject *oname = _PyUnicode_FromId(name); /* borrowed */
953 if (!oname) {
954 *result = NULL;
955 return -1;
956 }
957 return _PyObject_LookupAttr(v, oname, result);
INADA Naoki378edee2018-01-16 20:52:41 +0900958}
959
Guido van Rossum98ff96a1997-05-20 18:34:44 +0000960int
Fred Drake100814d2000-07-09 15:48:49 +0000961PyObject_HasAttr(PyObject *v, PyObject *name)
Guido van Rossum98ff96a1997-05-20 18:34:44 +0000962{
Serhiy Storchakaf320be72018-01-25 10:49:40 +0200963 PyObject *res;
964 if (_PyObject_LookupAttr(v, name, &res) < 0) {
965 PyErr_Clear();
966 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000967 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +0200968 if (res == NULL) {
969 return 0;
970 }
971 Py_DECREF(res);
972 return 1;
Guido van Rossum98ff96a1997-05-20 18:34:44 +0000973}
974
975int
Fred Drake100814d2000-07-09 15:48:49 +0000976PyObject_SetAttr(PyObject *v, PyObject *name, PyObject *value)
Guido van Rossum98ff96a1997-05-20 18:34:44 +0000977{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000978 PyTypeObject *tp = Py_TYPE(v);
979 int err;
Marc-André Lemburge44e5072000-09-18 16:20:57 +0000980
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000981 if (!PyUnicode_Check(name)) {
982 PyErr_Format(PyExc_TypeError,
983 "attribute name must be string, not '%.200s'",
Victor Stinner58ac7002020-02-07 03:04:21 +0100984 Py_TYPE(name)->tp_name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000985 return -1;
986 }
987 Py_INCREF(name);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000988
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000989 PyUnicode_InternInPlace(&name);
990 if (tp->tp_setattro != NULL) {
991 err = (*tp->tp_setattro)(v, name, value);
992 Py_DECREF(name);
993 return err;
994 }
995 if (tp->tp_setattr != NULL) {
Serhiy Storchaka2a404b62017-01-22 23:07:07 +0200996 const char *name_str = PyUnicode_AsUTF8(name);
Zackery Spytze0dcb852019-04-28 06:58:52 -0600997 if (name_str == NULL) {
998 Py_DECREF(name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000999 return -1;
Zackery Spytze0dcb852019-04-28 06:58:52 -06001000 }
Serhiy Storchaka2a404b62017-01-22 23:07:07 +02001001 err = (*tp->tp_setattr)(v, (char *)name_str, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001002 Py_DECREF(name);
1003 return err;
1004 }
1005 Py_DECREF(name);
Victor Stinnera93c51e2020-02-07 00:38:59 +01001006 _PyObject_ASSERT(name, Py_REFCNT(name) >= 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001007 if (tp->tp_getattr == NULL && tp->tp_getattro == NULL)
1008 PyErr_Format(PyExc_TypeError,
1009 "'%.100s' object has no attributes "
1010 "(%s .%U)",
1011 tp->tp_name,
1012 value==NULL ? "del" : "assign to",
1013 name);
1014 else
1015 PyErr_Format(PyExc_TypeError,
1016 "'%.100s' object has only read-only attributes "
1017 "(%s .%U)",
1018 tp->tp_name,
1019 value==NULL ? "del" : "assign to",
1020 name);
1021 return -1;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001022}
1023
1024/* Helper to get a pointer to an object's __dict__ slot, if any */
1025
1026PyObject **
1027_PyObject_GetDictPtr(PyObject *obj)
1028{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001029 Py_ssize_t dictoffset;
1030 PyTypeObject *tp = Py_TYPE(obj);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001031
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001032 dictoffset = tp->tp_dictoffset;
1033 if (dictoffset == 0)
1034 return NULL;
1035 if (dictoffset < 0) {
Victor Stinnerc65b3202020-02-07 11:18:33 +01001036 Py_ssize_t tsize = Py_SIZE(obj);
1037 if (tsize < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001038 tsize = -tsize;
Victor Stinnerc65b3202020-02-07 11:18:33 +01001039 }
1040 size_t size = _PyObject_VAR_SIZE(tp, tsize);
Guido van Rossum2eb0b872002-03-01 22:24:49 +00001041
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001042 dictoffset += (long)size;
Victor Stinner24702042018-10-26 17:16:37 +02001043 _PyObject_ASSERT(obj, dictoffset > 0);
1044 _PyObject_ASSERT(obj, dictoffset % SIZEOF_VOID_P == 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001045 }
1046 return (PyObject **) ((char *)obj + dictoffset);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001047}
1048
Tim Peters6d6c1a32001-08-02 04:15:00 +00001049PyObject *
Raymond Hettinger1da1dbf2003-03-17 19:46:11 +00001050PyObject_SelfIter(PyObject *obj)
Raymond Hettinger01538262003-03-17 08:24:35 +00001051{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001052 Py_INCREF(obj);
1053 return obj;
Raymond Hettinger01538262003-03-17 08:24:35 +00001054}
1055
Amaury Forgeot d'Arcf343e012009-01-12 23:58:21 +00001056/* Helper used when the __next__ method is removed from a type:
1057 tp_iternext is never NULL and can be safely called without checking
1058 on every iteration.
1059 */
1060
1061PyObject *
1062_PyObject_NextNotImplemented(PyObject *self)
1063{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001064 PyErr_Format(PyExc_TypeError,
1065 "'%.200s' object is not iterable",
1066 Py_TYPE(self)->tp_name);
1067 return NULL;
Amaury Forgeot d'Arcf343e012009-01-12 23:58:21 +00001068}
1069
Yury Selivanovf2392132016-12-13 19:03:51 -05001070
1071/* Specialized version of _PyObject_GenericGetAttrWithDict
1072 specifically for the LOAD_METHOD opcode.
1073
1074 Return 1 if a method is found, 0 if it's a regular attribute
1075 from __dict__ or something returned by using a descriptor
1076 protocol.
1077
1078 `method` will point to the resolved attribute or NULL. In the
1079 latter case, an error will be set.
1080*/
1081int
1082_PyObject_GetMethod(PyObject *obj, PyObject *name, PyObject **method)
1083{
1084 PyTypeObject *tp = Py_TYPE(obj);
1085 PyObject *descr;
1086 descrgetfunc f = NULL;
1087 PyObject **dictptr, *dict;
1088 PyObject *attr;
1089 int meth_found = 0;
1090
1091 assert(*method == NULL);
1092
1093 if (Py_TYPE(obj)->tp_getattro != PyObject_GenericGetAttr
1094 || !PyUnicode_Check(name)) {
1095 *method = PyObject_GetAttr(obj, name);
1096 return 0;
1097 }
1098
1099 if (tp->tp_dict == NULL && PyType_Ready(tp) < 0)
1100 return 0;
1101
1102 descr = _PyType_Lookup(tp, name);
1103 if (descr != NULL) {
1104 Py_INCREF(descr);
Jeroen Demeyereb65e242019-05-28 14:42:53 +02001105 if (PyType_HasFeature(Py_TYPE(descr), Py_TPFLAGS_METHOD_DESCRIPTOR)) {
Yury Selivanovf2392132016-12-13 19:03:51 -05001106 meth_found = 1;
1107 } else {
Victor Stinner58ac7002020-02-07 03:04:21 +01001108 f = Py_TYPE(descr)->tp_descr_get;
Yury Selivanovf2392132016-12-13 19:03:51 -05001109 if (f != NULL && PyDescr_IsData(descr)) {
Victor Stinner58ac7002020-02-07 03:04:21 +01001110 *method = f(descr, obj, (PyObject *)Py_TYPE(obj));
Yury Selivanovf2392132016-12-13 19:03:51 -05001111 Py_DECREF(descr);
1112 return 0;
1113 }
1114 }
1115 }
1116
1117 dictptr = _PyObject_GetDictPtr(obj);
1118 if (dictptr != NULL && (dict = *dictptr) != NULL) {
1119 Py_INCREF(dict);
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02001120 attr = PyDict_GetItemWithError(dict, name);
Yury Selivanovf2392132016-12-13 19:03:51 -05001121 if (attr != NULL) {
1122 Py_INCREF(attr);
1123 *method = attr;
1124 Py_DECREF(dict);
1125 Py_XDECREF(descr);
1126 return 0;
1127 }
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02001128 else {
1129 Py_DECREF(dict);
1130 if (PyErr_Occurred()) {
1131 Py_XDECREF(descr);
1132 return 0;
1133 }
1134 }
Yury Selivanovf2392132016-12-13 19:03:51 -05001135 }
1136
1137 if (meth_found) {
1138 *method = descr;
1139 return 1;
1140 }
1141
1142 if (f != NULL) {
1143 *method = f(descr, obj, (PyObject *)Py_TYPE(obj));
1144 Py_DECREF(descr);
1145 return 0;
1146 }
1147
1148 if (descr != NULL) {
1149 *method = descr;
1150 return 0;
1151 }
1152
1153 PyErr_Format(PyExc_AttributeError,
1154 "'%.50s' object has no attribute '%U'",
1155 tp->tp_name, name);
1156 return 0;
1157}
1158
1159/* Generic GetAttr functions - put these in your tp_[gs]etattro slot. */
Michael W. Hudson1593f502004-09-14 17:09:47 +00001160
Raymond Hettinger01538262003-03-17 08:24:35 +00001161PyObject *
INADA Naoki378edee2018-01-16 20:52:41 +09001162_PyObject_GenericGetAttrWithDict(PyObject *obj, PyObject *name,
1163 PyObject *dict, int suppress)
Tim Peters6d6c1a32001-08-02 04:15:00 +00001164{
Yury Selivanovf2392132016-12-13 19:03:51 -05001165 /* Make sure the logic of _PyObject_GetMethod is in sync with
1166 this method.
INADA Naoki378edee2018-01-16 20:52:41 +09001167
1168 When suppress=1, this function suppress AttributeError.
Yury Selivanovf2392132016-12-13 19:03:51 -05001169 */
1170
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001171 PyTypeObject *tp = Py_TYPE(obj);
1172 PyObject *descr = NULL;
1173 PyObject *res = NULL;
1174 descrgetfunc f;
1175 Py_ssize_t dictoffset;
1176 PyObject **dictptr;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001177
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001178 if (!PyUnicode_Check(name)){
1179 PyErr_Format(PyExc_TypeError,
1180 "attribute name must be string, not '%.200s'",
Victor Stinner58ac7002020-02-07 03:04:21 +01001181 Py_TYPE(name)->tp_name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001182 return NULL;
1183 }
Serhiy Storchaka55c861f2016-04-17 20:31:51 +03001184 Py_INCREF(name);
Guido van Rossumebca9fc2001-12-04 15:54:53 +00001185
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001186 if (tp->tp_dict == NULL) {
1187 if (PyType_Ready(tp) < 0)
1188 goto done;
1189 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001190
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001191 descr = _PyType_Lookup(tp, name);
Michael W. Hudsonb2c7de42003-08-15 13:07:47 +00001192
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001193 f = NULL;
1194 if (descr != NULL) {
Serhiy Storchaka55c861f2016-04-17 20:31:51 +03001195 Py_INCREF(descr);
Victor Stinner58ac7002020-02-07 03:04:21 +01001196 f = Py_TYPE(descr)->tp_descr_get;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001197 if (f != NULL && PyDescr_IsData(descr)) {
Victor Stinner58ac7002020-02-07 03:04:21 +01001198 res = f(descr, obj, (PyObject *)Py_TYPE(obj));
INADA Naoki378edee2018-01-16 20:52:41 +09001199 if (res == NULL && suppress &&
1200 PyErr_ExceptionMatches(PyExc_AttributeError)) {
1201 PyErr_Clear();
1202 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001203 goto done;
1204 }
1205 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001206
Antoine Pitrou1a9a9d52010-08-28 18:17:03 +00001207 if (dict == NULL) {
1208 /* Inline _PyObject_GetDictPtr */
1209 dictoffset = tp->tp_dictoffset;
1210 if (dictoffset != 0) {
1211 if (dictoffset < 0) {
Victor Stinnerc65b3202020-02-07 11:18:33 +01001212 Py_ssize_t tsize = Py_SIZE(obj);
1213 if (tsize < 0) {
Antoine Pitrou1a9a9d52010-08-28 18:17:03 +00001214 tsize = -tsize;
Victor Stinnerc65b3202020-02-07 11:18:33 +01001215 }
1216 size_t size = _PyObject_VAR_SIZE(tp, tsize);
Victor Stinner24702042018-10-26 17:16:37 +02001217 _PyObject_ASSERT(obj, size <= PY_SSIZE_T_MAX);
Guido van Rossumc66ff442002-08-19 16:50:48 +00001218
Serhiy Storchaka55c861f2016-04-17 20:31:51 +03001219 dictoffset += (Py_ssize_t)size;
Victor Stinner24702042018-10-26 17:16:37 +02001220 _PyObject_ASSERT(obj, dictoffset > 0);
1221 _PyObject_ASSERT(obj, dictoffset % SIZEOF_VOID_P == 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001222 }
Antoine Pitrou1a9a9d52010-08-28 18:17:03 +00001223 dictptr = (PyObject **) ((char *)obj + dictoffset);
1224 dict = *dictptr;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001225 }
1226 }
Antoine Pitrou1a9a9d52010-08-28 18:17:03 +00001227 if (dict != NULL) {
1228 Py_INCREF(dict);
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02001229 res = PyDict_GetItemWithError(dict, name);
Antoine Pitrou1a9a9d52010-08-28 18:17:03 +00001230 if (res != NULL) {
1231 Py_INCREF(res);
Antoine Pitrou1a9a9d52010-08-28 18:17:03 +00001232 Py_DECREF(dict);
1233 goto done;
1234 }
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02001235 else {
1236 Py_DECREF(dict);
1237 if (PyErr_Occurred()) {
1238 if (suppress && PyErr_ExceptionMatches(PyExc_AttributeError)) {
1239 PyErr_Clear();
1240 }
1241 else {
1242 goto done;
1243 }
1244 }
1245 }
Antoine Pitrou1a9a9d52010-08-28 18:17:03 +00001246 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001247
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001248 if (f != NULL) {
1249 res = f(descr, obj, (PyObject *)Py_TYPE(obj));
INADA Naoki378edee2018-01-16 20:52:41 +09001250 if (res == NULL && suppress &&
1251 PyErr_ExceptionMatches(PyExc_AttributeError)) {
1252 PyErr_Clear();
1253 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001254 goto done;
1255 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001256
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001257 if (descr != NULL) {
1258 res = descr;
Victor Stinner2d01dc02012-03-09 00:44:13 +01001259 descr = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001260 goto done;
1261 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001262
INADA Naoki378edee2018-01-16 20:52:41 +09001263 if (!suppress) {
1264 PyErr_Format(PyExc_AttributeError,
1265 "'%.50s' object has no attribute '%U'",
1266 tp->tp_name, name);
1267 }
Guido van Rossumebca9fc2001-12-04 15:54:53 +00001268 done:
Victor Stinner2d01dc02012-03-09 00:44:13 +01001269 Py_XDECREF(descr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001270 Py_DECREF(name);
1271 return res;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001272}
1273
Antoine Pitrou1a9a9d52010-08-28 18:17:03 +00001274PyObject *
1275PyObject_GenericGetAttr(PyObject *obj, PyObject *name)
1276{
INADA Naoki378edee2018-01-16 20:52:41 +09001277 return _PyObject_GenericGetAttrWithDict(obj, name, NULL, 0);
Antoine Pitrou1a9a9d52010-08-28 18:17:03 +00001278}
1279
Tim Peters6d6c1a32001-08-02 04:15:00 +00001280int
Antoine Pitrou1a9a9d52010-08-28 18:17:03 +00001281_PyObject_GenericSetAttrWithDict(PyObject *obj, PyObject *name,
1282 PyObject *value, PyObject *dict)
Tim Peters6d6c1a32001-08-02 04:15:00 +00001283{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001284 PyTypeObject *tp = Py_TYPE(obj);
1285 PyObject *descr;
1286 descrsetfunc f;
1287 PyObject **dictptr;
1288 int res = -1;
Guido van Rossumebca9fc2001-12-04 15:54:53 +00001289
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001290 if (!PyUnicode_Check(name)){
1291 PyErr_Format(PyExc_TypeError,
1292 "attribute name must be string, not '%.200s'",
Victor Stinner58ac7002020-02-07 03:04:21 +01001293 Py_TYPE(name)->tp_name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001294 return -1;
1295 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001296
Benjamin Peterson74529ad2012-03-09 07:25:32 -08001297 if (tp->tp_dict == NULL && PyType_Ready(tp) < 0)
1298 return -1;
1299
1300 Py_INCREF(name);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001301
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001302 descr = _PyType_Lookup(tp, name);
Victor Stinner2d01dc02012-03-09 00:44:13 +01001303
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001304 if (descr != NULL) {
Serhiy Storchaka55c861f2016-04-17 20:31:51 +03001305 Py_INCREF(descr);
Victor Stinner58ac7002020-02-07 03:04:21 +01001306 f = Py_TYPE(descr)->tp_descr_set;
Serhiy Storchaka55c861f2016-04-17 20:31:51 +03001307 if (f != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001308 res = f(descr, obj, value);
1309 goto done;
1310 }
1311 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001312
Steve Dowerb82e17e2019-05-23 08:45:22 -07001313 /* XXX [Steve Dower] These are really noisy - worth it? */
1314 /*if (PyType_Check(obj) || PyModule_Check(obj)) {
1315 if (value && PySys_Audit("object.__setattr__", "OOO", obj, name, value) < 0)
1316 return -1;
1317 if (!value && PySys_Audit("object.__delattr__", "OO", obj, name) < 0)
1318 return -1;
1319 }*/
1320
Antoine Pitrou1a9a9d52010-08-28 18:17:03 +00001321 if (dict == NULL) {
1322 dictptr = _PyObject_GetDictPtr(obj);
Serhiy Storchaka55c861f2016-04-17 20:31:51 +03001323 if (dictptr == NULL) {
1324 if (descr == NULL) {
1325 PyErr_Format(PyExc_AttributeError,
1326 "'%.100s' object has no attribute '%U'",
1327 tp->tp_name, name);
1328 }
1329 else {
1330 PyErr_Format(PyExc_AttributeError,
1331 "'%.50s' object attribute '%U' is read-only",
1332 tp->tp_name, name);
1333 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001334 goto done;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001335 }
Serhiy Storchaka55c861f2016-04-17 20:31:51 +03001336 res = _PyObjectDict_SetItem(tp, dictptr, name, value);
Antoine Pitrou1a9a9d52010-08-28 18:17:03 +00001337 }
Serhiy Storchaka55c861f2016-04-17 20:31:51 +03001338 else {
Antoine Pitrou1a9a9d52010-08-28 18:17:03 +00001339 Py_INCREF(dict);
1340 if (value == NULL)
1341 res = PyDict_DelItem(dict, name);
1342 else
1343 res = PyDict_SetItem(dict, name, value);
Benjamin Peterson74529ad2012-03-09 07:25:32 -08001344 Py_DECREF(dict);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001345 }
Serhiy Storchaka55c861f2016-04-17 20:31:51 +03001346 if (res < 0 && PyErr_ExceptionMatches(PyExc_KeyError))
1347 PyErr_SetObject(PyExc_AttributeError, name);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001348
Guido van Rossumebca9fc2001-12-04 15:54:53 +00001349 done:
Victor Stinner2d01dc02012-03-09 00:44:13 +01001350 Py_XDECREF(descr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001351 Py_DECREF(name);
1352 return res;
Guido van Rossum98ff96a1997-05-20 18:34:44 +00001353}
1354
Antoine Pitrou1a9a9d52010-08-28 18:17:03 +00001355int
1356PyObject_GenericSetAttr(PyObject *obj, PyObject *name, PyObject *value)
1357{
1358 return _PyObject_GenericSetAttrWithDict(obj, name, value, NULL);
1359}
1360
Benjamin Peterson8eb12692012-02-19 19:59:10 -05001361int
1362PyObject_GenericSetDict(PyObject *obj, PyObject *value, void *context)
1363{
Serhiy Storchaka576f1322016-01-05 21:27:54 +02001364 PyObject **dictptr = _PyObject_GetDictPtr(obj);
Benjamin Peterson8eb12692012-02-19 19:59:10 -05001365 if (dictptr == NULL) {
1366 PyErr_SetString(PyExc_AttributeError,
1367 "This object has no __dict__");
1368 return -1;
1369 }
1370 if (value == NULL) {
1371 PyErr_SetString(PyExc_TypeError, "cannot delete __dict__");
1372 return -1;
1373 }
1374 if (!PyDict_Check(value)) {
1375 PyErr_Format(PyExc_TypeError,
1376 "__dict__ must be set to a dictionary, "
1377 "not a '%.200s'", Py_TYPE(value)->tp_name);
1378 return -1;
1379 }
Serhiy Storchaka576f1322016-01-05 21:27:54 +02001380 Py_INCREF(value);
Serhiy Storchakaec397562016-04-06 09:50:03 +03001381 Py_XSETREF(*dictptr, value);
Benjamin Peterson8eb12692012-02-19 19:59:10 -05001382 return 0;
1383}
1384
Antoine Pitrou1a9a9d52010-08-28 18:17:03 +00001385
Guido van Rossum6ac258d1993-05-12 08:24:20 +00001386/* Test a value used as condition, e.g., in a for or if statement.
1387 Return -1 if an error occurred */
1388
1389int
Fred Drake100814d2000-07-09 15:48:49 +00001390PyObject_IsTrue(PyObject *v)
Guido van Rossum6ac258d1993-05-12 08:24:20 +00001391{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001392 Py_ssize_t res;
1393 if (v == Py_True)
1394 return 1;
1395 if (v == Py_False)
1396 return 0;
1397 if (v == Py_None)
1398 return 0;
Victor Stinner58ac7002020-02-07 03:04:21 +01001399 else if (Py_TYPE(v)->tp_as_number != NULL &&
1400 Py_TYPE(v)->tp_as_number->nb_bool != NULL)
1401 res = (*Py_TYPE(v)->tp_as_number->nb_bool)(v);
1402 else if (Py_TYPE(v)->tp_as_mapping != NULL &&
1403 Py_TYPE(v)->tp_as_mapping->mp_length != NULL)
1404 res = (*Py_TYPE(v)->tp_as_mapping->mp_length)(v);
1405 else if (Py_TYPE(v)->tp_as_sequence != NULL &&
1406 Py_TYPE(v)->tp_as_sequence->sq_length != NULL)
1407 res = (*Py_TYPE(v)->tp_as_sequence->sq_length)(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001408 else
1409 return 1;
1410 /* if it is negative, it should be either -1 or -2 */
1411 return (res > 0) ? 1 : Py_SAFE_DOWNCAST(res, Py_ssize_t, int);
Guido van Rossum6ac258d1993-05-12 08:24:20 +00001412}
1413
Tim Peters803526b2002-07-07 05:13:56 +00001414/* equivalent of 'not v'
Guido van Rossumc3d3f961998-04-09 17:53:59 +00001415 Return -1 if an error occurred */
1416
1417int
Fred Drake100814d2000-07-09 15:48:49 +00001418PyObject_Not(PyObject *v)
Guido van Rossumc3d3f961998-04-09 17:53:59 +00001419{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001420 int res;
1421 res = PyObject_IsTrue(v);
1422 if (res < 0)
1423 return res;
1424 return res == 0;
Guido van Rossumc3d3f961998-04-09 17:53:59 +00001425}
1426
Guido van Rossum49b11fe1995-01-26 00:38:22 +00001427/* Test whether an object can be called */
1428
1429int
Fred Drake100814d2000-07-09 15:48:49 +00001430PyCallable_Check(PyObject *x)
Guido van Rossum49b11fe1995-01-26 00:38:22 +00001431{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001432 if (x == NULL)
1433 return 0;
Victor Stinner58ac7002020-02-07 03:04:21 +01001434 return Py_TYPE(x)->tp_call != NULL;
Guido van Rossum49b11fe1995-01-26 00:38:22 +00001435}
1436
Tim Peters7eea37e2001-09-04 22:08:56 +00001437
Georg Brandle32b4222007-03-10 22:13:27 +00001438/* Helper for PyObject_Dir without arguments: returns the local scope. */
1439static PyObject *
Guido van Rossumad7d8d12007-04-13 01:39:34 +00001440_dir_locals(void)
Tim Peters305b5852001-09-17 02:38:46 +00001441{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001442 PyObject *names;
Victor Stinner41bb43a2013-10-29 01:19:37 +01001443 PyObject *locals;
Tim Peters305b5852001-09-17 02:38:46 +00001444
Victor Stinner41bb43a2013-10-29 01:19:37 +01001445 locals = PyEval_GetLocals();
1446 if (locals == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001447 return NULL;
Tim Peters305b5852001-09-17 02:38:46 +00001448
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001449 names = PyMapping_Keys(locals);
1450 if (!names)
1451 return NULL;
1452 if (!PyList_Check(names)) {
1453 PyErr_Format(PyExc_TypeError,
1454 "dir(): expected keys() of locals to be a list, "
1455 "not '%.200s'", Py_TYPE(names)->tp_name);
1456 Py_DECREF(names);
1457 return NULL;
1458 }
Benjamin Peterson3bbb7222011-06-11 16:12:08 -05001459 if (PyList_Sort(names)) {
1460 Py_DECREF(names);
1461 return NULL;
1462 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001463 /* the locals don't need to be DECREF'd */
1464 return names;
Georg Brandle32b4222007-03-10 22:13:27 +00001465}
1466
Benjamin Peterson82b00c12011-05-24 11:09:06 -05001467/* Helper for PyObject_Dir: object introspection. */
Georg Brandle32b4222007-03-10 22:13:27 +00001468static PyObject *
1469_dir_object(PyObject *obj)
1470{
Benjamin Peterson3bbb7222011-06-11 16:12:08 -05001471 PyObject *result, *sorted;
Benjamin Petersonce798522012-01-22 11:24:29 -05001472 PyObject *dirfunc = _PyObject_LookupSpecial(obj, &PyId___dir__);
Georg Brandle32b4222007-03-10 22:13:27 +00001473
Victor Stinner24702042018-10-26 17:16:37 +02001474 assert(obj != NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001475 if (dirfunc == NULL) {
Benjamin Peterson82b00c12011-05-24 11:09:06 -05001476 if (!PyErr_Occurred())
1477 PyErr_SetString(PyExc_TypeError, "object does not provide __dir__");
1478 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001479 }
Benjamin Peterson82b00c12011-05-24 11:09:06 -05001480 /* use __dir__ */
Victor Stinnerf17c3de2016-12-06 18:46:19 +01001481 result = _PyObject_CallNoArg(dirfunc);
Benjamin Peterson82b00c12011-05-24 11:09:06 -05001482 Py_DECREF(dirfunc);
1483 if (result == NULL)
1484 return NULL;
Benjamin Peterson3bbb7222011-06-11 16:12:08 -05001485 /* return sorted(result) */
1486 sorted = PySequence_List(result);
1487 Py_DECREF(result);
1488 if (sorted == NULL)
1489 return NULL;
1490 if (PyList_Sort(sorted)) {
1491 Py_DECREF(sorted);
1492 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001493 }
Benjamin Peterson3bbb7222011-06-11 16:12:08 -05001494 return sorted;
Georg Brandle32b4222007-03-10 22:13:27 +00001495}
1496
1497/* Implementation of dir() -- if obj is NULL, returns the names in the current
1498 (local) scope. Otherwise, performs introspection of the object: returns a
1499 sorted list of attribute names (supposedly) accessible from the object
1500*/
1501PyObject *
1502PyObject_Dir(PyObject *obj)
1503{
Benjamin Peterson3bbb7222011-06-11 16:12:08 -05001504 return (obj == NULL) ? _dir_locals() : _dir_object(obj);
Tim Peters7eea37e2001-09-04 22:08:56 +00001505}
Guido van Rossum49b11fe1995-01-26 00:38:22 +00001506
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001507/*
Raymond Hettinger66d2be82011-07-28 09:55:13 -07001508None is a non-NULL undefined value.
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001509There is (and should be!) no way to create other objects of this type,
Guido van Rossum3f5da241990-12-20 15:06:42 +00001510so there is exactly one (which is indestructible, by the way).
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001511*/
1512
Guido van Rossum0c182a11992-03-27 17:26:13 +00001513/* ARGSUSED */
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001514static PyObject *
Fred Drake100814d2000-07-09 15:48:49 +00001515none_repr(PyObject *op)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001516{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001517 return PyUnicode_FromString("None");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001518}
1519
Barry Warsaw9bf16442001-01-23 16:24:35 +00001520/* ARGUSED */
Victor Stinner2a4903f2020-01-30 13:09:11 +01001521static void _Py_NO_RETURN
Tim Peters803526b2002-07-07 05:13:56 +00001522none_dealloc(PyObject* ignore)
Barry Warsaw9bf16442001-01-23 16:24:35 +00001523{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001524 /* This should never get called, but we also don't want to SEGV if
1525 * we accidentally decref None out of existence.
1526 */
1527 Py_FatalError("deallocating None");
Barry Warsaw9bf16442001-01-23 16:24:35 +00001528}
1529
Benjamin Petersonc4607ae2011-07-29 18:19:43 -05001530static PyObject *
1531none_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
1532{
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +02001533 if (PyTuple_GET_SIZE(args) || (kwargs && PyDict_GET_SIZE(kwargs))) {
Benjamin Petersonc4607ae2011-07-29 18:19:43 -05001534 PyErr_SetString(PyExc_TypeError, "NoneType takes no arguments");
1535 return NULL;
1536 }
1537 Py_RETURN_NONE;
1538}
1539
Raymond Hettinger66d2be82011-07-28 09:55:13 -07001540static int
1541none_bool(PyObject *v)
1542{
1543 return 0;
1544}
1545
1546static PyNumberMethods none_as_number = {
1547 0, /* nb_add */
1548 0, /* nb_subtract */
1549 0, /* nb_multiply */
1550 0, /* nb_remainder */
1551 0, /* nb_divmod */
1552 0, /* nb_power */
1553 0, /* nb_negative */
1554 0, /* nb_positive */
1555 0, /* nb_absolute */
1556 (inquiry)none_bool, /* nb_bool */
1557 0, /* nb_invert */
1558 0, /* nb_lshift */
1559 0, /* nb_rshift */
1560 0, /* nb_and */
1561 0, /* nb_xor */
1562 0, /* nb_or */
1563 0, /* nb_int */
1564 0, /* nb_reserved */
1565 0, /* nb_float */
1566 0, /* nb_inplace_add */
1567 0, /* nb_inplace_subtract */
1568 0, /* nb_inplace_multiply */
1569 0, /* nb_inplace_remainder */
1570 0, /* nb_inplace_power */
1571 0, /* nb_inplace_lshift */
1572 0, /* nb_inplace_rshift */
1573 0, /* nb_inplace_and */
1574 0, /* nb_inplace_xor */
1575 0, /* nb_inplace_or */
1576 0, /* nb_floor_divide */
1577 0, /* nb_true_divide */
1578 0, /* nb_inplace_floor_divide */
1579 0, /* nb_inplace_true_divide */
1580 0, /* nb_index */
1581};
Barry Warsaw9bf16442001-01-23 16:24:35 +00001582
Alexandre Vassalotti65846c62013-11-30 17:55:48 -08001583PyTypeObject _PyNone_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001584 PyVarObject_HEAD_INIT(&PyType_Type, 0)
1585 "NoneType",
1586 0,
1587 0,
1588 none_dealloc, /*tp_dealloc*/ /*never called*/
Jeroen Demeyer530f5062019-05-31 04:13:39 +02001589 0, /*tp_vectorcall_offset*/
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001590 0, /*tp_getattr*/
1591 0, /*tp_setattr*/
Jeroen Demeyer530f5062019-05-31 04:13:39 +02001592 0, /*tp_as_async*/
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001593 none_repr, /*tp_repr*/
Raymond Hettinger66d2be82011-07-28 09:55:13 -07001594 &none_as_number, /*tp_as_number*/
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001595 0, /*tp_as_sequence*/
1596 0, /*tp_as_mapping*/
1597 0, /*tp_hash */
Benjamin Petersonc4607ae2011-07-29 18:19:43 -05001598 0, /*tp_call */
1599 0, /*tp_str */
1600 0, /*tp_getattro */
1601 0, /*tp_setattro */
1602 0, /*tp_as_buffer */
1603 Py_TPFLAGS_DEFAULT, /*tp_flags */
1604 0, /*tp_doc */
1605 0, /*tp_traverse */
1606 0, /*tp_clear */
1607 0, /*tp_richcompare */
1608 0, /*tp_weaklistoffset */
1609 0, /*tp_iter */
1610 0, /*tp_iternext */
1611 0, /*tp_methods */
1612 0, /*tp_members */
1613 0, /*tp_getset */
1614 0, /*tp_base */
1615 0, /*tp_dict */
1616 0, /*tp_descr_get */
1617 0, /*tp_descr_set */
1618 0, /*tp_dictoffset */
1619 0, /*tp_init */
1620 0, /*tp_alloc */
1621 none_new, /*tp_new */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001622};
1623
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001624PyObject _Py_NoneStruct = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001625 _PyObject_EXTRA_INIT
Alexandre Vassalotti65846c62013-11-30 17:55:48 -08001626 1, &_PyNone_Type
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001627};
1628
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +00001629/* NotImplemented is an object that can be used to signal that an
1630 operation is not implemented for the given type combination. */
1631
1632static PyObject *
1633NotImplemented_repr(PyObject *op)
1634{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001635 return PyUnicode_FromString("NotImplemented");
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +00001636}
1637
Benjamin Peterson18d7d7a2011-07-29 18:27:44 -05001638static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05301639NotImplemented_reduce(PyObject *op, PyObject *Py_UNUSED(ignored))
Alexandre Vassalottic49477b2013-11-24 02:53:45 -08001640{
1641 return PyUnicode_FromString("NotImplemented");
1642}
1643
1644static PyMethodDef notimplemented_methods[] = {
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05301645 {"__reduce__", NotImplemented_reduce, METH_NOARGS, NULL},
Alexandre Vassalottic49477b2013-11-24 02:53:45 -08001646 {NULL, NULL}
1647};
1648
1649static PyObject *
Benjamin Peterson18d7d7a2011-07-29 18:27:44 -05001650notimplemented_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
1651{
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +02001652 if (PyTuple_GET_SIZE(args) || (kwargs && PyDict_GET_SIZE(kwargs))) {
Benjamin Peterson18d7d7a2011-07-29 18:27:44 -05001653 PyErr_SetString(PyExc_TypeError, "NotImplementedType takes no arguments");
1654 return NULL;
1655 }
Brian Curtindfc80e32011-08-10 20:28:54 -05001656 Py_RETURN_NOTIMPLEMENTED;
Benjamin Peterson18d7d7a2011-07-29 18:27:44 -05001657}
1658
Victor Stinner2a4903f2020-01-30 13:09:11 +01001659static void _Py_NO_RETURN
Armin Ronacher226b1db2012-10-06 14:28:58 +02001660notimplemented_dealloc(PyObject* ignore)
1661{
1662 /* This should never get called, but we also don't want to SEGV if
1663 * we accidentally decref NotImplemented out of existence.
1664 */
1665 Py_FatalError("deallocating NotImplemented");
1666}
1667
MojoVampire469325c2020-03-03 18:50:17 +00001668static int
1669notimplemented_bool(PyObject *v)
1670{
1671 if (PyErr_WarnEx(PyExc_DeprecationWarning,
1672 "NotImplemented should not be used in a boolean context",
1673 1) < 0)
1674 {
1675 return -1;
1676 }
1677 return 1;
1678}
1679
1680static PyNumberMethods notimplemented_as_number = {
1681 .nb_bool = notimplemented_bool,
1682};
1683
Alexandre Vassalotti65846c62013-11-30 17:55:48 -08001684PyTypeObject _PyNotImplemented_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001685 PyVarObject_HEAD_INIT(&PyType_Type, 0)
1686 "NotImplementedType",
1687 0,
1688 0,
Armin Ronacher226b1db2012-10-06 14:28:58 +02001689 notimplemented_dealloc, /*tp_dealloc*/ /*never called*/
Jeroen Demeyer530f5062019-05-31 04:13:39 +02001690 0, /*tp_vectorcall_offset*/
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001691 0, /*tp_getattr*/
1692 0, /*tp_setattr*/
Jeroen Demeyer530f5062019-05-31 04:13:39 +02001693 0, /*tp_as_async*/
MojoVampire469325c2020-03-03 18:50:17 +00001694 NotImplemented_repr, /*tp_repr*/
1695 &notimplemented_as_number, /*tp_as_number*/
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001696 0, /*tp_as_sequence*/
1697 0, /*tp_as_mapping*/
1698 0, /*tp_hash */
Benjamin Peterson18d7d7a2011-07-29 18:27:44 -05001699 0, /*tp_call */
1700 0, /*tp_str */
1701 0, /*tp_getattro */
1702 0, /*tp_setattro */
1703 0, /*tp_as_buffer */
1704 Py_TPFLAGS_DEFAULT, /*tp_flags */
1705 0, /*tp_doc */
1706 0, /*tp_traverse */
1707 0, /*tp_clear */
1708 0, /*tp_richcompare */
1709 0, /*tp_weaklistoffset */
1710 0, /*tp_iter */
1711 0, /*tp_iternext */
Alexandre Vassalottic49477b2013-11-24 02:53:45 -08001712 notimplemented_methods, /*tp_methods */
Benjamin Peterson18d7d7a2011-07-29 18:27:44 -05001713 0, /*tp_members */
1714 0, /*tp_getset */
1715 0, /*tp_base */
1716 0, /*tp_dict */
1717 0, /*tp_descr_get */
1718 0, /*tp_descr_set */
1719 0, /*tp_dictoffset */
1720 0, /*tp_init */
1721 0, /*tp_alloc */
1722 notimplemented_new, /*tp_new */
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +00001723};
1724
1725PyObject _Py_NotImplementedStruct = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001726 _PyObject_EXTRA_INIT
Alexandre Vassalotti65846c62013-11-30 17:55:48 -08001727 1, &_PyNotImplemented_Type
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +00001728};
1729
Victor Stinner331a6a52019-05-27 16:39:22 +02001730PyStatus
Victor Stinnerab672812019-01-23 15:04:40 +01001731_PyTypes_Init(void)
Guido van Rossumba21a492001-08-16 08:17:26 +00001732{
Victor Stinner7a1f6c22020-01-30 09:02:14 +01001733 PyStatus status = _PyTypes_InitSlotDefs();
1734 if (_PyStatus_EXCEPTION(status)) {
1735 return status;
1736 }
1737
Victor Stinner6d43f6f2019-01-22 21:18:05 +01001738#define INIT_TYPE(TYPE, NAME) \
1739 do { \
1740 if (PyType_Ready(TYPE) < 0) { \
Victor Stinner331a6a52019-05-27 16:39:22 +02001741 return _PyStatus_ERR("Can't initialize " NAME " type"); \
Victor Stinner6d43f6f2019-01-22 21:18:05 +01001742 } \
1743 } while (0)
Victor Stinner5a1bb4e2014-06-02 14:10:59 +02001744
Victor Stinner6d43f6f2019-01-22 21:18:05 +01001745 INIT_TYPE(&PyBaseObject_Type, "object");
1746 INIT_TYPE(&PyType_Type, "type");
1747 INIT_TYPE(&_PyWeakref_RefType, "weakref");
1748 INIT_TYPE(&_PyWeakref_CallableProxyType, "callable weakref proxy");
1749 INIT_TYPE(&_PyWeakref_ProxyType, "weakref proxy");
1750 INIT_TYPE(&PyLong_Type, "int");
1751 INIT_TYPE(&PyBool_Type, "bool");
1752 INIT_TYPE(&PyByteArray_Type, "bytearray");
1753 INIT_TYPE(&PyBytes_Type, "str");
1754 INIT_TYPE(&PyList_Type, "list");
1755 INIT_TYPE(&_PyNone_Type, "None");
1756 INIT_TYPE(&_PyNotImplemented_Type, "NotImplemented");
1757 INIT_TYPE(&PyTraceBack_Type, "traceback");
1758 INIT_TYPE(&PySuper_Type, "super");
1759 INIT_TYPE(&PyRange_Type, "range");
1760 INIT_TYPE(&PyDict_Type, "dict");
1761 INIT_TYPE(&PyDictKeys_Type, "dict keys");
1762 INIT_TYPE(&PyDictValues_Type, "dict values");
1763 INIT_TYPE(&PyDictItems_Type, "dict items");
1764 INIT_TYPE(&PyDictRevIterKey_Type, "reversed dict keys");
1765 INIT_TYPE(&PyDictRevIterValue_Type, "reversed dict values");
1766 INIT_TYPE(&PyDictRevIterItem_Type, "reversed dict items");
1767 INIT_TYPE(&PyODict_Type, "OrderedDict");
1768 INIT_TYPE(&PyODictKeys_Type, "odict_keys");
1769 INIT_TYPE(&PyODictItems_Type, "odict_items");
1770 INIT_TYPE(&PyODictValues_Type, "odict_values");
1771 INIT_TYPE(&PyODictIter_Type, "odict_keyiterator");
1772 INIT_TYPE(&PySet_Type, "set");
1773 INIT_TYPE(&PyUnicode_Type, "str");
1774 INIT_TYPE(&PySlice_Type, "slice");
1775 INIT_TYPE(&PyStaticMethod_Type, "static method");
1776 INIT_TYPE(&PyComplex_Type, "complex");
1777 INIT_TYPE(&PyFloat_Type, "float");
1778 INIT_TYPE(&PyFrozenSet_Type, "frozenset");
1779 INIT_TYPE(&PyProperty_Type, "property");
1780 INIT_TYPE(&_PyManagedBuffer_Type, "managed buffer");
1781 INIT_TYPE(&PyMemoryView_Type, "memoryview");
1782 INIT_TYPE(&PyTuple_Type, "tuple");
1783 INIT_TYPE(&PyEnum_Type, "enumerate");
1784 INIT_TYPE(&PyReversed_Type, "reversed");
1785 INIT_TYPE(&PyStdPrinter_Type, "StdPrinter");
1786 INIT_TYPE(&PyCode_Type, "code");
1787 INIT_TYPE(&PyFrame_Type, "frame");
1788 INIT_TYPE(&PyCFunction_Type, "builtin function");
1789 INIT_TYPE(&PyMethod_Type, "method");
1790 INIT_TYPE(&PyFunction_Type, "function");
1791 INIT_TYPE(&PyDictProxy_Type, "dict proxy");
1792 INIT_TYPE(&PyGen_Type, "generator");
1793 INIT_TYPE(&PyGetSetDescr_Type, "get-set descriptor");
1794 INIT_TYPE(&PyWrapperDescr_Type, "wrapper");
1795 INIT_TYPE(&_PyMethodWrapper_Type, "method wrapper");
1796 INIT_TYPE(&PyEllipsis_Type, "ellipsis");
1797 INIT_TYPE(&PyMemberDescr_Type, "member descriptor");
1798 INIT_TYPE(&_PyNamespace_Type, "namespace");
1799 INIT_TYPE(&PyCapsule_Type, "capsule");
1800 INIT_TYPE(&PyLongRangeIter_Type, "long range iterator");
1801 INIT_TYPE(&PyCell_Type, "cell");
1802 INIT_TYPE(&PyInstanceMethod_Type, "instance method");
1803 INIT_TYPE(&PyClassMethodDescr_Type, "class method descr");
1804 INIT_TYPE(&PyMethodDescr_Type, "method descr");
1805 INIT_TYPE(&PyCallIter_Type, "call iter");
1806 INIT_TYPE(&PySeqIter_Type, "sequence iterator");
Antoine Pitrou91f43802019-05-26 17:10:09 +02001807 INIT_TYPE(&PyPickleBuffer_Type, "pickle.PickleBuffer");
Victor Stinner6d43f6f2019-01-22 21:18:05 +01001808 INIT_TYPE(&PyCoro_Type, "coroutine");
1809 INIT_TYPE(&_PyCoroWrapper_Type, "coroutine wrapper");
Eric Snowc11183c2019-03-15 16:35:46 -06001810 INIT_TYPE(&_PyInterpreterID_Type, "interpreter ID");
Victor Stinner331a6a52019-05-27 16:39:22 +02001811 return _PyStatus_OK();
Guido van Rossumba21a492001-08-16 08:17:26 +00001812
Victor Stinner6d43f6f2019-01-22 21:18:05 +01001813#undef INIT_TYPE
Guido van Rossumba21a492001-08-16 08:17:26 +00001814}
1815
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001816
Victor Stinner40e547d2020-02-05 01:11:10 +01001817void
1818_Py_NewReference(PyObject *op)
1819{
1820 if (_Py_tracemalloc_config.tracing) {
1821 _PyTraceMalloc_NewReference(op);
1822 }
1823#ifdef Py_REF_DEBUG
1824 _Py_RefTotal++;
1825#endif
Victor Stinnerc86a1122020-02-07 01:24:29 +01001826 Py_SET_REFCNT(op, 1);
Victor Stinner40e547d2020-02-05 01:11:10 +01001827#ifdef Py_TRACE_REFS
1828 _Py_AddToAllObjects(op, 1);
1829#endif
1830}
1831
1832
Guido van Rossum84a90321996-05-22 16:34:47 +00001833#ifdef Py_TRACE_REFS
Guido van Rossumaacdc9d1996-08-12 21:32:12 +00001834void
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02001835_Py_ForgetReference(PyObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001836{
Victor Stinnera93c51e2020-02-07 00:38:59 +01001837 if (Py_REFCNT(op) < 0) {
Victor Stinner5eb8bff2020-01-30 09:01:07 +01001838 _PyObject_ASSERT_FAILED_MSG(op, "negative refcnt");
1839 }
1840
1841 if (op == &refchain ||
1842 op->_ob_prev->_ob_next != op || op->_ob_next->_ob_prev != op)
1843 {
1844 _PyObject_ASSERT_FAILED_MSG(op, "invalid object chain");
1845 }
1846
Guido van Rossumbffd6832000-01-20 22:32:56 +00001847#ifdef SLOW_UNREF_CHECK
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02001848 PyObject *p;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001849 for (p = refchain._ob_next; p != &refchain; p = p->_ob_next) {
Victor Stinner5eb8bff2020-01-30 09:01:07 +01001850 if (p == op) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001851 break;
Victor Stinner5eb8bff2020-01-30 09:01:07 +01001852 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001853 }
Victor Stinner5eb8bff2020-01-30 09:01:07 +01001854 if (p == &refchain) {
1855 /* Not found */
1856 _PyObject_ASSERT_FAILED_MSG(op,
1857 "object not found in the objects list");
1858 }
Guido van Rossum2e8f6141992-09-03 20:32:55 +00001859#endif
Victor Stinner5eb8bff2020-01-30 09:01:07 +01001860
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001861 op->_ob_next->_ob_prev = op->_ob_prev;
1862 op->_ob_prev->_ob_next = op->_ob_next;
1863 op->_ob_next = op->_ob_prev = NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001864}
1865
Tim Peters269b2a62003-04-17 19:52:29 +00001866/* Print all live objects. Because PyObject_Print is called, the
1867 * interpreter must be in a healthy state.
1868 */
Guido van Rossumaacdc9d1996-08-12 21:32:12 +00001869void
Fred Drake100814d2000-07-09 15:48:49 +00001870_Py_PrintReferences(FILE *fp)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001871{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001872 PyObject *op;
1873 fprintf(fp, "Remaining objects:\n");
1874 for (op = refchain._ob_next; op != &refchain; op = op->_ob_next) {
Victor Stinnera93c51e2020-02-07 00:38:59 +01001875 fprintf(fp, "%p [%" PY_FORMAT_SIZE_T "d] ", (void *)op, Py_REFCNT(op));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001876 if (PyObject_Print(op, fp, 0) != 0)
1877 PyErr_Clear();
1878 putc('\n', fp);
1879 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001880}
1881
Tim Peters269b2a62003-04-17 19:52:29 +00001882/* Print the addresses of all live objects. Unlike _Py_PrintReferences, this
1883 * doesn't make any calls to the Python C API, so is always safe to call.
1884 */
1885void
1886_Py_PrintReferenceAddresses(FILE *fp)
1887{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001888 PyObject *op;
1889 fprintf(fp, "Remaining object addresses:\n");
1890 for (op = refchain._ob_next; op != &refchain; op = op->_ob_next)
Zackery Spytz1a2252e2019-05-06 10:56:51 -06001891 fprintf(fp, "%p [%" PY_FORMAT_SIZE_T "d] %s\n", (void *)op,
Victor Stinnera93c51e2020-02-07 00:38:59 +01001892 Py_REFCNT(op), Py_TYPE(op)->tp_name);
Tim Peters269b2a62003-04-17 19:52:29 +00001893}
1894
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +00001895PyObject *
Fred Drake100814d2000-07-09 15:48:49 +00001896_Py_GetObjects(PyObject *self, PyObject *args)
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +00001897{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001898 int i, n;
1899 PyObject *t = NULL;
1900 PyObject *res, *op;
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +00001901
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001902 if (!PyArg_ParseTuple(args, "i|O", &n, &t))
1903 return NULL;
1904 op = refchain._ob_next;
1905 res = PyList_New(0);
1906 if (res == NULL)
1907 return NULL;
1908 for (i = 0; (n == 0 || i < n) && op != &refchain; i++) {
1909 while (op == self || op == args || op == res || op == t ||
Andy Lester55728702020-03-06 16:53:17 -06001910 (t != NULL && !Py_IS_TYPE(op, (PyTypeObject *) t))) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001911 op = op->_ob_next;
1912 if (op == &refchain)
1913 return res;
1914 }
1915 if (PyList_Append(res, op) < 0) {
1916 Py_DECREF(res);
1917 return NULL;
1918 }
1919 op = op->_ob_next;
1920 }
1921 return res;
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +00001922}
1923
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001924#endif
Guido van Rossum97ead3f1996-01-12 01:24:09 +00001925
Benjamin Petersonb173f782009-05-05 22:31:58 +00001926
Guido van Rossum84a90321996-05-22 16:34:47 +00001927/* Hack to force loading of abstract.o */
Martin v. Löwis18e16552006-02-15 17:27:45 +00001928Py_ssize_t (*_Py_abstract_hack)(PyObject *) = PyObject_Size;
Guido van Rossume09fb551997-08-05 02:04:34 +00001929
1930
David Malcolm49526f42012-06-22 14:55:41 -04001931void
1932_PyObject_DebugTypeStats(FILE *out)
1933{
David Malcolm49526f42012-06-22 14:55:41 -04001934 _PyDict_DebugMallocStats(out);
1935 _PyFloat_DebugMallocStats(out);
1936 _PyFrame_DebugMallocStats(out);
1937 _PyList_DebugMallocStats(out);
David Malcolm49526f42012-06-22 14:55:41 -04001938 _PyTuple_DebugMallocStats(out);
1939}
Guido van Rossumb18618d2000-05-03 23:44:39 +00001940
Guido van Rossum86610361998-04-10 22:32:46 +00001941/* These methods are used to control infinite recursion in repr, str, print,
1942 etc. Container objects that may recursively contain themselves,
Martin Panter8d56c022016-05-29 04:13:35 +00001943 e.g. builtin dictionaries and lists, should use Py_ReprEnter() and
Guido van Rossum86610361998-04-10 22:32:46 +00001944 Py_ReprLeave() to avoid infinite recursion.
1945
1946 Py_ReprEnter() returns 0 the first time it is called for a particular
1947 object and 1 every time thereafter. It returns -1 if an exception
1948 occurred. Py_ReprLeave() has no return value.
1949
1950 See dictobject.c and listobject.c for examples of use.
1951*/
1952
Guido van Rossum86610361998-04-10 22:32:46 +00001953int
Fred Drake100814d2000-07-09 15:48:49 +00001954Py_ReprEnter(PyObject *obj)
Guido van Rossum86610361998-04-10 22:32:46 +00001955{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001956 PyObject *dict;
1957 PyObject *list;
1958 Py_ssize_t i;
Guido van Rossum86610361998-04-10 22:32:46 +00001959
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001960 dict = PyThreadState_GetDict();
Antoine Pitrou04d17d32014-03-31 22:04:38 +02001961 /* Ignore a missing thread-state, so that this function can be called
1962 early on startup. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001963 if (dict == NULL)
1964 return 0;
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02001965 list = _PyDict_GetItemIdWithError(dict, &PyId_Py_Repr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001966 if (list == NULL) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02001967 if (PyErr_Occurred()) {
1968 return -1;
1969 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001970 list = PyList_New(0);
1971 if (list == NULL)
1972 return -1;
Victor Stinner7a07e452013-11-06 18:57:29 +01001973 if (_PyDict_SetItemId(dict, &PyId_Py_Repr, list) < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001974 return -1;
1975 Py_DECREF(list);
1976 }
1977 i = PyList_GET_SIZE(list);
1978 while (--i >= 0) {
1979 if (PyList_GET_ITEM(list, i) == obj)
1980 return 1;
1981 }
Victor Stinnere901d1f2013-07-17 21:58:41 +02001982 if (PyList_Append(list, obj) < 0)
1983 return -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001984 return 0;
Guido van Rossum86610361998-04-10 22:32:46 +00001985}
1986
1987void
Fred Drake100814d2000-07-09 15:48:49 +00001988Py_ReprLeave(PyObject *obj)
Guido van Rossum86610361998-04-10 22:32:46 +00001989{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001990 PyObject *dict;
1991 PyObject *list;
1992 Py_ssize_t i;
Victor Stinner1b634932013-07-16 22:24:44 +02001993 PyObject *error_type, *error_value, *error_traceback;
1994
1995 PyErr_Fetch(&error_type, &error_value, &error_traceback);
Guido van Rossum86610361998-04-10 22:32:46 +00001996
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001997 dict = PyThreadState_GetDict();
1998 if (dict == NULL)
Victor Stinner1b634932013-07-16 22:24:44 +02001999 goto finally;
2000
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002001 list = _PyDict_GetItemIdWithError(dict, &PyId_Py_Repr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002002 if (list == NULL || !PyList_Check(list))
Victor Stinner1b634932013-07-16 22:24:44 +02002003 goto finally;
2004
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002005 i = PyList_GET_SIZE(list);
2006 /* Count backwards because we always expect obj to be list[-1] */
2007 while (--i >= 0) {
2008 if (PyList_GET_ITEM(list, i) == obj) {
2009 PyList_SetSlice(list, i, i + 1, NULL);
2010 break;
2011 }
2012 }
Victor Stinner1b634932013-07-16 22:24:44 +02002013
2014finally:
2015 /* ignore exceptions because there is no way to report them. */
2016 PyErr_Restore(error_type, error_value, error_traceback);
Guido van Rossum86610361998-04-10 22:32:46 +00002017}
Guido van Rossumd724b232000-03-13 16:01:29 +00002018
Tim Peters803526b2002-07-07 05:13:56 +00002019/* Trashcan support. */
Guido van Rossumd724b232000-03-13 16:01:29 +00002020
Tim Peters803526b2002-07-07 05:13:56 +00002021/* Add op to the _PyTrash_delete_later list. Called when the current
2022 * call-stack depth gets large. op must be a currently untracked gc'ed
2023 * object, with refcount 0. Py_DECREF must already have been called on it.
2024 */
Guido van Rossumd724b232000-03-13 16:01:29 +00002025void
Fred Drake100814d2000-07-09 15:48:49 +00002026_PyTrash_deposit_object(PyObject *op)
Guido van Rossumd724b232000-03-13 16:01:29 +00002027{
Victor Stinner72474072019-11-20 12:25:50 +01002028 PyThreadState *tstate = _PyThreadState_GET();
2029 struct _gc_runtime_state *gcstate = &tstate->interp->gc;
2030
Victor Stinner24702042018-10-26 17:16:37 +02002031 _PyObject_ASSERT(op, PyObject_IS_GC(op));
2032 _PyObject_ASSERT(op, !_PyObject_GC_IS_TRACKED(op));
Victor Stinnera93c51e2020-02-07 00:38:59 +01002033 _PyObject_ASSERT(op, Py_REFCNT(op) == 0);
Victor Stinner72474072019-11-20 12:25:50 +01002034 _PyGCHead_SET_PREV(_Py_AS_GC(op), gcstate->trash_delete_later);
2035 gcstate->trash_delete_later = op;
Guido van Rossumd724b232000-03-13 16:01:29 +00002036}
2037
Antoine Pitrou2b0218a2012-09-06 00:59:49 +02002038/* The equivalent API, using per-thread state recursion info */
2039void
2040_PyTrash_thread_deposit_object(PyObject *op)
2041{
Victor Stinner50b48572018-11-01 01:51:40 +01002042 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinner24702042018-10-26 17:16:37 +02002043 _PyObject_ASSERT(op, PyObject_IS_GC(op));
2044 _PyObject_ASSERT(op, !_PyObject_GC_IS_TRACKED(op));
Victor Stinnera93c51e2020-02-07 00:38:59 +01002045 _PyObject_ASSERT(op, Py_REFCNT(op) == 0);
INADA Naoki5ac9e6e2018-07-10 17:19:53 +09002046 _PyGCHead_SET_PREV(_Py_AS_GC(op), tstate->trash_delete_later);
Antoine Pitrou2b0218a2012-09-06 00:59:49 +02002047 tstate->trash_delete_later = op;
2048}
2049
Min ho Kimc4cacc82019-07-31 08:16:13 +10002050/* Deallocate all the objects in the _PyTrash_delete_later list. Called when
Tim Peters803526b2002-07-07 05:13:56 +00002051 * the call-stack unwinds again.
2052 */
Guido van Rossumd724b232000-03-13 16:01:29 +00002053void
Fred Drake100814d2000-07-09 15:48:49 +00002054_PyTrash_destroy_chain(void)
Guido van Rossumd724b232000-03-13 16:01:29 +00002055{
Victor Stinner72474072019-11-20 12:25:50 +01002056 PyThreadState *tstate = _PyThreadState_GET();
2057 struct _gc_runtime_state *gcstate = &tstate->interp->gc;
2058
2059 while (gcstate->trash_delete_later) {
2060 PyObject *op = gcstate->trash_delete_later;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002061 destructor dealloc = Py_TYPE(op)->tp_dealloc;
Neil Schemenauerf589c052002-03-29 03:05:54 +00002062
Victor Stinner72474072019-11-20 12:25:50 +01002063 gcstate->trash_delete_later =
INADA Naoki5ac9e6e2018-07-10 17:19:53 +09002064 (PyObject*) _PyGCHead_PREV(_Py_AS_GC(op));
Neil Schemenauerf589c052002-03-29 03:05:54 +00002065
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002066 /* Call the deallocator directly. This used to try to
2067 * fool Py_DECREF into calling it indirectly, but
2068 * Py_DECREF was already called on this object, and in
2069 * assorted non-release builds calling Py_DECREF again ends
2070 * up distorting allocation statistics.
2071 */
Victor Stinnera93c51e2020-02-07 00:38:59 +01002072 _PyObject_ASSERT(op, Py_REFCNT(op) == 0);
Victor Stinner72474072019-11-20 12:25:50 +01002073 ++gcstate->trash_delete_nesting;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002074 (*dealloc)(op);
Victor Stinner72474072019-11-20 12:25:50 +01002075 --gcstate->trash_delete_nesting;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002076 }
Guido van Rossumd724b232000-03-13 16:01:29 +00002077}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002078
Antoine Pitrou2b0218a2012-09-06 00:59:49 +02002079/* The equivalent API, using per-thread state recursion info */
2080void
2081_PyTrash_thread_destroy_chain(void)
2082{
Victor Stinner50b48572018-11-01 01:51:40 +01002083 PyThreadState *tstate = _PyThreadState_GET();
Xiang Zhanga66f9c62017-05-13 13:36:14 +08002084 /* We need to increase trash_delete_nesting here, otherwise,
2085 _PyTrash_thread_destroy_chain will be called recursively
2086 and then possibly crash. An example that may crash without
2087 increase:
2088 N = 500000 # need to be large enough
2089 ob = object()
2090 tups = [(ob,) for i in range(N)]
2091 for i in range(49):
2092 tups = [(tup,) for tup in tups]
2093 del tups
2094 */
2095 assert(tstate->trash_delete_nesting == 0);
2096 ++tstate->trash_delete_nesting;
Antoine Pitrou2b0218a2012-09-06 00:59:49 +02002097 while (tstate->trash_delete_later) {
2098 PyObject *op = tstate->trash_delete_later;
2099 destructor dealloc = Py_TYPE(op)->tp_dealloc;
2100
2101 tstate->trash_delete_later =
INADA Naoki5ac9e6e2018-07-10 17:19:53 +09002102 (PyObject*) _PyGCHead_PREV(_Py_AS_GC(op));
Antoine Pitrou2b0218a2012-09-06 00:59:49 +02002103
2104 /* Call the deallocator directly. This used to try to
2105 * fool Py_DECREF into calling it indirectly, but
2106 * Py_DECREF was already called on this object, and in
2107 * assorted non-release builds calling Py_DECREF again ends
2108 * up distorting allocation statistics.
2109 */
Victor Stinnera93c51e2020-02-07 00:38:59 +01002110 _PyObject_ASSERT(op, Py_REFCNT(op) == 0);
Antoine Pitrou2b0218a2012-09-06 00:59:49 +02002111 (*dealloc)(op);
Xiang Zhanga66f9c62017-05-13 13:36:14 +08002112 assert(tstate->trash_delete_nesting == 1);
Antoine Pitrou2b0218a2012-09-06 00:59:49 +02002113 }
Xiang Zhanga66f9c62017-05-13 13:36:14 +08002114 --tstate->trash_delete_nesting;
Antoine Pitrou2b0218a2012-09-06 00:59:49 +02002115}
2116
Victor Stinner626bff82018-10-25 17:31:10 +02002117
Victor Stinner2a4903f2020-01-30 13:09:11 +01002118void _Py_NO_RETURN
Victor Stinnerf1d002c2018-11-21 23:53:44 +01002119_PyObject_AssertFailed(PyObject *obj, const char *expr, const char *msg,
Victor Stinner626bff82018-10-25 17:31:10 +02002120 const char *file, int line, const char *function)
2121{
Victor Stinnerf1d002c2018-11-21 23:53:44 +01002122 fprintf(stderr, "%s:%d: ", file, line);
2123 if (function) {
2124 fprintf(stderr, "%s: ", function);
2125 }
Victor Stinner626bff82018-10-25 17:31:10 +02002126 fflush(stderr);
Victor Stinner68762572019-10-07 18:42:01 +02002127
Victor Stinnerf1d002c2018-11-21 23:53:44 +01002128 if (expr) {
2129 fprintf(stderr, "Assertion \"%s\" failed", expr);
Victor Stinner626bff82018-10-25 17:31:10 +02002130 }
2131 else {
Victor Stinnerf1d002c2018-11-21 23:53:44 +01002132 fprintf(stderr, "Assertion failed");
Victor Stinner626bff82018-10-25 17:31:10 +02002133 }
2134 fflush(stderr);
Victor Stinner68762572019-10-07 18:42:01 +02002135
Victor Stinnerf1d002c2018-11-21 23:53:44 +01002136 if (msg) {
2137 fprintf(stderr, ": %s", msg);
2138 }
2139 fprintf(stderr, "\n");
2140 fflush(stderr);
Victor Stinner626bff82018-10-25 17:31:10 +02002141
Victor Stinner68762572019-10-07 18:42:01 +02002142 if (_PyObject_IsFreed(obj)) {
Victor Stinner626bff82018-10-25 17:31:10 +02002143 /* It seems like the object memory has been freed:
2144 don't access it to prevent a segmentation fault. */
Victor Stinnerb39afb72019-09-17 23:36:28 +02002145 fprintf(stderr, "<object at %p is freed>\n", obj);
Victor Stinner68762572019-10-07 18:42:01 +02002146 fflush(stderr);
Victor Stinner626bff82018-10-25 17:31:10 +02002147 }
2148 else {
penguindustin96466302019-05-06 14:57:17 -04002149 /* Display the traceback where the object has been allocated.
Victor Stinner626bff82018-10-25 17:31:10 +02002150 Do it before dumping repr(obj), since repr() is more likely
2151 to crash than dumping the traceback. */
2152 void *ptr;
2153 PyTypeObject *type = Py_TYPE(obj);
2154 if (PyType_IS_GC(type)) {
2155 ptr = (void *)((char *)obj - sizeof(PyGC_Head));
2156 }
2157 else {
2158 ptr = (void *)obj;
2159 }
2160 _PyMem_DumpTraceback(fileno(stderr), ptr);
2161
2162 /* This might succeed or fail, but we're about to abort, so at least
2163 try to provide any extra info we can: */
2164 _PyObject_Dump(obj);
Victor Stinner77753492019-10-07 23:44:05 +02002165
2166 fprintf(stderr, "\n");
2167 fflush(stderr);
Victor Stinner626bff82018-10-25 17:31:10 +02002168 }
Victor Stinner626bff82018-10-25 17:31:10 +02002169
2170 Py_FatalError("_PyObject_AssertFailed");
2171}
2172
Victor Stinner3c09dca2018-10-30 14:48:26 +01002173
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00002174void
2175_Py_Dealloc(PyObject *op)
2176{
Victor Stinner3c09dca2018-10-30 14:48:26 +01002177 destructor dealloc = Py_TYPE(op)->tp_dealloc;
2178#ifdef Py_TRACE_REFS
2179 _Py_ForgetReference(op);
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00002180#endif
Victor Stinner3c09dca2018-10-30 14:48:26 +01002181 (*dealloc)(op);
2182}
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00002183
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002184#ifdef __cplusplus
2185}
2186#endif