blob: b613dba29fafc92486eef16ac7ccdb1b0014980a [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001
Guido van Rossum3f5da241990-12-20 15:06:42 +00002/* Generic object operations; and implementation of None (NoObject) */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003
Guido van Rossumc0b618a1997-05-02 03:12:38 +00004#include "Python.h"
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00005
Jack Jansen28fc8802000-07-11 21:47:20 +00006#ifdef macintosh
7#include "macglue.h"
8#endif
9
Guido van Rossume92e6102000-04-24 15:40:53 +000010/* just for trashcan: */
11#include "compile.h"
12#include "frameobject.h"
13#include "traceback.h"
14
Guido van Rossum6f9e4331995-03-29 16:57:48 +000015#if defined( Py_TRACE_REFS ) || defined( Py_REF_DEBUG )
Guido van Rossumbffd6832000-01-20 22:32:56 +000016DL_IMPORT(long) _Py_RefTotal;
Guido van Rossum3f5da241990-12-20 15:06:42 +000017#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000018
Guido van Rossum3f5da241990-12-20 15:06:42 +000019/* Object allocation routines used by NEWOBJ and NEWVAROBJ macros.
20 These are used by the individual routines for object creation.
21 Do not call them otherwise, they do not initialize the object! */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000022
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +000023#ifdef COUNT_ALLOCS
Guido van Rossumc0b618a1997-05-02 03:12:38 +000024static PyTypeObject *type_list;
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +000025extern int tuple_zero_allocs, fast_tuple_allocs;
26extern int quick_int_allocs, quick_neg_int_allocs;
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +000027extern int null_strings, one_strings;
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +000028void
Fred Drake100814d2000-07-09 15:48:49 +000029dump_counts(void)
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +000030{
Guido van Rossumc0b618a1997-05-02 03:12:38 +000031 PyTypeObject *tp;
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +000032
33 for (tp = type_list; tp; tp = tp->tp_next)
Sjoerd Mullender52c1f511993-10-25 08:40:52 +000034 fprintf(stderr, "%s alloc'd: %d, freed: %d, max in use: %d\n",
35 tp->tp_name, tp->tp_alloc, tp->tp_free,
36 tp->tp_maxalloc);
37 fprintf(stderr, "fast tuple allocs: %d, empty: %d\n",
38 fast_tuple_allocs, tuple_zero_allocs);
39 fprintf(stderr, "fast int allocs: pos: %d, neg: %d\n",
40 quick_int_allocs, quick_neg_int_allocs);
41 fprintf(stderr, "null strings: %d, 1-strings: %d\n",
42 null_strings, one_strings);
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +000043}
44
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +000045PyObject *
Fred Drake100814d2000-07-09 15:48:49 +000046get_counts(void)
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +000047{
48 PyTypeObject *tp;
49 PyObject *result;
50 PyObject *v;
51
52 result = PyList_New(0);
53 if (result == NULL)
54 return NULL;
55 for (tp = type_list; tp; tp = tp->tp_next) {
56 v = Py_BuildValue("(siii)", tp->tp_name, tp->tp_alloc,
57 tp->tp_free, tp->tp_maxalloc);
58 if (v == NULL) {
59 Py_DECREF(result);
60 return NULL;
61 }
62 if (PyList_Append(result, v) < 0) {
63 Py_DECREF(v);
64 Py_DECREF(result);
65 return NULL;
66 }
67 Py_DECREF(v);
68 }
69 return result;
70}
71
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +000072void
Fred Drake100814d2000-07-09 15:48:49 +000073inc_count(PyTypeObject *tp)
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +000074{
75 if (tp->tp_alloc == 0) {
Guido van Rossumd8953cb1995-04-06 14:46:26 +000076 /* first time; insert in linked list */
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +000077 if (tp->tp_next != NULL) /* sanity check */
Guido van Rossumc0b618a1997-05-02 03:12:38 +000078 Py_FatalError("XXX inc_count sanity check");
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +000079 tp->tp_next = type_list;
80 type_list = tp;
81 }
82 tp->tp_alloc++;
83 if (tp->tp_alloc - tp->tp_free > tp->tp_maxalloc)
84 tp->tp_maxalloc = tp->tp_alloc - tp->tp_free;
85}
86#endif
87
Guido van Rossumc0b618a1997-05-02 03:12:38 +000088PyObject *
Fred Drake100814d2000-07-09 15:48:49 +000089PyObject_Init(PyObject *op, PyTypeObject *tp)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000090{
Guido van Rossumb18618d2000-05-03 23:44:39 +000091 if (op == NULL) {
92 PyErr_SetString(PyExc_SystemError,
93 "NULL object passed to PyObject_Init");
94 return op;
95 }
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +000096#ifdef WITH_CYCLE_GC
97 if (PyType_IS_GC(tp))
98 op = (PyObject *) PyObject_FROM_GC(op);
99#endif
Guido van Rossumb18618d2000-05-03 23:44:39 +0000100 /* Any changes should be reflected in PyObject_INIT (objimpl.h) */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000101 op->ob_type = tp;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000102 _Py_NewReference(op);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000103 return op;
104}
105
Guido van Rossumb18618d2000-05-03 23:44:39 +0000106PyVarObject *
Fred Drake100814d2000-07-09 15:48:49 +0000107PyObject_InitVar(PyVarObject *op, PyTypeObject *tp, int size)
Guido van Rossumb18618d2000-05-03 23:44:39 +0000108{
109 if (op == NULL) {
110 PyErr_SetString(PyExc_SystemError,
111 "NULL object passed to PyObject_InitVar");
112 return op;
113 }
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000114#ifdef WITH_CYCLE_GC
115 if (PyType_IS_GC(tp))
116 op = (PyVarObject *) PyObject_FROM_GC(op);
117#endif
Guido van Rossumb18618d2000-05-03 23:44:39 +0000118 /* Any changes should be reflected in PyObject_INIT_VAR */
119 op->ob_size = size;
120 op->ob_type = tp;
121 _Py_NewReference((PyObject *)op);
122 return op;
123}
124
125PyObject *
Fred Drake100814d2000-07-09 15:48:49 +0000126_PyObject_New(PyTypeObject *tp)
Guido van Rossumb18618d2000-05-03 23:44:39 +0000127{
128 PyObject *op;
129 op = (PyObject *) PyObject_MALLOC(_PyObject_SIZE(tp));
130 if (op == NULL)
131 return PyErr_NoMemory();
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000132#ifdef WITH_CYCLE_GC
133 if (PyType_IS_GC(tp))
134 op = (PyObject *) PyObject_FROM_GC(op);
135#endif
Guido van Rossumb18618d2000-05-03 23:44:39 +0000136 return PyObject_INIT(op, tp);
137}
138
Guido van Rossumd0c87ee1997-05-15 21:31:03 +0000139PyVarObject *
Fred Drake100814d2000-07-09 15:48:49 +0000140_PyObject_NewVar(PyTypeObject *tp, int size)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000141{
Guido van Rossumb18618d2000-05-03 23:44:39 +0000142 PyVarObject *op;
143 op = (PyVarObject *) PyObject_MALLOC(_PyObject_VAR_SIZE(tp, size));
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000144 if (op == NULL)
Guido van Rossumd0c87ee1997-05-15 21:31:03 +0000145 return (PyVarObject *)PyErr_NoMemory();
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000146#ifdef WITH_CYCLE_GC
147 if (PyType_IS_GC(tp))
148 op = (PyVarObject *) PyObject_FROM_GC(op);
149#endif
Guido van Rossumb18618d2000-05-03 23:44:39 +0000150 return PyObject_INIT_VAR(op, tp, size);
151}
152
153void
Fred Drake100814d2000-07-09 15:48:49 +0000154_PyObject_Del(PyObject *op)
Guido van Rossumb18618d2000-05-03 23:44:39 +0000155{
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000156#ifdef WITH_CYCLE_GC
Guido van Rossum4cc6ac72000-07-01 01:00:38 +0000157 if (op && PyType_IS_GC(op->ob_type)) {
158 op = (PyObject *) PyObject_AS_GC(op);
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000159 }
Guido van Rossum4cc6ac72000-07-01 01:00:38 +0000160#endif
161 PyObject_FREE(op);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000162}
163
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000164#ifndef WITH_CYCLE_GC
165/* extension modules might need these */
166void _PyGC_Insert(PyObject *op) { }
167void _PyGC_Remove(PyObject *op) { }
168#endif
169
Guido van Rossum90933611991-06-07 16:10:43 +0000170int
Fred Drake100814d2000-07-09 15:48:49 +0000171PyObject_Print(PyObject *op, FILE *fp, int flags)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000172{
Guido van Rossum278ef591991-07-27 21:40:24 +0000173 int ret = 0;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000174 if (PyErr_CheckSignals())
Guido van Rossum90933611991-06-07 16:10:43 +0000175 return -1;
Guido van Rossum9b00dfa1998-04-28 16:06:54 +0000176#ifdef USE_STACKCHECK
177 if (PyOS_CheckStack()) {
Fred Drake661ea262000-10-24 19:57:45 +0000178 PyErr_SetString(PyExc_MemoryError, "stack overflow");
Guido van Rossum9b00dfa1998-04-28 16:06:54 +0000179 return -1;
180 }
181#endif
Guido van Rossum687ef6e2000-01-12 16:28:58 +0000182 clearerr(fp); /* Clear any previous error condition */
Guido van Rossum90933611991-06-07 16:10:43 +0000183 if (op == NULL) {
184 fprintf(fp, "<nil>");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000185 }
Guido van Rossum90933611991-06-07 16:10:43 +0000186 else {
187 if (op->ob_refcnt <= 0)
Fred Drakea44d3532000-06-30 15:01:00 +0000188 fprintf(fp, "<refcnt %u at %p>",
189 op->ob_refcnt, op);
Guido van Rossum2e8f6141992-09-03 20:32:55 +0000190 else if (op->ob_type->tp_print == NULL) {
191 if (op->ob_type->tp_repr == NULL) {
Fred Drakea44d3532000-06-30 15:01:00 +0000192 fprintf(fp, "<%s object at %p>",
193 op->ob_type->tp_name, op);
Guido van Rossum2e8f6141992-09-03 20:32:55 +0000194 }
195 else {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000196 PyObject *s;
197 if (flags & Py_PRINT_RAW)
198 s = PyObject_Str(op);
Guido van Rossumc6004111993-11-05 10:22:19 +0000199 else
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000200 s = PyObject_Repr(op);
Guido van Rossum2e8f6141992-09-03 20:32:55 +0000201 if (s == NULL)
202 ret = -1;
Guido van Rossum2e8f6141992-09-03 20:32:55 +0000203 else {
Guido van Rossum565798d1998-04-21 22:25:01 +0000204 ret = PyObject_Print(s, fp,
205 Py_PRINT_RAW);
Guido van Rossum2e8f6141992-09-03 20:32:55 +0000206 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000207 Py_XDECREF(s);
Guido van Rossum2e8f6141992-09-03 20:32:55 +0000208 }
209 }
Guido van Rossum90933611991-06-07 16:10:43 +0000210 else
Guido van Rossum278ef591991-07-27 21:40:24 +0000211 ret = (*op->ob_type->tp_print)(op, fp, flags);
Guido van Rossum90933611991-06-07 16:10:43 +0000212 }
Guido van Rossum278ef591991-07-27 21:40:24 +0000213 if (ret == 0) {
214 if (ferror(fp)) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000215 PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossum278ef591991-07-27 21:40:24 +0000216 clearerr(fp);
217 ret = -1;
218 }
219 }
220 return ret;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000221}
222
Barry Warsaw9bf16442001-01-23 16:24:35 +0000223/* For debugging convenience. See Misc/gdbinit for some useful gdb hooks */
224void PyObject_Dump(PyObject* op)
225{
226 (void)PyObject_Print(op, stderr, 0);
227 fprintf(stderr, "\nrefcounts: %d\n", op->ob_refcnt);
Barry Warsaw903138f2001-01-23 16:33:18 +0000228 fprintf(stderr, "address : %p\n", op);
Barry Warsaw9bf16442001-01-23 16:24:35 +0000229}
Barry Warsaw903138f2001-01-23 16:33:18 +0000230
231#ifdef WITH_CYCLE_GC
Barry Warsaw9bf16442001-01-23 16:24:35 +0000232void PyGC_Dump(PyGC_Head* op)
233{
234 PyObject_Dump(PyObject_FROM_GC(op));
235}
Barry Warsaw903138f2001-01-23 16:33:18 +0000236#endif /* WITH_CYCLE_GC */
Barry Warsaw9bf16442001-01-23 16:24:35 +0000237
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000238PyObject *
Fred Drake100814d2000-07-09 15:48:49 +0000239PyObject_Repr(PyObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000240{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000241 if (PyErr_CheckSignals())
Guido van Rossum90933611991-06-07 16:10:43 +0000242 return NULL;
Guido van Rossum9b00dfa1998-04-28 16:06:54 +0000243#ifdef USE_STACKCHECK
244 if (PyOS_CheckStack()) {
Fred Drake661ea262000-10-24 19:57:45 +0000245 PyErr_SetString(PyExc_MemoryError, "stack overflow");
Guido van Rossum9b00dfa1998-04-28 16:06:54 +0000246 return NULL;
247 }
248#endif
Guido van Rossum90933611991-06-07 16:10:43 +0000249 if (v == NULL)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000250 return PyString_FromString("<NULL>");
Guido van Rossum90933611991-06-07 16:10:43 +0000251 else if (v->ob_type->tp_repr == NULL) {
252 char buf[120];
Fred Drakea44d3532000-06-30 15:01:00 +0000253 sprintf(buf, "<%.80s object at %p>",
254 v->ob_type->tp_name, v);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000255 return PyString_FromString(buf);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000256 }
Guido van Rossum4c08d552000-03-10 22:55:18 +0000257 else {
258 PyObject *res;
259 res = (*v->ob_type->tp_repr)(v);
260 if (res == NULL)
261 return NULL;
Fredrik Lundhefecc7d2000-07-01 14:31:09 +0000262 if (PyUnicode_Check(res)) {
263 PyObject* str;
Fredrik Lundh2a1e0602000-07-08 17:43:32 +0000264 str = PyUnicode_AsUnicodeEscapeString(res);
Marc-André Lemburg891bc652000-07-03 09:57:53 +0000265 Py_DECREF(res);
266 if (str)
Fredrik Lundhefecc7d2000-07-01 14:31:09 +0000267 res = str;
Marc-André Lemburg891bc652000-07-03 09:57:53 +0000268 else
269 return NULL;
Fredrik Lundhefecc7d2000-07-01 14:31:09 +0000270 }
Guido van Rossum4c08d552000-03-10 22:55:18 +0000271 if (!PyString_Check(res)) {
272 PyErr_Format(PyExc_TypeError,
Guido van Rossum5db862d2000-04-10 12:46:51 +0000273 "__repr__ returned non-string (type %.200s)",
Guido van Rossum4c08d552000-03-10 22:55:18 +0000274 res->ob_type->tp_name);
275 Py_DECREF(res);
276 return NULL;
277 }
278 return res;
279 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000280}
281
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000282PyObject *
Fred Drake100814d2000-07-09 15:48:49 +0000283PyObject_Str(PyObject *v)
Guido van Rossumc6004111993-11-05 10:22:19 +0000284{
Guido van Rossum4c08d552000-03-10 22:55:18 +0000285 PyObject *res;
286
Guido van Rossumc6004111993-11-05 10:22:19 +0000287 if (v == NULL)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000288 return PyString_FromString("<NULL>");
289 else if (PyString_Check(v)) {
290 Py_INCREF(v);
Guido van Rossumc6004111993-11-05 10:22:19 +0000291 return v;
292 }
Guido van Rossum32b582b1995-01-17 16:35:13 +0000293 else if (v->ob_type->tp_str != NULL)
Guido van Rossum4c08d552000-03-10 22:55:18 +0000294 res = (*v->ob_type->tp_str)(v);
Guido van Rossumc6004111993-11-05 10:22:19 +0000295 else {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000296 PyObject *func;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000297 if (!PyInstance_Check(v) ||
298 (func = PyObject_GetAttrString(v, "__str__")) == NULL) {
299 PyErr_Clear();
300 return PyObject_Repr(v);
Guido van Rossumc6004111993-11-05 10:22:19 +0000301 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000302 res = PyEval_CallObject(func, (PyObject *)NULL);
303 Py_DECREF(func);
Guido van Rossumc6004111993-11-05 10:22:19 +0000304 }
Guido van Rossum4c08d552000-03-10 22:55:18 +0000305 if (res == NULL)
306 return NULL;
Marc-André Lemburg891bc652000-07-03 09:57:53 +0000307 if (PyUnicode_Check(res)) {
308 PyObject* str;
309 str = PyUnicode_AsEncodedString(res, NULL, NULL);
310 Py_DECREF(res);
311 if (str)
312 res = str;
313 else
314 return NULL;
315 }
Guido van Rossum4c08d552000-03-10 22:55:18 +0000316 if (!PyString_Check(res)) {
317 PyErr_Format(PyExc_TypeError,
Guido van Rossum5db862d2000-04-10 12:46:51 +0000318 "__str__ returned non-string (type %.200s)",
Guido van Rossum4c08d552000-03-10 22:55:18 +0000319 res->ob_type->tp_name);
320 Py_DECREF(res);
321 return NULL;
322 }
323 return res;
Guido van Rossumc6004111993-11-05 10:22:19 +0000324}
325
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000326PyObject *
327PyObject_Unicode(PyObject *v)
328{
329 PyObject *res;
330
331 if (v == NULL)
332 res = PyString_FromString("<NULL>");
333 else if (PyUnicode_Check(v)) {
334 Py_INCREF(v);
335 return v;
336 }
337 else if (PyString_Check(v))
338 res = v;
339 else if (v->ob_type->tp_str != NULL)
340 res = (*v->ob_type->tp_str)(v);
341 else {
342 PyObject *func;
343 static PyObject *strstr;
344 if (strstr == NULL) {
345 strstr= PyString_InternFromString("__str__");
346 if (strstr == NULL)
347 return NULL;
348 }
349 if (!PyInstance_Check(v) ||
350 (func = PyObject_GetAttr(v, strstr)) == NULL) {
351 PyErr_Clear();
352 res = PyObject_Repr(v);
353 }
354 else {
355 res = PyEval_CallObject(func, (PyObject *)NULL);
356 Py_DECREF(func);
357 }
358 }
359 if (res == NULL)
360 return NULL;
361 if (!PyUnicode_Check(res)) {
362 PyObject* str;
363 str = PyUnicode_FromObject(res);
364 Py_DECREF(res);
365 if (str)
366 res = str;
367 else
368 return NULL;
369 }
370 return res;
371}
372
373
Guido van Rossume797ec12001-01-17 15:24:28 +0000374/* Map rich comparison operators to their swapped version, e.g. LT --> GT */
375static int swapped_op[] = {Py_GT, Py_GE, Py_EQ, Py_NE, Py_LT, Py_LE};
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +0000376
Guido van Rossume797ec12001-01-17 15:24:28 +0000377/* Try a genuine rich comparison, returning an object. Return:
378 NULL for exception;
379 NotImplemented if this particular rich comparison is not implemented or
380 undefined;
381 some object not equal to NotImplemented if it is implemented
382 (this latter object may not be a Boolean).
383*/
384static PyObject *
385try_rich_compare(PyObject *v, PyObject *w, int op)
386{
387 richcmpfunc f;
388 PyObject *res;
389
390 if ((f = v->ob_type->tp_richcompare) != NULL) {
391 res = (*f)(v, w, op);
392 if (res != Py_NotImplemented)
393 return res;
394 Py_DECREF(res);
395 }
396 if ((f = w->ob_type->tp_richcompare) != NULL) {
397 return (*f)(w, v, swapped_op[op]);
398 }
399 res = Py_NotImplemented;
400 Py_INCREF(res);
401 return res;
402}
403
404/* Try a genuine rich comparison, returning an int. Return:
405 -1 for exception (including the case where try_rich_compare() returns an
406 object that's not a Boolean);
407 0 if the outcome is false;
408 1 if the outcome is true;
409 2 if this particular rich comparison is not implemented or undefined.
410*/
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +0000411static int
Guido van Rossume797ec12001-01-17 15:24:28 +0000412try_rich_compare_bool(PyObject *v, PyObject *w, int op)
413{
414 PyObject *res;
415 int ok;
416
417 if (v->ob_type->tp_richcompare == NULL &&
418 w->ob_type->tp_richcompare == NULL)
419 return 2; /* Shortcut, avoid INCREF+DECREF */
420 res = try_rich_compare(v, w, op);
421 if (res == NULL)
422 return -1;
423 if (res == Py_NotImplemented) {
424 Py_DECREF(res);
425 return 2;
426 }
427 ok = PyObject_IsTrue(res);
428 Py_DECREF(res);
429 return ok;
430}
431
432/* Try rich comparisons to determine a 3-way comparison. Return:
433 -2 for an exception;
434 -1 if v < w;
435 0 if v == w;
436 1 if v > w;
437 2 if this particular rich comparison is not implemented or undefined.
438*/
439static int
440try_rich_to_3way_compare(PyObject *v, PyObject *w)
441{
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000442 static struct { int op; int outcome; } tries[3] = {
443 /* Try this operator, and if it is true, use this outcome: */
444 {Py_EQ, 0},
445 {Py_LT, -1},
446 {Py_GT, 1},
447 };
448 int i;
449
Guido van Rossume797ec12001-01-17 15:24:28 +0000450 if (v->ob_type->tp_richcompare == NULL &&
451 w->ob_type->tp_richcompare == NULL)
452 return 2; /* Shortcut */
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000453
454 for (i = 0; i < 3; i++) {
455 switch (try_rich_compare_bool(v, w, tries[i].op)) {
456 case -1:
457 return -1;
458 case 1:
459 return tries[i].outcome;
460 }
Guido van Rossume797ec12001-01-17 15:24:28 +0000461 }
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000462
463 return 2;
Guido van Rossume797ec12001-01-17 15:24:28 +0000464}
465
466/* Try a 3-way comparison, returning an int. Return:
467 -2 for an exception;
468 -1 if v < w;
469 0 if v == w;
470 1 if v > w;
471 2 if this particular 3-way comparison is not implemented or undefined.
472*/
473static int
474try_3way_compare(PyObject *v, PyObject *w)
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +0000475{
476 int c;
Guido van Rossume797ec12001-01-17 15:24:28 +0000477 cmpfunc f;
478
479 /* Comparisons involving instances are given to instance_compare,
480 which has the same return conventions as this function. */
481
482 if (PyInstance_Check(v))
483 return (*v->ob_type->tp_compare)(v, w);
484 if (PyInstance_Check(w))
485 return (*w->ob_type->tp_compare)(v, w);
486
487 /* If the types are equal, don't bother with coercions etc. */
488 if (v->ob_type == w->ob_type) {
489 if ((f = v->ob_type->tp_compare) == NULL)
490 return 2;
491 c = (*f)(v, w);
492 if (PyErr_Occurred())
493 return -2;
494 return c < 0 ? -1 : c > 0 ? 1 : 0;
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +0000495 }
Guido van Rossume797ec12001-01-17 15:24:28 +0000496
497 /* Try coercion; if it fails, give up */
498 c = PyNumber_CoerceEx(&v, &w);
499 if (c < 0)
500 return -2;
501 if (c > 0)
502 return 2;
503
504 /* Try v's comparison, if defined */
505 if ((f = v->ob_type->tp_compare) != NULL) {
506 c = (*f)(v, w);
507 Py_DECREF(v);
508 Py_DECREF(w);
509 if (PyErr_Occurred())
510 return -2;
511 return c < 0 ? -1 : c > 0 ? 1 : 0;
512 }
513
514 /* Try w's comparison, if defined */
515 if ((f = w->ob_type->tp_compare) != NULL) {
516 c = (*f)(w, v); /* swapped! */
517 Py_DECREF(v);
518 Py_DECREF(w);
519 if (PyErr_Occurred())
520 return -2;
521 return c < 0 ? 1 : c > 0 ? -1 : 0; /* negated! */
522 }
523
524 /* No comparison defined */
525 Py_DECREF(v);
526 Py_DECREF(w);
527 return 2;
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +0000528}
529
Guido van Rossume797ec12001-01-17 15:24:28 +0000530/* Final fallback 3-way comparison, returning an int. Return:
531 -2 if an error occurred;
532 -1 if v < w;
533 0 if v == w;
534 1 if v > w.
535*/
536static int
537default_3way_compare(PyObject *v, PyObject *w)
538{
539 int c;
Guido van Rossum8f9143d2001-01-22 15:59:32 +0000540 char *vname, *wname;
Guido van Rossume797ec12001-01-17 15:24:28 +0000541
542 if (v->ob_type == w->ob_type) {
Barry Warsawb0e754d2001-01-20 06:24:55 +0000543 /* When comparing these pointers, they must be cast to
544 * integer types (i.e. Py_uintptr_t, our spelling of C9X's
545 * uintptr_t). ANSI specifies that pointer compares other
546 * than == and != to non-related structures are undefined.
547 */
Barry Warsaw71ff8d52001-01-20 06:08:10 +0000548 Py_uintptr_t vv = (Py_uintptr_t)v;
549 Py_uintptr_t ww = (Py_uintptr_t)w;
Guido van Rossume797ec12001-01-17 15:24:28 +0000550 return (vv < ww) ? -1 : (vv > ww) ? 1 : 0;
551 }
552
553 /* Special case for Unicode */
554 if (PyUnicode_Check(v) || PyUnicode_Check(w)) {
555 c = PyUnicode_Compare(v, w);
556 if (!PyErr_Occurred())
557 return c;
558 /* TypeErrors are ignored: if Unicode coercion fails due
559 to one of the arguments not having the right type, we
560 continue as defined by the coercion protocol (see
561 above). Luckily, decoding errors are reported as
562 ValueErrors and are not masked by this technique. */
563 if (!PyErr_ExceptionMatches(PyExc_TypeError))
564 return -2;
565 PyErr_Clear();
566 }
567
Guido van Rossum0871e932001-01-22 19:28:09 +0000568 /* None is smaller than anything */
569 if (v == Py_None)
570 return -1;
571 if (w == Py_None)
572 return 1;
573
Guido van Rossume797ec12001-01-17 15:24:28 +0000574 /* different type: compare type names */
Guido van Rossum8f9143d2001-01-22 15:59:32 +0000575 if (v->ob_type->tp_as_number)
576 vname = "";
577 else
578 vname = v->ob_type->tp_name;
579 if (w->ob_type->tp_as_number)
580 wname = "";
581 else
582 wname = w->ob_type->tp_name;
583 c = strcmp(vname, wname);
584 if (c < 0)
585 return -1;
586 if (c > 0)
587 return 1;
588 /* Same type name, or (more likely) incomparable numeric types */
589 return ((Py_uintptr_t)(v->ob_type) < (
590 Py_uintptr_t)(w->ob_type)) ? -1 : 1;
Guido van Rossume797ec12001-01-17 15:24:28 +0000591}
592
593#define CHECK_TYPES(o) PyType_HasFeature((o)->ob_type, Py_TPFLAGS_CHECKTYPES)
594
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +0000595static int
Fred Drake100814d2000-07-09 15:48:49 +0000596do_cmp(PyObject *v, PyObject *w)
Guido van Rossum20566841995-01-12 11:26:10 +0000597{
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +0000598 int c;
599
Guido van Rossume797ec12001-01-17 15:24:28 +0000600 c = try_rich_to_3way_compare(v, w);
601 if (c < 2)
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +0000602 return c;
Guido van Rossume797ec12001-01-17 15:24:28 +0000603 c = try_3way_compare(v, w);
604 if (c < 2)
605 return c;
606 return default_3way_compare(v, w);
Guido van Rossum20566841995-01-12 11:26:10 +0000607}
608
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000609/* compare_nesting is incremented before calling compare (for
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000610 some types) and decremented on exit. If the count exceeds the
611 nesting limit, enable code to detect circular data structures.
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000612
613 This is a tunable parameter that should only affect the performance
614 of comparisons, nothing else. Setting it high makes comparing deeply
615 nested non-cyclical data structures faster, but makes comparing cyclical
616 data structures slower.
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000617*/
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000618#define NESTING_LIMIT 20
619
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000620static int compare_nesting = 0;
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000621
622static PyObject*
Fred Drake100814d2000-07-09 15:48:49 +0000623get_inprogress_dict(void)
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000624{
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000625 static PyObject *key;
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000626 PyObject *tstate_dict, *inprogress;
627
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000628 if (key == NULL) {
629 key = PyString_InternFromString("cmp_state");
630 if (key == NULL)
631 return NULL;
632 }
633
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000634 tstate_dict = PyThreadState_GetDict();
635 if (tstate_dict == NULL) {
636 PyErr_BadInternalCall();
637 return NULL;
638 }
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000639
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000640 inprogress = PyDict_GetItem(tstate_dict, key);
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000641 if (inprogress == NULL) {
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000642 inprogress = PyDict_New();
643 if (inprogress == NULL)
644 return NULL;
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000645 if (PyDict_SetItem(tstate_dict, key, inprogress) == -1) {
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000646 Py_DECREF(inprogress);
647 return NULL;
648 }
Jeremy Hyltona251ea02000-06-09 16:20:39 +0000649 Py_DECREF(inprogress);
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000650 }
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000651
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000652 return inprogress;
653}
654
655static PyObject *
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000656check_recursion(PyObject *v, PyObject *w, int op)
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000657{
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000658 PyObject *inprogress;
659 PyObject *token;
Barry Warsaw9d23a4e2000-08-18 05:01:19 +0000660 Py_uintptr_t iv = (Py_uintptr_t)v;
661 Py_uintptr_t iw = (Py_uintptr_t)w;
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000662 PyObject *x, *y, *z;
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000663
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000664 inprogress = get_inprogress_dict();
665 if (inprogress == NULL)
666 return NULL;
667
668 token = PyTuple_New(3);
669 if (token == NULL)
670 return NULL;
671
672 if (iv <= iw) {
673 PyTuple_SET_ITEM(token, 0, x = PyLong_FromVoidPtr((void *)v));
674 PyTuple_SET_ITEM(token, 1, y = PyLong_FromVoidPtr((void *)w));
675 if (op >= 0)
676 op = swapped_op[op];
677 } else {
678 PyTuple_SET_ITEM(token, 0, x = PyLong_FromVoidPtr((void *)w));
679 PyTuple_SET_ITEM(token, 1, y = PyLong_FromVoidPtr((void *)v));
680 }
681 PyTuple_SET_ITEM(token, 2, z = PyInt_FromLong((long)op));
682 if (x == NULL || y == NULL || z == NULL) {
683 Py_DECREF(token);
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000684 return NULL;
685 }
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000686
687 if (PyDict_GetItem(inprogress, token) != NULL) {
688 Py_DECREF(token);
689 return Py_None; /* Without INCREF! */
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000690 }
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000691
692 if (PyDict_SetItem(inprogress, token, token) < 0) {
693 Py_DECREF(token);
694 return NULL;
695 }
696
697 return token;
698}
699
700static void
701delete_token(PyObject *token)
702{
703 PyObject *inprogress;
704
705 if (token == NULL || token == Py_None)
706 return;
707 inprogress = get_inprogress_dict();
708 if (inprogress == NULL)
709 PyErr_Clear();
710 else
711 PyDict_DelItem(inprogress, token);
712 Py_DECREF(token);
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000713}
714
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000715int
Fred Drake100814d2000-07-09 15:48:49 +0000716PyObject_Compare(PyObject *v, PyObject *w)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000717{
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000718 PyTypeObject *vtp;
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000719 int result;
720
Jack Jansend49cbe12000-08-22 21:52:51 +0000721#if defined(USE_STACKCHECK)
Guido van Rossum9c0a99e2000-08-30 15:53:50 +0000722 if (PyOS_CheckStack()) {
Jack Jansend49cbe12000-08-22 21:52:51 +0000723 PyErr_SetString(PyExc_MemoryError, "Stack overflow");
724 return -1;
725 }
726#endif
Guido van Rossumc8b6df91997-05-23 00:06:51 +0000727 if (v == NULL || w == NULL) {
728 PyErr_BadInternalCall();
729 return -1;
730 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000731 if (v == w)
732 return 0;
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +0000733 vtp = v->ob_type;
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000734 compare_nesting++;
735 if (compare_nesting > NESTING_LIMIT &&
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +0000736 (vtp->tp_as_mapping
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000737 || (vtp->tp_as_sequence
738 && !PyString_Check(v)
739 && !PyTuple_Check(v)))) {
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +0000740 /* try to detect circular data structures */
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000741 PyObject *token = check_recursion(v, w, -1);
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000742
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000743 if (token == NULL) {
744 result = -1;
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000745 }
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000746 else if (token == Py_None) {
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000747 /* already comparing these objects. assume
748 they're equal until shown otherwise */
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +0000749 result = 0;
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000750 }
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000751 else {
752 result = do_cmp(v, w);
753 delete_token(token);
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000754 }
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000755 }
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +0000756 else {
757 result = do_cmp(v, w);
758 }
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000759 compare_nesting--;
Guido van Rossume797ec12001-01-17 15:24:28 +0000760 return result < 0 ? -1 : result;
761}
762
763static PyObject *
764try_3way_to_rich_compare(PyObject *v, PyObject *w, int op)
765{
766 int c;
767 PyObject *result;
768
769 c = try_3way_compare(v, w);
770 if (c <= -2)
771 return NULL;
772 if (c >= 2)
773 c = default_3way_compare(v, w);
774 switch (op) {
775 case Py_LT: c = c < 0; break;
776 case Py_LE: c = c <= 0; break;
777 case Py_EQ: c = c == 0; break;
778 case Py_NE: c = c != 0; break;
779 case Py_GT: c = c > 0; break;
780 case Py_GE: c = c >= 0; break;
781 }
782 result = c ? Py_True : Py_False;
783 Py_INCREF(result);
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000784 return result;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000785}
786
Neil Schemenauerd38855c2001-01-21 16:25:18 +0000787static PyObject *
Guido van Rossume797ec12001-01-17 15:24:28 +0000788do_richcmp(PyObject *v, PyObject *w, int op)
789{
790 PyObject *res;
791
792 res = try_rich_compare(v, w, op);
793 if (res != Py_NotImplemented)
794 return res;
795 Py_DECREF(res);
796
797 return try_3way_to_rich_compare(v, w, op);
798}
799
800PyObject *
801PyObject_RichCompare(PyObject *v, PyObject *w, int op)
802{
803 PyObject *res;
804
805 assert(Py_LT <= op && op <= Py_GE);
806
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000807 compare_nesting++;
808 if (compare_nesting > NESTING_LIMIT &&
809 (v->ob_type->tp_as_mapping
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000810 || (v->ob_type->tp_as_sequence
811 && !PyString_Check(v)
812 && !PyTuple_Check(v)))) {
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000813 /* try to detect circular data structures */
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000814 PyObject *token = check_recursion(v, w, op);
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000815
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000816 if (token == NULL) {
817 res = NULL;
Guido van Rossume797ec12001-01-17 15:24:28 +0000818 }
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000819 else if (token == Py_None) {
820 /* already comparing these objects with this operator.
821 assume they're equal until shown otherwise */
822 if (op == Py_EQ)
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000823 res = Py_True;
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000824 else if (op == Py_NE)
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000825 res = Py_False;
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000826 else {
827 PyErr_SetString(PyExc_ValueError,
828 "can't order recursive values");
829 res = NULL;
830 }
831 Py_XINCREF(res);
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000832 }
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000833 else {
834 res = do_richcmp(v, w, op);
835 delete_token(token);
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000836 }
Guido van Rossume797ec12001-01-17 15:24:28 +0000837 }
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000838 else {
839 res = do_richcmp(v, w, op);
840 }
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000841 compare_nesting--;
Guido van Rossume797ec12001-01-17 15:24:28 +0000842 return res;
843}
844
845int
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 Rossum41c32442001-01-18 23:33:37 +0000957 if (tp->tp_compare == NULL && tp->tp_richcompare == 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
Jeremy Hylton99a8f902000-06-23 14:36:32 +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
1459/* These methods are used to control infinite recursion in repr, str, print,
1460 etc. Container objects that may recursively contain themselves,
1461 e.g. builtin dictionaries and lists, should used Py_ReprEnter() and
1462 Py_ReprLeave() to avoid infinite recursion.
1463
1464 Py_ReprEnter() returns 0 the first time it is called for a particular
1465 object and 1 every time thereafter. It returns -1 if an exception
1466 occurred. Py_ReprLeave() has no return value.
1467
1468 See dictobject.c and listobject.c for examples of use.
1469*/
1470
1471#define KEY "Py_Repr"
1472
1473int
Fred Drake100814d2000-07-09 15:48:49 +00001474Py_ReprEnter(PyObject *obj)
Guido van Rossum86610361998-04-10 22:32:46 +00001475{
1476 PyObject *dict;
1477 PyObject *list;
1478 int i;
1479
1480 dict = PyThreadState_GetDict();
1481 if (dict == NULL)
1482 return -1;
1483 list = PyDict_GetItemString(dict, KEY);
1484 if (list == NULL) {
1485 list = PyList_New(0);
1486 if (list == NULL)
1487 return -1;
1488 if (PyDict_SetItemString(dict, KEY, list) < 0)
1489 return -1;
1490 Py_DECREF(list);
1491 }
1492 i = PyList_GET_SIZE(list);
1493 while (--i >= 0) {
1494 if (PyList_GET_ITEM(list, i) == obj)
1495 return 1;
1496 }
1497 PyList_Append(list, obj);
1498 return 0;
1499}
1500
1501void
Fred Drake100814d2000-07-09 15:48:49 +00001502Py_ReprLeave(PyObject *obj)
Guido van Rossum86610361998-04-10 22:32:46 +00001503{
1504 PyObject *dict;
1505 PyObject *list;
1506 int i;
1507
1508 dict = PyThreadState_GetDict();
Guido van Rossumeb909461998-04-11 15:17:34 +00001509 if (dict == NULL)
1510 return;
Guido van Rossum86610361998-04-10 22:32:46 +00001511 list = PyDict_GetItemString(dict, KEY);
Guido van Rossumeb909461998-04-11 15:17:34 +00001512 if (list == NULL || !PyList_Check(list))
1513 return;
Guido van Rossum86610361998-04-10 22:32:46 +00001514 i = PyList_GET_SIZE(list);
1515 /* Count backwards because we always expect obj to be list[-1] */
1516 while (--i >= 0) {
1517 if (PyList_GET_ITEM(list, i) == obj) {
1518 PyList_SetSlice(list, i, i + 1, NULL);
1519 break;
1520 }
1521 }
1522}
Guido van Rossumd724b232000-03-13 16:01:29 +00001523
1524/*
1525 trashcan
1526 CT 2k0130
1527 non-recursively destroy nested objects
1528
1529 CT 2k0223
1530 everything is now done in a macro.
1531
1532 CT 2k0305
1533 modified to use functions, after Tim Peter's suggestion.
1534
1535 CT 2k0309
1536 modified to restore a possible error.
Guido van Rossum13ff8eb2000-03-25 18:39:19 +00001537
1538 CT 2k0325
1539 added better safe than sorry check for threadstate
Guido van Rossume92e6102000-04-24 15:40:53 +00001540
1541 CT 2k0422
1542 complete rewrite. We now build a chain via ob_type
1543 and save the limited number of types in ob_refcnt.
1544 This is perfect since we don't need any memory.
1545 A patch for free-threading would need just a lock.
Guido van Rossumd724b232000-03-13 16:01:29 +00001546*/
1547
Guido van Rossume92e6102000-04-24 15:40:53 +00001548#define Py_TRASHCAN_TUPLE 1
1549#define Py_TRASHCAN_LIST 2
1550#define Py_TRASHCAN_DICT 3
1551#define Py_TRASHCAN_FRAME 4
1552#define Py_TRASHCAN_TRACEBACK 5
1553/* extend here if other objects want protection */
1554
Guido van Rossumd724b232000-03-13 16:01:29 +00001555int _PyTrash_delete_nesting = 0;
Guido van Rossume92e6102000-04-24 15:40:53 +00001556
Guido van Rossumd724b232000-03-13 16:01:29 +00001557PyObject * _PyTrash_delete_later = NULL;
1558
1559void
Fred Drake100814d2000-07-09 15:48:49 +00001560_PyTrash_deposit_object(PyObject *op)
Guido van Rossumd724b232000-03-13 16:01:29 +00001561{
Guido van Rossume92e6102000-04-24 15:40:53 +00001562 int typecode;
Guido van Rossum13ff8eb2000-03-25 18:39:19 +00001563
Guido van Rossume92e6102000-04-24 15:40:53 +00001564 if (PyTuple_Check(op))
1565 typecode = Py_TRASHCAN_TUPLE;
1566 else if (PyList_Check(op))
1567 typecode = Py_TRASHCAN_LIST;
1568 else if (PyDict_Check(op))
1569 typecode = Py_TRASHCAN_DICT;
1570 else if (PyFrame_Check(op))
1571 typecode = Py_TRASHCAN_FRAME;
1572 else if (PyTraceBack_Check(op))
1573 typecode = Py_TRASHCAN_TRACEBACK;
Moshe Zadkacf703f02000-08-04 15:36:13 +00001574 else /* We have a bug here -- those are the only types in GC */ {
1575 Py_FatalError("Type not supported in GC -- internal bug");
1576 return; /* pacify compiler -- execution never here */
1577 }
Guido van Rossume92e6102000-04-24 15:40:53 +00001578 op->ob_refcnt = typecode;
Guido van Rossumd724b232000-03-13 16:01:29 +00001579
Guido van Rossume92e6102000-04-24 15:40:53 +00001580 op->ob_type = (PyTypeObject*)_PyTrash_delete_later;
1581 _PyTrash_delete_later = op;
Guido van Rossumd724b232000-03-13 16:01:29 +00001582}
1583
1584void
Fred Drake100814d2000-07-09 15:48:49 +00001585_PyTrash_destroy_chain(void)
Guido van Rossumd724b232000-03-13 16:01:29 +00001586{
1587 while (_PyTrash_delete_later) {
1588 PyObject *shredder = _PyTrash_delete_later;
Guido van Rossume92e6102000-04-24 15:40:53 +00001589 _PyTrash_delete_later = (PyObject*) shredder->ob_type;
1590
1591 switch (shredder->ob_refcnt) {
1592 case Py_TRASHCAN_TUPLE:
1593 shredder->ob_type = &PyTuple_Type;
1594 break;
1595 case Py_TRASHCAN_LIST:
1596 shredder->ob_type = &PyList_Type;
1597 break;
1598 case Py_TRASHCAN_DICT:
1599 shredder->ob_type = &PyDict_Type;
1600 break;
1601 case Py_TRASHCAN_FRAME:
1602 shredder->ob_type = &PyFrame_Type;
1603 break;
1604 case Py_TRASHCAN_TRACEBACK:
1605 shredder->ob_type = &PyTraceBack_Type;
1606 break;
1607 }
1608 _Py_NewReference(shredder);
1609
Guido van Rossumd724b232000-03-13 16:01:29 +00001610 ++_PyTrash_delete_nesting;
1611 Py_DECREF(shredder);
1612 --_PyTrash_delete_nesting;
1613 }
1614}