blob: 9f73cd313a2524d4ab37b41574260047e89cf3d9 [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"
Jack Jansend49cbe12000-08-22 21:52:51 +000014#ifdef HAVE_LIMITS_H
15#include <limits.h>
16#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000017
Jack Jansen28fc8802000-07-11 21:47:20 +000018#ifdef macintosh
19#include "macglue.h"
20#endif
21
Guido van Rossume92e6102000-04-24 15:40:53 +000022/* just for trashcan: */
23#include "compile.h"
24#include "frameobject.h"
25#include "traceback.h"
26
Guido van Rossum6f9e4331995-03-29 16:57:48 +000027#if defined( Py_TRACE_REFS ) || defined( Py_REF_DEBUG )
Guido van Rossumbffd6832000-01-20 22:32:56 +000028DL_IMPORT(long) _Py_RefTotal;
Guido van Rossum3f5da241990-12-20 15:06:42 +000029#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000030
Guido van Rossum3f5da241990-12-20 15:06:42 +000031/* Object allocation routines used by NEWOBJ and NEWVAROBJ macros.
32 These are used by the individual routines for object creation.
33 Do not call them otherwise, they do not initialize the object! */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000034
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +000035#ifdef COUNT_ALLOCS
Guido van Rossumc0b618a1997-05-02 03:12:38 +000036static PyTypeObject *type_list;
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +000037extern int tuple_zero_allocs, fast_tuple_allocs;
38extern int quick_int_allocs, quick_neg_int_allocs;
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +000039extern int null_strings, one_strings;
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +000040void
Fred Drake100814d2000-07-09 15:48:49 +000041dump_counts(void)
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +000042{
Guido van Rossumc0b618a1997-05-02 03:12:38 +000043 PyTypeObject *tp;
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +000044
45 for (tp = type_list; tp; tp = tp->tp_next)
Sjoerd Mullender52c1f511993-10-25 08:40:52 +000046 fprintf(stderr, "%s alloc'd: %d, freed: %d, max in use: %d\n",
47 tp->tp_name, tp->tp_alloc, tp->tp_free,
48 tp->tp_maxalloc);
49 fprintf(stderr, "fast tuple allocs: %d, empty: %d\n",
50 fast_tuple_allocs, tuple_zero_allocs);
51 fprintf(stderr, "fast int allocs: pos: %d, neg: %d\n",
52 quick_int_allocs, quick_neg_int_allocs);
53 fprintf(stderr, "null strings: %d, 1-strings: %d\n",
54 null_strings, one_strings);
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +000055}
56
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +000057PyObject *
Fred Drake100814d2000-07-09 15:48:49 +000058get_counts(void)
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +000059{
60 PyTypeObject *tp;
61 PyObject *result;
62 PyObject *v;
63
64 result = PyList_New(0);
65 if (result == NULL)
66 return NULL;
67 for (tp = type_list; tp; tp = tp->tp_next) {
68 v = Py_BuildValue("(siii)", tp->tp_name, tp->tp_alloc,
69 tp->tp_free, tp->tp_maxalloc);
70 if (v == NULL) {
71 Py_DECREF(result);
72 return NULL;
73 }
74 if (PyList_Append(result, v) < 0) {
75 Py_DECREF(v);
76 Py_DECREF(result);
77 return NULL;
78 }
79 Py_DECREF(v);
80 }
81 return result;
82}
83
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +000084void
Fred Drake100814d2000-07-09 15:48:49 +000085inc_count(PyTypeObject *tp)
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +000086{
87 if (tp->tp_alloc == 0) {
Guido van Rossumd8953cb1995-04-06 14:46:26 +000088 /* first time; insert in linked list */
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +000089 if (tp->tp_next != NULL) /* sanity check */
Guido van Rossumc0b618a1997-05-02 03:12:38 +000090 Py_FatalError("XXX inc_count sanity check");
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +000091 tp->tp_next = type_list;
92 type_list = tp;
93 }
94 tp->tp_alloc++;
95 if (tp->tp_alloc - tp->tp_free > tp->tp_maxalloc)
96 tp->tp_maxalloc = tp->tp_alloc - tp->tp_free;
97}
98#endif
99
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000100PyObject *
Fred Drake100814d2000-07-09 15:48:49 +0000101PyObject_Init(PyObject *op, PyTypeObject *tp)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000102{
Guido van Rossumb18618d2000-05-03 23:44:39 +0000103 if (op == NULL) {
104 PyErr_SetString(PyExc_SystemError,
105 "NULL object passed to PyObject_Init");
106 return op;
107 }
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000108#ifdef WITH_CYCLE_GC
109 if (PyType_IS_GC(tp))
110 op = (PyObject *) PyObject_FROM_GC(op);
111#endif
Guido van Rossumb18618d2000-05-03 23:44:39 +0000112 /* Any changes should be reflected in PyObject_INIT (objimpl.h) */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000113 op->ob_type = tp;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000114 _Py_NewReference(op);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000115 return op;
116}
117
Guido van Rossumb18618d2000-05-03 23:44:39 +0000118PyVarObject *
Fred Drake100814d2000-07-09 15:48:49 +0000119PyObject_InitVar(PyVarObject *op, PyTypeObject *tp, int size)
Guido van Rossumb18618d2000-05-03 23:44:39 +0000120{
121 if (op == NULL) {
122 PyErr_SetString(PyExc_SystemError,
123 "NULL object passed to PyObject_InitVar");
124 return op;
125 }
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000126#ifdef WITH_CYCLE_GC
127 if (PyType_IS_GC(tp))
128 op = (PyVarObject *) PyObject_FROM_GC(op);
129#endif
Guido van Rossumb18618d2000-05-03 23:44:39 +0000130 /* Any changes should be reflected in PyObject_INIT_VAR */
131 op->ob_size = size;
132 op->ob_type = tp;
133 _Py_NewReference((PyObject *)op);
134 return op;
135}
136
137PyObject *
Fred Drake100814d2000-07-09 15:48:49 +0000138_PyObject_New(PyTypeObject *tp)
Guido van Rossumb18618d2000-05-03 23:44:39 +0000139{
140 PyObject *op;
141 op = (PyObject *) PyObject_MALLOC(_PyObject_SIZE(tp));
142 if (op == NULL)
143 return PyErr_NoMemory();
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000144#ifdef WITH_CYCLE_GC
145 if (PyType_IS_GC(tp))
146 op = (PyObject *) PyObject_FROM_GC(op);
147#endif
Guido van Rossumb18618d2000-05-03 23:44:39 +0000148 return PyObject_INIT(op, tp);
149}
150
Guido van Rossumd0c87ee1997-05-15 21:31:03 +0000151PyVarObject *
Fred Drake100814d2000-07-09 15:48:49 +0000152_PyObject_NewVar(PyTypeObject *tp, int size)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000153{
Guido van Rossumb18618d2000-05-03 23:44:39 +0000154 PyVarObject *op;
155 op = (PyVarObject *) PyObject_MALLOC(_PyObject_VAR_SIZE(tp, size));
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000156 if (op == NULL)
Guido van Rossumd0c87ee1997-05-15 21:31:03 +0000157 return (PyVarObject *)PyErr_NoMemory();
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000158#ifdef WITH_CYCLE_GC
159 if (PyType_IS_GC(tp))
160 op = (PyVarObject *) PyObject_FROM_GC(op);
161#endif
Guido van Rossumb18618d2000-05-03 23:44:39 +0000162 return PyObject_INIT_VAR(op, tp, size);
163}
164
165void
Fred Drake100814d2000-07-09 15:48:49 +0000166_PyObject_Del(PyObject *op)
Guido van Rossumb18618d2000-05-03 23:44:39 +0000167{
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000168#ifdef WITH_CYCLE_GC
Guido van Rossum4cc6ac72000-07-01 01:00:38 +0000169 if (op && PyType_IS_GC(op->ob_type)) {
170 op = (PyObject *) PyObject_AS_GC(op);
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000171 }
Guido van Rossum4cc6ac72000-07-01 01:00:38 +0000172#endif
173 PyObject_FREE(op);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000174}
175
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000176#ifndef WITH_CYCLE_GC
177/* extension modules might need these */
178void _PyGC_Insert(PyObject *op) { }
179void _PyGC_Remove(PyObject *op) { }
180#endif
181
Guido van Rossum90933611991-06-07 16:10:43 +0000182int
Fred Drake100814d2000-07-09 15:48:49 +0000183PyObject_Print(PyObject *op, FILE *fp, int flags)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000184{
Guido van Rossum278ef591991-07-27 21:40:24 +0000185 int ret = 0;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000186 if (PyErr_CheckSignals())
Guido van Rossum90933611991-06-07 16:10:43 +0000187 return -1;
Guido van Rossum9b00dfa1998-04-28 16:06:54 +0000188#ifdef USE_STACKCHECK
189 if (PyOS_CheckStack()) {
190 PyErr_SetString(PyExc_MemoryError, "Stack overflow");
191 return -1;
192 }
193#endif
Guido van Rossum687ef6e2000-01-12 16:28:58 +0000194 clearerr(fp); /* Clear any previous error condition */
Guido van Rossum90933611991-06-07 16:10:43 +0000195 if (op == NULL) {
196 fprintf(fp, "<nil>");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000197 }
Guido van Rossum90933611991-06-07 16:10:43 +0000198 else {
199 if (op->ob_refcnt <= 0)
Fred Drakea44d3532000-06-30 15:01:00 +0000200 fprintf(fp, "<refcnt %u at %p>",
201 op->ob_refcnt, op);
Guido van Rossum2e8f6141992-09-03 20:32:55 +0000202 else if (op->ob_type->tp_print == NULL) {
203 if (op->ob_type->tp_repr == NULL) {
Fred Drakea44d3532000-06-30 15:01:00 +0000204 fprintf(fp, "<%s object at %p>",
205 op->ob_type->tp_name, op);
Guido van Rossum2e8f6141992-09-03 20:32:55 +0000206 }
207 else {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000208 PyObject *s;
209 if (flags & Py_PRINT_RAW)
210 s = PyObject_Str(op);
Guido van Rossumc6004111993-11-05 10:22:19 +0000211 else
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000212 s = PyObject_Repr(op);
Guido van Rossum2e8f6141992-09-03 20:32:55 +0000213 if (s == NULL)
214 ret = -1;
Guido van Rossum2e8f6141992-09-03 20:32:55 +0000215 else {
Guido van Rossum565798d1998-04-21 22:25:01 +0000216 ret = PyObject_Print(s, fp,
217 Py_PRINT_RAW);
Guido van Rossum2e8f6141992-09-03 20:32:55 +0000218 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000219 Py_XDECREF(s);
Guido van Rossum2e8f6141992-09-03 20:32:55 +0000220 }
221 }
Guido van Rossum90933611991-06-07 16:10:43 +0000222 else
Guido van Rossum278ef591991-07-27 21:40:24 +0000223 ret = (*op->ob_type->tp_print)(op, fp, flags);
Guido van Rossum90933611991-06-07 16:10:43 +0000224 }
Guido van Rossum278ef591991-07-27 21:40:24 +0000225 if (ret == 0) {
226 if (ferror(fp)) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000227 PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossum278ef591991-07-27 21:40:24 +0000228 clearerr(fp);
229 ret = -1;
230 }
231 }
232 return ret;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000233}
234
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000235PyObject *
Fred Drake100814d2000-07-09 15:48:49 +0000236PyObject_Repr(PyObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000237{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000238 if (PyErr_CheckSignals())
Guido van Rossum90933611991-06-07 16:10:43 +0000239 return NULL;
Guido van Rossum9b00dfa1998-04-28 16:06:54 +0000240#ifdef USE_STACKCHECK
241 if (PyOS_CheckStack()) {
242 PyErr_SetString(PyExc_MemoryError, "Stack overflow");
243 return NULL;
244 }
245#endif
Guido van Rossum90933611991-06-07 16:10:43 +0000246 if (v == NULL)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000247 return PyString_FromString("<NULL>");
Guido van Rossum90933611991-06-07 16:10:43 +0000248 else if (v->ob_type->tp_repr == NULL) {
249 char buf[120];
Fred Drakea44d3532000-06-30 15:01:00 +0000250 sprintf(buf, "<%.80s object at %p>",
251 v->ob_type->tp_name, v);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000252 return PyString_FromString(buf);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000253 }
Guido van Rossum4c08d552000-03-10 22:55:18 +0000254 else {
255 PyObject *res;
256 res = (*v->ob_type->tp_repr)(v);
257 if (res == NULL)
258 return NULL;
Fredrik Lundhefecc7d2000-07-01 14:31:09 +0000259 if (PyUnicode_Check(res)) {
260 PyObject* str;
Fredrik Lundh2a1e0602000-07-08 17:43:32 +0000261 str = PyUnicode_AsUnicodeEscapeString(res);
Marc-André Lemburg891bc652000-07-03 09:57:53 +0000262 Py_DECREF(res);
263 if (str)
Fredrik Lundhefecc7d2000-07-01 14:31:09 +0000264 res = str;
Marc-André Lemburg891bc652000-07-03 09:57:53 +0000265 else
266 return NULL;
Fredrik Lundhefecc7d2000-07-01 14:31:09 +0000267 }
Guido van Rossum4c08d552000-03-10 22:55:18 +0000268 if (!PyString_Check(res)) {
269 PyErr_Format(PyExc_TypeError,
Guido van Rossum5db862d2000-04-10 12:46:51 +0000270 "__repr__ returned non-string (type %.200s)",
Guido van Rossum4c08d552000-03-10 22:55:18 +0000271 res->ob_type->tp_name);
272 Py_DECREF(res);
273 return NULL;
274 }
275 return res;
276 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000277}
278
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000279PyObject *
Fred Drake100814d2000-07-09 15:48:49 +0000280PyObject_Str(PyObject *v)
Guido van Rossumc6004111993-11-05 10:22:19 +0000281{
Guido van Rossum4c08d552000-03-10 22:55:18 +0000282 PyObject *res;
283
Guido van Rossumc6004111993-11-05 10:22:19 +0000284 if (v == NULL)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000285 return PyString_FromString("<NULL>");
286 else if (PyString_Check(v)) {
287 Py_INCREF(v);
Guido van Rossumc6004111993-11-05 10:22:19 +0000288 return v;
289 }
Guido van Rossum32b582b1995-01-17 16:35:13 +0000290 else if (v->ob_type->tp_str != NULL)
Guido van Rossum4c08d552000-03-10 22:55:18 +0000291 res = (*v->ob_type->tp_str)(v);
Guido van Rossumc6004111993-11-05 10:22:19 +0000292 else {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000293 PyObject *func;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000294 if (!PyInstance_Check(v) ||
295 (func = PyObject_GetAttrString(v, "__str__")) == NULL) {
296 PyErr_Clear();
297 return PyObject_Repr(v);
Guido van Rossumc6004111993-11-05 10:22:19 +0000298 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000299 res = PyEval_CallObject(func, (PyObject *)NULL);
300 Py_DECREF(func);
Guido van Rossumc6004111993-11-05 10:22:19 +0000301 }
Guido van Rossum4c08d552000-03-10 22:55:18 +0000302 if (res == NULL)
303 return NULL;
Marc-André Lemburg891bc652000-07-03 09:57:53 +0000304 if (PyUnicode_Check(res)) {
305 PyObject* str;
306 str = PyUnicode_AsEncodedString(res, NULL, NULL);
307 Py_DECREF(res);
308 if (str)
309 res = str;
310 else
311 return NULL;
312 }
Guido van Rossum4c08d552000-03-10 22:55:18 +0000313 if (!PyString_Check(res)) {
314 PyErr_Format(PyExc_TypeError,
Guido van Rossum5db862d2000-04-10 12:46:51 +0000315 "__str__ returned non-string (type %.200s)",
Guido van Rossum4c08d552000-03-10 22:55:18 +0000316 res->ob_type->tp_name);
317 Py_DECREF(res);
318 return NULL;
319 }
320 return res;
Guido van Rossumc6004111993-11-05 10:22:19 +0000321}
322
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000323static PyObject *
Fred Drake100814d2000-07-09 15:48:49 +0000324do_cmp(PyObject *v, PyObject *w)
Guido van Rossum20566841995-01-12 11:26:10 +0000325{
Guido van Rossumc8b6df91997-05-23 00:06:51 +0000326 long c;
Guido van Rossum20566841995-01-12 11:26:10 +0000327 /* __rcmp__ actually won't be called unless __cmp__ isn't defined,
328 because the check in cmpobject() reverses the objects first.
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000329 This is intentional -- it makes no sense to define cmp(x,y)
330 different than -cmp(y,x). */
331 if (PyInstance_Check(v) || PyInstance_Check(w))
332 return PyInstance_DoBinOp(v, w, "__cmp__", "__rcmp__", do_cmp);
Guido van Rossumc8b6df91997-05-23 00:06:51 +0000333 c = PyObject_Compare(v, w);
334 if (c && PyErr_Occurred())
335 return NULL;
336 return PyInt_FromLong(c);
Guido van Rossum20566841995-01-12 11:26:10 +0000337}
338
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000339PyObject *_PyCompareState_Key;
340
Thomas Wouters7e474022000-07-16 12:04:32 +0000341/* _PyCompareState_nesting is incremented before calling compare (for
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000342 some types) and decremented on exit. If the count exceeds the
343 nesting limit, enable code to detect circular data structures.
344*/
Jack Jansend49cbe12000-08-22 21:52:51 +0000345#ifdef macintosh
346#define NESTING_LIMIT 60
347#else
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000348#define NESTING_LIMIT 500
Jack Jansend49cbe12000-08-22 21:52:51 +0000349#endif
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000350int _PyCompareState_nesting = 0;
351
352static PyObject*
Fred Drake100814d2000-07-09 15:48:49 +0000353get_inprogress_dict(void)
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000354{
355 PyObject *tstate_dict, *inprogress;
356
357 tstate_dict = PyThreadState_GetDict();
358 if (tstate_dict == NULL) {
359 PyErr_BadInternalCall();
360 return NULL;
361 }
362 inprogress = PyDict_GetItem(tstate_dict, _PyCompareState_Key);
363 if (inprogress == NULL) {
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000364 inprogress = PyDict_New();
365 if (inprogress == NULL)
366 return NULL;
367 if (PyDict_SetItem(tstate_dict, _PyCompareState_Key,
368 inprogress) == -1) {
369 Py_DECREF(inprogress);
370 return NULL;
371 }
Jeremy Hyltona251ea02000-06-09 16:20:39 +0000372 Py_DECREF(inprogress);
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000373 }
374 return inprogress;
375}
376
377static PyObject *
Fred Drake100814d2000-07-09 15:48:49 +0000378make_pair(PyObject *v, PyObject *w)
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000379{
380 PyObject *pair;
Barry Warsaw9d23a4e2000-08-18 05:01:19 +0000381 Py_uintptr_t iv = (Py_uintptr_t)v;
382 Py_uintptr_t iw = (Py_uintptr_t)w;
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000383
384 pair = PyTuple_New(2);
385 if (pair == NULL) {
386 return NULL;
387 }
Barry Warsaw9d23a4e2000-08-18 05:01:19 +0000388 if (iv <= iw) {
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000389 PyTuple_SET_ITEM(pair, 0, PyLong_FromVoidPtr((void *)v));
390 PyTuple_SET_ITEM(pair, 1, PyLong_FromVoidPtr((void *)w));
391 } else {
392 PyTuple_SET_ITEM(pair, 0, PyLong_FromVoidPtr((void *)w));
393 PyTuple_SET_ITEM(pair, 1, PyLong_FromVoidPtr((void *)v));
394 }
395 return pair;
396}
397
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000398int
Fred Drake100814d2000-07-09 15:48:49 +0000399PyObject_Compare(PyObject *v, PyObject *w)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000400{
Guido van Rossumcd5a5f61998-06-09 18:58:44 +0000401 PyTypeObject *vtp, *wtp;
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000402 int result;
403
Jack Jansend49cbe12000-08-22 21:52:51 +0000404#if defined(USE_STACKCHECK)
Guido van Rossum9c0a99e2000-08-30 15:53:50 +0000405 if (PyOS_CheckStack()) {
Jack Jansend49cbe12000-08-22 21:52:51 +0000406 PyErr_SetString(PyExc_MemoryError, "Stack overflow");
407 return -1;
408 }
409#endif
Guido van Rossumc8b6df91997-05-23 00:06:51 +0000410 if (v == NULL || w == NULL) {
411 PyErr_BadInternalCall();
412 return -1;
413 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000414 if (v == w)
415 return 0;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000416 if (PyInstance_Check(v) || PyInstance_Check(w)) {
417 PyObject *res;
Guido van Rossum20566841995-01-12 11:26:10 +0000418 int c;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000419 if (!PyInstance_Check(v))
420 return -PyObject_Compare(w, v);
Vladimir Marangozov1d3e2392000-08-11 00:14:26 +0000421 _PyCompareState_nesting++;
422 if (_PyCompareState_nesting > NESTING_LIMIT) {
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000423 PyObject *inprogress, *pair;
424
425 inprogress = get_inprogress_dict();
426 if (inprogress == NULL) {
Vladimir Marangozov1d3e2392000-08-11 00:14:26 +0000427 _PyCompareState_nesting--;
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000428 return -1;
429 }
430 pair = make_pair(v, w);
431 if (PyDict_GetItem(inprogress, pair)) {
432 /* already comparing these objects. assume
433 they're equal until shown otherwise */
434 Py_DECREF(pair);
Vladimir Marangozov1d3e2392000-08-11 00:14:26 +0000435 _PyCompareState_nesting--;
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000436 return 0;
437 }
438 if (PyDict_SetItem(inprogress, pair, pair) == -1) {
Vladimir Marangozov1d3e2392000-08-11 00:14:26 +0000439 _PyCompareState_nesting--;
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000440 return -1;
441 }
442 res = do_cmp(v, w);
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000443 /* XXX DelItem shouldn't fail */
444 PyDict_DelItem(inprogress, pair);
445 Py_DECREF(pair);
446 } else {
447 res = do_cmp(v, w);
448 }
Vladimir Marangozov1d3e2392000-08-11 00:14:26 +0000449 _PyCompareState_nesting--;
Guido van Rossumc8b6df91997-05-23 00:06:51 +0000450 if (res == NULL)
451 return -1;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000452 if (!PyInt_Check(res)) {
453 Py_DECREF(res);
Guido van Rossumc8b6df91997-05-23 00:06:51 +0000454 PyErr_SetString(PyExc_TypeError,
455 "comparison did not return an int");
456 return -1;
Guido van Rossum20566841995-01-12 11:26:10 +0000457 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000458 c = PyInt_AsLong(res);
459 Py_DECREF(res);
Guido van Rossum20566841995-01-12 11:26:10 +0000460 return (c < 0) ? -1 : (c > 0) ? 1 : 0;
461 }
Guido van Rossumcd5a5f61998-06-09 18:58:44 +0000462 if ((vtp = v->ob_type) != (wtp = w->ob_type)) {
463 char *vname = vtp->tp_name;
464 char *wname = wtp->tp_name;
Guido van Rossumb4db1941998-07-21 21:56:41 +0000465 if (vtp->tp_as_number != NULL && wtp->tp_as_number != NULL) {
Guido van Rossum242c6421997-11-19 16:03:17 +0000466 int err;
467 err = PyNumber_CoerceEx(&v, &w);
468 if (err < 0)
Guido van Rossumc8b6df91997-05-23 00:06:51 +0000469 return -1;
Guido van Rossum242c6421997-11-19 16:03:17 +0000470 else if (err == 0) {
Guido van Rossumb4db1941998-07-21 21:56:41 +0000471 int cmp;
472 vtp = v->ob_type;
473 if (vtp->tp_compare == NULL)
474 cmp = (v < w) ? -1 : 1;
475 else
476 cmp = (*vtp->tp_compare)(v, w);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000477 Py_DECREF(v);
478 Py_DECREF(w);
Guido van Rossum9fb03681991-07-01 18:48:04 +0000479 return cmp;
480 }
481 }
Guido van Rossumb244f692000-04-10 13:42:33 +0000482 else if (PyUnicode_Check(v) || PyUnicode_Check(w)) {
483 int result = PyUnicode_Compare(v, w);
484 if (result == -1 && PyErr_Occurred() &&
485 PyErr_ExceptionMatches(PyExc_TypeError))
486 /* TypeErrors are ignored: if Unicode coercion
487 fails due to one of the arguments not
488 having the right type, we continue as
489 defined by the coercion protocol (see
490 above). Luckily, decoding errors are
491 reported as ValueErrors and are not masked
492 by this technique. */
493 PyErr_Clear();
494 else
495 return result;
496 }
Guido van Rossumcd5a5f61998-06-09 18:58:44 +0000497 else if (vtp->tp_as_number != NULL)
498 vname = "";
499 else if (wtp->tp_as_number != NULL)
500 wname = "";
501 /* Numerical types compare smaller than all other types */
502 return strcmp(vname, wname);
Guido van Rossum9fb03681991-07-01 18:48:04 +0000503 }
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000504 if (vtp->tp_compare == NULL) {
Barry Warsaw9d23a4e2000-08-18 05:01:19 +0000505 Py_uintptr_t iv = (Py_uintptr_t)v;
506 Py_uintptr_t iw = (Py_uintptr_t)w;
507 return (iv < iw) ? -1 : 1;
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000508 }
Vladimir Marangozov1d3e2392000-08-11 00:14:26 +0000509 _PyCompareState_nesting++;
510 if (_PyCompareState_nesting > NESTING_LIMIT
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000511 && (vtp->tp_as_mapping
512 || (vtp->tp_as_sequence && !PyString_Check(v)))) {
513 PyObject *inprogress, *pair;
514
515 inprogress = get_inprogress_dict();
516 if (inprogress == NULL) {
Vladimir Marangozov1d3e2392000-08-11 00:14:26 +0000517 _PyCompareState_nesting--;
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000518 return -1;
519 }
520 pair = make_pair(v, w);
521 if (PyDict_GetItem(inprogress, pair)) {
522 /* already comparing these objects. assume
523 they're equal until shown otherwise */
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000524 Py_DECREF(pair);
Vladimir Marangozov1d3e2392000-08-11 00:14:26 +0000525 _PyCompareState_nesting--;
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000526 return 0;
527 }
528 if (PyDict_SetItem(inprogress, pair, pair) == -1) {
Vladimir Marangozov1d3e2392000-08-11 00:14:26 +0000529 _PyCompareState_nesting--;
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000530 return -1;
531 }
532 result = (*vtp->tp_compare)(v, w);
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000533 PyDict_DelItem(inprogress, pair); /* XXX shouldn't fail */
534 Py_DECREF(pair);
535 } else {
536 result = (*vtp->tp_compare)(v, w);
537 }
Vladimir Marangozov1d3e2392000-08-11 00:14:26 +0000538 _PyCompareState_nesting--;
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000539 return result;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000540}
541
Fred Drake13634cf2000-06-29 19:17:04 +0000542
543/* Set of hash utility functions to help maintaining the invariant that
544 iff a==b then hash(a)==hash(b)
545
546 All the utility functions (_Py_Hash*()) return "-1" to signify an error.
547*/
548
549long
Fred Drake100814d2000-07-09 15:48:49 +0000550_Py_HashDouble(double v)
Fred Drake13634cf2000-06-29 19:17:04 +0000551{
Tim Peters39dce292000-08-15 03:34:48 +0000552 double intpart, fractpart;
553 int expo;
554 long hipart;
555 long x; /* the final hash value */
556 /* This is designed so that Python numbers of different types
557 * that compare equal hash to the same value; otherwise comparisons
558 * of mapping keys will turn out weird.
559 */
560
561#ifdef MPW /* MPW C modf expects pointer to extended as second argument */
562{
563 extended e;
564 fractpart = modf(v, &e);
565 intpart = e;
566}
567#else
568 fractpart = modf(v, &intpart);
569#endif
570 if (fractpart == 0.0) {
571 /* This must return the same hash as an equal int or long. */
572 if (intpart > LONG_MAX || -intpart > LONG_MAX) {
573 /* Convert to long and use its hash. */
574 PyObject *plong; /* converted to Python long */
575 if (Py_IS_INFINITY(intpart))
576 /* can't convert to long int -- arbitrary */
577 v = v < 0 ? -271828.0 : 314159.0;
578 plong = PyLong_FromDouble(v);
579 if (plong == NULL)
580 return -1;
581 x = PyObject_Hash(plong);
582 Py_DECREF(plong);
583 return x;
584 }
585 /* Fits in a C long == a Python int, so is its own hash. */
586 x = (long)intpart;
587 if (x == -1)
588 x = -2;
589 return x;
590 }
591 /* The fractional part is non-zero, so we don't have to worry about
592 * making this match the hash of some other type.
593 * Use frexp to get at the bits in the double.
Fred Drake13634cf2000-06-29 19:17:04 +0000594 * Since the VAX D double format has 56 mantissa bits, which is the
595 * most of any double format in use, each of these parts may have as
596 * many as (but no more than) 56 significant bits.
Tim Peters39dce292000-08-15 03:34:48 +0000597 * So, assuming sizeof(long) >= 4, each part can be broken into two
598 * longs; frexp and multiplication are used to do that.
599 * Also, since the Cray double format has 15 exponent bits, which is
600 * the most of any double format in use, shifting the exponent field
601 * left by 15 won't overflow a long (again assuming sizeof(long) >= 4).
Fred Drake13634cf2000-06-29 19:17:04 +0000602 */
Tim Peters39dce292000-08-15 03:34:48 +0000603 v = frexp(v, &expo);
604 v *= 2147483648.0; /* 2**31 */
605 hipart = (long)v; /* take the top 32 bits */
606 v = (v - (double)hipart) * 2147483648.0; /* get the next 32 bits */
607 x = hipart + (long)v + (expo << 15);
608 if (x == -1)
609 x = -2;
610 return x;
Fred Drake13634cf2000-06-29 19:17:04 +0000611}
612
613long
Fred Drake100814d2000-07-09 15:48:49 +0000614_Py_HashPointer(void *p)
Fred Drake13634cf2000-06-29 19:17:04 +0000615{
616#if SIZEOF_LONG >= SIZEOF_VOID_P
617 return (long)p;
618#else
619 /* convert to a Python long and hash that */
620 PyObject* longobj;
621 long x;
622
623 if ((longobj = PyLong_FromVoidPtr(p)) == NULL) {
624 x = -1;
625 goto finally;
626 }
627 x = PyObject_Hash(longobj);
628
629finally:
630 Py_XDECREF(longobj);
631 return x;
632#endif
633}
634
635
Guido van Rossum9bfef441993-03-29 10:43:31 +0000636long
Fred Drake100814d2000-07-09 15:48:49 +0000637PyObject_Hash(PyObject *v)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000638{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000639 PyTypeObject *tp = v->ob_type;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000640 if (tp->tp_hash != NULL)
641 return (*tp->tp_hash)(v);
Fred Drake13634cf2000-06-29 19:17:04 +0000642 if (tp->tp_compare == NULL) {
643 return _Py_HashPointer(v); /* Use address as hash value */
644 }
Guido van Rossum9bfef441993-03-29 10:43:31 +0000645 /* If there's a cmp but no hash defined, the object can't be hashed */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000646 PyErr_SetString(PyExc_TypeError, "unhashable type");
Guido van Rossum9bfef441993-03-29 10:43:31 +0000647 return -1;
648}
649
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000650PyObject *
Fred Drake100814d2000-07-09 15:48:49 +0000651PyObject_GetAttrString(PyObject *v, char *name)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000652{
Guido van Rossumd8eb1b31996-08-09 20:52:03 +0000653 if (v->ob_type->tp_getattro != NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000654 PyObject *w, *res;
Guido van Rossumc6d06701997-01-18 07:57:16 +0000655 w = PyString_InternFromString(name);
Guido van Rossumd8eb1b31996-08-09 20:52:03 +0000656 if (w == NULL)
657 return NULL;
658 res = (*v->ob_type->tp_getattro)(v, w);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000659 Py_XDECREF(w);
Guido van Rossumd8eb1b31996-08-09 20:52:03 +0000660 return res;
661 }
662
Guido van Rossum3f5da241990-12-20 15:06:42 +0000663 if (v->ob_type->tp_getattr == NULL) {
Guido van Rossumdb935161998-01-19 22:16:36 +0000664 PyErr_Format(PyExc_AttributeError,
665 "'%.50s' object has no attribute '%.400s'",
666 v->ob_type->tp_name,
667 name);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000668 return NULL;
669 }
670 else {
671 return (*v->ob_type->tp_getattr)(v, name);
672 }
673}
674
675int
Fred Drake100814d2000-07-09 15:48:49 +0000676PyObject_HasAttrString(PyObject *v, char *name)
Guido van Rossumed18fdc1993-07-11 19:55:34 +0000677{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000678 PyObject *res = PyObject_GetAttrString(v, name);
Guido van Rossumed18fdc1993-07-11 19:55:34 +0000679 if (res != NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000680 Py_DECREF(res);
Guido van Rossumed18fdc1993-07-11 19:55:34 +0000681 return 1;
682 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000683 PyErr_Clear();
Guido van Rossumed18fdc1993-07-11 19:55:34 +0000684 return 0;
685}
686
687int
Fred Drake100814d2000-07-09 15:48:49 +0000688PyObject_SetAttrString(PyObject *v, char *name, PyObject *w)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000689{
Guido van Rossumd8eb1b31996-08-09 20:52:03 +0000690 if (v->ob_type->tp_setattro != NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000691 PyObject *s;
Guido van Rossumd8eb1b31996-08-09 20:52:03 +0000692 int res;
Guido van Rossumc6d06701997-01-18 07:57:16 +0000693 s = PyString_InternFromString(name);
Guido van Rossumd8eb1b31996-08-09 20:52:03 +0000694 if (s == NULL)
Guido van Rossumb7fc3041996-09-11 22:51:25 +0000695 return -1;
Guido van Rossumd8eb1b31996-08-09 20:52:03 +0000696 res = (*v->ob_type->tp_setattro)(v, s, w);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000697 Py_XDECREF(s);
Guido van Rossumd8eb1b31996-08-09 20:52:03 +0000698 return res;
699 }
700
Guido van Rossum3f5da241990-12-20 15:06:42 +0000701 if (v->ob_type->tp_setattr == NULL) {
702 if (v->ob_type->tp_getattr == NULL)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000703 PyErr_SetString(PyExc_TypeError,
Guido van Rossum3ea74121991-12-24 13:28:03 +0000704 "attribute-less object (assign or del)");
Guido van Rossum3f5da241990-12-20 15:06:42 +0000705 else
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000706 PyErr_SetString(PyExc_TypeError,
Guido van Rossum3ea74121991-12-24 13:28:03 +0000707 "object has read-only attributes");
Guido van Rossum73531a31990-12-20 23:12:40 +0000708 return -1;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000709 }
710 else {
711 return (*v->ob_type->tp_setattr)(v, name, w);
712 }
713}
714
Guido van Rossum98ff96a1997-05-20 18:34:44 +0000715PyObject *
Fred Drake100814d2000-07-09 15:48:49 +0000716PyObject_GetAttr(PyObject *v, PyObject *name)
Guido van Rossum98ff96a1997-05-20 18:34:44 +0000717{
718 if (v->ob_type->tp_getattro != NULL)
719 return (*v->ob_type->tp_getattro)(v, name);
Jeremy Hylton99a8f902000-06-23 14:36:32 +0000720
721 if (!PyString_Check(name)) {
722 PyErr_SetString(PyExc_TypeError,
723 "attribute name must be string");
724 return NULL;
725 }
726 return PyObject_GetAttrString(v, PyString_AS_STRING(name));
Guido van Rossum98ff96a1997-05-20 18:34:44 +0000727}
728
729int
Fred Drake100814d2000-07-09 15:48:49 +0000730PyObject_HasAttr(PyObject *v, PyObject *name)
Guido van Rossum98ff96a1997-05-20 18:34:44 +0000731{
732 PyObject *res = PyObject_GetAttr(v, name);
733 if (res != NULL) {
734 Py_DECREF(res);
735 return 1;
736 }
737 PyErr_Clear();
738 return 0;
739}
740
741int
Fred Drake100814d2000-07-09 15:48:49 +0000742PyObject_SetAttr(PyObject *v, PyObject *name, PyObject *value)
Guido van Rossum98ff96a1997-05-20 18:34:44 +0000743{
744 int err;
745 Py_INCREF(name);
Jeremy Hylton99a8f902000-06-23 14:36:32 +0000746 if (PyString_Check(name))
747 PyString_InternInPlace(&name);
Guido van Rossum98ff96a1997-05-20 18:34:44 +0000748 if (v->ob_type->tp_setattro != NULL)
749 err = (*v->ob_type->tp_setattro)(v, name, value);
Jeremy Hylton99a8f902000-06-23 14:36:32 +0000750 else if (PyString_Check(name)) {
Guido van Rossum98ff96a1997-05-20 18:34:44 +0000751 err = PyObject_SetAttrString(
Jeremy Hylton99a8f902000-06-23 14:36:32 +0000752 v, PyString_AS_STRING(name), value);
753 }
754 else {
755 PyErr_SetString(PyExc_TypeError,
756 "attribute name must be string");
757 err = -1;
758 }
Guido van Rossum98ff96a1997-05-20 18:34:44 +0000759 Py_DECREF(name);
760 return err;
761}
762
Guido van Rossum6ac258d1993-05-12 08:24:20 +0000763/* Test a value used as condition, e.g., in a for or if statement.
764 Return -1 if an error occurred */
765
766int
Fred Drake100814d2000-07-09 15:48:49 +0000767PyObject_IsTrue(PyObject *v)
Guido van Rossum6ac258d1993-05-12 08:24:20 +0000768{
769 int res;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000770 if (v == Py_None)
Guido van Rossum6ac258d1993-05-12 08:24:20 +0000771 res = 0;
Guido van Rossum1c4f4581998-05-22 00:53:24 +0000772 else if (v->ob_type->tp_as_number != NULL &&
773 v->ob_type->tp_as_number->nb_nonzero != NULL)
Guido van Rossum6ac258d1993-05-12 08:24:20 +0000774 res = (*v->ob_type->tp_as_number->nb_nonzero)(v);
Guido van Rossum1c4f4581998-05-22 00:53:24 +0000775 else if (v->ob_type->tp_as_mapping != NULL &&
776 v->ob_type->tp_as_mapping->mp_length != NULL)
Guido van Rossum6ac258d1993-05-12 08:24:20 +0000777 res = (*v->ob_type->tp_as_mapping->mp_length)(v);
Guido van Rossum1c4f4581998-05-22 00:53:24 +0000778 else if (v->ob_type->tp_as_sequence != NULL &&
779 v->ob_type->tp_as_sequence->sq_length != NULL)
Guido van Rossum6ac258d1993-05-12 08:24:20 +0000780 res = (*v->ob_type->tp_as_sequence->sq_length)(v);
781 else
782 res = 1;
783 if (res > 0)
784 res = 1;
785 return res;
786}
787
Guido van Rossumc3d3f961998-04-09 17:53:59 +0000788/* equivalent of 'not v'
789 Return -1 if an error occurred */
790
791int
Fred Drake100814d2000-07-09 15:48:49 +0000792PyObject_Not(PyObject *v)
Guido van Rossumc3d3f961998-04-09 17:53:59 +0000793{
794 int res;
795 res = PyObject_IsTrue(v);
796 if (res < 0)
797 return res;
798 return res == 0;
799}
800
Guido van Rossum5524a591995-01-10 15:26:20 +0000801/* Coerce two numeric types to the "larger" one.
802 Increment the reference count on each argument.
803 Return -1 and raise an exception if no coercion is possible
804 (and then no reference count is incremented).
805*/
806
807int
Fred Drake100814d2000-07-09 15:48:49 +0000808PyNumber_CoerceEx(PyObject **pv, PyObject **pw)
Guido van Rossum5524a591995-01-10 15:26:20 +0000809{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000810 register PyObject *v = *pv;
811 register PyObject *w = *pw;
Guido van Rossum5524a591995-01-10 15:26:20 +0000812 int res;
813
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000814 if (v->ob_type == w->ob_type && !PyInstance_Check(v)) {
815 Py_INCREF(v);
816 Py_INCREF(w);
Guido van Rossum5524a591995-01-10 15:26:20 +0000817 return 0;
818 }
819 if (v->ob_type->tp_as_number && v->ob_type->tp_as_number->nb_coerce) {
820 res = (*v->ob_type->tp_as_number->nb_coerce)(pv, pw);
821 if (res <= 0)
822 return res;
823 }
824 if (w->ob_type->tp_as_number && w->ob_type->tp_as_number->nb_coerce) {
825 res = (*w->ob_type->tp_as_number->nb_coerce)(pw, pv);
826 if (res <= 0)
827 return res;
828 }
Guido van Rossum242c6421997-11-19 16:03:17 +0000829 return 1;
830}
831
832int
Fred Drake100814d2000-07-09 15:48:49 +0000833PyNumber_Coerce(PyObject **pv, PyObject **pw)
Guido van Rossum242c6421997-11-19 16:03:17 +0000834{
835 int err = PyNumber_CoerceEx(pv, pw);
836 if (err <= 0)
837 return err;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000838 PyErr_SetString(PyExc_TypeError, "number coercion failed");
Guido van Rossum5524a591995-01-10 15:26:20 +0000839 return -1;
840}
841
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000842
Guido van Rossum49b11fe1995-01-26 00:38:22 +0000843/* Test whether an object can be called */
844
845int
Fred Drake100814d2000-07-09 15:48:49 +0000846PyCallable_Check(PyObject *x)
Guido van Rossum49b11fe1995-01-26 00:38:22 +0000847{
848 if (x == NULL)
849 return 0;
850 if (x->ob_type->tp_call != NULL ||
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000851 PyFunction_Check(x) ||
852 PyMethod_Check(x) ||
853 PyCFunction_Check(x) ||
854 PyClass_Check(x))
Guido van Rossum49b11fe1995-01-26 00:38:22 +0000855 return 1;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000856 if (PyInstance_Check(x)) {
857 PyObject *call = PyObject_GetAttrString(x, "__call__");
Guido van Rossum49b11fe1995-01-26 00:38:22 +0000858 if (call == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000859 PyErr_Clear();
Guido van Rossum49b11fe1995-01-26 00:38:22 +0000860 return 0;
861 }
862 /* Could test recursively but don't, for fear of endless
863 recursion if some joker sets self.__call__ = self */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000864 Py_DECREF(call);
Guido van Rossum49b11fe1995-01-26 00:38:22 +0000865 return 1;
866 }
867 return 0;
868}
869
870
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000871/*
872NoObject is usable as a non-NULL undefined value, used by the macro None.
873There is (and should be!) no way to create other objects of this type,
Guido van Rossum3f5da241990-12-20 15:06:42 +0000874so there is exactly one (which is indestructible, by the way).
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000875*/
876
Guido van Rossum0c182a11992-03-27 17:26:13 +0000877/* ARGSUSED */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000878static PyObject *
Fred Drake100814d2000-07-09 15:48:49 +0000879none_repr(PyObject *op)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000880{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000881 return PyString_FromString("None");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000882}
883
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000884static PyTypeObject PyNothing_Type = {
885 PyObject_HEAD_INIT(&PyType_Type)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000886 0,
Guido van Rossum3f5da241990-12-20 15:06:42 +0000887 "None",
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000888 0,
889 0,
890 0, /*tp_dealloc*/ /*never called*/
Guido van Rossum7066dd71992-09-17 17:54:56 +0000891 0, /*tp_print*/
Guido van Rossum3f5da241990-12-20 15:06:42 +0000892 0, /*tp_getattr*/
893 0, /*tp_setattr*/
894 0, /*tp_compare*/
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000895 (reprfunc)none_repr, /*tp_repr*/
Guido van Rossum3f5da241990-12-20 15:06:42 +0000896 0, /*tp_as_number*/
897 0, /*tp_as_sequence*/
898 0, /*tp_as_mapping*/
Guido van Rossum9bfef441993-03-29 10:43:31 +0000899 0, /*tp_hash */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000900};
901
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000902PyObject _Py_NoneStruct = {
903 PyObject_HEAD_INIT(&PyNothing_Type)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000904};
905
906
Guido van Rossum84a90321996-05-22 16:34:47 +0000907#ifdef Py_TRACE_REFS
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000908
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000909static PyObject refchain = {&refchain, &refchain};
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000910
Guido van Rossumaacdc9d1996-08-12 21:32:12 +0000911void
Fred Drake100814d2000-07-09 15:48:49 +0000912_Py_ResetReferences(void)
Guido van Rossume09fb551997-08-05 02:04:34 +0000913{
914 refchain._ob_prev = refchain._ob_next = &refchain;
915 _Py_RefTotal = 0;
916}
917
918void
Fred Drake100814d2000-07-09 15:48:49 +0000919_Py_NewReference(PyObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000920{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000921 _Py_RefTotal++;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000922 op->ob_refcnt = 1;
923 op->_ob_next = refchain._ob_next;
924 op->_ob_prev = &refchain;
925 refchain._ob_next->_ob_prev = op;
926 refchain._ob_next = op;
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +0000927#ifdef COUNT_ALLOCS
928 inc_count(op->ob_type);
929#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000930}
931
Guido van Rossumaacdc9d1996-08-12 21:32:12 +0000932void
Fred Drake100814d2000-07-09 15:48:49 +0000933_Py_ForgetReference(register PyObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000934{
Guido van Rossumbffd6832000-01-20 22:32:56 +0000935#ifdef SLOW_UNREF_CHECK
Guido van Rossum4c08d552000-03-10 22:55:18 +0000936 register PyObject *p;
Guido van Rossumbffd6832000-01-20 22:32:56 +0000937#endif
Guido van Rossumd7047b31995-01-02 19:07:15 +0000938 if (op->ob_refcnt < 0)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000939 Py_FatalError("UNREF negative refcnt");
Guido van Rossum2e8f6141992-09-03 20:32:55 +0000940 if (op == &refchain ||
Guido van Rossumd7047b31995-01-02 19:07:15 +0000941 op->_ob_prev->_ob_next != op || op->_ob_next->_ob_prev != op)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000942 Py_FatalError("UNREF invalid object");
Guido van Rossum2e8f6141992-09-03 20:32:55 +0000943#ifdef SLOW_UNREF_CHECK
Guido van Rossum3f5da241990-12-20 15:06:42 +0000944 for (p = refchain._ob_next; p != &refchain; p = p->_ob_next) {
945 if (p == op)
946 break;
947 }
Guido van Rossumd7047b31995-01-02 19:07:15 +0000948 if (p == &refchain) /* Not found */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000949 Py_FatalError("UNREF unknown object");
Guido van Rossum2e8f6141992-09-03 20:32:55 +0000950#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000951 op->_ob_next->_ob_prev = op->_ob_prev;
952 op->_ob_prev->_ob_next = op->_ob_next;
Guido van Rossum2e8f6141992-09-03 20:32:55 +0000953 op->_ob_next = op->_ob_prev = NULL;
Guido van Rossumd8953cb1995-04-06 14:46:26 +0000954#ifdef COUNT_ALLOCS
955 op->ob_type->tp_free++;
956#endif
Guido van Rossum3f5da241990-12-20 15:06:42 +0000957}
958
Guido van Rossumaacdc9d1996-08-12 21:32:12 +0000959void
Fred Drake100814d2000-07-09 15:48:49 +0000960_Py_Dealloc(PyObject *op)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000961{
Guido van Rossum9776adf1994-09-07 14:36:45 +0000962 destructor dealloc = op->ob_type->tp_dealloc;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000963 _Py_ForgetReference(op);
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000964#ifndef WITH_CYCLE_GC
Guido van Rossume92e6102000-04-24 15:40:53 +0000965 if (_PyTrash_delete_nesting < PyTrash_UNWIND_LEVEL-1)
966 op->ob_type = NULL;
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000967#endif
Guido van Rossum9776adf1994-09-07 14:36:45 +0000968 (*dealloc)(op);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000969}
970
Guido van Rossumaacdc9d1996-08-12 21:32:12 +0000971void
Fred Drake100814d2000-07-09 15:48:49 +0000972_Py_PrintReferences(FILE *fp)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000973{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000974 PyObject *op;
Guido van Rossume09fb551997-08-05 02:04:34 +0000975 fprintf(fp, "Remaining objects:\n");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000976 for (op = refchain._ob_next; op != &refchain; op = op->_ob_next) {
977 fprintf(fp, "[%d] ", op->ob_refcnt);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000978 if (PyObject_Print(op, fp, 0) != 0)
979 PyErr_Clear();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000980 putc('\n', fp);
981 }
982}
983
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000984PyObject *
Fred Drake100814d2000-07-09 15:48:49 +0000985_Py_GetObjects(PyObject *self, PyObject *args)
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000986{
987 int i, n;
988 PyObject *t = NULL;
989 PyObject *res, *op;
990
991 if (!PyArg_ParseTuple(args, "i|O", &n, &t))
992 return NULL;
993 op = refchain._ob_next;
994 res = PyList_New(0);
995 if (res == NULL)
996 return NULL;
997 for (i = 0; (n == 0 || i < n) && op != &refchain; i++) {
998 while (op == self || op == args || op == res || op == t ||
999 t != NULL && op->ob_type != (PyTypeObject *) t) {
1000 op = op->_ob_next;
1001 if (op == &refchain)
1002 return res;
1003 }
1004 if (PyList_Append(res, op) < 0) {
1005 Py_DECREF(res);
1006 return NULL;
1007 }
1008 op = op->_ob_next;
1009 }
1010 return res;
1011}
1012
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001013#endif
Guido van Rossum97ead3f1996-01-12 01:24:09 +00001014
1015
1016/* Hack to force loading of cobject.o */
Guido van Rossumda9c2711996-12-05 21:58:58 +00001017PyTypeObject *_Py_cobject_hack = &PyCObject_Type;
Guido van Rossum84a90321996-05-22 16:34:47 +00001018
1019
1020/* Hack to force loading of abstract.o */
Jeremy Hylton6253f832000-07-12 12:56:19 +00001021int (*_Py_abstract_hack)(PyObject *) = &PyObject_Size;
Guido van Rossume09fb551997-08-05 02:04:34 +00001022
1023
Andrew M. Kuchling1582a3a2000-08-16 12:27:23 +00001024/* Python's malloc wrappers (see pymem.h) */
Guido van Rossume09fb551997-08-05 02:04:34 +00001025
Thomas Wouters334fb892000-07-25 12:56:38 +00001026void *
Fred Drake100814d2000-07-09 15:48:49 +00001027PyMem_Malloc(size_t nbytes)
Guido van Rossume09fb551997-08-05 02:04:34 +00001028{
1029#if _PyMem_EXTRA > 0
1030 if (nbytes == 0)
1031 nbytes = _PyMem_EXTRA;
1032#endif
Guido van Rossumb18618d2000-05-03 23:44:39 +00001033 return PyMem_MALLOC(nbytes);
Guido van Rossume09fb551997-08-05 02:04:34 +00001034}
1035
Thomas Wouters334fb892000-07-25 12:56:38 +00001036void *
1037PyMem_Realloc(void *p, size_t nbytes)
Guido van Rossume09fb551997-08-05 02:04:34 +00001038{
1039#if _PyMem_EXTRA > 0
1040 if (nbytes == 0)
1041 nbytes = _PyMem_EXTRA;
1042#endif
Guido van Rossumb18618d2000-05-03 23:44:39 +00001043 return PyMem_REALLOC(p, nbytes);
Guido van Rossume09fb551997-08-05 02:04:34 +00001044}
1045
1046void
Thomas Wouters334fb892000-07-25 12:56:38 +00001047PyMem_Free(void *p)
Guido van Rossume09fb551997-08-05 02:04:34 +00001048{
Guido van Rossumb18618d2000-05-03 23:44:39 +00001049 PyMem_FREE(p);
1050}
1051
1052
1053/* Python's object malloc wrappers (see objimpl.h) */
1054
Thomas Wouters334fb892000-07-25 12:56:38 +00001055void *
Fred Drake100814d2000-07-09 15:48:49 +00001056PyObject_Malloc(size_t nbytes)
Guido van Rossumb18618d2000-05-03 23:44:39 +00001057{
1058 return PyObject_MALLOC(nbytes);
1059}
1060
Thomas Wouters334fb892000-07-25 12:56:38 +00001061void *
1062PyObject_Realloc(void *p, size_t nbytes)
Guido van Rossumb18618d2000-05-03 23:44:39 +00001063{
1064 return PyObject_REALLOC(p, nbytes);
1065}
1066
1067void
Thomas Wouters334fb892000-07-25 12:56:38 +00001068PyObject_Free(void *p)
Guido van Rossumb18618d2000-05-03 23:44:39 +00001069{
1070 PyObject_FREE(p);
Guido van Rossume09fb551997-08-05 02:04:34 +00001071}
Guido van Rossum86610361998-04-10 22:32:46 +00001072
1073
1074/* These methods are used to control infinite recursion in repr, str, print,
1075 etc. Container objects that may recursively contain themselves,
1076 e.g. builtin dictionaries and lists, should used Py_ReprEnter() and
1077 Py_ReprLeave() to avoid infinite recursion.
1078
1079 Py_ReprEnter() returns 0 the first time it is called for a particular
1080 object and 1 every time thereafter. It returns -1 if an exception
1081 occurred. Py_ReprLeave() has no return value.
1082
1083 See dictobject.c and listobject.c for examples of use.
1084*/
1085
1086#define KEY "Py_Repr"
1087
1088int
Fred Drake100814d2000-07-09 15:48:49 +00001089Py_ReprEnter(PyObject *obj)
Guido van Rossum86610361998-04-10 22:32:46 +00001090{
1091 PyObject *dict;
1092 PyObject *list;
1093 int i;
1094
1095 dict = PyThreadState_GetDict();
1096 if (dict == NULL)
1097 return -1;
1098 list = PyDict_GetItemString(dict, KEY);
1099 if (list == NULL) {
1100 list = PyList_New(0);
1101 if (list == NULL)
1102 return -1;
1103 if (PyDict_SetItemString(dict, KEY, list) < 0)
1104 return -1;
1105 Py_DECREF(list);
1106 }
1107 i = PyList_GET_SIZE(list);
1108 while (--i >= 0) {
1109 if (PyList_GET_ITEM(list, i) == obj)
1110 return 1;
1111 }
1112 PyList_Append(list, obj);
1113 return 0;
1114}
1115
1116void
Fred Drake100814d2000-07-09 15:48:49 +00001117Py_ReprLeave(PyObject *obj)
Guido van Rossum86610361998-04-10 22:32:46 +00001118{
1119 PyObject *dict;
1120 PyObject *list;
1121 int i;
1122
1123 dict = PyThreadState_GetDict();
Guido van Rossumeb909461998-04-11 15:17:34 +00001124 if (dict == NULL)
1125 return;
Guido van Rossum86610361998-04-10 22:32:46 +00001126 list = PyDict_GetItemString(dict, KEY);
Guido van Rossumeb909461998-04-11 15:17:34 +00001127 if (list == NULL || !PyList_Check(list))
1128 return;
Guido van Rossum86610361998-04-10 22:32:46 +00001129 i = PyList_GET_SIZE(list);
1130 /* Count backwards because we always expect obj to be list[-1] */
1131 while (--i >= 0) {
1132 if (PyList_GET_ITEM(list, i) == obj) {
1133 PyList_SetSlice(list, i, i + 1, NULL);
1134 break;
1135 }
1136 }
1137}
Guido van Rossumd724b232000-03-13 16:01:29 +00001138
1139/*
1140 trashcan
1141 CT 2k0130
1142 non-recursively destroy nested objects
1143
1144 CT 2k0223
1145 everything is now done in a macro.
1146
1147 CT 2k0305
1148 modified to use functions, after Tim Peter's suggestion.
1149
1150 CT 2k0309
1151 modified to restore a possible error.
Guido van Rossum13ff8eb2000-03-25 18:39:19 +00001152
1153 CT 2k0325
1154 added better safe than sorry check for threadstate
Guido van Rossume92e6102000-04-24 15:40:53 +00001155
1156 CT 2k0422
1157 complete rewrite. We now build a chain via ob_type
1158 and save the limited number of types in ob_refcnt.
1159 This is perfect since we don't need any memory.
1160 A patch for free-threading would need just a lock.
Guido van Rossumd724b232000-03-13 16:01:29 +00001161*/
1162
Guido van Rossume92e6102000-04-24 15:40:53 +00001163#define Py_TRASHCAN_TUPLE 1
1164#define Py_TRASHCAN_LIST 2
1165#define Py_TRASHCAN_DICT 3
1166#define Py_TRASHCAN_FRAME 4
1167#define Py_TRASHCAN_TRACEBACK 5
1168/* extend here if other objects want protection */
1169
Guido van Rossumd724b232000-03-13 16:01:29 +00001170int _PyTrash_delete_nesting = 0;
Guido van Rossume92e6102000-04-24 15:40:53 +00001171
Guido van Rossumd724b232000-03-13 16:01:29 +00001172PyObject * _PyTrash_delete_later = NULL;
1173
1174void
Fred Drake100814d2000-07-09 15:48:49 +00001175_PyTrash_deposit_object(PyObject *op)
Guido van Rossumd724b232000-03-13 16:01:29 +00001176{
Guido van Rossume92e6102000-04-24 15:40:53 +00001177 int typecode;
Guido van Rossum13ff8eb2000-03-25 18:39:19 +00001178
Guido van Rossume92e6102000-04-24 15:40:53 +00001179 if (PyTuple_Check(op))
1180 typecode = Py_TRASHCAN_TUPLE;
1181 else if (PyList_Check(op))
1182 typecode = Py_TRASHCAN_LIST;
1183 else if (PyDict_Check(op))
1184 typecode = Py_TRASHCAN_DICT;
1185 else if (PyFrame_Check(op))
1186 typecode = Py_TRASHCAN_FRAME;
1187 else if (PyTraceBack_Check(op))
1188 typecode = Py_TRASHCAN_TRACEBACK;
Moshe Zadkacf703f02000-08-04 15:36:13 +00001189 else /* We have a bug here -- those are the only types in GC */ {
1190 Py_FatalError("Type not supported in GC -- internal bug");
1191 return; /* pacify compiler -- execution never here */
1192 }
Guido van Rossume92e6102000-04-24 15:40:53 +00001193 op->ob_refcnt = typecode;
Guido van Rossumd724b232000-03-13 16:01:29 +00001194
Guido van Rossume92e6102000-04-24 15:40:53 +00001195 op->ob_type = (PyTypeObject*)_PyTrash_delete_later;
1196 _PyTrash_delete_later = op;
Guido van Rossumd724b232000-03-13 16:01:29 +00001197}
1198
1199void
Fred Drake100814d2000-07-09 15:48:49 +00001200_PyTrash_destroy_chain(void)
Guido van Rossumd724b232000-03-13 16:01:29 +00001201{
1202 while (_PyTrash_delete_later) {
1203 PyObject *shredder = _PyTrash_delete_later;
Guido van Rossume92e6102000-04-24 15:40:53 +00001204 _PyTrash_delete_later = (PyObject*) shredder->ob_type;
1205
1206 switch (shredder->ob_refcnt) {
1207 case Py_TRASHCAN_TUPLE:
1208 shredder->ob_type = &PyTuple_Type;
1209 break;
1210 case Py_TRASHCAN_LIST:
1211 shredder->ob_type = &PyList_Type;
1212 break;
1213 case Py_TRASHCAN_DICT:
1214 shredder->ob_type = &PyDict_Type;
1215 break;
1216 case Py_TRASHCAN_FRAME:
1217 shredder->ob_type = &PyFrame_Type;
1218 break;
1219 case Py_TRASHCAN_TRACEBACK:
1220 shredder->ob_type = &PyTraceBack_Type;
1221 break;
1222 }
1223 _Py_NewReference(shredder);
1224
Guido van Rossumd724b232000-03-13 16:01:29 +00001225 ++_PyTrash_delete_nesting;
1226 Py_DECREF(shredder);
1227 --_PyTrash_delete_nesting;
1228 }
1229}