blob: 87c8e1a801dc64bca7d1bf0cecf94b19d07d3c57 [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) {
Guido van Rossum4f288ab2001-05-01 16:53:37 +0000191 PyObject *s;
192 if (flags & Py_PRINT_RAW)
193 s = PyObject_Str(op);
194 else
195 s = PyObject_Repr(op);
196 if (s == NULL)
197 ret = -1;
Guido van Rossum2e8f6141992-09-03 20:32:55 +0000198 else {
Guido van Rossum4f288ab2001-05-01 16:53:37 +0000199 ret = PyObject_Print(s, fp, Py_PRINT_RAW);
Guido van Rossum2e8f6141992-09-03 20:32:55 +0000200 }
Guido van Rossum4f288ab2001-05-01 16:53:37 +0000201 Py_XDECREF(s);
Guido van Rossum2e8f6141992-09-03 20:32:55 +0000202 }
Guido van Rossum90933611991-06-07 16:10:43 +0000203 else
Guido van Rossum278ef591991-07-27 21:40:24 +0000204 ret = (*op->ob_type->tp_print)(op, fp, flags);
Guido van Rossum90933611991-06-07 16:10:43 +0000205 }
Guido van Rossum278ef591991-07-27 21:40:24 +0000206 if (ret == 0) {
207 if (ferror(fp)) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000208 PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossum278ef591991-07-27 21:40:24 +0000209 clearerr(fp);
210 ret = -1;
211 }
212 }
213 return ret;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000214}
215
Barry Warsaw9bf16442001-01-23 16:24:35 +0000216/* For debugging convenience. See Misc/gdbinit for some useful gdb hooks */
Barry Warsawbbd89b62001-01-24 04:18:13 +0000217void _PyObject_Dump(PyObject* op)
Barry Warsaw9bf16442001-01-23 16:24:35 +0000218{
Barry Warsaweefb1072001-02-22 22:39:18 +0000219 if (op == NULL)
220 fprintf(stderr, "NULL\n");
221 else {
222 (void)PyObject_Print(op, stderr, 0);
223 fprintf(stderr, "\nrefcounts: %d\n", op->ob_refcnt);
224 fprintf(stderr, "address : %p\n", op);
225 }
Barry Warsaw9bf16442001-01-23 16:24:35 +0000226}
Barry Warsaw903138f2001-01-23 16:33:18 +0000227
228#ifdef WITH_CYCLE_GC
Barry Warsawbbd89b62001-01-24 04:18:13 +0000229void _PyGC_Dump(PyGC_Head* op)
Barry Warsaw9bf16442001-01-23 16:24:35 +0000230{
Barry Warsawbbd89b62001-01-24 04:18:13 +0000231 _PyObject_Dump(PyObject_FROM_GC(op));
Barry Warsaw9bf16442001-01-23 16:24:35 +0000232}
Barry Warsaw903138f2001-01-23 16:33:18 +0000233#endif /* WITH_CYCLE_GC */
Barry Warsaw9bf16442001-01-23 16:24:35 +0000234
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000235PyObject *
Fred Drake100814d2000-07-09 15:48:49 +0000236PyObject_Repr(PyObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000237{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000238 if (PyErr_CheckSignals())
Guido van Rossum90933611991-06-07 16:10:43 +0000239 return NULL;
Guido van Rossum9b00dfa1998-04-28 16:06:54 +0000240#ifdef USE_STACKCHECK
241 if (PyOS_CheckStack()) {
Fred Drake661ea262000-10-24 19:57:45 +0000242 PyErr_SetString(PyExc_MemoryError, "stack overflow");
Guido van Rossum9b00dfa1998-04-28 16:06:54 +0000243 return NULL;
244 }
245#endif
Guido van Rossum90933611991-06-07 16:10:43 +0000246 if (v == NULL)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000247 return PyString_FromString("<NULL>");
Guido van Rossum90933611991-06-07 16:10:43 +0000248 else if (v->ob_type->tp_repr == NULL) {
249 char buf[120];
Fred Drakea44d3532000-06-30 15:01:00 +0000250 sprintf(buf, "<%.80s object at %p>",
251 v->ob_type->tp_name, v);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000252 return PyString_FromString(buf);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000253 }
Guido van Rossum4c08d552000-03-10 22:55:18 +0000254 else {
255 PyObject *res;
256 res = (*v->ob_type->tp_repr)(v);
257 if (res == NULL)
258 return NULL;
Fredrik Lundhefecc7d2000-07-01 14:31:09 +0000259 if (PyUnicode_Check(res)) {
260 PyObject* str;
Fredrik Lundh2a1e0602000-07-08 17:43:32 +0000261 str = PyUnicode_AsUnicodeEscapeString(res);
Marc-André Lemburg891bc652000-07-03 09:57:53 +0000262 Py_DECREF(res);
263 if (str)
Fredrik Lundhefecc7d2000-07-01 14:31:09 +0000264 res = str;
Marc-André Lemburg891bc652000-07-03 09:57:53 +0000265 else
266 return NULL;
Fredrik Lundhefecc7d2000-07-01 14:31:09 +0000267 }
Guido van Rossum4c08d552000-03-10 22:55:18 +0000268 if (!PyString_Check(res)) {
269 PyErr_Format(PyExc_TypeError,
Guido van Rossum5db862d2000-04-10 12:46:51 +0000270 "__repr__ returned non-string (type %.200s)",
Guido van Rossum4c08d552000-03-10 22:55:18 +0000271 res->ob_type->tp_name);
272 Py_DECREF(res);
273 return NULL;
274 }
275 return res;
276 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000277}
278
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000279PyObject *
Fred Drake100814d2000-07-09 15:48:49 +0000280PyObject_Str(PyObject *v)
Guido van Rossumc6004111993-11-05 10:22:19 +0000281{
Guido van Rossum4c08d552000-03-10 22:55:18 +0000282 PyObject *res;
283
Guido van Rossumc6004111993-11-05 10:22:19 +0000284 if (v == NULL)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000285 return PyString_FromString("<NULL>");
Guido van Rossum4f288ab2001-05-01 16:53:37 +0000286 if (PyString_Check(v)) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000287 Py_INCREF(v);
Guido van Rossumc6004111993-11-05 10:22:19 +0000288 return v;
289 }
Guido van Rossum4f288ab2001-05-01 16:53:37 +0000290 if (v->ob_type->tp_str == NULL)
291 return PyObject_Repr(v);
292
293 res = (*v->ob_type->tp_str)(v);
Guido van Rossum4c08d552000-03-10 22:55:18 +0000294 if (res == NULL)
295 return NULL;
Marc-André Lemburg891bc652000-07-03 09:57:53 +0000296 if (PyUnicode_Check(res)) {
297 PyObject* str;
298 str = PyUnicode_AsEncodedString(res, NULL, NULL);
299 Py_DECREF(res);
300 if (str)
301 res = str;
302 else
303 return NULL;
304 }
Guido van Rossum4c08d552000-03-10 22:55:18 +0000305 if (!PyString_Check(res)) {
306 PyErr_Format(PyExc_TypeError,
Guido van Rossum5db862d2000-04-10 12:46:51 +0000307 "__str__ returned non-string (type %.200s)",
Guido van Rossum4c08d552000-03-10 22:55:18 +0000308 res->ob_type->tp_name);
309 Py_DECREF(res);
310 return NULL;
311 }
312 return res;
Guido van Rossumc6004111993-11-05 10:22:19 +0000313}
314
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000315PyObject *
316PyObject_Unicode(PyObject *v)
317{
318 PyObject *res;
319
320 if (v == NULL)
321 res = PyString_FromString("<NULL>");
322 else if (PyUnicode_Check(v)) {
323 Py_INCREF(v);
324 return v;
325 }
Marc-André Lemburgae605342001-03-25 19:16:13 +0000326 else if (PyString_Check(v)) {
327 Py_INCREF(v);
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000328 res = v;
Marc-André Lemburgae605342001-03-25 19:16:13 +0000329 }
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000330 else if (v->ob_type->tp_str != NULL)
331 res = (*v->ob_type->tp_str)(v);
332 else {
333 PyObject *func;
334 static PyObject *strstr;
335 if (strstr == NULL) {
336 strstr= PyString_InternFromString("__str__");
337 if (strstr == NULL)
338 return NULL;
339 }
340 if (!PyInstance_Check(v) ||
341 (func = PyObject_GetAttr(v, strstr)) == NULL) {
342 PyErr_Clear();
343 res = PyObject_Repr(v);
344 }
345 else {
346 res = PyEval_CallObject(func, (PyObject *)NULL);
347 Py_DECREF(func);
348 }
349 }
350 if (res == NULL)
351 return NULL;
352 if (!PyUnicode_Check(res)) {
353 PyObject* str;
354 str = PyUnicode_FromObject(res);
355 Py_DECREF(res);
356 if (str)
357 res = str;
358 else
359 return NULL;
360 }
361 return res;
362}
363
364
Guido van Rossumd1f06b92001-01-24 22:14:43 +0000365/* Macro to get the tp_richcompare field of a type if defined */
366#define RICHCOMPARE(t) (PyType_HasFeature((t), Py_TPFLAGS_HAVE_RICHCOMPARE) \
367 ? (t)->tp_richcompare : NULL)
368
Guido van Rossume797ec12001-01-17 15:24:28 +0000369/* Map rich comparison operators to their swapped version, e.g. LT --> GT */
370static int swapped_op[] = {Py_GT, Py_GE, Py_EQ, Py_NE, Py_LT, Py_LE};
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +0000371
Guido van Rossume797ec12001-01-17 15:24:28 +0000372/* Try a genuine rich comparison, returning an object. Return:
373 NULL for exception;
374 NotImplemented if this particular rich comparison is not implemented or
375 undefined;
376 some object not equal to NotImplemented if it is implemented
377 (this latter object may not be a Boolean).
378*/
379static PyObject *
380try_rich_compare(PyObject *v, PyObject *w, int op)
381{
382 richcmpfunc f;
383 PyObject *res;
384
Guido van Rossumd1f06b92001-01-24 22:14:43 +0000385 if ((f = RICHCOMPARE(v->ob_type)) != NULL) {
Guido van Rossume797ec12001-01-17 15:24:28 +0000386 res = (*f)(v, w, op);
387 if (res != Py_NotImplemented)
388 return res;
389 Py_DECREF(res);
390 }
Guido van Rossumd1f06b92001-01-24 22:14:43 +0000391 if ((f = RICHCOMPARE(w->ob_type)) != NULL) {
Guido van Rossume797ec12001-01-17 15:24:28 +0000392 return (*f)(w, v, swapped_op[op]);
393 }
394 res = Py_NotImplemented;
395 Py_INCREF(res);
396 return res;
397}
398
399/* Try a genuine rich comparison, returning an int. Return:
400 -1 for exception (including the case where try_rich_compare() returns an
401 object that's not a Boolean);
402 0 if the outcome is false;
403 1 if the outcome is true;
404 2 if this particular rich comparison is not implemented or undefined.
405*/
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +0000406static int
Guido van Rossume797ec12001-01-17 15:24:28 +0000407try_rich_compare_bool(PyObject *v, PyObject *w, int op)
408{
409 PyObject *res;
410 int ok;
411
Guido van Rossumd1f06b92001-01-24 22:14:43 +0000412 if (RICHCOMPARE(v->ob_type) == NULL && RICHCOMPARE(w->ob_type) == NULL)
Guido van Rossume797ec12001-01-17 15:24:28 +0000413 return 2; /* Shortcut, avoid INCREF+DECREF */
414 res = try_rich_compare(v, w, op);
415 if (res == NULL)
416 return -1;
417 if (res == Py_NotImplemented) {
418 Py_DECREF(res);
419 return 2;
420 }
421 ok = PyObject_IsTrue(res);
422 Py_DECREF(res);
423 return ok;
424}
425
426/* Try rich comparisons to determine a 3-way comparison. Return:
427 -2 for an exception;
428 -1 if v < w;
429 0 if v == w;
430 1 if v > w;
431 2 if this particular rich comparison is not implemented or undefined.
432*/
433static int
434try_rich_to_3way_compare(PyObject *v, PyObject *w)
435{
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000436 static struct { int op; int outcome; } tries[3] = {
437 /* Try this operator, and if it is true, use this outcome: */
438 {Py_EQ, 0},
439 {Py_LT, -1},
440 {Py_GT, 1},
441 };
442 int i;
443
Guido van Rossumd1f06b92001-01-24 22:14:43 +0000444 if (RICHCOMPARE(v->ob_type) == NULL && RICHCOMPARE(w->ob_type) == NULL)
Guido van Rossume797ec12001-01-17 15:24:28 +0000445 return 2; /* Shortcut */
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000446
447 for (i = 0; i < 3; i++) {
448 switch (try_rich_compare_bool(v, w, tries[i].op)) {
449 case -1:
Tim Peters6d60b2e2001-05-07 20:53:51 +0000450 return -2;
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000451 case 1:
452 return tries[i].outcome;
453 }
Guido van Rossume797ec12001-01-17 15:24:28 +0000454 }
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000455
456 return 2;
Guido van Rossume797ec12001-01-17 15:24:28 +0000457}
458
459/* Try a 3-way comparison, returning an int. Return:
460 -2 for an exception;
461 -1 if v < w;
462 0 if v == w;
463 1 if v > w;
464 2 if this particular 3-way comparison is not implemented or undefined.
465*/
466static int
467try_3way_compare(PyObject *v, PyObject *w)
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +0000468{
469 int c;
Guido van Rossume797ec12001-01-17 15:24:28 +0000470 cmpfunc f;
471
472 /* Comparisons involving instances are given to instance_compare,
473 which has the same return conventions as this function. */
474
475 if (PyInstance_Check(v))
476 return (*v->ob_type->tp_compare)(v, w);
477 if (PyInstance_Check(w))
478 return (*w->ob_type->tp_compare)(v, w);
479
Guido van Rossume797ec12001-01-17 15:24:28 +0000480 /* Try coercion; if it fails, give up */
481 c = PyNumber_CoerceEx(&v, &w);
482 if (c < 0)
483 return -2;
484 if (c > 0)
485 return 2;
486
487 /* Try v's comparison, if defined */
488 if ((f = v->ob_type->tp_compare) != NULL) {
489 c = (*f)(v, w);
490 Py_DECREF(v);
491 Py_DECREF(w);
Martin v. Löwis0163d6d2001-06-09 07:34:05 +0000492 if (c < 0 && PyErr_Occurred())
Guido van Rossume797ec12001-01-17 15:24:28 +0000493 return -2;
494 return c < 0 ? -1 : c > 0 ? 1 : 0;
495 }
496
497 /* Try w's comparison, if defined */
498 if ((f = w->ob_type->tp_compare) != NULL) {
499 c = (*f)(w, v); /* swapped! */
500 Py_DECREF(v);
501 Py_DECREF(w);
Martin v. Löwis0163d6d2001-06-09 07:34:05 +0000502 if (c < 0 && PyErr_Occurred())
Guido van Rossume797ec12001-01-17 15:24:28 +0000503 return -2;
504 return c < 0 ? 1 : c > 0 ? -1 : 0; /* negated! */
505 }
506
507 /* No comparison defined */
508 Py_DECREF(v);
509 Py_DECREF(w);
510 return 2;
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +0000511}
512
Guido van Rossume797ec12001-01-17 15:24:28 +0000513/* Final fallback 3-way comparison, returning an int. Return:
514 -2 if an error occurred;
515 -1 if v < w;
516 0 if v == w;
517 1 if v > w.
518*/
519static int
520default_3way_compare(PyObject *v, PyObject *w)
521{
522 int c;
Guido van Rossum8f9143d2001-01-22 15:59:32 +0000523 char *vname, *wname;
Guido van Rossume797ec12001-01-17 15:24:28 +0000524
525 if (v->ob_type == w->ob_type) {
Barry Warsawb0e754d2001-01-20 06:24:55 +0000526 /* When comparing these pointers, they must be cast to
527 * integer types (i.e. Py_uintptr_t, our spelling of C9X's
528 * uintptr_t). ANSI specifies that pointer compares other
529 * than == and != to non-related structures are undefined.
530 */
Barry Warsaw71ff8d52001-01-20 06:08:10 +0000531 Py_uintptr_t vv = (Py_uintptr_t)v;
532 Py_uintptr_t ww = (Py_uintptr_t)w;
Guido van Rossume797ec12001-01-17 15:24:28 +0000533 return (vv < ww) ? -1 : (vv > ww) ? 1 : 0;
534 }
535
536 /* Special case for Unicode */
537 if (PyUnicode_Check(v) || PyUnicode_Check(w)) {
538 c = PyUnicode_Compare(v, w);
539 if (!PyErr_Occurred())
540 return c;
541 /* TypeErrors are ignored: if Unicode coercion fails due
542 to one of the arguments not having the right type, we
543 continue as defined by the coercion protocol (see
544 above). Luckily, decoding errors are reported as
545 ValueErrors and are not masked by this technique. */
546 if (!PyErr_ExceptionMatches(PyExc_TypeError))
547 return -2;
548 PyErr_Clear();
549 }
550
Guido van Rossum0871e932001-01-22 19:28:09 +0000551 /* None is smaller than anything */
552 if (v == Py_None)
553 return -1;
554 if (w == Py_None)
555 return 1;
556
Guido van Rossume797ec12001-01-17 15:24:28 +0000557 /* different type: compare type names */
Guido van Rossum8f9143d2001-01-22 15:59:32 +0000558 if (v->ob_type->tp_as_number)
559 vname = "";
560 else
561 vname = v->ob_type->tp_name;
562 if (w->ob_type->tp_as_number)
563 wname = "";
564 else
565 wname = w->ob_type->tp_name;
566 c = strcmp(vname, wname);
567 if (c < 0)
568 return -1;
569 if (c > 0)
570 return 1;
571 /* Same type name, or (more likely) incomparable numeric types */
572 return ((Py_uintptr_t)(v->ob_type) < (
573 Py_uintptr_t)(w->ob_type)) ? -1 : 1;
Guido van Rossume797ec12001-01-17 15:24:28 +0000574}
575
576#define CHECK_TYPES(o) PyType_HasFeature((o)->ob_type, Py_TPFLAGS_CHECKTYPES)
577
Tim Peters6d60b2e2001-05-07 20:53:51 +0000578/* Do a 3-way comparison, by hook or by crook. Return:
579 -2 for an exception;
580 -1 if v < w;
581 0 if v == w;
582 1 if v > w;
Martin v. Löwis0163d6d2001-06-09 07:34:05 +0000583 If the object implements a tp_compare function, it returns
584 whatever this function returns (whether with an exception or not).
Tim Peters6d60b2e2001-05-07 20:53:51 +0000585*/
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +0000586static int
Fred Drake100814d2000-07-09 15:48:49 +0000587do_cmp(PyObject *v, PyObject *w)
Guido van Rossum20566841995-01-12 11:26:10 +0000588{
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +0000589 int c;
Martin v. Löwis0163d6d2001-06-09 07:34:05 +0000590 cmpfunc f;
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +0000591
Martin v. Löwis0163d6d2001-06-09 07:34:05 +0000592 if (v->ob_type == w->ob_type
593 && (f = v->ob_type->tp_compare) != NULL)
594 return (*f)(v, w);
Guido van Rossume797ec12001-01-17 15:24:28 +0000595 c = try_rich_to_3way_compare(v, w);
596 if (c < 2)
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +0000597 return c;
Guido van Rossume797ec12001-01-17 15:24:28 +0000598 c = try_3way_compare(v, w);
599 if (c < 2)
600 return c;
601 return default_3way_compare(v, w);
Guido van Rossum20566841995-01-12 11:26:10 +0000602}
603
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000604/* compare_nesting is incremented before calling compare (for
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000605 some types) and decremented on exit. If the count exceeds the
606 nesting limit, enable code to detect circular data structures.
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000607
608 This is a tunable parameter that should only affect the performance
609 of comparisons, nothing else. Setting it high makes comparing deeply
610 nested non-cyclical data structures faster, but makes comparing cyclical
611 data structures slower.
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000612*/
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000613#define NESTING_LIMIT 20
614
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000615static int compare_nesting = 0;
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000616
617static PyObject*
Fred Drake100814d2000-07-09 15:48:49 +0000618get_inprogress_dict(void)
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000619{
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000620 static PyObject *key;
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000621 PyObject *tstate_dict, *inprogress;
622
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000623 if (key == NULL) {
624 key = PyString_InternFromString("cmp_state");
625 if (key == NULL)
626 return NULL;
627 }
628
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000629 tstate_dict = PyThreadState_GetDict();
630 if (tstate_dict == NULL) {
631 PyErr_BadInternalCall();
632 return NULL;
633 }
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000634
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000635 inprogress = PyDict_GetItem(tstate_dict, key);
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000636 if (inprogress == NULL) {
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000637 inprogress = PyDict_New();
638 if (inprogress == NULL)
639 return NULL;
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000640 if (PyDict_SetItem(tstate_dict, key, inprogress) == -1) {
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000641 Py_DECREF(inprogress);
642 return NULL;
643 }
Jeremy Hyltona251ea02000-06-09 16:20:39 +0000644 Py_DECREF(inprogress);
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000645 }
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000646
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000647 return inprogress;
648}
649
650static PyObject *
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000651check_recursion(PyObject *v, PyObject *w, int op)
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000652{
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000653 PyObject *inprogress;
654 PyObject *token;
Barry Warsaw9d23a4e2000-08-18 05:01:19 +0000655 Py_uintptr_t iv = (Py_uintptr_t)v;
656 Py_uintptr_t iw = (Py_uintptr_t)w;
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000657 PyObject *x, *y, *z;
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000658
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000659 inprogress = get_inprogress_dict();
660 if (inprogress == NULL)
661 return NULL;
662
663 token = PyTuple_New(3);
664 if (token == NULL)
665 return NULL;
666
667 if (iv <= iw) {
668 PyTuple_SET_ITEM(token, 0, x = PyLong_FromVoidPtr((void *)v));
669 PyTuple_SET_ITEM(token, 1, y = PyLong_FromVoidPtr((void *)w));
670 if (op >= 0)
671 op = swapped_op[op];
672 } else {
673 PyTuple_SET_ITEM(token, 0, x = PyLong_FromVoidPtr((void *)w));
674 PyTuple_SET_ITEM(token, 1, y = PyLong_FromVoidPtr((void *)v));
675 }
676 PyTuple_SET_ITEM(token, 2, z = PyInt_FromLong((long)op));
677 if (x == NULL || y == NULL || z == NULL) {
678 Py_DECREF(token);
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000679 return NULL;
680 }
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000681
682 if (PyDict_GetItem(inprogress, token) != NULL) {
683 Py_DECREF(token);
684 return Py_None; /* Without INCREF! */
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000685 }
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000686
687 if (PyDict_SetItem(inprogress, token, token) < 0) {
688 Py_DECREF(token);
689 return NULL;
690 }
691
692 return token;
693}
694
695static void
696delete_token(PyObject *token)
697{
698 PyObject *inprogress;
699
700 if (token == NULL || token == Py_None)
701 return;
702 inprogress = get_inprogress_dict();
703 if (inprogress == NULL)
704 PyErr_Clear();
705 else
706 PyDict_DelItem(inprogress, token);
707 Py_DECREF(token);
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000708}
709
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000710int
Fred Drake100814d2000-07-09 15:48:49 +0000711PyObject_Compare(PyObject *v, PyObject *w)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000712{
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000713 PyTypeObject *vtp;
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000714 int result;
715
Jack Jansend49cbe12000-08-22 21:52:51 +0000716#if defined(USE_STACKCHECK)
Guido van Rossum9c0a99e2000-08-30 15:53:50 +0000717 if (PyOS_CheckStack()) {
Jack Jansend49cbe12000-08-22 21:52:51 +0000718 PyErr_SetString(PyExc_MemoryError, "Stack overflow");
719 return -1;
720 }
721#endif
Guido van Rossumc8b6df91997-05-23 00:06:51 +0000722 if (v == NULL || w == NULL) {
723 PyErr_BadInternalCall();
724 return -1;
725 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000726 if (v == w)
727 return 0;
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +0000728 vtp = v->ob_type;
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000729 compare_nesting++;
730 if (compare_nesting > NESTING_LIMIT &&
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +0000731 (vtp->tp_as_mapping
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000732 || (vtp->tp_as_sequence
733 && !PyString_Check(v)
734 && !PyTuple_Check(v)))) {
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +0000735 /* try to detect circular data structures */
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000736 PyObject *token = check_recursion(v, w, -1);
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000737
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000738 if (token == NULL) {
739 result = -1;
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000740 }
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000741 else if (token == Py_None) {
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000742 /* already comparing these objects. assume
743 they're equal until shown otherwise */
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +0000744 result = 0;
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000745 }
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000746 else {
747 result = do_cmp(v, w);
748 delete_token(token);
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000749 }
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000750 }
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +0000751 else {
752 result = do_cmp(v, w);
753 }
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000754 compare_nesting--;
Guido van Rossume797ec12001-01-17 15:24:28 +0000755 return result < 0 ? -1 : result;
756}
757
758static PyObject *
Martin v. Löwis0163d6d2001-06-09 07:34:05 +0000759convert_3way_to_object(int op, int c)
Guido van Rossume797ec12001-01-17 15:24:28 +0000760{
Guido van Rossume797ec12001-01-17 15:24:28 +0000761 PyObject *result;
Guido van Rossume797ec12001-01-17 15:24:28 +0000762 switch (op) {
763 case Py_LT: c = c < 0; break;
764 case Py_LE: c = c <= 0; break;
765 case Py_EQ: c = c == 0; break;
766 case Py_NE: c = c != 0; break;
767 case Py_GT: c = c > 0; break;
768 case Py_GE: c = c >= 0; break;
769 }
770 result = c ? Py_True : Py_False;
771 Py_INCREF(result);
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000772 return result;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000773}
Martin v. Löwis0163d6d2001-06-09 07:34:05 +0000774
775
776static PyObject *
777try_3way_to_rich_compare(PyObject *v, PyObject *w, int op)
778{
779 int c;
780
781 c = try_3way_compare(v, w);
782 if (c >= 2)
783 c = default_3way_compare(v, w);
784 if (c <= -2)
785 return NULL;
786 return convert_3way_to_object(op, c);
787}
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000788
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;
Martin v. Löwis0163d6d2001-06-09 07:34:05 +0000793 cmpfunc f;
794
795 /* If the types are equal, don't bother with coercions etc.
796 Instances are special-cased in try_3way_compare, since
797 a result of 2 does *not* mean one value being greater
798 than the other. */
799 if (v->ob_type == w->ob_type
800 && (f = v->ob_type->tp_compare) != NULL
801 && !PyInstance_Check(v)) {
802 int c;
803 richcmpfunc f1;
804 if ((f1 = RICHCOMPARE(v->ob_type)) != NULL) {
805 /* If the type has richcmp, try it first.
806 try_rich_compare would try it two-sided,
807 which is not needed since we've a single
808 type only. */
809 res = (*f1)(v, w, op);
810 if (res != Py_NotImplemented)
811 return res;
812 Py_DECREF(res);
813 }
814 c = (*f)(v, w);
815 if (c < 0 && PyErr_Occurred())
816 return NULL;
817 return convert_3way_to_object(op, c);
818 }
Guido van Rossume797ec12001-01-17 15:24:28 +0000819
820 res = try_rich_compare(v, w, op);
821 if (res != Py_NotImplemented)
822 return res;
823 Py_DECREF(res);
824
825 return try_3way_to_rich_compare(v, w, op);
826}
827
828PyObject *
829PyObject_RichCompare(PyObject *v, PyObject *w, int op)
830{
831 PyObject *res;
832
833 assert(Py_LT <= op && op <= Py_GE);
834
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000835 compare_nesting++;
836 if (compare_nesting > NESTING_LIMIT &&
837 (v->ob_type->tp_as_mapping
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000838 || (v->ob_type->tp_as_sequence
839 && !PyString_Check(v)
840 && !PyTuple_Check(v)))) {
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000841 /* try to detect circular data structures */
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000842 PyObject *token = check_recursion(v, w, op);
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000843
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000844 if (token == NULL) {
845 res = NULL;
Guido van Rossume797ec12001-01-17 15:24:28 +0000846 }
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000847 else if (token == Py_None) {
848 /* already comparing these objects with this operator.
849 assume they're equal until shown otherwise */
850 if (op == Py_EQ)
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000851 res = Py_True;
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000852 else if (op == Py_NE)
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000853 res = Py_False;
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000854 else {
855 PyErr_SetString(PyExc_ValueError,
856 "can't order recursive values");
857 res = NULL;
858 }
859 Py_XINCREF(res);
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000860 }
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000861 else {
862 res = do_richcmp(v, w, op);
863 delete_token(token);
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000864 }
Guido van Rossume797ec12001-01-17 15:24:28 +0000865 }
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000866 else {
867 res = do_richcmp(v, w, op);
868 }
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000869 compare_nesting--;
Guido van Rossume797ec12001-01-17 15:24:28 +0000870 return res;
871}
872
Tim Petersde9725f2001-05-05 10:06:17 +0000873/* Return -1 if error; 1 if v op w; 0 if not (v op w). */
Guido van Rossume797ec12001-01-17 15:24:28 +0000874int
875PyObject_RichCompareBool(PyObject *v, PyObject *w, int op)
876{
877 PyObject *res = PyObject_RichCompare(v, w, op);
878 int ok;
879
880 if (res == NULL)
881 return -1;
882 ok = PyObject_IsTrue(res);
883 Py_DECREF(res);
884 return ok;
885}
Fred Drake13634cf2000-06-29 19:17:04 +0000886
887/* Set of hash utility functions to help maintaining the invariant that
888 iff a==b then hash(a)==hash(b)
889
890 All the utility functions (_Py_Hash*()) return "-1" to signify an error.
891*/
892
893long
Fred Drake100814d2000-07-09 15:48:49 +0000894_Py_HashDouble(double v)
Fred Drake13634cf2000-06-29 19:17:04 +0000895{
Tim Peters39dce292000-08-15 03:34:48 +0000896 double intpart, fractpart;
897 int expo;
898 long hipart;
899 long x; /* the final hash value */
900 /* This is designed so that Python numbers of different types
901 * that compare equal hash to the same value; otherwise comparisons
902 * of mapping keys will turn out weird.
903 */
904
905#ifdef MPW /* MPW C modf expects pointer to extended as second argument */
906{
907 extended e;
908 fractpart = modf(v, &e);
909 intpart = e;
910}
911#else
912 fractpart = modf(v, &intpart);
913#endif
914 if (fractpart == 0.0) {
915 /* This must return the same hash as an equal int or long. */
916 if (intpart > LONG_MAX || -intpart > LONG_MAX) {
917 /* Convert to long and use its hash. */
918 PyObject *plong; /* converted to Python long */
919 if (Py_IS_INFINITY(intpart))
920 /* can't convert to long int -- arbitrary */
921 v = v < 0 ? -271828.0 : 314159.0;
922 plong = PyLong_FromDouble(v);
923 if (plong == NULL)
924 return -1;
925 x = PyObject_Hash(plong);
926 Py_DECREF(plong);
927 return x;
928 }
929 /* Fits in a C long == a Python int, so is its own hash. */
930 x = (long)intpart;
931 if (x == -1)
932 x = -2;
933 return x;
934 }
935 /* The fractional part is non-zero, so we don't have to worry about
936 * making this match the hash of some other type.
937 * Use frexp to get at the bits in the double.
Fred Drake13634cf2000-06-29 19:17:04 +0000938 * Since the VAX D double format has 56 mantissa bits, which is the
939 * most of any double format in use, each of these parts may have as
940 * many as (but no more than) 56 significant bits.
Tim Peters39dce292000-08-15 03:34:48 +0000941 * So, assuming sizeof(long) >= 4, each part can be broken into two
942 * longs; frexp and multiplication are used to do that.
943 * Also, since the Cray double format has 15 exponent bits, which is
944 * the most of any double format in use, shifting the exponent field
945 * left by 15 won't overflow a long (again assuming sizeof(long) >= 4).
Fred Drake13634cf2000-06-29 19:17:04 +0000946 */
Tim Peters39dce292000-08-15 03:34:48 +0000947 v = frexp(v, &expo);
948 v *= 2147483648.0; /* 2**31 */
949 hipart = (long)v; /* take the top 32 bits */
950 v = (v - (double)hipart) * 2147483648.0; /* get the next 32 bits */
951 x = hipart + (long)v + (expo << 15);
952 if (x == -1)
953 x = -2;
954 return x;
Fred Drake13634cf2000-06-29 19:17:04 +0000955}
956
957long
Fred Drake100814d2000-07-09 15:48:49 +0000958_Py_HashPointer(void *p)
Fred Drake13634cf2000-06-29 19:17:04 +0000959{
960#if SIZEOF_LONG >= SIZEOF_VOID_P
961 return (long)p;
962#else
963 /* convert to a Python long and hash that */
964 PyObject* longobj;
965 long x;
966
967 if ((longobj = PyLong_FromVoidPtr(p)) == NULL) {
968 x = -1;
969 goto finally;
970 }
971 x = PyObject_Hash(longobj);
972
973finally:
974 Py_XDECREF(longobj);
975 return x;
976#endif
977}
978
979
Guido van Rossum9bfef441993-03-29 10:43:31 +0000980long
Fred Drake100814d2000-07-09 15:48:49 +0000981PyObject_Hash(PyObject *v)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000982{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000983 PyTypeObject *tp = v->ob_type;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000984 if (tp->tp_hash != NULL)
985 return (*tp->tp_hash)(v);
Guido van Rossumd1f06b92001-01-24 22:14:43 +0000986 if (tp->tp_compare == NULL && RICHCOMPARE(tp) == NULL) {
Fred Drake13634cf2000-06-29 19:17:04 +0000987 return _Py_HashPointer(v); /* Use address as hash value */
988 }
Guido van Rossum9bfef441993-03-29 10:43:31 +0000989 /* If there's a cmp but no hash defined, the object can't be hashed */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000990 PyErr_SetString(PyExc_TypeError, "unhashable type");
Guido van Rossum9bfef441993-03-29 10:43:31 +0000991 return -1;
992}
993
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000994PyObject *
Fred Drake100814d2000-07-09 15:48:49 +0000995PyObject_GetAttrString(PyObject *v, char *name)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000996{
Guido van Rossumd8eb1b31996-08-09 20:52:03 +0000997 if (v->ob_type->tp_getattro != NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000998 PyObject *w, *res;
Guido van Rossumc6d06701997-01-18 07:57:16 +0000999 w = PyString_InternFromString(name);
Guido van Rossumd8eb1b31996-08-09 20:52:03 +00001000 if (w == NULL)
1001 return NULL;
1002 res = (*v->ob_type->tp_getattro)(v, w);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001003 Py_XDECREF(w);
Guido van Rossumd8eb1b31996-08-09 20:52:03 +00001004 return res;
1005 }
1006
Guido van Rossum3f5da241990-12-20 15:06:42 +00001007 if (v->ob_type->tp_getattr == NULL) {
Guido van Rossumdb935161998-01-19 22:16:36 +00001008 PyErr_Format(PyExc_AttributeError,
1009 "'%.50s' object has no attribute '%.400s'",
1010 v->ob_type->tp_name,
1011 name);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001012 return NULL;
1013 }
1014 else {
1015 return (*v->ob_type->tp_getattr)(v, name);
1016 }
1017}
1018
1019int
Fred Drake100814d2000-07-09 15:48:49 +00001020PyObject_HasAttrString(PyObject *v, char *name)
Guido van Rossumed18fdc1993-07-11 19:55:34 +00001021{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001022 PyObject *res = PyObject_GetAttrString(v, name);
Guido van Rossumed18fdc1993-07-11 19:55:34 +00001023 if (res != NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001024 Py_DECREF(res);
Guido van Rossumed18fdc1993-07-11 19:55:34 +00001025 return 1;
1026 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001027 PyErr_Clear();
Guido van Rossumed18fdc1993-07-11 19:55:34 +00001028 return 0;
1029}
1030
1031int
Fred Drake100814d2000-07-09 15:48:49 +00001032PyObject_SetAttrString(PyObject *v, char *name, PyObject *w)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001033{
Guido van Rossumd8eb1b31996-08-09 20:52:03 +00001034 if (v->ob_type->tp_setattro != NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001035 PyObject *s;
Guido van Rossumd8eb1b31996-08-09 20:52:03 +00001036 int res;
Guido van Rossumc6d06701997-01-18 07:57:16 +00001037 s = PyString_InternFromString(name);
Guido van Rossumd8eb1b31996-08-09 20:52:03 +00001038 if (s == NULL)
Guido van Rossumb7fc3041996-09-11 22:51:25 +00001039 return -1;
Guido van Rossumd8eb1b31996-08-09 20:52:03 +00001040 res = (*v->ob_type->tp_setattro)(v, s, w);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001041 Py_XDECREF(s);
Guido van Rossumd8eb1b31996-08-09 20:52:03 +00001042 return res;
1043 }
1044
Guido van Rossum3f5da241990-12-20 15:06:42 +00001045 if (v->ob_type->tp_setattr == NULL) {
1046 if (v->ob_type->tp_getattr == NULL)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001047 PyErr_SetString(PyExc_TypeError,
Guido van Rossum3ea74121991-12-24 13:28:03 +00001048 "attribute-less object (assign or del)");
Guido van Rossum3f5da241990-12-20 15:06:42 +00001049 else
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001050 PyErr_SetString(PyExc_TypeError,
Guido van Rossum3ea74121991-12-24 13:28:03 +00001051 "object has read-only attributes");
Guido van Rossum73531a31990-12-20 23:12:40 +00001052 return -1;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001053 }
1054 else {
1055 return (*v->ob_type->tp_setattr)(v, name, w);
1056 }
1057}
1058
Guido van Rossum98ff96a1997-05-20 18:34:44 +00001059PyObject *
Fred Drake100814d2000-07-09 15:48:49 +00001060PyObject_GetAttr(PyObject *v, PyObject *name)
Guido van Rossum98ff96a1997-05-20 18:34:44 +00001061{
Marc-André Lemburge44e5072000-09-18 16:20:57 +00001062 /* The Unicode to string conversion is done here because the
1063 existing tp_getattro slots expect a string object as name
1064 and we wouldn't want to break those. */
1065 if (PyUnicode_Check(name)) {
1066 name = _PyUnicode_AsDefaultEncodedString(name, NULL);
1067 if (name == NULL)
1068 return NULL;
1069 }
Jeremy Hylton99a8f902000-06-23 14:36:32 +00001070
1071 if (!PyString_Check(name)) {
1072 PyErr_SetString(PyExc_TypeError,
1073 "attribute name must be string");
1074 return NULL;
1075 }
Marc-André Lemburge44e5072000-09-18 16:20:57 +00001076 if (v->ob_type->tp_getattro != NULL)
1077 return (*v->ob_type->tp_getattro)(v, name);
1078 else
Tim Peters5acbfcc2001-05-11 03:36:45 +00001079 return PyObject_GetAttrString(v, PyString_AS_STRING(name));
Guido van Rossum98ff96a1997-05-20 18:34:44 +00001080}
1081
1082int
Fred Drake100814d2000-07-09 15:48:49 +00001083PyObject_HasAttr(PyObject *v, PyObject *name)
Guido van Rossum98ff96a1997-05-20 18:34:44 +00001084{
1085 PyObject *res = PyObject_GetAttr(v, name);
1086 if (res != NULL) {
1087 Py_DECREF(res);
1088 return 1;
1089 }
1090 PyErr_Clear();
1091 return 0;
1092}
1093
1094int
Fred Drake100814d2000-07-09 15:48:49 +00001095PyObject_SetAttr(PyObject *v, PyObject *name, PyObject *value)
Guido van Rossum98ff96a1997-05-20 18:34:44 +00001096{
1097 int err;
Marc-André Lemburge44e5072000-09-18 16:20:57 +00001098
1099 /* The Unicode to string conversion is done here because the
1100 existing tp_setattro slots expect a string object as name
1101 and we wouldn't want to break those. */
1102 if (PyUnicode_Check(name)) {
1103 name = PyUnicode_AsEncodedString(name, NULL, NULL);
1104 if (name == NULL)
1105 return -1;
Jeremy Hylton99a8f902000-06-23 14:36:32 +00001106 }
Marc-André Lemburge44e5072000-09-18 16:20:57 +00001107 else
1108 Py_INCREF(name);
1109
1110 if (!PyString_Check(name)){
Jeremy Hylton99a8f902000-06-23 14:36:32 +00001111 PyErr_SetString(PyExc_TypeError,
1112 "attribute name must be string");
1113 err = -1;
1114 }
Marc-André Lemburge44e5072000-09-18 16:20:57 +00001115 else {
1116 PyString_InternInPlace(&name);
1117 if (v->ob_type->tp_setattro != NULL)
1118 err = (*v->ob_type->tp_setattro)(v, name, value);
1119 else
1120 err = PyObject_SetAttrString(v,
1121 PyString_AS_STRING(name), value);
1122 }
1123
Guido van Rossum98ff96a1997-05-20 18:34:44 +00001124 Py_DECREF(name);
1125 return err;
1126}
1127
Guido van Rossum6ac258d1993-05-12 08:24:20 +00001128/* Test a value used as condition, e.g., in a for or if statement.
1129 Return -1 if an error occurred */
1130
1131int
Fred Drake100814d2000-07-09 15:48:49 +00001132PyObject_IsTrue(PyObject *v)
Guido van Rossum6ac258d1993-05-12 08:24:20 +00001133{
1134 int res;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001135 if (v == Py_None)
Guido van Rossum6ac258d1993-05-12 08:24:20 +00001136 res = 0;
Guido van Rossum1c4f4581998-05-22 00:53:24 +00001137 else if (v->ob_type->tp_as_number != NULL &&
1138 v->ob_type->tp_as_number->nb_nonzero != NULL)
Guido van Rossum6ac258d1993-05-12 08:24:20 +00001139 res = (*v->ob_type->tp_as_number->nb_nonzero)(v);
Guido van Rossum1c4f4581998-05-22 00:53:24 +00001140 else if (v->ob_type->tp_as_mapping != NULL &&
1141 v->ob_type->tp_as_mapping->mp_length != NULL)
Guido van Rossum6ac258d1993-05-12 08:24:20 +00001142 res = (*v->ob_type->tp_as_mapping->mp_length)(v);
Guido van Rossum1c4f4581998-05-22 00:53:24 +00001143 else if (v->ob_type->tp_as_sequence != NULL &&
1144 v->ob_type->tp_as_sequence->sq_length != NULL)
Guido van Rossum6ac258d1993-05-12 08:24:20 +00001145 res = (*v->ob_type->tp_as_sequence->sq_length)(v);
1146 else
1147 res = 1;
1148 if (res > 0)
1149 res = 1;
1150 return res;
1151}
1152
Guido van Rossumc3d3f961998-04-09 17:53:59 +00001153/* equivalent of 'not v'
1154 Return -1 if an error occurred */
1155
1156int
Fred Drake100814d2000-07-09 15:48:49 +00001157PyObject_Not(PyObject *v)
Guido van Rossumc3d3f961998-04-09 17:53:59 +00001158{
1159 int res;
1160 res = PyObject_IsTrue(v);
1161 if (res < 0)
1162 return res;
1163 return res == 0;
1164}
1165
Guido van Rossum5524a591995-01-10 15:26:20 +00001166/* Coerce two numeric types to the "larger" one.
1167 Increment the reference count on each argument.
Guido van Rossume797ec12001-01-17 15:24:28 +00001168 Return value:
1169 -1 if an error occurred;
1170 0 if the coercion succeeded (and then the reference counts are increased);
1171 1 if no coercion is possible (and no error is raised).
Guido van Rossum5524a591995-01-10 15:26:20 +00001172*/
Guido van Rossum5524a591995-01-10 15:26:20 +00001173int
Fred Drake100814d2000-07-09 15:48:49 +00001174PyNumber_CoerceEx(PyObject **pv, PyObject **pw)
Guido van Rossum5524a591995-01-10 15:26:20 +00001175{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001176 register PyObject *v = *pv;
1177 register PyObject *w = *pw;
Guido van Rossum5524a591995-01-10 15:26:20 +00001178 int res;
1179
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001180 if (v->ob_type == w->ob_type && !PyInstance_Check(v)) {
1181 Py_INCREF(v);
1182 Py_INCREF(w);
Guido van Rossum5524a591995-01-10 15:26:20 +00001183 return 0;
1184 }
1185 if (v->ob_type->tp_as_number && v->ob_type->tp_as_number->nb_coerce) {
1186 res = (*v->ob_type->tp_as_number->nb_coerce)(pv, pw);
1187 if (res <= 0)
1188 return res;
1189 }
1190 if (w->ob_type->tp_as_number && w->ob_type->tp_as_number->nb_coerce) {
1191 res = (*w->ob_type->tp_as_number->nb_coerce)(pw, pv);
1192 if (res <= 0)
1193 return res;
1194 }
Guido van Rossum242c6421997-11-19 16:03:17 +00001195 return 1;
1196}
1197
Guido van Rossume797ec12001-01-17 15:24:28 +00001198/* Coerce two numeric types to the "larger" one.
1199 Increment the reference count on each argument.
1200 Return -1 and raise an exception if no coercion is possible
1201 (and then no reference count is incremented).
1202*/
Guido van Rossum242c6421997-11-19 16:03:17 +00001203int
Fred Drake100814d2000-07-09 15:48:49 +00001204PyNumber_Coerce(PyObject **pv, PyObject **pw)
Guido van Rossum242c6421997-11-19 16:03:17 +00001205{
1206 int err = PyNumber_CoerceEx(pv, pw);
1207 if (err <= 0)
1208 return err;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001209 PyErr_SetString(PyExc_TypeError, "number coercion failed");
Guido van Rossum5524a591995-01-10 15:26:20 +00001210 return -1;
1211}
1212
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001213
Guido van Rossum49b11fe1995-01-26 00:38:22 +00001214/* Test whether an object can be called */
1215
1216int
Fred Drake100814d2000-07-09 15:48:49 +00001217PyCallable_Check(PyObject *x)
Guido van Rossum49b11fe1995-01-26 00:38:22 +00001218{
1219 if (x == NULL)
1220 return 0;
1221 if (x->ob_type->tp_call != NULL ||
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001222 PyFunction_Check(x) ||
1223 PyMethod_Check(x) ||
1224 PyCFunction_Check(x) ||
1225 PyClass_Check(x))
Guido van Rossum49b11fe1995-01-26 00:38:22 +00001226 return 1;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001227 if (PyInstance_Check(x)) {
1228 PyObject *call = PyObject_GetAttrString(x, "__call__");
Guido van Rossum49b11fe1995-01-26 00:38:22 +00001229 if (call == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001230 PyErr_Clear();
Guido van Rossum49b11fe1995-01-26 00:38:22 +00001231 return 0;
1232 }
1233 /* Could test recursively but don't, for fear of endless
1234 recursion if some joker sets self.__call__ = self */
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001235 Py_DECREF(call);
Guido van Rossum49b11fe1995-01-26 00:38:22 +00001236 return 1;
1237 }
1238 return 0;
1239}
1240
1241
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001242/*
1243NoObject is usable as a non-NULL undefined value, used by the macro None.
1244There is (and should be!) no way to create other objects of this type,
Guido van Rossum3f5da241990-12-20 15:06:42 +00001245so there is exactly one (which is indestructible, by the way).
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001246*/
1247
Guido van Rossum0c182a11992-03-27 17:26:13 +00001248/* ARGSUSED */
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001249static PyObject *
Fred Drake100814d2000-07-09 15:48:49 +00001250none_repr(PyObject *op)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001251{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001252 return PyString_FromString("None");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001253}
1254
Barry Warsaw9bf16442001-01-23 16:24:35 +00001255/* ARGUSED */
1256static void
1257none_dealloc(PyObject* ignore)
1258{
1259 /* This should never get called, but we also don't want to SEGV if
1260 * we accidently decref None out of existance.
1261 */
1262 abort();
1263}
1264
1265
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001266static PyTypeObject PyNothing_Type = {
1267 PyObject_HEAD_INIT(&PyType_Type)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001268 0,
Guido van Rossum3f5da241990-12-20 15:06:42 +00001269 "None",
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001270 0,
1271 0,
Barry Warsaw9bf16442001-01-23 16:24:35 +00001272 (destructor)none_dealloc, /*tp_dealloc*/ /*never called*/
Guido van Rossum7066dd71992-09-17 17:54:56 +00001273 0, /*tp_print*/
Guido van Rossum3f5da241990-12-20 15:06:42 +00001274 0, /*tp_getattr*/
1275 0, /*tp_setattr*/
1276 0, /*tp_compare*/
Guido van Rossum1d5735e1994-08-30 08:27:36 +00001277 (reprfunc)none_repr, /*tp_repr*/
Guido van Rossum3f5da241990-12-20 15:06:42 +00001278 0, /*tp_as_number*/
1279 0, /*tp_as_sequence*/
1280 0, /*tp_as_mapping*/
Guido van Rossum9bfef441993-03-29 10:43:31 +00001281 0, /*tp_hash */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001282};
1283
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001284PyObject _Py_NoneStruct = {
1285 PyObject_HEAD_INIT(&PyNothing_Type)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001286};
1287
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +00001288/* NotImplemented is an object that can be used to signal that an
1289 operation is not implemented for the given type combination. */
1290
1291static PyObject *
1292NotImplemented_repr(PyObject *op)
1293{
1294 return PyString_FromString("NotImplemented");
1295}
1296
1297static PyTypeObject PyNotImplemented_Type = {
1298 PyObject_HEAD_INIT(&PyType_Type)
1299 0,
1300 "NotImplemented",
1301 0,
1302 0,
Barry Warsaw9bf16442001-01-23 16:24:35 +00001303 (destructor)none_dealloc, /*tp_dealloc*/ /*never called*/
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +00001304 0, /*tp_print*/
1305 0, /*tp_getattr*/
1306 0, /*tp_setattr*/
1307 0, /*tp_compare*/
1308 (reprfunc)NotImplemented_repr, /*tp_repr*/
1309 0, /*tp_as_number*/
1310 0, /*tp_as_sequence*/
1311 0, /*tp_as_mapping*/
1312 0, /*tp_hash */
1313};
1314
1315PyObject _Py_NotImplementedStruct = {
1316 PyObject_HEAD_INIT(&PyNotImplemented_Type)
1317};
1318
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001319
Guido van Rossum84a90321996-05-22 16:34:47 +00001320#ifdef Py_TRACE_REFS
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001321
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001322static PyObject refchain = {&refchain, &refchain};
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001323
Guido van Rossumaacdc9d1996-08-12 21:32:12 +00001324void
Fred Drake100814d2000-07-09 15:48:49 +00001325_Py_ResetReferences(void)
Guido van Rossume09fb551997-08-05 02:04:34 +00001326{
1327 refchain._ob_prev = refchain._ob_next = &refchain;
1328 _Py_RefTotal = 0;
1329}
1330
1331void
Fred Drake100814d2000-07-09 15:48:49 +00001332_Py_NewReference(PyObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001333{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001334 _Py_RefTotal++;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001335 op->ob_refcnt = 1;
1336 op->_ob_next = refchain._ob_next;
1337 op->_ob_prev = &refchain;
1338 refchain._ob_next->_ob_prev = op;
1339 refchain._ob_next = op;
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +00001340#ifdef COUNT_ALLOCS
1341 inc_count(op->ob_type);
1342#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001343}
1344
Guido van Rossumaacdc9d1996-08-12 21:32:12 +00001345void
Fred Drake100814d2000-07-09 15:48:49 +00001346_Py_ForgetReference(register PyObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001347{
Guido van Rossumbffd6832000-01-20 22:32:56 +00001348#ifdef SLOW_UNREF_CHECK
Guido van Rossum4c08d552000-03-10 22:55:18 +00001349 register PyObject *p;
Guido van Rossumbffd6832000-01-20 22:32:56 +00001350#endif
Guido van Rossumd7047b31995-01-02 19:07:15 +00001351 if (op->ob_refcnt < 0)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001352 Py_FatalError("UNREF negative refcnt");
Guido van Rossum2e8f6141992-09-03 20:32:55 +00001353 if (op == &refchain ||
Guido van Rossumd7047b31995-01-02 19:07:15 +00001354 op->_ob_prev->_ob_next != op || op->_ob_next->_ob_prev != op)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001355 Py_FatalError("UNREF invalid object");
Guido van Rossum2e8f6141992-09-03 20:32:55 +00001356#ifdef SLOW_UNREF_CHECK
Guido van Rossum3f5da241990-12-20 15:06:42 +00001357 for (p = refchain._ob_next; p != &refchain; p = p->_ob_next) {
1358 if (p == op)
1359 break;
1360 }
Guido van Rossumd7047b31995-01-02 19:07:15 +00001361 if (p == &refchain) /* Not found */
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001362 Py_FatalError("UNREF unknown object");
Guido van Rossum2e8f6141992-09-03 20:32:55 +00001363#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001364 op->_ob_next->_ob_prev = op->_ob_prev;
1365 op->_ob_prev->_ob_next = op->_ob_next;
Guido van Rossum2e8f6141992-09-03 20:32:55 +00001366 op->_ob_next = op->_ob_prev = NULL;
Guido van Rossumd8953cb1995-04-06 14:46:26 +00001367#ifdef COUNT_ALLOCS
1368 op->ob_type->tp_free++;
1369#endif
Guido van Rossum3f5da241990-12-20 15:06:42 +00001370}
1371
Guido van Rossumaacdc9d1996-08-12 21:32:12 +00001372void
Fred Drake100814d2000-07-09 15:48:49 +00001373_Py_Dealloc(PyObject *op)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001374{
Guido van Rossum9776adf1994-09-07 14:36:45 +00001375 destructor dealloc = op->ob_type->tp_dealloc;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001376 _Py_ForgetReference(op);
Guido van Rossum9776adf1994-09-07 14:36:45 +00001377 (*dealloc)(op);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001378}
1379
Guido van Rossumaacdc9d1996-08-12 21:32:12 +00001380void
Fred Drake100814d2000-07-09 15:48:49 +00001381_Py_PrintReferences(FILE *fp)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001382{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001383 PyObject *op;
Guido van Rossume09fb551997-08-05 02:04:34 +00001384 fprintf(fp, "Remaining objects:\n");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001385 for (op = refchain._ob_next; op != &refchain; op = op->_ob_next) {
1386 fprintf(fp, "[%d] ", op->ob_refcnt);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001387 if (PyObject_Print(op, fp, 0) != 0)
1388 PyErr_Clear();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001389 putc('\n', fp);
1390 }
1391}
1392
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +00001393PyObject *
Fred Drake100814d2000-07-09 15:48:49 +00001394_Py_GetObjects(PyObject *self, PyObject *args)
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +00001395{
1396 int i, n;
1397 PyObject *t = NULL;
1398 PyObject *res, *op;
1399
1400 if (!PyArg_ParseTuple(args, "i|O", &n, &t))
1401 return NULL;
1402 op = refchain._ob_next;
1403 res = PyList_New(0);
1404 if (res == NULL)
1405 return NULL;
1406 for (i = 0; (n == 0 || i < n) && op != &refchain; i++) {
1407 while (op == self || op == args || op == res || op == t ||
Guido van Rossum3c296022001-07-14 17:58:00 +00001408 (t != NULL && op->ob_type != (PyTypeObject *) t)) {
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +00001409 op = op->_ob_next;
1410 if (op == &refchain)
1411 return res;
1412 }
1413 if (PyList_Append(res, op) < 0) {
1414 Py_DECREF(res);
1415 return NULL;
1416 }
1417 op = op->_ob_next;
1418 }
1419 return res;
1420}
1421
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001422#endif
Guido van Rossum97ead3f1996-01-12 01:24:09 +00001423
1424
1425/* Hack to force loading of cobject.o */
Guido van Rossumda9c2711996-12-05 21:58:58 +00001426PyTypeObject *_Py_cobject_hack = &PyCObject_Type;
Guido van Rossum84a90321996-05-22 16:34:47 +00001427
1428
1429/* Hack to force loading of abstract.o */
Jeremy Hylton6253f832000-07-12 12:56:19 +00001430int (*_Py_abstract_hack)(PyObject *) = &PyObject_Size;
Guido van Rossume09fb551997-08-05 02:04:34 +00001431
1432
Andrew M. Kuchling1582a3a2000-08-16 12:27:23 +00001433/* Python's malloc wrappers (see pymem.h) */
Guido van Rossume09fb551997-08-05 02:04:34 +00001434
Thomas Wouters334fb892000-07-25 12:56:38 +00001435void *
Fred Drake100814d2000-07-09 15:48:49 +00001436PyMem_Malloc(size_t nbytes)
Guido van Rossume09fb551997-08-05 02:04:34 +00001437{
1438#if _PyMem_EXTRA > 0
1439 if (nbytes == 0)
1440 nbytes = _PyMem_EXTRA;
1441#endif
Guido van Rossumb18618d2000-05-03 23:44:39 +00001442 return PyMem_MALLOC(nbytes);
Guido van Rossume09fb551997-08-05 02:04:34 +00001443}
1444
Thomas Wouters334fb892000-07-25 12:56:38 +00001445void *
1446PyMem_Realloc(void *p, size_t nbytes)
Guido van Rossume09fb551997-08-05 02:04:34 +00001447{
1448#if _PyMem_EXTRA > 0
1449 if (nbytes == 0)
1450 nbytes = _PyMem_EXTRA;
1451#endif
Guido van Rossumb18618d2000-05-03 23:44:39 +00001452 return PyMem_REALLOC(p, nbytes);
Guido van Rossume09fb551997-08-05 02:04:34 +00001453}
1454
1455void
Thomas Wouters334fb892000-07-25 12:56:38 +00001456PyMem_Free(void *p)
Guido van Rossume09fb551997-08-05 02:04:34 +00001457{
Guido van Rossumb18618d2000-05-03 23:44:39 +00001458 PyMem_FREE(p);
1459}
1460
1461
1462/* Python's object malloc wrappers (see objimpl.h) */
1463
Thomas Wouters334fb892000-07-25 12:56:38 +00001464void *
Fred Drake100814d2000-07-09 15:48:49 +00001465PyObject_Malloc(size_t nbytes)
Guido van Rossumb18618d2000-05-03 23:44:39 +00001466{
1467 return PyObject_MALLOC(nbytes);
1468}
1469
Thomas Wouters334fb892000-07-25 12:56:38 +00001470void *
1471PyObject_Realloc(void *p, size_t nbytes)
Guido van Rossumb18618d2000-05-03 23:44:39 +00001472{
1473 return PyObject_REALLOC(p, nbytes);
1474}
1475
1476void
Thomas Wouters334fb892000-07-25 12:56:38 +00001477PyObject_Free(void *p)
Guido van Rossumb18618d2000-05-03 23:44:39 +00001478{
1479 PyObject_FREE(p);
Guido van Rossume09fb551997-08-05 02:04:34 +00001480}
Guido van Rossum86610361998-04-10 22:32:46 +00001481
1482
Fred Drake41deb1e2001-02-01 05:27:45 +00001483/* Hook to clear up weak references only once the _weakref module is
1484 imported. We use a dummy implementation to simplify the code at each
1485 call site instead of requiring a test for NULL.
1486*/
1487
Fred Drakeb60654b2001-02-26 18:56:37 +00001488static void
Fred Drake41deb1e2001-02-01 05:27:45 +00001489empty_clear_weak_refs(PyObject *o)
1490{
Fred Drakeb60654b2001-02-26 18:56:37 +00001491 return;
Fred Drake41deb1e2001-02-01 05:27:45 +00001492}
1493
Fred Drakeb60654b2001-02-26 18:56:37 +00001494void (*PyObject_ClearWeakRefs)(PyObject *) = empty_clear_weak_refs;
Fred Drake41deb1e2001-02-01 05:27:45 +00001495
1496
1497
Guido van Rossum86610361998-04-10 22:32:46 +00001498/* These methods are used to control infinite recursion in repr, str, print,
1499 etc. Container objects that may recursively contain themselves,
1500 e.g. builtin dictionaries and lists, should used Py_ReprEnter() and
1501 Py_ReprLeave() to avoid infinite recursion.
1502
1503 Py_ReprEnter() returns 0 the first time it is called for a particular
1504 object and 1 every time thereafter. It returns -1 if an exception
1505 occurred. Py_ReprLeave() has no return value.
1506
1507 See dictobject.c and listobject.c for examples of use.
1508*/
1509
1510#define KEY "Py_Repr"
1511
1512int
Fred Drake100814d2000-07-09 15:48:49 +00001513Py_ReprEnter(PyObject *obj)
Guido van Rossum86610361998-04-10 22:32:46 +00001514{
1515 PyObject *dict;
1516 PyObject *list;
1517 int i;
1518
1519 dict = PyThreadState_GetDict();
1520 if (dict == NULL)
1521 return -1;
1522 list = PyDict_GetItemString(dict, KEY);
1523 if (list == NULL) {
1524 list = PyList_New(0);
1525 if (list == NULL)
1526 return -1;
1527 if (PyDict_SetItemString(dict, KEY, list) < 0)
1528 return -1;
1529 Py_DECREF(list);
1530 }
1531 i = PyList_GET_SIZE(list);
1532 while (--i >= 0) {
1533 if (PyList_GET_ITEM(list, i) == obj)
1534 return 1;
1535 }
1536 PyList_Append(list, obj);
1537 return 0;
1538}
1539
1540void
Fred Drake100814d2000-07-09 15:48:49 +00001541Py_ReprLeave(PyObject *obj)
Guido van Rossum86610361998-04-10 22:32:46 +00001542{
1543 PyObject *dict;
1544 PyObject *list;
1545 int i;
1546
1547 dict = PyThreadState_GetDict();
Guido van Rossumeb909461998-04-11 15:17:34 +00001548 if (dict == NULL)
1549 return;
Guido van Rossum86610361998-04-10 22:32:46 +00001550 list = PyDict_GetItemString(dict, KEY);
Guido van Rossumeb909461998-04-11 15:17:34 +00001551 if (list == NULL || !PyList_Check(list))
1552 return;
Guido van Rossum86610361998-04-10 22:32:46 +00001553 i = PyList_GET_SIZE(list);
1554 /* Count backwards because we always expect obj to be list[-1] */
1555 while (--i >= 0) {
1556 if (PyList_GET_ITEM(list, i) == obj) {
1557 PyList_SetSlice(list, i, i + 1, NULL);
1558 break;
1559 }
1560 }
1561}
Guido van Rossumd724b232000-03-13 16:01:29 +00001562
1563/*
1564 trashcan
1565 CT 2k0130
1566 non-recursively destroy nested objects
1567
1568 CT 2k0223
1569 everything is now done in a macro.
1570
1571 CT 2k0305
1572 modified to use functions, after Tim Peter's suggestion.
1573
1574 CT 2k0309
1575 modified to restore a possible error.
Guido van Rossum13ff8eb2000-03-25 18:39:19 +00001576
1577 CT 2k0325
1578 added better safe than sorry check for threadstate
Guido van Rossume92e6102000-04-24 15:40:53 +00001579
1580 CT 2k0422
1581 complete rewrite. We now build a chain via ob_type
1582 and save the limited number of types in ob_refcnt.
1583 This is perfect since we don't need any memory.
1584 A patch for free-threading would need just a lock.
Guido van Rossumd724b232000-03-13 16:01:29 +00001585*/
1586
Guido van Rossume92e6102000-04-24 15:40:53 +00001587#define Py_TRASHCAN_TUPLE 1
1588#define Py_TRASHCAN_LIST 2
1589#define Py_TRASHCAN_DICT 3
1590#define Py_TRASHCAN_FRAME 4
1591#define Py_TRASHCAN_TRACEBACK 5
1592/* extend here if other objects want protection */
1593
Guido van Rossumd724b232000-03-13 16:01:29 +00001594int _PyTrash_delete_nesting = 0;
Guido van Rossume92e6102000-04-24 15:40:53 +00001595
Guido van Rossumd724b232000-03-13 16:01:29 +00001596PyObject * _PyTrash_delete_later = NULL;
1597
1598void
Fred Drake100814d2000-07-09 15:48:49 +00001599_PyTrash_deposit_object(PyObject *op)
Guido van Rossumd724b232000-03-13 16:01:29 +00001600{
Guido van Rossume92e6102000-04-24 15:40:53 +00001601 int typecode;
Guido van Rossum13ff8eb2000-03-25 18:39:19 +00001602
Guido van Rossume92e6102000-04-24 15:40:53 +00001603 if (PyTuple_Check(op))
1604 typecode = Py_TRASHCAN_TUPLE;
1605 else if (PyList_Check(op))
1606 typecode = Py_TRASHCAN_LIST;
1607 else if (PyDict_Check(op))
1608 typecode = Py_TRASHCAN_DICT;
1609 else if (PyFrame_Check(op))
1610 typecode = Py_TRASHCAN_FRAME;
1611 else if (PyTraceBack_Check(op))
1612 typecode = Py_TRASHCAN_TRACEBACK;
Moshe Zadkacf703f02000-08-04 15:36:13 +00001613 else /* We have a bug here -- those are the only types in GC */ {
1614 Py_FatalError("Type not supported in GC -- internal bug");
1615 return; /* pacify compiler -- execution never here */
1616 }
Guido van Rossume92e6102000-04-24 15:40:53 +00001617 op->ob_refcnt = typecode;
Guido van Rossumd724b232000-03-13 16:01:29 +00001618
Guido van Rossume92e6102000-04-24 15:40:53 +00001619 op->ob_type = (PyTypeObject*)_PyTrash_delete_later;
1620 _PyTrash_delete_later = op;
Guido van Rossumd724b232000-03-13 16:01:29 +00001621}
1622
1623void
Fred Drake100814d2000-07-09 15:48:49 +00001624_PyTrash_destroy_chain(void)
Guido van Rossumd724b232000-03-13 16:01:29 +00001625{
1626 while (_PyTrash_delete_later) {
1627 PyObject *shredder = _PyTrash_delete_later;
Guido van Rossume92e6102000-04-24 15:40:53 +00001628 _PyTrash_delete_later = (PyObject*) shredder->ob_type;
1629
1630 switch (shredder->ob_refcnt) {
1631 case Py_TRASHCAN_TUPLE:
1632 shredder->ob_type = &PyTuple_Type;
1633 break;
1634 case Py_TRASHCAN_LIST:
1635 shredder->ob_type = &PyList_Type;
1636 break;
1637 case Py_TRASHCAN_DICT:
1638 shredder->ob_type = &PyDict_Type;
1639 break;
1640 case Py_TRASHCAN_FRAME:
1641 shredder->ob_type = &PyFrame_Type;
1642 break;
1643 case Py_TRASHCAN_TRACEBACK:
1644 shredder->ob_type = &PyTraceBack_Type;
1645 break;
1646 }
1647 _Py_NewReference(shredder);
1648
Guido van Rossumd724b232000-03-13 16:01:29 +00001649 ++_PyTrash_delete_nesting;
1650 Py_DECREF(shredder);
1651 --_PyTrash_delete_nesting;
1652 }
1653}
Neil Schemenauera35c6882001-02-27 04:45:05 +00001654
1655#ifdef WITH_PYMALLOC
1656#include "obmalloc.c"
1657#endif