blob: 3cad24153ebd912c46505932012026ec4c615276 [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
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000223PyObject *
Fred Drake100814d2000-07-09 15:48:49 +0000224PyObject_Repr(PyObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000225{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000226 if (PyErr_CheckSignals())
Guido van Rossum90933611991-06-07 16:10:43 +0000227 return NULL;
Guido van Rossum9b00dfa1998-04-28 16:06:54 +0000228#ifdef USE_STACKCHECK
229 if (PyOS_CheckStack()) {
Fred Drake661ea262000-10-24 19:57:45 +0000230 PyErr_SetString(PyExc_MemoryError, "stack overflow");
Guido van Rossum9b00dfa1998-04-28 16:06:54 +0000231 return NULL;
232 }
233#endif
Guido van Rossum90933611991-06-07 16:10:43 +0000234 if (v == NULL)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000235 return PyString_FromString("<NULL>");
Guido van Rossum90933611991-06-07 16:10:43 +0000236 else if (v->ob_type->tp_repr == NULL) {
237 char buf[120];
Fred Drakea44d3532000-06-30 15:01:00 +0000238 sprintf(buf, "<%.80s object at %p>",
239 v->ob_type->tp_name, v);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000240 return PyString_FromString(buf);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000241 }
Guido van Rossum4c08d552000-03-10 22:55:18 +0000242 else {
243 PyObject *res;
244 res = (*v->ob_type->tp_repr)(v);
245 if (res == NULL)
246 return NULL;
Fredrik Lundhefecc7d2000-07-01 14:31:09 +0000247 if (PyUnicode_Check(res)) {
248 PyObject* str;
Fredrik Lundh2a1e0602000-07-08 17:43:32 +0000249 str = PyUnicode_AsUnicodeEscapeString(res);
Marc-André Lemburg891bc652000-07-03 09:57:53 +0000250 Py_DECREF(res);
251 if (str)
Fredrik Lundhefecc7d2000-07-01 14:31:09 +0000252 res = str;
Marc-André Lemburg891bc652000-07-03 09:57:53 +0000253 else
254 return NULL;
Fredrik Lundhefecc7d2000-07-01 14:31:09 +0000255 }
Guido van Rossum4c08d552000-03-10 22:55:18 +0000256 if (!PyString_Check(res)) {
257 PyErr_Format(PyExc_TypeError,
Guido van Rossum5db862d2000-04-10 12:46:51 +0000258 "__repr__ returned non-string (type %.200s)",
Guido van Rossum4c08d552000-03-10 22:55:18 +0000259 res->ob_type->tp_name);
260 Py_DECREF(res);
261 return NULL;
262 }
263 return res;
264 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000265}
266
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000267PyObject *
Fred Drake100814d2000-07-09 15:48:49 +0000268PyObject_Str(PyObject *v)
Guido van Rossumc6004111993-11-05 10:22:19 +0000269{
Guido van Rossum4c08d552000-03-10 22:55:18 +0000270 PyObject *res;
271
Guido van Rossumc6004111993-11-05 10:22:19 +0000272 if (v == NULL)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000273 return PyString_FromString("<NULL>");
274 else if (PyString_Check(v)) {
275 Py_INCREF(v);
Guido van Rossumc6004111993-11-05 10:22:19 +0000276 return v;
277 }
Guido van Rossum32b582b1995-01-17 16:35:13 +0000278 else if (v->ob_type->tp_str != NULL)
Guido van Rossum4c08d552000-03-10 22:55:18 +0000279 res = (*v->ob_type->tp_str)(v);
Guido van Rossumc6004111993-11-05 10:22:19 +0000280 else {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000281 PyObject *func;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000282 if (!PyInstance_Check(v) ||
283 (func = PyObject_GetAttrString(v, "__str__")) == NULL) {
284 PyErr_Clear();
285 return PyObject_Repr(v);
Guido van Rossumc6004111993-11-05 10:22:19 +0000286 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000287 res = PyEval_CallObject(func, (PyObject *)NULL);
288 Py_DECREF(func);
Guido van Rossumc6004111993-11-05 10:22:19 +0000289 }
Guido van Rossum4c08d552000-03-10 22:55:18 +0000290 if (res == NULL)
291 return NULL;
Marc-André Lemburg891bc652000-07-03 09:57:53 +0000292 if (PyUnicode_Check(res)) {
293 PyObject* str;
294 str = PyUnicode_AsEncodedString(res, NULL, NULL);
295 Py_DECREF(res);
296 if (str)
297 res = str;
298 else
299 return NULL;
300 }
Guido van Rossum4c08d552000-03-10 22:55:18 +0000301 if (!PyString_Check(res)) {
302 PyErr_Format(PyExc_TypeError,
Guido van Rossum5db862d2000-04-10 12:46:51 +0000303 "__str__ returned non-string (type %.200s)",
Guido van Rossum4c08d552000-03-10 22:55:18 +0000304 res->ob_type->tp_name);
305 Py_DECREF(res);
306 return NULL;
307 }
308 return res;
Guido van Rossumc6004111993-11-05 10:22:19 +0000309}
310
Guido van Rossume797ec12001-01-17 15:24:28 +0000311/* Map rich comparison operators to their swapped version, e.g. LT --> GT */
312static int swapped_op[] = {Py_GT, Py_GE, Py_EQ, Py_NE, Py_LT, Py_LE};
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +0000313
Guido van Rossume797ec12001-01-17 15:24:28 +0000314/* Try a genuine rich comparison, returning an object. Return:
315 NULL for exception;
316 NotImplemented if this particular rich comparison is not implemented or
317 undefined;
318 some object not equal to NotImplemented if it is implemented
319 (this latter object may not be a Boolean).
320*/
321static PyObject *
322try_rich_compare(PyObject *v, PyObject *w, int op)
323{
324 richcmpfunc f;
325 PyObject *res;
326
327 if ((f = v->ob_type->tp_richcompare) != NULL) {
328 res = (*f)(v, w, op);
329 if (res != Py_NotImplemented)
330 return res;
331 Py_DECREF(res);
332 }
333 if ((f = w->ob_type->tp_richcompare) != NULL) {
334 return (*f)(w, v, swapped_op[op]);
335 }
336 res = Py_NotImplemented;
337 Py_INCREF(res);
338 return res;
339}
340
341/* Try a genuine rich comparison, returning an int. Return:
342 -1 for exception (including the case where try_rich_compare() returns an
343 object that's not a Boolean);
344 0 if the outcome is false;
345 1 if the outcome is true;
346 2 if this particular rich comparison is not implemented or undefined.
347*/
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +0000348static int
Guido van Rossume797ec12001-01-17 15:24:28 +0000349try_rich_compare_bool(PyObject *v, PyObject *w, int op)
350{
351 PyObject *res;
352 int ok;
353
354 if (v->ob_type->tp_richcompare == NULL &&
355 w->ob_type->tp_richcompare == NULL)
356 return 2; /* Shortcut, avoid INCREF+DECREF */
357 res = try_rich_compare(v, w, op);
358 if (res == NULL)
359 return -1;
360 if (res == Py_NotImplemented) {
361 Py_DECREF(res);
362 return 2;
363 }
364 ok = PyObject_IsTrue(res);
365 Py_DECREF(res);
366 return ok;
367}
368
369/* Try rich comparisons to determine a 3-way comparison. Return:
370 -2 for an exception;
371 -1 if v < w;
372 0 if v == w;
373 1 if v > w;
374 2 if this particular rich comparison is not implemented or undefined.
375*/
376static int
377try_rich_to_3way_compare(PyObject *v, PyObject *w)
378{
379 if (v->ob_type->tp_richcompare == NULL &&
380 w->ob_type->tp_richcompare == NULL)
381 return 2; /* Shortcut */
382 switch (try_rich_compare_bool(v, w, Py_LT)) {
383 case -1: /* Error */
384 return -1;
385 case 0: /* False: not less */
386 break;
387 case 1: /* True: less */
388 return -1;
389 case 2: /* NotImplemented */
390 break;
391 }
392 switch (try_rich_compare_bool(v, w, Py_GT)) {
393 case -1: /* Error */
394 return -1;
395 case 0: /* False: not greater */
396 break;
397 case 1: /* True: greater */
398 return 1;
399 case 2: /* NotImplemented */
400 break;
401 }
402 switch (try_rich_compare_bool(v, w, Py_EQ)) {
403 case -1: /* Error */
404 return -1;
405 case 0: /* False: not equal */
406 break;
407 case 1: /* True: equal */
408 return 0;
409 case 2: /* NotImplemented */
410 break;
411 }
412 return 2; /* XXX Even if all three returned FALSE?! */
413}
414
415/* Try a 3-way comparison, returning an int. Return:
416 -2 for an exception;
417 -1 if v < w;
418 0 if v == w;
419 1 if v > w;
420 2 if this particular 3-way comparison is not implemented or undefined.
421*/
422static int
423try_3way_compare(PyObject *v, PyObject *w)
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +0000424{
425 int c;
Guido van Rossume797ec12001-01-17 15:24:28 +0000426 cmpfunc f;
427
428 /* Comparisons involving instances are given to instance_compare,
429 which has the same return conventions as this function. */
430
431 if (PyInstance_Check(v))
432 return (*v->ob_type->tp_compare)(v, w);
433 if (PyInstance_Check(w))
434 return (*w->ob_type->tp_compare)(v, w);
435
436 /* If the types are equal, don't bother with coercions etc. */
437 if (v->ob_type == w->ob_type) {
438 if ((f = v->ob_type->tp_compare) == NULL)
439 return 2;
440 c = (*f)(v, w);
441 if (PyErr_Occurred())
442 return -2;
443 return c < 0 ? -1 : c > 0 ? 1 : 0;
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +0000444 }
Guido van Rossume797ec12001-01-17 15:24:28 +0000445
446 /* Try coercion; if it fails, give up */
447 c = PyNumber_CoerceEx(&v, &w);
448 if (c < 0)
449 return -2;
450 if (c > 0)
451 return 2;
452
453 /* Try v's comparison, if defined */
454 if ((f = v->ob_type->tp_compare) != NULL) {
455 c = (*f)(v, w);
456 Py_DECREF(v);
457 Py_DECREF(w);
458 if (PyErr_Occurred())
459 return -2;
460 return c < 0 ? -1 : c > 0 ? 1 : 0;
461 }
462
463 /* Try w's comparison, if defined */
464 if ((f = w->ob_type->tp_compare) != NULL) {
465 c = (*f)(w, v); /* swapped! */
466 Py_DECREF(v);
467 Py_DECREF(w);
468 if (PyErr_Occurred())
469 return -2;
470 return c < 0 ? 1 : c > 0 ? -1 : 0; /* negated! */
471 }
472
473 /* No comparison defined */
474 Py_DECREF(v);
475 Py_DECREF(w);
476 return 2;
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +0000477}
478
Guido van Rossume797ec12001-01-17 15:24:28 +0000479/* Final fallback 3-way comparison, returning an int. Return:
480 -2 if an error occurred;
481 -1 if v < w;
482 0 if v == w;
483 1 if v > w.
484*/
485static int
486default_3way_compare(PyObject *v, PyObject *w)
487{
488 int c;
489
490 if (v->ob_type == w->ob_type) {
491 /* same type: compare pointers */
492 void *vv = v;
493 void *ww = w;
494 return (vv < ww) ? -1 : (vv > ww) ? 1 : 0;
495 }
496
497 /* Special case for Unicode */
498 if (PyUnicode_Check(v) || PyUnicode_Check(w)) {
499 c = PyUnicode_Compare(v, w);
500 if (!PyErr_Occurred())
501 return c;
502 /* TypeErrors are ignored: if Unicode coercion fails due
503 to one of the arguments not having the right type, we
504 continue as defined by the coercion protocol (see
505 above). Luckily, decoding errors are reported as
506 ValueErrors and are not masked by this technique. */
507 if (!PyErr_ExceptionMatches(PyExc_TypeError))
508 return -2;
509 PyErr_Clear();
510 }
511
512 /* different type: compare type names */
513 c = strcmp(v->ob_type->tp_name, w->ob_type->tp_name);
514 return (c < 0) ? -1 : (c > 0) ? 1 : 0;
515}
516
517#define CHECK_TYPES(o) PyType_HasFeature((o)->ob_type, Py_TPFLAGS_CHECKTYPES)
518
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +0000519static int
Fred Drake100814d2000-07-09 15:48:49 +0000520do_cmp(PyObject *v, PyObject *w)
Guido van Rossum20566841995-01-12 11:26:10 +0000521{
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +0000522 int c;
523
Guido van Rossume797ec12001-01-17 15:24:28 +0000524 c = try_rich_to_3way_compare(v, w);
525 if (c < 2)
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +0000526 return c;
Guido van Rossume797ec12001-01-17 15:24:28 +0000527 c = try_3way_compare(v, w);
528 if (c < 2)
529 return c;
530 return default_3way_compare(v, w);
Guido van Rossum20566841995-01-12 11:26:10 +0000531}
532
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000533PyObject *_PyCompareState_Key;
534
Thomas Wouters7e474022000-07-16 12:04:32 +0000535/* _PyCompareState_nesting is incremented before calling compare (for
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000536 some types) and decremented on exit. If the count exceeds the
537 nesting limit, enable code to detect circular data structures.
538*/
Jack Jansend49cbe12000-08-22 21:52:51 +0000539#ifdef macintosh
540#define NESTING_LIMIT 60
541#else
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000542#define NESTING_LIMIT 500
Jack Jansend49cbe12000-08-22 21:52:51 +0000543#endif
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000544int _PyCompareState_nesting = 0;
545
546static PyObject*
Fred Drake100814d2000-07-09 15:48:49 +0000547get_inprogress_dict(void)
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000548{
549 PyObject *tstate_dict, *inprogress;
550
551 tstate_dict = PyThreadState_GetDict();
552 if (tstate_dict == NULL) {
553 PyErr_BadInternalCall();
554 return NULL;
555 }
556 inprogress = PyDict_GetItem(tstate_dict, _PyCompareState_Key);
557 if (inprogress == NULL) {
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000558 inprogress = PyDict_New();
559 if (inprogress == NULL)
560 return NULL;
561 if (PyDict_SetItem(tstate_dict, _PyCompareState_Key,
562 inprogress) == -1) {
563 Py_DECREF(inprogress);
564 return NULL;
565 }
Jeremy Hyltona251ea02000-06-09 16:20:39 +0000566 Py_DECREF(inprogress);
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000567 }
568 return inprogress;
569}
570
571static PyObject *
Fred Drake100814d2000-07-09 15:48:49 +0000572make_pair(PyObject *v, PyObject *w)
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000573{
574 PyObject *pair;
Barry Warsaw9d23a4e2000-08-18 05:01:19 +0000575 Py_uintptr_t iv = (Py_uintptr_t)v;
576 Py_uintptr_t iw = (Py_uintptr_t)w;
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000577
578 pair = PyTuple_New(2);
579 if (pair == NULL) {
580 return NULL;
581 }
Barry Warsaw9d23a4e2000-08-18 05:01:19 +0000582 if (iv <= iw) {
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000583 PyTuple_SET_ITEM(pair, 0, PyLong_FromVoidPtr((void *)v));
584 PyTuple_SET_ITEM(pair, 1, PyLong_FromVoidPtr((void *)w));
585 } else {
586 PyTuple_SET_ITEM(pair, 0, PyLong_FromVoidPtr((void *)w));
587 PyTuple_SET_ITEM(pair, 1, PyLong_FromVoidPtr((void *)v));
588 }
589 return pair;
590}
591
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000592int
Fred Drake100814d2000-07-09 15:48:49 +0000593PyObject_Compare(PyObject *v, PyObject *w)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000594{
Guido van Rossumcd5a5f61998-06-09 18:58:44 +0000595 PyTypeObject *vtp, *wtp;
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000596 int result;
597
Jack Jansend49cbe12000-08-22 21:52:51 +0000598#if defined(USE_STACKCHECK)
Guido van Rossum9c0a99e2000-08-30 15:53:50 +0000599 if (PyOS_CheckStack()) {
Jack Jansend49cbe12000-08-22 21:52:51 +0000600 PyErr_SetString(PyExc_MemoryError, "Stack overflow");
601 return -1;
602 }
603#endif
Guido van Rossumc8b6df91997-05-23 00:06:51 +0000604 if (v == NULL || w == NULL) {
605 PyErr_BadInternalCall();
606 return -1;
607 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000608 if (v == w)
609 return 0;
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +0000610 vtp = v->ob_type;
611 wtp = w->ob_type;
Vladimir Marangozov1d3e2392000-08-11 00:14:26 +0000612 _PyCompareState_nesting++;
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +0000613 if (_PyCompareState_nesting > NESTING_LIMIT &&
614 (vtp->tp_as_mapping
615 || PyInstance_Check(v)
616 || (vtp->tp_as_sequence && !PyString_Check(v)))) {
617 /* try to detect circular data structures */
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000618 PyObject *inprogress, *pair;
619
620 inprogress = get_inprogress_dict();
621 if (inprogress == NULL) {
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +0000622 result = -1;
623 goto exit_cmp;
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000624 }
625 pair = make_pair(v, w);
626 if (PyDict_GetItem(inprogress, pair)) {
627 /* already comparing these objects. assume
628 they're equal until shown otherwise */
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000629 Py_DECREF(pair);
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +0000630 result = 0;
631 goto exit_cmp;
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000632 }
633 if (PyDict_SetItem(inprogress, pair, pair) == -1) {
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +0000634 result = -1;
635 goto exit_cmp;
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000636 }
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +0000637 result = do_cmp(v, w);
638 /* XXX DelItem shouldn't fail */
639 PyDict_DelItem(inprogress, pair);
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000640 Py_DECREF(pair);
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000641 }
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +0000642 else {
643 result = do_cmp(v, w);
644 }
645exit_cmp:
Vladimir Marangozov1d3e2392000-08-11 00:14:26 +0000646 _PyCompareState_nesting--;
Guido van Rossume797ec12001-01-17 15:24:28 +0000647 return result < 0 ? -1 : result;
648}
649
650static PyObject *
651try_3way_to_rich_compare(PyObject *v, PyObject *w, int op)
652{
653 int c;
654 PyObject *result;
655
656 c = try_3way_compare(v, w);
657 if (c <= -2)
658 return NULL;
659 if (c >= 2)
660 c = default_3way_compare(v, w);
661 switch (op) {
662 case Py_LT: c = c < 0; break;
663 case Py_LE: c = c <= 0; break;
664 case Py_EQ: c = c == 0; break;
665 case Py_NE: c = c != 0; break;
666 case Py_GT: c = c > 0; break;
667 case Py_GE: c = c >= 0; break;
668 }
669 result = c ? Py_True : Py_False;
670 Py_INCREF(result);
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000671 return result;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000672}
673
Guido van Rossume797ec12001-01-17 15:24:28 +0000674PyObject *
675do_richcmp(PyObject *v, PyObject *w, int op)
676{
677 PyObject *res;
678
679 res = try_rich_compare(v, w, op);
680 if (res != Py_NotImplemented)
681 return res;
682 Py_DECREF(res);
683
684 return try_3way_to_rich_compare(v, w, op);
685}
686
687PyObject *
688PyObject_RichCompare(PyObject *v, PyObject *w, int op)
689{
690 PyObject *res;
691
692 assert(Py_LT <= op && op <= Py_GE);
693
694 if (_PyCompareState_nesting > NESTING_LIMIT) {
695 /* Too deeply nested -- assume equal */
696 /* XXX This is an unfair shortcut!
697 Should use the same logic as PyObject_Compare. */
698 switch (op) {
699 case Py_LT:
700 case Py_NE:
701 case Py_GT:
702 res = Py_False;
703 break;
704 case Py_LE:
705 case Py_EQ:
706 case Py_GE:
707 res = Py_True;
708 break;
709 }
710 Py_INCREF(res);
711 return res;
712 }
713
714 _PyCompareState_nesting++;
715 res = do_richcmp(v, w, op);
716 _PyCompareState_nesting--;
717
718 return res;
719}
720
721int
722PyObject_RichCompareBool(PyObject *v, PyObject *w, int op)
723{
724 PyObject *res = PyObject_RichCompare(v, w, op);
725 int ok;
726
727 if (res == NULL)
728 return -1;
729 ok = PyObject_IsTrue(res);
730 Py_DECREF(res);
731 return ok;
732}
Fred Drake13634cf2000-06-29 19:17:04 +0000733
734/* Set of hash utility functions to help maintaining the invariant that
735 iff a==b then hash(a)==hash(b)
736
737 All the utility functions (_Py_Hash*()) return "-1" to signify an error.
738*/
739
740long
Fred Drake100814d2000-07-09 15:48:49 +0000741_Py_HashDouble(double v)
Fred Drake13634cf2000-06-29 19:17:04 +0000742{
Tim Peters39dce292000-08-15 03:34:48 +0000743 double intpart, fractpart;
744 int expo;
745 long hipart;
746 long x; /* the final hash value */
747 /* This is designed so that Python numbers of different types
748 * that compare equal hash to the same value; otherwise comparisons
749 * of mapping keys will turn out weird.
750 */
751
752#ifdef MPW /* MPW C modf expects pointer to extended as second argument */
753{
754 extended e;
755 fractpart = modf(v, &e);
756 intpart = e;
757}
758#else
759 fractpart = modf(v, &intpart);
760#endif
761 if (fractpart == 0.0) {
762 /* This must return the same hash as an equal int or long. */
763 if (intpart > LONG_MAX || -intpart > LONG_MAX) {
764 /* Convert to long and use its hash. */
765 PyObject *plong; /* converted to Python long */
766 if (Py_IS_INFINITY(intpart))
767 /* can't convert to long int -- arbitrary */
768 v = v < 0 ? -271828.0 : 314159.0;
769 plong = PyLong_FromDouble(v);
770 if (plong == NULL)
771 return -1;
772 x = PyObject_Hash(plong);
773 Py_DECREF(plong);
774 return x;
775 }
776 /* Fits in a C long == a Python int, so is its own hash. */
777 x = (long)intpart;
778 if (x == -1)
779 x = -2;
780 return x;
781 }
782 /* The fractional part is non-zero, so we don't have to worry about
783 * making this match the hash of some other type.
784 * Use frexp to get at the bits in the double.
Fred Drake13634cf2000-06-29 19:17:04 +0000785 * Since the VAX D double format has 56 mantissa bits, which is the
786 * most of any double format in use, each of these parts may have as
787 * many as (but no more than) 56 significant bits.
Tim Peters39dce292000-08-15 03:34:48 +0000788 * So, assuming sizeof(long) >= 4, each part can be broken into two
789 * longs; frexp and multiplication are used to do that.
790 * Also, since the Cray double format has 15 exponent bits, which is
791 * the most of any double format in use, shifting the exponent field
792 * left by 15 won't overflow a long (again assuming sizeof(long) >= 4).
Fred Drake13634cf2000-06-29 19:17:04 +0000793 */
Tim Peters39dce292000-08-15 03:34:48 +0000794 v = frexp(v, &expo);
795 v *= 2147483648.0; /* 2**31 */
796 hipart = (long)v; /* take the top 32 bits */
797 v = (v - (double)hipart) * 2147483648.0; /* get the next 32 bits */
798 x = hipart + (long)v + (expo << 15);
799 if (x == -1)
800 x = -2;
801 return x;
Fred Drake13634cf2000-06-29 19:17:04 +0000802}
803
804long
Fred Drake100814d2000-07-09 15:48:49 +0000805_Py_HashPointer(void *p)
Fred Drake13634cf2000-06-29 19:17:04 +0000806{
807#if SIZEOF_LONG >= SIZEOF_VOID_P
808 return (long)p;
809#else
810 /* convert to a Python long and hash that */
811 PyObject* longobj;
812 long x;
813
814 if ((longobj = PyLong_FromVoidPtr(p)) == NULL) {
815 x = -1;
816 goto finally;
817 }
818 x = PyObject_Hash(longobj);
819
820finally:
821 Py_XDECREF(longobj);
822 return x;
823#endif
824}
825
826
Guido van Rossum9bfef441993-03-29 10:43:31 +0000827long
Fred Drake100814d2000-07-09 15:48:49 +0000828PyObject_Hash(PyObject *v)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000829{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000830 PyTypeObject *tp = v->ob_type;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000831 if (tp->tp_hash != NULL)
832 return (*tp->tp_hash)(v);
Fred Drake13634cf2000-06-29 19:17:04 +0000833 if (tp->tp_compare == NULL) {
834 return _Py_HashPointer(v); /* Use address as hash value */
835 }
Guido van Rossum9bfef441993-03-29 10:43:31 +0000836 /* If there's a cmp but no hash defined, the object can't be hashed */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000837 PyErr_SetString(PyExc_TypeError, "unhashable type");
Guido van Rossum9bfef441993-03-29 10:43:31 +0000838 return -1;
839}
840
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000841PyObject *
Fred Drake100814d2000-07-09 15:48:49 +0000842PyObject_GetAttrString(PyObject *v, char *name)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000843{
Guido van Rossumd8eb1b31996-08-09 20:52:03 +0000844 if (v->ob_type->tp_getattro != NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000845 PyObject *w, *res;
Guido van Rossumc6d06701997-01-18 07:57:16 +0000846 w = PyString_InternFromString(name);
Guido van Rossumd8eb1b31996-08-09 20:52:03 +0000847 if (w == NULL)
848 return NULL;
849 res = (*v->ob_type->tp_getattro)(v, w);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000850 Py_XDECREF(w);
Guido van Rossumd8eb1b31996-08-09 20:52:03 +0000851 return res;
852 }
853
Guido van Rossum3f5da241990-12-20 15:06:42 +0000854 if (v->ob_type->tp_getattr == NULL) {
Guido van Rossumdb935161998-01-19 22:16:36 +0000855 PyErr_Format(PyExc_AttributeError,
856 "'%.50s' object has no attribute '%.400s'",
857 v->ob_type->tp_name,
858 name);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000859 return NULL;
860 }
861 else {
862 return (*v->ob_type->tp_getattr)(v, name);
863 }
864}
865
866int
Fred Drake100814d2000-07-09 15:48:49 +0000867PyObject_HasAttrString(PyObject *v, char *name)
Guido van Rossumed18fdc1993-07-11 19:55:34 +0000868{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000869 PyObject *res = PyObject_GetAttrString(v, name);
Guido van Rossumed18fdc1993-07-11 19:55:34 +0000870 if (res != NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000871 Py_DECREF(res);
Guido van Rossumed18fdc1993-07-11 19:55:34 +0000872 return 1;
873 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000874 PyErr_Clear();
Guido van Rossumed18fdc1993-07-11 19:55:34 +0000875 return 0;
876}
877
878int
Fred Drake100814d2000-07-09 15:48:49 +0000879PyObject_SetAttrString(PyObject *v, char *name, PyObject *w)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000880{
Guido van Rossumd8eb1b31996-08-09 20:52:03 +0000881 if (v->ob_type->tp_setattro != NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000882 PyObject *s;
Guido van Rossumd8eb1b31996-08-09 20:52:03 +0000883 int res;
Guido van Rossumc6d06701997-01-18 07:57:16 +0000884 s = PyString_InternFromString(name);
Guido van Rossumd8eb1b31996-08-09 20:52:03 +0000885 if (s == NULL)
Guido van Rossumb7fc3041996-09-11 22:51:25 +0000886 return -1;
Guido van Rossumd8eb1b31996-08-09 20:52:03 +0000887 res = (*v->ob_type->tp_setattro)(v, s, w);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000888 Py_XDECREF(s);
Guido van Rossumd8eb1b31996-08-09 20:52:03 +0000889 return res;
890 }
891
Guido van Rossum3f5da241990-12-20 15:06:42 +0000892 if (v->ob_type->tp_setattr == NULL) {
893 if (v->ob_type->tp_getattr == NULL)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000894 PyErr_SetString(PyExc_TypeError,
Guido van Rossum3ea74121991-12-24 13:28:03 +0000895 "attribute-less object (assign or del)");
Guido van Rossum3f5da241990-12-20 15:06:42 +0000896 else
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000897 PyErr_SetString(PyExc_TypeError,
Guido van Rossum3ea74121991-12-24 13:28:03 +0000898 "object has read-only attributes");
Guido van Rossum73531a31990-12-20 23:12:40 +0000899 return -1;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000900 }
901 else {
902 return (*v->ob_type->tp_setattr)(v, name, w);
903 }
904}
905
Marc-André Lemburge44e5072000-09-18 16:20:57 +0000906/* Internal API needed by PyObject_GetAttr(): */
907extern
908PyObject *_PyUnicode_AsDefaultEncodedString(PyObject *unicode,
909 const char *errors);
910
Guido van Rossum98ff96a1997-05-20 18:34:44 +0000911PyObject *
Fred Drake100814d2000-07-09 15:48:49 +0000912PyObject_GetAttr(PyObject *v, PyObject *name)
Guido van Rossum98ff96a1997-05-20 18:34:44 +0000913{
Marc-André Lemburge44e5072000-09-18 16:20:57 +0000914 /* The Unicode to string conversion is done here because the
915 existing tp_getattro slots expect a string object as name
916 and we wouldn't want to break those. */
917 if (PyUnicode_Check(name)) {
918 name = _PyUnicode_AsDefaultEncodedString(name, NULL);
919 if (name == NULL)
920 return NULL;
921 }
Jeremy Hylton99a8f902000-06-23 14:36:32 +0000922
923 if (!PyString_Check(name)) {
924 PyErr_SetString(PyExc_TypeError,
925 "attribute name must be string");
926 return NULL;
927 }
Marc-André Lemburge44e5072000-09-18 16:20:57 +0000928 if (v->ob_type->tp_getattro != NULL)
929 return (*v->ob_type->tp_getattro)(v, name);
930 else
Jeremy Hylton99a8f902000-06-23 14:36:32 +0000931 return PyObject_GetAttrString(v, PyString_AS_STRING(name));
Guido van Rossum98ff96a1997-05-20 18:34:44 +0000932}
933
934int
Fred Drake100814d2000-07-09 15:48:49 +0000935PyObject_HasAttr(PyObject *v, PyObject *name)
Guido van Rossum98ff96a1997-05-20 18:34:44 +0000936{
937 PyObject *res = PyObject_GetAttr(v, name);
938 if (res != NULL) {
939 Py_DECREF(res);
940 return 1;
941 }
942 PyErr_Clear();
943 return 0;
944}
945
946int
Fred Drake100814d2000-07-09 15:48:49 +0000947PyObject_SetAttr(PyObject *v, PyObject *name, PyObject *value)
Guido van Rossum98ff96a1997-05-20 18:34:44 +0000948{
949 int err;
Marc-André Lemburge44e5072000-09-18 16:20:57 +0000950
951 /* The Unicode to string conversion is done here because the
952 existing tp_setattro slots expect a string object as name
953 and we wouldn't want to break those. */
954 if (PyUnicode_Check(name)) {
955 name = PyUnicode_AsEncodedString(name, NULL, NULL);
956 if (name == NULL)
957 return -1;
Jeremy Hylton99a8f902000-06-23 14:36:32 +0000958 }
Marc-André Lemburge44e5072000-09-18 16:20:57 +0000959 else
960 Py_INCREF(name);
961
962 if (!PyString_Check(name)){
Jeremy Hylton99a8f902000-06-23 14:36:32 +0000963 PyErr_SetString(PyExc_TypeError,
964 "attribute name must be string");
965 err = -1;
966 }
Marc-André Lemburge44e5072000-09-18 16:20:57 +0000967 else {
968 PyString_InternInPlace(&name);
969 if (v->ob_type->tp_setattro != NULL)
970 err = (*v->ob_type->tp_setattro)(v, name, value);
971 else
972 err = PyObject_SetAttrString(v,
973 PyString_AS_STRING(name), value);
974 }
975
Guido van Rossum98ff96a1997-05-20 18:34:44 +0000976 Py_DECREF(name);
977 return err;
978}
979
Guido van Rossum6ac258d1993-05-12 08:24:20 +0000980/* Test a value used as condition, e.g., in a for or if statement.
981 Return -1 if an error occurred */
982
983int
Fred Drake100814d2000-07-09 15:48:49 +0000984PyObject_IsTrue(PyObject *v)
Guido van Rossum6ac258d1993-05-12 08:24:20 +0000985{
986 int res;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000987 if (v == Py_None)
Guido van Rossum6ac258d1993-05-12 08:24:20 +0000988 res = 0;
Guido van Rossum1c4f4581998-05-22 00:53:24 +0000989 else if (v->ob_type->tp_as_number != NULL &&
990 v->ob_type->tp_as_number->nb_nonzero != NULL)
Guido van Rossum6ac258d1993-05-12 08:24:20 +0000991 res = (*v->ob_type->tp_as_number->nb_nonzero)(v);
Guido van Rossum1c4f4581998-05-22 00:53:24 +0000992 else if (v->ob_type->tp_as_mapping != NULL &&
993 v->ob_type->tp_as_mapping->mp_length != NULL)
Guido van Rossum6ac258d1993-05-12 08:24:20 +0000994 res = (*v->ob_type->tp_as_mapping->mp_length)(v);
Guido van Rossum1c4f4581998-05-22 00:53:24 +0000995 else if (v->ob_type->tp_as_sequence != NULL &&
996 v->ob_type->tp_as_sequence->sq_length != NULL)
Guido van Rossum6ac258d1993-05-12 08:24:20 +0000997 res = (*v->ob_type->tp_as_sequence->sq_length)(v);
998 else
999 res = 1;
1000 if (res > 0)
1001 res = 1;
1002 return res;
1003}
1004
Guido van Rossumc3d3f961998-04-09 17:53:59 +00001005/* equivalent of 'not v'
1006 Return -1 if an error occurred */
1007
1008int
Fred Drake100814d2000-07-09 15:48:49 +00001009PyObject_Not(PyObject *v)
Guido van Rossumc3d3f961998-04-09 17:53:59 +00001010{
1011 int res;
1012 res = PyObject_IsTrue(v);
1013 if (res < 0)
1014 return res;
1015 return res == 0;
1016}
1017
Guido van Rossum5524a591995-01-10 15:26:20 +00001018/* Coerce two numeric types to the "larger" one.
1019 Increment the reference count on each argument.
Guido van Rossume797ec12001-01-17 15:24:28 +00001020 Return value:
1021 -1 if an error occurred;
1022 0 if the coercion succeeded (and then the reference counts are increased);
1023 1 if no coercion is possible (and no error is raised).
Guido van Rossum5524a591995-01-10 15:26:20 +00001024*/
Guido van Rossum5524a591995-01-10 15:26:20 +00001025int
Fred Drake100814d2000-07-09 15:48:49 +00001026PyNumber_CoerceEx(PyObject **pv, PyObject **pw)
Guido van Rossum5524a591995-01-10 15:26:20 +00001027{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001028 register PyObject *v = *pv;
1029 register PyObject *w = *pw;
Guido van Rossum5524a591995-01-10 15:26:20 +00001030 int res;
1031
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001032 if (v->ob_type == w->ob_type && !PyInstance_Check(v)) {
1033 Py_INCREF(v);
1034 Py_INCREF(w);
Guido van Rossum5524a591995-01-10 15:26:20 +00001035 return 0;
1036 }
1037 if (v->ob_type->tp_as_number && v->ob_type->tp_as_number->nb_coerce) {
1038 res = (*v->ob_type->tp_as_number->nb_coerce)(pv, pw);
1039 if (res <= 0)
1040 return res;
1041 }
1042 if (w->ob_type->tp_as_number && w->ob_type->tp_as_number->nb_coerce) {
1043 res = (*w->ob_type->tp_as_number->nb_coerce)(pw, pv);
1044 if (res <= 0)
1045 return res;
1046 }
Guido van Rossum242c6421997-11-19 16:03:17 +00001047 return 1;
1048}
1049
Guido van Rossume797ec12001-01-17 15:24:28 +00001050/* Coerce two numeric types to the "larger" one.
1051 Increment the reference count on each argument.
1052 Return -1 and raise an exception if no coercion is possible
1053 (and then no reference count is incremented).
1054*/
Guido van Rossum242c6421997-11-19 16:03:17 +00001055int
Fred Drake100814d2000-07-09 15:48:49 +00001056PyNumber_Coerce(PyObject **pv, PyObject **pw)
Guido van Rossum242c6421997-11-19 16:03:17 +00001057{
1058 int err = PyNumber_CoerceEx(pv, pw);
1059 if (err <= 0)
1060 return err;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001061 PyErr_SetString(PyExc_TypeError, "number coercion failed");
Guido van Rossum5524a591995-01-10 15:26:20 +00001062 return -1;
1063}
1064
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001065
Guido van Rossum49b11fe1995-01-26 00:38:22 +00001066/* Test whether an object can be called */
1067
1068int
Fred Drake100814d2000-07-09 15:48:49 +00001069PyCallable_Check(PyObject *x)
Guido van Rossum49b11fe1995-01-26 00:38:22 +00001070{
1071 if (x == NULL)
1072 return 0;
1073 if (x->ob_type->tp_call != NULL ||
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001074 PyFunction_Check(x) ||
1075 PyMethod_Check(x) ||
1076 PyCFunction_Check(x) ||
1077 PyClass_Check(x))
Guido van Rossum49b11fe1995-01-26 00:38:22 +00001078 return 1;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001079 if (PyInstance_Check(x)) {
1080 PyObject *call = PyObject_GetAttrString(x, "__call__");
Guido van Rossum49b11fe1995-01-26 00:38:22 +00001081 if (call == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001082 PyErr_Clear();
Guido van Rossum49b11fe1995-01-26 00:38:22 +00001083 return 0;
1084 }
1085 /* Could test recursively but don't, for fear of endless
1086 recursion if some joker sets self.__call__ = self */
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001087 Py_DECREF(call);
Guido van Rossum49b11fe1995-01-26 00:38:22 +00001088 return 1;
1089 }
1090 return 0;
1091}
1092
1093
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001094/*
1095NoObject is usable as a non-NULL undefined value, used by the macro None.
1096There is (and should be!) no way to create other objects of this type,
Guido van Rossum3f5da241990-12-20 15:06:42 +00001097so there is exactly one (which is indestructible, by the way).
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001098*/
1099
Guido van Rossum0c182a11992-03-27 17:26:13 +00001100/* ARGSUSED */
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001101static PyObject *
Fred Drake100814d2000-07-09 15:48:49 +00001102none_repr(PyObject *op)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001103{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001104 return PyString_FromString("None");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001105}
1106
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001107static PyTypeObject PyNothing_Type = {
1108 PyObject_HEAD_INIT(&PyType_Type)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001109 0,
Guido van Rossum3f5da241990-12-20 15:06:42 +00001110 "None",
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001111 0,
1112 0,
1113 0, /*tp_dealloc*/ /*never called*/
Guido van Rossum7066dd71992-09-17 17:54:56 +00001114 0, /*tp_print*/
Guido van Rossum3f5da241990-12-20 15:06:42 +00001115 0, /*tp_getattr*/
1116 0, /*tp_setattr*/
1117 0, /*tp_compare*/
Guido van Rossum1d5735e1994-08-30 08:27:36 +00001118 (reprfunc)none_repr, /*tp_repr*/
Guido van Rossum3f5da241990-12-20 15:06:42 +00001119 0, /*tp_as_number*/
1120 0, /*tp_as_sequence*/
1121 0, /*tp_as_mapping*/
Guido van Rossum9bfef441993-03-29 10:43:31 +00001122 0, /*tp_hash */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001123};
1124
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001125PyObject _Py_NoneStruct = {
1126 PyObject_HEAD_INIT(&PyNothing_Type)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001127};
1128
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +00001129/* NotImplemented is an object that can be used to signal that an
1130 operation is not implemented for the given type combination. */
1131
1132static PyObject *
1133NotImplemented_repr(PyObject *op)
1134{
1135 return PyString_FromString("NotImplemented");
1136}
1137
1138static PyTypeObject PyNotImplemented_Type = {
1139 PyObject_HEAD_INIT(&PyType_Type)
1140 0,
1141 "NotImplemented",
1142 0,
1143 0,
1144 0, /*tp_dealloc*/ /*never called*/
1145 0, /*tp_print*/
1146 0, /*tp_getattr*/
1147 0, /*tp_setattr*/
1148 0, /*tp_compare*/
1149 (reprfunc)NotImplemented_repr, /*tp_repr*/
1150 0, /*tp_as_number*/
1151 0, /*tp_as_sequence*/
1152 0, /*tp_as_mapping*/
1153 0, /*tp_hash */
1154};
1155
1156PyObject _Py_NotImplementedStruct = {
1157 PyObject_HEAD_INIT(&PyNotImplemented_Type)
1158};
1159
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001160
Guido van Rossum84a90321996-05-22 16:34:47 +00001161#ifdef Py_TRACE_REFS
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001162
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001163static PyObject refchain = {&refchain, &refchain};
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001164
Guido van Rossumaacdc9d1996-08-12 21:32:12 +00001165void
Fred Drake100814d2000-07-09 15:48:49 +00001166_Py_ResetReferences(void)
Guido van Rossume09fb551997-08-05 02:04:34 +00001167{
1168 refchain._ob_prev = refchain._ob_next = &refchain;
1169 _Py_RefTotal = 0;
1170}
1171
1172void
Fred Drake100814d2000-07-09 15:48:49 +00001173_Py_NewReference(PyObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001174{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001175 _Py_RefTotal++;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001176 op->ob_refcnt = 1;
1177 op->_ob_next = refchain._ob_next;
1178 op->_ob_prev = &refchain;
1179 refchain._ob_next->_ob_prev = op;
1180 refchain._ob_next = op;
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +00001181#ifdef COUNT_ALLOCS
1182 inc_count(op->ob_type);
1183#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001184}
1185
Guido van Rossumaacdc9d1996-08-12 21:32:12 +00001186void
Fred Drake100814d2000-07-09 15:48:49 +00001187_Py_ForgetReference(register PyObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001188{
Guido van Rossumbffd6832000-01-20 22:32:56 +00001189#ifdef SLOW_UNREF_CHECK
Guido van Rossum4c08d552000-03-10 22:55:18 +00001190 register PyObject *p;
Guido van Rossumbffd6832000-01-20 22:32:56 +00001191#endif
Guido van Rossumd7047b31995-01-02 19:07:15 +00001192 if (op->ob_refcnt < 0)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001193 Py_FatalError("UNREF negative refcnt");
Guido van Rossum2e8f6141992-09-03 20:32:55 +00001194 if (op == &refchain ||
Guido van Rossumd7047b31995-01-02 19:07:15 +00001195 op->_ob_prev->_ob_next != op || op->_ob_next->_ob_prev != op)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001196 Py_FatalError("UNREF invalid object");
Guido van Rossum2e8f6141992-09-03 20:32:55 +00001197#ifdef SLOW_UNREF_CHECK
Guido van Rossum3f5da241990-12-20 15:06:42 +00001198 for (p = refchain._ob_next; p != &refchain; p = p->_ob_next) {
1199 if (p == op)
1200 break;
1201 }
Guido van Rossumd7047b31995-01-02 19:07:15 +00001202 if (p == &refchain) /* Not found */
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001203 Py_FatalError("UNREF unknown object");
Guido van Rossum2e8f6141992-09-03 20:32:55 +00001204#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001205 op->_ob_next->_ob_prev = op->_ob_prev;
1206 op->_ob_prev->_ob_next = op->_ob_next;
Guido van Rossum2e8f6141992-09-03 20:32:55 +00001207 op->_ob_next = op->_ob_prev = NULL;
Guido van Rossumd8953cb1995-04-06 14:46:26 +00001208#ifdef COUNT_ALLOCS
1209 op->ob_type->tp_free++;
1210#endif
Guido van Rossum3f5da241990-12-20 15:06:42 +00001211}
1212
Guido van Rossumaacdc9d1996-08-12 21:32:12 +00001213void
Fred Drake100814d2000-07-09 15:48:49 +00001214_Py_Dealloc(PyObject *op)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001215{
Guido van Rossum9776adf1994-09-07 14:36:45 +00001216 destructor dealloc = op->ob_type->tp_dealloc;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001217 _Py_ForgetReference(op);
Guido van Rossum9776adf1994-09-07 14:36:45 +00001218 (*dealloc)(op);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001219}
1220
Guido van Rossumaacdc9d1996-08-12 21:32:12 +00001221void
Fred Drake100814d2000-07-09 15:48:49 +00001222_Py_PrintReferences(FILE *fp)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001223{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001224 PyObject *op;
Guido van Rossume09fb551997-08-05 02:04:34 +00001225 fprintf(fp, "Remaining objects:\n");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001226 for (op = refchain._ob_next; op != &refchain; op = op->_ob_next) {
1227 fprintf(fp, "[%d] ", op->ob_refcnt);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001228 if (PyObject_Print(op, fp, 0) != 0)
1229 PyErr_Clear();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001230 putc('\n', fp);
1231 }
1232}
1233
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +00001234PyObject *
Fred Drake100814d2000-07-09 15:48:49 +00001235_Py_GetObjects(PyObject *self, PyObject *args)
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +00001236{
1237 int i, n;
1238 PyObject *t = NULL;
1239 PyObject *res, *op;
1240
1241 if (!PyArg_ParseTuple(args, "i|O", &n, &t))
1242 return NULL;
1243 op = refchain._ob_next;
1244 res = PyList_New(0);
1245 if (res == NULL)
1246 return NULL;
1247 for (i = 0; (n == 0 || i < n) && op != &refchain; i++) {
1248 while (op == self || op == args || op == res || op == t ||
1249 t != NULL && op->ob_type != (PyTypeObject *) t) {
1250 op = op->_ob_next;
1251 if (op == &refchain)
1252 return res;
1253 }
1254 if (PyList_Append(res, op) < 0) {
1255 Py_DECREF(res);
1256 return NULL;
1257 }
1258 op = op->_ob_next;
1259 }
1260 return res;
1261}
1262
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001263#endif
Guido van Rossum97ead3f1996-01-12 01:24:09 +00001264
1265
1266/* Hack to force loading of cobject.o */
Guido van Rossumda9c2711996-12-05 21:58:58 +00001267PyTypeObject *_Py_cobject_hack = &PyCObject_Type;
Guido van Rossum84a90321996-05-22 16:34:47 +00001268
1269
1270/* Hack to force loading of abstract.o */
Jeremy Hylton6253f832000-07-12 12:56:19 +00001271int (*_Py_abstract_hack)(PyObject *) = &PyObject_Size;
Guido van Rossume09fb551997-08-05 02:04:34 +00001272
1273
Andrew M. Kuchling1582a3a2000-08-16 12:27:23 +00001274/* Python's malloc wrappers (see pymem.h) */
Guido van Rossume09fb551997-08-05 02:04:34 +00001275
Thomas Wouters334fb892000-07-25 12:56:38 +00001276void *
Fred Drake100814d2000-07-09 15:48:49 +00001277PyMem_Malloc(size_t nbytes)
Guido van Rossume09fb551997-08-05 02:04:34 +00001278{
1279#if _PyMem_EXTRA > 0
1280 if (nbytes == 0)
1281 nbytes = _PyMem_EXTRA;
1282#endif
Guido van Rossumb18618d2000-05-03 23:44:39 +00001283 return PyMem_MALLOC(nbytes);
Guido van Rossume09fb551997-08-05 02:04:34 +00001284}
1285
Thomas Wouters334fb892000-07-25 12:56:38 +00001286void *
1287PyMem_Realloc(void *p, size_t nbytes)
Guido van Rossume09fb551997-08-05 02:04:34 +00001288{
1289#if _PyMem_EXTRA > 0
1290 if (nbytes == 0)
1291 nbytes = _PyMem_EXTRA;
1292#endif
Guido van Rossumb18618d2000-05-03 23:44:39 +00001293 return PyMem_REALLOC(p, nbytes);
Guido van Rossume09fb551997-08-05 02:04:34 +00001294}
1295
1296void
Thomas Wouters334fb892000-07-25 12:56:38 +00001297PyMem_Free(void *p)
Guido van Rossume09fb551997-08-05 02:04:34 +00001298{
Guido van Rossumb18618d2000-05-03 23:44:39 +00001299 PyMem_FREE(p);
1300}
1301
1302
1303/* Python's object malloc wrappers (see objimpl.h) */
1304
Thomas Wouters334fb892000-07-25 12:56:38 +00001305void *
Fred Drake100814d2000-07-09 15:48:49 +00001306PyObject_Malloc(size_t nbytes)
Guido van Rossumb18618d2000-05-03 23:44:39 +00001307{
1308 return PyObject_MALLOC(nbytes);
1309}
1310
Thomas Wouters334fb892000-07-25 12:56:38 +00001311void *
1312PyObject_Realloc(void *p, size_t nbytes)
Guido van Rossumb18618d2000-05-03 23:44:39 +00001313{
1314 return PyObject_REALLOC(p, nbytes);
1315}
1316
1317void
Thomas Wouters334fb892000-07-25 12:56:38 +00001318PyObject_Free(void *p)
Guido van Rossumb18618d2000-05-03 23:44:39 +00001319{
1320 PyObject_FREE(p);
Guido van Rossume09fb551997-08-05 02:04:34 +00001321}
Guido van Rossum86610361998-04-10 22:32:46 +00001322
1323
1324/* These methods are used to control infinite recursion in repr, str, print,
1325 etc. Container objects that may recursively contain themselves,
1326 e.g. builtin dictionaries and lists, should used Py_ReprEnter() and
1327 Py_ReprLeave() to avoid infinite recursion.
1328
1329 Py_ReprEnter() returns 0 the first time it is called for a particular
1330 object and 1 every time thereafter. It returns -1 if an exception
1331 occurred. Py_ReprLeave() has no return value.
1332
1333 See dictobject.c and listobject.c for examples of use.
1334*/
1335
1336#define KEY "Py_Repr"
1337
1338int
Fred Drake100814d2000-07-09 15:48:49 +00001339Py_ReprEnter(PyObject *obj)
Guido van Rossum86610361998-04-10 22:32:46 +00001340{
1341 PyObject *dict;
1342 PyObject *list;
1343 int i;
1344
1345 dict = PyThreadState_GetDict();
1346 if (dict == NULL)
1347 return -1;
1348 list = PyDict_GetItemString(dict, KEY);
1349 if (list == NULL) {
1350 list = PyList_New(0);
1351 if (list == NULL)
1352 return -1;
1353 if (PyDict_SetItemString(dict, KEY, list) < 0)
1354 return -1;
1355 Py_DECREF(list);
1356 }
1357 i = PyList_GET_SIZE(list);
1358 while (--i >= 0) {
1359 if (PyList_GET_ITEM(list, i) == obj)
1360 return 1;
1361 }
1362 PyList_Append(list, obj);
1363 return 0;
1364}
1365
1366void
Fred Drake100814d2000-07-09 15:48:49 +00001367Py_ReprLeave(PyObject *obj)
Guido van Rossum86610361998-04-10 22:32:46 +00001368{
1369 PyObject *dict;
1370 PyObject *list;
1371 int i;
1372
1373 dict = PyThreadState_GetDict();
Guido van Rossumeb909461998-04-11 15:17:34 +00001374 if (dict == NULL)
1375 return;
Guido van Rossum86610361998-04-10 22:32:46 +00001376 list = PyDict_GetItemString(dict, KEY);
Guido van Rossumeb909461998-04-11 15:17:34 +00001377 if (list == NULL || !PyList_Check(list))
1378 return;
Guido van Rossum86610361998-04-10 22:32:46 +00001379 i = PyList_GET_SIZE(list);
1380 /* Count backwards because we always expect obj to be list[-1] */
1381 while (--i >= 0) {
1382 if (PyList_GET_ITEM(list, i) == obj) {
1383 PyList_SetSlice(list, i, i + 1, NULL);
1384 break;
1385 }
1386 }
1387}
Guido van Rossumd724b232000-03-13 16:01:29 +00001388
1389/*
1390 trashcan
1391 CT 2k0130
1392 non-recursively destroy nested objects
1393
1394 CT 2k0223
1395 everything is now done in a macro.
1396
1397 CT 2k0305
1398 modified to use functions, after Tim Peter's suggestion.
1399
1400 CT 2k0309
1401 modified to restore a possible error.
Guido van Rossum13ff8eb2000-03-25 18:39:19 +00001402
1403 CT 2k0325
1404 added better safe than sorry check for threadstate
Guido van Rossume92e6102000-04-24 15:40:53 +00001405
1406 CT 2k0422
1407 complete rewrite. We now build a chain via ob_type
1408 and save the limited number of types in ob_refcnt.
1409 This is perfect since we don't need any memory.
1410 A patch for free-threading would need just a lock.
Guido van Rossumd724b232000-03-13 16:01:29 +00001411*/
1412
Guido van Rossume92e6102000-04-24 15:40:53 +00001413#define Py_TRASHCAN_TUPLE 1
1414#define Py_TRASHCAN_LIST 2
1415#define Py_TRASHCAN_DICT 3
1416#define Py_TRASHCAN_FRAME 4
1417#define Py_TRASHCAN_TRACEBACK 5
1418/* extend here if other objects want protection */
1419
Guido van Rossumd724b232000-03-13 16:01:29 +00001420int _PyTrash_delete_nesting = 0;
Guido van Rossume92e6102000-04-24 15:40:53 +00001421
Guido van Rossumd724b232000-03-13 16:01:29 +00001422PyObject * _PyTrash_delete_later = NULL;
1423
1424void
Fred Drake100814d2000-07-09 15:48:49 +00001425_PyTrash_deposit_object(PyObject *op)
Guido van Rossumd724b232000-03-13 16:01:29 +00001426{
Guido van Rossume92e6102000-04-24 15:40:53 +00001427 int typecode;
Guido van Rossum13ff8eb2000-03-25 18:39:19 +00001428
Guido van Rossume92e6102000-04-24 15:40:53 +00001429 if (PyTuple_Check(op))
1430 typecode = Py_TRASHCAN_TUPLE;
1431 else if (PyList_Check(op))
1432 typecode = Py_TRASHCAN_LIST;
1433 else if (PyDict_Check(op))
1434 typecode = Py_TRASHCAN_DICT;
1435 else if (PyFrame_Check(op))
1436 typecode = Py_TRASHCAN_FRAME;
1437 else if (PyTraceBack_Check(op))
1438 typecode = Py_TRASHCAN_TRACEBACK;
Moshe Zadkacf703f02000-08-04 15:36:13 +00001439 else /* We have a bug here -- those are the only types in GC */ {
1440 Py_FatalError("Type not supported in GC -- internal bug");
1441 return; /* pacify compiler -- execution never here */
1442 }
Guido van Rossume92e6102000-04-24 15:40:53 +00001443 op->ob_refcnt = typecode;
Guido van Rossumd724b232000-03-13 16:01:29 +00001444
Guido van Rossume92e6102000-04-24 15:40:53 +00001445 op->ob_type = (PyTypeObject*)_PyTrash_delete_later;
1446 _PyTrash_delete_later = op;
Guido van Rossumd724b232000-03-13 16:01:29 +00001447}
1448
1449void
Fred Drake100814d2000-07-09 15:48:49 +00001450_PyTrash_destroy_chain(void)
Guido van Rossumd724b232000-03-13 16:01:29 +00001451{
1452 while (_PyTrash_delete_later) {
1453 PyObject *shredder = _PyTrash_delete_later;
Guido van Rossume92e6102000-04-24 15:40:53 +00001454 _PyTrash_delete_later = (PyObject*) shredder->ob_type;
1455
1456 switch (shredder->ob_refcnt) {
1457 case Py_TRASHCAN_TUPLE:
1458 shredder->ob_type = &PyTuple_Type;
1459 break;
1460 case Py_TRASHCAN_LIST:
1461 shredder->ob_type = &PyList_Type;
1462 break;
1463 case Py_TRASHCAN_DICT:
1464 shredder->ob_type = &PyDict_Type;
1465 break;
1466 case Py_TRASHCAN_FRAME:
1467 shredder->ob_type = &PyFrame_Type;
1468 break;
1469 case Py_TRASHCAN_TRACEBACK:
1470 shredder->ob_type = &PyTraceBack_Type;
1471 break;
1472 }
1473 _Py_NewReference(shredder);
1474
Guido van Rossumd724b232000-03-13 16:01:29 +00001475 ++_PyTrash_delete_nesting;
1476 Py_DECREF(shredder);
1477 --_PyTrash_delete_nesting;
1478 }
1479}