blob: 8cec6e2f1228760027a42e4c73559da9e188ef96 [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001
Benjamin Peterson722954a2011-06-11 16:33:35 -05002/* Generic object operations; and implementation of None */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003
Guido van Rossumc0b618a1997-05-02 03:12:38 +00004#include "Python.h"
Eric Snow2ebc5ce2017-09-07 23:51:28 -06005#include "internal/pystate.h"
Yury Selivanovf23746a2018-01-22 19:11:18 -05006#include "internal/context.h"
Benjamin Petersonfd838e62009-04-20 02:09:13 +00007#include "frameobject.h"
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00008
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00009#ifdef __cplusplus
10extern "C" {
11#endif
12
Victor Stinnerbd303c12013-11-07 23:07:29 +010013_Py_IDENTIFIER(Py_Repr);
14_Py_IDENTIFIER(__bytes__);
15_Py_IDENTIFIER(__dir__);
16_Py_IDENTIFIER(__isabstractmethod__);
17_Py_IDENTIFIER(builtins);
18
Tim Peters34592512002-07-11 06:23:50 +000019#ifdef Py_REF_DEBUG
Neal Norwitz84632ee2006-03-04 20:00:59 +000020Py_ssize_t _Py_RefTotal;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000021
22Py_ssize_t
23_Py_GetRefTotal(void)
24{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000025 PyObject *o;
26 Py_ssize_t total = _Py_RefTotal;
Antoine Pitrou9d952542013-08-24 21:07:07 +020027 o = _PySet_Dummy;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000028 if (o != NULL)
29 total -= o->ob_refcnt;
30 return total;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000031}
Nick Coghland6009512014-11-20 21:39:37 +100032
33void
34_PyDebug_PrintTotalRefs(void) {
Eric Snowdae02762017-09-14 00:35:58 -070035 fprintf(stderr,
36 "[%" PY_FORMAT_SIZE_T "d refs, "
37 "%" PY_FORMAT_SIZE_T "d blocks]\n",
38 _Py_GetRefTotal(), _Py_GetAllocatedBlocks());
Nick Coghland6009512014-11-20 21:39:37 +100039}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000040#endif /* Py_REF_DEBUG */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000041
Guido van Rossum3f5da241990-12-20 15:06:42 +000042/* Object allocation routines used by NEWOBJ and NEWVAROBJ macros.
43 These are used by the individual routines for object creation.
44 Do not call them otherwise, they do not initialize the object! */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000045
Tim Peters78be7992003-03-23 02:51:01 +000046#ifdef Py_TRACE_REFS
Tim Peters7571a0f2003-03-23 17:52:28 +000047/* Head of circular doubly-linked list of all objects. These are linked
48 * together via the _ob_prev and _ob_next members of a PyObject, which
49 * exist only in a Py_TRACE_REFS build.
50 */
Tim Peters78be7992003-03-23 02:51:01 +000051static PyObject refchain = {&refchain, &refchain};
Tim Peters36eb4df2003-03-23 03:33:13 +000052
Tim Peters7571a0f2003-03-23 17:52:28 +000053/* Insert op at the front of the list of all objects. If force is true,
54 * op is added even if _ob_prev and _ob_next are non-NULL already. If
55 * force is false amd _ob_prev or _ob_next are non-NULL, do nothing.
56 * force should be true if and only if op points to freshly allocated,
57 * uninitialized memory, or you've unlinked op from the list and are
Tim Peters51f8d382003-03-23 18:06:08 +000058 * relinking it into the front.
Tim Peters7571a0f2003-03-23 17:52:28 +000059 * Note that objects are normally added to the list via _Py_NewReference,
60 * which is called by PyObject_Init. Not all objects are initialized that
61 * way, though; exceptions include statically allocated type objects, and
62 * statically allocated singletons (like Py_True and Py_None).
63 */
Tim Peters36eb4df2003-03-23 03:33:13 +000064void
Tim Peters7571a0f2003-03-23 17:52:28 +000065_Py_AddToAllObjects(PyObject *op, int force)
Tim Peters36eb4df2003-03-23 03:33:13 +000066{
Tim Peters7571a0f2003-03-23 17:52:28 +000067#ifdef Py_DEBUG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000068 if (!force) {
69 /* If it's initialized memory, op must be in or out of
70 * the list unambiguously.
71 */
72 assert((op->_ob_prev == NULL) == (op->_ob_next == NULL));
73 }
Tim Peters78be7992003-03-23 02:51:01 +000074#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000075 if (force || op->_ob_prev == NULL) {
76 op->_ob_next = refchain._ob_next;
77 op->_ob_prev = &refchain;
78 refchain._ob_next->_ob_prev = op;
79 refchain._ob_next = op;
80 }
Tim Peters7571a0f2003-03-23 17:52:28 +000081}
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000082#endif /* Py_TRACE_REFS */
Tim Peters78be7992003-03-23 02:51:01 +000083
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +000084#ifdef COUNT_ALLOCS
Guido van Rossumc0b618a1997-05-02 03:12:38 +000085static PyTypeObject *type_list;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000086/* All types are added to type_list, at least when
87 they get one object created. That makes them
88 immortal, which unfortunately contributes to
89 garbage itself. If unlist_types_without_objects
90 is set, they will be removed from the type_list
91 once the last object is deallocated. */
Benjamin Petersona4a37fe2009-01-11 17:13:55 +000092static int unlist_types_without_objects;
93extern Py_ssize_t tuple_zero_allocs, fast_tuple_allocs;
94extern Py_ssize_t quick_int_allocs, quick_neg_int_allocs;
95extern Py_ssize_t null_strings, one_strings;
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +000096void
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000097dump_counts(FILE* f)
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +000098{
Victor Stinner25420fe2017-11-20 18:12:22 -080099 PyInterpreterState *interp = PyThreadState_GET()->interp;
100 if (!inter->core_config.show_alloc_count) {
Serhiy Storchaka7e160ce2016-07-03 21:03:53 +0300101 return;
Victor Stinner25420fe2017-11-20 18:12:22 -0800102 }
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +0000103
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000104 for (tp = type_list; tp; tp = tp->tp_next)
105 fprintf(f, "%s alloc'd: %" PY_FORMAT_SIZE_T "d, "
106 "freed: %" PY_FORMAT_SIZE_T "d, "
107 "max in use: %" PY_FORMAT_SIZE_T "d\n",
108 tp->tp_name, tp->tp_allocs, tp->tp_frees,
109 tp->tp_maxalloc);
110 fprintf(f, "fast tuple allocs: %" PY_FORMAT_SIZE_T "d, "
111 "empty: %" PY_FORMAT_SIZE_T "d\n",
112 fast_tuple_allocs, tuple_zero_allocs);
113 fprintf(f, "fast int allocs: pos: %" PY_FORMAT_SIZE_T "d, "
114 "neg: %" PY_FORMAT_SIZE_T "d\n",
115 quick_int_allocs, quick_neg_int_allocs);
116 fprintf(f, "null strings: %" PY_FORMAT_SIZE_T "d, "
117 "1-strings: %" PY_FORMAT_SIZE_T "d\n",
118 null_strings, one_strings);
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +0000119}
120
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000121PyObject *
Fred Drake100814d2000-07-09 15:48:49 +0000122get_counts(void)
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000123{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000124 PyTypeObject *tp;
125 PyObject *result;
126 PyObject *v;
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000127
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000128 result = PyList_New(0);
129 if (result == NULL)
130 return NULL;
131 for (tp = type_list; tp; tp = tp->tp_next) {
132 v = Py_BuildValue("(snnn)", tp->tp_name, tp->tp_allocs,
133 tp->tp_frees, tp->tp_maxalloc);
134 if (v == NULL) {
135 Py_DECREF(result);
136 return NULL;
137 }
138 if (PyList_Append(result, v) < 0) {
139 Py_DECREF(v);
140 Py_DECREF(result);
141 return NULL;
142 }
143 Py_DECREF(v);
144 }
145 return result;
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000146}
147
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +0000148void
Fred Drake100814d2000-07-09 15:48:49 +0000149inc_count(PyTypeObject *tp)
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +0000150{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000151 if (tp->tp_next == NULL && tp->tp_prev == NULL) {
152 /* first time; insert in linked list */
153 if (tp->tp_next != NULL) /* sanity check */
154 Py_FatalError("XXX inc_count sanity check");
155 if (type_list)
156 type_list->tp_prev = tp;
157 tp->tp_next = type_list;
158 /* Note that as of Python 2.2, heap-allocated type objects
159 * can go away, but this code requires that they stay alive
160 * until program exit. That's why we're careful with
161 * refcounts here. type_list gets a new reference to tp,
162 * while ownership of the reference type_list used to hold
163 * (if any) was transferred to tp->tp_next in the line above.
164 * tp is thus effectively immortal after this.
165 */
166 Py_INCREF(tp);
167 type_list = tp;
Tim Peters3e40c7f2003-03-23 03:04:32 +0000168#ifdef Py_TRACE_REFS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000169 /* Also insert in the doubly-linked list of all objects,
170 * if not already there.
171 */
172 _Py_AddToAllObjects((PyObject *)tp, 0);
Tim Peters78be7992003-03-23 02:51:01 +0000173#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000174 }
175 tp->tp_allocs++;
176 if (tp->tp_allocs - tp->tp_frees > tp->tp_maxalloc)
177 tp->tp_maxalloc = tp->tp_allocs - tp->tp_frees;
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +0000178}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000179
180void dec_count(PyTypeObject *tp)
181{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000182 tp->tp_frees++;
183 if (unlist_types_without_objects &&
184 tp->tp_allocs == tp->tp_frees) {
185 /* unlink the type from type_list */
186 if (tp->tp_prev)
187 tp->tp_prev->tp_next = tp->tp_next;
188 else
189 type_list = tp->tp_next;
190 if (tp->tp_next)
191 tp->tp_next->tp_prev = tp->tp_prev;
192 tp->tp_next = tp->tp_prev = NULL;
193 Py_DECREF(tp);
194 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000195}
196
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +0000197#endif
198
Tim Peters7c321a82002-07-09 02:57:01 +0000199#ifdef Py_REF_DEBUG
200/* Log a fatal error; doesn't return. */
201void
202_Py_NegativeRefcount(const char *fname, int lineno, PyObject *op)
203{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000204 char buf[300];
Tim Peters7c321a82002-07-09 02:57:01 +0000205
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000206 PyOS_snprintf(buf, sizeof(buf),
207 "%s:%i object at %p has negative ref count "
208 "%" PY_FORMAT_SIZE_T "d",
209 fname, lineno, op, op->ob_refcnt);
210 Py_FatalError(buf);
Tim Peters7c321a82002-07-09 02:57:01 +0000211}
212
213#endif /* Py_REF_DEBUG */
214
Thomas Heller1328b522004-04-22 17:23:49 +0000215void
216Py_IncRef(PyObject *o)
217{
218 Py_XINCREF(o);
219}
220
221void
222Py_DecRef(PyObject *o)
223{
224 Py_XDECREF(o);
225}
226
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000227PyObject *
Fred Drake100814d2000-07-09 15:48:49 +0000228PyObject_Init(PyObject *op, PyTypeObject *tp)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000229{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000230 if (op == NULL)
231 return PyErr_NoMemory();
232 /* Any changes should be reflected in PyObject_INIT (objimpl.h) */
233 Py_TYPE(op) = tp;
234 _Py_NewReference(op);
235 return op;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000236}
237
Guido van Rossumb18618d2000-05-03 23:44:39 +0000238PyVarObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000239PyObject_InitVar(PyVarObject *op, PyTypeObject *tp, Py_ssize_t size)
Guido van Rossumb18618d2000-05-03 23:44:39 +0000240{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000241 if (op == NULL)
242 return (PyVarObject *) PyErr_NoMemory();
243 /* Any changes should be reflected in PyObject_INIT_VAR */
244 op->ob_size = size;
245 Py_TYPE(op) = tp;
246 _Py_NewReference((PyObject *)op);
247 return op;
Guido van Rossumb18618d2000-05-03 23:44:39 +0000248}
249
250PyObject *
Fred Drake100814d2000-07-09 15:48:49 +0000251_PyObject_New(PyTypeObject *tp)
Guido van Rossumb18618d2000-05-03 23:44:39 +0000252{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000253 PyObject *op;
254 op = (PyObject *) PyObject_MALLOC(_PyObject_SIZE(tp));
255 if (op == NULL)
256 return PyErr_NoMemory();
257 return PyObject_INIT(op, tp);
Guido van Rossumb18618d2000-05-03 23:44:39 +0000258}
259
Guido van Rossumd0c87ee1997-05-15 21:31:03 +0000260PyVarObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000261_PyObject_NewVar(PyTypeObject *tp, Py_ssize_t nitems)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000262{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000263 PyVarObject *op;
264 const size_t size = _PyObject_VAR_SIZE(tp, nitems);
265 op = (PyVarObject *) PyObject_MALLOC(size);
266 if (op == NULL)
267 return (PyVarObject *)PyErr_NoMemory();
268 return PyObject_INIT_VAR(op, tp, nitems);
Guido van Rossumb18618d2000-05-03 23:44:39 +0000269}
270
Antoine Pitrou796564c2013-07-30 19:59:21 +0200271void
272PyObject_CallFinalizer(PyObject *self)
273{
274 PyTypeObject *tp = Py_TYPE(self);
275
276 /* The former could happen on heaptypes created from the C API, e.g.
277 PyType_FromSpec(). */
278 if (!PyType_HasFeature(tp, Py_TPFLAGS_HAVE_FINALIZE) ||
279 tp->tp_finalize == NULL)
280 return;
281 /* tp_finalize should only be called once. */
282 if (PyType_IS_GC(tp) && _PyGC_FINALIZED(self))
283 return;
284
285 tp->tp_finalize(self);
286 if (PyType_IS_GC(tp))
287 _PyGC_SET_FINALIZED(self, 1);
288}
289
290int
291PyObject_CallFinalizerFromDealloc(PyObject *self)
292{
293 Py_ssize_t refcnt;
294
295 /* Temporarily resurrect the object. */
296 if (self->ob_refcnt != 0) {
297 Py_FatalError("PyObject_CallFinalizerFromDealloc called on "
298 "object with a non-zero refcount");
299 }
300 self->ob_refcnt = 1;
301
302 PyObject_CallFinalizer(self);
303
304 /* Undo the temporary resurrection; can't use DECREF here, it would
305 * cause a recursive call.
306 */
307 assert(self->ob_refcnt > 0);
308 if (--self->ob_refcnt == 0)
309 return 0; /* this is the normal path out */
310
311 /* tp_finalize resurrected it! Make it look like the original Py_DECREF
312 * never happened.
313 */
314 refcnt = self->ob_refcnt;
315 _Py_NewReference(self);
316 self->ob_refcnt = refcnt;
317
318 if (PyType_IS_GC(Py_TYPE(self))) {
319 assert(_PyGC_REFS(self) != _PyGC_REFS_UNTRACKED);
320 }
321 /* If Py_REF_DEBUG, _Py_NewReference bumped _Py_RefTotal, so
322 * we need to undo that. */
323 _Py_DEC_REFTOTAL;
324 /* If Py_TRACE_REFS, _Py_NewReference re-added self to the object
325 * chain, so no more to do there.
326 * If COUNT_ALLOCS, the original decref bumped tp_frees, and
327 * _Py_NewReference bumped tp_allocs: both of those need to be
328 * undone.
329 */
330#ifdef COUNT_ALLOCS
331 --Py_TYPE(self)->tp_frees;
332 --Py_TYPE(self)->tp_allocs;
333#endif
334 return -1;
335}
336
Antoine Pitrouc47bd4a2010-07-27 22:08:27 +0000337int
338PyObject_Print(PyObject *op, FILE *fp, int flags)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000339{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000340 int ret = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000341 if (PyErr_CheckSignals())
342 return -1;
Guido van Rossum9b00dfa1998-04-28 16:06:54 +0000343#ifdef USE_STACKCHECK
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000344 if (PyOS_CheckStack()) {
345 PyErr_SetString(PyExc_MemoryError, "stack overflow");
346 return -1;
347 }
Guido van Rossum9b00dfa1998-04-28 16:06:54 +0000348#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000349 clearerr(fp); /* Clear any previous error condition */
350 if (op == NULL) {
351 Py_BEGIN_ALLOW_THREADS
352 fprintf(fp, "<nil>");
353 Py_END_ALLOW_THREADS
354 }
355 else {
356 if (op->ob_refcnt <= 0)
357 /* XXX(twouters) cast refcount to long until %zd is
358 universally available */
359 Py_BEGIN_ALLOW_THREADS
360 fprintf(fp, "<refcnt %ld at %p>",
361 (long)op->ob_refcnt, op);
362 Py_END_ALLOW_THREADS
363 else {
364 PyObject *s;
365 if (flags & Py_PRINT_RAW)
366 s = PyObject_Str(op);
367 else
368 s = PyObject_Repr(op);
369 if (s == NULL)
370 ret = -1;
371 else if (PyBytes_Check(s)) {
372 fwrite(PyBytes_AS_STRING(s), 1,
373 PyBytes_GET_SIZE(s), fp);
374 }
375 else if (PyUnicode_Check(s)) {
376 PyObject *t;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200377 t = PyUnicode_AsEncodedString(s, "utf-8", "backslashreplace");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000378 if (t == NULL)
379 ret = 0;
380 else {
381 fwrite(PyBytes_AS_STRING(t), 1,
382 PyBytes_GET_SIZE(t), fp);
Victor Stinnerba6b4302010-05-17 09:33:42 +0000383 Py_DECREF(t);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000384 }
385 }
386 else {
387 PyErr_Format(PyExc_TypeError,
388 "str() or repr() returned '%.100s'",
389 s->ob_type->tp_name);
390 ret = -1;
391 }
392 Py_XDECREF(s);
393 }
394 }
395 if (ret == 0) {
396 if (ferror(fp)) {
Serhiy Storchaka55fe1ae2017-04-16 10:46:38 +0300397 PyErr_SetFromErrno(PyExc_OSError);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000398 clearerr(fp);
399 ret = -1;
400 }
401 }
402 return ret;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000403}
404
Guido van Rossum38938152006-08-21 23:36:26 +0000405/* For debugging convenience. Set a breakpoint here and call it from your DLL */
406void
Thomas Woutersb2137042007-02-01 18:02:27 +0000407_Py_BreakPoint(void)
Guido van Rossum38938152006-08-21 23:36:26 +0000408{
409}
410
Neal Norwitz1a997502003-01-13 20:13:12 +0000411
Barry Warsaw9bf16442001-01-23 16:24:35 +0000412/* For debugging convenience. See Misc/gdbinit for some useful gdb hooks */
Guido van Rossum38938152006-08-21 23:36:26 +0000413void
414_PyObject_Dump(PyObject* op)
Barry Warsaw9bf16442001-01-23 16:24:35 +0000415{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000416 if (op == NULL)
417 fprintf(stderr, "NULL\n");
418 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000419 PyGILState_STATE gil;
Victor Stinnere5132102013-08-26 13:49:06 +0200420 PyObject *error_type, *error_value, *error_traceback;
421
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000422 fprintf(stderr, "object : ");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000423 gil = PyGILState_Ensure();
Victor Stinnere5132102013-08-26 13:49:06 +0200424
425 PyErr_Fetch(&error_type, &error_value, &error_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000426 (void)PyObject_Print(op, stderr, 0);
Victor Stinnere5132102013-08-26 13:49:06 +0200427 PyErr_Restore(error_type, error_value, error_traceback);
428
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000429 PyGILState_Release(gil);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000430 /* XXX(twouters) cast refcount to long until %zd is
431 universally available */
432 fprintf(stderr, "\n"
433 "type : %s\n"
434 "refcount: %ld\n"
435 "address : %p\n",
436 Py_TYPE(op)==NULL ? "NULL" : Py_TYPE(op)->tp_name,
437 (long)op->ob_refcnt,
438 op);
439 }
Barry Warsaw9bf16442001-01-23 16:24:35 +0000440}
Barry Warsaw903138f2001-01-23 16:33:18 +0000441
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000442PyObject *
Fred Drake100814d2000-07-09 15:48:49 +0000443PyObject_Repr(PyObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000444{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000445 PyObject *res;
446 if (PyErr_CheckSignals())
447 return NULL;
Guido van Rossum9b00dfa1998-04-28 16:06:54 +0000448#ifdef USE_STACKCHECK
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000449 if (PyOS_CheckStack()) {
450 PyErr_SetString(PyExc_MemoryError, "stack overflow");
451 return NULL;
452 }
Guido van Rossum9b00dfa1998-04-28 16:06:54 +0000453#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000454 if (v == NULL)
455 return PyUnicode_FromString("<NULL>");
456 if (Py_TYPE(v)->tp_repr == NULL)
457 return PyUnicode_FromFormat("<%s object at %p>",
458 v->ob_type->tp_name, v);
Victor Stinner33824f62013-08-26 14:05:19 +0200459
460#ifdef Py_DEBUG
461 /* PyObject_Repr() must not be called with an exception set,
Victor Stinnera8cb5152017-01-18 14:12:51 +0100462 because it can clear it (directly or indirectly) and so the
Martin Panter9955a372015-10-07 10:26:23 +0000463 caller loses its exception */
Victor Stinner33824f62013-08-26 14:05:19 +0200464 assert(!PyErr_Occurred());
465#endif
466
Serhiy Storchaka1fb72d22017-12-03 22:12:11 +0200467 /* It is possible for a type to have a tp_repr representation that loops
468 infinitely. */
469 if (Py_EnterRecursiveCall(" while getting the repr of an object"))
470 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000471 res = (*v->ob_type->tp_repr)(v);
Serhiy Storchaka1fb72d22017-12-03 22:12:11 +0200472 Py_LeaveRecursiveCall();
Victor Stinner0a54cf12011-12-01 03:22:44 +0100473 if (res == NULL)
474 return NULL;
475 if (!PyUnicode_Check(res)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000476 PyErr_Format(PyExc_TypeError,
477 "__repr__ returned non-string (type %.200s)",
478 res->ob_type->tp_name);
479 Py_DECREF(res);
480 return NULL;
481 }
Victor Stinnerdb88ae52011-12-01 02:15:00 +0100482#ifndef Py_DEBUG
483 if (PyUnicode_READY(res) < 0)
484 return NULL;
485#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000486 return res;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000487}
488
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000489PyObject *
Guido van Rossum98297ee2007-11-06 21:34:58 +0000490PyObject_Str(PyObject *v)
Guido van Rossumc6004111993-11-05 10:22:19 +0000491{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000492 PyObject *res;
493 if (PyErr_CheckSignals())
494 return NULL;
Guido van Rossum98297ee2007-11-06 21:34:58 +0000495#ifdef USE_STACKCHECK
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000496 if (PyOS_CheckStack()) {
497 PyErr_SetString(PyExc_MemoryError, "stack overflow");
498 return NULL;
499 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000500#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000501 if (v == NULL)
502 return PyUnicode_FromString("<NULL>");
503 if (PyUnicode_CheckExact(v)) {
Victor Stinnerdb88ae52011-12-01 02:15:00 +0100504#ifndef Py_DEBUG
Victor Stinner4ead7c72011-11-20 19:48:36 +0100505 if (PyUnicode_READY(v) < 0)
506 return NULL;
Victor Stinnerdb88ae52011-12-01 02:15:00 +0100507#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000508 Py_INCREF(v);
509 return v;
510 }
511 if (Py_TYPE(v)->tp_str == NULL)
512 return PyObject_Repr(v);
Guido van Rossum4f288ab2001-05-01 16:53:37 +0000513
Victor Stinner33824f62013-08-26 14:05:19 +0200514#ifdef Py_DEBUG
515 /* PyObject_Str() must not be called with an exception set,
Victor Stinnera8cb5152017-01-18 14:12:51 +0100516 because it can clear it (directly or indirectly) and so the
Nick Coghland979e432014-02-09 10:43:21 +1000517 caller loses its exception */
Victor Stinner33824f62013-08-26 14:05:19 +0200518 assert(!PyErr_Occurred());
519#endif
520
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000521 /* It is possible for a type to have a tp_str representation that loops
522 infinitely. */
523 if (Py_EnterRecursiveCall(" while getting the str of an object"))
524 return NULL;
525 res = (*Py_TYPE(v)->tp_str)(v);
526 Py_LeaveRecursiveCall();
527 if (res == NULL)
528 return NULL;
529 if (!PyUnicode_Check(res)) {
530 PyErr_Format(PyExc_TypeError,
531 "__str__ returned non-string (type %.200s)",
532 Py_TYPE(res)->tp_name);
533 Py_DECREF(res);
534 return NULL;
535 }
Victor Stinnerdb88ae52011-12-01 02:15:00 +0100536#ifndef Py_DEBUG
Victor Stinner4ead7c72011-11-20 19:48:36 +0100537 if (PyUnicode_READY(res) < 0)
538 return NULL;
Victor Stinnerdb88ae52011-12-01 02:15:00 +0100539#endif
Victor Stinner4ead7c72011-11-20 19:48:36 +0100540 assert(_PyUnicode_CheckConsistency(res, 1));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000541 return res;
Neil Schemenauercf52c072005-08-12 17:34:58 +0000542}
543
Georg Brandl559e5d72008-06-11 18:37:52 +0000544PyObject *
545PyObject_ASCII(PyObject *v)
546{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000547 PyObject *repr, *ascii, *res;
Georg Brandl559e5d72008-06-11 18:37:52 +0000548
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000549 repr = PyObject_Repr(v);
550 if (repr == NULL)
551 return NULL;
Georg Brandl559e5d72008-06-11 18:37:52 +0000552
Victor Stinneraf037572013-04-14 18:44:10 +0200553 if (PyUnicode_IS_ASCII(repr))
554 return repr;
555
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000556 /* repr is guaranteed to be a PyUnicode object by PyObject_Repr */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200557 ascii = _PyUnicode_AsASCIIString(repr, "backslashreplace");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000558 Py_DECREF(repr);
559 if (ascii == NULL)
560 return NULL;
Georg Brandl559e5d72008-06-11 18:37:52 +0000561
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000562 res = PyUnicode_DecodeASCII(
563 PyBytes_AS_STRING(ascii),
564 PyBytes_GET_SIZE(ascii),
565 NULL);
566
567 Py_DECREF(ascii);
568 return res;
Georg Brandl559e5d72008-06-11 18:37:52 +0000569}
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000570
Benjamin Petersonc15a0732008-08-26 16:46:47 +0000571PyObject *
572PyObject_Bytes(PyObject *v)
573{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000574 PyObject *result, *func;
Benjamin Petersonc15a0732008-08-26 16:46:47 +0000575
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000576 if (v == NULL)
577 return PyBytes_FromString("<NULL>");
Benjamin Petersonc15a0732008-08-26 16:46:47 +0000578
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000579 if (PyBytes_CheckExact(v)) {
580 Py_INCREF(v);
581 return v;
582 }
Benjamin Petersonc15a0732008-08-26 16:46:47 +0000583
Benjamin Petersonce798522012-01-22 11:24:29 -0500584 func = _PyObject_LookupSpecial(v, &PyId___bytes__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000585 if (func != NULL) {
Victor Stinnerf17c3de2016-12-06 18:46:19 +0100586 result = _PyObject_CallNoArg(func);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000587 Py_DECREF(func);
588 if (result == NULL)
Benjamin Peterson41ece392010-09-11 16:39:57 +0000589 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000590 if (!PyBytes_Check(result)) {
Benjamin Peterson41ece392010-09-11 16:39:57 +0000591 PyErr_Format(PyExc_TypeError,
592 "__bytes__ returned non-bytes (type %.200s)",
593 Py_TYPE(result)->tp_name);
594 Py_DECREF(result);
595 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000596 }
597 return result;
598 }
599 else if (PyErr_Occurred())
600 return NULL;
601 return PyBytes_FromObject(v);
Benjamin Petersonc15a0732008-08-26 16:46:47 +0000602}
603
Mark Dickinsonc008a172009-02-01 13:59:22 +0000604/* For Python 3.0.1 and later, the old three-way comparison has been
605 completely removed in favour of rich comparisons. PyObject_Compare() and
606 PyObject_Cmp() are gone, and the builtin cmp function no longer exists.
Mark Dickinsone94c6792009-02-02 20:36:42 +0000607 The old tp_compare slot has been renamed to tp_reserved, and should no
Mark Dickinsonc008a172009-02-01 13:59:22 +0000608 longer be used. Use tp_richcompare instead.
Guido van Rossum98297ee2007-11-06 21:34:58 +0000609
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000610 See (*) below for practical amendments.
611
Mark Dickinsonc008a172009-02-01 13:59:22 +0000612 tp_richcompare gets called with a first argument of the appropriate type
613 and a second object of an arbitrary type. We never do any kind of
614 coercion.
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000615
Mark Dickinsonc008a172009-02-01 13:59:22 +0000616 The tp_richcompare slot should return an object, as follows:
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000617
618 NULL if an exception occurred
619 NotImplemented if the requested comparison is not implemented
620 any other false value if the requested comparison is false
621 any other true value if the requested comparison is true
622
623 The PyObject_RichCompare[Bool]() wrappers raise TypeError when they get
624 NotImplemented.
625
626 (*) Practical amendments:
627
628 - If rich comparison returns NotImplemented, == and != are decided by
629 comparing the object pointer (i.e. falling back to the base object
630 implementation).
631
Guido van Rossuma4073002002-05-31 20:03:54 +0000632*/
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000633
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000634/* Map rich comparison operators to their swapped version, e.g. LT <--> GT */
Brett Cannona5ca2e72004-09-25 01:37:24 +0000635int _Py_SwappedOp[] = {Py_GT, Py_GE, Py_EQ, Py_NE, Py_LT, Py_LE};
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +0000636
Serhiy Storchaka2d06e842015-12-25 19:53:18 +0200637static const char * const opstrings[] = {"<", "<=", "==", "!=", ">", ">="};
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000638
639/* Perform a rich comparison, raising TypeError when the requested comparison
640 operator is not supported. */
Guido van Rossume797ec12001-01-17 15:24:28 +0000641static PyObject *
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000642do_richcompare(PyObject *v, PyObject *w, int op)
Guido van Rossume797ec12001-01-17 15:24:28 +0000643{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000644 richcmpfunc f;
645 PyObject *res;
646 int checked_reverse_op = 0;
Guido van Rossume797ec12001-01-17 15:24:28 +0000647
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000648 if (v->ob_type != w->ob_type &&
649 PyType_IsSubtype(w->ob_type, v->ob_type) &&
650 (f = w->ob_type->tp_richcompare) != NULL) {
651 checked_reverse_op = 1;
652 res = (*f)(w, v, _Py_SwappedOp[op]);
653 if (res != Py_NotImplemented)
654 return res;
655 Py_DECREF(res);
656 }
657 if ((f = v->ob_type->tp_richcompare) != NULL) {
658 res = (*f)(v, w, op);
659 if (res != Py_NotImplemented)
660 return res;
661 Py_DECREF(res);
662 }
663 if (!checked_reverse_op && (f = w->ob_type->tp_richcompare) != NULL) {
664 res = (*f)(w, v, _Py_SwappedOp[op]);
665 if (res != Py_NotImplemented)
666 return res;
667 Py_DECREF(res);
668 }
669 /* If neither object implements it, provide a sensible default
670 for == and !=, but raise an exception for ordering. */
671 switch (op) {
672 case Py_EQ:
673 res = (v == w) ? Py_True : Py_False;
674 break;
675 case Py_NE:
676 res = (v != w) ? Py_True : Py_False;
677 break;
678 default:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000679 PyErr_Format(PyExc_TypeError,
Victor Stinner91108f02015-10-14 18:25:31 +0200680 "'%s' not supported between instances of '%.100s' and '%.100s'",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000681 opstrings[op],
Victor Stinner91108f02015-10-14 18:25:31 +0200682 v->ob_type->tp_name,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000683 w->ob_type->tp_name);
684 return NULL;
685 }
686 Py_INCREF(res);
687 return res;
Guido van Rossume797ec12001-01-17 15:24:28 +0000688}
689
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000690/* Perform a rich comparison with object result. This wraps do_richcompare()
691 with a check for NULL arguments and a recursion check. */
Guido van Rossume797ec12001-01-17 15:24:28 +0000692
Guido van Rossume797ec12001-01-17 15:24:28 +0000693PyObject *
694PyObject_RichCompare(PyObject *v, PyObject *w, int op)
695{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000696 PyObject *res;
Guido van Rossume797ec12001-01-17 15:24:28 +0000697
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000698 assert(Py_LT <= op && op <= Py_GE);
699 if (v == NULL || w == NULL) {
700 if (!PyErr_Occurred())
701 PyErr_BadInternalCall();
702 return NULL;
703 }
704 if (Py_EnterRecursiveCall(" in comparison"))
705 return NULL;
706 res = do_richcompare(v, w, op);
707 Py_LeaveRecursiveCall();
708 return res;
Guido van Rossume797ec12001-01-17 15:24:28 +0000709}
710
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000711/* Perform a rich comparison with integer result. This wraps
712 PyObject_RichCompare(), returning -1 for error, 0 for false, 1 for true. */
Guido van Rossume797ec12001-01-17 15:24:28 +0000713int
714PyObject_RichCompareBool(PyObject *v, PyObject *w, int op)
715{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000716 PyObject *res;
717 int ok;
Guido van Rossume797ec12001-01-17 15:24:28 +0000718
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000719 /* Quick result when objects are the same.
720 Guarantees that identity implies equality. */
721 if (v == w) {
722 if (op == Py_EQ)
723 return 1;
724 else if (op == Py_NE)
725 return 0;
726 }
Mark Dickinson4a1f5932008-11-12 23:23:36 +0000727
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000728 res = PyObject_RichCompare(v, w, op);
729 if (res == NULL)
730 return -1;
731 if (PyBool_Check(res))
732 ok = (res == Py_True);
733 else
734 ok = PyObject_IsTrue(res);
735 Py_DECREF(res);
736 return ok;
Guido van Rossume797ec12001-01-17 15:24:28 +0000737}
Fred Drake13634cf2000-06-29 19:17:04 +0000738
Antoine Pitrouce4a9da2011-11-21 20:46:33 +0100739Py_hash_t
Nick Coghland1abd252008-07-15 15:46:38 +0000740PyObject_HashNotImplemented(PyObject *v)
741{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000742 PyErr_Format(PyExc_TypeError, "unhashable type: '%.200s'",
743 Py_TYPE(v)->tp_name);
744 return -1;
Nick Coghland1abd252008-07-15 15:46:38 +0000745}
Fred Drake13634cf2000-06-29 19:17:04 +0000746
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000747Py_hash_t
Fred Drake100814d2000-07-09 15:48:49 +0000748PyObject_Hash(PyObject *v)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000749{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000750 PyTypeObject *tp = Py_TYPE(v);
751 if (tp->tp_hash != NULL)
752 return (*tp->tp_hash)(v);
753 /* To keep to the general practice that inheriting
754 * solely from object in C code should work without
755 * an explicit call to PyType_Ready, we implicitly call
756 * PyType_Ready here and then check the tp_hash slot again
757 */
758 if (tp->tp_dict == NULL) {
759 if (PyType_Ready(tp) < 0)
760 return -1;
761 if (tp->tp_hash != NULL)
762 return (*tp->tp_hash)(v);
763 }
764 /* Otherwise, the object can't be hashed */
765 return PyObject_HashNotImplemented(v);
Guido van Rossum9bfef441993-03-29 10:43:31 +0000766}
767
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000768PyObject *
Jeremy Hyltonaf68c872005-12-10 18:50:16 +0000769PyObject_GetAttrString(PyObject *v, const char *name)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000770{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000771 PyObject *w, *res;
Guido van Rossumd8eb1b31996-08-09 20:52:03 +0000772
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000773 if (Py_TYPE(v)->tp_getattr != NULL)
774 return (*Py_TYPE(v)->tp_getattr)(v, (char*)name);
INADA Naoki3e8d6cb2017-02-21 23:57:25 +0900775 w = PyUnicode_FromString(name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000776 if (w == NULL)
777 return NULL;
778 res = PyObject_GetAttr(v, w);
Victor Stinner59af08f2012-03-22 02:09:08 +0100779 Py_DECREF(w);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000780 return res;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000781}
782
783int
Jeremy Hyltonaf68c872005-12-10 18:50:16 +0000784PyObject_HasAttrString(PyObject *v, const char *name)
Guido van Rossumed18fdc1993-07-11 19:55:34 +0000785{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000786 PyObject *res = PyObject_GetAttrString(v, name);
787 if (res != NULL) {
788 Py_DECREF(res);
789 return 1;
790 }
791 PyErr_Clear();
792 return 0;
Guido van Rossumed18fdc1993-07-11 19:55:34 +0000793}
794
795int
Jeremy Hyltonaf68c872005-12-10 18:50:16 +0000796PyObject_SetAttrString(PyObject *v, const char *name, PyObject *w)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000797{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000798 PyObject *s;
799 int res;
Guido van Rossumd8eb1b31996-08-09 20:52:03 +0000800
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000801 if (Py_TYPE(v)->tp_setattr != NULL)
802 return (*Py_TYPE(v)->tp_setattr)(v, (char*)name, w);
803 s = PyUnicode_InternFromString(name);
804 if (s == NULL)
805 return -1;
806 res = PyObject_SetAttr(v, s, w);
807 Py_XDECREF(s);
808 return res;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000809}
810
Benjamin Petersonbfebb7b2011-12-15 15:34:02 -0500811int
812_PyObject_IsAbstract(PyObject *obj)
813{
814 int res;
815 PyObject* isabstract;
Benjamin Petersonbfebb7b2011-12-15 15:34:02 -0500816
817 if (obj == NULL)
818 return 0;
819
820 isabstract = _PyObject_GetAttrId(obj, &PyId___isabstractmethod__);
821 if (isabstract == NULL) {
822 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
823 PyErr_Clear();
824 return 0;
825 }
826 return -1;
827 }
828 res = PyObject_IsTrue(isabstract);
829 Py_DECREF(isabstract);
830 return res;
831}
832
Guido van Rossum98ff96a1997-05-20 18:34:44 +0000833PyObject *
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200834_PyObject_GetAttrId(PyObject *v, _Py_Identifier *name)
835{
836 PyObject *result;
Martin v. Löwisd10759f2011-11-07 13:00:05 +0100837 PyObject *oname = _PyUnicode_FromId(name); /* borrowed */
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200838 if (!oname)
839 return NULL;
840 result = PyObject_GetAttr(v, oname);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200841 return result;
842}
843
844int
845_PyObject_HasAttrId(PyObject *v, _Py_Identifier *name)
846{
847 int result;
Martin v. Löwisd10759f2011-11-07 13:00:05 +0100848 PyObject *oname = _PyUnicode_FromId(name); /* borrowed */
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200849 if (!oname)
850 return -1;
851 result = PyObject_HasAttr(v, oname);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200852 return result;
853}
854
855int
856_PyObject_SetAttrId(PyObject *v, _Py_Identifier *name, PyObject *w)
857{
858 int result;
Martin v. Löwisd10759f2011-11-07 13:00:05 +0100859 PyObject *oname = _PyUnicode_FromId(name); /* borrowed */
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200860 if (!oname)
861 return -1;
862 result = PyObject_SetAttr(v, oname, w);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200863 return result;
864}
865
866PyObject *
Fred Drake100814d2000-07-09 15:48:49 +0000867PyObject_GetAttr(PyObject *v, PyObject *name)
Guido van Rossum98ff96a1997-05-20 18:34:44 +0000868{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000869 PyTypeObject *tp = Py_TYPE(v);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000870
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000871 if (!PyUnicode_Check(name)) {
872 PyErr_Format(PyExc_TypeError,
873 "attribute name must be string, not '%.200s'",
874 name->ob_type->tp_name);
875 return NULL;
876 }
877 if (tp->tp_getattro != NULL)
878 return (*tp->tp_getattro)(v, name);
879 if (tp->tp_getattr != NULL) {
Serhiy Storchaka2a404b62017-01-22 23:07:07 +0200880 const char *name_str = PyUnicode_AsUTF8(name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000881 if (name_str == NULL)
882 return NULL;
Serhiy Storchaka2a404b62017-01-22 23:07:07 +0200883 return (*tp->tp_getattr)(v, (char *)name_str);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000884 }
885 PyErr_Format(PyExc_AttributeError,
886 "'%.50s' object has no attribute '%U'",
887 tp->tp_name, name);
888 return NULL;
Guido van Rossum98ff96a1997-05-20 18:34:44 +0000889}
890
INADA Naoki378edee2018-01-16 20:52:41 +0900891PyObject *
892_PyObject_GetAttrWithoutError(PyObject *v, PyObject *name)
893{
894 PyTypeObject *tp = Py_TYPE(v);
895 PyObject *ret = NULL;
896
897 if (!PyUnicode_Check(name)) {
898 PyErr_Format(PyExc_TypeError,
899 "attribute name must be string, not '%.200s'",
900 name->ob_type->tp_name);
901 return NULL;
902 }
903
904 if (tp->tp_getattro == PyObject_GenericGetAttr) {
905 return _PyObject_GenericGetAttrWithDict(v, name, NULL, 1);
906 }
907 if (tp->tp_getattro != NULL) {
908 ret = (*tp->tp_getattro)(v, name);
909 }
910 else if (tp->tp_getattr != NULL) {
911 const char *name_str = PyUnicode_AsUTF8(name);
912 if (name_str == NULL)
913 return NULL;
914 ret = (*tp->tp_getattr)(v, (char *)name_str);
915 }
916 if (ret == NULL && PyErr_ExceptionMatches(PyExc_AttributeError)) {
917 PyErr_Clear();
918 }
919 return ret;
920}
921
Guido van Rossum98ff96a1997-05-20 18:34:44 +0000922int
Fred Drake100814d2000-07-09 15:48:49 +0000923PyObject_HasAttr(PyObject *v, PyObject *name)
Guido van Rossum98ff96a1997-05-20 18:34:44 +0000924{
INADA Naoki378edee2018-01-16 20:52:41 +0900925 PyObject *res = _PyObject_GetAttrWithoutError(v, name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000926 if (res != NULL) {
927 Py_DECREF(res);
928 return 1;
929 }
930 PyErr_Clear();
931 return 0;
Guido van Rossum98ff96a1997-05-20 18:34:44 +0000932}
933
934int
Fred Drake100814d2000-07-09 15:48:49 +0000935PyObject_SetAttr(PyObject *v, PyObject *name, PyObject *value)
Guido van Rossum98ff96a1997-05-20 18:34:44 +0000936{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000937 PyTypeObject *tp = Py_TYPE(v);
938 int err;
Marc-André Lemburge44e5072000-09-18 16:20:57 +0000939
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000940 if (!PyUnicode_Check(name)) {
941 PyErr_Format(PyExc_TypeError,
942 "attribute name must be string, not '%.200s'",
943 name->ob_type->tp_name);
944 return -1;
945 }
946 Py_INCREF(name);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000947
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000948 PyUnicode_InternInPlace(&name);
949 if (tp->tp_setattro != NULL) {
950 err = (*tp->tp_setattro)(v, name, value);
951 Py_DECREF(name);
952 return err;
953 }
954 if (tp->tp_setattr != NULL) {
Serhiy Storchaka2a404b62017-01-22 23:07:07 +0200955 const char *name_str = PyUnicode_AsUTF8(name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000956 if (name_str == NULL)
957 return -1;
Serhiy Storchaka2a404b62017-01-22 23:07:07 +0200958 err = (*tp->tp_setattr)(v, (char *)name_str, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000959 Py_DECREF(name);
960 return err;
961 }
962 Py_DECREF(name);
963 assert(name->ob_refcnt >= 1);
964 if (tp->tp_getattr == NULL && tp->tp_getattro == NULL)
965 PyErr_Format(PyExc_TypeError,
966 "'%.100s' object has no attributes "
967 "(%s .%U)",
968 tp->tp_name,
969 value==NULL ? "del" : "assign to",
970 name);
971 else
972 PyErr_Format(PyExc_TypeError,
973 "'%.100s' object has only read-only attributes "
974 "(%s .%U)",
975 tp->tp_name,
976 value==NULL ? "del" : "assign to",
977 name);
978 return -1;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000979}
980
981/* Helper to get a pointer to an object's __dict__ slot, if any */
982
983PyObject **
984_PyObject_GetDictPtr(PyObject *obj)
985{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000986 Py_ssize_t dictoffset;
987 PyTypeObject *tp = Py_TYPE(obj);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000988
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000989 dictoffset = tp->tp_dictoffset;
990 if (dictoffset == 0)
991 return NULL;
992 if (dictoffset < 0) {
993 Py_ssize_t tsize;
994 size_t size;
Guido van Rossum2eb0b872002-03-01 22:24:49 +0000995
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000996 tsize = ((PyVarObject *)obj)->ob_size;
997 if (tsize < 0)
998 tsize = -tsize;
999 size = _PyObject_VAR_SIZE(tp, tsize);
Guido van Rossum2eb0b872002-03-01 22:24:49 +00001000
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001001 dictoffset += (long)size;
1002 assert(dictoffset > 0);
1003 assert(dictoffset % SIZEOF_VOID_P == 0);
1004 }
1005 return (PyObject **) ((char *)obj + dictoffset);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001006}
1007
Tim Peters6d6c1a32001-08-02 04:15:00 +00001008PyObject *
Raymond Hettinger1da1dbf2003-03-17 19:46:11 +00001009PyObject_SelfIter(PyObject *obj)
Raymond Hettinger01538262003-03-17 08:24:35 +00001010{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001011 Py_INCREF(obj);
1012 return obj;
Raymond Hettinger01538262003-03-17 08:24:35 +00001013}
1014
Antoine Pitroua7013882012-04-05 00:04:20 +02001015/* Convenience function to get a builtin from its name */
1016PyObject *
1017_PyObject_GetBuiltin(const char *name)
1018{
Victor Stinner53e9ec42013-11-07 00:43:05 +01001019 PyObject *mod_name, *mod, *attr;
1020
Victor Stinnerbd303c12013-11-07 23:07:29 +01001021 mod_name = _PyUnicode_FromId(&PyId_builtins); /* borrowed */
Victor Stinner53e9ec42013-11-07 00:43:05 +01001022 if (mod_name == NULL)
1023 return NULL;
1024 mod = PyImport_Import(mod_name);
Antoine Pitroua7013882012-04-05 00:04:20 +02001025 if (mod == NULL)
1026 return NULL;
1027 attr = PyObject_GetAttrString(mod, name);
1028 Py_DECREF(mod);
1029 return attr;
1030}
1031
Amaury Forgeot d'Arcf343e012009-01-12 23:58:21 +00001032/* Helper used when the __next__ method is removed from a type:
1033 tp_iternext is never NULL and can be safely called without checking
1034 on every iteration.
1035 */
1036
1037PyObject *
1038_PyObject_NextNotImplemented(PyObject *self)
1039{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001040 PyErr_Format(PyExc_TypeError,
1041 "'%.200s' object is not iterable",
1042 Py_TYPE(self)->tp_name);
1043 return NULL;
Amaury Forgeot d'Arcf343e012009-01-12 23:58:21 +00001044}
1045
Yury Selivanovf2392132016-12-13 19:03:51 -05001046
1047/* Specialized version of _PyObject_GenericGetAttrWithDict
1048 specifically for the LOAD_METHOD opcode.
1049
1050 Return 1 if a method is found, 0 if it's a regular attribute
1051 from __dict__ or something returned by using a descriptor
1052 protocol.
1053
1054 `method` will point to the resolved attribute or NULL. In the
1055 latter case, an error will be set.
1056*/
1057int
1058_PyObject_GetMethod(PyObject *obj, PyObject *name, PyObject **method)
1059{
1060 PyTypeObject *tp = Py_TYPE(obj);
1061 PyObject *descr;
1062 descrgetfunc f = NULL;
1063 PyObject **dictptr, *dict;
1064 PyObject *attr;
1065 int meth_found = 0;
1066
1067 assert(*method == NULL);
1068
1069 if (Py_TYPE(obj)->tp_getattro != PyObject_GenericGetAttr
1070 || !PyUnicode_Check(name)) {
1071 *method = PyObject_GetAttr(obj, name);
1072 return 0;
1073 }
1074
1075 if (tp->tp_dict == NULL && PyType_Ready(tp) < 0)
1076 return 0;
1077
1078 descr = _PyType_Lookup(tp, name);
1079 if (descr != NULL) {
1080 Py_INCREF(descr);
INADA Naoki5566bbb2017-02-03 07:43:03 +09001081 if (PyFunction_Check(descr) ||
1082 (Py_TYPE(descr) == &PyMethodDescr_Type)) {
Yury Selivanovf2392132016-12-13 19:03:51 -05001083 meth_found = 1;
1084 } else {
1085 f = descr->ob_type->tp_descr_get;
1086 if (f != NULL && PyDescr_IsData(descr)) {
1087 *method = f(descr, obj, (PyObject *)obj->ob_type);
1088 Py_DECREF(descr);
1089 return 0;
1090 }
1091 }
1092 }
1093
1094 dictptr = _PyObject_GetDictPtr(obj);
1095 if (dictptr != NULL && (dict = *dictptr) != NULL) {
1096 Py_INCREF(dict);
1097 attr = PyDict_GetItem(dict, name);
1098 if (attr != NULL) {
1099 Py_INCREF(attr);
1100 *method = attr;
1101 Py_DECREF(dict);
1102 Py_XDECREF(descr);
1103 return 0;
1104 }
1105 Py_DECREF(dict);
1106 }
1107
1108 if (meth_found) {
1109 *method = descr;
1110 return 1;
1111 }
1112
1113 if (f != NULL) {
1114 *method = f(descr, obj, (PyObject *)Py_TYPE(obj));
1115 Py_DECREF(descr);
1116 return 0;
1117 }
1118
1119 if (descr != NULL) {
1120 *method = descr;
1121 return 0;
1122 }
1123
1124 PyErr_Format(PyExc_AttributeError,
1125 "'%.50s' object has no attribute '%U'",
1126 tp->tp_name, name);
1127 return 0;
1128}
1129
1130/* Generic GetAttr functions - put these in your tp_[gs]etattro slot. */
Michael W. Hudson1593f502004-09-14 17:09:47 +00001131
Raymond Hettinger01538262003-03-17 08:24:35 +00001132PyObject *
INADA Naoki378edee2018-01-16 20:52:41 +09001133_PyObject_GenericGetAttrWithDict(PyObject *obj, PyObject *name,
1134 PyObject *dict, int suppress)
Tim Peters6d6c1a32001-08-02 04:15:00 +00001135{
Yury Selivanovf2392132016-12-13 19:03:51 -05001136 /* Make sure the logic of _PyObject_GetMethod is in sync with
1137 this method.
INADA Naoki378edee2018-01-16 20:52:41 +09001138
1139 When suppress=1, this function suppress AttributeError.
Yury Selivanovf2392132016-12-13 19:03:51 -05001140 */
1141
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001142 PyTypeObject *tp = Py_TYPE(obj);
1143 PyObject *descr = NULL;
1144 PyObject *res = NULL;
1145 descrgetfunc f;
1146 Py_ssize_t dictoffset;
1147 PyObject **dictptr;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001148
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001149 if (!PyUnicode_Check(name)){
1150 PyErr_Format(PyExc_TypeError,
1151 "attribute name must be string, not '%.200s'",
1152 name->ob_type->tp_name);
1153 return NULL;
1154 }
Serhiy Storchaka55c861f2016-04-17 20:31:51 +03001155 Py_INCREF(name);
Guido van Rossumebca9fc2001-12-04 15:54:53 +00001156
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001157 if (tp->tp_dict == NULL) {
1158 if (PyType_Ready(tp) < 0)
1159 goto done;
1160 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001161
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001162 descr = _PyType_Lookup(tp, name);
Michael W. Hudsonb2c7de42003-08-15 13:07:47 +00001163
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001164 f = NULL;
1165 if (descr != NULL) {
Serhiy Storchaka55c861f2016-04-17 20:31:51 +03001166 Py_INCREF(descr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001167 f = descr->ob_type->tp_descr_get;
1168 if (f != NULL && PyDescr_IsData(descr)) {
1169 res = f(descr, obj, (PyObject *)obj->ob_type);
INADA Naoki378edee2018-01-16 20:52:41 +09001170 if (res == NULL && suppress &&
1171 PyErr_ExceptionMatches(PyExc_AttributeError)) {
1172 PyErr_Clear();
1173 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001174 goto done;
1175 }
1176 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001177
Antoine Pitrou1a9a9d52010-08-28 18:17:03 +00001178 if (dict == NULL) {
1179 /* Inline _PyObject_GetDictPtr */
1180 dictoffset = tp->tp_dictoffset;
1181 if (dictoffset != 0) {
1182 if (dictoffset < 0) {
1183 Py_ssize_t tsize;
1184 size_t size;
Guido van Rossumc66ff442002-08-19 16:50:48 +00001185
Antoine Pitrou1a9a9d52010-08-28 18:17:03 +00001186 tsize = ((PyVarObject *)obj)->ob_size;
1187 if (tsize < 0)
1188 tsize = -tsize;
1189 size = _PyObject_VAR_SIZE(tp, tsize);
Serhiy Storchaka55c861f2016-04-17 20:31:51 +03001190 assert(size <= PY_SSIZE_T_MAX);
Guido van Rossumc66ff442002-08-19 16:50:48 +00001191
Serhiy Storchaka55c861f2016-04-17 20:31:51 +03001192 dictoffset += (Py_ssize_t)size;
Antoine Pitrou1a9a9d52010-08-28 18:17:03 +00001193 assert(dictoffset > 0);
1194 assert(dictoffset % SIZEOF_VOID_P == 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001195 }
Antoine Pitrou1a9a9d52010-08-28 18:17:03 +00001196 dictptr = (PyObject **) ((char *)obj + dictoffset);
1197 dict = *dictptr;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001198 }
1199 }
Antoine Pitrou1a9a9d52010-08-28 18:17:03 +00001200 if (dict != NULL) {
1201 Py_INCREF(dict);
1202 res = PyDict_GetItem(dict, name);
1203 if (res != NULL) {
1204 Py_INCREF(res);
Antoine Pitrou1a9a9d52010-08-28 18:17:03 +00001205 Py_DECREF(dict);
1206 goto done;
1207 }
1208 Py_DECREF(dict);
1209 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001210
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001211 if (f != NULL) {
1212 res = f(descr, obj, (PyObject *)Py_TYPE(obj));
INADA Naoki378edee2018-01-16 20:52:41 +09001213 if (res == NULL && suppress &&
1214 PyErr_ExceptionMatches(PyExc_AttributeError)) {
1215 PyErr_Clear();
1216 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001217 goto done;
1218 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001219
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001220 if (descr != NULL) {
1221 res = descr;
Victor Stinner2d01dc02012-03-09 00:44:13 +01001222 descr = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001223 goto done;
1224 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001225
INADA Naoki378edee2018-01-16 20:52:41 +09001226 if (!suppress) {
1227 PyErr_Format(PyExc_AttributeError,
1228 "'%.50s' object has no attribute '%U'",
1229 tp->tp_name, name);
1230 }
Guido van Rossumebca9fc2001-12-04 15:54:53 +00001231 done:
Victor Stinner2d01dc02012-03-09 00:44:13 +01001232 Py_XDECREF(descr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001233 Py_DECREF(name);
1234 return res;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001235}
1236
Antoine Pitrou1a9a9d52010-08-28 18:17:03 +00001237PyObject *
1238PyObject_GenericGetAttr(PyObject *obj, PyObject *name)
1239{
INADA Naoki378edee2018-01-16 20:52:41 +09001240 return _PyObject_GenericGetAttrWithDict(obj, name, NULL, 0);
Antoine Pitrou1a9a9d52010-08-28 18:17:03 +00001241}
1242
Tim Peters6d6c1a32001-08-02 04:15:00 +00001243int
Antoine Pitrou1a9a9d52010-08-28 18:17:03 +00001244_PyObject_GenericSetAttrWithDict(PyObject *obj, PyObject *name,
1245 PyObject *value, PyObject *dict)
Tim Peters6d6c1a32001-08-02 04:15:00 +00001246{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001247 PyTypeObject *tp = Py_TYPE(obj);
1248 PyObject *descr;
1249 descrsetfunc f;
1250 PyObject **dictptr;
1251 int res = -1;
Guido van Rossumebca9fc2001-12-04 15:54:53 +00001252
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001253 if (!PyUnicode_Check(name)){
1254 PyErr_Format(PyExc_TypeError,
1255 "attribute name must be string, not '%.200s'",
1256 name->ob_type->tp_name);
1257 return -1;
1258 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001259
Benjamin Peterson74529ad2012-03-09 07:25:32 -08001260 if (tp->tp_dict == NULL && PyType_Ready(tp) < 0)
1261 return -1;
1262
1263 Py_INCREF(name);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001264
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001265 descr = _PyType_Lookup(tp, name);
Victor Stinner2d01dc02012-03-09 00:44:13 +01001266
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001267 if (descr != NULL) {
Serhiy Storchaka55c861f2016-04-17 20:31:51 +03001268 Py_INCREF(descr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001269 f = descr->ob_type->tp_descr_set;
Serhiy Storchaka55c861f2016-04-17 20:31:51 +03001270 if (f != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001271 res = f(descr, obj, value);
1272 goto done;
1273 }
1274 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001275
Antoine Pitrou1a9a9d52010-08-28 18:17:03 +00001276 if (dict == NULL) {
1277 dictptr = _PyObject_GetDictPtr(obj);
Serhiy Storchaka55c861f2016-04-17 20:31:51 +03001278 if (dictptr == NULL) {
1279 if (descr == NULL) {
1280 PyErr_Format(PyExc_AttributeError,
1281 "'%.100s' object has no attribute '%U'",
1282 tp->tp_name, name);
1283 }
1284 else {
1285 PyErr_Format(PyExc_AttributeError,
1286 "'%.50s' object attribute '%U' is read-only",
1287 tp->tp_name, name);
1288 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001289 goto done;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001290 }
Serhiy Storchaka55c861f2016-04-17 20:31:51 +03001291 res = _PyObjectDict_SetItem(tp, dictptr, name, value);
Antoine Pitrou1a9a9d52010-08-28 18:17:03 +00001292 }
Serhiy Storchaka55c861f2016-04-17 20:31:51 +03001293 else {
Antoine Pitrou1a9a9d52010-08-28 18:17:03 +00001294 Py_INCREF(dict);
1295 if (value == NULL)
1296 res = PyDict_DelItem(dict, name);
1297 else
1298 res = PyDict_SetItem(dict, name, value);
Benjamin Peterson74529ad2012-03-09 07:25:32 -08001299 Py_DECREF(dict);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001300 }
Serhiy Storchaka55c861f2016-04-17 20:31:51 +03001301 if (res < 0 && PyErr_ExceptionMatches(PyExc_KeyError))
1302 PyErr_SetObject(PyExc_AttributeError, name);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001303
Guido van Rossumebca9fc2001-12-04 15:54:53 +00001304 done:
Victor Stinner2d01dc02012-03-09 00:44:13 +01001305 Py_XDECREF(descr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001306 Py_DECREF(name);
1307 return res;
Guido van Rossum98ff96a1997-05-20 18:34:44 +00001308}
1309
Antoine Pitrou1a9a9d52010-08-28 18:17:03 +00001310int
1311PyObject_GenericSetAttr(PyObject *obj, PyObject *name, PyObject *value)
1312{
1313 return _PyObject_GenericSetAttrWithDict(obj, name, value, NULL);
1314}
1315
Benjamin Peterson8eb12692012-02-19 19:59:10 -05001316int
1317PyObject_GenericSetDict(PyObject *obj, PyObject *value, void *context)
1318{
Serhiy Storchaka576f1322016-01-05 21:27:54 +02001319 PyObject **dictptr = _PyObject_GetDictPtr(obj);
Benjamin Peterson8eb12692012-02-19 19:59:10 -05001320 if (dictptr == NULL) {
1321 PyErr_SetString(PyExc_AttributeError,
1322 "This object has no __dict__");
1323 return -1;
1324 }
1325 if (value == NULL) {
1326 PyErr_SetString(PyExc_TypeError, "cannot delete __dict__");
1327 return -1;
1328 }
1329 if (!PyDict_Check(value)) {
1330 PyErr_Format(PyExc_TypeError,
1331 "__dict__ must be set to a dictionary, "
1332 "not a '%.200s'", Py_TYPE(value)->tp_name);
1333 return -1;
1334 }
Serhiy Storchaka576f1322016-01-05 21:27:54 +02001335 Py_INCREF(value);
Serhiy Storchakaec397562016-04-06 09:50:03 +03001336 Py_XSETREF(*dictptr, value);
Benjamin Peterson8eb12692012-02-19 19:59:10 -05001337 return 0;
1338}
1339
Antoine Pitrou1a9a9d52010-08-28 18:17:03 +00001340
Guido van Rossum6ac258d1993-05-12 08:24:20 +00001341/* Test a value used as condition, e.g., in a for or if statement.
1342 Return -1 if an error occurred */
1343
1344int
Fred Drake100814d2000-07-09 15:48:49 +00001345PyObject_IsTrue(PyObject *v)
Guido van Rossum6ac258d1993-05-12 08:24:20 +00001346{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001347 Py_ssize_t res;
1348 if (v == Py_True)
1349 return 1;
1350 if (v == Py_False)
1351 return 0;
1352 if (v == Py_None)
1353 return 0;
1354 else if (v->ob_type->tp_as_number != NULL &&
1355 v->ob_type->tp_as_number->nb_bool != NULL)
1356 res = (*v->ob_type->tp_as_number->nb_bool)(v);
1357 else if (v->ob_type->tp_as_mapping != NULL &&
1358 v->ob_type->tp_as_mapping->mp_length != NULL)
1359 res = (*v->ob_type->tp_as_mapping->mp_length)(v);
1360 else if (v->ob_type->tp_as_sequence != NULL &&
1361 v->ob_type->tp_as_sequence->sq_length != NULL)
1362 res = (*v->ob_type->tp_as_sequence->sq_length)(v);
1363 else
1364 return 1;
1365 /* if it is negative, it should be either -1 or -2 */
1366 return (res > 0) ? 1 : Py_SAFE_DOWNCAST(res, Py_ssize_t, int);
Guido van Rossum6ac258d1993-05-12 08:24:20 +00001367}
1368
Tim Peters803526b2002-07-07 05:13:56 +00001369/* equivalent of 'not v'
Guido van Rossumc3d3f961998-04-09 17:53:59 +00001370 Return -1 if an error occurred */
1371
1372int
Fred Drake100814d2000-07-09 15:48:49 +00001373PyObject_Not(PyObject *v)
Guido van Rossumc3d3f961998-04-09 17:53:59 +00001374{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001375 int res;
1376 res = PyObject_IsTrue(v);
1377 if (res < 0)
1378 return res;
1379 return res == 0;
Guido van Rossumc3d3f961998-04-09 17:53:59 +00001380}
1381
Guido van Rossum49b11fe1995-01-26 00:38:22 +00001382/* Test whether an object can be called */
1383
1384int
Fred Drake100814d2000-07-09 15:48:49 +00001385PyCallable_Check(PyObject *x)
Guido van Rossum49b11fe1995-01-26 00:38:22 +00001386{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001387 if (x == NULL)
1388 return 0;
1389 return x->ob_type->tp_call != NULL;
Guido van Rossum49b11fe1995-01-26 00:38:22 +00001390}
1391
Tim Peters7eea37e2001-09-04 22:08:56 +00001392
Georg Brandle32b4222007-03-10 22:13:27 +00001393/* Helper for PyObject_Dir without arguments: returns the local scope. */
1394static PyObject *
Guido van Rossumad7d8d12007-04-13 01:39:34 +00001395_dir_locals(void)
Tim Peters305b5852001-09-17 02:38:46 +00001396{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001397 PyObject *names;
Victor Stinner41bb43a2013-10-29 01:19:37 +01001398 PyObject *locals;
Tim Peters305b5852001-09-17 02:38:46 +00001399
Victor Stinner41bb43a2013-10-29 01:19:37 +01001400 locals = PyEval_GetLocals();
1401 if (locals == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001402 return NULL;
Tim Peters305b5852001-09-17 02:38:46 +00001403
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001404 names = PyMapping_Keys(locals);
1405 if (!names)
1406 return NULL;
1407 if (!PyList_Check(names)) {
1408 PyErr_Format(PyExc_TypeError,
1409 "dir(): expected keys() of locals to be a list, "
1410 "not '%.200s'", Py_TYPE(names)->tp_name);
1411 Py_DECREF(names);
1412 return NULL;
1413 }
Benjamin Peterson3bbb7222011-06-11 16:12:08 -05001414 if (PyList_Sort(names)) {
1415 Py_DECREF(names);
1416 return NULL;
1417 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001418 /* the locals don't need to be DECREF'd */
1419 return names;
Georg Brandle32b4222007-03-10 22:13:27 +00001420}
1421
Benjamin Peterson82b00c12011-05-24 11:09:06 -05001422/* Helper for PyObject_Dir: object introspection. */
Georg Brandle32b4222007-03-10 22:13:27 +00001423static PyObject *
1424_dir_object(PyObject *obj)
1425{
Benjamin Peterson3bbb7222011-06-11 16:12:08 -05001426 PyObject *result, *sorted;
Benjamin Petersonce798522012-01-22 11:24:29 -05001427 PyObject *dirfunc = _PyObject_LookupSpecial(obj, &PyId___dir__);
Georg Brandle32b4222007-03-10 22:13:27 +00001428
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001429 assert(obj);
1430 if (dirfunc == NULL) {
Benjamin Peterson82b00c12011-05-24 11:09:06 -05001431 if (!PyErr_Occurred())
1432 PyErr_SetString(PyExc_TypeError, "object does not provide __dir__");
1433 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001434 }
Benjamin Peterson82b00c12011-05-24 11:09:06 -05001435 /* use __dir__ */
Victor Stinnerf17c3de2016-12-06 18:46:19 +01001436 result = _PyObject_CallNoArg(dirfunc);
Benjamin Peterson82b00c12011-05-24 11:09:06 -05001437 Py_DECREF(dirfunc);
1438 if (result == NULL)
1439 return NULL;
Benjamin Peterson3bbb7222011-06-11 16:12:08 -05001440 /* return sorted(result) */
1441 sorted = PySequence_List(result);
1442 Py_DECREF(result);
1443 if (sorted == NULL)
1444 return NULL;
1445 if (PyList_Sort(sorted)) {
1446 Py_DECREF(sorted);
1447 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001448 }
Benjamin Peterson3bbb7222011-06-11 16:12:08 -05001449 return sorted;
Georg Brandle32b4222007-03-10 22:13:27 +00001450}
1451
1452/* Implementation of dir() -- if obj is NULL, returns the names in the current
1453 (local) scope. Otherwise, performs introspection of the object: returns a
1454 sorted list of attribute names (supposedly) accessible from the object
1455*/
1456PyObject *
1457PyObject_Dir(PyObject *obj)
1458{
Benjamin Peterson3bbb7222011-06-11 16:12:08 -05001459 return (obj == NULL) ? _dir_locals() : _dir_object(obj);
Tim Peters7eea37e2001-09-04 22:08:56 +00001460}
Guido van Rossum49b11fe1995-01-26 00:38:22 +00001461
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001462/*
Raymond Hettinger66d2be82011-07-28 09:55:13 -07001463None is a non-NULL undefined value.
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001464There is (and should be!) no way to create other objects of this type,
Guido van Rossum3f5da241990-12-20 15:06:42 +00001465so there is exactly one (which is indestructible, by the way).
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001466*/
1467
Guido van Rossum0c182a11992-03-27 17:26:13 +00001468/* ARGSUSED */
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001469static PyObject *
Fred Drake100814d2000-07-09 15:48:49 +00001470none_repr(PyObject *op)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001471{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001472 return PyUnicode_FromString("None");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001473}
1474
Barry Warsaw9bf16442001-01-23 16:24:35 +00001475/* ARGUSED */
1476static void
Tim Peters803526b2002-07-07 05:13:56 +00001477none_dealloc(PyObject* ignore)
Barry Warsaw9bf16442001-01-23 16:24:35 +00001478{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001479 /* This should never get called, but we also don't want to SEGV if
1480 * we accidentally decref None out of existence.
1481 */
1482 Py_FatalError("deallocating None");
Barry Warsaw9bf16442001-01-23 16:24:35 +00001483}
1484
Benjamin Petersonc4607ae2011-07-29 18:19:43 -05001485static PyObject *
1486none_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
1487{
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +02001488 if (PyTuple_GET_SIZE(args) || (kwargs && PyDict_GET_SIZE(kwargs))) {
Benjamin Petersonc4607ae2011-07-29 18:19:43 -05001489 PyErr_SetString(PyExc_TypeError, "NoneType takes no arguments");
1490 return NULL;
1491 }
1492 Py_RETURN_NONE;
1493}
1494
Raymond Hettinger66d2be82011-07-28 09:55:13 -07001495static int
1496none_bool(PyObject *v)
1497{
1498 return 0;
1499}
1500
1501static PyNumberMethods none_as_number = {
1502 0, /* nb_add */
1503 0, /* nb_subtract */
1504 0, /* nb_multiply */
1505 0, /* nb_remainder */
1506 0, /* nb_divmod */
1507 0, /* nb_power */
1508 0, /* nb_negative */
1509 0, /* nb_positive */
1510 0, /* nb_absolute */
1511 (inquiry)none_bool, /* nb_bool */
1512 0, /* nb_invert */
1513 0, /* nb_lshift */
1514 0, /* nb_rshift */
1515 0, /* nb_and */
1516 0, /* nb_xor */
1517 0, /* nb_or */
1518 0, /* nb_int */
1519 0, /* nb_reserved */
1520 0, /* nb_float */
1521 0, /* nb_inplace_add */
1522 0, /* nb_inplace_subtract */
1523 0, /* nb_inplace_multiply */
1524 0, /* nb_inplace_remainder */
1525 0, /* nb_inplace_power */
1526 0, /* nb_inplace_lshift */
1527 0, /* nb_inplace_rshift */
1528 0, /* nb_inplace_and */
1529 0, /* nb_inplace_xor */
1530 0, /* nb_inplace_or */
1531 0, /* nb_floor_divide */
1532 0, /* nb_true_divide */
1533 0, /* nb_inplace_floor_divide */
1534 0, /* nb_inplace_true_divide */
1535 0, /* nb_index */
1536};
Barry Warsaw9bf16442001-01-23 16:24:35 +00001537
Alexandre Vassalotti65846c62013-11-30 17:55:48 -08001538PyTypeObject _PyNone_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001539 PyVarObject_HEAD_INIT(&PyType_Type, 0)
1540 "NoneType",
1541 0,
1542 0,
1543 none_dealloc, /*tp_dealloc*/ /*never called*/
1544 0, /*tp_print*/
1545 0, /*tp_getattr*/
1546 0, /*tp_setattr*/
1547 0, /*tp_reserved*/
1548 none_repr, /*tp_repr*/
Raymond Hettinger66d2be82011-07-28 09:55:13 -07001549 &none_as_number, /*tp_as_number*/
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001550 0, /*tp_as_sequence*/
1551 0, /*tp_as_mapping*/
1552 0, /*tp_hash */
Benjamin Petersonc4607ae2011-07-29 18:19:43 -05001553 0, /*tp_call */
1554 0, /*tp_str */
1555 0, /*tp_getattro */
1556 0, /*tp_setattro */
1557 0, /*tp_as_buffer */
1558 Py_TPFLAGS_DEFAULT, /*tp_flags */
1559 0, /*tp_doc */
1560 0, /*tp_traverse */
1561 0, /*tp_clear */
1562 0, /*tp_richcompare */
1563 0, /*tp_weaklistoffset */
1564 0, /*tp_iter */
1565 0, /*tp_iternext */
1566 0, /*tp_methods */
1567 0, /*tp_members */
1568 0, /*tp_getset */
1569 0, /*tp_base */
1570 0, /*tp_dict */
1571 0, /*tp_descr_get */
1572 0, /*tp_descr_set */
1573 0, /*tp_dictoffset */
1574 0, /*tp_init */
1575 0, /*tp_alloc */
1576 none_new, /*tp_new */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001577};
1578
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001579PyObject _Py_NoneStruct = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001580 _PyObject_EXTRA_INIT
Alexandre Vassalotti65846c62013-11-30 17:55:48 -08001581 1, &_PyNone_Type
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001582};
1583
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +00001584/* NotImplemented is an object that can be used to signal that an
1585 operation is not implemented for the given type combination. */
1586
1587static PyObject *
1588NotImplemented_repr(PyObject *op)
1589{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001590 return PyUnicode_FromString("NotImplemented");
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +00001591}
1592
Benjamin Peterson18d7d7a2011-07-29 18:27:44 -05001593static PyObject *
Alexandre Vassalottic49477b2013-11-24 02:53:45 -08001594NotImplemented_reduce(PyObject *op)
1595{
1596 return PyUnicode_FromString("NotImplemented");
1597}
1598
1599static PyMethodDef notimplemented_methods[] = {
1600 {"__reduce__", (PyCFunction)NotImplemented_reduce, METH_NOARGS, NULL},
1601 {NULL, NULL}
1602};
1603
1604static PyObject *
Benjamin Peterson18d7d7a2011-07-29 18:27:44 -05001605notimplemented_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
1606{
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +02001607 if (PyTuple_GET_SIZE(args) || (kwargs && PyDict_GET_SIZE(kwargs))) {
Benjamin Peterson18d7d7a2011-07-29 18:27:44 -05001608 PyErr_SetString(PyExc_TypeError, "NotImplementedType takes no arguments");
1609 return NULL;
1610 }
Brian Curtindfc80e32011-08-10 20:28:54 -05001611 Py_RETURN_NOTIMPLEMENTED;
Benjamin Peterson18d7d7a2011-07-29 18:27:44 -05001612}
1613
Armin Ronacher226b1db2012-10-06 14:28:58 +02001614static void
1615notimplemented_dealloc(PyObject* ignore)
1616{
1617 /* This should never get called, but we also don't want to SEGV if
1618 * we accidentally decref NotImplemented out of existence.
1619 */
1620 Py_FatalError("deallocating NotImplemented");
1621}
1622
Alexandre Vassalotti65846c62013-11-30 17:55:48 -08001623PyTypeObject _PyNotImplemented_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001624 PyVarObject_HEAD_INIT(&PyType_Type, 0)
1625 "NotImplementedType",
1626 0,
1627 0,
Armin Ronacher226b1db2012-10-06 14:28:58 +02001628 notimplemented_dealloc, /*tp_dealloc*/ /*never called*/
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001629 0, /*tp_print*/
1630 0, /*tp_getattr*/
1631 0, /*tp_setattr*/
1632 0, /*tp_reserved*/
1633 NotImplemented_repr, /*tp_repr*/
1634 0, /*tp_as_number*/
1635 0, /*tp_as_sequence*/
1636 0, /*tp_as_mapping*/
1637 0, /*tp_hash */
Benjamin Peterson18d7d7a2011-07-29 18:27:44 -05001638 0, /*tp_call */
1639 0, /*tp_str */
1640 0, /*tp_getattro */
1641 0, /*tp_setattro */
1642 0, /*tp_as_buffer */
1643 Py_TPFLAGS_DEFAULT, /*tp_flags */
1644 0, /*tp_doc */
1645 0, /*tp_traverse */
1646 0, /*tp_clear */
1647 0, /*tp_richcompare */
1648 0, /*tp_weaklistoffset */
1649 0, /*tp_iter */
1650 0, /*tp_iternext */
Alexandre Vassalottic49477b2013-11-24 02:53:45 -08001651 notimplemented_methods, /*tp_methods */
Benjamin Peterson18d7d7a2011-07-29 18:27:44 -05001652 0, /*tp_members */
1653 0, /*tp_getset */
1654 0, /*tp_base */
1655 0, /*tp_dict */
1656 0, /*tp_descr_get */
1657 0, /*tp_descr_set */
1658 0, /*tp_dictoffset */
1659 0, /*tp_init */
1660 0, /*tp_alloc */
1661 notimplemented_new, /*tp_new */
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +00001662};
1663
1664PyObject _Py_NotImplementedStruct = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001665 _PyObject_EXTRA_INIT
Alexandre Vassalotti65846c62013-11-30 17:55:48 -08001666 1, &_PyNotImplemented_Type
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +00001667};
1668
Guido van Rossumba21a492001-08-16 08:17:26 +00001669void
1670_Py_ReadyTypes(void)
1671{
Victor Stinner5a1bb4e2014-06-02 14:10:59 +02001672 if (PyType_Ready(&PyBaseObject_Type) < 0)
1673 Py_FatalError("Can't initialize object type");
1674
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001675 if (PyType_Ready(&PyType_Type) < 0)
1676 Py_FatalError("Can't initialize type type");
Guido van Rossumba21a492001-08-16 08:17:26 +00001677
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001678 if (PyType_Ready(&_PyWeakref_RefType) < 0)
1679 Py_FatalError("Can't initialize weakref type");
Fred Drake0a4dd392004-07-02 18:57:45 +00001680
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001681 if (PyType_Ready(&_PyWeakref_CallableProxyType) < 0)
1682 Py_FatalError("Can't initialize callable weakref proxy type");
Benjamin Petersonfd838e62009-04-20 02:09:13 +00001683
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001684 if (PyType_Ready(&_PyWeakref_ProxyType) < 0)
1685 Py_FatalError("Can't initialize weakref proxy type");
Benjamin Petersonfd838e62009-04-20 02:09:13 +00001686
Victor Stinner5a1bb4e2014-06-02 14:10:59 +02001687 if (PyType_Ready(&PyLong_Type) < 0)
1688 Py_FatalError("Can't initialize int type");
1689
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001690 if (PyType_Ready(&PyBool_Type) < 0)
1691 Py_FatalError("Can't initialize bool type");
Guido van Rossum77f6a652002-04-03 22:41:51 +00001692
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001693 if (PyType_Ready(&PyByteArray_Type) < 0)
1694 Py_FatalError("Can't initialize bytearray type");
Guido van Rossum4dfe8a12006-04-22 23:28:04 +00001695
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001696 if (PyType_Ready(&PyBytes_Type) < 0)
1697 Py_FatalError("Can't initialize 'str'");
Guido van Rossumcacfc072002-05-24 19:01:59 +00001698
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001699 if (PyType_Ready(&PyList_Type) < 0)
1700 Py_FatalError("Can't initialize list type");
Guido van Rossumba21a492001-08-16 08:17:26 +00001701
Alexandre Vassalotti65846c62013-11-30 17:55:48 -08001702 if (PyType_Ready(&_PyNone_Type) < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001703 Py_FatalError("Can't initialize None type");
Guido van Rossumba21a492001-08-16 08:17:26 +00001704
Alexandre Vassalotti65846c62013-11-30 17:55:48 -08001705 if (PyType_Ready(&_PyNotImplemented_Type) < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001706 Py_FatalError("Can't initialize NotImplemented type");
Benjamin Petersonae937c02009-04-18 20:54:08 +00001707
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001708 if (PyType_Ready(&PyTraceBack_Type) < 0)
1709 Py_FatalError("Can't initialize traceback type");
Benjamin Petersonae937c02009-04-18 20:54:08 +00001710
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001711 if (PyType_Ready(&PySuper_Type) < 0)
1712 Py_FatalError("Can't initialize super type");
Benjamin Petersonae937c02009-04-18 20:54:08 +00001713
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001714 if (PyType_Ready(&PyRange_Type) < 0)
1715 Py_FatalError("Can't initialize range type");
Benjamin Petersonae937c02009-04-18 20:54:08 +00001716
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001717 if (PyType_Ready(&PyDict_Type) < 0)
1718 Py_FatalError("Can't initialize dict type");
Benjamin Petersonae937c02009-04-18 20:54:08 +00001719
Benjamin Petersondb87c992016-11-06 13:01:07 -08001720 if (PyType_Ready(&PyDictKeys_Type) < 0)
1721 Py_FatalError("Can't initialize dict keys type");
1722
1723 if (PyType_Ready(&PyDictValues_Type) < 0)
1724 Py_FatalError("Can't initialize dict values type");
1725
1726 if (PyType_Ready(&PyDictItems_Type) < 0)
1727 Py_FatalError("Can't initialize dict items type");
1728
Eric Snow96c6af92015-05-29 22:21:39 -06001729 if (PyType_Ready(&PyODict_Type) < 0)
1730 Py_FatalError("Can't initialize OrderedDict type");
1731
1732 if (PyType_Ready(&PyODictKeys_Type) < 0)
1733 Py_FatalError("Can't initialize odict_keys type");
1734
1735 if (PyType_Ready(&PyODictItems_Type) < 0)
1736 Py_FatalError("Can't initialize odict_items type");
1737
1738 if (PyType_Ready(&PyODictValues_Type) < 0)
1739 Py_FatalError("Can't initialize odict_values type");
1740
1741 if (PyType_Ready(&PyODictIter_Type) < 0)
1742 Py_FatalError("Can't initialize odict_keyiterator type");
1743
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001744 if (PyType_Ready(&PySet_Type) < 0)
1745 Py_FatalError("Can't initialize set type");
Benjamin Petersonae937c02009-04-18 20:54:08 +00001746
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001747 if (PyType_Ready(&PyUnicode_Type) < 0)
1748 Py_FatalError("Can't initialize str type");
Benjamin Petersonae937c02009-04-18 20:54:08 +00001749
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001750 if (PyType_Ready(&PySlice_Type) < 0)
1751 Py_FatalError("Can't initialize slice type");
Benjamin Petersonae937c02009-04-18 20:54:08 +00001752
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001753 if (PyType_Ready(&PyStaticMethod_Type) < 0)
1754 Py_FatalError("Can't initialize static method type");
Benjamin Petersonae937c02009-04-18 20:54:08 +00001755
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001756 if (PyType_Ready(&PyComplex_Type) < 0)
1757 Py_FatalError("Can't initialize complex type");
Skip Montanaroba1e0f42009-10-18 14:25:35 +00001758
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001759 if (PyType_Ready(&PyFloat_Type) < 0)
1760 Py_FatalError("Can't initialize float type");
Benjamin Petersonae937c02009-04-18 20:54:08 +00001761
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001762 if (PyType_Ready(&PyFrozenSet_Type) < 0)
1763 Py_FatalError("Can't initialize frozenset type");
Benjamin Petersonae937c02009-04-18 20:54:08 +00001764
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001765 if (PyType_Ready(&PyProperty_Type) < 0)
1766 Py_FatalError("Can't initialize property type");
Benjamin Petersonae937c02009-04-18 20:54:08 +00001767
Stefan Krah9a2d99e2012-02-25 12:24:21 +01001768 if (PyType_Ready(&_PyManagedBuffer_Type) < 0)
1769 Py_FatalError("Can't initialize managed buffer type");
1770
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001771 if (PyType_Ready(&PyMemoryView_Type) < 0)
1772 Py_FatalError("Can't initialize memoryview type");
Benjamin Petersonae937c02009-04-18 20:54:08 +00001773
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001774 if (PyType_Ready(&PyTuple_Type) < 0)
1775 Py_FatalError("Can't initialize tuple type");
Benjamin Petersonae937c02009-04-18 20:54:08 +00001776
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001777 if (PyType_Ready(&PyEnum_Type) < 0)
1778 Py_FatalError("Can't initialize enumerate type");
Benjamin Petersonae937c02009-04-18 20:54:08 +00001779
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001780 if (PyType_Ready(&PyReversed_Type) < 0)
1781 Py_FatalError("Can't initialize reversed type");
Guido van Rossum47b9ff62006-08-24 00:41:19 +00001782
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001783 if (PyType_Ready(&PyStdPrinter_Type) < 0)
1784 Py_FatalError("Can't initialize StdPrinter");
Benjamin Petersonfd838e62009-04-20 02:09:13 +00001785
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001786 if (PyType_Ready(&PyCode_Type) < 0)
1787 Py_FatalError("Can't initialize code type");
Benjamin Petersonfd838e62009-04-20 02:09:13 +00001788
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001789 if (PyType_Ready(&PyFrame_Type) < 0)
1790 Py_FatalError("Can't initialize frame type");
Benjamin Petersonfd838e62009-04-20 02:09:13 +00001791
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001792 if (PyType_Ready(&PyCFunction_Type) < 0)
1793 Py_FatalError("Can't initialize builtin function type");
Benjamin Petersonfd838e62009-04-20 02:09:13 +00001794
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001795 if (PyType_Ready(&PyMethod_Type) < 0)
1796 Py_FatalError("Can't initialize method type");
Benjamin Petersonfd838e62009-04-20 02:09:13 +00001797
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001798 if (PyType_Ready(&PyFunction_Type) < 0)
1799 Py_FatalError("Can't initialize function type");
Benjamin Petersonfd838e62009-04-20 02:09:13 +00001800
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001801 if (PyType_Ready(&PyDictProxy_Type) < 0)
1802 Py_FatalError("Can't initialize dict proxy type");
Benjamin Petersonfd838e62009-04-20 02:09:13 +00001803
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001804 if (PyType_Ready(&PyGen_Type) < 0)
1805 Py_FatalError("Can't initialize generator type");
Benjamin Petersonfd838e62009-04-20 02:09:13 +00001806
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001807 if (PyType_Ready(&PyGetSetDescr_Type) < 0)
1808 Py_FatalError("Can't initialize get-set descriptor type");
Benjamin Petersonfd838e62009-04-20 02:09:13 +00001809
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001810 if (PyType_Ready(&PyWrapperDescr_Type) < 0)
1811 Py_FatalError("Can't initialize wrapper type");
Benjamin Petersonfd838e62009-04-20 02:09:13 +00001812
Benjamin Petersoneff61f62011-09-01 16:32:31 -04001813 if (PyType_Ready(&_PyMethodWrapper_Type) < 0)
1814 Py_FatalError("Can't initialize method wrapper type");
1815
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001816 if (PyType_Ready(&PyEllipsis_Type) < 0)
1817 Py_FatalError("Can't initialize ellipsis type");
Benjamin Petersonfd838e62009-04-20 02:09:13 +00001818
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001819 if (PyType_Ready(&PyMemberDescr_Type) < 0)
1820 Py_FatalError("Can't initialize member descriptor type");
Benjamin Peterson8bc5b682009-05-09 18:10:51 +00001821
Barry Warsaw409da152012-06-03 16:18:47 -04001822 if (PyType_Ready(&_PyNamespace_Type) < 0)
1823 Py_FatalError("Can't initialize namespace type");
Benjamin Petersone8ea97f2012-10-30 23:27:52 -04001824
Benjamin Petersonc4311282012-10-30 23:21:10 -04001825 if (PyType_Ready(&PyCapsule_Type) < 0)
1826 Py_FatalError("Can't initialize capsule type");
1827
1828 if (PyType_Ready(&PyLongRangeIter_Type) < 0)
1829 Py_FatalError("Can't initialize long range iterator type");
1830
1831 if (PyType_Ready(&PyCell_Type) < 0)
1832 Py_FatalError("Can't initialize cell type");
1833
1834 if (PyType_Ready(&PyInstanceMethod_Type) < 0)
1835 Py_FatalError("Can't initialize instance method type");
1836
1837 if (PyType_Ready(&PyClassMethodDescr_Type) < 0)
1838 Py_FatalError("Can't initialize class method descr type");
1839
1840 if (PyType_Ready(&PyMethodDescr_Type) < 0)
1841 Py_FatalError("Can't initialize method descr type");
1842
1843 if (PyType_Ready(&PyCallIter_Type) < 0)
1844 Py_FatalError("Can't initialize call iter type");
1845
1846 if (PyType_Ready(&PySeqIter_Type) < 0)
1847 Py_FatalError("Can't initialize sequence iterator type");
Yury Selivanov5376ba92015-06-22 12:19:30 -04001848
1849 if (PyType_Ready(&PyCoro_Type) < 0)
1850 Py_FatalError("Can't initialize coroutine type");
1851
1852 if (PyType_Ready(&_PyCoroWrapper_Type) < 0)
1853 Py_FatalError("Can't initialize coroutine wrapper type");
Guido van Rossumba21a492001-08-16 08:17:26 +00001854}
1855
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001856
Guido van Rossum84a90321996-05-22 16:34:47 +00001857#ifdef Py_TRACE_REFS
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001858
Guido van Rossumaacdc9d1996-08-12 21:32:12 +00001859void
Fred Drake100814d2000-07-09 15:48:49 +00001860_Py_NewReference(PyObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001861{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001862 _Py_INC_REFTOTAL;
1863 op->ob_refcnt = 1;
1864 _Py_AddToAllObjects(op, 1);
1865 _Py_INC_TPALLOCS(op);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001866}
1867
Guido van Rossumaacdc9d1996-08-12 21:32:12 +00001868void
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02001869_Py_ForgetReference(PyObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001870{
Guido van Rossumbffd6832000-01-20 22:32:56 +00001871#ifdef SLOW_UNREF_CHECK
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02001872 PyObject *p;
Guido van Rossumbffd6832000-01-20 22:32:56 +00001873#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001874 if (op->ob_refcnt < 0)
1875 Py_FatalError("UNREF negative refcnt");
1876 if (op == &refchain ||
1877 op->_ob_prev->_ob_next != op || op->_ob_next->_ob_prev != op) {
1878 fprintf(stderr, "* ob\n");
1879 _PyObject_Dump(op);
1880 fprintf(stderr, "* op->_ob_prev->_ob_next\n");
1881 _PyObject_Dump(op->_ob_prev->_ob_next);
1882 fprintf(stderr, "* op->_ob_next->_ob_prev\n");
1883 _PyObject_Dump(op->_ob_next->_ob_prev);
1884 Py_FatalError("UNREF invalid object");
1885 }
Guido van Rossum2e8f6141992-09-03 20:32:55 +00001886#ifdef SLOW_UNREF_CHECK
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001887 for (p = refchain._ob_next; p != &refchain; p = p->_ob_next) {
1888 if (p == op)
1889 break;
1890 }
1891 if (p == &refchain) /* Not found */
1892 Py_FatalError("UNREF unknown object");
Guido van Rossum2e8f6141992-09-03 20:32:55 +00001893#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001894 op->_ob_next->_ob_prev = op->_ob_prev;
1895 op->_ob_prev->_ob_next = op->_ob_next;
1896 op->_ob_next = op->_ob_prev = NULL;
1897 _Py_INC_TPFREES(op);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001898}
1899
Guido van Rossumaacdc9d1996-08-12 21:32:12 +00001900void
Fred Drake100814d2000-07-09 15:48:49 +00001901_Py_Dealloc(PyObject *op)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001902{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001903 destructor dealloc = Py_TYPE(op)->tp_dealloc;
1904 _Py_ForgetReference(op);
1905 (*dealloc)(op);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001906}
1907
Tim Peters269b2a62003-04-17 19:52:29 +00001908/* Print all live objects. Because PyObject_Print is called, the
1909 * interpreter must be in a healthy state.
1910 */
Guido van Rossumaacdc9d1996-08-12 21:32:12 +00001911void
Fred Drake100814d2000-07-09 15:48:49 +00001912_Py_PrintReferences(FILE *fp)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001913{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001914 PyObject *op;
1915 fprintf(fp, "Remaining objects:\n");
1916 for (op = refchain._ob_next; op != &refchain; op = op->_ob_next) {
1917 fprintf(fp, "%p [%" PY_FORMAT_SIZE_T "d] ", op, op->ob_refcnt);
1918 if (PyObject_Print(op, fp, 0) != 0)
1919 PyErr_Clear();
1920 putc('\n', fp);
1921 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001922}
1923
Tim Peters269b2a62003-04-17 19:52:29 +00001924/* Print the addresses of all live objects. Unlike _Py_PrintReferences, this
1925 * doesn't make any calls to the Python C API, so is always safe to call.
1926 */
1927void
1928_Py_PrintReferenceAddresses(FILE *fp)
1929{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001930 PyObject *op;
1931 fprintf(fp, "Remaining object addresses:\n");
1932 for (op = refchain._ob_next; op != &refchain; op = op->_ob_next)
1933 fprintf(fp, "%p [%" PY_FORMAT_SIZE_T "d] %s\n", op,
1934 op->ob_refcnt, Py_TYPE(op)->tp_name);
Tim Peters269b2a62003-04-17 19:52:29 +00001935}
1936
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +00001937PyObject *
Fred Drake100814d2000-07-09 15:48:49 +00001938_Py_GetObjects(PyObject *self, PyObject *args)
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +00001939{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001940 int i, n;
1941 PyObject *t = NULL;
1942 PyObject *res, *op;
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +00001943
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001944 if (!PyArg_ParseTuple(args, "i|O", &n, &t))
1945 return NULL;
1946 op = refchain._ob_next;
1947 res = PyList_New(0);
1948 if (res == NULL)
1949 return NULL;
1950 for (i = 0; (n == 0 || i < n) && op != &refchain; i++) {
1951 while (op == self || op == args || op == res || op == t ||
1952 (t != NULL && Py_TYPE(op) != (PyTypeObject *) t)) {
1953 op = op->_ob_next;
1954 if (op == &refchain)
1955 return res;
1956 }
1957 if (PyList_Append(res, op) < 0) {
1958 Py_DECREF(res);
1959 return NULL;
1960 }
1961 op = op->_ob_next;
1962 }
1963 return res;
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +00001964}
1965
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001966#endif
Guido van Rossum97ead3f1996-01-12 01:24:09 +00001967
Benjamin Petersonb173f782009-05-05 22:31:58 +00001968
Guido van Rossum84a90321996-05-22 16:34:47 +00001969/* Hack to force loading of abstract.o */
Martin v. Löwis18e16552006-02-15 17:27:45 +00001970Py_ssize_t (*_Py_abstract_hack)(PyObject *) = PyObject_Size;
Guido van Rossume09fb551997-08-05 02:04:34 +00001971
1972
David Malcolm49526f42012-06-22 14:55:41 -04001973void
1974_PyObject_DebugTypeStats(FILE *out)
1975{
1976 _PyCFunction_DebugMallocStats(out);
1977 _PyDict_DebugMallocStats(out);
1978 _PyFloat_DebugMallocStats(out);
1979 _PyFrame_DebugMallocStats(out);
1980 _PyList_DebugMallocStats(out);
1981 _PyMethod_DebugMallocStats(out);
David Malcolm49526f42012-06-22 14:55:41 -04001982 _PyTuple_DebugMallocStats(out);
1983}
Guido van Rossumb18618d2000-05-03 23:44:39 +00001984
Guido van Rossum86610361998-04-10 22:32:46 +00001985/* These methods are used to control infinite recursion in repr, str, print,
1986 etc. Container objects that may recursively contain themselves,
Martin Panter8d56c022016-05-29 04:13:35 +00001987 e.g. builtin dictionaries and lists, should use Py_ReprEnter() and
Guido van Rossum86610361998-04-10 22:32:46 +00001988 Py_ReprLeave() to avoid infinite recursion.
1989
1990 Py_ReprEnter() returns 0 the first time it is called for a particular
1991 object and 1 every time thereafter. It returns -1 if an exception
1992 occurred. Py_ReprLeave() has no return value.
1993
1994 See dictobject.c and listobject.c for examples of use.
1995*/
1996
Guido van Rossum86610361998-04-10 22:32:46 +00001997int
Fred Drake100814d2000-07-09 15:48:49 +00001998Py_ReprEnter(PyObject *obj)
Guido van Rossum86610361998-04-10 22:32:46 +00001999{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002000 PyObject *dict;
2001 PyObject *list;
2002 Py_ssize_t i;
Guido van Rossum86610361998-04-10 22:32:46 +00002003
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002004 dict = PyThreadState_GetDict();
Antoine Pitrou04d17d32014-03-31 22:04:38 +02002005 /* Ignore a missing thread-state, so that this function can be called
2006 early on startup. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002007 if (dict == NULL)
2008 return 0;
Victor Stinner7a07e452013-11-06 18:57:29 +01002009 list = _PyDict_GetItemId(dict, &PyId_Py_Repr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002010 if (list == NULL) {
2011 list = PyList_New(0);
2012 if (list == NULL)
2013 return -1;
Victor Stinner7a07e452013-11-06 18:57:29 +01002014 if (_PyDict_SetItemId(dict, &PyId_Py_Repr, list) < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002015 return -1;
2016 Py_DECREF(list);
2017 }
2018 i = PyList_GET_SIZE(list);
2019 while (--i >= 0) {
2020 if (PyList_GET_ITEM(list, i) == obj)
2021 return 1;
2022 }
Victor Stinnere901d1f2013-07-17 21:58:41 +02002023 if (PyList_Append(list, obj) < 0)
2024 return -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002025 return 0;
Guido van Rossum86610361998-04-10 22:32:46 +00002026}
2027
2028void
Fred Drake100814d2000-07-09 15:48:49 +00002029Py_ReprLeave(PyObject *obj)
Guido van Rossum86610361998-04-10 22:32:46 +00002030{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002031 PyObject *dict;
2032 PyObject *list;
2033 Py_ssize_t i;
Victor Stinner1b634932013-07-16 22:24:44 +02002034 PyObject *error_type, *error_value, *error_traceback;
2035
2036 PyErr_Fetch(&error_type, &error_value, &error_traceback);
Guido van Rossum86610361998-04-10 22:32:46 +00002037
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002038 dict = PyThreadState_GetDict();
2039 if (dict == NULL)
Victor Stinner1b634932013-07-16 22:24:44 +02002040 goto finally;
2041
Victor Stinner7a07e452013-11-06 18:57:29 +01002042 list = _PyDict_GetItemId(dict, &PyId_Py_Repr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002043 if (list == NULL || !PyList_Check(list))
Victor Stinner1b634932013-07-16 22:24:44 +02002044 goto finally;
2045
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002046 i = PyList_GET_SIZE(list);
2047 /* Count backwards because we always expect obj to be list[-1] */
2048 while (--i >= 0) {
2049 if (PyList_GET_ITEM(list, i) == obj) {
2050 PyList_SetSlice(list, i, i + 1, NULL);
2051 break;
2052 }
2053 }
Victor Stinner1b634932013-07-16 22:24:44 +02002054
2055finally:
2056 /* ignore exceptions because there is no way to report them. */
2057 PyErr_Restore(error_type, error_value, error_traceback);
Guido van Rossum86610361998-04-10 22:32:46 +00002058}
Guido van Rossumd724b232000-03-13 16:01:29 +00002059
Tim Peters803526b2002-07-07 05:13:56 +00002060/* Trashcan support. */
Guido van Rossumd724b232000-03-13 16:01:29 +00002061
Tim Peters803526b2002-07-07 05:13:56 +00002062/* Add op to the _PyTrash_delete_later list. Called when the current
2063 * call-stack depth gets large. op must be a currently untracked gc'ed
2064 * object, with refcount 0. Py_DECREF must already have been called on it.
2065 */
Guido van Rossumd724b232000-03-13 16:01:29 +00002066void
Fred Drake100814d2000-07-09 15:48:49 +00002067_PyTrash_deposit_object(PyObject *op)
Guido van Rossumd724b232000-03-13 16:01:29 +00002068{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002069 assert(PyObject_IS_GC(op));
Antoine Pitrou796564c2013-07-30 19:59:21 +02002070 assert(_PyGC_REFS(op) == _PyGC_REFS_UNTRACKED);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002071 assert(op->ob_refcnt == 0);
Eric Snow2ebc5ce2017-09-07 23:51:28 -06002072 _Py_AS_GC(op)->gc.gc_prev = (PyGC_Head *)_PyRuntime.gc.trash_delete_later;
2073 _PyRuntime.gc.trash_delete_later = op;
Guido van Rossumd724b232000-03-13 16:01:29 +00002074}
2075
Antoine Pitrou2b0218a2012-09-06 00:59:49 +02002076/* The equivalent API, using per-thread state recursion info */
2077void
2078_PyTrash_thread_deposit_object(PyObject *op)
2079{
2080 PyThreadState *tstate = PyThreadState_GET();
2081 assert(PyObject_IS_GC(op));
Antoine Pitrou796564c2013-07-30 19:59:21 +02002082 assert(_PyGC_REFS(op) == _PyGC_REFS_UNTRACKED);
Antoine Pitrou2b0218a2012-09-06 00:59:49 +02002083 assert(op->ob_refcnt == 0);
2084 _Py_AS_GC(op)->gc.gc_prev = (PyGC_Head *) tstate->trash_delete_later;
2085 tstate->trash_delete_later = op;
2086}
2087
Tim Peters803526b2002-07-07 05:13:56 +00002088/* Dealloccate all the objects in the _PyTrash_delete_later list. Called when
2089 * the call-stack unwinds again.
2090 */
Guido van Rossumd724b232000-03-13 16:01:29 +00002091void
Fred Drake100814d2000-07-09 15:48:49 +00002092_PyTrash_destroy_chain(void)
Guido van Rossumd724b232000-03-13 16:01:29 +00002093{
Eric Snow2ebc5ce2017-09-07 23:51:28 -06002094 while (_PyRuntime.gc.trash_delete_later) {
2095 PyObject *op = _PyRuntime.gc.trash_delete_later;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002096 destructor dealloc = Py_TYPE(op)->tp_dealloc;
Neil Schemenauerf589c052002-03-29 03:05:54 +00002097
Eric Snow2ebc5ce2017-09-07 23:51:28 -06002098 _PyRuntime.gc.trash_delete_later =
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002099 (PyObject*) _Py_AS_GC(op)->gc.gc_prev;
Neil Schemenauerf589c052002-03-29 03:05:54 +00002100
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002101 /* Call the deallocator directly. This used to try to
2102 * fool Py_DECREF into calling it indirectly, but
2103 * Py_DECREF was already called on this object, and in
2104 * assorted non-release builds calling Py_DECREF again ends
2105 * up distorting allocation statistics.
2106 */
2107 assert(op->ob_refcnt == 0);
Eric Snow2ebc5ce2017-09-07 23:51:28 -06002108 ++_PyRuntime.gc.trash_delete_nesting;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002109 (*dealloc)(op);
Eric Snow2ebc5ce2017-09-07 23:51:28 -06002110 --_PyRuntime.gc.trash_delete_nesting;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002111 }
Guido van Rossumd724b232000-03-13 16:01:29 +00002112}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002113
Antoine Pitrou2b0218a2012-09-06 00:59:49 +02002114/* The equivalent API, using per-thread state recursion info */
2115void
2116_PyTrash_thread_destroy_chain(void)
2117{
2118 PyThreadState *tstate = PyThreadState_GET();
Xiang Zhanga66f9c62017-05-13 13:36:14 +08002119 /* We need to increase trash_delete_nesting here, otherwise,
2120 _PyTrash_thread_destroy_chain will be called recursively
2121 and then possibly crash. An example that may crash without
2122 increase:
2123 N = 500000 # need to be large enough
2124 ob = object()
2125 tups = [(ob,) for i in range(N)]
2126 for i in range(49):
2127 tups = [(tup,) for tup in tups]
2128 del tups
2129 */
2130 assert(tstate->trash_delete_nesting == 0);
2131 ++tstate->trash_delete_nesting;
Antoine Pitrou2b0218a2012-09-06 00:59:49 +02002132 while (tstate->trash_delete_later) {
2133 PyObject *op = tstate->trash_delete_later;
2134 destructor dealloc = Py_TYPE(op)->tp_dealloc;
2135
2136 tstate->trash_delete_later =
2137 (PyObject*) _Py_AS_GC(op)->gc.gc_prev;
2138
2139 /* Call the deallocator directly. This used to try to
2140 * fool Py_DECREF into calling it indirectly, but
2141 * Py_DECREF was already called on this object, and in
2142 * assorted non-release builds calling Py_DECREF again ends
2143 * up distorting allocation statistics.
2144 */
2145 assert(op->ob_refcnt == 0);
Antoine Pitrou2b0218a2012-09-06 00:59:49 +02002146 (*dealloc)(op);
Xiang Zhanga66f9c62017-05-13 13:36:14 +08002147 assert(tstate->trash_delete_nesting == 1);
Antoine Pitrou2b0218a2012-09-06 00:59:49 +02002148 }
Xiang Zhanga66f9c62017-05-13 13:36:14 +08002149 --tstate->trash_delete_nesting;
Antoine Pitrou2b0218a2012-09-06 00:59:49 +02002150}
2151
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00002152#ifndef Py_TRACE_REFS
2153/* For Py_LIMITED_API, we need an out-of-line version of _Py_Dealloc.
2154 Define this here, so we can undefine the macro. */
2155#undef _Py_Dealloc
2156PyAPI_FUNC(void) _Py_Dealloc(PyObject *);
2157void
2158_Py_Dealloc(PyObject *op)
2159{
2160 _Py_INC_TPFREES(op) _Py_COUNT_ALLOCS_COMMA
2161 (*Py_TYPE(op)->tp_dealloc)(op);
2162}
2163#endif
2164
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002165#ifdef __cplusplus
2166}
2167#endif