blob: 220aa90bf59c5bc4eefb2c8d70dca4405af58b59 [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;
Eddie Elizondo745dc652018-02-21 20:55:18 -0800100 if (!interp->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
Eddie Elizondo745dc652018-02-21 20:55:18 -0800104 PyTypeObject *tp;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000105 for (tp = type_list; tp; tp = tp->tp_next)
106 fprintf(f, "%s alloc'd: %" PY_FORMAT_SIZE_T "d, "
107 "freed: %" PY_FORMAT_SIZE_T "d, "
108 "max in use: %" PY_FORMAT_SIZE_T "d\n",
109 tp->tp_name, tp->tp_allocs, tp->tp_frees,
110 tp->tp_maxalloc);
111 fprintf(f, "fast tuple allocs: %" PY_FORMAT_SIZE_T "d, "
112 "empty: %" PY_FORMAT_SIZE_T "d\n",
113 fast_tuple_allocs, tuple_zero_allocs);
114 fprintf(f, "fast int allocs: pos: %" PY_FORMAT_SIZE_T "d, "
115 "neg: %" PY_FORMAT_SIZE_T "d\n",
116 quick_int_allocs, quick_neg_int_allocs);
117 fprintf(f, "null strings: %" PY_FORMAT_SIZE_T "d, "
118 "1-strings: %" PY_FORMAT_SIZE_T "d\n",
119 null_strings, one_strings);
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +0000120}
121
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000122PyObject *
Fred Drake100814d2000-07-09 15:48:49 +0000123get_counts(void)
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000124{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000125 PyTypeObject *tp;
126 PyObject *result;
127 PyObject *v;
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000128
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000129 result = PyList_New(0);
130 if (result == NULL)
131 return NULL;
132 for (tp = type_list; tp; tp = tp->tp_next) {
133 v = Py_BuildValue("(snnn)", tp->tp_name, tp->tp_allocs,
134 tp->tp_frees, tp->tp_maxalloc);
135 if (v == NULL) {
136 Py_DECREF(result);
137 return NULL;
138 }
139 if (PyList_Append(result, v) < 0) {
140 Py_DECREF(v);
141 Py_DECREF(result);
142 return NULL;
143 }
144 Py_DECREF(v);
145 }
146 return result;
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000147}
148
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +0000149void
Fred Drake100814d2000-07-09 15:48:49 +0000150inc_count(PyTypeObject *tp)
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +0000151{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000152 if (tp->tp_next == NULL && tp->tp_prev == NULL) {
153 /* first time; insert in linked list */
154 if (tp->tp_next != NULL) /* sanity check */
155 Py_FatalError("XXX inc_count sanity check");
156 if (type_list)
157 type_list->tp_prev = tp;
158 tp->tp_next = type_list;
159 /* Note that as of Python 2.2, heap-allocated type objects
160 * can go away, but this code requires that they stay alive
161 * until program exit. That's why we're careful with
162 * refcounts here. type_list gets a new reference to tp,
163 * while ownership of the reference type_list used to hold
164 * (if any) was transferred to tp->tp_next in the line above.
165 * tp is thus effectively immortal after this.
166 */
167 Py_INCREF(tp);
168 type_list = tp;
Tim Peters3e40c7f2003-03-23 03:04:32 +0000169#ifdef Py_TRACE_REFS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000170 /* Also insert in the doubly-linked list of all objects,
171 * if not already there.
172 */
173 _Py_AddToAllObjects((PyObject *)tp, 0);
Tim Peters78be7992003-03-23 02:51:01 +0000174#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000175 }
176 tp->tp_allocs++;
177 if (tp->tp_allocs - tp->tp_frees > tp->tp_maxalloc)
178 tp->tp_maxalloc = tp->tp_allocs - tp->tp_frees;
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +0000179}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000180
181void dec_count(PyTypeObject *tp)
182{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000183 tp->tp_frees++;
184 if (unlist_types_without_objects &&
185 tp->tp_allocs == tp->tp_frees) {
186 /* unlink the type from type_list */
187 if (tp->tp_prev)
188 tp->tp_prev->tp_next = tp->tp_next;
189 else
190 type_list = tp->tp_next;
191 if (tp->tp_next)
192 tp->tp_next->tp_prev = tp->tp_prev;
193 tp->tp_next = tp->tp_prev = NULL;
194 Py_DECREF(tp);
195 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000196}
197
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +0000198#endif
199
Tim Peters7c321a82002-07-09 02:57:01 +0000200#ifdef Py_REF_DEBUG
201/* Log a fatal error; doesn't return. */
202void
203_Py_NegativeRefcount(const char *fname, int lineno, PyObject *op)
204{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000205 char buf[300];
Tim Peters7c321a82002-07-09 02:57:01 +0000206
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000207 PyOS_snprintf(buf, sizeof(buf),
208 "%s:%i object at %p has negative ref count "
209 "%" PY_FORMAT_SIZE_T "d",
210 fname, lineno, op, op->ob_refcnt);
211 Py_FatalError(buf);
Tim Peters7c321a82002-07-09 02:57:01 +0000212}
213
214#endif /* Py_REF_DEBUG */
215
Thomas Heller1328b522004-04-22 17:23:49 +0000216void
217Py_IncRef(PyObject *o)
218{
219 Py_XINCREF(o);
220}
221
222void
223Py_DecRef(PyObject *o)
224{
225 Py_XDECREF(o);
226}
227
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000228PyObject *
Fred Drake100814d2000-07-09 15:48:49 +0000229PyObject_Init(PyObject *op, PyTypeObject *tp)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000230{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000231 if (op == NULL)
232 return PyErr_NoMemory();
233 /* Any changes should be reflected in PyObject_INIT (objimpl.h) */
234 Py_TYPE(op) = tp;
235 _Py_NewReference(op);
236 return op;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000237}
238
Guido van Rossumb18618d2000-05-03 23:44:39 +0000239PyVarObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000240PyObject_InitVar(PyVarObject *op, PyTypeObject *tp, Py_ssize_t size)
Guido van Rossumb18618d2000-05-03 23:44:39 +0000241{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000242 if (op == NULL)
243 return (PyVarObject *) PyErr_NoMemory();
244 /* Any changes should be reflected in PyObject_INIT_VAR */
245 op->ob_size = size;
246 Py_TYPE(op) = tp;
247 _Py_NewReference((PyObject *)op);
248 return op;
Guido van Rossumb18618d2000-05-03 23:44:39 +0000249}
250
251PyObject *
Fred Drake100814d2000-07-09 15:48:49 +0000252_PyObject_New(PyTypeObject *tp)
Guido van Rossumb18618d2000-05-03 23:44:39 +0000253{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000254 PyObject *op;
255 op = (PyObject *) PyObject_MALLOC(_PyObject_SIZE(tp));
256 if (op == NULL)
257 return PyErr_NoMemory();
258 return PyObject_INIT(op, tp);
Guido van Rossumb18618d2000-05-03 23:44:39 +0000259}
260
Guido van Rossumd0c87ee1997-05-15 21:31:03 +0000261PyVarObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000262_PyObject_NewVar(PyTypeObject *tp, Py_ssize_t nitems)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000263{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000264 PyVarObject *op;
265 const size_t size = _PyObject_VAR_SIZE(tp, nitems);
266 op = (PyVarObject *) PyObject_MALLOC(size);
267 if (op == NULL)
268 return (PyVarObject *)PyErr_NoMemory();
269 return PyObject_INIT_VAR(op, tp, nitems);
Guido van Rossumb18618d2000-05-03 23:44:39 +0000270}
271
Antoine Pitrou796564c2013-07-30 19:59:21 +0200272void
273PyObject_CallFinalizer(PyObject *self)
274{
275 PyTypeObject *tp = Py_TYPE(self);
276
277 /* The former could happen on heaptypes created from the C API, e.g.
278 PyType_FromSpec(). */
279 if (!PyType_HasFeature(tp, Py_TPFLAGS_HAVE_FINALIZE) ||
280 tp->tp_finalize == NULL)
281 return;
282 /* tp_finalize should only be called once. */
283 if (PyType_IS_GC(tp) && _PyGC_FINALIZED(self))
284 return;
285
286 tp->tp_finalize(self);
287 if (PyType_IS_GC(tp))
288 _PyGC_SET_FINALIZED(self, 1);
289}
290
291int
292PyObject_CallFinalizerFromDealloc(PyObject *self)
293{
294 Py_ssize_t refcnt;
295
296 /* Temporarily resurrect the object. */
297 if (self->ob_refcnt != 0) {
298 Py_FatalError("PyObject_CallFinalizerFromDealloc called on "
299 "object with a non-zero refcount");
300 }
301 self->ob_refcnt = 1;
302
303 PyObject_CallFinalizer(self);
304
305 /* Undo the temporary resurrection; can't use DECREF here, it would
306 * cause a recursive call.
307 */
308 assert(self->ob_refcnt > 0);
309 if (--self->ob_refcnt == 0)
310 return 0; /* this is the normal path out */
311
312 /* tp_finalize resurrected it! Make it look like the original Py_DECREF
313 * never happened.
314 */
315 refcnt = self->ob_refcnt;
316 _Py_NewReference(self);
317 self->ob_refcnt = refcnt;
318
319 if (PyType_IS_GC(Py_TYPE(self))) {
320 assert(_PyGC_REFS(self) != _PyGC_REFS_UNTRACKED);
321 }
322 /* If Py_REF_DEBUG, _Py_NewReference bumped _Py_RefTotal, so
323 * we need to undo that. */
324 _Py_DEC_REFTOTAL;
325 /* If Py_TRACE_REFS, _Py_NewReference re-added self to the object
326 * chain, so no more to do there.
327 * If COUNT_ALLOCS, the original decref bumped tp_frees, and
328 * _Py_NewReference bumped tp_allocs: both of those need to be
329 * undone.
330 */
331#ifdef COUNT_ALLOCS
332 --Py_TYPE(self)->tp_frees;
333 --Py_TYPE(self)->tp_allocs;
334#endif
335 return -1;
336}
337
Antoine Pitrouc47bd4a2010-07-27 22:08:27 +0000338int
339PyObject_Print(PyObject *op, FILE *fp, int flags)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000340{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000341 int ret = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000342 if (PyErr_CheckSignals())
343 return -1;
Guido van Rossum9b00dfa1998-04-28 16:06:54 +0000344#ifdef USE_STACKCHECK
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000345 if (PyOS_CheckStack()) {
346 PyErr_SetString(PyExc_MemoryError, "stack overflow");
347 return -1;
348 }
Guido van Rossum9b00dfa1998-04-28 16:06:54 +0000349#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000350 clearerr(fp); /* Clear any previous error condition */
351 if (op == NULL) {
352 Py_BEGIN_ALLOW_THREADS
353 fprintf(fp, "<nil>");
354 Py_END_ALLOW_THREADS
355 }
356 else {
357 if (op->ob_refcnt <= 0)
358 /* XXX(twouters) cast refcount to long until %zd is
359 universally available */
360 Py_BEGIN_ALLOW_THREADS
361 fprintf(fp, "<refcnt %ld at %p>",
362 (long)op->ob_refcnt, op);
363 Py_END_ALLOW_THREADS
364 else {
365 PyObject *s;
366 if (flags & Py_PRINT_RAW)
367 s = PyObject_Str(op);
368 else
369 s = PyObject_Repr(op);
370 if (s == NULL)
371 ret = -1;
372 else if (PyBytes_Check(s)) {
373 fwrite(PyBytes_AS_STRING(s), 1,
374 PyBytes_GET_SIZE(s), fp);
375 }
376 else if (PyUnicode_Check(s)) {
377 PyObject *t;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200378 t = PyUnicode_AsEncodedString(s, "utf-8", "backslashreplace");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000379 if (t == NULL)
380 ret = 0;
381 else {
382 fwrite(PyBytes_AS_STRING(t), 1,
383 PyBytes_GET_SIZE(t), fp);
Victor Stinnerba6b4302010-05-17 09:33:42 +0000384 Py_DECREF(t);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000385 }
386 }
387 else {
388 PyErr_Format(PyExc_TypeError,
389 "str() or repr() returned '%.100s'",
390 s->ob_type->tp_name);
391 ret = -1;
392 }
393 Py_XDECREF(s);
394 }
395 }
396 if (ret == 0) {
397 if (ferror(fp)) {
Serhiy Storchaka55fe1ae2017-04-16 10:46:38 +0300398 PyErr_SetFromErrno(PyExc_OSError);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000399 clearerr(fp);
400 ret = -1;
401 }
402 }
403 return ret;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000404}
405
Guido van Rossum38938152006-08-21 23:36:26 +0000406/* For debugging convenience. Set a breakpoint here and call it from your DLL */
407void
Thomas Woutersb2137042007-02-01 18:02:27 +0000408_Py_BreakPoint(void)
Guido van Rossum38938152006-08-21 23:36:26 +0000409{
410}
411
Neal Norwitz1a997502003-01-13 20:13:12 +0000412
Barry Warsaw9bf16442001-01-23 16:24:35 +0000413/* For debugging convenience. See Misc/gdbinit for some useful gdb hooks */
Guido van Rossum38938152006-08-21 23:36:26 +0000414void
415_PyObject_Dump(PyObject* op)
Barry Warsaw9bf16442001-01-23 16:24:35 +0000416{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000417 if (op == NULL)
418 fprintf(stderr, "NULL\n");
419 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000420 PyGILState_STATE gil;
Victor Stinnere5132102013-08-26 13:49:06 +0200421 PyObject *error_type, *error_value, *error_traceback;
422
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000423 fprintf(stderr, "object : ");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000424 gil = PyGILState_Ensure();
Victor Stinnere5132102013-08-26 13:49:06 +0200425
426 PyErr_Fetch(&error_type, &error_value, &error_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000427 (void)PyObject_Print(op, stderr, 0);
Victor Stinnere5132102013-08-26 13:49:06 +0200428 PyErr_Restore(error_type, error_value, error_traceback);
429
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000430 PyGILState_Release(gil);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000431 /* XXX(twouters) cast refcount to long until %zd is
432 universally available */
433 fprintf(stderr, "\n"
434 "type : %s\n"
435 "refcount: %ld\n"
436 "address : %p\n",
437 Py_TYPE(op)==NULL ? "NULL" : Py_TYPE(op)->tp_name,
438 (long)op->ob_refcnt,
439 op);
440 }
Barry Warsaw9bf16442001-01-23 16:24:35 +0000441}
Barry Warsaw903138f2001-01-23 16:33:18 +0000442
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000443PyObject *
Fred Drake100814d2000-07-09 15:48:49 +0000444PyObject_Repr(PyObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000445{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000446 PyObject *res;
447 if (PyErr_CheckSignals())
448 return NULL;
Guido van Rossum9b00dfa1998-04-28 16:06:54 +0000449#ifdef USE_STACKCHECK
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000450 if (PyOS_CheckStack()) {
451 PyErr_SetString(PyExc_MemoryError, "stack overflow");
452 return NULL;
453 }
Guido van Rossum9b00dfa1998-04-28 16:06:54 +0000454#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000455 if (v == NULL)
456 return PyUnicode_FromString("<NULL>");
457 if (Py_TYPE(v)->tp_repr == NULL)
458 return PyUnicode_FromFormat("<%s object at %p>",
459 v->ob_type->tp_name, v);
Victor Stinner33824f62013-08-26 14:05:19 +0200460
461#ifdef Py_DEBUG
462 /* PyObject_Repr() must not be called with an exception set,
Victor Stinnera8cb5152017-01-18 14:12:51 +0100463 because it can clear it (directly or indirectly) and so the
Martin Panter9955a372015-10-07 10:26:23 +0000464 caller loses its exception */
Victor Stinner33824f62013-08-26 14:05:19 +0200465 assert(!PyErr_Occurred());
466#endif
467
Serhiy Storchaka1fb72d22017-12-03 22:12:11 +0200468 /* It is possible for a type to have a tp_repr representation that loops
469 infinitely. */
470 if (Py_EnterRecursiveCall(" while getting the repr of an object"))
471 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000472 res = (*v->ob_type->tp_repr)(v);
Serhiy Storchaka1fb72d22017-12-03 22:12:11 +0200473 Py_LeaveRecursiveCall();
Victor Stinner0a54cf12011-12-01 03:22:44 +0100474 if (res == NULL)
475 return NULL;
476 if (!PyUnicode_Check(res)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000477 PyErr_Format(PyExc_TypeError,
478 "__repr__ returned non-string (type %.200s)",
479 res->ob_type->tp_name);
480 Py_DECREF(res);
481 return NULL;
482 }
Victor Stinnerdb88ae52011-12-01 02:15:00 +0100483#ifndef Py_DEBUG
484 if (PyUnicode_READY(res) < 0)
485 return NULL;
486#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000487 return res;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000488}
489
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000490PyObject *
Guido van Rossum98297ee2007-11-06 21:34:58 +0000491PyObject_Str(PyObject *v)
Guido van Rossumc6004111993-11-05 10:22:19 +0000492{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000493 PyObject *res;
494 if (PyErr_CheckSignals())
495 return NULL;
Guido van Rossum98297ee2007-11-06 21:34:58 +0000496#ifdef USE_STACKCHECK
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000497 if (PyOS_CheckStack()) {
498 PyErr_SetString(PyExc_MemoryError, "stack overflow");
499 return NULL;
500 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000501#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000502 if (v == NULL)
503 return PyUnicode_FromString("<NULL>");
504 if (PyUnicode_CheckExact(v)) {
Victor Stinnerdb88ae52011-12-01 02:15:00 +0100505#ifndef Py_DEBUG
Victor Stinner4ead7c72011-11-20 19:48:36 +0100506 if (PyUnicode_READY(v) < 0)
507 return NULL;
Victor Stinnerdb88ae52011-12-01 02:15:00 +0100508#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000509 Py_INCREF(v);
510 return v;
511 }
512 if (Py_TYPE(v)->tp_str == NULL)
513 return PyObject_Repr(v);
Guido van Rossum4f288ab2001-05-01 16:53:37 +0000514
Victor Stinner33824f62013-08-26 14:05:19 +0200515#ifdef Py_DEBUG
516 /* PyObject_Str() must not be called with an exception set,
Victor Stinnera8cb5152017-01-18 14:12:51 +0100517 because it can clear it (directly or indirectly) and so the
Nick Coghland979e432014-02-09 10:43:21 +1000518 caller loses its exception */
Victor Stinner33824f62013-08-26 14:05:19 +0200519 assert(!PyErr_Occurred());
520#endif
521
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000522 /* It is possible for a type to have a tp_str representation that loops
523 infinitely. */
524 if (Py_EnterRecursiveCall(" while getting the str of an object"))
525 return NULL;
526 res = (*Py_TYPE(v)->tp_str)(v);
527 Py_LeaveRecursiveCall();
528 if (res == NULL)
529 return NULL;
530 if (!PyUnicode_Check(res)) {
531 PyErr_Format(PyExc_TypeError,
532 "__str__ returned non-string (type %.200s)",
533 Py_TYPE(res)->tp_name);
534 Py_DECREF(res);
535 return NULL;
536 }
Victor Stinnerdb88ae52011-12-01 02:15:00 +0100537#ifndef Py_DEBUG
Victor Stinner4ead7c72011-11-20 19:48:36 +0100538 if (PyUnicode_READY(res) < 0)
539 return NULL;
Victor Stinnerdb88ae52011-12-01 02:15:00 +0100540#endif
Victor Stinner4ead7c72011-11-20 19:48:36 +0100541 assert(_PyUnicode_CheckConsistency(res, 1));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000542 return res;
Neil Schemenauercf52c072005-08-12 17:34:58 +0000543}
544
Georg Brandl559e5d72008-06-11 18:37:52 +0000545PyObject *
546PyObject_ASCII(PyObject *v)
547{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000548 PyObject *repr, *ascii, *res;
Georg Brandl559e5d72008-06-11 18:37:52 +0000549
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000550 repr = PyObject_Repr(v);
551 if (repr == NULL)
552 return NULL;
Georg Brandl559e5d72008-06-11 18:37:52 +0000553
Victor Stinneraf037572013-04-14 18:44:10 +0200554 if (PyUnicode_IS_ASCII(repr))
555 return repr;
556
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000557 /* repr is guaranteed to be a PyUnicode object by PyObject_Repr */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200558 ascii = _PyUnicode_AsASCIIString(repr, "backslashreplace");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000559 Py_DECREF(repr);
560 if (ascii == NULL)
561 return NULL;
Georg Brandl559e5d72008-06-11 18:37:52 +0000562
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000563 res = PyUnicode_DecodeASCII(
564 PyBytes_AS_STRING(ascii),
565 PyBytes_GET_SIZE(ascii),
566 NULL);
567
568 Py_DECREF(ascii);
569 return res;
Georg Brandl559e5d72008-06-11 18:37:52 +0000570}
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000571
Benjamin Petersonc15a0732008-08-26 16:46:47 +0000572PyObject *
573PyObject_Bytes(PyObject *v)
574{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000575 PyObject *result, *func;
Benjamin Petersonc15a0732008-08-26 16:46:47 +0000576
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000577 if (v == NULL)
578 return PyBytes_FromString("<NULL>");
Benjamin Petersonc15a0732008-08-26 16:46:47 +0000579
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000580 if (PyBytes_CheckExact(v)) {
581 Py_INCREF(v);
582 return v;
583 }
Benjamin Petersonc15a0732008-08-26 16:46:47 +0000584
Benjamin Petersonce798522012-01-22 11:24:29 -0500585 func = _PyObject_LookupSpecial(v, &PyId___bytes__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000586 if (func != NULL) {
Victor Stinnerf17c3de2016-12-06 18:46:19 +0100587 result = _PyObject_CallNoArg(func);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000588 Py_DECREF(func);
589 if (result == NULL)
Benjamin Peterson41ece392010-09-11 16:39:57 +0000590 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000591 if (!PyBytes_Check(result)) {
Benjamin Peterson41ece392010-09-11 16:39:57 +0000592 PyErr_Format(PyExc_TypeError,
593 "__bytes__ returned non-bytes (type %.200s)",
594 Py_TYPE(result)->tp_name);
595 Py_DECREF(result);
596 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000597 }
598 return result;
599 }
600 else if (PyErr_Occurred())
601 return NULL;
602 return PyBytes_FromObject(v);
Benjamin Petersonc15a0732008-08-26 16:46:47 +0000603}
604
Mark Dickinsonc008a172009-02-01 13:59:22 +0000605/* For Python 3.0.1 and later, the old three-way comparison has been
606 completely removed in favour of rich comparisons. PyObject_Compare() and
607 PyObject_Cmp() are gone, and the builtin cmp function no longer exists.
Mark Dickinsone94c6792009-02-02 20:36:42 +0000608 The old tp_compare slot has been renamed to tp_reserved, and should no
Mark Dickinsonc008a172009-02-01 13:59:22 +0000609 longer be used. Use tp_richcompare instead.
Guido van Rossum98297ee2007-11-06 21:34:58 +0000610
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000611 See (*) below for practical amendments.
612
Mark Dickinsonc008a172009-02-01 13:59:22 +0000613 tp_richcompare gets called with a first argument of the appropriate type
614 and a second object of an arbitrary type. We never do any kind of
615 coercion.
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000616
Mark Dickinsonc008a172009-02-01 13:59:22 +0000617 The tp_richcompare slot should return an object, as follows:
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000618
619 NULL if an exception occurred
620 NotImplemented if the requested comparison is not implemented
621 any other false value if the requested comparison is false
622 any other true value if the requested comparison is true
623
624 The PyObject_RichCompare[Bool]() wrappers raise TypeError when they get
625 NotImplemented.
626
627 (*) Practical amendments:
628
629 - If rich comparison returns NotImplemented, == and != are decided by
630 comparing the object pointer (i.e. falling back to the base object
631 implementation).
632
Guido van Rossuma4073002002-05-31 20:03:54 +0000633*/
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000634
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000635/* Map rich comparison operators to their swapped version, e.g. LT <--> GT */
Brett Cannona5ca2e72004-09-25 01:37:24 +0000636int _Py_SwappedOp[] = {Py_GT, Py_GE, Py_EQ, Py_NE, Py_LT, Py_LE};
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +0000637
Serhiy Storchaka2d06e842015-12-25 19:53:18 +0200638static const char * const opstrings[] = {"<", "<=", "==", "!=", ">", ">="};
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000639
640/* Perform a rich comparison, raising TypeError when the requested comparison
641 operator is not supported. */
Guido van Rossume797ec12001-01-17 15:24:28 +0000642static PyObject *
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000643do_richcompare(PyObject *v, PyObject *w, int op)
Guido van Rossume797ec12001-01-17 15:24:28 +0000644{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000645 richcmpfunc f;
646 PyObject *res;
647 int checked_reverse_op = 0;
Guido van Rossume797ec12001-01-17 15:24:28 +0000648
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000649 if (v->ob_type != w->ob_type &&
650 PyType_IsSubtype(w->ob_type, v->ob_type) &&
651 (f = w->ob_type->tp_richcompare) != NULL) {
652 checked_reverse_op = 1;
653 res = (*f)(w, v, _Py_SwappedOp[op]);
654 if (res != Py_NotImplemented)
655 return res;
656 Py_DECREF(res);
657 }
658 if ((f = v->ob_type->tp_richcompare) != NULL) {
659 res = (*f)(v, w, op);
660 if (res != Py_NotImplemented)
661 return res;
662 Py_DECREF(res);
663 }
664 if (!checked_reverse_op && (f = w->ob_type->tp_richcompare) != NULL) {
665 res = (*f)(w, v, _Py_SwappedOp[op]);
666 if (res != Py_NotImplemented)
667 return res;
668 Py_DECREF(res);
669 }
670 /* If neither object implements it, provide a sensible default
671 for == and !=, but raise an exception for ordering. */
672 switch (op) {
673 case Py_EQ:
674 res = (v == w) ? Py_True : Py_False;
675 break;
676 case Py_NE:
677 res = (v != w) ? Py_True : Py_False;
678 break;
679 default:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000680 PyErr_Format(PyExc_TypeError,
Victor Stinner91108f02015-10-14 18:25:31 +0200681 "'%s' not supported between instances of '%.100s' and '%.100s'",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000682 opstrings[op],
Victor Stinner91108f02015-10-14 18:25:31 +0200683 v->ob_type->tp_name,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000684 w->ob_type->tp_name);
685 return NULL;
686 }
687 Py_INCREF(res);
688 return res;
Guido van Rossume797ec12001-01-17 15:24:28 +0000689}
690
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000691/* Perform a rich comparison with object result. This wraps do_richcompare()
692 with a check for NULL arguments and a recursion check. */
Guido van Rossume797ec12001-01-17 15:24:28 +0000693
Guido van Rossume797ec12001-01-17 15:24:28 +0000694PyObject *
695PyObject_RichCompare(PyObject *v, PyObject *w, int op)
696{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000697 PyObject *res;
Guido van Rossume797ec12001-01-17 15:24:28 +0000698
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000699 assert(Py_LT <= op && op <= Py_GE);
700 if (v == NULL || w == NULL) {
701 if (!PyErr_Occurred())
702 PyErr_BadInternalCall();
703 return NULL;
704 }
705 if (Py_EnterRecursiveCall(" in comparison"))
706 return NULL;
707 res = do_richcompare(v, w, op);
708 Py_LeaveRecursiveCall();
709 return res;
Guido van Rossume797ec12001-01-17 15:24:28 +0000710}
711
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000712/* Perform a rich comparison with integer result. This wraps
713 PyObject_RichCompare(), returning -1 for error, 0 for false, 1 for true. */
Guido van Rossume797ec12001-01-17 15:24:28 +0000714int
715PyObject_RichCompareBool(PyObject *v, PyObject *w, int op)
716{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000717 PyObject *res;
718 int ok;
Guido van Rossume797ec12001-01-17 15:24:28 +0000719
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000720 /* Quick result when objects are the same.
721 Guarantees that identity implies equality. */
722 if (v == w) {
723 if (op == Py_EQ)
724 return 1;
725 else if (op == Py_NE)
726 return 0;
727 }
Mark Dickinson4a1f5932008-11-12 23:23:36 +0000728
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000729 res = PyObject_RichCompare(v, w, op);
730 if (res == NULL)
731 return -1;
732 if (PyBool_Check(res))
733 ok = (res == Py_True);
734 else
735 ok = PyObject_IsTrue(res);
736 Py_DECREF(res);
737 return ok;
Guido van Rossume797ec12001-01-17 15:24:28 +0000738}
Fred Drake13634cf2000-06-29 19:17:04 +0000739
Antoine Pitrouce4a9da2011-11-21 20:46:33 +0100740Py_hash_t
Nick Coghland1abd252008-07-15 15:46:38 +0000741PyObject_HashNotImplemented(PyObject *v)
742{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000743 PyErr_Format(PyExc_TypeError, "unhashable type: '%.200s'",
744 Py_TYPE(v)->tp_name);
745 return -1;
Nick Coghland1abd252008-07-15 15:46:38 +0000746}
Fred Drake13634cf2000-06-29 19:17:04 +0000747
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000748Py_hash_t
Fred Drake100814d2000-07-09 15:48:49 +0000749PyObject_Hash(PyObject *v)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000750{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000751 PyTypeObject *tp = Py_TYPE(v);
752 if (tp->tp_hash != NULL)
753 return (*tp->tp_hash)(v);
754 /* To keep to the general practice that inheriting
755 * solely from object in C code should work without
756 * an explicit call to PyType_Ready, we implicitly call
757 * PyType_Ready here and then check the tp_hash slot again
758 */
759 if (tp->tp_dict == NULL) {
760 if (PyType_Ready(tp) < 0)
761 return -1;
762 if (tp->tp_hash != NULL)
763 return (*tp->tp_hash)(v);
764 }
765 /* Otherwise, the object can't be hashed */
766 return PyObject_HashNotImplemented(v);
Guido van Rossum9bfef441993-03-29 10:43:31 +0000767}
768
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000769PyObject *
Jeremy Hyltonaf68c872005-12-10 18:50:16 +0000770PyObject_GetAttrString(PyObject *v, const char *name)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000771{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000772 PyObject *w, *res;
Guido van Rossumd8eb1b31996-08-09 20:52:03 +0000773
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000774 if (Py_TYPE(v)->tp_getattr != NULL)
775 return (*Py_TYPE(v)->tp_getattr)(v, (char*)name);
INADA Naoki3e8d6cb2017-02-21 23:57:25 +0900776 w = PyUnicode_FromString(name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000777 if (w == NULL)
778 return NULL;
779 res = PyObject_GetAttr(v, w);
Victor Stinner59af08f2012-03-22 02:09:08 +0100780 Py_DECREF(w);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000781 return res;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000782}
783
784int
Jeremy Hyltonaf68c872005-12-10 18:50:16 +0000785PyObject_HasAttrString(PyObject *v, const char *name)
Guido van Rossumed18fdc1993-07-11 19:55:34 +0000786{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000787 PyObject *res = PyObject_GetAttrString(v, name);
788 if (res != NULL) {
789 Py_DECREF(res);
790 return 1;
791 }
792 PyErr_Clear();
793 return 0;
Guido van Rossumed18fdc1993-07-11 19:55:34 +0000794}
795
796int
Jeremy Hyltonaf68c872005-12-10 18:50:16 +0000797PyObject_SetAttrString(PyObject *v, const char *name, PyObject *w)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000798{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000799 PyObject *s;
800 int res;
Guido van Rossumd8eb1b31996-08-09 20:52:03 +0000801
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000802 if (Py_TYPE(v)->tp_setattr != NULL)
803 return (*Py_TYPE(v)->tp_setattr)(v, (char*)name, w);
804 s = PyUnicode_InternFromString(name);
805 if (s == NULL)
806 return -1;
807 res = PyObject_SetAttr(v, s, w);
808 Py_XDECREF(s);
809 return res;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000810}
811
Benjamin Petersonbfebb7b2011-12-15 15:34:02 -0500812int
813_PyObject_IsAbstract(PyObject *obj)
814{
815 int res;
816 PyObject* isabstract;
Benjamin Petersonbfebb7b2011-12-15 15:34:02 -0500817
818 if (obj == NULL)
819 return 0;
820
Serhiy Storchakaf320be72018-01-25 10:49:40 +0200821 res = _PyObject_LookupAttrId(obj, &PyId___isabstractmethod__, &isabstract);
822 if (res > 0) {
823 res = PyObject_IsTrue(isabstract);
824 Py_DECREF(isabstract);
Benjamin Petersonbfebb7b2011-12-15 15:34:02 -0500825 }
Benjamin Petersonbfebb7b2011-12-15 15:34:02 -0500826 return res;
827}
828
Guido van Rossum98ff96a1997-05-20 18:34:44 +0000829PyObject *
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200830_PyObject_GetAttrId(PyObject *v, _Py_Identifier *name)
831{
832 PyObject *result;
Martin v. Löwisd10759f2011-11-07 13:00:05 +0100833 PyObject *oname = _PyUnicode_FromId(name); /* borrowed */
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200834 if (!oname)
835 return NULL;
836 result = PyObject_GetAttr(v, oname);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200837 return result;
838}
839
840int
841_PyObject_HasAttrId(PyObject *v, _Py_Identifier *name)
842{
843 int result;
Martin v. Löwisd10759f2011-11-07 13:00:05 +0100844 PyObject *oname = _PyUnicode_FromId(name); /* borrowed */
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200845 if (!oname)
846 return -1;
847 result = PyObject_HasAttr(v, oname);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200848 return result;
849}
850
851int
852_PyObject_SetAttrId(PyObject *v, _Py_Identifier *name, PyObject *w)
853{
854 int result;
Martin v. Löwisd10759f2011-11-07 13:00:05 +0100855 PyObject *oname = _PyUnicode_FromId(name); /* borrowed */
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200856 if (!oname)
857 return -1;
858 result = PyObject_SetAttr(v, oname, w);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200859 return result;
860}
861
862PyObject *
Fred Drake100814d2000-07-09 15:48:49 +0000863PyObject_GetAttr(PyObject *v, PyObject *name)
Guido van Rossum98ff96a1997-05-20 18:34:44 +0000864{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000865 PyTypeObject *tp = Py_TYPE(v);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000866
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000867 if (!PyUnicode_Check(name)) {
868 PyErr_Format(PyExc_TypeError,
869 "attribute name must be string, not '%.200s'",
870 name->ob_type->tp_name);
871 return NULL;
872 }
873 if (tp->tp_getattro != NULL)
874 return (*tp->tp_getattro)(v, name);
875 if (tp->tp_getattr != NULL) {
Serhiy Storchaka2a404b62017-01-22 23:07:07 +0200876 const char *name_str = PyUnicode_AsUTF8(name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000877 if (name_str == NULL)
878 return NULL;
Serhiy Storchaka2a404b62017-01-22 23:07:07 +0200879 return (*tp->tp_getattr)(v, (char *)name_str);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000880 }
881 PyErr_Format(PyExc_AttributeError,
882 "'%.50s' object has no attribute '%U'",
883 tp->tp_name, name);
884 return NULL;
Guido van Rossum98ff96a1997-05-20 18:34:44 +0000885}
886
Serhiy Storchakaf320be72018-01-25 10:49:40 +0200887int
888_PyObject_LookupAttr(PyObject *v, PyObject *name, PyObject **result)
INADA Naoki378edee2018-01-16 20:52:41 +0900889{
890 PyTypeObject *tp = Py_TYPE(v);
INADA Naoki378edee2018-01-16 20:52:41 +0900891
892 if (!PyUnicode_Check(name)) {
893 PyErr_Format(PyExc_TypeError,
894 "attribute name must be string, not '%.200s'",
895 name->ob_type->tp_name);
Serhiy Storchakaf320be72018-01-25 10:49:40 +0200896 *result = NULL;
897 return -1;
INADA Naoki378edee2018-01-16 20:52:41 +0900898 }
899
900 if (tp->tp_getattro == PyObject_GenericGetAttr) {
Serhiy Storchakaf320be72018-01-25 10:49:40 +0200901 *result = _PyObject_GenericGetAttrWithDict(v, name, NULL, 1);
902 if (*result != NULL) {
903 return 1;
904 }
905 if (PyErr_Occurred()) {
906 return -1;
907 }
908 return 0;
INADA Naoki378edee2018-01-16 20:52:41 +0900909 }
910 if (tp->tp_getattro != NULL) {
Serhiy Storchakaf320be72018-01-25 10:49:40 +0200911 *result = (*tp->tp_getattro)(v, name);
INADA Naoki378edee2018-01-16 20:52:41 +0900912 }
913 else if (tp->tp_getattr != NULL) {
914 const char *name_str = PyUnicode_AsUTF8(name);
Serhiy Storchakaf320be72018-01-25 10:49:40 +0200915 if (name_str == NULL) {
916 *result = NULL;
917 return -1;
918 }
919 *result = (*tp->tp_getattr)(v, (char *)name_str);
INADA Naoki378edee2018-01-16 20:52:41 +0900920 }
INADA Naokie76daeb2018-01-26 16:22:51 +0900921 else {
922 *result = NULL;
923 return 0;
924 }
925
Serhiy Storchakaf320be72018-01-25 10:49:40 +0200926 if (*result != NULL) {
927 return 1;
INADA Naoki378edee2018-01-16 20:52:41 +0900928 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +0200929 if (!PyErr_ExceptionMatches(PyExc_AttributeError)) {
930 return -1;
931 }
932 PyErr_Clear();
933 return 0;
934}
935
936int
937_PyObject_LookupAttrId(PyObject *v, _Py_Identifier *name, PyObject **result)
938{
939 PyObject *oname = _PyUnicode_FromId(name); /* borrowed */
940 if (!oname) {
941 *result = NULL;
942 return -1;
943 }
944 return _PyObject_LookupAttr(v, oname, result);
INADA Naoki378edee2018-01-16 20:52:41 +0900945}
946
Guido van Rossum98ff96a1997-05-20 18:34:44 +0000947int
Fred Drake100814d2000-07-09 15:48:49 +0000948PyObject_HasAttr(PyObject *v, PyObject *name)
Guido van Rossum98ff96a1997-05-20 18:34:44 +0000949{
Serhiy Storchakaf320be72018-01-25 10:49:40 +0200950 PyObject *res;
951 if (_PyObject_LookupAttr(v, name, &res) < 0) {
952 PyErr_Clear();
953 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000954 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +0200955 if (res == NULL) {
956 return 0;
957 }
958 Py_DECREF(res);
959 return 1;
Guido van Rossum98ff96a1997-05-20 18:34:44 +0000960}
961
962int
Fred Drake100814d2000-07-09 15:48:49 +0000963PyObject_SetAttr(PyObject *v, PyObject *name, PyObject *value)
Guido van Rossum98ff96a1997-05-20 18:34:44 +0000964{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000965 PyTypeObject *tp = Py_TYPE(v);
966 int err;
Marc-André Lemburge44e5072000-09-18 16:20:57 +0000967
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000968 if (!PyUnicode_Check(name)) {
969 PyErr_Format(PyExc_TypeError,
970 "attribute name must be string, not '%.200s'",
971 name->ob_type->tp_name);
972 return -1;
973 }
974 Py_INCREF(name);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000975
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000976 PyUnicode_InternInPlace(&name);
977 if (tp->tp_setattro != NULL) {
978 err = (*tp->tp_setattro)(v, name, value);
979 Py_DECREF(name);
980 return err;
981 }
982 if (tp->tp_setattr != NULL) {
Serhiy Storchaka2a404b62017-01-22 23:07:07 +0200983 const char *name_str = PyUnicode_AsUTF8(name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000984 if (name_str == NULL)
985 return -1;
Serhiy Storchaka2a404b62017-01-22 23:07:07 +0200986 err = (*tp->tp_setattr)(v, (char *)name_str, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000987 Py_DECREF(name);
988 return err;
989 }
990 Py_DECREF(name);
991 assert(name->ob_refcnt >= 1);
992 if (tp->tp_getattr == NULL && tp->tp_getattro == NULL)
993 PyErr_Format(PyExc_TypeError,
994 "'%.100s' object has no attributes "
995 "(%s .%U)",
996 tp->tp_name,
997 value==NULL ? "del" : "assign to",
998 name);
999 else
1000 PyErr_Format(PyExc_TypeError,
1001 "'%.100s' object has only read-only attributes "
1002 "(%s .%U)",
1003 tp->tp_name,
1004 value==NULL ? "del" : "assign to",
1005 name);
1006 return -1;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001007}
1008
1009/* Helper to get a pointer to an object's __dict__ slot, if any */
1010
1011PyObject **
1012_PyObject_GetDictPtr(PyObject *obj)
1013{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001014 Py_ssize_t dictoffset;
1015 PyTypeObject *tp = Py_TYPE(obj);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001016
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001017 dictoffset = tp->tp_dictoffset;
1018 if (dictoffset == 0)
1019 return NULL;
1020 if (dictoffset < 0) {
1021 Py_ssize_t tsize;
1022 size_t size;
Guido van Rossum2eb0b872002-03-01 22:24:49 +00001023
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001024 tsize = ((PyVarObject *)obj)->ob_size;
1025 if (tsize < 0)
1026 tsize = -tsize;
1027 size = _PyObject_VAR_SIZE(tp, tsize);
Guido van Rossum2eb0b872002-03-01 22:24:49 +00001028
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001029 dictoffset += (long)size;
1030 assert(dictoffset > 0);
1031 assert(dictoffset % SIZEOF_VOID_P == 0);
1032 }
1033 return (PyObject **) ((char *)obj + dictoffset);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001034}
1035
Tim Peters6d6c1a32001-08-02 04:15:00 +00001036PyObject *
Raymond Hettinger1da1dbf2003-03-17 19:46:11 +00001037PyObject_SelfIter(PyObject *obj)
Raymond Hettinger01538262003-03-17 08:24:35 +00001038{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001039 Py_INCREF(obj);
1040 return obj;
Raymond Hettinger01538262003-03-17 08:24:35 +00001041}
1042
Antoine Pitroua7013882012-04-05 00:04:20 +02001043/* Convenience function to get a builtin from its name */
1044PyObject *
1045_PyObject_GetBuiltin(const char *name)
1046{
Victor Stinner53e9ec42013-11-07 00:43:05 +01001047 PyObject *mod_name, *mod, *attr;
1048
Victor Stinnerbd303c12013-11-07 23:07:29 +01001049 mod_name = _PyUnicode_FromId(&PyId_builtins); /* borrowed */
Victor Stinner53e9ec42013-11-07 00:43:05 +01001050 if (mod_name == NULL)
1051 return NULL;
1052 mod = PyImport_Import(mod_name);
Antoine Pitroua7013882012-04-05 00:04:20 +02001053 if (mod == NULL)
1054 return NULL;
1055 attr = PyObject_GetAttrString(mod, name);
1056 Py_DECREF(mod);
1057 return attr;
1058}
1059
Amaury Forgeot d'Arcf343e012009-01-12 23:58:21 +00001060/* Helper used when the __next__ method is removed from a type:
1061 tp_iternext is never NULL and can be safely called without checking
1062 on every iteration.
1063 */
1064
1065PyObject *
1066_PyObject_NextNotImplemented(PyObject *self)
1067{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001068 PyErr_Format(PyExc_TypeError,
1069 "'%.200s' object is not iterable",
1070 Py_TYPE(self)->tp_name);
1071 return NULL;
Amaury Forgeot d'Arcf343e012009-01-12 23:58:21 +00001072}
1073
Yury Selivanovf2392132016-12-13 19:03:51 -05001074
1075/* Specialized version of _PyObject_GenericGetAttrWithDict
1076 specifically for the LOAD_METHOD opcode.
1077
1078 Return 1 if a method is found, 0 if it's a regular attribute
1079 from __dict__ or something returned by using a descriptor
1080 protocol.
1081
1082 `method` will point to the resolved attribute or NULL. In the
1083 latter case, an error will be set.
1084*/
1085int
1086_PyObject_GetMethod(PyObject *obj, PyObject *name, PyObject **method)
1087{
1088 PyTypeObject *tp = Py_TYPE(obj);
1089 PyObject *descr;
1090 descrgetfunc f = NULL;
1091 PyObject **dictptr, *dict;
1092 PyObject *attr;
1093 int meth_found = 0;
1094
1095 assert(*method == NULL);
1096
1097 if (Py_TYPE(obj)->tp_getattro != PyObject_GenericGetAttr
1098 || !PyUnicode_Check(name)) {
1099 *method = PyObject_GetAttr(obj, name);
1100 return 0;
1101 }
1102
1103 if (tp->tp_dict == NULL && PyType_Ready(tp) < 0)
1104 return 0;
1105
1106 descr = _PyType_Lookup(tp, name);
1107 if (descr != NULL) {
1108 Py_INCREF(descr);
INADA Naoki5566bbb2017-02-03 07:43:03 +09001109 if (PyFunction_Check(descr) ||
1110 (Py_TYPE(descr) == &PyMethodDescr_Type)) {
Yury Selivanovf2392132016-12-13 19:03:51 -05001111 meth_found = 1;
1112 } else {
1113 f = descr->ob_type->tp_descr_get;
1114 if (f != NULL && PyDescr_IsData(descr)) {
1115 *method = f(descr, obj, (PyObject *)obj->ob_type);
1116 Py_DECREF(descr);
1117 return 0;
1118 }
1119 }
1120 }
1121
1122 dictptr = _PyObject_GetDictPtr(obj);
1123 if (dictptr != NULL && (dict = *dictptr) != NULL) {
1124 Py_INCREF(dict);
1125 attr = PyDict_GetItem(dict, name);
1126 if (attr != NULL) {
1127 Py_INCREF(attr);
1128 *method = attr;
1129 Py_DECREF(dict);
1130 Py_XDECREF(descr);
1131 return 0;
1132 }
1133 Py_DECREF(dict);
1134 }
1135
1136 if (meth_found) {
1137 *method = descr;
1138 return 1;
1139 }
1140
1141 if (f != NULL) {
1142 *method = f(descr, obj, (PyObject *)Py_TYPE(obj));
1143 Py_DECREF(descr);
1144 return 0;
1145 }
1146
1147 if (descr != NULL) {
1148 *method = descr;
1149 return 0;
1150 }
1151
1152 PyErr_Format(PyExc_AttributeError,
1153 "'%.50s' object has no attribute '%U'",
1154 tp->tp_name, name);
1155 return 0;
1156}
1157
1158/* Generic GetAttr functions - put these in your tp_[gs]etattro slot. */
Michael W. Hudson1593f502004-09-14 17:09:47 +00001159
Raymond Hettinger01538262003-03-17 08:24:35 +00001160PyObject *
INADA Naoki378edee2018-01-16 20:52:41 +09001161_PyObject_GenericGetAttrWithDict(PyObject *obj, PyObject *name,
1162 PyObject *dict, int suppress)
Tim Peters6d6c1a32001-08-02 04:15:00 +00001163{
Yury Selivanovf2392132016-12-13 19:03:51 -05001164 /* Make sure the logic of _PyObject_GetMethod is in sync with
1165 this method.
INADA Naoki378edee2018-01-16 20:52:41 +09001166
1167 When suppress=1, this function suppress AttributeError.
Yury Selivanovf2392132016-12-13 19:03:51 -05001168 */
1169
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001170 PyTypeObject *tp = Py_TYPE(obj);
1171 PyObject *descr = NULL;
1172 PyObject *res = NULL;
1173 descrgetfunc f;
1174 Py_ssize_t dictoffset;
1175 PyObject **dictptr;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001176
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001177 if (!PyUnicode_Check(name)){
1178 PyErr_Format(PyExc_TypeError,
1179 "attribute name must be string, not '%.200s'",
1180 name->ob_type->tp_name);
1181 return NULL;
1182 }
Serhiy Storchaka55c861f2016-04-17 20:31:51 +03001183 Py_INCREF(name);
Guido van Rossumebca9fc2001-12-04 15:54:53 +00001184
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001185 if (tp->tp_dict == NULL) {
1186 if (PyType_Ready(tp) < 0)
1187 goto done;
1188 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001189
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001190 descr = _PyType_Lookup(tp, name);
Michael W. Hudsonb2c7de42003-08-15 13:07:47 +00001191
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001192 f = NULL;
1193 if (descr != NULL) {
Serhiy Storchaka55c861f2016-04-17 20:31:51 +03001194 Py_INCREF(descr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001195 f = descr->ob_type->tp_descr_get;
1196 if (f != NULL && PyDescr_IsData(descr)) {
1197 res = f(descr, obj, (PyObject *)obj->ob_type);
INADA Naoki378edee2018-01-16 20:52:41 +09001198 if (res == NULL && suppress &&
1199 PyErr_ExceptionMatches(PyExc_AttributeError)) {
1200 PyErr_Clear();
1201 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001202 goto done;
1203 }
1204 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001205
Antoine Pitrou1a9a9d52010-08-28 18:17:03 +00001206 if (dict == NULL) {
1207 /* Inline _PyObject_GetDictPtr */
1208 dictoffset = tp->tp_dictoffset;
1209 if (dictoffset != 0) {
1210 if (dictoffset < 0) {
1211 Py_ssize_t tsize;
1212 size_t size;
Guido van Rossumc66ff442002-08-19 16:50:48 +00001213
Antoine Pitrou1a9a9d52010-08-28 18:17:03 +00001214 tsize = ((PyVarObject *)obj)->ob_size;
1215 if (tsize < 0)
1216 tsize = -tsize;
1217 size = _PyObject_VAR_SIZE(tp, tsize);
Serhiy Storchaka55c861f2016-04-17 20:31:51 +03001218 assert(size <= PY_SSIZE_T_MAX);
Guido van Rossumc66ff442002-08-19 16:50:48 +00001219
Serhiy Storchaka55c861f2016-04-17 20:31:51 +03001220 dictoffset += (Py_ssize_t)size;
Antoine Pitrou1a9a9d52010-08-28 18:17:03 +00001221 assert(dictoffset > 0);
1222 assert(dictoffset % SIZEOF_VOID_P == 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001223 }
Antoine Pitrou1a9a9d52010-08-28 18:17:03 +00001224 dictptr = (PyObject **) ((char *)obj + dictoffset);
1225 dict = *dictptr;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001226 }
1227 }
Antoine Pitrou1a9a9d52010-08-28 18:17:03 +00001228 if (dict != NULL) {
1229 Py_INCREF(dict);
1230 res = PyDict_GetItem(dict, name);
1231 if (res != NULL) {
1232 Py_INCREF(res);
Antoine Pitrou1a9a9d52010-08-28 18:17:03 +00001233 Py_DECREF(dict);
1234 goto done;
1235 }
1236 Py_DECREF(dict);
1237 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001238
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001239 if (f != NULL) {
1240 res = f(descr, obj, (PyObject *)Py_TYPE(obj));
INADA Naoki378edee2018-01-16 20:52:41 +09001241 if (res == NULL && suppress &&
1242 PyErr_ExceptionMatches(PyExc_AttributeError)) {
1243 PyErr_Clear();
1244 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001245 goto done;
1246 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001247
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001248 if (descr != NULL) {
1249 res = descr;
Victor Stinner2d01dc02012-03-09 00:44:13 +01001250 descr = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001251 goto done;
1252 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001253
INADA Naoki378edee2018-01-16 20:52:41 +09001254 if (!suppress) {
1255 PyErr_Format(PyExc_AttributeError,
1256 "'%.50s' object has no attribute '%U'",
1257 tp->tp_name, name);
1258 }
Guido van Rossumebca9fc2001-12-04 15:54:53 +00001259 done:
Victor Stinner2d01dc02012-03-09 00:44:13 +01001260 Py_XDECREF(descr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001261 Py_DECREF(name);
1262 return res;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001263}
1264
Antoine Pitrou1a9a9d52010-08-28 18:17:03 +00001265PyObject *
1266PyObject_GenericGetAttr(PyObject *obj, PyObject *name)
1267{
INADA Naoki378edee2018-01-16 20:52:41 +09001268 return _PyObject_GenericGetAttrWithDict(obj, name, NULL, 0);
Antoine Pitrou1a9a9d52010-08-28 18:17:03 +00001269}
1270
Tim Peters6d6c1a32001-08-02 04:15:00 +00001271int
Antoine Pitrou1a9a9d52010-08-28 18:17:03 +00001272_PyObject_GenericSetAttrWithDict(PyObject *obj, PyObject *name,
1273 PyObject *value, PyObject *dict)
Tim Peters6d6c1a32001-08-02 04:15:00 +00001274{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001275 PyTypeObject *tp = Py_TYPE(obj);
1276 PyObject *descr;
1277 descrsetfunc f;
1278 PyObject **dictptr;
1279 int res = -1;
Guido van Rossumebca9fc2001-12-04 15:54:53 +00001280
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001281 if (!PyUnicode_Check(name)){
1282 PyErr_Format(PyExc_TypeError,
1283 "attribute name must be string, not '%.200s'",
1284 name->ob_type->tp_name);
1285 return -1;
1286 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001287
Benjamin Peterson74529ad2012-03-09 07:25:32 -08001288 if (tp->tp_dict == NULL && PyType_Ready(tp) < 0)
1289 return -1;
1290
1291 Py_INCREF(name);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001292
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001293 descr = _PyType_Lookup(tp, name);
Victor Stinner2d01dc02012-03-09 00:44:13 +01001294
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001295 if (descr != NULL) {
Serhiy Storchaka55c861f2016-04-17 20:31:51 +03001296 Py_INCREF(descr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001297 f = descr->ob_type->tp_descr_set;
Serhiy Storchaka55c861f2016-04-17 20:31:51 +03001298 if (f != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001299 res = f(descr, obj, value);
1300 goto done;
1301 }
1302 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001303
Antoine Pitrou1a9a9d52010-08-28 18:17:03 +00001304 if (dict == NULL) {
1305 dictptr = _PyObject_GetDictPtr(obj);
Serhiy Storchaka55c861f2016-04-17 20:31:51 +03001306 if (dictptr == NULL) {
1307 if (descr == NULL) {
1308 PyErr_Format(PyExc_AttributeError,
1309 "'%.100s' object has no attribute '%U'",
1310 tp->tp_name, name);
1311 }
1312 else {
1313 PyErr_Format(PyExc_AttributeError,
1314 "'%.50s' object attribute '%U' is read-only",
1315 tp->tp_name, name);
1316 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001317 goto done;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001318 }
Serhiy Storchaka55c861f2016-04-17 20:31:51 +03001319 res = _PyObjectDict_SetItem(tp, dictptr, name, value);
Antoine Pitrou1a9a9d52010-08-28 18:17:03 +00001320 }
Serhiy Storchaka55c861f2016-04-17 20:31:51 +03001321 else {
Antoine Pitrou1a9a9d52010-08-28 18:17:03 +00001322 Py_INCREF(dict);
1323 if (value == NULL)
1324 res = PyDict_DelItem(dict, name);
1325 else
1326 res = PyDict_SetItem(dict, name, value);
Benjamin Peterson74529ad2012-03-09 07:25:32 -08001327 Py_DECREF(dict);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001328 }
Serhiy Storchaka55c861f2016-04-17 20:31:51 +03001329 if (res < 0 && PyErr_ExceptionMatches(PyExc_KeyError))
1330 PyErr_SetObject(PyExc_AttributeError, name);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001331
Guido van Rossumebca9fc2001-12-04 15:54:53 +00001332 done:
Victor Stinner2d01dc02012-03-09 00:44:13 +01001333 Py_XDECREF(descr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001334 Py_DECREF(name);
1335 return res;
Guido van Rossum98ff96a1997-05-20 18:34:44 +00001336}
1337
Antoine Pitrou1a9a9d52010-08-28 18:17:03 +00001338int
1339PyObject_GenericSetAttr(PyObject *obj, PyObject *name, PyObject *value)
1340{
1341 return _PyObject_GenericSetAttrWithDict(obj, name, value, NULL);
1342}
1343
Benjamin Peterson8eb12692012-02-19 19:59:10 -05001344int
1345PyObject_GenericSetDict(PyObject *obj, PyObject *value, void *context)
1346{
Serhiy Storchaka576f1322016-01-05 21:27:54 +02001347 PyObject **dictptr = _PyObject_GetDictPtr(obj);
Benjamin Peterson8eb12692012-02-19 19:59:10 -05001348 if (dictptr == NULL) {
1349 PyErr_SetString(PyExc_AttributeError,
1350 "This object has no __dict__");
1351 return -1;
1352 }
1353 if (value == NULL) {
1354 PyErr_SetString(PyExc_TypeError, "cannot delete __dict__");
1355 return -1;
1356 }
1357 if (!PyDict_Check(value)) {
1358 PyErr_Format(PyExc_TypeError,
1359 "__dict__ must be set to a dictionary, "
1360 "not a '%.200s'", Py_TYPE(value)->tp_name);
1361 return -1;
1362 }
Serhiy Storchaka576f1322016-01-05 21:27:54 +02001363 Py_INCREF(value);
Serhiy Storchakaec397562016-04-06 09:50:03 +03001364 Py_XSETREF(*dictptr, value);
Benjamin Peterson8eb12692012-02-19 19:59:10 -05001365 return 0;
1366}
1367
Antoine Pitrou1a9a9d52010-08-28 18:17:03 +00001368
Guido van Rossum6ac258d1993-05-12 08:24:20 +00001369/* Test a value used as condition, e.g., in a for or if statement.
1370 Return -1 if an error occurred */
1371
1372int
Fred Drake100814d2000-07-09 15:48:49 +00001373PyObject_IsTrue(PyObject *v)
Guido van Rossum6ac258d1993-05-12 08:24:20 +00001374{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001375 Py_ssize_t res;
1376 if (v == Py_True)
1377 return 1;
1378 if (v == Py_False)
1379 return 0;
1380 if (v == Py_None)
1381 return 0;
1382 else if (v->ob_type->tp_as_number != NULL &&
1383 v->ob_type->tp_as_number->nb_bool != NULL)
1384 res = (*v->ob_type->tp_as_number->nb_bool)(v);
1385 else if (v->ob_type->tp_as_mapping != NULL &&
1386 v->ob_type->tp_as_mapping->mp_length != NULL)
1387 res = (*v->ob_type->tp_as_mapping->mp_length)(v);
1388 else if (v->ob_type->tp_as_sequence != NULL &&
1389 v->ob_type->tp_as_sequence->sq_length != NULL)
1390 res = (*v->ob_type->tp_as_sequence->sq_length)(v);
1391 else
1392 return 1;
1393 /* if it is negative, it should be either -1 or -2 */
1394 return (res > 0) ? 1 : Py_SAFE_DOWNCAST(res, Py_ssize_t, int);
Guido van Rossum6ac258d1993-05-12 08:24:20 +00001395}
1396
Tim Peters803526b2002-07-07 05:13:56 +00001397/* equivalent of 'not v'
Guido van Rossumc3d3f961998-04-09 17:53:59 +00001398 Return -1 if an error occurred */
1399
1400int
Fred Drake100814d2000-07-09 15:48:49 +00001401PyObject_Not(PyObject *v)
Guido van Rossumc3d3f961998-04-09 17:53:59 +00001402{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001403 int res;
1404 res = PyObject_IsTrue(v);
1405 if (res < 0)
1406 return res;
1407 return res == 0;
Guido van Rossumc3d3f961998-04-09 17:53:59 +00001408}
1409
Guido van Rossum49b11fe1995-01-26 00:38:22 +00001410/* Test whether an object can be called */
1411
1412int
Fred Drake100814d2000-07-09 15:48:49 +00001413PyCallable_Check(PyObject *x)
Guido van Rossum49b11fe1995-01-26 00:38:22 +00001414{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001415 if (x == NULL)
1416 return 0;
1417 return x->ob_type->tp_call != NULL;
Guido van Rossum49b11fe1995-01-26 00:38:22 +00001418}
1419
Tim Peters7eea37e2001-09-04 22:08:56 +00001420
Georg Brandle32b4222007-03-10 22:13:27 +00001421/* Helper for PyObject_Dir without arguments: returns the local scope. */
1422static PyObject *
Guido van Rossumad7d8d12007-04-13 01:39:34 +00001423_dir_locals(void)
Tim Peters305b5852001-09-17 02:38:46 +00001424{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001425 PyObject *names;
Victor Stinner41bb43a2013-10-29 01:19:37 +01001426 PyObject *locals;
Tim Peters305b5852001-09-17 02:38:46 +00001427
Victor Stinner41bb43a2013-10-29 01:19:37 +01001428 locals = PyEval_GetLocals();
1429 if (locals == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001430 return NULL;
Tim Peters305b5852001-09-17 02:38:46 +00001431
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001432 names = PyMapping_Keys(locals);
1433 if (!names)
1434 return NULL;
1435 if (!PyList_Check(names)) {
1436 PyErr_Format(PyExc_TypeError,
1437 "dir(): expected keys() of locals to be a list, "
1438 "not '%.200s'", Py_TYPE(names)->tp_name);
1439 Py_DECREF(names);
1440 return NULL;
1441 }
Benjamin Peterson3bbb7222011-06-11 16:12:08 -05001442 if (PyList_Sort(names)) {
1443 Py_DECREF(names);
1444 return NULL;
1445 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001446 /* the locals don't need to be DECREF'd */
1447 return names;
Georg Brandle32b4222007-03-10 22:13:27 +00001448}
1449
Benjamin Peterson82b00c12011-05-24 11:09:06 -05001450/* Helper for PyObject_Dir: object introspection. */
Georg Brandle32b4222007-03-10 22:13:27 +00001451static PyObject *
1452_dir_object(PyObject *obj)
1453{
Benjamin Peterson3bbb7222011-06-11 16:12:08 -05001454 PyObject *result, *sorted;
Benjamin Petersonce798522012-01-22 11:24:29 -05001455 PyObject *dirfunc = _PyObject_LookupSpecial(obj, &PyId___dir__);
Georg Brandle32b4222007-03-10 22:13:27 +00001456
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001457 assert(obj);
1458 if (dirfunc == NULL) {
Benjamin Peterson82b00c12011-05-24 11:09:06 -05001459 if (!PyErr_Occurred())
1460 PyErr_SetString(PyExc_TypeError, "object does not provide __dir__");
1461 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001462 }
Benjamin Peterson82b00c12011-05-24 11:09:06 -05001463 /* use __dir__ */
Victor Stinnerf17c3de2016-12-06 18:46:19 +01001464 result = _PyObject_CallNoArg(dirfunc);
Benjamin Peterson82b00c12011-05-24 11:09:06 -05001465 Py_DECREF(dirfunc);
1466 if (result == NULL)
1467 return NULL;
Benjamin Peterson3bbb7222011-06-11 16:12:08 -05001468 /* return sorted(result) */
1469 sorted = PySequence_List(result);
1470 Py_DECREF(result);
1471 if (sorted == NULL)
1472 return NULL;
1473 if (PyList_Sort(sorted)) {
1474 Py_DECREF(sorted);
1475 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001476 }
Benjamin Peterson3bbb7222011-06-11 16:12:08 -05001477 return sorted;
Georg Brandle32b4222007-03-10 22:13:27 +00001478}
1479
1480/* Implementation of dir() -- if obj is NULL, returns the names in the current
1481 (local) scope. Otherwise, performs introspection of the object: returns a
1482 sorted list of attribute names (supposedly) accessible from the object
1483*/
1484PyObject *
1485PyObject_Dir(PyObject *obj)
1486{
Benjamin Peterson3bbb7222011-06-11 16:12:08 -05001487 return (obj == NULL) ? _dir_locals() : _dir_object(obj);
Tim Peters7eea37e2001-09-04 22:08:56 +00001488}
Guido van Rossum49b11fe1995-01-26 00:38:22 +00001489
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001490/*
Raymond Hettinger66d2be82011-07-28 09:55:13 -07001491None is a non-NULL undefined value.
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001492There is (and should be!) no way to create other objects of this type,
Guido van Rossum3f5da241990-12-20 15:06:42 +00001493so there is exactly one (which is indestructible, by the way).
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001494*/
1495
Guido van Rossum0c182a11992-03-27 17:26:13 +00001496/* ARGSUSED */
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001497static PyObject *
Fred Drake100814d2000-07-09 15:48:49 +00001498none_repr(PyObject *op)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001499{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001500 return PyUnicode_FromString("None");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001501}
1502
Barry Warsaw9bf16442001-01-23 16:24:35 +00001503/* ARGUSED */
1504static void
Tim Peters803526b2002-07-07 05:13:56 +00001505none_dealloc(PyObject* ignore)
Barry Warsaw9bf16442001-01-23 16:24:35 +00001506{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001507 /* This should never get called, but we also don't want to SEGV if
1508 * we accidentally decref None out of existence.
1509 */
1510 Py_FatalError("deallocating None");
Barry Warsaw9bf16442001-01-23 16:24:35 +00001511}
1512
Benjamin Petersonc4607ae2011-07-29 18:19:43 -05001513static PyObject *
1514none_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
1515{
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +02001516 if (PyTuple_GET_SIZE(args) || (kwargs && PyDict_GET_SIZE(kwargs))) {
Benjamin Petersonc4607ae2011-07-29 18:19:43 -05001517 PyErr_SetString(PyExc_TypeError, "NoneType takes no arguments");
1518 return NULL;
1519 }
1520 Py_RETURN_NONE;
1521}
1522
Raymond Hettinger66d2be82011-07-28 09:55:13 -07001523static int
1524none_bool(PyObject *v)
1525{
1526 return 0;
1527}
1528
1529static PyNumberMethods none_as_number = {
1530 0, /* nb_add */
1531 0, /* nb_subtract */
1532 0, /* nb_multiply */
1533 0, /* nb_remainder */
1534 0, /* nb_divmod */
1535 0, /* nb_power */
1536 0, /* nb_negative */
1537 0, /* nb_positive */
1538 0, /* nb_absolute */
1539 (inquiry)none_bool, /* nb_bool */
1540 0, /* nb_invert */
1541 0, /* nb_lshift */
1542 0, /* nb_rshift */
1543 0, /* nb_and */
1544 0, /* nb_xor */
1545 0, /* nb_or */
1546 0, /* nb_int */
1547 0, /* nb_reserved */
1548 0, /* nb_float */
1549 0, /* nb_inplace_add */
1550 0, /* nb_inplace_subtract */
1551 0, /* nb_inplace_multiply */
1552 0, /* nb_inplace_remainder */
1553 0, /* nb_inplace_power */
1554 0, /* nb_inplace_lshift */
1555 0, /* nb_inplace_rshift */
1556 0, /* nb_inplace_and */
1557 0, /* nb_inplace_xor */
1558 0, /* nb_inplace_or */
1559 0, /* nb_floor_divide */
1560 0, /* nb_true_divide */
1561 0, /* nb_inplace_floor_divide */
1562 0, /* nb_inplace_true_divide */
1563 0, /* nb_index */
1564};
Barry Warsaw9bf16442001-01-23 16:24:35 +00001565
Alexandre Vassalotti65846c62013-11-30 17:55:48 -08001566PyTypeObject _PyNone_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001567 PyVarObject_HEAD_INIT(&PyType_Type, 0)
1568 "NoneType",
1569 0,
1570 0,
1571 none_dealloc, /*tp_dealloc*/ /*never called*/
1572 0, /*tp_print*/
1573 0, /*tp_getattr*/
1574 0, /*tp_setattr*/
1575 0, /*tp_reserved*/
1576 none_repr, /*tp_repr*/
Raymond Hettinger66d2be82011-07-28 09:55:13 -07001577 &none_as_number, /*tp_as_number*/
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001578 0, /*tp_as_sequence*/
1579 0, /*tp_as_mapping*/
1580 0, /*tp_hash */
Benjamin Petersonc4607ae2011-07-29 18:19:43 -05001581 0, /*tp_call */
1582 0, /*tp_str */
1583 0, /*tp_getattro */
1584 0, /*tp_setattro */
1585 0, /*tp_as_buffer */
1586 Py_TPFLAGS_DEFAULT, /*tp_flags */
1587 0, /*tp_doc */
1588 0, /*tp_traverse */
1589 0, /*tp_clear */
1590 0, /*tp_richcompare */
1591 0, /*tp_weaklistoffset */
1592 0, /*tp_iter */
1593 0, /*tp_iternext */
1594 0, /*tp_methods */
1595 0, /*tp_members */
1596 0, /*tp_getset */
1597 0, /*tp_base */
1598 0, /*tp_dict */
1599 0, /*tp_descr_get */
1600 0, /*tp_descr_set */
1601 0, /*tp_dictoffset */
1602 0, /*tp_init */
1603 0, /*tp_alloc */
1604 none_new, /*tp_new */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001605};
1606
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001607PyObject _Py_NoneStruct = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001608 _PyObject_EXTRA_INIT
Alexandre Vassalotti65846c62013-11-30 17:55:48 -08001609 1, &_PyNone_Type
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001610};
1611
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +00001612/* NotImplemented is an object that can be used to signal that an
1613 operation is not implemented for the given type combination. */
1614
1615static PyObject *
1616NotImplemented_repr(PyObject *op)
1617{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001618 return PyUnicode_FromString("NotImplemented");
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +00001619}
1620
Benjamin Peterson18d7d7a2011-07-29 18:27:44 -05001621static PyObject *
Alexandre Vassalottic49477b2013-11-24 02:53:45 -08001622NotImplemented_reduce(PyObject *op)
1623{
1624 return PyUnicode_FromString("NotImplemented");
1625}
1626
1627static PyMethodDef notimplemented_methods[] = {
1628 {"__reduce__", (PyCFunction)NotImplemented_reduce, METH_NOARGS, NULL},
1629 {NULL, NULL}
1630};
1631
1632static PyObject *
Benjamin Peterson18d7d7a2011-07-29 18:27:44 -05001633notimplemented_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
1634{
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +02001635 if (PyTuple_GET_SIZE(args) || (kwargs && PyDict_GET_SIZE(kwargs))) {
Benjamin Peterson18d7d7a2011-07-29 18:27:44 -05001636 PyErr_SetString(PyExc_TypeError, "NotImplementedType takes no arguments");
1637 return NULL;
1638 }
Brian Curtindfc80e32011-08-10 20:28:54 -05001639 Py_RETURN_NOTIMPLEMENTED;
Benjamin Peterson18d7d7a2011-07-29 18:27:44 -05001640}
1641
Armin Ronacher226b1db2012-10-06 14:28:58 +02001642static void
1643notimplemented_dealloc(PyObject* ignore)
1644{
1645 /* This should never get called, but we also don't want to SEGV if
1646 * we accidentally decref NotImplemented out of existence.
1647 */
1648 Py_FatalError("deallocating NotImplemented");
1649}
1650
Alexandre Vassalotti65846c62013-11-30 17:55:48 -08001651PyTypeObject _PyNotImplemented_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001652 PyVarObject_HEAD_INIT(&PyType_Type, 0)
1653 "NotImplementedType",
1654 0,
1655 0,
Armin Ronacher226b1db2012-10-06 14:28:58 +02001656 notimplemented_dealloc, /*tp_dealloc*/ /*never called*/
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001657 0, /*tp_print*/
1658 0, /*tp_getattr*/
1659 0, /*tp_setattr*/
1660 0, /*tp_reserved*/
1661 NotImplemented_repr, /*tp_repr*/
1662 0, /*tp_as_number*/
1663 0, /*tp_as_sequence*/
1664 0, /*tp_as_mapping*/
1665 0, /*tp_hash */
Benjamin Peterson18d7d7a2011-07-29 18:27:44 -05001666 0, /*tp_call */
1667 0, /*tp_str */
1668 0, /*tp_getattro */
1669 0, /*tp_setattro */
1670 0, /*tp_as_buffer */
1671 Py_TPFLAGS_DEFAULT, /*tp_flags */
1672 0, /*tp_doc */
1673 0, /*tp_traverse */
1674 0, /*tp_clear */
1675 0, /*tp_richcompare */
1676 0, /*tp_weaklistoffset */
1677 0, /*tp_iter */
1678 0, /*tp_iternext */
Alexandre Vassalottic49477b2013-11-24 02:53:45 -08001679 notimplemented_methods, /*tp_methods */
Benjamin Peterson18d7d7a2011-07-29 18:27:44 -05001680 0, /*tp_members */
1681 0, /*tp_getset */
1682 0, /*tp_base */
1683 0, /*tp_dict */
1684 0, /*tp_descr_get */
1685 0, /*tp_descr_set */
1686 0, /*tp_dictoffset */
1687 0, /*tp_init */
1688 0, /*tp_alloc */
1689 notimplemented_new, /*tp_new */
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +00001690};
1691
1692PyObject _Py_NotImplementedStruct = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001693 _PyObject_EXTRA_INIT
Alexandre Vassalotti65846c62013-11-30 17:55:48 -08001694 1, &_PyNotImplemented_Type
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +00001695};
1696
Guido van Rossumba21a492001-08-16 08:17:26 +00001697void
1698_Py_ReadyTypes(void)
1699{
Victor Stinner5a1bb4e2014-06-02 14:10:59 +02001700 if (PyType_Ready(&PyBaseObject_Type) < 0)
1701 Py_FatalError("Can't initialize object type");
1702
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001703 if (PyType_Ready(&PyType_Type) < 0)
1704 Py_FatalError("Can't initialize type type");
Guido van Rossumba21a492001-08-16 08:17:26 +00001705
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001706 if (PyType_Ready(&_PyWeakref_RefType) < 0)
1707 Py_FatalError("Can't initialize weakref type");
Fred Drake0a4dd392004-07-02 18:57:45 +00001708
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001709 if (PyType_Ready(&_PyWeakref_CallableProxyType) < 0)
1710 Py_FatalError("Can't initialize callable weakref proxy type");
Benjamin Petersonfd838e62009-04-20 02:09:13 +00001711
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001712 if (PyType_Ready(&_PyWeakref_ProxyType) < 0)
1713 Py_FatalError("Can't initialize weakref proxy type");
Benjamin Petersonfd838e62009-04-20 02:09:13 +00001714
Victor Stinner5a1bb4e2014-06-02 14:10:59 +02001715 if (PyType_Ready(&PyLong_Type) < 0)
1716 Py_FatalError("Can't initialize int type");
1717
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001718 if (PyType_Ready(&PyBool_Type) < 0)
1719 Py_FatalError("Can't initialize bool type");
Guido van Rossum77f6a652002-04-03 22:41:51 +00001720
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001721 if (PyType_Ready(&PyByteArray_Type) < 0)
1722 Py_FatalError("Can't initialize bytearray type");
Guido van Rossum4dfe8a12006-04-22 23:28:04 +00001723
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001724 if (PyType_Ready(&PyBytes_Type) < 0)
1725 Py_FatalError("Can't initialize 'str'");
Guido van Rossumcacfc072002-05-24 19:01:59 +00001726
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001727 if (PyType_Ready(&PyList_Type) < 0)
1728 Py_FatalError("Can't initialize list type");
Guido van Rossumba21a492001-08-16 08:17:26 +00001729
Alexandre Vassalotti65846c62013-11-30 17:55:48 -08001730 if (PyType_Ready(&_PyNone_Type) < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001731 Py_FatalError("Can't initialize None type");
Guido van Rossumba21a492001-08-16 08:17:26 +00001732
Alexandre Vassalotti65846c62013-11-30 17:55:48 -08001733 if (PyType_Ready(&_PyNotImplemented_Type) < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001734 Py_FatalError("Can't initialize NotImplemented type");
Benjamin Petersonae937c02009-04-18 20:54:08 +00001735
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001736 if (PyType_Ready(&PyTraceBack_Type) < 0)
1737 Py_FatalError("Can't initialize traceback type");
Benjamin Petersonae937c02009-04-18 20:54:08 +00001738
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001739 if (PyType_Ready(&PySuper_Type) < 0)
1740 Py_FatalError("Can't initialize super type");
Benjamin Petersonae937c02009-04-18 20:54:08 +00001741
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001742 if (PyType_Ready(&PyRange_Type) < 0)
1743 Py_FatalError("Can't initialize range type");
Benjamin Petersonae937c02009-04-18 20:54:08 +00001744
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001745 if (PyType_Ready(&PyDict_Type) < 0)
1746 Py_FatalError("Can't initialize dict type");
Benjamin Petersonae937c02009-04-18 20:54:08 +00001747
Benjamin Petersondb87c992016-11-06 13:01:07 -08001748 if (PyType_Ready(&PyDictKeys_Type) < 0)
1749 Py_FatalError("Can't initialize dict keys type");
1750
1751 if (PyType_Ready(&PyDictValues_Type) < 0)
1752 Py_FatalError("Can't initialize dict values type");
1753
1754 if (PyType_Ready(&PyDictItems_Type) < 0)
1755 Py_FatalError("Can't initialize dict items type");
1756
Eric Snow96c6af92015-05-29 22:21:39 -06001757 if (PyType_Ready(&PyODict_Type) < 0)
1758 Py_FatalError("Can't initialize OrderedDict type");
1759
1760 if (PyType_Ready(&PyODictKeys_Type) < 0)
1761 Py_FatalError("Can't initialize odict_keys type");
1762
1763 if (PyType_Ready(&PyODictItems_Type) < 0)
1764 Py_FatalError("Can't initialize odict_items type");
1765
1766 if (PyType_Ready(&PyODictValues_Type) < 0)
1767 Py_FatalError("Can't initialize odict_values type");
1768
1769 if (PyType_Ready(&PyODictIter_Type) < 0)
1770 Py_FatalError("Can't initialize odict_keyiterator type");
1771
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001772 if (PyType_Ready(&PySet_Type) < 0)
1773 Py_FatalError("Can't initialize set type");
Benjamin Petersonae937c02009-04-18 20:54:08 +00001774
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001775 if (PyType_Ready(&PyUnicode_Type) < 0)
1776 Py_FatalError("Can't initialize str type");
Benjamin Petersonae937c02009-04-18 20:54:08 +00001777
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001778 if (PyType_Ready(&PySlice_Type) < 0)
1779 Py_FatalError("Can't initialize slice type");
Benjamin Petersonae937c02009-04-18 20:54:08 +00001780
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001781 if (PyType_Ready(&PyStaticMethod_Type) < 0)
1782 Py_FatalError("Can't initialize static method type");
Benjamin Petersonae937c02009-04-18 20:54:08 +00001783
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001784 if (PyType_Ready(&PyComplex_Type) < 0)
1785 Py_FatalError("Can't initialize complex type");
Skip Montanaroba1e0f42009-10-18 14:25:35 +00001786
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001787 if (PyType_Ready(&PyFloat_Type) < 0)
1788 Py_FatalError("Can't initialize float type");
Benjamin Petersonae937c02009-04-18 20:54:08 +00001789
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001790 if (PyType_Ready(&PyFrozenSet_Type) < 0)
1791 Py_FatalError("Can't initialize frozenset type");
Benjamin Petersonae937c02009-04-18 20:54:08 +00001792
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001793 if (PyType_Ready(&PyProperty_Type) < 0)
1794 Py_FatalError("Can't initialize property type");
Benjamin Petersonae937c02009-04-18 20:54:08 +00001795
Stefan Krah9a2d99e2012-02-25 12:24:21 +01001796 if (PyType_Ready(&_PyManagedBuffer_Type) < 0)
1797 Py_FatalError("Can't initialize managed buffer type");
1798
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001799 if (PyType_Ready(&PyMemoryView_Type) < 0)
1800 Py_FatalError("Can't initialize memoryview type");
Benjamin Petersonae937c02009-04-18 20:54:08 +00001801
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001802 if (PyType_Ready(&PyTuple_Type) < 0)
1803 Py_FatalError("Can't initialize tuple type");
Benjamin Petersonae937c02009-04-18 20:54:08 +00001804
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001805 if (PyType_Ready(&PyEnum_Type) < 0)
1806 Py_FatalError("Can't initialize enumerate type");
Benjamin Petersonae937c02009-04-18 20:54:08 +00001807
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001808 if (PyType_Ready(&PyReversed_Type) < 0)
1809 Py_FatalError("Can't initialize reversed type");
Guido van Rossum47b9ff62006-08-24 00:41:19 +00001810
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001811 if (PyType_Ready(&PyStdPrinter_Type) < 0)
1812 Py_FatalError("Can't initialize StdPrinter");
Benjamin Petersonfd838e62009-04-20 02:09:13 +00001813
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001814 if (PyType_Ready(&PyCode_Type) < 0)
1815 Py_FatalError("Can't initialize code type");
Benjamin Petersonfd838e62009-04-20 02:09:13 +00001816
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001817 if (PyType_Ready(&PyFrame_Type) < 0)
1818 Py_FatalError("Can't initialize frame type");
Benjamin Petersonfd838e62009-04-20 02:09:13 +00001819
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001820 if (PyType_Ready(&PyCFunction_Type) < 0)
1821 Py_FatalError("Can't initialize builtin function type");
Benjamin Petersonfd838e62009-04-20 02:09:13 +00001822
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001823 if (PyType_Ready(&PyMethod_Type) < 0)
1824 Py_FatalError("Can't initialize method type");
Benjamin Petersonfd838e62009-04-20 02:09:13 +00001825
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001826 if (PyType_Ready(&PyFunction_Type) < 0)
1827 Py_FatalError("Can't initialize function type");
Benjamin Petersonfd838e62009-04-20 02:09:13 +00001828
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001829 if (PyType_Ready(&PyDictProxy_Type) < 0)
1830 Py_FatalError("Can't initialize dict proxy type");
Benjamin Petersonfd838e62009-04-20 02:09:13 +00001831
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001832 if (PyType_Ready(&PyGen_Type) < 0)
1833 Py_FatalError("Can't initialize generator type");
Benjamin Petersonfd838e62009-04-20 02:09:13 +00001834
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001835 if (PyType_Ready(&PyGetSetDescr_Type) < 0)
1836 Py_FatalError("Can't initialize get-set descriptor type");
Benjamin Petersonfd838e62009-04-20 02:09:13 +00001837
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001838 if (PyType_Ready(&PyWrapperDescr_Type) < 0)
1839 Py_FatalError("Can't initialize wrapper type");
Benjamin Petersonfd838e62009-04-20 02:09:13 +00001840
Benjamin Petersoneff61f62011-09-01 16:32:31 -04001841 if (PyType_Ready(&_PyMethodWrapper_Type) < 0)
1842 Py_FatalError("Can't initialize method wrapper type");
1843
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001844 if (PyType_Ready(&PyEllipsis_Type) < 0)
1845 Py_FatalError("Can't initialize ellipsis type");
Benjamin Petersonfd838e62009-04-20 02:09:13 +00001846
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001847 if (PyType_Ready(&PyMemberDescr_Type) < 0)
1848 Py_FatalError("Can't initialize member descriptor type");
Benjamin Peterson8bc5b682009-05-09 18:10:51 +00001849
Barry Warsaw409da152012-06-03 16:18:47 -04001850 if (PyType_Ready(&_PyNamespace_Type) < 0)
1851 Py_FatalError("Can't initialize namespace type");
Benjamin Petersone8ea97f2012-10-30 23:27:52 -04001852
Benjamin Petersonc4311282012-10-30 23:21:10 -04001853 if (PyType_Ready(&PyCapsule_Type) < 0)
1854 Py_FatalError("Can't initialize capsule type");
1855
1856 if (PyType_Ready(&PyLongRangeIter_Type) < 0)
1857 Py_FatalError("Can't initialize long range iterator type");
1858
1859 if (PyType_Ready(&PyCell_Type) < 0)
1860 Py_FatalError("Can't initialize cell type");
1861
1862 if (PyType_Ready(&PyInstanceMethod_Type) < 0)
1863 Py_FatalError("Can't initialize instance method type");
1864
1865 if (PyType_Ready(&PyClassMethodDescr_Type) < 0)
1866 Py_FatalError("Can't initialize class method descr type");
1867
1868 if (PyType_Ready(&PyMethodDescr_Type) < 0)
1869 Py_FatalError("Can't initialize method descr type");
1870
1871 if (PyType_Ready(&PyCallIter_Type) < 0)
1872 Py_FatalError("Can't initialize call iter type");
1873
1874 if (PyType_Ready(&PySeqIter_Type) < 0)
1875 Py_FatalError("Can't initialize sequence iterator type");
Yury Selivanov5376ba92015-06-22 12:19:30 -04001876
1877 if (PyType_Ready(&PyCoro_Type) < 0)
1878 Py_FatalError("Can't initialize coroutine type");
1879
1880 if (PyType_Ready(&_PyCoroWrapper_Type) < 0)
1881 Py_FatalError("Can't initialize coroutine wrapper type");
Guido van Rossumba21a492001-08-16 08:17:26 +00001882}
1883
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001884
Guido van Rossum84a90321996-05-22 16:34:47 +00001885#ifdef Py_TRACE_REFS
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001886
Guido van Rossumaacdc9d1996-08-12 21:32:12 +00001887void
Fred Drake100814d2000-07-09 15:48:49 +00001888_Py_NewReference(PyObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001889{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001890 _Py_INC_REFTOTAL;
1891 op->ob_refcnt = 1;
1892 _Py_AddToAllObjects(op, 1);
1893 _Py_INC_TPALLOCS(op);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001894}
1895
Guido van Rossumaacdc9d1996-08-12 21:32:12 +00001896void
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02001897_Py_ForgetReference(PyObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001898{
Guido van Rossumbffd6832000-01-20 22:32:56 +00001899#ifdef SLOW_UNREF_CHECK
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02001900 PyObject *p;
Guido van Rossumbffd6832000-01-20 22:32:56 +00001901#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001902 if (op->ob_refcnt < 0)
1903 Py_FatalError("UNREF negative refcnt");
1904 if (op == &refchain ||
1905 op->_ob_prev->_ob_next != op || op->_ob_next->_ob_prev != op) {
1906 fprintf(stderr, "* ob\n");
1907 _PyObject_Dump(op);
1908 fprintf(stderr, "* op->_ob_prev->_ob_next\n");
1909 _PyObject_Dump(op->_ob_prev->_ob_next);
1910 fprintf(stderr, "* op->_ob_next->_ob_prev\n");
1911 _PyObject_Dump(op->_ob_next->_ob_prev);
1912 Py_FatalError("UNREF invalid object");
1913 }
Guido van Rossum2e8f6141992-09-03 20:32:55 +00001914#ifdef SLOW_UNREF_CHECK
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001915 for (p = refchain._ob_next; p != &refchain; p = p->_ob_next) {
1916 if (p == op)
1917 break;
1918 }
1919 if (p == &refchain) /* Not found */
1920 Py_FatalError("UNREF unknown object");
Guido van Rossum2e8f6141992-09-03 20:32:55 +00001921#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001922 op->_ob_next->_ob_prev = op->_ob_prev;
1923 op->_ob_prev->_ob_next = op->_ob_next;
1924 op->_ob_next = op->_ob_prev = NULL;
1925 _Py_INC_TPFREES(op);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001926}
1927
Guido van Rossumaacdc9d1996-08-12 21:32:12 +00001928void
Fred Drake100814d2000-07-09 15:48:49 +00001929_Py_Dealloc(PyObject *op)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001930{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001931 destructor dealloc = Py_TYPE(op)->tp_dealloc;
1932 _Py_ForgetReference(op);
1933 (*dealloc)(op);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001934}
1935
Tim Peters269b2a62003-04-17 19:52:29 +00001936/* Print all live objects. Because PyObject_Print is called, the
1937 * interpreter must be in a healthy state.
1938 */
Guido van Rossumaacdc9d1996-08-12 21:32:12 +00001939void
Fred Drake100814d2000-07-09 15:48:49 +00001940_Py_PrintReferences(FILE *fp)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001941{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001942 PyObject *op;
1943 fprintf(fp, "Remaining objects:\n");
1944 for (op = refchain._ob_next; op != &refchain; op = op->_ob_next) {
1945 fprintf(fp, "%p [%" PY_FORMAT_SIZE_T "d] ", op, op->ob_refcnt);
1946 if (PyObject_Print(op, fp, 0) != 0)
1947 PyErr_Clear();
1948 putc('\n', fp);
1949 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001950}
1951
Tim Peters269b2a62003-04-17 19:52:29 +00001952/* Print the addresses of all live objects. Unlike _Py_PrintReferences, this
1953 * doesn't make any calls to the Python C API, so is always safe to call.
1954 */
1955void
1956_Py_PrintReferenceAddresses(FILE *fp)
1957{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001958 PyObject *op;
1959 fprintf(fp, "Remaining object addresses:\n");
1960 for (op = refchain._ob_next; op != &refchain; op = op->_ob_next)
1961 fprintf(fp, "%p [%" PY_FORMAT_SIZE_T "d] %s\n", op,
1962 op->ob_refcnt, Py_TYPE(op)->tp_name);
Tim Peters269b2a62003-04-17 19:52:29 +00001963}
1964
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +00001965PyObject *
Fred Drake100814d2000-07-09 15:48:49 +00001966_Py_GetObjects(PyObject *self, PyObject *args)
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +00001967{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001968 int i, n;
1969 PyObject *t = NULL;
1970 PyObject *res, *op;
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +00001971
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001972 if (!PyArg_ParseTuple(args, "i|O", &n, &t))
1973 return NULL;
1974 op = refchain._ob_next;
1975 res = PyList_New(0);
1976 if (res == NULL)
1977 return NULL;
1978 for (i = 0; (n == 0 || i < n) && op != &refchain; i++) {
1979 while (op == self || op == args || op == res || op == t ||
1980 (t != NULL && Py_TYPE(op) != (PyTypeObject *) t)) {
1981 op = op->_ob_next;
1982 if (op == &refchain)
1983 return res;
1984 }
1985 if (PyList_Append(res, op) < 0) {
1986 Py_DECREF(res);
1987 return NULL;
1988 }
1989 op = op->_ob_next;
1990 }
1991 return res;
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +00001992}
1993
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001994#endif
Guido van Rossum97ead3f1996-01-12 01:24:09 +00001995
Benjamin Petersonb173f782009-05-05 22:31:58 +00001996
Guido van Rossum84a90321996-05-22 16:34:47 +00001997/* Hack to force loading of abstract.o */
Martin v. Löwis18e16552006-02-15 17:27:45 +00001998Py_ssize_t (*_Py_abstract_hack)(PyObject *) = PyObject_Size;
Guido van Rossume09fb551997-08-05 02:04:34 +00001999
2000
David Malcolm49526f42012-06-22 14:55:41 -04002001void
2002_PyObject_DebugTypeStats(FILE *out)
2003{
2004 _PyCFunction_DebugMallocStats(out);
2005 _PyDict_DebugMallocStats(out);
2006 _PyFloat_DebugMallocStats(out);
2007 _PyFrame_DebugMallocStats(out);
2008 _PyList_DebugMallocStats(out);
2009 _PyMethod_DebugMallocStats(out);
David Malcolm49526f42012-06-22 14:55:41 -04002010 _PyTuple_DebugMallocStats(out);
2011}
Guido van Rossumb18618d2000-05-03 23:44:39 +00002012
Guido van Rossum86610361998-04-10 22:32:46 +00002013/* These methods are used to control infinite recursion in repr, str, print,
2014 etc. Container objects that may recursively contain themselves,
Martin Panter8d56c022016-05-29 04:13:35 +00002015 e.g. builtin dictionaries and lists, should use Py_ReprEnter() and
Guido van Rossum86610361998-04-10 22:32:46 +00002016 Py_ReprLeave() to avoid infinite recursion.
2017
2018 Py_ReprEnter() returns 0 the first time it is called for a particular
2019 object and 1 every time thereafter. It returns -1 if an exception
2020 occurred. Py_ReprLeave() has no return value.
2021
2022 See dictobject.c and listobject.c for examples of use.
2023*/
2024
Guido van Rossum86610361998-04-10 22:32:46 +00002025int
Fred Drake100814d2000-07-09 15:48:49 +00002026Py_ReprEnter(PyObject *obj)
Guido van Rossum86610361998-04-10 22:32:46 +00002027{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002028 PyObject *dict;
2029 PyObject *list;
2030 Py_ssize_t i;
Guido van Rossum86610361998-04-10 22:32:46 +00002031
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002032 dict = PyThreadState_GetDict();
Antoine Pitrou04d17d32014-03-31 22:04:38 +02002033 /* Ignore a missing thread-state, so that this function can be called
2034 early on startup. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002035 if (dict == NULL)
2036 return 0;
Victor Stinner7a07e452013-11-06 18:57:29 +01002037 list = _PyDict_GetItemId(dict, &PyId_Py_Repr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002038 if (list == NULL) {
2039 list = PyList_New(0);
2040 if (list == NULL)
2041 return -1;
Victor Stinner7a07e452013-11-06 18:57:29 +01002042 if (_PyDict_SetItemId(dict, &PyId_Py_Repr, list) < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002043 return -1;
2044 Py_DECREF(list);
2045 }
2046 i = PyList_GET_SIZE(list);
2047 while (--i >= 0) {
2048 if (PyList_GET_ITEM(list, i) == obj)
2049 return 1;
2050 }
Victor Stinnere901d1f2013-07-17 21:58:41 +02002051 if (PyList_Append(list, obj) < 0)
2052 return -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002053 return 0;
Guido van Rossum86610361998-04-10 22:32:46 +00002054}
2055
2056void
Fred Drake100814d2000-07-09 15:48:49 +00002057Py_ReprLeave(PyObject *obj)
Guido van Rossum86610361998-04-10 22:32:46 +00002058{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002059 PyObject *dict;
2060 PyObject *list;
2061 Py_ssize_t i;
Victor Stinner1b634932013-07-16 22:24:44 +02002062 PyObject *error_type, *error_value, *error_traceback;
2063
2064 PyErr_Fetch(&error_type, &error_value, &error_traceback);
Guido van Rossum86610361998-04-10 22:32:46 +00002065
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002066 dict = PyThreadState_GetDict();
2067 if (dict == NULL)
Victor Stinner1b634932013-07-16 22:24:44 +02002068 goto finally;
2069
Victor Stinner7a07e452013-11-06 18:57:29 +01002070 list = _PyDict_GetItemId(dict, &PyId_Py_Repr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002071 if (list == NULL || !PyList_Check(list))
Victor Stinner1b634932013-07-16 22:24:44 +02002072 goto finally;
2073
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002074 i = PyList_GET_SIZE(list);
2075 /* Count backwards because we always expect obj to be list[-1] */
2076 while (--i >= 0) {
2077 if (PyList_GET_ITEM(list, i) == obj) {
2078 PyList_SetSlice(list, i, i + 1, NULL);
2079 break;
2080 }
2081 }
Victor Stinner1b634932013-07-16 22:24:44 +02002082
2083finally:
2084 /* ignore exceptions because there is no way to report them. */
2085 PyErr_Restore(error_type, error_value, error_traceback);
Guido van Rossum86610361998-04-10 22:32:46 +00002086}
Guido van Rossumd724b232000-03-13 16:01:29 +00002087
Tim Peters803526b2002-07-07 05:13:56 +00002088/* Trashcan support. */
Guido van Rossumd724b232000-03-13 16:01:29 +00002089
Tim Peters803526b2002-07-07 05:13:56 +00002090/* Add op to the _PyTrash_delete_later list. Called when the current
2091 * call-stack depth gets large. op must be a currently untracked gc'ed
2092 * object, with refcount 0. Py_DECREF must already have been called on it.
2093 */
Guido van Rossumd724b232000-03-13 16:01:29 +00002094void
Fred Drake100814d2000-07-09 15:48:49 +00002095_PyTrash_deposit_object(PyObject *op)
Guido van Rossumd724b232000-03-13 16:01:29 +00002096{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002097 assert(PyObject_IS_GC(op));
Antoine Pitrou796564c2013-07-30 19:59:21 +02002098 assert(_PyGC_REFS(op) == _PyGC_REFS_UNTRACKED);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002099 assert(op->ob_refcnt == 0);
Eric Snow2ebc5ce2017-09-07 23:51:28 -06002100 _Py_AS_GC(op)->gc.gc_prev = (PyGC_Head *)_PyRuntime.gc.trash_delete_later;
2101 _PyRuntime.gc.trash_delete_later = op;
Guido van Rossumd724b232000-03-13 16:01:29 +00002102}
2103
Antoine Pitrou2b0218a2012-09-06 00:59:49 +02002104/* The equivalent API, using per-thread state recursion info */
2105void
2106_PyTrash_thread_deposit_object(PyObject *op)
2107{
2108 PyThreadState *tstate = PyThreadState_GET();
2109 assert(PyObject_IS_GC(op));
Antoine Pitrou796564c2013-07-30 19:59:21 +02002110 assert(_PyGC_REFS(op) == _PyGC_REFS_UNTRACKED);
Antoine Pitrou2b0218a2012-09-06 00:59:49 +02002111 assert(op->ob_refcnt == 0);
2112 _Py_AS_GC(op)->gc.gc_prev = (PyGC_Head *) tstate->trash_delete_later;
2113 tstate->trash_delete_later = op;
2114}
2115
Tim Peters803526b2002-07-07 05:13:56 +00002116/* Dealloccate all the objects in the _PyTrash_delete_later list. Called when
2117 * the call-stack unwinds again.
2118 */
Guido van Rossumd724b232000-03-13 16:01:29 +00002119void
Fred Drake100814d2000-07-09 15:48:49 +00002120_PyTrash_destroy_chain(void)
Guido van Rossumd724b232000-03-13 16:01:29 +00002121{
Eric Snow2ebc5ce2017-09-07 23:51:28 -06002122 while (_PyRuntime.gc.trash_delete_later) {
2123 PyObject *op = _PyRuntime.gc.trash_delete_later;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002124 destructor dealloc = Py_TYPE(op)->tp_dealloc;
Neil Schemenauerf589c052002-03-29 03:05:54 +00002125
Eric Snow2ebc5ce2017-09-07 23:51:28 -06002126 _PyRuntime.gc.trash_delete_later =
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002127 (PyObject*) _Py_AS_GC(op)->gc.gc_prev;
Neil Schemenauerf589c052002-03-29 03:05:54 +00002128
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002129 /* Call the deallocator directly. This used to try to
2130 * fool Py_DECREF into calling it indirectly, but
2131 * Py_DECREF was already called on this object, and in
2132 * assorted non-release builds calling Py_DECREF again ends
2133 * up distorting allocation statistics.
2134 */
2135 assert(op->ob_refcnt == 0);
Eric Snow2ebc5ce2017-09-07 23:51:28 -06002136 ++_PyRuntime.gc.trash_delete_nesting;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002137 (*dealloc)(op);
Eric Snow2ebc5ce2017-09-07 23:51:28 -06002138 --_PyRuntime.gc.trash_delete_nesting;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002139 }
Guido van Rossumd724b232000-03-13 16:01:29 +00002140}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002141
Antoine Pitrou2b0218a2012-09-06 00:59:49 +02002142/* The equivalent API, using per-thread state recursion info */
2143void
2144_PyTrash_thread_destroy_chain(void)
2145{
2146 PyThreadState *tstate = PyThreadState_GET();
Xiang Zhanga66f9c62017-05-13 13:36:14 +08002147 /* We need to increase trash_delete_nesting here, otherwise,
2148 _PyTrash_thread_destroy_chain will be called recursively
2149 and then possibly crash. An example that may crash without
2150 increase:
2151 N = 500000 # need to be large enough
2152 ob = object()
2153 tups = [(ob,) for i in range(N)]
2154 for i in range(49):
2155 tups = [(tup,) for tup in tups]
2156 del tups
2157 */
2158 assert(tstate->trash_delete_nesting == 0);
2159 ++tstate->trash_delete_nesting;
Antoine Pitrou2b0218a2012-09-06 00:59:49 +02002160 while (tstate->trash_delete_later) {
2161 PyObject *op = tstate->trash_delete_later;
2162 destructor dealloc = Py_TYPE(op)->tp_dealloc;
2163
2164 tstate->trash_delete_later =
2165 (PyObject*) _Py_AS_GC(op)->gc.gc_prev;
2166
2167 /* Call the deallocator directly. This used to try to
2168 * fool Py_DECREF into calling it indirectly, but
2169 * Py_DECREF was already called on this object, and in
2170 * assorted non-release builds calling Py_DECREF again ends
2171 * up distorting allocation statistics.
2172 */
2173 assert(op->ob_refcnt == 0);
Antoine Pitrou2b0218a2012-09-06 00:59:49 +02002174 (*dealloc)(op);
Xiang Zhanga66f9c62017-05-13 13:36:14 +08002175 assert(tstate->trash_delete_nesting == 1);
Antoine Pitrou2b0218a2012-09-06 00:59:49 +02002176 }
Xiang Zhanga66f9c62017-05-13 13:36:14 +08002177 --tstate->trash_delete_nesting;
Antoine Pitrou2b0218a2012-09-06 00:59:49 +02002178}
2179
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00002180#ifndef Py_TRACE_REFS
2181/* For Py_LIMITED_API, we need an out-of-line version of _Py_Dealloc.
2182 Define this here, so we can undefine the macro. */
2183#undef _Py_Dealloc
2184PyAPI_FUNC(void) _Py_Dealloc(PyObject *);
2185void
2186_Py_Dealloc(PyObject *op)
2187{
2188 _Py_INC_TPFREES(op) _Py_COUNT_ALLOCS_COMMA
2189 (*Py_TYPE(op)->tp_dealloc)(op);
2190}
2191#endif
2192
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002193#ifdef __cplusplus
2194}
2195#endif