blob: 9c4cd6a05a37e5813ae53b0d107773ea3052b034 [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
Guido van Rossum4cc6ac72000-07-01 01:00:38 +0000174 if (op && PyType_IS_GC(op->ob_type)) {
175 op = (PyObject *) PyObject_AS_GC(op);
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000176 }
Guido van Rossum4cc6ac72000-07-01 01:00:38 +0000177#endif
178 PyObject_FREE(op);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000179}
180
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000181#ifndef WITH_CYCLE_GC
182/* extension modules might need these */
183void _PyGC_Insert(PyObject *op) { }
184void _PyGC_Remove(PyObject *op) { }
185#endif
186
Guido van Rossum90933611991-06-07 16:10:43 +0000187int
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000188PyObject_Print(op, fp, flags)
189 PyObject *op;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000190 FILE *fp;
191 int flags;
192{
Guido van Rossum278ef591991-07-27 21:40:24 +0000193 int ret = 0;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000194 if (PyErr_CheckSignals())
Guido van Rossum90933611991-06-07 16:10:43 +0000195 return -1;
Guido van Rossum9b00dfa1998-04-28 16:06:54 +0000196#ifdef USE_STACKCHECK
197 if (PyOS_CheckStack()) {
198 PyErr_SetString(PyExc_MemoryError, "Stack overflow");
199 return -1;
200 }
201#endif
Guido van Rossum687ef6e2000-01-12 16:28:58 +0000202 clearerr(fp); /* Clear any previous error condition */
Guido van Rossum90933611991-06-07 16:10:43 +0000203 if (op == NULL) {
204 fprintf(fp, "<nil>");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000205 }
Guido van Rossum90933611991-06-07 16:10:43 +0000206 else {
207 if (op->ob_refcnt <= 0)
Fred Drakea44d3532000-06-30 15:01:00 +0000208 fprintf(fp, "<refcnt %u at %p>",
209 op->ob_refcnt, op);
Guido van Rossum2e8f6141992-09-03 20:32:55 +0000210 else if (op->ob_type->tp_print == NULL) {
211 if (op->ob_type->tp_repr == NULL) {
Fred Drakea44d3532000-06-30 15:01:00 +0000212 fprintf(fp, "<%s object at %p>",
213 op->ob_type->tp_name, op);
Guido van Rossum2e8f6141992-09-03 20:32:55 +0000214 }
215 else {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000216 PyObject *s;
217 if (flags & Py_PRINT_RAW)
218 s = PyObject_Str(op);
Guido van Rossumc6004111993-11-05 10:22:19 +0000219 else
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000220 s = PyObject_Repr(op);
Guido van Rossum2e8f6141992-09-03 20:32:55 +0000221 if (s == NULL)
222 ret = -1;
Guido van Rossum2e8f6141992-09-03 20:32:55 +0000223 else {
Guido van Rossum565798d1998-04-21 22:25:01 +0000224 ret = PyObject_Print(s, fp,
225 Py_PRINT_RAW);
Guido van Rossum2e8f6141992-09-03 20:32:55 +0000226 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000227 Py_XDECREF(s);
Guido van Rossum2e8f6141992-09-03 20:32:55 +0000228 }
229 }
Guido van Rossum90933611991-06-07 16:10:43 +0000230 else
Guido van Rossum278ef591991-07-27 21:40:24 +0000231 ret = (*op->ob_type->tp_print)(op, fp, flags);
Guido van Rossum90933611991-06-07 16:10:43 +0000232 }
Guido van Rossum278ef591991-07-27 21:40:24 +0000233 if (ret == 0) {
234 if (ferror(fp)) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000235 PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossum278ef591991-07-27 21:40:24 +0000236 clearerr(fp);
237 ret = -1;
238 }
239 }
240 return ret;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000241}
242
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000243PyObject *
244PyObject_Repr(v)
245 PyObject *v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000246{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000247 if (PyErr_CheckSignals())
Guido van Rossum90933611991-06-07 16:10:43 +0000248 return NULL;
Guido van Rossum9b00dfa1998-04-28 16:06:54 +0000249#ifdef USE_STACKCHECK
250 if (PyOS_CheckStack()) {
251 PyErr_SetString(PyExc_MemoryError, "Stack overflow");
252 return NULL;
253 }
254#endif
Guido van Rossum90933611991-06-07 16:10:43 +0000255 if (v == NULL)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000256 return PyString_FromString("<NULL>");
Guido van Rossum90933611991-06-07 16:10:43 +0000257 else if (v->ob_type->tp_repr == NULL) {
258 char buf[120];
Fred Drakea44d3532000-06-30 15:01:00 +0000259 sprintf(buf, "<%.80s object at %p>",
260 v->ob_type->tp_name, v);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000261 return PyString_FromString(buf);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000262 }
Guido van Rossum4c08d552000-03-10 22:55:18 +0000263 else {
264 PyObject *res;
265 res = (*v->ob_type->tp_repr)(v);
266 if (res == NULL)
267 return NULL;
Fredrik Lundhefecc7d2000-07-01 14:31:09 +0000268 if (PyUnicode_Check(res)) {
269 PyObject* str;
Fredrik Lundh2a1e0602000-07-08 17:43:32 +0000270 str = PyUnicode_AsUnicodeEscapeString(res);
Marc-André Lemburg891bc652000-07-03 09:57:53 +0000271 Py_DECREF(res);
272 if (str)
Fredrik Lundhefecc7d2000-07-01 14:31:09 +0000273 res = str;
Marc-André Lemburg891bc652000-07-03 09:57:53 +0000274 else
275 return NULL;
Fredrik Lundhefecc7d2000-07-01 14:31:09 +0000276 }
Guido van Rossum4c08d552000-03-10 22:55:18 +0000277 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;
Marc-André Lemburg891bc652000-07-03 09:57:53 +0000314 if (PyUnicode_Check(res)) {
315 PyObject* str;
316 str = PyUnicode_AsEncodedString(res, NULL, NULL);
317 Py_DECREF(res);
318 if (str)
319 res = str;
320 else
321 return NULL;
322 }
Guido van Rossum4c08d552000-03-10 22:55:18 +0000323 if (!PyString_Check(res)) {
324 PyErr_Format(PyExc_TypeError,
Guido van Rossum5db862d2000-04-10 12:46:51 +0000325 "__str__ returned non-string (type %.200s)",
Guido van Rossum4c08d552000-03-10 22:55:18 +0000326 res->ob_type->tp_name);
327 Py_DECREF(res);
328 return NULL;
329 }
330 return res;
Guido van Rossumc6004111993-11-05 10:22:19 +0000331}
332
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000333static PyObject *
Guido van Rossum20566841995-01-12 11:26:10 +0000334do_cmp(v, w)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000335 PyObject *v, *w;
Guido van Rossum20566841995-01-12 11:26:10 +0000336{
Guido van Rossumc8b6df91997-05-23 00:06:51 +0000337 long c;
Guido van Rossum20566841995-01-12 11:26:10 +0000338 /* __rcmp__ actually won't be called unless __cmp__ isn't defined,
339 because the check in cmpobject() reverses the objects first.
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000340 This is intentional -- it makes no sense to define cmp(x,y)
341 different than -cmp(y,x). */
342 if (PyInstance_Check(v) || PyInstance_Check(w))
343 return PyInstance_DoBinOp(v, w, "__cmp__", "__rcmp__", do_cmp);
Guido van Rossumc8b6df91997-05-23 00:06:51 +0000344 c = PyObject_Compare(v, w);
345 if (c && PyErr_Occurred())
346 return NULL;
347 return PyInt_FromLong(c);
Guido van Rossum20566841995-01-12 11:26:10 +0000348}
349
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000350PyObject *_PyCompareState_Key;
351
352/* _PyCompareState_nesting is incremented beforing call compare (for
353 some types) and decremented on exit. If the count exceeds the
354 nesting limit, enable code to detect circular data structures.
355*/
356#define NESTING_LIMIT 500
357int _PyCompareState_nesting = 0;
358
359static PyObject*
360get_inprogress_dict()
361{
362 PyObject *tstate_dict, *inprogress;
363
364 tstate_dict = PyThreadState_GetDict();
365 if (tstate_dict == NULL) {
366 PyErr_BadInternalCall();
367 return NULL;
368 }
369 inprogress = PyDict_GetItem(tstate_dict, _PyCompareState_Key);
370 if (inprogress == NULL) {
371 PyErr_Clear();
372 inprogress = PyDict_New();
373 if (inprogress == NULL)
374 return NULL;
375 if (PyDict_SetItem(tstate_dict, _PyCompareState_Key,
376 inprogress) == -1) {
377 Py_DECREF(inprogress);
378 return NULL;
379 }
Jeremy Hyltona251ea02000-06-09 16:20:39 +0000380 Py_DECREF(inprogress);
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000381 }
382 return inprogress;
383}
384
385static PyObject *
386make_pair(v, w)
387 PyObject *v, *w;
388{
389 PyObject *pair;
390
391 pair = PyTuple_New(2);
392 if (pair == NULL) {
393 return NULL;
394 }
Guido van Rossumad89bbc2000-06-28 21:57:18 +0000395 if (v <= w) {
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000396 PyTuple_SET_ITEM(pair, 0, PyLong_FromVoidPtr((void *)v));
397 PyTuple_SET_ITEM(pair, 1, PyLong_FromVoidPtr((void *)w));
398 } else {
399 PyTuple_SET_ITEM(pair, 0, PyLong_FromVoidPtr((void *)w));
400 PyTuple_SET_ITEM(pair, 1, PyLong_FromVoidPtr((void *)v));
401 }
402 return pair;
403}
404
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000405int
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000406PyObject_Compare(v, w)
407 PyObject *v, *w;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000408{
Guido van Rossumcd5a5f61998-06-09 18:58:44 +0000409 PyTypeObject *vtp, *wtp;
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000410 int result;
411
Guido van Rossumc8b6df91997-05-23 00:06:51 +0000412 if (v == NULL || w == NULL) {
413 PyErr_BadInternalCall();
414 return -1;
415 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000416 if (v == w)
417 return 0;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000418 if (PyInstance_Check(v) || PyInstance_Check(w)) {
419 PyObject *res;
Guido van Rossum20566841995-01-12 11:26:10 +0000420 int c;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000421 if (!PyInstance_Check(v))
422 return -PyObject_Compare(w, v);
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000423 if (++_PyCompareState_nesting > NESTING_LIMIT) {
424 PyObject *inprogress, *pair;
425
426 inprogress = get_inprogress_dict();
427 if (inprogress == NULL) {
428 return -1;
429 }
430 pair = make_pair(v, w);
431 if (PyDict_GetItem(inprogress, pair)) {
432 /* already comparing these objects. assume
433 they're equal until shown otherwise */
434 Py_DECREF(pair);
435 --_PyCompareState_nesting;
436 return 0;
437 }
438 if (PyDict_SetItem(inprogress, pair, pair) == -1) {
439 return -1;
440 }
441 res = do_cmp(v, w);
442 _PyCompareState_nesting--;
443 /* XXX DelItem shouldn't fail */
444 PyDict_DelItem(inprogress, pair);
445 Py_DECREF(pair);
446 } else {
447 res = do_cmp(v, w);
448 }
Guido van Rossumc8b6df91997-05-23 00:06:51 +0000449 if (res == NULL)
450 return -1;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000451 if (!PyInt_Check(res)) {
452 Py_DECREF(res);
Guido van Rossumc8b6df91997-05-23 00:06:51 +0000453 PyErr_SetString(PyExc_TypeError,
454 "comparison did not return an int");
455 return -1;
Guido van Rossum20566841995-01-12 11:26:10 +0000456 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000457 c = PyInt_AsLong(res);
458 Py_DECREF(res);
Guido van Rossum20566841995-01-12 11:26:10 +0000459 return (c < 0) ? -1 : (c > 0) ? 1 : 0;
460 }
Guido van Rossumcd5a5f61998-06-09 18:58:44 +0000461 if ((vtp = v->ob_type) != (wtp = w->ob_type)) {
462 char *vname = vtp->tp_name;
463 char *wname = wtp->tp_name;
Guido van Rossumb4db1941998-07-21 21:56:41 +0000464 if (vtp->tp_as_number != NULL && wtp->tp_as_number != NULL) {
Guido van Rossum242c6421997-11-19 16:03:17 +0000465 int err;
466 err = PyNumber_CoerceEx(&v, &w);
467 if (err < 0)
Guido van Rossumc8b6df91997-05-23 00:06:51 +0000468 return -1;
Guido van Rossum242c6421997-11-19 16:03:17 +0000469 else if (err == 0) {
Guido van Rossumb4db1941998-07-21 21:56:41 +0000470 int cmp;
471 vtp = v->ob_type;
472 if (vtp->tp_compare == NULL)
473 cmp = (v < w) ? -1 : 1;
474 else
475 cmp = (*vtp->tp_compare)(v, w);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000476 Py_DECREF(v);
477 Py_DECREF(w);
Guido van Rossum9fb03681991-07-01 18:48:04 +0000478 return cmp;
479 }
480 }
Guido van Rossumb244f692000-04-10 13:42:33 +0000481 else if (PyUnicode_Check(v) || PyUnicode_Check(w)) {
482 int result = PyUnicode_Compare(v, w);
483 if (result == -1 && PyErr_Occurred() &&
484 PyErr_ExceptionMatches(PyExc_TypeError))
485 /* TypeErrors are ignored: if Unicode coercion
486 fails due to one of the arguments not
487 having the right type, we continue as
488 defined by the coercion protocol (see
489 above). Luckily, decoding errors are
490 reported as ValueErrors and are not masked
491 by this technique. */
492 PyErr_Clear();
493 else
494 return result;
495 }
Guido van Rossumcd5a5f61998-06-09 18:58:44 +0000496 else if (vtp->tp_as_number != NULL)
497 vname = "";
498 else if (wtp->tp_as_number != NULL)
499 wname = "";
500 /* Numerical types compare smaller than all other types */
501 return strcmp(vname, wname);
Guido van Rossum9fb03681991-07-01 18:48:04 +0000502 }
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000503 if (vtp->tp_compare == NULL) {
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000504 return (v < w) ? -1 : 1;
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000505 }
506 if (++_PyCompareState_nesting > NESTING_LIMIT
507 && (vtp->tp_as_mapping
508 || (vtp->tp_as_sequence && !PyString_Check(v)))) {
509 PyObject *inprogress, *pair;
510
511 inprogress = get_inprogress_dict();
512 if (inprogress == NULL) {
513 return -1;
514 }
515 pair = make_pair(v, w);
516 if (PyDict_GetItem(inprogress, pair)) {
517 /* already comparing these objects. assume
518 they're equal until shown otherwise */
519 _PyCompareState_nesting--;
520 Py_DECREF(pair);
521 return 0;
522 }
523 if (PyDict_SetItem(inprogress, pair, pair) == -1) {
524 return -1;
525 }
526 result = (*vtp->tp_compare)(v, w);
527 _PyCompareState_nesting--;
528 PyDict_DelItem(inprogress, pair); /* XXX shouldn't fail */
529 Py_DECREF(pair);
530 } else {
531 result = (*vtp->tp_compare)(v, w);
532 }
533 return result;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000534}
535
Fred Drake13634cf2000-06-29 19:17:04 +0000536
537/* Set of hash utility functions to help maintaining the invariant that
538 iff a==b then hash(a)==hash(b)
539
540 All the utility functions (_Py_Hash*()) return "-1" to signify an error.
541*/
542
543long
544_Py_HashDouble(v)
545 double v;
546{
547 /* Use frexp to get at the bits in the double.
548 * Since the VAX D double format has 56 mantissa bits, which is the
549 * most of any double format in use, each of these parts may have as
550 * many as (but no more than) 56 significant bits.
551 * So, assuming sizeof(long) >= 4, each part can be broken into two longs;
552 * frexp and multiplication are used to do that.
553 * Also, since the Cray double format has 15 exponent bits, which is the
554 * most of any double format in use, shifting the exponent field left by
555 * 15 won't overflow a long (again assuming sizeof(long) >= 4).
556 */
557 int expo;
558 long hipart;
559
560 v = frexp(v, &expo);
561 v = v * 2147483648.0; /* 2**31 */
562 hipart = (long)v; /* Take the top 32 bits */
563 v = (v - (double)hipart) * 2147483648.0; /* Get the next 32 bits */
564
565 return hipart + (long)v + (expo << 15); /* Combine everything */
566}
567
568long
569_Py_HashPointer(p)
570 void *p;
571{
572#if SIZEOF_LONG >= SIZEOF_VOID_P
573 return (long)p;
574#else
575 /* convert to a Python long and hash that */
576 PyObject* longobj;
577 long x;
578
579 if ((longobj = PyLong_FromVoidPtr(p)) == NULL) {
580 x = -1;
581 goto finally;
582 }
583 x = PyObject_Hash(longobj);
584
585finally:
586 Py_XDECREF(longobj);
587 return x;
588#endif
589}
590
591
Guido van Rossum9bfef441993-03-29 10:43:31 +0000592long
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000593PyObject_Hash(v)
594 PyObject *v;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000595{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000596 PyTypeObject *tp = v->ob_type;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000597 if (tp->tp_hash != NULL)
598 return (*tp->tp_hash)(v);
Fred Drake13634cf2000-06-29 19:17:04 +0000599 if (tp->tp_compare == NULL) {
600 return _Py_HashPointer(v); /* Use address as hash value */
601 }
Guido van Rossum9bfef441993-03-29 10:43:31 +0000602 /* If there's a cmp but no hash defined, the object can't be hashed */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000603 PyErr_SetString(PyExc_TypeError, "unhashable type");
Guido van Rossum9bfef441993-03-29 10:43:31 +0000604 return -1;
605}
606
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000607PyObject *
608PyObject_GetAttrString(v, name)
609 PyObject *v;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000610 char *name;
611{
Guido van Rossumd8eb1b31996-08-09 20:52:03 +0000612 if (v->ob_type->tp_getattro != NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000613 PyObject *w, *res;
Guido van Rossumc6d06701997-01-18 07:57:16 +0000614 w = PyString_InternFromString(name);
Guido van Rossumd8eb1b31996-08-09 20:52:03 +0000615 if (w == NULL)
616 return NULL;
617 res = (*v->ob_type->tp_getattro)(v, w);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000618 Py_XDECREF(w);
Guido van Rossumd8eb1b31996-08-09 20:52:03 +0000619 return res;
620 }
621
Guido van Rossum3f5da241990-12-20 15:06:42 +0000622 if (v->ob_type->tp_getattr == NULL) {
Guido van Rossumdb935161998-01-19 22:16:36 +0000623 PyErr_Format(PyExc_AttributeError,
624 "'%.50s' object has no attribute '%.400s'",
625 v->ob_type->tp_name,
626 name);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000627 return NULL;
628 }
629 else {
630 return (*v->ob_type->tp_getattr)(v, name);
631 }
632}
633
634int
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000635PyObject_HasAttrString(v, name)
636 PyObject *v;
Guido van Rossumed18fdc1993-07-11 19:55:34 +0000637 char *name;
638{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000639 PyObject *res = PyObject_GetAttrString(v, name);
Guido van Rossumed18fdc1993-07-11 19:55:34 +0000640 if (res != NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000641 Py_DECREF(res);
Guido van Rossumed18fdc1993-07-11 19:55:34 +0000642 return 1;
643 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000644 PyErr_Clear();
Guido van Rossumed18fdc1993-07-11 19:55:34 +0000645 return 0;
646}
647
648int
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000649PyObject_SetAttrString(v, name, w)
650 PyObject *v;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000651 char *name;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000652 PyObject *w;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000653{
Guido van Rossumd8eb1b31996-08-09 20:52:03 +0000654 if (v->ob_type->tp_setattro != NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000655 PyObject *s;
Guido van Rossumd8eb1b31996-08-09 20:52:03 +0000656 int res;
Guido van Rossumc6d06701997-01-18 07:57:16 +0000657 s = PyString_InternFromString(name);
Guido van Rossumd8eb1b31996-08-09 20:52:03 +0000658 if (s == NULL)
Guido van Rossumb7fc3041996-09-11 22:51:25 +0000659 return -1;
Guido van Rossumd8eb1b31996-08-09 20:52:03 +0000660 res = (*v->ob_type->tp_setattro)(v, s, w);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000661 Py_XDECREF(s);
Guido van Rossumd8eb1b31996-08-09 20:52:03 +0000662 return res;
663 }
664
Guido van Rossum3f5da241990-12-20 15:06:42 +0000665 if (v->ob_type->tp_setattr == NULL) {
666 if (v->ob_type->tp_getattr == NULL)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000667 PyErr_SetString(PyExc_TypeError,
Guido van Rossum3ea74121991-12-24 13:28:03 +0000668 "attribute-less object (assign or del)");
Guido van Rossum3f5da241990-12-20 15:06:42 +0000669 else
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000670 PyErr_SetString(PyExc_TypeError,
Guido van Rossum3ea74121991-12-24 13:28:03 +0000671 "object has read-only attributes");
Guido van Rossum73531a31990-12-20 23:12:40 +0000672 return -1;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000673 }
674 else {
675 return (*v->ob_type->tp_setattr)(v, name, w);
676 }
677}
678
Guido van Rossum98ff96a1997-05-20 18:34:44 +0000679PyObject *
680PyObject_GetAttr(v, name)
681 PyObject *v;
682 PyObject *name;
683{
684 if (v->ob_type->tp_getattro != NULL)
685 return (*v->ob_type->tp_getattro)(v, name);
Jeremy Hylton99a8f902000-06-23 14:36:32 +0000686
687 if (!PyString_Check(name)) {
688 PyErr_SetString(PyExc_TypeError,
689 "attribute name must be string");
690 return NULL;
691 }
692 return PyObject_GetAttrString(v, PyString_AS_STRING(name));
Guido van Rossum98ff96a1997-05-20 18:34:44 +0000693}
694
695int
696PyObject_HasAttr(v, name)
697 PyObject *v;
698 PyObject *name;
699{
700 PyObject *res = PyObject_GetAttr(v, name);
701 if (res != NULL) {
702 Py_DECREF(res);
703 return 1;
704 }
705 PyErr_Clear();
706 return 0;
707}
708
709int
710PyObject_SetAttr(v, name, value)
711 PyObject *v;
712 PyObject *name;
713 PyObject *value;
714{
715 int err;
716 Py_INCREF(name);
Jeremy Hylton99a8f902000-06-23 14:36:32 +0000717 if (PyString_Check(name))
718 PyString_InternInPlace(&name);
Guido van Rossum98ff96a1997-05-20 18:34:44 +0000719 if (v->ob_type->tp_setattro != NULL)
720 err = (*v->ob_type->tp_setattro)(v, name, value);
Jeremy Hylton99a8f902000-06-23 14:36:32 +0000721 else if (PyString_Check(name)) {
Guido van Rossum98ff96a1997-05-20 18:34:44 +0000722 err = PyObject_SetAttrString(
Jeremy Hylton99a8f902000-06-23 14:36:32 +0000723 v, PyString_AS_STRING(name), value);
724 }
725 else {
726 PyErr_SetString(PyExc_TypeError,
727 "attribute name must be string");
728 err = -1;
729 }
Guido van Rossum98ff96a1997-05-20 18:34:44 +0000730 Py_DECREF(name);
731 return err;
732}
733
Guido van Rossum6ac258d1993-05-12 08:24:20 +0000734/* Test a value used as condition, e.g., in a for or if statement.
735 Return -1 if an error occurred */
736
737int
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000738PyObject_IsTrue(v)
739 PyObject *v;
Guido van Rossum6ac258d1993-05-12 08:24:20 +0000740{
741 int res;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000742 if (v == Py_None)
Guido van Rossum6ac258d1993-05-12 08:24:20 +0000743 res = 0;
Guido van Rossum1c4f4581998-05-22 00:53:24 +0000744 else if (v->ob_type->tp_as_number != NULL &&
745 v->ob_type->tp_as_number->nb_nonzero != NULL)
Guido van Rossum6ac258d1993-05-12 08:24:20 +0000746 res = (*v->ob_type->tp_as_number->nb_nonzero)(v);
Guido van Rossum1c4f4581998-05-22 00:53:24 +0000747 else if (v->ob_type->tp_as_mapping != NULL &&
748 v->ob_type->tp_as_mapping->mp_length != NULL)
Guido van Rossum6ac258d1993-05-12 08:24:20 +0000749 res = (*v->ob_type->tp_as_mapping->mp_length)(v);
Guido van Rossum1c4f4581998-05-22 00:53:24 +0000750 else if (v->ob_type->tp_as_sequence != NULL &&
751 v->ob_type->tp_as_sequence->sq_length != NULL)
Guido van Rossum6ac258d1993-05-12 08:24:20 +0000752 res = (*v->ob_type->tp_as_sequence->sq_length)(v);
753 else
754 res = 1;
755 if (res > 0)
756 res = 1;
757 return res;
758}
759
Guido van Rossumc3d3f961998-04-09 17:53:59 +0000760/* equivalent of 'not v'
761 Return -1 if an error occurred */
762
763int
764PyObject_Not(v)
765 PyObject *v;
766{
767 int res;
768 res = PyObject_IsTrue(v);
769 if (res < 0)
770 return res;
771 return res == 0;
772}
773
Guido van Rossum5524a591995-01-10 15:26:20 +0000774/* Coerce two numeric types to the "larger" one.
775 Increment the reference count on each argument.
776 Return -1 and raise an exception if no coercion is possible
777 (and then no reference count is incremented).
778*/
779
780int
Guido van Rossum242c6421997-11-19 16:03:17 +0000781PyNumber_CoerceEx(pv, pw)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000782 PyObject **pv, **pw;
Guido van Rossum5524a591995-01-10 15:26:20 +0000783{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000784 register PyObject *v = *pv;
785 register PyObject *w = *pw;
Guido van Rossum5524a591995-01-10 15:26:20 +0000786 int res;
787
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000788 if (v->ob_type == w->ob_type && !PyInstance_Check(v)) {
789 Py_INCREF(v);
790 Py_INCREF(w);
Guido van Rossum5524a591995-01-10 15:26:20 +0000791 return 0;
792 }
793 if (v->ob_type->tp_as_number && v->ob_type->tp_as_number->nb_coerce) {
794 res = (*v->ob_type->tp_as_number->nb_coerce)(pv, pw);
795 if (res <= 0)
796 return res;
797 }
798 if (w->ob_type->tp_as_number && w->ob_type->tp_as_number->nb_coerce) {
799 res = (*w->ob_type->tp_as_number->nb_coerce)(pw, pv);
800 if (res <= 0)
801 return res;
802 }
Guido van Rossum242c6421997-11-19 16:03:17 +0000803 return 1;
804}
805
806int
807PyNumber_Coerce(pv, pw)
808 PyObject **pv, **pw;
809{
810 int err = PyNumber_CoerceEx(pv, pw);
811 if (err <= 0)
812 return err;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000813 PyErr_SetString(PyExc_TypeError, "number coercion failed");
Guido van Rossum5524a591995-01-10 15:26:20 +0000814 return -1;
815}
816
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000817
Guido van Rossum49b11fe1995-01-26 00:38:22 +0000818/* Test whether an object can be called */
819
820int
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000821PyCallable_Check(x)
822 PyObject *x;
Guido van Rossum49b11fe1995-01-26 00:38:22 +0000823{
824 if (x == NULL)
825 return 0;
826 if (x->ob_type->tp_call != NULL ||
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000827 PyFunction_Check(x) ||
828 PyMethod_Check(x) ||
829 PyCFunction_Check(x) ||
830 PyClass_Check(x))
Guido van Rossum49b11fe1995-01-26 00:38:22 +0000831 return 1;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000832 if (PyInstance_Check(x)) {
833 PyObject *call = PyObject_GetAttrString(x, "__call__");
Guido van Rossum49b11fe1995-01-26 00:38:22 +0000834 if (call == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000835 PyErr_Clear();
Guido van Rossum49b11fe1995-01-26 00:38:22 +0000836 return 0;
837 }
838 /* Could test recursively but don't, for fear of endless
839 recursion if some joker sets self.__call__ = self */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000840 Py_DECREF(call);
Guido van Rossum49b11fe1995-01-26 00:38:22 +0000841 return 1;
842 }
843 return 0;
844}
845
846
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000847/*
848NoObject is usable as a non-NULL undefined value, used by the macro None.
849There is (and should be!) no way to create other objects of this type,
Guido van Rossum3f5da241990-12-20 15:06:42 +0000850so there is exactly one (which is indestructible, by the way).
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000851*/
852
Guido van Rossum0c182a11992-03-27 17:26:13 +0000853/* ARGSUSED */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000854static PyObject *
Guido van Rossum3f5da241990-12-20 15:06:42 +0000855none_repr(op)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000856 PyObject *op;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000857{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000858 return PyString_FromString("None");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000859}
860
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000861static PyTypeObject PyNothing_Type = {
862 PyObject_HEAD_INIT(&PyType_Type)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000863 0,
Guido van Rossum3f5da241990-12-20 15:06:42 +0000864 "None",
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000865 0,
866 0,
867 0, /*tp_dealloc*/ /*never called*/
Guido van Rossum7066dd71992-09-17 17:54:56 +0000868 0, /*tp_print*/
Guido van Rossum3f5da241990-12-20 15:06:42 +0000869 0, /*tp_getattr*/
870 0, /*tp_setattr*/
871 0, /*tp_compare*/
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000872 (reprfunc)none_repr, /*tp_repr*/
Guido van Rossum3f5da241990-12-20 15:06:42 +0000873 0, /*tp_as_number*/
874 0, /*tp_as_sequence*/
875 0, /*tp_as_mapping*/
Guido van Rossum9bfef441993-03-29 10:43:31 +0000876 0, /*tp_hash */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000877};
878
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000879PyObject _Py_NoneStruct = {
880 PyObject_HEAD_INIT(&PyNothing_Type)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000881};
882
883
Guido van Rossum84a90321996-05-22 16:34:47 +0000884#ifdef Py_TRACE_REFS
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000885
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000886static PyObject refchain = {&refchain, &refchain};
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000887
Guido van Rossumaacdc9d1996-08-12 21:32:12 +0000888void
Guido van Rossume09fb551997-08-05 02:04:34 +0000889_Py_ResetReferences()
890{
891 refchain._ob_prev = refchain._ob_next = &refchain;
892 _Py_RefTotal = 0;
893}
894
895void
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000896_Py_NewReference(op)
897 PyObject *op;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000898{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000899 _Py_RefTotal++;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000900 op->ob_refcnt = 1;
901 op->_ob_next = refchain._ob_next;
902 op->_ob_prev = &refchain;
903 refchain._ob_next->_ob_prev = op;
904 refchain._ob_next = op;
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +0000905#ifdef COUNT_ALLOCS
906 inc_count(op->ob_type);
907#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000908}
909
Guido van Rossumaacdc9d1996-08-12 21:32:12 +0000910void
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000911_Py_ForgetReference(op)
912 register PyObject *op;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000913{
Guido van Rossumbffd6832000-01-20 22:32:56 +0000914#ifdef SLOW_UNREF_CHECK
Guido van Rossum4c08d552000-03-10 22:55:18 +0000915 register PyObject *p;
Guido van Rossumbffd6832000-01-20 22:32:56 +0000916#endif
Guido van Rossumd7047b31995-01-02 19:07:15 +0000917 if (op->ob_refcnt < 0)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000918 Py_FatalError("UNREF negative refcnt");
Guido van Rossum2e8f6141992-09-03 20:32:55 +0000919 if (op == &refchain ||
Guido van Rossumd7047b31995-01-02 19:07:15 +0000920 op->_ob_prev->_ob_next != op || op->_ob_next->_ob_prev != op)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000921 Py_FatalError("UNREF invalid object");
Guido van Rossum2e8f6141992-09-03 20:32:55 +0000922#ifdef SLOW_UNREF_CHECK
Guido van Rossum3f5da241990-12-20 15:06:42 +0000923 for (p = refchain._ob_next; p != &refchain; p = p->_ob_next) {
924 if (p == op)
925 break;
926 }
Guido van Rossumd7047b31995-01-02 19:07:15 +0000927 if (p == &refchain) /* Not found */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000928 Py_FatalError("UNREF unknown object");
Guido van Rossum2e8f6141992-09-03 20:32:55 +0000929#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000930 op->_ob_next->_ob_prev = op->_ob_prev;
931 op->_ob_prev->_ob_next = op->_ob_next;
Guido van Rossum2e8f6141992-09-03 20:32:55 +0000932 op->_ob_next = op->_ob_prev = NULL;
Guido van Rossumd8953cb1995-04-06 14:46:26 +0000933#ifdef COUNT_ALLOCS
934 op->ob_type->tp_free++;
935#endif
Guido van Rossum3f5da241990-12-20 15:06:42 +0000936}
937
Guido van Rossumaacdc9d1996-08-12 21:32:12 +0000938void
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000939_Py_Dealloc(op)
940 PyObject *op;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000941{
Guido van Rossum9776adf1994-09-07 14:36:45 +0000942 destructor dealloc = op->ob_type->tp_dealloc;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000943 _Py_ForgetReference(op);
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000944#ifndef WITH_CYCLE_GC
Guido van Rossume92e6102000-04-24 15:40:53 +0000945 if (_PyTrash_delete_nesting < PyTrash_UNWIND_LEVEL-1)
946 op->ob_type = NULL;
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000947#endif
Guido van Rossum9776adf1994-09-07 14:36:45 +0000948 (*dealloc)(op);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000949}
950
Guido van Rossumaacdc9d1996-08-12 21:32:12 +0000951void
Guido van Rossumded690f1996-05-24 20:48:31 +0000952_Py_PrintReferences(fp)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000953 FILE *fp;
954{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000955 PyObject *op;
Guido van Rossume09fb551997-08-05 02:04:34 +0000956 fprintf(fp, "Remaining objects:\n");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000957 for (op = refchain._ob_next; op != &refchain; op = op->_ob_next) {
958 fprintf(fp, "[%d] ", op->ob_refcnt);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000959 if (PyObject_Print(op, fp, 0) != 0)
960 PyErr_Clear();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000961 putc('\n', fp);
962 }
963}
964
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000965PyObject *
Guido van Rossumded690f1996-05-24 20:48:31 +0000966_Py_GetObjects(self, args)
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000967 PyObject *self;
968 PyObject *args;
969{
970 int i, n;
971 PyObject *t = NULL;
972 PyObject *res, *op;
973
974 if (!PyArg_ParseTuple(args, "i|O", &n, &t))
975 return NULL;
976 op = refchain._ob_next;
977 res = PyList_New(0);
978 if (res == NULL)
979 return NULL;
980 for (i = 0; (n == 0 || i < n) && op != &refchain; i++) {
981 while (op == self || op == args || op == res || op == t ||
982 t != NULL && op->ob_type != (PyTypeObject *) t) {
983 op = op->_ob_next;
984 if (op == &refchain)
985 return res;
986 }
987 if (PyList_Append(res, op) < 0) {
988 Py_DECREF(res);
989 return NULL;
990 }
991 op = op->_ob_next;
992 }
993 return res;
994}
995
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000996#endif
Guido van Rossum97ead3f1996-01-12 01:24:09 +0000997
998
999/* Hack to force loading of cobject.o */
Guido van Rossumda9c2711996-12-05 21:58:58 +00001000PyTypeObject *_Py_cobject_hack = &PyCObject_Type;
Guido van Rossum84a90321996-05-22 16:34:47 +00001001
1002
1003/* Hack to force loading of abstract.o */
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001004int (*_Py_abstract_hack) Py_FPROTO((PyObject *)) = &PyObject_Length;
Guido van Rossume09fb551997-08-05 02:04:34 +00001005
1006
Guido van Rossumb18618d2000-05-03 23:44:39 +00001007/* Python's malloc wrappers (see mymalloc.h) */
Guido van Rossume09fb551997-08-05 02:04:34 +00001008
1009ANY *
1010PyMem_Malloc(nbytes)
1011 size_t nbytes;
1012{
1013#if _PyMem_EXTRA > 0
1014 if (nbytes == 0)
1015 nbytes = _PyMem_EXTRA;
1016#endif
Guido van Rossumb18618d2000-05-03 23:44:39 +00001017 return PyMem_MALLOC(nbytes);
Guido van Rossume09fb551997-08-05 02:04:34 +00001018}
1019
1020ANY *
1021PyMem_Realloc(p, nbytes)
1022 ANY *p;
1023 size_t nbytes;
1024{
1025#if _PyMem_EXTRA > 0
1026 if (nbytes == 0)
1027 nbytes = _PyMem_EXTRA;
1028#endif
Guido van Rossumb18618d2000-05-03 23:44:39 +00001029 return PyMem_REALLOC(p, nbytes);
Guido van Rossume09fb551997-08-05 02:04:34 +00001030}
1031
1032void
1033PyMem_Free(p)
1034 ANY *p;
1035{
Guido van Rossumb18618d2000-05-03 23:44:39 +00001036 PyMem_FREE(p);
1037}
1038
1039
1040/* Python's object malloc wrappers (see objimpl.h) */
1041
1042ANY *
1043PyObject_Malloc(nbytes)
1044 size_t nbytes;
1045{
1046 return PyObject_MALLOC(nbytes);
1047}
1048
1049ANY *
1050PyObject_Realloc(p, nbytes)
1051 ANY *p;
1052 size_t nbytes;
1053{
1054 return PyObject_REALLOC(p, nbytes);
1055}
1056
1057void
1058PyObject_Free(p)
1059 ANY *p;
1060{
1061 PyObject_FREE(p);
Guido van Rossume09fb551997-08-05 02:04:34 +00001062}
Guido van Rossum86610361998-04-10 22:32:46 +00001063
1064
1065/* These methods are used to control infinite recursion in repr, str, print,
1066 etc. Container objects that may recursively contain themselves,
1067 e.g. builtin dictionaries and lists, should used Py_ReprEnter() and
1068 Py_ReprLeave() to avoid infinite recursion.
1069
1070 Py_ReprEnter() returns 0 the first time it is called for a particular
1071 object and 1 every time thereafter. It returns -1 if an exception
1072 occurred. Py_ReprLeave() has no return value.
1073
1074 See dictobject.c and listobject.c for examples of use.
1075*/
1076
1077#define KEY "Py_Repr"
1078
1079int
1080Py_ReprEnter(obj)
1081 PyObject *obj;
1082{
1083 PyObject *dict;
1084 PyObject *list;
1085 int i;
1086
1087 dict = PyThreadState_GetDict();
1088 if (dict == NULL)
1089 return -1;
1090 list = PyDict_GetItemString(dict, KEY);
1091 if (list == NULL) {
1092 list = PyList_New(0);
1093 if (list == NULL)
1094 return -1;
1095 if (PyDict_SetItemString(dict, KEY, list) < 0)
1096 return -1;
1097 Py_DECREF(list);
1098 }
1099 i = PyList_GET_SIZE(list);
1100 while (--i >= 0) {
1101 if (PyList_GET_ITEM(list, i) == obj)
1102 return 1;
1103 }
1104 PyList_Append(list, obj);
1105 return 0;
1106}
1107
1108void
1109Py_ReprLeave(obj)
1110 PyObject *obj;
1111{
1112 PyObject *dict;
1113 PyObject *list;
1114 int i;
1115
1116 dict = PyThreadState_GetDict();
Guido van Rossumeb909461998-04-11 15:17:34 +00001117 if (dict == NULL)
1118 return;
Guido van Rossum86610361998-04-10 22:32:46 +00001119 list = PyDict_GetItemString(dict, KEY);
Guido van Rossumeb909461998-04-11 15:17:34 +00001120 if (list == NULL || !PyList_Check(list))
1121 return;
Guido van Rossum86610361998-04-10 22:32:46 +00001122 i = PyList_GET_SIZE(list);
1123 /* Count backwards because we always expect obj to be list[-1] */
1124 while (--i >= 0) {
1125 if (PyList_GET_ITEM(list, i) == obj) {
1126 PyList_SetSlice(list, i, i + 1, NULL);
1127 break;
1128 }
1129 }
1130}
Guido van Rossumd724b232000-03-13 16:01:29 +00001131
1132/*
1133 trashcan
1134 CT 2k0130
1135 non-recursively destroy nested objects
1136
1137 CT 2k0223
1138 everything is now done in a macro.
1139
1140 CT 2k0305
1141 modified to use functions, after Tim Peter's suggestion.
1142
1143 CT 2k0309
1144 modified to restore a possible error.
Guido van Rossum13ff8eb2000-03-25 18:39:19 +00001145
1146 CT 2k0325
1147 added better safe than sorry check for threadstate
Guido van Rossume92e6102000-04-24 15:40:53 +00001148
1149 CT 2k0422
1150 complete rewrite. We now build a chain via ob_type
1151 and save the limited number of types in ob_refcnt.
1152 This is perfect since we don't need any memory.
1153 A patch for free-threading would need just a lock.
Guido van Rossumd724b232000-03-13 16:01:29 +00001154*/
1155
Guido van Rossume92e6102000-04-24 15:40:53 +00001156#define Py_TRASHCAN_TUPLE 1
1157#define Py_TRASHCAN_LIST 2
1158#define Py_TRASHCAN_DICT 3
1159#define Py_TRASHCAN_FRAME 4
1160#define Py_TRASHCAN_TRACEBACK 5
1161/* extend here if other objects want protection */
1162
Guido van Rossumd724b232000-03-13 16:01:29 +00001163int _PyTrash_delete_nesting = 0;
Guido van Rossume92e6102000-04-24 15:40:53 +00001164
Guido van Rossumd724b232000-03-13 16:01:29 +00001165PyObject * _PyTrash_delete_later = NULL;
1166
1167void
1168_PyTrash_deposit_object(op)
1169 PyObject *op;
1170{
Guido van Rossume92e6102000-04-24 15:40:53 +00001171 int typecode;
Guido van Rossum13ff8eb2000-03-25 18:39:19 +00001172
Guido van Rossume92e6102000-04-24 15:40:53 +00001173 if (PyTuple_Check(op))
1174 typecode = Py_TRASHCAN_TUPLE;
1175 else if (PyList_Check(op))
1176 typecode = Py_TRASHCAN_LIST;
1177 else if (PyDict_Check(op))
1178 typecode = Py_TRASHCAN_DICT;
1179 else if (PyFrame_Check(op))
1180 typecode = Py_TRASHCAN_FRAME;
1181 else if (PyTraceBack_Check(op))
1182 typecode = Py_TRASHCAN_TRACEBACK;
1183 op->ob_refcnt = typecode;
Guido van Rossumd724b232000-03-13 16:01:29 +00001184
Guido van Rossume92e6102000-04-24 15:40:53 +00001185 op->ob_type = (PyTypeObject*)_PyTrash_delete_later;
1186 _PyTrash_delete_later = op;
Guido van Rossumd724b232000-03-13 16:01:29 +00001187}
1188
1189void
Guido van Rossume92e6102000-04-24 15:40:53 +00001190_PyTrash_destroy_chain()
Guido van Rossumd724b232000-03-13 16:01:29 +00001191{
1192 while (_PyTrash_delete_later) {
1193 PyObject *shredder = _PyTrash_delete_later;
Guido van Rossume92e6102000-04-24 15:40:53 +00001194 _PyTrash_delete_later = (PyObject*) shredder->ob_type;
1195
1196 switch (shredder->ob_refcnt) {
1197 case Py_TRASHCAN_TUPLE:
1198 shredder->ob_type = &PyTuple_Type;
1199 break;
1200 case Py_TRASHCAN_LIST:
1201 shredder->ob_type = &PyList_Type;
1202 break;
1203 case Py_TRASHCAN_DICT:
1204 shredder->ob_type = &PyDict_Type;
1205 break;
1206 case Py_TRASHCAN_FRAME:
1207 shredder->ob_type = &PyFrame_Type;
1208 break;
1209 case Py_TRASHCAN_TRACEBACK:
1210 shredder->ob_type = &PyTraceBack_Type;
1211 break;
1212 }
1213 _Py_NewReference(shredder);
1214
Guido van Rossumd724b232000-03-13 16:01:29 +00001215 ++_PyTrash_delete_nesting;
1216 Py_DECREF(shredder);
1217 --_PyTrash_delete_nesting;
1218 }
1219}
Guido van Rossume92e6102000-04-24 15:40:53 +00001220