blob: 9b7c551d85bcf153698a8b7f0784a54a600377fb [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"
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00005
Jack Jansen28fc8802000-07-11 21:47:20 +00006#ifdef macintosh
7#include "macglue.h"
8#endif
9
Guido van Rossume92e6102000-04-24 15:40:53 +000010/* just for trashcan: */
11#include "compile.h"
12#include "frameobject.h"
13#include "traceback.h"
14
Guido van Rossum6f9e4331995-03-29 16:57:48 +000015#if defined( Py_TRACE_REFS ) || defined( Py_REF_DEBUG )
Guido van Rossumbffd6832000-01-20 22:32:56 +000016DL_IMPORT(long) _Py_RefTotal;
Guido van Rossum3f5da241990-12-20 15:06:42 +000017#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000018
Guido van Rossum3f5da241990-12-20 15:06:42 +000019/* Object allocation routines used by NEWOBJ and NEWVAROBJ macros.
20 These are used by the individual routines for object creation.
21 Do not call them otherwise, they do not initialize the object! */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000022
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +000023#ifdef COUNT_ALLOCS
Guido van Rossumc0b618a1997-05-02 03:12:38 +000024static PyTypeObject *type_list;
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +000025extern int tuple_zero_allocs, fast_tuple_allocs;
26extern int quick_int_allocs, quick_neg_int_allocs;
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +000027extern int null_strings, one_strings;
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +000028void
Fred Drake100814d2000-07-09 15:48:49 +000029dump_counts(void)
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +000030{
Guido van Rossumc0b618a1997-05-02 03:12:38 +000031 PyTypeObject *tp;
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +000032
33 for (tp = type_list; tp; tp = tp->tp_next)
Sjoerd Mullender52c1f511993-10-25 08:40:52 +000034 fprintf(stderr, "%s alloc'd: %d, freed: %d, max in use: %d\n",
35 tp->tp_name, tp->tp_alloc, tp->tp_free,
36 tp->tp_maxalloc);
37 fprintf(stderr, "fast tuple allocs: %d, empty: %d\n",
38 fast_tuple_allocs, tuple_zero_allocs);
39 fprintf(stderr, "fast int allocs: pos: %d, neg: %d\n",
40 quick_int_allocs, quick_neg_int_allocs);
41 fprintf(stderr, "null strings: %d, 1-strings: %d\n",
42 null_strings, one_strings);
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +000043}
44
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +000045PyObject *
Fred Drake100814d2000-07-09 15:48:49 +000046get_counts(void)
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +000047{
48 PyTypeObject *tp;
49 PyObject *result;
50 PyObject *v;
51
52 result = PyList_New(0);
53 if (result == NULL)
54 return NULL;
55 for (tp = type_list; tp; tp = tp->tp_next) {
56 v = Py_BuildValue("(siii)", tp->tp_name, tp->tp_alloc,
57 tp->tp_free, tp->tp_maxalloc);
58 if (v == NULL) {
59 Py_DECREF(result);
60 return NULL;
61 }
62 if (PyList_Append(result, v) < 0) {
63 Py_DECREF(v);
64 Py_DECREF(result);
65 return NULL;
66 }
67 Py_DECREF(v);
68 }
69 return result;
70}
71
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +000072void
Fred Drake100814d2000-07-09 15:48:49 +000073inc_count(PyTypeObject *tp)
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +000074{
75 if (tp->tp_alloc == 0) {
Guido van Rossumd8953cb1995-04-06 14:46:26 +000076 /* first time; insert in linked list */
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +000077 if (tp->tp_next != NULL) /* sanity check */
Guido van Rossumc0b618a1997-05-02 03:12:38 +000078 Py_FatalError("XXX inc_count sanity check");
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +000079 tp->tp_next = type_list;
80 type_list = tp;
81 }
82 tp->tp_alloc++;
83 if (tp->tp_alloc - tp->tp_free > tp->tp_maxalloc)
84 tp->tp_maxalloc = tp->tp_alloc - tp->tp_free;
85}
86#endif
87
Guido van Rossumc0b618a1997-05-02 03:12:38 +000088PyObject *
Fred Drake100814d2000-07-09 15:48:49 +000089PyObject_Init(PyObject *op, PyTypeObject *tp)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000090{
Guido van Rossumb18618d2000-05-03 23:44:39 +000091 if (op == NULL) {
92 PyErr_SetString(PyExc_SystemError,
93 "NULL object passed to PyObject_Init");
94 return op;
95 }
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +000096#ifdef WITH_CYCLE_GC
97 if (PyType_IS_GC(tp))
98 op = (PyObject *) PyObject_FROM_GC(op);
99#endif
Guido van Rossumb18618d2000-05-03 23:44:39 +0000100 /* Any changes should be reflected in PyObject_INIT (objimpl.h) */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000101 op->ob_type = tp;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000102 _Py_NewReference(op);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000103 return op;
104}
105
Guido van Rossumb18618d2000-05-03 23:44:39 +0000106PyVarObject *
Fred Drake100814d2000-07-09 15:48:49 +0000107PyObject_InitVar(PyVarObject *op, PyTypeObject *tp, int size)
Guido van Rossumb18618d2000-05-03 23:44:39 +0000108{
109 if (op == NULL) {
110 PyErr_SetString(PyExc_SystemError,
111 "NULL object passed to PyObject_InitVar");
112 return op;
113 }
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000114#ifdef WITH_CYCLE_GC
115 if (PyType_IS_GC(tp))
116 op = (PyVarObject *) PyObject_FROM_GC(op);
117#endif
Guido van Rossumb18618d2000-05-03 23:44:39 +0000118 /* Any changes should be reflected in PyObject_INIT_VAR */
119 op->ob_size = size;
120 op->ob_type = tp;
121 _Py_NewReference((PyObject *)op);
122 return op;
123}
124
125PyObject *
Fred Drake100814d2000-07-09 15:48:49 +0000126_PyObject_New(PyTypeObject *tp)
Guido van Rossumb18618d2000-05-03 23:44:39 +0000127{
128 PyObject *op;
129 op = (PyObject *) PyObject_MALLOC(_PyObject_SIZE(tp));
130 if (op == NULL)
131 return PyErr_NoMemory();
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000132#ifdef WITH_CYCLE_GC
133 if (PyType_IS_GC(tp))
134 op = (PyObject *) PyObject_FROM_GC(op);
135#endif
Guido van Rossumb18618d2000-05-03 23:44:39 +0000136 return PyObject_INIT(op, tp);
137}
138
Guido van Rossumd0c87ee1997-05-15 21:31:03 +0000139PyVarObject *
Fred Drake100814d2000-07-09 15:48:49 +0000140_PyObject_NewVar(PyTypeObject *tp, int size)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000141{
Guido van Rossumb18618d2000-05-03 23:44:39 +0000142 PyVarObject *op;
143 op = (PyVarObject *) PyObject_MALLOC(_PyObject_VAR_SIZE(tp, size));
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000144 if (op == NULL)
Guido van Rossumd0c87ee1997-05-15 21:31:03 +0000145 return (PyVarObject *)PyErr_NoMemory();
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000146#ifdef WITH_CYCLE_GC
147 if (PyType_IS_GC(tp))
148 op = (PyVarObject *) PyObject_FROM_GC(op);
149#endif
Guido van Rossumb18618d2000-05-03 23:44:39 +0000150 return PyObject_INIT_VAR(op, tp, size);
151}
152
153void
Fred Drake100814d2000-07-09 15:48:49 +0000154_PyObject_Del(PyObject *op)
Guido van Rossumb18618d2000-05-03 23:44:39 +0000155{
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000156#ifdef WITH_CYCLE_GC
Guido van Rossum4cc6ac72000-07-01 01:00:38 +0000157 if (op && PyType_IS_GC(op->ob_type)) {
158 op = (PyObject *) PyObject_AS_GC(op);
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000159 }
Guido van Rossum4cc6ac72000-07-01 01:00:38 +0000160#endif
161 PyObject_FREE(op);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000162}
163
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000164#ifndef WITH_CYCLE_GC
165/* extension modules might need these */
166void _PyGC_Insert(PyObject *op) { }
167void _PyGC_Remove(PyObject *op) { }
168#endif
169
Guido van Rossum90933611991-06-07 16:10:43 +0000170int
Fred Drake100814d2000-07-09 15:48:49 +0000171PyObject_Print(PyObject *op, FILE *fp, int flags)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000172{
Guido van Rossum278ef591991-07-27 21:40:24 +0000173 int ret = 0;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000174 if (PyErr_CheckSignals())
Guido van Rossum90933611991-06-07 16:10:43 +0000175 return -1;
Guido van Rossum9b00dfa1998-04-28 16:06:54 +0000176#ifdef USE_STACKCHECK
177 if (PyOS_CheckStack()) {
178 PyErr_SetString(PyExc_MemoryError, "Stack overflow");
179 return -1;
180 }
181#endif
Guido van Rossum687ef6e2000-01-12 16:28:58 +0000182 clearerr(fp); /* Clear any previous error condition */
Guido van Rossum90933611991-06-07 16:10:43 +0000183 if (op == NULL) {
184 fprintf(fp, "<nil>");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000185 }
Guido van Rossum90933611991-06-07 16:10:43 +0000186 else {
187 if (op->ob_refcnt <= 0)
Fred Drakea44d3532000-06-30 15:01:00 +0000188 fprintf(fp, "<refcnt %u at %p>",
189 op->ob_refcnt, op);
Guido van Rossum2e8f6141992-09-03 20:32:55 +0000190 else if (op->ob_type->tp_print == NULL) {
191 if (op->ob_type->tp_repr == NULL) {
Fred Drakea44d3532000-06-30 15:01:00 +0000192 fprintf(fp, "<%s object at %p>",
193 op->ob_type->tp_name, op);
Guido van Rossum2e8f6141992-09-03 20:32:55 +0000194 }
195 else {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000196 PyObject *s;
197 if (flags & Py_PRINT_RAW)
198 s = PyObject_Str(op);
Guido van Rossumc6004111993-11-05 10:22:19 +0000199 else
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000200 s = PyObject_Repr(op);
Guido van Rossum2e8f6141992-09-03 20:32:55 +0000201 if (s == NULL)
202 ret = -1;
Guido van Rossum2e8f6141992-09-03 20:32:55 +0000203 else {
Guido van Rossum565798d1998-04-21 22:25:01 +0000204 ret = PyObject_Print(s, fp,
205 Py_PRINT_RAW);
Guido van Rossum2e8f6141992-09-03 20:32:55 +0000206 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000207 Py_XDECREF(s);
Guido van Rossum2e8f6141992-09-03 20:32:55 +0000208 }
209 }
Guido van Rossum90933611991-06-07 16:10:43 +0000210 else
Guido van Rossum278ef591991-07-27 21:40:24 +0000211 ret = (*op->ob_type->tp_print)(op, fp, flags);
Guido van Rossum90933611991-06-07 16:10:43 +0000212 }
Guido van Rossum278ef591991-07-27 21:40:24 +0000213 if (ret == 0) {
214 if (ferror(fp)) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000215 PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossum278ef591991-07-27 21:40:24 +0000216 clearerr(fp);
217 ret = -1;
218 }
219 }
220 return ret;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000221}
222
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000223PyObject *
Fred Drake100814d2000-07-09 15:48:49 +0000224PyObject_Repr(PyObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000225{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000226 if (PyErr_CheckSignals())
Guido van Rossum90933611991-06-07 16:10:43 +0000227 return NULL;
Guido van Rossum9b00dfa1998-04-28 16:06:54 +0000228#ifdef USE_STACKCHECK
229 if (PyOS_CheckStack()) {
230 PyErr_SetString(PyExc_MemoryError, "Stack overflow");
231 return NULL;
232 }
233#endif
Guido van Rossum90933611991-06-07 16:10:43 +0000234 if (v == NULL)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000235 return PyString_FromString("<NULL>");
Guido van Rossum90933611991-06-07 16:10:43 +0000236 else if (v->ob_type->tp_repr == NULL) {
237 char buf[120];
Fred Drakea44d3532000-06-30 15:01:00 +0000238 sprintf(buf, "<%.80s object at %p>",
239 v->ob_type->tp_name, v);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000240 return PyString_FromString(buf);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000241 }
Guido van Rossum4c08d552000-03-10 22:55:18 +0000242 else {
243 PyObject *res;
244 res = (*v->ob_type->tp_repr)(v);
245 if (res == NULL)
246 return NULL;
Fredrik Lundhefecc7d2000-07-01 14:31:09 +0000247 if (PyUnicode_Check(res)) {
248 PyObject* str;
Fredrik Lundh2a1e0602000-07-08 17:43:32 +0000249 str = PyUnicode_AsUnicodeEscapeString(res);
Marc-André Lemburg891bc652000-07-03 09:57:53 +0000250 Py_DECREF(res);
251 if (str)
Fredrik Lundhefecc7d2000-07-01 14:31:09 +0000252 res = str;
Marc-André Lemburg891bc652000-07-03 09:57:53 +0000253 else
254 return NULL;
Fredrik Lundhefecc7d2000-07-01 14:31:09 +0000255 }
Guido van Rossum4c08d552000-03-10 22:55:18 +0000256 if (!PyString_Check(res)) {
257 PyErr_Format(PyExc_TypeError,
Guido van Rossum5db862d2000-04-10 12:46:51 +0000258 "__repr__ returned non-string (type %.200s)",
Guido van Rossum4c08d552000-03-10 22:55:18 +0000259 res->ob_type->tp_name);
260 Py_DECREF(res);
261 return NULL;
262 }
263 return res;
264 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000265}
266
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000267PyObject *
Fred Drake100814d2000-07-09 15:48:49 +0000268PyObject_Str(PyObject *v)
Guido van Rossumc6004111993-11-05 10:22:19 +0000269{
Guido van Rossum4c08d552000-03-10 22:55:18 +0000270 PyObject *res;
271
Guido van Rossumc6004111993-11-05 10:22:19 +0000272 if (v == NULL)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000273 return PyString_FromString("<NULL>");
274 else if (PyString_Check(v)) {
275 Py_INCREF(v);
Guido van Rossumc6004111993-11-05 10:22:19 +0000276 return v;
277 }
Guido van Rossum32b582b1995-01-17 16:35:13 +0000278 else if (v->ob_type->tp_str != NULL)
Guido van Rossum4c08d552000-03-10 22:55:18 +0000279 res = (*v->ob_type->tp_str)(v);
Guido van Rossumc6004111993-11-05 10:22:19 +0000280 else {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000281 PyObject *func;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000282 if (!PyInstance_Check(v) ||
283 (func = PyObject_GetAttrString(v, "__str__")) == NULL) {
284 PyErr_Clear();
285 return PyObject_Repr(v);
Guido van Rossumc6004111993-11-05 10:22:19 +0000286 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000287 res = PyEval_CallObject(func, (PyObject *)NULL);
288 Py_DECREF(func);
Guido van Rossumc6004111993-11-05 10:22:19 +0000289 }
Guido van Rossum4c08d552000-03-10 22:55:18 +0000290 if (res == NULL)
291 return NULL;
Marc-André Lemburg891bc652000-07-03 09:57:53 +0000292 if (PyUnicode_Check(res)) {
293 PyObject* str;
294 str = PyUnicode_AsEncodedString(res, NULL, NULL);
295 Py_DECREF(res);
296 if (str)
297 res = str;
298 else
299 return NULL;
300 }
Guido van Rossum4c08d552000-03-10 22:55:18 +0000301 if (!PyString_Check(res)) {
302 PyErr_Format(PyExc_TypeError,
Guido van Rossum5db862d2000-04-10 12:46:51 +0000303 "__str__ returned non-string (type %.200s)",
Guido van Rossum4c08d552000-03-10 22:55:18 +0000304 res->ob_type->tp_name);
305 Py_DECREF(res);
306 return NULL;
307 }
308 return res;
Guido van Rossumc6004111993-11-05 10:22:19 +0000309}
310
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000311static PyObject *
Fred Drake100814d2000-07-09 15:48:49 +0000312do_cmp(PyObject *v, PyObject *w)
Guido van Rossum20566841995-01-12 11:26:10 +0000313{
Guido van Rossumc8b6df91997-05-23 00:06:51 +0000314 long c;
Guido van Rossum20566841995-01-12 11:26:10 +0000315 /* __rcmp__ actually won't be called unless __cmp__ isn't defined,
316 because the check in cmpobject() reverses the objects first.
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000317 This is intentional -- it makes no sense to define cmp(x,y)
318 different than -cmp(y,x). */
319 if (PyInstance_Check(v) || PyInstance_Check(w))
320 return PyInstance_DoBinOp(v, w, "__cmp__", "__rcmp__", do_cmp);
Guido van Rossumc8b6df91997-05-23 00:06:51 +0000321 c = PyObject_Compare(v, w);
322 if (c && PyErr_Occurred())
323 return NULL;
324 return PyInt_FromLong(c);
Guido van Rossum20566841995-01-12 11:26:10 +0000325}
326
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000327PyObject *_PyCompareState_Key;
328
Thomas Wouters7e474022000-07-16 12:04:32 +0000329/* _PyCompareState_nesting is incremented before calling compare (for
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000330 some types) and decremented on exit. If the count exceeds the
331 nesting limit, enable code to detect circular data structures.
332*/
Jack Jansend49cbe12000-08-22 21:52:51 +0000333#ifdef macintosh
334#define NESTING_LIMIT 60
335#else
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000336#define NESTING_LIMIT 500
Jack Jansend49cbe12000-08-22 21:52:51 +0000337#endif
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000338int _PyCompareState_nesting = 0;
339
340static PyObject*
Fred Drake100814d2000-07-09 15:48:49 +0000341get_inprogress_dict(void)
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000342{
343 PyObject *tstate_dict, *inprogress;
344
345 tstate_dict = PyThreadState_GetDict();
346 if (tstate_dict == NULL) {
347 PyErr_BadInternalCall();
348 return NULL;
349 }
350 inprogress = PyDict_GetItem(tstate_dict, _PyCompareState_Key);
351 if (inprogress == NULL) {
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000352 inprogress = PyDict_New();
353 if (inprogress == NULL)
354 return NULL;
355 if (PyDict_SetItem(tstate_dict, _PyCompareState_Key,
356 inprogress) == -1) {
357 Py_DECREF(inprogress);
358 return NULL;
359 }
Jeremy Hyltona251ea02000-06-09 16:20:39 +0000360 Py_DECREF(inprogress);
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000361 }
362 return inprogress;
363}
364
365static PyObject *
Fred Drake100814d2000-07-09 15:48:49 +0000366make_pair(PyObject *v, PyObject *w)
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000367{
368 PyObject *pair;
Barry Warsaw9d23a4e2000-08-18 05:01:19 +0000369 Py_uintptr_t iv = (Py_uintptr_t)v;
370 Py_uintptr_t iw = (Py_uintptr_t)w;
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000371
372 pair = PyTuple_New(2);
373 if (pair == NULL) {
374 return NULL;
375 }
Barry Warsaw9d23a4e2000-08-18 05:01:19 +0000376 if (iv <= iw) {
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000377 PyTuple_SET_ITEM(pair, 0, PyLong_FromVoidPtr((void *)v));
378 PyTuple_SET_ITEM(pair, 1, PyLong_FromVoidPtr((void *)w));
379 } else {
380 PyTuple_SET_ITEM(pair, 0, PyLong_FromVoidPtr((void *)w));
381 PyTuple_SET_ITEM(pair, 1, PyLong_FromVoidPtr((void *)v));
382 }
383 return pair;
384}
385
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000386int
Fred Drake100814d2000-07-09 15:48:49 +0000387PyObject_Compare(PyObject *v, PyObject *w)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000388{
Guido van Rossumcd5a5f61998-06-09 18:58:44 +0000389 PyTypeObject *vtp, *wtp;
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000390 int result;
391
Jack Jansend49cbe12000-08-22 21:52:51 +0000392#if defined(USE_STACKCHECK)
Guido van Rossum9c0a99e2000-08-30 15:53:50 +0000393 if (PyOS_CheckStack()) {
Jack Jansend49cbe12000-08-22 21:52:51 +0000394 PyErr_SetString(PyExc_MemoryError, "Stack overflow");
395 return -1;
396 }
397#endif
Guido van Rossumc8b6df91997-05-23 00:06:51 +0000398 if (v == NULL || w == NULL) {
399 PyErr_BadInternalCall();
400 return -1;
401 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000402 if (v == w)
403 return 0;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000404 if (PyInstance_Check(v) || PyInstance_Check(w)) {
405 PyObject *res;
Guido van Rossum20566841995-01-12 11:26:10 +0000406 int c;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000407 if (!PyInstance_Check(v))
408 return -PyObject_Compare(w, v);
Vladimir Marangozov1d3e2392000-08-11 00:14:26 +0000409 _PyCompareState_nesting++;
410 if (_PyCompareState_nesting > NESTING_LIMIT) {
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000411 PyObject *inprogress, *pair;
412
413 inprogress = get_inprogress_dict();
414 if (inprogress == NULL) {
Vladimir Marangozov1d3e2392000-08-11 00:14:26 +0000415 _PyCompareState_nesting--;
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000416 return -1;
417 }
418 pair = make_pair(v, w);
419 if (PyDict_GetItem(inprogress, pair)) {
420 /* already comparing these objects. assume
421 they're equal until shown otherwise */
422 Py_DECREF(pair);
Vladimir Marangozov1d3e2392000-08-11 00:14:26 +0000423 _PyCompareState_nesting--;
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000424 return 0;
425 }
426 if (PyDict_SetItem(inprogress, pair, pair) == -1) {
Vladimir Marangozov1d3e2392000-08-11 00:14:26 +0000427 _PyCompareState_nesting--;
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000428 return -1;
429 }
430 res = do_cmp(v, w);
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000431 /* XXX DelItem shouldn't fail */
432 PyDict_DelItem(inprogress, pair);
433 Py_DECREF(pair);
434 } else {
435 res = do_cmp(v, w);
436 }
Vladimir Marangozov1d3e2392000-08-11 00:14:26 +0000437 _PyCompareState_nesting--;
Guido van Rossumc8b6df91997-05-23 00:06:51 +0000438 if (res == NULL)
439 return -1;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000440 if (!PyInt_Check(res)) {
441 Py_DECREF(res);
Guido van Rossumc8b6df91997-05-23 00:06:51 +0000442 PyErr_SetString(PyExc_TypeError,
443 "comparison did not return an int");
444 return -1;
Guido van Rossum20566841995-01-12 11:26:10 +0000445 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000446 c = PyInt_AsLong(res);
447 Py_DECREF(res);
Guido van Rossum20566841995-01-12 11:26:10 +0000448 return (c < 0) ? -1 : (c > 0) ? 1 : 0;
449 }
Guido van Rossumcd5a5f61998-06-09 18:58:44 +0000450 if ((vtp = v->ob_type) != (wtp = w->ob_type)) {
451 char *vname = vtp->tp_name;
452 char *wname = wtp->tp_name;
Guido van Rossumb4db1941998-07-21 21:56:41 +0000453 if (vtp->tp_as_number != NULL && wtp->tp_as_number != NULL) {
Guido van Rossum242c6421997-11-19 16:03:17 +0000454 int err;
455 err = PyNumber_CoerceEx(&v, &w);
456 if (err < 0)
Guido van Rossumc8b6df91997-05-23 00:06:51 +0000457 return -1;
Guido van Rossum242c6421997-11-19 16:03:17 +0000458 else if (err == 0) {
Guido van Rossumb4db1941998-07-21 21:56:41 +0000459 int cmp;
460 vtp = v->ob_type;
461 if (vtp->tp_compare == NULL)
462 cmp = (v < w) ? -1 : 1;
463 else
464 cmp = (*vtp->tp_compare)(v, w);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000465 Py_DECREF(v);
466 Py_DECREF(w);
Guido van Rossum9fb03681991-07-01 18:48:04 +0000467 return cmp;
468 }
469 }
Guido van Rossumb244f692000-04-10 13:42:33 +0000470 else if (PyUnicode_Check(v) || PyUnicode_Check(w)) {
471 int result = PyUnicode_Compare(v, w);
472 if (result == -1 && PyErr_Occurred() &&
473 PyErr_ExceptionMatches(PyExc_TypeError))
474 /* TypeErrors are ignored: if Unicode coercion
475 fails due to one of the arguments not
476 having the right type, we continue as
477 defined by the coercion protocol (see
478 above). Luckily, decoding errors are
479 reported as ValueErrors and are not masked
480 by this technique. */
481 PyErr_Clear();
482 else
483 return result;
484 }
Guido van Rossumcd5a5f61998-06-09 18:58:44 +0000485 else if (vtp->tp_as_number != NULL)
486 vname = "";
487 else if (wtp->tp_as_number != NULL)
488 wname = "";
489 /* Numerical types compare smaller than all other types */
490 return strcmp(vname, wname);
Guido van Rossum9fb03681991-07-01 18:48:04 +0000491 }
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000492 if (vtp->tp_compare == NULL) {
Barry Warsaw9d23a4e2000-08-18 05:01:19 +0000493 Py_uintptr_t iv = (Py_uintptr_t)v;
494 Py_uintptr_t iw = (Py_uintptr_t)w;
495 return (iv < iw) ? -1 : 1;
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000496 }
Vladimir Marangozov1d3e2392000-08-11 00:14:26 +0000497 _PyCompareState_nesting++;
498 if (_PyCompareState_nesting > NESTING_LIMIT
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000499 && (vtp->tp_as_mapping
500 || (vtp->tp_as_sequence && !PyString_Check(v)))) {
501 PyObject *inprogress, *pair;
502
503 inprogress = get_inprogress_dict();
504 if (inprogress == NULL) {
Vladimir Marangozov1d3e2392000-08-11 00:14:26 +0000505 _PyCompareState_nesting--;
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000506 return -1;
507 }
508 pair = make_pair(v, w);
509 if (PyDict_GetItem(inprogress, pair)) {
510 /* already comparing these objects. assume
511 they're equal until shown otherwise */
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000512 Py_DECREF(pair);
Vladimir Marangozov1d3e2392000-08-11 00:14:26 +0000513 _PyCompareState_nesting--;
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000514 return 0;
515 }
516 if (PyDict_SetItem(inprogress, pair, pair) == -1) {
Vladimir Marangozov1d3e2392000-08-11 00:14:26 +0000517 _PyCompareState_nesting--;
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000518 return -1;
519 }
520 result = (*vtp->tp_compare)(v, w);
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000521 PyDict_DelItem(inprogress, pair); /* XXX shouldn't fail */
522 Py_DECREF(pair);
523 } else {
524 result = (*vtp->tp_compare)(v, w);
525 }
Vladimir Marangozov1d3e2392000-08-11 00:14:26 +0000526 _PyCompareState_nesting--;
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000527 return result;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000528}
529
Fred Drake13634cf2000-06-29 19:17:04 +0000530
531/* Set of hash utility functions to help maintaining the invariant that
532 iff a==b then hash(a)==hash(b)
533
534 All the utility functions (_Py_Hash*()) return "-1" to signify an error.
535*/
536
537long
Fred Drake100814d2000-07-09 15:48:49 +0000538_Py_HashDouble(double v)
Fred Drake13634cf2000-06-29 19:17:04 +0000539{
Tim Peters39dce292000-08-15 03:34:48 +0000540 double intpart, fractpart;
541 int expo;
542 long hipart;
543 long x; /* the final hash value */
544 /* This is designed so that Python numbers of different types
545 * that compare equal hash to the same value; otherwise comparisons
546 * of mapping keys will turn out weird.
547 */
548
549#ifdef MPW /* MPW C modf expects pointer to extended as second argument */
550{
551 extended e;
552 fractpart = modf(v, &e);
553 intpart = e;
554}
555#else
556 fractpart = modf(v, &intpart);
557#endif
558 if (fractpart == 0.0) {
559 /* This must return the same hash as an equal int or long. */
560 if (intpart > LONG_MAX || -intpart > LONG_MAX) {
561 /* Convert to long and use its hash. */
562 PyObject *plong; /* converted to Python long */
563 if (Py_IS_INFINITY(intpart))
564 /* can't convert to long int -- arbitrary */
565 v = v < 0 ? -271828.0 : 314159.0;
566 plong = PyLong_FromDouble(v);
567 if (plong == NULL)
568 return -1;
569 x = PyObject_Hash(plong);
570 Py_DECREF(plong);
571 return x;
572 }
573 /* Fits in a C long == a Python int, so is its own hash. */
574 x = (long)intpart;
575 if (x == -1)
576 x = -2;
577 return x;
578 }
579 /* The fractional part is non-zero, so we don't have to worry about
580 * making this match the hash of some other type.
581 * Use frexp to get at the bits in the double.
Fred Drake13634cf2000-06-29 19:17:04 +0000582 * Since the VAX D double format has 56 mantissa bits, which is the
583 * most of any double format in use, each of these parts may have as
584 * many as (but no more than) 56 significant bits.
Tim Peters39dce292000-08-15 03:34:48 +0000585 * So, assuming sizeof(long) >= 4, each part can be broken into two
586 * longs; frexp and multiplication are used to do that.
587 * Also, since the Cray double format has 15 exponent bits, which is
588 * the most of any double format in use, shifting the exponent field
589 * left by 15 won't overflow a long (again assuming sizeof(long) >= 4).
Fred Drake13634cf2000-06-29 19:17:04 +0000590 */
Tim Peters39dce292000-08-15 03:34:48 +0000591 v = frexp(v, &expo);
592 v *= 2147483648.0; /* 2**31 */
593 hipart = (long)v; /* take the top 32 bits */
594 v = (v - (double)hipart) * 2147483648.0; /* get the next 32 bits */
595 x = hipart + (long)v + (expo << 15);
596 if (x == -1)
597 x = -2;
598 return x;
Fred Drake13634cf2000-06-29 19:17:04 +0000599}
600
601long
Fred Drake100814d2000-07-09 15:48:49 +0000602_Py_HashPointer(void *p)
Fred Drake13634cf2000-06-29 19:17:04 +0000603{
604#if SIZEOF_LONG >= SIZEOF_VOID_P
605 return (long)p;
606#else
607 /* convert to a Python long and hash that */
608 PyObject* longobj;
609 long x;
610
611 if ((longobj = PyLong_FromVoidPtr(p)) == NULL) {
612 x = -1;
613 goto finally;
614 }
615 x = PyObject_Hash(longobj);
616
617finally:
618 Py_XDECREF(longobj);
619 return x;
620#endif
621}
622
623
Guido van Rossum9bfef441993-03-29 10:43:31 +0000624long
Fred Drake100814d2000-07-09 15:48:49 +0000625PyObject_Hash(PyObject *v)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000626{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000627 PyTypeObject *tp = v->ob_type;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000628 if (tp->tp_hash != NULL)
629 return (*tp->tp_hash)(v);
Fred Drake13634cf2000-06-29 19:17:04 +0000630 if (tp->tp_compare == NULL) {
631 return _Py_HashPointer(v); /* Use address as hash value */
632 }
Guido van Rossum9bfef441993-03-29 10:43:31 +0000633 /* If there's a cmp but no hash defined, the object can't be hashed */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000634 PyErr_SetString(PyExc_TypeError, "unhashable type");
Guido van Rossum9bfef441993-03-29 10:43:31 +0000635 return -1;
636}
637
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000638PyObject *
Fred Drake100814d2000-07-09 15:48:49 +0000639PyObject_GetAttrString(PyObject *v, char *name)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000640{
Guido van Rossumd8eb1b31996-08-09 20:52:03 +0000641 if (v->ob_type->tp_getattro != NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000642 PyObject *w, *res;
Guido van Rossumc6d06701997-01-18 07:57:16 +0000643 w = PyString_InternFromString(name);
Guido van Rossumd8eb1b31996-08-09 20:52:03 +0000644 if (w == NULL)
645 return NULL;
646 res = (*v->ob_type->tp_getattro)(v, w);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000647 Py_XDECREF(w);
Guido van Rossumd8eb1b31996-08-09 20:52:03 +0000648 return res;
649 }
650
Guido van Rossum3f5da241990-12-20 15:06:42 +0000651 if (v->ob_type->tp_getattr == NULL) {
Guido van Rossumdb935161998-01-19 22:16:36 +0000652 PyErr_Format(PyExc_AttributeError,
653 "'%.50s' object has no attribute '%.400s'",
654 v->ob_type->tp_name,
655 name);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000656 return NULL;
657 }
658 else {
659 return (*v->ob_type->tp_getattr)(v, name);
660 }
661}
662
663int
Fred Drake100814d2000-07-09 15:48:49 +0000664PyObject_HasAttrString(PyObject *v, char *name)
Guido van Rossumed18fdc1993-07-11 19:55:34 +0000665{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000666 PyObject *res = PyObject_GetAttrString(v, name);
Guido van Rossumed18fdc1993-07-11 19:55:34 +0000667 if (res != NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000668 Py_DECREF(res);
Guido van Rossumed18fdc1993-07-11 19:55:34 +0000669 return 1;
670 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000671 PyErr_Clear();
Guido van Rossumed18fdc1993-07-11 19:55:34 +0000672 return 0;
673}
674
675int
Fred Drake100814d2000-07-09 15:48:49 +0000676PyObject_SetAttrString(PyObject *v, char *name, PyObject *w)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000677{
Guido van Rossumd8eb1b31996-08-09 20:52:03 +0000678 if (v->ob_type->tp_setattro != NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000679 PyObject *s;
Guido van Rossumd8eb1b31996-08-09 20:52:03 +0000680 int res;
Guido van Rossumc6d06701997-01-18 07:57:16 +0000681 s = PyString_InternFromString(name);
Guido van Rossumd8eb1b31996-08-09 20:52:03 +0000682 if (s == NULL)
Guido van Rossumb7fc3041996-09-11 22:51:25 +0000683 return -1;
Guido van Rossumd8eb1b31996-08-09 20:52:03 +0000684 res = (*v->ob_type->tp_setattro)(v, s, w);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000685 Py_XDECREF(s);
Guido van Rossumd8eb1b31996-08-09 20:52:03 +0000686 return res;
687 }
688
Guido van Rossum3f5da241990-12-20 15:06:42 +0000689 if (v->ob_type->tp_setattr == NULL) {
690 if (v->ob_type->tp_getattr == NULL)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000691 PyErr_SetString(PyExc_TypeError,
Guido van Rossum3ea74121991-12-24 13:28:03 +0000692 "attribute-less object (assign or del)");
Guido van Rossum3f5da241990-12-20 15:06:42 +0000693 else
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000694 PyErr_SetString(PyExc_TypeError,
Guido van Rossum3ea74121991-12-24 13:28:03 +0000695 "object has read-only attributes");
Guido van Rossum73531a31990-12-20 23:12:40 +0000696 return -1;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000697 }
698 else {
699 return (*v->ob_type->tp_setattr)(v, name, w);
700 }
701}
702
Marc-André Lemburge44e5072000-09-18 16:20:57 +0000703/* Internal API needed by PyObject_GetAttr(): */
704extern
705PyObject *_PyUnicode_AsDefaultEncodedString(PyObject *unicode,
706 const char *errors);
707
Guido van Rossum98ff96a1997-05-20 18:34:44 +0000708PyObject *
Fred Drake100814d2000-07-09 15:48:49 +0000709PyObject_GetAttr(PyObject *v, PyObject *name)
Guido van Rossum98ff96a1997-05-20 18:34:44 +0000710{
Marc-André Lemburge44e5072000-09-18 16:20:57 +0000711 /* The Unicode to string conversion is done here because the
712 existing tp_getattro slots expect a string object as name
713 and we wouldn't want to break those. */
714 if (PyUnicode_Check(name)) {
715 name = _PyUnicode_AsDefaultEncodedString(name, NULL);
716 if (name == NULL)
717 return NULL;
718 }
Jeremy Hylton99a8f902000-06-23 14:36:32 +0000719
720 if (!PyString_Check(name)) {
721 PyErr_SetString(PyExc_TypeError,
722 "attribute name must be string");
723 return NULL;
724 }
Marc-André Lemburge44e5072000-09-18 16:20:57 +0000725 if (v->ob_type->tp_getattro != NULL)
726 return (*v->ob_type->tp_getattro)(v, name);
727 else
Jeremy Hylton99a8f902000-06-23 14:36:32 +0000728 return PyObject_GetAttrString(v, PyString_AS_STRING(name));
Guido van Rossum98ff96a1997-05-20 18:34:44 +0000729}
730
731int
Fred Drake100814d2000-07-09 15:48:49 +0000732PyObject_HasAttr(PyObject *v, PyObject *name)
Guido van Rossum98ff96a1997-05-20 18:34:44 +0000733{
734 PyObject *res = PyObject_GetAttr(v, name);
735 if (res != NULL) {
736 Py_DECREF(res);
737 return 1;
738 }
739 PyErr_Clear();
740 return 0;
741}
742
743int
Fred Drake100814d2000-07-09 15:48:49 +0000744PyObject_SetAttr(PyObject *v, PyObject *name, PyObject *value)
Guido van Rossum98ff96a1997-05-20 18:34:44 +0000745{
746 int err;
Marc-André Lemburge44e5072000-09-18 16:20:57 +0000747
748 /* The Unicode to string conversion is done here because the
749 existing tp_setattro slots expect a string object as name
750 and we wouldn't want to break those. */
751 if (PyUnicode_Check(name)) {
752 name = PyUnicode_AsEncodedString(name, NULL, NULL);
753 if (name == NULL)
754 return -1;
Jeremy Hylton99a8f902000-06-23 14:36:32 +0000755 }
Marc-André Lemburge44e5072000-09-18 16:20:57 +0000756 else
757 Py_INCREF(name);
758
759 if (!PyString_Check(name)){
Jeremy Hylton99a8f902000-06-23 14:36:32 +0000760 PyErr_SetString(PyExc_TypeError,
761 "attribute name must be string");
762 err = -1;
763 }
Marc-André Lemburge44e5072000-09-18 16:20:57 +0000764 else {
765 PyString_InternInPlace(&name);
766 if (v->ob_type->tp_setattro != NULL)
767 err = (*v->ob_type->tp_setattro)(v, name, value);
768 else
769 err = PyObject_SetAttrString(v,
770 PyString_AS_STRING(name), value);
771 }
772
Guido van Rossum98ff96a1997-05-20 18:34:44 +0000773 Py_DECREF(name);
774 return err;
775}
776
Guido van Rossum6ac258d1993-05-12 08:24:20 +0000777/* Test a value used as condition, e.g., in a for or if statement.
778 Return -1 if an error occurred */
779
780int
Fred Drake100814d2000-07-09 15:48:49 +0000781PyObject_IsTrue(PyObject *v)
Guido van Rossum6ac258d1993-05-12 08:24:20 +0000782{
783 int res;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000784 if (v == Py_None)
Guido van Rossum6ac258d1993-05-12 08:24:20 +0000785 res = 0;
Guido van Rossum1c4f4581998-05-22 00:53:24 +0000786 else if (v->ob_type->tp_as_number != NULL &&
787 v->ob_type->tp_as_number->nb_nonzero != NULL)
Guido van Rossum6ac258d1993-05-12 08:24:20 +0000788 res = (*v->ob_type->tp_as_number->nb_nonzero)(v);
Guido van Rossum1c4f4581998-05-22 00:53:24 +0000789 else if (v->ob_type->tp_as_mapping != NULL &&
790 v->ob_type->tp_as_mapping->mp_length != NULL)
Guido van Rossum6ac258d1993-05-12 08:24:20 +0000791 res = (*v->ob_type->tp_as_mapping->mp_length)(v);
Guido van Rossum1c4f4581998-05-22 00:53:24 +0000792 else if (v->ob_type->tp_as_sequence != NULL &&
793 v->ob_type->tp_as_sequence->sq_length != NULL)
Guido van Rossum6ac258d1993-05-12 08:24:20 +0000794 res = (*v->ob_type->tp_as_sequence->sq_length)(v);
795 else
796 res = 1;
797 if (res > 0)
798 res = 1;
799 return res;
800}
801
Guido van Rossumc3d3f961998-04-09 17:53:59 +0000802/* equivalent of 'not v'
803 Return -1 if an error occurred */
804
805int
Fred Drake100814d2000-07-09 15:48:49 +0000806PyObject_Not(PyObject *v)
Guido van Rossumc3d3f961998-04-09 17:53:59 +0000807{
808 int res;
809 res = PyObject_IsTrue(v);
810 if (res < 0)
811 return res;
812 return res == 0;
813}
814
Guido van Rossum5524a591995-01-10 15:26:20 +0000815/* Coerce two numeric types to the "larger" one.
816 Increment the reference count on each argument.
817 Return -1 and raise an exception if no coercion is possible
818 (and then no reference count is incremented).
819*/
820
821int
Fred Drake100814d2000-07-09 15:48:49 +0000822PyNumber_CoerceEx(PyObject **pv, PyObject **pw)
Guido van Rossum5524a591995-01-10 15:26:20 +0000823{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000824 register PyObject *v = *pv;
825 register PyObject *w = *pw;
Guido van Rossum5524a591995-01-10 15:26:20 +0000826 int res;
827
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000828 if (v->ob_type == w->ob_type && !PyInstance_Check(v)) {
829 Py_INCREF(v);
830 Py_INCREF(w);
Guido van Rossum5524a591995-01-10 15:26:20 +0000831 return 0;
832 }
833 if (v->ob_type->tp_as_number && v->ob_type->tp_as_number->nb_coerce) {
834 res = (*v->ob_type->tp_as_number->nb_coerce)(pv, pw);
835 if (res <= 0)
836 return res;
837 }
838 if (w->ob_type->tp_as_number && w->ob_type->tp_as_number->nb_coerce) {
839 res = (*w->ob_type->tp_as_number->nb_coerce)(pw, pv);
840 if (res <= 0)
841 return res;
842 }
Guido van Rossum242c6421997-11-19 16:03:17 +0000843 return 1;
844}
845
846int
Fred Drake100814d2000-07-09 15:48:49 +0000847PyNumber_Coerce(PyObject **pv, PyObject **pw)
Guido van Rossum242c6421997-11-19 16:03:17 +0000848{
849 int err = PyNumber_CoerceEx(pv, pw);
850 if (err <= 0)
851 return err;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000852 PyErr_SetString(PyExc_TypeError, "number coercion failed");
Guido van Rossum5524a591995-01-10 15:26:20 +0000853 return -1;
854}
855
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000856
Guido van Rossum49b11fe1995-01-26 00:38:22 +0000857/* Test whether an object can be called */
858
859int
Fred Drake100814d2000-07-09 15:48:49 +0000860PyCallable_Check(PyObject *x)
Guido van Rossum49b11fe1995-01-26 00:38:22 +0000861{
862 if (x == NULL)
863 return 0;
864 if (x->ob_type->tp_call != NULL ||
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000865 PyFunction_Check(x) ||
866 PyMethod_Check(x) ||
867 PyCFunction_Check(x) ||
868 PyClass_Check(x))
Guido van Rossum49b11fe1995-01-26 00:38:22 +0000869 return 1;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000870 if (PyInstance_Check(x)) {
871 PyObject *call = PyObject_GetAttrString(x, "__call__");
Guido van Rossum49b11fe1995-01-26 00:38:22 +0000872 if (call == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000873 PyErr_Clear();
Guido van Rossum49b11fe1995-01-26 00:38:22 +0000874 return 0;
875 }
876 /* Could test recursively but don't, for fear of endless
877 recursion if some joker sets self.__call__ = self */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000878 Py_DECREF(call);
Guido van Rossum49b11fe1995-01-26 00:38:22 +0000879 return 1;
880 }
881 return 0;
882}
883
884
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000885/*
886NoObject is usable as a non-NULL undefined value, used by the macro None.
887There is (and should be!) no way to create other objects of this type,
Guido van Rossum3f5da241990-12-20 15:06:42 +0000888so there is exactly one (which is indestructible, by the way).
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000889*/
890
Guido van Rossum0c182a11992-03-27 17:26:13 +0000891/* ARGSUSED */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000892static PyObject *
Fred Drake100814d2000-07-09 15:48:49 +0000893none_repr(PyObject *op)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000894{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000895 return PyString_FromString("None");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000896}
897
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000898static PyTypeObject PyNothing_Type = {
899 PyObject_HEAD_INIT(&PyType_Type)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000900 0,
Guido van Rossum3f5da241990-12-20 15:06:42 +0000901 "None",
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000902 0,
903 0,
904 0, /*tp_dealloc*/ /*never called*/
Guido van Rossum7066dd71992-09-17 17:54:56 +0000905 0, /*tp_print*/
Guido van Rossum3f5da241990-12-20 15:06:42 +0000906 0, /*tp_getattr*/
907 0, /*tp_setattr*/
908 0, /*tp_compare*/
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000909 (reprfunc)none_repr, /*tp_repr*/
Guido van Rossum3f5da241990-12-20 15:06:42 +0000910 0, /*tp_as_number*/
911 0, /*tp_as_sequence*/
912 0, /*tp_as_mapping*/
Guido van Rossum9bfef441993-03-29 10:43:31 +0000913 0, /*tp_hash */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000914};
915
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000916PyObject _Py_NoneStruct = {
917 PyObject_HEAD_INIT(&PyNothing_Type)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000918};
919
920
Guido van Rossum84a90321996-05-22 16:34:47 +0000921#ifdef Py_TRACE_REFS
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000922
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000923static PyObject refchain = {&refchain, &refchain};
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000924
Guido van Rossumaacdc9d1996-08-12 21:32:12 +0000925void
Fred Drake100814d2000-07-09 15:48:49 +0000926_Py_ResetReferences(void)
Guido van Rossume09fb551997-08-05 02:04:34 +0000927{
928 refchain._ob_prev = refchain._ob_next = &refchain;
929 _Py_RefTotal = 0;
930}
931
932void
Fred Drake100814d2000-07-09 15:48:49 +0000933_Py_NewReference(PyObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000934{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000935 _Py_RefTotal++;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000936 op->ob_refcnt = 1;
937 op->_ob_next = refchain._ob_next;
938 op->_ob_prev = &refchain;
939 refchain._ob_next->_ob_prev = op;
940 refchain._ob_next = op;
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +0000941#ifdef COUNT_ALLOCS
942 inc_count(op->ob_type);
943#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000944}
945
Guido van Rossumaacdc9d1996-08-12 21:32:12 +0000946void
Fred Drake100814d2000-07-09 15:48:49 +0000947_Py_ForgetReference(register PyObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000948{
Guido van Rossumbffd6832000-01-20 22:32:56 +0000949#ifdef SLOW_UNREF_CHECK
Guido van Rossum4c08d552000-03-10 22:55:18 +0000950 register PyObject *p;
Guido van Rossumbffd6832000-01-20 22:32:56 +0000951#endif
Guido van Rossumd7047b31995-01-02 19:07:15 +0000952 if (op->ob_refcnt < 0)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000953 Py_FatalError("UNREF negative refcnt");
Guido van Rossum2e8f6141992-09-03 20:32:55 +0000954 if (op == &refchain ||
Guido van Rossumd7047b31995-01-02 19:07:15 +0000955 op->_ob_prev->_ob_next != op || op->_ob_next->_ob_prev != op)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000956 Py_FatalError("UNREF invalid object");
Guido van Rossum2e8f6141992-09-03 20:32:55 +0000957#ifdef SLOW_UNREF_CHECK
Guido van Rossum3f5da241990-12-20 15:06:42 +0000958 for (p = refchain._ob_next; p != &refchain; p = p->_ob_next) {
959 if (p == op)
960 break;
961 }
Guido van Rossumd7047b31995-01-02 19:07:15 +0000962 if (p == &refchain) /* Not found */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000963 Py_FatalError("UNREF unknown object");
Guido van Rossum2e8f6141992-09-03 20:32:55 +0000964#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000965 op->_ob_next->_ob_prev = op->_ob_prev;
966 op->_ob_prev->_ob_next = op->_ob_next;
Guido van Rossum2e8f6141992-09-03 20:32:55 +0000967 op->_ob_next = op->_ob_prev = NULL;
Guido van Rossumd8953cb1995-04-06 14:46:26 +0000968#ifdef COUNT_ALLOCS
969 op->ob_type->tp_free++;
970#endif
Guido van Rossum3f5da241990-12-20 15:06:42 +0000971}
972
Guido van Rossumaacdc9d1996-08-12 21:32:12 +0000973void
Fred Drake100814d2000-07-09 15:48:49 +0000974_Py_Dealloc(PyObject *op)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000975{
Guido van Rossum9776adf1994-09-07 14:36:45 +0000976 destructor dealloc = op->ob_type->tp_dealloc;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000977 _Py_ForgetReference(op);
Guido van Rossum9776adf1994-09-07 14:36:45 +0000978 (*dealloc)(op);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000979}
980
Guido van Rossumaacdc9d1996-08-12 21:32:12 +0000981void
Fred Drake100814d2000-07-09 15:48:49 +0000982_Py_PrintReferences(FILE *fp)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000983{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000984 PyObject *op;
Guido van Rossume09fb551997-08-05 02:04:34 +0000985 fprintf(fp, "Remaining objects:\n");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000986 for (op = refchain._ob_next; op != &refchain; op = op->_ob_next) {
987 fprintf(fp, "[%d] ", op->ob_refcnt);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000988 if (PyObject_Print(op, fp, 0) != 0)
989 PyErr_Clear();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000990 putc('\n', fp);
991 }
992}
993
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000994PyObject *
Fred Drake100814d2000-07-09 15:48:49 +0000995_Py_GetObjects(PyObject *self, PyObject *args)
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +0000996{
997 int i, n;
998 PyObject *t = NULL;
999 PyObject *res, *op;
1000
1001 if (!PyArg_ParseTuple(args, "i|O", &n, &t))
1002 return NULL;
1003 op = refchain._ob_next;
1004 res = PyList_New(0);
1005 if (res == NULL)
1006 return NULL;
1007 for (i = 0; (n == 0 || i < n) && op != &refchain; i++) {
1008 while (op == self || op == args || op == res || op == t ||
1009 t != NULL && op->ob_type != (PyTypeObject *) t) {
1010 op = op->_ob_next;
1011 if (op == &refchain)
1012 return res;
1013 }
1014 if (PyList_Append(res, op) < 0) {
1015 Py_DECREF(res);
1016 return NULL;
1017 }
1018 op = op->_ob_next;
1019 }
1020 return res;
1021}
1022
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001023#endif
Guido van Rossum97ead3f1996-01-12 01:24:09 +00001024
1025
1026/* Hack to force loading of cobject.o */
Guido van Rossumda9c2711996-12-05 21:58:58 +00001027PyTypeObject *_Py_cobject_hack = &PyCObject_Type;
Guido van Rossum84a90321996-05-22 16:34:47 +00001028
1029
1030/* Hack to force loading of abstract.o */
Jeremy Hylton6253f832000-07-12 12:56:19 +00001031int (*_Py_abstract_hack)(PyObject *) = &PyObject_Size;
Guido van Rossume09fb551997-08-05 02:04:34 +00001032
1033
Andrew M. Kuchling1582a3a2000-08-16 12:27:23 +00001034/* Python's malloc wrappers (see pymem.h) */
Guido van Rossume09fb551997-08-05 02:04:34 +00001035
Thomas Wouters334fb892000-07-25 12:56:38 +00001036void *
Fred Drake100814d2000-07-09 15:48:49 +00001037PyMem_Malloc(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_MALLOC(nbytes);
Guido van Rossume09fb551997-08-05 02:04:34 +00001044}
1045
Thomas Wouters334fb892000-07-25 12:56:38 +00001046void *
1047PyMem_Realloc(void *p, size_t nbytes)
Guido van Rossume09fb551997-08-05 02:04:34 +00001048{
1049#if _PyMem_EXTRA > 0
1050 if (nbytes == 0)
1051 nbytes = _PyMem_EXTRA;
1052#endif
Guido van Rossumb18618d2000-05-03 23:44:39 +00001053 return PyMem_REALLOC(p, nbytes);
Guido van Rossume09fb551997-08-05 02:04:34 +00001054}
1055
1056void
Thomas Wouters334fb892000-07-25 12:56:38 +00001057PyMem_Free(void *p)
Guido van Rossume09fb551997-08-05 02:04:34 +00001058{
Guido van Rossumb18618d2000-05-03 23:44:39 +00001059 PyMem_FREE(p);
1060}
1061
1062
1063/* Python's object malloc wrappers (see objimpl.h) */
1064
Thomas Wouters334fb892000-07-25 12:56:38 +00001065void *
Fred Drake100814d2000-07-09 15:48:49 +00001066PyObject_Malloc(size_t nbytes)
Guido van Rossumb18618d2000-05-03 23:44:39 +00001067{
1068 return PyObject_MALLOC(nbytes);
1069}
1070
Thomas Wouters334fb892000-07-25 12:56:38 +00001071void *
1072PyObject_Realloc(void *p, size_t nbytes)
Guido van Rossumb18618d2000-05-03 23:44:39 +00001073{
1074 return PyObject_REALLOC(p, nbytes);
1075}
1076
1077void
Thomas Wouters334fb892000-07-25 12:56:38 +00001078PyObject_Free(void *p)
Guido van Rossumb18618d2000-05-03 23:44:39 +00001079{
1080 PyObject_FREE(p);
Guido van Rossume09fb551997-08-05 02:04:34 +00001081}
Guido van Rossum86610361998-04-10 22:32:46 +00001082
1083
1084/* These methods are used to control infinite recursion in repr, str, print,
1085 etc. Container objects that may recursively contain themselves,
1086 e.g. builtin dictionaries and lists, should used Py_ReprEnter() and
1087 Py_ReprLeave() to avoid infinite recursion.
1088
1089 Py_ReprEnter() returns 0 the first time it is called for a particular
1090 object and 1 every time thereafter. It returns -1 if an exception
1091 occurred. Py_ReprLeave() has no return value.
1092
1093 See dictobject.c and listobject.c for examples of use.
1094*/
1095
1096#define KEY "Py_Repr"
1097
1098int
Fred Drake100814d2000-07-09 15:48:49 +00001099Py_ReprEnter(PyObject *obj)
Guido van Rossum86610361998-04-10 22:32:46 +00001100{
1101 PyObject *dict;
1102 PyObject *list;
1103 int i;
1104
1105 dict = PyThreadState_GetDict();
1106 if (dict == NULL)
1107 return -1;
1108 list = PyDict_GetItemString(dict, KEY);
1109 if (list == NULL) {
1110 list = PyList_New(0);
1111 if (list == NULL)
1112 return -1;
1113 if (PyDict_SetItemString(dict, KEY, list) < 0)
1114 return -1;
1115 Py_DECREF(list);
1116 }
1117 i = PyList_GET_SIZE(list);
1118 while (--i >= 0) {
1119 if (PyList_GET_ITEM(list, i) == obj)
1120 return 1;
1121 }
1122 PyList_Append(list, obj);
1123 return 0;
1124}
1125
1126void
Fred Drake100814d2000-07-09 15:48:49 +00001127Py_ReprLeave(PyObject *obj)
Guido van Rossum86610361998-04-10 22:32:46 +00001128{
1129 PyObject *dict;
1130 PyObject *list;
1131 int i;
1132
1133 dict = PyThreadState_GetDict();
Guido van Rossumeb909461998-04-11 15:17:34 +00001134 if (dict == NULL)
1135 return;
Guido van Rossum86610361998-04-10 22:32:46 +00001136 list = PyDict_GetItemString(dict, KEY);
Guido van Rossumeb909461998-04-11 15:17:34 +00001137 if (list == NULL || !PyList_Check(list))
1138 return;
Guido van Rossum86610361998-04-10 22:32:46 +00001139 i = PyList_GET_SIZE(list);
1140 /* Count backwards because we always expect obj to be list[-1] */
1141 while (--i >= 0) {
1142 if (PyList_GET_ITEM(list, i) == obj) {
1143 PyList_SetSlice(list, i, i + 1, NULL);
1144 break;
1145 }
1146 }
1147}
Guido van Rossumd724b232000-03-13 16:01:29 +00001148
1149/*
1150 trashcan
1151 CT 2k0130
1152 non-recursively destroy nested objects
1153
1154 CT 2k0223
1155 everything is now done in a macro.
1156
1157 CT 2k0305
1158 modified to use functions, after Tim Peter's suggestion.
1159
1160 CT 2k0309
1161 modified to restore a possible error.
Guido van Rossum13ff8eb2000-03-25 18:39:19 +00001162
1163 CT 2k0325
1164 added better safe than sorry check for threadstate
Guido van Rossume92e6102000-04-24 15:40:53 +00001165
1166 CT 2k0422
1167 complete rewrite. We now build a chain via ob_type
1168 and save the limited number of types in ob_refcnt.
1169 This is perfect since we don't need any memory.
1170 A patch for free-threading would need just a lock.
Guido van Rossumd724b232000-03-13 16:01:29 +00001171*/
1172
Guido van Rossume92e6102000-04-24 15:40:53 +00001173#define Py_TRASHCAN_TUPLE 1
1174#define Py_TRASHCAN_LIST 2
1175#define Py_TRASHCAN_DICT 3
1176#define Py_TRASHCAN_FRAME 4
1177#define Py_TRASHCAN_TRACEBACK 5
1178/* extend here if other objects want protection */
1179
Guido van Rossumd724b232000-03-13 16:01:29 +00001180int _PyTrash_delete_nesting = 0;
Guido van Rossume92e6102000-04-24 15:40:53 +00001181
Guido van Rossumd724b232000-03-13 16:01:29 +00001182PyObject * _PyTrash_delete_later = NULL;
1183
1184void
Fred Drake100814d2000-07-09 15:48:49 +00001185_PyTrash_deposit_object(PyObject *op)
Guido van Rossumd724b232000-03-13 16:01:29 +00001186{
Guido van Rossume92e6102000-04-24 15:40:53 +00001187 int typecode;
Guido van Rossum13ff8eb2000-03-25 18:39:19 +00001188
Guido van Rossume92e6102000-04-24 15:40:53 +00001189 if (PyTuple_Check(op))
1190 typecode = Py_TRASHCAN_TUPLE;
1191 else if (PyList_Check(op))
1192 typecode = Py_TRASHCAN_LIST;
1193 else if (PyDict_Check(op))
1194 typecode = Py_TRASHCAN_DICT;
1195 else if (PyFrame_Check(op))
1196 typecode = Py_TRASHCAN_FRAME;
1197 else if (PyTraceBack_Check(op))
1198 typecode = Py_TRASHCAN_TRACEBACK;
Moshe Zadkacf703f02000-08-04 15:36:13 +00001199 else /* We have a bug here -- those are the only types in GC */ {
1200 Py_FatalError("Type not supported in GC -- internal bug");
1201 return; /* pacify compiler -- execution never here */
1202 }
Guido van Rossume92e6102000-04-24 15:40:53 +00001203 op->ob_refcnt = typecode;
Guido van Rossumd724b232000-03-13 16:01:29 +00001204
Guido van Rossume92e6102000-04-24 15:40:53 +00001205 op->ob_type = (PyTypeObject*)_PyTrash_delete_later;
1206 _PyTrash_delete_later = op;
Guido van Rossumd724b232000-03-13 16:01:29 +00001207}
1208
1209void
Fred Drake100814d2000-07-09 15:48:49 +00001210_PyTrash_destroy_chain(void)
Guido van Rossumd724b232000-03-13 16:01:29 +00001211{
1212 while (_PyTrash_delete_later) {
1213 PyObject *shredder = _PyTrash_delete_later;
Guido van Rossume92e6102000-04-24 15:40:53 +00001214 _PyTrash_delete_later = (PyObject*) shredder->ob_type;
1215
1216 switch (shredder->ob_refcnt) {
1217 case Py_TRASHCAN_TUPLE:
1218 shredder->ob_type = &PyTuple_Type;
1219 break;
1220 case Py_TRASHCAN_LIST:
1221 shredder->ob_type = &PyList_Type;
1222 break;
1223 case Py_TRASHCAN_DICT:
1224 shredder->ob_type = &PyDict_Type;
1225 break;
1226 case Py_TRASHCAN_FRAME:
1227 shredder->ob_type = &PyFrame_Type;
1228 break;
1229 case Py_TRASHCAN_TRACEBACK:
1230 shredder->ob_type = &PyTraceBack_Type;
1231 break;
1232 }
1233 _Py_NewReference(shredder);
1234
Guido van Rossumd724b232000-03-13 16:01:29 +00001235 ++_PyTrash_delete_nesting;
1236 Py_DECREF(shredder);
1237 --_PyTrash_delete_nesting;
1238 }
1239}