blob: 4479e7dba12f0c046dc61a96e8a16324d243a07e [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
Fred Drake13634cf2000-06-29 19:17:04 +000015#include "mymath.h"
16
Guido van Rossume92e6102000-04-24 15:40:53 +000017/* just for trashcan: */
18#include "compile.h"
19#include "frameobject.h"
20#include "traceback.h"
21
Guido van Rossum6f9e4331995-03-29 16:57:48 +000022#if defined( Py_TRACE_REFS ) || defined( Py_REF_DEBUG )
Guido van Rossumbffd6832000-01-20 22:32:56 +000023DL_IMPORT(long) _Py_RefTotal;
Guido van Rossum3f5da241990-12-20 15:06:42 +000024#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000025
Guido van Rossum3f5da241990-12-20 15:06:42 +000026/* Object allocation routines used by NEWOBJ and NEWVAROBJ macros.
27 These are used by the individual routines for object creation.
28 Do not call them otherwise, they do not initialize the object! */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000029
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +000030#ifdef COUNT_ALLOCS
Guido van Rossumc0b618a1997-05-02 03:12:38 +000031static PyTypeObject *type_list;
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +000032extern int tuple_zero_allocs, fast_tuple_allocs;
33extern int quick_int_allocs, quick_neg_int_allocs;
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +000034extern int null_strings, one_strings;
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +000035void
36dump_counts()
37{
Guido van Rossumc0b618a1997-05-02 03:12:38 +000038 PyTypeObject *tp;
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +000039
40 for (tp = type_list; tp; tp = tp->tp_next)
Sjoerd Mullender52c1f511993-10-25 08:40:52 +000041 fprintf(stderr, "%s alloc'd: %d, freed: %d, max in use: %d\n",
42 tp->tp_name, tp->tp_alloc, tp->tp_free,
43 tp->tp_maxalloc);
44 fprintf(stderr, "fast tuple allocs: %d, empty: %d\n",
45 fast_tuple_allocs, tuple_zero_allocs);
46 fprintf(stderr, "fast int allocs: pos: %d, neg: %d\n",
47 quick_int_allocs, quick_neg_int_allocs);
48 fprintf(stderr, "null strings: %d, 1-strings: %d\n",
49 null_strings, one_strings);
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +000050}
51
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +000052PyObject *
53get_counts()
54{
55 PyTypeObject *tp;
56 PyObject *result;
57 PyObject *v;
58
59 result = PyList_New(0);
60 if (result == NULL)
61 return NULL;
62 for (tp = type_list; tp; tp = tp->tp_next) {
63 v = Py_BuildValue("(siii)", tp->tp_name, tp->tp_alloc,
64 tp->tp_free, tp->tp_maxalloc);
65 if (v == NULL) {
66 Py_DECREF(result);
67 return NULL;
68 }
69 if (PyList_Append(result, v) < 0) {
70 Py_DECREF(v);
71 Py_DECREF(result);
72 return NULL;
73 }
74 Py_DECREF(v);
75 }
76 return result;
77}
78
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +000079void
80inc_count(tp)
Guido van Rossumc0b618a1997-05-02 03:12:38 +000081 PyTypeObject *tp;
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +000082{
83 if (tp->tp_alloc == 0) {
Guido van Rossumd8953cb1995-04-06 14:46:26 +000084 /* first time; insert in linked list */
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +000085 if (tp->tp_next != NULL) /* sanity check */
Guido van Rossumc0b618a1997-05-02 03:12:38 +000086 Py_FatalError("XXX inc_count sanity check");
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +000087 tp->tp_next = type_list;
88 type_list = tp;
89 }
90 tp->tp_alloc++;
91 if (tp->tp_alloc - tp->tp_free > tp->tp_maxalloc)
92 tp->tp_maxalloc = tp->tp_alloc - tp->tp_free;
93}
94#endif
95
Guido van Rossumc0b618a1997-05-02 03:12:38 +000096PyObject *
Guido van Rossumb18618d2000-05-03 23:44:39 +000097PyObject_Init(op, tp)
Guido van Rossumf5030ab1996-07-21 02:30:39 +000098 PyObject *op;
Guido van Rossumb18618d2000-05-03 23:44:39 +000099 PyTypeObject *tp;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000100{
Guido van Rossumb18618d2000-05-03 23:44:39 +0000101 if (op == NULL) {
102 PyErr_SetString(PyExc_SystemError,
103 "NULL object passed to PyObject_Init");
104 return op;
105 }
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000106#ifdef WITH_CYCLE_GC
107 if (PyType_IS_GC(tp))
108 op = (PyObject *) PyObject_FROM_GC(op);
109#endif
Guido van Rossumb18618d2000-05-03 23:44:39 +0000110 /* Any changes should be reflected in PyObject_INIT (objimpl.h) */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000111 op->ob_type = tp;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000112 _Py_NewReference(op);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000113 return op;
114}
115
Guido van Rossumb18618d2000-05-03 23:44:39 +0000116PyVarObject *
117PyObject_InitVar(op, tp, size)
118 PyVarObject *op;
119 PyTypeObject *tp;
120 int size;
121{
122 if (op == NULL) {
123 PyErr_SetString(PyExc_SystemError,
124 "NULL object passed to PyObject_InitVar");
125 return op;
126 }
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000127#ifdef WITH_CYCLE_GC
128 if (PyType_IS_GC(tp))
129 op = (PyVarObject *) PyObject_FROM_GC(op);
130#endif
Guido van Rossumb18618d2000-05-03 23:44:39 +0000131 /* Any changes should be reflected in PyObject_INIT_VAR */
132 op->ob_size = size;
133 op->ob_type = tp;
134 _Py_NewReference((PyObject *)op);
135 return op;
136}
137
138PyObject *
139_PyObject_New(tp)
140 PyTypeObject *tp;
141{
142 PyObject *op;
143 op = (PyObject *) PyObject_MALLOC(_PyObject_SIZE(tp));
144 if (op == NULL)
145 return PyErr_NoMemory();
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000146#ifdef WITH_CYCLE_GC
147 if (PyType_IS_GC(tp))
148 op = (PyObject *) PyObject_FROM_GC(op);
149#endif
Guido van Rossumb18618d2000-05-03 23:44:39 +0000150 return PyObject_INIT(op, tp);
151}
152
Guido van Rossumd0c87ee1997-05-15 21:31:03 +0000153PyVarObject *
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000154_PyObject_NewVar(tp, size)
155 PyTypeObject *tp;
Guido van Rossum2497ead1995-02-10 17:00:27 +0000156 int size;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000157{
Guido van Rossumb18618d2000-05-03 23:44:39 +0000158 PyVarObject *op;
159 op = (PyVarObject *) PyObject_MALLOC(_PyObject_VAR_SIZE(tp, size));
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000160 if (op == NULL)
Guido van Rossumd0c87ee1997-05-15 21:31:03 +0000161 return (PyVarObject *)PyErr_NoMemory();
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000162#ifdef WITH_CYCLE_GC
163 if (PyType_IS_GC(tp))
164 op = (PyVarObject *) PyObject_FROM_GC(op);
165#endif
Guido van Rossumb18618d2000-05-03 23:44:39 +0000166 return PyObject_INIT_VAR(op, tp, size);
167}
168
169void
170_PyObject_Del(op)
171 PyObject *op;
172{
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000173#ifdef WITH_CYCLE_GC
174 if (PyType_IS_GC(op->ob_type)) {
175 PyGC_Head *g = PyObject_AS_GC(op);
176 PyObject_FREE(g);
177 } else
178#endif
179 {
180 PyObject_FREE(op);
181 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000182}
183
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000184#ifndef WITH_CYCLE_GC
185/* extension modules might need these */
186void _PyGC_Insert(PyObject *op) { }
187void _PyGC_Remove(PyObject *op) { }
188#endif
189
Guido van Rossum90933611991-06-07 16:10:43 +0000190int
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000191PyObject_Print(op, fp, flags)
192 PyObject *op;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000193 FILE *fp;
194 int flags;
195{
Guido van Rossum278ef591991-07-27 21:40:24 +0000196 int ret = 0;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000197 if (PyErr_CheckSignals())
Guido van Rossum90933611991-06-07 16:10:43 +0000198 return -1;
Guido van Rossum9b00dfa1998-04-28 16:06:54 +0000199#ifdef USE_STACKCHECK
200 if (PyOS_CheckStack()) {
201 PyErr_SetString(PyExc_MemoryError, "Stack overflow");
202 return -1;
203 }
204#endif
Guido van Rossum687ef6e2000-01-12 16:28:58 +0000205 clearerr(fp); /* Clear any previous error condition */
Guido van Rossum90933611991-06-07 16:10:43 +0000206 if (op == NULL) {
207 fprintf(fp, "<nil>");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000208 }
Guido van Rossum90933611991-06-07 16:10:43 +0000209 else {
210 if (op->ob_refcnt <= 0)
Fred Drakea44d3532000-06-30 15:01:00 +0000211 fprintf(fp, "<refcnt %u at %p>",
212 op->ob_refcnt, op);
Guido van Rossum2e8f6141992-09-03 20:32:55 +0000213 else if (op->ob_type->tp_print == NULL) {
214 if (op->ob_type->tp_repr == NULL) {
Fred Drakea44d3532000-06-30 15:01:00 +0000215 fprintf(fp, "<%s object at %p>",
216 op->ob_type->tp_name, op);
Guido van Rossum2e8f6141992-09-03 20:32:55 +0000217 }
218 else {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000219 PyObject *s;
220 if (flags & Py_PRINT_RAW)
221 s = PyObject_Str(op);
Guido van Rossumc6004111993-11-05 10:22:19 +0000222 else
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000223 s = PyObject_Repr(op);
Guido van Rossum2e8f6141992-09-03 20:32:55 +0000224 if (s == NULL)
225 ret = -1;
Guido van Rossum2e8f6141992-09-03 20:32:55 +0000226 else {
Guido van Rossum565798d1998-04-21 22:25:01 +0000227 ret = PyObject_Print(s, fp,
228 Py_PRINT_RAW);
Guido van Rossum2e8f6141992-09-03 20:32:55 +0000229 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000230 Py_XDECREF(s);
Guido van Rossum2e8f6141992-09-03 20:32:55 +0000231 }
232 }
Guido van Rossum90933611991-06-07 16:10:43 +0000233 else
Guido van Rossum278ef591991-07-27 21:40:24 +0000234 ret = (*op->ob_type->tp_print)(op, fp, flags);
Guido van Rossum90933611991-06-07 16:10:43 +0000235 }
Guido van Rossum278ef591991-07-27 21:40:24 +0000236 if (ret == 0) {
237 if (ferror(fp)) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000238 PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossum278ef591991-07-27 21:40:24 +0000239 clearerr(fp);
240 ret = -1;
241 }
242 }
243 return ret;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000244}
245
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000246PyObject *
247PyObject_Repr(v)
248 PyObject *v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000249{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000250 if (PyErr_CheckSignals())
Guido van Rossum90933611991-06-07 16:10:43 +0000251 return NULL;
Guido van Rossum9b00dfa1998-04-28 16:06:54 +0000252#ifdef USE_STACKCHECK
253 if (PyOS_CheckStack()) {
254 PyErr_SetString(PyExc_MemoryError, "Stack overflow");
255 return NULL;
256 }
257#endif
Guido van Rossum90933611991-06-07 16:10:43 +0000258 if (v == NULL)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000259 return PyString_FromString("<NULL>");
Guido van Rossum90933611991-06-07 16:10:43 +0000260 else if (v->ob_type->tp_repr == NULL) {
261 char buf[120];
Fred Drakea44d3532000-06-30 15:01:00 +0000262 sprintf(buf, "<%.80s object at %p>",
263 v->ob_type->tp_name, v);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000264 return PyString_FromString(buf);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000265 }
Guido van Rossum4c08d552000-03-10 22:55:18 +0000266 else {
267 PyObject *res;
268 res = (*v->ob_type->tp_repr)(v);
269 if (res == NULL)
270 return NULL;
271 if (!PyString_Check(res)) {
272 PyErr_Format(PyExc_TypeError,
Guido van Rossum5db862d2000-04-10 12:46:51 +0000273 "__repr__ returned non-string (type %.200s)",
Guido van Rossum4c08d552000-03-10 22:55:18 +0000274 res->ob_type->tp_name);
275 Py_DECREF(res);
276 return NULL;
277 }
278 return res;
279 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000280}
281
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000282PyObject *
283PyObject_Str(v)
284 PyObject *v;
Guido van Rossumc6004111993-11-05 10:22:19 +0000285{
Guido van Rossum4c08d552000-03-10 22:55:18 +0000286 PyObject *res;
287
Guido van Rossumc6004111993-11-05 10:22:19 +0000288 if (v == NULL)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000289 return PyString_FromString("<NULL>");
290 else if (PyString_Check(v)) {
291 Py_INCREF(v);
Guido van Rossumc6004111993-11-05 10:22:19 +0000292 return v;
293 }
Guido van Rossum32b582b1995-01-17 16:35:13 +0000294 else if (v->ob_type->tp_str != NULL)
Guido van Rossum4c08d552000-03-10 22:55:18 +0000295 res = (*v->ob_type->tp_str)(v);
Guido van Rossumc6004111993-11-05 10:22:19 +0000296 else {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000297 PyObject *func;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000298 if (!PyInstance_Check(v) ||
299 (func = PyObject_GetAttrString(v, "__str__")) == NULL) {
300 PyErr_Clear();
301 return PyObject_Repr(v);
Guido van Rossumc6004111993-11-05 10:22:19 +0000302 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000303 res = PyEval_CallObject(func, (PyObject *)NULL);
304 Py_DECREF(func);
Guido van Rossumc6004111993-11-05 10:22:19 +0000305 }
Guido van Rossum4c08d552000-03-10 22:55:18 +0000306 if (res == NULL)
307 return NULL;
308 if (!PyString_Check(res)) {
309 PyErr_Format(PyExc_TypeError,
Guido van Rossum5db862d2000-04-10 12:46:51 +0000310 "__str__ returned non-string (type %.200s)",
Guido van Rossum4c08d552000-03-10 22:55:18 +0000311 res->ob_type->tp_name);
312 Py_DECREF(res);
313 return NULL;
314 }
315 return res;
Guido van Rossumc6004111993-11-05 10:22:19 +0000316}
317
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000318static PyObject *
Guido van Rossum20566841995-01-12 11:26:10 +0000319do_cmp(v, w)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000320 PyObject *v, *w;
Guido van Rossum20566841995-01-12 11:26:10 +0000321{
Guido van Rossumc8b6df91997-05-23 00:06:51 +0000322 long c;
Guido van Rossum20566841995-01-12 11:26:10 +0000323 /* __rcmp__ actually won't be called unless __cmp__ isn't defined,
324 because the check in cmpobject() reverses the objects first.
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000325 This is intentional -- it makes no sense to define cmp(x,y)
326 different than -cmp(y,x). */
327 if (PyInstance_Check(v) || PyInstance_Check(w))
328 return PyInstance_DoBinOp(v, w, "__cmp__", "__rcmp__", do_cmp);
Guido van Rossumc8b6df91997-05-23 00:06:51 +0000329 c = PyObject_Compare(v, w);
330 if (c && PyErr_Occurred())
331 return NULL;
332 return PyInt_FromLong(c);
Guido van Rossum20566841995-01-12 11:26:10 +0000333}
334
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000335PyObject *_PyCompareState_Key;
336
337/* _PyCompareState_nesting is incremented beforing call compare (for
338 some types) and decremented on exit. If the count exceeds the
339 nesting limit, enable code to detect circular data structures.
340*/
341#define NESTING_LIMIT 500
342int _PyCompareState_nesting = 0;
343
344static PyObject*
345get_inprogress_dict()
346{
347 PyObject *tstate_dict, *inprogress;
348
349 tstate_dict = PyThreadState_GetDict();
350 if (tstate_dict == NULL) {
351 PyErr_BadInternalCall();
352 return NULL;
353 }
354 inprogress = PyDict_GetItem(tstate_dict, _PyCompareState_Key);
355 if (inprogress == NULL) {
356 PyErr_Clear();
357 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 *
371make_pair(v, w)
372 PyObject *v, *w;
373{
374 PyObject *pair;
375
376 pair = PyTuple_New(2);
377 if (pair == NULL) {
378 return NULL;
379 }
Guido van Rossumad89bbc2000-06-28 21:57:18 +0000380 if (v <= w) {
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000381 PyTuple_SET_ITEM(pair, 0, PyLong_FromVoidPtr((void *)v));
382 PyTuple_SET_ITEM(pair, 1, PyLong_FromVoidPtr((void *)w));
383 } else {
384 PyTuple_SET_ITEM(pair, 0, PyLong_FromVoidPtr((void *)w));
385 PyTuple_SET_ITEM(pair, 1, PyLong_FromVoidPtr((void *)v));
386 }
387 return pair;
388}
389
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000390int
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000391PyObject_Compare(v, w)
392 PyObject *v, *w;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000393{
Guido van Rossumcd5a5f61998-06-09 18:58:44 +0000394 PyTypeObject *vtp, *wtp;
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000395 int result;
396
Guido van Rossumc8b6df91997-05-23 00:06:51 +0000397 if (v == NULL || w == NULL) {
398 PyErr_BadInternalCall();
399 return -1;
400 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000401 if (v == w)
402 return 0;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000403 if (PyInstance_Check(v) || PyInstance_Check(w)) {
404 PyObject *res;
Guido van Rossum20566841995-01-12 11:26:10 +0000405 int c;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000406 if (!PyInstance_Check(v))
407 return -PyObject_Compare(w, v);
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000408 if (++_PyCompareState_nesting > NESTING_LIMIT) {
409 PyObject *inprogress, *pair;
410
411 inprogress = get_inprogress_dict();
412 if (inprogress == NULL) {
413 return -1;
414 }
415 pair = make_pair(v, w);
416 if (PyDict_GetItem(inprogress, pair)) {
417 /* already comparing these objects. assume
418 they're equal until shown otherwise */
419 Py_DECREF(pair);
420 --_PyCompareState_nesting;
421 return 0;
422 }
423 if (PyDict_SetItem(inprogress, pair, pair) == -1) {
424 return -1;
425 }
426 res = do_cmp(v, w);
427 _PyCompareState_nesting--;
428 /* XXX DelItem shouldn't fail */
429 PyDict_DelItem(inprogress, pair);
430 Py_DECREF(pair);
431 } else {
432 res = do_cmp(v, w);
433 }
Guido van Rossumc8b6df91997-05-23 00:06:51 +0000434 if (res == NULL)
435 return -1;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000436 if (!PyInt_Check(res)) {
437 Py_DECREF(res);
Guido van Rossumc8b6df91997-05-23 00:06:51 +0000438 PyErr_SetString(PyExc_TypeError,
439 "comparison did not return an int");
440 return -1;
Guido van Rossum20566841995-01-12 11:26:10 +0000441 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000442 c = PyInt_AsLong(res);
443 Py_DECREF(res);
Guido van Rossum20566841995-01-12 11:26:10 +0000444 return (c < 0) ? -1 : (c > 0) ? 1 : 0;
445 }
Guido van Rossumcd5a5f61998-06-09 18:58:44 +0000446 if ((vtp = v->ob_type) != (wtp = w->ob_type)) {
447 char *vname = vtp->tp_name;
448 char *wname = wtp->tp_name;
Guido van Rossumb4db1941998-07-21 21:56:41 +0000449 if (vtp->tp_as_number != NULL && wtp->tp_as_number != NULL) {
Guido van Rossum242c6421997-11-19 16:03:17 +0000450 int err;
451 err = PyNumber_CoerceEx(&v, &w);
452 if (err < 0)
Guido van Rossumc8b6df91997-05-23 00:06:51 +0000453 return -1;
Guido van Rossum242c6421997-11-19 16:03:17 +0000454 else if (err == 0) {
Guido van Rossumb4db1941998-07-21 21:56:41 +0000455 int cmp;
456 vtp = v->ob_type;
457 if (vtp->tp_compare == NULL)
458 cmp = (v < w) ? -1 : 1;
459 else
460 cmp = (*vtp->tp_compare)(v, w);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000461 Py_DECREF(v);
462 Py_DECREF(w);
Guido van Rossum9fb03681991-07-01 18:48:04 +0000463 return cmp;
464 }
465 }
Guido van Rossumb244f692000-04-10 13:42:33 +0000466 else if (PyUnicode_Check(v) || PyUnicode_Check(w)) {
467 int result = PyUnicode_Compare(v, w);
468 if (result == -1 && PyErr_Occurred() &&
469 PyErr_ExceptionMatches(PyExc_TypeError))
470 /* TypeErrors are ignored: if Unicode coercion
471 fails due to one of the arguments not
472 having the right type, we continue as
473 defined by the coercion protocol (see
474 above). Luckily, decoding errors are
475 reported as ValueErrors and are not masked
476 by this technique. */
477 PyErr_Clear();
478 else
479 return result;
480 }
Guido van Rossumcd5a5f61998-06-09 18:58:44 +0000481 else if (vtp->tp_as_number != NULL)
482 vname = "";
483 else if (wtp->tp_as_number != NULL)
484 wname = "";
485 /* Numerical types compare smaller than all other types */
486 return strcmp(vname, wname);
Guido van Rossum9fb03681991-07-01 18:48:04 +0000487 }
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000488 if (vtp->tp_compare == NULL) {
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000489 return (v < w) ? -1 : 1;
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000490 }
491 if (++_PyCompareState_nesting > NESTING_LIMIT
492 && (vtp->tp_as_mapping
493 || (vtp->tp_as_sequence && !PyString_Check(v)))) {
494 PyObject *inprogress, *pair;
495
496 inprogress = get_inprogress_dict();
497 if (inprogress == NULL) {
498 return -1;
499 }
500 pair = make_pair(v, w);
501 if (PyDict_GetItem(inprogress, pair)) {
502 /* already comparing these objects. assume
503 they're equal until shown otherwise */
504 _PyCompareState_nesting--;
505 Py_DECREF(pair);
506 return 0;
507 }
508 if (PyDict_SetItem(inprogress, pair, pair) == -1) {
509 return -1;
510 }
511 result = (*vtp->tp_compare)(v, w);
512 _PyCompareState_nesting--;
513 PyDict_DelItem(inprogress, pair); /* XXX shouldn't fail */
514 Py_DECREF(pair);
515 } else {
516 result = (*vtp->tp_compare)(v, w);
517 }
518 return result;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000519}
520
Fred Drake13634cf2000-06-29 19:17:04 +0000521
522/* Set of hash utility functions to help maintaining the invariant that
523 iff a==b then hash(a)==hash(b)
524
525 All the utility functions (_Py_Hash*()) return "-1" to signify an error.
526*/
527
528long
529_Py_HashDouble(v)
530 double v;
531{
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
554_Py_HashPointer(p)
555 void *p;
556{
557#if SIZEOF_LONG >= SIZEOF_VOID_P
558 return (long)p;
559#else
560 /* convert to a Python long and hash that */
561 PyObject* longobj;
562 long x;
563
564 if ((longobj = PyLong_FromVoidPtr(p)) == NULL) {
565 x = -1;
566 goto finally;
567 }
568 x = PyObject_Hash(longobj);
569
570finally:
571 Py_XDECREF(longobj);
572 return x;
573#endif
574}
575
576
Guido van Rossum9bfef441993-03-29 10:43:31 +0000577long
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000578PyObject_Hash(v)
579 PyObject *v;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000580{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000581 PyTypeObject *tp = v->ob_type;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000582 if (tp->tp_hash != NULL)
583 return (*tp->tp_hash)(v);
Fred Drake13634cf2000-06-29 19:17:04 +0000584 if (tp->tp_compare == NULL) {
585 return _Py_HashPointer(v); /* Use address as hash value */
586 }
Guido van Rossum9bfef441993-03-29 10:43:31 +0000587 /* If there's a cmp but no hash defined, the object can't be hashed */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000588 PyErr_SetString(PyExc_TypeError, "unhashable type");
Guido van Rossum9bfef441993-03-29 10:43:31 +0000589 return -1;
590}
591
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000592PyObject *
593PyObject_GetAttrString(v, name)
594 PyObject *v;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000595 char *name;
596{
Guido van Rossumd8eb1b31996-08-09 20:52:03 +0000597 if (v->ob_type->tp_getattro != NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000598 PyObject *w, *res;
Guido van Rossumc6d06701997-01-18 07:57:16 +0000599 w = PyString_InternFromString(name);
Guido van Rossumd8eb1b31996-08-09 20:52:03 +0000600 if (w == NULL)
601 return NULL;
602 res = (*v->ob_type->tp_getattro)(v, w);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000603 Py_XDECREF(w);
Guido van Rossumd8eb1b31996-08-09 20:52:03 +0000604 return res;
605 }
606
Guido van Rossum3f5da241990-12-20 15:06:42 +0000607 if (v->ob_type->tp_getattr == NULL) {
Guido van Rossumdb935161998-01-19 22:16:36 +0000608 PyErr_Format(PyExc_AttributeError,
609 "'%.50s' object has no attribute '%.400s'",
610 v->ob_type->tp_name,
611 name);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000612 return NULL;
613 }
614 else {
615 return (*v->ob_type->tp_getattr)(v, name);
616 }
617}
618
619int
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000620PyObject_HasAttrString(v, name)
621 PyObject *v;
Guido van Rossumed18fdc1993-07-11 19:55:34 +0000622 char *name;
623{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000624 PyObject *res = PyObject_GetAttrString(v, name);
Guido van Rossumed18fdc1993-07-11 19:55:34 +0000625 if (res != NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000626 Py_DECREF(res);
Guido van Rossumed18fdc1993-07-11 19:55:34 +0000627 return 1;
628 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000629 PyErr_Clear();
Guido van Rossumed18fdc1993-07-11 19:55:34 +0000630 return 0;
631}
632
633int
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000634PyObject_SetAttrString(v, name, w)
635 PyObject *v;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000636 char *name;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000637 PyObject *w;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000638{
Guido van Rossumd8eb1b31996-08-09 20:52:03 +0000639 if (v->ob_type->tp_setattro != NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000640 PyObject *s;
Guido van Rossumd8eb1b31996-08-09 20:52:03 +0000641 int res;
Guido van Rossumc6d06701997-01-18 07:57:16 +0000642 s = PyString_InternFromString(name);
Guido van Rossumd8eb1b31996-08-09 20:52:03 +0000643 if (s == NULL)
Guido van Rossumb7fc3041996-09-11 22:51:25 +0000644 return -1;
Guido van Rossumd8eb1b31996-08-09 20:52:03 +0000645 res = (*v->ob_type->tp_setattro)(v, s, w);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000646 Py_XDECREF(s);
Guido van Rossumd8eb1b31996-08-09 20:52:03 +0000647 return res;
648 }
649
Guido van Rossum3f5da241990-12-20 15:06:42 +0000650 if (v->ob_type->tp_setattr == NULL) {
651 if (v->ob_type->tp_getattr == NULL)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000652 PyErr_SetString(PyExc_TypeError,
Guido van Rossum3ea74121991-12-24 13:28:03 +0000653 "attribute-less object (assign or del)");
Guido van Rossum3f5da241990-12-20 15:06:42 +0000654 else
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000655 PyErr_SetString(PyExc_TypeError,
Guido van Rossum3ea74121991-12-24 13:28:03 +0000656 "object has read-only attributes");
Guido van Rossum73531a31990-12-20 23:12:40 +0000657 return -1;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000658 }
659 else {
660 return (*v->ob_type->tp_setattr)(v, name, w);
661 }
662}
663
Guido van Rossum98ff96a1997-05-20 18:34:44 +0000664PyObject *
665PyObject_GetAttr(v, name)
666 PyObject *v;
667 PyObject *name;
668{
669 if (v->ob_type->tp_getattro != NULL)
670 return (*v->ob_type->tp_getattro)(v, name);
Jeremy Hylton99a8f902000-06-23 14:36:32 +0000671
672 if (!PyString_Check(name)) {
673 PyErr_SetString(PyExc_TypeError,
674 "attribute name must be string");
675 return NULL;
676 }
677 return PyObject_GetAttrString(v, PyString_AS_STRING(name));
Guido van Rossum98ff96a1997-05-20 18:34:44 +0000678}
679
680int
681PyObject_HasAttr(v, name)
682 PyObject *v;
683 PyObject *name;
684{
685 PyObject *res = PyObject_GetAttr(v, name);
686 if (res != NULL) {
687 Py_DECREF(res);
688 return 1;
689 }
690 PyErr_Clear();
691 return 0;
692}
693
694int
695PyObject_SetAttr(v, name, value)
696 PyObject *v;
697 PyObject *name;
698 PyObject *value;
699{
700 int err;
701 Py_INCREF(name);
Jeremy Hylton99a8f902000-06-23 14:36:32 +0000702 if (PyString_Check(name))
703 PyString_InternInPlace(&name);
Guido van Rossum98ff96a1997-05-20 18:34:44 +0000704 if (v->ob_type->tp_setattro != NULL)
705 err = (*v->ob_type->tp_setattro)(v, name, value);
Jeremy Hylton99a8f902000-06-23 14:36:32 +0000706 else if (PyString_Check(name)) {
Guido van Rossum98ff96a1997-05-20 18:34:44 +0000707 err = PyObject_SetAttrString(
Jeremy Hylton99a8f902000-06-23 14:36:32 +0000708 v, PyString_AS_STRING(name), value);
709 }
710 else {
711 PyErr_SetString(PyExc_TypeError,
712 "attribute name must be string");
713 err = -1;
714 }
Guido van Rossum98ff96a1997-05-20 18:34:44 +0000715 Py_DECREF(name);
716 return err;
717}
718
Guido van Rossum6ac258d1993-05-12 08:24:20 +0000719/* Test a value used as condition, e.g., in a for or if statement.
720 Return -1 if an error occurred */
721
722int
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000723PyObject_IsTrue(v)
724 PyObject *v;
Guido van Rossum6ac258d1993-05-12 08:24:20 +0000725{
726 int res;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000727 if (v == Py_None)
Guido van Rossum6ac258d1993-05-12 08:24:20 +0000728 res = 0;
Guido van Rossum1c4f4581998-05-22 00:53:24 +0000729 else if (v->ob_type->tp_as_number != NULL &&
730 v->ob_type->tp_as_number->nb_nonzero != NULL)
Guido van Rossum6ac258d1993-05-12 08:24:20 +0000731 res = (*v->ob_type->tp_as_number->nb_nonzero)(v);
Guido van Rossum1c4f4581998-05-22 00:53:24 +0000732 else if (v->ob_type->tp_as_mapping != NULL &&
733 v->ob_type->tp_as_mapping->mp_length != NULL)
Guido van Rossum6ac258d1993-05-12 08:24:20 +0000734 res = (*v->ob_type->tp_as_mapping->mp_length)(v);
Guido van Rossum1c4f4581998-05-22 00:53:24 +0000735 else if (v->ob_type->tp_as_sequence != NULL &&
736 v->ob_type->tp_as_sequence->sq_length != NULL)
Guido van Rossum6ac258d1993-05-12 08:24:20 +0000737 res = (*v->ob_type->tp_as_sequence->sq_length)(v);
738 else
739 res = 1;
740 if (res > 0)
741 res = 1;
742 return res;
743}
744
Guido van Rossumc3d3f961998-04-09 17:53:59 +0000745/* equivalent of 'not v'
746 Return -1 if an error occurred */
747
748int
749PyObject_Not(v)
750 PyObject *v;
751{
752 int res;
753 res = PyObject_IsTrue(v);
754 if (res < 0)
755 return res;
756 return res == 0;
757}
758
Guido van Rossum5524a591995-01-10 15:26:20 +0000759/* Coerce two numeric types to the "larger" one.
760 Increment the reference count on each argument.
761 Return -1 and raise an exception if no coercion is possible
762 (and then no reference count is incremented).
763*/
764
765int
Guido van Rossum242c6421997-11-19 16:03:17 +0000766PyNumber_CoerceEx(pv, pw)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000767 PyObject **pv, **pw;
Guido van Rossum5524a591995-01-10 15:26:20 +0000768{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000769 register PyObject *v = *pv;
770 register PyObject *w = *pw;
Guido van Rossum5524a591995-01-10 15:26:20 +0000771 int res;
772
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000773 if (v->ob_type == w->ob_type && !PyInstance_Check(v)) {
774 Py_INCREF(v);
775 Py_INCREF(w);
Guido van Rossum5524a591995-01-10 15:26:20 +0000776 return 0;
777 }
778 if (v->ob_type->tp_as_number && v->ob_type->tp_as_number->nb_coerce) {
779 res = (*v->ob_type->tp_as_number->nb_coerce)(pv, pw);
780 if (res <= 0)
781 return res;
782 }
783 if (w->ob_type->tp_as_number && w->ob_type->tp_as_number->nb_coerce) {
784 res = (*w->ob_type->tp_as_number->nb_coerce)(pw, pv);
785 if (res <= 0)
786 return res;
787 }
Guido van Rossum242c6421997-11-19 16:03:17 +0000788 return 1;
789}
790
791int
792PyNumber_Coerce(pv, pw)
793 PyObject **pv, **pw;
794{
795 int err = PyNumber_CoerceEx(pv, pw);
796 if (err <= 0)
797 return err;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000798 PyErr_SetString(PyExc_TypeError, "number coercion failed");
Guido van Rossum5524a591995-01-10 15:26:20 +0000799 return -1;
800}
801
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000802
Guido van Rossum49b11fe1995-01-26 00:38:22 +0000803/* Test whether an object can be called */
804
805int
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000806PyCallable_Check(x)
807 PyObject *x;
Guido van Rossum49b11fe1995-01-26 00:38:22 +0000808{
809 if (x == NULL)
810 return 0;
811 if (x->ob_type->tp_call != NULL ||
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000812 PyFunction_Check(x) ||
813 PyMethod_Check(x) ||
814 PyCFunction_Check(x) ||
815 PyClass_Check(x))
Guido van Rossum49b11fe1995-01-26 00:38:22 +0000816 return 1;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000817 if (PyInstance_Check(x)) {
818 PyObject *call = PyObject_GetAttrString(x, "__call__");
Guido van Rossum49b11fe1995-01-26 00:38:22 +0000819 if (call == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000820 PyErr_Clear();
Guido van Rossum49b11fe1995-01-26 00:38:22 +0000821 return 0;
822 }
823 /* Could test recursively but don't, for fear of endless
824 recursion if some joker sets self.__call__ = self */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000825 Py_DECREF(call);
Guido van Rossum49b11fe1995-01-26 00:38:22 +0000826 return 1;
827 }
828 return 0;
829}
830
831
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000832/*
833NoObject is usable as a non-NULL undefined value, used by the macro None.
834There is (and should be!) no way to create other objects of this type,
Guido van Rossum3f5da241990-12-20 15:06:42 +0000835so there is exactly one (which is indestructible, by the way).
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000836*/
837
Guido van Rossum0c182a11992-03-27 17:26:13 +0000838/* ARGSUSED */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000839static PyObject *
Guido van Rossum3f5da241990-12-20 15:06:42 +0000840none_repr(op)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000841 PyObject *op;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000842{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000843 return PyString_FromString("None");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000844}
845
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000846static PyTypeObject PyNothing_Type = {
847 PyObject_HEAD_INIT(&PyType_Type)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000848 0,
Guido van Rossum3f5da241990-12-20 15:06:42 +0000849 "None",
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000850 0,
851 0,
852 0, /*tp_dealloc*/ /*never called*/
Guido van Rossum7066dd71992-09-17 17:54:56 +0000853 0, /*tp_print*/
Guido van Rossum3f5da241990-12-20 15:06:42 +0000854 0, /*tp_getattr*/
855 0, /*tp_setattr*/
856 0, /*tp_compare*/
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000857 (reprfunc)none_repr, /*tp_repr*/
Guido van Rossum3f5da241990-12-20 15:06:42 +0000858 0, /*tp_as_number*/
859 0, /*tp_as_sequence*/
860 0, /*tp_as_mapping*/
Guido van Rossum9bfef441993-03-29 10:43:31 +0000861 0, /*tp_hash */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000862};
863
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000864PyObject _Py_NoneStruct = {
865 PyObject_HEAD_INIT(&PyNothing_Type)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000866};
867
868
Guido van Rossum84a90321996-05-22 16:34:47 +0000869#ifdef Py_TRACE_REFS
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000870
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000871static PyObject refchain = {&refchain, &refchain};
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000872
Guido van Rossumaacdc9d1996-08-12 21:32:12 +0000873void
Guido van Rossume09fb551997-08-05 02:04:34 +0000874_Py_ResetReferences()
875{
876 refchain._ob_prev = refchain._ob_next = &refchain;
877 _Py_RefTotal = 0;
878}
879
880void
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000881_Py_NewReference(op)
882 PyObject *op;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000883{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000884 _Py_RefTotal++;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000885 op->ob_refcnt = 1;
886 op->_ob_next = refchain._ob_next;
887 op->_ob_prev = &refchain;
888 refchain._ob_next->_ob_prev = op;
889 refchain._ob_next = op;
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +0000890#ifdef COUNT_ALLOCS
891 inc_count(op->ob_type);
892#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000893}
894
Guido van Rossumaacdc9d1996-08-12 21:32:12 +0000895void
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000896_Py_ForgetReference(op)
897 register PyObject *op;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000898{
Guido van Rossumbffd6832000-01-20 22:32:56 +0000899#ifdef SLOW_UNREF_CHECK
Guido van Rossum4c08d552000-03-10 22:55:18 +0000900 register PyObject *p;
Guido van Rossumbffd6832000-01-20 22:32:56 +0000901#endif
Guido van Rossumd7047b31995-01-02 19:07:15 +0000902 if (op->ob_refcnt < 0)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000903 Py_FatalError("UNREF negative refcnt");
Guido van Rossum2e8f6141992-09-03 20:32:55 +0000904 if (op == &refchain ||
Guido van Rossumd7047b31995-01-02 19:07:15 +0000905 op->_ob_prev->_ob_next != op || op->_ob_next->_ob_prev != op)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000906 Py_FatalError("UNREF invalid object");
Guido van Rossum2e8f6141992-09-03 20:32:55 +0000907#ifdef SLOW_UNREF_CHECK
Guido van Rossum3f5da241990-12-20 15:06:42 +0000908 for (p = refchain._ob_next; p != &refchain; p = p->_ob_next) {
909 if (p == op)
910 break;
911 }
Guido van Rossumd7047b31995-01-02 19:07:15 +0000912 if (p == &refchain) /* Not found */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000913 Py_FatalError("UNREF unknown object");
Guido van Rossum2e8f6141992-09-03 20:32:55 +0000914#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000915 op->_ob_next->_ob_prev = op->_ob_prev;
916 op->_ob_prev->_ob_next = op->_ob_next;
Guido van Rossum2e8f6141992-09-03 20:32:55 +0000917 op->_ob_next = op->_ob_prev = NULL;
Guido van Rossumd8953cb1995-04-06 14:46:26 +0000918#ifdef COUNT_ALLOCS
919 op->ob_type->tp_free++;
920#endif
Guido van Rossum3f5da241990-12-20 15:06:42 +0000921}
922
Guido van Rossumaacdc9d1996-08-12 21:32:12 +0000923void
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000924_Py_Dealloc(op)
925 PyObject *op;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000926{
Guido van Rossum9776adf1994-09-07 14:36:45 +0000927 destructor dealloc = op->ob_type->tp_dealloc;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000928 _Py_ForgetReference(op);
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000929#ifndef WITH_CYCLE_GC
Guido van Rossume92e6102000-04-24 15:40:53 +0000930 if (_PyTrash_delete_nesting < PyTrash_UNWIND_LEVEL-1)
931 op->ob_type = NULL;
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000932#endif
Guido van Rossum9776adf1994-09-07 14:36:45 +0000933 (*dealloc)(op);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000934}
935
Guido van Rossumaacdc9d1996-08-12 21:32:12 +0000936void
Guido van Rossumded690f1996-05-24 20:48:31 +0000937_Py_PrintReferences(fp)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000938 FILE *fp;
939{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000940 PyObject *op;
Guido van Rossume09fb551997-08-05 02:04:34 +0000941 fprintf(fp, "Remaining objects:\n");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000942 for (op = refchain._ob_next; op != &refchain; op = op->_ob_next) {
943 fprintf(fp, "[%d] ", op->ob_refcnt);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000944 if (PyObject_Print(op, fp, 0) != 0)
945 PyErr_Clear();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000946 putc('\n', fp);
947 }
948}
949
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000950PyObject *
Guido van Rossumded690f1996-05-24 20:48:31 +0000951_Py_GetObjects(self, args)
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000952 PyObject *self;
953 PyObject *args;
954{
955 int i, n;
956 PyObject *t = NULL;
957 PyObject *res, *op;
958
959 if (!PyArg_ParseTuple(args, "i|O", &n, &t))
960 return NULL;
961 op = refchain._ob_next;
962 res = PyList_New(0);
963 if (res == NULL)
964 return NULL;
965 for (i = 0; (n == 0 || i < n) && op != &refchain; i++) {
966 while (op == self || op == args || op == res || op == t ||
967 t != NULL && op->ob_type != (PyTypeObject *) t) {
968 op = op->_ob_next;
969 if (op == &refchain)
970 return res;
971 }
972 if (PyList_Append(res, op) < 0) {
973 Py_DECREF(res);
974 return NULL;
975 }
976 op = op->_ob_next;
977 }
978 return res;
979}
980
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000981#endif
Guido van Rossum97ead3f1996-01-12 01:24:09 +0000982
983
984/* Hack to force loading of cobject.o */
Guido van Rossumda9c2711996-12-05 21:58:58 +0000985PyTypeObject *_Py_cobject_hack = &PyCObject_Type;
Guido van Rossum84a90321996-05-22 16:34:47 +0000986
987
988/* Hack to force loading of abstract.o */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000989int (*_Py_abstract_hack) Py_FPROTO((PyObject *)) = &PyObject_Length;
Guido van Rossume09fb551997-08-05 02:04:34 +0000990
991
Guido van Rossumb18618d2000-05-03 23:44:39 +0000992/* Python's malloc wrappers (see mymalloc.h) */
Guido van Rossume09fb551997-08-05 02:04:34 +0000993
994ANY *
995PyMem_Malloc(nbytes)
996 size_t nbytes;
997{
998#if _PyMem_EXTRA > 0
999 if (nbytes == 0)
1000 nbytes = _PyMem_EXTRA;
1001#endif
Guido van Rossumb18618d2000-05-03 23:44:39 +00001002 return PyMem_MALLOC(nbytes);
Guido van Rossume09fb551997-08-05 02:04:34 +00001003}
1004
1005ANY *
1006PyMem_Realloc(p, nbytes)
1007 ANY *p;
1008 size_t nbytes;
1009{
1010#if _PyMem_EXTRA > 0
1011 if (nbytes == 0)
1012 nbytes = _PyMem_EXTRA;
1013#endif
Guido van Rossumb18618d2000-05-03 23:44:39 +00001014 return PyMem_REALLOC(p, nbytes);
Guido van Rossume09fb551997-08-05 02:04:34 +00001015}
1016
1017void
1018PyMem_Free(p)
1019 ANY *p;
1020{
Guido van Rossumb18618d2000-05-03 23:44:39 +00001021 PyMem_FREE(p);
1022}
1023
1024
1025/* Python's object malloc wrappers (see objimpl.h) */
1026
1027ANY *
1028PyObject_Malloc(nbytes)
1029 size_t nbytes;
1030{
1031 return PyObject_MALLOC(nbytes);
1032}
1033
1034ANY *
1035PyObject_Realloc(p, nbytes)
1036 ANY *p;
1037 size_t nbytes;
1038{
1039 return PyObject_REALLOC(p, nbytes);
1040}
1041
1042void
1043PyObject_Free(p)
1044 ANY *p;
1045{
1046 PyObject_FREE(p);
Guido van Rossume09fb551997-08-05 02:04:34 +00001047}
Guido van Rossum86610361998-04-10 22:32:46 +00001048
1049
1050/* These methods are used to control infinite recursion in repr, str, print,
1051 etc. Container objects that may recursively contain themselves,
1052 e.g. builtin dictionaries and lists, should used Py_ReprEnter() and
1053 Py_ReprLeave() to avoid infinite recursion.
1054
1055 Py_ReprEnter() returns 0 the first time it is called for a particular
1056 object and 1 every time thereafter. It returns -1 if an exception
1057 occurred. Py_ReprLeave() has no return value.
1058
1059 See dictobject.c and listobject.c for examples of use.
1060*/
1061
1062#define KEY "Py_Repr"
1063
1064int
1065Py_ReprEnter(obj)
1066 PyObject *obj;
1067{
1068 PyObject *dict;
1069 PyObject *list;
1070 int i;
1071
1072 dict = PyThreadState_GetDict();
1073 if (dict == NULL)
1074 return -1;
1075 list = PyDict_GetItemString(dict, KEY);
1076 if (list == NULL) {
1077 list = PyList_New(0);
1078 if (list == NULL)
1079 return -1;
1080 if (PyDict_SetItemString(dict, KEY, list) < 0)
1081 return -1;
1082 Py_DECREF(list);
1083 }
1084 i = PyList_GET_SIZE(list);
1085 while (--i >= 0) {
1086 if (PyList_GET_ITEM(list, i) == obj)
1087 return 1;
1088 }
1089 PyList_Append(list, obj);
1090 return 0;
1091}
1092
1093void
1094Py_ReprLeave(obj)
1095 PyObject *obj;
1096{
1097 PyObject *dict;
1098 PyObject *list;
1099 int i;
1100
1101 dict = PyThreadState_GetDict();
Guido van Rossumeb909461998-04-11 15:17:34 +00001102 if (dict == NULL)
1103 return;
Guido van Rossum86610361998-04-10 22:32:46 +00001104 list = PyDict_GetItemString(dict, KEY);
Guido van Rossumeb909461998-04-11 15:17:34 +00001105 if (list == NULL || !PyList_Check(list))
1106 return;
Guido van Rossum86610361998-04-10 22:32:46 +00001107 i = PyList_GET_SIZE(list);
1108 /* Count backwards because we always expect obj to be list[-1] */
1109 while (--i >= 0) {
1110 if (PyList_GET_ITEM(list, i) == obj) {
1111 PyList_SetSlice(list, i, i + 1, NULL);
1112 break;
1113 }
1114 }
1115}
Guido van Rossumd724b232000-03-13 16:01:29 +00001116
1117/*
1118 trashcan
1119 CT 2k0130
1120 non-recursively destroy nested objects
1121
1122 CT 2k0223
1123 everything is now done in a macro.
1124
1125 CT 2k0305
1126 modified to use functions, after Tim Peter's suggestion.
1127
1128 CT 2k0309
1129 modified to restore a possible error.
Guido van Rossum13ff8eb2000-03-25 18:39:19 +00001130
1131 CT 2k0325
1132 added better safe than sorry check for threadstate
Guido van Rossume92e6102000-04-24 15:40:53 +00001133
1134 CT 2k0422
1135 complete rewrite. We now build a chain via ob_type
1136 and save the limited number of types in ob_refcnt.
1137 This is perfect since we don't need any memory.
1138 A patch for free-threading would need just a lock.
Guido van Rossumd724b232000-03-13 16:01:29 +00001139*/
1140
Guido van Rossume92e6102000-04-24 15:40:53 +00001141#define Py_TRASHCAN_TUPLE 1
1142#define Py_TRASHCAN_LIST 2
1143#define Py_TRASHCAN_DICT 3
1144#define Py_TRASHCAN_FRAME 4
1145#define Py_TRASHCAN_TRACEBACK 5
1146/* extend here if other objects want protection */
1147
Guido van Rossumd724b232000-03-13 16:01:29 +00001148int _PyTrash_delete_nesting = 0;
Guido van Rossume92e6102000-04-24 15:40:53 +00001149
Guido van Rossumd724b232000-03-13 16:01:29 +00001150PyObject * _PyTrash_delete_later = NULL;
1151
1152void
1153_PyTrash_deposit_object(op)
1154 PyObject *op;
1155{
Guido van Rossume92e6102000-04-24 15:40:53 +00001156 int typecode;
1157 PyObject *hold = _PyTrash_delete_later;
Guido van Rossum13ff8eb2000-03-25 18:39:19 +00001158
Guido van Rossume92e6102000-04-24 15:40:53 +00001159 if (PyTuple_Check(op))
1160 typecode = Py_TRASHCAN_TUPLE;
1161 else if (PyList_Check(op))
1162 typecode = Py_TRASHCAN_LIST;
1163 else if (PyDict_Check(op))
1164 typecode = Py_TRASHCAN_DICT;
1165 else if (PyFrame_Check(op))
1166 typecode = Py_TRASHCAN_FRAME;
1167 else if (PyTraceBack_Check(op))
1168 typecode = Py_TRASHCAN_TRACEBACK;
1169 op->ob_refcnt = typecode;
Guido van Rossumd724b232000-03-13 16:01:29 +00001170
Guido van Rossume92e6102000-04-24 15:40:53 +00001171 op->ob_type = (PyTypeObject*)_PyTrash_delete_later;
1172 _PyTrash_delete_later = op;
Guido van Rossumd724b232000-03-13 16:01:29 +00001173}
1174
1175void
Guido van Rossume92e6102000-04-24 15:40:53 +00001176_PyTrash_destroy_chain()
Guido van Rossumd724b232000-03-13 16:01:29 +00001177{
1178 while (_PyTrash_delete_later) {
1179 PyObject *shredder = _PyTrash_delete_later;
Guido van Rossume92e6102000-04-24 15:40:53 +00001180 _PyTrash_delete_later = (PyObject*) shredder->ob_type;
1181
1182 switch (shredder->ob_refcnt) {
1183 case Py_TRASHCAN_TUPLE:
1184 shredder->ob_type = &PyTuple_Type;
1185 break;
1186 case Py_TRASHCAN_LIST:
1187 shredder->ob_type = &PyList_Type;
1188 break;
1189 case Py_TRASHCAN_DICT:
1190 shredder->ob_type = &PyDict_Type;
1191 break;
1192 case Py_TRASHCAN_FRAME:
1193 shredder->ob_type = &PyFrame_Type;
1194 break;
1195 case Py_TRASHCAN_TRACEBACK:
1196 shredder->ob_type = &PyTraceBack_Type;
1197 break;
1198 }
1199 _Py_NewReference(shredder);
1200
Guido van Rossumd724b232000-03-13 16:01:29 +00001201 ++_PyTrash_delete_nesting;
1202 Py_DECREF(shredder);
1203 --_PyTrash_delete_nesting;
1204 }
1205}
Guido van Rossume92e6102000-04-24 15:40:53 +00001206