blob: fe3734404f5cf9773b5d12bbeb33e21bea16dbae [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,
Victor Stinnerd36cf5f2020-06-10 18:38:05 +020069 "[%zd refs, %zd blocks]\n",
Eric Snowdae02762017-09-14 00:35:58 -070070 _Py_GetRefTotal(), _Py_GetAllocatedBlocks());
Nick Coghland6009512014-11-20 21:39:37 +100071}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000072#endif /* Py_REF_DEBUG */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000073
Guido van Rossum3f5da241990-12-20 15:06:42 +000074/* Object allocation routines used by NEWOBJ and NEWVAROBJ macros.
75 These are used by the individual routines for object creation.
76 Do not call them otherwise, they do not initialize the object! */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000077
Tim Peters78be7992003-03-23 02:51:01 +000078#ifdef Py_TRACE_REFS
Tim Peters7571a0f2003-03-23 17:52:28 +000079/* Head of circular doubly-linked list of all objects. These are linked
80 * together via the _ob_prev and _ob_next members of a PyObject, which
81 * exist only in a Py_TRACE_REFS build.
82 */
Tim Peters78be7992003-03-23 02:51:01 +000083static PyObject refchain = {&refchain, &refchain};
Tim Peters36eb4df2003-03-23 03:33:13 +000084
Tim Peters7571a0f2003-03-23 17:52:28 +000085/* Insert op at the front of the list of all objects. If force is true,
86 * op is added even if _ob_prev and _ob_next are non-NULL already. If
87 * force is false amd _ob_prev or _ob_next are non-NULL, do nothing.
88 * force should be true if and only if op points to freshly allocated,
89 * uninitialized memory, or you've unlinked op from the list and are
Tim Peters51f8d382003-03-23 18:06:08 +000090 * relinking it into the front.
Tim Peters7571a0f2003-03-23 17:52:28 +000091 * Note that objects are normally added to the list via _Py_NewReference,
92 * which is called by PyObject_Init. Not all objects are initialized that
93 * way, though; exceptions include statically allocated type objects, and
94 * statically allocated singletons (like Py_True and Py_None).
95 */
Victor Stinner58f4e1a2020-02-05 18:24:33 +010096void
Tim Peters7571a0f2003-03-23 17:52:28 +000097_Py_AddToAllObjects(PyObject *op, int force)
Tim Peters36eb4df2003-03-23 03:33:13 +000098{
Tim Peters7571a0f2003-03-23 17:52:28 +000099#ifdef Py_DEBUG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000100 if (!force) {
101 /* If it's initialized memory, op must be in or out of
102 * the list unambiguously.
103 */
Victor Stinner24702042018-10-26 17:16:37 +0200104 _PyObject_ASSERT(op, (op->_ob_prev == NULL) == (op->_ob_next == NULL));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000105 }
Tim Peters78be7992003-03-23 02:51:01 +0000106#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000107 if (force || op->_ob_prev == NULL) {
108 op->_ob_next = refchain._ob_next;
109 op->_ob_prev = &refchain;
110 refchain._ob_next->_ob_prev = op;
111 refchain._ob_next = op;
112 }
Tim Peters7571a0f2003-03-23 17:52:28 +0000113}
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000114#endif /* Py_TRACE_REFS */
Tim Peters78be7992003-03-23 02:51:01 +0000115
Tim Peters7c321a82002-07-09 02:57:01 +0000116#ifdef Py_REF_DEBUG
117/* Log a fatal error; doesn't return. */
118void
Victor Stinner18618e652018-10-25 17:28:11 +0200119_Py_NegativeRefcount(const char *filename, int lineno, PyObject *op)
Tim Peters7c321a82002-07-09 02:57:01 +0000120{
Victor Stinnerf1d002c2018-11-21 23:53:44 +0100121 _PyObject_AssertFailed(op, NULL, "object has negative ref count",
Victor Stinner3ec9af72018-10-26 02:12:34 +0200122 filename, lineno, __func__);
Tim Peters7c321a82002-07-09 02:57:01 +0000123}
124
125#endif /* Py_REF_DEBUG */
126
Thomas Heller1328b522004-04-22 17:23:49 +0000127void
128Py_IncRef(PyObject *o)
129{
130 Py_XINCREF(o);
131}
132
133void
134Py_DecRef(PyObject *o)
135{
136 Py_XDECREF(o);
137}
138
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000139PyObject *
Fred Drake100814d2000-07-09 15:48:49 +0000140PyObject_Init(PyObject *op, PyTypeObject *tp)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000141{
Victor Stinnerf58bd7c2020-02-05 13:12:19 +0100142 if (op == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000143 return PyErr_NoMemory();
Victor Stinnerf58bd7c2020-02-05 13:12:19 +0100144 }
145
Victor Stinner04fc4f22020-06-16 01:28:07 +0200146 _PyObject_Init(op, tp);
147 return op;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000148}
149
Guido van Rossumb18618d2000-05-03 23:44:39 +0000150PyVarObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000151PyObject_InitVar(PyVarObject *op, PyTypeObject *tp, Py_ssize_t size)
Guido van Rossumb18618d2000-05-03 23:44:39 +0000152{
Victor Stinnerf58bd7c2020-02-05 13:12:19 +0100153 if (op == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000154 return (PyVarObject *) PyErr_NoMemory();
Victor Stinnerf58bd7c2020-02-05 13:12:19 +0100155 }
156
Victor Stinner04fc4f22020-06-16 01:28:07 +0200157 _PyObject_InitVar(op, tp, size);
158 return op;
Guido van Rossumb18618d2000-05-03 23:44:39 +0000159}
160
161PyObject *
Fred Drake100814d2000-07-09 15:48:49 +0000162_PyObject_New(PyTypeObject *tp)
Guido van Rossumb18618d2000-05-03 23:44:39 +0000163{
Victor Stinner92055202020-04-08 00:38:15 +0200164 PyObject *op = (PyObject *) PyObject_MALLOC(_PyObject_SIZE(tp));
165 if (op == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000166 return PyErr_NoMemory();
Victor Stinner92055202020-04-08 00:38:15 +0200167 }
Victor Stinner04fc4f22020-06-16 01:28:07 +0200168 _PyObject_Init(op, tp);
Victor Stinner92055202020-04-08 00:38:15 +0200169 return op;
Guido van Rossumb18618d2000-05-03 23:44:39 +0000170}
171
Guido van Rossumd0c87ee1997-05-15 21:31:03 +0000172PyVarObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000173_PyObject_NewVar(PyTypeObject *tp, Py_ssize_t nitems)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000174{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000175 PyVarObject *op;
176 const size_t size = _PyObject_VAR_SIZE(tp, nitems);
177 op = (PyVarObject *) PyObject_MALLOC(size);
Victor Stinner04fc4f22020-06-16 01:28:07 +0200178 if (op == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000179 return (PyVarObject *)PyErr_NoMemory();
Victor Stinner04fc4f22020-06-16 01:28:07 +0200180 }
181 _PyObject_InitVar(op, tp, nitems);
182 return op;
Guido van Rossumb18618d2000-05-03 23:44:39 +0000183}
184
Antoine Pitrou796564c2013-07-30 19:59:21 +0200185void
186PyObject_CallFinalizer(PyObject *self)
187{
188 PyTypeObject *tp = Py_TYPE(self);
189
Antoine Pitrouada319b2019-05-29 22:12:38 +0200190 if (tp->tp_finalize == NULL)
Antoine Pitrou796564c2013-07-30 19:59:21 +0200191 return;
192 /* tp_finalize should only be called once. */
Victor Stinner45ec5b92020-04-08 01:42:27 +0200193 if (_PyType_IS_GC(tp) && _PyGC_FINALIZED(self))
Antoine Pitrou796564c2013-07-30 19:59:21 +0200194 return;
195
196 tp->tp_finalize(self);
Victor Stinner45ec5b92020-04-08 01:42:27 +0200197 if (_PyType_IS_GC(tp)) {
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900198 _PyGC_SET_FINALIZED(self);
199 }
Antoine Pitrou796564c2013-07-30 19:59:21 +0200200}
201
202int
203PyObject_CallFinalizerFromDealloc(PyObject *self)
204{
Victor Stinnera93c51e2020-02-07 00:38:59 +0100205 if (Py_REFCNT(self) != 0) {
Victor Stinner5eb8bff2020-01-30 09:01:07 +0100206 _PyObject_ASSERT_FAILED_MSG(self,
207 "PyObject_CallFinalizerFromDealloc called "
208 "on object with a non-zero refcount");
209 }
Antoine Pitrou796564c2013-07-30 19:59:21 +0200210
211 /* Temporarily resurrect the object. */
Victor Stinnerc86a1122020-02-07 01:24:29 +0100212 Py_SET_REFCNT(self, 1);
Antoine Pitrou796564c2013-07-30 19:59:21 +0200213
214 PyObject_CallFinalizer(self);
215
Victor Stinner24702042018-10-26 17:16:37 +0200216 _PyObject_ASSERT_WITH_MSG(self,
Victor Stinnera93c51e2020-02-07 00:38:59 +0100217 Py_REFCNT(self) > 0,
Victor Stinner24702042018-10-26 17:16:37 +0200218 "refcount is too small");
Victor Stinner5eb8bff2020-01-30 09:01:07 +0100219
220 /* Undo the temporary resurrection; can't use DECREF here, it would
221 * cause a recursive call. */
Victor Stinnerc86a1122020-02-07 01:24:29 +0100222 Py_SET_REFCNT(self, Py_REFCNT(self) - 1);
223 if (Py_REFCNT(self) == 0) {
Antoine Pitrou796564c2013-07-30 19:59:21 +0200224 return 0; /* this is the normal path out */
Victor Stinner5eb8bff2020-01-30 09:01:07 +0100225 }
Antoine Pitrou796564c2013-07-30 19:59:21 +0200226
227 /* tp_finalize resurrected it! Make it look like the original Py_DECREF
Victor Stinner5eb8bff2020-01-30 09:01:07 +0100228 * never happened. */
Victor Stinnera93c51e2020-02-07 00:38:59 +0100229 Py_ssize_t refcnt = Py_REFCNT(self);
Antoine Pitrou796564c2013-07-30 19:59:21 +0200230 _Py_NewReference(self);
Victor Stinnerc86a1122020-02-07 01:24:29 +0100231 Py_SET_REFCNT(self, refcnt);
Antoine Pitrou796564c2013-07-30 19:59:21 +0200232
Victor Stinner24702042018-10-26 17:16:37 +0200233 _PyObject_ASSERT(self,
Victor Stinner45ec5b92020-04-08 01:42:27 +0200234 (!_PyType_IS_GC(Py_TYPE(self))
Victor Stinner24702042018-10-26 17:16:37 +0200235 || _PyObject_GC_IS_TRACKED(self)));
Victor Stinner49932fe2020-02-03 17:55:05 +0100236 /* If Py_REF_DEBUG macro is defined, _Py_NewReference() increased
237 _Py_RefTotal, so we need to undo that. */
238#ifdef Py_REF_DEBUG
239 _Py_RefTotal--;
240#endif
Antoine Pitrou796564c2013-07-30 19:59:21 +0200241 return -1;
242}
243
Antoine Pitrouc47bd4a2010-07-27 22:08:27 +0000244int
245PyObject_Print(PyObject *op, FILE *fp, int flags)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000246{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000247 int ret = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000248 if (PyErr_CheckSignals())
249 return -1;
Guido van Rossum9b00dfa1998-04-28 16:06:54 +0000250#ifdef USE_STACKCHECK
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000251 if (PyOS_CheckStack()) {
252 PyErr_SetString(PyExc_MemoryError, "stack overflow");
253 return -1;
254 }
Guido van Rossum9b00dfa1998-04-28 16:06:54 +0000255#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000256 clearerr(fp); /* Clear any previous error condition */
257 if (op == NULL) {
258 Py_BEGIN_ALLOW_THREADS
259 fprintf(fp, "<nil>");
260 Py_END_ALLOW_THREADS
261 }
262 else {
Victor Stinnera93c51e2020-02-07 00:38:59 +0100263 if (Py_REFCNT(op) <= 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000264 /* XXX(twouters) cast refcount to long until %zd is
265 universally available */
266 Py_BEGIN_ALLOW_THREADS
267 fprintf(fp, "<refcnt %ld at %p>",
Victor Stinnera93c51e2020-02-07 00:38:59 +0100268 (long)Py_REFCNT(op), (void *)op);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000269 Py_END_ALLOW_THREADS
Victor Stinner3ec9af72018-10-26 02:12:34 +0200270 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000271 else {
272 PyObject *s;
273 if (flags & Py_PRINT_RAW)
274 s = PyObject_Str(op);
275 else
276 s = PyObject_Repr(op);
277 if (s == NULL)
278 ret = -1;
279 else if (PyBytes_Check(s)) {
280 fwrite(PyBytes_AS_STRING(s), 1,
281 PyBytes_GET_SIZE(s), fp);
282 }
283 else if (PyUnicode_Check(s)) {
284 PyObject *t;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200285 t = PyUnicode_AsEncodedString(s, "utf-8", "backslashreplace");
Zackery Spytzae62f012018-10-06 00:44:25 -0600286 if (t == NULL) {
287 ret = -1;
288 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000289 else {
290 fwrite(PyBytes_AS_STRING(t), 1,
291 PyBytes_GET_SIZE(t), fp);
Victor Stinnerba6b4302010-05-17 09:33:42 +0000292 Py_DECREF(t);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000293 }
294 }
295 else {
296 PyErr_Format(PyExc_TypeError,
297 "str() or repr() returned '%.100s'",
Victor Stinner58ac7002020-02-07 03:04:21 +0100298 Py_TYPE(s)->tp_name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000299 ret = -1;
300 }
301 Py_XDECREF(s);
302 }
303 }
304 if (ret == 0) {
305 if (ferror(fp)) {
Serhiy Storchaka55fe1ae2017-04-16 10:46:38 +0300306 PyErr_SetFromErrno(PyExc_OSError);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000307 clearerr(fp);
308 ret = -1;
309 }
310 }
311 return ret;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000312}
313
Guido van Rossum38938152006-08-21 23:36:26 +0000314/* For debugging convenience. Set a breakpoint here and call it from your DLL */
315void
Thomas Woutersb2137042007-02-01 18:02:27 +0000316_Py_BreakPoint(void)
Guido van Rossum38938152006-08-21 23:36:26 +0000317{
318}
319
Neal Norwitz1a997502003-01-13 20:13:12 +0000320
Victor Stinner4c409be2019-04-11 13:01:15 +0200321/* Heuristic checking if the object memory is uninitialized or deallocated.
322 Rely on the debug hooks on Python memory allocators:
323 see _PyMem_IsPtrFreed().
Victor Stinner82af0b62018-10-23 17:39:40 +0200324
325 The function can be used to prevent segmentation fault on dereferencing
Victor Stinner4c409be2019-04-11 13:01:15 +0200326 pointers like 0xDDDDDDDDDDDDDDDD. */
Victor Stinner82af0b62018-10-23 17:39:40 +0200327int
328_PyObject_IsFreed(PyObject *op)
329{
Victor Stinner58ac7002020-02-07 03:04:21 +0100330 if (_PyMem_IsPtrFreed(op) || _PyMem_IsPtrFreed(Py_TYPE(op))) {
Victor Stinner2cf5d322018-11-22 16:32:57 +0100331 return 1;
332 }
Victor Stinner2b00db62019-04-11 11:33:27 +0200333 /* ignore op->ob_ref: its value can have be modified
Victor Stinner82af0b62018-10-23 17:39:40 +0200334 by Py_INCREF() and Py_DECREF(). */
335#ifdef Py_TRACE_REFS
Pablo Galindo36e33c32019-10-08 00:43:14 +0100336 if (op->_ob_next != NULL && _PyMem_IsPtrFreed(op->_ob_next)) {
Victor Stinner2b00db62019-04-11 11:33:27 +0200337 return 1;
338 }
Pablo Galindo36e33c32019-10-08 00:43:14 +0100339 if (op->_ob_prev != NULL && _PyMem_IsPtrFreed(op->_ob_prev)) {
340 return 1;
341 }
Victor Stinner82af0b62018-10-23 17:39:40 +0200342#endif
Victor Stinner2b00db62019-04-11 11:33:27 +0200343 return 0;
Victor Stinner82af0b62018-10-23 17:39:40 +0200344}
345
346
Barry Warsaw9bf16442001-01-23 16:24:35 +0000347/* For debugging convenience. See Misc/gdbinit for some useful gdb hooks */
Guido van Rossum38938152006-08-21 23:36:26 +0000348void
349_PyObject_Dump(PyObject* op)
Barry Warsaw9bf16442001-01-23 16:24:35 +0000350{
Victor Stinner82af0b62018-10-23 17:39:40 +0200351 if (_PyObject_IsFreed(op)) {
352 /* It seems like the object memory has been freed:
353 don't access it to prevent a segmentation fault. */
Victor Stinnerb39afb72019-09-17 23:36:28 +0200354 fprintf(stderr, "<object at %p is freed>\n", op);
Victor Stinner68762572019-10-07 18:42:01 +0200355 fflush(stderr);
Victor Stinner2cf5d322018-11-22 16:32:57 +0100356 return;
Victor Stinner82af0b62018-10-23 17:39:40 +0200357 }
358
Victor Stinner68762572019-10-07 18:42:01 +0200359 /* first, write fields which are the least likely to crash */
360 fprintf(stderr, "object address : %p\n", (void *)op);
Victor Stinner82af0b62018-10-23 17:39:40 +0200361 /* XXX(twouters) cast refcount to long until %zd is
362 universally available */
Victor Stinnera93c51e2020-02-07 00:38:59 +0100363 fprintf(stderr, "object refcount : %ld\n", (long)Py_REFCNT(op));
Victor Stinner68762572019-10-07 18:42:01 +0200364 fflush(stderr);
365
366 PyTypeObject *type = Py_TYPE(op);
367 fprintf(stderr, "object type : %p\n", type);
368 fprintf(stderr, "object type name: %s\n",
369 type==NULL ? "NULL" : type->tp_name);
370
371 /* the most dangerous part */
372 fprintf(stderr, "object repr : ");
373 fflush(stderr);
374
375 PyGILState_STATE gil = PyGILState_Ensure();
376 PyObject *error_type, *error_value, *error_traceback;
377 PyErr_Fetch(&error_type, &error_value, &error_traceback);
378
379 (void)PyObject_Print(op, stderr, 0);
380 fflush(stderr);
381
382 PyErr_Restore(error_type, error_value, error_traceback);
383 PyGILState_Release(gil);
384
385 fprintf(stderr, "\n");
Victor Stinner82af0b62018-10-23 17:39:40 +0200386 fflush(stderr);
Barry Warsaw9bf16442001-01-23 16:24:35 +0000387}
Barry Warsaw903138f2001-01-23 16:33:18 +0000388
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000389PyObject *
Fred Drake100814d2000-07-09 15:48:49 +0000390PyObject_Repr(PyObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000391{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000392 PyObject *res;
393 if (PyErr_CheckSignals())
394 return NULL;
Guido van Rossum9b00dfa1998-04-28 16:06:54 +0000395#ifdef USE_STACKCHECK
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000396 if (PyOS_CheckStack()) {
397 PyErr_SetString(PyExc_MemoryError, "stack overflow");
398 return NULL;
399 }
Guido van Rossum9b00dfa1998-04-28 16:06:54 +0000400#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000401 if (v == NULL)
402 return PyUnicode_FromString("<NULL>");
403 if (Py_TYPE(v)->tp_repr == NULL)
404 return PyUnicode_FromFormat("<%s object at %p>",
Victor Stinner58ac7002020-02-07 03:04:21 +0100405 Py_TYPE(v)->tp_name, v);
Victor Stinner33824f62013-08-26 14:05:19 +0200406
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100407 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinner33824f62013-08-26 14:05:19 +0200408#ifdef Py_DEBUG
409 /* PyObject_Repr() must not be called with an exception set,
Victor Stinnera8cb5152017-01-18 14:12:51 +0100410 because it can clear it (directly or indirectly) and so the
Martin Panter9955a372015-10-07 10:26:23 +0000411 caller loses its exception */
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100412 assert(!_PyErr_Occurred(tstate));
Victor Stinner33824f62013-08-26 14:05:19 +0200413#endif
414
Serhiy Storchaka1fb72d22017-12-03 22:12:11 +0200415 /* It is possible for a type to have a tp_repr representation that loops
416 infinitely. */
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100417 if (_Py_EnterRecursiveCall(tstate,
418 " while getting the repr of an object")) {
Serhiy Storchaka1fb72d22017-12-03 22:12:11 +0200419 return NULL;
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100420 }
Victor Stinner58ac7002020-02-07 03:04:21 +0100421 res = (*Py_TYPE(v)->tp_repr)(v);
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100422 _Py_LeaveRecursiveCall(tstate);
423
424 if (res == NULL) {
Victor Stinner0a54cf12011-12-01 03:22:44 +0100425 return NULL;
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100426 }
Victor Stinner0a54cf12011-12-01 03:22:44 +0100427 if (!PyUnicode_Check(res)) {
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100428 _PyErr_Format(tstate, PyExc_TypeError,
429 "__repr__ returned non-string (type %.200s)",
Victor Stinner58ac7002020-02-07 03:04:21 +0100430 Py_TYPE(res)->tp_name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000431 Py_DECREF(res);
432 return NULL;
433 }
Victor Stinnerdb88ae52011-12-01 02:15:00 +0100434#ifndef Py_DEBUG
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100435 if (PyUnicode_READY(res) < 0) {
Victor Stinnerdb88ae52011-12-01 02:15:00 +0100436 return NULL;
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100437 }
Victor Stinnerdb88ae52011-12-01 02:15:00 +0100438#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000439 return res;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000440}
441
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000442PyObject *
Guido van Rossum98297ee2007-11-06 21:34:58 +0000443PyObject_Str(PyObject *v)
Guido van Rossumc6004111993-11-05 10:22:19 +0000444{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000445 PyObject *res;
446 if (PyErr_CheckSignals())
447 return NULL;
Guido van Rossum98297ee2007-11-06 21:34:58 +0000448#ifdef USE_STACKCHECK
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000449 if (PyOS_CheckStack()) {
450 PyErr_SetString(PyExc_MemoryError, "stack overflow");
451 return NULL;
452 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000453#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000454 if (v == NULL)
455 return PyUnicode_FromString("<NULL>");
456 if (PyUnicode_CheckExact(v)) {
Victor Stinnerdb88ae52011-12-01 02:15:00 +0100457#ifndef Py_DEBUG
Victor Stinner4ead7c72011-11-20 19:48:36 +0100458 if (PyUnicode_READY(v) < 0)
459 return NULL;
Victor Stinnerdb88ae52011-12-01 02:15:00 +0100460#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000461 Py_INCREF(v);
462 return v;
463 }
464 if (Py_TYPE(v)->tp_str == NULL)
465 return PyObject_Repr(v);
Guido van Rossum4f288ab2001-05-01 16:53:37 +0000466
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100467 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinner33824f62013-08-26 14:05:19 +0200468#ifdef Py_DEBUG
469 /* PyObject_Str() must not be called with an exception set,
Victor Stinnera8cb5152017-01-18 14:12:51 +0100470 because it can clear it (directly or indirectly) and so the
Nick Coghland979e432014-02-09 10:43:21 +1000471 caller loses its exception */
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100472 assert(!_PyErr_Occurred(tstate));
Victor Stinner33824f62013-08-26 14:05:19 +0200473#endif
474
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000475 /* It is possible for a type to have a tp_str representation that loops
476 infinitely. */
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100477 if (_Py_EnterRecursiveCall(tstate, " while getting the str of an object")) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000478 return NULL;
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100479 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000480 res = (*Py_TYPE(v)->tp_str)(v);
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100481 _Py_LeaveRecursiveCall(tstate);
482
483 if (res == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000484 return NULL;
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100485 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000486 if (!PyUnicode_Check(res)) {
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100487 _PyErr_Format(tstate, PyExc_TypeError,
488 "__str__ returned non-string (type %.200s)",
489 Py_TYPE(res)->tp_name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000490 Py_DECREF(res);
491 return NULL;
492 }
Victor Stinnerdb88ae52011-12-01 02:15:00 +0100493#ifndef Py_DEBUG
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100494 if (PyUnicode_READY(res) < 0) {
Victor Stinner4ead7c72011-11-20 19:48:36 +0100495 return NULL;
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100496 }
Victor Stinnerdb88ae52011-12-01 02:15:00 +0100497#endif
Victor Stinner4ead7c72011-11-20 19:48:36 +0100498 assert(_PyUnicode_CheckConsistency(res, 1));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000499 return res;
Neil Schemenauercf52c072005-08-12 17:34:58 +0000500}
501
Georg Brandl559e5d72008-06-11 18:37:52 +0000502PyObject *
503PyObject_ASCII(PyObject *v)
504{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000505 PyObject *repr, *ascii, *res;
Georg Brandl559e5d72008-06-11 18:37:52 +0000506
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000507 repr = PyObject_Repr(v);
508 if (repr == NULL)
509 return NULL;
Georg Brandl559e5d72008-06-11 18:37:52 +0000510
Victor Stinneraf037572013-04-14 18:44:10 +0200511 if (PyUnicode_IS_ASCII(repr))
512 return repr;
513
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000514 /* repr is guaranteed to be a PyUnicode object by PyObject_Repr */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200515 ascii = _PyUnicode_AsASCIIString(repr, "backslashreplace");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000516 Py_DECREF(repr);
517 if (ascii == NULL)
518 return NULL;
Georg Brandl559e5d72008-06-11 18:37:52 +0000519
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000520 res = PyUnicode_DecodeASCII(
521 PyBytes_AS_STRING(ascii),
522 PyBytes_GET_SIZE(ascii),
523 NULL);
524
525 Py_DECREF(ascii);
526 return res;
Georg Brandl559e5d72008-06-11 18:37:52 +0000527}
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000528
Benjamin Petersonc15a0732008-08-26 16:46:47 +0000529PyObject *
530PyObject_Bytes(PyObject *v)
531{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000532 PyObject *result, *func;
Benjamin Petersonc15a0732008-08-26 16:46:47 +0000533
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000534 if (v == NULL)
535 return PyBytes_FromString("<NULL>");
Benjamin Petersonc15a0732008-08-26 16:46:47 +0000536
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000537 if (PyBytes_CheckExact(v)) {
538 Py_INCREF(v);
539 return v;
540 }
Benjamin Petersonc15a0732008-08-26 16:46:47 +0000541
Benjamin Petersonce798522012-01-22 11:24:29 -0500542 func = _PyObject_LookupSpecial(v, &PyId___bytes__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000543 if (func != NULL) {
Victor Stinnerf17c3de2016-12-06 18:46:19 +0100544 result = _PyObject_CallNoArg(func);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000545 Py_DECREF(func);
546 if (result == NULL)
Benjamin Peterson41ece392010-09-11 16:39:57 +0000547 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000548 if (!PyBytes_Check(result)) {
Benjamin Peterson41ece392010-09-11 16:39:57 +0000549 PyErr_Format(PyExc_TypeError,
550 "__bytes__ returned non-bytes (type %.200s)",
551 Py_TYPE(result)->tp_name);
552 Py_DECREF(result);
553 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000554 }
555 return result;
556 }
557 else if (PyErr_Occurred())
558 return NULL;
559 return PyBytes_FromObject(v);
Benjamin Petersonc15a0732008-08-26 16:46:47 +0000560}
561
Jeroen Demeyerbf17d412019-11-05 16:48:04 +0100562
563/*
564def _PyObject_FunctionStr(x):
565 try:
566 qualname = x.__qualname__
567 except AttributeError:
568 return str(x)
569 try:
570 mod = x.__module__
571 if mod is not None and mod != 'builtins':
572 return f"{x.__module__}.{qualname}()"
573 except AttributeError:
574 pass
575 return qualname
576*/
577PyObject *
578_PyObject_FunctionStr(PyObject *x)
579{
580 _Py_IDENTIFIER(__module__);
581 _Py_IDENTIFIER(__qualname__);
582 _Py_IDENTIFIER(builtins);
583 assert(!PyErr_Occurred());
584 PyObject *qualname;
585 int ret = _PyObject_LookupAttrId(x, &PyId___qualname__, &qualname);
586 if (qualname == NULL) {
587 if (ret < 0) {
588 return NULL;
589 }
590 return PyObject_Str(x);
591 }
592 PyObject *module;
593 PyObject *result = NULL;
594 ret = _PyObject_LookupAttrId(x, &PyId___module__, &module);
595 if (module != NULL && module != Py_None) {
596 PyObject *builtinsname = _PyUnicode_FromId(&PyId_builtins);
597 if (builtinsname == NULL) {
598 goto done;
599 }
600 ret = PyObject_RichCompareBool(module, builtinsname, Py_NE);
601 if (ret < 0) {
602 // error
603 goto done;
604 }
605 if (ret > 0) {
606 result = PyUnicode_FromFormat("%S.%S()", module, qualname);
607 goto done;
608 }
609 }
610 else if (ret < 0) {
611 goto done;
612 }
613 result = PyUnicode_FromFormat("%S()", qualname);
614done:
615 Py_DECREF(qualname);
616 Py_XDECREF(module);
617 return result;
618}
619
Mark Dickinsonc008a172009-02-01 13:59:22 +0000620/* For Python 3.0.1 and later, the old three-way comparison has been
621 completely removed in favour of rich comparisons. PyObject_Compare() and
622 PyObject_Cmp() are gone, and the builtin cmp function no longer exists.
Jeroen Demeyer530f5062019-05-31 04:13:39 +0200623 The old tp_compare slot has been renamed to tp_as_async, and should no
Mark Dickinsonc008a172009-02-01 13:59:22 +0000624 longer be used. Use tp_richcompare instead.
Guido van Rossum98297ee2007-11-06 21:34:58 +0000625
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000626 See (*) below for practical amendments.
627
Mark Dickinsonc008a172009-02-01 13:59:22 +0000628 tp_richcompare gets called with a first argument of the appropriate type
629 and a second object of an arbitrary type. We never do any kind of
630 coercion.
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000631
Mark Dickinsonc008a172009-02-01 13:59:22 +0000632 The tp_richcompare slot should return an object, as follows:
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000633
634 NULL if an exception occurred
635 NotImplemented if the requested comparison is not implemented
636 any other false value if the requested comparison is false
637 any other true value if the requested comparison is true
638
639 The PyObject_RichCompare[Bool]() wrappers raise TypeError when they get
640 NotImplemented.
641
642 (*) Practical amendments:
643
644 - If rich comparison returns NotImplemented, == and != are decided by
645 comparing the object pointer (i.e. falling back to the base object
646 implementation).
647
Guido van Rossuma4073002002-05-31 20:03:54 +0000648*/
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000649
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000650/* Map rich comparison operators to their swapped version, e.g. LT <--> GT */
Brett Cannona5ca2e72004-09-25 01:37:24 +0000651int _Py_SwappedOp[] = {Py_GT, Py_GE, Py_EQ, Py_NE, Py_LT, Py_LE};
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +0000652
Serhiy Storchaka2d06e842015-12-25 19:53:18 +0200653static const char * const opstrings[] = {"<", "<=", "==", "!=", ">", ">="};
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000654
655/* Perform a rich comparison, raising TypeError when the requested comparison
656 operator is not supported. */
Guido van Rossume797ec12001-01-17 15:24:28 +0000657static PyObject *
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100658do_richcompare(PyThreadState *tstate, PyObject *v, PyObject *w, int op)
Guido van Rossume797ec12001-01-17 15:24:28 +0000659{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000660 richcmpfunc f;
661 PyObject *res;
662 int checked_reverse_op = 0;
Guido van Rossume797ec12001-01-17 15:24:28 +0000663
Andy Lester55728702020-03-06 16:53:17 -0600664 if (!Py_IS_TYPE(v, Py_TYPE(w)) &&
Victor Stinner58ac7002020-02-07 03:04:21 +0100665 PyType_IsSubtype(Py_TYPE(w), Py_TYPE(v)) &&
666 (f = Py_TYPE(w)->tp_richcompare) != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000667 checked_reverse_op = 1;
668 res = (*f)(w, v, _Py_SwappedOp[op]);
669 if (res != Py_NotImplemented)
670 return res;
671 Py_DECREF(res);
672 }
Victor Stinner58ac7002020-02-07 03:04:21 +0100673 if ((f = Py_TYPE(v)->tp_richcompare) != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000674 res = (*f)(v, w, op);
675 if (res != Py_NotImplemented)
676 return res;
677 Py_DECREF(res);
678 }
Victor Stinner58ac7002020-02-07 03:04:21 +0100679 if (!checked_reverse_op && (f = Py_TYPE(w)->tp_richcompare) != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000680 res = (*f)(w, v, _Py_SwappedOp[op]);
681 if (res != Py_NotImplemented)
682 return res;
683 Py_DECREF(res);
684 }
685 /* If neither object implements it, provide a sensible default
686 for == and !=, but raise an exception for ordering. */
687 switch (op) {
688 case Py_EQ:
689 res = (v == w) ? Py_True : Py_False;
690 break;
691 case Py_NE:
692 res = (v != w) ? Py_True : Py_False;
693 break;
694 default:
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100695 _PyErr_Format(tstate, PyExc_TypeError,
696 "'%s' not supported between instances of '%.100s' and '%.100s'",
697 opstrings[op],
Victor Stinner58ac7002020-02-07 03:04:21 +0100698 Py_TYPE(v)->tp_name,
699 Py_TYPE(w)->tp_name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000700 return NULL;
701 }
702 Py_INCREF(res);
703 return res;
Guido van Rossume797ec12001-01-17 15:24:28 +0000704}
705
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000706/* Perform a rich comparison with object result. This wraps do_richcompare()
707 with a check for NULL arguments and a recursion check. */
Guido van Rossume797ec12001-01-17 15:24:28 +0000708
Guido van Rossume797ec12001-01-17 15:24:28 +0000709PyObject *
710PyObject_RichCompare(PyObject *v, PyObject *w, int op)
711{
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100712 PyThreadState *tstate = _PyThreadState_GET();
Guido van Rossume797ec12001-01-17 15:24:28 +0000713
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000714 assert(Py_LT <= op && op <= Py_GE);
715 if (v == NULL || w == NULL) {
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100716 if (!_PyErr_Occurred(tstate)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000717 PyErr_BadInternalCall();
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100718 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000719 return NULL;
720 }
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100721 if (_Py_EnterRecursiveCall(tstate, " in comparison")) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000722 return NULL;
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100723 }
724 PyObject *res = do_richcompare(tstate, v, w, op);
725 _Py_LeaveRecursiveCall(tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000726 return res;
Guido van Rossume797ec12001-01-17 15:24:28 +0000727}
728
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000729/* Perform a rich comparison with integer result. This wraps
730 PyObject_RichCompare(), returning -1 for error, 0 for false, 1 for true. */
Guido van Rossume797ec12001-01-17 15:24:28 +0000731int
732PyObject_RichCompareBool(PyObject *v, PyObject *w, int op)
733{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000734 PyObject *res;
735 int ok;
Guido van Rossume797ec12001-01-17 15:24:28 +0000736
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000737 /* Quick result when objects are the same.
738 Guarantees that identity implies equality. */
739 if (v == w) {
740 if (op == Py_EQ)
741 return 1;
742 else if (op == Py_NE)
743 return 0;
744 }
Mark Dickinson4a1f5932008-11-12 23:23:36 +0000745
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000746 res = PyObject_RichCompare(v, w, op);
747 if (res == NULL)
748 return -1;
749 if (PyBool_Check(res))
750 ok = (res == Py_True);
751 else
752 ok = PyObject_IsTrue(res);
753 Py_DECREF(res);
754 return ok;
Guido van Rossume797ec12001-01-17 15:24:28 +0000755}
Fred Drake13634cf2000-06-29 19:17:04 +0000756
Antoine Pitrouce4a9da2011-11-21 20:46:33 +0100757Py_hash_t
Nick Coghland1abd252008-07-15 15:46:38 +0000758PyObject_HashNotImplemented(PyObject *v)
759{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000760 PyErr_Format(PyExc_TypeError, "unhashable type: '%.200s'",
761 Py_TYPE(v)->tp_name);
762 return -1;
Nick Coghland1abd252008-07-15 15:46:38 +0000763}
Fred Drake13634cf2000-06-29 19:17:04 +0000764
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000765Py_hash_t
Fred Drake100814d2000-07-09 15:48:49 +0000766PyObject_Hash(PyObject *v)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000767{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000768 PyTypeObject *tp = Py_TYPE(v);
769 if (tp->tp_hash != NULL)
770 return (*tp->tp_hash)(v);
771 /* To keep to the general practice that inheriting
772 * solely from object in C code should work without
773 * an explicit call to PyType_Ready, we implicitly call
774 * PyType_Ready here and then check the tp_hash slot again
775 */
776 if (tp->tp_dict == NULL) {
777 if (PyType_Ready(tp) < 0)
778 return -1;
779 if (tp->tp_hash != NULL)
780 return (*tp->tp_hash)(v);
781 }
782 /* Otherwise, the object can't be hashed */
783 return PyObject_HashNotImplemented(v);
Guido van Rossum9bfef441993-03-29 10:43:31 +0000784}
785
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000786PyObject *
Jeremy Hyltonaf68c872005-12-10 18:50:16 +0000787PyObject_GetAttrString(PyObject *v, const char *name)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000788{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000789 PyObject *w, *res;
Guido van Rossumd8eb1b31996-08-09 20:52:03 +0000790
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000791 if (Py_TYPE(v)->tp_getattr != NULL)
792 return (*Py_TYPE(v)->tp_getattr)(v, (char*)name);
INADA Naoki3e8d6cb2017-02-21 23:57:25 +0900793 w = PyUnicode_FromString(name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000794 if (w == NULL)
795 return NULL;
796 res = PyObject_GetAttr(v, w);
Victor Stinner59af08f2012-03-22 02:09:08 +0100797 Py_DECREF(w);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000798 return res;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000799}
800
801int
Jeremy Hyltonaf68c872005-12-10 18:50:16 +0000802PyObject_HasAttrString(PyObject *v, const char *name)
Guido van Rossumed18fdc1993-07-11 19:55:34 +0000803{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000804 PyObject *res = PyObject_GetAttrString(v, name);
805 if (res != NULL) {
806 Py_DECREF(res);
807 return 1;
808 }
809 PyErr_Clear();
810 return 0;
Guido van Rossumed18fdc1993-07-11 19:55:34 +0000811}
812
813int
Jeremy Hyltonaf68c872005-12-10 18:50:16 +0000814PyObject_SetAttrString(PyObject *v, const char *name, PyObject *w)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000815{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000816 PyObject *s;
817 int res;
Guido van Rossumd8eb1b31996-08-09 20:52:03 +0000818
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000819 if (Py_TYPE(v)->tp_setattr != NULL)
820 return (*Py_TYPE(v)->tp_setattr)(v, (char*)name, w);
821 s = PyUnicode_InternFromString(name);
822 if (s == NULL)
823 return -1;
824 res = PyObject_SetAttr(v, s, w);
825 Py_XDECREF(s);
826 return res;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000827}
828
Benjamin Petersonbfebb7b2011-12-15 15:34:02 -0500829int
830_PyObject_IsAbstract(PyObject *obj)
831{
832 int res;
833 PyObject* isabstract;
Benjamin Petersonbfebb7b2011-12-15 15:34:02 -0500834
835 if (obj == NULL)
836 return 0;
837
Serhiy Storchakaf320be72018-01-25 10:49:40 +0200838 res = _PyObject_LookupAttrId(obj, &PyId___isabstractmethod__, &isabstract);
839 if (res > 0) {
840 res = PyObject_IsTrue(isabstract);
841 Py_DECREF(isabstract);
Benjamin Petersonbfebb7b2011-12-15 15:34:02 -0500842 }
Benjamin Petersonbfebb7b2011-12-15 15:34:02 -0500843 return res;
844}
845
Guido van Rossum98ff96a1997-05-20 18:34:44 +0000846PyObject *
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200847_PyObject_GetAttrId(PyObject *v, _Py_Identifier *name)
848{
849 PyObject *result;
Martin v. Löwisd10759f2011-11-07 13:00:05 +0100850 PyObject *oname = _PyUnicode_FromId(name); /* borrowed */
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200851 if (!oname)
852 return NULL;
853 result = PyObject_GetAttr(v, oname);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200854 return result;
855}
856
857int
858_PyObject_HasAttrId(PyObject *v, _Py_Identifier *name)
859{
860 int result;
Martin v. Löwisd10759f2011-11-07 13:00:05 +0100861 PyObject *oname = _PyUnicode_FromId(name); /* borrowed */
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200862 if (!oname)
863 return -1;
864 result = PyObject_HasAttr(v, oname);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200865 return result;
866}
867
868int
869_PyObject_SetAttrId(PyObject *v, _Py_Identifier *name, PyObject *w)
870{
871 int result;
Martin v. Löwisd10759f2011-11-07 13:00:05 +0100872 PyObject *oname = _PyUnicode_FromId(name); /* borrowed */
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200873 if (!oname)
874 return -1;
875 result = PyObject_SetAttr(v, oname, w);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200876 return result;
877}
878
879PyObject *
Fred Drake100814d2000-07-09 15:48:49 +0000880PyObject_GetAttr(PyObject *v, PyObject *name)
Guido van Rossum98ff96a1997-05-20 18:34:44 +0000881{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000882 PyTypeObject *tp = Py_TYPE(v);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000883
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000884 if (!PyUnicode_Check(name)) {
885 PyErr_Format(PyExc_TypeError,
886 "attribute name must be string, not '%.200s'",
Victor Stinner58ac7002020-02-07 03:04:21 +0100887 Py_TYPE(name)->tp_name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000888 return NULL;
889 }
890 if (tp->tp_getattro != NULL)
891 return (*tp->tp_getattro)(v, name);
892 if (tp->tp_getattr != NULL) {
Serhiy Storchaka2a404b62017-01-22 23:07:07 +0200893 const char *name_str = PyUnicode_AsUTF8(name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000894 if (name_str == NULL)
895 return NULL;
Serhiy Storchaka2a404b62017-01-22 23:07:07 +0200896 return (*tp->tp_getattr)(v, (char *)name_str);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000897 }
898 PyErr_Format(PyExc_AttributeError,
899 "'%.50s' object has no attribute '%U'",
900 tp->tp_name, name);
901 return NULL;
Guido van Rossum98ff96a1997-05-20 18:34:44 +0000902}
903
Serhiy Storchakaf320be72018-01-25 10:49:40 +0200904int
905_PyObject_LookupAttr(PyObject *v, PyObject *name, PyObject **result)
INADA Naoki378edee2018-01-16 20:52:41 +0900906{
907 PyTypeObject *tp = Py_TYPE(v);
INADA Naoki378edee2018-01-16 20:52:41 +0900908
909 if (!PyUnicode_Check(name)) {
910 PyErr_Format(PyExc_TypeError,
911 "attribute name must be string, not '%.200s'",
Victor Stinner58ac7002020-02-07 03:04:21 +0100912 Py_TYPE(name)->tp_name);
Serhiy Storchakaf320be72018-01-25 10:49:40 +0200913 *result = NULL;
914 return -1;
INADA Naoki378edee2018-01-16 20:52:41 +0900915 }
916
917 if (tp->tp_getattro == PyObject_GenericGetAttr) {
Serhiy Storchakaf320be72018-01-25 10:49:40 +0200918 *result = _PyObject_GenericGetAttrWithDict(v, name, NULL, 1);
919 if (*result != NULL) {
920 return 1;
921 }
922 if (PyErr_Occurred()) {
923 return -1;
924 }
925 return 0;
INADA Naoki378edee2018-01-16 20:52:41 +0900926 }
927 if (tp->tp_getattro != NULL) {
Serhiy Storchakaf320be72018-01-25 10:49:40 +0200928 *result = (*tp->tp_getattro)(v, name);
INADA Naoki378edee2018-01-16 20:52:41 +0900929 }
930 else if (tp->tp_getattr != NULL) {
931 const char *name_str = PyUnicode_AsUTF8(name);
Serhiy Storchakaf320be72018-01-25 10:49:40 +0200932 if (name_str == NULL) {
933 *result = NULL;
934 return -1;
935 }
936 *result = (*tp->tp_getattr)(v, (char *)name_str);
INADA Naoki378edee2018-01-16 20:52:41 +0900937 }
INADA Naokie76daeb2018-01-26 16:22:51 +0900938 else {
939 *result = NULL;
940 return 0;
941 }
942
Serhiy Storchakaf320be72018-01-25 10:49:40 +0200943 if (*result != NULL) {
944 return 1;
INADA Naoki378edee2018-01-16 20:52:41 +0900945 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +0200946 if (!PyErr_ExceptionMatches(PyExc_AttributeError)) {
947 return -1;
948 }
949 PyErr_Clear();
950 return 0;
951}
952
953int
954_PyObject_LookupAttrId(PyObject *v, _Py_Identifier *name, PyObject **result)
955{
956 PyObject *oname = _PyUnicode_FromId(name); /* borrowed */
957 if (!oname) {
958 *result = NULL;
959 return -1;
960 }
961 return _PyObject_LookupAttr(v, oname, result);
INADA Naoki378edee2018-01-16 20:52:41 +0900962}
963
Guido van Rossum98ff96a1997-05-20 18:34:44 +0000964int
Fred Drake100814d2000-07-09 15:48:49 +0000965PyObject_HasAttr(PyObject *v, PyObject *name)
Guido van Rossum98ff96a1997-05-20 18:34:44 +0000966{
Serhiy Storchakaf320be72018-01-25 10:49:40 +0200967 PyObject *res;
968 if (_PyObject_LookupAttr(v, name, &res) < 0) {
969 PyErr_Clear();
970 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000971 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +0200972 if (res == NULL) {
973 return 0;
974 }
975 Py_DECREF(res);
976 return 1;
Guido van Rossum98ff96a1997-05-20 18:34:44 +0000977}
978
979int
Fred Drake100814d2000-07-09 15:48:49 +0000980PyObject_SetAttr(PyObject *v, PyObject *name, PyObject *value)
Guido van Rossum98ff96a1997-05-20 18:34:44 +0000981{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000982 PyTypeObject *tp = Py_TYPE(v);
983 int err;
Marc-André Lemburge44e5072000-09-18 16:20:57 +0000984
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000985 if (!PyUnicode_Check(name)) {
986 PyErr_Format(PyExc_TypeError,
987 "attribute name must be string, not '%.200s'",
Victor Stinner58ac7002020-02-07 03:04:21 +0100988 Py_TYPE(name)->tp_name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000989 return -1;
990 }
991 Py_INCREF(name);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000992
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000993 PyUnicode_InternInPlace(&name);
994 if (tp->tp_setattro != NULL) {
995 err = (*tp->tp_setattro)(v, name, value);
996 Py_DECREF(name);
997 return err;
998 }
999 if (tp->tp_setattr != NULL) {
Serhiy Storchaka2a404b62017-01-22 23:07:07 +02001000 const char *name_str = PyUnicode_AsUTF8(name);
Zackery Spytze0dcb852019-04-28 06:58:52 -06001001 if (name_str == NULL) {
1002 Py_DECREF(name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001003 return -1;
Zackery Spytze0dcb852019-04-28 06:58:52 -06001004 }
Serhiy Storchaka2a404b62017-01-22 23:07:07 +02001005 err = (*tp->tp_setattr)(v, (char *)name_str, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001006 Py_DECREF(name);
1007 return err;
1008 }
1009 Py_DECREF(name);
Victor Stinnera93c51e2020-02-07 00:38:59 +01001010 _PyObject_ASSERT(name, Py_REFCNT(name) >= 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001011 if (tp->tp_getattr == NULL && tp->tp_getattro == NULL)
1012 PyErr_Format(PyExc_TypeError,
1013 "'%.100s' object has no attributes "
1014 "(%s .%U)",
1015 tp->tp_name,
1016 value==NULL ? "del" : "assign to",
1017 name);
1018 else
1019 PyErr_Format(PyExc_TypeError,
1020 "'%.100s' object has only read-only attributes "
1021 "(%s .%U)",
1022 tp->tp_name,
1023 value==NULL ? "del" : "assign to",
1024 name);
1025 return -1;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001026}
1027
1028/* Helper to get a pointer to an object's __dict__ slot, if any */
1029
1030PyObject **
1031_PyObject_GetDictPtr(PyObject *obj)
1032{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001033 Py_ssize_t dictoffset;
1034 PyTypeObject *tp = Py_TYPE(obj);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001035
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001036 dictoffset = tp->tp_dictoffset;
1037 if (dictoffset == 0)
1038 return NULL;
1039 if (dictoffset < 0) {
Victor Stinnerc65b3202020-02-07 11:18:33 +01001040 Py_ssize_t tsize = Py_SIZE(obj);
1041 if (tsize < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001042 tsize = -tsize;
Victor Stinnerc65b3202020-02-07 11:18:33 +01001043 }
1044 size_t size = _PyObject_VAR_SIZE(tp, tsize);
Guido van Rossum2eb0b872002-03-01 22:24:49 +00001045
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001046 dictoffset += (long)size;
Victor Stinner24702042018-10-26 17:16:37 +02001047 _PyObject_ASSERT(obj, dictoffset > 0);
1048 _PyObject_ASSERT(obj, dictoffset % SIZEOF_VOID_P == 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001049 }
1050 return (PyObject **) ((char *)obj + dictoffset);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001051}
1052
Tim Peters6d6c1a32001-08-02 04:15:00 +00001053PyObject *
Raymond Hettinger1da1dbf2003-03-17 19:46:11 +00001054PyObject_SelfIter(PyObject *obj)
Raymond Hettinger01538262003-03-17 08:24:35 +00001055{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001056 Py_INCREF(obj);
1057 return obj;
Raymond Hettinger01538262003-03-17 08:24:35 +00001058}
1059
Amaury Forgeot d'Arcf343e012009-01-12 23:58:21 +00001060/* Helper used when the __next__ method is removed from a type:
1061 tp_iternext is never NULL and can be safely called without checking
1062 on every iteration.
1063 */
1064
1065PyObject *
1066_PyObject_NextNotImplemented(PyObject *self)
1067{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001068 PyErr_Format(PyExc_TypeError,
1069 "'%.200s' object is not iterable",
1070 Py_TYPE(self)->tp_name);
1071 return NULL;
Amaury Forgeot d'Arcf343e012009-01-12 23:58:21 +00001072}
1073
Yury Selivanovf2392132016-12-13 19:03:51 -05001074
1075/* Specialized version of _PyObject_GenericGetAttrWithDict
1076 specifically for the LOAD_METHOD opcode.
1077
1078 Return 1 if a method is found, 0 if it's a regular attribute
1079 from __dict__ or something returned by using a descriptor
1080 protocol.
1081
1082 `method` will point to the resolved attribute or NULL. In the
1083 latter case, an error will be set.
1084*/
1085int
1086_PyObject_GetMethod(PyObject *obj, PyObject *name, PyObject **method)
1087{
1088 PyTypeObject *tp = Py_TYPE(obj);
1089 PyObject *descr;
1090 descrgetfunc f = NULL;
1091 PyObject **dictptr, *dict;
1092 PyObject *attr;
1093 int meth_found = 0;
1094
1095 assert(*method == NULL);
1096
1097 if (Py_TYPE(obj)->tp_getattro != PyObject_GenericGetAttr
1098 || !PyUnicode_Check(name)) {
1099 *method = PyObject_GetAttr(obj, name);
1100 return 0;
1101 }
1102
1103 if (tp->tp_dict == NULL && PyType_Ready(tp) < 0)
1104 return 0;
1105
1106 descr = _PyType_Lookup(tp, name);
1107 if (descr != NULL) {
1108 Py_INCREF(descr);
Victor Stinner45ec5b92020-04-08 01:42:27 +02001109 if (_PyType_HasFeature(Py_TYPE(descr), Py_TPFLAGS_METHOD_DESCRIPTOR)) {
Yury Selivanovf2392132016-12-13 19:03:51 -05001110 meth_found = 1;
1111 } else {
Victor Stinner58ac7002020-02-07 03:04:21 +01001112 f = Py_TYPE(descr)->tp_descr_get;
Yury Selivanovf2392132016-12-13 19:03:51 -05001113 if (f != NULL && PyDescr_IsData(descr)) {
Victor Stinner58ac7002020-02-07 03:04:21 +01001114 *method = f(descr, obj, (PyObject *)Py_TYPE(obj));
Yury Selivanovf2392132016-12-13 19:03:51 -05001115 Py_DECREF(descr);
1116 return 0;
1117 }
1118 }
1119 }
1120
1121 dictptr = _PyObject_GetDictPtr(obj);
1122 if (dictptr != NULL && (dict = *dictptr) != NULL) {
1123 Py_INCREF(dict);
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02001124 attr = PyDict_GetItemWithError(dict, name);
Yury Selivanovf2392132016-12-13 19:03:51 -05001125 if (attr != NULL) {
1126 Py_INCREF(attr);
1127 *method = attr;
1128 Py_DECREF(dict);
1129 Py_XDECREF(descr);
1130 return 0;
1131 }
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02001132 else {
1133 Py_DECREF(dict);
1134 if (PyErr_Occurred()) {
1135 Py_XDECREF(descr);
1136 return 0;
1137 }
1138 }
Yury Selivanovf2392132016-12-13 19:03:51 -05001139 }
1140
1141 if (meth_found) {
1142 *method = descr;
1143 return 1;
1144 }
1145
1146 if (f != NULL) {
1147 *method = f(descr, obj, (PyObject *)Py_TYPE(obj));
1148 Py_DECREF(descr);
1149 return 0;
1150 }
1151
1152 if (descr != NULL) {
1153 *method = descr;
1154 return 0;
1155 }
1156
1157 PyErr_Format(PyExc_AttributeError,
1158 "'%.50s' object has no attribute '%U'",
1159 tp->tp_name, name);
1160 return 0;
1161}
1162
1163/* Generic GetAttr functions - put these in your tp_[gs]etattro slot. */
Michael W. Hudson1593f502004-09-14 17:09:47 +00001164
Raymond Hettinger01538262003-03-17 08:24:35 +00001165PyObject *
INADA Naoki378edee2018-01-16 20:52:41 +09001166_PyObject_GenericGetAttrWithDict(PyObject *obj, PyObject *name,
1167 PyObject *dict, int suppress)
Tim Peters6d6c1a32001-08-02 04:15:00 +00001168{
Yury Selivanovf2392132016-12-13 19:03:51 -05001169 /* Make sure the logic of _PyObject_GetMethod is in sync with
1170 this method.
INADA Naoki378edee2018-01-16 20:52:41 +09001171
Brett Cannond5837382020-07-17 13:09:21 -07001172 When suppress=1, this function suppresses AttributeError.
Yury Selivanovf2392132016-12-13 19:03:51 -05001173 */
1174
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001175 PyTypeObject *tp = Py_TYPE(obj);
1176 PyObject *descr = NULL;
1177 PyObject *res = NULL;
1178 descrgetfunc f;
1179 Py_ssize_t dictoffset;
1180 PyObject **dictptr;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001181
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001182 if (!PyUnicode_Check(name)){
1183 PyErr_Format(PyExc_TypeError,
1184 "attribute name must be string, not '%.200s'",
Victor Stinner58ac7002020-02-07 03:04:21 +01001185 Py_TYPE(name)->tp_name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001186 return NULL;
1187 }
Serhiy Storchaka55c861f2016-04-17 20:31:51 +03001188 Py_INCREF(name);
Guido van Rossumebca9fc2001-12-04 15:54:53 +00001189
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001190 if (tp->tp_dict == NULL) {
1191 if (PyType_Ready(tp) < 0)
1192 goto done;
1193 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001194
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001195 descr = _PyType_Lookup(tp, name);
Michael W. Hudsonb2c7de42003-08-15 13:07:47 +00001196
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001197 f = NULL;
1198 if (descr != NULL) {
Serhiy Storchaka55c861f2016-04-17 20:31:51 +03001199 Py_INCREF(descr);
Victor Stinner58ac7002020-02-07 03:04:21 +01001200 f = Py_TYPE(descr)->tp_descr_get;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001201 if (f != NULL && PyDescr_IsData(descr)) {
Victor Stinner58ac7002020-02-07 03:04:21 +01001202 res = f(descr, obj, (PyObject *)Py_TYPE(obj));
INADA Naoki378edee2018-01-16 20:52:41 +09001203 if (res == NULL && suppress &&
1204 PyErr_ExceptionMatches(PyExc_AttributeError)) {
1205 PyErr_Clear();
1206 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001207 goto done;
1208 }
1209 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001210
Antoine Pitrou1a9a9d52010-08-28 18:17:03 +00001211 if (dict == NULL) {
1212 /* Inline _PyObject_GetDictPtr */
1213 dictoffset = tp->tp_dictoffset;
1214 if (dictoffset != 0) {
1215 if (dictoffset < 0) {
Victor Stinnerc65b3202020-02-07 11:18:33 +01001216 Py_ssize_t tsize = Py_SIZE(obj);
1217 if (tsize < 0) {
Antoine Pitrou1a9a9d52010-08-28 18:17:03 +00001218 tsize = -tsize;
Victor Stinnerc65b3202020-02-07 11:18:33 +01001219 }
1220 size_t size = _PyObject_VAR_SIZE(tp, tsize);
Victor Stinner24702042018-10-26 17:16:37 +02001221 _PyObject_ASSERT(obj, size <= PY_SSIZE_T_MAX);
Guido van Rossumc66ff442002-08-19 16:50:48 +00001222
Serhiy Storchaka55c861f2016-04-17 20:31:51 +03001223 dictoffset += (Py_ssize_t)size;
Victor Stinner24702042018-10-26 17:16:37 +02001224 _PyObject_ASSERT(obj, dictoffset > 0);
1225 _PyObject_ASSERT(obj, dictoffset % SIZEOF_VOID_P == 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001226 }
Antoine Pitrou1a9a9d52010-08-28 18:17:03 +00001227 dictptr = (PyObject **) ((char *)obj + dictoffset);
1228 dict = *dictptr;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001229 }
1230 }
Antoine Pitrou1a9a9d52010-08-28 18:17:03 +00001231 if (dict != NULL) {
1232 Py_INCREF(dict);
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02001233 res = PyDict_GetItemWithError(dict, name);
Antoine Pitrou1a9a9d52010-08-28 18:17:03 +00001234 if (res != NULL) {
1235 Py_INCREF(res);
Antoine Pitrou1a9a9d52010-08-28 18:17:03 +00001236 Py_DECREF(dict);
1237 goto done;
1238 }
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02001239 else {
1240 Py_DECREF(dict);
1241 if (PyErr_Occurred()) {
1242 if (suppress && PyErr_ExceptionMatches(PyExc_AttributeError)) {
1243 PyErr_Clear();
1244 }
1245 else {
1246 goto done;
1247 }
1248 }
1249 }
Antoine Pitrou1a9a9d52010-08-28 18:17:03 +00001250 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001251
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001252 if (f != NULL) {
1253 res = f(descr, obj, (PyObject *)Py_TYPE(obj));
INADA Naoki378edee2018-01-16 20:52:41 +09001254 if (res == NULL && suppress &&
1255 PyErr_ExceptionMatches(PyExc_AttributeError)) {
1256 PyErr_Clear();
1257 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001258 goto done;
1259 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001260
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001261 if (descr != NULL) {
1262 res = descr;
Victor Stinner2d01dc02012-03-09 00:44:13 +01001263 descr = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001264 goto done;
1265 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001266
INADA Naoki378edee2018-01-16 20:52:41 +09001267 if (!suppress) {
1268 PyErr_Format(PyExc_AttributeError,
1269 "'%.50s' object has no attribute '%U'",
1270 tp->tp_name, name);
1271 }
Guido van Rossumebca9fc2001-12-04 15:54:53 +00001272 done:
Victor Stinner2d01dc02012-03-09 00:44:13 +01001273 Py_XDECREF(descr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001274 Py_DECREF(name);
1275 return res;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001276}
1277
Antoine Pitrou1a9a9d52010-08-28 18:17:03 +00001278PyObject *
1279PyObject_GenericGetAttr(PyObject *obj, PyObject *name)
1280{
INADA Naoki378edee2018-01-16 20:52:41 +09001281 return _PyObject_GenericGetAttrWithDict(obj, name, NULL, 0);
Antoine Pitrou1a9a9d52010-08-28 18:17:03 +00001282}
1283
Tim Peters6d6c1a32001-08-02 04:15:00 +00001284int
Antoine Pitrou1a9a9d52010-08-28 18:17:03 +00001285_PyObject_GenericSetAttrWithDict(PyObject *obj, PyObject *name,
1286 PyObject *value, PyObject *dict)
Tim Peters6d6c1a32001-08-02 04:15:00 +00001287{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001288 PyTypeObject *tp = Py_TYPE(obj);
1289 PyObject *descr;
1290 descrsetfunc f;
1291 PyObject **dictptr;
1292 int res = -1;
Guido van Rossumebca9fc2001-12-04 15:54:53 +00001293
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001294 if (!PyUnicode_Check(name)){
1295 PyErr_Format(PyExc_TypeError,
1296 "attribute name must be string, not '%.200s'",
Victor Stinner58ac7002020-02-07 03:04:21 +01001297 Py_TYPE(name)->tp_name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001298 return -1;
1299 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001300
Benjamin Peterson74529ad2012-03-09 07:25:32 -08001301 if (tp->tp_dict == NULL && PyType_Ready(tp) < 0)
1302 return -1;
1303
1304 Py_INCREF(name);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001305
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001306 descr = _PyType_Lookup(tp, name);
Victor Stinner2d01dc02012-03-09 00:44:13 +01001307
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001308 if (descr != NULL) {
Serhiy Storchaka55c861f2016-04-17 20:31:51 +03001309 Py_INCREF(descr);
Victor Stinner58ac7002020-02-07 03:04:21 +01001310 f = Py_TYPE(descr)->tp_descr_set;
Serhiy Storchaka55c861f2016-04-17 20:31:51 +03001311 if (f != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001312 res = f(descr, obj, value);
1313 goto done;
1314 }
1315 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001316
Steve Dowerb82e17e2019-05-23 08:45:22 -07001317 /* XXX [Steve Dower] These are really noisy - worth it? */
1318 /*if (PyType_Check(obj) || PyModule_Check(obj)) {
1319 if (value && PySys_Audit("object.__setattr__", "OOO", obj, name, value) < 0)
1320 return -1;
1321 if (!value && PySys_Audit("object.__delattr__", "OO", obj, name) < 0)
1322 return -1;
1323 }*/
1324
Antoine Pitrou1a9a9d52010-08-28 18:17:03 +00001325 if (dict == NULL) {
1326 dictptr = _PyObject_GetDictPtr(obj);
Serhiy Storchaka55c861f2016-04-17 20:31:51 +03001327 if (dictptr == NULL) {
1328 if (descr == NULL) {
1329 PyErr_Format(PyExc_AttributeError,
1330 "'%.100s' object has no attribute '%U'",
1331 tp->tp_name, name);
1332 }
1333 else {
1334 PyErr_Format(PyExc_AttributeError,
1335 "'%.50s' object attribute '%U' is read-only",
1336 tp->tp_name, name);
1337 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001338 goto done;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001339 }
Serhiy Storchaka55c861f2016-04-17 20:31:51 +03001340 res = _PyObjectDict_SetItem(tp, dictptr, name, value);
Antoine Pitrou1a9a9d52010-08-28 18:17:03 +00001341 }
Serhiy Storchaka55c861f2016-04-17 20:31:51 +03001342 else {
Antoine Pitrou1a9a9d52010-08-28 18:17:03 +00001343 Py_INCREF(dict);
1344 if (value == NULL)
1345 res = PyDict_DelItem(dict, name);
1346 else
1347 res = PyDict_SetItem(dict, name, value);
Benjamin Peterson74529ad2012-03-09 07:25:32 -08001348 Py_DECREF(dict);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001349 }
Serhiy Storchaka55c861f2016-04-17 20:31:51 +03001350 if (res < 0 && PyErr_ExceptionMatches(PyExc_KeyError))
1351 PyErr_SetObject(PyExc_AttributeError, name);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001352
Guido van Rossumebca9fc2001-12-04 15:54:53 +00001353 done:
Victor Stinner2d01dc02012-03-09 00:44:13 +01001354 Py_XDECREF(descr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001355 Py_DECREF(name);
1356 return res;
Guido van Rossum98ff96a1997-05-20 18:34:44 +00001357}
1358
Antoine Pitrou1a9a9d52010-08-28 18:17:03 +00001359int
1360PyObject_GenericSetAttr(PyObject *obj, PyObject *name, PyObject *value)
1361{
1362 return _PyObject_GenericSetAttrWithDict(obj, name, value, NULL);
1363}
1364
Benjamin Peterson8eb12692012-02-19 19:59:10 -05001365int
1366PyObject_GenericSetDict(PyObject *obj, PyObject *value, void *context)
1367{
Serhiy Storchaka576f1322016-01-05 21:27:54 +02001368 PyObject **dictptr = _PyObject_GetDictPtr(obj);
Benjamin Peterson8eb12692012-02-19 19:59:10 -05001369 if (dictptr == NULL) {
1370 PyErr_SetString(PyExc_AttributeError,
1371 "This object has no __dict__");
1372 return -1;
1373 }
1374 if (value == NULL) {
1375 PyErr_SetString(PyExc_TypeError, "cannot delete __dict__");
1376 return -1;
1377 }
1378 if (!PyDict_Check(value)) {
1379 PyErr_Format(PyExc_TypeError,
1380 "__dict__ must be set to a dictionary, "
1381 "not a '%.200s'", Py_TYPE(value)->tp_name);
1382 return -1;
1383 }
Serhiy Storchaka576f1322016-01-05 21:27:54 +02001384 Py_INCREF(value);
Serhiy Storchakaec397562016-04-06 09:50:03 +03001385 Py_XSETREF(*dictptr, value);
Benjamin Peterson8eb12692012-02-19 19:59:10 -05001386 return 0;
1387}
1388
Antoine Pitrou1a9a9d52010-08-28 18:17:03 +00001389
Guido van Rossum6ac258d1993-05-12 08:24:20 +00001390/* Test a value used as condition, e.g., in a for or if statement.
1391 Return -1 if an error occurred */
1392
1393int
Fred Drake100814d2000-07-09 15:48:49 +00001394PyObject_IsTrue(PyObject *v)
Guido van Rossum6ac258d1993-05-12 08:24:20 +00001395{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001396 Py_ssize_t res;
1397 if (v == Py_True)
1398 return 1;
1399 if (v == Py_False)
1400 return 0;
1401 if (v == Py_None)
1402 return 0;
Victor Stinner58ac7002020-02-07 03:04:21 +01001403 else if (Py_TYPE(v)->tp_as_number != NULL &&
1404 Py_TYPE(v)->tp_as_number->nb_bool != NULL)
1405 res = (*Py_TYPE(v)->tp_as_number->nb_bool)(v);
1406 else if (Py_TYPE(v)->tp_as_mapping != NULL &&
1407 Py_TYPE(v)->tp_as_mapping->mp_length != NULL)
1408 res = (*Py_TYPE(v)->tp_as_mapping->mp_length)(v);
1409 else if (Py_TYPE(v)->tp_as_sequence != NULL &&
1410 Py_TYPE(v)->tp_as_sequence->sq_length != NULL)
1411 res = (*Py_TYPE(v)->tp_as_sequence->sq_length)(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001412 else
1413 return 1;
1414 /* if it is negative, it should be either -1 or -2 */
1415 return (res > 0) ? 1 : Py_SAFE_DOWNCAST(res, Py_ssize_t, int);
Guido van Rossum6ac258d1993-05-12 08:24:20 +00001416}
1417
Tim Peters803526b2002-07-07 05:13:56 +00001418/* equivalent of 'not v'
Guido van Rossumc3d3f961998-04-09 17:53:59 +00001419 Return -1 if an error occurred */
1420
1421int
Fred Drake100814d2000-07-09 15:48:49 +00001422PyObject_Not(PyObject *v)
Guido van Rossumc3d3f961998-04-09 17:53:59 +00001423{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001424 int res;
1425 res = PyObject_IsTrue(v);
1426 if (res < 0)
1427 return res;
1428 return res == 0;
Guido van Rossumc3d3f961998-04-09 17:53:59 +00001429}
1430
Guido van Rossum49b11fe1995-01-26 00:38:22 +00001431/* Test whether an object can be called */
1432
1433int
Fred Drake100814d2000-07-09 15:48:49 +00001434PyCallable_Check(PyObject *x)
Guido van Rossum49b11fe1995-01-26 00:38:22 +00001435{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001436 if (x == NULL)
1437 return 0;
Victor Stinner58ac7002020-02-07 03:04:21 +01001438 return Py_TYPE(x)->tp_call != NULL;
Guido van Rossum49b11fe1995-01-26 00:38:22 +00001439}
1440
Tim Peters7eea37e2001-09-04 22:08:56 +00001441
Georg Brandle32b4222007-03-10 22:13:27 +00001442/* Helper for PyObject_Dir without arguments: returns the local scope. */
1443static PyObject *
Guido van Rossumad7d8d12007-04-13 01:39:34 +00001444_dir_locals(void)
Tim Peters305b5852001-09-17 02:38:46 +00001445{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001446 PyObject *names;
Victor Stinner41bb43a2013-10-29 01:19:37 +01001447 PyObject *locals;
Tim Peters305b5852001-09-17 02:38:46 +00001448
Victor Stinner41bb43a2013-10-29 01:19:37 +01001449 locals = PyEval_GetLocals();
1450 if (locals == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001451 return NULL;
Tim Peters305b5852001-09-17 02:38:46 +00001452
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001453 names = PyMapping_Keys(locals);
1454 if (!names)
1455 return NULL;
1456 if (!PyList_Check(names)) {
1457 PyErr_Format(PyExc_TypeError,
1458 "dir(): expected keys() of locals to be a list, "
1459 "not '%.200s'", Py_TYPE(names)->tp_name);
1460 Py_DECREF(names);
1461 return NULL;
1462 }
Benjamin Peterson3bbb7222011-06-11 16:12:08 -05001463 if (PyList_Sort(names)) {
1464 Py_DECREF(names);
1465 return NULL;
1466 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001467 /* the locals don't need to be DECREF'd */
1468 return names;
Georg Brandle32b4222007-03-10 22:13:27 +00001469}
1470
Benjamin Peterson82b00c12011-05-24 11:09:06 -05001471/* Helper for PyObject_Dir: object introspection. */
Georg Brandle32b4222007-03-10 22:13:27 +00001472static PyObject *
1473_dir_object(PyObject *obj)
1474{
Benjamin Peterson3bbb7222011-06-11 16:12:08 -05001475 PyObject *result, *sorted;
Benjamin Petersonce798522012-01-22 11:24:29 -05001476 PyObject *dirfunc = _PyObject_LookupSpecial(obj, &PyId___dir__);
Georg Brandle32b4222007-03-10 22:13:27 +00001477
Victor Stinner24702042018-10-26 17:16:37 +02001478 assert(obj != NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001479 if (dirfunc == NULL) {
Benjamin Peterson82b00c12011-05-24 11:09:06 -05001480 if (!PyErr_Occurred())
1481 PyErr_SetString(PyExc_TypeError, "object does not provide __dir__");
1482 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001483 }
Benjamin Peterson82b00c12011-05-24 11:09:06 -05001484 /* use __dir__ */
Victor Stinnerf17c3de2016-12-06 18:46:19 +01001485 result = _PyObject_CallNoArg(dirfunc);
Benjamin Peterson82b00c12011-05-24 11:09:06 -05001486 Py_DECREF(dirfunc);
1487 if (result == NULL)
1488 return NULL;
Benjamin Peterson3bbb7222011-06-11 16:12:08 -05001489 /* return sorted(result) */
1490 sorted = PySequence_List(result);
1491 Py_DECREF(result);
1492 if (sorted == NULL)
1493 return NULL;
1494 if (PyList_Sort(sorted)) {
1495 Py_DECREF(sorted);
1496 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001497 }
Benjamin Peterson3bbb7222011-06-11 16:12:08 -05001498 return sorted;
Georg Brandle32b4222007-03-10 22:13:27 +00001499}
1500
1501/* Implementation of dir() -- if obj is NULL, returns the names in the current
1502 (local) scope. Otherwise, performs introspection of the object: returns a
1503 sorted list of attribute names (supposedly) accessible from the object
1504*/
1505PyObject *
1506PyObject_Dir(PyObject *obj)
1507{
Benjamin Peterson3bbb7222011-06-11 16:12:08 -05001508 return (obj == NULL) ? _dir_locals() : _dir_object(obj);
Tim Peters7eea37e2001-09-04 22:08:56 +00001509}
Guido van Rossum49b11fe1995-01-26 00:38:22 +00001510
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001511/*
Raymond Hettinger66d2be82011-07-28 09:55:13 -07001512None is a non-NULL undefined value.
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001513There is (and should be!) no way to create other objects of this type,
Guido van Rossum3f5da241990-12-20 15:06:42 +00001514so there is exactly one (which is indestructible, by the way).
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001515*/
1516
Guido van Rossum0c182a11992-03-27 17:26:13 +00001517/* ARGSUSED */
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001518static PyObject *
Fred Drake100814d2000-07-09 15:48:49 +00001519none_repr(PyObject *op)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001520{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001521 return PyUnicode_FromString("None");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001522}
1523
Barry Warsaw9bf16442001-01-23 16:24:35 +00001524/* ARGUSED */
Victor Stinner2a4903f2020-01-30 13:09:11 +01001525static void _Py_NO_RETURN
Tim Peters803526b2002-07-07 05:13:56 +00001526none_dealloc(PyObject* ignore)
Barry Warsaw9bf16442001-01-23 16:24:35 +00001527{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001528 /* This should never get called, but we also don't want to SEGV if
1529 * we accidentally decref None out of existence.
1530 */
1531 Py_FatalError("deallocating None");
Barry Warsaw9bf16442001-01-23 16:24:35 +00001532}
1533
Benjamin Petersonc4607ae2011-07-29 18:19:43 -05001534static PyObject *
1535none_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
1536{
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +02001537 if (PyTuple_GET_SIZE(args) || (kwargs && PyDict_GET_SIZE(kwargs))) {
Benjamin Petersonc4607ae2011-07-29 18:19:43 -05001538 PyErr_SetString(PyExc_TypeError, "NoneType takes no arguments");
1539 return NULL;
1540 }
1541 Py_RETURN_NONE;
1542}
1543
Raymond Hettinger66d2be82011-07-28 09:55:13 -07001544static int
1545none_bool(PyObject *v)
1546{
1547 return 0;
1548}
1549
1550static PyNumberMethods none_as_number = {
1551 0, /* nb_add */
1552 0, /* nb_subtract */
1553 0, /* nb_multiply */
1554 0, /* nb_remainder */
1555 0, /* nb_divmod */
1556 0, /* nb_power */
1557 0, /* nb_negative */
1558 0, /* nb_positive */
1559 0, /* nb_absolute */
1560 (inquiry)none_bool, /* nb_bool */
1561 0, /* nb_invert */
1562 0, /* nb_lshift */
1563 0, /* nb_rshift */
1564 0, /* nb_and */
1565 0, /* nb_xor */
1566 0, /* nb_or */
1567 0, /* nb_int */
1568 0, /* nb_reserved */
1569 0, /* nb_float */
1570 0, /* nb_inplace_add */
1571 0, /* nb_inplace_subtract */
1572 0, /* nb_inplace_multiply */
1573 0, /* nb_inplace_remainder */
1574 0, /* nb_inplace_power */
1575 0, /* nb_inplace_lshift */
1576 0, /* nb_inplace_rshift */
1577 0, /* nb_inplace_and */
1578 0, /* nb_inplace_xor */
1579 0, /* nb_inplace_or */
1580 0, /* nb_floor_divide */
1581 0, /* nb_true_divide */
1582 0, /* nb_inplace_floor_divide */
1583 0, /* nb_inplace_true_divide */
1584 0, /* nb_index */
1585};
Barry Warsaw9bf16442001-01-23 16:24:35 +00001586
Alexandre Vassalotti65846c62013-11-30 17:55:48 -08001587PyTypeObject _PyNone_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001588 PyVarObject_HEAD_INIT(&PyType_Type, 0)
1589 "NoneType",
1590 0,
1591 0,
1592 none_dealloc, /*tp_dealloc*/ /*never called*/
Jeroen Demeyer530f5062019-05-31 04:13:39 +02001593 0, /*tp_vectorcall_offset*/
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001594 0, /*tp_getattr*/
1595 0, /*tp_setattr*/
Jeroen Demeyer530f5062019-05-31 04:13:39 +02001596 0, /*tp_as_async*/
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001597 none_repr, /*tp_repr*/
Raymond Hettinger66d2be82011-07-28 09:55:13 -07001598 &none_as_number, /*tp_as_number*/
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001599 0, /*tp_as_sequence*/
1600 0, /*tp_as_mapping*/
1601 0, /*tp_hash */
Benjamin Petersonc4607ae2011-07-29 18:19:43 -05001602 0, /*tp_call */
1603 0, /*tp_str */
1604 0, /*tp_getattro */
1605 0, /*tp_setattro */
1606 0, /*tp_as_buffer */
1607 Py_TPFLAGS_DEFAULT, /*tp_flags */
1608 0, /*tp_doc */
1609 0, /*tp_traverse */
1610 0, /*tp_clear */
1611 0, /*tp_richcompare */
1612 0, /*tp_weaklistoffset */
1613 0, /*tp_iter */
1614 0, /*tp_iternext */
1615 0, /*tp_methods */
1616 0, /*tp_members */
1617 0, /*tp_getset */
1618 0, /*tp_base */
1619 0, /*tp_dict */
1620 0, /*tp_descr_get */
1621 0, /*tp_descr_set */
1622 0, /*tp_dictoffset */
1623 0, /*tp_init */
1624 0, /*tp_alloc */
1625 none_new, /*tp_new */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001626};
1627
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001628PyObject _Py_NoneStruct = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001629 _PyObject_EXTRA_INIT
Alexandre Vassalotti65846c62013-11-30 17:55:48 -08001630 1, &_PyNone_Type
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001631};
1632
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +00001633/* NotImplemented is an object that can be used to signal that an
1634 operation is not implemented for the given type combination. */
1635
1636static PyObject *
1637NotImplemented_repr(PyObject *op)
1638{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001639 return PyUnicode_FromString("NotImplemented");
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +00001640}
1641
Benjamin Peterson18d7d7a2011-07-29 18:27:44 -05001642static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05301643NotImplemented_reduce(PyObject *op, PyObject *Py_UNUSED(ignored))
Alexandre Vassalottic49477b2013-11-24 02:53:45 -08001644{
1645 return PyUnicode_FromString("NotImplemented");
1646}
1647
1648static PyMethodDef notimplemented_methods[] = {
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05301649 {"__reduce__", NotImplemented_reduce, METH_NOARGS, NULL},
Alexandre Vassalottic49477b2013-11-24 02:53:45 -08001650 {NULL, NULL}
1651};
1652
1653static PyObject *
Benjamin Peterson18d7d7a2011-07-29 18:27:44 -05001654notimplemented_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
1655{
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +02001656 if (PyTuple_GET_SIZE(args) || (kwargs && PyDict_GET_SIZE(kwargs))) {
Benjamin Peterson18d7d7a2011-07-29 18:27:44 -05001657 PyErr_SetString(PyExc_TypeError, "NotImplementedType takes no arguments");
1658 return NULL;
1659 }
Brian Curtindfc80e32011-08-10 20:28:54 -05001660 Py_RETURN_NOTIMPLEMENTED;
Benjamin Peterson18d7d7a2011-07-29 18:27:44 -05001661}
1662
Victor Stinner2a4903f2020-01-30 13:09:11 +01001663static void _Py_NO_RETURN
Armin Ronacher226b1db2012-10-06 14:28:58 +02001664notimplemented_dealloc(PyObject* ignore)
1665{
1666 /* This should never get called, but we also don't want to SEGV if
1667 * we accidentally decref NotImplemented out of existence.
1668 */
1669 Py_FatalError("deallocating NotImplemented");
1670}
1671
MojoVampire469325c2020-03-03 18:50:17 +00001672static int
1673notimplemented_bool(PyObject *v)
1674{
1675 if (PyErr_WarnEx(PyExc_DeprecationWarning,
1676 "NotImplemented should not be used in a boolean context",
1677 1) < 0)
1678 {
1679 return -1;
1680 }
1681 return 1;
1682}
1683
1684static PyNumberMethods notimplemented_as_number = {
1685 .nb_bool = notimplemented_bool,
1686};
1687
Alexandre Vassalotti65846c62013-11-30 17:55:48 -08001688PyTypeObject _PyNotImplemented_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001689 PyVarObject_HEAD_INIT(&PyType_Type, 0)
1690 "NotImplementedType",
1691 0,
1692 0,
Armin Ronacher226b1db2012-10-06 14:28:58 +02001693 notimplemented_dealloc, /*tp_dealloc*/ /*never called*/
Jeroen Demeyer530f5062019-05-31 04:13:39 +02001694 0, /*tp_vectorcall_offset*/
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001695 0, /*tp_getattr*/
1696 0, /*tp_setattr*/
Jeroen Demeyer530f5062019-05-31 04:13:39 +02001697 0, /*tp_as_async*/
MojoVampire469325c2020-03-03 18:50:17 +00001698 NotImplemented_repr, /*tp_repr*/
1699 &notimplemented_as_number, /*tp_as_number*/
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001700 0, /*tp_as_sequence*/
1701 0, /*tp_as_mapping*/
1702 0, /*tp_hash */
Benjamin Peterson18d7d7a2011-07-29 18:27:44 -05001703 0, /*tp_call */
1704 0, /*tp_str */
1705 0, /*tp_getattro */
1706 0, /*tp_setattro */
1707 0, /*tp_as_buffer */
1708 Py_TPFLAGS_DEFAULT, /*tp_flags */
1709 0, /*tp_doc */
1710 0, /*tp_traverse */
1711 0, /*tp_clear */
1712 0, /*tp_richcompare */
1713 0, /*tp_weaklistoffset */
1714 0, /*tp_iter */
1715 0, /*tp_iternext */
Alexandre Vassalottic49477b2013-11-24 02:53:45 -08001716 notimplemented_methods, /*tp_methods */
Benjamin Peterson18d7d7a2011-07-29 18:27:44 -05001717 0, /*tp_members */
1718 0, /*tp_getset */
1719 0, /*tp_base */
1720 0, /*tp_dict */
1721 0, /*tp_descr_get */
1722 0, /*tp_descr_set */
1723 0, /*tp_dictoffset */
1724 0, /*tp_init */
1725 0, /*tp_alloc */
1726 notimplemented_new, /*tp_new */
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +00001727};
1728
1729PyObject _Py_NotImplementedStruct = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001730 _PyObject_EXTRA_INIT
Alexandre Vassalotti65846c62013-11-30 17:55:48 -08001731 1, &_PyNotImplemented_Type
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +00001732};
1733
Victor Stinner331a6a52019-05-27 16:39:22 +02001734PyStatus
Victor Stinnerab672812019-01-23 15:04:40 +01001735_PyTypes_Init(void)
Guido van Rossumba21a492001-08-16 08:17:26 +00001736{
Victor Stinner7a1f6c22020-01-30 09:02:14 +01001737 PyStatus status = _PyTypes_InitSlotDefs();
1738 if (_PyStatus_EXCEPTION(status)) {
1739 return status;
1740 }
1741
Victor Stinner6d43f6f2019-01-22 21:18:05 +01001742#define INIT_TYPE(TYPE, NAME) \
1743 do { \
1744 if (PyType_Ready(TYPE) < 0) { \
Victor Stinner331a6a52019-05-27 16:39:22 +02001745 return _PyStatus_ERR("Can't initialize " NAME " type"); \
Victor Stinner6d43f6f2019-01-22 21:18:05 +01001746 } \
1747 } while (0)
Victor Stinner5a1bb4e2014-06-02 14:10:59 +02001748
Victor Stinner6d43f6f2019-01-22 21:18:05 +01001749 INIT_TYPE(&PyBaseObject_Type, "object");
1750 INIT_TYPE(&PyType_Type, "type");
1751 INIT_TYPE(&_PyWeakref_RefType, "weakref");
1752 INIT_TYPE(&_PyWeakref_CallableProxyType, "callable weakref proxy");
1753 INIT_TYPE(&_PyWeakref_ProxyType, "weakref proxy");
1754 INIT_TYPE(&PyLong_Type, "int");
1755 INIT_TYPE(&PyBool_Type, "bool");
1756 INIT_TYPE(&PyByteArray_Type, "bytearray");
1757 INIT_TYPE(&PyBytes_Type, "str");
1758 INIT_TYPE(&PyList_Type, "list");
1759 INIT_TYPE(&_PyNone_Type, "None");
1760 INIT_TYPE(&_PyNotImplemented_Type, "NotImplemented");
1761 INIT_TYPE(&PyTraceBack_Type, "traceback");
1762 INIT_TYPE(&PySuper_Type, "super");
1763 INIT_TYPE(&PyRange_Type, "range");
1764 INIT_TYPE(&PyDict_Type, "dict");
1765 INIT_TYPE(&PyDictKeys_Type, "dict keys");
1766 INIT_TYPE(&PyDictValues_Type, "dict values");
1767 INIT_TYPE(&PyDictItems_Type, "dict items");
1768 INIT_TYPE(&PyDictRevIterKey_Type, "reversed dict keys");
1769 INIT_TYPE(&PyDictRevIterValue_Type, "reversed dict values");
1770 INIT_TYPE(&PyDictRevIterItem_Type, "reversed dict items");
1771 INIT_TYPE(&PyODict_Type, "OrderedDict");
1772 INIT_TYPE(&PyODictKeys_Type, "odict_keys");
1773 INIT_TYPE(&PyODictItems_Type, "odict_items");
1774 INIT_TYPE(&PyODictValues_Type, "odict_values");
1775 INIT_TYPE(&PyODictIter_Type, "odict_keyiterator");
1776 INIT_TYPE(&PySet_Type, "set");
1777 INIT_TYPE(&PyUnicode_Type, "str");
1778 INIT_TYPE(&PySlice_Type, "slice");
1779 INIT_TYPE(&PyStaticMethod_Type, "static method");
1780 INIT_TYPE(&PyComplex_Type, "complex");
1781 INIT_TYPE(&PyFloat_Type, "float");
1782 INIT_TYPE(&PyFrozenSet_Type, "frozenset");
1783 INIT_TYPE(&PyProperty_Type, "property");
1784 INIT_TYPE(&_PyManagedBuffer_Type, "managed buffer");
1785 INIT_TYPE(&PyMemoryView_Type, "memoryview");
1786 INIT_TYPE(&PyTuple_Type, "tuple");
1787 INIT_TYPE(&PyEnum_Type, "enumerate");
1788 INIT_TYPE(&PyReversed_Type, "reversed");
1789 INIT_TYPE(&PyStdPrinter_Type, "StdPrinter");
1790 INIT_TYPE(&PyCode_Type, "code");
1791 INIT_TYPE(&PyFrame_Type, "frame");
1792 INIT_TYPE(&PyCFunction_Type, "builtin function");
Petr Viktorine1becf42020-05-07 15:39:59 +02001793 INIT_TYPE(&PyCMethod_Type, "builtin method");
Victor Stinner6d43f6f2019-01-22 21:18:05 +01001794 INIT_TYPE(&PyMethod_Type, "method");
1795 INIT_TYPE(&PyFunction_Type, "function");
1796 INIT_TYPE(&PyDictProxy_Type, "dict proxy");
1797 INIT_TYPE(&PyGen_Type, "generator");
1798 INIT_TYPE(&PyGetSetDescr_Type, "get-set descriptor");
1799 INIT_TYPE(&PyWrapperDescr_Type, "wrapper");
1800 INIT_TYPE(&_PyMethodWrapper_Type, "method wrapper");
1801 INIT_TYPE(&PyEllipsis_Type, "ellipsis");
1802 INIT_TYPE(&PyMemberDescr_Type, "member descriptor");
1803 INIT_TYPE(&_PyNamespace_Type, "namespace");
1804 INIT_TYPE(&PyCapsule_Type, "capsule");
1805 INIT_TYPE(&PyLongRangeIter_Type, "long range iterator");
1806 INIT_TYPE(&PyCell_Type, "cell");
1807 INIT_TYPE(&PyInstanceMethod_Type, "instance method");
1808 INIT_TYPE(&PyClassMethodDescr_Type, "class method descr");
1809 INIT_TYPE(&PyMethodDescr_Type, "method descr");
1810 INIT_TYPE(&PyCallIter_Type, "call iter");
1811 INIT_TYPE(&PySeqIter_Type, "sequence iterator");
Antoine Pitrou91f43802019-05-26 17:10:09 +02001812 INIT_TYPE(&PyPickleBuffer_Type, "pickle.PickleBuffer");
Victor Stinner6d43f6f2019-01-22 21:18:05 +01001813 INIT_TYPE(&PyCoro_Type, "coroutine");
1814 INIT_TYPE(&_PyCoroWrapper_Type, "coroutine wrapper");
Eric Snowc11183c2019-03-15 16:35:46 -06001815 INIT_TYPE(&_PyInterpreterID_Type, "interpreter ID");
Victor Stinner331a6a52019-05-27 16:39:22 +02001816 return _PyStatus_OK();
Guido van Rossumba21a492001-08-16 08:17:26 +00001817
Victor Stinner6d43f6f2019-01-22 21:18:05 +01001818#undef INIT_TYPE
Guido van Rossumba21a492001-08-16 08:17:26 +00001819}
1820
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001821
Victor Stinner40e547d2020-02-05 01:11:10 +01001822void
1823_Py_NewReference(PyObject *op)
1824{
1825 if (_Py_tracemalloc_config.tracing) {
1826 _PyTraceMalloc_NewReference(op);
1827 }
1828#ifdef Py_REF_DEBUG
1829 _Py_RefTotal++;
1830#endif
Victor Stinnerc86a1122020-02-07 01:24:29 +01001831 Py_SET_REFCNT(op, 1);
Victor Stinner40e547d2020-02-05 01:11:10 +01001832#ifdef Py_TRACE_REFS
1833 _Py_AddToAllObjects(op, 1);
1834#endif
1835}
1836
1837
Guido van Rossum84a90321996-05-22 16:34:47 +00001838#ifdef Py_TRACE_REFS
Guido van Rossumaacdc9d1996-08-12 21:32:12 +00001839void
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02001840_Py_ForgetReference(PyObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001841{
Victor Stinnera93c51e2020-02-07 00:38:59 +01001842 if (Py_REFCNT(op) < 0) {
Victor Stinner5eb8bff2020-01-30 09:01:07 +01001843 _PyObject_ASSERT_FAILED_MSG(op, "negative refcnt");
1844 }
1845
1846 if (op == &refchain ||
1847 op->_ob_prev->_ob_next != op || op->_ob_next->_ob_prev != op)
1848 {
1849 _PyObject_ASSERT_FAILED_MSG(op, "invalid object chain");
1850 }
1851
Guido van Rossumbffd6832000-01-20 22:32:56 +00001852#ifdef SLOW_UNREF_CHECK
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02001853 PyObject *p;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001854 for (p = refchain._ob_next; p != &refchain; p = p->_ob_next) {
Victor Stinner5eb8bff2020-01-30 09:01:07 +01001855 if (p == op) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001856 break;
Victor Stinner5eb8bff2020-01-30 09:01:07 +01001857 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001858 }
Victor Stinner5eb8bff2020-01-30 09:01:07 +01001859 if (p == &refchain) {
1860 /* Not found */
1861 _PyObject_ASSERT_FAILED_MSG(op,
1862 "object not found in the objects list");
1863 }
Guido van Rossum2e8f6141992-09-03 20:32:55 +00001864#endif
Victor Stinner5eb8bff2020-01-30 09:01:07 +01001865
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001866 op->_ob_next->_ob_prev = op->_ob_prev;
1867 op->_ob_prev->_ob_next = op->_ob_next;
1868 op->_ob_next = op->_ob_prev = NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001869}
1870
Tim Peters269b2a62003-04-17 19:52:29 +00001871/* Print all live objects. Because PyObject_Print is called, the
1872 * interpreter must be in a healthy state.
1873 */
Guido van Rossumaacdc9d1996-08-12 21:32:12 +00001874void
Fred Drake100814d2000-07-09 15:48:49 +00001875_Py_PrintReferences(FILE *fp)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001876{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001877 PyObject *op;
1878 fprintf(fp, "Remaining objects:\n");
1879 for (op = refchain._ob_next; op != &refchain; op = op->_ob_next) {
Victor Stinnerd36cf5f2020-06-10 18:38:05 +02001880 fprintf(fp, "%p [%zd] ", (void *)op, Py_REFCNT(op));
1881 if (PyObject_Print(op, fp, 0) != 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001882 PyErr_Clear();
Victor Stinnerd36cf5f2020-06-10 18:38:05 +02001883 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001884 putc('\n', fp);
1885 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001886}
1887
Tim Peters269b2a62003-04-17 19:52:29 +00001888/* Print the addresses of all live objects. Unlike _Py_PrintReferences, this
1889 * doesn't make any calls to the Python C API, so is always safe to call.
1890 */
1891void
1892_Py_PrintReferenceAddresses(FILE *fp)
1893{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001894 PyObject *op;
1895 fprintf(fp, "Remaining object addresses:\n");
1896 for (op = refchain._ob_next; op != &refchain; op = op->_ob_next)
Victor Stinnerd36cf5f2020-06-10 18:38:05 +02001897 fprintf(fp, "%p [%zd] %s\n", (void *)op,
Victor Stinnera93c51e2020-02-07 00:38:59 +01001898 Py_REFCNT(op), Py_TYPE(op)->tp_name);
Tim Peters269b2a62003-04-17 19:52:29 +00001899}
1900
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +00001901PyObject *
Fred Drake100814d2000-07-09 15:48:49 +00001902_Py_GetObjects(PyObject *self, PyObject *args)
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +00001903{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001904 int i, n;
1905 PyObject *t = NULL;
1906 PyObject *res, *op;
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +00001907
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001908 if (!PyArg_ParseTuple(args, "i|O", &n, &t))
1909 return NULL;
1910 op = refchain._ob_next;
1911 res = PyList_New(0);
1912 if (res == NULL)
1913 return NULL;
1914 for (i = 0; (n == 0 || i < n) && op != &refchain; i++) {
1915 while (op == self || op == args || op == res || op == t ||
Andy Lester55728702020-03-06 16:53:17 -06001916 (t != NULL && !Py_IS_TYPE(op, (PyTypeObject *) t))) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001917 op = op->_ob_next;
1918 if (op == &refchain)
1919 return res;
1920 }
1921 if (PyList_Append(res, op) < 0) {
1922 Py_DECREF(res);
1923 return NULL;
1924 }
1925 op = op->_ob_next;
1926 }
1927 return res;
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +00001928}
1929
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001930#endif
Guido van Rossum97ead3f1996-01-12 01:24:09 +00001931
Benjamin Petersonb173f782009-05-05 22:31:58 +00001932
Guido van Rossum84a90321996-05-22 16:34:47 +00001933/* Hack to force loading of abstract.o */
Martin v. Löwis18e16552006-02-15 17:27:45 +00001934Py_ssize_t (*_Py_abstract_hack)(PyObject *) = PyObject_Size;
Guido van Rossume09fb551997-08-05 02:04:34 +00001935
1936
David Malcolm49526f42012-06-22 14:55:41 -04001937void
1938_PyObject_DebugTypeStats(FILE *out)
1939{
David Malcolm49526f42012-06-22 14:55:41 -04001940 _PyDict_DebugMallocStats(out);
1941 _PyFloat_DebugMallocStats(out);
1942 _PyFrame_DebugMallocStats(out);
1943 _PyList_DebugMallocStats(out);
David Malcolm49526f42012-06-22 14:55:41 -04001944 _PyTuple_DebugMallocStats(out);
1945}
Guido van Rossumb18618d2000-05-03 23:44:39 +00001946
Guido van Rossum86610361998-04-10 22:32:46 +00001947/* These methods are used to control infinite recursion in repr, str, print,
1948 etc. Container objects that may recursively contain themselves,
Martin Panter8d56c022016-05-29 04:13:35 +00001949 e.g. builtin dictionaries and lists, should use Py_ReprEnter() and
Guido van Rossum86610361998-04-10 22:32:46 +00001950 Py_ReprLeave() to avoid infinite recursion.
1951
1952 Py_ReprEnter() returns 0 the first time it is called for a particular
1953 object and 1 every time thereafter. It returns -1 if an exception
1954 occurred. Py_ReprLeave() has no return value.
1955
1956 See dictobject.c and listobject.c for examples of use.
1957*/
1958
Guido van Rossum86610361998-04-10 22:32:46 +00001959int
Fred Drake100814d2000-07-09 15:48:49 +00001960Py_ReprEnter(PyObject *obj)
Guido van Rossum86610361998-04-10 22:32:46 +00001961{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001962 PyObject *dict;
1963 PyObject *list;
1964 Py_ssize_t i;
Guido van Rossum86610361998-04-10 22:32:46 +00001965
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001966 dict = PyThreadState_GetDict();
Antoine Pitrou04d17d32014-03-31 22:04:38 +02001967 /* Ignore a missing thread-state, so that this function can be called
1968 early on startup. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001969 if (dict == NULL)
1970 return 0;
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02001971 list = _PyDict_GetItemIdWithError(dict, &PyId_Py_Repr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001972 if (list == NULL) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02001973 if (PyErr_Occurred()) {
1974 return -1;
1975 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001976 list = PyList_New(0);
1977 if (list == NULL)
1978 return -1;
Victor Stinner7a07e452013-11-06 18:57:29 +01001979 if (_PyDict_SetItemId(dict, &PyId_Py_Repr, list) < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001980 return -1;
1981 Py_DECREF(list);
1982 }
1983 i = PyList_GET_SIZE(list);
1984 while (--i >= 0) {
1985 if (PyList_GET_ITEM(list, i) == obj)
1986 return 1;
1987 }
Victor Stinnere901d1f2013-07-17 21:58:41 +02001988 if (PyList_Append(list, obj) < 0)
1989 return -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001990 return 0;
Guido van Rossum86610361998-04-10 22:32:46 +00001991}
1992
1993void
Fred Drake100814d2000-07-09 15:48:49 +00001994Py_ReprLeave(PyObject *obj)
Guido van Rossum86610361998-04-10 22:32:46 +00001995{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001996 PyObject *dict;
1997 PyObject *list;
1998 Py_ssize_t i;
Victor Stinner1b634932013-07-16 22:24:44 +02001999 PyObject *error_type, *error_value, *error_traceback;
2000
2001 PyErr_Fetch(&error_type, &error_value, &error_traceback);
Guido van Rossum86610361998-04-10 22:32:46 +00002002
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002003 dict = PyThreadState_GetDict();
2004 if (dict == NULL)
Victor Stinner1b634932013-07-16 22:24:44 +02002005 goto finally;
2006
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002007 list = _PyDict_GetItemIdWithError(dict, &PyId_Py_Repr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002008 if (list == NULL || !PyList_Check(list))
Victor Stinner1b634932013-07-16 22:24:44 +02002009 goto finally;
2010
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002011 i = PyList_GET_SIZE(list);
2012 /* Count backwards because we always expect obj to be list[-1] */
2013 while (--i >= 0) {
2014 if (PyList_GET_ITEM(list, i) == obj) {
2015 PyList_SetSlice(list, i, i + 1, NULL);
2016 break;
2017 }
2018 }
Victor Stinner1b634932013-07-16 22:24:44 +02002019
2020finally:
2021 /* ignore exceptions because there is no way to report them. */
2022 PyErr_Restore(error_type, error_value, error_traceback);
Guido van Rossum86610361998-04-10 22:32:46 +00002023}
Guido van Rossumd724b232000-03-13 16:01:29 +00002024
Tim Peters803526b2002-07-07 05:13:56 +00002025/* Trashcan support. */
Guido van Rossumd724b232000-03-13 16:01:29 +00002026
Tim Peters803526b2002-07-07 05:13:56 +00002027/* Add op to the _PyTrash_delete_later list. Called when the current
2028 * call-stack depth gets large. op must be a currently untracked gc'ed
2029 * object, with refcount 0. Py_DECREF must already have been called on it.
2030 */
Guido van Rossumd724b232000-03-13 16:01:29 +00002031void
Fred Drake100814d2000-07-09 15:48:49 +00002032_PyTrash_deposit_object(PyObject *op)
Guido van Rossumd724b232000-03-13 16:01:29 +00002033{
Victor Stinner1bcc32f2020-06-10 20:08:26 +02002034 PyInterpreterState *interp = _PyInterpreterState_GET();
2035 struct _gc_runtime_state *gcstate = &interp->gc;
Victor Stinner72474072019-11-20 12:25:50 +01002036
Hai Shi675d9a32020-04-15 02:11:20 +08002037 _PyObject_ASSERT(op, _PyObject_IS_GC(op));
Victor Stinner24702042018-10-26 17:16:37 +02002038 _PyObject_ASSERT(op, !_PyObject_GC_IS_TRACKED(op));
Victor Stinnera93c51e2020-02-07 00:38:59 +01002039 _PyObject_ASSERT(op, Py_REFCNT(op) == 0);
Victor Stinner72474072019-11-20 12:25:50 +01002040 _PyGCHead_SET_PREV(_Py_AS_GC(op), gcstate->trash_delete_later);
2041 gcstate->trash_delete_later = op;
Guido van Rossumd724b232000-03-13 16:01:29 +00002042}
2043
Antoine Pitrou2b0218a2012-09-06 00:59:49 +02002044/* The equivalent API, using per-thread state recursion info */
2045void
2046_PyTrash_thread_deposit_object(PyObject *op)
2047{
Victor Stinner50b48572018-11-01 01:51:40 +01002048 PyThreadState *tstate = _PyThreadState_GET();
Hai Shi675d9a32020-04-15 02:11:20 +08002049 _PyObject_ASSERT(op, _PyObject_IS_GC(op));
Victor Stinner24702042018-10-26 17:16:37 +02002050 _PyObject_ASSERT(op, !_PyObject_GC_IS_TRACKED(op));
Victor Stinnera93c51e2020-02-07 00:38:59 +01002051 _PyObject_ASSERT(op, Py_REFCNT(op) == 0);
INADA Naoki5ac9e6e2018-07-10 17:19:53 +09002052 _PyGCHead_SET_PREV(_Py_AS_GC(op), tstate->trash_delete_later);
Antoine Pitrou2b0218a2012-09-06 00:59:49 +02002053 tstate->trash_delete_later = op;
2054}
2055
Min ho Kimc4cacc82019-07-31 08:16:13 +10002056/* Deallocate all the objects in the _PyTrash_delete_later list. Called when
Tim Peters803526b2002-07-07 05:13:56 +00002057 * the call-stack unwinds again.
2058 */
Guido van Rossumd724b232000-03-13 16:01:29 +00002059void
Fred Drake100814d2000-07-09 15:48:49 +00002060_PyTrash_destroy_chain(void)
Guido van Rossumd724b232000-03-13 16:01:29 +00002061{
Victor Stinner1bcc32f2020-06-10 20:08:26 +02002062 PyInterpreterState *interp = _PyInterpreterState_GET();
2063 struct _gc_runtime_state *gcstate = &interp->gc;
Victor Stinner72474072019-11-20 12:25:50 +01002064
2065 while (gcstate->trash_delete_later) {
2066 PyObject *op = gcstate->trash_delete_later;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002067 destructor dealloc = Py_TYPE(op)->tp_dealloc;
Neil Schemenauerf589c052002-03-29 03:05:54 +00002068
Victor Stinner72474072019-11-20 12:25:50 +01002069 gcstate->trash_delete_later =
INADA Naoki5ac9e6e2018-07-10 17:19:53 +09002070 (PyObject*) _PyGCHead_PREV(_Py_AS_GC(op));
Neil Schemenauerf589c052002-03-29 03:05:54 +00002071
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002072 /* Call the deallocator directly. This used to try to
2073 * fool Py_DECREF into calling it indirectly, but
2074 * Py_DECREF was already called on this object, and in
2075 * assorted non-release builds calling Py_DECREF again ends
2076 * up distorting allocation statistics.
2077 */
Victor Stinnera93c51e2020-02-07 00:38:59 +01002078 _PyObject_ASSERT(op, Py_REFCNT(op) == 0);
Victor Stinner72474072019-11-20 12:25:50 +01002079 ++gcstate->trash_delete_nesting;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002080 (*dealloc)(op);
Victor Stinner72474072019-11-20 12:25:50 +01002081 --gcstate->trash_delete_nesting;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002082 }
Guido van Rossumd724b232000-03-13 16:01:29 +00002083}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002084
Antoine Pitrou2b0218a2012-09-06 00:59:49 +02002085/* The equivalent API, using per-thread state recursion info */
2086void
2087_PyTrash_thread_destroy_chain(void)
2088{
Victor Stinner50b48572018-11-01 01:51:40 +01002089 PyThreadState *tstate = _PyThreadState_GET();
Xiang Zhanga66f9c62017-05-13 13:36:14 +08002090 /* We need to increase trash_delete_nesting here, otherwise,
2091 _PyTrash_thread_destroy_chain will be called recursively
2092 and then possibly crash. An example that may crash without
2093 increase:
2094 N = 500000 # need to be large enough
2095 ob = object()
2096 tups = [(ob,) for i in range(N)]
2097 for i in range(49):
2098 tups = [(tup,) for tup in tups]
2099 del tups
2100 */
2101 assert(tstate->trash_delete_nesting == 0);
2102 ++tstate->trash_delete_nesting;
Antoine Pitrou2b0218a2012-09-06 00:59:49 +02002103 while (tstate->trash_delete_later) {
2104 PyObject *op = tstate->trash_delete_later;
2105 destructor dealloc = Py_TYPE(op)->tp_dealloc;
2106
2107 tstate->trash_delete_later =
INADA Naoki5ac9e6e2018-07-10 17:19:53 +09002108 (PyObject*) _PyGCHead_PREV(_Py_AS_GC(op));
Antoine Pitrou2b0218a2012-09-06 00:59:49 +02002109
2110 /* Call the deallocator directly. This used to try to
2111 * fool Py_DECREF into calling it indirectly, but
2112 * Py_DECREF was already called on this object, and in
2113 * assorted non-release builds calling Py_DECREF again ends
2114 * up distorting allocation statistics.
2115 */
Victor Stinnera93c51e2020-02-07 00:38:59 +01002116 _PyObject_ASSERT(op, Py_REFCNT(op) == 0);
Antoine Pitrou2b0218a2012-09-06 00:59:49 +02002117 (*dealloc)(op);
Xiang Zhanga66f9c62017-05-13 13:36:14 +08002118 assert(tstate->trash_delete_nesting == 1);
Antoine Pitrou2b0218a2012-09-06 00:59:49 +02002119 }
Xiang Zhanga66f9c62017-05-13 13:36:14 +08002120 --tstate->trash_delete_nesting;
Antoine Pitrou2b0218a2012-09-06 00:59:49 +02002121}
2122
Victor Stinner626bff82018-10-25 17:31:10 +02002123
Victor Stinner38965ec2020-03-13 16:51:52 +01002124int
2125_PyTrash_begin(PyThreadState *tstate, PyObject *op)
2126{
2127 if (tstate->trash_delete_nesting >= PyTrash_UNWIND_LEVEL) {
2128 /* Store the object (to be deallocated later) and jump past
2129 * Py_TRASHCAN_END, skipping the body of the deallocator */
2130 _PyTrash_thread_deposit_object(op);
2131 return 1;
2132 }
2133 ++tstate->trash_delete_nesting;
2134 return 0;
2135}
2136
2137
2138void
2139_PyTrash_end(PyThreadState *tstate)
2140{
2141 --tstate->trash_delete_nesting;
2142 if (tstate->trash_delete_later && tstate->trash_delete_nesting <= 0) {
2143 _PyTrash_thread_destroy_chain();
2144 }
2145}
2146
2147
Victor Stinner2a4903f2020-01-30 13:09:11 +01002148void _Py_NO_RETURN
Victor Stinnerf1d002c2018-11-21 23:53:44 +01002149_PyObject_AssertFailed(PyObject *obj, const char *expr, const char *msg,
Victor Stinner626bff82018-10-25 17:31:10 +02002150 const char *file, int line, const char *function)
2151{
Victor Stinnerf1d002c2018-11-21 23:53:44 +01002152 fprintf(stderr, "%s:%d: ", file, line);
2153 if (function) {
2154 fprintf(stderr, "%s: ", function);
2155 }
Victor Stinner626bff82018-10-25 17:31:10 +02002156 fflush(stderr);
Victor Stinner68762572019-10-07 18:42:01 +02002157
Victor Stinnerf1d002c2018-11-21 23:53:44 +01002158 if (expr) {
2159 fprintf(stderr, "Assertion \"%s\" failed", expr);
Victor Stinner626bff82018-10-25 17:31:10 +02002160 }
2161 else {
Victor Stinnerf1d002c2018-11-21 23:53:44 +01002162 fprintf(stderr, "Assertion failed");
Victor Stinner626bff82018-10-25 17:31:10 +02002163 }
2164 fflush(stderr);
Victor Stinner68762572019-10-07 18:42:01 +02002165
Victor Stinnerf1d002c2018-11-21 23:53:44 +01002166 if (msg) {
2167 fprintf(stderr, ": %s", msg);
2168 }
2169 fprintf(stderr, "\n");
2170 fflush(stderr);
Victor Stinner626bff82018-10-25 17:31:10 +02002171
Victor Stinner68762572019-10-07 18:42:01 +02002172 if (_PyObject_IsFreed(obj)) {
Victor Stinner626bff82018-10-25 17:31:10 +02002173 /* It seems like the object memory has been freed:
2174 don't access it to prevent a segmentation fault. */
Victor Stinnerb39afb72019-09-17 23:36:28 +02002175 fprintf(stderr, "<object at %p is freed>\n", obj);
Victor Stinner68762572019-10-07 18:42:01 +02002176 fflush(stderr);
Victor Stinner626bff82018-10-25 17:31:10 +02002177 }
2178 else {
penguindustin96466302019-05-06 14:57:17 -04002179 /* Display the traceback where the object has been allocated.
Victor Stinner626bff82018-10-25 17:31:10 +02002180 Do it before dumping repr(obj), since repr() is more likely
2181 to crash than dumping the traceback. */
2182 void *ptr;
2183 PyTypeObject *type = Py_TYPE(obj);
Victor Stinner45ec5b92020-04-08 01:42:27 +02002184 if (_PyType_IS_GC(type)) {
Victor Stinner626bff82018-10-25 17:31:10 +02002185 ptr = (void *)((char *)obj - sizeof(PyGC_Head));
2186 }
2187 else {
2188 ptr = (void *)obj;
2189 }
2190 _PyMem_DumpTraceback(fileno(stderr), ptr);
2191
2192 /* This might succeed or fail, but we're about to abort, so at least
2193 try to provide any extra info we can: */
2194 _PyObject_Dump(obj);
Victor Stinner77753492019-10-07 23:44:05 +02002195
2196 fprintf(stderr, "\n");
2197 fflush(stderr);
Victor Stinner626bff82018-10-25 17:31:10 +02002198 }
Victor Stinner626bff82018-10-25 17:31:10 +02002199
2200 Py_FatalError("_PyObject_AssertFailed");
2201}
2202
Victor Stinner3c09dca2018-10-30 14:48:26 +01002203
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00002204void
2205_Py_Dealloc(PyObject *op)
2206{
Victor Stinner3c09dca2018-10-30 14:48:26 +01002207 destructor dealloc = Py_TYPE(op)->tp_dealloc;
2208#ifdef Py_TRACE_REFS
2209 _Py_ForgetReference(op);
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00002210#endif
Victor Stinner3c09dca2018-10-30 14:48:26 +01002211 (*dealloc)(op);
2212}
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00002213
Victor Stinner38aefc52020-04-06 14:07:02 +02002214
2215PyObject **
2216PyObject_GET_WEAKREFS_LISTPTR(PyObject *op)
2217{
2218 return _PyObject_GET_WEAKREFS_LISTPTR(op);
2219}
2220
2221
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002222#ifdef __cplusplus
2223}
2224#endif