blob: 846297170c036ba5688c3fceae122a1e96721e17 [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
480 /* If the types are equal, don't bother with coercions etc. */
481 if (v->ob_type == w->ob_type) {
482 if ((f = v->ob_type->tp_compare) == NULL)
483 return 2;
484 c = (*f)(v, w);
485 if (PyErr_Occurred())
486 return -2;
487 return c < 0 ? -1 : c > 0 ? 1 : 0;
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +0000488 }
Guido van Rossume797ec12001-01-17 15:24:28 +0000489
490 /* Try coercion; if it fails, give up */
491 c = PyNumber_CoerceEx(&v, &w);
492 if (c < 0)
493 return -2;
494 if (c > 0)
495 return 2;
496
497 /* Try v's comparison, if defined */
498 if ((f = v->ob_type->tp_compare) != NULL) {
499 c = (*f)(v, w);
500 Py_DECREF(v);
501 Py_DECREF(w);
502 if (PyErr_Occurred())
503 return -2;
504 return c < 0 ? -1 : c > 0 ? 1 : 0;
505 }
506
507 /* Try w's comparison, if defined */
508 if ((f = w->ob_type->tp_compare) != NULL) {
509 c = (*f)(w, v); /* swapped! */
510 Py_DECREF(v);
511 Py_DECREF(w);
512 if (PyErr_Occurred())
513 return -2;
514 return c < 0 ? 1 : c > 0 ? -1 : 0; /* negated! */
515 }
516
517 /* No comparison defined */
518 Py_DECREF(v);
519 Py_DECREF(w);
520 return 2;
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +0000521}
522
Guido van Rossume797ec12001-01-17 15:24:28 +0000523/* Final fallback 3-way comparison, returning an int. Return:
524 -2 if an error occurred;
525 -1 if v < w;
526 0 if v == w;
527 1 if v > w.
528*/
529static int
530default_3way_compare(PyObject *v, PyObject *w)
531{
532 int c;
Guido van Rossum8f9143d2001-01-22 15:59:32 +0000533 char *vname, *wname;
Guido van Rossume797ec12001-01-17 15:24:28 +0000534
535 if (v->ob_type == w->ob_type) {
Barry Warsawb0e754d2001-01-20 06:24:55 +0000536 /* When comparing these pointers, they must be cast to
537 * integer types (i.e. Py_uintptr_t, our spelling of C9X's
538 * uintptr_t). ANSI specifies that pointer compares other
539 * than == and != to non-related structures are undefined.
540 */
Barry Warsaw71ff8d52001-01-20 06:08:10 +0000541 Py_uintptr_t vv = (Py_uintptr_t)v;
542 Py_uintptr_t ww = (Py_uintptr_t)w;
Guido van Rossume797ec12001-01-17 15:24:28 +0000543 return (vv < ww) ? -1 : (vv > ww) ? 1 : 0;
544 }
545
546 /* Special case for Unicode */
547 if (PyUnicode_Check(v) || PyUnicode_Check(w)) {
548 c = PyUnicode_Compare(v, w);
549 if (!PyErr_Occurred())
550 return c;
551 /* TypeErrors are ignored: if Unicode coercion fails due
552 to one of the arguments not having the right type, we
553 continue as defined by the coercion protocol (see
554 above). Luckily, decoding errors are reported as
555 ValueErrors and are not masked by this technique. */
556 if (!PyErr_ExceptionMatches(PyExc_TypeError))
557 return -2;
558 PyErr_Clear();
559 }
560
Guido van Rossum0871e932001-01-22 19:28:09 +0000561 /* None is smaller than anything */
562 if (v == Py_None)
563 return -1;
564 if (w == Py_None)
565 return 1;
566
Guido van Rossume797ec12001-01-17 15:24:28 +0000567 /* different type: compare type names */
Guido van Rossum8f9143d2001-01-22 15:59:32 +0000568 if (v->ob_type->tp_as_number)
569 vname = "";
570 else
571 vname = v->ob_type->tp_name;
572 if (w->ob_type->tp_as_number)
573 wname = "";
574 else
575 wname = w->ob_type->tp_name;
576 c = strcmp(vname, wname);
577 if (c < 0)
578 return -1;
579 if (c > 0)
580 return 1;
581 /* Same type name, or (more likely) incomparable numeric types */
582 return ((Py_uintptr_t)(v->ob_type) < (
583 Py_uintptr_t)(w->ob_type)) ? -1 : 1;
Guido van Rossume797ec12001-01-17 15:24:28 +0000584}
585
586#define CHECK_TYPES(o) PyType_HasFeature((o)->ob_type, Py_TPFLAGS_CHECKTYPES)
587
Tim Peters6d60b2e2001-05-07 20:53:51 +0000588/* Do a 3-way comparison, by hook or by crook. Return:
589 -2 for an exception;
590 -1 if v < w;
591 0 if v == w;
592 1 if v > w;
593*/
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +0000594static int
Fred Drake100814d2000-07-09 15:48:49 +0000595do_cmp(PyObject *v, PyObject *w)
Guido van Rossum20566841995-01-12 11:26:10 +0000596{
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +0000597 int c;
598
Guido van Rossume797ec12001-01-17 15:24:28 +0000599 c = try_rich_to_3way_compare(v, w);
600 if (c < 2)
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +0000601 return c;
Guido van Rossume797ec12001-01-17 15:24:28 +0000602 c = try_3way_compare(v, w);
603 if (c < 2)
604 return c;
605 return default_3way_compare(v, w);
Guido van Rossum20566841995-01-12 11:26:10 +0000606}
607
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000608/* compare_nesting is incremented before calling compare (for
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000609 some types) and decremented on exit. If the count exceeds the
610 nesting limit, enable code to detect circular data structures.
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000611
612 This is a tunable parameter that should only affect the performance
613 of comparisons, nothing else. Setting it high makes comparing deeply
614 nested non-cyclical data structures faster, but makes comparing cyclical
615 data structures slower.
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000616*/
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000617#define NESTING_LIMIT 20
618
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000619static int compare_nesting = 0;
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000620
621static PyObject*
Fred Drake100814d2000-07-09 15:48:49 +0000622get_inprogress_dict(void)
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000623{
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000624 static PyObject *key;
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000625 PyObject *tstate_dict, *inprogress;
626
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000627 if (key == NULL) {
628 key = PyString_InternFromString("cmp_state");
629 if (key == NULL)
630 return NULL;
631 }
632
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000633 tstate_dict = PyThreadState_GetDict();
634 if (tstate_dict == NULL) {
635 PyErr_BadInternalCall();
636 return NULL;
637 }
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000638
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000639 inprogress = PyDict_GetItem(tstate_dict, key);
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000640 if (inprogress == NULL) {
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000641 inprogress = PyDict_New();
642 if (inprogress == NULL)
643 return NULL;
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000644 if (PyDict_SetItem(tstate_dict, key, inprogress) == -1) {
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000645 Py_DECREF(inprogress);
646 return NULL;
647 }
Jeremy Hyltona251ea02000-06-09 16:20:39 +0000648 Py_DECREF(inprogress);
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000649 }
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000650
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000651 return inprogress;
652}
653
654static PyObject *
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000655check_recursion(PyObject *v, PyObject *w, int op)
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000656{
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000657 PyObject *inprogress;
658 PyObject *token;
Barry Warsaw9d23a4e2000-08-18 05:01:19 +0000659 Py_uintptr_t iv = (Py_uintptr_t)v;
660 Py_uintptr_t iw = (Py_uintptr_t)w;
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000661 PyObject *x, *y, *z;
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000662
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000663 inprogress = get_inprogress_dict();
664 if (inprogress == NULL)
665 return NULL;
666
667 token = PyTuple_New(3);
668 if (token == NULL)
669 return NULL;
670
671 if (iv <= iw) {
672 PyTuple_SET_ITEM(token, 0, x = PyLong_FromVoidPtr((void *)v));
673 PyTuple_SET_ITEM(token, 1, y = PyLong_FromVoidPtr((void *)w));
674 if (op >= 0)
675 op = swapped_op[op];
676 } else {
677 PyTuple_SET_ITEM(token, 0, x = PyLong_FromVoidPtr((void *)w));
678 PyTuple_SET_ITEM(token, 1, y = PyLong_FromVoidPtr((void *)v));
679 }
680 PyTuple_SET_ITEM(token, 2, z = PyInt_FromLong((long)op));
681 if (x == NULL || y == NULL || z == NULL) {
682 Py_DECREF(token);
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000683 return NULL;
684 }
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000685
686 if (PyDict_GetItem(inprogress, token) != NULL) {
687 Py_DECREF(token);
688 return Py_None; /* Without INCREF! */
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000689 }
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000690
691 if (PyDict_SetItem(inprogress, token, token) < 0) {
692 Py_DECREF(token);
693 return NULL;
694 }
695
696 return token;
697}
698
699static void
700delete_token(PyObject *token)
701{
702 PyObject *inprogress;
703
704 if (token == NULL || token == Py_None)
705 return;
706 inprogress = get_inprogress_dict();
707 if (inprogress == NULL)
708 PyErr_Clear();
709 else
710 PyDict_DelItem(inprogress, token);
711 Py_DECREF(token);
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000712}
713
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000714int
Fred Drake100814d2000-07-09 15:48:49 +0000715PyObject_Compare(PyObject *v, PyObject *w)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000716{
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000717 PyTypeObject *vtp;
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000718 int result;
719
Jack Jansend49cbe12000-08-22 21:52:51 +0000720#if defined(USE_STACKCHECK)
Guido van Rossum9c0a99e2000-08-30 15:53:50 +0000721 if (PyOS_CheckStack()) {
Jack Jansend49cbe12000-08-22 21:52:51 +0000722 PyErr_SetString(PyExc_MemoryError, "Stack overflow");
723 return -1;
724 }
725#endif
Guido van Rossumc8b6df91997-05-23 00:06:51 +0000726 if (v == NULL || w == NULL) {
727 PyErr_BadInternalCall();
728 return -1;
729 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000730 if (v == w)
731 return 0;
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +0000732 vtp = v->ob_type;
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000733 compare_nesting++;
734 if (compare_nesting > NESTING_LIMIT &&
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +0000735 (vtp->tp_as_mapping
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000736 || (vtp->tp_as_sequence
737 && !PyString_Check(v)
738 && !PyTuple_Check(v)))) {
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +0000739 /* try to detect circular data structures */
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000740 PyObject *token = check_recursion(v, w, -1);
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000741
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000742 if (token == NULL) {
743 result = -1;
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000744 }
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000745 else if (token == Py_None) {
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000746 /* already comparing these objects. assume
747 they're equal until shown otherwise */
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +0000748 result = 0;
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000749 }
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000750 else {
751 result = do_cmp(v, w);
752 delete_token(token);
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000753 }
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000754 }
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +0000755 else {
756 result = do_cmp(v, w);
757 }
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000758 compare_nesting--;
Guido van Rossume797ec12001-01-17 15:24:28 +0000759 return result < 0 ? -1 : result;
760}
761
762static PyObject *
763try_3way_to_rich_compare(PyObject *v, PyObject *w, int op)
764{
765 int c;
766 PyObject *result;
767
768 c = try_3way_compare(v, w);
Guido van Rossume797ec12001-01-17 15:24:28 +0000769 if (c >= 2)
770 c = default_3way_compare(v, w);
Guido van Rossum2da0ea82001-02-22 22:18:04 +0000771 if (c <= -2)
772 return NULL;
Guido van Rossume797ec12001-01-17 15:24:28 +0000773 switch (op) {
774 case Py_LT: c = c < 0; break;
775 case Py_LE: c = c <= 0; break;
776 case Py_EQ: c = c == 0; break;
777 case Py_NE: c = c != 0; break;
778 case Py_GT: c = c > 0; break;
779 case Py_GE: c = c >= 0; break;
780 }
781 result = c ? Py_True : Py_False;
782 Py_INCREF(result);
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000783 return result;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000784}
785
Neil Schemenauerd38855c2001-01-21 16:25:18 +0000786static PyObject *
Guido van Rossume797ec12001-01-17 15:24:28 +0000787do_richcmp(PyObject *v, PyObject *w, int op)
788{
789 PyObject *res;
790
791 res = try_rich_compare(v, w, op);
792 if (res != Py_NotImplemented)
793 return res;
794 Py_DECREF(res);
795
796 return try_3way_to_rich_compare(v, w, op);
797}
798
799PyObject *
800PyObject_RichCompare(PyObject *v, PyObject *w, int op)
801{
802 PyObject *res;
803
804 assert(Py_LT <= op && op <= Py_GE);
805
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000806 compare_nesting++;
807 if (compare_nesting > NESTING_LIMIT &&
808 (v->ob_type->tp_as_mapping
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000809 || (v->ob_type->tp_as_sequence
810 && !PyString_Check(v)
811 && !PyTuple_Check(v)))) {
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000812 /* try to detect circular data structures */
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000813 PyObject *token = check_recursion(v, w, op);
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000814
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000815 if (token == NULL) {
816 res = NULL;
Guido van Rossume797ec12001-01-17 15:24:28 +0000817 }
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000818 else if (token == Py_None) {
819 /* already comparing these objects with this operator.
820 assume they're equal until shown otherwise */
821 if (op == Py_EQ)
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000822 res = Py_True;
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000823 else if (op == Py_NE)
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000824 res = Py_False;
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000825 else {
826 PyErr_SetString(PyExc_ValueError,
827 "can't order recursive values");
828 res = NULL;
829 }
830 Py_XINCREF(res);
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000831 }
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000832 else {
833 res = do_richcmp(v, w, op);
834 delete_token(token);
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000835 }
Guido van Rossume797ec12001-01-17 15:24:28 +0000836 }
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000837 else {
838 res = do_richcmp(v, w, op);
839 }
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000840 compare_nesting--;
Guido van Rossume797ec12001-01-17 15:24:28 +0000841 return res;
842}
843
Tim Petersde9725f2001-05-05 10:06:17 +0000844/* Return -1 if error; 1 if v op w; 0 if not (v op w). */
Guido van Rossume797ec12001-01-17 15:24:28 +0000845int
846PyObject_RichCompareBool(PyObject *v, PyObject *w, int op)
847{
848 PyObject *res = PyObject_RichCompare(v, w, op);
849 int ok;
850
851 if (res == NULL)
852 return -1;
853 ok = PyObject_IsTrue(res);
854 Py_DECREF(res);
855 return ok;
856}
Fred Drake13634cf2000-06-29 19:17:04 +0000857
858/* Set of hash utility functions to help maintaining the invariant that
859 iff a==b then hash(a)==hash(b)
860
861 All the utility functions (_Py_Hash*()) return "-1" to signify an error.
862*/
863
864long
Fred Drake100814d2000-07-09 15:48:49 +0000865_Py_HashDouble(double v)
Fred Drake13634cf2000-06-29 19:17:04 +0000866{
Tim Peters39dce292000-08-15 03:34:48 +0000867 double intpart, fractpart;
868 int expo;
869 long hipart;
870 long x; /* the final hash value */
871 /* This is designed so that Python numbers of different types
872 * that compare equal hash to the same value; otherwise comparisons
873 * of mapping keys will turn out weird.
874 */
875
876#ifdef MPW /* MPW C modf expects pointer to extended as second argument */
877{
878 extended e;
879 fractpart = modf(v, &e);
880 intpart = e;
881}
882#else
883 fractpart = modf(v, &intpart);
884#endif
885 if (fractpart == 0.0) {
886 /* This must return the same hash as an equal int or long. */
887 if (intpart > LONG_MAX || -intpart > LONG_MAX) {
888 /* Convert to long and use its hash. */
889 PyObject *plong; /* converted to Python long */
890 if (Py_IS_INFINITY(intpart))
891 /* can't convert to long int -- arbitrary */
892 v = v < 0 ? -271828.0 : 314159.0;
893 plong = PyLong_FromDouble(v);
894 if (plong == NULL)
895 return -1;
896 x = PyObject_Hash(plong);
897 Py_DECREF(plong);
898 return x;
899 }
900 /* Fits in a C long == a Python int, so is its own hash. */
901 x = (long)intpart;
902 if (x == -1)
903 x = -2;
904 return x;
905 }
906 /* The fractional part is non-zero, so we don't have to worry about
907 * making this match the hash of some other type.
908 * Use frexp to get at the bits in the double.
Fred Drake13634cf2000-06-29 19:17:04 +0000909 * Since the VAX D double format has 56 mantissa bits, which is the
910 * most of any double format in use, each of these parts may have as
911 * many as (but no more than) 56 significant bits.
Tim Peters39dce292000-08-15 03:34:48 +0000912 * So, assuming sizeof(long) >= 4, each part can be broken into two
913 * longs; frexp and multiplication are used to do that.
914 * Also, since the Cray double format has 15 exponent bits, which is
915 * the most of any double format in use, shifting the exponent field
916 * left by 15 won't overflow a long (again assuming sizeof(long) >= 4).
Fred Drake13634cf2000-06-29 19:17:04 +0000917 */
Tim Peters39dce292000-08-15 03:34:48 +0000918 v = frexp(v, &expo);
919 v *= 2147483648.0; /* 2**31 */
920 hipart = (long)v; /* take the top 32 bits */
921 v = (v - (double)hipart) * 2147483648.0; /* get the next 32 bits */
922 x = hipart + (long)v + (expo << 15);
923 if (x == -1)
924 x = -2;
925 return x;
Fred Drake13634cf2000-06-29 19:17:04 +0000926}
927
928long
Fred Drake100814d2000-07-09 15:48:49 +0000929_Py_HashPointer(void *p)
Fred Drake13634cf2000-06-29 19:17:04 +0000930{
931#if SIZEOF_LONG >= SIZEOF_VOID_P
932 return (long)p;
933#else
934 /* convert to a Python long and hash that */
935 PyObject* longobj;
936 long x;
937
938 if ((longobj = PyLong_FromVoidPtr(p)) == NULL) {
939 x = -1;
940 goto finally;
941 }
942 x = PyObject_Hash(longobj);
943
944finally:
945 Py_XDECREF(longobj);
946 return x;
947#endif
948}
949
950
Guido van Rossum9bfef441993-03-29 10:43:31 +0000951long
Fred Drake100814d2000-07-09 15:48:49 +0000952PyObject_Hash(PyObject *v)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000953{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000954 PyTypeObject *tp = v->ob_type;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000955 if (tp->tp_hash != NULL)
956 return (*tp->tp_hash)(v);
Guido van Rossumd1f06b92001-01-24 22:14:43 +0000957 if (tp->tp_compare == NULL && RICHCOMPARE(tp) == NULL) {
Fred Drake13634cf2000-06-29 19:17:04 +0000958 return _Py_HashPointer(v); /* Use address as hash value */
959 }
Guido van Rossum9bfef441993-03-29 10:43:31 +0000960 /* If there's a cmp but no hash defined, the object can't be hashed */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000961 PyErr_SetString(PyExc_TypeError, "unhashable type");
Guido van Rossum9bfef441993-03-29 10:43:31 +0000962 return -1;
963}
964
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000965PyObject *
Fred Drake100814d2000-07-09 15:48:49 +0000966PyObject_GetAttrString(PyObject *v, char *name)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000967{
Guido van Rossumd8eb1b31996-08-09 20:52:03 +0000968 if (v->ob_type->tp_getattro != NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000969 PyObject *w, *res;
Guido van Rossumc6d06701997-01-18 07:57:16 +0000970 w = PyString_InternFromString(name);
Guido van Rossumd8eb1b31996-08-09 20:52:03 +0000971 if (w == NULL)
972 return NULL;
973 res = (*v->ob_type->tp_getattro)(v, w);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000974 Py_XDECREF(w);
Guido van Rossumd8eb1b31996-08-09 20:52:03 +0000975 return res;
976 }
977
Guido van Rossum3f5da241990-12-20 15:06:42 +0000978 if (v->ob_type->tp_getattr == NULL) {
Guido van Rossumdb935161998-01-19 22:16:36 +0000979 PyErr_Format(PyExc_AttributeError,
980 "'%.50s' object has no attribute '%.400s'",
981 v->ob_type->tp_name,
982 name);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000983 return NULL;
984 }
985 else {
986 return (*v->ob_type->tp_getattr)(v, name);
987 }
988}
989
990int
Fred Drake100814d2000-07-09 15:48:49 +0000991PyObject_HasAttrString(PyObject *v, char *name)
Guido van Rossumed18fdc1993-07-11 19:55:34 +0000992{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000993 PyObject *res = PyObject_GetAttrString(v, name);
Guido van Rossumed18fdc1993-07-11 19:55:34 +0000994 if (res != NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000995 Py_DECREF(res);
Guido van Rossumed18fdc1993-07-11 19:55:34 +0000996 return 1;
997 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000998 PyErr_Clear();
Guido van Rossumed18fdc1993-07-11 19:55:34 +0000999 return 0;
1000}
1001
1002int
Fred Drake100814d2000-07-09 15:48:49 +00001003PyObject_SetAttrString(PyObject *v, char *name, PyObject *w)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001004{
Guido van Rossumd8eb1b31996-08-09 20:52:03 +00001005 if (v->ob_type->tp_setattro != NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001006 PyObject *s;
Guido van Rossumd8eb1b31996-08-09 20:52:03 +00001007 int res;
Guido van Rossumc6d06701997-01-18 07:57:16 +00001008 s = PyString_InternFromString(name);
Guido van Rossumd8eb1b31996-08-09 20:52:03 +00001009 if (s == NULL)
Guido van Rossumb7fc3041996-09-11 22:51:25 +00001010 return -1;
Guido van Rossumd8eb1b31996-08-09 20:52:03 +00001011 res = (*v->ob_type->tp_setattro)(v, s, w);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001012 Py_XDECREF(s);
Guido van Rossumd8eb1b31996-08-09 20:52:03 +00001013 return res;
1014 }
1015
Guido van Rossum3f5da241990-12-20 15:06:42 +00001016 if (v->ob_type->tp_setattr == NULL) {
1017 if (v->ob_type->tp_getattr == NULL)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001018 PyErr_SetString(PyExc_TypeError,
Guido van Rossum3ea74121991-12-24 13:28:03 +00001019 "attribute-less object (assign or del)");
Guido van Rossum3f5da241990-12-20 15:06:42 +00001020 else
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001021 PyErr_SetString(PyExc_TypeError,
Guido van Rossum3ea74121991-12-24 13:28:03 +00001022 "object has read-only attributes");
Guido van Rossum73531a31990-12-20 23:12:40 +00001023 return -1;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001024 }
1025 else {
1026 return (*v->ob_type->tp_setattr)(v, name, w);
1027 }
1028}
1029
Marc-André Lemburge44e5072000-09-18 16:20:57 +00001030/* Internal API needed by PyObject_GetAttr(): */
1031extern
1032PyObject *_PyUnicode_AsDefaultEncodedString(PyObject *unicode,
1033 const char *errors);
1034
Guido van Rossum98ff96a1997-05-20 18:34:44 +00001035PyObject *
Fred Drake100814d2000-07-09 15:48:49 +00001036PyObject_GetAttr(PyObject *v, PyObject *name)
Guido van Rossum98ff96a1997-05-20 18:34:44 +00001037{
Marc-André Lemburge44e5072000-09-18 16:20:57 +00001038 /* The Unicode to string conversion is done here because the
1039 existing tp_getattro slots expect a string object as name
1040 and we wouldn't want to break those. */
1041 if (PyUnicode_Check(name)) {
1042 name = _PyUnicode_AsDefaultEncodedString(name, NULL);
1043 if (name == NULL)
1044 return NULL;
1045 }
Jeremy Hylton99a8f902000-06-23 14:36:32 +00001046
1047 if (!PyString_Check(name)) {
1048 PyErr_SetString(PyExc_TypeError,
1049 "attribute name must be string");
1050 return NULL;
1051 }
Marc-André Lemburge44e5072000-09-18 16:20:57 +00001052 if (v->ob_type->tp_getattro != NULL)
1053 return (*v->ob_type->tp_getattro)(v, name);
1054 else
Tim Peters5acbfcc2001-05-11 03:36:45 +00001055 return PyObject_GetAttrString(v, PyString_AS_STRING(name));
Guido van Rossum98ff96a1997-05-20 18:34:44 +00001056}
1057
1058int
Fred Drake100814d2000-07-09 15:48:49 +00001059PyObject_HasAttr(PyObject *v, PyObject *name)
Guido van Rossum98ff96a1997-05-20 18:34:44 +00001060{
1061 PyObject *res = PyObject_GetAttr(v, name);
1062 if (res != NULL) {
1063 Py_DECREF(res);
1064 return 1;
1065 }
1066 PyErr_Clear();
1067 return 0;
1068}
1069
1070int
Fred Drake100814d2000-07-09 15:48:49 +00001071PyObject_SetAttr(PyObject *v, PyObject *name, PyObject *value)
Guido van Rossum98ff96a1997-05-20 18:34:44 +00001072{
1073 int err;
Marc-André Lemburge44e5072000-09-18 16:20:57 +00001074
1075 /* The Unicode to string conversion is done here because the
1076 existing tp_setattro slots expect a string object as name
1077 and we wouldn't want to break those. */
1078 if (PyUnicode_Check(name)) {
1079 name = PyUnicode_AsEncodedString(name, NULL, NULL);
1080 if (name == NULL)
1081 return -1;
Jeremy Hylton99a8f902000-06-23 14:36:32 +00001082 }
Marc-André Lemburge44e5072000-09-18 16:20:57 +00001083 else
1084 Py_INCREF(name);
1085
1086 if (!PyString_Check(name)){
Jeremy Hylton99a8f902000-06-23 14:36:32 +00001087 PyErr_SetString(PyExc_TypeError,
1088 "attribute name must be string");
1089 err = -1;
1090 }
Marc-André Lemburge44e5072000-09-18 16:20:57 +00001091 else {
1092 PyString_InternInPlace(&name);
1093 if (v->ob_type->tp_setattro != NULL)
1094 err = (*v->ob_type->tp_setattro)(v, name, value);
1095 else
1096 err = PyObject_SetAttrString(v,
1097 PyString_AS_STRING(name), value);
1098 }
1099
Guido van Rossum98ff96a1997-05-20 18:34:44 +00001100 Py_DECREF(name);
1101 return err;
1102}
1103
Guido van Rossum6ac258d1993-05-12 08:24:20 +00001104/* Test a value used as condition, e.g., in a for or if statement.
1105 Return -1 if an error occurred */
1106
1107int
Fred Drake100814d2000-07-09 15:48:49 +00001108PyObject_IsTrue(PyObject *v)
Guido van Rossum6ac258d1993-05-12 08:24:20 +00001109{
1110 int res;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001111 if (v == Py_None)
Guido van Rossum6ac258d1993-05-12 08:24:20 +00001112 res = 0;
Guido van Rossum1c4f4581998-05-22 00:53:24 +00001113 else if (v->ob_type->tp_as_number != NULL &&
1114 v->ob_type->tp_as_number->nb_nonzero != NULL)
Guido van Rossum6ac258d1993-05-12 08:24:20 +00001115 res = (*v->ob_type->tp_as_number->nb_nonzero)(v);
Guido van Rossum1c4f4581998-05-22 00:53:24 +00001116 else if (v->ob_type->tp_as_mapping != NULL &&
1117 v->ob_type->tp_as_mapping->mp_length != NULL)
Guido van Rossum6ac258d1993-05-12 08:24:20 +00001118 res = (*v->ob_type->tp_as_mapping->mp_length)(v);
Guido van Rossum1c4f4581998-05-22 00:53:24 +00001119 else if (v->ob_type->tp_as_sequence != NULL &&
1120 v->ob_type->tp_as_sequence->sq_length != NULL)
Guido van Rossum6ac258d1993-05-12 08:24:20 +00001121 res = (*v->ob_type->tp_as_sequence->sq_length)(v);
1122 else
1123 res = 1;
1124 if (res > 0)
1125 res = 1;
1126 return res;
1127}
1128
Guido van Rossumc3d3f961998-04-09 17:53:59 +00001129/* equivalent of 'not v'
1130 Return -1 if an error occurred */
1131
1132int
Fred Drake100814d2000-07-09 15:48:49 +00001133PyObject_Not(PyObject *v)
Guido van Rossumc3d3f961998-04-09 17:53:59 +00001134{
1135 int res;
1136 res = PyObject_IsTrue(v);
1137 if (res < 0)
1138 return res;
1139 return res == 0;
1140}
1141
Guido van Rossum5524a591995-01-10 15:26:20 +00001142/* Coerce two numeric types to the "larger" one.
1143 Increment the reference count on each argument.
Guido van Rossume797ec12001-01-17 15:24:28 +00001144 Return value:
1145 -1 if an error occurred;
1146 0 if the coercion succeeded (and then the reference counts are increased);
1147 1 if no coercion is possible (and no error is raised).
Guido van Rossum5524a591995-01-10 15:26:20 +00001148*/
Guido van Rossum5524a591995-01-10 15:26:20 +00001149int
Fred Drake100814d2000-07-09 15:48:49 +00001150PyNumber_CoerceEx(PyObject **pv, PyObject **pw)
Guido van Rossum5524a591995-01-10 15:26:20 +00001151{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001152 register PyObject *v = *pv;
1153 register PyObject *w = *pw;
Guido van Rossum5524a591995-01-10 15:26:20 +00001154 int res;
1155
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001156 if (v->ob_type == w->ob_type && !PyInstance_Check(v)) {
1157 Py_INCREF(v);
1158 Py_INCREF(w);
Guido van Rossum5524a591995-01-10 15:26:20 +00001159 return 0;
1160 }
1161 if (v->ob_type->tp_as_number && v->ob_type->tp_as_number->nb_coerce) {
1162 res = (*v->ob_type->tp_as_number->nb_coerce)(pv, pw);
1163 if (res <= 0)
1164 return res;
1165 }
1166 if (w->ob_type->tp_as_number && w->ob_type->tp_as_number->nb_coerce) {
1167 res = (*w->ob_type->tp_as_number->nb_coerce)(pw, pv);
1168 if (res <= 0)
1169 return res;
1170 }
Guido van Rossum242c6421997-11-19 16:03:17 +00001171 return 1;
1172}
1173
Guido van Rossume797ec12001-01-17 15:24:28 +00001174/* Coerce two numeric types to the "larger" one.
1175 Increment the reference count on each argument.
1176 Return -1 and raise an exception if no coercion is possible
1177 (and then no reference count is incremented).
1178*/
Guido van Rossum242c6421997-11-19 16:03:17 +00001179int
Fred Drake100814d2000-07-09 15:48:49 +00001180PyNumber_Coerce(PyObject **pv, PyObject **pw)
Guido van Rossum242c6421997-11-19 16:03:17 +00001181{
1182 int err = PyNumber_CoerceEx(pv, pw);
1183 if (err <= 0)
1184 return err;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001185 PyErr_SetString(PyExc_TypeError, "number coercion failed");
Guido van Rossum5524a591995-01-10 15:26:20 +00001186 return -1;
1187}
1188
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001189
Guido van Rossum49b11fe1995-01-26 00:38:22 +00001190/* Test whether an object can be called */
1191
1192int
Fred Drake100814d2000-07-09 15:48:49 +00001193PyCallable_Check(PyObject *x)
Guido van Rossum49b11fe1995-01-26 00:38:22 +00001194{
1195 if (x == NULL)
1196 return 0;
1197 if (x->ob_type->tp_call != NULL ||
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001198 PyFunction_Check(x) ||
1199 PyMethod_Check(x) ||
1200 PyCFunction_Check(x) ||
1201 PyClass_Check(x))
Guido van Rossum49b11fe1995-01-26 00:38:22 +00001202 return 1;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001203 if (PyInstance_Check(x)) {
1204 PyObject *call = PyObject_GetAttrString(x, "__call__");
Guido van Rossum49b11fe1995-01-26 00:38:22 +00001205 if (call == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001206 PyErr_Clear();
Guido van Rossum49b11fe1995-01-26 00:38:22 +00001207 return 0;
1208 }
1209 /* Could test recursively but don't, for fear of endless
1210 recursion if some joker sets self.__call__ = self */
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001211 Py_DECREF(call);
Guido van Rossum49b11fe1995-01-26 00:38:22 +00001212 return 1;
1213 }
1214 return 0;
1215}
1216
1217
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001218/*
1219NoObject is usable as a non-NULL undefined value, used by the macro None.
1220There is (and should be!) no way to create other objects of this type,
Guido van Rossum3f5da241990-12-20 15:06:42 +00001221so there is exactly one (which is indestructible, by the way).
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001222*/
1223
Guido van Rossum0c182a11992-03-27 17:26:13 +00001224/* ARGSUSED */
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001225static PyObject *
Fred Drake100814d2000-07-09 15:48:49 +00001226none_repr(PyObject *op)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001227{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001228 return PyString_FromString("None");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001229}
1230
Barry Warsaw9bf16442001-01-23 16:24:35 +00001231/* ARGUSED */
1232static void
1233none_dealloc(PyObject* ignore)
1234{
1235 /* This should never get called, but we also don't want to SEGV if
1236 * we accidently decref None out of existance.
1237 */
1238 abort();
1239}
1240
1241
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001242static PyTypeObject PyNothing_Type = {
1243 PyObject_HEAD_INIT(&PyType_Type)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001244 0,
Guido van Rossum3f5da241990-12-20 15:06:42 +00001245 "None",
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001246 0,
1247 0,
Barry Warsaw9bf16442001-01-23 16:24:35 +00001248 (destructor)none_dealloc, /*tp_dealloc*/ /*never called*/
Guido van Rossum7066dd71992-09-17 17:54:56 +00001249 0, /*tp_print*/
Guido van Rossum3f5da241990-12-20 15:06:42 +00001250 0, /*tp_getattr*/
1251 0, /*tp_setattr*/
1252 0, /*tp_compare*/
Guido van Rossum1d5735e1994-08-30 08:27:36 +00001253 (reprfunc)none_repr, /*tp_repr*/
Guido van Rossum3f5da241990-12-20 15:06:42 +00001254 0, /*tp_as_number*/
1255 0, /*tp_as_sequence*/
1256 0, /*tp_as_mapping*/
Guido van Rossum9bfef441993-03-29 10:43:31 +00001257 0, /*tp_hash */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001258};
1259
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001260PyObject _Py_NoneStruct = {
1261 PyObject_HEAD_INIT(&PyNothing_Type)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001262};
1263
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +00001264/* NotImplemented is an object that can be used to signal that an
1265 operation is not implemented for the given type combination. */
1266
1267static PyObject *
1268NotImplemented_repr(PyObject *op)
1269{
1270 return PyString_FromString("NotImplemented");
1271}
1272
1273static PyTypeObject PyNotImplemented_Type = {
1274 PyObject_HEAD_INIT(&PyType_Type)
1275 0,
1276 "NotImplemented",
1277 0,
1278 0,
Barry Warsaw9bf16442001-01-23 16:24:35 +00001279 (destructor)none_dealloc, /*tp_dealloc*/ /*never called*/
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +00001280 0, /*tp_print*/
1281 0, /*tp_getattr*/
1282 0, /*tp_setattr*/
1283 0, /*tp_compare*/
1284 (reprfunc)NotImplemented_repr, /*tp_repr*/
1285 0, /*tp_as_number*/
1286 0, /*tp_as_sequence*/
1287 0, /*tp_as_mapping*/
1288 0, /*tp_hash */
1289};
1290
1291PyObject _Py_NotImplementedStruct = {
1292 PyObject_HEAD_INIT(&PyNotImplemented_Type)
1293};
1294
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001295
Guido van Rossum84a90321996-05-22 16:34:47 +00001296#ifdef Py_TRACE_REFS
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001297
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001298static PyObject refchain = {&refchain, &refchain};
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001299
Guido van Rossumaacdc9d1996-08-12 21:32:12 +00001300void
Fred Drake100814d2000-07-09 15:48:49 +00001301_Py_ResetReferences(void)
Guido van Rossume09fb551997-08-05 02:04:34 +00001302{
1303 refchain._ob_prev = refchain._ob_next = &refchain;
1304 _Py_RefTotal = 0;
1305}
1306
1307void
Fred Drake100814d2000-07-09 15:48:49 +00001308_Py_NewReference(PyObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001309{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001310 _Py_RefTotal++;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001311 op->ob_refcnt = 1;
1312 op->_ob_next = refchain._ob_next;
1313 op->_ob_prev = &refchain;
1314 refchain._ob_next->_ob_prev = op;
1315 refchain._ob_next = op;
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +00001316#ifdef COUNT_ALLOCS
1317 inc_count(op->ob_type);
1318#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001319}
1320
Guido van Rossumaacdc9d1996-08-12 21:32:12 +00001321void
Fred Drake100814d2000-07-09 15:48:49 +00001322_Py_ForgetReference(register PyObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001323{
Guido van Rossumbffd6832000-01-20 22:32:56 +00001324#ifdef SLOW_UNREF_CHECK
Guido van Rossum4c08d552000-03-10 22:55:18 +00001325 register PyObject *p;
Guido van Rossumbffd6832000-01-20 22:32:56 +00001326#endif
Guido van Rossumd7047b31995-01-02 19:07:15 +00001327 if (op->ob_refcnt < 0)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001328 Py_FatalError("UNREF negative refcnt");
Guido van Rossum2e8f6141992-09-03 20:32:55 +00001329 if (op == &refchain ||
Guido van Rossumd7047b31995-01-02 19:07:15 +00001330 op->_ob_prev->_ob_next != op || op->_ob_next->_ob_prev != op)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001331 Py_FatalError("UNREF invalid object");
Guido van Rossum2e8f6141992-09-03 20:32:55 +00001332#ifdef SLOW_UNREF_CHECK
Guido van Rossum3f5da241990-12-20 15:06:42 +00001333 for (p = refchain._ob_next; p != &refchain; p = p->_ob_next) {
1334 if (p == op)
1335 break;
1336 }
Guido van Rossumd7047b31995-01-02 19:07:15 +00001337 if (p == &refchain) /* Not found */
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001338 Py_FatalError("UNREF unknown object");
Guido van Rossum2e8f6141992-09-03 20:32:55 +00001339#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001340 op->_ob_next->_ob_prev = op->_ob_prev;
1341 op->_ob_prev->_ob_next = op->_ob_next;
Guido van Rossum2e8f6141992-09-03 20:32:55 +00001342 op->_ob_next = op->_ob_prev = NULL;
Guido van Rossumd8953cb1995-04-06 14:46:26 +00001343#ifdef COUNT_ALLOCS
1344 op->ob_type->tp_free++;
1345#endif
Guido van Rossum3f5da241990-12-20 15:06:42 +00001346}
1347
Guido van Rossumaacdc9d1996-08-12 21:32:12 +00001348void
Fred Drake100814d2000-07-09 15:48:49 +00001349_Py_Dealloc(PyObject *op)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001350{
Guido van Rossum9776adf1994-09-07 14:36:45 +00001351 destructor dealloc = op->ob_type->tp_dealloc;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001352 _Py_ForgetReference(op);
Guido van Rossum9776adf1994-09-07 14:36:45 +00001353 (*dealloc)(op);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001354}
1355
Guido van Rossumaacdc9d1996-08-12 21:32:12 +00001356void
Fred Drake100814d2000-07-09 15:48:49 +00001357_Py_PrintReferences(FILE *fp)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001358{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001359 PyObject *op;
Guido van Rossume09fb551997-08-05 02:04:34 +00001360 fprintf(fp, "Remaining objects:\n");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001361 for (op = refchain._ob_next; op != &refchain; op = op->_ob_next) {
1362 fprintf(fp, "[%d] ", op->ob_refcnt);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001363 if (PyObject_Print(op, fp, 0) != 0)
1364 PyErr_Clear();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001365 putc('\n', fp);
1366 }
1367}
1368
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +00001369PyObject *
Fred Drake100814d2000-07-09 15:48:49 +00001370_Py_GetObjects(PyObject *self, PyObject *args)
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +00001371{
1372 int i, n;
1373 PyObject *t = NULL;
1374 PyObject *res, *op;
1375
1376 if (!PyArg_ParseTuple(args, "i|O", &n, &t))
1377 return NULL;
1378 op = refchain._ob_next;
1379 res = PyList_New(0);
1380 if (res == NULL)
1381 return NULL;
1382 for (i = 0; (n == 0 || i < n) && op != &refchain; i++) {
1383 while (op == self || op == args || op == res || op == t ||
1384 t != NULL && op->ob_type != (PyTypeObject *) t) {
1385 op = op->_ob_next;
1386 if (op == &refchain)
1387 return res;
1388 }
1389 if (PyList_Append(res, op) < 0) {
1390 Py_DECREF(res);
1391 return NULL;
1392 }
1393 op = op->_ob_next;
1394 }
1395 return res;
1396}
1397
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001398#endif
Guido van Rossum97ead3f1996-01-12 01:24:09 +00001399
1400
1401/* Hack to force loading of cobject.o */
Guido van Rossumda9c2711996-12-05 21:58:58 +00001402PyTypeObject *_Py_cobject_hack = &PyCObject_Type;
Guido van Rossum84a90321996-05-22 16:34:47 +00001403
1404
1405/* Hack to force loading of abstract.o */
Jeremy Hylton6253f832000-07-12 12:56:19 +00001406int (*_Py_abstract_hack)(PyObject *) = &PyObject_Size;
Guido van Rossume09fb551997-08-05 02:04:34 +00001407
1408
Andrew M. Kuchling1582a3a2000-08-16 12:27:23 +00001409/* Python's malloc wrappers (see pymem.h) */
Guido van Rossume09fb551997-08-05 02:04:34 +00001410
Thomas Wouters334fb892000-07-25 12:56:38 +00001411void *
Fred Drake100814d2000-07-09 15:48:49 +00001412PyMem_Malloc(size_t nbytes)
Guido van Rossume09fb551997-08-05 02:04:34 +00001413{
1414#if _PyMem_EXTRA > 0
1415 if (nbytes == 0)
1416 nbytes = _PyMem_EXTRA;
1417#endif
Guido van Rossumb18618d2000-05-03 23:44:39 +00001418 return PyMem_MALLOC(nbytes);
Guido van Rossume09fb551997-08-05 02:04:34 +00001419}
1420
Thomas Wouters334fb892000-07-25 12:56:38 +00001421void *
1422PyMem_Realloc(void *p, size_t nbytes)
Guido van Rossume09fb551997-08-05 02:04:34 +00001423{
1424#if _PyMem_EXTRA > 0
1425 if (nbytes == 0)
1426 nbytes = _PyMem_EXTRA;
1427#endif
Guido van Rossumb18618d2000-05-03 23:44:39 +00001428 return PyMem_REALLOC(p, nbytes);
Guido van Rossume09fb551997-08-05 02:04:34 +00001429}
1430
1431void
Thomas Wouters334fb892000-07-25 12:56:38 +00001432PyMem_Free(void *p)
Guido van Rossume09fb551997-08-05 02:04:34 +00001433{
Guido van Rossumb18618d2000-05-03 23:44:39 +00001434 PyMem_FREE(p);
1435}
1436
1437
1438/* Python's object malloc wrappers (see objimpl.h) */
1439
Thomas Wouters334fb892000-07-25 12:56:38 +00001440void *
Fred Drake100814d2000-07-09 15:48:49 +00001441PyObject_Malloc(size_t nbytes)
Guido van Rossumb18618d2000-05-03 23:44:39 +00001442{
1443 return PyObject_MALLOC(nbytes);
1444}
1445
Thomas Wouters334fb892000-07-25 12:56:38 +00001446void *
1447PyObject_Realloc(void *p, size_t nbytes)
Guido van Rossumb18618d2000-05-03 23:44:39 +00001448{
1449 return PyObject_REALLOC(p, nbytes);
1450}
1451
1452void
Thomas Wouters334fb892000-07-25 12:56:38 +00001453PyObject_Free(void *p)
Guido van Rossumb18618d2000-05-03 23:44:39 +00001454{
1455 PyObject_FREE(p);
Guido van Rossume09fb551997-08-05 02:04:34 +00001456}
Guido van Rossum86610361998-04-10 22:32:46 +00001457
1458
Fred Drake41deb1e2001-02-01 05:27:45 +00001459/* Hook to clear up weak references only once the _weakref module is
1460 imported. We use a dummy implementation to simplify the code at each
1461 call site instead of requiring a test for NULL.
1462*/
1463
Fred Drakeb60654b2001-02-26 18:56:37 +00001464static void
Fred Drake41deb1e2001-02-01 05:27:45 +00001465empty_clear_weak_refs(PyObject *o)
1466{
Fred Drakeb60654b2001-02-26 18:56:37 +00001467 return;
Fred Drake41deb1e2001-02-01 05:27:45 +00001468}
1469
Fred Drakeb60654b2001-02-26 18:56:37 +00001470void (*PyObject_ClearWeakRefs)(PyObject *) = empty_clear_weak_refs;
Fred Drake41deb1e2001-02-01 05:27:45 +00001471
1472
1473
Guido van Rossum86610361998-04-10 22:32:46 +00001474/* These methods are used to control infinite recursion in repr, str, print,
1475 etc. Container objects that may recursively contain themselves,
1476 e.g. builtin dictionaries and lists, should used Py_ReprEnter() and
1477 Py_ReprLeave() to avoid infinite recursion.
1478
1479 Py_ReprEnter() returns 0 the first time it is called for a particular
1480 object and 1 every time thereafter. It returns -1 if an exception
1481 occurred. Py_ReprLeave() has no return value.
1482
1483 See dictobject.c and listobject.c for examples of use.
1484*/
1485
1486#define KEY "Py_Repr"
1487
1488int
Fred Drake100814d2000-07-09 15:48:49 +00001489Py_ReprEnter(PyObject *obj)
Guido van Rossum86610361998-04-10 22:32:46 +00001490{
1491 PyObject *dict;
1492 PyObject *list;
1493 int i;
1494
1495 dict = PyThreadState_GetDict();
1496 if (dict == NULL)
1497 return -1;
1498 list = PyDict_GetItemString(dict, KEY);
1499 if (list == NULL) {
1500 list = PyList_New(0);
1501 if (list == NULL)
1502 return -1;
1503 if (PyDict_SetItemString(dict, KEY, list) < 0)
1504 return -1;
1505 Py_DECREF(list);
1506 }
1507 i = PyList_GET_SIZE(list);
1508 while (--i >= 0) {
1509 if (PyList_GET_ITEM(list, i) == obj)
1510 return 1;
1511 }
1512 PyList_Append(list, obj);
1513 return 0;
1514}
1515
1516void
Fred Drake100814d2000-07-09 15:48:49 +00001517Py_ReprLeave(PyObject *obj)
Guido van Rossum86610361998-04-10 22:32:46 +00001518{
1519 PyObject *dict;
1520 PyObject *list;
1521 int i;
1522
1523 dict = PyThreadState_GetDict();
Guido van Rossumeb909461998-04-11 15:17:34 +00001524 if (dict == NULL)
1525 return;
Guido van Rossum86610361998-04-10 22:32:46 +00001526 list = PyDict_GetItemString(dict, KEY);
Guido van Rossumeb909461998-04-11 15:17:34 +00001527 if (list == NULL || !PyList_Check(list))
1528 return;
Guido van Rossum86610361998-04-10 22:32:46 +00001529 i = PyList_GET_SIZE(list);
1530 /* Count backwards because we always expect obj to be list[-1] */
1531 while (--i >= 0) {
1532 if (PyList_GET_ITEM(list, i) == obj) {
1533 PyList_SetSlice(list, i, i + 1, NULL);
1534 break;
1535 }
1536 }
1537}
Guido van Rossumd724b232000-03-13 16:01:29 +00001538
1539/*
1540 trashcan
1541 CT 2k0130
1542 non-recursively destroy nested objects
1543
1544 CT 2k0223
1545 everything is now done in a macro.
1546
1547 CT 2k0305
1548 modified to use functions, after Tim Peter's suggestion.
1549
1550 CT 2k0309
1551 modified to restore a possible error.
Guido van Rossum13ff8eb2000-03-25 18:39:19 +00001552
1553 CT 2k0325
1554 added better safe than sorry check for threadstate
Guido van Rossume92e6102000-04-24 15:40:53 +00001555
1556 CT 2k0422
1557 complete rewrite. We now build a chain via ob_type
1558 and save the limited number of types in ob_refcnt.
1559 This is perfect since we don't need any memory.
1560 A patch for free-threading would need just a lock.
Guido van Rossumd724b232000-03-13 16:01:29 +00001561*/
1562
Guido van Rossume92e6102000-04-24 15:40:53 +00001563#define Py_TRASHCAN_TUPLE 1
1564#define Py_TRASHCAN_LIST 2
1565#define Py_TRASHCAN_DICT 3
1566#define Py_TRASHCAN_FRAME 4
1567#define Py_TRASHCAN_TRACEBACK 5
1568/* extend here if other objects want protection */
1569
Guido van Rossumd724b232000-03-13 16:01:29 +00001570int _PyTrash_delete_nesting = 0;
Guido van Rossume92e6102000-04-24 15:40:53 +00001571
Guido van Rossumd724b232000-03-13 16:01:29 +00001572PyObject * _PyTrash_delete_later = NULL;
1573
1574void
Fred Drake100814d2000-07-09 15:48:49 +00001575_PyTrash_deposit_object(PyObject *op)
Guido van Rossumd724b232000-03-13 16:01:29 +00001576{
Guido van Rossume92e6102000-04-24 15:40:53 +00001577 int typecode;
Guido van Rossum13ff8eb2000-03-25 18:39:19 +00001578
Guido van Rossume92e6102000-04-24 15:40:53 +00001579 if (PyTuple_Check(op))
1580 typecode = Py_TRASHCAN_TUPLE;
1581 else if (PyList_Check(op))
1582 typecode = Py_TRASHCAN_LIST;
1583 else if (PyDict_Check(op))
1584 typecode = Py_TRASHCAN_DICT;
1585 else if (PyFrame_Check(op))
1586 typecode = Py_TRASHCAN_FRAME;
1587 else if (PyTraceBack_Check(op))
1588 typecode = Py_TRASHCAN_TRACEBACK;
Moshe Zadkacf703f02000-08-04 15:36:13 +00001589 else /* We have a bug here -- those are the only types in GC */ {
1590 Py_FatalError("Type not supported in GC -- internal bug");
1591 return; /* pacify compiler -- execution never here */
1592 }
Guido van Rossume92e6102000-04-24 15:40:53 +00001593 op->ob_refcnt = typecode;
Guido van Rossumd724b232000-03-13 16:01:29 +00001594
Guido van Rossume92e6102000-04-24 15:40:53 +00001595 op->ob_type = (PyTypeObject*)_PyTrash_delete_later;
1596 _PyTrash_delete_later = op;
Guido van Rossumd724b232000-03-13 16:01:29 +00001597}
1598
1599void
Fred Drake100814d2000-07-09 15:48:49 +00001600_PyTrash_destroy_chain(void)
Guido van Rossumd724b232000-03-13 16:01:29 +00001601{
1602 while (_PyTrash_delete_later) {
1603 PyObject *shredder = _PyTrash_delete_later;
Guido van Rossume92e6102000-04-24 15:40:53 +00001604 _PyTrash_delete_later = (PyObject*) shredder->ob_type;
1605
1606 switch (shredder->ob_refcnt) {
1607 case Py_TRASHCAN_TUPLE:
1608 shredder->ob_type = &PyTuple_Type;
1609 break;
1610 case Py_TRASHCAN_LIST:
1611 shredder->ob_type = &PyList_Type;
1612 break;
1613 case Py_TRASHCAN_DICT:
1614 shredder->ob_type = &PyDict_Type;
1615 break;
1616 case Py_TRASHCAN_FRAME:
1617 shredder->ob_type = &PyFrame_Type;
1618 break;
1619 case Py_TRASHCAN_TRACEBACK:
1620 shredder->ob_type = &PyTraceBack_Type;
1621 break;
1622 }
1623 _Py_NewReference(shredder);
1624
Guido van Rossumd724b232000-03-13 16:01:29 +00001625 ++_PyTrash_delete_nesting;
1626 Py_DECREF(shredder);
1627 --_PyTrash_delete_nesting;
1628 }
1629}
Neil Schemenauera35c6882001-02-27 04:45:05 +00001630
1631#ifdef WITH_PYMALLOC
1632#include "obmalloc.c"
1633#endif