blob: 623ee52eb1e22da042d881b6c6dcfffcc744d857 [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001
Benjamin Peterson722954a2011-06-11 16:33:35 -05002/* Generic object operations; and implementation of None */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003
Guido van Rossumc0b618a1997-05-02 03:12:38 +00004#include "Python.h"
Victor Stinnere5014be2020-04-14 17:52:15 +02005#include "pycore_ceval.h" // _Py_EnterRecursiveCall()
Victor Stinnerbe434dc2019-11-05 00:51:22 +01006#include "pycore_context.h"
Victor Stinner331a6a52019-05-27 16:39:22 +02007#include "pycore_initconfig.h"
Victor Stinner0fc91ee2019-04-12 21:51:34 +02008#include "pycore_object.h"
Victor Stinnerbe434dc2019-11-05 00:51:22 +01009#include "pycore_pyerrors.h"
Victor Stinner7a1f6c22020-01-30 09:02:14 +010010#include "pycore_pylifecycle.h"
Victor Stinnere560f902020-04-14 18:30:41 +020011#include "pycore_pymem.h" // _PyMem_IsPtrFreed()
Victor Stinnere5014be2020-04-14 17:52:15 +020012#include "pycore_pystate.h" // _PyThreadState_GET()
Benjamin Petersonfd838e62009-04-20 02:09:13 +000013#include "frameobject.h"
Eric Snowc11183c2019-03-15 16:35:46 -060014#include "interpreteridobject.h"
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000015
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000016#ifdef __cplusplus
17extern "C" {
18#endif
19
Victor Stinner626bff82018-10-25 17:31:10 +020020/* Defined in tracemalloc.c */
21extern void _PyMem_DumpTraceback(int fd, const void *ptr);
22
Victor Stinnerbd303c12013-11-07 23:07:29 +010023_Py_IDENTIFIER(Py_Repr);
24_Py_IDENTIFIER(__bytes__);
25_Py_IDENTIFIER(__dir__);
26_Py_IDENTIFIER(__isabstractmethod__);
Victor Stinnerbd303c12013-11-07 23:07:29 +010027
Victor Stinner0fc91ee2019-04-12 21:51:34 +020028
29int
30_PyObject_CheckConsistency(PyObject *op, int check_content)
31{
Victor Stinner68762572019-10-07 18:42:01 +020032#define CHECK(expr) \
33 do { if (!(expr)) { _PyObject_ASSERT_FAILED_MSG(op, Py_STRINGIFY(expr)); } } while (0)
Victor Stinner0fc91ee2019-04-12 21:51:34 +020034
Victor Stinner68762572019-10-07 18:42:01 +020035 CHECK(!_PyObject_IsFreed(op));
36 CHECK(Py_REFCNT(op) >= 1);
37
Victor Stinner58ac7002020-02-07 03:04:21 +010038 _PyType_CheckConsistency(Py_TYPE(op));
Victor Stinner0fc91ee2019-04-12 21:51:34 +020039
40 if (PyUnicode_Check(op)) {
41 _PyUnicode_CheckConsistency(op, check_content);
42 }
43 else if (PyDict_Check(op)) {
44 _PyDict_CheckConsistency(op, check_content);
45 }
46 return 1;
Victor Stinner68762572019-10-07 18:42:01 +020047
48#undef CHECK
Victor Stinner0fc91ee2019-04-12 21:51:34 +020049}
50
51
Tim Peters34592512002-07-11 06:23:50 +000052#ifdef Py_REF_DEBUG
Neal Norwitz84632ee2006-03-04 20:00:59 +000053Py_ssize_t _Py_RefTotal;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000054
55Py_ssize_t
56_Py_GetRefTotal(void)
57{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000058 PyObject *o;
59 Py_ssize_t total = _Py_RefTotal;
Antoine Pitrou9d952542013-08-24 21:07:07 +020060 o = _PySet_Dummy;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000061 if (o != NULL)
Victor Stinnera93c51e2020-02-07 00:38:59 +010062 total -= Py_REFCNT(o);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000063 return total;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000064}
Nick Coghland6009512014-11-20 21:39:37 +100065
66void
67_PyDebug_PrintTotalRefs(void) {
Eric Snowdae02762017-09-14 00:35:58 -070068 fprintf(stderr,
69 "[%" PY_FORMAT_SIZE_T "d refs, "
70 "%" PY_FORMAT_SIZE_T "d blocks]\n",
71 _Py_GetRefTotal(), _Py_GetAllocatedBlocks());
Nick Coghland6009512014-11-20 21:39:37 +100072}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000073#endif /* Py_REF_DEBUG */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000074
Guido van Rossum3f5da241990-12-20 15:06:42 +000075/* Object allocation routines used by NEWOBJ and NEWVAROBJ macros.
76 These are used by the individual routines for object creation.
77 Do not call them otherwise, they do not initialize the object! */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000078
Tim Peters78be7992003-03-23 02:51:01 +000079#ifdef Py_TRACE_REFS
Tim Peters7571a0f2003-03-23 17:52:28 +000080/* Head of circular doubly-linked list of all objects. These are linked
81 * together via the _ob_prev and _ob_next members of a PyObject, which
82 * exist only in a Py_TRACE_REFS build.
83 */
Tim Peters78be7992003-03-23 02:51:01 +000084static PyObject refchain = {&refchain, &refchain};
Tim Peters36eb4df2003-03-23 03:33:13 +000085
Tim Peters7571a0f2003-03-23 17:52:28 +000086/* Insert op at the front of the list of all objects. If force is true,
87 * op is added even if _ob_prev and _ob_next are non-NULL already. If
88 * force is false amd _ob_prev or _ob_next are non-NULL, do nothing.
89 * force should be true if and only if op points to freshly allocated,
90 * uninitialized memory, or you've unlinked op from the list and are
Tim Peters51f8d382003-03-23 18:06:08 +000091 * relinking it into the front.
Tim Peters7571a0f2003-03-23 17:52:28 +000092 * Note that objects are normally added to the list via _Py_NewReference,
93 * which is called by PyObject_Init. Not all objects are initialized that
94 * way, though; exceptions include statically allocated type objects, and
95 * statically allocated singletons (like Py_True and Py_None).
96 */
Victor Stinner58f4e1a2020-02-05 18:24:33 +010097void
Tim Peters7571a0f2003-03-23 17:52:28 +000098_Py_AddToAllObjects(PyObject *op, int force)
Tim Peters36eb4df2003-03-23 03:33:13 +000099{
Tim Peters7571a0f2003-03-23 17:52:28 +0000100#ifdef Py_DEBUG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000101 if (!force) {
102 /* If it's initialized memory, op must be in or out of
103 * the list unambiguously.
104 */
Victor Stinner24702042018-10-26 17:16:37 +0200105 _PyObject_ASSERT(op, (op->_ob_prev == NULL) == (op->_ob_next == NULL));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000106 }
Tim Peters78be7992003-03-23 02:51:01 +0000107#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000108 if (force || op->_ob_prev == NULL) {
109 op->_ob_next = refchain._ob_next;
110 op->_ob_prev = &refchain;
111 refchain._ob_next->_ob_prev = op;
112 refchain._ob_next = op;
113 }
Tim Peters7571a0f2003-03-23 17:52:28 +0000114}
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000115#endif /* Py_TRACE_REFS */
Tim Peters78be7992003-03-23 02:51:01 +0000116
Tim Peters7c321a82002-07-09 02:57:01 +0000117#ifdef Py_REF_DEBUG
118/* Log a fatal error; doesn't return. */
119void
Victor Stinner18618e652018-10-25 17:28:11 +0200120_Py_NegativeRefcount(const char *filename, int lineno, PyObject *op)
Tim Peters7c321a82002-07-09 02:57:01 +0000121{
Victor Stinnerf1d002c2018-11-21 23:53:44 +0100122 _PyObject_AssertFailed(op, NULL, "object has negative ref count",
Victor Stinner3ec9af72018-10-26 02:12:34 +0200123 filename, lineno, __func__);
Tim Peters7c321a82002-07-09 02:57:01 +0000124}
125
126#endif /* Py_REF_DEBUG */
127
Thomas Heller1328b522004-04-22 17:23:49 +0000128void
129Py_IncRef(PyObject *o)
130{
131 Py_XINCREF(o);
132}
133
134void
135Py_DecRef(PyObject *o)
136{
137 Py_XDECREF(o);
138}
139
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000140PyObject *
Fred Drake100814d2000-07-09 15:48:49 +0000141PyObject_Init(PyObject *op, PyTypeObject *tp)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000142{
Victor Stinnerf58bd7c2020-02-05 13:12:19 +0100143 /* Any changes should be reflected in PyObject_INIT() macro */
144 if (op == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000145 return PyErr_NoMemory();
Victor Stinnerf58bd7c2020-02-05 13:12:19 +0100146 }
147
Victor Stinner1fb5a9f2020-03-06 15:55:14 +0100148 return PyObject_INIT(op, tp);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000149}
150
Guido van Rossumb18618d2000-05-03 23:44:39 +0000151PyVarObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000152PyObject_InitVar(PyVarObject *op, PyTypeObject *tp, Py_ssize_t size)
Guido van Rossumb18618d2000-05-03 23:44:39 +0000153{
Victor Stinnerf58bd7c2020-02-05 13:12:19 +0100154 /* Any changes should be reflected in PyObject_INIT_VAR() macro */
155 if (op == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000156 return (PyVarObject *) PyErr_NoMemory();
Victor Stinnerf58bd7c2020-02-05 13:12:19 +0100157 }
158
Victor Stinner1fb5a9f2020-03-06 15:55:14 +0100159 return PyObject_INIT_VAR(op, tp, size);
Guido van Rossumb18618d2000-05-03 23:44:39 +0000160}
161
162PyObject *
Fred Drake100814d2000-07-09 15:48:49 +0000163_PyObject_New(PyTypeObject *tp)
Guido van Rossumb18618d2000-05-03 23:44:39 +0000164{
Victor Stinner92055202020-04-08 00:38:15 +0200165 PyObject *op = (PyObject *) PyObject_MALLOC(_PyObject_SIZE(tp));
166 if (op == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000167 return PyErr_NoMemory();
Victor Stinner92055202020-04-08 00:38:15 +0200168 }
169 PyObject_INIT(op, tp);
170 return op;
Guido van Rossumb18618d2000-05-03 23:44:39 +0000171}
172
Guido van Rossumd0c87ee1997-05-15 21:31:03 +0000173PyVarObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000174_PyObject_NewVar(PyTypeObject *tp, Py_ssize_t nitems)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000175{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000176 PyVarObject *op;
177 const size_t size = _PyObject_VAR_SIZE(tp, nitems);
178 op = (PyVarObject *) PyObject_MALLOC(size);
179 if (op == NULL)
180 return (PyVarObject *)PyErr_NoMemory();
181 return PyObject_INIT_VAR(op, tp, nitems);
Guido van Rossumb18618d2000-05-03 23:44:39 +0000182}
183
Antoine Pitrou796564c2013-07-30 19:59:21 +0200184void
185PyObject_CallFinalizer(PyObject *self)
186{
187 PyTypeObject *tp = Py_TYPE(self);
188
Antoine Pitrouada319b2019-05-29 22:12:38 +0200189 if (tp->tp_finalize == NULL)
Antoine Pitrou796564c2013-07-30 19:59:21 +0200190 return;
191 /* tp_finalize should only be called once. */
Victor Stinner45ec5b92020-04-08 01:42:27 +0200192 if (_PyType_IS_GC(tp) && _PyGC_FINALIZED(self))
Antoine Pitrou796564c2013-07-30 19:59:21 +0200193 return;
194
195 tp->tp_finalize(self);
Victor Stinner45ec5b92020-04-08 01:42:27 +0200196 if (_PyType_IS_GC(tp)) {
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900197 _PyGC_SET_FINALIZED(self);
198 }
Antoine Pitrou796564c2013-07-30 19:59:21 +0200199}
200
201int
202PyObject_CallFinalizerFromDealloc(PyObject *self)
203{
Victor Stinnera93c51e2020-02-07 00:38:59 +0100204 if (Py_REFCNT(self) != 0) {
Victor Stinner5eb8bff2020-01-30 09:01:07 +0100205 _PyObject_ASSERT_FAILED_MSG(self,
206 "PyObject_CallFinalizerFromDealloc called "
207 "on object with a non-zero refcount");
208 }
Antoine Pitrou796564c2013-07-30 19:59:21 +0200209
210 /* Temporarily resurrect the object. */
Victor Stinnerc86a1122020-02-07 01:24:29 +0100211 Py_SET_REFCNT(self, 1);
Antoine Pitrou796564c2013-07-30 19:59:21 +0200212
213 PyObject_CallFinalizer(self);
214
Victor Stinner24702042018-10-26 17:16:37 +0200215 _PyObject_ASSERT_WITH_MSG(self,
Victor Stinnera93c51e2020-02-07 00:38:59 +0100216 Py_REFCNT(self) > 0,
Victor Stinner24702042018-10-26 17:16:37 +0200217 "refcount is too small");
Victor Stinner5eb8bff2020-01-30 09:01:07 +0100218
219 /* Undo the temporary resurrection; can't use DECREF here, it would
220 * cause a recursive call. */
Victor Stinnerc86a1122020-02-07 01:24:29 +0100221 Py_SET_REFCNT(self, Py_REFCNT(self) - 1);
222 if (Py_REFCNT(self) == 0) {
Antoine Pitrou796564c2013-07-30 19:59:21 +0200223 return 0; /* this is the normal path out */
Victor Stinner5eb8bff2020-01-30 09:01:07 +0100224 }
Antoine Pitrou796564c2013-07-30 19:59:21 +0200225
226 /* tp_finalize resurrected it! Make it look like the original Py_DECREF
Victor Stinner5eb8bff2020-01-30 09:01:07 +0100227 * never happened. */
Victor Stinnera93c51e2020-02-07 00:38:59 +0100228 Py_ssize_t refcnt = Py_REFCNT(self);
Antoine Pitrou796564c2013-07-30 19:59:21 +0200229 _Py_NewReference(self);
Victor Stinnerc86a1122020-02-07 01:24:29 +0100230 Py_SET_REFCNT(self, refcnt);
Antoine Pitrou796564c2013-07-30 19:59:21 +0200231
Victor Stinner24702042018-10-26 17:16:37 +0200232 _PyObject_ASSERT(self,
Victor Stinner45ec5b92020-04-08 01:42:27 +0200233 (!_PyType_IS_GC(Py_TYPE(self))
Victor Stinner24702042018-10-26 17:16:37 +0200234 || _PyObject_GC_IS_TRACKED(self)));
Victor Stinner49932fe2020-02-03 17:55:05 +0100235 /* If Py_REF_DEBUG macro is defined, _Py_NewReference() increased
236 _Py_RefTotal, so we need to undo that. */
237#ifdef Py_REF_DEBUG
238 _Py_RefTotal--;
239#endif
Antoine Pitrou796564c2013-07-30 19:59:21 +0200240 return -1;
241}
242
Antoine Pitrouc47bd4a2010-07-27 22:08:27 +0000243int
244PyObject_Print(PyObject *op, FILE *fp, int flags)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000245{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000246 int ret = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000247 if (PyErr_CheckSignals())
248 return -1;
Guido van Rossum9b00dfa1998-04-28 16:06:54 +0000249#ifdef USE_STACKCHECK
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000250 if (PyOS_CheckStack()) {
251 PyErr_SetString(PyExc_MemoryError, "stack overflow");
252 return -1;
253 }
Guido van Rossum9b00dfa1998-04-28 16:06:54 +0000254#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000255 clearerr(fp); /* Clear any previous error condition */
256 if (op == NULL) {
257 Py_BEGIN_ALLOW_THREADS
258 fprintf(fp, "<nil>");
259 Py_END_ALLOW_THREADS
260 }
261 else {
Victor Stinnera93c51e2020-02-07 00:38:59 +0100262 if (Py_REFCNT(op) <= 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000263 /* XXX(twouters) cast refcount to long until %zd is
264 universally available */
265 Py_BEGIN_ALLOW_THREADS
266 fprintf(fp, "<refcnt %ld at %p>",
Victor Stinnera93c51e2020-02-07 00:38:59 +0100267 (long)Py_REFCNT(op), (void *)op);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000268 Py_END_ALLOW_THREADS
Victor Stinner3ec9af72018-10-26 02:12:34 +0200269 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000270 else {
271 PyObject *s;
272 if (flags & Py_PRINT_RAW)
273 s = PyObject_Str(op);
274 else
275 s = PyObject_Repr(op);
276 if (s == NULL)
277 ret = -1;
278 else if (PyBytes_Check(s)) {
279 fwrite(PyBytes_AS_STRING(s), 1,
280 PyBytes_GET_SIZE(s), fp);
281 }
282 else if (PyUnicode_Check(s)) {
283 PyObject *t;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200284 t = PyUnicode_AsEncodedString(s, "utf-8", "backslashreplace");
Zackery Spytzae62f012018-10-06 00:44:25 -0600285 if (t == NULL) {
286 ret = -1;
287 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000288 else {
289 fwrite(PyBytes_AS_STRING(t), 1,
290 PyBytes_GET_SIZE(t), fp);
Victor Stinnerba6b4302010-05-17 09:33:42 +0000291 Py_DECREF(t);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000292 }
293 }
294 else {
295 PyErr_Format(PyExc_TypeError,
296 "str() or repr() returned '%.100s'",
Victor Stinner58ac7002020-02-07 03:04:21 +0100297 Py_TYPE(s)->tp_name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000298 ret = -1;
299 }
300 Py_XDECREF(s);
301 }
302 }
303 if (ret == 0) {
304 if (ferror(fp)) {
Serhiy Storchaka55fe1ae2017-04-16 10:46:38 +0300305 PyErr_SetFromErrno(PyExc_OSError);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000306 clearerr(fp);
307 ret = -1;
308 }
309 }
310 return ret;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000311}
312
Guido van Rossum38938152006-08-21 23:36:26 +0000313/* For debugging convenience. Set a breakpoint here and call it from your DLL */
314void
Thomas Woutersb2137042007-02-01 18:02:27 +0000315_Py_BreakPoint(void)
Guido van Rossum38938152006-08-21 23:36:26 +0000316{
317}
318
Neal Norwitz1a997502003-01-13 20:13:12 +0000319
Victor Stinner4c409be2019-04-11 13:01:15 +0200320/* Heuristic checking if the object memory is uninitialized or deallocated.
321 Rely on the debug hooks on Python memory allocators:
322 see _PyMem_IsPtrFreed().
Victor Stinner82af0b62018-10-23 17:39:40 +0200323
324 The function can be used to prevent segmentation fault on dereferencing
Victor Stinner4c409be2019-04-11 13:01:15 +0200325 pointers like 0xDDDDDDDDDDDDDDDD. */
Victor Stinner82af0b62018-10-23 17:39:40 +0200326int
327_PyObject_IsFreed(PyObject *op)
328{
Victor Stinner58ac7002020-02-07 03:04:21 +0100329 if (_PyMem_IsPtrFreed(op) || _PyMem_IsPtrFreed(Py_TYPE(op))) {
Victor Stinner2cf5d322018-11-22 16:32:57 +0100330 return 1;
331 }
Victor Stinner2b00db62019-04-11 11:33:27 +0200332 /* ignore op->ob_ref: its value can have be modified
Victor Stinner82af0b62018-10-23 17:39:40 +0200333 by Py_INCREF() and Py_DECREF(). */
334#ifdef Py_TRACE_REFS
Pablo Galindo36e33c32019-10-08 00:43:14 +0100335 if (op->_ob_next != NULL && _PyMem_IsPtrFreed(op->_ob_next)) {
Victor Stinner2b00db62019-04-11 11:33:27 +0200336 return 1;
337 }
Pablo Galindo36e33c32019-10-08 00:43:14 +0100338 if (op->_ob_prev != NULL && _PyMem_IsPtrFreed(op->_ob_prev)) {
339 return 1;
340 }
Victor Stinner82af0b62018-10-23 17:39:40 +0200341#endif
Victor Stinner2b00db62019-04-11 11:33:27 +0200342 return 0;
Victor Stinner82af0b62018-10-23 17:39:40 +0200343}
344
345
Barry Warsaw9bf16442001-01-23 16:24:35 +0000346/* For debugging convenience. See Misc/gdbinit for some useful gdb hooks */
Guido van Rossum38938152006-08-21 23:36:26 +0000347void
348_PyObject_Dump(PyObject* op)
Barry Warsaw9bf16442001-01-23 16:24:35 +0000349{
Victor Stinner82af0b62018-10-23 17:39:40 +0200350 if (_PyObject_IsFreed(op)) {
351 /* It seems like the object memory has been freed:
352 don't access it to prevent a segmentation fault. */
Victor Stinnerb39afb72019-09-17 23:36:28 +0200353 fprintf(stderr, "<object at %p is freed>\n", op);
Victor Stinner68762572019-10-07 18:42:01 +0200354 fflush(stderr);
Victor Stinner2cf5d322018-11-22 16:32:57 +0100355 return;
Victor Stinner82af0b62018-10-23 17:39:40 +0200356 }
357
Victor Stinner68762572019-10-07 18:42:01 +0200358 /* first, write fields which are the least likely to crash */
359 fprintf(stderr, "object address : %p\n", (void *)op);
Victor Stinner82af0b62018-10-23 17:39:40 +0200360 /* XXX(twouters) cast refcount to long until %zd is
361 universally available */
Victor Stinnera93c51e2020-02-07 00:38:59 +0100362 fprintf(stderr, "object refcount : %ld\n", (long)Py_REFCNT(op));
Victor Stinner68762572019-10-07 18:42:01 +0200363 fflush(stderr);
364
365 PyTypeObject *type = Py_TYPE(op);
366 fprintf(stderr, "object type : %p\n", type);
367 fprintf(stderr, "object type name: %s\n",
368 type==NULL ? "NULL" : type->tp_name);
369
370 /* the most dangerous part */
371 fprintf(stderr, "object repr : ");
372 fflush(stderr);
373
374 PyGILState_STATE gil = PyGILState_Ensure();
375 PyObject *error_type, *error_value, *error_traceback;
376 PyErr_Fetch(&error_type, &error_value, &error_traceback);
377
378 (void)PyObject_Print(op, stderr, 0);
379 fflush(stderr);
380
381 PyErr_Restore(error_type, error_value, error_traceback);
382 PyGILState_Release(gil);
383
384 fprintf(stderr, "\n");
Victor Stinner82af0b62018-10-23 17:39:40 +0200385 fflush(stderr);
Barry Warsaw9bf16442001-01-23 16:24:35 +0000386}
Barry Warsaw903138f2001-01-23 16:33:18 +0000387
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000388PyObject *
Fred Drake100814d2000-07-09 15:48:49 +0000389PyObject_Repr(PyObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000390{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000391 PyObject *res;
392 if (PyErr_CheckSignals())
393 return NULL;
Guido van Rossum9b00dfa1998-04-28 16:06:54 +0000394#ifdef USE_STACKCHECK
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000395 if (PyOS_CheckStack()) {
396 PyErr_SetString(PyExc_MemoryError, "stack overflow");
397 return NULL;
398 }
Guido van Rossum9b00dfa1998-04-28 16:06:54 +0000399#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000400 if (v == NULL)
401 return PyUnicode_FromString("<NULL>");
402 if (Py_TYPE(v)->tp_repr == NULL)
403 return PyUnicode_FromFormat("<%s object at %p>",
Victor Stinner58ac7002020-02-07 03:04:21 +0100404 Py_TYPE(v)->tp_name, v);
Victor Stinner33824f62013-08-26 14:05:19 +0200405
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100406 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinner33824f62013-08-26 14:05:19 +0200407#ifdef Py_DEBUG
408 /* PyObject_Repr() must not be called with an exception set,
Victor Stinnera8cb5152017-01-18 14:12:51 +0100409 because it can clear it (directly or indirectly) and so the
Martin Panter9955a372015-10-07 10:26:23 +0000410 caller loses its exception */
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100411 assert(!_PyErr_Occurred(tstate));
Victor Stinner33824f62013-08-26 14:05:19 +0200412#endif
413
Serhiy Storchaka1fb72d22017-12-03 22:12:11 +0200414 /* It is possible for a type to have a tp_repr representation that loops
415 infinitely. */
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100416 if (_Py_EnterRecursiveCall(tstate,
417 " while getting the repr of an object")) {
Serhiy Storchaka1fb72d22017-12-03 22:12:11 +0200418 return NULL;
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100419 }
Victor Stinner58ac7002020-02-07 03:04:21 +0100420 res = (*Py_TYPE(v)->tp_repr)(v);
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100421 _Py_LeaveRecursiveCall(tstate);
422
423 if (res == NULL) {
Victor Stinner0a54cf12011-12-01 03:22:44 +0100424 return NULL;
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100425 }
Victor Stinner0a54cf12011-12-01 03:22:44 +0100426 if (!PyUnicode_Check(res)) {
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100427 _PyErr_Format(tstate, PyExc_TypeError,
428 "__repr__ returned non-string (type %.200s)",
Victor Stinner58ac7002020-02-07 03:04:21 +0100429 Py_TYPE(res)->tp_name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000430 Py_DECREF(res);
431 return NULL;
432 }
Victor Stinnerdb88ae52011-12-01 02:15:00 +0100433#ifndef Py_DEBUG
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100434 if (PyUnicode_READY(res) < 0) {
Victor Stinnerdb88ae52011-12-01 02:15:00 +0100435 return NULL;
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100436 }
Victor Stinnerdb88ae52011-12-01 02:15:00 +0100437#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000438 return res;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000439}
440
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000441PyObject *
Guido van Rossum98297ee2007-11-06 21:34:58 +0000442PyObject_Str(PyObject *v)
Guido van Rossumc6004111993-11-05 10:22:19 +0000443{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000444 PyObject *res;
445 if (PyErr_CheckSignals())
446 return NULL;
Guido van Rossum98297ee2007-11-06 21:34:58 +0000447#ifdef USE_STACKCHECK
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000448 if (PyOS_CheckStack()) {
449 PyErr_SetString(PyExc_MemoryError, "stack overflow");
450 return NULL;
451 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000452#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000453 if (v == NULL)
454 return PyUnicode_FromString("<NULL>");
455 if (PyUnicode_CheckExact(v)) {
Victor Stinnerdb88ae52011-12-01 02:15:00 +0100456#ifndef Py_DEBUG
Victor Stinner4ead7c72011-11-20 19:48:36 +0100457 if (PyUnicode_READY(v) < 0)
458 return NULL;
Victor Stinnerdb88ae52011-12-01 02:15:00 +0100459#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000460 Py_INCREF(v);
461 return v;
462 }
463 if (Py_TYPE(v)->tp_str == NULL)
464 return PyObject_Repr(v);
Guido van Rossum4f288ab2001-05-01 16:53:37 +0000465
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100466 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinner33824f62013-08-26 14:05:19 +0200467#ifdef Py_DEBUG
468 /* PyObject_Str() must not be called with an exception set,
Victor Stinnera8cb5152017-01-18 14:12:51 +0100469 because it can clear it (directly or indirectly) and so the
Nick Coghland979e432014-02-09 10:43:21 +1000470 caller loses its exception */
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100471 assert(!_PyErr_Occurred(tstate));
Victor Stinner33824f62013-08-26 14:05:19 +0200472#endif
473
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000474 /* It is possible for a type to have a tp_str representation that loops
475 infinitely. */
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100476 if (_Py_EnterRecursiveCall(tstate, " while getting the str of an object")) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000477 return NULL;
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100478 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000479 res = (*Py_TYPE(v)->tp_str)(v);
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100480 _Py_LeaveRecursiveCall(tstate);
481
482 if (res == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000483 return NULL;
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100484 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000485 if (!PyUnicode_Check(res)) {
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100486 _PyErr_Format(tstate, PyExc_TypeError,
487 "__str__ returned non-string (type %.200s)",
488 Py_TYPE(res)->tp_name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000489 Py_DECREF(res);
490 return NULL;
491 }
Victor Stinnerdb88ae52011-12-01 02:15:00 +0100492#ifndef Py_DEBUG
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100493 if (PyUnicode_READY(res) < 0) {
Victor Stinner4ead7c72011-11-20 19:48:36 +0100494 return NULL;
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100495 }
Victor Stinnerdb88ae52011-12-01 02:15:00 +0100496#endif
Victor Stinner4ead7c72011-11-20 19:48:36 +0100497 assert(_PyUnicode_CheckConsistency(res, 1));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000498 return res;
Neil Schemenauercf52c072005-08-12 17:34:58 +0000499}
500
Georg Brandl559e5d72008-06-11 18:37:52 +0000501PyObject *
502PyObject_ASCII(PyObject *v)
503{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000504 PyObject *repr, *ascii, *res;
Georg Brandl559e5d72008-06-11 18:37:52 +0000505
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000506 repr = PyObject_Repr(v);
507 if (repr == NULL)
508 return NULL;
Georg Brandl559e5d72008-06-11 18:37:52 +0000509
Victor Stinneraf037572013-04-14 18:44:10 +0200510 if (PyUnicode_IS_ASCII(repr))
511 return repr;
512
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000513 /* repr is guaranteed to be a PyUnicode object by PyObject_Repr */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200514 ascii = _PyUnicode_AsASCIIString(repr, "backslashreplace");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000515 Py_DECREF(repr);
516 if (ascii == NULL)
517 return NULL;
Georg Brandl559e5d72008-06-11 18:37:52 +0000518
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000519 res = PyUnicode_DecodeASCII(
520 PyBytes_AS_STRING(ascii),
521 PyBytes_GET_SIZE(ascii),
522 NULL);
523
524 Py_DECREF(ascii);
525 return res;
Georg Brandl559e5d72008-06-11 18:37:52 +0000526}
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000527
Benjamin Petersonc15a0732008-08-26 16:46:47 +0000528PyObject *
529PyObject_Bytes(PyObject *v)
530{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000531 PyObject *result, *func;
Benjamin Petersonc15a0732008-08-26 16:46:47 +0000532
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000533 if (v == NULL)
534 return PyBytes_FromString("<NULL>");
Benjamin Petersonc15a0732008-08-26 16:46:47 +0000535
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000536 if (PyBytes_CheckExact(v)) {
537 Py_INCREF(v);
538 return v;
539 }
Benjamin Petersonc15a0732008-08-26 16:46:47 +0000540
Benjamin Petersonce798522012-01-22 11:24:29 -0500541 func = _PyObject_LookupSpecial(v, &PyId___bytes__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000542 if (func != NULL) {
Victor Stinnerf17c3de2016-12-06 18:46:19 +0100543 result = _PyObject_CallNoArg(func);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000544 Py_DECREF(func);
545 if (result == NULL)
Benjamin Peterson41ece392010-09-11 16:39:57 +0000546 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000547 if (!PyBytes_Check(result)) {
Benjamin Peterson41ece392010-09-11 16:39:57 +0000548 PyErr_Format(PyExc_TypeError,
549 "__bytes__ returned non-bytes (type %.200s)",
550 Py_TYPE(result)->tp_name);
551 Py_DECREF(result);
552 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000553 }
554 return result;
555 }
556 else if (PyErr_Occurred())
557 return NULL;
558 return PyBytes_FromObject(v);
Benjamin Petersonc15a0732008-08-26 16:46:47 +0000559}
560
Jeroen Demeyerbf17d412019-11-05 16:48:04 +0100561
562/*
563def _PyObject_FunctionStr(x):
564 try:
565 qualname = x.__qualname__
566 except AttributeError:
567 return str(x)
568 try:
569 mod = x.__module__
570 if mod is not None and mod != 'builtins':
571 return f"{x.__module__}.{qualname}()"
572 except AttributeError:
573 pass
574 return qualname
575*/
576PyObject *
577_PyObject_FunctionStr(PyObject *x)
578{
579 _Py_IDENTIFIER(__module__);
580 _Py_IDENTIFIER(__qualname__);
581 _Py_IDENTIFIER(builtins);
582 assert(!PyErr_Occurred());
583 PyObject *qualname;
584 int ret = _PyObject_LookupAttrId(x, &PyId___qualname__, &qualname);
585 if (qualname == NULL) {
586 if (ret < 0) {
587 return NULL;
588 }
589 return PyObject_Str(x);
590 }
591 PyObject *module;
592 PyObject *result = NULL;
593 ret = _PyObject_LookupAttrId(x, &PyId___module__, &module);
594 if (module != NULL && module != Py_None) {
595 PyObject *builtinsname = _PyUnicode_FromId(&PyId_builtins);
596 if (builtinsname == NULL) {
597 goto done;
598 }
599 ret = PyObject_RichCompareBool(module, builtinsname, Py_NE);
600 if (ret < 0) {
601 // error
602 goto done;
603 }
604 if (ret > 0) {
605 result = PyUnicode_FromFormat("%S.%S()", module, qualname);
606 goto done;
607 }
608 }
609 else if (ret < 0) {
610 goto done;
611 }
612 result = PyUnicode_FromFormat("%S()", qualname);
613done:
614 Py_DECREF(qualname);
615 Py_XDECREF(module);
616 return result;
617}
618
Mark Dickinsonc008a172009-02-01 13:59:22 +0000619/* For Python 3.0.1 and later, the old three-way comparison has been
620 completely removed in favour of rich comparisons. PyObject_Compare() and
621 PyObject_Cmp() are gone, and the builtin cmp function no longer exists.
Jeroen Demeyer530f5062019-05-31 04:13:39 +0200622 The old tp_compare slot has been renamed to tp_as_async, and should no
Mark Dickinsonc008a172009-02-01 13:59:22 +0000623 longer be used. Use tp_richcompare instead.
Guido van Rossum98297ee2007-11-06 21:34:58 +0000624
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000625 See (*) below for practical amendments.
626
Mark Dickinsonc008a172009-02-01 13:59:22 +0000627 tp_richcompare gets called with a first argument of the appropriate type
628 and a second object of an arbitrary type. We never do any kind of
629 coercion.
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000630
Mark Dickinsonc008a172009-02-01 13:59:22 +0000631 The tp_richcompare slot should return an object, as follows:
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000632
633 NULL if an exception occurred
634 NotImplemented if the requested comparison is not implemented
635 any other false value if the requested comparison is false
636 any other true value if the requested comparison is true
637
638 The PyObject_RichCompare[Bool]() wrappers raise TypeError when they get
639 NotImplemented.
640
641 (*) Practical amendments:
642
643 - If rich comparison returns NotImplemented, == and != are decided by
644 comparing the object pointer (i.e. falling back to the base object
645 implementation).
646
Guido van Rossuma4073002002-05-31 20:03:54 +0000647*/
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000648
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000649/* Map rich comparison operators to their swapped version, e.g. LT <--> GT */
Brett Cannona5ca2e72004-09-25 01:37:24 +0000650int _Py_SwappedOp[] = {Py_GT, Py_GE, Py_EQ, Py_NE, Py_LT, Py_LE};
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +0000651
Serhiy Storchaka2d06e842015-12-25 19:53:18 +0200652static const char * const opstrings[] = {"<", "<=", "==", "!=", ">", ">="};
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000653
654/* Perform a rich comparison, raising TypeError when the requested comparison
655 operator is not supported. */
Guido van Rossume797ec12001-01-17 15:24:28 +0000656static PyObject *
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100657do_richcompare(PyThreadState *tstate, PyObject *v, PyObject *w, int op)
Guido van Rossume797ec12001-01-17 15:24:28 +0000658{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000659 richcmpfunc f;
660 PyObject *res;
661 int checked_reverse_op = 0;
Guido van Rossume797ec12001-01-17 15:24:28 +0000662
Andy Lester55728702020-03-06 16:53:17 -0600663 if (!Py_IS_TYPE(v, Py_TYPE(w)) &&
Victor Stinner58ac7002020-02-07 03:04:21 +0100664 PyType_IsSubtype(Py_TYPE(w), Py_TYPE(v)) &&
665 (f = Py_TYPE(w)->tp_richcompare) != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000666 checked_reverse_op = 1;
667 res = (*f)(w, v, _Py_SwappedOp[op]);
668 if (res != Py_NotImplemented)
669 return res;
670 Py_DECREF(res);
671 }
Victor Stinner58ac7002020-02-07 03:04:21 +0100672 if ((f = Py_TYPE(v)->tp_richcompare) != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000673 res = (*f)(v, w, op);
674 if (res != Py_NotImplemented)
675 return res;
676 Py_DECREF(res);
677 }
Victor Stinner58ac7002020-02-07 03:04:21 +0100678 if (!checked_reverse_op && (f = Py_TYPE(w)->tp_richcompare) != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000679 res = (*f)(w, v, _Py_SwappedOp[op]);
680 if (res != Py_NotImplemented)
681 return res;
682 Py_DECREF(res);
683 }
684 /* If neither object implements it, provide a sensible default
685 for == and !=, but raise an exception for ordering. */
686 switch (op) {
687 case Py_EQ:
688 res = (v == w) ? Py_True : Py_False;
689 break;
690 case Py_NE:
691 res = (v != w) ? Py_True : Py_False;
692 break;
693 default:
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100694 _PyErr_Format(tstate, PyExc_TypeError,
695 "'%s' not supported between instances of '%.100s' and '%.100s'",
696 opstrings[op],
Victor Stinner58ac7002020-02-07 03:04:21 +0100697 Py_TYPE(v)->tp_name,
698 Py_TYPE(w)->tp_name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000699 return NULL;
700 }
701 Py_INCREF(res);
702 return res;
Guido van Rossume797ec12001-01-17 15:24:28 +0000703}
704
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000705/* Perform a rich comparison with object result. This wraps do_richcompare()
706 with a check for NULL arguments and a recursion check. */
Guido van Rossume797ec12001-01-17 15:24:28 +0000707
Guido van Rossume797ec12001-01-17 15:24:28 +0000708PyObject *
709PyObject_RichCompare(PyObject *v, PyObject *w, int op)
710{
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100711 PyThreadState *tstate = _PyThreadState_GET();
Guido van Rossume797ec12001-01-17 15:24:28 +0000712
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000713 assert(Py_LT <= op && op <= Py_GE);
714 if (v == NULL || w == NULL) {
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100715 if (!_PyErr_Occurred(tstate)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000716 PyErr_BadInternalCall();
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100717 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000718 return NULL;
719 }
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100720 if (_Py_EnterRecursiveCall(tstate, " in comparison")) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000721 return NULL;
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100722 }
723 PyObject *res = do_richcompare(tstate, v, w, op);
724 _Py_LeaveRecursiveCall(tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000725 return res;
Guido van Rossume797ec12001-01-17 15:24:28 +0000726}
727
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000728/* Perform a rich comparison with integer result. This wraps
729 PyObject_RichCompare(), returning -1 for error, 0 for false, 1 for true. */
Guido van Rossume797ec12001-01-17 15:24:28 +0000730int
731PyObject_RichCompareBool(PyObject *v, PyObject *w, int op)
732{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000733 PyObject *res;
734 int ok;
Guido van Rossume797ec12001-01-17 15:24:28 +0000735
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000736 /* Quick result when objects are the same.
737 Guarantees that identity implies equality. */
738 if (v == w) {
739 if (op == Py_EQ)
740 return 1;
741 else if (op == Py_NE)
742 return 0;
743 }
Mark Dickinson4a1f5932008-11-12 23:23:36 +0000744
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000745 res = PyObject_RichCompare(v, w, op);
746 if (res == NULL)
747 return -1;
748 if (PyBool_Check(res))
749 ok = (res == Py_True);
750 else
751 ok = PyObject_IsTrue(res);
752 Py_DECREF(res);
753 return ok;
Guido van Rossume797ec12001-01-17 15:24:28 +0000754}
Fred Drake13634cf2000-06-29 19:17:04 +0000755
Antoine Pitrouce4a9da2011-11-21 20:46:33 +0100756Py_hash_t
Nick Coghland1abd252008-07-15 15:46:38 +0000757PyObject_HashNotImplemented(PyObject *v)
758{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000759 PyErr_Format(PyExc_TypeError, "unhashable type: '%.200s'",
760 Py_TYPE(v)->tp_name);
761 return -1;
Nick Coghland1abd252008-07-15 15:46:38 +0000762}
Fred Drake13634cf2000-06-29 19:17:04 +0000763
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000764Py_hash_t
Fred Drake100814d2000-07-09 15:48:49 +0000765PyObject_Hash(PyObject *v)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000766{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000767 PyTypeObject *tp = Py_TYPE(v);
768 if (tp->tp_hash != NULL)
769 return (*tp->tp_hash)(v);
770 /* To keep to the general practice that inheriting
771 * solely from object in C code should work without
772 * an explicit call to PyType_Ready, we implicitly call
773 * PyType_Ready here and then check the tp_hash slot again
774 */
775 if (tp->tp_dict == NULL) {
776 if (PyType_Ready(tp) < 0)
777 return -1;
778 if (tp->tp_hash != NULL)
779 return (*tp->tp_hash)(v);
780 }
781 /* Otherwise, the object can't be hashed */
782 return PyObject_HashNotImplemented(v);
Guido van Rossum9bfef441993-03-29 10:43:31 +0000783}
784
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000785PyObject *
Jeremy Hyltonaf68c872005-12-10 18:50:16 +0000786PyObject_GetAttrString(PyObject *v, const char *name)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000787{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000788 PyObject *w, *res;
Guido van Rossumd8eb1b31996-08-09 20:52:03 +0000789
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000790 if (Py_TYPE(v)->tp_getattr != NULL)
791 return (*Py_TYPE(v)->tp_getattr)(v, (char*)name);
INADA Naoki3e8d6cb2017-02-21 23:57:25 +0900792 w = PyUnicode_FromString(name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000793 if (w == NULL)
794 return NULL;
795 res = PyObject_GetAttr(v, w);
Victor Stinner59af08f2012-03-22 02:09:08 +0100796 Py_DECREF(w);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000797 return res;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000798}
799
800int
Jeremy Hyltonaf68c872005-12-10 18:50:16 +0000801PyObject_HasAttrString(PyObject *v, const char *name)
Guido van Rossumed18fdc1993-07-11 19:55:34 +0000802{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000803 PyObject *res = PyObject_GetAttrString(v, name);
804 if (res != NULL) {
805 Py_DECREF(res);
806 return 1;
807 }
808 PyErr_Clear();
809 return 0;
Guido van Rossumed18fdc1993-07-11 19:55:34 +0000810}
811
812int
Jeremy Hyltonaf68c872005-12-10 18:50:16 +0000813PyObject_SetAttrString(PyObject *v, const char *name, PyObject *w)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000814{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000815 PyObject *s;
816 int res;
Guido van Rossumd8eb1b31996-08-09 20:52:03 +0000817
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000818 if (Py_TYPE(v)->tp_setattr != NULL)
819 return (*Py_TYPE(v)->tp_setattr)(v, (char*)name, w);
820 s = PyUnicode_InternFromString(name);
821 if (s == NULL)
822 return -1;
823 res = PyObject_SetAttr(v, s, w);
824 Py_XDECREF(s);
825 return res;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000826}
827
Benjamin Petersonbfebb7b2011-12-15 15:34:02 -0500828int
829_PyObject_IsAbstract(PyObject *obj)
830{
831 int res;
832 PyObject* isabstract;
Benjamin Petersonbfebb7b2011-12-15 15:34:02 -0500833
834 if (obj == NULL)
835 return 0;
836
Serhiy Storchakaf320be72018-01-25 10:49:40 +0200837 res = _PyObject_LookupAttrId(obj, &PyId___isabstractmethod__, &isabstract);
838 if (res > 0) {
839 res = PyObject_IsTrue(isabstract);
840 Py_DECREF(isabstract);
Benjamin Petersonbfebb7b2011-12-15 15:34:02 -0500841 }
Benjamin Petersonbfebb7b2011-12-15 15:34:02 -0500842 return res;
843}
844
Guido van Rossum98ff96a1997-05-20 18:34:44 +0000845PyObject *
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200846_PyObject_GetAttrId(PyObject *v, _Py_Identifier *name)
847{
848 PyObject *result;
Martin v. Löwisd10759f2011-11-07 13:00:05 +0100849 PyObject *oname = _PyUnicode_FromId(name); /* borrowed */
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200850 if (!oname)
851 return NULL;
852 result = PyObject_GetAttr(v, oname);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200853 return result;
854}
855
856int
857_PyObject_HasAttrId(PyObject *v, _Py_Identifier *name)
858{
859 int result;
Martin v. Löwisd10759f2011-11-07 13:00:05 +0100860 PyObject *oname = _PyUnicode_FromId(name); /* borrowed */
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200861 if (!oname)
862 return -1;
863 result = PyObject_HasAttr(v, oname);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200864 return result;
865}
866
867int
868_PyObject_SetAttrId(PyObject *v, _Py_Identifier *name, PyObject *w)
869{
870 int result;
Martin v. Löwisd10759f2011-11-07 13:00:05 +0100871 PyObject *oname = _PyUnicode_FromId(name); /* borrowed */
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200872 if (!oname)
873 return -1;
874 result = PyObject_SetAttr(v, oname, w);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200875 return result;
876}
877
878PyObject *
Fred Drake100814d2000-07-09 15:48:49 +0000879PyObject_GetAttr(PyObject *v, PyObject *name)
Guido van Rossum98ff96a1997-05-20 18:34:44 +0000880{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000881 PyTypeObject *tp = Py_TYPE(v);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000882
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000883 if (!PyUnicode_Check(name)) {
884 PyErr_Format(PyExc_TypeError,
885 "attribute name must be string, not '%.200s'",
Victor Stinner58ac7002020-02-07 03:04:21 +0100886 Py_TYPE(name)->tp_name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000887 return NULL;
888 }
889 if (tp->tp_getattro != NULL)
890 return (*tp->tp_getattro)(v, name);
891 if (tp->tp_getattr != NULL) {
Serhiy Storchaka2a404b62017-01-22 23:07:07 +0200892 const char *name_str = PyUnicode_AsUTF8(name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000893 if (name_str == NULL)
894 return NULL;
Serhiy Storchaka2a404b62017-01-22 23:07:07 +0200895 return (*tp->tp_getattr)(v, (char *)name_str);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000896 }
897 PyErr_Format(PyExc_AttributeError,
898 "'%.50s' object has no attribute '%U'",
899 tp->tp_name, name);
900 return NULL;
Guido van Rossum98ff96a1997-05-20 18:34:44 +0000901}
902
Serhiy Storchakaf320be72018-01-25 10:49:40 +0200903int
904_PyObject_LookupAttr(PyObject *v, PyObject *name, PyObject **result)
INADA Naoki378edee2018-01-16 20:52:41 +0900905{
906 PyTypeObject *tp = Py_TYPE(v);
INADA Naoki378edee2018-01-16 20:52:41 +0900907
908 if (!PyUnicode_Check(name)) {
909 PyErr_Format(PyExc_TypeError,
910 "attribute name must be string, not '%.200s'",
Victor Stinner58ac7002020-02-07 03:04:21 +0100911 Py_TYPE(name)->tp_name);
Serhiy Storchakaf320be72018-01-25 10:49:40 +0200912 *result = NULL;
913 return -1;
INADA Naoki378edee2018-01-16 20:52:41 +0900914 }
915
916 if (tp->tp_getattro == PyObject_GenericGetAttr) {
Serhiy Storchakaf320be72018-01-25 10:49:40 +0200917 *result = _PyObject_GenericGetAttrWithDict(v, name, NULL, 1);
918 if (*result != NULL) {
919 return 1;
920 }
921 if (PyErr_Occurred()) {
922 return -1;
923 }
924 return 0;
INADA Naoki378edee2018-01-16 20:52:41 +0900925 }
926 if (tp->tp_getattro != NULL) {
Serhiy Storchakaf320be72018-01-25 10:49:40 +0200927 *result = (*tp->tp_getattro)(v, name);
INADA Naoki378edee2018-01-16 20:52:41 +0900928 }
929 else if (tp->tp_getattr != NULL) {
930 const char *name_str = PyUnicode_AsUTF8(name);
Serhiy Storchakaf320be72018-01-25 10:49:40 +0200931 if (name_str == NULL) {
932 *result = NULL;
933 return -1;
934 }
935 *result = (*tp->tp_getattr)(v, (char *)name_str);
INADA Naoki378edee2018-01-16 20:52:41 +0900936 }
INADA Naokie76daeb2018-01-26 16:22:51 +0900937 else {
938 *result = NULL;
939 return 0;
940 }
941
Serhiy Storchakaf320be72018-01-25 10:49:40 +0200942 if (*result != NULL) {
943 return 1;
INADA Naoki378edee2018-01-16 20:52:41 +0900944 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +0200945 if (!PyErr_ExceptionMatches(PyExc_AttributeError)) {
946 return -1;
947 }
948 PyErr_Clear();
949 return 0;
950}
951
952int
953_PyObject_LookupAttrId(PyObject *v, _Py_Identifier *name, PyObject **result)
954{
955 PyObject *oname = _PyUnicode_FromId(name); /* borrowed */
956 if (!oname) {
957 *result = NULL;
958 return -1;
959 }
960 return _PyObject_LookupAttr(v, oname, result);
INADA Naoki378edee2018-01-16 20:52:41 +0900961}
962
Guido van Rossum98ff96a1997-05-20 18:34:44 +0000963int
Fred Drake100814d2000-07-09 15:48:49 +0000964PyObject_HasAttr(PyObject *v, PyObject *name)
Guido van Rossum98ff96a1997-05-20 18:34:44 +0000965{
Serhiy Storchakaf320be72018-01-25 10:49:40 +0200966 PyObject *res;
967 if (_PyObject_LookupAttr(v, name, &res) < 0) {
968 PyErr_Clear();
969 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000970 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +0200971 if (res == NULL) {
972 return 0;
973 }
974 Py_DECREF(res);
975 return 1;
Guido van Rossum98ff96a1997-05-20 18:34:44 +0000976}
977
978int
Fred Drake100814d2000-07-09 15:48:49 +0000979PyObject_SetAttr(PyObject *v, PyObject *name, PyObject *value)
Guido van Rossum98ff96a1997-05-20 18:34:44 +0000980{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000981 PyTypeObject *tp = Py_TYPE(v);
982 int err;
Marc-André Lemburge44e5072000-09-18 16:20:57 +0000983
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000984 if (!PyUnicode_Check(name)) {
985 PyErr_Format(PyExc_TypeError,
986 "attribute name must be string, not '%.200s'",
Victor Stinner58ac7002020-02-07 03:04:21 +0100987 Py_TYPE(name)->tp_name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000988 return -1;
989 }
990 Py_INCREF(name);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000991
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000992 PyUnicode_InternInPlace(&name);
993 if (tp->tp_setattro != NULL) {
994 err = (*tp->tp_setattro)(v, name, value);
995 Py_DECREF(name);
996 return err;
997 }
998 if (tp->tp_setattr != NULL) {
Serhiy Storchaka2a404b62017-01-22 23:07:07 +0200999 const char *name_str = PyUnicode_AsUTF8(name);
Zackery Spytze0dcb852019-04-28 06:58:52 -06001000 if (name_str == NULL) {
1001 Py_DECREF(name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001002 return -1;
Zackery Spytze0dcb852019-04-28 06:58:52 -06001003 }
Serhiy Storchaka2a404b62017-01-22 23:07:07 +02001004 err = (*tp->tp_setattr)(v, (char *)name_str, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001005 Py_DECREF(name);
1006 return err;
1007 }
1008 Py_DECREF(name);
Victor Stinnera93c51e2020-02-07 00:38:59 +01001009 _PyObject_ASSERT(name, Py_REFCNT(name) >= 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001010 if (tp->tp_getattr == NULL && tp->tp_getattro == NULL)
1011 PyErr_Format(PyExc_TypeError,
1012 "'%.100s' object has no attributes "
1013 "(%s .%U)",
1014 tp->tp_name,
1015 value==NULL ? "del" : "assign to",
1016 name);
1017 else
1018 PyErr_Format(PyExc_TypeError,
1019 "'%.100s' object has only read-only attributes "
1020 "(%s .%U)",
1021 tp->tp_name,
1022 value==NULL ? "del" : "assign to",
1023 name);
1024 return -1;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001025}
1026
1027/* Helper to get a pointer to an object's __dict__ slot, if any */
1028
1029PyObject **
1030_PyObject_GetDictPtr(PyObject *obj)
1031{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001032 Py_ssize_t dictoffset;
1033 PyTypeObject *tp = Py_TYPE(obj);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001034
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001035 dictoffset = tp->tp_dictoffset;
1036 if (dictoffset == 0)
1037 return NULL;
1038 if (dictoffset < 0) {
Victor Stinnerc65b3202020-02-07 11:18:33 +01001039 Py_ssize_t tsize = Py_SIZE(obj);
1040 if (tsize < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001041 tsize = -tsize;
Victor Stinnerc65b3202020-02-07 11:18:33 +01001042 }
1043 size_t size = _PyObject_VAR_SIZE(tp, tsize);
Guido van Rossum2eb0b872002-03-01 22:24:49 +00001044
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001045 dictoffset += (long)size;
Victor Stinner24702042018-10-26 17:16:37 +02001046 _PyObject_ASSERT(obj, dictoffset > 0);
1047 _PyObject_ASSERT(obj, dictoffset % SIZEOF_VOID_P == 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001048 }
1049 return (PyObject **) ((char *)obj + dictoffset);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001050}
1051
Tim Peters6d6c1a32001-08-02 04:15:00 +00001052PyObject *
Raymond Hettinger1da1dbf2003-03-17 19:46:11 +00001053PyObject_SelfIter(PyObject *obj)
Raymond Hettinger01538262003-03-17 08:24:35 +00001054{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001055 Py_INCREF(obj);
1056 return obj;
Raymond Hettinger01538262003-03-17 08:24:35 +00001057}
1058
Amaury Forgeot d'Arcf343e012009-01-12 23:58:21 +00001059/* Helper used when the __next__ method is removed from a type:
1060 tp_iternext is never NULL and can be safely called without checking
1061 on every iteration.
1062 */
1063
1064PyObject *
1065_PyObject_NextNotImplemented(PyObject *self)
1066{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001067 PyErr_Format(PyExc_TypeError,
1068 "'%.200s' object is not iterable",
1069 Py_TYPE(self)->tp_name);
1070 return NULL;
Amaury Forgeot d'Arcf343e012009-01-12 23:58:21 +00001071}
1072
Yury Selivanovf2392132016-12-13 19:03:51 -05001073
1074/* Specialized version of _PyObject_GenericGetAttrWithDict
1075 specifically for the LOAD_METHOD opcode.
1076
1077 Return 1 if a method is found, 0 if it's a regular attribute
1078 from __dict__ or something returned by using a descriptor
1079 protocol.
1080
1081 `method` will point to the resolved attribute or NULL. In the
1082 latter case, an error will be set.
1083*/
1084int
1085_PyObject_GetMethod(PyObject *obj, PyObject *name, PyObject **method)
1086{
1087 PyTypeObject *tp = Py_TYPE(obj);
1088 PyObject *descr;
1089 descrgetfunc f = NULL;
1090 PyObject **dictptr, *dict;
1091 PyObject *attr;
1092 int meth_found = 0;
1093
1094 assert(*method == NULL);
1095
1096 if (Py_TYPE(obj)->tp_getattro != PyObject_GenericGetAttr
1097 || !PyUnicode_Check(name)) {
1098 *method = PyObject_GetAttr(obj, name);
1099 return 0;
1100 }
1101
1102 if (tp->tp_dict == NULL && PyType_Ready(tp) < 0)
1103 return 0;
1104
1105 descr = _PyType_Lookup(tp, name);
1106 if (descr != NULL) {
1107 Py_INCREF(descr);
Victor Stinner45ec5b92020-04-08 01:42:27 +02001108 if (_PyType_HasFeature(Py_TYPE(descr), Py_TPFLAGS_METHOD_DESCRIPTOR)) {
Yury Selivanovf2392132016-12-13 19:03:51 -05001109 meth_found = 1;
1110 } else {
Victor Stinner58ac7002020-02-07 03:04:21 +01001111 f = Py_TYPE(descr)->tp_descr_get;
Yury Selivanovf2392132016-12-13 19:03:51 -05001112 if (f != NULL && PyDescr_IsData(descr)) {
Victor Stinner58ac7002020-02-07 03:04:21 +01001113 *method = f(descr, obj, (PyObject *)Py_TYPE(obj));
Yury Selivanovf2392132016-12-13 19:03:51 -05001114 Py_DECREF(descr);
1115 return 0;
1116 }
1117 }
1118 }
1119
1120 dictptr = _PyObject_GetDictPtr(obj);
1121 if (dictptr != NULL && (dict = *dictptr) != NULL) {
1122 Py_INCREF(dict);
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02001123 attr = PyDict_GetItemWithError(dict, name);
Yury Selivanovf2392132016-12-13 19:03:51 -05001124 if (attr != NULL) {
1125 Py_INCREF(attr);
1126 *method = attr;
1127 Py_DECREF(dict);
1128 Py_XDECREF(descr);
1129 return 0;
1130 }
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02001131 else {
1132 Py_DECREF(dict);
1133 if (PyErr_Occurred()) {
1134 Py_XDECREF(descr);
1135 return 0;
1136 }
1137 }
Yury Selivanovf2392132016-12-13 19:03:51 -05001138 }
1139
1140 if (meth_found) {
1141 *method = descr;
1142 return 1;
1143 }
1144
1145 if (f != NULL) {
1146 *method = f(descr, obj, (PyObject *)Py_TYPE(obj));
1147 Py_DECREF(descr);
1148 return 0;
1149 }
1150
1151 if (descr != NULL) {
1152 *method = descr;
1153 return 0;
1154 }
1155
1156 PyErr_Format(PyExc_AttributeError,
1157 "'%.50s' object has no attribute '%U'",
1158 tp->tp_name, name);
1159 return 0;
1160}
1161
1162/* Generic GetAttr functions - put these in your tp_[gs]etattro slot. */
Michael W. Hudson1593f502004-09-14 17:09:47 +00001163
Raymond Hettinger01538262003-03-17 08:24:35 +00001164PyObject *
INADA Naoki378edee2018-01-16 20:52:41 +09001165_PyObject_GenericGetAttrWithDict(PyObject *obj, PyObject *name,
1166 PyObject *dict, int suppress)
Tim Peters6d6c1a32001-08-02 04:15:00 +00001167{
Yury Selivanovf2392132016-12-13 19:03:51 -05001168 /* Make sure the logic of _PyObject_GetMethod is in sync with
1169 this method.
INADA Naoki378edee2018-01-16 20:52:41 +09001170
1171 When suppress=1, this function suppress AttributeError.
Yury Selivanovf2392132016-12-13 19:03:51 -05001172 */
1173
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001174 PyTypeObject *tp = Py_TYPE(obj);
1175 PyObject *descr = NULL;
1176 PyObject *res = NULL;
1177 descrgetfunc f;
1178 Py_ssize_t dictoffset;
1179 PyObject **dictptr;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001180
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001181 if (!PyUnicode_Check(name)){
1182 PyErr_Format(PyExc_TypeError,
1183 "attribute name must be string, not '%.200s'",
Victor Stinner58ac7002020-02-07 03:04:21 +01001184 Py_TYPE(name)->tp_name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001185 return NULL;
1186 }
Serhiy Storchaka55c861f2016-04-17 20:31:51 +03001187 Py_INCREF(name);
Guido van Rossumebca9fc2001-12-04 15:54:53 +00001188
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001189 if (tp->tp_dict == NULL) {
1190 if (PyType_Ready(tp) < 0)
1191 goto done;
1192 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001193
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001194 descr = _PyType_Lookup(tp, name);
Michael W. Hudsonb2c7de42003-08-15 13:07:47 +00001195
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001196 f = NULL;
1197 if (descr != NULL) {
Serhiy Storchaka55c861f2016-04-17 20:31:51 +03001198 Py_INCREF(descr);
Victor Stinner58ac7002020-02-07 03:04:21 +01001199 f = Py_TYPE(descr)->tp_descr_get;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001200 if (f != NULL && PyDescr_IsData(descr)) {
Victor Stinner58ac7002020-02-07 03:04:21 +01001201 res = f(descr, obj, (PyObject *)Py_TYPE(obj));
INADA Naoki378edee2018-01-16 20:52:41 +09001202 if (res == NULL && suppress &&
1203 PyErr_ExceptionMatches(PyExc_AttributeError)) {
1204 PyErr_Clear();
1205 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001206 goto done;
1207 }
1208 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001209
Antoine Pitrou1a9a9d52010-08-28 18:17:03 +00001210 if (dict == NULL) {
1211 /* Inline _PyObject_GetDictPtr */
1212 dictoffset = tp->tp_dictoffset;
1213 if (dictoffset != 0) {
1214 if (dictoffset < 0) {
Victor Stinnerc65b3202020-02-07 11:18:33 +01001215 Py_ssize_t tsize = Py_SIZE(obj);
1216 if (tsize < 0) {
Antoine Pitrou1a9a9d52010-08-28 18:17:03 +00001217 tsize = -tsize;
Victor Stinnerc65b3202020-02-07 11:18:33 +01001218 }
1219 size_t size = _PyObject_VAR_SIZE(tp, tsize);
Victor Stinner24702042018-10-26 17:16:37 +02001220 _PyObject_ASSERT(obj, size <= PY_SSIZE_T_MAX);
Guido van Rossumc66ff442002-08-19 16:50:48 +00001221
Serhiy Storchaka55c861f2016-04-17 20:31:51 +03001222 dictoffset += (Py_ssize_t)size;
Victor Stinner24702042018-10-26 17:16:37 +02001223 _PyObject_ASSERT(obj, dictoffset > 0);
1224 _PyObject_ASSERT(obj, dictoffset % SIZEOF_VOID_P == 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001225 }
Antoine Pitrou1a9a9d52010-08-28 18:17:03 +00001226 dictptr = (PyObject **) ((char *)obj + dictoffset);
1227 dict = *dictptr;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001228 }
1229 }
Antoine Pitrou1a9a9d52010-08-28 18:17:03 +00001230 if (dict != NULL) {
1231 Py_INCREF(dict);
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02001232 res = PyDict_GetItemWithError(dict, name);
Antoine Pitrou1a9a9d52010-08-28 18:17:03 +00001233 if (res != NULL) {
1234 Py_INCREF(res);
Antoine Pitrou1a9a9d52010-08-28 18:17:03 +00001235 Py_DECREF(dict);
1236 goto done;
1237 }
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02001238 else {
1239 Py_DECREF(dict);
1240 if (PyErr_Occurred()) {
1241 if (suppress && PyErr_ExceptionMatches(PyExc_AttributeError)) {
1242 PyErr_Clear();
1243 }
1244 else {
1245 goto done;
1246 }
1247 }
1248 }
Antoine Pitrou1a9a9d52010-08-28 18:17:03 +00001249 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001250
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001251 if (f != NULL) {
1252 res = f(descr, obj, (PyObject *)Py_TYPE(obj));
INADA Naoki378edee2018-01-16 20:52:41 +09001253 if (res == NULL && suppress &&
1254 PyErr_ExceptionMatches(PyExc_AttributeError)) {
1255 PyErr_Clear();
1256 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001257 goto done;
1258 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001259
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001260 if (descr != NULL) {
1261 res = descr;
Victor Stinner2d01dc02012-03-09 00:44:13 +01001262 descr = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001263 goto done;
1264 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001265
INADA Naoki378edee2018-01-16 20:52:41 +09001266 if (!suppress) {
1267 PyErr_Format(PyExc_AttributeError,
1268 "'%.50s' object has no attribute '%U'",
1269 tp->tp_name, name);
1270 }
Guido van Rossumebca9fc2001-12-04 15:54:53 +00001271 done:
Victor Stinner2d01dc02012-03-09 00:44:13 +01001272 Py_XDECREF(descr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001273 Py_DECREF(name);
1274 return res;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001275}
1276
Antoine Pitrou1a9a9d52010-08-28 18:17:03 +00001277PyObject *
1278PyObject_GenericGetAttr(PyObject *obj, PyObject *name)
1279{
INADA Naoki378edee2018-01-16 20:52:41 +09001280 return _PyObject_GenericGetAttrWithDict(obj, name, NULL, 0);
Antoine Pitrou1a9a9d52010-08-28 18:17:03 +00001281}
1282
Tim Peters6d6c1a32001-08-02 04:15:00 +00001283int
Antoine Pitrou1a9a9d52010-08-28 18:17:03 +00001284_PyObject_GenericSetAttrWithDict(PyObject *obj, PyObject *name,
1285 PyObject *value, PyObject *dict)
Tim Peters6d6c1a32001-08-02 04:15:00 +00001286{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001287 PyTypeObject *tp = Py_TYPE(obj);
1288 PyObject *descr;
1289 descrsetfunc f;
1290 PyObject **dictptr;
1291 int res = -1;
Guido van Rossumebca9fc2001-12-04 15:54:53 +00001292
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001293 if (!PyUnicode_Check(name)){
1294 PyErr_Format(PyExc_TypeError,
1295 "attribute name must be string, not '%.200s'",
Victor Stinner58ac7002020-02-07 03:04:21 +01001296 Py_TYPE(name)->tp_name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001297 return -1;
1298 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001299
Benjamin Peterson74529ad2012-03-09 07:25:32 -08001300 if (tp->tp_dict == NULL && PyType_Ready(tp) < 0)
1301 return -1;
1302
1303 Py_INCREF(name);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001304
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001305 descr = _PyType_Lookup(tp, name);
Victor Stinner2d01dc02012-03-09 00:44:13 +01001306
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001307 if (descr != NULL) {
Serhiy Storchaka55c861f2016-04-17 20:31:51 +03001308 Py_INCREF(descr);
Victor Stinner58ac7002020-02-07 03:04:21 +01001309 f = Py_TYPE(descr)->tp_descr_set;
Serhiy Storchaka55c861f2016-04-17 20:31:51 +03001310 if (f != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001311 res = f(descr, obj, value);
1312 goto done;
1313 }
1314 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001315
Steve Dowerb82e17e2019-05-23 08:45:22 -07001316 /* XXX [Steve Dower] These are really noisy - worth it? */
1317 /*if (PyType_Check(obj) || PyModule_Check(obj)) {
1318 if (value && PySys_Audit("object.__setattr__", "OOO", obj, name, value) < 0)
1319 return -1;
1320 if (!value && PySys_Audit("object.__delattr__", "OO", obj, name) < 0)
1321 return -1;
1322 }*/
1323
Antoine Pitrou1a9a9d52010-08-28 18:17:03 +00001324 if (dict == NULL) {
1325 dictptr = _PyObject_GetDictPtr(obj);
Serhiy Storchaka55c861f2016-04-17 20:31:51 +03001326 if (dictptr == NULL) {
1327 if (descr == NULL) {
1328 PyErr_Format(PyExc_AttributeError,
1329 "'%.100s' object has no attribute '%U'",
1330 tp->tp_name, name);
1331 }
1332 else {
1333 PyErr_Format(PyExc_AttributeError,
1334 "'%.50s' object attribute '%U' is read-only",
1335 tp->tp_name, name);
1336 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001337 goto done;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001338 }
Serhiy Storchaka55c861f2016-04-17 20:31:51 +03001339 res = _PyObjectDict_SetItem(tp, dictptr, name, value);
Antoine Pitrou1a9a9d52010-08-28 18:17:03 +00001340 }
Serhiy Storchaka55c861f2016-04-17 20:31:51 +03001341 else {
Antoine Pitrou1a9a9d52010-08-28 18:17:03 +00001342 Py_INCREF(dict);
1343 if (value == NULL)
1344 res = PyDict_DelItem(dict, name);
1345 else
1346 res = PyDict_SetItem(dict, name, value);
Benjamin Peterson74529ad2012-03-09 07:25:32 -08001347 Py_DECREF(dict);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001348 }
Serhiy Storchaka55c861f2016-04-17 20:31:51 +03001349 if (res < 0 && PyErr_ExceptionMatches(PyExc_KeyError))
1350 PyErr_SetObject(PyExc_AttributeError, name);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001351
Guido van Rossumebca9fc2001-12-04 15:54:53 +00001352 done:
Victor Stinner2d01dc02012-03-09 00:44:13 +01001353 Py_XDECREF(descr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001354 Py_DECREF(name);
1355 return res;
Guido van Rossum98ff96a1997-05-20 18:34:44 +00001356}
1357
Antoine Pitrou1a9a9d52010-08-28 18:17:03 +00001358int
1359PyObject_GenericSetAttr(PyObject *obj, PyObject *name, PyObject *value)
1360{
1361 return _PyObject_GenericSetAttrWithDict(obj, name, value, NULL);
1362}
1363
Benjamin Peterson8eb12692012-02-19 19:59:10 -05001364int
1365PyObject_GenericSetDict(PyObject *obj, PyObject *value, void *context)
1366{
Serhiy Storchaka576f1322016-01-05 21:27:54 +02001367 PyObject **dictptr = _PyObject_GetDictPtr(obj);
Benjamin Peterson8eb12692012-02-19 19:59:10 -05001368 if (dictptr == NULL) {
1369 PyErr_SetString(PyExc_AttributeError,
1370 "This object has no __dict__");
1371 return -1;
1372 }
1373 if (value == NULL) {
1374 PyErr_SetString(PyExc_TypeError, "cannot delete __dict__");
1375 return -1;
1376 }
1377 if (!PyDict_Check(value)) {
1378 PyErr_Format(PyExc_TypeError,
1379 "__dict__ must be set to a dictionary, "
1380 "not a '%.200s'", Py_TYPE(value)->tp_name);
1381 return -1;
1382 }
Serhiy Storchaka576f1322016-01-05 21:27:54 +02001383 Py_INCREF(value);
Serhiy Storchakaec397562016-04-06 09:50:03 +03001384 Py_XSETREF(*dictptr, value);
Benjamin Peterson8eb12692012-02-19 19:59:10 -05001385 return 0;
1386}
1387
Antoine Pitrou1a9a9d52010-08-28 18:17:03 +00001388
Guido van Rossum6ac258d1993-05-12 08:24:20 +00001389/* Test a value used as condition, e.g., in a for or if statement.
1390 Return -1 if an error occurred */
1391
1392int
Fred Drake100814d2000-07-09 15:48:49 +00001393PyObject_IsTrue(PyObject *v)
Guido van Rossum6ac258d1993-05-12 08:24:20 +00001394{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001395 Py_ssize_t res;
1396 if (v == Py_True)
1397 return 1;
1398 if (v == Py_False)
1399 return 0;
1400 if (v == Py_None)
1401 return 0;
Victor Stinner58ac7002020-02-07 03:04:21 +01001402 else if (Py_TYPE(v)->tp_as_number != NULL &&
1403 Py_TYPE(v)->tp_as_number->nb_bool != NULL)
1404 res = (*Py_TYPE(v)->tp_as_number->nb_bool)(v);
1405 else if (Py_TYPE(v)->tp_as_mapping != NULL &&
1406 Py_TYPE(v)->tp_as_mapping->mp_length != NULL)
1407 res = (*Py_TYPE(v)->tp_as_mapping->mp_length)(v);
1408 else if (Py_TYPE(v)->tp_as_sequence != NULL &&
1409 Py_TYPE(v)->tp_as_sequence->sq_length != NULL)
1410 res = (*Py_TYPE(v)->tp_as_sequence->sq_length)(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001411 else
1412 return 1;
1413 /* if it is negative, it should be either -1 or -2 */
1414 return (res > 0) ? 1 : Py_SAFE_DOWNCAST(res, Py_ssize_t, int);
Guido van Rossum6ac258d1993-05-12 08:24:20 +00001415}
1416
Tim Peters803526b2002-07-07 05:13:56 +00001417/* equivalent of 'not v'
Guido van Rossumc3d3f961998-04-09 17:53:59 +00001418 Return -1 if an error occurred */
1419
1420int
Fred Drake100814d2000-07-09 15:48:49 +00001421PyObject_Not(PyObject *v)
Guido van Rossumc3d3f961998-04-09 17:53:59 +00001422{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001423 int res;
1424 res = PyObject_IsTrue(v);
1425 if (res < 0)
1426 return res;
1427 return res == 0;
Guido van Rossumc3d3f961998-04-09 17:53:59 +00001428}
1429
Guido van Rossum49b11fe1995-01-26 00:38:22 +00001430/* Test whether an object can be called */
1431
1432int
Fred Drake100814d2000-07-09 15:48:49 +00001433PyCallable_Check(PyObject *x)
Guido van Rossum49b11fe1995-01-26 00:38:22 +00001434{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001435 if (x == NULL)
1436 return 0;
Victor Stinner58ac7002020-02-07 03:04:21 +01001437 return Py_TYPE(x)->tp_call != NULL;
Guido van Rossum49b11fe1995-01-26 00:38:22 +00001438}
1439
Tim Peters7eea37e2001-09-04 22:08:56 +00001440
Georg Brandle32b4222007-03-10 22:13:27 +00001441/* Helper for PyObject_Dir without arguments: returns the local scope. */
1442static PyObject *
Guido van Rossumad7d8d12007-04-13 01:39:34 +00001443_dir_locals(void)
Tim Peters305b5852001-09-17 02:38:46 +00001444{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001445 PyObject *names;
Victor Stinner41bb43a2013-10-29 01:19:37 +01001446 PyObject *locals;
Tim Peters305b5852001-09-17 02:38:46 +00001447
Victor Stinner41bb43a2013-10-29 01:19:37 +01001448 locals = PyEval_GetLocals();
1449 if (locals == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001450 return NULL;
Tim Peters305b5852001-09-17 02:38:46 +00001451
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001452 names = PyMapping_Keys(locals);
1453 if (!names)
1454 return NULL;
1455 if (!PyList_Check(names)) {
1456 PyErr_Format(PyExc_TypeError,
1457 "dir(): expected keys() of locals to be a list, "
1458 "not '%.200s'", Py_TYPE(names)->tp_name);
1459 Py_DECREF(names);
1460 return NULL;
1461 }
Benjamin Peterson3bbb7222011-06-11 16:12:08 -05001462 if (PyList_Sort(names)) {
1463 Py_DECREF(names);
1464 return NULL;
1465 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001466 /* the locals don't need to be DECREF'd */
1467 return names;
Georg Brandle32b4222007-03-10 22:13:27 +00001468}
1469
Benjamin Peterson82b00c12011-05-24 11:09:06 -05001470/* Helper for PyObject_Dir: object introspection. */
Georg Brandle32b4222007-03-10 22:13:27 +00001471static PyObject *
1472_dir_object(PyObject *obj)
1473{
Benjamin Peterson3bbb7222011-06-11 16:12:08 -05001474 PyObject *result, *sorted;
Benjamin Petersonce798522012-01-22 11:24:29 -05001475 PyObject *dirfunc = _PyObject_LookupSpecial(obj, &PyId___dir__);
Georg Brandle32b4222007-03-10 22:13:27 +00001476
Victor Stinner24702042018-10-26 17:16:37 +02001477 assert(obj != NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001478 if (dirfunc == NULL) {
Benjamin Peterson82b00c12011-05-24 11:09:06 -05001479 if (!PyErr_Occurred())
1480 PyErr_SetString(PyExc_TypeError, "object does not provide __dir__");
1481 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001482 }
Benjamin Peterson82b00c12011-05-24 11:09:06 -05001483 /* use __dir__ */
Victor Stinnerf17c3de2016-12-06 18:46:19 +01001484 result = _PyObject_CallNoArg(dirfunc);
Benjamin Peterson82b00c12011-05-24 11:09:06 -05001485 Py_DECREF(dirfunc);
1486 if (result == NULL)
1487 return NULL;
Benjamin Peterson3bbb7222011-06-11 16:12:08 -05001488 /* return sorted(result) */
1489 sorted = PySequence_List(result);
1490 Py_DECREF(result);
1491 if (sorted == NULL)
1492 return NULL;
1493 if (PyList_Sort(sorted)) {
1494 Py_DECREF(sorted);
1495 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001496 }
Benjamin Peterson3bbb7222011-06-11 16:12:08 -05001497 return sorted;
Georg Brandle32b4222007-03-10 22:13:27 +00001498}
1499
1500/* Implementation of dir() -- if obj is NULL, returns the names in the current
1501 (local) scope. Otherwise, performs introspection of the object: returns a
1502 sorted list of attribute names (supposedly) accessible from the object
1503*/
1504PyObject *
1505PyObject_Dir(PyObject *obj)
1506{
Benjamin Peterson3bbb7222011-06-11 16:12:08 -05001507 return (obj == NULL) ? _dir_locals() : _dir_object(obj);
Tim Peters7eea37e2001-09-04 22:08:56 +00001508}
Guido van Rossum49b11fe1995-01-26 00:38:22 +00001509
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001510/*
Raymond Hettinger66d2be82011-07-28 09:55:13 -07001511None is a non-NULL undefined value.
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001512There is (and should be!) no way to create other objects of this type,
Guido van Rossum3f5da241990-12-20 15:06:42 +00001513so there is exactly one (which is indestructible, by the way).
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001514*/
1515
Guido van Rossum0c182a11992-03-27 17:26:13 +00001516/* ARGSUSED */
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001517static PyObject *
Fred Drake100814d2000-07-09 15:48:49 +00001518none_repr(PyObject *op)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001519{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001520 return PyUnicode_FromString("None");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001521}
1522
Barry Warsaw9bf16442001-01-23 16:24:35 +00001523/* ARGUSED */
Victor Stinner2a4903f2020-01-30 13:09:11 +01001524static void _Py_NO_RETURN
Tim Peters803526b2002-07-07 05:13:56 +00001525none_dealloc(PyObject* ignore)
Barry Warsaw9bf16442001-01-23 16:24:35 +00001526{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001527 /* This should never get called, but we also don't want to SEGV if
1528 * we accidentally decref None out of existence.
1529 */
1530 Py_FatalError("deallocating None");
Barry Warsaw9bf16442001-01-23 16:24:35 +00001531}
1532
Benjamin Petersonc4607ae2011-07-29 18:19:43 -05001533static PyObject *
1534none_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
1535{
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +02001536 if (PyTuple_GET_SIZE(args) || (kwargs && PyDict_GET_SIZE(kwargs))) {
Benjamin Petersonc4607ae2011-07-29 18:19:43 -05001537 PyErr_SetString(PyExc_TypeError, "NoneType takes no arguments");
1538 return NULL;
1539 }
1540 Py_RETURN_NONE;
1541}
1542
Raymond Hettinger66d2be82011-07-28 09:55:13 -07001543static int
1544none_bool(PyObject *v)
1545{
1546 return 0;
1547}
1548
1549static PyNumberMethods none_as_number = {
1550 0, /* nb_add */
1551 0, /* nb_subtract */
1552 0, /* nb_multiply */
1553 0, /* nb_remainder */
1554 0, /* nb_divmod */
1555 0, /* nb_power */
1556 0, /* nb_negative */
1557 0, /* nb_positive */
1558 0, /* nb_absolute */
1559 (inquiry)none_bool, /* nb_bool */
1560 0, /* nb_invert */
1561 0, /* nb_lshift */
1562 0, /* nb_rshift */
1563 0, /* nb_and */
1564 0, /* nb_xor */
1565 0, /* nb_or */
1566 0, /* nb_int */
1567 0, /* nb_reserved */
1568 0, /* nb_float */
1569 0, /* nb_inplace_add */
1570 0, /* nb_inplace_subtract */
1571 0, /* nb_inplace_multiply */
1572 0, /* nb_inplace_remainder */
1573 0, /* nb_inplace_power */
1574 0, /* nb_inplace_lshift */
1575 0, /* nb_inplace_rshift */
1576 0, /* nb_inplace_and */
1577 0, /* nb_inplace_xor */
1578 0, /* nb_inplace_or */
1579 0, /* nb_floor_divide */
1580 0, /* nb_true_divide */
1581 0, /* nb_inplace_floor_divide */
1582 0, /* nb_inplace_true_divide */
1583 0, /* nb_index */
1584};
Barry Warsaw9bf16442001-01-23 16:24:35 +00001585
Alexandre Vassalotti65846c62013-11-30 17:55:48 -08001586PyTypeObject _PyNone_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001587 PyVarObject_HEAD_INIT(&PyType_Type, 0)
1588 "NoneType",
1589 0,
1590 0,
1591 none_dealloc, /*tp_dealloc*/ /*never called*/
Jeroen Demeyer530f5062019-05-31 04:13:39 +02001592 0, /*tp_vectorcall_offset*/
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001593 0, /*tp_getattr*/
1594 0, /*tp_setattr*/
Jeroen Demeyer530f5062019-05-31 04:13:39 +02001595 0, /*tp_as_async*/
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001596 none_repr, /*tp_repr*/
Raymond Hettinger66d2be82011-07-28 09:55:13 -07001597 &none_as_number, /*tp_as_number*/
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001598 0, /*tp_as_sequence*/
1599 0, /*tp_as_mapping*/
1600 0, /*tp_hash */
Benjamin Petersonc4607ae2011-07-29 18:19:43 -05001601 0, /*tp_call */
1602 0, /*tp_str */
1603 0, /*tp_getattro */
1604 0, /*tp_setattro */
1605 0, /*tp_as_buffer */
1606 Py_TPFLAGS_DEFAULT, /*tp_flags */
1607 0, /*tp_doc */
1608 0, /*tp_traverse */
1609 0, /*tp_clear */
1610 0, /*tp_richcompare */
1611 0, /*tp_weaklistoffset */
1612 0, /*tp_iter */
1613 0, /*tp_iternext */
1614 0, /*tp_methods */
1615 0, /*tp_members */
1616 0, /*tp_getset */
1617 0, /*tp_base */
1618 0, /*tp_dict */
1619 0, /*tp_descr_get */
1620 0, /*tp_descr_set */
1621 0, /*tp_dictoffset */
1622 0, /*tp_init */
1623 0, /*tp_alloc */
1624 none_new, /*tp_new */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001625};
1626
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001627PyObject _Py_NoneStruct = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001628 _PyObject_EXTRA_INIT
Alexandre Vassalotti65846c62013-11-30 17:55:48 -08001629 1, &_PyNone_Type
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001630};
1631
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +00001632/* NotImplemented is an object that can be used to signal that an
1633 operation is not implemented for the given type combination. */
1634
1635static PyObject *
1636NotImplemented_repr(PyObject *op)
1637{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001638 return PyUnicode_FromString("NotImplemented");
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +00001639}
1640
Benjamin Peterson18d7d7a2011-07-29 18:27:44 -05001641static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05301642NotImplemented_reduce(PyObject *op, PyObject *Py_UNUSED(ignored))
Alexandre Vassalottic49477b2013-11-24 02:53:45 -08001643{
1644 return PyUnicode_FromString("NotImplemented");
1645}
1646
1647static PyMethodDef notimplemented_methods[] = {
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05301648 {"__reduce__", NotImplemented_reduce, METH_NOARGS, NULL},
Alexandre Vassalottic49477b2013-11-24 02:53:45 -08001649 {NULL, NULL}
1650};
1651
1652static PyObject *
Benjamin Peterson18d7d7a2011-07-29 18:27:44 -05001653notimplemented_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
1654{
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +02001655 if (PyTuple_GET_SIZE(args) || (kwargs && PyDict_GET_SIZE(kwargs))) {
Benjamin Peterson18d7d7a2011-07-29 18:27:44 -05001656 PyErr_SetString(PyExc_TypeError, "NotImplementedType takes no arguments");
1657 return NULL;
1658 }
Brian Curtindfc80e32011-08-10 20:28:54 -05001659 Py_RETURN_NOTIMPLEMENTED;
Benjamin Peterson18d7d7a2011-07-29 18:27:44 -05001660}
1661
Victor Stinner2a4903f2020-01-30 13:09:11 +01001662static void _Py_NO_RETURN
Armin Ronacher226b1db2012-10-06 14:28:58 +02001663notimplemented_dealloc(PyObject* ignore)
1664{
1665 /* This should never get called, but we also don't want to SEGV if
1666 * we accidentally decref NotImplemented out of existence.
1667 */
1668 Py_FatalError("deallocating NotImplemented");
1669}
1670
MojoVampire469325c2020-03-03 18:50:17 +00001671static int
1672notimplemented_bool(PyObject *v)
1673{
1674 if (PyErr_WarnEx(PyExc_DeprecationWarning,
1675 "NotImplemented should not be used in a boolean context",
1676 1) < 0)
1677 {
1678 return -1;
1679 }
1680 return 1;
1681}
1682
1683static PyNumberMethods notimplemented_as_number = {
1684 .nb_bool = notimplemented_bool,
1685};
1686
Alexandre Vassalotti65846c62013-11-30 17:55:48 -08001687PyTypeObject _PyNotImplemented_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001688 PyVarObject_HEAD_INIT(&PyType_Type, 0)
1689 "NotImplementedType",
1690 0,
1691 0,
Armin Ronacher226b1db2012-10-06 14:28:58 +02001692 notimplemented_dealloc, /*tp_dealloc*/ /*never called*/
Jeroen Demeyer530f5062019-05-31 04:13:39 +02001693 0, /*tp_vectorcall_offset*/
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001694 0, /*tp_getattr*/
1695 0, /*tp_setattr*/
Jeroen Demeyer530f5062019-05-31 04:13:39 +02001696 0, /*tp_as_async*/
MojoVampire469325c2020-03-03 18:50:17 +00001697 NotImplemented_repr, /*tp_repr*/
1698 &notimplemented_as_number, /*tp_as_number*/
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001699 0, /*tp_as_sequence*/
1700 0, /*tp_as_mapping*/
1701 0, /*tp_hash */
Benjamin Peterson18d7d7a2011-07-29 18:27:44 -05001702 0, /*tp_call */
1703 0, /*tp_str */
1704 0, /*tp_getattro */
1705 0, /*tp_setattro */
1706 0, /*tp_as_buffer */
1707 Py_TPFLAGS_DEFAULT, /*tp_flags */
1708 0, /*tp_doc */
1709 0, /*tp_traverse */
1710 0, /*tp_clear */
1711 0, /*tp_richcompare */
1712 0, /*tp_weaklistoffset */
1713 0, /*tp_iter */
1714 0, /*tp_iternext */
Alexandre Vassalottic49477b2013-11-24 02:53:45 -08001715 notimplemented_methods, /*tp_methods */
Benjamin Peterson18d7d7a2011-07-29 18:27:44 -05001716 0, /*tp_members */
1717 0, /*tp_getset */
1718 0, /*tp_base */
1719 0, /*tp_dict */
1720 0, /*tp_descr_get */
1721 0, /*tp_descr_set */
1722 0, /*tp_dictoffset */
1723 0, /*tp_init */
1724 0, /*tp_alloc */
1725 notimplemented_new, /*tp_new */
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +00001726};
1727
1728PyObject _Py_NotImplementedStruct = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001729 _PyObject_EXTRA_INIT
Alexandre Vassalotti65846c62013-11-30 17:55:48 -08001730 1, &_PyNotImplemented_Type
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +00001731};
1732
Victor Stinner331a6a52019-05-27 16:39:22 +02001733PyStatus
Victor Stinnerab672812019-01-23 15:04:40 +01001734_PyTypes_Init(void)
Guido van Rossumba21a492001-08-16 08:17:26 +00001735{
Victor Stinner7a1f6c22020-01-30 09:02:14 +01001736 PyStatus status = _PyTypes_InitSlotDefs();
1737 if (_PyStatus_EXCEPTION(status)) {
1738 return status;
1739 }
1740
Victor Stinner6d43f6f2019-01-22 21:18:05 +01001741#define INIT_TYPE(TYPE, NAME) \
1742 do { \
1743 if (PyType_Ready(TYPE) < 0) { \
Victor Stinner331a6a52019-05-27 16:39:22 +02001744 return _PyStatus_ERR("Can't initialize " NAME " type"); \
Victor Stinner6d43f6f2019-01-22 21:18:05 +01001745 } \
1746 } while (0)
Victor Stinner5a1bb4e2014-06-02 14:10:59 +02001747
Victor Stinner6d43f6f2019-01-22 21:18:05 +01001748 INIT_TYPE(&PyBaseObject_Type, "object");
1749 INIT_TYPE(&PyType_Type, "type");
1750 INIT_TYPE(&_PyWeakref_RefType, "weakref");
1751 INIT_TYPE(&_PyWeakref_CallableProxyType, "callable weakref proxy");
1752 INIT_TYPE(&_PyWeakref_ProxyType, "weakref proxy");
1753 INIT_TYPE(&PyLong_Type, "int");
1754 INIT_TYPE(&PyBool_Type, "bool");
1755 INIT_TYPE(&PyByteArray_Type, "bytearray");
1756 INIT_TYPE(&PyBytes_Type, "str");
1757 INIT_TYPE(&PyList_Type, "list");
1758 INIT_TYPE(&_PyNone_Type, "None");
1759 INIT_TYPE(&_PyNotImplemented_Type, "NotImplemented");
1760 INIT_TYPE(&PyTraceBack_Type, "traceback");
1761 INIT_TYPE(&PySuper_Type, "super");
1762 INIT_TYPE(&PyRange_Type, "range");
1763 INIT_TYPE(&PyDict_Type, "dict");
1764 INIT_TYPE(&PyDictKeys_Type, "dict keys");
1765 INIT_TYPE(&PyDictValues_Type, "dict values");
1766 INIT_TYPE(&PyDictItems_Type, "dict items");
1767 INIT_TYPE(&PyDictRevIterKey_Type, "reversed dict keys");
1768 INIT_TYPE(&PyDictRevIterValue_Type, "reversed dict values");
1769 INIT_TYPE(&PyDictRevIterItem_Type, "reversed dict items");
1770 INIT_TYPE(&PyODict_Type, "OrderedDict");
1771 INIT_TYPE(&PyODictKeys_Type, "odict_keys");
1772 INIT_TYPE(&PyODictItems_Type, "odict_items");
1773 INIT_TYPE(&PyODictValues_Type, "odict_values");
1774 INIT_TYPE(&PyODictIter_Type, "odict_keyiterator");
1775 INIT_TYPE(&PySet_Type, "set");
1776 INIT_TYPE(&PyUnicode_Type, "str");
1777 INIT_TYPE(&PySlice_Type, "slice");
1778 INIT_TYPE(&PyStaticMethod_Type, "static method");
1779 INIT_TYPE(&PyComplex_Type, "complex");
1780 INIT_TYPE(&PyFloat_Type, "float");
1781 INIT_TYPE(&PyFrozenSet_Type, "frozenset");
1782 INIT_TYPE(&PyProperty_Type, "property");
1783 INIT_TYPE(&_PyManagedBuffer_Type, "managed buffer");
1784 INIT_TYPE(&PyMemoryView_Type, "memoryview");
1785 INIT_TYPE(&PyTuple_Type, "tuple");
1786 INIT_TYPE(&PyEnum_Type, "enumerate");
1787 INIT_TYPE(&PyReversed_Type, "reversed");
1788 INIT_TYPE(&PyStdPrinter_Type, "StdPrinter");
1789 INIT_TYPE(&PyCode_Type, "code");
1790 INIT_TYPE(&PyFrame_Type, "frame");
1791 INIT_TYPE(&PyCFunction_Type, "builtin function");
Petr Viktorine1becf42020-05-07 15:39:59 +02001792 INIT_TYPE(&PyCMethod_Type, "builtin method");
Victor Stinner6d43f6f2019-01-22 21:18:05 +01001793 INIT_TYPE(&PyMethod_Type, "method");
1794 INIT_TYPE(&PyFunction_Type, "function");
1795 INIT_TYPE(&PyDictProxy_Type, "dict proxy");
1796 INIT_TYPE(&PyGen_Type, "generator");
1797 INIT_TYPE(&PyGetSetDescr_Type, "get-set descriptor");
1798 INIT_TYPE(&PyWrapperDescr_Type, "wrapper");
1799 INIT_TYPE(&_PyMethodWrapper_Type, "method wrapper");
1800 INIT_TYPE(&PyEllipsis_Type, "ellipsis");
1801 INIT_TYPE(&PyMemberDescr_Type, "member descriptor");
1802 INIT_TYPE(&_PyNamespace_Type, "namespace");
1803 INIT_TYPE(&PyCapsule_Type, "capsule");
1804 INIT_TYPE(&PyLongRangeIter_Type, "long range iterator");
1805 INIT_TYPE(&PyCell_Type, "cell");
1806 INIT_TYPE(&PyInstanceMethod_Type, "instance method");
1807 INIT_TYPE(&PyClassMethodDescr_Type, "class method descr");
1808 INIT_TYPE(&PyMethodDescr_Type, "method descr");
1809 INIT_TYPE(&PyCallIter_Type, "call iter");
1810 INIT_TYPE(&PySeqIter_Type, "sequence iterator");
Antoine Pitrou91f43802019-05-26 17:10:09 +02001811 INIT_TYPE(&PyPickleBuffer_Type, "pickle.PickleBuffer");
Victor Stinner6d43f6f2019-01-22 21:18:05 +01001812 INIT_TYPE(&PyCoro_Type, "coroutine");
1813 INIT_TYPE(&_PyCoroWrapper_Type, "coroutine wrapper");
Eric Snowc11183c2019-03-15 16:35:46 -06001814 INIT_TYPE(&_PyInterpreterID_Type, "interpreter ID");
Victor Stinner331a6a52019-05-27 16:39:22 +02001815 return _PyStatus_OK();
Guido van Rossumba21a492001-08-16 08:17:26 +00001816
Victor Stinner6d43f6f2019-01-22 21:18:05 +01001817#undef INIT_TYPE
Guido van Rossumba21a492001-08-16 08:17:26 +00001818}
1819
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001820
Victor Stinner40e547d2020-02-05 01:11:10 +01001821void
1822_Py_NewReference(PyObject *op)
1823{
1824 if (_Py_tracemalloc_config.tracing) {
1825 _PyTraceMalloc_NewReference(op);
1826 }
1827#ifdef Py_REF_DEBUG
1828 _Py_RefTotal++;
1829#endif
Victor Stinnerc86a1122020-02-07 01:24:29 +01001830 Py_SET_REFCNT(op, 1);
Victor Stinner40e547d2020-02-05 01:11:10 +01001831#ifdef Py_TRACE_REFS
1832 _Py_AddToAllObjects(op, 1);
1833#endif
1834}
1835
1836
Guido van Rossum84a90321996-05-22 16:34:47 +00001837#ifdef Py_TRACE_REFS
Guido van Rossumaacdc9d1996-08-12 21:32:12 +00001838void
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02001839_Py_ForgetReference(PyObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001840{
Victor Stinnera93c51e2020-02-07 00:38:59 +01001841 if (Py_REFCNT(op) < 0) {
Victor Stinner5eb8bff2020-01-30 09:01:07 +01001842 _PyObject_ASSERT_FAILED_MSG(op, "negative refcnt");
1843 }
1844
1845 if (op == &refchain ||
1846 op->_ob_prev->_ob_next != op || op->_ob_next->_ob_prev != op)
1847 {
1848 _PyObject_ASSERT_FAILED_MSG(op, "invalid object chain");
1849 }
1850
Guido van Rossumbffd6832000-01-20 22:32:56 +00001851#ifdef SLOW_UNREF_CHECK
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02001852 PyObject *p;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001853 for (p = refchain._ob_next; p != &refchain; p = p->_ob_next) {
Victor Stinner5eb8bff2020-01-30 09:01:07 +01001854 if (p == op) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001855 break;
Victor Stinner5eb8bff2020-01-30 09:01:07 +01001856 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001857 }
Victor Stinner5eb8bff2020-01-30 09:01:07 +01001858 if (p == &refchain) {
1859 /* Not found */
1860 _PyObject_ASSERT_FAILED_MSG(op,
1861 "object not found in the objects list");
1862 }
Guido van Rossum2e8f6141992-09-03 20:32:55 +00001863#endif
Victor Stinner5eb8bff2020-01-30 09:01:07 +01001864
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001865 op->_ob_next->_ob_prev = op->_ob_prev;
1866 op->_ob_prev->_ob_next = op->_ob_next;
1867 op->_ob_next = op->_ob_prev = NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001868}
1869
Tim Peters269b2a62003-04-17 19:52:29 +00001870/* Print all live objects. Because PyObject_Print is called, the
1871 * interpreter must be in a healthy state.
1872 */
Guido van Rossumaacdc9d1996-08-12 21:32:12 +00001873void
Fred Drake100814d2000-07-09 15:48:49 +00001874_Py_PrintReferences(FILE *fp)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001875{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001876 PyObject *op;
1877 fprintf(fp, "Remaining objects:\n");
1878 for (op = refchain._ob_next; op != &refchain; op = op->_ob_next) {
Victor Stinnera93c51e2020-02-07 00:38:59 +01001879 fprintf(fp, "%p [%" PY_FORMAT_SIZE_T "d] ", (void *)op, Py_REFCNT(op));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001880 if (PyObject_Print(op, fp, 0) != 0)
1881 PyErr_Clear();
1882 putc('\n', fp);
1883 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001884}
1885
Tim Peters269b2a62003-04-17 19:52:29 +00001886/* Print the addresses of all live objects. Unlike _Py_PrintReferences, this
1887 * doesn't make any calls to the Python C API, so is always safe to call.
1888 */
1889void
1890_Py_PrintReferenceAddresses(FILE *fp)
1891{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001892 PyObject *op;
1893 fprintf(fp, "Remaining object addresses:\n");
1894 for (op = refchain._ob_next; op != &refchain; op = op->_ob_next)
Zackery Spytz1a2252e2019-05-06 10:56:51 -06001895 fprintf(fp, "%p [%" PY_FORMAT_SIZE_T "d] %s\n", (void *)op,
Victor Stinnera93c51e2020-02-07 00:38:59 +01001896 Py_REFCNT(op), Py_TYPE(op)->tp_name);
Tim Peters269b2a62003-04-17 19:52:29 +00001897}
1898
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +00001899PyObject *
Fred Drake100814d2000-07-09 15:48:49 +00001900_Py_GetObjects(PyObject *self, PyObject *args)
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +00001901{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001902 int i, n;
1903 PyObject *t = NULL;
1904 PyObject *res, *op;
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +00001905
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001906 if (!PyArg_ParseTuple(args, "i|O", &n, &t))
1907 return NULL;
1908 op = refchain._ob_next;
1909 res = PyList_New(0);
1910 if (res == NULL)
1911 return NULL;
1912 for (i = 0; (n == 0 || i < n) && op != &refchain; i++) {
1913 while (op == self || op == args || op == res || op == t ||
Andy Lester55728702020-03-06 16:53:17 -06001914 (t != NULL && !Py_IS_TYPE(op, (PyTypeObject *) t))) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001915 op = op->_ob_next;
1916 if (op == &refchain)
1917 return res;
1918 }
1919 if (PyList_Append(res, op) < 0) {
1920 Py_DECREF(res);
1921 return NULL;
1922 }
1923 op = op->_ob_next;
1924 }
1925 return res;
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +00001926}
1927
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001928#endif
Guido van Rossum97ead3f1996-01-12 01:24:09 +00001929
Benjamin Petersonb173f782009-05-05 22:31:58 +00001930
Guido van Rossum84a90321996-05-22 16:34:47 +00001931/* Hack to force loading of abstract.o */
Martin v. Löwis18e16552006-02-15 17:27:45 +00001932Py_ssize_t (*_Py_abstract_hack)(PyObject *) = PyObject_Size;
Guido van Rossume09fb551997-08-05 02:04:34 +00001933
1934
David Malcolm49526f42012-06-22 14:55:41 -04001935void
1936_PyObject_DebugTypeStats(FILE *out)
1937{
David Malcolm49526f42012-06-22 14:55:41 -04001938 _PyDict_DebugMallocStats(out);
1939 _PyFloat_DebugMallocStats(out);
1940 _PyFrame_DebugMallocStats(out);
1941 _PyList_DebugMallocStats(out);
David Malcolm49526f42012-06-22 14:55:41 -04001942 _PyTuple_DebugMallocStats(out);
1943}
Guido van Rossumb18618d2000-05-03 23:44:39 +00001944
Guido van Rossum86610361998-04-10 22:32:46 +00001945/* These methods are used to control infinite recursion in repr, str, print,
1946 etc. Container objects that may recursively contain themselves,
Martin Panter8d56c022016-05-29 04:13:35 +00001947 e.g. builtin dictionaries and lists, should use Py_ReprEnter() and
Guido van Rossum86610361998-04-10 22:32:46 +00001948 Py_ReprLeave() to avoid infinite recursion.
1949
1950 Py_ReprEnter() returns 0 the first time it is called for a particular
1951 object and 1 every time thereafter. It returns -1 if an exception
1952 occurred. Py_ReprLeave() has no return value.
1953
1954 See dictobject.c and listobject.c for examples of use.
1955*/
1956
Guido van Rossum86610361998-04-10 22:32:46 +00001957int
Fred Drake100814d2000-07-09 15:48:49 +00001958Py_ReprEnter(PyObject *obj)
Guido van Rossum86610361998-04-10 22:32:46 +00001959{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001960 PyObject *dict;
1961 PyObject *list;
1962 Py_ssize_t i;
Guido van Rossum86610361998-04-10 22:32:46 +00001963
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001964 dict = PyThreadState_GetDict();
Antoine Pitrou04d17d32014-03-31 22:04:38 +02001965 /* Ignore a missing thread-state, so that this function can be called
1966 early on startup. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001967 if (dict == NULL)
1968 return 0;
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02001969 list = _PyDict_GetItemIdWithError(dict, &PyId_Py_Repr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001970 if (list == NULL) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02001971 if (PyErr_Occurred()) {
1972 return -1;
1973 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001974 list = PyList_New(0);
1975 if (list == NULL)
1976 return -1;
Victor Stinner7a07e452013-11-06 18:57:29 +01001977 if (_PyDict_SetItemId(dict, &PyId_Py_Repr, list) < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001978 return -1;
1979 Py_DECREF(list);
1980 }
1981 i = PyList_GET_SIZE(list);
1982 while (--i >= 0) {
1983 if (PyList_GET_ITEM(list, i) == obj)
1984 return 1;
1985 }
Victor Stinnere901d1f2013-07-17 21:58:41 +02001986 if (PyList_Append(list, obj) < 0)
1987 return -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001988 return 0;
Guido van Rossum86610361998-04-10 22:32:46 +00001989}
1990
1991void
Fred Drake100814d2000-07-09 15:48:49 +00001992Py_ReprLeave(PyObject *obj)
Guido van Rossum86610361998-04-10 22:32:46 +00001993{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001994 PyObject *dict;
1995 PyObject *list;
1996 Py_ssize_t i;
Victor Stinner1b634932013-07-16 22:24:44 +02001997 PyObject *error_type, *error_value, *error_traceback;
1998
1999 PyErr_Fetch(&error_type, &error_value, &error_traceback);
Guido van Rossum86610361998-04-10 22:32:46 +00002000
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002001 dict = PyThreadState_GetDict();
2002 if (dict == NULL)
Victor Stinner1b634932013-07-16 22:24:44 +02002003 goto finally;
2004
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002005 list = _PyDict_GetItemIdWithError(dict, &PyId_Py_Repr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002006 if (list == NULL || !PyList_Check(list))
Victor Stinner1b634932013-07-16 22:24:44 +02002007 goto finally;
2008
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002009 i = PyList_GET_SIZE(list);
2010 /* Count backwards because we always expect obj to be list[-1] */
2011 while (--i >= 0) {
2012 if (PyList_GET_ITEM(list, i) == obj) {
2013 PyList_SetSlice(list, i, i + 1, NULL);
2014 break;
2015 }
2016 }
Victor Stinner1b634932013-07-16 22:24:44 +02002017
2018finally:
2019 /* ignore exceptions because there is no way to report them. */
2020 PyErr_Restore(error_type, error_value, error_traceback);
Guido van Rossum86610361998-04-10 22:32:46 +00002021}
Guido van Rossumd724b232000-03-13 16:01:29 +00002022
Tim Peters803526b2002-07-07 05:13:56 +00002023/* Trashcan support. */
Guido van Rossumd724b232000-03-13 16:01:29 +00002024
Tim Peters803526b2002-07-07 05:13:56 +00002025/* Add op to the _PyTrash_delete_later list. Called when the current
2026 * call-stack depth gets large. op must be a currently untracked gc'ed
2027 * object, with refcount 0. Py_DECREF must already have been called on it.
2028 */
Guido van Rossumd724b232000-03-13 16:01:29 +00002029void
Fred Drake100814d2000-07-09 15:48:49 +00002030_PyTrash_deposit_object(PyObject *op)
Guido van Rossumd724b232000-03-13 16:01:29 +00002031{
Victor Stinner72474072019-11-20 12:25:50 +01002032 PyThreadState *tstate = _PyThreadState_GET();
2033 struct _gc_runtime_state *gcstate = &tstate->interp->gc;
2034
Hai Shi675d9a32020-04-15 02:11:20 +08002035 _PyObject_ASSERT(op, _PyObject_IS_GC(op));
Victor Stinner24702042018-10-26 17:16:37 +02002036 _PyObject_ASSERT(op, !_PyObject_GC_IS_TRACKED(op));
Victor Stinnera93c51e2020-02-07 00:38:59 +01002037 _PyObject_ASSERT(op, Py_REFCNT(op) == 0);
Victor Stinner72474072019-11-20 12:25:50 +01002038 _PyGCHead_SET_PREV(_Py_AS_GC(op), gcstate->trash_delete_later);
2039 gcstate->trash_delete_later = op;
Guido van Rossumd724b232000-03-13 16:01:29 +00002040}
2041
Antoine Pitrou2b0218a2012-09-06 00:59:49 +02002042/* The equivalent API, using per-thread state recursion info */
2043void
2044_PyTrash_thread_deposit_object(PyObject *op)
2045{
Victor Stinner50b48572018-11-01 01:51:40 +01002046 PyThreadState *tstate = _PyThreadState_GET();
Hai Shi675d9a32020-04-15 02:11:20 +08002047 _PyObject_ASSERT(op, _PyObject_IS_GC(op));
Victor Stinner24702042018-10-26 17:16:37 +02002048 _PyObject_ASSERT(op, !_PyObject_GC_IS_TRACKED(op));
Victor Stinnera93c51e2020-02-07 00:38:59 +01002049 _PyObject_ASSERT(op, Py_REFCNT(op) == 0);
INADA Naoki5ac9e6e2018-07-10 17:19:53 +09002050 _PyGCHead_SET_PREV(_Py_AS_GC(op), tstate->trash_delete_later);
Antoine Pitrou2b0218a2012-09-06 00:59:49 +02002051 tstate->trash_delete_later = op;
2052}
2053
Min ho Kimc4cacc82019-07-31 08:16:13 +10002054/* Deallocate all the objects in the _PyTrash_delete_later list. Called when
Tim Peters803526b2002-07-07 05:13:56 +00002055 * the call-stack unwinds again.
2056 */
Guido van Rossumd724b232000-03-13 16:01:29 +00002057void
Fred Drake100814d2000-07-09 15:48:49 +00002058_PyTrash_destroy_chain(void)
Guido van Rossumd724b232000-03-13 16:01:29 +00002059{
Victor Stinner72474072019-11-20 12:25:50 +01002060 PyThreadState *tstate = _PyThreadState_GET();
2061 struct _gc_runtime_state *gcstate = &tstate->interp->gc;
2062
2063 while (gcstate->trash_delete_later) {
2064 PyObject *op = gcstate->trash_delete_later;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002065 destructor dealloc = Py_TYPE(op)->tp_dealloc;
Neil Schemenauerf589c052002-03-29 03:05:54 +00002066
Victor Stinner72474072019-11-20 12:25:50 +01002067 gcstate->trash_delete_later =
INADA Naoki5ac9e6e2018-07-10 17:19:53 +09002068 (PyObject*) _PyGCHead_PREV(_Py_AS_GC(op));
Neil Schemenauerf589c052002-03-29 03:05:54 +00002069
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002070 /* Call the deallocator directly. This used to try to
2071 * fool Py_DECREF into calling it indirectly, but
2072 * Py_DECREF was already called on this object, and in
2073 * assorted non-release builds calling Py_DECREF again ends
2074 * up distorting allocation statistics.
2075 */
Victor Stinnera93c51e2020-02-07 00:38:59 +01002076 _PyObject_ASSERT(op, Py_REFCNT(op) == 0);
Victor Stinner72474072019-11-20 12:25:50 +01002077 ++gcstate->trash_delete_nesting;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002078 (*dealloc)(op);
Victor Stinner72474072019-11-20 12:25:50 +01002079 --gcstate->trash_delete_nesting;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002080 }
Guido van Rossumd724b232000-03-13 16:01:29 +00002081}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002082
Antoine Pitrou2b0218a2012-09-06 00:59:49 +02002083/* The equivalent API, using per-thread state recursion info */
2084void
2085_PyTrash_thread_destroy_chain(void)
2086{
Victor Stinner50b48572018-11-01 01:51:40 +01002087 PyThreadState *tstate = _PyThreadState_GET();
Xiang Zhanga66f9c62017-05-13 13:36:14 +08002088 /* We need to increase trash_delete_nesting here, otherwise,
2089 _PyTrash_thread_destroy_chain will be called recursively
2090 and then possibly crash. An example that may crash without
2091 increase:
2092 N = 500000 # need to be large enough
2093 ob = object()
2094 tups = [(ob,) for i in range(N)]
2095 for i in range(49):
2096 tups = [(tup,) for tup in tups]
2097 del tups
2098 */
2099 assert(tstate->trash_delete_nesting == 0);
2100 ++tstate->trash_delete_nesting;
Antoine Pitrou2b0218a2012-09-06 00:59:49 +02002101 while (tstate->trash_delete_later) {
2102 PyObject *op = tstate->trash_delete_later;
2103 destructor dealloc = Py_TYPE(op)->tp_dealloc;
2104
2105 tstate->trash_delete_later =
INADA Naoki5ac9e6e2018-07-10 17:19:53 +09002106 (PyObject*) _PyGCHead_PREV(_Py_AS_GC(op));
Antoine Pitrou2b0218a2012-09-06 00:59:49 +02002107
2108 /* Call the deallocator directly. This used to try to
2109 * fool Py_DECREF into calling it indirectly, but
2110 * Py_DECREF was already called on this object, and in
2111 * assorted non-release builds calling Py_DECREF again ends
2112 * up distorting allocation statistics.
2113 */
Victor Stinnera93c51e2020-02-07 00:38:59 +01002114 _PyObject_ASSERT(op, Py_REFCNT(op) == 0);
Antoine Pitrou2b0218a2012-09-06 00:59:49 +02002115 (*dealloc)(op);
Xiang Zhanga66f9c62017-05-13 13:36:14 +08002116 assert(tstate->trash_delete_nesting == 1);
Antoine Pitrou2b0218a2012-09-06 00:59:49 +02002117 }
Xiang Zhanga66f9c62017-05-13 13:36:14 +08002118 --tstate->trash_delete_nesting;
Antoine Pitrou2b0218a2012-09-06 00:59:49 +02002119}
2120
Victor Stinner626bff82018-10-25 17:31:10 +02002121
Victor Stinner38965ec2020-03-13 16:51:52 +01002122int
2123_PyTrash_begin(PyThreadState *tstate, PyObject *op)
2124{
2125 if (tstate->trash_delete_nesting >= PyTrash_UNWIND_LEVEL) {
2126 /* Store the object (to be deallocated later) and jump past
2127 * Py_TRASHCAN_END, skipping the body of the deallocator */
2128 _PyTrash_thread_deposit_object(op);
2129 return 1;
2130 }
2131 ++tstate->trash_delete_nesting;
2132 return 0;
2133}
2134
2135
2136void
2137_PyTrash_end(PyThreadState *tstate)
2138{
2139 --tstate->trash_delete_nesting;
2140 if (tstate->trash_delete_later && tstate->trash_delete_nesting <= 0) {
2141 _PyTrash_thread_destroy_chain();
2142 }
2143}
2144
2145
Victor Stinner2a4903f2020-01-30 13:09:11 +01002146void _Py_NO_RETURN
Victor Stinnerf1d002c2018-11-21 23:53:44 +01002147_PyObject_AssertFailed(PyObject *obj, const char *expr, const char *msg,
Victor Stinner626bff82018-10-25 17:31:10 +02002148 const char *file, int line, const char *function)
2149{
Victor Stinnerf1d002c2018-11-21 23:53:44 +01002150 fprintf(stderr, "%s:%d: ", file, line);
2151 if (function) {
2152 fprintf(stderr, "%s: ", function);
2153 }
Victor Stinner626bff82018-10-25 17:31:10 +02002154 fflush(stderr);
Victor Stinner68762572019-10-07 18:42:01 +02002155
Victor Stinnerf1d002c2018-11-21 23:53:44 +01002156 if (expr) {
2157 fprintf(stderr, "Assertion \"%s\" failed", expr);
Victor Stinner626bff82018-10-25 17:31:10 +02002158 }
2159 else {
Victor Stinnerf1d002c2018-11-21 23:53:44 +01002160 fprintf(stderr, "Assertion failed");
Victor Stinner626bff82018-10-25 17:31:10 +02002161 }
2162 fflush(stderr);
Victor Stinner68762572019-10-07 18:42:01 +02002163
Victor Stinnerf1d002c2018-11-21 23:53:44 +01002164 if (msg) {
2165 fprintf(stderr, ": %s", msg);
2166 }
2167 fprintf(stderr, "\n");
2168 fflush(stderr);
Victor Stinner626bff82018-10-25 17:31:10 +02002169
Victor Stinner68762572019-10-07 18:42:01 +02002170 if (_PyObject_IsFreed(obj)) {
Victor Stinner626bff82018-10-25 17:31:10 +02002171 /* It seems like the object memory has been freed:
2172 don't access it to prevent a segmentation fault. */
Victor Stinnerb39afb72019-09-17 23:36:28 +02002173 fprintf(stderr, "<object at %p is freed>\n", obj);
Victor Stinner68762572019-10-07 18:42:01 +02002174 fflush(stderr);
Victor Stinner626bff82018-10-25 17:31:10 +02002175 }
2176 else {
penguindustin96466302019-05-06 14:57:17 -04002177 /* Display the traceback where the object has been allocated.
Victor Stinner626bff82018-10-25 17:31:10 +02002178 Do it before dumping repr(obj), since repr() is more likely
2179 to crash than dumping the traceback. */
2180 void *ptr;
2181 PyTypeObject *type = Py_TYPE(obj);
Victor Stinner45ec5b92020-04-08 01:42:27 +02002182 if (_PyType_IS_GC(type)) {
Victor Stinner626bff82018-10-25 17:31:10 +02002183 ptr = (void *)((char *)obj - sizeof(PyGC_Head));
2184 }
2185 else {
2186 ptr = (void *)obj;
2187 }
2188 _PyMem_DumpTraceback(fileno(stderr), ptr);
2189
2190 /* This might succeed or fail, but we're about to abort, so at least
2191 try to provide any extra info we can: */
2192 _PyObject_Dump(obj);
Victor Stinner77753492019-10-07 23:44:05 +02002193
2194 fprintf(stderr, "\n");
2195 fflush(stderr);
Victor Stinner626bff82018-10-25 17:31:10 +02002196 }
Victor Stinner626bff82018-10-25 17:31:10 +02002197
2198 Py_FatalError("_PyObject_AssertFailed");
2199}
2200
Victor Stinner3c09dca2018-10-30 14:48:26 +01002201
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00002202void
2203_Py_Dealloc(PyObject *op)
2204{
Victor Stinner3c09dca2018-10-30 14:48:26 +01002205 destructor dealloc = Py_TYPE(op)->tp_dealloc;
2206#ifdef Py_TRACE_REFS
2207 _Py_ForgetReference(op);
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00002208#endif
Victor Stinner3c09dca2018-10-30 14:48:26 +01002209 (*dealloc)(op);
2210}
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00002211
Victor Stinner38aefc52020-04-06 14:07:02 +02002212
2213PyObject **
2214PyObject_GET_WEAKREFS_LISTPTR(PyObject *op)
2215{
2216 return _PyObject_GET_WEAKREFS_LISTPTR(op);
2217}
2218
2219
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002220#ifdef __cplusplus
2221}
2222#endif