blob: 33239068045b58038758242c87ee96173dab4fac [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001
Guido van Rossum3f5da241990-12-20 15:06:42 +00002/* Generic object operations; and implementation of None (NoObject) */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003
Guido van Rossumc0b618a1997-05-02 03:12:38 +00004#include "Python.h"
Jack Jansend49cbe12000-08-22 21:52:51 +00005#ifdef HAVE_LIMITS_H
6#include <limits.h>
7#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00008
Jack Jansen28fc8802000-07-11 21:47:20 +00009#ifdef macintosh
10#include "macglue.h"
11#endif
12
Guido van Rossume92e6102000-04-24 15:40:53 +000013/* just for trashcan: */
14#include "compile.h"
15#include "frameobject.h"
16#include "traceback.h"
17
Guido van Rossum6f9e4331995-03-29 16:57:48 +000018#if defined( Py_TRACE_REFS ) || defined( Py_REF_DEBUG )
Guido van Rossumbffd6832000-01-20 22:32:56 +000019DL_IMPORT(long) _Py_RefTotal;
Guido van Rossum3f5da241990-12-20 15:06:42 +000020#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000021
Guido van Rossum3f5da241990-12-20 15:06:42 +000022/* Object allocation routines used by NEWOBJ and NEWVAROBJ macros.
23 These are used by the individual routines for object creation.
24 Do not call them otherwise, they do not initialize the object! */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000025
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +000026#ifdef COUNT_ALLOCS
Guido van Rossumc0b618a1997-05-02 03:12:38 +000027static PyTypeObject *type_list;
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +000028extern int tuple_zero_allocs, fast_tuple_allocs;
29extern int quick_int_allocs, quick_neg_int_allocs;
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +000030extern int null_strings, one_strings;
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +000031void
Fred Drake100814d2000-07-09 15:48:49 +000032dump_counts(void)
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +000033{
Guido van Rossumc0b618a1997-05-02 03:12:38 +000034 PyTypeObject *tp;
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +000035
36 for (tp = type_list; tp; tp = tp->tp_next)
Sjoerd Mullender52c1f511993-10-25 08:40:52 +000037 fprintf(stderr, "%s alloc'd: %d, freed: %d, max in use: %d\n",
38 tp->tp_name, tp->tp_alloc, tp->tp_free,
39 tp->tp_maxalloc);
40 fprintf(stderr, "fast tuple allocs: %d, empty: %d\n",
41 fast_tuple_allocs, tuple_zero_allocs);
42 fprintf(stderr, "fast int allocs: pos: %d, neg: %d\n",
43 quick_int_allocs, quick_neg_int_allocs);
44 fprintf(stderr, "null strings: %d, 1-strings: %d\n",
45 null_strings, one_strings);
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +000046}
47
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +000048PyObject *
Fred Drake100814d2000-07-09 15:48:49 +000049get_counts(void)
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +000050{
51 PyTypeObject *tp;
52 PyObject *result;
53 PyObject *v;
54
55 result = PyList_New(0);
56 if (result == NULL)
57 return NULL;
58 for (tp = type_list; tp; tp = tp->tp_next) {
59 v = Py_BuildValue("(siii)", tp->tp_name, tp->tp_alloc,
60 tp->tp_free, tp->tp_maxalloc);
61 if (v == NULL) {
62 Py_DECREF(result);
63 return NULL;
64 }
65 if (PyList_Append(result, v) < 0) {
66 Py_DECREF(v);
67 Py_DECREF(result);
68 return NULL;
69 }
70 Py_DECREF(v);
71 }
72 return result;
73}
74
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +000075void
Fred Drake100814d2000-07-09 15:48:49 +000076inc_count(PyTypeObject *tp)
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +000077{
78 if (tp->tp_alloc == 0) {
Guido van Rossumd8953cb1995-04-06 14:46:26 +000079 /* first time; insert in linked list */
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +000080 if (tp->tp_next != NULL) /* sanity check */
Guido van Rossumc0b618a1997-05-02 03:12:38 +000081 Py_FatalError("XXX inc_count sanity check");
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +000082 tp->tp_next = type_list;
83 type_list = tp;
84 }
85 tp->tp_alloc++;
86 if (tp->tp_alloc - tp->tp_free > tp->tp_maxalloc)
87 tp->tp_maxalloc = tp->tp_alloc - tp->tp_free;
88}
89#endif
90
Guido van Rossumc0b618a1997-05-02 03:12:38 +000091PyObject *
Fred Drake100814d2000-07-09 15:48:49 +000092PyObject_Init(PyObject *op, PyTypeObject *tp)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000093{
Guido van Rossumb18618d2000-05-03 23:44:39 +000094 if (op == NULL) {
95 PyErr_SetString(PyExc_SystemError,
96 "NULL object passed to PyObject_Init");
97 return op;
98 }
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +000099#ifdef WITH_CYCLE_GC
100 if (PyType_IS_GC(tp))
101 op = (PyObject *) PyObject_FROM_GC(op);
102#endif
Guido van Rossumb18618d2000-05-03 23:44:39 +0000103 /* Any changes should be reflected in PyObject_INIT (objimpl.h) */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000104 op->ob_type = tp;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000105 _Py_NewReference(op);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000106 return op;
107}
108
Guido van Rossumb18618d2000-05-03 23:44:39 +0000109PyVarObject *
Fred Drake100814d2000-07-09 15:48:49 +0000110PyObject_InitVar(PyVarObject *op, PyTypeObject *tp, int size)
Guido van Rossumb18618d2000-05-03 23:44:39 +0000111{
112 if (op == NULL) {
113 PyErr_SetString(PyExc_SystemError,
114 "NULL object passed to PyObject_InitVar");
115 return op;
116 }
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000117#ifdef WITH_CYCLE_GC
118 if (PyType_IS_GC(tp))
119 op = (PyVarObject *) PyObject_FROM_GC(op);
120#endif
Guido van Rossumb18618d2000-05-03 23:44:39 +0000121 /* Any changes should be reflected in PyObject_INIT_VAR */
122 op->ob_size = size;
123 op->ob_type = tp;
124 _Py_NewReference((PyObject *)op);
125 return op;
126}
127
128PyObject *
Fred Drake100814d2000-07-09 15:48:49 +0000129_PyObject_New(PyTypeObject *tp)
Guido van Rossumb18618d2000-05-03 23:44:39 +0000130{
131 PyObject *op;
132 op = (PyObject *) PyObject_MALLOC(_PyObject_SIZE(tp));
133 if (op == NULL)
134 return PyErr_NoMemory();
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000135#ifdef WITH_CYCLE_GC
136 if (PyType_IS_GC(tp))
137 op = (PyObject *) PyObject_FROM_GC(op);
138#endif
Guido van Rossumb18618d2000-05-03 23:44:39 +0000139 return PyObject_INIT(op, tp);
140}
141
Guido van Rossumd0c87ee1997-05-15 21:31:03 +0000142PyVarObject *
Fred Drake100814d2000-07-09 15:48:49 +0000143_PyObject_NewVar(PyTypeObject *tp, int size)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000144{
Guido van Rossumb18618d2000-05-03 23:44:39 +0000145 PyVarObject *op;
146 op = (PyVarObject *) PyObject_MALLOC(_PyObject_VAR_SIZE(tp, size));
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000147 if (op == NULL)
Guido van Rossumd0c87ee1997-05-15 21:31:03 +0000148 return (PyVarObject *)PyErr_NoMemory();
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000149#ifdef WITH_CYCLE_GC
150 if (PyType_IS_GC(tp))
151 op = (PyVarObject *) PyObject_FROM_GC(op);
152#endif
Guido van Rossumb18618d2000-05-03 23:44:39 +0000153 return PyObject_INIT_VAR(op, tp, size);
154}
155
156void
Fred Drake100814d2000-07-09 15:48:49 +0000157_PyObject_Del(PyObject *op)
Guido van Rossumb18618d2000-05-03 23:44:39 +0000158{
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000159#ifdef WITH_CYCLE_GC
Guido van Rossum4cc6ac72000-07-01 01:00:38 +0000160 if (op && PyType_IS_GC(op->ob_type)) {
161 op = (PyObject *) PyObject_AS_GC(op);
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000162 }
Guido van Rossum4cc6ac72000-07-01 01:00:38 +0000163#endif
164 PyObject_FREE(op);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000165}
166
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000167#ifndef WITH_CYCLE_GC
168/* extension modules might need these */
169void _PyGC_Insert(PyObject *op) { }
170void _PyGC_Remove(PyObject *op) { }
171#endif
172
Guido van Rossum90933611991-06-07 16:10:43 +0000173int
Fred Drake100814d2000-07-09 15:48:49 +0000174PyObject_Print(PyObject *op, FILE *fp, int flags)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000175{
Guido van Rossum278ef591991-07-27 21:40:24 +0000176 int ret = 0;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000177 if (PyErr_CheckSignals())
Guido van Rossum90933611991-06-07 16:10:43 +0000178 return -1;
Guido van Rossum9b00dfa1998-04-28 16:06:54 +0000179#ifdef USE_STACKCHECK
180 if (PyOS_CheckStack()) {
181 PyErr_SetString(PyExc_MemoryError, "Stack overflow");
182 return -1;
183 }
184#endif
Guido van Rossum687ef6e2000-01-12 16:28:58 +0000185 clearerr(fp); /* Clear any previous error condition */
Guido van Rossum90933611991-06-07 16:10:43 +0000186 if (op == NULL) {
187 fprintf(fp, "<nil>");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000188 }
Guido van Rossum90933611991-06-07 16:10:43 +0000189 else {
190 if (op->ob_refcnt <= 0)
Fred Drakea44d3532000-06-30 15:01:00 +0000191 fprintf(fp, "<refcnt %u at %p>",
192 op->ob_refcnt, op);
Guido van Rossum2e8f6141992-09-03 20:32:55 +0000193 else if (op->ob_type->tp_print == NULL) {
194 if (op->ob_type->tp_repr == NULL) {
Fred Drakea44d3532000-06-30 15:01:00 +0000195 fprintf(fp, "<%s object at %p>",
196 op->ob_type->tp_name, op);
Guido van Rossum2e8f6141992-09-03 20:32:55 +0000197 }
198 else {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000199 PyObject *s;
200 if (flags & Py_PRINT_RAW)
201 s = PyObject_Str(op);
Guido van Rossumc6004111993-11-05 10:22:19 +0000202 else
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000203 s = PyObject_Repr(op);
Guido van Rossum2e8f6141992-09-03 20:32:55 +0000204 if (s == NULL)
205 ret = -1;
Guido van Rossum2e8f6141992-09-03 20:32:55 +0000206 else {
Guido van Rossum565798d1998-04-21 22:25:01 +0000207 ret = PyObject_Print(s, fp,
208 Py_PRINT_RAW);
Guido van Rossum2e8f6141992-09-03 20:32:55 +0000209 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000210 Py_XDECREF(s);
Guido van Rossum2e8f6141992-09-03 20:32:55 +0000211 }
212 }
Guido van Rossum90933611991-06-07 16:10:43 +0000213 else
Guido van Rossum278ef591991-07-27 21:40:24 +0000214 ret = (*op->ob_type->tp_print)(op, fp, flags);
Guido van Rossum90933611991-06-07 16:10:43 +0000215 }
Guido van Rossum278ef591991-07-27 21:40:24 +0000216 if (ret == 0) {
217 if (ferror(fp)) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000218 PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossum278ef591991-07-27 21:40:24 +0000219 clearerr(fp);
220 ret = -1;
221 }
222 }
223 return ret;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000224}
225
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000226PyObject *
Fred Drake100814d2000-07-09 15:48:49 +0000227PyObject_Repr(PyObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000228{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000229 if (PyErr_CheckSignals())
Guido van Rossum90933611991-06-07 16:10:43 +0000230 return NULL;
Guido van Rossum9b00dfa1998-04-28 16:06:54 +0000231#ifdef USE_STACKCHECK
232 if (PyOS_CheckStack()) {
233 PyErr_SetString(PyExc_MemoryError, "Stack overflow");
234 return NULL;
235 }
236#endif
Guido van Rossum90933611991-06-07 16:10:43 +0000237 if (v == NULL)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000238 return PyString_FromString("<NULL>");
Guido van Rossum90933611991-06-07 16:10:43 +0000239 else if (v->ob_type->tp_repr == NULL) {
240 char buf[120];
Fred Drakea44d3532000-06-30 15:01:00 +0000241 sprintf(buf, "<%.80s object at %p>",
242 v->ob_type->tp_name, v);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000243 return PyString_FromString(buf);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000244 }
Guido van Rossum4c08d552000-03-10 22:55:18 +0000245 else {
246 PyObject *res;
247 res = (*v->ob_type->tp_repr)(v);
248 if (res == NULL)
249 return NULL;
Fredrik Lundhefecc7d2000-07-01 14:31:09 +0000250 if (PyUnicode_Check(res)) {
251 PyObject* str;
Fredrik Lundh2a1e0602000-07-08 17:43:32 +0000252 str = PyUnicode_AsUnicodeEscapeString(res);
Marc-André Lemburg891bc652000-07-03 09:57:53 +0000253 Py_DECREF(res);
254 if (str)
Fredrik Lundhefecc7d2000-07-01 14:31:09 +0000255 res = str;
Marc-André Lemburg891bc652000-07-03 09:57:53 +0000256 else
257 return NULL;
Fredrik Lundhefecc7d2000-07-01 14:31:09 +0000258 }
Guido van Rossum4c08d552000-03-10 22:55:18 +0000259 if (!PyString_Check(res)) {
260 PyErr_Format(PyExc_TypeError,
Guido van Rossum5db862d2000-04-10 12:46:51 +0000261 "__repr__ returned non-string (type %.200s)",
Guido van Rossum4c08d552000-03-10 22:55:18 +0000262 res->ob_type->tp_name);
263 Py_DECREF(res);
264 return NULL;
265 }
266 return res;
267 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000268}
269
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000270PyObject *
Fred Drake100814d2000-07-09 15:48:49 +0000271PyObject_Str(PyObject *v)
Guido van Rossumc6004111993-11-05 10:22:19 +0000272{
Guido van Rossum4c08d552000-03-10 22:55:18 +0000273 PyObject *res;
274
Guido van Rossumc6004111993-11-05 10:22:19 +0000275 if (v == NULL)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000276 return PyString_FromString("<NULL>");
277 else if (PyString_Check(v)) {
278 Py_INCREF(v);
Guido van Rossumc6004111993-11-05 10:22:19 +0000279 return v;
280 }
Guido van Rossum32b582b1995-01-17 16:35:13 +0000281 else if (v->ob_type->tp_str != NULL)
Guido van Rossum4c08d552000-03-10 22:55:18 +0000282 res = (*v->ob_type->tp_str)(v);
Guido van Rossumc6004111993-11-05 10:22:19 +0000283 else {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000284 PyObject *func;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000285 if (!PyInstance_Check(v) ||
286 (func = PyObject_GetAttrString(v, "__str__")) == NULL) {
287 PyErr_Clear();
288 return PyObject_Repr(v);
Guido van Rossumc6004111993-11-05 10:22:19 +0000289 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000290 res = PyEval_CallObject(func, (PyObject *)NULL);
291 Py_DECREF(func);
Guido van Rossumc6004111993-11-05 10:22:19 +0000292 }
Guido van Rossum4c08d552000-03-10 22:55:18 +0000293 if (res == NULL)
294 return NULL;
Marc-André Lemburg891bc652000-07-03 09:57:53 +0000295 if (PyUnicode_Check(res)) {
296 PyObject* str;
297 str = PyUnicode_AsEncodedString(res, NULL, NULL);
298 Py_DECREF(res);
299 if (str)
300 res = str;
301 else
302 return NULL;
303 }
Guido van Rossum4c08d552000-03-10 22:55:18 +0000304 if (!PyString_Check(res)) {
305 PyErr_Format(PyExc_TypeError,
Guido van Rossum5db862d2000-04-10 12:46:51 +0000306 "__str__ returned non-string (type %.200s)",
Guido van Rossum4c08d552000-03-10 22:55:18 +0000307 res->ob_type->tp_name);
308 Py_DECREF(res);
309 return NULL;
310 }
311 return res;
Guido van Rossumc6004111993-11-05 10:22:19 +0000312}
313
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000314static PyObject *
Fred Drake100814d2000-07-09 15:48:49 +0000315do_cmp(PyObject *v, PyObject *w)
Guido van Rossum20566841995-01-12 11:26:10 +0000316{
Guido van Rossumc8b6df91997-05-23 00:06:51 +0000317 long c;
Guido van Rossum20566841995-01-12 11:26:10 +0000318 /* __rcmp__ actually won't be called unless __cmp__ isn't defined,
319 because the check in cmpobject() reverses the objects first.
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000320 This is intentional -- it makes no sense to define cmp(x,y)
321 different than -cmp(y,x). */
322 if (PyInstance_Check(v) || PyInstance_Check(w))
323 return PyInstance_DoBinOp(v, w, "__cmp__", "__rcmp__", do_cmp);
Guido van Rossumc8b6df91997-05-23 00:06:51 +0000324 c = PyObject_Compare(v, w);
325 if (c && PyErr_Occurred())
326 return NULL;
327 return PyInt_FromLong(c);
Guido van Rossum20566841995-01-12 11:26:10 +0000328}
329
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000330PyObject *_PyCompareState_Key;
331
Thomas Wouters7e474022000-07-16 12:04:32 +0000332/* _PyCompareState_nesting is incremented before calling compare (for
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000333 some types) and decremented on exit. If the count exceeds the
334 nesting limit, enable code to detect circular data structures.
335*/
Jack Jansend49cbe12000-08-22 21:52:51 +0000336#ifdef macintosh
337#define NESTING_LIMIT 60
338#else
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000339#define NESTING_LIMIT 500
Jack Jansend49cbe12000-08-22 21:52:51 +0000340#endif
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000341int _PyCompareState_nesting = 0;
342
343static PyObject*
Fred Drake100814d2000-07-09 15:48:49 +0000344get_inprogress_dict(void)
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000345{
346 PyObject *tstate_dict, *inprogress;
347
348 tstate_dict = PyThreadState_GetDict();
349 if (tstate_dict == NULL) {
350 PyErr_BadInternalCall();
351 return NULL;
352 }
353 inprogress = PyDict_GetItem(tstate_dict, _PyCompareState_Key);
354 if (inprogress == NULL) {
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000355 inprogress = PyDict_New();
356 if (inprogress == NULL)
357 return NULL;
358 if (PyDict_SetItem(tstate_dict, _PyCompareState_Key,
359 inprogress) == -1) {
360 Py_DECREF(inprogress);
361 return NULL;
362 }
Jeremy Hyltona251ea02000-06-09 16:20:39 +0000363 Py_DECREF(inprogress);
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000364 }
365 return inprogress;
366}
367
368static PyObject *
Fred Drake100814d2000-07-09 15:48:49 +0000369make_pair(PyObject *v, PyObject *w)
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000370{
371 PyObject *pair;
Barry Warsaw9d23a4e2000-08-18 05:01:19 +0000372 Py_uintptr_t iv = (Py_uintptr_t)v;
373 Py_uintptr_t iw = (Py_uintptr_t)w;
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000374
375 pair = PyTuple_New(2);
376 if (pair == NULL) {
377 return NULL;
378 }
Barry Warsaw9d23a4e2000-08-18 05:01:19 +0000379 if (iv <= iw) {
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000380 PyTuple_SET_ITEM(pair, 0, PyLong_FromVoidPtr((void *)v));
381 PyTuple_SET_ITEM(pair, 1, PyLong_FromVoidPtr((void *)w));
382 } else {
383 PyTuple_SET_ITEM(pair, 0, PyLong_FromVoidPtr((void *)w));
384 PyTuple_SET_ITEM(pair, 1, PyLong_FromVoidPtr((void *)v));
385 }
386 return pair;
387}
388
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000389int
Fred Drake100814d2000-07-09 15:48:49 +0000390PyObject_Compare(PyObject *v, PyObject *w)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000391{
Guido van Rossumcd5a5f61998-06-09 18:58:44 +0000392 PyTypeObject *vtp, *wtp;
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000393 int result;
394
Jack Jansend49cbe12000-08-22 21:52:51 +0000395#if defined(USE_STACKCHECK)
Guido van Rossum9c0a99e2000-08-30 15:53:50 +0000396 if (PyOS_CheckStack()) {
Jack Jansend49cbe12000-08-22 21:52:51 +0000397 PyErr_SetString(PyExc_MemoryError, "Stack overflow");
398 return -1;
399 }
400#endif
Guido van Rossumc8b6df91997-05-23 00:06:51 +0000401 if (v == NULL || w == NULL) {
402 PyErr_BadInternalCall();
403 return -1;
404 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000405 if (v == w)
406 return 0;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000407 if (PyInstance_Check(v) || PyInstance_Check(w)) {
408 PyObject *res;
Guido van Rossum20566841995-01-12 11:26:10 +0000409 int c;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000410 if (!PyInstance_Check(v))
411 return -PyObject_Compare(w, v);
Vladimir Marangozov1d3e2392000-08-11 00:14:26 +0000412 _PyCompareState_nesting++;
413 if (_PyCompareState_nesting > NESTING_LIMIT) {
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000414 PyObject *inprogress, *pair;
415
416 inprogress = get_inprogress_dict();
417 if (inprogress == NULL) {
Vladimir Marangozov1d3e2392000-08-11 00:14:26 +0000418 _PyCompareState_nesting--;
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000419 return -1;
420 }
421 pair = make_pair(v, w);
422 if (PyDict_GetItem(inprogress, pair)) {
423 /* already comparing these objects. assume
424 they're equal until shown otherwise */
425 Py_DECREF(pair);
Vladimir Marangozov1d3e2392000-08-11 00:14:26 +0000426 _PyCompareState_nesting--;
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000427 return 0;
428 }
429 if (PyDict_SetItem(inprogress, pair, pair) == -1) {
Vladimir Marangozov1d3e2392000-08-11 00:14:26 +0000430 _PyCompareState_nesting--;
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000431 return -1;
432 }
433 res = do_cmp(v, w);
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000434 /* XXX DelItem shouldn't fail */
435 PyDict_DelItem(inprogress, pair);
436 Py_DECREF(pair);
437 } else {
438 res = do_cmp(v, w);
439 }
Vladimir Marangozov1d3e2392000-08-11 00:14:26 +0000440 _PyCompareState_nesting--;
Guido van Rossumc8b6df91997-05-23 00:06:51 +0000441 if (res == NULL)
442 return -1;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000443 if (!PyInt_Check(res)) {
444 Py_DECREF(res);
Guido van Rossumc8b6df91997-05-23 00:06:51 +0000445 PyErr_SetString(PyExc_TypeError,
446 "comparison did not return an int");
447 return -1;
Guido van Rossum20566841995-01-12 11:26:10 +0000448 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000449 c = PyInt_AsLong(res);
450 Py_DECREF(res);
Guido van Rossum20566841995-01-12 11:26:10 +0000451 return (c < 0) ? -1 : (c > 0) ? 1 : 0;
452 }
Guido van Rossumcd5a5f61998-06-09 18:58:44 +0000453 if ((vtp = v->ob_type) != (wtp = w->ob_type)) {
454 char *vname = vtp->tp_name;
455 char *wname = wtp->tp_name;
Guido van Rossumb4db1941998-07-21 21:56:41 +0000456 if (vtp->tp_as_number != NULL && wtp->tp_as_number != NULL) {
Guido van Rossum242c6421997-11-19 16:03:17 +0000457 int err;
458 err = PyNumber_CoerceEx(&v, &w);
459 if (err < 0)
Guido van Rossumc8b6df91997-05-23 00:06:51 +0000460 return -1;
Guido van Rossum242c6421997-11-19 16:03:17 +0000461 else if (err == 0) {
Guido van Rossumb4db1941998-07-21 21:56:41 +0000462 int cmp;
463 vtp = v->ob_type;
464 if (vtp->tp_compare == NULL)
465 cmp = (v < w) ? -1 : 1;
466 else
467 cmp = (*vtp->tp_compare)(v, w);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000468 Py_DECREF(v);
469 Py_DECREF(w);
Guido van Rossum9fb03681991-07-01 18:48:04 +0000470 return cmp;
471 }
472 }
Guido van Rossumb244f692000-04-10 13:42:33 +0000473 else if (PyUnicode_Check(v) || PyUnicode_Check(w)) {
474 int result = PyUnicode_Compare(v, w);
475 if (result == -1 && PyErr_Occurred() &&
476 PyErr_ExceptionMatches(PyExc_TypeError))
477 /* TypeErrors are ignored: if Unicode coercion
478 fails due to one of the arguments not
479 having the right type, we continue as
480 defined by the coercion protocol (see
481 above). Luckily, decoding errors are
482 reported as ValueErrors and are not masked
483 by this technique. */
484 PyErr_Clear();
485 else
486 return result;
487 }
Guido van Rossumcd5a5f61998-06-09 18:58:44 +0000488 else if (vtp->tp_as_number != NULL)
489 vname = "";
490 else if (wtp->tp_as_number != NULL)
491 wname = "";
492 /* Numerical types compare smaller than all other types */
493 return strcmp(vname, wname);
Guido van Rossum9fb03681991-07-01 18:48:04 +0000494 }
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000495 if (vtp->tp_compare == NULL) {
Barry Warsaw9d23a4e2000-08-18 05:01:19 +0000496 Py_uintptr_t iv = (Py_uintptr_t)v;
497 Py_uintptr_t iw = (Py_uintptr_t)w;
498 return (iv < iw) ? -1 : 1;
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000499 }
Vladimir Marangozov1d3e2392000-08-11 00:14:26 +0000500 _PyCompareState_nesting++;
501 if (_PyCompareState_nesting > NESTING_LIMIT
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000502 && (vtp->tp_as_mapping
503 || (vtp->tp_as_sequence && !PyString_Check(v)))) {
504 PyObject *inprogress, *pair;
505
506 inprogress = get_inprogress_dict();
507 if (inprogress == NULL) {
Vladimir Marangozov1d3e2392000-08-11 00:14:26 +0000508 _PyCompareState_nesting--;
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000509 return -1;
510 }
511 pair = make_pair(v, w);
512 if (PyDict_GetItem(inprogress, pair)) {
513 /* already comparing these objects. assume
514 they're equal until shown otherwise */
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000515 Py_DECREF(pair);
Vladimir Marangozov1d3e2392000-08-11 00:14:26 +0000516 _PyCompareState_nesting--;
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000517 return 0;
518 }
519 if (PyDict_SetItem(inprogress, pair, pair) == -1) {
Vladimir Marangozov1d3e2392000-08-11 00:14:26 +0000520 _PyCompareState_nesting--;
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000521 return -1;
522 }
523 result = (*vtp->tp_compare)(v, w);
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000524 PyDict_DelItem(inprogress, pair); /* XXX shouldn't fail */
525 Py_DECREF(pair);
526 } else {
527 result = (*vtp->tp_compare)(v, w);
528 }
Vladimir Marangozov1d3e2392000-08-11 00:14:26 +0000529 _PyCompareState_nesting--;
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000530 return result;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000531}
532
Fred Drake13634cf2000-06-29 19:17:04 +0000533
534/* Set of hash utility functions to help maintaining the invariant that
535 iff a==b then hash(a)==hash(b)
536
537 All the utility functions (_Py_Hash*()) return "-1" to signify an error.
538*/
539
540long
Fred Drake100814d2000-07-09 15:48:49 +0000541_Py_HashDouble(double v)
Fred Drake13634cf2000-06-29 19:17:04 +0000542{
Tim Peters39dce292000-08-15 03:34:48 +0000543 double intpart, fractpart;
544 int expo;
545 long hipart;
546 long x; /* the final hash value */
547 /* This is designed so that Python numbers of different types
548 * that compare equal hash to the same value; otherwise comparisons
549 * of mapping keys will turn out weird.
550 */
551
552#ifdef MPW /* MPW C modf expects pointer to extended as second argument */
553{
554 extended e;
555 fractpart = modf(v, &e);
556 intpart = e;
557}
558#else
559 fractpart = modf(v, &intpart);
560#endif
561 if (fractpart == 0.0) {
562 /* This must return the same hash as an equal int or long. */
563 if (intpart > LONG_MAX || -intpart > LONG_MAX) {
564 /* Convert to long and use its hash. */
565 PyObject *plong; /* converted to Python long */
566 if (Py_IS_INFINITY(intpart))
567 /* can't convert to long int -- arbitrary */
568 v = v < 0 ? -271828.0 : 314159.0;
569 plong = PyLong_FromDouble(v);
570 if (plong == NULL)
571 return -1;
572 x = PyObject_Hash(plong);
573 Py_DECREF(plong);
574 return x;
575 }
576 /* Fits in a C long == a Python int, so is its own hash. */
577 x = (long)intpart;
578 if (x == -1)
579 x = -2;
580 return x;
581 }
582 /* The fractional part is non-zero, so we don't have to worry about
583 * making this match the hash of some other type.
584 * Use frexp to get at the bits in the double.
Fred Drake13634cf2000-06-29 19:17:04 +0000585 * Since the VAX D double format has 56 mantissa bits, which is the
586 * most of any double format in use, each of these parts may have as
587 * many as (but no more than) 56 significant bits.
Tim Peters39dce292000-08-15 03:34:48 +0000588 * So, assuming sizeof(long) >= 4, each part can be broken into two
589 * longs; frexp and multiplication are used to do that.
590 * Also, since the Cray double format has 15 exponent bits, which is
591 * the most of any double format in use, shifting the exponent field
592 * left by 15 won't overflow a long (again assuming sizeof(long) >= 4).
Fred Drake13634cf2000-06-29 19:17:04 +0000593 */
Tim Peters39dce292000-08-15 03:34:48 +0000594 v = frexp(v, &expo);
595 v *= 2147483648.0; /* 2**31 */
596 hipart = (long)v; /* take the top 32 bits */
597 v = (v - (double)hipart) * 2147483648.0; /* get the next 32 bits */
598 x = hipart + (long)v + (expo << 15);
599 if (x == -1)
600 x = -2;
601 return x;
Fred Drake13634cf2000-06-29 19:17:04 +0000602}
603
604long
Fred Drake100814d2000-07-09 15:48:49 +0000605_Py_HashPointer(void *p)
Fred Drake13634cf2000-06-29 19:17:04 +0000606{
607#if SIZEOF_LONG >= SIZEOF_VOID_P
608 return (long)p;
609#else
610 /* convert to a Python long and hash that */
611 PyObject* longobj;
612 long x;
613
614 if ((longobj = PyLong_FromVoidPtr(p)) == NULL) {
615 x = -1;
616 goto finally;
617 }
618 x = PyObject_Hash(longobj);
619
620finally:
621 Py_XDECREF(longobj);
622 return x;
623#endif
624}
625
626
Guido van Rossum9bfef441993-03-29 10:43:31 +0000627long
Fred Drake100814d2000-07-09 15:48:49 +0000628PyObject_Hash(PyObject *v)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000629{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000630 PyTypeObject *tp = v->ob_type;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000631 if (tp->tp_hash != NULL)
632 return (*tp->tp_hash)(v);
Fred Drake13634cf2000-06-29 19:17:04 +0000633 if (tp->tp_compare == NULL) {
634 return _Py_HashPointer(v); /* Use address as hash value */
635 }
Guido van Rossum9bfef441993-03-29 10:43:31 +0000636 /* If there's a cmp but no hash defined, the object can't be hashed */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000637 PyErr_SetString(PyExc_TypeError, "unhashable type");
Guido van Rossum9bfef441993-03-29 10:43:31 +0000638 return -1;
639}
640
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000641PyObject *
Fred Drake100814d2000-07-09 15:48:49 +0000642PyObject_GetAttrString(PyObject *v, char *name)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000643{
Guido van Rossumd8eb1b31996-08-09 20:52:03 +0000644 if (v->ob_type->tp_getattro != NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000645 PyObject *w, *res;
Guido van Rossumc6d06701997-01-18 07:57:16 +0000646 w = PyString_InternFromString(name);
Guido van Rossumd8eb1b31996-08-09 20:52:03 +0000647 if (w == NULL)
648 return NULL;
649 res = (*v->ob_type->tp_getattro)(v, w);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000650 Py_XDECREF(w);
Guido van Rossumd8eb1b31996-08-09 20:52:03 +0000651 return res;
652 }
653
Guido van Rossum3f5da241990-12-20 15:06:42 +0000654 if (v->ob_type->tp_getattr == NULL) {
Guido van Rossumdb935161998-01-19 22:16:36 +0000655 PyErr_Format(PyExc_AttributeError,
656 "'%.50s' object has no attribute '%.400s'",
657 v->ob_type->tp_name,
658 name);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000659 return NULL;
660 }
661 else {
662 return (*v->ob_type->tp_getattr)(v, name);
663 }
664}
665
666int
Fred Drake100814d2000-07-09 15:48:49 +0000667PyObject_HasAttrString(PyObject *v, char *name)
Guido van Rossumed18fdc1993-07-11 19:55:34 +0000668{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000669 PyObject *res = PyObject_GetAttrString(v, name);
Guido van Rossumed18fdc1993-07-11 19:55:34 +0000670 if (res != NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000671 Py_DECREF(res);
Guido van Rossumed18fdc1993-07-11 19:55:34 +0000672 return 1;
673 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000674 PyErr_Clear();
Guido van Rossumed18fdc1993-07-11 19:55:34 +0000675 return 0;
676}
677
678int
Fred Drake100814d2000-07-09 15:48:49 +0000679PyObject_SetAttrString(PyObject *v, char *name, PyObject *w)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000680{
Guido van Rossumd8eb1b31996-08-09 20:52:03 +0000681 if (v->ob_type->tp_setattro != NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000682 PyObject *s;
Guido van Rossumd8eb1b31996-08-09 20:52:03 +0000683 int res;
Guido van Rossumc6d06701997-01-18 07:57:16 +0000684 s = PyString_InternFromString(name);
Guido van Rossumd8eb1b31996-08-09 20:52:03 +0000685 if (s == NULL)
Guido van Rossumb7fc3041996-09-11 22:51:25 +0000686 return -1;
Guido van Rossumd8eb1b31996-08-09 20:52:03 +0000687 res = (*v->ob_type->tp_setattro)(v, s, w);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000688 Py_XDECREF(s);
Guido van Rossumd8eb1b31996-08-09 20:52:03 +0000689 return res;
690 }
691
Guido van Rossum3f5da241990-12-20 15:06:42 +0000692 if (v->ob_type->tp_setattr == NULL) {
693 if (v->ob_type->tp_getattr == NULL)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000694 PyErr_SetString(PyExc_TypeError,
Guido van Rossum3ea74121991-12-24 13:28:03 +0000695 "attribute-less object (assign or del)");
Guido van Rossum3f5da241990-12-20 15:06:42 +0000696 else
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000697 PyErr_SetString(PyExc_TypeError,
Guido van Rossum3ea74121991-12-24 13:28:03 +0000698 "object has read-only attributes");
Guido van Rossum73531a31990-12-20 23:12:40 +0000699 return -1;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000700 }
701 else {
702 return (*v->ob_type->tp_setattr)(v, name, w);
703 }
704}
705
Marc-André Lemburge44e5072000-09-18 16:20:57 +0000706/* Internal API needed by PyObject_GetAttr(): */
707extern
708PyObject *_PyUnicode_AsDefaultEncodedString(PyObject *unicode,
709 const char *errors);
710
Guido van Rossum98ff96a1997-05-20 18:34:44 +0000711PyObject *
Fred Drake100814d2000-07-09 15:48:49 +0000712PyObject_GetAttr(PyObject *v, PyObject *name)
Guido van Rossum98ff96a1997-05-20 18:34:44 +0000713{
Marc-André Lemburge44e5072000-09-18 16:20:57 +0000714 /* The Unicode to string conversion is done here because the
715 existing tp_getattro slots expect a string object as name
716 and we wouldn't want to break those. */
717 if (PyUnicode_Check(name)) {
718 name = _PyUnicode_AsDefaultEncodedString(name, NULL);
719 if (name == NULL)
720 return NULL;
721 }
Jeremy Hylton99a8f902000-06-23 14:36:32 +0000722
723 if (!PyString_Check(name)) {
724 PyErr_SetString(PyExc_TypeError,
725 "attribute name must be string");
726 return NULL;
727 }
Marc-André Lemburge44e5072000-09-18 16:20:57 +0000728 if (v->ob_type->tp_getattro != NULL)
729 return (*v->ob_type->tp_getattro)(v, name);
730 else
Jeremy Hylton99a8f902000-06-23 14:36:32 +0000731 return PyObject_GetAttrString(v, PyString_AS_STRING(name));
Guido van Rossum98ff96a1997-05-20 18:34:44 +0000732}
733
734int
Fred Drake100814d2000-07-09 15:48:49 +0000735PyObject_HasAttr(PyObject *v, PyObject *name)
Guido van Rossum98ff96a1997-05-20 18:34:44 +0000736{
737 PyObject *res = PyObject_GetAttr(v, name);
738 if (res != NULL) {
739 Py_DECREF(res);
740 return 1;
741 }
742 PyErr_Clear();
743 return 0;
744}
745
746int
Fred Drake100814d2000-07-09 15:48:49 +0000747PyObject_SetAttr(PyObject *v, PyObject *name, PyObject *value)
Guido van Rossum98ff96a1997-05-20 18:34:44 +0000748{
749 int err;
Marc-André Lemburge44e5072000-09-18 16:20:57 +0000750
751 /* The Unicode to string conversion is done here because the
752 existing tp_setattro slots expect a string object as name
753 and we wouldn't want to break those. */
754 if (PyUnicode_Check(name)) {
755 name = PyUnicode_AsEncodedString(name, NULL, NULL);
756 if (name == NULL)
757 return -1;
Jeremy Hylton99a8f902000-06-23 14:36:32 +0000758 }
Marc-André Lemburge44e5072000-09-18 16:20:57 +0000759 else
760 Py_INCREF(name);
761
762 if (!PyString_Check(name)){
Jeremy Hylton99a8f902000-06-23 14:36:32 +0000763 PyErr_SetString(PyExc_TypeError,
764 "attribute name must be string");
765 err = -1;
766 }
Marc-André Lemburge44e5072000-09-18 16:20:57 +0000767 else {
768 PyString_InternInPlace(&name);
769 if (v->ob_type->tp_setattro != NULL)
770 err = (*v->ob_type->tp_setattro)(v, name, value);
771 else
772 err = PyObject_SetAttrString(v,
773 PyString_AS_STRING(name), value);
774 }
775
Guido van Rossum98ff96a1997-05-20 18:34:44 +0000776 Py_DECREF(name);
777 return err;
778}
779
Guido van Rossum6ac258d1993-05-12 08:24:20 +0000780/* Test a value used as condition, e.g., in a for or if statement.
781 Return -1 if an error occurred */
782
783int
Fred Drake100814d2000-07-09 15:48:49 +0000784PyObject_IsTrue(PyObject *v)
Guido van Rossum6ac258d1993-05-12 08:24:20 +0000785{
786 int res;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000787 if (v == Py_None)
Guido van Rossum6ac258d1993-05-12 08:24:20 +0000788 res = 0;
Guido van Rossum1c4f4581998-05-22 00:53:24 +0000789 else if (v->ob_type->tp_as_number != NULL &&
790 v->ob_type->tp_as_number->nb_nonzero != NULL)
Guido van Rossum6ac258d1993-05-12 08:24:20 +0000791 res = (*v->ob_type->tp_as_number->nb_nonzero)(v);
Guido van Rossum1c4f4581998-05-22 00:53:24 +0000792 else if (v->ob_type->tp_as_mapping != NULL &&
793 v->ob_type->tp_as_mapping->mp_length != NULL)
Guido van Rossum6ac258d1993-05-12 08:24:20 +0000794 res = (*v->ob_type->tp_as_mapping->mp_length)(v);
Guido van Rossum1c4f4581998-05-22 00:53:24 +0000795 else if (v->ob_type->tp_as_sequence != NULL &&
796 v->ob_type->tp_as_sequence->sq_length != NULL)
Guido van Rossum6ac258d1993-05-12 08:24:20 +0000797 res = (*v->ob_type->tp_as_sequence->sq_length)(v);
798 else
799 res = 1;
800 if (res > 0)
801 res = 1;
802 return res;
803}
804
Guido van Rossumc3d3f961998-04-09 17:53:59 +0000805/* equivalent of 'not v'
806 Return -1 if an error occurred */
807
808int
Fred Drake100814d2000-07-09 15:48:49 +0000809PyObject_Not(PyObject *v)
Guido van Rossumc3d3f961998-04-09 17:53:59 +0000810{
811 int res;
812 res = PyObject_IsTrue(v);
813 if (res < 0)
814 return res;
815 return res == 0;
816}
817
Guido van Rossum5524a591995-01-10 15:26:20 +0000818/* Coerce two numeric types to the "larger" one.
819 Increment the reference count on each argument.
820 Return -1 and raise an exception if no coercion is possible
821 (and then no reference count is incremented).
822*/
823
824int
Fred Drake100814d2000-07-09 15:48:49 +0000825PyNumber_CoerceEx(PyObject **pv, PyObject **pw)
Guido van Rossum5524a591995-01-10 15:26:20 +0000826{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000827 register PyObject *v = *pv;
828 register PyObject *w = *pw;
Guido van Rossum5524a591995-01-10 15:26:20 +0000829 int res;
830
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000831 if (v->ob_type == w->ob_type && !PyInstance_Check(v)) {
832 Py_INCREF(v);
833 Py_INCREF(w);
Guido van Rossum5524a591995-01-10 15:26:20 +0000834 return 0;
835 }
836 if (v->ob_type->tp_as_number && v->ob_type->tp_as_number->nb_coerce) {
837 res = (*v->ob_type->tp_as_number->nb_coerce)(pv, pw);
838 if (res <= 0)
839 return res;
840 }
841 if (w->ob_type->tp_as_number && w->ob_type->tp_as_number->nb_coerce) {
842 res = (*w->ob_type->tp_as_number->nb_coerce)(pw, pv);
843 if (res <= 0)
844 return res;
845 }
Guido van Rossum242c6421997-11-19 16:03:17 +0000846 return 1;
847}
848
849int
Fred Drake100814d2000-07-09 15:48:49 +0000850PyNumber_Coerce(PyObject **pv, PyObject **pw)
Guido van Rossum242c6421997-11-19 16:03:17 +0000851{
852 int err = PyNumber_CoerceEx(pv, pw);
853 if (err <= 0)
854 return err;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000855 PyErr_SetString(PyExc_TypeError, "number coercion failed");
Guido van Rossum5524a591995-01-10 15:26:20 +0000856 return -1;
857}
858
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000859
Guido van Rossum49b11fe1995-01-26 00:38:22 +0000860/* Test whether an object can be called */
861
862int
Fred Drake100814d2000-07-09 15:48:49 +0000863PyCallable_Check(PyObject *x)
Guido van Rossum49b11fe1995-01-26 00:38:22 +0000864{
865 if (x == NULL)
866 return 0;
867 if (x->ob_type->tp_call != NULL ||
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000868 PyFunction_Check(x) ||
869 PyMethod_Check(x) ||
870 PyCFunction_Check(x) ||
871 PyClass_Check(x))
Guido van Rossum49b11fe1995-01-26 00:38:22 +0000872 return 1;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000873 if (PyInstance_Check(x)) {
874 PyObject *call = PyObject_GetAttrString(x, "__call__");
Guido van Rossum49b11fe1995-01-26 00:38:22 +0000875 if (call == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000876 PyErr_Clear();
Guido van Rossum49b11fe1995-01-26 00:38:22 +0000877 return 0;
878 }
879 /* Could test recursively but don't, for fear of endless
880 recursion if some joker sets self.__call__ = self */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000881 Py_DECREF(call);
Guido van Rossum49b11fe1995-01-26 00:38:22 +0000882 return 1;
883 }
884 return 0;
885}
886
887
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000888/*
889NoObject is usable as a non-NULL undefined value, used by the macro None.
890There is (and should be!) no way to create other objects of this type,
Guido van Rossum3f5da241990-12-20 15:06:42 +0000891so there is exactly one (which is indestructible, by the way).
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000892*/
893
Guido van Rossum0c182a11992-03-27 17:26:13 +0000894/* ARGSUSED */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000895static PyObject *
Fred Drake100814d2000-07-09 15:48:49 +0000896none_repr(PyObject *op)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000897{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000898 return PyString_FromString("None");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000899}
900
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000901static PyTypeObject PyNothing_Type = {
902 PyObject_HEAD_INIT(&PyType_Type)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000903 0,
Guido van Rossum3f5da241990-12-20 15:06:42 +0000904 "None",
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000905 0,
906 0,
907 0, /*tp_dealloc*/ /*never called*/
Guido van Rossum7066dd71992-09-17 17:54:56 +0000908 0, /*tp_print*/
Guido van Rossum3f5da241990-12-20 15:06:42 +0000909 0, /*tp_getattr*/
910 0, /*tp_setattr*/
911 0, /*tp_compare*/
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000912 (reprfunc)none_repr, /*tp_repr*/
Guido van Rossum3f5da241990-12-20 15:06:42 +0000913 0, /*tp_as_number*/
914 0, /*tp_as_sequence*/
915 0, /*tp_as_mapping*/
Guido van Rossum9bfef441993-03-29 10:43:31 +0000916 0, /*tp_hash */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000917};
918
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000919PyObject _Py_NoneStruct = {
920 PyObject_HEAD_INIT(&PyNothing_Type)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000921};
922
923
Guido van Rossum84a90321996-05-22 16:34:47 +0000924#ifdef Py_TRACE_REFS
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000925
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000926static PyObject refchain = {&refchain, &refchain};
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000927
Guido van Rossumaacdc9d1996-08-12 21:32:12 +0000928void
Fred Drake100814d2000-07-09 15:48:49 +0000929_Py_ResetReferences(void)
Guido van Rossume09fb551997-08-05 02:04:34 +0000930{
931 refchain._ob_prev = refchain._ob_next = &refchain;
932 _Py_RefTotal = 0;
933}
934
935void
Fred Drake100814d2000-07-09 15:48:49 +0000936_Py_NewReference(PyObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000937{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000938 _Py_RefTotal++;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000939 op->ob_refcnt = 1;
940 op->_ob_next = refchain._ob_next;
941 op->_ob_prev = &refchain;
942 refchain._ob_next->_ob_prev = op;
943 refchain._ob_next = op;
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +0000944#ifdef COUNT_ALLOCS
945 inc_count(op->ob_type);
946#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000947}
948
Guido van Rossumaacdc9d1996-08-12 21:32:12 +0000949void
Fred Drake100814d2000-07-09 15:48:49 +0000950_Py_ForgetReference(register PyObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000951{
Guido van Rossumbffd6832000-01-20 22:32:56 +0000952#ifdef SLOW_UNREF_CHECK
Guido van Rossum4c08d552000-03-10 22:55:18 +0000953 register PyObject *p;
Guido van Rossumbffd6832000-01-20 22:32:56 +0000954#endif
Guido van Rossumd7047b31995-01-02 19:07:15 +0000955 if (op->ob_refcnt < 0)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000956 Py_FatalError("UNREF negative refcnt");
Guido van Rossum2e8f6141992-09-03 20:32:55 +0000957 if (op == &refchain ||
Guido van Rossumd7047b31995-01-02 19:07:15 +0000958 op->_ob_prev->_ob_next != op || op->_ob_next->_ob_prev != op)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000959 Py_FatalError("UNREF invalid object");
Guido van Rossum2e8f6141992-09-03 20:32:55 +0000960#ifdef SLOW_UNREF_CHECK
Guido van Rossum3f5da241990-12-20 15:06:42 +0000961 for (p = refchain._ob_next; p != &refchain; p = p->_ob_next) {
962 if (p == op)
963 break;
964 }
Guido van Rossumd7047b31995-01-02 19:07:15 +0000965 if (p == &refchain) /* Not found */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000966 Py_FatalError("UNREF unknown object");
Guido van Rossum2e8f6141992-09-03 20:32:55 +0000967#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000968 op->_ob_next->_ob_prev = op->_ob_prev;
969 op->_ob_prev->_ob_next = op->_ob_next;
Guido van Rossum2e8f6141992-09-03 20:32:55 +0000970 op->_ob_next = op->_ob_prev = NULL;
Guido van Rossumd8953cb1995-04-06 14:46:26 +0000971#ifdef COUNT_ALLOCS
972 op->ob_type->tp_free++;
973#endif
Guido van Rossum3f5da241990-12-20 15:06:42 +0000974}
975
Guido van Rossumaacdc9d1996-08-12 21:32:12 +0000976void
Fred Drake100814d2000-07-09 15:48:49 +0000977_Py_Dealloc(PyObject *op)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000978{
Guido van Rossum9776adf1994-09-07 14:36:45 +0000979 destructor dealloc = op->ob_type->tp_dealloc;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000980 _Py_ForgetReference(op);
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000981#ifndef WITH_CYCLE_GC
Guido van Rossume92e6102000-04-24 15:40:53 +0000982 if (_PyTrash_delete_nesting < PyTrash_UNWIND_LEVEL-1)
983 op->ob_type = NULL;
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000984#endif
Guido van Rossum9776adf1994-09-07 14:36:45 +0000985 (*dealloc)(op);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000986}
987
Guido van Rossumaacdc9d1996-08-12 21:32:12 +0000988void
Fred Drake100814d2000-07-09 15:48:49 +0000989_Py_PrintReferences(FILE *fp)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000990{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000991 PyObject *op;
Guido van Rossume09fb551997-08-05 02:04:34 +0000992 fprintf(fp, "Remaining objects:\n");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000993 for (op = refchain._ob_next; op != &refchain; op = op->_ob_next) {
994 fprintf(fp, "[%d] ", op->ob_refcnt);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000995 if (PyObject_Print(op, fp, 0) != 0)
996 PyErr_Clear();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000997 putc('\n', fp);
998 }
999}
1000
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +00001001PyObject *
Fred Drake100814d2000-07-09 15:48:49 +00001002_Py_GetObjects(PyObject *self, PyObject *args)
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +00001003{
1004 int i, n;
1005 PyObject *t = NULL;
1006 PyObject *res, *op;
1007
1008 if (!PyArg_ParseTuple(args, "i|O", &n, &t))
1009 return NULL;
1010 op = refchain._ob_next;
1011 res = PyList_New(0);
1012 if (res == NULL)
1013 return NULL;
1014 for (i = 0; (n == 0 || i < n) && op != &refchain; i++) {
1015 while (op == self || op == args || op == res || op == t ||
1016 t != NULL && op->ob_type != (PyTypeObject *) t) {
1017 op = op->_ob_next;
1018 if (op == &refchain)
1019 return res;
1020 }
1021 if (PyList_Append(res, op) < 0) {
1022 Py_DECREF(res);
1023 return NULL;
1024 }
1025 op = op->_ob_next;
1026 }
1027 return res;
1028}
1029
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001030#endif
Guido van Rossum97ead3f1996-01-12 01:24:09 +00001031
1032
1033/* Hack to force loading of cobject.o */
Guido van Rossumda9c2711996-12-05 21:58:58 +00001034PyTypeObject *_Py_cobject_hack = &PyCObject_Type;
Guido van Rossum84a90321996-05-22 16:34:47 +00001035
1036
1037/* Hack to force loading of abstract.o */
Jeremy Hylton6253f832000-07-12 12:56:19 +00001038int (*_Py_abstract_hack)(PyObject *) = &PyObject_Size;
Guido van Rossume09fb551997-08-05 02:04:34 +00001039
1040
Andrew M. Kuchling1582a3a2000-08-16 12:27:23 +00001041/* Python's malloc wrappers (see pymem.h) */
Guido van Rossume09fb551997-08-05 02:04:34 +00001042
Thomas Wouters334fb892000-07-25 12:56:38 +00001043void *
Fred Drake100814d2000-07-09 15:48:49 +00001044PyMem_Malloc(size_t nbytes)
Guido van Rossume09fb551997-08-05 02:04:34 +00001045{
1046#if _PyMem_EXTRA > 0
1047 if (nbytes == 0)
1048 nbytes = _PyMem_EXTRA;
1049#endif
Guido van Rossumb18618d2000-05-03 23:44:39 +00001050 return PyMem_MALLOC(nbytes);
Guido van Rossume09fb551997-08-05 02:04:34 +00001051}
1052
Thomas Wouters334fb892000-07-25 12:56:38 +00001053void *
1054PyMem_Realloc(void *p, size_t nbytes)
Guido van Rossume09fb551997-08-05 02:04:34 +00001055{
1056#if _PyMem_EXTRA > 0
1057 if (nbytes == 0)
1058 nbytes = _PyMem_EXTRA;
1059#endif
Guido van Rossumb18618d2000-05-03 23:44:39 +00001060 return PyMem_REALLOC(p, nbytes);
Guido van Rossume09fb551997-08-05 02:04:34 +00001061}
1062
1063void
Thomas Wouters334fb892000-07-25 12:56:38 +00001064PyMem_Free(void *p)
Guido van Rossume09fb551997-08-05 02:04:34 +00001065{
Guido van Rossumb18618d2000-05-03 23:44:39 +00001066 PyMem_FREE(p);
1067}
1068
1069
1070/* Python's object malloc wrappers (see objimpl.h) */
1071
Thomas Wouters334fb892000-07-25 12:56:38 +00001072void *
Fred Drake100814d2000-07-09 15:48:49 +00001073PyObject_Malloc(size_t nbytes)
Guido van Rossumb18618d2000-05-03 23:44:39 +00001074{
1075 return PyObject_MALLOC(nbytes);
1076}
1077
Thomas Wouters334fb892000-07-25 12:56:38 +00001078void *
1079PyObject_Realloc(void *p, size_t nbytes)
Guido van Rossumb18618d2000-05-03 23:44:39 +00001080{
1081 return PyObject_REALLOC(p, nbytes);
1082}
1083
1084void
Thomas Wouters334fb892000-07-25 12:56:38 +00001085PyObject_Free(void *p)
Guido van Rossumb18618d2000-05-03 23:44:39 +00001086{
1087 PyObject_FREE(p);
Guido van Rossume09fb551997-08-05 02:04:34 +00001088}
Guido van Rossum86610361998-04-10 22:32:46 +00001089
1090
1091/* These methods are used to control infinite recursion in repr, str, print,
1092 etc. Container objects that may recursively contain themselves,
1093 e.g. builtin dictionaries and lists, should used Py_ReprEnter() and
1094 Py_ReprLeave() to avoid infinite recursion.
1095
1096 Py_ReprEnter() returns 0 the first time it is called for a particular
1097 object and 1 every time thereafter. It returns -1 if an exception
1098 occurred. Py_ReprLeave() has no return value.
1099
1100 See dictobject.c and listobject.c for examples of use.
1101*/
1102
1103#define KEY "Py_Repr"
1104
1105int
Fred Drake100814d2000-07-09 15:48:49 +00001106Py_ReprEnter(PyObject *obj)
Guido van Rossum86610361998-04-10 22:32:46 +00001107{
1108 PyObject *dict;
1109 PyObject *list;
1110 int i;
1111
1112 dict = PyThreadState_GetDict();
1113 if (dict == NULL)
1114 return -1;
1115 list = PyDict_GetItemString(dict, KEY);
1116 if (list == NULL) {
1117 list = PyList_New(0);
1118 if (list == NULL)
1119 return -1;
1120 if (PyDict_SetItemString(dict, KEY, list) < 0)
1121 return -1;
1122 Py_DECREF(list);
1123 }
1124 i = PyList_GET_SIZE(list);
1125 while (--i >= 0) {
1126 if (PyList_GET_ITEM(list, i) == obj)
1127 return 1;
1128 }
1129 PyList_Append(list, obj);
1130 return 0;
1131}
1132
1133void
Fred Drake100814d2000-07-09 15:48:49 +00001134Py_ReprLeave(PyObject *obj)
Guido van Rossum86610361998-04-10 22:32:46 +00001135{
1136 PyObject *dict;
1137 PyObject *list;
1138 int i;
1139
1140 dict = PyThreadState_GetDict();
Guido van Rossumeb909461998-04-11 15:17:34 +00001141 if (dict == NULL)
1142 return;
Guido van Rossum86610361998-04-10 22:32:46 +00001143 list = PyDict_GetItemString(dict, KEY);
Guido van Rossumeb909461998-04-11 15:17:34 +00001144 if (list == NULL || !PyList_Check(list))
1145 return;
Guido van Rossum86610361998-04-10 22:32:46 +00001146 i = PyList_GET_SIZE(list);
1147 /* Count backwards because we always expect obj to be list[-1] */
1148 while (--i >= 0) {
1149 if (PyList_GET_ITEM(list, i) == obj) {
1150 PyList_SetSlice(list, i, i + 1, NULL);
1151 break;
1152 }
1153 }
1154}
Guido van Rossumd724b232000-03-13 16:01:29 +00001155
1156/*
1157 trashcan
1158 CT 2k0130
1159 non-recursively destroy nested objects
1160
1161 CT 2k0223
1162 everything is now done in a macro.
1163
1164 CT 2k0305
1165 modified to use functions, after Tim Peter's suggestion.
1166
1167 CT 2k0309
1168 modified to restore a possible error.
Guido van Rossum13ff8eb2000-03-25 18:39:19 +00001169
1170 CT 2k0325
1171 added better safe than sorry check for threadstate
Guido van Rossume92e6102000-04-24 15:40:53 +00001172
1173 CT 2k0422
1174 complete rewrite. We now build a chain via ob_type
1175 and save the limited number of types in ob_refcnt.
1176 This is perfect since we don't need any memory.
1177 A patch for free-threading would need just a lock.
Guido van Rossumd724b232000-03-13 16:01:29 +00001178*/
1179
Guido van Rossume92e6102000-04-24 15:40:53 +00001180#define Py_TRASHCAN_TUPLE 1
1181#define Py_TRASHCAN_LIST 2
1182#define Py_TRASHCAN_DICT 3
1183#define Py_TRASHCAN_FRAME 4
1184#define Py_TRASHCAN_TRACEBACK 5
1185/* extend here if other objects want protection */
1186
Guido van Rossumd724b232000-03-13 16:01:29 +00001187int _PyTrash_delete_nesting = 0;
Guido van Rossume92e6102000-04-24 15:40:53 +00001188
Guido van Rossumd724b232000-03-13 16:01:29 +00001189PyObject * _PyTrash_delete_later = NULL;
1190
1191void
Fred Drake100814d2000-07-09 15:48:49 +00001192_PyTrash_deposit_object(PyObject *op)
Guido van Rossumd724b232000-03-13 16:01:29 +00001193{
Guido van Rossume92e6102000-04-24 15:40:53 +00001194 int typecode;
Guido van Rossum13ff8eb2000-03-25 18:39:19 +00001195
Guido van Rossume92e6102000-04-24 15:40:53 +00001196 if (PyTuple_Check(op))
1197 typecode = Py_TRASHCAN_TUPLE;
1198 else if (PyList_Check(op))
1199 typecode = Py_TRASHCAN_LIST;
1200 else if (PyDict_Check(op))
1201 typecode = Py_TRASHCAN_DICT;
1202 else if (PyFrame_Check(op))
1203 typecode = Py_TRASHCAN_FRAME;
1204 else if (PyTraceBack_Check(op))
1205 typecode = Py_TRASHCAN_TRACEBACK;
Moshe Zadkacf703f02000-08-04 15:36:13 +00001206 else /* We have a bug here -- those are the only types in GC */ {
1207 Py_FatalError("Type not supported in GC -- internal bug");
1208 return; /* pacify compiler -- execution never here */
1209 }
Guido van Rossume92e6102000-04-24 15:40:53 +00001210 op->ob_refcnt = typecode;
Guido van Rossumd724b232000-03-13 16:01:29 +00001211
Guido van Rossume92e6102000-04-24 15:40:53 +00001212 op->ob_type = (PyTypeObject*)_PyTrash_delete_later;
1213 _PyTrash_delete_later = op;
Guido van Rossumd724b232000-03-13 16:01:29 +00001214}
1215
1216void
Fred Drake100814d2000-07-09 15:48:49 +00001217_PyTrash_destroy_chain(void)
Guido van Rossumd724b232000-03-13 16:01:29 +00001218{
1219 while (_PyTrash_delete_later) {
1220 PyObject *shredder = _PyTrash_delete_later;
Guido van Rossume92e6102000-04-24 15:40:53 +00001221 _PyTrash_delete_later = (PyObject*) shredder->ob_type;
1222
1223 switch (shredder->ob_refcnt) {
1224 case Py_TRASHCAN_TUPLE:
1225 shredder->ob_type = &PyTuple_Type;
1226 break;
1227 case Py_TRASHCAN_LIST:
1228 shredder->ob_type = &PyList_Type;
1229 break;
1230 case Py_TRASHCAN_DICT:
1231 shredder->ob_type = &PyDict_Type;
1232 break;
1233 case Py_TRASHCAN_FRAME:
1234 shredder->ob_type = &PyFrame_Type;
1235 break;
1236 case Py_TRASHCAN_TRACEBACK:
1237 shredder->ob_type = &PyTraceBack_Type;
1238 break;
1239 }
1240 _Py_NewReference(shredder);
1241
Guido van Rossumd724b232000-03-13 16:01:29 +00001242 ++_PyTrash_delete_nesting;
1243 Py_DECREF(shredder);
1244 --_PyTrash_delete_nesting;
1245 }
1246}