blob: 365d1313f7966e3469caba99eba4b651581b2e60 [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 Rossum393661d2001-08-31 17:40:15 +000019DL_IMPORT(int) Py_DivisionWarningFlag;
20
Guido van Rossum3f5da241990-12-20 15:06:42 +000021/* Object allocation routines used by NEWOBJ and NEWVAROBJ macros.
22 These are used by the individual routines for object creation.
23 Do not call them otherwise, they do not initialize the object! */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000024
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +000025#ifdef COUNT_ALLOCS
Guido van Rossumc0b618a1997-05-02 03:12:38 +000026static PyTypeObject *type_list;
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +000027extern int tuple_zero_allocs, fast_tuple_allocs;
28extern int quick_int_allocs, quick_neg_int_allocs;
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +000029extern int null_strings, one_strings;
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +000030void
Fred Drake100814d2000-07-09 15:48:49 +000031dump_counts(void)
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +000032{
Guido van Rossumc0b618a1997-05-02 03:12:38 +000033 PyTypeObject *tp;
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +000034
35 for (tp = type_list; tp; tp = tp->tp_next)
Sjoerd Mullender52c1f511993-10-25 08:40:52 +000036 fprintf(stderr, "%s alloc'd: %d, freed: %d, max in use: %d\n",
Tim Peters6d6c1a32001-08-02 04:15:00 +000037 tp->tp_name, tp->tp_allocs, tp->tp_frees,
Sjoerd Mullender52c1f511993-10-25 08:40:52 +000038 tp->tp_maxalloc);
39 fprintf(stderr, "fast tuple allocs: %d, empty: %d\n",
40 fast_tuple_allocs, tuple_zero_allocs);
41 fprintf(stderr, "fast int allocs: pos: %d, neg: %d\n",
42 quick_int_allocs, quick_neg_int_allocs);
43 fprintf(stderr, "null strings: %d, 1-strings: %d\n",
44 null_strings, one_strings);
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +000045}
46
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +000047PyObject *
Fred Drake100814d2000-07-09 15:48:49 +000048get_counts(void)
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +000049{
50 PyTypeObject *tp;
51 PyObject *result;
52 PyObject *v;
53
54 result = PyList_New(0);
55 if (result == NULL)
56 return NULL;
57 for (tp = type_list; tp; tp = tp->tp_next) {
Tim Peters6d6c1a32001-08-02 04:15:00 +000058 v = Py_BuildValue("(siii)", tp->tp_name, tp->tp_allocs,
59 tp->tp_frees, tp->tp_maxalloc);
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +000060 if (v == NULL) {
61 Py_DECREF(result);
62 return NULL;
63 }
64 if (PyList_Append(result, v) < 0) {
65 Py_DECREF(v);
66 Py_DECREF(result);
67 return NULL;
68 }
69 Py_DECREF(v);
70 }
71 return result;
72}
73
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +000074void
Fred Drake100814d2000-07-09 15:48:49 +000075inc_count(PyTypeObject *tp)
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +000076{
Tim Peters6d6c1a32001-08-02 04:15:00 +000077 if (tp->tp_allocs == 0) {
Guido van Rossumd8953cb1995-04-06 14:46:26 +000078 /* first time; insert in linked list */
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +000079 if (tp->tp_next != NULL) /* sanity check */
Guido van Rossumc0b618a1997-05-02 03:12:38 +000080 Py_FatalError("XXX inc_count sanity check");
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +000081 tp->tp_next = type_list;
82 type_list = tp;
83 }
Tim Peters6d6c1a32001-08-02 04:15:00 +000084 tp->tp_allocs++;
85 if (tp->tp_allocs - tp->tp_frees > tp->tp_maxalloc)
86 tp->tp_maxalloc = tp->tp_allocs - tp->tp_frees;
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +000087}
88#endif
89
Guido van Rossumc0b618a1997-05-02 03:12:38 +000090PyObject *
Fred Drake100814d2000-07-09 15:48:49 +000091PyObject_Init(PyObject *op, PyTypeObject *tp)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000092{
Guido van Rossumb18618d2000-05-03 23:44:39 +000093 if (op == NULL) {
94 PyErr_SetString(PyExc_SystemError,
95 "NULL object passed to PyObject_Init");
96 return op;
97 }
98 /* Any changes should be reflected in PyObject_INIT (objimpl.h) */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000099 op->ob_type = tp;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000100 _Py_NewReference(op);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000101 return op;
102}
103
Guido van Rossumb18618d2000-05-03 23:44:39 +0000104PyVarObject *
Fred Drake100814d2000-07-09 15:48:49 +0000105PyObject_InitVar(PyVarObject *op, PyTypeObject *tp, int size)
Guido van Rossumb18618d2000-05-03 23:44:39 +0000106{
107 if (op == NULL) {
108 PyErr_SetString(PyExc_SystemError,
109 "NULL object passed to PyObject_InitVar");
110 return op;
111 }
112 /* Any changes should be reflected in PyObject_INIT_VAR */
113 op->ob_size = size;
114 op->ob_type = tp;
115 _Py_NewReference((PyObject *)op);
116 return op;
117}
118
119PyObject *
Fred Drake100814d2000-07-09 15:48:49 +0000120_PyObject_New(PyTypeObject *tp)
Guido van Rossumb18618d2000-05-03 23:44:39 +0000121{
122 PyObject *op;
123 op = (PyObject *) PyObject_MALLOC(_PyObject_SIZE(tp));
124 if (op == NULL)
125 return PyErr_NoMemory();
126 return PyObject_INIT(op, tp);
127}
128
Guido van Rossumd0c87ee1997-05-15 21:31:03 +0000129PyVarObject *
Fred Drake100814d2000-07-09 15:48:49 +0000130_PyObject_NewVar(PyTypeObject *tp, int size)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000131{
Guido van Rossumb18618d2000-05-03 23:44:39 +0000132 PyVarObject *op;
133 op = (PyVarObject *) PyObject_MALLOC(_PyObject_VAR_SIZE(tp, size));
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000134 if (op == NULL)
Guido van Rossumd0c87ee1997-05-15 21:31:03 +0000135 return (PyVarObject *)PyErr_NoMemory();
Guido van Rossumb18618d2000-05-03 23:44:39 +0000136 return PyObject_INIT_VAR(op, tp, size);
137}
138
139void
Fred Drake100814d2000-07-09 15:48:49 +0000140_PyObject_Del(PyObject *op)
Guido van Rossumb18618d2000-05-03 23:44:39 +0000141{
Guido van Rossum4cc6ac72000-07-01 01:00:38 +0000142 PyObject_FREE(op);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000143}
144
Guido van Rossum90933611991-06-07 16:10:43 +0000145int
Fred Drake100814d2000-07-09 15:48:49 +0000146PyObject_Print(PyObject *op, FILE *fp, int flags)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000147{
Guido van Rossum278ef591991-07-27 21:40:24 +0000148 int ret = 0;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000149 if (PyErr_CheckSignals())
Guido van Rossum90933611991-06-07 16:10:43 +0000150 return -1;
Guido van Rossum9b00dfa1998-04-28 16:06:54 +0000151#ifdef USE_STACKCHECK
152 if (PyOS_CheckStack()) {
Fred Drake661ea262000-10-24 19:57:45 +0000153 PyErr_SetString(PyExc_MemoryError, "stack overflow");
Guido van Rossum9b00dfa1998-04-28 16:06:54 +0000154 return -1;
155 }
156#endif
Guido van Rossum687ef6e2000-01-12 16:28:58 +0000157 clearerr(fp); /* Clear any previous error condition */
Guido van Rossum90933611991-06-07 16:10:43 +0000158 if (op == NULL) {
159 fprintf(fp, "<nil>");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000160 }
Guido van Rossum90933611991-06-07 16:10:43 +0000161 else {
162 if (op->ob_refcnt <= 0)
Fred Drakea44d3532000-06-30 15:01:00 +0000163 fprintf(fp, "<refcnt %u at %p>",
164 op->ob_refcnt, op);
Guido van Rossum2e8f6141992-09-03 20:32:55 +0000165 else if (op->ob_type->tp_print == NULL) {
Guido van Rossum4f288ab2001-05-01 16:53:37 +0000166 PyObject *s;
167 if (flags & Py_PRINT_RAW)
168 s = PyObject_Str(op);
169 else
170 s = PyObject_Repr(op);
171 if (s == NULL)
172 ret = -1;
Guido van Rossum2e8f6141992-09-03 20:32:55 +0000173 else {
Guido van Rossum4f288ab2001-05-01 16:53:37 +0000174 ret = PyObject_Print(s, fp, Py_PRINT_RAW);
Guido van Rossum2e8f6141992-09-03 20:32:55 +0000175 }
Guido van Rossum4f288ab2001-05-01 16:53:37 +0000176 Py_XDECREF(s);
Guido van Rossum2e8f6141992-09-03 20:32:55 +0000177 }
Guido van Rossum90933611991-06-07 16:10:43 +0000178 else
Guido van Rossum278ef591991-07-27 21:40:24 +0000179 ret = (*op->ob_type->tp_print)(op, fp, flags);
Guido van Rossum90933611991-06-07 16:10:43 +0000180 }
Guido van Rossum278ef591991-07-27 21:40:24 +0000181 if (ret == 0) {
182 if (ferror(fp)) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000183 PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossum278ef591991-07-27 21:40:24 +0000184 clearerr(fp);
185 ret = -1;
186 }
187 }
188 return ret;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000189}
190
Barry Warsaw9bf16442001-01-23 16:24:35 +0000191/* For debugging convenience. See Misc/gdbinit for some useful gdb hooks */
Barry Warsawbbd89b62001-01-24 04:18:13 +0000192void _PyObject_Dump(PyObject* op)
Barry Warsaw9bf16442001-01-23 16:24:35 +0000193{
Barry Warsaweefb1072001-02-22 22:39:18 +0000194 if (op == NULL)
195 fprintf(stderr, "NULL\n");
196 else {
197 (void)PyObject_Print(op, stderr, 0);
198 fprintf(stderr, "\nrefcounts: %d\n", op->ob_refcnt);
199 fprintf(stderr, "address : %p\n", op);
200 }
Barry Warsaw9bf16442001-01-23 16:24:35 +0000201}
Barry Warsaw903138f2001-01-23 16:33:18 +0000202
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000203PyObject *
Fred Drake100814d2000-07-09 15:48:49 +0000204PyObject_Repr(PyObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000205{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000206 if (PyErr_CheckSignals())
Guido van Rossum90933611991-06-07 16:10:43 +0000207 return NULL;
Guido van Rossum9b00dfa1998-04-28 16:06:54 +0000208#ifdef USE_STACKCHECK
209 if (PyOS_CheckStack()) {
Fred Drake661ea262000-10-24 19:57:45 +0000210 PyErr_SetString(PyExc_MemoryError, "stack overflow");
Guido van Rossum9b00dfa1998-04-28 16:06:54 +0000211 return NULL;
212 }
213#endif
Guido van Rossum90933611991-06-07 16:10:43 +0000214 if (v == NULL)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000215 return PyString_FromString("<NULL>");
Barry Warsaw7ce36942001-08-24 18:34:26 +0000216 else if (v->ob_type->tp_repr == NULL)
Guido van Rossum21922aa2001-08-30 20:26:05 +0000217 return PyString_FromFormat("<%s object at %p>",
Barry Warsaw7ce36942001-08-24 18:34:26 +0000218 v->ob_type->tp_name, v);
Guido van Rossum4c08d552000-03-10 22:55:18 +0000219 else {
220 PyObject *res;
221 res = (*v->ob_type->tp_repr)(v);
222 if (res == NULL)
223 return NULL;
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000224#ifdef Py_USING_UNICODE
Fredrik Lundhefecc7d2000-07-01 14:31:09 +0000225 if (PyUnicode_Check(res)) {
226 PyObject* str;
Fredrik Lundh2a1e0602000-07-08 17:43:32 +0000227 str = PyUnicode_AsUnicodeEscapeString(res);
Marc-André Lemburg891bc652000-07-03 09:57:53 +0000228 Py_DECREF(res);
229 if (str)
Fredrik Lundhefecc7d2000-07-01 14:31:09 +0000230 res = str;
Marc-André Lemburg891bc652000-07-03 09:57:53 +0000231 else
232 return NULL;
Fredrik Lundhefecc7d2000-07-01 14:31:09 +0000233 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000234#endif
Guido van Rossum4c08d552000-03-10 22:55:18 +0000235 if (!PyString_Check(res)) {
236 PyErr_Format(PyExc_TypeError,
Guido van Rossum5db862d2000-04-10 12:46:51 +0000237 "__repr__ returned non-string (type %.200s)",
Guido van Rossum4c08d552000-03-10 22:55:18 +0000238 res->ob_type->tp_name);
239 Py_DECREF(res);
240 return NULL;
241 }
242 return res;
243 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000244}
245
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000246PyObject *
Fred Drake100814d2000-07-09 15:48:49 +0000247PyObject_Str(PyObject *v)
Guido van Rossumc6004111993-11-05 10:22:19 +0000248{
Guido van Rossum4c08d552000-03-10 22:55:18 +0000249 PyObject *res;
250
Guido van Rossumc6004111993-11-05 10:22:19 +0000251 if (v == NULL)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000252 return PyString_FromString("<NULL>");
Guido van Rossum4f288ab2001-05-01 16:53:37 +0000253 if (PyString_Check(v)) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000254 Py_INCREF(v);
Guido van Rossumc6004111993-11-05 10:22:19 +0000255 return v;
256 }
Guido van Rossum4f288ab2001-05-01 16:53:37 +0000257 if (v->ob_type->tp_str == NULL)
258 return PyObject_Repr(v);
259
260 res = (*v->ob_type->tp_str)(v);
Guido van Rossum4c08d552000-03-10 22:55:18 +0000261 if (res == NULL)
262 return NULL;
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000263#ifdef Py_USING_UNICODE
Marc-André Lemburg891bc652000-07-03 09:57:53 +0000264 if (PyUnicode_Check(res)) {
265 PyObject* str;
266 str = PyUnicode_AsEncodedString(res, NULL, NULL);
267 Py_DECREF(res);
268 if (str)
269 res = str;
270 else
271 return NULL;
272 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000273#endif
Guido van Rossum4c08d552000-03-10 22:55:18 +0000274 if (!PyString_Check(res)) {
275 PyErr_Format(PyExc_TypeError,
Guido van Rossum5db862d2000-04-10 12:46:51 +0000276 "__str__ returned non-string (type %.200s)",
Guido van Rossum4c08d552000-03-10 22:55:18 +0000277 res->ob_type->tp_name);
278 Py_DECREF(res);
279 return NULL;
280 }
281 return res;
Guido van Rossumc6004111993-11-05 10:22:19 +0000282}
283
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000284#ifdef Py_USING_UNICODE
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000285PyObject *
286PyObject_Unicode(PyObject *v)
287{
288 PyObject *res;
289
290 if (v == NULL)
291 res = PyString_FromString("<NULL>");
292 else if (PyUnicode_Check(v)) {
293 Py_INCREF(v);
294 return v;
295 }
Marc-André Lemburgae605342001-03-25 19:16:13 +0000296 else if (PyString_Check(v)) {
297 Py_INCREF(v);
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000298 res = v;
Marc-André Lemburgae605342001-03-25 19:16:13 +0000299 }
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000300 else if (v->ob_type->tp_str != NULL)
301 res = (*v->ob_type->tp_str)(v);
302 else {
303 PyObject *func;
304 static PyObject *strstr;
305 if (strstr == NULL) {
306 strstr= PyString_InternFromString("__str__");
307 if (strstr == NULL)
308 return NULL;
309 }
310 if (!PyInstance_Check(v) ||
311 (func = PyObject_GetAttr(v, strstr)) == NULL) {
312 PyErr_Clear();
313 res = PyObject_Repr(v);
314 }
315 else {
316 res = PyEval_CallObject(func, (PyObject *)NULL);
317 Py_DECREF(func);
318 }
319 }
320 if (res == NULL)
321 return NULL;
322 if (!PyUnicode_Check(res)) {
323 PyObject* str;
324 str = PyUnicode_FromObject(res);
325 Py_DECREF(res);
326 if (str)
327 res = str;
328 else
329 return NULL;
330 }
331 return res;
332}
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000333#endif
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000334
335
Guido van Rossumd1f06b92001-01-24 22:14:43 +0000336/* Macro to get the tp_richcompare field of a type if defined */
337#define RICHCOMPARE(t) (PyType_HasFeature((t), Py_TPFLAGS_HAVE_RICHCOMPARE) \
338 ? (t)->tp_richcompare : NULL)
339
Guido van Rossume797ec12001-01-17 15:24:28 +0000340/* Map rich comparison operators to their swapped version, e.g. LT --> GT */
341static int swapped_op[] = {Py_GT, Py_GE, Py_EQ, Py_NE, Py_LT, Py_LE};
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +0000342
Guido van Rossume797ec12001-01-17 15:24:28 +0000343/* Try a genuine rich comparison, returning an object. Return:
344 NULL for exception;
345 NotImplemented if this particular rich comparison is not implemented or
346 undefined;
347 some object not equal to NotImplemented if it is implemented
348 (this latter object may not be a Boolean).
349*/
350static PyObject *
351try_rich_compare(PyObject *v, PyObject *w, int op)
352{
353 richcmpfunc f;
354 PyObject *res;
355
Guido van Rossumd1f06b92001-01-24 22:14:43 +0000356 if ((f = RICHCOMPARE(v->ob_type)) != NULL) {
Guido van Rossume797ec12001-01-17 15:24:28 +0000357 res = (*f)(v, w, op);
358 if (res != Py_NotImplemented)
359 return res;
360 Py_DECREF(res);
361 }
Guido van Rossumd1f06b92001-01-24 22:14:43 +0000362 if ((f = RICHCOMPARE(w->ob_type)) != NULL) {
Guido van Rossume797ec12001-01-17 15:24:28 +0000363 return (*f)(w, v, swapped_op[op]);
364 }
365 res = Py_NotImplemented;
366 Py_INCREF(res);
367 return res;
368}
369
370/* Try a genuine rich comparison, returning an int. Return:
371 -1 for exception (including the case where try_rich_compare() returns an
372 object that's not a Boolean);
373 0 if the outcome is false;
374 1 if the outcome is true;
375 2 if this particular rich comparison is not implemented or undefined.
376*/
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +0000377static int
Guido van Rossume797ec12001-01-17 15:24:28 +0000378try_rich_compare_bool(PyObject *v, PyObject *w, int op)
379{
380 PyObject *res;
381 int ok;
382
Guido van Rossumd1f06b92001-01-24 22:14:43 +0000383 if (RICHCOMPARE(v->ob_type) == NULL && RICHCOMPARE(w->ob_type) == NULL)
Guido van Rossume797ec12001-01-17 15:24:28 +0000384 return 2; /* Shortcut, avoid INCREF+DECREF */
385 res = try_rich_compare(v, w, op);
386 if (res == NULL)
387 return -1;
388 if (res == Py_NotImplemented) {
389 Py_DECREF(res);
390 return 2;
391 }
392 ok = PyObject_IsTrue(res);
393 Py_DECREF(res);
394 return ok;
395}
396
397/* Try rich comparisons to determine a 3-way comparison. Return:
398 -2 for an exception;
399 -1 if v < w;
400 0 if v == w;
401 1 if v > w;
402 2 if this particular rich comparison is not implemented or undefined.
403*/
404static int
405try_rich_to_3way_compare(PyObject *v, PyObject *w)
406{
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000407 static struct { int op; int outcome; } tries[3] = {
408 /* Try this operator, and if it is true, use this outcome: */
409 {Py_EQ, 0},
410 {Py_LT, -1},
411 {Py_GT, 1},
412 };
413 int i;
414
Guido van Rossumd1f06b92001-01-24 22:14:43 +0000415 if (RICHCOMPARE(v->ob_type) == NULL && RICHCOMPARE(w->ob_type) == NULL)
Guido van Rossume797ec12001-01-17 15:24:28 +0000416 return 2; /* Shortcut */
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000417
418 for (i = 0; i < 3; i++) {
419 switch (try_rich_compare_bool(v, w, tries[i].op)) {
420 case -1:
Tim Peters6d60b2e2001-05-07 20:53:51 +0000421 return -2;
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000422 case 1:
423 return tries[i].outcome;
424 }
Guido van Rossume797ec12001-01-17 15:24:28 +0000425 }
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000426
427 return 2;
Guido van Rossume797ec12001-01-17 15:24:28 +0000428}
429
430/* Try a 3-way comparison, returning an int. Return:
431 -2 for an exception;
432 -1 if v < w;
433 0 if v == w;
434 1 if v > w;
435 2 if this particular 3-way comparison is not implemented or undefined.
436*/
437static int
438try_3way_compare(PyObject *v, PyObject *w)
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +0000439{
440 int c;
Guido van Rossume797ec12001-01-17 15:24:28 +0000441 cmpfunc f;
442
443 /* Comparisons involving instances are given to instance_compare,
444 which has the same return conventions as this function. */
445
446 if (PyInstance_Check(v))
447 return (*v->ob_type->tp_compare)(v, w);
448 if (PyInstance_Check(w))
449 return (*w->ob_type->tp_compare)(v, w);
450
Guido van Rossume797ec12001-01-17 15:24:28 +0000451 /* Try coercion; if it fails, give up */
452 c = PyNumber_CoerceEx(&v, &w);
453 if (c < 0)
454 return -2;
455 if (c > 0)
456 return 2;
457
458 /* Try v's comparison, if defined */
459 if ((f = v->ob_type->tp_compare) != NULL) {
460 c = (*f)(v, w);
461 Py_DECREF(v);
462 Py_DECREF(w);
Martin v. Löwis0163d6d2001-06-09 07:34:05 +0000463 if (c < 0 && PyErr_Occurred())
Guido van Rossume797ec12001-01-17 15:24:28 +0000464 return -2;
465 return c < 0 ? -1 : c > 0 ? 1 : 0;
466 }
467
468 /* Try w's comparison, if defined */
469 if ((f = w->ob_type->tp_compare) != NULL) {
470 c = (*f)(w, v); /* swapped! */
471 Py_DECREF(v);
472 Py_DECREF(w);
Martin v. Löwis0163d6d2001-06-09 07:34:05 +0000473 if (c < 0 && PyErr_Occurred())
Guido van Rossume797ec12001-01-17 15:24:28 +0000474 return -2;
475 return c < 0 ? 1 : c > 0 ? -1 : 0; /* negated! */
476 }
477
478 /* No comparison defined */
479 Py_DECREF(v);
480 Py_DECREF(w);
481 return 2;
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +0000482}
483
Guido van Rossume797ec12001-01-17 15:24:28 +0000484/* Final fallback 3-way comparison, returning an int. Return:
485 -2 if an error occurred;
486 -1 if v < w;
487 0 if v == w;
488 1 if v > w.
489*/
490static int
491default_3way_compare(PyObject *v, PyObject *w)
492{
493 int c;
Guido van Rossum8f9143d2001-01-22 15:59:32 +0000494 char *vname, *wname;
Guido van Rossume797ec12001-01-17 15:24:28 +0000495
496 if (v->ob_type == w->ob_type) {
Barry Warsawb0e754d2001-01-20 06:24:55 +0000497 /* When comparing these pointers, they must be cast to
498 * integer types (i.e. Py_uintptr_t, our spelling of C9X's
499 * uintptr_t). ANSI specifies that pointer compares other
500 * than == and != to non-related structures are undefined.
501 */
Barry Warsaw71ff8d52001-01-20 06:08:10 +0000502 Py_uintptr_t vv = (Py_uintptr_t)v;
503 Py_uintptr_t ww = (Py_uintptr_t)w;
Guido van Rossume797ec12001-01-17 15:24:28 +0000504 return (vv < ww) ? -1 : (vv > ww) ? 1 : 0;
505 }
506
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000507#ifdef Py_USING_UNICODE
Guido van Rossume797ec12001-01-17 15:24:28 +0000508 /* Special case for Unicode */
509 if (PyUnicode_Check(v) || PyUnicode_Check(w)) {
510 c = PyUnicode_Compare(v, w);
511 if (!PyErr_Occurred())
512 return c;
513 /* TypeErrors are ignored: if Unicode coercion fails due
514 to one of the arguments not having the right type, we
515 continue as defined by the coercion protocol (see
516 above). Luckily, decoding errors are reported as
517 ValueErrors and are not masked by this technique. */
518 if (!PyErr_ExceptionMatches(PyExc_TypeError))
519 return -2;
520 PyErr_Clear();
521 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000522#endif
Guido van Rossume797ec12001-01-17 15:24:28 +0000523
Guido van Rossum0871e932001-01-22 19:28:09 +0000524 /* None is smaller than anything */
525 if (v == Py_None)
526 return -1;
527 if (w == Py_None)
528 return 1;
529
Guido van Rossume797ec12001-01-17 15:24:28 +0000530 /* different type: compare type names */
Guido van Rossum8f9143d2001-01-22 15:59:32 +0000531 if (v->ob_type->tp_as_number)
532 vname = "";
533 else
534 vname = v->ob_type->tp_name;
535 if (w->ob_type->tp_as_number)
536 wname = "";
537 else
538 wname = w->ob_type->tp_name;
539 c = strcmp(vname, wname);
540 if (c < 0)
541 return -1;
542 if (c > 0)
543 return 1;
544 /* Same type name, or (more likely) incomparable numeric types */
545 return ((Py_uintptr_t)(v->ob_type) < (
546 Py_uintptr_t)(w->ob_type)) ? -1 : 1;
Guido van Rossume797ec12001-01-17 15:24:28 +0000547}
548
549#define CHECK_TYPES(o) PyType_HasFeature((o)->ob_type, Py_TPFLAGS_CHECKTYPES)
550
Tim Peters6d60b2e2001-05-07 20:53:51 +0000551/* Do a 3-way comparison, by hook or by crook. Return:
552 -2 for an exception;
553 -1 if v < w;
554 0 if v == w;
555 1 if v > w;
Martin v. Löwis0163d6d2001-06-09 07:34:05 +0000556 If the object implements a tp_compare function, it returns
557 whatever this function returns (whether with an exception or not).
Tim Peters6d60b2e2001-05-07 20:53:51 +0000558*/
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +0000559static int
Fred Drake100814d2000-07-09 15:48:49 +0000560do_cmp(PyObject *v, PyObject *w)
Guido van Rossum20566841995-01-12 11:26:10 +0000561{
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +0000562 int c;
Martin v. Löwis0163d6d2001-06-09 07:34:05 +0000563 cmpfunc f;
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +0000564
Martin v. Löwis0163d6d2001-06-09 07:34:05 +0000565 if (v->ob_type == w->ob_type
Guido van Rossum82fc51c12001-08-16 08:02:45 +0000566 && (f = v->ob_type->tp_compare) != NULL) {
567 c = (*f)(v, w);
568 if (c != 2 || !PyInstance_Check(v))
569 return c;
570 }
Guido van Rossume797ec12001-01-17 15:24:28 +0000571 c = try_rich_to_3way_compare(v, w);
572 if (c < 2)
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +0000573 return c;
Guido van Rossume797ec12001-01-17 15:24:28 +0000574 c = try_3way_compare(v, w);
575 if (c < 2)
576 return c;
577 return default_3way_compare(v, w);
Guido van Rossum20566841995-01-12 11:26:10 +0000578}
579
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000580/* compare_nesting is incremented before calling compare (for
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000581 some types) and decremented on exit. If the count exceeds the
582 nesting limit, enable code to detect circular data structures.
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000583
584 This is a tunable parameter that should only affect the performance
585 of comparisons, nothing else. Setting it high makes comparing deeply
586 nested non-cyclical data structures faster, but makes comparing cyclical
587 data structures slower.
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000588*/
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000589#define NESTING_LIMIT 20
590
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000591static int compare_nesting = 0;
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000592
593static PyObject*
Fred Drake100814d2000-07-09 15:48:49 +0000594get_inprogress_dict(void)
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000595{
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000596 static PyObject *key;
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000597 PyObject *tstate_dict, *inprogress;
598
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000599 if (key == NULL) {
600 key = PyString_InternFromString("cmp_state");
601 if (key == NULL)
602 return NULL;
603 }
604
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000605 tstate_dict = PyThreadState_GetDict();
606 if (tstate_dict == NULL) {
607 PyErr_BadInternalCall();
608 return NULL;
609 }
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000610
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000611 inprogress = PyDict_GetItem(tstate_dict, key);
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000612 if (inprogress == NULL) {
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000613 inprogress = PyDict_New();
614 if (inprogress == NULL)
615 return NULL;
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000616 if (PyDict_SetItem(tstate_dict, key, inprogress) == -1) {
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000617 Py_DECREF(inprogress);
618 return NULL;
619 }
Jeremy Hyltona251ea02000-06-09 16:20:39 +0000620 Py_DECREF(inprogress);
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000621 }
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000622
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000623 return inprogress;
624}
625
626static PyObject *
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000627check_recursion(PyObject *v, PyObject *w, int op)
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000628{
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000629 PyObject *inprogress;
630 PyObject *token;
Barry Warsaw9d23a4e2000-08-18 05:01:19 +0000631 Py_uintptr_t iv = (Py_uintptr_t)v;
632 Py_uintptr_t iw = (Py_uintptr_t)w;
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000633 PyObject *x, *y, *z;
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000634
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000635 inprogress = get_inprogress_dict();
636 if (inprogress == NULL)
637 return NULL;
638
639 token = PyTuple_New(3);
640 if (token == NULL)
641 return NULL;
642
643 if (iv <= iw) {
644 PyTuple_SET_ITEM(token, 0, x = PyLong_FromVoidPtr((void *)v));
645 PyTuple_SET_ITEM(token, 1, y = PyLong_FromVoidPtr((void *)w));
646 if (op >= 0)
647 op = swapped_op[op];
648 } else {
649 PyTuple_SET_ITEM(token, 0, x = PyLong_FromVoidPtr((void *)w));
650 PyTuple_SET_ITEM(token, 1, y = PyLong_FromVoidPtr((void *)v));
651 }
652 PyTuple_SET_ITEM(token, 2, z = PyInt_FromLong((long)op));
653 if (x == NULL || y == NULL || z == NULL) {
654 Py_DECREF(token);
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000655 return NULL;
656 }
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000657
658 if (PyDict_GetItem(inprogress, token) != NULL) {
659 Py_DECREF(token);
660 return Py_None; /* Without INCREF! */
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000661 }
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000662
663 if (PyDict_SetItem(inprogress, token, token) < 0) {
664 Py_DECREF(token);
665 return NULL;
666 }
667
668 return token;
669}
670
671static void
672delete_token(PyObject *token)
673{
674 PyObject *inprogress;
675
676 if (token == NULL || token == Py_None)
677 return;
678 inprogress = get_inprogress_dict();
679 if (inprogress == NULL)
680 PyErr_Clear();
681 else
682 PyDict_DelItem(inprogress, token);
683 Py_DECREF(token);
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000684}
685
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000686int
Fred Drake100814d2000-07-09 15:48:49 +0000687PyObject_Compare(PyObject *v, PyObject *w)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000688{
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000689 PyTypeObject *vtp;
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000690 int result;
691
Jack Jansend49cbe12000-08-22 21:52:51 +0000692#if defined(USE_STACKCHECK)
Guido van Rossum9c0a99e2000-08-30 15:53:50 +0000693 if (PyOS_CheckStack()) {
Jack Jansend49cbe12000-08-22 21:52:51 +0000694 PyErr_SetString(PyExc_MemoryError, "Stack overflow");
695 return -1;
696 }
697#endif
Guido van Rossumc8b6df91997-05-23 00:06:51 +0000698 if (v == NULL || w == NULL) {
699 PyErr_BadInternalCall();
700 return -1;
701 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000702 if (v == w)
703 return 0;
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +0000704 vtp = v->ob_type;
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000705 compare_nesting++;
706 if (compare_nesting > NESTING_LIMIT &&
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +0000707 (vtp->tp_as_mapping
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000708 || (vtp->tp_as_sequence
709 && !PyString_Check(v)
710 && !PyTuple_Check(v)))) {
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +0000711 /* try to detect circular data structures */
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000712 PyObject *token = check_recursion(v, w, -1);
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000713
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000714 if (token == NULL) {
715 result = -1;
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000716 }
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000717 else if (token == Py_None) {
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000718 /* already comparing these objects. assume
719 they're equal until shown otherwise */
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +0000720 result = 0;
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000721 }
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000722 else {
723 result = do_cmp(v, w);
724 delete_token(token);
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000725 }
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000726 }
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +0000727 else {
728 result = do_cmp(v, w);
729 }
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000730 compare_nesting--;
Guido van Rossume797ec12001-01-17 15:24:28 +0000731 return result < 0 ? -1 : result;
732}
733
734static PyObject *
Martin v. Löwis0163d6d2001-06-09 07:34:05 +0000735convert_3way_to_object(int op, int c)
Guido van Rossume797ec12001-01-17 15:24:28 +0000736{
Guido van Rossume797ec12001-01-17 15:24:28 +0000737 PyObject *result;
Guido van Rossume797ec12001-01-17 15:24:28 +0000738 switch (op) {
739 case Py_LT: c = c < 0; break;
740 case Py_LE: c = c <= 0; break;
741 case Py_EQ: c = c == 0; break;
742 case Py_NE: c = c != 0; break;
743 case Py_GT: c = c > 0; break;
744 case Py_GE: c = c >= 0; break;
745 }
746 result = c ? Py_True : Py_False;
747 Py_INCREF(result);
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000748 return result;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000749}
Martin v. Löwis0163d6d2001-06-09 07:34:05 +0000750
751
752static PyObject *
753try_3way_to_rich_compare(PyObject *v, PyObject *w, int op)
754{
755 int c;
756
757 c = try_3way_compare(v, w);
758 if (c >= 2)
759 c = default_3way_compare(v, w);
760 if (c <= -2)
761 return NULL;
762 return convert_3way_to_object(op, c);
763}
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000764
Neil Schemenauerd38855c2001-01-21 16:25:18 +0000765static PyObject *
Guido van Rossume797ec12001-01-17 15:24:28 +0000766do_richcmp(PyObject *v, PyObject *w, int op)
767{
768 PyObject *res;
Martin v. Löwis0163d6d2001-06-09 07:34:05 +0000769 cmpfunc f;
770
771 /* If the types are equal, don't bother with coercions etc.
772 Instances are special-cased in try_3way_compare, since
773 a result of 2 does *not* mean one value being greater
774 than the other. */
775 if (v->ob_type == w->ob_type
776 && (f = v->ob_type->tp_compare) != NULL
777 && !PyInstance_Check(v)) {
778 int c;
779 richcmpfunc f1;
780 if ((f1 = RICHCOMPARE(v->ob_type)) != NULL) {
781 /* If the type has richcmp, try it first.
782 try_rich_compare would try it two-sided,
783 which is not needed since we've a single
784 type only. */
785 res = (*f1)(v, w, op);
786 if (res != Py_NotImplemented)
787 return res;
788 Py_DECREF(res);
789 }
790 c = (*f)(v, w);
791 if (c < 0 && PyErr_Occurred())
792 return NULL;
793 return convert_3way_to_object(op, c);
794 }
Guido van Rossume797ec12001-01-17 15:24:28 +0000795
796 res = try_rich_compare(v, w, op);
797 if (res != Py_NotImplemented)
798 return res;
799 Py_DECREF(res);
800
801 return try_3way_to_rich_compare(v, w, op);
802}
803
804PyObject *
805PyObject_RichCompare(PyObject *v, PyObject *w, int op)
806{
807 PyObject *res;
808
809 assert(Py_LT <= op && op <= Py_GE);
810
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000811 compare_nesting++;
812 if (compare_nesting > NESTING_LIMIT &&
813 (v->ob_type->tp_as_mapping
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000814 || (v->ob_type->tp_as_sequence
815 && !PyString_Check(v)
816 && !PyTuple_Check(v)))) {
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000817 /* try to detect circular data structures */
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000818 PyObject *token = check_recursion(v, w, op);
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000819
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000820 if (token == NULL) {
821 res = NULL;
Guido van Rossume797ec12001-01-17 15:24:28 +0000822 }
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000823 else if (token == Py_None) {
824 /* already comparing these objects with this operator.
825 assume they're equal until shown otherwise */
826 if (op == Py_EQ)
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000827 res = Py_True;
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000828 else if (op == Py_NE)
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000829 res = Py_False;
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000830 else {
831 PyErr_SetString(PyExc_ValueError,
832 "can't order recursive values");
833 res = NULL;
834 }
835 Py_XINCREF(res);
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000836 }
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000837 else {
838 res = do_richcmp(v, w, op);
839 delete_token(token);
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000840 }
Guido van Rossume797ec12001-01-17 15:24:28 +0000841 }
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000842 else {
843 res = do_richcmp(v, w, op);
844 }
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000845 compare_nesting--;
Guido van Rossume797ec12001-01-17 15:24:28 +0000846 return res;
847}
848
Tim Petersde9725f2001-05-05 10:06:17 +0000849/* Return -1 if error; 1 if v op w; 0 if not (v op w). */
Guido van Rossume797ec12001-01-17 15:24:28 +0000850int
851PyObject_RichCompareBool(PyObject *v, PyObject *w, int op)
852{
853 PyObject *res = PyObject_RichCompare(v, w, op);
854 int ok;
855
856 if (res == NULL)
857 return -1;
858 ok = PyObject_IsTrue(res);
859 Py_DECREF(res);
860 return ok;
861}
Fred Drake13634cf2000-06-29 19:17:04 +0000862
863/* Set of hash utility functions to help maintaining the invariant that
864 iff a==b then hash(a)==hash(b)
865
866 All the utility functions (_Py_Hash*()) return "-1" to signify an error.
867*/
868
869long
Fred Drake100814d2000-07-09 15:48:49 +0000870_Py_HashDouble(double v)
Fred Drake13634cf2000-06-29 19:17:04 +0000871{
Tim Peters39dce292000-08-15 03:34:48 +0000872 double intpart, fractpart;
873 int expo;
874 long hipart;
875 long x; /* the final hash value */
876 /* This is designed so that Python numbers of different types
877 * that compare equal hash to the same value; otherwise comparisons
878 * of mapping keys will turn out weird.
879 */
880
881#ifdef MPW /* MPW C modf expects pointer to extended as second argument */
882{
883 extended e;
884 fractpart = modf(v, &e);
885 intpart = e;
886}
887#else
888 fractpart = modf(v, &intpart);
889#endif
890 if (fractpart == 0.0) {
891 /* This must return the same hash as an equal int or long. */
892 if (intpart > LONG_MAX || -intpart > LONG_MAX) {
893 /* Convert to long and use its hash. */
894 PyObject *plong; /* converted to Python long */
895 if (Py_IS_INFINITY(intpart))
896 /* can't convert to long int -- arbitrary */
897 v = v < 0 ? -271828.0 : 314159.0;
898 plong = PyLong_FromDouble(v);
899 if (plong == NULL)
900 return -1;
901 x = PyObject_Hash(plong);
902 Py_DECREF(plong);
903 return x;
904 }
905 /* Fits in a C long == a Python int, so is its own hash. */
906 x = (long)intpart;
907 if (x == -1)
908 x = -2;
909 return x;
910 }
911 /* The fractional part is non-zero, so we don't have to worry about
912 * making this match the hash of some other type.
913 * Use frexp to get at the bits in the double.
Fred Drake13634cf2000-06-29 19:17:04 +0000914 * Since the VAX D double format has 56 mantissa bits, which is the
915 * most of any double format in use, each of these parts may have as
916 * many as (but no more than) 56 significant bits.
Tim Peters39dce292000-08-15 03:34:48 +0000917 * So, assuming sizeof(long) >= 4, each part can be broken into two
918 * longs; frexp and multiplication are used to do that.
919 * Also, since the Cray double format has 15 exponent bits, which is
920 * the most of any double format in use, shifting the exponent field
921 * left by 15 won't overflow a long (again assuming sizeof(long) >= 4).
Fred Drake13634cf2000-06-29 19:17:04 +0000922 */
Tim Peters39dce292000-08-15 03:34:48 +0000923 v = frexp(v, &expo);
924 v *= 2147483648.0; /* 2**31 */
925 hipart = (long)v; /* take the top 32 bits */
926 v = (v - (double)hipart) * 2147483648.0; /* get the next 32 bits */
927 x = hipart + (long)v + (expo << 15);
928 if (x == -1)
929 x = -2;
930 return x;
Fred Drake13634cf2000-06-29 19:17:04 +0000931}
932
933long
Fred Drake100814d2000-07-09 15:48:49 +0000934_Py_HashPointer(void *p)
Fred Drake13634cf2000-06-29 19:17:04 +0000935{
936#if SIZEOF_LONG >= SIZEOF_VOID_P
937 return (long)p;
938#else
939 /* convert to a Python long and hash that */
940 PyObject* longobj;
941 long x;
942
943 if ((longobj = PyLong_FromVoidPtr(p)) == NULL) {
944 x = -1;
945 goto finally;
946 }
947 x = PyObject_Hash(longobj);
948
949finally:
950 Py_XDECREF(longobj);
951 return x;
952#endif
953}
954
955
Guido van Rossum9bfef441993-03-29 10:43:31 +0000956long
Fred Drake100814d2000-07-09 15:48:49 +0000957PyObject_Hash(PyObject *v)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000958{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000959 PyTypeObject *tp = v->ob_type;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000960 if (tp->tp_hash != NULL)
961 return (*tp->tp_hash)(v);
Guido van Rossumd1f06b92001-01-24 22:14:43 +0000962 if (tp->tp_compare == NULL && RICHCOMPARE(tp) == NULL) {
Fred Drake13634cf2000-06-29 19:17:04 +0000963 return _Py_HashPointer(v); /* Use address as hash value */
964 }
Guido van Rossum9bfef441993-03-29 10:43:31 +0000965 /* If there's a cmp but no hash defined, the object can't be hashed */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000966 PyErr_SetString(PyExc_TypeError, "unhashable type");
Guido van Rossum9bfef441993-03-29 10:43:31 +0000967 return -1;
968}
969
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000970PyObject *
Fred Drake100814d2000-07-09 15:48:49 +0000971PyObject_GetAttrString(PyObject *v, char *name)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000972{
Tim Peters6d6c1a32001-08-02 04:15:00 +0000973 PyObject *w, *res;
Guido van Rossumd8eb1b31996-08-09 20:52:03 +0000974
Tim Peters6d6c1a32001-08-02 04:15:00 +0000975 if (v->ob_type->tp_getattr != NULL)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000976 return (*v->ob_type->tp_getattr)(v, name);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000977 w = PyString_InternFromString(name);
978 if (w == NULL)
979 return NULL;
980 res = PyObject_GetAttr(v, w);
981 Py_XDECREF(w);
982 return res;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000983}
984
985int
Fred Drake100814d2000-07-09 15:48:49 +0000986PyObject_HasAttrString(PyObject *v, char *name)
Guido van Rossumed18fdc1993-07-11 19:55:34 +0000987{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000988 PyObject *res = PyObject_GetAttrString(v, name);
Guido van Rossumed18fdc1993-07-11 19:55:34 +0000989 if (res != NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000990 Py_DECREF(res);
Guido van Rossumed18fdc1993-07-11 19:55:34 +0000991 return 1;
992 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000993 PyErr_Clear();
Guido van Rossumed18fdc1993-07-11 19:55:34 +0000994 return 0;
995}
996
997int
Fred Drake100814d2000-07-09 15:48:49 +0000998PyObject_SetAttrString(PyObject *v, char *name, PyObject *w)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000999{
Tim Peters6d6c1a32001-08-02 04:15:00 +00001000 PyObject *s;
1001 int res;
Guido van Rossumd8eb1b31996-08-09 20:52:03 +00001002
Tim Peters6d6c1a32001-08-02 04:15:00 +00001003 if (v->ob_type->tp_setattr != NULL)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001004 return (*v->ob_type->tp_setattr)(v, name, w);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001005 s = PyString_InternFromString(name);
1006 if (s == NULL)
1007 return -1;
1008 res = PyObject_SetAttr(v, s, w);
1009 Py_XDECREF(s);
1010 return res;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001011}
1012
Guido van Rossum98ff96a1997-05-20 18:34:44 +00001013PyObject *
Fred Drake100814d2000-07-09 15:48:49 +00001014PyObject_GetAttr(PyObject *v, PyObject *name)
Guido van Rossum98ff96a1997-05-20 18:34:44 +00001015{
Tim Peters6d6c1a32001-08-02 04:15:00 +00001016 PyTypeObject *tp = v->ob_type;
1017
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001018#ifdef Py_USING_UNICODE
Marc-André Lemburge44e5072000-09-18 16:20:57 +00001019 /* The Unicode to string conversion is done here because the
1020 existing tp_getattro slots expect a string object as name
1021 and we wouldn't want to break those. */
1022 if (PyUnicode_Check(name)) {
1023 name = _PyUnicode_AsDefaultEncodedString(name, NULL);
1024 if (name == NULL)
1025 return NULL;
1026 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001027#endif
1028
Jeremy Hylton99a8f902000-06-23 14:36:32 +00001029 if (!PyString_Check(name)) {
1030 PyErr_SetString(PyExc_TypeError,
1031 "attribute name must be string");
1032 return NULL;
1033 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001034 if (tp->tp_getattro != NULL)
1035 return (*tp->tp_getattro)(v, name);
1036 if (tp->tp_getattr != NULL)
1037 return (*tp->tp_getattr)(v, PyString_AS_STRING(name));
1038 PyErr_Format(PyExc_AttributeError,
1039 "'%.50s' object has no attribute '%.400s'",
1040 tp->tp_name, PyString_AS_STRING(name));
1041 return NULL;
Guido van Rossum98ff96a1997-05-20 18:34:44 +00001042}
1043
1044int
Fred Drake100814d2000-07-09 15:48:49 +00001045PyObject_HasAttr(PyObject *v, PyObject *name)
Guido van Rossum98ff96a1997-05-20 18:34:44 +00001046{
1047 PyObject *res = PyObject_GetAttr(v, name);
1048 if (res != NULL) {
1049 Py_DECREF(res);
1050 return 1;
1051 }
1052 PyErr_Clear();
1053 return 0;
1054}
1055
1056int
Fred Drake100814d2000-07-09 15:48:49 +00001057PyObject_SetAttr(PyObject *v, PyObject *name, PyObject *value)
Guido van Rossum98ff96a1997-05-20 18:34:44 +00001058{
Tim Peters6d6c1a32001-08-02 04:15:00 +00001059 PyTypeObject *tp = v->ob_type;
Guido van Rossum98ff96a1997-05-20 18:34:44 +00001060 int err;
Marc-André Lemburge44e5072000-09-18 16:20:57 +00001061
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001062#ifdef Py_USING_UNICODE
Marc-André Lemburge44e5072000-09-18 16:20:57 +00001063 /* The Unicode to string conversion is done here because the
1064 existing tp_setattro slots expect a string object as name
1065 and we wouldn't want to break those. */
1066 if (PyUnicode_Check(name)) {
1067 name = PyUnicode_AsEncodedString(name, NULL, NULL);
1068 if (name == NULL)
1069 return -1;
Jeremy Hylton99a8f902000-06-23 14:36:32 +00001070 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001071 else
1072#endif
1073 if (!PyString_Check(name)){
Jeremy Hylton99a8f902000-06-23 14:36:32 +00001074 PyErr_SetString(PyExc_TypeError,
1075 "attribute name must be string");
Tim Peters6d6c1a32001-08-02 04:15:00 +00001076 return -1;
Jeremy Hylton99a8f902000-06-23 14:36:32 +00001077 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001078 else
1079 Py_INCREF(name);
1080
1081 PyString_InternInPlace(&name);
1082 if (tp->tp_setattro != NULL) {
1083 err = (*tp->tp_setattro)(v, name, value);
1084 Py_DECREF(name);
1085 return err;
Marc-André Lemburge44e5072000-09-18 16:20:57 +00001086 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001087 if (tp->tp_setattr != NULL) {
1088 err = (*tp->tp_setattr)(v, PyString_AS_STRING(name), value);
1089 Py_DECREF(name);
1090 return err;
1091 }
Guido van Rossum98ff96a1997-05-20 18:34:44 +00001092 Py_DECREF(name);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001093 if (tp->tp_getattr == NULL && tp->tp_getattro == NULL)
1094 PyErr_Format(PyExc_TypeError,
1095 "'%.100s' object has no attributes "
1096 "(%s .%.100s)",
1097 tp->tp_name,
1098 value==NULL ? "del" : "assign to",
1099 PyString_AS_STRING(name));
1100 else
1101 PyErr_Format(PyExc_TypeError,
1102 "'%.100s' object has only read-only attributes "
1103 "(%s .%.100s)",
1104 tp->tp_name,
1105 value==NULL ? "del" : "assign to",
1106 PyString_AS_STRING(name));
1107 return -1;
1108}
1109
1110/* Helper to get a pointer to an object's __dict__ slot, if any */
1111
1112PyObject **
1113_PyObject_GetDictPtr(PyObject *obj)
1114{
1115#define PTRSIZE (sizeof(PyObject *))
1116
1117 long dictoffset;
1118 PyTypeObject *tp = obj->ob_type;
1119
1120 if (!(tp->tp_flags & Py_TPFLAGS_HAVE_CLASS))
1121 return NULL;
1122 dictoffset = tp->tp_dictoffset;
1123 if (dictoffset == 0)
1124 return NULL;
1125 if (dictoffset < 0) {
Neil Schemenauerfd343692001-08-29 23:54:03 +00001126 dictoffset += tp->tp_basicsize;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001127 assert(dictoffset > 0); /* Sanity check */
1128 if (tp->tp_itemsize > 0) {
1129 int n = ((PyVarObject *)obj)->ob_size;
1130 if (n > 0) {
1131 dictoffset += tp->tp_itemsize * n;
1132 /* Round up, if necessary */
1133 if (tp->tp_itemsize % PTRSIZE != 0) {
1134 dictoffset += PTRSIZE - 1;
1135 dictoffset /= PTRSIZE;
1136 dictoffset *= PTRSIZE;
1137 }
1138 }
1139 }
1140 }
1141 return (PyObject **) ((char *)obj + dictoffset);
1142}
1143
1144/* Generic GetAttr functions - put these in your tp_[gs]etattro slot */
1145
1146PyObject *
1147PyObject_GenericGetAttr(PyObject *obj, PyObject *name)
1148{
1149 PyTypeObject *tp = obj->ob_type;
1150 PyObject *descr;
1151 descrgetfunc f;
1152 PyObject **dictptr;
1153
1154 if (tp->tp_dict == NULL) {
Guido van Rossum528b7eb2001-08-07 17:24:28 +00001155 if (PyType_Ready(tp) < 0)
Tim Peters6d6c1a32001-08-02 04:15:00 +00001156 return NULL;
1157 }
1158
1159 descr = _PyType_Lookup(tp, name);
1160 f = NULL;
1161 if (descr != NULL) {
1162 f = descr->ob_type->tp_descr_get;
1163 if (f != NULL && PyDescr_IsData(descr))
1164 return f(descr, obj, (PyObject *)obj->ob_type);
1165 }
1166
1167 dictptr = _PyObject_GetDictPtr(obj);
1168 if (dictptr != NULL) {
1169 PyObject *dict = *dictptr;
1170 if (dict != NULL) {
1171 PyObject *res = PyDict_GetItem(dict, name);
1172 if (res != NULL) {
1173 Py_INCREF(res);
1174 return res;
1175 }
1176 }
1177 }
1178
1179 if (f != NULL)
1180 return f(descr, obj, (PyObject *)obj->ob_type);
1181
1182 if (descr != NULL) {
1183 Py_INCREF(descr);
1184 return descr;
1185 }
1186
1187 PyErr_Format(PyExc_AttributeError,
1188 "'%.50s' object has no attribute '%.400s'",
1189 tp->tp_name, PyString_AS_STRING(name));
1190 return NULL;
1191}
1192
1193int
1194PyObject_GenericSetAttr(PyObject *obj, PyObject *name, PyObject *value)
1195{
1196 PyTypeObject *tp = obj->ob_type;
1197 PyObject *descr;
1198 descrsetfunc f;
1199 PyObject **dictptr;
1200
1201 if (tp->tp_dict == NULL) {
Guido van Rossum528b7eb2001-08-07 17:24:28 +00001202 if (PyType_Ready(tp) < 0)
Tim Peters6d6c1a32001-08-02 04:15:00 +00001203 return -1;
1204 }
1205
1206 descr = _PyType_Lookup(tp, name);
1207 f = NULL;
1208 if (descr != NULL) {
1209 f = descr->ob_type->tp_descr_set;
1210 if (f != NULL && PyDescr_IsData(descr))
1211 return f(descr, obj, value);
1212 }
1213
1214 dictptr = _PyObject_GetDictPtr(obj);
1215 if (dictptr != NULL) {
1216 PyObject *dict = *dictptr;
1217 if (dict == NULL && value != NULL) {
1218 dict = PyDict_New();
1219 if (dict == NULL)
1220 return -1;
1221 *dictptr = dict;
1222 }
1223 if (dict != NULL) {
1224 int res;
1225 if (value == NULL)
1226 res = PyDict_DelItem(dict, name);
1227 else
1228 res = PyDict_SetItem(dict, name, value);
1229 if (res < 0 && PyErr_ExceptionMatches(PyExc_KeyError))
1230 PyErr_SetObject(PyExc_AttributeError, name);
1231 return res;
1232 }
1233 }
1234
1235 if (f != NULL)
1236 return f(descr, obj, value);
1237
1238 if (descr == NULL) {
1239 PyErr_Format(PyExc_AttributeError,
1240 "'%.50s' object has no attribute '%.400s'",
1241 tp->tp_name, PyString_AS_STRING(name));
1242 return -1;
1243 }
1244
1245 PyErr_Format(PyExc_AttributeError,
1246 "'%.50s' object attribute '%.400s' is read-only",
1247 tp->tp_name, PyString_AS_STRING(name));
1248 return -1;
Guido van Rossum98ff96a1997-05-20 18:34:44 +00001249}
1250
Guido van Rossum6ac258d1993-05-12 08:24:20 +00001251/* Test a value used as condition, e.g., in a for or if statement.
1252 Return -1 if an error occurred */
1253
1254int
Fred Drake100814d2000-07-09 15:48:49 +00001255PyObject_IsTrue(PyObject *v)
Guido van Rossum6ac258d1993-05-12 08:24:20 +00001256{
1257 int res;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001258 if (v == Py_None)
Guido van Rossum6ac258d1993-05-12 08:24:20 +00001259 res = 0;
Guido van Rossum1c4f4581998-05-22 00:53:24 +00001260 else if (v->ob_type->tp_as_number != NULL &&
1261 v->ob_type->tp_as_number->nb_nonzero != NULL)
Guido van Rossum6ac258d1993-05-12 08:24:20 +00001262 res = (*v->ob_type->tp_as_number->nb_nonzero)(v);
Guido van Rossum1c4f4581998-05-22 00:53:24 +00001263 else if (v->ob_type->tp_as_mapping != NULL &&
1264 v->ob_type->tp_as_mapping->mp_length != NULL)
Guido van Rossum6ac258d1993-05-12 08:24:20 +00001265 res = (*v->ob_type->tp_as_mapping->mp_length)(v);
Guido van Rossum1c4f4581998-05-22 00:53:24 +00001266 else if (v->ob_type->tp_as_sequence != NULL &&
1267 v->ob_type->tp_as_sequence->sq_length != NULL)
Guido van Rossum6ac258d1993-05-12 08:24:20 +00001268 res = (*v->ob_type->tp_as_sequence->sq_length)(v);
1269 else
1270 res = 1;
1271 if (res > 0)
1272 res = 1;
1273 return res;
1274}
1275
Guido van Rossumc3d3f961998-04-09 17:53:59 +00001276/* equivalent of 'not v'
1277 Return -1 if an error occurred */
1278
1279int
Fred Drake100814d2000-07-09 15:48:49 +00001280PyObject_Not(PyObject *v)
Guido van Rossumc3d3f961998-04-09 17:53:59 +00001281{
1282 int res;
1283 res = PyObject_IsTrue(v);
1284 if (res < 0)
1285 return res;
1286 return res == 0;
1287}
1288
Guido van Rossum5524a591995-01-10 15:26:20 +00001289/* Coerce two numeric types to the "larger" one.
1290 Increment the reference count on each argument.
Guido van Rossume797ec12001-01-17 15:24:28 +00001291 Return value:
1292 -1 if an error occurred;
1293 0 if the coercion succeeded (and then the reference counts are increased);
1294 1 if no coercion is possible (and no error is raised).
Guido van Rossum5524a591995-01-10 15:26:20 +00001295*/
Guido van Rossum5524a591995-01-10 15:26:20 +00001296int
Fred Drake100814d2000-07-09 15:48:49 +00001297PyNumber_CoerceEx(PyObject **pv, PyObject **pw)
Guido van Rossum5524a591995-01-10 15:26:20 +00001298{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001299 register PyObject *v = *pv;
1300 register PyObject *w = *pw;
Guido van Rossum5524a591995-01-10 15:26:20 +00001301 int res;
1302
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001303 if (v->ob_type == w->ob_type && !PyInstance_Check(v)) {
1304 Py_INCREF(v);
1305 Py_INCREF(w);
Guido van Rossum5524a591995-01-10 15:26:20 +00001306 return 0;
1307 }
1308 if (v->ob_type->tp_as_number && v->ob_type->tp_as_number->nb_coerce) {
1309 res = (*v->ob_type->tp_as_number->nb_coerce)(pv, pw);
1310 if (res <= 0)
1311 return res;
1312 }
1313 if (w->ob_type->tp_as_number && w->ob_type->tp_as_number->nb_coerce) {
1314 res = (*w->ob_type->tp_as_number->nb_coerce)(pw, pv);
1315 if (res <= 0)
1316 return res;
1317 }
Guido van Rossum242c6421997-11-19 16:03:17 +00001318 return 1;
1319}
1320
Guido van Rossume797ec12001-01-17 15:24:28 +00001321/* Coerce two numeric types to the "larger" one.
1322 Increment the reference count on each argument.
1323 Return -1 and raise an exception if no coercion is possible
1324 (and then no reference count is incremented).
1325*/
Guido van Rossum242c6421997-11-19 16:03:17 +00001326int
Fred Drake100814d2000-07-09 15:48:49 +00001327PyNumber_Coerce(PyObject **pv, PyObject **pw)
Guido van Rossum242c6421997-11-19 16:03:17 +00001328{
1329 int err = PyNumber_CoerceEx(pv, pw);
1330 if (err <= 0)
1331 return err;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001332 PyErr_SetString(PyExc_TypeError, "number coercion failed");
Guido van Rossum5524a591995-01-10 15:26:20 +00001333 return -1;
1334}
1335
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001336
Guido van Rossum49b11fe1995-01-26 00:38:22 +00001337/* Test whether an object can be called */
1338
1339int
Fred Drake100814d2000-07-09 15:48:49 +00001340PyCallable_Check(PyObject *x)
Guido van Rossum49b11fe1995-01-26 00:38:22 +00001341{
1342 if (x == NULL)
1343 return 0;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001344 if (PyInstance_Check(x)) {
1345 PyObject *call = PyObject_GetAttrString(x, "__call__");
Guido van Rossum49b11fe1995-01-26 00:38:22 +00001346 if (call == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001347 PyErr_Clear();
Guido van Rossum49b11fe1995-01-26 00:38:22 +00001348 return 0;
1349 }
1350 /* Could test recursively but don't, for fear of endless
1351 recursion if some joker sets self.__call__ = self */
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001352 Py_DECREF(call);
Guido van Rossum49b11fe1995-01-26 00:38:22 +00001353 return 1;
1354 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001355 else {
1356 return x->ob_type->tp_call != NULL;
1357 }
Guido van Rossum49b11fe1995-01-26 00:38:22 +00001358}
1359
Tim Peters7eea37e2001-09-04 22:08:56 +00001360/* Helper for PyObject_Dir.
1361 Merge the __dict__ of aclass into dict, and recursively also all
1362 the __dict__s of aclass's base classes. The order of merging isn't
1363 defined, as it's expected that only the final set of dict keys is
1364 interesting.
1365 Return 0 on success, -1 on error.
1366*/
1367
1368static int
1369merge_class_dict(PyObject* dict, PyObject* aclass)
1370{
1371 PyObject *classdict;
1372 PyObject *bases;
1373
1374 assert(PyDict_Check(dict));
1375 assert(aclass);
1376
1377 /* Merge in the type's dict (if any). */
1378 classdict = PyObject_GetAttrString(aclass, "__dict__");
1379 if (classdict == NULL)
1380 PyErr_Clear();
1381 else {
1382 int status = PyDict_Update(dict, classdict);
1383 Py_DECREF(classdict);
1384 if (status < 0)
1385 return -1;
1386 }
1387
1388 /* Recursively merge in the base types' (if any) dicts. */
1389 bases = PyObject_GetAttrString(aclass, "__bases__");
1390 if (bases != NULL) {
1391 int i, n;
1392 assert(PyTuple_Check(bases));
1393 n = PyTuple_GET_SIZE(bases);
1394 for (i = 0; i < n; i++) {
1395 PyObject *base = PyTuple_GET_ITEM(bases, i);
1396 if (merge_class_dict(dict, base) < 0) {
1397 Py_DECREF(bases);
1398 return -1;
1399 }
1400 }
1401 Py_DECREF(bases);
1402 }
1403 return 0;
1404}
1405
1406/* Like __builtin__.dir(arg). See bltinmodule.c's builtin_dir for the
1407 docstring, which should be kept in synch with this implementation. */
1408
1409PyObject *
1410PyObject_Dir(PyObject *arg)
1411{
1412 /* Set exactly one of these non-NULL before the end. */
1413 PyObject *result = NULL; /* result list */
1414 PyObject *masterdict = NULL; /* result is masterdict.keys() */
1415
1416 /* If NULL arg, return the locals. */
1417 if (arg == NULL) {
1418 PyObject *locals = PyEval_GetLocals();
1419 if (locals == NULL)
1420 goto error;
1421 result = PyDict_Keys(locals);
1422 if (result == NULL)
1423 goto error;
1424 }
1425
1426 /* Elif this is some form of module, we only want its dict. */
1427 else if (PyObject_TypeCheck(arg, &PyModule_Type)) {
1428 masterdict = PyObject_GetAttrString(arg, "__dict__");
1429 if (masterdict == NULL)
1430 goto error;
1431 assert(PyDict_Check(masterdict));
1432 }
1433
1434 /* Elif some form of type or class, grab its dict and its bases.
1435 We deliberately don't suck up its __class__, as methods belonging
1436 to the metaclass would probably be more confusing than helpful. */
1437 else if (PyType_Check(arg) || PyClass_Check(arg)) {
1438 masterdict = PyDict_New();
1439 if (masterdict == NULL)
1440 goto error;
1441 if (merge_class_dict(masterdict, arg) < 0)
1442 goto error;
1443 }
1444
1445 /* Else look at its dict, and the attrs reachable from its class. */
1446 else {
1447 PyObject *itsclass;
1448 /* Create a dict to start with. CAUTION: Not everything
1449 responding to __dict__ returns a dict! */
1450 masterdict = PyObject_GetAttrString(arg, "__dict__");
1451 if (masterdict == NULL) {
1452 PyErr_Clear();
1453 masterdict = PyDict_New();
1454 }
1455 else if (!PyDict_Check(masterdict)) {
1456 Py_DECREF(masterdict);
1457 masterdict = PyDict_New();
1458 }
1459 else {
1460 /* The object may have returned a reference to its
1461 dict, so copy it to avoid mutating it. */
1462 PyObject *temp = PyDict_Copy(masterdict);
1463 Py_DECREF(masterdict);
1464 masterdict = temp;
1465 }
1466 if (masterdict == NULL)
1467 goto error;
1468
1469 /* Merge in attrs reachable from its class.
1470 CAUTION: Not all objects have a __class__ attr. */
1471 itsclass = PyObject_GetAttrString(arg, "__class__");
1472 if (itsclass == NULL)
1473 PyErr_Clear();
1474 else {
1475 int status = merge_class_dict(masterdict, itsclass);
1476 Py_DECREF(itsclass);
1477 if (status < 0)
1478 goto error;
1479 }
1480 }
1481
1482 assert((result == NULL) ^ (masterdict == NULL));
1483 if (masterdict != NULL) {
1484 /* The result comes from its keys. */
1485 assert(result == NULL);
1486 result = PyDict_Keys(masterdict);
1487 if (result == NULL)
1488 goto error;
1489 }
1490
1491 assert(result);
1492 if (PyList_Sort(result) != 0)
1493 goto error;
1494 else
1495 goto normal_return;
1496
1497 error:
1498 Py_XDECREF(result);
1499 result = NULL;
1500 /* fall through */
1501 normal_return:
1502 Py_XDECREF(masterdict);
1503 return result;
1504}
Guido van Rossum49b11fe1995-01-26 00:38:22 +00001505
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001506/*
1507NoObject is usable as a non-NULL undefined value, used by the macro None.
1508There is (and should be!) no way to create other objects of this type,
Guido van Rossum3f5da241990-12-20 15:06:42 +00001509so there is exactly one (which is indestructible, by the way).
Guido van Rossumba21a492001-08-16 08:17:26 +00001510(XXX This type and the type of NotImplemented below should be unified.)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001511*/
1512
Guido van Rossum0c182a11992-03-27 17:26:13 +00001513/* ARGSUSED */
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001514static PyObject *
Fred Drake100814d2000-07-09 15:48:49 +00001515none_repr(PyObject *op)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001516{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001517 return PyString_FromString("None");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001518}
1519
Barry Warsaw9bf16442001-01-23 16:24:35 +00001520/* ARGUSED */
1521static void
1522none_dealloc(PyObject* ignore)
1523{
1524 /* This should never get called, but we also don't want to SEGV if
1525 * we accidently decref None out of existance.
1526 */
1527 abort();
1528}
1529
1530
Guido van Rossumba21a492001-08-16 08:17:26 +00001531static PyTypeObject PyNone_Type = {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001532 PyObject_HEAD_INIT(&PyType_Type)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001533 0,
Guido van Rossumba21a492001-08-16 08:17:26 +00001534 "NoneType",
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001535 0,
1536 0,
Barry Warsaw9bf16442001-01-23 16:24:35 +00001537 (destructor)none_dealloc, /*tp_dealloc*/ /*never called*/
Guido van Rossum7066dd71992-09-17 17:54:56 +00001538 0, /*tp_print*/
Guido van Rossum3f5da241990-12-20 15:06:42 +00001539 0, /*tp_getattr*/
1540 0, /*tp_setattr*/
1541 0, /*tp_compare*/
Guido van Rossum1d5735e1994-08-30 08:27:36 +00001542 (reprfunc)none_repr, /*tp_repr*/
Guido van Rossum3f5da241990-12-20 15:06:42 +00001543 0, /*tp_as_number*/
1544 0, /*tp_as_sequence*/
1545 0, /*tp_as_mapping*/
Guido van Rossum9bfef441993-03-29 10:43:31 +00001546 0, /*tp_hash */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001547};
1548
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001549PyObject _Py_NoneStruct = {
Guido van Rossumba21a492001-08-16 08:17:26 +00001550 PyObject_HEAD_INIT(&PyNone_Type)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001551};
1552
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +00001553/* NotImplemented is an object that can be used to signal that an
1554 operation is not implemented for the given type combination. */
1555
1556static PyObject *
1557NotImplemented_repr(PyObject *op)
1558{
1559 return PyString_FromString("NotImplemented");
1560}
1561
1562static PyTypeObject PyNotImplemented_Type = {
1563 PyObject_HEAD_INIT(&PyType_Type)
1564 0,
Guido van Rossumba21a492001-08-16 08:17:26 +00001565 "NotImplementedType",
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +00001566 0,
1567 0,
Barry Warsaw9bf16442001-01-23 16:24:35 +00001568 (destructor)none_dealloc, /*tp_dealloc*/ /*never called*/
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +00001569 0, /*tp_print*/
1570 0, /*tp_getattr*/
1571 0, /*tp_setattr*/
1572 0, /*tp_compare*/
1573 (reprfunc)NotImplemented_repr, /*tp_repr*/
1574 0, /*tp_as_number*/
1575 0, /*tp_as_sequence*/
1576 0, /*tp_as_mapping*/
1577 0, /*tp_hash */
1578};
1579
1580PyObject _Py_NotImplementedStruct = {
1581 PyObject_HEAD_INIT(&PyNotImplemented_Type)
1582};
1583
Guido van Rossumba21a492001-08-16 08:17:26 +00001584void
1585_Py_ReadyTypes(void)
1586{
1587 if (PyType_Ready(&PyType_Type) < 0)
1588 Py_FatalError("Can't initialize 'type'");
1589
1590 if (PyType_Ready(&PyList_Type) < 0)
1591 Py_FatalError("Can't initialize 'list'");
1592
1593 if (PyType_Ready(&PyNone_Type) < 0)
1594 Py_FatalError("Can't initialize type(None)");
1595
1596 if (PyType_Ready(&PyNotImplemented_Type) < 0)
1597 Py_FatalError("Can't initialize type(NotImplemented)");
1598}
1599
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001600
Guido van Rossum84a90321996-05-22 16:34:47 +00001601#ifdef Py_TRACE_REFS
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001602
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001603static PyObject refchain = {&refchain, &refchain};
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001604
Guido van Rossumaacdc9d1996-08-12 21:32:12 +00001605void
Fred Drake100814d2000-07-09 15:48:49 +00001606_Py_ResetReferences(void)
Guido van Rossume09fb551997-08-05 02:04:34 +00001607{
1608 refchain._ob_prev = refchain._ob_next = &refchain;
1609 _Py_RefTotal = 0;
1610}
1611
1612void
Fred Drake100814d2000-07-09 15:48:49 +00001613_Py_NewReference(PyObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001614{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001615 _Py_RefTotal++;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001616 op->ob_refcnt = 1;
1617 op->_ob_next = refchain._ob_next;
1618 op->_ob_prev = &refchain;
1619 refchain._ob_next->_ob_prev = op;
1620 refchain._ob_next = op;
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +00001621#ifdef COUNT_ALLOCS
1622 inc_count(op->ob_type);
1623#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001624}
1625
Guido van Rossumaacdc9d1996-08-12 21:32:12 +00001626void
Fred Drake100814d2000-07-09 15:48:49 +00001627_Py_ForgetReference(register PyObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001628{
Guido van Rossumbffd6832000-01-20 22:32:56 +00001629#ifdef SLOW_UNREF_CHECK
Guido van Rossum4c08d552000-03-10 22:55:18 +00001630 register PyObject *p;
Guido van Rossumbffd6832000-01-20 22:32:56 +00001631#endif
Guido van Rossumd7047b31995-01-02 19:07:15 +00001632 if (op->ob_refcnt < 0)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001633 Py_FatalError("UNREF negative refcnt");
Guido van Rossum2e8f6141992-09-03 20:32:55 +00001634 if (op == &refchain ||
Guido van Rossumd7047b31995-01-02 19:07:15 +00001635 op->_ob_prev->_ob_next != op || op->_ob_next->_ob_prev != op)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001636 Py_FatalError("UNREF invalid object");
Guido van Rossum2e8f6141992-09-03 20:32:55 +00001637#ifdef SLOW_UNREF_CHECK
Guido van Rossum3f5da241990-12-20 15:06:42 +00001638 for (p = refchain._ob_next; p != &refchain; p = p->_ob_next) {
1639 if (p == op)
1640 break;
1641 }
Guido van Rossumd7047b31995-01-02 19:07:15 +00001642 if (p == &refchain) /* Not found */
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001643 Py_FatalError("UNREF unknown object");
Guido van Rossum2e8f6141992-09-03 20:32:55 +00001644#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001645 op->_ob_next->_ob_prev = op->_ob_prev;
1646 op->_ob_prev->_ob_next = op->_ob_next;
Guido van Rossum2e8f6141992-09-03 20:32:55 +00001647 op->_ob_next = op->_ob_prev = NULL;
Guido van Rossumd8953cb1995-04-06 14:46:26 +00001648#ifdef COUNT_ALLOCS
Tim Peters6d6c1a32001-08-02 04:15:00 +00001649 op->ob_type->tp_frees++;
Guido van Rossumd8953cb1995-04-06 14:46:26 +00001650#endif
Guido van Rossum3f5da241990-12-20 15:06:42 +00001651}
1652
Guido van Rossumaacdc9d1996-08-12 21:32:12 +00001653void
Fred Drake100814d2000-07-09 15:48:49 +00001654_Py_Dealloc(PyObject *op)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001655{
Guido van Rossum9776adf1994-09-07 14:36:45 +00001656 destructor dealloc = op->ob_type->tp_dealloc;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001657 _Py_ForgetReference(op);
Guido van Rossum9776adf1994-09-07 14:36:45 +00001658 (*dealloc)(op);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001659}
1660
Guido van Rossumaacdc9d1996-08-12 21:32:12 +00001661void
Fred Drake100814d2000-07-09 15:48:49 +00001662_Py_PrintReferences(FILE *fp)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001663{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001664 PyObject *op;
Guido van Rossume09fb551997-08-05 02:04:34 +00001665 fprintf(fp, "Remaining objects:\n");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001666 for (op = refchain._ob_next; op != &refchain; op = op->_ob_next) {
1667 fprintf(fp, "[%d] ", op->ob_refcnt);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001668 if (PyObject_Print(op, fp, 0) != 0)
1669 PyErr_Clear();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001670 putc('\n', fp);
1671 }
1672}
1673
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +00001674PyObject *
Fred Drake100814d2000-07-09 15:48:49 +00001675_Py_GetObjects(PyObject *self, PyObject *args)
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +00001676{
1677 int i, n;
1678 PyObject *t = NULL;
1679 PyObject *res, *op;
1680
1681 if (!PyArg_ParseTuple(args, "i|O", &n, &t))
1682 return NULL;
1683 op = refchain._ob_next;
1684 res = PyList_New(0);
1685 if (res == NULL)
1686 return NULL;
1687 for (i = 0; (n == 0 || i < n) && op != &refchain; i++) {
1688 while (op == self || op == args || op == res || op == t ||
Guido van Rossum3c296022001-07-14 17:58:00 +00001689 (t != NULL && op->ob_type != (PyTypeObject *) t)) {
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +00001690 op = op->_ob_next;
1691 if (op == &refchain)
1692 return res;
1693 }
1694 if (PyList_Append(res, op) < 0) {
1695 Py_DECREF(res);
1696 return NULL;
1697 }
1698 op = op->_ob_next;
1699 }
1700 return res;
1701}
1702
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001703#endif
Guido van Rossum97ead3f1996-01-12 01:24:09 +00001704
1705
1706/* Hack to force loading of cobject.o */
Guido van Rossumda9c2711996-12-05 21:58:58 +00001707PyTypeObject *_Py_cobject_hack = &PyCObject_Type;
Guido van Rossum84a90321996-05-22 16:34:47 +00001708
1709
1710/* Hack to force loading of abstract.o */
Jeremy Hylton6253f832000-07-12 12:56:19 +00001711int (*_Py_abstract_hack)(PyObject *) = &PyObject_Size;
Guido van Rossume09fb551997-08-05 02:04:34 +00001712
1713
Andrew M. Kuchling1582a3a2000-08-16 12:27:23 +00001714/* Python's malloc wrappers (see pymem.h) */
Guido van Rossume09fb551997-08-05 02:04:34 +00001715
Thomas Wouters334fb892000-07-25 12:56:38 +00001716void *
Fred Drake100814d2000-07-09 15:48:49 +00001717PyMem_Malloc(size_t nbytes)
Guido van Rossume09fb551997-08-05 02:04:34 +00001718{
1719#if _PyMem_EXTRA > 0
1720 if (nbytes == 0)
1721 nbytes = _PyMem_EXTRA;
1722#endif
Guido van Rossumb18618d2000-05-03 23:44:39 +00001723 return PyMem_MALLOC(nbytes);
Guido van Rossume09fb551997-08-05 02:04:34 +00001724}
1725
Thomas Wouters334fb892000-07-25 12:56:38 +00001726void *
1727PyMem_Realloc(void *p, size_t nbytes)
Guido van Rossume09fb551997-08-05 02:04:34 +00001728{
1729#if _PyMem_EXTRA > 0
1730 if (nbytes == 0)
1731 nbytes = _PyMem_EXTRA;
1732#endif
Guido van Rossumb18618d2000-05-03 23:44:39 +00001733 return PyMem_REALLOC(p, nbytes);
Guido van Rossume09fb551997-08-05 02:04:34 +00001734}
1735
1736void
Thomas Wouters334fb892000-07-25 12:56:38 +00001737PyMem_Free(void *p)
Guido van Rossume09fb551997-08-05 02:04:34 +00001738{
Guido van Rossumb18618d2000-05-03 23:44:39 +00001739 PyMem_FREE(p);
1740}
1741
1742
1743/* Python's object malloc wrappers (see objimpl.h) */
1744
Thomas Wouters334fb892000-07-25 12:56:38 +00001745void *
Fred Drake100814d2000-07-09 15:48:49 +00001746PyObject_Malloc(size_t nbytes)
Guido van Rossumb18618d2000-05-03 23:44:39 +00001747{
1748 return PyObject_MALLOC(nbytes);
1749}
1750
Thomas Wouters334fb892000-07-25 12:56:38 +00001751void *
1752PyObject_Realloc(void *p, size_t nbytes)
Guido van Rossumb18618d2000-05-03 23:44:39 +00001753{
1754 return PyObject_REALLOC(p, nbytes);
1755}
1756
1757void
Thomas Wouters334fb892000-07-25 12:56:38 +00001758PyObject_Free(void *p)
Guido van Rossumb18618d2000-05-03 23:44:39 +00001759{
1760 PyObject_FREE(p);
Guido van Rossume09fb551997-08-05 02:04:34 +00001761}
Guido van Rossum86610361998-04-10 22:32:46 +00001762
1763
Fred Drake41deb1e2001-02-01 05:27:45 +00001764/* Hook to clear up weak references only once the _weakref module is
1765 imported. We use a dummy implementation to simplify the code at each
1766 call site instead of requiring a test for NULL.
1767*/
1768
Fred Drakeb60654b2001-02-26 18:56:37 +00001769static void
Fred Drake41deb1e2001-02-01 05:27:45 +00001770empty_clear_weak_refs(PyObject *o)
1771{
Fred Drakeb60654b2001-02-26 18:56:37 +00001772 return;
Fred Drake41deb1e2001-02-01 05:27:45 +00001773}
1774
Fred Drakeb60654b2001-02-26 18:56:37 +00001775void (*PyObject_ClearWeakRefs)(PyObject *) = empty_clear_weak_refs;
Fred Drake41deb1e2001-02-01 05:27:45 +00001776
1777
1778
Guido van Rossum86610361998-04-10 22:32:46 +00001779/* These methods are used to control infinite recursion in repr, str, print,
1780 etc. Container objects that may recursively contain themselves,
1781 e.g. builtin dictionaries and lists, should used Py_ReprEnter() and
1782 Py_ReprLeave() to avoid infinite recursion.
1783
1784 Py_ReprEnter() returns 0 the first time it is called for a particular
1785 object and 1 every time thereafter. It returns -1 if an exception
1786 occurred. Py_ReprLeave() has no return value.
1787
1788 See dictobject.c and listobject.c for examples of use.
1789*/
1790
1791#define KEY "Py_Repr"
1792
1793int
Fred Drake100814d2000-07-09 15:48:49 +00001794Py_ReprEnter(PyObject *obj)
Guido van Rossum86610361998-04-10 22:32:46 +00001795{
1796 PyObject *dict;
1797 PyObject *list;
1798 int i;
1799
1800 dict = PyThreadState_GetDict();
1801 if (dict == NULL)
1802 return -1;
1803 list = PyDict_GetItemString(dict, KEY);
1804 if (list == NULL) {
1805 list = PyList_New(0);
1806 if (list == NULL)
1807 return -1;
1808 if (PyDict_SetItemString(dict, KEY, list) < 0)
1809 return -1;
1810 Py_DECREF(list);
1811 }
1812 i = PyList_GET_SIZE(list);
1813 while (--i >= 0) {
1814 if (PyList_GET_ITEM(list, i) == obj)
1815 return 1;
1816 }
1817 PyList_Append(list, obj);
1818 return 0;
1819}
1820
1821void
Fred Drake100814d2000-07-09 15:48:49 +00001822Py_ReprLeave(PyObject *obj)
Guido van Rossum86610361998-04-10 22:32:46 +00001823{
1824 PyObject *dict;
1825 PyObject *list;
1826 int i;
1827
1828 dict = PyThreadState_GetDict();
Guido van Rossumeb909461998-04-11 15:17:34 +00001829 if (dict == NULL)
1830 return;
Guido van Rossum86610361998-04-10 22:32:46 +00001831 list = PyDict_GetItemString(dict, KEY);
Guido van Rossumeb909461998-04-11 15:17:34 +00001832 if (list == NULL || !PyList_Check(list))
1833 return;
Guido van Rossum86610361998-04-10 22:32:46 +00001834 i = PyList_GET_SIZE(list);
1835 /* Count backwards because we always expect obj to be list[-1] */
1836 while (--i >= 0) {
1837 if (PyList_GET_ITEM(list, i) == obj) {
1838 PyList_SetSlice(list, i, i + 1, NULL);
1839 break;
1840 }
1841 }
1842}
Guido van Rossumd724b232000-03-13 16:01:29 +00001843
1844/*
1845 trashcan
1846 CT 2k0130
1847 non-recursively destroy nested objects
1848
1849 CT 2k0223
1850 everything is now done in a macro.
1851
1852 CT 2k0305
1853 modified to use functions, after Tim Peter's suggestion.
1854
1855 CT 2k0309
1856 modified to restore a possible error.
Guido van Rossum13ff8eb2000-03-25 18:39:19 +00001857
1858 CT 2k0325
1859 added better safe than sorry check for threadstate
Guido van Rossume92e6102000-04-24 15:40:53 +00001860
1861 CT 2k0422
1862 complete rewrite. We now build a chain via ob_type
1863 and save the limited number of types in ob_refcnt.
1864 This is perfect since we don't need any memory.
1865 A patch for free-threading would need just a lock.
Guido van Rossumd724b232000-03-13 16:01:29 +00001866*/
1867
Guido van Rossume92e6102000-04-24 15:40:53 +00001868#define Py_TRASHCAN_TUPLE 1
1869#define Py_TRASHCAN_LIST 2
1870#define Py_TRASHCAN_DICT 3
1871#define Py_TRASHCAN_FRAME 4
1872#define Py_TRASHCAN_TRACEBACK 5
1873/* extend here if other objects want protection */
1874
Guido van Rossumd724b232000-03-13 16:01:29 +00001875int _PyTrash_delete_nesting = 0;
Guido van Rossume92e6102000-04-24 15:40:53 +00001876
Guido van Rossumd724b232000-03-13 16:01:29 +00001877PyObject * _PyTrash_delete_later = NULL;
1878
1879void
Fred Drake100814d2000-07-09 15:48:49 +00001880_PyTrash_deposit_object(PyObject *op)
Guido van Rossumd724b232000-03-13 16:01:29 +00001881{
Guido van Rossume92e6102000-04-24 15:40:53 +00001882 int typecode;
Guido van Rossum13ff8eb2000-03-25 18:39:19 +00001883
Guido van Rossume92e6102000-04-24 15:40:53 +00001884 if (PyTuple_Check(op))
1885 typecode = Py_TRASHCAN_TUPLE;
1886 else if (PyList_Check(op))
1887 typecode = Py_TRASHCAN_LIST;
1888 else if (PyDict_Check(op))
1889 typecode = Py_TRASHCAN_DICT;
1890 else if (PyFrame_Check(op))
1891 typecode = Py_TRASHCAN_FRAME;
1892 else if (PyTraceBack_Check(op))
1893 typecode = Py_TRASHCAN_TRACEBACK;
Moshe Zadkacf703f02000-08-04 15:36:13 +00001894 else /* We have a bug here -- those are the only types in GC */ {
1895 Py_FatalError("Type not supported in GC -- internal bug");
1896 return; /* pacify compiler -- execution never here */
1897 }
Guido van Rossume92e6102000-04-24 15:40:53 +00001898 op->ob_refcnt = typecode;
Guido van Rossumd724b232000-03-13 16:01:29 +00001899
Guido van Rossume92e6102000-04-24 15:40:53 +00001900 op->ob_type = (PyTypeObject*)_PyTrash_delete_later;
1901 _PyTrash_delete_later = op;
Guido van Rossumd724b232000-03-13 16:01:29 +00001902}
1903
1904void
Fred Drake100814d2000-07-09 15:48:49 +00001905_PyTrash_destroy_chain(void)
Guido van Rossumd724b232000-03-13 16:01:29 +00001906{
1907 while (_PyTrash_delete_later) {
1908 PyObject *shredder = _PyTrash_delete_later;
Guido van Rossume92e6102000-04-24 15:40:53 +00001909 _PyTrash_delete_later = (PyObject*) shredder->ob_type;
1910
1911 switch (shredder->ob_refcnt) {
1912 case Py_TRASHCAN_TUPLE:
1913 shredder->ob_type = &PyTuple_Type;
1914 break;
1915 case Py_TRASHCAN_LIST:
1916 shredder->ob_type = &PyList_Type;
1917 break;
1918 case Py_TRASHCAN_DICT:
1919 shredder->ob_type = &PyDict_Type;
1920 break;
1921 case Py_TRASHCAN_FRAME:
1922 shredder->ob_type = &PyFrame_Type;
1923 break;
1924 case Py_TRASHCAN_TRACEBACK:
1925 shredder->ob_type = &PyTraceBack_Type;
1926 break;
1927 }
1928 _Py_NewReference(shredder);
1929
Guido van Rossumd724b232000-03-13 16:01:29 +00001930 ++_PyTrash_delete_nesting;
1931 Py_DECREF(shredder);
1932 --_PyTrash_delete_nesting;
1933 }
1934}
Neil Schemenauera35c6882001-02-27 04:45:05 +00001935
1936#ifdef WITH_PYMALLOC
1937#include "obmalloc.c"
1938#endif