blob: 04f75e95c1070a2ebfa84473ecf9689e942c9023 [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) {
Guido van Rossum4f288ab2001-05-01 16:53:37 +0000199 PyObject *s;
200 if (flags & Py_PRINT_RAW)
201 s = PyObject_Str(op);
202 else
203 s = PyObject_Repr(op);
204 if (s == NULL)
205 ret = -1;
Guido van Rossum2e8f6141992-09-03 20:32:55 +0000206 else {
Guido van Rossum4f288ab2001-05-01 16:53:37 +0000207 ret = PyObject_Print(s, fp, Py_PRINT_RAW);
Guido van Rossum2e8f6141992-09-03 20:32:55 +0000208 }
Guido van Rossum4f288ab2001-05-01 16:53:37 +0000209 Py_XDECREF(s);
Guido van Rossum2e8f6141992-09-03 20:32:55 +0000210 }
Guido van Rossum90933611991-06-07 16:10:43 +0000211 else
Guido van Rossum278ef591991-07-27 21:40:24 +0000212 ret = (*op->ob_type->tp_print)(op, fp, flags);
Guido van Rossum90933611991-06-07 16:10:43 +0000213 }
Guido van Rossum278ef591991-07-27 21:40:24 +0000214 if (ret == 0) {
215 if (ferror(fp)) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000216 PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossum278ef591991-07-27 21:40:24 +0000217 clearerr(fp);
218 ret = -1;
219 }
220 }
221 return ret;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000222}
223
Barry Warsaw9bf16442001-01-23 16:24:35 +0000224/* For debugging convenience. See Misc/gdbinit for some useful gdb hooks */
Barry Warsawbbd89b62001-01-24 04:18:13 +0000225void _PyObject_Dump(PyObject* op)
Barry Warsaw9bf16442001-01-23 16:24:35 +0000226{
Barry Warsaweefb1072001-02-22 22:39:18 +0000227 if (op == NULL)
228 fprintf(stderr, "NULL\n");
229 else {
230 (void)PyObject_Print(op, stderr, 0);
231 fprintf(stderr, "\nrefcounts: %d\n", op->ob_refcnt);
232 fprintf(stderr, "address : %p\n", op);
233 }
Barry Warsaw9bf16442001-01-23 16:24:35 +0000234}
Barry Warsaw903138f2001-01-23 16:33:18 +0000235
236#ifdef WITH_CYCLE_GC
Barry Warsawbbd89b62001-01-24 04:18:13 +0000237void _PyGC_Dump(PyGC_Head* op)
Barry Warsaw9bf16442001-01-23 16:24:35 +0000238{
Barry Warsawbbd89b62001-01-24 04:18:13 +0000239 _PyObject_Dump(PyObject_FROM_GC(op));
Barry Warsaw9bf16442001-01-23 16:24:35 +0000240}
Barry Warsaw903138f2001-01-23 16:33:18 +0000241#endif /* WITH_CYCLE_GC */
Barry Warsaw9bf16442001-01-23 16:24:35 +0000242
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000243PyObject *
Fred Drake100814d2000-07-09 15:48:49 +0000244PyObject_Repr(PyObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000245{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000246 if (PyErr_CheckSignals())
Guido van Rossum90933611991-06-07 16:10:43 +0000247 return NULL;
Guido van Rossum9b00dfa1998-04-28 16:06:54 +0000248#ifdef USE_STACKCHECK
249 if (PyOS_CheckStack()) {
Fred Drake661ea262000-10-24 19:57:45 +0000250 PyErr_SetString(PyExc_MemoryError, "stack overflow");
Guido van Rossum9b00dfa1998-04-28 16:06:54 +0000251 return NULL;
252 }
253#endif
Guido van Rossum90933611991-06-07 16:10:43 +0000254 if (v == NULL)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000255 return PyString_FromString("<NULL>");
Guido van Rossum90933611991-06-07 16:10:43 +0000256 else if (v->ob_type->tp_repr == NULL) {
257 char buf[120];
Fred Drakea44d3532000-06-30 15:01:00 +0000258 sprintf(buf, "<%.80s object at %p>",
259 v->ob_type->tp_name, v);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000260 return PyString_FromString(buf);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000261 }
Guido van Rossum4c08d552000-03-10 22:55:18 +0000262 else {
263 PyObject *res;
264 res = (*v->ob_type->tp_repr)(v);
265 if (res == NULL)
266 return NULL;
Fredrik Lundhefecc7d2000-07-01 14:31:09 +0000267 if (PyUnicode_Check(res)) {
268 PyObject* str;
Fredrik Lundh2a1e0602000-07-08 17:43:32 +0000269 str = PyUnicode_AsUnicodeEscapeString(res);
Marc-André Lemburg891bc652000-07-03 09:57:53 +0000270 Py_DECREF(res);
271 if (str)
Fredrik Lundhefecc7d2000-07-01 14:31:09 +0000272 res = str;
Marc-André Lemburg891bc652000-07-03 09:57:53 +0000273 else
274 return NULL;
Fredrik Lundhefecc7d2000-07-01 14:31:09 +0000275 }
Guido van Rossum4c08d552000-03-10 22:55:18 +0000276 if (!PyString_Check(res)) {
277 PyErr_Format(PyExc_TypeError,
Guido van Rossum5db862d2000-04-10 12:46:51 +0000278 "__repr__ returned non-string (type %.200s)",
Guido van Rossum4c08d552000-03-10 22:55:18 +0000279 res->ob_type->tp_name);
280 Py_DECREF(res);
281 return NULL;
282 }
283 return res;
284 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000285}
286
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000287PyObject *
Fred Drake100814d2000-07-09 15:48:49 +0000288PyObject_Str(PyObject *v)
Guido van Rossumc6004111993-11-05 10:22:19 +0000289{
Guido van Rossum4c08d552000-03-10 22:55:18 +0000290 PyObject *res;
291
Guido van Rossumc6004111993-11-05 10:22:19 +0000292 if (v == NULL)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000293 return PyString_FromString("<NULL>");
Guido van Rossum4f288ab2001-05-01 16:53:37 +0000294 if (PyString_Check(v)) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000295 Py_INCREF(v);
Guido van Rossumc6004111993-11-05 10:22:19 +0000296 return v;
297 }
Guido van Rossum4f288ab2001-05-01 16:53:37 +0000298 if (v->ob_type->tp_str == NULL)
299 return PyObject_Repr(v);
300
301 res = (*v->ob_type->tp_str)(v);
Guido van Rossum4c08d552000-03-10 22:55:18 +0000302 if (res == NULL)
303 return NULL;
Marc-André Lemburg891bc652000-07-03 09:57:53 +0000304 if (PyUnicode_Check(res)) {
305 PyObject* str;
306 str = PyUnicode_AsEncodedString(res, NULL, NULL);
307 Py_DECREF(res);
308 if (str)
309 res = str;
310 else
311 return NULL;
312 }
Guido van Rossum4c08d552000-03-10 22:55:18 +0000313 if (!PyString_Check(res)) {
314 PyErr_Format(PyExc_TypeError,
Guido van Rossum5db862d2000-04-10 12:46:51 +0000315 "__str__ returned non-string (type %.200s)",
Guido van Rossum4c08d552000-03-10 22:55:18 +0000316 res->ob_type->tp_name);
317 Py_DECREF(res);
318 return NULL;
319 }
320 return res;
Guido van Rossumc6004111993-11-05 10:22:19 +0000321}
322
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000323PyObject *
324PyObject_Unicode(PyObject *v)
325{
326 PyObject *res;
327
328 if (v == NULL)
329 res = PyString_FromString("<NULL>");
330 else if (PyUnicode_Check(v)) {
331 Py_INCREF(v);
332 return v;
333 }
Marc-André Lemburgae605342001-03-25 19:16:13 +0000334 else if (PyString_Check(v)) {
335 Py_INCREF(v);
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000336 res = v;
Marc-André Lemburgae605342001-03-25 19:16:13 +0000337 }
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000338 else if (v->ob_type->tp_str != NULL)
339 res = (*v->ob_type->tp_str)(v);
340 else {
341 PyObject *func;
342 static PyObject *strstr;
343 if (strstr == NULL) {
344 strstr= PyString_InternFromString("__str__");
345 if (strstr == NULL)
346 return NULL;
347 }
348 if (!PyInstance_Check(v) ||
349 (func = PyObject_GetAttr(v, strstr)) == NULL) {
350 PyErr_Clear();
351 res = PyObject_Repr(v);
352 }
353 else {
354 res = PyEval_CallObject(func, (PyObject *)NULL);
355 Py_DECREF(func);
356 }
357 }
358 if (res == NULL)
359 return NULL;
360 if (!PyUnicode_Check(res)) {
361 PyObject* str;
362 str = PyUnicode_FromObject(res);
363 Py_DECREF(res);
364 if (str)
365 res = str;
366 else
367 return NULL;
368 }
369 return res;
370}
371
372
Guido van Rossumd1f06b92001-01-24 22:14:43 +0000373/* Macro to get the tp_richcompare field of a type if defined */
374#define RICHCOMPARE(t) (PyType_HasFeature((t), Py_TPFLAGS_HAVE_RICHCOMPARE) \
375 ? (t)->tp_richcompare : NULL)
376
Guido van Rossume797ec12001-01-17 15:24:28 +0000377/* Map rich comparison operators to their swapped version, e.g. LT --> GT */
378static int swapped_op[] = {Py_GT, Py_GE, Py_EQ, Py_NE, Py_LT, Py_LE};
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +0000379
Guido van Rossume797ec12001-01-17 15:24:28 +0000380/* Try a genuine rich comparison, returning an object. Return:
381 NULL for exception;
382 NotImplemented if this particular rich comparison is not implemented or
383 undefined;
384 some object not equal to NotImplemented if it is implemented
385 (this latter object may not be a Boolean).
386*/
387static PyObject *
388try_rich_compare(PyObject *v, PyObject *w, int op)
389{
390 richcmpfunc f;
391 PyObject *res;
392
Guido van Rossumd1f06b92001-01-24 22:14:43 +0000393 if ((f = RICHCOMPARE(v->ob_type)) != NULL) {
Guido van Rossume797ec12001-01-17 15:24:28 +0000394 res = (*f)(v, w, op);
395 if (res != Py_NotImplemented)
396 return res;
397 Py_DECREF(res);
398 }
Guido van Rossumd1f06b92001-01-24 22:14:43 +0000399 if ((f = RICHCOMPARE(w->ob_type)) != NULL) {
Guido van Rossume797ec12001-01-17 15:24:28 +0000400 return (*f)(w, v, swapped_op[op]);
401 }
402 res = Py_NotImplemented;
403 Py_INCREF(res);
404 return res;
405}
406
407/* Try a genuine rich comparison, returning an int. Return:
408 -1 for exception (including the case where try_rich_compare() returns an
409 object that's not a Boolean);
410 0 if the outcome is false;
411 1 if the outcome is true;
412 2 if this particular rich comparison is not implemented or undefined.
413*/
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +0000414static int
Guido van Rossume797ec12001-01-17 15:24:28 +0000415try_rich_compare_bool(PyObject *v, PyObject *w, int op)
416{
417 PyObject *res;
418 int ok;
419
Guido van Rossumd1f06b92001-01-24 22:14:43 +0000420 if (RICHCOMPARE(v->ob_type) == NULL && RICHCOMPARE(w->ob_type) == NULL)
Guido van Rossume797ec12001-01-17 15:24:28 +0000421 return 2; /* Shortcut, avoid INCREF+DECREF */
422 res = try_rich_compare(v, w, op);
423 if (res == NULL)
424 return -1;
425 if (res == Py_NotImplemented) {
426 Py_DECREF(res);
427 return 2;
428 }
429 ok = PyObject_IsTrue(res);
430 Py_DECREF(res);
431 return ok;
432}
433
434/* Try rich comparisons to determine a 3-way comparison. Return:
435 -2 for an exception;
436 -1 if v < w;
437 0 if v == w;
438 1 if v > w;
439 2 if this particular rich comparison is not implemented or undefined.
440*/
441static int
442try_rich_to_3way_compare(PyObject *v, PyObject *w)
443{
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000444 static struct { int op; int outcome; } tries[3] = {
445 /* Try this operator, and if it is true, use this outcome: */
446 {Py_EQ, 0},
447 {Py_LT, -1},
448 {Py_GT, 1},
449 };
450 int i;
451
Guido van Rossumd1f06b92001-01-24 22:14:43 +0000452 if (RICHCOMPARE(v->ob_type) == NULL && RICHCOMPARE(w->ob_type) == NULL)
Guido van Rossume797ec12001-01-17 15:24:28 +0000453 return 2; /* Shortcut */
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000454
455 for (i = 0; i < 3; i++) {
456 switch (try_rich_compare_bool(v, w, tries[i].op)) {
457 case -1:
458 return -1;
459 case 1:
460 return tries[i].outcome;
461 }
Guido van Rossume797ec12001-01-17 15:24:28 +0000462 }
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000463
464 return 2;
Guido van Rossume797ec12001-01-17 15:24:28 +0000465}
466
467/* Try a 3-way comparison, returning an int. Return:
468 -2 for an exception;
469 -1 if v < w;
470 0 if v == w;
471 1 if v > w;
472 2 if this particular 3-way comparison is not implemented or undefined.
473*/
474static int
475try_3way_compare(PyObject *v, PyObject *w)
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +0000476{
477 int c;
Guido van Rossume797ec12001-01-17 15:24:28 +0000478 cmpfunc f;
479
480 /* Comparisons involving instances are given to instance_compare,
481 which has the same return conventions as this function. */
482
483 if (PyInstance_Check(v))
484 return (*v->ob_type->tp_compare)(v, w);
485 if (PyInstance_Check(w))
486 return (*w->ob_type->tp_compare)(v, w);
487
488 /* If the types are equal, don't bother with coercions etc. */
489 if (v->ob_type == w->ob_type) {
490 if ((f = v->ob_type->tp_compare) == NULL)
491 return 2;
492 c = (*f)(v, w);
493 if (PyErr_Occurred())
494 return -2;
495 return c < 0 ? -1 : c > 0 ? 1 : 0;
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +0000496 }
Guido van Rossume797ec12001-01-17 15:24:28 +0000497
498 /* Try coercion; if it fails, give up */
499 c = PyNumber_CoerceEx(&v, &w);
500 if (c < 0)
501 return -2;
502 if (c > 0)
503 return 2;
504
505 /* Try v's comparison, if defined */
506 if ((f = v->ob_type->tp_compare) != NULL) {
507 c = (*f)(v, w);
508 Py_DECREF(v);
509 Py_DECREF(w);
510 if (PyErr_Occurred())
511 return -2;
512 return c < 0 ? -1 : c > 0 ? 1 : 0;
513 }
514
515 /* Try w's comparison, if defined */
516 if ((f = w->ob_type->tp_compare) != NULL) {
517 c = (*f)(w, v); /* swapped! */
518 Py_DECREF(v);
519 Py_DECREF(w);
520 if (PyErr_Occurred())
521 return -2;
522 return c < 0 ? 1 : c > 0 ? -1 : 0; /* negated! */
523 }
524
525 /* No comparison defined */
526 Py_DECREF(v);
527 Py_DECREF(w);
528 return 2;
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +0000529}
530
Guido van Rossume797ec12001-01-17 15:24:28 +0000531/* Final fallback 3-way comparison, returning an int. Return:
532 -2 if an error occurred;
533 -1 if v < w;
534 0 if v == w;
535 1 if v > w.
536*/
537static int
538default_3way_compare(PyObject *v, PyObject *w)
539{
540 int c;
Guido van Rossum8f9143d2001-01-22 15:59:32 +0000541 char *vname, *wname;
Guido van Rossume797ec12001-01-17 15:24:28 +0000542
543 if (v->ob_type == w->ob_type) {
Barry Warsawb0e754d2001-01-20 06:24:55 +0000544 /* When comparing these pointers, they must be cast to
545 * integer types (i.e. Py_uintptr_t, our spelling of C9X's
546 * uintptr_t). ANSI specifies that pointer compares other
547 * than == and != to non-related structures are undefined.
548 */
Barry Warsaw71ff8d52001-01-20 06:08:10 +0000549 Py_uintptr_t vv = (Py_uintptr_t)v;
550 Py_uintptr_t ww = (Py_uintptr_t)w;
Guido van Rossume797ec12001-01-17 15:24:28 +0000551 return (vv < ww) ? -1 : (vv > ww) ? 1 : 0;
552 }
553
554 /* Special case for Unicode */
555 if (PyUnicode_Check(v) || PyUnicode_Check(w)) {
556 c = PyUnicode_Compare(v, w);
557 if (!PyErr_Occurred())
558 return c;
559 /* TypeErrors are ignored: if Unicode coercion fails due
560 to one of the arguments not having the right type, we
561 continue as defined by the coercion protocol (see
562 above). Luckily, decoding errors are reported as
563 ValueErrors and are not masked by this technique. */
564 if (!PyErr_ExceptionMatches(PyExc_TypeError))
565 return -2;
566 PyErr_Clear();
567 }
568
Guido van Rossum0871e932001-01-22 19:28:09 +0000569 /* None is smaller than anything */
570 if (v == Py_None)
571 return -1;
572 if (w == Py_None)
573 return 1;
574
Guido van Rossume797ec12001-01-17 15:24:28 +0000575 /* different type: compare type names */
Guido van Rossum8f9143d2001-01-22 15:59:32 +0000576 if (v->ob_type->tp_as_number)
577 vname = "";
578 else
579 vname = v->ob_type->tp_name;
580 if (w->ob_type->tp_as_number)
581 wname = "";
582 else
583 wname = w->ob_type->tp_name;
584 c = strcmp(vname, wname);
585 if (c < 0)
586 return -1;
587 if (c > 0)
588 return 1;
589 /* Same type name, or (more likely) incomparable numeric types */
590 return ((Py_uintptr_t)(v->ob_type) < (
591 Py_uintptr_t)(w->ob_type)) ? -1 : 1;
Guido van Rossume797ec12001-01-17 15:24:28 +0000592}
593
594#define CHECK_TYPES(o) PyType_HasFeature((o)->ob_type, Py_TPFLAGS_CHECKTYPES)
595
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +0000596static int
Fred Drake100814d2000-07-09 15:48:49 +0000597do_cmp(PyObject *v, PyObject *w)
Guido van Rossum20566841995-01-12 11:26:10 +0000598{
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +0000599 int c;
600
Guido van Rossume797ec12001-01-17 15:24:28 +0000601 c = try_rich_to_3way_compare(v, w);
602 if (c < 2)
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +0000603 return c;
Guido van Rossume797ec12001-01-17 15:24:28 +0000604 c = try_3way_compare(v, w);
605 if (c < 2)
606 return c;
607 return default_3way_compare(v, w);
Guido van Rossum20566841995-01-12 11:26:10 +0000608}
609
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000610/* compare_nesting is incremented before calling compare (for
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000611 some types) and decremented on exit. If the count exceeds the
612 nesting limit, enable code to detect circular data structures.
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000613
614 This is a tunable parameter that should only affect the performance
615 of comparisons, nothing else. Setting it high makes comparing deeply
616 nested non-cyclical data structures faster, but makes comparing cyclical
617 data structures slower.
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000618*/
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000619#define NESTING_LIMIT 20
620
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000621static int compare_nesting = 0;
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000622
623static PyObject*
Fred Drake100814d2000-07-09 15:48:49 +0000624get_inprogress_dict(void)
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000625{
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000626 static PyObject *key;
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000627 PyObject *tstate_dict, *inprogress;
628
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000629 if (key == NULL) {
630 key = PyString_InternFromString("cmp_state");
631 if (key == NULL)
632 return NULL;
633 }
634
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000635 tstate_dict = PyThreadState_GetDict();
636 if (tstate_dict == NULL) {
637 PyErr_BadInternalCall();
638 return NULL;
639 }
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000640
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000641 inprogress = PyDict_GetItem(tstate_dict, key);
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000642 if (inprogress == NULL) {
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000643 inprogress = PyDict_New();
644 if (inprogress == NULL)
645 return NULL;
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000646 if (PyDict_SetItem(tstate_dict, key, inprogress) == -1) {
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000647 Py_DECREF(inprogress);
648 return NULL;
649 }
Jeremy Hyltona251ea02000-06-09 16:20:39 +0000650 Py_DECREF(inprogress);
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000651 }
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000652
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000653 return inprogress;
654}
655
656static PyObject *
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000657check_recursion(PyObject *v, PyObject *w, int op)
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000658{
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000659 PyObject *inprogress;
660 PyObject *token;
Barry Warsaw9d23a4e2000-08-18 05:01:19 +0000661 Py_uintptr_t iv = (Py_uintptr_t)v;
662 Py_uintptr_t iw = (Py_uintptr_t)w;
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000663 PyObject *x, *y, *z;
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000664
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000665 inprogress = get_inprogress_dict();
666 if (inprogress == NULL)
667 return NULL;
668
669 token = PyTuple_New(3);
670 if (token == NULL)
671 return NULL;
672
673 if (iv <= iw) {
674 PyTuple_SET_ITEM(token, 0, x = PyLong_FromVoidPtr((void *)v));
675 PyTuple_SET_ITEM(token, 1, y = PyLong_FromVoidPtr((void *)w));
676 if (op >= 0)
677 op = swapped_op[op];
678 } else {
679 PyTuple_SET_ITEM(token, 0, x = PyLong_FromVoidPtr((void *)w));
680 PyTuple_SET_ITEM(token, 1, y = PyLong_FromVoidPtr((void *)v));
681 }
682 PyTuple_SET_ITEM(token, 2, z = PyInt_FromLong((long)op));
683 if (x == NULL || y == NULL || z == NULL) {
684 Py_DECREF(token);
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000685 return NULL;
686 }
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000687
688 if (PyDict_GetItem(inprogress, token) != NULL) {
689 Py_DECREF(token);
690 return Py_None; /* Without INCREF! */
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000691 }
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000692
693 if (PyDict_SetItem(inprogress, token, token) < 0) {
694 Py_DECREF(token);
695 return NULL;
696 }
697
698 return token;
699}
700
701static void
702delete_token(PyObject *token)
703{
704 PyObject *inprogress;
705
706 if (token == NULL || token == Py_None)
707 return;
708 inprogress = get_inprogress_dict();
709 if (inprogress == NULL)
710 PyErr_Clear();
711 else
712 PyDict_DelItem(inprogress, token);
713 Py_DECREF(token);
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000714}
715
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000716int
Fred Drake100814d2000-07-09 15:48:49 +0000717PyObject_Compare(PyObject *v, PyObject *w)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000718{
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000719 PyTypeObject *vtp;
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000720 int result;
721
Jack Jansend49cbe12000-08-22 21:52:51 +0000722#if defined(USE_STACKCHECK)
Guido van Rossum9c0a99e2000-08-30 15:53:50 +0000723 if (PyOS_CheckStack()) {
Jack Jansend49cbe12000-08-22 21:52:51 +0000724 PyErr_SetString(PyExc_MemoryError, "Stack overflow");
725 return -1;
726 }
727#endif
Guido van Rossumc8b6df91997-05-23 00:06:51 +0000728 if (v == NULL || w == NULL) {
729 PyErr_BadInternalCall();
730 return -1;
731 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000732 if (v == w)
733 return 0;
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +0000734 vtp = v->ob_type;
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000735 compare_nesting++;
736 if (compare_nesting > NESTING_LIMIT &&
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +0000737 (vtp->tp_as_mapping
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000738 || (vtp->tp_as_sequence
739 && !PyString_Check(v)
740 && !PyTuple_Check(v)))) {
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +0000741 /* try to detect circular data structures */
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000742 PyObject *token = check_recursion(v, w, -1);
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000743
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000744 if (token == NULL) {
745 result = -1;
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000746 }
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000747 else if (token == Py_None) {
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000748 /* already comparing these objects. assume
749 they're equal until shown otherwise */
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +0000750 result = 0;
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000751 }
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000752 else {
753 result = do_cmp(v, w);
754 delete_token(token);
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000755 }
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000756 }
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +0000757 else {
758 result = do_cmp(v, w);
759 }
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000760 compare_nesting--;
Guido van Rossume797ec12001-01-17 15:24:28 +0000761 return result < 0 ? -1 : result;
762}
763
764static PyObject *
765try_3way_to_rich_compare(PyObject *v, PyObject *w, int op)
766{
767 int c;
768 PyObject *result;
769
770 c = try_3way_compare(v, w);
Guido van Rossume797ec12001-01-17 15:24:28 +0000771 if (c >= 2)
772 c = default_3way_compare(v, w);
Guido van Rossum2da0ea82001-02-22 22:18:04 +0000773 if (c <= -2)
774 return NULL;
Guido van Rossume797ec12001-01-17 15:24:28 +0000775 switch (op) {
776 case Py_LT: c = c < 0; break;
777 case Py_LE: c = c <= 0; break;
778 case Py_EQ: c = c == 0; break;
779 case Py_NE: c = c != 0; break;
780 case Py_GT: c = c > 0; break;
781 case Py_GE: c = c >= 0; break;
782 }
783 result = c ? Py_True : Py_False;
784 Py_INCREF(result);
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000785 return result;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000786}
787
Neil Schemenauerd38855c2001-01-21 16:25:18 +0000788static PyObject *
Guido van Rossume797ec12001-01-17 15:24:28 +0000789do_richcmp(PyObject *v, PyObject *w, int op)
790{
791 PyObject *res;
792
793 res = try_rich_compare(v, w, op);
794 if (res != Py_NotImplemented)
795 return res;
796 Py_DECREF(res);
797
798 return try_3way_to_rich_compare(v, w, op);
799}
800
801PyObject *
802PyObject_RichCompare(PyObject *v, PyObject *w, int op)
803{
804 PyObject *res;
805
806 assert(Py_LT <= op && op <= Py_GE);
807
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000808 compare_nesting++;
809 if (compare_nesting > NESTING_LIMIT &&
810 (v->ob_type->tp_as_mapping
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000811 || (v->ob_type->tp_as_sequence
812 && !PyString_Check(v)
813 && !PyTuple_Check(v)))) {
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000814 /* try to detect circular data structures */
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000815 PyObject *token = check_recursion(v, w, op);
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000816
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000817 if (token == NULL) {
818 res = NULL;
Guido van Rossume797ec12001-01-17 15:24:28 +0000819 }
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000820 else if (token == Py_None) {
821 /* already comparing these objects with this operator.
822 assume they're equal until shown otherwise */
823 if (op == Py_EQ)
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000824 res = Py_True;
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000825 else if (op == Py_NE)
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000826 res = Py_False;
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000827 else {
828 PyErr_SetString(PyExc_ValueError,
829 "can't order recursive values");
830 res = NULL;
831 }
832 Py_XINCREF(res);
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000833 }
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000834 else {
835 res = do_richcmp(v, w, op);
836 delete_token(token);
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000837 }
Guido van Rossume797ec12001-01-17 15:24:28 +0000838 }
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000839 else {
840 res = do_richcmp(v, w, op);
841 }
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000842 compare_nesting--;
Guido van Rossume797ec12001-01-17 15:24:28 +0000843 return res;
844}
845
846int
847PyObject_RichCompareBool(PyObject *v, PyObject *w, int op)
848{
849 PyObject *res = PyObject_RichCompare(v, w, op);
850 int ok;
851
852 if (res == NULL)
853 return -1;
854 ok = PyObject_IsTrue(res);
855 Py_DECREF(res);
856 return ok;
857}
Fred Drake13634cf2000-06-29 19:17:04 +0000858
859/* Set of hash utility functions to help maintaining the invariant that
860 iff a==b then hash(a)==hash(b)
861
862 All the utility functions (_Py_Hash*()) return "-1" to signify an error.
863*/
864
865long
Fred Drake100814d2000-07-09 15:48:49 +0000866_Py_HashDouble(double v)
Fred Drake13634cf2000-06-29 19:17:04 +0000867{
Tim Peters39dce292000-08-15 03:34:48 +0000868 double intpart, fractpart;
869 int expo;
870 long hipart;
871 long x; /* the final hash value */
872 /* This is designed so that Python numbers of different types
873 * that compare equal hash to the same value; otherwise comparisons
874 * of mapping keys will turn out weird.
875 */
876
877#ifdef MPW /* MPW C modf expects pointer to extended as second argument */
878{
879 extended e;
880 fractpart = modf(v, &e);
881 intpart = e;
882}
883#else
884 fractpart = modf(v, &intpart);
885#endif
886 if (fractpart == 0.0) {
887 /* This must return the same hash as an equal int or long. */
888 if (intpart > LONG_MAX || -intpart > LONG_MAX) {
889 /* Convert to long and use its hash. */
890 PyObject *plong; /* converted to Python long */
891 if (Py_IS_INFINITY(intpart))
892 /* can't convert to long int -- arbitrary */
893 v = v < 0 ? -271828.0 : 314159.0;
894 plong = PyLong_FromDouble(v);
895 if (plong == NULL)
896 return -1;
897 x = PyObject_Hash(plong);
898 Py_DECREF(plong);
899 return x;
900 }
901 /* Fits in a C long == a Python int, so is its own hash. */
902 x = (long)intpart;
903 if (x == -1)
904 x = -2;
905 return x;
906 }
907 /* The fractional part is non-zero, so we don't have to worry about
908 * making this match the hash of some other type.
909 * Use frexp to get at the bits in the double.
Fred Drake13634cf2000-06-29 19:17:04 +0000910 * Since the VAX D double format has 56 mantissa bits, which is the
911 * most of any double format in use, each of these parts may have as
912 * many as (but no more than) 56 significant bits.
Tim Peters39dce292000-08-15 03:34:48 +0000913 * So, assuming sizeof(long) >= 4, each part can be broken into two
914 * longs; frexp and multiplication are used to do that.
915 * Also, since the Cray double format has 15 exponent bits, which is
916 * the most of any double format in use, shifting the exponent field
917 * left by 15 won't overflow a long (again assuming sizeof(long) >= 4).
Fred Drake13634cf2000-06-29 19:17:04 +0000918 */
Tim Peters39dce292000-08-15 03:34:48 +0000919 v = frexp(v, &expo);
920 v *= 2147483648.0; /* 2**31 */
921 hipart = (long)v; /* take the top 32 bits */
922 v = (v - (double)hipart) * 2147483648.0; /* get the next 32 bits */
923 x = hipart + (long)v + (expo << 15);
924 if (x == -1)
925 x = -2;
926 return x;
Fred Drake13634cf2000-06-29 19:17:04 +0000927}
928
929long
Fred Drake100814d2000-07-09 15:48:49 +0000930_Py_HashPointer(void *p)
Fred Drake13634cf2000-06-29 19:17:04 +0000931{
932#if SIZEOF_LONG >= SIZEOF_VOID_P
933 return (long)p;
934#else
935 /* convert to a Python long and hash that */
936 PyObject* longobj;
937 long x;
938
939 if ((longobj = PyLong_FromVoidPtr(p)) == NULL) {
940 x = -1;
941 goto finally;
942 }
943 x = PyObject_Hash(longobj);
944
945finally:
946 Py_XDECREF(longobj);
947 return x;
948#endif
949}
950
951
Guido van Rossum9bfef441993-03-29 10:43:31 +0000952long
Fred Drake100814d2000-07-09 15:48:49 +0000953PyObject_Hash(PyObject *v)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000954{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000955 PyTypeObject *tp = v->ob_type;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000956 if (tp->tp_hash != NULL)
957 return (*tp->tp_hash)(v);
Guido van Rossumd1f06b92001-01-24 22:14:43 +0000958 if (tp->tp_compare == NULL && RICHCOMPARE(tp) == NULL) {
Fred Drake13634cf2000-06-29 19:17:04 +0000959 return _Py_HashPointer(v); /* Use address as hash value */
960 }
Guido van Rossum9bfef441993-03-29 10:43:31 +0000961 /* If there's a cmp but no hash defined, the object can't be hashed */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000962 PyErr_SetString(PyExc_TypeError, "unhashable type");
Guido van Rossum9bfef441993-03-29 10:43:31 +0000963 return -1;
964}
965
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000966PyObject *
Fred Drake100814d2000-07-09 15:48:49 +0000967PyObject_GetAttrString(PyObject *v, char *name)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000968{
Guido van Rossumd8eb1b31996-08-09 20:52:03 +0000969 if (v->ob_type->tp_getattro != NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000970 PyObject *w, *res;
Guido van Rossumc6d06701997-01-18 07:57:16 +0000971 w = PyString_InternFromString(name);
Guido van Rossumd8eb1b31996-08-09 20:52:03 +0000972 if (w == NULL)
973 return NULL;
974 res = (*v->ob_type->tp_getattro)(v, w);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000975 Py_XDECREF(w);
Guido van Rossumd8eb1b31996-08-09 20:52:03 +0000976 return res;
977 }
978
Guido van Rossum3f5da241990-12-20 15:06:42 +0000979 if (v->ob_type->tp_getattr == NULL) {
Guido van Rossumdb935161998-01-19 22:16:36 +0000980 PyErr_Format(PyExc_AttributeError,
981 "'%.50s' object has no attribute '%.400s'",
982 v->ob_type->tp_name,
983 name);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000984 return NULL;
985 }
986 else {
987 return (*v->ob_type->tp_getattr)(v, name);
988 }
989}
990
991int
Fred Drake100814d2000-07-09 15:48:49 +0000992PyObject_HasAttrString(PyObject *v, char *name)
Guido van Rossumed18fdc1993-07-11 19:55:34 +0000993{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000994 PyObject *res = PyObject_GetAttrString(v, name);
Guido van Rossumed18fdc1993-07-11 19:55:34 +0000995 if (res != NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000996 Py_DECREF(res);
Guido van Rossumed18fdc1993-07-11 19:55:34 +0000997 return 1;
998 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000999 PyErr_Clear();
Guido van Rossumed18fdc1993-07-11 19:55:34 +00001000 return 0;
1001}
1002
1003int
Fred Drake100814d2000-07-09 15:48:49 +00001004PyObject_SetAttrString(PyObject *v, char *name, PyObject *w)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001005{
Guido van Rossumd8eb1b31996-08-09 20:52:03 +00001006 if (v->ob_type->tp_setattro != NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001007 PyObject *s;
Guido van Rossumd8eb1b31996-08-09 20:52:03 +00001008 int res;
Guido van Rossumc6d06701997-01-18 07:57:16 +00001009 s = PyString_InternFromString(name);
Guido van Rossumd8eb1b31996-08-09 20:52:03 +00001010 if (s == NULL)
Guido van Rossumb7fc3041996-09-11 22:51:25 +00001011 return -1;
Guido van Rossumd8eb1b31996-08-09 20:52:03 +00001012 res = (*v->ob_type->tp_setattro)(v, s, w);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001013 Py_XDECREF(s);
Guido van Rossumd8eb1b31996-08-09 20:52:03 +00001014 return res;
1015 }
1016
Guido van Rossum3f5da241990-12-20 15:06:42 +00001017 if (v->ob_type->tp_setattr == NULL) {
1018 if (v->ob_type->tp_getattr == NULL)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001019 PyErr_SetString(PyExc_TypeError,
Guido van Rossum3ea74121991-12-24 13:28:03 +00001020 "attribute-less object (assign or del)");
Guido van Rossum3f5da241990-12-20 15:06:42 +00001021 else
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001022 PyErr_SetString(PyExc_TypeError,
Guido van Rossum3ea74121991-12-24 13:28:03 +00001023 "object has read-only attributes");
Guido van Rossum73531a31990-12-20 23:12:40 +00001024 return -1;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001025 }
1026 else {
1027 return (*v->ob_type->tp_setattr)(v, name, w);
1028 }
1029}
1030
Marc-André Lemburge44e5072000-09-18 16:20:57 +00001031/* Internal API needed by PyObject_GetAttr(): */
1032extern
1033PyObject *_PyUnicode_AsDefaultEncodedString(PyObject *unicode,
1034 const char *errors);
1035
Guido van Rossum98ff96a1997-05-20 18:34:44 +00001036PyObject *
Fred Drake100814d2000-07-09 15:48:49 +00001037PyObject_GetAttr(PyObject *v, PyObject *name)
Guido van Rossum98ff96a1997-05-20 18:34:44 +00001038{
Marc-André Lemburge44e5072000-09-18 16:20:57 +00001039 /* The Unicode to string conversion is done here because the
1040 existing tp_getattro slots expect a string object as name
1041 and we wouldn't want to break those. */
1042 if (PyUnicode_Check(name)) {
1043 name = _PyUnicode_AsDefaultEncodedString(name, NULL);
1044 if (name == NULL)
1045 return NULL;
1046 }
Jeremy Hylton99a8f902000-06-23 14:36:32 +00001047
1048 if (!PyString_Check(name)) {
1049 PyErr_SetString(PyExc_TypeError,
1050 "attribute name must be string");
1051 return NULL;
1052 }
Marc-André Lemburge44e5072000-09-18 16:20:57 +00001053 if (v->ob_type->tp_getattro != NULL)
1054 return (*v->ob_type->tp_getattro)(v, name);
1055 else
Jeremy Hylton99a8f902000-06-23 14:36:32 +00001056 return PyObject_GetAttrString(v, PyString_AS_STRING(name));
Guido van Rossum98ff96a1997-05-20 18:34:44 +00001057}
1058
1059int
Fred Drake100814d2000-07-09 15:48:49 +00001060PyObject_HasAttr(PyObject *v, PyObject *name)
Guido van Rossum98ff96a1997-05-20 18:34:44 +00001061{
1062 PyObject *res = PyObject_GetAttr(v, name);
1063 if (res != NULL) {
1064 Py_DECREF(res);
1065 return 1;
1066 }
1067 PyErr_Clear();
1068 return 0;
1069}
1070
1071int
Fred Drake100814d2000-07-09 15:48:49 +00001072PyObject_SetAttr(PyObject *v, PyObject *name, PyObject *value)
Guido van Rossum98ff96a1997-05-20 18:34:44 +00001073{
1074 int err;
Marc-André Lemburge44e5072000-09-18 16:20:57 +00001075
1076 /* The Unicode to string conversion is done here because the
1077 existing tp_setattro slots expect a string object as name
1078 and we wouldn't want to break those. */
1079 if (PyUnicode_Check(name)) {
1080 name = PyUnicode_AsEncodedString(name, NULL, NULL);
1081 if (name == NULL)
1082 return -1;
Jeremy Hylton99a8f902000-06-23 14:36:32 +00001083 }
Marc-André Lemburge44e5072000-09-18 16:20:57 +00001084 else
1085 Py_INCREF(name);
1086
1087 if (!PyString_Check(name)){
Jeremy Hylton99a8f902000-06-23 14:36:32 +00001088 PyErr_SetString(PyExc_TypeError,
1089 "attribute name must be string");
1090 err = -1;
1091 }
Marc-André Lemburge44e5072000-09-18 16:20:57 +00001092 else {
1093 PyString_InternInPlace(&name);
1094 if (v->ob_type->tp_setattro != NULL)
1095 err = (*v->ob_type->tp_setattro)(v, name, value);
1096 else
1097 err = PyObject_SetAttrString(v,
1098 PyString_AS_STRING(name), value);
1099 }
1100
Guido van Rossum98ff96a1997-05-20 18:34:44 +00001101 Py_DECREF(name);
1102 return err;
1103}
1104
Guido van Rossum6ac258d1993-05-12 08:24:20 +00001105/* Test a value used as condition, e.g., in a for or if statement.
1106 Return -1 if an error occurred */
1107
1108int
Fred Drake100814d2000-07-09 15:48:49 +00001109PyObject_IsTrue(PyObject *v)
Guido van Rossum6ac258d1993-05-12 08:24:20 +00001110{
1111 int res;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001112 if (v == Py_None)
Guido van Rossum6ac258d1993-05-12 08:24:20 +00001113 res = 0;
Guido van Rossum1c4f4581998-05-22 00:53:24 +00001114 else if (v->ob_type->tp_as_number != NULL &&
1115 v->ob_type->tp_as_number->nb_nonzero != NULL)
Guido van Rossum6ac258d1993-05-12 08:24:20 +00001116 res = (*v->ob_type->tp_as_number->nb_nonzero)(v);
Guido van Rossum1c4f4581998-05-22 00:53:24 +00001117 else if (v->ob_type->tp_as_mapping != NULL &&
1118 v->ob_type->tp_as_mapping->mp_length != NULL)
Guido van Rossum6ac258d1993-05-12 08:24:20 +00001119 res = (*v->ob_type->tp_as_mapping->mp_length)(v);
Guido van Rossum1c4f4581998-05-22 00:53:24 +00001120 else if (v->ob_type->tp_as_sequence != NULL &&
1121 v->ob_type->tp_as_sequence->sq_length != NULL)
Guido van Rossum6ac258d1993-05-12 08:24:20 +00001122 res = (*v->ob_type->tp_as_sequence->sq_length)(v);
1123 else
1124 res = 1;
1125 if (res > 0)
1126 res = 1;
1127 return res;
1128}
1129
Guido van Rossumc3d3f961998-04-09 17:53:59 +00001130/* equivalent of 'not v'
1131 Return -1 if an error occurred */
1132
1133int
Fred Drake100814d2000-07-09 15:48:49 +00001134PyObject_Not(PyObject *v)
Guido van Rossumc3d3f961998-04-09 17:53:59 +00001135{
1136 int res;
1137 res = PyObject_IsTrue(v);
1138 if (res < 0)
1139 return res;
1140 return res == 0;
1141}
1142
Guido van Rossum5524a591995-01-10 15:26:20 +00001143/* Coerce two numeric types to the "larger" one.
1144 Increment the reference count on each argument.
Guido van Rossume797ec12001-01-17 15:24:28 +00001145 Return value:
1146 -1 if an error occurred;
1147 0 if the coercion succeeded (and then the reference counts are increased);
1148 1 if no coercion is possible (and no error is raised).
Guido van Rossum5524a591995-01-10 15:26:20 +00001149*/
Guido van Rossum5524a591995-01-10 15:26:20 +00001150int
Fred Drake100814d2000-07-09 15:48:49 +00001151PyNumber_CoerceEx(PyObject **pv, PyObject **pw)
Guido van Rossum5524a591995-01-10 15:26:20 +00001152{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001153 register PyObject *v = *pv;
1154 register PyObject *w = *pw;
Guido van Rossum5524a591995-01-10 15:26:20 +00001155 int res;
1156
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001157 if (v->ob_type == w->ob_type && !PyInstance_Check(v)) {
1158 Py_INCREF(v);
1159 Py_INCREF(w);
Guido van Rossum5524a591995-01-10 15:26:20 +00001160 return 0;
1161 }
1162 if (v->ob_type->tp_as_number && v->ob_type->tp_as_number->nb_coerce) {
1163 res = (*v->ob_type->tp_as_number->nb_coerce)(pv, pw);
1164 if (res <= 0)
1165 return res;
1166 }
1167 if (w->ob_type->tp_as_number && w->ob_type->tp_as_number->nb_coerce) {
1168 res = (*w->ob_type->tp_as_number->nb_coerce)(pw, pv);
1169 if (res <= 0)
1170 return res;
1171 }
Guido van Rossum242c6421997-11-19 16:03:17 +00001172 return 1;
1173}
1174
Guido van Rossume797ec12001-01-17 15:24:28 +00001175/* Coerce two numeric types to the "larger" one.
1176 Increment the reference count on each argument.
1177 Return -1 and raise an exception if no coercion is possible
1178 (and then no reference count is incremented).
1179*/
Guido van Rossum242c6421997-11-19 16:03:17 +00001180int
Fred Drake100814d2000-07-09 15:48:49 +00001181PyNumber_Coerce(PyObject **pv, PyObject **pw)
Guido van Rossum242c6421997-11-19 16:03:17 +00001182{
1183 int err = PyNumber_CoerceEx(pv, pw);
1184 if (err <= 0)
1185 return err;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001186 PyErr_SetString(PyExc_TypeError, "number coercion failed");
Guido van Rossum5524a591995-01-10 15:26:20 +00001187 return -1;
1188}
1189
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001190
Guido van Rossum49b11fe1995-01-26 00:38:22 +00001191/* Test whether an object can be called */
1192
1193int
Fred Drake100814d2000-07-09 15:48:49 +00001194PyCallable_Check(PyObject *x)
Guido van Rossum49b11fe1995-01-26 00:38:22 +00001195{
1196 if (x == NULL)
1197 return 0;
1198 if (x->ob_type->tp_call != NULL ||
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001199 PyFunction_Check(x) ||
1200 PyMethod_Check(x) ||
1201 PyCFunction_Check(x) ||
1202 PyClass_Check(x))
Guido van Rossum49b11fe1995-01-26 00:38:22 +00001203 return 1;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001204 if (PyInstance_Check(x)) {
1205 PyObject *call = PyObject_GetAttrString(x, "__call__");
Guido van Rossum49b11fe1995-01-26 00:38:22 +00001206 if (call == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001207 PyErr_Clear();
Guido van Rossum49b11fe1995-01-26 00:38:22 +00001208 return 0;
1209 }
1210 /* Could test recursively but don't, for fear of endless
1211 recursion if some joker sets self.__call__ = self */
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001212 Py_DECREF(call);
Guido van Rossum49b11fe1995-01-26 00:38:22 +00001213 return 1;
1214 }
1215 return 0;
1216}
1217
1218
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001219/*
1220NoObject is usable as a non-NULL undefined value, used by the macro None.
1221There is (and should be!) no way to create other objects of this type,
Guido van Rossum3f5da241990-12-20 15:06:42 +00001222so there is exactly one (which is indestructible, by the way).
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001223*/
1224
Guido van Rossum0c182a11992-03-27 17:26:13 +00001225/* ARGSUSED */
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001226static PyObject *
Fred Drake100814d2000-07-09 15:48:49 +00001227none_repr(PyObject *op)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001228{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001229 return PyString_FromString("None");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001230}
1231
Barry Warsaw9bf16442001-01-23 16:24:35 +00001232/* ARGUSED */
1233static void
1234none_dealloc(PyObject* ignore)
1235{
1236 /* This should never get called, but we also don't want to SEGV if
1237 * we accidently decref None out of existance.
1238 */
1239 abort();
1240}
1241
1242
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001243static PyTypeObject PyNothing_Type = {
1244 PyObject_HEAD_INIT(&PyType_Type)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001245 0,
Guido van Rossum3f5da241990-12-20 15:06:42 +00001246 "None",
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001247 0,
1248 0,
Barry Warsaw9bf16442001-01-23 16:24:35 +00001249 (destructor)none_dealloc, /*tp_dealloc*/ /*never called*/
Guido van Rossum7066dd71992-09-17 17:54:56 +00001250 0, /*tp_print*/
Guido van Rossum3f5da241990-12-20 15:06:42 +00001251 0, /*tp_getattr*/
1252 0, /*tp_setattr*/
1253 0, /*tp_compare*/
Guido van Rossum1d5735e1994-08-30 08:27:36 +00001254 (reprfunc)none_repr, /*tp_repr*/
Guido van Rossum3f5da241990-12-20 15:06:42 +00001255 0, /*tp_as_number*/
1256 0, /*tp_as_sequence*/
1257 0, /*tp_as_mapping*/
Guido van Rossum9bfef441993-03-29 10:43:31 +00001258 0, /*tp_hash */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001259};
1260
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001261PyObject _Py_NoneStruct = {
1262 PyObject_HEAD_INIT(&PyNothing_Type)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001263};
1264
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +00001265/* NotImplemented is an object that can be used to signal that an
1266 operation is not implemented for the given type combination. */
1267
1268static PyObject *
1269NotImplemented_repr(PyObject *op)
1270{
1271 return PyString_FromString("NotImplemented");
1272}
1273
1274static PyTypeObject PyNotImplemented_Type = {
1275 PyObject_HEAD_INIT(&PyType_Type)
1276 0,
1277 "NotImplemented",
1278 0,
1279 0,
Barry Warsaw9bf16442001-01-23 16:24:35 +00001280 (destructor)none_dealloc, /*tp_dealloc*/ /*never called*/
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +00001281 0, /*tp_print*/
1282 0, /*tp_getattr*/
1283 0, /*tp_setattr*/
1284 0, /*tp_compare*/
1285 (reprfunc)NotImplemented_repr, /*tp_repr*/
1286 0, /*tp_as_number*/
1287 0, /*tp_as_sequence*/
1288 0, /*tp_as_mapping*/
1289 0, /*tp_hash */
1290};
1291
1292PyObject _Py_NotImplementedStruct = {
1293 PyObject_HEAD_INIT(&PyNotImplemented_Type)
1294};
1295
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001296
Guido van Rossum84a90321996-05-22 16:34:47 +00001297#ifdef Py_TRACE_REFS
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001298
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001299static PyObject refchain = {&refchain, &refchain};
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001300
Guido van Rossumaacdc9d1996-08-12 21:32:12 +00001301void
Fred Drake100814d2000-07-09 15:48:49 +00001302_Py_ResetReferences(void)
Guido van Rossume09fb551997-08-05 02:04:34 +00001303{
1304 refchain._ob_prev = refchain._ob_next = &refchain;
1305 _Py_RefTotal = 0;
1306}
1307
1308void
Fred Drake100814d2000-07-09 15:48:49 +00001309_Py_NewReference(PyObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001310{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001311 _Py_RefTotal++;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001312 op->ob_refcnt = 1;
1313 op->_ob_next = refchain._ob_next;
1314 op->_ob_prev = &refchain;
1315 refchain._ob_next->_ob_prev = op;
1316 refchain._ob_next = op;
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +00001317#ifdef COUNT_ALLOCS
1318 inc_count(op->ob_type);
1319#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001320}
1321
Guido van Rossumaacdc9d1996-08-12 21:32:12 +00001322void
Fred Drake100814d2000-07-09 15:48:49 +00001323_Py_ForgetReference(register PyObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001324{
Guido van Rossumbffd6832000-01-20 22:32:56 +00001325#ifdef SLOW_UNREF_CHECK
Guido van Rossum4c08d552000-03-10 22:55:18 +00001326 register PyObject *p;
Guido van Rossumbffd6832000-01-20 22:32:56 +00001327#endif
Guido van Rossumd7047b31995-01-02 19:07:15 +00001328 if (op->ob_refcnt < 0)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001329 Py_FatalError("UNREF negative refcnt");
Guido van Rossum2e8f6141992-09-03 20:32:55 +00001330 if (op == &refchain ||
Guido van Rossumd7047b31995-01-02 19:07:15 +00001331 op->_ob_prev->_ob_next != op || op->_ob_next->_ob_prev != op)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001332 Py_FatalError("UNREF invalid object");
Guido van Rossum2e8f6141992-09-03 20:32:55 +00001333#ifdef SLOW_UNREF_CHECK
Guido van Rossum3f5da241990-12-20 15:06:42 +00001334 for (p = refchain._ob_next; p != &refchain; p = p->_ob_next) {
1335 if (p == op)
1336 break;
1337 }
Guido van Rossumd7047b31995-01-02 19:07:15 +00001338 if (p == &refchain) /* Not found */
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001339 Py_FatalError("UNREF unknown object");
Guido van Rossum2e8f6141992-09-03 20:32:55 +00001340#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001341 op->_ob_next->_ob_prev = op->_ob_prev;
1342 op->_ob_prev->_ob_next = op->_ob_next;
Guido van Rossum2e8f6141992-09-03 20:32:55 +00001343 op->_ob_next = op->_ob_prev = NULL;
Guido van Rossumd8953cb1995-04-06 14:46:26 +00001344#ifdef COUNT_ALLOCS
1345 op->ob_type->tp_free++;
1346#endif
Guido van Rossum3f5da241990-12-20 15:06:42 +00001347}
1348
Guido van Rossumaacdc9d1996-08-12 21:32:12 +00001349void
Fred Drake100814d2000-07-09 15:48:49 +00001350_Py_Dealloc(PyObject *op)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001351{
Guido van Rossum9776adf1994-09-07 14:36:45 +00001352 destructor dealloc = op->ob_type->tp_dealloc;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001353 _Py_ForgetReference(op);
Guido van Rossum9776adf1994-09-07 14:36:45 +00001354 (*dealloc)(op);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001355}
1356
Guido van Rossumaacdc9d1996-08-12 21:32:12 +00001357void
Fred Drake100814d2000-07-09 15:48:49 +00001358_Py_PrintReferences(FILE *fp)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001359{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001360 PyObject *op;
Guido van Rossume09fb551997-08-05 02:04:34 +00001361 fprintf(fp, "Remaining objects:\n");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001362 for (op = refchain._ob_next; op != &refchain; op = op->_ob_next) {
1363 fprintf(fp, "[%d] ", op->ob_refcnt);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001364 if (PyObject_Print(op, fp, 0) != 0)
1365 PyErr_Clear();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001366 putc('\n', fp);
1367 }
1368}
1369
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +00001370PyObject *
Fred Drake100814d2000-07-09 15:48:49 +00001371_Py_GetObjects(PyObject *self, PyObject *args)
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +00001372{
1373 int i, n;
1374 PyObject *t = NULL;
1375 PyObject *res, *op;
1376
1377 if (!PyArg_ParseTuple(args, "i|O", &n, &t))
1378 return NULL;
1379 op = refchain._ob_next;
1380 res = PyList_New(0);
1381 if (res == NULL)
1382 return NULL;
1383 for (i = 0; (n == 0 || i < n) && op != &refchain; i++) {
1384 while (op == self || op == args || op == res || op == t ||
1385 t != NULL && op->ob_type != (PyTypeObject *) t) {
1386 op = op->_ob_next;
1387 if (op == &refchain)
1388 return res;
1389 }
1390 if (PyList_Append(res, op) < 0) {
1391 Py_DECREF(res);
1392 return NULL;
1393 }
1394 op = op->_ob_next;
1395 }
1396 return res;
1397}
1398
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001399#endif
Guido van Rossum97ead3f1996-01-12 01:24:09 +00001400
1401
1402/* Hack to force loading of cobject.o */
Guido van Rossumda9c2711996-12-05 21:58:58 +00001403PyTypeObject *_Py_cobject_hack = &PyCObject_Type;
Guido van Rossum84a90321996-05-22 16:34:47 +00001404
1405
1406/* Hack to force loading of abstract.o */
Jeremy Hylton6253f832000-07-12 12:56:19 +00001407int (*_Py_abstract_hack)(PyObject *) = &PyObject_Size;
Guido van Rossume09fb551997-08-05 02:04:34 +00001408
1409
Andrew M. Kuchling1582a3a2000-08-16 12:27:23 +00001410/* Python's malloc wrappers (see pymem.h) */
Guido van Rossume09fb551997-08-05 02:04:34 +00001411
Thomas Wouters334fb892000-07-25 12:56:38 +00001412void *
Fred Drake100814d2000-07-09 15:48:49 +00001413PyMem_Malloc(size_t nbytes)
Guido van Rossume09fb551997-08-05 02:04:34 +00001414{
1415#if _PyMem_EXTRA > 0
1416 if (nbytes == 0)
1417 nbytes = _PyMem_EXTRA;
1418#endif
Guido van Rossumb18618d2000-05-03 23:44:39 +00001419 return PyMem_MALLOC(nbytes);
Guido van Rossume09fb551997-08-05 02:04:34 +00001420}
1421
Thomas Wouters334fb892000-07-25 12:56:38 +00001422void *
1423PyMem_Realloc(void *p, size_t nbytes)
Guido van Rossume09fb551997-08-05 02:04:34 +00001424{
1425#if _PyMem_EXTRA > 0
1426 if (nbytes == 0)
1427 nbytes = _PyMem_EXTRA;
1428#endif
Guido van Rossumb18618d2000-05-03 23:44:39 +00001429 return PyMem_REALLOC(p, nbytes);
Guido van Rossume09fb551997-08-05 02:04:34 +00001430}
1431
1432void
Thomas Wouters334fb892000-07-25 12:56:38 +00001433PyMem_Free(void *p)
Guido van Rossume09fb551997-08-05 02:04:34 +00001434{
Guido van Rossumb18618d2000-05-03 23:44:39 +00001435 PyMem_FREE(p);
1436}
1437
1438
1439/* Python's object malloc wrappers (see objimpl.h) */
1440
Thomas Wouters334fb892000-07-25 12:56:38 +00001441void *
Fred Drake100814d2000-07-09 15:48:49 +00001442PyObject_Malloc(size_t nbytes)
Guido van Rossumb18618d2000-05-03 23:44:39 +00001443{
1444 return PyObject_MALLOC(nbytes);
1445}
1446
Thomas Wouters334fb892000-07-25 12:56:38 +00001447void *
1448PyObject_Realloc(void *p, size_t nbytes)
Guido van Rossumb18618d2000-05-03 23:44:39 +00001449{
1450 return PyObject_REALLOC(p, nbytes);
1451}
1452
1453void
Thomas Wouters334fb892000-07-25 12:56:38 +00001454PyObject_Free(void *p)
Guido van Rossumb18618d2000-05-03 23:44:39 +00001455{
1456 PyObject_FREE(p);
Guido van Rossume09fb551997-08-05 02:04:34 +00001457}
Guido van Rossum86610361998-04-10 22:32:46 +00001458
1459
Fred Drake41deb1e2001-02-01 05:27:45 +00001460/* Hook to clear up weak references only once the _weakref module is
1461 imported. We use a dummy implementation to simplify the code at each
1462 call site instead of requiring a test for NULL.
1463*/
1464
Fred Drakeb60654b2001-02-26 18:56:37 +00001465static void
Fred Drake41deb1e2001-02-01 05:27:45 +00001466empty_clear_weak_refs(PyObject *o)
1467{
Fred Drakeb60654b2001-02-26 18:56:37 +00001468 return;
Fred Drake41deb1e2001-02-01 05:27:45 +00001469}
1470
Fred Drakeb60654b2001-02-26 18:56:37 +00001471void (*PyObject_ClearWeakRefs)(PyObject *) = empty_clear_weak_refs;
Fred Drake41deb1e2001-02-01 05:27:45 +00001472
1473
1474
Guido van Rossum86610361998-04-10 22:32:46 +00001475/* These methods are used to control infinite recursion in repr, str, print,
1476 etc. Container objects that may recursively contain themselves,
1477 e.g. builtin dictionaries and lists, should used Py_ReprEnter() and
1478 Py_ReprLeave() to avoid infinite recursion.
1479
1480 Py_ReprEnter() returns 0 the first time it is called for a particular
1481 object and 1 every time thereafter. It returns -1 if an exception
1482 occurred. Py_ReprLeave() has no return value.
1483
1484 See dictobject.c and listobject.c for examples of use.
1485*/
1486
1487#define KEY "Py_Repr"
1488
1489int
Fred Drake100814d2000-07-09 15:48:49 +00001490Py_ReprEnter(PyObject *obj)
Guido van Rossum86610361998-04-10 22:32:46 +00001491{
1492 PyObject *dict;
1493 PyObject *list;
1494 int i;
1495
1496 dict = PyThreadState_GetDict();
1497 if (dict == NULL)
1498 return -1;
1499 list = PyDict_GetItemString(dict, KEY);
1500 if (list == NULL) {
1501 list = PyList_New(0);
1502 if (list == NULL)
1503 return -1;
1504 if (PyDict_SetItemString(dict, KEY, list) < 0)
1505 return -1;
1506 Py_DECREF(list);
1507 }
1508 i = PyList_GET_SIZE(list);
1509 while (--i >= 0) {
1510 if (PyList_GET_ITEM(list, i) == obj)
1511 return 1;
1512 }
1513 PyList_Append(list, obj);
1514 return 0;
1515}
1516
1517void
Fred Drake100814d2000-07-09 15:48:49 +00001518Py_ReprLeave(PyObject *obj)
Guido van Rossum86610361998-04-10 22:32:46 +00001519{
1520 PyObject *dict;
1521 PyObject *list;
1522 int i;
1523
1524 dict = PyThreadState_GetDict();
Guido van Rossumeb909461998-04-11 15:17:34 +00001525 if (dict == NULL)
1526 return;
Guido van Rossum86610361998-04-10 22:32:46 +00001527 list = PyDict_GetItemString(dict, KEY);
Guido van Rossumeb909461998-04-11 15:17:34 +00001528 if (list == NULL || !PyList_Check(list))
1529 return;
Guido van Rossum86610361998-04-10 22:32:46 +00001530 i = PyList_GET_SIZE(list);
1531 /* Count backwards because we always expect obj to be list[-1] */
1532 while (--i >= 0) {
1533 if (PyList_GET_ITEM(list, i) == obj) {
1534 PyList_SetSlice(list, i, i + 1, NULL);
1535 break;
1536 }
1537 }
1538}
Guido van Rossumd724b232000-03-13 16:01:29 +00001539
1540/*
1541 trashcan
1542 CT 2k0130
1543 non-recursively destroy nested objects
1544
1545 CT 2k0223
1546 everything is now done in a macro.
1547
1548 CT 2k0305
1549 modified to use functions, after Tim Peter's suggestion.
1550
1551 CT 2k0309
1552 modified to restore a possible error.
Guido van Rossum13ff8eb2000-03-25 18:39:19 +00001553
1554 CT 2k0325
1555 added better safe than sorry check for threadstate
Guido van Rossume92e6102000-04-24 15:40:53 +00001556
1557 CT 2k0422
1558 complete rewrite. We now build a chain via ob_type
1559 and save the limited number of types in ob_refcnt.
1560 This is perfect since we don't need any memory.
1561 A patch for free-threading would need just a lock.
Guido van Rossumd724b232000-03-13 16:01:29 +00001562*/
1563
Guido van Rossume92e6102000-04-24 15:40:53 +00001564#define Py_TRASHCAN_TUPLE 1
1565#define Py_TRASHCAN_LIST 2
1566#define Py_TRASHCAN_DICT 3
1567#define Py_TRASHCAN_FRAME 4
1568#define Py_TRASHCAN_TRACEBACK 5
1569/* extend here if other objects want protection */
1570
Guido van Rossumd724b232000-03-13 16:01:29 +00001571int _PyTrash_delete_nesting = 0;
Guido van Rossume92e6102000-04-24 15:40:53 +00001572
Guido van Rossumd724b232000-03-13 16:01:29 +00001573PyObject * _PyTrash_delete_later = NULL;
1574
1575void
Fred Drake100814d2000-07-09 15:48:49 +00001576_PyTrash_deposit_object(PyObject *op)
Guido van Rossumd724b232000-03-13 16:01:29 +00001577{
Guido van Rossume92e6102000-04-24 15:40:53 +00001578 int typecode;
Guido van Rossum13ff8eb2000-03-25 18:39:19 +00001579
Guido van Rossume92e6102000-04-24 15:40:53 +00001580 if (PyTuple_Check(op))
1581 typecode = Py_TRASHCAN_TUPLE;
1582 else if (PyList_Check(op))
1583 typecode = Py_TRASHCAN_LIST;
1584 else if (PyDict_Check(op))
1585 typecode = Py_TRASHCAN_DICT;
1586 else if (PyFrame_Check(op))
1587 typecode = Py_TRASHCAN_FRAME;
1588 else if (PyTraceBack_Check(op))
1589 typecode = Py_TRASHCAN_TRACEBACK;
Moshe Zadkacf703f02000-08-04 15:36:13 +00001590 else /* We have a bug here -- those are the only types in GC */ {
1591 Py_FatalError("Type not supported in GC -- internal bug");
1592 return; /* pacify compiler -- execution never here */
1593 }
Guido van Rossume92e6102000-04-24 15:40:53 +00001594 op->ob_refcnt = typecode;
Guido van Rossumd724b232000-03-13 16:01:29 +00001595
Guido van Rossume92e6102000-04-24 15:40:53 +00001596 op->ob_type = (PyTypeObject*)_PyTrash_delete_later;
1597 _PyTrash_delete_later = op;
Guido van Rossumd724b232000-03-13 16:01:29 +00001598}
1599
1600void
Fred Drake100814d2000-07-09 15:48:49 +00001601_PyTrash_destroy_chain(void)
Guido van Rossumd724b232000-03-13 16:01:29 +00001602{
1603 while (_PyTrash_delete_later) {
1604 PyObject *shredder = _PyTrash_delete_later;
Guido van Rossume92e6102000-04-24 15:40:53 +00001605 _PyTrash_delete_later = (PyObject*) shredder->ob_type;
1606
1607 switch (shredder->ob_refcnt) {
1608 case Py_TRASHCAN_TUPLE:
1609 shredder->ob_type = &PyTuple_Type;
1610 break;
1611 case Py_TRASHCAN_LIST:
1612 shredder->ob_type = &PyList_Type;
1613 break;
1614 case Py_TRASHCAN_DICT:
1615 shredder->ob_type = &PyDict_Type;
1616 break;
1617 case Py_TRASHCAN_FRAME:
1618 shredder->ob_type = &PyFrame_Type;
1619 break;
1620 case Py_TRASHCAN_TRACEBACK:
1621 shredder->ob_type = &PyTraceBack_Type;
1622 break;
1623 }
1624 _Py_NewReference(shredder);
1625
Guido van Rossumd724b232000-03-13 16:01:29 +00001626 ++_PyTrash_delete_nesting;
1627 Py_DECREF(shredder);
1628 --_PyTrash_delete_nesting;
1629 }
1630}
Neil Schemenauera35c6882001-02-27 04:45:05 +00001631
1632#ifdef WITH_PYMALLOC
1633#include "obmalloc.c"
1634#endif