blob: 674180d7203f084af0c61fe2cad0fa6f78c49f70 [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001
Benjamin Peterson722954a2011-06-11 16:33:35 -05002/* Generic object operations; and implementation of None */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003
Guido van Rossumc0b618a1997-05-02 03:12:38 +00004#include "Python.h"
Eric Snow2ebc5ce2017-09-07 23:51:28 -06005#include "internal/pystate.h"
Benjamin Petersonfd838e62009-04-20 02:09:13 +00006#include "frameobject.h"
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00007
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00008#ifdef __cplusplus
9extern "C" {
10#endif
11
Victor Stinnerbd303c12013-11-07 23:07:29 +010012_Py_IDENTIFIER(Py_Repr);
13_Py_IDENTIFIER(__bytes__);
14_Py_IDENTIFIER(__dir__);
15_Py_IDENTIFIER(__isabstractmethod__);
16_Py_IDENTIFIER(builtins);
17
Tim Peters34592512002-07-11 06:23:50 +000018#ifdef Py_REF_DEBUG
Neal Norwitz84632ee2006-03-04 20:00:59 +000019Py_ssize_t _Py_RefTotal;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000020
21Py_ssize_t
22_Py_GetRefTotal(void)
23{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000024 PyObject *o;
25 Py_ssize_t total = _Py_RefTotal;
Antoine Pitrou9d952542013-08-24 21:07:07 +020026 o = _PySet_Dummy;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000027 if (o != NULL)
28 total -= o->ob_refcnt;
29 return total;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000030}
Nick Coghland6009512014-11-20 21:39:37 +100031
32void
33_PyDebug_PrintTotalRefs(void) {
Eric Snowdae02762017-09-14 00:35:58 -070034 fprintf(stderr,
35 "[%" PY_FORMAT_SIZE_T "d refs, "
36 "%" PY_FORMAT_SIZE_T "d blocks]\n",
37 _Py_GetRefTotal(), _Py_GetAllocatedBlocks());
Nick Coghland6009512014-11-20 21:39:37 +100038}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000039#endif /* Py_REF_DEBUG */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000040
Guido van Rossum3f5da241990-12-20 15:06:42 +000041/* Object allocation routines used by NEWOBJ and NEWVAROBJ macros.
42 These are used by the individual routines for object creation.
43 Do not call them otherwise, they do not initialize the object! */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000044
Tim Peters78be7992003-03-23 02:51:01 +000045#ifdef Py_TRACE_REFS
Tim Peters7571a0f2003-03-23 17:52:28 +000046/* Head of circular doubly-linked list of all objects. These are linked
47 * together via the _ob_prev and _ob_next members of a PyObject, which
48 * exist only in a Py_TRACE_REFS build.
49 */
Tim Peters78be7992003-03-23 02:51:01 +000050static PyObject refchain = {&refchain, &refchain};
Tim Peters36eb4df2003-03-23 03:33:13 +000051
Tim Peters7571a0f2003-03-23 17:52:28 +000052/* Insert op at the front of the list of all objects. If force is true,
53 * op is added even if _ob_prev and _ob_next are non-NULL already. If
54 * force is false amd _ob_prev or _ob_next are non-NULL, do nothing.
55 * force should be true if and only if op points to freshly allocated,
56 * uninitialized memory, or you've unlinked op from the list and are
Tim Peters51f8d382003-03-23 18:06:08 +000057 * relinking it into the front.
Tim Peters7571a0f2003-03-23 17:52:28 +000058 * Note that objects are normally added to the list via _Py_NewReference,
59 * which is called by PyObject_Init. Not all objects are initialized that
60 * way, though; exceptions include statically allocated type objects, and
61 * statically allocated singletons (like Py_True and Py_None).
62 */
Tim Peters36eb4df2003-03-23 03:33:13 +000063void
Tim Peters7571a0f2003-03-23 17:52:28 +000064_Py_AddToAllObjects(PyObject *op, int force)
Tim Peters36eb4df2003-03-23 03:33:13 +000065{
Tim Peters7571a0f2003-03-23 17:52:28 +000066#ifdef Py_DEBUG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000067 if (!force) {
68 /* If it's initialized memory, op must be in or out of
69 * the list unambiguously.
70 */
71 assert((op->_ob_prev == NULL) == (op->_ob_next == NULL));
72 }
Tim Peters78be7992003-03-23 02:51:01 +000073#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000074 if (force || op->_ob_prev == NULL) {
75 op->_ob_next = refchain._ob_next;
76 op->_ob_prev = &refchain;
77 refchain._ob_next->_ob_prev = op;
78 refchain._ob_next = op;
79 }
Tim Peters7571a0f2003-03-23 17:52:28 +000080}
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000081#endif /* Py_TRACE_REFS */
Tim Peters78be7992003-03-23 02:51:01 +000082
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +000083#ifdef COUNT_ALLOCS
Guido van Rossumc0b618a1997-05-02 03:12:38 +000084static PyTypeObject *type_list;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000085/* All types are added to type_list, at least when
86 they get one object created. That makes them
87 immortal, which unfortunately contributes to
88 garbage itself. If unlist_types_without_objects
89 is set, they will be removed from the type_list
90 once the last object is deallocated. */
Benjamin Petersona4a37fe2009-01-11 17:13:55 +000091static int unlist_types_without_objects;
92extern Py_ssize_t tuple_zero_allocs, fast_tuple_allocs;
93extern Py_ssize_t quick_int_allocs, quick_neg_int_allocs;
94extern Py_ssize_t null_strings, one_strings;
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +000095void
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000096dump_counts(FILE* f)
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +000097{
Victor Stinner25420fe2017-11-20 18:12:22 -080098 PyInterpreterState *interp = PyThreadState_GET()->interp;
99 if (!inter->core_config.show_alloc_count) {
Serhiy Storchaka7e160ce2016-07-03 21:03:53 +0300100 return;
Victor Stinner25420fe2017-11-20 18:12:22 -0800101 }
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +0000102
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000103 for (tp = type_list; tp; tp = tp->tp_next)
104 fprintf(f, "%s alloc'd: %" PY_FORMAT_SIZE_T "d, "
105 "freed: %" PY_FORMAT_SIZE_T "d, "
106 "max in use: %" PY_FORMAT_SIZE_T "d\n",
107 tp->tp_name, tp->tp_allocs, tp->tp_frees,
108 tp->tp_maxalloc);
109 fprintf(f, "fast tuple allocs: %" PY_FORMAT_SIZE_T "d, "
110 "empty: %" PY_FORMAT_SIZE_T "d\n",
111 fast_tuple_allocs, tuple_zero_allocs);
112 fprintf(f, "fast int allocs: pos: %" PY_FORMAT_SIZE_T "d, "
113 "neg: %" PY_FORMAT_SIZE_T "d\n",
114 quick_int_allocs, quick_neg_int_allocs);
115 fprintf(f, "null strings: %" PY_FORMAT_SIZE_T "d, "
116 "1-strings: %" PY_FORMAT_SIZE_T "d\n",
117 null_strings, one_strings);
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +0000118}
119
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000120PyObject *
Fred Drake100814d2000-07-09 15:48:49 +0000121get_counts(void)
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000122{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000123 PyTypeObject *tp;
124 PyObject *result;
125 PyObject *v;
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000126
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000127 result = PyList_New(0);
128 if (result == NULL)
129 return NULL;
130 for (tp = type_list; tp; tp = tp->tp_next) {
131 v = Py_BuildValue("(snnn)", tp->tp_name, tp->tp_allocs,
132 tp->tp_frees, tp->tp_maxalloc);
133 if (v == NULL) {
134 Py_DECREF(result);
135 return NULL;
136 }
137 if (PyList_Append(result, v) < 0) {
138 Py_DECREF(v);
139 Py_DECREF(result);
140 return NULL;
141 }
142 Py_DECREF(v);
143 }
144 return result;
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000145}
146
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +0000147void
Fred Drake100814d2000-07-09 15:48:49 +0000148inc_count(PyTypeObject *tp)
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +0000149{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000150 if (tp->tp_next == NULL && tp->tp_prev == NULL) {
151 /* first time; insert in linked list */
152 if (tp->tp_next != NULL) /* sanity check */
153 Py_FatalError("XXX inc_count sanity check");
154 if (type_list)
155 type_list->tp_prev = tp;
156 tp->tp_next = type_list;
157 /* Note that as of Python 2.2, heap-allocated type objects
158 * can go away, but this code requires that they stay alive
159 * until program exit. That's why we're careful with
160 * refcounts here. type_list gets a new reference to tp,
161 * while ownership of the reference type_list used to hold
162 * (if any) was transferred to tp->tp_next in the line above.
163 * tp is thus effectively immortal after this.
164 */
165 Py_INCREF(tp);
166 type_list = tp;
Tim Peters3e40c7f2003-03-23 03:04:32 +0000167#ifdef Py_TRACE_REFS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000168 /* Also insert in the doubly-linked list of all objects,
169 * if not already there.
170 */
171 _Py_AddToAllObjects((PyObject *)tp, 0);
Tim Peters78be7992003-03-23 02:51:01 +0000172#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000173 }
174 tp->tp_allocs++;
175 if (tp->tp_allocs - tp->tp_frees > tp->tp_maxalloc)
176 tp->tp_maxalloc = tp->tp_allocs - tp->tp_frees;
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +0000177}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000178
179void dec_count(PyTypeObject *tp)
180{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000181 tp->tp_frees++;
182 if (unlist_types_without_objects &&
183 tp->tp_allocs == tp->tp_frees) {
184 /* unlink the type from type_list */
185 if (tp->tp_prev)
186 tp->tp_prev->tp_next = tp->tp_next;
187 else
188 type_list = tp->tp_next;
189 if (tp->tp_next)
190 tp->tp_next->tp_prev = tp->tp_prev;
191 tp->tp_next = tp->tp_prev = NULL;
192 Py_DECREF(tp);
193 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000194}
195
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +0000196#endif
197
Tim Peters7c321a82002-07-09 02:57:01 +0000198#ifdef Py_REF_DEBUG
199/* Log a fatal error; doesn't return. */
200void
201_Py_NegativeRefcount(const char *fname, int lineno, PyObject *op)
202{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000203 char buf[300];
Tim Peters7c321a82002-07-09 02:57:01 +0000204
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000205 PyOS_snprintf(buf, sizeof(buf),
206 "%s:%i object at %p has negative ref count "
207 "%" PY_FORMAT_SIZE_T "d",
208 fname, lineno, op, op->ob_refcnt);
209 Py_FatalError(buf);
Tim Peters7c321a82002-07-09 02:57:01 +0000210}
211
212#endif /* Py_REF_DEBUG */
213
Thomas Heller1328b522004-04-22 17:23:49 +0000214void
215Py_IncRef(PyObject *o)
216{
217 Py_XINCREF(o);
218}
219
220void
221Py_DecRef(PyObject *o)
222{
223 Py_XDECREF(o);
224}
225
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000226PyObject *
Fred Drake100814d2000-07-09 15:48:49 +0000227PyObject_Init(PyObject *op, PyTypeObject *tp)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000228{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000229 if (op == NULL)
230 return PyErr_NoMemory();
231 /* Any changes should be reflected in PyObject_INIT (objimpl.h) */
232 Py_TYPE(op) = tp;
233 _Py_NewReference(op);
234 return op;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000235}
236
Guido van Rossumb18618d2000-05-03 23:44:39 +0000237PyVarObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000238PyObject_InitVar(PyVarObject *op, PyTypeObject *tp, Py_ssize_t size)
Guido van Rossumb18618d2000-05-03 23:44:39 +0000239{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000240 if (op == NULL)
241 return (PyVarObject *) PyErr_NoMemory();
242 /* Any changes should be reflected in PyObject_INIT_VAR */
243 op->ob_size = size;
244 Py_TYPE(op) = tp;
245 _Py_NewReference((PyObject *)op);
246 return op;
Guido van Rossumb18618d2000-05-03 23:44:39 +0000247}
248
249PyObject *
Fred Drake100814d2000-07-09 15:48:49 +0000250_PyObject_New(PyTypeObject *tp)
Guido van Rossumb18618d2000-05-03 23:44:39 +0000251{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000252 PyObject *op;
253 op = (PyObject *) PyObject_MALLOC(_PyObject_SIZE(tp));
254 if (op == NULL)
255 return PyErr_NoMemory();
256 return PyObject_INIT(op, tp);
Guido van Rossumb18618d2000-05-03 23:44:39 +0000257}
258
Guido van Rossumd0c87ee1997-05-15 21:31:03 +0000259PyVarObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000260_PyObject_NewVar(PyTypeObject *tp, Py_ssize_t nitems)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000261{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000262 PyVarObject *op;
263 const size_t size = _PyObject_VAR_SIZE(tp, nitems);
264 op = (PyVarObject *) PyObject_MALLOC(size);
265 if (op == NULL)
266 return (PyVarObject *)PyErr_NoMemory();
267 return PyObject_INIT_VAR(op, tp, nitems);
Guido van Rossumb18618d2000-05-03 23:44:39 +0000268}
269
Antoine Pitrou796564c2013-07-30 19:59:21 +0200270void
271PyObject_CallFinalizer(PyObject *self)
272{
273 PyTypeObject *tp = Py_TYPE(self);
274
275 /* The former could happen on heaptypes created from the C API, e.g.
276 PyType_FromSpec(). */
277 if (!PyType_HasFeature(tp, Py_TPFLAGS_HAVE_FINALIZE) ||
278 tp->tp_finalize == NULL)
279 return;
280 /* tp_finalize should only be called once. */
281 if (PyType_IS_GC(tp) && _PyGC_FINALIZED(self))
282 return;
283
284 tp->tp_finalize(self);
285 if (PyType_IS_GC(tp))
286 _PyGC_SET_FINALIZED(self, 1);
287}
288
289int
290PyObject_CallFinalizerFromDealloc(PyObject *self)
291{
292 Py_ssize_t refcnt;
293
294 /* Temporarily resurrect the object. */
295 if (self->ob_refcnt != 0) {
296 Py_FatalError("PyObject_CallFinalizerFromDealloc called on "
297 "object with a non-zero refcount");
298 }
299 self->ob_refcnt = 1;
300
301 PyObject_CallFinalizer(self);
302
303 /* Undo the temporary resurrection; can't use DECREF here, it would
304 * cause a recursive call.
305 */
306 assert(self->ob_refcnt > 0);
307 if (--self->ob_refcnt == 0)
308 return 0; /* this is the normal path out */
309
310 /* tp_finalize resurrected it! Make it look like the original Py_DECREF
311 * never happened.
312 */
313 refcnt = self->ob_refcnt;
314 _Py_NewReference(self);
315 self->ob_refcnt = refcnt;
316
317 if (PyType_IS_GC(Py_TYPE(self))) {
318 assert(_PyGC_REFS(self) != _PyGC_REFS_UNTRACKED);
319 }
320 /* 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
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000466 res = (*v->ob_type->tp_repr)(v);
Victor Stinner0a54cf12011-12-01 03:22:44 +0100467 if (res == NULL)
468 return NULL;
469 if (!PyUnicode_Check(res)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000470 PyErr_Format(PyExc_TypeError,
471 "__repr__ returned non-string (type %.200s)",
472 res->ob_type->tp_name);
473 Py_DECREF(res);
474 return NULL;
475 }
Victor Stinnerdb88ae52011-12-01 02:15:00 +0100476#ifndef Py_DEBUG
477 if (PyUnicode_READY(res) < 0)
478 return NULL;
479#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000480 return res;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000481}
482
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000483PyObject *
Guido van Rossum98297ee2007-11-06 21:34:58 +0000484PyObject_Str(PyObject *v)
Guido van Rossumc6004111993-11-05 10:22:19 +0000485{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000486 PyObject *res;
487 if (PyErr_CheckSignals())
488 return NULL;
Guido van Rossum98297ee2007-11-06 21:34:58 +0000489#ifdef USE_STACKCHECK
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000490 if (PyOS_CheckStack()) {
491 PyErr_SetString(PyExc_MemoryError, "stack overflow");
492 return NULL;
493 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000494#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000495 if (v == NULL)
496 return PyUnicode_FromString("<NULL>");
497 if (PyUnicode_CheckExact(v)) {
Victor Stinnerdb88ae52011-12-01 02:15:00 +0100498#ifndef Py_DEBUG
Victor Stinner4ead7c72011-11-20 19:48:36 +0100499 if (PyUnicode_READY(v) < 0)
500 return NULL;
Victor Stinnerdb88ae52011-12-01 02:15:00 +0100501#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000502 Py_INCREF(v);
503 return v;
504 }
505 if (Py_TYPE(v)->tp_str == NULL)
506 return PyObject_Repr(v);
Guido van Rossum4f288ab2001-05-01 16:53:37 +0000507
Victor Stinner33824f62013-08-26 14:05:19 +0200508#ifdef Py_DEBUG
509 /* PyObject_Str() must not be called with an exception set,
Victor Stinnera8cb5152017-01-18 14:12:51 +0100510 because it can clear it (directly or indirectly) and so the
Nick Coghland979e432014-02-09 10:43:21 +1000511 caller loses its exception */
Victor Stinner33824f62013-08-26 14:05:19 +0200512 assert(!PyErr_Occurred());
513#endif
514
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000515 /* It is possible for a type to have a tp_str representation that loops
516 infinitely. */
517 if (Py_EnterRecursiveCall(" while getting the str of an object"))
518 return NULL;
519 res = (*Py_TYPE(v)->tp_str)(v);
520 Py_LeaveRecursiveCall();
521 if (res == NULL)
522 return NULL;
523 if (!PyUnicode_Check(res)) {
524 PyErr_Format(PyExc_TypeError,
525 "__str__ returned non-string (type %.200s)",
526 Py_TYPE(res)->tp_name);
527 Py_DECREF(res);
528 return NULL;
529 }
Victor Stinnerdb88ae52011-12-01 02:15:00 +0100530#ifndef Py_DEBUG
Victor Stinner4ead7c72011-11-20 19:48:36 +0100531 if (PyUnicode_READY(res) < 0)
532 return NULL;
Victor Stinnerdb88ae52011-12-01 02:15:00 +0100533#endif
Victor Stinner4ead7c72011-11-20 19:48:36 +0100534 assert(_PyUnicode_CheckConsistency(res, 1));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000535 return res;
Neil Schemenauercf52c072005-08-12 17:34:58 +0000536}
537
Georg Brandl559e5d72008-06-11 18:37:52 +0000538PyObject *
539PyObject_ASCII(PyObject *v)
540{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000541 PyObject *repr, *ascii, *res;
Georg Brandl559e5d72008-06-11 18:37:52 +0000542
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000543 repr = PyObject_Repr(v);
544 if (repr == NULL)
545 return NULL;
Georg Brandl559e5d72008-06-11 18:37:52 +0000546
Victor Stinneraf037572013-04-14 18:44:10 +0200547 if (PyUnicode_IS_ASCII(repr))
548 return repr;
549
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000550 /* repr is guaranteed to be a PyUnicode object by PyObject_Repr */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200551 ascii = _PyUnicode_AsASCIIString(repr, "backslashreplace");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000552 Py_DECREF(repr);
553 if (ascii == NULL)
554 return NULL;
Georg Brandl559e5d72008-06-11 18:37:52 +0000555
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000556 res = PyUnicode_DecodeASCII(
557 PyBytes_AS_STRING(ascii),
558 PyBytes_GET_SIZE(ascii),
559 NULL);
560
561 Py_DECREF(ascii);
562 return res;
Georg Brandl559e5d72008-06-11 18:37:52 +0000563}
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000564
Benjamin Petersonc15a0732008-08-26 16:46:47 +0000565PyObject *
566PyObject_Bytes(PyObject *v)
567{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000568 PyObject *result, *func;
Benjamin Petersonc15a0732008-08-26 16:46:47 +0000569
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000570 if (v == NULL)
571 return PyBytes_FromString("<NULL>");
Benjamin Petersonc15a0732008-08-26 16:46:47 +0000572
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000573 if (PyBytes_CheckExact(v)) {
574 Py_INCREF(v);
575 return v;
576 }
Benjamin Petersonc15a0732008-08-26 16:46:47 +0000577
Benjamin Petersonce798522012-01-22 11:24:29 -0500578 func = _PyObject_LookupSpecial(v, &PyId___bytes__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000579 if (func != NULL) {
Victor Stinnerf17c3de2016-12-06 18:46:19 +0100580 result = _PyObject_CallNoArg(func);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000581 Py_DECREF(func);
582 if (result == NULL)
Benjamin Peterson41ece392010-09-11 16:39:57 +0000583 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000584 if (!PyBytes_Check(result)) {
Benjamin Peterson41ece392010-09-11 16:39:57 +0000585 PyErr_Format(PyExc_TypeError,
586 "__bytes__ returned non-bytes (type %.200s)",
587 Py_TYPE(result)->tp_name);
588 Py_DECREF(result);
589 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000590 }
591 return result;
592 }
593 else if (PyErr_Occurred())
594 return NULL;
595 return PyBytes_FromObject(v);
Benjamin Petersonc15a0732008-08-26 16:46:47 +0000596}
597
Mark Dickinsonc008a172009-02-01 13:59:22 +0000598/* For Python 3.0.1 and later, the old three-way comparison has been
599 completely removed in favour of rich comparisons. PyObject_Compare() and
600 PyObject_Cmp() are gone, and the builtin cmp function no longer exists.
Mark Dickinsone94c6792009-02-02 20:36:42 +0000601 The old tp_compare slot has been renamed to tp_reserved, and should no
Mark Dickinsonc008a172009-02-01 13:59:22 +0000602 longer be used. Use tp_richcompare instead.
Guido van Rossum98297ee2007-11-06 21:34:58 +0000603
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000604 See (*) below for practical amendments.
605
Mark Dickinsonc008a172009-02-01 13:59:22 +0000606 tp_richcompare gets called with a first argument of the appropriate type
607 and a second object of an arbitrary type. We never do any kind of
608 coercion.
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000609
Mark Dickinsonc008a172009-02-01 13:59:22 +0000610 The tp_richcompare slot should return an object, as follows:
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000611
612 NULL if an exception occurred
613 NotImplemented if the requested comparison is not implemented
614 any other false value if the requested comparison is false
615 any other true value if the requested comparison is true
616
617 The PyObject_RichCompare[Bool]() wrappers raise TypeError when they get
618 NotImplemented.
619
620 (*) Practical amendments:
621
622 - If rich comparison returns NotImplemented, == and != are decided by
623 comparing the object pointer (i.e. falling back to the base object
624 implementation).
625
Guido van Rossuma4073002002-05-31 20:03:54 +0000626*/
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000627
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000628/* Map rich comparison operators to their swapped version, e.g. LT <--> GT */
Brett Cannona5ca2e72004-09-25 01:37:24 +0000629int _Py_SwappedOp[] = {Py_GT, Py_GE, Py_EQ, Py_NE, Py_LT, Py_LE};
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +0000630
Serhiy Storchaka2d06e842015-12-25 19:53:18 +0200631static const char * const opstrings[] = {"<", "<=", "==", "!=", ">", ">="};
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000632
633/* Perform a rich comparison, raising TypeError when the requested comparison
634 operator is not supported. */
Guido van Rossume797ec12001-01-17 15:24:28 +0000635static PyObject *
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000636do_richcompare(PyObject *v, PyObject *w, int op)
Guido van Rossume797ec12001-01-17 15:24:28 +0000637{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000638 richcmpfunc f;
639 PyObject *res;
640 int checked_reverse_op = 0;
Guido van Rossume797ec12001-01-17 15:24:28 +0000641
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000642 if (v->ob_type != w->ob_type &&
643 PyType_IsSubtype(w->ob_type, v->ob_type) &&
644 (f = w->ob_type->tp_richcompare) != NULL) {
645 checked_reverse_op = 1;
646 res = (*f)(w, v, _Py_SwappedOp[op]);
647 if (res != Py_NotImplemented)
648 return res;
649 Py_DECREF(res);
650 }
651 if ((f = v->ob_type->tp_richcompare) != NULL) {
652 res = (*f)(v, w, op);
653 if (res != Py_NotImplemented)
654 return res;
655 Py_DECREF(res);
656 }
657 if (!checked_reverse_op && (f = w->ob_type->tp_richcompare) != NULL) {
658 res = (*f)(w, v, _Py_SwappedOp[op]);
659 if (res != Py_NotImplemented)
660 return res;
661 Py_DECREF(res);
662 }
663 /* If neither object implements it, provide a sensible default
664 for == and !=, but raise an exception for ordering. */
665 switch (op) {
666 case Py_EQ:
667 res = (v == w) ? Py_True : Py_False;
668 break;
669 case Py_NE:
670 res = (v != w) ? Py_True : Py_False;
671 break;
672 default:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000673 PyErr_Format(PyExc_TypeError,
Victor Stinner91108f02015-10-14 18:25:31 +0200674 "'%s' not supported between instances of '%.100s' and '%.100s'",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000675 opstrings[op],
Victor Stinner91108f02015-10-14 18:25:31 +0200676 v->ob_type->tp_name,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000677 w->ob_type->tp_name);
678 return NULL;
679 }
680 Py_INCREF(res);
681 return res;
Guido van Rossume797ec12001-01-17 15:24:28 +0000682}
683
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000684/* Perform a rich comparison with object result. This wraps do_richcompare()
685 with a check for NULL arguments and a recursion check. */
Guido van Rossume797ec12001-01-17 15:24:28 +0000686
Guido van Rossume797ec12001-01-17 15:24:28 +0000687PyObject *
688PyObject_RichCompare(PyObject *v, PyObject *w, int op)
689{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000690 PyObject *res;
Guido van Rossume797ec12001-01-17 15:24:28 +0000691
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000692 assert(Py_LT <= op && op <= Py_GE);
693 if (v == NULL || w == NULL) {
694 if (!PyErr_Occurred())
695 PyErr_BadInternalCall();
696 return NULL;
697 }
698 if (Py_EnterRecursiveCall(" in comparison"))
699 return NULL;
700 res = do_richcompare(v, w, op);
701 Py_LeaveRecursiveCall();
702 return res;
Guido van Rossume797ec12001-01-17 15:24:28 +0000703}
704
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000705/* Perform a rich comparison with integer result. This wraps
706 PyObject_RichCompare(), returning -1 for error, 0 for false, 1 for true. */
Guido van Rossume797ec12001-01-17 15:24:28 +0000707int
708PyObject_RichCompareBool(PyObject *v, PyObject *w, int op)
709{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000710 PyObject *res;
711 int ok;
Guido van Rossume797ec12001-01-17 15:24:28 +0000712
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000713 /* Quick result when objects are the same.
714 Guarantees that identity implies equality. */
715 if (v == w) {
716 if (op == Py_EQ)
717 return 1;
718 else if (op == Py_NE)
719 return 0;
720 }
Mark Dickinson4a1f5932008-11-12 23:23:36 +0000721
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000722 res = PyObject_RichCompare(v, w, op);
723 if (res == NULL)
724 return -1;
725 if (PyBool_Check(res))
726 ok = (res == Py_True);
727 else
728 ok = PyObject_IsTrue(res);
729 Py_DECREF(res);
730 return ok;
Guido van Rossume797ec12001-01-17 15:24:28 +0000731}
Fred Drake13634cf2000-06-29 19:17:04 +0000732
Antoine Pitrouce4a9da2011-11-21 20:46:33 +0100733Py_hash_t
Nick Coghland1abd252008-07-15 15:46:38 +0000734PyObject_HashNotImplemented(PyObject *v)
735{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000736 PyErr_Format(PyExc_TypeError, "unhashable type: '%.200s'",
737 Py_TYPE(v)->tp_name);
738 return -1;
Nick Coghland1abd252008-07-15 15:46:38 +0000739}
Fred Drake13634cf2000-06-29 19:17:04 +0000740
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000741Py_hash_t
Fred Drake100814d2000-07-09 15:48:49 +0000742PyObject_Hash(PyObject *v)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000743{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000744 PyTypeObject *tp = Py_TYPE(v);
745 if (tp->tp_hash != NULL)
746 return (*tp->tp_hash)(v);
747 /* To keep to the general practice that inheriting
748 * solely from object in C code should work without
749 * an explicit call to PyType_Ready, we implicitly call
750 * PyType_Ready here and then check the tp_hash slot again
751 */
752 if (tp->tp_dict == NULL) {
753 if (PyType_Ready(tp) < 0)
754 return -1;
755 if (tp->tp_hash != NULL)
756 return (*tp->tp_hash)(v);
757 }
758 /* Otherwise, the object can't be hashed */
759 return PyObject_HashNotImplemented(v);
Guido van Rossum9bfef441993-03-29 10:43:31 +0000760}
761
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000762PyObject *
Jeremy Hyltonaf68c872005-12-10 18:50:16 +0000763PyObject_GetAttrString(PyObject *v, const char *name)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000764{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000765 PyObject *w, *res;
Guido van Rossumd8eb1b31996-08-09 20:52:03 +0000766
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000767 if (Py_TYPE(v)->tp_getattr != NULL)
768 return (*Py_TYPE(v)->tp_getattr)(v, (char*)name);
INADA Naoki3e8d6cb2017-02-21 23:57:25 +0900769 w = PyUnicode_FromString(name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000770 if (w == NULL)
771 return NULL;
772 res = PyObject_GetAttr(v, w);
Victor Stinner59af08f2012-03-22 02:09:08 +0100773 Py_DECREF(w);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000774 return res;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000775}
776
777int
Jeremy Hyltonaf68c872005-12-10 18:50:16 +0000778PyObject_HasAttrString(PyObject *v, const char *name)
Guido van Rossumed18fdc1993-07-11 19:55:34 +0000779{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000780 PyObject *res = PyObject_GetAttrString(v, name);
781 if (res != NULL) {
782 Py_DECREF(res);
783 return 1;
784 }
785 PyErr_Clear();
786 return 0;
Guido van Rossumed18fdc1993-07-11 19:55:34 +0000787}
788
789int
Jeremy Hyltonaf68c872005-12-10 18:50:16 +0000790PyObject_SetAttrString(PyObject *v, const char *name, PyObject *w)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000791{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000792 PyObject *s;
793 int res;
Guido van Rossumd8eb1b31996-08-09 20:52:03 +0000794
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000795 if (Py_TYPE(v)->tp_setattr != NULL)
796 return (*Py_TYPE(v)->tp_setattr)(v, (char*)name, w);
797 s = PyUnicode_InternFromString(name);
798 if (s == NULL)
799 return -1;
800 res = PyObject_SetAttr(v, s, w);
801 Py_XDECREF(s);
802 return res;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000803}
804
Benjamin Petersonbfebb7b2011-12-15 15:34:02 -0500805int
806_PyObject_IsAbstract(PyObject *obj)
807{
808 int res;
809 PyObject* isabstract;
Benjamin Petersonbfebb7b2011-12-15 15:34:02 -0500810
811 if (obj == NULL)
812 return 0;
813
814 isabstract = _PyObject_GetAttrId(obj, &PyId___isabstractmethod__);
815 if (isabstract == NULL) {
816 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
817 PyErr_Clear();
818 return 0;
819 }
820 return -1;
821 }
822 res = PyObject_IsTrue(isabstract);
823 Py_DECREF(isabstract);
824 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
885int
Fred Drake100814d2000-07-09 15:48:49 +0000886PyObject_HasAttr(PyObject *v, PyObject *name)
Guido van Rossum98ff96a1997-05-20 18:34:44 +0000887{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000888 PyObject *res = PyObject_GetAttr(v, name);
889 if (res != NULL) {
890 Py_DECREF(res);
891 return 1;
892 }
893 PyErr_Clear();
894 return 0;
Guido van Rossum98ff96a1997-05-20 18:34:44 +0000895}
896
897int
Fred Drake100814d2000-07-09 15:48:49 +0000898PyObject_SetAttr(PyObject *v, PyObject *name, PyObject *value)
Guido van Rossum98ff96a1997-05-20 18:34:44 +0000899{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000900 PyTypeObject *tp = Py_TYPE(v);
901 int err;
Marc-André Lemburge44e5072000-09-18 16:20:57 +0000902
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000903 if (!PyUnicode_Check(name)) {
904 PyErr_Format(PyExc_TypeError,
905 "attribute name must be string, not '%.200s'",
906 name->ob_type->tp_name);
907 return -1;
908 }
909 Py_INCREF(name);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000910
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000911 PyUnicode_InternInPlace(&name);
912 if (tp->tp_setattro != NULL) {
913 err = (*tp->tp_setattro)(v, name, value);
914 Py_DECREF(name);
915 return err;
916 }
917 if (tp->tp_setattr != NULL) {
Serhiy Storchaka2a404b62017-01-22 23:07:07 +0200918 const char *name_str = PyUnicode_AsUTF8(name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000919 if (name_str == NULL)
920 return -1;
Serhiy Storchaka2a404b62017-01-22 23:07:07 +0200921 err = (*tp->tp_setattr)(v, (char *)name_str, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000922 Py_DECREF(name);
923 return err;
924 }
925 Py_DECREF(name);
926 assert(name->ob_refcnt >= 1);
927 if (tp->tp_getattr == NULL && tp->tp_getattro == NULL)
928 PyErr_Format(PyExc_TypeError,
929 "'%.100s' object has no attributes "
930 "(%s .%U)",
931 tp->tp_name,
932 value==NULL ? "del" : "assign to",
933 name);
934 else
935 PyErr_Format(PyExc_TypeError,
936 "'%.100s' object has only read-only attributes "
937 "(%s .%U)",
938 tp->tp_name,
939 value==NULL ? "del" : "assign to",
940 name);
941 return -1;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000942}
943
944/* Helper to get a pointer to an object's __dict__ slot, if any */
945
946PyObject **
947_PyObject_GetDictPtr(PyObject *obj)
948{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000949 Py_ssize_t dictoffset;
950 PyTypeObject *tp = Py_TYPE(obj);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000951
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000952 dictoffset = tp->tp_dictoffset;
953 if (dictoffset == 0)
954 return NULL;
955 if (dictoffset < 0) {
956 Py_ssize_t tsize;
957 size_t size;
Guido van Rossum2eb0b872002-03-01 22:24:49 +0000958
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000959 tsize = ((PyVarObject *)obj)->ob_size;
960 if (tsize < 0)
961 tsize = -tsize;
962 size = _PyObject_VAR_SIZE(tp, tsize);
Guido van Rossum2eb0b872002-03-01 22:24:49 +0000963
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000964 dictoffset += (long)size;
965 assert(dictoffset > 0);
966 assert(dictoffset % SIZEOF_VOID_P == 0);
967 }
968 return (PyObject **) ((char *)obj + dictoffset);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000969}
970
Tim Peters6d6c1a32001-08-02 04:15:00 +0000971PyObject *
Raymond Hettinger1da1dbf2003-03-17 19:46:11 +0000972PyObject_SelfIter(PyObject *obj)
Raymond Hettinger01538262003-03-17 08:24:35 +0000973{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000974 Py_INCREF(obj);
975 return obj;
Raymond Hettinger01538262003-03-17 08:24:35 +0000976}
977
Antoine Pitroua7013882012-04-05 00:04:20 +0200978/* Convenience function to get a builtin from its name */
979PyObject *
980_PyObject_GetBuiltin(const char *name)
981{
Victor Stinner53e9ec42013-11-07 00:43:05 +0100982 PyObject *mod_name, *mod, *attr;
983
Victor Stinnerbd303c12013-11-07 23:07:29 +0100984 mod_name = _PyUnicode_FromId(&PyId_builtins); /* borrowed */
Victor Stinner53e9ec42013-11-07 00:43:05 +0100985 if (mod_name == NULL)
986 return NULL;
987 mod = PyImport_Import(mod_name);
Antoine Pitroua7013882012-04-05 00:04:20 +0200988 if (mod == NULL)
989 return NULL;
990 attr = PyObject_GetAttrString(mod, name);
991 Py_DECREF(mod);
992 return attr;
993}
994
Amaury Forgeot d'Arcf343e012009-01-12 23:58:21 +0000995/* Helper used when the __next__ method is removed from a type:
996 tp_iternext is never NULL and can be safely called without checking
997 on every iteration.
998 */
999
1000PyObject *
1001_PyObject_NextNotImplemented(PyObject *self)
1002{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001003 PyErr_Format(PyExc_TypeError,
1004 "'%.200s' object is not iterable",
1005 Py_TYPE(self)->tp_name);
1006 return NULL;
Amaury Forgeot d'Arcf343e012009-01-12 23:58:21 +00001007}
1008
Yury Selivanovf2392132016-12-13 19:03:51 -05001009
1010/* Specialized version of _PyObject_GenericGetAttrWithDict
1011 specifically for the LOAD_METHOD opcode.
1012
1013 Return 1 if a method is found, 0 if it's a regular attribute
1014 from __dict__ or something returned by using a descriptor
1015 protocol.
1016
1017 `method` will point to the resolved attribute or NULL. In the
1018 latter case, an error will be set.
1019*/
1020int
1021_PyObject_GetMethod(PyObject *obj, PyObject *name, PyObject **method)
1022{
1023 PyTypeObject *tp = Py_TYPE(obj);
1024 PyObject *descr;
1025 descrgetfunc f = NULL;
1026 PyObject **dictptr, *dict;
1027 PyObject *attr;
1028 int meth_found = 0;
1029
1030 assert(*method == NULL);
1031
1032 if (Py_TYPE(obj)->tp_getattro != PyObject_GenericGetAttr
1033 || !PyUnicode_Check(name)) {
1034 *method = PyObject_GetAttr(obj, name);
1035 return 0;
1036 }
1037
1038 if (tp->tp_dict == NULL && PyType_Ready(tp) < 0)
1039 return 0;
1040
1041 descr = _PyType_Lookup(tp, name);
1042 if (descr != NULL) {
1043 Py_INCREF(descr);
INADA Naoki5566bbb2017-02-03 07:43:03 +09001044 if (PyFunction_Check(descr) ||
1045 (Py_TYPE(descr) == &PyMethodDescr_Type)) {
Yury Selivanovf2392132016-12-13 19:03:51 -05001046 meth_found = 1;
1047 } else {
1048 f = descr->ob_type->tp_descr_get;
1049 if (f != NULL && PyDescr_IsData(descr)) {
1050 *method = f(descr, obj, (PyObject *)obj->ob_type);
1051 Py_DECREF(descr);
1052 return 0;
1053 }
1054 }
1055 }
1056
1057 dictptr = _PyObject_GetDictPtr(obj);
1058 if (dictptr != NULL && (dict = *dictptr) != NULL) {
1059 Py_INCREF(dict);
1060 attr = PyDict_GetItem(dict, name);
1061 if (attr != NULL) {
1062 Py_INCREF(attr);
1063 *method = attr;
1064 Py_DECREF(dict);
1065 Py_XDECREF(descr);
1066 return 0;
1067 }
1068 Py_DECREF(dict);
1069 }
1070
1071 if (meth_found) {
1072 *method = descr;
1073 return 1;
1074 }
1075
1076 if (f != NULL) {
1077 *method = f(descr, obj, (PyObject *)Py_TYPE(obj));
1078 Py_DECREF(descr);
1079 return 0;
1080 }
1081
1082 if (descr != NULL) {
1083 *method = descr;
1084 return 0;
1085 }
1086
1087 PyErr_Format(PyExc_AttributeError,
1088 "'%.50s' object has no attribute '%U'",
1089 tp->tp_name, name);
1090 return 0;
1091}
1092
1093/* Generic GetAttr functions - put these in your tp_[gs]etattro slot. */
Michael W. Hudson1593f502004-09-14 17:09:47 +00001094
Raymond Hettinger01538262003-03-17 08:24:35 +00001095PyObject *
Antoine Pitrou1a9a9d52010-08-28 18:17:03 +00001096_PyObject_GenericGetAttrWithDict(PyObject *obj, PyObject *name, PyObject *dict)
Tim Peters6d6c1a32001-08-02 04:15:00 +00001097{
Yury Selivanovf2392132016-12-13 19:03:51 -05001098 /* Make sure the logic of _PyObject_GetMethod is in sync with
1099 this method.
1100 */
1101
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001102 PyTypeObject *tp = Py_TYPE(obj);
1103 PyObject *descr = NULL;
1104 PyObject *res = NULL;
1105 descrgetfunc f;
1106 Py_ssize_t dictoffset;
1107 PyObject **dictptr;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001108
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001109 if (!PyUnicode_Check(name)){
1110 PyErr_Format(PyExc_TypeError,
1111 "attribute name must be string, not '%.200s'",
1112 name->ob_type->tp_name);
1113 return NULL;
1114 }
Serhiy Storchaka55c861f2016-04-17 20:31:51 +03001115 Py_INCREF(name);
Guido van Rossumebca9fc2001-12-04 15:54:53 +00001116
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001117 if (tp->tp_dict == NULL) {
1118 if (PyType_Ready(tp) < 0)
1119 goto done;
1120 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001121
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001122 descr = _PyType_Lookup(tp, name);
Michael W. Hudsonb2c7de42003-08-15 13:07:47 +00001123
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001124 f = NULL;
1125 if (descr != NULL) {
Serhiy Storchaka55c861f2016-04-17 20:31:51 +03001126 Py_INCREF(descr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001127 f = descr->ob_type->tp_descr_get;
1128 if (f != NULL && PyDescr_IsData(descr)) {
1129 res = f(descr, obj, (PyObject *)obj->ob_type);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001130 goto done;
1131 }
1132 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001133
Antoine Pitrou1a9a9d52010-08-28 18:17:03 +00001134 if (dict == NULL) {
1135 /* Inline _PyObject_GetDictPtr */
1136 dictoffset = tp->tp_dictoffset;
1137 if (dictoffset != 0) {
1138 if (dictoffset < 0) {
1139 Py_ssize_t tsize;
1140 size_t size;
Guido van Rossumc66ff442002-08-19 16:50:48 +00001141
Antoine Pitrou1a9a9d52010-08-28 18:17:03 +00001142 tsize = ((PyVarObject *)obj)->ob_size;
1143 if (tsize < 0)
1144 tsize = -tsize;
1145 size = _PyObject_VAR_SIZE(tp, tsize);
Serhiy Storchaka55c861f2016-04-17 20:31:51 +03001146 assert(size <= PY_SSIZE_T_MAX);
Guido van Rossumc66ff442002-08-19 16:50:48 +00001147
Serhiy Storchaka55c861f2016-04-17 20:31:51 +03001148 dictoffset += (Py_ssize_t)size;
Antoine Pitrou1a9a9d52010-08-28 18:17:03 +00001149 assert(dictoffset > 0);
1150 assert(dictoffset % SIZEOF_VOID_P == 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001151 }
Antoine Pitrou1a9a9d52010-08-28 18:17:03 +00001152 dictptr = (PyObject **) ((char *)obj + dictoffset);
1153 dict = *dictptr;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001154 }
1155 }
Antoine Pitrou1a9a9d52010-08-28 18:17:03 +00001156 if (dict != NULL) {
1157 Py_INCREF(dict);
1158 res = PyDict_GetItem(dict, name);
1159 if (res != NULL) {
1160 Py_INCREF(res);
Antoine Pitrou1a9a9d52010-08-28 18:17:03 +00001161 Py_DECREF(dict);
1162 goto done;
1163 }
1164 Py_DECREF(dict);
1165 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001166
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001167 if (f != NULL) {
1168 res = f(descr, obj, (PyObject *)Py_TYPE(obj));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001169 goto done;
1170 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001171
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001172 if (descr != NULL) {
1173 res = descr;
Victor Stinner2d01dc02012-03-09 00:44:13 +01001174 descr = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001175 goto done;
1176 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001177
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001178 PyErr_Format(PyExc_AttributeError,
1179 "'%.50s' object has no attribute '%U'",
1180 tp->tp_name, name);
Guido van Rossumebca9fc2001-12-04 15:54:53 +00001181 done:
Victor Stinner2d01dc02012-03-09 00:44:13 +01001182 Py_XDECREF(descr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001183 Py_DECREF(name);
1184 return res;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001185}
1186
Antoine Pitrou1a9a9d52010-08-28 18:17:03 +00001187PyObject *
1188PyObject_GenericGetAttr(PyObject *obj, PyObject *name)
1189{
1190 return _PyObject_GenericGetAttrWithDict(obj, name, NULL);
1191}
1192
Tim Peters6d6c1a32001-08-02 04:15:00 +00001193int
Antoine Pitrou1a9a9d52010-08-28 18:17:03 +00001194_PyObject_GenericSetAttrWithDict(PyObject *obj, PyObject *name,
1195 PyObject *value, PyObject *dict)
Tim Peters6d6c1a32001-08-02 04:15:00 +00001196{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001197 PyTypeObject *tp = Py_TYPE(obj);
1198 PyObject *descr;
1199 descrsetfunc f;
1200 PyObject **dictptr;
1201 int res = -1;
Guido van Rossumebca9fc2001-12-04 15:54:53 +00001202
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001203 if (!PyUnicode_Check(name)){
1204 PyErr_Format(PyExc_TypeError,
1205 "attribute name must be string, not '%.200s'",
1206 name->ob_type->tp_name);
1207 return -1;
1208 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001209
Benjamin Peterson74529ad2012-03-09 07:25:32 -08001210 if (tp->tp_dict == NULL && PyType_Ready(tp) < 0)
1211 return -1;
1212
1213 Py_INCREF(name);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001214
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001215 descr = _PyType_Lookup(tp, name);
Victor Stinner2d01dc02012-03-09 00:44:13 +01001216
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001217 if (descr != NULL) {
Serhiy Storchaka55c861f2016-04-17 20:31:51 +03001218 Py_INCREF(descr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001219 f = descr->ob_type->tp_descr_set;
Serhiy Storchaka55c861f2016-04-17 20:31:51 +03001220 if (f != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001221 res = f(descr, obj, value);
1222 goto done;
1223 }
1224 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001225
Antoine Pitrou1a9a9d52010-08-28 18:17:03 +00001226 if (dict == NULL) {
1227 dictptr = _PyObject_GetDictPtr(obj);
Serhiy Storchaka55c861f2016-04-17 20:31:51 +03001228 if (dictptr == NULL) {
1229 if (descr == NULL) {
1230 PyErr_Format(PyExc_AttributeError,
1231 "'%.100s' object has no attribute '%U'",
1232 tp->tp_name, name);
1233 }
1234 else {
1235 PyErr_Format(PyExc_AttributeError,
1236 "'%.50s' object attribute '%U' is read-only",
1237 tp->tp_name, name);
1238 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001239 goto done;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001240 }
Serhiy Storchaka55c861f2016-04-17 20:31:51 +03001241 res = _PyObjectDict_SetItem(tp, dictptr, name, value);
Antoine Pitrou1a9a9d52010-08-28 18:17:03 +00001242 }
Serhiy Storchaka55c861f2016-04-17 20:31:51 +03001243 else {
Antoine Pitrou1a9a9d52010-08-28 18:17:03 +00001244 Py_INCREF(dict);
1245 if (value == NULL)
1246 res = PyDict_DelItem(dict, name);
1247 else
1248 res = PyDict_SetItem(dict, name, value);
Benjamin Peterson74529ad2012-03-09 07:25:32 -08001249 Py_DECREF(dict);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001250 }
Serhiy Storchaka55c861f2016-04-17 20:31:51 +03001251 if (res < 0 && PyErr_ExceptionMatches(PyExc_KeyError))
1252 PyErr_SetObject(PyExc_AttributeError, name);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001253
Guido van Rossumebca9fc2001-12-04 15:54:53 +00001254 done:
Victor Stinner2d01dc02012-03-09 00:44:13 +01001255 Py_XDECREF(descr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001256 Py_DECREF(name);
1257 return res;
Guido van Rossum98ff96a1997-05-20 18:34:44 +00001258}
1259
Antoine Pitrou1a9a9d52010-08-28 18:17:03 +00001260int
1261PyObject_GenericSetAttr(PyObject *obj, PyObject *name, PyObject *value)
1262{
1263 return _PyObject_GenericSetAttrWithDict(obj, name, value, NULL);
1264}
1265
Benjamin Peterson8eb12692012-02-19 19:59:10 -05001266int
1267PyObject_GenericSetDict(PyObject *obj, PyObject *value, void *context)
1268{
Serhiy Storchaka576f1322016-01-05 21:27:54 +02001269 PyObject **dictptr = _PyObject_GetDictPtr(obj);
Benjamin Peterson8eb12692012-02-19 19:59:10 -05001270 if (dictptr == NULL) {
1271 PyErr_SetString(PyExc_AttributeError,
1272 "This object has no __dict__");
1273 return -1;
1274 }
1275 if (value == NULL) {
1276 PyErr_SetString(PyExc_TypeError, "cannot delete __dict__");
1277 return -1;
1278 }
1279 if (!PyDict_Check(value)) {
1280 PyErr_Format(PyExc_TypeError,
1281 "__dict__ must be set to a dictionary, "
1282 "not a '%.200s'", Py_TYPE(value)->tp_name);
1283 return -1;
1284 }
Serhiy Storchaka576f1322016-01-05 21:27:54 +02001285 Py_INCREF(value);
Serhiy Storchakaec397562016-04-06 09:50:03 +03001286 Py_XSETREF(*dictptr, value);
Benjamin Peterson8eb12692012-02-19 19:59:10 -05001287 return 0;
1288}
1289
Antoine Pitrou1a9a9d52010-08-28 18:17:03 +00001290
Guido van Rossum6ac258d1993-05-12 08:24:20 +00001291/* Test a value used as condition, e.g., in a for or if statement.
1292 Return -1 if an error occurred */
1293
1294int
Fred Drake100814d2000-07-09 15:48:49 +00001295PyObject_IsTrue(PyObject *v)
Guido van Rossum6ac258d1993-05-12 08:24:20 +00001296{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001297 Py_ssize_t res;
1298 if (v == Py_True)
1299 return 1;
1300 if (v == Py_False)
1301 return 0;
1302 if (v == Py_None)
1303 return 0;
1304 else if (v->ob_type->tp_as_number != NULL &&
1305 v->ob_type->tp_as_number->nb_bool != NULL)
1306 res = (*v->ob_type->tp_as_number->nb_bool)(v);
1307 else if (v->ob_type->tp_as_mapping != NULL &&
1308 v->ob_type->tp_as_mapping->mp_length != NULL)
1309 res = (*v->ob_type->tp_as_mapping->mp_length)(v);
1310 else if (v->ob_type->tp_as_sequence != NULL &&
1311 v->ob_type->tp_as_sequence->sq_length != NULL)
1312 res = (*v->ob_type->tp_as_sequence->sq_length)(v);
1313 else
1314 return 1;
1315 /* if it is negative, it should be either -1 or -2 */
1316 return (res > 0) ? 1 : Py_SAFE_DOWNCAST(res, Py_ssize_t, int);
Guido van Rossum6ac258d1993-05-12 08:24:20 +00001317}
1318
Tim Peters803526b2002-07-07 05:13:56 +00001319/* equivalent of 'not v'
Guido van Rossumc3d3f961998-04-09 17:53:59 +00001320 Return -1 if an error occurred */
1321
1322int
Fred Drake100814d2000-07-09 15:48:49 +00001323PyObject_Not(PyObject *v)
Guido van Rossumc3d3f961998-04-09 17:53:59 +00001324{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001325 int res;
1326 res = PyObject_IsTrue(v);
1327 if (res < 0)
1328 return res;
1329 return res == 0;
Guido van Rossumc3d3f961998-04-09 17:53:59 +00001330}
1331
Guido van Rossum49b11fe1995-01-26 00:38:22 +00001332/* Test whether an object can be called */
1333
1334int
Fred Drake100814d2000-07-09 15:48:49 +00001335PyCallable_Check(PyObject *x)
Guido van Rossum49b11fe1995-01-26 00:38:22 +00001336{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001337 if (x == NULL)
1338 return 0;
1339 return x->ob_type->tp_call != NULL;
Guido van Rossum49b11fe1995-01-26 00:38:22 +00001340}
1341
Tim Peters7eea37e2001-09-04 22:08:56 +00001342
Georg Brandle32b4222007-03-10 22:13:27 +00001343/* Helper for PyObject_Dir without arguments: returns the local scope. */
1344static PyObject *
Guido van Rossumad7d8d12007-04-13 01:39:34 +00001345_dir_locals(void)
Tim Peters305b5852001-09-17 02:38:46 +00001346{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001347 PyObject *names;
Victor Stinner41bb43a2013-10-29 01:19:37 +01001348 PyObject *locals;
Tim Peters305b5852001-09-17 02:38:46 +00001349
Victor Stinner41bb43a2013-10-29 01:19:37 +01001350 locals = PyEval_GetLocals();
1351 if (locals == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001352 return NULL;
Tim Peters305b5852001-09-17 02:38:46 +00001353
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001354 names = PyMapping_Keys(locals);
1355 if (!names)
1356 return NULL;
1357 if (!PyList_Check(names)) {
1358 PyErr_Format(PyExc_TypeError,
1359 "dir(): expected keys() of locals to be a list, "
1360 "not '%.200s'", Py_TYPE(names)->tp_name);
1361 Py_DECREF(names);
1362 return NULL;
1363 }
Benjamin Peterson3bbb7222011-06-11 16:12:08 -05001364 if (PyList_Sort(names)) {
1365 Py_DECREF(names);
1366 return NULL;
1367 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001368 /* the locals don't need to be DECREF'd */
1369 return names;
Georg Brandle32b4222007-03-10 22:13:27 +00001370}
1371
Benjamin Peterson82b00c12011-05-24 11:09:06 -05001372/* Helper for PyObject_Dir: object introspection. */
Georg Brandle32b4222007-03-10 22:13:27 +00001373static PyObject *
1374_dir_object(PyObject *obj)
1375{
Benjamin Peterson3bbb7222011-06-11 16:12:08 -05001376 PyObject *result, *sorted;
Benjamin Petersonce798522012-01-22 11:24:29 -05001377 PyObject *dirfunc = _PyObject_LookupSpecial(obj, &PyId___dir__);
Georg Brandle32b4222007-03-10 22:13:27 +00001378
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001379 assert(obj);
1380 if (dirfunc == NULL) {
Benjamin Peterson82b00c12011-05-24 11:09:06 -05001381 if (!PyErr_Occurred())
1382 PyErr_SetString(PyExc_TypeError, "object does not provide __dir__");
1383 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001384 }
Benjamin Peterson82b00c12011-05-24 11:09:06 -05001385 /* use __dir__ */
Victor Stinnerf17c3de2016-12-06 18:46:19 +01001386 result = _PyObject_CallNoArg(dirfunc);
Benjamin Peterson82b00c12011-05-24 11:09:06 -05001387 Py_DECREF(dirfunc);
1388 if (result == NULL)
1389 return NULL;
Benjamin Peterson3bbb7222011-06-11 16:12:08 -05001390 /* return sorted(result) */
1391 sorted = PySequence_List(result);
1392 Py_DECREF(result);
1393 if (sorted == NULL)
1394 return NULL;
1395 if (PyList_Sort(sorted)) {
1396 Py_DECREF(sorted);
1397 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001398 }
Benjamin Peterson3bbb7222011-06-11 16:12:08 -05001399 return sorted;
Georg Brandle32b4222007-03-10 22:13:27 +00001400}
1401
1402/* Implementation of dir() -- if obj is NULL, returns the names in the current
1403 (local) scope. Otherwise, performs introspection of the object: returns a
1404 sorted list of attribute names (supposedly) accessible from the object
1405*/
1406PyObject *
1407PyObject_Dir(PyObject *obj)
1408{
Benjamin Peterson3bbb7222011-06-11 16:12:08 -05001409 return (obj == NULL) ? _dir_locals() : _dir_object(obj);
Tim Peters7eea37e2001-09-04 22:08:56 +00001410}
Guido van Rossum49b11fe1995-01-26 00:38:22 +00001411
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001412/*
Raymond Hettinger66d2be82011-07-28 09:55:13 -07001413None is a non-NULL undefined value.
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001414There is (and should be!) no way to create other objects of this type,
Guido van Rossum3f5da241990-12-20 15:06:42 +00001415so there is exactly one (which is indestructible, by the way).
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001416*/
1417
Guido van Rossum0c182a11992-03-27 17:26:13 +00001418/* ARGSUSED */
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001419static PyObject *
Fred Drake100814d2000-07-09 15:48:49 +00001420none_repr(PyObject *op)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001421{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001422 return PyUnicode_FromString("None");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001423}
1424
Barry Warsaw9bf16442001-01-23 16:24:35 +00001425/* ARGUSED */
1426static void
Tim Peters803526b2002-07-07 05:13:56 +00001427none_dealloc(PyObject* ignore)
Barry Warsaw9bf16442001-01-23 16:24:35 +00001428{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001429 /* This should never get called, but we also don't want to SEGV if
1430 * we accidentally decref None out of existence.
1431 */
1432 Py_FatalError("deallocating None");
Barry Warsaw9bf16442001-01-23 16:24:35 +00001433}
1434
Benjamin Petersonc4607ae2011-07-29 18:19:43 -05001435static PyObject *
1436none_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
1437{
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +02001438 if (PyTuple_GET_SIZE(args) || (kwargs && PyDict_GET_SIZE(kwargs))) {
Benjamin Petersonc4607ae2011-07-29 18:19:43 -05001439 PyErr_SetString(PyExc_TypeError, "NoneType takes no arguments");
1440 return NULL;
1441 }
1442 Py_RETURN_NONE;
1443}
1444
Raymond Hettinger66d2be82011-07-28 09:55:13 -07001445static int
1446none_bool(PyObject *v)
1447{
1448 return 0;
1449}
1450
1451static PyNumberMethods none_as_number = {
1452 0, /* nb_add */
1453 0, /* nb_subtract */
1454 0, /* nb_multiply */
1455 0, /* nb_remainder */
1456 0, /* nb_divmod */
1457 0, /* nb_power */
1458 0, /* nb_negative */
1459 0, /* nb_positive */
1460 0, /* nb_absolute */
1461 (inquiry)none_bool, /* nb_bool */
1462 0, /* nb_invert */
1463 0, /* nb_lshift */
1464 0, /* nb_rshift */
1465 0, /* nb_and */
1466 0, /* nb_xor */
1467 0, /* nb_or */
1468 0, /* nb_int */
1469 0, /* nb_reserved */
1470 0, /* nb_float */
1471 0, /* nb_inplace_add */
1472 0, /* nb_inplace_subtract */
1473 0, /* nb_inplace_multiply */
1474 0, /* nb_inplace_remainder */
1475 0, /* nb_inplace_power */
1476 0, /* nb_inplace_lshift */
1477 0, /* nb_inplace_rshift */
1478 0, /* nb_inplace_and */
1479 0, /* nb_inplace_xor */
1480 0, /* nb_inplace_or */
1481 0, /* nb_floor_divide */
1482 0, /* nb_true_divide */
1483 0, /* nb_inplace_floor_divide */
1484 0, /* nb_inplace_true_divide */
1485 0, /* nb_index */
1486};
Barry Warsaw9bf16442001-01-23 16:24:35 +00001487
Alexandre Vassalotti65846c62013-11-30 17:55:48 -08001488PyTypeObject _PyNone_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001489 PyVarObject_HEAD_INIT(&PyType_Type, 0)
1490 "NoneType",
1491 0,
1492 0,
1493 none_dealloc, /*tp_dealloc*/ /*never called*/
1494 0, /*tp_print*/
1495 0, /*tp_getattr*/
1496 0, /*tp_setattr*/
1497 0, /*tp_reserved*/
1498 none_repr, /*tp_repr*/
Raymond Hettinger66d2be82011-07-28 09:55:13 -07001499 &none_as_number, /*tp_as_number*/
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001500 0, /*tp_as_sequence*/
1501 0, /*tp_as_mapping*/
1502 0, /*tp_hash */
Benjamin Petersonc4607ae2011-07-29 18:19:43 -05001503 0, /*tp_call */
1504 0, /*tp_str */
1505 0, /*tp_getattro */
1506 0, /*tp_setattro */
1507 0, /*tp_as_buffer */
1508 Py_TPFLAGS_DEFAULT, /*tp_flags */
1509 0, /*tp_doc */
1510 0, /*tp_traverse */
1511 0, /*tp_clear */
1512 0, /*tp_richcompare */
1513 0, /*tp_weaklistoffset */
1514 0, /*tp_iter */
1515 0, /*tp_iternext */
1516 0, /*tp_methods */
1517 0, /*tp_members */
1518 0, /*tp_getset */
1519 0, /*tp_base */
1520 0, /*tp_dict */
1521 0, /*tp_descr_get */
1522 0, /*tp_descr_set */
1523 0, /*tp_dictoffset */
1524 0, /*tp_init */
1525 0, /*tp_alloc */
1526 none_new, /*tp_new */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001527};
1528
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001529PyObject _Py_NoneStruct = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001530 _PyObject_EXTRA_INIT
Alexandre Vassalotti65846c62013-11-30 17:55:48 -08001531 1, &_PyNone_Type
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001532};
1533
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +00001534/* NotImplemented is an object that can be used to signal that an
1535 operation is not implemented for the given type combination. */
1536
1537static PyObject *
1538NotImplemented_repr(PyObject *op)
1539{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001540 return PyUnicode_FromString("NotImplemented");
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +00001541}
1542
Benjamin Peterson18d7d7a2011-07-29 18:27:44 -05001543static PyObject *
Alexandre Vassalottic49477b2013-11-24 02:53:45 -08001544NotImplemented_reduce(PyObject *op)
1545{
1546 return PyUnicode_FromString("NotImplemented");
1547}
1548
1549static PyMethodDef notimplemented_methods[] = {
1550 {"__reduce__", (PyCFunction)NotImplemented_reduce, METH_NOARGS, NULL},
1551 {NULL, NULL}
1552};
1553
1554static PyObject *
Benjamin Peterson18d7d7a2011-07-29 18:27:44 -05001555notimplemented_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
1556{
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +02001557 if (PyTuple_GET_SIZE(args) || (kwargs && PyDict_GET_SIZE(kwargs))) {
Benjamin Peterson18d7d7a2011-07-29 18:27:44 -05001558 PyErr_SetString(PyExc_TypeError, "NotImplementedType takes no arguments");
1559 return NULL;
1560 }
Brian Curtindfc80e32011-08-10 20:28:54 -05001561 Py_RETURN_NOTIMPLEMENTED;
Benjamin Peterson18d7d7a2011-07-29 18:27:44 -05001562}
1563
Armin Ronacher226b1db2012-10-06 14:28:58 +02001564static void
1565notimplemented_dealloc(PyObject* ignore)
1566{
1567 /* This should never get called, but we also don't want to SEGV if
1568 * we accidentally decref NotImplemented out of existence.
1569 */
1570 Py_FatalError("deallocating NotImplemented");
1571}
1572
Alexandre Vassalotti65846c62013-11-30 17:55:48 -08001573PyTypeObject _PyNotImplemented_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001574 PyVarObject_HEAD_INIT(&PyType_Type, 0)
1575 "NotImplementedType",
1576 0,
1577 0,
Armin Ronacher226b1db2012-10-06 14:28:58 +02001578 notimplemented_dealloc, /*tp_dealloc*/ /*never called*/
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001579 0, /*tp_print*/
1580 0, /*tp_getattr*/
1581 0, /*tp_setattr*/
1582 0, /*tp_reserved*/
1583 NotImplemented_repr, /*tp_repr*/
1584 0, /*tp_as_number*/
1585 0, /*tp_as_sequence*/
1586 0, /*tp_as_mapping*/
1587 0, /*tp_hash */
Benjamin Peterson18d7d7a2011-07-29 18:27:44 -05001588 0, /*tp_call */
1589 0, /*tp_str */
1590 0, /*tp_getattro */
1591 0, /*tp_setattro */
1592 0, /*tp_as_buffer */
1593 Py_TPFLAGS_DEFAULT, /*tp_flags */
1594 0, /*tp_doc */
1595 0, /*tp_traverse */
1596 0, /*tp_clear */
1597 0, /*tp_richcompare */
1598 0, /*tp_weaklistoffset */
1599 0, /*tp_iter */
1600 0, /*tp_iternext */
Alexandre Vassalottic49477b2013-11-24 02:53:45 -08001601 notimplemented_methods, /*tp_methods */
Benjamin Peterson18d7d7a2011-07-29 18:27:44 -05001602 0, /*tp_members */
1603 0, /*tp_getset */
1604 0, /*tp_base */
1605 0, /*tp_dict */
1606 0, /*tp_descr_get */
1607 0, /*tp_descr_set */
1608 0, /*tp_dictoffset */
1609 0, /*tp_init */
1610 0, /*tp_alloc */
1611 notimplemented_new, /*tp_new */
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +00001612};
1613
1614PyObject _Py_NotImplementedStruct = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001615 _PyObject_EXTRA_INIT
Alexandre Vassalotti65846c62013-11-30 17:55:48 -08001616 1, &_PyNotImplemented_Type
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +00001617};
1618
Guido van Rossumba21a492001-08-16 08:17:26 +00001619void
1620_Py_ReadyTypes(void)
1621{
Victor Stinner5a1bb4e2014-06-02 14:10:59 +02001622 if (PyType_Ready(&PyBaseObject_Type) < 0)
1623 Py_FatalError("Can't initialize object type");
1624
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001625 if (PyType_Ready(&PyType_Type) < 0)
1626 Py_FatalError("Can't initialize type type");
Guido van Rossumba21a492001-08-16 08:17:26 +00001627
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001628 if (PyType_Ready(&_PyWeakref_RefType) < 0)
1629 Py_FatalError("Can't initialize weakref type");
Fred Drake0a4dd392004-07-02 18:57:45 +00001630
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001631 if (PyType_Ready(&_PyWeakref_CallableProxyType) < 0)
1632 Py_FatalError("Can't initialize callable weakref proxy type");
Benjamin Petersonfd838e62009-04-20 02:09:13 +00001633
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001634 if (PyType_Ready(&_PyWeakref_ProxyType) < 0)
1635 Py_FatalError("Can't initialize weakref proxy type");
Benjamin Petersonfd838e62009-04-20 02:09:13 +00001636
Victor Stinner5a1bb4e2014-06-02 14:10:59 +02001637 if (PyType_Ready(&PyLong_Type) < 0)
1638 Py_FatalError("Can't initialize int type");
1639
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001640 if (PyType_Ready(&PyBool_Type) < 0)
1641 Py_FatalError("Can't initialize bool type");
Guido van Rossum77f6a652002-04-03 22:41:51 +00001642
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001643 if (PyType_Ready(&PyByteArray_Type) < 0)
1644 Py_FatalError("Can't initialize bytearray type");
Guido van Rossum4dfe8a12006-04-22 23:28:04 +00001645
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001646 if (PyType_Ready(&PyBytes_Type) < 0)
1647 Py_FatalError("Can't initialize 'str'");
Guido van Rossumcacfc072002-05-24 19:01:59 +00001648
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001649 if (PyType_Ready(&PyList_Type) < 0)
1650 Py_FatalError("Can't initialize list type");
Guido van Rossumba21a492001-08-16 08:17:26 +00001651
Alexandre Vassalotti65846c62013-11-30 17:55:48 -08001652 if (PyType_Ready(&_PyNone_Type) < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001653 Py_FatalError("Can't initialize None type");
Guido van Rossumba21a492001-08-16 08:17:26 +00001654
Alexandre Vassalotti65846c62013-11-30 17:55:48 -08001655 if (PyType_Ready(&_PyNotImplemented_Type) < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001656 Py_FatalError("Can't initialize NotImplemented type");
Benjamin Petersonae937c02009-04-18 20:54:08 +00001657
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001658 if (PyType_Ready(&PyTraceBack_Type) < 0)
1659 Py_FatalError("Can't initialize traceback type");
Benjamin Petersonae937c02009-04-18 20:54:08 +00001660
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001661 if (PyType_Ready(&PySuper_Type) < 0)
1662 Py_FatalError("Can't initialize super type");
Benjamin Petersonae937c02009-04-18 20:54:08 +00001663
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001664 if (PyType_Ready(&PyRange_Type) < 0)
1665 Py_FatalError("Can't initialize range type");
Benjamin Petersonae937c02009-04-18 20:54:08 +00001666
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001667 if (PyType_Ready(&PyDict_Type) < 0)
1668 Py_FatalError("Can't initialize dict type");
Benjamin Petersonae937c02009-04-18 20:54:08 +00001669
Benjamin Petersondb87c992016-11-06 13:01:07 -08001670 if (PyType_Ready(&PyDictKeys_Type) < 0)
1671 Py_FatalError("Can't initialize dict keys type");
1672
1673 if (PyType_Ready(&PyDictValues_Type) < 0)
1674 Py_FatalError("Can't initialize dict values type");
1675
1676 if (PyType_Ready(&PyDictItems_Type) < 0)
1677 Py_FatalError("Can't initialize dict items type");
1678
Eric Snow96c6af92015-05-29 22:21:39 -06001679 if (PyType_Ready(&PyODict_Type) < 0)
1680 Py_FatalError("Can't initialize OrderedDict type");
1681
1682 if (PyType_Ready(&PyODictKeys_Type) < 0)
1683 Py_FatalError("Can't initialize odict_keys type");
1684
1685 if (PyType_Ready(&PyODictItems_Type) < 0)
1686 Py_FatalError("Can't initialize odict_items type");
1687
1688 if (PyType_Ready(&PyODictValues_Type) < 0)
1689 Py_FatalError("Can't initialize odict_values type");
1690
1691 if (PyType_Ready(&PyODictIter_Type) < 0)
1692 Py_FatalError("Can't initialize odict_keyiterator type");
1693
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001694 if (PyType_Ready(&PySet_Type) < 0)
1695 Py_FatalError("Can't initialize set type");
Benjamin Petersonae937c02009-04-18 20:54:08 +00001696
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001697 if (PyType_Ready(&PyUnicode_Type) < 0)
1698 Py_FatalError("Can't initialize str type");
Benjamin Petersonae937c02009-04-18 20:54:08 +00001699
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001700 if (PyType_Ready(&PySlice_Type) < 0)
1701 Py_FatalError("Can't initialize slice type");
Benjamin Petersonae937c02009-04-18 20:54:08 +00001702
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001703 if (PyType_Ready(&PyStaticMethod_Type) < 0)
1704 Py_FatalError("Can't initialize static method type");
Benjamin Petersonae937c02009-04-18 20:54:08 +00001705
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001706 if (PyType_Ready(&PyComplex_Type) < 0)
1707 Py_FatalError("Can't initialize complex type");
Skip Montanaroba1e0f42009-10-18 14:25:35 +00001708
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001709 if (PyType_Ready(&PyFloat_Type) < 0)
1710 Py_FatalError("Can't initialize float type");
Benjamin Petersonae937c02009-04-18 20:54:08 +00001711
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001712 if (PyType_Ready(&PyFrozenSet_Type) < 0)
1713 Py_FatalError("Can't initialize frozenset type");
Benjamin Petersonae937c02009-04-18 20:54:08 +00001714
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001715 if (PyType_Ready(&PyProperty_Type) < 0)
1716 Py_FatalError("Can't initialize property type");
Benjamin Petersonae937c02009-04-18 20:54:08 +00001717
Stefan Krah9a2d99e2012-02-25 12:24:21 +01001718 if (PyType_Ready(&_PyManagedBuffer_Type) < 0)
1719 Py_FatalError("Can't initialize managed buffer type");
1720
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001721 if (PyType_Ready(&PyMemoryView_Type) < 0)
1722 Py_FatalError("Can't initialize memoryview type");
Benjamin Petersonae937c02009-04-18 20:54:08 +00001723
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001724 if (PyType_Ready(&PyTuple_Type) < 0)
1725 Py_FatalError("Can't initialize tuple type");
Benjamin Petersonae937c02009-04-18 20:54:08 +00001726
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001727 if (PyType_Ready(&PyEnum_Type) < 0)
1728 Py_FatalError("Can't initialize enumerate type");
Benjamin Petersonae937c02009-04-18 20:54:08 +00001729
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001730 if (PyType_Ready(&PyReversed_Type) < 0)
1731 Py_FatalError("Can't initialize reversed type");
Guido van Rossum47b9ff62006-08-24 00:41:19 +00001732
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001733 if (PyType_Ready(&PyStdPrinter_Type) < 0)
1734 Py_FatalError("Can't initialize StdPrinter");
Benjamin Petersonfd838e62009-04-20 02:09:13 +00001735
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001736 if (PyType_Ready(&PyCode_Type) < 0)
1737 Py_FatalError("Can't initialize code type");
Benjamin Petersonfd838e62009-04-20 02:09:13 +00001738
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001739 if (PyType_Ready(&PyFrame_Type) < 0)
1740 Py_FatalError("Can't initialize frame type");
Benjamin Petersonfd838e62009-04-20 02:09:13 +00001741
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001742 if (PyType_Ready(&PyCFunction_Type) < 0)
1743 Py_FatalError("Can't initialize builtin function type");
Benjamin Petersonfd838e62009-04-20 02:09:13 +00001744
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001745 if (PyType_Ready(&PyMethod_Type) < 0)
1746 Py_FatalError("Can't initialize method type");
Benjamin Petersonfd838e62009-04-20 02:09:13 +00001747
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001748 if (PyType_Ready(&PyFunction_Type) < 0)
1749 Py_FatalError("Can't initialize function type");
Benjamin Petersonfd838e62009-04-20 02:09:13 +00001750
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001751 if (PyType_Ready(&PyDictProxy_Type) < 0)
1752 Py_FatalError("Can't initialize dict proxy type");
Benjamin Petersonfd838e62009-04-20 02:09:13 +00001753
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001754 if (PyType_Ready(&PyGen_Type) < 0)
1755 Py_FatalError("Can't initialize generator type");
Benjamin Petersonfd838e62009-04-20 02:09:13 +00001756
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001757 if (PyType_Ready(&PyGetSetDescr_Type) < 0)
1758 Py_FatalError("Can't initialize get-set descriptor type");
Benjamin Petersonfd838e62009-04-20 02:09:13 +00001759
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001760 if (PyType_Ready(&PyWrapperDescr_Type) < 0)
1761 Py_FatalError("Can't initialize wrapper type");
Benjamin Petersonfd838e62009-04-20 02:09:13 +00001762
Benjamin Petersoneff61f62011-09-01 16:32:31 -04001763 if (PyType_Ready(&_PyMethodWrapper_Type) < 0)
1764 Py_FatalError("Can't initialize method wrapper type");
1765
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001766 if (PyType_Ready(&PyEllipsis_Type) < 0)
1767 Py_FatalError("Can't initialize ellipsis type");
Benjamin Petersonfd838e62009-04-20 02:09:13 +00001768
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001769 if (PyType_Ready(&PyMemberDescr_Type) < 0)
1770 Py_FatalError("Can't initialize member descriptor type");
Benjamin Peterson8bc5b682009-05-09 18:10:51 +00001771
Barry Warsaw409da152012-06-03 16:18:47 -04001772 if (PyType_Ready(&_PyNamespace_Type) < 0)
1773 Py_FatalError("Can't initialize namespace type");
Benjamin Petersone8ea97f2012-10-30 23:27:52 -04001774
Benjamin Petersonc4311282012-10-30 23:21:10 -04001775 if (PyType_Ready(&PyCapsule_Type) < 0)
1776 Py_FatalError("Can't initialize capsule type");
1777
1778 if (PyType_Ready(&PyLongRangeIter_Type) < 0)
1779 Py_FatalError("Can't initialize long range iterator type");
1780
1781 if (PyType_Ready(&PyCell_Type) < 0)
1782 Py_FatalError("Can't initialize cell type");
1783
1784 if (PyType_Ready(&PyInstanceMethod_Type) < 0)
1785 Py_FatalError("Can't initialize instance method type");
1786
1787 if (PyType_Ready(&PyClassMethodDescr_Type) < 0)
1788 Py_FatalError("Can't initialize class method descr type");
1789
1790 if (PyType_Ready(&PyMethodDescr_Type) < 0)
1791 Py_FatalError("Can't initialize method descr type");
1792
1793 if (PyType_Ready(&PyCallIter_Type) < 0)
1794 Py_FatalError("Can't initialize call iter type");
1795
1796 if (PyType_Ready(&PySeqIter_Type) < 0)
1797 Py_FatalError("Can't initialize sequence iterator type");
Yury Selivanov5376ba92015-06-22 12:19:30 -04001798
1799 if (PyType_Ready(&PyCoro_Type) < 0)
1800 Py_FatalError("Can't initialize coroutine type");
1801
1802 if (PyType_Ready(&_PyCoroWrapper_Type) < 0)
1803 Py_FatalError("Can't initialize coroutine wrapper type");
Guido van Rossumba21a492001-08-16 08:17:26 +00001804}
1805
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001806
Guido van Rossum84a90321996-05-22 16:34:47 +00001807#ifdef Py_TRACE_REFS
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001808
Guido van Rossumaacdc9d1996-08-12 21:32:12 +00001809void
Fred Drake100814d2000-07-09 15:48:49 +00001810_Py_NewReference(PyObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001811{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001812 _Py_INC_REFTOTAL;
1813 op->ob_refcnt = 1;
1814 _Py_AddToAllObjects(op, 1);
1815 _Py_INC_TPALLOCS(op);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001816}
1817
Guido van Rossumaacdc9d1996-08-12 21:32:12 +00001818void
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02001819_Py_ForgetReference(PyObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001820{
Guido van Rossumbffd6832000-01-20 22:32:56 +00001821#ifdef SLOW_UNREF_CHECK
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02001822 PyObject *p;
Guido van Rossumbffd6832000-01-20 22:32:56 +00001823#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001824 if (op->ob_refcnt < 0)
1825 Py_FatalError("UNREF negative refcnt");
1826 if (op == &refchain ||
1827 op->_ob_prev->_ob_next != op || op->_ob_next->_ob_prev != op) {
1828 fprintf(stderr, "* ob\n");
1829 _PyObject_Dump(op);
1830 fprintf(stderr, "* op->_ob_prev->_ob_next\n");
1831 _PyObject_Dump(op->_ob_prev->_ob_next);
1832 fprintf(stderr, "* op->_ob_next->_ob_prev\n");
1833 _PyObject_Dump(op->_ob_next->_ob_prev);
1834 Py_FatalError("UNREF invalid object");
1835 }
Guido van Rossum2e8f6141992-09-03 20:32:55 +00001836#ifdef SLOW_UNREF_CHECK
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001837 for (p = refchain._ob_next; p != &refchain; p = p->_ob_next) {
1838 if (p == op)
1839 break;
1840 }
1841 if (p == &refchain) /* Not found */
1842 Py_FatalError("UNREF unknown object");
Guido van Rossum2e8f6141992-09-03 20:32:55 +00001843#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001844 op->_ob_next->_ob_prev = op->_ob_prev;
1845 op->_ob_prev->_ob_next = op->_ob_next;
1846 op->_ob_next = op->_ob_prev = NULL;
1847 _Py_INC_TPFREES(op);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001848}
1849
Guido van Rossumaacdc9d1996-08-12 21:32:12 +00001850void
Fred Drake100814d2000-07-09 15:48:49 +00001851_Py_Dealloc(PyObject *op)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001852{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001853 destructor dealloc = Py_TYPE(op)->tp_dealloc;
1854 _Py_ForgetReference(op);
1855 (*dealloc)(op);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001856}
1857
Tim Peters269b2a62003-04-17 19:52:29 +00001858/* Print all live objects. Because PyObject_Print is called, the
1859 * interpreter must be in a healthy state.
1860 */
Guido van Rossumaacdc9d1996-08-12 21:32:12 +00001861void
Fred Drake100814d2000-07-09 15:48:49 +00001862_Py_PrintReferences(FILE *fp)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001863{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001864 PyObject *op;
1865 fprintf(fp, "Remaining objects:\n");
1866 for (op = refchain._ob_next; op != &refchain; op = op->_ob_next) {
1867 fprintf(fp, "%p [%" PY_FORMAT_SIZE_T "d] ", op, op->ob_refcnt);
1868 if (PyObject_Print(op, fp, 0) != 0)
1869 PyErr_Clear();
1870 putc('\n', fp);
1871 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001872}
1873
Tim Peters269b2a62003-04-17 19:52:29 +00001874/* Print the addresses of all live objects. Unlike _Py_PrintReferences, this
1875 * doesn't make any calls to the Python C API, so is always safe to call.
1876 */
1877void
1878_Py_PrintReferenceAddresses(FILE *fp)
1879{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001880 PyObject *op;
1881 fprintf(fp, "Remaining object addresses:\n");
1882 for (op = refchain._ob_next; op != &refchain; op = op->_ob_next)
1883 fprintf(fp, "%p [%" PY_FORMAT_SIZE_T "d] %s\n", op,
1884 op->ob_refcnt, Py_TYPE(op)->tp_name);
Tim Peters269b2a62003-04-17 19:52:29 +00001885}
1886
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +00001887PyObject *
Fred Drake100814d2000-07-09 15:48:49 +00001888_Py_GetObjects(PyObject *self, PyObject *args)
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +00001889{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001890 int i, n;
1891 PyObject *t = NULL;
1892 PyObject *res, *op;
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +00001893
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001894 if (!PyArg_ParseTuple(args, "i|O", &n, &t))
1895 return NULL;
1896 op = refchain._ob_next;
1897 res = PyList_New(0);
1898 if (res == NULL)
1899 return NULL;
1900 for (i = 0; (n == 0 || i < n) && op != &refchain; i++) {
1901 while (op == self || op == args || op == res || op == t ||
1902 (t != NULL && Py_TYPE(op) != (PyTypeObject *) t)) {
1903 op = op->_ob_next;
1904 if (op == &refchain)
1905 return res;
1906 }
1907 if (PyList_Append(res, op) < 0) {
1908 Py_DECREF(res);
1909 return NULL;
1910 }
1911 op = op->_ob_next;
1912 }
1913 return res;
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +00001914}
1915
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001916#endif
Guido van Rossum97ead3f1996-01-12 01:24:09 +00001917
Benjamin Petersonb173f782009-05-05 22:31:58 +00001918
Guido van Rossum84a90321996-05-22 16:34:47 +00001919/* Hack to force loading of abstract.o */
Martin v. Löwis18e16552006-02-15 17:27:45 +00001920Py_ssize_t (*_Py_abstract_hack)(PyObject *) = PyObject_Size;
Guido van Rossume09fb551997-08-05 02:04:34 +00001921
1922
David Malcolm49526f42012-06-22 14:55:41 -04001923void
1924_PyObject_DebugTypeStats(FILE *out)
1925{
1926 _PyCFunction_DebugMallocStats(out);
1927 _PyDict_DebugMallocStats(out);
1928 _PyFloat_DebugMallocStats(out);
1929 _PyFrame_DebugMallocStats(out);
1930 _PyList_DebugMallocStats(out);
1931 _PyMethod_DebugMallocStats(out);
David Malcolm49526f42012-06-22 14:55:41 -04001932 _PyTuple_DebugMallocStats(out);
1933}
Guido van Rossumb18618d2000-05-03 23:44:39 +00001934
Guido van Rossum86610361998-04-10 22:32:46 +00001935/* These methods are used to control infinite recursion in repr, str, print,
1936 etc. Container objects that may recursively contain themselves,
Martin Panter8d56c022016-05-29 04:13:35 +00001937 e.g. builtin dictionaries and lists, should use Py_ReprEnter() and
Guido van Rossum86610361998-04-10 22:32:46 +00001938 Py_ReprLeave() to avoid infinite recursion.
1939
1940 Py_ReprEnter() returns 0 the first time it is called for a particular
1941 object and 1 every time thereafter. It returns -1 if an exception
1942 occurred. Py_ReprLeave() has no return value.
1943
1944 See dictobject.c and listobject.c for examples of use.
1945*/
1946
Guido van Rossum86610361998-04-10 22:32:46 +00001947int
Fred Drake100814d2000-07-09 15:48:49 +00001948Py_ReprEnter(PyObject *obj)
Guido van Rossum86610361998-04-10 22:32:46 +00001949{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001950 PyObject *dict;
1951 PyObject *list;
1952 Py_ssize_t i;
Guido van Rossum86610361998-04-10 22:32:46 +00001953
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001954 dict = PyThreadState_GetDict();
Antoine Pitrou04d17d32014-03-31 22:04:38 +02001955 /* Ignore a missing thread-state, so that this function can be called
1956 early on startup. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001957 if (dict == NULL)
1958 return 0;
Victor Stinner7a07e452013-11-06 18:57:29 +01001959 list = _PyDict_GetItemId(dict, &PyId_Py_Repr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001960 if (list == NULL) {
1961 list = PyList_New(0);
1962 if (list == NULL)
1963 return -1;
Victor Stinner7a07e452013-11-06 18:57:29 +01001964 if (_PyDict_SetItemId(dict, &PyId_Py_Repr, list) < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001965 return -1;
1966 Py_DECREF(list);
1967 }
1968 i = PyList_GET_SIZE(list);
1969 while (--i >= 0) {
1970 if (PyList_GET_ITEM(list, i) == obj)
1971 return 1;
1972 }
Victor Stinnere901d1f2013-07-17 21:58:41 +02001973 if (PyList_Append(list, obj) < 0)
1974 return -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001975 return 0;
Guido van Rossum86610361998-04-10 22:32:46 +00001976}
1977
1978void
Fred Drake100814d2000-07-09 15:48:49 +00001979Py_ReprLeave(PyObject *obj)
Guido van Rossum86610361998-04-10 22:32:46 +00001980{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001981 PyObject *dict;
1982 PyObject *list;
1983 Py_ssize_t i;
Victor Stinner1b634932013-07-16 22:24:44 +02001984 PyObject *error_type, *error_value, *error_traceback;
1985
1986 PyErr_Fetch(&error_type, &error_value, &error_traceback);
Guido van Rossum86610361998-04-10 22:32:46 +00001987
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001988 dict = PyThreadState_GetDict();
1989 if (dict == NULL)
Victor Stinner1b634932013-07-16 22:24:44 +02001990 goto finally;
1991
Victor Stinner7a07e452013-11-06 18:57:29 +01001992 list = _PyDict_GetItemId(dict, &PyId_Py_Repr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001993 if (list == NULL || !PyList_Check(list))
Victor Stinner1b634932013-07-16 22:24:44 +02001994 goto finally;
1995
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001996 i = PyList_GET_SIZE(list);
1997 /* Count backwards because we always expect obj to be list[-1] */
1998 while (--i >= 0) {
1999 if (PyList_GET_ITEM(list, i) == obj) {
2000 PyList_SetSlice(list, i, i + 1, NULL);
2001 break;
2002 }
2003 }
Victor Stinner1b634932013-07-16 22:24:44 +02002004
2005finally:
2006 /* ignore exceptions because there is no way to report them. */
2007 PyErr_Restore(error_type, error_value, error_traceback);
Guido van Rossum86610361998-04-10 22:32:46 +00002008}
Guido van Rossumd724b232000-03-13 16:01:29 +00002009
Tim Peters803526b2002-07-07 05:13:56 +00002010/* Trashcan support. */
Guido van Rossumd724b232000-03-13 16:01:29 +00002011
Tim Peters803526b2002-07-07 05:13:56 +00002012/* Add op to the _PyTrash_delete_later list. Called when the current
2013 * call-stack depth gets large. op must be a currently untracked gc'ed
2014 * object, with refcount 0. Py_DECREF must already have been called on it.
2015 */
Guido van Rossumd724b232000-03-13 16:01:29 +00002016void
Fred Drake100814d2000-07-09 15:48:49 +00002017_PyTrash_deposit_object(PyObject *op)
Guido van Rossumd724b232000-03-13 16:01:29 +00002018{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002019 assert(PyObject_IS_GC(op));
Antoine Pitrou796564c2013-07-30 19:59:21 +02002020 assert(_PyGC_REFS(op) == _PyGC_REFS_UNTRACKED);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002021 assert(op->ob_refcnt == 0);
Eric Snow2ebc5ce2017-09-07 23:51:28 -06002022 _Py_AS_GC(op)->gc.gc_prev = (PyGC_Head *)_PyRuntime.gc.trash_delete_later;
2023 _PyRuntime.gc.trash_delete_later = op;
Guido van Rossumd724b232000-03-13 16:01:29 +00002024}
2025
Antoine Pitrou2b0218a2012-09-06 00:59:49 +02002026/* The equivalent API, using per-thread state recursion info */
2027void
2028_PyTrash_thread_deposit_object(PyObject *op)
2029{
2030 PyThreadState *tstate = PyThreadState_GET();
2031 assert(PyObject_IS_GC(op));
Antoine Pitrou796564c2013-07-30 19:59:21 +02002032 assert(_PyGC_REFS(op) == _PyGC_REFS_UNTRACKED);
Antoine Pitrou2b0218a2012-09-06 00:59:49 +02002033 assert(op->ob_refcnt == 0);
2034 _Py_AS_GC(op)->gc.gc_prev = (PyGC_Head *) tstate->trash_delete_later;
2035 tstate->trash_delete_later = op;
2036}
2037
Tim Peters803526b2002-07-07 05:13:56 +00002038/* Dealloccate all the objects in the _PyTrash_delete_later list. Called when
2039 * the call-stack unwinds again.
2040 */
Guido van Rossumd724b232000-03-13 16:01:29 +00002041void
Fred Drake100814d2000-07-09 15:48:49 +00002042_PyTrash_destroy_chain(void)
Guido van Rossumd724b232000-03-13 16:01:29 +00002043{
Eric Snow2ebc5ce2017-09-07 23:51:28 -06002044 while (_PyRuntime.gc.trash_delete_later) {
2045 PyObject *op = _PyRuntime.gc.trash_delete_later;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002046 destructor dealloc = Py_TYPE(op)->tp_dealloc;
Neil Schemenauerf589c052002-03-29 03:05:54 +00002047
Eric Snow2ebc5ce2017-09-07 23:51:28 -06002048 _PyRuntime.gc.trash_delete_later =
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002049 (PyObject*) _Py_AS_GC(op)->gc.gc_prev;
Neil Schemenauerf589c052002-03-29 03:05:54 +00002050
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002051 /* Call the deallocator directly. This used to try to
2052 * fool Py_DECREF into calling it indirectly, but
2053 * Py_DECREF was already called on this object, and in
2054 * assorted non-release builds calling Py_DECREF again ends
2055 * up distorting allocation statistics.
2056 */
2057 assert(op->ob_refcnt == 0);
Eric Snow2ebc5ce2017-09-07 23:51:28 -06002058 ++_PyRuntime.gc.trash_delete_nesting;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002059 (*dealloc)(op);
Eric Snow2ebc5ce2017-09-07 23:51:28 -06002060 --_PyRuntime.gc.trash_delete_nesting;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002061 }
Guido van Rossumd724b232000-03-13 16:01:29 +00002062}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002063
Antoine Pitrou2b0218a2012-09-06 00:59:49 +02002064/* The equivalent API, using per-thread state recursion info */
2065void
2066_PyTrash_thread_destroy_chain(void)
2067{
2068 PyThreadState *tstate = PyThreadState_GET();
Xiang Zhanga66f9c62017-05-13 13:36:14 +08002069 /* We need to increase trash_delete_nesting here, otherwise,
2070 _PyTrash_thread_destroy_chain will be called recursively
2071 and then possibly crash. An example that may crash without
2072 increase:
2073 N = 500000 # need to be large enough
2074 ob = object()
2075 tups = [(ob,) for i in range(N)]
2076 for i in range(49):
2077 tups = [(tup,) for tup in tups]
2078 del tups
2079 */
2080 assert(tstate->trash_delete_nesting == 0);
2081 ++tstate->trash_delete_nesting;
Antoine Pitrou2b0218a2012-09-06 00:59:49 +02002082 while (tstate->trash_delete_later) {
2083 PyObject *op = tstate->trash_delete_later;
2084 destructor dealloc = Py_TYPE(op)->tp_dealloc;
2085
2086 tstate->trash_delete_later =
2087 (PyObject*) _Py_AS_GC(op)->gc.gc_prev;
2088
2089 /* Call the deallocator directly. This used to try to
2090 * fool Py_DECREF into calling it indirectly, but
2091 * Py_DECREF was already called on this object, and in
2092 * assorted non-release builds calling Py_DECREF again ends
2093 * up distorting allocation statistics.
2094 */
2095 assert(op->ob_refcnt == 0);
Antoine Pitrou2b0218a2012-09-06 00:59:49 +02002096 (*dealloc)(op);
Xiang Zhanga66f9c62017-05-13 13:36:14 +08002097 assert(tstate->trash_delete_nesting == 1);
Antoine Pitrou2b0218a2012-09-06 00:59:49 +02002098 }
Xiang Zhanga66f9c62017-05-13 13:36:14 +08002099 --tstate->trash_delete_nesting;
Antoine Pitrou2b0218a2012-09-06 00:59:49 +02002100}
2101
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00002102#ifndef Py_TRACE_REFS
2103/* For Py_LIMITED_API, we need an out-of-line version of _Py_Dealloc.
2104 Define this here, so we can undefine the macro. */
2105#undef _Py_Dealloc
2106PyAPI_FUNC(void) _Py_Dealloc(PyObject *);
2107void
2108_Py_Dealloc(PyObject *op)
2109{
2110 _Py_INC_TPFREES(op) _Py_COUNT_ALLOCS_COMMA
2111 (*Py_TYPE(op)->tp_dealloc)(op);
2112}
2113#endif
2114
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002115#ifdef __cplusplus
2116}
2117#endif