blob: 61954798c76811b2f05979ade0c81cc380bc4326 [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 }
354 }
355 return inprogress;
356}
357
358static PyObject *
359make_pair(v, w)
360 PyObject *v, *w;
361{
362 PyObject *pair;
363
364 pair = PyTuple_New(2);
365 if (pair == NULL) {
366 return NULL;
367 }
368 if ((long)v <= (long)w) {
369 PyTuple_SET_ITEM(pair, 0, PyLong_FromVoidPtr((void *)v));
370 PyTuple_SET_ITEM(pair, 1, PyLong_FromVoidPtr((void *)w));
371 } else {
372 PyTuple_SET_ITEM(pair, 0, PyLong_FromVoidPtr((void *)w));
373 PyTuple_SET_ITEM(pair, 1, PyLong_FromVoidPtr((void *)v));
374 }
375 return pair;
376}
377
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000378int
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000379PyObject_Compare(v, w)
380 PyObject *v, *w;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000381{
Guido van Rossumcd5a5f61998-06-09 18:58:44 +0000382 PyTypeObject *vtp, *wtp;
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000383 int result;
384
Guido van Rossumc8b6df91997-05-23 00:06:51 +0000385 if (v == NULL || w == NULL) {
386 PyErr_BadInternalCall();
387 return -1;
388 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000389 if (v == w)
390 return 0;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000391 if (PyInstance_Check(v) || PyInstance_Check(w)) {
392 PyObject *res;
Guido van Rossum20566841995-01-12 11:26:10 +0000393 int c;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000394 if (!PyInstance_Check(v))
395 return -PyObject_Compare(w, v);
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000396 if (++_PyCompareState_nesting > NESTING_LIMIT) {
397 PyObject *inprogress, *pair;
398
399 inprogress = get_inprogress_dict();
400 if (inprogress == NULL) {
401 return -1;
402 }
403 pair = make_pair(v, w);
404 if (PyDict_GetItem(inprogress, pair)) {
405 /* already comparing these objects. assume
406 they're equal until shown otherwise */
407 Py_DECREF(pair);
408 --_PyCompareState_nesting;
409 return 0;
410 }
411 if (PyDict_SetItem(inprogress, pair, pair) == -1) {
412 return -1;
413 }
414 res = do_cmp(v, w);
415 _PyCompareState_nesting--;
416 /* XXX DelItem shouldn't fail */
417 PyDict_DelItem(inprogress, pair);
418 Py_DECREF(pair);
419 } else {
420 res = do_cmp(v, w);
421 }
Guido van Rossumc8b6df91997-05-23 00:06:51 +0000422 if (res == NULL)
423 return -1;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000424 if (!PyInt_Check(res)) {
425 Py_DECREF(res);
Guido van Rossumc8b6df91997-05-23 00:06:51 +0000426 PyErr_SetString(PyExc_TypeError,
427 "comparison did not return an int");
428 return -1;
Guido van Rossum20566841995-01-12 11:26:10 +0000429 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000430 c = PyInt_AsLong(res);
431 Py_DECREF(res);
Guido van Rossum20566841995-01-12 11:26:10 +0000432 return (c < 0) ? -1 : (c > 0) ? 1 : 0;
433 }
Guido van Rossumcd5a5f61998-06-09 18:58:44 +0000434 if ((vtp = v->ob_type) != (wtp = w->ob_type)) {
435 char *vname = vtp->tp_name;
436 char *wname = wtp->tp_name;
Guido van Rossumb4db1941998-07-21 21:56:41 +0000437 if (vtp->tp_as_number != NULL && wtp->tp_as_number != NULL) {
Guido van Rossum242c6421997-11-19 16:03:17 +0000438 int err;
439 err = PyNumber_CoerceEx(&v, &w);
440 if (err < 0)
Guido van Rossumc8b6df91997-05-23 00:06:51 +0000441 return -1;
Guido van Rossum242c6421997-11-19 16:03:17 +0000442 else if (err == 0) {
Guido van Rossumb4db1941998-07-21 21:56:41 +0000443 int cmp;
444 vtp = v->ob_type;
445 if (vtp->tp_compare == NULL)
446 cmp = (v < w) ? -1 : 1;
447 else
448 cmp = (*vtp->tp_compare)(v, w);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000449 Py_DECREF(v);
450 Py_DECREF(w);
Guido van Rossum9fb03681991-07-01 18:48:04 +0000451 return cmp;
452 }
453 }
Guido van Rossumb244f692000-04-10 13:42:33 +0000454 else if (PyUnicode_Check(v) || PyUnicode_Check(w)) {
455 int result = PyUnicode_Compare(v, w);
456 if (result == -1 && PyErr_Occurred() &&
457 PyErr_ExceptionMatches(PyExc_TypeError))
458 /* TypeErrors are ignored: if Unicode coercion
459 fails due to one of the arguments not
460 having the right type, we continue as
461 defined by the coercion protocol (see
462 above). Luckily, decoding errors are
463 reported as ValueErrors and are not masked
464 by this technique. */
465 PyErr_Clear();
466 else
467 return result;
468 }
Guido van Rossumcd5a5f61998-06-09 18:58:44 +0000469 else if (vtp->tp_as_number != NULL)
470 vname = "";
471 else if (wtp->tp_as_number != NULL)
472 wname = "";
473 /* Numerical types compare smaller than all other types */
474 return strcmp(vname, wname);
Guido van Rossum9fb03681991-07-01 18:48:04 +0000475 }
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000476 if (vtp->tp_compare == NULL) {
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000477 return (v < w) ? -1 : 1;
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000478 }
479 if (++_PyCompareState_nesting > NESTING_LIMIT
480 && (vtp->tp_as_mapping
481 || (vtp->tp_as_sequence && !PyString_Check(v)))) {
482 PyObject *inprogress, *pair;
483
484 inprogress = get_inprogress_dict();
485 if (inprogress == NULL) {
486 return -1;
487 }
488 pair = make_pair(v, w);
489 if (PyDict_GetItem(inprogress, pair)) {
490 /* already comparing these objects. assume
491 they're equal until shown otherwise */
492 _PyCompareState_nesting--;
493 Py_DECREF(pair);
494 return 0;
495 }
496 if (PyDict_SetItem(inprogress, pair, pair) == -1) {
497 return -1;
498 }
499 result = (*vtp->tp_compare)(v, w);
500 _PyCompareState_nesting--;
501 PyDict_DelItem(inprogress, pair); /* XXX shouldn't fail */
502 Py_DECREF(pair);
503 } else {
504 result = (*vtp->tp_compare)(v, w);
505 }
506 return result;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000507}
508
Guido van Rossum9bfef441993-03-29 10:43:31 +0000509long
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000510PyObject_Hash(v)
511 PyObject *v;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000512{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000513 PyTypeObject *tp = v->ob_type;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000514 if (tp->tp_hash != NULL)
515 return (*tp->tp_hash)(v);
516 if (tp->tp_compare == NULL)
517 return (long) v; /* Use address as hash value */
518 /* If there's a cmp but no hash defined, the object can't be hashed */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000519 PyErr_SetString(PyExc_TypeError, "unhashable type");
Guido van Rossum9bfef441993-03-29 10:43:31 +0000520 return -1;
521}
522
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000523PyObject *
524PyObject_GetAttrString(v, name)
525 PyObject *v;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000526 char *name;
527{
Guido van Rossumd8eb1b31996-08-09 20:52:03 +0000528 if (v->ob_type->tp_getattro != NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000529 PyObject *w, *res;
Guido van Rossumc6d06701997-01-18 07:57:16 +0000530 w = PyString_InternFromString(name);
Guido van Rossumd8eb1b31996-08-09 20:52:03 +0000531 if (w == NULL)
532 return NULL;
533 res = (*v->ob_type->tp_getattro)(v, w);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000534 Py_XDECREF(w);
Guido van Rossumd8eb1b31996-08-09 20:52:03 +0000535 return res;
536 }
537
Guido van Rossum3f5da241990-12-20 15:06:42 +0000538 if (v->ob_type->tp_getattr == NULL) {
Guido van Rossumdb935161998-01-19 22:16:36 +0000539 PyErr_Format(PyExc_AttributeError,
540 "'%.50s' object has no attribute '%.400s'",
541 v->ob_type->tp_name,
542 name);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000543 return NULL;
544 }
545 else {
546 return (*v->ob_type->tp_getattr)(v, name);
547 }
548}
549
550int
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000551PyObject_HasAttrString(v, name)
552 PyObject *v;
Guido van Rossumed18fdc1993-07-11 19:55:34 +0000553 char *name;
554{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000555 PyObject *res = PyObject_GetAttrString(v, name);
Guido van Rossumed18fdc1993-07-11 19:55:34 +0000556 if (res != NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000557 Py_DECREF(res);
Guido van Rossumed18fdc1993-07-11 19:55:34 +0000558 return 1;
559 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000560 PyErr_Clear();
Guido van Rossumed18fdc1993-07-11 19:55:34 +0000561 return 0;
562}
563
564int
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000565PyObject_SetAttrString(v, name, w)
566 PyObject *v;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000567 char *name;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000568 PyObject *w;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000569{
Guido van Rossumd8eb1b31996-08-09 20:52:03 +0000570 if (v->ob_type->tp_setattro != NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000571 PyObject *s;
Guido van Rossumd8eb1b31996-08-09 20:52:03 +0000572 int res;
Guido van Rossumc6d06701997-01-18 07:57:16 +0000573 s = PyString_InternFromString(name);
Guido van Rossumd8eb1b31996-08-09 20:52:03 +0000574 if (s == NULL)
Guido van Rossumb7fc3041996-09-11 22:51:25 +0000575 return -1;
Guido van Rossumd8eb1b31996-08-09 20:52:03 +0000576 res = (*v->ob_type->tp_setattro)(v, s, w);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000577 Py_XDECREF(s);
Guido van Rossumd8eb1b31996-08-09 20:52:03 +0000578 return res;
579 }
580
Guido van Rossum3f5da241990-12-20 15:06:42 +0000581 if (v->ob_type->tp_setattr == NULL) {
582 if (v->ob_type->tp_getattr == NULL)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000583 PyErr_SetString(PyExc_TypeError,
Guido van Rossum3ea74121991-12-24 13:28:03 +0000584 "attribute-less object (assign or del)");
Guido van Rossum3f5da241990-12-20 15:06:42 +0000585 else
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000586 PyErr_SetString(PyExc_TypeError,
Guido van Rossum3ea74121991-12-24 13:28:03 +0000587 "object has read-only attributes");
Guido van Rossum73531a31990-12-20 23:12:40 +0000588 return -1;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000589 }
590 else {
591 return (*v->ob_type->tp_setattr)(v, name, w);
592 }
593}
594
Guido van Rossum98ff96a1997-05-20 18:34:44 +0000595PyObject *
596PyObject_GetAttr(v, name)
597 PyObject *v;
598 PyObject *name;
599{
600 if (v->ob_type->tp_getattro != NULL)
601 return (*v->ob_type->tp_getattro)(v, name);
602 else
603 return PyObject_GetAttrString(v, PyString_AsString(name));
604}
605
606int
607PyObject_HasAttr(v, name)
608 PyObject *v;
609 PyObject *name;
610{
611 PyObject *res = PyObject_GetAttr(v, name);
612 if (res != NULL) {
613 Py_DECREF(res);
614 return 1;
615 }
616 PyErr_Clear();
617 return 0;
618}
619
620int
621PyObject_SetAttr(v, name, value)
622 PyObject *v;
623 PyObject *name;
624 PyObject *value;
625{
626 int err;
627 Py_INCREF(name);
628 PyString_InternInPlace(&name);
629 if (v->ob_type->tp_setattro != NULL)
630 err = (*v->ob_type->tp_setattro)(v, name, value);
631 else
632 err = PyObject_SetAttrString(
633 v, PyString_AsString(name), value);
634 Py_DECREF(name);
635 return err;
636}
637
Guido van Rossum6ac258d1993-05-12 08:24:20 +0000638/* Test a value used as condition, e.g., in a for or if statement.
639 Return -1 if an error occurred */
640
641int
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000642PyObject_IsTrue(v)
643 PyObject *v;
Guido van Rossum6ac258d1993-05-12 08:24:20 +0000644{
645 int res;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000646 if (v == Py_None)
Guido van Rossum6ac258d1993-05-12 08:24:20 +0000647 res = 0;
Guido van Rossum1c4f4581998-05-22 00:53:24 +0000648 else if (v->ob_type->tp_as_number != NULL &&
649 v->ob_type->tp_as_number->nb_nonzero != NULL)
Guido van Rossum6ac258d1993-05-12 08:24:20 +0000650 res = (*v->ob_type->tp_as_number->nb_nonzero)(v);
Guido van Rossum1c4f4581998-05-22 00:53:24 +0000651 else if (v->ob_type->tp_as_mapping != NULL &&
652 v->ob_type->tp_as_mapping->mp_length != NULL)
Guido van Rossum6ac258d1993-05-12 08:24:20 +0000653 res = (*v->ob_type->tp_as_mapping->mp_length)(v);
Guido van Rossum1c4f4581998-05-22 00:53:24 +0000654 else if (v->ob_type->tp_as_sequence != NULL &&
655 v->ob_type->tp_as_sequence->sq_length != NULL)
Guido van Rossum6ac258d1993-05-12 08:24:20 +0000656 res = (*v->ob_type->tp_as_sequence->sq_length)(v);
657 else
658 res = 1;
659 if (res > 0)
660 res = 1;
661 return res;
662}
663
Guido van Rossumc3d3f961998-04-09 17:53:59 +0000664/* equivalent of 'not v'
665 Return -1 if an error occurred */
666
667int
668PyObject_Not(v)
669 PyObject *v;
670{
671 int res;
672 res = PyObject_IsTrue(v);
673 if (res < 0)
674 return res;
675 return res == 0;
676}
677
Guido van Rossum5524a591995-01-10 15:26:20 +0000678/* Coerce two numeric types to the "larger" one.
679 Increment the reference count on each argument.
680 Return -1 and raise an exception if no coercion is possible
681 (and then no reference count is incremented).
682*/
683
684int
Guido van Rossum242c6421997-11-19 16:03:17 +0000685PyNumber_CoerceEx(pv, pw)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000686 PyObject **pv, **pw;
Guido van Rossum5524a591995-01-10 15:26:20 +0000687{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000688 register PyObject *v = *pv;
689 register PyObject *w = *pw;
Guido van Rossum5524a591995-01-10 15:26:20 +0000690 int res;
691
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000692 if (v->ob_type == w->ob_type && !PyInstance_Check(v)) {
693 Py_INCREF(v);
694 Py_INCREF(w);
Guido van Rossum5524a591995-01-10 15:26:20 +0000695 return 0;
696 }
697 if (v->ob_type->tp_as_number && v->ob_type->tp_as_number->nb_coerce) {
698 res = (*v->ob_type->tp_as_number->nb_coerce)(pv, pw);
699 if (res <= 0)
700 return res;
701 }
702 if (w->ob_type->tp_as_number && w->ob_type->tp_as_number->nb_coerce) {
703 res = (*w->ob_type->tp_as_number->nb_coerce)(pw, pv);
704 if (res <= 0)
705 return res;
706 }
Guido van Rossum242c6421997-11-19 16:03:17 +0000707 return 1;
708}
709
710int
711PyNumber_Coerce(pv, pw)
712 PyObject **pv, **pw;
713{
714 int err = PyNumber_CoerceEx(pv, pw);
715 if (err <= 0)
716 return err;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000717 PyErr_SetString(PyExc_TypeError, "number coercion failed");
Guido van Rossum5524a591995-01-10 15:26:20 +0000718 return -1;
719}
720
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000721
Guido van Rossum49b11fe1995-01-26 00:38:22 +0000722/* Test whether an object can be called */
723
724int
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000725PyCallable_Check(x)
726 PyObject *x;
Guido van Rossum49b11fe1995-01-26 00:38:22 +0000727{
728 if (x == NULL)
729 return 0;
730 if (x->ob_type->tp_call != NULL ||
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000731 PyFunction_Check(x) ||
732 PyMethod_Check(x) ||
733 PyCFunction_Check(x) ||
734 PyClass_Check(x))
Guido van Rossum49b11fe1995-01-26 00:38:22 +0000735 return 1;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000736 if (PyInstance_Check(x)) {
737 PyObject *call = PyObject_GetAttrString(x, "__call__");
Guido van Rossum49b11fe1995-01-26 00:38:22 +0000738 if (call == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000739 PyErr_Clear();
Guido van Rossum49b11fe1995-01-26 00:38:22 +0000740 return 0;
741 }
742 /* Could test recursively but don't, for fear of endless
743 recursion if some joker sets self.__call__ = self */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000744 Py_DECREF(call);
Guido van Rossum49b11fe1995-01-26 00:38:22 +0000745 return 1;
746 }
747 return 0;
748}
749
750
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000751/*
752NoObject is usable as a non-NULL undefined value, used by the macro None.
753There is (and should be!) no way to create other objects of this type,
Guido van Rossum3f5da241990-12-20 15:06:42 +0000754so there is exactly one (which is indestructible, by the way).
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000755*/
756
Guido van Rossum0c182a11992-03-27 17:26:13 +0000757/* ARGSUSED */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000758static PyObject *
Guido van Rossum3f5da241990-12-20 15:06:42 +0000759none_repr(op)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000760 PyObject *op;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000761{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000762 return PyString_FromString("None");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000763}
764
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000765static PyTypeObject PyNothing_Type = {
766 PyObject_HEAD_INIT(&PyType_Type)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000767 0,
Guido van Rossum3f5da241990-12-20 15:06:42 +0000768 "None",
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000769 0,
770 0,
771 0, /*tp_dealloc*/ /*never called*/
Guido van Rossum7066dd71992-09-17 17:54:56 +0000772 0, /*tp_print*/
Guido van Rossum3f5da241990-12-20 15:06:42 +0000773 0, /*tp_getattr*/
774 0, /*tp_setattr*/
775 0, /*tp_compare*/
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000776 (reprfunc)none_repr, /*tp_repr*/
Guido van Rossum3f5da241990-12-20 15:06:42 +0000777 0, /*tp_as_number*/
778 0, /*tp_as_sequence*/
779 0, /*tp_as_mapping*/
Guido van Rossum9bfef441993-03-29 10:43:31 +0000780 0, /*tp_hash */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000781};
782
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000783PyObject _Py_NoneStruct = {
784 PyObject_HEAD_INIT(&PyNothing_Type)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000785};
786
787
Guido van Rossum84a90321996-05-22 16:34:47 +0000788#ifdef Py_TRACE_REFS
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000789
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000790static PyObject refchain = {&refchain, &refchain};
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000791
Guido van Rossumaacdc9d1996-08-12 21:32:12 +0000792void
Guido van Rossume09fb551997-08-05 02:04:34 +0000793_Py_ResetReferences()
794{
795 refchain._ob_prev = refchain._ob_next = &refchain;
796 _Py_RefTotal = 0;
797}
798
799void
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000800_Py_NewReference(op)
801 PyObject *op;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000802{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000803 _Py_RefTotal++;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000804 op->ob_refcnt = 1;
805 op->_ob_next = refchain._ob_next;
806 op->_ob_prev = &refchain;
807 refchain._ob_next->_ob_prev = op;
808 refchain._ob_next = op;
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +0000809#ifdef COUNT_ALLOCS
810 inc_count(op->ob_type);
811#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000812}
813
Guido van Rossumaacdc9d1996-08-12 21:32:12 +0000814void
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000815_Py_ForgetReference(op)
816 register PyObject *op;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000817{
Guido van Rossumbffd6832000-01-20 22:32:56 +0000818#ifdef SLOW_UNREF_CHECK
Guido van Rossum4c08d552000-03-10 22:55:18 +0000819 register PyObject *p;
Guido van Rossumbffd6832000-01-20 22:32:56 +0000820#endif
Guido van Rossumd7047b31995-01-02 19:07:15 +0000821 if (op->ob_refcnt < 0)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000822 Py_FatalError("UNREF negative refcnt");
Guido van Rossum2e8f6141992-09-03 20:32:55 +0000823 if (op == &refchain ||
Guido van Rossumd7047b31995-01-02 19:07:15 +0000824 op->_ob_prev->_ob_next != op || op->_ob_next->_ob_prev != op)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000825 Py_FatalError("UNREF invalid object");
Guido van Rossum2e8f6141992-09-03 20:32:55 +0000826#ifdef SLOW_UNREF_CHECK
Guido van Rossum3f5da241990-12-20 15:06:42 +0000827 for (p = refchain._ob_next; p != &refchain; p = p->_ob_next) {
828 if (p == op)
829 break;
830 }
Guido van Rossumd7047b31995-01-02 19:07:15 +0000831 if (p == &refchain) /* Not found */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000832 Py_FatalError("UNREF unknown object");
Guido van Rossum2e8f6141992-09-03 20:32:55 +0000833#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000834 op->_ob_next->_ob_prev = op->_ob_prev;
835 op->_ob_prev->_ob_next = op->_ob_next;
Guido van Rossum2e8f6141992-09-03 20:32:55 +0000836 op->_ob_next = op->_ob_prev = NULL;
Guido van Rossumd8953cb1995-04-06 14:46:26 +0000837#ifdef COUNT_ALLOCS
838 op->ob_type->tp_free++;
839#endif
Guido van Rossum3f5da241990-12-20 15:06:42 +0000840}
841
Guido van Rossumaacdc9d1996-08-12 21:32:12 +0000842void
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000843_Py_Dealloc(op)
844 PyObject *op;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000845{
Guido van Rossum9776adf1994-09-07 14:36:45 +0000846 destructor dealloc = op->ob_type->tp_dealloc;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000847 _Py_ForgetReference(op);
Guido van Rossume92e6102000-04-24 15:40:53 +0000848 if (_PyTrash_delete_nesting < PyTrash_UNWIND_LEVEL-1)
849 op->ob_type = NULL;
Guido van Rossum9776adf1994-09-07 14:36:45 +0000850 (*dealloc)(op);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000851}
852
Guido van Rossumaacdc9d1996-08-12 21:32:12 +0000853void
Guido van Rossumded690f1996-05-24 20:48:31 +0000854_Py_PrintReferences(fp)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000855 FILE *fp;
856{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000857 PyObject *op;
Guido van Rossume09fb551997-08-05 02:04:34 +0000858 fprintf(fp, "Remaining objects:\n");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000859 for (op = refchain._ob_next; op != &refchain; op = op->_ob_next) {
860 fprintf(fp, "[%d] ", op->ob_refcnt);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000861 if (PyObject_Print(op, fp, 0) != 0)
862 PyErr_Clear();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000863 putc('\n', fp);
864 }
865}
866
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000867PyObject *
Guido van Rossumded690f1996-05-24 20:48:31 +0000868_Py_GetObjects(self, args)
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000869 PyObject *self;
870 PyObject *args;
871{
872 int i, n;
873 PyObject *t = NULL;
874 PyObject *res, *op;
875
876 if (!PyArg_ParseTuple(args, "i|O", &n, &t))
877 return NULL;
878 op = refchain._ob_next;
879 res = PyList_New(0);
880 if (res == NULL)
881 return NULL;
882 for (i = 0; (n == 0 || i < n) && op != &refchain; i++) {
883 while (op == self || op == args || op == res || op == t ||
884 t != NULL && op->ob_type != (PyTypeObject *) t) {
885 op = op->_ob_next;
886 if (op == &refchain)
887 return res;
888 }
889 if (PyList_Append(res, op) < 0) {
890 Py_DECREF(res);
891 return NULL;
892 }
893 op = op->_ob_next;
894 }
895 return res;
896}
897
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000898#endif
Guido van Rossum97ead3f1996-01-12 01:24:09 +0000899
900
901/* Hack to force loading of cobject.o */
Guido van Rossumda9c2711996-12-05 21:58:58 +0000902PyTypeObject *_Py_cobject_hack = &PyCObject_Type;
Guido van Rossum84a90321996-05-22 16:34:47 +0000903
904
905/* Hack to force loading of abstract.o */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000906int (*_Py_abstract_hack) Py_FPROTO((PyObject *)) = &PyObject_Length;
Guido van Rossume09fb551997-08-05 02:04:34 +0000907
908
Guido van Rossumb18618d2000-05-03 23:44:39 +0000909/* Python's malloc wrappers (see mymalloc.h) */
Guido van Rossume09fb551997-08-05 02:04:34 +0000910
911ANY *
912PyMem_Malloc(nbytes)
913 size_t nbytes;
914{
915#if _PyMem_EXTRA > 0
916 if (nbytes == 0)
917 nbytes = _PyMem_EXTRA;
918#endif
Guido van Rossumb18618d2000-05-03 23:44:39 +0000919 return PyMem_MALLOC(nbytes);
Guido van Rossume09fb551997-08-05 02:04:34 +0000920}
921
922ANY *
923PyMem_Realloc(p, nbytes)
924 ANY *p;
925 size_t nbytes;
926{
927#if _PyMem_EXTRA > 0
928 if (nbytes == 0)
929 nbytes = _PyMem_EXTRA;
930#endif
Guido van Rossumb18618d2000-05-03 23:44:39 +0000931 return PyMem_REALLOC(p, nbytes);
Guido van Rossume09fb551997-08-05 02:04:34 +0000932}
933
934void
935PyMem_Free(p)
936 ANY *p;
937{
Guido van Rossumb18618d2000-05-03 23:44:39 +0000938 PyMem_FREE(p);
939}
940
941
942/* Python's object malloc wrappers (see objimpl.h) */
943
944ANY *
945PyObject_Malloc(nbytes)
946 size_t nbytes;
947{
948 return PyObject_MALLOC(nbytes);
949}
950
951ANY *
952PyObject_Realloc(p, nbytes)
953 ANY *p;
954 size_t nbytes;
955{
956 return PyObject_REALLOC(p, nbytes);
957}
958
959void
960PyObject_Free(p)
961 ANY *p;
962{
963 PyObject_FREE(p);
Guido van Rossume09fb551997-08-05 02:04:34 +0000964}
Guido van Rossum86610361998-04-10 22:32:46 +0000965
966
967/* These methods are used to control infinite recursion in repr, str, print,
968 etc. Container objects that may recursively contain themselves,
969 e.g. builtin dictionaries and lists, should used Py_ReprEnter() and
970 Py_ReprLeave() to avoid infinite recursion.
971
972 Py_ReprEnter() returns 0 the first time it is called for a particular
973 object and 1 every time thereafter. It returns -1 if an exception
974 occurred. Py_ReprLeave() has no return value.
975
976 See dictobject.c and listobject.c for examples of use.
977*/
978
979#define KEY "Py_Repr"
980
981int
982Py_ReprEnter(obj)
983 PyObject *obj;
984{
985 PyObject *dict;
986 PyObject *list;
987 int i;
988
989 dict = PyThreadState_GetDict();
990 if (dict == NULL)
991 return -1;
992 list = PyDict_GetItemString(dict, KEY);
993 if (list == NULL) {
994 list = PyList_New(0);
995 if (list == NULL)
996 return -1;
997 if (PyDict_SetItemString(dict, KEY, list) < 0)
998 return -1;
999 Py_DECREF(list);
1000 }
1001 i = PyList_GET_SIZE(list);
1002 while (--i >= 0) {
1003 if (PyList_GET_ITEM(list, i) == obj)
1004 return 1;
1005 }
1006 PyList_Append(list, obj);
1007 return 0;
1008}
1009
1010void
1011Py_ReprLeave(obj)
1012 PyObject *obj;
1013{
1014 PyObject *dict;
1015 PyObject *list;
1016 int i;
1017
1018 dict = PyThreadState_GetDict();
Guido van Rossumeb909461998-04-11 15:17:34 +00001019 if (dict == NULL)
1020 return;
Guido van Rossum86610361998-04-10 22:32:46 +00001021 list = PyDict_GetItemString(dict, KEY);
Guido van Rossumeb909461998-04-11 15:17:34 +00001022 if (list == NULL || !PyList_Check(list))
1023 return;
Guido van Rossum86610361998-04-10 22:32:46 +00001024 i = PyList_GET_SIZE(list);
1025 /* Count backwards because we always expect obj to be list[-1] */
1026 while (--i >= 0) {
1027 if (PyList_GET_ITEM(list, i) == obj) {
1028 PyList_SetSlice(list, i, i + 1, NULL);
1029 break;
1030 }
1031 }
1032}
Guido van Rossumd724b232000-03-13 16:01:29 +00001033
1034/*
1035 trashcan
1036 CT 2k0130
1037 non-recursively destroy nested objects
1038
1039 CT 2k0223
1040 everything is now done in a macro.
1041
1042 CT 2k0305
1043 modified to use functions, after Tim Peter's suggestion.
1044
1045 CT 2k0309
1046 modified to restore a possible error.
Guido van Rossum13ff8eb2000-03-25 18:39:19 +00001047
1048 CT 2k0325
1049 added better safe than sorry check for threadstate
Guido van Rossume92e6102000-04-24 15:40:53 +00001050
1051 CT 2k0422
1052 complete rewrite. We now build a chain via ob_type
1053 and save the limited number of types in ob_refcnt.
1054 This is perfect since we don't need any memory.
1055 A patch for free-threading would need just a lock.
Guido van Rossumd724b232000-03-13 16:01:29 +00001056*/
1057
Guido van Rossume92e6102000-04-24 15:40:53 +00001058#define Py_TRASHCAN_TUPLE 1
1059#define Py_TRASHCAN_LIST 2
1060#define Py_TRASHCAN_DICT 3
1061#define Py_TRASHCAN_FRAME 4
1062#define Py_TRASHCAN_TRACEBACK 5
1063/* extend here if other objects want protection */
1064
Guido van Rossumd724b232000-03-13 16:01:29 +00001065int _PyTrash_delete_nesting = 0;
Guido van Rossume92e6102000-04-24 15:40:53 +00001066
Guido van Rossumd724b232000-03-13 16:01:29 +00001067PyObject * _PyTrash_delete_later = NULL;
1068
1069void
1070_PyTrash_deposit_object(op)
1071 PyObject *op;
1072{
Guido van Rossume92e6102000-04-24 15:40:53 +00001073 int typecode;
1074 PyObject *hold = _PyTrash_delete_later;
Guido van Rossum13ff8eb2000-03-25 18:39:19 +00001075
Guido van Rossume92e6102000-04-24 15:40:53 +00001076 if (PyTuple_Check(op))
1077 typecode = Py_TRASHCAN_TUPLE;
1078 else if (PyList_Check(op))
1079 typecode = Py_TRASHCAN_LIST;
1080 else if (PyDict_Check(op))
1081 typecode = Py_TRASHCAN_DICT;
1082 else if (PyFrame_Check(op))
1083 typecode = Py_TRASHCAN_FRAME;
1084 else if (PyTraceBack_Check(op))
1085 typecode = Py_TRASHCAN_TRACEBACK;
1086 op->ob_refcnt = typecode;
Guido van Rossumd724b232000-03-13 16:01:29 +00001087
Guido van Rossume92e6102000-04-24 15:40:53 +00001088 op->ob_type = (PyTypeObject*)_PyTrash_delete_later;
1089 _PyTrash_delete_later = op;
Guido van Rossumd724b232000-03-13 16:01:29 +00001090}
1091
1092void
Guido van Rossume92e6102000-04-24 15:40:53 +00001093_PyTrash_destroy_chain()
Guido van Rossumd724b232000-03-13 16:01:29 +00001094{
1095 while (_PyTrash_delete_later) {
1096 PyObject *shredder = _PyTrash_delete_later;
Guido van Rossume92e6102000-04-24 15:40:53 +00001097 _PyTrash_delete_later = (PyObject*) shredder->ob_type;
1098
1099 switch (shredder->ob_refcnt) {
1100 case Py_TRASHCAN_TUPLE:
1101 shredder->ob_type = &PyTuple_Type;
1102 break;
1103 case Py_TRASHCAN_LIST:
1104 shredder->ob_type = &PyList_Type;
1105 break;
1106 case Py_TRASHCAN_DICT:
1107 shredder->ob_type = &PyDict_Type;
1108 break;
1109 case Py_TRASHCAN_FRAME:
1110 shredder->ob_type = &PyFrame_Type;
1111 break;
1112 case Py_TRASHCAN_TRACEBACK:
1113 shredder->ob_type = &PyTraceBack_Type;
1114 break;
1115 }
1116 _Py_NewReference(shredder);
1117
Guido van Rossumd724b232000-03-13 16:01:29 +00001118 ++_PyTrash_delete_nesting;
1119 Py_DECREF(shredder);
1120 --_PyTrash_delete_nesting;
1121 }
1122}
Guido van Rossume92e6102000-04-24 15:40:53 +00001123