blob: c634e707ba1e7a101fec10c26ca487a3b0b07a30 [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}
36#endif /* Py_REF_DEBUG */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000037
Guido van Rossum3f5da241990-12-20 15:06:42 +000038/* Object allocation routines used by NEWOBJ and NEWVAROBJ macros.
39 These are used by the individual routines for object creation.
40 Do not call them otherwise, they do not initialize the object! */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000041
Tim Peters78be7992003-03-23 02:51:01 +000042#ifdef Py_TRACE_REFS
Tim Peters7571a0f2003-03-23 17:52:28 +000043/* Head of circular doubly-linked list of all objects. These are linked
44 * together via the _ob_prev and _ob_next members of a PyObject, which
45 * exist only in a Py_TRACE_REFS build.
46 */
Tim Peters78be7992003-03-23 02:51:01 +000047static PyObject refchain = {&refchain, &refchain};
Tim Peters36eb4df2003-03-23 03:33:13 +000048
Tim Peters7571a0f2003-03-23 17:52:28 +000049/* Insert op at the front of the list of all objects. If force is true,
50 * op is added even if _ob_prev and _ob_next are non-NULL already. If
51 * force is false amd _ob_prev or _ob_next are non-NULL, do nothing.
52 * force should be true if and only if op points to freshly allocated,
53 * uninitialized memory, or you've unlinked op from the list and are
Tim Peters51f8d382003-03-23 18:06:08 +000054 * relinking it into the front.
Tim Peters7571a0f2003-03-23 17:52:28 +000055 * Note that objects are normally added to the list via _Py_NewReference,
56 * which is called by PyObject_Init. Not all objects are initialized that
57 * way, though; exceptions include statically allocated type objects, and
58 * statically allocated singletons (like Py_True and Py_None).
59 */
Tim Peters36eb4df2003-03-23 03:33:13 +000060void
Tim Peters7571a0f2003-03-23 17:52:28 +000061_Py_AddToAllObjects(PyObject *op, int force)
Tim Peters36eb4df2003-03-23 03:33:13 +000062{
Tim Peters7571a0f2003-03-23 17:52:28 +000063#ifdef Py_DEBUG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000064 if (!force) {
65 /* If it's initialized memory, op must be in or out of
66 * the list unambiguously.
67 */
68 assert((op->_ob_prev == NULL) == (op->_ob_next == NULL));
69 }
Tim Peters78be7992003-03-23 02:51:01 +000070#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000071 if (force || op->_ob_prev == NULL) {
72 op->_ob_next = refchain._ob_next;
73 op->_ob_prev = &refchain;
74 refchain._ob_next->_ob_prev = op;
75 refchain._ob_next = op;
76 }
Tim Peters7571a0f2003-03-23 17:52:28 +000077}
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000078#endif /* Py_TRACE_REFS */
Tim Peters78be7992003-03-23 02:51:01 +000079
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +000080#ifdef COUNT_ALLOCS
Guido van Rossumc0b618a1997-05-02 03:12:38 +000081static PyTypeObject *type_list;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000082/* All types are added to type_list, at least when
83 they get one object created. That makes them
84 immortal, which unfortunately contributes to
85 garbage itself. If unlist_types_without_objects
86 is set, they will be removed from the type_list
87 once the last object is deallocated. */
Benjamin Petersona4a37fe2009-01-11 17:13:55 +000088static int unlist_types_without_objects;
89extern Py_ssize_t tuple_zero_allocs, fast_tuple_allocs;
90extern Py_ssize_t quick_int_allocs, quick_neg_int_allocs;
91extern Py_ssize_t null_strings, one_strings;
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +000092void
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000093dump_counts(FILE* f)
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +000094{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000095 PyTypeObject *tp;
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +000096
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000097 for (tp = type_list; tp; tp = tp->tp_next)
98 fprintf(f, "%s alloc'd: %" PY_FORMAT_SIZE_T "d, "
99 "freed: %" PY_FORMAT_SIZE_T "d, "
100 "max in use: %" PY_FORMAT_SIZE_T "d\n",
101 tp->tp_name, tp->tp_allocs, tp->tp_frees,
102 tp->tp_maxalloc);
103 fprintf(f, "fast tuple allocs: %" PY_FORMAT_SIZE_T "d, "
104 "empty: %" PY_FORMAT_SIZE_T "d\n",
105 fast_tuple_allocs, tuple_zero_allocs);
106 fprintf(f, "fast int allocs: pos: %" PY_FORMAT_SIZE_T "d, "
107 "neg: %" PY_FORMAT_SIZE_T "d\n",
108 quick_int_allocs, quick_neg_int_allocs);
109 fprintf(f, "null strings: %" PY_FORMAT_SIZE_T "d, "
110 "1-strings: %" PY_FORMAT_SIZE_T "d\n",
111 null_strings, one_strings);
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +0000112}
113
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000114PyObject *
Fred Drake100814d2000-07-09 15:48:49 +0000115get_counts(void)
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000116{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000117 PyTypeObject *tp;
118 PyObject *result;
119 PyObject *v;
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000120
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000121 result = PyList_New(0);
122 if (result == NULL)
123 return NULL;
124 for (tp = type_list; tp; tp = tp->tp_next) {
125 v = Py_BuildValue("(snnn)", tp->tp_name, tp->tp_allocs,
126 tp->tp_frees, tp->tp_maxalloc);
127 if (v == NULL) {
128 Py_DECREF(result);
129 return NULL;
130 }
131 if (PyList_Append(result, v) < 0) {
132 Py_DECREF(v);
133 Py_DECREF(result);
134 return NULL;
135 }
136 Py_DECREF(v);
137 }
138 return result;
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000139}
140
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +0000141void
Fred Drake100814d2000-07-09 15:48:49 +0000142inc_count(PyTypeObject *tp)
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +0000143{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000144 if (tp->tp_next == NULL && tp->tp_prev == NULL) {
145 /* first time; insert in linked list */
146 if (tp->tp_next != NULL) /* sanity check */
147 Py_FatalError("XXX inc_count sanity check");
148 if (type_list)
149 type_list->tp_prev = tp;
150 tp->tp_next = type_list;
151 /* Note that as of Python 2.2, heap-allocated type objects
152 * can go away, but this code requires that they stay alive
153 * until program exit. That's why we're careful with
154 * refcounts here. type_list gets a new reference to tp,
155 * while ownership of the reference type_list used to hold
156 * (if any) was transferred to tp->tp_next in the line above.
157 * tp is thus effectively immortal after this.
158 */
159 Py_INCREF(tp);
160 type_list = tp;
Tim Peters3e40c7f2003-03-23 03:04:32 +0000161#ifdef Py_TRACE_REFS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000162 /* Also insert in the doubly-linked list of all objects,
163 * if not already there.
164 */
165 _Py_AddToAllObjects((PyObject *)tp, 0);
Tim Peters78be7992003-03-23 02:51:01 +0000166#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000167 }
168 tp->tp_allocs++;
169 if (tp->tp_allocs - tp->tp_frees > tp->tp_maxalloc)
170 tp->tp_maxalloc = tp->tp_allocs - tp->tp_frees;
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +0000171}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000172
173void dec_count(PyTypeObject *tp)
174{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000175 tp->tp_frees++;
176 if (unlist_types_without_objects &&
177 tp->tp_allocs == tp->tp_frees) {
178 /* unlink the type from type_list */
179 if (tp->tp_prev)
180 tp->tp_prev->tp_next = tp->tp_next;
181 else
182 type_list = tp->tp_next;
183 if (tp->tp_next)
184 tp->tp_next->tp_prev = tp->tp_prev;
185 tp->tp_next = tp->tp_prev = NULL;
186 Py_DECREF(tp);
187 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000188}
189
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +0000190#endif
191
Tim Peters7c321a82002-07-09 02:57:01 +0000192#ifdef Py_REF_DEBUG
193/* Log a fatal error; doesn't return. */
194void
195_Py_NegativeRefcount(const char *fname, int lineno, PyObject *op)
196{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000197 char buf[300];
Tim Peters7c321a82002-07-09 02:57:01 +0000198
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000199 PyOS_snprintf(buf, sizeof(buf),
200 "%s:%i object at %p has negative ref count "
201 "%" PY_FORMAT_SIZE_T "d",
202 fname, lineno, op, op->ob_refcnt);
203 Py_FatalError(buf);
Tim Peters7c321a82002-07-09 02:57:01 +0000204}
205
206#endif /* Py_REF_DEBUG */
207
Thomas Heller1328b522004-04-22 17:23:49 +0000208void
209Py_IncRef(PyObject *o)
210{
211 Py_XINCREF(o);
212}
213
214void
215Py_DecRef(PyObject *o)
216{
217 Py_XDECREF(o);
218}
219
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000220PyObject *
Fred Drake100814d2000-07-09 15:48:49 +0000221PyObject_Init(PyObject *op, PyTypeObject *tp)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000222{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000223 if (op == NULL)
224 return PyErr_NoMemory();
225 /* Any changes should be reflected in PyObject_INIT (objimpl.h) */
226 Py_TYPE(op) = tp;
227 _Py_NewReference(op);
228 return op;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000229}
230
Guido van Rossumb18618d2000-05-03 23:44:39 +0000231PyVarObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000232PyObject_InitVar(PyVarObject *op, PyTypeObject *tp, Py_ssize_t size)
Guido van Rossumb18618d2000-05-03 23:44:39 +0000233{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000234 if (op == NULL)
235 return (PyVarObject *) PyErr_NoMemory();
236 /* Any changes should be reflected in PyObject_INIT_VAR */
237 op->ob_size = size;
238 Py_TYPE(op) = tp;
239 _Py_NewReference((PyObject *)op);
240 return op;
Guido van Rossumb18618d2000-05-03 23:44:39 +0000241}
242
243PyObject *
Fred Drake100814d2000-07-09 15:48:49 +0000244_PyObject_New(PyTypeObject *tp)
Guido van Rossumb18618d2000-05-03 23:44:39 +0000245{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000246 PyObject *op;
247 op = (PyObject *) PyObject_MALLOC(_PyObject_SIZE(tp));
248 if (op == NULL)
249 return PyErr_NoMemory();
250 return PyObject_INIT(op, tp);
Guido van Rossumb18618d2000-05-03 23:44:39 +0000251}
252
Guido van Rossumd0c87ee1997-05-15 21:31:03 +0000253PyVarObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000254_PyObject_NewVar(PyTypeObject *tp, Py_ssize_t nitems)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000255{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000256 PyVarObject *op;
257 const size_t size = _PyObject_VAR_SIZE(tp, nitems);
258 op = (PyVarObject *) PyObject_MALLOC(size);
259 if (op == NULL)
260 return (PyVarObject *)PyErr_NoMemory();
261 return PyObject_INIT_VAR(op, tp, nitems);
Guido van Rossumb18618d2000-05-03 23:44:39 +0000262}
263
Antoine Pitrou796564c2013-07-30 19:59:21 +0200264void
265PyObject_CallFinalizer(PyObject *self)
266{
267 PyTypeObject *tp = Py_TYPE(self);
268
269 /* The former could happen on heaptypes created from the C API, e.g.
270 PyType_FromSpec(). */
271 if (!PyType_HasFeature(tp, Py_TPFLAGS_HAVE_FINALIZE) ||
272 tp->tp_finalize == NULL)
273 return;
274 /* tp_finalize should only be called once. */
275 if (PyType_IS_GC(tp) && _PyGC_FINALIZED(self))
276 return;
277
278 tp->tp_finalize(self);
279 if (PyType_IS_GC(tp))
280 _PyGC_SET_FINALIZED(self, 1);
281}
282
283int
284PyObject_CallFinalizerFromDealloc(PyObject *self)
285{
286 Py_ssize_t refcnt;
287
288 /* Temporarily resurrect the object. */
289 if (self->ob_refcnt != 0) {
290 Py_FatalError("PyObject_CallFinalizerFromDealloc called on "
291 "object with a non-zero refcount");
292 }
293 self->ob_refcnt = 1;
294
295 PyObject_CallFinalizer(self);
296
297 /* Undo the temporary resurrection; can't use DECREF here, it would
298 * cause a recursive call.
299 */
300 assert(self->ob_refcnt > 0);
301 if (--self->ob_refcnt == 0)
302 return 0; /* this is the normal path out */
303
304 /* tp_finalize resurrected it! Make it look like the original Py_DECREF
305 * never happened.
306 */
307 refcnt = self->ob_refcnt;
308 _Py_NewReference(self);
309 self->ob_refcnt = refcnt;
310
311 if (PyType_IS_GC(Py_TYPE(self))) {
312 assert(_PyGC_REFS(self) != _PyGC_REFS_UNTRACKED);
313 }
314 /* If Py_REF_DEBUG, _Py_NewReference bumped _Py_RefTotal, so
315 * we need to undo that. */
316 _Py_DEC_REFTOTAL;
317 /* If Py_TRACE_REFS, _Py_NewReference re-added self to the object
318 * chain, so no more to do there.
319 * If COUNT_ALLOCS, the original decref bumped tp_frees, and
320 * _Py_NewReference bumped tp_allocs: both of those need to be
321 * undone.
322 */
323#ifdef COUNT_ALLOCS
324 --Py_TYPE(self)->tp_frees;
325 --Py_TYPE(self)->tp_allocs;
326#endif
327 return -1;
328}
329
Antoine Pitrouc47bd4a2010-07-27 22:08:27 +0000330int
331PyObject_Print(PyObject *op, FILE *fp, int flags)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000332{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000333 int ret = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000334 if (PyErr_CheckSignals())
335 return -1;
Guido van Rossum9b00dfa1998-04-28 16:06:54 +0000336#ifdef USE_STACKCHECK
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000337 if (PyOS_CheckStack()) {
338 PyErr_SetString(PyExc_MemoryError, "stack overflow");
339 return -1;
340 }
Guido van Rossum9b00dfa1998-04-28 16:06:54 +0000341#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000342 clearerr(fp); /* Clear any previous error condition */
343 if (op == NULL) {
344 Py_BEGIN_ALLOW_THREADS
345 fprintf(fp, "<nil>");
346 Py_END_ALLOW_THREADS
347 }
348 else {
349 if (op->ob_refcnt <= 0)
350 /* XXX(twouters) cast refcount to long until %zd is
351 universally available */
352 Py_BEGIN_ALLOW_THREADS
353 fprintf(fp, "<refcnt %ld at %p>",
354 (long)op->ob_refcnt, op);
355 Py_END_ALLOW_THREADS
356 else {
357 PyObject *s;
358 if (flags & Py_PRINT_RAW)
359 s = PyObject_Str(op);
360 else
361 s = PyObject_Repr(op);
362 if (s == NULL)
363 ret = -1;
364 else if (PyBytes_Check(s)) {
365 fwrite(PyBytes_AS_STRING(s), 1,
366 PyBytes_GET_SIZE(s), fp);
367 }
368 else if (PyUnicode_Check(s)) {
369 PyObject *t;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200370 t = PyUnicode_AsEncodedString(s, "utf-8", "backslashreplace");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000371 if (t == NULL)
372 ret = 0;
373 else {
374 fwrite(PyBytes_AS_STRING(t), 1,
375 PyBytes_GET_SIZE(t), fp);
Victor Stinnerba6b4302010-05-17 09:33:42 +0000376 Py_DECREF(t);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000377 }
378 }
379 else {
380 PyErr_Format(PyExc_TypeError,
381 "str() or repr() returned '%.100s'",
382 s->ob_type->tp_name);
383 ret = -1;
384 }
385 Py_XDECREF(s);
386 }
387 }
388 if (ret == 0) {
389 if (ferror(fp)) {
390 PyErr_SetFromErrno(PyExc_IOError);
391 clearerr(fp);
392 ret = -1;
393 }
394 }
395 return ret;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000396}
397
Guido van Rossum38938152006-08-21 23:36:26 +0000398/* For debugging convenience. Set a breakpoint here and call it from your DLL */
399void
Thomas Woutersb2137042007-02-01 18:02:27 +0000400_Py_BreakPoint(void)
Guido van Rossum38938152006-08-21 23:36:26 +0000401{
402}
403
Neal Norwitz1a997502003-01-13 20:13:12 +0000404
Barry Warsaw9bf16442001-01-23 16:24:35 +0000405/* For debugging convenience. See Misc/gdbinit for some useful gdb hooks */
Guido van Rossum38938152006-08-21 23:36:26 +0000406void
407_PyObject_Dump(PyObject* op)
Barry Warsaw9bf16442001-01-23 16:24:35 +0000408{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000409 if (op == NULL)
410 fprintf(stderr, "NULL\n");
411 else {
Georg Brandldfd73442009-04-05 11:47:34 +0000412#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000413 PyGILState_STATE gil;
Georg Brandldfd73442009-04-05 11:47:34 +0000414#endif
Victor Stinnere5132102013-08-26 13:49:06 +0200415 PyObject *error_type, *error_value, *error_traceback;
416
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000417 fprintf(stderr, "object : ");
Georg Brandldfd73442009-04-05 11:47:34 +0000418#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000419 gil = PyGILState_Ensure();
Georg Brandldfd73442009-04-05 11:47:34 +0000420#endif
Victor Stinnere5132102013-08-26 13:49:06 +0200421
422 PyErr_Fetch(&error_type, &error_value, &error_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000423 (void)PyObject_Print(op, stderr, 0);
Victor Stinnere5132102013-08-26 13:49:06 +0200424 PyErr_Restore(error_type, error_value, error_traceback);
425
Georg Brandldfd73442009-04-05 11:47:34 +0000426#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000427 PyGILState_Release(gil);
Georg Brandldfd73442009-04-05 11:47:34 +0000428#endif
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,
461 because it may clear it (directly or indirectly) and so the
462 caller looses its exception */
463 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,
510 because it may 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) {
580 result = PyObject_CallFunctionObjArgs(func, NULL);
581 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
Guido van Rossum9a4e95c2006-12-19 21:35:46 +0000631static char *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:
673 /* XXX Special-case None so it doesn't show as NoneType() */
674 PyErr_Format(PyExc_TypeError,
675 "unorderable types: %.100s() %s %.100s()",
676 v->ob_type->tp_name,
677 opstrings[op],
678 w->ob_type->tp_name);
679 return NULL;
680 }
681 Py_INCREF(res);
682 return res;
Guido van Rossume797ec12001-01-17 15:24:28 +0000683}
684
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000685/* Perform a rich comparison with object result. This wraps do_richcompare()
686 with a check for NULL arguments and a recursion check. */
Guido van Rossume797ec12001-01-17 15:24:28 +0000687
Guido van Rossume797ec12001-01-17 15:24:28 +0000688PyObject *
689PyObject_RichCompare(PyObject *v, PyObject *w, int op)
690{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000691 PyObject *res;
Guido van Rossume797ec12001-01-17 15:24:28 +0000692
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000693 assert(Py_LT <= op && op <= Py_GE);
694 if (v == NULL || w == NULL) {
695 if (!PyErr_Occurred())
696 PyErr_BadInternalCall();
697 return NULL;
698 }
699 if (Py_EnterRecursiveCall(" in comparison"))
700 return NULL;
701 res = do_richcompare(v, w, op);
702 Py_LeaveRecursiveCall();
703 return res;
Guido van Rossume797ec12001-01-17 15:24:28 +0000704}
705
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000706/* Perform a rich comparison with integer result. This wraps
707 PyObject_RichCompare(), returning -1 for error, 0 for false, 1 for true. */
Guido van Rossume797ec12001-01-17 15:24:28 +0000708int
709PyObject_RichCompareBool(PyObject *v, PyObject *w, int op)
710{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000711 PyObject *res;
712 int ok;
Guido van Rossume797ec12001-01-17 15:24:28 +0000713
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000714 /* Quick result when objects are the same.
715 Guarantees that identity implies equality. */
716 if (v == w) {
717 if (op == Py_EQ)
718 return 1;
719 else if (op == Py_NE)
720 return 0;
721 }
Mark Dickinson4a1f5932008-11-12 23:23:36 +0000722
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000723 res = PyObject_RichCompare(v, w, op);
724 if (res == NULL)
725 return -1;
726 if (PyBool_Check(res))
727 ok = (res == Py_True);
728 else
729 ok = PyObject_IsTrue(res);
730 Py_DECREF(res);
731 return ok;
Guido van Rossume797ec12001-01-17 15:24:28 +0000732}
Fred Drake13634cf2000-06-29 19:17:04 +0000733
Antoine Pitrouce4a9da2011-11-21 20:46:33 +0100734Py_hash_t
Nick Coghland1abd252008-07-15 15:46:38 +0000735PyObject_HashNotImplemented(PyObject *v)
736{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000737 PyErr_Format(PyExc_TypeError, "unhashable type: '%.200s'",
738 Py_TYPE(v)->tp_name);
739 return -1;
Nick Coghland1abd252008-07-15 15:46:38 +0000740}
Fred Drake13634cf2000-06-29 19:17:04 +0000741
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000742Py_hash_t
Fred Drake100814d2000-07-09 15:48:49 +0000743PyObject_Hash(PyObject *v)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000744{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000745 PyTypeObject *tp = Py_TYPE(v);
746 if (tp->tp_hash != NULL)
747 return (*tp->tp_hash)(v);
748 /* To keep to the general practice that inheriting
749 * solely from object in C code should work without
750 * an explicit call to PyType_Ready, we implicitly call
751 * PyType_Ready here and then check the tp_hash slot again
752 */
753 if (tp->tp_dict == NULL) {
754 if (PyType_Ready(tp) < 0)
755 return -1;
756 if (tp->tp_hash != NULL)
757 return (*tp->tp_hash)(v);
758 }
759 /* Otherwise, the object can't be hashed */
760 return PyObject_HashNotImplemented(v);
Guido van Rossum9bfef441993-03-29 10:43:31 +0000761}
762
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000763PyObject *
Jeremy Hyltonaf68c872005-12-10 18:50:16 +0000764PyObject_GetAttrString(PyObject *v, const char *name)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000765{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000766 PyObject *w, *res;
Guido van Rossumd8eb1b31996-08-09 20:52:03 +0000767
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000768 if (Py_TYPE(v)->tp_getattr != NULL)
769 return (*Py_TYPE(v)->tp_getattr)(v, (char*)name);
770 w = PyUnicode_InternFromString(name);
771 if (w == NULL)
772 return NULL;
773 res = PyObject_GetAttr(v, w);
Victor Stinner59af08f2012-03-22 02:09:08 +0100774 Py_DECREF(w);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000775 return res;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000776}
777
778int
Jeremy Hyltonaf68c872005-12-10 18:50:16 +0000779PyObject_HasAttrString(PyObject *v, const char *name)
Guido van Rossumed18fdc1993-07-11 19:55:34 +0000780{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000781 PyObject *res = PyObject_GetAttrString(v, name);
782 if (res != NULL) {
783 Py_DECREF(res);
784 return 1;
785 }
786 PyErr_Clear();
787 return 0;
Guido van Rossumed18fdc1993-07-11 19:55:34 +0000788}
789
790int
Jeremy Hyltonaf68c872005-12-10 18:50:16 +0000791PyObject_SetAttrString(PyObject *v, const char *name, PyObject *w)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000792{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000793 PyObject *s;
794 int res;
Guido van Rossumd8eb1b31996-08-09 20:52:03 +0000795
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000796 if (Py_TYPE(v)->tp_setattr != NULL)
797 return (*Py_TYPE(v)->tp_setattr)(v, (char*)name, w);
798 s = PyUnicode_InternFromString(name);
799 if (s == NULL)
800 return -1;
801 res = PyObject_SetAttr(v, s, w);
802 Py_XDECREF(s);
803 return res;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000804}
805
Benjamin Petersonbfebb7b2011-12-15 15:34:02 -0500806int
807_PyObject_IsAbstract(PyObject *obj)
808{
809 int res;
810 PyObject* isabstract;
Benjamin Petersonbfebb7b2011-12-15 15:34:02 -0500811
812 if (obj == NULL)
813 return 0;
814
815 isabstract = _PyObject_GetAttrId(obj, &PyId___isabstractmethod__);
816 if (isabstract == NULL) {
817 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
818 PyErr_Clear();
819 return 0;
820 }
821 return -1;
822 }
823 res = PyObject_IsTrue(isabstract);
824 Py_DECREF(isabstract);
825 return res;
826}
827
Guido van Rossum98ff96a1997-05-20 18:34:44 +0000828PyObject *
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200829_PyObject_GetAttrId(PyObject *v, _Py_Identifier *name)
830{
831 PyObject *result;
Martin v. Löwisd10759f2011-11-07 13:00:05 +0100832 PyObject *oname = _PyUnicode_FromId(name); /* borrowed */
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200833 if (!oname)
834 return NULL;
835 result = PyObject_GetAttr(v, oname);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200836 return result;
837}
838
839int
840_PyObject_HasAttrId(PyObject *v, _Py_Identifier *name)
841{
842 int result;
Martin v. Löwisd10759f2011-11-07 13:00:05 +0100843 PyObject *oname = _PyUnicode_FromId(name); /* borrowed */
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200844 if (!oname)
845 return -1;
846 result = PyObject_HasAttr(v, oname);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200847 return result;
848}
849
850int
851_PyObject_SetAttrId(PyObject *v, _Py_Identifier *name, PyObject *w)
852{
853 int result;
Martin v. Löwisd10759f2011-11-07 13:00:05 +0100854 PyObject *oname = _PyUnicode_FromId(name); /* borrowed */
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200855 if (!oname)
856 return -1;
857 result = PyObject_SetAttr(v, oname, w);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200858 return result;
859}
860
861PyObject *
Fred Drake100814d2000-07-09 15:48:49 +0000862PyObject_GetAttr(PyObject *v, PyObject *name)
Guido van Rossum98ff96a1997-05-20 18:34:44 +0000863{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000864 PyTypeObject *tp = Py_TYPE(v);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000865
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000866 if (!PyUnicode_Check(name)) {
867 PyErr_Format(PyExc_TypeError,
868 "attribute name must be string, not '%.200s'",
869 name->ob_type->tp_name);
870 return NULL;
871 }
872 if (tp->tp_getattro != NULL)
873 return (*tp->tp_getattro)(v, name);
874 if (tp->tp_getattr != NULL) {
875 char *name_str = _PyUnicode_AsString(name);
876 if (name_str == NULL)
877 return NULL;
878 return (*tp->tp_getattr)(v, name_str);
879 }
880 PyErr_Format(PyExc_AttributeError,
881 "'%.50s' object has no attribute '%U'",
882 tp->tp_name, name);
883 return NULL;
Guido van Rossum98ff96a1997-05-20 18:34:44 +0000884}
885
886int
Fred Drake100814d2000-07-09 15:48:49 +0000887PyObject_HasAttr(PyObject *v, PyObject *name)
Guido van Rossum98ff96a1997-05-20 18:34:44 +0000888{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000889 PyObject *res = PyObject_GetAttr(v, name);
890 if (res != NULL) {
891 Py_DECREF(res);
892 return 1;
893 }
894 PyErr_Clear();
895 return 0;
Guido van Rossum98ff96a1997-05-20 18:34:44 +0000896}
897
898int
Fred Drake100814d2000-07-09 15:48:49 +0000899PyObject_SetAttr(PyObject *v, PyObject *name, PyObject *value)
Guido van Rossum98ff96a1997-05-20 18:34:44 +0000900{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000901 PyTypeObject *tp = Py_TYPE(v);
902 int err;
Marc-André Lemburge44e5072000-09-18 16:20:57 +0000903
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000904 if (!PyUnicode_Check(name)) {
905 PyErr_Format(PyExc_TypeError,
906 "attribute name must be string, not '%.200s'",
907 name->ob_type->tp_name);
908 return -1;
909 }
910 Py_INCREF(name);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000911
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000912 PyUnicode_InternInPlace(&name);
913 if (tp->tp_setattro != NULL) {
914 err = (*tp->tp_setattro)(v, name, value);
915 Py_DECREF(name);
916 return err;
917 }
918 if (tp->tp_setattr != NULL) {
919 char *name_str = _PyUnicode_AsString(name);
920 if (name_str == NULL)
921 return -1;
922 err = (*tp->tp_setattr)(v, name_str, value);
923 Py_DECREF(name);
924 return err;
925 }
926 Py_DECREF(name);
927 assert(name->ob_refcnt >= 1);
928 if (tp->tp_getattr == NULL && tp->tp_getattro == NULL)
929 PyErr_Format(PyExc_TypeError,
930 "'%.100s' object has no attributes "
931 "(%s .%U)",
932 tp->tp_name,
933 value==NULL ? "del" : "assign to",
934 name);
935 else
936 PyErr_Format(PyExc_TypeError,
937 "'%.100s' object has only read-only attributes "
938 "(%s .%U)",
939 tp->tp_name,
940 value==NULL ? "del" : "assign to",
941 name);
942 return -1;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000943}
944
945/* Helper to get a pointer to an object's __dict__ slot, if any */
946
947PyObject **
948_PyObject_GetDictPtr(PyObject *obj)
949{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000950 Py_ssize_t dictoffset;
951 PyTypeObject *tp = Py_TYPE(obj);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000952
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000953 dictoffset = tp->tp_dictoffset;
954 if (dictoffset == 0)
955 return NULL;
956 if (dictoffset < 0) {
957 Py_ssize_t tsize;
958 size_t size;
Guido van Rossum2eb0b872002-03-01 22:24:49 +0000959
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000960 tsize = ((PyVarObject *)obj)->ob_size;
961 if (tsize < 0)
962 tsize = -tsize;
963 size = _PyObject_VAR_SIZE(tp, tsize);
Guido van Rossum2eb0b872002-03-01 22:24:49 +0000964
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000965 dictoffset += (long)size;
966 assert(dictoffset > 0);
967 assert(dictoffset % SIZEOF_VOID_P == 0);
968 }
969 return (PyObject **) ((char *)obj + dictoffset);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000970}
971
Tim Peters6d6c1a32001-08-02 04:15:00 +0000972PyObject *
Raymond Hettinger1da1dbf2003-03-17 19:46:11 +0000973PyObject_SelfIter(PyObject *obj)
Raymond Hettinger01538262003-03-17 08:24:35 +0000974{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000975 Py_INCREF(obj);
976 return obj;
Raymond Hettinger01538262003-03-17 08:24:35 +0000977}
978
Antoine Pitroua7013882012-04-05 00:04:20 +0200979/* Convenience function to get a builtin from its name */
980PyObject *
981_PyObject_GetBuiltin(const char *name)
982{
Victor Stinner53e9ec42013-11-07 00:43:05 +0100983 PyObject *mod_name, *mod, *attr;
984
Victor Stinnerbd303c12013-11-07 23:07:29 +0100985 mod_name = _PyUnicode_FromId(&PyId_builtins); /* borrowed */
Victor Stinner53e9ec42013-11-07 00:43:05 +0100986 if (mod_name == NULL)
987 return NULL;
988 mod = PyImport_Import(mod_name);
Antoine Pitroua7013882012-04-05 00:04:20 +0200989 if (mod == NULL)
990 return NULL;
991 attr = PyObject_GetAttrString(mod, name);
992 Py_DECREF(mod);
993 return attr;
994}
995
Amaury Forgeot d'Arcf343e012009-01-12 23:58:21 +0000996/* Helper used when the __next__ method is removed from a type:
997 tp_iternext is never NULL and can be safely called without checking
998 on every iteration.
999 */
1000
1001PyObject *
1002_PyObject_NextNotImplemented(PyObject *self)
1003{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001004 PyErr_Format(PyExc_TypeError,
1005 "'%.200s' object is not iterable",
1006 Py_TYPE(self)->tp_name);
1007 return NULL;
Amaury Forgeot d'Arcf343e012009-01-12 23:58:21 +00001008}
1009
Michael W. Hudson1593f502004-09-14 17:09:47 +00001010/* Generic GetAttr functions - put these in your tp_[gs]etattro slot */
1011
Raymond Hettinger01538262003-03-17 08:24:35 +00001012PyObject *
Antoine Pitrou1a9a9d52010-08-28 18:17:03 +00001013_PyObject_GenericGetAttrWithDict(PyObject *obj, PyObject *name, PyObject *dict)
Tim Peters6d6c1a32001-08-02 04:15:00 +00001014{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001015 PyTypeObject *tp = Py_TYPE(obj);
1016 PyObject *descr = NULL;
1017 PyObject *res = NULL;
1018 descrgetfunc f;
1019 Py_ssize_t dictoffset;
1020 PyObject **dictptr;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001021
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001022 if (!PyUnicode_Check(name)){
1023 PyErr_Format(PyExc_TypeError,
1024 "attribute name must be string, not '%.200s'",
1025 name->ob_type->tp_name);
1026 return NULL;
1027 }
1028 else
1029 Py_INCREF(name);
Guido van Rossumebca9fc2001-12-04 15:54:53 +00001030
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001031 if (tp->tp_dict == NULL) {
1032 if (PyType_Ready(tp) < 0)
1033 goto done;
1034 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001035
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001036 descr = _PyType_Lookup(tp, name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001037 Py_XINCREF(descr);
Michael W. Hudsonb2c7de42003-08-15 13:07:47 +00001038
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001039 f = NULL;
1040 if (descr != NULL) {
1041 f = descr->ob_type->tp_descr_get;
1042 if (f != NULL && PyDescr_IsData(descr)) {
1043 res = f(descr, obj, (PyObject *)obj->ob_type);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001044 goto done;
1045 }
1046 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001047
Antoine Pitrou1a9a9d52010-08-28 18:17:03 +00001048 if (dict == NULL) {
1049 /* Inline _PyObject_GetDictPtr */
1050 dictoffset = tp->tp_dictoffset;
1051 if (dictoffset != 0) {
1052 if (dictoffset < 0) {
1053 Py_ssize_t tsize;
1054 size_t size;
Guido van Rossumc66ff442002-08-19 16:50:48 +00001055
Antoine Pitrou1a9a9d52010-08-28 18:17:03 +00001056 tsize = ((PyVarObject *)obj)->ob_size;
1057 if (tsize < 0)
1058 tsize = -tsize;
1059 size = _PyObject_VAR_SIZE(tp, tsize);
Guido van Rossumc66ff442002-08-19 16:50:48 +00001060
Antoine Pitrou1a9a9d52010-08-28 18:17:03 +00001061 dictoffset += (long)size;
1062 assert(dictoffset > 0);
1063 assert(dictoffset % SIZEOF_VOID_P == 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001064 }
Antoine Pitrou1a9a9d52010-08-28 18:17:03 +00001065 dictptr = (PyObject **) ((char *)obj + dictoffset);
1066 dict = *dictptr;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001067 }
1068 }
Antoine Pitrou1a9a9d52010-08-28 18:17:03 +00001069 if (dict != NULL) {
1070 Py_INCREF(dict);
1071 res = PyDict_GetItem(dict, name);
1072 if (res != NULL) {
1073 Py_INCREF(res);
Antoine Pitrou1a9a9d52010-08-28 18:17:03 +00001074 Py_DECREF(dict);
1075 goto done;
1076 }
1077 Py_DECREF(dict);
1078 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001079
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001080 if (f != NULL) {
1081 res = f(descr, obj, (PyObject *)Py_TYPE(obj));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001082 goto done;
1083 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001084
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001085 if (descr != NULL) {
1086 res = descr;
Victor Stinner2d01dc02012-03-09 00:44:13 +01001087 descr = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001088 goto done;
1089 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001090
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001091 PyErr_Format(PyExc_AttributeError,
1092 "'%.50s' object has no attribute '%U'",
1093 tp->tp_name, name);
Guido van Rossumebca9fc2001-12-04 15:54:53 +00001094 done:
Victor Stinner2d01dc02012-03-09 00:44:13 +01001095 Py_XDECREF(descr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001096 Py_DECREF(name);
1097 return res;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001098}
1099
Antoine Pitrou1a9a9d52010-08-28 18:17:03 +00001100PyObject *
1101PyObject_GenericGetAttr(PyObject *obj, PyObject *name)
1102{
1103 return _PyObject_GenericGetAttrWithDict(obj, name, NULL);
1104}
1105
Tim Peters6d6c1a32001-08-02 04:15:00 +00001106int
Antoine Pitrou1a9a9d52010-08-28 18:17:03 +00001107_PyObject_GenericSetAttrWithDict(PyObject *obj, PyObject *name,
1108 PyObject *value, PyObject *dict)
Tim Peters6d6c1a32001-08-02 04:15:00 +00001109{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001110 PyTypeObject *tp = Py_TYPE(obj);
1111 PyObject *descr;
1112 descrsetfunc f;
1113 PyObject **dictptr;
1114 int res = -1;
Guido van Rossumebca9fc2001-12-04 15:54:53 +00001115
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001116 if (!PyUnicode_Check(name)){
1117 PyErr_Format(PyExc_TypeError,
1118 "attribute name must be string, not '%.200s'",
1119 name->ob_type->tp_name);
1120 return -1;
1121 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001122
Benjamin Peterson74529ad2012-03-09 07:25:32 -08001123 if (tp->tp_dict == NULL && PyType_Ready(tp) < 0)
1124 return -1;
1125
1126 Py_INCREF(name);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001127
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001128 descr = _PyType_Lookup(tp, name);
Victor Stinner2d01dc02012-03-09 00:44:13 +01001129 Py_XINCREF(descr);
1130
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001131 f = NULL;
1132 if (descr != NULL) {
1133 f = descr->ob_type->tp_descr_set;
1134 if (f != NULL && PyDescr_IsData(descr)) {
1135 res = f(descr, obj, value);
1136 goto done;
1137 }
1138 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001139
Antoine Pitrou1a9a9d52010-08-28 18:17:03 +00001140 if (dict == NULL) {
1141 dictptr = _PyObject_GetDictPtr(obj);
1142 if (dictptr != NULL) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001143 res = _PyObjectDict_SetItem(Py_TYPE(obj), dictptr, name, value);
1144 if (res < 0 && PyErr_ExceptionMatches(PyExc_KeyError))
1145 PyErr_SetObject(PyExc_AttributeError, name);
1146 goto done;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001147 }
Antoine Pitrou1a9a9d52010-08-28 18:17:03 +00001148 }
1149 if (dict != NULL) {
1150 Py_INCREF(dict);
1151 if (value == NULL)
1152 res = PyDict_DelItem(dict, name);
1153 else
1154 res = PyDict_SetItem(dict, name, value);
Benjamin Peterson74529ad2012-03-09 07:25:32 -08001155 Py_DECREF(dict);
Antoine Pitrou1a9a9d52010-08-28 18:17:03 +00001156 if (res < 0 && PyErr_ExceptionMatches(PyExc_KeyError))
1157 PyErr_SetObject(PyExc_AttributeError, name);
Antoine Pitrou1a9a9d52010-08-28 18:17:03 +00001158 goto done;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001159 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001160
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001161 if (f != NULL) {
1162 res = f(descr, obj, value);
1163 goto done;
1164 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001165
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001166 if (descr == NULL) {
1167 PyErr_Format(PyExc_AttributeError,
1168 "'%.100s' object has no attribute '%U'",
1169 tp->tp_name, name);
1170 goto done;
1171 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001172
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001173 PyErr_Format(PyExc_AttributeError,
1174 "'%.50s' object attribute '%U' is read-only",
1175 tp->tp_name, name);
Guido van Rossumebca9fc2001-12-04 15:54:53 +00001176 done:
Victor Stinner2d01dc02012-03-09 00:44:13 +01001177 Py_XDECREF(descr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001178 Py_DECREF(name);
1179 return res;
Guido van Rossum98ff96a1997-05-20 18:34:44 +00001180}
1181
Antoine Pitrou1a9a9d52010-08-28 18:17:03 +00001182int
1183PyObject_GenericSetAttr(PyObject *obj, PyObject *name, PyObject *value)
1184{
1185 return _PyObject_GenericSetAttrWithDict(obj, name, value, NULL);
1186}
1187
Benjamin Peterson8eb12692012-02-19 19:59:10 -05001188int
1189PyObject_GenericSetDict(PyObject *obj, PyObject *value, void *context)
1190{
1191 PyObject *dict, **dictptr = _PyObject_GetDictPtr(obj);
1192 if (dictptr == NULL) {
1193 PyErr_SetString(PyExc_AttributeError,
1194 "This object has no __dict__");
1195 return -1;
1196 }
1197 if (value == NULL) {
1198 PyErr_SetString(PyExc_TypeError, "cannot delete __dict__");
1199 return -1;
1200 }
1201 if (!PyDict_Check(value)) {
1202 PyErr_Format(PyExc_TypeError,
1203 "__dict__ must be set to a dictionary, "
1204 "not a '%.200s'", Py_TYPE(value)->tp_name);
1205 return -1;
1206 }
1207 dict = *dictptr;
1208 Py_XINCREF(value);
1209 *dictptr = value;
1210 Py_XDECREF(dict);
1211 return 0;
1212}
1213
Antoine Pitrou1a9a9d52010-08-28 18:17:03 +00001214
Guido van Rossum6ac258d1993-05-12 08:24:20 +00001215/* Test a value used as condition, e.g., in a for or if statement.
1216 Return -1 if an error occurred */
1217
1218int
Fred Drake100814d2000-07-09 15:48:49 +00001219PyObject_IsTrue(PyObject *v)
Guido van Rossum6ac258d1993-05-12 08:24:20 +00001220{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001221 Py_ssize_t res;
1222 if (v == Py_True)
1223 return 1;
1224 if (v == Py_False)
1225 return 0;
1226 if (v == Py_None)
1227 return 0;
1228 else if (v->ob_type->tp_as_number != NULL &&
1229 v->ob_type->tp_as_number->nb_bool != NULL)
1230 res = (*v->ob_type->tp_as_number->nb_bool)(v);
1231 else if (v->ob_type->tp_as_mapping != NULL &&
1232 v->ob_type->tp_as_mapping->mp_length != NULL)
1233 res = (*v->ob_type->tp_as_mapping->mp_length)(v);
1234 else if (v->ob_type->tp_as_sequence != NULL &&
1235 v->ob_type->tp_as_sequence->sq_length != NULL)
1236 res = (*v->ob_type->tp_as_sequence->sq_length)(v);
1237 else
1238 return 1;
1239 /* if it is negative, it should be either -1 or -2 */
1240 return (res > 0) ? 1 : Py_SAFE_DOWNCAST(res, Py_ssize_t, int);
Guido van Rossum6ac258d1993-05-12 08:24:20 +00001241}
1242
Tim Peters803526b2002-07-07 05:13:56 +00001243/* equivalent of 'not v'
Guido van Rossumc3d3f961998-04-09 17:53:59 +00001244 Return -1 if an error occurred */
1245
1246int
Fred Drake100814d2000-07-09 15:48:49 +00001247PyObject_Not(PyObject *v)
Guido van Rossumc3d3f961998-04-09 17:53:59 +00001248{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001249 int res;
1250 res = PyObject_IsTrue(v);
1251 if (res < 0)
1252 return res;
1253 return res == 0;
Guido van Rossumc3d3f961998-04-09 17:53:59 +00001254}
1255
Guido van Rossum49b11fe1995-01-26 00:38:22 +00001256/* Test whether an object can be called */
1257
1258int
Fred Drake100814d2000-07-09 15:48:49 +00001259PyCallable_Check(PyObject *x)
Guido van Rossum49b11fe1995-01-26 00:38:22 +00001260{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001261 if (x == NULL)
1262 return 0;
1263 return x->ob_type->tp_call != NULL;
Guido van Rossum49b11fe1995-01-26 00:38:22 +00001264}
1265
Tim Peters7eea37e2001-09-04 22:08:56 +00001266
Georg Brandle32b4222007-03-10 22:13:27 +00001267/* Helper for PyObject_Dir without arguments: returns the local scope. */
1268static PyObject *
Guido van Rossumad7d8d12007-04-13 01:39:34 +00001269_dir_locals(void)
Tim Peters305b5852001-09-17 02:38:46 +00001270{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001271 PyObject *names;
Victor Stinner41bb43a2013-10-29 01:19:37 +01001272 PyObject *locals;
Tim Peters305b5852001-09-17 02:38:46 +00001273
Victor Stinner41bb43a2013-10-29 01:19:37 +01001274 locals = PyEval_GetLocals();
1275 if (locals == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001276 return NULL;
Tim Peters305b5852001-09-17 02:38:46 +00001277
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001278 names = PyMapping_Keys(locals);
1279 if (!names)
1280 return NULL;
1281 if (!PyList_Check(names)) {
1282 PyErr_Format(PyExc_TypeError,
1283 "dir(): expected keys() of locals to be a list, "
1284 "not '%.200s'", Py_TYPE(names)->tp_name);
1285 Py_DECREF(names);
1286 return NULL;
1287 }
Benjamin Peterson3bbb7222011-06-11 16:12:08 -05001288 if (PyList_Sort(names)) {
1289 Py_DECREF(names);
1290 return NULL;
1291 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001292 /* the locals don't need to be DECREF'd */
1293 return names;
Georg Brandle32b4222007-03-10 22:13:27 +00001294}
1295
Benjamin Peterson82b00c12011-05-24 11:09:06 -05001296/* Helper for PyObject_Dir: object introspection. */
Georg Brandle32b4222007-03-10 22:13:27 +00001297static PyObject *
1298_dir_object(PyObject *obj)
1299{
Benjamin Peterson3bbb7222011-06-11 16:12:08 -05001300 PyObject *result, *sorted;
Benjamin Petersonce798522012-01-22 11:24:29 -05001301 PyObject *dirfunc = _PyObject_LookupSpecial(obj, &PyId___dir__);
Georg Brandle32b4222007-03-10 22:13:27 +00001302
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001303 assert(obj);
1304 if (dirfunc == NULL) {
Benjamin Peterson82b00c12011-05-24 11:09:06 -05001305 if (!PyErr_Occurred())
1306 PyErr_SetString(PyExc_TypeError, "object does not provide __dir__");
1307 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001308 }
Benjamin Peterson82b00c12011-05-24 11:09:06 -05001309 /* use __dir__ */
1310 result = PyObject_CallFunctionObjArgs(dirfunc, NULL);
1311 Py_DECREF(dirfunc);
1312 if (result == NULL)
1313 return NULL;
Benjamin Peterson3bbb7222011-06-11 16:12:08 -05001314 /* return sorted(result) */
1315 sorted = PySequence_List(result);
1316 Py_DECREF(result);
1317 if (sorted == NULL)
1318 return NULL;
1319 if (PyList_Sort(sorted)) {
1320 Py_DECREF(sorted);
1321 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001322 }
Benjamin Peterson3bbb7222011-06-11 16:12:08 -05001323 return sorted;
Georg Brandle32b4222007-03-10 22:13:27 +00001324}
1325
1326/* Implementation of dir() -- if obj is NULL, returns the names in the current
1327 (local) scope. Otherwise, performs introspection of the object: returns a
1328 sorted list of attribute names (supposedly) accessible from the object
1329*/
1330PyObject *
1331PyObject_Dir(PyObject *obj)
1332{
Benjamin Peterson3bbb7222011-06-11 16:12:08 -05001333 return (obj == NULL) ? _dir_locals() : _dir_object(obj);
Tim Peters7eea37e2001-09-04 22:08:56 +00001334}
Guido van Rossum49b11fe1995-01-26 00:38:22 +00001335
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001336/*
Raymond Hettinger66d2be82011-07-28 09:55:13 -07001337None is a non-NULL undefined value.
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001338There is (and should be!) no way to create other objects of this type,
Guido van Rossum3f5da241990-12-20 15:06:42 +00001339so there is exactly one (which is indestructible, by the way).
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001340*/
1341
Guido van Rossum0c182a11992-03-27 17:26:13 +00001342/* ARGSUSED */
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001343static PyObject *
Fred Drake100814d2000-07-09 15:48:49 +00001344none_repr(PyObject *op)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001345{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001346 return PyUnicode_FromString("None");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001347}
1348
Barry Warsaw9bf16442001-01-23 16:24:35 +00001349/* ARGUSED */
1350static void
Tim Peters803526b2002-07-07 05:13:56 +00001351none_dealloc(PyObject* ignore)
Barry Warsaw9bf16442001-01-23 16:24:35 +00001352{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001353 /* This should never get called, but we also don't want to SEGV if
1354 * we accidentally decref None out of existence.
1355 */
1356 Py_FatalError("deallocating None");
Barry Warsaw9bf16442001-01-23 16:24:35 +00001357}
1358
Benjamin Petersonc4607ae2011-07-29 18:19:43 -05001359static PyObject *
1360none_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
1361{
1362 if (PyTuple_GET_SIZE(args) || (kwargs && PyDict_Size(kwargs))) {
1363 PyErr_SetString(PyExc_TypeError, "NoneType takes no arguments");
1364 return NULL;
1365 }
1366 Py_RETURN_NONE;
1367}
1368
Raymond Hettinger66d2be82011-07-28 09:55:13 -07001369static int
1370none_bool(PyObject *v)
1371{
1372 return 0;
1373}
1374
1375static PyNumberMethods none_as_number = {
1376 0, /* nb_add */
1377 0, /* nb_subtract */
1378 0, /* nb_multiply */
1379 0, /* nb_remainder */
1380 0, /* nb_divmod */
1381 0, /* nb_power */
1382 0, /* nb_negative */
1383 0, /* nb_positive */
1384 0, /* nb_absolute */
1385 (inquiry)none_bool, /* nb_bool */
1386 0, /* nb_invert */
1387 0, /* nb_lshift */
1388 0, /* nb_rshift */
1389 0, /* nb_and */
1390 0, /* nb_xor */
1391 0, /* nb_or */
1392 0, /* nb_int */
1393 0, /* nb_reserved */
1394 0, /* nb_float */
1395 0, /* nb_inplace_add */
1396 0, /* nb_inplace_subtract */
1397 0, /* nb_inplace_multiply */
1398 0, /* nb_inplace_remainder */
1399 0, /* nb_inplace_power */
1400 0, /* nb_inplace_lshift */
1401 0, /* nb_inplace_rshift */
1402 0, /* nb_inplace_and */
1403 0, /* nb_inplace_xor */
1404 0, /* nb_inplace_or */
1405 0, /* nb_floor_divide */
1406 0, /* nb_true_divide */
1407 0, /* nb_inplace_floor_divide */
1408 0, /* nb_inplace_true_divide */
1409 0, /* nb_index */
1410};
Barry Warsaw9bf16442001-01-23 16:24:35 +00001411
Alexandre Vassalotti65846c62013-11-30 17:55:48 -08001412PyTypeObject _PyNone_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001413 PyVarObject_HEAD_INIT(&PyType_Type, 0)
1414 "NoneType",
1415 0,
1416 0,
1417 none_dealloc, /*tp_dealloc*/ /*never called*/
1418 0, /*tp_print*/
1419 0, /*tp_getattr*/
1420 0, /*tp_setattr*/
1421 0, /*tp_reserved*/
1422 none_repr, /*tp_repr*/
Raymond Hettinger66d2be82011-07-28 09:55:13 -07001423 &none_as_number, /*tp_as_number*/
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001424 0, /*tp_as_sequence*/
1425 0, /*tp_as_mapping*/
1426 0, /*tp_hash */
Benjamin Petersonc4607ae2011-07-29 18:19:43 -05001427 0, /*tp_call */
1428 0, /*tp_str */
1429 0, /*tp_getattro */
1430 0, /*tp_setattro */
1431 0, /*tp_as_buffer */
1432 Py_TPFLAGS_DEFAULT, /*tp_flags */
1433 0, /*tp_doc */
1434 0, /*tp_traverse */
1435 0, /*tp_clear */
1436 0, /*tp_richcompare */
1437 0, /*tp_weaklistoffset */
1438 0, /*tp_iter */
1439 0, /*tp_iternext */
1440 0, /*tp_methods */
1441 0, /*tp_members */
1442 0, /*tp_getset */
1443 0, /*tp_base */
1444 0, /*tp_dict */
1445 0, /*tp_descr_get */
1446 0, /*tp_descr_set */
1447 0, /*tp_dictoffset */
1448 0, /*tp_init */
1449 0, /*tp_alloc */
1450 none_new, /*tp_new */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001451};
1452
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001453PyObject _Py_NoneStruct = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001454 _PyObject_EXTRA_INIT
Alexandre Vassalotti65846c62013-11-30 17:55:48 -08001455 1, &_PyNone_Type
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001456};
1457
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +00001458/* NotImplemented is an object that can be used to signal that an
1459 operation is not implemented for the given type combination. */
1460
1461static PyObject *
1462NotImplemented_repr(PyObject *op)
1463{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001464 return PyUnicode_FromString("NotImplemented");
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +00001465}
1466
Benjamin Peterson18d7d7a2011-07-29 18:27:44 -05001467static PyObject *
Alexandre Vassalottic49477b2013-11-24 02:53:45 -08001468NotImplemented_reduce(PyObject *op)
1469{
1470 return PyUnicode_FromString("NotImplemented");
1471}
1472
1473static PyMethodDef notimplemented_methods[] = {
1474 {"__reduce__", (PyCFunction)NotImplemented_reduce, METH_NOARGS, NULL},
1475 {NULL, NULL}
1476};
1477
1478static PyObject *
Benjamin Peterson18d7d7a2011-07-29 18:27:44 -05001479notimplemented_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
1480{
1481 if (PyTuple_GET_SIZE(args) || (kwargs && PyDict_Size(kwargs))) {
1482 PyErr_SetString(PyExc_TypeError, "NotImplementedType takes no arguments");
1483 return NULL;
1484 }
Brian Curtindfc80e32011-08-10 20:28:54 -05001485 Py_RETURN_NOTIMPLEMENTED;
Benjamin Peterson18d7d7a2011-07-29 18:27:44 -05001486}
1487
Armin Ronacher226b1db2012-10-06 14:28:58 +02001488static void
1489notimplemented_dealloc(PyObject* ignore)
1490{
1491 /* This should never get called, but we also don't want to SEGV if
1492 * we accidentally decref NotImplemented out of existence.
1493 */
1494 Py_FatalError("deallocating NotImplemented");
1495}
1496
Alexandre Vassalotti65846c62013-11-30 17:55:48 -08001497PyTypeObject _PyNotImplemented_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001498 PyVarObject_HEAD_INIT(&PyType_Type, 0)
1499 "NotImplementedType",
1500 0,
1501 0,
Armin Ronacher226b1db2012-10-06 14:28:58 +02001502 notimplemented_dealloc, /*tp_dealloc*/ /*never called*/
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001503 0, /*tp_print*/
1504 0, /*tp_getattr*/
1505 0, /*tp_setattr*/
1506 0, /*tp_reserved*/
1507 NotImplemented_repr, /*tp_repr*/
1508 0, /*tp_as_number*/
1509 0, /*tp_as_sequence*/
1510 0, /*tp_as_mapping*/
1511 0, /*tp_hash */
Benjamin Peterson18d7d7a2011-07-29 18:27:44 -05001512 0, /*tp_call */
1513 0, /*tp_str */
1514 0, /*tp_getattro */
1515 0, /*tp_setattro */
1516 0, /*tp_as_buffer */
1517 Py_TPFLAGS_DEFAULT, /*tp_flags */
1518 0, /*tp_doc */
1519 0, /*tp_traverse */
1520 0, /*tp_clear */
1521 0, /*tp_richcompare */
1522 0, /*tp_weaklistoffset */
1523 0, /*tp_iter */
1524 0, /*tp_iternext */
Alexandre Vassalottic49477b2013-11-24 02:53:45 -08001525 notimplemented_methods, /*tp_methods */
Benjamin Peterson18d7d7a2011-07-29 18:27:44 -05001526 0, /*tp_members */
1527 0, /*tp_getset */
1528 0, /*tp_base */
1529 0, /*tp_dict */
1530 0, /*tp_descr_get */
1531 0, /*tp_descr_set */
1532 0, /*tp_dictoffset */
1533 0, /*tp_init */
1534 0, /*tp_alloc */
1535 notimplemented_new, /*tp_new */
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +00001536};
1537
1538PyObject _Py_NotImplementedStruct = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001539 _PyObject_EXTRA_INIT
Alexandre Vassalotti65846c62013-11-30 17:55:48 -08001540 1, &_PyNotImplemented_Type
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +00001541};
1542
Guido van Rossumba21a492001-08-16 08:17:26 +00001543void
1544_Py_ReadyTypes(void)
1545{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001546 if (PyType_Ready(&PyType_Type) < 0)
1547 Py_FatalError("Can't initialize type type");
Guido van Rossumba21a492001-08-16 08:17:26 +00001548
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001549 if (PyType_Ready(&_PyWeakref_RefType) < 0)
1550 Py_FatalError("Can't initialize weakref type");
Fred Drake0a4dd392004-07-02 18:57:45 +00001551
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001552 if (PyType_Ready(&_PyWeakref_CallableProxyType) < 0)
1553 Py_FatalError("Can't initialize callable weakref proxy type");
Benjamin Petersonfd838e62009-04-20 02:09:13 +00001554
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001555 if (PyType_Ready(&_PyWeakref_ProxyType) < 0)
1556 Py_FatalError("Can't initialize weakref proxy type");
Benjamin Petersonfd838e62009-04-20 02:09:13 +00001557
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001558 if (PyType_Ready(&PyBool_Type) < 0)
1559 Py_FatalError("Can't initialize bool type");
Guido van Rossum77f6a652002-04-03 22:41:51 +00001560
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001561 if (PyType_Ready(&PyByteArray_Type) < 0)
1562 Py_FatalError("Can't initialize bytearray type");
Guido van Rossum4dfe8a12006-04-22 23:28:04 +00001563
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001564 if (PyType_Ready(&PyBytes_Type) < 0)
1565 Py_FatalError("Can't initialize 'str'");
Guido van Rossumcacfc072002-05-24 19:01:59 +00001566
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001567 if (PyType_Ready(&PyList_Type) < 0)
1568 Py_FatalError("Can't initialize list type");
Guido van Rossumba21a492001-08-16 08:17:26 +00001569
Alexandre Vassalotti65846c62013-11-30 17:55:48 -08001570 if (PyType_Ready(&_PyNone_Type) < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001571 Py_FatalError("Can't initialize None type");
Guido van Rossumba21a492001-08-16 08:17:26 +00001572
Alexandre Vassalotti65846c62013-11-30 17:55:48 -08001573 if (PyType_Ready(&_PyNotImplemented_Type) < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001574 Py_FatalError("Can't initialize NotImplemented type");
Benjamin Petersonae937c02009-04-18 20:54:08 +00001575
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001576 if (PyType_Ready(&PyTraceBack_Type) < 0)
1577 Py_FatalError("Can't initialize traceback type");
Benjamin Petersonae937c02009-04-18 20:54:08 +00001578
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001579 if (PyType_Ready(&PySuper_Type) < 0)
1580 Py_FatalError("Can't initialize super type");
Benjamin Petersonae937c02009-04-18 20:54:08 +00001581
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001582 if (PyType_Ready(&PyBaseObject_Type) < 0)
1583 Py_FatalError("Can't initialize object type");
Benjamin Petersonae937c02009-04-18 20:54:08 +00001584
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001585 if (PyType_Ready(&PyRange_Type) < 0)
1586 Py_FatalError("Can't initialize range type");
Benjamin Petersonae937c02009-04-18 20:54:08 +00001587
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001588 if (PyType_Ready(&PyDict_Type) < 0)
1589 Py_FatalError("Can't initialize dict type");
Benjamin Petersonae937c02009-04-18 20:54:08 +00001590
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001591 if (PyType_Ready(&PySet_Type) < 0)
1592 Py_FatalError("Can't initialize set type");
Benjamin Petersonae937c02009-04-18 20:54:08 +00001593
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001594 if (PyType_Ready(&PyUnicode_Type) < 0)
1595 Py_FatalError("Can't initialize str type");
Benjamin Petersonae937c02009-04-18 20:54:08 +00001596
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001597 if (PyType_Ready(&PySlice_Type) < 0)
1598 Py_FatalError("Can't initialize slice type");
Benjamin Petersonae937c02009-04-18 20:54:08 +00001599
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001600 if (PyType_Ready(&PyStaticMethod_Type) < 0)
1601 Py_FatalError("Can't initialize static method type");
Benjamin Petersonae937c02009-04-18 20:54:08 +00001602
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001603 if (PyType_Ready(&PyComplex_Type) < 0)
1604 Py_FatalError("Can't initialize complex type");
Skip Montanaroba1e0f42009-10-18 14:25:35 +00001605
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001606 if (PyType_Ready(&PyFloat_Type) < 0)
1607 Py_FatalError("Can't initialize float type");
Benjamin Petersonae937c02009-04-18 20:54:08 +00001608
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001609 if (PyType_Ready(&PyLong_Type) < 0)
1610 Py_FatalError("Can't initialize int type");
Benjamin Petersonae937c02009-04-18 20:54:08 +00001611
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001612 if (PyType_Ready(&PyFrozenSet_Type) < 0)
1613 Py_FatalError("Can't initialize frozenset type");
Benjamin Petersonae937c02009-04-18 20:54:08 +00001614
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001615 if (PyType_Ready(&PyProperty_Type) < 0)
1616 Py_FatalError("Can't initialize property type");
Benjamin Petersonae937c02009-04-18 20:54:08 +00001617
Stefan Krah9a2d99e2012-02-25 12:24:21 +01001618 if (PyType_Ready(&_PyManagedBuffer_Type) < 0)
1619 Py_FatalError("Can't initialize managed buffer type");
1620
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001621 if (PyType_Ready(&PyMemoryView_Type) < 0)
1622 Py_FatalError("Can't initialize memoryview type");
Benjamin Petersonae937c02009-04-18 20:54:08 +00001623
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001624 if (PyType_Ready(&PyTuple_Type) < 0)
1625 Py_FatalError("Can't initialize tuple type");
Benjamin Petersonae937c02009-04-18 20:54:08 +00001626
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001627 if (PyType_Ready(&PyEnum_Type) < 0)
1628 Py_FatalError("Can't initialize enumerate type");
Benjamin Petersonae937c02009-04-18 20:54:08 +00001629
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001630 if (PyType_Ready(&PyReversed_Type) < 0)
1631 Py_FatalError("Can't initialize reversed type");
Guido van Rossum47b9ff62006-08-24 00:41:19 +00001632
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001633 if (PyType_Ready(&PyStdPrinter_Type) < 0)
1634 Py_FatalError("Can't initialize StdPrinter");
Benjamin Petersonfd838e62009-04-20 02:09:13 +00001635
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001636 if (PyType_Ready(&PyCode_Type) < 0)
1637 Py_FatalError("Can't initialize code type");
Benjamin Petersonfd838e62009-04-20 02:09:13 +00001638
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001639 if (PyType_Ready(&PyFrame_Type) < 0)
1640 Py_FatalError("Can't initialize frame type");
Benjamin Petersonfd838e62009-04-20 02:09:13 +00001641
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001642 if (PyType_Ready(&PyCFunction_Type) < 0)
1643 Py_FatalError("Can't initialize builtin function type");
Benjamin Petersonfd838e62009-04-20 02:09:13 +00001644
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001645 if (PyType_Ready(&PyMethod_Type) < 0)
1646 Py_FatalError("Can't initialize method type");
Benjamin Petersonfd838e62009-04-20 02:09:13 +00001647
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001648 if (PyType_Ready(&PyFunction_Type) < 0)
1649 Py_FatalError("Can't initialize function type");
Benjamin Petersonfd838e62009-04-20 02:09:13 +00001650
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001651 if (PyType_Ready(&PyDictProxy_Type) < 0)
1652 Py_FatalError("Can't initialize dict proxy type");
Benjamin Petersonfd838e62009-04-20 02:09:13 +00001653
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001654 if (PyType_Ready(&PyGen_Type) < 0)
1655 Py_FatalError("Can't initialize generator type");
Benjamin Petersonfd838e62009-04-20 02:09:13 +00001656
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001657 if (PyType_Ready(&PyGetSetDescr_Type) < 0)
1658 Py_FatalError("Can't initialize get-set descriptor type");
Benjamin Petersonfd838e62009-04-20 02:09:13 +00001659
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001660 if (PyType_Ready(&PyWrapperDescr_Type) < 0)
1661 Py_FatalError("Can't initialize wrapper type");
Benjamin Petersonfd838e62009-04-20 02:09:13 +00001662
Benjamin Petersoneff61f62011-09-01 16:32:31 -04001663 if (PyType_Ready(&_PyMethodWrapper_Type) < 0)
1664 Py_FatalError("Can't initialize method wrapper type");
1665
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001666 if (PyType_Ready(&PyEllipsis_Type) < 0)
1667 Py_FatalError("Can't initialize ellipsis type");
Benjamin Petersonfd838e62009-04-20 02:09:13 +00001668
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001669 if (PyType_Ready(&PyMemberDescr_Type) < 0)
1670 Py_FatalError("Can't initialize member descriptor type");
Benjamin Peterson8bc5b682009-05-09 18:10:51 +00001671
Barry Warsaw409da152012-06-03 16:18:47 -04001672 if (PyType_Ready(&_PyNamespace_Type) < 0)
1673 Py_FatalError("Can't initialize namespace type");
Benjamin Petersone8ea97f2012-10-30 23:27:52 -04001674
Benjamin Petersonc4311282012-10-30 23:21:10 -04001675 if (PyType_Ready(&PyCapsule_Type) < 0)
1676 Py_FatalError("Can't initialize capsule type");
1677
1678 if (PyType_Ready(&PyLongRangeIter_Type) < 0)
1679 Py_FatalError("Can't initialize long range iterator type");
1680
1681 if (PyType_Ready(&PyCell_Type) < 0)
1682 Py_FatalError("Can't initialize cell type");
1683
1684 if (PyType_Ready(&PyInstanceMethod_Type) < 0)
1685 Py_FatalError("Can't initialize instance method type");
1686
1687 if (PyType_Ready(&PyClassMethodDescr_Type) < 0)
1688 Py_FatalError("Can't initialize class method descr type");
1689
1690 if (PyType_Ready(&PyMethodDescr_Type) < 0)
1691 Py_FatalError("Can't initialize method descr type");
1692
1693 if (PyType_Ready(&PyCallIter_Type) < 0)
1694 Py_FatalError("Can't initialize call iter type");
1695
1696 if (PyType_Ready(&PySeqIter_Type) < 0)
1697 Py_FatalError("Can't initialize sequence iterator type");
Guido van Rossumba21a492001-08-16 08:17:26 +00001698}
1699
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001700
Guido van Rossum84a90321996-05-22 16:34:47 +00001701#ifdef Py_TRACE_REFS
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001702
Guido van Rossumaacdc9d1996-08-12 21:32:12 +00001703void
Fred Drake100814d2000-07-09 15:48:49 +00001704_Py_NewReference(PyObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001705{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001706 _Py_INC_REFTOTAL;
1707 op->ob_refcnt = 1;
1708 _Py_AddToAllObjects(op, 1);
1709 _Py_INC_TPALLOCS(op);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001710}
1711
Guido van Rossumaacdc9d1996-08-12 21:32:12 +00001712void
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02001713_Py_ForgetReference(PyObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001714{
Guido van Rossumbffd6832000-01-20 22:32:56 +00001715#ifdef SLOW_UNREF_CHECK
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02001716 PyObject *p;
Guido van Rossumbffd6832000-01-20 22:32:56 +00001717#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001718 if (op->ob_refcnt < 0)
1719 Py_FatalError("UNREF negative refcnt");
1720 if (op == &refchain ||
1721 op->_ob_prev->_ob_next != op || op->_ob_next->_ob_prev != op) {
1722 fprintf(stderr, "* ob\n");
1723 _PyObject_Dump(op);
1724 fprintf(stderr, "* op->_ob_prev->_ob_next\n");
1725 _PyObject_Dump(op->_ob_prev->_ob_next);
1726 fprintf(stderr, "* op->_ob_next->_ob_prev\n");
1727 _PyObject_Dump(op->_ob_next->_ob_prev);
1728 Py_FatalError("UNREF invalid object");
1729 }
Guido van Rossum2e8f6141992-09-03 20:32:55 +00001730#ifdef SLOW_UNREF_CHECK
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001731 for (p = refchain._ob_next; p != &refchain; p = p->_ob_next) {
1732 if (p == op)
1733 break;
1734 }
1735 if (p == &refchain) /* Not found */
1736 Py_FatalError("UNREF unknown object");
Guido van Rossum2e8f6141992-09-03 20:32:55 +00001737#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001738 op->_ob_next->_ob_prev = op->_ob_prev;
1739 op->_ob_prev->_ob_next = op->_ob_next;
1740 op->_ob_next = op->_ob_prev = NULL;
1741 _Py_INC_TPFREES(op);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001742}
1743
Guido van Rossumaacdc9d1996-08-12 21:32:12 +00001744void
Fred Drake100814d2000-07-09 15:48:49 +00001745_Py_Dealloc(PyObject *op)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001746{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001747 destructor dealloc = Py_TYPE(op)->tp_dealloc;
1748 _Py_ForgetReference(op);
1749 (*dealloc)(op);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001750}
1751
Tim Peters269b2a62003-04-17 19:52:29 +00001752/* Print all live objects. Because PyObject_Print is called, the
1753 * interpreter must be in a healthy state.
1754 */
Guido van Rossumaacdc9d1996-08-12 21:32:12 +00001755void
Fred Drake100814d2000-07-09 15:48:49 +00001756_Py_PrintReferences(FILE *fp)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001757{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001758 PyObject *op;
1759 fprintf(fp, "Remaining objects:\n");
1760 for (op = refchain._ob_next; op != &refchain; op = op->_ob_next) {
1761 fprintf(fp, "%p [%" PY_FORMAT_SIZE_T "d] ", op, op->ob_refcnt);
1762 if (PyObject_Print(op, fp, 0) != 0)
1763 PyErr_Clear();
1764 putc('\n', fp);
1765 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001766}
1767
Tim Peters269b2a62003-04-17 19:52:29 +00001768/* Print the addresses of all live objects. Unlike _Py_PrintReferences, this
1769 * doesn't make any calls to the Python C API, so is always safe to call.
1770 */
1771void
1772_Py_PrintReferenceAddresses(FILE *fp)
1773{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001774 PyObject *op;
1775 fprintf(fp, "Remaining object addresses:\n");
1776 for (op = refchain._ob_next; op != &refchain; op = op->_ob_next)
1777 fprintf(fp, "%p [%" PY_FORMAT_SIZE_T "d] %s\n", op,
1778 op->ob_refcnt, Py_TYPE(op)->tp_name);
Tim Peters269b2a62003-04-17 19:52:29 +00001779}
1780
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +00001781PyObject *
Fred Drake100814d2000-07-09 15:48:49 +00001782_Py_GetObjects(PyObject *self, PyObject *args)
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +00001783{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001784 int i, n;
1785 PyObject *t = NULL;
1786 PyObject *res, *op;
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +00001787
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001788 if (!PyArg_ParseTuple(args, "i|O", &n, &t))
1789 return NULL;
1790 op = refchain._ob_next;
1791 res = PyList_New(0);
1792 if (res == NULL)
1793 return NULL;
1794 for (i = 0; (n == 0 || i < n) && op != &refchain; i++) {
1795 while (op == self || op == args || op == res || op == t ||
1796 (t != NULL && Py_TYPE(op) != (PyTypeObject *) t)) {
1797 op = op->_ob_next;
1798 if (op == &refchain)
1799 return res;
1800 }
1801 if (PyList_Append(res, op) < 0) {
1802 Py_DECREF(res);
1803 return NULL;
1804 }
1805 op = op->_ob_next;
1806 }
1807 return res;
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +00001808}
1809
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001810#endif
Guido van Rossum97ead3f1996-01-12 01:24:09 +00001811
Benjamin Petersonb173f782009-05-05 22:31:58 +00001812/* Hack to force loading of pycapsule.o */
1813PyTypeObject *_PyCapsule_hack = &PyCapsule_Type;
1814
1815
Guido van Rossum84a90321996-05-22 16:34:47 +00001816/* Hack to force loading of abstract.o */
Martin v. Löwis18e16552006-02-15 17:27:45 +00001817Py_ssize_t (*_Py_abstract_hack)(PyObject *) = PyObject_Size;
Guido van Rossume09fb551997-08-05 02:04:34 +00001818
1819
David Malcolm49526f42012-06-22 14:55:41 -04001820void
1821_PyObject_DebugTypeStats(FILE *out)
1822{
1823 _PyCFunction_DebugMallocStats(out);
1824 _PyDict_DebugMallocStats(out);
1825 _PyFloat_DebugMallocStats(out);
1826 _PyFrame_DebugMallocStats(out);
1827 _PyList_DebugMallocStats(out);
1828 _PyMethod_DebugMallocStats(out);
David Malcolm49526f42012-06-22 14:55:41 -04001829 _PyTuple_DebugMallocStats(out);
1830}
Guido van Rossumb18618d2000-05-03 23:44:39 +00001831
Guido van Rossum86610361998-04-10 22:32:46 +00001832/* These methods are used to control infinite recursion in repr, str, print,
1833 etc. Container objects that may recursively contain themselves,
1834 e.g. builtin dictionaries and lists, should used Py_ReprEnter() and
1835 Py_ReprLeave() to avoid infinite recursion.
1836
1837 Py_ReprEnter() returns 0 the first time it is called for a particular
1838 object and 1 every time thereafter. It returns -1 if an exception
1839 occurred. Py_ReprLeave() has no return value.
1840
1841 See dictobject.c and listobject.c for examples of use.
1842*/
1843
Guido van Rossum86610361998-04-10 22:32:46 +00001844int
Fred Drake100814d2000-07-09 15:48:49 +00001845Py_ReprEnter(PyObject *obj)
Guido van Rossum86610361998-04-10 22:32:46 +00001846{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001847 PyObject *dict;
1848 PyObject *list;
1849 Py_ssize_t i;
Guido van Rossum86610361998-04-10 22:32:46 +00001850
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001851 dict = PyThreadState_GetDict();
1852 if (dict == NULL)
1853 return 0;
Victor Stinner7a07e452013-11-06 18:57:29 +01001854 list = _PyDict_GetItemId(dict, &PyId_Py_Repr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001855 if (list == NULL) {
1856 list = PyList_New(0);
1857 if (list == NULL)
1858 return -1;
Victor Stinner7a07e452013-11-06 18:57:29 +01001859 if (_PyDict_SetItemId(dict, &PyId_Py_Repr, list) < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001860 return -1;
1861 Py_DECREF(list);
1862 }
1863 i = PyList_GET_SIZE(list);
1864 while (--i >= 0) {
1865 if (PyList_GET_ITEM(list, i) == obj)
1866 return 1;
1867 }
Victor Stinnere901d1f2013-07-17 21:58:41 +02001868 if (PyList_Append(list, obj) < 0)
1869 return -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001870 return 0;
Guido van Rossum86610361998-04-10 22:32:46 +00001871}
1872
1873void
Fred Drake100814d2000-07-09 15:48:49 +00001874Py_ReprLeave(PyObject *obj)
Guido van Rossum86610361998-04-10 22:32:46 +00001875{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001876 PyObject *dict;
1877 PyObject *list;
1878 Py_ssize_t i;
Victor Stinner1b634932013-07-16 22:24:44 +02001879 PyObject *error_type, *error_value, *error_traceback;
1880
1881 PyErr_Fetch(&error_type, &error_value, &error_traceback);
Guido van Rossum86610361998-04-10 22:32:46 +00001882
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001883 dict = PyThreadState_GetDict();
1884 if (dict == NULL)
Victor Stinner1b634932013-07-16 22:24:44 +02001885 goto finally;
1886
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 || !PyList_Check(list))
Victor Stinner1b634932013-07-16 22:24:44 +02001889 goto finally;
1890
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001891 i = PyList_GET_SIZE(list);
1892 /* Count backwards because we always expect obj to be list[-1] */
1893 while (--i >= 0) {
1894 if (PyList_GET_ITEM(list, i) == obj) {
1895 PyList_SetSlice(list, i, i + 1, NULL);
1896 break;
1897 }
1898 }
Victor Stinner1b634932013-07-16 22:24:44 +02001899
1900finally:
1901 /* ignore exceptions because there is no way to report them. */
1902 PyErr_Restore(error_type, error_value, error_traceback);
Guido van Rossum86610361998-04-10 22:32:46 +00001903}
Guido van Rossumd724b232000-03-13 16:01:29 +00001904
Tim Peters803526b2002-07-07 05:13:56 +00001905/* Trashcan support. */
Guido van Rossumd724b232000-03-13 16:01:29 +00001906
Tim Peters803526b2002-07-07 05:13:56 +00001907/* Current call-stack depth of tp_dealloc calls. */
Guido van Rossumd724b232000-03-13 16:01:29 +00001908int _PyTrash_delete_nesting = 0;
Guido van Rossume92e6102000-04-24 15:40:53 +00001909
Tim Peters803526b2002-07-07 05:13:56 +00001910/* List of objects that still need to be cleaned up, singly linked via their
1911 * gc headers' gc_prev pointers.
1912 */
1913PyObject *_PyTrash_delete_later = NULL;
Guido van Rossumd724b232000-03-13 16:01:29 +00001914
Tim Peters803526b2002-07-07 05:13:56 +00001915/* Add op to the _PyTrash_delete_later list. Called when the current
1916 * call-stack depth gets large. op must be a currently untracked gc'ed
1917 * object, with refcount 0. Py_DECREF must already have been called on it.
1918 */
Guido van Rossumd724b232000-03-13 16:01:29 +00001919void
Fred Drake100814d2000-07-09 15:48:49 +00001920_PyTrash_deposit_object(PyObject *op)
Guido van Rossumd724b232000-03-13 16:01:29 +00001921{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001922 assert(PyObject_IS_GC(op));
Antoine Pitrou796564c2013-07-30 19:59:21 +02001923 assert(_PyGC_REFS(op) == _PyGC_REFS_UNTRACKED);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001924 assert(op->ob_refcnt == 0);
1925 _Py_AS_GC(op)->gc.gc_prev = (PyGC_Head *)_PyTrash_delete_later;
1926 _PyTrash_delete_later = op;
Guido van Rossumd724b232000-03-13 16:01:29 +00001927}
1928
Antoine Pitrou2b0218a2012-09-06 00:59:49 +02001929/* The equivalent API, using per-thread state recursion info */
1930void
1931_PyTrash_thread_deposit_object(PyObject *op)
1932{
1933 PyThreadState *tstate = PyThreadState_GET();
1934 assert(PyObject_IS_GC(op));
Antoine Pitrou796564c2013-07-30 19:59:21 +02001935 assert(_PyGC_REFS(op) == _PyGC_REFS_UNTRACKED);
Antoine Pitrou2b0218a2012-09-06 00:59:49 +02001936 assert(op->ob_refcnt == 0);
1937 _Py_AS_GC(op)->gc.gc_prev = (PyGC_Head *) tstate->trash_delete_later;
1938 tstate->trash_delete_later = op;
1939}
1940
Tim Peters803526b2002-07-07 05:13:56 +00001941/* Dealloccate all the objects in the _PyTrash_delete_later list. Called when
1942 * the call-stack unwinds again.
1943 */
Guido van Rossumd724b232000-03-13 16:01:29 +00001944void
Fred Drake100814d2000-07-09 15:48:49 +00001945_PyTrash_destroy_chain(void)
Guido van Rossumd724b232000-03-13 16:01:29 +00001946{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001947 while (_PyTrash_delete_later) {
1948 PyObject *op = _PyTrash_delete_later;
1949 destructor dealloc = Py_TYPE(op)->tp_dealloc;
Neil Schemenauerf589c052002-03-29 03:05:54 +00001950
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001951 _PyTrash_delete_later =
1952 (PyObject*) _Py_AS_GC(op)->gc.gc_prev;
Neil Schemenauerf589c052002-03-29 03:05:54 +00001953
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001954 /* Call the deallocator directly. This used to try to
1955 * fool Py_DECREF into calling it indirectly, but
1956 * Py_DECREF was already called on this object, and in
1957 * assorted non-release builds calling Py_DECREF again ends
1958 * up distorting allocation statistics.
1959 */
1960 assert(op->ob_refcnt == 0);
1961 ++_PyTrash_delete_nesting;
1962 (*dealloc)(op);
1963 --_PyTrash_delete_nesting;
1964 }
Guido van Rossumd724b232000-03-13 16:01:29 +00001965}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001966
Antoine Pitrou2b0218a2012-09-06 00:59:49 +02001967/* The equivalent API, using per-thread state recursion info */
1968void
1969_PyTrash_thread_destroy_chain(void)
1970{
1971 PyThreadState *tstate = PyThreadState_GET();
1972 while (tstate->trash_delete_later) {
1973 PyObject *op = tstate->trash_delete_later;
1974 destructor dealloc = Py_TYPE(op)->tp_dealloc;
1975
1976 tstate->trash_delete_later =
1977 (PyObject*) _Py_AS_GC(op)->gc.gc_prev;
1978
1979 /* Call the deallocator directly. This used to try to
1980 * fool Py_DECREF into calling it indirectly, but
1981 * Py_DECREF was already called on this object, and in
1982 * assorted non-release builds calling Py_DECREF again ends
1983 * up distorting allocation statistics.
1984 */
1985 assert(op->ob_refcnt == 0);
1986 ++tstate->trash_delete_nesting;
1987 (*dealloc)(op);
1988 --tstate->trash_delete_nesting;
1989 }
1990}
1991
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00001992#ifndef Py_TRACE_REFS
1993/* For Py_LIMITED_API, we need an out-of-line version of _Py_Dealloc.
1994 Define this here, so we can undefine the macro. */
1995#undef _Py_Dealloc
1996PyAPI_FUNC(void) _Py_Dealloc(PyObject *);
1997void
1998_Py_Dealloc(PyObject *op)
1999{
2000 _Py_INC_TPFREES(op) _Py_COUNT_ALLOCS_COMMA
2001 (*Py_TYPE(op)->tp_dealloc)(op);
2002}
2003#endif
2004
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002005#ifdef __cplusplus
2006}
2007#endif