blob: 3eb4810bd9acb4bb36031e944523fda4b8802668 [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
INADA Naokid8521422018-05-17 11:07:21 +0900319 assert(!PyType_IS_GC(Py_TYPE(self)) || _PyObject_GC_IS_TRACKED(self));
Antoine Pitrou796564c2013-07-30 19:59:21 +0200320 /* If Py_REF_DEBUG, _Py_NewReference bumped _Py_RefTotal, so
321 * we need to undo that. */
322 _Py_DEC_REFTOTAL;
323 /* If Py_TRACE_REFS, _Py_NewReference re-added self to the object
324 * chain, so no more to do there.
325 * If COUNT_ALLOCS, the original decref bumped tp_frees, and
326 * _Py_NewReference bumped tp_allocs: both of those need to be
327 * undone.
328 */
329#ifdef COUNT_ALLOCS
330 --Py_TYPE(self)->tp_frees;
331 --Py_TYPE(self)->tp_allocs;
332#endif
333 return -1;
334}
335
Antoine Pitrouc47bd4a2010-07-27 22:08:27 +0000336int
337PyObject_Print(PyObject *op, FILE *fp, int flags)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000338{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000339 int ret = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000340 if (PyErr_CheckSignals())
341 return -1;
Guido van Rossum9b00dfa1998-04-28 16:06:54 +0000342#ifdef USE_STACKCHECK
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000343 if (PyOS_CheckStack()) {
344 PyErr_SetString(PyExc_MemoryError, "stack overflow");
345 return -1;
346 }
Guido van Rossum9b00dfa1998-04-28 16:06:54 +0000347#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000348 clearerr(fp); /* Clear any previous error condition */
349 if (op == NULL) {
350 Py_BEGIN_ALLOW_THREADS
351 fprintf(fp, "<nil>");
352 Py_END_ALLOW_THREADS
353 }
354 else {
355 if (op->ob_refcnt <= 0)
356 /* XXX(twouters) cast refcount to long until %zd is
357 universally available */
358 Py_BEGIN_ALLOW_THREADS
359 fprintf(fp, "<refcnt %ld at %p>",
360 (long)op->ob_refcnt, op);
361 Py_END_ALLOW_THREADS
362 else {
363 PyObject *s;
364 if (flags & Py_PRINT_RAW)
365 s = PyObject_Str(op);
366 else
367 s = PyObject_Repr(op);
368 if (s == NULL)
369 ret = -1;
370 else if (PyBytes_Check(s)) {
371 fwrite(PyBytes_AS_STRING(s), 1,
372 PyBytes_GET_SIZE(s), fp);
373 }
374 else if (PyUnicode_Check(s)) {
375 PyObject *t;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200376 t = PyUnicode_AsEncodedString(s, "utf-8", "backslashreplace");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000377 if (t == NULL)
378 ret = 0;
379 else {
380 fwrite(PyBytes_AS_STRING(t), 1,
381 PyBytes_GET_SIZE(t), fp);
Victor Stinnerba6b4302010-05-17 09:33:42 +0000382 Py_DECREF(t);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000383 }
384 }
385 else {
386 PyErr_Format(PyExc_TypeError,
387 "str() or repr() returned '%.100s'",
388 s->ob_type->tp_name);
389 ret = -1;
390 }
391 Py_XDECREF(s);
392 }
393 }
394 if (ret == 0) {
395 if (ferror(fp)) {
Serhiy Storchaka55fe1ae2017-04-16 10:46:38 +0300396 PyErr_SetFromErrno(PyExc_OSError);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000397 clearerr(fp);
398 ret = -1;
399 }
400 }
401 return ret;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000402}
403
Guido van Rossum38938152006-08-21 23:36:26 +0000404/* For debugging convenience. Set a breakpoint here and call it from your DLL */
405void
Thomas Woutersb2137042007-02-01 18:02:27 +0000406_Py_BreakPoint(void)
Guido van Rossum38938152006-08-21 23:36:26 +0000407{
408}
409
Neal Norwitz1a997502003-01-13 20:13:12 +0000410
Barry Warsaw9bf16442001-01-23 16:24:35 +0000411/* For debugging convenience. See Misc/gdbinit for some useful gdb hooks */
Guido van Rossum38938152006-08-21 23:36:26 +0000412void
413_PyObject_Dump(PyObject* op)
Barry Warsaw9bf16442001-01-23 16:24:35 +0000414{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000415 if (op == NULL)
416 fprintf(stderr, "NULL\n");
417 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000418 PyGILState_STATE gil;
Victor Stinnere5132102013-08-26 13:49:06 +0200419 PyObject *error_type, *error_value, *error_traceback;
420
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000421 fprintf(stderr, "object : ");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000422 gil = PyGILState_Ensure();
Victor Stinnere5132102013-08-26 13:49:06 +0200423
424 PyErr_Fetch(&error_type, &error_value, &error_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000425 (void)PyObject_Print(op, stderr, 0);
Victor Stinnere5132102013-08-26 13:49:06 +0200426 PyErr_Restore(error_type, error_value, error_traceback);
427
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000428 PyGILState_Release(gil);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000429 /* XXX(twouters) cast refcount to long until %zd is
430 universally available */
431 fprintf(stderr, "\n"
432 "type : %s\n"
433 "refcount: %ld\n"
434 "address : %p\n",
435 Py_TYPE(op)==NULL ? "NULL" : Py_TYPE(op)->tp_name,
436 (long)op->ob_refcnt,
437 op);
438 }
Barry Warsaw9bf16442001-01-23 16:24:35 +0000439}
Barry Warsaw903138f2001-01-23 16:33:18 +0000440
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000441PyObject *
Fred Drake100814d2000-07-09 15:48:49 +0000442PyObject_Repr(PyObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000443{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000444 PyObject *res;
445 if (PyErr_CheckSignals())
446 return NULL;
Guido van Rossum9b00dfa1998-04-28 16:06:54 +0000447#ifdef USE_STACKCHECK
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000448 if (PyOS_CheckStack()) {
449 PyErr_SetString(PyExc_MemoryError, "stack overflow");
450 return NULL;
451 }
Guido van Rossum9b00dfa1998-04-28 16:06:54 +0000452#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000453 if (v == NULL)
454 return PyUnicode_FromString("<NULL>");
455 if (Py_TYPE(v)->tp_repr == NULL)
456 return PyUnicode_FromFormat("<%s object at %p>",
457 v->ob_type->tp_name, v);
Victor Stinner33824f62013-08-26 14:05:19 +0200458
459#ifdef Py_DEBUG
460 /* PyObject_Repr() must not be called with an exception set,
Victor Stinnera8cb5152017-01-18 14:12:51 +0100461 because it can clear it (directly or indirectly) and so the
Martin Panter9955a372015-10-07 10:26:23 +0000462 caller loses its exception */
Victor Stinner33824f62013-08-26 14:05:19 +0200463 assert(!PyErr_Occurred());
464#endif
465
Serhiy Storchaka1fb72d22017-12-03 22:12:11 +0200466 /* It is possible for a type to have a tp_repr representation that loops
467 infinitely. */
468 if (Py_EnterRecursiveCall(" while getting the repr of an object"))
469 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000470 res = (*v->ob_type->tp_repr)(v);
Serhiy Storchaka1fb72d22017-12-03 22:12:11 +0200471 Py_LeaveRecursiveCall();
Victor Stinner0a54cf12011-12-01 03:22:44 +0100472 if (res == NULL)
473 return NULL;
474 if (!PyUnicode_Check(res)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000475 PyErr_Format(PyExc_TypeError,
476 "__repr__ returned non-string (type %.200s)",
477 res->ob_type->tp_name);
478 Py_DECREF(res);
479 return NULL;
480 }
Victor Stinnerdb88ae52011-12-01 02:15:00 +0100481#ifndef Py_DEBUG
482 if (PyUnicode_READY(res) < 0)
483 return NULL;
484#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000485 return res;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000486}
487
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000488PyObject *
Guido van Rossum98297ee2007-11-06 21:34:58 +0000489PyObject_Str(PyObject *v)
Guido van Rossumc6004111993-11-05 10:22:19 +0000490{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000491 PyObject *res;
492 if (PyErr_CheckSignals())
493 return NULL;
Guido van Rossum98297ee2007-11-06 21:34:58 +0000494#ifdef USE_STACKCHECK
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000495 if (PyOS_CheckStack()) {
496 PyErr_SetString(PyExc_MemoryError, "stack overflow");
497 return NULL;
498 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000499#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000500 if (v == NULL)
501 return PyUnicode_FromString("<NULL>");
502 if (PyUnicode_CheckExact(v)) {
Victor Stinnerdb88ae52011-12-01 02:15:00 +0100503#ifndef Py_DEBUG
Victor Stinner4ead7c72011-11-20 19:48:36 +0100504 if (PyUnicode_READY(v) < 0)
505 return NULL;
Victor Stinnerdb88ae52011-12-01 02:15:00 +0100506#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000507 Py_INCREF(v);
508 return v;
509 }
510 if (Py_TYPE(v)->tp_str == NULL)
511 return PyObject_Repr(v);
Guido van Rossum4f288ab2001-05-01 16:53:37 +0000512
Victor Stinner33824f62013-08-26 14:05:19 +0200513#ifdef Py_DEBUG
514 /* PyObject_Str() must not be called with an exception set,
Victor Stinnera8cb5152017-01-18 14:12:51 +0100515 because it can clear it (directly or indirectly) and so the
Nick Coghland979e432014-02-09 10:43:21 +1000516 caller loses its exception */
Victor Stinner33824f62013-08-26 14:05:19 +0200517 assert(!PyErr_Occurred());
518#endif
519
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000520 /* It is possible for a type to have a tp_str representation that loops
521 infinitely. */
522 if (Py_EnterRecursiveCall(" while getting the str of an object"))
523 return NULL;
524 res = (*Py_TYPE(v)->tp_str)(v);
525 Py_LeaveRecursiveCall();
526 if (res == NULL)
527 return NULL;
528 if (!PyUnicode_Check(res)) {
529 PyErr_Format(PyExc_TypeError,
530 "__str__ returned non-string (type %.200s)",
531 Py_TYPE(res)->tp_name);
532 Py_DECREF(res);
533 return NULL;
534 }
Victor Stinnerdb88ae52011-12-01 02:15:00 +0100535#ifndef Py_DEBUG
Victor Stinner4ead7c72011-11-20 19:48:36 +0100536 if (PyUnicode_READY(res) < 0)
537 return NULL;
Victor Stinnerdb88ae52011-12-01 02:15:00 +0100538#endif
Victor Stinner4ead7c72011-11-20 19:48:36 +0100539 assert(_PyUnicode_CheckConsistency(res, 1));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000540 return res;
Neil Schemenauercf52c072005-08-12 17:34:58 +0000541}
542
Georg Brandl559e5d72008-06-11 18:37:52 +0000543PyObject *
544PyObject_ASCII(PyObject *v)
545{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000546 PyObject *repr, *ascii, *res;
Georg Brandl559e5d72008-06-11 18:37:52 +0000547
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000548 repr = PyObject_Repr(v);
549 if (repr == NULL)
550 return NULL;
Georg Brandl559e5d72008-06-11 18:37:52 +0000551
Victor Stinneraf037572013-04-14 18:44:10 +0200552 if (PyUnicode_IS_ASCII(repr))
553 return repr;
554
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000555 /* repr is guaranteed to be a PyUnicode object by PyObject_Repr */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200556 ascii = _PyUnicode_AsASCIIString(repr, "backslashreplace");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000557 Py_DECREF(repr);
558 if (ascii == NULL)
559 return NULL;
Georg Brandl559e5d72008-06-11 18:37:52 +0000560
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000561 res = PyUnicode_DecodeASCII(
562 PyBytes_AS_STRING(ascii),
563 PyBytes_GET_SIZE(ascii),
564 NULL);
565
566 Py_DECREF(ascii);
567 return res;
Georg Brandl559e5d72008-06-11 18:37:52 +0000568}
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000569
Benjamin Petersonc15a0732008-08-26 16:46:47 +0000570PyObject *
571PyObject_Bytes(PyObject *v)
572{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000573 PyObject *result, *func;
Benjamin Petersonc15a0732008-08-26 16:46:47 +0000574
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000575 if (v == NULL)
576 return PyBytes_FromString("<NULL>");
Benjamin Petersonc15a0732008-08-26 16:46:47 +0000577
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000578 if (PyBytes_CheckExact(v)) {
579 Py_INCREF(v);
580 return v;
581 }
Benjamin Petersonc15a0732008-08-26 16:46:47 +0000582
Benjamin Petersonce798522012-01-22 11:24:29 -0500583 func = _PyObject_LookupSpecial(v, &PyId___bytes__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000584 if (func != NULL) {
Victor Stinnerf17c3de2016-12-06 18:46:19 +0100585 result = _PyObject_CallNoArg(func);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000586 Py_DECREF(func);
587 if (result == NULL)
Benjamin Peterson41ece392010-09-11 16:39:57 +0000588 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000589 if (!PyBytes_Check(result)) {
Benjamin Peterson41ece392010-09-11 16:39:57 +0000590 PyErr_Format(PyExc_TypeError,
591 "__bytes__ returned non-bytes (type %.200s)",
592 Py_TYPE(result)->tp_name);
593 Py_DECREF(result);
594 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000595 }
596 return result;
597 }
598 else if (PyErr_Occurred())
599 return NULL;
600 return PyBytes_FromObject(v);
Benjamin Petersonc15a0732008-08-26 16:46:47 +0000601}
602
Mark Dickinsonc008a172009-02-01 13:59:22 +0000603/* For Python 3.0.1 and later, the old three-way comparison has been
604 completely removed in favour of rich comparisons. PyObject_Compare() and
605 PyObject_Cmp() are gone, and the builtin cmp function no longer exists.
Mark Dickinsone94c6792009-02-02 20:36:42 +0000606 The old tp_compare slot has been renamed to tp_reserved, and should no
Mark Dickinsonc008a172009-02-01 13:59:22 +0000607 longer be used. Use tp_richcompare instead.
Guido van Rossum98297ee2007-11-06 21:34:58 +0000608
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000609 See (*) below for practical amendments.
610
Mark Dickinsonc008a172009-02-01 13:59:22 +0000611 tp_richcompare gets called with a first argument of the appropriate type
612 and a second object of an arbitrary type. We never do any kind of
613 coercion.
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000614
Mark Dickinsonc008a172009-02-01 13:59:22 +0000615 The tp_richcompare slot should return an object, as follows:
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000616
617 NULL if an exception occurred
618 NotImplemented if the requested comparison is not implemented
619 any other false value if the requested comparison is false
620 any other true value if the requested comparison is true
621
622 The PyObject_RichCompare[Bool]() wrappers raise TypeError when they get
623 NotImplemented.
624
625 (*) Practical amendments:
626
627 - If rich comparison returns NotImplemented, == and != are decided by
628 comparing the object pointer (i.e. falling back to the base object
629 implementation).
630
Guido van Rossuma4073002002-05-31 20:03:54 +0000631*/
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000632
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000633/* Map rich comparison operators to their swapped version, e.g. LT <--> GT */
Brett Cannona5ca2e72004-09-25 01:37:24 +0000634int _Py_SwappedOp[] = {Py_GT, Py_GE, Py_EQ, Py_NE, Py_LT, Py_LE};
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +0000635
Serhiy Storchaka2d06e842015-12-25 19:53:18 +0200636static const char * const opstrings[] = {"<", "<=", "==", "!=", ">", ">="};
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000637
638/* Perform a rich comparison, raising TypeError when the requested comparison
639 operator is not supported. */
Guido van Rossume797ec12001-01-17 15:24:28 +0000640static PyObject *
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000641do_richcompare(PyObject *v, PyObject *w, int op)
Guido van Rossume797ec12001-01-17 15:24:28 +0000642{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000643 richcmpfunc f;
644 PyObject *res;
645 int checked_reverse_op = 0;
Guido van Rossume797ec12001-01-17 15:24:28 +0000646
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000647 if (v->ob_type != w->ob_type &&
648 PyType_IsSubtype(w->ob_type, v->ob_type) &&
649 (f = w->ob_type->tp_richcompare) != NULL) {
650 checked_reverse_op = 1;
651 res = (*f)(w, v, _Py_SwappedOp[op]);
652 if (res != Py_NotImplemented)
653 return res;
654 Py_DECREF(res);
655 }
656 if ((f = v->ob_type->tp_richcompare) != NULL) {
657 res = (*f)(v, w, op);
658 if (res != Py_NotImplemented)
659 return res;
660 Py_DECREF(res);
661 }
662 if (!checked_reverse_op && (f = w->ob_type->tp_richcompare) != NULL) {
663 res = (*f)(w, v, _Py_SwappedOp[op]);
664 if (res != Py_NotImplemented)
665 return res;
666 Py_DECREF(res);
667 }
668 /* If neither object implements it, provide a sensible default
669 for == and !=, but raise an exception for ordering. */
670 switch (op) {
671 case Py_EQ:
672 res = (v == w) ? Py_True : Py_False;
673 break;
674 case Py_NE:
675 res = (v != w) ? Py_True : Py_False;
676 break;
677 default:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000678 PyErr_Format(PyExc_TypeError,
Victor Stinner91108f02015-10-14 18:25:31 +0200679 "'%s' not supported between instances of '%.100s' and '%.100s'",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000680 opstrings[op],
Victor Stinner91108f02015-10-14 18:25:31 +0200681 v->ob_type->tp_name,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000682 w->ob_type->tp_name);
683 return NULL;
684 }
685 Py_INCREF(res);
686 return res;
Guido van Rossume797ec12001-01-17 15:24:28 +0000687}
688
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000689/* Perform a rich comparison with object result. This wraps do_richcompare()
690 with a check for NULL arguments and a recursion check. */
Guido van Rossume797ec12001-01-17 15:24:28 +0000691
Guido van Rossume797ec12001-01-17 15:24:28 +0000692PyObject *
693PyObject_RichCompare(PyObject *v, PyObject *w, int op)
694{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000695 PyObject *res;
Guido van Rossume797ec12001-01-17 15:24:28 +0000696
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000697 assert(Py_LT <= op && op <= Py_GE);
698 if (v == NULL || w == NULL) {
699 if (!PyErr_Occurred())
700 PyErr_BadInternalCall();
701 return NULL;
702 }
703 if (Py_EnterRecursiveCall(" in comparison"))
704 return NULL;
705 res = do_richcompare(v, w, op);
706 Py_LeaveRecursiveCall();
707 return res;
Guido van Rossume797ec12001-01-17 15:24:28 +0000708}
709
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000710/* Perform a rich comparison with integer result. This wraps
711 PyObject_RichCompare(), returning -1 for error, 0 for false, 1 for true. */
Guido van Rossume797ec12001-01-17 15:24:28 +0000712int
713PyObject_RichCompareBool(PyObject *v, PyObject *w, int op)
714{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000715 PyObject *res;
716 int ok;
Guido van Rossume797ec12001-01-17 15:24:28 +0000717
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000718 /* Quick result when objects are the same.
719 Guarantees that identity implies equality. */
720 if (v == w) {
721 if (op == Py_EQ)
722 return 1;
723 else if (op == Py_NE)
724 return 0;
725 }
Mark Dickinson4a1f5932008-11-12 23:23:36 +0000726
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000727 res = PyObject_RichCompare(v, w, op);
728 if (res == NULL)
729 return -1;
730 if (PyBool_Check(res))
731 ok = (res == Py_True);
732 else
733 ok = PyObject_IsTrue(res);
734 Py_DECREF(res);
735 return ok;
Guido van Rossume797ec12001-01-17 15:24:28 +0000736}
Fred Drake13634cf2000-06-29 19:17:04 +0000737
Antoine Pitrouce4a9da2011-11-21 20:46:33 +0100738Py_hash_t
Nick Coghland1abd252008-07-15 15:46:38 +0000739PyObject_HashNotImplemented(PyObject *v)
740{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000741 PyErr_Format(PyExc_TypeError, "unhashable type: '%.200s'",
742 Py_TYPE(v)->tp_name);
743 return -1;
Nick Coghland1abd252008-07-15 15:46:38 +0000744}
Fred Drake13634cf2000-06-29 19:17:04 +0000745
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000746Py_hash_t
Fred Drake100814d2000-07-09 15:48:49 +0000747PyObject_Hash(PyObject *v)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000748{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000749 PyTypeObject *tp = Py_TYPE(v);
750 if (tp->tp_hash != NULL)
751 return (*tp->tp_hash)(v);
752 /* To keep to the general practice that inheriting
753 * solely from object in C code should work without
754 * an explicit call to PyType_Ready, we implicitly call
755 * PyType_Ready here and then check the tp_hash slot again
756 */
757 if (tp->tp_dict == NULL) {
758 if (PyType_Ready(tp) < 0)
759 return -1;
760 if (tp->tp_hash != NULL)
761 return (*tp->tp_hash)(v);
762 }
763 /* Otherwise, the object can't be hashed */
764 return PyObject_HashNotImplemented(v);
Guido van Rossum9bfef441993-03-29 10:43:31 +0000765}
766
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000767PyObject *
Jeremy Hyltonaf68c872005-12-10 18:50:16 +0000768PyObject_GetAttrString(PyObject *v, const char *name)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000769{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000770 PyObject *w, *res;
Guido van Rossumd8eb1b31996-08-09 20:52:03 +0000771
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000772 if (Py_TYPE(v)->tp_getattr != NULL)
773 return (*Py_TYPE(v)->tp_getattr)(v, (char*)name);
INADA Naoki3e8d6cb2017-02-21 23:57:25 +0900774 w = PyUnicode_FromString(name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000775 if (w == NULL)
776 return NULL;
777 res = PyObject_GetAttr(v, w);
Victor Stinner59af08f2012-03-22 02:09:08 +0100778 Py_DECREF(w);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000779 return res;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000780}
781
782int
Jeremy Hyltonaf68c872005-12-10 18:50:16 +0000783PyObject_HasAttrString(PyObject *v, const char *name)
Guido van Rossumed18fdc1993-07-11 19:55:34 +0000784{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000785 PyObject *res = PyObject_GetAttrString(v, name);
786 if (res != NULL) {
787 Py_DECREF(res);
788 return 1;
789 }
790 PyErr_Clear();
791 return 0;
Guido van Rossumed18fdc1993-07-11 19:55:34 +0000792}
793
794int
Jeremy Hyltonaf68c872005-12-10 18:50:16 +0000795PyObject_SetAttrString(PyObject *v, const char *name, PyObject *w)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000796{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000797 PyObject *s;
798 int res;
Guido van Rossumd8eb1b31996-08-09 20:52:03 +0000799
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000800 if (Py_TYPE(v)->tp_setattr != NULL)
801 return (*Py_TYPE(v)->tp_setattr)(v, (char*)name, w);
802 s = PyUnicode_InternFromString(name);
803 if (s == NULL)
804 return -1;
805 res = PyObject_SetAttr(v, s, w);
806 Py_XDECREF(s);
807 return res;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000808}
809
Benjamin Petersonbfebb7b2011-12-15 15:34:02 -0500810int
811_PyObject_IsAbstract(PyObject *obj)
812{
813 int res;
814 PyObject* isabstract;
Benjamin Petersonbfebb7b2011-12-15 15:34:02 -0500815
816 if (obj == NULL)
817 return 0;
818
Serhiy Storchakaf320be72018-01-25 10:49:40 +0200819 res = _PyObject_LookupAttrId(obj, &PyId___isabstractmethod__, &isabstract);
820 if (res > 0) {
821 res = PyObject_IsTrue(isabstract);
822 Py_DECREF(isabstract);
Benjamin Petersonbfebb7b2011-12-15 15:34:02 -0500823 }
Benjamin Petersonbfebb7b2011-12-15 15:34:02 -0500824 return res;
825}
826
Guido van Rossum98ff96a1997-05-20 18:34:44 +0000827PyObject *
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200828_PyObject_GetAttrId(PyObject *v, _Py_Identifier *name)
829{
830 PyObject *result;
Martin v. Löwisd10759f2011-11-07 13:00:05 +0100831 PyObject *oname = _PyUnicode_FromId(name); /* borrowed */
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200832 if (!oname)
833 return NULL;
834 result = PyObject_GetAttr(v, oname);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200835 return result;
836}
837
838int
839_PyObject_HasAttrId(PyObject *v, _Py_Identifier *name)
840{
841 int result;
Martin v. Löwisd10759f2011-11-07 13:00:05 +0100842 PyObject *oname = _PyUnicode_FromId(name); /* borrowed */
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200843 if (!oname)
844 return -1;
845 result = PyObject_HasAttr(v, oname);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200846 return result;
847}
848
849int
850_PyObject_SetAttrId(PyObject *v, _Py_Identifier *name, PyObject *w)
851{
852 int result;
Martin v. Löwisd10759f2011-11-07 13:00:05 +0100853 PyObject *oname = _PyUnicode_FromId(name); /* borrowed */
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200854 if (!oname)
855 return -1;
856 result = PyObject_SetAttr(v, oname, w);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200857 return result;
858}
859
860PyObject *
Fred Drake100814d2000-07-09 15:48:49 +0000861PyObject_GetAttr(PyObject *v, PyObject *name)
Guido van Rossum98ff96a1997-05-20 18:34:44 +0000862{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000863 PyTypeObject *tp = Py_TYPE(v);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000864
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000865 if (!PyUnicode_Check(name)) {
866 PyErr_Format(PyExc_TypeError,
867 "attribute name must be string, not '%.200s'",
868 name->ob_type->tp_name);
869 return NULL;
870 }
871 if (tp->tp_getattro != NULL)
872 return (*tp->tp_getattro)(v, name);
873 if (tp->tp_getattr != NULL) {
Serhiy Storchaka2a404b62017-01-22 23:07:07 +0200874 const char *name_str = PyUnicode_AsUTF8(name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000875 if (name_str == NULL)
876 return NULL;
Serhiy Storchaka2a404b62017-01-22 23:07:07 +0200877 return (*tp->tp_getattr)(v, (char *)name_str);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000878 }
879 PyErr_Format(PyExc_AttributeError,
880 "'%.50s' object has no attribute '%U'",
881 tp->tp_name, name);
882 return NULL;
Guido van Rossum98ff96a1997-05-20 18:34:44 +0000883}
884
Serhiy Storchakaf320be72018-01-25 10:49:40 +0200885int
886_PyObject_LookupAttr(PyObject *v, PyObject *name, PyObject **result)
INADA Naoki378edee2018-01-16 20:52:41 +0900887{
888 PyTypeObject *tp = Py_TYPE(v);
INADA Naoki378edee2018-01-16 20:52:41 +0900889
890 if (!PyUnicode_Check(name)) {
891 PyErr_Format(PyExc_TypeError,
892 "attribute name must be string, not '%.200s'",
893 name->ob_type->tp_name);
Serhiy Storchakaf320be72018-01-25 10:49:40 +0200894 *result = NULL;
895 return -1;
INADA Naoki378edee2018-01-16 20:52:41 +0900896 }
897
898 if (tp->tp_getattro == PyObject_GenericGetAttr) {
Serhiy Storchakaf320be72018-01-25 10:49:40 +0200899 *result = _PyObject_GenericGetAttrWithDict(v, name, NULL, 1);
900 if (*result != NULL) {
901 return 1;
902 }
903 if (PyErr_Occurred()) {
904 return -1;
905 }
906 return 0;
INADA Naoki378edee2018-01-16 20:52:41 +0900907 }
908 if (tp->tp_getattro != NULL) {
Serhiy Storchakaf320be72018-01-25 10:49:40 +0200909 *result = (*tp->tp_getattro)(v, name);
INADA Naoki378edee2018-01-16 20:52:41 +0900910 }
911 else if (tp->tp_getattr != NULL) {
912 const char *name_str = PyUnicode_AsUTF8(name);
Serhiy Storchakaf320be72018-01-25 10:49:40 +0200913 if (name_str == NULL) {
914 *result = NULL;
915 return -1;
916 }
917 *result = (*tp->tp_getattr)(v, (char *)name_str);
INADA Naoki378edee2018-01-16 20:52:41 +0900918 }
INADA Naokie76daeb2018-01-26 16:22:51 +0900919 else {
920 *result = NULL;
921 return 0;
922 }
923
Serhiy Storchakaf320be72018-01-25 10:49:40 +0200924 if (*result != NULL) {
925 return 1;
INADA Naoki378edee2018-01-16 20:52:41 +0900926 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +0200927 if (!PyErr_ExceptionMatches(PyExc_AttributeError)) {
928 return -1;
929 }
930 PyErr_Clear();
931 return 0;
932}
933
934int
935_PyObject_LookupAttrId(PyObject *v, _Py_Identifier *name, PyObject **result)
936{
937 PyObject *oname = _PyUnicode_FromId(name); /* borrowed */
938 if (!oname) {
939 *result = NULL;
940 return -1;
941 }
942 return _PyObject_LookupAttr(v, oname, result);
INADA Naoki378edee2018-01-16 20:52:41 +0900943}
944
Guido van Rossum98ff96a1997-05-20 18:34:44 +0000945int
Fred Drake100814d2000-07-09 15:48:49 +0000946PyObject_HasAttr(PyObject *v, PyObject *name)
Guido van Rossum98ff96a1997-05-20 18:34:44 +0000947{
Serhiy Storchakaf320be72018-01-25 10:49:40 +0200948 PyObject *res;
949 if (_PyObject_LookupAttr(v, name, &res) < 0) {
950 PyErr_Clear();
951 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000952 }
Serhiy Storchakaf320be72018-01-25 10:49:40 +0200953 if (res == NULL) {
954 return 0;
955 }
956 Py_DECREF(res);
957 return 1;
Guido van Rossum98ff96a1997-05-20 18:34:44 +0000958}
959
960int
Fred Drake100814d2000-07-09 15:48:49 +0000961PyObject_SetAttr(PyObject *v, PyObject *name, PyObject *value)
Guido van Rossum98ff96a1997-05-20 18:34:44 +0000962{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000963 PyTypeObject *tp = Py_TYPE(v);
964 int err;
Marc-André Lemburge44e5072000-09-18 16:20:57 +0000965
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000966 if (!PyUnicode_Check(name)) {
967 PyErr_Format(PyExc_TypeError,
968 "attribute name must be string, not '%.200s'",
969 name->ob_type->tp_name);
970 return -1;
971 }
972 Py_INCREF(name);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000973
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000974 PyUnicode_InternInPlace(&name);
975 if (tp->tp_setattro != NULL) {
976 err = (*tp->tp_setattro)(v, name, value);
977 Py_DECREF(name);
978 return err;
979 }
980 if (tp->tp_setattr != NULL) {
Serhiy Storchaka2a404b62017-01-22 23:07:07 +0200981 const char *name_str = PyUnicode_AsUTF8(name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000982 if (name_str == NULL)
983 return -1;
Serhiy Storchaka2a404b62017-01-22 23:07:07 +0200984 err = (*tp->tp_setattr)(v, (char *)name_str, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000985 Py_DECREF(name);
986 return err;
987 }
988 Py_DECREF(name);
989 assert(name->ob_refcnt >= 1);
990 if (tp->tp_getattr == NULL && tp->tp_getattro == NULL)
991 PyErr_Format(PyExc_TypeError,
992 "'%.100s' object has no attributes "
993 "(%s .%U)",
994 tp->tp_name,
995 value==NULL ? "del" : "assign to",
996 name);
997 else
998 PyErr_Format(PyExc_TypeError,
999 "'%.100s' object has only read-only attributes "
1000 "(%s .%U)",
1001 tp->tp_name,
1002 value==NULL ? "del" : "assign to",
1003 name);
1004 return -1;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001005}
1006
1007/* Helper to get a pointer to an object's __dict__ slot, if any */
1008
1009PyObject **
1010_PyObject_GetDictPtr(PyObject *obj)
1011{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001012 Py_ssize_t dictoffset;
1013 PyTypeObject *tp = Py_TYPE(obj);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001014
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001015 dictoffset = tp->tp_dictoffset;
1016 if (dictoffset == 0)
1017 return NULL;
1018 if (dictoffset < 0) {
1019 Py_ssize_t tsize;
1020 size_t size;
Guido van Rossum2eb0b872002-03-01 22:24:49 +00001021
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001022 tsize = ((PyVarObject *)obj)->ob_size;
1023 if (tsize < 0)
1024 tsize = -tsize;
1025 size = _PyObject_VAR_SIZE(tp, tsize);
Guido van Rossum2eb0b872002-03-01 22:24:49 +00001026
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001027 dictoffset += (long)size;
1028 assert(dictoffset > 0);
1029 assert(dictoffset % SIZEOF_VOID_P == 0);
1030 }
1031 return (PyObject **) ((char *)obj + dictoffset);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001032}
1033
Tim Peters6d6c1a32001-08-02 04:15:00 +00001034PyObject *
Raymond Hettinger1da1dbf2003-03-17 19:46:11 +00001035PyObject_SelfIter(PyObject *obj)
Raymond Hettinger01538262003-03-17 08:24:35 +00001036{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001037 Py_INCREF(obj);
1038 return obj;
Raymond Hettinger01538262003-03-17 08:24:35 +00001039}
1040
Antoine Pitroua7013882012-04-05 00:04:20 +02001041/* Convenience function to get a builtin from its name */
1042PyObject *
1043_PyObject_GetBuiltin(const char *name)
1044{
Victor Stinner53e9ec42013-11-07 00:43:05 +01001045 PyObject *mod_name, *mod, *attr;
1046
Victor Stinnerbd303c12013-11-07 23:07:29 +01001047 mod_name = _PyUnicode_FromId(&PyId_builtins); /* borrowed */
Victor Stinner53e9ec42013-11-07 00:43:05 +01001048 if (mod_name == NULL)
1049 return NULL;
1050 mod = PyImport_Import(mod_name);
Antoine Pitroua7013882012-04-05 00:04:20 +02001051 if (mod == NULL)
1052 return NULL;
1053 attr = PyObject_GetAttrString(mod, name);
1054 Py_DECREF(mod);
1055 return attr;
1056}
1057
Amaury Forgeot d'Arcf343e012009-01-12 23:58:21 +00001058/* Helper used when the __next__ method is removed from a type:
1059 tp_iternext is never NULL and can be safely called without checking
1060 on every iteration.
1061 */
1062
1063PyObject *
1064_PyObject_NextNotImplemented(PyObject *self)
1065{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001066 PyErr_Format(PyExc_TypeError,
1067 "'%.200s' object is not iterable",
1068 Py_TYPE(self)->tp_name);
1069 return NULL;
Amaury Forgeot d'Arcf343e012009-01-12 23:58:21 +00001070}
1071
Yury Selivanovf2392132016-12-13 19:03:51 -05001072
1073/* Specialized version of _PyObject_GenericGetAttrWithDict
1074 specifically for the LOAD_METHOD opcode.
1075
1076 Return 1 if a method is found, 0 if it's a regular attribute
1077 from __dict__ or something returned by using a descriptor
1078 protocol.
1079
1080 `method` will point to the resolved attribute or NULL. In the
1081 latter case, an error will be set.
1082*/
1083int
1084_PyObject_GetMethod(PyObject *obj, PyObject *name, PyObject **method)
1085{
1086 PyTypeObject *tp = Py_TYPE(obj);
1087 PyObject *descr;
1088 descrgetfunc f = NULL;
1089 PyObject **dictptr, *dict;
1090 PyObject *attr;
1091 int meth_found = 0;
1092
1093 assert(*method == NULL);
1094
1095 if (Py_TYPE(obj)->tp_getattro != PyObject_GenericGetAttr
1096 || !PyUnicode_Check(name)) {
1097 *method = PyObject_GetAttr(obj, name);
1098 return 0;
1099 }
1100
1101 if (tp->tp_dict == NULL && PyType_Ready(tp) < 0)
1102 return 0;
1103
1104 descr = _PyType_Lookup(tp, name);
1105 if (descr != NULL) {
1106 Py_INCREF(descr);
INADA Naoki5566bbb2017-02-03 07:43:03 +09001107 if (PyFunction_Check(descr) ||
1108 (Py_TYPE(descr) == &PyMethodDescr_Type)) {
Yury Selivanovf2392132016-12-13 19:03:51 -05001109 meth_found = 1;
1110 } else {
1111 f = descr->ob_type->tp_descr_get;
1112 if (f != NULL && PyDescr_IsData(descr)) {
1113 *method = f(descr, obj, (PyObject *)obj->ob_type);
1114 Py_DECREF(descr);
1115 return 0;
1116 }
1117 }
1118 }
1119
1120 dictptr = _PyObject_GetDictPtr(obj);
1121 if (dictptr != NULL && (dict = *dictptr) != NULL) {
1122 Py_INCREF(dict);
1123 attr = PyDict_GetItem(dict, name);
1124 if (attr != NULL) {
1125 Py_INCREF(attr);
1126 *method = attr;
1127 Py_DECREF(dict);
1128 Py_XDECREF(descr);
1129 return 0;
1130 }
1131 Py_DECREF(dict);
1132 }
1133
1134 if (meth_found) {
1135 *method = descr;
1136 return 1;
1137 }
1138
1139 if (f != NULL) {
1140 *method = f(descr, obj, (PyObject *)Py_TYPE(obj));
1141 Py_DECREF(descr);
1142 return 0;
1143 }
1144
1145 if (descr != NULL) {
1146 *method = descr;
1147 return 0;
1148 }
1149
1150 PyErr_Format(PyExc_AttributeError,
1151 "'%.50s' object has no attribute '%U'",
1152 tp->tp_name, name);
1153 return 0;
1154}
1155
1156/* Generic GetAttr functions - put these in your tp_[gs]etattro slot. */
Michael W. Hudson1593f502004-09-14 17:09:47 +00001157
Raymond Hettinger01538262003-03-17 08:24:35 +00001158PyObject *
INADA Naoki378edee2018-01-16 20:52:41 +09001159_PyObject_GenericGetAttrWithDict(PyObject *obj, PyObject *name,
1160 PyObject *dict, int suppress)
Tim Peters6d6c1a32001-08-02 04:15:00 +00001161{
Yury Selivanovf2392132016-12-13 19:03:51 -05001162 /* Make sure the logic of _PyObject_GetMethod is in sync with
1163 this method.
INADA Naoki378edee2018-01-16 20:52:41 +09001164
1165 When suppress=1, this function suppress AttributeError.
Yury Selivanovf2392132016-12-13 19:03:51 -05001166 */
1167
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001168 PyTypeObject *tp = Py_TYPE(obj);
1169 PyObject *descr = NULL;
1170 PyObject *res = NULL;
1171 descrgetfunc f;
1172 Py_ssize_t dictoffset;
1173 PyObject **dictptr;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001174
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001175 if (!PyUnicode_Check(name)){
1176 PyErr_Format(PyExc_TypeError,
1177 "attribute name must be string, not '%.200s'",
1178 name->ob_type->tp_name);
1179 return NULL;
1180 }
Serhiy Storchaka55c861f2016-04-17 20:31:51 +03001181 Py_INCREF(name);
Guido van Rossumebca9fc2001-12-04 15:54:53 +00001182
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001183 if (tp->tp_dict == NULL) {
1184 if (PyType_Ready(tp) < 0)
1185 goto done;
1186 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001187
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001188 descr = _PyType_Lookup(tp, name);
Michael W. Hudsonb2c7de42003-08-15 13:07:47 +00001189
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001190 f = NULL;
1191 if (descr != NULL) {
Serhiy Storchaka55c861f2016-04-17 20:31:51 +03001192 Py_INCREF(descr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001193 f = descr->ob_type->tp_descr_get;
1194 if (f != NULL && PyDescr_IsData(descr)) {
1195 res = f(descr, obj, (PyObject *)obj->ob_type);
INADA Naoki378edee2018-01-16 20:52:41 +09001196 if (res == NULL && suppress &&
1197 PyErr_ExceptionMatches(PyExc_AttributeError)) {
1198 PyErr_Clear();
1199 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001200 goto done;
1201 }
1202 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001203
Antoine Pitrou1a9a9d52010-08-28 18:17:03 +00001204 if (dict == NULL) {
1205 /* Inline _PyObject_GetDictPtr */
1206 dictoffset = tp->tp_dictoffset;
1207 if (dictoffset != 0) {
1208 if (dictoffset < 0) {
1209 Py_ssize_t tsize;
1210 size_t size;
Guido van Rossumc66ff442002-08-19 16:50:48 +00001211
Antoine Pitrou1a9a9d52010-08-28 18:17:03 +00001212 tsize = ((PyVarObject *)obj)->ob_size;
1213 if (tsize < 0)
1214 tsize = -tsize;
1215 size = _PyObject_VAR_SIZE(tp, tsize);
Serhiy Storchaka55c861f2016-04-17 20:31:51 +03001216 assert(size <= PY_SSIZE_T_MAX);
Guido van Rossumc66ff442002-08-19 16:50:48 +00001217
Serhiy Storchaka55c861f2016-04-17 20:31:51 +03001218 dictoffset += (Py_ssize_t)size;
Antoine Pitrou1a9a9d52010-08-28 18:17:03 +00001219 assert(dictoffset > 0);
1220 assert(dictoffset % SIZEOF_VOID_P == 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001221 }
Antoine Pitrou1a9a9d52010-08-28 18:17:03 +00001222 dictptr = (PyObject **) ((char *)obj + dictoffset);
1223 dict = *dictptr;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001224 }
1225 }
Antoine Pitrou1a9a9d52010-08-28 18:17:03 +00001226 if (dict != NULL) {
1227 Py_INCREF(dict);
1228 res = PyDict_GetItem(dict, name);
1229 if (res != NULL) {
1230 Py_INCREF(res);
Antoine Pitrou1a9a9d52010-08-28 18:17:03 +00001231 Py_DECREF(dict);
1232 goto done;
1233 }
1234 Py_DECREF(dict);
1235 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001236
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001237 if (f != NULL) {
1238 res = f(descr, obj, (PyObject *)Py_TYPE(obj));
INADA Naoki378edee2018-01-16 20:52:41 +09001239 if (res == NULL && suppress &&
1240 PyErr_ExceptionMatches(PyExc_AttributeError)) {
1241 PyErr_Clear();
1242 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001243 goto done;
1244 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001245
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001246 if (descr != NULL) {
1247 res = descr;
Victor Stinner2d01dc02012-03-09 00:44:13 +01001248 descr = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001249 goto done;
1250 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001251
INADA Naoki378edee2018-01-16 20:52:41 +09001252 if (!suppress) {
1253 PyErr_Format(PyExc_AttributeError,
1254 "'%.50s' object has no attribute '%U'",
1255 tp->tp_name, name);
1256 }
Guido van Rossumebca9fc2001-12-04 15:54:53 +00001257 done:
Victor Stinner2d01dc02012-03-09 00:44:13 +01001258 Py_XDECREF(descr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001259 Py_DECREF(name);
1260 return res;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001261}
1262
Antoine Pitrou1a9a9d52010-08-28 18:17:03 +00001263PyObject *
1264PyObject_GenericGetAttr(PyObject *obj, PyObject *name)
1265{
INADA Naoki378edee2018-01-16 20:52:41 +09001266 return _PyObject_GenericGetAttrWithDict(obj, name, NULL, 0);
Antoine Pitrou1a9a9d52010-08-28 18:17:03 +00001267}
1268
Tim Peters6d6c1a32001-08-02 04:15:00 +00001269int
Antoine Pitrou1a9a9d52010-08-28 18:17:03 +00001270_PyObject_GenericSetAttrWithDict(PyObject *obj, PyObject *name,
1271 PyObject *value, PyObject *dict)
Tim Peters6d6c1a32001-08-02 04:15:00 +00001272{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001273 PyTypeObject *tp = Py_TYPE(obj);
1274 PyObject *descr;
1275 descrsetfunc f;
1276 PyObject **dictptr;
1277 int res = -1;
Guido van Rossumebca9fc2001-12-04 15:54:53 +00001278
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001279 if (!PyUnicode_Check(name)){
1280 PyErr_Format(PyExc_TypeError,
1281 "attribute name must be string, not '%.200s'",
1282 name->ob_type->tp_name);
1283 return -1;
1284 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001285
Benjamin Peterson74529ad2012-03-09 07:25:32 -08001286 if (tp->tp_dict == NULL && PyType_Ready(tp) < 0)
1287 return -1;
1288
1289 Py_INCREF(name);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001290
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001291 descr = _PyType_Lookup(tp, name);
Victor Stinner2d01dc02012-03-09 00:44:13 +01001292
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001293 if (descr != NULL) {
Serhiy Storchaka55c861f2016-04-17 20:31:51 +03001294 Py_INCREF(descr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001295 f = descr->ob_type->tp_descr_set;
Serhiy Storchaka55c861f2016-04-17 20:31:51 +03001296 if (f != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001297 res = f(descr, obj, value);
1298 goto done;
1299 }
1300 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001301
Antoine Pitrou1a9a9d52010-08-28 18:17:03 +00001302 if (dict == NULL) {
1303 dictptr = _PyObject_GetDictPtr(obj);
Serhiy Storchaka55c861f2016-04-17 20:31:51 +03001304 if (dictptr == NULL) {
1305 if (descr == NULL) {
1306 PyErr_Format(PyExc_AttributeError,
1307 "'%.100s' object has no attribute '%U'",
1308 tp->tp_name, name);
1309 }
1310 else {
1311 PyErr_Format(PyExc_AttributeError,
1312 "'%.50s' object attribute '%U' is read-only",
1313 tp->tp_name, name);
1314 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001315 goto done;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001316 }
Serhiy Storchaka55c861f2016-04-17 20:31:51 +03001317 res = _PyObjectDict_SetItem(tp, dictptr, name, value);
Antoine Pitrou1a9a9d52010-08-28 18:17:03 +00001318 }
Serhiy Storchaka55c861f2016-04-17 20:31:51 +03001319 else {
Antoine Pitrou1a9a9d52010-08-28 18:17:03 +00001320 Py_INCREF(dict);
1321 if (value == NULL)
1322 res = PyDict_DelItem(dict, name);
1323 else
1324 res = PyDict_SetItem(dict, name, value);
Benjamin Peterson74529ad2012-03-09 07:25:32 -08001325 Py_DECREF(dict);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001326 }
Serhiy Storchaka55c861f2016-04-17 20:31:51 +03001327 if (res < 0 && PyErr_ExceptionMatches(PyExc_KeyError))
1328 PyErr_SetObject(PyExc_AttributeError, name);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001329
Guido van Rossumebca9fc2001-12-04 15:54:53 +00001330 done:
Victor Stinner2d01dc02012-03-09 00:44:13 +01001331 Py_XDECREF(descr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001332 Py_DECREF(name);
1333 return res;
Guido van Rossum98ff96a1997-05-20 18:34:44 +00001334}
1335
Antoine Pitrou1a9a9d52010-08-28 18:17:03 +00001336int
1337PyObject_GenericSetAttr(PyObject *obj, PyObject *name, PyObject *value)
1338{
1339 return _PyObject_GenericSetAttrWithDict(obj, name, value, NULL);
1340}
1341
Benjamin Peterson8eb12692012-02-19 19:59:10 -05001342int
1343PyObject_GenericSetDict(PyObject *obj, PyObject *value, void *context)
1344{
Serhiy Storchaka576f1322016-01-05 21:27:54 +02001345 PyObject **dictptr = _PyObject_GetDictPtr(obj);
Benjamin Peterson8eb12692012-02-19 19:59:10 -05001346 if (dictptr == NULL) {
1347 PyErr_SetString(PyExc_AttributeError,
1348 "This object has no __dict__");
1349 return -1;
1350 }
1351 if (value == NULL) {
1352 PyErr_SetString(PyExc_TypeError, "cannot delete __dict__");
1353 return -1;
1354 }
1355 if (!PyDict_Check(value)) {
1356 PyErr_Format(PyExc_TypeError,
1357 "__dict__ must be set to a dictionary, "
1358 "not a '%.200s'", Py_TYPE(value)->tp_name);
1359 return -1;
1360 }
Serhiy Storchaka576f1322016-01-05 21:27:54 +02001361 Py_INCREF(value);
Serhiy Storchakaec397562016-04-06 09:50:03 +03001362 Py_XSETREF(*dictptr, value);
Benjamin Peterson8eb12692012-02-19 19:59:10 -05001363 return 0;
1364}
1365
Antoine Pitrou1a9a9d52010-08-28 18:17:03 +00001366
Guido van Rossum6ac258d1993-05-12 08:24:20 +00001367/* Test a value used as condition, e.g., in a for or if statement.
1368 Return -1 if an error occurred */
1369
1370int
Fred Drake100814d2000-07-09 15:48:49 +00001371PyObject_IsTrue(PyObject *v)
Guido van Rossum6ac258d1993-05-12 08:24:20 +00001372{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001373 Py_ssize_t res;
1374 if (v == Py_True)
1375 return 1;
1376 if (v == Py_False)
1377 return 0;
1378 if (v == Py_None)
1379 return 0;
1380 else if (v->ob_type->tp_as_number != NULL &&
1381 v->ob_type->tp_as_number->nb_bool != NULL)
1382 res = (*v->ob_type->tp_as_number->nb_bool)(v);
1383 else if (v->ob_type->tp_as_mapping != NULL &&
1384 v->ob_type->tp_as_mapping->mp_length != NULL)
1385 res = (*v->ob_type->tp_as_mapping->mp_length)(v);
1386 else if (v->ob_type->tp_as_sequence != NULL &&
1387 v->ob_type->tp_as_sequence->sq_length != NULL)
1388 res = (*v->ob_type->tp_as_sequence->sq_length)(v);
1389 else
1390 return 1;
1391 /* if it is negative, it should be either -1 or -2 */
1392 return (res > 0) ? 1 : Py_SAFE_DOWNCAST(res, Py_ssize_t, int);
Guido van Rossum6ac258d1993-05-12 08:24:20 +00001393}
1394
Tim Peters803526b2002-07-07 05:13:56 +00001395/* equivalent of 'not v'
Guido van Rossumc3d3f961998-04-09 17:53:59 +00001396 Return -1 if an error occurred */
1397
1398int
Fred Drake100814d2000-07-09 15:48:49 +00001399PyObject_Not(PyObject *v)
Guido van Rossumc3d3f961998-04-09 17:53:59 +00001400{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001401 int res;
1402 res = PyObject_IsTrue(v);
1403 if (res < 0)
1404 return res;
1405 return res == 0;
Guido van Rossumc3d3f961998-04-09 17:53:59 +00001406}
1407
Guido van Rossum49b11fe1995-01-26 00:38:22 +00001408/* Test whether an object can be called */
1409
1410int
Fred Drake100814d2000-07-09 15:48:49 +00001411PyCallable_Check(PyObject *x)
Guido van Rossum49b11fe1995-01-26 00:38:22 +00001412{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001413 if (x == NULL)
1414 return 0;
1415 return x->ob_type->tp_call != NULL;
Guido van Rossum49b11fe1995-01-26 00:38:22 +00001416}
1417
Tim Peters7eea37e2001-09-04 22:08:56 +00001418
Georg Brandle32b4222007-03-10 22:13:27 +00001419/* Helper for PyObject_Dir without arguments: returns the local scope. */
1420static PyObject *
Guido van Rossumad7d8d12007-04-13 01:39:34 +00001421_dir_locals(void)
Tim Peters305b5852001-09-17 02:38:46 +00001422{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001423 PyObject *names;
Victor Stinner41bb43a2013-10-29 01:19:37 +01001424 PyObject *locals;
Tim Peters305b5852001-09-17 02:38:46 +00001425
Victor Stinner41bb43a2013-10-29 01:19:37 +01001426 locals = PyEval_GetLocals();
1427 if (locals == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001428 return NULL;
Tim Peters305b5852001-09-17 02:38:46 +00001429
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001430 names = PyMapping_Keys(locals);
1431 if (!names)
1432 return NULL;
1433 if (!PyList_Check(names)) {
1434 PyErr_Format(PyExc_TypeError,
1435 "dir(): expected keys() of locals to be a list, "
1436 "not '%.200s'", Py_TYPE(names)->tp_name);
1437 Py_DECREF(names);
1438 return NULL;
1439 }
Benjamin Peterson3bbb7222011-06-11 16:12:08 -05001440 if (PyList_Sort(names)) {
1441 Py_DECREF(names);
1442 return NULL;
1443 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001444 /* the locals don't need to be DECREF'd */
1445 return names;
Georg Brandle32b4222007-03-10 22:13:27 +00001446}
1447
Benjamin Peterson82b00c12011-05-24 11:09:06 -05001448/* Helper for PyObject_Dir: object introspection. */
Georg Brandle32b4222007-03-10 22:13:27 +00001449static PyObject *
1450_dir_object(PyObject *obj)
1451{
Benjamin Peterson3bbb7222011-06-11 16:12:08 -05001452 PyObject *result, *sorted;
Benjamin Petersonce798522012-01-22 11:24:29 -05001453 PyObject *dirfunc = _PyObject_LookupSpecial(obj, &PyId___dir__);
Georg Brandle32b4222007-03-10 22:13:27 +00001454
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001455 assert(obj);
1456 if (dirfunc == NULL) {
Benjamin Peterson82b00c12011-05-24 11:09:06 -05001457 if (!PyErr_Occurred())
1458 PyErr_SetString(PyExc_TypeError, "object does not provide __dir__");
1459 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001460 }
Benjamin Peterson82b00c12011-05-24 11:09:06 -05001461 /* use __dir__ */
Victor Stinnerf17c3de2016-12-06 18:46:19 +01001462 result = _PyObject_CallNoArg(dirfunc);
Benjamin Peterson82b00c12011-05-24 11:09:06 -05001463 Py_DECREF(dirfunc);
1464 if (result == NULL)
1465 return NULL;
Benjamin Peterson3bbb7222011-06-11 16:12:08 -05001466 /* return sorted(result) */
1467 sorted = PySequence_List(result);
1468 Py_DECREF(result);
1469 if (sorted == NULL)
1470 return NULL;
1471 if (PyList_Sort(sorted)) {
1472 Py_DECREF(sorted);
1473 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001474 }
Benjamin Peterson3bbb7222011-06-11 16:12:08 -05001475 return sorted;
Georg Brandle32b4222007-03-10 22:13:27 +00001476}
1477
1478/* Implementation of dir() -- if obj is NULL, returns the names in the current
1479 (local) scope. Otherwise, performs introspection of the object: returns a
1480 sorted list of attribute names (supposedly) accessible from the object
1481*/
1482PyObject *
1483PyObject_Dir(PyObject *obj)
1484{
Benjamin Peterson3bbb7222011-06-11 16:12:08 -05001485 return (obj == NULL) ? _dir_locals() : _dir_object(obj);
Tim Peters7eea37e2001-09-04 22:08:56 +00001486}
Guido van Rossum49b11fe1995-01-26 00:38:22 +00001487
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001488/*
Raymond Hettinger66d2be82011-07-28 09:55:13 -07001489None is a non-NULL undefined value.
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001490There is (and should be!) no way to create other objects of this type,
Guido van Rossum3f5da241990-12-20 15:06:42 +00001491so there is exactly one (which is indestructible, by the way).
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001492*/
1493
Guido van Rossum0c182a11992-03-27 17:26:13 +00001494/* ARGSUSED */
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001495static PyObject *
Fred Drake100814d2000-07-09 15:48:49 +00001496none_repr(PyObject *op)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001497{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001498 return PyUnicode_FromString("None");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001499}
1500
Barry Warsaw9bf16442001-01-23 16:24:35 +00001501/* ARGUSED */
1502static void
Tim Peters803526b2002-07-07 05:13:56 +00001503none_dealloc(PyObject* ignore)
Barry Warsaw9bf16442001-01-23 16:24:35 +00001504{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001505 /* This should never get called, but we also don't want to SEGV if
1506 * we accidentally decref None out of existence.
1507 */
1508 Py_FatalError("deallocating None");
Barry Warsaw9bf16442001-01-23 16:24:35 +00001509}
1510
Benjamin Petersonc4607ae2011-07-29 18:19:43 -05001511static PyObject *
1512none_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
1513{
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +02001514 if (PyTuple_GET_SIZE(args) || (kwargs && PyDict_GET_SIZE(kwargs))) {
Benjamin Petersonc4607ae2011-07-29 18:19:43 -05001515 PyErr_SetString(PyExc_TypeError, "NoneType takes no arguments");
1516 return NULL;
1517 }
1518 Py_RETURN_NONE;
1519}
1520
Raymond Hettinger66d2be82011-07-28 09:55:13 -07001521static int
1522none_bool(PyObject *v)
1523{
1524 return 0;
1525}
1526
1527static PyNumberMethods none_as_number = {
1528 0, /* nb_add */
1529 0, /* nb_subtract */
1530 0, /* nb_multiply */
1531 0, /* nb_remainder */
1532 0, /* nb_divmod */
1533 0, /* nb_power */
1534 0, /* nb_negative */
1535 0, /* nb_positive */
1536 0, /* nb_absolute */
1537 (inquiry)none_bool, /* nb_bool */
1538 0, /* nb_invert */
1539 0, /* nb_lshift */
1540 0, /* nb_rshift */
1541 0, /* nb_and */
1542 0, /* nb_xor */
1543 0, /* nb_or */
1544 0, /* nb_int */
1545 0, /* nb_reserved */
1546 0, /* nb_float */
1547 0, /* nb_inplace_add */
1548 0, /* nb_inplace_subtract */
1549 0, /* nb_inplace_multiply */
1550 0, /* nb_inplace_remainder */
1551 0, /* nb_inplace_power */
1552 0, /* nb_inplace_lshift */
1553 0, /* nb_inplace_rshift */
1554 0, /* nb_inplace_and */
1555 0, /* nb_inplace_xor */
1556 0, /* nb_inplace_or */
1557 0, /* nb_floor_divide */
1558 0, /* nb_true_divide */
1559 0, /* nb_inplace_floor_divide */
1560 0, /* nb_inplace_true_divide */
1561 0, /* nb_index */
1562};
Barry Warsaw9bf16442001-01-23 16:24:35 +00001563
Alexandre Vassalotti65846c62013-11-30 17:55:48 -08001564PyTypeObject _PyNone_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001565 PyVarObject_HEAD_INIT(&PyType_Type, 0)
1566 "NoneType",
1567 0,
1568 0,
1569 none_dealloc, /*tp_dealloc*/ /*never called*/
1570 0, /*tp_print*/
1571 0, /*tp_getattr*/
1572 0, /*tp_setattr*/
1573 0, /*tp_reserved*/
1574 none_repr, /*tp_repr*/
Raymond Hettinger66d2be82011-07-28 09:55:13 -07001575 &none_as_number, /*tp_as_number*/
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001576 0, /*tp_as_sequence*/
1577 0, /*tp_as_mapping*/
1578 0, /*tp_hash */
Benjamin Petersonc4607ae2011-07-29 18:19:43 -05001579 0, /*tp_call */
1580 0, /*tp_str */
1581 0, /*tp_getattro */
1582 0, /*tp_setattro */
1583 0, /*tp_as_buffer */
1584 Py_TPFLAGS_DEFAULT, /*tp_flags */
1585 0, /*tp_doc */
1586 0, /*tp_traverse */
1587 0, /*tp_clear */
1588 0, /*tp_richcompare */
1589 0, /*tp_weaklistoffset */
1590 0, /*tp_iter */
1591 0, /*tp_iternext */
1592 0, /*tp_methods */
1593 0, /*tp_members */
1594 0, /*tp_getset */
1595 0, /*tp_base */
1596 0, /*tp_dict */
1597 0, /*tp_descr_get */
1598 0, /*tp_descr_set */
1599 0, /*tp_dictoffset */
1600 0, /*tp_init */
1601 0, /*tp_alloc */
1602 none_new, /*tp_new */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001603};
1604
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001605PyObject _Py_NoneStruct = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001606 _PyObject_EXTRA_INIT
Alexandre Vassalotti65846c62013-11-30 17:55:48 -08001607 1, &_PyNone_Type
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001608};
1609
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +00001610/* NotImplemented is an object that can be used to signal that an
1611 operation is not implemented for the given type combination. */
1612
1613static PyObject *
1614NotImplemented_repr(PyObject *op)
1615{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001616 return PyUnicode_FromString("NotImplemented");
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +00001617}
1618
Benjamin Peterson18d7d7a2011-07-29 18:27:44 -05001619static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05301620NotImplemented_reduce(PyObject *op, PyObject *Py_UNUSED(ignored))
Alexandre Vassalottic49477b2013-11-24 02:53:45 -08001621{
1622 return PyUnicode_FromString("NotImplemented");
1623}
1624
1625static PyMethodDef notimplemented_methods[] = {
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05301626 {"__reduce__", NotImplemented_reduce, METH_NOARGS, NULL},
Alexandre Vassalottic49477b2013-11-24 02:53:45 -08001627 {NULL, NULL}
1628};
1629
1630static PyObject *
Benjamin Peterson18d7d7a2011-07-29 18:27:44 -05001631notimplemented_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
1632{
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +02001633 if (PyTuple_GET_SIZE(args) || (kwargs && PyDict_GET_SIZE(kwargs))) {
Benjamin Peterson18d7d7a2011-07-29 18:27:44 -05001634 PyErr_SetString(PyExc_TypeError, "NotImplementedType takes no arguments");
1635 return NULL;
1636 }
Brian Curtindfc80e32011-08-10 20:28:54 -05001637 Py_RETURN_NOTIMPLEMENTED;
Benjamin Peterson18d7d7a2011-07-29 18:27:44 -05001638}
1639
Armin Ronacher226b1db2012-10-06 14:28:58 +02001640static void
1641notimplemented_dealloc(PyObject* ignore)
1642{
1643 /* This should never get called, but we also don't want to SEGV if
1644 * we accidentally decref NotImplemented out of existence.
1645 */
1646 Py_FatalError("deallocating NotImplemented");
1647}
1648
Alexandre Vassalotti65846c62013-11-30 17:55:48 -08001649PyTypeObject _PyNotImplemented_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001650 PyVarObject_HEAD_INIT(&PyType_Type, 0)
1651 "NotImplementedType",
1652 0,
1653 0,
Armin Ronacher226b1db2012-10-06 14:28:58 +02001654 notimplemented_dealloc, /*tp_dealloc*/ /*never called*/
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001655 0, /*tp_print*/
1656 0, /*tp_getattr*/
1657 0, /*tp_setattr*/
1658 0, /*tp_reserved*/
1659 NotImplemented_repr, /*tp_repr*/
1660 0, /*tp_as_number*/
1661 0, /*tp_as_sequence*/
1662 0, /*tp_as_mapping*/
1663 0, /*tp_hash */
Benjamin Peterson18d7d7a2011-07-29 18:27:44 -05001664 0, /*tp_call */
1665 0, /*tp_str */
1666 0, /*tp_getattro */
1667 0, /*tp_setattro */
1668 0, /*tp_as_buffer */
1669 Py_TPFLAGS_DEFAULT, /*tp_flags */
1670 0, /*tp_doc */
1671 0, /*tp_traverse */
1672 0, /*tp_clear */
1673 0, /*tp_richcompare */
1674 0, /*tp_weaklistoffset */
1675 0, /*tp_iter */
1676 0, /*tp_iternext */
Alexandre Vassalottic49477b2013-11-24 02:53:45 -08001677 notimplemented_methods, /*tp_methods */
Benjamin Peterson18d7d7a2011-07-29 18:27:44 -05001678 0, /*tp_members */
1679 0, /*tp_getset */
1680 0, /*tp_base */
1681 0, /*tp_dict */
1682 0, /*tp_descr_get */
1683 0, /*tp_descr_set */
1684 0, /*tp_dictoffset */
1685 0, /*tp_init */
1686 0, /*tp_alloc */
1687 notimplemented_new, /*tp_new */
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +00001688};
1689
1690PyObject _Py_NotImplementedStruct = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001691 _PyObject_EXTRA_INIT
Alexandre Vassalotti65846c62013-11-30 17:55:48 -08001692 1, &_PyNotImplemented_Type
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +00001693};
1694
Guido van Rossumba21a492001-08-16 08:17:26 +00001695void
1696_Py_ReadyTypes(void)
1697{
Victor Stinner5a1bb4e2014-06-02 14:10:59 +02001698 if (PyType_Ready(&PyBaseObject_Type) < 0)
1699 Py_FatalError("Can't initialize object type");
1700
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001701 if (PyType_Ready(&PyType_Type) < 0)
1702 Py_FatalError("Can't initialize type type");
Guido van Rossumba21a492001-08-16 08:17:26 +00001703
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001704 if (PyType_Ready(&_PyWeakref_RefType) < 0)
1705 Py_FatalError("Can't initialize weakref type");
Fred Drake0a4dd392004-07-02 18:57:45 +00001706
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001707 if (PyType_Ready(&_PyWeakref_CallableProxyType) < 0)
1708 Py_FatalError("Can't initialize callable weakref proxy type");
Benjamin Petersonfd838e62009-04-20 02:09:13 +00001709
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001710 if (PyType_Ready(&_PyWeakref_ProxyType) < 0)
1711 Py_FatalError("Can't initialize weakref proxy type");
Benjamin Petersonfd838e62009-04-20 02:09:13 +00001712
Victor Stinner5a1bb4e2014-06-02 14:10:59 +02001713 if (PyType_Ready(&PyLong_Type) < 0)
1714 Py_FatalError("Can't initialize int type");
1715
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001716 if (PyType_Ready(&PyBool_Type) < 0)
1717 Py_FatalError("Can't initialize bool type");
Guido van Rossum77f6a652002-04-03 22:41:51 +00001718
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001719 if (PyType_Ready(&PyByteArray_Type) < 0)
1720 Py_FatalError("Can't initialize bytearray type");
Guido van Rossum4dfe8a12006-04-22 23:28:04 +00001721
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001722 if (PyType_Ready(&PyBytes_Type) < 0)
1723 Py_FatalError("Can't initialize 'str'");
Guido van Rossumcacfc072002-05-24 19:01:59 +00001724
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001725 if (PyType_Ready(&PyList_Type) < 0)
1726 Py_FatalError("Can't initialize list type");
Guido van Rossumba21a492001-08-16 08:17:26 +00001727
Alexandre Vassalotti65846c62013-11-30 17:55:48 -08001728 if (PyType_Ready(&_PyNone_Type) < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001729 Py_FatalError("Can't initialize None type");
Guido van Rossumba21a492001-08-16 08:17:26 +00001730
Alexandre Vassalotti65846c62013-11-30 17:55:48 -08001731 if (PyType_Ready(&_PyNotImplemented_Type) < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001732 Py_FatalError("Can't initialize NotImplemented type");
Benjamin Petersonae937c02009-04-18 20:54:08 +00001733
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001734 if (PyType_Ready(&PyTraceBack_Type) < 0)
1735 Py_FatalError("Can't initialize traceback type");
Benjamin Petersonae937c02009-04-18 20:54:08 +00001736
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001737 if (PyType_Ready(&PySuper_Type) < 0)
1738 Py_FatalError("Can't initialize super type");
Benjamin Petersonae937c02009-04-18 20:54:08 +00001739
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001740 if (PyType_Ready(&PyRange_Type) < 0)
1741 Py_FatalError("Can't initialize range type");
Benjamin Petersonae937c02009-04-18 20:54:08 +00001742
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001743 if (PyType_Ready(&PyDict_Type) < 0)
1744 Py_FatalError("Can't initialize dict type");
Benjamin Petersonae937c02009-04-18 20:54:08 +00001745
Benjamin Petersondb87c992016-11-06 13:01:07 -08001746 if (PyType_Ready(&PyDictKeys_Type) < 0)
1747 Py_FatalError("Can't initialize dict keys type");
1748
1749 if (PyType_Ready(&PyDictValues_Type) < 0)
1750 Py_FatalError("Can't initialize dict values type");
1751
1752 if (PyType_Ready(&PyDictItems_Type) < 0)
1753 Py_FatalError("Can't initialize dict items type");
1754
Eric Snow96c6af92015-05-29 22:21:39 -06001755 if (PyType_Ready(&PyODict_Type) < 0)
1756 Py_FatalError("Can't initialize OrderedDict type");
1757
1758 if (PyType_Ready(&PyODictKeys_Type) < 0)
1759 Py_FatalError("Can't initialize odict_keys type");
1760
1761 if (PyType_Ready(&PyODictItems_Type) < 0)
1762 Py_FatalError("Can't initialize odict_items type");
1763
1764 if (PyType_Ready(&PyODictValues_Type) < 0)
1765 Py_FatalError("Can't initialize odict_values type");
1766
1767 if (PyType_Ready(&PyODictIter_Type) < 0)
1768 Py_FatalError("Can't initialize odict_keyiterator type");
1769
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001770 if (PyType_Ready(&PySet_Type) < 0)
1771 Py_FatalError("Can't initialize set type");
Benjamin Petersonae937c02009-04-18 20:54:08 +00001772
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001773 if (PyType_Ready(&PyUnicode_Type) < 0)
1774 Py_FatalError("Can't initialize str type");
Benjamin Petersonae937c02009-04-18 20:54:08 +00001775
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001776 if (PyType_Ready(&PySlice_Type) < 0)
1777 Py_FatalError("Can't initialize slice type");
Benjamin Petersonae937c02009-04-18 20:54:08 +00001778
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001779 if (PyType_Ready(&PyStaticMethod_Type) < 0)
1780 Py_FatalError("Can't initialize static method type");
Benjamin Petersonae937c02009-04-18 20:54:08 +00001781
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001782 if (PyType_Ready(&PyComplex_Type) < 0)
1783 Py_FatalError("Can't initialize complex type");
Skip Montanaroba1e0f42009-10-18 14:25:35 +00001784
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001785 if (PyType_Ready(&PyFloat_Type) < 0)
1786 Py_FatalError("Can't initialize float type");
Benjamin Petersonae937c02009-04-18 20:54:08 +00001787
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001788 if (PyType_Ready(&PyFrozenSet_Type) < 0)
1789 Py_FatalError("Can't initialize frozenset type");
Benjamin Petersonae937c02009-04-18 20:54:08 +00001790
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001791 if (PyType_Ready(&PyProperty_Type) < 0)
1792 Py_FatalError("Can't initialize property type");
Benjamin Petersonae937c02009-04-18 20:54:08 +00001793
Stefan Krah9a2d99e2012-02-25 12:24:21 +01001794 if (PyType_Ready(&_PyManagedBuffer_Type) < 0)
1795 Py_FatalError("Can't initialize managed buffer type");
1796
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001797 if (PyType_Ready(&PyMemoryView_Type) < 0)
1798 Py_FatalError("Can't initialize memoryview type");
Benjamin Petersonae937c02009-04-18 20:54:08 +00001799
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001800 if (PyType_Ready(&PyTuple_Type) < 0)
1801 Py_FatalError("Can't initialize tuple type");
Benjamin Petersonae937c02009-04-18 20:54:08 +00001802
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001803 if (PyType_Ready(&PyEnum_Type) < 0)
1804 Py_FatalError("Can't initialize enumerate type");
Benjamin Petersonae937c02009-04-18 20:54:08 +00001805
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001806 if (PyType_Ready(&PyReversed_Type) < 0)
1807 Py_FatalError("Can't initialize reversed type");
Guido van Rossum47b9ff62006-08-24 00:41:19 +00001808
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001809 if (PyType_Ready(&PyStdPrinter_Type) < 0)
1810 Py_FatalError("Can't initialize StdPrinter");
Benjamin Petersonfd838e62009-04-20 02:09:13 +00001811
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001812 if (PyType_Ready(&PyCode_Type) < 0)
1813 Py_FatalError("Can't initialize code type");
Benjamin Petersonfd838e62009-04-20 02:09:13 +00001814
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001815 if (PyType_Ready(&PyFrame_Type) < 0)
1816 Py_FatalError("Can't initialize frame type");
Benjamin Petersonfd838e62009-04-20 02:09:13 +00001817
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001818 if (PyType_Ready(&PyCFunction_Type) < 0)
1819 Py_FatalError("Can't initialize builtin function type");
Benjamin Petersonfd838e62009-04-20 02:09:13 +00001820
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001821 if (PyType_Ready(&PyMethod_Type) < 0)
1822 Py_FatalError("Can't initialize method type");
Benjamin Petersonfd838e62009-04-20 02:09:13 +00001823
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001824 if (PyType_Ready(&PyFunction_Type) < 0)
1825 Py_FatalError("Can't initialize function type");
Benjamin Petersonfd838e62009-04-20 02:09:13 +00001826
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001827 if (PyType_Ready(&PyDictProxy_Type) < 0)
1828 Py_FatalError("Can't initialize dict proxy type");
Benjamin Petersonfd838e62009-04-20 02:09:13 +00001829
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001830 if (PyType_Ready(&PyGen_Type) < 0)
1831 Py_FatalError("Can't initialize generator type");
Benjamin Petersonfd838e62009-04-20 02:09:13 +00001832
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001833 if (PyType_Ready(&PyGetSetDescr_Type) < 0)
1834 Py_FatalError("Can't initialize get-set descriptor type");
Benjamin Petersonfd838e62009-04-20 02:09:13 +00001835
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001836 if (PyType_Ready(&PyWrapperDescr_Type) < 0)
1837 Py_FatalError("Can't initialize wrapper type");
Benjamin Petersonfd838e62009-04-20 02:09:13 +00001838
Benjamin Petersoneff61f62011-09-01 16:32:31 -04001839 if (PyType_Ready(&_PyMethodWrapper_Type) < 0)
1840 Py_FatalError("Can't initialize method wrapper type");
1841
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001842 if (PyType_Ready(&PyEllipsis_Type) < 0)
1843 Py_FatalError("Can't initialize ellipsis type");
Benjamin Petersonfd838e62009-04-20 02:09:13 +00001844
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001845 if (PyType_Ready(&PyMemberDescr_Type) < 0)
1846 Py_FatalError("Can't initialize member descriptor type");
Benjamin Peterson8bc5b682009-05-09 18:10:51 +00001847
Barry Warsaw409da152012-06-03 16:18:47 -04001848 if (PyType_Ready(&_PyNamespace_Type) < 0)
1849 Py_FatalError("Can't initialize namespace type");
Benjamin Petersone8ea97f2012-10-30 23:27:52 -04001850
Benjamin Petersonc4311282012-10-30 23:21:10 -04001851 if (PyType_Ready(&PyCapsule_Type) < 0)
1852 Py_FatalError("Can't initialize capsule type");
1853
1854 if (PyType_Ready(&PyLongRangeIter_Type) < 0)
1855 Py_FatalError("Can't initialize long range iterator type");
1856
1857 if (PyType_Ready(&PyCell_Type) < 0)
1858 Py_FatalError("Can't initialize cell type");
1859
1860 if (PyType_Ready(&PyInstanceMethod_Type) < 0)
1861 Py_FatalError("Can't initialize instance method type");
1862
1863 if (PyType_Ready(&PyClassMethodDescr_Type) < 0)
1864 Py_FatalError("Can't initialize class method descr type");
1865
1866 if (PyType_Ready(&PyMethodDescr_Type) < 0)
1867 Py_FatalError("Can't initialize method descr type");
1868
1869 if (PyType_Ready(&PyCallIter_Type) < 0)
1870 Py_FatalError("Can't initialize call iter type");
1871
1872 if (PyType_Ready(&PySeqIter_Type) < 0)
1873 Py_FatalError("Can't initialize sequence iterator type");
Yury Selivanov5376ba92015-06-22 12:19:30 -04001874
1875 if (PyType_Ready(&PyCoro_Type) < 0)
1876 Py_FatalError("Can't initialize coroutine type");
1877
1878 if (PyType_Ready(&_PyCoroWrapper_Type) < 0)
1879 Py_FatalError("Can't initialize coroutine wrapper type");
Guido van Rossumba21a492001-08-16 08:17:26 +00001880}
1881
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001882
Guido van Rossum84a90321996-05-22 16:34:47 +00001883#ifdef Py_TRACE_REFS
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001884
Guido van Rossumaacdc9d1996-08-12 21:32:12 +00001885void
Fred Drake100814d2000-07-09 15:48:49 +00001886_Py_NewReference(PyObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001887{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001888 _Py_INC_REFTOTAL;
1889 op->ob_refcnt = 1;
1890 _Py_AddToAllObjects(op, 1);
1891 _Py_INC_TPALLOCS(op);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001892}
1893
Guido van Rossumaacdc9d1996-08-12 21:32:12 +00001894void
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02001895_Py_ForgetReference(PyObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001896{
Guido van Rossumbffd6832000-01-20 22:32:56 +00001897#ifdef SLOW_UNREF_CHECK
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02001898 PyObject *p;
Guido van Rossumbffd6832000-01-20 22:32:56 +00001899#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001900 if (op->ob_refcnt < 0)
1901 Py_FatalError("UNREF negative refcnt");
1902 if (op == &refchain ||
1903 op->_ob_prev->_ob_next != op || op->_ob_next->_ob_prev != op) {
1904 fprintf(stderr, "* ob\n");
1905 _PyObject_Dump(op);
1906 fprintf(stderr, "* op->_ob_prev->_ob_next\n");
1907 _PyObject_Dump(op->_ob_prev->_ob_next);
1908 fprintf(stderr, "* op->_ob_next->_ob_prev\n");
1909 _PyObject_Dump(op->_ob_next->_ob_prev);
1910 Py_FatalError("UNREF invalid object");
1911 }
Guido van Rossum2e8f6141992-09-03 20:32:55 +00001912#ifdef SLOW_UNREF_CHECK
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001913 for (p = refchain._ob_next; p != &refchain; p = p->_ob_next) {
1914 if (p == op)
1915 break;
1916 }
1917 if (p == &refchain) /* Not found */
1918 Py_FatalError("UNREF unknown object");
Guido van Rossum2e8f6141992-09-03 20:32:55 +00001919#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001920 op->_ob_next->_ob_prev = op->_ob_prev;
1921 op->_ob_prev->_ob_next = op->_ob_next;
1922 op->_ob_next = op->_ob_prev = NULL;
1923 _Py_INC_TPFREES(op);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001924}
1925
Guido van Rossumaacdc9d1996-08-12 21:32:12 +00001926void
Fred Drake100814d2000-07-09 15:48:49 +00001927_Py_Dealloc(PyObject *op)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001928{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001929 destructor dealloc = Py_TYPE(op)->tp_dealloc;
1930 _Py_ForgetReference(op);
1931 (*dealloc)(op);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001932}
1933
Tim Peters269b2a62003-04-17 19:52:29 +00001934/* Print all live objects. Because PyObject_Print is called, the
1935 * interpreter must be in a healthy state.
1936 */
Guido van Rossumaacdc9d1996-08-12 21:32:12 +00001937void
Fred Drake100814d2000-07-09 15:48:49 +00001938_Py_PrintReferences(FILE *fp)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001939{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001940 PyObject *op;
1941 fprintf(fp, "Remaining objects:\n");
1942 for (op = refchain._ob_next; op != &refchain; op = op->_ob_next) {
1943 fprintf(fp, "%p [%" PY_FORMAT_SIZE_T "d] ", op, op->ob_refcnt);
1944 if (PyObject_Print(op, fp, 0) != 0)
1945 PyErr_Clear();
1946 putc('\n', fp);
1947 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001948}
1949
Tim Peters269b2a62003-04-17 19:52:29 +00001950/* Print the addresses of all live objects. Unlike _Py_PrintReferences, this
1951 * doesn't make any calls to the Python C API, so is always safe to call.
1952 */
1953void
1954_Py_PrintReferenceAddresses(FILE *fp)
1955{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001956 PyObject *op;
1957 fprintf(fp, "Remaining object addresses:\n");
1958 for (op = refchain._ob_next; op != &refchain; op = op->_ob_next)
1959 fprintf(fp, "%p [%" PY_FORMAT_SIZE_T "d] %s\n", op,
1960 op->ob_refcnt, Py_TYPE(op)->tp_name);
Tim Peters269b2a62003-04-17 19:52:29 +00001961}
1962
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +00001963PyObject *
Fred Drake100814d2000-07-09 15:48:49 +00001964_Py_GetObjects(PyObject *self, PyObject *args)
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +00001965{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001966 int i, n;
1967 PyObject *t = NULL;
1968 PyObject *res, *op;
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +00001969
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001970 if (!PyArg_ParseTuple(args, "i|O", &n, &t))
1971 return NULL;
1972 op = refchain._ob_next;
1973 res = PyList_New(0);
1974 if (res == NULL)
1975 return NULL;
1976 for (i = 0; (n == 0 || i < n) && op != &refchain; i++) {
1977 while (op == self || op == args || op == res || op == t ||
1978 (t != NULL && Py_TYPE(op) != (PyTypeObject *) t)) {
1979 op = op->_ob_next;
1980 if (op == &refchain)
1981 return res;
1982 }
1983 if (PyList_Append(res, op) < 0) {
1984 Py_DECREF(res);
1985 return NULL;
1986 }
1987 op = op->_ob_next;
1988 }
1989 return res;
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +00001990}
1991
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001992#endif
Guido van Rossum97ead3f1996-01-12 01:24:09 +00001993
Benjamin Petersonb173f782009-05-05 22:31:58 +00001994
Guido van Rossum84a90321996-05-22 16:34:47 +00001995/* Hack to force loading of abstract.o */
Martin v. Löwis18e16552006-02-15 17:27:45 +00001996Py_ssize_t (*_Py_abstract_hack)(PyObject *) = PyObject_Size;
Guido van Rossume09fb551997-08-05 02:04:34 +00001997
1998
David Malcolm49526f42012-06-22 14:55:41 -04001999void
2000_PyObject_DebugTypeStats(FILE *out)
2001{
2002 _PyCFunction_DebugMallocStats(out);
2003 _PyDict_DebugMallocStats(out);
2004 _PyFloat_DebugMallocStats(out);
2005 _PyFrame_DebugMallocStats(out);
2006 _PyList_DebugMallocStats(out);
2007 _PyMethod_DebugMallocStats(out);
David Malcolm49526f42012-06-22 14:55:41 -04002008 _PyTuple_DebugMallocStats(out);
2009}
Guido van Rossumb18618d2000-05-03 23:44:39 +00002010
Guido van Rossum86610361998-04-10 22:32:46 +00002011/* These methods are used to control infinite recursion in repr, str, print,
2012 etc. Container objects that may recursively contain themselves,
Martin Panter8d56c022016-05-29 04:13:35 +00002013 e.g. builtin dictionaries and lists, should use Py_ReprEnter() and
Guido van Rossum86610361998-04-10 22:32:46 +00002014 Py_ReprLeave() to avoid infinite recursion.
2015
2016 Py_ReprEnter() returns 0 the first time it is called for a particular
2017 object and 1 every time thereafter. It returns -1 if an exception
2018 occurred. Py_ReprLeave() has no return value.
2019
2020 See dictobject.c and listobject.c for examples of use.
2021*/
2022
Guido van Rossum86610361998-04-10 22:32:46 +00002023int
Fred Drake100814d2000-07-09 15:48:49 +00002024Py_ReprEnter(PyObject *obj)
Guido van Rossum86610361998-04-10 22:32:46 +00002025{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002026 PyObject *dict;
2027 PyObject *list;
2028 Py_ssize_t i;
Guido van Rossum86610361998-04-10 22:32:46 +00002029
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002030 dict = PyThreadState_GetDict();
Antoine Pitrou04d17d32014-03-31 22:04:38 +02002031 /* Ignore a missing thread-state, so that this function can be called
2032 early on startup. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002033 if (dict == NULL)
2034 return 0;
Victor Stinner7a07e452013-11-06 18:57:29 +01002035 list = _PyDict_GetItemId(dict, &PyId_Py_Repr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002036 if (list == NULL) {
2037 list = PyList_New(0);
2038 if (list == NULL)
2039 return -1;
Victor Stinner7a07e452013-11-06 18:57:29 +01002040 if (_PyDict_SetItemId(dict, &PyId_Py_Repr, list) < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002041 return -1;
2042 Py_DECREF(list);
2043 }
2044 i = PyList_GET_SIZE(list);
2045 while (--i >= 0) {
2046 if (PyList_GET_ITEM(list, i) == obj)
2047 return 1;
2048 }
Victor Stinnere901d1f2013-07-17 21:58:41 +02002049 if (PyList_Append(list, obj) < 0)
2050 return -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002051 return 0;
Guido van Rossum86610361998-04-10 22:32:46 +00002052}
2053
2054void
Fred Drake100814d2000-07-09 15:48:49 +00002055Py_ReprLeave(PyObject *obj)
Guido van Rossum86610361998-04-10 22:32:46 +00002056{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002057 PyObject *dict;
2058 PyObject *list;
2059 Py_ssize_t i;
Victor Stinner1b634932013-07-16 22:24:44 +02002060 PyObject *error_type, *error_value, *error_traceback;
2061
2062 PyErr_Fetch(&error_type, &error_value, &error_traceback);
Guido van Rossum86610361998-04-10 22:32:46 +00002063
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002064 dict = PyThreadState_GetDict();
2065 if (dict == NULL)
Victor Stinner1b634932013-07-16 22:24:44 +02002066 goto finally;
2067
Victor Stinner7a07e452013-11-06 18:57:29 +01002068 list = _PyDict_GetItemId(dict, &PyId_Py_Repr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002069 if (list == NULL || !PyList_Check(list))
Victor Stinner1b634932013-07-16 22:24:44 +02002070 goto finally;
2071
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002072 i = PyList_GET_SIZE(list);
2073 /* Count backwards because we always expect obj to be list[-1] */
2074 while (--i >= 0) {
2075 if (PyList_GET_ITEM(list, i) == obj) {
2076 PyList_SetSlice(list, i, i + 1, NULL);
2077 break;
2078 }
2079 }
Victor Stinner1b634932013-07-16 22:24:44 +02002080
2081finally:
2082 /* ignore exceptions because there is no way to report them. */
2083 PyErr_Restore(error_type, error_value, error_traceback);
Guido van Rossum86610361998-04-10 22:32:46 +00002084}
Guido van Rossumd724b232000-03-13 16:01:29 +00002085
Tim Peters803526b2002-07-07 05:13:56 +00002086/* Trashcan support. */
Guido van Rossumd724b232000-03-13 16:01:29 +00002087
Tim Peters803526b2002-07-07 05:13:56 +00002088/* Add op to the _PyTrash_delete_later list. Called when the current
2089 * call-stack depth gets large. op must be a currently untracked gc'ed
2090 * object, with refcount 0. Py_DECREF must already have been called on it.
2091 */
Guido van Rossumd724b232000-03-13 16:01:29 +00002092void
Fred Drake100814d2000-07-09 15:48:49 +00002093_PyTrash_deposit_object(PyObject *op)
Guido van Rossumd724b232000-03-13 16:01:29 +00002094{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002095 assert(PyObject_IS_GC(op));
INADA Naokid8521422018-05-17 11:07:21 +09002096 assert(!_PyObject_GC_IS_TRACKED(op));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002097 assert(op->ob_refcnt == 0);
Eric Snow2ebc5ce2017-09-07 23:51:28 -06002098 _Py_AS_GC(op)->gc.gc_prev = (PyGC_Head *)_PyRuntime.gc.trash_delete_later;
2099 _PyRuntime.gc.trash_delete_later = op;
Guido van Rossumd724b232000-03-13 16:01:29 +00002100}
2101
Antoine Pitrou2b0218a2012-09-06 00:59:49 +02002102/* The equivalent API, using per-thread state recursion info */
2103void
2104_PyTrash_thread_deposit_object(PyObject *op)
2105{
2106 PyThreadState *tstate = PyThreadState_GET();
2107 assert(PyObject_IS_GC(op));
INADA Naokid8521422018-05-17 11:07:21 +09002108 assert(!_PyObject_GC_IS_TRACKED(op));
Antoine Pitrou2b0218a2012-09-06 00:59:49 +02002109 assert(op->ob_refcnt == 0);
2110 _Py_AS_GC(op)->gc.gc_prev = (PyGC_Head *) tstate->trash_delete_later;
2111 tstate->trash_delete_later = op;
2112}
2113
Tim Peters803526b2002-07-07 05:13:56 +00002114/* Dealloccate all the objects in the _PyTrash_delete_later list. Called when
2115 * the call-stack unwinds again.
2116 */
Guido van Rossumd724b232000-03-13 16:01:29 +00002117void
Fred Drake100814d2000-07-09 15:48:49 +00002118_PyTrash_destroy_chain(void)
Guido van Rossumd724b232000-03-13 16:01:29 +00002119{
Eric Snow2ebc5ce2017-09-07 23:51:28 -06002120 while (_PyRuntime.gc.trash_delete_later) {
2121 PyObject *op = _PyRuntime.gc.trash_delete_later;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002122 destructor dealloc = Py_TYPE(op)->tp_dealloc;
Neil Schemenauerf589c052002-03-29 03:05:54 +00002123
Eric Snow2ebc5ce2017-09-07 23:51:28 -06002124 _PyRuntime.gc.trash_delete_later =
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002125 (PyObject*) _Py_AS_GC(op)->gc.gc_prev;
Neil Schemenauerf589c052002-03-29 03:05:54 +00002126
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002127 /* Call the deallocator directly. This used to try to
2128 * fool Py_DECREF into calling it indirectly, but
2129 * Py_DECREF was already called on this object, and in
2130 * assorted non-release builds calling Py_DECREF again ends
2131 * up distorting allocation statistics.
2132 */
2133 assert(op->ob_refcnt == 0);
Eric Snow2ebc5ce2017-09-07 23:51:28 -06002134 ++_PyRuntime.gc.trash_delete_nesting;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002135 (*dealloc)(op);
Eric Snow2ebc5ce2017-09-07 23:51:28 -06002136 --_PyRuntime.gc.trash_delete_nesting;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002137 }
Guido van Rossumd724b232000-03-13 16:01:29 +00002138}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002139
Antoine Pitrou2b0218a2012-09-06 00:59:49 +02002140/* The equivalent API, using per-thread state recursion info */
2141void
2142_PyTrash_thread_destroy_chain(void)
2143{
2144 PyThreadState *tstate = PyThreadState_GET();
Xiang Zhanga66f9c62017-05-13 13:36:14 +08002145 /* We need to increase trash_delete_nesting here, otherwise,
2146 _PyTrash_thread_destroy_chain will be called recursively
2147 and then possibly crash. An example that may crash without
2148 increase:
2149 N = 500000 # need to be large enough
2150 ob = object()
2151 tups = [(ob,) for i in range(N)]
2152 for i in range(49):
2153 tups = [(tup,) for tup in tups]
2154 del tups
2155 */
2156 assert(tstate->trash_delete_nesting == 0);
2157 ++tstate->trash_delete_nesting;
Antoine Pitrou2b0218a2012-09-06 00:59:49 +02002158 while (tstate->trash_delete_later) {
2159 PyObject *op = tstate->trash_delete_later;
2160 destructor dealloc = Py_TYPE(op)->tp_dealloc;
2161
2162 tstate->trash_delete_later =
2163 (PyObject*) _Py_AS_GC(op)->gc.gc_prev;
2164
2165 /* Call the deallocator directly. This used to try to
2166 * fool Py_DECREF into calling it indirectly, but
2167 * Py_DECREF was already called on this object, and in
2168 * assorted non-release builds calling Py_DECREF again ends
2169 * up distorting allocation statistics.
2170 */
2171 assert(op->ob_refcnt == 0);
Antoine Pitrou2b0218a2012-09-06 00:59:49 +02002172 (*dealloc)(op);
Xiang Zhanga66f9c62017-05-13 13:36:14 +08002173 assert(tstate->trash_delete_nesting == 1);
Antoine Pitrou2b0218a2012-09-06 00:59:49 +02002174 }
Xiang Zhanga66f9c62017-05-13 13:36:14 +08002175 --tstate->trash_delete_nesting;
Antoine Pitrou2b0218a2012-09-06 00:59:49 +02002176}
2177
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00002178#ifndef Py_TRACE_REFS
2179/* For Py_LIMITED_API, we need an out-of-line version of _Py_Dealloc.
2180 Define this here, so we can undefine the macro. */
2181#undef _Py_Dealloc
2182PyAPI_FUNC(void) _Py_Dealloc(PyObject *);
2183void
2184_Py_Dealloc(PyObject *op)
2185{
2186 _Py_INC_TPFREES(op) _Py_COUNT_ALLOCS_COMMA
2187 (*Py_TYPE(op)->tp_dealloc)(op);
2188}
2189#endif
2190
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002191#ifdef __cplusplus
2192}
2193#endif