blob: 9ed03b2fbe9b37a3384ab7687beae2914fb3dadb [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 }
127 /* Any changes should be reflected in PyObject_INIT (objimpl.h) */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000128 op->ob_type = tp;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000129 _Py_NewReference(op);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000130 return op;
131}
132
Guido van Rossumb18618d2000-05-03 23:44:39 +0000133PyVarObject *
134PyObject_InitVar(op, tp, size)
135 PyVarObject *op;
136 PyTypeObject *tp;
137 int size;
138{
139 if (op == NULL) {
140 PyErr_SetString(PyExc_SystemError,
141 "NULL object passed to PyObject_InitVar");
142 return op;
143 }
144 /* Any changes should be reflected in PyObject_INIT_VAR */
145 op->ob_size = size;
146 op->ob_type = tp;
147 _Py_NewReference((PyObject *)op);
148 return op;
149}
150
151PyObject *
152_PyObject_New(tp)
153 PyTypeObject *tp;
154{
155 PyObject *op;
156 op = (PyObject *) PyObject_MALLOC(_PyObject_SIZE(tp));
157 if (op == NULL)
158 return PyErr_NoMemory();
159 return PyObject_INIT(op, tp);
160}
161
Guido van Rossumd0c87ee1997-05-15 21:31:03 +0000162PyVarObject *
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000163_PyObject_NewVar(tp, size)
164 PyTypeObject *tp;
Guido van Rossum2497ead1995-02-10 17:00:27 +0000165 int size;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000166{
Guido van Rossumb18618d2000-05-03 23:44:39 +0000167 PyVarObject *op;
168 op = (PyVarObject *) PyObject_MALLOC(_PyObject_VAR_SIZE(tp, size));
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000169 if (op == NULL)
Guido van Rossumd0c87ee1997-05-15 21:31:03 +0000170 return (PyVarObject *)PyErr_NoMemory();
Guido van Rossumb18618d2000-05-03 23:44:39 +0000171 return PyObject_INIT_VAR(op, tp, size);
172}
173
174void
175_PyObject_Del(op)
176 PyObject *op;
177{
178 PyObject_FREE(op);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000179}
180
Guido van Rossum90933611991-06-07 16:10:43 +0000181int
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000182PyObject_Print(op, fp, flags)
183 PyObject *op;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000184 FILE *fp;
185 int flags;
186{
Guido van Rossum278ef591991-07-27 21:40:24 +0000187 int ret = 0;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000188 if (PyErr_CheckSignals())
Guido van Rossum90933611991-06-07 16:10:43 +0000189 return -1;
Guido van Rossum9b00dfa1998-04-28 16:06:54 +0000190#ifdef USE_STACKCHECK
191 if (PyOS_CheckStack()) {
192 PyErr_SetString(PyExc_MemoryError, "Stack overflow");
193 return -1;
194 }
195#endif
Guido van Rossum687ef6e2000-01-12 16:28:58 +0000196 clearerr(fp); /* Clear any previous error condition */
Guido van Rossum90933611991-06-07 16:10:43 +0000197 if (op == NULL) {
198 fprintf(fp, "<nil>");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000199 }
Guido van Rossum90933611991-06-07 16:10:43 +0000200 else {
201 if (op->ob_refcnt <= 0)
Guido van Rossum2e8f6141992-09-03 20:32:55 +0000202 fprintf(fp, "<refcnt %u at %lx>",
203 op->ob_refcnt, (long)op);
204 else if (op->ob_type->tp_print == NULL) {
205 if (op->ob_type->tp_repr == NULL) {
206 fprintf(fp, "<%s object at %lx>",
207 op->ob_type->tp_name, (long)op);
208 }
209 else {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000210 PyObject *s;
211 if (flags & Py_PRINT_RAW)
212 s = PyObject_Str(op);
Guido van Rossumc6004111993-11-05 10:22:19 +0000213 else
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000214 s = PyObject_Repr(op);
Guido van Rossum2e8f6141992-09-03 20:32:55 +0000215 if (s == NULL)
216 ret = -1;
Guido van Rossum2e8f6141992-09-03 20:32:55 +0000217 else {
Guido van Rossum565798d1998-04-21 22:25:01 +0000218 ret = PyObject_Print(s, fp,
219 Py_PRINT_RAW);
Guido van Rossum2e8f6141992-09-03 20:32:55 +0000220 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000221 Py_XDECREF(s);
Guido van Rossum2e8f6141992-09-03 20:32:55 +0000222 }
223 }
Guido van Rossum90933611991-06-07 16:10:43 +0000224 else
Guido van Rossum278ef591991-07-27 21:40:24 +0000225 ret = (*op->ob_type->tp_print)(op, fp, flags);
Guido van Rossum90933611991-06-07 16:10:43 +0000226 }
Guido van Rossum278ef591991-07-27 21:40:24 +0000227 if (ret == 0) {
228 if (ferror(fp)) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000229 PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossum278ef591991-07-27 21:40:24 +0000230 clearerr(fp);
231 ret = -1;
232 }
233 }
234 return ret;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000235}
236
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000237PyObject *
238PyObject_Repr(v)
239 PyObject *v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000240{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000241 if (PyErr_CheckSignals())
Guido van Rossum90933611991-06-07 16:10:43 +0000242 return NULL;
Guido van Rossum9b00dfa1998-04-28 16:06:54 +0000243#ifdef USE_STACKCHECK
244 if (PyOS_CheckStack()) {
245 PyErr_SetString(PyExc_MemoryError, "Stack overflow");
246 return NULL;
247 }
248#endif
Guido van Rossum90933611991-06-07 16:10:43 +0000249 if (v == NULL)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000250 return PyString_FromString("<NULL>");
Guido van Rossum90933611991-06-07 16:10:43 +0000251 else if (v->ob_type->tp_repr == NULL) {
252 char buf[120];
253 sprintf(buf, "<%.80s object at %lx>",
254 v->ob_type->tp_name, (long)v);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000255 return PyString_FromString(buf);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000256 }
Guido van Rossum4c08d552000-03-10 22:55:18 +0000257 else {
258 PyObject *res;
259 res = (*v->ob_type->tp_repr)(v);
260 if (res == NULL)
261 return NULL;
262 if (!PyString_Check(res)) {
263 PyErr_Format(PyExc_TypeError,
Guido van Rossum5db862d2000-04-10 12:46:51 +0000264 "__repr__ returned non-string (type %.200s)",
Guido van Rossum4c08d552000-03-10 22:55:18 +0000265 res->ob_type->tp_name);
266 Py_DECREF(res);
267 return NULL;
268 }
269 return res;
270 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000271}
272
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000273PyObject *
274PyObject_Str(v)
275 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;
299 if (!PyString_Check(res)) {
300 PyErr_Format(PyExc_TypeError,
Guido van Rossum5db862d2000-04-10 12:46:51 +0000301 "__str__ returned non-string (type %.200s)",
Guido van Rossum4c08d552000-03-10 22:55:18 +0000302 res->ob_type->tp_name);
303 Py_DECREF(res);
304 return NULL;
305 }
306 return res;
Guido van Rossumc6004111993-11-05 10:22:19 +0000307}
308
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000309static PyObject *
Guido van Rossum20566841995-01-12 11:26:10 +0000310do_cmp(v, w)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000311 PyObject *v, *w;
Guido van Rossum20566841995-01-12 11:26:10 +0000312{
Guido van Rossumc8b6df91997-05-23 00:06:51 +0000313 long c;
Guido van Rossum20566841995-01-12 11:26:10 +0000314 /* __rcmp__ actually won't be called unless __cmp__ isn't defined,
315 because the check in cmpobject() reverses the objects first.
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000316 This is intentional -- it makes no sense to define cmp(x,y)
317 different than -cmp(y,x). */
318 if (PyInstance_Check(v) || PyInstance_Check(w))
319 return PyInstance_DoBinOp(v, w, "__cmp__", "__rcmp__", do_cmp);
Guido van Rossumc8b6df91997-05-23 00:06:51 +0000320 c = PyObject_Compare(v, w);
321 if (c && PyErr_Occurred())
322 return NULL;
323 return PyInt_FromLong(c);
Guido van Rossum20566841995-01-12 11:26:10 +0000324}
325
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000326PyObject *_PyCompareState_Key;
327
328/* _PyCompareState_nesting is incremented beforing call compare (for
329 some types) and decremented on exit. If the count exceeds the
330 nesting limit, enable code to detect circular data structures.
331*/
332#define NESTING_LIMIT 500
333int _PyCompareState_nesting = 0;
334
335static PyObject*
336get_inprogress_dict()
337{
338 PyObject *tstate_dict, *inprogress;
339
340 tstate_dict = PyThreadState_GetDict();
341 if (tstate_dict == NULL) {
342 PyErr_BadInternalCall();
343 return NULL;
344 }
345 inprogress = PyDict_GetItem(tstate_dict, _PyCompareState_Key);
346 if (inprogress == NULL) {
347 PyErr_Clear();
348 inprogress = PyDict_New();
349 if (inprogress == NULL)
350 return NULL;
351 if (PyDict_SetItem(tstate_dict, _PyCompareState_Key,
352 inprogress) == -1) {
353 Py_DECREF(inprogress);
354 return NULL;
355 }
Jeremy Hyltona251ea02000-06-09 16:20:39 +0000356 Py_DECREF(inprogress);
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000357 }
358 return inprogress;
359}
360
361static PyObject *
362make_pair(v, w)
363 PyObject *v, *w;
364{
365 PyObject *pair;
366
367 pair = PyTuple_New(2);
368 if (pair == NULL) {
369 return NULL;
370 }
Guido van Rossumad89bbc2000-06-28 21:57:18 +0000371 if (v <= w) {
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000372 PyTuple_SET_ITEM(pair, 0, PyLong_FromVoidPtr((void *)v));
373 PyTuple_SET_ITEM(pair, 1, PyLong_FromVoidPtr((void *)w));
374 } else {
375 PyTuple_SET_ITEM(pair, 0, PyLong_FromVoidPtr((void *)w));
376 PyTuple_SET_ITEM(pair, 1, PyLong_FromVoidPtr((void *)v));
377 }
378 return pair;
379}
380
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000381int
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000382PyObject_Compare(v, w)
383 PyObject *v, *w;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000384{
Guido van Rossumcd5a5f61998-06-09 18:58:44 +0000385 PyTypeObject *vtp, *wtp;
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000386 int result;
387
Guido van Rossumc8b6df91997-05-23 00:06:51 +0000388 if (v == NULL || w == NULL) {
389 PyErr_BadInternalCall();
390 return -1;
391 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000392 if (v == w)
393 return 0;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000394 if (PyInstance_Check(v) || PyInstance_Check(w)) {
395 PyObject *res;
Guido van Rossum20566841995-01-12 11:26:10 +0000396 int c;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000397 if (!PyInstance_Check(v))
398 return -PyObject_Compare(w, v);
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000399 if (++_PyCompareState_nesting > NESTING_LIMIT) {
400 PyObject *inprogress, *pair;
401
402 inprogress = get_inprogress_dict();
403 if (inprogress == NULL) {
404 return -1;
405 }
406 pair = make_pair(v, w);
407 if (PyDict_GetItem(inprogress, pair)) {
408 /* already comparing these objects. assume
409 they're equal until shown otherwise */
410 Py_DECREF(pair);
411 --_PyCompareState_nesting;
412 return 0;
413 }
414 if (PyDict_SetItem(inprogress, pair, pair) == -1) {
415 return -1;
416 }
417 res = do_cmp(v, w);
418 _PyCompareState_nesting--;
419 /* XXX DelItem shouldn't fail */
420 PyDict_DelItem(inprogress, pair);
421 Py_DECREF(pair);
422 } else {
423 res = do_cmp(v, w);
424 }
Guido van Rossumc8b6df91997-05-23 00:06:51 +0000425 if (res == NULL)
426 return -1;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000427 if (!PyInt_Check(res)) {
428 Py_DECREF(res);
Guido van Rossumc8b6df91997-05-23 00:06:51 +0000429 PyErr_SetString(PyExc_TypeError,
430 "comparison did not return an int");
431 return -1;
Guido van Rossum20566841995-01-12 11:26:10 +0000432 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000433 c = PyInt_AsLong(res);
434 Py_DECREF(res);
Guido van Rossum20566841995-01-12 11:26:10 +0000435 return (c < 0) ? -1 : (c > 0) ? 1 : 0;
436 }
Guido van Rossumcd5a5f61998-06-09 18:58:44 +0000437 if ((vtp = v->ob_type) != (wtp = w->ob_type)) {
438 char *vname = vtp->tp_name;
439 char *wname = wtp->tp_name;
Guido van Rossumb4db1941998-07-21 21:56:41 +0000440 if (vtp->tp_as_number != NULL && wtp->tp_as_number != NULL) {
Guido van Rossum242c6421997-11-19 16:03:17 +0000441 int err;
442 err = PyNumber_CoerceEx(&v, &w);
443 if (err < 0)
Guido van Rossumc8b6df91997-05-23 00:06:51 +0000444 return -1;
Guido van Rossum242c6421997-11-19 16:03:17 +0000445 else if (err == 0) {
Guido van Rossumb4db1941998-07-21 21:56:41 +0000446 int cmp;
447 vtp = v->ob_type;
448 if (vtp->tp_compare == NULL)
449 cmp = (v < w) ? -1 : 1;
450 else
451 cmp = (*vtp->tp_compare)(v, w);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000452 Py_DECREF(v);
453 Py_DECREF(w);
Guido van Rossum9fb03681991-07-01 18:48:04 +0000454 return cmp;
455 }
456 }
Guido van Rossumb244f692000-04-10 13:42:33 +0000457 else if (PyUnicode_Check(v) || PyUnicode_Check(w)) {
458 int result = PyUnicode_Compare(v, w);
459 if (result == -1 && PyErr_Occurred() &&
460 PyErr_ExceptionMatches(PyExc_TypeError))
461 /* TypeErrors are ignored: if Unicode coercion
462 fails due to one of the arguments not
463 having the right type, we continue as
464 defined by the coercion protocol (see
465 above). Luckily, decoding errors are
466 reported as ValueErrors and are not masked
467 by this technique. */
468 PyErr_Clear();
469 else
470 return result;
471 }
Guido van Rossumcd5a5f61998-06-09 18:58:44 +0000472 else if (vtp->tp_as_number != NULL)
473 vname = "";
474 else if (wtp->tp_as_number != NULL)
475 wname = "";
476 /* Numerical types compare smaller than all other types */
477 return strcmp(vname, wname);
Guido van Rossum9fb03681991-07-01 18:48:04 +0000478 }
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000479 if (vtp->tp_compare == NULL) {
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000480 return (v < w) ? -1 : 1;
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000481 }
482 if (++_PyCompareState_nesting > NESTING_LIMIT
483 && (vtp->tp_as_mapping
484 || (vtp->tp_as_sequence && !PyString_Check(v)))) {
485 PyObject *inprogress, *pair;
486
487 inprogress = get_inprogress_dict();
488 if (inprogress == NULL) {
489 return -1;
490 }
491 pair = make_pair(v, w);
492 if (PyDict_GetItem(inprogress, pair)) {
493 /* already comparing these objects. assume
494 they're equal until shown otherwise */
495 _PyCompareState_nesting--;
496 Py_DECREF(pair);
497 return 0;
498 }
499 if (PyDict_SetItem(inprogress, pair, pair) == -1) {
500 return -1;
501 }
502 result = (*vtp->tp_compare)(v, w);
503 _PyCompareState_nesting--;
504 PyDict_DelItem(inprogress, pair); /* XXX shouldn't fail */
505 Py_DECREF(pair);
506 } else {
507 result = (*vtp->tp_compare)(v, w);
508 }
509 return result;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000510}
511
Fred Drake13634cf2000-06-29 19:17:04 +0000512
513/* Set of hash utility functions to help maintaining the invariant that
514 iff a==b then hash(a)==hash(b)
515
516 All the utility functions (_Py_Hash*()) return "-1" to signify an error.
517*/
518
519long
520_Py_HashDouble(v)
521 double v;
522{
523 /* Use frexp to get at the bits in the double.
524 * Since the VAX D double format has 56 mantissa bits, which is the
525 * most of any double format in use, each of these parts may have as
526 * many as (but no more than) 56 significant bits.
527 * So, assuming sizeof(long) >= 4, each part can be broken into two longs;
528 * frexp and multiplication are used to do that.
529 * Also, since the Cray double format has 15 exponent bits, which is the
530 * most of any double format in use, shifting the exponent field left by
531 * 15 won't overflow a long (again assuming sizeof(long) >= 4).
532 */
533 int expo;
534 long hipart;
535
536 v = frexp(v, &expo);
537 v = v * 2147483648.0; /* 2**31 */
538 hipart = (long)v; /* Take the top 32 bits */
539 v = (v - (double)hipart) * 2147483648.0; /* Get the next 32 bits */
540
541 return hipart + (long)v + (expo << 15); /* Combine everything */
542}
543
544long
545_Py_HashPointer(p)
546 void *p;
547{
548#if SIZEOF_LONG >= SIZEOF_VOID_P
549 return (long)p;
550#else
551 /* convert to a Python long and hash that */
552 PyObject* longobj;
553 long x;
554
555 if ((longobj = PyLong_FromVoidPtr(p)) == NULL) {
556 x = -1;
557 goto finally;
558 }
559 x = PyObject_Hash(longobj);
560
561finally:
562 Py_XDECREF(longobj);
563 return x;
564#endif
565}
566
567
Guido van Rossum9bfef441993-03-29 10:43:31 +0000568long
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000569PyObject_Hash(v)
570 PyObject *v;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000571{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000572 PyTypeObject *tp = v->ob_type;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000573 if (tp->tp_hash != NULL)
574 return (*tp->tp_hash)(v);
Fred Drake13634cf2000-06-29 19:17:04 +0000575 if (tp->tp_compare == NULL) {
576 return _Py_HashPointer(v); /* Use address as hash value */
577 }
Guido van Rossum9bfef441993-03-29 10:43:31 +0000578 /* If there's a cmp but no hash defined, the object can't be hashed */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000579 PyErr_SetString(PyExc_TypeError, "unhashable type");
Guido van Rossum9bfef441993-03-29 10:43:31 +0000580 return -1;
581}
582
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000583PyObject *
584PyObject_GetAttrString(v, name)
585 PyObject *v;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000586 char *name;
587{
Guido van Rossumd8eb1b31996-08-09 20:52:03 +0000588 if (v->ob_type->tp_getattro != NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000589 PyObject *w, *res;
Guido van Rossumc6d06701997-01-18 07:57:16 +0000590 w = PyString_InternFromString(name);
Guido van Rossumd8eb1b31996-08-09 20:52:03 +0000591 if (w == NULL)
592 return NULL;
593 res = (*v->ob_type->tp_getattro)(v, w);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000594 Py_XDECREF(w);
Guido van Rossumd8eb1b31996-08-09 20:52:03 +0000595 return res;
596 }
597
Guido van Rossum3f5da241990-12-20 15:06:42 +0000598 if (v->ob_type->tp_getattr == NULL) {
Guido van Rossumdb935161998-01-19 22:16:36 +0000599 PyErr_Format(PyExc_AttributeError,
600 "'%.50s' object has no attribute '%.400s'",
601 v->ob_type->tp_name,
602 name);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000603 return NULL;
604 }
605 else {
606 return (*v->ob_type->tp_getattr)(v, name);
607 }
608}
609
610int
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000611PyObject_HasAttrString(v, name)
612 PyObject *v;
Guido van Rossumed18fdc1993-07-11 19:55:34 +0000613 char *name;
614{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000615 PyObject *res = PyObject_GetAttrString(v, name);
Guido van Rossumed18fdc1993-07-11 19:55:34 +0000616 if (res != NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000617 Py_DECREF(res);
Guido van Rossumed18fdc1993-07-11 19:55:34 +0000618 return 1;
619 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000620 PyErr_Clear();
Guido van Rossumed18fdc1993-07-11 19:55:34 +0000621 return 0;
622}
623
624int
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000625PyObject_SetAttrString(v, name, w)
626 PyObject *v;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000627 char *name;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000628 PyObject *w;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000629{
Guido van Rossumd8eb1b31996-08-09 20:52:03 +0000630 if (v->ob_type->tp_setattro != NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000631 PyObject *s;
Guido van Rossumd8eb1b31996-08-09 20:52:03 +0000632 int res;
Guido van Rossumc6d06701997-01-18 07:57:16 +0000633 s = PyString_InternFromString(name);
Guido van Rossumd8eb1b31996-08-09 20:52:03 +0000634 if (s == NULL)
Guido van Rossumb7fc3041996-09-11 22:51:25 +0000635 return -1;
Guido van Rossumd8eb1b31996-08-09 20:52:03 +0000636 res = (*v->ob_type->tp_setattro)(v, s, w);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000637 Py_XDECREF(s);
Guido van Rossumd8eb1b31996-08-09 20:52:03 +0000638 return res;
639 }
640
Guido van Rossum3f5da241990-12-20 15:06:42 +0000641 if (v->ob_type->tp_setattr == NULL) {
642 if (v->ob_type->tp_getattr == NULL)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000643 PyErr_SetString(PyExc_TypeError,
Guido van Rossum3ea74121991-12-24 13:28:03 +0000644 "attribute-less object (assign or del)");
Guido van Rossum3f5da241990-12-20 15:06:42 +0000645 else
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000646 PyErr_SetString(PyExc_TypeError,
Guido van Rossum3ea74121991-12-24 13:28:03 +0000647 "object has read-only attributes");
Guido van Rossum73531a31990-12-20 23:12:40 +0000648 return -1;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000649 }
650 else {
651 return (*v->ob_type->tp_setattr)(v, name, w);
652 }
653}
654
Guido van Rossum98ff96a1997-05-20 18:34:44 +0000655PyObject *
656PyObject_GetAttr(v, name)
657 PyObject *v;
658 PyObject *name;
659{
660 if (v->ob_type->tp_getattro != NULL)
661 return (*v->ob_type->tp_getattro)(v, name);
Jeremy Hylton99a8f902000-06-23 14:36:32 +0000662
663 if (!PyString_Check(name)) {
664 PyErr_SetString(PyExc_TypeError,
665 "attribute name must be string");
666 return NULL;
667 }
668 return PyObject_GetAttrString(v, PyString_AS_STRING(name));
Guido van Rossum98ff96a1997-05-20 18:34:44 +0000669}
670
671int
672PyObject_HasAttr(v, name)
673 PyObject *v;
674 PyObject *name;
675{
676 PyObject *res = PyObject_GetAttr(v, name);
677 if (res != NULL) {
678 Py_DECREF(res);
679 return 1;
680 }
681 PyErr_Clear();
682 return 0;
683}
684
685int
686PyObject_SetAttr(v, name, value)
687 PyObject *v;
688 PyObject *name;
689 PyObject *value;
690{
691 int err;
692 Py_INCREF(name);
Jeremy Hylton99a8f902000-06-23 14:36:32 +0000693 if (PyString_Check(name))
694 PyString_InternInPlace(&name);
Guido van Rossum98ff96a1997-05-20 18:34:44 +0000695 if (v->ob_type->tp_setattro != NULL)
696 err = (*v->ob_type->tp_setattro)(v, name, value);
Jeremy Hylton99a8f902000-06-23 14:36:32 +0000697 else if (PyString_Check(name)) {
Guido van Rossum98ff96a1997-05-20 18:34:44 +0000698 err = PyObject_SetAttrString(
Jeremy Hylton99a8f902000-06-23 14:36:32 +0000699 v, PyString_AS_STRING(name), value);
700 }
701 else {
702 PyErr_SetString(PyExc_TypeError,
703 "attribute name must be string");
704 err = -1;
705 }
Guido van Rossum98ff96a1997-05-20 18:34:44 +0000706 Py_DECREF(name);
707 return err;
708}
709
Guido van Rossum6ac258d1993-05-12 08:24:20 +0000710/* Test a value used as condition, e.g., in a for or if statement.
711 Return -1 if an error occurred */
712
713int
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000714PyObject_IsTrue(v)
715 PyObject *v;
Guido van Rossum6ac258d1993-05-12 08:24:20 +0000716{
717 int res;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000718 if (v == Py_None)
Guido van Rossum6ac258d1993-05-12 08:24:20 +0000719 res = 0;
Guido van Rossum1c4f4581998-05-22 00:53:24 +0000720 else if (v->ob_type->tp_as_number != NULL &&
721 v->ob_type->tp_as_number->nb_nonzero != NULL)
Guido van Rossum6ac258d1993-05-12 08:24:20 +0000722 res = (*v->ob_type->tp_as_number->nb_nonzero)(v);
Guido van Rossum1c4f4581998-05-22 00:53:24 +0000723 else if (v->ob_type->tp_as_mapping != NULL &&
724 v->ob_type->tp_as_mapping->mp_length != NULL)
Guido van Rossum6ac258d1993-05-12 08:24:20 +0000725 res = (*v->ob_type->tp_as_mapping->mp_length)(v);
Guido van Rossum1c4f4581998-05-22 00:53:24 +0000726 else if (v->ob_type->tp_as_sequence != NULL &&
727 v->ob_type->tp_as_sequence->sq_length != NULL)
Guido van Rossum6ac258d1993-05-12 08:24:20 +0000728 res = (*v->ob_type->tp_as_sequence->sq_length)(v);
729 else
730 res = 1;
731 if (res > 0)
732 res = 1;
733 return res;
734}
735
Guido van Rossumc3d3f961998-04-09 17:53:59 +0000736/* equivalent of 'not v'
737 Return -1 if an error occurred */
738
739int
740PyObject_Not(v)
741 PyObject *v;
742{
743 int res;
744 res = PyObject_IsTrue(v);
745 if (res < 0)
746 return res;
747 return res == 0;
748}
749
Guido van Rossum5524a591995-01-10 15:26:20 +0000750/* Coerce two numeric types to the "larger" one.
751 Increment the reference count on each argument.
752 Return -1 and raise an exception if no coercion is possible
753 (and then no reference count is incremented).
754*/
755
756int
Guido van Rossum242c6421997-11-19 16:03:17 +0000757PyNumber_CoerceEx(pv, pw)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000758 PyObject **pv, **pw;
Guido van Rossum5524a591995-01-10 15:26:20 +0000759{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000760 register PyObject *v = *pv;
761 register PyObject *w = *pw;
Guido van Rossum5524a591995-01-10 15:26:20 +0000762 int res;
763
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000764 if (v->ob_type == w->ob_type && !PyInstance_Check(v)) {
765 Py_INCREF(v);
766 Py_INCREF(w);
Guido van Rossum5524a591995-01-10 15:26:20 +0000767 return 0;
768 }
769 if (v->ob_type->tp_as_number && v->ob_type->tp_as_number->nb_coerce) {
770 res = (*v->ob_type->tp_as_number->nb_coerce)(pv, pw);
771 if (res <= 0)
772 return res;
773 }
774 if (w->ob_type->tp_as_number && w->ob_type->tp_as_number->nb_coerce) {
775 res = (*w->ob_type->tp_as_number->nb_coerce)(pw, pv);
776 if (res <= 0)
777 return res;
778 }
Guido van Rossum242c6421997-11-19 16:03:17 +0000779 return 1;
780}
781
782int
783PyNumber_Coerce(pv, pw)
784 PyObject **pv, **pw;
785{
786 int err = PyNumber_CoerceEx(pv, pw);
787 if (err <= 0)
788 return err;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000789 PyErr_SetString(PyExc_TypeError, "number coercion failed");
Guido van Rossum5524a591995-01-10 15:26:20 +0000790 return -1;
791}
792
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000793
Guido van Rossum49b11fe1995-01-26 00:38:22 +0000794/* Test whether an object can be called */
795
796int
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000797PyCallable_Check(x)
798 PyObject *x;
Guido van Rossum49b11fe1995-01-26 00:38:22 +0000799{
800 if (x == NULL)
801 return 0;
802 if (x->ob_type->tp_call != NULL ||
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000803 PyFunction_Check(x) ||
804 PyMethod_Check(x) ||
805 PyCFunction_Check(x) ||
806 PyClass_Check(x))
Guido van Rossum49b11fe1995-01-26 00:38:22 +0000807 return 1;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000808 if (PyInstance_Check(x)) {
809 PyObject *call = PyObject_GetAttrString(x, "__call__");
Guido van Rossum49b11fe1995-01-26 00:38:22 +0000810 if (call == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000811 PyErr_Clear();
Guido van Rossum49b11fe1995-01-26 00:38:22 +0000812 return 0;
813 }
814 /* Could test recursively but don't, for fear of endless
815 recursion if some joker sets self.__call__ = self */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000816 Py_DECREF(call);
Guido van Rossum49b11fe1995-01-26 00:38:22 +0000817 return 1;
818 }
819 return 0;
820}
821
822
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000823/*
824NoObject is usable as a non-NULL undefined value, used by the macro None.
825There is (and should be!) no way to create other objects of this type,
Guido van Rossum3f5da241990-12-20 15:06:42 +0000826so there is exactly one (which is indestructible, by the way).
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000827*/
828
Guido van Rossum0c182a11992-03-27 17:26:13 +0000829/* ARGSUSED */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000830static PyObject *
Guido van Rossum3f5da241990-12-20 15:06:42 +0000831none_repr(op)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000832 PyObject *op;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000833{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000834 return PyString_FromString("None");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000835}
836
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000837static PyTypeObject PyNothing_Type = {
838 PyObject_HEAD_INIT(&PyType_Type)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000839 0,
Guido van Rossum3f5da241990-12-20 15:06:42 +0000840 "None",
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000841 0,
842 0,
843 0, /*tp_dealloc*/ /*never called*/
Guido van Rossum7066dd71992-09-17 17:54:56 +0000844 0, /*tp_print*/
Guido van Rossum3f5da241990-12-20 15:06:42 +0000845 0, /*tp_getattr*/
846 0, /*tp_setattr*/
847 0, /*tp_compare*/
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000848 (reprfunc)none_repr, /*tp_repr*/
Guido van Rossum3f5da241990-12-20 15:06:42 +0000849 0, /*tp_as_number*/
850 0, /*tp_as_sequence*/
851 0, /*tp_as_mapping*/
Guido van Rossum9bfef441993-03-29 10:43:31 +0000852 0, /*tp_hash */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000853};
854
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000855PyObject _Py_NoneStruct = {
856 PyObject_HEAD_INIT(&PyNothing_Type)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000857};
858
859
Guido van Rossum84a90321996-05-22 16:34:47 +0000860#ifdef Py_TRACE_REFS
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000861
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000862static PyObject refchain = {&refchain, &refchain};
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000863
Guido van Rossumaacdc9d1996-08-12 21:32:12 +0000864void
Guido van Rossume09fb551997-08-05 02:04:34 +0000865_Py_ResetReferences()
866{
867 refchain._ob_prev = refchain._ob_next = &refchain;
868 _Py_RefTotal = 0;
869}
870
871void
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000872_Py_NewReference(op)
873 PyObject *op;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000874{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000875 _Py_RefTotal++;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000876 op->ob_refcnt = 1;
877 op->_ob_next = refchain._ob_next;
878 op->_ob_prev = &refchain;
879 refchain._ob_next->_ob_prev = op;
880 refchain._ob_next = op;
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +0000881#ifdef COUNT_ALLOCS
882 inc_count(op->ob_type);
883#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000884}
885
Guido van Rossumaacdc9d1996-08-12 21:32:12 +0000886void
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000887_Py_ForgetReference(op)
888 register PyObject *op;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000889{
Guido van Rossumbffd6832000-01-20 22:32:56 +0000890#ifdef SLOW_UNREF_CHECK
Guido van Rossum4c08d552000-03-10 22:55:18 +0000891 register PyObject *p;
Guido van Rossumbffd6832000-01-20 22:32:56 +0000892#endif
Guido van Rossumd7047b31995-01-02 19:07:15 +0000893 if (op->ob_refcnt < 0)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000894 Py_FatalError("UNREF negative refcnt");
Guido van Rossum2e8f6141992-09-03 20:32:55 +0000895 if (op == &refchain ||
Guido van Rossumd7047b31995-01-02 19:07:15 +0000896 op->_ob_prev->_ob_next != op || op->_ob_next->_ob_prev != op)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000897 Py_FatalError("UNREF invalid object");
Guido van Rossum2e8f6141992-09-03 20:32:55 +0000898#ifdef SLOW_UNREF_CHECK
Guido van Rossum3f5da241990-12-20 15:06:42 +0000899 for (p = refchain._ob_next; p != &refchain; p = p->_ob_next) {
900 if (p == op)
901 break;
902 }
Guido van Rossumd7047b31995-01-02 19:07:15 +0000903 if (p == &refchain) /* Not found */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000904 Py_FatalError("UNREF unknown object");
Guido van Rossum2e8f6141992-09-03 20:32:55 +0000905#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000906 op->_ob_next->_ob_prev = op->_ob_prev;
907 op->_ob_prev->_ob_next = op->_ob_next;
Guido van Rossum2e8f6141992-09-03 20:32:55 +0000908 op->_ob_next = op->_ob_prev = NULL;
Guido van Rossumd8953cb1995-04-06 14:46:26 +0000909#ifdef COUNT_ALLOCS
910 op->ob_type->tp_free++;
911#endif
Guido van Rossum3f5da241990-12-20 15:06:42 +0000912}
913
Guido van Rossumaacdc9d1996-08-12 21:32:12 +0000914void
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000915_Py_Dealloc(op)
916 PyObject *op;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000917{
Guido van Rossum9776adf1994-09-07 14:36:45 +0000918 destructor dealloc = op->ob_type->tp_dealloc;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000919 _Py_ForgetReference(op);
Guido van Rossume92e6102000-04-24 15:40:53 +0000920 if (_PyTrash_delete_nesting < PyTrash_UNWIND_LEVEL-1)
921 op->ob_type = NULL;
Guido van Rossum9776adf1994-09-07 14:36:45 +0000922 (*dealloc)(op);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000923}
924
Guido van Rossumaacdc9d1996-08-12 21:32:12 +0000925void
Guido van Rossumded690f1996-05-24 20:48:31 +0000926_Py_PrintReferences(fp)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000927 FILE *fp;
928{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000929 PyObject *op;
Guido van Rossume09fb551997-08-05 02:04:34 +0000930 fprintf(fp, "Remaining objects:\n");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000931 for (op = refchain._ob_next; op != &refchain; op = op->_ob_next) {
932 fprintf(fp, "[%d] ", op->ob_refcnt);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000933 if (PyObject_Print(op, fp, 0) != 0)
934 PyErr_Clear();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000935 putc('\n', fp);
936 }
937}
938
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000939PyObject *
Guido van Rossumded690f1996-05-24 20:48:31 +0000940_Py_GetObjects(self, args)
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000941 PyObject *self;
942 PyObject *args;
943{
944 int i, n;
945 PyObject *t = NULL;
946 PyObject *res, *op;
947
948 if (!PyArg_ParseTuple(args, "i|O", &n, &t))
949 return NULL;
950 op = refchain._ob_next;
951 res = PyList_New(0);
952 if (res == NULL)
953 return NULL;
954 for (i = 0; (n == 0 || i < n) && op != &refchain; i++) {
955 while (op == self || op == args || op == res || op == t ||
956 t != NULL && op->ob_type != (PyTypeObject *) t) {
957 op = op->_ob_next;
958 if (op == &refchain)
959 return res;
960 }
961 if (PyList_Append(res, op) < 0) {
962 Py_DECREF(res);
963 return NULL;
964 }
965 op = op->_ob_next;
966 }
967 return res;
968}
969
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000970#endif
Guido van Rossum97ead3f1996-01-12 01:24:09 +0000971
972
973/* Hack to force loading of cobject.o */
Guido van Rossumda9c2711996-12-05 21:58:58 +0000974PyTypeObject *_Py_cobject_hack = &PyCObject_Type;
Guido van Rossum84a90321996-05-22 16:34:47 +0000975
976
977/* Hack to force loading of abstract.o */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000978int (*_Py_abstract_hack) Py_FPROTO((PyObject *)) = &PyObject_Length;
Guido van Rossume09fb551997-08-05 02:04:34 +0000979
980
Guido van Rossumb18618d2000-05-03 23:44:39 +0000981/* Python's malloc wrappers (see mymalloc.h) */
Guido van Rossume09fb551997-08-05 02:04:34 +0000982
983ANY *
984PyMem_Malloc(nbytes)
985 size_t nbytes;
986{
987#if _PyMem_EXTRA > 0
988 if (nbytes == 0)
989 nbytes = _PyMem_EXTRA;
990#endif
Guido van Rossumb18618d2000-05-03 23:44:39 +0000991 return PyMem_MALLOC(nbytes);
Guido van Rossume09fb551997-08-05 02:04:34 +0000992}
993
994ANY *
995PyMem_Realloc(p, nbytes)
996 ANY *p;
997 size_t nbytes;
998{
999#if _PyMem_EXTRA > 0
1000 if (nbytes == 0)
1001 nbytes = _PyMem_EXTRA;
1002#endif
Guido van Rossumb18618d2000-05-03 23:44:39 +00001003 return PyMem_REALLOC(p, nbytes);
Guido van Rossume09fb551997-08-05 02:04:34 +00001004}
1005
1006void
1007PyMem_Free(p)
1008 ANY *p;
1009{
Guido van Rossumb18618d2000-05-03 23:44:39 +00001010 PyMem_FREE(p);
1011}
1012
1013
1014/* Python's object malloc wrappers (see objimpl.h) */
1015
1016ANY *
1017PyObject_Malloc(nbytes)
1018 size_t nbytes;
1019{
1020 return PyObject_MALLOC(nbytes);
1021}
1022
1023ANY *
1024PyObject_Realloc(p, nbytes)
1025 ANY *p;
1026 size_t nbytes;
1027{
1028 return PyObject_REALLOC(p, nbytes);
1029}
1030
1031void
1032PyObject_Free(p)
1033 ANY *p;
1034{
1035 PyObject_FREE(p);
Guido van Rossume09fb551997-08-05 02:04:34 +00001036}
Guido van Rossum86610361998-04-10 22:32:46 +00001037
1038
1039/* These methods are used to control infinite recursion in repr, str, print,
1040 etc. Container objects that may recursively contain themselves,
1041 e.g. builtin dictionaries and lists, should used Py_ReprEnter() and
1042 Py_ReprLeave() to avoid infinite recursion.
1043
1044 Py_ReprEnter() returns 0 the first time it is called for a particular
1045 object and 1 every time thereafter. It returns -1 if an exception
1046 occurred. Py_ReprLeave() has no return value.
1047
1048 See dictobject.c and listobject.c for examples of use.
1049*/
1050
1051#define KEY "Py_Repr"
1052
1053int
1054Py_ReprEnter(obj)
1055 PyObject *obj;
1056{
1057 PyObject *dict;
1058 PyObject *list;
1059 int i;
1060
1061 dict = PyThreadState_GetDict();
1062 if (dict == NULL)
1063 return -1;
1064 list = PyDict_GetItemString(dict, KEY);
1065 if (list == NULL) {
1066 list = PyList_New(0);
1067 if (list == NULL)
1068 return -1;
1069 if (PyDict_SetItemString(dict, KEY, list) < 0)
1070 return -1;
1071 Py_DECREF(list);
1072 }
1073 i = PyList_GET_SIZE(list);
1074 while (--i >= 0) {
1075 if (PyList_GET_ITEM(list, i) == obj)
1076 return 1;
1077 }
1078 PyList_Append(list, obj);
1079 return 0;
1080}
1081
1082void
1083Py_ReprLeave(obj)
1084 PyObject *obj;
1085{
1086 PyObject *dict;
1087 PyObject *list;
1088 int i;
1089
1090 dict = PyThreadState_GetDict();
Guido van Rossumeb909461998-04-11 15:17:34 +00001091 if (dict == NULL)
1092 return;
Guido van Rossum86610361998-04-10 22:32:46 +00001093 list = PyDict_GetItemString(dict, KEY);
Guido van Rossumeb909461998-04-11 15:17:34 +00001094 if (list == NULL || !PyList_Check(list))
1095 return;
Guido van Rossum86610361998-04-10 22:32:46 +00001096 i = PyList_GET_SIZE(list);
1097 /* Count backwards because we always expect obj to be list[-1] */
1098 while (--i >= 0) {
1099 if (PyList_GET_ITEM(list, i) == obj) {
1100 PyList_SetSlice(list, i, i + 1, NULL);
1101 break;
1102 }
1103 }
1104}
Guido van Rossumd724b232000-03-13 16:01:29 +00001105
1106/*
1107 trashcan
1108 CT 2k0130
1109 non-recursively destroy nested objects
1110
1111 CT 2k0223
1112 everything is now done in a macro.
1113
1114 CT 2k0305
1115 modified to use functions, after Tim Peter's suggestion.
1116
1117 CT 2k0309
1118 modified to restore a possible error.
Guido van Rossum13ff8eb2000-03-25 18:39:19 +00001119
1120 CT 2k0325
1121 added better safe than sorry check for threadstate
Guido van Rossume92e6102000-04-24 15:40:53 +00001122
1123 CT 2k0422
1124 complete rewrite. We now build a chain via ob_type
1125 and save the limited number of types in ob_refcnt.
1126 This is perfect since we don't need any memory.
1127 A patch for free-threading would need just a lock.
Guido van Rossumd724b232000-03-13 16:01:29 +00001128*/
1129
Guido van Rossume92e6102000-04-24 15:40:53 +00001130#define Py_TRASHCAN_TUPLE 1
1131#define Py_TRASHCAN_LIST 2
1132#define Py_TRASHCAN_DICT 3
1133#define Py_TRASHCAN_FRAME 4
1134#define Py_TRASHCAN_TRACEBACK 5
1135/* extend here if other objects want protection */
1136
Guido van Rossumd724b232000-03-13 16:01:29 +00001137int _PyTrash_delete_nesting = 0;
Guido van Rossume92e6102000-04-24 15:40:53 +00001138
Guido van Rossumd724b232000-03-13 16:01:29 +00001139PyObject * _PyTrash_delete_later = NULL;
1140
1141void
1142_PyTrash_deposit_object(op)
1143 PyObject *op;
1144{
Guido van Rossume92e6102000-04-24 15:40:53 +00001145 int typecode;
1146 PyObject *hold = _PyTrash_delete_later;
Guido van Rossum13ff8eb2000-03-25 18:39:19 +00001147
Guido van Rossume92e6102000-04-24 15:40:53 +00001148 if (PyTuple_Check(op))
1149 typecode = Py_TRASHCAN_TUPLE;
1150 else if (PyList_Check(op))
1151 typecode = Py_TRASHCAN_LIST;
1152 else if (PyDict_Check(op))
1153 typecode = Py_TRASHCAN_DICT;
1154 else if (PyFrame_Check(op))
1155 typecode = Py_TRASHCAN_FRAME;
1156 else if (PyTraceBack_Check(op))
1157 typecode = Py_TRASHCAN_TRACEBACK;
1158 op->ob_refcnt = typecode;
Guido van Rossumd724b232000-03-13 16:01:29 +00001159
Guido van Rossume92e6102000-04-24 15:40:53 +00001160 op->ob_type = (PyTypeObject*)_PyTrash_delete_later;
1161 _PyTrash_delete_later = op;
Guido van Rossumd724b232000-03-13 16:01:29 +00001162}
1163
1164void
Guido van Rossume92e6102000-04-24 15:40:53 +00001165_PyTrash_destroy_chain()
Guido van Rossumd724b232000-03-13 16:01:29 +00001166{
1167 while (_PyTrash_delete_later) {
1168 PyObject *shredder = _PyTrash_delete_later;
Guido van Rossume92e6102000-04-24 15:40:53 +00001169 _PyTrash_delete_later = (PyObject*) shredder->ob_type;
1170
1171 switch (shredder->ob_refcnt) {
1172 case Py_TRASHCAN_TUPLE:
1173 shredder->ob_type = &PyTuple_Type;
1174 break;
1175 case Py_TRASHCAN_LIST:
1176 shredder->ob_type = &PyList_Type;
1177 break;
1178 case Py_TRASHCAN_DICT:
1179 shredder->ob_type = &PyDict_Type;
1180 break;
1181 case Py_TRASHCAN_FRAME:
1182 shredder->ob_type = &PyFrame_Type;
1183 break;
1184 case Py_TRASHCAN_TRACEBACK:
1185 shredder->ob_type = &PyTraceBack_Type;
1186 break;
1187 }
1188 _Py_NewReference(shredder);
1189
Guido van Rossumd724b232000-03-13 16:01:29 +00001190 ++_PyTrash_delete_nesting;
1191 Py_DECREF(shredder);
1192 --_PyTrash_delete_nesting;
1193 }
1194}
Guido van Rossume92e6102000-04-24 15:40:53 +00001195