blob: 718dddf7cfe31057341bab8a296019269a22d10e [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>");
Tim Peters5a49ade2001-09-11 01:41:59 +0000253 if (PyString_CheckExact(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 }
Tim Peters5a49ade2001-09-11 01:41:59 +0000257 if (PyString_Check(v)) {
258 /* For a string subtype that's not a string, return a true
259 string with the same string data. */
260 PyStringObject *s = (PyStringObject *)v;
261 return PyString_FromStringAndSize(s->ob_sval, s->ob_size);
262 }
Guido van Rossum4f288ab2001-05-01 16:53:37 +0000263 if (v->ob_type->tp_str == NULL)
264 return PyObject_Repr(v);
265
266 res = (*v->ob_type->tp_str)(v);
Guido van Rossum4c08d552000-03-10 22:55:18 +0000267 if (res == NULL)
268 return NULL;
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000269#ifdef Py_USING_UNICODE
Marc-André Lemburg891bc652000-07-03 09:57:53 +0000270 if (PyUnicode_Check(res)) {
271 PyObject* str;
272 str = PyUnicode_AsEncodedString(res, NULL, NULL);
273 Py_DECREF(res);
274 if (str)
275 res = str;
276 else
277 return NULL;
278 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000279#endif
Guido van Rossum4c08d552000-03-10 22:55:18 +0000280 if (!PyString_Check(res)) {
281 PyErr_Format(PyExc_TypeError,
Guido van Rossum5db862d2000-04-10 12:46:51 +0000282 "__str__ returned non-string (type %.200s)",
Guido van Rossum4c08d552000-03-10 22:55:18 +0000283 res->ob_type->tp_name);
284 Py_DECREF(res);
285 return NULL;
286 }
287 return res;
Guido van Rossumc6004111993-11-05 10:22:19 +0000288}
289
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000290#ifdef Py_USING_UNICODE
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000291PyObject *
292PyObject_Unicode(PyObject *v)
293{
294 PyObject *res;
295
296 if (v == NULL)
297 res = PyString_FromString("<NULL>");
298 else if (PyUnicode_Check(v)) {
299 Py_INCREF(v);
300 return v;
301 }
Marc-André Lemburgae605342001-03-25 19:16:13 +0000302 else if (PyString_Check(v)) {
303 Py_INCREF(v);
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000304 res = v;
Marc-André Lemburgae605342001-03-25 19:16:13 +0000305 }
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000306 else if (v->ob_type->tp_str != NULL)
307 res = (*v->ob_type->tp_str)(v);
308 else {
309 PyObject *func;
310 static PyObject *strstr;
311 if (strstr == NULL) {
312 strstr= PyString_InternFromString("__str__");
313 if (strstr == NULL)
314 return NULL;
315 }
316 if (!PyInstance_Check(v) ||
317 (func = PyObject_GetAttr(v, strstr)) == NULL) {
318 PyErr_Clear();
319 res = PyObject_Repr(v);
320 }
321 else {
322 res = PyEval_CallObject(func, (PyObject *)NULL);
323 Py_DECREF(func);
324 }
325 }
326 if (res == NULL)
327 return NULL;
328 if (!PyUnicode_Check(res)) {
329 PyObject* str;
330 str = PyUnicode_FromObject(res);
331 Py_DECREF(res);
332 if (str)
333 res = str;
334 else
335 return NULL;
336 }
337 return res;
338}
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000339#endif
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000340
341
Guido van Rossumd1f06b92001-01-24 22:14:43 +0000342/* Macro to get the tp_richcompare field of a type if defined */
343#define RICHCOMPARE(t) (PyType_HasFeature((t), Py_TPFLAGS_HAVE_RICHCOMPARE) \
344 ? (t)->tp_richcompare : NULL)
345
Guido van Rossume797ec12001-01-17 15:24:28 +0000346/* Map rich comparison operators to their swapped version, e.g. LT --> GT */
347static int swapped_op[] = {Py_GT, Py_GE, Py_EQ, Py_NE, Py_LT, Py_LE};
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +0000348
Guido van Rossume797ec12001-01-17 15:24:28 +0000349/* Try a genuine rich comparison, returning an object. Return:
350 NULL for exception;
351 NotImplemented if this particular rich comparison is not implemented or
352 undefined;
353 some object not equal to NotImplemented if it is implemented
354 (this latter object may not be a Boolean).
355*/
356static PyObject *
357try_rich_compare(PyObject *v, PyObject *w, int op)
358{
359 richcmpfunc f;
360 PyObject *res;
361
Guido van Rossumd1f06b92001-01-24 22:14:43 +0000362 if ((f = RICHCOMPARE(v->ob_type)) != NULL) {
Guido van Rossume797ec12001-01-17 15:24:28 +0000363 res = (*f)(v, w, op);
364 if (res != Py_NotImplemented)
365 return res;
366 Py_DECREF(res);
367 }
Guido van Rossumd1f06b92001-01-24 22:14:43 +0000368 if ((f = RICHCOMPARE(w->ob_type)) != NULL) {
Guido van Rossume797ec12001-01-17 15:24:28 +0000369 return (*f)(w, v, swapped_op[op]);
370 }
371 res = Py_NotImplemented;
372 Py_INCREF(res);
373 return res;
374}
375
376/* Try a genuine rich comparison, returning an int. Return:
377 -1 for exception (including the case where try_rich_compare() returns an
378 object that's not a Boolean);
379 0 if the outcome is false;
380 1 if the outcome is true;
381 2 if this particular rich comparison is not implemented or undefined.
382*/
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +0000383static int
Guido van Rossume797ec12001-01-17 15:24:28 +0000384try_rich_compare_bool(PyObject *v, PyObject *w, int op)
385{
386 PyObject *res;
387 int ok;
388
Guido van Rossumd1f06b92001-01-24 22:14:43 +0000389 if (RICHCOMPARE(v->ob_type) == NULL && RICHCOMPARE(w->ob_type) == NULL)
Guido van Rossume797ec12001-01-17 15:24:28 +0000390 return 2; /* Shortcut, avoid INCREF+DECREF */
391 res = try_rich_compare(v, w, op);
392 if (res == NULL)
393 return -1;
394 if (res == Py_NotImplemented) {
395 Py_DECREF(res);
396 return 2;
397 }
398 ok = PyObject_IsTrue(res);
399 Py_DECREF(res);
400 return ok;
401}
402
403/* Try rich comparisons to determine a 3-way comparison. 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 rich comparison is not implemented or undefined.
409*/
410static int
411try_rich_to_3way_compare(PyObject *v, PyObject *w)
412{
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000413 static struct { int op; int outcome; } tries[3] = {
414 /* Try this operator, and if it is true, use this outcome: */
415 {Py_EQ, 0},
416 {Py_LT, -1},
417 {Py_GT, 1},
418 };
419 int i;
420
Guido van Rossumd1f06b92001-01-24 22:14:43 +0000421 if (RICHCOMPARE(v->ob_type) == NULL && RICHCOMPARE(w->ob_type) == NULL)
Guido van Rossume797ec12001-01-17 15:24:28 +0000422 return 2; /* Shortcut */
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000423
424 for (i = 0; i < 3; i++) {
425 switch (try_rich_compare_bool(v, w, tries[i].op)) {
426 case -1:
Tim Peters6d60b2e2001-05-07 20:53:51 +0000427 return -2;
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000428 case 1:
429 return tries[i].outcome;
430 }
Guido van Rossume797ec12001-01-17 15:24:28 +0000431 }
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000432
433 return 2;
Guido van Rossume797ec12001-01-17 15:24:28 +0000434}
435
436/* Try a 3-way comparison, returning an int. Return:
437 -2 for an exception;
438 -1 if v < w;
439 0 if v == w;
440 1 if v > w;
441 2 if this particular 3-way comparison is not implemented or undefined.
442*/
443static int
444try_3way_compare(PyObject *v, PyObject *w)
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +0000445{
446 int c;
Guido van Rossume797ec12001-01-17 15:24:28 +0000447 cmpfunc f;
448
449 /* Comparisons involving instances are given to instance_compare,
450 which has the same return conventions as this function. */
451
452 if (PyInstance_Check(v))
453 return (*v->ob_type->tp_compare)(v, w);
454 if (PyInstance_Check(w))
455 return (*w->ob_type->tp_compare)(v, w);
456
Guido van Rossume797ec12001-01-17 15:24:28 +0000457 /* Try coercion; if it fails, give up */
458 c = PyNumber_CoerceEx(&v, &w);
459 if (c < 0)
460 return -2;
461 if (c > 0)
462 return 2;
463
464 /* Try v's comparison, if defined */
465 if ((f = v->ob_type->tp_compare) != NULL) {
466 c = (*f)(v, w);
467 Py_DECREF(v);
468 Py_DECREF(w);
Martin v. Löwis0163d6d2001-06-09 07:34:05 +0000469 if (c < 0 && PyErr_Occurred())
Guido van Rossume797ec12001-01-17 15:24:28 +0000470 return -2;
471 return c < 0 ? -1 : c > 0 ? 1 : 0;
472 }
473
474 /* Try w's comparison, if defined */
475 if ((f = w->ob_type->tp_compare) != NULL) {
476 c = (*f)(w, v); /* swapped! */
477 Py_DECREF(v);
478 Py_DECREF(w);
Martin v. Löwis0163d6d2001-06-09 07:34:05 +0000479 if (c < 0 && PyErr_Occurred())
Guido van Rossume797ec12001-01-17 15:24:28 +0000480 return -2;
481 return c < 0 ? 1 : c > 0 ? -1 : 0; /* negated! */
482 }
483
484 /* No comparison defined */
485 Py_DECREF(v);
486 Py_DECREF(w);
487 return 2;
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +0000488}
489
Guido van Rossume797ec12001-01-17 15:24:28 +0000490/* Final fallback 3-way comparison, returning an int. Return:
491 -2 if an error occurred;
492 -1 if v < w;
493 0 if v == w;
494 1 if v > w.
495*/
496static int
497default_3way_compare(PyObject *v, PyObject *w)
498{
499 int c;
Guido van Rossum8f9143d2001-01-22 15:59:32 +0000500 char *vname, *wname;
Guido van Rossume797ec12001-01-17 15:24:28 +0000501
502 if (v->ob_type == w->ob_type) {
Barry Warsawb0e754d2001-01-20 06:24:55 +0000503 /* When comparing these pointers, they must be cast to
504 * integer types (i.e. Py_uintptr_t, our spelling of C9X's
505 * uintptr_t). ANSI specifies that pointer compares other
506 * than == and != to non-related structures are undefined.
507 */
Barry Warsaw71ff8d52001-01-20 06:08:10 +0000508 Py_uintptr_t vv = (Py_uintptr_t)v;
509 Py_uintptr_t ww = (Py_uintptr_t)w;
Guido van Rossume797ec12001-01-17 15:24:28 +0000510 return (vv < ww) ? -1 : (vv > ww) ? 1 : 0;
511 }
512
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000513#ifdef Py_USING_UNICODE
Guido van Rossume797ec12001-01-17 15:24:28 +0000514 /* Special case for Unicode */
515 if (PyUnicode_Check(v) || PyUnicode_Check(w)) {
516 c = PyUnicode_Compare(v, w);
517 if (!PyErr_Occurred())
518 return c;
519 /* TypeErrors are ignored: if Unicode coercion fails due
520 to one of the arguments not having the right type, we
521 continue as defined by the coercion protocol (see
522 above). Luckily, decoding errors are reported as
523 ValueErrors and are not masked by this technique. */
524 if (!PyErr_ExceptionMatches(PyExc_TypeError))
525 return -2;
526 PyErr_Clear();
527 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000528#endif
Guido van Rossume797ec12001-01-17 15:24:28 +0000529
Guido van Rossum0871e932001-01-22 19:28:09 +0000530 /* None is smaller than anything */
531 if (v == Py_None)
532 return -1;
533 if (w == Py_None)
534 return 1;
535
Guido van Rossume797ec12001-01-17 15:24:28 +0000536 /* different type: compare type names */
Guido van Rossum8f9143d2001-01-22 15:59:32 +0000537 if (v->ob_type->tp_as_number)
538 vname = "";
539 else
540 vname = v->ob_type->tp_name;
541 if (w->ob_type->tp_as_number)
542 wname = "";
543 else
544 wname = w->ob_type->tp_name;
545 c = strcmp(vname, wname);
546 if (c < 0)
547 return -1;
548 if (c > 0)
549 return 1;
550 /* Same type name, or (more likely) incomparable numeric types */
551 return ((Py_uintptr_t)(v->ob_type) < (
552 Py_uintptr_t)(w->ob_type)) ? -1 : 1;
Guido van Rossume797ec12001-01-17 15:24:28 +0000553}
554
555#define CHECK_TYPES(o) PyType_HasFeature((o)->ob_type, Py_TPFLAGS_CHECKTYPES)
556
Tim Peters6d60b2e2001-05-07 20:53:51 +0000557/* Do a 3-way comparison, by hook or by crook. Return:
558 -2 for an exception;
559 -1 if v < w;
560 0 if v == w;
561 1 if v > w;
Martin v. Löwis0163d6d2001-06-09 07:34:05 +0000562 If the object implements a tp_compare function, it returns
563 whatever this function returns (whether with an exception or not).
Tim Peters6d60b2e2001-05-07 20:53:51 +0000564*/
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +0000565static int
Fred Drake100814d2000-07-09 15:48:49 +0000566do_cmp(PyObject *v, PyObject *w)
Guido van Rossum20566841995-01-12 11:26:10 +0000567{
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +0000568 int c;
Martin v. Löwis0163d6d2001-06-09 07:34:05 +0000569 cmpfunc f;
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +0000570
Martin v. Löwis0163d6d2001-06-09 07:34:05 +0000571 if (v->ob_type == w->ob_type
Guido van Rossum82fc51c12001-08-16 08:02:45 +0000572 && (f = v->ob_type->tp_compare) != NULL) {
573 c = (*f)(v, w);
574 if (c != 2 || !PyInstance_Check(v))
575 return c;
576 }
Guido van Rossume797ec12001-01-17 15:24:28 +0000577 c = try_rich_to_3way_compare(v, w);
578 if (c < 2)
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +0000579 return c;
Guido van Rossume797ec12001-01-17 15:24:28 +0000580 c = try_3way_compare(v, w);
581 if (c < 2)
582 return c;
583 return default_3way_compare(v, w);
Guido van Rossum20566841995-01-12 11:26:10 +0000584}
585
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000586/* compare_nesting is incremented before calling compare (for
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000587 some types) and decremented on exit. If the count exceeds the
588 nesting limit, enable code to detect circular data structures.
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000589
590 This is a tunable parameter that should only affect the performance
591 of comparisons, nothing else. Setting it high makes comparing deeply
592 nested non-cyclical data structures faster, but makes comparing cyclical
593 data structures slower.
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000594*/
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000595#define NESTING_LIMIT 20
596
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000597static int compare_nesting = 0;
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000598
599static PyObject*
Fred Drake100814d2000-07-09 15:48:49 +0000600get_inprogress_dict(void)
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000601{
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000602 static PyObject *key;
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000603 PyObject *tstate_dict, *inprogress;
604
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000605 if (key == NULL) {
606 key = PyString_InternFromString("cmp_state");
607 if (key == NULL)
608 return NULL;
609 }
610
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000611 tstate_dict = PyThreadState_GetDict();
612 if (tstate_dict == NULL) {
613 PyErr_BadInternalCall();
614 return NULL;
615 }
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000616
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000617 inprogress = PyDict_GetItem(tstate_dict, key);
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000618 if (inprogress == NULL) {
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000619 inprogress = PyDict_New();
620 if (inprogress == NULL)
621 return NULL;
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000622 if (PyDict_SetItem(tstate_dict, key, inprogress) == -1) {
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000623 Py_DECREF(inprogress);
624 return NULL;
625 }
Jeremy Hyltona251ea02000-06-09 16:20:39 +0000626 Py_DECREF(inprogress);
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000627 }
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000628
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000629 return inprogress;
630}
631
632static PyObject *
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000633check_recursion(PyObject *v, PyObject *w, int op)
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000634{
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000635 PyObject *inprogress;
636 PyObject *token;
Barry Warsaw9d23a4e2000-08-18 05:01:19 +0000637 Py_uintptr_t iv = (Py_uintptr_t)v;
638 Py_uintptr_t iw = (Py_uintptr_t)w;
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000639 PyObject *x, *y, *z;
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000640
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000641 inprogress = get_inprogress_dict();
642 if (inprogress == NULL)
643 return NULL;
644
645 token = PyTuple_New(3);
646 if (token == NULL)
647 return NULL;
648
649 if (iv <= iw) {
650 PyTuple_SET_ITEM(token, 0, x = PyLong_FromVoidPtr((void *)v));
651 PyTuple_SET_ITEM(token, 1, y = PyLong_FromVoidPtr((void *)w));
652 if (op >= 0)
653 op = swapped_op[op];
654 } else {
655 PyTuple_SET_ITEM(token, 0, x = PyLong_FromVoidPtr((void *)w));
656 PyTuple_SET_ITEM(token, 1, y = PyLong_FromVoidPtr((void *)v));
657 }
658 PyTuple_SET_ITEM(token, 2, z = PyInt_FromLong((long)op));
659 if (x == NULL || y == NULL || z == NULL) {
660 Py_DECREF(token);
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000661 return NULL;
662 }
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000663
664 if (PyDict_GetItem(inprogress, token) != NULL) {
665 Py_DECREF(token);
666 return Py_None; /* Without INCREF! */
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000667 }
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000668
669 if (PyDict_SetItem(inprogress, token, token) < 0) {
670 Py_DECREF(token);
671 return NULL;
672 }
673
674 return token;
675}
676
677static void
678delete_token(PyObject *token)
679{
680 PyObject *inprogress;
681
682 if (token == NULL || token == Py_None)
683 return;
684 inprogress = get_inprogress_dict();
685 if (inprogress == NULL)
686 PyErr_Clear();
687 else
688 PyDict_DelItem(inprogress, token);
689 Py_DECREF(token);
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000690}
691
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000692int
Fred Drake100814d2000-07-09 15:48:49 +0000693PyObject_Compare(PyObject *v, PyObject *w)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000694{
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000695 PyTypeObject *vtp;
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000696 int result;
697
Jack Jansend49cbe12000-08-22 21:52:51 +0000698#if defined(USE_STACKCHECK)
Guido van Rossum9c0a99e2000-08-30 15:53:50 +0000699 if (PyOS_CheckStack()) {
Jack Jansend49cbe12000-08-22 21:52:51 +0000700 PyErr_SetString(PyExc_MemoryError, "Stack overflow");
701 return -1;
702 }
703#endif
Guido van Rossumc8b6df91997-05-23 00:06:51 +0000704 if (v == NULL || w == NULL) {
705 PyErr_BadInternalCall();
706 return -1;
707 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000708 if (v == w)
709 return 0;
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +0000710 vtp = v->ob_type;
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000711 compare_nesting++;
712 if (compare_nesting > NESTING_LIMIT &&
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +0000713 (vtp->tp_as_mapping
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000714 || (vtp->tp_as_sequence
715 && !PyString_Check(v)
716 && !PyTuple_Check(v)))) {
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +0000717 /* try to detect circular data structures */
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000718 PyObject *token = check_recursion(v, w, -1);
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000719
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000720 if (token == NULL) {
721 result = -1;
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000722 }
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000723 else if (token == Py_None) {
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000724 /* already comparing these objects. assume
725 they're equal until shown otherwise */
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +0000726 result = 0;
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000727 }
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000728 else {
729 result = do_cmp(v, w);
730 delete_token(token);
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000731 }
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000732 }
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +0000733 else {
734 result = do_cmp(v, w);
735 }
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000736 compare_nesting--;
Guido van Rossume797ec12001-01-17 15:24:28 +0000737 return result < 0 ? -1 : result;
738}
739
740static PyObject *
Martin v. Löwis0163d6d2001-06-09 07:34:05 +0000741convert_3way_to_object(int op, int c)
Guido van Rossume797ec12001-01-17 15:24:28 +0000742{
Guido van Rossume797ec12001-01-17 15:24:28 +0000743 PyObject *result;
Guido van Rossume797ec12001-01-17 15:24:28 +0000744 switch (op) {
745 case Py_LT: c = c < 0; break;
746 case Py_LE: c = c <= 0; break;
747 case Py_EQ: c = c == 0; break;
748 case Py_NE: c = c != 0; break;
749 case Py_GT: c = c > 0; break;
750 case Py_GE: c = c >= 0; break;
751 }
752 result = c ? Py_True : Py_False;
753 Py_INCREF(result);
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000754 return result;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000755}
Martin v. Löwis0163d6d2001-06-09 07:34:05 +0000756
757
758static PyObject *
759try_3way_to_rich_compare(PyObject *v, PyObject *w, int op)
760{
761 int c;
762
763 c = try_3way_compare(v, w);
764 if (c >= 2)
765 c = default_3way_compare(v, w);
766 if (c <= -2)
767 return NULL;
768 return convert_3way_to_object(op, c);
769}
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000770
Neil Schemenauerd38855c2001-01-21 16:25:18 +0000771static PyObject *
Guido van Rossume797ec12001-01-17 15:24:28 +0000772do_richcmp(PyObject *v, PyObject *w, int op)
773{
774 PyObject *res;
Martin v. Löwis0163d6d2001-06-09 07:34:05 +0000775 cmpfunc f;
776
777 /* If the types are equal, don't bother with coercions etc.
778 Instances are special-cased in try_3way_compare, since
779 a result of 2 does *not* mean one value being greater
780 than the other. */
781 if (v->ob_type == w->ob_type
782 && (f = v->ob_type->tp_compare) != NULL
783 && !PyInstance_Check(v)) {
784 int c;
785 richcmpfunc f1;
786 if ((f1 = RICHCOMPARE(v->ob_type)) != NULL) {
787 /* If the type has richcmp, try it first.
788 try_rich_compare would try it two-sided,
789 which is not needed since we've a single
790 type only. */
791 res = (*f1)(v, w, op);
792 if (res != Py_NotImplemented)
793 return res;
794 Py_DECREF(res);
795 }
796 c = (*f)(v, w);
797 if (c < 0 && PyErr_Occurred())
798 return NULL;
799 return convert_3way_to_object(op, c);
800 }
Guido van Rossume797ec12001-01-17 15:24:28 +0000801
802 res = try_rich_compare(v, w, op);
803 if (res != Py_NotImplemented)
804 return res;
805 Py_DECREF(res);
806
807 return try_3way_to_rich_compare(v, w, op);
808}
809
810PyObject *
811PyObject_RichCompare(PyObject *v, PyObject *w, int op)
812{
813 PyObject *res;
814
815 assert(Py_LT <= op && op <= Py_GE);
816
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000817 compare_nesting++;
818 if (compare_nesting > NESTING_LIMIT &&
819 (v->ob_type->tp_as_mapping
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000820 || (v->ob_type->tp_as_sequence
821 && !PyString_Check(v)
822 && !PyTuple_Check(v)))) {
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000823 /* try to detect circular data structures */
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000824 PyObject *token = check_recursion(v, w, op);
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000825
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000826 if (token == NULL) {
827 res = NULL;
Guido van Rossume797ec12001-01-17 15:24:28 +0000828 }
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000829 else if (token == Py_None) {
830 /* already comparing these objects with this operator.
831 assume they're equal until shown otherwise */
832 if (op == Py_EQ)
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000833 res = Py_True;
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000834 else if (op == Py_NE)
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000835 res = Py_False;
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000836 else {
837 PyErr_SetString(PyExc_ValueError,
838 "can't order recursive values");
839 res = NULL;
840 }
841 Py_XINCREF(res);
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000842 }
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000843 else {
844 res = do_richcmp(v, w, op);
845 delete_token(token);
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000846 }
Guido van Rossume797ec12001-01-17 15:24:28 +0000847 }
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000848 else {
849 res = do_richcmp(v, w, op);
850 }
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000851 compare_nesting--;
Guido van Rossume797ec12001-01-17 15:24:28 +0000852 return res;
853}
854
Tim Petersde9725f2001-05-05 10:06:17 +0000855/* Return -1 if error; 1 if v op w; 0 if not (v op w). */
Guido van Rossume797ec12001-01-17 15:24:28 +0000856int
857PyObject_RichCompareBool(PyObject *v, PyObject *w, int op)
858{
859 PyObject *res = PyObject_RichCompare(v, w, op);
860 int ok;
861
862 if (res == NULL)
863 return -1;
864 ok = PyObject_IsTrue(res);
865 Py_DECREF(res);
866 return ok;
867}
Fred Drake13634cf2000-06-29 19:17:04 +0000868
869/* Set of hash utility functions to help maintaining the invariant that
870 iff a==b then hash(a)==hash(b)
871
872 All the utility functions (_Py_Hash*()) return "-1" to signify an error.
873*/
874
875long
Fred Drake100814d2000-07-09 15:48:49 +0000876_Py_HashDouble(double v)
Fred Drake13634cf2000-06-29 19:17:04 +0000877{
Tim Peters39dce292000-08-15 03:34:48 +0000878 double intpart, fractpart;
879 int expo;
880 long hipart;
881 long x; /* the final hash value */
882 /* This is designed so that Python numbers of different types
883 * that compare equal hash to the same value; otherwise comparisons
884 * of mapping keys will turn out weird.
885 */
886
887#ifdef MPW /* MPW C modf expects pointer to extended as second argument */
888{
889 extended e;
890 fractpart = modf(v, &e);
891 intpart = e;
892}
893#else
894 fractpart = modf(v, &intpart);
895#endif
896 if (fractpart == 0.0) {
897 /* This must return the same hash as an equal int or long. */
898 if (intpart > LONG_MAX || -intpart > LONG_MAX) {
899 /* Convert to long and use its hash. */
900 PyObject *plong; /* converted to Python long */
901 if (Py_IS_INFINITY(intpart))
902 /* can't convert to long int -- arbitrary */
903 v = v < 0 ? -271828.0 : 314159.0;
904 plong = PyLong_FromDouble(v);
905 if (plong == NULL)
906 return -1;
907 x = PyObject_Hash(plong);
908 Py_DECREF(plong);
909 return x;
910 }
911 /* Fits in a C long == a Python int, so is its own hash. */
912 x = (long)intpart;
913 if (x == -1)
914 x = -2;
915 return x;
916 }
917 /* The fractional part is non-zero, so we don't have to worry about
918 * making this match the hash of some other type.
919 * Use frexp to get at the bits in the double.
Fred Drake13634cf2000-06-29 19:17:04 +0000920 * Since the VAX D double format has 56 mantissa bits, which is the
921 * most of any double format in use, each of these parts may have as
922 * many as (but no more than) 56 significant bits.
Tim Peters39dce292000-08-15 03:34:48 +0000923 * So, assuming sizeof(long) >= 4, each part can be broken into two
924 * longs; frexp and multiplication are used to do that.
925 * Also, since the Cray double format has 15 exponent bits, which is
926 * the most of any double format in use, shifting the exponent field
927 * left by 15 won't overflow a long (again assuming sizeof(long) >= 4).
Fred Drake13634cf2000-06-29 19:17:04 +0000928 */
Tim Peters39dce292000-08-15 03:34:48 +0000929 v = frexp(v, &expo);
930 v *= 2147483648.0; /* 2**31 */
931 hipart = (long)v; /* take the top 32 bits */
932 v = (v - (double)hipart) * 2147483648.0; /* get the next 32 bits */
933 x = hipart + (long)v + (expo << 15);
934 if (x == -1)
935 x = -2;
936 return x;
Fred Drake13634cf2000-06-29 19:17:04 +0000937}
938
939long
Fred Drake100814d2000-07-09 15:48:49 +0000940_Py_HashPointer(void *p)
Fred Drake13634cf2000-06-29 19:17:04 +0000941{
942#if SIZEOF_LONG >= SIZEOF_VOID_P
943 return (long)p;
944#else
945 /* convert to a Python long and hash that */
946 PyObject* longobj;
947 long x;
948
949 if ((longobj = PyLong_FromVoidPtr(p)) == NULL) {
950 x = -1;
951 goto finally;
952 }
953 x = PyObject_Hash(longobj);
954
955finally:
956 Py_XDECREF(longobj);
957 return x;
958#endif
959}
960
961
Guido van Rossum9bfef441993-03-29 10:43:31 +0000962long
Fred Drake100814d2000-07-09 15:48:49 +0000963PyObject_Hash(PyObject *v)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000964{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000965 PyTypeObject *tp = v->ob_type;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000966 if (tp->tp_hash != NULL)
967 return (*tp->tp_hash)(v);
Guido van Rossumd1f06b92001-01-24 22:14:43 +0000968 if (tp->tp_compare == NULL && RICHCOMPARE(tp) == NULL) {
Fred Drake13634cf2000-06-29 19:17:04 +0000969 return _Py_HashPointer(v); /* Use address as hash value */
970 }
Guido van Rossum9bfef441993-03-29 10:43:31 +0000971 /* If there's a cmp but no hash defined, the object can't be hashed */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000972 PyErr_SetString(PyExc_TypeError, "unhashable type");
Guido van Rossum9bfef441993-03-29 10:43:31 +0000973 return -1;
974}
975
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000976PyObject *
Fred Drake100814d2000-07-09 15:48:49 +0000977PyObject_GetAttrString(PyObject *v, char *name)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000978{
Tim Peters6d6c1a32001-08-02 04:15:00 +0000979 PyObject *w, *res;
Guido van Rossumd8eb1b31996-08-09 20:52:03 +0000980
Tim Peters6d6c1a32001-08-02 04:15:00 +0000981 if (v->ob_type->tp_getattr != NULL)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000982 return (*v->ob_type->tp_getattr)(v, name);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000983 w = PyString_InternFromString(name);
984 if (w == NULL)
985 return NULL;
986 res = PyObject_GetAttr(v, w);
987 Py_XDECREF(w);
988 return res;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000989}
990
991int
Fred Drake100814d2000-07-09 15:48:49 +0000992PyObject_HasAttrString(PyObject *v, char *name)
Guido van Rossumed18fdc1993-07-11 19:55:34 +0000993{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000994 PyObject *res = PyObject_GetAttrString(v, name);
Guido van Rossumed18fdc1993-07-11 19:55:34 +0000995 if (res != NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000996 Py_DECREF(res);
Guido van Rossumed18fdc1993-07-11 19:55:34 +0000997 return 1;
998 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000999 PyErr_Clear();
Guido van Rossumed18fdc1993-07-11 19:55:34 +00001000 return 0;
1001}
1002
1003int
Fred Drake100814d2000-07-09 15:48:49 +00001004PyObject_SetAttrString(PyObject *v, char *name, PyObject *w)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001005{
Tim Peters6d6c1a32001-08-02 04:15:00 +00001006 PyObject *s;
1007 int res;
Guido van Rossumd8eb1b31996-08-09 20:52:03 +00001008
Tim Peters6d6c1a32001-08-02 04:15:00 +00001009 if (v->ob_type->tp_setattr != NULL)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001010 return (*v->ob_type->tp_setattr)(v, name, w);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001011 s = PyString_InternFromString(name);
1012 if (s == NULL)
1013 return -1;
1014 res = PyObject_SetAttr(v, s, w);
1015 Py_XDECREF(s);
1016 return res;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001017}
1018
Guido van Rossum98ff96a1997-05-20 18:34:44 +00001019PyObject *
Fred Drake100814d2000-07-09 15:48:49 +00001020PyObject_GetAttr(PyObject *v, PyObject *name)
Guido van Rossum98ff96a1997-05-20 18:34:44 +00001021{
Tim Peters6d6c1a32001-08-02 04:15:00 +00001022 PyTypeObject *tp = v->ob_type;
1023
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001024#ifdef Py_USING_UNICODE
Marc-André Lemburge44e5072000-09-18 16:20:57 +00001025 /* The Unicode to string conversion is done here because the
1026 existing tp_getattro slots expect a string object as name
1027 and we wouldn't want to break those. */
1028 if (PyUnicode_Check(name)) {
1029 name = _PyUnicode_AsDefaultEncodedString(name, NULL);
1030 if (name == NULL)
1031 return NULL;
1032 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001033#endif
1034
Jeremy Hylton99a8f902000-06-23 14:36:32 +00001035 if (!PyString_Check(name)) {
1036 PyErr_SetString(PyExc_TypeError,
1037 "attribute name must be string");
1038 return NULL;
1039 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001040 if (tp->tp_getattro != NULL)
1041 return (*tp->tp_getattro)(v, name);
1042 if (tp->tp_getattr != NULL)
1043 return (*tp->tp_getattr)(v, PyString_AS_STRING(name));
1044 PyErr_Format(PyExc_AttributeError,
1045 "'%.50s' object has no attribute '%.400s'",
1046 tp->tp_name, PyString_AS_STRING(name));
1047 return NULL;
Guido van Rossum98ff96a1997-05-20 18:34:44 +00001048}
1049
1050int
Fred Drake100814d2000-07-09 15:48:49 +00001051PyObject_HasAttr(PyObject *v, PyObject *name)
Guido van Rossum98ff96a1997-05-20 18:34:44 +00001052{
1053 PyObject *res = PyObject_GetAttr(v, name);
1054 if (res != NULL) {
1055 Py_DECREF(res);
1056 return 1;
1057 }
1058 PyErr_Clear();
1059 return 0;
1060}
1061
1062int
Fred Drake100814d2000-07-09 15:48:49 +00001063PyObject_SetAttr(PyObject *v, PyObject *name, PyObject *value)
Guido van Rossum98ff96a1997-05-20 18:34:44 +00001064{
Tim Peters6d6c1a32001-08-02 04:15:00 +00001065 PyTypeObject *tp = v->ob_type;
Guido van Rossum98ff96a1997-05-20 18:34:44 +00001066 int err;
Marc-André Lemburge44e5072000-09-18 16:20:57 +00001067
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001068#ifdef Py_USING_UNICODE
Marc-André Lemburge44e5072000-09-18 16:20:57 +00001069 /* The Unicode to string conversion is done here because the
1070 existing tp_setattro slots expect a string object as name
1071 and we wouldn't want to break those. */
1072 if (PyUnicode_Check(name)) {
1073 name = PyUnicode_AsEncodedString(name, NULL, NULL);
1074 if (name == NULL)
1075 return -1;
Jeremy Hylton99a8f902000-06-23 14:36:32 +00001076 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001077 else
1078#endif
1079 if (!PyString_Check(name)){
Jeremy Hylton99a8f902000-06-23 14:36:32 +00001080 PyErr_SetString(PyExc_TypeError,
1081 "attribute name must be string");
Tim Peters6d6c1a32001-08-02 04:15:00 +00001082 return -1;
Jeremy Hylton99a8f902000-06-23 14:36:32 +00001083 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001084 else
1085 Py_INCREF(name);
1086
1087 PyString_InternInPlace(&name);
1088 if (tp->tp_setattro != NULL) {
1089 err = (*tp->tp_setattro)(v, name, value);
1090 Py_DECREF(name);
1091 return err;
Marc-André Lemburge44e5072000-09-18 16:20:57 +00001092 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001093 if (tp->tp_setattr != NULL) {
1094 err = (*tp->tp_setattr)(v, PyString_AS_STRING(name), value);
1095 Py_DECREF(name);
1096 return err;
1097 }
Guido van Rossum98ff96a1997-05-20 18:34:44 +00001098 Py_DECREF(name);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001099 if (tp->tp_getattr == NULL && tp->tp_getattro == NULL)
1100 PyErr_Format(PyExc_TypeError,
1101 "'%.100s' object has no attributes "
1102 "(%s .%.100s)",
1103 tp->tp_name,
1104 value==NULL ? "del" : "assign to",
1105 PyString_AS_STRING(name));
1106 else
1107 PyErr_Format(PyExc_TypeError,
1108 "'%.100s' object has only read-only attributes "
1109 "(%s .%.100s)",
1110 tp->tp_name,
1111 value==NULL ? "del" : "assign to",
1112 PyString_AS_STRING(name));
1113 return -1;
1114}
1115
1116/* Helper to get a pointer to an object's __dict__ slot, if any */
1117
1118PyObject **
1119_PyObject_GetDictPtr(PyObject *obj)
1120{
1121#define PTRSIZE (sizeof(PyObject *))
1122
1123 long dictoffset;
1124 PyTypeObject *tp = obj->ob_type;
1125
1126 if (!(tp->tp_flags & Py_TPFLAGS_HAVE_CLASS))
1127 return NULL;
1128 dictoffset = tp->tp_dictoffset;
1129 if (dictoffset == 0)
1130 return NULL;
1131 if (dictoffset < 0) {
Neil Schemenauerfd343692001-08-29 23:54:03 +00001132 dictoffset += tp->tp_basicsize;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001133 assert(dictoffset > 0); /* Sanity check */
1134 if (tp->tp_itemsize > 0) {
1135 int n = ((PyVarObject *)obj)->ob_size;
1136 if (n > 0) {
1137 dictoffset += tp->tp_itemsize * n;
1138 /* Round up, if necessary */
1139 if (tp->tp_itemsize % PTRSIZE != 0) {
1140 dictoffset += PTRSIZE - 1;
1141 dictoffset /= PTRSIZE;
1142 dictoffset *= PTRSIZE;
1143 }
1144 }
1145 }
1146 }
1147 return (PyObject **) ((char *)obj + dictoffset);
1148}
1149
1150/* Generic GetAttr functions - put these in your tp_[gs]etattro slot */
1151
1152PyObject *
1153PyObject_GenericGetAttr(PyObject *obj, PyObject *name)
1154{
1155 PyTypeObject *tp = obj->ob_type;
1156 PyObject *descr;
1157 descrgetfunc f;
1158 PyObject **dictptr;
1159
1160 if (tp->tp_dict == NULL) {
Guido van Rossum528b7eb2001-08-07 17:24:28 +00001161 if (PyType_Ready(tp) < 0)
Tim Peters6d6c1a32001-08-02 04:15:00 +00001162 return NULL;
1163 }
1164
1165 descr = _PyType_Lookup(tp, name);
1166 f = NULL;
1167 if (descr != NULL) {
1168 f = descr->ob_type->tp_descr_get;
1169 if (f != NULL && PyDescr_IsData(descr))
1170 return f(descr, obj, (PyObject *)obj->ob_type);
1171 }
1172
1173 dictptr = _PyObject_GetDictPtr(obj);
1174 if (dictptr != NULL) {
1175 PyObject *dict = *dictptr;
1176 if (dict != NULL) {
1177 PyObject *res = PyDict_GetItem(dict, name);
1178 if (res != NULL) {
1179 Py_INCREF(res);
1180 return res;
1181 }
1182 }
1183 }
1184
1185 if (f != NULL)
1186 return f(descr, obj, (PyObject *)obj->ob_type);
1187
1188 if (descr != NULL) {
1189 Py_INCREF(descr);
1190 return descr;
1191 }
1192
1193 PyErr_Format(PyExc_AttributeError,
1194 "'%.50s' object has no attribute '%.400s'",
1195 tp->tp_name, PyString_AS_STRING(name));
1196 return NULL;
1197}
1198
1199int
1200PyObject_GenericSetAttr(PyObject *obj, PyObject *name, PyObject *value)
1201{
1202 PyTypeObject *tp = obj->ob_type;
1203 PyObject *descr;
1204 descrsetfunc f;
1205 PyObject **dictptr;
1206
1207 if (tp->tp_dict == NULL) {
Guido van Rossum528b7eb2001-08-07 17:24:28 +00001208 if (PyType_Ready(tp) < 0)
Tim Peters6d6c1a32001-08-02 04:15:00 +00001209 return -1;
1210 }
1211
1212 descr = _PyType_Lookup(tp, name);
1213 f = NULL;
1214 if (descr != NULL) {
1215 f = descr->ob_type->tp_descr_set;
1216 if (f != NULL && PyDescr_IsData(descr))
1217 return f(descr, obj, value);
1218 }
1219
1220 dictptr = _PyObject_GetDictPtr(obj);
1221 if (dictptr != NULL) {
1222 PyObject *dict = *dictptr;
1223 if (dict == NULL && value != NULL) {
1224 dict = PyDict_New();
1225 if (dict == NULL)
1226 return -1;
1227 *dictptr = dict;
1228 }
1229 if (dict != NULL) {
1230 int res;
1231 if (value == NULL)
1232 res = PyDict_DelItem(dict, name);
1233 else
1234 res = PyDict_SetItem(dict, name, value);
1235 if (res < 0 && PyErr_ExceptionMatches(PyExc_KeyError))
1236 PyErr_SetObject(PyExc_AttributeError, name);
1237 return res;
1238 }
1239 }
1240
1241 if (f != NULL)
1242 return f(descr, obj, value);
1243
1244 if (descr == NULL) {
1245 PyErr_Format(PyExc_AttributeError,
1246 "'%.50s' object has no attribute '%.400s'",
1247 tp->tp_name, PyString_AS_STRING(name));
1248 return -1;
1249 }
1250
1251 PyErr_Format(PyExc_AttributeError,
1252 "'%.50s' object attribute '%.400s' is read-only",
1253 tp->tp_name, PyString_AS_STRING(name));
1254 return -1;
Guido van Rossum98ff96a1997-05-20 18:34:44 +00001255}
1256
Guido van Rossum6ac258d1993-05-12 08:24:20 +00001257/* Test a value used as condition, e.g., in a for or if statement.
1258 Return -1 if an error occurred */
1259
1260int
Fred Drake100814d2000-07-09 15:48:49 +00001261PyObject_IsTrue(PyObject *v)
Guido van Rossum6ac258d1993-05-12 08:24:20 +00001262{
1263 int res;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001264 if (v == Py_None)
Guido van Rossum6ac258d1993-05-12 08:24:20 +00001265 res = 0;
Guido van Rossum1c4f4581998-05-22 00:53:24 +00001266 else if (v->ob_type->tp_as_number != NULL &&
1267 v->ob_type->tp_as_number->nb_nonzero != NULL)
Guido van Rossum6ac258d1993-05-12 08:24:20 +00001268 res = (*v->ob_type->tp_as_number->nb_nonzero)(v);
Guido van Rossum1c4f4581998-05-22 00:53:24 +00001269 else if (v->ob_type->tp_as_mapping != NULL &&
1270 v->ob_type->tp_as_mapping->mp_length != NULL)
Guido van Rossum6ac258d1993-05-12 08:24:20 +00001271 res = (*v->ob_type->tp_as_mapping->mp_length)(v);
Guido van Rossum1c4f4581998-05-22 00:53:24 +00001272 else if (v->ob_type->tp_as_sequence != NULL &&
1273 v->ob_type->tp_as_sequence->sq_length != NULL)
Guido van Rossum6ac258d1993-05-12 08:24:20 +00001274 res = (*v->ob_type->tp_as_sequence->sq_length)(v);
1275 else
1276 res = 1;
1277 if (res > 0)
1278 res = 1;
1279 return res;
1280}
1281
Guido van Rossumc3d3f961998-04-09 17:53:59 +00001282/* equivalent of 'not v'
1283 Return -1 if an error occurred */
1284
1285int
Fred Drake100814d2000-07-09 15:48:49 +00001286PyObject_Not(PyObject *v)
Guido van Rossumc3d3f961998-04-09 17:53:59 +00001287{
1288 int res;
1289 res = PyObject_IsTrue(v);
1290 if (res < 0)
1291 return res;
1292 return res == 0;
1293}
1294
Guido van Rossum5524a591995-01-10 15:26:20 +00001295/* Coerce two numeric types to the "larger" one.
1296 Increment the reference count on each argument.
Guido van Rossume797ec12001-01-17 15:24:28 +00001297 Return value:
1298 -1 if an error occurred;
1299 0 if the coercion succeeded (and then the reference counts are increased);
1300 1 if no coercion is possible (and no error is raised).
Guido van Rossum5524a591995-01-10 15:26:20 +00001301*/
Guido van Rossum5524a591995-01-10 15:26:20 +00001302int
Fred Drake100814d2000-07-09 15:48:49 +00001303PyNumber_CoerceEx(PyObject **pv, PyObject **pw)
Guido van Rossum5524a591995-01-10 15:26:20 +00001304{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001305 register PyObject *v = *pv;
1306 register PyObject *w = *pw;
Guido van Rossum5524a591995-01-10 15:26:20 +00001307 int res;
1308
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001309 if (v->ob_type == w->ob_type && !PyInstance_Check(v)) {
1310 Py_INCREF(v);
1311 Py_INCREF(w);
Guido van Rossum5524a591995-01-10 15:26:20 +00001312 return 0;
1313 }
1314 if (v->ob_type->tp_as_number && v->ob_type->tp_as_number->nb_coerce) {
1315 res = (*v->ob_type->tp_as_number->nb_coerce)(pv, pw);
1316 if (res <= 0)
1317 return res;
1318 }
1319 if (w->ob_type->tp_as_number && w->ob_type->tp_as_number->nb_coerce) {
1320 res = (*w->ob_type->tp_as_number->nb_coerce)(pw, pv);
1321 if (res <= 0)
1322 return res;
1323 }
Guido van Rossum242c6421997-11-19 16:03:17 +00001324 return 1;
1325}
1326
Guido van Rossume797ec12001-01-17 15:24:28 +00001327/* Coerce two numeric types to the "larger" one.
1328 Increment the reference count on each argument.
1329 Return -1 and raise an exception if no coercion is possible
1330 (and then no reference count is incremented).
1331*/
Guido van Rossum242c6421997-11-19 16:03:17 +00001332int
Fred Drake100814d2000-07-09 15:48:49 +00001333PyNumber_Coerce(PyObject **pv, PyObject **pw)
Guido van Rossum242c6421997-11-19 16:03:17 +00001334{
1335 int err = PyNumber_CoerceEx(pv, pw);
1336 if (err <= 0)
1337 return err;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001338 PyErr_SetString(PyExc_TypeError, "number coercion failed");
Guido van Rossum5524a591995-01-10 15:26:20 +00001339 return -1;
1340}
1341
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001342
Guido van Rossum49b11fe1995-01-26 00:38:22 +00001343/* Test whether an object can be called */
1344
1345int
Fred Drake100814d2000-07-09 15:48:49 +00001346PyCallable_Check(PyObject *x)
Guido van Rossum49b11fe1995-01-26 00:38:22 +00001347{
1348 if (x == NULL)
1349 return 0;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001350 if (PyInstance_Check(x)) {
1351 PyObject *call = PyObject_GetAttrString(x, "__call__");
Guido van Rossum49b11fe1995-01-26 00:38:22 +00001352 if (call == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001353 PyErr_Clear();
Guido van Rossum49b11fe1995-01-26 00:38:22 +00001354 return 0;
1355 }
1356 /* Could test recursively but don't, for fear of endless
1357 recursion if some joker sets self.__call__ = self */
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001358 Py_DECREF(call);
Guido van Rossum49b11fe1995-01-26 00:38:22 +00001359 return 1;
1360 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001361 else {
1362 return x->ob_type->tp_call != NULL;
1363 }
Guido van Rossum49b11fe1995-01-26 00:38:22 +00001364}
1365
Tim Peters7eea37e2001-09-04 22:08:56 +00001366/* Helper for PyObject_Dir.
1367 Merge the __dict__ of aclass into dict, and recursively also all
1368 the __dict__s of aclass's base classes. The order of merging isn't
1369 defined, as it's expected that only the final set of dict keys is
1370 interesting.
1371 Return 0 on success, -1 on error.
1372*/
1373
1374static int
1375merge_class_dict(PyObject* dict, PyObject* aclass)
1376{
1377 PyObject *classdict;
1378 PyObject *bases;
1379
1380 assert(PyDict_Check(dict));
1381 assert(aclass);
1382
1383 /* Merge in the type's dict (if any). */
1384 classdict = PyObject_GetAttrString(aclass, "__dict__");
1385 if (classdict == NULL)
1386 PyErr_Clear();
1387 else {
1388 int status = PyDict_Update(dict, classdict);
1389 Py_DECREF(classdict);
1390 if (status < 0)
1391 return -1;
1392 }
1393
1394 /* Recursively merge in the base types' (if any) dicts. */
1395 bases = PyObject_GetAttrString(aclass, "__bases__");
1396 if (bases != NULL) {
1397 int i, n;
1398 assert(PyTuple_Check(bases));
1399 n = PyTuple_GET_SIZE(bases);
1400 for (i = 0; i < n; i++) {
1401 PyObject *base = PyTuple_GET_ITEM(bases, i);
1402 if (merge_class_dict(dict, base) < 0) {
1403 Py_DECREF(bases);
1404 return -1;
1405 }
1406 }
1407 Py_DECREF(bases);
1408 }
1409 return 0;
1410}
1411
1412/* Like __builtin__.dir(arg). See bltinmodule.c's builtin_dir for the
1413 docstring, which should be kept in synch with this implementation. */
1414
1415PyObject *
1416PyObject_Dir(PyObject *arg)
1417{
1418 /* Set exactly one of these non-NULL before the end. */
1419 PyObject *result = NULL; /* result list */
1420 PyObject *masterdict = NULL; /* result is masterdict.keys() */
1421
1422 /* If NULL arg, return the locals. */
1423 if (arg == NULL) {
1424 PyObject *locals = PyEval_GetLocals();
1425 if (locals == NULL)
1426 goto error;
1427 result = PyDict_Keys(locals);
1428 if (result == NULL)
1429 goto error;
1430 }
1431
1432 /* Elif this is some form of module, we only want its dict. */
Guido van Rossum8dbd3d82001-09-10 18:27:43 +00001433 else if (PyModule_Check(arg)) {
Tim Peters7eea37e2001-09-04 22:08:56 +00001434 masterdict = PyObject_GetAttrString(arg, "__dict__");
1435 if (masterdict == NULL)
1436 goto error;
Guido van Rossum8dbd3d82001-09-10 18:27:43 +00001437 if (!PyDict_Check(masterdict)) {
1438 PyErr_SetString(PyExc_TypeError,
1439 "module.__dict__ is not a dictionary");
1440 goto error;
1441 }
Tim Peters7eea37e2001-09-04 22:08:56 +00001442 }
1443
1444 /* Elif some form of type or class, grab its dict and its bases.
1445 We deliberately don't suck up its __class__, as methods belonging
1446 to the metaclass would probably be more confusing than helpful. */
1447 else if (PyType_Check(arg) || PyClass_Check(arg)) {
1448 masterdict = PyDict_New();
1449 if (masterdict == NULL)
1450 goto error;
1451 if (merge_class_dict(masterdict, arg) < 0)
1452 goto error;
1453 }
1454
1455 /* Else look at its dict, and the attrs reachable from its class. */
1456 else {
1457 PyObject *itsclass;
1458 /* Create a dict to start with. CAUTION: Not everything
1459 responding to __dict__ returns a dict! */
1460 masterdict = PyObject_GetAttrString(arg, "__dict__");
1461 if (masterdict == NULL) {
1462 PyErr_Clear();
1463 masterdict = PyDict_New();
1464 }
1465 else if (!PyDict_Check(masterdict)) {
1466 Py_DECREF(masterdict);
1467 masterdict = PyDict_New();
1468 }
1469 else {
1470 /* The object may have returned a reference to its
1471 dict, so copy it to avoid mutating it. */
1472 PyObject *temp = PyDict_Copy(masterdict);
1473 Py_DECREF(masterdict);
1474 masterdict = temp;
1475 }
1476 if (masterdict == NULL)
1477 goto error;
1478
1479 /* Merge in attrs reachable from its class.
1480 CAUTION: Not all objects have a __class__ attr. */
1481 itsclass = PyObject_GetAttrString(arg, "__class__");
1482 if (itsclass == NULL)
1483 PyErr_Clear();
1484 else {
1485 int status = merge_class_dict(masterdict, itsclass);
1486 Py_DECREF(itsclass);
1487 if (status < 0)
1488 goto error;
1489 }
1490 }
1491
1492 assert((result == NULL) ^ (masterdict == NULL));
1493 if (masterdict != NULL) {
1494 /* The result comes from its keys. */
1495 assert(result == NULL);
1496 result = PyDict_Keys(masterdict);
1497 if (result == NULL)
1498 goto error;
1499 }
1500
1501 assert(result);
1502 if (PyList_Sort(result) != 0)
1503 goto error;
1504 else
1505 goto normal_return;
1506
1507 error:
1508 Py_XDECREF(result);
1509 result = NULL;
1510 /* fall through */
1511 normal_return:
1512 Py_XDECREF(masterdict);
1513 return result;
1514}
Guido van Rossum49b11fe1995-01-26 00:38:22 +00001515
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001516/*
1517NoObject is usable as a non-NULL undefined value, used by the macro None.
1518There is (and should be!) no way to create other objects of this type,
Guido van Rossum3f5da241990-12-20 15:06:42 +00001519so there is exactly one (which is indestructible, by the way).
Guido van Rossumba21a492001-08-16 08:17:26 +00001520(XXX This type and the type of NotImplemented below should be unified.)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001521*/
1522
Guido van Rossum0c182a11992-03-27 17:26:13 +00001523/* ARGSUSED */
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001524static PyObject *
Fred Drake100814d2000-07-09 15:48:49 +00001525none_repr(PyObject *op)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001526{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001527 return PyString_FromString("None");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001528}
1529
Barry Warsaw9bf16442001-01-23 16:24:35 +00001530/* ARGUSED */
1531static void
1532none_dealloc(PyObject* ignore)
1533{
1534 /* This should never get called, but we also don't want to SEGV if
1535 * we accidently decref None out of existance.
1536 */
1537 abort();
1538}
1539
1540
Guido van Rossumba21a492001-08-16 08:17:26 +00001541static PyTypeObject PyNone_Type = {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001542 PyObject_HEAD_INIT(&PyType_Type)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001543 0,
Guido van Rossumba21a492001-08-16 08:17:26 +00001544 "NoneType",
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001545 0,
1546 0,
Barry Warsaw9bf16442001-01-23 16:24:35 +00001547 (destructor)none_dealloc, /*tp_dealloc*/ /*never called*/
Guido van Rossum7066dd71992-09-17 17:54:56 +00001548 0, /*tp_print*/
Guido van Rossum3f5da241990-12-20 15:06:42 +00001549 0, /*tp_getattr*/
1550 0, /*tp_setattr*/
1551 0, /*tp_compare*/
Guido van Rossum1d5735e1994-08-30 08:27:36 +00001552 (reprfunc)none_repr, /*tp_repr*/
Guido van Rossum3f5da241990-12-20 15:06:42 +00001553 0, /*tp_as_number*/
1554 0, /*tp_as_sequence*/
1555 0, /*tp_as_mapping*/
Guido van Rossum9bfef441993-03-29 10:43:31 +00001556 0, /*tp_hash */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001557};
1558
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001559PyObject _Py_NoneStruct = {
Guido van Rossumba21a492001-08-16 08:17:26 +00001560 PyObject_HEAD_INIT(&PyNone_Type)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001561};
1562
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +00001563/* NotImplemented is an object that can be used to signal that an
1564 operation is not implemented for the given type combination. */
1565
1566static PyObject *
1567NotImplemented_repr(PyObject *op)
1568{
1569 return PyString_FromString("NotImplemented");
1570}
1571
1572static PyTypeObject PyNotImplemented_Type = {
1573 PyObject_HEAD_INIT(&PyType_Type)
1574 0,
Guido van Rossumba21a492001-08-16 08:17:26 +00001575 "NotImplementedType",
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +00001576 0,
1577 0,
Barry Warsaw9bf16442001-01-23 16:24:35 +00001578 (destructor)none_dealloc, /*tp_dealloc*/ /*never called*/
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +00001579 0, /*tp_print*/
1580 0, /*tp_getattr*/
1581 0, /*tp_setattr*/
1582 0, /*tp_compare*/
1583 (reprfunc)NotImplemented_repr, /*tp_repr*/
1584 0, /*tp_as_number*/
1585 0, /*tp_as_sequence*/
1586 0, /*tp_as_mapping*/
1587 0, /*tp_hash */
1588};
1589
1590PyObject _Py_NotImplementedStruct = {
1591 PyObject_HEAD_INIT(&PyNotImplemented_Type)
1592};
1593
Guido van Rossumba21a492001-08-16 08:17:26 +00001594void
1595_Py_ReadyTypes(void)
1596{
1597 if (PyType_Ready(&PyType_Type) < 0)
1598 Py_FatalError("Can't initialize 'type'");
1599
1600 if (PyType_Ready(&PyList_Type) < 0)
1601 Py_FatalError("Can't initialize 'list'");
1602
1603 if (PyType_Ready(&PyNone_Type) < 0)
1604 Py_FatalError("Can't initialize type(None)");
1605
1606 if (PyType_Ready(&PyNotImplemented_Type) < 0)
1607 Py_FatalError("Can't initialize type(NotImplemented)");
1608}
1609
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001610
Guido van Rossum84a90321996-05-22 16:34:47 +00001611#ifdef Py_TRACE_REFS
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001612
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001613static PyObject refchain = {&refchain, &refchain};
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001614
Guido van Rossumaacdc9d1996-08-12 21:32:12 +00001615void
Fred Drake100814d2000-07-09 15:48:49 +00001616_Py_ResetReferences(void)
Guido van Rossume09fb551997-08-05 02:04:34 +00001617{
1618 refchain._ob_prev = refchain._ob_next = &refchain;
1619 _Py_RefTotal = 0;
1620}
1621
1622void
Fred Drake100814d2000-07-09 15:48:49 +00001623_Py_NewReference(PyObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001624{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001625 _Py_RefTotal++;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001626 op->ob_refcnt = 1;
1627 op->_ob_next = refchain._ob_next;
1628 op->_ob_prev = &refchain;
1629 refchain._ob_next->_ob_prev = op;
1630 refchain._ob_next = op;
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +00001631#ifdef COUNT_ALLOCS
1632 inc_count(op->ob_type);
1633#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001634}
1635
Guido van Rossumaacdc9d1996-08-12 21:32:12 +00001636void
Fred Drake100814d2000-07-09 15:48:49 +00001637_Py_ForgetReference(register PyObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001638{
Guido van Rossumbffd6832000-01-20 22:32:56 +00001639#ifdef SLOW_UNREF_CHECK
Guido van Rossum4c08d552000-03-10 22:55:18 +00001640 register PyObject *p;
Guido van Rossumbffd6832000-01-20 22:32:56 +00001641#endif
Guido van Rossumd7047b31995-01-02 19:07:15 +00001642 if (op->ob_refcnt < 0)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001643 Py_FatalError("UNREF negative refcnt");
Guido van Rossum2e8f6141992-09-03 20:32:55 +00001644 if (op == &refchain ||
Guido van Rossumd7047b31995-01-02 19:07:15 +00001645 op->_ob_prev->_ob_next != op || op->_ob_next->_ob_prev != op)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001646 Py_FatalError("UNREF invalid object");
Guido van Rossum2e8f6141992-09-03 20:32:55 +00001647#ifdef SLOW_UNREF_CHECK
Guido van Rossum3f5da241990-12-20 15:06:42 +00001648 for (p = refchain._ob_next; p != &refchain; p = p->_ob_next) {
1649 if (p == op)
1650 break;
1651 }
Guido van Rossumd7047b31995-01-02 19:07:15 +00001652 if (p == &refchain) /* Not found */
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001653 Py_FatalError("UNREF unknown object");
Guido van Rossum2e8f6141992-09-03 20:32:55 +00001654#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001655 op->_ob_next->_ob_prev = op->_ob_prev;
1656 op->_ob_prev->_ob_next = op->_ob_next;
Guido van Rossum2e8f6141992-09-03 20:32:55 +00001657 op->_ob_next = op->_ob_prev = NULL;
Guido van Rossumd8953cb1995-04-06 14:46:26 +00001658#ifdef COUNT_ALLOCS
Tim Peters6d6c1a32001-08-02 04:15:00 +00001659 op->ob_type->tp_frees++;
Guido van Rossumd8953cb1995-04-06 14:46:26 +00001660#endif
Guido van Rossum3f5da241990-12-20 15:06:42 +00001661}
1662
Guido van Rossumaacdc9d1996-08-12 21:32:12 +00001663void
Fred Drake100814d2000-07-09 15:48:49 +00001664_Py_Dealloc(PyObject *op)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001665{
Guido van Rossum9776adf1994-09-07 14:36:45 +00001666 destructor dealloc = op->ob_type->tp_dealloc;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001667 _Py_ForgetReference(op);
Guido van Rossum9776adf1994-09-07 14:36:45 +00001668 (*dealloc)(op);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001669}
1670
Guido van Rossumaacdc9d1996-08-12 21:32:12 +00001671void
Fred Drake100814d2000-07-09 15:48:49 +00001672_Py_PrintReferences(FILE *fp)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001673{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001674 PyObject *op;
Guido van Rossume09fb551997-08-05 02:04:34 +00001675 fprintf(fp, "Remaining objects:\n");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001676 for (op = refchain._ob_next; op != &refchain; op = op->_ob_next) {
1677 fprintf(fp, "[%d] ", op->ob_refcnt);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001678 if (PyObject_Print(op, fp, 0) != 0)
1679 PyErr_Clear();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001680 putc('\n', fp);
1681 }
1682}
1683
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +00001684PyObject *
Fred Drake100814d2000-07-09 15:48:49 +00001685_Py_GetObjects(PyObject *self, PyObject *args)
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +00001686{
1687 int i, n;
1688 PyObject *t = NULL;
1689 PyObject *res, *op;
1690
1691 if (!PyArg_ParseTuple(args, "i|O", &n, &t))
1692 return NULL;
1693 op = refchain._ob_next;
1694 res = PyList_New(0);
1695 if (res == NULL)
1696 return NULL;
1697 for (i = 0; (n == 0 || i < n) && op != &refchain; i++) {
1698 while (op == self || op == args || op == res || op == t ||
Guido van Rossum3c296022001-07-14 17:58:00 +00001699 (t != NULL && op->ob_type != (PyTypeObject *) t)) {
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +00001700 op = op->_ob_next;
1701 if (op == &refchain)
1702 return res;
1703 }
1704 if (PyList_Append(res, op) < 0) {
1705 Py_DECREF(res);
1706 return NULL;
1707 }
1708 op = op->_ob_next;
1709 }
1710 return res;
1711}
1712
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001713#endif
Guido van Rossum97ead3f1996-01-12 01:24:09 +00001714
1715
1716/* Hack to force loading of cobject.o */
Guido van Rossumda9c2711996-12-05 21:58:58 +00001717PyTypeObject *_Py_cobject_hack = &PyCObject_Type;
Guido van Rossum84a90321996-05-22 16:34:47 +00001718
1719
1720/* Hack to force loading of abstract.o */
Jeremy Hylton6253f832000-07-12 12:56:19 +00001721int (*_Py_abstract_hack)(PyObject *) = &PyObject_Size;
Guido van Rossume09fb551997-08-05 02:04:34 +00001722
1723
Andrew M. Kuchling1582a3a2000-08-16 12:27:23 +00001724/* Python's malloc wrappers (see pymem.h) */
Guido van Rossume09fb551997-08-05 02:04:34 +00001725
Thomas Wouters334fb892000-07-25 12:56:38 +00001726void *
Fred Drake100814d2000-07-09 15:48:49 +00001727PyMem_Malloc(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_MALLOC(nbytes);
Guido van Rossume09fb551997-08-05 02:04:34 +00001734}
1735
Thomas Wouters334fb892000-07-25 12:56:38 +00001736void *
1737PyMem_Realloc(void *p, size_t nbytes)
Guido van Rossume09fb551997-08-05 02:04:34 +00001738{
1739#if _PyMem_EXTRA > 0
1740 if (nbytes == 0)
1741 nbytes = _PyMem_EXTRA;
1742#endif
Guido van Rossumb18618d2000-05-03 23:44:39 +00001743 return PyMem_REALLOC(p, nbytes);
Guido van Rossume09fb551997-08-05 02:04:34 +00001744}
1745
1746void
Thomas Wouters334fb892000-07-25 12:56:38 +00001747PyMem_Free(void *p)
Guido van Rossume09fb551997-08-05 02:04:34 +00001748{
Guido van Rossumb18618d2000-05-03 23:44:39 +00001749 PyMem_FREE(p);
1750}
1751
1752
1753/* Python's object malloc wrappers (see objimpl.h) */
1754
Thomas Wouters334fb892000-07-25 12:56:38 +00001755void *
Fred Drake100814d2000-07-09 15:48:49 +00001756PyObject_Malloc(size_t nbytes)
Guido van Rossumb18618d2000-05-03 23:44:39 +00001757{
1758 return PyObject_MALLOC(nbytes);
1759}
1760
Thomas Wouters334fb892000-07-25 12:56:38 +00001761void *
1762PyObject_Realloc(void *p, size_t nbytes)
Guido van Rossumb18618d2000-05-03 23:44:39 +00001763{
1764 return PyObject_REALLOC(p, nbytes);
1765}
1766
1767void
Thomas Wouters334fb892000-07-25 12:56:38 +00001768PyObject_Free(void *p)
Guido van Rossumb18618d2000-05-03 23:44:39 +00001769{
1770 PyObject_FREE(p);
Guido van Rossume09fb551997-08-05 02:04:34 +00001771}
Guido van Rossum86610361998-04-10 22:32:46 +00001772
1773
Fred Drake41deb1e2001-02-01 05:27:45 +00001774/* Hook to clear up weak references only once the _weakref module is
1775 imported. We use a dummy implementation to simplify the code at each
1776 call site instead of requiring a test for NULL.
1777*/
1778
Fred Drakeb60654b2001-02-26 18:56:37 +00001779static void
Fred Drake41deb1e2001-02-01 05:27:45 +00001780empty_clear_weak_refs(PyObject *o)
1781{
Fred Drakeb60654b2001-02-26 18:56:37 +00001782 return;
Fred Drake41deb1e2001-02-01 05:27:45 +00001783}
1784
Fred Drakeb60654b2001-02-26 18:56:37 +00001785void (*PyObject_ClearWeakRefs)(PyObject *) = empty_clear_weak_refs;
Fred Drake41deb1e2001-02-01 05:27:45 +00001786
1787
1788
Guido van Rossum86610361998-04-10 22:32:46 +00001789/* These methods are used to control infinite recursion in repr, str, print,
1790 etc. Container objects that may recursively contain themselves,
1791 e.g. builtin dictionaries and lists, should used Py_ReprEnter() and
1792 Py_ReprLeave() to avoid infinite recursion.
1793
1794 Py_ReprEnter() returns 0 the first time it is called for a particular
1795 object and 1 every time thereafter. It returns -1 if an exception
1796 occurred. Py_ReprLeave() has no return value.
1797
1798 See dictobject.c and listobject.c for examples of use.
1799*/
1800
1801#define KEY "Py_Repr"
1802
1803int
Fred Drake100814d2000-07-09 15:48:49 +00001804Py_ReprEnter(PyObject *obj)
Guido van Rossum86610361998-04-10 22:32:46 +00001805{
1806 PyObject *dict;
1807 PyObject *list;
1808 int i;
1809
1810 dict = PyThreadState_GetDict();
1811 if (dict == NULL)
1812 return -1;
1813 list = PyDict_GetItemString(dict, KEY);
1814 if (list == NULL) {
1815 list = PyList_New(0);
1816 if (list == NULL)
1817 return -1;
1818 if (PyDict_SetItemString(dict, KEY, list) < 0)
1819 return -1;
1820 Py_DECREF(list);
1821 }
1822 i = PyList_GET_SIZE(list);
1823 while (--i >= 0) {
1824 if (PyList_GET_ITEM(list, i) == obj)
1825 return 1;
1826 }
1827 PyList_Append(list, obj);
1828 return 0;
1829}
1830
1831void
Fred Drake100814d2000-07-09 15:48:49 +00001832Py_ReprLeave(PyObject *obj)
Guido van Rossum86610361998-04-10 22:32:46 +00001833{
1834 PyObject *dict;
1835 PyObject *list;
1836 int i;
1837
1838 dict = PyThreadState_GetDict();
Guido van Rossumeb909461998-04-11 15:17:34 +00001839 if (dict == NULL)
1840 return;
Guido van Rossum86610361998-04-10 22:32:46 +00001841 list = PyDict_GetItemString(dict, KEY);
Guido van Rossumeb909461998-04-11 15:17:34 +00001842 if (list == NULL || !PyList_Check(list))
1843 return;
Guido van Rossum86610361998-04-10 22:32:46 +00001844 i = PyList_GET_SIZE(list);
1845 /* Count backwards because we always expect obj to be list[-1] */
1846 while (--i >= 0) {
1847 if (PyList_GET_ITEM(list, i) == obj) {
1848 PyList_SetSlice(list, i, i + 1, NULL);
1849 break;
1850 }
1851 }
1852}
Guido van Rossumd724b232000-03-13 16:01:29 +00001853
1854/*
1855 trashcan
1856 CT 2k0130
1857 non-recursively destroy nested objects
1858
1859 CT 2k0223
1860 everything is now done in a macro.
1861
1862 CT 2k0305
1863 modified to use functions, after Tim Peter's suggestion.
1864
1865 CT 2k0309
1866 modified to restore a possible error.
Guido van Rossum13ff8eb2000-03-25 18:39:19 +00001867
1868 CT 2k0325
1869 added better safe than sorry check for threadstate
Guido van Rossume92e6102000-04-24 15:40:53 +00001870
1871 CT 2k0422
1872 complete rewrite. We now build a chain via ob_type
1873 and save the limited number of types in ob_refcnt.
1874 This is perfect since we don't need any memory.
1875 A patch for free-threading would need just a lock.
Guido van Rossumd724b232000-03-13 16:01:29 +00001876*/
1877
Guido van Rossume92e6102000-04-24 15:40:53 +00001878#define Py_TRASHCAN_TUPLE 1
1879#define Py_TRASHCAN_LIST 2
1880#define Py_TRASHCAN_DICT 3
1881#define Py_TRASHCAN_FRAME 4
1882#define Py_TRASHCAN_TRACEBACK 5
1883/* extend here if other objects want protection */
1884
Guido van Rossumd724b232000-03-13 16:01:29 +00001885int _PyTrash_delete_nesting = 0;
Guido van Rossume92e6102000-04-24 15:40:53 +00001886
Guido van Rossumd724b232000-03-13 16:01:29 +00001887PyObject * _PyTrash_delete_later = NULL;
1888
1889void
Fred Drake100814d2000-07-09 15:48:49 +00001890_PyTrash_deposit_object(PyObject *op)
Guido van Rossumd724b232000-03-13 16:01:29 +00001891{
Guido van Rossume92e6102000-04-24 15:40:53 +00001892 int typecode;
Guido van Rossum13ff8eb2000-03-25 18:39:19 +00001893
Guido van Rossume92e6102000-04-24 15:40:53 +00001894 if (PyTuple_Check(op))
1895 typecode = Py_TRASHCAN_TUPLE;
1896 else if (PyList_Check(op))
1897 typecode = Py_TRASHCAN_LIST;
1898 else if (PyDict_Check(op))
1899 typecode = Py_TRASHCAN_DICT;
1900 else if (PyFrame_Check(op))
1901 typecode = Py_TRASHCAN_FRAME;
1902 else if (PyTraceBack_Check(op))
1903 typecode = Py_TRASHCAN_TRACEBACK;
Moshe Zadkacf703f02000-08-04 15:36:13 +00001904 else /* We have a bug here -- those are the only types in GC */ {
1905 Py_FatalError("Type not supported in GC -- internal bug");
1906 return; /* pacify compiler -- execution never here */
1907 }
Guido van Rossume92e6102000-04-24 15:40:53 +00001908 op->ob_refcnt = typecode;
Guido van Rossumd724b232000-03-13 16:01:29 +00001909
Guido van Rossume92e6102000-04-24 15:40:53 +00001910 op->ob_type = (PyTypeObject*)_PyTrash_delete_later;
1911 _PyTrash_delete_later = op;
Guido van Rossumd724b232000-03-13 16:01:29 +00001912}
1913
1914void
Fred Drake100814d2000-07-09 15:48:49 +00001915_PyTrash_destroy_chain(void)
Guido van Rossumd724b232000-03-13 16:01:29 +00001916{
1917 while (_PyTrash_delete_later) {
1918 PyObject *shredder = _PyTrash_delete_later;
Guido van Rossume92e6102000-04-24 15:40:53 +00001919 _PyTrash_delete_later = (PyObject*) shredder->ob_type;
1920
1921 switch (shredder->ob_refcnt) {
1922 case Py_TRASHCAN_TUPLE:
1923 shredder->ob_type = &PyTuple_Type;
1924 break;
1925 case Py_TRASHCAN_LIST:
1926 shredder->ob_type = &PyList_Type;
1927 break;
1928 case Py_TRASHCAN_DICT:
1929 shredder->ob_type = &PyDict_Type;
1930 break;
1931 case Py_TRASHCAN_FRAME:
1932 shredder->ob_type = &PyFrame_Type;
1933 break;
1934 case Py_TRASHCAN_TRACEBACK:
1935 shredder->ob_type = &PyTraceBack_Type;
1936 break;
1937 }
1938 _Py_NewReference(shredder);
1939
Guido van Rossumd724b232000-03-13 16:01:29 +00001940 ++_PyTrash_delete_nesting;
1941 Py_DECREF(shredder);
1942 --_PyTrash_delete_nesting;
1943 }
1944}
Neil Schemenauera35c6882001-02-27 04:45:05 +00001945
1946#ifdef WITH_PYMALLOC
1947#include "obmalloc.c"
1948#endif