blob: a4c9f087aa5bf2e10252dcfb5f67470e389d4209 [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{
234 (void)PyObject_Print(op, stderr, 0);
235 fprintf(stderr, "\nrefcounts: %d\n", op->ob_refcnt);
Barry Warsaw903138f2001-01-23 16:33:18 +0000236 fprintf(stderr, "address : %p\n", op);
Barry Warsaw9bf16442001-01-23 16:24:35 +0000237}
Barry Warsaw903138f2001-01-23 16:33:18 +0000238
239#ifdef WITH_CYCLE_GC
Barry Warsawbbd89b62001-01-24 04:18:13 +0000240void _PyGC_Dump(PyGC_Head* op)
Barry Warsaw9bf16442001-01-23 16:24:35 +0000241{
Barry Warsawbbd89b62001-01-24 04:18:13 +0000242 _PyObject_Dump(PyObject_FROM_GC(op));
Barry Warsaw9bf16442001-01-23 16:24:35 +0000243}
Barry Warsaw903138f2001-01-23 16:33:18 +0000244#endif /* WITH_CYCLE_GC */
Barry Warsaw9bf16442001-01-23 16:24:35 +0000245
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000246PyObject *
Fred Drake100814d2000-07-09 15:48:49 +0000247PyObject_Repr(PyObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000248{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000249 if (PyErr_CheckSignals())
Guido van Rossum90933611991-06-07 16:10:43 +0000250 return NULL;
Guido van Rossum9b00dfa1998-04-28 16:06:54 +0000251#ifdef USE_STACKCHECK
252 if (PyOS_CheckStack()) {
Fred Drake661ea262000-10-24 19:57:45 +0000253 PyErr_SetString(PyExc_MemoryError, "stack overflow");
Guido van Rossum9b00dfa1998-04-28 16:06:54 +0000254 return NULL;
255 }
256#endif
Guido van Rossum90933611991-06-07 16:10:43 +0000257 if (v == NULL)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000258 return PyString_FromString("<NULL>");
Guido van Rossum90933611991-06-07 16:10:43 +0000259 else if (v->ob_type->tp_repr == NULL) {
260 char buf[120];
Fred Drakea44d3532000-06-30 15:01:00 +0000261 sprintf(buf, "<%.80s object at %p>",
262 v->ob_type->tp_name, v);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000263 return PyString_FromString(buf);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000264 }
Guido van Rossum4c08d552000-03-10 22:55:18 +0000265 else {
266 PyObject *res;
267 res = (*v->ob_type->tp_repr)(v);
268 if (res == NULL)
269 return NULL;
Fredrik Lundhefecc7d2000-07-01 14:31:09 +0000270 if (PyUnicode_Check(res)) {
271 PyObject* str;
Fredrik Lundh2a1e0602000-07-08 17:43:32 +0000272 str = PyUnicode_AsUnicodeEscapeString(res);
Marc-André Lemburg891bc652000-07-03 09:57:53 +0000273 Py_DECREF(res);
274 if (str)
Fredrik Lundhefecc7d2000-07-01 14:31:09 +0000275 res = str;
Marc-André Lemburg891bc652000-07-03 09:57:53 +0000276 else
277 return NULL;
Fredrik Lundhefecc7d2000-07-01 14:31:09 +0000278 }
Guido van Rossum4c08d552000-03-10 22:55:18 +0000279 if (!PyString_Check(res)) {
280 PyErr_Format(PyExc_TypeError,
Guido van Rossum5db862d2000-04-10 12:46:51 +0000281 "__repr__ returned non-string (type %.200s)",
Guido van Rossum4c08d552000-03-10 22:55:18 +0000282 res->ob_type->tp_name);
283 Py_DECREF(res);
284 return NULL;
285 }
286 return res;
287 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000288}
289
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000290PyObject *
Fred Drake100814d2000-07-09 15:48:49 +0000291PyObject_Str(PyObject *v)
Guido van Rossumc6004111993-11-05 10:22:19 +0000292{
Guido van Rossum4c08d552000-03-10 22:55:18 +0000293 PyObject *res;
294
Guido van Rossumc6004111993-11-05 10:22:19 +0000295 if (v == NULL)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000296 return PyString_FromString("<NULL>");
297 else if (PyString_Check(v)) {
298 Py_INCREF(v);
Guido van Rossumc6004111993-11-05 10:22:19 +0000299 return v;
300 }
Guido van Rossum32b582b1995-01-17 16:35:13 +0000301 else if (v->ob_type->tp_str != NULL)
Guido van Rossum4c08d552000-03-10 22:55:18 +0000302 res = (*v->ob_type->tp_str)(v);
Guido van Rossumc6004111993-11-05 10:22:19 +0000303 else {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000304 PyObject *func;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000305 if (!PyInstance_Check(v) ||
306 (func = PyObject_GetAttrString(v, "__str__")) == NULL) {
307 PyErr_Clear();
308 return PyObject_Repr(v);
Guido van Rossumc6004111993-11-05 10:22:19 +0000309 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000310 res = PyEval_CallObject(func, (PyObject *)NULL);
311 Py_DECREF(func);
Guido van Rossumc6004111993-11-05 10:22:19 +0000312 }
Guido van Rossum4c08d552000-03-10 22:55:18 +0000313 if (res == NULL)
314 return NULL;
Marc-André Lemburg891bc652000-07-03 09:57:53 +0000315 if (PyUnicode_Check(res)) {
316 PyObject* str;
317 str = PyUnicode_AsEncodedString(res, NULL, NULL);
318 Py_DECREF(res);
319 if (str)
320 res = str;
321 else
322 return NULL;
323 }
Guido van Rossum4c08d552000-03-10 22:55:18 +0000324 if (!PyString_Check(res)) {
325 PyErr_Format(PyExc_TypeError,
Guido van Rossum5db862d2000-04-10 12:46:51 +0000326 "__str__ returned non-string (type %.200s)",
Guido van Rossum4c08d552000-03-10 22:55:18 +0000327 res->ob_type->tp_name);
328 Py_DECREF(res);
329 return NULL;
330 }
331 return res;
Guido van Rossumc6004111993-11-05 10:22:19 +0000332}
333
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000334PyObject *
335PyObject_Unicode(PyObject *v)
336{
337 PyObject *res;
338
339 if (v == NULL)
340 res = PyString_FromString("<NULL>");
341 else if (PyUnicode_Check(v)) {
342 Py_INCREF(v);
343 return v;
344 }
345 else if (PyString_Check(v))
346 res = v;
347 else if (v->ob_type->tp_str != NULL)
348 res = (*v->ob_type->tp_str)(v);
349 else {
350 PyObject *func;
351 static PyObject *strstr;
352 if (strstr == NULL) {
353 strstr= PyString_InternFromString("__str__");
354 if (strstr == NULL)
355 return NULL;
356 }
357 if (!PyInstance_Check(v) ||
358 (func = PyObject_GetAttr(v, strstr)) == NULL) {
359 PyErr_Clear();
360 res = PyObject_Repr(v);
361 }
362 else {
363 res = PyEval_CallObject(func, (PyObject *)NULL);
364 Py_DECREF(func);
365 }
366 }
367 if (res == NULL)
368 return NULL;
369 if (!PyUnicode_Check(res)) {
370 PyObject* str;
371 str = PyUnicode_FromObject(res);
372 Py_DECREF(res);
373 if (str)
374 res = str;
375 else
376 return NULL;
377 }
378 return res;
379}
380
381
Guido van Rossumd1f06b92001-01-24 22:14:43 +0000382/* Macro to get the tp_richcompare field of a type if defined */
383#define RICHCOMPARE(t) (PyType_HasFeature((t), Py_TPFLAGS_HAVE_RICHCOMPARE) \
384 ? (t)->tp_richcompare : NULL)
385
Guido van Rossume797ec12001-01-17 15:24:28 +0000386/* Map rich comparison operators to their swapped version, e.g. LT --> GT */
387static int swapped_op[] = {Py_GT, Py_GE, Py_EQ, Py_NE, Py_LT, Py_LE};
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +0000388
Guido van Rossume797ec12001-01-17 15:24:28 +0000389/* Try a genuine rich comparison, returning an object. Return:
390 NULL for exception;
391 NotImplemented if this particular rich comparison is not implemented or
392 undefined;
393 some object not equal to NotImplemented if it is implemented
394 (this latter object may not be a Boolean).
395*/
396static PyObject *
397try_rich_compare(PyObject *v, PyObject *w, int op)
398{
399 richcmpfunc f;
400 PyObject *res;
401
Guido van Rossumd1f06b92001-01-24 22:14:43 +0000402 if ((f = RICHCOMPARE(v->ob_type)) != NULL) {
Guido van Rossume797ec12001-01-17 15:24:28 +0000403 res = (*f)(v, w, op);
404 if (res != Py_NotImplemented)
405 return res;
406 Py_DECREF(res);
407 }
Guido van Rossumd1f06b92001-01-24 22:14:43 +0000408 if ((f = RICHCOMPARE(w->ob_type)) != NULL) {
Guido van Rossume797ec12001-01-17 15:24:28 +0000409 return (*f)(w, v, swapped_op[op]);
410 }
411 res = Py_NotImplemented;
412 Py_INCREF(res);
413 return res;
414}
415
416/* Try a genuine rich comparison, returning an int. Return:
417 -1 for exception (including the case where try_rich_compare() returns an
418 object that's not a Boolean);
419 0 if the outcome is false;
420 1 if the outcome is true;
421 2 if this particular rich comparison is not implemented or undefined.
422*/
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +0000423static int
Guido van Rossume797ec12001-01-17 15:24:28 +0000424try_rich_compare_bool(PyObject *v, PyObject *w, int op)
425{
426 PyObject *res;
427 int ok;
428
Guido van Rossumd1f06b92001-01-24 22:14:43 +0000429 if (RICHCOMPARE(v->ob_type) == NULL && RICHCOMPARE(w->ob_type) == NULL)
Guido van Rossume797ec12001-01-17 15:24:28 +0000430 return 2; /* Shortcut, avoid INCREF+DECREF */
431 res = try_rich_compare(v, w, op);
432 if (res == NULL)
433 return -1;
434 if (res == Py_NotImplemented) {
435 Py_DECREF(res);
436 return 2;
437 }
438 ok = PyObject_IsTrue(res);
439 Py_DECREF(res);
440 return ok;
441}
442
443/* Try rich comparisons to determine a 3-way comparison. Return:
444 -2 for an exception;
445 -1 if v < w;
446 0 if v == w;
447 1 if v > w;
448 2 if this particular rich comparison is not implemented or undefined.
449*/
450static int
451try_rich_to_3way_compare(PyObject *v, PyObject *w)
452{
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000453 static struct { int op; int outcome; } tries[3] = {
454 /* Try this operator, and if it is true, use this outcome: */
455 {Py_EQ, 0},
456 {Py_LT, -1},
457 {Py_GT, 1},
458 };
459 int i;
460
Guido van Rossumd1f06b92001-01-24 22:14:43 +0000461 if (RICHCOMPARE(v->ob_type) == NULL && RICHCOMPARE(w->ob_type) == NULL)
Guido van Rossume797ec12001-01-17 15:24:28 +0000462 return 2; /* Shortcut */
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000463
464 for (i = 0; i < 3; i++) {
465 switch (try_rich_compare_bool(v, w, tries[i].op)) {
466 case -1:
467 return -1;
468 case 1:
469 return tries[i].outcome;
470 }
Guido van Rossume797ec12001-01-17 15:24:28 +0000471 }
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000472
473 return 2;
Guido van Rossume797ec12001-01-17 15:24:28 +0000474}
475
476/* Try a 3-way comparison, returning an int. Return:
477 -2 for an exception;
478 -1 if v < w;
479 0 if v == w;
480 1 if v > w;
481 2 if this particular 3-way comparison is not implemented or undefined.
482*/
483static int
484try_3way_compare(PyObject *v, PyObject *w)
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +0000485{
486 int c;
Guido van Rossume797ec12001-01-17 15:24:28 +0000487 cmpfunc f;
488
489 /* Comparisons involving instances are given to instance_compare,
490 which has the same return conventions as this function. */
491
492 if (PyInstance_Check(v))
493 return (*v->ob_type->tp_compare)(v, w);
494 if (PyInstance_Check(w))
495 return (*w->ob_type->tp_compare)(v, w);
496
497 /* If the types are equal, don't bother with coercions etc. */
498 if (v->ob_type == w->ob_type) {
499 if ((f = v->ob_type->tp_compare) == NULL)
500 return 2;
501 c = (*f)(v, w);
502 if (PyErr_Occurred())
503 return -2;
504 return c < 0 ? -1 : c > 0 ? 1 : 0;
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +0000505 }
Guido van Rossume797ec12001-01-17 15:24:28 +0000506
507 /* Try coercion; if it fails, give up */
508 c = PyNumber_CoerceEx(&v, &w);
509 if (c < 0)
510 return -2;
511 if (c > 0)
512 return 2;
513
514 /* Try v's comparison, if defined */
515 if ((f = v->ob_type->tp_compare) != NULL) {
516 c = (*f)(v, w);
517 Py_DECREF(v);
518 Py_DECREF(w);
519 if (PyErr_Occurred())
520 return -2;
521 return c < 0 ? -1 : c > 0 ? 1 : 0;
522 }
523
524 /* Try w's comparison, if defined */
525 if ((f = w->ob_type->tp_compare) != NULL) {
526 c = (*f)(w, v); /* swapped! */
527 Py_DECREF(v);
528 Py_DECREF(w);
529 if (PyErr_Occurred())
530 return -2;
531 return c < 0 ? 1 : c > 0 ? -1 : 0; /* negated! */
532 }
533
534 /* No comparison defined */
535 Py_DECREF(v);
536 Py_DECREF(w);
537 return 2;
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +0000538}
539
Guido van Rossume797ec12001-01-17 15:24:28 +0000540/* Final fallback 3-way comparison, returning an int. Return:
541 -2 if an error occurred;
542 -1 if v < w;
543 0 if v == w;
544 1 if v > w.
545*/
546static int
547default_3way_compare(PyObject *v, PyObject *w)
548{
549 int c;
Guido van Rossum8f9143d2001-01-22 15:59:32 +0000550 char *vname, *wname;
Guido van Rossume797ec12001-01-17 15:24:28 +0000551
552 if (v->ob_type == w->ob_type) {
Barry Warsawb0e754d2001-01-20 06:24:55 +0000553 /* When comparing these pointers, they must be cast to
554 * integer types (i.e. Py_uintptr_t, our spelling of C9X's
555 * uintptr_t). ANSI specifies that pointer compares other
556 * than == and != to non-related structures are undefined.
557 */
Barry Warsaw71ff8d52001-01-20 06:08:10 +0000558 Py_uintptr_t vv = (Py_uintptr_t)v;
559 Py_uintptr_t ww = (Py_uintptr_t)w;
Guido van Rossume797ec12001-01-17 15:24:28 +0000560 return (vv < ww) ? -1 : (vv > ww) ? 1 : 0;
561 }
562
563 /* Special case for Unicode */
564 if (PyUnicode_Check(v) || PyUnicode_Check(w)) {
565 c = PyUnicode_Compare(v, w);
566 if (!PyErr_Occurred())
567 return c;
568 /* TypeErrors are ignored: if Unicode coercion fails due
569 to one of the arguments not having the right type, we
570 continue as defined by the coercion protocol (see
571 above). Luckily, decoding errors are reported as
572 ValueErrors and are not masked by this technique. */
573 if (!PyErr_ExceptionMatches(PyExc_TypeError))
574 return -2;
575 PyErr_Clear();
576 }
577
Guido van Rossum0871e932001-01-22 19:28:09 +0000578 /* None is smaller than anything */
579 if (v == Py_None)
580 return -1;
581 if (w == Py_None)
582 return 1;
583
Guido van Rossume797ec12001-01-17 15:24:28 +0000584 /* different type: compare type names */
Guido van Rossum8f9143d2001-01-22 15:59:32 +0000585 if (v->ob_type->tp_as_number)
586 vname = "";
587 else
588 vname = v->ob_type->tp_name;
589 if (w->ob_type->tp_as_number)
590 wname = "";
591 else
592 wname = w->ob_type->tp_name;
593 c = strcmp(vname, wname);
594 if (c < 0)
595 return -1;
596 if (c > 0)
597 return 1;
598 /* Same type name, or (more likely) incomparable numeric types */
599 return ((Py_uintptr_t)(v->ob_type) < (
600 Py_uintptr_t)(w->ob_type)) ? -1 : 1;
Guido van Rossume797ec12001-01-17 15:24:28 +0000601}
602
603#define CHECK_TYPES(o) PyType_HasFeature((o)->ob_type, Py_TPFLAGS_CHECKTYPES)
604
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +0000605static int
Fred Drake100814d2000-07-09 15:48:49 +0000606do_cmp(PyObject *v, PyObject *w)
Guido van Rossum20566841995-01-12 11:26:10 +0000607{
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +0000608 int c;
609
Guido van Rossume797ec12001-01-17 15:24:28 +0000610 c = try_rich_to_3way_compare(v, w);
611 if (c < 2)
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +0000612 return c;
Guido van Rossume797ec12001-01-17 15:24:28 +0000613 c = try_3way_compare(v, w);
614 if (c < 2)
615 return c;
616 return default_3way_compare(v, w);
Guido van Rossum20566841995-01-12 11:26:10 +0000617}
618
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000619/* compare_nesting is incremented before calling compare (for
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000620 some types) and decremented on exit. If the count exceeds the
621 nesting limit, enable code to detect circular data structures.
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000622
623 This is a tunable parameter that should only affect the performance
624 of comparisons, nothing else. Setting it high makes comparing deeply
625 nested non-cyclical data structures faster, but makes comparing cyclical
626 data structures slower.
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000627*/
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000628#define NESTING_LIMIT 20
629
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000630static int compare_nesting = 0;
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000631
632static PyObject*
Fred Drake100814d2000-07-09 15:48:49 +0000633get_inprogress_dict(void)
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000634{
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000635 static PyObject *key;
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000636 PyObject *tstate_dict, *inprogress;
637
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000638 if (key == NULL) {
639 key = PyString_InternFromString("cmp_state");
640 if (key == NULL)
641 return NULL;
642 }
643
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000644 tstate_dict = PyThreadState_GetDict();
645 if (tstate_dict == NULL) {
646 PyErr_BadInternalCall();
647 return NULL;
648 }
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000649
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000650 inprogress = PyDict_GetItem(tstate_dict, key);
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000651 if (inprogress == NULL) {
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000652 inprogress = PyDict_New();
653 if (inprogress == NULL)
654 return NULL;
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000655 if (PyDict_SetItem(tstate_dict, key, inprogress) == -1) {
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000656 Py_DECREF(inprogress);
657 return NULL;
658 }
Jeremy Hyltona251ea02000-06-09 16:20:39 +0000659 Py_DECREF(inprogress);
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000660 }
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000661
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000662 return inprogress;
663}
664
665static PyObject *
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000666check_recursion(PyObject *v, PyObject *w, int op)
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000667{
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000668 PyObject *inprogress;
669 PyObject *token;
Barry Warsaw9d23a4e2000-08-18 05:01:19 +0000670 Py_uintptr_t iv = (Py_uintptr_t)v;
671 Py_uintptr_t iw = (Py_uintptr_t)w;
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000672 PyObject *x, *y, *z;
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000673
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000674 inprogress = get_inprogress_dict();
675 if (inprogress == NULL)
676 return NULL;
677
678 token = PyTuple_New(3);
679 if (token == NULL)
680 return NULL;
681
682 if (iv <= iw) {
683 PyTuple_SET_ITEM(token, 0, x = PyLong_FromVoidPtr((void *)v));
684 PyTuple_SET_ITEM(token, 1, y = PyLong_FromVoidPtr((void *)w));
685 if (op >= 0)
686 op = swapped_op[op];
687 } else {
688 PyTuple_SET_ITEM(token, 0, x = PyLong_FromVoidPtr((void *)w));
689 PyTuple_SET_ITEM(token, 1, y = PyLong_FromVoidPtr((void *)v));
690 }
691 PyTuple_SET_ITEM(token, 2, z = PyInt_FromLong((long)op));
692 if (x == NULL || y == NULL || z == NULL) {
693 Py_DECREF(token);
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000694 return NULL;
695 }
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000696
697 if (PyDict_GetItem(inprogress, token) != NULL) {
698 Py_DECREF(token);
699 return Py_None; /* Without INCREF! */
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000700 }
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000701
702 if (PyDict_SetItem(inprogress, token, token) < 0) {
703 Py_DECREF(token);
704 return NULL;
705 }
706
707 return token;
708}
709
710static void
711delete_token(PyObject *token)
712{
713 PyObject *inprogress;
714
715 if (token == NULL || token == Py_None)
716 return;
717 inprogress = get_inprogress_dict();
718 if (inprogress == NULL)
719 PyErr_Clear();
720 else
721 PyDict_DelItem(inprogress, token);
722 Py_DECREF(token);
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000723}
724
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000725int
Fred Drake100814d2000-07-09 15:48:49 +0000726PyObject_Compare(PyObject *v, PyObject *w)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000727{
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000728 PyTypeObject *vtp;
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000729 int result;
730
Jack Jansend49cbe12000-08-22 21:52:51 +0000731#if defined(USE_STACKCHECK)
Guido van Rossum9c0a99e2000-08-30 15:53:50 +0000732 if (PyOS_CheckStack()) {
Jack Jansend49cbe12000-08-22 21:52:51 +0000733 PyErr_SetString(PyExc_MemoryError, "Stack overflow");
734 return -1;
735 }
736#endif
Guido van Rossumc8b6df91997-05-23 00:06:51 +0000737 if (v == NULL || w == NULL) {
738 PyErr_BadInternalCall();
739 return -1;
740 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000741 if (v == w)
742 return 0;
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +0000743 vtp = v->ob_type;
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000744 compare_nesting++;
745 if (compare_nesting > NESTING_LIMIT &&
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +0000746 (vtp->tp_as_mapping
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000747 || (vtp->tp_as_sequence
748 && !PyString_Check(v)
749 && !PyTuple_Check(v)))) {
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +0000750 /* try to detect circular data structures */
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000751 PyObject *token = check_recursion(v, w, -1);
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000752
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000753 if (token == NULL) {
754 result = -1;
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000755 }
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000756 else if (token == Py_None) {
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000757 /* already comparing these objects. assume
758 they're equal until shown otherwise */
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +0000759 result = 0;
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000760 }
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000761 else {
762 result = do_cmp(v, w);
763 delete_token(token);
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000764 }
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000765 }
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +0000766 else {
767 result = do_cmp(v, w);
768 }
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000769 compare_nesting--;
Guido van Rossume797ec12001-01-17 15:24:28 +0000770 return result < 0 ? -1 : result;
771}
772
773static PyObject *
774try_3way_to_rich_compare(PyObject *v, PyObject *w, int op)
775{
776 int c;
777 PyObject *result;
778
779 c = try_3way_compare(v, w);
780 if (c <= -2)
781 return NULL;
782 if (c >= 2)
783 c = default_3way_compare(v, w);
784 switch (op) {
785 case Py_LT: c = c < 0; break;
786 case Py_LE: c = c <= 0; break;
787 case Py_EQ: c = c == 0; break;
788 case Py_NE: c = c != 0; break;
789 case Py_GT: c = c > 0; break;
790 case Py_GE: c = c >= 0; break;
791 }
792 result = c ? Py_True : Py_False;
793 Py_INCREF(result);
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000794 return result;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000795}
796
Neil Schemenauerd38855c2001-01-21 16:25:18 +0000797static PyObject *
Guido van Rossume797ec12001-01-17 15:24:28 +0000798do_richcmp(PyObject *v, PyObject *w, int op)
799{
800 PyObject *res;
801
802 res = try_rich_compare(v, w, op);
803 if (res != Py_NotImplemented)
804 return res;
805 Py_DECREF(res);
806
807 return try_3way_to_rich_compare(v, w, op);
808}
809
810PyObject *
811PyObject_RichCompare(PyObject *v, PyObject *w, int op)
812{
813 PyObject *res;
814
815 assert(Py_LT <= op && op <= Py_GE);
816
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000817 compare_nesting++;
818 if (compare_nesting > NESTING_LIMIT &&
819 (v->ob_type->tp_as_mapping
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000820 || (v->ob_type->tp_as_sequence
821 && !PyString_Check(v)
822 && !PyTuple_Check(v)))) {
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000823 /* try to detect circular data structures */
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000824 PyObject *token = check_recursion(v, w, op);
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000825
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000826 if (token == NULL) {
827 res = NULL;
Guido van Rossume797ec12001-01-17 15:24:28 +0000828 }
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000829 else if (token == Py_None) {
830 /* already comparing these objects with this operator.
831 assume they're equal until shown otherwise */
832 if (op == Py_EQ)
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000833 res = Py_True;
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000834 else if (op == Py_NE)
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000835 res = Py_False;
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000836 else {
837 PyErr_SetString(PyExc_ValueError,
838 "can't order recursive values");
839 res = NULL;
840 }
841 Py_XINCREF(res);
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000842 }
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000843 else {
844 res = do_richcmp(v, w, op);
845 delete_token(token);
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000846 }
Guido van Rossume797ec12001-01-17 15:24:28 +0000847 }
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000848 else {
849 res = do_richcmp(v, w, op);
850 }
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000851 compare_nesting--;
Guido van Rossume797ec12001-01-17 15:24:28 +0000852 return res;
853}
854
855int
856PyObject_RichCompareBool(PyObject *v, PyObject *w, int op)
857{
858 PyObject *res = PyObject_RichCompare(v, w, op);
859 int ok;
860
861 if (res == NULL)
862 return -1;
863 ok = PyObject_IsTrue(res);
864 Py_DECREF(res);
865 return ok;
866}
Fred Drake13634cf2000-06-29 19:17:04 +0000867
868/* Set of hash utility functions to help maintaining the invariant that
869 iff a==b then hash(a)==hash(b)
870
871 All the utility functions (_Py_Hash*()) return "-1" to signify an error.
872*/
873
874long
Fred Drake100814d2000-07-09 15:48:49 +0000875_Py_HashDouble(double v)
Fred Drake13634cf2000-06-29 19:17:04 +0000876{
Tim Peters39dce292000-08-15 03:34:48 +0000877 double intpart, fractpart;
878 int expo;
879 long hipart;
880 long x; /* the final hash value */
881 /* This is designed so that Python numbers of different types
882 * that compare equal hash to the same value; otherwise comparisons
883 * of mapping keys will turn out weird.
884 */
885
886#ifdef MPW /* MPW C modf expects pointer to extended as second argument */
887{
888 extended e;
889 fractpart = modf(v, &e);
890 intpart = e;
891}
892#else
893 fractpart = modf(v, &intpart);
894#endif
895 if (fractpart == 0.0) {
896 /* This must return the same hash as an equal int or long. */
897 if (intpart > LONG_MAX || -intpart > LONG_MAX) {
898 /* Convert to long and use its hash. */
899 PyObject *plong; /* converted to Python long */
900 if (Py_IS_INFINITY(intpart))
901 /* can't convert to long int -- arbitrary */
902 v = v < 0 ? -271828.0 : 314159.0;
903 plong = PyLong_FromDouble(v);
904 if (plong == NULL)
905 return -1;
906 x = PyObject_Hash(plong);
907 Py_DECREF(plong);
908 return x;
909 }
910 /* Fits in a C long == a Python int, so is its own hash. */
911 x = (long)intpart;
912 if (x == -1)
913 x = -2;
914 return x;
915 }
916 /* The fractional part is non-zero, so we don't have to worry about
917 * making this match the hash of some other type.
918 * Use frexp to get at the bits in the double.
Fred Drake13634cf2000-06-29 19:17:04 +0000919 * Since the VAX D double format has 56 mantissa bits, which is the
920 * most of any double format in use, each of these parts may have as
921 * many as (but no more than) 56 significant bits.
Tim Peters39dce292000-08-15 03:34:48 +0000922 * So, assuming sizeof(long) >= 4, each part can be broken into two
923 * longs; frexp and multiplication are used to do that.
924 * Also, since the Cray double format has 15 exponent bits, which is
925 * the most of any double format in use, shifting the exponent field
926 * left by 15 won't overflow a long (again assuming sizeof(long) >= 4).
Fred Drake13634cf2000-06-29 19:17:04 +0000927 */
Tim Peters39dce292000-08-15 03:34:48 +0000928 v = frexp(v, &expo);
929 v *= 2147483648.0; /* 2**31 */
930 hipart = (long)v; /* take the top 32 bits */
931 v = (v - (double)hipart) * 2147483648.0; /* get the next 32 bits */
932 x = hipart + (long)v + (expo << 15);
933 if (x == -1)
934 x = -2;
935 return x;
Fred Drake13634cf2000-06-29 19:17:04 +0000936}
937
938long
Fred Drake100814d2000-07-09 15:48:49 +0000939_Py_HashPointer(void *p)
Fred Drake13634cf2000-06-29 19:17:04 +0000940{
941#if SIZEOF_LONG >= SIZEOF_VOID_P
942 return (long)p;
943#else
944 /* convert to a Python long and hash that */
945 PyObject* longobj;
946 long x;
947
948 if ((longobj = PyLong_FromVoidPtr(p)) == NULL) {
949 x = -1;
950 goto finally;
951 }
952 x = PyObject_Hash(longobj);
953
954finally:
955 Py_XDECREF(longobj);
956 return x;
957#endif
958}
959
960
Guido van Rossum9bfef441993-03-29 10:43:31 +0000961long
Fred Drake100814d2000-07-09 15:48:49 +0000962PyObject_Hash(PyObject *v)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000963{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000964 PyTypeObject *tp = v->ob_type;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000965 if (tp->tp_hash != NULL)
966 return (*tp->tp_hash)(v);
Guido van Rossumd1f06b92001-01-24 22:14:43 +0000967 if (tp->tp_compare == NULL && RICHCOMPARE(tp) == NULL) {
Fred Drake13634cf2000-06-29 19:17:04 +0000968 return _Py_HashPointer(v); /* Use address as hash value */
969 }
Guido van Rossum9bfef441993-03-29 10:43:31 +0000970 /* If there's a cmp but no hash defined, the object can't be hashed */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000971 PyErr_SetString(PyExc_TypeError, "unhashable type");
Guido van Rossum9bfef441993-03-29 10:43:31 +0000972 return -1;
973}
974
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000975PyObject *
Fred Drake100814d2000-07-09 15:48:49 +0000976PyObject_GetAttrString(PyObject *v, char *name)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000977{
Guido van Rossumd8eb1b31996-08-09 20:52:03 +0000978 if (v->ob_type->tp_getattro != NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000979 PyObject *w, *res;
Guido van Rossumc6d06701997-01-18 07:57:16 +0000980 w = PyString_InternFromString(name);
Guido van Rossumd8eb1b31996-08-09 20:52:03 +0000981 if (w == NULL)
982 return NULL;
983 res = (*v->ob_type->tp_getattro)(v, w);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000984 Py_XDECREF(w);
Guido van Rossumd8eb1b31996-08-09 20:52:03 +0000985 return res;
986 }
987
Guido van Rossum3f5da241990-12-20 15:06:42 +0000988 if (v->ob_type->tp_getattr == NULL) {
Guido van Rossumdb935161998-01-19 22:16:36 +0000989 PyErr_Format(PyExc_AttributeError,
990 "'%.50s' object has no attribute '%.400s'",
991 v->ob_type->tp_name,
992 name);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000993 return NULL;
994 }
995 else {
996 return (*v->ob_type->tp_getattr)(v, name);
997 }
998}
999
1000int
Fred Drake100814d2000-07-09 15:48:49 +00001001PyObject_HasAttrString(PyObject *v, char *name)
Guido van Rossumed18fdc1993-07-11 19:55:34 +00001002{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001003 PyObject *res = PyObject_GetAttrString(v, name);
Guido van Rossumed18fdc1993-07-11 19:55:34 +00001004 if (res != NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001005 Py_DECREF(res);
Guido van Rossumed18fdc1993-07-11 19:55:34 +00001006 return 1;
1007 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001008 PyErr_Clear();
Guido van Rossumed18fdc1993-07-11 19:55:34 +00001009 return 0;
1010}
1011
1012int
Fred Drake100814d2000-07-09 15:48:49 +00001013PyObject_SetAttrString(PyObject *v, char *name, PyObject *w)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001014{
Guido van Rossumd8eb1b31996-08-09 20:52:03 +00001015 if (v->ob_type->tp_setattro != NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001016 PyObject *s;
Guido van Rossumd8eb1b31996-08-09 20:52:03 +00001017 int res;
Guido van Rossumc6d06701997-01-18 07:57:16 +00001018 s = PyString_InternFromString(name);
Guido van Rossumd8eb1b31996-08-09 20:52:03 +00001019 if (s == NULL)
Guido van Rossumb7fc3041996-09-11 22:51:25 +00001020 return -1;
Guido van Rossumd8eb1b31996-08-09 20:52:03 +00001021 res = (*v->ob_type->tp_setattro)(v, s, w);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001022 Py_XDECREF(s);
Guido van Rossumd8eb1b31996-08-09 20:52:03 +00001023 return res;
1024 }
1025
Guido van Rossum3f5da241990-12-20 15:06:42 +00001026 if (v->ob_type->tp_setattr == NULL) {
1027 if (v->ob_type->tp_getattr == NULL)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001028 PyErr_SetString(PyExc_TypeError,
Guido van Rossum3ea74121991-12-24 13:28:03 +00001029 "attribute-less object (assign or del)");
Guido van Rossum3f5da241990-12-20 15:06:42 +00001030 else
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001031 PyErr_SetString(PyExc_TypeError,
Guido van Rossum3ea74121991-12-24 13:28:03 +00001032 "object has read-only attributes");
Guido van Rossum73531a31990-12-20 23:12:40 +00001033 return -1;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001034 }
1035 else {
1036 return (*v->ob_type->tp_setattr)(v, name, w);
1037 }
1038}
1039
Marc-André Lemburge44e5072000-09-18 16:20:57 +00001040/* Internal API needed by PyObject_GetAttr(): */
1041extern
1042PyObject *_PyUnicode_AsDefaultEncodedString(PyObject *unicode,
1043 const char *errors);
1044
Guido van Rossum98ff96a1997-05-20 18:34:44 +00001045PyObject *
Fred Drake100814d2000-07-09 15:48:49 +00001046PyObject_GetAttr(PyObject *v, PyObject *name)
Guido van Rossum98ff96a1997-05-20 18:34:44 +00001047{
Marc-André Lemburge44e5072000-09-18 16:20:57 +00001048 /* The Unicode to string conversion is done here because the
1049 existing tp_getattro slots expect a string object as name
1050 and we wouldn't want to break those. */
1051 if (PyUnicode_Check(name)) {
1052 name = _PyUnicode_AsDefaultEncodedString(name, NULL);
1053 if (name == NULL)
1054 return NULL;
1055 }
Jeremy Hylton99a8f902000-06-23 14:36:32 +00001056
1057 if (!PyString_Check(name)) {
1058 PyErr_SetString(PyExc_TypeError,
1059 "attribute name must be string");
1060 return NULL;
1061 }
Marc-André Lemburge44e5072000-09-18 16:20:57 +00001062 if (v->ob_type->tp_getattro != NULL)
1063 return (*v->ob_type->tp_getattro)(v, name);
1064 else
Jeremy Hylton99a8f902000-06-23 14:36:32 +00001065 return PyObject_GetAttrString(v, PyString_AS_STRING(name));
Guido van Rossum98ff96a1997-05-20 18:34:44 +00001066}
1067
1068int
Fred Drake100814d2000-07-09 15:48:49 +00001069PyObject_HasAttr(PyObject *v, PyObject *name)
Guido van Rossum98ff96a1997-05-20 18:34:44 +00001070{
1071 PyObject *res = PyObject_GetAttr(v, name);
1072 if (res != NULL) {
1073 Py_DECREF(res);
1074 return 1;
1075 }
1076 PyErr_Clear();
1077 return 0;
1078}
1079
1080int
Fred Drake100814d2000-07-09 15:48:49 +00001081PyObject_SetAttr(PyObject *v, PyObject *name, PyObject *value)
Guido van Rossum98ff96a1997-05-20 18:34:44 +00001082{
1083 int err;
Marc-André Lemburge44e5072000-09-18 16:20:57 +00001084
1085 /* The Unicode to string conversion is done here because the
1086 existing tp_setattro slots expect a string object as name
1087 and we wouldn't want to break those. */
1088 if (PyUnicode_Check(name)) {
1089 name = PyUnicode_AsEncodedString(name, NULL, NULL);
1090 if (name == NULL)
1091 return -1;
Jeremy Hylton99a8f902000-06-23 14:36:32 +00001092 }
Marc-André Lemburge44e5072000-09-18 16:20:57 +00001093 else
1094 Py_INCREF(name);
1095
1096 if (!PyString_Check(name)){
Jeremy Hylton99a8f902000-06-23 14:36:32 +00001097 PyErr_SetString(PyExc_TypeError,
1098 "attribute name must be string");
1099 err = -1;
1100 }
Marc-André Lemburge44e5072000-09-18 16:20:57 +00001101 else {
1102 PyString_InternInPlace(&name);
1103 if (v->ob_type->tp_setattro != NULL)
1104 err = (*v->ob_type->tp_setattro)(v, name, value);
1105 else
1106 err = PyObject_SetAttrString(v,
1107 PyString_AS_STRING(name), value);
1108 }
1109
Guido van Rossum98ff96a1997-05-20 18:34:44 +00001110 Py_DECREF(name);
1111 return err;
1112}
1113
Guido van Rossum6ac258d1993-05-12 08:24:20 +00001114/* Test a value used as condition, e.g., in a for or if statement.
1115 Return -1 if an error occurred */
1116
1117int
Fred Drake100814d2000-07-09 15:48:49 +00001118PyObject_IsTrue(PyObject *v)
Guido van Rossum6ac258d1993-05-12 08:24:20 +00001119{
1120 int res;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001121 if (v == Py_None)
Guido van Rossum6ac258d1993-05-12 08:24:20 +00001122 res = 0;
Guido van Rossum1c4f4581998-05-22 00:53:24 +00001123 else if (v->ob_type->tp_as_number != NULL &&
1124 v->ob_type->tp_as_number->nb_nonzero != NULL)
Guido van Rossum6ac258d1993-05-12 08:24:20 +00001125 res = (*v->ob_type->tp_as_number->nb_nonzero)(v);
Guido van Rossum1c4f4581998-05-22 00:53:24 +00001126 else if (v->ob_type->tp_as_mapping != NULL &&
1127 v->ob_type->tp_as_mapping->mp_length != NULL)
Guido van Rossum6ac258d1993-05-12 08:24:20 +00001128 res = (*v->ob_type->tp_as_mapping->mp_length)(v);
Guido van Rossum1c4f4581998-05-22 00:53:24 +00001129 else if (v->ob_type->tp_as_sequence != NULL &&
1130 v->ob_type->tp_as_sequence->sq_length != NULL)
Guido van Rossum6ac258d1993-05-12 08:24:20 +00001131 res = (*v->ob_type->tp_as_sequence->sq_length)(v);
1132 else
1133 res = 1;
1134 if (res > 0)
1135 res = 1;
1136 return res;
1137}
1138
Guido van Rossumc3d3f961998-04-09 17:53:59 +00001139/* equivalent of 'not v'
1140 Return -1 if an error occurred */
1141
1142int
Fred Drake100814d2000-07-09 15:48:49 +00001143PyObject_Not(PyObject *v)
Guido van Rossumc3d3f961998-04-09 17:53:59 +00001144{
1145 int res;
1146 res = PyObject_IsTrue(v);
1147 if (res < 0)
1148 return res;
1149 return res == 0;
1150}
1151
Guido van Rossum5524a591995-01-10 15:26:20 +00001152/* Coerce two numeric types to the "larger" one.
1153 Increment the reference count on each argument.
Guido van Rossume797ec12001-01-17 15:24:28 +00001154 Return value:
1155 -1 if an error occurred;
1156 0 if the coercion succeeded (and then the reference counts are increased);
1157 1 if no coercion is possible (and no error is raised).
Guido van Rossum5524a591995-01-10 15:26:20 +00001158*/
Guido van Rossum5524a591995-01-10 15:26:20 +00001159int
Fred Drake100814d2000-07-09 15:48:49 +00001160PyNumber_CoerceEx(PyObject **pv, PyObject **pw)
Guido van Rossum5524a591995-01-10 15:26:20 +00001161{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001162 register PyObject *v = *pv;
1163 register PyObject *w = *pw;
Guido van Rossum5524a591995-01-10 15:26:20 +00001164 int res;
1165
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001166 if (v->ob_type == w->ob_type && !PyInstance_Check(v)) {
1167 Py_INCREF(v);
1168 Py_INCREF(w);
Guido van Rossum5524a591995-01-10 15:26:20 +00001169 return 0;
1170 }
1171 if (v->ob_type->tp_as_number && v->ob_type->tp_as_number->nb_coerce) {
1172 res = (*v->ob_type->tp_as_number->nb_coerce)(pv, pw);
1173 if (res <= 0)
1174 return res;
1175 }
1176 if (w->ob_type->tp_as_number && w->ob_type->tp_as_number->nb_coerce) {
1177 res = (*w->ob_type->tp_as_number->nb_coerce)(pw, pv);
1178 if (res <= 0)
1179 return res;
1180 }
Guido van Rossum242c6421997-11-19 16:03:17 +00001181 return 1;
1182}
1183
Guido van Rossume797ec12001-01-17 15:24:28 +00001184/* Coerce two numeric types to the "larger" one.
1185 Increment the reference count on each argument.
1186 Return -1 and raise an exception if no coercion is possible
1187 (and then no reference count is incremented).
1188*/
Guido van Rossum242c6421997-11-19 16:03:17 +00001189int
Fred Drake100814d2000-07-09 15:48:49 +00001190PyNumber_Coerce(PyObject **pv, PyObject **pw)
Guido van Rossum242c6421997-11-19 16:03:17 +00001191{
1192 int err = PyNumber_CoerceEx(pv, pw);
1193 if (err <= 0)
1194 return err;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001195 PyErr_SetString(PyExc_TypeError, "number coercion failed");
Guido van Rossum5524a591995-01-10 15:26:20 +00001196 return -1;
1197}
1198
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001199
Guido van Rossum49b11fe1995-01-26 00:38:22 +00001200/* Test whether an object can be called */
1201
1202int
Fred Drake100814d2000-07-09 15:48:49 +00001203PyCallable_Check(PyObject *x)
Guido van Rossum49b11fe1995-01-26 00:38:22 +00001204{
1205 if (x == NULL)
1206 return 0;
1207 if (x->ob_type->tp_call != NULL ||
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001208 PyFunction_Check(x) ||
1209 PyMethod_Check(x) ||
1210 PyCFunction_Check(x) ||
1211 PyClass_Check(x))
Guido van Rossum49b11fe1995-01-26 00:38:22 +00001212 return 1;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001213 if (PyInstance_Check(x)) {
1214 PyObject *call = PyObject_GetAttrString(x, "__call__");
Guido van Rossum49b11fe1995-01-26 00:38:22 +00001215 if (call == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001216 PyErr_Clear();
Guido van Rossum49b11fe1995-01-26 00:38:22 +00001217 return 0;
1218 }
1219 /* Could test recursively but don't, for fear of endless
1220 recursion if some joker sets self.__call__ = self */
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001221 Py_DECREF(call);
Guido van Rossum49b11fe1995-01-26 00:38:22 +00001222 return 1;
1223 }
1224 return 0;
1225}
1226
1227
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001228/*
1229NoObject is usable as a non-NULL undefined value, used by the macro None.
1230There is (and should be!) no way to create other objects of this type,
Guido van Rossum3f5da241990-12-20 15:06:42 +00001231so there is exactly one (which is indestructible, by the way).
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001232*/
1233
Guido van Rossum0c182a11992-03-27 17:26:13 +00001234/* ARGSUSED */
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001235static PyObject *
Fred Drake100814d2000-07-09 15:48:49 +00001236none_repr(PyObject *op)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001237{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001238 return PyString_FromString("None");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001239}
1240
Barry Warsaw9bf16442001-01-23 16:24:35 +00001241/* ARGUSED */
1242static void
1243none_dealloc(PyObject* ignore)
1244{
1245 /* This should never get called, but we also don't want to SEGV if
1246 * we accidently decref None out of existance.
1247 */
1248 abort();
1249}
1250
1251
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001252static PyTypeObject PyNothing_Type = {
1253 PyObject_HEAD_INIT(&PyType_Type)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001254 0,
Guido van Rossum3f5da241990-12-20 15:06:42 +00001255 "None",
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001256 0,
1257 0,
Barry Warsaw9bf16442001-01-23 16:24:35 +00001258 (destructor)none_dealloc, /*tp_dealloc*/ /*never called*/
Guido van Rossum7066dd71992-09-17 17:54:56 +00001259 0, /*tp_print*/
Guido van Rossum3f5da241990-12-20 15:06:42 +00001260 0, /*tp_getattr*/
1261 0, /*tp_setattr*/
1262 0, /*tp_compare*/
Guido van Rossum1d5735e1994-08-30 08:27:36 +00001263 (reprfunc)none_repr, /*tp_repr*/
Guido van Rossum3f5da241990-12-20 15:06:42 +00001264 0, /*tp_as_number*/
1265 0, /*tp_as_sequence*/
1266 0, /*tp_as_mapping*/
Guido van Rossum9bfef441993-03-29 10:43:31 +00001267 0, /*tp_hash */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001268};
1269
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001270PyObject _Py_NoneStruct = {
1271 PyObject_HEAD_INIT(&PyNothing_Type)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001272};
1273
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +00001274/* NotImplemented is an object that can be used to signal that an
1275 operation is not implemented for the given type combination. */
1276
1277static PyObject *
1278NotImplemented_repr(PyObject *op)
1279{
1280 return PyString_FromString("NotImplemented");
1281}
1282
1283static PyTypeObject PyNotImplemented_Type = {
1284 PyObject_HEAD_INIT(&PyType_Type)
1285 0,
1286 "NotImplemented",
1287 0,
1288 0,
Barry Warsaw9bf16442001-01-23 16:24:35 +00001289 (destructor)none_dealloc, /*tp_dealloc*/ /*never called*/
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +00001290 0, /*tp_print*/
1291 0, /*tp_getattr*/
1292 0, /*tp_setattr*/
1293 0, /*tp_compare*/
1294 (reprfunc)NotImplemented_repr, /*tp_repr*/
1295 0, /*tp_as_number*/
1296 0, /*tp_as_sequence*/
1297 0, /*tp_as_mapping*/
1298 0, /*tp_hash */
1299};
1300
1301PyObject _Py_NotImplementedStruct = {
1302 PyObject_HEAD_INIT(&PyNotImplemented_Type)
1303};
1304
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001305
Guido van Rossum84a90321996-05-22 16:34:47 +00001306#ifdef Py_TRACE_REFS
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001307
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001308static PyObject refchain = {&refchain, &refchain};
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001309
Guido van Rossumaacdc9d1996-08-12 21:32:12 +00001310void
Fred Drake100814d2000-07-09 15:48:49 +00001311_Py_ResetReferences(void)
Guido van Rossume09fb551997-08-05 02:04:34 +00001312{
1313 refchain._ob_prev = refchain._ob_next = &refchain;
1314 _Py_RefTotal = 0;
1315}
1316
1317void
Fred Drake100814d2000-07-09 15:48:49 +00001318_Py_NewReference(PyObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001319{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001320 _Py_RefTotal++;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001321 op->ob_refcnt = 1;
1322 op->_ob_next = refchain._ob_next;
1323 op->_ob_prev = &refchain;
1324 refchain._ob_next->_ob_prev = op;
1325 refchain._ob_next = op;
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +00001326#ifdef COUNT_ALLOCS
1327 inc_count(op->ob_type);
1328#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001329}
1330
Guido van Rossumaacdc9d1996-08-12 21:32:12 +00001331void
Fred Drake100814d2000-07-09 15:48:49 +00001332_Py_ForgetReference(register PyObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001333{
Guido van Rossumbffd6832000-01-20 22:32:56 +00001334#ifdef SLOW_UNREF_CHECK
Guido van Rossum4c08d552000-03-10 22:55:18 +00001335 register PyObject *p;
Guido van Rossumbffd6832000-01-20 22:32:56 +00001336#endif
Guido van Rossumd7047b31995-01-02 19:07:15 +00001337 if (op->ob_refcnt < 0)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001338 Py_FatalError("UNREF negative refcnt");
Guido van Rossum2e8f6141992-09-03 20:32:55 +00001339 if (op == &refchain ||
Guido van Rossumd7047b31995-01-02 19:07:15 +00001340 op->_ob_prev->_ob_next != op || op->_ob_next->_ob_prev != op)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001341 Py_FatalError("UNREF invalid object");
Guido van Rossum2e8f6141992-09-03 20:32:55 +00001342#ifdef SLOW_UNREF_CHECK
Guido van Rossum3f5da241990-12-20 15:06:42 +00001343 for (p = refchain._ob_next; p != &refchain; p = p->_ob_next) {
1344 if (p == op)
1345 break;
1346 }
Guido van Rossumd7047b31995-01-02 19:07:15 +00001347 if (p == &refchain) /* Not found */
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001348 Py_FatalError("UNREF unknown object");
Guido van Rossum2e8f6141992-09-03 20:32:55 +00001349#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001350 op->_ob_next->_ob_prev = op->_ob_prev;
1351 op->_ob_prev->_ob_next = op->_ob_next;
Guido van Rossum2e8f6141992-09-03 20:32:55 +00001352 op->_ob_next = op->_ob_prev = NULL;
Guido van Rossumd8953cb1995-04-06 14:46:26 +00001353#ifdef COUNT_ALLOCS
1354 op->ob_type->tp_free++;
1355#endif
Guido van Rossum3f5da241990-12-20 15:06:42 +00001356}
1357
Guido van Rossumaacdc9d1996-08-12 21:32:12 +00001358void
Fred Drake100814d2000-07-09 15:48:49 +00001359_Py_Dealloc(PyObject *op)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001360{
Guido van Rossum9776adf1994-09-07 14:36:45 +00001361 destructor dealloc = op->ob_type->tp_dealloc;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001362 _Py_ForgetReference(op);
Guido van Rossum9776adf1994-09-07 14:36:45 +00001363 (*dealloc)(op);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001364}
1365
Guido van Rossumaacdc9d1996-08-12 21:32:12 +00001366void
Fred Drake100814d2000-07-09 15:48:49 +00001367_Py_PrintReferences(FILE *fp)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001368{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001369 PyObject *op;
Guido van Rossume09fb551997-08-05 02:04:34 +00001370 fprintf(fp, "Remaining objects:\n");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001371 for (op = refchain._ob_next; op != &refchain; op = op->_ob_next) {
1372 fprintf(fp, "[%d] ", op->ob_refcnt);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001373 if (PyObject_Print(op, fp, 0) != 0)
1374 PyErr_Clear();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001375 putc('\n', fp);
1376 }
1377}
1378
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +00001379PyObject *
Fred Drake100814d2000-07-09 15:48:49 +00001380_Py_GetObjects(PyObject *self, PyObject *args)
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +00001381{
1382 int i, n;
1383 PyObject *t = NULL;
1384 PyObject *res, *op;
1385
1386 if (!PyArg_ParseTuple(args, "i|O", &n, &t))
1387 return NULL;
1388 op = refchain._ob_next;
1389 res = PyList_New(0);
1390 if (res == NULL)
1391 return NULL;
1392 for (i = 0; (n == 0 || i < n) && op != &refchain; i++) {
1393 while (op == self || op == args || op == res || op == t ||
1394 t != NULL && op->ob_type != (PyTypeObject *) t) {
1395 op = op->_ob_next;
1396 if (op == &refchain)
1397 return res;
1398 }
1399 if (PyList_Append(res, op) < 0) {
1400 Py_DECREF(res);
1401 return NULL;
1402 }
1403 op = op->_ob_next;
1404 }
1405 return res;
1406}
1407
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001408#endif
Guido van Rossum97ead3f1996-01-12 01:24:09 +00001409
1410
1411/* Hack to force loading of cobject.o */
Guido van Rossumda9c2711996-12-05 21:58:58 +00001412PyTypeObject *_Py_cobject_hack = &PyCObject_Type;
Guido van Rossum84a90321996-05-22 16:34:47 +00001413
1414
1415/* Hack to force loading of abstract.o */
Jeremy Hylton6253f832000-07-12 12:56:19 +00001416int (*_Py_abstract_hack)(PyObject *) = &PyObject_Size;
Guido van Rossume09fb551997-08-05 02:04:34 +00001417
1418
Andrew M. Kuchling1582a3a2000-08-16 12:27:23 +00001419/* Python's malloc wrappers (see pymem.h) */
Guido van Rossume09fb551997-08-05 02:04:34 +00001420
Thomas Wouters334fb892000-07-25 12:56:38 +00001421void *
Fred Drake100814d2000-07-09 15:48:49 +00001422PyMem_Malloc(size_t nbytes)
Guido van Rossume09fb551997-08-05 02:04:34 +00001423{
1424#if _PyMem_EXTRA > 0
1425 if (nbytes == 0)
1426 nbytes = _PyMem_EXTRA;
1427#endif
Guido van Rossumb18618d2000-05-03 23:44:39 +00001428 return PyMem_MALLOC(nbytes);
Guido van Rossume09fb551997-08-05 02:04:34 +00001429}
1430
Thomas Wouters334fb892000-07-25 12:56:38 +00001431void *
1432PyMem_Realloc(void *p, size_t nbytes)
Guido van Rossume09fb551997-08-05 02:04:34 +00001433{
1434#if _PyMem_EXTRA > 0
1435 if (nbytes == 0)
1436 nbytes = _PyMem_EXTRA;
1437#endif
Guido van Rossumb18618d2000-05-03 23:44:39 +00001438 return PyMem_REALLOC(p, nbytes);
Guido van Rossume09fb551997-08-05 02:04:34 +00001439}
1440
1441void
Thomas Wouters334fb892000-07-25 12:56:38 +00001442PyMem_Free(void *p)
Guido van Rossume09fb551997-08-05 02:04:34 +00001443{
Guido van Rossumb18618d2000-05-03 23:44:39 +00001444 PyMem_FREE(p);
1445}
1446
1447
1448/* Python's object malloc wrappers (see objimpl.h) */
1449
Thomas Wouters334fb892000-07-25 12:56:38 +00001450void *
Fred Drake100814d2000-07-09 15:48:49 +00001451PyObject_Malloc(size_t nbytes)
Guido van Rossumb18618d2000-05-03 23:44:39 +00001452{
1453 return PyObject_MALLOC(nbytes);
1454}
1455
Thomas Wouters334fb892000-07-25 12:56:38 +00001456void *
1457PyObject_Realloc(void *p, size_t nbytes)
Guido van Rossumb18618d2000-05-03 23:44:39 +00001458{
1459 return PyObject_REALLOC(p, nbytes);
1460}
1461
1462void
Thomas Wouters334fb892000-07-25 12:56:38 +00001463PyObject_Free(void *p)
Guido van Rossumb18618d2000-05-03 23:44:39 +00001464{
1465 PyObject_FREE(p);
Guido van Rossume09fb551997-08-05 02:04:34 +00001466}
Guido van Rossum86610361998-04-10 22:32:46 +00001467
1468
Fred Drake41deb1e2001-02-01 05:27:45 +00001469/* Hook to clear up weak references only once the _weakref module is
1470 imported. We use a dummy implementation to simplify the code at each
1471 call site instead of requiring a test for NULL.
1472*/
1473
1474static int
1475empty_clear_weak_refs(PyObject *o)
1476{
1477 return 1;
1478}
1479
1480int (*PyObject_ClearWeakRefs)(PyObject *) = empty_clear_weak_refs;
1481
1482
1483
Guido van Rossum86610361998-04-10 22:32:46 +00001484/* These methods are used to control infinite recursion in repr, str, print,
1485 etc. Container objects that may recursively contain themselves,
1486 e.g. builtin dictionaries and lists, should used Py_ReprEnter() and
1487 Py_ReprLeave() to avoid infinite recursion.
1488
1489 Py_ReprEnter() returns 0 the first time it is called for a particular
1490 object and 1 every time thereafter. It returns -1 if an exception
1491 occurred. Py_ReprLeave() has no return value.
1492
1493 See dictobject.c and listobject.c for examples of use.
1494*/
1495
1496#define KEY "Py_Repr"
1497
1498int
Fred Drake100814d2000-07-09 15:48:49 +00001499Py_ReprEnter(PyObject *obj)
Guido van Rossum86610361998-04-10 22:32:46 +00001500{
1501 PyObject *dict;
1502 PyObject *list;
1503 int i;
1504
1505 dict = PyThreadState_GetDict();
1506 if (dict == NULL)
1507 return -1;
1508 list = PyDict_GetItemString(dict, KEY);
1509 if (list == NULL) {
1510 list = PyList_New(0);
1511 if (list == NULL)
1512 return -1;
1513 if (PyDict_SetItemString(dict, KEY, list) < 0)
1514 return -1;
1515 Py_DECREF(list);
1516 }
1517 i = PyList_GET_SIZE(list);
1518 while (--i >= 0) {
1519 if (PyList_GET_ITEM(list, i) == obj)
1520 return 1;
1521 }
1522 PyList_Append(list, obj);
1523 return 0;
1524}
1525
1526void
Fred Drake100814d2000-07-09 15:48:49 +00001527Py_ReprLeave(PyObject *obj)
Guido van Rossum86610361998-04-10 22:32:46 +00001528{
1529 PyObject *dict;
1530 PyObject *list;
1531 int i;
1532
1533 dict = PyThreadState_GetDict();
Guido van Rossumeb909461998-04-11 15:17:34 +00001534 if (dict == NULL)
1535 return;
Guido van Rossum86610361998-04-10 22:32:46 +00001536 list = PyDict_GetItemString(dict, KEY);
Guido van Rossumeb909461998-04-11 15:17:34 +00001537 if (list == NULL || !PyList_Check(list))
1538 return;
Guido van Rossum86610361998-04-10 22:32:46 +00001539 i = PyList_GET_SIZE(list);
1540 /* Count backwards because we always expect obj to be list[-1] */
1541 while (--i >= 0) {
1542 if (PyList_GET_ITEM(list, i) == obj) {
1543 PyList_SetSlice(list, i, i + 1, NULL);
1544 break;
1545 }
1546 }
1547}
Guido van Rossumd724b232000-03-13 16:01:29 +00001548
1549/*
1550 trashcan
1551 CT 2k0130
1552 non-recursively destroy nested objects
1553
1554 CT 2k0223
1555 everything is now done in a macro.
1556
1557 CT 2k0305
1558 modified to use functions, after Tim Peter's suggestion.
1559
1560 CT 2k0309
1561 modified to restore a possible error.
Guido van Rossum13ff8eb2000-03-25 18:39:19 +00001562
1563 CT 2k0325
1564 added better safe than sorry check for threadstate
Guido van Rossume92e6102000-04-24 15:40:53 +00001565
1566 CT 2k0422
1567 complete rewrite. We now build a chain via ob_type
1568 and save the limited number of types in ob_refcnt.
1569 This is perfect since we don't need any memory.
1570 A patch for free-threading would need just a lock.
Guido van Rossumd724b232000-03-13 16:01:29 +00001571*/
1572
Guido van Rossume92e6102000-04-24 15:40:53 +00001573#define Py_TRASHCAN_TUPLE 1
1574#define Py_TRASHCAN_LIST 2
1575#define Py_TRASHCAN_DICT 3
1576#define Py_TRASHCAN_FRAME 4
1577#define Py_TRASHCAN_TRACEBACK 5
1578/* extend here if other objects want protection */
1579
Guido van Rossumd724b232000-03-13 16:01:29 +00001580int _PyTrash_delete_nesting = 0;
Guido van Rossume92e6102000-04-24 15:40:53 +00001581
Guido van Rossumd724b232000-03-13 16:01:29 +00001582PyObject * _PyTrash_delete_later = NULL;
1583
1584void
Fred Drake100814d2000-07-09 15:48:49 +00001585_PyTrash_deposit_object(PyObject *op)
Guido van Rossumd724b232000-03-13 16:01:29 +00001586{
Guido van Rossume92e6102000-04-24 15:40:53 +00001587 int typecode;
Guido van Rossum13ff8eb2000-03-25 18:39:19 +00001588
Guido van Rossume92e6102000-04-24 15:40:53 +00001589 if (PyTuple_Check(op))
1590 typecode = Py_TRASHCAN_TUPLE;
1591 else if (PyList_Check(op))
1592 typecode = Py_TRASHCAN_LIST;
1593 else if (PyDict_Check(op))
1594 typecode = Py_TRASHCAN_DICT;
1595 else if (PyFrame_Check(op))
1596 typecode = Py_TRASHCAN_FRAME;
1597 else if (PyTraceBack_Check(op))
1598 typecode = Py_TRASHCAN_TRACEBACK;
Moshe Zadkacf703f02000-08-04 15:36:13 +00001599 else /* We have a bug here -- those are the only types in GC */ {
1600 Py_FatalError("Type not supported in GC -- internal bug");
1601 return; /* pacify compiler -- execution never here */
1602 }
Guido van Rossume92e6102000-04-24 15:40:53 +00001603 op->ob_refcnt = typecode;
Guido van Rossumd724b232000-03-13 16:01:29 +00001604
Guido van Rossume92e6102000-04-24 15:40:53 +00001605 op->ob_type = (PyTypeObject*)_PyTrash_delete_later;
1606 _PyTrash_delete_later = op;
Guido van Rossumd724b232000-03-13 16:01:29 +00001607}
1608
1609void
Fred Drake100814d2000-07-09 15:48:49 +00001610_PyTrash_destroy_chain(void)
Guido van Rossumd724b232000-03-13 16:01:29 +00001611{
1612 while (_PyTrash_delete_later) {
1613 PyObject *shredder = _PyTrash_delete_later;
Guido van Rossume92e6102000-04-24 15:40:53 +00001614 _PyTrash_delete_later = (PyObject*) shredder->ob_type;
1615
1616 switch (shredder->ob_refcnt) {
1617 case Py_TRASHCAN_TUPLE:
1618 shredder->ob_type = &PyTuple_Type;
1619 break;
1620 case Py_TRASHCAN_LIST:
1621 shredder->ob_type = &PyList_Type;
1622 break;
1623 case Py_TRASHCAN_DICT:
1624 shredder->ob_type = &PyDict_Type;
1625 break;
1626 case Py_TRASHCAN_FRAME:
1627 shredder->ob_type = &PyFrame_Type;
1628 break;
1629 case Py_TRASHCAN_TRACEBACK:
1630 shredder->ob_type = &PyTraceBack_Type;
1631 break;
1632 }
1633 _Py_NewReference(shredder);
1634
Guido van Rossumd724b232000-03-13 16:01:29 +00001635 ++_PyTrash_delete_nesting;
1636 Py_DECREF(shredder);
1637 --_PyTrash_delete_nesting;
1638 }
1639}