blob: 7327f7f868bd029e016346e2db0714e12223529d [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
Fred Drake100814d2000-07-09 15:48:49 +000036dump_counts(void)
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +000037{
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 *
Fred Drake100814d2000-07-09 15:48:49 +000053get_counts(void)
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +000054{
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
Fred Drake100814d2000-07-09 15:48:49 +000080inc_count(PyTypeObject *tp)
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +000081{
82 if (tp->tp_alloc == 0) {
Guido van Rossumd8953cb1995-04-06 14:46:26 +000083 /* first time; insert in linked list */
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +000084 if (tp->tp_next != NULL) /* sanity check */
Guido van Rossumc0b618a1997-05-02 03:12:38 +000085 Py_FatalError("XXX inc_count sanity check");
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +000086 tp->tp_next = type_list;
87 type_list = tp;
88 }
89 tp->tp_alloc++;
90 if (tp->tp_alloc - tp->tp_free > tp->tp_maxalloc)
91 tp->tp_maxalloc = tp->tp_alloc - tp->tp_free;
92}
93#endif
94
Guido van Rossumc0b618a1997-05-02 03:12:38 +000095PyObject *
Fred Drake100814d2000-07-09 15:48:49 +000096PyObject_Init(PyObject *op, PyTypeObject *tp)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000097{
Guido van Rossumb18618d2000-05-03 23:44:39 +000098 if (op == NULL) {
99 PyErr_SetString(PyExc_SystemError,
100 "NULL object passed to PyObject_Init");
101 return op;
102 }
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000103#ifdef WITH_CYCLE_GC
104 if (PyType_IS_GC(tp))
105 op = (PyObject *) PyObject_FROM_GC(op);
106#endif
Guido van Rossumb18618d2000-05-03 23:44:39 +0000107 /* Any changes should be reflected in PyObject_INIT (objimpl.h) */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000108 op->ob_type = tp;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000109 _Py_NewReference(op);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000110 return op;
111}
112
Guido van Rossumb18618d2000-05-03 23:44:39 +0000113PyVarObject *
Fred Drake100814d2000-07-09 15:48:49 +0000114PyObject_InitVar(PyVarObject *op, PyTypeObject *tp, int size)
Guido van Rossumb18618d2000-05-03 23:44:39 +0000115{
116 if (op == NULL) {
117 PyErr_SetString(PyExc_SystemError,
118 "NULL object passed to PyObject_InitVar");
119 return op;
120 }
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000121#ifdef WITH_CYCLE_GC
122 if (PyType_IS_GC(tp))
123 op = (PyVarObject *) PyObject_FROM_GC(op);
124#endif
Guido van Rossumb18618d2000-05-03 23:44:39 +0000125 /* Any changes should be reflected in PyObject_INIT_VAR */
126 op->ob_size = size;
127 op->ob_type = tp;
128 _Py_NewReference((PyObject *)op);
129 return op;
130}
131
132PyObject *
Fred Drake100814d2000-07-09 15:48:49 +0000133_PyObject_New(PyTypeObject *tp)
Guido van Rossumb18618d2000-05-03 23:44:39 +0000134{
135 PyObject *op;
136 op = (PyObject *) PyObject_MALLOC(_PyObject_SIZE(tp));
137 if (op == NULL)
138 return PyErr_NoMemory();
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000139#ifdef WITH_CYCLE_GC
140 if (PyType_IS_GC(tp))
141 op = (PyObject *) PyObject_FROM_GC(op);
142#endif
Guido van Rossumb18618d2000-05-03 23:44:39 +0000143 return PyObject_INIT(op, tp);
144}
145
Guido van Rossumd0c87ee1997-05-15 21:31:03 +0000146PyVarObject *
Fred Drake100814d2000-07-09 15:48:49 +0000147_PyObject_NewVar(PyTypeObject *tp, int size)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000148{
Guido van Rossumb18618d2000-05-03 23:44:39 +0000149 PyVarObject *op;
150 op = (PyVarObject *) PyObject_MALLOC(_PyObject_VAR_SIZE(tp, size));
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000151 if (op == NULL)
Guido van Rossumd0c87ee1997-05-15 21:31:03 +0000152 return (PyVarObject *)PyErr_NoMemory();
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000153#ifdef WITH_CYCLE_GC
154 if (PyType_IS_GC(tp))
155 op = (PyVarObject *) PyObject_FROM_GC(op);
156#endif
Guido van Rossumb18618d2000-05-03 23:44:39 +0000157 return PyObject_INIT_VAR(op, tp, size);
158}
159
160void
Fred Drake100814d2000-07-09 15:48:49 +0000161_PyObject_Del(PyObject *op)
Guido van Rossumb18618d2000-05-03 23:44:39 +0000162{
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000163#ifdef WITH_CYCLE_GC
Guido van Rossum4cc6ac72000-07-01 01:00:38 +0000164 if (op && PyType_IS_GC(op->ob_type)) {
165 op = (PyObject *) PyObject_AS_GC(op);
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000166 }
Guido van Rossum4cc6ac72000-07-01 01:00:38 +0000167#endif
168 PyObject_FREE(op);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000169}
170
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000171#ifndef WITH_CYCLE_GC
172/* extension modules might need these */
173void _PyGC_Insert(PyObject *op) { }
174void _PyGC_Remove(PyObject *op) { }
175#endif
176
Guido van Rossum90933611991-06-07 16:10:43 +0000177int
Fred Drake100814d2000-07-09 15:48:49 +0000178PyObject_Print(PyObject *op, FILE *fp, int flags)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000179{
Guido van Rossum278ef591991-07-27 21:40:24 +0000180 int ret = 0;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000181 if (PyErr_CheckSignals())
Guido van Rossum90933611991-06-07 16:10:43 +0000182 return -1;
Guido van Rossum9b00dfa1998-04-28 16:06:54 +0000183#ifdef USE_STACKCHECK
184 if (PyOS_CheckStack()) {
185 PyErr_SetString(PyExc_MemoryError, "Stack overflow");
186 return -1;
187 }
188#endif
Guido van Rossum687ef6e2000-01-12 16:28:58 +0000189 clearerr(fp); /* Clear any previous error condition */
Guido van Rossum90933611991-06-07 16:10:43 +0000190 if (op == NULL) {
191 fprintf(fp, "<nil>");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000192 }
Guido van Rossum90933611991-06-07 16:10:43 +0000193 else {
194 if (op->ob_refcnt <= 0)
Fred Drakea44d3532000-06-30 15:01:00 +0000195 fprintf(fp, "<refcnt %u at %p>",
196 op->ob_refcnt, op);
Guido van Rossum2e8f6141992-09-03 20:32:55 +0000197 else if (op->ob_type->tp_print == NULL) {
198 if (op->ob_type->tp_repr == NULL) {
Fred Drakea44d3532000-06-30 15:01:00 +0000199 fprintf(fp, "<%s object at %p>",
200 op->ob_type->tp_name, op);
Guido van Rossum2e8f6141992-09-03 20:32:55 +0000201 }
202 else {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000203 PyObject *s;
204 if (flags & Py_PRINT_RAW)
205 s = PyObject_Str(op);
Guido van Rossumc6004111993-11-05 10:22:19 +0000206 else
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000207 s = PyObject_Repr(op);
Guido van Rossum2e8f6141992-09-03 20:32:55 +0000208 if (s == NULL)
209 ret = -1;
Guido van Rossum2e8f6141992-09-03 20:32:55 +0000210 else {
Guido van Rossum565798d1998-04-21 22:25:01 +0000211 ret = PyObject_Print(s, fp,
212 Py_PRINT_RAW);
Guido van Rossum2e8f6141992-09-03 20:32:55 +0000213 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000214 Py_XDECREF(s);
Guido van Rossum2e8f6141992-09-03 20:32:55 +0000215 }
216 }
Guido van Rossum90933611991-06-07 16:10:43 +0000217 else
Guido van Rossum278ef591991-07-27 21:40:24 +0000218 ret = (*op->ob_type->tp_print)(op, fp, flags);
Guido van Rossum90933611991-06-07 16:10:43 +0000219 }
Guido van Rossum278ef591991-07-27 21:40:24 +0000220 if (ret == 0) {
221 if (ferror(fp)) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000222 PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossum278ef591991-07-27 21:40:24 +0000223 clearerr(fp);
224 ret = -1;
225 }
226 }
227 return ret;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000228}
229
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000230PyObject *
Fred Drake100814d2000-07-09 15:48:49 +0000231PyObject_Repr(PyObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000232{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000233 if (PyErr_CheckSignals())
Guido van Rossum90933611991-06-07 16:10:43 +0000234 return NULL;
Guido van Rossum9b00dfa1998-04-28 16:06:54 +0000235#ifdef USE_STACKCHECK
236 if (PyOS_CheckStack()) {
237 PyErr_SetString(PyExc_MemoryError, "Stack overflow");
238 return NULL;
239 }
240#endif
Guido van Rossum90933611991-06-07 16:10:43 +0000241 if (v == NULL)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000242 return PyString_FromString("<NULL>");
Guido van Rossum90933611991-06-07 16:10:43 +0000243 else if (v->ob_type->tp_repr == NULL) {
244 char buf[120];
Fred Drakea44d3532000-06-30 15:01:00 +0000245 sprintf(buf, "<%.80s object at %p>",
246 v->ob_type->tp_name, v);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000247 return PyString_FromString(buf);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000248 }
Guido van Rossum4c08d552000-03-10 22:55:18 +0000249 else {
250 PyObject *res;
251 res = (*v->ob_type->tp_repr)(v);
252 if (res == NULL)
253 return NULL;
Fredrik Lundhefecc7d2000-07-01 14:31:09 +0000254 if (PyUnicode_Check(res)) {
255 PyObject* str;
Fredrik Lundh2a1e0602000-07-08 17:43:32 +0000256 str = PyUnicode_AsUnicodeEscapeString(res);
Marc-André Lemburg891bc652000-07-03 09:57:53 +0000257 Py_DECREF(res);
258 if (str)
Fredrik Lundhefecc7d2000-07-01 14:31:09 +0000259 res = str;
Marc-André Lemburg891bc652000-07-03 09:57:53 +0000260 else
261 return NULL;
Fredrik Lundhefecc7d2000-07-01 14:31:09 +0000262 }
Guido van Rossum4c08d552000-03-10 22:55:18 +0000263 if (!PyString_Check(res)) {
264 PyErr_Format(PyExc_TypeError,
Guido van Rossum5db862d2000-04-10 12:46:51 +0000265 "__repr__ returned non-string (type %.200s)",
Guido van Rossum4c08d552000-03-10 22:55:18 +0000266 res->ob_type->tp_name);
267 Py_DECREF(res);
268 return NULL;
269 }
270 return res;
271 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000272}
273
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000274PyObject *
Fred Drake100814d2000-07-09 15:48:49 +0000275PyObject_Str(PyObject *v)
Guido van Rossumc6004111993-11-05 10:22:19 +0000276{
Guido van Rossum4c08d552000-03-10 22:55:18 +0000277 PyObject *res;
278
Guido van Rossumc6004111993-11-05 10:22:19 +0000279 if (v == NULL)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000280 return PyString_FromString("<NULL>");
281 else if (PyString_Check(v)) {
282 Py_INCREF(v);
Guido van Rossumc6004111993-11-05 10:22:19 +0000283 return v;
284 }
Guido van Rossum32b582b1995-01-17 16:35:13 +0000285 else if (v->ob_type->tp_str != NULL)
Guido van Rossum4c08d552000-03-10 22:55:18 +0000286 res = (*v->ob_type->tp_str)(v);
Guido van Rossumc6004111993-11-05 10:22:19 +0000287 else {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000288 PyObject *func;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000289 if (!PyInstance_Check(v) ||
290 (func = PyObject_GetAttrString(v, "__str__")) == NULL) {
291 PyErr_Clear();
292 return PyObject_Repr(v);
Guido van Rossumc6004111993-11-05 10:22:19 +0000293 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000294 res = PyEval_CallObject(func, (PyObject *)NULL);
295 Py_DECREF(func);
Guido van Rossumc6004111993-11-05 10:22:19 +0000296 }
Guido van Rossum4c08d552000-03-10 22:55:18 +0000297 if (res == NULL)
298 return NULL;
Marc-André Lemburg891bc652000-07-03 09:57:53 +0000299 if (PyUnicode_Check(res)) {
300 PyObject* str;
301 str = PyUnicode_AsEncodedString(res, NULL, NULL);
302 Py_DECREF(res);
303 if (str)
304 res = str;
305 else
306 return NULL;
307 }
Guido van Rossum4c08d552000-03-10 22:55:18 +0000308 if (!PyString_Check(res)) {
309 PyErr_Format(PyExc_TypeError,
Guido van Rossum5db862d2000-04-10 12:46:51 +0000310 "__str__ returned non-string (type %.200s)",
Guido van Rossum4c08d552000-03-10 22:55:18 +0000311 res->ob_type->tp_name);
312 Py_DECREF(res);
313 return NULL;
314 }
315 return res;
Guido van Rossumc6004111993-11-05 10:22:19 +0000316}
317
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000318static PyObject *
Fred Drake100814d2000-07-09 15:48:49 +0000319do_cmp(PyObject *v, PyObject *w)
Guido van Rossum20566841995-01-12 11:26:10 +0000320{
Guido van Rossumc8b6df91997-05-23 00:06:51 +0000321 long c;
Guido van Rossum20566841995-01-12 11:26:10 +0000322 /* __rcmp__ actually won't be called unless __cmp__ isn't defined,
323 because the check in cmpobject() reverses the objects first.
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000324 This is intentional -- it makes no sense to define cmp(x,y)
325 different than -cmp(y,x). */
326 if (PyInstance_Check(v) || PyInstance_Check(w))
327 return PyInstance_DoBinOp(v, w, "__cmp__", "__rcmp__", do_cmp);
Guido van Rossumc8b6df91997-05-23 00:06:51 +0000328 c = PyObject_Compare(v, w);
329 if (c && PyErr_Occurred())
330 return NULL;
331 return PyInt_FromLong(c);
Guido van Rossum20566841995-01-12 11:26:10 +0000332}
333
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000334PyObject *_PyCompareState_Key;
335
336/* _PyCompareState_nesting is incremented beforing call compare (for
337 some types) and decremented on exit. If the count exceeds the
338 nesting limit, enable code to detect circular data structures.
339*/
340#define NESTING_LIMIT 500
341int _PyCompareState_nesting = 0;
342
343static PyObject*
Fred Drake100814d2000-07-09 15:48:49 +0000344get_inprogress_dict(void)
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000345{
346 PyObject *tstate_dict, *inprogress;
347
348 tstate_dict = PyThreadState_GetDict();
349 if (tstate_dict == NULL) {
350 PyErr_BadInternalCall();
351 return NULL;
352 }
353 inprogress = PyDict_GetItem(tstate_dict, _PyCompareState_Key);
354 if (inprogress == NULL) {
355 PyErr_Clear();
356 inprogress = PyDict_New();
357 if (inprogress == NULL)
358 return NULL;
359 if (PyDict_SetItem(tstate_dict, _PyCompareState_Key,
360 inprogress) == -1) {
361 Py_DECREF(inprogress);
362 return NULL;
363 }
Jeremy Hyltona251ea02000-06-09 16:20:39 +0000364 Py_DECREF(inprogress);
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000365 }
366 return inprogress;
367}
368
369static PyObject *
Fred Drake100814d2000-07-09 15:48:49 +0000370make_pair(PyObject *v, PyObject *w)
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000371{
372 PyObject *pair;
373
374 pair = PyTuple_New(2);
375 if (pair == NULL) {
376 return NULL;
377 }
Guido van Rossumad89bbc2000-06-28 21:57:18 +0000378 if (v <= w) {
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000379 PyTuple_SET_ITEM(pair, 0, PyLong_FromVoidPtr((void *)v));
380 PyTuple_SET_ITEM(pair, 1, PyLong_FromVoidPtr((void *)w));
381 } else {
382 PyTuple_SET_ITEM(pair, 0, PyLong_FromVoidPtr((void *)w));
383 PyTuple_SET_ITEM(pair, 1, PyLong_FromVoidPtr((void *)v));
384 }
385 return pair;
386}
387
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000388int
Fred Drake100814d2000-07-09 15:48:49 +0000389PyObject_Compare(PyObject *v, PyObject *w)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000390{
Guido van Rossumcd5a5f61998-06-09 18:58:44 +0000391 PyTypeObject *vtp, *wtp;
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000392 int result;
393
Guido van Rossumc8b6df91997-05-23 00:06:51 +0000394 if (v == NULL || w == NULL) {
395 PyErr_BadInternalCall();
396 return -1;
397 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000398 if (v == w)
399 return 0;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000400 if (PyInstance_Check(v) || PyInstance_Check(w)) {
401 PyObject *res;
Guido van Rossum20566841995-01-12 11:26:10 +0000402 int c;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000403 if (!PyInstance_Check(v))
404 return -PyObject_Compare(w, v);
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000405 if (++_PyCompareState_nesting > NESTING_LIMIT) {
406 PyObject *inprogress, *pair;
407
408 inprogress = get_inprogress_dict();
409 if (inprogress == NULL) {
410 return -1;
411 }
412 pair = make_pair(v, w);
413 if (PyDict_GetItem(inprogress, pair)) {
414 /* already comparing these objects. assume
415 they're equal until shown otherwise */
416 Py_DECREF(pair);
417 --_PyCompareState_nesting;
418 return 0;
419 }
420 if (PyDict_SetItem(inprogress, pair, pair) == -1) {
421 return -1;
422 }
423 res = do_cmp(v, w);
424 _PyCompareState_nesting--;
425 /* XXX DelItem shouldn't fail */
426 PyDict_DelItem(inprogress, pair);
427 Py_DECREF(pair);
428 } else {
429 res = do_cmp(v, w);
430 }
Guido van Rossumc8b6df91997-05-23 00:06:51 +0000431 if (res == NULL)
432 return -1;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000433 if (!PyInt_Check(res)) {
434 Py_DECREF(res);
Guido van Rossumc8b6df91997-05-23 00:06:51 +0000435 PyErr_SetString(PyExc_TypeError,
436 "comparison did not return an int");
437 return -1;
Guido van Rossum20566841995-01-12 11:26:10 +0000438 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000439 c = PyInt_AsLong(res);
440 Py_DECREF(res);
Guido van Rossum20566841995-01-12 11:26:10 +0000441 return (c < 0) ? -1 : (c > 0) ? 1 : 0;
442 }
Guido van Rossumcd5a5f61998-06-09 18:58:44 +0000443 if ((vtp = v->ob_type) != (wtp = w->ob_type)) {
444 char *vname = vtp->tp_name;
445 char *wname = wtp->tp_name;
Guido van Rossumb4db1941998-07-21 21:56:41 +0000446 if (vtp->tp_as_number != NULL && wtp->tp_as_number != NULL) {
Guido van Rossum242c6421997-11-19 16:03:17 +0000447 int err;
448 err = PyNumber_CoerceEx(&v, &w);
449 if (err < 0)
Guido van Rossumc8b6df91997-05-23 00:06:51 +0000450 return -1;
Guido van Rossum242c6421997-11-19 16:03:17 +0000451 else if (err == 0) {
Guido van Rossumb4db1941998-07-21 21:56:41 +0000452 int cmp;
453 vtp = v->ob_type;
454 if (vtp->tp_compare == NULL)
455 cmp = (v < w) ? -1 : 1;
456 else
457 cmp = (*vtp->tp_compare)(v, w);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000458 Py_DECREF(v);
459 Py_DECREF(w);
Guido van Rossum9fb03681991-07-01 18:48:04 +0000460 return cmp;
461 }
462 }
Guido van Rossumb244f692000-04-10 13:42:33 +0000463 else if (PyUnicode_Check(v) || PyUnicode_Check(w)) {
464 int result = PyUnicode_Compare(v, w);
465 if (result == -1 && PyErr_Occurred() &&
466 PyErr_ExceptionMatches(PyExc_TypeError))
467 /* TypeErrors are ignored: if Unicode coercion
468 fails due to one of the arguments not
469 having the right type, we continue as
470 defined by the coercion protocol (see
471 above). Luckily, decoding errors are
472 reported as ValueErrors and are not masked
473 by this technique. */
474 PyErr_Clear();
475 else
476 return result;
477 }
Guido van Rossumcd5a5f61998-06-09 18:58:44 +0000478 else if (vtp->tp_as_number != NULL)
479 vname = "";
480 else if (wtp->tp_as_number != NULL)
481 wname = "";
482 /* Numerical types compare smaller than all other types */
483 return strcmp(vname, wname);
Guido van Rossum9fb03681991-07-01 18:48:04 +0000484 }
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000485 if (vtp->tp_compare == NULL) {
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000486 return (v < w) ? -1 : 1;
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000487 }
488 if (++_PyCompareState_nesting > NESTING_LIMIT
489 && (vtp->tp_as_mapping
490 || (vtp->tp_as_sequence && !PyString_Check(v)))) {
491 PyObject *inprogress, *pair;
492
493 inprogress = get_inprogress_dict();
494 if (inprogress == NULL) {
495 return -1;
496 }
497 pair = make_pair(v, w);
498 if (PyDict_GetItem(inprogress, pair)) {
499 /* already comparing these objects. assume
500 they're equal until shown otherwise */
501 _PyCompareState_nesting--;
502 Py_DECREF(pair);
503 return 0;
504 }
505 if (PyDict_SetItem(inprogress, pair, pair) == -1) {
506 return -1;
507 }
508 result = (*vtp->tp_compare)(v, w);
509 _PyCompareState_nesting--;
510 PyDict_DelItem(inprogress, pair); /* XXX shouldn't fail */
511 Py_DECREF(pair);
512 } else {
513 result = (*vtp->tp_compare)(v, w);
514 }
515 return result;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000516}
517
Fred Drake13634cf2000-06-29 19:17:04 +0000518
519/* Set of hash utility functions to help maintaining the invariant that
520 iff a==b then hash(a)==hash(b)
521
522 All the utility functions (_Py_Hash*()) return "-1" to signify an error.
523*/
524
525long
Fred Drake100814d2000-07-09 15:48:49 +0000526_Py_HashDouble(double v)
Fred Drake13634cf2000-06-29 19:17:04 +0000527{
528 /* Use frexp to get at the bits in the double.
529 * Since the VAX D double format has 56 mantissa bits, which is the
530 * most of any double format in use, each of these parts may have as
531 * many as (but no more than) 56 significant bits.
532 * So, assuming sizeof(long) >= 4, each part can be broken into two longs;
533 * frexp and multiplication are used to do that.
534 * Also, since the Cray double format has 15 exponent bits, which is the
535 * most of any double format in use, shifting the exponent field left by
536 * 15 won't overflow a long (again assuming sizeof(long) >= 4).
537 */
538 int expo;
539 long hipart;
540
541 v = frexp(v, &expo);
542 v = v * 2147483648.0; /* 2**31 */
543 hipart = (long)v; /* Take the top 32 bits */
544 v = (v - (double)hipart) * 2147483648.0; /* Get the next 32 bits */
545
546 return hipart + (long)v + (expo << 15); /* Combine everything */
547}
548
549long
Fred Drake100814d2000-07-09 15:48:49 +0000550_Py_HashPointer(void *p)
Fred Drake13634cf2000-06-29 19:17:04 +0000551{
552#if SIZEOF_LONG >= SIZEOF_VOID_P
553 return (long)p;
554#else
555 /* convert to a Python long and hash that */
556 PyObject* longobj;
557 long x;
558
559 if ((longobj = PyLong_FromVoidPtr(p)) == NULL) {
560 x = -1;
561 goto finally;
562 }
563 x = PyObject_Hash(longobj);
564
565finally:
566 Py_XDECREF(longobj);
567 return x;
568#endif
569}
570
571
Guido van Rossum9bfef441993-03-29 10:43:31 +0000572long
Fred Drake100814d2000-07-09 15:48:49 +0000573PyObject_Hash(PyObject *v)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000574{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000575 PyTypeObject *tp = v->ob_type;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000576 if (tp->tp_hash != NULL)
577 return (*tp->tp_hash)(v);
Fred Drake13634cf2000-06-29 19:17:04 +0000578 if (tp->tp_compare == NULL) {
579 return _Py_HashPointer(v); /* Use address as hash value */
580 }
Guido van Rossum9bfef441993-03-29 10:43:31 +0000581 /* If there's a cmp but no hash defined, the object can't be hashed */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000582 PyErr_SetString(PyExc_TypeError, "unhashable type");
Guido van Rossum9bfef441993-03-29 10:43:31 +0000583 return -1;
584}
585
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000586PyObject *
Fred Drake100814d2000-07-09 15:48:49 +0000587PyObject_GetAttrString(PyObject *v, char *name)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000588{
Guido van Rossumd8eb1b31996-08-09 20:52:03 +0000589 if (v->ob_type->tp_getattro != NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000590 PyObject *w, *res;
Guido van Rossumc6d06701997-01-18 07:57:16 +0000591 w = PyString_InternFromString(name);
Guido van Rossumd8eb1b31996-08-09 20:52:03 +0000592 if (w == NULL)
593 return NULL;
594 res = (*v->ob_type->tp_getattro)(v, w);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000595 Py_XDECREF(w);
Guido van Rossumd8eb1b31996-08-09 20:52:03 +0000596 return res;
597 }
598
Guido van Rossum3f5da241990-12-20 15:06:42 +0000599 if (v->ob_type->tp_getattr == NULL) {
Guido van Rossumdb935161998-01-19 22:16:36 +0000600 PyErr_Format(PyExc_AttributeError,
601 "'%.50s' object has no attribute '%.400s'",
602 v->ob_type->tp_name,
603 name);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000604 return NULL;
605 }
606 else {
607 return (*v->ob_type->tp_getattr)(v, name);
608 }
609}
610
611int
Fred Drake100814d2000-07-09 15:48:49 +0000612PyObject_HasAttrString(PyObject *v, char *name)
Guido van Rossumed18fdc1993-07-11 19:55:34 +0000613{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000614 PyObject *res = PyObject_GetAttrString(v, name);
Guido van Rossumed18fdc1993-07-11 19:55:34 +0000615 if (res != NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000616 Py_DECREF(res);
Guido van Rossumed18fdc1993-07-11 19:55:34 +0000617 return 1;
618 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000619 PyErr_Clear();
Guido van Rossumed18fdc1993-07-11 19:55:34 +0000620 return 0;
621}
622
623int
Fred Drake100814d2000-07-09 15:48:49 +0000624PyObject_SetAttrString(PyObject *v, char *name, PyObject *w)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000625{
Guido van Rossumd8eb1b31996-08-09 20:52:03 +0000626 if (v->ob_type->tp_setattro != NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000627 PyObject *s;
Guido van Rossumd8eb1b31996-08-09 20:52:03 +0000628 int res;
Guido van Rossumc6d06701997-01-18 07:57:16 +0000629 s = PyString_InternFromString(name);
Guido van Rossumd8eb1b31996-08-09 20:52:03 +0000630 if (s == NULL)
Guido van Rossumb7fc3041996-09-11 22:51:25 +0000631 return -1;
Guido van Rossumd8eb1b31996-08-09 20:52:03 +0000632 res = (*v->ob_type->tp_setattro)(v, s, w);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000633 Py_XDECREF(s);
Guido van Rossumd8eb1b31996-08-09 20:52:03 +0000634 return res;
635 }
636
Guido van Rossum3f5da241990-12-20 15:06:42 +0000637 if (v->ob_type->tp_setattr == NULL) {
638 if (v->ob_type->tp_getattr == NULL)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000639 PyErr_SetString(PyExc_TypeError,
Guido van Rossum3ea74121991-12-24 13:28:03 +0000640 "attribute-less object (assign or del)");
Guido van Rossum3f5da241990-12-20 15:06:42 +0000641 else
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000642 PyErr_SetString(PyExc_TypeError,
Guido van Rossum3ea74121991-12-24 13:28:03 +0000643 "object has read-only attributes");
Guido van Rossum73531a31990-12-20 23:12:40 +0000644 return -1;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000645 }
646 else {
647 return (*v->ob_type->tp_setattr)(v, name, w);
648 }
649}
650
Guido van Rossum98ff96a1997-05-20 18:34:44 +0000651PyObject *
Fred Drake100814d2000-07-09 15:48:49 +0000652PyObject_GetAttr(PyObject *v, PyObject *name)
Guido van Rossum98ff96a1997-05-20 18:34:44 +0000653{
654 if (v->ob_type->tp_getattro != NULL)
655 return (*v->ob_type->tp_getattro)(v, name);
Jeremy Hylton99a8f902000-06-23 14:36:32 +0000656
657 if (!PyString_Check(name)) {
658 PyErr_SetString(PyExc_TypeError,
659 "attribute name must be string");
660 return NULL;
661 }
662 return PyObject_GetAttrString(v, PyString_AS_STRING(name));
Guido van Rossum98ff96a1997-05-20 18:34:44 +0000663}
664
665int
Fred Drake100814d2000-07-09 15:48:49 +0000666PyObject_HasAttr(PyObject *v, PyObject *name)
Guido van Rossum98ff96a1997-05-20 18:34:44 +0000667{
668 PyObject *res = PyObject_GetAttr(v, name);
669 if (res != NULL) {
670 Py_DECREF(res);
671 return 1;
672 }
673 PyErr_Clear();
674 return 0;
675}
676
677int
Fred Drake100814d2000-07-09 15:48:49 +0000678PyObject_SetAttr(PyObject *v, PyObject *name, PyObject *value)
Guido van Rossum98ff96a1997-05-20 18:34:44 +0000679{
680 int err;
681 Py_INCREF(name);
Jeremy Hylton99a8f902000-06-23 14:36:32 +0000682 if (PyString_Check(name))
683 PyString_InternInPlace(&name);
Guido van Rossum98ff96a1997-05-20 18:34:44 +0000684 if (v->ob_type->tp_setattro != NULL)
685 err = (*v->ob_type->tp_setattro)(v, name, value);
Jeremy Hylton99a8f902000-06-23 14:36:32 +0000686 else if (PyString_Check(name)) {
Guido van Rossum98ff96a1997-05-20 18:34:44 +0000687 err = PyObject_SetAttrString(
Jeremy Hylton99a8f902000-06-23 14:36:32 +0000688 v, PyString_AS_STRING(name), value);
689 }
690 else {
691 PyErr_SetString(PyExc_TypeError,
692 "attribute name must be string");
693 err = -1;
694 }
Guido van Rossum98ff96a1997-05-20 18:34:44 +0000695 Py_DECREF(name);
696 return err;
697}
698
Guido van Rossum6ac258d1993-05-12 08:24:20 +0000699/* Test a value used as condition, e.g., in a for or if statement.
700 Return -1 if an error occurred */
701
702int
Fred Drake100814d2000-07-09 15:48:49 +0000703PyObject_IsTrue(PyObject *v)
Guido van Rossum6ac258d1993-05-12 08:24:20 +0000704{
705 int res;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000706 if (v == Py_None)
Guido van Rossum6ac258d1993-05-12 08:24:20 +0000707 res = 0;
Guido van Rossum1c4f4581998-05-22 00:53:24 +0000708 else if (v->ob_type->tp_as_number != NULL &&
709 v->ob_type->tp_as_number->nb_nonzero != NULL)
Guido van Rossum6ac258d1993-05-12 08:24:20 +0000710 res = (*v->ob_type->tp_as_number->nb_nonzero)(v);
Guido van Rossum1c4f4581998-05-22 00:53:24 +0000711 else if (v->ob_type->tp_as_mapping != NULL &&
712 v->ob_type->tp_as_mapping->mp_length != NULL)
Guido van Rossum6ac258d1993-05-12 08:24:20 +0000713 res = (*v->ob_type->tp_as_mapping->mp_length)(v);
Guido van Rossum1c4f4581998-05-22 00:53:24 +0000714 else if (v->ob_type->tp_as_sequence != NULL &&
715 v->ob_type->tp_as_sequence->sq_length != NULL)
Guido van Rossum6ac258d1993-05-12 08:24:20 +0000716 res = (*v->ob_type->tp_as_sequence->sq_length)(v);
717 else
718 res = 1;
719 if (res > 0)
720 res = 1;
721 return res;
722}
723
Guido van Rossumc3d3f961998-04-09 17:53:59 +0000724/* equivalent of 'not v'
725 Return -1 if an error occurred */
726
727int
Fred Drake100814d2000-07-09 15:48:49 +0000728PyObject_Not(PyObject *v)
Guido van Rossumc3d3f961998-04-09 17:53:59 +0000729{
730 int res;
731 res = PyObject_IsTrue(v);
732 if (res < 0)
733 return res;
734 return res == 0;
735}
736
Guido van Rossum5524a591995-01-10 15:26:20 +0000737/* Coerce two numeric types to the "larger" one.
738 Increment the reference count on each argument.
739 Return -1 and raise an exception if no coercion is possible
740 (and then no reference count is incremented).
741*/
742
743int
Fred Drake100814d2000-07-09 15:48:49 +0000744PyNumber_CoerceEx(PyObject **pv, PyObject **pw)
Guido van Rossum5524a591995-01-10 15:26:20 +0000745{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000746 register PyObject *v = *pv;
747 register PyObject *w = *pw;
Guido van Rossum5524a591995-01-10 15:26:20 +0000748 int res;
749
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000750 if (v->ob_type == w->ob_type && !PyInstance_Check(v)) {
751 Py_INCREF(v);
752 Py_INCREF(w);
Guido van Rossum5524a591995-01-10 15:26:20 +0000753 return 0;
754 }
755 if (v->ob_type->tp_as_number && v->ob_type->tp_as_number->nb_coerce) {
756 res = (*v->ob_type->tp_as_number->nb_coerce)(pv, pw);
757 if (res <= 0)
758 return res;
759 }
760 if (w->ob_type->tp_as_number && w->ob_type->tp_as_number->nb_coerce) {
761 res = (*w->ob_type->tp_as_number->nb_coerce)(pw, pv);
762 if (res <= 0)
763 return res;
764 }
Guido van Rossum242c6421997-11-19 16:03:17 +0000765 return 1;
766}
767
768int
Fred Drake100814d2000-07-09 15:48:49 +0000769PyNumber_Coerce(PyObject **pv, PyObject **pw)
Guido van Rossum242c6421997-11-19 16:03:17 +0000770{
771 int err = PyNumber_CoerceEx(pv, pw);
772 if (err <= 0)
773 return err;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000774 PyErr_SetString(PyExc_TypeError, "number coercion failed");
Guido van Rossum5524a591995-01-10 15:26:20 +0000775 return -1;
776}
777
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000778
Guido van Rossum49b11fe1995-01-26 00:38:22 +0000779/* Test whether an object can be called */
780
781int
Fred Drake100814d2000-07-09 15:48:49 +0000782PyCallable_Check(PyObject *x)
Guido van Rossum49b11fe1995-01-26 00:38:22 +0000783{
784 if (x == NULL)
785 return 0;
786 if (x->ob_type->tp_call != NULL ||
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000787 PyFunction_Check(x) ||
788 PyMethod_Check(x) ||
789 PyCFunction_Check(x) ||
790 PyClass_Check(x))
Guido van Rossum49b11fe1995-01-26 00:38:22 +0000791 return 1;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000792 if (PyInstance_Check(x)) {
793 PyObject *call = PyObject_GetAttrString(x, "__call__");
Guido van Rossum49b11fe1995-01-26 00:38:22 +0000794 if (call == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000795 PyErr_Clear();
Guido van Rossum49b11fe1995-01-26 00:38:22 +0000796 return 0;
797 }
798 /* Could test recursively but don't, for fear of endless
799 recursion if some joker sets self.__call__ = self */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000800 Py_DECREF(call);
Guido van Rossum49b11fe1995-01-26 00:38:22 +0000801 return 1;
802 }
803 return 0;
804}
805
806
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000807/*
808NoObject is usable as a non-NULL undefined value, used by the macro None.
809There is (and should be!) no way to create other objects of this type,
Guido van Rossum3f5da241990-12-20 15:06:42 +0000810so there is exactly one (which is indestructible, by the way).
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000811*/
812
Guido van Rossum0c182a11992-03-27 17:26:13 +0000813/* ARGSUSED */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000814static PyObject *
Fred Drake100814d2000-07-09 15:48:49 +0000815none_repr(PyObject *op)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000816{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000817 return PyString_FromString("None");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000818}
819
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000820static PyTypeObject PyNothing_Type = {
821 PyObject_HEAD_INIT(&PyType_Type)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000822 0,
Guido van Rossum3f5da241990-12-20 15:06:42 +0000823 "None",
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000824 0,
825 0,
826 0, /*tp_dealloc*/ /*never called*/
Guido van Rossum7066dd71992-09-17 17:54:56 +0000827 0, /*tp_print*/
Guido van Rossum3f5da241990-12-20 15:06:42 +0000828 0, /*tp_getattr*/
829 0, /*tp_setattr*/
830 0, /*tp_compare*/
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000831 (reprfunc)none_repr, /*tp_repr*/
Guido van Rossum3f5da241990-12-20 15:06:42 +0000832 0, /*tp_as_number*/
833 0, /*tp_as_sequence*/
834 0, /*tp_as_mapping*/
Guido van Rossum9bfef441993-03-29 10:43:31 +0000835 0, /*tp_hash */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000836};
837
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000838PyObject _Py_NoneStruct = {
839 PyObject_HEAD_INIT(&PyNothing_Type)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000840};
841
842
Guido van Rossum84a90321996-05-22 16:34:47 +0000843#ifdef Py_TRACE_REFS
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000844
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000845static PyObject refchain = {&refchain, &refchain};
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000846
Guido van Rossumaacdc9d1996-08-12 21:32:12 +0000847void
Fred Drake100814d2000-07-09 15:48:49 +0000848_Py_ResetReferences(void)
Guido van Rossume09fb551997-08-05 02:04:34 +0000849{
850 refchain._ob_prev = refchain._ob_next = &refchain;
851 _Py_RefTotal = 0;
852}
853
854void
Fred Drake100814d2000-07-09 15:48:49 +0000855_Py_NewReference(PyObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000856{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000857 _Py_RefTotal++;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000858 op->ob_refcnt = 1;
859 op->_ob_next = refchain._ob_next;
860 op->_ob_prev = &refchain;
861 refchain._ob_next->_ob_prev = op;
862 refchain._ob_next = op;
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +0000863#ifdef COUNT_ALLOCS
864 inc_count(op->ob_type);
865#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000866}
867
Guido van Rossumaacdc9d1996-08-12 21:32:12 +0000868void
Fred Drake100814d2000-07-09 15:48:49 +0000869_Py_ForgetReference(register PyObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000870{
Guido van Rossumbffd6832000-01-20 22:32:56 +0000871#ifdef SLOW_UNREF_CHECK
Guido van Rossum4c08d552000-03-10 22:55:18 +0000872 register PyObject *p;
Guido van Rossumbffd6832000-01-20 22:32:56 +0000873#endif
Guido van Rossumd7047b31995-01-02 19:07:15 +0000874 if (op->ob_refcnt < 0)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000875 Py_FatalError("UNREF negative refcnt");
Guido van Rossum2e8f6141992-09-03 20:32:55 +0000876 if (op == &refchain ||
Guido van Rossumd7047b31995-01-02 19:07:15 +0000877 op->_ob_prev->_ob_next != op || op->_ob_next->_ob_prev != op)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000878 Py_FatalError("UNREF invalid object");
Guido van Rossum2e8f6141992-09-03 20:32:55 +0000879#ifdef SLOW_UNREF_CHECK
Guido van Rossum3f5da241990-12-20 15:06:42 +0000880 for (p = refchain._ob_next; p != &refchain; p = p->_ob_next) {
881 if (p == op)
882 break;
883 }
Guido van Rossumd7047b31995-01-02 19:07:15 +0000884 if (p == &refchain) /* Not found */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000885 Py_FatalError("UNREF unknown object");
Guido van Rossum2e8f6141992-09-03 20:32:55 +0000886#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000887 op->_ob_next->_ob_prev = op->_ob_prev;
888 op->_ob_prev->_ob_next = op->_ob_next;
Guido van Rossum2e8f6141992-09-03 20:32:55 +0000889 op->_ob_next = op->_ob_prev = NULL;
Guido van Rossumd8953cb1995-04-06 14:46:26 +0000890#ifdef COUNT_ALLOCS
891 op->ob_type->tp_free++;
892#endif
Guido van Rossum3f5da241990-12-20 15:06:42 +0000893}
894
Guido van Rossumaacdc9d1996-08-12 21:32:12 +0000895void
Fred Drake100814d2000-07-09 15:48:49 +0000896_Py_Dealloc(PyObject *op)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000897{
Guido van Rossum9776adf1994-09-07 14:36:45 +0000898 destructor dealloc = op->ob_type->tp_dealloc;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000899 _Py_ForgetReference(op);
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000900#ifndef WITH_CYCLE_GC
Guido van Rossume92e6102000-04-24 15:40:53 +0000901 if (_PyTrash_delete_nesting < PyTrash_UNWIND_LEVEL-1)
902 op->ob_type = NULL;
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000903#endif
Guido van Rossum9776adf1994-09-07 14:36:45 +0000904 (*dealloc)(op);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000905}
906
Guido van Rossumaacdc9d1996-08-12 21:32:12 +0000907void
Fred Drake100814d2000-07-09 15:48:49 +0000908_Py_PrintReferences(FILE *fp)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000909{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000910 PyObject *op;
Guido van Rossume09fb551997-08-05 02:04:34 +0000911 fprintf(fp, "Remaining objects:\n");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000912 for (op = refchain._ob_next; op != &refchain; op = op->_ob_next) {
913 fprintf(fp, "[%d] ", op->ob_refcnt);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000914 if (PyObject_Print(op, fp, 0) != 0)
915 PyErr_Clear();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000916 putc('\n', fp);
917 }
918}
919
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000920PyObject *
Fred Drake100814d2000-07-09 15:48:49 +0000921_Py_GetObjects(PyObject *self, PyObject *args)
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000922{
923 int i, n;
924 PyObject *t = NULL;
925 PyObject *res, *op;
926
927 if (!PyArg_ParseTuple(args, "i|O", &n, &t))
928 return NULL;
929 op = refchain._ob_next;
930 res = PyList_New(0);
931 if (res == NULL)
932 return NULL;
933 for (i = 0; (n == 0 || i < n) && op != &refchain; i++) {
934 while (op == self || op == args || op == res || op == t ||
935 t != NULL && op->ob_type != (PyTypeObject *) t) {
936 op = op->_ob_next;
937 if (op == &refchain)
938 return res;
939 }
940 if (PyList_Append(res, op) < 0) {
941 Py_DECREF(res);
942 return NULL;
943 }
944 op = op->_ob_next;
945 }
946 return res;
947}
948
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000949#endif
Guido van Rossum97ead3f1996-01-12 01:24:09 +0000950
951
952/* Hack to force loading of cobject.o */
Guido van Rossumda9c2711996-12-05 21:58:58 +0000953PyTypeObject *_Py_cobject_hack = &PyCObject_Type;
Guido van Rossum84a90321996-05-22 16:34:47 +0000954
955
956/* Hack to force loading of abstract.o */
Tim Petersdbd9ba62000-07-09 03:09:57 +0000957int (*_Py_abstract_hack)(PyObject *) = &PyObject_Length;
Guido van Rossume09fb551997-08-05 02:04:34 +0000958
959
Guido van Rossumb18618d2000-05-03 23:44:39 +0000960/* Python's malloc wrappers (see mymalloc.h) */
Guido van Rossume09fb551997-08-05 02:04:34 +0000961
962ANY *
Fred Drake100814d2000-07-09 15:48:49 +0000963PyMem_Malloc(size_t nbytes)
Guido van Rossume09fb551997-08-05 02:04:34 +0000964{
965#if _PyMem_EXTRA > 0
966 if (nbytes == 0)
967 nbytes = _PyMem_EXTRA;
968#endif
Guido van Rossumb18618d2000-05-03 23:44:39 +0000969 return PyMem_MALLOC(nbytes);
Guido van Rossume09fb551997-08-05 02:04:34 +0000970}
971
972ANY *
Fred Drake100814d2000-07-09 15:48:49 +0000973PyMem_Realloc(ANY *p, size_t nbytes)
Guido van Rossume09fb551997-08-05 02:04:34 +0000974{
975#if _PyMem_EXTRA > 0
976 if (nbytes == 0)
977 nbytes = _PyMem_EXTRA;
978#endif
Guido van Rossumb18618d2000-05-03 23:44:39 +0000979 return PyMem_REALLOC(p, nbytes);
Guido van Rossume09fb551997-08-05 02:04:34 +0000980}
981
982void
Fred Drake100814d2000-07-09 15:48:49 +0000983PyMem_Free(ANY *p)
Guido van Rossume09fb551997-08-05 02:04:34 +0000984{
Guido van Rossumb18618d2000-05-03 23:44:39 +0000985 PyMem_FREE(p);
986}
987
988
989/* Python's object malloc wrappers (see objimpl.h) */
990
991ANY *
Fred Drake100814d2000-07-09 15:48:49 +0000992PyObject_Malloc(size_t nbytes)
Guido van Rossumb18618d2000-05-03 23:44:39 +0000993{
994 return PyObject_MALLOC(nbytes);
995}
996
997ANY *
Fred Drake100814d2000-07-09 15:48:49 +0000998PyObject_Realloc(ANY *p, size_t nbytes)
Guido van Rossumb18618d2000-05-03 23:44:39 +0000999{
1000 return PyObject_REALLOC(p, nbytes);
1001}
1002
1003void
Fred Drake100814d2000-07-09 15:48:49 +00001004PyObject_Free(ANY *p)
Guido van Rossumb18618d2000-05-03 23:44:39 +00001005{
1006 PyObject_FREE(p);
Guido van Rossume09fb551997-08-05 02:04:34 +00001007}
Guido van Rossum86610361998-04-10 22:32:46 +00001008
1009
1010/* These methods are used to control infinite recursion in repr, str, print,
1011 etc. Container objects that may recursively contain themselves,
1012 e.g. builtin dictionaries and lists, should used Py_ReprEnter() and
1013 Py_ReprLeave() to avoid infinite recursion.
1014
1015 Py_ReprEnter() returns 0 the first time it is called for a particular
1016 object and 1 every time thereafter. It returns -1 if an exception
1017 occurred. Py_ReprLeave() has no return value.
1018
1019 See dictobject.c and listobject.c for examples of use.
1020*/
1021
1022#define KEY "Py_Repr"
1023
1024int
Fred Drake100814d2000-07-09 15:48:49 +00001025Py_ReprEnter(PyObject *obj)
Guido van Rossum86610361998-04-10 22:32:46 +00001026{
1027 PyObject *dict;
1028 PyObject *list;
1029 int i;
1030
1031 dict = PyThreadState_GetDict();
1032 if (dict == NULL)
1033 return -1;
1034 list = PyDict_GetItemString(dict, KEY);
1035 if (list == NULL) {
1036 list = PyList_New(0);
1037 if (list == NULL)
1038 return -1;
1039 if (PyDict_SetItemString(dict, KEY, list) < 0)
1040 return -1;
1041 Py_DECREF(list);
1042 }
1043 i = PyList_GET_SIZE(list);
1044 while (--i >= 0) {
1045 if (PyList_GET_ITEM(list, i) == obj)
1046 return 1;
1047 }
1048 PyList_Append(list, obj);
1049 return 0;
1050}
1051
1052void
Fred Drake100814d2000-07-09 15:48:49 +00001053Py_ReprLeave(PyObject *obj)
Guido van Rossum86610361998-04-10 22:32:46 +00001054{
1055 PyObject *dict;
1056 PyObject *list;
1057 int i;
1058
1059 dict = PyThreadState_GetDict();
Guido van Rossumeb909461998-04-11 15:17:34 +00001060 if (dict == NULL)
1061 return;
Guido van Rossum86610361998-04-10 22:32:46 +00001062 list = PyDict_GetItemString(dict, KEY);
Guido van Rossumeb909461998-04-11 15:17:34 +00001063 if (list == NULL || !PyList_Check(list))
1064 return;
Guido van Rossum86610361998-04-10 22:32:46 +00001065 i = PyList_GET_SIZE(list);
1066 /* Count backwards because we always expect obj to be list[-1] */
1067 while (--i >= 0) {
1068 if (PyList_GET_ITEM(list, i) == obj) {
1069 PyList_SetSlice(list, i, i + 1, NULL);
1070 break;
1071 }
1072 }
1073}
Guido van Rossumd724b232000-03-13 16:01:29 +00001074
1075/*
1076 trashcan
1077 CT 2k0130
1078 non-recursively destroy nested objects
1079
1080 CT 2k0223
1081 everything is now done in a macro.
1082
1083 CT 2k0305
1084 modified to use functions, after Tim Peter's suggestion.
1085
1086 CT 2k0309
1087 modified to restore a possible error.
Guido van Rossum13ff8eb2000-03-25 18:39:19 +00001088
1089 CT 2k0325
1090 added better safe than sorry check for threadstate
Guido van Rossume92e6102000-04-24 15:40:53 +00001091
1092 CT 2k0422
1093 complete rewrite. We now build a chain via ob_type
1094 and save the limited number of types in ob_refcnt.
1095 This is perfect since we don't need any memory.
1096 A patch for free-threading would need just a lock.
Guido van Rossumd724b232000-03-13 16:01:29 +00001097*/
1098
Guido van Rossume92e6102000-04-24 15:40:53 +00001099#define Py_TRASHCAN_TUPLE 1
1100#define Py_TRASHCAN_LIST 2
1101#define Py_TRASHCAN_DICT 3
1102#define Py_TRASHCAN_FRAME 4
1103#define Py_TRASHCAN_TRACEBACK 5
1104/* extend here if other objects want protection */
1105
Guido van Rossumd724b232000-03-13 16:01:29 +00001106int _PyTrash_delete_nesting = 0;
Guido van Rossume92e6102000-04-24 15:40:53 +00001107
Guido van Rossumd724b232000-03-13 16:01:29 +00001108PyObject * _PyTrash_delete_later = NULL;
1109
1110void
Fred Drake100814d2000-07-09 15:48:49 +00001111_PyTrash_deposit_object(PyObject *op)
Guido van Rossumd724b232000-03-13 16:01:29 +00001112{
Guido van Rossume92e6102000-04-24 15:40:53 +00001113 int typecode;
Guido van Rossum13ff8eb2000-03-25 18:39:19 +00001114
Guido van Rossume92e6102000-04-24 15:40:53 +00001115 if (PyTuple_Check(op))
1116 typecode = Py_TRASHCAN_TUPLE;
1117 else if (PyList_Check(op))
1118 typecode = Py_TRASHCAN_LIST;
1119 else if (PyDict_Check(op))
1120 typecode = Py_TRASHCAN_DICT;
1121 else if (PyFrame_Check(op))
1122 typecode = Py_TRASHCAN_FRAME;
1123 else if (PyTraceBack_Check(op))
1124 typecode = Py_TRASHCAN_TRACEBACK;
1125 op->ob_refcnt = typecode;
Guido van Rossumd724b232000-03-13 16:01:29 +00001126
Guido van Rossume92e6102000-04-24 15:40:53 +00001127 op->ob_type = (PyTypeObject*)_PyTrash_delete_later;
1128 _PyTrash_delete_later = op;
Guido van Rossumd724b232000-03-13 16:01:29 +00001129}
1130
1131void
Fred Drake100814d2000-07-09 15:48:49 +00001132_PyTrash_destroy_chain(void)
Guido van Rossumd724b232000-03-13 16:01:29 +00001133{
1134 while (_PyTrash_delete_later) {
1135 PyObject *shredder = _PyTrash_delete_later;
Guido van Rossume92e6102000-04-24 15:40:53 +00001136 _PyTrash_delete_later = (PyObject*) shredder->ob_type;
1137
1138 switch (shredder->ob_refcnt) {
1139 case Py_TRASHCAN_TUPLE:
1140 shredder->ob_type = &PyTuple_Type;
1141 break;
1142 case Py_TRASHCAN_LIST:
1143 shredder->ob_type = &PyList_Type;
1144 break;
1145 case Py_TRASHCAN_DICT:
1146 shredder->ob_type = &PyDict_Type;
1147 break;
1148 case Py_TRASHCAN_FRAME:
1149 shredder->ob_type = &PyFrame_Type;
1150 break;
1151 case Py_TRASHCAN_TRACEBACK:
1152 shredder->ob_type = &PyTraceBack_Type;
1153 break;
1154 }
1155 _Py_NewReference(shredder);
1156
Guido van Rossumd724b232000-03-13 16:01:29 +00001157 ++_PyTrash_delete_nesting;
1158 Py_DECREF(shredder);
1159 --_PyTrash_delete_nesting;
1160 }
1161}