blob: f61a1dda655aa5e09f3ca775af49ed23c8651296 [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001/***********************************************************
Guido van Rossumfd71b9e2000-06-30 23:50:40 +00002Copyright (c) 2000, BeOpen.com.
3Copyright (c) 1995-2000, Corporation for National Research Initiatives.
4Copyright (c) 1990-1995, Stichting Mathematisch Centrum.
5All rights reserved.
Guido van Rossumf70e43a1991-02-19 12:39:46 +00006
Guido van Rossumfd71b9e2000-06-30 23:50:40 +00007See the file "Misc/COPYRIGHT" for information on usage and
8redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES.
Guido van Rossumf70e43a1991-02-19 12:39:46 +00009******************************************************************/
10
Guido van Rossum3f5da241990-12-20 15:06:42 +000011/* Generic object operations; and implementation of None (NoObject) */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000012
Guido van Rossumc0b618a1997-05-02 03:12:38 +000013#include "Python.h"
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000014
Jack Jansen28fc8802000-07-11 21:47:20 +000015#ifdef macintosh
16#include "macglue.h"
17#endif
18
Guido van Rossume92e6102000-04-24 15:40:53 +000019/* just for trashcan: */
20#include "compile.h"
21#include "frameobject.h"
22#include "traceback.h"
23
Guido van Rossum6f9e4331995-03-29 16:57:48 +000024#if defined( Py_TRACE_REFS ) || defined( Py_REF_DEBUG )
Guido van Rossumbffd6832000-01-20 22:32:56 +000025DL_IMPORT(long) _Py_RefTotal;
Guido van Rossum3f5da241990-12-20 15:06:42 +000026#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000027
Guido van Rossum3f5da241990-12-20 15:06:42 +000028/* Object allocation routines used by NEWOBJ and NEWVAROBJ macros.
29 These are used by the individual routines for object creation.
30 Do not call them otherwise, they do not initialize the object! */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000031
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +000032#ifdef COUNT_ALLOCS
Guido van Rossumc0b618a1997-05-02 03:12:38 +000033static PyTypeObject *type_list;
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +000034extern int tuple_zero_allocs, fast_tuple_allocs;
35extern int quick_int_allocs, quick_neg_int_allocs;
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +000036extern int null_strings, one_strings;
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +000037void
Fred Drake100814d2000-07-09 15:48:49 +000038dump_counts(void)
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +000039{
Guido van Rossumc0b618a1997-05-02 03:12:38 +000040 PyTypeObject *tp;
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +000041
42 for (tp = type_list; tp; tp = tp->tp_next)
Sjoerd Mullender52c1f511993-10-25 08:40:52 +000043 fprintf(stderr, "%s alloc'd: %d, freed: %d, max in use: %d\n",
44 tp->tp_name, tp->tp_alloc, tp->tp_free,
45 tp->tp_maxalloc);
46 fprintf(stderr, "fast tuple allocs: %d, empty: %d\n",
47 fast_tuple_allocs, tuple_zero_allocs);
48 fprintf(stderr, "fast int allocs: pos: %d, neg: %d\n",
49 quick_int_allocs, quick_neg_int_allocs);
50 fprintf(stderr, "null strings: %d, 1-strings: %d\n",
51 null_strings, one_strings);
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +000052}
53
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +000054PyObject *
Fred Drake100814d2000-07-09 15:48:49 +000055get_counts(void)
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +000056{
57 PyTypeObject *tp;
58 PyObject *result;
59 PyObject *v;
60
61 result = PyList_New(0);
62 if (result == NULL)
63 return NULL;
64 for (tp = type_list; tp; tp = tp->tp_next) {
65 v = Py_BuildValue("(siii)", tp->tp_name, tp->tp_alloc,
66 tp->tp_free, tp->tp_maxalloc);
67 if (v == NULL) {
68 Py_DECREF(result);
69 return NULL;
70 }
71 if (PyList_Append(result, v) < 0) {
72 Py_DECREF(v);
73 Py_DECREF(result);
74 return NULL;
75 }
76 Py_DECREF(v);
77 }
78 return result;
79}
80
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +000081void
Fred Drake100814d2000-07-09 15:48:49 +000082inc_count(PyTypeObject *tp)
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +000083{
84 if (tp->tp_alloc == 0) {
Guido van Rossumd8953cb1995-04-06 14:46:26 +000085 /* first time; insert in linked list */
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +000086 if (tp->tp_next != NULL) /* sanity check */
Guido van Rossumc0b618a1997-05-02 03:12:38 +000087 Py_FatalError("XXX inc_count sanity check");
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +000088 tp->tp_next = type_list;
89 type_list = tp;
90 }
91 tp->tp_alloc++;
92 if (tp->tp_alloc - tp->tp_free > tp->tp_maxalloc)
93 tp->tp_maxalloc = tp->tp_alloc - tp->tp_free;
94}
95#endif
96
Guido van Rossumc0b618a1997-05-02 03:12:38 +000097PyObject *
Fred Drake100814d2000-07-09 15:48:49 +000098PyObject_Init(PyObject *op, PyTypeObject *tp)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000099{
Guido van Rossumb18618d2000-05-03 23:44:39 +0000100 if (op == NULL) {
101 PyErr_SetString(PyExc_SystemError,
102 "NULL object passed to PyObject_Init");
103 return op;
104 }
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000105#ifdef WITH_CYCLE_GC
106 if (PyType_IS_GC(tp))
107 op = (PyObject *) PyObject_FROM_GC(op);
108#endif
Guido van Rossumb18618d2000-05-03 23:44:39 +0000109 /* Any changes should be reflected in PyObject_INIT (objimpl.h) */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000110 op->ob_type = tp;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000111 _Py_NewReference(op);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000112 return op;
113}
114
Guido van Rossumb18618d2000-05-03 23:44:39 +0000115PyVarObject *
Fred Drake100814d2000-07-09 15:48:49 +0000116PyObject_InitVar(PyVarObject *op, PyTypeObject *tp, int size)
Guido van Rossumb18618d2000-05-03 23:44:39 +0000117{
118 if (op == NULL) {
119 PyErr_SetString(PyExc_SystemError,
120 "NULL object passed to PyObject_InitVar");
121 return op;
122 }
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000123#ifdef WITH_CYCLE_GC
124 if (PyType_IS_GC(tp))
125 op = (PyVarObject *) PyObject_FROM_GC(op);
126#endif
Guido van Rossumb18618d2000-05-03 23:44:39 +0000127 /* Any changes should be reflected in PyObject_INIT_VAR */
128 op->ob_size = size;
129 op->ob_type = tp;
130 _Py_NewReference((PyObject *)op);
131 return op;
132}
133
134PyObject *
Fred Drake100814d2000-07-09 15:48:49 +0000135_PyObject_New(PyTypeObject *tp)
Guido van Rossumb18618d2000-05-03 23:44:39 +0000136{
137 PyObject *op;
138 op = (PyObject *) PyObject_MALLOC(_PyObject_SIZE(tp));
139 if (op == NULL)
140 return PyErr_NoMemory();
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000141#ifdef WITH_CYCLE_GC
142 if (PyType_IS_GC(tp))
143 op = (PyObject *) PyObject_FROM_GC(op);
144#endif
Guido van Rossumb18618d2000-05-03 23:44:39 +0000145 return PyObject_INIT(op, tp);
146}
147
Guido van Rossumd0c87ee1997-05-15 21:31:03 +0000148PyVarObject *
Fred Drake100814d2000-07-09 15:48:49 +0000149_PyObject_NewVar(PyTypeObject *tp, int size)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000150{
Guido van Rossumb18618d2000-05-03 23:44:39 +0000151 PyVarObject *op;
152 op = (PyVarObject *) PyObject_MALLOC(_PyObject_VAR_SIZE(tp, size));
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000153 if (op == NULL)
Guido van Rossumd0c87ee1997-05-15 21:31:03 +0000154 return (PyVarObject *)PyErr_NoMemory();
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000155#ifdef WITH_CYCLE_GC
156 if (PyType_IS_GC(tp))
157 op = (PyVarObject *) PyObject_FROM_GC(op);
158#endif
Guido van Rossumb18618d2000-05-03 23:44:39 +0000159 return PyObject_INIT_VAR(op, tp, size);
160}
161
162void
Fred Drake100814d2000-07-09 15:48:49 +0000163_PyObject_Del(PyObject *op)
Guido van Rossumb18618d2000-05-03 23:44:39 +0000164{
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000165#ifdef WITH_CYCLE_GC
Guido van Rossum4cc6ac72000-07-01 01:00:38 +0000166 if (op && PyType_IS_GC(op->ob_type)) {
167 op = (PyObject *) PyObject_AS_GC(op);
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000168 }
Guido van Rossum4cc6ac72000-07-01 01:00:38 +0000169#endif
170 PyObject_FREE(op);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000171}
172
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000173#ifndef WITH_CYCLE_GC
174/* extension modules might need these */
175void _PyGC_Insert(PyObject *op) { }
176void _PyGC_Remove(PyObject *op) { }
177#endif
178
Guido van Rossum90933611991-06-07 16:10:43 +0000179int
Fred Drake100814d2000-07-09 15:48:49 +0000180PyObject_Print(PyObject *op, FILE *fp, int flags)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000181{
Guido van Rossum278ef591991-07-27 21:40:24 +0000182 int ret = 0;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000183 if (PyErr_CheckSignals())
Guido van Rossum90933611991-06-07 16:10:43 +0000184 return -1;
Guido van Rossum9b00dfa1998-04-28 16:06:54 +0000185#ifdef USE_STACKCHECK
186 if (PyOS_CheckStack()) {
187 PyErr_SetString(PyExc_MemoryError, "Stack overflow");
188 return -1;
189 }
190#endif
Guido van Rossum687ef6e2000-01-12 16:28:58 +0000191 clearerr(fp); /* Clear any previous error condition */
Guido van Rossum90933611991-06-07 16:10:43 +0000192 if (op == NULL) {
193 fprintf(fp, "<nil>");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000194 }
Guido van Rossum90933611991-06-07 16:10:43 +0000195 else {
196 if (op->ob_refcnt <= 0)
Fred Drakea44d3532000-06-30 15:01:00 +0000197 fprintf(fp, "<refcnt %u at %p>",
198 op->ob_refcnt, op);
Guido van Rossum2e8f6141992-09-03 20:32:55 +0000199 else if (op->ob_type->tp_print == NULL) {
200 if (op->ob_type->tp_repr == NULL) {
Fred Drakea44d3532000-06-30 15:01:00 +0000201 fprintf(fp, "<%s object at %p>",
202 op->ob_type->tp_name, op);
Guido van Rossum2e8f6141992-09-03 20:32:55 +0000203 }
204 else {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000205 PyObject *s;
206 if (flags & Py_PRINT_RAW)
207 s = PyObject_Str(op);
Guido van Rossumc6004111993-11-05 10:22:19 +0000208 else
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000209 s = PyObject_Repr(op);
Guido van Rossum2e8f6141992-09-03 20:32:55 +0000210 if (s == NULL)
211 ret = -1;
Guido van Rossum2e8f6141992-09-03 20:32:55 +0000212 else {
Guido van Rossum565798d1998-04-21 22:25:01 +0000213 ret = PyObject_Print(s, fp,
214 Py_PRINT_RAW);
Guido van Rossum2e8f6141992-09-03 20:32:55 +0000215 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000216 Py_XDECREF(s);
Guido van Rossum2e8f6141992-09-03 20:32:55 +0000217 }
218 }
Guido van Rossum90933611991-06-07 16:10:43 +0000219 else
Guido van Rossum278ef591991-07-27 21:40:24 +0000220 ret = (*op->ob_type->tp_print)(op, fp, flags);
Guido van Rossum90933611991-06-07 16:10:43 +0000221 }
Guido van Rossum278ef591991-07-27 21:40:24 +0000222 if (ret == 0) {
223 if (ferror(fp)) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000224 PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossum278ef591991-07-27 21:40:24 +0000225 clearerr(fp);
226 ret = -1;
227 }
228 }
229 return ret;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000230}
231
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000232PyObject *
Fred Drake100814d2000-07-09 15:48:49 +0000233PyObject_Repr(PyObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000234{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000235 if (PyErr_CheckSignals())
Guido van Rossum90933611991-06-07 16:10:43 +0000236 return NULL;
Guido van Rossum9b00dfa1998-04-28 16:06:54 +0000237#ifdef USE_STACKCHECK
238 if (PyOS_CheckStack()) {
239 PyErr_SetString(PyExc_MemoryError, "Stack overflow");
240 return NULL;
241 }
242#endif
Guido van Rossum90933611991-06-07 16:10:43 +0000243 if (v == NULL)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000244 return PyString_FromString("<NULL>");
Guido van Rossum90933611991-06-07 16:10:43 +0000245 else if (v->ob_type->tp_repr == NULL) {
246 char buf[120];
Fred Drakea44d3532000-06-30 15:01:00 +0000247 sprintf(buf, "<%.80s object at %p>",
248 v->ob_type->tp_name, v);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000249 return PyString_FromString(buf);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000250 }
Guido van Rossum4c08d552000-03-10 22:55:18 +0000251 else {
252 PyObject *res;
253 res = (*v->ob_type->tp_repr)(v);
254 if (res == NULL)
255 return NULL;
Fredrik Lundhefecc7d2000-07-01 14:31:09 +0000256 if (PyUnicode_Check(res)) {
257 PyObject* str;
Fredrik Lundh2a1e0602000-07-08 17:43:32 +0000258 str = PyUnicode_AsUnicodeEscapeString(res);
Marc-André Lemburg891bc652000-07-03 09:57:53 +0000259 Py_DECREF(res);
260 if (str)
Fredrik Lundhefecc7d2000-07-01 14:31:09 +0000261 res = str;
Marc-André Lemburg891bc652000-07-03 09:57:53 +0000262 else
263 return NULL;
Fredrik Lundhefecc7d2000-07-01 14:31:09 +0000264 }
Guido van Rossum4c08d552000-03-10 22:55:18 +0000265 if (!PyString_Check(res)) {
266 PyErr_Format(PyExc_TypeError,
Guido van Rossum5db862d2000-04-10 12:46:51 +0000267 "__repr__ returned non-string (type %.200s)",
Guido van Rossum4c08d552000-03-10 22:55:18 +0000268 res->ob_type->tp_name);
269 Py_DECREF(res);
270 return NULL;
271 }
272 return res;
273 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000274}
275
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000276PyObject *
Fred Drake100814d2000-07-09 15:48:49 +0000277PyObject_Str(PyObject *v)
Guido van Rossumc6004111993-11-05 10:22:19 +0000278{
Guido van Rossum4c08d552000-03-10 22:55:18 +0000279 PyObject *res;
280
Guido van Rossumc6004111993-11-05 10:22:19 +0000281 if (v == NULL)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000282 return PyString_FromString("<NULL>");
283 else if (PyString_Check(v)) {
284 Py_INCREF(v);
Guido van Rossumc6004111993-11-05 10:22:19 +0000285 return v;
286 }
Guido van Rossum32b582b1995-01-17 16:35:13 +0000287 else if (v->ob_type->tp_str != NULL)
Guido van Rossum4c08d552000-03-10 22:55:18 +0000288 res = (*v->ob_type->tp_str)(v);
Guido van Rossumc6004111993-11-05 10:22:19 +0000289 else {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000290 PyObject *func;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000291 if (!PyInstance_Check(v) ||
292 (func = PyObject_GetAttrString(v, "__str__")) == NULL) {
293 PyErr_Clear();
294 return PyObject_Repr(v);
Guido van Rossumc6004111993-11-05 10:22:19 +0000295 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000296 res = PyEval_CallObject(func, (PyObject *)NULL);
297 Py_DECREF(func);
Guido van Rossumc6004111993-11-05 10:22:19 +0000298 }
Guido van Rossum4c08d552000-03-10 22:55:18 +0000299 if (res == NULL)
300 return NULL;
Marc-André Lemburg891bc652000-07-03 09:57:53 +0000301 if (PyUnicode_Check(res)) {
302 PyObject* str;
303 str = PyUnicode_AsEncodedString(res, NULL, NULL);
304 Py_DECREF(res);
305 if (str)
306 res = str;
307 else
308 return NULL;
309 }
Guido van Rossum4c08d552000-03-10 22:55:18 +0000310 if (!PyString_Check(res)) {
311 PyErr_Format(PyExc_TypeError,
Guido van Rossum5db862d2000-04-10 12:46:51 +0000312 "__str__ returned non-string (type %.200s)",
Guido van Rossum4c08d552000-03-10 22:55:18 +0000313 res->ob_type->tp_name);
314 Py_DECREF(res);
315 return NULL;
316 }
317 return res;
Guido van Rossumc6004111993-11-05 10:22:19 +0000318}
319
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000320static PyObject *
Fred Drake100814d2000-07-09 15:48:49 +0000321do_cmp(PyObject *v, PyObject *w)
Guido van Rossum20566841995-01-12 11:26:10 +0000322{
Guido van Rossumc8b6df91997-05-23 00:06:51 +0000323 long c;
Guido van Rossum20566841995-01-12 11:26:10 +0000324 /* __rcmp__ actually won't be called unless __cmp__ isn't defined,
325 because the check in cmpobject() reverses the objects first.
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000326 This is intentional -- it makes no sense to define cmp(x,y)
327 different than -cmp(y,x). */
328 if (PyInstance_Check(v) || PyInstance_Check(w))
329 return PyInstance_DoBinOp(v, w, "__cmp__", "__rcmp__", do_cmp);
Guido van Rossumc8b6df91997-05-23 00:06:51 +0000330 c = PyObject_Compare(v, w);
331 if (c && PyErr_Occurred())
332 return NULL;
333 return PyInt_FromLong(c);
Guido van Rossum20566841995-01-12 11:26:10 +0000334}
335
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000336PyObject *_PyCompareState_Key;
337
Thomas Wouters7e474022000-07-16 12:04:32 +0000338/* _PyCompareState_nesting is incremented before calling compare (for
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000339 some types) and decremented on exit. If the count exceeds the
340 nesting limit, enable code to detect circular data structures.
341*/
342#define NESTING_LIMIT 500
343int _PyCompareState_nesting = 0;
344
345static PyObject*
Fred Drake100814d2000-07-09 15:48:49 +0000346get_inprogress_dict(void)
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000347{
348 PyObject *tstate_dict, *inprogress;
349
350 tstate_dict = PyThreadState_GetDict();
351 if (tstate_dict == NULL) {
352 PyErr_BadInternalCall();
353 return NULL;
354 }
355 inprogress = PyDict_GetItem(tstate_dict, _PyCompareState_Key);
356 if (inprogress == NULL) {
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000357 inprogress = PyDict_New();
358 if (inprogress == NULL)
359 return NULL;
360 if (PyDict_SetItem(tstate_dict, _PyCompareState_Key,
361 inprogress) == -1) {
362 Py_DECREF(inprogress);
363 return NULL;
364 }
Jeremy Hyltona251ea02000-06-09 16:20:39 +0000365 Py_DECREF(inprogress);
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000366 }
367 return inprogress;
368}
369
370static PyObject *
Fred Drake100814d2000-07-09 15:48:49 +0000371make_pair(PyObject *v, PyObject *w)
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000372{
373 PyObject *pair;
Barry Warsaw9d23a4e2000-08-18 05:01:19 +0000374 Py_uintptr_t iv = (Py_uintptr_t)v;
375 Py_uintptr_t iw = (Py_uintptr_t)w;
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000376
377 pair = PyTuple_New(2);
378 if (pair == NULL) {
379 return NULL;
380 }
Barry Warsaw9d23a4e2000-08-18 05:01:19 +0000381 if (iv <= iw) {
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000382 PyTuple_SET_ITEM(pair, 0, PyLong_FromVoidPtr((void *)v));
383 PyTuple_SET_ITEM(pair, 1, PyLong_FromVoidPtr((void *)w));
384 } else {
385 PyTuple_SET_ITEM(pair, 0, PyLong_FromVoidPtr((void *)w));
386 PyTuple_SET_ITEM(pair, 1, PyLong_FromVoidPtr((void *)v));
387 }
388 return pair;
389}
390
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000391int
Fred Drake100814d2000-07-09 15:48:49 +0000392PyObject_Compare(PyObject *v, PyObject *w)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000393{
Guido van Rossumcd5a5f61998-06-09 18:58:44 +0000394 PyTypeObject *vtp, *wtp;
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000395 int result;
396
Guido van Rossumc8b6df91997-05-23 00:06:51 +0000397 if (v == NULL || w == NULL) {
398 PyErr_BadInternalCall();
399 return -1;
400 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000401 if (v == w)
402 return 0;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000403 if (PyInstance_Check(v) || PyInstance_Check(w)) {
404 PyObject *res;
Guido van Rossum20566841995-01-12 11:26:10 +0000405 int c;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000406 if (!PyInstance_Check(v))
407 return -PyObject_Compare(w, v);
Vladimir Marangozov1d3e2392000-08-11 00:14:26 +0000408 _PyCompareState_nesting++;
409 if (_PyCompareState_nesting > NESTING_LIMIT) {
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000410 PyObject *inprogress, *pair;
411
412 inprogress = get_inprogress_dict();
413 if (inprogress == NULL) {
Vladimir Marangozov1d3e2392000-08-11 00:14:26 +0000414 _PyCompareState_nesting--;
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000415 return -1;
416 }
417 pair = make_pair(v, w);
418 if (PyDict_GetItem(inprogress, pair)) {
419 /* already comparing these objects. assume
420 they're equal until shown otherwise */
421 Py_DECREF(pair);
Vladimir Marangozov1d3e2392000-08-11 00:14:26 +0000422 _PyCompareState_nesting--;
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000423 return 0;
424 }
425 if (PyDict_SetItem(inprogress, pair, pair) == -1) {
Vladimir Marangozov1d3e2392000-08-11 00:14:26 +0000426 _PyCompareState_nesting--;
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000427 return -1;
428 }
429 res = do_cmp(v, w);
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000430 /* XXX DelItem shouldn't fail */
431 PyDict_DelItem(inprogress, pair);
432 Py_DECREF(pair);
433 } else {
434 res = do_cmp(v, w);
435 }
Vladimir Marangozov1d3e2392000-08-11 00:14:26 +0000436 _PyCompareState_nesting--;
Guido van Rossumc8b6df91997-05-23 00:06:51 +0000437 if (res == NULL)
438 return -1;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000439 if (!PyInt_Check(res)) {
440 Py_DECREF(res);
Guido van Rossumc8b6df91997-05-23 00:06:51 +0000441 PyErr_SetString(PyExc_TypeError,
442 "comparison did not return an int");
443 return -1;
Guido van Rossum20566841995-01-12 11:26:10 +0000444 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000445 c = PyInt_AsLong(res);
446 Py_DECREF(res);
Guido van Rossum20566841995-01-12 11:26:10 +0000447 return (c < 0) ? -1 : (c > 0) ? 1 : 0;
448 }
Guido van Rossumcd5a5f61998-06-09 18:58:44 +0000449 if ((vtp = v->ob_type) != (wtp = w->ob_type)) {
450 char *vname = vtp->tp_name;
451 char *wname = wtp->tp_name;
Guido van Rossumb4db1941998-07-21 21:56:41 +0000452 if (vtp->tp_as_number != NULL && wtp->tp_as_number != NULL) {
Guido van Rossum242c6421997-11-19 16:03:17 +0000453 int err;
454 err = PyNumber_CoerceEx(&v, &w);
455 if (err < 0)
Guido van Rossumc8b6df91997-05-23 00:06:51 +0000456 return -1;
Guido van Rossum242c6421997-11-19 16:03:17 +0000457 else if (err == 0) {
Guido van Rossumb4db1941998-07-21 21:56:41 +0000458 int cmp;
459 vtp = v->ob_type;
460 if (vtp->tp_compare == NULL)
461 cmp = (v < w) ? -1 : 1;
462 else
463 cmp = (*vtp->tp_compare)(v, w);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000464 Py_DECREF(v);
465 Py_DECREF(w);
Guido van Rossum9fb03681991-07-01 18:48:04 +0000466 return cmp;
467 }
468 }
Guido van Rossumb244f692000-04-10 13:42:33 +0000469 else if (PyUnicode_Check(v) || PyUnicode_Check(w)) {
470 int result = PyUnicode_Compare(v, w);
471 if (result == -1 && PyErr_Occurred() &&
472 PyErr_ExceptionMatches(PyExc_TypeError))
473 /* TypeErrors are ignored: if Unicode coercion
474 fails due to one of the arguments not
475 having the right type, we continue as
476 defined by the coercion protocol (see
477 above). Luckily, decoding errors are
478 reported as ValueErrors and are not masked
479 by this technique. */
480 PyErr_Clear();
481 else
482 return result;
483 }
Guido van Rossumcd5a5f61998-06-09 18:58:44 +0000484 else if (vtp->tp_as_number != NULL)
485 vname = "";
486 else if (wtp->tp_as_number != NULL)
487 wname = "";
488 /* Numerical types compare smaller than all other types */
489 return strcmp(vname, wname);
Guido van Rossum9fb03681991-07-01 18:48:04 +0000490 }
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000491 if (vtp->tp_compare == NULL) {
Barry Warsaw9d23a4e2000-08-18 05:01:19 +0000492 Py_uintptr_t iv = (Py_uintptr_t)v;
493 Py_uintptr_t iw = (Py_uintptr_t)w;
494 return (iv < iw) ? -1 : 1;
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000495 }
Vladimir Marangozov1d3e2392000-08-11 00:14:26 +0000496 _PyCompareState_nesting++;
497 if (_PyCompareState_nesting > NESTING_LIMIT
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000498 && (vtp->tp_as_mapping
499 || (vtp->tp_as_sequence && !PyString_Check(v)))) {
500 PyObject *inprogress, *pair;
501
502 inprogress = get_inprogress_dict();
503 if (inprogress == NULL) {
Vladimir Marangozov1d3e2392000-08-11 00:14:26 +0000504 _PyCompareState_nesting--;
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000505 return -1;
506 }
507 pair = make_pair(v, w);
508 if (PyDict_GetItem(inprogress, pair)) {
509 /* already comparing these objects. assume
510 they're equal until shown otherwise */
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000511 Py_DECREF(pair);
Vladimir Marangozov1d3e2392000-08-11 00:14:26 +0000512 _PyCompareState_nesting--;
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000513 return 0;
514 }
515 if (PyDict_SetItem(inprogress, pair, pair) == -1) {
Vladimir Marangozov1d3e2392000-08-11 00:14:26 +0000516 _PyCompareState_nesting--;
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000517 return -1;
518 }
519 result = (*vtp->tp_compare)(v, w);
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000520 PyDict_DelItem(inprogress, pair); /* XXX shouldn't fail */
521 Py_DECREF(pair);
522 } else {
523 result = (*vtp->tp_compare)(v, w);
524 }
Vladimir Marangozov1d3e2392000-08-11 00:14:26 +0000525 _PyCompareState_nesting--;
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000526 return result;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000527}
528
Fred Drake13634cf2000-06-29 19:17:04 +0000529
530/* Set of hash utility functions to help maintaining the invariant that
531 iff a==b then hash(a)==hash(b)
532
533 All the utility functions (_Py_Hash*()) return "-1" to signify an error.
534*/
535
536long
Fred Drake100814d2000-07-09 15:48:49 +0000537_Py_HashDouble(double v)
Fred Drake13634cf2000-06-29 19:17:04 +0000538{
Tim Peters39dce292000-08-15 03:34:48 +0000539 double intpart, fractpart;
540 int expo;
541 long hipart;
542 long x; /* the final hash value */
543 /* This is designed so that Python numbers of different types
544 * that compare equal hash to the same value; otherwise comparisons
545 * of mapping keys will turn out weird.
546 */
547
548#ifdef MPW /* MPW C modf expects pointer to extended as second argument */
549{
550 extended e;
551 fractpart = modf(v, &e);
552 intpart = e;
553}
554#else
555 fractpart = modf(v, &intpart);
556#endif
557 if (fractpart == 0.0) {
558 /* This must return the same hash as an equal int or long. */
559 if (intpart > LONG_MAX || -intpart > LONG_MAX) {
560 /* Convert to long and use its hash. */
561 PyObject *plong; /* converted to Python long */
562 if (Py_IS_INFINITY(intpart))
563 /* can't convert to long int -- arbitrary */
564 v = v < 0 ? -271828.0 : 314159.0;
565 plong = PyLong_FromDouble(v);
566 if (plong == NULL)
567 return -1;
568 x = PyObject_Hash(plong);
569 Py_DECREF(plong);
570 return x;
571 }
572 /* Fits in a C long == a Python int, so is its own hash. */
573 x = (long)intpart;
574 if (x == -1)
575 x = -2;
576 return x;
577 }
578 /* The fractional part is non-zero, so we don't have to worry about
579 * making this match the hash of some other type.
580 * Use frexp to get at the bits in the double.
Fred Drake13634cf2000-06-29 19:17:04 +0000581 * Since the VAX D double format has 56 mantissa bits, which is the
582 * most of any double format in use, each of these parts may have as
583 * many as (but no more than) 56 significant bits.
Tim Peters39dce292000-08-15 03:34:48 +0000584 * So, assuming sizeof(long) >= 4, each part can be broken into two
585 * longs; frexp and multiplication are used to do that.
586 * Also, since the Cray double format has 15 exponent bits, which is
587 * the most of any double format in use, shifting the exponent field
588 * left by 15 won't overflow a long (again assuming sizeof(long) >= 4).
Fred Drake13634cf2000-06-29 19:17:04 +0000589 */
Tim Peters39dce292000-08-15 03:34:48 +0000590 v = frexp(v, &expo);
591 v *= 2147483648.0; /* 2**31 */
592 hipart = (long)v; /* take the top 32 bits */
593 v = (v - (double)hipart) * 2147483648.0; /* get the next 32 bits */
594 x = hipart + (long)v + (expo << 15);
595 if (x == -1)
596 x = -2;
597 return x;
Fred Drake13634cf2000-06-29 19:17:04 +0000598}
599
600long
Fred Drake100814d2000-07-09 15:48:49 +0000601_Py_HashPointer(void *p)
Fred Drake13634cf2000-06-29 19:17:04 +0000602{
603#if SIZEOF_LONG >= SIZEOF_VOID_P
604 return (long)p;
605#else
606 /* convert to a Python long and hash that */
607 PyObject* longobj;
608 long x;
609
610 if ((longobj = PyLong_FromVoidPtr(p)) == NULL) {
611 x = -1;
612 goto finally;
613 }
614 x = PyObject_Hash(longobj);
615
616finally:
617 Py_XDECREF(longobj);
618 return x;
619#endif
620}
621
622
Guido van Rossum9bfef441993-03-29 10:43:31 +0000623long
Fred Drake100814d2000-07-09 15:48:49 +0000624PyObject_Hash(PyObject *v)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000625{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000626 PyTypeObject *tp = v->ob_type;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000627 if (tp->tp_hash != NULL)
628 return (*tp->tp_hash)(v);
Fred Drake13634cf2000-06-29 19:17:04 +0000629 if (tp->tp_compare == NULL) {
630 return _Py_HashPointer(v); /* Use address as hash value */
631 }
Guido van Rossum9bfef441993-03-29 10:43:31 +0000632 /* If there's a cmp but no hash defined, the object can't be hashed */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000633 PyErr_SetString(PyExc_TypeError, "unhashable type");
Guido van Rossum9bfef441993-03-29 10:43:31 +0000634 return -1;
635}
636
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000637PyObject *
Fred Drake100814d2000-07-09 15:48:49 +0000638PyObject_GetAttrString(PyObject *v, char *name)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000639{
Guido van Rossumd8eb1b31996-08-09 20:52:03 +0000640 if (v->ob_type->tp_getattro != NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000641 PyObject *w, *res;
Guido van Rossumc6d06701997-01-18 07:57:16 +0000642 w = PyString_InternFromString(name);
Guido van Rossumd8eb1b31996-08-09 20:52:03 +0000643 if (w == NULL)
644 return NULL;
645 res = (*v->ob_type->tp_getattro)(v, w);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000646 Py_XDECREF(w);
Guido van Rossumd8eb1b31996-08-09 20:52:03 +0000647 return res;
648 }
649
Guido van Rossum3f5da241990-12-20 15:06:42 +0000650 if (v->ob_type->tp_getattr == NULL) {
Guido van Rossumdb935161998-01-19 22:16:36 +0000651 PyErr_Format(PyExc_AttributeError,
652 "'%.50s' object has no attribute '%.400s'",
653 v->ob_type->tp_name,
654 name);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000655 return NULL;
656 }
657 else {
658 return (*v->ob_type->tp_getattr)(v, name);
659 }
660}
661
662int
Fred Drake100814d2000-07-09 15:48:49 +0000663PyObject_HasAttrString(PyObject *v, char *name)
Guido van Rossumed18fdc1993-07-11 19:55:34 +0000664{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000665 PyObject *res = PyObject_GetAttrString(v, name);
Guido van Rossumed18fdc1993-07-11 19:55:34 +0000666 if (res != NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000667 Py_DECREF(res);
Guido van Rossumed18fdc1993-07-11 19:55:34 +0000668 return 1;
669 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000670 PyErr_Clear();
Guido van Rossumed18fdc1993-07-11 19:55:34 +0000671 return 0;
672}
673
674int
Fred Drake100814d2000-07-09 15:48:49 +0000675PyObject_SetAttrString(PyObject *v, char *name, PyObject *w)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000676{
Guido van Rossumd8eb1b31996-08-09 20:52:03 +0000677 if (v->ob_type->tp_setattro != NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000678 PyObject *s;
Guido van Rossumd8eb1b31996-08-09 20:52:03 +0000679 int res;
Guido van Rossumc6d06701997-01-18 07:57:16 +0000680 s = PyString_InternFromString(name);
Guido van Rossumd8eb1b31996-08-09 20:52:03 +0000681 if (s == NULL)
Guido van Rossumb7fc3041996-09-11 22:51:25 +0000682 return -1;
Guido van Rossumd8eb1b31996-08-09 20:52:03 +0000683 res = (*v->ob_type->tp_setattro)(v, s, w);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000684 Py_XDECREF(s);
Guido van Rossumd8eb1b31996-08-09 20:52:03 +0000685 return res;
686 }
687
Guido van Rossum3f5da241990-12-20 15:06:42 +0000688 if (v->ob_type->tp_setattr == NULL) {
689 if (v->ob_type->tp_getattr == NULL)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000690 PyErr_SetString(PyExc_TypeError,
Guido van Rossum3ea74121991-12-24 13:28:03 +0000691 "attribute-less object (assign or del)");
Guido van Rossum3f5da241990-12-20 15:06:42 +0000692 else
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000693 PyErr_SetString(PyExc_TypeError,
Guido van Rossum3ea74121991-12-24 13:28:03 +0000694 "object has read-only attributes");
Guido van Rossum73531a31990-12-20 23:12:40 +0000695 return -1;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000696 }
697 else {
698 return (*v->ob_type->tp_setattr)(v, name, w);
699 }
700}
701
Guido van Rossum98ff96a1997-05-20 18:34:44 +0000702PyObject *
Fred Drake100814d2000-07-09 15:48:49 +0000703PyObject_GetAttr(PyObject *v, PyObject *name)
Guido van Rossum98ff96a1997-05-20 18:34:44 +0000704{
705 if (v->ob_type->tp_getattro != NULL)
706 return (*v->ob_type->tp_getattro)(v, name);
Jeremy Hylton99a8f902000-06-23 14:36:32 +0000707
708 if (!PyString_Check(name)) {
709 PyErr_SetString(PyExc_TypeError,
710 "attribute name must be string");
711 return NULL;
712 }
713 return PyObject_GetAttrString(v, PyString_AS_STRING(name));
Guido van Rossum98ff96a1997-05-20 18:34:44 +0000714}
715
716int
Fred Drake100814d2000-07-09 15:48:49 +0000717PyObject_HasAttr(PyObject *v, PyObject *name)
Guido van Rossum98ff96a1997-05-20 18:34:44 +0000718{
719 PyObject *res = PyObject_GetAttr(v, name);
720 if (res != NULL) {
721 Py_DECREF(res);
722 return 1;
723 }
724 PyErr_Clear();
725 return 0;
726}
727
728int
Fred Drake100814d2000-07-09 15:48:49 +0000729PyObject_SetAttr(PyObject *v, PyObject *name, PyObject *value)
Guido van Rossum98ff96a1997-05-20 18:34:44 +0000730{
731 int err;
732 Py_INCREF(name);
Jeremy Hylton99a8f902000-06-23 14:36:32 +0000733 if (PyString_Check(name))
734 PyString_InternInPlace(&name);
Guido van Rossum98ff96a1997-05-20 18:34:44 +0000735 if (v->ob_type->tp_setattro != NULL)
736 err = (*v->ob_type->tp_setattro)(v, name, value);
Jeremy Hylton99a8f902000-06-23 14:36:32 +0000737 else if (PyString_Check(name)) {
Guido van Rossum98ff96a1997-05-20 18:34:44 +0000738 err = PyObject_SetAttrString(
Jeremy Hylton99a8f902000-06-23 14:36:32 +0000739 v, PyString_AS_STRING(name), value);
740 }
741 else {
742 PyErr_SetString(PyExc_TypeError,
743 "attribute name must be string");
744 err = -1;
745 }
Guido van Rossum98ff96a1997-05-20 18:34:44 +0000746 Py_DECREF(name);
747 return err;
748}
749
Guido van Rossum6ac258d1993-05-12 08:24:20 +0000750/* Test a value used as condition, e.g., in a for or if statement.
751 Return -1 if an error occurred */
752
753int
Fred Drake100814d2000-07-09 15:48:49 +0000754PyObject_IsTrue(PyObject *v)
Guido van Rossum6ac258d1993-05-12 08:24:20 +0000755{
756 int res;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000757 if (v == Py_None)
Guido van Rossum6ac258d1993-05-12 08:24:20 +0000758 res = 0;
Guido van Rossum1c4f4581998-05-22 00:53:24 +0000759 else if (v->ob_type->tp_as_number != NULL &&
760 v->ob_type->tp_as_number->nb_nonzero != NULL)
Guido van Rossum6ac258d1993-05-12 08:24:20 +0000761 res = (*v->ob_type->tp_as_number->nb_nonzero)(v);
Guido van Rossum1c4f4581998-05-22 00:53:24 +0000762 else if (v->ob_type->tp_as_mapping != NULL &&
763 v->ob_type->tp_as_mapping->mp_length != NULL)
Guido van Rossum6ac258d1993-05-12 08:24:20 +0000764 res = (*v->ob_type->tp_as_mapping->mp_length)(v);
Guido van Rossum1c4f4581998-05-22 00:53:24 +0000765 else if (v->ob_type->tp_as_sequence != NULL &&
766 v->ob_type->tp_as_sequence->sq_length != NULL)
Guido van Rossum6ac258d1993-05-12 08:24:20 +0000767 res = (*v->ob_type->tp_as_sequence->sq_length)(v);
768 else
769 res = 1;
770 if (res > 0)
771 res = 1;
772 return res;
773}
774
Guido van Rossumc3d3f961998-04-09 17:53:59 +0000775/* equivalent of 'not v'
776 Return -1 if an error occurred */
777
778int
Fred Drake100814d2000-07-09 15:48:49 +0000779PyObject_Not(PyObject *v)
Guido van Rossumc3d3f961998-04-09 17:53:59 +0000780{
781 int res;
782 res = PyObject_IsTrue(v);
783 if (res < 0)
784 return res;
785 return res == 0;
786}
787
Guido van Rossum5524a591995-01-10 15:26:20 +0000788/* Coerce two numeric types to the "larger" one.
789 Increment the reference count on each argument.
790 Return -1 and raise an exception if no coercion is possible
791 (and then no reference count is incremented).
792*/
793
794int
Fred Drake100814d2000-07-09 15:48:49 +0000795PyNumber_CoerceEx(PyObject **pv, PyObject **pw)
Guido van Rossum5524a591995-01-10 15:26:20 +0000796{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000797 register PyObject *v = *pv;
798 register PyObject *w = *pw;
Guido van Rossum5524a591995-01-10 15:26:20 +0000799 int res;
800
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000801 if (v->ob_type == w->ob_type && !PyInstance_Check(v)) {
802 Py_INCREF(v);
803 Py_INCREF(w);
Guido van Rossum5524a591995-01-10 15:26:20 +0000804 return 0;
805 }
806 if (v->ob_type->tp_as_number && v->ob_type->tp_as_number->nb_coerce) {
807 res = (*v->ob_type->tp_as_number->nb_coerce)(pv, pw);
808 if (res <= 0)
809 return res;
810 }
811 if (w->ob_type->tp_as_number && w->ob_type->tp_as_number->nb_coerce) {
812 res = (*w->ob_type->tp_as_number->nb_coerce)(pw, pv);
813 if (res <= 0)
814 return res;
815 }
Guido van Rossum242c6421997-11-19 16:03:17 +0000816 return 1;
817}
818
819int
Fred Drake100814d2000-07-09 15:48:49 +0000820PyNumber_Coerce(PyObject **pv, PyObject **pw)
Guido van Rossum242c6421997-11-19 16:03:17 +0000821{
822 int err = PyNumber_CoerceEx(pv, pw);
823 if (err <= 0)
824 return err;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000825 PyErr_SetString(PyExc_TypeError, "number coercion failed");
Guido van Rossum5524a591995-01-10 15:26:20 +0000826 return -1;
827}
828
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000829
Guido van Rossum49b11fe1995-01-26 00:38:22 +0000830/* Test whether an object can be called */
831
832int
Fred Drake100814d2000-07-09 15:48:49 +0000833PyCallable_Check(PyObject *x)
Guido van Rossum49b11fe1995-01-26 00:38:22 +0000834{
835 if (x == NULL)
836 return 0;
837 if (x->ob_type->tp_call != NULL ||
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000838 PyFunction_Check(x) ||
839 PyMethod_Check(x) ||
840 PyCFunction_Check(x) ||
841 PyClass_Check(x))
Guido van Rossum49b11fe1995-01-26 00:38:22 +0000842 return 1;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000843 if (PyInstance_Check(x)) {
844 PyObject *call = PyObject_GetAttrString(x, "__call__");
Guido van Rossum49b11fe1995-01-26 00:38:22 +0000845 if (call == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000846 PyErr_Clear();
Guido van Rossum49b11fe1995-01-26 00:38:22 +0000847 return 0;
848 }
849 /* Could test recursively but don't, for fear of endless
850 recursion if some joker sets self.__call__ = self */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000851 Py_DECREF(call);
Guido van Rossum49b11fe1995-01-26 00:38:22 +0000852 return 1;
853 }
854 return 0;
855}
856
857
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000858/*
859NoObject is usable as a non-NULL undefined value, used by the macro None.
860There is (and should be!) no way to create other objects of this type,
Guido van Rossum3f5da241990-12-20 15:06:42 +0000861so there is exactly one (which is indestructible, by the way).
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000862*/
863
Guido van Rossum0c182a11992-03-27 17:26:13 +0000864/* ARGSUSED */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000865static PyObject *
Fred Drake100814d2000-07-09 15:48:49 +0000866none_repr(PyObject *op)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000867{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000868 return PyString_FromString("None");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000869}
870
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000871static PyTypeObject PyNothing_Type = {
872 PyObject_HEAD_INIT(&PyType_Type)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000873 0,
Guido van Rossum3f5da241990-12-20 15:06:42 +0000874 "None",
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000875 0,
876 0,
877 0, /*tp_dealloc*/ /*never called*/
Guido van Rossum7066dd71992-09-17 17:54:56 +0000878 0, /*tp_print*/
Guido van Rossum3f5da241990-12-20 15:06:42 +0000879 0, /*tp_getattr*/
880 0, /*tp_setattr*/
881 0, /*tp_compare*/
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000882 (reprfunc)none_repr, /*tp_repr*/
Guido van Rossum3f5da241990-12-20 15:06:42 +0000883 0, /*tp_as_number*/
884 0, /*tp_as_sequence*/
885 0, /*tp_as_mapping*/
Guido van Rossum9bfef441993-03-29 10:43:31 +0000886 0, /*tp_hash */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000887};
888
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000889PyObject _Py_NoneStruct = {
890 PyObject_HEAD_INIT(&PyNothing_Type)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000891};
892
893
Guido van Rossum84a90321996-05-22 16:34:47 +0000894#ifdef Py_TRACE_REFS
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000895
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000896static PyObject refchain = {&refchain, &refchain};
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000897
Guido van Rossumaacdc9d1996-08-12 21:32:12 +0000898void
Fred Drake100814d2000-07-09 15:48:49 +0000899_Py_ResetReferences(void)
Guido van Rossume09fb551997-08-05 02:04:34 +0000900{
901 refchain._ob_prev = refchain._ob_next = &refchain;
902 _Py_RefTotal = 0;
903}
904
905void
Fred Drake100814d2000-07-09 15:48:49 +0000906_Py_NewReference(PyObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000907{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000908 _Py_RefTotal++;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000909 op->ob_refcnt = 1;
910 op->_ob_next = refchain._ob_next;
911 op->_ob_prev = &refchain;
912 refchain._ob_next->_ob_prev = op;
913 refchain._ob_next = op;
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +0000914#ifdef COUNT_ALLOCS
915 inc_count(op->ob_type);
916#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000917}
918
Guido van Rossumaacdc9d1996-08-12 21:32:12 +0000919void
Fred Drake100814d2000-07-09 15:48:49 +0000920_Py_ForgetReference(register PyObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000921{
Guido van Rossumbffd6832000-01-20 22:32:56 +0000922#ifdef SLOW_UNREF_CHECK
Guido van Rossum4c08d552000-03-10 22:55:18 +0000923 register PyObject *p;
Guido van Rossumbffd6832000-01-20 22:32:56 +0000924#endif
Guido van Rossumd7047b31995-01-02 19:07:15 +0000925 if (op->ob_refcnt < 0)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000926 Py_FatalError("UNREF negative refcnt");
Guido van Rossum2e8f6141992-09-03 20:32:55 +0000927 if (op == &refchain ||
Guido van Rossumd7047b31995-01-02 19:07:15 +0000928 op->_ob_prev->_ob_next != op || op->_ob_next->_ob_prev != op)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000929 Py_FatalError("UNREF invalid object");
Guido van Rossum2e8f6141992-09-03 20:32:55 +0000930#ifdef SLOW_UNREF_CHECK
Guido van Rossum3f5da241990-12-20 15:06:42 +0000931 for (p = refchain._ob_next; p != &refchain; p = p->_ob_next) {
932 if (p == op)
933 break;
934 }
Guido van Rossumd7047b31995-01-02 19:07:15 +0000935 if (p == &refchain) /* Not found */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000936 Py_FatalError("UNREF unknown object");
Guido van Rossum2e8f6141992-09-03 20:32:55 +0000937#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000938 op->_ob_next->_ob_prev = op->_ob_prev;
939 op->_ob_prev->_ob_next = op->_ob_next;
Guido van Rossum2e8f6141992-09-03 20:32:55 +0000940 op->_ob_next = op->_ob_prev = NULL;
Guido van Rossumd8953cb1995-04-06 14:46:26 +0000941#ifdef COUNT_ALLOCS
942 op->ob_type->tp_free++;
943#endif
Guido van Rossum3f5da241990-12-20 15:06:42 +0000944}
945
Guido van Rossumaacdc9d1996-08-12 21:32:12 +0000946void
Fred Drake100814d2000-07-09 15:48:49 +0000947_Py_Dealloc(PyObject *op)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000948{
Guido van Rossum9776adf1994-09-07 14:36:45 +0000949 destructor dealloc = op->ob_type->tp_dealloc;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000950 _Py_ForgetReference(op);
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000951#ifndef WITH_CYCLE_GC
Guido van Rossume92e6102000-04-24 15:40:53 +0000952 if (_PyTrash_delete_nesting < PyTrash_UNWIND_LEVEL-1)
953 op->ob_type = NULL;
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000954#endif
Guido van Rossum9776adf1994-09-07 14:36:45 +0000955 (*dealloc)(op);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000956}
957
Guido van Rossumaacdc9d1996-08-12 21:32:12 +0000958void
Fred Drake100814d2000-07-09 15:48:49 +0000959_Py_PrintReferences(FILE *fp)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000960{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000961 PyObject *op;
Guido van Rossume09fb551997-08-05 02:04:34 +0000962 fprintf(fp, "Remaining objects:\n");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000963 for (op = refchain._ob_next; op != &refchain; op = op->_ob_next) {
964 fprintf(fp, "[%d] ", op->ob_refcnt);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000965 if (PyObject_Print(op, fp, 0) != 0)
966 PyErr_Clear();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000967 putc('\n', fp);
968 }
969}
970
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000971PyObject *
Fred Drake100814d2000-07-09 15:48:49 +0000972_Py_GetObjects(PyObject *self, PyObject *args)
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000973{
974 int i, n;
975 PyObject *t = NULL;
976 PyObject *res, *op;
977
978 if (!PyArg_ParseTuple(args, "i|O", &n, &t))
979 return NULL;
980 op = refchain._ob_next;
981 res = PyList_New(0);
982 if (res == NULL)
983 return NULL;
984 for (i = 0; (n == 0 || i < n) && op != &refchain; i++) {
985 while (op == self || op == args || op == res || op == t ||
986 t != NULL && op->ob_type != (PyTypeObject *) t) {
987 op = op->_ob_next;
988 if (op == &refchain)
989 return res;
990 }
991 if (PyList_Append(res, op) < 0) {
992 Py_DECREF(res);
993 return NULL;
994 }
995 op = op->_ob_next;
996 }
997 return res;
998}
999
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001000#endif
Guido van Rossum97ead3f1996-01-12 01:24:09 +00001001
1002
1003/* Hack to force loading of cobject.o */
Guido van Rossumda9c2711996-12-05 21:58:58 +00001004PyTypeObject *_Py_cobject_hack = &PyCObject_Type;
Guido van Rossum84a90321996-05-22 16:34:47 +00001005
1006
1007/* Hack to force loading of abstract.o */
Jeremy Hylton6253f832000-07-12 12:56:19 +00001008int (*_Py_abstract_hack)(PyObject *) = &PyObject_Size;
Guido van Rossume09fb551997-08-05 02:04:34 +00001009
1010
Andrew M. Kuchling1582a3a2000-08-16 12:27:23 +00001011/* Python's malloc wrappers (see pymem.h) */
Guido van Rossume09fb551997-08-05 02:04:34 +00001012
Thomas Wouters334fb892000-07-25 12:56:38 +00001013void *
Fred Drake100814d2000-07-09 15:48:49 +00001014PyMem_Malloc(size_t nbytes)
Guido van Rossume09fb551997-08-05 02:04:34 +00001015{
1016#if _PyMem_EXTRA > 0
1017 if (nbytes == 0)
1018 nbytes = _PyMem_EXTRA;
1019#endif
Guido van Rossumb18618d2000-05-03 23:44:39 +00001020 return PyMem_MALLOC(nbytes);
Guido van Rossume09fb551997-08-05 02:04:34 +00001021}
1022
Thomas Wouters334fb892000-07-25 12:56:38 +00001023void *
1024PyMem_Realloc(void *p, size_t nbytes)
Guido van Rossume09fb551997-08-05 02:04:34 +00001025{
1026#if _PyMem_EXTRA > 0
1027 if (nbytes == 0)
1028 nbytes = _PyMem_EXTRA;
1029#endif
Guido van Rossumb18618d2000-05-03 23:44:39 +00001030 return PyMem_REALLOC(p, nbytes);
Guido van Rossume09fb551997-08-05 02:04:34 +00001031}
1032
1033void
Thomas Wouters334fb892000-07-25 12:56:38 +00001034PyMem_Free(void *p)
Guido van Rossume09fb551997-08-05 02:04:34 +00001035{
Guido van Rossumb18618d2000-05-03 23:44:39 +00001036 PyMem_FREE(p);
1037}
1038
1039
1040/* Python's object malloc wrappers (see objimpl.h) */
1041
Thomas Wouters334fb892000-07-25 12:56:38 +00001042void *
Fred Drake100814d2000-07-09 15:48:49 +00001043PyObject_Malloc(size_t nbytes)
Guido van Rossumb18618d2000-05-03 23:44:39 +00001044{
1045 return PyObject_MALLOC(nbytes);
1046}
1047
Thomas Wouters334fb892000-07-25 12:56:38 +00001048void *
1049PyObject_Realloc(void *p, size_t nbytes)
Guido van Rossumb18618d2000-05-03 23:44:39 +00001050{
1051 return PyObject_REALLOC(p, nbytes);
1052}
1053
1054void
Thomas Wouters334fb892000-07-25 12:56:38 +00001055PyObject_Free(void *p)
Guido van Rossumb18618d2000-05-03 23:44:39 +00001056{
1057 PyObject_FREE(p);
Guido van Rossume09fb551997-08-05 02:04:34 +00001058}
Guido van Rossum86610361998-04-10 22:32:46 +00001059
1060
1061/* These methods are used to control infinite recursion in repr, str, print,
1062 etc. Container objects that may recursively contain themselves,
1063 e.g. builtin dictionaries and lists, should used Py_ReprEnter() and
1064 Py_ReprLeave() to avoid infinite recursion.
1065
1066 Py_ReprEnter() returns 0 the first time it is called for a particular
1067 object and 1 every time thereafter. It returns -1 if an exception
1068 occurred. Py_ReprLeave() has no return value.
1069
1070 See dictobject.c and listobject.c for examples of use.
1071*/
1072
1073#define KEY "Py_Repr"
1074
1075int
Fred Drake100814d2000-07-09 15:48:49 +00001076Py_ReprEnter(PyObject *obj)
Guido van Rossum86610361998-04-10 22:32:46 +00001077{
1078 PyObject *dict;
1079 PyObject *list;
1080 int i;
1081
1082 dict = PyThreadState_GetDict();
1083 if (dict == NULL)
1084 return -1;
1085 list = PyDict_GetItemString(dict, KEY);
1086 if (list == NULL) {
1087 list = PyList_New(0);
1088 if (list == NULL)
1089 return -1;
1090 if (PyDict_SetItemString(dict, KEY, list) < 0)
1091 return -1;
1092 Py_DECREF(list);
1093 }
1094 i = PyList_GET_SIZE(list);
1095 while (--i >= 0) {
1096 if (PyList_GET_ITEM(list, i) == obj)
1097 return 1;
1098 }
1099 PyList_Append(list, obj);
1100 return 0;
1101}
1102
1103void
Fred Drake100814d2000-07-09 15:48:49 +00001104Py_ReprLeave(PyObject *obj)
Guido van Rossum86610361998-04-10 22:32:46 +00001105{
1106 PyObject *dict;
1107 PyObject *list;
1108 int i;
1109
1110 dict = PyThreadState_GetDict();
Guido van Rossumeb909461998-04-11 15:17:34 +00001111 if (dict == NULL)
1112 return;
Guido van Rossum86610361998-04-10 22:32:46 +00001113 list = PyDict_GetItemString(dict, KEY);
Guido van Rossumeb909461998-04-11 15:17:34 +00001114 if (list == NULL || !PyList_Check(list))
1115 return;
Guido van Rossum86610361998-04-10 22:32:46 +00001116 i = PyList_GET_SIZE(list);
1117 /* Count backwards because we always expect obj to be list[-1] */
1118 while (--i >= 0) {
1119 if (PyList_GET_ITEM(list, i) == obj) {
1120 PyList_SetSlice(list, i, i + 1, NULL);
1121 break;
1122 }
1123 }
1124}
Guido van Rossumd724b232000-03-13 16:01:29 +00001125
1126/*
1127 trashcan
1128 CT 2k0130
1129 non-recursively destroy nested objects
1130
1131 CT 2k0223
1132 everything is now done in a macro.
1133
1134 CT 2k0305
1135 modified to use functions, after Tim Peter's suggestion.
1136
1137 CT 2k0309
1138 modified to restore a possible error.
Guido van Rossum13ff8eb2000-03-25 18:39:19 +00001139
1140 CT 2k0325
1141 added better safe than sorry check for threadstate
Guido van Rossume92e6102000-04-24 15:40:53 +00001142
1143 CT 2k0422
1144 complete rewrite. We now build a chain via ob_type
1145 and save the limited number of types in ob_refcnt.
1146 This is perfect since we don't need any memory.
1147 A patch for free-threading would need just a lock.
Guido van Rossumd724b232000-03-13 16:01:29 +00001148*/
1149
Guido van Rossume92e6102000-04-24 15:40:53 +00001150#define Py_TRASHCAN_TUPLE 1
1151#define Py_TRASHCAN_LIST 2
1152#define Py_TRASHCAN_DICT 3
1153#define Py_TRASHCAN_FRAME 4
1154#define Py_TRASHCAN_TRACEBACK 5
1155/* extend here if other objects want protection */
1156
Guido van Rossumd724b232000-03-13 16:01:29 +00001157int _PyTrash_delete_nesting = 0;
Guido van Rossume92e6102000-04-24 15:40:53 +00001158
Guido van Rossumd724b232000-03-13 16:01:29 +00001159PyObject * _PyTrash_delete_later = NULL;
1160
1161void
Fred Drake100814d2000-07-09 15:48:49 +00001162_PyTrash_deposit_object(PyObject *op)
Guido van Rossumd724b232000-03-13 16:01:29 +00001163{
Guido van Rossume92e6102000-04-24 15:40:53 +00001164 int typecode;
Guido van Rossum13ff8eb2000-03-25 18:39:19 +00001165
Guido van Rossume92e6102000-04-24 15:40:53 +00001166 if (PyTuple_Check(op))
1167 typecode = Py_TRASHCAN_TUPLE;
1168 else if (PyList_Check(op))
1169 typecode = Py_TRASHCAN_LIST;
1170 else if (PyDict_Check(op))
1171 typecode = Py_TRASHCAN_DICT;
1172 else if (PyFrame_Check(op))
1173 typecode = Py_TRASHCAN_FRAME;
1174 else if (PyTraceBack_Check(op))
1175 typecode = Py_TRASHCAN_TRACEBACK;
Moshe Zadkacf703f02000-08-04 15:36:13 +00001176 else /* We have a bug here -- those are the only types in GC */ {
1177 Py_FatalError("Type not supported in GC -- internal bug");
1178 return; /* pacify compiler -- execution never here */
1179 }
Guido van Rossume92e6102000-04-24 15:40:53 +00001180 op->ob_refcnt = typecode;
Guido van Rossumd724b232000-03-13 16:01:29 +00001181
Guido van Rossume92e6102000-04-24 15:40:53 +00001182 op->ob_type = (PyTypeObject*)_PyTrash_delete_later;
1183 _PyTrash_delete_later = op;
Guido van Rossumd724b232000-03-13 16:01:29 +00001184}
1185
1186void
Fred Drake100814d2000-07-09 15:48:49 +00001187_PyTrash_destroy_chain(void)
Guido van Rossumd724b232000-03-13 16:01:29 +00001188{
1189 while (_PyTrash_delete_later) {
1190 PyObject *shredder = _PyTrash_delete_later;
Guido van Rossume92e6102000-04-24 15:40:53 +00001191 _PyTrash_delete_later = (PyObject*) shredder->ob_type;
1192
1193 switch (shredder->ob_refcnt) {
1194 case Py_TRASHCAN_TUPLE:
1195 shredder->ob_type = &PyTuple_Type;
1196 break;
1197 case Py_TRASHCAN_LIST:
1198 shredder->ob_type = &PyList_Type;
1199 break;
1200 case Py_TRASHCAN_DICT:
1201 shredder->ob_type = &PyDict_Type;
1202 break;
1203 case Py_TRASHCAN_FRAME:
1204 shredder->ob_type = &PyFrame_Type;
1205 break;
1206 case Py_TRASHCAN_TRACEBACK:
1207 shredder->ob_type = &PyTraceBack_Type;
1208 break;
1209 }
1210 _Py_NewReference(shredder);
1211
Guido van Rossumd724b232000-03-13 16:01:29 +00001212 ++_PyTrash_delete_nesting;
1213 Py_DECREF(shredder);
1214 --_PyTrash_delete_nesting;
1215 }
1216}