blob: ed8a62a163aa6521f9a2e0d47b30008f3ad47066 [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"
Eric Snow2ebc5ce2017-09-07 23:51:28 -06005#include "internal/pystate.h"
Benjamin Petersonfd838e62009-04-20 02:09:13 +00006#include "frameobject.h"
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00007
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00008#ifdef __cplusplus
9extern "C" {
10#endif
11
Victor Stinnerbd303c12013-11-07 23:07:29 +010012_Py_IDENTIFIER(Py_Repr);
13_Py_IDENTIFIER(__bytes__);
14_Py_IDENTIFIER(__dir__);
15_Py_IDENTIFIER(__isabstractmethod__);
16_Py_IDENTIFIER(builtins);
17
Tim Peters34592512002-07-11 06:23:50 +000018#ifdef Py_REF_DEBUG
Neal Norwitz84632ee2006-03-04 20:00:59 +000019Py_ssize_t _Py_RefTotal;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000020
21Py_ssize_t
22_Py_GetRefTotal(void)
23{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000024 PyObject *o;
25 Py_ssize_t total = _Py_RefTotal;
Antoine Pitrou9d952542013-08-24 21:07:07 +020026 o = _PySet_Dummy;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000027 if (o != NULL)
28 total -= o->ob_refcnt;
29 return total;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000030}
Nick Coghland6009512014-11-20 21:39:37 +100031
Eric Snow87280182017-09-11 17:59:22 -070032PyObject *
33_PyDebug_XOptionShowRefCount(void)
34{
35 PyObject *xoptions = PySys_GetXOptions();
36 if (xoptions == NULL)
37 return NULL;
38
39 _Py_IDENTIFIER(showrefcount);
40 return _PyDict_GetItemId(xoptions, &PyId_showrefcount);
41}
42
Nick Coghland6009512014-11-20 21:39:37 +100043void
44_PyDebug_PrintTotalRefs(void) {
Eric Snow87280182017-09-11 17:59:22 -070045 fprintf(stderr,
46 "[%" PY_FORMAT_SIZE_T "d refs, "
47 "%" PY_FORMAT_SIZE_T "d blocks]\n",
48 _Py_GetRefTotal(), _Py_GetAllocatedBlocks());
Nick Coghland6009512014-11-20 21:39:37 +100049}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000050#endif /* Py_REF_DEBUG */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000051
Guido van Rossum3f5da241990-12-20 15:06:42 +000052/* Object allocation routines used by NEWOBJ and NEWVAROBJ macros.
53 These are used by the individual routines for object creation.
54 Do not call them otherwise, they do not initialize the object! */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000055
Tim Peters78be7992003-03-23 02:51:01 +000056#ifdef Py_TRACE_REFS
Tim Peters7571a0f2003-03-23 17:52:28 +000057/* Head of circular doubly-linked list of all objects. These are linked
58 * together via the _ob_prev and _ob_next members of a PyObject, which
59 * exist only in a Py_TRACE_REFS build.
60 */
Tim Peters78be7992003-03-23 02:51:01 +000061static PyObject refchain = {&refchain, &refchain};
Tim Peters36eb4df2003-03-23 03:33:13 +000062
Tim Peters7571a0f2003-03-23 17:52:28 +000063/* Insert op at the front of the list of all objects. If force is true,
64 * op is added even if _ob_prev and _ob_next are non-NULL already. If
65 * force is false amd _ob_prev or _ob_next are non-NULL, do nothing.
66 * force should be true if and only if op points to freshly allocated,
67 * uninitialized memory, or you've unlinked op from the list and are
Tim Peters51f8d382003-03-23 18:06:08 +000068 * relinking it into the front.
Tim Peters7571a0f2003-03-23 17:52:28 +000069 * Note that objects are normally added to the list via _Py_NewReference,
70 * which is called by PyObject_Init. Not all objects are initialized that
71 * way, though; exceptions include statically allocated type objects, and
72 * statically allocated singletons (like Py_True and Py_None).
73 */
Tim Peters36eb4df2003-03-23 03:33:13 +000074void
Tim Peters7571a0f2003-03-23 17:52:28 +000075_Py_AddToAllObjects(PyObject *op, int force)
Tim Peters36eb4df2003-03-23 03:33:13 +000076{
Tim Peters7571a0f2003-03-23 17:52:28 +000077#ifdef Py_DEBUG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000078 if (!force) {
79 /* If it's initialized memory, op must be in or out of
80 * the list unambiguously.
81 */
82 assert((op->_ob_prev == NULL) == (op->_ob_next == NULL));
83 }
Tim Peters78be7992003-03-23 02:51:01 +000084#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000085 if (force || op->_ob_prev == NULL) {
86 op->_ob_next = refchain._ob_next;
87 op->_ob_prev = &refchain;
88 refchain._ob_next->_ob_prev = op;
89 refchain._ob_next = op;
90 }
Tim Peters7571a0f2003-03-23 17:52:28 +000091}
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000092#endif /* Py_TRACE_REFS */
Tim Peters78be7992003-03-23 02:51:01 +000093
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +000094#ifdef COUNT_ALLOCS
Guido van Rossumc0b618a1997-05-02 03:12:38 +000095static PyTypeObject *type_list;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000096/* All types are added to type_list, at least when
97 they get one object created. That makes them
98 immortal, which unfortunately contributes to
99 garbage itself. If unlist_types_without_objects
100 is set, they will be removed from the type_list
101 once the last object is deallocated. */
Benjamin Petersona4a37fe2009-01-11 17:13:55 +0000102static int unlist_types_without_objects;
103extern Py_ssize_t tuple_zero_allocs, fast_tuple_allocs;
104extern Py_ssize_t quick_int_allocs, quick_neg_int_allocs;
105extern Py_ssize_t null_strings, one_strings;
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +0000106void
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000107dump_counts(FILE* f)
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +0000108{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000109 PyTypeObject *tp;
Serhiy Storchaka7e160ce2016-07-03 21:03:53 +0300110 PyObject *xoptions, *value;
111 _Py_IDENTIFIER(showalloccount);
112
113 xoptions = PySys_GetXOptions();
114 if (xoptions == NULL)
115 return;
116 value = _PyDict_GetItemId(xoptions, &PyId_showalloccount);
117 if (value != Py_True)
118 return;
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +0000119
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000120 for (tp = type_list; tp; tp = tp->tp_next)
121 fprintf(f, "%s alloc'd: %" PY_FORMAT_SIZE_T "d, "
122 "freed: %" PY_FORMAT_SIZE_T "d, "
123 "max in use: %" PY_FORMAT_SIZE_T "d\n",
124 tp->tp_name, tp->tp_allocs, tp->tp_frees,
125 tp->tp_maxalloc);
126 fprintf(f, "fast tuple allocs: %" PY_FORMAT_SIZE_T "d, "
127 "empty: %" PY_FORMAT_SIZE_T "d\n",
128 fast_tuple_allocs, tuple_zero_allocs);
129 fprintf(f, "fast int allocs: pos: %" PY_FORMAT_SIZE_T "d, "
130 "neg: %" PY_FORMAT_SIZE_T "d\n",
131 quick_int_allocs, quick_neg_int_allocs);
132 fprintf(f, "null strings: %" PY_FORMAT_SIZE_T "d, "
133 "1-strings: %" PY_FORMAT_SIZE_T "d\n",
134 null_strings, one_strings);
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +0000135}
136
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000137PyObject *
Fred Drake100814d2000-07-09 15:48:49 +0000138get_counts(void)
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000139{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000140 PyTypeObject *tp;
141 PyObject *result;
142 PyObject *v;
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000143
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000144 result = PyList_New(0);
145 if (result == NULL)
146 return NULL;
147 for (tp = type_list; tp; tp = tp->tp_next) {
148 v = Py_BuildValue("(snnn)", tp->tp_name, tp->tp_allocs,
149 tp->tp_frees, tp->tp_maxalloc);
150 if (v == NULL) {
151 Py_DECREF(result);
152 return NULL;
153 }
154 if (PyList_Append(result, v) < 0) {
155 Py_DECREF(v);
156 Py_DECREF(result);
157 return NULL;
158 }
159 Py_DECREF(v);
160 }
161 return result;
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000162}
163
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +0000164void
Fred Drake100814d2000-07-09 15:48:49 +0000165inc_count(PyTypeObject *tp)
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +0000166{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000167 if (tp->tp_next == NULL && tp->tp_prev == NULL) {
168 /* first time; insert in linked list */
169 if (tp->tp_next != NULL) /* sanity check */
170 Py_FatalError("XXX inc_count sanity check");
171 if (type_list)
172 type_list->tp_prev = tp;
173 tp->tp_next = type_list;
174 /* Note that as of Python 2.2, heap-allocated type objects
175 * can go away, but this code requires that they stay alive
176 * until program exit. That's why we're careful with
177 * refcounts here. type_list gets a new reference to tp,
178 * while ownership of the reference type_list used to hold
179 * (if any) was transferred to tp->tp_next in the line above.
180 * tp is thus effectively immortal after this.
181 */
182 Py_INCREF(tp);
183 type_list = tp;
Tim Peters3e40c7f2003-03-23 03:04:32 +0000184#ifdef Py_TRACE_REFS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000185 /* Also insert in the doubly-linked list of all objects,
186 * if not already there.
187 */
188 _Py_AddToAllObjects((PyObject *)tp, 0);
Tim Peters78be7992003-03-23 02:51:01 +0000189#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000190 }
191 tp->tp_allocs++;
192 if (tp->tp_allocs - tp->tp_frees > tp->tp_maxalloc)
193 tp->tp_maxalloc = tp->tp_allocs - tp->tp_frees;
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +0000194}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000195
196void dec_count(PyTypeObject *tp)
197{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000198 tp->tp_frees++;
199 if (unlist_types_without_objects &&
200 tp->tp_allocs == tp->tp_frees) {
201 /* unlink the type from type_list */
202 if (tp->tp_prev)
203 tp->tp_prev->tp_next = tp->tp_next;
204 else
205 type_list = tp->tp_next;
206 if (tp->tp_next)
207 tp->tp_next->tp_prev = tp->tp_prev;
208 tp->tp_next = tp->tp_prev = NULL;
209 Py_DECREF(tp);
210 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000211}
212
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +0000213#endif
214
Tim Peters7c321a82002-07-09 02:57:01 +0000215#ifdef Py_REF_DEBUG
216/* Log a fatal error; doesn't return. */
217void
218_Py_NegativeRefcount(const char *fname, int lineno, PyObject *op)
219{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000220 char buf[300];
Tim Peters7c321a82002-07-09 02:57:01 +0000221
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000222 PyOS_snprintf(buf, sizeof(buf),
223 "%s:%i object at %p has negative ref count "
224 "%" PY_FORMAT_SIZE_T "d",
225 fname, lineno, op, op->ob_refcnt);
226 Py_FatalError(buf);
Tim Peters7c321a82002-07-09 02:57:01 +0000227}
228
229#endif /* Py_REF_DEBUG */
230
Thomas Heller1328b522004-04-22 17:23:49 +0000231void
232Py_IncRef(PyObject *o)
233{
234 Py_XINCREF(o);
235}
236
237void
238Py_DecRef(PyObject *o)
239{
240 Py_XDECREF(o);
241}
242
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000243PyObject *
Fred Drake100814d2000-07-09 15:48:49 +0000244PyObject_Init(PyObject *op, PyTypeObject *tp)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000245{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000246 if (op == NULL)
247 return PyErr_NoMemory();
248 /* Any changes should be reflected in PyObject_INIT (objimpl.h) */
249 Py_TYPE(op) = tp;
250 _Py_NewReference(op);
251 return op;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000252}
253
Guido van Rossumb18618d2000-05-03 23:44:39 +0000254PyVarObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000255PyObject_InitVar(PyVarObject *op, PyTypeObject *tp, Py_ssize_t size)
Guido van Rossumb18618d2000-05-03 23:44:39 +0000256{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000257 if (op == NULL)
258 return (PyVarObject *) PyErr_NoMemory();
259 /* Any changes should be reflected in PyObject_INIT_VAR */
260 op->ob_size = size;
261 Py_TYPE(op) = tp;
262 _Py_NewReference((PyObject *)op);
263 return op;
Guido van Rossumb18618d2000-05-03 23:44:39 +0000264}
265
266PyObject *
Fred Drake100814d2000-07-09 15:48:49 +0000267_PyObject_New(PyTypeObject *tp)
Guido van Rossumb18618d2000-05-03 23:44:39 +0000268{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000269 PyObject *op;
270 op = (PyObject *) PyObject_MALLOC(_PyObject_SIZE(tp));
271 if (op == NULL)
272 return PyErr_NoMemory();
273 return PyObject_INIT(op, tp);
Guido van Rossumb18618d2000-05-03 23:44:39 +0000274}
275
Guido van Rossumd0c87ee1997-05-15 21:31:03 +0000276PyVarObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000277_PyObject_NewVar(PyTypeObject *tp, Py_ssize_t nitems)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000278{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000279 PyVarObject *op;
280 const size_t size = _PyObject_VAR_SIZE(tp, nitems);
281 op = (PyVarObject *) PyObject_MALLOC(size);
282 if (op == NULL)
283 return (PyVarObject *)PyErr_NoMemory();
284 return PyObject_INIT_VAR(op, tp, nitems);
Guido van Rossumb18618d2000-05-03 23:44:39 +0000285}
286
Antoine Pitrou796564c2013-07-30 19:59:21 +0200287void
288PyObject_CallFinalizer(PyObject *self)
289{
290 PyTypeObject *tp = Py_TYPE(self);
291
292 /* The former could happen on heaptypes created from the C API, e.g.
293 PyType_FromSpec(). */
294 if (!PyType_HasFeature(tp, Py_TPFLAGS_HAVE_FINALIZE) ||
295 tp->tp_finalize == NULL)
296 return;
297 /* tp_finalize should only be called once. */
298 if (PyType_IS_GC(tp) && _PyGC_FINALIZED(self))
299 return;
300
301 tp->tp_finalize(self);
302 if (PyType_IS_GC(tp))
303 _PyGC_SET_FINALIZED(self, 1);
304}
305
306int
307PyObject_CallFinalizerFromDealloc(PyObject *self)
308{
309 Py_ssize_t refcnt;
310
311 /* Temporarily resurrect the object. */
312 if (self->ob_refcnt != 0) {
313 Py_FatalError("PyObject_CallFinalizerFromDealloc called on "
314 "object with a non-zero refcount");
315 }
316 self->ob_refcnt = 1;
317
318 PyObject_CallFinalizer(self);
319
320 /* Undo the temporary resurrection; can't use DECREF here, it would
321 * cause a recursive call.
322 */
323 assert(self->ob_refcnt > 0);
324 if (--self->ob_refcnt == 0)
325 return 0; /* this is the normal path out */
326
327 /* tp_finalize resurrected it! Make it look like the original Py_DECREF
328 * never happened.
329 */
330 refcnt = self->ob_refcnt;
331 _Py_NewReference(self);
332 self->ob_refcnt = refcnt;
333
334 if (PyType_IS_GC(Py_TYPE(self))) {
335 assert(_PyGC_REFS(self) != _PyGC_REFS_UNTRACKED);
336 }
337 /* If Py_REF_DEBUG, _Py_NewReference bumped _Py_RefTotal, so
338 * we need to undo that. */
339 _Py_DEC_REFTOTAL;
340 /* If Py_TRACE_REFS, _Py_NewReference re-added self to the object
341 * chain, so no more to do there.
342 * If COUNT_ALLOCS, the original decref bumped tp_frees, and
343 * _Py_NewReference bumped tp_allocs: both of those need to be
344 * undone.
345 */
346#ifdef COUNT_ALLOCS
347 --Py_TYPE(self)->tp_frees;
348 --Py_TYPE(self)->tp_allocs;
349#endif
350 return -1;
351}
352
Antoine Pitrouc47bd4a2010-07-27 22:08:27 +0000353int
354PyObject_Print(PyObject *op, FILE *fp, int flags)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000355{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000356 int ret = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000357 if (PyErr_CheckSignals())
358 return -1;
Guido van Rossum9b00dfa1998-04-28 16:06:54 +0000359#ifdef USE_STACKCHECK
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000360 if (PyOS_CheckStack()) {
361 PyErr_SetString(PyExc_MemoryError, "stack overflow");
362 return -1;
363 }
Guido van Rossum9b00dfa1998-04-28 16:06:54 +0000364#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000365 clearerr(fp); /* Clear any previous error condition */
366 if (op == NULL) {
367 Py_BEGIN_ALLOW_THREADS
368 fprintf(fp, "<nil>");
369 Py_END_ALLOW_THREADS
370 }
371 else {
372 if (op->ob_refcnt <= 0)
373 /* XXX(twouters) cast refcount to long until %zd is
374 universally available */
375 Py_BEGIN_ALLOW_THREADS
376 fprintf(fp, "<refcnt %ld at %p>",
377 (long)op->ob_refcnt, op);
378 Py_END_ALLOW_THREADS
379 else {
380 PyObject *s;
381 if (flags & Py_PRINT_RAW)
382 s = PyObject_Str(op);
383 else
384 s = PyObject_Repr(op);
385 if (s == NULL)
386 ret = -1;
387 else if (PyBytes_Check(s)) {
388 fwrite(PyBytes_AS_STRING(s), 1,
389 PyBytes_GET_SIZE(s), fp);
390 }
391 else if (PyUnicode_Check(s)) {
392 PyObject *t;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200393 t = PyUnicode_AsEncodedString(s, "utf-8", "backslashreplace");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000394 if (t == NULL)
395 ret = 0;
396 else {
397 fwrite(PyBytes_AS_STRING(t), 1,
398 PyBytes_GET_SIZE(t), fp);
Victor Stinnerba6b4302010-05-17 09:33:42 +0000399 Py_DECREF(t);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000400 }
401 }
402 else {
403 PyErr_Format(PyExc_TypeError,
404 "str() or repr() returned '%.100s'",
405 s->ob_type->tp_name);
406 ret = -1;
407 }
408 Py_XDECREF(s);
409 }
410 }
411 if (ret == 0) {
412 if (ferror(fp)) {
Serhiy Storchaka55fe1ae2017-04-16 10:46:38 +0300413 PyErr_SetFromErrno(PyExc_OSError);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000414 clearerr(fp);
415 ret = -1;
416 }
417 }
418 return ret;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000419}
420
Guido van Rossum38938152006-08-21 23:36:26 +0000421/* For debugging convenience. Set a breakpoint here and call it from your DLL */
422void
Thomas Woutersb2137042007-02-01 18:02:27 +0000423_Py_BreakPoint(void)
Guido van Rossum38938152006-08-21 23:36:26 +0000424{
425}
426
Neal Norwitz1a997502003-01-13 20:13:12 +0000427
Barry Warsaw9bf16442001-01-23 16:24:35 +0000428/* For debugging convenience. See Misc/gdbinit for some useful gdb hooks */
Guido van Rossum38938152006-08-21 23:36:26 +0000429void
430_PyObject_Dump(PyObject* op)
Barry Warsaw9bf16442001-01-23 16:24:35 +0000431{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000432 if (op == NULL)
433 fprintf(stderr, "NULL\n");
434 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000435 PyGILState_STATE gil;
Victor Stinnere5132102013-08-26 13:49:06 +0200436 PyObject *error_type, *error_value, *error_traceback;
437
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000438 fprintf(stderr, "object : ");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000439 gil = PyGILState_Ensure();
Victor Stinnere5132102013-08-26 13:49:06 +0200440
441 PyErr_Fetch(&error_type, &error_value, &error_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000442 (void)PyObject_Print(op, stderr, 0);
Victor Stinnere5132102013-08-26 13:49:06 +0200443 PyErr_Restore(error_type, error_value, error_traceback);
444
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000445 PyGILState_Release(gil);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000446 /* XXX(twouters) cast refcount to long until %zd is
447 universally available */
448 fprintf(stderr, "\n"
449 "type : %s\n"
450 "refcount: %ld\n"
451 "address : %p\n",
452 Py_TYPE(op)==NULL ? "NULL" : Py_TYPE(op)->tp_name,
453 (long)op->ob_refcnt,
454 op);
455 }
Barry Warsaw9bf16442001-01-23 16:24:35 +0000456}
Barry Warsaw903138f2001-01-23 16:33:18 +0000457
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000458PyObject *
Fred Drake100814d2000-07-09 15:48:49 +0000459PyObject_Repr(PyObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000460{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000461 PyObject *res;
462 if (PyErr_CheckSignals())
463 return NULL;
Guido van Rossum9b00dfa1998-04-28 16:06:54 +0000464#ifdef USE_STACKCHECK
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000465 if (PyOS_CheckStack()) {
466 PyErr_SetString(PyExc_MemoryError, "stack overflow");
467 return NULL;
468 }
Guido van Rossum9b00dfa1998-04-28 16:06:54 +0000469#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000470 if (v == NULL)
471 return PyUnicode_FromString("<NULL>");
472 if (Py_TYPE(v)->tp_repr == NULL)
473 return PyUnicode_FromFormat("<%s object at %p>",
474 v->ob_type->tp_name, v);
Victor Stinner33824f62013-08-26 14:05:19 +0200475
476#ifdef Py_DEBUG
477 /* PyObject_Repr() must not be called with an exception set,
Victor Stinnera8cb5152017-01-18 14:12:51 +0100478 because it can clear it (directly or indirectly) and so the
Martin Panter9955a372015-10-07 10:26:23 +0000479 caller loses its exception */
Victor Stinner33824f62013-08-26 14:05:19 +0200480 assert(!PyErr_Occurred());
481#endif
482
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000483 res = (*v->ob_type->tp_repr)(v);
Victor Stinner0a54cf12011-12-01 03:22:44 +0100484 if (res == NULL)
485 return NULL;
486 if (!PyUnicode_Check(res)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000487 PyErr_Format(PyExc_TypeError,
488 "__repr__ returned non-string (type %.200s)",
489 res->ob_type->tp_name);
490 Py_DECREF(res);
491 return NULL;
492 }
Victor Stinnerdb88ae52011-12-01 02:15:00 +0100493#ifndef Py_DEBUG
494 if (PyUnicode_READY(res) < 0)
495 return NULL;
496#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000497 return res;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000498}
499
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000500PyObject *
Guido van Rossum98297ee2007-11-06 21:34:58 +0000501PyObject_Str(PyObject *v)
Guido van Rossumc6004111993-11-05 10:22:19 +0000502{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000503 PyObject *res;
504 if (PyErr_CheckSignals())
505 return NULL;
Guido van Rossum98297ee2007-11-06 21:34:58 +0000506#ifdef USE_STACKCHECK
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000507 if (PyOS_CheckStack()) {
508 PyErr_SetString(PyExc_MemoryError, "stack overflow");
509 return NULL;
510 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000511#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000512 if (v == NULL)
513 return PyUnicode_FromString("<NULL>");
514 if (PyUnicode_CheckExact(v)) {
Victor Stinnerdb88ae52011-12-01 02:15:00 +0100515#ifndef Py_DEBUG
Victor Stinner4ead7c72011-11-20 19:48:36 +0100516 if (PyUnicode_READY(v) < 0)
517 return NULL;
Victor Stinnerdb88ae52011-12-01 02:15:00 +0100518#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000519 Py_INCREF(v);
520 return v;
521 }
522 if (Py_TYPE(v)->tp_str == NULL)
523 return PyObject_Repr(v);
Guido van Rossum4f288ab2001-05-01 16:53:37 +0000524
Victor Stinner33824f62013-08-26 14:05:19 +0200525#ifdef Py_DEBUG
526 /* PyObject_Str() must not be called with an exception set,
Victor Stinnera8cb5152017-01-18 14:12:51 +0100527 because it can clear it (directly or indirectly) and so the
Nick Coghland979e432014-02-09 10:43:21 +1000528 caller loses its exception */
Victor Stinner33824f62013-08-26 14:05:19 +0200529 assert(!PyErr_Occurred());
530#endif
531
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000532 /* It is possible for a type to have a tp_str representation that loops
533 infinitely. */
534 if (Py_EnterRecursiveCall(" while getting the str of an object"))
535 return NULL;
536 res = (*Py_TYPE(v)->tp_str)(v);
537 Py_LeaveRecursiveCall();
538 if (res == NULL)
539 return NULL;
540 if (!PyUnicode_Check(res)) {
541 PyErr_Format(PyExc_TypeError,
542 "__str__ returned non-string (type %.200s)",
543 Py_TYPE(res)->tp_name);
544 Py_DECREF(res);
545 return NULL;
546 }
Victor Stinnerdb88ae52011-12-01 02:15:00 +0100547#ifndef Py_DEBUG
Victor Stinner4ead7c72011-11-20 19:48:36 +0100548 if (PyUnicode_READY(res) < 0)
549 return NULL;
Victor Stinnerdb88ae52011-12-01 02:15:00 +0100550#endif
Victor Stinner4ead7c72011-11-20 19:48:36 +0100551 assert(_PyUnicode_CheckConsistency(res, 1));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000552 return res;
Neil Schemenauercf52c072005-08-12 17:34:58 +0000553}
554
Georg Brandl559e5d72008-06-11 18:37:52 +0000555PyObject *
556PyObject_ASCII(PyObject *v)
557{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000558 PyObject *repr, *ascii, *res;
Georg Brandl559e5d72008-06-11 18:37:52 +0000559
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000560 repr = PyObject_Repr(v);
561 if (repr == NULL)
562 return NULL;
Georg Brandl559e5d72008-06-11 18:37:52 +0000563
Victor Stinneraf037572013-04-14 18:44:10 +0200564 if (PyUnicode_IS_ASCII(repr))
565 return repr;
566
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000567 /* repr is guaranteed to be a PyUnicode object by PyObject_Repr */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200568 ascii = _PyUnicode_AsASCIIString(repr, "backslashreplace");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000569 Py_DECREF(repr);
570 if (ascii == NULL)
571 return NULL;
Georg Brandl559e5d72008-06-11 18:37:52 +0000572
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000573 res = PyUnicode_DecodeASCII(
574 PyBytes_AS_STRING(ascii),
575 PyBytes_GET_SIZE(ascii),
576 NULL);
577
578 Py_DECREF(ascii);
579 return res;
Georg Brandl559e5d72008-06-11 18:37:52 +0000580}
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000581
Benjamin Petersonc15a0732008-08-26 16:46:47 +0000582PyObject *
583PyObject_Bytes(PyObject *v)
584{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000585 PyObject *result, *func;
Benjamin Petersonc15a0732008-08-26 16:46:47 +0000586
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000587 if (v == NULL)
588 return PyBytes_FromString("<NULL>");
Benjamin Petersonc15a0732008-08-26 16:46:47 +0000589
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000590 if (PyBytes_CheckExact(v)) {
591 Py_INCREF(v);
592 return v;
593 }
Benjamin Petersonc15a0732008-08-26 16:46:47 +0000594
Benjamin Petersonce798522012-01-22 11:24:29 -0500595 func = _PyObject_LookupSpecial(v, &PyId___bytes__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000596 if (func != NULL) {
Victor Stinnerf17c3de2016-12-06 18:46:19 +0100597 result = _PyObject_CallNoArg(func);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000598 Py_DECREF(func);
599 if (result == NULL)
Benjamin Peterson41ece392010-09-11 16:39:57 +0000600 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000601 if (!PyBytes_Check(result)) {
Benjamin Peterson41ece392010-09-11 16:39:57 +0000602 PyErr_Format(PyExc_TypeError,
603 "__bytes__ returned non-bytes (type %.200s)",
604 Py_TYPE(result)->tp_name);
605 Py_DECREF(result);
606 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000607 }
608 return result;
609 }
610 else if (PyErr_Occurred())
611 return NULL;
612 return PyBytes_FromObject(v);
Benjamin Petersonc15a0732008-08-26 16:46:47 +0000613}
614
Mark Dickinsonc008a172009-02-01 13:59:22 +0000615/* For Python 3.0.1 and later, the old three-way comparison has been
616 completely removed in favour of rich comparisons. PyObject_Compare() and
617 PyObject_Cmp() are gone, and the builtin cmp function no longer exists.
Mark Dickinsone94c6792009-02-02 20:36:42 +0000618 The old tp_compare slot has been renamed to tp_reserved, and should no
Mark Dickinsonc008a172009-02-01 13:59:22 +0000619 longer be used. Use tp_richcompare instead.
Guido van Rossum98297ee2007-11-06 21:34:58 +0000620
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000621 See (*) below for practical amendments.
622
Mark Dickinsonc008a172009-02-01 13:59:22 +0000623 tp_richcompare gets called with a first argument of the appropriate type
624 and a second object of an arbitrary type. We never do any kind of
625 coercion.
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000626
Mark Dickinsonc008a172009-02-01 13:59:22 +0000627 The tp_richcompare slot should return an object, as follows:
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000628
629 NULL if an exception occurred
630 NotImplemented if the requested comparison is not implemented
631 any other false value if the requested comparison is false
632 any other true value if the requested comparison is true
633
634 The PyObject_RichCompare[Bool]() wrappers raise TypeError when they get
635 NotImplemented.
636
637 (*) Practical amendments:
638
639 - If rich comparison returns NotImplemented, == and != are decided by
640 comparing the object pointer (i.e. falling back to the base object
641 implementation).
642
Guido van Rossuma4073002002-05-31 20:03:54 +0000643*/
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000644
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000645/* Map rich comparison operators to their swapped version, e.g. LT <--> GT */
Brett Cannona5ca2e72004-09-25 01:37:24 +0000646int _Py_SwappedOp[] = {Py_GT, Py_GE, Py_EQ, Py_NE, Py_LT, Py_LE};
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +0000647
Serhiy Storchaka2d06e842015-12-25 19:53:18 +0200648static const char * const opstrings[] = {"<", "<=", "==", "!=", ">", ">="};
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000649
650/* Perform a rich comparison, raising TypeError when the requested comparison
651 operator is not supported. */
Guido van Rossume797ec12001-01-17 15:24:28 +0000652static PyObject *
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000653do_richcompare(PyObject *v, PyObject *w, int op)
Guido van Rossume797ec12001-01-17 15:24:28 +0000654{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000655 richcmpfunc f;
656 PyObject *res;
657 int checked_reverse_op = 0;
Guido van Rossume797ec12001-01-17 15:24:28 +0000658
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000659 if (v->ob_type != w->ob_type &&
660 PyType_IsSubtype(w->ob_type, v->ob_type) &&
661 (f = w->ob_type->tp_richcompare) != NULL) {
662 checked_reverse_op = 1;
663 res = (*f)(w, v, _Py_SwappedOp[op]);
664 if (res != Py_NotImplemented)
665 return res;
666 Py_DECREF(res);
667 }
668 if ((f = v->ob_type->tp_richcompare) != NULL) {
669 res = (*f)(v, w, op);
670 if (res != Py_NotImplemented)
671 return res;
672 Py_DECREF(res);
673 }
674 if (!checked_reverse_op && (f = w->ob_type->tp_richcompare) != NULL) {
675 res = (*f)(w, v, _Py_SwappedOp[op]);
676 if (res != Py_NotImplemented)
677 return res;
678 Py_DECREF(res);
679 }
680 /* If neither object implements it, provide a sensible default
681 for == and !=, but raise an exception for ordering. */
682 switch (op) {
683 case Py_EQ:
684 res = (v == w) ? Py_True : Py_False;
685 break;
686 case Py_NE:
687 res = (v != w) ? Py_True : Py_False;
688 break;
689 default:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000690 PyErr_Format(PyExc_TypeError,
Victor Stinner91108f02015-10-14 18:25:31 +0200691 "'%s' not supported between instances of '%.100s' and '%.100s'",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000692 opstrings[op],
Victor Stinner91108f02015-10-14 18:25:31 +0200693 v->ob_type->tp_name,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000694 w->ob_type->tp_name);
695 return NULL;
696 }
697 Py_INCREF(res);
698 return res;
Guido van Rossume797ec12001-01-17 15:24:28 +0000699}
700
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000701/* Perform a rich comparison with object result. This wraps do_richcompare()
702 with a check for NULL arguments and a recursion check. */
Guido van Rossume797ec12001-01-17 15:24:28 +0000703
Guido van Rossume797ec12001-01-17 15:24:28 +0000704PyObject *
705PyObject_RichCompare(PyObject *v, PyObject *w, int op)
706{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000707 PyObject *res;
Guido van Rossume797ec12001-01-17 15:24:28 +0000708
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000709 assert(Py_LT <= op && op <= Py_GE);
710 if (v == NULL || w == NULL) {
711 if (!PyErr_Occurred())
712 PyErr_BadInternalCall();
713 return NULL;
714 }
715 if (Py_EnterRecursiveCall(" in comparison"))
716 return NULL;
717 res = do_richcompare(v, w, op);
718 Py_LeaveRecursiveCall();
719 return res;
Guido van Rossume797ec12001-01-17 15:24:28 +0000720}
721
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000722/* Perform a rich comparison with integer result. This wraps
723 PyObject_RichCompare(), returning -1 for error, 0 for false, 1 for true. */
Guido van Rossume797ec12001-01-17 15:24:28 +0000724int
725PyObject_RichCompareBool(PyObject *v, PyObject *w, int op)
726{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000727 PyObject *res;
728 int ok;
Guido van Rossume797ec12001-01-17 15:24:28 +0000729
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000730 /* Quick result when objects are the same.
731 Guarantees that identity implies equality. */
732 if (v == w) {
733 if (op == Py_EQ)
734 return 1;
735 else if (op == Py_NE)
736 return 0;
737 }
Mark Dickinson4a1f5932008-11-12 23:23:36 +0000738
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000739 res = PyObject_RichCompare(v, w, op);
740 if (res == NULL)
741 return -1;
742 if (PyBool_Check(res))
743 ok = (res == Py_True);
744 else
745 ok = PyObject_IsTrue(res);
746 Py_DECREF(res);
747 return ok;
Guido van Rossume797ec12001-01-17 15:24:28 +0000748}
Fred Drake13634cf2000-06-29 19:17:04 +0000749
Antoine Pitrouce4a9da2011-11-21 20:46:33 +0100750Py_hash_t
Nick Coghland1abd252008-07-15 15:46:38 +0000751PyObject_HashNotImplemented(PyObject *v)
752{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000753 PyErr_Format(PyExc_TypeError, "unhashable type: '%.200s'",
754 Py_TYPE(v)->tp_name);
755 return -1;
Nick Coghland1abd252008-07-15 15:46:38 +0000756}
Fred Drake13634cf2000-06-29 19:17:04 +0000757
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000758Py_hash_t
Fred Drake100814d2000-07-09 15:48:49 +0000759PyObject_Hash(PyObject *v)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000760{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000761 PyTypeObject *tp = Py_TYPE(v);
762 if (tp->tp_hash != NULL)
763 return (*tp->tp_hash)(v);
764 /* To keep to the general practice that inheriting
765 * solely from object in C code should work without
766 * an explicit call to PyType_Ready, we implicitly call
767 * PyType_Ready here and then check the tp_hash slot again
768 */
769 if (tp->tp_dict == NULL) {
770 if (PyType_Ready(tp) < 0)
771 return -1;
772 if (tp->tp_hash != NULL)
773 return (*tp->tp_hash)(v);
774 }
775 /* Otherwise, the object can't be hashed */
776 return PyObject_HashNotImplemented(v);
Guido van Rossum9bfef441993-03-29 10:43:31 +0000777}
778
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000779PyObject *
Jeremy Hyltonaf68c872005-12-10 18:50:16 +0000780PyObject_GetAttrString(PyObject *v, const char *name)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000781{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000782 PyObject *w, *res;
Guido van Rossumd8eb1b31996-08-09 20:52:03 +0000783
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000784 if (Py_TYPE(v)->tp_getattr != NULL)
785 return (*Py_TYPE(v)->tp_getattr)(v, (char*)name);
INADA Naoki3e8d6cb2017-02-21 23:57:25 +0900786 w = PyUnicode_FromString(name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000787 if (w == NULL)
788 return NULL;
789 res = PyObject_GetAttr(v, w);
Victor Stinner59af08f2012-03-22 02:09:08 +0100790 Py_DECREF(w);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000791 return res;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000792}
793
794int
Jeremy Hyltonaf68c872005-12-10 18:50:16 +0000795PyObject_HasAttrString(PyObject *v, const char *name)
Guido van Rossumed18fdc1993-07-11 19:55:34 +0000796{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000797 PyObject *res = PyObject_GetAttrString(v, name);
798 if (res != NULL) {
799 Py_DECREF(res);
800 return 1;
801 }
802 PyErr_Clear();
803 return 0;
Guido van Rossumed18fdc1993-07-11 19:55:34 +0000804}
805
806int
Jeremy Hyltonaf68c872005-12-10 18:50:16 +0000807PyObject_SetAttrString(PyObject *v, const char *name, PyObject *w)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000808{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000809 PyObject *s;
810 int res;
Guido van Rossumd8eb1b31996-08-09 20:52:03 +0000811
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000812 if (Py_TYPE(v)->tp_setattr != NULL)
813 return (*Py_TYPE(v)->tp_setattr)(v, (char*)name, w);
814 s = PyUnicode_InternFromString(name);
815 if (s == NULL)
816 return -1;
817 res = PyObject_SetAttr(v, s, w);
818 Py_XDECREF(s);
819 return res;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000820}
821
Benjamin Petersonbfebb7b2011-12-15 15:34:02 -0500822int
823_PyObject_IsAbstract(PyObject *obj)
824{
825 int res;
826 PyObject* isabstract;
Benjamin Petersonbfebb7b2011-12-15 15:34:02 -0500827
828 if (obj == NULL)
829 return 0;
830
831 isabstract = _PyObject_GetAttrId(obj, &PyId___isabstractmethod__);
832 if (isabstract == NULL) {
833 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
834 PyErr_Clear();
835 return 0;
836 }
837 return -1;
838 }
839 res = PyObject_IsTrue(isabstract);
840 Py_DECREF(isabstract);
841 return res;
842}
843
Guido van Rossum98ff96a1997-05-20 18:34:44 +0000844PyObject *
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200845_PyObject_GetAttrId(PyObject *v, _Py_Identifier *name)
846{
847 PyObject *result;
Martin v. Löwisd10759f2011-11-07 13:00:05 +0100848 PyObject *oname = _PyUnicode_FromId(name); /* borrowed */
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200849 if (!oname)
850 return NULL;
851 result = PyObject_GetAttr(v, oname);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200852 return result;
853}
854
855int
856_PyObject_HasAttrId(PyObject *v, _Py_Identifier *name)
857{
858 int result;
Martin v. Löwisd10759f2011-11-07 13:00:05 +0100859 PyObject *oname = _PyUnicode_FromId(name); /* borrowed */
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200860 if (!oname)
861 return -1;
862 result = PyObject_HasAttr(v, oname);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200863 return result;
864}
865
866int
867_PyObject_SetAttrId(PyObject *v, _Py_Identifier *name, PyObject *w)
868{
869 int result;
Martin v. Löwisd10759f2011-11-07 13:00:05 +0100870 PyObject *oname = _PyUnicode_FromId(name); /* borrowed */
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200871 if (!oname)
872 return -1;
873 result = PyObject_SetAttr(v, oname, w);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200874 return result;
875}
876
877PyObject *
Fred Drake100814d2000-07-09 15:48:49 +0000878PyObject_GetAttr(PyObject *v, PyObject *name)
Guido van Rossum98ff96a1997-05-20 18:34:44 +0000879{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000880 PyTypeObject *tp = Py_TYPE(v);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000881
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000882 if (!PyUnicode_Check(name)) {
883 PyErr_Format(PyExc_TypeError,
884 "attribute name must be string, not '%.200s'",
885 name->ob_type->tp_name);
886 return NULL;
887 }
888 if (tp->tp_getattro != NULL)
889 return (*tp->tp_getattro)(v, name);
890 if (tp->tp_getattr != NULL) {
Serhiy Storchaka2a404b62017-01-22 23:07:07 +0200891 const char *name_str = PyUnicode_AsUTF8(name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000892 if (name_str == NULL)
893 return NULL;
Serhiy Storchaka2a404b62017-01-22 23:07:07 +0200894 return (*tp->tp_getattr)(v, (char *)name_str);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000895 }
896 PyErr_Format(PyExc_AttributeError,
897 "'%.50s' object has no attribute '%U'",
898 tp->tp_name, name);
899 return NULL;
Guido van Rossum98ff96a1997-05-20 18:34:44 +0000900}
901
902int
Fred Drake100814d2000-07-09 15:48:49 +0000903PyObject_HasAttr(PyObject *v, PyObject *name)
Guido van Rossum98ff96a1997-05-20 18:34:44 +0000904{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000905 PyObject *res = PyObject_GetAttr(v, name);
906 if (res != NULL) {
907 Py_DECREF(res);
908 return 1;
909 }
910 PyErr_Clear();
911 return 0;
Guido van Rossum98ff96a1997-05-20 18:34:44 +0000912}
913
914int
Fred Drake100814d2000-07-09 15:48:49 +0000915PyObject_SetAttr(PyObject *v, PyObject *name, PyObject *value)
Guido van Rossum98ff96a1997-05-20 18:34:44 +0000916{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000917 PyTypeObject *tp = Py_TYPE(v);
918 int err;
Marc-André Lemburge44e5072000-09-18 16:20:57 +0000919
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000920 if (!PyUnicode_Check(name)) {
921 PyErr_Format(PyExc_TypeError,
922 "attribute name must be string, not '%.200s'",
923 name->ob_type->tp_name);
924 return -1;
925 }
926 Py_INCREF(name);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000927
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000928 PyUnicode_InternInPlace(&name);
929 if (tp->tp_setattro != NULL) {
930 err = (*tp->tp_setattro)(v, name, value);
931 Py_DECREF(name);
932 return err;
933 }
934 if (tp->tp_setattr != NULL) {
Serhiy Storchaka2a404b62017-01-22 23:07:07 +0200935 const char *name_str = PyUnicode_AsUTF8(name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000936 if (name_str == NULL)
937 return -1;
Serhiy Storchaka2a404b62017-01-22 23:07:07 +0200938 err = (*tp->tp_setattr)(v, (char *)name_str, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000939 Py_DECREF(name);
940 return err;
941 }
942 Py_DECREF(name);
943 assert(name->ob_refcnt >= 1);
944 if (tp->tp_getattr == NULL && tp->tp_getattro == NULL)
945 PyErr_Format(PyExc_TypeError,
946 "'%.100s' object has no attributes "
947 "(%s .%U)",
948 tp->tp_name,
949 value==NULL ? "del" : "assign to",
950 name);
951 else
952 PyErr_Format(PyExc_TypeError,
953 "'%.100s' object has only read-only attributes "
954 "(%s .%U)",
955 tp->tp_name,
956 value==NULL ? "del" : "assign to",
957 name);
958 return -1;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000959}
960
961/* Helper to get a pointer to an object's __dict__ slot, if any */
962
963PyObject **
964_PyObject_GetDictPtr(PyObject *obj)
965{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000966 Py_ssize_t dictoffset;
967 PyTypeObject *tp = Py_TYPE(obj);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000968
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000969 dictoffset = tp->tp_dictoffset;
970 if (dictoffset == 0)
971 return NULL;
972 if (dictoffset < 0) {
973 Py_ssize_t tsize;
974 size_t size;
Guido van Rossum2eb0b872002-03-01 22:24:49 +0000975
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000976 tsize = ((PyVarObject *)obj)->ob_size;
977 if (tsize < 0)
978 tsize = -tsize;
979 size = _PyObject_VAR_SIZE(tp, tsize);
Guido van Rossum2eb0b872002-03-01 22:24:49 +0000980
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000981 dictoffset += (long)size;
982 assert(dictoffset > 0);
983 assert(dictoffset % SIZEOF_VOID_P == 0);
984 }
985 return (PyObject **) ((char *)obj + dictoffset);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000986}
987
Tim Peters6d6c1a32001-08-02 04:15:00 +0000988PyObject *
Raymond Hettinger1da1dbf2003-03-17 19:46:11 +0000989PyObject_SelfIter(PyObject *obj)
Raymond Hettinger01538262003-03-17 08:24:35 +0000990{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000991 Py_INCREF(obj);
992 return obj;
Raymond Hettinger01538262003-03-17 08:24:35 +0000993}
994
Antoine Pitroua7013882012-04-05 00:04:20 +0200995/* Convenience function to get a builtin from its name */
996PyObject *
997_PyObject_GetBuiltin(const char *name)
998{
Victor Stinner53e9ec42013-11-07 00:43:05 +0100999 PyObject *mod_name, *mod, *attr;
1000
Victor Stinnerbd303c12013-11-07 23:07:29 +01001001 mod_name = _PyUnicode_FromId(&PyId_builtins); /* borrowed */
Victor Stinner53e9ec42013-11-07 00:43:05 +01001002 if (mod_name == NULL)
1003 return NULL;
1004 mod = PyImport_Import(mod_name);
Antoine Pitroua7013882012-04-05 00:04:20 +02001005 if (mod == NULL)
1006 return NULL;
1007 attr = PyObject_GetAttrString(mod, name);
1008 Py_DECREF(mod);
1009 return attr;
1010}
1011
Amaury Forgeot d'Arcf343e012009-01-12 23:58:21 +00001012/* Helper used when the __next__ method is removed from a type:
1013 tp_iternext is never NULL and can be safely called without checking
1014 on every iteration.
1015 */
1016
1017PyObject *
1018_PyObject_NextNotImplemented(PyObject *self)
1019{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001020 PyErr_Format(PyExc_TypeError,
1021 "'%.200s' object is not iterable",
1022 Py_TYPE(self)->tp_name);
1023 return NULL;
Amaury Forgeot d'Arcf343e012009-01-12 23:58:21 +00001024}
1025
Yury Selivanovf2392132016-12-13 19:03:51 -05001026
1027/* Specialized version of _PyObject_GenericGetAttrWithDict
1028 specifically for the LOAD_METHOD opcode.
1029
1030 Return 1 if a method is found, 0 if it's a regular attribute
1031 from __dict__ or something returned by using a descriptor
1032 protocol.
1033
1034 `method` will point to the resolved attribute or NULL. In the
1035 latter case, an error will be set.
1036*/
1037int
1038_PyObject_GetMethod(PyObject *obj, PyObject *name, PyObject **method)
1039{
1040 PyTypeObject *tp = Py_TYPE(obj);
1041 PyObject *descr;
1042 descrgetfunc f = NULL;
1043 PyObject **dictptr, *dict;
1044 PyObject *attr;
1045 int meth_found = 0;
1046
1047 assert(*method == NULL);
1048
1049 if (Py_TYPE(obj)->tp_getattro != PyObject_GenericGetAttr
1050 || !PyUnicode_Check(name)) {
1051 *method = PyObject_GetAttr(obj, name);
1052 return 0;
1053 }
1054
1055 if (tp->tp_dict == NULL && PyType_Ready(tp) < 0)
1056 return 0;
1057
1058 descr = _PyType_Lookup(tp, name);
1059 if (descr != NULL) {
1060 Py_INCREF(descr);
INADA Naoki5566bbb2017-02-03 07:43:03 +09001061 if (PyFunction_Check(descr) ||
1062 (Py_TYPE(descr) == &PyMethodDescr_Type)) {
Yury Selivanovf2392132016-12-13 19:03:51 -05001063 meth_found = 1;
1064 } else {
1065 f = descr->ob_type->tp_descr_get;
1066 if (f != NULL && PyDescr_IsData(descr)) {
1067 *method = f(descr, obj, (PyObject *)obj->ob_type);
1068 Py_DECREF(descr);
1069 return 0;
1070 }
1071 }
1072 }
1073
1074 dictptr = _PyObject_GetDictPtr(obj);
1075 if (dictptr != NULL && (dict = *dictptr) != NULL) {
1076 Py_INCREF(dict);
1077 attr = PyDict_GetItem(dict, name);
1078 if (attr != NULL) {
1079 Py_INCREF(attr);
1080 *method = attr;
1081 Py_DECREF(dict);
1082 Py_XDECREF(descr);
1083 return 0;
1084 }
1085 Py_DECREF(dict);
1086 }
1087
1088 if (meth_found) {
1089 *method = descr;
1090 return 1;
1091 }
1092
1093 if (f != NULL) {
1094 *method = f(descr, obj, (PyObject *)Py_TYPE(obj));
1095 Py_DECREF(descr);
1096 return 0;
1097 }
1098
1099 if (descr != NULL) {
1100 *method = descr;
1101 return 0;
1102 }
1103
1104 PyErr_Format(PyExc_AttributeError,
1105 "'%.50s' object has no attribute '%U'",
1106 tp->tp_name, name);
1107 return 0;
1108}
1109
1110/* Generic GetAttr functions - put these in your tp_[gs]etattro slot. */
Michael W. Hudson1593f502004-09-14 17:09:47 +00001111
Raymond Hettinger01538262003-03-17 08:24:35 +00001112PyObject *
Antoine Pitrou1a9a9d52010-08-28 18:17:03 +00001113_PyObject_GenericGetAttrWithDict(PyObject *obj, PyObject *name, PyObject *dict)
Tim Peters6d6c1a32001-08-02 04:15:00 +00001114{
Yury Selivanovf2392132016-12-13 19:03:51 -05001115 /* Make sure the logic of _PyObject_GetMethod is in sync with
1116 this method.
1117 */
1118
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001119 PyTypeObject *tp = Py_TYPE(obj);
1120 PyObject *descr = NULL;
1121 PyObject *res = NULL;
1122 descrgetfunc f;
1123 Py_ssize_t dictoffset;
1124 PyObject **dictptr;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001125
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001126 if (!PyUnicode_Check(name)){
1127 PyErr_Format(PyExc_TypeError,
1128 "attribute name must be string, not '%.200s'",
1129 name->ob_type->tp_name);
1130 return NULL;
1131 }
Serhiy Storchaka55c861f2016-04-17 20:31:51 +03001132 Py_INCREF(name);
Guido van Rossumebca9fc2001-12-04 15:54:53 +00001133
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001134 if (tp->tp_dict == NULL) {
1135 if (PyType_Ready(tp) < 0)
1136 goto done;
1137 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001138
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001139 descr = _PyType_Lookup(tp, name);
Michael W. Hudsonb2c7de42003-08-15 13:07:47 +00001140
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001141 f = NULL;
1142 if (descr != NULL) {
Serhiy Storchaka55c861f2016-04-17 20:31:51 +03001143 Py_INCREF(descr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001144 f = descr->ob_type->tp_descr_get;
1145 if (f != NULL && PyDescr_IsData(descr)) {
1146 res = f(descr, obj, (PyObject *)obj->ob_type);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001147 goto done;
1148 }
1149 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001150
Antoine Pitrou1a9a9d52010-08-28 18:17:03 +00001151 if (dict == NULL) {
1152 /* Inline _PyObject_GetDictPtr */
1153 dictoffset = tp->tp_dictoffset;
1154 if (dictoffset != 0) {
1155 if (dictoffset < 0) {
1156 Py_ssize_t tsize;
1157 size_t size;
Guido van Rossumc66ff442002-08-19 16:50:48 +00001158
Antoine Pitrou1a9a9d52010-08-28 18:17:03 +00001159 tsize = ((PyVarObject *)obj)->ob_size;
1160 if (tsize < 0)
1161 tsize = -tsize;
1162 size = _PyObject_VAR_SIZE(tp, tsize);
Serhiy Storchaka55c861f2016-04-17 20:31:51 +03001163 assert(size <= PY_SSIZE_T_MAX);
Guido van Rossumc66ff442002-08-19 16:50:48 +00001164
Serhiy Storchaka55c861f2016-04-17 20:31:51 +03001165 dictoffset += (Py_ssize_t)size;
Antoine Pitrou1a9a9d52010-08-28 18:17:03 +00001166 assert(dictoffset > 0);
1167 assert(dictoffset % SIZEOF_VOID_P == 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001168 }
Antoine Pitrou1a9a9d52010-08-28 18:17:03 +00001169 dictptr = (PyObject **) ((char *)obj + dictoffset);
1170 dict = *dictptr;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001171 }
1172 }
Antoine Pitrou1a9a9d52010-08-28 18:17:03 +00001173 if (dict != NULL) {
1174 Py_INCREF(dict);
1175 res = PyDict_GetItem(dict, name);
1176 if (res != NULL) {
1177 Py_INCREF(res);
Antoine Pitrou1a9a9d52010-08-28 18:17:03 +00001178 Py_DECREF(dict);
1179 goto done;
1180 }
1181 Py_DECREF(dict);
1182 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001183
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001184 if (f != NULL) {
1185 res = f(descr, obj, (PyObject *)Py_TYPE(obj));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001186 goto done;
1187 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001188
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001189 if (descr != NULL) {
1190 res = descr;
Victor Stinner2d01dc02012-03-09 00:44:13 +01001191 descr = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001192 goto done;
1193 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001194
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001195 PyErr_Format(PyExc_AttributeError,
1196 "'%.50s' object has no attribute '%U'",
1197 tp->tp_name, name);
Guido van Rossumebca9fc2001-12-04 15:54:53 +00001198 done:
Victor Stinner2d01dc02012-03-09 00:44:13 +01001199 Py_XDECREF(descr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001200 Py_DECREF(name);
1201 return res;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001202}
1203
Antoine Pitrou1a9a9d52010-08-28 18:17:03 +00001204PyObject *
1205PyObject_GenericGetAttr(PyObject *obj, PyObject *name)
1206{
1207 return _PyObject_GenericGetAttrWithDict(obj, name, NULL);
1208}
1209
Tim Peters6d6c1a32001-08-02 04:15:00 +00001210int
Antoine Pitrou1a9a9d52010-08-28 18:17:03 +00001211_PyObject_GenericSetAttrWithDict(PyObject *obj, PyObject *name,
1212 PyObject *value, PyObject *dict)
Tim Peters6d6c1a32001-08-02 04:15:00 +00001213{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001214 PyTypeObject *tp = Py_TYPE(obj);
1215 PyObject *descr;
1216 descrsetfunc f;
1217 PyObject **dictptr;
1218 int res = -1;
Guido van Rossumebca9fc2001-12-04 15:54:53 +00001219
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001220 if (!PyUnicode_Check(name)){
1221 PyErr_Format(PyExc_TypeError,
1222 "attribute name must be string, not '%.200s'",
1223 name->ob_type->tp_name);
1224 return -1;
1225 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001226
Benjamin Peterson74529ad2012-03-09 07:25:32 -08001227 if (tp->tp_dict == NULL && PyType_Ready(tp) < 0)
1228 return -1;
1229
1230 Py_INCREF(name);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001231
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001232 descr = _PyType_Lookup(tp, name);
Victor Stinner2d01dc02012-03-09 00:44:13 +01001233
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001234 if (descr != NULL) {
Serhiy Storchaka55c861f2016-04-17 20:31:51 +03001235 Py_INCREF(descr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001236 f = descr->ob_type->tp_descr_set;
Serhiy Storchaka55c861f2016-04-17 20:31:51 +03001237 if (f != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001238 res = f(descr, obj, value);
1239 goto done;
1240 }
1241 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001242
Antoine Pitrou1a9a9d52010-08-28 18:17:03 +00001243 if (dict == NULL) {
1244 dictptr = _PyObject_GetDictPtr(obj);
Serhiy Storchaka55c861f2016-04-17 20:31:51 +03001245 if (dictptr == NULL) {
1246 if (descr == NULL) {
1247 PyErr_Format(PyExc_AttributeError,
1248 "'%.100s' object has no attribute '%U'",
1249 tp->tp_name, name);
1250 }
1251 else {
1252 PyErr_Format(PyExc_AttributeError,
1253 "'%.50s' object attribute '%U' is read-only",
1254 tp->tp_name, name);
1255 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001256 goto done;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001257 }
Serhiy Storchaka55c861f2016-04-17 20:31:51 +03001258 res = _PyObjectDict_SetItem(tp, dictptr, name, value);
Antoine Pitrou1a9a9d52010-08-28 18:17:03 +00001259 }
Serhiy Storchaka55c861f2016-04-17 20:31:51 +03001260 else {
Antoine Pitrou1a9a9d52010-08-28 18:17:03 +00001261 Py_INCREF(dict);
1262 if (value == NULL)
1263 res = PyDict_DelItem(dict, name);
1264 else
1265 res = PyDict_SetItem(dict, name, value);
Benjamin Peterson74529ad2012-03-09 07:25:32 -08001266 Py_DECREF(dict);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001267 }
Serhiy Storchaka55c861f2016-04-17 20:31:51 +03001268 if (res < 0 && PyErr_ExceptionMatches(PyExc_KeyError))
1269 PyErr_SetObject(PyExc_AttributeError, name);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001270
Guido van Rossumebca9fc2001-12-04 15:54:53 +00001271 done:
Victor Stinner2d01dc02012-03-09 00:44:13 +01001272 Py_XDECREF(descr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001273 Py_DECREF(name);
1274 return res;
Guido van Rossum98ff96a1997-05-20 18:34:44 +00001275}
1276
Antoine Pitrou1a9a9d52010-08-28 18:17:03 +00001277int
1278PyObject_GenericSetAttr(PyObject *obj, PyObject *name, PyObject *value)
1279{
1280 return _PyObject_GenericSetAttrWithDict(obj, name, value, NULL);
1281}
1282
Benjamin Peterson8eb12692012-02-19 19:59:10 -05001283int
1284PyObject_GenericSetDict(PyObject *obj, PyObject *value, void *context)
1285{
Serhiy Storchaka576f1322016-01-05 21:27:54 +02001286 PyObject **dictptr = _PyObject_GetDictPtr(obj);
Benjamin Peterson8eb12692012-02-19 19:59:10 -05001287 if (dictptr == NULL) {
1288 PyErr_SetString(PyExc_AttributeError,
1289 "This object has no __dict__");
1290 return -1;
1291 }
1292 if (value == NULL) {
1293 PyErr_SetString(PyExc_TypeError, "cannot delete __dict__");
1294 return -1;
1295 }
1296 if (!PyDict_Check(value)) {
1297 PyErr_Format(PyExc_TypeError,
1298 "__dict__ must be set to a dictionary, "
1299 "not a '%.200s'", Py_TYPE(value)->tp_name);
1300 return -1;
1301 }
Serhiy Storchaka576f1322016-01-05 21:27:54 +02001302 Py_INCREF(value);
Serhiy Storchakaec397562016-04-06 09:50:03 +03001303 Py_XSETREF(*dictptr, value);
Benjamin Peterson8eb12692012-02-19 19:59:10 -05001304 return 0;
1305}
1306
Antoine Pitrou1a9a9d52010-08-28 18:17:03 +00001307
Guido van Rossum6ac258d1993-05-12 08:24:20 +00001308/* Test a value used as condition, e.g., in a for or if statement.
1309 Return -1 if an error occurred */
1310
1311int
Fred Drake100814d2000-07-09 15:48:49 +00001312PyObject_IsTrue(PyObject *v)
Guido van Rossum6ac258d1993-05-12 08:24:20 +00001313{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001314 Py_ssize_t res;
1315 if (v == Py_True)
1316 return 1;
1317 if (v == Py_False)
1318 return 0;
1319 if (v == Py_None)
1320 return 0;
1321 else if (v->ob_type->tp_as_number != NULL &&
1322 v->ob_type->tp_as_number->nb_bool != NULL)
1323 res = (*v->ob_type->tp_as_number->nb_bool)(v);
1324 else if (v->ob_type->tp_as_mapping != NULL &&
1325 v->ob_type->tp_as_mapping->mp_length != NULL)
1326 res = (*v->ob_type->tp_as_mapping->mp_length)(v);
1327 else if (v->ob_type->tp_as_sequence != NULL &&
1328 v->ob_type->tp_as_sequence->sq_length != NULL)
1329 res = (*v->ob_type->tp_as_sequence->sq_length)(v);
1330 else
1331 return 1;
1332 /* if it is negative, it should be either -1 or -2 */
1333 return (res > 0) ? 1 : Py_SAFE_DOWNCAST(res, Py_ssize_t, int);
Guido van Rossum6ac258d1993-05-12 08:24:20 +00001334}
1335
Tim Peters803526b2002-07-07 05:13:56 +00001336/* equivalent of 'not v'
Guido van Rossumc3d3f961998-04-09 17:53:59 +00001337 Return -1 if an error occurred */
1338
1339int
Fred Drake100814d2000-07-09 15:48:49 +00001340PyObject_Not(PyObject *v)
Guido van Rossumc3d3f961998-04-09 17:53:59 +00001341{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001342 int res;
1343 res = PyObject_IsTrue(v);
1344 if (res < 0)
1345 return res;
1346 return res == 0;
Guido van Rossumc3d3f961998-04-09 17:53:59 +00001347}
1348
Guido van Rossum49b11fe1995-01-26 00:38:22 +00001349/* Test whether an object can be called */
1350
1351int
Fred Drake100814d2000-07-09 15:48:49 +00001352PyCallable_Check(PyObject *x)
Guido van Rossum49b11fe1995-01-26 00:38:22 +00001353{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001354 if (x == NULL)
1355 return 0;
1356 return x->ob_type->tp_call != NULL;
Guido van Rossum49b11fe1995-01-26 00:38:22 +00001357}
1358
Tim Peters7eea37e2001-09-04 22:08:56 +00001359
Georg Brandle32b4222007-03-10 22:13:27 +00001360/* Helper for PyObject_Dir without arguments: returns the local scope. */
1361static PyObject *
Guido van Rossumad7d8d12007-04-13 01:39:34 +00001362_dir_locals(void)
Tim Peters305b5852001-09-17 02:38:46 +00001363{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001364 PyObject *names;
Victor Stinner41bb43a2013-10-29 01:19:37 +01001365 PyObject *locals;
Tim Peters305b5852001-09-17 02:38:46 +00001366
Victor Stinner41bb43a2013-10-29 01:19:37 +01001367 locals = PyEval_GetLocals();
1368 if (locals == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001369 return NULL;
Tim Peters305b5852001-09-17 02:38:46 +00001370
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001371 names = PyMapping_Keys(locals);
1372 if (!names)
1373 return NULL;
1374 if (!PyList_Check(names)) {
1375 PyErr_Format(PyExc_TypeError,
1376 "dir(): expected keys() of locals to be a list, "
1377 "not '%.200s'", Py_TYPE(names)->tp_name);
1378 Py_DECREF(names);
1379 return NULL;
1380 }
Benjamin Peterson3bbb7222011-06-11 16:12:08 -05001381 if (PyList_Sort(names)) {
1382 Py_DECREF(names);
1383 return NULL;
1384 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001385 /* the locals don't need to be DECREF'd */
1386 return names;
Georg Brandle32b4222007-03-10 22:13:27 +00001387}
1388
Benjamin Peterson82b00c12011-05-24 11:09:06 -05001389/* Helper for PyObject_Dir: object introspection. */
Georg Brandle32b4222007-03-10 22:13:27 +00001390static PyObject *
1391_dir_object(PyObject *obj)
1392{
Benjamin Peterson3bbb7222011-06-11 16:12:08 -05001393 PyObject *result, *sorted;
Benjamin Petersonce798522012-01-22 11:24:29 -05001394 PyObject *dirfunc = _PyObject_LookupSpecial(obj, &PyId___dir__);
Georg Brandle32b4222007-03-10 22:13:27 +00001395
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001396 assert(obj);
1397 if (dirfunc == NULL) {
Benjamin Peterson82b00c12011-05-24 11:09:06 -05001398 if (!PyErr_Occurred())
1399 PyErr_SetString(PyExc_TypeError, "object does not provide __dir__");
1400 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001401 }
Benjamin Peterson82b00c12011-05-24 11:09:06 -05001402 /* use __dir__ */
Victor Stinnerf17c3de2016-12-06 18:46:19 +01001403 result = _PyObject_CallNoArg(dirfunc);
Benjamin Peterson82b00c12011-05-24 11:09:06 -05001404 Py_DECREF(dirfunc);
1405 if (result == NULL)
1406 return NULL;
Benjamin Peterson3bbb7222011-06-11 16:12:08 -05001407 /* return sorted(result) */
1408 sorted = PySequence_List(result);
1409 Py_DECREF(result);
1410 if (sorted == NULL)
1411 return NULL;
1412 if (PyList_Sort(sorted)) {
1413 Py_DECREF(sorted);
1414 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001415 }
Benjamin Peterson3bbb7222011-06-11 16:12:08 -05001416 return sorted;
Georg Brandle32b4222007-03-10 22:13:27 +00001417}
1418
1419/* Implementation of dir() -- if obj is NULL, returns the names in the current
1420 (local) scope. Otherwise, performs introspection of the object: returns a
1421 sorted list of attribute names (supposedly) accessible from the object
1422*/
1423PyObject *
1424PyObject_Dir(PyObject *obj)
1425{
Benjamin Peterson3bbb7222011-06-11 16:12:08 -05001426 return (obj == NULL) ? _dir_locals() : _dir_object(obj);
Tim Peters7eea37e2001-09-04 22:08:56 +00001427}
Guido van Rossum49b11fe1995-01-26 00:38:22 +00001428
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001429/*
Raymond Hettinger66d2be82011-07-28 09:55:13 -07001430None is a non-NULL undefined value.
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001431There is (and should be!) no way to create other objects of this type,
Guido van Rossum3f5da241990-12-20 15:06:42 +00001432so there is exactly one (which is indestructible, by the way).
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001433*/
1434
Guido van Rossum0c182a11992-03-27 17:26:13 +00001435/* ARGSUSED */
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001436static PyObject *
Fred Drake100814d2000-07-09 15:48:49 +00001437none_repr(PyObject *op)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001438{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001439 return PyUnicode_FromString("None");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001440}
1441
Barry Warsaw9bf16442001-01-23 16:24:35 +00001442/* ARGUSED */
1443static void
Tim Peters803526b2002-07-07 05:13:56 +00001444none_dealloc(PyObject* ignore)
Barry Warsaw9bf16442001-01-23 16:24:35 +00001445{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001446 /* This should never get called, but we also don't want to SEGV if
1447 * we accidentally decref None out of existence.
1448 */
1449 Py_FatalError("deallocating None");
Barry Warsaw9bf16442001-01-23 16:24:35 +00001450}
1451
Benjamin Petersonc4607ae2011-07-29 18:19:43 -05001452static PyObject *
1453none_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
1454{
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +02001455 if (PyTuple_GET_SIZE(args) || (kwargs && PyDict_GET_SIZE(kwargs))) {
Benjamin Petersonc4607ae2011-07-29 18:19:43 -05001456 PyErr_SetString(PyExc_TypeError, "NoneType takes no arguments");
1457 return NULL;
1458 }
1459 Py_RETURN_NONE;
1460}
1461
Raymond Hettinger66d2be82011-07-28 09:55:13 -07001462static int
1463none_bool(PyObject *v)
1464{
1465 return 0;
1466}
1467
1468static PyNumberMethods none_as_number = {
1469 0, /* nb_add */
1470 0, /* nb_subtract */
1471 0, /* nb_multiply */
1472 0, /* nb_remainder */
1473 0, /* nb_divmod */
1474 0, /* nb_power */
1475 0, /* nb_negative */
1476 0, /* nb_positive */
1477 0, /* nb_absolute */
1478 (inquiry)none_bool, /* nb_bool */
1479 0, /* nb_invert */
1480 0, /* nb_lshift */
1481 0, /* nb_rshift */
1482 0, /* nb_and */
1483 0, /* nb_xor */
1484 0, /* nb_or */
1485 0, /* nb_int */
1486 0, /* nb_reserved */
1487 0, /* nb_float */
1488 0, /* nb_inplace_add */
1489 0, /* nb_inplace_subtract */
1490 0, /* nb_inplace_multiply */
1491 0, /* nb_inplace_remainder */
1492 0, /* nb_inplace_power */
1493 0, /* nb_inplace_lshift */
1494 0, /* nb_inplace_rshift */
1495 0, /* nb_inplace_and */
1496 0, /* nb_inplace_xor */
1497 0, /* nb_inplace_or */
1498 0, /* nb_floor_divide */
1499 0, /* nb_true_divide */
1500 0, /* nb_inplace_floor_divide */
1501 0, /* nb_inplace_true_divide */
1502 0, /* nb_index */
1503};
Barry Warsaw9bf16442001-01-23 16:24:35 +00001504
Alexandre Vassalotti65846c62013-11-30 17:55:48 -08001505PyTypeObject _PyNone_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001506 PyVarObject_HEAD_INIT(&PyType_Type, 0)
1507 "NoneType",
1508 0,
1509 0,
1510 none_dealloc, /*tp_dealloc*/ /*never called*/
1511 0, /*tp_print*/
1512 0, /*tp_getattr*/
1513 0, /*tp_setattr*/
1514 0, /*tp_reserved*/
1515 none_repr, /*tp_repr*/
Raymond Hettinger66d2be82011-07-28 09:55:13 -07001516 &none_as_number, /*tp_as_number*/
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001517 0, /*tp_as_sequence*/
1518 0, /*tp_as_mapping*/
1519 0, /*tp_hash */
Benjamin Petersonc4607ae2011-07-29 18:19:43 -05001520 0, /*tp_call */
1521 0, /*tp_str */
1522 0, /*tp_getattro */
1523 0, /*tp_setattro */
1524 0, /*tp_as_buffer */
1525 Py_TPFLAGS_DEFAULT, /*tp_flags */
1526 0, /*tp_doc */
1527 0, /*tp_traverse */
1528 0, /*tp_clear */
1529 0, /*tp_richcompare */
1530 0, /*tp_weaklistoffset */
1531 0, /*tp_iter */
1532 0, /*tp_iternext */
1533 0, /*tp_methods */
1534 0, /*tp_members */
1535 0, /*tp_getset */
1536 0, /*tp_base */
1537 0, /*tp_dict */
1538 0, /*tp_descr_get */
1539 0, /*tp_descr_set */
1540 0, /*tp_dictoffset */
1541 0, /*tp_init */
1542 0, /*tp_alloc */
1543 none_new, /*tp_new */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001544};
1545
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001546PyObject _Py_NoneStruct = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001547 _PyObject_EXTRA_INIT
Alexandre Vassalotti65846c62013-11-30 17:55:48 -08001548 1, &_PyNone_Type
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001549};
1550
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +00001551/* NotImplemented is an object that can be used to signal that an
1552 operation is not implemented for the given type combination. */
1553
1554static PyObject *
1555NotImplemented_repr(PyObject *op)
1556{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001557 return PyUnicode_FromString("NotImplemented");
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +00001558}
1559
Benjamin Peterson18d7d7a2011-07-29 18:27:44 -05001560static PyObject *
Alexandre Vassalottic49477b2013-11-24 02:53:45 -08001561NotImplemented_reduce(PyObject *op)
1562{
1563 return PyUnicode_FromString("NotImplemented");
1564}
1565
1566static PyMethodDef notimplemented_methods[] = {
1567 {"__reduce__", (PyCFunction)NotImplemented_reduce, METH_NOARGS, NULL},
1568 {NULL, NULL}
1569};
1570
1571static PyObject *
Benjamin Peterson18d7d7a2011-07-29 18:27:44 -05001572notimplemented_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
1573{
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +02001574 if (PyTuple_GET_SIZE(args) || (kwargs && PyDict_GET_SIZE(kwargs))) {
Benjamin Peterson18d7d7a2011-07-29 18:27:44 -05001575 PyErr_SetString(PyExc_TypeError, "NotImplementedType takes no arguments");
1576 return NULL;
1577 }
Brian Curtindfc80e32011-08-10 20:28:54 -05001578 Py_RETURN_NOTIMPLEMENTED;
Benjamin Peterson18d7d7a2011-07-29 18:27:44 -05001579}
1580
Armin Ronacher226b1db2012-10-06 14:28:58 +02001581static void
1582notimplemented_dealloc(PyObject* ignore)
1583{
1584 /* This should never get called, but we also don't want to SEGV if
1585 * we accidentally decref NotImplemented out of existence.
1586 */
1587 Py_FatalError("deallocating NotImplemented");
1588}
1589
Alexandre Vassalotti65846c62013-11-30 17:55:48 -08001590PyTypeObject _PyNotImplemented_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001591 PyVarObject_HEAD_INIT(&PyType_Type, 0)
1592 "NotImplementedType",
1593 0,
1594 0,
Armin Ronacher226b1db2012-10-06 14:28:58 +02001595 notimplemented_dealloc, /*tp_dealloc*/ /*never called*/
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001596 0, /*tp_print*/
1597 0, /*tp_getattr*/
1598 0, /*tp_setattr*/
1599 0, /*tp_reserved*/
1600 NotImplemented_repr, /*tp_repr*/
1601 0, /*tp_as_number*/
1602 0, /*tp_as_sequence*/
1603 0, /*tp_as_mapping*/
1604 0, /*tp_hash */
Benjamin Peterson18d7d7a2011-07-29 18:27:44 -05001605 0, /*tp_call */
1606 0, /*tp_str */
1607 0, /*tp_getattro */
1608 0, /*tp_setattro */
1609 0, /*tp_as_buffer */
1610 Py_TPFLAGS_DEFAULT, /*tp_flags */
1611 0, /*tp_doc */
1612 0, /*tp_traverse */
1613 0, /*tp_clear */
1614 0, /*tp_richcompare */
1615 0, /*tp_weaklistoffset */
1616 0, /*tp_iter */
1617 0, /*tp_iternext */
Alexandre Vassalottic49477b2013-11-24 02:53:45 -08001618 notimplemented_methods, /*tp_methods */
Benjamin Peterson18d7d7a2011-07-29 18:27:44 -05001619 0, /*tp_members */
1620 0, /*tp_getset */
1621 0, /*tp_base */
1622 0, /*tp_dict */
1623 0, /*tp_descr_get */
1624 0, /*tp_descr_set */
1625 0, /*tp_dictoffset */
1626 0, /*tp_init */
1627 0, /*tp_alloc */
1628 notimplemented_new, /*tp_new */
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +00001629};
1630
1631PyObject _Py_NotImplementedStruct = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001632 _PyObject_EXTRA_INIT
Alexandre Vassalotti65846c62013-11-30 17:55:48 -08001633 1, &_PyNotImplemented_Type
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +00001634};
1635
Guido van Rossumba21a492001-08-16 08:17:26 +00001636void
1637_Py_ReadyTypes(void)
1638{
Victor Stinner5a1bb4e2014-06-02 14:10:59 +02001639 if (PyType_Ready(&PyBaseObject_Type) < 0)
1640 Py_FatalError("Can't initialize object type");
1641
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001642 if (PyType_Ready(&PyType_Type) < 0)
1643 Py_FatalError("Can't initialize type type");
Guido van Rossumba21a492001-08-16 08:17:26 +00001644
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001645 if (PyType_Ready(&_PyWeakref_RefType) < 0)
1646 Py_FatalError("Can't initialize weakref type");
Fred Drake0a4dd392004-07-02 18:57:45 +00001647
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001648 if (PyType_Ready(&_PyWeakref_CallableProxyType) < 0)
1649 Py_FatalError("Can't initialize callable weakref proxy type");
Benjamin Petersonfd838e62009-04-20 02:09:13 +00001650
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001651 if (PyType_Ready(&_PyWeakref_ProxyType) < 0)
1652 Py_FatalError("Can't initialize weakref proxy type");
Benjamin Petersonfd838e62009-04-20 02:09:13 +00001653
Victor Stinner5a1bb4e2014-06-02 14:10:59 +02001654 if (PyType_Ready(&PyLong_Type) < 0)
1655 Py_FatalError("Can't initialize int type");
1656
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001657 if (PyType_Ready(&PyBool_Type) < 0)
1658 Py_FatalError("Can't initialize bool type");
Guido van Rossum77f6a652002-04-03 22:41:51 +00001659
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001660 if (PyType_Ready(&PyByteArray_Type) < 0)
1661 Py_FatalError("Can't initialize bytearray type");
Guido van Rossum4dfe8a12006-04-22 23:28:04 +00001662
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001663 if (PyType_Ready(&PyBytes_Type) < 0)
1664 Py_FatalError("Can't initialize 'str'");
Guido van Rossumcacfc072002-05-24 19:01:59 +00001665
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001666 if (PyType_Ready(&PyList_Type) < 0)
1667 Py_FatalError("Can't initialize list type");
Guido van Rossumba21a492001-08-16 08:17:26 +00001668
Alexandre Vassalotti65846c62013-11-30 17:55:48 -08001669 if (PyType_Ready(&_PyNone_Type) < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001670 Py_FatalError("Can't initialize None type");
Guido van Rossumba21a492001-08-16 08:17:26 +00001671
Alexandre Vassalotti65846c62013-11-30 17:55:48 -08001672 if (PyType_Ready(&_PyNotImplemented_Type) < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001673 Py_FatalError("Can't initialize NotImplemented type");
Benjamin Petersonae937c02009-04-18 20:54:08 +00001674
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001675 if (PyType_Ready(&PyTraceBack_Type) < 0)
1676 Py_FatalError("Can't initialize traceback type");
Benjamin Petersonae937c02009-04-18 20:54:08 +00001677
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001678 if (PyType_Ready(&PySuper_Type) < 0)
1679 Py_FatalError("Can't initialize super type");
Benjamin Petersonae937c02009-04-18 20:54:08 +00001680
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001681 if (PyType_Ready(&PyRange_Type) < 0)
1682 Py_FatalError("Can't initialize range type");
Benjamin Petersonae937c02009-04-18 20:54:08 +00001683
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001684 if (PyType_Ready(&PyDict_Type) < 0)
1685 Py_FatalError("Can't initialize dict type");
Benjamin Petersonae937c02009-04-18 20:54:08 +00001686
Benjamin Petersondb87c992016-11-06 13:01:07 -08001687 if (PyType_Ready(&PyDictKeys_Type) < 0)
1688 Py_FatalError("Can't initialize dict keys type");
1689
1690 if (PyType_Ready(&PyDictValues_Type) < 0)
1691 Py_FatalError("Can't initialize dict values type");
1692
1693 if (PyType_Ready(&PyDictItems_Type) < 0)
1694 Py_FatalError("Can't initialize dict items type");
1695
Eric Snow96c6af92015-05-29 22:21:39 -06001696 if (PyType_Ready(&PyODict_Type) < 0)
1697 Py_FatalError("Can't initialize OrderedDict type");
1698
1699 if (PyType_Ready(&PyODictKeys_Type) < 0)
1700 Py_FatalError("Can't initialize odict_keys type");
1701
1702 if (PyType_Ready(&PyODictItems_Type) < 0)
1703 Py_FatalError("Can't initialize odict_items type");
1704
1705 if (PyType_Ready(&PyODictValues_Type) < 0)
1706 Py_FatalError("Can't initialize odict_values type");
1707
1708 if (PyType_Ready(&PyODictIter_Type) < 0)
1709 Py_FatalError("Can't initialize odict_keyiterator type");
1710
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001711 if (PyType_Ready(&PySet_Type) < 0)
1712 Py_FatalError("Can't initialize set type");
Benjamin Petersonae937c02009-04-18 20:54:08 +00001713
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001714 if (PyType_Ready(&PyUnicode_Type) < 0)
1715 Py_FatalError("Can't initialize str type");
Benjamin Petersonae937c02009-04-18 20:54:08 +00001716
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001717 if (PyType_Ready(&PySlice_Type) < 0)
1718 Py_FatalError("Can't initialize slice type");
Benjamin Petersonae937c02009-04-18 20:54:08 +00001719
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001720 if (PyType_Ready(&PyStaticMethod_Type) < 0)
1721 Py_FatalError("Can't initialize static method type");
Benjamin Petersonae937c02009-04-18 20:54:08 +00001722
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001723 if (PyType_Ready(&PyComplex_Type) < 0)
1724 Py_FatalError("Can't initialize complex type");
Skip Montanaroba1e0f42009-10-18 14:25:35 +00001725
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001726 if (PyType_Ready(&PyFloat_Type) < 0)
1727 Py_FatalError("Can't initialize float type");
Benjamin Petersonae937c02009-04-18 20:54:08 +00001728
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001729 if (PyType_Ready(&PyFrozenSet_Type) < 0)
1730 Py_FatalError("Can't initialize frozenset type");
Benjamin Petersonae937c02009-04-18 20:54:08 +00001731
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001732 if (PyType_Ready(&PyProperty_Type) < 0)
1733 Py_FatalError("Can't initialize property type");
Benjamin Petersonae937c02009-04-18 20:54:08 +00001734
Stefan Krah9a2d99e2012-02-25 12:24:21 +01001735 if (PyType_Ready(&_PyManagedBuffer_Type) < 0)
1736 Py_FatalError("Can't initialize managed buffer type");
1737
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001738 if (PyType_Ready(&PyMemoryView_Type) < 0)
1739 Py_FatalError("Can't initialize memoryview type");
Benjamin Petersonae937c02009-04-18 20:54:08 +00001740
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001741 if (PyType_Ready(&PyTuple_Type) < 0)
1742 Py_FatalError("Can't initialize tuple type");
Benjamin Petersonae937c02009-04-18 20:54:08 +00001743
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001744 if (PyType_Ready(&PyEnum_Type) < 0)
1745 Py_FatalError("Can't initialize enumerate type");
Benjamin Petersonae937c02009-04-18 20:54:08 +00001746
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001747 if (PyType_Ready(&PyReversed_Type) < 0)
1748 Py_FatalError("Can't initialize reversed type");
Guido van Rossum47b9ff62006-08-24 00:41:19 +00001749
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001750 if (PyType_Ready(&PyStdPrinter_Type) < 0)
1751 Py_FatalError("Can't initialize StdPrinter");
Benjamin Petersonfd838e62009-04-20 02:09:13 +00001752
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001753 if (PyType_Ready(&PyCode_Type) < 0)
1754 Py_FatalError("Can't initialize code type");
Benjamin Petersonfd838e62009-04-20 02:09:13 +00001755
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001756 if (PyType_Ready(&PyFrame_Type) < 0)
1757 Py_FatalError("Can't initialize frame type");
Benjamin Petersonfd838e62009-04-20 02:09:13 +00001758
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001759 if (PyType_Ready(&PyCFunction_Type) < 0)
1760 Py_FatalError("Can't initialize builtin function type");
Benjamin Petersonfd838e62009-04-20 02:09:13 +00001761
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001762 if (PyType_Ready(&PyMethod_Type) < 0)
1763 Py_FatalError("Can't initialize method type");
Benjamin Petersonfd838e62009-04-20 02:09:13 +00001764
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001765 if (PyType_Ready(&PyFunction_Type) < 0)
1766 Py_FatalError("Can't initialize function type");
Benjamin Petersonfd838e62009-04-20 02:09:13 +00001767
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001768 if (PyType_Ready(&PyDictProxy_Type) < 0)
1769 Py_FatalError("Can't initialize dict proxy type");
Benjamin Petersonfd838e62009-04-20 02:09:13 +00001770
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001771 if (PyType_Ready(&PyGen_Type) < 0)
1772 Py_FatalError("Can't initialize generator type");
Benjamin Petersonfd838e62009-04-20 02:09:13 +00001773
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001774 if (PyType_Ready(&PyGetSetDescr_Type) < 0)
1775 Py_FatalError("Can't initialize get-set descriptor type");
Benjamin Petersonfd838e62009-04-20 02:09:13 +00001776
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001777 if (PyType_Ready(&PyWrapperDescr_Type) < 0)
1778 Py_FatalError("Can't initialize wrapper type");
Benjamin Petersonfd838e62009-04-20 02:09:13 +00001779
Benjamin Petersoneff61f62011-09-01 16:32:31 -04001780 if (PyType_Ready(&_PyMethodWrapper_Type) < 0)
1781 Py_FatalError("Can't initialize method wrapper type");
1782
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001783 if (PyType_Ready(&PyEllipsis_Type) < 0)
1784 Py_FatalError("Can't initialize ellipsis type");
Benjamin Petersonfd838e62009-04-20 02:09:13 +00001785
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001786 if (PyType_Ready(&PyMemberDescr_Type) < 0)
1787 Py_FatalError("Can't initialize member descriptor type");
Benjamin Peterson8bc5b682009-05-09 18:10:51 +00001788
Barry Warsaw409da152012-06-03 16:18:47 -04001789 if (PyType_Ready(&_PyNamespace_Type) < 0)
1790 Py_FatalError("Can't initialize namespace type");
Benjamin Petersone8ea97f2012-10-30 23:27:52 -04001791
Benjamin Petersonc4311282012-10-30 23:21:10 -04001792 if (PyType_Ready(&PyCapsule_Type) < 0)
1793 Py_FatalError("Can't initialize capsule type");
1794
1795 if (PyType_Ready(&PyLongRangeIter_Type) < 0)
1796 Py_FatalError("Can't initialize long range iterator type");
1797
1798 if (PyType_Ready(&PyCell_Type) < 0)
1799 Py_FatalError("Can't initialize cell type");
1800
1801 if (PyType_Ready(&PyInstanceMethod_Type) < 0)
1802 Py_FatalError("Can't initialize instance method type");
1803
1804 if (PyType_Ready(&PyClassMethodDescr_Type) < 0)
1805 Py_FatalError("Can't initialize class method descr type");
1806
1807 if (PyType_Ready(&PyMethodDescr_Type) < 0)
1808 Py_FatalError("Can't initialize method descr type");
1809
1810 if (PyType_Ready(&PyCallIter_Type) < 0)
1811 Py_FatalError("Can't initialize call iter type");
1812
1813 if (PyType_Ready(&PySeqIter_Type) < 0)
1814 Py_FatalError("Can't initialize sequence iterator type");
Yury Selivanov5376ba92015-06-22 12:19:30 -04001815
1816 if (PyType_Ready(&PyCoro_Type) < 0)
1817 Py_FatalError("Can't initialize coroutine type");
1818
1819 if (PyType_Ready(&_PyCoroWrapper_Type) < 0)
1820 Py_FatalError("Can't initialize coroutine wrapper type");
Guido van Rossumba21a492001-08-16 08:17:26 +00001821}
1822
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001823
Guido van Rossum84a90321996-05-22 16:34:47 +00001824#ifdef Py_TRACE_REFS
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001825
Guido van Rossumaacdc9d1996-08-12 21:32:12 +00001826void
Fred Drake100814d2000-07-09 15:48:49 +00001827_Py_NewReference(PyObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001828{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001829 _Py_INC_REFTOTAL;
1830 op->ob_refcnt = 1;
1831 _Py_AddToAllObjects(op, 1);
1832 _Py_INC_TPALLOCS(op);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001833}
1834
Guido van Rossumaacdc9d1996-08-12 21:32:12 +00001835void
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02001836_Py_ForgetReference(PyObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001837{
Guido van Rossumbffd6832000-01-20 22:32:56 +00001838#ifdef SLOW_UNREF_CHECK
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02001839 PyObject *p;
Guido van Rossumbffd6832000-01-20 22:32:56 +00001840#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001841 if (op->ob_refcnt < 0)
1842 Py_FatalError("UNREF negative refcnt");
1843 if (op == &refchain ||
1844 op->_ob_prev->_ob_next != op || op->_ob_next->_ob_prev != op) {
1845 fprintf(stderr, "* ob\n");
1846 _PyObject_Dump(op);
1847 fprintf(stderr, "* op->_ob_prev->_ob_next\n");
1848 _PyObject_Dump(op->_ob_prev->_ob_next);
1849 fprintf(stderr, "* op->_ob_next->_ob_prev\n");
1850 _PyObject_Dump(op->_ob_next->_ob_prev);
1851 Py_FatalError("UNREF invalid object");
1852 }
Guido van Rossum2e8f6141992-09-03 20:32:55 +00001853#ifdef SLOW_UNREF_CHECK
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001854 for (p = refchain._ob_next; p != &refchain; p = p->_ob_next) {
1855 if (p == op)
1856 break;
1857 }
1858 if (p == &refchain) /* Not found */
1859 Py_FatalError("UNREF unknown object");
Guido van Rossum2e8f6141992-09-03 20:32:55 +00001860#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001861 op->_ob_next->_ob_prev = op->_ob_prev;
1862 op->_ob_prev->_ob_next = op->_ob_next;
1863 op->_ob_next = op->_ob_prev = NULL;
1864 _Py_INC_TPFREES(op);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001865}
1866
Guido van Rossumaacdc9d1996-08-12 21:32:12 +00001867void
Fred Drake100814d2000-07-09 15:48:49 +00001868_Py_Dealloc(PyObject *op)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001869{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001870 destructor dealloc = Py_TYPE(op)->tp_dealloc;
1871 _Py_ForgetReference(op);
1872 (*dealloc)(op);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001873}
1874
Tim Peters269b2a62003-04-17 19:52:29 +00001875/* Print all live objects. Because PyObject_Print is called, the
1876 * interpreter must be in a healthy state.
1877 */
Guido van Rossumaacdc9d1996-08-12 21:32:12 +00001878void
Fred Drake100814d2000-07-09 15:48:49 +00001879_Py_PrintReferences(FILE *fp)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001880{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001881 PyObject *op;
1882 fprintf(fp, "Remaining objects:\n");
1883 for (op = refchain._ob_next; op != &refchain; op = op->_ob_next) {
1884 fprintf(fp, "%p [%" PY_FORMAT_SIZE_T "d] ", op, op->ob_refcnt);
1885 if (PyObject_Print(op, fp, 0) != 0)
1886 PyErr_Clear();
1887 putc('\n', fp);
1888 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001889}
1890
Tim Peters269b2a62003-04-17 19:52:29 +00001891/* Print the addresses of all live objects. Unlike _Py_PrintReferences, this
1892 * doesn't make any calls to the Python C API, so is always safe to call.
1893 */
1894void
1895_Py_PrintReferenceAddresses(FILE *fp)
1896{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001897 PyObject *op;
1898 fprintf(fp, "Remaining object addresses:\n");
1899 for (op = refchain._ob_next; op != &refchain; op = op->_ob_next)
1900 fprintf(fp, "%p [%" PY_FORMAT_SIZE_T "d] %s\n", op,
1901 op->ob_refcnt, Py_TYPE(op)->tp_name);
Tim Peters269b2a62003-04-17 19:52:29 +00001902}
1903
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +00001904PyObject *
Fred Drake100814d2000-07-09 15:48:49 +00001905_Py_GetObjects(PyObject *self, PyObject *args)
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +00001906{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001907 int i, n;
1908 PyObject *t = NULL;
1909 PyObject *res, *op;
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +00001910
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001911 if (!PyArg_ParseTuple(args, "i|O", &n, &t))
1912 return NULL;
1913 op = refchain._ob_next;
1914 res = PyList_New(0);
1915 if (res == NULL)
1916 return NULL;
1917 for (i = 0; (n == 0 || i < n) && op != &refchain; i++) {
1918 while (op == self || op == args || op == res || op == t ||
1919 (t != NULL && Py_TYPE(op) != (PyTypeObject *) t)) {
1920 op = op->_ob_next;
1921 if (op == &refchain)
1922 return res;
1923 }
1924 if (PyList_Append(res, op) < 0) {
1925 Py_DECREF(res);
1926 return NULL;
1927 }
1928 op = op->_ob_next;
1929 }
1930 return res;
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +00001931}
1932
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001933#endif
Guido van Rossum97ead3f1996-01-12 01:24:09 +00001934
Benjamin Petersonb173f782009-05-05 22:31:58 +00001935
Guido van Rossum84a90321996-05-22 16:34:47 +00001936/* Hack to force loading of abstract.o */
Martin v. Löwis18e16552006-02-15 17:27:45 +00001937Py_ssize_t (*_Py_abstract_hack)(PyObject *) = PyObject_Size;
Guido van Rossume09fb551997-08-05 02:04:34 +00001938
1939
David Malcolm49526f42012-06-22 14:55:41 -04001940void
1941_PyObject_DebugTypeStats(FILE *out)
1942{
1943 _PyCFunction_DebugMallocStats(out);
1944 _PyDict_DebugMallocStats(out);
1945 _PyFloat_DebugMallocStats(out);
1946 _PyFrame_DebugMallocStats(out);
1947 _PyList_DebugMallocStats(out);
1948 _PyMethod_DebugMallocStats(out);
David Malcolm49526f42012-06-22 14:55:41 -04001949 _PyTuple_DebugMallocStats(out);
1950}
Guido van Rossumb18618d2000-05-03 23:44:39 +00001951
Guido van Rossum86610361998-04-10 22:32:46 +00001952/* These methods are used to control infinite recursion in repr, str, print,
1953 etc. Container objects that may recursively contain themselves,
Martin Panter8d56c022016-05-29 04:13:35 +00001954 e.g. builtin dictionaries and lists, should use Py_ReprEnter() and
Guido van Rossum86610361998-04-10 22:32:46 +00001955 Py_ReprLeave() to avoid infinite recursion.
1956
1957 Py_ReprEnter() returns 0 the first time it is called for a particular
1958 object and 1 every time thereafter. It returns -1 if an exception
1959 occurred. Py_ReprLeave() has no return value.
1960
1961 See dictobject.c and listobject.c for examples of use.
1962*/
1963
Guido van Rossum86610361998-04-10 22:32:46 +00001964int
Fred Drake100814d2000-07-09 15:48:49 +00001965Py_ReprEnter(PyObject *obj)
Guido van Rossum86610361998-04-10 22:32:46 +00001966{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001967 PyObject *dict;
1968 PyObject *list;
1969 Py_ssize_t i;
Guido van Rossum86610361998-04-10 22:32:46 +00001970
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001971 dict = PyThreadState_GetDict();
Antoine Pitrou04d17d32014-03-31 22:04:38 +02001972 /* Ignore a missing thread-state, so that this function can be called
1973 early on startup. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001974 if (dict == NULL)
1975 return 0;
Victor Stinner7a07e452013-11-06 18:57:29 +01001976 list = _PyDict_GetItemId(dict, &PyId_Py_Repr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001977 if (list == NULL) {
1978 list = PyList_New(0);
1979 if (list == NULL)
1980 return -1;
Victor Stinner7a07e452013-11-06 18:57:29 +01001981 if (_PyDict_SetItemId(dict, &PyId_Py_Repr, list) < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001982 return -1;
1983 Py_DECREF(list);
1984 }
1985 i = PyList_GET_SIZE(list);
1986 while (--i >= 0) {
1987 if (PyList_GET_ITEM(list, i) == obj)
1988 return 1;
1989 }
Victor Stinnere901d1f2013-07-17 21:58:41 +02001990 if (PyList_Append(list, obj) < 0)
1991 return -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001992 return 0;
Guido van Rossum86610361998-04-10 22:32:46 +00001993}
1994
1995void
Fred Drake100814d2000-07-09 15:48:49 +00001996Py_ReprLeave(PyObject *obj)
Guido van Rossum86610361998-04-10 22:32:46 +00001997{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001998 PyObject *dict;
1999 PyObject *list;
2000 Py_ssize_t i;
Victor Stinner1b634932013-07-16 22:24:44 +02002001 PyObject *error_type, *error_value, *error_traceback;
2002
2003 PyErr_Fetch(&error_type, &error_value, &error_traceback);
Guido van Rossum86610361998-04-10 22:32:46 +00002004
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002005 dict = PyThreadState_GetDict();
2006 if (dict == NULL)
Victor Stinner1b634932013-07-16 22:24:44 +02002007 goto finally;
2008
Victor Stinner7a07e452013-11-06 18:57:29 +01002009 list = _PyDict_GetItemId(dict, &PyId_Py_Repr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002010 if (list == NULL || !PyList_Check(list))
Victor Stinner1b634932013-07-16 22:24:44 +02002011 goto finally;
2012
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002013 i = PyList_GET_SIZE(list);
2014 /* Count backwards because we always expect obj to be list[-1] */
2015 while (--i >= 0) {
2016 if (PyList_GET_ITEM(list, i) == obj) {
2017 PyList_SetSlice(list, i, i + 1, NULL);
2018 break;
2019 }
2020 }
Victor Stinner1b634932013-07-16 22:24:44 +02002021
2022finally:
2023 /* ignore exceptions because there is no way to report them. */
2024 PyErr_Restore(error_type, error_value, error_traceback);
Guido van Rossum86610361998-04-10 22:32:46 +00002025}
Guido van Rossumd724b232000-03-13 16:01:29 +00002026
Tim Peters803526b2002-07-07 05:13:56 +00002027/* Trashcan support. */
Guido van Rossumd724b232000-03-13 16:01:29 +00002028
Tim Peters803526b2002-07-07 05:13:56 +00002029/* Add op to the _PyTrash_delete_later list. Called when the current
2030 * call-stack depth gets large. op must be a currently untracked gc'ed
2031 * object, with refcount 0. Py_DECREF must already have been called on it.
2032 */
Guido van Rossumd724b232000-03-13 16:01:29 +00002033void
Fred Drake100814d2000-07-09 15:48:49 +00002034_PyTrash_deposit_object(PyObject *op)
Guido van Rossumd724b232000-03-13 16:01:29 +00002035{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002036 assert(PyObject_IS_GC(op));
Antoine Pitrou796564c2013-07-30 19:59:21 +02002037 assert(_PyGC_REFS(op) == _PyGC_REFS_UNTRACKED);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002038 assert(op->ob_refcnt == 0);
Eric Snow2ebc5ce2017-09-07 23:51:28 -06002039 _Py_AS_GC(op)->gc.gc_prev = (PyGC_Head *)_PyRuntime.gc.trash_delete_later;
2040 _PyRuntime.gc.trash_delete_later = op;
Guido van Rossumd724b232000-03-13 16:01:29 +00002041}
2042
Antoine Pitrou2b0218a2012-09-06 00:59:49 +02002043/* The equivalent API, using per-thread state recursion info */
2044void
2045_PyTrash_thread_deposit_object(PyObject *op)
2046{
2047 PyThreadState *tstate = PyThreadState_GET();
2048 assert(PyObject_IS_GC(op));
Antoine Pitrou796564c2013-07-30 19:59:21 +02002049 assert(_PyGC_REFS(op) == _PyGC_REFS_UNTRACKED);
Antoine Pitrou2b0218a2012-09-06 00:59:49 +02002050 assert(op->ob_refcnt == 0);
2051 _Py_AS_GC(op)->gc.gc_prev = (PyGC_Head *) tstate->trash_delete_later;
2052 tstate->trash_delete_later = op;
2053}
2054
Tim Peters803526b2002-07-07 05:13:56 +00002055/* Dealloccate all the objects in the _PyTrash_delete_later list. Called when
2056 * the call-stack unwinds again.
2057 */
Guido van Rossumd724b232000-03-13 16:01:29 +00002058void
Fred Drake100814d2000-07-09 15:48:49 +00002059_PyTrash_destroy_chain(void)
Guido van Rossumd724b232000-03-13 16:01:29 +00002060{
Eric Snow2ebc5ce2017-09-07 23:51:28 -06002061 while (_PyRuntime.gc.trash_delete_later) {
2062 PyObject *op = _PyRuntime.gc.trash_delete_later;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002063 destructor dealloc = Py_TYPE(op)->tp_dealloc;
Neil Schemenauerf589c052002-03-29 03:05:54 +00002064
Eric Snow2ebc5ce2017-09-07 23:51:28 -06002065 _PyRuntime.gc.trash_delete_later =
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002066 (PyObject*) _Py_AS_GC(op)->gc.gc_prev;
Neil Schemenauerf589c052002-03-29 03:05:54 +00002067
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002068 /* Call the deallocator directly. This used to try to
2069 * fool Py_DECREF into calling it indirectly, but
2070 * Py_DECREF was already called on this object, and in
2071 * assorted non-release builds calling Py_DECREF again ends
2072 * up distorting allocation statistics.
2073 */
2074 assert(op->ob_refcnt == 0);
Eric Snow2ebc5ce2017-09-07 23:51:28 -06002075 ++_PyRuntime.gc.trash_delete_nesting;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002076 (*dealloc)(op);
Eric Snow2ebc5ce2017-09-07 23:51:28 -06002077 --_PyRuntime.gc.trash_delete_nesting;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002078 }
Guido van Rossumd724b232000-03-13 16:01:29 +00002079}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002080
Antoine Pitrou2b0218a2012-09-06 00:59:49 +02002081/* The equivalent API, using per-thread state recursion info */
2082void
2083_PyTrash_thread_destroy_chain(void)
2084{
2085 PyThreadState *tstate = PyThreadState_GET();
Xiang Zhanga66f9c62017-05-13 13:36:14 +08002086 /* We need to increase trash_delete_nesting here, otherwise,
2087 _PyTrash_thread_destroy_chain will be called recursively
2088 and then possibly crash. An example that may crash without
2089 increase:
2090 N = 500000 # need to be large enough
2091 ob = object()
2092 tups = [(ob,) for i in range(N)]
2093 for i in range(49):
2094 tups = [(tup,) for tup in tups]
2095 del tups
2096 */
2097 assert(tstate->trash_delete_nesting == 0);
2098 ++tstate->trash_delete_nesting;
Antoine Pitrou2b0218a2012-09-06 00:59:49 +02002099 while (tstate->trash_delete_later) {
2100 PyObject *op = tstate->trash_delete_later;
2101 destructor dealloc = Py_TYPE(op)->tp_dealloc;
2102
2103 tstate->trash_delete_later =
2104 (PyObject*) _Py_AS_GC(op)->gc.gc_prev;
2105
2106 /* Call the deallocator directly. This used to try to
2107 * fool Py_DECREF into calling it indirectly, but
2108 * Py_DECREF was already called on this object, and in
2109 * assorted non-release builds calling Py_DECREF again ends
2110 * up distorting allocation statistics.
2111 */
2112 assert(op->ob_refcnt == 0);
Antoine Pitrou2b0218a2012-09-06 00:59:49 +02002113 (*dealloc)(op);
Xiang Zhanga66f9c62017-05-13 13:36:14 +08002114 assert(tstate->trash_delete_nesting == 1);
Antoine Pitrou2b0218a2012-09-06 00:59:49 +02002115 }
Xiang Zhanga66f9c62017-05-13 13:36:14 +08002116 --tstate->trash_delete_nesting;
Antoine Pitrou2b0218a2012-09-06 00:59:49 +02002117}
2118
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00002119#ifndef Py_TRACE_REFS
2120/* For Py_LIMITED_API, we need an out-of-line version of _Py_Dealloc.
2121 Define this here, so we can undefine the macro. */
2122#undef _Py_Dealloc
2123PyAPI_FUNC(void) _Py_Dealloc(PyObject *);
2124void
2125_Py_Dealloc(PyObject *op)
2126{
2127 _Py_INC_TPFREES(op) _Py_COUNT_ALLOCS_COMMA
2128 (*Py_TYPE(op)->tp_dealloc)(op);
2129}
2130#endif
2131
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002132#ifdef __cplusplus
2133}
2134#endif