blob: 680a7d49012ab9dbf06d99cf226564f71def6a8d [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
Marc-André Lemburge44e5072000-09-18 16:20:57 +00001059/* Internal API needed by PyObject_GetAttr(): */
1060extern
1061PyObject *_PyUnicode_AsDefaultEncodedString(PyObject *unicode,
1062 const char *errors);
1063
Guido van Rossum98ff96a1997-05-20 18:34:44 +00001064PyObject *
Fred Drake100814d2000-07-09 15:48:49 +00001065PyObject_GetAttr(PyObject *v, PyObject *name)
Guido van Rossum98ff96a1997-05-20 18:34:44 +00001066{
Marc-André Lemburge44e5072000-09-18 16:20:57 +00001067 /* The Unicode to string conversion is done here because the
1068 existing tp_getattro slots expect a string object as name
1069 and we wouldn't want to break those. */
1070 if (PyUnicode_Check(name)) {
1071 name = _PyUnicode_AsDefaultEncodedString(name, NULL);
1072 if (name == NULL)
1073 return NULL;
1074 }
Jeremy Hylton99a8f902000-06-23 14:36:32 +00001075
1076 if (!PyString_Check(name)) {
1077 PyErr_SetString(PyExc_TypeError,
1078 "attribute name must be string");
1079 return NULL;
1080 }
Marc-André Lemburge44e5072000-09-18 16:20:57 +00001081 if (v->ob_type->tp_getattro != NULL)
1082 return (*v->ob_type->tp_getattro)(v, name);
1083 else
Tim Peters5acbfcc2001-05-11 03:36:45 +00001084 return PyObject_GetAttrString(v, PyString_AS_STRING(name));
Guido van Rossum98ff96a1997-05-20 18:34:44 +00001085}
1086
1087int
Fred Drake100814d2000-07-09 15:48:49 +00001088PyObject_HasAttr(PyObject *v, PyObject *name)
Guido van Rossum98ff96a1997-05-20 18:34:44 +00001089{
1090 PyObject *res = PyObject_GetAttr(v, name);
1091 if (res != NULL) {
1092 Py_DECREF(res);
1093 return 1;
1094 }
1095 PyErr_Clear();
1096 return 0;
1097}
1098
1099int
Fred Drake100814d2000-07-09 15:48:49 +00001100PyObject_SetAttr(PyObject *v, PyObject *name, PyObject *value)
Guido van Rossum98ff96a1997-05-20 18:34:44 +00001101{
1102 int err;
Marc-André Lemburge44e5072000-09-18 16:20:57 +00001103
1104 /* The Unicode to string conversion is done here because the
1105 existing tp_setattro slots expect a string object as name
1106 and we wouldn't want to break those. */
1107 if (PyUnicode_Check(name)) {
1108 name = PyUnicode_AsEncodedString(name, NULL, NULL);
1109 if (name == NULL)
1110 return -1;
Jeremy Hylton99a8f902000-06-23 14:36:32 +00001111 }
Marc-André Lemburge44e5072000-09-18 16:20:57 +00001112 else
1113 Py_INCREF(name);
1114
1115 if (!PyString_Check(name)){
Jeremy Hylton99a8f902000-06-23 14:36:32 +00001116 PyErr_SetString(PyExc_TypeError,
1117 "attribute name must be string");
1118 err = -1;
1119 }
Marc-André Lemburge44e5072000-09-18 16:20:57 +00001120 else {
1121 PyString_InternInPlace(&name);
1122 if (v->ob_type->tp_setattro != NULL)
1123 err = (*v->ob_type->tp_setattro)(v, name, value);
1124 else
1125 err = PyObject_SetAttrString(v,
1126 PyString_AS_STRING(name), value);
1127 }
1128
Guido van Rossum98ff96a1997-05-20 18:34:44 +00001129 Py_DECREF(name);
1130 return err;
1131}
1132
Guido van Rossum6ac258d1993-05-12 08:24:20 +00001133/* Test a value used as condition, e.g., in a for or if statement.
1134 Return -1 if an error occurred */
1135
1136int
Fred Drake100814d2000-07-09 15:48:49 +00001137PyObject_IsTrue(PyObject *v)
Guido van Rossum6ac258d1993-05-12 08:24:20 +00001138{
1139 int res;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001140 if (v == Py_None)
Guido van Rossum6ac258d1993-05-12 08:24:20 +00001141 res = 0;
Guido van Rossum1c4f4581998-05-22 00:53:24 +00001142 else if (v->ob_type->tp_as_number != NULL &&
1143 v->ob_type->tp_as_number->nb_nonzero != NULL)
Guido van Rossum6ac258d1993-05-12 08:24:20 +00001144 res = (*v->ob_type->tp_as_number->nb_nonzero)(v);
Guido van Rossum1c4f4581998-05-22 00:53:24 +00001145 else if (v->ob_type->tp_as_mapping != NULL &&
1146 v->ob_type->tp_as_mapping->mp_length != NULL)
Guido van Rossum6ac258d1993-05-12 08:24:20 +00001147 res = (*v->ob_type->tp_as_mapping->mp_length)(v);
Guido van Rossum1c4f4581998-05-22 00:53:24 +00001148 else if (v->ob_type->tp_as_sequence != NULL &&
1149 v->ob_type->tp_as_sequence->sq_length != NULL)
Guido van Rossum6ac258d1993-05-12 08:24:20 +00001150 res = (*v->ob_type->tp_as_sequence->sq_length)(v);
1151 else
1152 res = 1;
1153 if (res > 0)
1154 res = 1;
1155 return res;
1156}
1157
Guido van Rossumc3d3f961998-04-09 17:53:59 +00001158/* equivalent of 'not v'
1159 Return -1 if an error occurred */
1160
1161int
Fred Drake100814d2000-07-09 15:48:49 +00001162PyObject_Not(PyObject *v)
Guido van Rossumc3d3f961998-04-09 17:53:59 +00001163{
1164 int res;
1165 res = PyObject_IsTrue(v);
1166 if (res < 0)
1167 return res;
1168 return res == 0;
1169}
1170
Guido van Rossum5524a591995-01-10 15:26:20 +00001171/* Coerce two numeric types to the "larger" one.
1172 Increment the reference count on each argument.
Guido van Rossume797ec12001-01-17 15:24:28 +00001173 Return value:
1174 -1 if an error occurred;
1175 0 if the coercion succeeded (and then the reference counts are increased);
1176 1 if no coercion is possible (and no error is raised).
Guido van Rossum5524a591995-01-10 15:26:20 +00001177*/
Guido van Rossum5524a591995-01-10 15:26:20 +00001178int
Fred Drake100814d2000-07-09 15:48:49 +00001179PyNumber_CoerceEx(PyObject **pv, PyObject **pw)
Guido van Rossum5524a591995-01-10 15:26:20 +00001180{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001181 register PyObject *v = *pv;
1182 register PyObject *w = *pw;
Guido van Rossum5524a591995-01-10 15:26:20 +00001183 int res;
1184
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001185 if (v->ob_type == w->ob_type && !PyInstance_Check(v)) {
1186 Py_INCREF(v);
1187 Py_INCREF(w);
Guido van Rossum5524a591995-01-10 15:26:20 +00001188 return 0;
1189 }
1190 if (v->ob_type->tp_as_number && v->ob_type->tp_as_number->nb_coerce) {
1191 res = (*v->ob_type->tp_as_number->nb_coerce)(pv, pw);
1192 if (res <= 0)
1193 return res;
1194 }
1195 if (w->ob_type->tp_as_number && w->ob_type->tp_as_number->nb_coerce) {
1196 res = (*w->ob_type->tp_as_number->nb_coerce)(pw, pv);
1197 if (res <= 0)
1198 return res;
1199 }
Guido van Rossum242c6421997-11-19 16:03:17 +00001200 return 1;
1201}
1202
Guido van Rossume797ec12001-01-17 15:24:28 +00001203/* Coerce two numeric types to the "larger" one.
1204 Increment the reference count on each argument.
1205 Return -1 and raise an exception if no coercion is possible
1206 (and then no reference count is incremented).
1207*/
Guido van Rossum242c6421997-11-19 16:03:17 +00001208int
Fred Drake100814d2000-07-09 15:48:49 +00001209PyNumber_Coerce(PyObject **pv, PyObject **pw)
Guido van Rossum242c6421997-11-19 16:03:17 +00001210{
1211 int err = PyNumber_CoerceEx(pv, pw);
1212 if (err <= 0)
1213 return err;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001214 PyErr_SetString(PyExc_TypeError, "number coercion failed");
Guido van Rossum5524a591995-01-10 15:26:20 +00001215 return -1;
1216}
1217
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001218
Guido van Rossum49b11fe1995-01-26 00:38:22 +00001219/* Test whether an object can be called */
1220
1221int
Fred Drake100814d2000-07-09 15:48:49 +00001222PyCallable_Check(PyObject *x)
Guido van Rossum49b11fe1995-01-26 00:38:22 +00001223{
1224 if (x == NULL)
1225 return 0;
1226 if (x->ob_type->tp_call != NULL ||
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001227 PyFunction_Check(x) ||
1228 PyMethod_Check(x) ||
1229 PyCFunction_Check(x) ||
1230 PyClass_Check(x))
Guido van Rossum49b11fe1995-01-26 00:38:22 +00001231 return 1;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001232 if (PyInstance_Check(x)) {
1233 PyObject *call = PyObject_GetAttrString(x, "__call__");
Guido van Rossum49b11fe1995-01-26 00:38:22 +00001234 if (call == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001235 PyErr_Clear();
Guido van Rossum49b11fe1995-01-26 00:38:22 +00001236 return 0;
1237 }
1238 /* Could test recursively but don't, for fear of endless
1239 recursion if some joker sets self.__call__ = self */
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001240 Py_DECREF(call);
Guido van Rossum49b11fe1995-01-26 00:38:22 +00001241 return 1;
1242 }
1243 return 0;
1244}
1245
1246
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001247/*
1248NoObject is usable as a non-NULL undefined value, used by the macro None.
1249There is (and should be!) no way to create other objects of this type,
Guido van Rossum3f5da241990-12-20 15:06:42 +00001250so there is exactly one (which is indestructible, by the way).
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001251*/
1252
Guido van Rossum0c182a11992-03-27 17:26:13 +00001253/* ARGSUSED */
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001254static PyObject *
Fred Drake100814d2000-07-09 15:48:49 +00001255none_repr(PyObject *op)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001256{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001257 return PyString_FromString("None");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001258}
1259
Barry Warsaw9bf16442001-01-23 16:24:35 +00001260/* ARGUSED */
1261static void
1262none_dealloc(PyObject* ignore)
1263{
1264 /* This should never get called, but we also don't want to SEGV if
1265 * we accidently decref None out of existance.
1266 */
1267 abort();
1268}
1269
1270
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001271static PyTypeObject PyNothing_Type = {
1272 PyObject_HEAD_INIT(&PyType_Type)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001273 0,
Guido van Rossum3f5da241990-12-20 15:06:42 +00001274 "None",
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001275 0,
1276 0,
Barry Warsaw9bf16442001-01-23 16:24:35 +00001277 (destructor)none_dealloc, /*tp_dealloc*/ /*never called*/
Guido van Rossum7066dd71992-09-17 17:54:56 +00001278 0, /*tp_print*/
Guido van Rossum3f5da241990-12-20 15:06:42 +00001279 0, /*tp_getattr*/
1280 0, /*tp_setattr*/
1281 0, /*tp_compare*/
Guido van Rossum1d5735e1994-08-30 08:27:36 +00001282 (reprfunc)none_repr, /*tp_repr*/
Guido van Rossum3f5da241990-12-20 15:06:42 +00001283 0, /*tp_as_number*/
1284 0, /*tp_as_sequence*/
1285 0, /*tp_as_mapping*/
Guido van Rossum9bfef441993-03-29 10:43:31 +00001286 0, /*tp_hash */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001287};
1288
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001289PyObject _Py_NoneStruct = {
1290 PyObject_HEAD_INIT(&PyNothing_Type)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001291};
1292
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +00001293/* NotImplemented is an object that can be used to signal that an
1294 operation is not implemented for the given type combination. */
1295
1296static PyObject *
1297NotImplemented_repr(PyObject *op)
1298{
1299 return PyString_FromString("NotImplemented");
1300}
1301
1302static PyTypeObject PyNotImplemented_Type = {
1303 PyObject_HEAD_INIT(&PyType_Type)
1304 0,
1305 "NotImplemented",
1306 0,
1307 0,
Barry Warsaw9bf16442001-01-23 16:24:35 +00001308 (destructor)none_dealloc, /*tp_dealloc*/ /*never called*/
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +00001309 0, /*tp_print*/
1310 0, /*tp_getattr*/
1311 0, /*tp_setattr*/
1312 0, /*tp_compare*/
1313 (reprfunc)NotImplemented_repr, /*tp_repr*/
1314 0, /*tp_as_number*/
1315 0, /*tp_as_sequence*/
1316 0, /*tp_as_mapping*/
1317 0, /*tp_hash */
1318};
1319
1320PyObject _Py_NotImplementedStruct = {
1321 PyObject_HEAD_INIT(&PyNotImplemented_Type)
1322};
1323
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001324
Guido van Rossum84a90321996-05-22 16:34:47 +00001325#ifdef Py_TRACE_REFS
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001326
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001327static PyObject refchain = {&refchain, &refchain};
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001328
Guido van Rossumaacdc9d1996-08-12 21:32:12 +00001329void
Fred Drake100814d2000-07-09 15:48:49 +00001330_Py_ResetReferences(void)
Guido van Rossume09fb551997-08-05 02:04:34 +00001331{
1332 refchain._ob_prev = refchain._ob_next = &refchain;
1333 _Py_RefTotal = 0;
1334}
1335
1336void
Fred Drake100814d2000-07-09 15:48:49 +00001337_Py_NewReference(PyObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001338{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001339 _Py_RefTotal++;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001340 op->ob_refcnt = 1;
1341 op->_ob_next = refchain._ob_next;
1342 op->_ob_prev = &refchain;
1343 refchain._ob_next->_ob_prev = op;
1344 refchain._ob_next = op;
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +00001345#ifdef COUNT_ALLOCS
1346 inc_count(op->ob_type);
1347#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001348}
1349
Guido van Rossumaacdc9d1996-08-12 21:32:12 +00001350void
Fred Drake100814d2000-07-09 15:48:49 +00001351_Py_ForgetReference(register PyObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001352{
Guido van Rossumbffd6832000-01-20 22:32:56 +00001353#ifdef SLOW_UNREF_CHECK
Guido van Rossum4c08d552000-03-10 22:55:18 +00001354 register PyObject *p;
Guido van Rossumbffd6832000-01-20 22:32:56 +00001355#endif
Guido van Rossumd7047b31995-01-02 19:07:15 +00001356 if (op->ob_refcnt < 0)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001357 Py_FatalError("UNREF negative refcnt");
Guido van Rossum2e8f6141992-09-03 20:32:55 +00001358 if (op == &refchain ||
Guido van Rossumd7047b31995-01-02 19:07:15 +00001359 op->_ob_prev->_ob_next != op || op->_ob_next->_ob_prev != op)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001360 Py_FatalError("UNREF invalid object");
Guido van Rossum2e8f6141992-09-03 20:32:55 +00001361#ifdef SLOW_UNREF_CHECK
Guido van Rossum3f5da241990-12-20 15:06:42 +00001362 for (p = refchain._ob_next; p != &refchain; p = p->_ob_next) {
1363 if (p == op)
1364 break;
1365 }
Guido van Rossumd7047b31995-01-02 19:07:15 +00001366 if (p == &refchain) /* Not found */
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001367 Py_FatalError("UNREF unknown object");
Guido van Rossum2e8f6141992-09-03 20:32:55 +00001368#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001369 op->_ob_next->_ob_prev = op->_ob_prev;
1370 op->_ob_prev->_ob_next = op->_ob_next;
Guido van Rossum2e8f6141992-09-03 20:32:55 +00001371 op->_ob_next = op->_ob_prev = NULL;
Guido van Rossumd8953cb1995-04-06 14:46:26 +00001372#ifdef COUNT_ALLOCS
1373 op->ob_type->tp_free++;
1374#endif
Guido van Rossum3f5da241990-12-20 15:06:42 +00001375}
1376
Guido van Rossumaacdc9d1996-08-12 21:32:12 +00001377void
Fred Drake100814d2000-07-09 15:48:49 +00001378_Py_Dealloc(PyObject *op)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001379{
Guido van Rossum9776adf1994-09-07 14:36:45 +00001380 destructor dealloc = op->ob_type->tp_dealloc;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001381 _Py_ForgetReference(op);
Guido van Rossum9776adf1994-09-07 14:36:45 +00001382 (*dealloc)(op);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001383}
1384
Guido van Rossumaacdc9d1996-08-12 21:32:12 +00001385void
Fred Drake100814d2000-07-09 15:48:49 +00001386_Py_PrintReferences(FILE *fp)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001387{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001388 PyObject *op;
Guido van Rossume09fb551997-08-05 02:04:34 +00001389 fprintf(fp, "Remaining objects:\n");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001390 for (op = refchain._ob_next; op != &refchain; op = op->_ob_next) {
1391 fprintf(fp, "[%d] ", op->ob_refcnt);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001392 if (PyObject_Print(op, fp, 0) != 0)
1393 PyErr_Clear();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001394 putc('\n', fp);
1395 }
1396}
1397
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +00001398PyObject *
Fred Drake100814d2000-07-09 15:48:49 +00001399_Py_GetObjects(PyObject *self, PyObject *args)
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +00001400{
1401 int i, n;
1402 PyObject *t = NULL;
1403 PyObject *res, *op;
1404
1405 if (!PyArg_ParseTuple(args, "i|O", &n, &t))
1406 return NULL;
1407 op = refchain._ob_next;
1408 res = PyList_New(0);
1409 if (res == NULL)
1410 return NULL;
1411 for (i = 0; (n == 0 || i < n) && op != &refchain; i++) {
1412 while (op == self || op == args || op == res || op == t ||
1413 t != NULL && op->ob_type != (PyTypeObject *) t) {
1414 op = op->_ob_next;
1415 if (op == &refchain)
1416 return res;
1417 }
1418 if (PyList_Append(res, op) < 0) {
1419 Py_DECREF(res);
1420 return NULL;
1421 }
1422 op = op->_ob_next;
1423 }
1424 return res;
1425}
1426
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001427#endif
Guido van Rossum97ead3f1996-01-12 01:24:09 +00001428
1429
1430/* Hack to force loading of cobject.o */
Guido van Rossumda9c2711996-12-05 21:58:58 +00001431PyTypeObject *_Py_cobject_hack = &PyCObject_Type;
Guido van Rossum84a90321996-05-22 16:34:47 +00001432
1433
1434/* Hack to force loading of abstract.o */
Jeremy Hylton6253f832000-07-12 12:56:19 +00001435int (*_Py_abstract_hack)(PyObject *) = &PyObject_Size;
Guido van Rossume09fb551997-08-05 02:04:34 +00001436
1437
Andrew M. Kuchling1582a3a2000-08-16 12:27:23 +00001438/* Python's malloc wrappers (see pymem.h) */
Guido van Rossume09fb551997-08-05 02:04:34 +00001439
Thomas Wouters334fb892000-07-25 12:56:38 +00001440void *
Fred Drake100814d2000-07-09 15:48:49 +00001441PyMem_Malloc(size_t nbytes)
Guido van Rossume09fb551997-08-05 02:04:34 +00001442{
1443#if _PyMem_EXTRA > 0
1444 if (nbytes == 0)
1445 nbytes = _PyMem_EXTRA;
1446#endif
Guido van Rossumb18618d2000-05-03 23:44:39 +00001447 return PyMem_MALLOC(nbytes);
Guido van Rossume09fb551997-08-05 02:04:34 +00001448}
1449
Thomas Wouters334fb892000-07-25 12:56:38 +00001450void *
1451PyMem_Realloc(void *p, size_t nbytes)
Guido van Rossume09fb551997-08-05 02:04:34 +00001452{
1453#if _PyMem_EXTRA > 0
1454 if (nbytes == 0)
1455 nbytes = _PyMem_EXTRA;
1456#endif
Guido van Rossumb18618d2000-05-03 23:44:39 +00001457 return PyMem_REALLOC(p, nbytes);
Guido van Rossume09fb551997-08-05 02:04:34 +00001458}
1459
1460void
Thomas Wouters334fb892000-07-25 12:56:38 +00001461PyMem_Free(void *p)
Guido van Rossume09fb551997-08-05 02:04:34 +00001462{
Guido van Rossumb18618d2000-05-03 23:44:39 +00001463 PyMem_FREE(p);
1464}
1465
1466
1467/* Python's object malloc wrappers (see objimpl.h) */
1468
Thomas Wouters334fb892000-07-25 12:56:38 +00001469void *
Fred Drake100814d2000-07-09 15:48:49 +00001470PyObject_Malloc(size_t nbytes)
Guido van Rossumb18618d2000-05-03 23:44:39 +00001471{
1472 return PyObject_MALLOC(nbytes);
1473}
1474
Thomas Wouters334fb892000-07-25 12:56:38 +00001475void *
1476PyObject_Realloc(void *p, size_t nbytes)
Guido van Rossumb18618d2000-05-03 23:44:39 +00001477{
1478 return PyObject_REALLOC(p, nbytes);
1479}
1480
1481void
Thomas Wouters334fb892000-07-25 12:56:38 +00001482PyObject_Free(void *p)
Guido van Rossumb18618d2000-05-03 23:44:39 +00001483{
1484 PyObject_FREE(p);
Guido van Rossume09fb551997-08-05 02:04:34 +00001485}
Guido van Rossum86610361998-04-10 22:32:46 +00001486
1487
Fred Drake41deb1e2001-02-01 05:27:45 +00001488/* Hook to clear up weak references only once the _weakref module is
1489 imported. We use a dummy implementation to simplify the code at each
1490 call site instead of requiring a test for NULL.
1491*/
1492
Fred Drakeb60654b2001-02-26 18:56:37 +00001493static void
Fred Drake41deb1e2001-02-01 05:27:45 +00001494empty_clear_weak_refs(PyObject *o)
1495{
Fred Drakeb60654b2001-02-26 18:56:37 +00001496 return;
Fred Drake41deb1e2001-02-01 05:27:45 +00001497}
1498
Fred Drakeb60654b2001-02-26 18:56:37 +00001499void (*PyObject_ClearWeakRefs)(PyObject *) = empty_clear_weak_refs;
Fred Drake41deb1e2001-02-01 05:27:45 +00001500
1501
1502
Guido van Rossum86610361998-04-10 22:32:46 +00001503/* These methods are used to control infinite recursion in repr, str, print,
1504 etc. Container objects that may recursively contain themselves,
1505 e.g. builtin dictionaries and lists, should used Py_ReprEnter() and
1506 Py_ReprLeave() to avoid infinite recursion.
1507
1508 Py_ReprEnter() returns 0 the first time it is called for a particular
1509 object and 1 every time thereafter. It returns -1 if an exception
1510 occurred. Py_ReprLeave() has no return value.
1511
1512 See dictobject.c and listobject.c for examples of use.
1513*/
1514
1515#define KEY "Py_Repr"
1516
1517int
Fred Drake100814d2000-07-09 15:48:49 +00001518Py_ReprEnter(PyObject *obj)
Guido van Rossum86610361998-04-10 22:32:46 +00001519{
1520 PyObject *dict;
1521 PyObject *list;
1522 int i;
1523
1524 dict = PyThreadState_GetDict();
1525 if (dict == NULL)
1526 return -1;
1527 list = PyDict_GetItemString(dict, KEY);
1528 if (list == NULL) {
1529 list = PyList_New(0);
1530 if (list == NULL)
1531 return -1;
1532 if (PyDict_SetItemString(dict, KEY, list) < 0)
1533 return -1;
1534 Py_DECREF(list);
1535 }
1536 i = PyList_GET_SIZE(list);
1537 while (--i >= 0) {
1538 if (PyList_GET_ITEM(list, i) == obj)
1539 return 1;
1540 }
1541 PyList_Append(list, obj);
1542 return 0;
1543}
1544
1545void
Fred Drake100814d2000-07-09 15:48:49 +00001546Py_ReprLeave(PyObject *obj)
Guido van Rossum86610361998-04-10 22:32:46 +00001547{
1548 PyObject *dict;
1549 PyObject *list;
1550 int i;
1551
1552 dict = PyThreadState_GetDict();
Guido van Rossumeb909461998-04-11 15:17:34 +00001553 if (dict == NULL)
1554 return;
Guido van Rossum86610361998-04-10 22:32:46 +00001555 list = PyDict_GetItemString(dict, KEY);
Guido van Rossumeb909461998-04-11 15:17:34 +00001556 if (list == NULL || !PyList_Check(list))
1557 return;
Guido van Rossum86610361998-04-10 22:32:46 +00001558 i = PyList_GET_SIZE(list);
1559 /* Count backwards because we always expect obj to be list[-1] */
1560 while (--i >= 0) {
1561 if (PyList_GET_ITEM(list, i) == obj) {
1562 PyList_SetSlice(list, i, i + 1, NULL);
1563 break;
1564 }
1565 }
1566}
Guido van Rossumd724b232000-03-13 16:01:29 +00001567
1568/*
1569 trashcan
1570 CT 2k0130
1571 non-recursively destroy nested objects
1572
1573 CT 2k0223
1574 everything is now done in a macro.
1575
1576 CT 2k0305
1577 modified to use functions, after Tim Peter's suggestion.
1578
1579 CT 2k0309
1580 modified to restore a possible error.
Guido van Rossum13ff8eb2000-03-25 18:39:19 +00001581
1582 CT 2k0325
1583 added better safe than sorry check for threadstate
Guido van Rossume92e6102000-04-24 15:40:53 +00001584
1585 CT 2k0422
1586 complete rewrite. We now build a chain via ob_type
1587 and save the limited number of types in ob_refcnt.
1588 This is perfect since we don't need any memory.
1589 A patch for free-threading would need just a lock.
Guido van Rossumd724b232000-03-13 16:01:29 +00001590*/
1591
Guido van Rossume92e6102000-04-24 15:40:53 +00001592#define Py_TRASHCAN_TUPLE 1
1593#define Py_TRASHCAN_LIST 2
1594#define Py_TRASHCAN_DICT 3
1595#define Py_TRASHCAN_FRAME 4
1596#define Py_TRASHCAN_TRACEBACK 5
1597/* extend here if other objects want protection */
1598
Guido van Rossumd724b232000-03-13 16:01:29 +00001599int _PyTrash_delete_nesting = 0;
Guido van Rossume92e6102000-04-24 15:40:53 +00001600
Guido van Rossumd724b232000-03-13 16:01:29 +00001601PyObject * _PyTrash_delete_later = NULL;
1602
1603void
Fred Drake100814d2000-07-09 15:48:49 +00001604_PyTrash_deposit_object(PyObject *op)
Guido van Rossumd724b232000-03-13 16:01:29 +00001605{
Guido van Rossume92e6102000-04-24 15:40:53 +00001606 int typecode;
Guido van Rossum13ff8eb2000-03-25 18:39:19 +00001607
Guido van Rossume92e6102000-04-24 15:40:53 +00001608 if (PyTuple_Check(op))
1609 typecode = Py_TRASHCAN_TUPLE;
1610 else if (PyList_Check(op))
1611 typecode = Py_TRASHCAN_LIST;
1612 else if (PyDict_Check(op))
1613 typecode = Py_TRASHCAN_DICT;
1614 else if (PyFrame_Check(op))
1615 typecode = Py_TRASHCAN_FRAME;
1616 else if (PyTraceBack_Check(op))
1617 typecode = Py_TRASHCAN_TRACEBACK;
Moshe Zadkacf703f02000-08-04 15:36:13 +00001618 else /* We have a bug here -- those are the only types in GC */ {
1619 Py_FatalError("Type not supported in GC -- internal bug");
1620 return; /* pacify compiler -- execution never here */
1621 }
Guido van Rossume92e6102000-04-24 15:40:53 +00001622 op->ob_refcnt = typecode;
Guido van Rossumd724b232000-03-13 16:01:29 +00001623
Guido van Rossume92e6102000-04-24 15:40:53 +00001624 op->ob_type = (PyTypeObject*)_PyTrash_delete_later;
1625 _PyTrash_delete_later = op;
Guido van Rossumd724b232000-03-13 16:01:29 +00001626}
1627
1628void
Fred Drake100814d2000-07-09 15:48:49 +00001629_PyTrash_destroy_chain(void)
Guido van Rossumd724b232000-03-13 16:01:29 +00001630{
1631 while (_PyTrash_delete_later) {
1632 PyObject *shredder = _PyTrash_delete_later;
Guido van Rossume92e6102000-04-24 15:40:53 +00001633 _PyTrash_delete_later = (PyObject*) shredder->ob_type;
1634
1635 switch (shredder->ob_refcnt) {
1636 case Py_TRASHCAN_TUPLE:
1637 shredder->ob_type = &PyTuple_Type;
1638 break;
1639 case Py_TRASHCAN_LIST:
1640 shredder->ob_type = &PyList_Type;
1641 break;
1642 case Py_TRASHCAN_DICT:
1643 shredder->ob_type = &PyDict_Type;
1644 break;
1645 case Py_TRASHCAN_FRAME:
1646 shredder->ob_type = &PyFrame_Type;
1647 break;
1648 case Py_TRASHCAN_TRACEBACK:
1649 shredder->ob_type = &PyTraceBack_Type;
1650 break;
1651 }
1652 _Py_NewReference(shredder);
1653
Guido van Rossumd724b232000-03-13 16:01:29 +00001654 ++_PyTrash_delete_nesting;
1655 Py_DECREF(shredder);
1656 --_PyTrash_delete_nesting;
1657 }
1658}
Neil Schemenauera35c6882001-02-27 04:45:05 +00001659
1660#ifdef WITH_PYMALLOC
1661#include "obmalloc.c"
1662#endif