blob: 556079276cacf8fd98a1c66af14e9ec114222131 [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
Guido van Rossume92e6102000-04-24 15:40:53 +000036/* just for trashcan: */
37#include "compile.h"
38#include "frameobject.h"
39#include "traceback.h"
40
Guido van Rossum6f9e4331995-03-29 16:57:48 +000041#if defined( Py_TRACE_REFS ) || defined( Py_REF_DEBUG )
Guido van Rossumbffd6832000-01-20 22:32:56 +000042DL_IMPORT(long) _Py_RefTotal;
Guido van Rossum3f5da241990-12-20 15:06:42 +000043#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000044
Guido van Rossum3f5da241990-12-20 15:06:42 +000045/* Object allocation routines used by NEWOBJ and NEWVAROBJ macros.
46 These are used by the individual routines for object creation.
47 Do not call them otherwise, they do not initialize the object! */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000048
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +000049#ifdef COUNT_ALLOCS
Guido van Rossumc0b618a1997-05-02 03:12:38 +000050static PyTypeObject *type_list;
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +000051extern int tuple_zero_allocs, fast_tuple_allocs;
52extern int quick_int_allocs, quick_neg_int_allocs;
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +000053extern int null_strings, one_strings;
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +000054void
55dump_counts()
56{
Guido van Rossumc0b618a1997-05-02 03:12:38 +000057 PyTypeObject *tp;
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +000058
59 for (tp = type_list; tp; tp = tp->tp_next)
Sjoerd Mullender52c1f511993-10-25 08:40:52 +000060 fprintf(stderr, "%s alloc'd: %d, freed: %d, max in use: %d\n",
61 tp->tp_name, tp->tp_alloc, tp->tp_free,
62 tp->tp_maxalloc);
63 fprintf(stderr, "fast tuple allocs: %d, empty: %d\n",
64 fast_tuple_allocs, tuple_zero_allocs);
65 fprintf(stderr, "fast int allocs: pos: %d, neg: %d\n",
66 quick_int_allocs, quick_neg_int_allocs);
67 fprintf(stderr, "null strings: %d, 1-strings: %d\n",
68 null_strings, one_strings);
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +000069}
70
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +000071PyObject *
72get_counts()
73{
74 PyTypeObject *tp;
75 PyObject *result;
76 PyObject *v;
77
78 result = PyList_New(0);
79 if (result == NULL)
80 return NULL;
81 for (tp = type_list; tp; tp = tp->tp_next) {
82 v = Py_BuildValue("(siii)", tp->tp_name, tp->tp_alloc,
83 tp->tp_free, tp->tp_maxalloc);
84 if (v == NULL) {
85 Py_DECREF(result);
86 return NULL;
87 }
88 if (PyList_Append(result, v) < 0) {
89 Py_DECREF(v);
90 Py_DECREF(result);
91 return NULL;
92 }
93 Py_DECREF(v);
94 }
95 return result;
96}
97
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +000098void
99inc_count(tp)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000100 PyTypeObject *tp;
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +0000101{
102 if (tp->tp_alloc == 0) {
Guido van Rossumd8953cb1995-04-06 14:46:26 +0000103 /* first time; insert in linked list */
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +0000104 if (tp->tp_next != NULL) /* sanity check */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000105 Py_FatalError("XXX inc_count sanity check");
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +0000106 tp->tp_next = type_list;
107 type_list = tp;
108 }
109 tp->tp_alloc++;
110 if (tp->tp_alloc - tp->tp_free > tp->tp_maxalloc)
111 tp->tp_maxalloc = tp->tp_alloc - tp->tp_free;
112}
113#endif
114
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000115PyObject *
Guido van Rossumb18618d2000-05-03 23:44:39 +0000116PyObject_Init(op, tp)
Guido van Rossumf5030ab1996-07-21 02:30:39 +0000117 PyObject *op;
Guido van Rossumb18618d2000-05-03 23:44:39 +0000118 PyTypeObject *tp;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000119{
Guido van Rossumb18618d2000-05-03 23:44:39 +0000120 if (op == NULL) {
121 PyErr_SetString(PyExc_SystemError,
122 "NULL object passed to PyObject_Init");
123 return op;
124 }
125 /* Any changes should be reflected in PyObject_INIT (objimpl.h) */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000126 op->ob_type = tp;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000127 _Py_NewReference(op);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000128 return op;
129}
130
Guido van Rossumb18618d2000-05-03 23:44:39 +0000131PyVarObject *
132PyObject_InitVar(op, tp, size)
133 PyVarObject *op;
134 PyTypeObject *tp;
135 int size;
136{
137 if (op == NULL) {
138 PyErr_SetString(PyExc_SystemError,
139 "NULL object passed to PyObject_InitVar");
140 return op;
141 }
142 /* Any changes should be reflected in PyObject_INIT_VAR */
143 op->ob_size = size;
144 op->ob_type = tp;
145 _Py_NewReference((PyObject *)op);
146 return op;
147}
148
149PyObject *
150_PyObject_New(tp)
151 PyTypeObject *tp;
152{
153 PyObject *op;
154 op = (PyObject *) PyObject_MALLOC(_PyObject_SIZE(tp));
155 if (op == NULL)
156 return PyErr_NoMemory();
157 return PyObject_INIT(op, tp);
158}
159
Guido van Rossumd0c87ee1997-05-15 21:31:03 +0000160PyVarObject *
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000161_PyObject_NewVar(tp, size)
162 PyTypeObject *tp;
Guido van Rossum2497ead1995-02-10 17:00:27 +0000163 int size;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000164{
Guido van Rossumb18618d2000-05-03 23:44:39 +0000165 PyVarObject *op;
166 op = (PyVarObject *) PyObject_MALLOC(_PyObject_VAR_SIZE(tp, size));
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000167 if (op == NULL)
Guido van Rossumd0c87ee1997-05-15 21:31:03 +0000168 return (PyVarObject *)PyErr_NoMemory();
Guido van Rossumb18618d2000-05-03 23:44:39 +0000169 return PyObject_INIT_VAR(op, tp, size);
170}
171
172void
173_PyObject_Del(op)
174 PyObject *op;
175{
176 PyObject_FREE(op);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000177}
178
Guido van Rossum90933611991-06-07 16:10:43 +0000179int
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000180PyObject_Print(op, fp, flags)
181 PyObject *op;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000182 FILE *fp;
183 int flags;
184{
Guido van Rossum278ef591991-07-27 21:40:24 +0000185 int ret = 0;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000186 if (PyErr_CheckSignals())
Guido van Rossum90933611991-06-07 16:10:43 +0000187 return -1;
Guido van Rossum9b00dfa1998-04-28 16:06:54 +0000188#ifdef USE_STACKCHECK
189 if (PyOS_CheckStack()) {
190 PyErr_SetString(PyExc_MemoryError, "Stack overflow");
191 return -1;
192 }
193#endif
Guido van Rossum687ef6e2000-01-12 16:28:58 +0000194 clearerr(fp); /* Clear any previous error condition */
Guido van Rossum90933611991-06-07 16:10:43 +0000195 if (op == NULL) {
196 fprintf(fp, "<nil>");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000197 }
Guido van Rossum90933611991-06-07 16:10:43 +0000198 else {
199 if (op->ob_refcnt <= 0)
Guido van Rossum2e8f6141992-09-03 20:32:55 +0000200 fprintf(fp, "<refcnt %u at %lx>",
201 op->ob_refcnt, (long)op);
202 else if (op->ob_type->tp_print == NULL) {
203 if (op->ob_type->tp_repr == NULL) {
204 fprintf(fp, "<%s object at %lx>",
205 op->ob_type->tp_name, (long)op);
206 }
207 else {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000208 PyObject *s;
209 if (flags & Py_PRINT_RAW)
210 s = PyObject_Str(op);
Guido van Rossumc6004111993-11-05 10:22:19 +0000211 else
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000212 s = PyObject_Repr(op);
Guido van Rossum2e8f6141992-09-03 20:32:55 +0000213 if (s == NULL)
214 ret = -1;
Guido van Rossum2e8f6141992-09-03 20:32:55 +0000215 else {
Guido van Rossum565798d1998-04-21 22:25:01 +0000216 ret = PyObject_Print(s, fp,
217 Py_PRINT_RAW);
Guido van Rossum2e8f6141992-09-03 20:32:55 +0000218 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000219 Py_XDECREF(s);
Guido van Rossum2e8f6141992-09-03 20:32:55 +0000220 }
221 }
Guido van Rossum90933611991-06-07 16:10:43 +0000222 else
Guido van Rossum278ef591991-07-27 21:40:24 +0000223 ret = (*op->ob_type->tp_print)(op, fp, flags);
Guido van Rossum90933611991-06-07 16:10:43 +0000224 }
Guido van Rossum278ef591991-07-27 21:40:24 +0000225 if (ret == 0) {
226 if (ferror(fp)) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000227 PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossum278ef591991-07-27 21:40:24 +0000228 clearerr(fp);
229 ret = -1;
230 }
231 }
232 return ret;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000233}
234
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000235PyObject *
236PyObject_Repr(v)
237 PyObject *v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000238{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000239 if (PyErr_CheckSignals())
Guido van Rossum90933611991-06-07 16:10:43 +0000240 return NULL;
Guido van Rossum9b00dfa1998-04-28 16:06:54 +0000241#ifdef USE_STACKCHECK
242 if (PyOS_CheckStack()) {
243 PyErr_SetString(PyExc_MemoryError, "Stack overflow");
244 return NULL;
245 }
246#endif
Guido van Rossum90933611991-06-07 16:10:43 +0000247 if (v == NULL)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000248 return PyString_FromString("<NULL>");
Guido van Rossum90933611991-06-07 16:10:43 +0000249 else if (v->ob_type->tp_repr == NULL) {
250 char buf[120];
251 sprintf(buf, "<%.80s object at %lx>",
252 v->ob_type->tp_name, (long)v);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000253 return PyString_FromString(buf);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000254 }
Guido van Rossum4c08d552000-03-10 22:55:18 +0000255 else {
256 PyObject *res;
257 res = (*v->ob_type->tp_repr)(v);
258 if (res == NULL)
259 return NULL;
260 if (!PyString_Check(res)) {
261 PyErr_Format(PyExc_TypeError,
Guido van Rossum5db862d2000-04-10 12:46:51 +0000262 "__repr__ returned non-string (type %.200s)",
Guido van Rossum4c08d552000-03-10 22:55:18 +0000263 res->ob_type->tp_name);
264 Py_DECREF(res);
265 return NULL;
266 }
267 return res;
268 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000269}
270
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000271PyObject *
272PyObject_Str(v)
273 PyObject *v;
Guido van Rossumc6004111993-11-05 10:22:19 +0000274{
Guido van Rossum4c08d552000-03-10 22:55:18 +0000275 PyObject *res;
276
Guido van Rossumc6004111993-11-05 10:22:19 +0000277 if (v == NULL)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000278 return PyString_FromString("<NULL>");
279 else if (PyString_Check(v)) {
280 Py_INCREF(v);
Guido van Rossumc6004111993-11-05 10:22:19 +0000281 return v;
282 }
Guido van Rossum32b582b1995-01-17 16:35:13 +0000283 else if (v->ob_type->tp_str != NULL)
Guido van Rossum4c08d552000-03-10 22:55:18 +0000284 res = (*v->ob_type->tp_str)(v);
Guido van Rossumc6004111993-11-05 10:22:19 +0000285 else {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000286 PyObject *func;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000287 if (!PyInstance_Check(v) ||
288 (func = PyObject_GetAttrString(v, "__str__")) == NULL) {
289 PyErr_Clear();
290 return PyObject_Repr(v);
Guido van Rossumc6004111993-11-05 10:22:19 +0000291 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000292 res = PyEval_CallObject(func, (PyObject *)NULL);
293 Py_DECREF(func);
Guido van Rossumc6004111993-11-05 10:22:19 +0000294 }
Guido van Rossum4c08d552000-03-10 22:55:18 +0000295 if (res == NULL)
296 return NULL;
297 if (!PyString_Check(res)) {
298 PyErr_Format(PyExc_TypeError,
Guido van Rossum5db862d2000-04-10 12:46:51 +0000299 "__str__ returned non-string (type %.200s)",
Guido van Rossum4c08d552000-03-10 22:55:18 +0000300 res->ob_type->tp_name);
301 Py_DECREF(res);
302 return NULL;
303 }
304 return res;
Guido van Rossumc6004111993-11-05 10:22:19 +0000305}
306
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000307static PyObject *
Guido van Rossum20566841995-01-12 11:26:10 +0000308do_cmp(v, w)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000309 PyObject *v, *w;
Guido van Rossum20566841995-01-12 11:26:10 +0000310{
Guido van Rossumc8b6df91997-05-23 00:06:51 +0000311 long c;
Guido van Rossum20566841995-01-12 11:26:10 +0000312 /* __rcmp__ actually won't be called unless __cmp__ isn't defined,
313 because the check in cmpobject() reverses the objects first.
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000314 This is intentional -- it makes no sense to define cmp(x,y)
315 different than -cmp(y,x). */
316 if (PyInstance_Check(v) || PyInstance_Check(w))
317 return PyInstance_DoBinOp(v, w, "__cmp__", "__rcmp__", do_cmp);
Guido van Rossumc8b6df91997-05-23 00:06:51 +0000318 c = PyObject_Compare(v, w);
319 if (c && PyErr_Occurred())
320 return NULL;
321 return PyInt_FromLong(c);
Guido van Rossum20566841995-01-12 11:26:10 +0000322}
323
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000324PyObject *_PyCompareState_Key;
325
326/* _PyCompareState_nesting is incremented beforing call compare (for
327 some types) and decremented on exit. If the count exceeds the
328 nesting limit, enable code to detect circular data structures.
329*/
330#define NESTING_LIMIT 500
331int _PyCompareState_nesting = 0;
332
333static PyObject*
334get_inprogress_dict()
335{
336 PyObject *tstate_dict, *inprogress;
337
338 tstate_dict = PyThreadState_GetDict();
339 if (tstate_dict == NULL) {
340 PyErr_BadInternalCall();
341 return NULL;
342 }
343 inprogress = PyDict_GetItem(tstate_dict, _PyCompareState_Key);
344 if (inprogress == NULL) {
345 PyErr_Clear();
346 inprogress = PyDict_New();
347 if (inprogress == NULL)
348 return NULL;
349 if (PyDict_SetItem(tstate_dict, _PyCompareState_Key,
350 inprogress) == -1) {
351 Py_DECREF(inprogress);
352 return NULL;
353 }
Jeremy Hyltona251ea02000-06-09 16:20:39 +0000354 Py_DECREF(inprogress);
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000355 }
356 return inprogress;
357}
358
359static PyObject *
360make_pair(v, w)
361 PyObject *v, *w;
362{
363 PyObject *pair;
364
365 pair = PyTuple_New(2);
366 if (pair == NULL) {
367 return NULL;
368 }
369 if ((long)v <= (long)w) {
370 PyTuple_SET_ITEM(pair, 0, PyLong_FromVoidPtr((void *)v));
371 PyTuple_SET_ITEM(pair, 1, PyLong_FromVoidPtr((void *)w));
372 } else {
373 PyTuple_SET_ITEM(pair, 0, PyLong_FromVoidPtr((void *)w));
374 PyTuple_SET_ITEM(pair, 1, PyLong_FromVoidPtr((void *)v));
375 }
376 return pair;
377}
378
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000379int
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000380PyObject_Compare(v, w)
381 PyObject *v, *w;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000382{
Guido van Rossumcd5a5f61998-06-09 18:58:44 +0000383 PyTypeObject *vtp, *wtp;
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000384 int result;
385
Guido van Rossumc8b6df91997-05-23 00:06:51 +0000386 if (v == NULL || w == NULL) {
387 PyErr_BadInternalCall();
388 return -1;
389 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000390 if (v == w)
391 return 0;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000392 if (PyInstance_Check(v) || PyInstance_Check(w)) {
393 PyObject *res;
Guido van Rossum20566841995-01-12 11:26:10 +0000394 int c;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000395 if (!PyInstance_Check(v))
396 return -PyObject_Compare(w, v);
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000397 if (++_PyCompareState_nesting > NESTING_LIMIT) {
398 PyObject *inprogress, *pair;
399
400 inprogress = get_inprogress_dict();
401 if (inprogress == NULL) {
402 return -1;
403 }
404 pair = make_pair(v, w);
405 if (PyDict_GetItem(inprogress, pair)) {
406 /* already comparing these objects. assume
407 they're equal until shown otherwise */
408 Py_DECREF(pair);
409 --_PyCompareState_nesting;
410 return 0;
411 }
412 if (PyDict_SetItem(inprogress, pair, pair) == -1) {
413 return -1;
414 }
415 res = do_cmp(v, w);
416 _PyCompareState_nesting--;
417 /* XXX DelItem shouldn't fail */
418 PyDict_DelItem(inprogress, pair);
419 Py_DECREF(pair);
420 } else {
421 res = do_cmp(v, w);
422 }
Guido van Rossumc8b6df91997-05-23 00:06:51 +0000423 if (res == NULL)
424 return -1;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000425 if (!PyInt_Check(res)) {
426 Py_DECREF(res);
Guido van Rossumc8b6df91997-05-23 00:06:51 +0000427 PyErr_SetString(PyExc_TypeError,
428 "comparison did not return an int");
429 return -1;
Guido van Rossum20566841995-01-12 11:26:10 +0000430 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000431 c = PyInt_AsLong(res);
432 Py_DECREF(res);
Guido van Rossum20566841995-01-12 11:26:10 +0000433 return (c < 0) ? -1 : (c > 0) ? 1 : 0;
434 }
Guido van Rossumcd5a5f61998-06-09 18:58:44 +0000435 if ((vtp = v->ob_type) != (wtp = w->ob_type)) {
436 char *vname = vtp->tp_name;
437 char *wname = wtp->tp_name;
Guido van Rossumb4db1941998-07-21 21:56:41 +0000438 if (vtp->tp_as_number != NULL && wtp->tp_as_number != NULL) {
Guido van Rossum242c6421997-11-19 16:03:17 +0000439 int err;
440 err = PyNumber_CoerceEx(&v, &w);
441 if (err < 0)
Guido van Rossumc8b6df91997-05-23 00:06:51 +0000442 return -1;
Guido van Rossum242c6421997-11-19 16:03:17 +0000443 else if (err == 0) {
Guido van Rossumb4db1941998-07-21 21:56:41 +0000444 int cmp;
445 vtp = v->ob_type;
446 if (vtp->tp_compare == NULL)
447 cmp = (v < w) ? -1 : 1;
448 else
449 cmp = (*vtp->tp_compare)(v, w);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000450 Py_DECREF(v);
451 Py_DECREF(w);
Guido van Rossum9fb03681991-07-01 18:48:04 +0000452 return cmp;
453 }
454 }
Guido van Rossumb244f692000-04-10 13:42:33 +0000455 else if (PyUnicode_Check(v) || PyUnicode_Check(w)) {
456 int result = PyUnicode_Compare(v, w);
457 if (result == -1 && PyErr_Occurred() &&
458 PyErr_ExceptionMatches(PyExc_TypeError))
459 /* TypeErrors are ignored: if Unicode coercion
460 fails due to one of the arguments not
461 having the right type, we continue as
462 defined by the coercion protocol (see
463 above). Luckily, decoding errors are
464 reported as ValueErrors and are not masked
465 by this technique. */
466 PyErr_Clear();
467 else
468 return result;
469 }
Guido van Rossumcd5a5f61998-06-09 18:58:44 +0000470 else if (vtp->tp_as_number != NULL)
471 vname = "";
472 else if (wtp->tp_as_number != NULL)
473 wname = "";
474 /* Numerical types compare smaller than all other types */
475 return strcmp(vname, wname);
Guido van Rossum9fb03681991-07-01 18:48:04 +0000476 }
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000477 if (vtp->tp_compare == NULL) {
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000478 return (v < w) ? -1 : 1;
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000479 }
480 if (++_PyCompareState_nesting > NESTING_LIMIT
481 && (vtp->tp_as_mapping
482 || (vtp->tp_as_sequence && !PyString_Check(v)))) {
483 PyObject *inprogress, *pair;
484
485 inprogress = get_inprogress_dict();
486 if (inprogress == NULL) {
487 return -1;
488 }
489 pair = make_pair(v, w);
490 if (PyDict_GetItem(inprogress, pair)) {
491 /* already comparing these objects. assume
492 they're equal until shown otherwise */
493 _PyCompareState_nesting--;
494 Py_DECREF(pair);
495 return 0;
496 }
497 if (PyDict_SetItem(inprogress, pair, pair) == -1) {
498 return -1;
499 }
500 result = (*vtp->tp_compare)(v, w);
501 _PyCompareState_nesting--;
502 PyDict_DelItem(inprogress, pair); /* XXX shouldn't fail */
503 Py_DECREF(pair);
504 } else {
505 result = (*vtp->tp_compare)(v, w);
506 }
507 return result;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000508}
509
Guido van Rossum9bfef441993-03-29 10:43:31 +0000510long
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000511PyObject_Hash(v)
512 PyObject *v;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000513{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000514 PyTypeObject *tp = v->ob_type;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000515 if (tp->tp_hash != NULL)
516 return (*tp->tp_hash)(v);
517 if (tp->tp_compare == NULL)
518 return (long) v; /* Use address as hash value */
519 /* If there's a cmp but no hash defined, the object can't be hashed */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000520 PyErr_SetString(PyExc_TypeError, "unhashable type");
Guido van Rossum9bfef441993-03-29 10:43:31 +0000521 return -1;
522}
523
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000524PyObject *
525PyObject_GetAttrString(v, name)
526 PyObject *v;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000527 char *name;
528{
Guido van Rossumd8eb1b31996-08-09 20:52:03 +0000529 if (v->ob_type->tp_getattro != NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000530 PyObject *w, *res;
Guido van Rossumc6d06701997-01-18 07:57:16 +0000531 w = PyString_InternFromString(name);
Guido van Rossumd8eb1b31996-08-09 20:52:03 +0000532 if (w == NULL)
533 return NULL;
534 res = (*v->ob_type->tp_getattro)(v, w);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000535 Py_XDECREF(w);
Guido van Rossumd8eb1b31996-08-09 20:52:03 +0000536 return res;
537 }
538
Guido van Rossum3f5da241990-12-20 15:06:42 +0000539 if (v->ob_type->tp_getattr == NULL) {
Guido van Rossumdb935161998-01-19 22:16:36 +0000540 PyErr_Format(PyExc_AttributeError,
541 "'%.50s' object has no attribute '%.400s'",
542 v->ob_type->tp_name,
543 name);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000544 return NULL;
545 }
546 else {
547 return (*v->ob_type->tp_getattr)(v, name);
548 }
549}
550
551int
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000552PyObject_HasAttrString(v, name)
553 PyObject *v;
Guido van Rossumed18fdc1993-07-11 19:55:34 +0000554 char *name;
555{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000556 PyObject *res = PyObject_GetAttrString(v, name);
Guido van Rossumed18fdc1993-07-11 19:55:34 +0000557 if (res != NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000558 Py_DECREF(res);
Guido van Rossumed18fdc1993-07-11 19:55:34 +0000559 return 1;
560 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000561 PyErr_Clear();
Guido van Rossumed18fdc1993-07-11 19:55:34 +0000562 return 0;
563}
564
565int
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000566PyObject_SetAttrString(v, name, w)
567 PyObject *v;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000568 char *name;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000569 PyObject *w;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000570{
Guido van Rossumd8eb1b31996-08-09 20:52:03 +0000571 if (v->ob_type->tp_setattro != NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000572 PyObject *s;
Guido van Rossumd8eb1b31996-08-09 20:52:03 +0000573 int res;
Guido van Rossumc6d06701997-01-18 07:57:16 +0000574 s = PyString_InternFromString(name);
Guido van Rossumd8eb1b31996-08-09 20:52:03 +0000575 if (s == NULL)
Guido van Rossumb7fc3041996-09-11 22:51:25 +0000576 return -1;
Guido van Rossumd8eb1b31996-08-09 20:52:03 +0000577 res = (*v->ob_type->tp_setattro)(v, s, w);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000578 Py_XDECREF(s);
Guido van Rossumd8eb1b31996-08-09 20:52:03 +0000579 return res;
580 }
581
Guido van Rossum3f5da241990-12-20 15:06:42 +0000582 if (v->ob_type->tp_setattr == NULL) {
583 if (v->ob_type->tp_getattr == NULL)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000584 PyErr_SetString(PyExc_TypeError,
Guido van Rossum3ea74121991-12-24 13:28:03 +0000585 "attribute-less object (assign or del)");
Guido van Rossum3f5da241990-12-20 15:06:42 +0000586 else
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000587 PyErr_SetString(PyExc_TypeError,
Guido van Rossum3ea74121991-12-24 13:28:03 +0000588 "object has read-only attributes");
Guido van Rossum73531a31990-12-20 23:12:40 +0000589 return -1;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000590 }
591 else {
592 return (*v->ob_type->tp_setattr)(v, name, w);
593 }
594}
595
Guido van Rossum98ff96a1997-05-20 18:34:44 +0000596PyObject *
597PyObject_GetAttr(v, name)
598 PyObject *v;
599 PyObject *name;
600{
601 if (v->ob_type->tp_getattro != NULL)
602 return (*v->ob_type->tp_getattro)(v, name);
Jeremy Hylton99a8f902000-06-23 14:36:32 +0000603
604 if (!PyString_Check(name)) {
605 PyErr_SetString(PyExc_TypeError,
606 "attribute name must be string");
607 return NULL;
608 }
609 return PyObject_GetAttrString(v, PyString_AS_STRING(name));
Guido van Rossum98ff96a1997-05-20 18:34:44 +0000610}
611
612int
613PyObject_HasAttr(v, name)
614 PyObject *v;
615 PyObject *name;
616{
617 PyObject *res = PyObject_GetAttr(v, name);
618 if (res != NULL) {
619 Py_DECREF(res);
620 return 1;
621 }
622 PyErr_Clear();
623 return 0;
624}
625
626int
627PyObject_SetAttr(v, name, value)
628 PyObject *v;
629 PyObject *name;
630 PyObject *value;
631{
632 int err;
633 Py_INCREF(name);
Jeremy Hylton99a8f902000-06-23 14:36:32 +0000634 if (PyString_Check(name))
635 PyString_InternInPlace(&name);
Guido van Rossum98ff96a1997-05-20 18:34:44 +0000636 if (v->ob_type->tp_setattro != NULL)
637 err = (*v->ob_type->tp_setattro)(v, name, value);
Jeremy Hylton99a8f902000-06-23 14:36:32 +0000638 else if (PyString_Check(name)) {
Guido van Rossum98ff96a1997-05-20 18:34:44 +0000639 err = PyObject_SetAttrString(
Jeremy Hylton99a8f902000-06-23 14:36:32 +0000640 v, PyString_AS_STRING(name), value);
641 }
642 else {
643 PyErr_SetString(PyExc_TypeError,
644 "attribute name must be string");
645 err = -1;
646 }
Guido van Rossum98ff96a1997-05-20 18:34:44 +0000647 Py_DECREF(name);
648 return err;
649}
650
Guido van Rossum6ac258d1993-05-12 08:24:20 +0000651/* Test a value used as condition, e.g., in a for or if statement.
652 Return -1 if an error occurred */
653
654int
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000655PyObject_IsTrue(v)
656 PyObject *v;
Guido van Rossum6ac258d1993-05-12 08:24:20 +0000657{
658 int res;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000659 if (v == Py_None)
Guido van Rossum6ac258d1993-05-12 08:24:20 +0000660 res = 0;
Guido van Rossum1c4f4581998-05-22 00:53:24 +0000661 else if (v->ob_type->tp_as_number != NULL &&
662 v->ob_type->tp_as_number->nb_nonzero != NULL)
Guido van Rossum6ac258d1993-05-12 08:24:20 +0000663 res = (*v->ob_type->tp_as_number->nb_nonzero)(v);
Guido van Rossum1c4f4581998-05-22 00:53:24 +0000664 else if (v->ob_type->tp_as_mapping != NULL &&
665 v->ob_type->tp_as_mapping->mp_length != NULL)
Guido van Rossum6ac258d1993-05-12 08:24:20 +0000666 res = (*v->ob_type->tp_as_mapping->mp_length)(v);
Guido van Rossum1c4f4581998-05-22 00:53:24 +0000667 else if (v->ob_type->tp_as_sequence != NULL &&
668 v->ob_type->tp_as_sequence->sq_length != NULL)
Guido van Rossum6ac258d1993-05-12 08:24:20 +0000669 res = (*v->ob_type->tp_as_sequence->sq_length)(v);
670 else
671 res = 1;
672 if (res > 0)
673 res = 1;
674 return res;
675}
676
Guido van Rossumc3d3f961998-04-09 17:53:59 +0000677/* equivalent of 'not v'
678 Return -1 if an error occurred */
679
680int
681PyObject_Not(v)
682 PyObject *v;
683{
684 int res;
685 res = PyObject_IsTrue(v);
686 if (res < 0)
687 return res;
688 return res == 0;
689}
690
Guido van Rossum5524a591995-01-10 15:26:20 +0000691/* Coerce two numeric types to the "larger" one.
692 Increment the reference count on each argument.
693 Return -1 and raise an exception if no coercion is possible
694 (and then no reference count is incremented).
695*/
696
697int
Guido van Rossum242c6421997-11-19 16:03:17 +0000698PyNumber_CoerceEx(pv, pw)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000699 PyObject **pv, **pw;
Guido van Rossum5524a591995-01-10 15:26:20 +0000700{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000701 register PyObject *v = *pv;
702 register PyObject *w = *pw;
Guido van Rossum5524a591995-01-10 15:26:20 +0000703 int res;
704
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000705 if (v->ob_type == w->ob_type && !PyInstance_Check(v)) {
706 Py_INCREF(v);
707 Py_INCREF(w);
Guido van Rossum5524a591995-01-10 15:26:20 +0000708 return 0;
709 }
710 if (v->ob_type->tp_as_number && v->ob_type->tp_as_number->nb_coerce) {
711 res = (*v->ob_type->tp_as_number->nb_coerce)(pv, pw);
712 if (res <= 0)
713 return res;
714 }
715 if (w->ob_type->tp_as_number && w->ob_type->tp_as_number->nb_coerce) {
716 res = (*w->ob_type->tp_as_number->nb_coerce)(pw, pv);
717 if (res <= 0)
718 return res;
719 }
Guido van Rossum242c6421997-11-19 16:03:17 +0000720 return 1;
721}
722
723int
724PyNumber_Coerce(pv, pw)
725 PyObject **pv, **pw;
726{
727 int err = PyNumber_CoerceEx(pv, pw);
728 if (err <= 0)
729 return err;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000730 PyErr_SetString(PyExc_TypeError, "number coercion failed");
Guido van Rossum5524a591995-01-10 15:26:20 +0000731 return -1;
732}
733
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000734
Guido van Rossum49b11fe1995-01-26 00:38:22 +0000735/* Test whether an object can be called */
736
737int
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000738PyCallable_Check(x)
739 PyObject *x;
Guido van Rossum49b11fe1995-01-26 00:38:22 +0000740{
741 if (x == NULL)
742 return 0;
743 if (x->ob_type->tp_call != NULL ||
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000744 PyFunction_Check(x) ||
745 PyMethod_Check(x) ||
746 PyCFunction_Check(x) ||
747 PyClass_Check(x))
Guido van Rossum49b11fe1995-01-26 00:38:22 +0000748 return 1;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000749 if (PyInstance_Check(x)) {
750 PyObject *call = PyObject_GetAttrString(x, "__call__");
Guido van Rossum49b11fe1995-01-26 00:38:22 +0000751 if (call == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000752 PyErr_Clear();
Guido van Rossum49b11fe1995-01-26 00:38:22 +0000753 return 0;
754 }
755 /* Could test recursively but don't, for fear of endless
756 recursion if some joker sets self.__call__ = self */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000757 Py_DECREF(call);
Guido van Rossum49b11fe1995-01-26 00:38:22 +0000758 return 1;
759 }
760 return 0;
761}
762
763
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000764/*
765NoObject is usable as a non-NULL undefined value, used by the macro None.
766There is (and should be!) no way to create other objects of this type,
Guido van Rossum3f5da241990-12-20 15:06:42 +0000767so there is exactly one (which is indestructible, by the way).
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000768*/
769
Guido van Rossum0c182a11992-03-27 17:26:13 +0000770/* ARGSUSED */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000771static PyObject *
Guido van Rossum3f5da241990-12-20 15:06:42 +0000772none_repr(op)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000773 PyObject *op;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000774{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000775 return PyString_FromString("None");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000776}
777
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000778static PyTypeObject PyNothing_Type = {
779 PyObject_HEAD_INIT(&PyType_Type)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000780 0,
Guido van Rossum3f5da241990-12-20 15:06:42 +0000781 "None",
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000782 0,
783 0,
784 0, /*tp_dealloc*/ /*never called*/
Guido van Rossum7066dd71992-09-17 17:54:56 +0000785 0, /*tp_print*/
Guido van Rossum3f5da241990-12-20 15:06:42 +0000786 0, /*tp_getattr*/
787 0, /*tp_setattr*/
788 0, /*tp_compare*/
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000789 (reprfunc)none_repr, /*tp_repr*/
Guido van Rossum3f5da241990-12-20 15:06:42 +0000790 0, /*tp_as_number*/
791 0, /*tp_as_sequence*/
792 0, /*tp_as_mapping*/
Guido van Rossum9bfef441993-03-29 10:43:31 +0000793 0, /*tp_hash */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000794};
795
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000796PyObject _Py_NoneStruct = {
797 PyObject_HEAD_INIT(&PyNothing_Type)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000798};
799
800
Guido van Rossum84a90321996-05-22 16:34:47 +0000801#ifdef Py_TRACE_REFS
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000802
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000803static PyObject refchain = {&refchain, &refchain};
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000804
Guido van Rossumaacdc9d1996-08-12 21:32:12 +0000805void
Guido van Rossume09fb551997-08-05 02:04:34 +0000806_Py_ResetReferences()
807{
808 refchain._ob_prev = refchain._ob_next = &refchain;
809 _Py_RefTotal = 0;
810}
811
812void
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000813_Py_NewReference(op)
814 PyObject *op;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000815{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000816 _Py_RefTotal++;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000817 op->ob_refcnt = 1;
818 op->_ob_next = refchain._ob_next;
819 op->_ob_prev = &refchain;
820 refchain._ob_next->_ob_prev = op;
821 refchain._ob_next = op;
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +0000822#ifdef COUNT_ALLOCS
823 inc_count(op->ob_type);
824#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000825}
826
Guido van Rossumaacdc9d1996-08-12 21:32:12 +0000827void
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000828_Py_ForgetReference(op)
829 register PyObject *op;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000830{
Guido van Rossumbffd6832000-01-20 22:32:56 +0000831#ifdef SLOW_UNREF_CHECK
Guido van Rossum4c08d552000-03-10 22:55:18 +0000832 register PyObject *p;
Guido van Rossumbffd6832000-01-20 22:32:56 +0000833#endif
Guido van Rossumd7047b31995-01-02 19:07:15 +0000834 if (op->ob_refcnt < 0)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000835 Py_FatalError("UNREF negative refcnt");
Guido van Rossum2e8f6141992-09-03 20:32:55 +0000836 if (op == &refchain ||
Guido van Rossumd7047b31995-01-02 19:07:15 +0000837 op->_ob_prev->_ob_next != op || op->_ob_next->_ob_prev != op)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000838 Py_FatalError("UNREF invalid object");
Guido van Rossum2e8f6141992-09-03 20:32:55 +0000839#ifdef SLOW_UNREF_CHECK
Guido van Rossum3f5da241990-12-20 15:06:42 +0000840 for (p = refchain._ob_next; p != &refchain; p = p->_ob_next) {
841 if (p == op)
842 break;
843 }
Guido van Rossumd7047b31995-01-02 19:07:15 +0000844 if (p == &refchain) /* Not found */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000845 Py_FatalError("UNREF unknown object");
Guido van Rossum2e8f6141992-09-03 20:32:55 +0000846#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000847 op->_ob_next->_ob_prev = op->_ob_prev;
848 op->_ob_prev->_ob_next = op->_ob_next;
Guido van Rossum2e8f6141992-09-03 20:32:55 +0000849 op->_ob_next = op->_ob_prev = NULL;
Guido van Rossumd8953cb1995-04-06 14:46:26 +0000850#ifdef COUNT_ALLOCS
851 op->ob_type->tp_free++;
852#endif
Guido van Rossum3f5da241990-12-20 15:06:42 +0000853}
854
Guido van Rossumaacdc9d1996-08-12 21:32:12 +0000855void
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000856_Py_Dealloc(op)
857 PyObject *op;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000858{
Guido van Rossum9776adf1994-09-07 14:36:45 +0000859 destructor dealloc = op->ob_type->tp_dealloc;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000860 _Py_ForgetReference(op);
Guido van Rossume92e6102000-04-24 15:40:53 +0000861 if (_PyTrash_delete_nesting < PyTrash_UNWIND_LEVEL-1)
862 op->ob_type = NULL;
Guido van Rossum9776adf1994-09-07 14:36:45 +0000863 (*dealloc)(op);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000864}
865
Guido van Rossumaacdc9d1996-08-12 21:32:12 +0000866void
Guido van Rossumded690f1996-05-24 20:48:31 +0000867_Py_PrintReferences(fp)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000868 FILE *fp;
869{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000870 PyObject *op;
Guido van Rossume09fb551997-08-05 02:04:34 +0000871 fprintf(fp, "Remaining objects:\n");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000872 for (op = refchain._ob_next; op != &refchain; op = op->_ob_next) {
873 fprintf(fp, "[%d] ", op->ob_refcnt);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000874 if (PyObject_Print(op, fp, 0) != 0)
875 PyErr_Clear();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000876 putc('\n', fp);
877 }
878}
879
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000880PyObject *
Guido van Rossumded690f1996-05-24 20:48:31 +0000881_Py_GetObjects(self, args)
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000882 PyObject *self;
883 PyObject *args;
884{
885 int i, n;
886 PyObject *t = NULL;
887 PyObject *res, *op;
888
889 if (!PyArg_ParseTuple(args, "i|O", &n, &t))
890 return NULL;
891 op = refchain._ob_next;
892 res = PyList_New(0);
893 if (res == NULL)
894 return NULL;
895 for (i = 0; (n == 0 || i < n) && op != &refchain; i++) {
896 while (op == self || op == args || op == res || op == t ||
897 t != NULL && op->ob_type != (PyTypeObject *) t) {
898 op = op->_ob_next;
899 if (op == &refchain)
900 return res;
901 }
902 if (PyList_Append(res, op) < 0) {
903 Py_DECREF(res);
904 return NULL;
905 }
906 op = op->_ob_next;
907 }
908 return res;
909}
910
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000911#endif
Guido van Rossum97ead3f1996-01-12 01:24:09 +0000912
913
914/* Hack to force loading of cobject.o */
Guido van Rossumda9c2711996-12-05 21:58:58 +0000915PyTypeObject *_Py_cobject_hack = &PyCObject_Type;
Guido van Rossum84a90321996-05-22 16:34:47 +0000916
917
918/* Hack to force loading of abstract.o */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000919int (*_Py_abstract_hack) Py_FPROTO((PyObject *)) = &PyObject_Length;
Guido van Rossume09fb551997-08-05 02:04:34 +0000920
921
Guido van Rossumb18618d2000-05-03 23:44:39 +0000922/* Python's malloc wrappers (see mymalloc.h) */
Guido van Rossume09fb551997-08-05 02:04:34 +0000923
924ANY *
925PyMem_Malloc(nbytes)
926 size_t nbytes;
927{
928#if _PyMem_EXTRA > 0
929 if (nbytes == 0)
930 nbytes = _PyMem_EXTRA;
931#endif
Guido van Rossumb18618d2000-05-03 23:44:39 +0000932 return PyMem_MALLOC(nbytes);
Guido van Rossume09fb551997-08-05 02:04:34 +0000933}
934
935ANY *
936PyMem_Realloc(p, nbytes)
937 ANY *p;
938 size_t nbytes;
939{
940#if _PyMem_EXTRA > 0
941 if (nbytes == 0)
942 nbytes = _PyMem_EXTRA;
943#endif
Guido van Rossumb18618d2000-05-03 23:44:39 +0000944 return PyMem_REALLOC(p, nbytes);
Guido van Rossume09fb551997-08-05 02:04:34 +0000945}
946
947void
948PyMem_Free(p)
949 ANY *p;
950{
Guido van Rossumb18618d2000-05-03 23:44:39 +0000951 PyMem_FREE(p);
952}
953
954
955/* Python's object malloc wrappers (see objimpl.h) */
956
957ANY *
958PyObject_Malloc(nbytes)
959 size_t nbytes;
960{
961 return PyObject_MALLOC(nbytes);
962}
963
964ANY *
965PyObject_Realloc(p, nbytes)
966 ANY *p;
967 size_t nbytes;
968{
969 return PyObject_REALLOC(p, nbytes);
970}
971
972void
973PyObject_Free(p)
974 ANY *p;
975{
976 PyObject_FREE(p);
Guido van Rossume09fb551997-08-05 02:04:34 +0000977}
Guido van Rossum86610361998-04-10 22:32:46 +0000978
979
980/* These methods are used to control infinite recursion in repr, str, print,
981 etc. Container objects that may recursively contain themselves,
982 e.g. builtin dictionaries and lists, should used Py_ReprEnter() and
983 Py_ReprLeave() to avoid infinite recursion.
984
985 Py_ReprEnter() returns 0 the first time it is called for a particular
986 object and 1 every time thereafter. It returns -1 if an exception
987 occurred. Py_ReprLeave() has no return value.
988
989 See dictobject.c and listobject.c for examples of use.
990*/
991
992#define KEY "Py_Repr"
993
994int
995Py_ReprEnter(obj)
996 PyObject *obj;
997{
998 PyObject *dict;
999 PyObject *list;
1000 int i;
1001
1002 dict = PyThreadState_GetDict();
1003 if (dict == NULL)
1004 return -1;
1005 list = PyDict_GetItemString(dict, KEY);
1006 if (list == NULL) {
1007 list = PyList_New(0);
1008 if (list == NULL)
1009 return -1;
1010 if (PyDict_SetItemString(dict, KEY, list) < 0)
1011 return -1;
1012 Py_DECREF(list);
1013 }
1014 i = PyList_GET_SIZE(list);
1015 while (--i >= 0) {
1016 if (PyList_GET_ITEM(list, i) == obj)
1017 return 1;
1018 }
1019 PyList_Append(list, obj);
1020 return 0;
1021}
1022
1023void
1024Py_ReprLeave(obj)
1025 PyObject *obj;
1026{
1027 PyObject *dict;
1028 PyObject *list;
1029 int i;
1030
1031 dict = PyThreadState_GetDict();
Guido van Rossumeb909461998-04-11 15:17:34 +00001032 if (dict == NULL)
1033 return;
Guido van Rossum86610361998-04-10 22:32:46 +00001034 list = PyDict_GetItemString(dict, KEY);
Guido van Rossumeb909461998-04-11 15:17:34 +00001035 if (list == NULL || !PyList_Check(list))
1036 return;
Guido van Rossum86610361998-04-10 22:32:46 +00001037 i = PyList_GET_SIZE(list);
1038 /* Count backwards because we always expect obj to be list[-1] */
1039 while (--i >= 0) {
1040 if (PyList_GET_ITEM(list, i) == obj) {
1041 PyList_SetSlice(list, i, i + 1, NULL);
1042 break;
1043 }
1044 }
1045}
Guido van Rossumd724b232000-03-13 16:01:29 +00001046
1047/*
1048 trashcan
1049 CT 2k0130
1050 non-recursively destroy nested objects
1051
1052 CT 2k0223
1053 everything is now done in a macro.
1054
1055 CT 2k0305
1056 modified to use functions, after Tim Peter's suggestion.
1057
1058 CT 2k0309
1059 modified to restore a possible error.
Guido van Rossum13ff8eb2000-03-25 18:39:19 +00001060
1061 CT 2k0325
1062 added better safe than sorry check for threadstate
Guido van Rossume92e6102000-04-24 15:40:53 +00001063
1064 CT 2k0422
1065 complete rewrite. We now build a chain via ob_type
1066 and save the limited number of types in ob_refcnt.
1067 This is perfect since we don't need any memory.
1068 A patch for free-threading would need just a lock.
Guido van Rossumd724b232000-03-13 16:01:29 +00001069*/
1070
Guido van Rossume92e6102000-04-24 15:40:53 +00001071#define Py_TRASHCAN_TUPLE 1
1072#define Py_TRASHCAN_LIST 2
1073#define Py_TRASHCAN_DICT 3
1074#define Py_TRASHCAN_FRAME 4
1075#define Py_TRASHCAN_TRACEBACK 5
1076/* extend here if other objects want protection */
1077
Guido van Rossumd724b232000-03-13 16:01:29 +00001078int _PyTrash_delete_nesting = 0;
Guido van Rossume92e6102000-04-24 15:40:53 +00001079
Guido van Rossumd724b232000-03-13 16:01:29 +00001080PyObject * _PyTrash_delete_later = NULL;
1081
1082void
1083_PyTrash_deposit_object(op)
1084 PyObject *op;
1085{
Guido van Rossume92e6102000-04-24 15:40:53 +00001086 int typecode;
1087 PyObject *hold = _PyTrash_delete_later;
Guido van Rossum13ff8eb2000-03-25 18:39:19 +00001088
Guido van Rossume92e6102000-04-24 15:40:53 +00001089 if (PyTuple_Check(op))
1090 typecode = Py_TRASHCAN_TUPLE;
1091 else if (PyList_Check(op))
1092 typecode = Py_TRASHCAN_LIST;
1093 else if (PyDict_Check(op))
1094 typecode = Py_TRASHCAN_DICT;
1095 else if (PyFrame_Check(op))
1096 typecode = Py_TRASHCAN_FRAME;
1097 else if (PyTraceBack_Check(op))
1098 typecode = Py_TRASHCAN_TRACEBACK;
1099 op->ob_refcnt = typecode;
Guido van Rossumd724b232000-03-13 16:01:29 +00001100
Guido van Rossume92e6102000-04-24 15:40:53 +00001101 op->ob_type = (PyTypeObject*)_PyTrash_delete_later;
1102 _PyTrash_delete_later = op;
Guido van Rossumd724b232000-03-13 16:01:29 +00001103}
1104
1105void
Guido van Rossume92e6102000-04-24 15:40:53 +00001106_PyTrash_destroy_chain()
Guido van Rossumd724b232000-03-13 16:01:29 +00001107{
1108 while (_PyTrash_delete_later) {
1109 PyObject *shredder = _PyTrash_delete_later;
Guido van Rossume92e6102000-04-24 15:40:53 +00001110 _PyTrash_delete_later = (PyObject*) shredder->ob_type;
1111
1112 switch (shredder->ob_refcnt) {
1113 case Py_TRASHCAN_TUPLE:
1114 shredder->ob_type = &PyTuple_Type;
1115 break;
1116 case Py_TRASHCAN_LIST:
1117 shredder->ob_type = &PyList_Type;
1118 break;
1119 case Py_TRASHCAN_DICT:
1120 shredder->ob_type = &PyDict_Type;
1121 break;
1122 case Py_TRASHCAN_FRAME:
1123 shredder->ob_type = &PyFrame_Type;
1124 break;
1125 case Py_TRASHCAN_TRACEBACK:
1126 shredder->ob_type = &PyTraceBack_Type;
1127 break;
1128 }
1129 _Py_NewReference(shredder);
1130
Guido van Rossumd724b232000-03-13 16:01:29 +00001131 ++_PyTrash_delete_nesting;
1132 Py_DECREF(shredder);
1133 --_PyTrash_delete_nesting;
1134 }
1135}
Guido van Rossume92e6102000-04-24 15:40:53 +00001136