blob: 0c5017546605b1ee05875da54cb4772ab6db3c8d [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
Fred Drake13634cf2000-06-29 19:17:04 +000019#include "mymath.h"
20
Guido van Rossume92e6102000-04-24 15:40:53 +000021/* just for trashcan: */
22#include "compile.h"
23#include "frameobject.h"
24#include "traceback.h"
25
Guido van Rossum6f9e4331995-03-29 16:57:48 +000026#if defined( Py_TRACE_REFS ) || defined( Py_REF_DEBUG )
Guido van Rossumbffd6832000-01-20 22:32:56 +000027DL_IMPORT(long) _Py_RefTotal;
Guido van Rossum3f5da241990-12-20 15:06:42 +000028#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000029
Guido van Rossum3f5da241990-12-20 15:06:42 +000030/* Object allocation routines used by NEWOBJ and NEWVAROBJ macros.
31 These are used by the individual routines for object creation.
32 Do not call them otherwise, they do not initialize the object! */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000033
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +000034#ifdef COUNT_ALLOCS
Guido van Rossumc0b618a1997-05-02 03:12:38 +000035static PyTypeObject *type_list;
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +000036extern int tuple_zero_allocs, fast_tuple_allocs;
37extern int quick_int_allocs, quick_neg_int_allocs;
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +000038extern int null_strings, one_strings;
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +000039void
Fred Drake100814d2000-07-09 15:48:49 +000040dump_counts(void)
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +000041{
Guido van Rossumc0b618a1997-05-02 03:12:38 +000042 PyTypeObject *tp;
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +000043
44 for (tp = type_list; tp; tp = tp->tp_next)
Sjoerd Mullender52c1f511993-10-25 08:40:52 +000045 fprintf(stderr, "%s alloc'd: %d, freed: %d, max in use: %d\n",
46 tp->tp_name, tp->tp_alloc, tp->tp_free,
47 tp->tp_maxalloc);
48 fprintf(stderr, "fast tuple allocs: %d, empty: %d\n",
49 fast_tuple_allocs, tuple_zero_allocs);
50 fprintf(stderr, "fast int allocs: pos: %d, neg: %d\n",
51 quick_int_allocs, quick_neg_int_allocs);
52 fprintf(stderr, "null strings: %d, 1-strings: %d\n",
53 null_strings, one_strings);
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +000054}
55
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +000056PyObject *
Fred Drake100814d2000-07-09 15:48:49 +000057get_counts(void)
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +000058{
59 PyTypeObject *tp;
60 PyObject *result;
61 PyObject *v;
62
63 result = PyList_New(0);
64 if (result == NULL)
65 return NULL;
66 for (tp = type_list; tp; tp = tp->tp_next) {
67 v = Py_BuildValue("(siii)", tp->tp_name, tp->tp_alloc,
68 tp->tp_free, tp->tp_maxalloc);
69 if (v == NULL) {
70 Py_DECREF(result);
71 return NULL;
72 }
73 if (PyList_Append(result, v) < 0) {
74 Py_DECREF(v);
75 Py_DECREF(result);
76 return NULL;
77 }
78 Py_DECREF(v);
79 }
80 return result;
81}
82
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +000083void
Fred Drake100814d2000-07-09 15:48:49 +000084inc_count(PyTypeObject *tp)
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +000085{
86 if (tp->tp_alloc == 0) {
Guido van Rossumd8953cb1995-04-06 14:46:26 +000087 /* first time; insert in linked list */
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +000088 if (tp->tp_next != NULL) /* sanity check */
Guido van Rossumc0b618a1997-05-02 03:12:38 +000089 Py_FatalError("XXX inc_count sanity check");
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +000090 tp->tp_next = type_list;
91 type_list = tp;
92 }
93 tp->tp_alloc++;
94 if (tp->tp_alloc - tp->tp_free > tp->tp_maxalloc)
95 tp->tp_maxalloc = tp->tp_alloc - tp->tp_free;
96}
97#endif
98
Guido van Rossumc0b618a1997-05-02 03:12:38 +000099PyObject *
Fred Drake100814d2000-07-09 15:48:49 +0000100PyObject_Init(PyObject *op, PyTypeObject *tp)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000101{
Guido van Rossumb18618d2000-05-03 23:44:39 +0000102 if (op == NULL) {
103 PyErr_SetString(PyExc_SystemError,
104 "NULL object passed to PyObject_Init");
105 return op;
106 }
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000107#ifdef WITH_CYCLE_GC
108 if (PyType_IS_GC(tp))
109 op = (PyObject *) PyObject_FROM_GC(op);
110#endif
Guido van Rossumb18618d2000-05-03 23:44:39 +0000111 /* Any changes should be reflected in PyObject_INIT (objimpl.h) */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000112 op->ob_type = tp;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000113 _Py_NewReference(op);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000114 return op;
115}
116
Guido van Rossumb18618d2000-05-03 23:44:39 +0000117PyVarObject *
Fred Drake100814d2000-07-09 15:48:49 +0000118PyObject_InitVar(PyVarObject *op, PyTypeObject *tp, int size)
Guido van Rossumb18618d2000-05-03 23:44:39 +0000119{
120 if (op == NULL) {
121 PyErr_SetString(PyExc_SystemError,
122 "NULL object passed to PyObject_InitVar");
123 return op;
124 }
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000125#ifdef WITH_CYCLE_GC
126 if (PyType_IS_GC(tp))
127 op = (PyVarObject *) PyObject_FROM_GC(op);
128#endif
Guido van Rossumb18618d2000-05-03 23:44:39 +0000129 /* Any changes should be reflected in PyObject_INIT_VAR */
130 op->ob_size = size;
131 op->ob_type = tp;
132 _Py_NewReference((PyObject *)op);
133 return op;
134}
135
136PyObject *
Fred Drake100814d2000-07-09 15:48:49 +0000137_PyObject_New(PyTypeObject *tp)
Guido van Rossumb18618d2000-05-03 23:44:39 +0000138{
139 PyObject *op;
140 op = (PyObject *) PyObject_MALLOC(_PyObject_SIZE(tp));
141 if (op == NULL)
142 return PyErr_NoMemory();
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000143#ifdef WITH_CYCLE_GC
144 if (PyType_IS_GC(tp))
145 op = (PyObject *) PyObject_FROM_GC(op);
146#endif
Guido van Rossumb18618d2000-05-03 23:44:39 +0000147 return PyObject_INIT(op, tp);
148}
149
Guido van Rossumd0c87ee1997-05-15 21:31:03 +0000150PyVarObject *
Fred Drake100814d2000-07-09 15:48:49 +0000151_PyObject_NewVar(PyTypeObject *tp, int size)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000152{
Guido van Rossumb18618d2000-05-03 23:44:39 +0000153 PyVarObject *op;
154 op = (PyVarObject *) PyObject_MALLOC(_PyObject_VAR_SIZE(tp, size));
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000155 if (op == NULL)
Guido van Rossumd0c87ee1997-05-15 21:31:03 +0000156 return (PyVarObject *)PyErr_NoMemory();
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000157#ifdef WITH_CYCLE_GC
158 if (PyType_IS_GC(tp))
159 op = (PyVarObject *) PyObject_FROM_GC(op);
160#endif
Guido van Rossumb18618d2000-05-03 23:44:39 +0000161 return PyObject_INIT_VAR(op, tp, size);
162}
163
164void
Fred Drake100814d2000-07-09 15:48:49 +0000165_PyObject_Del(PyObject *op)
Guido van Rossumb18618d2000-05-03 23:44:39 +0000166{
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000167#ifdef WITH_CYCLE_GC
Guido van Rossum4cc6ac72000-07-01 01:00:38 +0000168 if (op && PyType_IS_GC(op->ob_type)) {
169 op = (PyObject *) PyObject_AS_GC(op);
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000170 }
Guido van Rossum4cc6ac72000-07-01 01:00:38 +0000171#endif
172 PyObject_FREE(op);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000173}
174
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000175#ifndef WITH_CYCLE_GC
176/* extension modules might need these */
177void _PyGC_Insert(PyObject *op) { }
178void _PyGC_Remove(PyObject *op) { }
179#endif
180
Guido van Rossum90933611991-06-07 16:10:43 +0000181int
Fred Drake100814d2000-07-09 15:48:49 +0000182PyObject_Print(PyObject *op, FILE *fp, int flags)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000183{
Guido van Rossum278ef591991-07-27 21:40:24 +0000184 int ret = 0;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000185 if (PyErr_CheckSignals())
Guido van Rossum90933611991-06-07 16:10:43 +0000186 return -1;
Guido van Rossum9b00dfa1998-04-28 16:06:54 +0000187#ifdef USE_STACKCHECK
188 if (PyOS_CheckStack()) {
189 PyErr_SetString(PyExc_MemoryError, "Stack overflow");
190 return -1;
191 }
192#endif
Guido van Rossum687ef6e2000-01-12 16:28:58 +0000193 clearerr(fp); /* Clear any previous error condition */
Guido van Rossum90933611991-06-07 16:10:43 +0000194 if (op == NULL) {
195 fprintf(fp, "<nil>");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000196 }
Guido van Rossum90933611991-06-07 16:10:43 +0000197 else {
198 if (op->ob_refcnt <= 0)
Fred Drakea44d3532000-06-30 15:01:00 +0000199 fprintf(fp, "<refcnt %u at %p>",
200 op->ob_refcnt, op);
Guido van Rossum2e8f6141992-09-03 20:32:55 +0000201 else if (op->ob_type->tp_print == NULL) {
202 if (op->ob_type->tp_repr == NULL) {
Fred Drakea44d3532000-06-30 15:01:00 +0000203 fprintf(fp, "<%s object at %p>",
204 op->ob_type->tp_name, op);
Guido van Rossum2e8f6141992-09-03 20:32:55 +0000205 }
206 else {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000207 PyObject *s;
208 if (flags & Py_PRINT_RAW)
209 s = PyObject_Str(op);
Guido van Rossumc6004111993-11-05 10:22:19 +0000210 else
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000211 s = PyObject_Repr(op);
Guido van Rossum2e8f6141992-09-03 20:32:55 +0000212 if (s == NULL)
213 ret = -1;
Guido van Rossum2e8f6141992-09-03 20:32:55 +0000214 else {
Guido van Rossum565798d1998-04-21 22:25:01 +0000215 ret = PyObject_Print(s, fp,
216 Py_PRINT_RAW);
Guido van Rossum2e8f6141992-09-03 20:32:55 +0000217 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000218 Py_XDECREF(s);
Guido van Rossum2e8f6141992-09-03 20:32:55 +0000219 }
220 }
Guido van Rossum90933611991-06-07 16:10:43 +0000221 else
Guido van Rossum278ef591991-07-27 21:40:24 +0000222 ret = (*op->ob_type->tp_print)(op, fp, flags);
Guido van Rossum90933611991-06-07 16:10:43 +0000223 }
Guido van Rossum278ef591991-07-27 21:40:24 +0000224 if (ret == 0) {
225 if (ferror(fp)) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000226 PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossum278ef591991-07-27 21:40:24 +0000227 clearerr(fp);
228 ret = -1;
229 }
230 }
231 return ret;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000232}
233
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000234PyObject *
Fred Drake100814d2000-07-09 15:48:49 +0000235PyObject_Repr(PyObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000236{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000237 if (PyErr_CheckSignals())
Guido van Rossum90933611991-06-07 16:10:43 +0000238 return NULL;
Guido van Rossum9b00dfa1998-04-28 16:06:54 +0000239#ifdef USE_STACKCHECK
240 if (PyOS_CheckStack()) {
241 PyErr_SetString(PyExc_MemoryError, "Stack overflow");
242 return NULL;
243 }
244#endif
Guido van Rossum90933611991-06-07 16:10:43 +0000245 if (v == NULL)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000246 return PyString_FromString("<NULL>");
Guido van Rossum90933611991-06-07 16:10:43 +0000247 else if (v->ob_type->tp_repr == NULL) {
248 char buf[120];
Fred Drakea44d3532000-06-30 15:01:00 +0000249 sprintf(buf, "<%.80s object at %p>",
250 v->ob_type->tp_name, v);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000251 return PyString_FromString(buf);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000252 }
Guido van Rossum4c08d552000-03-10 22:55:18 +0000253 else {
254 PyObject *res;
255 res = (*v->ob_type->tp_repr)(v);
256 if (res == NULL)
257 return NULL;
Fredrik Lundhefecc7d2000-07-01 14:31:09 +0000258 if (PyUnicode_Check(res)) {
259 PyObject* str;
Fredrik Lundh2a1e0602000-07-08 17:43:32 +0000260 str = PyUnicode_AsUnicodeEscapeString(res);
Marc-André Lemburg891bc652000-07-03 09:57:53 +0000261 Py_DECREF(res);
262 if (str)
Fredrik Lundhefecc7d2000-07-01 14:31:09 +0000263 res = str;
Marc-André Lemburg891bc652000-07-03 09:57:53 +0000264 else
265 return NULL;
Fredrik Lundhefecc7d2000-07-01 14:31:09 +0000266 }
Guido van Rossum4c08d552000-03-10 22:55:18 +0000267 if (!PyString_Check(res)) {
268 PyErr_Format(PyExc_TypeError,
Guido van Rossum5db862d2000-04-10 12:46:51 +0000269 "__repr__ returned non-string (type %.200s)",
Guido van Rossum4c08d552000-03-10 22:55:18 +0000270 res->ob_type->tp_name);
271 Py_DECREF(res);
272 return NULL;
273 }
274 return res;
275 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000276}
277
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000278PyObject *
Fred Drake100814d2000-07-09 15:48:49 +0000279PyObject_Str(PyObject *v)
Guido van Rossumc6004111993-11-05 10:22:19 +0000280{
Guido van Rossum4c08d552000-03-10 22:55:18 +0000281 PyObject *res;
282
Guido van Rossumc6004111993-11-05 10:22:19 +0000283 if (v == NULL)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000284 return PyString_FromString("<NULL>");
285 else if (PyString_Check(v)) {
286 Py_INCREF(v);
Guido van Rossumc6004111993-11-05 10:22:19 +0000287 return v;
288 }
Guido van Rossum32b582b1995-01-17 16:35:13 +0000289 else if (v->ob_type->tp_str != NULL)
Guido van Rossum4c08d552000-03-10 22:55:18 +0000290 res = (*v->ob_type->tp_str)(v);
Guido van Rossumc6004111993-11-05 10:22:19 +0000291 else {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000292 PyObject *func;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000293 if (!PyInstance_Check(v) ||
294 (func = PyObject_GetAttrString(v, "__str__")) == NULL) {
295 PyErr_Clear();
296 return PyObject_Repr(v);
Guido van Rossumc6004111993-11-05 10:22:19 +0000297 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000298 res = PyEval_CallObject(func, (PyObject *)NULL);
299 Py_DECREF(func);
Guido van Rossumc6004111993-11-05 10:22:19 +0000300 }
Guido van Rossum4c08d552000-03-10 22:55:18 +0000301 if (res == NULL)
302 return NULL;
Marc-André Lemburg891bc652000-07-03 09:57:53 +0000303 if (PyUnicode_Check(res)) {
304 PyObject* str;
305 str = PyUnicode_AsEncodedString(res, NULL, NULL);
306 Py_DECREF(res);
307 if (str)
308 res = str;
309 else
310 return NULL;
311 }
Guido van Rossum4c08d552000-03-10 22:55:18 +0000312 if (!PyString_Check(res)) {
313 PyErr_Format(PyExc_TypeError,
Guido van Rossum5db862d2000-04-10 12:46:51 +0000314 "__str__ returned non-string (type %.200s)",
Guido van Rossum4c08d552000-03-10 22:55:18 +0000315 res->ob_type->tp_name);
316 Py_DECREF(res);
317 return NULL;
318 }
319 return res;
Guido van Rossumc6004111993-11-05 10:22:19 +0000320}
321
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000322static PyObject *
Fred Drake100814d2000-07-09 15:48:49 +0000323do_cmp(PyObject *v, PyObject *w)
Guido van Rossum20566841995-01-12 11:26:10 +0000324{
Guido van Rossumc8b6df91997-05-23 00:06:51 +0000325 long c;
Guido van Rossum20566841995-01-12 11:26:10 +0000326 /* __rcmp__ actually won't be called unless __cmp__ isn't defined,
327 because the check in cmpobject() reverses the objects first.
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000328 This is intentional -- it makes no sense to define cmp(x,y)
329 different than -cmp(y,x). */
330 if (PyInstance_Check(v) || PyInstance_Check(w))
331 return PyInstance_DoBinOp(v, w, "__cmp__", "__rcmp__", do_cmp);
Guido van Rossumc8b6df91997-05-23 00:06:51 +0000332 c = PyObject_Compare(v, w);
333 if (c && PyErr_Occurred())
334 return NULL;
335 return PyInt_FromLong(c);
Guido van Rossum20566841995-01-12 11:26:10 +0000336}
337
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000338PyObject *_PyCompareState_Key;
339
340/* _PyCompareState_nesting is incremented beforing call compare (for
341 some types) and decremented on exit. If the count exceeds the
342 nesting limit, enable code to detect circular data structures.
343*/
344#define NESTING_LIMIT 500
345int _PyCompareState_nesting = 0;
346
347static PyObject*
Fred Drake100814d2000-07-09 15:48:49 +0000348get_inprogress_dict(void)
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000349{
350 PyObject *tstate_dict, *inprogress;
351
352 tstate_dict = PyThreadState_GetDict();
353 if (tstate_dict == NULL) {
354 PyErr_BadInternalCall();
355 return NULL;
356 }
357 inprogress = PyDict_GetItem(tstate_dict, _PyCompareState_Key);
358 if (inprogress == NULL) {
359 PyErr_Clear();
360 inprogress = PyDict_New();
361 if (inprogress == NULL)
362 return NULL;
363 if (PyDict_SetItem(tstate_dict, _PyCompareState_Key,
364 inprogress) == -1) {
365 Py_DECREF(inprogress);
366 return NULL;
367 }
Jeremy Hyltona251ea02000-06-09 16:20:39 +0000368 Py_DECREF(inprogress);
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000369 }
370 return inprogress;
371}
372
373static PyObject *
Fred Drake100814d2000-07-09 15:48:49 +0000374make_pair(PyObject *v, PyObject *w)
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000375{
376 PyObject *pair;
377
378 pair = PyTuple_New(2);
379 if (pair == NULL) {
380 return NULL;
381 }
Guido van Rossumad89bbc2000-06-28 21:57:18 +0000382 if (v <= w) {
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000383 PyTuple_SET_ITEM(pair, 0, PyLong_FromVoidPtr((void *)v));
384 PyTuple_SET_ITEM(pair, 1, PyLong_FromVoidPtr((void *)w));
385 } else {
386 PyTuple_SET_ITEM(pair, 0, PyLong_FromVoidPtr((void *)w));
387 PyTuple_SET_ITEM(pair, 1, PyLong_FromVoidPtr((void *)v));
388 }
389 return pair;
390}
391
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000392int
Fred Drake100814d2000-07-09 15:48:49 +0000393PyObject_Compare(PyObject *v, PyObject *w)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000394{
Guido van Rossumcd5a5f61998-06-09 18:58:44 +0000395 PyTypeObject *vtp, *wtp;
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000396 int result;
397
Guido van Rossumc8b6df91997-05-23 00:06:51 +0000398 if (v == NULL || w == NULL) {
399 PyErr_BadInternalCall();
400 return -1;
401 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000402 if (v == w)
403 return 0;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000404 if (PyInstance_Check(v) || PyInstance_Check(w)) {
405 PyObject *res;
Guido van Rossum20566841995-01-12 11:26:10 +0000406 int c;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000407 if (!PyInstance_Check(v))
408 return -PyObject_Compare(w, v);
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000409 if (++_PyCompareState_nesting > NESTING_LIMIT) {
410 PyObject *inprogress, *pair;
411
412 inprogress = get_inprogress_dict();
413 if (inprogress == NULL) {
414 return -1;
415 }
416 pair = make_pair(v, w);
417 if (PyDict_GetItem(inprogress, pair)) {
418 /* already comparing these objects. assume
419 they're equal until shown otherwise */
420 Py_DECREF(pair);
421 --_PyCompareState_nesting;
422 return 0;
423 }
424 if (PyDict_SetItem(inprogress, pair, pair) == -1) {
425 return -1;
426 }
427 res = do_cmp(v, w);
428 _PyCompareState_nesting--;
429 /* XXX DelItem shouldn't fail */
430 PyDict_DelItem(inprogress, pair);
431 Py_DECREF(pair);
432 } else {
433 res = do_cmp(v, w);
434 }
Guido van Rossumc8b6df91997-05-23 00:06:51 +0000435 if (res == NULL)
436 return -1;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000437 if (!PyInt_Check(res)) {
438 Py_DECREF(res);
Guido van Rossumc8b6df91997-05-23 00:06:51 +0000439 PyErr_SetString(PyExc_TypeError,
440 "comparison did not return an int");
441 return -1;
Guido van Rossum20566841995-01-12 11:26:10 +0000442 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000443 c = PyInt_AsLong(res);
444 Py_DECREF(res);
Guido van Rossum20566841995-01-12 11:26:10 +0000445 return (c < 0) ? -1 : (c > 0) ? 1 : 0;
446 }
Guido van Rossumcd5a5f61998-06-09 18:58:44 +0000447 if ((vtp = v->ob_type) != (wtp = w->ob_type)) {
448 char *vname = vtp->tp_name;
449 char *wname = wtp->tp_name;
Guido van Rossumb4db1941998-07-21 21:56:41 +0000450 if (vtp->tp_as_number != NULL && wtp->tp_as_number != NULL) {
Guido van Rossum242c6421997-11-19 16:03:17 +0000451 int err;
452 err = PyNumber_CoerceEx(&v, &w);
453 if (err < 0)
Guido van Rossumc8b6df91997-05-23 00:06:51 +0000454 return -1;
Guido van Rossum242c6421997-11-19 16:03:17 +0000455 else if (err == 0) {
Guido van Rossumb4db1941998-07-21 21:56:41 +0000456 int cmp;
457 vtp = v->ob_type;
458 if (vtp->tp_compare == NULL)
459 cmp = (v < w) ? -1 : 1;
460 else
461 cmp = (*vtp->tp_compare)(v, w);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000462 Py_DECREF(v);
463 Py_DECREF(w);
Guido van Rossum9fb03681991-07-01 18:48:04 +0000464 return cmp;
465 }
466 }
Guido van Rossumb244f692000-04-10 13:42:33 +0000467 else if (PyUnicode_Check(v) || PyUnicode_Check(w)) {
468 int result = PyUnicode_Compare(v, w);
469 if (result == -1 && PyErr_Occurred() &&
470 PyErr_ExceptionMatches(PyExc_TypeError))
471 /* TypeErrors are ignored: if Unicode coercion
472 fails due to one of the arguments not
473 having the right type, we continue as
474 defined by the coercion protocol (see
475 above). Luckily, decoding errors are
476 reported as ValueErrors and are not masked
477 by this technique. */
478 PyErr_Clear();
479 else
480 return result;
481 }
Guido van Rossumcd5a5f61998-06-09 18:58:44 +0000482 else if (vtp->tp_as_number != NULL)
483 vname = "";
484 else if (wtp->tp_as_number != NULL)
485 wname = "";
486 /* Numerical types compare smaller than all other types */
487 return strcmp(vname, wname);
Guido van Rossum9fb03681991-07-01 18:48:04 +0000488 }
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000489 if (vtp->tp_compare == NULL) {
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000490 return (v < w) ? -1 : 1;
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000491 }
492 if (++_PyCompareState_nesting > NESTING_LIMIT
493 && (vtp->tp_as_mapping
494 || (vtp->tp_as_sequence && !PyString_Check(v)))) {
495 PyObject *inprogress, *pair;
496
497 inprogress = get_inprogress_dict();
498 if (inprogress == NULL) {
499 return -1;
500 }
501 pair = make_pair(v, w);
502 if (PyDict_GetItem(inprogress, pair)) {
503 /* already comparing these objects. assume
504 they're equal until shown otherwise */
505 _PyCompareState_nesting--;
506 Py_DECREF(pair);
507 return 0;
508 }
509 if (PyDict_SetItem(inprogress, pair, pair) == -1) {
510 return -1;
511 }
512 result = (*vtp->tp_compare)(v, w);
513 _PyCompareState_nesting--;
514 PyDict_DelItem(inprogress, pair); /* XXX shouldn't fail */
515 Py_DECREF(pair);
516 } else {
517 result = (*vtp->tp_compare)(v, w);
518 }
519 return result;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000520}
521
Fred Drake13634cf2000-06-29 19:17:04 +0000522
523/* Set of hash utility functions to help maintaining the invariant that
524 iff a==b then hash(a)==hash(b)
525
526 All the utility functions (_Py_Hash*()) return "-1" to signify an error.
527*/
528
529long
Fred Drake100814d2000-07-09 15:48:49 +0000530_Py_HashDouble(double v)
Fred Drake13634cf2000-06-29 19:17:04 +0000531{
532 /* Use frexp to get at the bits in the double.
533 * Since the VAX D double format has 56 mantissa bits, which is the
534 * most of any double format in use, each of these parts may have as
535 * many as (but no more than) 56 significant bits.
536 * So, assuming sizeof(long) >= 4, each part can be broken into two longs;
537 * frexp and multiplication are used to do that.
538 * Also, since the Cray double format has 15 exponent bits, which is the
539 * most of any double format in use, shifting the exponent field left by
540 * 15 won't overflow a long (again assuming sizeof(long) >= 4).
541 */
542 int expo;
543 long hipart;
544
545 v = frexp(v, &expo);
546 v = v * 2147483648.0; /* 2**31 */
547 hipart = (long)v; /* Take the top 32 bits */
548 v = (v - (double)hipart) * 2147483648.0; /* Get the next 32 bits */
549
550 return hipart + (long)v + (expo << 15); /* Combine everything */
551}
552
553long
Fred Drake100814d2000-07-09 15:48:49 +0000554_Py_HashPointer(void *p)
Fred Drake13634cf2000-06-29 19:17:04 +0000555{
556#if SIZEOF_LONG >= SIZEOF_VOID_P
557 return (long)p;
558#else
559 /* convert to a Python long and hash that */
560 PyObject* longobj;
561 long x;
562
563 if ((longobj = PyLong_FromVoidPtr(p)) == NULL) {
564 x = -1;
565 goto finally;
566 }
567 x = PyObject_Hash(longobj);
568
569finally:
570 Py_XDECREF(longobj);
571 return x;
572#endif
573}
574
575
Guido van Rossum9bfef441993-03-29 10:43:31 +0000576long
Fred Drake100814d2000-07-09 15:48:49 +0000577PyObject_Hash(PyObject *v)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000578{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000579 PyTypeObject *tp = v->ob_type;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000580 if (tp->tp_hash != NULL)
581 return (*tp->tp_hash)(v);
Fred Drake13634cf2000-06-29 19:17:04 +0000582 if (tp->tp_compare == NULL) {
583 return _Py_HashPointer(v); /* Use address as hash value */
584 }
Guido van Rossum9bfef441993-03-29 10:43:31 +0000585 /* If there's a cmp but no hash defined, the object can't be hashed */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000586 PyErr_SetString(PyExc_TypeError, "unhashable type");
Guido van Rossum9bfef441993-03-29 10:43:31 +0000587 return -1;
588}
589
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000590PyObject *
Fred Drake100814d2000-07-09 15:48:49 +0000591PyObject_GetAttrString(PyObject *v, char *name)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000592{
Guido van Rossumd8eb1b31996-08-09 20:52:03 +0000593 if (v->ob_type->tp_getattro != NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000594 PyObject *w, *res;
Guido van Rossumc6d06701997-01-18 07:57:16 +0000595 w = PyString_InternFromString(name);
Guido van Rossumd8eb1b31996-08-09 20:52:03 +0000596 if (w == NULL)
597 return NULL;
598 res = (*v->ob_type->tp_getattro)(v, w);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000599 Py_XDECREF(w);
Guido van Rossumd8eb1b31996-08-09 20:52:03 +0000600 return res;
601 }
602
Guido van Rossum3f5da241990-12-20 15:06:42 +0000603 if (v->ob_type->tp_getattr == NULL) {
Guido van Rossumdb935161998-01-19 22:16:36 +0000604 PyErr_Format(PyExc_AttributeError,
605 "'%.50s' object has no attribute '%.400s'",
606 v->ob_type->tp_name,
607 name);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000608 return NULL;
609 }
610 else {
611 return (*v->ob_type->tp_getattr)(v, name);
612 }
613}
614
615int
Fred Drake100814d2000-07-09 15:48:49 +0000616PyObject_HasAttrString(PyObject *v, char *name)
Guido van Rossumed18fdc1993-07-11 19:55:34 +0000617{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000618 PyObject *res = PyObject_GetAttrString(v, name);
Guido van Rossumed18fdc1993-07-11 19:55:34 +0000619 if (res != NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000620 Py_DECREF(res);
Guido van Rossumed18fdc1993-07-11 19:55:34 +0000621 return 1;
622 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000623 PyErr_Clear();
Guido van Rossumed18fdc1993-07-11 19:55:34 +0000624 return 0;
625}
626
627int
Fred Drake100814d2000-07-09 15:48:49 +0000628PyObject_SetAttrString(PyObject *v, char *name, PyObject *w)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000629{
Guido van Rossumd8eb1b31996-08-09 20:52:03 +0000630 if (v->ob_type->tp_setattro != NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000631 PyObject *s;
Guido van Rossumd8eb1b31996-08-09 20:52:03 +0000632 int res;
Guido van Rossumc6d06701997-01-18 07:57:16 +0000633 s = PyString_InternFromString(name);
Guido van Rossumd8eb1b31996-08-09 20:52:03 +0000634 if (s == NULL)
Guido van Rossumb7fc3041996-09-11 22:51:25 +0000635 return -1;
Guido van Rossumd8eb1b31996-08-09 20:52:03 +0000636 res = (*v->ob_type->tp_setattro)(v, s, w);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000637 Py_XDECREF(s);
Guido van Rossumd8eb1b31996-08-09 20:52:03 +0000638 return res;
639 }
640
Guido van Rossum3f5da241990-12-20 15:06:42 +0000641 if (v->ob_type->tp_setattr == NULL) {
642 if (v->ob_type->tp_getattr == NULL)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000643 PyErr_SetString(PyExc_TypeError,
Guido van Rossum3ea74121991-12-24 13:28:03 +0000644 "attribute-less object (assign or del)");
Guido van Rossum3f5da241990-12-20 15:06:42 +0000645 else
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000646 PyErr_SetString(PyExc_TypeError,
Guido van Rossum3ea74121991-12-24 13:28:03 +0000647 "object has read-only attributes");
Guido van Rossum73531a31990-12-20 23:12:40 +0000648 return -1;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000649 }
650 else {
651 return (*v->ob_type->tp_setattr)(v, name, w);
652 }
653}
654
Guido van Rossum98ff96a1997-05-20 18:34:44 +0000655PyObject *
Fred Drake100814d2000-07-09 15:48:49 +0000656PyObject_GetAttr(PyObject *v, PyObject *name)
Guido van Rossum98ff96a1997-05-20 18:34:44 +0000657{
658 if (v->ob_type->tp_getattro != NULL)
659 return (*v->ob_type->tp_getattro)(v, name);
Jeremy Hylton99a8f902000-06-23 14:36:32 +0000660
661 if (!PyString_Check(name)) {
662 PyErr_SetString(PyExc_TypeError,
663 "attribute name must be string");
664 return NULL;
665 }
666 return PyObject_GetAttrString(v, PyString_AS_STRING(name));
Guido van Rossum98ff96a1997-05-20 18:34:44 +0000667}
668
669int
Fred Drake100814d2000-07-09 15:48:49 +0000670PyObject_HasAttr(PyObject *v, PyObject *name)
Guido van Rossum98ff96a1997-05-20 18:34:44 +0000671{
672 PyObject *res = PyObject_GetAttr(v, name);
673 if (res != NULL) {
674 Py_DECREF(res);
675 return 1;
676 }
677 PyErr_Clear();
678 return 0;
679}
680
681int
Fred Drake100814d2000-07-09 15:48:49 +0000682PyObject_SetAttr(PyObject *v, PyObject *name, PyObject *value)
Guido van Rossum98ff96a1997-05-20 18:34:44 +0000683{
684 int err;
685 Py_INCREF(name);
Jeremy Hylton99a8f902000-06-23 14:36:32 +0000686 if (PyString_Check(name))
687 PyString_InternInPlace(&name);
Guido van Rossum98ff96a1997-05-20 18:34:44 +0000688 if (v->ob_type->tp_setattro != NULL)
689 err = (*v->ob_type->tp_setattro)(v, name, value);
Jeremy Hylton99a8f902000-06-23 14:36:32 +0000690 else if (PyString_Check(name)) {
Guido van Rossum98ff96a1997-05-20 18:34:44 +0000691 err = PyObject_SetAttrString(
Jeremy Hylton99a8f902000-06-23 14:36:32 +0000692 v, PyString_AS_STRING(name), value);
693 }
694 else {
695 PyErr_SetString(PyExc_TypeError,
696 "attribute name must be string");
697 err = -1;
698 }
Guido van Rossum98ff96a1997-05-20 18:34:44 +0000699 Py_DECREF(name);
700 return err;
701}
702
Guido van Rossum6ac258d1993-05-12 08:24:20 +0000703/* Test a value used as condition, e.g., in a for or if statement.
704 Return -1 if an error occurred */
705
706int
Fred Drake100814d2000-07-09 15:48:49 +0000707PyObject_IsTrue(PyObject *v)
Guido van Rossum6ac258d1993-05-12 08:24:20 +0000708{
709 int res;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000710 if (v == Py_None)
Guido van Rossum6ac258d1993-05-12 08:24:20 +0000711 res = 0;
Guido van Rossum1c4f4581998-05-22 00:53:24 +0000712 else if (v->ob_type->tp_as_number != NULL &&
713 v->ob_type->tp_as_number->nb_nonzero != NULL)
Guido van Rossum6ac258d1993-05-12 08:24:20 +0000714 res = (*v->ob_type->tp_as_number->nb_nonzero)(v);
Guido van Rossum1c4f4581998-05-22 00:53:24 +0000715 else if (v->ob_type->tp_as_mapping != NULL &&
716 v->ob_type->tp_as_mapping->mp_length != NULL)
Guido van Rossum6ac258d1993-05-12 08:24:20 +0000717 res = (*v->ob_type->tp_as_mapping->mp_length)(v);
Guido van Rossum1c4f4581998-05-22 00:53:24 +0000718 else if (v->ob_type->tp_as_sequence != NULL &&
719 v->ob_type->tp_as_sequence->sq_length != NULL)
Guido van Rossum6ac258d1993-05-12 08:24:20 +0000720 res = (*v->ob_type->tp_as_sequence->sq_length)(v);
721 else
722 res = 1;
723 if (res > 0)
724 res = 1;
725 return res;
726}
727
Guido van Rossumc3d3f961998-04-09 17:53:59 +0000728/* equivalent of 'not v'
729 Return -1 if an error occurred */
730
731int
Fred Drake100814d2000-07-09 15:48:49 +0000732PyObject_Not(PyObject *v)
Guido van Rossumc3d3f961998-04-09 17:53:59 +0000733{
734 int res;
735 res = PyObject_IsTrue(v);
736 if (res < 0)
737 return res;
738 return res == 0;
739}
740
Guido van Rossum5524a591995-01-10 15:26:20 +0000741/* Coerce two numeric types to the "larger" one.
742 Increment the reference count on each argument.
743 Return -1 and raise an exception if no coercion is possible
744 (and then no reference count is incremented).
745*/
746
747int
Fred Drake100814d2000-07-09 15:48:49 +0000748PyNumber_CoerceEx(PyObject **pv, PyObject **pw)
Guido van Rossum5524a591995-01-10 15:26:20 +0000749{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000750 register PyObject *v = *pv;
751 register PyObject *w = *pw;
Guido van Rossum5524a591995-01-10 15:26:20 +0000752 int res;
753
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000754 if (v->ob_type == w->ob_type && !PyInstance_Check(v)) {
755 Py_INCREF(v);
756 Py_INCREF(w);
Guido van Rossum5524a591995-01-10 15:26:20 +0000757 return 0;
758 }
759 if (v->ob_type->tp_as_number && v->ob_type->tp_as_number->nb_coerce) {
760 res = (*v->ob_type->tp_as_number->nb_coerce)(pv, pw);
761 if (res <= 0)
762 return res;
763 }
764 if (w->ob_type->tp_as_number && w->ob_type->tp_as_number->nb_coerce) {
765 res = (*w->ob_type->tp_as_number->nb_coerce)(pw, pv);
766 if (res <= 0)
767 return res;
768 }
Guido van Rossum242c6421997-11-19 16:03:17 +0000769 return 1;
770}
771
772int
Fred Drake100814d2000-07-09 15:48:49 +0000773PyNumber_Coerce(PyObject **pv, PyObject **pw)
Guido van Rossum242c6421997-11-19 16:03:17 +0000774{
775 int err = PyNumber_CoerceEx(pv, pw);
776 if (err <= 0)
777 return err;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000778 PyErr_SetString(PyExc_TypeError, "number coercion failed");
Guido van Rossum5524a591995-01-10 15:26:20 +0000779 return -1;
780}
781
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000782
Guido van Rossum49b11fe1995-01-26 00:38:22 +0000783/* Test whether an object can be called */
784
785int
Fred Drake100814d2000-07-09 15:48:49 +0000786PyCallable_Check(PyObject *x)
Guido van Rossum49b11fe1995-01-26 00:38:22 +0000787{
788 if (x == NULL)
789 return 0;
790 if (x->ob_type->tp_call != NULL ||
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000791 PyFunction_Check(x) ||
792 PyMethod_Check(x) ||
793 PyCFunction_Check(x) ||
794 PyClass_Check(x))
Guido van Rossum49b11fe1995-01-26 00:38:22 +0000795 return 1;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000796 if (PyInstance_Check(x)) {
797 PyObject *call = PyObject_GetAttrString(x, "__call__");
Guido van Rossum49b11fe1995-01-26 00:38:22 +0000798 if (call == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000799 PyErr_Clear();
Guido van Rossum49b11fe1995-01-26 00:38:22 +0000800 return 0;
801 }
802 /* Could test recursively but don't, for fear of endless
803 recursion if some joker sets self.__call__ = self */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000804 Py_DECREF(call);
Guido van Rossum49b11fe1995-01-26 00:38:22 +0000805 return 1;
806 }
807 return 0;
808}
809
810
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000811/*
812NoObject is usable as a non-NULL undefined value, used by the macro None.
813There is (and should be!) no way to create other objects of this type,
Guido van Rossum3f5da241990-12-20 15:06:42 +0000814so there is exactly one (which is indestructible, by the way).
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000815*/
816
Guido van Rossum0c182a11992-03-27 17:26:13 +0000817/* ARGSUSED */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000818static PyObject *
Fred Drake100814d2000-07-09 15:48:49 +0000819none_repr(PyObject *op)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000820{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000821 return PyString_FromString("None");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000822}
823
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000824static PyTypeObject PyNothing_Type = {
825 PyObject_HEAD_INIT(&PyType_Type)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000826 0,
Guido van Rossum3f5da241990-12-20 15:06:42 +0000827 "None",
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000828 0,
829 0,
830 0, /*tp_dealloc*/ /*never called*/
Guido van Rossum7066dd71992-09-17 17:54:56 +0000831 0, /*tp_print*/
Guido van Rossum3f5da241990-12-20 15:06:42 +0000832 0, /*tp_getattr*/
833 0, /*tp_setattr*/
834 0, /*tp_compare*/
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000835 (reprfunc)none_repr, /*tp_repr*/
Guido van Rossum3f5da241990-12-20 15:06:42 +0000836 0, /*tp_as_number*/
837 0, /*tp_as_sequence*/
838 0, /*tp_as_mapping*/
Guido van Rossum9bfef441993-03-29 10:43:31 +0000839 0, /*tp_hash */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000840};
841
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000842PyObject _Py_NoneStruct = {
843 PyObject_HEAD_INIT(&PyNothing_Type)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000844};
845
846
Guido van Rossum84a90321996-05-22 16:34:47 +0000847#ifdef Py_TRACE_REFS
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000848
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000849static PyObject refchain = {&refchain, &refchain};
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000850
Guido van Rossumaacdc9d1996-08-12 21:32:12 +0000851void
Fred Drake100814d2000-07-09 15:48:49 +0000852_Py_ResetReferences(void)
Guido van Rossume09fb551997-08-05 02:04:34 +0000853{
854 refchain._ob_prev = refchain._ob_next = &refchain;
855 _Py_RefTotal = 0;
856}
857
858void
Fred Drake100814d2000-07-09 15:48:49 +0000859_Py_NewReference(PyObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000860{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000861 _Py_RefTotal++;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000862 op->ob_refcnt = 1;
863 op->_ob_next = refchain._ob_next;
864 op->_ob_prev = &refchain;
865 refchain._ob_next->_ob_prev = op;
866 refchain._ob_next = op;
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +0000867#ifdef COUNT_ALLOCS
868 inc_count(op->ob_type);
869#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000870}
871
Guido van Rossumaacdc9d1996-08-12 21:32:12 +0000872void
Fred Drake100814d2000-07-09 15:48:49 +0000873_Py_ForgetReference(register PyObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000874{
Guido van Rossumbffd6832000-01-20 22:32:56 +0000875#ifdef SLOW_UNREF_CHECK
Guido van Rossum4c08d552000-03-10 22:55:18 +0000876 register PyObject *p;
Guido van Rossumbffd6832000-01-20 22:32:56 +0000877#endif
Guido van Rossumd7047b31995-01-02 19:07:15 +0000878 if (op->ob_refcnt < 0)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000879 Py_FatalError("UNREF negative refcnt");
Guido van Rossum2e8f6141992-09-03 20:32:55 +0000880 if (op == &refchain ||
Guido van Rossumd7047b31995-01-02 19:07:15 +0000881 op->_ob_prev->_ob_next != op || op->_ob_next->_ob_prev != op)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000882 Py_FatalError("UNREF invalid object");
Guido van Rossum2e8f6141992-09-03 20:32:55 +0000883#ifdef SLOW_UNREF_CHECK
Guido van Rossum3f5da241990-12-20 15:06:42 +0000884 for (p = refchain._ob_next; p != &refchain; p = p->_ob_next) {
885 if (p == op)
886 break;
887 }
Guido van Rossumd7047b31995-01-02 19:07:15 +0000888 if (p == &refchain) /* Not found */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000889 Py_FatalError("UNREF unknown object");
Guido van Rossum2e8f6141992-09-03 20:32:55 +0000890#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000891 op->_ob_next->_ob_prev = op->_ob_prev;
892 op->_ob_prev->_ob_next = op->_ob_next;
Guido van Rossum2e8f6141992-09-03 20:32:55 +0000893 op->_ob_next = op->_ob_prev = NULL;
Guido van Rossumd8953cb1995-04-06 14:46:26 +0000894#ifdef COUNT_ALLOCS
895 op->ob_type->tp_free++;
896#endif
Guido van Rossum3f5da241990-12-20 15:06:42 +0000897}
898
Guido van Rossumaacdc9d1996-08-12 21:32:12 +0000899void
Fred Drake100814d2000-07-09 15:48:49 +0000900_Py_Dealloc(PyObject *op)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000901{
Guido van Rossum9776adf1994-09-07 14:36:45 +0000902 destructor dealloc = op->ob_type->tp_dealloc;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000903 _Py_ForgetReference(op);
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000904#ifndef WITH_CYCLE_GC
Guido van Rossume92e6102000-04-24 15:40:53 +0000905 if (_PyTrash_delete_nesting < PyTrash_UNWIND_LEVEL-1)
906 op->ob_type = NULL;
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000907#endif
Guido van Rossum9776adf1994-09-07 14:36:45 +0000908 (*dealloc)(op);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000909}
910
Guido van Rossumaacdc9d1996-08-12 21:32:12 +0000911void
Fred Drake100814d2000-07-09 15:48:49 +0000912_Py_PrintReferences(FILE *fp)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000913{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000914 PyObject *op;
Guido van Rossume09fb551997-08-05 02:04:34 +0000915 fprintf(fp, "Remaining objects:\n");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000916 for (op = refchain._ob_next; op != &refchain; op = op->_ob_next) {
917 fprintf(fp, "[%d] ", op->ob_refcnt);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000918 if (PyObject_Print(op, fp, 0) != 0)
919 PyErr_Clear();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000920 putc('\n', fp);
921 }
922}
923
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000924PyObject *
Fred Drake100814d2000-07-09 15:48:49 +0000925_Py_GetObjects(PyObject *self, PyObject *args)
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000926{
927 int i, n;
928 PyObject *t = NULL;
929 PyObject *res, *op;
930
931 if (!PyArg_ParseTuple(args, "i|O", &n, &t))
932 return NULL;
933 op = refchain._ob_next;
934 res = PyList_New(0);
935 if (res == NULL)
936 return NULL;
937 for (i = 0; (n == 0 || i < n) && op != &refchain; i++) {
938 while (op == self || op == args || op == res || op == t ||
939 t != NULL && op->ob_type != (PyTypeObject *) t) {
940 op = op->_ob_next;
941 if (op == &refchain)
942 return res;
943 }
944 if (PyList_Append(res, op) < 0) {
945 Py_DECREF(res);
946 return NULL;
947 }
948 op = op->_ob_next;
949 }
950 return res;
951}
952
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000953#endif
Guido van Rossum97ead3f1996-01-12 01:24:09 +0000954
955
956/* Hack to force loading of cobject.o */
Guido van Rossumda9c2711996-12-05 21:58:58 +0000957PyTypeObject *_Py_cobject_hack = &PyCObject_Type;
Guido van Rossum84a90321996-05-22 16:34:47 +0000958
959
960/* Hack to force loading of abstract.o */
Jeremy Hylton6253f832000-07-12 12:56:19 +0000961int (*_Py_abstract_hack)(PyObject *) = &PyObject_Size;
Guido van Rossume09fb551997-08-05 02:04:34 +0000962
963
Guido van Rossumb18618d2000-05-03 23:44:39 +0000964/* Python's malloc wrappers (see mymalloc.h) */
Guido van Rossume09fb551997-08-05 02:04:34 +0000965
966ANY *
Fred Drake100814d2000-07-09 15:48:49 +0000967PyMem_Malloc(size_t nbytes)
Guido van Rossume09fb551997-08-05 02:04:34 +0000968{
969#if _PyMem_EXTRA > 0
970 if (nbytes == 0)
971 nbytes = _PyMem_EXTRA;
972#endif
Guido van Rossumb18618d2000-05-03 23:44:39 +0000973 return PyMem_MALLOC(nbytes);
Guido van Rossume09fb551997-08-05 02:04:34 +0000974}
975
976ANY *
Fred Drake100814d2000-07-09 15:48:49 +0000977PyMem_Realloc(ANY *p, size_t nbytes)
Guido van Rossume09fb551997-08-05 02:04:34 +0000978{
979#if _PyMem_EXTRA > 0
980 if (nbytes == 0)
981 nbytes = _PyMem_EXTRA;
982#endif
Guido van Rossumb18618d2000-05-03 23:44:39 +0000983 return PyMem_REALLOC(p, nbytes);
Guido van Rossume09fb551997-08-05 02:04:34 +0000984}
985
986void
Fred Drake100814d2000-07-09 15:48:49 +0000987PyMem_Free(ANY *p)
Guido van Rossume09fb551997-08-05 02:04:34 +0000988{
Guido van Rossumb18618d2000-05-03 23:44:39 +0000989 PyMem_FREE(p);
990}
991
992
993/* Python's object malloc wrappers (see objimpl.h) */
994
995ANY *
Fred Drake100814d2000-07-09 15:48:49 +0000996PyObject_Malloc(size_t nbytes)
Guido van Rossumb18618d2000-05-03 23:44:39 +0000997{
998 return PyObject_MALLOC(nbytes);
999}
1000
1001ANY *
Fred Drake100814d2000-07-09 15:48:49 +00001002PyObject_Realloc(ANY *p, size_t nbytes)
Guido van Rossumb18618d2000-05-03 23:44:39 +00001003{
1004 return PyObject_REALLOC(p, nbytes);
1005}
1006
1007void
Fred Drake100814d2000-07-09 15:48:49 +00001008PyObject_Free(ANY *p)
Guido van Rossumb18618d2000-05-03 23:44:39 +00001009{
1010 PyObject_FREE(p);
Guido van Rossume09fb551997-08-05 02:04:34 +00001011}
Guido van Rossum86610361998-04-10 22:32:46 +00001012
1013
1014/* These methods are used to control infinite recursion in repr, str, print,
1015 etc. Container objects that may recursively contain themselves,
1016 e.g. builtin dictionaries and lists, should used Py_ReprEnter() and
1017 Py_ReprLeave() to avoid infinite recursion.
1018
1019 Py_ReprEnter() returns 0 the first time it is called for a particular
1020 object and 1 every time thereafter. It returns -1 if an exception
1021 occurred. Py_ReprLeave() has no return value.
1022
1023 See dictobject.c and listobject.c for examples of use.
1024*/
1025
1026#define KEY "Py_Repr"
1027
1028int
Fred Drake100814d2000-07-09 15:48:49 +00001029Py_ReprEnter(PyObject *obj)
Guido van Rossum86610361998-04-10 22:32:46 +00001030{
1031 PyObject *dict;
1032 PyObject *list;
1033 int i;
1034
1035 dict = PyThreadState_GetDict();
1036 if (dict == NULL)
1037 return -1;
1038 list = PyDict_GetItemString(dict, KEY);
1039 if (list == NULL) {
1040 list = PyList_New(0);
1041 if (list == NULL)
1042 return -1;
1043 if (PyDict_SetItemString(dict, KEY, list) < 0)
1044 return -1;
1045 Py_DECREF(list);
1046 }
1047 i = PyList_GET_SIZE(list);
1048 while (--i >= 0) {
1049 if (PyList_GET_ITEM(list, i) == obj)
1050 return 1;
1051 }
1052 PyList_Append(list, obj);
1053 return 0;
1054}
1055
1056void
Fred Drake100814d2000-07-09 15:48:49 +00001057Py_ReprLeave(PyObject *obj)
Guido van Rossum86610361998-04-10 22:32:46 +00001058{
1059 PyObject *dict;
1060 PyObject *list;
1061 int i;
1062
1063 dict = PyThreadState_GetDict();
Guido van Rossumeb909461998-04-11 15:17:34 +00001064 if (dict == NULL)
1065 return;
Guido van Rossum86610361998-04-10 22:32:46 +00001066 list = PyDict_GetItemString(dict, KEY);
Guido van Rossumeb909461998-04-11 15:17:34 +00001067 if (list == NULL || !PyList_Check(list))
1068 return;
Guido van Rossum86610361998-04-10 22:32:46 +00001069 i = PyList_GET_SIZE(list);
1070 /* Count backwards because we always expect obj to be list[-1] */
1071 while (--i >= 0) {
1072 if (PyList_GET_ITEM(list, i) == obj) {
1073 PyList_SetSlice(list, i, i + 1, NULL);
1074 break;
1075 }
1076 }
1077}
Guido van Rossumd724b232000-03-13 16:01:29 +00001078
1079/*
1080 trashcan
1081 CT 2k0130
1082 non-recursively destroy nested objects
1083
1084 CT 2k0223
1085 everything is now done in a macro.
1086
1087 CT 2k0305
1088 modified to use functions, after Tim Peter's suggestion.
1089
1090 CT 2k0309
1091 modified to restore a possible error.
Guido van Rossum13ff8eb2000-03-25 18:39:19 +00001092
1093 CT 2k0325
1094 added better safe than sorry check for threadstate
Guido van Rossume92e6102000-04-24 15:40:53 +00001095
1096 CT 2k0422
1097 complete rewrite. We now build a chain via ob_type
1098 and save the limited number of types in ob_refcnt.
1099 This is perfect since we don't need any memory.
1100 A patch for free-threading would need just a lock.
Guido van Rossumd724b232000-03-13 16:01:29 +00001101*/
1102
Guido van Rossume92e6102000-04-24 15:40:53 +00001103#define Py_TRASHCAN_TUPLE 1
1104#define Py_TRASHCAN_LIST 2
1105#define Py_TRASHCAN_DICT 3
1106#define Py_TRASHCAN_FRAME 4
1107#define Py_TRASHCAN_TRACEBACK 5
1108/* extend here if other objects want protection */
1109
Guido van Rossumd724b232000-03-13 16:01:29 +00001110int _PyTrash_delete_nesting = 0;
Guido van Rossume92e6102000-04-24 15:40:53 +00001111
Guido van Rossumd724b232000-03-13 16:01:29 +00001112PyObject * _PyTrash_delete_later = NULL;
1113
1114void
Fred Drake100814d2000-07-09 15:48:49 +00001115_PyTrash_deposit_object(PyObject *op)
Guido van Rossumd724b232000-03-13 16:01:29 +00001116{
Guido van Rossume92e6102000-04-24 15:40:53 +00001117 int typecode;
Guido van Rossum13ff8eb2000-03-25 18:39:19 +00001118
Guido van Rossume92e6102000-04-24 15:40:53 +00001119 if (PyTuple_Check(op))
1120 typecode = Py_TRASHCAN_TUPLE;
1121 else if (PyList_Check(op))
1122 typecode = Py_TRASHCAN_LIST;
1123 else if (PyDict_Check(op))
1124 typecode = Py_TRASHCAN_DICT;
1125 else if (PyFrame_Check(op))
1126 typecode = Py_TRASHCAN_FRAME;
1127 else if (PyTraceBack_Check(op))
1128 typecode = Py_TRASHCAN_TRACEBACK;
1129 op->ob_refcnt = typecode;
Guido van Rossumd724b232000-03-13 16:01:29 +00001130
Guido van Rossume92e6102000-04-24 15:40:53 +00001131 op->ob_type = (PyTypeObject*)_PyTrash_delete_later;
1132 _PyTrash_delete_later = op;
Guido van Rossumd724b232000-03-13 16:01:29 +00001133}
1134
1135void
Fred Drake100814d2000-07-09 15:48:49 +00001136_PyTrash_destroy_chain(void)
Guido van Rossumd724b232000-03-13 16:01:29 +00001137{
1138 while (_PyTrash_delete_later) {
1139 PyObject *shredder = _PyTrash_delete_later;
Guido van Rossume92e6102000-04-24 15:40:53 +00001140 _PyTrash_delete_later = (PyObject*) shredder->ob_type;
1141
1142 switch (shredder->ob_refcnt) {
1143 case Py_TRASHCAN_TUPLE:
1144 shredder->ob_type = &PyTuple_Type;
1145 break;
1146 case Py_TRASHCAN_LIST:
1147 shredder->ob_type = &PyList_Type;
1148 break;
1149 case Py_TRASHCAN_DICT:
1150 shredder->ob_type = &PyDict_Type;
1151 break;
1152 case Py_TRASHCAN_FRAME:
1153 shredder->ob_type = &PyFrame_Type;
1154 break;
1155 case Py_TRASHCAN_TRACEBACK:
1156 shredder->ob_type = &PyTraceBack_Type;
1157 break;
1158 }
1159 _Py_NewReference(shredder);
1160
Guido van Rossumd724b232000-03-13 16:01:29 +00001161 ++_PyTrash_delete_nesting;
1162 Py_DECREF(shredder);
1163 --_PyTrash_delete_nesting;
1164 }
1165}