blob: 91be3a6d8c0dae525aea90bb6dc75f20dc6a5ee5 [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001/***********************************************************
Guido van Rossum6610ad91995-01-04 19:07:38 +00002Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
3The Netherlands.
Guido van Rossumf70e43a1991-02-19 12:39:46 +00004
5 All Rights Reserved
6
Guido van Rossumfd71b9e2000-06-30 23:50:40 +00007Copyright (c) 2000, BeOpen.com.
8Copyright (c) 1995-2000, Corporation for National Research Initiatives.
9Copyright (c) 1990-1995, Stichting Mathematisch Centrum.
10All rights reserved.
Guido van Rossumf70e43a1991-02-19 12:39:46 +000011
Guido van Rossumfd71b9e2000-06-30 23:50:40 +000012See the file "Misc/COPYRIGHT" for information on usage and
13redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES.
Guido van Rossumf70e43a1991-02-19 12:39:46 +000014
15******************************************************************/
16
Guido van Rossum3f5da241990-12-20 15:06:42 +000017/* Generic object operations; and implementation of None (NoObject) */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000018
Guido van Rossumc0b618a1997-05-02 03:12:38 +000019#include "Python.h"
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000020
Fred Drake13634cf2000-06-29 19:17:04 +000021#include "mymath.h"
22
Guido van Rossume92e6102000-04-24 15:40:53 +000023/* just for trashcan: */
24#include "compile.h"
25#include "frameobject.h"
26#include "traceback.h"
27
Guido van Rossum6f9e4331995-03-29 16:57:48 +000028#if defined( Py_TRACE_REFS ) || defined( Py_REF_DEBUG )
Guido van Rossumbffd6832000-01-20 22:32:56 +000029DL_IMPORT(long) _Py_RefTotal;
Guido van Rossum3f5da241990-12-20 15:06:42 +000030#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000031
Guido van Rossum3f5da241990-12-20 15:06:42 +000032/* Object allocation routines used by NEWOBJ and NEWVAROBJ macros.
33 These are used by the individual routines for object creation.
34 Do not call them otherwise, they do not initialize the object! */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000035
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +000036#ifdef COUNT_ALLOCS
Guido van Rossumc0b618a1997-05-02 03:12:38 +000037static PyTypeObject *type_list;
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +000038extern int tuple_zero_allocs, fast_tuple_allocs;
39extern int quick_int_allocs, quick_neg_int_allocs;
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +000040extern int null_strings, one_strings;
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +000041void
42dump_counts()
43{
Guido van Rossumc0b618a1997-05-02 03:12:38 +000044 PyTypeObject *tp;
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +000045
46 for (tp = type_list; tp; tp = tp->tp_next)
Sjoerd Mullender52c1f511993-10-25 08:40:52 +000047 fprintf(stderr, "%s alloc'd: %d, freed: %d, max in use: %d\n",
48 tp->tp_name, tp->tp_alloc, tp->tp_free,
49 tp->tp_maxalloc);
50 fprintf(stderr, "fast tuple allocs: %d, empty: %d\n",
51 fast_tuple_allocs, tuple_zero_allocs);
52 fprintf(stderr, "fast int allocs: pos: %d, neg: %d\n",
53 quick_int_allocs, quick_neg_int_allocs);
54 fprintf(stderr, "null strings: %d, 1-strings: %d\n",
55 null_strings, one_strings);
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +000056}
57
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +000058PyObject *
59get_counts()
60{
61 PyTypeObject *tp;
62 PyObject *result;
63 PyObject *v;
64
65 result = PyList_New(0);
66 if (result == NULL)
67 return NULL;
68 for (tp = type_list; tp; tp = tp->tp_next) {
69 v = Py_BuildValue("(siii)", tp->tp_name, tp->tp_alloc,
70 tp->tp_free, tp->tp_maxalloc);
71 if (v == NULL) {
72 Py_DECREF(result);
73 return NULL;
74 }
75 if (PyList_Append(result, v) < 0) {
76 Py_DECREF(v);
77 Py_DECREF(result);
78 return NULL;
79 }
80 Py_DECREF(v);
81 }
82 return result;
83}
84
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +000085void
86inc_count(tp)
Guido van Rossumc0b618a1997-05-02 03:12:38 +000087 PyTypeObject *tp;
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +000088{
89 if (tp->tp_alloc == 0) {
Guido van Rossumd8953cb1995-04-06 14:46:26 +000090 /* first time; insert in linked list */
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +000091 if (tp->tp_next != NULL) /* sanity check */
Guido van Rossumc0b618a1997-05-02 03:12:38 +000092 Py_FatalError("XXX inc_count sanity check");
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +000093 tp->tp_next = type_list;
94 type_list = tp;
95 }
96 tp->tp_alloc++;
97 if (tp->tp_alloc - tp->tp_free > tp->tp_maxalloc)
98 tp->tp_maxalloc = tp->tp_alloc - tp->tp_free;
99}
100#endif
101
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000102PyObject *
Guido van Rossumb18618d2000-05-03 23:44:39 +0000103PyObject_Init(op, tp)
Guido van Rossumf5030ab1996-07-21 02:30:39 +0000104 PyObject *op;
Guido van Rossumb18618d2000-05-03 23:44:39 +0000105 PyTypeObject *tp;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000106{
Guido van Rossumb18618d2000-05-03 23:44:39 +0000107 if (op == NULL) {
108 PyErr_SetString(PyExc_SystemError,
109 "NULL object passed to PyObject_Init");
110 return op;
111 }
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000112#ifdef WITH_CYCLE_GC
113 if (PyType_IS_GC(tp))
114 op = (PyObject *) PyObject_FROM_GC(op);
115#endif
Guido van Rossumb18618d2000-05-03 23:44:39 +0000116 /* Any changes should be reflected in PyObject_INIT (objimpl.h) */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000117 op->ob_type = tp;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000118 _Py_NewReference(op);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000119 return op;
120}
121
Guido van Rossumb18618d2000-05-03 23:44:39 +0000122PyVarObject *
123PyObject_InitVar(op, tp, size)
124 PyVarObject *op;
125 PyTypeObject *tp;
126 int size;
127{
128 if (op == NULL) {
129 PyErr_SetString(PyExc_SystemError,
130 "NULL object passed to PyObject_InitVar");
131 return op;
132 }
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000133#ifdef WITH_CYCLE_GC
134 if (PyType_IS_GC(tp))
135 op = (PyVarObject *) PyObject_FROM_GC(op);
136#endif
Guido van Rossumb18618d2000-05-03 23:44:39 +0000137 /* Any changes should be reflected in PyObject_INIT_VAR */
138 op->ob_size = size;
139 op->ob_type = tp;
140 _Py_NewReference((PyObject *)op);
141 return op;
142}
143
144PyObject *
145_PyObject_New(tp)
146 PyTypeObject *tp;
147{
148 PyObject *op;
149 op = (PyObject *) PyObject_MALLOC(_PyObject_SIZE(tp));
150 if (op == NULL)
151 return PyErr_NoMemory();
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000152#ifdef WITH_CYCLE_GC
153 if (PyType_IS_GC(tp))
154 op = (PyObject *) PyObject_FROM_GC(op);
155#endif
Guido van Rossumb18618d2000-05-03 23:44:39 +0000156 return PyObject_INIT(op, tp);
157}
158
Guido van Rossumd0c87ee1997-05-15 21:31:03 +0000159PyVarObject *
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000160_PyObject_NewVar(tp, size)
161 PyTypeObject *tp;
Guido van Rossum2497ead1995-02-10 17:00:27 +0000162 int size;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000163{
Guido van Rossumb18618d2000-05-03 23:44:39 +0000164 PyVarObject *op;
165 op = (PyVarObject *) PyObject_MALLOC(_PyObject_VAR_SIZE(tp, size));
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000166 if (op == NULL)
Guido van Rossumd0c87ee1997-05-15 21:31:03 +0000167 return (PyVarObject *)PyErr_NoMemory();
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000168#ifdef WITH_CYCLE_GC
169 if (PyType_IS_GC(tp))
170 op = (PyVarObject *) PyObject_FROM_GC(op);
171#endif
Guido van Rossumb18618d2000-05-03 23:44:39 +0000172 return PyObject_INIT_VAR(op, tp, size);
173}
174
175void
176_PyObject_Del(op)
177 PyObject *op;
178{
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000179#ifdef WITH_CYCLE_GC
180 if (PyType_IS_GC(op->ob_type)) {
181 PyGC_Head *g = PyObject_AS_GC(op);
182 PyObject_FREE(g);
183 } else
184#endif
185 {
186 PyObject_FREE(op);
187 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000188}
189
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000190#ifndef WITH_CYCLE_GC
191/* extension modules might need these */
192void _PyGC_Insert(PyObject *op) { }
193void _PyGC_Remove(PyObject *op) { }
194#endif
195
Guido van Rossum90933611991-06-07 16:10:43 +0000196int
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000197PyObject_Print(op, fp, flags)
198 PyObject *op;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000199 FILE *fp;
200 int flags;
201{
Guido van Rossum278ef591991-07-27 21:40:24 +0000202 int ret = 0;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000203 if (PyErr_CheckSignals())
Guido van Rossum90933611991-06-07 16:10:43 +0000204 return -1;
Guido van Rossum9b00dfa1998-04-28 16:06:54 +0000205#ifdef USE_STACKCHECK
206 if (PyOS_CheckStack()) {
207 PyErr_SetString(PyExc_MemoryError, "Stack overflow");
208 return -1;
209 }
210#endif
Guido van Rossum687ef6e2000-01-12 16:28:58 +0000211 clearerr(fp); /* Clear any previous error condition */
Guido van Rossum90933611991-06-07 16:10:43 +0000212 if (op == NULL) {
213 fprintf(fp, "<nil>");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000214 }
Guido van Rossum90933611991-06-07 16:10:43 +0000215 else {
216 if (op->ob_refcnt <= 0)
Fred Drakea44d3532000-06-30 15:01:00 +0000217 fprintf(fp, "<refcnt %u at %p>",
218 op->ob_refcnt, op);
Guido van Rossum2e8f6141992-09-03 20:32:55 +0000219 else if (op->ob_type->tp_print == NULL) {
220 if (op->ob_type->tp_repr == NULL) {
Fred Drakea44d3532000-06-30 15:01:00 +0000221 fprintf(fp, "<%s object at %p>",
222 op->ob_type->tp_name, op);
Guido van Rossum2e8f6141992-09-03 20:32:55 +0000223 }
224 else {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000225 PyObject *s;
226 if (flags & Py_PRINT_RAW)
227 s = PyObject_Str(op);
Guido van Rossumc6004111993-11-05 10:22:19 +0000228 else
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000229 s = PyObject_Repr(op);
Guido van Rossum2e8f6141992-09-03 20:32:55 +0000230 if (s == NULL)
231 ret = -1;
Guido van Rossum2e8f6141992-09-03 20:32:55 +0000232 else {
Guido van Rossum565798d1998-04-21 22:25:01 +0000233 ret = PyObject_Print(s, fp,
234 Py_PRINT_RAW);
Guido van Rossum2e8f6141992-09-03 20:32:55 +0000235 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000236 Py_XDECREF(s);
Guido van Rossum2e8f6141992-09-03 20:32:55 +0000237 }
238 }
Guido van Rossum90933611991-06-07 16:10:43 +0000239 else
Guido van Rossum278ef591991-07-27 21:40:24 +0000240 ret = (*op->ob_type->tp_print)(op, fp, flags);
Guido van Rossum90933611991-06-07 16:10:43 +0000241 }
Guido van Rossum278ef591991-07-27 21:40:24 +0000242 if (ret == 0) {
243 if (ferror(fp)) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000244 PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossum278ef591991-07-27 21:40:24 +0000245 clearerr(fp);
246 ret = -1;
247 }
248 }
249 return ret;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000250}
251
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000252PyObject *
253PyObject_Repr(v)
254 PyObject *v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000255{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000256 if (PyErr_CheckSignals())
Guido van Rossum90933611991-06-07 16:10:43 +0000257 return NULL;
Guido van Rossum9b00dfa1998-04-28 16:06:54 +0000258#ifdef USE_STACKCHECK
259 if (PyOS_CheckStack()) {
260 PyErr_SetString(PyExc_MemoryError, "Stack overflow");
261 return NULL;
262 }
263#endif
Guido van Rossum90933611991-06-07 16:10:43 +0000264 if (v == NULL)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000265 return PyString_FromString("<NULL>");
Guido van Rossum90933611991-06-07 16:10:43 +0000266 else if (v->ob_type->tp_repr == NULL) {
267 char buf[120];
Fred Drakea44d3532000-06-30 15:01:00 +0000268 sprintf(buf, "<%.80s object at %p>",
269 v->ob_type->tp_name, v);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000270 return PyString_FromString(buf);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000271 }
Guido van Rossum4c08d552000-03-10 22:55:18 +0000272 else {
273 PyObject *res;
274 res = (*v->ob_type->tp_repr)(v);
275 if (res == NULL)
276 return NULL;
277 if (!PyString_Check(res)) {
278 PyErr_Format(PyExc_TypeError,
Guido van Rossum5db862d2000-04-10 12:46:51 +0000279 "__repr__ returned non-string (type %.200s)",
Guido van Rossum4c08d552000-03-10 22:55:18 +0000280 res->ob_type->tp_name);
281 Py_DECREF(res);
282 return NULL;
283 }
284 return res;
285 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000286}
287
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000288PyObject *
289PyObject_Str(v)
290 PyObject *v;
Guido van Rossumc6004111993-11-05 10:22:19 +0000291{
Guido van Rossum4c08d552000-03-10 22:55:18 +0000292 PyObject *res;
293
Guido van Rossumc6004111993-11-05 10:22:19 +0000294 if (v == NULL)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000295 return PyString_FromString("<NULL>");
296 else if (PyString_Check(v)) {
297 Py_INCREF(v);
Guido van Rossumc6004111993-11-05 10:22:19 +0000298 return v;
299 }
Guido van Rossum32b582b1995-01-17 16:35:13 +0000300 else if (v->ob_type->tp_str != NULL)
Guido van Rossum4c08d552000-03-10 22:55:18 +0000301 res = (*v->ob_type->tp_str)(v);
Guido van Rossumc6004111993-11-05 10:22:19 +0000302 else {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000303 PyObject *func;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000304 if (!PyInstance_Check(v) ||
305 (func = PyObject_GetAttrString(v, "__str__")) == NULL) {
306 PyErr_Clear();
307 return PyObject_Repr(v);
Guido van Rossumc6004111993-11-05 10:22:19 +0000308 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000309 res = PyEval_CallObject(func, (PyObject *)NULL);
310 Py_DECREF(func);
Guido van Rossumc6004111993-11-05 10:22:19 +0000311 }
Guido van Rossum4c08d552000-03-10 22:55:18 +0000312 if (res == NULL)
313 return NULL;
314 if (!PyString_Check(res)) {
315 PyErr_Format(PyExc_TypeError,
Guido van Rossum5db862d2000-04-10 12:46:51 +0000316 "__str__ returned non-string (type %.200s)",
Guido van Rossum4c08d552000-03-10 22:55:18 +0000317 res->ob_type->tp_name);
318 Py_DECREF(res);
319 return NULL;
320 }
321 return res;
Guido van Rossumc6004111993-11-05 10:22:19 +0000322}
323
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000324static PyObject *
Guido van Rossum20566841995-01-12 11:26:10 +0000325do_cmp(v, w)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000326 PyObject *v, *w;
Guido van Rossum20566841995-01-12 11:26:10 +0000327{
Guido van Rossumc8b6df91997-05-23 00:06:51 +0000328 long c;
Guido van Rossum20566841995-01-12 11:26:10 +0000329 /* __rcmp__ actually won't be called unless __cmp__ isn't defined,
330 because the check in cmpobject() reverses the objects first.
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000331 This is intentional -- it makes no sense to define cmp(x,y)
332 different than -cmp(y,x). */
333 if (PyInstance_Check(v) || PyInstance_Check(w))
334 return PyInstance_DoBinOp(v, w, "__cmp__", "__rcmp__", do_cmp);
Guido van Rossumc8b6df91997-05-23 00:06:51 +0000335 c = PyObject_Compare(v, w);
336 if (c && PyErr_Occurred())
337 return NULL;
338 return PyInt_FromLong(c);
Guido van Rossum20566841995-01-12 11:26:10 +0000339}
340
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000341PyObject *_PyCompareState_Key;
342
343/* _PyCompareState_nesting is incremented beforing call compare (for
344 some types) and decremented on exit. If the count exceeds the
345 nesting limit, enable code to detect circular data structures.
346*/
347#define NESTING_LIMIT 500
348int _PyCompareState_nesting = 0;
349
350static PyObject*
351get_inprogress_dict()
352{
353 PyObject *tstate_dict, *inprogress;
354
355 tstate_dict = PyThreadState_GetDict();
356 if (tstate_dict == NULL) {
357 PyErr_BadInternalCall();
358 return NULL;
359 }
360 inprogress = PyDict_GetItem(tstate_dict, _PyCompareState_Key);
361 if (inprogress == NULL) {
362 PyErr_Clear();
363 inprogress = PyDict_New();
364 if (inprogress == NULL)
365 return NULL;
366 if (PyDict_SetItem(tstate_dict, _PyCompareState_Key,
367 inprogress) == -1) {
368 Py_DECREF(inprogress);
369 return NULL;
370 }
Jeremy Hyltona251ea02000-06-09 16:20:39 +0000371 Py_DECREF(inprogress);
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000372 }
373 return inprogress;
374}
375
376static PyObject *
377make_pair(v, w)
378 PyObject *v, *w;
379{
380 PyObject *pair;
381
382 pair = PyTuple_New(2);
383 if (pair == NULL) {
384 return NULL;
385 }
Guido van Rossumad89bbc2000-06-28 21:57:18 +0000386 if (v <= w) {
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000387 PyTuple_SET_ITEM(pair, 0, PyLong_FromVoidPtr((void *)v));
388 PyTuple_SET_ITEM(pair, 1, PyLong_FromVoidPtr((void *)w));
389 } else {
390 PyTuple_SET_ITEM(pair, 0, PyLong_FromVoidPtr((void *)w));
391 PyTuple_SET_ITEM(pair, 1, PyLong_FromVoidPtr((void *)v));
392 }
393 return pair;
394}
395
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000396int
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000397PyObject_Compare(v, w)
398 PyObject *v, *w;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000399{
Guido van Rossumcd5a5f61998-06-09 18:58:44 +0000400 PyTypeObject *vtp, *wtp;
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000401 int result;
402
Guido van Rossumc8b6df91997-05-23 00:06:51 +0000403 if (v == NULL || w == NULL) {
404 PyErr_BadInternalCall();
405 return -1;
406 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000407 if (v == w)
408 return 0;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000409 if (PyInstance_Check(v) || PyInstance_Check(w)) {
410 PyObject *res;
Guido van Rossum20566841995-01-12 11:26:10 +0000411 int c;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000412 if (!PyInstance_Check(v))
413 return -PyObject_Compare(w, v);
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000414 if (++_PyCompareState_nesting > NESTING_LIMIT) {
415 PyObject *inprogress, *pair;
416
417 inprogress = get_inprogress_dict();
418 if (inprogress == NULL) {
419 return -1;
420 }
421 pair = make_pair(v, w);
422 if (PyDict_GetItem(inprogress, pair)) {
423 /* already comparing these objects. assume
424 they're equal until shown otherwise */
425 Py_DECREF(pair);
426 --_PyCompareState_nesting;
427 return 0;
428 }
429 if (PyDict_SetItem(inprogress, pair, pair) == -1) {
430 return -1;
431 }
432 res = do_cmp(v, w);
433 _PyCompareState_nesting--;
434 /* XXX DelItem shouldn't fail */
435 PyDict_DelItem(inprogress, pair);
436 Py_DECREF(pair);
437 } else {
438 res = do_cmp(v, w);
439 }
Guido van Rossumc8b6df91997-05-23 00:06:51 +0000440 if (res == NULL)
441 return -1;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000442 if (!PyInt_Check(res)) {
443 Py_DECREF(res);
Guido van Rossumc8b6df91997-05-23 00:06:51 +0000444 PyErr_SetString(PyExc_TypeError,
445 "comparison did not return an int");
446 return -1;
Guido van Rossum20566841995-01-12 11:26:10 +0000447 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000448 c = PyInt_AsLong(res);
449 Py_DECREF(res);
Guido van Rossum20566841995-01-12 11:26:10 +0000450 return (c < 0) ? -1 : (c > 0) ? 1 : 0;
451 }
Guido van Rossumcd5a5f61998-06-09 18:58:44 +0000452 if ((vtp = v->ob_type) != (wtp = w->ob_type)) {
453 char *vname = vtp->tp_name;
454 char *wname = wtp->tp_name;
Guido van Rossumb4db1941998-07-21 21:56:41 +0000455 if (vtp->tp_as_number != NULL && wtp->tp_as_number != NULL) {
Guido van Rossum242c6421997-11-19 16:03:17 +0000456 int err;
457 err = PyNumber_CoerceEx(&v, &w);
458 if (err < 0)
Guido van Rossumc8b6df91997-05-23 00:06:51 +0000459 return -1;
Guido van Rossum242c6421997-11-19 16:03:17 +0000460 else if (err == 0) {
Guido van Rossumb4db1941998-07-21 21:56:41 +0000461 int cmp;
462 vtp = v->ob_type;
463 if (vtp->tp_compare == NULL)
464 cmp = (v < w) ? -1 : 1;
465 else
466 cmp = (*vtp->tp_compare)(v, w);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000467 Py_DECREF(v);
468 Py_DECREF(w);
Guido van Rossum9fb03681991-07-01 18:48:04 +0000469 return cmp;
470 }
471 }
Guido van Rossumb244f692000-04-10 13:42:33 +0000472 else if (PyUnicode_Check(v) || PyUnicode_Check(w)) {
473 int result = PyUnicode_Compare(v, w);
474 if (result == -1 && PyErr_Occurred() &&
475 PyErr_ExceptionMatches(PyExc_TypeError))
476 /* TypeErrors are ignored: if Unicode coercion
477 fails due to one of the arguments not
478 having the right type, we continue as
479 defined by the coercion protocol (see
480 above). Luckily, decoding errors are
481 reported as ValueErrors and are not masked
482 by this technique. */
483 PyErr_Clear();
484 else
485 return result;
486 }
Guido van Rossumcd5a5f61998-06-09 18:58:44 +0000487 else if (vtp->tp_as_number != NULL)
488 vname = "";
489 else if (wtp->tp_as_number != NULL)
490 wname = "";
491 /* Numerical types compare smaller than all other types */
492 return strcmp(vname, wname);
Guido van Rossum9fb03681991-07-01 18:48:04 +0000493 }
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000494 if (vtp->tp_compare == NULL) {
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000495 return (v < w) ? -1 : 1;
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000496 }
497 if (++_PyCompareState_nesting > NESTING_LIMIT
498 && (vtp->tp_as_mapping
499 || (vtp->tp_as_sequence && !PyString_Check(v)))) {
500 PyObject *inprogress, *pair;
501
502 inprogress = get_inprogress_dict();
503 if (inprogress == NULL) {
504 return -1;
505 }
506 pair = make_pair(v, w);
507 if (PyDict_GetItem(inprogress, pair)) {
508 /* already comparing these objects. assume
509 they're equal until shown otherwise */
510 _PyCompareState_nesting--;
511 Py_DECREF(pair);
512 return 0;
513 }
514 if (PyDict_SetItem(inprogress, pair, pair) == -1) {
515 return -1;
516 }
517 result = (*vtp->tp_compare)(v, w);
518 _PyCompareState_nesting--;
519 PyDict_DelItem(inprogress, pair); /* XXX shouldn't fail */
520 Py_DECREF(pair);
521 } else {
522 result = (*vtp->tp_compare)(v, w);
523 }
524 return result;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000525}
526
Fred Drake13634cf2000-06-29 19:17:04 +0000527
528/* Set of hash utility functions to help maintaining the invariant that
529 iff a==b then hash(a)==hash(b)
530
531 All the utility functions (_Py_Hash*()) return "-1" to signify an error.
532*/
533
534long
535_Py_HashDouble(v)
536 double v;
537{
538 /* Use frexp to get at the bits in the double.
539 * Since the VAX D double format has 56 mantissa bits, which is the
540 * most of any double format in use, each of these parts may have as
541 * many as (but no more than) 56 significant bits.
542 * So, assuming sizeof(long) >= 4, each part can be broken into two longs;
543 * frexp and multiplication are used to do that.
544 * Also, since the Cray double format has 15 exponent bits, which is the
545 * most of any double format in use, shifting the exponent field left by
546 * 15 won't overflow a long (again assuming sizeof(long) >= 4).
547 */
548 int expo;
549 long hipart;
550
551 v = frexp(v, &expo);
552 v = v * 2147483648.0; /* 2**31 */
553 hipart = (long)v; /* Take the top 32 bits */
554 v = (v - (double)hipart) * 2147483648.0; /* Get the next 32 bits */
555
556 return hipart + (long)v + (expo << 15); /* Combine everything */
557}
558
559long
560_Py_HashPointer(p)
561 void *p;
562{
563#if SIZEOF_LONG >= SIZEOF_VOID_P
564 return (long)p;
565#else
566 /* convert to a Python long and hash that */
567 PyObject* longobj;
568 long x;
569
570 if ((longobj = PyLong_FromVoidPtr(p)) == NULL) {
571 x = -1;
572 goto finally;
573 }
574 x = PyObject_Hash(longobj);
575
576finally:
577 Py_XDECREF(longobj);
578 return x;
579#endif
580}
581
582
Guido van Rossum9bfef441993-03-29 10:43:31 +0000583long
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000584PyObject_Hash(v)
585 PyObject *v;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000586{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000587 PyTypeObject *tp = v->ob_type;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000588 if (tp->tp_hash != NULL)
589 return (*tp->tp_hash)(v);
Fred Drake13634cf2000-06-29 19:17:04 +0000590 if (tp->tp_compare == NULL) {
591 return _Py_HashPointer(v); /* Use address as hash value */
592 }
Guido van Rossum9bfef441993-03-29 10:43:31 +0000593 /* If there's a cmp but no hash defined, the object can't be hashed */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000594 PyErr_SetString(PyExc_TypeError, "unhashable type");
Guido van Rossum9bfef441993-03-29 10:43:31 +0000595 return -1;
596}
597
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000598PyObject *
599PyObject_GetAttrString(v, name)
600 PyObject *v;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000601 char *name;
602{
Guido van Rossumd8eb1b31996-08-09 20:52:03 +0000603 if (v->ob_type->tp_getattro != NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000604 PyObject *w, *res;
Guido van Rossumc6d06701997-01-18 07:57:16 +0000605 w = PyString_InternFromString(name);
Guido van Rossumd8eb1b31996-08-09 20:52:03 +0000606 if (w == NULL)
607 return NULL;
608 res = (*v->ob_type->tp_getattro)(v, w);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000609 Py_XDECREF(w);
Guido van Rossumd8eb1b31996-08-09 20:52:03 +0000610 return res;
611 }
612
Guido van Rossum3f5da241990-12-20 15:06:42 +0000613 if (v->ob_type->tp_getattr == NULL) {
Guido van Rossumdb935161998-01-19 22:16:36 +0000614 PyErr_Format(PyExc_AttributeError,
615 "'%.50s' object has no attribute '%.400s'",
616 v->ob_type->tp_name,
617 name);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000618 return NULL;
619 }
620 else {
621 return (*v->ob_type->tp_getattr)(v, name);
622 }
623}
624
625int
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000626PyObject_HasAttrString(v, name)
627 PyObject *v;
Guido van Rossumed18fdc1993-07-11 19:55:34 +0000628 char *name;
629{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000630 PyObject *res = PyObject_GetAttrString(v, name);
Guido van Rossumed18fdc1993-07-11 19:55:34 +0000631 if (res != NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000632 Py_DECREF(res);
Guido van Rossumed18fdc1993-07-11 19:55:34 +0000633 return 1;
634 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000635 PyErr_Clear();
Guido van Rossumed18fdc1993-07-11 19:55:34 +0000636 return 0;
637}
638
639int
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000640PyObject_SetAttrString(v, name, w)
641 PyObject *v;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000642 char *name;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000643 PyObject *w;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000644{
Guido van Rossumd8eb1b31996-08-09 20:52:03 +0000645 if (v->ob_type->tp_setattro != NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000646 PyObject *s;
Guido van Rossumd8eb1b31996-08-09 20:52:03 +0000647 int res;
Guido van Rossumc6d06701997-01-18 07:57:16 +0000648 s = PyString_InternFromString(name);
Guido van Rossumd8eb1b31996-08-09 20:52:03 +0000649 if (s == NULL)
Guido van Rossumb7fc3041996-09-11 22:51:25 +0000650 return -1;
Guido van Rossumd8eb1b31996-08-09 20:52:03 +0000651 res = (*v->ob_type->tp_setattro)(v, s, w);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000652 Py_XDECREF(s);
Guido van Rossumd8eb1b31996-08-09 20:52:03 +0000653 return res;
654 }
655
Guido van Rossum3f5da241990-12-20 15:06:42 +0000656 if (v->ob_type->tp_setattr == NULL) {
657 if (v->ob_type->tp_getattr == NULL)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000658 PyErr_SetString(PyExc_TypeError,
Guido van Rossum3ea74121991-12-24 13:28:03 +0000659 "attribute-less object (assign or del)");
Guido van Rossum3f5da241990-12-20 15:06:42 +0000660 else
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000661 PyErr_SetString(PyExc_TypeError,
Guido van Rossum3ea74121991-12-24 13:28:03 +0000662 "object has read-only attributes");
Guido van Rossum73531a31990-12-20 23:12:40 +0000663 return -1;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000664 }
665 else {
666 return (*v->ob_type->tp_setattr)(v, name, w);
667 }
668}
669
Guido van Rossum98ff96a1997-05-20 18:34:44 +0000670PyObject *
671PyObject_GetAttr(v, name)
672 PyObject *v;
673 PyObject *name;
674{
675 if (v->ob_type->tp_getattro != NULL)
676 return (*v->ob_type->tp_getattro)(v, name);
Jeremy Hylton99a8f902000-06-23 14:36:32 +0000677
678 if (!PyString_Check(name)) {
679 PyErr_SetString(PyExc_TypeError,
680 "attribute name must be string");
681 return NULL;
682 }
683 return PyObject_GetAttrString(v, PyString_AS_STRING(name));
Guido van Rossum98ff96a1997-05-20 18:34:44 +0000684}
685
686int
687PyObject_HasAttr(v, name)
688 PyObject *v;
689 PyObject *name;
690{
691 PyObject *res = PyObject_GetAttr(v, name);
692 if (res != NULL) {
693 Py_DECREF(res);
694 return 1;
695 }
696 PyErr_Clear();
697 return 0;
698}
699
700int
701PyObject_SetAttr(v, name, value)
702 PyObject *v;
703 PyObject *name;
704 PyObject *value;
705{
706 int err;
707 Py_INCREF(name);
Jeremy Hylton99a8f902000-06-23 14:36:32 +0000708 if (PyString_Check(name))
709 PyString_InternInPlace(&name);
Guido van Rossum98ff96a1997-05-20 18:34:44 +0000710 if (v->ob_type->tp_setattro != NULL)
711 err = (*v->ob_type->tp_setattro)(v, name, value);
Jeremy Hylton99a8f902000-06-23 14:36:32 +0000712 else if (PyString_Check(name)) {
Guido van Rossum98ff96a1997-05-20 18:34:44 +0000713 err = PyObject_SetAttrString(
Jeremy Hylton99a8f902000-06-23 14:36:32 +0000714 v, PyString_AS_STRING(name), value);
715 }
716 else {
717 PyErr_SetString(PyExc_TypeError,
718 "attribute name must be string");
719 err = -1;
720 }
Guido van Rossum98ff96a1997-05-20 18:34:44 +0000721 Py_DECREF(name);
722 return err;
723}
724
Guido van Rossum6ac258d1993-05-12 08:24:20 +0000725/* Test a value used as condition, e.g., in a for or if statement.
726 Return -1 if an error occurred */
727
728int
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000729PyObject_IsTrue(v)
730 PyObject *v;
Guido van Rossum6ac258d1993-05-12 08:24:20 +0000731{
732 int res;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000733 if (v == Py_None)
Guido van Rossum6ac258d1993-05-12 08:24:20 +0000734 res = 0;
Guido van Rossum1c4f4581998-05-22 00:53:24 +0000735 else if (v->ob_type->tp_as_number != NULL &&
736 v->ob_type->tp_as_number->nb_nonzero != NULL)
Guido van Rossum6ac258d1993-05-12 08:24:20 +0000737 res = (*v->ob_type->tp_as_number->nb_nonzero)(v);
Guido van Rossum1c4f4581998-05-22 00:53:24 +0000738 else if (v->ob_type->tp_as_mapping != NULL &&
739 v->ob_type->tp_as_mapping->mp_length != NULL)
Guido van Rossum6ac258d1993-05-12 08:24:20 +0000740 res = (*v->ob_type->tp_as_mapping->mp_length)(v);
Guido van Rossum1c4f4581998-05-22 00:53:24 +0000741 else if (v->ob_type->tp_as_sequence != NULL &&
742 v->ob_type->tp_as_sequence->sq_length != NULL)
Guido van Rossum6ac258d1993-05-12 08:24:20 +0000743 res = (*v->ob_type->tp_as_sequence->sq_length)(v);
744 else
745 res = 1;
746 if (res > 0)
747 res = 1;
748 return res;
749}
750
Guido van Rossumc3d3f961998-04-09 17:53:59 +0000751/* equivalent of 'not v'
752 Return -1 if an error occurred */
753
754int
755PyObject_Not(v)
756 PyObject *v;
757{
758 int res;
759 res = PyObject_IsTrue(v);
760 if (res < 0)
761 return res;
762 return res == 0;
763}
764
Guido van Rossum5524a591995-01-10 15:26:20 +0000765/* Coerce two numeric types to the "larger" one.
766 Increment the reference count on each argument.
767 Return -1 and raise an exception if no coercion is possible
768 (and then no reference count is incremented).
769*/
770
771int
Guido van Rossum242c6421997-11-19 16:03:17 +0000772PyNumber_CoerceEx(pv, pw)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000773 PyObject **pv, **pw;
Guido van Rossum5524a591995-01-10 15:26:20 +0000774{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000775 register PyObject *v = *pv;
776 register PyObject *w = *pw;
Guido van Rossum5524a591995-01-10 15:26:20 +0000777 int res;
778
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000779 if (v->ob_type == w->ob_type && !PyInstance_Check(v)) {
780 Py_INCREF(v);
781 Py_INCREF(w);
Guido van Rossum5524a591995-01-10 15:26:20 +0000782 return 0;
783 }
784 if (v->ob_type->tp_as_number && v->ob_type->tp_as_number->nb_coerce) {
785 res = (*v->ob_type->tp_as_number->nb_coerce)(pv, pw);
786 if (res <= 0)
787 return res;
788 }
789 if (w->ob_type->tp_as_number && w->ob_type->tp_as_number->nb_coerce) {
790 res = (*w->ob_type->tp_as_number->nb_coerce)(pw, pv);
791 if (res <= 0)
792 return res;
793 }
Guido van Rossum242c6421997-11-19 16:03:17 +0000794 return 1;
795}
796
797int
798PyNumber_Coerce(pv, pw)
799 PyObject **pv, **pw;
800{
801 int err = PyNumber_CoerceEx(pv, pw);
802 if (err <= 0)
803 return err;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000804 PyErr_SetString(PyExc_TypeError, "number coercion failed");
Guido van Rossum5524a591995-01-10 15:26:20 +0000805 return -1;
806}
807
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000808
Guido van Rossum49b11fe1995-01-26 00:38:22 +0000809/* Test whether an object can be called */
810
811int
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000812PyCallable_Check(x)
813 PyObject *x;
Guido van Rossum49b11fe1995-01-26 00:38:22 +0000814{
815 if (x == NULL)
816 return 0;
817 if (x->ob_type->tp_call != NULL ||
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000818 PyFunction_Check(x) ||
819 PyMethod_Check(x) ||
820 PyCFunction_Check(x) ||
821 PyClass_Check(x))
Guido van Rossum49b11fe1995-01-26 00:38:22 +0000822 return 1;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000823 if (PyInstance_Check(x)) {
824 PyObject *call = PyObject_GetAttrString(x, "__call__");
Guido van Rossum49b11fe1995-01-26 00:38:22 +0000825 if (call == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000826 PyErr_Clear();
Guido van Rossum49b11fe1995-01-26 00:38:22 +0000827 return 0;
828 }
829 /* Could test recursively but don't, for fear of endless
830 recursion if some joker sets self.__call__ = self */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000831 Py_DECREF(call);
Guido van Rossum49b11fe1995-01-26 00:38:22 +0000832 return 1;
833 }
834 return 0;
835}
836
837
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000838/*
839NoObject is usable as a non-NULL undefined value, used by the macro None.
840There is (and should be!) no way to create other objects of this type,
Guido van Rossum3f5da241990-12-20 15:06:42 +0000841so there is exactly one (which is indestructible, by the way).
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000842*/
843
Guido van Rossum0c182a11992-03-27 17:26:13 +0000844/* ARGSUSED */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000845static PyObject *
Guido van Rossum3f5da241990-12-20 15:06:42 +0000846none_repr(op)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000847 PyObject *op;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000848{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000849 return PyString_FromString("None");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000850}
851
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000852static PyTypeObject PyNothing_Type = {
853 PyObject_HEAD_INIT(&PyType_Type)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000854 0,
Guido van Rossum3f5da241990-12-20 15:06:42 +0000855 "None",
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000856 0,
857 0,
858 0, /*tp_dealloc*/ /*never called*/
Guido van Rossum7066dd71992-09-17 17:54:56 +0000859 0, /*tp_print*/
Guido van Rossum3f5da241990-12-20 15:06:42 +0000860 0, /*tp_getattr*/
861 0, /*tp_setattr*/
862 0, /*tp_compare*/
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000863 (reprfunc)none_repr, /*tp_repr*/
Guido van Rossum3f5da241990-12-20 15:06:42 +0000864 0, /*tp_as_number*/
865 0, /*tp_as_sequence*/
866 0, /*tp_as_mapping*/
Guido van Rossum9bfef441993-03-29 10:43:31 +0000867 0, /*tp_hash */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000868};
869
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000870PyObject _Py_NoneStruct = {
871 PyObject_HEAD_INIT(&PyNothing_Type)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000872};
873
874
Guido van Rossum84a90321996-05-22 16:34:47 +0000875#ifdef Py_TRACE_REFS
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000876
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000877static PyObject refchain = {&refchain, &refchain};
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000878
Guido van Rossumaacdc9d1996-08-12 21:32:12 +0000879void
Guido van Rossume09fb551997-08-05 02:04:34 +0000880_Py_ResetReferences()
881{
882 refchain._ob_prev = refchain._ob_next = &refchain;
883 _Py_RefTotal = 0;
884}
885
886void
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000887_Py_NewReference(op)
888 PyObject *op;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000889{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000890 _Py_RefTotal++;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000891 op->ob_refcnt = 1;
892 op->_ob_next = refchain._ob_next;
893 op->_ob_prev = &refchain;
894 refchain._ob_next->_ob_prev = op;
895 refchain._ob_next = op;
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +0000896#ifdef COUNT_ALLOCS
897 inc_count(op->ob_type);
898#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000899}
900
Guido van Rossumaacdc9d1996-08-12 21:32:12 +0000901void
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000902_Py_ForgetReference(op)
903 register PyObject *op;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000904{
Guido van Rossumbffd6832000-01-20 22:32:56 +0000905#ifdef SLOW_UNREF_CHECK
Guido van Rossum4c08d552000-03-10 22:55:18 +0000906 register PyObject *p;
Guido van Rossumbffd6832000-01-20 22:32:56 +0000907#endif
Guido van Rossumd7047b31995-01-02 19:07:15 +0000908 if (op->ob_refcnt < 0)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000909 Py_FatalError("UNREF negative refcnt");
Guido van Rossum2e8f6141992-09-03 20:32:55 +0000910 if (op == &refchain ||
Guido van Rossumd7047b31995-01-02 19:07:15 +0000911 op->_ob_prev->_ob_next != op || op->_ob_next->_ob_prev != op)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000912 Py_FatalError("UNREF invalid object");
Guido van Rossum2e8f6141992-09-03 20:32:55 +0000913#ifdef SLOW_UNREF_CHECK
Guido van Rossum3f5da241990-12-20 15:06:42 +0000914 for (p = refchain._ob_next; p != &refchain; p = p->_ob_next) {
915 if (p == op)
916 break;
917 }
Guido van Rossumd7047b31995-01-02 19:07:15 +0000918 if (p == &refchain) /* Not found */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000919 Py_FatalError("UNREF unknown object");
Guido van Rossum2e8f6141992-09-03 20:32:55 +0000920#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000921 op->_ob_next->_ob_prev = op->_ob_prev;
922 op->_ob_prev->_ob_next = op->_ob_next;
Guido van Rossum2e8f6141992-09-03 20:32:55 +0000923 op->_ob_next = op->_ob_prev = NULL;
Guido van Rossumd8953cb1995-04-06 14:46:26 +0000924#ifdef COUNT_ALLOCS
925 op->ob_type->tp_free++;
926#endif
Guido van Rossum3f5da241990-12-20 15:06:42 +0000927}
928
Guido van Rossumaacdc9d1996-08-12 21:32:12 +0000929void
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000930_Py_Dealloc(op)
931 PyObject *op;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000932{
Guido van Rossum9776adf1994-09-07 14:36:45 +0000933 destructor dealloc = op->ob_type->tp_dealloc;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000934 _Py_ForgetReference(op);
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000935#ifndef WITH_CYCLE_GC
Guido van Rossume92e6102000-04-24 15:40:53 +0000936 if (_PyTrash_delete_nesting < PyTrash_UNWIND_LEVEL-1)
937 op->ob_type = NULL;
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000938#endif
Guido van Rossum9776adf1994-09-07 14:36:45 +0000939 (*dealloc)(op);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000940}
941
Guido van Rossumaacdc9d1996-08-12 21:32:12 +0000942void
Guido van Rossumded690f1996-05-24 20:48:31 +0000943_Py_PrintReferences(fp)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000944 FILE *fp;
945{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000946 PyObject *op;
Guido van Rossume09fb551997-08-05 02:04:34 +0000947 fprintf(fp, "Remaining objects:\n");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000948 for (op = refchain._ob_next; op != &refchain; op = op->_ob_next) {
949 fprintf(fp, "[%d] ", op->ob_refcnt);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000950 if (PyObject_Print(op, fp, 0) != 0)
951 PyErr_Clear();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000952 putc('\n', fp);
953 }
954}
955
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000956PyObject *
Guido van Rossumded690f1996-05-24 20:48:31 +0000957_Py_GetObjects(self, args)
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000958 PyObject *self;
959 PyObject *args;
960{
961 int i, n;
962 PyObject *t = NULL;
963 PyObject *res, *op;
964
965 if (!PyArg_ParseTuple(args, "i|O", &n, &t))
966 return NULL;
967 op = refchain._ob_next;
968 res = PyList_New(0);
969 if (res == NULL)
970 return NULL;
971 for (i = 0; (n == 0 || i < n) && op != &refchain; i++) {
972 while (op == self || op == args || op == res || op == t ||
973 t != NULL && op->ob_type != (PyTypeObject *) t) {
974 op = op->_ob_next;
975 if (op == &refchain)
976 return res;
977 }
978 if (PyList_Append(res, op) < 0) {
979 Py_DECREF(res);
980 return NULL;
981 }
982 op = op->_ob_next;
983 }
984 return res;
985}
986
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000987#endif
Guido van Rossum97ead3f1996-01-12 01:24:09 +0000988
989
990/* Hack to force loading of cobject.o */
Guido van Rossumda9c2711996-12-05 21:58:58 +0000991PyTypeObject *_Py_cobject_hack = &PyCObject_Type;
Guido van Rossum84a90321996-05-22 16:34:47 +0000992
993
994/* Hack to force loading of abstract.o */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000995int (*_Py_abstract_hack) Py_FPROTO((PyObject *)) = &PyObject_Length;
Guido van Rossume09fb551997-08-05 02:04:34 +0000996
997
Guido van Rossumb18618d2000-05-03 23:44:39 +0000998/* Python's malloc wrappers (see mymalloc.h) */
Guido van Rossume09fb551997-08-05 02:04:34 +0000999
1000ANY *
1001PyMem_Malloc(nbytes)
1002 size_t nbytes;
1003{
1004#if _PyMem_EXTRA > 0
1005 if (nbytes == 0)
1006 nbytes = _PyMem_EXTRA;
1007#endif
Guido van Rossumb18618d2000-05-03 23:44:39 +00001008 return PyMem_MALLOC(nbytes);
Guido van Rossume09fb551997-08-05 02:04:34 +00001009}
1010
1011ANY *
1012PyMem_Realloc(p, nbytes)
1013 ANY *p;
1014 size_t nbytes;
1015{
1016#if _PyMem_EXTRA > 0
1017 if (nbytes == 0)
1018 nbytes = _PyMem_EXTRA;
1019#endif
Guido van Rossumb18618d2000-05-03 23:44:39 +00001020 return PyMem_REALLOC(p, nbytes);
Guido van Rossume09fb551997-08-05 02:04:34 +00001021}
1022
1023void
1024PyMem_Free(p)
1025 ANY *p;
1026{
Guido van Rossumb18618d2000-05-03 23:44:39 +00001027 PyMem_FREE(p);
1028}
1029
1030
1031/* Python's object malloc wrappers (see objimpl.h) */
1032
1033ANY *
1034PyObject_Malloc(nbytes)
1035 size_t nbytes;
1036{
1037 return PyObject_MALLOC(nbytes);
1038}
1039
1040ANY *
1041PyObject_Realloc(p, nbytes)
1042 ANY *p;
1043 size_t nbytes;
1044{
1045 return PyObject_REALLOC(p, nbytes);
1046}
1047
1048void
1049PyObject_Free(p)
1050 ANY *p;
1051{
1052 PyObject_FREE(p);
Guido van Rossume09fb551997-08-05 02:04:34 +00001053}
Guido van Rossum86610361998-04-10 22:32:46 +00001054
1055
1056/* These methods are used to control infinite recursion in repr, str, print,
1057 etc. Container objects that may recursively contain themselves,
1058 e.g. builtin dictionaries and lists, should used Py_ReprEnter() and
1059 Py_ReprLeave() to avoid infinite recursion.
1060
1061 Py_ReprEnter() returns 0 the first time it is called for a particular
1062 object and 1 every time thereafter. It returns -1 if an exception
1063 occurred. Py_ReprLeave() has no return value.
1064
1065 See dictobject.c and listobject.c for examples of use.
1066*/
1067
1068#define KEY "Py_Repr"
1069
1070int
1071Py_ReprEnter(obj)
1072 PyObject *obj;
1073{
1074 PyObject *dict;
1075 PyObject *list;
1076 int i;
1077
1078 dict = PyThreadState_GetDict();
1079 if (dict == NULL)
1080 return -1;
1081 list = PyDict_GetItemString(dict, KEY);
1082 if (list == NULL) {
1083 list = PyList_New(0);
1084 if (list == NULL)
1085 return -1;
1086 if (PyDict_SetItemString(dict, KEY, list) < 0)
1087 return -1;
1088 Py_DECREF(list);
1089 }
1090 i = PyList_GET_SIZE(list);
1091 while (--i >= 0) {
1092 if (PyList_GET_ITEM(list, i) == obj)
1093 return 1;
1094 }
1095 PyList_Append(list, obj);
1096 return 0;
1097}
1098
1099void
1100Py_ReprLeave(obj)
1101 PyObject *obj;
1102{
1103 PyObject *dict;
1104 PyObject *list;
1105 int i;
1106
1107 dict = PyThreadState_GetDict();
Guido van Rossumeb909461998-04-11 15:17:34 +00001108 if (dict == NULL)
1109 return;
Guido van Rossum86610361998-04-10 22:32:46 +00001110 list = PyDict_GetItemString(dict, KEY);
Guido van Rossumeb909461998-04-11 15:17:34 +00001111 if (list == NULL || !PyList_Check(list))
1112 return;
Guido van Rossum86610361998-04-10 22:32:46 +00001113 i = PyList_GET_SIZE(list);
1114 /* Count backwards because we always expect obj to be list[-1] */
1115 while (--i >= 0) {
1116 if (PyList_GET_ITEM(list, i) == obj) {
1117 PyList_SetSlice(list, i, i + 1, NULL);
1118 break;
1119 }
1120 }
1121}
Guido van Rossumd724b232000-03-13 16:01:29 +00001122
1123/*
1124 trashcan
1125 CT 2k0130
1126 non-recursively destroy nested objects
1127
1128 CT 2k0223
1129 everything is now done in a macro.
1130
1131 CT 2k0305
1132 modified to use functions, after Tim Peter's suggestion.
1133
1134 CT 2k0309
1135 modified to restore a possible error.
Guido van Rossum13ff8eb2000-03-25 18:39:19 +00001136
1137 CT 2k0325
1138 added better safe than sorry check for threadstate
Guido van Rossume92e6102000-04-24 15:40:53 +00001139
1140 CT 2k0422
1141 complete rewrite. We now build a chain via ob_type
1142 and save the limited number of types in ob_refcnt.
1143 This is perfect since we don't need any memory.
1144 A patch for free-threading would need just a lock.
Guido van Rossumd724b232000-03-13 16:01:29 +00001145*/
1146
Guido van Rossume92e6102000-04-24 15:40:53 +00001147#define Py_TRASHCAN_TUPLE 1
1148#define Py_TRASHCAN_LIST 2
1149#define Py_TRASHCAN_DICT 3
1150#define Py_TRASHCAN_FRAME 4
1151#define Py_TRASHCAN_TRACEBACK 5
1152/* extend here if other objects want protection */
1153
Guido van Rossumd724b232000-03-13 16:01:29 +00001154int _PyTrash_delete_nesting = 0;
Guido van Rossume92e6102000-04-24 15:40:53 +00001155
Guido van Rossumd724b232000-03-13 16:01:29 +00001156PyObject * _PyTrash_delete_later = NULL;
1157
1158void
1159_PyTrash_deposit_object(op)
1160 PyObject *op;
1161{
Guido van Rossume92e6102000-04-24 15:40:53 +00001162 int typecode;
1163 PyObject *hold = _PyTrash_delete_later;
Guido van Rossum13ff8eb2000-03-25 18:39:19 +00001164
Guido van Rossume92e6102000-04-24 15:40:53 +00001165 if (PyTuple_Check(op))
1166 typecode = Py_TRASHCAN_TUPLE;
1167 else if (PyList_Check(op))
1168 typecode = Py_TRASHCAN_LIST;
1169 else if (PyDict_Check(op))
1170 typecode = Py_TRASHCAN_DICT;
1171 else if (PyFrame_Check(op))
1172 typecode = Py_TRASHCAN_FRAME;
1173 else if (PyTraceBack_Check(op))
1174 typecode = Py_TRASHCAN_TRACEBACK;
1175 op->ob_refcnt = typecode;
Guido van Rossumd724b232000-03-13 16:01:29 +00001176
Guido van Rossume92e6102000-04-24 15:40:53 +00001177 op->ob_type = (PyTypeObject*)_PyTrash_delete_later;
1178 _PyTrash_delete_later = op;
Guido van Rossumd724b232000-03-13 16:01:29 +00001179}
1180
1181void
Guido van Rossume92e6102000-04-24 15:40:53 +00001182_PyTrash_destroy_chain()
Guido van Rossumd724b232000-03-13 16:01:29 +00001183{
1184 while (_PyTrash_delete_later) {
1185 PyObject *shredder = _PyTrash_delete_later;
Guido van Rossume92e6102000-04-24 15:40:53 +00001186 _PyTrash_delete_later = (PyObject*) shredder->ob_type;
1187
1188 switch (shredder->ob_refcnt) {
1189 case Py_TRASHCAN_TUPLE:
1190 shredder->ob_type = &PyTuple_Type;
1191 break;
1192 case Py_TRASHCAN_LIST:
1193 shredder->ob_type = &PyList_Type;
1194 break;
1195 case Py_TRASHCAN_DICT:
1196 shredder->ob_type = &PyDict_Type;
1197 break;
1198 case Py_TRASHCAN_FRAME:
1199 shredder->ob_type = &PyFrame_Type;
1200 break;
1201 case Py_TRASHCAN_TRACEBACK:
1202 shredder->ob_type = &PyTraceBack_Type;
1203 break;
1204 }
1205 _Py_NewReference(shredder);
1206
Guido van Rossumd724b232000-03-13 16:01:29 +00001207 ++_PyTrash_delete_nesting;
1208 Py_DECREF(shredder);
1209 --_PyTrash_delete_nesting;
1210 }
1211}
Guido van Rossume92e6102000-04-24 15:40:53 +00001212