blob: 638f7e7c9ba6f3472e9390f85898182ac556c42f [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"
Benjamin Petersonfd838e62009-04-20 02:09:13 +00005#include "frameobject.h"
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00006
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007#ifdef __cplusplus
8extern "C" {
9#endif
10
Victor Stinnerbd303c12013-11-07 23:07:29 +010011_Py_IDENTIFIER(Py_Repr);
12_Py_IDENTIFIER(__bytes__);
13_Py_IDENTIFIER(__dir__);
14_Py_IDENTIFIER(__isabstractmethod__);
15_Py_IDENTIFIER(builtins);
16
Tim Peters34592512002-07-11 06:23:50 +000017#ifdef Py_REF_DEBUG
Neal Norwitz84632ee2006-03-04 20:00:59 +000018Py_ssize_t _Py_RefTotal;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000019
20Py_ssize_t
21_Py_GetRefTotal(void)
22{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000023 PyObject *o;
24 Py_ssize_t total = _Py_RefTotal;
Antoine Pitrou9d952542013-08-24 21:07:07 +020025 o = _PySet_Dummy;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000026 if (o != NULL)
27 total -= o->ob_refcnt;
28 return total;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000029}
Nick Coghland6009512014-11-20 21:39:37 +100030
31void
32_PyDebug_PrintTotalRefs(void) {
33 PyObject *xoptions, *value;
34 _Py_IDENTIFIER(showrefcount);
35
36 xoptions = PySys_GetXOptions();
37 if (xoptions == NULL)
38 return;
39 value = _PyDict_GetItemId(xoptions, &PyId_showrefcount);
40 if (value == Py_True)
41 fprintf(stderr,
42 "[%" PY_FORMAT_SIZE_T "d refs, "
43 "%" PY_FORMAT_SIZE_T "d blocks]\n",
44 _Py_GetRefTotal(), _Py_GetAllocatedBlocks());
45}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000046#endif /* Py_REF_DEBUG */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000047
Guido van Rossum3f5da241990-12-20 15:06:42 +000048/* Object allocation routines used by NEWOBJ and NEWVAROBJ macros.
49 These are used by the individual routines for object creation.
50 Do not call them otherwise, they do not initialize the object! */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000051
Tim Peters78be7992003-03-23 02:51:01 +000052#ifdef Py_TRACE_REFS
Tim Peters7571a0f2003-03-23 17:52:28 +000053/* Head of circular doubly-linked list of all objects. These are linked
54 * together via the _ob_prev and _ob_next members of a PyObject, which
55 * exist only in a Py_TRACE_REFS build.
56 */
Tim Peters78be7992003-03-23 02:51:01 +000057static PyObject refchain = {&refchain, &refchain};
Tim Peters36eb4df2003-03-23 03:33:13 +000058
Tim Peters7571a0f2003-03-23 17:52:28 +000059/* Insert op at the front of the list of all objects. If force is true,
60 * op is added even if _ob_prev and _ob_next are non-NULL already. If
61 * force is false amd _ob_prev or _ob_next are non-NULL, do nothing.
62 * force should be true if and only if op points to freshly allocated,
63 * uninitialized memory, or you've unlinked op from the list and are
Tim Peters51f8d382003-03-23 18:06:08 +000064 * relinking it into the front.
Tim Peters7571a0f2003-03-23 17:52:28 +000065 * Note that objects are normally added to the list via _Py_NewReference,
66 * which is called by PyObject_Init. Not all objects are initialized that
67 * way, though; exceptions include statically allocated type objects, and
68 * statically allocated singletons (like Py_True and Py_None).
69 */
Tim Peters36eb4df2003-03-23 03:33:13 +000070void
Tim Peters7571a0f2003-03-23 17:52:28 +000071_Py_AddToAllObjects(PyObject *op, int force)
Tim Peters36eb4df2003-03-23 03:33:13 +000072{
Tim Peters7571a0f2003-03-23 17:52:28 +000073#ifdef Py_DEBUG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000074 if (!force) {
75 /* If it's initialized memory, op must be in or out of
76 * the list unambiguously.
77 */
78 assert((op->_ob_prev == NULL) == (op->_ob_next == NULL));
79 }
Tim Peters78be7992003-03-23 02:51:01 +000080#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000081 if (force || op->_ob_prev == NULL) {
82 op->_ob_next = refchain._ob_next;
83 op->_ob_prev = &refchain;
84 refchain._ob_next->_ob_prev = op;
85 refchain._ob_next = op;
86 }
Tim Peters7571a0f2003-03-23 17:52:28 +000087}
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000088#endif /* Py_TRACE_REFS */
Tim Peters78be7992003-03-23 02:51:01 +000089
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +000090#ifdef COUNT_ALLOCS
Guido van Rossumc0b618a1997-05-02 03:12:38 +000091static PyTypeObject *type_list;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000092/* All types are added to type_list, at least when
93 they get one object created. That makes them
94 immortal, which unfortunately contributes to
95 garbage itself. If unlist_types_without_objects
96 is set, they will be removed from the type_list
97 once the last object is deallocated. */
Benjamin Petersona4a37fe2009-01-11 17:13:55 +000098static int unlist_types_without_objects;
99extern Py_ssize_t tuple_zero_allocs, fast_tuple_allocs;
100extern Py_ssize_t quick_int_allocs, quick_neg_int_allocs;
101extern Py_ssize_t null_strings, one_strings;
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +0000102void
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000103dump_counts(FILE* f)
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +0000104{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000105 PyTypeObject *tp;
Serhiy Storchaka7e160ce2016-07-03 21:03:53 +0300106 PyObject *xoptions, *value;
107 _Py_IDENTIFIER(showalloccount);
108
109 xoptions = PySys_GetXOptions();
110 if (xoptions == NULL)
111 return;
112 value = _PyDict_GetItemId(xoptions, &PyId_showalloccount);
113 if (value != Py_True)
114 return;
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +0000115
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000116 for (tp = type_list; tp; tp = tp->tp_next)
117 fprintf(f, "%s alloc'd: %" PY_FORMAT_SIZE_T "d, "
118 "freed: %" PY_FORMAT_SIZE_T "d, "
119 "max in use: %" PY_FORMAT_SIZE_T "d\n",
120 tp->tp_name, tp->tp_allocs, tp->tp_frees,
121 tp->tp_maxalloc);
122 fprintf(f, "fast tuple allocs: %" PY_FORMAT_SIZE_T "d, "
123 "empty: %" PY_FORMAT_SIZE_T "d\n",
124 fast_tuple_allocs, tuple_zero_allocs);
125 fprintf(f, "fast int allocs: pos: %" PY_FORMAT_SIZE_T "d, "
126 "neg: %" PY_FORMAT_SIZE_T "d\n",
127 quick_int_allocs, quick_neg_int_allocs);
128 fprintf(f, "null strings: %" PY_FORMAT_SIZE_T "d, "
129 "1-strings: %" PY_FORMAT_SIZE_T "d\n",
130 null_strings, one_strings);
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +0000131}
132
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000133PyObject *
Fred Drake100814d2000-07-09 15:48:49 +0000134get_counts(void)
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000135{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000136 PyTypeObject *tp;
137 PyObject *result;
138 PyObject *v;
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000139
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000140 result = PyList_New(0);
141 if (result == NULL)
142 return NULL;
143 for (tp = type_list; tp; tp = tp->tp_next) {
144 v = Py_BuildValue("(snnn)", tp->tp_name, tp->tp_allocs,
145 tp->tp_frees, tp->tp_maxalloc);
146 if (v == NULL) {
147 Py_DECREF(result);
148 return NULL;
149 }
150 if (PyList_Append(result, v) < 0) {
151 Py_DECREF(v);
152 Py_DECREF(result);
153 return NULL;
154 }
155 Py_DECREF(v);
156 }
157 return result;
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000158}
159
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +0000160void
Fred Drake100814d2000-07-09 15:48:49 +0000161inc_count(PyTypeObject *tp)
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +0000162{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000163 if (tp->tp_next == NULL && tp->tp_prev == NULL) {
164 /* first time; insert in linked list */
165 if (tp->tp_next != NULL) /* sanity check */
166 Py_FatalError("XXX inc_count sanity check");
167 if (type_list)
168 type_list->tp_prev = tp;
169 tp->tp_next = type_list;
170 /* Note that as of Python 2.2, heap-allocated type objects
171 * can go away, but this code requires that they stay alive
172 * until program exit. That's why we're careful with
173 * refcounts here. type_list gets a new reference to tp,
174 * while ownership of the reference type_list used to hold
175 * (if any) was transferred to tp->tp_next in the line above.
176 * tp is thus effectively immortal after this.
177 */
178 Py_INCREF(tp);
179 type_list = tp;
Tim Peters3e40c7f2003-03-23 03:04:32 +0000180#ifdef Py_TRACE_REFS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000181 /* Also insert in the doubly-linked list of all objects,
182 * if not already there.
183 */
184 _Py_AddToAllObjects((PyObject *)tp, 0);
Tim Peters78be7992003-03-23 02:51:01 +0000185#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000186 }
187 tp->tp_allocs++;
188 if (tp->tp_allocs - tp->tp_frees > tp->tp_maxalloc)
189 tp->tp_maxalloc = tp->tp_allocs - tp->tp_frees;
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +0000190}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000191
192void dec_count(PyTypeObject *tp)
193{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000194 tp->tp_frees++;
195 if (unlist_types_without_objects &&
196 tp->tp_allocs == tp->tp_frees) {
197 /* unlink the type from type_list */
198 if (tp->tp_prev)
199 tp->tp_prev->tp_next = tp->tp_next;
200 else
201 type_list = tp->tp_next;
202 if (tp->tp_next)
203 tp->tp_next->tp_prev = tp->tp_prev;
204 tp->tp_next = tp->tp_prev = NULL;
205 Py_DECREF(tp);
206 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000207}
208
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +0000209#endif
210
Tim Peters7c321a82002-07-09 02:57:01 +0000211#ifdef Py_REF_DEBUG
212/* Log a fatal error; doesn't return. */
213void
214_Py_NegativeRefcount(const char *fname, int lineno, PyObject *op)
215{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000216 char buf[300];
Tim Peters7c321a82002-07-09 02:57:01 +0000217
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000218 PyOS_snprintf(buf, sizeof(buf),
219 "%s:%i object at %p has negative ref count "
220 "%" PY_FORMAT_SIZE_T "d",
221 fname, lineno, op, op->ob_refcnt);
222 Py_FatalError(buf);
Tim Peters7c321a82002-07-09 02:57:01 +0000223}
224
225#endif /* Py_REF_DEBUG */
226
Thomas Heller1328b522004-04-22 17:23:49 +0000227void
228Py_IncRef(PyObject *o)
229{
230 Py_XINCREF(o);
231}
232
233void
234Py_DecRef(PyObject *o)
235{
236 Py_XDECREF(o);
237}
238
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000239PyObject *
Fred Drake100814d2000-07-09 15:48:49 +0000240PyObject_Init(PyObject *op, PyTypeObject *tp)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000241{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000242 if (op == NULL)
243 return PyErr_NoMemory();
244 /* Any changes should be reflected in PyObject_INIT (objimpl.h) */
245 Py_TYPE(op) = tp;
246 _Py_NewReference(op);
247 return op;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000248}
249
Guido van Rossumb18618d2000-05-03 23:44:39 +0000250PyVarObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000251PyObject_InitVar(PyVarObject *op, PyTypeObject *tp, Py_ssize_t size)
Guido van Rossumb18618d2000-05-03 23:44:39 +0000252{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000253 if (op == NULL)
254 return (PyVarObject *) PyErr_NoMemory();
255 /* Any changes should be reflected in PyObject_INIT_VAR */
256 op->ob_size = size;
257 Py_TYPE(op) = tp;
258 _Py_NewReference((PyObject *)op);
259 return op;
Guido van Rossumb18618d2000-05-03 23:44:39 +0000260}
261
262PyObject *
Fred Drake100814d2000-07-09 15:48:49 +0000263_PyObject_New(PyTypeObject *tp)
Guido van Rossumb18618d2000-05-03 23:44:39 +0000264{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000265 PyObject *op;
266 op = (PyObject *) PyObject_MALLOC(_PyObject_SIZE(tp));
267 if (op == NULL)
268 return PyErr_NoMemory();
269 return PyObject_INIT(op, tp);
Guido van Rossumb18618d2000-05-03 23:44:39 +0000270}
271
Guido van Rossumd0c87ee1997-05-15 21:31:03 +0000272PyVarObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000273_PyObject_NewVar(PyTypeObject *tp, Py_ssize_t nitems)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000274{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000275 PyVarObject *op;
276 const size_t size = _PyObject_VAR_SIZE(tp, nitems);
277 op = (PyVarObject *) PyObject_MALLOC(size);
278 if (op == NULL)
279 return (PyVarObject *)PyErr_NoMemory();
280 return PyObject_INIT_VAR(op, tp, nitems);
Guido van Rossumb18618d2000-05-03 23:44:39 +0000281}
282
Antoine Pitrou796564c2013-07-30 19:59:21 +0200283void
284PyObject_CallFinalizer(PyObject *self)
285{
286 PyTypeObject *tp = Py_TYPE(self);
287
288 /* The former could happen on heaptypes created from the C API, e.g.
289 PyType_FromSpec(). */
290 if (!PyType_HasFeature(tp, Py_TPFLAGS_HAVE_FINALIZE) ||
291 tp->tp_finalize == NULL)
292 return;
293 /* tp_finalize should only be called once. */
294 if (PyType_IS_GC(tp) && _PyGC_FINALIZED(self))
295 return;
296
297 tp->tp_finalize(self);
298 if (PyType_IS_GC(tp))
299 _PyGC_SET_FINALIZED(self, 1);
300}
301
302int
303PyObject_CallFinalizerFromDealloc(PyObject *self)
304{
305 Py_ssize_t refcnt;
306
307 /* Temporarily resurrect the object. */
308 if (self->ob_refcnt != 0) {
309 Py_FatalError("PyObject_CallFinalizerFromDealloc called on "
310 "object with a non-zero refcount");
311 }
312 self->ob_refcnt = 1;
313
314 PyObject_CallFinalizer(self);
315
316 /* Undo the temporary resurrection; can't use DECREF here, it would
317 * cause a recursive call.
318 */
319 assert(self->ob_refcnt > 0);
320 if (--self->ob_refcnt == 0)
321 return 0; /* this is the normal path out */
322
323 /* tp_finalize resurrected it! Make it look like the original Py_DECREF
324 * never happened.
325 */
326 refcnt = self->ob_refcnt;
327 _Py_NewReference(self);
328 self->ob_refcnt = refcnt;
329
330 if (PyType_IS_GC(Py_TYPE(self))) {
331 assert(_PyGC_REFS(self) != _PyGC_REFS_UNTRACKED);
332 }
333 /* If Py_REF_DEBUG, _Py_NewReference bumped _Py_RefTotal, so
334 * we need to undo that. */
335 _Py_DEC_REFTOTAL;
336 /* If Py_TRACE_REFS, _Py_NewReference re-added self to the object
337 * chain, so no more to do there.
338 * If COUNT_ALLOCS, the original decref bumped tp_frees, and
339 * _Py_NewReference bumped tp_allocs: both of those need to be
340 * undone.
341 */
342#ifdef COUNT_ALLOCS
343 --Py_TYPE(self)->tp_frees;
344 --Py_TYPE(self)->tp_allocs;
345#endif
346 return -1;
347}
348
Antoine Pitrouc47bd4a2010-07-27 22:08:27 +0000349int
350PyObject_Print(PyObject *op, FILE *fp, int flags)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000351{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000352 int ret = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000353 if (PyErr_CheckSignals())
354 return -1;
Guido van Rossum9b00dfa1998-04-28 16:06:54 +0000355#ifdef USE_STACKCHECK
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000356 if (PyOS_CheckStack()) {
357 PyErr_SetString(PyExc_MemoryError, "stack overflow");
358 return -1;
359 }
Guido van Rossum9b00dfa1998-04-28 16:06:54 +0000360#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000361 clearerr(fp); /* Clear any previous error condition */
362 if (op == NULL) {
363 Py_BEGIN_ALLOW_THREADS
364 fprintf(fp, "<nil>");
365 Py_END_ALLOW_THREADS
366 }
367 else {
368 if (op->ob_refcnt <= 0)
369 /* XXX(twouters) cast refcount to long until %zd is
370 universally available */
371 Py_BEGIN_ALLOW_THREADS
372 fprintf(fp, "<refcnt %ld at %p>",
373 (long)op->ob_refcnt, op);
374 Py_END_ALLOW_THREADS
375 else {
376 PyObject *s;
377 if (flags & Py_PRINT_RAW)
378 s = PyObject_Str(op);
379 else
380 s = PyObject_Repr(op);
381 if (s == NULL)
382 ret = -1;
383 else if (PyBytes_Check(s)) {
384 fwrite(PyBytes_AS_STRING(s), 1,
385 PyBytes_GET_SIZE(s), fp);
386 }
387 else if (PyUnicode_Check(s)) {
388 PyObject *t;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200389 t = PyUnicode_AsEncodedString(s, "utf-8", "backslashreplace");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000390 if (t == NULL)
391 ret = 0;
392 else {
393 fwrite(PyBytes_AS_STRING(t), 1,
394 PyBytes_GET_SIZE(t), fp);
Victor Stinnerba6b4302010-05-17 09:33:42 +0000395 Py_DECREF(t);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000396 }
397 }
398 else {
399 PyErr_Format(PyExc_TypeError,
400 "str() or repr() returned '%.100s'",
401 s->ob_type->tp_name);
402 ret = -1;
403 }
404 Py_XDECREF(s);
405 }
406 }
407 if (ret == 0) {
408 if (ferror(fp)) {
Serhiy Storchaka55fe1ae2017-04-16 10:46:38 +0300409 PyErr_SetFromErrno(PyExc_OSError);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000410 clearerr(fp);
411 ret = -1;
412 }
413 }
414 return ret;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000415}
416
Guido van Rossum38938152006-08-21 23:36:26 +0000417/* For debugging convenience. Set a breakpoint here and call it from your DLL */
418void
Thomas Woutersb2137042007-02-01 18:02:27 +0000419_Py_BreakPoint(void)
Guido van Rossum38938152006-08-21 23:36:26 +0000420{
421}
422
Neal Norwitz1a997502003-01-13 20:13:12 +0000423
Barry Warsaw9bf16442001-01-23 16:24:35 +0000424/* For debugging convenience. See Misc/gdbinit for some useful gdb hooks */
Guido van Rossum38938152006-08-21 23:36:26 +0000425void
426_PyObject_Dump(PyObject* op)
Barry Warsaw9bf16442001-01-23 16:24:35 +0000427{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000428 if (op == NULL)
429 fprintf(stderr, "NULL\n");
430 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000431 PyGILState_STATE gil;
Victor Stinnere5132102013-08-26 13:49:06 +0200432 PyObject *error_type, *error_value, *error_traceback;
433
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000434 fprintf(stderr, "object : ");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000435 gil = PyGILState_Ensure();
Victor Stinnere5132102013-08-26 13:49:06 +0200436
437 PyErr_Fetch(&error_type, &error_value, &error_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000438 (void)PyObject_Print(op, stderr, 0);
Victor Stinnere5132102013-08-26 13:49:06 +0200439 PyErr_Restore(error_type, error_value, error_traceback);
440
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000441 PyGILState_Release(gil);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000442 /* XXX(twouters) cast refcount to long until %zd is
443 universally available */
444 fprintf(stderr, "\n"
445 "type : %s\n"
446 "refcount: %ld\n"
447 "address : %p\n",
448 Py_TYPE(op)==NULL ? "NULL" : Py_TYPE(op)->tp_name,
449 (long)op->ob_refcnt,
450 op);
451 }
Barry Warsaw9bf16442001-01-23 16:24:35 +0000452}
Barry Warsaw903138f2001-01-23 16:33:18 +0000453
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000454PyObject *
Fred Drake100814d2000-07-09 15:48:49 +0000455PyObject_Repr(PyObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000456{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000457 PyObject *res;
458 if (PyErr_CheckSignals())
459 return NULL;
Guido van Rossum9b00dfa1998-04-28 16:06:54 +0000460#ifdef USE_STACKCHECK
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000461 if (PyOS_CheckStack()) {
462 PyErr_SetString(PyExc_MemoryError, "stack overflow");
463 return NULL;
464 }
Guido van Rossum9b00dfa1998-04-28 16:06:54 +0000465#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000466 if (v == NULL)
467 return PyUnicode_FromString("<NULL>");
468 if (Py_TYPE(v)->tp_repr == NULL)
469 return PyUnicode_FromFormat("<%s object at %p>",
470 v->ob_type->tp_name, v);
Victor Stinner33824f62013-08-26 14:05:19 +0200471
472#ifdef Py_DEBUG
473 /* PyObject_Repr() must not be called with an exception set,
Victor Stinnera8cb5152017-01-18 14:12:51 +0100474 because it can clear it (directly or indirectly) and so the
Martin Panter9955a372015-10-07 10:26:23 +0000475 caller loses its exception */
Victor Stinner33824f62013-08-26 14:05:19 +0200476 assert(!PyErr_Occurred());
477#endif
478
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000479 res = (*v->ob_type->tp_repr)(v);
Victor Stinner0a54cf12011-12-01 03:22:44 +0100480 if (res == NULL)
481 return NULL;
482 if (!PyUnicode_Check(res)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000483 PyErr_Format(PyExc_TypeError,
484 "__repr__ returned non-string (type %.200s)",
485 res->ob_type->tp_name);
486 Py_DECREF(res);
487 return NULL;
488 }
Victor Stinnerdb88ae52011-12-01 02:15:00 +0100489#ifndef Py_DEBUG
490 if (PyUnicode_READY(res) < 0)
491 return NULL;
492#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000493 return res;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000494}
495
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000496PyObject *
Guido van Rossum98297ee2007-11-06 21:34:58 +0000497PyObject_Str(PyObject *v)
Guido van Rossumc6004111993-11-05 10:22:19 +0000498{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000499 PyObject *res;
500 if (PyErr_CheckSignals())
501 return NULL;
Guido van Rossum98297ee2007-11-06 21:34:58 +0000502#ifdef USE_STACKCHECK
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000503 if (PyOS_CheckStack()) {
504 PyErr_SetString(PyExc_MemoryError, "stack overflow");
505 return NULL;
506 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000507#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000508 if (v == NULL)
509 return PyUnicode_FromString("<NULL>");
510 if (PyUnicode_CheckExact(v)) {
Victor Stinnerdb88ae52011-12-01 02:15:00 +0100511#ifndef Py_DEBUG
Victor Stinner4ead7c72011-11-20 19:48:36 +0100512 if (PyUnicode_READY(v) < 0)
513 return NULL;
Victor Stinnerdb88ae52011-12-01 02:15:00 +0100514#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000515 Py_INCREF(v);
516 return v;
517 }
518 if (Py_TYPE(v)->tp_str == NULL)
519 return PyObject_Repr(v);
Guido van Rossum4f288ab2001-05-01 16:53:37 +0000520
Victor Stinner33824f62013-08-26 14:05:19 +0200521#ifdef Py_DEBUG
522 /* PyObject_Str() must not be called with an exception set,
Victor Stinnera8cb5152017-01-18 14:12:51 +0100523 because it can clear it (directly or indirectly) and so the
Nick Coghland979e432014-02-09 10:43:21 +1000524 caller loses its exception */
Victor Stinner33824f62013-08-26 14:05:19 +0200525 assert(!PyErr_Occurred());
526#endif
527
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000528 /* It is possible for a type to have a tp_str representation that loops
529 infinitely. */
530 if (Py_EnterRecursiveCall(" while getting the str of an object"))
531 return NULL;
532 res = (*Py_TYPE(v)->tp_str)(v);
533 Py_LeaveRecursiveCall();
534 if (res == NULL)
535 return NULL;
536 if (!PyUnicode_Check(res)) {
537 PyErr_Format(PyExc_TypeError,
538 "__str__ returned non-string (type %.200s)",
539 Py_TYPE(res)->tp_name);
540 Py_DECREF(res);
541 return NULL;
542 }
Victor Stinnerdb88ae52011-12-01 02:15:00 +0100543#ifndef Py_DEBUG
Victor Stinner4ead7c72011-11-20 19:48:36 +0100544 if (PyUnicode_READY(res) < 0)
545 return NULL;
Victor Stinnerdb88ae52011-12-01 02:15:00 +0100546#endif
Victor Stinner4ead7c72011-11-20 19:48:36 +0100547 assert(_PyUnicode_CheckConsistency(res, 1));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000548 return res;
Neil Schemenauercf52c072005-08-12 17:34:58 +0000549}
550
Georg Brandl559e5d72008-06-11 18:37:52 +0000551PyObject *
552PyObject_ASCII(PyObject *v)
553{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000554 PyObject *repr, *ascii, *res;
Georg Brandl559e5d72008-06-11 18:37:52 +0000555
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000556 repr = PyObject_Repr(v);
557 if (repr == NULL)
558 return NULL;
Georg Brandl559e5d72008-06-11 18:37:52 +0000559
Victor Stinneraf037572013-04-14 18:44:10 +0200560 if (PyUnicode_IS_ASCII(repr))
561 return repr;
562
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000563 /* repr is guaranteed to be a PyUnicode object by PyObject_Repr */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200564 ascii = _PyUnicode_AsASCIIString(repr, "backslashreplace");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000565 Py_DECREF(repr);
566 if (ascii == NULL)
567 return NULL;
Georg Brandl559e5d72008-06-11 18:37:52 +0000568
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000569 res = PyUnicode_DecodeASCII(
570 PyBytes_AS_STRING(ascii),
571 PyBytes_GET_SIZE(ascii),
572 NULL);
573
574 Py_DECREF(ascii);
575 return res;
Georg Brandl559e5d72008-06-11 18:37:52 +0000576}
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000577
Benjamin Petersonc15a0732008-08-26 16:46:47 +0000578PyObject *
579PyObject_Bytes(PyObject *v)
580{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000581 PyObject *result, *func;
Benjamin Petersonc15a0732008-08-26 16:46:47 +0000582
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000583 if (v == NULL)
584 return PyBytes_FromString("<NULL>");
Benjamin Petersonc15a0732008-08-26 16:46:47 +0000585
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000586 if (PyBytes_CheckExact(v)) {
587 Py_INCREF(v);
588 return v;
589 }
Benjamin Petersonc15a0732008-08-26 16:46:47 +0000590
Benjamin Petersonce798522012-01-22 11:24:29 -0500591 func = _PyObject_LookupSpecial(v, &PyId___bytes__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000592 if (func != NULL) {
Victor Stinnerf17c3de2016-12-06 18:46:19 +0100593 result = _PyObject_CallNoArg(func);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000594 Py_DECREF(func);
595 if (result == NULL)
Benjamin Peterson41ece392010-09-11 16:39:57 +0000596 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000597 if (!PyBytes_Check(result)) {
Benjamin Peterson41ece392010-09-11 16:39:57 +0000598 PyErr_Format(PyExc_TypeError,
599 "__bytes__ returned non-bytes (type %.200s)",
600 Py_TYPE(result)->tp_name);
601 Py_DECREF(result);
602 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000603 }
604 return result;
605 }
606 else if (PyErr_Occurred())
607 return NULL;
608 return PyBytes_FromObject(v);
Benjamin Petersonc15a0732008-08-26 16:46:47 +0000609}
610
Mark Dickinsonc008a172009-02-01 13:59:22 +0000611/* For Python 3.0.1 and later, the old three-way comparison has been
612 completely removed in favour of rich comparisons. PyObject_Compare() and
613 PyObject_Cmp() are gone, and the builtin cmp function no longer exists.
Mark Dickinsone94c6792009-02-02 20:36:42 +0000614 The old tp_compare slot has been renamed to tp_reserved, and should no
Mark Dickinsonc008a172009-02-01 13:59:22 +0000615 longer be used. Use tp_richcompare instead.
Guido van Rossum98297ee2007-11-06 21:34:58 +0000616
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000617 See (*) below for practical amendments.
618
Mark Dickinsonc008a172009-02-01 13:59:22 +0000619 tp_richcompare gets called with a first argument of the appropriate type
620 and a second object of an arbitrary type. We never do any kind of
621 coercion.
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000622
Mark Dickinsonc008a172009-02-01 13:59:22 +0000623 The tp_richcompare slot should return an object, as follows:
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000624
625 NULL if an exception occurred
626 NotImplemented if the requested comparison is not implemented
627 any other false value if the requested comparison is false
628 any other true value if the requested comparison is true
629
630 The PyObject_RichCompare[Bool]() wrappers raise TypeError when they get
631 NotImplemented.
632
633 (*) Practical amendments:
634
635 - If rich comparison returns NotImplemented, == and != are decided by
636 comparing the object pointer (i.e. falling back to the base object
637 implementation).
638
Guido van Rossuma4073002002-05-31 20:03:54 +0000639*/
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000640
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000641/* Map rich comparison operators to their swapped version, e.g. LT <--> GT */
Brett Cannona5ca2e72004-09-25 01:37:24 +0000642int _Py_SwappedOp[] = {Py_GT, Py_GE, Py_EQ, Py_NE, Py_LT, Py_LE};
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +0000643
Serhiy Storchaka2d06e842015-12-25 19:53:18 +0200644static const char * const opstrings[] = {"<", "<=", "==", "!=", ">", ">="};
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000645
646/* Perform a rich comparison, raising TypeError when the requested comparison
647 operator is not supported. */
Guido van Rossume797ec12001-01-17 15:24:28 +0000648static PyObject *
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000649do_richcompare(PyObject *v, PyObject *w, int op)
Guido van Rossume797ec12001-01-17 15:24:28 +0000650{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000651 richcmpfunc f;
652 PyObject *res;
653 int checked_reverse_op = 0;
Guido van Rossume797ec12001-01-17 15:24:28 +0000654
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000655 if (v->ob_type != w->ob_type &&
656 PyType_IsSubtype(w->ob_type, v->ob_type) &&
657 (f = w->ob_type->tp_richcompare) != NULL) {
658 checked_reverse_op = 1;
659 res = (*f)(w, v, _Py_SwappedOp[op]);
660 if (res != Py_NotImplemented)
661 return res;
662 Py_DECREF(res);
663 }
664 if ((f = v->ob_type->tp_richcompare) != NULL) {
665 res = (*f)(v, w, op);
666 if (res != Py_NotImplemented)
667 return res;
668 Py_DECREF(res);
669 }
670 if (!checked_reverse_op && (f = w->ob_type->tp_richcompare) != NULL) {
671 res = (*f)(w, v, _Py_SwappedOp[op]);
672 if (res != Py_NotImplemented)
673 return res;
674 Py_DECREF(res);
675 }
676 /* If neither object implements it, provide a sensible default
677 for == and !=, but raise an exception for ordering. */
678 switch (op) {
679 case Py_EQ:
680 res = (v == w) ? Py_True : Py_False;
681 break;
682 case Py_NE:
683 res = (v != w) ? Py_True : Py_False;
684 break;
685 default:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000686 PyErr_Format(PyExc_TypeError,
Victor Stinner91108f02015-10-14 18:25:31 +0200687 "'%s' not supported between instances of '%.100s' and '%.100s'",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000688 opstrings[op],
Victor Stinner91108f02015-10-14 18:25:31 +0200689 v->ob_type->tp_name,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000690 w->ob_type->tp_name);
691 return NULL;
692 }
693 Py_INCREF(res);
694 return res;
Guido van Rossume797ec12001-01-17 15:24:28 +0000695}
696
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000697/* Perform a rich comparison with object result. This wraps do_richcompare()
698 with a check for NULL arguments and a recursion check. */
Guido van Rossume797ec12001-01-17 15:24:28 +0000699
Guido van Rossume797ec12001-01-17 15:24:28 +0000700PyObject *
701PyObject_RichCompare(PyObject *v, PyObject *w, int op)
702{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000703 PyObject *res;
Guido van Rossume797ec12001-01-17 15:24:28 +0000704
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000705 assert(Py_LT <= op && op <= Py_GE);
706 if (v == NULL || w == NULL) {
707 if (!PyErr_Occurred())
708 PyErr_BadInternalCall();
709 return NULL;
710 }
711 if (Py_EnterRecursiveCall(" in comparison"))
712 return NULL;
713 res = do_richcompare(v, w, op);
714 Py_LeaveRecursiveCall();
715 return res;
Guido van Rossume797ec12001-01-17 15:24:28 +0000716}
717
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000718/* Perform a rich comparison with integer result. This wraps
719 PyObject_RichCompare(), returning -1 for error, 0 for false, 1 for true. */
Guido van Rossume797ec12001-01-17 15:24:28 +0000720int
721PyObject_RichCompareBool(PyObject *v, PyObject *w, int op)
722{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000723 PyObject *res;
724 int ok;
Guido van Rossume797ec12001-01-17 15:24:28 +0000725
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000726 /* Quick result when objects are the same.
727 Guarantees that identity implies equality. */
728 if (v == w) {
729 if (op == Py_EQ)
730 return 1;
731 else if (op == Py_NE)
732 return 0;
733 }
Mark Dickinson4a1f5932008-11-12 23:23:36 +0000734
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000735 res = PyObject_RichCompare(v, w, op);
736 if (res == NULL)
737 return -1;
738 if (PyBool_Check(res))
739 ok = (res == Py_True);
740 else
741 ok = PyObject_IsTrue(res);
742 Py_DECREF(res);
743 return ok;
Guido van Rossume797ec12001-01-17 15:24:28 +0000744}
Fred Drake13634cf2000-06-29 19:17:04 +0000745
Antoine Pitrouce4a9da2011-11-21 20:46:33 +0100746Py_hash_t
Nick Coghland1abd252008-07-15 15:46:38 +0000747PyObject_HashNotImplemented(PyObject *v)
748{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000749 PyErr_Format(PyExc_TypeError, "unhashable type: '%.200s'",
750 Py_TYPE(v)->tp_name);
751 return -1;
Nick Coghland1abd252008-07-15 15:46:38 +0000752}
Fred Drake13634cf2000-06-29 19:17:04 +0000753
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000754Py_hash_t
Fred Drake100814d2000-07-09 15:48:49 +0000755PyObject_Hash(PyObject *v)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000756{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000757 PyTypeObject *tp = Py_TYPE(v);
758 if (tp->tp_hash != NULL)
759 return (*tp->tp_hash)(v);
760 /* To keep to the general practice that inheriting
761 * solely from object in C code should work without
762 * an explicit call to PyType_Ready, we implicitly call
763 * PyType_Ready here and then check the tp_hash slot again
764 */
765 if (tp->tp_dict == NULL) {
766 if (PyType_Ready(tp) < 0)
767 return -1;
768 if (tp->tp_hash != NULL)
769 return (*tp->tp_hash)(v);
770 }
771 /* Otherwise, the object can't be hashed */
772 return PyObject_HashNotImplemented(v);
Guido van Rossum9bfef441993-03-29 10:43:31 +0000773}
774
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000775PyObject *
Jeremy Hyltonaf68c872005-12-10 18:50:16 +0000776PyObject_GetAttrString(PyObject *v, const char *name)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000777{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000778 PyObject *w, *res;
Guido van Rossumd8eb1b31996-08-09 20:52:03 +0000779
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000780 if (Py_TYPE(v)->tp_getattr != NULL)
781 return (*Py_TYPE(v)->tp_getattr)(v, (char*)name);
INADA Naoki3e8d6cb2017-02-21 23:57:25 +0900782 w = PyUnicode_FromString(name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000783 if (w == NULL)
784 return NULL;
785 res = PyObject_GetAttr(v, w);
Victor Stinner59af08f2012-03-22 02:09:08 +0100786 Py_DECREF(w);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000787 return res;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000788}
789
790int
Jeremy Hyltonaf68c872005-12-10 18:50:16 +0000791PyObject_HasAttrString(PyObject *v, const char *name)
Guido van Rossumed18fdc1993-07-11 19:55:34 +0000792{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000793 PyObject *res = PyObject_GetAttrString(v, name);
794 if (res != NULL) {
795 Py_DECREF(res);
796 return 1;
797 }
798 PyErr_Clear();
799 return 0;
Guido van Rossumed18fdc1993-07-11 19:55:34 +0000800}
801
802int
Jeremy Hyltonaf68c872005-12-10 18:50:16 +0000803PyObject_SetAttrString(PyObject *v, const char *name, PyObject *w)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000804{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000805 PyObject *s;
806 int res;
Guido van Rossumd8eb1b31996-08-09 20:52:03 +0000807
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000808 if (Py_TYPE(v)->tp_setattr != NULL)
809 return (*Py_TYPE(v)->tp_setattr)(v, (char*)name, w);
810 s = PyUnicode_InternFromString(name);
811 if (s == NULL)
812 return -1;
813 res = PyObject_SetAttr(v, s, w);
814 Py_XDECREF(s);
815 return res;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000816}
817
Benjamin Petersonbfebb7b2011-12-15 15:34:02 -0500818int
819_PyObject_IsAbstract(PyObject *obj)
820{
821 int res;
822 PyObject* isabstract;
Benjamin Petersonbfebb7b2011-12-15 15:34:02 -0500823
824 if (obj == NULL)
825 return 0;
826
827 isabstract = _PyObject_GetAttrId(obj, &PyId___isabstractmethod__);
828 if (isabstract == NULL) {
829 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
830 PyErr_Clear();
831 return 0;
832 }
833 return -1;
834 }
835 res = PyObject_IsTrue(isabstract);
836 Py_DECREF(isabstract);
837 return res;
838}
839
Guido van Rossum98ff96a1997-05-20 18:34:44 +0000840PyObject *
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200841_PyObject_GetAttrId(PyObject *v, _Py_Identifier *name)
842{
843 PyObject *result;
Martin v. Löwisd10759f2011-11-07 13:00:05 +0100844 PyObject *oname = _PyUnicode_FromId(name); /* borrowed */
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200845 if (!oname)
846 return NULL;
847 result = PyObject_GetAttr(v, oname);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200848 return result;
849}
850
851int
852_PyObject_HasAttrId(PyObject *v, _Py_Identifier *name)
853{
854 int result;
Martin v. Löwisd10759f2011-11-07 13:00:05 +0100855 PyObject *oname = _PyUnicode_FromId(name); /* borrowed */
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200856 if (!oname)
857 return -1;
858 result = PyObject_HasAttr(v, oname);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200859 return result;
860}
861
862int
863_PyObject_SetAttrId(PyObject *v, _Py_Identifier *name, PyObject *w)
864{
865 int result;
Martin v. Löwisd10759f2011-11-07 13:00:05 +0100866 PyObject *oname = _PyUnicode_FromId(name); /* borrowed */
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200867 if (!oname)
868 return -1;
869 result = PyObject_SetAttr(v, oname, w);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200870 return result;
871}
872
873PyObject *
Fred Drake100814d2000-07-09 15:48:49 +0000874PyObject_GetAttr(PyObject *v, PyObject *name)
Guido van Rossum98ff96a1997-05-20 18:34:44 +0000875{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000876 PyTypeObject *tp = Py_TYPE(v);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000877
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000878 if (!PyUnicode_Check(name)) {
879 PyErr_Format(PyExc_TypeError,
880 "attribute name must be string, not '%.200s'",
881 name->ob_type->tp_name);
882 return NULL;
883 }
884 if (tp->tp_getattro != NULL)
885 return (*tp->tp_getattro)(v, name);
886 if (tp->tp_getattr != NULL) {
Serhiy Storchaka2a404b62017-01-22 23:07:07 +0200887 const char *name_str = PyUnicode_AsUTF8(name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000888 if (name_str == NULL)
889 return NULL;
Serhiy Storchaka2a404b62017-01-22 23:07:07 +0200890 return (*tp->tp_getattr)(v, (char *)name_str);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000891 }
892 PyErr_Format(PyExc_AttributeError,
893 "'%.50s' object has no attribute '%U'",
894 tp->tp_name, name);
895 return NULL;
Guido van Rossum98ff96a1997-05-20 18:34:44 +0000896}
897
898int
Fred Drake100814d2000-07-09 15:48:49 +0000899PyObject_HasAttr(PyObject *v, PyObject *name)
Guido van Rossum98ff96a1997-05-20 18:34:44 +0000900{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000901 PyObject *res = PyObject_GetAttr(v, name);
902 if (res != NULL) {
903 Py_DECREF(res);
904 return 1;
905 }
906 PyErr_Clear();
907 return 0;
Guido van Rossum98ff96a1997-05-20 18:34:44 +0000908}
909
910int
Fred Drake100814d2000-07-09 15:48:49 +0000911PyObject_SetAttr(PyObject *v, PyObject *name, PyObject *value)
Guido van Rossum98ff96a1997-05-20 18:34:44 +0000912{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000913 PyTypeObject *tp = Py_TYPE(v);
914 int err;
Marc-André Lemburge44e5072000-09-18 16:20:57 +0000915
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000916 if (!PyUnicode_Check(name)) {
917 PyErr_Format(PyExc_TypeError,
918 "attribute name must be string, not '%.200s'",
919 name->ob_type->tp_name);
920 return -1;
921 }
922 Py_INCREF(name);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000923
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000924 PyUnicode_InternInPlace(&name);
925 if (tp->tp_setattro != NULL) {
926 err = (*tp->tp_setattro)(v, name, value);
927 Py_DECREF(name);
928 return err;
929 }
930 if (tp->tp_setattr != NULL) {
Serhiy Storchaka2a404b62017-01-22 23:07:07 +0200931 const char *name_str = PyUnicode_AsUTF8(name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000932 if (name_str == NULL)
933 return -1;
Serhiy Storchaka2a404b62017-01-22 23:07:07 +0200934 err = (*tp->tp_setattr)(v, (char *)name_str, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000935 Py_DECREF(name);
936 return err;
937 }
938 Py_DECREF(name);
939 assert(name->ob_refcnt >= 1);
940 if (tp->tp_getattr == NULL && tp->tp_getattro == NULL)
941 PyErr_Format(PyExc_TypeError,
942 "'%.100s' object has no attributes "
943 "(%s .%U)",
944 tp->tp_name,
945 value==NULL ? "del" : "assign to",
946 name);
947 else
948 PyErr_Format(PyExc_TypeError,
949 "'%.100s' object has only read-only attributes "
950 "(%s .%U)",
951 tp->tp_name,
952 value==NULL ? "del" : "assign to",
953 name);
954 return -1;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000955}
956
957/* Helper to get a pointer to an object's __dict__ slot, if any */
958
959PyObject **
960_PyObject_GetDictPtr(PyObject *obj)
961{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000962 Py_ssize_t dictoffset;
963 PyTypeObject *tp = Py_TYPE(obj);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000964
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000965 dictoffset = tp->tp_dictoffset;
966 if (dictoffset == 0)
967 return NULL;
968 if (dictoffset < 0) {
969 Py_ssize_t tsize;
970 size_t size;
Guido van Rossum2eb0b872002-03-01 22:24:49 +0000971
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000972 tsize = ((PyVarObject *)obj)->ob_size;
973 if (tsize < 0)
974 tsize = -tsize;
975 size = _PyObject_VAR_SIZE(tp, tsize);
Guido van Rossum2eb0b872002-03-01 22:24:49 +0000976
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000977 dictoffset += (long)size;
978 assert(dictoffset > 0);
979 assert(dictoffset % SIZEOF_VOID_P == 0);
980 }
981 return (PyObject **) ((char *)obj + dictoffset);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000982}
983
Tim Peters6d6c1a32001-08-02 04:15:00 +0000984PyObject *
Raymond Hettinger1da1dbf2003-03-17 19:46:11 +0000985PyObject_SelfIter(PyObject *obj)
Raymond Hettinger01538262003-03-17 08:24:35 +0000986{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000987 Py_INCREF(obj);
988 return obj;
Raymond Hettinger01538262003-03-17 08:24:35 +0000989}
990
Antoine Pitroua7013882012-04-05 00:04:20 +0200991/* Convenience function to get a builtin from its name */
992PyObject *
993_PyObject_GetBuiltin(const char *name)
994{
Victor Stinner53e9ec42013-11-07 00:43:05 +0100995 PyObject *mod_name, *mod, *attr;
996
Victor Stinnerbd303c12013-11-07 23:07:29 +0100997 mod_name = _PyUnicode_FromId(&PyId_builtins); /* borrowed */
Victor Stinner53e9ec42013-11-07 00:43:05 +0100998 if (mod_name == NULL)
999 return NULL;
1000 mod = PyImport_Import(mod_name);
Antoine Pitroua7013882012-04-05 00:04:20 +02001001 if (mod == NULL)
1002 return NULL;
1003 attr = PyObject_GetAttrString(mod, name);
1004 Py_DECREF(mod);
1005 return attr;
1006}
1007
Amaury Forgeot d'Arcf343e012009-01-12 23:58:21 +00001008/* Helper used when the __next__ method is removed from a type:
1009 tp_iternext is never NULL and can be safely called without checking
1010 on every iteration.
1011 */
1012
1013PyObject *
1014_PyObject_NextNotImplemented(PyObject *self)
1015{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001016 PyErr_Format(PyExc_TypeError,
1017 "'%.200s' object is not iterable",
1018 Py_TYPE(self)->tp_name);
1019 return NULL;
Amaury Forgeot d'Arcf343e012009-01-12 23:58:21 +00001020}
1021
Yury Selivanovf2392132016-12-13 19:03:51 -05001022
1023/* Specialized version of _PyObject_GenericGetAttrWithDict
1024 specifically for the LOAD_METHOD opcode.
1025
1026 Return 1 if a method is found, 0 if it's a regular attribute
1027 from __dict__ or something returned by using a descriptor
1028 protocol.
1029
1030 `method` will point to the resolved attribute or NULL. In the
1031 latter case, an error will be set.
1032*/
1033int
1034_PyObject_GetMethod(PyObject *obj, PyObject *name, PyObject **method)
1035{
1036 PyTypeObject *tp = Py_TYPE(obj);
1037 PyObject *descr;
1038 descrgetfunc f = NULL;
1039 PyObject **dictptr, *dict;
1040 PyObject *attr;
1041 int meth_found = 0;
1042
1043 assert(*method == NULL);
1044
1045 if (Py_TYPE(obj)->tp_getattro != PyObject_GenericGetAttr
1046 || !PyUnicode_Check(name)) {
1047 *method = PyObject_GetAttr(obj, name);
1048 return 0;
1049 }
1050
1051 if (tp->tp_dict == NULL && PyType_Ready(tp) < 0)
1052 return 0;
1053
1054 descr = _PyType_Lookup(tp, name);
1055 if (descr != NULL) {
1056 Py_INCREF(descr);
INADA Naoki5566bbb2017-02-03 07:43:03 +09001057 if (PyFunction_Check(descr) ||
1058 (Py_TYPE(descr) == &PyMethodDescr_Type)) {
Yury Selivanovf2392132016-12-13 19:03:51 -05001059 meth_found = 1;
1060 } else {
1061 f = descr->ob_type->tp_descr_get;
1062 if (f != NULL && PyDescr_IsData(descr)) {
1063 *method = f(descr, obj, (PyObject *)obj->ob_type);
1064 Py_DECREF(descr);
1065 return 0;
1066 }
1067 }
1068 }
1069
1070 dictptr = _PyObject_GetDictPtr(obj);
1071 if (dictptr != NULL && (dict = *dictptr) != NULL) {
1072 Py_INCREF(dict);
1073 attr = PyDict_GetItem(dict, name);
1074 if (attr != NULL) {
1075 Py_INCREF(attr);
1076 *method = attr;
1077 Py_DECREF(dict);
1078 Py_XDECREF(descr);
1079 return 0;
1080 }
1081 Py_DECREF(dict);
1082 }
1083
1084 if (meth_found) {
1085 *method = descr;
1086 return 1;
1087 }
1088
1089 if (f != NULL) {
1090 *method = f(descr, obj, (PyObject *)Py_TYPE(obj));
1091 Py_DECREF(descr);
1092 return 0;
1093 }
1094
1095 if (descr != NULL) {
1096 *method = descr;
1097 return 0;
1098 }
1099
1100 PyErr_Format(PyExc_AttributeError,
1101 "'%.50s' object has no attribute '%U'",
1102 tp->tp_name, name);
1103 return 0;
1104}
1105
1106/* Generic GetAttr functions - put these in your tp_[gs]etattro slot. */
Michael W. Hudson1593f502004-09-14 17:09:47 +00001107
Raymond Hettinger01538262003-03-17 08:24:35 +00001108PyObject *
Antoine Pitrou1a9a9d52010-08-28 18:17:03 +00001109_PyObject_GenericGetAttrWithDict(PyObject *obj, PyObject *name, PyObject *dict)
Tim Peters6d6c1a32001-08-02 04:15:00 +00001110{
Yury Selivanovf2392132016-12-13 19:03:51 -05001111 /* Make sure the logic of _PyObject_GetMethod is in sync with
1112 this method.
1113 */
1114
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001115 PyTypeObject *tp = Py_TYPE(obj);
1116 PyObject *descr = NULL;
1117 PyObject *res = NULL;
1118 descrgetfunc f;
1119 Py_ssize_t dictoffset;
1120 PyObject **dictptr;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001121
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001122 if (!PyUnicode_Check(name)){
1123 PyErr_Format(PyExc_TypeError,
1124 "attribute name must be string, not '%.200s'",
1125 name->ob_type->tp_name);
1126 return NULL;
1127 }
Serhiy Storchaka55c861f2016-04-17 20:31:51 +03001128 Py_INCREF(name);
Guido van Rossumebca9fc2001-12-04 15:54:53 +00001129
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001130 if (tp->tp_dict == NULL) {
1131 if (PyType_Ready(tp) < 0)
1132 goto done;
1133 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001134
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001135 descr = _PyType_Lookup(tp, name);
Michael W. Hudsonb2c7de42003-08-15 13:07:47 +00001136
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001137 f = NULL;
1138 if (descr != NULL) {
Serhiy Storchaka55c861f2016-04-17 20:31:51 +03001139 Py_INCREF(descr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001140 f = descr->ob_type->tp_descr_get;
1141 if (f != NULL && PyDescr_IsData(descr)) {
1142 res = f(descr, obj, (PyObject *)obj->ob_type);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001143 goto done;
1144 }
1145 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001146
Antoine Pitrou1a9a9d52010-08-28 18:17:03 +00001147 if (dict == NULL) {
1148 /* Inline _PyObject_GetDictPtr */
1149 dictoffset = tp->tp_dictoffset;
1150 if (dictoffset != 0) {
1151 if (dictoffset < 0) {
1152 Py_ssize_t tsize;
1153 size_t size;
Guido van Rossumc66ff442002-08-19 16:50:48 +00001154
Antoine Pitrou1a9a9d52010-08-28 18:17:03 +00001155 tsize = ((PyVarObject *)obj)->ob_size;
1156 if (tsize < 0)
1157 tsize = -tsize;
1158 size = _PyObject_VAR_SIZE(tp, tsize);
Serhiy Storchaka55c861f2016-04-17 20:31:51 +03001159 assert(size <= PY_SSIZE_T_MAX);
Guido van Rossumc66ff442002-08-19 16:50:48 +00001160
Serhiy Storchaka55c861f2016-04-17 20:31:51 +03001161 dictoffset += (Py_ssize_t)size;
Antoine Pitrou1a9a9d52010-08-28 18:17:03 +00001162 assert(dictoffset > 0);
1163 assert(dictoffset % SIZEOF_VOID_P == 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001164 }
Antoine Pitrou1a9a9d52010-08-28 18:17:03 +00001165 dictptr = (PyObject **) ((char *)obj + dictoffset);
1166 dict = *dictptr;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001167 }
1168 }
Antoine Pitrou1a9a9d52010-08-28 18:17:03 +00001169 if (dict != NULL) {
1170 Py_INCREF(dict);
1171 res = PyDict_GetItem(dict, name);
1172 if (res != NULL) {
1173 Py_INCREF(res);
Antoine Pitrou1a9a9d52010-08-28 18:17:03 +00001174 Py_DECREF(dict);
1175 goto done;
1176 }
1177 Py_DECREF(dict);
1178 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001179
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001180 if (f != NULL) {
1181 res = f(descr, obj, (PyObject *)Py_TYPE(obj));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001182 goto done;
1183 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001184
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001185 if (descr != NULL) {
1186 res = descr;
Victor Stinner2d01dc02012-03-09 00:44:13 +01001187 descr = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001188 goto done;
1189 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001190
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001191 PyErr_Format(PyExc_AttributeError,
1192 "'%.50s' object has no attribute '%U'",
1193 tp->tp_name, name);
Guido van Rossumebca9fc2001-12-04 15:54:53 +00001194 done:
Victor Stinner2d01dc02012-03-09 00:44:13 +01001195 Py_XDECREF(descr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001196 Py_DECREF(name);
1197 return res;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001198}
1199
Antoine Pitrou1a9a9d52010-08-28 18:17:03 +00001200PyObject *
1201PyObject_GenericGetAttr(PyObject *obj, PyObject *name)
1202{
1203 return _PyObject_GenericGetAttrWithDict(obj, name, NULL);
1204}
1205
Tim Peters6d6c1a32001-08-02 04:15:00 +00001206int
Antoine Pitrou1a9a9d52010-08-28 18:17:03 +00001207_PyObject_GenericSetAttrWithDict(PyObject *obj, PyObject *name,
1208 PyObject *value, PyObject *dict)
Tim Peters6d6c1a32001-08-02 04:15:00 +00001209{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001210 PyTypeObject *tp = Py_TYPE(obj);
1211 PyObject *descr;
1212 descrsetfunc f;
1213 PyObject **dictptr;
1214 int res = -1;
Guido van Rossumebca9fc2001-12-04 15:54:53 +00001215
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001216 if (!PyUnicode_Check(name)){
1217 PyErr_Format(PyExc_TypeError,
1218 "attribute name must be string, not '%.200s'",
1219 name->ob_type->tp_name);
1220 return -1;
1221 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001222
Benjamin Peterson74529ad2012-03-09 07:25:32 -08001223 if (tp->tp_dict == NULL && PyType_Ready(tp) < 0)
1224 return -1;
1225
1226 Py_INCREF(name);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001227
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001228 descr = _PyType_Lookup(tp, name);
Victor Stinner2d01dc02012-03-09 00:44:13 +01001229
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001230 if (descr != NULL) {
Serhiy Storchaka55c861f2016-04-17 20:31:51 +03001231 Py_INCREF(descr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001232 f = descr->ob_type->tp_descr_set;
Serhiy Storchaka55c861f2016-04-17 20:31:51 +03001233 if (f != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001234 res = f(descr, obj, value);
1235 goto done;
1236 }
1237 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001238
Antoine Pitrou1a9a9d52010-08-28 18:17:03 +00001239 if (dict == NULL) {
1240 dictptr = _PyObject_GetDictPtr(obj);
Serhiy Storchaka55c861f2016-04-17 20:31:51 +03001241 if (dictptr == NULL) {
1242 if (descr == NULL) {
1243 PyErr_Format(PyExc_AttributeError,
1244 "'%.100s' object has no attribute '%U'",
1245 tp->tp_name, name);
1246 }
1247 else {
1248 PyErr_Format(PyExc_AttributeError,
1249 "'%.50s' object attribute '%U' is read-only",
1250 tp->tp_name, name);
1251 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001252 goto done;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001253 }
Serhiy Storchaka55c861f2016-04-17 20:31:51 +03001254 res = _PyObjectDict_SetItem(tp, dictptr, name, value);
Antoine Pitrou1a9a9d52010-08-28 18:17:03 +00001255 }
Serhiy Storchaka55c861f2016-04-17 20:31:51 +03001256 else {
Antoine Pitrou1a9a9d52010-08-28 18:17:03 +00001257 Py_INCREF(dict);
1258 if (value == NULL)
1259 res = PyDict_DelItem(dict, name);
1260 else
1261 res = PyDict_SetItem(dict, name, value);
Benjamin Peterson74529ad2012-03-09 07:25:32 -08001262 Py_DECREF(dict);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001263 }
Serhiy Storchaka55c861f2016-04-17 20:31:51 +03001264 if (res < 0 && PyErr_ExceptionMatches(PyExc_KeyError))
1265 PyErr_SetObject(PyExc_AttributeError, name);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001266
Guido van Rossumebca9fc2001-12-04 15:54:53 +00001267 done:
Victor Stinner2d01dc02012-03-09 00:44:13 +01001268 Py_XDECREF(descr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001269 Py_DECREF(name);
1270 return res;
Guido van Rossum98ff96a1997-05-20 18:34:44 +00001271}
1272
Antoine Pitrou1a9a9d52010-08-28 18:17:03 +00001273int
1274PyObject_GenericSetAttr(PyObject *obj, PyObject *name, PyObject *value)
1275{
1276 return _PyObject_GenericSetAttrWithDict(obj, name, value, NULL);
1277}
1278
Benjamin Peterson8eb12692012-02-19 19:59:10 -05001279int
1280PyObject_GenericSetDict(PyObject *obj, PyObject *value, void *context)
1281{
Serhiy Storchaka576f1322016-01-05 21:27:54 +02001282 PyObject **dictptr = _PyObject_GetDictPtr(obj);
Benjamin Peterson8eb12692012-02-19 19:59:10 -05001283 if (dictptr == NULL) {
1284 PyErr_SetString(PyExc_AttributeError,
1285 "This object has no __dict__");
1286 return -1;
1287 }
1288 if (value == NULL) {
1289 PyErr_SetString(PyExc_TypeError, "cannot delete __dict__");
1290 return -1;
1291 }
1292 if (!PyDict_Check(value)) {
1293 PyErr_Format(PyExc_TypeError,
1294 "__dict__ must be set to a dictionary, "
1295 "not a '%.200s'", Py_TYPE(value)->tp_name);
1296 return -1;
1297 }
Serhiy Storchaka576f1322016-01-05 21:27:54 +02001298 Py_INCREF(value);
Serhiy Storchakaec397562016-04-06 09:50:03 +03001299 Py_XSETREF(*dictptr, value);
Benjamin Peterson8eb12692012-02-19 19:59:10 -05001300 return 0;
1301}
1302
Antoine Pitrou1a9a9d52010-08-28 18:17:03 +00001303
Guido van Rossum6ac258d1993-05-12 08:24:20 +00001304/* Test a value used as condition, e.g., in a for or if statement.
1305 Return -1 if an error occurred */
1306
1307int
Fred Drake100814d2000-07-09 15:48:49 +00001308PyObject_IsTrue(PyObject *v)
Guido van Rossum6ac258d1993-05-12 08:24:20 +00001309{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001310 Py_ssize_t res;
1311 if (v == Py_True)
1312 return 1;
1313 if (v == Py_False)
1314 return 0;
1315 if (v == Py_None)
1316 return 0;
1317 else if (v->ob_type->tp_as_number != NULL &&
1318 v->ob_type->tp_as_number->nb_bool != NULL)
1319 res = (*v->ob_type->tp_as_number->nb_bool)(v);
1320 else if (v->ob_type->tp_as_mapping != NULL &&
1321 v->ob_type->tp_as_mapping->mp_length != NULL)
1322 res = (*v->ob_type->tp_as_mapping->mp_length)(v);
1323 else if (v->ob_type->tp_as_sequence != NULL &&
1324 v->ob_type->tp_as_sequence->sq_length != NULL)
1325 res = (*v->ob_type->tp_as_sequence->sq_length)(v);
1326 else
1327 return 1;
1328 /* if it is negative, it should be either -1 or -2 */
1329 return (res > 0) ? 1 : Py_SAFE_DOWNCAST(res, Py_ssize_t, int);
Guido van Rossum6ac258d1993-05-12 08:24:20 +00001330}
1331
Tim Peters803526b2002-07-07 05:13:56 +00001332/* equivalent of 'not v'
Guido van Rossumc3d3f961998-04-09 17:53:59 +00001333 Return -1 if an error occurred */
1334
1335int
Fred Drake100814d2000-07-09 15:48:49 +00001336PyObject_Not(PyObject *v)
Guido van Rossumc3d3f961998-04-09 17:53:59 +00001337{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001338 int res;
1339 res = PyObject_IsTrue(v);
1340 if (res < 0)
1341 return res;
1342 return res == 0;
Guido van Rossumc3d3f961998-04-09 17:53:59 +00001343}
1344
Guido van Rossum49b11fe1995-01-26 00:38:22 +00001345/* Test whether an object can be called */
1346
1347int
Fred Drake100814d2000-07-09 15:48:49 +00001348PyCallable_Check(PyObject *x)
Guido van Rossum49b11fe1995-01-26 00:38:22 +00001349{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001350 if (x == NULL)
1351 return 0;
1352 return x->ob_type->tp_call != NULL;
Guido van Rossum49b11fe1995-01-26 00:38:22 +00001353}
1354
Tim Peters7eea37e2001-09-04 22:08:56 +00001355
Georg Brandle32b4222007-03-10 22:13:27 +00001356/* Helper for PyObject_Dir without arguments: returns the local scope. */
1357static PyObject *
Guido van Rossumad7d8d12007-04-13 01:39:34 +00001358_dir_locals(void)
Tim Peters305b5852001-09-17 02:38:46 +00001359{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001360 PyObject *names;
Victor Stinner41bb43a2013-10-29 01:19:37 +01001361 PyObject *locals;
Tim Peters305b5852001-09-17 02:38:46 +00001362
Victor Stinner41bb43a2013-10-29 01:19:37 +01001363 locals = PyEval_GetLocals();
1364 if (locals == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001365 return NULL;
Tim Peters305b5852001-09-17 02:38:46 +00001366
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001367 names = PyMapping_Keys(locals);
1368 if (!names)
1369 return NULL;
1370 if (!PyList_Check(names)) {
1371 PyErr_Format(PyExc_TypeError,
1372 "dir(): expected keys() of locals to be a list, "
1373 "not '%.200s'", Py_TYPE(names)->tp_name);
1374 Py_DECREF(names);
1375 return NULL;
1376 }
Benjamin Peterson3bbb7222011-06-11 16:12:08 -05001377 if (PyList_Sort(names)) {
1378 Py_DECREF(names);
1379 return NULL;
1380 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001381 /* the locals don't need to be DECREF'd */
1382 return names;
Georg Brandle32b4222007-03-10 22:13:27 +00001383}
1384
Benjamin Peterson82b00c12011-05-24 11:09:06 -05001385/* Helper for PyObject_Dir: object introspection. */
Georg Brandle32b4222007-03-10 22:13:27 +00001386static PyObject *
1387_dir_object(PyObject *obj)
1388{
Benjamin Peterson3bbb7222011-06-11 16:12:08 -05001389 PyObject *result, *sorted;
Benjamin Petersonce798522012-01-22 11:24:29 -05001390 PyObject *dirfunc = _PyObject_LookupSpecial(obj, &PyId___dir__);
Georg Brandle32b4222007-03-10 22:13:27 +00001391
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001392 assert(obj);
1393 if (dirfunc == NULL) {
Benjamin Peterson82b00c12011-05-24 11:09:06 -05001394 if (!PyErr_Occurred())
1395 PyErr_SetString(PyExc_TypeError, "object does not provide __dir__");
1396 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001397 }
Benjamin Peterson82b00c12011-05-24 11:09:06 -05001398 /* use __dir__ */
Victor Stinnerf17c3de2016-12-06 18:46:19 +01001399 result = _PyObject_CallNoArg(dirfunc);
Benjamin Peterson82b00c12011-05-24 11:09:06 -05001400 Py_DECREF(dirfunc);
1401 if (result == NULL)
1402 return NULL;
Benjamin Peterson3bbb7222011-06-11 16:12:08 -05001403 /* return sorted(result) */
1404 sorted = PySequence_List(result);
1405 Py_DECREF(result);
1406 if (sorted == NULL)
1407 return NULL;
1408 if (PyList_Sort(sorted)) {
1409 Py_DECREF(sorted);
1410 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001411 }
Benjamin Peterson3bbb7222011-06-11 16:12:08 -05001412 return sorted;
Georg Brandle32b4222007-03-10 22:13:27 +00001413}
1414
1415/* Implementation of dir() -- if obj is NULL, returns the names in the current
1416 (local) scope. Otherwise, performs introspection of the object: returns a
1417 sorted list of attribute names (supposedly) accessible from the object
1418*/
1419PyObject *
1420PyObject_Dir(PyObject *obj)
1421{
Benjamin Peterson3bbb7222011-06-11 16:12:08 -05001422 return (obj == NULL) ? _dir_locals() : _dir_object(obj);
Tim Peters7eea37e2001-09-04 22:08:56 +00001423}
Guido van Rossum49b11fe1995-01-26 00:38:22 +00001424
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001425/*
Raymond Hettinger66d2be82011-07-28 09:55:13 -07001426None is a non-NULL undefined value.
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001427There is (and should be!) no way to create other objects of this type,
Guido van Rossum3f5da241990-12-20 15:06:42 +00001428so there is exactly one (which is indestructible, by the way).
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001429*/
1430
Guido van Rossum0c182a11992-03-27 17:26:13 +00001431/* ARGSUSED */
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001432static PyObject *
Fred Drake100814d2000-07-09 15:48:49 +00001433none_repr(PyObject *op)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001434{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001435 return PyUnicode_FromString("None");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001436}
1437
Barry Warsaw9bf16442001-01-23 16:24:35 +00001438/* ARGUSED */
1439static void
Tim Peters803526b2002-07-07 05:13:56 +00001440none_dealloc(PyObject* ignore)
Barry Warsaw9bf16442001-01-23 16:24:35 +00001441{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001442 /* This should never get called, but we also don't want to SEGV if
1443 * we accidentally decref None out of existence.
1444 */
1445 Py_FatalError("deallocating None");
Barry Warsaw9bf16442001-01-23 16:24:35 +00001446}
1447
Benjamin Petersonc4607ae2011-07-29 18:19:43 -05001448static PyObject *
1449none_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
1450{
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +02001451 if (PyTuple_GET_SIZE(args) || (kwargs && PyDict_GET_SIZE(kwargs))) {
Benjamin Petersonc4607ae2011-07-29 18:19:43 -05001452 PyErr_SetString(PyExc_TypeError, "NoneType takes no arguments");
1453 return NULL;
1454 }
1455 Py_RETURN_NONE;
1456}
1457
Raymond Hettinger66d2be82011-07-28 09:55:13 -07001458static int
1459none_bool(PyObject *v)
1460{
1461 return 0;
1462}
1463
1464static PyNumberMethods none_as_number = {
1465 0, /* nb_add */
1466 0, /* nb_subtract */
1467 0, /* nb_multiply */
1468 0, /* nb_remainder */
1469 0, /* nb_divmod */
1470 0, /* nb_power */
1471 0, /* nb_negative */
1472 0, /* nb_positive */
1473 0, /* nb_absolute */
1474 (inquiry)none_bool, /* nb_bool */
1475 0, /* nb_invert */
1476 0, /* nb_lshift */
1477 0, /* nb_rshift */
1478 0, /* nb_and */
1479 0, /* nb_xor */
1480 0, /* nb_or */
1481 0, /* nb_int */
1482 0, /* nb_reserved */
1483 0, /* nb_float */
1484 0, /* nb_inplace_add */
1485 0, /* nb_inplace_subtract */
1486 0, /* nb_inplace_multiply */
1487 0, /* nb_inplace_remainder */
1488 0, /* nb_inplace_power */
1489 0, /* nb_inplace_lshift */
1490 0, /* nb_inplace_rshift */
1491 0, /* nb_inplace_and */
1492 0, /* nb_inplace_xor */
1493 0, /* nb_inplace_or */
1494 0, /* nb_floor_divide */
1495 0, /* nb_true_divide */
1496 0, /* nb_inplace_floor_divide */
1497 0, /* nb_inplace_true_divide */
1498 0, /* nb_index */
1499};
Barry Warsaw9bf16442001-01-23 16:24:35 +00001500
Alexandre Vassalotti65846c62013-11-30 17:55:48 -08001501PyTypeObject _PyNone_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001502 PyVarObject_HEAD_INIT(&PyType_Type, 0)
1503 "NoneType",
1504 0,
1505 0,
1506 none_dealloc, /*tp_dealloc*/ /*never called*/
1507 0, /*tp_print*/
1508 0, /*tp_getattr*/
1509 0, /*tp_setattr*/
1510 0, /*tp_reserved*/
1511 none_repr, /*tp_repr*/
Raymond Hettinger66d2be82011-07-28 09:55:13 -07001512 &none_as_number, /*tp_as_number*/
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001513 0, /*tp_as_sequence*/
1514 0, /*tp_as_mapping*/
1515 0, /*tp_hash */
Benjamin Petersonc4607ae2011-07-29 18:19:43 -05001516 0, /*tp_call */
1517 0, /*tp_str */
1518 0, /*tp_getattro */
1519 0, /*tp_setattro */
1520 0, /*tp_as_buffer */
1521 Py_TPFLAGS_DEFAULT, /*tp_flags */
1522 0, /*tp_doc */
1523 0, /*tp_traverse */
1524 0, /*tp_clear */
1525 0, /*tp_richcompare */
1526 0, /*tp_weaklistoffset */
1527 0, /*tp_iter */
1528 0, /*tp_iternext */
1529 0, /*tp_methods */
1530 0, /*tp_members */
1531 0, /*tp_getset */
1532 0, /*tp_base */
1533 0, /*tp_dict */
1534 0, /*tp_descr_get */
1535 0, /*tp_descr_set */
1536 0, /*tp_dictoffset */
1537 0, /*tp_init */
1538 0, /*tp_alloc */
1539 none_new, /*tp_new */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001540};
1541
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001542PyObject _Py_NoneStruct = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001543 _PyObject_EXTRA_INIT
Alexandre Vassalotti65846c62013-11-30 17:55:48 -08001544 1, &_PyNone_Type
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001545};
1546
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +00001547/* NotImplemented is an object that can be used to signal that an
1548 operation is not implemented for the given type combination. */
1549
1550static PyObject *
1551NotImplemented_repr(PyObject *op)
1552{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001553 return PyUnicode_FromString("NotImplemented");
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +00001554}
1555
Benjamin Peterson18d7d7a2011-07-29 18:27:44 -05001556static PyObject *
Alexandre Vassalottic49477b2013-11-24 02:53:45 -08001557NotImplemented_reduce(PyObject *op)
1558{
1559 return PyUnicode_FromString("NotImplemented");
1560}
1561
1562static PyMethodDef notimplemented_methods[] = {
1563 {"__reduce__", (PyCFunction)NotImplemented_reduce, METH_NOARGS, NULL},
1564 {NULL, NULL}
1565};
1566
1567static PyObject *
Benjamin Peterson18d7d7a2011-07-29 18:27:44 -05001568notimplemented_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
1569{
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +02001570 if (PyTuple_GET_SIZE(args) || (kwargs && PyDict_GET_SIZE(kwargs))) {
Benjamin Peterson18d7d7a2011-07-29 18:27:44 -05001571 PyErr_SetString(PyExc_TypeError, "NotImplementedType takes no arguments");
1572 return NULL;
1573 }
Brian Curtindfc80e32011-08-10 20:28:54 -05001574 Py_RETURN_NOTIMPLEMENTED;
Benjamin Peterson18d7d7a2011-07-29 18:27:44 -05001575}
1576
Armin Ronacher226b1db2012-10-06 14:28:58 +02001577static void
1578notimplemented_dealloc(PyObject* ignore)
1579{
1580 /* This should never get called, but we also don't want to SEGV if
1581 * we accidentally decref NotImplemented out of existence.
1582 */
1583 Py_FatalError("deallocating NotImplemented");
1584}
1585
Alexandre Vassalotti65846c62013-11-30 17:55:48 -08001586PyTypeObject _PyNotImplemented_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001587 PyVarObject_HEAD_INIT(&PyType_Type, 0)
1588 "NotImplementedType",
1589 0,
1590 0,
Armin Ronacher226b1db2012-10-06 14:28:58 +02001591 notimplemented_dealloc, /*tp_dealloc*/ /*never called*/
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001592 0, /*tp_print*/
1593 0, /*tp_getattr*/
1594 0, /*tp_setattr*/
1595 0, /*tp_reserved*/
1596 NotImplemented_repr, /*tp_repr*/
1597 0, /*tp_as_number*/
1598 0, /*tp_as_sequence*/
1599 0, /*tp_as_mapping*/
1600 0, /*tp_hash */
Benjamin Peterson18d7d7a2011-07-29 18:27:44 -05001601 0, /*tp_call */
1602 0, /*tp_str */
1603 0, /*tp_getattro */
1604 0, /*tp_setattro */
1605 0, /*tp_as_buffer */
1606 Py_TPFLAGS_DEFAULT, /*tp_flags */
1607 0, /*tp_doc */
1608 0, /*tp_traverse */
1609 0, /*tp_clear */
1610 0, /*tp_richcompare */
1611 0, /*tp_weaklistoffset */
1612 0, /*tp_iter */
1613 0, /*tp_iternext */
Alexandre Vassalottic49477b2013-11-24 02:53:45 -08001614 notimplemented_methods, /*tp_methods */
Benjamin Peterson18d7d7a2011-07-29 18:27:44 -05001615 0, /*tp_members */
1616 0, /*tp_getset */
1617 0, /*tp_base */
1618 0, /*tp_dict */
1619 0, /*tp_descr_get */
1620 0, /*tp_descr_set */
1621 0, /*tp_dictoffset */
1622 0, /*tp_init */
1623 0, /*tp_alloc */
1624 notimplemented_new, /*tp_new */
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +00001625};
1626
1627PyObject _Py_NotImplementedStruct = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001628 _PyObject_EXTRA_INIT
Alexandre Vassalotti65846c62013-11-30 17:55:48 -08001629 1, &_PyNotImplemented_Type
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +00001630};
1631
Guido van Rossumba21a492001-08-16 08:17:26 +00001632void
1633_Py_ReadyTypes(void)
1634{
Victor Stinner5a1bb4e2014-06-02 14:10:59 +02001635 if (PyType_Ready(&PyBaseObject_Type) < 0)
1636 Py_FatalError("Can't initialize object type");
1637
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001638 if (PyType_Ready(&PyType_Type) < 0)
1639 Py_FatalError("Can't initialize type type");
Guido van Rossumba21a492001-08-16 08:17:26 +00001640
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001641 if (PyType_Ready(&_PyWeakref_RefType) < 0)
1642 Py_FatalError("Can't initialize weakref type");
Fred Drake0a4dd392004-07-02 18:57:45 +00001643
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001644 if (PyType_Ready(&_PyWeakref_CallableProxyType) < 0)
1645 Py_FatalError("Can't initialize callable weakref proxy type");
Benjamin Petersonfd838e62009-04-20 02:09:13 +00001646
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001647 if (PyType_Ready(&_PyWeakref_ProxyType) < 0)
1648 Py_FatalError("Can't initialize weakref proxy type");
Benjamin Petersonfd838e62009-04-20 02:09:13 +00001649
Victor Stinner5a1bb4e2014-06-02 14:10:59 +02001650 if (PyType_Ready(&PyLong_Type) < 0)
1651 Py_FatalError("Can't initialize int type");
1652
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001653 if (PyType_Ready(&PyBool_Type) < 0)
1654 Py_FatalError("Can't initialize bool type");
Guido van Rossum77f6a652002-04-03 22:41:51 +00001655
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001656 if (PyType_Ready(&PyByteArray_Type) < 0)
1657 Py_FatalError("Can't initialize bytearray type");
Guido van Rossum4dfe8a12006-04-22 23:28:04 +00001658
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001659 if (PyType_Ready(&PyBytes_Type) < 0)
1660 Py_FatalError("Can't initialize 'str'");
Guido van Rossumcacfc072002-05-24 19:01:59 +00001661
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001662 if (PyType_Ready(&PyList_Type) < 0)
1663 Py_FatalError("Can't initialize list type");
Guido van Rossumba21a492001-08-16 08:17:26 +00001664
Alexandre Vassalotti65846c62013-11-30 17:55:48 -08001665 if (PyType_Ready(&_PyNone_Type) < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001666 Py_FatalError("Can't initialize None type");
Guido van Rossumba21a492001-08-16 08:17:26 +00001667
Alexandre Vassalotti65846c62013-11-30 17:55:48 -08001668 if (PyType_Ready(&_PyNotImplemented_Type) < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001669 Py_FatalError("Can't initialize NotImplemented type");
Benjamin Petersonae937c02009-04-18 20:54:08 +00001670
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001671 if (PyType_Ready(&PyTraceBack_Type) < 0)
1672 Py_FatalError("Can't initialize traceback type");
Benjamin Petersonae937c02009-04-18 20:54:08 +00001673
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001674 if (PyType_Ready(&PySuper_Type) < 0)
1675 Py_FatalError("Can't initialize super type");
Benjamin Petersonae937c02009-04-18 20:54:08 +00001676
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001677 if (PyType_Ready(&PyRange_Type) < 0)
1678 Py_FatalError("Can't initialize range type");
Benjamin Petersonae937c02009-04-18 20:54:08 +00001679
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001680 if (PyType_Ready(&PyDict_Type) < 0)
1681 Py_FatalError("Can't initialize dict type");
Benjamin Petersonae937c02009-04-18 20:54:08 +00001682
Benjamin Petersondb87c992016-11-06 13:01:07 -08001683 if (PyType_Ready(&PyDictKeys_Type) < 0)
1684 Py_FatalError("Can't initialize dict keys type");
1685
1686 if (PyType_Ready(&PyDictValues_Type) < 0)
1687 Py_FatalError("Can't initialize dict values type");
1688
1689 if (PyType_Ready(&PyDictItems_Type) < 0)
1690 Py_FatalError("Can't initialize dict items type");
1691
Eric Snow96c6af92015-05-29 22:21:39 -06001692 if (PyType_Ready(&PyODict_Type) < 0)
1693 Py_FatalError("Can't initialize OrderedDict type");
1694
1695 if (PyType_Ready(&PyODictKeys_Type) < 0)
1696 Py_FatalError("Can't initialize odict_keys type");
1697
1698 if (PyType_Ready(&PyODictItems_Type) < 0)
1699 Py_FatalError("Can't initialize odict_items type");
1700
1701 if (PyType_Ready(&PyODictValues_Type) < 0)
1702 Py_FatalError("Can't initialize odict_values type");
1703
1704 if (PyType_Ready(&PyODictIter_Type) < 0)
1705 Py_FatalError("Can't initialize odict_keyiterator type");
1706
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001707 if (PyType_Ready(&PySet_Type) < 0)
1708 Py_FatalError("Can't initialize set type");
Benjamin Petersonae937c02009-04-18 20:54:08 +00001709
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001710 if (PyType_Ready(&PyUnicode_Type) < 0)
1711 Py_FatalError("Can't initialize str type");
Benjamin Petersonae937c02009-04-18 20:54:08 +00001712
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001713 if (PyType_Ready(&PySlice_Type) < 0)
1714 Py_FatalError("Can't initialize slice type");
Benjamin Petersonae937c02009-04-18 20:54:08 +00001715
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001716 if (PyType_Ready(&PyStaticMethod_Type) < 0)
1717 Py_FatalError("Can't initialize static method type");
Benjamin Petersonae937c02009-04-18 20:54:08 +00001718
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001719 if (PyType_Ready(&PyComplex_Type) < 0)
1720 Py_FatalError("Can't initialize complex type");
Skip Montanaroba1e0f42009-10-18 14:25:35 +00001721
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001722 if (PyType_Ready(&PyFloat_Type) < 0)
1723 Py_FatalError("Can't initialize float type");
Benjamin Petersonae937c02009-04-18 20:54:08 +00001724
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001725 if (PyType_Ready(&PyFrozenSet_Type) < 0)
1726 Py_FatalError("Can't initialize frozenset type");
Benjamin Petersonae937c02009-04-18 20:54:08 +00001727
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001728 if (PyType_Ready(&PyProperty_Type) < 0)
1729 Py_FatalError("Can't initialize property type");
Benjamin Petersonae937c02009-04-18 20:54:08 +00001730
Stefan Krah9a2d99e2012-02-25 12:24:21 +01001731 if (PyType_Ready(&_PyManagedBuffer_Type) < 0)
1732 Py_FatalError("Can't initialize managed buffer type");
1733
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001734 if (PyType_Ready(&PyMemoryView_Type) < 0)
1735 Py_FatalError("Can't initialize memoryview type");
Benjamin Petersonae937c02009-04-18 20:54:08 +00001736
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001737 if (PyType_Ready(&PyTuple_Type) < 0)
1738 Py_FatalError("Can't initialize tuple type");
Benjamin Petersonae937c02009-04-18 20:54:08 +00001739
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001740 if (PyType_Ready(&PyEnum_Type) < 0)
1741 Py_FatalError("Can't initialize enumerate type");
Benjamin Petersonae937c02009-04-18 20:54:08 +00001742
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001743 if (PyType_Ready(&PyReversed_Type) < 0)
1744 Py_FatalError("Can't initialize reversed type");
Guido van Rossum47b9ff62006-08-24 00:41:19 +00001745
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001746 if (PyType_Ready(&PyStdPrinter_Type) < 0)
1747 Py_FatalError("Can't initialize StdPrinter");
Benjamin Petersonfd838e62009-04-20 02:09:13 +00001748
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001749 if (PyType_Ready(&PyCode_Type) < 0)
1750 Py_FatalError("Can't initialize code type");
Benjamin Petersonfd838e62009-04-20 02:09:13 +00001751
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001752 if (PyType_Ready(&PyFrame_Type) < 0)
1753 Py_FatalError("Can't initialize frame type");
Benjamin Petersonfd838e62009-04-20 02:09:13 +00001754
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001755 if (PyType_Ready(&PyCFunction_Type) < 0)
1756 Py_FatalError("Can't initialize builtin function type");
Benjamin Petersonfd838e62009-04-20 02:09:13 +00001757
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001758 if (PyType_Ready(&PyMethod_Type) < 0)
1759 Py_FatalError("Can't initialize method type");
Benjamin Petersonfd838e62009-04-20 02:09:13 +00001760
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001761 if (PyType_Ready(&PyFunction_Type) < 0)
1762 Py_FatalError("Can't initialize function type");
Benjamin Petersonfd838e62009-04-20 02:09:13 +00001763
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001764 if (PyType_Ready(&PyDictProxy_Type) < 0)
1765 Py_FatalError("Can't initialize dict proxy type");
Benjamin Petersonfd838e62009-04-20 02:09:13 +00001766
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001767 if (PyType_Ready(&PyGen_Type) < 0)
1768 Py_FatalError("Can't initialize generator type");
Benjamin Petersonfd838e62009-04-20 02:09:13 +00001769
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001770 if (PyType_Ready(&PyGetSetDescr_Type) < 0)
1771 Py_FatalError("Can't initialize get-set descriptor type");
Benjamin Petersonfd838e62009-04-20 02:09:13 +00001772
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001773 if (PyType_Ready(&PyWrapperDescr_Type) < 0)
1774 Py_FatalError("Can't initialize wrapper type");
Benjamin Petersonfd838e62009-04-20 02:09:13 +00001775
Benjamin Petersoneff61f62011-09-01 16:32:31 -04001776 if (PyType_Ready(&_PyMethodWrapper_Type) < 0)
1777 Py_FatalError("Can't initialize method wrapper type");
1778
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001779 if (PyType_Ready(&PyEllipsis_Type) < 0)
1780 Py_FatalError("Can't initialize ellipsis type");
Benjamin Petersonfd838e62009-04-20 02:09:13 +00001781
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001782 if (PyType_Ready(&PyMemberDescr_Type) < 0)
1783 Py_FatalError("Can't initialize member descriptor type");
Benjamin Peterson8bc5b682009-05-09 18:10:51 +00001784
Barry Warsaw409da152012-06-03 16:18:47 -04001785 if (PyType_Ready(&_PyNamespace_Type) < 0)
1786 Py_FatalError("Can't initialize namespace type");
Benjamin Petersone8ea97f2012-10-30 23:27:52 -04001787
Benjamin Petersonc4311282012-10-30 23:21:10 -04001788 if (PyType_Ready(&PyCapsule_Type) < 0)
1789 Py_FatalError("Can't initialize capsule type");
1790
1791 if (PyType_Ready(&PyLongRangeIter_Type) < 0)
1792 Py_FatalError("Can't initialize long range iterator type");
1793
1794 if (PyType_Ready(&PyCell_Type) < 0)
1795 Py_FatalError("Can't initialize cell type");
1796
1797 if (PyType_Ready(&PyInstanceMethod_Type) < 0)
1798 Py_FatalError("Can't initialize instance method type");
1799
1800 if (PyType_Ready(&PyClassMethodDescr_Type) < 0)
1801 Py_FatalError("Can't initialize class method descr type");
1802
1803 if (PyType_Ready(&PyMethodDescr_Type) < 0)
1804 Py_FatalError("Can't initialize method descr type");
1805
1806 if (PyType_Ready(&PyCallIter_Type) < 0)
1807 Py_FatalError("Can't initialize call iter type");
1808
1809 if (PyType_Ready(&PySeqIter_Type) < 0)
1810 Py_FatalError("Can't initialize sequence iterator type");
Yury Selivanov5376ba92015-06-22 12:19:30 -04001811
1812 if (PyType_Ready(&PyCoro_Type) < 0)
1813 Py_FatalError("Can't initialize coroutine type");
1814
1815 if (PyType_Ready(&_PyCoroWrapper_Type) < 0)
1816 Py_FatalError("Can't initialize coroutine wrapper type");
Guido van Rossumba21a492001-08-16 08:17:26 +00001817}
1818
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001819
Guido van Rossum84a90321996-05-22 16:34:47 +00001820#ifdef Py_TRACE_REFS
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001821
Guido van Rossumaacdc9d1996-08-12 21:32:12 +00001822void
Fred Drake100814d2000-07-09 15:48:49 +00001823_Py_NewReference(PyObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001824{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001825 _Py_INC_REFTOTAL;
1826 op->ob_refcnt = 1;
1827 _Py_AddToAllObjects(op, 1);
1828 _Py_INC_TPALLOCS(op);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001829}
1830
Guido van Rossumaacdc9d1996-08-12 21:32:12 +00001831void
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02001832_Py_ForgetReference(PyObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001833{
Guido van Rossumbffd6832000-01-20 22:32:56 +00001834#ifdef SLOW_UNREF_CHECK
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02001835 PyObject *p;
Guido van Rossumbffd6832000-01-20 22:32:56 +00001836#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001837 if (op->ob_refcnt < 0)
1838 Py_FatalError("UNREF negative refcnt");
1839 if (op == &refchain ||
1840 op->_ob_prev->_ob_next != op || op->_ob_next->_ob_prev != op) {
1841 fprintf(stderr, "* ob\n");
1842 _PyObject_Dump(op);
1843 fprintf(stderr, "* op->_ob_prev->_ob_next\n");
1844 _PyObject_Dump(op->_ob_prev->_ob_next);
1845 fprintf(stderr, "* op->_ob_next->_ob_prev\n");
1846 _PyObject_Dump(op->_ob_next->_ob_prev);
1847 Py_FatalError("UNREF invalid object");
1848 }
Guido van Rossum2e8f6141992-09-03 20:32:55 +00001849#ifdef SLOW_UNREF_CHECK
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001850 for (p = refchain._ob_next; p != &refchain; p = p->_ob_next) {
1851 if (p == op)
1852 break;
1853 }
1854 if (p == &refchain) /* Not found */
1855 Py_FatalError("UNREF unknown object");
Guido van Rossum2e8f6141992-09-03 20:32:55 +00001856#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001857 op->_ob_next->_ob_prev = op->_ob_prev;
1858 op->_ob_prev->_ob_next = op->_ob_next;
1859 op->_ob_next = op->_ob_prev = NULL;
1860 _Py_INC_TPFREES(op);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001861}
1862
Guido van Rossumaacdc9d1996-08-12 21:32:12 +00001863void
Fred Drake100814d2000-07-09 15:48:49 +00001864_Py_Dealloc(PyObject *op)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001865{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001866 destructor dealloc = Py_TYPE(op)->tp_dealloc;
1867 _Py_ForgetReference(op);
1868 (*dealloc)(op);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001869}
1870
Tim Peters269b2a62003-04-17 19:52:29 +00001871/* Print all live objects. Because PyObject_Print is called, the
1872 * interpreter must be in a healthy state.
1873 */
Guido van Rossumaacdc9d1996-08-12 21:32:12 +00001874void
Fred Drake100814d2000-07-09 15:48:49 +00001875_Py_PrintReferences(FILE *fp)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001876{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001877 PyObject *op;
1878 fprintf(fp, "Remaining objects:\n");
1879 for (op = refchain._ob_next; op != &refchain; op = op->_ob_next) {
1880 fprintf(fp, "%p [%" PY_FORMAT_SIZE_T "d] ", op, op->ob_refcnt);
1881 if (PyObject_Print(op, fp, 0) != 0)
1882 PyErr_Clear();
1883 putc('\n', fp);
1884 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001885}
1886
Tim Peters269b2a62003-04-17 19:52:29 +00001887/* Print the addresses of all live objects. Unlike _Py_PrintReferences, this
1888 * doesn't make any calls to the Python C API, so is always safe to call.
1889 */
1890void
1891_Py_PrintReferenceAddresses(FILE *fp)
1892{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001893 PyObject *op;
1894 fprintf(fp, "Remaining object addresses:\n");
1895 for (op = refchain._ob_next; op != &refchain; op = op->_ob_next)
1896 fprintf(fp, "%p [%" PY_FORMAT_SIZE_T "d] %s\n", op,
1897 op->ob_refcnt, Py_TYPE(op)->tp_name);
Tim Peters269b2a62003-04-17 19:52:29 +00001898}
1899
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +00001900PyObject *
Fred Drake100814d2000-07-09 15:48:49 +00001901_Py_GetObjects(PyObject *self, PyObject *args)
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +00001902{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001903 int i, n;
1904 PyObject *t = NULL;
1905 PyObject *res, *op;
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +00001906
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001907 if (!PyArg_ParseTuple(args, "i|O", &n, &t))
1908 return NULL;
1909 op = refchain._ob_next;
1910 res = PyList_New(0);
1911 if (res == NULL)
1912 return NULL;
1913 for (i = 0; (n == 0 || i < n) && op != &refchain; i++) {
1914 while (op == self || op == args || op == res || op == t ||
1915 (t != NULL && Py_TYPE(op) != (PyTypeObject *) t)) {
1916 op = op->_ob_next;
1917 if (op == &refchain)
1918 return res;
1919 }
1920 if (PyList_Append(res, op) < 0) {
1921 Py_DECREF(res);
1922 return NULL;
1923 }
1924 op = op->_ob_next;
1925 }
1926 return res;
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +00001927}
1928
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001929#endif
Guido van Rossum97ead3f1996-01-12 01:24:09 +00001930
Benjamin Petersonb173f782009-05-05 22:31:58 +00001931
Guido van Rossum84a90321996-05-22 16:34:47 +00001932/* Hack to force loading of abstract.o */
Martin v. Löwis18e16552006-02-15 17:27:45 +00001933Py_ssize_t (*_Py_abstract_hack)(PyObject *) = PyObject_Size;
Guido van Rossume09fb551997-08-05 02:04:34 +00001934
1935
David Malcolm49526f42012-06-22 14:55:41 -04001936void
1937_PyObject_DebugTypeStats(FILE *out)
1938{
1939 _PyCFunction_DebugMallocStats(out);
1940 _PyDict_DebugMallocStats(out);
1941 _PyFloat_DebugMallocStats(out);
1942 _PyFrame_DebugMallocStats(out);
1943 _PyList_DebugMallocStats(out);
1944 _PyMethod_DebugMallocStats(out);
David Malcolm49526f42012-06-22 14:55:41 -04001945 _PyTuple_DebugMallocStats(out);
1946}
Guido van Rossumb18618d2000-05-03 23:44:39 +00001947
Guido van Rossum86610361998-04-10 22:32:46 +00001948/* These methods are used to control infinite recursion in repr, str, print,
1949 etc. Container objects that may recursively contain themselves,
Martin Panter8d56c022016-05-29 04:13:35 +00001950 e.g. builtin dictionaries and lists, should use Py_ReprEnter() and
Guido van Rossum86610361998-04-10 22:32:46 +00001951 Py_ReprLeave() to avoid infinite recursion.
1952
1953 Py_ReprEnter() returns 0 the first time it is called for a particular
1954 object and 1 every time thereafter. It returns -1 if an exception
1955 occurred. Py_ReprLeave() has no return value.
1956
1957 See dictobject.c and listobject.c for examples of use.
1958*/
1959
Guido van Rossum86610361998-04-10 22:32:46 +00001960int
Fred Drake100814d2000-07-09 15:48:49 +00001961Py_ReprEnter(PyObject *obj)
Guido van Rossum86610361998-04-10 22:32:46 +00001962{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001963 PyObject *dict;
1964 PyObject *list;
1965 Py_ssize_t i;
Guido van Rossum86610361998-04-10 22:32:46 +00001966
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001967 dict = PyThreadState_GetDict();
Antoine Pitrou04d17d32014-03-31 22:04:38 +02001968 /* Ignore a missing thread-state, so that this function can be called
1969 early on startup. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001970 if (dict == NULL)
1971 return 0;
Victor Stinner7a07e452013-11-06 18:57:29 +01001972 list = _PyDict_GetItemId(dict, &PyId_Py_Repr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001973 if (list == NULL) {
1974 list = PyList_New(0);
1975 if (list == NULL)
1976 return -1;
Victor Stinner7a07e452013-11-06 18:57:29 +01001977 if (_PyDict_SetItemId(dict, &PyId_Py_Repr, list) < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001978 return -1;
1979 Py_DECREF(list);
1980 }
1981 i = PyList_GET_SIZE(list);
1982 while (--i >= 0) {
1983 if (PyList_GET_ITEM(list, i) == obj)
1984 return 1;
1985 }
Victor Stinnere901d1f2013-07-17 21:58:41 +02001986 if (PyList_Append(list, obj) < 0)
1987 return -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001988 return 0;
Guido van Rossum86610361998-04-10 22:32:46 +00001989}
1990
1991void
Fred Drake100814d2000-07-09 15:48:49 +00001992Py_ReprLeave(PyObject *obj)
Guido van Rossum86610361998-04-10 22:32:46 +00001993{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001994 PyObject *dict;
1995 PyObject *list;
1996 Py_ssize_t i;
Victor Stinner1b634932013-07-16 22:24:44 +02001997 PyObject *error_type, *error_value, *error_traceback;
1998
1999 PyErr_Fetch(&error_type, &error_value, &error_traceback);
Guido van Rossum86610361998-04-10 22:32:46 +00002000
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002001 dict = PyThreadState_GetDict();
2002 if (dict == NULL)
Victor Stinner1b634932013-07-16 22:24:44 +02002003 goto finally;
2004
Victor Stinner7a07e452013-11-06 18:57:29 +01002005 list = _PyDict_GetItemId(dict, &PyId_Py_Repr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002006 if (list == NULL || !PyList_Check(list))
Victor Stinner1b634932013-07-16 22:24:44 +02002007 goto finally;
2008
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002009 i = PyList_GET_SIZE(list);
2010 /* Count backwards because we always expect obj to be list[-1] */
2011 while (--i >= 0) {
2012 if (PyList_GET_ITEM(list, i) == obj) {
2013 PyList_SetSlice(list, i, i + 1, NULL);
2014 break;
2015 }
2016 }
Victor Stinner1b634932013-07-16 22:24:44 +02002017
2018finally:
2019 /* ignore exceptions because there is no way to report them. */
2020 PyErr_Restore(error_type, error_value, error_traceback);
Guido van Rossum86610361998-04-10 22:32:46 +00002021}
Guido van Rossumd724b232000-03-13 16:01:29 +00002022
Tim Peters803526b2002-07-07 05:13:56 +00002023/* Trashcan support. */
Guido van Rossumd724b232000-03-13 16:01:29 +00002024
Eric Snow05351c12017-09-05 21:43:08 -07002025/* Current call-stack depth of tp_dealloc calls. */
2026int _PyTrash_delete_nesting = 0;
2027
2028/* List of objects that still need to be cleaned up, singly linked via their
2029 * gc headers' gc_prev pointers.
2030 */
2031PyObject *_PyTrash_delete_later = NULL;
2032
Tim Peters803526b2002-07-07 05:13:56 +00002033/* Add op to the _PyTrash_delete_later list. Called when the current
2034 * call-stack depth gets large. op must be a currently untracked gc'ed
2035 * object, with refcount 0. Py_DECREF must already have been called on it.
2036 */
Guido van Rossumd724b232000-03-13 16:01:29 +00002037void
Fred Drake100814d2000-07-09 15:48:49 +00002038_PyTrash_deposit_object(PyObject *op)
Guido van Rossumd724b232000-03-13 16:01:29 +00002039{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002040 assert(PyObject_IS_GC(op));
Antoine Pitrou796564c2013-07-30 19:59:21 +02002041 assert(_PyGC_REFS(op) == _PyGC_REFS_UNTRACKED);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002042 assert(op->ob_refcnt == 0);
Eric Snow05351c12017-09-05 21:43:08 -07002043 _Py_AS_GC(op)->gc.gc_prev = (PyGC_Head *)_PyTrash_delete_later;
2044 _PyTrash_delete_later = op;
Guido van Rossumd724b232000-03-13 16:01:29 +00002045}
2046
Antoine Pitrou2b0218a2012-09-06 00:59:49 +02002047/* The equivalent API, using per-thread state recursion info */
2048void
2049_PyTrash_thread_deposit_object(PyObject *op)
2050{
2051 PyThreadState *tstate = PyThreadState_GET();
2052 assert(PyObject_IS_GC(op));
Antoine Pitrou796564c2013-07-30 19:59:21 +02002053 assert(_PyGC_REFS(op) == _PyGC_REFS_UNTRACKED);
Antoine Pitrou2b0218a2012-09-06 00:59:49 +02002054 assert(op->ob_refcnt == 0);
2055 _Py_AS_GC(op)->gc.gc_prev = (PyGC_Head *) tstate->trash_delete_later;
2056 tstate->trash_delete_later = op;
2057}
2058
Tim Peters803526b2002-07-07 05:13:56 +00002059/* Dealloccate all the objects in the _PyTrash_delete_later list. Called when
2060 * the call-stack unwinds again.
2061 */
Guido van Rossumd724b232000-03-13 16:01:29 +00002062void
Fred Drake100814d2000-07-09 15:48:49 +00002063_PyTrash_destroy_chain(void)
Guido van Rossumd724b232000-03-13 16:01:29 +00002064{
Eric Snow05351c12017-09-05 21:43:08 -07002065 while (_PyTrash_delete_later) {
2066 PyObject *op = _PyTrash_delete_later;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002067 destructor dealloc = Py_TYPE(op)->tp_dealloc;
Neil Schemenauerf589c052002-03-29 03:05:54 +00002068
Eric Snow05351c12017-09-05 21:43:08 -07002069 _PyTrash_delete_later =
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002070 (PyObject*) _Py_AS_GC(op)->gc.gc_prev;
Neil Schemenauerf589c052002-03-29 03:05:54 +00002071
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002072 /* Call the deallocator directly. This used to try to
2073 * fool Py_DECREF into calling it indirectly, but
2074 * Py_DECREF was already called on this object, and in
2075 * assorted non-release builds calling Py_DECREF again ends
2076 * up distorting allocation statistics.
2077 */
2078 assert(op->ob_refcnt == 0);
Eric Snow05351c12017-09-05 21:43:08 -07002079 ++_PyTrash_delete_nesting;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002080 (*dealloc)(op);
Eric Snow05351c12017-09-05 21:43:08 -07002081 --_PyTrash_delete_nesting;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002082 }
Guido van Rossumd724b232000-03-13 16:01:29 +00002083}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002084
Antoine Pitrou2b0218a2012-09-06 00:59:49 +02002085/* The equivalent API, using per-thread state recursion info */
2086void
2087_PyTrash_thread_destroy_chain(void)
2088{
2089 PyThreadState *tstate = PyThreadState_GET();
Xiang Zhanga66f9c62017-05-13 13:36:14 +08002090 /* We need to increase trash_delete_nesting here, otherwise,
2091 _PyTrash_thread_destroy_chain will be called recursively
2092 and then possibly crash. An example that may crash without
2093 increase:
2094 N = 500000 # need to be large enough
2095 ob = object()
2096 tups = [(ob,) for i in range(N)]
2097 for i in range(49):
2098 tups = [(tup,) for tup in tups]
2099 del tups
2100 */
2101 assert(tstate->trash_delete_nesting == 0);
2102 ++tstate->trash_delete_nesting;
Antoine Pitrou2b0218a2012-09-06 00:59:49 +02002103 while (tstate->trash_delete_later) {
2104 PyObject *op = tstate->trash_delete_later;
2105 destructor dealloc = Py_TYPE(op)->tp_dealloc;
2106
2107 tstate->trash_delete_later =
2108 (PyObject*) _Py_AS_GC(op)->gc.gc_prev;
2109
2110 /* Call the deallocator directly. This used to try to
2111 * fool Py_DECREF into calling it indirectly, but
2112 * Py_DECREF was already called on this object, and in
2113 * assorted non-release builds calling Py_DECREF again ends
2114 * up distorting allocation statistics.
2115 */
2116 assert(op->ob_refcnt == 0);
Antoine Pitrou2b0218a2012-09-06 00:59:49 +02002117 (*dealloc)(op);
Xiang Zhanga66f9c62017-05-13 13:36:14 +08002118 assert(tstate->trash_delete_nesting == 1);
Antoine Pitrou2b0218a2012-09-06 00:59:49 +02002119 }
Xiang Zhanga66f9c62017-05-13 13:36:14 +08002120 --tstate->trash_delete_nesting;
Antoine Pitrou2b0218a2012-09-06 00:59:49 +02002121}
2122
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00002123#ifndef Py_TRACE_REFS
2124/* For Py_LIMITED_API, we need an out-of-line version of _Py_Dealloc.
2125 Define this here, so we can undefine the macro. */
2126#undef _Py_Dealloc
2127PyAPI_FUNC(void) _Py_Dealloc(PyObject *);
2128void
2129_Py_Dealloc(PyObject *op)
2130{
2131 _Py_INC_TPFREES(op) _Py_COUNT_ALLOCS_COMMA
2132 (*Py_TYPE(op)->tp_dealloc)(op);
2133}
2134#endif
2135
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002136#ifdef __cplusplus
2137}
2138#endif