blob: 47907bcb1d7f558ad6aa621f3303365bd61f209d [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 }
Marc-André Lemburgae605342001-03-25 19:16:13 +0000349 else if (PyString_Check(v)) {
350 Py_INCREF(v);
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000351 res = v;
Marc-André Lemburgae605342001-03-25 19:16:13 +0000352 }
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000353 else if (v->ob_type->tp_str != NULL)
354 res = (*v->ob_type->tp_str)(v);
355 else {
356 PyObject *func;
357 static PyObject *strstr;
358 if (strstr == NULL) {
359 strstr= PyString_InternFromString("__str__");
360 if (strstr == NULL)
361 return NULL;
362 }
363 if (!PyInstance_Check(v) ||
364 (func = PyObject_GetAttr(v, strstr)) == NULL) {
365 PyErr_Clear();
366 res = PyObject_Repr(v);
367 }
368 else {
369 res = PyEval_CallObject(func, (PyObject *)NULL);
370 Py_DECREF(func);
371 }
372 }
373 if (res == NULL)
374 return NULL;
375 if (!PyUnicode_Check(res)) {
376 PyObject* str;
377 str = PyUnicode_FromObject(res);
378 Py_DECREF(res);
379 if (str)
380 res = str;
381 else
382 return NULL;
383 }
384 return res;
385}
386
387
Guido van Rossumd1f06b92001-01-24 22:14:43 +0000388/* Macro to get the tp_richcompare field of a type if defined */
389#define RICHCOMPARE(t) (PyType_HasFeature((t), Py_TPFLAGS_HAVE_RICHCOMPARE) \
390 ? (t)->tp_richcompare : NULL)
391
Guido van Rossume797ec12001-01-17 15:24:28 +0000392/* Map rich comparison operators to their swapped version, e.g. LT --> GT */
393static int swapped_op[] = {Py_GT, Py_GE, Py_EQ, Py_NE, Py_LT, Py_LE};
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +0000394
Guido van Rossume797ec12001-01-17 15:24:28 +0000395/* Try a genuine rich comparison, returning an object. Return:
396 NULL for exception;
397 NotImplemented if this particular rich comparison is not implemented or
398 undefined;
399 some object not equal to NotImplemented if it is implemented
400 (this latter object may not be a Boolean).
401*/
402static PyObject *
403try_rich_compare(PyObject *v, PyObject *w, int op)
404{
405 richcmpfunc f;
406 PyObject *res;
407
Guido van Rossumd1f06b92001-01-24 22:14:43 +0000408 if ((f = RICHCOMPARE(v->ob_type)) != NULL) {
Guido van Rossume797ec12001-01-17 15:24:28 +0000409 res = (*f)(v, w, op);
410 if (res != Py_NotImplemented)
411 return res;
412 Py_DECREF(res);
413 }
Guido van Rossumd1f06b92001-01-24 22:14:43 +0000414 if ((f = RICHCOMPARE(w->ob_type)) != NULL) {
Guido van Rossume797ec12001-01-17 15:24:28 +0000415 return (*f)(w, v, swapped_op[op]);
416 }
417 res = Py_NotImplemented;
418 Py_INCREF(res);
419 return res;
420}
421
422/* Try a genuine rich comparison, returning an int. Return:
423 -1 for exception (including the case where try_rich_compare() returns an
424 object that's not a Boolean);
425 0 if the outcome is false;
426 1 if the outcome is true;
427 2 if this particular rich comparison is not implemented or undefined.
428*/
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +0000429static int
Guido van Rossume797ec12001-01-17 15:24:28 +0000430try_rich_compare_bool(PyObject *v, PyObject *w, int op)
431{
432 PyObject *res;
433 int ok;
434
Guido van Rossumd1f06b92001-01-24 22:14:43 +0000435 if (RICHCOMPARE(v->ob_type) == NULL && RICHCOMPARE(w->ob_type) == NULL)
Guido van Rossume797ec12001-01-17 15:24:28 +0000436 return 2; /* Shortcut, avoid INCREF+DECREF */
437 res = try_rich_compare(v, w, op);
438 if (res == NULL)
439 return -1;
440 if (res == Py_NotImplemented) {
441 Py_DECREF(res);
442 return 2;
443 }
444 ok = PyObject_IsTrue(res);
445 Py_DECREF(res);
446 return ok;
447}
448
449/* Try rich comparisons to determine a 3-way comparison. Return:
450 -2 for an exception;
451 -1 if v < w;
452 0 if v == w;
453 1 if v > w;
454 2 if this particular rich comparison is not implemented or undefined.
455*/
456static int
457try_rich_to_3way_compare(PyObject *v, PyObject *w)
458{
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000459 static struct { int op; int outcome; } tries[3] = {
460 /* Try this operator, and if it is true, use this outcome: */
461 {Py_EQ, 0},
462 {Py_LT, -1},
463 {Py_GT, 1},
464 };
465 int i;
466
Guido van Rossumd1f06b92001-01-24 22:14:43 +0000467 if (RICHCOMPARE(v->ob_type) == NULL && RICHCOMPARE(w->ob_type) == NULL)
Guido van Rossume797ec12001-01-17 15:24:28 +0000468 return 2; /* Shortcut */
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000469
470 for (i = 0; i < 3; i++) {
471 switch (try_rich_compare_bool(v, w, tries[i].op)) {
472 case -1:
473 return -1;
474 case 1:
475 return tries[i].outcome;
476 }
Guido van Rossume797ec12001-01-17 15:24:28 +0000477 }
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000478
479 return 2;
Guido van Rossume797ec12001-01-17 15:24:28 +0000480}
481
482/* Try a 3-way comparison, returning an int. Return:
483 -2 for an exception;
484 -1 if v < w;
485 0 if v == w;
486 1 if v > w;
487 2 if this particular 3-way comparison is not implemented or undefined.
488*/
489static int
490try_3way_compare(PyObject *v, PyObject *w)
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +0000491{
492 int c;
Guido van Rossume797ec12001-01-17 15:24:28 +0000493 cmpfunc f;
494
495 /* Comparisons involving instances are given to instance_compare,
496 which has the same return conventions as this function. */
497
498 if (PyInstance_Check(v))
499 return (*v->ob_type->tp_compare)(v, w);
500 if (PyInstance_Check(w))
501 return (*w->ob_type->tp_compare)(v, w);
502
503 /* If the types are equal, don't bother with coercions etc. */
504 if (v->ob_type == w->ob_type) {
505 if ((f = v->ob_type->tp_compare) == NULL)
506 return 2;
507 c = (*f)(v, w);
508 if (PyErr_Occurred())
509 return -2;
510 return c < 0 ? -1 : c > 0 ? 1 : 0;
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +0000511 }
Guido van Rossume797ec12001-01-17 15:24:28 +0000512
513 /* Try coercion; if it fails, give up */
514 c = PyNumber_CoerceEx(&v, &w);
515 if (c < 0)
516 return -2;
517 if (c > 0)
518 return 2;
519
520 /* Try v's comparison, if defined */
521 if ((f = v->ob_type->tp_compare) != NULL) {
522 c = (*f)(v, w);
523 Py_DECREF(v);
524 Py_DECREF(w);
525 if (PyErr_Occurred())
526 return -2;
527 return c < 0 ? -1 : c > 0 ? 1 : 0;
528 }
529
530 /* Try w's comparison, if defined */
531 if ((f = w->ob_type->tp_compare) != NULL) {
532 c = (*f)(w, v); /* swapped! */
533 Py_DECREF(v);
534 Py_DECREF(w);
535 if (PyErr_Occurred())
536 return -2;
537 return c < 0 ? 1 : c > 0 ? -1 : 0; /* negated! */
538 }
539
540 /* No comparison defined */
541 Py_DECREF(v);
542 Py_DECREF(w);
543 return 2;
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +0000544}
545
Guido van Rossume797ec12001-01-17 15:24:28 +0000546/* Final fallback 3-way comparison, returning an int. Return:
547 -2 if an error occurred;
548 -1 if v < w;
549 0 if v == w;
550 1 if v > w.
551*/
552static int
553default_3way_compare(PyObject *v, PyObject *w)
554{
555 int c;
Guido van Rossum8f9143d2001-01-22 15:59:32 +0000556 char *vname, *wname;
Guido van Rossume797ec12001-01-17 15:24:28 +0000557
558 if (v->ob_type == w->ob_type) {
Barry Warsawb0e754d2001-01-20 06:24:55 +0000559 /* When comparing these pointers, they must be cast to
560 * integer types (i.e. Py_uintptr_t, our spelling of C9X's
561 * uintptr_t). ANSI specifies that pointer compares other
562 * than == and != to non-related structures are undefined.
563 */
Barry Warsaw71ff8d52001-01-20 06:08:10 +0000564 Py_uintptr_t vv = (Py_uintptr_t)v;
565 Py_uintptr_t ww = (Py_uintptr_t)w;
Guido van Rossume797ec12001-01-17 15:24:28 +0000566 return (vv < ww) ? -1 : (vv > ww) ? 1 : 0;
567 }
568
569 /* Special case for Unicode */
570 if (PyUnicode_Check(v) || PyUnicode_Check(w)) {
571 c = PyUnicode_Compare(v, w);
572 if (!PyErr_Occurred())
573 return c;
574 /* TypeErrors are ignored: if Unicode coercion fails due
575 to one of the arguments not having the right type, we
576 continue as defined by the coercion protocol (see
577 above). Luckily, decoding errors are reported as
578 ValueErrors and are not masked by this technique. */
579 if (!PyErr_ExceptionMatches(PyExc_TypeError))
580 return -2;
581 PyErr_Clear();
582 }
583
Guido van Rossum0871e932001-01-22 19:28:09 +0000584 /* None is smaller than anything */
585 if (v == Py_None)
586 return -1;
587 if (w == Py_None)
588 return 1;
589
Guido van Rossume797ec12001-01-17 15:24:28 +0000590 /* different type: compare type names */
Guido van Rossum8f9143d2001-01-22 15:59:32 +0000591 if (v->ob_type->tp_as_number)
592 vname = "";
593 else
594 vname = v->ob_type->tp_name;
595 if (w->ob_type->tp_as_number)
596 wname = "";
597 else
598 wname = w->ob_type->tp_name;
599 c = strcmp(vname, wname);
600 if (c < 0)
601 return -1;
602 if (c > 0)
603 return 1;
604 /* Same type name, or (more likely) incomparable numeric types */
605 return ((Py_uintptr_t)(v->ob_type) < (
606 Py_uintptr_t)(w->ob_type)) ? -1 : 1;
Guido van Rossume797ec12001-01-17 15:24:28 +0000607}
608
609#define CHECK_TYPES(o) PyType_HasFeature((o)->ob_type, Py_TPFLAGS_CHECKTYPES)
610
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +0000611static int
Fred Drake100814d2000-07-09 15:48:49 +0000612do_cmp(PyObject *v, PyObject *w)
Guido van Rossum20566841995-01-12 11:26:10 +0000613{
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +0000614 int c;
615
Guido van Rossume797ec12001-01-17 15:24:28 +0000616 c = try_rich_to_3way_compare(v, w);
617 if (c < 2)
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +0000618 return c;
Guido van Rossume797ec12001-01-17 15:24:28 +0000619 c = try_3way_compare(v, w);
620 if (c < 2)
621 return c;
622 return default_3way_compare(v, w);
Guido van Rossum20566841995-01-12 11:26:10 +0000623}
624
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000625/* compare_nesting is incremented before calling compare (for
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000626 some types) and decremented on exit. If the count exceeds the
627 nesting limit, enable code to detect circular data structures.
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000628
629 This is a tunable parameter that should only affect the performance
630 of comparisons, nothing else. Setting it high makes comparing deeply
631 nested non-cyclical data structures faster, but makes comparing cyclical
632 data structures slower.
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000633*/
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000634#define NESTING_LIMIT 20
635
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000636static int compare_nesting = 0;
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000637
638static PyObject*
Fred Drake100814d2000-07-09 15:48:49 +0000639get_inprogress_dict(void)
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000640{
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000641 static PyObject *key;
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000642 PyObject *tstate_dict, *inprogress;
643
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000644 if (key == NULL) {
645 key = PyString_InternFromString("cmp_state");
646 if (key == NULL)
647 return NULL;
648 }
649
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000650 tstate_dict = PyThreadState_GetDict();
651 if (tstate_dict == NULL) {
652 PyErr_BadInternalCall();
653 return NULL;
654 }
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000655
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000656 inprogress = PyDict_GetItem(tstate_dict, key);
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000657 if (inprogress == NULL) {
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000658 inprogress = PyDict_New();
659 if (inprogress == NULL)
660 return NULL;
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000661 if (PyDict_SetItem(tstate_dict, key, inprogress) == -1) {
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000662 Py_DECREF(inprogress);
663 return NULL;
664 }
Jeremy Hyltona251ea02000-06-09 16:20:39 +0000665 Py_DECREF(inprogress);
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000666 }
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000667
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000668 return inprogress;
669}
670
671static PyObject *
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000672check_recursion(PyObject *v, PyObject *w, int op)
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000673{
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000674 PyObject *inprogress;
675 PyObject *token;
Barry Warsaw9d23a4e2000-08-18 05:01:19 +0000676 Py_uintptr_t iv = (Py_uintptr_t)v;
677 Py_uintptr_t iw = (Py_uintptr_t)w;
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000678 PyObject *x, *y, *z;
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000679
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000680 inprogress = get_inprogress_dict();
681 if (inprogress == NULL)
682 return NULL;
683
684 token = PyTuple_New(3);
685 if (token == NULL)
686 return NULL;
687
688 if (iv <= iw) {
689 PyTuple_SET_ITEM(token, 0, x = PyLong_FromVoidPtr((void *)v));
690 PyTuple_SET_ITEM(token, 1, y = PyLong_FromVoidPtr((void *)w));
691 if (op >= 0)
692 op = swapped_op[op];
693 } else {
694 PyTuple_SET_ITEM(token, 0, x = PyLong_FromVoidPtr((void *)w));
695 PyTuple_SET_ITEM(token, 1, y = PyLong_FromVoidPtr((void *)v));
696 }
697 PyTuple_SET_ITEM(token, 2, z = PyInt_FromLong((long)op));
698 if (x == NULL || y == NULL || z == NULL) {
699 Py_DECREF(token);
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000700 return NULL;
701 }
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000702
703 if (PyDict_GetItem(inprogress, token) != NULL) {
704 Py_DECREF(token);
705 return Py_None; /* Without INCREF! */
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000706 }
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000707
708 if (PyDict_SetItem(inprogress, token, token) < 0) {
709 Py_DECREF(token);
710 return NULL;
711 }
712
713 return token;
714}
715
716static void
717delete_token(PyObject *token)
718{
719 PyObject *inprogress;
720
721 if (token == NULL || token == Py_None)
722 return;
723 inprogress = get_inprogress_dict();
724 if (inprogress == NULL)
725 PyErr_Clear();
726 else
727 PyDict_DelItem(inprogress, token);
728 Py_DECREF(token);
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000729}
730
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000731int
Fred Drake100814d2000-07-09 15:48:49 +0000732PyObject_Compare(PyObject *v, PyObject *w)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000733{
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000734 PyTypeObject *vtp;
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000735 int result;
736
Jack Jansend49cbe12000-08-22 21:52:51 +0000737#if defined(USE_STACKCHECK)
Guido van Rossum9c0a99e2000-08-30 15:53:50 +0000738 if (PyOS_CheckStack()) {
Jack Jansend49cbe12000-08-22 21:52:51 +0000739 PyErr_SetString(PyExc_MemoryError, "Stack overflow");
740 return -1;
741 }
742#endif
Guido van Rossumc8b6df91997-05-23 00:06:51 +0000743 if (v == NULL || w == NULL) {
744 PyErr_BadInternalCall();
745 return -1;
746 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000747 if (v == w)
748 return 0;
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +0000749 vtp = v->ob_type;
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000750 compare_nesting++;
751 if (compare_nesting > NESTING_LIMIT &&
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +0000752 (vtp->tp_as_mapping
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000753 || (vtp->tp_as_sequence
754 && !PyString_Check(v)
755 && !PyTuple_Check(v)))) {
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +0000756 /* try to detect circular data structures */
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000757 PyObject *token = check_recursion(v, w, -1);
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000758
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000759 if (token == NULL) {
760 result = -1;
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000761 }
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000762 else if (token == Py_None) {
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000763 /* already comparing these objects. assume
764 they're equal until shown otherwise */
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +0000765 result = 0;
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000766 }
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000767 else {
768 result = do_cmp(v, w);
769 delete_token(token);
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000770 }
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000771 }
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +0000772 else {
773 result = do_cmp(v, w);
774 }
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000775 compare_nesting--;
Guido van Rossume797ec12001-01-17 15:24:28 +0000776 return result < 0 ? -1 : result;
777}
778
779static PyObject *
780try_3way_to_rich_compare(PyObject *v, PyObject *w, int op)
781{
782 int c;
783 PyObject *result;
784
785 c = try_3way_compare(v, w);
Guido van Rossume797ec12001-01-17 15:24:28 +0000786 if (c >= 2)
787 c = default_3way_compare(v, w);
Guido van Rossum2da0ea82001-02-22 22:18:04 +0000788 if (c <= -2)
789 return NULL;
Guido van Rossume797ec12001-01-17 15:24:28 +0000790 switch (op) {
791 case Py_LT: c = c < 0; break;
792 case Py_LE: c = c <= 0; break;
793 case Py_EQ: c = c == 0; break;
794 case Py_NE: c = c != 0; break;
795 case Py_GT: c = c > 0; break;
796 case Py_GE: c = c >= 0; break;
797 }
798 result = c ? Py_True : Py_False;
799 Py_INCREF(result);
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000800 return result;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000801}
802
Neil Schemenauerd38855c2001-01-21 16:25:18 +0000803static PyObject *
Guido van Rossume797ec12001-01-17 15:24:28 +0000804do_richcmp(PyObject *v, PyObject *w, int op)
805{
806 PyObject *res;
807
808 res = try_rich_compare(v, w, op);
809 if (res != Py_NotImplemented)
810 return res;
811 Py_DECREF(res);
812
813 return try_3way_to_rich_compare(v, w, op);
814}
815
816PyObject *
817PyObject_RichCompare(PyObject *v, PyObject *w, int op)
818{
819 PyObject *res;
820
821 assert(Py_LT <= op && op <= Py_GE);
822
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000823 compare_nesting++;
824 if (compare_nesting > NESTING_LIMIT &&
825 (v->ob_type->tp_as_mapping
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000826 || (v->ob_type->tp_as_sequence
827 && !PyString_Check(v)
828 && !PyTuple_Check(v)))) {
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000829 /* try to detect circular data structures */
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000830 PyObject *token = check_recursion(v, w, op);
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000831
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000832 if (token == NULL) {
833 res = NULL;
Guido van Rossume797ec12001-01-17 15:24:28 +0000834 }
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000835 else if (token == Py_None) {
836 /* already comparing these objects with this operator.
837 assume they're equal until shown otherwise */
838 if (op == Py_EQ)
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000839 res = Py_True;
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000840 else if (op == Py_NE)
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000841 res = Py_False;
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000842 else {
843 PyErr_SetString(PyExc_ValueError,
844 "can't order recursive values");
845 res = NULL;
846 }
847 Py_XINCREF(res);
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000848 }
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000849 else {
850 res = do_richcmp(v, w, op);
851 delete_token(token);
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000852 }
Guido van Rossume797ec12001-01-17 15:24:28 +0000853 }
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000854 else {
855 res = do_richcmp(v, w, op);
856 }
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000857 compare_nesting--;
Guido van Rossume797ec12001-01-17 15:24:28 +0000858 return res;
859}
860
861int
862PyObject_RichCompareBool(PyObject *v, PyObject *w, int op)
863{
864 PyObject *res = PyObject_RichCompare(v, w, op);
865 int ok;
866
867 if (res == NULL)
868 return -1;
869 ok = PyObject_IsTrue(res);
870 Py_DECREF(res);
871 return ok;
872}
Fred Drake13634cf2000-06-29 19:17:04 +0000873
874/* Set of hash utility functions to help maintaining the invariant that
875 iff a==b then hash(a)==hash(b)
876
877 All the utility functions (_Py_Hash*()) return "-1" to signify an error.
878*/
879
880long
Fred Drake100814d2000-07-09 15:48:49 +0000881_Py_HashDouble(double v)
Fred Drake13634cf2000-06-29 19:17:04 +0000882{
Tim Peters39dce292000-08-15 03:34:48 +0000883 double intpart, fractpart;
884 int expo;
885 long hipart;
886 long x; /* the final hash value */
887 /* This is designed so that Python numbers of different types
888 * that compare equal hash to the same value; otherwise comparisons
889 * of mapping keys will turn out weird.
890 */
891
892#ifdef MPW /* MPW C modf expects pointer to extended as second argument */
893{
894 extended e;
895 fractpart = modf(v, &e);
896 intpart = e;
897}
898#else
899 fractpart = modf(v, &intpart);
900#endif
901 if (fractpart == 0.0) {
902 /* This must return the same hash as an equal int or long. */
903 if (intpart > LONG_MAX || -intpart > LONG_MAX) {
904 /* Convert to long and use its hash. */
905 PyObject *plong; /* converted to Python long */
906 if (Py_IS_INFINITY(intpart))
907 /* can't convert to long int -- arbitrary */
908 v = v < 0 ? -271828.0 : 314159.0;
909 plong = PyLong_FromDouble(v);
910 if (plong == NULL)
911 return -1;
912 x = PyObject_Hash(plong);
913 Py_DECREF(plong);
914 return x;
915 }
916 /* Fits in a C long == a Python int, so is its own hash. */
917 x = (long)intpart;
918 if (x == -1)
919 x = -2;
920 return x;
921 }
922 /* The fractional part is non-zero, so we don't have to worry about
923 * making this match the hash of some other type.
924 * Use frexp to get at the bits in the double.
Fred Drake13634cf2000-06-29 19:17:04 +0000925 * Since the VAX D double format has 56 mantissa bits, which is the
926 * most of any double format in use, each of these parts may have as
927 * many as (but no more than) 56 significant bits.
Tim Peters39dce292000-08-15 03:34:48 +0000928 * So, assuming sizeof(long) >= 4, each part can be broken into two
929 * longs; frexp and multiplication are used to do that.
930 * Also, since the Cray double format has 15 exponent bits, which is
931 * the most of any double format in use, shifting the exponent field
932 * left by 15 won't overflow a long (again assuming sizeof(long) >= 4).
Fred Drake13634cf2000-06-29 19:17:04 +0000933 */
Tim Peters39dce292000-08-15 03:34:48 +0000934 v = frexp(v, &expo);
935 v *= 2147483648.0; /* 2**31 */
936 hipart = (long)v; /* take the top 32 bits */
937 v = (v - (double)hipart) * 2147483648.0; /* get the next 32 bits */
938 x = hipart + (long)v + (expo << 15);
939 if (x == -1)
940 x = -2;
941 return x;
Fred Drake13634cf2000-06-29 19:17:04 +0000942}
943
944long
Fred Drake100814d2000-07-09 15:48:49 +0000945_Py_HashPointer(void *p)
Fred Drake13634cf2000-06-29 19:17:04 +0000946{
947#if SIZEOF_LONG >= SIZEOF_VOID_P
948 return (long)p;
949#else
950 /* convert to a Python long and hash that */
951 PyObject* longobj;
952 long x;
953
954 if ((longobj = PyLong_FromVoidPtr(p)) == NULL) {
955 x = -1;
956 goto finally;
957 }
958 x = PyObject_Hash(longobj);
959
960finally:
961 Py_XDECREF(longobj);
962 return x;
963#endif
964}
965
966
Guido van Rossum9bfef441993-03-29 10:43:31 +0000967long
Fred Drake100814d2000-07-09 15:48:49 +0000968PyObject_Hash(PyObject *v)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000969{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000970 PyTypeObject *tp = v->ob_type;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000971 if (tp->tp_hash != NULL)
972 return (*tp->tp_hash)(v);
Guido van Rossumd1f06b92001-01-24 22:14:43 +0000973 if (tp->tp_compare == NULL && RICHCOMPARE(tp) == NULL) {
Fred Drake13634cf2000-06-29 19:17:04 +0000974 return _Py_HashPointer(v); /* Use address as hash value */
975 }
Guido van Rossum9bfef441993-03-29 10:43:31 +0000976 /* If there's a cmp but no hash defined, the object can't be hashed */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000977 PyErr_SetString(PyExc_TypeError, "unhashable type");
Guido van Rossum9bfef441993-03-29 10:43:31 +0000978 return -1;
979}
980
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000981PyObject *
Fred Drake100814d2000-07-09 15:48:49 +0000982PyObject_GetAttrString(PyObject *v, char *name)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000983{
Guido van Rossumd8eb1b31996-08-09 20:52:03 +0000984 if (v->ob_type->tp_getattro != NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000985 PyObject *w, *res;
Guido van Rossumc6d06701997-01-18 07:57:16 +0000986 w = PyString_InternFromString(name);
Guido van Rossumd8eb1b31996-08-09 20:52:03 +0000987 if (w == NULL)
988 return NULL;
989 res = (*v->ob_type->tp_getattro)(v, w);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000990 Py_XDECREF(w);
Guido van Rossumd8eb1b31996-08-09 20:52:03 +0000991 return res;
992 }
993
Guido van Rossum3f5da241990-12-20 15:06:42 +0000994 if (v->ob_type->tp_getattr == NULL) {
Guido van Rossumdb935161998-01-19 22:16:36 +0000995 PyErr_Format(PyExc_AttributeError,
996 "'%.50s' object has no attribute '%.400s'",
997 v->ob_type->tp_name,
998 name);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000999 return NULL;
1000 }
1001 else {
1002 return (*v->ob_type->tp_getattr)(v, name);
1003 }
1004}
1005
1006int
Fred Drake100814d2000-07-09 15:48:49 +00001007PyObject_HasAttrString(PyObject *v, char *name)
Guido van Rossumed18fdc1993-07-11 19:55:34 +00001008{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001009 PyObject *res = PyObject_GetAttrString(v, name);
Guido van Rossumed18fdc1993-07-11 19:55:34 +00001010 if (res != NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001011 Py_DECREF(res);
Guido van Rossumed18fdc1993-07-11 19:55:34 +00001012 return 1;
1013 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001014 PyErr_Clear();
Guido van Rossumed18fdc1993-07-11 19:55:34 +00001015 return 0;
1016}
1017
1018int
Fred Drake100814d2000-07-09 15:48:49 +00001019PyObject_SetAttrString(PyObject *v, char *name, PyObject *w)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001020{
Guido van Rossumd8eb1b31996-08-09 20:52:03 +00001021 if (v->ob_type->tp_setattro != NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001022 PyObject *s;
Guido van Rossumd8eb1b31996-08-09 20:52:03 +00001023 int res;
Guido van Rossumc6d06701997-01-18 07:57:16 +00001024 s = PyString_InternFromString(name);
Guido van Rossumd8eb1b31996-08-09 20:52:03 +00001025 if (s == NULL)
Guido van Rossumb7fc3041996-09-11 22:51:25 +00001026 return -1;
Guido van Rossumd8eb1b31996-08-09 20:52:03 +00001027 res = (*v->ob_type->tp_setattro)(v, s, w);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001028 Py_XDECREF(s);
Guido van Rossumd8eb1b31996-08-09 20:52:03 +00001029 return res;
1030 }
1031
Guido van Rossum3f5da241990-12-20 15:06:42 +00001032 if (v->ob_type->tp_setattr == NULL) {
1033 if (v->ob_type->tp_getattr == NULL)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001034 PyErr_SetString(PyExc_TypeError,
Guido van Rossum3ea74121991-12-24 13:28:03 +00001035 "attribute-less object (assign or del)");
Guido van Rossum3f5da241990-12-20 15:06:42 +00001036 else
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001037 PyErr_SetString(PyExc_TypeError,
Guido van Rossum3ea74121991-12-24 13:28:03 +00001038 "object has read-only attributes");
Guido van Rossum73531a31990-12-20 23:12:40 +00001039 return -1;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001040 }
1041 else {
1042 return (*v->ob_type->tp_setattr)(v, name, w);
1043 }
1044}
1045
Marc-André Lemburge44e5072000-09-18 16:20:57 +00001046/* Internal API needed by PyObject_GetAttr(): */
1047extern
1048PyObject *_PyUnicode_AsDefaultEncodedString(PyObject *unicode,
1049 const char *errors);
1050
Guido van Rossum98ff96a1997-05-20 18:34:44 +00001051PyObject *
Fred Drake100814d2000-07-09 15:48:49 +00001052PyObject_GetAttr(PyObject *v, PyObject *name)
Guido van Rossum98ff96a1997-05-20 18:34:44 +00001053{
Marc-André Lemburge44e5072000-09-18 16:20:57 +00001054 /* The Unicode to string conversion is done here because the
1055 existing tp_getattro slots expect a string object as name
1056 and we wouldn't want to break those. */
1057 if (PyUnicode_Check(name)) {
1058 name = _PyUnicode_AsDefaultEncodedString(name, NULL);
1059 if (name == NULL)
1060 return NULL;
1061 }
Jeremy Hylton99a8f902000-06-23 14:36:32 +00001062
1063 if (!PyString_Check(name)) {
1064 PyErr_SetString(PyExc_TypeError,
1065 "attribute name must be string");
1066 return NULL;
1067 }
Marc-André Lemburge44e5072000-09-18 16:20:57 +00001068 if (v->ob_type->tp_getattro != NULL)
1069 return (*v->ob_type->tp_getattro)(v, name);
1070 else
Jeremy Hylton99a8f902000-06-23 14:36:32 +00001071 return PyObject_GetAttrString(v, PyString_AS_STRING(name));
Guido van Rossum98ff96a1997-05-20 18:34:44 +00001072}
1073
1074int
Fred Drake100814d2000-07-09 15:48:49 +00001075PyObject_HasAttr(PyObject *v, PyObject *name)
Guido van Rossum98ff96a1997-05-20 18:34:44 +00001076{
1077 PyObject *res = PyObject_GetAttr(v, name);
1078 if (res != NULL) {
1079 Py_DECREF(res);
1080 return 1;
1081 }
1082 PyErr_Clear();
1083 return 0;
1084}
1085
1086int
Fred Drake100814d2000-07-09 15:48:49 +00001087PyObject_SetAttr(PyObject *v, PyObject *name, PyObject *value)
Guido van Rossum98ff96a1997-05-20 18:34:44 +00001088{
1089 int err;
Marc-André Lemburge44e5072000-09-18 16:20:57 +00001090
1091 /* The Unicode to string conversion is done here because the
1092 existing tp_setattro slots expect a string object as name
1093 and we wouldn't want to break those. */
1094 if (PyUnicode_Check(name)) {
1095 name = PyUnicode_AsEncodedString(name, NULL, NULL);
1096 if (name == NULL)
1097 return -1;
Jeremy Hylton99a8f902000-06-23 14:36:32 +00001098 }
Marc-André Lemburge44e5072000-09-18 16:20:57 +00001099 else
1100 Py_INCREF(name);
1101
1102 if (!PyString_Check(name)){
Jeremy Hylton99a8f902000-06-23 14:36:32 +00001103 PyErr_SetString(PyExc_TypeError,
1104 "attribute name must be string");
1105 err = -1;
1106 }
Marc-André Lemburge44e5072000-09-18 16:20:57 +00001107 else {
1108 PyString_InternInPlace(&name);
1109 if (v->ob_type->tp_setattro != NULL)
1110 err = (*v->ob_type->tp_setattro)(v, name, value);
1111 else
1112 err = PyObject_SetAttrString(v,
1113 PyString_AS_STRING(name), value);
1114 }
1115
Guido van Rossum98ff96a1997-05-20 18:34:44 +00001116 Py_DECREF(name);
1117 return err;
1118}
1119
Guido van Rossum6ac258d1993-05-12 08:24:20 +00001120/* Test a value used as condition, e.g., in a for or if statement.
1121 Return -1 if an error occurred */
1122
1123int
Fred Drake100814d2000-07-09 15:48:49 +00001124PyObject_IsTrue(PyObject *v)
Guido van Rossum6ac258d1993-05-12 08:24:20 +00001125{
1126 int res;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001127 if (v == Py_None)
Guido van Rossum6ac258d1993-05-12 08:24:20 +00001128 res = 0;
Guido van Rossum1c4f4581998-05-22 00:53:24 +00001129 else if (v->ob_type->tp_as_number != NULL &&
1130 v->ob_type->tp_as_number->nb_nonzero != NULL)
Guido van Rossum6ac258d1993-05-12 08:24:20 +00001131 res = (*v->ob_type->tp_as_number->nb_nonzero)(v);
Guido van Rossum1c4f4581998-05-22 00:53:24 +00001132 else if (v->ob_type->tp_as_mapping != NULL &&
1133 v->ob_type->tp_as_mapping->mp_length != NULL)
Guido van Rossum6ac258d1993-05-12 08:24:20 +00001134 res = (*v->ob_type->tp_as_mapping->mp_length)(v);
Guido van Rossum1c4f4581998-05-22 00:53:24 +00001135 else if (v->ob_type->tp_as_sequence != NULL &&
1136 v->ob_type->tp_as_sequence->sq_length != NULL)
Guido van Rossum6ac258d1993-05-12 08:24:20 +00001137 res = (*v->ob_type->tp_as_sequence->sq_length)(v);
1138 else
1139 res = 1;
1140 if (res > 0)
1141 res = 1;
1142 return res;
1143}
1144
Guido van Rossumc3d3f961998-04-09 17:53:59 +00001145/* equivalent of 'not v'
1146 Return -1 if an error occurred */
1147
1148int
Fred Drake100814d2000-07-09 15:48:49 +00001149PyObject_Not(PyObject *v)
Guido van Rossumc3d3f961998-04-09 17:53:59 +00001150{
1151 int res;
1152 res = PyObject_IsTrue(v);
1153 if (res < 0)
1154 return res;
1155 return res == 0;
1156}
1157
Guido van Rossum5524a591995-01-10 15:26:20 +00001158/* Coerce two numeric types to the "larger" one.
1159 Increment the reference count on each argument.
Guido van Rossume797ec12001-01-17 15:24:28 +00001160 Return value:
1161 -1 if an error occurred;
1162 0 if the coercion succeeded (and then the reference counts are increased);
1163 1 if no coercion is possible (and no error is raised).
Guido van Rossum5524a591995-01-10 15:26:20 +00001164*/
Guido van Rossum5524a591995-01-10 15:26:20 +00001165int
Fred Drake100814d2000-07-09 15:48:49 +00001166PyNumber_CoerceEx(PyObject **pv, PyObject **pw)
Guido van Rossum5524a591995-01-10 15:26:20 +00001167{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001168 register PyObject *v = *pv;
1169 register PyObject *w = *pw;
Guido van Rossum5524a591995-01-10 15:26:20 +00001170 int res;
1171
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001172 if (v->ob_type == w->ob_type && !PyInstance_Check(v)) {
1173 Py_INCREF(v);
1174 Py_INCREF(w);
Guido van Rossum5524a591995-01-10 15:26:20 +00001175 return 0;
1176 }
1177 if (v->ob_type->tp_as_number && v->ob_type->tp_as_number->nb_coerce) {
1178 res = (*v->ob_type->tp_as_number->nb_coerce)(pv, pw);
1179 if (res <= 0)
1180 return res;
1181 }
1182 if (w->ob_type->tp_as_number && w->ob_type->tp_as_number->nb_coerce) {
1183 res = (*w->ob_type->tp_as_number->nb_coerce)(pw, pv);
1184 if (res <= 0)
1185 return res;
1186 }
Guido van Rossum242c6421997-11-19 16:03:17 +00001187 return 1;
1188}
1189
Guido van Rossume797ec12001-01-17 15:24:28 +00001190/* Coerce two numeric types to the "larger" one.
1191 Increment the reference count on each argument.
1192 Return -1 and raise an exception if no coercion is possible
1193 (and then no reference count is incremented).
1194*/
Guido van Rossum242c6421997-11-19 16:03:17 +00001195int
Fred Drake100814d2000-07-09 15:48:49 +00001196PyNumber_Coerce(PyObject **pv, PyObject **pw)
Guido van Rossum242c6421997-11-19 16:03:17 +00001197{
1198 int err = PyNumber_CoerceEx(pv, pw);
1199 if (err <= 0)
1200 return err;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001201 PyErr_SetString(PyExc_TypeError, "number coercion failed");
Guido van Rossum5524a591995-01-10 15:26:20 +00001202 return -1;
1203}
1204
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001205
Guido van Rossum49b11fe1995-01-26 00:38:22 +00001206/* Test whether an object can be called */
1207
1208int
Fred Drake100814d2000-07-09 15:48:49 +00001209PyCallable_Check(PyObject *x)
Guido van Rossum49b11fe1995-01-26 00:38:22 +00001210{
1211 if (x == NULL)
1212 return 0;
1213 if (x->ob_type->tp_call != NULL ||
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001214 PyFunction_Check(x) ||
1215 PyMethod_Check(x) ||
1216 PyCFunction_Check(x) ||
1217 PyClass_Check(x))
Guido van Rossum49b11fe1995-01-26 00:38:22 +00001218 return 1;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001219 if (PyInstance_Check(x)) {
1220 PyObject *call = PyObject_GetAttrString(x, "__call__");
Guido van Rossum49b11fe1995-01-26 00:38:22 +00001221 if (call == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001222 PyErr_Clear();
Guido van Rossum49b11fe1995-01-26 00:38:22 +00001223 return 0;
1224 }
1225 /* Could test recursively but don't, for fear of endless
1226 recursion if some joker sets self.__call__ = self */
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001227 Py_DECREF(call);
Guido van Rossum49b11fe1995-01-26 00:38:22 +00001228 return 1;
1229 }
1230 return 0;
1231}
1232
1233
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001234/*
1235NoObject is usable as a non-NULL undefined value, used by the macro None.
1236There is (and should be!) no way to create other objects of this type,
Guido van Rossum3f5da241990-12-20 15:06:42 +00001237so there is exactly one (which is indestructible, by the way).
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001238*/
1239
Guido van Rossum0c182a11992-03-27 17:26:13 +00001240/* ARGSUSED */
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001241static PyObject *
Fred Drake100814d2000-07-09 15:48:49 +00001242none_repr(PyObject *op)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001243{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001244 return PyString_FromString("None");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001245}
1246
Barry Warsaw9bf16442001-01-23 16:24:35 +00001247/* ARGUSED */
1248static void
1249none_dealloc(PyObject* ignore)
1250{
1251 /* This should never get called, but we also don't want to SEGV if
1252 * we accidently decref None out of existance.
1253 */
1254 abort();
1255}
1256
1257
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001258static PyTypeObject PyNothing_Type = {
1259 PyObject_HEAD_INIT(&PyType_Type)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001260 0,
Guido van Rossum3f5da241990-12-20 15:06:42 +00001261 "None",
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001262 0,
1263 0,
Barry Warsaw9bf16442001-01-23 16:24:35 +00001264 (destructor)none_dealloc, /*tp_dealloc*/ /*never called*/
Guido van Rossum7066dd71992-09-17 17:54:56 +00001265 0, /*tp_print*/
Guido van Rossum3f5da241990-12-20 15:06:42 +00001266 0, /*tp_getattr*/
1267 0, /*tp_setattr*/
1268 0, /*tp_compare*/
Guido van Rossum1d5735e1994-08-30 08:27:36 +00001269 (reprfunc)none_repr, /*tp_repr*/
Guido van Rossum3f5da241990-12-20 15:06:42 +00001270 0, /*tp_as_number*/
1271 0, /*tp_as_sequence*/
1272 0, /*tp_as_mapping*/
Guido van Rossum9bfef441993-03-29 10:43:31 +00001273 0, /*tp_hash */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001274};
1275
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001276PyObject _Py_NoneStruct = {
1277 PyObject_HEAD_INIT(&PyNothing_Type)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001278};
1279
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +00001280/* NotImplemented is an object that can be used to signal that an
1281 operation is not implemented for the given type combination. */
1282
1283static PyObject *
1284NotImplemented_repr(PyObject *op)
1285{
1286 return PyString_FromString("NotImplemented");
1287}
1288
1289static PyTypeObject PyNotImplemented_Type = {
1290 PyObject_HEAD_INIT(&PyType_Type)
1291 0,
1292 "NotImplemented",
1293 0,
1294 0,
Barry Warsaw9bf16442001-01-23 16:24:35 +00001295 (destructor)none_dealloc, /*tp_dealloc*/ /*never called*/
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +00001296 0, /*tp_print*/
1297 0, /*tp_getattr*/
1298 0, /*tp_setattr*/
1299 0, /*tp_compare*/
1300 (reprfunc)NotImplemented_repr, /*tp_repr*/
1301 0, /*tp_as_number*/
1302 0, /*tp_as_sequence*/
1303 0, /*tp_as_mapping*/
1304 0, /*tp_hash */
1305};
1306
1307PyObject _Py_NotImplementedStruct = {
1308 PyObject_HEAD_INIT(&PyNotImplemented_Type)
1309};
1310
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001311
Guido van Rossum84a90321996-05-22 16:34:47 +00001312#ifdef Py_TRACE_REFS
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001313
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001314static PyObject refchain = {&refchain, &refchain};
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001315
Guido van Rossumaacdc9d1996-08-12 21:32:12 +00001316void
Fred Drake100814d2000-07-09 15:48:49 +00001317_Py_ResetReferences(void)
Guido van Rossume09fb551997-08-05 02:04:34 +00001318{
1319 refchain._ob_prev = refchain._ob_next = &refchain;
1320 _Py_RefTotal = 0;
1321}
1322
1323void
Fred Drake100814d2000-07-09 15:48:49 +00001324_Py_NewReference(PyObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001325{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001326 _Py_RefTotal++;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001327 op->ob_refcnt = 1;
1328 op->_ob_next = refchain._ob_next;
1329 op->_ob_prev = &refchain;
1330 refchain._ob_next->_ob_prev = op;
1331 refchain._ob_next = op;
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +00001332#ifdef COUNT_ALLOCS
1333 inc_count(op->ob_type);
1334#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001335}
1336
Guido van Rossumaacdc9d1996-08-12 21:32:12 +00001337void
Fred Drake100814d2000-07-09 15:48:49 +00001338_Py_ForgetReference(register PyObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001339{
Guido van Rossumbffd6832000-01-20 22:32:56 +00001340#ifdef SLOW_UNREF_CHECK
Guido van Rossum4c08d552000-03-10 22:55:18 +00001341 register PyObject *p;
Guido van Rossumbffd6832000-01-20 22:32:56 +00001342#endif
Guido van Rossumd7047b31995-01-02 19:07:15 +00001343 if (op->ob_refcnt < 0)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001344 Py_FatalError("UNREF negative refcnt");
Guido van Rossum2e8f6141992-09-03 20:32:55 +00001345 if (op == &refchain ||
Guido van Rossumd7047b31995-01-02 19:07:15 +00001346 op->_ob_prev->_ob_next != op || op->_ob_next->_ob_prev != op)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001347 Py_FatalError("UNREF invalid object");
Guido van Rossum2e8f6141992-09-03 20:32:55 +00001348#ifdef SLOW_UNREF_CHECK
Guido van Rossum3f5da241990-12-20 15:06:42 +00001349 for (p = refchain._ob_next; p != &refchain; p = p->_ob_next) {
1350 if (p == op)
1351 break;
1352 }
Guido van Rossumd7047b31995-01-02 19:07:15 +00001353 if (p == &refchain) /* Not found */
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001354 Py_FatalError("UNREF unknown object");
Guido van Rossum2e8f6141992-09-03 20:32:55 +00001355#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001356 op->_ob_next->_ob_prev = op->_ob_prev;
1357 op->_ob_prev->_ob_next = op->_ob_next;
Guido van Rossum2e8f6141992-09-03 20:32:55 +00001358 op->_ob_next = op->_ob_prev = NULL;
Guido van Rossumd8953cb1995-04-06 14:46:26 +00001359#ifdef COUNT_ALLOCS
1360 op->ob_type->tp_free++;
1361#endif
Guido van Rossum3f5da241990-12-20 15:06:42 +00001362}
1363
Guido van Rossumaacdc9d1996-08-12 21:32:12 +00001364void
Fred Drake100814d2000-07-09 15:48:49 +00001365_Py_Dealloc(PyObject *op)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001366{
Guido van Rossum9776adf1994-09-07 14:36:45 +00001367 destructor dealloc = op->ob_type->tp_dealloc;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001368 _Py_ForgetReference(op);
Guido van Rossum9776adf1994-09-07 14:36:45 +00001369 (*dealloc)(op);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001370}
1371
Guido van Rossumaacdc9d1996-08-12 21:32:12 +00001372void
Fred Drake100814d2000-07-09 15:48:49 +00001373_Py_PrintReferences(FILE *fp)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001374{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001375 PyObject *op;
Guido van Rossume09fb551997-08-05 02:04:34 +00001376 fprintf(fp, "Remaining objects:\n");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001377 for (op = refchain._ob_next; op != &refchain; op = op->_ob_next) {
1378 fprintf(fp, "[%d] ", op->ob_refcnt);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001379 if (PyObject_Print(op, fp, 0) != 0)
1380 PyErr_Clear();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001381 putc('\n', fp);
1382 }
1383}
1384
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +00001385PyObject *
Fred Drake100814d2000-07-09 15:48:49 +00001386_Py_GetObjects(PyObject *self, PyObject *args)
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +00001387{
1388 int i, n;
1389 PyObject *t = NULL;
1390 PyObject *res, *op;
1391
1392 if (!PyArg_ParseTuple(args, "i|O", &n, &t))
1393 return NULL;
1394 op = refchain._ob_next;
1395 res = PyList_New(0);
1396 if (res == NULL)
1397 return NULL;
1398 for (i = 0; (n == 0 || i < n) && op != &refchain; i++) {
1399 while (op == self || op == args || op == res || op == t ||
1400 t != NULL && op->ob_type != (PyTypeObject *) t) {
1401 op = op->_ob_next;
1402 if (op == &refchain)
1403 return res;
1404 }
1405 if (PyList_Append(res, op) < 0) {
1406 Py_DECREF(res);
1407 return NULL;
1408 }
1409 op = op->_ob_next;
1410 }
1411 return res;
1412}
1413
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001414#endif
Guido van Rossum97ead3f1996-01-12 01:24:09 +00001415
1416
1417/* Hack to force loading of cobject.o */
Guido van Rossumda9c2711996-12-05 21:58:58 +00001418PyTypeObject *_Py_cobject_hack = &PyCObject_Type;
Guido van Rossum84a90321996-05-22 16:34:47 +00001419
1420
1421/* Hack to force loading of abstract.o */
Jeremy Hylton6253f832000-07-12 12:56:19 +00001422int (*_Py_abstract_hack)(PyObject *) = &PyObject_Size;
Guido van Rossume09fb551997-08-05 02:04:34 +00001423
1424
Andrew M. Kuchling1582a3a2000-08-16 12:27:23 +00001425/* Python's malloc wrappers (see pymem.h) */
Guido van Rossume09fb551997-08-05 02:04:34 +00001426
Thomas Wouters334fb892000-07-25 12:56:38 +00001427void *
Fred Drake100814d2000-07-09 15:48:49 +00001428PyMem_Malloc(size_t nbytes)
Guido van Rossume09fb551997-08-05 02:04:34 +00001429{
1430#if _PyMem_EXTRA > 0
1431 if (nbytes == 0)
1432 nbytes = _PyMem_EXTRA;
1433#endif
Guido van Rossumb18618d2000-05-03 23:44:39 +00001434 return PyMem_MALLOC(nbytes);
Guido van Rossume09fb551997-08-05 02:04:34 +00001435}
1436
Thomas Wouters334fb892000-07-25 12:56:38 +00001437void *
1438PyMem_Realloc(void *p, size_t nbytes)
Guido van Rossume09fb551997-08-05 02:04:34 +00001439{
1440#if _PyMem_EXTRA > 0
1441 if (nbytes == 0)
1442 nbytes = _PyMem_EXTRA;
1443#endif
Guido van Rossumb18618d2000-05-03 23:44:39 +00001444 return PyMem_REALLOC(p, nbytes);
Guido van Rossume09fb551997-08-05 02:04:34 +00001445}
1446
1447void
Thomas Wouters334fb892000-07-25 12:56:38 +00001448PyMem_Free(void *p)
Guido van Rossume09fb551997-08-05 02:04:34 +00001449{
Guido van Rossumb18618d2000-05-03 23:44:39 +00001450 PyMem_FREE(p);
1451}
1452
1453
1454/* Python's object malloc wrappers (see objimpl.h) */
1455
Thomas Wouters334fb892000-07-25 12:56:38 +00001456void *
Fred Drake100814d2000-07-09 15:48:49 +00001457PyObject_Malloc(size_t nbytes)
Guido van Rossumb18618d2000-05-03 23:44:39 +00001458{
1459 return PyObject_MALLOC(nbytes);
1460}
1461
Thomas Wouters334fb892000-07-25 12:56:38 +00001462void *
1463PyObject_Realloc(void *p, size_t nbytes)
Guido van Rossumb18618d2000-05-03 23:44:39 +00001464{
1465 return PyObject_REALLOC(p, nbytes);
1466}
1467
1468void
Thomas Wouters334fb892000-07-25 12:56:38 +00001469PyObject_Free(void *p)
Guido van Rossumb18618d2000-05-03 23:44:39 +00001470{
1471 PyObject_FREE(p);
Guido van Rossume09fb551997-08-05 02:04:34 +00001472}
Guido van Rossum86610361998-04-10 22:32:46 +00001473
1474
Fred Drake41deb1e2001-02-01 05:27:45 +00001475/* Hook to clear up weak references only once the _weakref module is
1476 imported. We use a dummy implementation to simplify the code at each
1477 call site instead of requiring a test for NULL.
1478*/
1479
Fred Drakeb60654b2001-02-26 18:56:37 +00001480static void
Fred Drake41deb1e2001-02-01 05:27:45 +00001481empty_clear_weak_refs(PyObject *o)
1482{
Fred Drakeb60654b2001-02-26 18:56:37 +00001483 return;
Fred Drake41deb1e2001-02-01 05:27:45 +00001484}
1485
Fred Drakeb60654b2001-02-26 18:56:37 +00001486void (*PyObject_ClearWeakRefs)(PyObject *) = empty_clear_weak_refs;
Fred Drake41deb1e2001-02-01 05:27:45 +00001487
1488
1489
Guido van Rossum86610361998-04-10 22:32:46 +00001490/* These methods are used to control infinite recursion in repr, str, print,
1491 etc. Container objects that may recursively contain themselves,
1492 e.g. builtin dictionaries and lists, should used Py_ReprEnter() and
1493 Py_ReprLeave() to avoid infinite recursion.
1494
1495 Py_ReprEnter() returns 0 the first time it is called for a particular
1496 object and 1 every time thereafter. It returns -1 if an exception
1497 occurred. Py_ReprLeave() has no return value.
1498
1499 See dictobject.c and listobject.c for examples of use.
1500*/
1501
1502#define KEY "Py_Repr"
1503
1504int
Fred Drake100814d2000-07-09 15:48:49 +00001505Py_ReprEnter(PyObject *obj)
Guido van Rossum86610361998-04-10 22:32:46 +00001506{
1507 PyObject *dict;
1508 PyObject *list;
1509 int i;
1510
1511 dict = PyThreadState_GetDict();
1512 if (dict == NULL)
1513 return -1;
1514 list = PyDict_GetItemString(dict, KEY);
1515 if (list == NULL) {
1516 list = PyList_New(0);
1517 if (list == NULL)
1518 return -1;
1519 if (PyDict_SetItemString(dict, KEY, list) < 0)
1520 return -1;
1521 Py_DECREF(list);
1522 }
1523 i = PyList_GET_SIZE(list);
1524 while (--i >= 0) {
1525 if (PyList_GET_ITEM(list, i) == obj)
1526 return 1;
1527 }
1528 PyList_Append(list, obj);
1529 return 0;
1530}
1531
1532void
Fred Drake100814d2000-07-09 15:48:49 +00001533Py_ReprLeave(PyObject *obj)
Guido van Rossum86610361998-04-10 22:32:46 +00001534{
1535 PyObject *dict;
1536 PyObject *list;
1537 int i;
1538
1539 dict = PyThreadState_GetDict();
Guido van Rossumeb909461998-04-11 15:17:34 +00001540 if (dict == NULL)
1541 return;
Guido van Rossum86610361998-04-10 22:32:46 +00001542 list = PyDict_GetItemString(dict, KEY);
Guido van Rossumeb909461998-04-11 15:17:34 +00001543 if (list == NULL || !PyList_Check(list))
1544 return;
Guido van Rossum86610361998-04-10 22:32:46 +00001545 i = PyList_GET_SIZE(list);
1546 /* Count backwards because we always expect obj to be list[-1] */
1547 while (--i >= 0) {
1548 if (PyList_GET_ITEM(list, i) == obj) {
1549 PyList_SetSlice(list, i, i + 1, NULL);
1550 break;
1551 }
1552 }
1553}
Guido van Rossumd724b232000-03-13 16:01:29 +00001554
1555/*
1556 trashcan
1557 CT 2k0130
1558 non-recursively destroy nested objects
1559
1560 CT 2k0223
1561 everything is now done in a macro.
1562
1563 CT 2k0305
1564 modified to use functions, after Tim Peter's suggestion.
1565
1566 CT 2k0309
1567 modified to restore a possible error.
Guido van Rossum13ff8eb2000-03-25 18:39:19 +00001568
1569 CT 2k0325
1570 added better safe than sorry check for threadstate
Guido van Rossume92e6102000-04-24 15:40:53 +00001571
1572 CT 2k0422
1573 complete rewrite. We now build a chain via ob_type
1574 and save the limited number of types in ob_refcnt.
1575 This is perfect since we don't need any memory.
1576 A patch for free-threading would need just a lock.
Guido van Rossumd724b232000-03-13 16:01:29 +00001577*/
1578
Guido van Rossume92e6102000-04-24 15:40:53 +00001579#define Py_TRASHCAN_TUPLE 1
1580#define Py_TRASHCAN_LIST 2
1581#define Py_TRASHCAN_DICT 3
1582#define Py_TRASHCAN_FRAME 4
1583#define Py_TRASHCAN_TRACEBACK 5
1584/* extend here if other objects want protection */
1585
Guido van Rossumd724b232000-03-13 16:01:29 +00001586int _PyTrash_delete_nesting = 0;
Guido van Rossume92e6102000-04-24 15:40:53 +00001587
Guido van Rossumd724b232000-03-13 16:01:29 +00001588PyObject * _PyTrash_delete_later = NULL;
1589
1590void
Fred Drake100814d2000-07-09 15:48:49 +00001591_PyTrash_deposit_object(PyObject *op)
Guido van Rossumd724b232000-03-13 16:01:29 +00001592{
Guido van Rossume92e6102000-04-24 15:40:53 +00001593 int typecode;
Guido van Rossum13ff8eb2000-03-25 18:39:19 +00001594
Guido van Rossume92e6102000-04-24 15:40:53 +00001595 if (PyTuple_Check(op))
1596 typecode = Py_TRASHCAN_TUPLE;
1597 else if (PyList_Check(op))
1598 typecode = Py_TRASHCAN_LIST;
1599 else if (PyDict_Check(op))
1600 typecode = Py_TRASHCAN_DICT;
1601 else if (PyFrame_Check(op))
1602 typecode = Py_TRASHCAN_FRAME;
1603 else if (PyTraceBack_Check(op))
1604 typecode = Py_TRASHCAN_TRACEBACK;
Moshe Zadkacf703f02000-08-04 15:36:13 +00001605 else /* We have a bug here -- those are the only types in GC */ {
1606 Py_FatalError("Type not supported in GC -- internal bug");
1607 return; /* pacify compiler -- execution never here */
1608 }
Guido van Rossume92e6102000-04-24 15:40:53 +00001609 op->ob_refcnt = typecode;
Guido van Rossumd724b232000-03-13 16:01:29 +00001610
Guido van Rossume92e6102000-04-24 15:40:53 +00001611 op->ob_type = (PyTypeObject*)_PyTrash_delete_later;
1612 _PyTrash_delete_later = op;
Guido van Rossumd724b232000-03-13 16:01:29 +00001613}
1614
1615void
Fred Drake100814d2000-07-09 15:48:49 +00001616_PyTrash_destroy_chain(void)
Guido van Rossumd724b232000-03-13 16:01:29 +00001617{
1618 while (_PyTrash_delete_later) {
1619 PyObject *shredder = _PyTrash_delete_later;
Guido van Rossume92e6102000-04-24 15:40:53 +00001620 _PyTrash_delete_later = (PyObject*) shredder->ob_type;
1621
1622 switch (shredder->ob_refcnt) {
1623 case Py_TRASHCAN_TUPLE:
1624 shredder->ob_type = &PyTuple_Type;
1625 break;
1626 case Py_TRASHCAN_LIST:
1627 shredder->ob_type = &PyList_Type;
1628 break;
1629 case Py_TRASHCAN_DICT:
1630 shredder->ob_type = &PyDict_Type;
1631 break;
1632 case Py_TRASHCAN_FRAME:
1633 shredder->ob_type = &PyFrame_Type;
1634 break;
1635 case Py_TRASHCAN_TRACEBACK:
1636 shredder->ob_type = &PyTraceBack_Type;
1637 break;
1638 }
1639 _Py_NewReference(shredder);
1640
Guido van Rossumd724b232000-03-13 16:01:29 +00001641 ++_PyTrash_delete_nesting;
1642 Py_DECREF(shredder);
1643 --_PyTrash_delete_nesting;
1644 }
1645}
Neil Schemenauera35c6882001-02-27 04:45:05 +00001646
1647#ifdef WITH_PYMALLOC
1648#include "obmalloc.c"
1649#endif