blob: 2c033f88f2f5ab3831004fdcbe0228846b8761ea [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:
450 return -1;
451 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
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +0000588static int
Fred Drake100814d2000-07-09 15:48:49 +0000589do_cmp(PyObject *v, PyObject *w)
Guido van Rossum20566841995-01-12 11:26:10 +0000590{
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +0000591 int c;
592
Guido van Rossume797ec12001-01-17 15:24:28 +0000593 c = try_rich_to_3way_compare(v, w);
594 if (c < 2)
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +0000595 return c;
Guido van Rossume797ec12001-01-17 15:24:28 +0000596 c = try_3way_compare(v, w);
597 if (c < 2)
598 return c;
599 return default_3way_compare(v, w);
Guido van Rossum20566841995-01-12 11:26:10 +0000600}
601
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000602/* compare_nesting is incremented before calling compare (for
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000603 some types) and decremented on exit. If the count exceeds the
604 nesting limit, enable code to detect circular data structures.
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000605
606 This is a tunable parameter that should only affect the performance
607 of comparisons, nothing else. Setting it high makes comparing deeply
608 nested non-cyclical data structures faster, but makes comparing cyclical
609 data structures slower.
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000610*/
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000611#define NESTING_LIMIT 20
612
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000613static int compare_nesting = 0;
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000614
615static PyObject*
Fred Drake100814d2000-07-09 15:48:49 +0000616get_inprogress_dict(void)
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000617{
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000618 static PyObject *key;
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000619 PyObject *tstate_dict, *inprogress;
620
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000621 if (key == NULL) {
622 key = PyString_InternFromString("cmp_state");
623 if (key == NULL)
624 return NULL;
625 }
626
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000627 tstate_dict = PyThreadState_GetDict();
628 if (tstate_dict == NULL) {
629 PyErr_BadInternalCall();
630 return NULL;
631 }
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000632
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000633 inprogress = PyDict_GetItem(tstate_dict, key);
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000634 if (inprogress == NULL) {
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000635 inprogress = PyDict_New();
636 if (inprogress == NULL)
637 return NULL;
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000638 if (PyDict_SetItem(tstate_dict, key, inprogress) == -1) {
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000639 Py_DECREF(inprogress);
640 return NULL;
641 }
Jeremy Hyltona251ea02000-06-09 16:20:39 +0000642 Py_DECREF(inprogress);
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000643 }
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000644
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000645 return inprogress;
646}
647
648static PyObject *
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000649check_recursion(PyObject *v, PyObject *w, int op)
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000650{
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000651 PyObject *inprogress;
652 PyObject *token;
Barry Warsaw9d23a4e2000-08-18 05:01:19 +0000653 Py_uintptr_t iv = (Py_uintptr_t)v;
654 Py_uintptr_t iw = (Py_uintptr_t)w;
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000655 PyObject *x, *y, *z;
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000656
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000657 inprogress = get_inprogress_dict();
658 if (inprogress == NULL)
659 return NULL;
660
661 token = PyTuple_New(3);
662 if (token == NULL)
663 return NULL;
664
665 if (iv <= iw) {
666 PyTuple_SET_ITEM(token, 0, x = PyLong_FromVoidPtr((void *)v));
667 PyTuple_SET_ITEM(token, 1, y = PyLong_FromVoidPtr((void *)w));
668 if (op >= 0)
669 op = swapped_op[op];
670 } else {
671 PyTuple_SET_ITEM(token, 0, x = PyLong_FromVoidPtr((void *)w));
672 PyTuple_SET_ITEM(token, 1, y = PyLong_FromVoidPtr((void *)v));
673 }
674 PyTuple_SET_ITEM(token, 2, z = PyInt_FromLong((long)op));
675 if (x == NULL || y == NULL || z == NULL) {
676 Py_DECREF(token);
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000677 return NULL;
678 }
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000679
680 if (PyDict_GetItem(inprogress, token) != NULL) {
681 Py_DECREF(token);
682 return Py_None; /* Without INCREF! */
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000683 }
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000684
685 if (PyDict_SetItem(inprogress, token, token) < 0) {
686 Py_DECREF(token);
687 return NULL;
688 }
689
690 return token;
691}
692
693static void
694delete_token(PyObject *token)
695{
696 PyObject *inprogress;
697
698 if (token == NULL || token == Py_None)
699 return;
700 inprogress = get_inprogress_dict();
701 if (inprogress == NULL)
702 PyErr_Clear();
703 else
704 PyDict_DelItem(inprogress, token);
705 Py_DECREF(token);
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000706}
707
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000708int
Fred Drake100814d2000-07-09 15:48:49 +0000709PyObject_Compare(PyObject *v, PyObject *w)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000710{
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000711 PyTypeObject *vtp;
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000712 int result;
713
Jack Jansend49cbe12000-08-22 21:52:51 +0000714#if defined(USE_STACKCHECK)
Guido van Rossum9c0a99e2000-08-30 15:53:50 +0000715 if (PyOS_CheckStack()) {
Jack Jansend49cbe12000-08-22 21:52:51 +0000716 PyErr_SetString(PyExc_MemoryError, "Stack overflow");
717 return -1;
718 }
719#endif
Guido van Rossumc8b6df91997-05-23 00:06:51 +0000720 if (v == NULL || w == NULL) {
721 PyErr_BadInternalCall();
722 return -1;
723 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000724 if (v == w)
725 return 0;
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +0000726 vtp = v->ob_type;
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000727 compare_nesting++;
728 if (compare_nesting > NESTING_LIMIT &&
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +0000729 (vtp->tp_as_mapping
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000730 || (vtp->tp_as_sequence
731 && !PyString_Check(v)
732 && !PyTuple_Check(v)))) {
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +0000733 /* try to detect circular data structures */
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000734 PyObject *token = check_recursion(v, w, -1);
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000735
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000736 if (token == NULL) {
737 result = -1;
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000738 }
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000739 else if (token == Py_None) {
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000740 /* already comparing these objects. assume
741 they're equal until shown otherwise */
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +0000742 result = 0;
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000743 }
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000744 else {
745 result = do_cmp(v, w);
746 delete_token(token);
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000747 }
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000748 }
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +0000749 else {
750 result = do_cmp(v, w);
751 }
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000752 compare_nesting--;
Guido van Rossume797ec12001-01-17 15:24:28 +0000753 return result < 0 ? -1 : result;
754}
755
756static PyObject *
757try_3way_to_rich_compare(PyObject *v, PyObject *w, int op)
758{
759 int c;
760 PyObject *result;
761
762 c = try_3way_compare(v, w);
Guido van Rossume797ec12001-01-17 15:24:28 +0000763 if (c >= 2)
764 c = default_3way_compare(v, w);
Guido van Rossum2da0ea82001-02-22 22:18:04 +0000765 if (c <= -2)
766 return NULL;
Guido van Rossume797ec12001-01-17 15:24:28 +0000767 switch (op) {
768 case Py_LT: c = c < 0; break;
769 case Py_LE: c = c <= 0; break;
770 case Py_EQ: c = c == 0; break;
771 case Py_NE: c = c != 0; break;
772 case Py_GT: c = c > 0; break;
773 case Py_GE: c = c >= 0; break;
774 }
775 result = c ? Py_True : Py_False;
776 Py_INCREF(result);
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000777 return result;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000778}
779
Neil Schemenauerd38855c2001-01-21 16:25:18 +0000780static PyObject *
Guido van Rossume797ec12001-01-17 15:24:28 +0000781do_richcmp(PyObject *v, PyObject *w, int op)
782{
783 PyObject *res;
784
785 res = try_rich_compare(v, w, op);
786 if (res != Py_NotImplemented)
787 return res;
788 Py_DECREF(res);
789
790 return try_3way_to_rich_compare(v, w, op);
791}
792
793PyObject *
794PyObject_RichCompare(PyObject *v, PyObject *w, int op)
795{
796 PyObject *res;
797
798 assert(Py_LT <= op && op <= Py_GE);
799
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000800 compare_nesting++;
801 if (compare_nesting > NESTING_LIMIT &&
802 (v->ob_type->tp_as_mapping
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000803 || (v->ob_type->tp_as_sequence
804 && !PyString_Check(v)
805 && !PyTuple_Check(v)))) {
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000806 /* try to detect circular data structures */
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000807 PyObject *token = check_recursion(v, w, op);
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000808
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000809 if (token == NULL) {
810 res = NULL;
Guido van Rossume797ec12001-01-17 15:24:28 +0000811 }
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000812 else if (token == Py_None) {
813 /* already comparing these objects with this operator.
814 assume they're equal until shown otherwise */
815 if (op == Py_EQ)
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000816 res = Py_True;
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000817 else if (op == Py_NE)
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000818 res = Py_False;
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000819 else {
820 PyErr_SetString(PyExc_ValueError,
821 "can't order recursive values");
822 res = NULL;
823 }
824 Py_XINCREF(res);
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000825 }
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000826 else {
827 res = do_richcmp(v, w, op);
828 delete_token(token);
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000829 }
Guido van Rossume797ec12001-01-17 15:24:28 +0000830 }
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000831 else {
832 res = do_richcmp(v, w, op);
833 }
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000834 compare_nesting--;
Guido van Rossume797ec12001-01-17 15:24:28 +0000835 return res;
836}
837
838int
839PyObject_RichCompareBool(PyObject *v, PyObject *w, int op)
840{
841 PyObject *res = PyObject_RichCompare(v, w, op);
842 int ok;
843
844 if (res == NULL)
845 return -1;
846 ok = PyObject_IsTrue(res);
847 Py_DECREF(res);
848 return ok;
849}
Fred Drake13634cf2000-06-29 19:17:04 +0000850
851/* Set of hash utility functions to help maintaining the invariant that
852 iff a==b then hash(a)==hash(b)
853
854 All the utility functions (_Py_Hash*()) return "-1" to signify an error.
855*/
856
857long
Fred Drake100814d2000-07-09 15:48:49 +0000858_Py_HashDouble(double v)
Fred Drake13634cf2000-06-29 19:17:04 +0000859{
Tim Peters39dce292000-08-15 03:34:48 +0000860 double intpart, fractpart;
861 int expo;
862 long hipart;
863 long x; /* the final hash value */
864 /* This is designed so that Python numbers of different types
865 * that compare equal hash to the same value; otherwise comparisons
866 * of mapping keys will turn out weird.
867 */
868
869#ifdef MPW /* MPW C modf expects pointer to extended as second argument */
870{
871 extended e;
872 fractpart = modf(v, &e);
873 intpart = e;
874}
875#else
876 fractpart = modf(v, &intpart);
877#endif
878 if (fractpart == 0.0) {
879 /* This must return the same hash as an equal int or long. */
880 if (intpart > LONG_MAX || -intpart > LONG_MAX) {
881 /* Convert to long and use its hash. */
882 PyObject *plong; /* converted to Python long */
883 if (Py_IS_INFINITY(intpart))
884 /* can't convert to long int -- arbitrary */
885 v = v < 0 ? -271828.0 : 314159.0;
886 plong = PyLong_FromDouble(v);
887 if (plong == NULL)
888 return -1;
889 x = PyObject_Hash(plong);
890 Py_DECREF(plong);
891 return x;
892 }
893 /* Fits in a C long == a Python int, so is its own hash. */
894 x = (long)intpart;
895 if (x == -1)
896 x = -2;
897 return x;
898 }
899 /* The fractional part is non-zero, so we don't have to worry about
900 * making this match the hash of some other type.
901 * Use frexp to get at the bits in the double.
Fred Drake13634cf2000-06-29 19:17:04 +0000902 * Since the VAX D double format has 56 mantissa bits, which is the
903 * most of any double format in use, each of these parts may have as
904 * many as (but no more than) 56 significant bits.
Tim Peters39dce292000-08-15 03:34:48 +0000905 * So, assuming sizeof(long) >= 4, each part can be broken into two
906 * longs; frexp and multiplication are used to do that.
907 * Also, since the Cray double format has 15 exponent bits, which is
908 * the most of any double format in use, shifting the exponent field
909 * left by 15 won't overflow a long (again assuming sizeof(long) >= 4).
Fred Drake13634cf2000-06-29 19:17:04 +0000910 */
Tim Peters39dce292000-08-15 03:34:48 +0000911 v = frexp(v, &expo);
912 v *= 2147483648.0; /* 2**31 */
913 hipart = (long)v; /* take the top 32 bits */
914 v = (v - (double)hipart) * 2147483648.0; /* get the next 32 bits */
915 x = hipart + (long)v + (expo << 15);
916 if (x == -1)
917 x = -2;
918 return x;
Fred Drake13634cf2000-06-29 19:17:04 +0000919}
920
921long
Fred Drake100814d2000-07-09 15:48:49 +0000922_Py_HashPointer(void *p)
Fred Drake13634cf2000-06-29 19:17:04 +0000923{
924#if SIZEOF_LONG >= SIZEOF_VOID_P
925 return (long)p;
926#else
927 /* convert to a Python long and hash that */
928 PyObject* longobj;
929 long x;
930
931 if ((longobj = PyLong_FromVoidPtr(p)) == NULL) {
932 x = -1;
933 goto finally;
934 }
935 x = PyObject_Hash(longobj);
936
937finally:
938 Py_XDECREF(longobj);
939 return x;
940#endif
941}
942
943
Guido van Rossum9bfef441993-03-29 10:43:31 +0000944long
Fred Drake100814d2000-07-09 15:48:49 +0000945PyObject_Hash(PyObject *v)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000946{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000947 PyTypeObject *tp = v->ob_type;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000948 if (tp->tp_hash != NULL)
949 return (*tp->tp_hash)(v);
Guido van Rossumd1f06b92001-01-24 22:14:43 +0000950 if (tp->tp_compare == NULL && RICHCOMPARE(tp) == NULL) {
Fred Drake13634cf2000-06-29 19:17:04 +0000951 return _Py_HashPointer(v); /* Use address as hash value */
952 }
Guido van Rossum9bfef441993-03-29 10:43:31 +0000953 /* If there's a cmp but no hash defined, the object can't be hashed */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000954 PyErr_SetString(PyExc_TypeError, "unhashable type");
Guido van Rossum9bfef441993-03-29 10:43:31 +0000955 return -1;
956}
957
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000958PyObject *
Fred Drake100814d2000-07-09 15:48:49 +0000959PyObject_GetAttrString(PyObject *v, char *name)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000960{
Guido van Rossumd8eb1b31996-08-09 20:52:03 +0000961 if (v->ob_type->tp_getattro != NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000962 PyObject *w, *res;
Guido van Rossumc6d06701997-01-18 07:57:16 +0000963 w = PyString_InternFromString(name);
Guido van Rossumd8eb1b31996-08-09 20:52:03 +0000964 if (w == NULL)
965 return NULL;
966 res = (*v->ob_type->tp_getattro)(v, w);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000967 Py_XDECREF(w);
Guido van Rossumd8eb1b31996-08-09 20:52:03 +0000968 return res;
969 }
970
Guido van Rossum3f5da241990-12-20 15:06:42 +0000971 if (v->ob_type->tp_getattr == NULL) {
Guido van Rossumdb935161998-01-19 22:16:36 +0000972 PyErr_Format(PyExc_AttributeError,
973 "'%.50s' object has no attribute '%.400s'",
974 v->ob_type->tp_name,
975 name);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000976 return NULL;
977 }
978 else {
979 return (*v->ob_type->tp_getattr)(v, name);
980 }
981}
982
983int
Fred Drake100814d2000-07-09 15:48:49 +0000984PyObject_HasAttrString(PyObject *v, char *name)
Guido van Rossumed18fdc1993-07-11 19:55:34 +0000985{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000986 PyObject *res = PyObject_GetAttrString(v, name);
Guido van Rossumed18fdc1993-07-11 19:55:34 +0000987 if (res != NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000988 Py_DECREF(res);
Guido van Rossumed18fdc1993-07-11 19:55:34 +0000989 return 1;
990 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000991 PyErr_Clear();
Guido van Rossumed18fdc1993-07-11 19:55:34 +0000992 return 0;
993}
994
995int
Fred Drake100814d2000-07-09 15:48:49 +0000996PyObject_SetAttrString(PyObject *v, char *name, PyObject *w)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000997{
Guido van Rossumd8eb1b31996-08-09 20:52:03 +0000998 if (v->ob_type->tp_setattro != NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000999 PyObject *s;
Guido van Rossumd8eb1b31996-08-09 20:52:03 +00001000 int res;
Guido van Rossumc6d06701997-01-18 07:57:16 +00001001 s = PyString_InternFromString(name);
Guido van Rossumd8eb1b31996-08-09 20:52:03 +00001002 if (s == NULL)
Guido van Rossumb7fc3041996-09-11 22:51:25 +00001003 return -1;
Guido van Rossumd8eb1b31996-08-09 20:52:03 +00001004 res = (*v->ob_type->tp_setattro)(v, s, w);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001005 Py_XDECREF(s);
Guido van Rossumd8eb1b31996-08-09 20:52:03 +00001006 return res;
1007 }
1008
Guido van Rossum3f5da241990-12-20 15:06:42 +00001009 if (v->ob_type->tp_setattr == NULL) {
1010 if (v->ob_type->tp_getattr == NULL)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001011 PyErr_SetString(PyExc_TypeError,
Guido van Rossum3ea74121991-12-24 13:28:03 +00001012 "attribute-less object (assign or del)");
Guido van Rossum3f5da241990-12-20 15:06:42 +00001013 else
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001014 PyErr_SetString(PyExc_TypeError,
Guido van Rossum3ea74121991-12-24 13:28:03 +00001015 "object has read-only attributes");
Guido van Rossum73531a31990-12-20 23:12:40 +00001016 return -1;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001017 }
1018 else {
1019 return (*v->ob_type->tp_setattr)(v, name, w);
1020 }
1021}
1022
Marc-André Lemburge44e5072000-09-18 16:20:57 +00001023/* Internal API needed by PyObject_GetAttr(): */
1024extern
1025PyObject *_PyUnicode_AsDefaultEncodedString(PyObject *unicode,
1026 const char *errors);
1027
Guido van Rossum98ff96a1997-05-20 18:34:44 +00001028PyObject *
Fred Drake100814d2000-07-09 15:48:49 +00001029PyObject_GetAttr(PyObject *v, PyObject *name)
Guido van Rossum98ff96a1997-05-20 18:34:44 +00001030{
Marc-André Lemburge44e5072000-09-18 16:20:57 +00001031 /* The Unicode to string conversion is done here because the
1032 existing tp_getattro slots expect a string object as name
1033 and we wouldn't want to break those. */
1034 if (PyUnicode_Check(name)) {
1035 name = _PyUnicode_AsDefaultEncodedString(name, NULL);
1036 if (name == NULL)
1037 return NULL;
1038 }
Jeremy Hylton99a8f902000-06-23 14:36:32 +00001039
1040 if (!PyString_Check(name)) {
1041 PyErr_SetString(PyExc_TypeError,
1042 "attribute name must be string");
1043 return NULL;
1044 }
Marc-André Lemburge44e5072000-09-18 16:20:57 +00001045 if (v->ob_type->tp_getattro != NULL)
1046 return (*v->ob_type->tp_getattro)(v, name);
1047 else
Jeremy Hylton99a8f902000-06-23 14:36:32 +00001048 return PyObject_GetAttrString(v, PyString_AS_STRING(name));
Guido van Rossum98ff96a1997-05-20 18:34:44 +00001049}
1050
1051int
Fred Drake100814d2000-07-09 15:48:49 +00001052PyObject_HasAttr(PyObject *v, PyObject *name)
Guido van Rossum98ff96a1997-05-20 18:34:44 +00001053{
1054 PyObject *res = PyObject_GetAttr(v, name);
1055 if (res != NULL) {
1056 Py_DECREF(res);
1057 return 1;
1058 }
1059 PyErr_Clear();
1060 return 0;
1061}
1062
1063int
Fred Drake100814d2000-07-09 15:48:49 +00001064PyObject_SetAttr(PyObject *v, PyObject *name, PyObject *value)
Guido van Rossum98ff96a1997-05-20 18:34:44 +00001065{
1066 int err;
Marc-André Lemburge44e5072000-09-18 16:20:57 +00001067
1068 /* The Unicode to string conversion is done here because the
1069 existing tp_setattro slots expect a string object as name
1070 and we wouldn't want to break those. */
1071 if (PyUnicode_Check(name)) {
1072 name = PyUnicode_AsEncodedString(name, NULL, NULL);
1073 if (name == NULL)
1074 return -1;
Jeremy Hylton99a8f902000-06-23 14:36:32 +00001075 }
Marc-André Lemburge44e5072000-09-18 16:20:57 +00001076 else
1077 Py_INCREF(name);
1078
1079 if (!PyString_Check(name)){
Jeremy Hylton99a8f902000-06-23 14:36:32 +00001080 PyErr_SetString(PyExc_TypeError,
1081 "attribute name must be string");
1082 err = -1;
1083 }
Marc-André Lemburge44e5072000-09-18 16:20:57 +00001084 else {
1085 PyString_InternInPlace(&name);
1086 if (v->ob_type->tp_setattro != NULL)
1087 err = (*v->ob_type->tp_setattro)(v, name, value);
1088 else
1089 err = PyObject_SetAttrString(v,
1090 PyString_AS_STRING(name), value);
1091 }
1092
Guido van Rossum98ff96a1997-05-20 18:34:44 +00001093 Py_DECREF(name);
1094 return err;
1095}
1096
Guido van Rossum6ac258d1993-05-12 08:24:20 +00001097/* Test a value used as condition, e.g., in a for or if statement.
1098 Return -1 if an error occurred */
1099
1100int
Fred Drake100814d2000-07-09 15:48:49 +00001101PyObject_IsTrue(PyObject *v)
Guido van Rossum6ac258d1993-05-12 08:24:20 +00001102{
1103 int res;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001104 if (v == Py_None)
Guido van Rossum6ac258d1993-05-12 08:24:20 +00001105 res = 0;
Guido van Rossum1c4f4581998-05-22 00:53:24 +00001106 else if (v->ob_type->tp_as_number != NULL &&
1107 v->ob_type->tp_as_number->nb_nonzero != NULL)
Guido van Rossum6ac258d1993-05-12 08:24:20 +00001108 res = (*v->ob_type->tp_as_number->nb_nonzero)(v);
Guido van Rossum1c4f4581998-05-22 00:53:24 +00001109 else if (v->ob_type->tp_as_mapping != NULL &&
1110 v->ob_type->tp_as_mapping->mp_length != NULL)
Guido van Rossum6ac258d1993-05-12 08:24:20 +00001111 res = (*v->ob_type->tp_as_mapping->mp_length)(v);
Guido van Rossum1c4f4581998-05-22 00:53:24 +00001112 else if (v->ob_type->tp_as_sequence != NULL &&
1113 v->ob_type->tp_as_sequence->sq_length != NULL)
Guido van Rossum6ac258d1993-05-12 08:24:20 +00001114 res = (*v->ob_type->tp_as_sequence->sq_length)(v);
1115 else
1116 res = 1;
1117 if (res > 0)
1118 res = 1;
1119 return res;
1120}
1121
Guido van Rossumc3d3f961998-04-09 17:53:59 +00001122/* equivalent of 'not v'
1123 Return -1 if an error occurred */
1124
1125int
Fred Drake100814d2000-07-09 15:48:49 +00001126PyObject_Not(PyObject *v)
Guido van Rossumc3d3f961998-04-09 17:53:59 +00001127{
1128 int res;
1129 res = PyObject_IsTrue(v);
1130 if (res < 0)
1131 return res;
1132 return res == 0;
1133}
1134
Guido van Rossum5524a591995-01-10 15:26:20 +00001135/* Coerce two numeric types to the "larger" one.
1136 Increment the reference count on each argument.
Guido van Rossume797ec12001-01-17 15:24:28 +00001137 Return value:
1138 -1 if an error occurred;
1139 0 if the coercion succeeded (and then the reference counts are increased);
1140 1 if no coercion is possible (and no error is raised).
Guido van Rossum5524a591995-01-10 15:26:20 +00001141*/
Guido van Rossum5524a591995-01-10 15:26:20 +00001142int
Fred Drake100814d2000-07-09 15:48:49 +00001143PyNumber_CoerceEx(PyObject **pv, PyObject **pw)
Guido van Rossum5524a591995-01-10 15:26:20 +00001144{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001145 register PyObject *v = *pv;
1146 register PyObject *w = *pw;
Guido van Rossum5524a591995-01-10 15:26:20 +00001147 int res;
1148
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001149 if (v->ob_type == w->ob_type && !PyInstance_Check(v)) {
1150 Py_INCREF(v);
1151 Py_INCREF(w);
Guido van Rossum5524a591995-01-10 15:26:20 +00001152 return 0;
1153 }
1154 if (v->ob_type->tp_as_number && v->ob_type->tp_as_number->nb_coerce) {
1155 res = (*v->ob_type->tp_as_number->nb_coerce)(pv, pw);
1156 if (res <= 0)
1157 return res;
1158 }
1159 if (w->ob_type->tp_as_number && w->ob_type->tp_as_number->nb_coerce) {
1160 res = (*w->ob_type->tp_as_number->nb_coerce)(pw, pv);
1161 if (res <= 0)
1162 return res;
1163 }
Guido van Rossum242c6421997-11-19 16:03:17 +00001164 return 1;
1165}
1166
Guido van Rossume797ec12001-01-17 15:24:28 +00001167/* Coerce two numeric types to the "larger" one.
1168 Increment the reference count on each argument.
1169 Return -1 and raise an exception if no coercion is possible
1170 (and then no reference count is incremented).
1171*/
Guido van Rossum242c6421997-11-19 16:03:17 +00001172int
Fred Drake100814d2000-07-09 15:48:49 +00001173PyNumber_Coerce(PyObject **pv, PyObject **pw)
Guido van Rossum242c6421997-11-19 16:03:17 +00001174{
1175 int err = PyNumber_CoerceEx(pv, pw);
1176 if (err <= 0)
1177 return err;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001178 PyErr_SetString(PyExc_TypeError, "number coercion failed");
Guido van Rossum5524a591995-01-10 15:26:20 +00001179 return -1;
1180}
1181
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001182
Guido van Rossum49b11fe1995-01-26 00:38:22 +00001183/* Test whether an object can be called */
1184
1185int
Fred Drake100814d2000-07-09 15:48:49 +00001186PyCallable_Check(PyObject *x)
Guido van Rossum49b11fe1995-01-26 00:38:22 +00001187{
1188 if (x == NULL)
1189 return 0;
1190 if (x->ob_type->tp_call != NULL ||
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001191 PyFunction_Check(x) ||
1192 PyMethod_Check(x) ||
1193 PyCFunction_Check(x) ||
1194 PyClass_Check(x))
Guido van Rossum49b11fe1995-01-26 00:38:22 +00001195 return 1;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001196 if (PyInstance_Check(x)) {
1197 PyObject *call = PyObject_GetAttrString(x, "__call__");
Guido van Rossum49b11fe1995-01-26 00:38:22 +00001198 if (call == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001199 PyErr_Clear();
Guido van Rossum49b11fe1995-01-26 00:38:22 +00001200 return 0;
1201 }
1202 /* Could test recursively but don't, for fear of endless
1203 recursion if some joker sets self.__call__ = self */
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001204 Py_DECREF(call);
Guido van Rossum49b11fe1995-01-26 00:38:22 +00001205 return 1;
1206 }
1207 return 0;
1208}
1209
1210
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001211/*
1212NoObject is usable as a non-NULL undefined value, used by the macro None.
1213There is (and should be!) no way to create other objects of this type,
Guido van Rossum3f5da241990-12-20 15:06:42 +00001214so there is exactly one (which is indestructible, by the way).
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001215*/
1216
Guido van Rossum0c182a11992-03-27 17:26:13 +00001217/* ARGSUSED */
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001218static PyObject *
Fred Drake100814d2000-07-09 15:48:49 +00001219none_repr(PyObject *op)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001220{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001221 return PyString_FromString("None");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001222}
1223
Barry Warsaw9bf16442001-01-23 16:24:35 +00001224/* ARGUSED */
1225static void
1226none_dealloc(PyObject* ignore)
1227{
1228 /* This should never get called, but we also don't want to SEGV if
1229 * we accidently decref None out of existance.
1230 */
1231 abort();
1232}
1233
1234
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001235static PyTypeObject PyNothing_Type = {
1236 PyObject_HEAD_INIT(&PyType_Type)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001237 0,
Guido van Rossum3f5da241990-12-20 15:06:42 +00001238 "None",
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001239 0,
1240 0,
Barry Warsaw9bf16442001-01-23 16:24:35 +00001241 (destructor)none_dealloc, /*tp_dealloc*/ /*never called*/
Guido van Rossum7066dd71992-09-17 17:54:56 +00001242 0, /*tp_print*/
Guido van Rossum3f5da241990-12-20 15:06:42 +00001243 0, /*tp_getattr*/
1244 0, /*tp_setattr*/
1245 0, /*tp_compare*/
Guido van Rossum1d5735e1994-08-30 08:27:36 +00001246 (reprfunc)none_repr, /*tp_repr*/
Guido van Rossum3f5da241990-12-20 15:06:42 +00001247 0, /*tp_as_number*/
1248 0, /*tp_as_sequence*/
1249 0, /*tp_as_mapping*/
Guido van Rossum9bfef441993-03-29 10:43:31 +00001250 0, /*tp_hash */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001251};
1252
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001253PyObject _Py_NoneStruct = {
1254 PyObject_HEAD_INIT(&PyNothing_Type)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001255};
1256
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +00001257/* NotImplemented is an object that can be used to signal that an
1258 operation is not implemented for the given type combination. */
1259
1260static PyObject *
1261NotImplemented_repr(PyObject *op)
1262{
1263 return PyString_FromString("NotImplemented");
1264}
1265
1266static PyTypeObject PyNotImplemented_Type = {
1267 PyObject_HEAD_INIT(&PyType_Type)
1268 0,
1269 "NotImplemented",
1270 0,
1271 0,
Barry Warsaw9bf16442001-01-23 16:24:35 +00001272 (destructor)none_dealloc, /*tp_dealloc*/ /*never called*/
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +00001273 0, /*tp_print*/
1274 0, /*tp_getattr*/
1275 0, /*tp_setattr*/
1276 0, /*tp_compare*/
1277 (reprfunc)NotImplemented_repr, /*tp_repr*/
1278 0, /*tp_as_number*/
1279 0, /*tp_as_sequence*/
1280 0, /*tp_as_mapping*/
1281 0, /*tp_hash */
1282};
1283
1284PyObject _Py_NotImplementedStruct = {
1285 PyObject_HEAD_INIT(&PyNotImplemented_Type)
1286};
1287
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001288
Guido van Rossum84a90321996-05-22 16:34:47 +00001289#ifdef Py_TRACE_REFS
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001290
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001291static PyObject refchain = {&refchain, &refchain};
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001292
Guido van Rossumaacdc9d1996-08-12 21:32:12 +00001293void
Fred Drake100814d2000-07-09 15:48:49 +00001294_Py_ResetReferences(void)
Guido van Rossume09fb551997-08-05 02:04:34 +00001295{
1296 refchain._ob_prev = refchain._ob_next = &refchain;
1297 _Py_RefTotal = 0;
1298}
1299
1300void
Fred Drake100814d2000-07-09 15:48:49 +00001301_Py_NewReference(PyObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001302{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001303 _Py_RefTotal++;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001304 op->ob_refcnt = 1;
1305 op->_ob_next = refchain._ob_next;
1306 op->_ob_prev = &refchain;
1307 refchain._ob_next->_ob_prev = op;
1308 refchain._ob_next = op;
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +00001309#ifdef COUNT_ALLOCS
1310 inc_count(op->ob_type);
1311#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001312}
1313
Guido van Rossumaacdc9d1996-08-12 21:32:12 +00001314void
Fred Drake100814d2000-07-09 15:48:49 +00001315_Py_ForgetReference(register PyObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001316{
Guido van Rossumbffd6832000-01-20 22:32:56 +00001317#ifdef SLOW_UNREF_CHECK
Guido van Rossum4c08d552000-03-10 22:55:18 +00001318 register PyObject *p;
Guido van Rossumbffd6832000-01-20 22:32:56 +00001319#endif
Guido van Rossumd7047b31995-01-02 19:07:15 +00001320 if (op->ob_refcnt < 0)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001321 Py_FatalError("UNREF negative refcnt");
Guido van Rossum2e8f6141992-09-03 20:32:55 +00001322 if (op == &refchain ||
Guido van Rossumd7047b31995-01-02 19:07:15 +00001323 op->_ob_prev->_ob_next != op || op->_ob_next->_ob_prev != op)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001324 Py_FatalError("UNREF invalid object");
Guido van Rossum2e8f6141992-09-03 20:32:55 +00001325#ifdef SLOW_UNREF_CHECK
Guido van Rossum3f5da241990-12-20 15:06:42 +00001326 for (p = refchain._ob_next; p != &refchain; p = p->_ob_next) {
1327 if (p == op)
1328 break;
1329 }
Guido van Rossumd7047b31995-01-02 19:07:15 +00001330 if (p == &refchain) /* Not found */
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001331 Py_FatalError("UNREF unknown object");
Guido van Rossum2e8f6141992-09-03 20:32:55 +00001332#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001333 op->_ob_next->_ob_prev = op->_ob_prev;
1334 op->_ob_prev->_ob_next = op->_ob_next;
Guido van Rossum2e8f6141992-09-03 20:32:55 +00001335 op->_ob_next = op->_ob_prev = NULL;
Guido van Rossumd8953cb1995-04-06 14:46:26 +00001336#ifdef COUNT_ALLOCS
1337 op->ob_type->tp_free++;
1338#endif
Guido van Rossum3f5da241990-12-20 15:06:42 +00001339}
1340
Guido van Rossumaacdc9d1996-08-12 21:32:12 +00001341void
Fred Drake100814d2000-07-09 15:48:49 +00001342_Py_Dealloc(PyObject *op)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001343{
Guido van Rossum9776adf1994-09-07 14:36:45 +00001344 destructor dealloc = op->ob_type->tp_dealloc;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001345 _Py_ForgetReference(op);
Guido van Rossum9776adf1994-09-07 14:36:45 +00001346 (*dealloc)(op);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001347}
1348
Guido van Rossumaacdc9d1996-08-12 21:32:12 +00001349void
Fred Drake100814d2000-07-09 15:48:49 +00001350_Py_PrintReferences(FILE *fp)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001351{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001352 PyObject *op;
Guido van Rossume09fb551997-08-05 02:04:34 +00001353 fprintf(fp, "Remaining objects:\n");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001354 for (op = refchain._ob_next; op != &refchain; op = op->_ob_next) {
1355 fprintf(fp, "[%d] ", op->ob_refcnt);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001356 if (PyObject_Print(op, fp, 0) != 0)
1357 PyErr_Clear();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001358 putc('\n', fp);
1359 }
1360}
1361
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +00001362PyObject *
Fred Drake100814d2000-07-09 15:48:49 +00001363_Py_GetObjects(PyObject *self, PyObject *args)
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +00001364{
1365 int i, n;
1366 PyObject *t = NULL;
1367 PyObject *res, *op;
1368
1369 if (!PyArg_ParseTuple(args, "i|O", &n, &t))
1370 return NULL;
1371 op = refchain._ob_next;
1372 res = PyList_New(0);
1373 if (res == NULL)
1374 return NULL;
1375 for (i = 0; (n == 0 || i < n) && op != &refchain; i++) {
1376 while (op == self || op == args || op == res || op == t ||
1377 t != NULL && op->ob_type != (PyTypeObject *) t) {
1378 op = op->_ob_next;
1379 if (op == &refchain)
1380 return res;
1381 }
1382 if (PyList_Append(res, op) < 0) {
1383 Py_DECREF(res);
1384 return NULL;
1385 }
1386 op = op->_ob_next;
1387 }
1388 return res;
1389}
1390
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001391#endif
Guido van Rossum97ead3f1996-01-12 01:24:09 +00001392
1393
1394/* Hack to force loading of cobject.o */
Guido van Rossumda9c2711996-12-05 21:58:58 +00001395PyTypeObject *_Py_cobject_hack = &PyCObject_Type;
Guido van Rossum84a90321996-05-22 16:34:47 +00001396
1397
1398/* Hack to force loading of abstract.o */
Jeremy Hylton6253f832000-07-12 12:56:19 +00001399int (*_Py_abstract_hack)(PyObject *) = &PyObject_Size;
Guido van Rossume09fb551997-08-05 02:04:34 +00001400
1401
Andrew M. Kuchling1582a3a2000-08-16 12:27:23 +00001402/* Python's malloc wrappers (see pymem.h) */
Guido van Rossume09fb551997-08-05 02:04:34 +00001403
Thomas Wouters334fb892000-07-25 12:56:38 +00001404void *
Fred Drake100814d2000-07-09 15:48:49 +00001405PyMem_Malloc(size_t nbytes)
Guido van Rossume09fb551997-08-05 02:04:34 +00001406{
1407#if _PyMem_EXTRA > 0
1408 if (nbytes == 0)
1409 nbytes = _PyMem_EXTRA;
1410#endif
Guido van Rossumb18618d2000-05-03 23:44:39 +00001411 return PyMem_MALLOC(nbytes);
Guido van Rossume09fb551997-08-05 02:04:34 +00001412}
1413
Thomas Wouters334fb892000-07-25 12:56:38 +00001414void *
1415PyMem_Realloc(void *p, size_t nbytes)
Guido van Rossume09fb551997-08-05 02:04:34 +00001416{
1417#if _PyMem_EXTRA > 0
1418 if (nbytes == 0)
1419 nbytes = _PyMem_EXTRA;
1420#endif
Guido van Rossumb18618d2000-05-03 23:44:39 +00001421 return PyMem_REALLOC(p, nbytes);
Guido van Rossume09fb551997-08-05 02:04:34 +00001422}
1423
1424void
Thomas Wouters334fb892000-07-25 12:56:38 +00001425PyMem_Free(void *p)
Guido van Rossume09fb551997-08-05 02:04:34 +00001426{
Guido van Rossumb18618d2000-05-03 23:44:39 +00001427 PyMem_FREE(p);
1428}
1429
1430
1431/* Python's object malloc wrappers (see objimpl.h) */
1432
Thomas Wouters334fb892000-07-25 12:56:38 +00001433void *
Fred Drake100814d2000-07-09 15:48:49 +00001434PyObject_Malloc(size_t nbytes)
Guido van Rossumb18618d2000-05-03 23:44:39 +00001435{
1436 return PyObject_MALLOC(nbytes);
1437}
1438
Thomas Wouters334fb892000-07-25 12:56:38 +00001439void *
1440PyObject_Realloc(void *p, size_t nbytes)
Guido van Rossumb18618d2000-05-03 23:44:39 +00001441{
1442 return PyObject_REALLOC(p, nbytes);
1443}
1444
1445void
Thomas Wouters334fb892000-07-25 12:56:38 +00001446PyObject_Free(void *p)
Guido van Rossumb18618d2000-05-03 23:44:39 +00001447{
1448 PyObject_FREE(p);
Guido van Rossume09fb551997-08-05 02:04:34 +00001449}
Guido van Rossum86610361998-04-10 22:32:46 +00001450
1451
Fred Drake41deb1e2001-02-01 05:27:45 +00001452/* Hook to clear up weak references only once the _weakref module is
1453 imported. We use a dummy implementation to simplify the code at each
1454 call site instead of requiring a test for NULL.
1455*/
1456
Fred Drakeb60654b2001-02-26 18:56:37 +00001457static void
Fred Drake41deb1e2001-02-01 05:27:45 +00001458empty_clear_weak_refs(PyObject *o)
1459{
Fred Drakeb60654b2001-02-26 18:56:37 +00001460 return;
Fred Drake41deb1e2001-02-01 05:27:45 +00001461}
1462
Fred Drakeb60654b2001-02-26 18:56:37 +00001463void (*PyObject_ClearWeakRefs)(PyObject *) = empty_clear_weak_refs;
Fred Drake41deb1e2001-02-01 05:27:45 +00001464
1465
1466
Guido van Rossum86610361998-04-10 22:32:46 +00001467/* These methods are used to control infinite recursion in repr, str, print,
1468 etc. Container objects that may recursively contain themselves,
1469 e.g. builtin dictionaries and lists, should used Py_ReprEnter() and
1470 Py_ReprLeave() to avoid infinite recursion.
1471
1472 Py_ReprEnter() returns 0 the first time it is called for a particular
1473 object and 1 every time thereafter. It returns -1 if an exception
1474 occurred. Py_ReprLeave() has no return value.
1475
1476 See dictobject.c and listobject.c for examples of use.
1477*/
1478
1479#define KEY "Py_Repr"
1480
1481int
Fred Drake100814d2000-07-09 15:48:49 +00001482Py_ReprEnter(PyObject *obj)
Guido van Rossum86610361998-04-10 22:32:46 +00001483{
1484 PyObject *dict;
1485 PyObject *list;
1486 int i;
1487
1488 dict = PyThreadState_GetDict();
1489 if (dict == NULL)
1490 return -1;
1491 list = PyDict_GetItemString(dict, KEY);
1492 if (list == NULL) {
1493 list = PyList_New(0);
1494 if (list == NULL)
1495 return -1;
1496 if (PyDict_SetItemString(dict, KEY, list) < 0)
1497 return -1;
1498 Py_DECREF(list);
1499 }
1500 i = PyList_GET_SIZE(list);
1501 while (--i >= 0) {
1502 if (PyList_GET_ITEM(list, i) == obj)
1503 return 1;
1504 }
1505 PyList_Append(list, obj);
1506 return 0;
1507}
1508
1509void
Fred Drake100814d2000-07-09 15:48:49 +00001510Py_ReprLeave(PyObject *obj)
Guido van Rossum86610361998-04-10 22:32:46 +00001511{
1512 PyObject *dict;
1513 PyObject *list;
1514 int i;
1515
1516 dict = PyThreadState_GetDict();
Guido van Rossumeb909461998-04-11 15:17:34 +00001517 if (dict == NULL)
1518 return;
Guido van Rossum86610361998-04-10 22:32:46 +00001519 list = PyDict_GetItemString(dict, KEY);
Guido van Rossumeb909461998-04-11 15:17:34 +00001520 if (list == NULL || !PyList_Check(list))
1521 return;
Guido van Rossum86610361998-04-10 22:32:46 +00001522 i = PyList_GET_SIZE(list);
1523 /* Count backwards because we always expect obj to be list[-1] */
1524 while (--i >= 0) {
1525 if (PyList_GET_ITEM(list, i) == obj) {
1526 PyList_SetSlice(list, i, i + 1, NULL);
1527 break;
1528 }
1529 }
1530}
Guido van Rossumd724b232000-03-13 16:01:29 +00001531
1532/*
1533 trashcan
1534 CT 2k0130
1535 non-recursively destroy nested objects
1536
1537 CT 2k0223
1538 everything is now done in a macro.
1539
1540 CT 2k0305
1541 modified to use functions, after Tim Peter's suggestion.
1542
1543 CT 2k0309
1544 modified to restore a possible error.
Guido van Rossum13ff8eb2000-03-25 18:39:19 +00001545
1546 CT 2k0325
1547 added better safe than sorry check for threadstate
Guido van Rossume92e6102000-04-24 15:40:53 +00001548
1549 CT 2k0422
1550 complete rewrite. We now build a chain via ob_type
1551 and save the limited number of types in ob_refcnt.
1552 This is perfect since we don't need any memory.
1553 A patch for free-threading would need just a lock.
Guido van Rossumd724b232000-03-13 16:01:29 +00001554*/
1555
Guido van Rossume92e6102000-04-24 15:40:53 +00001556#define Py_TRASHCAN_TUPLE 1
1557#define Py_TRASHCAN_LIST 2
1558#define Py_TRASHCAN_DICT 3
1559#define Py_TRASHCAN_FRAME 4
1560#define Py_TRASHCAN_TRACEBACK 5
1561/* extend here if other objects want protection */
1562
Guido van Rossumd724b232000-03-13 16:01:29 +00001563int _PyTrash_delete_nesting = 0;
Guido van Rossume92e6102000-04-24 15:40:53 +00001564
Guido van Rossumd724b232000-03-13 16:01:29 +00001565PyObject * _PyTrash_delete_later = NULL;
1566
1567void
Fred Drake100814d2000-07-09 15:48:49 +00001568_PyTrash_deposit_object(PyObject *op)
Guido van Rossumd724b232000-03-13 16:01:29 +00001569{
Guido van Rossume92e6102000-04-24 15:40:53 +00001570 int typecode;
Guido van Rossum13ff8eb2000-03-25 18:39:19 +00001571
Guido van Rossume92e6102000-04-24 15:40:53 +00001572 if (PyTuple_Check(op))
1573 typecode = Py_TRASHCAN_TUPLE;
1574 else if (PyList_Check(op))
1575 typecode = Py_TRASHCAN_LIST;
1576 else if (PyDict_Check(op))
1577 typecode = Py_TRASHCAN_DICT;
1578 else if (PyFrame_Check(op))
1579 typecode = Py_TRASHCAN_FRAME;
1580 else if (PyTraceBack_Check(op))
1581 typecode = Py_TRASHCAN_TRACEBACK;
Moshe Zadkacf703f02000-08-04 15:36:13 +00001582 else /* We have a bug here -- those are the only types in GC */ {
1583 Py_FatalError("Type not supported in GC -- internal bug");
1584 return; /* pacify compiler -- execution never here */
1585 }
Guido van Rossume92e6102000-04-24 15:40:53 +00001586 op->ob_refcnt = typecode;
Guido van Rossumd724b232000-03-13 16:01:29 +00001587
Guido van Rossume92e6102000-04-24 15:40:53 +00001588 op->ob_type = (PyTypeObject*)_PyTrash_delete_later;
1589 _PyTrash_delete_later = op;
Guido van Rossumd724b232000-03-13 16:01:29 +00001590}
1591
1592void
Fred Drake100814d2000-07-09 15:48:49 +00001593_PyTrash_destroy_chain(void)
Guido van Rossumd724b232000-03-13 16:01:29 +00001594{
1595 while (_PyTrash_delete_later) {
1596 PyObject *shredder = _PyTrash_delete_later;
Guido van Rossume92e6102000-04-24 15:40:53 +00001597 _PyTrash_delete_later = (PyObject*) shredder->ob_type;
1598
1599 switch (shredder->ob_refcnt) {
1600 case Py_TRASHCAN_TUPLE:
1601 shredder->ob_type = &PyTuple_Type;
1602 break;
1603 case Py_TRASHCAN_LIST:
1604 shredder->ob_type = &PyList_Type;
1605 break;
1606 case Py_TRASHCAN_DICT:
1607 shredder->ob_type = &PyDict_Type;
1608 break;
1609 case Py_TRASHCAN_FRAME:
1610 shredder->ob_type = &PyFrame_Type;
1611 break;
1612 case Py_TRASHCAN_TRACEBACK:
1613 shredder->ob_type = &PyTraceBack_Type;
1614 break;
1615 }
1616 _Py_NewReference(shredder);
1617
Guido van Rossumd724b232000-03-13 16:01:29 +00001618 ++_PyTrash_delete_nesting;
1619 Py_DECREF(shredder);
1620 --_PyTrash_delete_nesting;
1621 }
1622}
Neil Schemenauera35c6882001-02-27 04:45:05 +00001623
1624#ifdef WITH_PYMALLOC
1625#include "obmalloc.c"
1626#endif