blob: 51188b7862b7ea62dfa4918d9e46304f56bb839f [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
Tim Peters34592512002-07-11 06:23:50 +000011#ifdef Py_REF_DEBUG
Neal Norwitz84632ee2006-03-04 20:00:59 +000012Py_ssize_t _Py_RefTotal;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000013
14Py_ssize_t
15_Py_GetRefTotal(void)
16{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000017 PyObject *o;
18 Py_ssize_t total = _Py_RefTotal;
19 /* ignore the references to the dummy object of the dicts and sets
20 because they are not reliable and not useful (now that the
21 hash table code is well-tested) */
22 o = _PyDict_Dummy();
23 if (o != NULL)
24 total -= o->ob_refcnt;
25 o = _PySet_Dummy();
26 if (o != NULL)
27 total -= o->ob_refcnt;
28 return total;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000029}
30#endif /* Py_REF_DEBUG */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000031
Guido van Rossum3f5da241990-12-20 15:06:42 +000032/* Object allocation routines used by NEWOBJ and NEWVAROBJ macros.
33 These are used by the individual routines for object creation.
34 Do not call them otherwise, they do not initialize the object! */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000035
Tim Peters78be7992003-03-23 02:51:01 +000036#ifdef Py_TRACE_REFS
Tim Peters7571a0f2003-03-23 17:52:28 +000037/* Head of circular doubly-linked list of all objects. These are linked
38 * together via the _ob_prev and _ob_next members of a PyObject, which
39 * exist only in a Py_TRACE_REFS build.
40 */
Tim Peters78be7992003-03-23 02:51:01 +000041static PyObject refchain = {&refchain, &refchain};
Tim Peters36eb4df2003-03-23 03:33:13 +000042
Tim Peters7571a0f2003-03-23 17:52:28 +000043/* Insert op at the front of the list of all objects. If force is true,
44 * op is added even if _ob_prev and _ob_next are non-NULL already. If
45 * force is false amd _ob_prev or _ob_next are non-NULL, do nothing.
46 * force should be true if and only if op points to freshly allocated,
47 * uninitialized memory, or you've unlinked op from the list and are
Tim Peters51f8d382003-03-23 18:06:08 +000048 * relinking it into the front.
Tim Peters7571a0f2003-03-23 17:52:28 +000049 * Note that objects are normally added to the list via _Py_NewReference,
50 * which is called by PyObject_Init. Not all objects are initialized that
51 * way, though; exceptions include statically allocated type objects, and
52 * statically allocated singletons (like Py_True and Py_None).
53 */
Tim Peters36eb4df2003-03-23 03:33:13 +000054void
Tim Peters7571a0f2003-03-23 17:52:28 +000055_Py_AddToAllObjects(PyObject *op, int force)
Tim Peters36eb4df2003-03-23 03:33:13 +000056{
Tim Peters7571a0f2003-03-23 17:52:28 +000057#ifdef Py_DEBUG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000058 if (!force) {
59 /* If it's initialized memory, op must be in or out of
60 * the list unambiguously.
61 */
62 assert((op->_ob_prev == NULL) == (op->_ob_next == NULL));
63 }
Tim Peters78be7992003-03-23 02:51:01 +000064#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000065 if (force || op->_ob_prev == NULL) {
66 op->_ob_next = refchain._ob_next;
67 op->_ob_prev = &refchain;
68 refchain._ob_next->_ob_prev = op;
69 refchain._ob_next = op;
70 }
Tim Peters7571a0f2003-03-23 17:52:28 +000071}
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000072#endif /* Py_TRACE_REFS */
Tim Peters78be7992003-03-23 02:51:01 +000073
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +000074#ifdef COUNT_ALLOCS
Guido van Rossumc0b618a1997-05-02 03:12:38 +000075static PyTypeObject *type_list;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000076/* All types are added to type_list, at least when
77 they get one object created. That makes them
78 immortal, which unfortunately contributes to
79 garbage itself. If unlist_types_without_objects
80 is set, they will be removed from the type_list
81 once the last object is deallocated. */
Benjamin Petersona4a37fe2009-01-11 17:13:55 +000082static int unlist_types_without_objects;
83extern Py_ssize_t tuple_zero_allocs, fast_tuple_allocs;
84extern Py_ssize_t quick_int_allocs, quick_neg_int_allocs;
85extern Py_ssize_t null_strings, one_strings;
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +000086void
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000087dump_counts(FILE* f)
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +000088{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000089 PyTypeObject *tp;
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +000090
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000091 for (tp = type_list; tp; tp = tp->tp_next)
92 fprintf(f, "%s alloc'd: %" PY_FORMAT_SIZE_T "d, "
93 "freed: %" PY_FORMAT_SIZE_T "d, "
94 "max in use: %" PY_FORMAT_SIZE_T "d\n",
95 tp->tp_name, tp->tp_allocs, tp->tp_frees,
96 tp->tp_maxalloc);
97 fprintf(f, "fast tuple allocs: %" PY_FORMAT_SIZE_T "d, "
98 "empty: %" PY_FORMAT_SIZE_T "d\n",
99 fast_tuple_allocs, tuple_zero_allocs);
100 fprintf(f, "fast int allocs: pos: %" PY_FORMAT_SIZE_T "d, "
101 "neg: %" PY_FORMAT_SIZE_T "d\n",
102 quick_int_allocs, quick_neg_int_allocs);
103 fprintf(f, "null strings: %" PY_FORMAT_SIZE_T "d, "
104 "1-strings: %" PY_FORMAT_SIZE_T "d\n",
105 null_strings, one_strings);
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +0000106}
107
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000108PyObject *
Fred Drake100814d2000-07-09 15:48:49 +0000109get_counts(void)
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000110{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000111 PyTypeObject *tp;
112 PyObject *result;
113 PyObject *v;
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000114
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000115 result = PyList_New(0);
116 if (result == NULL)
117 return NULL;
118 for (tp = type_list; tp; tp = tp->tp_next) {
119 v = Py_BuildValue("(snnn)", tp->tp_name, tp->tp_allocs,
120 tp->tp_frees, tp->tp_maxalloc);
121 if (v == NULL) {
122 Py_DECREF(result);
123 return NULL;
124 }
125 if (PyList_Append(result, v) < 0) {
126 Py_DECREF(v);
127 Py_DECREF(result);
128 return NULL;
129 }
130 Py_DECREF(v);
131 }
132 return result;
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000133}
134
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +0000135void
Fred Drake100814d2000-07-09 15:48:49 +0000136inc_count(PyTypeObject *tp)
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +0000137{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000138 if (tp->tp_next == NULL && tp->tp_prev == NULL) {
139 /* first time; insert in linked list */
140 if (tp->tp_next != NULL) /* sanity check */
141 Py_FatalError("XXX inc_count sanity check");
142 if (type_list)
143 type_list->tp_prev = tp;
144 tp->tp_next = type_list;
145 /* Note that as of Python 2.2, heap-allocated type objects
146 * can go away, but this code requires that they stay alive
147 * until program exit. That's why we're careful with
148 * refcounts here. type_list gets a new reference to tp,
149 * while ownership of the reference type_list used to hold
150 * (if any) was transferred to tp->tp_next in the line above.
151 * tp is thus effectively immortal after this.
152 */
153 Py_INCREF(tp);
154 type_list = tp;
Tim Peters3e40c7f2003-03-23 03:04:32 +0000155#ifdef Py_TRACE_REFS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000156 /* Also insert in the doubly-linked list of all objects,
157 * if not already there.
158 */
159 _Py_AddToAllObjects((PyObject *)tp, 0);
Tim Peters78be7992003-03-23 02:51:01 +0000160#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000161 }
162 tp->tp_allocs++;
163 if (tp->tp_allocs - tp->tp_frees > tp->tp_maxalloc)
164 tp->tp_maxalloc = tp->tp_allocs - tp->tp_frees;
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +0000165}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000166
167void dec_count(PyTypeObject *tp)
168{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000169 tp->tp_frees++;
170 if (unlist_types_without_objects &&
171 tp->tp_allocs == tp->tp_frees) {
172 /* unlink the type from type_list */
173 if (tp->tp_prev)
174 tp->tp_prev->tp_next = tp->tp_next;
175 else
176 type_list = tp->tp_next;
177 if (tp->tp_next)
178 tp->tp_next->tp_prev = tp->tp_prev;
179 tp->tp_next = tp->tp_prev = NULL;
180 Py_DECREF(tp);
181 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000182}
183
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +0000184#endif
185
Tim Peters7c321a82002-07-09 02:57:01 +0000186#ifdef Py_REF_DEBUG
187/* Log a fatal error; doesn't return. */
188void
189_Py_NegativeRefcount(const char *fname, int lineno, PyObject *op)
190{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000191 char buf[300];
Tim Peters7c321a82002-07-09 02:57:01 +0000192
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000193 PyOS_snprintf(buf, sizeof(buf),
194 "%s:%i object at %p has negative ref count "
195 "%" PY_FORMAT_SIZE_T "d",
196 fname, lineno, op, op->ob_refcnt);
197 Py_FatalError(buf);
Tim Peters7c321a82002-07-09 02:57:01 +0000198}
199
200#endif /* Py_REF_DEBUG */
201
Thomas Heller1328b522004-04-22 17:23:49 +0000202void
203Py_IncRef(PyObject *o)
204{
205 Py_XINCREF(o);
206}
207
208void
209Py_DecRef(PyObject *o)
210{
211 Py_XDECREF(o);
212}
213
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000214PyObject *
Fred Drake100814d2000-07-09 15:48:49 +0000215PyObject_Init(PyObject *op, PyTypeObject *tp)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000216{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000217 if (op == NULL)
218 return PyErr_NoMemory();
219 /* Any changes should be reflected in PyObject_INIT (objimpl.h) */
220 Py_TYPE(op) = tp;
221 _Py_NewReference(op);
222 return op;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000223}
224
Guido van Rossumb18618d2000-05-03 23:44:39 +0000225PyVarObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000226PyObject_InitVar(PyVarObject *op, PyTypeObject *tp, Py_ssize_t size)
Guido van Rossumb18618d2000-05-03 23:44:39 +0000227{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000228 if (op == NULL)
229 return (PyVarObject *) PyErr_NoMemory();
230 /* Any changes should be reflected in PyObject_INIT_VAR */
231 op->ob_size = size;
232 Py_TYPE(op) = tp;
233 _Py_NewReference((PyObject *)op);
234 return op;
Guido van Rossumb18618d2000-05-03 23:44:39 +0000235}
236
237PyObject *
Fred Drake100814d2000-07-09 15:48:49 +0000238_PyObject_New(PyTypeObject *tp)
Guido van Rossumb18618d2000-05-03 23:44:39 +0000239{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000240 PyObject *op;
241 op = (PyObject *) PyObject_MALLOC(_PyObject_SIZE(tp));
242 if (op == NULL)
243 return PyErr_NoMemory();
244 return PyObject_INIT(op, tp);
Guido van Rossumb18618d2000-05-03 23:44:39 +0000245}
246
Guido van Rossumd0c87ee1997-05-15 21:31:03 +0000247PyVarObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000248_PyObject_NewVar(PyTypeObject *tp, Py_ssize_t nitems)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000249{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000250 PyVarObject *op;
251 const size_t size = _PyObject_VAR_SIZE(tp, nitems);
252 op = (PyVarObject *) PyObject_MALLOC(size);
253 if (op == NULL)
254 return (PyVarObject *)PyErr_NoMemory();
255 return PyObject_INIT_VAR(op, tp, nitems);
Guido van Rossumb18618d2000-05-03 23:44:39 +0000256}
257
Antoine Pitrouc47bd4a2010-07-27 22:08:27 +0000258int
259PyObject_Print(PyObject *op, FILE *fp, int flags)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000260{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000261 int ret = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000262 if (PyErr_CheckSignals())
263 return -1;
Guido van Rossum9b00dfa1998-04-28 16:06:54 +0000264#ifdef USE_STACKCHECK
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000265 if (PyOS_CheckStack()) {
266 PyErr_SetString(PyExc_MemoryError, "stack overflow");
267 return -1;
268 }
Guido van Rossum9b00dfa1998-04-28 16:06:54 +0000269#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000270 clearerr(fp); /* Clear any previous error condition */
271 if (op == NULL) {
272 Py_BEGIN_ALLOW_THREADS
273 fprintf(fp, "<nil>");
274 Py_END_ALLOW_THREADS
275 }
276 else {
277 if (op->ob_refcnt <= 0)
278 /* XXX(twouters) cast refcount to long until %zd is
279 universally available */
280 Py_BEGIN_ALLOW_THREADS
281 fprintf(fp, "<refcnt %ld at %p>",
282 (long)op->ob_refcnt, op);
283 Py_END_ALLOW_THREADS
284 else {
285 PyObject *s;
286 if (flags & Py_PRINT_RAW)
287 s = PyObject_Str(op);
288 else
289 s = PyObject_Repr(op);
290 if (s == NULL)
291 ret = -1;
292 else if (PyBytes_Check(s)) {
293 fwrite(PyBytes_AS_STRING(s), 1,
294 PyBytes_GET_SIZE(s), fp);
295 }
296 else if (PyUnicode_Check(s)) {
297 PyObject *t;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200298 t = PyUnicode_AsEncodedString(s, "utf-8", "backslashreplace");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000299 if (t == NULL)
300 ret = 0;
301 else {
302 fwrite(PyBytes_AS_STRING(t), 1,
303 PyBytes_GET_SIZE(t), fp);
Victor Stinnerba6b4302010-05-17 09:33:42 +0000304 Py_DECREF(t);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000305 }
306 }
307 else {
308 PyErr_Format(PyExc_TypeError,
309 "str() or repr() returned '%.100s'",
310 s->ob_type->tp_name);
311 ret = -1;
312 }
313 Py_XDECREF(s);
314 }
315 }
316 if (ret == 0) {
317 if (ferror(fp)) {
318 PyErr_SetFromErrno(PyExc_IOError);
319 clearerr(fp);
320 ret = -1;
321 }
322 }
323 return ret;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000324}
325
Guido van Rossum38938152006-08-21 23:36:26 +0000326/* For debugging convenience. Set a breakpoint here and call it from your DLL */
327void
Thomas Woutersb2137042007-02-01 18:02:27 +0000328_Py_BreakPoint(void)
Guido van Rossum38938152006-08-21 23:36:26 +0000329{
330}
331
Neal Norwitz1a997502003-01-13 20:13:12 +0000332
Barry Warsaw9bf16442001-01-23 16:24:35 +0000333/* For debugging convenience. See Misc/gdbinit for some useful gdb hooks */
Guido van Rossum38938152006-08-21 23:36:26 +0000334void
335_PyObject_Dump(PyObject* op)
Barry Warsaw9bf16442001-01-23 16:24:35 +0000336{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000337 if (op == NULL)
338 fprintf(stderr, "NULL\n");
339 else {
Georg Brandldfd73442009-04-05 11:47:34 +0000340#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000341 PyGILState_STATE gil;
Georg Brandldfd73442009-04-05 11:47:34 +0000342#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000343 fprintf(stderr, "object : ");
Georg Brandldfd73442009-04-05 11:47:34 +0000344#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000345 gil = PyGILState_Ensure();
Georg Brandldfd73442009-04-05 11:47:34 +0000346#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000347 (void)PyObject_Print(op, stderr, 0);
Georg Brandldfd73442009-04-05 11:47:34 +0000348#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000349 PyGILState_Release(gil);
Georg Brandldfd73442009-04-05 11:47:34 +0000350#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000351 /* XXX(twouters) cast refcount to long until %zd is
352 universally available */
353 fprintf(stderr, "\n"
354 "type : %s\n"
355 "refcount: %ld\n"
356 "address : %p\n",
357 Py_TYPE(op)==NULL ? "NULL" : Py_TYPE(op)->tp_name,
358 (long)op->ob_refcnt,
359 op);
360 }
Barry Warsaw9bf16442001-01-23 16:24:35 +0000361}
Barry Warsaw903138f2001-01-23 16:33:18 +0000362
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000363PyObject *
Fred Drake100814d2000-07-09 15:48:49 +0000364PyObject_Repr(PyObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000365{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000366 PyObject *res;
367 if (PyErr_CheckSignals())
368 return NULL;
Guido van Rossum9b00dfa1998-04-28 16:06:54 +0000369#ifdef USE_STACKCHECK
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000370 if (PyOS_CheckStack()) {
371 PyErr_SetString(PyExc_MemoryError, "stack overflow");
372 return NULL;
373 }
Guido van Rossum9b00dfa1998-04-28 16:06:54 +0000374#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000375 if (v == NULL)
376 return PyUnicode_FromString("<NULL>");
377 if (Py_TYPE(v)->tp_repr == NULL)
378 return PyUnicode_FromFormat("<%s object at %p>",
379 v->ob_type->tp_name, v);
380 res = (*v->ob_type->tp_repr)(v);
381 if (res != NULL && !PyUnicode_Check(res)) {
382 PyErr_Format(PyExc_TypeError,
383 "__repr__ returned non-string (type %.200s)",
384 res->ob_type->tp_name);
385 Py_DECREF(res);
386 return NULL;
387 }
388 return res;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000389}
390
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000391PyObject *
Guido van Rossum98297ee2007-11-06 21:34:58 +0000392PyObject_Str(PyObject *v)
Guido van Rossumc6004111993-11-05 10:22:19 +0000393{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000394 PyObject *res;
395 if (PyErr_CheckSignals())
396 return NULL;
Guido van Rossum98297ee2007-11-06 21:34:58 +0000397#ifdef USE_STACKCHECK
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000398 if (PyOS_CheckStack()) {
399 PyErr_SetString(PyExc_MemoryError, "stack overflow");
400 return NULL;
401 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000402#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000403 if (v == NULL)
404 return PyUnicode_FromString("<NULL>");
405 if (PyUnicode_CheckExact(v)) {
406 Py_INCREF(v);
407 return v;
408 }
409 if (Py_TYPE(v)->tp_str == NULL)
410 return PyObject_Repr(v);
Guido van Rossum4f288ab2001-05-01 16:53:37 +0000411
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000412 /* It is possible for a type to have a tp_str representation that loops
413 infinitely. */
414 if (Py_EnterRecursiveCall(" while getting the str of an object"))
415 return NULL;
416 res = (*Py_TYPE(v)->tp_str)(v);
417 Py_LeaveRecursiveCall();
418 if (res == NULL)
419 return NULL;
420 if (!PyUnicode_Check(res)) {
421 PyErr_Format(PyExc_TypeError,
422 "__str__ returned non-string (type %.200s)",
423 Py_TYPE(res)->tp_name);
424 Py_DECREF(res);
425 return NULL;
426 }
427 return res;
Neil Schemenauercf52c072005-08-12 17:34:58 +0000428}
429
Georg Brandl559e5d72008-06-11 18:37:52 +0000430PyObject *
431PyObject_ASCII(PyObject *v)
432{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000433 PyObject *repr, *ascii, *res;
Georg Brandl559e5d72008-06-11 18:37:52 +0000434
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000435 repr = PyObject_Repr(v);
436 if (repr == NULL)
437 return NULL;
Georg Brandl559e5d72008-06-11 18:37:52 +0000438
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000439 /* repr is guaranteed to be a PyUnicode object by PyObject_Repr */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200440 ascii = _PyUnicode_AsASCIIString(repr, "backslashreplace");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000441 Py_DECREF(repr);
442 if (ascii == NULL)
443 return NULL;
Georg Brandl559e5d72008-06-11 18:37:52 +0000444
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000445 res = PyUnicode_DecodeASCII(
446 PyBytes_AS_STRING(ascii),
447 PyBytes_GET_SIZE(ascii),
448 NULL);
449
450 Py_DECREF(ascii);
451 return res;
Georg Brandl559e5d72008-06-11 18:37:52 +0000452}
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000453
Benjamin Petersonc15a0732008-08-26 16:46:47 +0000454PyObject *
455PyObject_Bytes(PyObject *v)
456{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000457 PyObject *result, *func;
458 static PyObject *bytesstring = NULL;
Benjamin Petersonc15a0732008-08-26 16:46:47 +0000459
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000460 if (v == NULL)
461 return PyBytes_FromString("<NULL>");
Benjamin Petersonc15a0732008-08-26 16:46:47 +0000462
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000463 if (PyBytes_CheckExact(v)) {
464 Py_INCREF(v);
465 return v;
466 }
Benjamin Petersonc15a0732008-08-26 16:46:47 +0000467
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000468 func = _PyObject_LookupSpecial(v, "__bytes__", &bytesstring);
469 if (func != NULL) {
470 result = PyObject_CallFunctionObjArgs(func, NULL);
471 Py_DECREF(func);
472 if (result == NULL)
Benjamin Peterson41ece392010-09-11 16:39:57 +0000473 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000474 if (!PyBytes_Check(result)) {
Benjamin Peterson41ece392010-09-11 16:39:57 +0000475 PyErr_Format(PyExc_TypeError,
476 "__bytes__ returned non-bytes (type %.200s)",
477 Py_TYPE(result)->tp_name);
478 Py_DECREF(result);
479 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000480 }
481 return result;
482 }
483 else if (PyErr_Occurred())
484 return NULL;
485 return PyBytes_FromObject(v);
Benjamin Petersonc15a0732008-08-26 16:46:47 +0000486}
487
Mark Dickinsonc008a172009-02-01 13:59:22 +0000488/* For Python 3.0.1 and later, the old three-way comparison has been
489 completely removed in favour of rich comparisons. PyObject_Compare() and
490 PyObject_Cmp() are gone, and the builtin cmp function no longer exists.
Mark Dickinsone94c6792009-02-02 20:36:42 +0000491 The old tp_compare slot has been renamed to tp_reserved, and should no
Mark Dickinsonc008a172009-02-01 13:59:22 +0000492 longer be used. Use tp_richcompare instead.
Guido van Rossum98297ee2007-11-06 21:34:58 +0000493
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000494 See (*) below for practical amendments.
495
Mark Dickinsonc008a172009-02-01 13:59:22 +0000496 tp_richcompare gets called with a first argument of the appropriate type
497 and a second object of an arbitrary type. We never do any kind of
498 coercion.
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000499
Mark Dickinsonc008a172009-02-01 13:59:22 +0000500 The tp_richcompare slot should return an object, as follows:
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000501
502 NULL if an exception occurred
503 NotImplemented if the requested comparison is not implemented
504 any other false value if the requested comparison is false
505 any other true value if the requested comparison is true
506
507 The PyObject_RichCompare[Bool]() wrappers raise TypeError when they get
508 NotImplemented.
509
510 (*) Practical amendments:
511
512 - If rich comparison returns NotImplemented, == and != are decided by
513 comparing the object pointer (i.e. falling back to the base object
514 implementation).
515
Guido van Rossuma4073002002-05-31 20:03:54 +0000516*/
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000517
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000518/* Map rich comparison operators to their swapped version, e.g. LT <--> GT */
Brett Cannona5ca2e72004-09-25 01:37:24 +0000519int _Py_SwappedOp[] = {Py_GT, Py_GE, Py_EQ, Py_NE, Py_LT, Py_LE};
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +0000520
Guido van Rossum9a4e95c2006-12-19 21:35:46 +0000521static char *opstrings[] = {"<", "<=", "==", "!=", ">", ">="};
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000522
523/* Perform a rich comparison, raising TypeError when the requested comparison
524 operator is not supported. */
Guido van Rossume797ec12001-01-17 15:24:28 +0000525static PyObject *
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000526do_richcompare(PyObject *v, PyObject *w, int op)
Guido van Rossume797ec12001-01-17 15:24:28 +0000527{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000528 richcmpfunc f;
529 PyObject *res;
530 int checked_reverse_op = 0;
Guido van Rossume797ec12001-01-17 15:24:28 +0000531
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000532 if (v->ob_type != w->ob_type &&
533 PyType_IsSubtype(w->ob_type, v->ob_type) &&
534 (f = w->ob_type->tp_richcompare) != NULL) {
535 checked_reverse_op = 1;
536 res = (*f)(w, v, _Py_SwappedOp[op]);
537 if (res != Py_NotImplemented)
538 return res;
539 Py_DECREF(res);
540 }
541 if ((f = v->ob_type->tp_richcompare) != NULL) {
542 res = (*f)(v, w, op);
543 if (res != Py_NotImplemented)
544 return res;
545 Py_DECREF(res);
546 }
547 if (!checked_reverse_op && (f = w->ob_type->tp_richcompare) != NULL) {
548 res = (*f)(w, v, _Py_SwappedOp[op]);
549 if (res != Py_NotImplemented)
550 return res;
551 Py_DECREF(res);
552 }
553 /* If neither object implements it, provide a sensible default
554 for == and !=, but raise an exception for ordering. */
555 switch (op) {
556 case Py_EQ:
557 res = (v == w) ? Py_True : Py_False;
558 break;
559 case Py_NE:
560 res = (v != w) ? Py_True : Py_False;
561 break;
562 default:
563 /* XXX Special-case None so it doesn't show as NoneType() */
564 PyErr_Format(PyExc_TypeError,
565 "unorderable types: %.100s() %s %.100s()",
566 v->ob_type->tp_name,
567 opstrings[op],
568 w->ob_type->tp_name);
569 return NULL;
570 }
571 Py_INCREF(res);
572 return res;
Guido van Rossume797ec12001-01-17 15:24:28 +0000573}
574
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000575/* Perform a rich comparison with object result. This wraps do_richcompare()
576 with a check for NULL arguments and a recursion check. */
Guido van Rossume797ec12001-01-17 15:24:28 +0000577
Guido van Rossume797ec12001-01-17 15:24:28 +0000578PyObject *
579PyObject_RichCompare(PyObject *v, PyObject *w, int op)
580{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000581 PyObject *res;
Guido van Rossume797ec12001-01-17 15:24:28 +0000582
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000583 assert(Py_LT <= op && op <= Py_GE);
584 if (v == NULL || w == NULL) {
585 if (!PyErr_Occurred())
586 PyErr_BadInternalCall();
587 return NULL;
588 }
589 if (Py_EnterRecursiveCall(" in comparison"))
590 return NULL;
591 res = do_richcompare(v, w, op);
592 Py_LeaveRecursiveCall();
593 return res;
Guido van Rossume797ec12001-01-17 15:24:28 +0000594}
595
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000596/* Perform a rich comparison with integer result. This wraps
597 PyObject_RichCompare(), returning -1 for error, 0 for false, 1 for true. */
Guido van Rossume797ec12001-01-17 15:24:28 +0000598int
599PyObject_RichCompareBool(PyObject *v, PyObject *w, int op)
600{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000601 PyObject *res;
602 int ok;
Guido van Rossume797ec12001-01-17 15:24:28 +0000603
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000604 /* Quick result when objects are the same.
605 Guarantees that identity implies equality. */
606 if (v == w) {
607 if (op == Py_EQ)
608 return 1;
609 else if (op == Py_NE)
610 return 0;
611 }
Mark Dickinson4a1f5932008-11-12 23:23:36 +0000612
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000613 res = PyObject_RichCompare(v, w, op);
614 if (res == NULL)
615 return -1;
616 if (PyBool_Check(res))
617 ok = (res == Py_True);
618 else
619 ok = PyObject_IsTrue(res);
620 Py_DECREF(res);
621 return ok;
Guido van Rossume797ec12001-01-17 15:24:28 +0000622}
Fred Drake13634cf2000-06-29 19:17:04 +0000623
624/* Set of hash utility functions to help maintaining the invariant that
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000625 if a==b then hash(a)==hash(b)
Fred Drake13634cf2000-06-29 19:17:04 +0000626
627 All the utility functions (_Py_Hash*()) return "-1" to signify an error.
628*/
629
Mark Dickinsondc787d22010-05-23 13:33:13 +0000630/* For numeric types, the hash of a number x is based on the reduction
631 of x modulo the prime P = 2**_PyHASH_BITS - 1. It's designed so that
632 hash(x) == hash(y) whenever x and y are numerically equal, even if
633 x and y have different types.
634
635 A quick summary of the hashing strategy:
636
637 (1) First define the 'reduction of x modulo P' for any rational
638 number x; this is a standard extension of the usual notion of
639 reduction modulo P for integers. If x == p/q (written in lowest
640 terms), the reduction is interpreted as the reduction of p times
641 the inverse of the reduction of q, all modulo P; if q is exactly
642 divisible by P then define the reduction to be infinity. So we've
643 got a well-defined map
644
645 reduce : { rational numbers } -> { 0, 1, 2, ..., P-1, infinity }.
646
647 (2) Now for a rational number x, define hash(x) by:
648
649 reduce(x) if x >= 0
650 -reduce(-x) if x < 0
651
652 If the result of the reduction is infinity (this is impossible for
653 integers, floats and Decimals) then use the predefined hash value
654 _PyHASH_INF for x >= 0, or -_PyHASH_INF for x < 0, instead.
655 _PyHASH_INF, -_PyHASH_INF and _PyHASH_NAN are also used for the
656 hashes of float and Decimal infinities and nans.
657
658 A selling point for the above strategy is that it makes it possible
659 to compute hashes of decimal and binary floating-point numbers
660 efficiently, even if the exponent of the binary or decimal number
661 is large. The key point is that
662
663 reduce(x * y) == reduce(x) * reduce(y) (modulo _PyHASH_MODULUS)
664
665 provided that {reduce(x), reduce(y)} != {0, infinity}. The reduction of a
666 binary or decimal float is never infinity, since the denominator is a power
667 of 2 (for binary) or a divisor of a power of 10 (for decimal). So we have,
668 for nonnegative x,
669
670 reduce(x * 2**e) == reduce(x) * reduce(2**e) % _PyHASH_MODULUS
671
672 reduce(x * 10**e) == reduce(x) * reduce(10**e) % _PyHASH_MODULUS
673
674 and reduce(10**e) can be computed efficiently by the usual modular
675 exponentiation algorithm. For reduce(2**e) it's even better: since
676 P is of the form 2**n-1, reduce(2**e) is 2**(e mod n), and multiplication
677 by 2**(e mod n) modulo 2**n-1 just amounts to a rotation of bits.
678
679 */
680
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000681Py_hash_t
Fred Drake100814d2000-07-09 15:48:49 +0000682_Py_HashDouble(double v)
Fred Drake13634cf2000-06-29 19:17:04 +0000683{
Mark Dickinsondc787d22010-05-23 13:33:13 +0000684 int e, sign;
685 double m;
Benjamin Peterson8035bc52010-10-23 16:20:50 +0000686 Py_uhash_t x, y;
Tim Peters39dce292000-08-15 03:34:48 +0000687
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000688 if (!Py_IS_FINITE(v)) {
689 if (Py_IS_INFINITY(v))
Mark Dickinsondc787d22010-05-23 13:33:13 +0000690 return v > 0 ? _PyHASH_INF : -_PyHASH_INF;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000691 else
Mark Dickinsondc787d22010-05-23 13:33:13 +0000692 return _PyHASH_NAN;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000693 }
Mark Dickinsondc787d22010-05-23 13:33:13 +0000694
695 m = frexp(v, &e);
696
697 sign = 1;
698 if (m < 0) {
699 sign = -1;
700 m = -m;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000701 }
Mark Dickinsondc787d22010-05-23 13:33:13 +0000702
703 /* process 28 bits at a time; this should work well both for binary
704 and hexadecimal floating point. */
705 x = 0;
706 while (m) {
707 x = ((x << 28) & _PyHASH_MODULUS) | x >> (_PyHASH_BITS - 28);
708 m *= 268435456.0; /* 2**28 */
709 e -= 28;
Benjamin Peterson8035bc52010-10-23 16:20:50 +0000710 y = (Py_uhash_t)m; /* pull out integer part */
Mark Dickinsondc787d22010-05-23 13:33:13 +0000711 m -= y;
712 x += y;
713 if (x >= _PyHASH_MODULUS)
714 x -= _PyHASH_MODULUS;
715 }
716
717 /* adjust for the exponent; first reduce it modulo _PyHASH_BITS */
718 e = e >= 0 ? e % _PyHASH_BITS : _PyHASH_BITS-1-((-1-e) % _PyHASH_BITS);
719 x = ((x << e) & _PyHASH_MODULUS) | x >> (_PyHASH_BITS - e);
720
721 x = x * sign;
Benjamin Peterson8035bc52010-10-23 16:20:50 +0000722 if (x == (Py_uhash_t)-1)
723 x = (Py_uhash_t)-2;
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000724 return (Py_hash_t)x;
Fred Drake13634cf2000-06-29 19:17:04 +0000725}
726
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000727Py_hash_t
Fred Drake100814d2000-07-09 15:48:49 +0000728_Py_HashPointer(void *p)
Fred Drake13634cf2000-06-29 19:17:04 +0000729{
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000730 Py_hash_t x;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000731 size_t y = (size_t)p;
732 /* bottom 3 or 4 bits are likely to be 0; rotate y by 4 to avoid
733 excessive hash collisions for dicts and sets */
734 y = (y >> 4) | (y << (8 * SIZEOF_VOID_P - 4));
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000735 x = (Py_hash_t)y;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000736 if (x == -1)
737 x = -2;
738 return x;
Fred Drake13634cf2000-06-29 19:17:04 +0000739}
740
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000741Py_hash_t
Nick Coghland1abd252008-07-15 15:46:38 +0000742PyObject_HashNotImplemented(PyObject *v)
743{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000744 PyErr_Format(PyExc_TypeError, "unhashable type: '%.200s'",
745 Py_TYPE(v)->tp_name);
746 return -1;
Nick Coghland1abd252008-07-15 15:46:38 +0000747}
Fred Drake13634cf2000-06-29 19:17:04 +0000748
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000749Py_hash_t
Fred Drake100814d2000-07-09 15:48:49 +0000750PyObject_Hash(PyObject *v)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000751{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000752 PyTypeObject *tp = Py_TYPE(v);
753 if (tp->tp_hash != NULL)
754 return (*tp->tp_hash)(v);
755 /* To keep to the general practice that inheriting
756 * solely from object in C code should work without
757 * an explicit call to PyType_Ready, we implicitly call
758 * PyType_Ready here and then check the tp_hash slot again
759 */
760 if (tp->tp_dict == NULL) {
761 if (PyType_Ready(tp) < 0)
762 return -1;
763 if (tp->tp_hash != NULL)
764 return (*tp->tp_hash)(v);
765 }
766 /* Otherwise, the object can't be hashed */
767 return PyObject_HashNotImplemented(v);
Guido van Rossum9bfef441993-03-29 10:43:31 +0000768}
769
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000770PyObject *
Jeremy Hyltonaf68c872005-12-10 18:50:16 +0000771PyObject_GetAttrString(PyObject *v, const char *name)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000772{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000773 PyObject *w, *res;
Guido van Rossumd8eb1b31996-08-09 20:52:03 +0000774
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000775 if (Py_TYPE(v)->tp_getattr != NULL)
776 return (*Py_TYPE(v)->tp_getattr)(v, (char*)name);
777 w = PyUnicode_InternFromString(name);
778 if (w == NULL)
779 return NULL;
780 res = PyObject_GetAttr(v, w);
781 Py_XDECREF(w);
782 return res;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000783}
784
785int
Jeremy Hyltonaf68c872005-12-10 18:50:16 +0000786PyObject_HasAttrString(PyObject *v, const char *name)
Guido van Rossumed18fdc1993-07-11 19:55:34 +0000787{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000788 PyObject *res = PyObject_GetAttrString(v, name);
789 if (res != NULL) {
790 Py_DECREF(res);
791 return 1;
792 }
793 PyErr_Clear();
794 return 0;
Guido van Rossumed18fdc1993-07-11 19:55:34 +0000795}
796
797int
Jeremy Hyltonaf68c872005-12-10 18:50:16 +0000798PyObject_SetAttrString(PyObject *v, const char *name, PyObject *w)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000799{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000800 PyObject *s;
801 int res;
Guido van Rossumd8eb1b31996-08-09 20:52:03 +0000802
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000803 if (Py_TYPE(v)->tp_setattr != NULL)
804 return (*Py_TYPE(v)->tp_setattr)(v, (char*)name, w);
805 s = PyUnicode_InternFromString(name);
806 if (s == NULL)
807 return -1;
808 res = PyObject_SetAttr(v, s, w);
809 Py_XDECREF(s);
810 return res;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000811}
812
Guido van Rossum98ff96a1997-05-20 18:34:44 +0000813PyObject *
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200814_PyObject_GetAttrId(PyObject *v, _Py_Identifier *name)
815{
816 PyObject *result;
Martin v. Löwisd10759f2011-11-07 13:00:05 +0100817 PyObject *oname = _PyUnicode_FromId(name); /* borrowed */
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200818 if (!oname)
819 return NULL;
820 result = PyObject_GetAttr(v, oname);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200821 return result;
822}
823
824int
825_PyObject_HasAttrId(PyObject *v, _Py_Identifier *name)
826{
827 int result;
Martin v. Löwisd10759f2011-11-07 13:00:05 +0100828 PyObject *oname = _PyUnicode_FromId(name); /* borrowed */
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200829 if (!oname)
830 return -1;
831 result = PyObject_HasAttr(v, oname);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200832 return result;
833}
834
835int
836_PyObject_SetAttrId(PyObject *v, _Py_Identifier *name, PyObject *w)
837{
838 int result;
Martin v. Löwisd10759f2011-11-07 13:00:05 +0100839 PyObject *oname = _PyUnicode_FromId(name); /* borrowed */
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200840 if (!oname)
841 return -1;
842 result = PyObject_SetAttr(v, oname, w);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200843 return result;
844}
845
846PyObject *
Fred Drake100814d2000-07-09 15:48:49 +0000847PyObject_GetAttr(PyObject *v, PyObject *name)
Guido van Rossum98ff96a1997-05-20 18:34:44 +0000848{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000849 PyTypeObject *tp = Py_TYPE(v);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000850
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000851 if (!PyUnicode_Check(name)) {
852 PyErr_Format(PyExc_TypeError,
853 "attribute name must be string, not '%.200s'",
854 name->ob_type->tp_name);
855 return NULL;
856 }
857 if (tp->tp_getattro != NULL)
858 return (*tp->tp_getattro)(v, name);
859 if (tp->tp_getattr != NULL) {
860 char *name_str = _PyUnicode_AsString(name);
861 if (name_str == NULL)
862 return NULL;
863 return (*tp->tp_getattr)(v, name_str);
864 }
865 PyErr_Format(PyExc_AttributeError,
866 "'%.50s' object has no attribute '%U'",
867 tp->tp_name, name);
868 return NULL;
Guido van Rossum98ff96a1997-05-20 18:34:44 +0000869}
870
871int
Fred Drake100814d2000-07-09 15:48:49 +0000872PyObject_HasAttr(PyObject *v, PyObject *name)
Guido van Rossum98ff96a1997-05-20 18:34:44 +0000873{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000874 PyObject *res = PyObject_GetAttr(v, name);
875 if (res != NULL) {
876 Py_DECREF(res);
877 return 1;
878 }
879 PyErr_Clear();
880 return 0;
Guido van Rossum98ff96a1997-05-20 18:34:44 +0000881}
882
883int
Fred Drake100814d2000-07-09 15:48:49 +0000884PyObject_SetAttr(PyObject *v, PyObject *name, PyObject *value)
Guido van Rossum98ff96a1997-05-20 18:34:44 +0000885{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000886 PyTypeObject *tp = Py_TYPE(v);
887 int err;
Marc-André Lemburge44e5072000-09-18 16:20:57 +0000888
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000889 if (!PyUnicode_Check(name)) {
890 PyErr_Format(PyExc_TypeError,
891 "attribute name must be string, not '%.200s'",
892 name->ob_type->tp_name);
893 return -1;
894 }
895 Py_INCREF(name);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000896
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000897 PyUnicode_InternInPlace(&name);
898 if (tp->tp_setattro != NULL) {
899 err = (*tp->tp_setattro)(v, name, value);
900 Py_DECREF(name);
901 return err;
902 }
903 if (tp->tp_setattr != NULL) {
904 char *name_str = _PyUnicode_AsString(name);
905 if (name_str == NULL)
906 return -1;
907 err = (*tp->tp_setattr)(v, name_str, value);
908 Py_DECREF(name);
909 return err;
910 }
911 Py_DECREF(name);
912 assert(name->ob_refcnt >= 1);
913 if (tp->tp_getattr == NULL && tp->tp_getattro == NULL)
914 PyErr_Format(PyExc_TypeError,
915 "'%.100s' object has no attributes "
916 "(%s .%U)",
917 tp->tp_name,
918 value==NULL ? "del" : "assign to",
919 name);
920 else
921 PyErr_Format(PyExc_TypeError,
922 "'%.100s' object has only read-only attributes "
923 "(%s .%U)",
924 tp->tp_name,
925 value==NULL ? "del" : "assign to",
926 name);
927 return -1;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000928}
929
930/* Helper to get a pointer to an object's __dict__ slot, if any */
931
932PyObject **
933_PyObject_GetDictPtr(PyObject *obj)
934{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000935 Py_ssize_t dictoffset;
936 PyTypeObject *tp = Py_TYPE(obj);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000937
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000938 dictoffset = tp->tp_dictoffset;
939 if (dictoffset == 0)
940 return NULL;
941 if (dictoffset < 0) {
942 Py_ssize_t tsize;
943 size_t size;
Guido van Rossum2eb0b872002-03-01 22:24:49 +0000944
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000945 tsize = ((PyVarObject *)obj)->ob_size;
946 if (tsize < 0)
947 tsize = -tsize;
948 size = _PyObject_VAR_SIZE(tp, tsize);
Guido van Rossum2eb0b872002-03-01 22:24:49 +0000949
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000950 dictoffset += (long)size;
951 assert(dictoffset > 0);
952 assert(dictoffset % SIZEOF_VOID_P == 0);
953 }
954 return (PyObject **) ((char *)obj + dictoffset);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000955}
956
Tim Peters6d6c1a32001-08-02 04:15:00 +0000957PyObject *
Raymond Hettinger1da1dbf2003-03-17 19:46:11 +0000958PyObject_SelfIter(PyObject *obj)
Raymond Hettinger01538262003-03-17 08:24:35 +0000959{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000960 Py_INCREF(obj);
961 return obj;
Raymond Hettinger01538262003-03-17 08:24:35 +0000962}
963
Amaury Forgeot d'Arcf343e012009-01-12 23:58:21 +0000964/* Helper used when the __next__ method is removed from a type:
965 tp_iternext is never NULL and can be safely called without checking
966 on every iteration.
967 */
968
969PyObject *
970_PyObject_NextNotImplemented(PyObject *self)
971{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000972 PyErr_Format(PyExc_TypeError,
973 "'%.200s' object is not iterable",
974 Py_TYPE(self)->tp_name);
975 return NULL;
Amaury Forgeot d'Arcf343e012009-01-12 23:58:21 +0000976}
977
Michael W. Hudson1593f502004-09-14 17:09:47 +0000978/* Generic GetAttr functions - put these in your tp_[gs]etattro slot */
979
Raymond Hettinger01538262003-03-17 08:24:35 +0000980PyObject *
Antoine Pitrou1a9a9d52010-08-28 18:17:03 +0000981_PyObject_GenericGetAttrWithDict(PyObject *obj, PyObject *name, PyObject *dict)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000982{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000983 PyTypeObject *tp = Py_TYPE(obj);
984 PyObject *descr = NULL;
985 PyObject *res = NULL;
986 descrgetfunc f;
987 Py_ssize_t dictoffset;
988 PyObject **dictptr;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000989
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000990 if (!PyUnicode_Check(name)){
991 PyErr_Format(PyExc_TypeError,
992 "attribute name must be string, not '%.200s'",
993 name->ob_type->tp_name);
994 return NULL;
995 }
996 else
997 Py_INCREF(name);
Guido van Rossumebca9fc2001-12-04 15:54:53 +0000998
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000999 if (tp->tp_dict == NULL) {
1000 if (PyType_Ready(tp) < 0)
1001 goto done;
1002 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001003
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001004 descr = _PyType_Lookup(tp, name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001005 Py_XINCREF(descr);
Michael W. Hudsonb2c7de42003-08-15 13:07:47 +00001006
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001007 f = NULL;
1008 if (descr != NULL) {
1009 f = descr->ob_type->tp_descr_get;
1010 if (f != NULL && PyDescr_IsData(descr)) {
1011 res = f(descr, obj, (PyObject *)obj->ob_type);
1012 Py_DECREF(descr);
1013 goto done;
1014 }
1015 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001016
Antoine Pitrou1a9a9d52010-08-28 18:17:03 +00001017 if (dict == NULL) {
1018 /* Inline _PyObject_GetDictPtr */
1019 dictoffset = tp->tp_dictoffset;
1020 if (dictoffset != 0) {
1021 if (dictoffset < 0) {
1022 Py_ssize_t tsize;
1023 size_t size;
Guido van Rossumc66ff442002-08-19 16:50:48 +00001024
Antoine Pitrou1a9a9d52010-08-28 18:17:03 +00001025 tsize = ((PyVarObject *)obj)->ob_size;
1026 if (tsize < 0)
1027 tsize = -tsize;
1028 size = _PyObject_VAR_SIZE(tp, tsize);
Guido van Rossumc66ff442002-08-19 16:50:48 +00001029
Antoine Pitrou1a9a9d52010-08-28 18:17:03 +00001030 dictoffset += (long)size;
1031 assert(dictoffset > 0);
1032 assert(dictoffset % SIZEOF_VOID_P == 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001033 }
Antoine Pitrou1a9a9d52010-08-28 18:17:03 +00001034 dictptr = (PyObject **) ((char *)obj + dictoffset);
1035 dict = *dictptr;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001036 }
1037 }
Antoine Pitrou1a9a9d52010-08-28 18:17:03 +00001038 if (dict != NULL) {
1039 Py_INCREF(dict);
1040 res = PyDict_GetItem(dict, name);
1041 if (res != NULL) {
1042 Py_INCREF(res);
1043 Py_XDECREF(descr);
1044 Py_DECREF(dict);
1045 goto done;
1046 }
1047 Py_DECREF(dict);
1048 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001049
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001050 if (f != NULL) {
1051 res = f(descr, obj, (PyObject *)Py_TYPE(obj));
1052 Py_DECREF(descr);
1053 goto done;
1054 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001055
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001056 if (descr != NULL) {
1057 res = descr;
1058 /* descr was already increfed above */
1059 goto done;
1060 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001061
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001062 PyErr_Format(PyExc_AttributeError,
1063 "'%.50s' object has no attribute '%U'",
1064 tp->tp_name, name);
Guido van Rossumebca9fc2001-12-04 15:54:53 +00001065 done:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001066 Py_DECREF(name);
1067 return res;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001068}
1069
Antoine Pitrou1a9a9d52010-08-28 18:17:03 +00001070PyObject *
1071PyObject_GenericGetAttr(PyObject *obj, PyObject *name)
1072{
1073 return _PyObject_GenericGetAttrWithDict(obj, name, NULL);
1074}
1075
Tim Peters6d6c1a32001-08-02 04:15:00 +00001076int
Antoine Pitrou1a9a9d52010-08-28 18:17:03 +00001077_PyObject_GenericSetAttrWithDict(PyObject *obj, PyObject *name,
1078 PyObject *value, PyObject *dict)
Tim Peters6d6c1a32001-08-02 04:15:00 +00001079{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001080 PyTypeObject *tp = Py_TYPE(obj);
1081 PyObject *descr;
1082 descrsetfunc f;
1083 PyObject **dictptr;
1084 int res = -1;
Guido van Rossumebca9fc2001-12-04 15:54:53 +00001085
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001086 if (!PyUnicode_Check(name)){
1087 PyErr_Format(PyExc_TypeError,
1088 "attribute name must be string, not '%.200s'",
1089 name->ob_type->tp_name);
1090 return -1;
1091 }
1092 else
1093 Py_INCREF(name);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001094
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001095 if (tp->tp_dict == NULL) {
1096 if (PyType_Ready(tp) < 0)
1097 goto done;
1098 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001099
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001100 descr = _PyType_Lookup(tp, name);
1101 f = NULL;
1102 if (descr != NULL) {
1103 f = descr->ob_type->tp_descr_set;
1104 if (f != NULL && PyDescr_IsData(descr)) {
1105 res = f(descr, obj, value);
1106 goto done;
1107 }
1108 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001109
Antoine Pitrou1a9a9d52010-08-28 18:17:03 +00001110 if (dict == NULL) {
1111 dictptr = _PyObject_GetDictPtr(obj);
1112 if (dictptr != NULL) {
1113 dict = *dictptr;
1114 if (dict == NULL && value != NULL) {
1115 dict = PyDict_New();
1116 if (dict == NULL)
1117 goto done;
1118 *dictptr = dict;
1119 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001120 }
Antoine Pitrou1a9a9d52010-08-28 18:17:03 +00001121 }
1122 if (dict != NULL) {
1123 Py_INCREF(dict);
1124 if (value == NULL)
1125 res = PyDict_DelItem(dict, name);
1126 else
1127 res = PyDict_SetItem(dict, name, value);
1128 if (res < 0 && PyErr_ExceptionMatches(PyExc_KeyError))
1129 PyErr_SetObject(PyExc_AttributeError, name);
1130 Py_DECREF(dict);
1131 goto done;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001132 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001133
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001134 if (f != NULL) {
1135 res = f(descr, obj, value);
1136 goto done;
1137 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001138
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001139 if (descr == NULL) {
1140 PyErr_Format(PyExc_AttributeError,
1141 "'%.100s' object has no attribute '%U'",
1142 tp->tp_name, name);
1143 goto done;
1144 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001145
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001146 PyErr_Format(PyExc_AttributeError,
1147 "'%.50s' object attribute '%U' is read-only",
1148 tp->tp_name, name);
Guido van Rossumebca9fc2001-12-04 15:54:53 +00001149 done:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001150 Py_DECREF(name);
1151 return res;
Guido van Rossum98ff96a1997-05-20 18:34:44 +00001152}
1153
Antoine Pitrou1a9a9d52010-08-28 18:17:03 +00001154int
1155PyObject_GenericSetAttr(PyObject *obj, PyObject *name, PyObject *value)
1156{
1157 return _PyObject_GenericSetAttrWithDict(obj, name, value, NULL);
1158}
1159
1160
Guido van Rossum6ac258d1993-05-12 08:24:20 +00001161/* Test a value used as condition, e.g., in a for or if statement.
1162 Return -1 if an error occurred */
1163
1164int
Fred Drake100814d2000-07-09 15:48:49 +00001165PyObject_IsTrue(PyObject *v)
Guido van Rossum6ac258d1993-05-12 08:24:20 +00001166{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001167 Py_ssize_t res;
1168 if (v == Py_True)
1169 return 1;
1170 if (v == Py_False)
1171 return 0;
1172 if (v == Py_None)
1173 return 0;
1174 else if (v->ob_type->tp_as_number != NULL &&
1175 v->ob_type->tp_as_number->nb_bool != NULL)
1176 res = (*v->ob_type->tp_as_number->nb_bool)(v);
1177 else if (v->ob_type->tp_as_mapping != NULL &&
1178 v->ob_type->tp_as_mapping->mp_length != NULL)
1179 res = (*v->ob_type->tp_as_mapping->mp_length)(v);
1180 else if (v->ob_type->tp_as_sequence != NULL &&
1181 v->ob_type->tp_as_sequence->sq_length != NULL)
1182 res = (*v->ob_type->tp_as_sequence->sq_length)(v);
1183 else
1184 return 1;
1185 /* if it is negative, it should be either -1 or -2 */
1186 return (res > 0) ? 1 : Py_SAFE_DOWNCAST(res, Py_ssize_t, int);
Guido van Rossum6ac258d1993-05-12 08:24:20 +00001187}
1188
Tim Peters803526b2002-07-07 05:13:56 +00001189/* equivalent of 'not v'
Guido van Rossumc3d3f961998-04-09 17:53:59 +00001190 Return -1 if an error occurred */
1191
1192int
Fred Drake100814d2000-07-09 15:48:49 +00001193PyObject_Not(PyObject *v)
Guido van Rossumc3d3f961998-04-09 17:53:59 +00001194{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001195 int res;
1196 res = PyObject_IsTrue(v);
1197 if (res < 0)
1198 return res;
1199 return res == 0;
Guido van Rossumc3d3f961998-04-09 17:53:59 +00001200}
1201
Guido van Rossum49b11fe1995-01-26 00:38:22 +00001202/* Test whether an object can be called */
1203
1204int
Fred Drake100814d2000-07-09 15:48:49 +00001205PyCallable_Check(PyObject *x)
Guido van Rossum49b11fe1995-01-26 00:38:22 +00001206{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001207 if (x == NULL)
1208 return 0;
1209 return x->ob_type->tp_call != NULL;
Guido van Rossum49b11fe1995-01-26 00:38:22 +00001210}
1211
Tim Peters7eea37e2001-09-04 22:08:56 +00001212
Georg Brandle32b4222007-03-10 22:13:27 +00001213/* Helper for PyObject_Dir without arguments: returns the local scope. */
1214static PyObject *
Guido van Rossumad7d8d12007-04-13 01:39:34 +00001215_dir_locals(void)
Tim Peters305b5852001-09-17 02:38:46 +00001216{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001217 PyObject *names;
1218 PyObject *locals = PyEval_GetLocals();
Tim Peters305b5852001-09-17 02:38:46 +00001219
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001220 if (locals == NULL) {
1221 PyErr_SetString(PyExc_SystemError, "frame does not exist");
1222 return NULL;
1223 }
Tim Peters305b5852001-09-17 02:38:46 +00001224
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001225 names = PyMapping_Keys(locals);
1226 if (!names)
1227 return NULL;
1228 if (!PyList_Check(names)) {
1229 PyErr_Format(PyExc_TypeError,
1230 "dir(): expected keys() of locals to be a list, "
1231 "not '%.200s'", Py_TYPE(names)->tp_name);
1232 Py_DECREF(names);
1233 return NULL;
1234 }
Benjamin Peterson3bbb7222011-06-11 16:12:08 -05001235 if (PyList_Sort(names)) {
1236 Py_DECREF(names);
1237 return NULL;
1238 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001239 /* the locals don't need to be DECREF'd */
1240 return names;
Georg Brandle32b4222007-03-10 22:13:27 +00001241}
1242
Benjamin Peterson82b00c12011-05-24 11:09:06 -05001243/* Helper for PyObject_Dir: object introspection. */
Georg Brandle32b4222007-03-10 22:13:27 +00001244static PyObject *
1245_dir_object(PyObject *obj)
1246{
Benjamin Peterson3bbb7222011-06-11 16:12:08 -05001247 PyObject *result, *sorted;
Benjamin Peterson7963a352011-05-23 16:11:05 -05001248 static PyObject *dir_str = NULL;
1249 PyObject *dirfunc = _PyObject_LookupSpecial(obj, "__dir__", &dir_str);
Georg Brandle32b4222007-03-10 22:13:27 +00001250
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001251 assert(obj);
1252 if (dirfunc == NULL) {
Benjamin Peterson82b00c12011-05-24 11:09:06 -05001253 if (!PyErr_Occurred())
1254 PyErr_SetString(PyExc_TypeError, "object does not provide __dir__");
1255 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001256 }
Benjamin Peterson82b00c12011-05-24 11:09:06 -05001257 /* use __dir__ */
1258 result = PyObject_CallFunctionObjArgs(dirfunc, NULL);
1259 Py_DECREF(dirfunc);
1260 if (result == NULL)
1261 return NULL;
Benjamin Peterson3bbb7222011-06-11 16:12:08 -05001262 /* return sorted(result) */
1263 sorted = PySequence_List(result);
1264 Py_DECREF(result);
1265 if (sorted == NULL)
1266 return NULL;
1267 if (PyList_Sort(sorted)) {
1268 Py_DECREF(sorted);
1269 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001270 }
Benjamin Peterson3bbb7222011-06-11 16:12:08 -05001271 return sorted;
Georg Brandle32b4222007-03-10 22:13:27 +00001272}
1273
1274/* Implementation of dir() -- if obj is NULL, returns the names in the current
1275 (local) scope. Otherwise, performs introspection of the object: returns a
1276 sorted list of attribute names (supposedly) accessible from the object
1277*/
1278PyObject *
1279PyObject_Dir(PyObject *obj)
1280{
Benjamin Peterson3bbb7222011-06-11 16:12:08 -05001281 return (obj == NULL) ? _dir_locals() : _dir_object(obj);
Tim Peters7eea37e2001-09-04 22:08:56 +00001282}
Guido van Rossum49b11fe1995-01-26 00:38:22 +00001283
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001284/*
Raymond Hettinger66d2be82011-07-28 09:55:13 -07001285None is a non-NULL undefined value.
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001286There is (and should be!) no way to create other objects of this type,
Guido van Rossum3f5da241990-12-20 15:06:42 +00001287so there is exactly one (which is indestructible, by the way).
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001288*/
1289
Guido van Rossum0c182a11992-03-27 17:26:13 +00001290/* ARGSUSED */
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001291static PyObject *
Fred Drake100814d2000-07-09 15:48:49 +00001292none_repr(PyObject *op)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001293{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001294 return PyUnicode_FromString("None");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001295}
1296
Barry Warsaw9bf16442001-01-23 16:24:35 +00001297/* ARGUSED */
1298static void
Tim Peters803526b2002-07-07 05:13:56 +00001299none_dealloc(PyObject* ignore)
Barry Warsaw9bf16442001-01-23 16:24:35 +00001300{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001301 /* This should never get called, but we also don't want to SEGV if
1302 * we accidentally decref None out of existence.
1303 */
1304 Py_FatalError("deallocating None");
Barry Warsaw9bf16442001-01-23 16:24:35 +00001305}
1306
Benjamin Petersonc4607ae2011-07-29 18:19:43 -05001307static PyObject *
1308none_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
1309{
1310 if (PyTuple_GET_SIZE(args) || (kwargs && PyDict_Size(kwargs))) {
1311 PyErr_SetString(PyExc_TypeError, "NoneType takes no arguments");
1312 return NULL;
1313 }
1314 Py_RETURN_NONE;
1315}
1316
Raymond Hettinger66d2be82011-07-28 09:55:13 -07001317static int
1318none_bool(PyObject *v)
1319{
1320 return 0;
1321}
1322
1323static PyNumberMethods none_as_number = {
1324 0, /* nb_add */
1325 0, /* nb_subtract */
1326 0, /* nb_multiply */
1327 0, /* nb_remainder */
1328 0, /* nb_divmod */
1329 0, /* nb_power */
1330 0, /* nb_negative */
1331 0, /* nb_positive */
1332 0, /* nb_absolute */
1333 (inquiry)none_bool, /* nb_bool */
1334 0, /* nb_invert */
1335 0, /* nb_lshift */
1336 0, /* nb_rshift */
1337 0, /* nb_and */
1338 0, /* nb_xor */
1339 0, /* nb_or */
1340 0, /* nb_int */
1341 0, /* nb_reserved */
1342 0, /* nb_float */
1343 0, /* nb_inplace_add */
1344 0, /* nb_inplace_subtract */
1345 0, /* nb_inplace_multiply */
1346 0, /* nb_inplace_remainder */
1347 0, /* nb_inplace_power */
1348 0, /* nb_inplace_lshift */
1349 0, /* nb_inplace_rshift */
1350 0, /* nb_inplace_and */
1351 0, /* nb_inplace_xor */
1352 0, /* nb_inplace_or */
1353 0, /* nb_floor_divide */
1354 0, /* nb_true_divide */
1355 0, /* nb_inplace_floor_divide */
1356 0, /* nb_inplace_true_divide */
1357 0, /* nb_index */
1358};
Barry Warsaw9bf16442001-01-23 16:24:35 +00001359
Guido van Rossumba21a492001-08-16 08:17:26 +00001360static PyTypeObject PyNone_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001361 PyVarObject_HEAD_INIT(&PyType_Type, 0)
1362 "NoneType",
1363 0,
1364 0,
1365 none_dealloc, /*tp_dealloc*/ /*never called*/
1366 0, /*tp_print*/
1367 0, /*tp_getattr*/
1368 0, /*tp_setattr*/
1369 0, /*tp_reserved*/
1370 none_repr, /*tp_repr*/
Raymond Hettinger66d2be82011-07-28 09:55:13 -07001371 &none_as_number, /*tp_as_number*/
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001372 0, /*tp_as_sequence*/
1373 0, /*tp_as_mapping*/
1374 0, /*tp_hash */
Benjamin Petersonc4607ae2011-07-29 18:19:43 -05001375 0, /*tp_call */
1376 0, /*tp_str */
1377 0, /*tp_getattro */
1378 0, /*tp_setattro */
1379 0, /*tp_as_buffer */
1380 Py_TPFLAGS_DEFAULT, /*tp_flags */
1381 0, /*tp_doc */
1382 0, /*tp_traverse */
1383 0, /*tp_clear */
1384 0, /*tp_richcompare */
1385 0, /*tp_weaklistoffset */
1386 0, /*tp_iter */
1387 0, /*tp_iternext */
1388 0, /*tp_methods */
1389 0, /*tp_members */
1390 0, /*tp_getset */
1391 0, /*tp_base */
1392 0, /*tp_dict */
1393 0, /*tp_descr_get */
1394 0, /*tp_descr_set */
1395 0, /*tp_dictoffset */
1396 0, /*tp_init */
1397 0, /*tp_alloc */
1398 none_new, /*tp_new */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001399};
1400
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001401PyObject _Py_NoneStruct = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001402 _PyObject_EXTRA_INIT
1403 1, &PyNone_Type
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001404};
1405
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +00001406/* NotImplemented is an object that can be used to signal that an
1407 operation is not implemented for the given type combination. */
1408
1409static PyObject *
1410NotImplemented_repr(PyObject *op)
1411{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001412 return PyUnicode_FromString("NotImplemented");
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +00001413}
1414
Benjamin Peterson18d7d7a2011-07-29 18:27:44 -05001415static PyObject *
1416notimplemented_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
1417{
1418 if (PyTuple_GET_SIZE(args) || (kwargs && PyDict_Size(kwargs))) {
1419 PyErr_SetString(PyExc_TypeError, "NotImplementedType takes no arguments");
1420 return NULL;
1421 }
Brian Curtindfc80e32011-08-10 20:28:54 -05001422 Py_RETURN_NOTIMPLEMENTED;
Benjamin Peterson18d7d7a2011-07-29 18:27:44 -05001423}
1424
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +00001425static PyTypeObject PyNotImplemented_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001426 PyVarObject_HEAD_INIT(&PyType_Type, 0)
1427 "NotImplementedType",
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 NotImplemented_repr, /*tp_repr*/
1436 0, /*tp_as_number*/
1437 0, /*tp_as_sequence*/
1438 0, /*tp_as_mapping*/
1439 0, /*tp_hash */
Benjamin Peterson18d7d7a2011-07-29 18:27:44 -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 notimplemented_new, /*tp_new */
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +00001464};
1465
1466PyObject _Py_NotImplementedStruct = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001467 _PyObject_EXTRA_INIT
1468 1, &PyNotImplemented_Type
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +00001469};
1470
Guido van Rossumba21a492001-08-16 08:17:26 +00001471void
1472_Py_ReadyTypes(void)
1473{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001474 if (PyType_Ready(&PyType_Type) < 0)
1475 Py_FatalError("Can't initialize type type");
Guido van Rossumba21a492001-08-16 08:17:26 +00001476
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001477 if (PyType_Ready(&_PyWeakref_RefType) < 0)
1478 Py_FatalError("Can't initialize weakref type");
Fred Drake0a4dd392004-07-02 18:57:45 +00001479
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001480 if (PyType_Ready(&_PyWeakref_CallableProxyType) < 0)
1481 Py_FatalError("Can't initialize callable weakref proxy type");
Benjamin Petersonfd838e62009-04-20 02:09:13 +00001482
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001483 if (PyType_Ready(&_PyWeakref_ProxyType) < 0)
1484 Py_FatalError("Can't initialize weakref proxy type");
Benjamin Petersonfd838e62009-04-20 02:09:13 +00001485
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001486 if (PyType_Ready(&PyBool_Type) < 0)
1487 Py_FatalError("Can't initialize bool type");
Guido van Rossum77f6a652002-04-03 22:41:51 +00001488
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001489 if (PyType_Ready(&PyByteArray_Type) < 0)
1490 Py_FatalError("Can't initialize bytearray type");
Guido van Rossum4dfe8a12006-04-22 23:28:04 +00001491
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001492 if (PyType_Ready(&PyBytes_Type) < 0)
1493 Py_FatalError("Can't initialize 'str'");
Guido van Rossumcacfc072002-05-24 19:01:59 +00001494
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001495 if (PyType_Ready(&PyList_Type) < 0)
1496 Py_FatalError("Can't initialize list type");
Guido van Rossumba21a492001-08-16 08:17:26 +00001497
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001498 if (PyType_Ready(&PyNone_Type) < 0)
1499 Py_FatalError("Can't initialize None type");
Guido van Rossumba21a492001-08-16 08:17:26 +00001500
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001501 if (PyType_Ready(&PyNotImplemented_Type) < 0)
1502 Py_FatalError("Can't initialize NotImplemented type");
Benjamin Petersonae937c02009-04-18 20:54:08 +00001503
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001504 if (PyType_Ready(&PyTraceBack_Type) < 0)
1505 Py_FatalError("Can't initialize traceback type");
Benjamin Petersonae937c02009-04-18 20:54:08 +00001506
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001507 if (PyType_Ready(&PySuper_Type) < 0)
1508 Py_FatalError("Can't initialize super type");
Benjamin Petersonae937c02009-04-18 20:54:08 +00001509
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001510 if (PyType_Ready(&PyBaseObject_Type) < 0)
1511 Py_FatalError("Can't initialize object type");
Benjamin Petersonae937c02009-04-18 20:54:08 +00001512
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001513 if (PyType_Ready(&PyRange_Type) < 0)
1514 Py_FatalError("Can't initialize range type");
Benjamin Petersonae937c02009-04-18 20:54:08 +00001515
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001516 if (PyType_Ready(&PyDict_Type) < 0)
1517 Py_FatalError("Can't initialize dict type");
Benjamin Petersonae937c02009-04-18 20:54:08 +00001518
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001519 if (PyType_Ready(&PySet_Type) < 0)
1520 Py_FatalError("Can't initialize set type");
Benjamin Petersonae937c02009-04-18 20:54:08 +00001521
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001522 if (PyType_Ready(&PyUnicode_Type) < 0)
1523 Py_FatalError("Can't initialize str type");
Benjamin Petersonae937c02009-04-18 20:54:08 +00001524
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001525 if (PyType_Ready(&PySlice_Type) < 0)
1526 Py_FatalError("Can't initialize slice type");
Benjamin Petersonae937c02009-04-18 20:54:08 +00001527
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001528 if (PyType_Ready(&PyStaticMethod_Type) < 0)
1529 Py_FatalError("Can't initialize static method type");
Benjamin Petersonae937c02009-04-18 20:54:08 +00001530
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001531 if (PyType_Ready(&PyComplex_Type) < 0)
1532 Py_FatalError("Can't initialize complex type");
Skip Montanaroba1e0f42009-10-18 14:25:35 +00001533
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001534 if (PyType_Ready(&PyFloat_Type) < 0)
1535 Py_FatalError("Can't initialize float type");
Benjamin Petersonae937c02009-04-18 20:54:08 +00001536
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001537 if (PyType_Ready(&PyLong_Type) < 0)
1538 Py_FatalError("Can't initialize int type");
Benjamin Petersonae937c02009-04-18 20:54:08 +00001539
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001540 if (PyType_Ready(&PyFrozenSet_Type) < 0)
1541 Py_FatalError("Can't initialize frozenset type");
Benjamin Petersonae937c02009-04-18 20:54:08 +00001542
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001543 if (PyType_Ready(&PyProperty_Type) < 0)
1544 Py_FatalError("Can't initialize property type");
Benjamin Petersonae937c02009-04-18 20:54:08 +00001545
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001546 if (PyType_Ready(&PyMemoryView_Type) < 0)
1547 Py_FatalError("Can't initialize memoryview type");
Benjamin Petersonae937c02009-04-18 20:54:08 +00001548
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001549 if (PyType_Ready(&PyTuple_Type) < 0)
1550 Py_FatalError("Can't initialize tuple type");
Benjamin Petersonae937c02009-04-18 20:54:08 +00001551
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001552 if (PyType_Ready(&PyEnum_Type) < 0)
1553 Py_FatalError("Can't initialize enumerate type");
Benjamin Petersonae937c02009-04-18 20:54:08 +00001554
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001555 if (PyType_Ready(&PyReversed_Type) < 0)
1556 Py_FatalError("Can't initialize reversed type");
Guido van Rossum47b9ff62006-08-24 00:41:19 +00001557
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001558 if (PyType_Ready(&PyStdPrinter_Type) < 0)
1559 Py_FatalError("Can't initialize StdPrinter");
Benjamin Petersonfd838e62009-04-20 02:09:13 +00001560
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001561 if (PyType_Ready(&PyCode_Type) < 0)
1562 Py_FatalError("Can't initialize code type");
Benjamin Petersonfd838e62009-04-20 02:09:13 +00001563
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001564 if (PyType_Ready(&PyFrame_Type) < 0)
1565 Py_FatalError("Can't initialize frame type");
Benjamin Petersonfd838e62009-04-20 02:09:13 +00001566
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001567 if (PyType_Ready(&PyCFunction_Type) < 0)
1568 Py_FatalError("Can't initialize builtin function type");
Benjamin Petersonfd838e62009-04-20 02:09:13 +00001569
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001570 if (PyType_Ready(&PyMethod_Type) < 0)
1571 Py_FatalError("Can't initialize method type");
Benjamin Petersonfd838e62009-04-20 02:09:13 +00001572
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001573 if (PyType_Ready(&PyFunction_Type) < 0)
1574 Py_FatalError("Can't initialize function type");
Benjamin Petersonfd838e62009-04-20 02:09:13 +00001575
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001576 if (PyType_Ready(&PyDictProxy_Type) < 0)
1577 Py_FatalError("Can't initialize dict proxy type");
Benjamin Petersonfd838e62009-04-20 02:09:13 +00001578
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001579 if (PyType_Ready(&PyGen_Type) < 0)
1580 Py_FatalError("Can't initialize generator type");
Benjamin Petersonfd838e62009-04-20 02:09:13 +00001581
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001582 if (PyType_Ready(&PyGetSetDescr_Type) < 0)
1583 Py_FatalError("Can't initialize get-set descriptor type");
Benjamin Petersonfd838e62009-04-20 02:09:13 +00001584
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001585 if (PyType_Ready(&PyWrapperDescr_Type) < 0)
1586 Py_FatalError("Can't initialize wrapper type");
Benjamin Petersonfd838e62009-04-20 02:09:13 +00001587
Benjamin Petersoneff61f62011-09-01 16:32:31 -04001588 if (PyType_Ready(&_PyMethodWrapper_Type) < 0)
1589 Py_FatalError("Can't initialize method wrapper type");
1590
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001591 if (PyType_Ready(&PyEllipsis_Type) < 0)
1592 Py_FatalError("Can't initialize ellipsis type");
Benjamin Petersonfd838e62009-04-20 02:09:13 +00001593
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001594 if (PyType_Ready(&PyMemberDescr_Type) < 0)
1595 Py_FatalError("Can't initialize member descriptor type");
Benjamin Peterson8bc5b682009-05-09 18:10:51 +00001596
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001597 if (PyType_Ready(&PyFilter_Type) < 0)
1598 Py_FatalError("Can't initialize filter type");
Benjamin Peterson8bc5b682009-05-09 18:10:51 +00001599
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001600 if (PyType_Ready(&PyMap_Type) < 0)
1601 Py_FatalError("Can't initialize map type");
Benjamin Peterson8bc5b682009-05-09 18:10:51 +00001602
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001603 if (PyType_Ready(&PyZip_Type) < 0)
1604 Py_FatalError("Can't initialize zip type");
Guido van Rossumba21a492001-08-16 08:17:26 +00001605}
1606
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001607
Guido van Rossum84a90321996-05-22 16:34:47 +00001608#ifdef Py_TRACE_REFS
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001609
Guido van Rossumaacdc9d1996-08-12 21:32:12 +00001610void
Fred Drake100814d2000-07-09 15:48:49 +00001611_Py_NewReference(PyObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001612{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001613 _Py_INC_REFTOTAL;
1614 op->ob_refcnt = 1;
1615 _Py_AddToAllObjects(op, 1);
1616 _Py_INC_TPALLOCS(op);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001617}
1618
Guido van Rossumaacdc9d1996-08-12 21:32:12 +00001619void
Fred Drake100814d2000-07-09 15:48:49 +00001620_Py_ForgetReference(register PyObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001621{
Guido van Rossumbffd6832000-01-20 22:32:56 +00001622#ifdef SLOW_UNREF_CHECK
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001623 register PyObject *p;
Guido van Rossumbffd6832000-01-20 22:32:56 +00001624#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001625 if (op->ob_refcnt < 0)
1626 Py_FatalError("UNREF negative refcnt");
1627 if (op == &refchain ||
1628 op->_ob_prev->_ob_next != op || op->_ob_next->_ob_prev != op) {
1629 fprintf(stderr, "* ob\n");
1630 _PyObject_Dump(op);
1631 fprintf(stderr, "* op->_ob_prev->_ob_next\n");
1632 _PyObject_Dump(op->_ob_prev->_ob_next);
1633 fprintf(stderr, "* op->_ob_next->_ob_prev\n");
1634 _PyObject_Dump(op->_ob_next->_ob_prev);
1635 Py_FatalError("UNREF invalid object");
1636 }
Guido van Rossum2e8f6141992-09-03 20:32:55 +00001637#ifdef SLOW_UNREF_CHECK
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001638 for (p = refchain._ob_next; p != &refchain; p = p->_ob_next) {
1639 if (p == op)
1640 break;
1641 }
1642 if (p == &refchain) /* Not found */
1643 Py_FatalError("UNREF unknown object");
Guido van Rossum2e8f6141992-09-03 20:32:55 +00001644#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001645 op->_ob_next->_ob_prev = op->_ob_prev;
1646 op->_ob_prev->_ob_next = op->_ob_next;
1647 op->_ob_next = op->_ob_prev = NULL;
1648 _Py_INC_TPFREES(op);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001649}
1650
Guido van Rossumaacdc9d1996-08-12 21:32:12 +00001651void
Fred Drake100814d2000-07-09 15:48:49 +00001652_Py_Dealloc(PyObject *op)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001653{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001654 destructor dealloc = Py_TYPE(op)->tp_dealloc;
1655 _Py_ForgetReference(op);
1656 (*dealloc)(op);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001657}
1658
Tim Peters269b2a62003-04-17 19:52:29 +00001659/* Print all live objects. Because PyObject_Print is called, the
1660 * interpreter must be in a healthy state.
1661 */
Guido van Rossumaacdc9d1996-08-12 21:32:12 +00001662void
Fred Drake100814d2000-07-09 15:48:49 +00001663_Py_PrintReferences(FILE *fp)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001664{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001665 PyObject *op;
1666 fprintf(fp, "Remaining objects:\n");
1667 for (op = refchain._ob_next; op != &refchain; op = op->_ob_next) {
1668 fprintf(fp, "%p [%" PY_FORMAT_SIZE_T "d] ", op, op->ob_refcnt);
1669 if (PyObject_Print(op, fp, 0) != 0)
1670 PyErr_Clear();
1671 putc('\n', fp);
1672 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001673}
1674
Tim Peters269b2a62003-04-17 19:52:29 +00001675/* Print the addresses of all live objects. Unlike _Py_PrintReferences, this
1676 * doesn't make any calls to the Python C API, so is always safe to call.
1677 */
1678void
1679_Py_PrintReferenceAddresses(FILE *fp)
1680{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001681 PyObject *op;
1682 fprintf(fp, "Remaining object addresses:\n");
1683 for (op = refchain._ob_next; op != &refchain; op = op->_ob_next)
1684 fprintf(fp, "%p [%" PY_FORMAT_SIZE_T "d] %s\n", op,
1685 op->ob_refcnt, Py_TYPE(op)->tp_name);
Tim Peters269b2a62003-04-17 19:52:29 +00001686}
1687
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +00001688PyObject *
Fred Drake100814d2000-07-09 15:48:49 +00001689_Py_GetObjects(PyObject *self, PyObject *args)
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +00001690{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001691 int i, n;
1692 PyObject *t = NULL;
1693 PyObject *res, *op;
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +00001694
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001695 if (!PyArg_ParseTuple(args, "i|O", &n, &t))
1696 return NULL;
1697 op = refchain._ob_next;
1698 res = PyList_New(0);
1699 if (res == NULL)
1700 return NULL;
1701 for (i = 0; (n == 0 || i < n) && op != &refchain; i++) {
1702 while (op == self || op == args || op == res || op == t ||
1703 (t != NULL && Py_TYPE(op) != (PyTypeObject *) t)) {
1704 op = op->_ob_next;
1705 if (op == &refchain)
1706 return res;
1707 }
1708 if (PyList_Append(res, op) < 0) {
1709 Py_DECREF(res);
1710 return NULL;
1711 }
1712 op = op->_ob_next;
1713 }
1714 return res;
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +00001715}
1716
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001717#endif
Guido van Rossum97ead3f1996-01-12 01:24:09 +00001718
Benjamin Petersonb173f782009-05-05 22:31:58 +00001719/* Hack to force loading of pycapsule.o */
1720PyTypeObject *_PyCapsule_hack = &PyCapsule_Type;
1721
1722
Guido van Rossum84a90321996-05-22 16:34:47 +00001723/* Hack to force loading of abstract.o */
Martin v. Löwis18e16552006-02-15 17:27:45 +00001724Py_ssize_t (*_Py_abstract_hack)(PyObject *) = PyObject_Size;
Guido van Rossume09fb551997-08-05 02:04:34 +00001725
1726
Andrew M. Kuchling1582a3a2000-08-16 12:27:23 +00001727/* Python's malloc wrappers (see pymem.h) */
Guido van Rossume09fb551997-08-05 02:04:34 +00001728
Thomas Wouters334fb892000-07-25 12:56:38 +00001729void *
Fred Drake100814d2000-07-09 15:48:49 +00001730PyMem_Malloc(size_t nbytes)
Guido van Rossume09fb551997-08-05 02:04:34 +00001731{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001732 return PyMem_MALLOC(nbytes);
Guido van Rossume09fb551997-08-05 02:04:34 +00001733}
1734
Thomas Wouters334fb892000-07-25 12:56:38 +00001735void *
1736PyMem_Realloc(void *p, size_t nbytes)
Guido van Rossume09fb551997-08-05 02:04:34 +00001737{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001738 return PyMem_REALLOC(p, nbytes);
Guido van Rossume09fb551997-08-05 02:04:34 +00001739}
1740
1741void
Thomas Wouters334fb892000-07-25 12:56:38 +00001742PyMem_Free(void *p)
Guido van Rossume09fb551997-08-05 02:04:34 +00001743{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001744 PyMem_FREE(p);
Guido van Rossumb18618d2000-05-03 23:44:39 +00001745}
1746
1747
Guido van Rossum86610361998-04-10 22:32:46 +00001748/* These methods are used to control infinite recursion in repr, str, print,
1749 etc. Container objects that may recursively contain themselves,
1750 e.g. builtin dictionaries and lists, should used Py_ReprEnter() and
1751 Py_ReprLeave() to avoid infinite recursion.
1752
1753 Py_ReprEnter() returns 0 the first time it is called for a particular
1754 object and 1 every time thereafter. It returns -1 if an exception
1755 occurred. Py_ReprLeave() has no return value.
1756
1757 See dictobject.c and listobject.c for examples of use.
1758*/
1759
1760#define KEY "Py_Repr"
1761
1762int
Fred Drake100814d2000-07-09 15:48:49 +00001763Py_ReprEnter(PyObject *obj)
Guido van Rossum86610361998-04-10 22:32:46 +00001764{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001765 PyObject *dict;
1766 PyObject *list;
1767 Py_ssize_t i;
Guido van Rossum86610361998-04-10 22:32:46 +00001768
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001769 dict = PyThreadState_GetDict();
1770 if (dict == NULL)
1771 return 0;
1772 list = PyDict_GetItemString(dict, KEY);
1773 if (list == NULL) {
1774 list = PyList_New(0);
1775 if (list == NULL)
1776 return -1;
1777 if (PyDict_SetItemString(dict, KEY, list) < 0)
1778 return -1;
1779 Py_DECREF(list);
1780 }
1781 i = PyList_GET_SIZE(list);
1782 while (--i >= 0) {
1783 if (PyList_GET_ITEM(list, i) == obj)
1784 return 1;
1785 }
1786 PyList_Append(list, obj);
1787 return 0;
Guido van Rossum86610361998-04-10 22:32:46 +00001788}
1789
1790void
Fred Drake100814d2000-07-09 15:48:49 +00001791Py_ReprLeave(PyObject *obj)
Guido van Rossum86610361998-04-10 22:32:46 +00001792{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001793 PyObject *dict;
1794 PyObject *list;
1795 Py_ssize_t i;
Guido van Rossum86610361998-04-10 22:32:46 +00001796
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001797 dict = PyThreadState_GetDict();
1798 if (dict == NULL)
1799 return;
1800 list = PyDict_GetItemString(dict, KEY);
1801 if (list == NULL || !PyList_Check(list))
1802 return;
1803 i = PyList_GET_SIZE(list);
1804 /* Count backwards because we always expect obj to be list[-1] */
1805 while (--i >= 0) {
1806 if (PyList_GET_ITEM(list, i) == obj) {
1807 PyList_SetSlice(list, i, i + 1, NULL);
1808 break;
1809 }
1810 }
Guido van Rossum86610361998-04-10 22:32:46 +00001811}
Guido van Rossumd724b232000-03-13 16:01:29 +00001812
Tim Peters803526b2002-07-07 05:13:56 +00001813/* Trashcan support. */
Guido van Rossumd724b232000-03-13 16:01:29 +00001814
Tim Peters803526b2002-07-07 05:13:56 +00001815/* Current call-stack depth of tp_dealloc calls. */
Guido van Rossumd724b232000-03-13 16:01:29 +00001816int _PyTrash_delete_nesting = 0;
Guido van Rossume92e6102000-04-24 15:40:53 +00001817
Tim Peters803526b2002-07-07 05:13:56 +00001818/* List of objects that still need to be cleaned up, singly linked via their
1819 * gc headers' gc_prev pointers.
1820 */
1821PyObject *_PyTrash_delete_later = NULL;
Guido van Rossumd724b232000-03-13 16:01:29 +00001822
Tim Peters803526b2002-07-07 05:13:56 +00001823/* Add op to the _PyTrash_delete_later list. Called when the current
1824 * call-stack depth gets large. op must be a currently untracked gc'ed
1825 * object, with refcount 0. Py_DECREF must already have been called on it.
1826 */
Guido van Rossumd724b232000-03-13 16:01:29 +00001827void
Fred Drake100814d2000-07-09 15:48:49 +00001828_PyTrash_deposit_object(PyObject *op)
Guido van Rossumd724b232000-03-13 16:01:29 +00001829{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001830 assert(PyObject_IS_GC(op));
1831 assert(_Py_AS_GC(op)->gc.gc_refs == _PyGC_REFS_UNTRACKED);
1832 assert(op->ob_refcnt == 0);
1833 _Py_AS_GC(op)->gc.gc_prev = (PyGC_Head *)_PyTrash_delete_later;
1834 _PyTrash_delete_later = op;
Guido van Rossumd724b232000-03-13 16:01:29 +00001835}
1836
Tim Peters803526b2002-07-07 05:13:56 +00001837/* Dealloccate all the objects in the _PyTrash_delete_later list. Called when
1838 * the call-stack unwinds again.
1839 */
Guido van Rossumd724b232000-03-13 16:01:29 +00001840void
Fred Drake100814d2000-07-09 15:48:49 +00001841_PyTrash_destroy_chain(void)
Guido van Rossumd724b232000-03-13 16:01:29 +00001842{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001843 while (_PyTrash_delete_later) {
1844 PyObject *op = _PyTrash_delete_later;
1845 destructor dealloc = Py_TYPE(op)->tp_dealloc;
Neil Schemenauerf589c052002-03-29 03:05:54 +00001846
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001847 _PyTrash_delete_later =
1848 (PyObject*) _Py_AS_GC(op)->gc.gc_prev;
Neil Schemenauerf589c052002-03-29 03:05:54 +00001849
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001850 /* Call the deallocator directly. This used to try to
1851 * fool Py_DECREF into calling it indirectly, but
1852 * Py_DECREF was already called on this object, and in
1853 * assorted non-release builds calling Py_DECREF again ends
1854 * up distorting allocation statistics.
1855 */
1856 assert(op->ob_refcnt == 0);
1857 ++_PyTrash_delete_nesting;
1858 (*dealloc)(op);
1859 --_PyTrash_delete_nesting;
1860 }
Guido van Rossumd724b232000-03-13 16:01:29 +00001861}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001862
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00001863#ifndef Py_TRACE_REFS
1864/* For Py_LIMITED_API, we need an out-of-line version of _Py_Dealloc.
1865 Define this here, so we can undefine the macro. */
1866#undef _Py_Dealloc
1867PyAPI_FUNC(void) _Py_Dealloc(PyObject *);
1868void
1869_Py_Dealloc(PyObject *op)
1870{
1871 _Py_INC_TPFREES(op) _Py_COUNT_ALLOCS_COMMA
1872 (*Py_TYPE(op)->tp_dealloc)(op);
1873}
1874#endif
1875
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001876#ifdef __cplusplus
1877}
1878#endif