blob: 6eaff67dacdce14c39f1490b82832a169a3a6d1e [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001/***********************************************************
Guido van Rossum6610ad91995-01-04 19:07:38 +00002Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
3The Netherlands.
Guido van Rossumf70e43a1991-02-19 12:39:46 +00004
5 All Rights Reserved
6
Guido van Rossumd266eb41996-10-25 14:44:06 +00007Permission to use, copy, modify, and distribute this software and its
8documentation for any purpose and without fee is hereby granted,
Guido van Rossumf70e43a1991-02-19 12:39:46 +00009provided that the above copyright notice appear in all copies and that
Guido van Rossumd266eb41996-10-25 14:44:06 +000010both that copyright notice and this permission notice appear in
Guido van Rossumf70e43a1991-02-19 12:39:46 +000011supporting documentation, and that the names of Stichting Mathematisch
Guido van Rossumd266eb41996-10-25 14:44:06 +000012Centrum or CWI or Corporation for National Research Initiatives or
13CNRI not be used in advertising or publicity pertaining to
14distribution of the software without specific, written prior
15permission.
Guido van Rossumf70e43a1991-02-19 12:39:46 +000016
Guido van Rossumd266eb41996-10-25 14:44:06 +000017While CWI is the initial source for this software, a modified version
18is made available by the Corporation for National Research Initiatives
19(CNRI) at the Internet address ftp://ftp.python.org.
20
21STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH
22REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
23MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH
24CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
25DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
26PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
27TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
28PERFORMANCE OF THIS SOFTWARE.
Guido van Rossumf70e43a1991-02-19 12:39:46 +000029
30******************************************************************/
31
Guido van Rossum3f5da241990-12-20 15:06:42 +000032/* Generic object operations; and implementation of None (NoObject) */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000033
Guido van Rossumc0b618a1997-05-02 03:12:38 +000034#include "Python.h"
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000035
Fred Drake13634cf2000-06-29 19:17:04 +000036#include "mymath.h"
37
Guido van Rossume92e6102000-04-24 15:40:53 +000038/* just for trashcan: */
39#include "compile.h"
40#include "frameobject.h"
41#include "traceback.h"
42
Guido van Rossum6f9e4331995-03-29 16:57:48 +000043#if defined( Py_TRACE_REFS ) || defined( Py_REF_DEBUG )
Guido van Rossumbffd6832000-01-20 22:32:56 +000044DL_IMPORT(long) _Py_RefTotal;
Guido van Rossum3f5da241990-12-20 15:06:42 +000045#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000046
Guido van Rossum3f5da241990-12-20 15:06:42 +000047/* Object allocation routines used by NEWOBJ and NEWVAROBJ macros.
48 These are used by the individual routines for object creation.
49 Do not call them otherwise, they do not initialize the object! */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000050
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +000051#ifdef COUNT_ALLOCS
Guido van Rossumc0b618a1997-05-02 03:12:38 +000052static PyTypeObject *type_list;
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +000053extern int tuple_zero_allocs, fast_tuple_allocs;
54extern int quick_int_allocs, quick_neg_int_allocs;
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +000055extern int null_strings, one_strings;
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +000056void
57dump_counts()
58{
Guido van Rossumc0b618a1997-05-02 03:12:38 +000059 PyTypeObject *tp;
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +000060
61 for (tp = type_list; tp; tp = tp->tp_next)
Sjoerd Mullender52c1f511993-10-25 08:40:52 +000062 fprintf(stderr, "%s alloc'd: %d, freed: %d, max in use: %d\n",
63 tp->tp_name, tp->tp_alloc, tp->tp_free,
64 tp->tp_maxalloc);
65 fprintf(stderr, "fast tuple allocs: %d, empty: %d\n",
66 fast_tuple_allocs, tuple_zero_allocs);
67 fprintf(stderr, "fast int allocs: pos: %d, neg: %d\n",
68 quick_int_allocs, quick_neg_int_allocs);
69 fprintf(stderr, "null strings: %d, 1-strings: %d\n",
70 null_strings, one_strings);
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +000071}
72
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +000073PyObject *
74get_counts()
75{
76 PyTypeObject *tp;
77 PyObject *result;
78 PyObject *v;
79
80 result = PyList_New(0);
81 if (result == NULL)
82 return NULL;
83 for (tp = type_list; tp; tp = tp->tp_next) {
84 v = Py_BuildValue("(siii)", tp->tp_name, tp->tp_alloc,
85 tp->tp_free, tp->tp_maxalloc);
86 if (v == NULL) {
87 Py_DECREF(result);
88 return NULL;
89 }
90 if (PyList_Append(result, v) < 0) {
91 Py_DECREF(v);
92 Py_DECREF(result);
93 return NULL;
94 }
95 Py_DECREF(v);
96 }
97 return result;
98}
99
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +0000100void
101inc_count(tp)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000102 PyTypeObject *tp;
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +0000103{
104 if (tp->tp_alloc == 0) {
Guido van Rossumd8953cb1995-04-06 14:46:26 +0000105 /* first time; insert in linked list */
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +0000106 if (tp->tp_next != NULL) /* sanity check */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000107 Py_FatalError("XXX inc_count sanity check");
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +0000108 tp->tp_next = type_list;
109 type_list = tp;
110 }
111 tp->tp_alloc++;
112 if (tp->tp_alloc - tp->tp_free > tp->tp_maxalloc)
113 tp->tp_maxalloc = tp->tp_alloc - tp->tp_free;
114}
115#endif
116
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000117PyObject *
Guido van Rossumb18618d2000-05-03 23:44:39 +0000118PyObject_Init(op, tp)
Guido van Rossumf5030ab1996-07-21 02:30:39 +0000119 PyObject *op;
Guido van Rossumb18618d2000-05-03 23:44:39 +0000120 PyTypeObject *tp;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000121{
Guido van Rossumb18618d2000-05-03 23:44:39 +0000122 if (op == NULL) {
123 PyErr_SetString(PyExc_SystemError,
124 "NULL object passed to PyObject_Init");
125 return op;
126 }
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000127#ifdef WITH_CYCLE_GC
128 if (PyType_IS_GC(tp))
129 op = (PyObject *) PyObject_FROM_GC(op);
130#endif
Guido van Rossumb18618d2000-05-03 23:44:39 +0000131 /* Any changes should be reflected in PyObject_INIT (objimpl.h) */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000132 op->ob_type = tp;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000133 _Py_NewReference(op);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000134 return op;
135}
136
Guido van Rossumb18618d2000-05-03 23:44:39 +0000137PyVarObject *
138PyObject_InitVar(op, tp, size)
139 PyVarObject *op;
140 PyTypeObject *tp;
141 int size;
142{
143 if (op == NULL) {
144 PyErr_SetString(PyExc_SystemError,
145 "NULL object passed to PyObject_InitVar");
146 return op;
147 }
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000148#ifdef WITH_CYCLE_GC
149 if (PyType_IS_GC(tp))
150 op = (PyVarObject *) PyObject_FROM_GC(op);
151#endif
Guido van Rossumb18618d2000-05-03 23:44:39 +0000152 /* Any changes should be reflected in PyObject_INIT_VAR */
153 op->ob_size = size;
154 op->ob_type = tp;
155 _Py_NewReference((PyObject *)op);
156 return op;
157}
158
159PyObject *
160_PyObject_New(tp)
161 PyTypeObject *tp;
162{
163 PyObject *op;
164 op = (PyObject *) PyObject_MALLOC(_PyObject_SIZE(tp));
165 if (op == NULL)
166 return PyErr_NoMemory();
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000167#ifdef WITH_CYCLE_GC
168 if (PyType_IS_GC(tp))
169 op = (PyObject *) PyObject_FROM_GC(op);
170#endif
Guido van Rossumb18618d2000-05-03 23:44:39 +0000171 return PyObject_INIT(op, tp);
172}
173
Guido van Rossumd0c87ee1997-05-15 21:31:03 +0000174PyVarObject *
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000175_PyObject_NewVar(tp, size)
176 PyTypeObject *tp;
Guido van Rossum2497ead1995-02-10 17:00:27 +0000177 int size;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000178{
Guido van Rossumb18618d2000-05-03 23:44:39 +0000179 PyVarObject *op;
180 op = (PyVarObject *) PyObject_MALLOC(_PyObject_VAR_SIZE(tp, size));
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000181 if (op == NULL)
Guido van Rossumd0c87ee1997-05-15 21:31:03 +0000182 return (PyVarObject *)PyErr_NoMemory();
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000183#ifdef WITH_CYCLE_GC
184 if (PyType_IS_GC(tp))
185 op = (PyVarObject *) PyObject_FROM_GC(op);
186#endif
Guido van Rossumb18618d2000-05-03 23:44:39 +0000187 return PyObject_INIT_VAR(op, tp, size);
188}
189
190void
191_PyObject_Del(op)
192 PyObject *op;
193{
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000194#ifdef WITH_CYCLE_GC
195 if (PyType_IS_GC(op->ob_type)) {
196 PyGC_Head *g = PyObject_AS_GC(op);
197 PyObject_FREE(g);
198 } else
199#endif
200 {
201 PyObject_FREE(op);
202 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000203}
204
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000205#ifndef WITH_CYCLE_GC
206/* extension modules might need these */
207void _PyGC_Insert(PyObject *op) { }
208void _PyGC_Remove(PyObject *op) { }
209#endif
210
Guido van Rossum90933611991-06-07 16:10:43 +0000211int
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000212PyObject_Print(op, fp, flags)
213 PyObject *op;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000214 FILE *fp;
215 int flags;
216{
Guido van Rossum278ef591991-07-27 21:40:24 +0000217 int ret = 0;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000218 if (PyErr_CheckSignals())
Guido van Rossum90933611991-06-07 16:10:43 +0000219 return -1;
Guido van Rossum9b00dfa1998-04-28 16:06:54 +0000220#ifdef USE_STACKCHECK
221 if (PyOS_CheckStack()) {
222 PyErr_SetString(PyExc_MemoryError, "Stack overflow");
223 return -1;
224 }
225#endif
Guido van Rossum687ef6e2000-01-12 16:28:58 +0000226 clearerr(fp); /* Clear any previous error condition */
Guido van Rossum90933611991-06-07 16:10:43 +0000227 if (op == NULL) {
228 fprintf(fp, "<nil>");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000229 }
Guido van Rossum90933611991-06-07 16:10:43 +0000230 else {
231 if (op->ob_refcnt <= 0)
Guido van Rossum2e8f6141992-09-03 20:32:55 +0000232 fprintf(fp, "<refcnt %u at %lx>",
233 op->ob_refcnt, (long)op);
234 else if (op->ob_type->tp_print == NULL) {
235 if (op->ob_type->tp_repr == NULL) {
236 fprintf(fp, "<%s object at %lx>",
237 op->ob_type->tp_name, (long)op);
238 }
239 else {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000240 PyObject *s;
241 if (flags & Py_PRINT_RAW)
242 s = PyObject_Str(op);
Guido van Rossumc6004111993-11-05 10:22:19 +0000243 else
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000244 s = PyObject_Repr(op);
Guido van Rossum2e8f6141992-09-03 20:32:55 +0000245 if (s == NULL)
246 ret = -1;
Guido van Rossum2e8f6141992-09-03 20:32:55 +0000247 else {
Guido van Rossum565798d1998-04-21 22:25:01 +0000248 ret = PyObject_Print(s, fp,
249 Py_PRINT_RAW);
Guido van Rossum2e8f6141992-09-03 20:32:55 +0000250 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000251 Py_XDECREF(s);
Guido van Rossum2e8f6141992-09-03 20:32:55 +0000252 }
253 }
Guido van Rossum90933611991-06-07 16:10:43 +0000254 else
Guido van Rossum278ef591991-07-27 21:40:24 +0000255 ret = (*op->ob_type->tp_print)(op, fp, flags);
Guido van Rossum90933611991-06-07 16:10:43 +0000256 }
Guido van Rossum278ef591991-07-27 21:40:24 +0000257 if (ret == 0) {
258 if (ferror(fp)) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000259 PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossum278ef591991-07-27 21:40:24 +0000260 clearerr(fp);
261 ret = -1;
262 }
263 }
264 return ret;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000265}
266
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000267PyObject *
268PyObject_Repr(v)
269 PyObject *v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000270{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000271 if (PyErr_CheckSignals())
Guido van Rossum90933611991-06-07 16:10:43 +0000272 return NULL;
Guido van Rossum9b00dfa1998-04-28 16:06:54 +0000273#ifdef USE_STACKCHECK
274 if (PyOS_CheckStack()) {
275 PyErr_SetString(PyExc_MemoryError, "Stack overflow");
276 return NULL;
277 }
278#endif
Guido van Rossum90933611991-06-07 16:10:43 +0000279 if (v == NULL)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000280 return PyString_FromString("<NULL>");
Guido van Rossum90933611991-06-07 16:10:43 +0000281 else if (v->ob_type->tp_repr == NULL) {
282 char buf[120];
283 sprintf(buf, "<%.80s object at %lx>",
284 v->ob_type->tp_name, (long)v);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000285 return PyString_FromString(buf);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000286 }
Guido van Rossum4c08d552000-03-10 22:55:18 +0000287 else {
288 PyObject *res;
289 res = (*v->ob_type->tp_repr)(v);
290 if (res == NULL)
291 return NULL;
292 if (!PyString_Check(res)) {
293 PyErr_Format(PyExc_TypeError,
Guido van Rossum5db862d2000-04-10 12:46:51 +0000294 "__repr__ returned non-string (type %.200s)",
Guido van Rossum4c08d552000-03-10 22:55:18 +0000295 res->ob_type->tp_name);
296 Py_DECREF(res);
297 return NULL;
298 }
299 return res;
300 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000301}
302
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000303PyObject *
304PyObject_Str(v)
305 PyObject *v;
Guido van Rossumc6004111993-11-05 10:22:19 +0000306{
Guido van Rossum4c08d552000-03-10 22:55:18 +0000307 PyObject *res;
308
Guido van Rossumc6004111993-11-05 10:22:19 +0000309 if (v == NULL)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000310 return PyString_FromString("<NULL>");
311 else if (PyString_Check(v)) {
312 Py_INCREF(v);
Guido van Rossumc6004111993-11-05 10:22:19 +0000313 return v;
314 }
Guido van Rossum32b582b1995-01-17 16:35:13 +0000315 else if (v->ob_type->tp_str != NULL)
Guido van Rossum4c08d552000-03-10 22:55:18 +0000316 res = (*v->ob_type->tp_str)(v);
Guido van Rossumc6004111993-11-05 10:22:19 +0000317 else {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000318 PyObject *func;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000319 if (!PyInstance_Check(v) ||
320 (func = PyObject_GetAttrString(v, "__str__")) == NULL) {
321 PyErr_Clear();
322 return PyObject_Repr(v);
Guido van Rossumc6004111993-11-05 10:22:19 +0000323 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000324 res = PyEval_CallObject(func, (PyObject *)NULL);
325 Py_DECREF(func);
Guido van Rossumc6004111993-11-05 10:22:19 +0000326 }
Guido van Rossum4c08d552000-03-10 22:55:18 +0000327 if (res == NULL)
328 return NULL;
329 if (!PyString_Check(res)) {
330 PyErr_Format(PyExc_TypeError,
Guido van Rossum5db862d2000-04-10 12:46:51 +0000331 "__str__ returned non-string (type %.200s)",
Guido van Rossum4c08d552000-03-10 22:55:18 +0000332 res->ob_type->tp_name);
333 Py_DECREF(res);
334 return NULL;
335 }
336 return res;
Guido van Rossumc6004111993-11-05 10:22:19 +0000337}
338
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000339static PyObject *
Guido van Rossum20566841995-01-12 11:26:10 +0000340do_cmp(v, w)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000341 PyObject *v, *w;
Guido van Rossum20566841995-01-12 11:26:10 +0000342{
Guido van Rossumc8b6df91997-05-23 00:06:51 +0000343 long c;
Guido van Rossum20566841995-01-12 11:26:10 +0000344 /* __rcmp__ actually won't be called unless __cmp__ isn't defined,
345 because the check in cmpobject() reverses the objects first.
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000346 This is intentional -- it makes no sense to define cmp(x,y)
347 different than -cmp(y,x). */
348 if (PyInstance_Check(v) || PyInstance_Check(w))
349 return PyInstance_DoBinOp(v, w, "__cmp__", "__rcmp__", do_cmp);
Guido van Rossumc8b6df91997-05-23 00:06:51 +0000350 c = PyObject_Compare(v, w);
351 if (c && PyErr_Occurred())
352 return NULL;
353 return PyInt_FromLong(c);
Guido van Rossum20566841995-01-12 11:26:10 +0000354}
355
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000356PyObject *_PyCompareState_Key;
357
358/* _PyCompareState_nesting is incremented beforing call compare (for
359 some types) and decremented on exit. If the count exceeds the
360 nesting limit, enable code to detect circular data structures.
361*/
362#define NESTING_LIMIT 500
363int _PyCompareState_nesting = 0;
364
365static PyObject*
366get_inprogress_dict()
367{
368 PyObject *tstate_dict, *inprogress;
369
370 tstate_dict = PyThreadState_GetDict();
371 if (tstate_dict == NULL) {
372 PyErr_BadInternalCall();
373 return NULL;
374 }
375 inprogress = PyDict_GetItem(tstate_dict, _PyCompareState_Key);
376 if (inprogress == NULL) {
377 PyErr_Clear();
378 inprogress = PyDict_New();
379 if (inprogress == NULL)
380 return NULL;
381 if (PyDict_SetItem(tstate_dict, _PyCompareState_Key,
382 inprogress) == -1) {
383 Py_DECREF(inprogress);
384 return NULL;
385 }
Jeremy Hyltona251ea02000-06-09 16:20:39 +0000386 Py_DECREF(inprogress);
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000387 }
388 return inprogress;
389}
390
391static PyObject *
392make_pair(v, w)
393 PyObject *v, *w;
394{
395 PyObject *pair;
396
397 pair = PyTuple_New(2);
398 if (pair == NULL) {
399 return NULL;
400 }
Guido van Rossumad89bbc2000-06-28 21:57:18 +0000401 if (v <= w) {
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000402 PyTuple_SET_ITEM(pair, 0, PyLong_FromVoidPtr((void *)v));
403 PyTuple_SET_ITEM(pair, 1, PyLong_FromVoidPtr((void *)w));
404 } else {
405 PyTuple_SET_ITEM(pair, 0, PyLong_FromVoidPtr((void *)w));
406 PyTuple_SET_ITEM(pair, 1, PyLong_FromVoidPtr((void *)v));
407 }
408 return pair;
409}
410
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000411int
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000412PyObject_Compare(v, w)
413 PyObject *v, *w;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000414{
Guido van Rossumcd5a5f61998-06-09 18:58:44 +0000415 PyTypeObject *vtp, *wtp;
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000416 int result;
417
Guido van Rossumc8b6df91997-05-23 00:06:51 +0000418 if (v == NULL || w == NULL) {
419 PyErr_BadInternalCall();
420 return -1;
421 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000422 if (v == w)
423 return 0;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000424 if (PyInstance_Check(v) || PyInstance_Check(w)) {
425 PyObject *res;
Guido van Rossum20566841995-01-12 11:26:10 +0000426 int c;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000427 if (!PyInstance_Check(v))
428 return -PyObject_Compare(w, v);
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000429 if (++_PyCompareState_nesting > NESTING_LIMIT) {
430 PyObject *inprogress, *pair;
431
432 inprogress = get_inprogress_dict();
433 if (inprogress == NULL) {
434 return -1;
435 }
436 pair = make_pair(v, w);
437 if (PyDict_GetItem(inprogress, pair)) {
438 /* already comparing these objects. assume
439 they're equal until shown otherwise */
440 Py_DECREF(pair);
441 --_PyCompareState_nesting;
442 return 0;
443 }
444 if (PyDict_SetItem(inprogress, pair, pair) == -1) {
445 return -1;
446 }
447 res = do_cmp(v, w);
448 _PyCompareState_nesting--;
449 /* XXX DelItem shouldn't fail */
450 PyDict_DelItem(inprogress, pair);
451 Py_DECREF(pair);
452 } else {
453 res = do_cmp(v, w);
454 }
Guido van Rossumc8b6df91997-05-23 00:06:51 +0000455 if (res == NULL)
456 return -1;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000457 if (!PyInt_Check(res)) {
458 Py_DECREF(res);
Guido van Rossumc8b6df91997-05-23 00:06:51 +0000459 PyErr_SetString(PyExc_TypeError,
460 "comparison did not return an int");
461 return -1;
Guido van Rossum20566841995-01-12 11:26:10 +0000462 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000463 c = PyInt_AsLong(res);
464 Py_DECREF(res);
Guido van Rossum20566841995-01-12 11:26:10 +0000465 return (c < 0) ? -1 : (c > 0) ? 1 : 0;
466 }
Guido van Rossumcd5a5f61998-06-09 18:58:44 +0000467 if ((vtp = v->ob_type) != (wtp = w->ob_type)) {
468 char *vname = vtp->tp_name;
469 char *wname = wtp->tp_name;
Guido van Rossumb4db1941998-07-21 21:56:41 +0000470 if (vtp->tp_as_number != NULL && wtp->tp_as_number != NULL) {
Guido van Rossum242c6421997-11-19 16:03:17 +0000471 int err;
472 err = PyNumber_CoerceEx(&v, &w);
473 if (err < 0)
Guido van Rossumc8b6df91997-05-23 00:06:51 +0000474 return -1;
Guido van Rossum242c6421997-11-19 16:03:17 +0000475 else if (err == 0) {
Guido van Rossumb4db1941998-07-21 21:56:41 +0000476 int cmp;
477 vtp = v->ob_type;
478 if (vtp->tp_compare == NULL)
479 cmp = (v < w) ? -1 : 1;
480 else
481 cmp = (*vtp->tp_compare)(v, w);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000482 Py_DECREF(v);
483 Py_DECREF(w);
Guido van Rossum9fb03681991-07-01 18:48:04 +0000484 return cmp;
485 }
486 }
Guido van Rossumb244f692000-04-10 13:42:33 +0000487 else if (PyUnicode_Check(v) || PyUnicode_Check(w)) {
488 int result = PyUnicode_Compare(v, w);
489 if (result == -1 && PyErr_Occurred() &&
490 PyErr_ExceptionMatches(PyExc_TypeError))
491 /* TypeErrors are ignored: if Unicode coercion
492 fails due to one of the arguments not
493 having the right type, we continue as
494 defined by the coercion protocol (see
495 above). Luckily, decoding errors are
496 reported as ValueErrors and are not masked
497 by this technique. */
498 PyErr_Clear();
499 else
500 return result;
501 }
Guido van Rossumcd5a5f61998-06-09 18:58:44 +0000502 else if (vtp->tp_as_number != NULL)
503 vname = "";
504 else if (wtp->tp_as_number != NULL)
505 wname = "";
506 /* Numerical types compare smaller than all other types */
507 return strcmp(vname, wname);
Guido van Rossum9fb03681991-07-01 18:48:04 +0000508 }
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000509 if (vtp->tp_compare == NULL) {
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000510 return (v < w) ? -1 : 1;
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000511 }
512 if (++_PyCompareState_nesting > NESTING_LIMIT
513 && (vtp->tp_as_mapping
514 || (vtp->tp_as_sequence && !PyString_Check(v)))) {
515 PyObject *inprogress, *pair;
516
517 inprogress = get_inprogress_dict();
518 if (inprogress == NULL) {
519 return -1;
520 }
521 pair = make_pair(v, w);
522 if (PyDict_GetItem(inprogress, pair)) {
523 /* already comparing these objects. assume
524 they're equal until shown otherwise */
525 _PyCompareState_nesting--;
526 Py_DECREF(pair);
527 return 0;
528 }
529 if (PyDict_SetItem(inprogress, pair, pair) == -1) {
530 return -1;
531 }
532 result = (*vtp->tp_compare)(v, w);
533 _PyCompareState_nesting--;
534 PyDict_DelItem(inprogress, pair); /* XXX shouldn't fail */
535 Py_DECREF(pair);
536 } else {
537 result = (*vtp->tp_compare)(v, w);
538 }
539 return result;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000540}
541
Fred Drake13634cf2000-06-29 19:17:04 +0000542
543/* Set of hash utility functions to help maintaining the invariant that
544 iff a==b then hash(a)==hash(b)
545
546 All the utility functions (_Py_Hash*()) return "-1" to signify an error.
547*/
548
549long
550_Py_HashDouble(v)
551 double v;
552{
553 /* Use frexp to get at the bits in the double.
554 * Since the VAX D double format has 56 mantissa bits, which is the
555 * most of any double format in use, each of these parts may have as
556 * many as (but no more than) 56 significant bits.
557 * So, assuming sizeof(long) >= 4, each part can be broken into two longs;
558 * frexp and multiplication are used to do that.
559 * Also, since the Cray double format has 15 exponent bits, which is the
560 * most of any double format in use, shifting the exponent field left by
561 * 15 won't overflow a long (again assuming sizeof(long) >= 4).
562 */
563 int expo;
564 long hipart;
565
566 v = frexp(v, &expo);
567 v = v * 2147483648.0; /* 2**31 */
568 hipart = (long)v; /* Take the top 32 bits */
569 v = (v - (double)hipart) * 2147483648.0; /* Get the next 32 bits */
570
571 return hipart + (long)v + (expo << 15); /* Combine everything */
572}
573
574long
575_Py_HashPointer(p)
576 void *p;
577{
578#if SIZEOF_LONG >= SIZEOF_VOID_P
579 return (long)p;
580#else
581 /* convert to a Python long and hash that */
582 PyObject* longobj;
583 long x;
584
585 if ((longobj = PyLong_FromVoidPtr(p)) == NULL) {
586 x = -1;
587 goto finally;
588 }
589 x = PyObject_Hash(longobj);
590
591finally:
592 Py_XDECREF(longobj);
593 return x;
594#endif
595}
596
597
Guido van Rossum9bfef441993-03-29 10:43:31 +0000598long
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000599PyObject_Hash(v)
600 PyObject *v;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000601{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000602 PyTypeObject *tp = v->ob_type;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000603 if (tp->tp_hash != NULL)
604 return (*tp->tp_hash)(v);
Fred Drake13634cf2000-06-29 19:17:04 +0000605 if (tp->tp_compare == NULL) {
606 return _Py_HashPointer(v); /* Use address as hash value */
607 }
Guido van Rossum9bfef441993-03-29 10:43:31 +0000608 /* If there's a cmp but no hash defined, the object can't be hashed */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000609 PyErr_SetString(PyExc_TypeError, "unhashable type");
Guido van Rossum9bfef441993-03-29 10:43:31 +0000610 return -1;
611}
612
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000613PyObject *
614PyObject_GetAttrString(v, name)
615 PyObject *v;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000616 char *name;
617{
Guido van Rossumd8eb1b31996-08-09 20:52:03 +0000618 if (v->ob_type->tp_getattro != NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000619 PyObject *w, *res;
Guido van Rossumc6d06701997-01-18 07:57:16 +0000620 w = PyString_InternFromString(name);
Guido van Rossumd8eb1b31996-08-09 20:52:03 +0000621 if (w == NULL)
622 return NULL;
623 res = (*v->ob_type->tp_getattro)(v, w);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000624 Py_XDECREF(w);
Guido van Rossumd8eb1b31996-08-09 20:52:03 +0000625 return res;
626 }
627
Guido van Rossum3f5da241990-12-20 15:06:42 +0000628 if (v->ob_type->tp_getattr == NULL) {
Guido van Rossumdb935161998-01-19 22:16:36 +0000629 PyErr_Format(PyExc_AttributeError,
630 "'%.50s' object has no attribute '%.400s'",
631 v->ob_type->tp_name,
632 name);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000633 return NULL;
634 }
635 else {
636 return (*v->ob_type->tp_getattr)(v, name);
637 }
638}
639
640int
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000641PyObject_HasAttrString(v, name)
642 PyObject *v;
Guido van Rossumed18fdc1993-07-11 19:55:34 +0000643 char *name;
644{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000645 PyObject *res = PyObject_GetAttrString(v, name);
Guido van Rossumed18fdc1993-07-11 19:55:34 +0000646 if (res != NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000647 Py_DECREF(res);
Guido van Rossumed18fdc1993-07-11 19:55:34 +0000648 return 1;
649 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000650 PyErr_Clear();
Guido van Rossumed18fdc1993-07-11 19:55:34 +0000651 return 0;
652}
653
654int
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000655PyObject_SetAttrString(v, name, w)
656 PyObject *v;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000657 char *name;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000658 PyObject *w;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000659{
Guido van Rossumd8eb1b31996-08-09 20:52:03 +0000660 if (v->ob_type->tp_setattro != NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000661 PyObject *s;
Guido van Rossumd8eb1b31996-08-09 20:52:03 +0000662 int res;
Guido van Rossumc6d06701997-01-18 07:57:16 +0000663 s = PyString_InternFromString(name);
Guido van Rossumd8eb1b31996-08-09 20:52:03 +0000664 if (s == NULL)
Guido van Rossumb7fc3041996-09-11 22:51:25 +0000665 return -1;
Guido van Rossumd8eb1b31996-08-09 20:52:03 +0000666 res = (*v->ob_type->tp_setattro)(v, s, w);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000667 Py_XDECREF(s);
Guido van Rossumd8eb1b31996-08-09 20:52:03 +0000668 return res;
669 }
670
Guido van Rossum3f5da241990-12-20 15:06:42 +0000671 if (v->ob_type->tp_setattr == NULL) {
672 if (v->ob_type->tp_getattr == NULL)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000673 PyErr_SetString(PyExc_TypeError,
Guido van Rossum3ea74121991-12-24 13:28:03 +0000674 "attribute-less object (assign or del)");
Guido van Rossum3f5da241990-12-20 15:06:42 +0000675 else
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000676 PyErr_SetString(PyExc_TypeError,
Guido van Rossum3ea74121991-12-24 13:28:03 +0000677 "object has read-only attributes");
Guido van Rossum73531a31990-12-20 23:12:40 +0000678 return -1;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000679 }
680 else {
681 return (*v->ob_type->tp_setattr)(v, name, w);
682 }
683}
684
Guido van Rossum98ff96a1997-05-20 18:34:44 +0000685PyObject *
686PyObject_GetAttr(v, name)
687 PyObject *v;
688 PyObject *name;
689{
690 if (v->ob_type->tp_getattro != NULL)
691 return (*v->ob_type->tp_getattro)(v, name);
Jeremy Hylton99a8f902000-06-23 14:36:32 +0000692
693 if (!PyString_Check(name)) {
694 PyErr_SetString(PyExc_TypeError,
695 "attribute name must be string");
696 return NULL;
697 }
698 return PyObject_GetAttrString(v, PyString_AS_STRING(name));
Guido van Rossum98ff96a1997-05-20 18:34:44 +0000699}
700
701int
702PyObject_HasAttr(v, name)
703 PyObject *v;
704 PyObject *name;
705{
706 PyObject *res = PyObject_GetAttr(v, name);
707 if (res != NULL) {
708 Py_DECREF(res);
709 return 1;
710 }
711 PyErr_Clear();
712 return 0;
713}
714
715int
716PyObject_SetAttr(v, name, value)
717 PyObject *v;
718 PyObject *name;
719 PyObject *value;
720{
721 int err;
722 Py_INCREF(name);
Jeremy Hylton99a8f902000-06-23 14:36:32 +0000723 if (PyString_Check(name))
724 PyString_InternInPlace(&name);
Guido van Rossum98ff96a1997-05-20 18:34:44 +0000725 if (v->ob_type->tp_setattro != NULL)
726 err = (*v->ob_type->tp_setattro)(v, name, value);
Jeremy Hylton99a8f902000-06-23 14:36:32 +0000727 else if (PyString_Check(name)) {
Guido van Rossum98ff96a1997-05-20 18:34:44 +0000728 err = PyObject_SetAttrString(
Jeremy Hylton99a8f902000-06-23 14:36:32 +0000729 v, PyString_AS_STRING(name), value);
730 }
731 else {
732 PyErr_SetString(PyExc_TypeError,
733 "attribute name must be string");
734 err = -1;
735 }
Guido van Rossum98ff96a1997-05-20 18:34:44 +0000736 Py_DECREF(name);
737 return err;
738}
739
Guido van Rossum6ac258d1993-05-12 08:24:20 +0000740/* Test a value used as condition, e.g., in a for or if statement.
741 Return -1 if an error occurred */
742
743int
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000744PyObject_IsTrue(v)
745 PyObject *v;
Guido van Rossum6ac258d1993-05-12 08:24:20 +0000746{
747 int res;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000748 if (v == Py_None)
Guido van Rossum6ac258d1993-05-12 08:24:20 +0000749 res = 0;
Guido van Rossum1c4f4581998-05-22 00:53:24 +0000750 else if (v->ob_type->tp_as_number != NULL &&
751 v->ob_type->tp_as_number->nb_nonzero != NULL)
Guido van Rossum6ac258d1993-05-12 08:24:20 +0000752 res = (*v->ob_type->tp_as_number->nb_nonzero)(v);
Guido van Rossum1c4f4581998-05-22 00:53:24 +0000753 else if (v->ob_type->tp_as_mapping != NULL &&
754 v->ob_type->tp_as_mapping->mp_length != NULL)
Guido van Rossum6ac258d1993-05-12 08:24:20 +0000755 res = (*v->ob_type->tp_as_mapping->mp_length)(v);
Guido van Rossum1c4f4581998-05-22 00:53:24 +0000756 else if (v->ob_type->tp_as_sequence != NULL &&
757 v->ob_type->tp_as_sequence->sq_length != NULL)
Guido van Rossum6ac258d1993-05-12 08:24:20 +0000758 res = (*v->ob_type->tp_as_sequence->sq_length)(v);
759 else
760 res = 1;
761 if (res > 0)
762 res = 1;
763 return res;
764}
765
Guido van Rossumc3d3f961998-04-09 17:53:59 +0000766/* equivalent of 'not v'
767 Return -1 if an error occurred */
768
769int
770PyObject_Not(v)
771 PyObject *v;
772{
773 int res;
774 res = PyObject_IsTrue(v);
775 if (res < 0)
776 return res;
777 return res == 0;
778}
779
Guido van Rossum5524a591995-01-10 15:26:20 +0000780/* Coerce two numeric types to the "larger" one.
781 Increment the reference count on each argument.
782 Return -1 and raise an exception if no coercion is possible
783 (and then no reference count is incremented).
784*/
785
786int
Guido van Rossum242c6421997-11-19 16:03:17 +0000787PyNumber_CoerceEx(pv, pw)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000788 PyObject **pv, **pw;
Guido van Rossum5524a591995-01-10 15:26:20 +0000789{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000790 register PyObject *v = *pv;
791 register PyObject *w = *pw;
Guido van Rossum5524a591995-01-10 15:26:20 +0000792 int res;
793
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000794 if (v->ob_type == w->ob_type && !PyInstance_Check(v)) {
795 Py_INCREF(v);
796 Py_INCREF(w);
Guido van Rossum5524a591995-01-10 15:26:20 +0000797 return 0;
798 }
799 if (v->ob_type->tp_as_number && v->ob_type->tp_as_number->nb_coerce) {
800 res = (*v->ob_type->tp_as_number->nb_coerce)(pv, pw);
801 if (res <= 0)
802 return res;
803 }
804 if (w->ob_type->tp_as_number && w->ob_type->tp_as_number->nb_coerce) {
805 res = (*w->ob_type->tp_as_number->nb_coerce)(pw, pv);
806 if (res <= 0)
807 return res;
808 }
Guido van Rossum242c6421997-11-19 16:03:17 +0000809 return 1;
810}
811
812int
813PyNumber_Coerce(pv, pw)
814 PyObject **pv, **pw;
815{
816 int err = PyNumber_CoerceEx(pv, pw);
817 if (err <= 0)
818 return err;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000819 PyErr_SetString(PyExc_TypeError, "number coercion failed");
Guido van Rossum5524a591995-01-10 15:26:20 +0000820 return -1;
821}
822
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000823
Guido van Rossum49b11fe1995-01-26 00:38:22 +0000824/* Test whether an object can be called */
825
826int
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000827PyCallable_Check(x)
828 PyObject *x;
Guido van Rossum49b11fe1995-01-26 00:38:22 +0000829{
830 if (x == NULL)
831 return 0;
832 if (x->ob_type->tp_call != NULL ||
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000833 PyFunction_Check(x) ||
834 PyMethod_Check(x) ||
835 PyCFunction_Check(x) ||
836 PyClass_Check(x))
Guido van Rossum49b11fe1995-01-26 00:38:22 +0000837 return 1;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000838 if (PyInstance_Check(x)) {
839 PyObject *call = PyObject_GetAttrString(x, "__call__");
Guido van Rossum49b11fe1995-01-26 00:38:22 +0000840 if (call == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000841 PyErr_Clear();
Guido van Rossum49b11fe1995-01-26 00:38:22 +0000842 return 0;
843 }
844 /* Could test recursively but don't, for fear of endless
845 recursion if some joker sets self.__call__ = self */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000846 Py_DECREF(call);
Guido van Rossum49b11fe1995-01-26 00:38:22 +0000847 return 1;
848 }
849 return 0;
850}
851
852
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000853/*
854NoObject is usable as a non-NULL undefined value, used by the macro None.
855There is (and should be!) no way to create other objects of this type,
Guido van Rossum3f5da241990-12-20 15:06:42 +0000856so there is exactly one (which is indestructible, by the way).
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000857*/
858
Guido van Rossum0c182a11992-03-27 17:26:13 +0000859/* ARGSUSED */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000860static PyObject *
Guido van Rossum3f5da241990-12-20 15:06:42 +0000861none_repr(op)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000862 PyObject *op;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000863{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000864 return PyString_FromString("None");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000865}
866
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000867static PyTypeObject PyNothing_Type = {
868 PyObject_HEAD_INIT(&PyType_Type)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000869 0,
Guido van Rossum3f5da241990-12-20 15:06:42 +0000870 "None",
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000871 0,
872 0,
873 0, /*tp_dealloc*/ /*never called*/
Guido van Rossum7066dd71992-09-17 17:54:56 +0000874 0, /*tp_print*/
Guido van Rossum3f5da241990-12-20 15:06:42 +0000875 0, /*tp_getattr*/
876 0, /*tp_setattr*/
877 0, /*tp_compare*/
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000878 (reprfunc)none_repr, /*tp_repr*/
Guido van Rossum3f5da241990-12-20 15:06:42 +0000879 0, /*tp_as_number*/
880 0, /*tp_as_sequence*/
881 0, /*tp_as_mapping*/
Guido van Rossum9bfef441993-03-29 10:43:31 +0000882 0, /*tp_hash */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000883};
884
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000885PyObject _Py_NoneStruct = {
886 PyObject_HEAD_INIT(&PyNothing_Type)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000887};
888
889
Guido van Rossum84a90321996-05-22 16:34:47 +0000890#ifdef Py_TRACE_REFS
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000891
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000892static PyObject refchain = {&refchain, &refchain};
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000893
Guido van Rossumaacdc9d1996-08-12 21:32:12 +0000894void
Guido van Rossume09fb551997-08-05 02:04:34 +0000895_Py_ResetReferences()
896{
897 refchain._ob_prev = refchain._ob_next = &refchain;
898 _Py_RefTotal = 0;
899}
900
901void
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000902_Py_NewReference(op)
903 PyObject *op;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000904{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000905 _Py_RefTotal++;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000906 op->ob_refcnt = 1;
907 op->_ob_next = refchain._ob_next;
908 op->_ob_prev = &refchain;
909 refchain._ob_next->_ob_prev = op;
910 refchain._ob_next = op;
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +0000911#ifdef COUNT_ALLOCS
912 inc_count(op->ob_type);
913#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000914}
915
Guido van Rossumaacdc9d1996-08-12 21:32:12 +0000916void
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000917_Py_ForgetReference(op)
918 register PyObject *op;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000919{
Guido van Rossumbffd6832000-01-20 22:32:56 +0000920#ifdef SLOW_UNREF_CHECK
Guido van Rossum4c08d552000-03-10 22:55:18 +0000921 register PyObject *p;
Guido van Rossumbffd6832000-01-20 22:32:56 +0000922#endif
Guido van Rossumd7047b31995-01-02 19:07:15 +0000923 if (op->ob_refcnt < 0)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000924 Py_FatalError("UNREF negative refcnt");
Guido van Rossum2e8f6141992-09-03 20:32:55 +0000925 if (op == &refchain ||
Guido van Rossumd7047b31995-01-02 19:07:15 +0000926 op->_ob_prev->_ob_next != op || op->_ob_next->_ob_prev != op)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000927 Py_FatalError("UNREF invalid object");
Guido van Rossum2e8f6141992-09-03 20:32:55 +0000928#ifdef SLOW_UNREF_CHECK
Guido van Rossum3f5da241990-12-20 15:06:42 +0000929 for (p = refchain._ob_next; p != &refchain; p = p->_ob_next) {
930 if (p == op)
931 break;
932 }
Guido van Rossumd7047b31995-01-02 19:07:15 +0000933 if (p == &refchain) /* Not found */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000934 Py_FatalError("UNREF unknown object");
Guido van Rossum2e8f6141992-09-03 20:32:55 +0000935#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000936 op->_ob_next->_ob_prev = op->_ob_prev;
937 op->_ob_prev->_ob_next = op->_ob_next;
Guido van Rossum2e8f6141992-09-03 20:32:55 +0000938 op->_ob_next = op->_ob_prev = NULL;
Guido van Rossumd8953cb1995-04-06 14:46:26 +0000939#ifdef COUNT_ALLOCS
940 op->ob_type->tp_free++;
941#endif
Guido van Rossum3f5da241990-12-20 15:06:42 +0000942}
943
Guido van Rossumaacdc9d1996-08-12 21:32:12 +0000944void
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000945_Py_Dealloc(op)
946 PyObject *op;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000947{
Guido van Rossum9776adf1994-09-07 14:36:45 +0000948 destructor dealloc = op->ob_type->tp_dealloc;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000949 _Py_ForgetReference(op);
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000950#ifndef WITH_CYCLE_GC
Guido van Rossume92e6102000-04-24 15:40:53 +0000951 if (_PyTrash_delete_nesting < PyTrash_UNWIND_LEVEL-1)
952 op->ob_type = NULL;
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000953#endif
Guido van Rossum9776adf1994-09-07 14:36:45 +0000954 (*dealloc)(op);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000955}
956
Guido van Rossumaacdc9d1996-08-12 21:32:12 +0000957void
Guido van Rossumded690f1996-05-24 20:48:31 +0000958_Py_PrintReferences(fp)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000959 FILE *fp;
960{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000961 PyObject *op;
Guido van Rossume09fb551997-08-05 02:04:34 +0000962 fprintf(fp, "Remaining objects:\n");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000963 for (op = refchain._ob_next; op != &refchain; op = op->_ob_next) {
964 fprintf(fp, "[%d] ", op->ob_refcnt);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000965 if (PyObject_Print(op, fp, 0) != 0)
966 PyErr_Clear();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000967 putc('\n', fp);
968 }
969}
970
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000971PyObject *
Guido van Rossumded690f1996-05-24 20:48:31 +0000972_Py_GetObjects(self, args)
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000973 PyObject *self;
974 PyObject *args;
975{
976 int i, n;
977 PyObject *t = NULL;
978 PyObject *res, *op;
979
980 if (!PyArg_ParseTuple(args, "i|O", &n, &t))
981 return NULL;
982 op = refchain._ob_next;
983 res = PyList_New(0);
984 if (res == NULL)
985 return NULL;
986 for (i = 0; (n == 0 || i < n) && op != &refchain; i++) {
987 while (op == self || op == args || op == res || op == t ||
988 t != NULL && op->ob_type != (PyTypeObject *) t) {
989 op = op->_ob_next;
990 if (op == &refchain)
991 return res;
992 }
993 if (PyList_Append(res, op) < 0) {
994 Py_DECREF(res);
995 return NULL;
996 }
997 op = op->_ob_next;
998 }
999 return res;
1000}
1001
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001002#endif
Guido van Rossum97ead3f1996-01-12 01:24:09 +00001003
1004
1005/* Hack to force loading of cobject.o */
Guido van Rossumda9c2711996-12-05 21:58:58 +00001006PyTypeObject *_Py_cobject_hack = &PyCObject_Type;
Guido van Rossum84a90321996-05-22 16:34:47 +00001007
1008
1009/* Hack to force loading of abstract.o */
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001010int (*_Py_abstract_hack) Py_FPROTO((PyObject *)) = &PyObject_Length;
Guido van Rossume09fb551997-08-05 02:04:34 +00001011
1012
Guido van Rossumb18618d2000-05-03 23:44:39 +00001013/* Python's malloc wrappers (see mymalloc.h) */
Guido van Rossume09fb551997-08-05 02:04:34 +00001014
1015ANY *
1016PyMem_Malloc(nbytes)
1017 size_t nbytes;
1018{
1019#if _PyMem_EXTRA > 0
1020 if (nbytes == 0)
1021 nbytes = _PyMem_EXTRA;
1022#endif
Guido van Rossumb18618d2000-05-03 23:44:39 +00001023 return PyMem_MALLOC(nbytes);
Guido van Rossume09fb551997-08-05 02:04:34 +00001024}
1025
1026ANY *
1027PyMem_Realloc(p, nbytes)
1028 ANY *p;
1029 size_t nbytes;
1030{
1031#if _PyMem_EXTRA > 0
1032 if (nbytes == 0)
1033 nbytes = _PyMem_EXTRA;
1034#endif
Guido van Rossumb18618d2000-05-03 23:44:39 +00001035 return PyMem_REALLOC(p, nbytes);
Guido van Rossume09fb551997-08-05 02:04:34 +00001036}
1037
1038void
1039PyMem_Free(p)
1040 ANY *p;
1041{
Guido van Rossumb18618d2000-05-03 23:44:39 +00001042 PyMem_FREE(p);
1043}
1044
1045
1046/* Python's object malloc wrappers (see objimpl.h) */
1047
1048ANY *
1049PyObject_Malloc(nbytes)
1050 size_t nbytes;
1051{
1052 return PyObject_MALLOC(nbytes);
1053}
1054
1055ANY *
1056PyObject_Realloc(p, nbytes)
1057 ANY *p;
1058 size_t nbytes;
1059{
1060 return PyObject_REALLOC(p, nbytes);
1061}
1062
1063void
1064PyObject_Free(p)
1065 ANY *p;
1066{
1067 PyObject_FREE(p);
Guido van Rossume09fb551997-08-05 02:04:34 +00001068}
Guido van Rossum86610361998-04-10 22:32:46 +00001069
1070
1071/* These methods are used to control infinite recursion in repr, str, print,
1072 etc. Container objects that may recursively contain themselves,
1073 e.g. builtin dictionaries and lists, should used Py_ReprEnter() and
1074 Py_ReprLeave() to avoid infinite recursion.
1075
1076 Py_ReprEnter() returns 0 the first time it is called for a particular
1077 object and 1 every time thereafter. It returns -1 if an exception
1078 occurred. Py_ReprLeave() has no return value.
1079
1080 See dictobject.c and listobject.c for examples of use.
1081*/
1082
1083#define KEY "Py_Repr"
1084
1085int
1086Py_ReprEnter(obj)
1087 PyObject *obj;
1088{
1089 PyObject *dict;
1090 PyObject *list;
1091 int i;
1092
1093 dict = PyThreadState_GetDict();
1094 if (dict == NULL)
1095 return -1;
1096 list = PyDict_GetItemString(dict, KEY);
1097 if (list == NULL) {
1098 list = PyList_New(0);
1099 if (list == NULL)
1100 return -1;
1101 if (PyDict_SetItemString(dict, KEY, list) < 0)
1102 return -1;
1103 Py_DECREF(list);
1104 }
1105 i = PyList_GET_SIZE(list);
1106 while (--i >= 0) {
1107 if (PyList_GET_ITEM(list, i) == obj)
1108 return 1;
1109 }
1110 PyList_Append(list, obj);
1111 return 0;
1112}
1113
1114void
1115Py_ReprLeave(obj)
1116 PyObject *obj;
1117{
1118 PyObject *dict;
1119 PyObject *list;
1120 int i;
1121
1122 dict = PyThreadState_GetDict();
Guido van Rossumeb909461998-04-11 15:17:34 +00001123 if (dict == NULL)
1124 return;
Guido van Rossum86610361998-04-10 22:32:46 +00001125 list = PyDict_GetItemString(dict, KEY);
Guido van Rossumeb909461998-04-11 15:17:34 +00001126 if (list == NULL || !PyList_Check(list))
1127 return;
Guido van Rossum86610361998-04-10 22:32:46 +00001128 i = PyList_GET_SIZE(list);
1129 /* Count backwards because we always expect obj to be list[-1] */
1130 while (--i >= 0) {
1131 if (PyList_GET_ITEM(list, i) == obj) {
1132 PyList_SetSlice(list, i, i + 1, NULL);
1133 break;
1134 }
1135 }
1136}
Guido van Rossumd724b232000-03-13 16:01:29 +00001137
1138/*
1139 trashcan
1140 CT 2k0130
1141 non-recursively destroy nested objects
1142
1143 CT 2k0223
1144 everything is now done in a macro.
1145
1146 CT 2k0305
1147 modified to use functions, after Tim Peter's suggestion.
1148
1149 CT 2k0309
1150 modified to restore a possible error.
Guido van Rossum13ff8eb2000-03-25 18:39:19 +00001151
1152 CT 2k0325
1153 added better safe than sorry check for threadstate
Guido van Rossume92e6102000-04-24 15:40:53 +00001154
1155 CT 2k0422
1156 complete rewrite. We now build a chain via ob_type
1157 and save the limited number of types in ob_refcnt.
1158 This is perfect since we don't need any memory.
1159 A patch for free-threading would need just a lock.
Guido van Rossumd724b232000-03-13 16:01:29 +00001160*/
1161
Guido van Rossume92e6102000-04-24 15:40:53 +00001162#define Py_TRASHCAN_TUPLE 1
1163#define Py_TRASHCAN_LIST 2
1164#define Py_TRASHCAN_DICT 3
1165#define Py_TRASHCAN_FRAME 4
1166#define Py_TRASHCAN_TRACEBACK 5
1167/* extend here if other objects want protection */
1168
Guido van Rossumd724b232000-03-13 16:01:29 +00001169int _PyTrash_delete_nesting = 0;
Guido van Rossume92e6102000-04-24 15:40:53 +00001170
Guido van Rossumd724b232000-03-13 16:01:29 +00001171PyObject * _PyTrash_delete_later = NULL;
1172
1173void
1174_PyTrash_deposit_object(op)
1175 PyObject *op;
1176{
Guido van Rossume92e6102000-04-24 15:40:53 +00001177 int typecode;
1178 PyObject *hold = _PyTrash_delete_later;
Guido van Rossum13ff8eb2000-03-25 18:39:19 +00001179
Guido van Rossume92e6102000-04-24 15:40:53 +00001180 if (PyTuple_Check(op))
1181 typecode = Py_TRASHCAN_TUPLE;
1182 else if (PyList_Check(op))
1183 typecode = Py_TRASHCAN_LIST;
1184 else if (PyDict_Check(op))
1185 typecode = Py_TRASHCAN_DICT;
1186 else if (PyFrame_Check(op))
1187 typecode = Py_TRASHCAN_FRAME;
1188 else if (PyTraceBack_Check(op))
1189 typecode = Py_TRASHCAN_TRACEBACK;
1190 op->ob_refcnt = typecode;
Guido van Rossumd724b232000-03-13 16:01:29 +00001191
Guido van Rossume92e6102000-04-24 15:40:53 +00001192 op->ob_type = (PyTypeObject*)_PyTrash_delete_later;
1193 _PyTrash_delete_later = op;
Guido van Rossumd724b232000-03-13 16:01:29 +00001194}
1195
1196void
Guido van Rossume92e6102000-04-24 15:40:53 +00001197_PyTrash_destroy_chain()
Guido van Rossumd724b232000-03-13 16:01:29 +00001198{
1199 while (_PyTrash_delete_later) {
1200 PyObject *shredder = _PyTrash_delete_later;
Guido van Rossume92e6102000-04-24 15:40:53 +00001201 _PyTrash_delete_later = (PyObject*) shredder->ob_type;
1202
1203 switch (shredder->ob_refcnt) {
1204 case Py_TRASHCAN_TUPLE:
1205 shredder->ob_type = &PyTuple_Type;
1206 break;
1207 case Py_TRASHCAN_LIST:
1208 shredder->ob_type = &PyList_Type;
1209 break;
1210 case Py_TRASHCAN_DICT:
1211 shredder->ob_type = &PyDict_Type;
1212 break;
1213 case Py_TRASHCAN_FRAME:
1214 shredder->ob_type = &PyFrame_Type;
1215 break;
1216 case Py_TRASHCAN_TRACEBACK:
1217 shredder->ob_type = &PyTraceBack_Type;
1218 break;
1219 }
1220 _Py_NewReference(shredder);
1221
Guido van Rossumd724b232000-03-13 16:01:29 +00001222 ++_PyTrash_delete_nesting;
1223 Py_DECREF(shredder);
1224 --_PyTrash_delete_nesting;
1225 }
1226}
Guido van Rossume92e6102000-04-24 15:40:53 +00001227