blob: a7df5260724c620530165b215e5eb20ab4effc6e [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001/***********************************************************
Guido van Rossumfd71b9e2000-06-30 23:50:40 +00002Copyright (c) 2000, BeOpen.com.
3Copyright (c) 1995-2000, Corporation for National Research Initiatives.
4Copyright (c) 1990-1995, Stichting Mathematisch Centrum.
5All rights reserved.
Guido van Rossumf70e43a1991-02-19 12:39:46 +00006
Guido van Rossumfd71b9e2000-06-30 23:50:40 +00007See the file "Misc/COPYRIGHT" for information on usage and
8redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES.
Guido van Rossumf70e43a1991-02-19 12:39:46 +00009******************************************************************/
10
Guido van Rossum3f5da241990-12-20 15:06:42 +000011/* Generic object operations; and implementation of None (NoObject) */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000012
Guido van Rossumc0b618a1997-05-02 03:12:38 +000013#include "Python.h"
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000014
Jack Jansen28fc8802000-07-11 21:47:20 +000015#ifdef macintosh
16#include "macglue.h"
17#endif
18
Guido van Rossume92e6102000-04-24 15:40:53 +000019/* just for trashcan: */
20#include "compile.h"
21#include "frameobject.h"
22#include "traceback.h"
23
Guido van Rossum6f9e4331995-03-29 16:57:48 +000024#if defined( Py_TRACE_REFS ) || defined( Py_REF_DEBUG )
Guido van Rossumbffd6832000-01-20 22:32:56 +000025DL_IMPORT(long) _Py_RefTotal;
Guido van Rossum3f5da241990-12-20 15:06:42 +000026#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000027
Guido van Rossum3f5da241990-12-20 15:06:42 +000028/* Object allocation routines used by NEWOBJ and NEWVAROBJ macros.
29 These are used by the individual routines for object creation.
30 Do not call them otherwise, they do not initialize the object! */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000031
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +000032#ifdef COUNT_ALLOCS
Guido van Rossumc0b618a1997-05-02 03:12:38 +000033static PyTypeObject *type_list;
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +000034extern int tuple_zero_allocs, fast_tuple_allocs;
35extern int quick_int_allocs, quick_neg_int_allocs;
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +000036extern int null_strings, one_strings;
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +000037void
Fred Drake100814d2000-07-09 15:48:49 +000038dump_counts(void)
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +000039{
Guido van Rossumc0b618a1997-05-02 03:12:38 +000040 PyTypeObject *tp;
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +000041
42 for (tp = type_list; tp; tp = tp->tp_next)
Sjoerd Mullender52c1f511993-10-25 08:40:52 +000043 fprintf(stderr, "%s alloc'd: %d, freed: %d, max in use: %d\n",
44 tp->tp_name, tp->tp_alloc, tp->tp_free,
45 tp->tp_maxalloc);
46 fprintf(stderr, "fast tuple allocs: %d, empty: %d\n",
47 fast_tuple_allocs, tuple_zero_allocs);
48 fprintf(stderr, "fast int allocs: pos: %d, neg: %d\n",
49 quick_int_allocs, quick_neg_int_allocs);
50 fprintf(stderr, "null strings: %d, 1-strings: %d\n",
51 null_strings, one_strings);
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +000052}
53
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +000054PyObject *
Fred Drake100814d2000-07-09 15:48:49 +000055get_counts(void)
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +000056{
57 PyTypeObject *tp;
58 PyObject *result;
59 PyObject *v;
60
61 result = PyList_New(0);
62 if (result == NULL)
63 return NULL;
64 for (tp = type_list; tp; tp = tp->tp_next) {
65 v = Py_BuildValue("(siii)", tp->tp_name, tp->tp_alloc,
66 tp->tp_free, tp->tp_maxalloc);
67 if (v == NULL) {
68 Py_DECREF(result);
69 return NULL;
70 }
71 if (PyList_Append(result, v) < 0) {
72 Py_DECREF(v);
73 Py_DECREF(result);
74 return NULL;
75 }
76 Py_DECREF(v);
77 }
78 return result;
79}
80
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +000081void
Fred Drake100814d2000-07-09 15:48:49 +000082inc_count(PyTypeObject *tp)
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +000083{
84 if (tp->tp_alloc == 0) {
Guido van Rossumd8953cb1995-04-06 14:46:26 +000085 /* first time; insert in linked list */
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +000086 if (tp->tp_next != NULL) /* sanity check */
Guido van Rossumc0b618a1997-05-02 03:12:38 +000087 Py_FatalError("XXX inc_count sanity check");
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +000088 tp->tp_next = type_list;
89 type_list = tp;
90 }
91 tp->tp_alloc++;
92 if (tp->tp_alloc - tp->tp_free > tp->tp_maxalloc)
93 tp->tp_maxalloc = tp->tp_alloc - tp->tp_free;
94}
95#endif
96
Guido van Rossumc0b618a1997-05-02 03:12:38 +000097PyObject *
Fred Drake100814d2000-07-09 15:48:49 +000098PyObject_Init(PyObject *op, PyTypeObject *tp)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000099{
Guido van Rossumb18618d2000-05-03 23:44:39 +0000100 if (op == NULL) {
101 PyErr_SetString(PyExc_SystemError,
102 "NULL object passed to PyObject_Init");
103 return op;
104 }
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000105#ifdef WITH_CYCLE_GC
106 if (PyType_IS_GC(tp))
107 op = (PyObject *) PyObject_FROM_GC(op);
108#endif
Guido van Rossumb18618d2000-05-03 23:44:39 +0000109 /* Any changes should be reflected in PyObject_INIT (objimpl.h) */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000110 op->ob_type = tp;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000111 _Py_NewReference(op);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000112 return op;
113}
114
Guido van Rossumb18618d2000-05-03 23:44:39 +0000115PyVarObject *
Fred Drake100814d2000-07-09 15:48:49 +0000116PyObject_InitVar(PyVarObject *op, PyTypeObject *tp, int size)
Guido van Rossumb18618d2000-05-03 23:44:39 +0000117{
118 if (op == NULL) {
119 PyErr_SetString(PyExc_SystemError,
120 "NULL object passed to PyObject_InitVar");
121 return op;
122 }
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000123#ifdef WITH_CYCLE_GC
124 if (PyType_IS_GC(tp))
125 op = (PyVarObject *) PyObject_FROM_GC(op);
126#endif
Guido van Rossumb18618d2000-05-03 23:44:39 +0000127 /* Any changes should be reflected in PyObject_INIT_VAR */
128 op->ob_size = size;
129 op->ob_type = tp;
130 _Py_NewReference((PyObject *)op);
131 return op;
132}
133
134PyObject *
Fred Drake100814d2000-07-09 15:48:49 +0000135_PyObject_New(PyTypeObject *tp)
Guido van Rossumb18618d2000-05-03 23:44:39 +0000136{
137 PyObject *op;
138 op = (PyObject *) PyObject_MALLOC(_PyObject_SIZE(tp));
139 if (op == NULL)
140 return PyErr_NoMemory();
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000141#ifdef WITH_CYCLE_GC
142 if (PyType_IS_GC(tp))
143 op = (PyObject *) PyObject_FROM_GC(op);
144#endif
Guido van Rossumb18618d2000-05-03 23:44:39 +0000145 return PyObject_INIT(op, tp);
146}
147
Guido van Rossumd0c87ee1997-05-15 21:31:03 +0000148PyVarObject *
Fred Drake100814d2000-07-09 15:48:49 +0000149_PyObject_NewVar(PyTypeObject *tp, int size)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000150{
Guido van Rossumb18618d2000-05-03 23:44:39 +0000151 PyVarObject *op;
152 op = (PyVarObject *) PyObject_MALLOC(_PyObject_VAR_SIZE(tp, size));
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000153 if (op == NULL)
Guido van Rossumd0c87ee1997-05-15 21:31:03 +0000154 return (PyVarObject *)PyErr_NoMemory();
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000155#ifdef WITH_CYCLE_GC
156 if (PyType_IS_GC(tp))
157 op = (PyVarObject *) PyObject_FROM_GC(op);
158#endif
Guido van Rossumb18618d2000-05-03 23:44:39 +0000159 return PyObject_INIT_VAR(op, tp, size);
160}
161
162void
Fred Drake100814d2000-07-09 15:48:49 +0000163_PyObject_Del(PyObject *op)
Guido van Rossumb18618d2000-05-03 23:44:39 +0000164{
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000165#ifdef WITH_CYCLE_GC
Guido van Rossum4cc6ac72000-07-01 01:00:38 +0000166 if (op && PyType_IS_GC(op->ob_type)) {
167 op = (PyObject *) PyObject_AS_GC(op);
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000168 }
Guido van Rossum4cc6ac72000-07-01 01:00:38 +0000169#endif
170 PyObject_FREE(op);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000171}
172
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000173#ifndef WITH_CYCLE_GC
174/* extension modules might need these */
175void _PyGC_Insert(PyObject *op) { }
176void _PyGC_Remove(PyObject *op) { }
177#endif
178
Guido van Rossum90933611991-06-07 16:10:43 +0000179int
Fred Drake100814d2000-07-09 15:48:49 +0000180PyObject_Print(PyObject *op, FILE *fp, int flags)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000181{
Guido van Rossum278ef591991-07-27 21:40:24 +0000182 int ret = 0;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000183 if (PyErr_CheckSignals())
Guido van Rossum90933611991-06-07 16:10:43 +0000184 return -1;
Guido van Rossum9b00dfa1998-04-28 16:06:54 +0000185#ifdef USE_STACKCHECK
186 if (PyOS_CheckStack()) {
187 PyErr_SetString(PyExc_MemoryError, "Stack overflow");
188 return -1;
189 }
190#endif
Guido van Rossum687ef6e2000-01-12 16:28:58 +0000191 clearerr(fp); /* Clear any previous error condition */
Guido van Rossum90933611991-06-07 16:10:43 +0000192 if (op == NULL) {
193 fprintf(fp, "<nil>");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000194 }
Guido van Rossum90933611991-06-07 16:10:43 +0000195 else {
196 if (op->ob_refcnt <= 0)
Fred Drakea44d3532000-06-30 15:01:00 +0000197 fprintf(fp, "<refcnt %u at %p>",
198 op->ob_refcnt, op);
Guido van Rossum2e8f6141992-09-03 20:32:55 +0000199 else if (op->ob_type->tp_print == NULL) {
200 if (op->ob_type->tp_repr == NULL) {
Fred Drakea44d3532000-06-30 15:01:00 +0000201 fprintf(fp, "<%s object at %p>",
202 op->ob_type->tp_name, op);
Guido van Rossum2e8f6141992-09-03 20:32:55 +0000203 }
204 else {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000205 PyObject *s;
206 if (flags & Py_PRINT_RAW)
207 s = PyObject_Str(op);
Guido van Rossumc6004111993-11-05 10:22:19 +0000208 else
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000209 s = PyObject_Repr(op);
Guido van Rossum2e8f6141992-09-03 20:32:55 +0000210 if (s == NULL)
211 ret = -1;
Guido van Rossum2e8f6141992-09-03 20:32:55 +0000212 else {
Guido van Rossum565798d1998-04-21 22:25:01 +0000213 ret = PyObject_Print(s, fp,
214 Py_PRINT_RAW);
Guido van Rossum2e8f6141992-09-03 20:32:55 +0000215 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000216 Py_XDECREF(s);
Guido van Rossum2e8f6141992-09-03 20:32:55 +0000217 }
218 }
Guido van Rossum90933611991-06-07 16:10:43 +0000219 else
Guido van Rossum278ef591991-07-27 21:40:24 +0000220 ret = (*op->ob_type->tp_print)(op, fp, flags);
Guido van Rossum90933611991-06-07 16:10:43 +0000221 }
Guido van Rossum278ef591991-07-27 21:40:24 +0000222 if (ret == 0) {
223 if (ferror(fp)) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000224 PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossum278ef591991-07-27 21:40:24 +0000225 clearerr(fp);
226 ret = -1;
227 }
228 }
229 return ret;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000230}
231
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000232PyObject *
Fred Drake100814d2000-07-09 15:48:49 +0000233PyObject_Repr(PyObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000234{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000235 if (PyErr_CheckSignals())
Guido van Rossum90933611991-06-07 16:10:43 +0000236 return NULL;
Guido van Rossum9b00dfa1998-04-28 16:06:54 +0000237#ifdef USE_STACKCHECK
238 if (PyOS_CheckStack()) {
239 PyErr_SetString(PyExc_MemoryError, "Stack overflow");
240 return NULL;
241 }
242#endif
Guido van Rossum90933611991-06-07 16:10:43 +0000243 if (v == NULL)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000244 return PyString_FromString("<NULL>");
Guido van Rossum90933611991-06-07 16:10:43 +0000245 else if (v->ob_type->tp_repr == NULL) {
246 char buf[120];
Fred Drakea44d3532000-06-30 15:01:00 +0000247 sprintf(buf, "<%.80s object at %p>",
248 v->ob_type->tp_name, v);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000249 return PyString_FromString(buf);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000250 }
Guido van Rossum4c08d552000-03-10 22:55:18 +0000251 else {
252 PyObject *res;
253 res = (*v->ob_type->tp_repr)(v);
254 if (res == NULL)
255 return NULL;
Fredrik Lundhefecc7d2000-07-01 14:31:09 +0000256 if (PyUnicode_Check(res)) {
257 PyObject* str;
Fredrik Lundh2a1e0602000-07-08 17:43:32 +0000258 str = PyUnicode_AsUnicodeEscapeString(res);
Marc-André Lemburg891bc652000-07-03 09:57:53 +0000259 Py_DECREF(res);
260 if (str)
Fredrik Lundhefecc7d2000-07-01 14:31:09 +0000261 res = str;
Marc-André Lemburg891bc652000-07-03 09:57:53 +0000262 else
263 return NULL;
Fredrik Lundhefecc7d2000-07-01 14:31:09 +0000264 }
Guido van Rossum4c08d552000-03-10 22:55:18 +0000265 if (!PyString_Check(res)) {
266 PyErr_Format(PyExc_TypeError,
Guido van Rossum5db862d2000-04-10 12:46:51 +0000267 "__repr__ returned non-string (type %.200s)",
Guido van Rossum4c08d552000-03-10 22:55:18 +0000268 res->ob_type->tp_name);
269 Py_DECREF(res);
270 return NULL;
271 }
272 return res;
273 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000274}
275
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000276PyObject *
Fred Drake100814d2000-07-09 15:48:49 +0000277PyObject_Str(PyObject *v)
Guido van Rossumc6004111993-11-05 10:22:19 +0000278{
Guido van Rossum4c08d552000-03-10 22:55:18 +0000279 PyObject *res;
280
Guido van Rossumc6004111993-11-05 10:22:19 +0000281 if (v == NULL)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000282 return PyString_FromString("<NULL>");
283 else if (PyString_Check(v)) {
284 Py_INCREF(v);
Guido van Rossumc6004111993-11-05 10:22:19 +0000285 return v;
286 }
Guido van Rossum32b582b1995-01-17 16:35:13 +0000287 else if (v->ob_type->tp_str != NULL)
Guido van Rossum4c08d552000-03-10 22:55:18 +0000288 res = (*v->ob_type->tp_str)(v);
Guido van Rossumc6004111993-11-05 10:22:19 +0000289 else {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000290 PyObject *func;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000291 if (!PyInstance_Check(v) ||
292 (func = PyObject_GetAttrString(v, "__str__")) == NULL) {
293 PyErr_Clear();
294 return PyObject_Repr(v);
Guido van Rossumc6004111993-11-05 10:22:19 +0000295 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000296 res = PyEval_CallObject(func, (PyObject *)NULL);
297 Py_DECREF(func);
Guido van Rossumc6004111993-11-05 10:22:19 +0000298 }
Guido van Rossum4c08d552000-03-10 22:55:18 +0000299 if (res == NULL)
300 return NULL;
Marc-André Lemburg891bc652000-07-03 09:57:53 +0000301 if (PyUnicode_Check(res)) {
302 PyObject* str;
303 str = PyUnicode_AsEncodedString(res, NULL, NULL);
304 Py_DECREF(res);
305 if (str)
306 res = str;
307 else
308 return NULL;
309 }
Guido van Rossum4c08d552000-03-10 22:55:18 +0000310 if (!PyString_Check(res)) {
311 PyErr_Format(PyExc_TypeError,
Guido van Rossum5db862d2000-04-10 12:46:51 +0000312 "__str__ returned non-string (type %.200s)",
Guido van Rossum4c08d552000-03-10 22:55:18 +0000313 res->ob_type->tp_name);
314 Py_DECREF(res);
315 return NULL;
316 }
317 return res;
Guido van Rossumc6004111993-11-05 10:22:19 +0000318}
319
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000320static PyObject *
Fred Drake100814d2000-07-09 15:48:49 +0000321do_cmp(PyObject *v, PyObject *w)
Guido van Rossum20566841995-01-12 11:26:10 +0000322{
Guido van Rossumc8b6df91997-05-23 00:06:51 +0000323 long c;
Guido van Rossum20566841995-01-12 11:26:10 +0000324 /* __rcmp__ actually won't be called unless __cmp__ isn't defined,
325 because the check in cmpobject() reverses the objects first.
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000326 This is intentional -- it makes no sense to define cmp(x,y)
327 different than -cmp(y,x). */
328 if (PyInstance_Check(v) || PyInstance_Check(w))
329 return PyInstance_DoBinOp(v, w, "__cmp__", "__rcmp__", do_cmp);
Guido van Rossumc8b6df91997-05-23 00:06:51 +0000330 c = PyObject_Compare(v, w);
331 if (c && PyErr_Occurred())
332 return NULL;
333 return PyInt_FromLong(c);
Guido van Rossum20566841995-01-12 11:26:10 +0000334}
335
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000336PyObject *_PyCompareState_Key;
337
Thomas Wouters7e474022000-07-16 12:04:32 +0000338/* _PyCompareState_nesting is incremented before calling compare (for
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000339 some types) and decremented on exit. If the count exceeds the
340 nesting limit, enable code to detect circular data structures.
341*/
342#define NESTING_LIMIT 500
343int _PyCompareState_nesting = 0;
344
345static PyObject*
Fred Drake100814d2000-07-09 15:48:49 +0000346get_inprogress_dict(void)
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000347{
348 PyObject *tstate_dict, *inprogress;
349
350 tstate_dict = PyThreadState_GetDict();
351 if (tstate_dict == NULL) {
352 PyErr_BadInternalCall();
353 return NULL;
354 }
355 inprogress = PyDict_GetItem(tstate_dict, _PyCompareState_Key);
356 if (inprogress == NULL) {
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000357 inprogress = PyDict_New();
358 if (inprogress == NULL)
359 return NULL;
360 if (PyDict_SetItem(tstate_dict, _PyCompareState_Key,
361 inprogress) == -1) {
362 Py_DECREF(inprogress);
363 return NULL;
364 }
Jeremy Hyltona251ea02000-06-09 16:20:39 +0000365 Py_DECREF(inprogress);
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000366 }
367 return inprogress;
368}
369
370static PyObject *
Fred Drake100814d2000-07-09 15:48:49 +0000371make_pair(PyObject *v, PyObject *w)
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000372{
373 PyObject *pair;
374
375 pair = PyTuple_New(2);
376 if (pair == NULL) {
377 return NULL;
378 }
Guido van Rossumad89bbc2000-06-28 21:57:18 +0000379 if (v <= w) {
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
Guido van Rossumc8b6df91997-05-23 00:06:51 +0000395 if (v == NULL || w == NULL) {
396 PyErr_BadInternalCall();
397 return -1;
398 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000399 if (v == w)
400 return 0;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000401 if (PyInstance_Check(v) || PyInstance_Check(w)) {
402 PyObject *res;
Guido van Rossum20566841995-01-12 11:26:10 +0000403 int c;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000404 if (!PyInstance_Check(v))
405 return -PyObject_Compare(w, v);
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000406 if (++_PyCompareState_nesting > NESTING_LIMIT) {
407 PyObject *inprogress, *pair;
408
409 inprogress = get_inprogress_dict();
410 if (inprogress == NULL) {
411 return -1;
412 }
413 pair = make_pair(v, w);
414 if (PyDict_GetItem(inprogress, pair)) {
415 /* already comparing these objects. assume
416 they're equal until shown otherwise */
417 Py_DECREF(pair);
418 --_PyCompareState_nesting;
419 return 0;
420 }
421 if (PyDict_SetItem(inprogress, pair, pair) == -1) {
422 return -1;
423 }
424 res = do_cmp(v, w);
425 _PyCompareState_nesting--;
426 /* XXX DelItem shouldn't fail */
427 PyDict_DelItem(inprogress, pair);
428 Py_DECREF(pair);
429 } else {
430 res = do_cmp(v, w);
431 }
Guido van Rossumc8b6df91997-05-23 00:06:51 +0000432 if (res == NULL)
433 return -1;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000434 if (!PyInt_Check(res)) {
435 Py_DECREF(res);
Guido van Rossumc8b6df91997-05-23 00:06:51 +0000436 PyErr_SetString(PyExc_TypeError,
437 "comparison did not return an int");
438 return -1;
Guido van Rossum20566841995-01-12 11:26:10 +0000439 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000440 c = PyInt_AsLong(res);
441 Py_DECREF(res);
Guido van Rossum20566841995-01-12 11:26:10 +0000442 return (c < 0) ? -1 : (c > 0) ? 1 : 0;
443 }
Guido van Rossumcd5a5f61998-06-09 18:58:44 +0000444 if ((vtp = v->ob_type) != (wtp = w->ob_type)) {
445 char *vname = vtp->tp_name;
446 char *wname = wtp->tp_name;
Guido van Rossumb4db1941998-07-21 21:56:41 +0000447 if (vtp->tp_as_number != NULL && wtp->tp_as_number != NULL) {
Guido van Rossum242c6421997-11-19 16:03:17 +0000448 int err;
449 err = PyNumber_CoerceEx(&v, &w);
450 if (err < 0)
Guido van Rossumc8b6df91997-05-23 00:06:51 +0000451 return -1;
Guido van Rossum242c6421997-11-19 16:03:17 +0000452 else if (err == 0) {
Guido van Rossumb4db1941998-07-21 21:56:41 +0000453 int cmp;
454 vtp = v->ob_type;
455 if (vtp->tp_compare == NULL)
456 cmp = (v < w) ? -1 : 1;
457 else
458 cmp = (*vtp->tp_compare)(v, w);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000459 Py_DECREF(v);
460 Py_DECREF(w);
Guido van Rossum9fb03681991-07-01 18:48:04 +0000461 return cmp;
462 }
463 }
Guido van Rossumb244f692000-04-10 13:42:33 +0000464 else if (PyUnicode_Check(v) || PyUnicode_Check(w)) {
465 int result = PyUnicode_Compare(v, w);
466 if (result == -1 && PyErr_Occurred() &&
467 PyErr_ExceptionMatches(PyExc_TypeError))
468 /* TypeErrors are ignored: if Unicode coercion
469 fails due to one of the arguments not
470 having the right type, we continue as
471 defined by the coercion protocol (see
472 above). Luckily, decoding errors are
473 reported as ValueErrors and are not masked
474 by this technique. */
475 PyErr_Clear();
476 else
477 return result;
478 }
Guido van Rossumcd5a5f61998-06-09 18:58:44 +0000479 else if (vtp->tp_as_number != NULL)
480 vname = "";
481 else if (wtp->tp_as_number != NULL)
482 wname = "";
483 /* Numerical types compare smaller than all other types */
484 return strcmp(vname, wname);
Guido van Rossum9fb03681991-07-01 18:48:04 +0000485 }
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000486 if (vtp->tp_compare == NULL) {
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000487 return (v < w) ? -1 : 1;
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000488 }
489 if (++_PyCompareState_nesting > NESTING_LIMIT
490 && (vtp->tp_as_mapping
491 || (vtp->tp_as_sequence && !PyString_Check(v)))) {
492 PyObject *inprogress, *pair;
493
494 inprogress = get_inprogress_dict();
495 if (inprogress == NULL) {
496 return -1;
497 }
498 pair = make_pair(v, w);
499 if (PyDict_GetItem(inprogress, pair)) {
500 /* already comparing these objects. assume
501 they're equal until shown otherwise */
502 _PyCompareState_nesting--;
503 Py_DECREF(pair);
504 return 0;
505 }
506 if (PyDict_SetItem(inprogress, pair, pair) == -1) {
507 return -1;
508 }
509 result = (*vtp->tp_compare)(v, w);
510 _PyCompareState_nesting--;
511 PyDict_DelItem(inprogress, pair); /* XXX shouldn't fail */
512 Py_DECREF(pair);
513 } else {
514 result = (*vtp->tp_compare)(v, w);
515 }
516 return result;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000517}
518
Fred Drake13634cf2000-06-29 19:17:04 +0000519
520/* Set of hash utility functions to help maintaining the invariant that
521 iff a==b then hash(a)==hash(b)
522
523 All the utility functions (_Py_Hash*()) return "-1" to signify an error.
524*/
525
526long
Fred Drake100814d2000-07-09 15:48:49 +0000527_Py_HashDouble(double v)
Fred Drake13634cf2000-06-29 19:17:04 +0000528{
529 /* Use frexp to get at the bits in the double.
530 * Since the VAX D double format has 56 mantissa bits, which is the
531 * most of any double format in use, each of these parts may have as
532 * many as (but no more than) 56 significant bits.
533 * So, assuming sizeof(long) >= 4, each part can be broken into two longs;
534 * frexp and multiplication are used to do that.
535 * Also, since the Cray double format has 15 exponent bits, which is the
536 * most of any double format in use, shifting the exponent field left by
537 * 15 won't overflow a long (again assuming sizeof(long) >= 4).
538 */
539 int expo;
540 long hipart;
541
542 v = frexp(v, &expo);
543 v = v * 2147483648.0; /* 2**31 */
544 hipart = (long)v; /* Take the top 32 bits */
545 v = (v - (double)hipart) * 2147483648.0; /* Get the next 32 bits */
546
547 return hipart + (long)v + (expo << 15); /* Combine everything */
548}
549
550long
Fred Drake100814d2000-07-09 15:48:49 +0000551_Py_HashPointer(void *p)
Fred Drake13634cf2000-06-29 19:17:04 +0000552{
553#if SIZEOF_LONG >= SIZEOF_VOID_P
554 return (long)p;
555#else
556 /* convert to a Python long and hash that */
557 PyObject* longobj;
558 long x;
559
560 if ((longobj = PyLong_FromVoidPtr(p)) == NULL) {
561 x = -1;
562 goto finally;
563 }
564 x = PyObject_Hash(longobj);
565
566finally:
567 Py_XDECREF(longobj);
568 return x;
569#endif
570}
571
572
Guido van Rossum9bfef441993-03-29 10:43:31 +0000573long
Fred Drake100814d2000-07-09 15:48:49 +0000574PyObject_Hash(PyObject *v)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000575{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000576 PyTypeObject *tp = v->ob_type;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000577 if (tp->tp_hash != NULL)
578 return (*tp->tp_hash)(v);
Fred Drake13634cf2000-06-29 19:17:04 +0000579 if (tp->tp_compare == NULL) {
580 return _Py_HashPointer(v); /* Use address as hash value */
581 }
Guido van Rossum9bfef441993-03-29 10:43:31 +0000582 /* If there's a cmp but no hash defined, the object can't be hashed */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000583 PyErr_SetString(PyExc_TypeError, "unhashable type");
Guido van Rossum9bfef441993-03-29 10:43:31 +0000584 return -1;
585}
586
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000587PyObject *
Fred Drake100814d2000-07-09 15:48:49 +0000588PyObject_GetAttrString(PyObject *v, char *name)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000589{
Guido van Rossumd8eb1b31996-08-09 20:52:03 +0000590 if (v->ob_type->tp_getattro != NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000591 PyObject *w, *res;
Guido van Rossumc6d06701997-01-18 07:57:16 +0000592 w = PyString_InternFromString(name);
Guido van Rossumd8eb1b31996-08-09 20:52:03 +0000593 if (w == NULL)
594 return NULL;
595 res = (*v->ob_type->tp_getattro)(v, w);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000596 Py_XDECREF(w);
Guido van Rossumd8eb1b31996-08-09 20:52:03 +0000597 return res;
598 }
599
Guido van Rossum3f5da241990-12-20 15:06:42 +0000600 if (v->ob_type->tp_getattr == NULL) {
Guido van Rossumdb935161998-01-19 22:16:36 +0000601 PyErr_Format(PyExc_AttributeError,
602 "'%.50s' object has no attribute '%.400s'",
603 v->ob_type->tp_name,
604 name);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000605 return NULL;
606 }
607 else {
608 return (*v->ob_type->tp_getattr)(v, name);
609 }
610}
611
612int
Fred Drake100814d2000-07-09 15:48:49 +0000613PyObject_HasAttrString(PyObject *v, char *name)
Guido van Rossumed18fdc1993-07-11 19:55:34 +0000614{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000615 PyObject *res = PyObject_GetAttrString(v, name);
Guido van Rossumed18fdc1993-07-11 19:55:34 +0000616 if (res != NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000617 Py_DECREF(res);
Guido van Rossumed18fdc1993-07-11 19:55:34 +0000618 return 1;
619 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000620 PyErr_Clear();
Guido van Rossumed18fdc1993-07-11 19:55:34 +0000621 return 0;
622}
623
624int
Fred Drake100814d2000-07-09 15:48:49 +0000625PyObject_SetAttrString(PyObject *v, char *name, PyObject *w)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000626{
Guido van Rossumd8eb1b31996-08-09 20:52:03 +0000627 if (v->ob_type->tp_setattro != NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000628 PyObject *s;
Guido van Rossumd8eb1b31996-08-09 20:52:03 +0000629 int res;
Guido van Rossumc6d06701997-01-18 07:57:16 +0000630 s = PyString_InternFromString(name);
Guido van Rossumd8eb1b31996-08-09 20:52:03 +0000631 if (s == NULL)
Guido van Rossumb7fc3041996-09-11 22:51:25 +0000632 return -1;
Guido van Rossumd8eb1b31996-08-09 20:52:03 +0000633 res = (*v->ob_type->tp_setattro)(v, s, w);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000634 Py_XDECREF(s);
Guido van Rossumd8eb1b31996-08-09 20:52:03 +0000635 return res;
636 }
637
Guido van Rossum3f5da241990-12-20 15:06:42 +0000638 if (v->ob_type->tp_setattr == NULL) {
639 if (v->ob_type->tp_getattr == NULL)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000640 PyErr_SetString(PyExc_TypeError,
Guido van Rossum3ea74121991-12-24 13:28:03 +0000641 "attribute-less object (assign or del)");
Guido van Rossum3f5da241990-12-20 15:06:42 +0000642 else
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000643 PyErr_SetString(PyExc_TypeError,
Guido van Rossum3ea74121991-12-24 13:28:03 +0000644 "object has read-only attributes");
Guido van Rossum73531a31990-12-20 23:12:40 +0000645 return -1;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000646 }
647 else {
648 return (*v->ob_type->tp_setattr)(v, name, w);
649 }
650}
651
Guido van Rossum98ff96a1997-05-20 18:34:44 +0000652PyObject *
Fred Drake100814d2000-07-09 15:48:49 +0000653PyObject_GetAttr(PyObject *v, PyObject *name)
Guido van Rossum98ff96a1997-05-20 18:34:44 +0000654{
655 if (v->ob_type->tp_getattro != NULL)
656 return (*v->ob_type->tp_getattro)(v, name);
Jeremy Hylton99a8f902000-06-23 14:36:32 +0000657
658 if (!PyString_Check(name)) {
659 PyErr_SetString(PyExc_TypeError,
660 "attribute name must be string");
661 return NULL;
662 }
663 return PyObject_GetAttrString(v, PyString_AS_STRING(name));
Guido van Rossum98ff96a1997-05-20 18:34:44 +0000664}
665
666int
Fred Drake100814d2000-07-09 15:48:49 +0000667PyObject_HasAttr(PyObject *v, PyObject *name)
Guido van Rossum98ff96a1997-05-20 18:34:44 +0000668{
669 PyObject *res = PyObject_GetAttr(v, name);
670 if (res != NULL) {
671 Py_DECREF(res);
672 return 1;
673 }
674 PyErr_Clear();
675 return 0;
676}
677
678int
Fred Drake100814d2000-07-09 15:48:49 +0000679PyObject_SetAttr(PyObject *v, PyObject *name, PyObject *value)
Guido van Rossum98ff96a1997-05-20 18:34:44 +0000680{
681 int err;
682 Py_INCREF(name);
Jeremy Hylton99a8f902000-06-23 14:36:32 +0000683 if (PyString_Check(name))
684 PyString_InternInPlace(&name);
Guido van Rossum98ff96a1997-05-20 18:34:44 +0000685 if (v->ob_type->tp_setattro != NULL)
686 err = (*v->ob_type->tp_setattro)(v, name, value);
Jeremy Hylton99a8f902000-06-23 14:36:32 +0000687 else if (PyString_Check(name)) {
Guido van Rossum98ff96a1997-05-20 18:34:44 +0000688 err = PyObject_SetAttrString(
Jeremy Hylton99a8f902000-06-23 14:36:32 +0000689 v, PyString_AS_STRING(name), value);
690 }
691 else {
692 PyErr_SetString(PyExc_TypeError,
693 "attribute name must be string");
694 err = -1;
695 }
Guido van Rossum98ff96a1997-05-20 18:34:44 +0000696 Py_DECREF(name);
697 return err;
698}
699
Guido van Rossum6ac258d1993-05-12 08:24:20 +0000700/* Test a value used as condition, e.g., in a for or if statement.
701 Return -1 if an error occurred */
702
703int
Fred Drake100814d2000-07-09 15:48:49 +0000704PyObject_IsTrue(PyObject *v)
Guido van Rossum6ac258d1993-05-12 08:24:20 +0000705{
706 int res;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000707 if (v == Py_None)
Guido van Rossum6ac258d1993-05-12 08:24:20 +0000708 res = 0;
Guido van Rossum1c4f4581998-05-22 00:53:24 +0000709 else if (v->ob_type->tp_as_number != NULL &&
710 v->ob_type->tp_as_number->nb_nonzero != NULL)
Guido van Rossum6ac258d1993-05-12 08:24:20 +0000711 res = (*v->ob_type->tp_as_number->nb_nonzero)(v);
Guido van Rossum1c4f4581998-05-22 00:53:24 +0000712 else if (v->ob_type->tp_as_mapping != NULL &&
713 v->ob_type->tp_as_mapping->mp_length != NULL)
Guido van Rossum6ac258d1993-05-12 08:24:20 +0000714 res = (*v->ob_type->tp_as_mapping->mp_length)(v);
Guido van Rossum1c4f4581998-05-22 00:53:24 +0000715 else if (v->ob_type->tp_as_sequence != NULL &&
716 v->ob_type->tp_as_sequence->sq_length != NULL)
Guido van Rossum6ac258d1993-05-12 08:24:20 +0000717 res = (*v->ob_type->tp_as_sequence->sq_length)(v);
718 else
719 res = 1;
720 if (res > 0)
721 res = 1;
722 return res;
723}
724
Guido van Rossumc3d3f961998-04-09 17:53:59 +0000725/* equivalent of 'not v'
726 Return -1 if an error occurred */
727
728int
Fred Drake100814d2000-07-09 15:48:49 +0000729PyObject_Not(PyObject *v)
Guido van Rossumc3d3f961998-04-09 17:53:59 +0000730{
731 int res;
732 res = PyObject_IsTrue(v);
733 if (res < 0)
734 return res;
735 return res == 0;
736}
737
Guido van Rossum5524a591995-01-10 15:26:20 +0000738/* Coerce two numeric types to the "larger" one.
739 Increment the reference count on each argument.
740 Return -1 and raise an exception if no coercion is possible
741 (and then no reference count is incremented).
742*/
743
744int
Fred Drake100814d2000-07-09 15:48:49 +0000745PyNumber_CoerceEx(PyObject **pv, PyObject **pw)
Guido van Rossum5524a591995-01-10 15:26:20 +0000746{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000747 register PyObject *v = *pv;
748 register PyObject *w = *pw;
Guido van Rossum5524a591995-01-10 15:26:20 +0000749 int res;
750
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000751 if (v->ob_type == w->ob_type && !PyInstance_Check(v)) {
752 Py_INCREF(v);
753 Py_INCREF(w);
Guido van Rossum5524a591995-01-10 15:26:20 +0000754 return 0;
755 }
756 if (v->ob_type->tp_as_number && v->ob_type->tp_as_number->nb_coerce) {
757 res = (*v->ob_type->tp_as_number->nb_coerce)(pv, pw);
758 if (res <= 0)
759 return res;
760 }
761 if (w->ob_type->tp_as_number && w->ob_type->tp_as_number->nb_coerce) {
762 res = (*w->ob_type->tp_as_number->nb_coerce)(pw, pv);
763 if (res <= 0)
764 return res;
765 }
Guido van Rossum242c6421997-11-19 16:03:17 +0000766 return 1;
767}
768
769int
Fred Drake100814d2000-07-09 15:48:49 +0000770PyNumber_Coerce(PyObject **pv, PyObject **pw)
Guido van Rossum242c6421997-11-19 16:03:17 +0000771{
772 int err = PyNumber_CoerceEx(pv, pw);
773 if (err <= 0)
774 return err;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000775 PyErr_SetString(PyExc_TypeError, "number coercion failed");
Guido van Rossum5524a591995-01-10 15:26:20 +0000776 return -1;
777}
778
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000779
Guido van Rossum49b11fe1995-01-26 00:38:22 +0000780/* Test whether an object can be called */
781
782int
Fred Drake100814d2000-07-09 15:48:49 +0000783PyCallable_Check(PyObject *x)
Guido van Rossum49b11fe1995-01-26 00:38:22 +0000784{
785 if (x == NULL)
786 return 0;
787 if (x->ob_type->tp_call != NULL ||
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000788 PyFunction_Check(x) ||
789 PyMethod_Check(x) ||
790 PyCFunction_Check(x) ||
791 PyClass_Check(x))
Guido van Rossum49b11fe1995-01-26 00:38:22 +0000792 return 1;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000793 if (PyInstance_Check(x)) {
794 PyObject *call = PyObject_GetAttrString(x, "__call__");
Guido van Rossum49b11fe1995-01-26 00:38:22 +0000795 if (call == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000796 PyErr_Clear();
Guido van Rossum49b11fe1995-01-26 00:38:22 +0000797 return 0;
798 }
799 /* Could test recursively but don't, for fear of endless
800 recursion if some joker sets self.__call__ = self */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000801 Py_DECREF(call);
Guido van Rossum49b11fe1995-01-26 00:38:22 +0000802 return 1;
803 }
804 return 0;
805}
806
807
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000808/*
809NoObject is usable as a non-NULL undefined value, used by the macro None.
810There is (and should be!) no way to create other objects of this type,
Guido van Rossum3f5da241990-12-20 15:06:42 +0000811so there is exactly one (which is indestructible, by the way).
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000812*/
813
Guido van Rossum0c182a11992-03-27 17:26:13 +0000814/* ARGSUSED */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000815static PyObject *
Fred Drake100814d2000-07-09 15:48:49 +0000816none_repr(PyObject *op)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000817{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000818 return PyString_FromString("None");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000819}
820
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000821static PyTypeObject PyNothing_Type = {
822 PyObject_HEAD_INIT(&PyType_Type)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000823 0,
Guido van Rossum3f5da241990-12-20 15:06:42 +0000824 "None",
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000825 0,
826 0,
827 0, /*tp_dealloc*/ /*never called*/
Guido van Rossum7066dd71992-09-17 17:54:56 +0000828 0, /*tp_print*/
Guido van Rossum3f5da241990-12-20 15:06:42 +0000829 0, /*tp_getattr*/
830 0, /*tp_setattr*/
831 0, /*tp_compare*/
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000832 (reprfunc)none_repr, /*tp_repr*/
Guido van Rossum3f5da241990-12-20 15:06:42 +0000833 0, /*tp_as_number*/
834 0, /*tp_as_sequence*/
835 0, /*tp_as_mapping*/
Guido van Rossum9bfef441993-03-29 10:43:31 +0000836 0, /*tp_hash */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000837};
838
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000839PyObject _Py_NoneStruct = {
840 PyObject_HEAD_INIT(&PyNothing_Type)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000841};
842
843
Guido van Rossum84a90321996-05-22 16:34:47 +0000844#ifdef Py_TRACE_REFS
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000845
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000846static PyObject refchain = {&refchain, &refchain};
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000847
Guido van Rossumaacdc9d1996-08-12 21:32:12 +0000848void
Fred Drake100814d2000-07-09 15:48:49 +0000849_Py_ResetReferences(void)
Guido van Rossume09fb551997-08-05 02:04:34 +0000850{
851 refchain._ob_prev = refchain._ob_next = &refchain;
852 _Py_RefTotal = 0;
853}
854
855void
Fred Drake100814d2000-07-09 15:48:49 +0000856_Py_NewReference(PyObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000857{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000858 _Py_RefTotal++;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000859 op->ob_refcnt = 1;
860 op->_ob_next = refchain._ob_next;
861 op->_ob_prev = &refchain;
862 refchain._ob_next->_ob_prev = op;
863 refchain._ob_next = op;
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +0000864#ifdef COUNT_ALLOCS
865 inc_count(op->ob_type);
866#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000867}
868
Guido van Rossumaacdc9d1996-08-12 21:32:12 +0000869void
Fred Drake100814d2000-07-09 15:48:49 +0000870_Py_ForgetReference(register PyObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000871{
Guido van Rossumbffd6832000-01-20 22:32:56 +0000872#ifdef SLOW_UNREF_CHECK
Guido van Rossum4c08d552000-03-10 22:55:18 +0000873 register PyObject *p;
Guido van Rossumbffd6832000-01-20 22:32:56 +0000874#endif
Guido van Rossumd7047b31995-01-02 19:07:15 +0000875 if (op->ob_refcnt < 0)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000876 Py_FatalError("UNREF negative refcnt");
Guido van Rossum2e8f6141992-09-03 20:32:55 +0000877 if (op == &refchain ||
Guido van Rossumd7047b31995-01-02 19:07:15 +0000878 op->_ob_prev->_ob_next != op || op->_ob_next->_ob_prev != op)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000879 Py_FatalError("UNREF invalid object");
Guido van Rossum2e8f6141992-09-03 20:32:55 +0000880#ifdef SLOW_UNREF_CHECK
Guido van Rossum3f5da241990-12-20 15:06:42 +0000881 for (p = refchain._ob_next; p != &refchain; p = p->_ob_next) {
882 if (p == op)
883 break;
884 }
Guido van Rossumd7047b31995-01-02 19:07:15 +0000885 if (p == &refchain) /* Not found */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000886 Py_FatalError("UNREF unknown object");
Guido van Rossum2e8f6141992-09-03 20:32:55 +0000887#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000888 op->_ob_next->_ob_prev = op->_ob_prev;
889 op->_ob_prev->_ob_next = op->_ob_next;
Guido van Rossum2e8f6141992-09-03 20:32:55 +0000890 op->_ob_next = op->_ob_prev = NULL;
Guido van Rossumd8953cb1995-04-06 14:46:26 +0000891#ifdef COUNT_ALLOCS
892 op->ob_type->tp_free++;
893#endif
Guido van Rossum3f5da241990-12-20 15:06:42 +0000894}
895
Guido van Rossumaacdc9d1996-08-12 21:32:12 +0000896void
Fred Drake100814d2000-07-09 15:48:49 +0000897_Py_Dealloc(PyObject *op)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000898{
Guido van Rossum9776adf1994-09-07 14:36:45 +0000899 destructor dealloc = op->ob_type->tp_dealloc;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000900 _Py_ForgetReference(op);
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000901#ifndef WITH_CYCLE_GC
Guido van Rossume92e6102000-04-24 15:40:53 +0000902 if (_PyTrash_delete_nesting < PyTrash_UNWIND_LEVEL-1)
903 op->ob_type = NULL;
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000904#endif
Guido van Rossum9776adf1994-09-07 14:36:45 +0000905 (*dealloc)(op);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000906}
907
Guido van Rossumaacdc9d1996-08-12 21:32:12 +0000908void
Fred Drake100814d2000-07-09 15:48:49 +0000909_Py_PrintReferences(FILE *fp)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000910{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000911 PyObject *op;
Guido van Rossume09fb551997-08-05 02:04:34 +0000912 fprintf(fp, "Remaining objects:\n");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000913 for (op = refchain._ob_next; op != &refchain; op = op->_ob_next) {
914 fprintf(fp, "[%d] ", op->ob_refcnt);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000915 if (PyObject_Print(op, fp, 0) != 0)
916 PyErr_Clear();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000917 putc('\n', fp);
918 }
919}
920
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000921PyObject *
Fred Drake100814d2000-07-09 15:48:49 +0000922_Py_GetObjects(PyObject *self, PyObject *args)
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000923{
924 int i, n;
925 PyObject *t = NULL;
926 PyObject *res, *op;
927
928 if (!PyArg_ParseTuple(args, "i|O", &n, &t))
929 return NULL;
930 op = refchain._ob_next;
931 res = PyList_New(0);
932 if (res == NULL)
933 return NULL;
934 for (i = 0; (n == 0 || i < n) && op != &refchain; i++) {
935 while (op == self || op == args || op == res || op == t ||
936 t != NULL && op->ob_type != (PyTypeObject *) t) {
937 op = op->_ob_next;
938 if (op == &refchain)
939 return res;
940 }
941 if (PyList_Append(res, op) < 0) {
942 Py_DECREF(res);
943 return NULL;
944 }
945 op = op->_ob_next;
946 }
947 return res;
948}
949
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000950#endif
Guido van Rossum97ead3f1996-01-12 01:24:09 +0000951
952
953/* Hack to force loading of cobject.o */
Guido van Rossumda9c2711996-12-05 21:58:58 +0000954PyTypeObject *_Py_cobject_hack = &PyCObject_Type;
Guido van Rossum84a90321996-05-22 16:34:47 +0000955
956
957/* Hack to force loading of abstract.o */
Jeremy Hylton6253f832000-07-12 12:56:19 +0000958int (*_Py_abstract_hack)(PyObject *) = &PyObject_Size;
Guido van Rossume09fb551997-08-05 02:04:34 +0000959
960
Guido van Rossumb18618d2000-05-03 23:44:39 +0000961/* Python's malloc wrappers (see mymalloc.h) */
Guido van Rossume09fb551997-08-05 02:04:34 +0000962
Thomas Wouters334fb892000-07-25 12:56:38 +0000963void *
Fred Drake100814d2000-07-09 15:48:49 +0000964PyMem_Malloc(size_t nbytes)
Guido van Rossume09fb551997-08-05 02:04:34 +0000965{
966#if _PyMem_EXTRA > 0
967 if (nbytes == 0)
968 nbytes = _PyMem_EXTRA;
969#endif
Guido van Rossumb18618d2000-05-03 23:44:39 +0000970 return PyMem_MALLOC(nbytes);
Guido van Rossume09fb551997-08-05 02:04:34 +0000971}
972
Thomas Wouters334fb892000-07-25 12:56:38 +0000973void *
974PyMem_Realloc(void *p, size_t nbytes)
Guido van Rossume09fb551997-08-05 02:04:34 +0000975{
976#if _PyMem_EXTRA > 0
977 if (nbytes == 0)
978 nbytes = _PyMem_EXTRA;
979#endif
Guido van Rossumb18618d2000-05-03 23:44:39 +0000980 return PyMem_REALLOC(p, nbytes);
Guido van Rossume09fb551997-08-05 02:04:34 +0000981}
982
983void
Thomas Wouters334fb892000-07-25 12:56:38 +0000984PyMem_Free(void *p)
Guido van Rossume09fb551997-08-05 02:04:34 +0000985{
Guido van Rossumb18618d2000-05-03 23:44:39 +0000986 PyMem_FREE(p);
987}
988
989
990/* Python's object malloc wrappers (see objimpl.h) */
991
Thomas Wouters334fb892000-07-25 12:56:38 +0000992void *
Fred Drake100814d2000-07-09 15:48:49 +0000993PyObject_Malloc(size_t nbytes)
Guido van Rossumb18618d2000-05-03 23:44:39 +0000994{
995 return PyObject_MALLOC(nbytes);
996}
997
Thomas Wouters334fb892000-07-25 12:56:38 +0000998void *
999PyObject_Realloc(void *p, size_t nbytes)
Guido van Rossumb18618d2000-05-03 23:44:39 +00001000{
1001 return PyObject_REALLOC(p, nbytes);
1002}
1003
1004void
Thomas Wouters334fb892000-07-25 12:56:38 +00001005PyObject_Free(void *p)
Guido van Rossumb18618d2000-05-03 23:44:39 +00001006{
1007 PyObject_FREE(p);
Guido van Rossume09fb551997-08-05 02:04:34 +00001008}
Guido van Rossum86610361998-04-10 22:32:46 +00001009
1010
1011/* These methods are used to control infinite recursion in repr, str, print,
1012 etc. Container objects that may recursively contain themselves,
1013 e.g. builtin dictionaries and lists, should used Py_ReprEnter() and
1014 Py_ReprLeave() to avoid infinite recursion.
1015
1016 Py_ReprEnter() returns 0 the first time it is called for a particular
1017 object and 1 every time thereafter. It returns -1 if an exception
1018 occurred. Py_ReprLeave() has no return value.
1019
1020 See dictobject.c and listobject.c for examples of use.
1021*/
1022
1023#define KEY "Py_Repr"
1024
1025int
Fred Drake100814d2000-07-09 15:48:49 +00001026Py_ReprEnter(PyObject *obj)
Guido van Rossum86610361998-04-10 22:32:46 +00001027{
1028 PyObject *dict;
1029 PyObject *list;
1030 int i;
1031
1032 dict = PyThreadState_GetDict();
1033 if (dict == NULL)
1034 return -1;
1035 list = PyDict_GetItemString(dict, KEY);
1036 if (list == NULL) {
1037 list = PyList_New(0);
1038 if (list == NULL)
1039 return -1;
1040 if (PyDict_SetItemString(dict, KEY, list) < 0)
1041 return -1;
1042 Py_DECREF(list);
1043 }
1044 i = PyList_GET_SIZE(list);
1045 while (--i >= 0) {
1046 if (PyList_GET_ITEM(list, i) == obj)
1047 return 1;
1048 }
1049 PyList_Append(list, obj);
1050 return 0;
1051}
1052
1053void
Fred Drake100814d2000-07-09 15:48:49 +00001054Py_ReprLeave(PyObject *obj)
Guido van Rossum86610361998-04-10 22:32:46 +00001055{
1056 PyObject *dict;
1057 PyObject *list;
1058 int i;
1059
1060 dict = PyThreadState_GetDict();
Guido van Rossumeb909461998-04-11 15:17:34 +00001061 if (dict == NULL)
1062 return;
Guido van Rossum86610361998-04-10 22:32:46 +00001063 list = PyDict_GetItemString(dict, KEY);
Guido van Rossumeb909461998-04-11 15:17:34 +00001064 if (list == NULL || !PyList_Check(list))
1065 return;
Guido van Rossum86610361998-04-10 22:32:46 +00001066 i = PyList_GET_SIZE(list);
1067 /* Count backwards because we always expect obj to be list[-1] */
1068 while (--i >= 0) {
1069 if (PyList_GET_ITEM(list, i) == obj) {
1070 PyList_SetSlice(list, i, i + 1, NULL);
1071 break;
1072 }
1073 }
1074}
Guido van Rossumd724b232000-03-13 16:01:29 +00001075
1076/*
1077 trashcan
1078 CT 2k0130
1079 non-recursively destroy nested objects
1080
1081 CT 2k0223
1082 everything is now done in a macro.
1083
1084 CT 2k0305
1085 modified to use functions, after Tim Peter's suggestion.
1086
1087 CT 2k0309
1088 modified to restore a possible error.
Guido van Rossum13ff8eb2000-03-25 18:39:19 +00001089
1090 CT 2k0325
1091 added better safe than sorry check for threadstate
Guido van Rossume92e6102000-04-24 15:40:53 +00001092
1093 CT 2k0422
1094 complete rewrite. We now build a chain via ob_type
1095 and save the limited number of types in ob_refcnt.
1096 This is perfect since we don't need any memory.
1097 A patch for free-threading would need just a lock.
Guido van Rossumd724b232000-03-13 16:01:29 +00001098*/
1099
Guido van Rossume92e6102000-04-24 15:40:53 +00001100#define Py_TRASHCAN_TUPLE 1
1101#define Py_TRASHCAN_LIST 2
1102#define Py_TRASHCAN_DICT 3
1103#define Py_TRASHCAN_FRAME 4
1104#define Py_TRASHCAN_TRACEBACK 5
1105/* extend here if other objects want protection */
1106
Guido van Rossumd724b232000-03-13 16:01:29 +00001107int _PyTrash_delete_nesting = 0;
Guido van Rossume92e6102000-04-24 15:40:53 +00001108
Guido van Rossumd724b232000-03-13 16:01:29 +00001109PyObject * _PyTrash_delete_later = NULL;
1110
1111void
Fred Drake100814d2000-07-09 15:48:49 +00001112_PyTrash_deposit_object(PyObject *op)
Guido van Rossumd724b232000-03-13 16:01:29 +00001113{
Guido van Rossume92e6102000-04-24 15:40:53 +00001114 int typecode;
Guido van Rossum13ff8eb2000-03-25 18:39:19 +00001115
Guido van Rossume92e6102000-04-24 15:40:53 +00001116 if (PyTuple_Check(op))
1117 typecode = Py_TRASHCAN_TUPLE;
1118 else if (PyList_Check(op))
1119 typecode = Py_TRASHCAN_LIST;
1120 else if (PyDict_Check(op))
1121 typecode = Py_TRASHCAN_DICT;
1122 else if (PyFrame_Check(op))
1123 typecode = Py_TRASHCAN_FRAME;
1124 else if (PyTraceBack_Check(op))
1125 typecode = Py_TRASHCAN_TRACEBACK;
Moshe Zadkacf703f02000-08-04 15:36:13 +00001126 else /* We have a bug here -- those are the only types in GC */ {
1127 Py_FatalError("Type not supported in GC -- internal bug");
1128 return; /* pacify compiler -- execution never here */
1129 }
Guido van Rossume92e6102000-04-24 15:40:53 +00001130 op->ob_refcnt = typecode;
Guido van Rossumd724b232000-03-13 16:01:29 +00001131
Guido van Rossume92e6102000-04-24 15:40:53 +00001132 op->ob_type = (PyTypeObject*)_PyTrash_delete_later;
1133 _PyTrash_delete_later = op;
Guido van Rossumd724b232000-03-13 16:01:29 +00001134}
1135
1136void
Fred Drake100814d2000-07-09 15:48:49 +00001137_PyTrash_destroy_chain(void)
Guido van Rossumd724b232000-03-13 16:01:29 +00001138{
1139 while (_PyTrash_delete_later) {
1140 PyObject *shredder = _PyTrash_delete_later;
Guido van Rossume92e6102000-04-24 15:40:53 +00001141 _PyTrash_delete_later = (PyObject*) shredder->ob_type;
1142
1143 switch (shredder->ob_refcnt) {
1144 case Py_TRASHCAN_TUPLE:
1145 shredder->ob_type = &PyTuple_Type;
1146 break;
1147 case Py_TRASHCAN_LIST:
1148 shredder->ob_type = &PyList_Type;
1149 break;
1150 case Py_TRASHCAN_DICT:
1151 shredder->ob_type = &PyDict_Type;
1152 break;
1153 case Py_TRASHCAN_FRAME:
1154 shredder->ob_type = &PyFrame_Type;
1155 break;
1156 case Py_TRASHCAN_TRACEBACK:
1157 shredder->ob_type = &PyTraceBack_Type;
1158 break;
1159 }
1160 _Py_NewReference(shredder);
1161
Guido van Rossumd724b232000-03-13 16:01:29 +00001162 ++_PyTrash_delete_nesting;
1163 Py_DECREF(shredder);
1164 --_PyTrash_delete_nesting;
1165 }
1166}