blob: 40c939a20d788424c65c0deae8afee81d7721d31 [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001
Guido van Rossum3f5da241990-12-20 15:06:42 +00002/* Generic object operations; and implementation of None (NoObject) */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003
Guido van Rossumc0b618a1997-05-02 03:12:38 +00004#include "Python.h"
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00005
Jack Jansen28fc8802000-07-11 21:47:20 +00006#ifdef macintosh
7#include "macglue.h"
8#endif
9
Guido van Rossume92e6102000-04-24 15:40:53 +000010/* just for trashcan: */
11#include "compile.h"
12#include "frameobject.h"
13#include "traceback.h"
14
Guido van Rossum6f9e4331995-03-29 16:57:48 +000015#if defined( Py_TRACE_REFS ) || defined( Py_REF_DEBUG )
Guido van Rossumbffd6832000-01-20 22:32:56 +000016DL_IMPORT(long) _Py_RefTotal;
Guido van Rossum3f5da241990-12-20 15:06:42 +000017#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000018
Guido van Rossum3f5da241990-12-20 15:06:42 +000019/* Object allocation routines used by NEWOBJ and NEWVAROBJ macros.
20 These are used by the individual routines for object creation.
21 Do not call them otherwise, they do not initialize the object! */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000022
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +000023#ifdef COUNT_ALLOCS
Guido van Rossumc0b618a1997-05-02 03:12:38 +000024static PyTypeObject *type_list;
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +000025extern int tuple_zero_allocs, fast_tuple_allocs;
26extern int quick_int_allocs, quick_neg_int_allocs;
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +000027extern int null_strings, one_strings;
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +000028void
Fred Drake100814d2000-07-09 15:48:49 +000029dump_counts(void)
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +000030{
Guido van Rossumc0b618a1997-05-02 03:12:38 +000031 PyTypeObject *tp;
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +000032
33 for (tp = type_list; tp; tp = tp->tp_next)
Sjoerd Mullender52c1f511993-10-25 08:40:52 +000034 fprintf(stderr, "%s alloc'd: %d, freed: %d, max in use: %d\n",
35 tp->tp_name, tp->tp_alloc, tp->tp_free,
36 tp->tp_maxalloc);
37 fprintf(stderr, "fast tuple allocs: %d, empty: %d\n",
38 fast_tuple_allocs, tuple_zero_allocs);
39 fprintf(stderr, "fast int allocs: pos: %d, neg: %d\n",
40 quick_int_allocs, quick_neg_int_allocs);
41 fprintf(stderr, "null strings: %d, 1-strings: %d\n",
42 null_strings, one_strings);
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +000043}
44
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +000045PyObject *
Fred Drake100814d2000-07-09 15:48:49 +000046get_counts(void)
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +000047{
48 PyTypeObject *tp;
49 PyObject *result;
50 PyObject *v;
51
52 result = PyList_New(0);
53 if (result == NULL)
54 return NULL;
55 for (tp = type_list; tp; tp = tp->tp_next) {
56 v = Py_BuildValue("(siii)", tp->tp_name, tp->tp_alloc,
57 tp->tp_free, tp->tp_maxalloc);
58 if (v == NULL) {
59 Py_DECREF(result);
60 return NULL;
61 }
62 if (PyList_Append(result, v) < 0) {
63 Py_DECREF(v);
64 Py_DECREF(result);
65 return NULL;
66 }
67 Py_DECREF(v);
68 }
69 return result;
70}
71
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +000072void
Fred Drake100814d2000-07-09 15:48:49 +000073inc_count(PyTypeObject *tp)
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +000074{
75 if (tp->tp_alloc == 0) {
Guido van Rossumd8953cb1995-04-06 14:46:26 +000076 /* first time; insert in linked list */
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +000077 if (tp->tp_next != NULL) /* sanity check */
Guido van Rossumc0b618a1997-05-02 03:12:38 +000078 Py_FatalError("XXX inc_count sanity check");
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +000079 tp->tp_next = type_list;
80 type_list = tp;
81 }
82 tp->tp_alloc++;
83 if (tp->tp_alloc - tp->tp_free > tp->tp_maxalloc)
84 tp->tp_maxalloc = tp->tp_alloc - tp->tp_free;
85}
86#endif
87
Guido van Rossumc0b618a1997-05-02 03:12:38 +000088PyObject *
Fred Drake100814d2000-07-09 15:48:49 +000089PyObject_Init(PyObject *op, PyTypeObject *tp)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000090{
Guido van Rossumb18618d2000-05-03 23:44:39 +000091 if (op == NULL) {
92 PyErr_SetString(PyExc_SystemError,
93 "NULL object passed to PyObject_Init");
94 return op;
95 }
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +000096#ifdef WITH_CYCLE_GC
97 if (PyType_IS_GC(tp))
98 op = (PyObject *) PyObject_FROM_GC(op);
99#endif
Guido van Rossumb18618d2000-05-03 23:44:39 +0000100 /* Any changes should be reflected in PyObject_INIT (objimpl.h) */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000101 op->ob_type = tp;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000102 _Py_NewReference(op);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000103 return op;
104}
105
Guido van Rossumb18618d2000-05-03 23:44:39 +0000106PyVarObject *
Fred Drake100814d2000-07-09 15:48:49 +0000107PyObject_InitVar(PyVarObject *op, PyTypeObject *tp, int size)
Guido van Rossumb18618d2000-05-03 23:44:39 +0000108{
109 if (op == NULL) {
110 PyErr_SetString(PyExc_SystemError,
111 "NULL object passed to PyObject_InitVar");
112 return op;
113 }
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000114#ifdef WITH_CYCLE_GC
115 if (PyType_IS_GC(tp))
116 op = (PyVarObject *) PyObject_FROM_GC(op);
117#endif
Guido van Rossumb18618d2000-05-03 23:44:39 +0000118 /* Any changes should be reflected in PyObject_INIT_VAR */
119 op->ob_size = size;
120 op->ob_type = tp;
121 _Py_NewReference((PyObject *)op);
122 return op;
123}
124
125PyObject *
Fred Drake100814d2000-07-09 15:48:49 +0000126_PyObject_New(PyTypeObject *tp)
Guido van Rossumb18618d2000-05-03 23:44:39 +0000127{
128 PyObject *op;
129 op = (PyObject *) PyObject_MALLOC(_PyObject_SIZE(tp));
130 if (op == NULL)
131 return PyErr_NoMemory();
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000132#ifdef WITH_CYCLE_GC
133 if (PyType_IS_GC(tp))
134 op = (PyObject *) PyObject_FROM_GC(op);
135#endif
Guido van Rossumb18618d2000-05-03 23:44:39 +0000136 return PyObject_INIT(op, tp);
137}
138
Guido van Rossumd0c87ee1997-05-15 21:31:03 +0000139PyVarObject *
Fred Drake100814d2000-07-09 15:48:49 +0000140_PyObject_NewVar(PyTypeObject *tp, int size)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000141{
Guido van Rossumb18618d2000-05-03 23:44:39 +0000142 PyVarObject *op;
143 op = (PyVarObject *) PyObject_MALLOC(_PyObject_VAR_SIZE(tp, size));
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000144 if (op == NULL)
Guido van Rossumd0c87ee1997-05-15 21:31:03 +0000145 return (PyVarObject *)PyErr_NoMemory();
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000146#ifdef WITH_CYCLE_GC
147 if (PyType_IS_GC(tp))
148 op = (PyVarObject *) PyObject_FROM_GC(op);
149#endif
Guido van Rossumb18618d2000-05-03 23:44:39 +0000150 return PyObject_INIT_VAR(op, tp, size);
151}
152
153void
Fred Drake100814d2000-07-09 15:48:49 +0000154_PyObject_Del(PyObject *op)
Guido van Rossumb18618d2000-05-03 23:44:39 +0000155{
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000156#ifdef WITH_CYCLE_GC
Guido van Rossum4cc6ac72000-07-01 01:00:38 +0000157 if (op && PyType_IS_GC(op->ob_type)) {
158 op = (PyObject *) PyObject_AS_GC(op);
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000159 }
Guido van Rossum4cc6ac72000-07-01 01:00:38 +0000160#endif
161 PyObject_FREE(op);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000162}
163
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000164#ifndef WITH_CYCLE_GC
165/* extension modules might need these */
166void _PyGC_Insert(PyObject *op) { }
167void _PyGC_Remove(PyObject *op) { }
168#endif
169
Guido van Rossum90933611991-06-07 16:10:43 +0000170int
Fred Drake100814d2000-07-09 15:48:49 +0000171PyObject_Print(PyObject *op, FILE *fp, int flags)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000172{
Guido van Rossum278ef591991-07-27 21:40:24 +0000173 int ret = 0;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000174 if (PyErr_CheckSignals())
Guido van Rossum90933611991-06-07 16:10:43 +0000175 return -1;
Guido van Rossum9b00dfa1998-04-28 16:06:54 +0000176#ifdef USE_STACKCHECK
177 if (PyOS_CheckStack()) {
Fred Drake661ea262000-10-24 19:57:45 +0000178 PyErr_SetString(PyExc_MemoryError, "stack overflow");
Guido van Rossum9b00dfa1998-04-28 16:06:54 +0000179 return -1;
180 }
181#endif
Guido van Rossum687ef6e2000-01-12 16:28:58 +0000182 clearerr(fp); /* Clear any previous error condition */
Guido van Rossum90933611991-06-07 16:10:43 +0000183 if (op == NULL) {
184 fprintf(fp, "<nil>");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000185 }
Guido van Rossum90933611991-06-07 16:10:43 +0000186 else {
187 if (op->ob_refcnt <= 0)
Fred Drakea44d3532000-06-30 15:01:00 +0000188 fprintf(fp, "<refcnt %u at %p>",
189 op->ob_refcnt, op);
Guido van Rossum2e8f6141992-09-03 20:32:55 +0000190 else if (op->ob_type->tp_print == NULL) {
191 if (op->ob_type->tp_repr == NULL) {
Fred Drakea44d3532000-06-30 15:01:00 +0000192 fprintf(fp, "<%s object at %p>",
193 op->ob_type->tp_name, op);
Guido van Rossum2e8f6141992-09-03 20:32:55 +0000194 }
195 else {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000196 PyObject *s;
197 if (flags & Py_PRINT_RAW)
198 s = PyObject_Str(op);
Guido van Rossumc6004111993-11-05 10:22:19 +0000199 else
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000200 s = PyObject_Repr(op);
Guido van Rossum2e8f6141992-09-03 20:32:55 +0000201 if (s == NULL)
202 ret = -1;
Guido van Rossum2e8f6141992-09-03 20:32:55 +0000203 else {
Guido van Rossum565798d1998-04-21 22:25:01 +0000204 ret = PyObject_Print(s, fp,
205 Py_PRINT_RAW);
Guido van Rossum2e8f6141992-09-03 20:32:55 +0000206 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000207 Py_XDECREF(s);
Guido van Rossum2e8f6141992-09-03 20:32:55 +0000208 }
209 }
Guido van Rossum90933611991-06-07 16:10:43 +0000210 else
Guido van Rossum278ef591991-07-27 21:40:24 +0000211 ret = (*op->ob_type->tp_print)(op, fp, flags);
Guido van Rossum90933611991-06-07 16:10:43 +0000212 }
Guido van Rossum278ef591991-07-27 21:40:24 +0000213 if (ret == 0) {
214 if (ferror(fp)) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000215 PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossum278ef591991-07-27 21:40:24 +0000216 clearerr(fp);
217 ret = -1;
218 }
219 }
220 return ret;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000221}
222
Barry Warsaw9bf16442001-01-23 16:24:35 +0000223/* For debugging convenience. See Misc/gdbinit for some useful gdb hooks */
Barry Warsawbbd89b62001-01-24 04:18:13 +0000224void _PyObject_Dump(PyObject* op)
Barry Warsaw9bf16442001-01-23 16:24:35 +0000225{
226 (void)PyObject_Print(op, stderr, 0);
227 fprintf(stderr, "\nrefcounts: %d\n", op->ob_refcnt);
Barry Warsaw903138f2001-01-23 16:33:18 +0000228 fprintf(stderr, "address : %p\n", op);
Barry Warsaw9bf16442001-01-23 16:24:35 +0000229}
Barry Warsaw903138f2001-01-23 16:33:18 +0000230
231#ifdef WITH_CYCLE_GC
Barry Warsawbbd89b62001-01-24 04:18:13 +0000232void _PyGC_Dump(PyGC_Head* op)
Barry Warsaw9bf16442001-01-23 16:24:35 +0000233{
Barry Warsawbbd89b62001-01-24 04:18:13 +0000234 _PyObject_Dump(PyObject_FROM_GC(op));
Barry Warsaw9bf16442001-01-23 16:24:35 +0000235}
Barry Warsaw903138f2001-01-23 16:33:18 +0000236#endif /* WITH_CYCLE_GC */
Barry Warsaw9bf16442001-01-23 16:24:35 +0000237
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000238PyObject *
Fred Drake100814d2000-07-09 15:48:49 +0000239PyObject_Repr(PyObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000240{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000241 if (PyErr_CheckSignals())
Guido van Rossum90933611991-06-07 16:10:43 +0000242 return NULL;
Guido van Rossum9b00dfa1998-04-28 16:06:54 +0000243#ifdef USE_STACKCHECK
244 if (PyOS_CheckStack()) {
Fred Drake661ea262000-10-24 19:57:45 +0000245 PyErr_SetString(PyExc_MemoryError, "stack overflow");
Guido van Rossum9b00dfa1998-04-28 16:06:54 +0000246 return NULL;
247 }
248#endif
Guido van Rossum90933611991-06-07 16:10:43 +0000249 if (v == NULL)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000250 return PyString_FromString("<NULL>");
Guido van Rossum90933611991-06-07 16:10:43 +0000251 else if (v->ob_type->tp_repr == NULL) {
252 char buf[120];
Fred Drakea44d3532000-06-30 15:01:00 +0000253 sprintf(buf, "<%.80s object at %p>",
254 v->ob_type->tp_name, v);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000255 return PyString_FromString(buf);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000256 }
Guido van Rossum4c08d552000-03-10 22:55:18 +0000257 else {
258 PyObject *res;
259 res = (*v->ob_type->tp_repr)(v);
260 if (res == NULL)
261 return NULL;
Fredrik Lundhefecc7d2000-07-01 14:31:09 +0000262 if (PyUnicode_Check(res)) {
263 PyObject* str;
Fredrik Lundh2a1e0602000-07-08 17:43:32 +0000264 str = PyUnicode_AsUnicodeEscapeString(res);
Marc-André Lemburg891bc652000-07-03 09:57:53 +0000265 Py_DECREF(res);
266 if (str)
Fredrik Lundhefecc7d2000-07-01 14:31:09 +0000267 res = str;
Marc-André Lemburg891bc652000-07-03 09:57:53 +0000268 else
269 return NULL;
Fredrik Lundhefecc7d2000-07-01 14:31:09 +0000270 }
Guido van Rossum4c08d552000-03-10 22:55:18 +0000271 if (!PyString_Check(res)) {
272 PyErr_Format(PyExc_TypeError,
Guido van Rossum5db862d2000-04-10 12:46:51 +0000273 "__repr__ returned non-string (type %.200s)",
Guido van Rossum4c08d552000-03-10 22:55:18 +0000274 res->ob_type->tp_name);
275 Py_DECREF(res);
276 return NULL;
277 }
278 return res;
279 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000280}
281
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000282PyObject *
Fred Drake100814d2000-07-09 15:48:49 +0000283PyObject_Str(PyObject *v)
Guido van Rossumc6004111993-11-05 10:22:19 +0000284{
Guido van Rossum4c08d552000-03-10 22:55:18 +0000285 PyObject *res;
286
Guido van Rossumc6004111993-11-05 10:22:19 +0000287 if (v == NULL)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000288 return PyString_FromString("<NULL>");
289 else if (PyString_Check(v)) {
290 Py_INCREF(v);
Guido van Rossumc6004111993-11-05 10:22:19 +0000291 return v;
292 }
Guido van Rossum32b582b1995-01-17 16:35:13 +0000293 else if (v->ob_type->tp_str != NULL)
Guido van Rossum4c08d552000-03-10 22:55:18 +0000294 res = (*v->ob_type->tp_str)(v);
Guido van Rossumc6004111993-11-05 10:22:19 +0000295 else {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000296 PyObject *func;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000297 if (!PyInstance_Check(v) ||
298 (func = PyObject_GetAttrString(v, "__str__")) == NULL) {
299 PyErr_Clear();
300 return PyObject_Repr(v);
Guido van Rossumc6004111993-11-05 10:22:19 +0000301 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000302 res = PyEval_CallObject(func, (PyObject *)NULL);
303 Py_DECREF(func);
Guido van Rossumc6004111993-11-05 10:22:19 +0000304 }
Guido van Rossum4c08d552000-03-10 22:55:18 +0000305 if (res == NULL)
306 return NULL;
Marc-André Lemburg891bc652000-07-03 09:57:53 +0000307 if (PyUnicode_Check(res)) {
308 PyObject* str;
309 str = PyUnicode_AsEncodedString(res, NULL, NULL);
310 Py_DECREF(res);
311 if (str)
312 res = str;
313 else
314 return NULL;
315 }
Guido van Rossum4c08d552000-03-10 22:55:18 +0000316 if (!PyString_Check(res)) {
317 PyErr_Format(PyExc_TypeError,
Guido van Rossum5db862d2000-04-10 12:46:51 +0000318 "__str__ returned non-string (type %.200s)",
Guido van Rossum4c08d552000-03-10 22:55:18 +0000319 res->ob_type->tp_name);
320 Py_DECREF(res);
321 return NULL;
322 }
323 return res;
Guido van Rossumc6004111993-11-05 10:22:19 +0000324}
325
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000326PyObject *
327PyObject_Unicode(PyObject *v)
328{
329 PyObject *res;
330
331 if (v == NULL)
332 res = PyString_FromString("<NULL>");
333 else if (PyUnicode_Check(v)) {
334 Py_INCREF(v);
335 return v;
336 }
337 else if (PyString_Check(v))
338 res = v;
339 else if (v->ob_type->tp_str != NULL)
340 res = (*v->ob_type->tp_str)(v);
341 else {
342 PyObject *func;
343 static PyObject *strstr;
344 if (strstr == NULL) {
345 strstr= PyString_InternFromString("__str__");
346 if (strstr == NULL)
347 return NULL;
348 }
349 if (!PyInstance_Check(v) ||
350 (func = PyObject_GetAttr(v, strstr)) == NULL) {
351 PyErr_Clear();
352 res = PyObject_Repr(v);
353 }
354 else {
355 res = PyEval_CallObject(func, (PyObject *)NULL);
356 Py_DECREF(func);
357 }
358 }
359 if (res == NULL)
360 return NULL;
361 if (!PyUnicode_Check(res)) {
362 PyObject* str;
363 str = PyUnicode_FromObject(res);
364 Py_DECREF(res);
365 if (str)
366 res = str;
367 else
368 return NULL;
369 }
370 return res;
371}
372
373
Guido van Rossumd1f06b92001-01-24 22:14:43 +0000374/* Macro to get the tp_richcompare field of a type if defined */
375#define RICHCOMPARE(t) (PyType_HasFeature((t), Py_TPFLAGS_HAVE_RICHCOMPARE) \
376 ? (t)->tp_richcompare : NULL)
377
Guido van Rossume797ec12001-01-17 15:24:28 +0000378/* Map rich comparison operators to their swapped version, e.g. LT --> GT */
379static int swapped_op[] = {Py_GT, Py_GE, Py_EQ, Py_NE, Py_LT, Py_LE};
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +0000380
Guido van Rossume797ec12001-01-17 15:24:28 +0000381/* Try a genuine rich comparison, returning an object. Return:
382 NULL for exception;
383 NotImplemented if this particular rich comparison is not implemented or
384 undefined;
385 some object not equal to NotImplemented if it is implemented
386 (this latter object may not be a Boolean).
387*/
388static PyObject *
389try_rich_compare(PyObject *v, PyObject *w, int op)
390{
391 richcmpfunc f;
392 PyObject *res;
393
Guido van Rossumd1f06b92001-01-24 22:14:43 +0000394 if ((f = RICHCOMPARE(v->ob_type)) != NULL) {
Guido van Rossume797ec12001-01-17 15:24:28 +0000395 res = (*f)(v, w, op);
396 if (res != Py_NotImplemented)
397 return res;
398 Py_DECREF(res);
399 }
Guido van Rossumd1f06b92001-01-24 22:14:43 +0000400 if ((f = RICHCOMPARE(w->ob_type)) != NULL) {
Guido van Rossume797ec12001-01-17 15:24:28 +0000401 return (*f)(w, v, swapped_op[op]);
402 }
403 res = Py_NotImplemented;
404 Py_INCREF(res);
405 return res;
406}
407
408/* Try a genuine rich comparison, returning an int. Return:
409 -1 for exception (including the case where try_rich_compare() returns an
410 object that's not a Boolean);
411 0 if the outcome is false;
412 1 if the outcome is true;
413 2 if this particular rich comparison is not implemented or undefined.
414*/
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +0000415static int
Guido van Rossume797ec12001-01-17 15:24:28 +0000416try_rich_compare_bool(PyObject *v, PyObject *w, int op)
417{
418 PyObject *res;
419 int ok;
420
Guido van Rossumd1f06b92001-01-24 22:14:43 +0000421 if (RICHCOMPARE(v->ob_type) == NULL && RICHCOMPARE(w->ob_type) == NULL)
Guido van Rossume797ec12001-01-17 15:24:28 +0000422 return 2; /* Shortcut, avoid INCREF+DECREF */
423 res = try_rich_compare(v, w, op);
424 if (res == NULL)
425 return -1;
426 if (res == Py_NotImplemented) {
427 Py_DECREF(res);
428 return 2;
429 }
430 ok = PyObject_IsTrue(res);
431 Py_DECREF(res);
432 return ok;
433}
434
435/* Try rich comparisons to determine a 3-way comparison. Return:
436 -2 for an exception;
437 -1 if v < w;
438 0 if v == w;
439 1 if v > w;
440 2 if this particular rich comparison is not implemented or undefined.
441*/
442static int
443try_rich_to_3way_compare(PyObject *v, PyObject *w)
444{
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000445 static struct { int op; int outcome; } tries[3] = {
446 /* Try this operator, and if it is true, use this outcome: */
447 {Py_EQ, 0},
448 {Py_LT, -1},
449 {Py_GT, 1},
450 };
451 int i;
452
Guido van Rossumd1f06b92001-01-24 22:14:43 +0000453 if (RICHCOMPARE(v->ob_type) == NULL && RICHCOMPARE(w->ob_type) == NULL)
Guido van Rossume797ec12001-01-17 15:24:28 +0000454 return 2; /* Shortcut */
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000455
456 for (i = 0; i < 3; i++) {
457 switch (try_rich_compare_bool(v, w, tries[i].op)) {
458 case -1:
459 return -1;
460 case 1:
461 return tries[i].outcome;
462 }
Guido van Rossume797ec12001-01-17 15:24:28 +0000463 }
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000464
465 return 2;
Guido van Rossume797ec12001-01-17 15:24:28 +0000466}
467
468/* Try a 3-way comparison, returning an int. Return:
469 -2 for an exception;
470 -1 if v < w;
471 0 if v == w;
472 1 if v > w;
473 2 if this particular 3-way comparison is not implemented or undefined.
474*/
475static int
476try_3way_compare(PyObject *v, PyObject *w)
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +0000477{
478 int c;
Guido van Rossume797ec12001-01-17 15:24:28 +0000479 cmpfunc f;
480
481 /* Comparisons involving instances are given to instance_compare,
482 which has the same return conventions as this function. */
483
484 if (PyInstance_Check(v))
485 return (*v->ob_type->tp_compare)(v, w);
486 if (PyInstance_Check(w))
487 return (*w->ob_type->tp_compare)(v, w);
488
489 /* If the types are equal, don't bother with coercions etc. */
490 if (v->ob_type == w->ob_type) {
491 if ((f = v->ob_type->tp_compare) == NULL)
492 return 2;
493 c = (*f)(v, w);
494 if (PyErr_Occurred())
495 return -2;
496 return c < 0 ? -1 : c > 0 ? 1 : 0;
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +0000497 }
Guido van Rossume797ec12001-01-17 15:24:28 +0000498
499 /* Try coercion; if it fails, give up */
500 c = PyNumber_CoerceEx(&v, &w);
501 if (c < 0)
502 return -2;
503 if (c > 0)
504 return 2;
505
506 /* Try v's comparison, if defined */
507 if ((f = v->ob_type->tp_compare) != NULL) {
508 c = (*f)(v, w);
509 Py_DECREF(v);
510 Py_DECREF(w);
511 if (PyErr_Occurred())
512 return -2;
513 return c < 0 ? -1 : c > 0 ? 1 : 0;
514 }
515
516 /* Try w's comparison, if defined */
517 if ((f = w->ob_type->tp_compare) != NULL) {
518 c = (*f)(w, v); /* swapped! */
519 Py_DECREF(v);
520 Py_DECREF(w);
521 if (PyErr_Occurred())
522 return -2;
523 return c < 0 ? 1 : c > 0 ? -1 : 0; /* negated! */
524 }
525
526 /* No comparison defined */
527 Py_DECREF(v);
528 Py_DECREF(w);
529 return 2;
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +0000530}
531
Guido van Rossume797ec12001-01-17 15:24:28 +0000532/* Final fallback 3-way comparison, returning an int. Return:
533 -2 if an error occurred;
534 -1 if v < w;
535 0 if v == w;
536 1 if v > w.
537*/
538static int
539default_3way_compare(PyObject *v, PyObject *w)
540{
541 int c;
Guido van Rossum8f9143d2001-01-22 15:59:32 +0000542 char *vname, *wname;
Guido van Rossume797ec12001-01-17 15:24:28 +0000543
544 if (v->ob_type == w->ob_type) {
Barry Warsawb0e754d2001-01-20 06:24:55 +0000545 /* When comparing these pointers, they must be cast to
546 * integer types (i.e. Py_uintptr_t, our spelling of C9X's
547 * uintptr_t). ANSI specifies that pointer compares other
548 * than == and != to non-related structures are undefined.
549 */
Barry Warsaw71ff8d52001-01-20 06:08:10 +0000550 Py_uintptr_t vv = (Py_uintptr_t)v;
551 Py_uintptr_t ww = (Py_uintptr_t)w;
Guido van Rossume797ec12001-01-17 15:24:28 +0000552 return (vv < ww) ? -1 : (vv > ww) ? 1 : 0;
553 }
554
555 /* Special case for Unicode */
556 if (PyUnicode_Check(v) || PyUnicode_Check(w)) {
557 c = PyUnicode_Compare(v, w);
558 if (!PyErr_Occurred())
559 return c;
560 /* TypeErrors are ignored: if Unicode coercion fails due
561 to one of the arguments not having the right type, we
562 continue as defined by the coercion protocol (see
563 above). Luckily, decoding errors are reported as
564 ValueErrors and are not masked by this technique. */
565 if (!PyErr_ExceptionMatches(PyExc_TypeError))
566 return -2;
567 PyErr_Clear();
568 }
569
Guido van Rossum0871e932001-01-22 19:28:09 +0000570 /* None is smaller than anything */
571 if (v == Py_None)
572 return -1;
573 if (w == Py_None)
574 return 1;
575
Guido van Rossume797ec12001-01-17 15:24:28 +0000576 /* different type: compare type names */
Guido van Rossum8f9143d2001-01-22 15:59:32 +0000577 if (v->ob_type->tp_as_number)
578 vname = "";
579 else
580 vname = v->ob_type->tp_name;
581 if (w->ob_type->tp_as_number)
582 wname = "";
583 else
584 wname = w->ob_type->tp_name;
585 c = strcmp(vname, wname);
586 if (c < 0)
587 return -1;
588 if (c > 0)
589 return 1;
590 /* Same type name, or (more likely) incomparable numeric types */
591 return ((Py_uintptr_t)(v->ob_type) < (
592 Py_uintptr_t)(w->ob_type)) ? -1 : 1;
Guido van Rossume797ec12001-01-17 15:24:28 +0000593}
594
595#define CHECK_TYPES(o) PyType_HasFeature((o)->ob_type, Py_TPFLAGS_CHECKTYPES)
596
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +0000597static int
Fred Drake100814d2000-07-09 15:48:49 +0000598do_cmp(PyObject *v, PyObject *w)
Guido van Rossum20566841995-01-12 11:26:10 +0000599{
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +0000600 int c;
601
Guido van Rossume797ec12001-01-17 15:24:28 +0000602 c = try_rich_to_3way_compare(v, w);
603 if (c < 2)
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +0000604 return c;
Guido van Rossume797ec12001-01-17 15:24:28 +0000605 c = try_3way_compare(v, w);
606 if (c < 2)
607 return c;
608 return default_3way_compare(v, w);
Guido van Rossum20566841995-01-12 11:26:10 +0000609}
610
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000611/* compare_nesting is incremented before calling compare (for
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000612 some types) and decremented on exit. If the count exceeds the
613 nesting limit, enable code to detect circular data structures.
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000614
615 This is a tunable parameter that should only affect the performance
616 of comparisons, nothing else. Setting it high makes comparing deeply
617 nested non-cyclical data structures faster, but makes comparing cyclical
618 data structures slower.
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000619*/
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000620#define NESTING_LIMIT 20
621
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000622static int compare_nesting = 0;
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000623
624static PyObject*
Fred Drake100814d2000-07-09 15:48:49 +0000625get_inprogress_dict(void)
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000626{
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000627 static PyObject *key;
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000628 PyObject *tstate_dict, *inprogress;
629
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000630 if (key == NULL) {
631 key = PyString_InternFromString("cmp_state");
632 if (key == NULL)
633 return NULL;
634 }
635
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000636 tstate_dict = PyThreadState_GetDict();
637 if (tstate_dict == NULL) {
638 PyErr_BadInternalCall();
639 return NULL;
640 }
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000641
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000642 inprogress = PyDict_GetItem(tstate_dict, key);
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000643 if (inprogress == NULL) {
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000644 inprogress = PyDict_New();
645 if (inprogress == NULL)
646 return NULL;
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000647 if (PyDict_SetItem(tstate_dict, key, inprogress) == -1) {
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000648 Py_DECREF(inprogress);
649 return NULL;
650 }
Jeremy Hyltona251ea02000-06-09 16:20:39 +0000651 Py_DECREF(inprogress);
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000652 }
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000653
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000654 return inprogress;
655}
656
657static PyObject *
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000658check_recursion(PyObject *v, PyObject *w, int op)
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000659{
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000660 PyObject *inprogress;
661 PyObject *token;
Barry Warsaw9d23a4e2000-08-18 05:01:19 +0000662 Py_uintptr_t iv = (Py_uintptr_t)v;
663 Py_uintptr_t iw = (Py_uintptr_t)w;
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000664 PyObject *x, *y, *z;
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000665
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000666 inprogress = get_inprogress_dict();
667 if (inprogress == NULL)
668 return NULL;
669
670 token = PyTuple_New(3);
671 if (token == NULL)
672 return NULL;
673
674 if (iv <= iw) {
675 PyTuple_SET_ITEM(token, 0, x = PyLong_FromVoidPtr((void *)v));
676 PyTuple_SET_ITEM(token, 1, y = PyLong_FromVoidPtr((void *)w));
677 if (op >= 0)
678 op = swapped_op[op];
679 } else {
680 PyTuple_SET_ITEM(token, 0, x = PyLong_FromVoidPtr((void *)w));
681 PyTuple_SET_ITEM(token, 1, y = PyLong_FromVoidPtr((void *)v));
682 }
683 PyTuple_SET_ITEM(token, 2, z = PyInt_FromLong((long)op));
684 if (x == NULL || y == NULL || z == NULL) {
685 Py_DECREF(token);
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000686 return NULL;
687 }
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000688
689 if (PyDict_GetItem(inprogress, token) != NULL) {
690 Py_DECREF(token);
691 return Py_None; /* Without INCREF! */
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000692 }
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000693
694 if (PyDict_SetItem(inprogress, token, token) < 0) {
695 Py_DECREF(token);
696 return NULL;
697 }
698
699 return token;
700}
701
702static void
703delete_token(PyObject *token)
704{
705 PyObject *inprogress;
706
707 if (token == NULL || token == Py_None)
708 return;
709 inprogress = get_inprogress_dict();
710 if (inprogress == NULL)
711 PyErr_Clear();
712 else
713 PyDict_DelItem(inprogress, token);
714 Py_DECREF(token);
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000715}
716
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000717int
Fred Drake100814d2000-07-09 15:48:49 +0000718PyObject_Compare(PyObject *v, PyObject *w)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000719{
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000720 PyTypeObject *vtp;
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000721 int result;
722
Jack Jansend49cbe12000-08-22 21:52:51 +0000723#if defined(USE_STACKCHECK)
Guido van Rossum9c0a99e2000-08-30 15:53:50 +0000724 if (PyOS_CheckStack()) {
Jack Jansend49cbe12000-08-22 21:52:51 +0000725 PyErr_SetString(PyExc_MemoryError, "Stack overflow");
726 return -1;
727 }
728#endif
Guido van Rossumc8b6df91997-05-23 00:06:51 +0000729 if (v == NULL || w == NULL) {
730 PyErr_BadInternalCall();
731 return -1;
732 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000733 if (v == w)
734 return 0;
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +0000735 vtp = v->ob_type;
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000736 compare_nesting++;
737 if (compare_nesting > NESTING_LIMIT &&
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +0000738 (vtp->tp_as_mapping
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000739 || (vtp->tp_as_sequence
740 && !PyString_Check(v)
741 && !PyTuple_Check(v)))) {
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +0000742 /* try to detect circular data structures */
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000743 PyObject *token = check_recursion(v, w, -1);
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000744
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000745 if (token == NULL) {
746 result = -1;
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000747 }
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000748 else if (token == Py_None) {
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000749 /* already comparing these objects. assume
750 they're equal until shown otherwise */
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +0000751 result = 0;
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000752 }
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000753 else {
754 result = do_cmp(v, w);
755 delete_token(token);
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000756 }
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000757 }
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +0000758 else {
759 result = do_cmp(v, w);
760 }
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000761 compare_nesting--;
Guido van Rossume797ec12001-01-17 15:24:28 +0000762 return result < 0 ? -1 : result;
763}
764
765static PyObject *
766try_3way_to_rich_compare(PyObject *v, PyObject *w, int op)
767{
768 int c;
769 PyObject *result;
770
771 c = try_3way_compare(v, w);
772 if (c <= -2)
773 return NULL;
774 if (c >= 2)
775 c = default_3way_compare(v, w);
776 switch (op) {
777 case Py_LT: c = c < 0; break;
778 case Py_LE: c = c <= 0; break;
779 case Py_EQ: c = c == 0; break;
780 case Py_NE: c = c != 0; break;
781 case Py_GT: c = c > 0; break;
782 case Py_GE: c = c >= 0; break;
783 }
784 result = c ? Py_True : Py_False;
785 Py_INCREF(result);
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000786 return result;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000787}
788
Neil Schemenauerd38855c2001-01-21 16:25:18 +0000789static PyObject *
Guido van Rossume797ec12001-01-17 15:24:28 +0000790do_richcmp(PyObject *v, PyObject *w, int op)
791{
792 PyObject *res;
793
794 res = try_rich_compare(v, w, op);
795 if (res != Py_NotImplemented)
796 return res;
797 Py_DECREF(res);
798
799 return try_3way_to_rich_compare(v, w, op);
800}
801
802PyObject *
803PyObject_RichCompare(PyObject *v, PyObject *w, int op)
804{
805 PyObject *res;
806
807 assert(Py_LT <= op && op <= Py_GE);
808
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000809 compare_nesting++;
810 if (compare_nesting > NESTING_LIMIT &&
811 (v->ob_type->tp_as_mapping
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000812 || (v->ob_type->tp_as_sequence
813 && !PyString_Check(v)
814 && !PyTuple_Check(v)))) {
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000815 /* try to detect circular data structures */
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000816 PyObject *token = check_recursion(v, w, op);
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000817
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000818 if (token == NULL) {
819 res = NULL;
Guido van Rossume797ec12001-01-17 15:24:28 +0000820 }
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000821 else if (token == Py_None) {
822 /* already comparing these objects with this operator.
823 assume they're equal until shown otherwise */
824 if (op == Py_EQ)
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000825 res = Py_True;
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000826 else if (op == Py_NE)
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000827 res = Py_False;
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000828 else {
829 PyErr_SetString(PyExc_ValueError,
830 "can't order recursive values");
831 res = NULL;
832 }
833 Py_XINCREF(res);
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000834 }
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000835 else {
836 res = do_richcmp(v, w, op);
837 delete_token(token);
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000838 }
Guido van Rossume797ec12001-01-17 15:24:28 +0000839 }
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000840 else {
841 res = do_richcmp(v, w, op);
842 }
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000843 compare_nesting--;
Guido van Rossume797ec12001-01-17 15:24:28 +0000844 return res;
845}
846
847int
848PyObject_RichCompareBool(PyObject *v, PyObject *w, int op)
849{
850 PyObject *res = PyObject_RichCompare(v, w, op);
851 int ok;
852
853 if (res == NULL)
854 return -1;
855 ok = PyObject_IsTrue(res);
856 Py_DECREF(res);
857 return ok;
858}
Fred Drake13634cf2000-06-29 19:17:04 +0000859
860/* Set of hash utility functions to help maintaining the invariant that
861 iff a==b then hash(a)==hash(b)
862
863 All the utility functions (_Py_Hash*()) return "-1" to signify an error.
864*/
865
866long
Fred Drake100814d2000-07-09 15:48:49 +0000867_Py_HashDouble(double v)
Fred Drake13634cf2000-06-29 19:17:04 +0000868{
Tim Peters39dce292000-08-15 03:34:48 +0000869 double intpart, fractpart;
870 int expo;
871 long hipart;
872 long x; /* the final hash value */
873 /* This is designed so that Python numbers of different types
874 * that compare equal hash to the same value; otherwise comparisons
875 * of mapping keys will turn out weird.
876 */
877
878#ifdef MPW /* MPW C modf expects pointer to extended as second argument */
879{
880 extended e;
881 fractpart = modf(v, &e);
882 intpart = e;
883}
884#else
885 fractpart = modf(v, &intpart);
886#endif
887 if (fractpart == 0.0) {
888 /* This must return the same hash as an equal int or long. */
889 if (intpart > LONG_MAX || -intpart > LONG_MAX) {
890 /* Convert to long and use its hash. */
891 PyObject *plong; /* converted to Python long */
892 if (Py_IS_INFINITY(intpart))
893 /* can't convert to long int -- arbitrary */
894 v = v < 0 ? -271828.0 : 314159.0;
895 plong = PyLong_FromDouble(v);
896 if (plong == NULL)
897 return -1;
898 x = PyObject_Hash(plong);
899 Py_DECREF(plong);
900 return x;
901 }
902 /* Fits in a C long == a Python int, so is its own hash. */
903 x = (long)intpart;
904 if (x == -1)
905 x = -2;
906 return x;
907 }
908 /* The fractional part is non-zero, so we don't have to worry about
909 * making this match the hash of some other type.
910 * Use frexp to get at the bits in the double.
Fred Drake13634cf2000-06-29 19:17:04 +0000911 * Since the VAX D double format has 56 mantissa bits, which is the
912 * most of any double format in use, each of these parts may have as
913 * many as (but no more than) 56 significant bits.
Tim Peters39dce292000-08-15 03:34:48 +0000914 * So, assuming sizeof(long) >= 4, each part can be broken into two
915 * longs; frexp and multiplication are used to do that.
916 * Also, since the Cray double format has 15 exponent bits, which is
917 * the most of any double format in use, shifting the exponent field
918 * left by 15 won't overflow a long (again assuming sizeof(long) >= 4).
Fred Drake13634cf2000-06-29 19:17:04 +0000919 */
Tim Peters39dce292000-08-15 03:34:48 +0000920 v = frexp(v, &expo);
921 v *= 2147483648.0; /* 2**31 */
922 hipart = (long)v; /* take the top 32 bits */
923 v = (v - (double)hipart) * 2147483648.0; /* get the next 32 bits */
924 x = hipart + (long)v + (expo << 15);
925 if (x == -1)
926 x = -2;
927 return x;
Fred Drake13634cf2000-06-29 19:17:04 +0000928}
929
930long
Fred Drake100814d2000-07-09 15:48:49 +0000931_Py_HashPointer(void *p)
Fred Drake13634cf2000-06-29 19:17:04 +0000932{
933#if SIZEOF_LONG >= SIZEOF_VOID_P
934 return (long)p;
935#else
936 /* convert to a Python long and hash that */
937 PyObject* longobj;
938 long x;
939
940 if ((longobj = PyLong_FromVoidPtr(p)) == NULL) {
941 x = -1;
942 goto finally;
943 }
944 x = PyObject_Hash(longobj);
945
946finally:
947 Py_XDECREF(longobj);
948 return x;
949#endif
950}
951
952
Guido van Rossum9bfef441993-03-29 10:43:31 +0000953long
Fred Drake100814d2000-07-09 15:48:49 +0000954PyObject_Hash(PyObject *v)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000955{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000956 PyTypeObject *tp = v->ob_type;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000957 if (tp->tp_hash != NULL)
958 return (*tp->tp_hash)(v);
Guido van Rossumd1f06b92001-01-24 22:14:43 +0000959 if (tp->tp_compare == NULL && RICHCOMPARE(tp) == NULL) {
Fred Drake13634cf2000-06-29 19:17:04 +0000960 return _Py_HashPointer(v); /* Use address as hash value */
961 }
Guido van Rossum9bfef441993-03-29 10:43:31 +0000962 /* If there's a cmp but no hash defined, the object can't be hashed */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000963 PyErr_SetString(PyExc_TypeError, "unhashable type");
Guido van Rossum9bfef441993-03-29 10:43:31 +0000964 return -1;
965}
966
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000967PyObject *
Fred Drake100814d2000-07-09 15:48:49 +0000968PyObject_GetAttrString(PyObject *v, char *name)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000969{
Guido van Rossumd8eb1b31996-08-09 20:52:03 +0000970 if (v->ob_type->tp_getattro != NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000971 PyObject *w, *res;
Guido van Rossumc6d06701997-01-18 07:57:16 +0000972 w = PyString_InternFromString(name);
Guido van Rossumd8eb1b31996-08-09 20:52:03 +0000973 if (w == NULL)
974 return NULL;
975 res = (*v->ob_type->tp_getattro)(v, w);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000976 Py_XDECREF(w);
Guido van Rossumd8eb1b31996-08-09 20:52:03 +0000977 return res;
978 }
979
Guido van Rossum3f5da241990-12-20 15:06:42 +0000980 if (v->ob_type->tp_getattr == NULL) {
Guido van Rossumdb935161998-01-19 22:16:36 +0000981 PyErr_Format(PyExc_AttributeError,
982 "'%.50s' object has no attribute '%.400s'",
983 v->ob_type->tp_name,
984 name);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000985 return NULL;
986 }
987 else {
988 return (*v->ob_type->tp_getattr)(v, name);
989 }
990}
991
992int
Fred Drake100814d2000-07-09 15:48:49 +0000993PyObject_HasAttrString(PyObject *v, char *name)
Guido van Rossumed18fdc1993-07-11 19:55:34 +0000994{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000995 PyObject *res = PyObject_GetAttrString(v, name);
Guido van Rossumed18fdc1993-07-11 19:55:34 +0000996 if (res != NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000997 Py_DECREF(res);
Guido van Rossumed18fdc1993-07-11 19:55:34 +0000998 return 1;
999 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001000 PyErr_Clear();
Guido van Rossumed18fdc1993-07-11 19:55:34 +00001001 return 0;
1002}
1003
1004int
Fred Drake100814d2000-07-09 15:48:49 +00001005PyObject_SetAttrString(PyObject *v, char *name, PyObject *w)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001006{
Guido van Rossumd8eb1b31996-08-09 20:52:03 +00001007 if (v->ob_type->tp_setattro != NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001008 PyObject *s;
Guido van Rossumd8eb1b31996-08-09 20:52:03 +00001009 int res;
Guido van Rossumc6d06701997-01-18 07:57:16 +00001010 s = PyString_InternFromString(name);
Guido van Rossumd8eb1b31996-08-09 20:52:03 +00001011 if (s == NULL)
Guido van Rossumb7fc3041996-09-11 22:51:25 +00001012 return -1;
Guido van Rossumd8eb1b31996-08-09 20:52:03 +00001013 res = (*v->ob_type->tp_setattro)(v, s, w);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001014 Py_XDECREF(s);
Guido van Rossumd8eb1b31996-08-09 20:52:03 +00001015 return res;
1016 }
1017
Guido van Rossum3f5da241990-12-20 15:06:42 +00001018 if (v->ob_type->tp_setattr == NULL) {
1019 if (v->ob_type->tp_getattr == NULL)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001020 PyErr_SetString(PyExc_TypeError,
Guido van Rossum3ea74121991-12-24 13:28:03 +00001021 "attribute-less object (assign or del)");
Guido van Rossum3f5da241990-12-20 15:06:42 +00001022 else
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001023 PyErr_SetString(PyExc_TypeError,
Guido van Rossum3ea74121991-12-24 13:28:03 +00001024 "object has read-only attributes");
Guido van Rossum73531a31990-12-20 23:12:40 +00001025 return -1;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001026 }
1027 else {
1028 return (*v->ob_type->tp_setattr)(v, name, w);
1029 }
1030}
1031
Marc-André Lemburge44e5072000-09-18 16:20:57 +00001032/* Internal API needed by PyObject_GetAttr(): */
1033extern
1034PyObject *_PyUnicode_AsDefaultEncodedString(PyObject *unicode,
1035 const char *errors);
1036
Guido van Rossum98ff96a1997-05-20 18:34:44 +00001037PyObject *
Fred Drake100814d2000-07-09 15:48:49 +00001038PyObject_GetAttr(PyObject *v, PyObject *name)
Guido van Rossum98ff96a1997-05-20 18:34:44 +00001039{
Marc-André Lemburge44e5072000-09-18 16:20:57 +00001040 /* The Unicode to string conversion is done here because the
1041 existing tp_getattro slots expect a string object as name
1042 and we wouldn't want to break those. */
1043 if (PyUnicode_Check(name)) {
1044 name = _PyUnicode_AsDefaultEncodedString(name, NULL);
1045 if (name == NULL)
1046 return NULL;
1047 }
Jeremy Hylton99a8f902000-06-23 14:36:32 +00001048
1049 if (!PyString_Check(name)) {
1050 PyErr_SetString(PyExc_TypeError,
1051 "attribute name must be string");
1052 return NULL;
1053 }
Marc-André Lemburge44e5072000-09-18 16:20:57 +00001054 if (v->ob_type->tp_getattro != NULL)
1055 return (*v->ob_type->tp_getattro)(v, name);
1056 else
Jeremy Hylton99a8f902000-06-23 14:36:32 +00001057 return PyObject_GetAttrString(v, PyString_AS_STRING(name));
Guido van Rossum98ff96a1997-05-20 18:34:44 +00001058}
1059
1060int
Fred Drake100814d2000-07-09 15:48:49 +00001061PyObject_HasAttr(PyObject *v, PyObject *name)
Guido van Rossum98ff96a1997-05-20 18:34:44 +00001062{
1063 PyObject *res = PyObject_GetAttr(v, name);
1064 if (res != NULL) {
1065 Py_DECREF(res);
1066 return 1;
1067 }
1068 PyErr_Clear();
1069 return 0;
1070}
1071
1072int
Fred Drake100814d2000-07-09 15:48:49 +00001073PyObject_SetAttr(PyObject *v, PyObject *name, PyObject *value)
Guido van Rossum98ff96a1997-05-20 18:34:44 +00001074{
1075 int err;
Marc-André Lemburge44e5072000-09-18 16:20:57 +00001076
1077 /* The Unicode to string conversion is done here because the
1078 existing tp_setattro slots expect a string object as name
1079 and we wouldn't want to break those. */
1080 if (PyUnicode_Check(name)) {
1081 name = PyUnicode_AsEncodedString(name, NULL, NULL);
1082 if (name == NULL)
1083 return -1;
Jeremy Hylton99a8f902000-06-23 14:36:32 +00001084 }
Marc-André Lemburge44e5072000-09-18 16:20:57 +00001085 else
1086 Py_INCREF(name);
1087
1088 if (!PyString_Check(name)){
Jeremy Hylton99a8f902000-06-23 14:36:32 +00001089 PyErr_SetString(PyExc_TypeError,
1090 "attribute name must be string");
1091 err = -1;
1092 }
Marc-André Lemburge44e5072000-09-18 16:20:57 +00001093 else {
1094 PyString_InternInPlace(&name);
1095 if (v->ob_type->tp_setattro != NULL)
1096 err = (*v->ob_type->tp_setattro)(v, name, value);
1097 else
1098 err = PyObject_SetAttrString(v,
1099 PyString_AS_STRING(name), value);
1100 }
1101
Guido van Rossum98ff96a1997-05-20 18:34:44 +00001102 Py_DECREF(name);
1103 return err;
1104}
1105
Guido van Rossum6ac258d1993-05-12 08:24:20 +00001106/* Test a value used as condition, e.g., in a for or if statement.
1107 Return -1 if an error occurred */
1108
1109int
Fred Drake100814d2000-07-09 15:48:49 +00001110PyObject_IsTrue(PyObject *v)
Guido van Rossum6ac258d1993-05-12 08:24:20 +00001111{
1112 int res;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001113 if (v == Py_None)
Guido van Rossum6ac258d1993-05-12 08:24:20 +00001114 res = 0;
Guido van Rossum1c4f4581998-05-22 00:53:24 +00001115 else if (v->ob_type->tp_as_number != NULL &&
1116 v->ob_type->tp_as_number->nb_nonzero != NULL)
Guido van Rossum6ac258d1993-05-12 08:24:20 +00001117 res = (*v->ob_type->tp_as_number->nb_nonzero)(v);
Guido van Rossum1c4f4581998-05-22 00:53:24 +00001118 else if (v->ob_type->tp_as_mapping != NULL &&
1119 v->ob_type->tp_as_mapping->mp_length != NULL)
Guido van Rossum6ac258d1993-05-12 08:24:20 +00001120 res = (*v->ob_type->tp_as_mapping->mp_length)(v);
Guido van Rossum1c4f4581998-05-22 00:53:24 +00001121 else if (v->ob_type->tp_as_sequence != NULL &&
1122 v->ob_type->tp_as_sequence->sq_length != NULL)
Guido van Rossum6ac258d1993-05-12 08:24:20 +00001123 res = (*v->ob_type->tp_as_sequence->sq_length)(v);
1124 else
1125 res = 1;
1126 if (res > 0)
1127 res = 1;
1128 return res;
1129}
1130
Guido van Rossumc3d3f961998-04-09 17:53:59 +00001131/* equivalent of 'not v'
1132 Return -1 if an error occurred */
1133
1134int
Fred Drake100814d2000-07-09 15:48:49 +00001135PyObject_Not(PyObject *v)
Guido van Rossumc3d3f961998-04-09 17:53:59 +00001136{
1137 int res;
1138 res = PyObject_IsTrue(v);
1139 if (res < 0)
1140 return res;
1141 return res == 0;
1142}
1143
Guido van Rossum5524a591995-01-10 15:26:20 +00001144/* Coerce two numeric types to the "larger" one.
1145 Increment the reference count on each argument.
Guido van Rossume797ec12001-01-17 15:24:28 +00001146 Return value:
1147 -1 if an error occurred;
1148 0 if the coercion succeeded (and then the reference counts are increased);
1149 1 if no coercion is possible (and no error is raised).
Guido van Rossum5524a591995-01-10 15:26:20 +00001150*/
Guido van Rossum5524a591995-01-10 15:26:20 +00001151int
Fred Drake100814d2000-07-09 15:48:49 +00001152PyNumber_CoerceEx(PyObject **pv, PyObject **pw)
Guido van Rossum5524a591995-01-10 15:26:20 +00001153{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001154 register PyObject *v = *pv;
1155 register PyObject *w = *pw;
Guido van Rossum5524a591995-01-10 15:26:20 +00001156 int res;
1157
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001158 if (v->ob_type == w->ob_type && !PyInstance_Check(v)) {
1159 Py_INCREF(v);
1160 Py_INCREF(w);
Guido van Rossum5524a591995-01-10 15:26:20 +00001161 return 0;
1162 }
1163 if (v->ob_type->tp_as_number && v->ob_type->tp_as_number->nb_coerce) {
1164 res = (*v->ob_type->tp_as_number->nb_coerce)(pv, pw);
1165 if (res <= 0)
1166 return res;
1167 }
1168 if (w->ob_type->tp_as_number && w->ob_type->tp_as_number->nb_coerce) {
1169 res = (*w->ob_type->tp_as_number->nb_coerce)(pw, pv);
1170 if (res <= 0)
1171 return res;
1172 }
Guido van Rossum242c6421997-11-19 16:03:17 +00001173 return 1;
1174}
1175
Guido van Rossume797ec12001-01-17 15:24:28 +00001176/* Coerce two numeric types to the "larger" one.
1177 Increment the reference count on each argument.
1178 Return -1 and raise an exception if no coercion is possible
1179 (and then no reference count is incremented).
1180*/
Guido van Rossum242c6421997-11-19 16:03:17 +00001181int
Fred Drake100814d2000-07-09 15:48:49 +00001182PyNumber_Coerce(PyObject **pv, PyObject **pw)
Guido van Rossum242c6421997-11-19 16:03:17 +00001183{
1184 int err = PyNumber_CoerceEx(pv, pw);
1185 if (err <= 0)
1186 return err;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001187 PyErr_SetString(PyExc_TypeError, "number coercion failed");
Guido van Rossum5524a591995-01-10 15:26:20 +00001188 return -1;
1189}
1190
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001191
Guido van Rossum49b11fe1995-01-26 00:38:22 +00001192/* Test whether an object can be called */
1193
1194int
Fred Drake100814d2000-07-09 15:48:49 +00001195PyCallable_Check(PyObject *x)
Guido van Rossum49b11fe1995-01-26 00:38:22 +00001196{
1197 if (x == NULL)
1198 return 0;
1199 if (x->ob_type->tp_call != NULL ||
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001200 PyFunction_Check(x) ||
1201 PyMethod_Check(x) ||
1202 PyCFunction_Check(x) ||
1203 PyClass_Check(x))
Guido van Rossum49b11fe1995-01-26 00:38:22 +00001204 return 1;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001205 if (PyInstance_Check(x)) {
1206 PyObject *call = PyObject_GetAttrString(x, "__call__");
Guido van Rossum49b11fe1995-01-26 00:38:22 +00001207 if (call == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001208 PyErr_Clear();
Guido van Rossum49b11fe1995-01-26 00:38:22 +00001209 return 0;
1210 }
1211 /* Could test recursively but don't, for fear of endless
1212 recursion if some joker sets self.__call__ = self */
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001213 Py_DECREF(call);
Guido van Rossum49b11fe1995-01-26 00:38:22 +00001214 return 1;
1215 }
1216 return 0;
1217}
1218
1219
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001220/*
1221NoObject is usable as a non-NULL undefined value, used by the macro None.
1222There is (and should be!) no way to create other objects of this type,
Guido van Rossum3f5da241990-12-20 15:06:42 +00001223so there is exactly one (which is indestructible, by the way).
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001224*/
1225
Guido van Rossum0c182a11992-03-27 17:26:13 +00001226/* ARGSUSED */
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001227static PyObject *
Fred Drake100814d2000-07-09 15:48:49 +00001228none_repr(PyObject *op)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001229{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001230 return PyString_FromString("None");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001231}
1232
Barry Warsaw9bf16442001-01-23 16:24:35 +00001233/* ARGUSED */
1234static void
1235none_dealloc(PyObject* ignore)
1236{
1237 /* This should never get called, but we also don't want to SEGV if
1238 * we accidently decref None out of existance.
1239 */
1240 abort();
1241}
1242
1243
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001244static PyTypeObject PyNothing_Type = {
1245 PyObject_HEAD_INIT(&PyType_Type)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001246 0,
Guido van Rossum3f5da241990-12-20 15:06:42 +00001247 "None",
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001248 0,
1249 0,
Barry Warsaw9bf16442001-01-23 16:24:35 +00001250 (destructor)none_dealloc, /*tp_dealloc*/ /*never called*/
Guido van Rossum7066dd71992-09-17 17:54:56 +00001251 0, /*tp_print*/
Guido van Rossum3f5da241990-12-20 15:06:42 +00001252 0, /*tp_getattr*/
1253 0, /*tp_setattr*/
1254 0, /*tp_compare*/
Guido van Rossum1d5735e1994-08-30 08:27:36 +00001255 (reprfunc)none_repr, /*tp_repr*/
Guido van Rossum3f5da241990-12-20 15:06:42 +00001256 0, /*tp_as_number*/
1257 0, /*tp_as_sequence*/
1258 0, /*tp_as_mapping*/
Guido van Rossum9bfef441993-03-29 10:43:31 +00001259 0, /*tp_hash */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001260};
1261
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001262PyObject _Py_NoneStruct = {
1263 PyObject_HEAD_INIT(&PyNothing_Type)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001264};
1265
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +00001266/* NotImplemented is an object that can be used to signal that an
1267 operation is not implemented for the given type combination. */
1268
1269static PyObject *
1270NotImplemented_repr(PyObject *op)
1271{
1272 return PyString_FromString("NotImplemented");
1273}
1274
1275static PyTypeObject PyNotImplemented_Type = {
1276 PyObject_HEAD_INIT(&PyType_Type)
1277 0,
1278 "NotImplemented",
1279 0,
1280 0,
Barry Warsaw9bf16442001-01-23 16:24:35 +00001281 (destructor)none_dealloc, /*tp_dealloc*/ /*never called*/
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +00001282 0, /*tp_print*/
1283 0, /*tp_getattr*/
1284 0, /*tp_setattr*/
1285 0, /*tp_compare*/
1286 (reprfunc)NotImplemented_repr, /*tp_repr*/
1287 0, /*tp_as_number*/
1288 0, /*tp_as_sequence*/
1289 0, /*tp_as_mapping*/
1290 0, /*tp_hash */
1291};
1292
1293PyObject _Py_NotImplementedStruct = {
1294 PyObject_HEAD_INIT(&PyNotImplemented_Type)
1295};
1296
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001297
Guido van Rossum84a90321996-05-22 16:34:47 +00001298#ifdef Py_TRACE_REFS
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001299
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001300static PyObject refchain = {&refchain, &refchain};
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001301
Guido van Rossumaacdc9d1996-08-12 21:32:12 +00001302void
Fred Drake100814d2000-07-09 15:48:49 +00001303_Py_ResetReferences(void)
Guido van Rossume09fb551997-08-05 02:04:34 +00001304{
1305 refchain._ob_prev = refchain._ob_next = &refchain;
1306 _Py_RefTotal = 0;
1307}
1308
1309void
Fred Drake100814d2000-07-09 15:48:49 +00001310_Py_NewReference(PyObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001311{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001312 _Py_RefTotal++;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001313 op->ob_refcnt = 1;
1314 op->_ob_next = refchain._ob_next;
1315 op->_ob_prev = &refchain;
1316 refchain._ob_next->_ob_prev = op;
1317 refchain._ob_next = op;
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +00001318#ifdef COUNT_ALLOCS
1319 inc_count(op->ob_type);
1320#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001321}
1322
Guido van Rossumaacdc9d1996-08-12 21:32:12 +00001323void
Fred Drake100814d2000-07-09 15:48:49 +00001324_Py_ForgetReference(register PyObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001325{
Guido van Rossumbffd6832000-01-20 22:32:56 +00001326#ifdef SLOW_UNREF_CHECK
Guido van Rossum4c08d552000-03-10 22:55:18 +00001327 register PyObject *p;
Guido van Rossumbffd6832000-01-20 22:32:56 +00001328#endif
Guido van Rossumd7047b31995-01-02 19:07:15 +00001329 if (op->ob_refcnt < 0)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001330 Py_FatalError("UNREF negative refcnt");
Guido van Rossum2e8f6141992-09-03 20:32:55 +00001331 if (op == &refchain ||
Guido van Rossumd7047b31995-01-02 19:07:15 +00001332 op->_ob_prev->_ob_next != op || op->_ob_next->_ob_prev != op)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001333 Py_FatalError("UNREF invalid object");
Guido van Rossum2e8f6141992-09-03 20:32:55 +00001334#ifdef SLOW_UNREF_CHECK
Guido van Rossum3f5da241990-12-20 15:06:42 +00001335 for (p = refchain._ob_next; p != &refchain; p = p->_ob_next) {
1336 if (p == op)
1337 break;
1338 }
Guido van Rossumd7047b31995-01-02 19:07:15 +00001339 if (p == &refchain) /* Not found */
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001340 Py_FatalError("UNREF unknown object");
Guido van Rossum2e8f6141992-09-03 20:32:55 +00001341#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001342 op->_ob_next->_ob_prev = op->_ob_prev;
1343 op->_ob_prev->_ob_next = op->_ob_next;
Guido van Rossum2e8f6141992-09-03 20:32:55 +00001344 op->_ob_next = op->_ob_prev = NULL;
Guido van Rossumd8953cb1995-04-06 14:46:26 +00001345#ifdef COUNT_ALLOCS
1346 op->ob_type->tp_free++;
1347#endif
Guido van Rossum3f5da241990-12-20 15:06:42 +00001348}
1349
Guido van Rossumaacdc9d1996-08-12 21:32:12 +00001350void
Fred Drake100814d2000-07-09 15:48:49 +00001351_Py_Dealloc(PyObject *op)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001352{
Guido van Rossum9776adf1994-09-07 14:36:45 +00001353 destructor dealloc = op->ob_type->tp_dealloc;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001354 _Py_ForgetReference(op);
Guido van Rossum9776adf1994-09-07 14:36:45 +00001355 (*dealloc)(op);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001356}
1357
Guido van Rossumaacdc9d1996-08-12 21:32:12 +00001358void
Fred Drake100814d2000-07-09 15:48:49 +00001359_Py_PrintReferences(FILE *fp)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001360{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001361 PyObject *op;
Guido van Rossume09fb551997-08-05 02:04:34 +00001362 fprintf(fp, "Remaining objects:\n");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001363 for (op = refchain._ob_next; op != &refchain; op = op->_ob_next) {
1364 fprintf(fp, "[%d] ", op->ob_refcnt);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001365 if (PyObject_Print(op, fp, 0) != 0)
1366 PyErr_Clear();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001367 putc('\n', fp);
1368 }
1369}
1370
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +00001371PyObject *
Fred Drake100814d2000-07-09 15:48:49 +00001372_Py_GetObjects(PyObject *self, PyObject *args)
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +00001373{
1374 int i, n;
1375 PyObject *t = NULL;
1376 PyObject *res, *op;
1377
1378 if (!PyArg_ParseTuple(args, "i|O", &n, &t))
1379 return NULL;
1380 op = refchain._ob_next;
1381 res = PyList_New(0);
1382 if (res == NULL)
1383 return NULL;
1384 for (i = 0; (n == 0 || i < n) && op != &refchain; i++) {
1385 while (op == self || op == args || op == res || op == t ||
1386 t != NULL && op->ob_type != (PyTypeObject *) t) {
1387 op = op->_ob_next;
1388 if (op == &refchain)
1389 return res;
1390 }
1391 if (PyList_Append(res, op) < 0) {
1392 Py_DECREF(res);
1393 return NULL;
1394 }
1395 op = op->_ob_next;
1396 }
1397 return res;
1398}
1399
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001400#endif
Guido van Rossum97ead3f1996-01-12 01:24:09 +00001401
1402
1403/* Hack to force loading of cobject.o */
Guido van Rossumda9c2711996-12-05 21:58:58 +00001404PyTypeObject *_Py_cobject_hack = &PyCObject_Type;
Guido van Rossum84a90321996-05-22 16:34:47 +00001405
1406
1407/* Hack to force loading of abstract.o */
Jeremy Hylton6253f832000-07-12 12:56:19 +00001408int (*_Py_abstract_hack)(PyObject *) = &PyObject_Size;
Guido van Rossume09fb551997-08-05 02:04:34 +00001409
1410
Andrew M. Kuchling1582a3a2000-08-16 12:27:23 +00001411/* Python's malloc wrappers (see pymem.h) */
Guido van Rossume09fb551997-08-05 02:04:34 +00001412
Thomas Wouters334fb892000-07-25 12:56:38 +00001413void *
Fred Drake100814d2000-07-09 15:48:49 +00001414PyMem_Malloc(size_t nbytes)
Guido van Rossume09fb551997-08-05 02:04:34 +00001415{
1416#if _PyMem_EXTRA > 0
1417 if (nbytes == 0)
1418 nbytes = _PyMem_EXTRA;
1419#endif
Guido van Rossumb18618d2000-05-03 23:44:39 +00001420 return PyMem_MALLOC(nbytes);
Guido van Rossume09fb551997-08-05 02:04:34 +00001421}
1422
Thomas Wouters334fb892000-07-25 12:56:38 +00001423void *
1424PyMem_Realloc(void *p, size_t nbytes)
Guido van Rossume09fb551997-08-05 02:04:34 +00001425{
1426#if _PyMem_EXTRA > 0
1427 if (nbytes == 0)
1428 nbytes = _PyMem_EXTRA;
1429#endif
Guido van Rossumb18618d2000-05-03 23:44:39 +00001430 return PyMem_REALLOC(p, nbytes);
Guido van Rossume09fb551997-08-05 02:04:34 +00001431}
1432
1433void
Thomas Wouters334fb892000-07-25 12:56:38 +00001434PyMem_Free(void *p)
Guido van Rossume09fb551997-08-05 02:04:34 +00001435{
Guido van Rossumb18618d2000-05-03 23:44:39 +00001436 PyMem_FREE(p);
1437}
1438
1439
1440/* Python's object malloc wrappers (see objimpl.h) */
1441
Thomas Wouters334fb892000-07-25 12:56:38 +00001442void *
Fred Drake100814d2000-07-09 15:48:49 +00001443PyObject_Malloc(size_t nbytes)
Guido van Rossumb18618d2000-05-03 23:44:39 +00001444{
1445 return PyObject_MALLOC(nbytes);
1446}
1447
Thomas Wouters334fb892000-07-25 12:56:38 +00001448void *
1449PyObject_Realloc(void *p, size_t nbytes)
Guido van Rossumb18618d2000-05-03 23:44:39 +00001450{
1451 return PyObject_REALLOC(p, nbytes);
1452}
1453
1454void
Thomas Wouters334fb892000-07-25 12:56:38 +00001455PyObject_Free(void *p)
Guido van Rossumb18618d2000-05-03 23:44:39 +00001456{
1457 PyObject_FREE(p);
Guido van Rossume09fb551997-08-05 02:04:34 +00001458}
Guido van Rossum86610361998-04-10 22:32:46 +00001459
1460
1461/* These methods are used to control infinite recursion in repr, str, print,
1462 etc. Container objects that may recursively contain themselves,
1463 e.g. builtin dictionaries and lists, should used Py_ReprEnter() and
1464 Py_ReprLeave() to avoid infinite recursion.
1465
1466 Py_ReprEnter() returns 0 the first time it is called for a particular
1467 object and 1 every time thereafter. It returns -1 if an exception
1468 occurred. Py_ReprLeave() has no return value.
1469
1470 See dictobject.c and listobject.c for examples of use.
1471*/
1472
1473#define KEY "Py_Repr"
1474
1475int
Fred Drake100814d2000-07-09 15:48:49 +00001476Py_ReprEnter(PyObject *obj)
Guido van Rossum86610361998-04-10 22:32:46 +00001477{
1478 PyObject *dict;
1479 PyObject *list;
1480 int i;
1481
1482 dict = PyThreadState_GetDict();
1483 if (dict == NULL)
1484 return -1;
1485 list = PyDict_GetItemString(dict, KEY);
1486 if (list == NULL) {
1487 list = PyList_New(0);
1488 if (list == NULL)
1489 return -1;
1490 if (PyDict_SetItemString(dict, KEY, list) < 0)
1491 return -1;
1492 Py_DECREF(list);
1493 }
1494 i = PyList_GET_SIZE(list);
1495 while (--i >= 0) {
1496 if (PyList_GET_ITEM(list, i) == obj)
1497 return 1;
1498 }
1499 PyList_Append(list, obj);
1500 return 0;
1501}
1502
1503void
Fred Drake100814d2000-07-09 15:48:49 +00001504Py_ReprLeave(PyObject *obj)
Guido van Rossum86610361998-04-10 22:32:46 +00001505{
1506 PyObject *dict;
1507 PyObject *list;
1508 int i;
1509
1510 dict = PyThreadState_GetDict();
Guido van Rossumeb909461998-04-11 15:17:34 +00001511 if (dict == NULL)
1512 return;
Guido van Rossum86610361998-04-10 22:32:46 +00001513 list = PyDict_GetItemString(dict, KEY);
Guido van Rossumeb909461998-04-11 15:17:34 +00001514 if (list == NULL || !PyList_Check(list))
1515 return;
Guido van Rossum86610361998-04-10 22:32:46 +00001516 i = PyList_GET_SIZE(list);
1517 /* Count backwards because we always expect obj to be list[-1] */
1518 while (--i >= 0) {
1519 if (PyList_GET_ITEM(list, i) == obj) {
1520 PyList_SetSlice(list, i, i + 1, NULL);
1521 break;
1522 }
1523 }
1524}
Guido van Rossumd724b232000-03-13 16:01:29 +00001525
1526/*
1527 trashcan
1528 CT 2k0130
1529 non-recursively destroy nested objects
1530
1531 CT 2k0223
1532 everything is now done in a macro.
1533
1534 CT 2k0305
1535 modified to use functions, after Tim Peter's suggestion.
1536
1537 CT 2k0309
1538 modified to restore a possible error.
Guido van Rossum13ff8eb2000-03-25 18:39:19 +00001539
1540 CT 2k0325
1541 added better safe than sorry check for threadstate
Guido van Rossume92e6102000-04-24 15:40:53 +00001542
1543 CT 2k0422
1544 complete rewrite. We now build a chain via ob_type
1545 and save the limited number of types in ob_refcnt.
1546 This is perfect since we don't need any memory.
1547 A patch for free-threading would need just a lock.
Guido van Rossumd724b232000-03-13 16:01:29 +00001548*/
1549
Guido van Rossume92e6102000-04-24 15:40:53 +00001550#define Py_TRASHCAN_TUPLE 1
1551#define Py_TRASHCAN_LIST 2
1552#define Py_TRASHCAN_DICT 3
1553#define Py_TRASHCAN_FRAME 4
1554#define Py_TRASHCAN_TRACEBACK 5
1555/* extend here if other objects want protection */
1556
Guido van Rossumd724b232000-03-13 16:01:29 +00001557int _PyTrash_delete_nesting = 0;
Guido van Rossume92e6102000-04-24 15:40:53 +00001558
Guido van Rossumd724b232000-03-13 16:01:29 +00001559PyObject * _PyTrash_delete_later = NULL;
1560
1561void
Fred Drake100814d2000-07-09 15:48:49 +00001562_PyTrash_deposit_object(PyObject *op)
Guido van Rossumd724b232000-03-13 16:01:29 +00001563{
Guido van Rossume92e6102000-04-24 15:40:53 +00001564 int typecode;
Guido van Rossum13ff8eb2000-03-25 18:39:19 +00001565
Guido van Rossume92e6102000-04-24 15:40:53 +00001566 if (PyTuple_Check(op))
1567 typecode = Py_TRASHCAN_TUPLE;
1568 else if (PyList_Check(op))
1569 typecode = Py_TRASHCAN_LIST;
1570 else if (PyDict_Check(op))
1571 typecode = Py_TRASHCAN_DICT;
1572 else if (PyFrame_Check(op))
1573 typecode = Py_TRASHCAN_FRAME;
1574 else if (PyTraceBack_Check(op))
1575 typecode = Py_TRASHCAN_TRACEBACK;
Moshe Zadkacf703f02000-08-04 15:36:13 +00001576 else /* We have a bug here -- those are the only types in GC */ {
1577 Py_FatalError("Type not supported in GC -- internal bug");
1578 return; /* pacify compiler -- execution never here */
1579 }
Guido van Rossume92e6102000-04-24 15:40:53 +00001580 op->ob_refcnt = typecode;
Guido van Rossumd724b232000-03-13 16:01:29 +00001581
Guido van Rossume92e6102000-04-24 15:40:53 +00001582 op->ob_type = (PyTypeObject*)_PyTrash_delete_later;
1583 _PyTrash_delete_later = op;
Guido van Rossumd724b232000-03-13 16:01:29 +00001584}
1585
1586void
Fred Drake100814d2000-07-09 15:48:49 +00001587_PyTrash_destroy_chain(void)
Guido van Rossumd724b232000-03-13 16:01:29 +00001588{
1589 while (_PyTrash_delete_later) {
1590 PyObject *shredder = _PyTrash_delete_later;
Guido van Rossume92e6102000-04-24 15:40:53 +00001591 _PyTrash_delete_later = (PyObject*) shredder->ob_type;
1592
1593 switch (shredder->ob_refcnt) {
1594 case Py_TRASHCAN_TUPLE:
1595 shredder->ob_type = &PyTuple_Type;
1596 break;
1597 case Py_TRASHCAN_LIST:
1598 shredder->ob_type = &PyList_Type;
1599 break;
1600 case Py_TRASHCAN_DICT:
1601 shredder->ob_type = &PyDict_Type;
1602 break;
1603 case Py_TRASHCAN_FRAME:
1604 shredder->ob_type = &PyFrame_Type;
1605 break;
1606 case Py_TRASHCAN_TRACEBACK:
1607 shredder->ob_type = &PyTraceBack_Type;
1608 break;
1609 }
1610 _Py_NewReference(shredder);
1611
Guido van Rossumd724b232000-03-13 16:01:29 +00001612 ++_PyTrash_delete_nesting;
1613 Py_DECREF(shredder);
1614 --_PyTrash_delete_nesting;
1615 }
1616}