blob: 8a898f8bbf6b978f456eb33b81c720fbf83c7beb [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);
Fred Drake41deb1e2001-02-01 05:27:45 +0000103 if (PyType_SUPPORTS_WEAKREFS(tp)) {
104 PyObject **weaklist = PyObject_GET_WEAKREFS_LISTPTR(op);
105 *weaklist = NULL;
106 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000107 return op;
108}
109
Guido van Rossumb18618d2000-05-03 23:44:39 +0000110PyVarObject *
Fred Drake100814d2000-07-09 15:48:49 +0000111PyObject_InitVar(PyVarObject *op, PyTypeObject *tp, int size)
Guido van Rossumb18618d2000-05-03 23:44:39 +0000112{
113 if (op == NULL) {
114 PyErr_SetString(PyExc_SystemError,
115 "NULL object passed to PyObject_InitVar");
116 return op;
117 }
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000118#ifdef WITH_CYCLE_GC
119 if (PyType_IS_GC(tp))
120 op = (PyVarObject *) PyObject_FROM_GC(op);
121#endif
Guido van Rossumb18618d2000-05-03 23:44:39 +0000122 /* Any changes should be reflected in PyObject_INIT_VAR */
123 op->ob_size = size;
124 op->ob_type = tp;
125 _Py_NewReference((PyObject *)op);
Fred Drake41deb1e2001-02-01 05:27:45 +0000126 if (PyType_SUPPORTS_WEAKREFS(tp)) {
127 PyObject **weaklist = PyObject_GET_WEAKREFS_LISTPTR(op);
128 *weaklist = NULL;
129 }
Guido van Rossumb18618d2000-05-03 23:44:39 +0000130 return op;
131}
132
133PyObject *
Fred Drake100814d2000-07-09 15:48:49 +0000134_PyObject_New(PyTypeObject *tp)
Guido van Rossumb18618d2000-05-03 23:44:39 +0000135{
136 PyObject *op;
137 op = (PyObject *) PyObject_MALLOC(_PyObject_SIZE(tp));
138 if (op == NULL)
139 return PyErr_NoMemory();
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000140#ifdef WITH_CYCLE_GC
141 if (PyType_IS_GC(tp))
142 op = (PyObject *) PyObject_FROM_GC(op);
143#endif
Guido van Rossumb18618d2000-05-03 23:44:39 +0000144 return PyObject_INIT(op, tp);
145}
146
Guido van Rossumd0c87ee1997-05-15 21:31:03 +0000147PyVarObject *
Fred Drake100814d2000-07-09 15:48:49 +0000148_PyObject_NewVar(PyTypeObject *tp, int size)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000149{
Guido van Rossumb18618d2000-05-03 23:44:39 +0000150 PyVarObject *op;
151 op = (PyVarObject *) PyObject_MALLOC(_PyObject_VAR_SIZE(tp, size));
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000152 if (op == NULL)
Guido van Rossumd0c87ee1997-05-15 21:31:03 +0000153 return (PyVarObject *)PyErr_NoMemory();
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000154#ifdef WITH_CYCLE_GC
155 if (PyType_IS_GC(tp))
156 op = (PyVarObject *) PyObject_FROM_GC(op);
157#endif
Guido van Rossumb18618d2000-05-03 23:44:39 +0000158 return PyObject_INIT_VAR(op, tp, size);
159}
160
161void
Fred Drake100814d2000-07-09 15:48:49 +0000162_PyObject_Del(PyObject *op)
Guido van Rossumb18618d2000-05-03 23:44:39 +0000163{
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000164#ifdef WITH_CYCLE_GC
Guido van Rossum4cc6ac72000-07-01 01:00:38 +0000165 if (op && PyType_IS_GC(op->ob_type)) {
166 op = (PyObject *) PyObject_AS_GC(op);
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000167 }
Guido van Rossum4cc6ac72000-07-01 01:00:38 +0000168#endif
169 PyObject_FREE(op);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000170}
171
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000172#ifndef WITH_CYCLE_GC
173/* extension modules might need these */
174void _PyGC_Insert(PyObject *op) { }
175void _PyGC_Remove(PyObject *op) { }
176#endif
177
Guido van Rossum90933611991-06-07 16:10:43 +0000178int
Fred Drake100814d2000-07-09 15:48:49 +0000179PyObject_Print(PyObject *op, FILE *fp, int flags)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000180{
Guido van Rossum278ef591991-07-27 21:40:24 +0000181 int ret = 0;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000182 if (PyErr_CheckSignals())
Guido van Rossum90933611991-06-07 16:10:43 +0000183 return -1;
Guido van Rossum9b00dfa1998-04-28 16:06:54 +0000184#ifdef USE_STACKCHECK
185 if (PyOS_CheckStack()) {
Fred Drake661ea262000-10-24 19:57:45 +0000186 PyErr_SetString(PyExc_MemoryError, "stack overflow");
Guido van Rossum9b00dfa1998-04-28 16:06:54 +0000187 return -1;
188 }
189#endif
Guido van Rossum687ef6e2000-01-12 16:28:58 +0000190 clearerr(fp); /* Clear any previous error condition */
Guido van Rossum90933611991-06-07 16:10:43 +0000191 if (op == NULL) {
192 fprintf(fp, "<nil>");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000193 }
Guido van Rossum90933611991-06-07 16:10:43 +0000194 else {
195 if (op->ob_refcnt <= 0)
Fred Drakea44d3532000-06-30 15:01:00 +0000196 fprintf(fp, "<refcnt %u at %p>",
197 op->ob_refcnt, op);
Guido van Rossum2e8f6141992-09-03 20:32:55 +0000198 else if (op->ob_type->tp_print == NULL) {
199 if (op->ob_type->tp_repr == NULL) {
Fred Drakea44d3532000-06-30 15:01:00 +0000200 fprintf(fp, "<%s object at %p>",
201 op->ob_type->tp_name, op);
Guido van Rossum2e8f6141992-09-03 20:32:55 +0000202 }
203 else {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000204 PyObject *s;
205 if (flags & Py_PRINT_RAW)
206 s = PyObject_Str(op);
Guido van Rossumc6004111993-11-05 10:22:19 +0000207 else
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000208 s = PyObject_Repr(op);
Guido van Rossum2e8f6141992-09-03 20:32:55 +0000209 if (s == NULL)
210 ret = -1;
Guido van Rossum2e8f6141992-09-03 20:32:55 +0000211 else {
Guido van Rossum565798d1998-04-21 22:25:01 +0000212 ret = PyObject_Print(s, fp,
213 Py_PRINT_RAW);
Guido van Rossum2e8f6141992-09-03 20:32:55 +0000214 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000215 Py_XDECREF(s);
Guido van Rossum2e8f6141992-09-03 20:32:55 +0000216 }
217 }
Guido van Rossum90933611991-06-07 16:10:43 +0000218 else
Guido van Rossum278ef591991-07-27 21:40:24 +0000219 ret = (*op->ob_type->tp_print)(op, fp, flags);
Guido van Rossum90933611991-06-07 16:10:43 +0000220 }
Guido van Rossum278ef591991-07-27 21:40:24 +0000221 if (ret == 0) {
222 if (ferror(fp)) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000223 PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossum278ef591991-07-27 21:40:24 +0000224 clearerr(fp);
225 ret = -1;
226 }
227 }
228 return ret;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000229}
230
Barry Warsaw9bf16442001-01-23 16:24:35 +0000231/* For debugging convenience. See Misc/gdbinit for some useful gdb hooks */
Barry Warsawbbd89b62001-01-24 04:18:13 +0000232void _PyObject_Dump(PyObject* op)
Barry Warsaw9bf16442001-01-23 16:24:35 +0000233{
Barry Warsaweefb1072001-02-22 22:39:18 +0000234 if (op == NULL)
235 fprintf(stderr, "NULL\n");
236 else {
237 (void)PyObject_Print(op, stderr, 0);
238 fprintf(stderr, "\nrefcounts: %d\n", op->ob_refcnt);
239 fprintf(stderr, "address : %p\n", op);
240 }
Barry Warsaw9bf16442001-01-23 16:24:35 +0000241}
Barry Warsaw903138f2001-01-23 16:33:18 +0000242
243#ifdef WITH_CYCLE_GC
Barry Warsawbbd89b62001-01-24 04:18:13 +0000244void _PyGC_Dump(PyGC_Head* op)
Barry Warsaw9bf16442001-01-23 16:24:35 +0000245{
Barry Warsawbbd89b62001-01-24 04:18:13 +0000246 _PyObject_Dump(PyObject_FROM_GC(op));
Barry Warsaw9bf16442001-01-23 16:24:35 +0000247}
Barry Warsaw903138f2001-01-23 16:33:18 +0000248#endif /* WITH_CYCLE_GC */
Barry Warsaw9bf16442001-01-23 16:24:35 +0000249
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000250PyObject *
Fred Drake100814d2000-07-09 15:48:49 +0000251PyObject_Repr(PyObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000252{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000253 if (PyErr_CheckSignals())
Guido van Rossum90933611991-06-07 16:10:43 +0000254 return NULL;
Guido van Rossum9b00dfa1998-04-28 16:06:54 +0000255#ifdef USE_STACKCHECK
256 if (PyOS_CheckStack()) {
Fred Drake661ea262000-10-24 19:57:45 +0000257 PyErr_SetString(PyExc_MemoryError, "stack overflow");
Guido van Rossum9b00dfa1998-04-28 16:06:54 +0000258 return NULL;
259 }
260#endif
Guido van Rossum90933611991-06-07 16:10:43 +0000261 if (v == NULL)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000262 return PyString_FromString("<NULL>");
Guido van Rossum90933611991-06-07 16:10:43 +0000263 else if (v->ob_type->tp_repr == NULL) {
264 char buf[120];
Fred Drakea44d3532000-06-30 15:01:00 +0000265 sprintf(buf, "<%.80s object at %p>",
266 v->ob_type->tp_name, v);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000267 return PyString_FromString(buf);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000268 }
Guido van Rossum4c08d552000-03-10 22:55:18 +0000269 else {
270 PyObject *res;
271 res = (*v->ob_type->tp_repr)(v);
272 if (res == NULL)
273 return NULL;
Fredrik Lundhefecc7d2000-07-01 14:31:09 +0000274 if (PyUnicode_Check(res)) {
275 PyObject* str;
Fredrik Lundh2a1e0602000-07-08 17:43:32 +0000276 str = PyUnicode_AsUnicodeEscapeString(res);
Marc-André Lemburg891bc652000-07-03 09:57:53 +0000277 Py_DECREF(res);
278 if (str)
Fredrik Lundhefecc7d2000-07-01 14:31:09 +0000279 res = str;
Marc-André Lemburg891bc652000-07-03 09:57:53 +0000280 else
281 return NULL;
Fredrik Lundhefecc7d2000-07-01 14:31:09 +0000282 }
Guido van Rossum4c08d552000-03-10 22:55:18 +0000283 if (!PyString_Check(res)) {
284 PyErr_Format(PyExc_TypeError,
Guido van Rossum5db862d2000-04-10 12:46:51 +0000285 "__repr__ returned non-string (type %.200s)",
Guido van Rossum4c08d552000-03-10 22:55:18 +0000286 res->ob_type->tp_name);
287 Py_DECREF(res);
288 return NULL;
289 }
290 return res;
291 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000292}
293
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000294PyObject *
Fred Drake100814d2000-07-09 15:48:49 +0000295PyObject_Str(PyObject *v)
Guido van Rossumc6004111993-11-05 10:22:19 +0000296{
Guido van Rossum4c08d552000-03-10 22:55:18 +0000297 PyObject *res;
298
Guido van Rossumc6004111993-11-05 10:22:19 +0000299 if (v == NULL)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000300 return PyString_FromString("<NULL>");
301 else if (PyString_Check(v)) {
302 Py_INCREF(v);
Guido van Rossumc6004111993-11-05 10:22:19 +0000303 return v;
304 }
Guido van Rossum32b582b1995-01-17 16:35:13 +0000305 else if (v->ob_type->tp_str != NULL)
Guido van Rossum4c08d552000-03-10 22:55:18 +0000306 res = (*v->ob_type->tp_str)(v);
Guido van Rossumc6004111993-11-05 10:22:19 +0000307 else {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000308 PyObject *func;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000309 if (!PyInstance_Check(v) ||
310 (func = PyObject_GetAttrString(v, "__str__")) == NULL) {
311 PyErr_Clear();
312 return PyObject_Repr(v);
Guido van Rossumc6004111993-11-05 10:22:19 +0000313 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000314 res = PyEval_CallObject(func, (PyObject *)NULL);
315 Py_DECREF(func);
Guido van Rossumc6004111993-11-05 10:22:19 +0000316 }
Guido van Rossum4c08d552000-03-10 22:55:18 +0000317 if (res == NULL)
318 return NULL;
Marc-André Lemburg891bc652000-07-03 09:57:53 +0000319 if (PyUnicode_Check(res)) {
320 PyObject* str;
321 str = PyUnicode_AsEncodedString(res, NULL, NULL);
322 Py_DECREF(res);
323 if (str)
324 res = str;
325 else
326 return NULL;
327 }
Guido van Rossum4c08d552000-03-10 22:55:18 +0000328 if (!PyString_Check(res)) {
329 PyErr_Format(PyExc_TypeError,
Guido van Rossum5db862d2000-04-10 12:46:51 +0000330 "__str__ returned non-string (type %.200s)",
Guido van Rossum4c08d552000-03-10 22:55:18 +0000331 res->ob_type->tp_name);
332 Py_DECREF(res);
333 return NULL;
334 }
335 return res;
Guido van Rossumc6004111993-11-05 10:22:19 +0000336}
337
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000338PyObject *
339PyObject_Unicode(PyObject *v)
340{
341 PyObject *res;
342
343 if (v == NULL)
344 res = PyString_FromString("<NULL>");
345 else if (PyUnicode_Check(v)) {
346 Py_INCREF(v);
347 return v;
348 }
349 else if (PyString_Check(v))
350 res = v;
351 else if (v->ob_type->tp_str != NULL)
352 res = (*v->ob_type->tp_str)(v);
353 else {
354 PyObject *func;
355 static PyObject *strstr;
356 if (strstr == NULL) {
357 strstr= PyString_InternFromString("__str__");
358 if (strstr == NULL)
359 return NULL;
360 }
361 if (!PyInstance_Check(v) ||
362 (func = PyObject_GetAttr(v, strstr)) == NULL) {
363 PyErr_Clear();
364 res = PyObject_Repr(v);
365 }
366 else {
367 res = PyEval_CallObject(func, (PyObject *)NULL);
368 Py_DECREF(func);
369 }
370 }
371 if (res == NULL)
372 return NULL;
373 if (!PyUnicode_Check(res)) {
374 PyObject* str;
375 str = PyUnicode_FromObject(res);
376 Py_DECREF(res);
377 if (str)
378 res = str;
379 else
380 return NULL;
381 }
382 return res;
383}
384
385
Guido van Rossumd1f06b92001-01-24 22:14:43 +0000386/* Macro to get the tp_richcompare field of a type if defined */
387#define RICHCOMPARE(t) (PyType_HasFeature((t), Py_TPFLAGS_HAVE_RICHCOMPARE) \
388 ? (t)->tp_richcompare : NULL)
389
Guido van Rossume797ec12001-01-17 15:24:28 +0000390/* Map rich comparison operators to their swapped version, e.g. LT --> GT */
391static int swapped_op[] = {Py_GT, Py_GE, Py_EQ, Py_NE, Py_LT, Py_LE};
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +0000392
Guido van Rossume797ec12001-01-17 15:24:28 +0000393/* Try a genuine rich comparison, returning an object. Return:
394 NULL for exception;
395 NotImplemented if this particular rich comparison is not implemented or
396 undefined;
397 some object not equal to NotImplemented if it is implemented
398 (this latter object may not be a Boolean).
399*/
400static PyObject *
401try_rich_compare(PyObject *v, PyObject *w, int op)
402{
403 richcmpfunc f;
404 PyObject *res;
405
Guido van Rossumd1f06b92001-01-24 22:14:43 +0000406 if ((f = RICHCOMPARE(v->ob_type)) != NULL) {
Guido van Rossume797ec12001-01-17 15:24:28 +0000407 res = (*f)(v, w, op);
408 if (res != Py_NotImplemented)
409 return res;
410 Py_DECREF(res);
411 }
Guido van Rossumd1f06b92001-01-24 22:14:43 +0000412 if ((f = RICHCOMPARE(w->ob_type)) != NULL) {
Guido van Rossume797ec12001-01-17 15:24:28 +0000413 return (*f)(w, v, swapped_op[op]);
414 }
415 res = Py_NotImplemented;
416 Py_INCREF(res);
417 return res;
418}
419
420/* Try a genuine rich comparison, returning an int. Return:
421 -1 for exception (including the case where try_rich_compare() returns an
422 object that's not a Boolean);
423 0 if the outcome is false;
424 1 if the outcome is true;
425 2 if this particular rich comparison is not implemented or undefined.
426*/
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +0000427static int
Guido van Rossume797ec12001-01-17 15:24:28 +0000428try_rich_compare_bool(PyObject *v, PyObject *w, int op)
429{
430 PyObject *res;
431 int ok;
432
Guido van Rossumd1f06b92001-01-24 22:14:43 +0000433 if (RICHCOMPARE(v->ob_type) == NULL && RICHCOMPARE(w->ob_type) == NULL)
Guido van Rossume797ec12001-01-17 15:24:28 +0000434 return 2; /* Shortcut, avoid INCREF+DECREF */
435 res = try_rich_compare(v, w, op);
436 if (res == NULL)
437 return -1;
438 if (res == Py_NotImplemented) {
439 Py_DECREF(res);
440 return 2;
441 }
442 ok = PyObject_IsTrue(res);
443 Py_DECREF(res);
444 return ok;
445}
446
447/* Try rich comparisons to determine a 3-way comparison. Return:
448 -2 for an exception;
449 -1 if v < w;
450 0 if v == w;
451 1 if v > w;
452 2 if this particular rich comparison is not implemented or undefined.
453*/
454static int
455try_rich_to_3way_compare(PyObject *v, PyObject *w)
456{
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000457 static struct { int op; int outcome; } tries[3] = {
458 /* Try this operator, and if it is true, use this outcome: */
459 {Py_EQ, 0},
460 {Py_LT, -1},
461 {Py_GT, 1},
462 };
463 int i;
464
Guido van Rossumd1f06b92001-01-24 22:14:43 +0000465 if (RICHCOMPARE(v->ob_type) == NULL && RICHCOMPARE(w->ob_type) == NULL)
Guido van Rossume797ec12001-01-17 15:24:28 +0000466 return 2; /* Shortcut */
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000467
468 for (i = 0; i < 3; i++) {
469 switch (try_rich_compare_bool(v, w, tries[i].op)) {
470 case -1:
471 return -1;
472 case 1:
473 return tries[i].outcome;
474 }
Guido van Rossume797ec12001-01-17 15:24:28 +0000475 }
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000476
477 return 2;
Guido van Rossume797ec12001-01-17 15:24:28 +0000478}
479
480/* Try a 3-way comparison, returning an int. Return:
481 -2 for an exception;
482 -1 if v < w;
483 0 if v == w;
484 1 if v > w;
485 2 if this particular 3-way comparison is not implemented or undefined.
486*/
487static int
488try_3way_compare(PyObject *v, PyObject *w)
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +0000489{
490 int c;
Guido van Rossume797ec12001-01-17 15:24:28 +0000491 cmpfunc f;
492
493 /* Comparisons involving instances are given to instance_compare,
494 which has the same return conventions as this function. */
495
496 if (PyInstance_Check(v))
497 return (*v->ob_type->tp_compare)(v, w);
498 if (PyInstance_Check(w))
499 return (*w->ob_type->tp_compare)(v, w);
500
501 /* If the types are equal, don't bother with coercions etc. */
502 if (v->ob_type == w->ob_type) {
503 if ((f = v->ob_type->tp_compare) == NULL)
504 return 2;
505 c = (*f)(v, w);
506 if (PyErr_Occurred())
507 return -2;
508 return c < 0 ? -1 : c > 0 ? 1 : 0;
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +0000509 }
Guido van Rossume797ec12001-01-17 15:24:28 +0000510
511 /* Try coercion; if it fails, give up */
512 c = PyNumber_CoerceEx(&v, &w);
513 if (c < 0)
514 return -2;
515 if (c > 0)
516 return 2;
517
518 /* Try v's comparison, if defined */
519 if ((f = v->ob_type->tp_compare) != NULL) {
520 c = (*f)(v, w);
521 Py_DECREF(v);
522 Py_DECREF(w);
523 if (PyErr_Occurred())
524 return -2;
525 return c < 0 ? -1 : c > 0 ? 1 : 0;
526 }
527
528 /* Try w's comparison, if defined */
529 if ((f = w->ob_type->tp_compare) != NULL) {
530 c = (*f)(w, v); /* swapped! */
531 Py_DECREF(v);
532 Py_DECREF(w);
533 if (PyErr_Occurred())
534 return -2;
535 return c < 0 ? 1 : c > 0 ? -1 : 0; /* negated! */
536 }
537
538 /* No comparison defined */
539 Py_DECREF(v);
540 Py_DECREF(w);
541 return 2;
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +0000542}
543
Guido van Rossume797ec12001-01-17 15:24:28 +0000544/* Final fallback 3-way comparison, returning an int. Return:
545 -2 if an error occurred;
546 -1 if v < w;
547 0 if v == w;
548 1 if v > w.
549*/
550static int
551default_3way_compare(PyObject *v, PyObject *w)
552{
553 int c;
Guido van Rossum8f9143d2001-01-22 15:59:32 +0000554 char *vname, *wname;
Guido van Rossume797ec12001-01-17 15:24:28 +0000555
556 if (v->ob_type == w->ob_type) {
Barry Warsawb0e754d2001-01-20 06:24:55 +0000557 /* When comparing these pointers, they must be cast to
558 * integer types (i.e. Py_uintptr_t, our spelling of C9X's
559 * uintptr_t). ANSI specifies that pointer compares other
560 * than == and != to non-related structures are undefined.
561 */
Barry Warsaw71ff8d52001-01-20 06:08:10 +0000562 Py_uintptr_t vv = (Py_uintptr_t)v;
563 Py_uintptr_t ww = (Py_uintptr_t)w;
Guido van Rossume797ec12001-01-17 15:24:28 +0000564 return (vv < ww) ? -1 : (vv > ww) ? 1 : 0;
565 }
566
567 /* Special case for Unicode */
568 if (PyUnicode_Check(v) || PyUnicode_Check(w)) {
569 c = PyUnicode_Compare(v, w);
570 if (!PyErr_Occurred())
571 return c;
572 /* TypeErrors are ignored: if Unicode coercion fails due
573 to one of the arguments not having the right type, we
574 continue as defined by the coercion protocol (see
575 above). Luckily, decoding errors are reported as
576 ValueErrors and are not masked by this technique. */
577 if (!PyErr_ExceptionMatches(PyExc_TypeError))
578 return -2;
579 PyErr_Clear();
580 }
581
Guido van Rossum0871e932001-01-22 19:28:09 +0000582 /* None is smaller than anything */
583 if (v == Py_None)
584 return -1;
585 if (w == Py_None)
586 return 1;
587
Guido van Rossume797ec12001-01-17 15:24:28 +0000588 /* different type: compare type names */
Guido van Rossum8f9143d2001-01-22 15:59:32 +0000589 if (v->ob_type->tp_as_number)
590 vname = "";
591 else
592 vname = v->ob_type->tp_name;
593 if (w->ob_type->tp_as_number)
594 wname = "";
595 else
596 wname = w->ob_type->tp_name;
597 c = strcmp(vname, wname);
598 if (c < 0)
599 return -1;
600 if (c > 0)
601 return 1;
602 /* Same type name, or (more likely) incomparable numeric types */
603 return ((Py_uintptr_t)(v->ob_type) < (
604 Py_uintptr_t)(w->ob_type)) ? -1 : 1;
Guido van Rossume797ec12001-01-17 15:24:28 +0000605}
606
607#define CHECK_TYPES(o) PyType_HasFeature((o)->ob_type, Py_TPFLAGS_CHECKTYPES)
608
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +0000609static int
Fred Drake100814d2000-07-09 15:48:49 +0000610do_cmp(PyObject *v, PyObject *w)
Guido van Rossum20566841995-01-12 11:26:10 +0000611{
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +0000612 int c;
613
Guido van Rossume797ec12001-01-17 15:24:28 +0000614 c = try_rich_to_3way_compare(v, w);
615 if (c < 2)
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +0000616 return c;
Guido van Rossume797ec12001-01-17 15:24:28 +0000617 c = try_3way_compare(v, w);
618 if (c < 2)
619 return c;
620 return default_3way_compare(v, w);
Guido van Rossum20566841995-01-12 11:26:10 +0000621}
622
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000623/* compare_nesting is incremented before calling compare (for
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000624 some types) and decremented on exit. If the count exceeds the
625 nesting limit, enable code to detect circular data structures.
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000626
627 This is a tunable parameter that should only affect the performance
628 of comparisons, nothing else. Setting it high makes comparing deeply
629 nested non-cyclical data structures faster, but makes comparing cyclical
630 data structures slower.
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000631*/
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000632#define NESTING_LIMIT 20
633
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000634static int compare_nesting = 0;
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000635
636static PyObject*
Fred Drake100814d2000-07-09 15:48:49 +0000637get_inprogress_dict(void)
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000638{
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000639 static PyObject *key;
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000640 PyObject *tstate_dict, *inprogress;
641
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000642 if (key == NULL) {
643 key = PyString_InternFromString("cmp_state");
644 if (key == NULL)
645 return NULL;
646 }
647
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000648 tstate_dict = PyThreadState_GetDict();
649 if (tstate_dict == NULL) {
650 PyErr_BadInternalCall();
651 return NULL;
652 }
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000653
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000654 inprogress = PyDict_GetItem(tstate_dict, key);
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000655 if (inprogress == NULL) {
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000656 inprogress = PyDict_New();
657 if (inprogress == NULL)
658 return NULL;
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000659 if (PyDict_SetItem(tstate_dict, key, inprogress) == -1) {
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000660 Py_DECREF(inprogress);
661 return NULL;
662 }
Jeremy Hyltona251ea02000-06-09 16:20:39 +0000663 Py_DECREF(inprogress);
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000664 }
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000665
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000666 return inprogress;
667}
668
669static PyObject *
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000670check_recursion(PyObject *v, PyObject *w, int op)
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000671{
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000672 PyObject *inprogress;
673 PyObject *token;
Barry Warsaw9d23a4e2000-08-18 05:01:19 +0000674 Py_uintptr_t iv = (Py_uintptr_t)v;
675 Py_uintptr_t iw = (Py_uintptr_t)w;
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000676 PyObject *x, *y, *z;
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000677
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000678 inprogress = get_inprogress_dict();
679 if (inprogress == NULL)
680 return NULL;
681
682 token = PyTuple_New(3);
683 if (token == NULL)
684 return NULL;
685
686 if (iv <= iw) {
687 PyTuple_SET_ITEM(token, 0, x = PyLong_FromVoidPtr((void *)v));
688 PyTuple_SET_ITEM(token, 1, y = PyLong_FromVoidPtr((void *)w));
689 if (op >= 0)
690 op = swapped_op[op];
691 } else {
692 PyTuple_SET_ITEM(token, 0, x = PyLong_FromVoidPtr((void *)w));
693 PyTuple_SET_ITEM(token, 1, y = PyLong_FromVoidPtr((void *)v));
694 }
695 PyTuple_SET_ITEM(token, 2, z = PyInt_FromLong((long)op));
696 if (x == NULL || y == NULL || z == NULL) {
697 Py_DECREF(token);
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000698 return NULL;
699 }
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000700
701 if (PyDict_GetItem(inprogress, token) != NULL) {
702 Py_DECREF(token);
703 return Py_None; /* Without INCREF! */
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000704 }
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000705
706 if (PyDict_SetItem(inprogress, token, token) < 0) {
707 Py_DECREF(token);
708 return NULL;
709 }
710
711 return token;
712}
713
714static void
715delete_token(PyObject *token)
716{
717 PyObject *inprogress;
718
719 if (token == NULL || token == Py_None)
720 return;
721 inprogress = get_inprogress_dict();
722 if (inprogress == NULL)
723 PyErr_Clear();
724 else
725 PyDict_DelItem(inprogress, token);
726 Py_DECREF(token);
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000727}
728
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000729int
Fred Drake100814d2000-07-09 15:48:49 +0000730PyObject_Compare(PyObject *v, PyObject *w)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000731{
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000732 PyTypeObject *vtp;
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000733 int result;
734
Jack Jansend49cbe12000-08-22 21:52:51 +0000735#if defined(USE_STACKCHECK)
Guido van Rossum9c0a99e2000-08-30 15:53:50 +0000736 if (PyOS_CheckStack()) {
Jack Jansend49cbe12000-08-22 21:52:51 +0000737 PyErr_SetString(PyExc_MemoryError, "Stack overflow");
738 return -1;
739 }
740#endif
Guido van Rossumc8b6df91997-05-23 00:06:51 +0000741 if (v == NULL || w == NULL) {
742 PyErr_BadInternalCall();
743 return -1;
744 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000745 if (v == w)
746 return 0;
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +0000747 vtp = v->ob_type;
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000748 compare_nesting++;
749 if (compare_nesting > NESTING_LIMIT &&
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +0000750 (vtp->tp_as_mapping
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000751 || (vtp->tp_as_sequence
752 && !PyString_Check(v)
753 && !PyTuple_Check(v)))) {
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +0000754 /* try to detect circular data structures */
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000755 PyObject *token = check_recursion(v, w, -1);
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000756
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000757 if (token == NULL) {
758 result = -1;
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000759 }
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000760 else if (token == Py_None) {
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000761 /* already comparing these objects. assume
762 they're equal until shown otherwise */
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +0000763 result = 0;
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000764 }
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000765 else {
766 result = do_cmp(v, w);
767 delete_token(token);
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000768 }
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000769 }
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +0000770 else {
771 result = do_cmp(v, w);
772 }
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000773 compare_nesting--;
Guido van Rossume797ec12001-01-17 15:24:28 +0000774 return result < 0 ? -1 : result;
775}
776
777static PyObject *
778try_3way_to_rich_compare(PyObject *v, PyObject *w, int op)
779{
780 int c;
781 PyObject *result;
782
783 c = try_3way_compare(v, w);
Guido van Rossume797ec12001-01-17 15:24:28 +0000784 if (c >= 2)
785 c = default_3way_compare(v, w);
Guido van Rossum2da0ea82001-02-22 22:18:04 +0000786 if (c <= -2)
787 return NULL;
Guido van Rossume797ec12001-01-17 15:24:28 +0000788 switch (op) {
789 case Py_LT: c = c < 0; break;
790 case Py_LE: c = c <= 0; break;
791 case Py_EQ: c = c == 0; break;
792 case Py_NE: c = c != 0; break;
793 case Py_GT: c = c > 0; break;
794 case Py_GE: c = c >= 0; break;
795 }
796 result = c ? Py_True : Py_False;
797 Py_INCREF(result);
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000798 return result;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000799}
800
Neil Schemenauerd38855c2001-01-21 16:25:18 +0000801static PyObject *
Guido van Rossume797ec12001-01-17 15:24:28 +0000802do_richcmp(PyObject *v, PyObject *w, int op)
803{
804 PyObject *res;
805
806 res = try_rich_compare(v, w, op);
807 if (res != Py_NotImplemented)
808 return res;
809 Py_DECREF(res);
810
811 return try_3way_to_rich_compare(v, w, op);
812}
813
814PyObject *
815PyObject_RichCompare(PyObject *v, PyObject *w, int op)
816{
817 PyObject *res;
818
819 assert(Py_LT <= op && op <= Py_GE);
820
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000821 compare_nesting++;
822 if (compare_nesting > NESTING_LIMIT &&
823 (v->ob_type->tp_as_mapping
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000824 || (v->ob_type->tp_as_sequence
825 && !PyString_Check(v)
826 && !PyTuple_Check(v)))) {
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000827 /* try to detect circular data structures */
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000828 PyObject *token = check_recursion(v, w, op);
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000829
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000830 if (token == NULL) {
831 res = NULL;
Guido van Rossume797ec12001-01-17 15:24:28 +0000832 }
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000833 else if (token == Py_None) {
834 /* already comparing these objects with this operator.
835 assume they're equal until shown otherwise */
836 if (op == Py_EQ)
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000837 res = Py_True;
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000838 else if (op == Py_NE)
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000839 res = Py_False;
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000840 else {
841 PyErr_SetString(PyExc_ValueError,
842 "can't order recursive values");
843 res = NULL;
844 }
845 Py_XINCREF(res);
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000846 }
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000847 else {
848 res = do_richcmp(v, w, op);
849 delete_token(token);
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000850 }
Guido van Rossume797ec12001-01-17 15:24:28 +0000851 }
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000852 else {
853 res = do_richcmp(v, w, op);
854 }
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000855 compare_nesting--;
Guido van Rossume797ec12001-01-17 15:24:28 +0000856 return res;
857}
858
859int
860PyObject_RichCompareBool(PyObject *v, PyObject *w, int op)
861{
862 PyObject *res = PyObject_RichCompare(v, w, op);
863 int ok;
864
865 if (res == NULL)
866 return -1;
867 ok = PyObject_IsTrue(res);
868 Py_DECREF(res);
869 return ok;
870}
Fred Drake13634cf2000-06-29 19:17:04 +0000871
872/* Set of hash utility functions to help maintaining the invariant that
873 iff a==b then hash(a)==hash(b)
874
875 All the utility functions (_Py_Hash*()) return "-1" to signify an error.
876*/
877
878long
Fred Drake100814d2000-07-09 15:48:49 +0000879_Py_HashDouble(double v)
Fred Drake13634cf2000-06-29 19:17:04 +0000880{
Tim Peters39dce292000-08-15 03:34:48 +0000881 double intpart, fractpart;
882 int expo;
883 long hipart;
884 long x; /* the final hash value */
885 /* This is designed so that Python numbers of different types
886 * that compare equal hash to the same value; otherwise comparisons
887 * of mapping keys will turn out weird.
888 */
889
890#ifdef MPW /* MPW C modf expects pointer to extended as second argument */
891{
892 extended e;
893 fractpart = modf(v, &e);
894 intpart = e;
895}
896#else
897 fractpart = modf(v, &intpart);
898#endif
899 if (fractpart == 0.0) {
900 /* This must return the same hash as an equal int or long. */
901 if (intpart > LONG_MAX || -intpart > LONG_MAX) {
902 /* Convert to long and use its hash. */
903 PyObject *plong; /* converted to Python long */
904 if (Py_IS_INFINITY(intpart))
905 /* can't convert to long int -- arbitrary */
906 v = v < 0 ? -271828.0 : 314159.0;
907 plong = PyLong_FromDouble(v);
908 if (plong == NULL)
909 return -1;
910 x = PyObject_Hash(plong);
911 Py_DECREF(plong);
912 return x;
913 }
914 /* Fits in a C long == a Python int, so is its own hash. */
915 x = (long)intpart;
916 if (x == -1)
917 x = -2;
918 return x;
919 }
920 /* The fractional part is non-zero, so we don't have to worry about
921 * making this match the hash of some other type.
922 * Use frexp to get at the bits in the double.
Fred Drake13634cf2000-06-29 19:17:04 +0000923 * Since the VAX D double format has 56 mantissa bits, which is the
924 * most of any double format in use, each of these parts may have as
925 * many as (but no more than) 56 significant bits.
Tim Peters39dce292000-08-15 03:34:48 +0000926 * So, assuming sizeof(long) >= 4, each part can be broken into two
927 * longs; frexp and multiplication are used to do that.
928 * Also, since the Cray double format has 15 exponent bits, which is
929 * the most of any double format in use, shifting the exponent field
930 * left by 15 won't overflow a long (again assuming sizeof(long) >= 4).
Fred Drake13634cf2000-06-29 19:17:04 +0000931 */
Tim Peters39dce292000-08-15 03:34:48 +0000932 v = frexp(v, &expo);
933 v *= 2147483648.0; /* 2**31 */
934 hipart = (long)v; /* take the top 32 bits */
935 v = (v - (double)hipart) * 2147483648.0; /* get the next 32 bits */
936 x = hipart + (long)v + (expo << 15);
937 if (x == -1)
938 x = -2;
939 return x;
Fred Drake13634cf2000-06-29 19:17:04 +0000940}
941
942long
Fred Drake100814d2000-07-09 15:48:49 +0000943_Py_HashPointer(void *p)
Fred Drake13634cf2000-06-29 19:17:04 +0000944{
945#if SIZEOF_LONG >= SIZEOF_VOID_P
946 return (long)p;
947#else
948 /* convert to a Python long and hash that */
949 PyObject* longobj;
950 long x;
951
952 if ((longobj = PyLong_FromVoidPtr(p)) == NULL) {
953 x = -1;
954 goto finally;
955 }
956 x = PyObject_Hash(longobj);
957
958finally:
959 Py_XDECREF(longobj);
960 return x;
961#endif
962}
963
964
Guido van Rossum9bfef441993-03-29 10:43:31 +0000965long
Fred Drake100814d2000-07-09 15:48:49 +0000966PyObject_Hash(PyObject *v)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000967{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000968 PyTypeObject *tp = v->ob_type;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000969 if (tp->tp_hash != NULL)
970 return (*tp->tp_hash)(v);
Guido van Rossumd1f06b92001-01-24 22:14:43 +0000971 if (tp->tp_compare == NULL && RICHCOMPARE(tp) == NULL) {
Fred Drake13634cf2000-06-29 19:17:04 +0000972 return _Py_HashPointer(v); /* Use address as hash value */
973 }
Guido van Rossum9bfef441993-03-29 10:43:31 +0000974 /* If there's a cmp but no hash defined, the object can't be hashed */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000975 PyErr_SetString(PyExc_TypeError, "unhashable type");
Guido van Rossum9bfef441993-03-29 10:43:31 +0000976 return -1;
977}
978
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000979PyObject *
Fred Drake100814d2000-07-09 15:48:49 +0000980PyObject_GetAttrString(PyObject *v, char *name)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000981{
Guido van Rossumd8eb1b31996-08-09 20:52:03 +0000982 if (v->ob_type->tp_getattro != NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000983 PyObject *w, *res;
Guido van Rossumc6d06701997-01-18 07:57:16 +0000984 w = PyString_InternFromString(name);
Guido van Rossumd8eb1b31996-08-09 20:52:03 +0000985 if (w == NULL)
986 return NULL;
987 res = (*v->ob_type->tp_getattro)(v, w);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000988 Py_XDECREF(w);
Guido van Rossumd8eb1b31996-08-09 20:52:03 +0000989 return res;
990 }
991
Guido van Rossum3f5da241990-12-20 15:06:42 +0000992 if (v->ob_type->tp_getattr == NULL) {
Guido van Rossumdb935161998-01-19 22:16:36 +0000993 PyErr_Format(PyExc_AttributeError,
994 "'%.50s' object has no attribute '%.400s'",
995 v->ob_type->tp_name,
996 name);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000997 return NULL;
998 }
999 else {
1000 return (*v->ob_type->tp_getattr)(v, name);
1001 }
1002}
1003
1004int
Fred Drake100814d2000-07-09 15:48:49 +00001005PyObject_HasAttrString(PyObject *v, char *name)
Guido van Rossumed18fdc1993-07-11 19:55:34 +00001006{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001007 PyObject *res = PyObject_GetAttrString(v, name);
Guido van Rossumed18fdc1993-07-11 19:55:34 +00001008 if (res != NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001009 Py_DECREF(res);
Guido van Rossumed18fdc1993-07-11 19:55:34 +00001010 return 1;
1011 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001012 PyErr_Clear();
Guido van Rossumed18fdc1993-07-11 19:55:34 +00001013 return 0;
1014}
1015
1016int
Fred Drake100814d2000-07-09 15:48:49 +00001017PyObject_SetAttrString(PyObject *v, char *name, PyObject *w)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001018{
Guido van Rossumd8eb1b31996-08-09 20:52:03 +00001019 if (v->ob_type->tp_setattro != NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001020 PyObject *s;
Guido van Rossumd8eb1b31996-08-09 20:52:03 +00001021 int res;
Guido van Rossumc6d06701997-01-18 07:57:16 +00001022 s = PyString_InternFromString(name);
Guido van Rossumd8eb1b31996-08-09 20:52:03 +00001023 if (s == NULL)
Guido van Rossumb7fc3041996-09-11 22:51:25 +00001024 return -1;
Guido van Rossumd8eb1b31996-08-09 20:52:03 +00001025 res = (*v->ob_type->tp_setattro)(v, s, w);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001026 Py_XDECREF(s);
Guido van Rossumd8eb1b31996-08-09 20:52:03 +00001027 return res;
1028 }
1029
Guido van Rossum3f5da241990-12-20 15:06:42 +00001030 if (v->ob_type->tp_setattr == NULL) {
1031 if (v->ob_type->tp_getattr == NULL)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001032 PyErr_SetString(PyExc_TypeError,
Guido van Rossum3ea74121991-12-24 13:28:03 +00001033 "attribute-less object (assign or del)");
Guido van Rossum3f5da241990-12-20 15:06:42 +00001034 else
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001035 PyErr_SetString(PyExc_TypeError,
Guido van Rossum3ea74121991-12-24 13:28:03 +00001036 "object has read-only attributes");
Guido van Rossum73531a31990-12-20 23:12:40 +00001037 return -1;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001038 }
1039 else {
1040 return (*v->ob_type->tp_setattr)(v, name, w);
1041 }
1042}
1043
Marc-André Lemburge44e5072000-09-18 16:20:57 +00001044/* Internal API needed by PyObject_GetAttr(): */
1045extern
1046PyObject *_PyUnicode_AsDefaultEncodedString(PyObject *unicode,
1047 const char *errors);
1048
Guido van Rossum98ff96a1997-05-20 18:34:44 +00001049PyObject *
Fred Drake100814d2000-07-09 15:48:49 +00001050PyObject_GetAttr(PyObject *v, PyObject *name)
Guido van Rossum98ff96a1997-05-20 18:34:44 +00001051{
Marc-André Lemburge44e5072000-09-18 16:20:57 +00001052 /* The Unicode to string conversion is done here because the
1053 existing tp_getattro slots expect a string object as name
1054 and we wouldn't want to break those. */
1055 if (PyUnicode_Check(name)) {
1056 name = _PyUnicode_AsDefaultEncodedString(name, NULL);
1057 if (name == NULL)
1058 return NULL;
1059 }
Jeremy Hylton99a8f902000-06-23 14:36:32 +00001060
1061 if (!PyString_Check(name)) {
1062 PyErr_SetString(PyExc_TypeError,
1063 "attribute name must be string");
1064 return NULL;
1065 }
Marc-André Lemburge44e5072000-09-18 16:20:57 +00001066 if (v->ob_type->tp_getattro != NULL)
1067 return (*v->ob_type->tp_getattro)(v, name);
1068 else
Jeremy Hylton99a8f902000-06-23 14:36:32 +00001069 return PyObject_GetAttrString(v, PyString_AS_STRING(name));
Guido van Rossum98ff96a1997-05-20 18:34:44 +00001070}
1071
1072int
Fred Drake100814d2000-07-09 15:48:49 +00001073PyObject_HasAttr(PyObject *v, PyObject *name)
Guido van Rossum98ff96a1997-05-20 18:34:44 +00001074{
1075 PyObject *res = PyObject_GetAttr(v, name);
1076 if (res != NULL) {
1077 Py_DECREF(res);
1078 return 1;
1079 }
1080 PyErr_Clear();
1081 return 0;
1082}
1083
1084int
Fred Drake100814d2000-07-09 15:48:49 +00001085PyObject_SetAttr(PyObject *v, PyObject *name, PyObject *value)
Guido van Rossum98ff96a1997-05-20 18:34:44 +00001086{
1087 int err;
Marc-André Lemburge44e5072000-09-18 16:20:57 +00001088
1089 /* The Unicode to string conversion is done here because the
1090 existing tp_setattro slots expect a string object as name
1091 and we wouldn't want to break those. */
1092 if (PyUnicode_Check(name)) {
1093 name = PyUnicode_AsEncodedString(name, NULL, NULL);
1094 if (name == NULL)
1095 return -1;
Jeremy Hylton99a8f902000-06-23 14:36:32 +00001096 }
Marc-André Lemburge44e5072000-09-18 16:20:57 +00001097 else
1098 Py_INCREF(name);
1099
1100 if (!PyString_Check(name)){
Jeremy Hylton99a8f902000-06-23 14:36:32 +00001101 PyErr_SetString(PyExc_TypeError,
1102 "attribute name must be string");
1103 err = -1;
1104 }
Marc-André Lemburge44e5072000-09-18 16:20:57 +00001105 else {
1106 PyString_InternInPlace(&name);
1107 if (v->ob_type->tp_setattro != NULL)
1108 err = (*v->ob_type->tp_setattro)(v, name, value);
1109 else
1110 err = PyObject_SetAttrString(v,
1111 PyString_AS_STRING(name), value);
1112 }
1113
Guido van Rossum98ff96a1997-05-20 18:34:44 +00001114 Py_DECREF(name);
1115 return err;
1116}
1117
Guido van Rossum6ac258d1993-05-12 08:24:20 +00001118/* Test a value used as condition, e.g., in a for or if statement.
1119 Return -1 if an error occurred */
1120
1121int
Fred Drake100814d2000-07-09 15:48:49 +00001122PyObject_IsTrue(PyObject *v)
Guido van Rossum6ac258d1993-05-12 08:24:20 +00001123{
1124 int res;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001125 if (v == Py_None)
Guido van Rossum6ac258d1993-05-12 08:24:20 +00001126 res = 0;
Guido van Rossum1c4f4581998-05-22 00:53:24 +00001127 else if (v->ob_type->tp_as_number != NULL &&
1128 v->ob_type->tp_as_number->nb_nonzero != NULL)
Guido van Rossum6ac258d1993-05-12 08:24:20 +00001129 res = (*v->ob_type->tp_as_number->nb_nonzero)(v);
Guido van Rossum1c4f4581998-05-22 00:53:24 +00001130 else if (v->ob_type->tp_as_mapping != NULL &&
1131 v->ob_type->tp_as_mapping->mp_length != NULL)
Guido van Rossum6ac258d1993-05-12 08:24:20 +00001132 res = (*v->ob_type->tp_as_mapping->mp_length)(v);
Guido van Rossum1c4f4581998-05-22 00:53:24 +00001133 else if (v->ob_type->tp_as_sequence != NULL &&
1134 v->ob_type->tp_as_sequence->sq_length != NULL)
Guido van Rossum6ac258d1993-05-12 08:24:20 +00001135 res = (*v->ob_type->tp_as_sequence->sq_length)(v);
1136 else
1137 res = 1;
1138 if (res > 0)
1139 res = 1;
1140 return res;
1141}
1142
Guido van Rossumc3d3f961998-04-09 17:53:59 +00001143/* equivalent of 'not v'
1144 Return -1 if an error occurred */
1145
1146int
Fred Drake100814d2000-07-09 15:48:49 +00001147PyObject_Not(PyObject *v)
Guido van Rossumc3d3f961998-04-09 17:53:59 +00001148{
1149 int res;
1150 res = PyObject_IsTrue(v);
1151 if (res < 0)
1152 return res;
1153 return res == 0;
1154}
1155
Guido van Rossum5524a591995-01-10 15:26:20 +00001156/* Coerce two numeric types to the "larger" one.
1157 Increment the reference count on each argument.
Guido van Rossume797ec12001-01-17 15:24:28 +00001158 Return value:
1159 -1 if an error occurred;
1160 0 if the coercion succeeded (and then the reference counts are increased);
1161 1 if no coercion is possible (and no error is raised).
Guido van Rossum5524a591995-01-10 15:26:20 +00001162*/
Guido van Rossum5524a591995-01-10 15:26:20 +00001163int
Fred Drake100814d2000-07-09 15:48:49 +00001164PyNumber_CoerceEx(PyObject **pv, PyObject **pw)
Guido van Rossum5524a591995-01-10 15:26:20 +00001165{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001166 register PyObject *v = *pv;
1167 register PyObject *w = *pw;
Guido van Rossum5524a591995-01-10 15:26:20 +00001168 int res;
1169
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001170 if (v->ob_type == w->ob_type && !PyInstance_Check(v)) {
1171 Py_INCREF(v);
1172 Py_INCREF(w);
Guido van Rossum5524a591995-01-10 15:26:20 +00001173 return 0;
1174 }
1175 if (v->ob_type->tp_as_number && v->ob_type->tp_as_number->nb_coerce) {
1176 res = (*v->ob_type->tp_as_number->nb_coerce)(pv, pw);
1177 if (res <= 0)
1178 return res;
1179 }
1180 if (w->ob_type->tp_as_number && w->ob_type->tp_as_number->nb_coerce) {
1181 res = (*w->ob_type->tp_as_number->nb_coerce)(pw, pv);
1182 if (res <= 0)
1183 return res;
1184 }
Guido van Rossum242c6421997-11-19 16:03:17 +00001185 return 1;
1186}
1187
Guido van Rossume797ec12001-01-17 15:24:28 +00001188/* Coerce two numeric types to the "larger" one.
1189 Increment the reference count on each argument.
1190 Return -1 and raise an exception if no coercion is possible
1191 (and then no reference count is incremented).
1192*/
Guido van Rossum242c6421997-11-19 16:03:17 +00001193int
Fred Drake100814d2000-07-09 15:48:49 +00001194PyNumber_Coerce(PyObject **pv, PyObject **pw)
Guido van Rossum242c6421997-11-19 16:03:17 +00001195{
1196 int err = PyNumber_CoerceEx(pv, pw);
1197 if (err <= 0)
1198 return err;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001199 PyErr_SetString(PyExc_TypeError, "number coercion failed");
Guido van Rossum5524a591995-01-10 15:26:20 +00001200 return -1;
1201}
1202
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001203
Guido van Rossum49b11fe1995-01-26 00:38:22 +00001204/* Test whether an object can be called */
1205
1206int
Fred Drake100814d2000-07-09 15:48:49 +00001207PyCallable_Check(PyObject *x)
Guido van Rossum49b11fe1995-01-26 00:38:22 +00001208{
1209 if (x == NULL)
1210 return 0;
1211 if (x->ob_type->tp_call != NULL ||
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001212 PyFunction_Check(x) ||
1213 PyMethod_Check(x) ||
1214 PyCFunction_Check(x) ||
1215 PyClass_Check(x))
Guido van Rossum49b11fe1995-01-26 00:38:22 +00001216 return 1;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001217 if (PyInstance_Check(x)) {
1218 PyObject *call = PyObject_GetAttrString(x, "__call__");
Guido van Rossum49b11fe1995-01-26 00:38:22 +00001219 if (call == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001220 PyErr_Clear();
Guido van Rossum49b11fe1995-01-26 00:38:22 +00001221 return 0;
1222 }
1223 /* Could test recursively but don't, for fear of endless
1224 recursion if some joker sets self.__call__ = self */
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001225 Py_DECREF(call);
Guido van Rossum49b11fe1995-01-26 00:38:22 +00001226 return 1;
1227 }
1228 return 0;
1229}
1230
1231
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001232/*
1233NoObject is usable as a non-NULL undefined value, used by the macro None.
1234There is (and should be!) no way to create other objects of this type,
Guido van Rossum3f5da241990-12-20 15:06:42 +00001235so there is exactly one (which is indestructible, by the way).
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001236*/
1237
Guido van Rossum0c182a11992-03-27 17:26:13 +00001238/* ARGSUSED */
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001239static PyObject *
Fred Drake100814d2000-07-09 15:48:49 +00001240none_repr(PyObject *op)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001241{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001242 return PyString_FromString("None");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001243}
1244
Barry Warsaw9bf16442001-01-23 16:24:35 +00001245/* ARGUSED */
1246static void
1247none_dealloc(PyObject* ignore)
1248{
1249 /* This should never get called, but we also don't want to SEGV if
1250 * we accidently decref None out of existance.
1251 */
1252 abort();
1253}
1254
1255
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001256static PyTypeObject PyNothing_Type = {
1257 PyObject_HEAD_INIT(&PyType_Type)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001258 0,
Guido van Rossum3f5da241990-12-20 15:06:42 +00001259 "None",
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001260 0,
1261 0,
Barry Warsaw9bf16442001-01-23 16:24:35 +00001262 (destructor)none_dealloc, /*tp_dealloc*/ /*never called*/
Guido van Rossum7066dd71992-09-17 17:54:56 +00001263 0, /*tp_print*/
Guido van Rossum3f5da241990-12-20 15:06:42 +00001264 0, /*tp_getattr*/
1265 0, /*tp_setattr*/
1266 0, /*tp_compare*/
Guido van Rossum1d5735e1994-08-30 08:27:36 +00001267 (reprfunc)none_repr, /*tp_repr*/
Guido van Rossum3f5da241990-12-20 15:06:42 +00001268 0, /*tp_as_number*/
1269 0, /*tp_as_sequence*/
1270 0, /*tp_as_mapping*/
Guido van Rossum9bfef441993-03-29 10:43:31 +00001271 0, /*tp_hash */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001272};
1273
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001274PyObject _Py_NoneStruct = {
1275 PyObject_HEAD_INIT(&PyNothing_Type)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001276};
1277
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +00001278/* NotImplemented is an object that can be used to signal that an
1279 operation is not implemented for the given type combination. */
1280
1281static PyObject *
1282NotImplemented_repr(PyObject *op)
1283{
1284 return PyString_FromString("NotImplemented");
1285}
1286
1287static PyTypeObject PyNotImplemented_Type = {
1288 PyObject_HEAD_INIT(&PyType_Type)
1289 0,
1290 "NotImplemented",
1291 0,
1292 0,
Barry Warsaw9bf16442001-01-23 16:24:35 +00001293 (destructor)none_dealloc, /*tp_dealloc*/ /*never called*/
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +00001294 0, /*tp_print*/
1295 0, /*tp_getattr*/
1296 0, /*tp_setattr*/
1297 0, /*tp_compare*/
1298 (reprfunc)NotImplemented_repr, /*tp_repr*/
1299 0, /*tp_as_number*/
1300 0, /*tp_as_sequence*/
1301 0, /*tp_as_mapping*/
1302 0, /*tp_hash */
1303};
1304
1305PyObject _Py_NotImplementedStruct = {
1306 PyObject_HEAD_INIT(&PyNotImplemented_Type)
1307};
1308
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001309
Guido van Rossum84a90321996-05-22 16:34:47 +00001310#ifdef Py_TRACE_REFS
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001311
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001312static PyObject refchain = {&refchain, &refchain};
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001313
Guido van Rossumaacdc9d1996-08-12 21:32:12 +00001314void
Fred Drake100814d2000-07-09 15:48:49 +00001315_Py_ResetReferences(void)
Guido van Rossume09fb551997-08-05 02:04:34 +00001316{
1317 refchain._ob_prev = refchain._ob_next = &refchain;
1318 _Py_RefTotal = 0;
1319}
1320
1321void
Fred Drake100814d2000-07-09 15:48:49 +00001322_Py_NewReference(PyObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001323{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001324 _Py_RefTotal++;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001325 op->ob_refcnt = 1;
1326 op->_ob_next = refchain._ob_next;
1327 op->_ob_prev = &refchain;
1328 refchain._ob_next->_ob_prev = op;
1329 refchain._ob_next = op;
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +00001330#ifdef COUNT_ALLOCS
1331 inc_count(op->ob_type);
1332#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001333}
1334
Guido van Rossumaacdc9d1996-08-12 21:32:12 +00001335void
Fred Drake100814d2000-07-09 15:48:49 +00001336_Py_ForgetReference(register PyObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001337{
Guido van Rossumbffd6832000-01-20 22:32:56 +00001338#ifdef SLOW_UNREF_CHECK
Guido van Rossum4c08d552000-03-10 22:55:18 +00001339 register PyObject *p;
Guido van Rossumbffd6832000-01-20 22:32:56 +00001340#endif
Guido van Rossumd7047b31995-01-02 19:07:15 +00001341 if (op->ob_refcnt < 0)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001342 Py_FatalError("UNREF negative refcnt");
Guido van Rossum2e8f6141992-09-03 20:32:55 +00001343 if (op == &refchain ||
Guido van Rossumd7047b31995-01-02 19:07:15 +00001344 op->_ob_prev->_ob_next != op || op->_ob_next->_ob_prev != op)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001345 Py_FatalError("UNREF invalid object");
Guido van Rossum2e8f6141992-09-03 20:32:55 +00001346#ifdef SLOW_UNREF_CHECK
Guido van Rossum3f5da241990-12-20 15:06:42 +00001347 for (p = refchain._ob_next; p != &refchain; p = p->_ob_next) {
1348 if (p == op)
1349 break;
1350 }
Guido van Rossumd7047b31995-01-02 19:07:15 +00001351 if (p == &refchain) /* Not found */
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001352 Py_FatalError("UNREF unknown object");
Guido van Rossum2e8f6141992-09-03 20:32:55 +00001353#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001354 op->_ob_next->_ob_prev = op->_ob_prev;
1355 op->_ob_prev->_ob_next = op->_ob_next;
Guido van Rossum2e8f6141992-09-03 20:32:55 +00001356 op->_ob_next = op->_ob_prev = NULL;
Guido van Rossumd8953cb1995-04-06 14:46:26 +00001357#ifdef COUNT_ALLOCS
1358 op->ob_type->tp_free++;
1359#endif
Guido van Rossum3f5da241990-12-20 15:06:42 +00001360}
1361
Guido van Rossumaacdc9d1996-08-12 21:32:12 +00001362void
Fred Drake100814d2000-07-09 15:48:49 +00001363_Py_Dealloc(PyObject *op)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001364{
Guido van Rossum9776adf1994-09-07 14:36:45 +00001365 destructor dealloc = op->ob_type->tp_dealloc;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001366 _Py_ForgetReference(op);
Guido van Rossum9776adf1994-09-07 14:36:45 +00001367 (*dealloc)(op);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001368}
1369
Guido van Rossumaacdc9d1996-08-12 21:32:12 +00001370void
Fred Drake100814d2000-07-09 15:48:49 +00001371_Py_PrintReferences(FILE *fp)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001372{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001373 PyObject *op;
Guido van Rossume09fb551997-08-05 02:04:34 +00001374 fprintf(fp, "Remaining objects:\n");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001375 for (op = refchain._ob_next; op != &refchain; op = op->_ob_next) {
1376 fprintf(fp, "[%d] ", op->ob_refcnt);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001377 if (PyObject_Print(op, fp, 0) != 0)
1378 PyErr_Clear();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001379 putc('\n', fp);
1380 }
1381}
1382
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +00001383PyObject *
Fred Drake100814d2000-07-09 15:48:49 +00001384_Py_GetObjects(PyObject *self, PyObject *args)
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +00001385{
1386 int i, n;
1387 PyObject *t = NULL;
1388 PyObject *res, *op;
1389
1390 if (!PyArg_ParseTuple(args, "i|O", &n, &t))
1391 return NULL;
1392 op = refchain._ob_next;
1393 res = PyList_New(0);
1394 if (res == NULL)
1395 return NULL;
1396 for (i = 0; (n == 0 || i < n) && op != &refchain; i++) {
1397 while (op == self || op == args || op == res || op == t ||
1398 t != NULL && op->ob_type != (PyTypeObject *) t) {
1399 op = op->_ob_next;
1400 if (op == &refchain)
1401 return res;
1402 }
1403 if (PyList_Append(res, op) < 0) {
1404 Py_DECREF(res);
1405 return NULL;
1406 }
1407 op = op->_ob_next;
1408 }
1409 return res;
1410}
1411
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001412#endif
Guido van Rossum97ead3f1996-01-12 01:24:09 +00001413
1414
1415/* Hack to force loading of cobject.o */
Guido van Rossumda9c2711996-12-05 21:58:58 +00001416PyTypeObject *_Py_cobject_hack = &PyCObject_Type;
Guido van Rossum84a90321996-05-22 16:34:47 +00001417
1418
1419/* Hack to force loading of abstract.o */
Jeremy Hylton6253f832000-07-12 12:56:19 +00001420int (*_Py_abstract_hack)(PyObject *) = &PyObject_Size;
Guido van Rossume09fb551997-08-05 02:04:34 +00001421
1422
Andrew M. Kuchling1582a3a2000-08-16 12:27:23 +00001423/* Python's malloc wrappers (see pymem.h) */
Guido van Rossume09fb551997-08-05 02:04:34 +00001424
Thomas Wouters334fb892000-07-25 12:56:38 +00001425void *
Fred Drake100814d2000-07-09 15:48:49 +00001426PyMem_Malloc(size_t nbytes)
Guido van Rossume09fb551997-08-05 02:04:34 +00001427{
1428#if _PyMem_EXTRA > 0
1429 if (nbytes == 0)
1430 nbytes = _PyMem_EXTRA;
1431#endif
Guido van Rossumb18618d2000-05-03 23:44:39 +00001432 return PyMem_MALLOC(nbytes);
Guido van Rossume09fb551997-08-05 02:04:34 +00001433}
1434
Thomas Wouters334fb892000-07-25 12:56:38 +00001435void *
1436PyMem_Realloc(void *p, size_t nbytes)
Guido van Rossume09fb551997-08-05 02:04:34 +00001437{
1438#if _PyMem_EXTRA > 0
1439 if (nbytes == 0)
1440 nbytes = _PyMem_EXTRA;
1441#endif
Guido van Rossumb18618d2000-05-03 23:44:39 +00001442 return PyMem_REALLOC(p, nbytes);
Guido van Rossume09fb551997-08-05 02:04:34 +00001443}
1444
1445void
Thomas Wouters334fb892000-07-25 12:56:38 +00001446PyMem_Free(void *p)
Guido van Rossume09fb551997-08-05 02:04:34 +00001447{
Guido van Rossumb18618d2000-05-03 23:44:39 +00001448 PyMem_FREE(p);
1449}
1450
1451
1452/* Python's object malloc wrappers (see objimpl.h) */
1453
Thomas Wouters334fb892000-07-25 12:56:38 +00001454void *
Fred Drake100814d2000-07-09 15:48:49 +00001455PyObject_Malloc(size_t nbytes)
Guido van Rossumb18618d2000-05-03 23:44:39 +00001456{
1457 return PyObject_MALLOC(nbytes);
1458}
1459
Thomas Wouters334fb892000-07-25 12:56:38 +00001460void *
1461PyObject_Realloc(void *p, size_t nbytes)
Guido van Rossumb18618d2000-05-03 23:44:39 +00001462{
1463 return PyObject_REALLOC(p, nbytes);
1464}
1465
1466void
Thomas Wouters334fb892000-07-25 12:56:38 +00001467PyObject_Free(void *p)
Guido van Rossumb18618d2000-05-03 23:44:39 +00001468{
1469 PyObject_FREE(p);
Guido van Rossume09fb551997-08-05 02:04:34 +00001470}
Guido van Rossum86610361998-04-10 22:32:46 +00001471
1472
Fred Drake41deb1e2001-02-01 05:27:45 +00001473/* Hook to clear up weak references only once the _weakref module is
1474 imported. We use a dummy implementation to simplify the code at each
1475 call site instead of requiring a test for NULL.
1476*/
1477
1478static int
1479empty_clear_weak_refs(PyObject *o)
1480{
1481 return 1;
1482}
1483
1484int (*PyObject_ClearWeakRefs)(PyObject *) = empty_clear_weak_refs;
1485
1486
1487
Guido van Rossum86610361998-04-10 22:32:46 +00001488/* These methods are used to control infinite recursion in repr, str, print,
1489 etc. Container objects that may recursively contain themselves,
1490 e.g. builtin dictionaries and lists, should used Py_ReprEnter() and
1491 Py_ReprLeave() to avoid infinite recursion.
1492
1493 Py_ReprEnter() returns 0 the first time it is called for a particular
1494 object and 1 every time thereafter. It returns -1 if an exception
1495 occurred. Py_ReprLeave() has no return value.
1496
1497 See dictobject.c and listobject.c for examples of use.
1498*/
1499
1500#define KEY "Py_Repr"
1501
1502int
Fred Drake100814d2000-07-09 15:48:49 +00001503Py_ReprEnter(PyObject *obj)
Guido van Rossum86610361998-04-10 22:32:46 +00001504{
1505 PyObject *dict;
1506 PyObject *list;
1507 int i;
1508
1509 dict = PyThreadState_GetDict();
1510 if (dict == NULL)
1511 return -1;
1512 list = PyDict_GetItemString(dict, KEY);
1513 if (list == NULL) {
1514 list = PyList_New(0);
1515 if (list == NULL)
1516 return -1;
1517 if (PyDict_SetItemString(dict, KEY, list) < 0)
1518 return -1;
1519 Py_DECREF(list);
1520 }
1521 i = PyList_GET_SIZE(list);
1522 while (--i >= 0) {
1523 if (PyList_GET_ITEM(list, i) == obj)
1524 return 1;
1525 }
1526 PyList_Append(list, obj);
1527 return 0;
1528}
1529
1530void
Fred Drake100814d2000-07-09 15:48:49 +00001531Py_ReprLeave(PyObject *obj)
Guido van Rossum86610361998-04-10 22:32:46 +00001532{
1533 PyObject *dict;
1534 PyObject *list;
1535 int i;
1536
1537 dict = PyThreadState_GetDict();
Guido van Rossumeb909461998-04-11 15:17:34 +00001538 if (dict == NULL)
1539 return;
Guido van Rossum86610361998-04-10 22:32:46 +00001540 list = PyDict_GetItemString(dict, KEY);
Guido van Rossumeb909461998-04-11 15:17:34 +00001541 if (list == NULL || !PyList_Check(list))
1542 return;
Guido van Rossum86610361998-04-10 22:32:46 +00001543 i = PyList_GET_SIZE(list);
1544 /* Count backwards because we always expect obj to be list[-1] */
1545 while (--i >= 0) {
1546 if (PyList_GET_ITEM(list, i) == obj) {
1547 PyList_SetSlice(list, i, i + 1, NULL);
1548 break;
1549 }
1550 }
1551}
Guido van Rossumd724b232000-03-13 16:01:29 +00001552
1553/*
1554 trashcan
1555 CT 2k0130
1556 non-recursively destroy nested objects
1557
1558 CT 2k0223
1559 everything is now done in a macro.
1560
1561 CT 2k0305
1562 modified to use functions, after Tim Peter's suggestion.
1563
1564 CT 2k0309
1565 modified to restore a possible error.
Guido van Rossum13ff8eb2000-03-25 18:39:19 +00001566
1567 CT 2k0325
1568 added better safe than sorry check for threadstate
Guido van Rossume92e6102000-04-24 15:40:53 +00001569
1570 CT 2k0422
1571 complete rewrite. We now build a chain via ob_type
1572 and save the limited number of types in ob_refcnt.
1573 This is perfect since we don't need any memory.
1574 A patch for free-threading would need just a lock.
Guido van Rossumd724b232000-03-13 16:01:29 +00001575*/
1576
Guido van Rossume92e6102000-04-24 15:40:53 +00001577#define Py_TRASHCAN_TUPLE 1
1578#define Py_TRASHCAN_LIST 2
1579#define Py_TRASHCAN_DICT 3
1580#define Py_TRASHCAN_FRAME 4
1581#define Py_TRASHCAN_TRACEBACK 5
1582/* extend here if other objects want protection */
1583
Guido van Rossumd724b232000-03-13 16:01:29 +00001584int _PyTrash_delete_nesting = 0;
Guido van Rossume92e6102000-04-24 15:40:53 +00001585
Guido van Rossumd724b232000-03-13 16:01:29 +00001586PyObject * _PyTrash_delete_later = NULL;
1587
1588void
Fred Drake100814d2000-07-09 15:48:49 +00001589_PyTrash_deposit_object(PyObject *op)
Guido van Rossumd724b232000-03-13 16:01:29 +00001590{
Guido van Rossume92e6102000-04-24 15:40:53 +00001591 int typecode;
Guido van Rossum13ff8eb2000-03-25 18:39:19 +00001592
Guido van Rossume92e6102000-04-24 15:40:53 +00001593 if (PyTuple_Check(op))
1594 typecode = Py_TRASHCAN_TUPLE;
1595 else if (PyList_Check(op))
1596 typecode = Py_TRASHCAN_LIST;
1597 else if (PyDict_Check(op))
1598 typecode = Py_TRASHCAN_DICT;
1599 else if (PyFrame_Check(op))
1600 typecode = Py_TRASHCAN_FRAME;
1601 else if (PyTraceBack_Check(op))
1602 typecode = Py_TRASHCAN_TRACEBACK;
Moshe Zadkacf703f02000-08-04 15:36:13 +00001603 else /* We have a bug here -- those are the only types in GC */ {
1604 Py_FatalError("Type not supported in GC -- internal bug");
1605 return; /* pacify compiler -- execution never here */
1606 }
Guido van Rossume92e6102000-04-24 15:40:53 +00001607 op->ob_refcnt = typecode;
Guido van Rossumd724b232000-03-13 16:01:29 +00001608
Guido van Rossume92e6102000-04-24 15:40:53 +00001609 op->ob_type = (PyTypeObject*)_PyTrash_delete_later;
1610 _PyTrash_delete_later = op;
Guido van Rossumd724b232000-03-13 16:01:29 +00001611}
1612
1613void
Fred Drake100814d2000-07-09 15:48:49 +00001614_PyTrash_destroy_chain(void)
Guido van Rossumd724b232000-03-13 16:01:29 +00001615{
1616 while (_PyTrash_delete_later) {
1617 PyObject *shredder = _PyTrash_delete_later;
Guido van Rossume92e6102000-04-24 15:40:53 +00001618 _PyTrash_delete_later = (PyObject*) shredder->ob_type;
1619
1620 switch (shredder->ob_refcnt) {
1621 case Py_TRASHCAN_TUPLE:
1622 shredder->ob_type = &PyTuple_Type;
1623 break;
1624 case Py_TRASHCAN_LIST:
1625 shredder->ob_type = &PyList_Type;
1626 break;
1627 case Py_TRASHCAN_DICT:
1628 shredder->ob_type = &PyDict_Type;
1629 break;
1630 case Py_TRASHCAN_FRAME:
1631 shredder->ob_type = &PyFrame_Type;
1632 break;
1633 case Py_TRASHCAN_TRACEBACK:
1634 shredder->ob_type = &PyTraceBack_Type;
1635 break;
1636 }
1637 _Py_NewReference(shredder);
1638
Guido van Rossumd724b232000-03-13 16:01:29 +00001639 ++_PyTrash_delete_nesting;
1640 Py_DECREF(shredder);
1641 --_PyTrash_delete_nesting;
1642 }
1643}