blob: ab9d4ed41df807ad27f49afb31ef198666b30e43 [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);
603 else
604 return PyObject_GetAttrString(v, PyString_AsString(name));
605}
606
607int
608PyObject_HasAttr(v, name)
609 PyObject *v;
610 PyObject *name;
611{
612 PyObject *res = PyObject_GetAttr(v, name);
613 if (res != NULL) {
614 Py_DECREF(res);
615 return 1;
616 }
617 PyErr_Clear();
618 return 0;
619}
620
621int
622PyObject_SetAttr(v, name, value)
623 PyObject *v;
624 PyObject *name;
625 PyObject *value;
626{
627 int err;
628 Py_INCREF(name);
629 PyString_InternInPlace(&name);
630 if (v->ob_type->tp_setattro != NULL)
631 err = (*v->ob_type->tp_setattro)(v, name, value);
632 else
633 err = PyObject_SetAttrString(
634 v, PyString_AsString(name), value);
635 Py_DECREF(name);
636 return err;
637}
638
Guido van Rossum6ac258d1993-05-12 08:24:20 +0000639/* Test a value used as condition, e.g., in a for or if statement.
640 Return -1 if an error occurred */
641
642int
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000643PyObject_IsTrue(v)
644 PyObject *v;
Guido van Rossum6ac258d1993-05-12 08:24:20 +0000645{
646 int res;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000647 if (v == Py_None)
Guido van Rossum6ac258d1993-05-12 08:24:20 +0000648 res = 0;
Guido van Rossum1c4f4581998-05-22 00:53:24 +0000649 else if (v->ob_type->tp_as_number != NULL &&
650 v->ob_type->tp_as_number->nb_nonzero != NULL)
Guido van Rossum6ac258d1993-05-12 08:24:20 +0000651 res = (*v->ob_type->tp_as_number->nb_nonzero)(v);
Guido van Rossum1c4f4581998-05-22 00:53:24 +0000652 else if (v->ob_type->tp_as_mapping != NULL &&
653 v->ob_type->tp_as_mapping->mp_length != NULL)
Guido van Rossum6ac258d1993-05-12 08:24:20 +0000654 res = (*v->ob_type->tp_as_mapping->mp_length)(v);
Guido van Rossum1c4f4581998-05-22 00:53:24 +0000655 else if (v->ob_type->tp_as_sequence != NULL &&
656 v->ob_type->tp_as_sequence->sq_length != NULL)
Guido van Rossum6ac258d1993-05-12 08:24:20 +0000657 res = (*v->ob_type->tp_as_sequence->sq_length)(v);
658 else
659 res = 1;
660 if (res > 0)
661 res = 1;
662 return res;
663}
664
Guido van Rossumc3d3f961998-04-09 17:53:59 +0000665/* equivalent of 'not v'
666 Return -1 if an error occurred */
667
668int
669PyObject_Not(v)
670 PyObject *v;
671{
672 int res;
673 res = PyObject_IsTrue(v);
674 if (res < 0)
675 return res;
676 return res == 0;
677}
678
Guido van Rossum5524a591995-01-10 15:26:20 +0000679/* Coerce two numeric types to the "larger" one.
680 Increment the reference count on each argument.
681 Return -1 and raise an exception if no coercion is possible
682 (and then no reference count is incremented).
683*/
684
685int
Guido van Rossum242c6421997-11-19 16:03:17 +0000686PyNumber_CoerceEx(pv, pw)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000687 PyObject **pv, **pw;
Guido van Rossum5524a591995-01-10 15:26:20 +0000688{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000689 register PyObject *v = *pv;
690 register PyObject *w = *pw;
Guido van Rossum5524a591995-01-10 15:26:20 +0000691 int res;
692
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000693 if (v->ob_type == w->ob_type && !PyInstance_Check(v)) {
694 Py_INCREF(v);
695 Py_INCREF(w);
Guido van Rossum5524a591995-01-10 15:26:20 +0000696 return 0;
697 }
698 if (v->ob_type->tp_as_number && v->ob_type->tp_as_number->nb_coerce) {
699 res = (*v->ob_type->tp_as_number->nb_coerce)(pv, pw);
700 if (res <= 0)
701 return res;
702 }
703 if (w->ob_type->tp_as_number && w->ob_type->tp_as_number->nb_coerce) {
704 res = (*w->ob_type->tp_as_number->nb_coerce)(pw, pv);
705 if (res <= 0)
706 return res;
707 }
Guido van Rossum242c6421997-11-19 16:03:17 +0000708 return 1;
709}
710
711int
712PyNumber_Coerce(pv, pw)
713 PyObject **pv, **pw;
714{
715 int err = PyNumber_CoerceEx(pv, pw);
716 if (err <= 0)
717 return err;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000718 PyErr_SetString(PyExc_TypeError, "number coercion failed");
Guido van Rossum5524a591995-01-10 15:26:20 +0000719 return -1;
720}
721
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000722
Guido van Rossum49b11fe1995-01-26 00:38:22 +0000723/* Test whether an object can be called */
724
725int
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000726PyCallable_Check(x)
727 PyObject *x;
Guido van Rossum49b11fe1995-01-26 00:38:22 +0000728{
729 if (x == NULL)
730 return 0;
731 if (x->ob_type->tp_call != NULL ||
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000732 PyFunction_Check(x) ||
733 PyMethod_Check(x) ||
734 PyCFunction_Check(x) ||
735 PyClass_Check(x))
Guido van Rossum49b11fe1995-01-26 00:38:22 +0000736 return 1;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000737 if (PyInstance_Check(x)) {
738 PyObject *call = PyObject_GetAttrString(x, "__call__");
Guido van Rossum49b11fe1995-01-26 00:38:22 +0000739 if (call == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000740 PyErr_Clear();
Guido van Rossum49b11fe1995-01-26 00:38:22 +0000741 return 0;
742 }
743 /* Could test recursively but don't, for fear of endless
744 recursion if some joker sets self.__call__ = self */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000745 Py_DECREF(call);
Guido van Rossum49b11fe1995-01-26 00:38:22 +0000746 return 1;
747 }
748 return 0;
749}
750
751
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000752/*
753NoObject is usable as a non-NULL undefined value, used by the macro None.
754There is (and should be!) no way to create other objects of this type,
Guido van Rossum3f5da241990-12-20 15:06:42 +0000755so there is exactly one (which is indestructible, by the way).
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000756*/
757
Guido van Rossum0c182a11992-03-27 17:26:13 +0000758/* ARGSUSED */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000759static PyObject *
Guido van Rossum3f5da241990-12-20 15:06:42 +0000760none_repr(op)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000761 PyObject *op;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000762{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000763 return PyString_FromString("None");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000764}
765
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000766static PyTypeObject PyNothing_Type = {
767 PyObject_HEAD_INIT(&PyType_Type)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000768 0,
Guido van Rossum3f5da241990-12-20 15:06:42 +0000769 "None",
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000770 0,
771 0,
772 0, /*tp_dealloc*/ /*never called*/
Guido van Rossum7066dd71992-09-17 17:54:56 +0000773 0, /*tp_print*/
Guido van Rossum3f5da241990-12-20 15:06:42 +0000774 0, /*tp_getattr*/
775 0, /*tp_setattr*/
776 0, /*tp_compare*/
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000777 (reprfunc)none_repr, /*tp_repr*/
Guido van Rossum3f5da241990-12-20 15:06:42 +0000778 0, /*tp_as_number*/
779 0, /*tp_as_sequence*/
780 0, /*tp_as_mapping*/
Guido van Rossum9bfef441993-03-29 10:43:31 +0000781 0, /*tp_hash */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000782};
783
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000784PyObject _Py_NoneStruct = {
785 PyObject_HEAD_INIT(&PyNothing_Type)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000786};
787
788
Guido van Rossum84a90321996-05-22 16:34:47 +0000789#ifdef Py_TRACE_REFS
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000790
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000791static PyObject refchain = {&refchain, &refchain};
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000792
Guido van Rossumaacdc9d1996-08-12 21:32:12 +0000793void
Guido van Rossume09fb551997-08-05 02:04:34 +0000794_Py_ResetReferences()
795{
796 refchain._ob_prev = refchain._ob_next = &refchain;
797 _Py_RefTotal = 0;
798}
799
800void
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000801_Py_NewReference(op)
802 PyObject *op;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000803{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000804 _Py_RefTotal++;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000805 op->ob_refcnt = 1;
806 op->_ob_next = refchain._ob_next;
807 op->_ob_prev = &refchain;
808 refchain._ob_next->_ob_prev = op;
809 refchain._ob_next = op;
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +0000810#ifdef COUNT_ALLOCS
811 inc_count(op->ob_type);
812#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000813}
814
Guido van Rossumaacdc9d1996-08-12 21:32:12 +0000815void
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000816_Py_ForgetReference(op)
817 register PyObject *op;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000818{
Guido van Rossumbffd6832000-01-20 22:32:56 +0000819#ifdef SLOW_UNREF_CHECK
Guido van Rossum4c08d552000-03-10 22:55:18 +0000820 register PyObject *p;
Guido van Rossumbffd6832000-01-20 22:32:56 +0000821#endif
Guido van Rossumd7047b31995-01-02 19:07:15 +0000822 if (op->ob_refcnt < 0)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000823 Py_FatalError("UNREF negative refcnt");
Guido van Rossum2e8f6141992-09-03 20:32:55 +0000824 if (op == &refchain ||
Guido van Rossumd7047b31995-01-02 19:07:15 +0000825 op->_ob_prev->_ob_next != op || op->_ob_next->_ob_prev != op)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000826 Py_FatalError("UNREF invalid object");
Guido van Rossum2e8f6141992-09-03 20:32:55 +0000827#ifdef SLOW_UNREF_CHECK
Guido van Rossum3f5da241990-12-20 15:06:42 +0000828 for (p = refchain._ob_next; p != &refchain; p = p->_ob_next) {
829 if (p == op)
830 break;
831 }
Guido van Rossumd7047b31995-01-02 19:07:15 +0000832 if (p == &refchain) /* Not found */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000833 Py_FatalError("UNREF unknown object");
Guido van Rossum2e8f6141992-09-03 20:32:55 +0000834#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000835 op->_ob_next->_ob_prev = op->_ob_prev;
836 op->_ob_prev->_ob_next = op->_ob_next;
Guido van Rossum2e8f6141992-09-03 20:32:55 +0000837 op->_ob_next = op->_ob_prev = NULL;
Guido van Rossumd8953cb1995-04-06 14:46:26 +0000838#ifdef COUNT_ALLOCS
839 op->ob_type->tp_free++;
840#endif
Guido van Rossum3f5da241990-12-20 15:06:42 +0000841}
842
Guido van Rossumaacdc9d1996-08-12 21:32:12 +0000843void
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000844_Py_Dealloc(op)
845 PyObject *op;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000846{
Guido van Rossum9776adf1994-09-07 14:36:45 +0000847 destructor dealloc = op->ob_type->tp_dealloc;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000848 _Py_ForgetReference(op);
Guido van Rossume92e6102000-04-24 15:40:53 +0000849 if (_PyTrash_delete_nesting < PyTrash_UNWIND_LEVEL-1)
850 op->ob_type = NULL;
Guido van Rossum9776adf1994-09-07 14:36:45 +0000851 (*dealloc)(op);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000852}
853
Guido van Rossumaacdc9d1996-08-12 21:32:12 +0000854void
Guido van Rossumded690f1996-05-24 20:48:31 +0000855_Py_PrintReferences(fp)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000856 FILE *fp;
857{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000858 PyObject *op;
Guido van Rossume09fb551997-08-05 02:04:34 +0000859 fprintf(fp, "Remaining objects:\n");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000860 for (op = refchain._ob_next; op != &refchain; op = op->_ob_next) {
861 fprintf(fp, "[%d] ", op->ob_refcnt);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000862 if (PyObject_Print(op, fp, 0) != 0)
863 PyErr_Clear();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000864 putc('\n', fp);
865 }
866}
867
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000868PyObject *
Guido van Rossumded690f1996-05-24 20:48:31 +0000869_Py_GetObjects(self, args)
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000870 PyObject *self;
871 PyObject *args;
872{
873 int i, n;
874 PyObject *t = NULL;
875 PyObject *res, *op;
876
877 if (!PyArg_ParseTuple(args, "i|O", &n, &t))
878 return NULL;
879 op = refchain._ob_next;
880 res = PyList_New(0);
881 if (res == NULL)
882 return NULL;
883 for (i = 0; (n == 0 || i < n) && op != &refchain; i++) {
884 while (op == self || op == args || op == res || op == t ||
885 t != NULL && op->ob_type != (PyTypeObject *) t) {
886 op = op->_ob_next;
887 if (op == &refchain)
888 return res;
889 }
890 if (PyList_Append(res, op) < 0) {
891 Py_DECREF(res);
892 return NULL;
893 }
894 op = op->_ob_next;
895 }
896 return res;
897}
898
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000899#endif
Guido van Rossum97ead3f1996-01-12 01:24:09 +0000900
901
902/* Hack to force loading of cobject.o */
Guido van Rossumda9c2711996-12-05 21:58:58 +0000903PyTypeObject *_Py_cobject_hack = &PyCObject_Type;
Guido van Rossum84a90321996-05-22 16:34:47 +0000904
905
906/* Hack to force loading of abstract.o */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000907int (*_Py_abstract_hack) Py_FPROTO((PyObject *)) = &PyObject_Length;
Guido van Rossume09fb551997-08-05 02:04:34 +0000908
909
Guido van Rossumb18618d2000-05-03 23:44:39 +0000910/* Python's malloc wrappers (see mymalloc.h) */
Guido van Rossume09fb551997-08-05 02:04:34 +0000911
912ANY *
913PyMem_Malloc(nbytes)
914 size_t nbytes;
915{
916#if _PyMem_EXTRA > 0
917 if (nbytes == 0)
918 nbytes = _PyMem_EXTRA;
919#endif
Guido van Rossumb18618d2000-05-03 23:44:39 +0000920 return PyMem_MALLOC(nbytes);
Guido van Rossume09fb551997-08-05 02:04:34 +0000921}
922
923ANY *
924PyMem_Realloc(p, nbytes)
925 ANY *p;
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_REALLOC(p, nbytes);
Guido van Rossume09fb551997-08-05 02:04:34 +0000933}
934
935void
936PyMem_Free(p)
937 ANY *p;
938{
Guido van Rossumb18618d2000-05-03 23:44:39 +0000939 PyMem_FREE(p);
940}
941
942
943/* Python's object malloc wrappers (see objimpl.h) */
944
945ANY *
946PyObject_Malloc(nbytes)
947 size_t nbytes;
948{
949 return PyObject_MALLOC(nbytes);
950}
951
952ANY *
953PyObject_Realloc(p, nbytes)
954 ANY *p;
955 size_t nbytes;
956{
957 return PyObject_REALLOC(p, nbytes);
958}
959
960void
961PyObject_Free(p)
962 ANY *p;
963{
964 PyObject_FREE(p);
Guido van Rossume09fb551997-08-05 02:04:34 +0000965}
Guido van Rossum86610361998-04-10 22:32:46 +0000966
967
968/* These methods are used to control infinite recursion in repr, str, print,
969 etc. Container objects that may recursively contain themselves,
970 e.g. builtin dictionaries and lists, should used Py_ReprEnter() and
971 Py_ReprLeave() to avoid infinite recursion.
972
973 Py_ReprEnter() returns 0 the first time it is called for a particular
974 object and 1 every time thereafter. It returns -1 if an exception
975 occurred. Py_ReprLeave() has no return value.
976
977 See dictobject.c and listobject.c for examples of use.
978*/
979
980#define KEY "Py_Repr"
981
982int
983Py_ReprEnter(obj)
984 PyObject *obj;
985{
986 PyObject *dict;
987 PyObject *list;
988 int i;
989
990 dict = PyThreadState_GetDict();
991 if (dict == NULL)
992 return -1;
993 list = PyDict_GetItemString(dict, KEY);
994 if (list == NULL) {
995 list = PyList_New(0);
996 if (list == NULL)
997 return -1;
998 if (PyDict_SetItemString(dict, KEY, list) < 0)
999 return -1;
1000 Py_DECREF(list);
1001 }
1002 i = PyList_GET_SIZE(list);
1003 while (--i >= 0) {
1004 if (PyList_GET_ITEM(list, i) == obj)
1005 return 1;
1006 }
1007 PyList_Append(list, obj);
1008 return 0;
1009}
1010
1011void
1012Py_ReprLeave(obj)
1013 PyObject *obj;
1014{
1015 PyObject *dict;
1016 PyObject *list;
1017 int i;
1018
1019 dict = PyThreadState_GetDict();
Guido van Rossumeb909461998-04-11 15:17:34 +00001020 if (dict == NULL)
1021 return;
Guido van Rossum86610361998-04-10 22:32:46 +00001022 list = PyDict_GetItemString(dict, KEY);
Guido van Rossumeb909461998-04-11 15:17:34 +00001023 if (list == NULL || !PyList_Check(list))
1024 return;
Guido van Rossum86610361998-04-10 22:32:46 +00001025 i = PyList_GET_SIZE(list);
1026 /* Count backwards because we always expect obj to be list[-1] */
1027 while (--i >= 0) {
1028 if (PyList_GET_ITEM(list, i) == obj) {
1029 PyList_SetSlice(list, i, i + 1, NULL);
1030 break;
1031 }
1032 }
1033}
Guido van Rossumd724b232000-03-13 16:01:29 +00001034
1035/*
1036 trashcan
1037 CT 2k0130
1038 non-recursively destroy nested objects
1039
1040 CT 2k0223
1041 everything is now done in a macro.
1042
1043 CT 2k0305
1044 modified to use functions, after Tim Peter's suggestion.
1045
1046 CT 2k0309
1047 modified to restore a possible error.
Guido van Rossum13ff8eb2000-03-25 18:39:19 +00001048
1049 CT 2k0325
1050 added better safe than sorry check for threadstate
Guido van Rossume92e6102000-04-24 15:40:53 +00001051
1052 CT 2k0422
1053 complete rewrite. We now build a chain via ob_type
1054 and save the limited number of types in ob_refcnt.
1055 This is perfect since we don't need any memory.
1056 A patch for free-threading would need just a lock.
Guido van Rossumd724b232000-03-13 16:01:29 +00001057*/
1058
Guido van Rossume92e6102000-04-24 15:40:53 +00001059#define Py_TRASHCAN_TUPLE 1
1060#define Py_TRASHCAN_LIST 2
1061#define Py_TRASHCAN_DICT 3
1062#define Py_TRASHCAN_FRAME 4
1063#define Py_TRASHCAN_TRACEBACK 5
1064/* extend here if other objects want protection */
1065
Guido van Rossumd724b232000-03-13 16:01:29 +00001066int _PyTrash_delete_nesting = 0;
Guido van Rossume92e6102000-04-24 15:40:53 +00001067
Guido van Rossumd724b232000-03-13 16:01:29 +00001068PyObject * _PyTrash_delete_later = NULL;
1069
1070void
1071_PyTrash_deposit_object(op)
1072 PyObject *op;
1073{
Guido van Rossume92e6102000-04-24 15:40:53 +00001074 int typecode;
1075 PyObject *hold = _PyTrash_delete_later;
Guido van Rossum13ff8eb2000-03-25 18:39:19 +00001076
Guido van Rossume92e6102000-04-24 15:40:53 +00001077 if (PyTuple_Check(op))
1078 typecode = Py_TRASHCAN_TUPLE;
1079 else if (PyList_Check(op))
1080 typecode = Py_TRASHCAN_LIST;
1081 else if (PyDict_Check(op))
1082 typecode = Py_TRASHCAN_DICT;
1083 else if (PyFrame_Check(op))
1084 typecode = Py_TRASHCAN_FRAME;
1085 else if (PyTraceBack_Check(op))
1086 typecode = Py_TRASHCAN_TRACEBACK;
1087 op->ob_refcnt = typecode;
Guido van Rossumd724b232000-03-13 16:01:29 +00001088
Guido van Rossume92e6102000-04-24 15:40:53 +00001089 op->ob_type = (PyTypeObject*)_PyTrash_delete_later;
1090 _PyTrash_delete_later = op;
Guido van Rossumd724b232000-03-13 16:01:29 +00001091}
1092
1093void
Guido van Rossume92e6102000-04-24 15:40:53 +00001094_PyTrash_destroy_chain()
Guido van Rossumd724b232000-03-13 16:01:29 +00001095{
1096 while (_PyTrash_delete_later) {
1097 PyObject *shredder = _PyTrash_delete_later;
Guido van Rossume92e6102000-04-24 15:40:53 +00001098 _PyTrash_delete_later = (PyObject*) shredder->ob_type;
1099
1100 switch (shredder->ob_refcnt) {
1101 case Py_TRASHCAN_TUPLE:
1102 shredder->ob_type = &PyTuple_Type;
1103 break;
1104 case Py_TRASHCAN_LIST:
1105 shredder->ob_type = &PyList_Type;
1106 break;
1107 case Py_TRASHCAN_DICT:
1108 shredder->ob_type = &PyDict_Type;
1109 break;
1110 case Py_TRASHCAN_FRAME:
1111 shredder->ob_type = &PyFrame_Type;
1112 break;
1113 case Py_TRASHCAN_TRACEBACK:
1114 shredder->ob_type = &PyTraceBack_Type;
1115 break;
1116 }
1117 _Py_NewReference(shredder);
1118
Guido van Rossumd724b232000-03-13 16:01:29 +00001119 ++_PyTrash_delete_nesting;
1120 Py_DECREF(shredder);
1121 --_PyTrash_delete_nesting;
1122 }
1123}
Guido van Rossume92e6102000-04-24 15:40:53 +00001124