blob: 7bf2a634488da328320762223a2fa108d342e401 [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{
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000379 static struct { int op; int outcome; } tries[3] = {
380 /* Try this operator, and if it is true, use this outcome: */
381 {Py_EQ, 0},
382 {Py_LT, -1},
383 {Py_GT, 1},
384 };
385 int i;
386
Guido van Rossume797ec12001-01-17 15:24:28 +0000387 if (v->ob_type->tp_richcompare == NULL &&
388 w->ob_type->tp_richcompare == NULL)
389 return 2; /* Shortcut */
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000390
391 for (i = 0; i < 3; i++) {
392 switch (try_rich_compare_bool(v, w, tries[i].op)) {
393 case -1:
394 return -1;
395 case 1:
396 return tries[i].outcome;
397 }
Guido van Rossume797ec12001-01-17 15:24:28 +0000398 }
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000399
400 return 2;
Guido van Rossume797ec12001-01-17 15:24:28 +0000401}
402
403/* Try a 3-way comparison, returning an int. Return:
404 -2 for an exception;
405 -1 if v < w;
406 0 if v == w;
407 1 if v > w;
408 2 if this particular 3-way comparison is not implemented or undefined.
409*/
410static int
411try_3way_compare(PyObject *v, PyObject *w)
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +0000412{
413 int c;
Guido van Rossume797ec12001-01-17 15:24:28 +0000414 cmpfunc f;
415
416 /* Comparisons involving instances are given to instance_compare,
417 which has the same return conventions as this function. */
418
419 if (PyInstance_Check(v))
420 return (*v->ob_type->tp_compare)(v, w);
421 if (PyInstance_Check(w))
422 return (*w->ob_type->tp_compare)(v, w);
423
424 /* If the types are equal, don't bother with coercions etc. */
425 if (v->ob_type == w->ob_type) {
426 if ((f = v->ob_type->tp_compare) == NULL)
427 return 2;
428 c = (*f)(v, w);
429 if (PyErr_Occurred())
430 return -2;
431 return c < 0 ? -1 : c > 0 ? 1 : 0;
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +0000432 }
Guido van Rossume797ec12001-01-17 15:24:28 +0000433
434 /* Try coercion; if it fails, give up */
435 c = PyNumber_CoerceEx(&v, &w);
436 if (c < 0)
437 return -2;
438 if (c > 0)
439 return 2;
440
441 /* Try v's comparison, if defined */
442 if ((f = v->ob_type->tp_compare) != NULL) {
443 c = (*f)(v, w);
444 Py_DECREF(v);
445 Py_DECREF(w);
446 if (PyErr_Occurred())
447 return -2;
448 return c < 0 ? -1 : c > 0 ? 1 : 0;
449 }
450
451 /* Try w's comparison, if defined */
452 if ((f = w->ob_type->tp_compare) != NULL) {
453 c = (*f)(w, v); /* swapped! */
454 Py_DECREF(v);
455 Py_DECREF(w);
456 if (PyErr_Occurred())
457 return -2;
458 return c < 0 ? 1 : c > 0 ? -1 : 0; /* negated! */
459 }
460
461 /* No comparison defined */
462 Py_DECREF(v);
463 Py_DECREF(w);
464 return 2;
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +0000465}
466
Guido van Rossume797ec12001-01-17 15:24:28 +0000467/* Final fallback 3-way comparison, returning an int. Return:
468 -2 if an error occurred;
469 -1 if v < w;
470 0 if v == w;
471 1 if v > w.
472*/
473static int
474default_3way_compare(PyObject *v, PyObject *w)
475{
476 int c;
477
478 if (v->ob_type == w->ob_type) {
479 /* same type: compare pointers */
480 void *vv = v;
481 void *ww = w;
482 return (vv < ww) ? -1 : (vv > ww) ? 1 : 0;
483 }
484
485 /* Special case for Unicode */
486 if (PyUnicode_Check(v) || PyUnicode_Check(w)) {
487 c = PyUnicode_Compare(v, w);
488 if (!PyErr_Occurred())
489 return c;
490 /* TypeErrors are ignored: if Unicode coercion fails due
491 to one of the arguments not having the right type, we
492 continue as defined by the coercion protocol (see
493 above). Luckily, decoding errors are reported as
494 ValueErrors and are not masked by this technique. */
495 if (!PyErr_ExceptionMatches(PyExc_TypeError))
496 return -2;
497 PyErr_Clear();
498 }
499
500 /* different type: compare type names */
501 c = strcmp(v->ob_type->tp_name, w->ob_type->tp_name);
502 return (c < 0) ? -1 : (c > 0) ? 1 : 0;
503}
504
505#define CHECK_TYPES(o) PyType_HasFeature((o)->ob_type, Py_TPFLAGS_CHECKTYPES)
506
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +0000507static int
Fred Drake100814d2000-07-09 15:48:49 +0000508do_cmp(PyObject *v, PyObject *w)
Guido van Rossum20566841995-01-12 11:26:10 +0000509{
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +0000510 int c;
511
Guido van Rossume797ec12001-01-17 15:24:28 +0000512 c = try_rich_to_3way_compare(v, w);
513 if (c < 2)
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +0000514 return c;
Guido van Rossume797ec12001-01-17 15:24:28 +0000515 c = try_3way_compare(v, w);
516 if (c < 2)
517 return c;
518 return default_3way_compare(v, w);
Guido van Rossum20566841995-01-12 11:26:10 +0000519}
520
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000521/* compare_nesting is incremented before calling compare (for
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000522 some types) and decremented on exit. If the count exceeds the
523 nesting limit, enable code to detect circular data structures.
524*/
Jack Jansend49cbe12000-08-22 21:52:51 +0000525#ifdef macintosh
526#define NESTING_LIMIT 60
527#else
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000528#define NESTING_LIMIT 500
Jack Jansend49cbe12000-08-22 21:52:51 +0000529#endif
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000530static int compare_nesting = 0;
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000531
532static PyObject*
Fred Drake100814d2000-07-09 15:48:49 +0000533get_inprogress_dict(void)
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000534{
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000535 static PyObject *key;
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000536 PyObject *tstate_dict, *inprogress;
537
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000538 if (key == NULL) {
539 key = PyString_InternFromString("cmp_state");
540 if (key == NULL)
541 return NULL;
542 }
543
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000544 tstate_dict = PyThreadState_GetDict();
545 if (tstate_dict == NULL) {
546 PyErr_BadInternalCall();
547 return NULL;
548 }
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000549 inprogress = PyDict_GetItem(tstate_dict, key);
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000550 if (inprogress == NULL) {
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000551 inprogress = PyDict_New();
552 if (inprogress == NULL)
553 return NULL;
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000554 if (PyDict_SetItem(tstate_dict, key, inprogress) == -1) {
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000555 Py_DECREF(inprogress);
556 return NULL;
557 }
Jeremy Hyltona251ea02000-06-09 16:20:39 +0000558 Py_DECREF(inprogress);
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000559 }
560 return inprogress;
561}
562
Marc-André Lemburgad7c98e2001-01-17 17:09:53 +0000563PyObject *
564PyObject_Unicode(PyObject *v)
565{
566 PyObject *res;
567
568 if (v == NULL)
569 res = PyString_FromString("<NULL>");
570 else if (PyUnicode_Check(v)) {
571 Py_INCREF(v);
572 return v;
573 }
574 else if (PyString_Check(v))
575 res = v;
576 else if (v->ob_type->tp_str != NULL)
577 res = (*v->ob_type->tp_str)(v);
578 else {
579 PyObject *func;
580 static PyObject *strstr;
581 if (strstr == NULL) {
582 strstr= PyString_InternFromString("__str__");
583 if (strstr == NULL)
584 return NULL;
585 }
586 if (!PyInstance_Check(v) ||
587 (func = PyObject_GetAttr(v, strstr)) == NULL) {
588 PyErr_Clear();
589 res = PyObject_Repr(v);
590 }
591 else {
592 res = PyEval_CallObject(func, (PyObject *)NULL);
593 Py_DECREF(func);
594 }
595 }
596 if (res == NULL)
597 return NULL;
598 if (!PyUnicode_Check(res)) {
599 PyObject* str;
600 str = PyUnicode_FromObject(res);
601 Py_DECREF(res);
602 if (str)
603 res = str;
604 else
605 return NULL;
606 }
607 return res;
608}
609
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000610static PyObject *
Fred Drake100814d2000-07-09 15:48:49 +0000611make_pair(PyObject *v, PyObject *w)
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000612{
613 PyObject *pair;
Barry Warsaw9d23a4e2000-08-18 05:01:19 +0000614 Py_uintptr_t iv = (Py_uintptr_t)v;
615 Py_uintptr_t iw = (Py_uintptr_t)w;
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000616
617 pair = PyTuple_New(2);
618 if (pair == NULL) {
619 return NULL;
620 }
Barry Warsaw9d23a4e2000-08-18 05:01:19 +0000621 if (iv <= iw) {
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000622 PyTuple_SET_ITEM(pair, 0, PyLong_FromVoidPtr((void *)v));
623 PyTuple_SET_ITEM(pair, 1, PyLong_FromVoidPtr((void *)w));
624 } else {
625 PyTuple_SET_ITEM(pair, 0, PyLong_FromVoidPtr((void *)w));
626 PyTuple_SET_ITEM(pair, 1, PyLong_FromVoidPtr((void *)v));
627 }
628 return pair;
629}
630
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000631int
Fred Drake100814d2000-07-09 15:48:49 +0000632PyObject_Compare(PyObject *v, PyObject *w)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000633{
Guido van Rossumcd5a5f61998-06-09 18:58:44 +0000634 PyTypeObject *vtp, *wtp;
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000635 int result;
636
Jack Jansend49cbe12000-08-22 21:52:51 +0000637#if defined(USE_STACKCHECK)
Guido van Rossum9c0a99e2000-08-30 15:53:50 +0000638 if (PyOS_CheckStack()) {
Jack Jansend49cbe12000-08-22 21:52:51 +0000639 PyErr_SetString(PyExc_MemoryError, "Stack overflow");
640 return -1;
641 }
642#endif
Guido van Rossumc8b6df91997-05-23 00:06:51 +0000643 if (v == NULL || w == NULL) {
644 PyErr_BadInternalCall();
645 return -1;
646 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000647 if (v == w)
648 return 0;
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +0000649 vtp = v->ob_type;
650 wtp = w->ob_type;
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000651 compare_nesting++;
652 if (compare_nesting > NESTING_LIMIT &&
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +0000653 (vtp->tp_as_mapping
654 || PyInstance_Check(v)
655 || (vtp->tp_as_sequence && !PyString_Check(v)))) {
656 /* try to detect circular data structures */
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000657 PyObject *inprogress, *pair;
658
659 inprogress = get_inprogress_dict();
660 if (inprogress == NULL) {
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +0000661 result = -1;
662 goto exit_cmp;
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000663 }
664 pair = make_pair(v, w);
665 if (PyDict_GetItem(inprogress, pair)) {
666 /* already comparing these objects. assume
667 they're equal until shown otherwise */
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000668 Py_DECREF(pair);
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +0000669 result = 0;
670 goto exit_cmp;
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000671 }
672 if (PyDict_SetItem(inprogress, pair, pair) == -1) {
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +0000673 result = -1;
674 goto exit_cmp;
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000675 }
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +0000676 result = do_cmp(v, w);
677 /* XXX DelItem shouldn't fail */
678 PyDict_DelItem(inprogress, pair);
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000679 Py_DECREF(pair);
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000680 }
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +0000681 else {
682 result = do_cmp(v, w);
683 }
684exit_cmp:
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000685 compare_nesting--;
Guido van Rossume797ec12001-01-17 15:24:28 +0000686 return result < 0 ? -1 : result;
687}
688
689static PyObject *
690try_3way_to_rich_compare(PyObject *v, PyObject *w, int op)
691{
692 int c;
693 PyObject *result;
694
695 c = try_3way_compare(v, w);
696 if (c <= -2)
697 return NULL;
698 if (c >= 2)
699 c = default_3way_compare(v, w);
700 switch (op) {
701 case Py_LT: c = c < 0; break;
702 case Py_LE: c = c <= 0; break;
703 case Py_EQ: c = c == 0; break;
704 case Py_NE: c = c != 0; break;
705 case Py_GT: c = c > 0; break;
706 case Py_GE: c = c >= 0; break;
707 }
708 result = c ? Py_True : Py_False;
709 Py_INCREF(result);
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000710 return result;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000711}
712
Guido van Rossume797ec12001-01-17 15:24:28 +0000713PyObject *
714do_richcmp(PyObject *v, PyObject *w, int op)
715{
716 PyObject *res;
717
718 res = try_rich_compare(v, w, op);
719 if (res != Py_NotImplemented)
720 return res;
721 Py_DECREF(res);
722
723 return try_3way_to_rich_compare(v, w, op);
724}
725
726PyObject *
727PyObject_RichCompare(PyObject *v, PyObject *w, int op)
728{
729 PyObject *res;
730
731 assert(Py_LT <= op && op <= Py_GE);
732
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000733 compare_nesting++;
734 if (compare_nesting > NESTING_LIMIT &&
735 (v->ob_type->tp_as_mapping
736 || PyInstance_Check(v)
737 || (v->ob_type->tp_as_sequence && !PyString_Check(v)))) {
738 /* try to detect circular data structures */
739 PyObject *inprogress, *pair;
740
741 inprogress = get_inprogress_dict();
742 if (inprogress == NULL) {
743 res = NULL;
744 goto exit_cmp;
Guido van Rossume797ec12001-01-17 15:24:28 +0000745 }
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000746 pair = make_pair(v, w);
747 if (PyDict_GetItem(inprogress, pair)) {
748 /* already comparing these objects. assume
749 they're equal until shown otherwise */
750 Py_DECREF(pair);
751 if (op == Py_EQ || op == Py_LE || op == Py_GE)
752 res = Py_True;
753 else
754 res = Py_False;
755 Py_INCREF(res);
756 goto exit_cmp;
757 }
758 if (PyDict_SetItem(inprogress, pair, pair) == -1) {
759 res = NULL;
760 goto exit_cmp;
761 }
762 res = do_richcmp(v, w, op);
763 /* XXX DelItem shouldn't fail */
764 PyDict_DelItem(inprogress, pair);
765 Py_DECREF(pair);
Guido van Rossume797ec12001-01-17 15:24:28 +0000766 }
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000767 else {
768 res = do_richcmp(v, w, op);
769 }
770 exit_cmp:
771 compare_nesting--;
Guido van Rossume797ec12001-01-17 15:24:28 +0000772 return res;
773}
774
775int
776PyObject_RichCompareBool(PyObject *v, PyObject *w, int op)
777{
778 PyObject *res = PyObject_RichCompare(v, w, op);
779 int ok;
780
781 if (res == NULL)
782 return -1;
783 ok = PyObject_IsTrue(res);
784 Py_DECREF(res);
785 return ok;
786}
Fred Drake13634cf2000-06-29 19:17:04 +0000787
788/* Set of hash utility functions to help maintaining the invariant that
789 iff a==b then hash(a)==hash(b)
790
791 All the utility functions (_Py_Hash*()) return "-1" to signify an error.
792*/
793
794long
Fred Drake100814d2000-07-09 15:48:49 +0000795_Py_HashDouble(double v)
Fred Drake13634cf2000-06-29 19:17:04 +0000796{
Tim Peters39dce292000-08-15 03:34:48 +0000797 double intpart, fractpart;
798 int expo;
799 long hipart;
800 long x; /* the final hash value */
801 /* This is designed so that Python numbers of different types
802 * that compare equal hash to the same value; otherwise comparisons
803 * of mapping keys will turn out weird.
804 */
805
806#ifdef MPW /* MPW C modf expects pointer to extended as second argument */
807{
808 extended e;
809 fractpart = modf(v, &e);
810 intpart = e;
811}
812#else
813 fractpart = modf(v, &intpart);
814#endif
815 if (fractpart == 0.0) {
816 /* This must return the same hash as an equal int or long. */
817 if (intpart > LONG_MAX || -intpart > LONG_MAX) {
818 /* Convert to long and use its hash. */
819 PyObject *plong; /* converted to Python long */
820 if (Py_IS_INFINITY(intpart))
821 /* can't convert to long int -- arbitrary */
822 v = v < 0 ? -271828.0 : 314159.0;
823 plong = PyLong_FromDouble(v);
824 if (plong == NULL)
825 return -1;
826 x = PyObject_Hash(plong);
827 Py_DECREF(plong);
828 return x;
829 }
830 /* Fits in a C long == a Python int, so is its own hash. */
831 x = (long)intpart;
832 if (x == -1)
833 x = -2;
834 return x;
835 }
836 /* The fractional part is non-zero, so we don't have to worry about
837 * making this match the hash of some other type.
838 * Use frexp to get at the bits in the double.
Fred Drake13634cf2000-06-29 19:17:04 +0000839 * Since the VAX D double format has 56 mantissa bits, which is the
840 * most of any double format in use, each of these parts may have as
841 * many as (but no more than) 56 significant bits.
Tim Peters39dce292000-08-15 03:34:48 +0000842 * So, assuming sizeof(long) >= 4, each part can be broken into two
843 * longs; frexp and multiplication are used to do that.
844 * Also, since the Cray double format has 15 exponent bits, which is
845 * the most of any double format in use, shifting the exponent field
846 * left by 15 won't overflow a long (again assuming sizeof(long) >= 4).
Fred Drake13634cf2000-06-29 19:17:04 +0000847 */
Tim Peters39dce292000-08-15 03:34:48 +0000848 v = frexp(v, &expo);
849 v *= 2147483648.0; /* 2**31 */
850 hipart = (long)v; /* take the top 32 bits */
851 v = (v - (double)hipart) * 2147483648.0; /* get the next 32 bits */
852 x = hipart + (long)v + (expo << 15);
853 if (x == -1)
854 x = -2;
855 return x;
Fred Drake13634cf2000-06-29 19:17:04 +0000856}
857
858long
Fred Drake100814d2000-07-09 15:48:49 +0000859_Py_HashPointer(void *p)
Fred Drake13634cf2000-06-29 19:17:04 +0000860{
861#if SIZEOF_LONG >= SIZEOF_VOID_P
862 return (long)p;
863#else
864 /* convert to a Python long and hash that */
865 PyObject* longobj;
866 long x;
867
868 if ((longobj = PyLong_FromVoidPtr(p)) == NULL) {
869 x = -1;
870 goto finally;
871 }
872 x = PyObject_Hash(longobj);
873
874finally:
875 Py_XDECREF(longobj);
876 return x;
877#endif
878}
879
880
Guido van Rossum9bfef441993-03-29 10:43:31 +0000881long
Fred Drake100814d2000-07-09 15:48:49 +0000882PyObject_Hash(PyObject *v)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000883{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000884 PyTypeObject *tp = v->ob_type;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000885 if (tp->tp_hash != NULL)
886 return (*tp->tp_hash)(v);
Fred Drake13634cf2000-06-29 19:17:04 +0000887 if (tp->tp_compare == NULL) {
888 return _Py_HashPointer(v); /* Use address as hash value */
889 }
Guido van Rossum9bfef441993-03-29 10:43:31 +0000890 /* If there's a cmp but no hash defined, the object can't be hashed */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000891 PyErr_SetString(PyExc_TypeError, "unhashable type");
Guido van Rossum9bfef441993-03-29 10:43:31 +0000892 return -1;
893}
894
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000895PyObject *
Fred Drake100814d2000-07-09 15:48:49 +0000896PyObject_GetAttrString(PyObject *v, char *name)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000897{
Guido van Rossumd8eb1b31996-08-09 20:52:03 +0000898 if (v->ob_type->tp_getattro != NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000899 PyObject *w, *res;
Guido van Rossumc6d06701997-01-18 07:57:16 +0000900 w = PyString_InternFromString(name);
Guido van Rossumd8eb1b31996-08-09 20:52:03 +0000901 if (w == NULL)
902 return NULL;
903 res = (*v->ob_type->tp_getattro)(v, w);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000904 Py_XDECREF(w);
Guido van Rossumd8eb1b31996-08-09 20:52:03 +0000905 return res;
906 }
907
Guido van Rossum3f5da241990-12-20 15:06:42 +0000908 if (v->ob_type->tp_getattr == NULL) {
Guido van Rossumdb935161998-01-19 22:16:36 +0000909 PyErr_Format(PyExc_AttributeError,
910 "'%.50s' object has no attribute '%.400s'",
911 v->ob_type->tp_name,
912 name);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000913 return NULL;
914 }
915 else {
916 return (*v->ob_type->tp_getattr)(v, name);
917 }
918}
919
920int
Fred Drake100814d2000-07-09 15:48:49 +0000921PyObject_HasAttrString(PyObject *v, char *name)
Guido van Rossumed18fdc1993-07-11 19:55:34 +0000922{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000923 PyObject *res = PyObject_GetAttrString(v, name);
Guido van Rossumed18fdc1993-07-11 19:55:34 +0000924 if (res != NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000925 Py_DECREF(res);
Guido van Rossumed18fdc1993-07-11 19:55:34 +0000926 return 1;
927 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000928 PyErr_Clear();
Guido van Rossumed18fdc1993-07-11 19:55:34 +0000929 return 0;
930}
931
932int
Fred Drake100814d2000-07-09 15:48:49 +0000933PyObject_SetAttrString(PyObject *v, char *name, PyObject *w)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000934{
Guido van Rossumd8eb1b31996-08-09 20:52:03 +0000935 if (v->ob_type->tp_setattro != NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000936 PyObject *s;
Guido van Rossumd8eb1b31996-08-09 20:52:03 +0000937 int res;
Guido van Rossumc6d06701997-01-18 07:57:16 +0000938 s = PyString_InternFromString(name);
Guido van Rossumd8eb1b31996-08-09 20:52:03 +0000939 if (s == NULL)
Guido van Rossumb7fc3041996-09-11 22:51:25 +0000940 return -1;
Guido van Rossumd8eb1b31996-08-09 20:52:03 +0000941 res = (*v->ob_type->tp_setattro)(v, s, w);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000942 Py_XDECREF(s);
Guido van Rossumd8eb1b31996-08-09 20:52:03 +0000943 return res;
944 }
945
Guido van Rossum3f5da241990-12-20 15:06:42 +0000946 if (v->ob_type->tp_setattr == NULL) {
947 if (v->ob_type->tp_getattr == NULL)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000948 PyErr_SetString(PyExc_TypeError,
Guido van Rossum3ea74121991-12-24 13:28:03 +0000949 "attribute-less object (assign or del)");
Guido van Rossum3f5da241990-12-20 15:06:42 +0000950 else
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000951 PyErr_SetString(PyExc_TypeError,
Guido van Rossum3ea74121991-12-24 13:28:03 +0000952 "object has read-only attributes");
Guido van Rossum73531a31990-12-20 23:12:40 +0000953 return -1;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000954 }
955 else {
956 return (*v->ob_type->tp_setattr)(v, name, w);
957 }
958}
959
Marc-André Lemburge44e5072000-09-18 16:20:57 +0000960/* Internal API needed by PyObject_GetAttr(): */
961extern
962PyObject *_PyUnicode_AsDefaultEncodedString(PyObject *unicode,
963 const char *errors);
964
Guido van Rossum98ff96a1997-05-20 18:34:44 +0000965PyObject *
Fred Drake100814d2000-07-09 15:48:49 +0000966PyObject_GetAttr(PyObject *v, PyObject *name)
Guido van Rossum98ff96a1997-05-20 18:34:44 +0000967{
Marc-André Lemburge44e5072000-09-18 16:20:57 +0000968 /* The Unicode to string conversion is done here because the
969 existing tp_getattro slots expect a string object as name
970 and we wouldn't want to break those. */
971 if (PyUnicode_Check(name)) {
972 name = _PyUnicode_AsDefaultEncodedString(name, NULL);
973 if (name == NULL)
974 return NULL;
975 }
Jeremy Hylton99a8f902000-06-23 14:36:32 +0000976
977 if (!PyString_Check(name)) {
978 PyErr_SetString(PyExc_TypeError,
979 "attribute name must be string");
980 return NULL;
981 }
Marc-André Lemburge44e5072000-09-18 16:20:57 +0000982 if (v->ob_type->tp_getattro != NULL)
983 return (*v->ob_type->tp_getattro)(v, name);
984 else
Jeremy Hylton99a8f902000-06-23 14:36:32 +0000985 return PyObject_GetAttrString(v, PyString_AS_STRING(name));
Guido van Rossum98ff96a1997-05-20 18:34:44 +0000986}
987
988int
Fred Drake100814d2000-07-09 15:48:49 +0000989PyObject_HasAttr(PyObject *v, PyObject *name)
Guido van Rossum98ff96a1997-05-20 18:34:44 +0000990{
991 PyObject *res = PyObject_GetAttr(v, name);
992 if (res != NULL) {
993 Py_DECREF(res);
994 return 1;
995 }
996 PyErr_Clear();
997 return 0;
998}
999
1000int
Fred Drake100814d2000-07-09 15:48:49 +00001001PyObject_SetAttr(PyObject *v, PyObject *name, PyObject *value)
Guido van Rossum98ff96a1997-05-20 18:34:44 +00001002{
1003 int err;
Marc-André Lemburge44e5072000-09-18 16:20:57 +00001004
1005 /* The Unicode to string conversion is done here because the
1006 existing tp_setattro slots expect a string object as name
1007 and we wouldn't want to break those. */
1008 if (PyUnicode_Check(name)) {
1009 name = PyUnicode_AsEncodedString(name, NULL, NULL);
1010 if (name == NULL)
1011 return -1;
Jeremy Hylton99a8f902000-06-23 14:36:32 +00001012 }
Marc-André Lemburge44e5072000-09-18 16:20:57 +00001013 else
1014 Py_INCREF(name);
1015
1016 if (!PyString_Check(name)){
Jeremy Hylton99a8f902000-06-23 14:36:32 +00001017 PyErr_SetString(PyExc_TypeError,
1018 "attribute name must be string");
1019 err = -1;
1020 }
Marc-André Lemburge44e5072000-09-18 16:20:57 +00001021 else {
1022 PyString_InternInPlace(&name);
1023 if (v->ob_type->tp_setattro != NULL)
1024 err = (*v->ob_type->tp_setattro)(v, name, value);
1025 else
1026 err = PyObject_SetAttrString(v,
1027 PyString_AS_STRING(name), value);
1028 }
1029
Guido van Rossum98ff96a1997-05-20 18:34:44 +00001030 Py_DECREF(name);
1031 return err;
1032}
1033
Guido van Rossum6ac258d1993-05-12 08:24:20 +00001034/* Test a value used as condition, e.g., in a for or if statement.
1035 Return -1 if an error occurred */
1036
1037int
Fred Drake100814d2000-07-09 15:48:49 +00001038PyObject_IsTrue(PyObject *v)
Guido van Rossum6ac258d1993-05-12 08:24:20 +00001039{
1040 int res;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001041 if (v == Py_None)
Guido van Rossum6ac258d1993-05-12 08:24:20 +00001042 res = 0;
Guido van Rossum1c4f4581998-05-22 00:53:24 +00001043 else if (v->ob_type->tp_as_number != NULL &&
1044 v->ob_type->tp_as_number->nb_nonzero != NULL)
Guido van Rossum6ac258d1993-05-12 08:24:20 +00001045 res = (*v->ob_type->tp_as_number->nb_nonzero)(v);
Guido van Rossum1c4f4581998-05-22 00:53:24 +00001046 else if (v->ob_type->tp_as_mapping != NULL &&
1047 v->ob_type->tp_as_mapping->mp_length != NULL)
Guido van Rossum6ac258d1993-05-12 08:24:20 +00001048 res = (*v->ob_type->tp_as_mapping->mp_length)(v);
Guido van Rossum1c4f4581998-05-22 00:53:24 +00001049 else if (v->ob_type->tp_as_sequence != NULL &&
1050 v->ob_type->tp_as_sequence->sq_length != NULL)
Guido van Rossum6ac258d1993-05-12 08:24:20 +00001051 res = (*v->ob_type->tp_as_sequence->sq_length)(v);
1052 else
1053 res = 1;
1054 if (res > 0)
1055 res = 1;
1056 return res;
1057}
1058
Guido van Rossumc3d3f961998-04-09 17:53:59 +00001059/* equivalent of 'not v'
1060 Return -1 if an error occurred */
1061
1062int
Fred Drake100814d2000-07-09 15:48:49 +00001063PyObject_Not(PyObject *v)
Guido van Rossumc3d3f961998-04-09 17:53:59 +00001064{
1065 int res;
1066 res = PyObject_IsTrue(v);
1067 if (res < 0)
1068 return res;
1069 return res == 0;
1070}
1071
Guido van Rossum5524a591995-01-10 15:26:20 +00001072/* Coerce two numeric types to the "larger" one.
1073 Increment the reference count on each argument.
Guido van Rossume797ec12001-01-17 15:24:28 +00001074 Return value:
1075 -1 if an error occurred;
1076 0 if the coercion succeeded (and then the reference counts are increased);
1077 1 if no coercion is possible (and no error is raised).
Guido van Rossum5524a591995-01-10 15:26:20 +00001078*/
Guido van Rossum5524a591995-01-10 15:26:20 +00001079int
Fred Drake100814d2000-07-09 15:48:49 +00001080PyNumber_CoerceEx(PyObject **pv, PyObject **pw)
Guido van Rossum5524a591995-01-10 15:26:20 +00001081{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001082 register PyObject *v = *pv;
1083 register PyObject *w = *pw;
Guido van Rossum5524a591995-01-10 15:26:20 +00001084 int res;
1085
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001086 if (v->ob_type == w->ob_type && !PyInstance_Check(v)) {
1087 Py_INCREF(v);
1088 Py_INCREF(w);
Guido van Rossum5524a591995-01-10 15:26:20 +00001089 return 0;
1090 }
1091 if (v->ob_type->tp_as_number && v->ob_type->tp_as_number->nb_coerce) {
1092 res = (*v->ob_type->tp_as_number->nb_coerce)(pv, pw);
1093 if (res <= 0)
1094 return res;
1095 }
1096 if (w->ob_type->tp_as_number && w->ob_type->tp_as_number->nb_coerce) {
1097 res = (*w->ob_type->tp_as_number->nb_coerce)(pw, pv);
1098 if (res <= 0)
1099 return res;
1100 }
Guido van Rossum242c6421997-11-19 16:03:17 +00001101 return 1;
1102}
1103
Guido van Rossume797ec12001-01-17 15:24:28 +00001104/* Coerce two numeric types to the "larger" one.
1105 Increment the reference count on each argument.
1106 Return -1 and raise an exception if no coercion is possible
1107 (and then no reference count is incremented).
1108*/
Guido van Rossum242c6421997-11-19 16:03:17 +00001109int
Fred Drake100814d2000-07-09 15:48:49 +00001110PyNumber_Coerce(PyObject **pv, PyObject **pw)
Guido van Rossum242c6421997-11-19 16:03:17 +00001111{
1112 int err = PyNumber_CoerceEx(pv, pw);
1113 if (err <= 0)
1114 return err;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001115 PyErr_SetString(PyExc_TypeError, "number coercion failed");
Guido van Rossum5524a591995-01-10 15:26:20 +00001116 return -1;
1117}
1118
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001119
Guido van Rossum49b11fe1995-01-26 00:38:22 +00001120/* Test whether an object can be called */
1121
1122int
Fred Drake100814d2000-07-09 15:48:49 +00001123PyCallable_Check(PyObject *x)
Guido van Rossum49b11fe1995-01-26 00:38:22 +00001124{
1125 if (x == NULL)
1126 return 0;
1127 if (x->ob_type->tp_call != NULL ||
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001128 PyFunction_Check(x) ||
1129 PyMethod_Check(x) ||
1130 PyCFunction_Check(x) ||
1131 PyClass_Check(x))
Guido van Rossum49b11fe1995-01-26 00:38:22 +00001132 return 1;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001133 if (PyInstance_Check(x)) {
1134 PyObject *call = PyObject_GetAttrString(x, "__call__");
Guido van Rossum49b11fe1995-01-26 00:38:22 +00001135 if (call == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001136 PyErr_Clear();
Guido van Rossum49b11fe1995-01-26 00:38:22 +00001137 return 0;
1138 }
1139 /* Could test recursively but don't, for fear of endless
1140 recursion if some joker sets self.__call__ = self */
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001141 Py_DECREF(call);
Guido van Rossum49b11fe1995-01-26 00:38:22 +00001142 return 1;
1143 }
1144 return 0;
1145}
1146
1147
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001148/*
1149NoObject is usable as a non-NULL undefined value, used by the macro None.
1150There is (and should be!) no way to create other objects of this type,
Guido van Rossum3f5da241990-12-20 15:06:42 +00001151so there is exactly one (which is indestructible, by the way).
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001152*/
1153
Guido van Rossum0c182a11992-03-27 17:26:13 +00001154/* ARGSUSED */
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001155static PyObject *
Fred Drake100814d2000-07-09 15:48:49 +00001156none_repr(PyObject *op)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001157{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001158 return PyString_FromString("None");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001159}
1160
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001161static PyTypeObject PyNothing_Type = {
1162 PyObject_HEAD_INIT(&PyType_Type)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001163 0,
Guido van Rossum3f5da241990-12-20 15:06:42 +00001164 "None",
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001165 0,
1166 0,
1167 0, /*tp_dealloc*/ /*never called*/
Guido van Rossum7066dd71992-09-17 17:54:56 +00001168 0, /*tp_print*/
Guido van Rossum3f5da241990-12-20 15:06:42 +00001169 0, /*tp_getattr*/
1170 0, /*tp_setattr*/
1171 0, /*tp_compare*/
Guido van Rossum1d5735e1994-08-30 08:27:36 +00001172 (reprfunc)none_repr, /*tp_repr*/
Guido van Rossum3f5da241990-12-20 15:06:42 +00001173 0, /*tp_as_number*/
1174 0, /*tp_as_sequence*/
1175 0, /*tp_as_mapping*/
Guido van Rossum9bfef441993-03-29 10:43:31 +00001176 0, /*tp_hash */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001177};
1178
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001179PyObject _Py_NoneStruct = {
1180 PyObject_HEAD_INIT(&PyNothing_Type)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001181};
1182
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +00001183/* NotImplemented is an object that can be used to signal that an
1184 operation is not implemented for the given type combination. */
1185
1186static PyObject *
1187NotImplemented_repr(PyObject *op)
1188{
1189 return PyString_FromString("NotImplemented");
1190}
1191
1192static PyTypeObject PyNotImplemented_Type = {
1193 PyObject_HEAD_INIT(&PyType_Type)
1194 0,
1195 "NotImplemented",
1196 0,
1197 0,
1198 0, /*tp_dealloc*/ /*never called*/
1199 0, /*tp_print*/
1200 0, /*tp_getattr*/
1201 0, /*tp_setattr*/
1202 0, /*tp_compare*/
1203 (reprfunc)NotImplemented_repr, /*tp_repr*/
1204 0, /*tp_as_number*/
1205 0, /*tp_as_sequence*/
1206 0, /*tp_as_mapping*/
1207 0, /*tp_hash */
1208};
1209
1210PyObject _Py_NotImplementedStruct = {
1211 PyObject_HEAD_INIT(&PyNotImplemented_Type)
1212};
1213
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001214
Guido van Rossum84a90321996-05-22 16:34:47 +00001215#ifdef Py_TRACE_REFS
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001216
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001217static PyObject refchain = {&refchain, &refchain};
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001218
Guido van Rossumaacdc9d1996-08-12 21:32:12 +00001219void
Fred Drake100814d2000-07-09 15:48:49 +00001220_Py_ResetReferences(void)
Guido van Rossume09fb551997-08-05 02:04:34 +00001221{
1222 refchain._ob_prev = refchain._ob_next = &refchain;
1223 _Py_RefTotal = 0;
1224}
1225
1226void
Fred Drake100814d2000-07-09 15:48:49 +00001227_Py_NewReference(PyObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001228{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001229 _Py_RefTotal++;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001230 op->ob_refcnt = 1;
1231 op->_ob_next = refchain._ob_next;
1232 op->_ob_prev = &refchain;
1233 refchain._ob_next->_ob_prev = op;
1234 refchain._ob_next = op;
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +00001235#ifdef COUNT_ALLOCS
1236 inc_count(op->ob_type);
1237#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001238}
1239
Guido van Rossumaacdc9d1996-08-12 21:32:12 +00001240void
Fred Drake100814d2000-07-09 15:48:49 +00001241_Py_ForgetReference(register PyObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001242{
Guido van Rossumbffd6832000-01-20 22:32:56 +00001243#ifdef SLOW_UNREF_CHECK
Guido van Rossum4c08d552000-03-10 22:55:18 +00001244 register PyObject *p;
Guido van Rossumbffd6832000-01-20 22:32:56 +00001245#endif
Guido van Rossumd7047b31995-01-02 19:07:15 +00001246 if (op->ob_refcnt < 0)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001247 Py_FatalError("UNREF negative refcnt");
Guido van Rossum2e8f6141992-09-03 20:32:55 +00001248 if (op == &refchain ||
Guido van Rossumd7047b31995-01-02 19:07:15 +00001249 op->_ob_prev->_ob_next != op || op->_ob_next->_ob_prev != op)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001250 Py_FatalError("UNREF invalid object");
Guido van Rossum2e8f6141992-09-03 20:32:55 +00001251#ifdef SLOW_UNREF_CHECK
Guido van Rossum3f5da241990-12-20 15:06:42 +00001252 for (p = refchain._ob_next; p != &refchain; p = p->_ob_next) {
1253 if (p == op)
1254 break;
1255 }
Guido van Rossumd7047b31995-01-02 19:07:15 +00001256 if (p == &refchain) /* Not found */
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001257 Py_FatalError("UNREF unknown object");
Guido van Rossum2e8f6141992-09-03 20:32:55 +00001258#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001259 op->_ob_next->_ob_prev = op->_ob_prev;
1260 op->_ob_prev->_ob_next = op->_ob_next;
Guido van Rossum2e8f6141992-09-03 20:32:55 +00001261 op->_ob_next = op->_ob_prev = NULL;
Guido van Rossumd8953cb1995-04-06 14:46:26 +00001262#ifdef COUNT_ALLOCS
1263 op->ob_type->tp_free++;
1264#endif
Guido van Rossum3f5da241990-12-20 15:06:42 +00001265}
1266
Guido van Rossumaacdc9d1996-08-12 21:32:12 +00001267void
Fred Drake100814d2000-07-09 15:48:49 +00001268_Py_Dealloc(PyObject *op)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001269{
Guido van Rossum9776adf1994-09-07 14:36:45 +00001270 destructor dealloc = op->ob_type->tp_dealloc;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001271 _Py_ForgetReference(op);
Guido van Rossum9776adf1994-09-07 14:36:45 +00001272 (*dealloc)(op);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001273}
1274
Guido van Rossumaacdc9d1996-08-12 21:32:12 +00001275void
Fred Drake100814d2000-07-09 15:48:49 +00001276_Py_PrintReferences(FILE *fp)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001277{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001278 PyObject *op;
Guido van Rossume09fb551997-08-05 02:04:34 +00001279 fprintf(fp, "Remaining objects:\n");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001280 for (op = refchain._ob_next; op != &refchain; op = op->_ob_next) {
1281 fprintf(fp, "[%d] ", op->ob_refcnt);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001282 if (PyObject_Print(op, fp, 0) != 0)
1283 PyErr_Clear();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001284 putc('\n', fp);
1285 }
1286}
1287
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +00001288PyObject *
Fred Drake100814d2000-07-09 15:48:49 +00001289_Py_GetObjects(PyObject *self, PyObject *args)
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +00001290{
1291 int i, n;
1292 PyObject *t = NULL;
1293 PyObject *res, *op;
1294
1295 if (!PyArg_ParseTuple(args, "i|O", &n, &t))
1296 return NULL;
1297 op = refchain._ob_next;
1298 res = PyList_New(0);
1299 if (res == NULL)
1300 return NULL;
1301 for (i = 0; (n == 0 || i < n) && op != &refchain; i++) {
1302 while (op == self || op == args || op == res || op == t ||
1303 t != NULL && op->ob_type != (PyTypeObject *) t) {
1304 op = op->_ob_next;
1305 if (op == &refchain)
1306 return res;
1307 }
1308 if (PyList_Append(res, op) < 0) {
1309 Py_DECREF(res);
1310 return NULL;
1311 }
1312 op = op->_ob_next;
1313 }
1314 return res;
1315}
1316
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001317#endif
Guido van Rossum97ead3f1996-01-12 01:24:09 +00001318
1319
1320/* Hack to force loading of cobject.o */
Guido van Rossumda9c2711996-12-05 21:58:58 +00001321PyTypeObject *_Py_cobject_hack = &PyCObject_Type;
Guido van Rossum84a90321996-05-22 16:34:47 +00001322
1323
1324/* Hack to force loading of abstract.o */
Jeremy Hylton6253f832000-07-12 12:56:19 +00001325int (*_Py_abstract_hack)(PyObject *) = &PyObject_Size;
Guido van Rossume09fb551997-08-05 02:04:34 +00001326
1327
Andrew M. Kuchling1582a3a2000-08-16 12:27:23 +00001328/* Python's malloc wrappers (see pymem.h) */
Guido van Rossume09fb551997-08-05 02:04:34 +00001329
Thomas Wouters334fb892000-07-25 12:56:38 +00001330void *
Fred Drake100814d2000-07-09 15:48:49 +00001331PyMem_Malloc(size_t nbytes)
Guido van Rossume09fb551997-08-05 02:04:34 +00001332{
1333#if _PyMem_EXTRA > 0
1334 if (nbytes == 0)
1335 nbytes = _PyMem_EXTRA;
1336#endif
Guido van Rossumb18618d2000-05-03 23:44:39 +00001337 return PyMem_MALLOC(nbytes);
Guido van Rossume09fb551997-08-05 02:04:34 +00001338}
1339
Thomas Wouters334fb892000-07-25 12:56:38 +00001340void *
1341PyMem_Realloc(void *p, size_t nbytes)
Guido van Rossume09fb551997-08-05 02:04:34 +00001342{
1343#if _PyMem_EXTRA > 0
1344 if (nbytes == 0)
1345 nbytes = _PyMem_EXTRA;
1346#endif
Guido van Rossumb18618d2000-05-03 23:44:39 +00001347 return PyMem_REALLOC(p, nbytes);
Guido van Rossume09fb551997-08-05 02:04:34 +00001348}
1349
1350void
Thomas Wouters334fb892000-07-25 12:56:38 +00001351PyMem_Free(void *p)
Guido van Rossume09fb551997-08-05 02:04:34 +00001352{
Guido van Rossumb18618d2000-05-03 23:44:39 +00001353 PyMem_FREE(p);
1354}
1355
1356
1357/* Python's object malloc wrappers (see objimpl.h) */
1358
Thomas Wouters334fb892000-07-25 12:56:38 +00001359void *
Fred Drake100814d2000-07-09 15:48:49 +00001360PyObject_Malloc(size_t nbytes)
Guido van Rossumb18618d2000-05-03 23:44:39 +00001361{
1362 return PyObject_MALLOC(nbytes);
1363}
1364
Thomas Wouters334fb892000-07-25 12:56:38 +00001365void *
1366PyObject_Realloc(void *p, size_t nbytes)
Guido van Rossumb18618d2000-05-03 23:44:39 +00001367{
1368 return PyObject_REALLOC(p, nbytes);
1369}
1370
1371void
Thomas Wouters334fb892000-07-25 12:56:38 +00001372PyObject_Free(void *p)
Guido van Rossumb18618d2000-05-03 23:44:39 +00001373{
1374 PyObject_FREE(p);
Guido van Rossume09fb551997-08-05 02:04:34 +00001375}
Guido van Rossum86610361998-04-10 22:32:46 +00001376
1377
1378/* These methods are used to control infinite recursion in repr, str, print,
1379 etc. Container objects that may recursively contain themselves,
1380 e.g. builtin dictionaries and lists, should used Py_ReprEnter() and
1381 Py_ReprLeave() to avoid infinite recursion.
1382
1383 Py_ReprEnter() returns 0 the first time it is called for a particular
1384 object and 1 every time thereafter. It returns -1 if an exception
1385 occurred. Py_ReprLeave() has no return value.
1386
1387 See dictobject.c and listobject.c for examples of use.
1388*/
1389
1390#define KEY "Py_Repr"
1391
1392int
Fred Drake100814d2000-07-09 15:48:49 +00001393Py_ReprEnter(PyObject *obj)
Guido van Rossum86610361998-04-10 22:32:46 +00001394{
1395 PyObject *dict;
1396 PyObject *list;
1397 int i;
1398
1399 dict = PyThreadState_GetDict();
1400 if (dict == NULL)
1401 return -1;
1402 list = PyDict_GetItemString(dict, KEY);
1403 if (list == NULL) {
1404 list = PyList_New(0);
1405 if (list == NULL)
1406 return -1;
1407 if (PyDict_SetItemString(dict, KEY, list) < 0)
1408 return -1;
1409 Py_DECREF(list);
1410 }
1411 i = PyList_GET_SIZE(list);
1412 while (--i >= 0) {
1413 if (PyList_GET_ITEM(list, i) == obj)
1414 return 1;
1415 }
1416 PyList_Append(list, obj);
1417 return 0;
1418}
1419
1420void
Fred Drake100814d2000-07-09 15:48:49 +00001421Py_ReprLeave(PyObject *obj)
Guido van Rossum86610361998-04-10 22:32:46 +00001422{
1423 PyObject *dict;
1424 PyObject *list;
1425 int i;
1426
1427 dict = PyThreadState_GetDict();
Guido van Rossumeb909461998-04-11 15:17:34 +00001428 if (dict == NULL)
1429 return;
Guido van Rossum86610361998-04-10 22:32:46 +00001430 list = PyDict_GetItemString(dict, KEY);
Guido van Rossumeb909461998-04-11 15:17:34 +00001431 if (list == NULL || !PyList_Check(list))
1432 return;
Guido van Rossum86610361998-04-10 22:32:46 +00001433 i = PyList_GET_SIZE(list);
1434 /* Count backwards because we always expect obj to be list[-1] */
1435 while (--i >= 0) {
1436 if (PyList_GET_ITEM(list, i) == obj) {
1437 PyList_SetSlice(list, i, i + 1, NULL);
1438 break;
1439 }
1440 }
1441}
Guido van Rossumd724b232000-03-13 16:01:29 +00001442
1443/*
1444 trashcan
1445 CT 2k0130
1446 non-recursively destroy nested objects
1447
1448 CT 2k0223
1449 everything is now done in a macro.
1450
1451 CT 2k0305
1452 modified to use functions, after Tim Peter's suggestion.
1453
1454 CT 2k0309
1455 modified to restore a possible error.
Guido van Rossum13ff8eb2000-03-25 18:39:19 +00001456
1457 CT 2k0325
1458 added better safe than sorry check for threadstate
Guido van Rossume92e6102000-04-24 15:40:53 +00001459
1460 CT 2k0422
1461 complete rewrite. We now build a chain via ob_type
1462 and save the limited number of types in ob_refcnt.
1463 This is perfect since we don't need any memory.
1464 A patch for free-threading would need just a lock.
Guido van Rossumd724b232000-03-13 16:01:29 +00001465*/
1466
Guido van Rossume92e6102000-04-24 15:40:53 +00001467#define Py_TRASHCAN_TUPLE 1
1468#define Py_TRASHCAN_LIST 2
1469#define Py_TRASHCAN_DICT 3
1470#define Py_TRASHCAN_FRAME 4
1471#define Py_TRASHCAN_TRACEBACK 5
1472/* extend here if other objects want protection */
1473
Guido van Rossumd724b232000-03-13 16:01:29 +00001474int _PyTrash_delete_nesting = 0;
Guido van Rossume92e6102000-04-24 15:40:53 +00001475
Guido van Rossumd724b232000-03-13 16:01:29 +00001476PyObject * _PyTrash_delete_later = NULL;
1477
1478void
Fred Drake100814d2000-07-09 15:48:49 +00001479_PyTrash_deposit_object(PyObject *op)
Guido van Rossumd724b232000-03-13 16:01:29 +00001480{
Guido van Rossume92e6102000-04-24 15:40:53 +00001481 int typecode;
Guido van Rossum13ff8eb2000-03-25 18:39:19 +00001482
Guido van Rossume92e6102000-04-24 15:40:53 +00001483 if (PyTuple_Check(op))
1484 typecode = Py_TRASHCAN_TUPLE;
1485 else if (PyList_Check(op))
1486 typecode = Py_TRASHCAN_LIST;
1487 else if (PyDict_Check(op))
1488 typecode = Py_TRASHCAN_DICT;
1489 else if (PyFrame_Check(op))
1490 typecode = Py_TRASHCAN_FRAME;
1491 else if (PyTraceBack_Check(op))
1492 typecode = Py_TRASHCAN_TRACEBACK;
Moshe Zadkacf703f02000-08-04 15:36:13 +00001493 else /* We have a bug here -- those are the only types in GC */ {
1494 Py_FatalError("Type not supported in GC -- internal bug");
1495 return; /* pacify compiler -- execution never here */
1496 }
Guido van Rossume92e6102000-04-24 15:40:53 +00001497 op->ob_refcnt = typecode;
Guido van Rossumd724b232000-03-13 16:01:29 +00001498
Guido van Rossume92e6102000-04-24 15:40:53 +00001499 op->ob_type = (PyTypeObject*)_PyTrash_delete_later;
1500 _PyTrash_delete_later = op;
Guido van Rossumd724b232000-03-13 16:01:29 +00001501}
1502
1503void
Fred Drake100814d2000-07-09 15:48:49 +00001504_PyTrash_destroy_chain(void)
Guido van Rossumd724b232000-03-13 16:01:29 +00001505{
1506 while (_PyTrash_delete_later) {
1507 PyObject *shredder = _PyTrash_delete_later;
Guido van Rossume92e6102000-04-24 15:40:53 +00001508 _PyTrash_delete_later = (PyObject*) shredder->ob_type;
1509
1510 switch (shredder->ob_refcnt) {
1511 case Py_TRASHCAN_TUPLE:
1512 shredder->ob_type = &PyTuple_Type;
1513 break;
1514 case Py_TRASHCAN_LIST:
1515 shredder->ob_type = &PyList_Type;
1516 break;
1517 case Py_TRASHCAN_DICT:
1518 shredder->ob_type = &PyDict_Type;
1519 break;
1520 case Py_TRASHCAN_FRAME:
1521 shredder->ob_type = &PyFrame_Type;
1522 break;
1523 case Py_TRASHCAN_TRACEBACK:
1524 shredder->ob_type = &PyTraceBack_Type;
1525 break;
1526 }
1527 _Py_NewReference(shredder);
1528
Guido van Rossumd724b232000-03-13 16:01:29 +00001529 ++_PyTrash_delete_nesting;
1530 Py_DECREF(shredder);
1531 --_PyTrash_delete_nesting;
1532 }
1533}