blob: 559794f5b4c4033ec64848573a8dfbaceec16678 [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001
Benjamin Peterson722954a2011-06-11 16:33:35 -05002/* Generic object operations; and implementation of None */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003
Guido van Rossumc0b618a1997-05-02 03:12:38 +00004#include "Python.h"
Benjamin Petersonfd838e62009-04-20 02:09:13 +00005#include "frameobject.h"
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00006
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007#ifdef __cplusplus
8extern "C" {
9#endif
10
Victor Stinnerbd303c12013-11-07 23:07:29 +010011_Py_IDENTIFIER(Py_Repr);
12_Py_IDENTIFIER(__bytes__);
13_Py_IDENTIFIER(__dir__);
14_Py_IDENTIFIER(__isabstractmethod__);
15_Py_IDENTIFIER(builtins);
16
Tim Peters34592512002-07-11 06:23:50 +000017#ifdef Py_REF_DEBUG
Neal Norwitz84632ee2006-03-04 20:00:59 +000018Py_ssize_t _Py_RefTotal;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000019
20Py_ssize_t
21_Py_GetRefTotal(void)
22{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000023 PyObject *o;
24 Py_ssize_t total = _Py_RefTotal;
25 /* ignore the references to the dummy object of the dicts and sets
26 because they are not reliable and not useful (now that the
27 hash table code is well-tested) */
28 o = _PyDict_Dummy();
29 if (o != NULL)
30 total -= o->ob_refcnt;
Antoine Pitrou9d952542013-08-24 21:07:07 +020031 o = _PySet_Dummy;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000032 if (o != NULL)
33 total -= o->ob_refcnt;
34 return total;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000035}
Nick Coghland6009512014-11-20 21:39:37 +100036
37void
38_PyDebug_PrintTotalRefs(void) {
39 PyObject *xoptions, *value;
40 _Py_IDENTIFIER(showrefcount);
41
42 xoptions = PySys_GetXOptions();
43 if (xoptions == NULL)
44 return;
45 value = _PyDict_GetItemId(xoptions, &PyId_showrefcount);
46 if (value == Py_True)
47 fprintf(stderr,
48 "[%" PY_FORMAT_SIZE_T "d refs, "
49 "%" PY_FORMAT_SIZE_T "d blocks]\n",
50 _Py_GetRefTotal(), _Py_GetAllocatedBlocks());
51}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000052#endif /* Py_REF_DEBUG */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000053
Guido van Rossum3f5da241990-12-20 15:06:42 +000054/* Object allocation routines used by NEWOBJ and NEWVAROBJ macros.
55 These are used by the individual routines for object creation.
56 Do not call them otherwise, they do not initialize the object! */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000057
Tim Peters78be7992003-03-23 02:51:01 +000058#ifdef Py_TRACE_REFS
Tim Peters7571a0f2003-03-23 17:52:28 +000059/* Head of circular doubly-linked list of all objects. These are linked
60 * together via the _ob_prev and _ob_next members of a PyObject, which
61 * exist only in a Py_TRACE_REFS build.
62 */
Tim Peters78be7992003-03-23 02:51:01 +000063static PyObject refchain = {&refchain, &refchain};
Tim Peters36eb4df2003-03-23 03:33:13 +000064
Tim Peters7571a0f2003-03-23 17:52:28 +000065/* Insert op at the front of the list of all objects. If force is true,
66 * op is added even if _ob_prev and _ob_next are non-NULL already. If
67 * force is false amd _ob_prev or _ob_next are non-NULL, do nothing.
68 * force should be true if and only if op points to freshly allocated,
69 * uninitialized memory, or you've unlinked op from the list and are
Tim Peters51f8d382003-03-23 18:06:08 +000070 * relinking it into the front.
Tim Peters7571a0f2003-03-23 17:52:28 +000071 * Note that objects are normally added to the list via _Py_NewReference,
72 * which is called by PyObject_Init. Not all objects are initialized that
73 * way, though; exceptions include statically allocated type objects, and
74 * statically allocated singletons (like Py_True and Py_None).
75 */
Tim Peters36eb4df2003-03-23 03:33:13 +000076void
Tim Peters7571a0f2003-03-23 17:52:28 +000077_Py_AddToAllObjects(PyObject *op, int force)
Tim Peters36eb4df2003-03-23 03:33:13 +000078{
Tim Peters7571a0f2003-03-23 17:52:28 +000079#ifdef Py_DEBUG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000080 if (!force) {
81 /* If it's initialized memory, op must be in or out of
82 * the list unambiguously.
83 */
84 assert((op->_ob_prev == NULL) == (op->_ob_next == NULL));
85 }
Tim Peters78be7992003-03-23 02:51:01 +000086#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000087 if (force || op->_ob_prev == NULL) {
88 op->_ob_next = refchain._ob_next;
89 op->_ob_prev = &refchain;
90 refchain._ob_next->_ob_prev = op;
91 refchain._ob_next = op;
92 }
Tim Peters7571a0f2003-03-23 17:52:28 +000093}
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000094#endif /* Py_TRACE_REFS */
Tim Peters78be7992003-03-23 02:51:01 +000095
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +000096#ifdef COUNT_ALLOCS
Guido van Rossumc0b618a1997-05-02 03:12:38 +000097static PyTypeObject *type_list;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000098/* All types are added to type_list, at least when
99 they get one object created. That makes them
100 immortal, which unfortunately contributes to
101 garbage itself. If unlist_types_without_objects
102 is set, they will be removed from the type_list
103 once the last object is deallocated. */
Benjamin Petersona4a37fe2009-01-11 17:13:55 +0000104static int unlist_types_without_objects;
105extern Py_ssize_t tuple_zero_allocs, fast_tuple_allocs;
106extern Py_ssize_t quick_int_allocs, quick_neg_int_allocs;
107extern Py_ssize_t null_strings, one_strings;
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +0000108void
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000109dump_counts(FILE* f)
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +0000110{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000111 PyTypeObject *tp;
Serhiy Storchaka7e160ce2016-07-03 21:03:53 +0300112 PyObject *xoptions, *value;
113 _Py_IDENTIFIER(showalloccount);
114
115 xoptions = PySys_GetXOptions();
116 if (xoptions == NULL)
117 return;
118 value = _PyDict_GetItemId(xoptions, &PyId_showalloccount);
119 if (value != Py_True)
120 return;
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +0000121
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000122 for (tp = type_list; tp; tp = tp->tp_next)
123 fprintf(f, "%s alloc'd: %" PY_FORMAT_SIZE_T "d, "
124 "freed: %" PY_FORMAT_SIZE_T "d, "
125 "max in use: %" PY_FORMAT_SIZE_T "d\n",
126 tp->tp_name, tp->tp_allocs, tp->tp_frees,
127 tp->tp_maxalloc);
128 fprintf(f, "fast tuple allocs: %" PY_FORMAT_SIZE_T "d, "
129 "empty: %" PY_FORMAT_SIZE_T "d\n",
130 fast_tuple_allocs, tuple_zero_allocs);
131 fprintf(f, "fast int allocs: pos: %" PY_FORMAT_SIZE_T "d, "
132 "neg: %" PY_FORMAT_SIZE_T "d\n",
133 quick_int_allocs, quick_neg_int_allocs);
134 fprintf(f, "null strings: %" PY_FORMAT_SIZE_T "d, "
135 "1-strings: %" PY_FORMAT_SIZE_T "d\n",
136 null_strings, one_strings);
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +0000137}
138
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000139PyObject *
Fred Drake100814d2000-07-09 15:48:49 +0000140get_counts(void)
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000141{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000142 PyTypeObject *tp;
143 PyObject *result;
144 PyObject *v;
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000145
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000146 result = PyList_New(0);
147 if (result == NULL)
148 return NULL;
149 for (tp = type_list; tp; tp = tp->tp_next) {
150 v = Py_BuildValue("(snnn)", tp->tp_name, tp->tp_allocs,
151 tp->tp_frees, tp->tp_maxalloc);
152 if (v == NULL) {
153 Py_DECREF(result);
154 return NULL;
155 }
156 if (PyList_Append(result, v) < 0) {
157 Py_DECREF(v);
158 Py_DECREF(result);
159 return NULL;
160 }
161 Py_DECREF(v);
162 }
163 return result;
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000164}
165
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +0000166void
Fred Drake100814d2000-07-09 15:48:49 +0000167inc_count(PyTypeObject *tp)
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +0000168{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000169 if (tp->tp_next == NULL && tp->tp_prev == NULL) {
170 /* first time; insert in linked list */
171 if (tp->tp_next != NULL) /* sanity check */
172 Py_FatalError("XXX inc_count sanity check");
173 if (type_list)
174 type_list->tp_prev = tp;
175 tp->tp_next = type_list;
176 /* Note that as of Python 2.2, heap-allocated type objects
177 * can go away, but this code requires that they stay alive
178 * until program exit. That's why we're careful with
179 * refcounts here. type_list gets a new reference to tp,
180 * while ownership of the reference type_list used to hold
181 * (if any) was transferred to tp->tp_next in the line above.
182 * tp is thus effectively immortal after this.
183 */
184 Py_INCREF(tp);
185 type_list = tp;
Tim Peters3e40c7f2003-03-23 03:04:32 +0000186#ifdef Py_TRACE_REFS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000187 /* Also insert in the doubly-linked list of all objects,
188 * if not already there.
189 */
190 _Py_AddToAllObjects((PyObject *)tp, 0);
Tim Peters78be7992003-03-23 02:51:01 +0000191#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000192 }
193 tp->tp_allocs++;
194 if (tp->tp_allocs - tp->tp_frees > tp->tp_maxalloc)
195 tp->tp_maxalloc = tp->tp_allocs - tp->tp_frees;
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +0000196}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000197
198void dec_count(PyTypeObject *tp)
199{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000200 tp->tp_frees++;
201 if (unlist_types_without_objects &&
202 tp->tp_allocs == tp->tp_frees) {
203 /* unlink the type from type_list */
204 if (tp->tp_prev)
205 tp->tp_prev->tp_next = tp->tp_next;
206 else
207 type_list = tp->tp_next;
208 if (tp->tp_next)
209 tp->tp_next->tp_prev = tp->tp_prev;
210 tp->tp_next = tp->tp_prev = NULL;
211 Py_DECREF(tp);
212 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000213}
214
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +0000215#endif
216
Tim Peters7c321a82002-07-09 02:57:01 +0000217#ifdef Py_REF_DEBUG
218/* Log a fatal error; doesn't return. */
219void
220_Py_NegativeRefcount(const char *fname, int lineno, PyObject *op)
221{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000222 char buf[300];
Tim Peters7c321a82002-07-09 02:57:01 +0000223
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000224 PyOS_snprintf(buf, sizeof(buf),
225 "%s:%i object at %p has negative ref count "
226 "%" PY_FORMAT_SIZE_T "d",
227 fname, lineno, op, op->ob_refcnt);
228 Py_FatalError(buf);
Tim Peters7c321a82002-07-09 02:57:01 +0000229}
230
231#endif /* Py_REF_DEBUG */
232
Thomas Heller1328b522004-04-22 17:23:49 +0000233void
234Py_IncRef(PyObject *o)
235{
236 Py_XINCREF(o);
237}
238
239void
240Py_DecRef(PyObject *o)
241{
242 Py_XDECREF(o);
243}
244
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000245PyObject *
Fred Drake100814d2000-07-09 15:48:49 +0000246PyObject_Init(PyObject *op, PyTypeObject *tp)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000247{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000248 if (op == NULL)
249 return PyErr_NoMemory();
250 /* Any changes should be reflected in PyObject_INIT (objimpl.h) */
251 Py_TYPE(op) = tp;
252 _Py_NewReference(op);
253 return op;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000254}
255
Guido van Rossumb18618d2000-05-03 23:44:39 +0000256PyVarObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000257PyObject_InitVar(PyVarObject *op, PyTypeObject *tp, Py_ssize_t size)
Guido van Rossumb18618d2000-05-03 23:44:39 +0000258{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000259 if (op == NULL)
260 return (PyVarObject *) PyErr_NoMemory();
261 /* Any changes should be reflected in PyObject_INIT_VAR */
262 op->ob_size = size;
263 Py_TYPE(op) = tp;
264 _Py_NewReference((PyObject *)op);
265 return op;
Guido van Rossumb18618d2000-05-03 23:44:39 +0000266}
267
268PyObject *
Fred Drake100814d2000-07-09 15:48:49 +0000269_PyObject_New(PyTypeObject *tp)
Guido van Rossumb18618d2000-05-03 23:44:39 +0000270{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000271 PyObject *op;
272 op = (PyObject *) PyObject_MALLOC(_PyObject_SIZE(tp));
273 if (op == NULL)
274 return PyErr_NoMemory();
275 return PyObject_INIT(op, tp);
Guido van Rossumb18618d2000-05-03 23:44:39 +0000276}
277
Guido van Rossumd0c87ee1997-05-15 21:31:03 +0000278PyVarObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000279_PyObject_NewVar(PyTypeObject *tp, Py_ssize_t nitems)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000280{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000281 PyVarObject *op;
282 const size_t size = _PyObject_VAR_SIZE(tp, nitems);
283 op = (PyVarObject *) PyObject_MALLOC(size);
284 if (op == NULL)
285 return (PyVarObject *)PyErr_NoMemory();
286 return PyObject_INIT_VAR(op, tp, nitems);
Guido van Rossumb18618d2000-05-03 23:44:39 +0000287}
288
Antoine Pitrou796564c2013-07-30 19:59:21 +0200289void
290PyObject_CallFinalizer(PyObject *self)
291{
292 PyTypeObject *tp = Py_TYPE(self);
293
294 /* The former could happen on heaptypes created from the C API, e.g.
295 PyType_FromSpec(). */
296 if (!PyType_HasFeature(tp, Py_TPFLAGS_HAVE_FINALIZE) ||
297 tp->tp_finalize == NULL)
298 return;
299 /* tp_finalize should only be called once. */
300 if (PyType_IS_GC(tp) && _PyGC_FINALIZED(self))
301 return;
302
303 tp->tp_finalize(self);
304 if (PyType_IS_GC(tp))
305 _PyGC_SET_FINALIZED(self, 1);
306}
307
308int
309PyObject_CallFinalizerFromDealloc(PyObject *self)
310{
311 Py_ssize_t refcnt;
312
313 /* Temporarily resurrect the object. */
314 if (self->ob_refcnt != 0) {
315 Py_FatalError("PyObject_CallFinalizerFromDealloc called on "
316 "object with a non-zero refcount");
317 }
318 self->ob_refcnt = 1;
319
320 PyObject_CallFinalizer(self);
321
322 /* Undo the temporary resurrection; can't use DECREF here, it would
323 * cause a recursive call.
324 */
325 assert(self->ob_refcnt > 0);
326 if (--self->ob_refcnt == 0)
327 return 0; /* this is the normal path out */
328
329 /* tp_finalize resurrected it! Make it look like the original Py_DECREF
330 * never happened.
331 */
332 refcnt = self->ob_refcnt;
333 _Py_NewReference(self);
334 self->ob_refcnt = refcnt;
335
336 if (PyType_IS_GC(Py_TYPE(self))) {
337 assert(_PyGC_REFS(self) != _PyGC_REFS_UNTRACKED);
338 }
339 /* If Py_REF_DEBUG, _Py_NewReference bumped _Py_RefTotal, so
340 * we need to undo that. */
341 _Py_DEC_REFTOTAL;
342 /* If Py_TRACE_REFS, _Py_NewReference re-added self to the object
343 * chain, so no more to do there.
344 * If COUNT_ALLOCS, the original decref bumped tp_frees, and
345 * _Py_NewReference bumped tp_allocs: both of those need to be
346 * undone.
347 */
348#ifdef COUNT_ALLOCS
349 --Py_TYPE(self)->tp_frees;
350 --Py_TYPE(self)->tp_allocs;
351#endif
352 return -1;
353}
354
Antoine Pitrouc47bd4a2010-07-27 22:08:27 +0000355int
356PyObject_Print(PyObject *op, FILE *fp, int flags)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000357{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000358 int ret = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000359 if (PyErr_CheckSignals())
360 return -1;
Guido van Rossum9b00dfa1998-04-28 16:06:54 +0000361#ifdef USE_STACKCHECK
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000362 if (PyOS_CheckStack()) {
363 PyErr_SetString(PyExc_MemoryError, "stack overflow");
364 return -1;
365 }
Guido van Rossum9b00dfa1998-04-28 16:06:54 +0000366#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000367 clearerr(fp); /* Clear any previous error condition */
368 if (op == NULL) {
369 Py_BEGIN_ALLOW_THREADS
370 fprintf(fp, "<nil>");
371 Py_END_ALLOW_THREADS
372 }
373 else {
374 if (op->ob_refcnt <= 0)
375 /* XXX(twouters) cast refcount to long until %zd is
376 universally available */
377 Py_BEGIN_ALLOW_THREADS
378 fprintf(fp, "<refcnt %ld at %p>",
379 (long)op->ob_refcnt, op);
380 Py_END_ALLOW_THREADS
381 else {
382 PyObject *s;
383 if (flags & Py_PRINT_RAW)
384 s = PyObject_Str(op);
385 else
386 s = PyObject_Repr(op);
387 if (s == NULL)
388 ret = -1;
389 else if (PyBytes_Check(s)) {
390 fwrite(PyBytes_AS_STRING(s), 1,
391 PyBytes_GET_SIZE(s), fp);
392 }
393 else if (PyUnicode_Check(s)) {
394 PyObject *t;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200395 t = PyUnicode_AsEncodedString(s, "utf-8", "backslashreplace");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000396 if (t == NULL)
397 ret = 0;
398 else {
399 fwrite(PyBytes_AS_STRING(t), 1,
400 PyBytes_GET_SIZE(t), fp);
Victor Stinnerba6b4302010-05-17 09:33:42 +0000401 Py_DECREF(t);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000402 }
403 }
404 else {
405 PyErr_Format(PyExc_TypeError,
406 "str() or repr() returned '%.100s'",
407 s->ob_type->tp_name);
408 ret = -1;
409 }
410 Py_XDECREF(s);
411 }
412 }
413 if (ret == 0) {
414 if (ferror(fp)) {
415 PyErr_SetFromErrno(PyExc_IOError);
416 clearerr(fp);
417 ret = -1;
418 }
419 }
420 return ret;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000421}
422
Guido van Rossum38938152006-08-21 23:36:26 +0000423/* For debugging convenience. Set a breakpoint here and call it from your DLL */
424void
Thomas Woutersb2137042007-02-01 18:02:27 +0000425_Py_BreakPoint(void)
Guido van Rossum38938152006-08-21 23:36:26 +0000426{
427}
428
Neal Norwitz1a997502003-01-13 20:13:12 +0000429
Barry Warsaw9bf16442001-01-23 16:24:35 +0000430/* For debugging convenience. See Misc/gdbinit for some useful gdb hooks */
Guido van Rossum38938152006-08-21 23:36:26 +0000431void
432_PyObject_Dump(PyObject* op)
Barry Warsaw9bf16442001-01-23 16:24:35 +0000433{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000434 if (op == NULL)
435 fprintf(stderr, "NULL\n");
436 else {
Georg Brandldfd73442009-04-05 11:47:34 +0000437#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000438 PyGILState_STATE gil;
Georg Brandldfd73442009-04-05 11:47:34 +0000439#endif
Victor Stinnere5132102013-08-26 13:49:06 +0200440 PyObject *error_type, *error_value, *error_traceback;
441
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000442 fprintf(stderr, "object : ");
Georg Brandldfd73442009-04-05 11:47:34 +0000443#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000444 gil = PyGILState_Ensure();
Georg Brandldfd73442009-04-05 11:47:34 +0000445#endif
Victor Stinnere5132102013-08-26 13:49:06 +0200446
447 PyErr_Fetch(&error_type, &error_value, &error_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000448 (void)PyObject_Print(op, stderr, 0);
Victor Stinnere5132102013-08-26 13:49:06 +0200449 PyErr_Restore(error_type, error_value, error_traceback);
450
Georg Brandldfd73442009-04-05 11:47:34 +0000451#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000452 PyGILState_Release(gil);
Georg Brandldfd73442009-04-05 11:47:34 +0000453#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000454 /* XXX(twouters) cast refcount to long until %zd is
455 universally available */
456 fprintf(stderr, "\n"
457 "type : %s\n"
458 "refcount: %ld\n"
459 "address : %p\n",
460 Py_TYPE(op)==NULL ? "NULL" : Py_TYPE(op)->tp_name,
461 (long)op->ob_refcnt,
462 op);
463 }
Barry Warsaw9bf16442001-01-23 16:24:35 +0000464}
Barry Warsaw903138f2001-01-23 16:33:18 +0000465
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000466PyObject *
Fred Drake100814d2000-07-09 15:48:49 +0000467PyObject_Repr(PyObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000468{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000469 PyObject *res;
470 if (PyErr_CheckSignals())
471 return NULL;
Guido van Rossum9b00dfa1998-04-28 16:06:54 +0000472#ifdef USE_STACKCHECK
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000473 if (PyOS_CheckStack()) {
474 PyErr_SetString(PyExc_MemoryError, "stack overflow");
475 return NULL;
476 }
Guido van Rossum9b00dfa1998-04-28 16:06:54 +0000477#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000478 if (v == NULL)
479 return PyUnicode_FromString("<NULL>");
480 if (Py_TYPE(v)->tp_repr == NULL)
481 return PyUnicode_FromFormat("<%s object at %p>",
482 v->ob_type->tp_name, v);
Victor Stinner33824f62013-08-26 14:05:19 +0200483
484#ifdef Py_DEBUG
485 /* PyObject_Repr() must not be called with an exception set,
486 because it may clear it (directly or indirectly) and so the
Martin Panter9955a372015-10-07 10:26:23 +0000487 caller loses its exception */
Victor Stinner33824f62013-08-26 14:05:19 +0200488 assert(!PyErr_Occurred());
489#endif
490
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000491 res = (*v->ob_type->tp_repr)(v);
Victor Stinner0a54cf12011-12-01 03:22:44 +0100492 if (res == NULL)
493 return NULL;
494 if (!PyUnicode_Check(res)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000495 PyErr_Format(PyExc_TypeError,
496 "__repr__ returned non-string (type %.200s)",
497 res->ob_type->tp_name);
498 Py_DECREF(res);
499 return NULL;
500 }
Victor Stinnerdb88ae52011-12-01 02:15:00 +0100501#ifndef Py_DEBUG
502 if (PyUnicode_READY(res) < 0)
503 return NULL;
504#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000505 return res;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000506}
507
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000508PyObject *
Guido van Rossum98297ee2007-11-06 21:34:58 +0000509PyObject_Str(PyObject *v)
Guido van Rossumc6004111993-11-05 10:22:19 +0000510{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000511 PyObject *res;
512 if (PyErr_CheckSignals())
513 return NULL;
Guido van Rossum98297ee2007-11-06 21:34:58 +0000514#ifdef USE_STACKCHECK
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000515 if (PyOS_CheckStack()) {
516 PyErr_SetString(PyExc_MemoryError, "stack overflow");
517 return NULL;
518 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000519#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000520 if (v == NULL)
521 return PyUnicode_FromString("<NULL>");
522 if (PyUnicode_CheckExact(v)) {
Victor Stinnerdb88ae52011-12-01 02:15:00 +0100523#ifndef Py_DEBUG
Victor Stinner4ead7c72011-11-20 19:48:36 +0100524 if (PyUnicode_READY(v) < 0)
525 return NULL;
Victor Stinnerdb88ae52011-12-01 02:15:00 +0100526#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000527 Py_INCREF(v);
528 return v;
529 }
530 if (Py_TYPE(v)->tp_str == NULL)
531 return PyObject_Repr(v);
Guido van Rossum4f288ab2001-05-01 16:53:37 +0000532
Victor Stinner33824f62013-08-26 14:05:19 +0200533#ifdef Py_DEBUG
534 /* PyObject_Str() must not be called with an exception set,
535 because it may clear it (directly or indirectly) and so the
Nick Coghland979e432014-02-09 10:43:21 +1000536 caller loses its exception */
Victor Stinner33824f62013-08-26 14:05:19 +0200537 assert(!PyErr_Occurred());
538#endif
539
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000540 /* It is possible for a type to have a tp_str representation that loops
541 infinitely. */
542 if (Py_EnterRecursiveCall(" while getting the str of an object"))
543 return NULL;
544 res = (*Py_TYPE(v)->tp_str)(v);
545 Py_LeaveRecursiveCall();
546 if (res == NULL)
547 return NULL;
548 if (!PyUnicode_Check(res)) {
549 PyErr_Format(PyExc_TypeError,
550 "__str__ returned non-string (type %.200s)",
551 Py_TYPE(res)->tp_name);
552 Py_DECREF(res);
553 return NULL;
554 }
Victor Stinnerdb88ae52011-12-01 02:15:00 +0100555#ifndef Py_DEBUG
Victor Stinner4ead7c72011-11-20 19:48:36 +0100556 if (PyUnicode_READY(res) < 0)
557 return NULL;
Victor Stinnerdb88ae52011-12-01 02:15:00 +0100558#endif
Victor Stinner4ead7c72011-11-20 19:48:36 +0100559 assert(_PyUnicode_CheckConsistency(res, 1));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000560 return res;
Neil Schemenauercf52c072005-08-12 17:34:58 +0000561}
562
Georg Brandl559e5d72008-06-11 18:37:52 +0000563PyObject *
564PyObject_ASCII(PyObject *v)
565{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000566 PyObject *repr, *ascii, *res;
Georg Brandl559e5d72008-06-11 18:37:52 +0000567
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000568 repr = PyObject_Repr(v);
569 if (repr == NULL)
570 return NULL;
Georg Brandl559e5d72008-06-11 18:37:52 +0000571
Victor Stinneraf037572013-04-14 18:44:10 +0200572 if (PyUnicode_IS_ASCII(repr))
573 return repr;
574
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000575 /* repr is guaranteed to be a PyUnicode object by PyObject_Repr */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200576 ascii = _PyUnicode_AsASCIIString(repr, "backslashreplace");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000577 Py_DECREF(repr);
578 if (ascii == NULL)
579 return NULL;
Georg Brandl559e5d72008-06-11 18:37:52 +0000580
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000581 res = PyUnicode_DecodeASCII(
582 PyBytes_AS_STRING(ascii),
583 PyBytes_GET_SIZE(ascii),
584 NULL);
585
586 Py_DECREF(ascii);
587 return res;
Georg Brandl559e5d72008-06-11 18:37:52 +0000588}
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000589
Benjamin Petersonc15a0732008-08-26 16:46:47 +0000590PyObject *
591PyObject_Bytes(PyObject *v)
592{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000593 PyObject *result, *func;
Benjamin Petersonc15a0732008-08-26 16:46:47 +0000594
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000595 if (v == NULL)
596 return PyBytes_FromString("<NULL>");
Benjamin Petersonc15a0732008-08-26 16:46:47 +0000597
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000598 if (PyBytes_CheckExact(v)) {
599 Py_INCREF(v);
600 return v;
601 }
Benjamin Petersonc15a0732008-08-26 16:46:47 +0000602
Benjamin Petersonce798522012-01-22 11:24:29 -0500603 func = _PyObject_LookupSpecial(v, &PyId___bytes__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000604 if (func != NULL) {
605 result = PyObject_CallFunctionObjArgs(func, NULL);
606 Py_DECREF(func);
607 if (result == NULL)
Benjamin Peterson41ece392010-09-11 16:39:57 +0000608 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000609 if (!PyBytes_Check(result)) {
Benjamin Peterson41ece392010-09-11 16:39:57 +0000610 PyErr_Format(PyExc_TypeError,
611 "__bytes__ returned non-bytes (type %.200s)",
612 Py_TYPE(result)->tp_name);
613 Py_DECREF(result);
614 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000615 }
616 return result;
617 }
618 else if (PyErr_Occurred())
619 return NULL;
620 return PyBytes_FromObject(v);
Benjamin Petersonc15a0732008-08-26 16:46:47 +0000621}
622
Mark Dickinsonc008a172009-02-01 13:59:22 +0000623/* For Python 3.0.1 and later, the old three-way comparison has been
624 completely removed in favour of rich comparisons. PyObject_Compare() and
625 PyObject_Cmp() are gone, and the builtin cmp function no longer exists.
Mark Dickinsone94c6792009-02-02 20:36:42 +0000626 The old tp_compare slot has been renamed to tp_reserved, and should no
Mark Dickinsonc008a172009-02-01 13:59:22 +0000627 longer be used. Use tp_richcompare instead.
Guido van Rossum98297ee2007-11-06 21:34:58 +0000628
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000629 See (*) below for practical amendments.
630
Mark Dickinsonc008a172009-02-01 13:59:22 +0000631 tp_richcompare gets called with a first argument of the appropriate type
632 and a second object of an arbitrary type. We never do any kind of
633 coercion.
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000634
Mark Dickinsonc008a172009-02-01 13:59:22 +0000635 The tp_richcompare slot should return an object, as follows:
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000636
637 NULL if an exception occurred
638 NotImplemented if the requested comparison is not implemented
639 any other false value if the requested comparison is false
640 any other true value if the requested comparison is true
641
642 The PyObject_RichCompare[Bool]() wrappers raise TypeError when they get
643 NotImplemented.
644
645 (*) Practical amendments:
646
647 - If rich comparison returns NotImplemented, == and != are decided by
648 comparing the object pointer (i.e. falling back to the base object
649 implementation).
650
Guido van Rossuma4073002002-05-31 20:03:54 +0000651*/
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000652
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000653/* Map rich comparison operators to their swapped version, e.g. LT <--> GT */
Brett Cannona5ca2e72004-09-25 01:37:24 +0000654int _Py_SwappedOp[] = {Py_GT, Py_GE, Py_EQ, Py_NE, Py_LT, Py_LE};
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +0000655
Serhiy Storchaka2d06e842015-12-25 19:53:18 +0200656static const char * const opstrings[] = {"<", "<=", "==", "!=", ">", ">="};
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000657
658/* Perform a rich comparison, raising TypeError when the requested comparison
659 operator is not supported. */
Guido van Rossume797ec12001-01-17 15:24:28 +0000660static PyObject *
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000661do_richcompare(PyObject *v, PyObject *w, int op)
Guido van Rossume797ec12001-01-17 15:24:28 +0000662{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000663 richcmpfunc f;
664 PyObject *res;
665 int checked_reverse_op = 0;
Guido van Rossume797ec12001-01-17 15:24:28 +0000666
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000667 if (v->ob_type != w->ob_type &&
668 PyType_IsSubtype(w->ob_type, v->ob_type) &&
669 (f = w->ob_type->tp_richcompare) != NULL) {
670 checked_reverse_op = 1;
671 res = (*f)(w, v, _Py_SwappedOp[op]);
672 if (res != Py_NotImplemented)
673 return res;
674 Py_DECREF(res);
675 }
676 if ((f = v->ob_type->tp_richcompare) != NULL) {
677 res = (*f)(v, w, op);
678 if (res != Py_NotImplemented)
679 return res;
680 Py_DECREF(res);
681 }
682 if (!checked_reverse_op && (f = w->ob_type->tp_richcompare) != NULL) {
683 res = (*f)(w, v, _Py_SwappedOp[op]);
684 if (res != Py_NotImplemented)
685 return res;
686 Py_DECREF(res);
687 }
688 /* If neither object implements it, provide a sensible default
689 for == and !=, but raise an exception for ordering. */
690 switch (op) {
691 case Py_EQ:
692 res = (v == w) ? Py_True : Py_False;
693 break;
694 case Py_NE:
695 res = (v != w) ? Py_True : Py_False;
696 break;
697 default:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000698 PyErr_Format(PyExc_TypeError,
Victor Stinner91108f02015-10-14 18:25:31 +0200699 "'%s' not supported between instances of '%.100s' and '%.100s'",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000700 opstrings[op],
Victor Stinner91108f02015-10-14 18:25:31 +0200701 v->ob_type->tp_name,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000702 w->ob_type->tp_name);
703 return NULL;
704 }
705 Py_INCREF(res);
706 return res;
Guido van Rossume797ec12001-01-17 15:24:28 +0000707}
708
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000709/* Perform a rich comparison with object result. This wraps do_richcompare()
710 with a check for NULL arguments and a recursion check. */
Guido van Rossume797ec12001-01-17 15:24:28 +0000711
Guido van Rossume797ec12001-01-17 15:24:28 +0000712PyObject *
713PyObject_RichCompare(PyObject *v, PyObject *w, int op)
714{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000715 PyObject *res;
Guido van Rossume797ec12001-01-17 15:24:28 +0000716
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000717 assert(Py_LT <= op && op <= Py_GE);
718 if (v == NULL || w == NULL) {
719 if (!PyErr_Occurred())
720 PyErr_BadInternalCall();
721 return NULL;
722 }
723 if (Py_EnterRecursiveCall(" in comparison"))
724 return NULL;
725 res = do_richcompare(v, w, op);
726 Py_LeaveRecursiveCall();
727 return res;
Guido van Rossume797ec12001-01-17 15:24:28 +0000728}
729
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000730/* Perform a rich comparison with integer result. This wraps
731 PyObject_RichCompare(), returning -1 for error, 0 for false, 1 for true. */
Guido van Rossume797ec12001-01-17 15:24:28 +0000732int
733PyObject_RichCompareBool(PyObject *v, PyObject *w, int op)
734{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000735 PyObject *res;
736 int ok;
Guido van Rossume797ec12001-01-17 15:24:28 +0000737
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000738 /* Quick result when objects are the same.
739 Guarantees that identity implies equality. */
740 if (v == w) {
741 if (op == Py_EQ)
742 return 1;
743 else if (op == Py_NE)
744 return 0;
745 }
Mark Dickinson4a1f5932008-11-12 23:23:36 +0000746
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000747 res = PyObject_RichCompare(v, w, op);
748 if (res == NULL)
749 return -1;
750 if (PyBool_Check(res))
751 ok = (res == Py_True);
752 else
753 ok = PyObject_IsTrue(res);
754 Py_DECREF(res);
755 return ok;
Guido van Rossume797ec12001-01-17 15:24:28 +0000756}
Fred Drake13634cf2000-06-29 19:17:04 +0000757
Antoine Pitrouce4a9da2011-11-21 20:46:33 +0100758Py_hash_t
Nick Coghland1abd252008-07-15 15:46:38 +0000759PyObject_HashNotImplemented(PyObject *v)
760{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000761 PyErr_Format(PyExc_TypeError, "unhashable type: '%.200s'",
762 Py_TYPE(v)->tp_name);
763 return -1;
Nick Coghland1abd252008-07-15 15:46:38 +0000764}
Fred Drake13634cf2000-06-29 19:17:04 +0000765
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000766Py_hash_t
Fred Drake100814d2000-07-09 15:48:49 +0000767PyObject_Hash(PyObject *v)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000768{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000769 PyTypeObject *tp = Py_TYPE(v);
770 if (tp->tp_hash != NULL)
771 return (*tp->tp_hash)(v);
772 /* To keep to the general practice that inheriting
773 * solely from object in C code should work without
774 * an explicit call to PyType_Ready, we implicitly call
775 * PyType_Ready here and then check the tp_hash slot again
776 */
777 if (tp->tp_dict == NULL) {
778 if (PyType_Ready(tp) < 0)
779 return -1;
780 if (tp->tp_hash != NULL)
781 return (*tp->tp_hash)(v);
782 }
783 /* Otherwise, the object can't be hashed */
784 return PyObject_HashNotImplemented(v);
Guido van Rossum9bfef441993-03-29 10:43:31 +0000785}
786
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000787PyObject *
Jeremy Hyltonaf68c872005-12-10 18:50:16 +0000788PyObject_GetAttrString(PyObject *v, const char *name)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000789{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000790 PyObject *w, *res;
Guido van Rossumd8eb1b31996-08-09 20:52:03 +0000791
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000792 if (Py_TYPE(v)->tp_getattr != NULL)
793 return (*Py_TYPE(v)->tp_getattr)(v, (char*)name);
794 w = PyUnicode_InternFromString(name);
795 if (w == NULL)
796 return NULL;
797 res = PyObject_GetAttr(v, w);
Victor Stinner59af08f2012-03-22 02:09:08 +0100798 Py_DECREF(w);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000799 return res;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000800}
801
802int
Jeremy Hyltonaf68c872005-12-10 18:50:16 +0000803PyObject_HasAttrString(PyObject *v, const char *name)
Guido van Rossumed18fdc1993-07-11 19:55:34 +0000804{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000805 PyObject *res = PyObject_GetAttrString(v, name);
806 if (res != NULL) {
807 Py_DECREF(res);
808 return 1;
809 }
810 PyErr_Clear();
811 return 0;
Guido van Rossumed18fdc1993-07-11 19:55:34 +0000812}
813
814int
Jeremy Hyltonaf68c872005-12-10 18:50:16 +0000815PyObject_SetAttrString(PyObject *v, const char *name, PyObject *w)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000816{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000817 PyObject *s;
818 int res;
Guido van Rossumd8eb1b31996-08-09 20:52:03 +0000819
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000820 if (Py_TYPE(v)->tp_setattr != NULL)
821 return (*Py_TYPE(v)->tp_setattr)(v, (char*)name, w);
822 s = PyUnicode_InternFromString(name);
823 if (s == NULL)
824 return -1;
825 res = PyObject_SetAttr(v, s, w);
826 Py_XDECREF(s);
827 return res;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000828}
829
Benjamin Petersonbfebb7b2011-12-15 15:34:02 -0500830int
831_PyObject_IsAbstract(PyObject *obj)
832{
833 int res;
834 PyObject* isabstract;
Benjamin Petersonbfebb7b2011-12-15 15:34:02 -0500835
836 if (obj == NULL)
837 return 0;
838
839 isabstract = _PyObject_GetAttrId(obj, &PyId___isabstractmethod__);
840 if (isabstract == NULL) {
841 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
842 PyErr_Clear();
843 return 0;
844 }
845 return -1;
846 }
847 res = PyObject_IsTrue(isabstract);
848 Py_DECREF(isabstract);
849 return res;
850}
851
Guido van Rossum98ff96a1997-05-20 18:34:44 +0000852PyObject *
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200853_PyObject_GetAttrId(PyObject *v, _Py_Identifier *name)
854{
855 PyObject *result;
Martin v. Löwisd10759f2011-11-07 13:00:05 +0100856 PyObject *oname = _PyUnicode_FromId(name); /* borrowed */
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200857 if (!oname)
858 return NULL;
859 result = PyObject_GetAttr(v, oname);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200860 return result;
861}
862
863int
864_PyObject_HasAttrId(PyObject *v, _Py_Identifier *name)
865{
866 int result;
Martin v. Löwisd10759f2011-11-07 13:00:05 +0100867 PyObject *oname = _PyUnicode_FromId(name); /* borrowed */
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200868 if (!oname)
869 return -1;
870 result = PyObject_HasAttr(v, oname);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200871 return result;
872}
873
874int
875_PyObject_SetAttrId(PyObject *v, _Py_Identifier *name, PyObject *w)
876{
877 int result;
Martin v. Löwisd10759f2011-11-07 13:00:05 +0100878 PyObject *oname = _PyUnicode_FromId(name); /* borrowed */
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200879 if (!oname)
880 return -1;
881 result = PyObject_SetAttr(v, oname, w);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200882 return result;
883}
884
885PyObject *
Fred Drake100814d2000-07-09 15:48:49 +0000886PyObject_GetAttr(PyObject *v, PyObject *name)
Guido van Rossum98ff96a1997-05-20 18:34:44 +0000887{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000888 PyTypeObject *tp = Py_TYPE(v);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000889
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000890 if (!PyUnicode_Check(name)) {
891 PyErr_Format(PyExc_TypeError,
892 "attribute name must be string, not '%.200s'",
893 name->ob_type->tp_name);
894 return NULL;
895 }
896 if (tp->tp_getattro != NULL)
897 return (*tp->tp_getattro)(v, name);
898 if (tp->tp_getattr != NULL) {
899 char *name_str = _PyUnicode_AsString(name);
900 if (name_str == NULL)
901 return NULL;
902 return (*tp->tp_getattr)(v, name_str);
903 }
904 PyErr_Format(PyExc_AttributeError,
905 "'%.50s' object has no attribute '%U'",
906 tp->tp_name, name);
907 return NULL;
Guido van Rossum98ff96a1997-05-20 18:34:44 +0000908}
909
910int
Fred Drake100814d2000-07-09 15:48:49 +0000911PyObject_HasAttr(PyObject *v, PyObject *name)
Guido van Rossum98ff96a1997-05-20 18:34:44 +0000912{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000913 PyObject *res = PyObject_GetAttr(v, name);
914 if (res != NULL) {
915 Py_DECREF(res);
916 return 1;
917 }
918 PyErr_Clear();
919 return 0;
Guido van Rossum98ff96a1997-05-20 18:34:44 +0000920}
921
922int
Fred Drake100814d2000-07-09 15:48:49 +0000923PyObject_SetAttr(PyObject *v, PyObject *name, PyObject *value)
Guido van Rossum98ff96a1997-05-20 18:34:44 +0000924{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000925 PyTypeObject *tp = Py_TYPE(v);
926 int err;
Marc-André Lemburge44e5072000-09-18 16:20:57 +0000927
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000928 if (!PyUnicode_Check(name)) {
929 PyErr_Format(PyExc_TypeError,
930 "attribute name must be string, not '%.200s'",
931 name->ob_type->tp_name);
932 return -1;
933 }
934 Py_INCREF(name);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000935
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000936 PyUnicode_InternInPlace(&name);
937 if (tp->tp_setattro != NULL) {
938 err = (*tp->tp_setattro)(v, name, value);
939 Py_DECREF(name);
940 return err;
941 }
942 if (tp->tp_setattr != NULL) {
943 char *name_str = _PyUnicode_AsString(name);
944 if (name_str == NULL)
945 return -1;
946 err = (*tp->tp_setattr)(v, name_str, value);
947 Py_DECREF(name);
948 return err;
949 }
950 Py_DECREF(name);
951 assert(name->ob_refcnt >= 1);
952 if (tp->tp_getattr == NULL && tp->tp_getattro == NULL)
953 PyErr_Format(PyExc_TypeError,
954 "'%.100s' object has no attributes "
955 "(%s .%U)",
956 tp->tp_name,
957 value==NULL ? "del" : "assign to",
958 name);
959 else
960 PyErr_Format(PyExc_TypeError,
961 "'%.100s' object has only read-only attributes "
962 "(%s .%U)",
963 tp->tp_name,
964 value==NULL ? "del" : "assign to",
965 name);
966 return -1;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000967}
968
969/* Helper to get a pointer to an object's __dict__ slot, if any */
970
971PyObject **
972_PyObject_GetDictPtr(PyObject *obj)
973{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000974 Py_ssize_t dictoffset;
975 PyTypeObject *tp = Py_TYPE(obj);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000976
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000977 dictoffset = tp->tp_dictoffset;
978 if (dictoffset == 0)
979 return NULL;
980 if (dictoffset < 0) {
981 Py_ssize_t tsize;
982 size_t size;
Guido van Rossum2eb0b872002-03-01 22:24:49 +0000983
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000984 tsize = ((PyVarObject *)obj)->ob_size;
985 if (tsize < 0)
986 tsize = -tsize;
987 size = _PyObject_VAR_SIZE(tp, tsize);
Guido van Rossum2eb0b872002-03-01 22:24:49 +0000988
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000989 dictoffset += (long)size;
990 assert(dictoffset > 0);
991 assert(dictoffset % SIZEOF_VOID_P == 0);
992 }
993 return (PyObject **) ((char *)obj + dictoffset);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000994}
995
Tim Peters6d6c1a32001-08-02 04:15:00 +0000996PyObject *
Raymond Hettinger1da1dbf2003-03-17 19:46:11 +0000997PyObject_SelfIter(PyObject *obj)
Raymond Hettinger01538262003-03-17 08:24:35 +0000998{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000999 Py_INCREF(obj);
1000 return obj;
Raymond Hettinger01538262003-03-17 08:24:35 +00001001}
1002
Antoine Pitroua7013882012-04-05 00:04:20 +02001003/* Convenience function to get a builtin from its name */
1004PyObject *
1005_PyObject_GetBuiltin(const char *name)
1006{
Victor Stinner53e9ec42013-11-07 00:43:05 +01001007 PyObject *mod_name, *mod, *attr;
1008
Victor Stinnerbd303c12013-11-07 23:07:29 +01001009 mod_name = _PyUnicode_FromId(&PyId_builtins); /* borrowed */
Victor Stinner53e9ec42013-11-07 00:43:05 +01001010 if (mod_name == NULL)
1011 return NULL;
1012 mod = PyImport_Import(mod_name);
Antoine Pitroua7013882012-04-05 00:04:20 +02001013 if (mod == NULL)
1014 return NULL;
1015 attr = PyObject_GetAttrString(mod, name);
1016 Py_DECREF(mod);
1017 return attr;
1018}
1019
Amaury Forgeot d'Arcf343e012009-01-12 23:58:21 +00001020/* Helper used when the __next__ method is removed from a type:
1021 tp_iternext is never NULL and can be safely called without checking
1022 on every iteration.
1023 */
1024
1025PyObject *
1026_PyObject_NextNotImplemented(PyObject *self)
1027{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001028 PyErr_Format(PyExc_TypeError,
1029 "'%.200s' object is not iterable",
1030 Py_TYPE(self)->tp_name);
1031 return NULL;
Amaury Forgeot d'Arcf343e012009-01-12 23:58:21 +00001032}
1033
Michael W. Hudson1593f502004-09-14 17:09:47 +00001034/* Generic GetAttr functions - put these in your tp_[gs]etattro slot */
1035
Raymond Hettinger01538262003-03-17 08:24:35 +00001036PyObject *
Antoine Pitrou1a9a9d52010-08-28 18:17:03 +00001037_PyObject_GenericGetAttrWithDict(PyObject *obj, PyObject *name, PyObject *dict)
Tim Peters6d6c1a32001-08-02 04:15:00 +00001038{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001039 PyTypeObject *tp = Py_TYPE(obj);
1040 PyObject *descr = NULL;
1041 PyObject *res = NULL;
1042 descrgetfunc f;
1043 Py_ssize_t dictoffset;
1044 PyObject **dictptr;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001045
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001046 if (!PyUnicode_Check(name)){
1047 PyErr_Format(PyExc_TypeError,
1048 "attribute name must be string, not '%.200s'",
1049 name->ob_type->tp_name);
1050 return NULL;
1051 }
Serhiy Storchaka55c861f2016-04-17 20:31:51 +03001052 Py_INCREF(name);
Guido van Rossumebca9fc2001-12-04 15:54:53 +00001053
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001054 if (tp->tp_dict == NULL) {
1055 if (PyType_Ready(tp) < 0)
1056 goto done;
1057 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001058
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001059 descr = _PyType_Lookup(tp, name);
Michael W. Hudsonb2c7de42003-08-15 13:07:47 +00001060
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001061 f = NULL;
1062 if (descr != NULL) {
Serhiy Storchaka55c861f2016-04-17 20:31:51 +03001063 Py_INCREF(descr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001064 f = descr->ob_type->tp_descr_get;
1065 if (f != NULL && PyDescr_IsData(descr)) {
1066 res = f(descr, obj, (PyObject *)obj->ob_type);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001067 goto done;
1068 }
1069 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001070
Antoine Pitrou1a9a9d52010-08-28 18:17:03 +00001071 if (dict == NULL) {
1072 /* Inline _PyObject_GetDictPtr */
1073 dictoffset = tp->tp_dictoffset;
1074 if (dictoffset != 0) {
1075 if (dictoffset < 0) {
1076 Py_ssize_t tsize;
1077 size_t size;
Guido van Rossumc66ff442002-08-19 16:50:48 +00001078
Antoine Pitrou1a9a9d52010-08-28 18:17:03 +00001079 tsize = ((PyVarObject *)obj)->ob_size;
1080 if (tsize < 0)
1081 tsize = -tsize;
1082 size = _PyObject_VAR_SIZE(tp, tsize);
Serhiy Storchaka55c861f2016-04-17 20:31:51 +03001083 assert(size <= PY_SSIZE_T_MAX);
Guido van Rossumc66ff442002-08-19 16:50:48 +00001084
Serhiy Storchaka55c861f2016-04-17 20:31:51 +03001085 dictoffset += (Py_ssize_t)size;
Antoine Pitrou1a9a9d52010-08-28 18:17:03 +00001086 assert(dictoffset > 0);
1087 assert(dictoffset % SIZEOF_VOID_P == 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001088 }
Antoine Pitrou1a9a9d52010-08-28 18:17:03 +00001089 dictptr = (PyObject **) ((char *)obj + dictoffset);
1090 dict = *dictptr;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001091 }
1092 }
Antoine Pitrou1a9a9d52010-08-28 18:17:03 +00001093 if (dict != NULL) {
1094 Py_INCREF(dict);
1095 res = PyDict_GetItem(dict, name);
1096 if (res != NULL) {
1097 Py_INCREF(res);
Antoine Pitrou1a9a9d52010-08-28 18:17:03 +00001098 Py_DECREF(dict);
1099 goto done;
1100 }
1101 Py_DECREF(dict);
1102 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001103
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001104 if (f != NULL) {
1105 res = f(descr, obj, (PyObject *)Py_TYPE(obj));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001106 goto done;
1107 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001108
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001109 if (descr != NULL) {
1110 res = descr;
Victor Stinner2d01dc02012-03-09 00:44:13 +01001111 descr = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001112 goto done;
1113 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001114
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001115 PyErr_Format(PyExc_AttributeError,
1116 "'%.50s' object has no attribute '%U'",
1117 tp->tp_name, name);
Guido van Rossumebca9fc2001-12-04 15:54:53 +00001118 done:
Victor Stinner2d01dc02012-03-09 00:44:13 +01001119 Py_XDECREF(descr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001120 Py_DECREF(name);
1121 return res;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001122}
1123
Antoine Pitrou1a9a9d52010-08-28 18:17:03 +00001124PyObject *
1125PyObject_GenericGetAttr(PyObject *obj, PyObject *name)
1126{
1127 return _PyObject_GenericGetAttrWithDict(obj, name, NULL);
1128}
1129
Tim Peters6d6c1a32001-08-02 04:15:00 +00001130int
Antoine Pitrou1a9a9d52010-08-28 18:17:03 +00001131_PyObject_GenericSetAttrWithDict(PyObject *obj, PyObject *name,
1132 PyObject *value, PyObject *dict)
Tim Peters6d6c1a32001-08-02 04:15:00 +00001133{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001134 PyTypeObject *tp = Py_TYPE(obj);
1135 PyObject *descr;
1136 descrsetfunc f;
1137 PyObject **dictptr;
1138 int res = -1;
Guido van Rossumebca9fc2001-12-04 15:54:53 +00001139
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001140 if (!PyUnicode_Check(name)){
1141 PyErr_Format(PyExc_TypeError,
1142 "attribute name must be string, not '%.200s'",
1143 name->ob_type->tp_name);
1144 return -1;
1145 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001146
Benjamin Peterson74529ad2012-03-09 07:25:32 -08001147 if (tp->tp_dict == NULL && PyType_Ready(tp) < 0)
1148 return -1;
1149
1150 Py_INCREF(name);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001151
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001152 descr = _PyType_Lookup(tp, name);
Victor Stinner2d01dc02012-03-09 00:44:13 +01001153
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001154 if (descr != NULL) {
Serhiy Storchaka55c861f2016-04-17 20:31:51 +03001155 Py_INCREF(descr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001156 f = descr->ob_type->tp_descr_set;
Serhiy Storchaka55c861f2016-04-17 20:31:51 +03001157 if (f != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001158 res = f(descr, obj, value);
1159 goto done;
1160 }
1161 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001162
Antoine Pitrou1a9a9d52010-08-28 18:17:03 +00001163 if (dict == NULL) {
1164 dictptr = _PyObject_GetDictPtr(obj);
Serhiy Storchaka55c861f2016-04-17 20:31:51 +03001165 if (dictptr == NULL) {
1166 if (descr == NULL) {
1167 PyErr_Format(PyExc_AttributeError,
1168 "'%.100s' object has no attribute '%U'",
1169 tp->tp_name, name);
1170 }
1171 else {
1172 PyErr_Format(PyExc_AttributeError,
1173 "'%.50s' object attribute '%U' is read-only",
1174 tp->tp_name, name);
1175 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001176 goto done;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001177 }
Serhiy Storchaka55c861f2016-04-17 20:31:51 +03001178 res = _PyObjectDict_SetItem(tp, dictptr, name, value);
Antoine Pitrou1a9a9d52010-08-28 18:17:03 +00001179 }
Serhiy Storchaka55c861f2016-04-17 20:31:51 +03001180 else {
Antoine Pitrou1a9a9d52010-08-28 18:17:03 +00001181 Py_INCREF(dict);
1182 if (value == NULL)
1183 res = PyDict_DelItem(dict, name);
1184 else
1185 res = PyDict_SetItem(dict, name, value);
Benjamin Peterson74529ad2012-03-09 07:25:32 -08001186 Py_DECREF(dict);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001187 }
Serhiy Storchaka55c861f2016-04-17 20:31:51 +03001188 if (res < 0 && PyErr_ExceptionMatches(PyExc_KeyError))
1189 PyErr_SetObject(PyExc_AttributeError, name);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001190
Guido van Rossumebca9fc2001-12-04 15:54:53 +00001191 done:
Victor Stinner2d01dc02012-03-09 00:44:13 +01001192 Py_XDECREF(descr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001193 Py_DECREF(name);
1194 return res;
Guido van Rossum98ff96a1997-05-20 18:34:44 +00001195}
1196
Antoine Pitrou1a9a9d52010-08-28 18:17:03 +00001197int
1198PyObject_GenericSetAttr(PyObject *obj, PyObject *name, PyObject *value)
1199{
1200 return _PyObject_GenericSetAttrWithDict(obj, name, value, NULL);
1201}
1202
Benjamin Peterson8eb12692012-02-19 19:59:10 -05001203int
1204PyObject_GenericSetDict(PyObject *obj, PyObject *value, void *context)
1205{
Serhiy Storchaka576f1322016-01-05 21:27:54 +02001206 PyObject **dictptr = _PyObject_GetDictPtr(obj);
Benjamin Peterson8eb12692012-02-19 19:59:10 -05001207 if (dictptr == NULL) {
1208 PyErr_SetString(PyExc_AttributeError,
1209 "This object has no __dict__");
1210 return -1;
1211 }
1212 if (value == NULL) {
1213 PyErr_SetString(PyExc_TypeError, "cannot delete __dict__");
1214 return -1;
1215 }
1216 if (!PyDict_Check(value)) {
1217 PyErr_Format(PyExc_TypeError,
1218 "__dict__ must be set to a dictionary, "
1219 "not a '%.200s'", Py_TYPE(value)->tp_name);
1220 return -1;
1221 }
Serhiy Storchaka576f1322016-01-05 21:27:54 +02001222 Py_INCREF(value);
Serhiy Storchakaec397562016-04-06 09:50:03 +03001223 Py_XSETREF(*dictptr, value);
Benjamin Peterson8eb12692012-02-19 19:59:10 -05001224 return 0;
1225}
1226
Antoine Pitrou1a9a9d52010-08-28 18:17:03 +00001227
Guido van Rossum6ac258d1993-05-12 08:24:20 +00001228/* Test a value used as condition, e.g., in a for or if statement.
1229 Return -1 if an error occurred */
1230
1231int
Fred Drake100814d2000-07-09 15:48:49 +00001232PyObject_IsTrue(PyObject *v)
Guido van Rossum6ac258d1993-05-12 08:24:20 +00001233{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001234 Py_ssize_t res;
1235 if (v == Py_True)
1236 return 1;
1237 if (v == Py_False)
1238 return 0;
1239 if (v == Py_None)
1240 return 0;
1241 else if (v->ob_type->tp_as_number != NULL &&
1242 v->ob_type->tp_as_number->nb_bool != NULL)
1243 res = (*v->ob_type->tp_as_number->nb_bool)(v);
1244 else if (v->ob_type->tp_as_mapping != NULL &&
1245 v->ob_type->tp_as_mapping->mp_length != NULL)
1246 res = (*v->ob_type->tp_as_mapping->mp_length)(v);
1247 else if (v->ob_type->tp_as_sequence != NULL &&
1248 v->ob_type->tp_as_sequence->sq_length != NULL)
1249 res = (*v->ob_type->tp_as_sequence->sq_length)(v);
1250 else
1251 return 1;
1252 /* if it is negative, it should be either -1 or -2 */
1253 return (res > 0) ? 1 : Py_SAFE_DOWNCAST(res, Py_ssize_t, int);
Guido van Rossum6ac258d1993-05-12 08:24:20 +00001254}
1255
Tim Peters803526b2002-07-07 05:13:56 +00001256/* equivalent of 'not v'
Guido van Rossumc3d3f961998-04-09 17:53:59 +00001257 Return -1 if an error occurred */
1258
1259int
Fred Drake100814d2000-07-09 15:48:49 +00001260PyObject_Not(PyObject *v)
Guido van Rossumc3d3f961998-04-09 17:53:59 +00001261{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001262 int res;
1263 res = PyObject_IsTrue(v);
1264 if (res < 0)
1265 return res;
1266 return res == 0;
Guido van Rossumc3d3f961998-04-09 17:53:59 +00001267}
1268
Guido van Rossum49b11fe1995-01-26 00:38:22 +00001269/* Test whether an object can be called */
1270
1271int
Fred Drake100814d2000-07-09 15:48:49 +00001272PyCallable_Check(PyObject *x)
Guido van Rossum49b11fe1995-01-26 00:38:22 +00001273{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001274 if (x == NULL)
1275 return 0;
1276 return x->ob_type->tp_call != NULL;
Guido van Rossum49b11fe1995-01-26 00:38:22 +00001277}
1278
Tim Peters7eea37e2001-09-04 22:08:56 +00001279
Georg Brandle32b4222007-03-10 22:13:27 +00001280/* Helper for PyObject_Dir without arguments: returns the local scope. */
1281static PyObject *
Guido van Rossumad7d8d12007-04-13 01:39:34 +00001282_dir_locals(void)
Tim Peters305b5852001-09-17 02:38:46 +00001283{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001284 PyObject *names;
Victor Stinner41bb43a2013-10-29 01:19:37 +01001285 PyObject *locals;
Tim Peters305b5852001-09-17 02:38:46 +00001286
Victor Stinner41bb43a2013-10-29 01:19:37 +01001287 locals = PyEval_GetLocals();
1288 if (locals == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001289 return NULL;
Tim Peters305b5852001-09-17 02:38:46 +00001290
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001291 names = PyMapping_Keys(locals);
1292 if (!names)
1293 return NULL;
1294 if (!PyList_Check(names)) {
1295 PyErr_Format(PyExc_TypeError,
1296 "dir(): expected keys() of locals to be a list, "
1297 "not '%.200s'", Py_TYPE(names)->tp_name);
1298 Py_DECREF(names);
1299 return NULL;
1300 }
Benjamin Peterson3bbb7222011-06-11 16:12:08 -05001301 if (PyList_Sort(names)) {
1302 Py_DECREF(names);
1303 return NULL;
1304 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001305 /* the locals don't need to be DECREF'd */
1306 return names;
Georg Brandle32b4222007-03-10 22:13:27 +00001307}
1308
Benjamin Peterson82b00c12011-05-24 11:09:06 -05001309/* Helper for PyObject_Dir: object introspection. */
Georg Brandle32b4222007-03-10 22:13:27 +00001310static PyObject *
1311_dir_object(PyObject *obj)
1312{
Benjamin Peterson3bbb7222011-06-11 16:12:08 -05001313 PyObject *result, *sorted;
Benjamin Petersonce798522012-01-22 11:24:29 -05001314 PyObject *dirfunc = _PyObject_LookupSpecial(obj, &PyId___dir__);
Georg Brandle32b4222007-03-10 22:13:27 +00001315
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001316 assert(obj);
1317 if (dirfunc == NULL) {
Benjamin Peterson82b00c12011-05-24 11:09:06 -05001318 if (!PyErr_Occurred())
1319 PyErr_SetString(PyExc_TypeError, "object does not provide __dir__");
1320 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001321 }
Benjamin Peterson82b00c12011-05-24 11:09:06 -05001322 /* use __dir__ */
1323 result = PyObject_CallFunctionObjArgs(dirfunc, NULL);
1324 Py_DECREF(dirfunc);
1325 if (result == NULL)
1326 return NULL;
Benjamin Peterson3bbb7222011-06-11 16:12:08 -05001327 /* return sorted(result) */
1328 sorted = PySequence_List(result);
1329 Py_DECREF(result);
1330 if (sorted == NULL)
1331 return NULL;
1332 if (PyList_Sort(sorted)) {
1333 Py_DECREF(sorted);
1334 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001335 }
Benjamin Peterson3bbb7222011-06-11 16:12:08 -05001336 return sorted;
Georg Brandle32b4222007-03-10 22:13:27 +00001337}
1338
1339/* Implementation of dir() -- if obj is NULL, returns the names in the current
1340 (local) scope. Otherwise, performs introspection of the object: returns a
1341 sorted list of attribute names (supposedly) accessible from the object
1342*/
1343PyObject *
1344PyObject_Dir(PyObject *obj)
1345{
Benjamin Peterson3bbb7222011-06-11 16:12:08 -05001346 return (obj == NULL) ? _dir_locals() : _dir_object(obj);
Tim Peters7eea37e2001-09-04 22:08:56 +00001347}
Guido van Rossum49b11fe1995-01-26 00:38:22 +00001348
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001349/*
Raymond Hettinger66d2be82011-07-28 09:55:13 -07001350None is a non-NULL undefined value.
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001351There is (and should be!) no way to create other objects of this type,
Guido van Rossum3f5da241990-12-20 15:06:42 +00001352so there is exactly one (which is indestructible, by the way).
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001353*/
1354
Guido van Rossum0c182a11992-03-27 17:26:13 +00001355/* ARGSUSED */
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001356static PyObject *
Fred Drake100814d2000-07-09 15:48:49 +00001357none_repr(PyObject *op)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001358{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001359 return PyUnicode_FromString("None");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001360}
1361
Barry Warsaw9bf16442001-01-23 16:24:35 +00001362/* ARGUSED */
1363static void
Tim Peters803526b2002-07-07 05:13:56 +00001364none_dealloc(PyObject* ignore)
Barry Warsaw9bf16442001-01-23 16:24:35 +00001365{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001366 /* This should never get called, but we also don't want to SEGV if
1367 * we accidentally decref None out of existence.
1368 */
1369 Py_FatalError("deallocating None");
Barry Warsaw9bf16442001-01-23 16:24:35 +00001370}
1371
Benjamin Petersonc4607ae2011-07-29 18:19:43 -05001372static PyObject *
1373none_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
1374{
1375 if (PyTuple_GET_SIZE(args) || (kwargs && PyDict_Size(kwargs))) {
1376 PyErr_SetString(PyExc_TypeError, "NoneType takes no arguments");
1377 return NULL;
1378 }
1379 Py_RETURN_NONE;
1380}
1381
Raymond Hettinger66d2be82011-07-28 09:55:13 -07001382static int
1383none_bool(PyObject *v)
1384{
1385 return 0;
1386}
1387
1388static PyNumberMethods none_as_number = {
1389 0, /* nb_add */
1390 0, /* nb_subtract */
1391 0, /* nb_multiply */
1392 0, /* nb_remainder */
1393 0, /* nb_divmod */
1394 0, /* nb_power */
1395 0, /* nb_negative */
1396 0, /* nb_positive */
1397 0, /* nb_absolute */
1398 (inquiry)none_bool, /* nb_bool */
1399 0, /* nb_invert */
1400 0, /* nb_lshift */
1401 0, /* nb_rshift */
1402 0, /* nb_and */
1403 0, /* nb_xor */
1404 0, /* nb_or */
1405 0, /* nb_int */
1406 0, /* nb_reserved */
1407 0, /* nb_float */
1408 0, /* nb_inplace_add */
1409 0, /* nb_inplace_subtract */
1410 0, /* nb_inplace_multiply */
1411 0, /* nb_inplace_remainder */
1412 0, /* nb_inplace_power */
1413 0, /* nb_inplace_lshift */
1414 0, /* nb_inplace_rshift */
1415 0, /* nb_inplace_and */
1416 0, /* nb_inplace_xor */
1417 0, /* nb_inplace_or */
1418 0, /* nb_floor_divide */
1419 0, /* nb_true_divide */
1420 0, /* nb_inplace_floor_divide */
1421 0, /* nb_inplace_true_divide */
1422 0, /* nb_index */
1423};
Barry Warsaw9bf16442001-01-23 16:24:35 +00001424
Alexandre Vassalotti65846c62013-11-30 17:55:48 -08001425PyTypeObject _PyNone_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001426 PyVarObject_HEAD_INIT(&PyType_Type, 0)
1427 "NoneType",
1428 0,
1429 0,
1430 none_dealloc, /*tp_dealloc*/ /*never called*/
1431 0, /*tp_print*/
1432 0, /*tp_getattr*/
1433 0, /*tp_setattr*/
1434 0, /*tp_reserved*/
1435 none_repr, /*tp_repr*/
Raymond Hettinger66d2be82011-07-28 09:55:13 -07001436 &none_as_number, /*tp_as_number*/
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001437 0, /*tp_as_sequence*/
1438 0, /*tp_as_mapping*/
1439 0, /*tp_hash */
Benjamin Petersonc4607ae2011-07-29 18:19:43 -05001440 0, /*tp_call */
1441 0, /*tp_str */
1442 0, /*tp_getattro */
1443 0, /*tp_setattro */
1444 0, /*tp_as_buffer */
1445 Py_TPFLAGS_DEFAULT, /*tp_flags */
1446 0, /*tp_doc */
1447 0, /*tp_traverse */
1448 0, /*tp_clear */
1449 0, /*tp_richcompare */
1450 0, /*tp_weaklistoffset */
1451 0, /*tp_iter */
1452 0, /*tp_iternext */
1453 0, /*tp_methods */
1454 0, /*tp_members */
1455 0, /*tp_getset */
1456 0, /*tp_base */
1457 0, /*tp_dict */
1458 0, /*tp_descr_get */
1459 0, /*tp_descr_set */
1460 0, /*tp_dictoffset */
1461 0, /*tp_init */
1462 0, /*tp_alloc */
1463 none_new, /*tp_new */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001464};
1465
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001466PyObject _Py_NoneStruct = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001467 _PyObject_EXTRA_INIT
Alexandre Vassalotti65846c62013-11-30 17:55:48 -08001468 1, &_PyNone_Type
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001469};
1470
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +00001471/* NotImplemented is an object that can be used to signal that an
1472 operation is not implemented for the given type combination. */
1473
1474static PyObject *
1475NotImplemented_repr(PyObject *op)
1476{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001477 return PyUnicode_FromString("NotImplemented");
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +00001478}
1479
Benjamin Peterson18d7d7a2011-07-29 18:27:44 -05001480static PyObject *
Alexandre Vassalottic49477b2013-11-24 02:53:45 -08001481NotImplemented_reduce(PyObject *op)
1482{
1483 return PyUnicode_FromString("NotImplemented");
1484}
1485
1486static PyMethodDef notimplemented_methods[] = {
1487 {"__reduce__", (PyCFunction)NotImplemented_reduce, METH_NOARGS, NULL},
1488 {NULL, NULL}
1489};
1490
1491static PyObject *
Benjamin Peterson18d7d7a2011-07-29 18:27:44 -05001492notimplemented_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
1493{
1494 if (PyTuple_GET_SIZE(args) || (kwargs && PyDict_Size(kwargs))) {
1495 PyErr_SetString(PyExc_TypeError, "NotImplementedType takes no arguments");
1496 return NULL;
1497 }
Brian Curtindfc80e32011-08-10 20:28:54 -05001498 Py_RETURN_NOTIMPLEMENTED;
Benjamin Peterson18d7d7a2011-07-29 18:27:44 -05001499}
1500
Armin Ronacher226b1db2012-10-06 14:28:58 +02001501static void
1502notimplemented_dealloc(PyObject* ignore)
1503{
1504 /* This should never get called, but we also don't want to SEGV if
1505 * we accidentally decref NotImplemented out of existence.
1506 */
1507 Py_FatalError("deallocating NotImplemented");
1508}
1509
Alexandre Vassalotti65846c62013-11-30 17:55:48 -08001510PyTypeObject _PyNotImplemented_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001511 PyVarObject_HEAD_INIT(&PyType_Type, 0)
1512 "NotImplementedType",
1513 0,
1514 0,
Armin Ronacher226b1db2012-10-06 14:28:58 +02001515 notimplemented_dealloc, /*tp_dealloc*/ /*never called*/
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001516 0, /*tp_print*/
1517 0, /*tp_getattr*/
1518 0, /*tp_setattr*/
1519 0, /*tp_reserved*/
1520 NotImplemented_repr, /*tp_repr*/
1521 0, /*tp_as_number*/
1522 0, /*tp_as_sequence*/
1523 0, /*tp_as_mapping*/
1524 0, /*tp_hash */
Benjamin Peterson18d7d7a2011-07-29 18:27:44 -05001525 0, /*tp_call */
1526 0, /*tp_str */
1527 0, /*tp_getattro */
1528 0, /*tp_setattro */
1529 0, /*tp_as_buffer */
1530 Py_TPFLAGS_DEFAULT, /*tp_flags */
1531 0, /*tp_doc */
1532 0, /*tp_traverse */
1533 0, /*tp_clear */
1534 0, /*tp_richcompare */
1535 0, /*tp_weaklistoffset */
1536 0, /*tp_iter */
1537 0, /*tp_iternext */
Alexandre Vassalottic49477b2013-11-24 02:53:45 -08001538 notimplemented_methods, /*tp_methods */
Benjamin Peterson18d7d7a2011-07-29 18:27:44 -05001539 0, /*tp_members */
1540 0, /*tp_getset */
1541 0, /*tp_base */
1542 0, /*tp_dict */
1543 0, /*tp_descr_get */
1544 0, /*tp_descr_set */
1545 0, /*tp_dictoffset */
1546 0, /*tp_init */
1547 0, /*tp_alloc */
1548 notimplemented_new, /*tp_new */
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +00001549};
1550
1551PyObject _Py_NotImplementedStruct = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001552 _PyObject_EXTRA_INIT
Alexandre Vassalotti65846c62013-11-30 17:55:48 -08001553 1, &_PyNotImplemented_Type
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +00001554};
1555
Guido van Rossumba21a492001-08-16 08:17:26 +00001556void
1557_Py_ReadyTypes(void)
1558{
Victor Stinner5a1bb4e2014-06-02 14:10:59 +02001559 if (PyType_Ready(&PyBaseObject_Type) < 0)
1560 Py_FatalError("Can't initialize object type");
1561
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001562 if (PyType_Ready(&PyType_Type) < 0)
1563 Py_FatalError("Can't initialize type type");
Guido van Rossumba21a492001-08-16 08:17:26 +00001564
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001565 if (PyType_Ready(&_PyWeakref_RefType) < 0)
1566 Py_FatalError("Can't initialize weakref type");
Fred Drake0a4dd392004-07-02 18:57:45 +00001567
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001568 if (PyType_Ready(&_PyWeakref_CallableProxyType) < 0)
1569 Py_FatalError("Can't initialize callable weakref proxy type");
Benjamin Petersonfd838e62009-04-20 02:09:13 +00001570
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001571 if (PyType_Ready(&_PyWeakref_ProxyType) < 0)
1572 Py_FatalError("Can't initialize weakref proxy type");
Benjamin Petersonfd838e62009-04-20 02:09:13 +00001573
Victor Stinner5a1bb4e2014-06-02 14:10:59 +02001574 if (PyType_Ready(&PyLong_Type) < 0)
1575 Py_FatalError("Can't initialize int type");
1576
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001577 if (PyType_Ready(&PyBool_Type) < 0)
1578 Py_FatalError("Can't initialize bool type");
Guido van Rossum77f6a652002-04-03 22:41:51 +00001579
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001580 if (PyType_Ready(&PyByteArray_Type) < 0)
1581 Py_FatalError("Can't initialize bytearray type");
Guido van Rossum4dfe8a12006-04-22 23:28:04 +00001582
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001583 if (PyType_Ready(&PyBytes_Type) < 0)
1584 Py_FatalError("Can't initialize 'str'");
Guido van Rossumcacfc072002-05-24 19:01:59 +00001585
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001586 if (PyType_Ready(&PyList_Type) < 0)
1587 Py_FatalError("Can't initialize list type");
Guido van Rossumba21a492001-08-16 08:17:26 +00001588
Alexandre Vassalotti65846c62013-11-30 17:55:48 -08001589 if (PyType_Ready(&_PyNone_Type) < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001590 Py_FatalError("Can't initialize None type");
Guido van Rossumba21a492001-08-16 08:17:26 +00001591
Alexandre Vassalotti65846c62013-11-30 17:55:48 -08001592 if (PyType_Ready(&_PyNotImplemented_Type) < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001593 Py_FatalError("Can't initialize NotImplemented type");
Benjamin Petersonae937c02009-04-18 20:54:08 +00001594
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001595 if (PyType_Ready(&PyTraceBack_Type) < 0)
1596 Py_FatalError("Can't initialize traceback type");
Benjamin Petersonae937c02009-04-18 20:54:08 +00001597
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001598 if (PyType_Ready(&PySuper_Type) < 0)
1599 Py_FatalError("Can't initialize super type");
Benjamin Petersonae937c02009-04-18 20:54:08 +00001600
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001601 if (PyType_Ready(&PyRange_Type) < 0)
1602 Py_FatalError("Can't initialize range type");
Benjamin Petersonae937c02009-04-18 20:54:08 +00001603
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001604 if (PyType_Ready(&PyDict_Type) < 0)
1605 Py_FatalError("Can't initialize dict type");
Benjamin Petersonae937c02009-04-18 20:54:08 +00001606
Eric Snow96c6af92015-05-29 22:21:39 -06001607 if (PyType_Ready(&PyODict_Type) < 0)
1608 Py_FatalError("Can't initialize OrderedDict type");
1609
1610 if (PyType_Ready(&PyODictKeys_Type) < 0)
1611 Py_FatalError("Can't initialize odict_keys type");
1612
1613 if (PyType_Ready(&PyODictItems_Type) < 0)
1614 Py_FatalError("Can't initialize odict_items type");
1615
1616 if (PyType_Ready(&PyODictValues_Type) < 0)
1617 Py_FatalError("Can't initialize odict_values type");
1618
1619 if (PyType_Ready(&PyODictIter_Type) < 0)
1620 Py_FatalError("Can't initialize odict_keyiterator type");
1621
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001622 if (PyType_Ready(&PySet_Type) < 0)
1623 Py_FatalError("Can't initialize set type");
Benjamin Petersonae937c02009-04-18 20:54:08 +00001624
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001625 if (PyType_Ready(&PyUnicode_Type) < 0)
1626 Py_FatalError("Can't initialize str type");
Benjamin Petersonae937c02009-04-18 20:54:08 +00001627
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001628 if (PyType_Ready(&PySlice_Type) < 0)
1629 Py_FatalError("Can't initialize slice type");
Benjamin Petersonae937c02009-04-18 20:54:08 +00001630
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001631 if (PyType_Ready(&PyStaticMethod_Type) < 0)
1632 Py_FatalError("Can't initialize static method type");
Benjamin Petersonae937c02009-04-18 20:54:08 +00001633
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001634 if (PyType_Ready(&PyComplex_Type) < 0)
1635 Py_FatalError("Can't initialize complex type");
Skip Montanaroba1e0f42009-10-18 14:25:35 +00001636
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001637 if (PyType_Ready(&PyFloat_Type) < 0)
1638 Py_FatalError("Can't initialize float type");
Benjamin Petersonae937c02009-04-18 20:54:08 +00001639
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001640 if (PyType_Ready(&PyFrozenSet_Type) < 0)
1641 Py_FatalError("Can't initialize frozenset type");
Benjamin Petersonae937c02009-04-18 20:54:08 +00001642
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001643 if (PyType_Ready(&PyProperty_Type) < 0)
1644 Py_FatalError("Can't initialize property type");
Benjamin Petersonae937c02009-04-18 20:54:08 +00001645
Stefan Krah9a2d99e2012-02-25 12:24:21 +01001646 if (PyType_Ready(&_PyManagedBuffer_Type) < 0)
1647 Py_FatalError("Can't initialize managed buffer type");
1648
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001649 if (PyType_Ready(&PyMemoryView_Type) < 0)
1650 Py_FatalError("Can't initialize memoryview type");
Benjamin Petersonae937c02009-04-18 20:54:08 +00001651
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001652 if (PyType_Ready(&PyTuple_Type) < 0)
1653 Py_FatalError("Can't initialize tuple type");
Benjamin Petersonae937c02009-04-18 20:54:08 +00001654
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001655 if (PyType_Ready(&PyEnum_Type) < 0)
1656 Py_FatalError("Can't initialize enumerate type");
Benjamin Petersonae937c02009-04-18 20:54:08 +00001657
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001658 if (PyType_Ready(&PyReversed_Type) < 0)
1659 Py_FatalError("Can't initialize reversed type");
Guido van Rossum47b9ff62006-08-24 00:41:19 +00001660
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001661 if (PyType_Ready(&PyStdPrinter_Type) < 0)
1662 Py_FatalError("Can't initialize StdPrinter");
Benjamin Petersonfd838e62009-04-20 02:09:13 +00001663
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001664 if (PyType_Ready(&PyCode_Type) < 0)
1665 Py_FatalError("Can't initialize code type");
Benjamin Petersonfd838e62009-04-20 02:09:13 +00001666
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001667 if (PyType_Ready(&PyFrame_Type) < 0)
1668 Py_FatalError("Can't initialize frame type");
Benjamin Petersonfd838e62009-04-20 02:09:13 +00001669
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001670 if (PyType_Ready(&PyCFunction_Type) < 0)
1671 Py_FatalError("Can't initialize builtin function type");
Benjamin Petersonfd838e62009-04-20 02:09:13 +00001672
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001673 if (PyType_Ready(&PyMethod_Type) < 0)
1674 Py_FatalError("Can't initialize method type");
Benjamin Petersonfd838e62009-04-20 02:09:13 +00001675
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001676 if (PyType_Ready(&PyFunction_Type) < 0)
1677 Py_FatalError("Can't initialize function type");
Benjamin Petersonfd838e62009-04-20 02:09:13 +00001678
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001679 if (PyType_Ready(&PyDictProxy_Type) < 0)
1680 Py_FatalError("Can't initialize dict proxy type");
Benjamin Petersonfd838e62009-04-20 02:09:13 +00001681
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001682 if (PyType_Ready(&PyGen_Type) < 0)
1683 Py_FatalError("Can't initialize generator type");
Benjamin Petersonfd838e62009-04-20 02:09:13 +00001684
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001685 if (PyType_Ready(&PyGetSetDescr_Type) < 0)
1686 Py_FatalError("Can't initialize get-set descriptor type");
Benjamin Petersonfd838e62009-04-20 02:09:13 +00001687
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001688 if (PyType_Ready(&PyWrapperDescr_Type) < 0)
1689 Py_FatalError("Can't initialize wrapper type");
Benjamin Petersonfd838e62009-04-20 02:09:13 +00001690
Benjamin Petersoneff61f62011-09-01 16:32:31 -04001691 if (PyType_Ready(&_PyMethodWrapper_Type) < 0)
1692 Py_FatalError("Can't initialize method wrapper type");
1693
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001694 if (PyType_Ready(&PyEllipsis_Type) < 0)
1695 Py_FatalError("Can't initialize ellipsis type");
Benjamin Petersonfd838e62009-04-20 02:09:13 +00001696
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001697 if (PyType_Ready(&PyMemberDescr_Type) < 0)
1698 Py_FatalError("Can't initialize member descriptor type");
Benjamin Peterson8bc5b682009-05-09 18:10:51 +00001699
Barry Warsaw409da152012-06-03 16:18:47 -04001700 if (PyType_Ready(&_PyNamespace_Type) < 0)
1701 Py_FatalError("Can't initialize namespace type");
Benjamin Petersone8ea97f2012-10-30 23:27:52 -04001702
Benjamin Petersonc4311282012-10-30 23:21:10 -04001703 if (PyType_Ready(&PyCapsule_Type) < 0)
1704 Py_FatalError("Can't initialize capsule type");
1705
1706 if (PyType_Ready(&PyLongRangeIter_Type) < 0)
1707 Py_FatalError("Can't initialize long range iterator type");
1708
1709 if (PyType_Ready(&PyCell_Type) < 0)
1710 Py_FatalError("Can't initialize cell type");
1711
1712 if (PyType_Ready(&PyInstanceMethod_Type) < 0)
1713 Py_FatalError("Can't initialize instance method type");
1714
1715 if (PyType_Ready(&PyClassMethodDescr_Type) < 0)
1716 Py_FatalError("Can't initialize class method descr type");
1717
1718 if (PyType_Ready(&PyMethodDescr_Type) < 0)
1719 Py_FatalError("Can't initialize method descr type");
1720
1721 if (PyType_Ready(&PyCallIter_Type) < 0)
1722 Py_FatalError("Can't initialize call iter type");
1723
1724 if (PyType_Ready(&PySeqIter_Type) < 0)
1725 Py_FatalError("Can't initialize sequence iterator type");
Yury Selivanov5376ba92015-06-22 12:19:30 -04001726
1727 if (PyType_Ready(&PyCoro_Type) < 0)
1728 Py_FatalError("Can't initialize coroutine type");
1729
1730 if (PyType_Ready(&_PyCoroWrapper_Type) < 0)
1731 Py_FatalError("Can't initialize coroutine wrapper type");
Guido van Rossumba21a492001-08-16 08:17:26 +00001732}
1733
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001734
Guido van Rossum84a90321996-05-22 16:34:47 +00001735#ifdef Py_TRACE_REFS
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001736
Guido van Rossumaacdc9d1996-08-12 21:32:12 +00001737void
Fred Drake100814d2000-07-09 15:48:49 +00001738_Py_NewReference(PyObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001739{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001740 _Py_INC_REFTOTAL;
1741 op->ob_refcnt = 1;
1742 _Py_AddToAllObjects(op, 1);
1743 _Py_INC_TPALLOCS(op);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001744}
1745
Guido van Rossumaacdc9d1996-08-12 21:32:12 +00001746void
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02001747_Py_ForgetReference(PyObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001748{
Guido van Rossumbffd6832000-01-20 22:32:56 +00001749#ifdef SLOW_UNREF_CHECK
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02001750 PyObject *p;
Guido van Rossumbffd6832000-01-20 22:32:56 +00001751#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001752 if (op->ob_refcnt < 0)
1753 Py_FatalError("UNREF negative refcnt");
1754 if (op == &refchain ||
1755 op->_ob_prev->_ob_next != op || op->_ob_next->_ob_prev != op) {
1756 fprintf(stderr, "* ob\n");
1757 _PyObject_Dump(op);
1758 fprintf(stderr, "* op->_ob_prev->_ob_next\n");
1759 _PyObject_Dump(op->_ob_prev->_ob_next);
1760 fprintf(stderr, "* op->_ob_next->_ob_prev\n");
1761 _PyObject_Dump(op->_ob_next->_ob_prev);
1762 Py_FatalError("UNREF invalid object");
1763 }
Guido van Rossum2e8f6141992-09-03 20:32:55 +00001764#ifdef SLOW_UNREF_CHECK
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001765 for (p = refchain._ob_next; p != &refchain; p = p->_ob_next) {
1766 if (p == op)
1767 break;
1768 }
1769 if (p == &refchain) /* Not found */
1770 Py_FatalError("UNREF unknown object");
Guido van Rossum2e8f6141992-09-03 20:32:55 +00001771#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001772 op->_ob_next->_ob_prev = op->_ob_prev;
1773 op->_ob_prev->_ob_next = op->_ob_next;
1774 op->_ob_next = op->_ob_prev = NULL;
1775 _Py_INC_TPFREES(op);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001776}
1777
Guido van Rossumaacdc9d1996-08-12 21:32:12 +00001778void
Fred Drake100814d2000-07-09 15:48:49 +00001779_Py_Dealloc(PyObject *op)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001780{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001781 destructor dealloc = Py_TYPE(op)->tp_dealloc;
1782 _Py_ForgetReference(op);
1783 (*dealloc)(op);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001784}
1785
Tim Peters269b2a62003-04-17 19:52:29 +00001786/* Print all live objects. Because PyObject_Print is called, the
1787 * interpreter must be in a healthy state.
1788 */
Guido van Rossumaacdc9d1996-08-12 21:32:12 +00001789void
Fred Drake100814d2000-07-09 15:48:49 +00001790_Py_PrintReferences(FILE *fp)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001791{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001792 PyObject *op;
1793 fprintf(fp, "Remaining objects:\n");
1794 for (op = refchain._ob_next; op != &refchain; op = op->_ob_next) {
1795 fprintf(fp, "%p [%" PY_FORMAT_SIZE_T "d] ", op, op->ob_refcnt);
1796 if (PyObject_Print(op, fp, 0) != 0)
1797 PyErr_Clear();
1798 putc('\n', fp);
1799 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001800}
1801
Tim Peters269b2a62003-04-17 19:52:29 +00001802/* Print the addresses of all live objects. Unlike _Py_PrintReferences, this
1803 * doesn't make any calls to the Python C API, so is always safe to call.
1804 */
1805void
1806_Py_PrintReferenceAddresses(FILE *fp)
1807{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001808 PyObject *op;
1809 fprintf(fp, "Remaining object addresses:\n");
1810 for (op = refchain._ob_next; op != &refchain; op = op->_ob_next)
1811 fprintf(fp, "%p [%" PY_FORMAT_SIZE_T "d] %s\n", op,
1812 op->ob_refcnt, Py_TYPE(op)->tp_name);
Tim Peters269b2a62003-04-17 19:52:29 +00001813}
1814
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +00001815PyObject *
Fred Drake100814d2000-07-09 15:48:49 +00001816_Py_GetObjects(PyObject *self, PyObject *args)
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +00001817{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001818 int i, n;
1819 PyObject *t = NULL;
1820 PyObject *res, *op;
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +00001821
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001822 if (!PyArg_ParseTuple(args, "i|O", &n, &t))
1823 return NULL;
1824 op = refchain._ob_next;
1825 res = PyList_New(0);
1826 if (res == NULL)
1827 return NULL;
1828 for (i = 0; (n == 0 || i < n) && op != &refchain; i++) {
1829 while (op == self || op == args || op == res || op == t ||
1830 (t != NULL && Py_TYPE(op) != (PyTypeObject *) t)) {
1831 op = op->_ob_next;
1832 if (op == &refchain)
1833 return res;
1834 }
1835 if (PyList_Append(res, op) < 0) {
1836 Py_DECREF(res);
1837 return NULL;
1838 }
1839 op = op->_ob_next;
1840 }
1841 return res;
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +00001842}
1843
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001844#endif
Guido van Rossum97ead3f1996-01-12 01:24:09 +00001845
Benjamin Petersonb173f782009-05-05 22:31:58 +00001846
Guido van Rossum84a90321996-05-22 16:34:47 +00001847/* Hack to force loading of abstract.o */
Martin v. Löwis18e16552006-02-15 17:27:45 +00001848Py_ssize_t (*_Py_abstract_hack)(PyObject *) = PyObject_Size;
Guido van Rossume09fb551997-08-05 02:04:34 +00001849
1850
David Malcolm49526f42012-06-22 14:55:41 -04001851void
1852_PyObject_DebugTypeStats(FILE *out)
1853{
1854 _PyCFunction_DebugMallocStats(out);
1855 _PyDict_DebugMallocStats(out);
1856 _PyFloat_DebugMallocStats(out);
1857 _PyFrame_DebugMallocStats(out);
1858 _PyList_DebugMallocStats(out);
1859 _PyMethod_DebugMallocStats(out);
David Malcolm49526f42012-06-22 14:55:41 -04001860 _PyTuple_DebugMallocStats(out);
1861}
Guido van Rossumb18618d2000-05-03 23:44:39 +00001862
Guido van Rossum86610361998-04-10 22:32:46 +00001863/* These methods are used to control infinite recursion in repr, str, print,
1864 etc. Container objects that may recursively contain themselves,
Martin Panter8d56c022016-05-29 04:13:35 +00001865 e.g. builtin dictionaries and lists, should use Py_ReprEnter() and
Guido van Rossum86610361998-04-10 22:32:46 +00001866 Py_ReprLeave() to avoid infinite recursion.
1867
1868 Py_ReprEnter() returns 0 the first time it is called for a particular
1869 object and 1 every time thereafter. It returns -1 if an exception
1870 occurred. Py_ReprLeave() has no return value.
1871
1872 See dictobject.c and listobject.c for examples of use.
1873*/
1874
Guido van Rossum86610361998-04-10 22:32:46 +00001875int
Fred Drake100814d2000-07-09 15:48:49 +00001876Py_ReprEnter(PyObject *obj)
Guido van Rossum86610361998-04-10 22:32:46 +00001877{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001878 PyObject *dict;
1879 PyObject *list;
1880 Py_ssize_t i;
Guido van Rossum86610361998-04-10 22:32:46 +00001881
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001882 dict = PyThreadState_GetDict();
Antoine Pitrou04d17d32014-03-31 22:04:38 +02001883 /* Ignore a missing thread-state, so that this function can be called
1884 early on startup. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001885 if (dict == NULL)
1886 return 0;
Victor Stinner7a07e452013-11-06 18:57:29 +01001887 list = _PyDict_GetItemId(dict, &PyId_Py_Repr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001888 if (list == NULL) {
1889 list = PyList_New(0);
1890 if (list == NULL)
1891 return -1;
Victor Stinner7a07e452013-11-06 18:57:29 +01001892 if (_PyDict_SetItemId(dict, &PyId_Py_Repr, list) < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001893 return -1;
1894 Py_DECREF(list);
1895 }
1896 i = PyList_GET_SIZE(list);
1897 while (--i >= 0) {
1898 if (PyList_GET_ITEM(list, i) == obj)
1899 return 1;
1900 }
Victor Stinnere901d1f2013-07-17 21:58:41 +02001901 if (PyList_Append(list, obj) < 0)
1902 return -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001903 return 0;
Guido van Rossum86610361998-04-10 22:32:46 +00001904}
1905
1906void
Fred Drake100814d2000-07-09 15:48:49 +00001907Py_ReprLeave(PyObject *obj)
Guido van Rossum86610361998-04-10 22:32:46 +00001908{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001909 PyObject *dict;
1910 PyObject *list;
1911 Py_ssize_t i;
Victor Stinner1b634932013-07-16 22:24:44 +02001912 PyObject *error_type, *error_value, *error_traceback;
1913
1914 PyErr_Fetch(&error_type, &error_value, &error_traceback);
Guido van Rossum86610361998-04-10 22:32:46 +00001915
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001916 dict = PyThreadState_GetDict();
1917 if (dict == NULL)
Victor Stinner1b634932013-07-16 22:24:44 +02001918 goto finally;
1919
Victor Stinner7a07e452013-11-06 18:57:29 +01001920 list = _PyDict_GetItemId(dict, &PyId_Py_Repr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001921 if (list == NULL || !PyList_Check(list))
Victor Stinner1b634932013-07-16 22:24:44 +02001922 goto finally;
1923
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001924 i = PyList_GET_SIZE(list);
1925 /* Count backwards because we always expect obj to be list[-1] */
1926 while (--i >= 0) {
1927 if (PyList_GET_ITEM(list, i) == obj) {
1928 PyList_SetSlice(list, i, i + 1, NULL);
1929 break;
1930 }
1931 }
Victor Stinner1b634932013-07-16 22:24:44 +02001932
1933finally:
1934 /* ignore exceptions because there is no way to report them. */
1935 PyErr_Restore(error_type, error_value, error_traceback);
Guido van Rossum86610361998-04-10 22:32:46 +00001936}
Guido van Rossumd724b232000-03-13 16:01:29 +00001937
Tim Peters803526b2002-07-07 05:13:56 +00001938/* Trashcan support. */
Guido van Rossumd724b232000-03-13 16:01:29 +00001939
Tim Peters803526b2002-07-07 05:13:56 +00001940/* Current call-stack depth of tp_dealloc calls. */
Guido van Rossumd724b232000-03-13 16:01:29 +00001941int _PyTrash_delete_nesting = 0;
Guido van Rossume92e6102000-04-24 15:40:53 +00001942
Tim Peters803526b2002-07-07 05:13:56 +00001943/* List of objects that still need to be cleaned up, singly linked via their
1944 * gc headers' gc_prev pointers.
1945 */
1946PyObject *_PyTrash_delete_later = NULL;
Guido van Rossumd724b232000-03-13 16:01:29 +00001947
Tim Peters803526b2002-07-07 05:13:56 +00001948/* Add op to the _PyTrash_delete_later list. Called when the current
1949 * call-stack depth gets large. op must be a currently untracked gc'ed
1950 * object, with refcount 0. Py_DECREF must already have been called on it.
1951 */
Guido van Rossumd724b232000-03-13 16:01:29 +00001952void
Fred Drake100814d2000-07-09 15:48:49 +00001953_PyTrash_deposit_object(PyObject *op)
Guido van Rossumd724b232000-03-13 16:01:29 +00001954{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001955 assert(PyObject_IS_GC(op));
Antoine Pitrou796564c2013-07-30 19:59:21 +02001956 assert(_PyGC_REFS(op) == _PyGC_REFS_UNTRACKED);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001957 assert(op->ob_refcnt == 0);
1958 _Py_AS_GC(op)->gc.gc_prev = (PyGC_Head *)_PyTrash_delete_later;
1959 _PyTrash_delete_later = op;
Guido van Rossumd724b232000-03-13 16:01:29 +00001960}
1961
Antoine Pitrou2b0218a2012-09-06 00:59:49 +02001962/* The equivalent API, using per-thread state recursion info */
1963void
1964_PyTrash_thread_deposit_object(PyObject *op)
1965{
1966 PyThreadState *tstate = PyThreadState_GET();
1967 assert(PyObject_IS_GC(op));
Antoine Pitrou796564c2013-07-30 19:59:21 +02001968 assert(_PyGC_REFS(op) == _PyGC_REFS_UNTRACKED);
Antoine Pitrou2b0218a2012-09-06 00:59:49 +02001969 assert(op->ob_refcnt == 0);
1970 _Py_AS_GC(op)->gc.gc_prev = (PyGC_Head *) tstate->trash_delete_later;
1971 tstate->trash_delete_later = op;
1972}
1973
Tim Peters803526b2002-07-07 05:13:56 +00001974/* Dealloccate all the objects in the _PyTrash_delete_later list. Called when
1975 * the call-stack unwinds again.
1976 */
Guido van Rossumd724b232000-03-13 16:01:29 +00001977void
Fred Drake100814d2000-07-09 15:48:49 +00001978_PyTrash_destroy_chain(void)
Guido van Rossumd724b232000-03-13 16:01:29 +00001979{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001980 while (_PyTrash_delete_later) {
1981 PyObject *op = _PyTrash_delete_later;
1982 destructor dealloc = Py_TYPE(op)->tp_dealloc;
Neil Schemenauerf589c052002-03-29 03:05:54 +00001983
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001984 _PyTrash_delete_later =
1985 (PyObject*) _Py_AS_GC(op)->gc.gc_prev;
Neil Schemenauerf589c052002-03-29 03:05:54 +00001986
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001987 /* Call the deallocator directly. This used to try to
1988 * fool Py_DECREF into calling it indirectly, but
1989 * Py_DECREF was already called on this object, and in
1990 * assorted non-release builds calling Py_DECREF again ends
1991 * up distorting allocation statistics.
1992 */
1993 assert(op->ob_refcnt == 0);
1994 ++_PyTrash_delete_nesting;
1995 (*dealloc)(op);
1996 --_PyTrash_delete_nesting;
1997 }
Guido van Rossumd724b232000-03-13 16:01:29 +00001998}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001999
Antoine Pitrou2b0218a2012-09-06 00:59:49 +02002000/* The equivalent API, using per-thread state recursion info */
2001void
2002_PyTrash_thread_destroy_chain(void)
2003{
2004 PyThreadState *tstate = PyThreadState_GET();
2005 while (tstate->trash_delete_later) {
2006 PyObject *op = tstate->trash_delete_later;
2007 destructor dealloc = Py_TYPE(op)->tp_dealloc;
2008
2009 tstate->trash_delete_later =
2010 (PyObject*) _Py_AS_GC(op)->gc.gc_prev;
2011
2012 /* Call the deallocator directly. This used to try to
2013 * fool Py_DECREF into calling it indirectly, but
2014 * Py_DECREF was already called on this object, and in
2015 * assorted non-release builds calling Py_DECREF again ends
2016 * up distorting allocation statistics.
2017 */
2018 assert(op->ob_refcnt == 0);
2019 ++tstate->trash_delete_nesting;
2020 (*dealloc)(op);
2021 --tstate->trash_delete_nesting;
2022 }
2023}
2024
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00002025#ifndef Py_TRACE_REFS
2026/* For Py_LIMITED_API, we need an out-of-line version of _Py_Dealloc.
2027 Define this here, so we can undefine the macro. */
2028#undef _Py_Dealloc
2029PyAPI_FUNC(void) _Py_Dealloc(PyObject *);
2030void
2031_Py_Dealloc(PyObject *op)
2032{
2033 _Py_INC_TPFREES(op) _Py_COUNT_ALLOCS_COMMA
2034 (*Py_TYPE(op)->tp_dealloc)(op);
2035}
2036#endif
2037
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002038#ifdef __cplusplus
2039}
2040#endif