blob: 0a8621b3503b31eef8c0e4e411267153fa72ee95 [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 Stinner32bd68c2020-12-01 10:37:39 +0100164 PyObject *op = (PyObject *) PyObject_Malloc(_PyObject_SIZE(tp));
Victor Stinner92055202020-04-08 00:38:15 +0200165 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);
Victor Stinner32bd68c2020-12-01 10:37:39 +0100177 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
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200858_PyObject_SetAttrId(PyObject *v, _Py_Identifier *name, PyObject *w)
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_SetAttr(v, oname, w);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200865 return result;
866}
867
868PyObject *
Fred Drake100814d2000-07-09 15:48:49 +0000869PyObject_GetAttr(PyObject *v, PyObject *name)
Guido van Rossum98ff96a1997-05-20 18:34:44 +0000870{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000871 PyTypeObject *tp = Py_TYPE(v);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000872
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000873 if (!PyUnicode_Check(name)) {
874 PyErr_Format(PyExc_TypeError,
875 "attribute name must be string, not '%.200s'",
Victor Stinner58ac7002020-02-07 03:04:21 +0100876 Py_TYPE(name)->tp_name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000877 return NULL;
878 }
879 if (tp->tp_getattro != NULL)
880 return (*tp->tp_getattro)(v, name);
881 if (tp->tp_getattr != NULL) {
Serhiy Storchaka2a404b62017-01-22 23:07:07 +0200882 const char *name_str = PyUnicode_AsUTF8(name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000883 if (name_str == NULL)
884 return NULL;
Serhiy Storchaka2a404b62017-01-22 23:07:07 +0200885 return (*tp->tp_getattr)(v, (char *)name_str);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000886 }
887 PyErr_Format(PyExc_AttributeError,
888 "'%.50s' object has no attribute '%U'",
889 tp->tp_name, name);
890 return NULL;
Guido van Rossum98ff96a1997-05-20 18:34:44 +0000891}
892
Serhiy Storchakaf320be72018-01-25 10:49:40 +0200893int
894_PyObject_LookupAttr(PyObject *v, PyObject *name, PyObject **result)
INADA Naoki378edee2018-01-16 20:52:41 +0900895{
896 PyTypeObject *tp = Py_TYPE(v);
INADA Naoki378edee2018-01-16 20:52:41 +0900897
898 if (!PyUnicode_Check(name)) {
899 PyErr_Format(PyExc_TypeError,
900 "attribute name must be string, not '%.200s'",
Victor Stinner58ac7002020-02-07 03:04:21 +0100901 Py_TYPE(name)->tp_name);
Serhiy Storchakaf320be72018-01-25 10:49:40 +0200902 *result = NULL;
903 return -1;
INADA Naoki378edee2018-01-16 20:52:41 +0900904 }
905
906 if (tp->tp_getattro == PyObject_GenericGetAttr) {
Serhiy Storchakaf320be72018-01-25 10:49:40 +0200907 *result = _PyObject_GenericGetAttrWithDict(v, name, NULL, 1);
908 if (*result != NULL) {
909 return 1;
910 }
911 if (PyErr_Occurred()) {
912 return -1;
913 }
914 return 0;
INADA Naoki378edee2018-01-16 20:52:41 +0900915 }
916 if (tp->tp_getattro != NULL) {
Serhiy Storchakaf320be72018-01-25 10:49:40 +0200917 *result = (*tp->tp_getattro)(v, name);
INADA Naoki378edee2018-01-16 20:52:41 +0900918 }
919 else if (tp->tp_getattr != NULL) {
920 const char *name_str = PyUnicode_AsUTF8(name);
Serhiy Storchakaf320be72018-01-25 10:49:40 +0200921 if (name_str == NULL) {
922 *result = NULL;
923 return -1;
924 }
925 *result = (*tp->tp_getattr)(v, (char *)name_str);
INADA Naoki378edee2018-01-16 20:52:41 +0900926 }
INADA Naokie76daeb2018-01-26 16:22:51 +0900927 else {
928 *result = NULL;
929 return 0;
930 }
931
Serhiy Storchakaf320be72018-01-25 10:49:40 +0200932 if (*result != NULL) {
933 return 1;
INADA Naoki378edee2018-01-16 20:52:41 +0900934 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +0200935 if (!PyErr_ExceptionMatches(PyExc_AttributeError)) {
936 return -1;
937 }
938 PyErr_Clear();
939 return 0;
940}
941
942int
943_PyObject_LookupAttrId(PyObject *v, _Py_Identifier *name, PyObject **result)
944{
945 PyObject *oname = _PyUnicode_FromId(name); /* borrowed */
946 if (!oname) {
947 *result = NULL;
948 return -1;
949 }
950 return _PyObject_LookupAttr(v, oname, result);
INADA Naoki378edee2018-01-16 20:52:41 +0900951}
952
Guido van Rossum98ff96a1997-05-20 18:34:44 +0000953int
Fred Drake100814d2000-07-09 15:48:49 +0000954PyObject_HasAttr(PyObject *v, PyObject *name)
Guido van Rossum98ff96a1997-05-20 18:34:44 +0000955{
Serhiy Storchakaf320be72018-01-25 10:49:40 +0200956 PyObject *res;
957 if (_PyObject_LookupAttr(v, name, &res) < 0) {
958 PyErr_Clear();
959 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000960 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +0200961 if (res == NULL) {
962 return 0;
963 }
964 Py_DECREF(res);
965 return 1;
Guido van Rossum98ff96a1997-05-20 18:34:44 +0000966}
967
968int
Fred Drake100814d2000-07-09 15:48:49 +0000969PyObject_SetAttr(PyObject *v, PyObject *name, PyObject *value)
Guido van Rossum98ff96a1997-05-20 18:34:44 +0000970{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000971 PyTypeObject *tp = Py_TYPE(v);
972 int err;
Marc-André Lemburge44e5072000-09-18 16:20:57 +0000973
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000974 if (!PyUnicode_Check(name)) {
975 PyErr_Format(PyExc_TypeError,
976 "attribute name must be string, not '%.200s'",
Victor Stinner58ac7002020-02-07 03:04:21 +0100977 Py_TYPE(name)->tp_name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000978 return -1;
979 }
980 Py_INCREF(name);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000981
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000982 PyUnicode_InternInPlace(&name);
983 if (tp->tp_setattro != NULL) {
984 err = (*tp->tp_setattro)(v, name, value);
985 Py_DECREF(name);
986 return err;
987 }
988 if (tp->tp_setattr != NULL) {
Serhiy Storchaka2a404b62017-01-22 23:07:07 +0200989 const char *name_str = PyUnicode_AsUTF8(name);
Zackery Spytze0dcb852019-04-28 06:58:52 -0600990 if (name_str == NULL) {
991 Py_DECREF(name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000992 return -1;
Zackery Spytze0dcb852019-04-28 06:58:52 -0600993 }
Serhiy Storchaka2a404b62017-01-22 23:07:07 +0200994 err = (*tp->tp_setattr)(v, (char *)name_str, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000995 Py_DECREF(name);
996 return err;
997 }
998 Py_DECREF(name);
Victor Stinnera93c51e2020-02-07 00:38:59 +0100999 _PyObject_ASSERT(name, Py_REFCNT(name) >= 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001000 if (tp->tp_getattr == NULL && tp->tp_getattro == NULL)
1001 PyErr_Format(PyExc_TypeError,
1002 "'%.100s' object has no attributes "
1003 "(%s .%U)",
1004 tp->tp_name,
1005 value==NULL ? "del" : "assign to",
1006 name);
1007 else
1008 PyErr_Format(PyExc_TypeError,
1009 "'%.100s' object has only read-only attributes "
1010 "(%s .%U)",
1011 tp->tp_name,
1012 value==NULL ? "del" : "assign to",
1013 name);
1014 return -1;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001015}
1016
1017/* Helper to get a pointer to an object's __dict__ slot, if any */
1018
1019PyObject **
1020_PyObject_GetDictPtr(PyObject *obj)
1021{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001022 Py_ssize_t dictoffset;
1023 PyTypeObject *tp = Py_TYPE(obj);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001024
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001025 dictoffset = tp->tp_dictoffset;
1026 if (dictoffset == 0)
1027 return NULL;
1028 if (dictoffset < 0) {
Victor Stinnerc65b3202020-02-07 11:18:33 +01001029 Py_ssize_t tsize = Py_SIZE(obj);
1030 if (tsize < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001031 tsize = -tsize;
Victor Stinnerc65b3202020-02-07 11:18:33 +01001032 }
1033 size_t size = _PyObject_VAR_SIZE(tp, tsize);
Guido van Rossum2eb0b872002-03-01 22:24:49 +00001034
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001035 dictoffset += (long)size;
Victor Stinner24702042018-10-26 17:16:37 +02001036 _PyObject_ASSERT(obj, dictoffset > 0);
1037 _PyObject_ASSERT(obj, dictoffset % SIZEOF_VOID_P == 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001038 }
1039 return (PyObject **) ((char *)obj + dictoffset);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001040}
1041
Tim Peters6d6c1a32001-08-02 04:15:00 +00001042PyObject *
Raymond Hettinger1da1dbf2003-03-17 19:46:11 +00001043PyObject_SelfIter(PyObject *obj)
Raymond Hettinger01538262003-03-17 08:24:35 +00001044{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001045 Py_INCREF(obj);
1046 return obj;
Raymond Hettinger01538262003-03-17 08:24:35 +00001047}
1048
Amaury Forgeot d'Arcf343e012009-01-12 23:58:21 +00001049/* Helper used when the __next__ method is removed from a type:
1050 tp_iternext is never NULL and can be safely called without checking
1051 on every iteration.
1052 */
1053
1054PyObject *
1055_PyObject_NextNotImplemented(PyObject *self)
1056{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001057 PyErr_Format(PyExc_TypeError,
1058 "'%.200s' object is not iterable",
1059 Py_TYPE(self)->tp_name);
1060 return NULL;
Amaury Forgeot d'Arcf343e012009-01-12 23:58:21 +00001061}
1062
Yury Selivanovf2392132016-12-13 19:03:51 -05001063
1064/* Specialized version of _PyObject_GenericGetAttrWithDict
1065 specifically for the LOAD_METHOD opcode.
1066
1067 Return 1 if a method is found, 0 if it's a regular attribute
1068 from __dict__ or something returned by using a descriptor
1069 protocol.
1070
1071 `method` will point to the resolved attribute or NULL. In the
1072 latter case, an error will be set.
1073*/
1074int
1075_PyObject_GetMethod(PyObject *obj, PyObject *name, PyObject **method)
1076{
1077 PyTypeObject *tp = Py_TYPE(obj);
1078 PyObject *descr;
1079 descrgetfunc f = NULL;
1080 PyObject **dictptr, *dict;
1081 PyObject *attr;
1082 int meth_found = 0;
1083
1084 assert(*method == NULL);
1085
1086 if (Py_TYPE(obj)->tp_getattro != PyObject_GenericGetAttr
1087 || !PyUnicode_Check(name)) {
1088 *method = PyObject_GetAttr(obj, name);
1089 return 0;
1090 }
1091
1092 if (tp->tp_dict == NULL && PyType_Ready(tp) < 0)
1093 return 0;
1094
1095 descr = _PyType_Lookup(tp, name);
1096 if (descr != NULL) {
1097 Py_INCREF(descr);
Victor Stinner45ec5b92020-04-08 01:42:27 +02001098 if (_PyType_HasFeature(Py_TYPE(descr), Py_TPFLAGS_METHOD_DESCRIPTOR)) {
Yury Selivanovf2392132016-12-13 19:03:51 -05001099 meth_found = 1;
1100 } else {
Victor Stinner58ac7002020-02-07 03:04:21 +01001101 f = Py_TYPE(descr)->tp_descr_get;
Yury Selivanovf2392132016-12-13 19:03:51 -05001102 if (f != NULL && PyDescr_IsData(descr)) {
Victor Stinner58ac7002020-02-07 03:04:21 +01001103 *method = f(descr, obj, (PyObject *)Py_TYPE(obj));
Yury Selivanovf2392132016-12-13 19:03:51 -05001104 Py_DECREF(descr);
1105 return 0;
1106 }
1107 }
1108 }
1109
1110 dictptr = _PyObject_GetDictPtr(obj);
1111 if (dictptr != NULL && (dict = *dictptr) != NULL) {
1112 Py_INCREF(dict);
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02001113 attr = PyDict_GetItemWithError(dict, name);
Yury Selivanovf2392132016-12-13 19:03:51 -05001114 if (attr != NULL) {
1115 Py_INCREF(attr);
1116 *method = attr;
1117 Py_DECREF(dict);
1118 Py_XDECREF(descr);
1119 return 0;
1120 }
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02001121 else {
1122 Py_DECREF(dict);
1123 if (PyErr_Occurred()) {
1124 Py_XDECREF(descr);
1125 return 0;
1126 }
1127 }
Yury Selivanovf2392132016-12-13 19:03:51 -05001128 }
1129
1130 if (meth_found) {
1131 *method = descr;
1132 return 1;
1133 }
1134
1135 if (f != NULL) {
1136 *method = f(descr, obj, (PyObject *)Py_TYPE(obj));
1137 Py_DECREF(descr);
1138 return 0;
1139 }
1140
1141 if (descr != NULL) {
1142 *method = descr;
1143 return 0;
1144 }
1145
1146 PyErr_Format(PyExc_AttributeError,
1147 "'%.50s' object has no attribute '%U'",
1148 tp->tp_name, name);
1149 return 0;
1150}
1151
1152/* Generic GetAttr functions - put these in your tp_[gs]etattro slot. */
Michael W. Hudson1593f502004-09-14 17:09:47 +00001153
Raymond Hettinger01538262003-03-17 08:24:35 +00001154PyObject *
INADA Naoki378edee2018-01-16 20:52:41 +09001155_PyObject_GenericGetAttrWithDict(PyObject *obj, PyObject *name,
1156 PyObject *dict, int suppress)
Tim Peters6d6c1a32001-08-02 04:15:00 +00001157{
Yury Selivanovf2392132016-12-13 19:03:51 -05001158 /* Make sure the logic of _PyObject_GetMethod is in sync with
1159 this method.
INADA Naoki378edee2018-01-16 20:52:41 +09001160
Brett Cannond5837382020-07-17 13:09:21 -07001161 When suppress=1, this function suppresses AttributeError.
Yury Selivanovf2392132016-12-13 19:03:51 -05001162 */
1163
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001164 PyTypeObject *tp = Py_TYPE(obj);
1165 PyObject *descr = NULL;
1166 PyObject *res = NULL;
1167 descrgetfunc f;
1168 Py_ssize_t dictoffset;
1169 PyObject **dictptr;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001170
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001171 if (!PyUnicode_Check(name)){
1172 PyErr_Format(PyExc_TypeError,
1173 "attribute name must be string, not '%.200s'",
Victor Stinner58ac7002020-02-07 03:04:21 +01001174 Py_TYPE(name)->tp_name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001175 return NULL;
1176 }
Serhiy Storchaka55c861f2016-04-17 20:31:51 +03001177 Py_INCREF(name);
Guido van Rossumebca9fc2001-12-04 15:54:53 +00001178
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001179 if (tp->tp_dict == NULL) {
1180 if (PyType_Ready(tp) < 0)
1181 goto done;
1182 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001183
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001184 descr = _PyType_Lookup(tp, name);
Michael W. Hudsonb2c7de42003-08-15 13:07:47 +00001185
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001186 f = NULL;
1187 if (descr != NULL) {
Serhiy Storchaka55c861f2016-04-17 20:31:51 +03001188 Py_INCREF(descr);
Victor Stinner58ac7002020-02-07 03:04:21 +01001189 f = Py_TYPE(descr)->tp_descr_get;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001190 if (f != NULL && PyDescr_IsData(descr)) {
Victor Stinner58ac7002020-02-07 03:04:21 +01001191 res = f(descr, obj, (PyObject *)Py_TYPE(obj));
INADA Naoki378edee2018-01-16 20:52:41 +09001192 if (res == NULL && suppress &&
1193 PyErr_ExceptionMatches(PyExc_AttributeError)) {
1194 PyErr_Clear();
1195 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001196 goto done;
1197 }
1198 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001199
Antoine Pitrou1a9a9d52010-08-28 18:17:03 +00001200 if (dict == NULL) {
1201 /* Inline _PyObject_GetDictPtr */
1202 dictoffset = tp->tp_dictoffset;
1203 if (dictoffset != 0) {
1204 if (dictoffset < 0) {
Victor Stinnerc65b3202020-02-07 11:18:33 +01001205 Py_ssize_t tsize = Py_SIZE(obj);
1206 if (tsize < 0) {
Antoine Pitrou1a9a9d52010-08-28 18:17:03 +00001207 tsize = -tsize;
Victor Stinnerc65b3202020-02-07 11:18:33 +01001208 }
1209 size_t size = _PyObject_VAR_SIZE(tp, tsize);
Victor Stinner24702042018-10-26 17:16:37 +02001210 _PyObject_ASSERT(obj, size <= PY_SSIZE_T_MAX);
Guido van Rossumc66ff442002-08-19 16:50:48 +00001211
Serhiy Storchaka55c861f2016-04-17 20:31:51 +03001212 dictoffset += (Py_ssize_t)size;
Victor Stinner24702042018-10-26 17:16:37 +02001213 _PyObject_ASSERT(obj, dictoffset > 0);
1214 _PyObject_ASSERT(obj, dictoffset % SIZEOF_VOID_P == 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001215 }
Antoine Pitrou1a9a9d52010-08-28 18:17:03 +00001216 dictptr = (PyObject **) ((char *)obj + dictoffset);
1217 dict = *dictptr;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001218 }
1219 }
Antoine Pitrou1a9a9d52010-08-28 18:17:03 +00001220 if (dict != NULL) {
1221 Py_INCREF(dict);
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02001222 res = PyDict_GetItemWithError(dict, name);
Antoine Pitrou1a9a9d52010-08-28 18:17:03 +00001223 if (res != NULL) {
1224 Py_INCREF(res);
Antoine Pitrou1a9a9d52010-08-28 18:17:03 +00001225 Py_DECREF(dict);
1226 goto done;
1227 }
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02001228 else {
1229 Py_DECREF(dict);
1230 if (PyErr_Occurred()) {
1231 if (suppress && PyErr_ExceptionMatches(PyExc_AttributeError)) {
1232 PyErr_Clear();
1233 }
1234 else {
1235 goto done;
1236 }
1237 }
1238 }
Antoine Pitrou1a9a9d52010-08-28 18:17:03 +00001239 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001240
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001241 if (f != NULL) {
1242 res = f(descr, obj, (PyObject *)Py_TYPE(obj));
INADA Naoki378edee2018-01-16 20:52:41 +09001243 if (res == NULL && suppress &&
1244 PyErr_ExceptionMatches(PyExc_AttributeError)) {
1245 PyErr_Clear();
1246 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001247 goto done;
1248 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001249
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001250 if (descr != NULL) {
1251 res = descr;
Victor Stinner2d01dc02012-03-09 00:44:13 +01001252 descr = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001253 goto done;
1254 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001255
INADA Naoki378edee2018-01-16 20:52:41 +09001256 if (!suppress) {
1257 PyErr_Format(PyExc_AttributeError,
1258 "'%.50s' object has no attribute '%U'",
1259 tp->tp_name, name);
1260 }
Guido van Rossumebca9fc2001-12-04 15:54:53 +00001261 done:
Victor Stinner2d01dc02012-03-09 00:44:13 +01001262 Py_XDECREF(descr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001263 Py_DECREF(name);
1264 return res;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001265}
1266
Antoine Pitrou1a9a9d52010-08-28 18:17:03 +00001267PyObject *
1268PyObject_GenericGetAttr(PyObject *obj, PyObject *name)
1269{
INADA Naoki378edee2018-01-16 20:52:41 +09001270 return _PyObject_GenericGetAttrWithDict(obj, name, NULL, 0);
Antoine Pitrou1a9a9d52010-08-28 18:17:03 +00001271}
1272
Tim Peters6d6c1a32001-08-02 04:15:00 +00001273int
Antoine Pitrou1a9a9d52010-08-28 18:17:03 +00001274_PyObject_GenericSetAttrWithDict(PyObject *obj, PyObject *name,
1275 PyObject *value, PyObject *dict)
Tim Peters6d6c1a32001-08-02 04:15:00 +00001276{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001277 PyTypeObject *tp = Py_TYPE(obj);
1278 PyObject *descr;
1279 descrsetfunc f;
1280 PyObject **dictptr;
1281 int res = -1;
Guido van Rossumebca9fc2001-12-04 15:54:53 +00001282
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001283 if (!PyUnicode_Check(name)){
1284 PyErr_Format(PyExc_TypeError,
1285 "attribute name must be string, not '%.200s'",
Victor Stinner58ac7002020-02-07 03:04:21 +01001286 Py_TYPE(name)->tp_name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001287 return -1;
1288 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001289
Benjamin Peterson74529ad2012-03-09 07:25:32 -08001290 if (tp->tp_dict == NULL && PyType_Ready(tp) < 0)
1291 return -1;
1292
1293 Py_INCREF(name);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001294
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001295 descr = _PyType_Lookup(tp, name);
Victor Stinner2d01dc02012-03-09 00:44:13 +01001296
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001297 if (descr != NULL) {
Serhiy Storchaka55c861f2016-04-17 20:31:51 +03001298 Py_INCREF(descr);
Victor Stinner58ac7002020-02-07 03:04:21 +01001299 f = Py_TYPE(descr)->tp_descr_set;
Serhiy Storchaka55c861f2016-04-17 20:31:51 +03001300 if (f != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001301 res = f(descr, obj, value);
1302 goto done;
1303 }
1304 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001305
Steve Dowerb82e17e2019-05-23 08:45:22 -07001306 /* XXX [Steve Dower] These are really noisy - worth it? */
1307 /*if (PyType_Check(obj) || PyModule_Check(obj)) {
1308 if (value && PySys_Audit("object.__setattr__", "OOO", obj, name, value) < 0)
1309 return -1;
1310 if (!value && PySys_Audit("object.__delattr__", "OO", obj, name) < 0)
1311 return -1;
1312 }*/
1313
Antoine Pitrou1a9a9d52010-08-28 18:17:03 +00001314 if (dict == NULL) {
1315 dictptr = _PyObject_GetDictPtr(obj);
Serhiy Storchaka55c861f2016-04-17 20:31:51 +03001316 if (dictptr == NULL) {
1317 if (descr == NULL) {
1318 PyErr_Format(PyExc_AttributeError,
1319 "'%.100s' object has no attribute '%U'",
1320 tp->tp_name, name);
1321 }
1322 else {
1323 PyErr_Format(PyExc_AttributeError,
1324 "'%.50s' object attribute '%U' is read-only",
1325 tp->tp_name, name);
1326 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001327 goto done;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001328 }
Serhiy Storchaka55c861f2016-04-17 20:31:51 +03001329 res = _PyObjectDict_SetItem(tp, dictptr, name, value);
Antoine Pitrou1a9a9d52010-08-28 18:17:03 +00001330 }
Serhiy Storchaka55c861f2016-04-17 20:31:51 +03001331 else {
Antoine Pitrou1a9a9d52010-08-28 18:17:03 +00001332 Py_INCREF(dict);
1333 if (value == NULL)
1334 res = PyDict_DelItem(dict, name);
1335 else
1336 res = PyDict_SetItem(dict, name, value);
Benjamin Peterson74529ad2012-03-09 07:25:32 -08001337 Py_DECREF(dict);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001338 }
Serhiy Storchaka55c861f2016-04-17 20:31:51 +03001339 if (res < 0 && PyErr_ExceptionMatches(PyExc_KeyError))
1340 PyErr_SetObject(PyExc_AttributeError, name);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001341
Guido van Rossumebca9fc2001-12-04 15:54:53 +00001342 done:
Victor Stinner2d01dc02012-03-09 00:44:13 +01001343 Py_XDECREF(descr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001344 Py_DECREF(name);
1345 return res;
Guido van Rossum98ff96a1997-05-20 18:34:44 +00001346}
1347
Antoine Pitrou1a9a9d52010-08-28 18:17:03 +00001348int
1349PyObject_GenericSetAttr(PyObject *obj, PyObject *name, PyObject *value)
1350{
1351 return _PyObject_GenericSetAttrWithDict(obj, name, value, NULL);
1352}
1353
Benjamin Peterson8eb12692012-02-19 19:59:10 -05001354int
1355PyObject_GenericSetDict(PyObject *obj, PyObject *value, void *context)
1356{
Serhiy Storchaka576f1322016-01-05 21:27:54 +02001357 PyObject **dictptr = _PyObject_GetDictPtr(obj);
Benjamin Peterson8eb12692012-02-19 19:59:10 -05001358 if (dictptr == NULL) {
1359 PyErr_SetString(PyExc_AttributeError,
1360 "This object has no __dict__");
1361 return -1;
1362 }
1363 if (value == NULL) {
1364 PyErr_SetString(PyExc_TypeError, "cannot delete __dict__");
1365 return -1;
1366 }
1367 if (!PyDict_Check(value)) {
1368 PyErr_Format(PyExc_TypeError,
1369 "__dict__ must be set to a dictionary, "
1370 "not a '%.200s'", Py_TYPE(value)->tp_name);
1371 return -1;
1372 }
Serhiy Storchaka576f1322016-01-05 21:27:54 +02001373 Py_INCREF(value);
Serhiy Storchakaec397562016-04-06 09:50:03 +03001374 Py_XSETREF(*dictptr, value);
Benjamin Peterson8eb12692012-02-19 19:59:10 -05001375 return 0;
1376}
1377
Antoine Pitrou1a9a9d52010-08-28 18:17:03 +00001378
Stefan Pochmannf90dc362020-10-07 16:12:52 +02001379/* Test a value used as condition, e.g., in a while or if statement.
Guido van Rossum6ac258d1993-05-12 08:24:20 +00001380 Return -1 if an error occurred */
1381
1382int
Fred Drake100814d2000-07-09 15:48:49 +00001383PyObject_IsTrue(PyObject *v)
Guido van Rossum6ac258d1993-05-12 08:24:20 +00001384{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001385 Py_ssize_t res;
1386 if (v == Py_True)
1387 return 1;
1388 if (v == Py_False)
1389 return 0;
1390 if (v == Py_None)
1391 return 0;
Victor Stinner58ac7002020-02-07 03:04:21 +01001392 else if (Py_TYPE(v)->tp_as_number != NULL &&
1393 Py_TYPE(v)->tp_as_number->nb_bool != NULL)
1394 res = (*Py_TYPE(v)->tp_as_number->nb_bool)(v);
1395 else if (Py_TYPE(v)->tp_as_mapping != NULL &&
1396 Py_TYPE(v)->tp_as_mapping->mp_length != NULL)
1397 res = (*Py_TYPE(v)->tp_as_mapping->mp_length)(v);
1398 else if (Py_TYPE(v)->tp_as_sequence != NULL &&
1399 Py_TYPE(v)->tp_as_sequence->sq_length != NULL)
1400 res = (*Py_TYPE(v)->tp_as_sequence->sq_length)(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001401 else
1402 return 1;
1403 /* if it is negative, it should be either -1 or -2 */
1404 return (res > 0) ? 1 : Py_SAFE_DOWNCAST(res, Py_ssize_t, int);
Guido van Rossum6ac258d1993-05-12 08:24:20 +00001405}
1406
Tim Peters803526b2002-07-07 05:13:56 +00001407/* equivalent of 'not v'
Guido van Rossumc3d3f961998-04-09 17:53:59 +00001408 Return -1 if an error occurred */
1409
1410int
Fred Drake100814d2000-07-09 15:48:49 +00001411PyObject_Not(PyObject *v)
Guido van Rossumc3d3f961998-04-09 17:53:59 +00001412{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001413 int res;
1414 res = PyObject_IsTrue(v);
1415 if (res < 0)
1416 return res;
1417 return res == 0;
Guido van Rossumc3d3f961998-04-09 17:53:59 +00001418}
1419
Guido van Rossum49b11fe1995-01-26 00:38:22 +00001420/* Test whether an object can be called */
1421
1422int
Fred Drake100814d2000-07-09 15:48:49 +00001423PyCallable_Check(PyObject *x)
Guido van Rossum49b11fe1995-01-26 00:38:22 +00001424{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001425 if (x == NULL)
1426 return 0;
Victor Stinner58ac7002020-02-07 03:04:21 +01001427 return Py_TYPE(x)->tp_call != NULL;
Guido van Rossum49b11fe1995-01-26 00:38:22 +00001428}
1429
Tim Peters7eea37e2001-09-04 22:08:56 +00001430
Georg Brandle32b4222007-03-10 22:13:27 +00001431/* Helper for PyObject_Dir without arguments: returns the local scope. */
1432static PyObject *
Guido van Rossumad7d8d12007-04-13 01:39:34 +00001433_dir_locals(void)
Tim Peters305b5852001-09-17 02:38:46 +00001434{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001435 PyObject *names;
Victor Stinner41bb43a2013-10-29 01:19:37 +01001436 PyObject *locals;
Tim Peters305b5852001-09-17 02:38:46 +00001437
Victor Stinner41bb43a2013-10-29 01:19:37 +01001438 locals = PyEval_GetLocals();
1439 if (locals == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001440 return NULL;
Tim Peters305b5852001-09-17 02:38:46 +00001441
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001442 names = PyMapping_Keys(locals);
1443 if (!names)
1444 return NULL;
1445 if (!PyList_Check(names)) {
1446 PyErr_Format(PyExc_TypeError,
1447 "dir(): expected keys() of locals to be a list, "
1448 "not '%.200s'", Py_TYPE(names)->tp_name);
1449 Py_DECREF(names);
1450 return NULL;
1451 }
Benjamin Peterson3bbb7222011-06-11 16:12:08 -05001452 if (PyList_Sort(names)) {
1453 Py_DECREF(names);
1454 return NULL;
1455 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001456 /* the locals don't need to be DECREF'd */
1457 return names;
Georg Brandle32b4222007-03-10 22:13:27 +00001458}
1459
Benjamin Peterson82b00c12011-05-24 11:09:06 -05001460/* Helper for PyObject_Dir: object introspection. */
Georg Brandle32b4222007-03-10 22:13:27 +00001461static PyObject *
1462_dir_object(PyObject *obj)
1463{
Benjamin Peterson3bbb7222011-06-11 16:12:08 -05001464 PyObject *result, *sorted;
Benjamin Petersonce798522012-01-22 11:24:29 -05001465 PyObject *dirfunc = _PyObject_LookupSpecial(obj, &PyId___dir__);
Georg Brandle32b4222007-03-10 22:13:27 +00001466
Victor Stinner24702042018-10-26 17:16:37 +02001467 assert(obj != NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001468 if (dirfunc == NULL) {
Benjamin Peterson82b00c12011-05-24 11:09:06 -05001469 if (!PyErr_Occurred())
1470 PyErr_SetString(PyExc_TypeError, "object does not provide __dir__");
1471 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001472 }
Benjamin Peterson82b00c12011-05-24 11:09:06 -05001473 /* use __dir__ */
Victor Stinnerf17c3de2016-12-06 18:46:19 +01001474 result = _PyObject_CallNoArg(dirfunc);
Benjamin Peterson82b00c12011-05-24 11:09:06 -05001475 Py_DECREF(dirfunc);
1476 if (result == NULL)
1477 return NULL;
Benjamin Peterson3bbb7222011-06-11 16:12:08 -05001478 /* return sorted(result) */
1479 sorted = PySequence_List(result);
1480 Py_DECREF(result);
1481 if (sorted == NULL)
1482 return NULL;
1483 if (PyList_Sort(sorted)) {
1484 Py_DECREF(sorted);
1485 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001486 }
Benjamin Peterson3bbb7222011-06-11 16:12:08 -05001487 return sorted;
Georg Brandle32b4222007-03-10 22:13:27 +00001488}
1489
1490/* Implementation of dir() -- if obj is NULL, returns the names in the current
1491 (local) scope. Otherwise, performs introspection of the object: returns a
1492 sorted list of attribute names (supposedly) accessible from the object
1493*/
1494PyObject *
1495PyObject_Dir(PyObject *obj)
1496{
Benjamin Peterson3bbb7222011-06-11 16:12:08 -05001497 return (obj == NULL) ? _dir_locals() : _dir_object(obj);
Tim Peters7eea37e2001-09-04 22:08:56 +00001498}
Guido van Rossum49b11fe1995-01-26 00:38:22 +00001499
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001500/*
Raymond Hettinger66d2be82011-07-28 09:55:13 -07001501None is a non-NULL undefined value.
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001502There is (and should be!) no way to create other objects of this type,
Guido van Rossum3f5da241990-12-20 15:06:42 +00001503so there is exactly one (which is indestructible, by the way).
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001504*/
1505
Guido van Rossum0c182a11992-03-27 17:26:13 +00001506/* ARGSUSED */
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001507static PyObject *
Fred Drake100814d2000-07-09 15:48:49 +00001508none_repr(PyObject *op)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001509{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001510 return PyUnicode_FromString("None");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001511}
1512
Barry Warsaw9bf16442001-01-23 16:24:35 +00001513/* ARGUSED */
Victor Stinner2a4903f2020-01-30 13:09:11 +01001514static void _Py_NO_RETURN
Tim Peters803526b2002-07-07 05:13:56 +00001515none_dealloc(PyObject* ignore)
Barry Warsaw9bf16442001-01-23 16:24:35 +00001516{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001517 /* This should never get called, but we also don't want to SEGV if
1518 * we accidentally decref None out of existence.
1519 */
1520 Py_FatalError("deallocating None");
Barry Warsaw9bf16442001-01-23 16:24:35 +00001521}
1522
Benjamin Petersonc4607ae2011-07-29 18:19:43 -05001523static PyObject *
1524none_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
1525{
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +02001526 if (PyTuple_GET_SIZE(args) || (kwargs && PyDict_GET_SIZE(kwargs))) {
Benjamin Petersonc4607ae2011-07-29 18:19:43 -05001527 PyErr_SetString(PyExc_TypeError, "NoneType takes no arguments");
1528 return NULL;
1529 }
1530 Py_RETURN_NONE;
1531}
1532
Raymond Hettinger66d2be82011-07-28 09:55:13 -07001533static int
1534none_bool(PyObject *v)
1535{
1536 return 0;
1537}
1538
1539static PyNumberMethods none_as_number = {
1540 0, /* nb_add */
1541 0, /* nb_subtract */
1542 0, /* nb_multiply */
1543 0, /* nb_remainder */
1544 0, /* nb_divmod */
1545 0, /* nb_power */
1546 0, /* nb_negative */
1547 0, /* nb_positive */
1548 0, /* nb_absolute */
1549 (inquiry)none_bool, /* nb_bool */
1550 0, /* nb_invert */
1551 0, /* nb_lshift */
1552 0, /* nb_rshift */
1553 0, /* nb_and */
1554 0, /* nb_xor */
1555 0, /* nb_or */
1556 0, /* nb_int */
1557 0, /* nb_reserved */
1558 0, /* nb_float */
1559 0, /* nb_inplace_add */
1560 0, /* nb_inplace_subtract */
1561 0, /* nb_inplace_multiply */
1562 0, /* nb_inplace_remainder */
1563 0, /* nb_inplace_power */
1564 0, /* nb_inplace_lshift */
1565 0, /* nb_inplace_rshift */
1566 0, /* nb_inplace_and */
1567 0, /* nb_inplace_xor */
1568 0, /* nb_inplace_or */
1569 0, /* nb_floor_divide */
1570 0, /* nb_true_divide */
1571 0, /* nb_inplace_floor_divide */
1572 0, /* nb_inplace_true_divide */
1573 0, /* nb_index */
1574};
Barry Warsaw9bf16442001-01-23 16:24:35 +00001575
Alexandre Vassalotti65846c62013-11-30 17:55:48 -08001576PyTypeObject _PyNone_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001577 PyVarObject_HEAD_INIT(&PyType_Type, 0)
1578 "NoneType",
1579 0,
1580 0,
1581 none_dealloc, /*tp_dealloc*/ /*never called*/
Jeroen Demeyer530f5062019-05-31 04:13:39 +02001582 0, /*tp_vectorcall_offset*/
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001583 0, /*tp_getattr*/
1584 0, /*tp_setattr*/
Jeroen Demeyer530f5062019-05-31 04:13:39 +02001585 0, /*tp_as_async*/
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001586 none_repr, /*tp_repr*/
Raymond Hettinger66d2be82011-07-28 09:55:13 -07001587 &none_as_number, /*tp_as_number*/
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001588 0, /*tp_as_sequence*/
1589 0, /*tp_as_mapping*/
1590 0, /*tp_hash */
Benjamin Petersonc4607ae2011-07-29 18:19:43 -05001591 0, /*tp_call */
1592 0, /*tp_str */
1593 0, /*tp_getattro */
1594 0, /*tp_setattro */
1595 0, /*tp_as_buffer */
1596 Py_TPFLAGS_DEFAULT, /*tp_flags */
1597 0, /*tp_doc */
1598 0, /*tp_traverse */
1599 0, /*tp_clear */
1600 0, /*tp_richcompare */
1601 0, /*tp_weaklistoffset */
1602 0, /*tp_iter */
1603 0, /*tp_iternext */
1604 0, /*tp_methods */
1605 0, /*tp_members */
1606 0, /*tp_getset */
1607 0, /*tp_base */
1608 0, /*tp_dict */
1609 0, /*tp_descr_get */
1610 0, /*tp_descr_set */
1611 0, /*tp_dictoffset */
1612 0, /*tp_init */
1613 0, /*tp_alloc */
1614 none_new, /*tp_new */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001615};
1616
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001617PyObject _Py_NoneStruct = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001618 _PyObject_EXTRA_INIT
Alexandre Vassalotti65846c62013-11-30 17:55:48 -08001619 1, &_PyNone_Type
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001620};
1621
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +00001622/* NotImplemented is an object that can be used to signal that an
1623 operation is not implemented for the given type combination. */
1624
1625static PyObject *
1626NotImplemented_repr(PyObject *op)
1627{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001628 return PyUnicode_FromString("NotImplemented");
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +00001629}
1630
Benjamin Peterson18d7d7a2011-07-29 18:27:44 -05001631static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05301632NotImplemented_reduce(PyObject *op, PyObject *Py_UNUSED(ignored))
Alexandre Vassalottic49477b2013-11-24 02:53:45 -08001633{
1634 return PyUnicode_FromString("NotImplemented");
1635}
1636
1637static PyMethodDef notimplemented_methods[] = {
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05301638 {"__reduce__", NotImplemented_reduce, METH_NOARGS, NULL},
Alexandre Vassalottic49477b2013-11-24 02:53:45 -08001639 {NULL, NULL}
1640};
1641
1642static PyObject *
Benjamin Peterson18d7d7a2011-07-29 18:27:44 -05001643notimplemented_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
1644{
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +02001645 if (PyTuple_GET_SIZE(args) || (kwargs && PyDict_GET_SIZE(kwargs))) {
Benjamin Peterson18d7d7a2011-07-29 18:27:44 -05001646 PyErr_SetString(PyExc_TypeError, "NotImplementedType takes no arguments");
1647 return NULL;
1648 }
Brian Curtindfc80e32011-08-10 20:28:54 -05001649 Py_RETURN_NOTIMPLEMENTED;
Benjamin Peterson18d7d7a2011-07-29 18:27:44 -05001650}
1651
Victor Stinner2a4903f2020-01-30 13:09:11 +01001652static void _Py_NO_RETURN
Armin Ronacher226b1db2012-10-06 14:28:58 +02001653notimplemented_dealloc(PyObject* ignore)
1654{
1655 /* This should never get called, but we also don't want to SEGV if
1656 * we accidentally decref NotImplemented out of existence.
1657 */
1658 Py_FatalError("deallocating NotImplemented");
1659}
1660
MojoVampire469325c2020-03-03 18:50:17 +00001661static int
1662notimplemented_bool(PyObject *v)
1663{
1664 if (PyErr_WarnEx(PyExc_DeprecationWarning,
1665 "NotImplemented should not be used in a boolean context",
1666 1) < 0)
1667 {
1668 return -1;
1669 }
1670 return 1;
1671}
1672
1673static PyNumberMethods notimplemented_as_number = {
1674 .nb_bool = notimplemented_bool,
1675};
1676
Alexandre Vassalotti65846c62013-11-30 17:55:48 -08001677PyTypeObject _PyNotImplemented_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001678 PyVarObject_HEAD_INIT(&PyType_Type, 0)
1679 "NotImplementedType",
1680 0,
1681 0,
Armin Ronacher226b1db2012-10-06 14:28:58 +02001682 notimplemented_dealloc, /*tp_dealloc*/ /*never called*/
Jeroen Demeyer530f5062019-05-31 04:13:39 +02001683 0, /*tp_vectorcall_offset*/
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001684 0, /*tp_getattr*/
1685 0, /*tp_setattr*/
Jeroen Demeyer530f5062019-05-31 04:13:39 +02001686 0, /*tp_as_async*/
MojoVampire469325c2020-03-03 18:50:17 +00001687 NotImplemented_repr, /*tp_repr*/
1688 &notimplemented_as_number, /*tp_as_number*/
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001689 0, /*tp_as_sequence*/
1690 0, /*tp_as_mapping*/
1691 0, /*tp_hash */
Benjamin Peterson18d7d7a2011-07-29 18:27:44 -05001692 0, /*tp_call */
1693 0, /*tp_str */
1694 0, /*tp_getattro */
1695 0, /*tp_setattro */
1696 0, /*tp_as_buffer */
1697 Py_TPFLAGS_DEFAULT, /*tp_flags */
1698 0, /*tp_doc */
1699 0, /*tp_traverse */
1700 0, /*tp_clear */
1701 0, /*tp_richcompare */
1702 0, /*tp_weaklistoffset */
1703 0, /*tp_iter */
1704 0, /*tp_iternext */
Alexandre Vassalottic49477b2013-11-24 02:53:45 -08001705 notimplemented_methods, /*tp_methods */
Benjamin Peterson18d7d7a2011-07-29 18:27:44 -05001706 0, /*tp_members */
1707 0, /*tp_getset */
1708 0, /*tp_base */
1709 0, /*tp_dict */
1710 0, /*tp_descr_get */
1711 0, /*tp_descr_set */
1712 0, /*tp_dictoffset */
1713 0, /*tp_init */
1714 0, /*tp_alloc */
1715 notimplemented_new, /*tp_new */
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +00001716};
1717
1718PyObject _Py_NotImplementedStruct = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001719 _PyObject_EXTRA_INIT
Alexandre Vassalotti65846c62013-11-30 17:55:48 -08001720 1, &_PyNotImplemented_Type
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +00001721};
1722
Victor Stinner331a6a52019-05-27 16:39:22 +02001723PyStatus
Victor Stinnerab672812019-01-23 15:04:40 +01001724_PyTypes_Init(void)
Guido van Rossumba21a492001-08-16 08:17:26 +00001725{
Victor Stinner7a1f6c22020-01-30 09:02:14 +01001726 PyStatus status = _PyTypes_InitSlotDefs();
1727 if (_PyStatus_EXCEPTION(status)) {
1728 return status;
1729 }
1730
Victor Stinner6d43f6f2019-01-22 21:18:05 +01001731#define INIT_TYPE(TYPE, NAME) \
1732 do { \
1733 if (PyType_Ready(TYPE) < 0) { \
Victor Stinner331a6a52019-05-27 16:39:22 +02001734 return _PyStatus_ERR("Can't initialize " NAME " type"); \
Victor Stinner6d43f6f2019-01-22 21:18:05 +01001735 } \
1736 } while (0)
Victor Stinner5a1bb4e2014-06-02 14:10:59 +02001737
Victor Stinner6d43f6f2019-01-22 21:18:05 +01001738 INIT_TYPE(&PyBaseObject_Type, "object");
1739 INIT_TYPE(&PyType_Type, "type");
1740 INIT_TYPE(&_PyWeakref_RefType, "weakref");
1741 INIT_TYPE(&_PyWeakref_CallableProxyType, "callable weakref proxy");
1742 INIT_TYPE(&_PyWeakref_ProxyType, "weakref proxy");
1743 INIT_TYPE(&PyLong_Type, "int");
1744 INIT_TYPE(&PyBool_Type, "bool");
1745 INIT_TYPE(&PyByteArray_Type, "bytearray");
1746 INIT_TYPE(&PyBytes_Type, "str");
1747 INIT_TYPE(&PyList_Type, "list");
1748 INIT_TYPE(&_PyNone_Type, "None");
1749 INIT_TYPE(&_PyNotImplemented_Type, "NotImplemented");
1750 INIT_TYPE(&PyTraceBack_Type, "traceback");
1751 INIT_TYPE(&PySuper_Type, "super");
1752 INIT_TYPE(&PyRange_Type, "range");
1753 INIT_TYPE(&PyDict_Type, "dict");
1754 INIT_TYPE(&PyDictKeys_Type, "dict keys");
1755 INIT_TYPE(&PyDictValues_Type, "dict values");
1756 INIT_TYPE(&PyDictItems_Type, "dict items");
1757 INIT_TYPE(&PyDictRevIterKey_Type, "reversed dict keys");
1758 INIT_TYPE(&PyDictRevIterValue_Type, "reversed dict values");
1759 INIT_TYPE(&PyDictRevIterItem_Type, "reversed dict items");
1760 INIT_TYPE(&PyODict_Type, "OrderedDict");
1761 INIT_TYPE(&PyODictKeys_Type, "odict_keys");
1762 INIT_TYPE(&PyODictItems_Type, "odict_items");
1763 INIT_TYPE(&PyODictValues_Type, "odict_values");
1764 INIT_TYPE(&PyODictIter_Type, "odict_keyiterator");
1765 INIT_TYPE(&PySet_Type, "set");
1766 INIT_TYPE(&PyUnicode_Type, "str");
1767 INIT_TYPE(&PySlice_Type, "slice");
1768 INIT_TYPE(&PyStaticMethod_Type, "static method");
1769 INIT_TYPE(&PyComplex_Type, "complex");
1770 INIT_TYPE(&PyFloat_Type, "float");
1771 INIT_TYPE(&PyFrozenSet_Type, "frozenset");
1772 INIT_TYPE(&PyProperty_Type, "property");
1773 INIT_TYPE(&_PyManagedBuffer_Type, "managed buffer");
1774 INIT_TYPE(&PyMemoryView_Type, "memoryview");
1775 INIT_TYPE(&PyTuple_Type, "tuple");
1776 INIT_TYPE(&PyEnum_Type, "enumerate");
1777 INIT_TYPE(&PyReversed_Type, "reversed");
1778 INIT_TYPE(&PyStdPrinter_Type, "StdPrinter");
1779 INIT_TYPE(&PyCode_Type, "code");
1780 INIT_TYPE(&PyFrame_Type, "frame");
1781 INIT_TYPE(&PyCFunction_Type, "builtin function");
Petr Viktorine1becf42020-05-07 15:39:59 +02001782 INIT_TYPE(&PyCMethod_Type, "builtin method");
Victor Stinner6d43f6f2019-01-22 21:18:05 +01001783 INIT_TYPE(&PyMethod_Type, "method");
1784 INIT_TYPE(&PyFunction_Type, "function");
1785 INIT_TYPE(&PyDictProxy_Type, "dict proxy");
1786 INIT_TYPE(&PyGen_Type, "generator");
1787 INIT_TYPE(&PyGetSetDescr_Type, "get-set descriptor");
1788 INIT_TYPE(&PyWrapperDescr_Type, "wrapper");
1789 INIT_TYPE(&_PyMethodWrapper_Type, "method wrapper");
1790 INIT_TYPE(&PyEllipsis_Type, "ellipsis");
1791 INIT_TYPE(&PyMemberDescr_Type, "member descriptor");
1792 INIT_TYPE(&_PyNamespace_Type, "namespace");
1793 INIT_TYPE(&PyCapsule_Type, "capsule");
1794 INIT_TYPE(&PyLongRangeIter_Type, "long range iterator");
1795 INIT_TYPE(&PyCell_Type, "cell");
1796 INIT_TYPE(&PyInstanceMethod_Type, "instance method");
1797 INIT_TYPE(&PyClassMethodDescr_Type, "class method descr");
1798 INIT_TYPE(&PyMethodDescr_Type, "method descr");
1799 INIT_TYPE(&PyCallIter_Type, "call iter");
1800 INIT_TYPE(&PySeqIter_Type, "sequence iterator");
Antoine Pitrou91f43802019-05-26 17:10:09 +02001801 INIT_TYPE(&PyPickleBuffer_Type, "pickle.PickleBuffer");
Victor Stinner6d43f6f2019-01-22 21:18:05 +01001802 INIT_TYPE(&PyCoro_Type, "coroutine");
1803 INIT_TYPE(&_PyCoroWrapper_Type, "coroutine wrapper");
Eric Snowc11183c2019-03-15 16:35:46 -06001804 INIT_TYPE(&_PyInterpreterID_Type, "interpreter ID");
Victor Stinner331a6a52019-05-27 16:39:22 +02001805 return _PyStatus_OK();
Guido van Rossumba21a492001-08-16 08:17:26 +00001806
Victor Stinner6d43f6f2019-01-22 21:18:05 +01001807#undef INIT_TYPE
Guido van Rossumba21a492001-08-16 08:17:26 +00001808}
1809
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001810
Victor Stinner40e547d2020-02-05 01:11:10 +01001811void
1812_Py_NewReference(PyObject *op)
1813{
1814 if (_Py_tracemalloc_config.tracing) {
1815 _PyTraceMalloc_NewReference(op);
1816 }
1817#ifdef Py_REF_DEBUG
1818 _Py_RefTotal++;
1819#endif
Victor Stinnerc86a1122020-02-07 01:24:29 +01001820 Py_SET_REFCNT(op, 1);
Victor Stinner40e547d2020-02-05 01:11:10 +01001821#ifdef Py_TRACE_REFS
1822 _Py_AddToAllObjects(op, 1);
1823#endif
1824}
1825
1826
Guido van Rossum84a90321996-05-22 16:34:47 +00001827#ifdef Py_TRACE_REFS
Guido van Rossumaacdc9d1996-08-12 21:32:12 +00001828void
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02001829_Py_ForgetReference(PyObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001830{
Victor Stinnera93c51e2020-02-07 00:38:59 +01001831 if (Py_REFCNT(op) < 0) {
Victor Stinner5eb8bff2020-01-30 09:01:07 +01001832 _PyObject_ASSERT_FAILED_MSG(op, "negative refcnt");
1833 }
1834
1835 if (op == &refchain ||
1836 op->_ob_prev->_ob_next != op || op->_ob_next->_ob_prev != op)
1837 {
1838 _PyObject_ASSERT_FAILED_MSG(op, "invalid object chain");
1839 }
1840
Guido van Rossumbffd6832000-01-20 22:32:56 +00001841#ifdef SLOW_UNREF_CHECK
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02001842 PyObject *p;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001843 for (p = refchain._ob_next; p != &refchain; p = p->_ob_next) {
Victor Stinner5eb8bff2020-01-30 09:01:07 +01001844 if (p == op) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001845 break;
Victor Stinner5eb8bff2020-01-30 09:01:07 +01001846 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001847 }
Victor Stinner5eb8bff2020-01-30 09:01:07 +01001848 if (p == &refchain) {
1849 /* Not found */
1850 _PyObject_ASSERT_FAILED_MSG(op,
1851 "object not found in the objects list");
1852 }
Guido van Rossum2e8f6141992-09-03 20:32:55 +00001853#endif
Victor Stinner5eb8bff2020-01-30 09:01:07 +01001854
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001855 op->_ob_next->_ob_prev = op->_ob_prev;
1856 op->_ob_prev->_ob_next = op->_ob_next;
1857 op->_ob_next = op->_ob_prev = NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001858}
1859
Tim Peters269b2a62003-04-17 19:52:29 +00001860/* Print all live objects. Because PyObject_Print is called, the
1861 * interpreter must be in a healthy state.
1862 */
Guido van Rossumaacdc9d1996-08-12 21:32:12 +00001863void
Fred Drake100814d2000-07-09 15:48:49 +00001864_Py_PrintReferences(FILE *fp)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001865{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001866 PyObject *op;
1867 fprintf(fp, "Remaining objects:\n");
1868 for (op = refchain._ob_next; op != &refchain; op = op->_ob_next) {
Victor Stinnerd36cf5f2020-06-10 18:38:05 +02001869 fprintf(fp, "%p [%zd] ", (void *)op, Py_REFCNT(op));
1870 if (PyObject_Print(op, fp, 0) != 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001871 PyErr_Clear();
Victor Stinnerd36cf5f2020-06-10 18:38:05 +02001872 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001873 putc('\n', fp);
1874 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001875}
1876
Tim Peters269b2a62003-04-17 19:52:29 +00001877/* Print the addresses of all live objects. Unlike _Py_PrintReferences, this
1878 * doesn't make any calls to the Python C API, so is always safe to call.
1879 */
1880void
1881_Py_PrintReferenceAddresses(FILE *fp)
1882{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001883 PyObject *op;
1884 fprintf(fp, "Remaining object addresses:\n");
1885 for (op = refchain._ob_next; op != &refchain; op = op->_ob_next)
Victor Stinnerd36cf5f2020-06-10 18:38:05 +02001886 fprintf(fp, "%p [%zd] %s\n", (void *)op,
Victor Stinnera93c51e2020-02-07 00:38:59 +01001887 Py_REFCNT(op), Py_TYPE(op)->tp_name);
Tim Peters269b2a62003-04-17 19:52:29 +00001888}
1889
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +00001890PyObject *
Fred Drake100814d2000-07-09 15:48:49 +00001891_Py_GetObjects(PyObject *self, PyObject *args)
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +00001892{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001893 int i, n;
1894 PyObject *t = NULL;
1895 PyObject *res, *op;
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +00001896
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001897 if (!PyArg_ParseTuple(args, "i|O", &n, &t))
1898 return NULL;
1899 op = refchain._ob_next;
1900 res = PyList_New(0);
1901 if (res == NULL)
1902 return NULL;
1903 for (i = 0; (n == 0 || i < n) && op != &refchain; i++) {
1904 while (op == self || op == args || op == res || op == t ||
Andy Lester55728702020-03-06 16:53:17 -06001905 (t != NULL && !Py_IS_TYPE(op, (PyTypeObject *) t))) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001906 op = op->_ob_next;
1907 if (op == &refchain)
1908 return res;
1909 }
1910 if (PyList_Append(res, op) < 0) {
1911 Py_DECREF(res);
1912 return NULL;
1913 }
1914 op = op->_ob_next;
1915 }
1916 return res;
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +00001917}
1918
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001919#endif
Guido van Rossum97ead3f1996-01-12 01:24:09 +00001920
Benjamin Petersonb173f782009-05-05 22:31:58 +00001921
Guido van Rossum84a90321996-05-22 16:34:47 +00001922/* Hack to force loading of abstract.o */
Martin v. Löwis18e16552006-02-15 17:27:45 +00001923Py_ssize_t (*_Py_abstract_hack)(PyObject *) = PyObject_Size;
Guido van Rossume09fb551997-08-05 02:04:34 +00001924
1925
David Malcolm49526f42012-06-22 14:55:41 -04001926void
1927_PyObject_DebugTypeStats(FILE *out)
1928{
David Malcolm49526f42012-06-22 14:55:41 -04001929 _PyDict_DebugMallocStats(out);
1930 _PyFloat_DebugMallocStats(out);
1931 _PyFrame_DebugMallocStats(out);
1932 _PyList_DebugMallocStats(out);
David Malcolm49526f42012-06-22 14:55:41 -04001933 _PyTuple_DebugMallocStats(out);
1934}
Guido van Rossumb18618d2000-05-03 23:44:39 +00001935
Guido van Rossum86610361998-04-10 22:32:46 +00001936/* These methods are used to control infinite recursion in repr, str, print,
1937 etc. Container objects that may recursively contain themselves,
Martin Panter8d56c022016-05-29 04:13:35 +00001938 e.g. builtin dictionaries and lists, should use Py_ReprEnter() and
Guido van Rossum86610361998-04-10 22:32:46 +00001939 Py_ReprLeave() to avoid infinite recursion.
1940
1941 Py_ReprEnter() returns 0 the first time it is called for a particular
1942 object and 1 every time thereafter. It returns -1 if an exception
1943 occurred. Py_ReprLeave() has no return value.
1944
1945 See dictobject.c and listobject.c for examples of use.
1946*/
1947
Guido van Rossum86610361998-04-10 22:32:46 +00001948int
Fred Drake100814d2000-07-09 15:48:49 +00001949Py_ReprEnter(PyObject *obj)
Guido van Rossum86610361998-04-10 22:32:46 +00001950{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001951 PyObject *dict;
1952 PyObject *list;
1953 Py_ssize_t i;
Guido van Rossum86610361998-04-10 22:32:46 +00001954
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001955 dict = PyThreadState_GetDict();
Antoine Pitrou04d17d32014-03-31 22:04:38 +02001956 /* Ignore a missing thread-state, so that this function can be called
1957 early on startup. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001958 if (dict == NULL)
1959 return 0;
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02001960 list = _PyDict_GetItemIdWithError(dict, &PyId_Py_Repr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001961 if (list == NULL) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02001962 if (PyErr_Occurred()) {
1963 return -1;
1964 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001965 list = PyList_New(0);
1966 if (list == NULL)
1967 return -1;
Victor Stinner7a07e452013-11-06 18:57:29 +01001968 if (_PyDict_SetItemId(dict, &PyId_Py_Repr, list) < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001969 return -1;
1970 Py_DECREF(list);
1971 }
1972 i = PyList_GET_SIZE(list);
1973 while (--i >= 0) {
1974 if (PyList_GET_ITEM(list, i) == obj)
1975 return 1;
1976 }
Victor Stinnere901d1f2013-07-17 21:58:41 +02001977 if (PyList_Append(list, obj) < 0)
1978 return -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001979 return 0;
Guido van Rossum86610361998-04-10 22:32:46 +00001980}
1981
1982void
Fred Drake100814d2000-07-09 15:48:49 +00001983Py_ReprLeave(PyObject *obj)
Guido van Rossum86610361998-04-10 22:32:46 +00001984{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001985 PyObject *dict;
1986 PyObject *list;
1987 Py_ssize_t i;
Victor Stinner1b634932013-07-16 22:24:44 +02001988 PyObject *error_type, *error_value, *error_traceback;
1989
1990 PyErr_Fetch(&error_type, &error_value, &error_traceback);
Guido van Rossum86610361998-04-10 22:32:46 +00001991
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001992 dict = PyThreadState_GetDict();
1993 if (dict == NULL)
Victor Stinner1b634932013-07-16 22:24:44 +02001994 goto finally;
1995
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02001996 list = _PyDict_GetItemIdWithError(dict, &PyId_Py_Repr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001997 if (list == NULL || !PyList_Check(list))
Victor Stinner1b634932013-07-16 22:24:44 +02001998 goto finally;
1999
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002000 i = PyList_GET_SIZE(list);
2001 /* Count backwards because we always expect obj to be list[-1] */
2002 while (--i >= 0) {
2003 if (PyList_GET_ITEM(list, i) == obj) {
2004 PyList_SetSlice(list, i, i + 1, NULL);
2005 break;
2006 }
2007 }
Victor Stinner1b634932013-07-16 22:24:44 +02002008
2009finally:
2010 /* ignore exceptions because there is no way to report them. */
2011 PyErr_Restore(error_type, error_value, error_traceback);
Guido van Rossum86610361998-04-10 22:32:46 +00002012}
Guido van Rossumd724b232000-03-13 16:01:29 +00002013
Tim Peters803526b2002-07-07 05:13:56 +00002014/* Trashcan support. */
Guido van Rossumd724b232000-03-13 16:01:29 +00002015
Tim Peters803526b2002-07-07 05:13:56 +00002016/* Add op to the _PyTrash_delete_later list. Called when the current
2017 * call-stack depth gets large. op must be a currently untracked gc'ed
2018 * object, with refcount 0. Py_DECREF must already have been called on it.
2019 */
Guido van Rossumd724b232000-03-13 16:01:29 +00002020void
Fred Drake100814d2000-07-09 15:48:49 +00002021_PyTrash_deposit_object(PyObject *op)
Guido van Rossumd724b232000-03-13 16:01:29 +00002022{
Victor Stinner1bcc32f2020-06-10 20:08:26 +02002023 PyInterpreterState *interp = _PyInterpreterState_GET();
2024 struct _gc_runtime_state *gcstate = &interp->gc;
Victor Stinner72474072019-11-20 12:25:50 +01002025
Hai Shi675d9a32020-04-15 02:11:20 +08002026 _PyObject_ASSERT(op, _PyObject_IS_GC(op));
Victor Stinner24702042018-10-26 17:16:37 +02002027 _PyObject_ASSERT(op, !_PyObject_GC_IS_TRACKED(op));
Victor Stinnera93c51e2020-02-07 00:38:59 +01002028 _PyObject_ASSERT(op, Py_REFCNT(op) == 0);
Victor Stinner72474072019-11-20 12:25:50 +01002029 _PyGCHead_SET_PREV(_Py_AS_GC(op), gcstate->trash_delete_later);
2030 gcstate->trash_delete_later = op;
Guido van Rossumd724b232000-03-13 16:01:29 +00002031}
2032
Antoine Pitrou2b0218a2012-09-06 00:59:49 +02002033/* The equivalent API, using per-thread state recursion info */
2034void
2035_PyTrash_thread_deposit_object(PyObject *op)
2036{
Victor Stinner50b48572018-11-01 01:51:40 +01002037 PyThreadState *tstate = _PyThreadState_GET();
Hai Shi675d9a32020-04-15 02:11:20 +08002038 _PyObject_ASSERT(op, _PyObject_IS_GC(op));
Victor Stinner24702042018-10-26 17:16:37 +02002039 _PyObject_ASSERT(op, !_PyObject_GC_IS_TRACKED(op));
Victor Stinnera93c51e2020-02-07 00:38:59 +01002040 _PyObject_ASSERT(op, Py_REFCNT(op) == 0);
INADA Naoki5ac9e6e2018-07-10 17:19:53 +09002041 _PyGCHead_SET_PREV(_Py_AS_GC(op), tstate->trash_delete_later);
Antoine Pitrou2b0218a2012-09-06 00:59:49 +02002042 tstate->trash_delete_later = op;
2043}
2044
Min ho Kimc4cacc82019-07-31 08:16:13 +10002045/* Deallocate all the objects in the _PyTrash_delete_later list. Called when
Tim Peters803526b2002-07-07 05:13:56 +00002046 * the call-stack unwinds again.
2047 */
Guido van Rossumd724b232000-03-13 16:01:29 +00002048void
Fred Drake100814d2000-07-09 15:48:49 +00002049_PyTrash_destroy_chain(void)
Guido van Rossumd724b232000-03-13 16:01:29 +00002050{
Victor Stinner1bcc32f2020-06-10 20:08:26 +02002051 PyInterpreterState *interp = _PyInterpreterState_GET();
2052 struct _gc_runtime_state *gcstate = &interp->gc;
Victor Stinner72474072019-11-20 12:25:50 +01002053
2054 while (gcstate->trash_delete_later) {
2055 PyObject *op = gcstate->trash_delete_later;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002056 destructor dealloc = Py_TYPE(op)->tp_dealloc;
Neil Schemenauerf589c052002-03-29 03:05:54 +00002057
Victor Stinner72474072019-11-20 12:25:50 +01002058 gcstate->trash_delete_later =
INADA Naoki5ac9e6e2018-07-10 17:19:53 +09002059 (PyObject*) _PyGCHead_PREV(_Py_AS_GC(op));
Neil Schemenauerf589c052002-03-29 03:05:54 +00002060
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002061 /* Call the deallocator directly. This used to try to
2062 * fool Py_DECREF into calling it indirectly, but
2063 * Py_DECREF was already called on this object, and in
2064 * assorted non-release builds calling Py_DECREF again ends
2065 * up distorting allocation statistics.
2066 */
Victor Stinnera93c51e2020-02-07 00:38:59 +01002067 _PyObject_ASSERT(op, Py_REFCNT(op) == 0);
Victor Stinner72474072019-11-20 12:25:50 +01002068 ++gcstate->trash_delete_nesting;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002069 (*dealloc)(op);
Victor Stinner72474072019-11-20 12:25:50 +01002070 --gcstate->trash_delete_nesting;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002071 }
Guido van Rossumd724b232000-03-13 16:01:29 +00002072}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002073
Antoine Pitrou2b0218a2012-09-06 00:59:49 +02002074/* The equivalent API, using per-thread state recursion info */
2075void
2076_PyTrash_thread_destroy_chain(void)
2077{
Victor Stinner50b48572018-11-01 01:51:40 +01002078 PyThreadState *tstate = _PyThreadState_GET();
Xiang Zhanga66f9c62017-05-13 13:36:14 +08002079 /* We need to increase trash_delete_nesting here, otherwise,
2080 _PyTrash_thread_destroy_chain will be called recursively
2081 and then possibly crash. An example that may crash without
2082 increase:
2083 N = 500000 # need to be large enough
2084 ob = object()
2085 tups = [(ob,) for i in range(N)]
2086 for i in range(49):
2087 tups = [(tup,) for tup in tups]
2088 del tups
2089 */
2090 assert(tstate->trash_delete_nesting == 0);
2091 ++tstate->trash_delete_nesting;
Antoine Pitrou2b0218a2012-09-06 00:59:49 +02002092 while (tstate->trash_delete_later) {
2093 PyObject *op = tstate->trash_delete_later;
2094 destructor dealloc = Py_TYPE(op)->tp_dealloc;
2095
2096 tstate->trash_delete_later =
INADA Naoki5ac9e6e2018-07-10 17:19:53 +09002097 (PyObject*) _PyGCHead_PREV(_Py_AS_GC(op));
Antoine Pitrou2b0218a2012-09-06 00:59:49 +02002098
2099 /* Call the deallocator directly. This used to try to
2100 * fool Py_DECREF into calling it indirectly, but
2101 * Py_DECREF was already called on this object, and in
2102 * assorted non-release builds calling Py_DECREF again ends
2103 * up distorting allocation statistics.
2104 */
Victor Stinnera93c51e2020-02-07 00:38:59 +01002105 _PyObject_ASSERT(op, Py_REFCNT(op) == 0);
Antoine Pitrou2b0218a2012-09-06 00:59:49 +02002106 (*dealloc)(op);
Xiang Zhanga66f9c62017-05-13 13:36:14 +08002107 assert(tstate->trash_delete_nesting == 1);
Antoine Pitrou2b0218a2012-09-06 00:59:49 +02002108 }
Xiang Zhanga66f9c62017-05-13 13:36:14 +08002109 --tstate->trash_delete_nesting;
Antoine Pitrou2b0218a2012-09-06 00:59:49 +02002110}
2111
Victor Stinner626bff82018-10-25 17:31:10 +02002112
Victor Stinner38965ec2020-03-13 16:51:52 +01002113int
2114_PyTrash_begin(PyThreadState *tstate, PyObject *op)
2115{
2116 if (tstate->trash_delete_nesting >= PyTrash_UNWIND_LEVEL) {
2117 /* Store the object (to be deallocated later) and jump past
2118 * Py_TRASHCAN_END, skipping the body of the deallocator */
2119 _PyTrash_thread_deposit_object(op);
2120 return 1;
2121 }
2122 ++tstate->trash_delete_nesting;
2123 return 0;
2124}
2125
2126
2127void
2128_PyTrash_end(PyThreadState *tstate)
2129{
2130 --tstate->trash_delete_nesting;
2131 if (tstate->trash_delete_later && tstate->trash_delete_nesting <= 0) {
2132 _PyTrash_thread_destroy_chain();
2133 }
2134}
2135
2136
Hai Shied1a5a52020-11-25 06:03:31 +08002137/* bpo-40170: It's only be used in Py_TRASHCAN_BEGIN macro to hide
2138 implementation details. */
2139int
2140_PyTrash_cond(PyObject *op, destructor dealloc)
2141{
2142 return Py_TYPE(op)->tp_dealloc == dealloc;
2143}
2144
2145
Victor Stinner2a4903f2020-01-30 13:09:11 +01002146void _Py_NO_RETURN
Victor Stinnerf1d002c2018-11-21 23:53:44 +01002147_PyObject_AssertFailed(PyObject *obj, const char *expr, const char *msg,
Victor Stinner626bff82018-10-25 17:31:10 +02002148 const char *file, int line, const char *function)
2149{
Victor Stinnerf1d002c2018-11-21 23:53:44 +01002150 fprintf(stderr, "%s:%d: ", file, line);
2151 if (function) {
2152 fprintf(stderr, "%s: ", function);
2153 }
Victor Stinner626bff82018-10-25 17:31:10 +02002154 fflush(stderr);
Victor Stinner68762572019-10-07 18:42:01 +02002155
Victor Stinnerf1d002c2018-11-21 23:53:44 +01002156 if (expr) {
2157 fprintf(stderr, "Assertion \"%s\" failed", expr);
Victor Stinner626bff82018-10-25 17:31:10 +02002158 }
2159 else {
Victor Stinnerf1d002c2018-11-21 23:53:44 +01002160 fprintf(stderr, "Assertion failed");
Victor Stinner626bff82018-10-25 17:31:10 +02002161 }
2162 fflush(stderr);
Victor Stinner68762572019-10-07 18:42:01 +02002163
Victor Stinnerf1d002c2018-11-21 23:53:44 +01002164 if (msg) {
2165 fprintf(stderr, ": %s", msg);
2166 }
2167 fprintf(stderr, "\n");
2168 fflush(stderr);
Victor Stinner626bff82018-10-25 17:31:10 +02002169
Victor Stinner68762572019-10-07 18:42:01 +02002170 if (_PyObject_IsFreed(obj)) {
Victor Stinner626bff82018-10-25 17:31:10 +02002171 /* It seems like the object memory has been freed:
2172 don't access it to prevent a segmentation fault. */
Victor Stinnerb39afb72019-09-17 23:36:28 +02002173 fprintf(stderr, "<object at %p is freed>\n", obj);
Victor Stinner68762572019-10-07 18:42:01 +02002174 fflush(stderr);
Victor Stinner626bff82018-10-25 17:31:10 +02002175 }
2176 else {
penguindustin96466302019-05-06 14:57:17 -04002177 /* Display the traceback where the object has been allocated.
Victor Stinner626bff82018-10-25 17:31:10 +02002178 Do it before dumping repr(obj), since repr() is more likely
2179 to crash than dumping the traceback. */
2180 void *ptr;
2181 PyTypeObject *type = Py_TYPE(obj);
Victor Stinner45ec5b92020-04-08 01:42:27 +02002182 if (_PyType_IS_GC(type)) {
Victor Stinner626bff82018-10-25 17:31:10 +02002183 ptr = (void *)((char *)obj - sizeof(PyGC_Head));
2184 }
2185 else {
2186 ptr = (void *)obj;
2187 }
2188 _PyMem_DumpTraceback(fileno(stderr), ptr);
2189
2190 /* This might succeed or fail, but we're about to abort, so at least
2191 try to provide any extra info we can: */
2192 _PyObject_Dump(obj);
Victor Stinner77753492019-10-07 23:44:05 +02002193
2194 fprintf(stderr, "\n");
2195 fflush(stderr);
Victor Stinner626bff82018-10-25 17:31:10 +02002196 }
Victor Stinner626bff82018-10-25 17:31:10 +02002197
2198 Py_FatalError("_PyObject_AssertFailed");
2199}
2200
Victor Stinner3c09dca2018-10-30 14:48:26 +01002201
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00002202void
2203_Py_Dealloc(PyObject *op)
2204{
Victor Stinner3c09dca2018-10-30 14:48:26 +01002205 destructor dealloc = Py_TYPE(op)->tp_dealloc;
2206#ifdef Py_TRACE_REFS
2207 _Py_ForgetReference(op);
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00002208#endif
Victor Stinner3c09dca2018-10-30 14:48:26 +01002209 (*dealloc)(op);
2210}
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00002211
Victor Stinner38aefc52020-04-06 14:07:02 +02002212
2213PyObject **
2214PyObject_GET_WEAKREFS_LISTPTR(PyObject *op)
2215{
2216 return _PyObject_GET_WEAKREFS_LISTPTR(op);
2217}
2218
2219
Victor Stinner53a03aa2020-11-05 15:02:12 +01002220#undef Py_NewRef
2221#undef Py_XNewRef
2222
2223// Export Py_NewRef() and Py_XNewRef() as regular functions for the stable ABI.
2224PyObject*
2225Py_NewRef(PyObject *obj)
2226{
2227 return _Py_NewRef(obj);
2228}
2229
2230PyObject*
2231Py_XNewRef(PyObject *obj)
2232{
2233 return _Py_XNewRef(obj);
2234}
2235
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002236#ifdef __cplusplus
2237}
2238#endif