blob: 150f3576c6014f86164a37b2812358ca94748f88 [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001
Guido van Rossum3f5da241990-12-20 15:06:42 +00002/* Generic object operations; and implementation of None (NoObject) */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003
Guido van Rossumc0b618a1997-05-02 03:12:38 +00004#include "Python.h"
Jack Jansend49cbe12000-08-22 21:52:51 +00005#ifdef HAVE_LIMITS_H
6#include <limits.h>
7#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00008
Jack Jansen28fc8802000-07-11 21:47:20 +00009#ifdef macintosh
10#include "macglue.h"
11#endif
12
Guido van Rossume92e6102000-04-24 15:40:53 +000013/* just for trashcan: */
14#include "compile.h"
15#include "frameobject.h"
16#include "traceback.h"
17
Guido van Rossum6f9e4331995-03-29 16:57:48 +000018#if defined( Py_TRACE_REFS ) || defined( Py_REF_DEBUG )
Guido van Rossumbffd6832000-01-20 22:32:56 +000019DL_IMPORT(long) _Py_RefTotal;
Guido van Rossum3f5da241990-12-20 15:06:42 +000020#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000021
Guido van Rossum3f5da241990-12-20 15:06:42 +000022/* Object allocation routines used by NEWOBJ and NEWVAROBJ macros.
23 These are used by the individual routines for object creation.
24 Do not call them otherwise, they do not initialize the object! */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000025
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +000026#ifdef COUNT_ALLOCS
Guido van Rossumc0b618a1997-05-02 03:12:38 +000027static PyTypeObject *type_list;
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +000028extern int tuple_zero_allocs, fast_tuple_allocs;
29extern int quick_int_allocs, quick_neg_int_allocs;
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +000030extern int null_strings, one_strings;
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +000031void
Fred Drake100814d2000-07-09 15:48:49 +000032dump_counts(void)
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +000033{
Guido van Rossumc0b618a1997-05-02 03:12:38 +000034 PyTypeObject *tp;
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +000035
36 for (tp = type_list; tp; tp = tp->tp_next)
Sjoerd Mullender52c1f511993-10-25 08:40:52 +000037 fprintf(stderr, "%s alloc'd: %d, freed: %d, max in use: %d\n",
38 tp->tp_name, tp->tp_alloc, tp->tp_free,
39 tp->tp_maxalloc);
40 fprintf(stderr, "fast tuple allocs: %d, empty: %d\n",
41 fast_tuple_allocs, tuple_zero_allocs);
42 fprintf(stderr, "fast int allocs: pos: %d, neg: %d\n",
43 quick_int_allocs, quick_neg_int_allocs);
44 fprintf(stderr, "null strings: %d, 1-strings: %d\n",
45 null_strings, one_strings);
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +000046}
47
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +000048PyObject *
Fred Drake100814d2000-07-09 15:48:49 +000049get_counts(void)
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +000050{
51 PyTypeObject *tp;
52 PyObject *result;
53 PyObject *v;
54
55 result = PyList_New(0);
56 if (result == NULL)
57 return NULL;
58 for (tp = type_list; tp; tp = tp->tp_next) {
59 v = Py_BuildValue("(siii)", tp->tp_name, tp->tp_alloc,
60 tp->tp_free, tp->tp_maxalloc);
61 if (v == NULL) {
62 Py_DECREF(result);
63 return NULL;
64 }
65 if (PyList_Append(result, v) < 0) {
66 Py_DECREF(v);
67 Py_DECREF(result);
68 return NULL;
69 }
70 Py_DECREF(v);
71 }
72 return result;
73}
74
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +000075void
Fred Drake100814d2000-07-09 15:48:49 +000076inc_count(PyTypeObject *tp)
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +000077{
78 if (tp->tp_alloc == 0) {
Guido van Rossumd8953cb1995-04-06 14:46:26 +000079 /* first time; insert in linked list */
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +000080 if (tp->tp_next != NULL) /* sanity check */
Guido van Rossumc0b618a1997-05-02 03:12:38 +000081 Py_FatalError("XXX inc_count sanity check");
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +000082 tp->tp_next = type_list;
83 type_list = tp;
84 }
85 tp->tp_alloc++;
86 if (tp->tp_alloc - tp->tp_free > tp->tp_maxalloc)
87 tp->tp_maxalloc = tp->tp_alloc - tp->tp_free;
88}
89#endif
90
Guido van Rossumc0b618a1997-05-02 03:12:38 +000091PyObject *
Fred Drake100814d2000-07-09 15:48:49 +000092PyObject_Init(PyObject *op, PyTypeObject *tp)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000093{
Guido van Rossumb18618d2000-05-03 23:44:39 +000094 if (op == NULL) {
95 PyErr_SetString(PyExc_SystemError,
96 "NULL object passed to PyObject_Init");
97 return op;
98 }
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +000099#ifdef WITH_CYCLE_GC
100 if (PyType_IS_GC(tp))
101 op = (PyObject *) PyObject_FROM_GC(op);
102#endif
Guido van Rossumb18618d2000-05-03 23:44:39 +0000103 /* Any changes should be reflected in PyObject_INIT (objimpl.h) */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000104 op->ob_type = tp;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000105 _Py_NewReference(op);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000106 return op;
107}
108
Guido van Rossumb18618d2000-05-03 23:44:39 +0000109PyVarObject *
Fred Drake100814d2000-07-09 15:48:49 +0000110PyObject_InitVar(PyVarObject *op, PyTypeObject *tp, int size)
Guido van Rossumb18618d2000-05-03 23:44:39 +0000111{
112 if (op == NULL) {
113 PyErr_SetString(PyExc_SystemError,
114 "NULL object passed to PyObject_InitVar");
115 return op;
116 }
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000117#ifdef WITH_CYCLE_GC
118 if (PyType_IS_GC(tp))
119 op = (PyVarObject *) PyObject_FROM_GC(op);
120#endif
Guido van Rossumb18618d2000-05-03 23:44:39 +0000121 /* Any changes should be reflected in PyObject_INIT_VAR */
122 op->ob_size = size;
123 op->ob_type = tp;
124 _Py_NewReference((PyObject *)op);
125 return op;
126}
127
128PyObject *
Fred Drake100814d2000-07-09 15:48:49 +0000129_PyObject_New(PyTypeObject *tp)
Guido van Rossumb18618d2000-05-03 23:44:39 +0000130{
131 PyObject *op;
132 op = (PyObject *) PyObject_MALLOC(_PyObject_SIZE(tp));
133 if (op == NULL)
134 return PyErr_NoMemory();
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000135#ifdef WITH_CYCLE_GC
136 if (PyType_IS_GC(tp))
137 op = (PyObject *) PyObject_FROM_GC(op);
138#endif
Guido van Rossumb18618d2000-05-03 23:44:39 +0000139 return PyObject_INIT(op, tp);
140}
141
Guido van Rossumd0c87ee1997-05-15 21:31:03 +0000142PyVarObject *
Fred Drake100814d2000-07-09 15:48:49 +0000143_PyObject_NewVar(PyTypeObject *tp, int size)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000144{
Guido van Rossumb18618d2000-05-03 23:44:39 +0000145 PyVarObject *op;
146 op = (PyVarObject *) PyObject_MALLOC(_PyObject_VAR_SIZE(tp, size));
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000147 if (op == NULL)
Guido van Rossumd0c87ee1997-05-15 21:31:03 +0000148 return (PyVarObject *)PyErr_NoMemory();
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000149#ifdef WITH_CYCLE_GC
150 if (PyType_IS_GC(tp))
151 op = (PyVarObject *) PyObject_FROM_GC(op);
152#endif
Guido van Rossumb18618d2000-05-03 23:44:39 +0000153 return PyObject_INIT_VAR(op, tp, size);
154}
155
156void
Fred Drake100814d2000-07-09 15:48:49 +0000157_PyObject_Del(PyObject *op)
Guido van Rossumb18618d2000-05-03 23:44:39 +0000158{
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000159#ifdef WITH_CYCLE_GC
Guido van Rossum4cc6ac72000-07-01 01:00:38 +0000160 if (op && PyType_IS_GC(op->ob_type)) {
161 op = (PyObject *) PyObject_AS_GC(op);
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000162 }
Guido van Rossum4cc6ac72000-07-01 01:00:38 +0000163#endif
164 PyObject_FREE(op);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000165}
166
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000167#ifndef WITH_CYCLE_GC
168/* extension modules might need these */
169void _PyGC_Insert(PyObject *op) { }
170void _PyGC_Remove(PyObject *op) { }
171#endif
172
Guido van Rossum90933611991-06-07 16:10:43 +0000173int
Fred Drake100814d2000-07-09 15:48:49 +0000174PyObject_Print(PyObject *op, FILE *fp, int flags)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000175{
Guido van Rossum278ef591991-07-27 21:40:24 +0000176 int ret = 0;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000177 if (PyErr_CheckSignals())
Guido van Rossum90933611991-06-07 16:10:43 +0000178 return -1;
Guido van Rossum9b00dfa1998-04-28 16:06:54 +0000179#ifdef USE_STACKCHECK
180 if (PyOS_CheckStack()) {
181 PyErr_SetString(PyExc_MemoryError, "Stack overflow");
182 return -1;
183 }
184#endif
Guido van Rossum687ef6e2000-01-12 16:28:58 +0000185 clearerr(fp); /* Clear any previous error condition */
Guido van Rossum90933611991-06-07 16:10:43 +0000186 if (op == NULL) {
187 fprintf(fp, "<nil>");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000188 }
Guido van Rossum90933611991-06-07 16:10:43 +0000189 else {
190 if (op->ob_refcnt <= 0)
Fred Drakea44d3532000-06-30 15:01:00 +0000191 fprintf(fp, "<refcnt %u at %p>",
192 op->ob_refcnt, op);
Guido van Rossum2e8f6141992-09-03 20:32:55 +0000193 else if (op->ob_type->tp_print == NULL) {
194 if (op->ob_type->tp_repr == NULL) {
Fred Drakea44d3532000-06-30 15:01:00 +0000195 fprintf(fp, "<%s object at %p>",
196 op->ob_type->tp_name, op);
Guido van Rossum2e8f6141992-09-03 20:32:55 +0000197 }
198 else {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000199 PyObject *s;
200 if (flags & Py_PRINT_RAW)
201 s = PyObject_Str(op);
Guido van Rossumc6004111993-11-05 10:22:19 +0000202 else
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000203 s = PyObject_Repr(op);
Guido van Rossum2e8f6141992-09-03 20:32:55 +0000204 if (s == NULL)
205 ret = -1;
Guido van Rossum2e8f6141992-09-03 20:32:55 +0000206 else {
Guido van Rossum565798d1998-04-21 22:25:01 +0000207 ret = PyObject_Print(s, fp,
208 Py_PRINT_RAW);
Guido van Rossum2e8f6141992-09-03 20:32:55 +0000209 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000210 Py_XDECREF(s);
Guido van Rossum2e8f6141992-09-03 20:32:55 +0000211 }
212 }
Guido van Rossum90933611991-06-07 16:10:43 +0000213 else
Guido van Rossum278ef591991-07-27 21:40:24 +0000214 ret = (*op->ob_type->tp_print)(op, fp, flags);
Guido van Rossum90933611991-06-07 16:10:43 +0000215 }
Guido van Rossum278ef591991-07-27 21:40:24 +0000216 if (ret == 0) {
217 if (ferror(fp)) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000218 PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossum278ef591991-07-27 21:40:24 +0000219 clearerr(fp);
220 ret = -1;
221 }
222 }
223 return ret;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000224}
225
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000226PyObject *
Fred Drake100814d2000-07-09 15:48:49 +0000227PyObject_Repr(PyObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000228{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000229 if (PyErr_CheckSignals())
Guido van Rossum90933611991-06-07 16:10:43 +0000230 return NULL;
Guido van Rossum9b00dfa1998-04-28 16:06:54 +0000231#ifdef USE_STACKCHECK
232 if (PyOS_CheckStack()) {
233 PyErr_SetString(PyExc_MemoryError, "Stack overflow");
234 return NULL;
235 }
236#endif
Guido van Rossum90933611991-06-07 16:10:43 +0000237 if (v == NULL)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000238 return PyString_FromString("<NULL>");
Guido van Rossum90933611991-06-07 16:10:43 +0000239 else if (v->ob_type->tp_repr == NULL) {
240 char buf[120];
Fred Drakea44d3532000-06-30 15:01:00 +0000241 sprintf(buf, "<%.80s object at %p>",
242 v->ob_type->tp_name, v);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000243 return PyString_FromString(buf);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000244 }
Guido van Rossum4c08d552000-03-10 22:55:18 +0000245 else {
246 PyObject *res;
247 res = (*v->ob_type->tp_repr)(v);
248 if (res == NULL)
249 return NULL;
Fredrik Lundhefecc7d2000-07-01 14:31:09 +0000250 if (PyUnicode_Check(res)) {
251 PyObject* str;
Fredrik Lundh2a1e0602000-07-08 17:43:32 +0000252 str = PyUnicode_AsUnicodeEscapeString(res);
Marc-André Lemburg891bc652000-07-03 09:57:53 +0000253 Py_DECREF(res);
254 if (str)
Fredrik Lundhefecc7d2000-07-01 14:31:09 +0000255 res = str;
Marc-André Lemburg891bc652000-07-03 09:57:53 +0000256 else
257 return NULL;
Fredrik Lundhefecc7d2000-07-01 14:31:09 +0000258 }
Guido van Rossum4c08d552000-03-10 22:55:18 +0000259 if (!PyString_Check(res)) {
260 PyErr_Format(PyExc_TypeError,
Guido van Rossum5db862d2000-04-10 12:46:51 +0000261 "__repr__ returned non-string (type %.200s)",
Guido van Rossum4c08d552000-03-10 22:55:18 +0000262 res->ob_type->tp_name);
263 Py_DECREF(res);
264 return NULL;
265 }
266 return res;
267 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000268}
269
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000270PyObject *
Fred Drake100814d2000-07-09 15:48:49 +0000271PyObject_Str(PyObject *v)
Guido van Rossumc6004111993-11-05 10:22:19 +0000272{
Guido van Rossum4c08d552000-03-10 22:55:18 +0000273 PyObject *res;
274
Guido van Rossumc6004111993-11-05 10:22:19 +0000275 if (v == NULL)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000276 return PyString_FromString("<NULL>");
277 else if (PyString_Check(v)) {
278 Py_INCREF(v);
Guido van Rossumc6004111993-11-05 10:22:19 +0000279 return v;
280 }
Guido van Rossum32b582b1995-01-17 16:35:13 +0000281 else if (v->ob_type->tp_str != NULL)
Guido van Rossum4c08d552000-03-10 22:55:18 +0000282 res = (*v->ob_type->tp_str)(v);
Guido van Rossumc6004111993-11-05 10:22:19 +0000283 else {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000284 PyObject *func;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000285 if (!PyInstance_Check(v) ||
286 (func = PyObject_GetAttrString(v, "__str__")) == NULL) {
287 PyErr_Clear();
288 return PyObject_Repr(v);
Guido van Rossumc6004111993-11-05 10:22:19 +0000289 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000290 res = PyEval_CallObject(func, (PyObject *)NULL);
291 Py_DECREF(func);
Guido van Rossumc6004111993-11-05 10:22:19 +0000292 }
Guido van Rossum4c08d552000-03-10 22:55:18 +0000293 if (res == NULL)
294 return NULL;
Marc-André Lemburg891bc652000-07-03 09:57:53 +0000295 if (PyUnicode_Check(res)) {
296 PyObject* str;
297 str = PyUnicode_AsEncodedString(res, NULL, NULL);
298 Py_DECREF(res);
299 if (str)
300 res = str;
301 else
302 return NULL;
303 }
Guido van Rossum4c08d552000-03-10 22:55:18 +0000304 if (!PyString_Check(res)) {
305 PyErr_Format(PyExc_TypeError,
Guido van Rossum5db862d2000-04-10 12:46:51 +0000306 "__str__ returned non-string (type %.200s)",
Guido van Rossum4c08d552000-03-10 22:55:18 +0000307 res->ob_type->tp_name);
308 Py_DECREF(res);
309 return NULL;
310 }
311 return res;
Guido van Rossumc6004111993-11-05 10:22:19 +0000312}
313
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000314static PyObject *
Fred Drake100814d2000-07-09 15:48:49 +0000315do_cmp(PyObject *v, PyObject *w)
Guido van Rossum20566841995-01-12 11:26:10 +0000316{
Guido van Rossumc8b6df91997-05-23 00:06:51 +0000317 long c;
Guido van Rossum20566841995-01-12 11:26:10 +0000318 /* __rcmp__ actually won't be called unless __cmp__ isn't defined,
319 because the check in cmpobject() reverses the objects first.
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000320 This is intentional -- it makes no sense to define cmp(x,y)
321 different than -cmp(y,x). */
322 if (PyInstance_Check(v) || PyInstance_Check(w))
323 return PyInstance_DoBinOp(v, w, "__cmp__", "__rcmp__", do_cmp);
Guido van Rossumc8b6df91997-05-23 00:06:51 +0000324 c = PyObject_Compare(v, w);
325 if (c && PyErr_Occurred())
326 return NULL;
327 return PyInt_FromLong(c);
Guido van Rossum20566841995-01-12 11:26:10 +0000328}
329
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000330PyObject *_PyCompareState_Key;
331
Thomas Wouters7e474022000-07-16 12:04:32 +0000332/* _PyCompareState_nesting is incremented before calling compare (for
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000333 some types) and decremented on exit. If the count exceeds the
334 nesting limit, enable code to detect circular data structures.
335*/
Jack Jansend49cbe12000-08-22 21:52:51 +0000336#ifdef macintosh
337#define NESTING_LIMIT 60
338#else
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000339#define NESTING_LIMIT 500
Jack Jansend49cbe12000-08-22 21:52:51 +0000340#endif
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000341int _PyCompareState_nesting = 0;
342
343static PyObject*
Fred Drake100814d2000-07-09 15:48:49 +0000344get_inprogress_dict(void)
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000345{
346 PyObject *tstate_dict, *inprogress;
347
348 tstate_dict = PyThreadState_GetDict();
349 if (tstate_dict == NULL) {
350 PyErr_BadInternalCall();
351 return NULL;
352 }
353 inprogress = PyDict_GetItem(tstate_dict, _PyCompareState_Key);
354 if (inprogress == NULL) {
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000355 inprogress = PyDict_New();
356 if (inprogress == NULL)
357 return NULL;
358 if (PyDict_SetItem(tstate_dict, _PyCompareState_Key,
359 inprogress) == -1) {
360 Py_DECREF(inprogress);
361 return NULL;
362 }
Jeremy Hyltona251ea02000-06-09 16:20:39 +0000363 Py_DECREF(inprogress);
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000364 }
365 return inprogress;
366}
367
368static PyObject *
Fred Drake100814d2000-07-09 15:48:49 +0000369make_pair(PyObject *v, PyObject *w)
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000370{
371 PyObject *pair;
Barry Warsaw9d23a4e2000-08-18 05:01:19 +0000372 Py_uintptr_t iv = (Py_uintptr_t)v;
373 Py_uintptr_t iw = (Py_uintptr_t)w;
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000374
375 pair = PyTuple_New(2);
376 if (pair == NULL) {
377 return NULL;
378 }
Barry Warsaw9d23a4e2000-08-18 05:01:19 +0000379 if (iv <= iw) {
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000380 PyTuple_SET_ITEM(pair, 0, PyLong_FromVoidPtr((void *)v));
381 PyTuple_SET_ITEM(pair, 1, PyLong_FromVoidPtr((void *)w));
382 } else {
383 PyTuple_SET_ITEM(pair, 0, PyLong_FromVoidPtr((void *)w));
384 PyTuple_SET_ITEM(pair, 1, PyLong_FromVoidPtr((void *)v));
385 }
386 return pair;
387}
388
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000389int
Fred Drake100814d2000-07-09 15:48:49 +0000390PyObject_Compare(PyObject *v, PyObject *w)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000391{
Guido van Rossumcd5a5f61998-06-09 18:58:44 +0000392 PyTypeObject *vtp, *wtp;
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000393 int result;
394
Jack Jansend49cbe12000-08-22 21:52:51 +0000395#if defined(USE_STACKCHECK)
Guido van Rossum9c0a99e2000-08-30 15:53:50 +0000396 if (PyOS_CheckStack()) {
Jack Jansend49cbe12000-08-22 21:52:51 +0000397 PyErr_SetString(PyExc_MemoryError, "Stack overflow");
398 return -1;
399 }
400#endif
Guido van Rossumc8b6df91997-05-23 00:06:51 +0000401 if (v == NULL || w == NULL) {
402 PyErr_BadInternalCall();
403 return -1;
404 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000405 if (v == w)
406 return 0;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000407 if (PyInstance_Check(v) || PyInstance_Check(w)) {
408 PyObject *res;
Guido van Rossum20566841995-01-12 11:26:10 +0000409 int c;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000410 if (!PyInstance_Check(v))
411 return -PyObject_Compare(w, v);
Vladimir Marangozov1d3e2392000-08-11 00:14:26 +0000412 _PyCompareState_nesting++;
413 if (_PyCompareState_nesting > NESTING_LIMIT) {
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000414 PyObject *inprogress, *pair;
415
416 inprogress = get_inprogress_dict();
417 if (inprogress == NULL) {
Vladimir Marangozov1d3e2392000-08-11 00:14:26 +0000418 _PyCompareState_nesting--;
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000419 return -1;
420 }
421 pair = make_pair(v, w);
422 if (PyDict_GetItem(inprogress, pair)) {
423 /* already comparing these objects. assume
424 they're equal until shown otherwise */
425 Py_DECREF(pair);
Vladimir Marangozov1d3e2392000-08-11 00:14:26 +0000426 _PyCompareState_nesting--;
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000427 return 0;
428 }
429 if (PyDict_SetItem(inprogress, pair, pair) == -1) {
Vladimir Marangozov1d3e2392000-08-11 00:14:26 +0000430 _PyCompareState_nesting--;
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000431 return -1;
432 }
433 res = do_cmp(v, w);
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000434 /* XXX DelItem shouldn't fail */
435 PyDict_DelItem(inprogress, pair);
436 Py_DECREF(pair);
437 } else {
438 res = do_cmp(v, w);
439 }
Vladimir Marangozov1d3e2392000-08-11 00:14:26 +0000440 _PyCompareState_nesting--;
Guido van Rossumc8b6df91997-05-23 00:06:51 +0000441 if (res == NULL)
442 return -1;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000443 if (!PyInt_Check(res)) {
444 Py_DECREF(res);
Guido van Rossumc8b6df91997-05-23 00:06:51 +0000445 PyErr_SetString(PyExc_TypeError,
446 "comparison did not return an int");
447 return -1;
Guido van Rossum20566841995-01-12 11:26:10 +0000448 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000449 c = PyInt_AsLong(res);
450 Py_DECREF(res);
Guido van Rossum20566841995-01-12 11:26:10 +0000451 return (c < 0) ? -1 : (c > 0) ? 1 : 0;
452 }
Guido van Rossumcd5a5f61998-06-09 18:58:44 +0000453 if ((vtp = v->ob_type) != (wtp = w->ob_type)) {
454 char *vname = vtp->tp_name;
455 char *wname = wtp->tp_name;
Guido van Rossumb4db1941998-07-21 21:56:41 +0000456 if (vtp->tp_as_number != NULL && wtp->tp_as_number != NULL) {
Guido van Rossum242c6421997-11-19 16:03:17 +0000457 int err;
458 err = PyNumber_CoerceEx(&v, &w);
459 if (err < 0)
Guido van Rossumc8b6df91997-05-23 00:06:51 +0000460 return -1;
Guido van Rossum242c6421997-11-19 16:03:17 +0000461 else if (err == 0) {
Guido van Rossumb4db1941998-07-21 21:56:41 +0000462 int cmp;
463 vtp = v->ob_type;
464 if (vtp->tp_compare == NULL)
465 cmp = (v < w) ? -1 : 1;
466 else
467 cmp = (*vtp->tp_compare)(v, w);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000468 Py_DECREF(v);
469 Py_DECREF(w);
Guido van Rossum9fb03681991-07-01 18:48:04 +0000470 return cmp;
471 }
472 }
Guido van Rossumb244f692000-04-10 13:42:33 +0000473 else if (PyUnicode_Check(v) || PyUnicode_Check(w)) {
474 int result = PyUnicode_Compare(v, w);
475 if (result == -1 && PyErr_Occurred() &&
476 PyErr_ExceptionMatches(PyExc_TypeError))
477 /* TypeErrors are ignored: if Unicode coercion
478 fails due to one of the arguments not
479 having the right type, we continue as
480 defined by the coercion protocol (see
481 above). Luckily, decoding errors are
482 reported as ValueErrors and are not masked
483 by this technique. */
484 PyErr_Clear();
485 else
486 return result;
487 }
Guido van Rossumcd5a5f61998-06-09 18:58:44 +0000488 else if (vtp->tp_as_number != NULL)
489 vname = "";
490 else if (wtp->tp_as_number != NULL)
491 wname = "";
492 /* Numerical types compare smaller than all other types */
493 return strcmp(vname, wname);
Guido van Rossum9fb03681991-07-01 18:48:04 +0000494 }
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000495 if (vtp->tp_compare == NULL) {
Barry Warsaw9d23a4e2000-08-18 05:01:19 +0000496 Py_uintptr_t iv = (Py_uintptr_t)v;
497 Py_uintptr_t iw = (Py_uintptr_t)w;
498 return (iv < iw) ? -1 : 1;
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000499 }
Vladimir Marangozov1d3e2392000-08-11 00:14:26 +0000500 _PyCompareState_nesting++;
501 if (_PyCompareState_nesting > NESTING_LIMIT
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000502 && (vtp->tp_as_mapping
503 || (vtp->tp_as_sequence && !PyString_Check(v)))) {
504 PyObject *inprogress, *pair;
505
506 inprogress = get_inprogress_dict();
507 if (inprogress == NULL) {
Vladimir Marangozov1d3e2392000-08-11 00:14:26 +0000508 _PyCompareState_nesting--;
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000509 return -1;
510 }
511 pair = make_pair(v, w);
512 if (PyDict_GetItem(inprogress, pair)) {
513 /* already comparing these objects. assume
514 they're equal until shown otherwise */
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000515 Py_DECREF(pair);
Vladimir Marangozov1d3e2392000-08-11 00:14:26 +0000516 _PyCompareState_nesting--;
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000517 return 0;
518 }
519 if (PyDict_SetItem(inprogress, pair, pair) == -1) {
Vladimir Marangozov1d3e2392000-08-11 00:14:26 +0000520 _PyCompareState_nesting--;
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000521 return -1;
522 }
523 result = (*vtp->tp_compare)(v, w);
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000524 PyDict_DelItem(inprogress, pair); /* XXX shouldn't fail */
525 Py_DECREF(pair);
526 } else {
527 result = (*vtp->tp_compare)(v, w);
528 }
Vladimir Marangozov1d3e2392000-08-11 00:14:26 +0000529 _PyCompareState_nesting--;
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000530 return result;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000531}
532
Fred Drake13634cf2000-06-29 19:17:04 +0000533
534/* Set of hash utility functions to help maintaining the invariant that
535 iff a==b then hash(a)==hash(b)
536
537 All the utility functions (_Py_Hash*()) return "-1" to signify an error.
538*/
539
540long
Fred Drake100814d2000-07-09 15:48:49 +0000541_Py_HashDouble(double v)
Fred Drake13634cf2000-06-29 19:17:04 +0000542{
Tim Peters39dce292000-08-15 03:34:48 +0000543 double intpart, fractpart;
544 int expo;
545 long hipart;
546 long x; /* the final hash value */
547 /* This is designed so that Python numbers of different types
548 * that compare equal hash to the same value; otherwise comparisons
549 * of mapping keys will turn out weird.
550 */
551
552#ifdef MPW /* MPW C modf expects pointer to extended as second argument */
553{
554 extended e;
555 fractpart = modf(v, &e);
556 intpart = e;
557}
558#else
559 fractpart = modf(v, &intpart);
560#endif
561 if (fractpart == 0.0) {
562 /* This must return the same hash as an equal int or long. */
563 if (intpart > LONG_MAX || -intpart > LONG_MAX) {
564 /* Convert to long and use its hash. */
565 PyObject *plong; /* converted to Python long */
566 if (Py_IS_INFINITY(intpart))
567 /* can't convert to long int -- arbitrary */
568 v = v < 0 ? -271828.0 : 314159.0;
569 plong = PyLong_FromDouble(v);
570 if (plong == NULL)
571 return -1;
572 x = PyObject_Hash(plong);
573 Py_DECREF(plong);
574 return x;
575 }
576 /* Fits in a C long == a Python int, so is its own hash. */
577 x = (long)intpart;
578 if (x == -1)
579 x = -2;
580 return x;
581 }
582 /* The fractional part is non-zero, so we don't have to worry about
583 * making this match the hash of some other type.
584 * Use frexp to get at the bits in the double.
Fred Drake13634cf2000-06-29 19:17:04 +0000585 * Since the VAX D double format has 56 mantissa bits, which is the
586 * most of any double format in use, each of these parts may have as
587 * many as (but no more than) 56 significant bits.
Tim Peters39dce292000-08-15 03:34:48 +0000588 * So, assuming sizeof(long) >= 4, each part can be broken into two
589 * longs; frexp and multiplication are used to do that.
590 * Also, since the Cray double format has 15 exponent bits, which is
591 * the most of any double format in use, shifting the exponent field
592 * left by 15 won't overflow a long (again assuming sizeof(long) >= 4).
Fred Drake13634cf2000-06-29 19:17:04 +0000593 */
Tim Peters39dce292000-08-15 03:34:48 +0000594 v = frexp(v, &expo);
595 v *= 2147483648.0; /* 2**31 */
596 hipart = (long)v; /* take the top 32 bits */
597 v = (v - (double)hipart) * 2147483648.0; /* get the next 32 bits */
598 x = hipart + (long)v + (expo << 15);
599 if (x == -1)
600 x = -2;
601 return x;
Fred Drake13634cf2000-06-29 19:17:04 +0000602}
603
604long
Fred Drake100814d2000-07-09 15:48:49 +0000605_Py_HashPointer(void *p)
Fred Drake13634cf2000-06-29 19:17:04 +0000606{
607#if SIZEOF_LONG >= SIZEOF_VOID_P
608 return (long)p;
609#else
610 /* convert to a Python long and hash that */
611 PyObject* longobj;
612 long x;
613
614 if ((longobj = PyLong_FromVoidPtr(p)) == NULL) {
615 x = -1;
616 goto finally;
617 }
618 x = PyObject_Hash(longobj);
619
620finally:
621 Py_XDECREF(longobj);
622 return x;
623#endif
624}
625
626
Guido van Rossum9bfef441993-03-29 10:43:31 +0000627long
Fred Drake100814d2000-07-09 15:48:49 +0000628PyObject_Hash(PyObject *v)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000629{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000630 PyTypeObject *tp = v->ob_type;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000631 if (tp->tp_hash != NULL)
632 return (*tp->tp_hash)(v);
Fred Drake13634cf2000-06-29 19:17:04 +0000633 if (tp->tp_compare == NULL) {
634 return _Py_HashPointer(v); /* Use address as hash value */
635 }
Guido van Rossum9bfef441993-03-29 10:43:31 +0000636 /* If there's a cmp but no hash defined, the object can't be hashed */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000637 PyErr_SetString(PyExc_TypeError, "unhashable type");
Guido van Rossum9bfef441993-03-29 10:43:31 +0000638 return -1;
639}
640
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000641PyObject *
Fred Drake100814d2000-07-09 15:48:49 +0000642PyObject_GetAttrString(PyObject *v, char *name)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000643{
Guido van Rossumd8eb1b31996-08-09 20:52:03 +0000644 if (v->ob_type->tp_getattro != NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000645 PyObject *w, *res;
Guido van Rossumc6d06701997-01-18 07:57:16 +0000646 w = PyString_InternFromString(name);
Guido van Rossumd8eb1b31996-08-09 20:52:03 +0000647 if (w == NULL)
648 return NULL;
649 res = (*v->ob_type->tp_getattro)(v, w);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000650 Py_XDECREF(w);
Guido van Rossumd8eb1b31996-08-09 20:52:03 +0000651 return res;
652 }
653
Guido van Rossum3f5da241990-12-20 15:06:42 +0000654 if (v->ob_type->tp_getattr == NULL) {
Guido van Rossumdb935161998-01-19 22:16:36 +0000655 PyErr_Format(PyExc_AttributeError,
656 "'%.50s' object has no attribute '%.400s'",
657 v->ob_type->tp_name,
658 name);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000659 return NULL;
660 }
661 else {
662 return (*v->ob_type->tp_getattr)(v, name);
663 }
664}
665
666int
Fred Drake100814d2000-07-09 15:48:49 +0000667PyObject_HasAttrString(PyObject *v, char *name)
Guido van Rossumed18fdc1993-07-11 19:55:34 +0000668{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000669 PyObject *res = PyObject_GetAttrString(v, name);
Guido van Rossumed18fdc1993-07-11 19:55:34 +0000670 if (res != NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000671 Py_DECREF(res);
Guido van Rossumed18fdc1993-07-11 19:55:34 +0000672 return 1;
673 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000674 PyErr_Clear();
Guido van Rossumed18fdc1993-07-11 19:55:34 +0000675 return 0;
676}
677
678int
Fred Drake100814d2000-07-09 15:48:49 +0000679PyObject_SetAttrString(PyObject *v, char *name, PyObject *w)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000680{
Guido van Rossumd8eb1b31996-08-09 20:52:03 +0000681 if (v->ob_type->tp_setattro != NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000682 PyObject *s;
Guido van Rossumd8eb1b31996-08-09 20:52:03 +0000683 int res;
Guido van Rossumc6d06701997-01-18 07:57:16 +0000684 s = PyString_InternFromString(name);
Guido van Rossumd8eb1b31996-08-09 20:52:03 +0000685 if (s == NULL)
Guido van Rossumb7fc3041996-09-11 22:51:25 +0000686 return -1;
Guido van Rossumd8eb1b31996-08-09 20:52:03 +0000687 res = (*v->ob_type->tp_setattro)(v, s, w);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000688 Py_XDECREF(s);
Guido van Rossumd8eb1b31996-08-09 20:52:03 +0000689 return res;
690 }
691
Guido van Rossum3f5da241990-12-20 15:06:42 +0000692 if (v->ob_type->tp_setattr == NULL) {
693 if (v->ob_type->tp_getattr == NULL)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000694 PyErr_SetString(PyExc_TypeError,
Guido van Rossum3ea74121991-12-24 13:28:03 +0000695 "attribute-less object (assign or del)");
Guido van Rossum3f5da241990-12-20 15:06:42 +0000696 else
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000697 PyErr_SetString(PyExc_TypeError,
Guido van Rossum3ea74121991-12-24 13:28:03 +0000698 "object has read-only attributes");
Guido van Rossum73531a31990-12-20 23:12:40 +0000699 return -1;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000700 }
701 else {
702 return (*v->ob_type->tp_setattr)(v, name, w);
703 }
704}
705
Guido van Rossum98ff96a1997-05-20 18:34:44 +0000706PyObject *
Fred Drake100814d2000-07-09 15:48:49 +0000707PyObject_GetAttr(PyObject *v, PyObject *name)
Guido van Rossum98ff96a1997-05-20 18:34:44 +0000708{
709 if (v->ob_type->tp_getattro != NULL)
710 return (*v->ob_type->tp_getattro)(v, name);
Jeremy Hylton99a8f902000-06-23 14:36:32 +0000711
712 if (!PyString_Check(name)) {
713 PyErr_SetString(PyExc_TypeError,
714 "attribute name must be string");
715 return NULL;
716 }
717 return PyObject_GetAttrString(v, PyString_AS_STRING(name));
Guido van Rossum98ff96a1997-05-20 18:34:44 +0000718}
719
720int
Fred Drake100814d2000-07-09 15:48:49 +0000721PyObject_HasAttr(PyObject *v, PyObject *name)
Guido van Rossum98ff96a1997-05-20 18:34:44 +0000722{
723 PyObject *res = PyObject_GetAttr(v, name);
724 if (res != NULL) {
725 Py_DECREF(res);
726 return 1;
727 }
728 PyErr_Clear();
729 return 0;
730}
731
732int
Fred Drake100814d2000-07-09 15:48:49 +0000733PyObject_SetAttr(PyObject *v, PyObject *name, PyObject *value)
Guido van Rossum98ff96a1997-05-20 18:34:44 +0000734{
735 int err;
736 Py_INCREF(name);
Jeremy Hylton99a8f902000-06-23 14:36:32 +0000737 if (PyString_Check(name))
738 PyString_InternInPlace(&name);
Guido van Rossum98ff96a1997-05-20 18:34:44 +0000739 if (v->ob_type->tp_setattro != NULL)
740 err = (*v->ob_type->tp_setattro)(v, name, value);
Jeremy Hylton99a8f902000-06-23 14:36:32 +0000741 else if (PyString_Check(name)) {
Guido van Rossum98ff96a1997-05-20 18:34:44 +0000742 err = PyObject_SetAttrString(
Jeremy Hylton99a8f902000-06-23 14:36:32 +0000743 v, PyString_AS_STRING(name), value);
744 }
745 else {
746 PyErr_SetString(PyExc_TypeError,
747 "attribute name must be string");
748 err = -1;
749 }
Guido van Rossum98ff96a1997-05-20 18:34:44 +0000750 Py_DECREF(name);
751 return err;
752}
753
Guido van Rossum6ac258d1993-05-12 08:24:20 +0000754/* Test a value used as condition, e.g., in a for or if statement.
755 Return -1 if an error occurred */
756
757int
Fred Drake100814d2000-07-09 15:48:49 +0000758PyObject_IsTrue(PyObject *v)
Guido van Rossum6ac258d1993-05-12 08:24:20 +0000759{
760 int res;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000761 if (v == Py_None)
Guido van Rossum6ac258d1993-05-12 08:24:20 +0000762 res = 0;
Guido van Rossum1c4f4581998-05-22 00:53:24 +0000763 else if (v->ob_type->tp_as_number != NULL &&
764 v->ob_type->tp_as_number->nb_nonzero != NULL)
Guido van Rossum6ac258d1993-05-12 08:24:20 +0000765 res = (*v->ob_type->tp_as_number->nb_nonzero)(v);
Guido van Rossum1c4f4581998-05-22 00:53:24 +0000766 else if (v->ob_type->tp_as_mapping != NULL &&
767 v->ob_type->tp_as_mapping->mp_length != NULL)
Guido van Rossum6ac258d1993-05-12 08:24:20 +0000768 res = (*v->ob_type->tp_as_mapping->mp_length)(v);
Guido van Rossum1c4f4581998-05-22 00:53:24 +0000769 else if (v->ob_type->tp_as_sequence != NULL &&
770 v->ob_type->tp_as_sequence->sq_length != NULL)
Guido van Rossum6ac258d1993-05-12 08:24:20 +0000771 res = (*v->ob_type->tp_as_sequence->sq_length)(v);
772 else
773 res = 1;
774 if (res > 0)
775 res = 1;
776 return res;
777}
778
Guido van Rossumc3d3f961998-04-09 17:53:59 +0000779/* equivalent of 'not v'
780 Return -1 if an error occurred */
781
782int
Fred Drake100814d2000-07-09 15:48:49 +0000783PyObject_Not(PyObject *v)
Guido van Rossumc3d3f961998-04-09 17:53:59 +0000784{
785 int res;
786 res = PyObject_IsTrue(v);
787 if (res < 0)
788 return res;
789 return res == 0;
790}
791
Guido van Rossum5524a591995-01-10 15:26:20 +0000792/* Coerce two numeric types to the "larger" one.
793 Increment the reference count on each argument.
794 Return -1 and raise an exception if no coercion is possible
795 (and then no reference count is incremented).
796*/
797
798int
Fred Drake100814d2000-07-09 15:48:49 +0000799PyNumber_CoerceEx(PyObject **pv, PyObject **pw)
Guido van Rossum5524a591995-01-10 15:26:20 +0000800{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000801 register PyObject *v = *pv;
802 register PyObject *w = *pw;
Guido van Rossum5524a591995-01-10 15:26:20 +0000803 int res;
804
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000805 if (v->ob_type == w->ob_type && !PyInstance_Check(v)) {
806 Py_INCREF(v);
807 Py_INCREF(w);
Guido van Rossum5524a591995-01-10 15:26:20 +0000808 return 0;
809 }
810 if (v->ob_type->tp_as_number && v->ob_type->tp_as_number->nb_coerce) {
811 res = (*v->ob_type->tp_as_number->nb_coerce)(pv, pw);
812 if (res <= 0)
813 return res;
814 }
815 if (w->ob_type->tp_as_number && w->ob_type->tp_as_number->nb_coerce) {
816 res = (*w->ob_type->tp_as_number->nb_coerce)(pw, pv);
817 if (res <= 0)
818 return res;
819 }
Guido van Rossum242c6421997-11-19 16:03:17 +0000820 return 1;
821}
822
823int
Fred Drake100814d2000-07-09 15:48:49 +0000824PyNumber_Coerce(PyObject **pv, PyObject **pw)
Guido van Rossum242c6421997-11-19 16:03:17 +0000825{
826 int err = PyNumber_CoerceEx(pv, pw);
827 if (err <= 0)
828 return err;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000829 PyErr_SetString(PyExc_TypeError, "number coercion failed");
Guido van Rossum5524a591995-01-10 15:26:20 +0000830 return -1;
831}
832
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000833
Guido van Rossum49b11fe1995-01-26 00:38:22 +0000834/* Test whether an object can be called */
835
836int
Fred Drake100814d2000-07-09 15:48:49 +0000837PyCallable_Check(PyObject *x)
Guido van Rossum49b11fe1995-01-26 00:38:22 +0000838{
839 if (x == NULL)
840 return 0;
841 if (x->ob_type->tp_call != NULL ||
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000842 PyFunction_Check(x) ||
843 PyMethod_Check(x) ||
844 PyCFunction_Check(x) ||
845 PyClass_Check(x))
Guido van Rossum49b11fe1995-01-26 00:38:22 +0000846 return 1;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000847 if (PyInstance_Check(x)) {
848 PyObject *call = PyObject_GetAttrString(x, "__call__");
Guido van Rossum49b11fe1995-01-26 00:38:22 +0000849 if (call == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000850 PyErr_Clear();
Guido van Rossum49b11fe1995-01-26 00:38:22 +0000851 return 0;
852 }
853 /* Could test recursively but don't, for fear of endless
854 recursion if some joker sets self.__call__ = self */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000855 Py_DECREF(call);
Guido van Rossum49b11fe1995-01-26 00:38:22 +0000856 return 1;
857 }
858 return 0;
859}
860
861
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000862/*
863NoObject is usable as a non-NULL undefined value, used by the macro None.
864There is (and should be!) no way to create other objects of this type,
Guido van Rossum3f5da241990-12-20 15:06:42 +0000865so there is exactly one (which is indestructible, by the way).
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000866*/
867
Guido van Rossum0c182a11992-03-27 17:26:13 +0000868/* ARGSUSED */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000869static PyObject *
Fred Drake100814d2000-07-09 15:48:49 +0000870none_repr(PyObject *op)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000871{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000872 return PyString_FromString("None");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000873}
874
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000875static PyTypeObject PyNothing_Type = {
876 PyObject_HEAD_INIT(&PyType_Type)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000877 0,
Guido van Rossum3f5da241990-12-20 15:06:42 +0000878 "None",
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000879 0,
880 0,
881 0, /*tp_dealloc*/ /*never called*/
Guido van Rossum7066dd71992-09-17 17:54:56 +0000882 0, /*tp_print*/
Guido van Rossum3f5da241990-12-20 15:06:42 +0000883 0, /*tp_getattr*/
884 0, /*tp_setattr*/
885 0, /*tp_compare*/
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000886 (reprfunc)none_repr, /*tp_repr*/
Guido van Rossum3f5da241990-12-20 15:06:42 +0000887 0, /*tp_as_number*/
888 0, /*tp_as_sequence*/
889 0, /*tp_as_mapping*/
Guido van Rossum9bfef441993-03-29 10:43:31 +0000890 0, /*tp_hash */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000891};
892
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000893PyObject _Py_NoneStruct = {
894 PyObject_HEAD_INIT(&PyNothing_Type)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000895};
896
897
Guido van Rossum84a90321996-05-22 16:34:47 +0000898#ifdef Py_TRACE_REFS
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000899
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000900static PyObject refchain = {&refchain, &refchain};
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000901
Guido van Rossumaacdc9d1996-08-12 21:32:12 +0000902void
Fred Drake100814d2000-07-09 15:48:49 +0000903_Py_ResetReferences(void)
Guido van Rossume09fb551997-08-05 02:04:34 +0000904{
905 refchain._ob_prev = refchain._ob_next = &refchain;
906 _Py_RefTotal = 0;
907}
908
909void
Fred Drake100814d2000-07-09 15:48:49 +0000910_Py_NewReference(PyObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000911{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000912 _Py_RefTotal++;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000913 op->ob_refcnt = 1;
914 op->_ob_next = refchain._ob_next;
915 op->_ob_prev = &refchain;
916 refchain._ob_next->_ob_prev = op;
917 refchain._ob_next = op;
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +0000918#ifdef COUNT_ALLOCS
919 inc_count(op->ob_type);
920#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000921}
922
Guido van Rossumaacdc9d1996-08-12 21:32:12 +0000923void
Fred Drake100814d2000-07-09 15:48:49 +0000924_Py_ForgetReference(register PyObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000925{
Guido van Rossumbffd6832000-01-20 22:32:56 +0000926#ifdef SLOW_UNREF_CHECK
Guido van Rossum4c08d552000-03-10 22:55:18 +0000927 register PyObject *p;
Guido van Rossumbffd6832000-01-20 22:32:56 +0000928#endif
Guido van Rossumd7047b31995-01-02 19:07:15 +0000929 if (op->ob_refcnt < 0)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000930 Py_FatalError("UNREF negative refcnt");
Guido van Rossum2e8f6141992-09-03 20:32:55 +0000931 if (op == &refchain ||
Guido van Rossumd7047b31995-01-02 19:07:15 +0000932 op->_ob_prev->_ob_next != op || op->_ob_next->_ob_prev != op)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000933 Py_FatalError("UNREF invalid object");
Guido van Rossum2e8f6141992-09-03 20:32:55 +0000934#ifdef SLOW_UNREF_CHECK
Guido van Rossum3f5da241990-12-20 15:06:42 +0000935 for (p = refchain._ob_next; p != &refchain; p = p->_ob_next) {
936 if (p == op)
937 break;
938 }
Guido van Rossumd7047b31995-01-02 19:07:15 +0000939 if (p == &refchain) /* Not found */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000940 Py_FatalError("UNREF unknown object");
Guido van Rossum2e8f6141992-09-03 20:32:55 +0000941#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000942 op->_ob_next->_ob_prev = op->_ob_prev;
943 op->_ob_prev->_ob_next = op->_ob_next;
Guido van Rossum2e8f6141992-09-03 20:32:55 +0000944 op->_ob_next = op->_ob_prev = NULL;
Guido van Rossumd8953cb1995-04-06 14:46:26 +0000945#ifdef COUNT_ALLOCS
946 op->ob_type->tp_free++;
947#endif
Guido van Rossum3f5da241990-12-20 15:06:42 +0000948}
949
Guido van Rossumaacdc9d1996-08-12 21:32:12 +0000950void
Fred Drake100814d2000-07-09 15:48:49 +0000951_Py_Dealloc(PyObject *op)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000952{
Guido van Rossum9776adf1994-09-07 14:36:45 +0000953 destructor dealloc = op->ob_type->tp_dealloc;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000954 _Py_ForgetReference(op);
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000955#ifndef WITH_CYCLE_GC
Guido van Rossume92e6102000-04-24 15:40:53 +0000956 if (_PyTrash_delete_nesting < PyTrash_UNWIND_LEVEL-1)
957 op->ob_type = NULL;
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000958#endif
Guido van Rossum9776adf1994-09-07 14:36:45 +0000959 (*dealloc)(op);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000960}
961
Guido van Rossumaacdc9d1996-08-12 21:32:12 +0000962void
Fred Drake100814d2000-07-09 15:48:49 +0000963_Py_PrintReferences(FILE *fp)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000964{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000965 PyObject *op;
Guido van Rossume09fb551997-08-05 02:04:34 +0000966 fprintf(fp, "Remaining objects:\n");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000967 for (op = refchain._ob_next; op != &refchain; op = op->_ob_next) {
968 fprintf(fp, "[%d] ", op->ob_refcnt);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000969 if (PyObject_Print(op, fp, 0) != 0)
970 PyErr_Clear();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000971 putc('\n', fp);
972 }
973}
974
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000975PyObject *
Fred Drake100814d2000-07-09 15:48:49 +0000976_Py_GetObjects(PyObject *self, PyObject *args)
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000977{
978 int i, n;
979 PyObject *t = NULL;
980 PyObject *res, *op;
981
982 if (!PyArg_ParseTuple(args, "i|O", &n, &t))
983 return NULL;
984 op = refchain._ob_next;
985 res = PyList_New(0);
986 if (res == NULL)
987 return NULL;
988 for (i = 0; (n == 0 || i < n) && op != &refchain; i++) {
989 while (op == self || op == args || op == res || op == t ||
990 t != NULL && op->ob_type != (PyTypeObject *) t) {
991 op = op->_ob_next;
992 if (op == &refchain)
993 return res;
994 }
995 if (PyList_Append(res, op) < 0) {
996 Py_DECREF(res);
997 return NULL;
998 }
999 op = op->_ob_next;
1000 }
1001 return res;
1002}
1003
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001004#endif
Guido van Rossum97ead3f1996-01-12 01:24:09 +00001005
1006
1007/* Hack to force loading of cobject.o */
Guido van Rossumda9c2711996-12-05 21:58:58 +00001008PyTypeObject *_Py_cobject_hack = &PyCObject_Type;
Guido van Rossum84a90321996-05-22 16:34:47 +00001009
1010
1011/* Hack to force loading of abstract.o */
Jeremy Hylton6253f832000-07-12 12:56:19 +00001012int (*_Py_abstract_hack)(PyObject *) = &PyObject_Size;
Guido van Rossume09fb551997-08-05 02:04:34 +00001013
1014
Andrew M. Kuchling1582a3a2000-08-16 12:27:23 +00001015/* Python's malloc wrappers (see pymem.h) */
Guido van Rossume09fb551997-08-05 02:04:34 +00001016
Thomas Wouters334fb892000-07-25 12:56:38 +00001017void *
Fred Drake100814d2000-07-09 15:48:49 +00001018PyMem_Malloc(size_t nbytes)
Guido van Rossume09fb551997-08-05 02:04:34 +00001019{
1020#if _PyMem_EXTRA > 0
1021 if (nbytes == 0)
1022 nbytes = _PyMem_EXTRA;
1023#endif
Guido van Rossumb18618d2000-05-03 23:44:39 +00001024 return PyMem_MALLOC(nbytes);
Guido van Rossume09fb551997-08-05 02:04:34 +00001025}
1026
Thomas Wouters334fb892000-07-25 12:56:38 +00001027void *
1028PyMem_Realloc(void *p, size_t nbytes)
Guido van Rossume09fb551997-08-05 02:04:34 +00001029{
1030#if _PyMem_EXTRA > 0
1031 if (nbytes == 0)
1032 nbytes = _PyMem_EXTRA;
1033#endif
Guido van Rossumb18618d2000-05-03 23:44:39 +00001034 return PyMem_REALLOC(p, nbytes);
Guido van Rossume09fb551997-08-05 02:04:34 +00001035}
1036
1037void
Thomas Wouters334fb892000-07-25 12:56:38 +00001038PyMem_Free(void *p)
Guido van Rossume09fb551997-08-05 02:04:34 +00001039{
Guido van Rossumb18618d2000-05-03 23:44:39 +00001040 PyMem_FREE(p);
1041}
1042
1043
1044/* Python's object malloc wrappers (see objimpl.h) */
1045
Thomas Wouters334fb892000-07-25 12:56:38 +00001046void *
Fred Drake100814d2000-07-09 15:48:49 +00001047PyObject_Malloc(size_t nbytes)
Guido van Rossumb18618d2000-05-03 23:44:39 +00001048{
1049 return PyObject_MALLOC(nbytes);
1050}
1051
Thomas Wouters334fb892000-07-25 12:56:38 +00001052void *
1053PyObject_Realloc(void *p, size_t nbytes)
Guido van Rossumb18618d2000-05-03 23:44:39 +00001054{
1055 return PyObject_REALLOC(p, nbytes);
1056}
1057
1058void
Thomas Wouters334fb892000-07-25 12:56:38 +00001059PyObject_Free(void *p)
Guido van Rossumb18618d2000-05-03 23:44:39 +00001060{
1061 PyObject_FREE(p);
Guido van Rossume09fb551997-08-05 02:04:34 +00001062}
Guido van Rossum86610361998-04-10 22:32:46 +00001063
1064
1065/* These methods are used to control infinite recursion in repr, str, print,
1066 etc. Container objects that may recursively contain themselves,
1067 e.g. builtin dictionaries and lists, should used Py_ReprEnter() and
1068 Py_ReprLeave() to avoid infinite recursion.
1069
1070 Py_ReprEnter() returns 0 the first time it is called for a particular
1071 object and 1 every time thereafter. It returns -1 if an exception
1072 occurred. Py_ReprLeave() has no return value.
1073
1074 See dictobject.c and listobject.c for examples of use.
1075*/
1076
1077#define KEY "Py_Repr"
1078
1079int
Fred Drake100814d2000-07-09 15:48:49 +00001080Py_ReprEnter(PyObject *obj)
Guido van Rossum86610361998-04-10 22:32:46 +00001081{
1082 PyObject *dict;
1083 PyObject *list;
1084 int i;
1085
1086 dict = PyThreadState_GetDict();
1087 if (dict == NULL)
1088 return -1;
1089 list = PyDict_GetItemString(dict, KEY);
1090 if (list == NULL) {
1091 list = PyList_New(0);
1092 if (list == NULL)
1093 return -1;
1094 if (PyDict_SetItemString(dict, KEY, list) < 0)
1095 return -1;
1096 Py_DECREF(list);
1097 }
1098 i = PyList_GET_SIZE(list);
1099 while (--i >= 0) {
1100 if (PyList_GET_ITEM(list, i) == obj)
1101 return 1;
1102 }
1103 PyList_Append(list, obj);
1104 return 0;
1105}
1106
1107void
Fred Drake100814d2000-07-09 15:48:49 +00001108Py_ReprLeave(PyObject *obj)
Guido van Rossum86610361998-04-10 22:32:46 +00001109{
1110 PyObject *dict;
1111 PyObject *list;
1112 int i;
1113
1114 dict = PyThreadState_GetDict();
Guido van Rossumeb909461998-04-11 15:17:34 +00001115 if (dict == NULL)
1116 return;
Guido van Rossum86610361998-04-10 22:32:46 +00001117 list = PyDict_GetItemString(dict, KEY);
Guido van Rossumeb909461998-04-11 15:17:34 +00001118 if (list == NULL || !PyList_Check(list))
1119 return;
Guido van Rossum86610361998-04-10 22:32:46 +00001120 i = PyList_GET_SIZE(list);
1121 /* Count backwards because we always expect obj to be list[-1] */
1122 while (--i >= 0) {
1123 if (PyList_GET_ITEM(list, i) == obj) {
1124 PyList_SetSlice(list, i, i + 1, NULL);
1125 break;
1126 }
1127 }
1128}
Guido van Rossumd724b232000-03-13 16:01:29 +00001129
1130/*
1131 trashcan
1132 CT 2k0130
1133 non-recursively destroy nested objects
1134
1135 CT 2k0223
1136 everything is now done in a macro.
1137
1138 CT 2k0305
1139 modified to use functions, after Tim Peter's suggestion.
1140
1141 CT 2k0309
1142 modified to restore a possible error.
Guido van Rossum13ff8eb2000-03-25 18:39:19 +00001143
1144 CT 2k0325
1145 added better safe than sorry check for threadstate
Guido van Rossume92e6102000-04-24 15:40:53 +00001146
1147 CT 2k0422
1148 complete rewrite. We now build a chain via ob_type
1149 and save the limited number of types in ob_refcnt.
1150 This is perfect since we don't need any memory.
1151 A patch for free-threading would need just a lock.
Guido van Rossumd724b232000-03-13 16:01:29 +00001152*/
1153
Guido van Rossume92e6102000-04-24 15:40:53 +00001154#define Py_TRASHCAN_TUPLE 1
1155#define Py_TRASHCAN_LIST 2
1156#define Py_TRASHCAN_DICT 3
1157#define Py_TRASHCAN_FRAME 4
1158#define Py_TRASHCAN_TRACEBACK 5
1159/* extend here if other objects want protection */
1160
Guido van Rossumd724b232000-03-13 16:01:29 +00001161int _PyTrash_delete_nesting = 0;
Guido van Rossume92e6102000-04-24 15:40:53 +00001162
Guido van Rossumd724b232000-03-13 16:01:29 +00001163PyObject * _PyTrash_delete_later = NULL;
1164
1165void
Fred Drake100814d2000-07-09 15:48:49 +00001166_PyTrash_deposit_object(PyObject *op)
Guido van Rossumd724b232000-03-13 16:01:29 +00001167{
Guido van Rossume92e6102000-04-24 15:40:53 +00001168 int typecode;
Guido van Rossum13ff8eb2000-03-25 18:39:19 +00001169
Guido van Rossume92e6102000-04-24 15:40:53 +00001170 if (PyTuple_Check(op))
1171 typecode = Py_TRASHCAN_TUPLE;
1172 else if (PyList_Check(op))
1173 typecode = Py_TRASHCAN_LIST;
1174 else if (PyDict_Check(op))
1175 typecode = Py_TRASHCAN_DICT;
1176 else if (PyFrame_Check(op))
1177 typecode = Py_TRASHCAN_FRAME;
1178 else if (PyTraceBack_Check(op))
1179 typecode = Py_TRASHCAN_TRACEBACK;
Moshe Zadkacf703f02000-08-04 15:36:13 +00001180 else /* We have a bug here -- those are the only types in GC */ {
1181 Py_FatalError("Type not supported in GC -- internal bug");
1182 return; /* pacify compiler -- execution never here */
1183 }
Guido van Rossume92e6102000-04-24 15:40:53 +00001184 op->ob_refcnt = typecode;
Guido van Rossumd724b232000-03-13 16:01:29 +00001185
Guido van Rossume92e6102000-04-24 15:40:53 +00001186 op->ob_type = (PyTypeObject*)_PyTrash_delete_later;
1187 _PyTrash_delete_later = op;
Guido van Rossumd724b232000-03-13 16:01:29 +00001188}
1189
1190void
Fred Drake100814d2000-07-09 15:48:49 +00001191_PyTrash_destroy_chain(void)
Guido van Rossumd724b232000-03-13 16:01:29 +00001192{
1193 while (_PyTrash_delete_later) {
1194 PyObject *shredder = _PyTrash_delete_later;
Guido van Rossume92e6102000-04-24 15:40:53 +00001195 _PyTrash_delete_later = (PyObject*) shredder->ob_type;
1196
1197 switch (shredder->ob_refcnt) {
1198 case Py_TRASHCAN_TUPLE:
1199 shredder->ob_type = &PyTuple_Type;
1200 break;
1201 case Py_TRASHCAN_LIST:
1202 shredder->ob_type = &PyList_Type;
1203 break;
1204 case Py_TRASHCAN_DICT:
1205 shredder->ob_type = &PyDict_Type;
1206 break;
1207 case Py_TRASHCAN_FRAME:
1208 shredder->ob_type = &PyFrame_Type;
1209 break;
1210 case Py_TRASHCAN_TRACEBACK:
1211 shredder->ob_type = &PyTraceBack_Type;
1212 break;
1213 }
1214 _Py_NewReference(shredder);
1215
Guido van Rossumd724b232000-03-13 16:01:29 +00001216 ++_PyTrash_delete_nesting;
1217 Py_DECREF(shredder);
1218 --_PyTrash_delete_nesting;
1219 }
1220}