blob: c56c3be9175a613a59e2be27fba5862552521c3e [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 {
Guido van Rossum5f5512d2001-09-14 15:50:08 +0000197 fprintf(stderr, "object : ");
Barry Warsaweefb1072001-02-22 22:39:18 +0000198 (void)PyObject_Print(op, stderr, 0);
Guido van Rossum5f5512d2001-09-14 15:50:08 +0000199 fprintf(stderr, "\n"
200 "type : %s\n"
201 "refcount: %d\n"
202 "address : %p\n",
203 op->ob_type==NULL ? "NULL" : op->ob_type->tp_name,
204 op->ob_refcnt,
205 op);
Barry Warsaweefb1072001-02-22 22:39:18 +0000206 }
Barry Warsaw9bf16442001-01-23 16:24:35 +0000207}
Barry Warsaw903138f2001-01-23 16:33:18 +0000208
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000209PyObject *
Fred Drake100814d2000-07-09 15:48:49 +0000210PyObject_Repr(PyObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000211{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000212 if (PyErr_CheckSignals())
Guido van Rossum90933611991-06-07 16:10:43 +0000213 return NULL;
Guido van Rossum9b00dfa1998-04-28 16:06:54 +0000214#ifdef USE_STACKCHECK
215 if (PyOS_CheckStack()) {
Fred Drake661ea262000-10-24 19:57:45 +0000216 PyErr_SetString(PyExc_MemoryError, "stack overflow");
Guido van Rossum9b00dfa1998-04-28 16:06:54 +0000217 return NULL;
218 }
219#endif
Guido van Rossum90933611991-06-07 16:10:43 +0000220 if (v == NULL)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000221 return PyString_FromString("<NULL>");
Barry Warsaw7ce36942001-08-24 18:34:26 +0000222 else if (v->ob_type->tp_repr == NULL)
Guido van Rossum21922aa2001-08-30 20:26:05 +0000223 return PyString_FromFormat("<%s object at %p>",
Barry Warsaw7ce36942001-08-24 18:34:26 +0000224 v->ob_type->tp_name, v);
Guido van Rossum4c08d552000-03-10 22:55:18 +0000225 else {
226 PyObject *res;
227 res = (*v->ob_type->tp_repr)(v);
228 if (res == NULL)
229 return NULL;
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000230#ifdef Py_USING_UNICODE
Fredrik Lundhefecc7d2000-07-01 14:31:09 +0000231 if (PyUnicode_Check(res)) {
232 PyObject* str;
Fredrik Lundh2a1e0602000-07-08 17:43:32 +0000233 str = PyUnicode_AsUnicodeEscapeString(res);
Marc-André Lemburg891bc652000-07-03 09:57:53 +0000234 Py_DECREF(res);
235 if (str)
Fredrik Lundhefecc7d2000-07-01 14:31:09 +0000236 res = str;
Marc-André Lemburg891bc652000-07-03 09:57:53 +0000237 else
238 return NULL;
Fredrik Lundhefecc7d2000-07-01 14:31:09 +0000239 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000240#endif
Guido van Rossum4c08d552000-03-10 22:55:18 +0000241 if (!PyString_Check(res)) {
242 PyErr_Format(PyExc_TypeError,
Guido van Rossum5db862d2000-04-10 12:46:51 +0000243 "__repr__ returned non-string (type %.200s)",
Guido van Rossum4c08d552000-03-10 22:55:18 +0000244 res->ob_type->tp_name);
245 Py_DECREF(res);
246 return NULL;
247 }
248 return res;
249 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000250}
251
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000252PyObject *
Fred Drake100814d2000-07-09 15:48:49 +0000253PyObject_Str(PyObject *v)
Guido van Rossumc6004111993-11-05 10:22:19 +0000254{
Guido van Rossum4c08d552000-03-10 22:55:18 +0000255 PyObject *res;
256
Guido van Rossumc6004111993-11-05 10:22:19 +0000257 if (v == NULL)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000258 return PyString_FromString("<NULL>");
Tim Peters5a49ade2001-09-11 01:41:59 +0000259 if (PyString_CheckExact(v)) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000260 Py_INCREF(v);
Guido van Rossumc6004111993-11-05 10:22:19 +0000261 return v;
262 }
Tim Peters5a49ade2001-09-11 01:41:59 +0000263 if (PyString_Check(v)) {
264 /* For a string subtype that's not a string, return a true
265 string with the same string data. */
266 PyStringObject *s = (PyStringObject *)v;
267 return PyString_FromStringAndSize(s->ob_sval, s->ob_size);
268 }
Guido van Rossum4f288ab2001-05-01 16:53:37 +0000269 if (v->ob_type->tp_str == NULL)
270 return PyObject_Repr(v);
271
272 res = (*v->ob_type->tp_str)(v);
Guido van Rossum4c08d552000-03-10 22:55:18 +0000273 if (res == NULL)
274 return NULL;
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000275#ifdef Py_USING_UNICODE
Marc-André Lemburg891bc652000-07-03 09:57:53 +0000276 if (PyUnicode_Check(res)) {
277 PyObject* str;
278 str = PyUnicode_AsEncodedString(res, NULL, NULL);
279 Py_DECREF(res);
280 if (str)
281 res = str;
282 else
283 return NULL;
284 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000285#endif
Guido van Rossum4c08d552000-03-10 22:55:18 +0000286 if (!PyString_Check(res)) {
287 PyErr_Format(PyExc_TypeError,
Guido van Rossum5db862d2000-04-10 12:46:51 +0000288 "__str__ returned non-string (type %.200s)",
Guido van Rossum4c08d552000-03-10 22:55:18 +0000289 res->ob_type->tp_name);
290 Py_DECREF(res);
291 return NULL;
292 }
293 return res;
Guido van Rossumc6004111993-11-05 10:22:19 +0000294}
295
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000296#ifdef Py_USING_UNICODE
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000297PyObject *
298PyObject_Unicode(PyObject *v)
299{
300 PyObject *res;
301
302 if (v == NULL)
303 res = PyString_FromString("<NULL>");
304 else if (PyUnicode_Check(v)) {
305 Py_INCREF(v);
306 return v;
307 }
Marc-André Lemburgae605342001-03-25 19:16:13 +0000308 else if (PyString_Check(v)) {
309 Py_INCREF(v);
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000310 res = v;
Marc-André Lemburgae605342001-03-25 19:16:13 +0000311 }
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000312 else if (v->ob_type->tp_str != NULL)
313 res = (*v->ob_type->tp_str)(v);
314 else {
315 PyObject *func;
316 static PyObject *strstr;
317 if (strstr == NULL) {
318 strstr= PyString_InternFromString("__str__");
319 if (strstr == NULL)
320 return NULL;
321 }
322 if (!PyInstance_Check(v) ||
323 (func = PyObject_GetAttr(v, strstr)) == NULL) {
324 PyErr_Clear();
325 res = PyObject_Repr(v);
326 }
327 else {
328 res = PyEval_CallObject(func, (PyObject *)NULL);
329 Py_DECREF(func);
330 }
331 }
332 if (res == NULL)
333 return NULL;
334 if (!PyUnicode_Check(res)) {
335 PyObject* str;
336 str = PyUnicode_FromObject(res);
337 Py_DECREF(res);
338 if (str)
339 res = str;
340 else
341 return NULL;
342 }
343 return res;
344}
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000345#endif
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000346
347
Guido van Rossumd1f06b92001-01-24 22:14:43 +0000348/* Macro to get the tp_richcompare field of a type if defined */
349#define RICHCOMPARE(t) (PyType_HasFeature((t), Py_TPFLAGS_HAVE_RICHCOMPARE) \
350 ? (t)->tp_richcompare : NULL)
351
Guido van Rossume797ec12001-01-17 15:24:28 +0000352/* Map rich comparison operators to their swapped version, e.g. LT --> GT */
353static int swapped_op[] = {Py_GT, Py_GE, Py_EQ, Py_NE, Py_LT, Py_LE};
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +0000354
Guido van Rossume797ec12001-01-17 15:24:28 +0000355/* Try a genuine rich comparison, returning an object. Return:
356 NULL for exception;
357 NotImplemented if this particular rich comparison is not implemented or
358 undefined;
359 some object not equal to NotImplemented if it is implemented
360 (this latter object may not be a Boolean).
361*/
362static PyObject *
363try_rich_compare(PyObject *v, PyObject *w, int op)
364{
365 richcmpfunc f;
366 PyObject *res;
367
Guido van Rossumd1f06b92001-01-24 22:14:43 +0000368 if ((f = RICHCOMPARE(v->ob_type)) != NULL) {
Guido van Rossume797ec12001-01-17 15:24:28 +0000369 res = (*f)(v, w, op);
370 if (res != Py_NotImplemented)
371 return res;
372 Py_DECREF(res);
373 }
Guido van Rossumd1f06b92001-01-24 22:14:43 +0000374 if ((f = RICHCOMPARE(w->ob_type)) != NULL) {
Guido van Rossume797ec12001-01-17 15:24:28 +0000375 return (*f)(w, v, swapped_op[op]);
376 }
377 res = Py_NotImplemented;
378 Py_INCREF(res);
379 return res;
380}
381
382/* Try a genuine rich comparison, returning an int. Return:
383 -1 for exception (including the case where try_rich_compare() returns an
384 object that's not a Boolean);
385 0 if the outcome is false;
386 1 if the outcome is true;
387 2 if this particular rich comparison is not implemented or undefined.
388*/
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +0000389static int
Guido van Rossume797ec12001-01-17 15:24:28 +0000390try_rich_compare_bool(PyObject *v, PyObject *w, int op)
391{
392 PyObject *res;
393 int ok;
394
Guido van Rossumd1f06b92001-01-24 22:14:43 +0000395 if (RICHCOMPARE(v->ob_type) == NULL && RICHCOMPARE(w->ob_type) == NULL)
Guido van Rossume797ec12001-01-17 15:24:28 +0000396 return 2; /* Shortcut, avoid INCREF+DECREF */
397 res = try_rich_compare(v, w, op);
398 if (res == NULL)
399 return -1;
400 if (res == Py_NotImplemented) {
401 Py_DECREF(res);
402 return 2;
403 }
404 ok = PyObject_IsTrue(res);
405 Py_DECREF(res);
406 return ok;
407}
408
409/* Try rich comparisons to determine a 3-way comparison. Return:
410 -2 for an exception;
411 -1 if v < w;
412 0 if v == w;
413 1 if v > w;
414 2 if this particular rich comparison is not implemented or undefined.
415*/
416static int
417try_rich_to_3way_compare(PyObject *v, PyObject *w)
418{
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000419 static struct { int op; int outcome; } tries[3] = {
420 /* Try this operator, and if it is true, use this outcome: */
421 {Py_EQ, 0},
422 {Py_LT, -1},
423 {Py_GT, 1},
424 };
425 int i;
426
Guido van Rossumd1f06b92001-01-24 22:14:43 +0000427 if (RICHCOMPARE(v->ob_type) == NULL && RICHCOMPARE(w->ob_type) == NULL)
Guido van Rossume797ec12001-01-17 15:24:28 +0000428 return 2; /* Shortcut */
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000429
430 for (i = 0; i < 3; i++) {
431 switch (try_rich_compare_bool(v, w, tries[i].op)) {
432 case -1:
Tim Peters6d60b2e2001-05-07 20:53:51 +0000433 return -2;
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000434 case 1:
435 return tries[i].outcome;
436 }
Guido van Rossume797ec12001-01-17 15:24:28 +0000437 }
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000438
439 return 2;
Guido van Rossume797ec12001-01-17 15:24:28 +0000440}
441
442/* Try a 3-way comparison, returning an int. Return:
443 -2 for an exception;
444 -1 if v < w;
445 0 if v == w;
446 1 if v > w;
447 2 if this particular 3-way comparison is not implemented or undefined.
448*/
449static int
450try_3way_compare(PyObject *v, PyObject *w)
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +0000451{
452 int c;
Guido van Rossume797ec12001-01-17 15:24:28 +0000453 cmpfunc f;
454
455 /* Comparisons involving instances are given to instance_compare,
456 which has the same return conventions as this function. */
457
458 if (PyInstance_Check(v))
459 return (*v->ob_type->tp_compare)(v, w);
460 if (PyInstance_Check(w))
461 return (*w->ob_type->tp_compare)(v, w);
462
Guido van Rossume797ec12001-01-17 15:24:28 +0000463 /* Try coercion; if it fails, give up */
464 c = PyNumber_CoerceEx(&v, &w);
465 if (c < 0)
466 return -2;
467 if (c > 0)
468 return 2;
469
470 /* Try v's comparison, if defined */
471 if ((f = v->ob_type->tp_compare) != NULL) {
472 c = (*f)(v, w);
473 Py_DECREF(v);
474 Py_DECREF(w);
Martin v. Löwis0163d6d2001-06-09 07:34:05 +0000475 if (c < 0 && PyErr_Occurred())
Guido van Rossume797ec12001-01-17 15:24:28 +0000476 return -2;
477 return c < 0 ? -1 : c > 0 ? 1 : 0;
478 }
479
480 /* Try w's comparison, if defined */
481 if ((f = w->ob_type->tp_compare) != NULL) {
482 c = (*f)(w, v); /* swapped! */
483 Py_DECREF(v);
484 Py_DECREF(w);
Martin v. Löwis0163d6d2001-06-09 07:34:05 +0000485 if (c < 0 && PyErr_Occurred())
Guido van Rossume797ec12001-01-17 15:24:28 +0000486 return -2;
487 return c < 0 ? 1 : c > 0 ? -1 : 0; /* negated! */
488 }
489
490 /* No comparison defined */
491 Py_DECREF(v);
492 Py_DECREF(w);
493 return 2;
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +0000494}
495
Guido van Rossume797ec12001-01-17 15:24:28 +0000496/* Final fallback 3-way comparison, returning an int. Return:
497 -2 if an error occurred;
498 -1 if v < w;
499 0 if v == w;
500 1 if v > w.
501*/
502static int
503default_3way_compare(PyObject *v, PyObject *w)
504{
505 int c;
Guido van Rossum8f9143d2001-01-22 15:59:32 +0000506 char *vname, *wname;
Guido van Rossume797ec12001-01-17 15:24:28 +0000507
508 if (v->ob_type == w->ob_type) {
Barry Warsawb0e754d2001-01-20 06:24:55 +0000509 /* When comparing these pointers, they must be cast to
510 * integer types (i.e. Py_uintptr_t, our spelling of C9X's
511 * uintptr_t). ANSI specifies that pointer compares other
512 * than == and != to non-related structures are undefined.
513 */
Barry Warsaw71ff8d52001-01-20 06:08:10 +0000514 Py_uintptr_t vv = (Py_uintptr_t)v;
515 Py_uintptr_t ww = (Py_uintptr_t)w;
Guido van Rossume797ec12001-01-17 15:24:28 +0000516 return (vv < ww) ? -1 : (vv > ww) ? 1 : 0;
517 }
518
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000519#ifdef Py_USING_UNICODE
Guido van Rossume797ec12001-01-17 15:24:28 +0000520 /* Special case for Unicode */
521 if (PyUnicode_Check(v) || PyUnicode_Check(w)) {
522 c = PyUnicode_Compare(v, w);
523 if (!PyErr_Occurred())
524 return c;
525 /* TypeErrors are ignored: if Unicode coercion fails due
526 to one of the arguments not having the right type, we
527 continue as defined by the coercion protocol (see
528 above). Luckily, decoding errors are reported as
529 ValueErrors and are not masked by this technique. */
530 if (!PyErr_ExceptionMatches(PyExc_TypeError))
531 return -2;
532 PyErr_Clear();
533 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000534#endif
Guido van Rossume797ec12001-01-17 15:24:28 +0000535
Guido van Rossum0871e932001-01-22 19:28:09 +0000536 /* None is smaller than anything */
537 if (v == Py_None)
538 return -1;
539 if (w == Py_None)
540 return 1;
541
Guido van Rossume797ec12001-01-17 15:24:28 +0000542 /* different type: compare type names */
Guido van Rossum8f9143d2001-01-22 15:59:32 +0000543 if (v->ob_type->tp_as_number)
544 vname = "";
545 else
546 vname = v->ob_type->tp_name;
547 if (w->ob_type->tp_as_number)
548 wname = "";
549 else
550 wname = w->ob_type->tp_name;
551 c = strcmp(vname, wname);
552 if (c < 0)
553 return -1;
554 if (c > 0)
555 return 1;
556 /* Same type name, or (more likely) incomparable numeric types */
557 return ((Py_uintptr_t)(v->ob_type) < (
558 Py_uintptr_t)(w->ob_type)) ? -1 : 1;
Guido van Rossume797ec12001-01-17 15:24:28 +0000559}
560
561#define CHECK_TYPES(o) PyType_HasFeature((o)->ob_type, Py_TPFLAGS_CHECKTYPES)
562
Tim Peters6d60b2e2001-05-07 20:53:51 +0000563/* Do a 3-way comparison, by hook or by crook. Return:
564 -2 for an exception;
565 -1 if v < w;
566 0 if v == w;
567 1 if v > w;
Martin v. Löwis0163d6d2001-06-09 07:34:05 +0000568 If the object implements a tp_compare function, it returns
569 whatever this function returns (whether with an exception or not).
Tim Peters6d60b2e2001-05-07 20:53:51 +0000570*/
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +0000571static int
Fred Drake100814d2000-07-09 15:48:49 +0000572do_cmp(PyObject *v, PyObject *w)
Guido van Rossum20566841995-01-12 11:26:10 +0000573{
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +0000574 int c;
Martin v. Löwis0163d6d2001-06-09 07:34:05 +0000575 cmpfunc f;
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +0000576
Martin v. Löwis0163d6d2001-06-09 07:34:05 +0000577 if (v->ob_type == w->ob_type
Guido van Rossum82fc51c12001-08-16 08:02:45 +0000578 && (f = v->ob_type->tp_compare) != NULL) {
579 c = (*f)(v, w);
580 if (c != 2 || !PyInstance_Check(v))
581 return c;
582 }
Guido van Rossume797ec12001-01-17 15:24:28 +0000583 c = try_rich_to_3way_compare(v, w);
584 if (c < 2)
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +0000585 return c;
Guido van Rossume797ec12001-01-17 15:24:28 +0000586 c = try_3way_compare(v, w);
587 if (c < 2)
588 return c;
589 return default_3way_compare(v, w);
Guido van Rossum20566841995-01-12 11:26:10 +0000590}
591
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000592/* compare_nesting is incremented before calling compare (for
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000593 some types) and decremented on exit. If the count exceeds the
594 nesting limit, enable code to detect circular data structures.
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000595
596 This is a tunable parameter that should only affect the performance
597 of comparisons, nothing else. Setting it high makes comparing deeply
598 nested non-cyclical data structures faster, but makes comparing cyclical
599 data structures slower.
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000600*/
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000601#define NESTING_LIMIT 20
602
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000603static int compare_nesting = 0;
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000604
605static PyObject*
Fred Drake100814d2000-07-09 15:48:49 +0000606get_inprogress_dict(void)
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000607{
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000608 static PyObject *key;
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000609 PyObject *tstate_dict, *inprogress;
610
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000611 if (key == NULL) {
612 key = PyString_InternFromString("cmp_state");
613 if (key == NULL)
614 return NULL;
615 }
616
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000617 tstate_dict = PyThreadState_GetDict();
618 if (tstate_dict == NULL) {
619 PyErr_BadInternalCall();
620 return NULL;
621 }
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000622
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000623 inprogress = PyDict_GetItem(tstate_dict, key);
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000624 if (inprogress == NULL) {
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000625 inprogress = PyDict_New();
626 if (inprogress == NULL)
627 return NULL;
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000628 if (PyDict_SetItem(tstate_dict, key, inprogress) == -1) {
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000629 Py_DECREF(inprogress);
630 return NULL;
631 }
Jeremy Hyltona251ea02000-06-09 16:20:39 +0000632 Py_DECREF(inprogress);
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000633 }
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000634
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000635 return inprogress;
636}
637
638static PyObject *
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000639check_recursion(PyObject *v, PyObject *w, int op)
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000640{
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000641 PyObject *inprogress;
642 PyObject *token;
Barry Warsaw9d23a4e2000-08-18 05:01:19 +0000643 Py_uintptr_t iv = (Py_uintptr_t)v;
644 Py_uintptr_t iw = (Py_uintptr_t)w;
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000645 PyObject *x, *y, *z;
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000646
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000647 inprogress = get_inprogress_dict();
648 if (inprogress == NULL)
649 return NULL;
650
651 token = PyTuple_New(3);
652 if (token == NULL)
653 return NULL;
654
655 if (iv <= iw) {
656 PyTuple_SET_ITEM(token, 0, x = PyLong_FromVoidPtr((void *)v));
657 PyTuple_SET_ITEM(token, 1, y = PyLong_FromVoidPtr((void *)w));
658 if (op >= 0)
659 op = swapped_op[op];
660 } else {
661 PyTuple_SET_ITEM(token, 0, x = PyLong_FromVoidPtr((void *)w));
662 PyTuple_SET_ITEM(token, 1, y = PyLong_FromVoidPtr((void *)v));
663 }
664 PyTuple_SET_ITEM(token, 2, z = PyInt_FromLong((long)op));
665 if (x == NULL || y == NULL || z == NULL) {
666 Py_DECREF(token);
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000667 return NULL;
668 }
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000669
670 if (PyDict_GetItem(inprogress, token) != NULL) {
671 Py_DECREF(token);
672 return Py_None; /* Without INCREF! */
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000673 }
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000674
675 if (PyDict_SetItem(inprogress, token, token) < 0) {
676 Py_DECREF(token);
677 return NULL;
678 }
679
680 return token;
681}
682
683static void
684delete_token(PyObject *token)
685{
686 PyObject *inprogress;
687
688 if (token == NULL || token == Py_None)
689 return;
690 inprogress = get_inprogress_dict();
691 if (inprogress == NULL)
692 PyErr_Clear();
693 else
694 PyDict_DelItem(inprogress, token);
695 Py_DECREF(token);
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000696}
697
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000698int
Fred Drake100814d2000-07-09 15:48:49 +0000699PyObject_Compare(PyObject *v, PyObject *w)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000700{
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000701 PyTypeObject *vtp;
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000702 int result;
703
Jack Jansend49cbe12000-08-22 21:52:51 +0000704#if defined(USE_STACKCHECK)
Guido van Rossum9c0a99e2000-08-30 15:53:50 +0000705 if (PyOS_CheckStack()) {
Jack Jansend49cbe12000-08-22 21:52:51 +0000706 PyErr_SetString(PyExc_MemoryError, "Stack overflow");
707 return -1;
708 }
709#endif
Guido van Rossumc8b6df91997-05-23 00:06:51 +0000710 if (v == NULL || w == NULL) {
711 PyErr_BadInternalCall();
712 return -1;
713 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000714 if (v == w)
715 return 0;
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +0000716 vtp = v->ob_type;
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000717 compare_nesting++;
718 if (compare_nesting > NESTING_LIMIT &&
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +0000719 (vtp->tp_as_mapping
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000720 || (vtp->tp_as_sequence
721 && !PyString_Check(v)
722 && !PyTuple_Check(v)))) {
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +0000723 /* try to detect circular data structures */
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000724 PyObject *token = check_recursion(v, w, -1);
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000725
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000726 if (token == NULL) {
727 result = -1;
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000728 }
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000729 else if (token == Py_None) {
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000730 /* already comparing these objects. assume
731 they're equal until shown otherwise */
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +0000732 result = 0;
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000733 }
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000734 else {
735 result = do_cmp(v, w);
736 delete_token(token);
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000737 }
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000738 }
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +0000739 else {
740 result = do_cmp(v, w);
741 }
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000742 compare_nesting--;
Guido van Rossume797ec12001-01-17 15:24:28 +0000743 return result < 0 ? -1 : result;
744}
745
746static PyObject *
Martin v. Löwis0163d6d2001-06-09 07:34:05 +0000747convert_3way_to_object(int op, int c)
Guido van Rossume797ec12001-01-17 15:24:28 +0000748{
Guido van Rossume797ec12001-01-17 15:24:28 +0000749 PyObject *result;
Guido van Rossume797ec12001-01-17 15:24:28 +0000750 switch (op) {
751 case Py_LT: c = c < 0; break;
752 case Py_LE: c = c <= 0; break;
753 case Py_EQ: c = c == 0; break;
754 case Py_NE: c = c != 0; break;
755 case Py_GT: c = c > 0; break;
756 case Py_GE: c = c >= 0; break;
757 }
758 result = c ? Py_True : Py_False;
759 Py_INCREF(result);
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000760 return result;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000761}
Martin v. Löwis0163d6d2001-06-09 07:34:05 +0000762
763
764static PyObject *
765try_3way_to_rich_compare(PyObject *v, PyObject *w, int op)
766{
767 int c;
768
769 c = try_3way_compare(v, w);
770 if (c >= 2)
771 c = default_3way_compare(v, w);
772 if (c <= -2)
773 return NULL;
774 return convert_3way_to_object(op, c);
775}
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000776
Neil Schemenauerd38855c2001-01-21 16:25:18 +0000777static PyObject *
Guido van Rossume797ec12001-01-17 15:24:28 +0000778do_richcmp(PyObject *v, PyObject *w, int op)
779{
780 PyObject *res;
Martin v. Löwis0163d6d2001-06-09 07:34:05 +0000781 cmpfunc f;
782
783 /* If the types are equal, don't bother with coercions etc.
784 Instances are special-cased in try_3way_compare, since
785 a result of 2 does *not* mean one value being greater
786 than the other. */
787 if (v->ob_type == w->ob_type
788 && (f = v->ob_type->tp_compare) != NULL
789 && !PyInstance_Check(v)) {
790 int c;
791 richcmpfunc f1;
792 if ((f1 = RICHCOMPARE(v->ob_type)) != NULL) {
793 /* If the type has richcmp, try it first.
794 try_rich_compare would try it two-sided,
795 which is not needed since we've a single
796 type only. */
797 res = (*f1)(v, w, op);
798 if (res != Py_NotImplemented)
799 return res;
800 Py_DECREF(res);
801 }
802 c = (*f)(v, w);
803 if (c < 0 && PyErr_Occurred())
804 return NULL;
805 return convert_3way_to_object(op, c);
806 }
Guido van Rossume797ec12001-01-17 15:24:28 +0000807
808 res = try_rich_compare(v, w, op);
809 if (res != Py_NotImplemented)
810 return res;
811 Py_DECREF(res);
812
813 return try_3way_to_rich_compare(v, w, op);
814}
815
816PyObject *
817PyObject_RichCompare(PyObject *v, PyObject *w, int op)
818{
819 PyObject *res;
820
821 assert(Py_LT <= op && op <= Py_GE);
822
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000823 compare_nesting++;
824 if (compare_nesting > NESTING_LIMIT &&
825 (v->ob_type->tp_as_mapping
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000826 || (v->ob_type->tp_as_sequence
827 && !PyString_Check(v)
828 && !PyTuple_Check(v)))) {
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000829 /* try to detect circular data structures */
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000830 PyObject *token = check_recursion(v, w, op);
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000831
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000832 if (token == NULL) {
833 res = NULL;
Guido van Rossume797ec12001-01-17 15:24:28 +0000834 }
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000835 else if (token == Py_None) {
836 /* already comparing these objects with this operator.
837 assume they're equal until shown otherwise */
838 if (op == Py_EQ)
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000839 res = Py_True;
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000840 else if (op == Py_NE)
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000841 res = Py_False;
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000842 else {
843 PyErr_SetString(PyExc_ValueError,
844 "can't order recursive values");
845 res = NULL;
846 }
847 Py_XINCREF(res);
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000848 }
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000849 else {
850 res = do_richcmp(v, w, op);
851 delete_token(token);
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000852 }
Guido van Rossume797ec12001-01-17 15:24:28 +0000853 }
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000854 else {
855 res = do_richcmp(v, w, op);
856 }
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000857 compare_nesting--;
Guido van Rossume797ec12001-01-17 15:24:28 +0000858 return res;
859}
860
Tim Petersde9725f2001-05-05 10:06:17 +0000861/* Return -1 if error; 1 if v op w; 0 if not (v op w). */
Guido van Rossume797ec12001-01-17 15:24:28 +0000862int
863PyObject_RichCompareBool(PyObject *v, PyObject *w, int op)
864{
865 PyObject *res = PyObject_RichCompare(v, w, op);
866 int ok;
867
868 if (res == NULL)
869 return -1;
870 ok = PyObject_IsTrue(res);
871 Py_DECREF(res);
872 return ok;
873}
Fred Drake13634cf2000-06-29 19:17:04 +0000874
875/* Set of hash utility functions to help maintaining the invariant that
876 iff a==b then hash(a)==hash(b)
877
878 All the utility functions (_Py_Hash*()) return "-1" to signify an error.
879*/
880
881long
Fred Drake100814d2000-07-09 15:48:49 +0000882_Py_HashDouble(double v)
Fred Drake13634cf2000-06-29 19:17:04 +0000883{
Tim Peters39dce292000-08-15 03:34:48 +0000884 double intpart, fractpart;
885 int expo;
886 long hipart;
887 long x; /* the final hash value */
888 /* This is designed so that Python numbers of different types
889 * that compare equal hash to the same value; otherwise comparisons
890 * of mapping keys will turn out weird.
891 */
892
893#ifdef MPW /* MPW C modf expects pointer to extended as second argument */
894{
895 extended e;
896 fractpart = modf(v, &e);
897 intpart = e;
898}
899#else
900 fractpart = modf(v, &intpart);
901#endif
902 if (fractpart == 0.0) {
903 /* This must return the same hash as an equal int or long. */
904 if (intpart > LONG_MAX || -intpart > LONG_MAX) {
905 /* Convert to long and use its hash. */
906 PyObject *plong; /* converted to Python long */
907 if (Py_IS_INFINITY(intpart))
908 /* can't convert to long int -- arbitrary */
909 v = v < 0 ? -271828.0 : 314159.0;
910 plong = PyLong_FromDouble(v);
911 if (plong == NULL)
912 return -1;
913 x = PyObject_Hash(plong);
914 Py_DECREF(plong);
915 return x;
916 }
917 /* Fits in a C long == a Python int, so is its own hash. */
918 x = (long)intpart;
919 if (x == -1)
920 x = -2;
921 return x;
922 }
923 /* The fractional part is non-zero, so we don't have to worry about
924 * making this match the hash of some other type.
925 * Use frexp to get at the bits in the double.
Fred Drake13634cf2000-06-29 19:17:04 +0000926 * Since the VAX D double format has 56 mantissa bits, which is the
927 * most of any double format in use, each of these parts may have as
928 * many as (but no more than) 56 significant bits.
Tim Peters39dce292000-08-15 03:34:48 +0000929 * So, assuming sizeof(long) >= 4, each part can be broken into two
930 * longs; frexp and multiplication are used to do that.
931 * Also, since the Cray double format has 15 exponent bits, which is
932 * the most of any double format in use, shifting the exponent field
933 * left by 15 won't overflow a long (again assuming sizeof(long) >= 4).
Fred Drake13634cf2000-06-29 19:17:04 +0000934 */
Tim Peters39dce292000-08-15 03:34:48 +0000935 v = frexp(v, &expo);
936 v *= 2147483648.0; /* 2**31 */
937 hipart = (long)v; /* take the top 32 bits */
938 v = (v - (double)hipart) * 2147483648.0; /* get the next 32 bits */
939 x = hipart + (long)v + (expo << 15);
940 if (x == -1)
941 x = -2;
942 return x;
Fred Drake13634cf2000-06-29 19:17:04 +0000943}
944
945long
Fred Drake100814d2000-07-09 15:48:49 +0000946_Py_HashPointer(void *p)
Fred Drake13634cf2000-06-29 19:17:04 +0000947{
948#if SIZEOF_LONG >= SIZEOF_VOID_P
949 return (long)p;
950#else
951 /* convert to a Python long and hash that */
952 PyObject* longobj;
953 long x;
954
955 if ((longobj = PyLong_FromVoidPtr(p)) == NULL) {
956 x = -1;
957 goto finally;
958 }
959 x = PyObject_Hash(longobj);
960
961finally:
962 Py_XDECREF(longobj);
963 return x;
964#endif
965}
966
967
Guido van Rossum9bfef441993-03-29 10:43:31 +0000968long
Fred Drake100814d2000-07-09 15:48:49 +0000969PyObject_Hash(PyObject *v)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000970{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000971 PyTypeObject *tp = v->ob_type;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000972 if (tp->tp_hash != NULL)
973 return (*tp->tp_hash)(v);
Guido van Rossumd1f06b92001-01-24 22:14:43 +0000974 if (tp->tp_compare == NULL && RICHCOMPARE(tp) == NULL) {
Fred Drake13634cf2000-06-29 19:17:04 +0000975 return _Py_HashPointer(v); /* Use address as hash value */
976 }
Guido van Rossum9bfef441993-03-29 10:43:31 +0000977 /* If there's a cmp but no hash defined, the object can't be hashed */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000978 PyErr_SetString(PyExc_TypeError, "unhashable type");
Guido van Rossum9bfef441993-03-29 10:43:31 +0000979 return -1;
980}
981
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000982PyObject *
Fred Drake100814d2000-07-09 15:48:49 +0000983PyObject_GetAttrString(PyObject *v, char *name)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000984{
Tim Peters6d6c1a32001-08-02 04:15:00 +0000985 PyObject *w, *res;
Guido van Rossumd8eb1b31996-08-09 20:52:03 +0000986
Tim Peters6d6c1a32001-08-02 04:15:00 +0000987 if (v->ob_type->tp_getattr != NULL)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000988 return (*v->ob_type->tp_getattr)(v, name);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000989 w = PyString_InternFromString(name);
990 if (w == NULL)
991 return NULL;
992 res = PyObject_GetAttr(v, w);
993 Py_XDECREF(w);
994 return res;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000995}
996
997int
Fred Drake100814d2000-07-09 15:48:49 +0000998PyObject_HasAttrString(PyObject *v, char *name)
Guido van Rossumed18fdc1993-07-11 19:55:34 +0000999{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001000 PyObject *res = PyObject_GetAttrString(v, name);
Guido van Rossumed18fdc1993-07-11 19:55:34 +00001001 if (res != NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001002 Py_DECREF(res);
Guido van Rossumed18fdc1993-07-11 19:55:34 +00001003 return 1;
1004 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001005 PyErr_Clear();
Guido van Rossumed18fdc1993-07-11 19:55:34 +00001006 return 0;
1007}
1008
1009int
Fred Drake100814d2000-07-09 15:48:49 +00001010PyObject_SetAttrString(PyObject *v, char *name, PyObject *w)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001011{
Tim Peters6d6c1a32001-08-02 04:15:00 +00001012 PyObject *s;
1013 int res;
Guido van Rossumd8eb1b31996-08-09 20:52:03 +00001014
Tim Peters6d6c1a32001-08-02 04:15:00 +00001015 if (v->ob_type->tp_setattr != NULL)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001016 return (*v->ob_type->tp_setattr)(v, name, w);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001017 s = PyString_InternFromString(name);
1018 if (s == NULL)
1019 return -1;
1020 res = PyObject_SetAttr(v, s, w);
1021 Py_XDECREF(s);
1022 return res;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001023}
1024
Guido van Rossum98ff96a1997-05-20 18:34:44 +00001025PyObject *
Fred Drake100814d2000-07-09 15:48:49 +00001026PyObject_GetAttr(PyObject *v, PyObject *name)
Guido van Rossum98ff96a1997-05-20 18:34:44 +00001027{
Tim Peters6d6c1a32001-08-02 04:15:00 +00001028 PyTypeObject *tp = v->ob_type;
1029
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001030#ifdef Py_USING_UNICODE
Marc-André Lemburge44e5072000-09-18 16:20:57 +00001031 /* The Unicode to string conversion is done here because the
1032 existing tp_getattro slots expect a string object as name
1033 and we wouldn't want to break those. */
1034 if (PyUnicode_Check(name)) {
1035 name = _PyUnicode_AsDefaultEncodedString(name, NULL);
1036 if (name == NULL)
1037 return NULL;
1038 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001039#endif
1040
Jeremy Hylton99a8f902000-06-23 14:36:32 +00001041 if (!PyString_Check(name)) {
1042 PyErr_SetString(PyExc_TypeError,
1043 "attribute name must be string");
1044 return NULL;
1045 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001046 if (tp->tp_getattro != NULL)
1047 return (*tp->tp_getattro)(v, name);
1048 if (tp->tp_getattr != NULL)
1049 return (*tp->tp_getattr)(v, PyString_AS_STRING(name));
1050 PyErr_Format(PyExc_AttributeError,
1051 "'%.50s' object has no attribute '%.400s'",
1052 tp->tp_name, PyString_AS_STRING(name));
1053 return NULL;
Guido van Rossum98ff96a1997-05-20 18:34:44 +00001054}
1055
1056int
Fred Drake100814d2000-07-09 15:48:49 +00001057PyObject_HasAttr(PyObject *v, PyObject *name)
Guido van Rossum98ff96a1997-05-20 18:34:44 +00001058{
1059 PyObject *res = PyObject_GetAttr(v, name);
1060 if (res != NULL) {
1061 Py_DECREF(res);
1062 return 1;
1063 }
1064 PyErr_Clear();
1065 return 0;
1066}
1067
1068int
Fred Drake100814d2000-07-09 15:48:49 +00001069PyObject_SetAttr(PyObject *v, PyObject *name, PyObject *value)
Guido van Rossum98ff96a1997-05-20 18:34:44 +00001070{
Tim Peters6d6c1a32001-08-02 04:15:00 +00001071 PyTypeObject *tp = v->ob_type;
Guido van Rossum98ff96a1997-05-20 18:34:44 +00001072 int err;
Marc-André Lemburge44e5072000-09-18 16:20:57 +00001073
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001074#ifdef Py_USING_UNICODE
Marc-André Lemburge44e5072000-09-18 16:20:57 +00001075 /* The Unicode to string conversion is done here because the
1076 existing tp_setattro slots expect a string object as name
1077 and we wouldn't want to break those. */
1078 if (PyUnicode_Check(name)) {
1079 name = PyUnicode_AsEncodedString(name, NULL, NULL);
1080 if (name == NULL)
1081 return -1;
Jeremy Hylton99a8f902000-06-23 14:36:32 +00001082 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001083 else
1084#endif
1085 if (!PyString_Check(name)){
Jeremy Hylton99a8f902000-06-23 14:36:32 +00001086 PyErr_SetString(PyExc_TypeError,
1087 "attribute name must be string");
Tim Peters6d6c1a32001-08-02 04:15:00 +00001088 return -1;
Jeremy Hylton99a8f902000-06-23 14:36:32 +00001089 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001090 else
1091 Py_INCREF(name);
1092
1093 PyString_InternInPlace(&name);
1094 if (tp->tp_setattro != NULL) {
1095 err = (*tp->tp_setattro)(v, name, value);
1096 Py_DECREF(name);
1097 return err;
Marc-André Lemburge44e5072000-09-18 16:20:57 +00001098 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001099 if (tp->tp_setattr != NULL) {
1100 err = (*tp->tp_setattr)(v, PyString_AS_STRING(name), value);
1101 Py_DECREF(name);
1102 return err;
1103 }
Guido van Rossum98ff96a1997-05-20 18:34:44 +00001104 Py_DECREF(name);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001105 if (tp->tp_getattr == NULL && tp->tp_getattro == NULL)
1106 PyErr_Format(PyExc_TypeError,
1107 "'%.100s' object has no attributes "
1108 "(%s .%.100s)",
1109 tp->tp_name,
1110 value==NULL ? "del" : "assign to",
1111 PyString_AS_STRING(name));
1112 else
1113 PyErr_Format(PyExc_TypeError,
1114 "'%.100s' object has only read-only attributes "
1115 "(%s .%.100s)",
1116 tp->tp_name,
1117 value==NULL ? "del" : "assign to",
1118 PyString_AS_STRING(name));
1119 return -1;
1120}
1121
1122/* Helper to get a pointer to an object's __dict__ slot, if any */
1123
1124PyObject **
1125_PyObject_GetDictPtr(PyObject *obj)
1126{
1127#define PTRSIZE (sizeof(PyObject *))
1128
1129 long dictoffset;
1130 PyTypeObject *tp = obj->ob_type;
1131
1132 if (!(tp->tp_flags & Py_TPFLAGS_HAVE_CLASS))
1133 return NULL;
1134 dictoffset = tp->tp_dictoffset;
1135 if (dictoffset == 0)
1136 return NULL;
1137 if (dictoffset < 0) {
Neil Schemenauerfd343692001-08-29 23:54:03 +00001138 dictoffset += tp->tp_basicsize;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001139 assert(dictoffset > 0); /* Sanity check */
1140 if (tp->tp_itemsize > 0) {
1141 int n = ((PyVarObject *)obj)->ob_size;
1142 if (n > 0) {
1143 dictoffset += tp->tp_itemsize * n;
1144 /* Round up, if necessary */
1145 if (tp->tp_itemsize % PTRSIZE != 0) {
1146 dictoffset += PTRSIZE - 1;
1147 dictoffset /= PTRSIZE;
1148 dictoffset *= PTRSIZE;
1149 }
1150 }
1151 }
1152 }
1153 return (PyObject **) ((char *)obj + dictoffset);
1154}
1155
1156/* Generic GetAttr functions - put these in your tp_[gs]etattro slot */
1157
1158PyObject *
1159PyObject_GenericGetAttr(PyObject *obj, PyObject *name)
1160{
1161 PyTypeObject *tp = obj->ob_type;
1162 PyObject *descr;
1163 descrgetfunc f;
1164 PyObject **dictptr;
1165
1166 if (tp->tp_dict == NULL) {
Guido van Rossum528b7eb2001-08-07 17:24:28 +00001167 if (PyType_Ready(tp) < 0)
Tim Peters6d6c1a32001-08-02 04:15:00 +00001168 return NULL;
1169 }
1170
1171 descr = _PyType_Lookup(tp, name);
1172 f = NULL;
1173 if (descr != NULL) {
1174 f = descr->ob_type->tp_descr_get;
1175 if (f != NULL && PyDescr_IsData(descr))
1176 return f(descr, obj, (PyObject *)obj->ob_type);
1177 }
1178
1179 dictptr = _PyObject_GetDictPtr(obj);
1180 if (dictptr != NULL) {
1181 PyObject *dict = *dictptr;
1182 if (dict != NULL) {
1183 PyObject *res = PyDict_GetItem(dict, name);
1184 if (res != NULL) {
1185 Py_INCREF(res);
1186 return res;
1187 }
1188 }
1189 }
1190
1191 if (f != NULL)
1192 return f(descr, obj, (PyObject *)obj->ob_type);
1193
1194 if (descr != NULL) {
1195 Py_INCREF(descr);
1196 return descr;
1197 }
1198
1199 PyErr_Format(PyExc_AttributeError,
1200 "'%.50s' object has no attribute '%.400s'",
1201 tp->tp_name, PyString_AS_STRING(name));
1202 return NULL;
1203}
1204
1205int
1206PyObject_GenericSetAttr(PyObject *obj, PyObject *name, PyObject *value)
1207{
1208 PyTypeObject *tp = obj->ob_type;
1209 PyObject *descr;
1210 descrsetfunc f;
1211 PyObject **dictptr;
1212
1213 if (tp->tp_dict == NULL) {
Guido van Rossum528b7eb2001-08-07 17:24:28 +00001214 if (PyType_Ready(tp) < 0)
Tim Peters6d6c1a32001-08-02 04:15:00 +00001215 return -1;
1216 }
1217
1218 descr = _PyType_Lookup(tp, name);
1219 f = NULL;
1220 if (descr != NULL) {
1221 f = descr->ob_type->tp_descr_set;
1222 if (f != NULL && PyDescr_IsData(descr))
1223 return f(descr, obj, value);
1224 }
1225
1226 dictptr = _PyObject_GetDictPtr(obj);
1227 if (dictptr != NULL) {
1228 PyObject *dict = *dictptr;
1229 if (dict == NULL && value != NULL) {
1230 dict = PyDict_New();
1231 if (dict == NULL)
1232 return -1;
1233 *dictptr = dict;
1234 }
1235 if (dict != NULL) {
1236 int res;
1237 if (value == NULL)
1238 res = PyDict_DelItem(dict, name);
1239 else
1240 res = PyDict_SetItem(dict, name, value);
1241 if (res < 0 && PyErr_ExceptionMatches(PyExc_KeyError))
1242 PyErr_SetObject(PyExc_AttributeError, name);
1243 return res;
1244 }
1245 }
1246
1247 if (f != NULL)
1248 return f(descr, obj, value);
1249
1250 if (descr == NULL) {
1251 PyErr_Format(PyExc_AttributeError,
1252 "'%.50s' object has no attribute '%.400s'",
1253 tp->tp_name, PyString_AS_STRING(name));
1254 return -1;
1255 }
1256
1257 PyErr_Format(PyExc_AttributeError,
1258 "'%.50s' object attribute '%.400s' is read-only",
1259 tp->tp_name, PyString_AS_STRING(name));
1260 return -1;
Guido van Rossum98ff96a1997-05-20 18:34:44 +00001261}
1262
Guido van Rossum6ac258d1993-05-12 08:24:20 +00001263/* Test a value used as condition, e.g., in a for or if statement.
1264 Return -1 if an error occurred */
1265
1266int
Fred Drake100814d2000-07-09 15:48:49 +00001267PyObject_IsTrue(PyObject *v)
Guido van Rossum6ac258d1993-05-12 08:24:20 +00001268{
1269 int res;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001270 if (v == Py_None)
Guido van Rossum6ac258d1993-05-12 08:24:20 +00001271 res = 0;
Guido van Rossum1c4f4581998-05-22 00:53:24 +00001272 else if (v->ob_type->tp_as_number != NULL &&
1273 v->ob_type->tp_as_number->nb_nonzero != NULL)
Guido van Rossum6ac258d1993-05-12 08:24:20 +00001274 res = (*v->ob_type->tp_as_number->nb_nonzero)(v);
Guido van Rossum1c4f4581998-05-22 00:53:24 +00001275 else if (v->ob_type->tp_as_mapping != NULL &&
1276 v->ob_type->tp_as_mapping->mp_length != NULL)
Guido van Rossum6ac258d1993-05-12 08:24:20 +00001277 res = (*v->ob_type->tp_as_mapping->mp_length)(v);
Guido van Rossum1c4f4581998-05-22 00:53:24 +00001278 else if (v->ob_type->tp_as_sequence != NULL &&
1279 v->ob_type->tp_as_sequence->sq_length != NULL)
Guido van Rossum6ac258d1993-05-12 08:24:20 +00001280 res = (*v->ob_type->tp_as_sequence->sq_length)(v);
1281 else
1282 res = 1;
1283 if (res > 0)
1284 res = 1;
1285 return res;
1286}
1287
Guido van Rossumc3d3f961998-04-09 17:53:59 +00001288/* equivalent of 'not v'
1289 Return -1 if an error occurred */
1290
1291int
Fred Drake100814d2000-07-09 15:48:49 +00001292PyObject_Not(PyObject *v)
Guido van Rossumc3d3f961998-04-09 17:53:59 +00001293{
1294 int res;
1295 res = PyObject_IsTrue(v);
1296 if (res < 0)
1297 return res;
1298 return res == 0;
1299}
1300
Guido van Rossum5524a591995-01-10 15:26:20 +00001301/* Coerce two numeric types to the "larger" one.
1302 Increment the reference count on each argument.
Guido van Rossume797ec12001-01-17 15:24:28 +00001303 Return value:
1304 -1 if an error occurred;
1305 0 if the coercion succeeded (and then the reference counts are increased);
1306 1 if no coercion is possible (and no error is raised).
Guido van Rossum5524a591995-01-10 15:26:20 +00001307*/
Guido van Rossum5524a591995-01-10 15:26:20 +00001308int
Fred Drake100814d2000-07-09 15:48:49 +00001309PyNumber_CoerceEx(PyObject **pv, PyObject **pw)
Guido van Rossum5524a591995-01-10 15:26:20 +00001310{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001311 register PyObject *v = *pv;
1312 register PyObject *w = *pw;
Guido van Rossum5524a591995-01-10 15:26:20 +00001313 int res;
1314
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001315 if (v->ob_type == w->ob_type && !PyInstance_Check(v)) {
1316 Py_INCREF(v);
1317 Py_INCREF(w);
Guido van Rossum5524a591995-01-10 15:26:20 +00001318 return 0;
1319 }
1320 if (v->ob_type->tp_as_number && v->ob_type->tp_as_number->nb_coerce) {
1321 res = (*v->ob_type->tp_as_number->nb_coerce)(pv, pw);
1322 if (res <= 0)
1323 return res;
1324 }
1325 if (w->ob_type->tp_as_number && w->ob_type->tp_as_number->nb_coerce) {
1326 res = (*w->ob_type->tp_as_number->nb_coerce)(pw, pv);
1327 if (res <= 0)
1328 return res;
1329 }
Guido van Rossum242c6421997-11-19 16:03:17 +00001330 return 1;
1331}
1332
Guido van Rossume797ec12001-01-17 15:24:28 +00001333/* Coerce two numeric types to the "larger" one.
1334 Increment the reference count on each argument.
1335 Return -1 and raise an exception if no coercion is possible
1336 (and then no reference count is incremented).
1337*/
Guido van Rossum242c6421997-11-19 16:03:17 +00001338int
Fred Drake100814d2000-07-09 15:48:49 +00001339PyNumber_Coerce(PyObject **pv, PyObject **pw)
Guido van Rossum242c6421997-11-19 16:03:17 +00001340{
1341 int err = PyNumber_CoerceEx(pv, pw);
1342 if (err <= 0)
1343 return err;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001344 PyErr_SetString(PyExc_TypeError, "number coercion failed");
Guido van Rossum5524a591995-01-10 15:26:20 +00001345 return -1;
1346}
1347
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001348
Guido van Rossum49b11fe1995-01-26 00:38:22 +00001349/* Test whether an object can be called */
1350
1351int
Fred Drake100814d2000-07-09 15:48:49 +00001352PyCallable_Check(PyObject *x)
Guido van Rossum49b11fe1995-01-26 00:38:22 +00001353{
1354 if (x == NULL)
1355 return 0;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001356 if (PyInstance_Check(x)) {
1357 PyObject *call = PyObject_GetAttrString(x, "__call__");
Guido van Rossum49b11fe1995-01-26 00:38:22 +00001358 if (call == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001359 PyErr_Clear();
Guido van Rossum49b11fe1995-01-26 00:38:22 +00001360 return 0;
1361 }
1362 /* Could test recursively but don't, for fear of endless
1363 recursion if some joker sets self.__call__ = self */
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001364 Py_DECREF(call);
Guido van Rossum49b11fe1995-01-26 00:38:22 +00001365 return 1;
1366 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001367 else {
1368 return x->ob_type->tp_call != NULL;
1369 }
Guido van Rossum49b11fe1995-01-26 00:38:22 +00001370}
1371
Tim Peters7eea37e2001-09-04 22:08:56 +00001372/* Helper for PyObject_Dir.
1373 Merge the __dict__ of aclass into dict, and recursively also all
1374 the __dict__s of aclass's base classes. The order of merging isn't
1375 defined, as it's expected that only the final set of dict keys is
1376 interesting.
1377 Return 0 on success, -1 on error.
1378*/
1379
1380static int
1381merge_class_dict(PyObject* dict, PyObject* aclass)
1382{
1383 PyObject *classdict;
1384 PyObject *bases;
1385
1386 assert(PyDict_Check(dict));
1387 assert(aclass);
1388
1389 /* Merge in the type's dict (if any). */
1390 classdict = PyObject_GetAttrString(aclass, "__dict__");
1391 if (classdict == NULL)
1392 PyErr_Clear();
1393 else {
1394 int status = PyDict_Update(dict, classdict);
1395 Py_DECREF(classdict);
1396 if (status < 0)
1397 return -1;
1398 }
1399
1400 /* Recursively merge in the base types' (if any) dicts. */
1401 bases = PyObject_GetAttrString(aclass, "__bases__");
Tim Petersbc7e8632001-09-16 20:33:22 +00001402 if (bases == NULL)
1403 PyErr_Clear();
1404 else {
Tim Peters7eea37e2001-09-04 22:08:56 +00001405 int i, n;
1406 assert(PyTuple_Check(bases));
1407 n = PyTuple_GET_SIZE(bases);
1408 for (i = 0; i < n; i++) {
1409 PyObject *base = PyTuple_GET_ITEM(bases, i);
1410 if (merge_class_dict(dict, base) < 0) {
1411 Py_DECREF(bases);
1412 return -1;
1413 }
1414 }
1415 Py_DECREF(bases);
1416 }
1417 return 0;
1418}
1419
Tim Peters305b5852001-09-17 02:38:46 +00001420/* Helper for PyObject_Dir.
1421 If obj has an attr named attrname that's a list, merge its string
1422 elements into keys of dict.
1423 Return 0 on success, -1 on error. Errors due to not finding the attr,
1424 or the attr not being a list, are suppressed.
1425*/
1426
1427static int
1428merge_list_attr(PyObject* dict, PyObject* obj, char *attrname)
1429{
1430 PyObject *list;
1431 int result = 0;
1432
1433 assert(PyDict_Check(dict));
1434 assert(obj);
1435 assert(attrname);
1436
1437 list = PyObject_GetAttrString(obj, attrname);
1438 if (list == NULL)
1439 PyErr_Clear();
1440
1441 else if (PyList_Check(list)) {
1442 int i;
1443 for (i = 0; i < PyList_GET_SIZE(list); ++i) {
1444 PyObject *item = PyList_GET_ITEM(list, i);
1445 if (PyString_Check(item)) {
1446 result = PyDict_SetItem(dict, item, Py_None);
1447 if (result < 0)
1448 break;
1449 }
1450 }
1451 }
1452
1453 Py_XDECREF(list);
1454 return result;
1455}
1456
Tim Peters7eea37e2001-09-04 22:08:56 +00001457/* Like __builtin__.dir(arg). See bltinmodule.c's builtin_dir for the
1458 docstring, which should be kept in synch with this implementation. */
1459
1460PyObject *
1461PyObject_Dir(PyObject *arg)
1462{
1463 /* Set exactly one of these non-NULL before the end. */
1464 PyObject *result = NULL; /* result list */
1465 PyObject *masterdict = NULL; /* result is masterdict.keys() */
1466
1467 /* If NULL arg, return the locals. */
1468 if (arg == NULL) {
1469 PyObject *locals = PyEval_GetLocals();
1470 if (locals == NULL)
1471 goto error;
1472 result = PyDict_Keys(locals);
1473 if (result == NULL)
1474 goto error;
1475 }
1476
1477 /* Elif this is some form of module, we only want its dict. */
Guido van Rossum8dbd3d82001-09-10 18:27:43 +00001478 else if (PyModule_Check(arg)) {
Tim Peters7eea37e2001-09-04 22:08:56 +00001479 masterdict = PyObject_GetAttrString(arg, "__dict__");
1480 if (masterdict == NULL)
1481 goto error;
Guido van Rossum8dbd3d82001-09-10 18:27:43 +00001482 if (!PyDict_Check(masterdict)) {
1483 PyErr_SetString(PyExc_TypeError,
1484 "module.__dict__ is not a dictionary");
1485 goto error;
1486 }
Tim Peters7eea37e2001-09-04 22:08:56 +00001487 }
1488
1489 /* Elif some form of type or class, grab its dict and its bases.
1490 We deliberately don't suck up its __class__, as methods belonging
1491 to the metaclass would probably be more confusing than helpful. */
1492 else if (PyType_Check(arg) || PyClass_Check(arg)) {
1493 masterdict = PyDict_New();
1494 if (masterdict == NULL)
1495 goto error;
1496 if (merge_class_dict(masterdict, arg) < 0)
1497 goto error;
1498 }
1499
1500 /* Else look at its dict, and the attrs reachable from its class. */
1501 else {
1502 PyObject *itsclass;
1503 /* Create a dict to start with. CAUTION: Not everything
1504 responding to __dict__ returns a dict! */
1505 masterdict = PyObject_GetAttrString(arg, "__dict__");
1506 if (masterdict == NULL) {
1507 PyErr_Clear();
1508 masterdict = PyDict_New();
1509 }
1510 else if (!PyDict_Check(masterdict)) {
1511 Py_DECREF(masterdict);
1512 masterdict = PyDict_New();
1513 }
1514 else {
1515 /* The object may have returned a reference to its
1516 dict, so copy it to avoid mutating it. */
1517 PyObject *temp = PyDict_Copy(masterdict);
1518 Py_DECREF(masterdict);
1519 masterdict = temp;
1520 }
1521 if (masterdict == NULL)
1522 goto error;
1523
Tim Peters305b5852001-09-17 02:38:46 +00001524 /* Merge in __members__ and __methods__ (if any).
1525 XXX Would like this to go away someday; for now, it's
1526 XXX needed to get at im_self etc of method objects. */
1527 if (merge_list_attr(masterdict, arg, "__members__") < 0)
1528 goto error;
1529 if (merge_list_attr(masterdict, arg, "__methods__") < 0)
1530 goto error;
1531
Tim Peters7eea37e2001-09-04 22:08:56 +00001532 /* Merge in attrs reachable from its class.
1533 CAUTION: Not all objects have a __class__ attr. */
1534 itsclass = PyObject_GetAttrString(arg, "__class__");
1535 if (itsclass == NULL)
1536 PyErr_Clear();
1537 else {
1538 int status = merge_class_dict(masterdict, itsclass);
1539 Py_DECREF(itsclass);
1540 if (status < 0)
1541 goto error;
1542 }
1543 }
1544
1545 assert((result == NULL) ^ (masterdict == NULL));
1546 if (masterdict != NULL) {
1547 /* The result comes from its keys. */
1548 assert(result == NULL);
1549 result = PyDict_Keys(masterdict);
1550 if (result == NULL)
1551 goto error;
1552 }
1553
1554 assert(result);
1555 if (PyList_Sort(result) != 0)
1556 goto error;
1557 else
1558 goto normal_return;
1559
1560 error:
1561 Py_XDECREF(result);
1562 result = NULL;
1563 /* fall through */
1564 normal_return:
1565 Py_XDECREF(masterdict);
1566 return result;
1567}
Guido van Rossum49b11fe1995-01-26 00:38:22 +00001568
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001569/*
1570NoObject is usable as a non-NULL undefined value, used by the macro None.
1571There is (and should be!) no way to create other objects of this type,
Guido van Rossum3f5da241990-12-20 15:06:42 +00001572so there is exactly one (which is indestructible, by the way).
Guido van Rossumba21a492001-08-16 08:17:26 +00001573(XXX This type and the type of NotImplemented below should be unified.)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001574*/
1575
Guido van Rossum0c182a11992-03-27 17:26:13 +00001576/* ARGSUSED */
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001577static PyObject *
Fred Drake100814d2000-07-09 15:48:49 +00001578none_repr(PyObject *op)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001579{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001580 return PyString_FromString("None");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001581}
1582
Barry Warsaw9bf16442001-01-23 16:24:35 +00001583/* ARGUSED */
1584static void
1585none_dealloc(PyObject* ignore)
1586{
1587 /* This should never get called, but we also don't want to SEGV if
1588 * we accidently decref None out of existance.
1589 */
1590 abort();
1591}
1592
1593
Guido van Rossumba21a492001-08-16 08:17:26 +00001594static PyTypeObject PyNone_Type = {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001595 PyObject_HEAD_INIT(&PyType_Type)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001596 0,
Guido van Rossumba21a492001-08-16 08:17:26 +00001597 "NoneType",
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001598 0,
1599 0,
Barry Warsaw9bf16442001-01-23 16:24:35 +00001600 (destructor)none_dealloc, /*tp_dealloc*/ /*never called*/
Guido van Rossum7066dd71992-09-17 17:54:56 +00001601 0, /*tp_print*/
Guido van Rossum3f5da241990-12-20 15:06:42 +00001602 0, /*tp_getattr*/
1603 0, /*tp_setattr*/
1604 0, /*tp_compare*/
Guido van Rossum1d5735e1994-08-30 08:27:36 +00001605 (reprfunc)none_repr, /*tp_repr*/
Guido van Rossum3f5da241990-12-20 15:06:42 +00001606 0, /*tp_as_number*/
1607 0, /*tp_as_sequence*/
1608 0, /*tp_as_mapping*/
Guido van Rossum9bfef441993-03-29 10:43:31 +00001609 0, /*tp_hash */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001610};
1611
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001612PyObject _Py_NoneStruct = {
Guido van Rossumba21a492001-08-16 08:17:26 +00001613 PyObject_HEAD_INIT(&PyNone_Type)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001614};
1615
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +00001616/* NotImplemented is an object that can be used to signal that an
1617 operation is not implemented for the given type combination. */
1618
1619static PyObject *
1620NotImplemented_repr(PyObject *op)
1621{
1622 return PyString_FromString("NotImplemented");
1623}
1624
1625static PyTypeObject PyNotImplemented_Type = {
1626 PyObject_HEAD_INIT(&PyType_Type)
1627 0,
Guido van Rossumba21a492001-08-16 08:17:26 +00001628 "NotImplementedType",
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +00001629 0,
1630 0,
Barry Warsaw9bf16442001-01-23 16:24:35 +00001631 (destructor)none_dealloc, /*tp_dealloc*/ /*never called*/
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +00001632 0, /*tp_print*/
1633 0, /*tp_getattr*/
1634 0, /*tp_setattr*/
1635 0, /*tp_compare*/
1636 (reprfunc)NotImplemented_repr, /*tp_repr*/
1637 0, /*tp_as_number*/
1638 0, /*tp_as_sequence*/
1639 0, /*tp_as_mapping*/
1640 0, /*tp_hash */
1641};
1642
1643PyObject _Py_NotImplementedStruct = {
1644 PyObject_HEAD_INIT(&PyNotImplemented_Type)
1645};
1646
Guido van Rossumba21a492001-08-16 08:17:26 +00001647void
1648_Py_ReadyTypes(void)
1649{
1650 if (PyType_Ready(&PyType_Type) < 0)
1651 Py_FatalError("Can't initialize 'type'");
1652
1653 if (PyType_Ready(&PyList_Type) < 0)
1654 Py_FatalError("Can't initialize 'list'");
1655
1656 if (PyType_Ready(&PyNone_Type) < 0)
1657 Py_FatalError("Can't initialize type(None)");
1658
1659 if (PyType_Ready(&PyNotImplemented_Type) < 0)
1660 Py_FatalError("Can't initialize type(NotImplemented)");
1661}
1662
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001663
Guido van Rossum84a90321996-05-22 16:34:47 +00001664#ifdef Py_TRACE_REFS
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001665
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001666static PyObject refchain = {&refchain, &refchain};
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001667
Guido van Rossumaacdc9d1996-08-12 21:32:12 +00001668void
Fred Drake100814d2000-07-09 15:48:49 +00001669_Py_ResetReferences(void)
Guido van Rossume09fb551997-08-05 02:04:34 +00001670{
1671 refchain._ob_prev = refchain._ob_next = &refchain;
1672 _Py_RefTotal = 0;
1673}
1674
1675void
Fred Drake100814d2000-07-09 15:48:49 +00001676_Py_NewReference(PyObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001677{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001678 _Py_RefTotal++;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001679 op->ob_refcnt = 1;
1680 op->_ob_next = refchain._ob_next;
1681 op->_ob_prev = &refchain;
1682 refchain._ob_next->_ob_prev = op;
1683 refchain._ob_next = op;
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +00001684#ifdef COUNT_ALLOCS
1685 inc_count(op->ob_type);
1686#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001687}
1688
Guido van Rossumaacdc9d1996-08-12 21:32:12 +00001689void
Fred Drake100814d2000-07-09 15:48:49 +00001690_Py_ForgetReference(register PyObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001691{
Guido van Rossumbffd6832000-01-20 22:32:56 +00001692#ifdef SLOW_UNREF_CHECK
Guido van Rossum4c08d552000-03-10 22:55:18 +00001693 register PyObject *p;
Guido van Rossumbffd6832000-01-20 22:32:56 +00001694#endif
Guido van Rossumd7047b31995-01-02 19:07:15 +00001695 if (op->ob_refcnt < 0)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001696 Py_FatalError("UNREF negative refcnt");
Guido van Rossum2e8f6141992-09-03 20:32:55 +00001697 if (op == &refchain ||
Guido van Rossumd7047b31995-01-02 19:07:15 +00001698 op->_ob_prev->_ob_next != op || op->_ob_next->_ob_prev != op)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001699 Py_FatalError("UNREF invalid object");
Guido van Rossum2e8f6141992-09-03 20:32:55 +00001700#ifdef SLOW_UNREF_CHECK
Guido van Rossum3f5da241990-12-20 15:06:42 +00001701 for (p = refchain._ob_next; p != &refchain; p = p->_ob_next) {
1702 if (p == op)
1703 break;
1704 }
Guido van Rossumd7047b31995-01-02 19:07:15 +00001705 if (p == &refchain) /* Not found */
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001706 Py_FatalError("UNREF unknown object");
Guido van Rossum2e8f6141992-09-03 20:32:55 +00001707#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001708 op->_ob_next->_ob_prev = op->_ob_prev;
1709 op->_ob_prev->_ob_next = op->_ob_next;
Guido van Rossum2e8f6141992-09-03 20:32:55 +00001710 op->_ob_next = op->_ob_prev = NULL;
Guido van Rossumd8953cb1995-04-06 14:46:26 +00001711#ifdef COUNT_ALLOCS
Tim Peters6d6c1a32001-08-02 04:15:00 +00001712 op->ob_type->tp_frees++;
Guido van Rossumd8953cb1995-04-06 14:46:26 +00001713#endif
Guido van Rossum3f5da241990-12-20 15:06:42 +00001714}
1715
Guido van Rossumaacdc9d1996-08-12 21:32:12 +00001716void
Fred Drake100814d2000-07-09 15:48:49 +00001717_Py_Dealloc(PyObject *op)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001718{
Guido van Rossum9776adf1994-09-07 14:36:45 +00001719 destructor dealloc = op->ob_type->tp_dealloc;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001720 _Py_ForgetReference(op);
Guido van Rossum9776adf1994-09-07 14:36:45 +00001721 (*dealloc)(op);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001722}
1723
Guido van Rossumaacdc9d1996-08-12 21:32:12 +00001724void
Fred Drake100814d2000-07-09 15:48:49 +00001725_Py_PrintReferences(FILE *fp)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001726{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001727 PyObject *op;
Guido van Rossume09fb551997-08-05 02:04:34 +00001728 fprintf(fp, "Remaining objects:\n");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001729 for (op = refchain._ob_next; op != &refchain; op = op->_ob_next) {
1730 fprintf(fp, "[%d] ", op->ob_refcnt);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001731 if (PyObject_Print(op, fp, 0) != 0)
1732 PyErr_Clear();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001733 putc('\n', fp);
1734 }
1735}
1736
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +00001737PyObject *
Fred Drake100814d2000-07-09 15:48:49 +00001738_Py_GetObjects(PyObject *self, PyObject *args)
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +00001739{
1740 int i, n;
1741 PyObject *t = NULL;
1742 PyObject *res, *op;
1743
1744 if (!PyArg_ParseTuple(args, "i|O", &n, &t))
1745 return NULL;
1746 op = refchain._ob_next;
1747 res = PyList_New(0);
1748 if (res == NULL)
1749 return NULL;
1750 for (i = 0; (n == 0 || i < n) && op != &refchain; i++) {
1751 while (op == self || op == args || op == res || op == t ||
Guido van Rossum3c296022001-07-14 17:58:00 +00001752 (t != NULL && op->ob_type != (PyTypeObject *) t)) {
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +00001753 op = op->_ob_next;
1754 if (op == &refchain)
1755 return res;
1756 }
1757 if (PyList_Append(res, op) < 0) {
1758 Py_DECREF(res);
1759 return NULL;
1760 }
1761 op = op->_ob_next;
1762 }
1763 return res;
1764}
1765
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001766#endif
Guido van Rossum97ead3f1996-01-12 01:24:09 +00001767
1768
1769/* Hack to force loading of cobject.o */
Guido van Rossumda9c2711996-12-05 21:58:58 +00001770PyTypeObject *_Py_cobject_hack = &PyCObject_Type;
Guido van Rossum84a90321996-05-22 16:34:47 +00001771
1772
1773/* Hack to force loading of abstract.o */
Jeremy Hylton6253f832000-07-12 12:56:19 +00001774int (*_Py_abstract_hack)(PyObject *) = &PyObject_Size;
Guido van Rossume09fb551997-08-05 02:04:34 +00001775
1776
Andrew M. Kuchling1582a3a2000-08-16 12:27:23 +00001777/* Python's malloc wrappers (see pymem.h) */
Guido van Rossume09fb551997-08-05 02:04:34 +00001778
Thomas Wouters334fb892000-07-25 12:56:38 +00001779void *
Fred Drake100814d2000-07-09 15:48:49 +00001780PyMem_Malloc(size_t nbytes)
Guido van Rossume09fb551997-08-05 02:04:34 +00001781{
1782#if _PyMem_EXTRA > 0
1783 if (nbytes == 0)
1784 nbytes = _PyMem_EXTRA;
1785#endif
Guido van Rossumb18618d2000-05-03 23:44:39 +00001786 return PyMem_MALLOC(nbytes);
Guido van Rossume09fb551997-08-05 02:04:34 +00001787}
1788
Thomas Wouters334fb892000-07-25 12:56:38 +00001789void *
1790PyMem_Realloc(void *p, size_t nbytes)
Guido van Rossume09fb551997-08-05 02:04:34 +00001791{
1792#if _PyMem_EXTRA > 0
1793 if (nbytes == 0)
1794 nbytes = _PyMem_EXTRA;
1795#endif
Guido van Rossumb18618d2000-05-03 23:44:39 +00001796 return PyMem_REALLOC(p, nbytes);
Guido van Rossume09fb551997-08-05 02:04:34 +00001797}
1798
1799void
Thomas Wouters334fb892000-07-25 12:56:38 +00001800PyMem_Free(void *p)
Guido van Rossume09fb551997-08-05 02:04:34 +00001801{
Guido van Rossumb18618d2000-05-03 23:44:39 +00001802 PyMem_FREE(p);
1803}
1804
1805
1806/* Python's object malloc wrappers (see objimpl.h) */
1807
Thomas Wouters334fb892000-07-25 12:56:38 +00001808void *
Fred Drake100814d2000-07-09 15:48:49 +00001809PyObject_Malloc(size_t nbytes)
Guido van Rossumb18618d2000-05-03 23:44:39 +00001810{
1811 return PyObject_MALLOC(nbytes);
1812}
1813
Thomas Wouters334fb892000-07-25 12:56:38 +00001814void *
1815PyObject_Realloc(void *p, size_t nbytes)
Guido van Rossumb18618d2000-05-03 23:44:39 +00001816{
1817 return PyObject_REALLOC(p, nbytes);
1818}
1819
1820void
Thomas Wouters334fb892000-07-25 12:56:38 +00001821PyObject_Free(void *p)
Guido van Rossumb18618d2000-05-03 23:44:39 +00001822{
1823 PyObject_FREE(p);
Guido van Rossume09fb551997-08-05 02:04:34 +00001824}
Guido van Rossum86610361998-04-10 22:32:46 +00001825
1826
Fred Drake41deb1e2001-02-01 05:27:45 +00001827/* Hook to clear up weak references only once the _weakref module is
1828 imported. We use a dummy implementation to simplify the code at each
1829 call site instead of requiring a test for NULL.
1830*/
1831
Fred Drakeb60654b2001-02-26 18:56:37 +00001832static void
Fred Drake41deb1e2001-02-01 05:27:45 +00001833empty_clear_weak_refs(PyObject *o)
1834{
Fred Drakeb60654b2001-02-26 18:56:37 +00001835 return;
Fred Drake41deb1e2001-02-01 05:27:45 +00001836}
1837
Fred Drakeb60654b2001-02-26 18:56:37 +00001838void (*PyObject_ClearWeakRefs)(PyObject *) = empty_clear_weak_refs;
Fred Drake41deb1e2001-02-01 05:27:45 +00001839
1840
1841
Guido van Rossum86610361998-04-10 22:32:46 +00001842/* These methods are used to control infinite recursion in repr, str, print,
1843 etc. Container objects that may recursively contain themselves,
1844 e.g. builtin dictionaries and lists, should used Py_ReprEnter() and
1845 Py_ReprLeave() to avoid infinite recursion.
1846
1847 Py_ReprEnter() returns 0 the first time it is called for a particular
1848 object and 1 every time thereafter. It returns -1 if an exception
1849 occurred. Py_ReprLeave() has no return value.
1850
1851 See dictobject.c and listobject.c for examples of use.
1852*/
1853
1854#define KEY "Py_Repr"
1855
1856int
Fred Drake100814d2000-07-09 15:48:49 +00001857Py_ReprEnter(PyObject *obj)
Guido van Rossum86610361998-04-10 22:32:46 +00001858{
1859 PyObject *dict;
1860 PyObject *list;
1861 int i;
1862
1863 dict = PyThreadState_GetDict();
1864 if (dict == NULL)
1865 return -1;
1866 list = PyDict_GetItemString(dict, KEY);
1867 if (list == NULL) {
1868 list = PyList_New(0);
1869 if (list == NULL)
1870 return -1;
1871 if (PyDict_SetItemString(dict, KEY, list) < 0)
1872 return -1;
1873 Py_DECREF(list);
1874 }
1875 i = PyList_GET_SIZE(list);
1876 while (--i >= 0) {
1877 if (PyList_GET_ITEM(list, i) == obj)
1878 return 1;
1879 }
1880 PyList_Append(list, obj);
1881 return 0;
1882}
1883
1884void
Fred Drake100814d2000-07-09 15:48:49 +00001885Py_ReprLeave(PyObject *obj)
Guido van Rossum86610361998-04-10 22:32:46 +00001886{
1887 PyObject *dict;
1888 PyObject *list;
1889 int i;
1890
1891 dict = PyThreadState_GetDict();
Guido van Rossumeb909461998-04-11 15:17:34 +00001892 if (dict == NULL)
1893 return;
Guido van Rossum86610361998-04-10 22:32:46 +00001894 list = PyDict_GetItemString(dict, KEY);
Guido van Rossumeb909461998-04-11 15:17:34 +00001895 if (list == NULL || !PyList_Check(list))
1896 return;
Guido van Rossum86610361998-04-10 22:32:46 +00001897 i = PyList_GET_SIZE(list);
1898 /* Count backwards because we always expect obj to be list[-1] */
1899 while (--i >= 0) {
1900 if (PyList_GET_ITEM(list, i) == obj) {
1901 PyList_SetSlice(list, i, i + 1, NULL);
1902 break;
1903 }
1904 }
1905}
Guido van Rossumd724b232000-03-13 16:01:29 +00001906
1907/*
1908 trashcan
1909 CT 2k0130
1910 non-recursively destroy nested objects
1911
1912 CT 2k0223
1913 everything is now done in a macro.
1914
1915 CT 2k0305
1916 modified to use functions, after Tim Peter's suggestion.
1917
1918 CT 2k0309
1919 modified to restore a possible error.
Guido van Rossum13ff8eb2000-03-25 18:39:19 +00001920
1921 CT 2k0325
1922 added better safe than sorry check for threadstate
Guido van Rossume92e6102000-04-24 15:40:53 +00001923
1924 CT 2k0422
1925 complete rewrite. We now build a chain via ob_type
1926 and save the limited number of types in ob_refcnt.
1927 This is perfect since we don't need any memory.
1928 A patch for free-threading would need just a lock.
Guido van Rossumd724b232000-03-13 16:01:29 +00001929*/
1930
Guido van Rossume92e6102000-04-24 15:40:53 +00001931#define Py_TRASHCAN_TUPLE 1
1932#define Py_TRASHCAN_LIST 2
1933#define Py_TRASHCAN_DICT 3
1934#define Py_TRASHCAN_FRAME 4
1935#define Py_TRASHCAN_TRACEBACK 5
1936/* extend here if other objects want protection */
1937
Guido van Rossumd724b232000-03-13 16:01:29 +00001938int _PyTrash_delete_nesting = 0;
Guido van Rossume92e6102000-04-24 15:40:53 +00001939
Guido van Rossumd724b232000-03-13 16:01:29 +00001940PyObject * _PyTrash_delete_later = NULL;
1941
1942void
Fred Drake100814d2000-07-09 15:48:49 +00001943_PyTrash_deposit_object(PyObject *op)
Guido van Rossumd724b232000-03-13 16:01:29 +00001944{
Guido van Rossume92e6102000-04-24 15:40:53 +00001945 int typecode;
Guido van Rossum13ff8eb2000-03-25 18:39:19 +00001946
Guido van Rossume92e6102000-04-24 15:40:53 +00001947 if (PyTuple_Check(op))
1948 typecode = Py_TRASHCAN_TUPLE;
1949 else if (PyList_Check(op))
1950 typecode = Py_TRASHCAN_LIST;
1951 else if (PyDict_Check(op))
1952 typecode = Py_TRASHCAN_DICT;
1953 else if (PyFrame_Check(op))
1954 typecode = Py_TRASHCAN_FRAME;
1955 else if (PyTraceBack_Check(op))
1956 typecode = Py_TRASHCAN_TRACEBACK;
Moshe Zadkacf703f02000-08-04 15:36:13 +00001957 else /* We have a bug here -- those are the only types in GC */ {
1958 Py_FatalError("Type not supported in GC -- internal bug");
1959 return; /* pacify compiler -- execution never here */
1960 }
Guido van Rossume92e6102000-04-24 15:40:53 +00001961 op->ob_refcnt = typecode;
Guido van Rossumd724b232000-03-13 16:01:29 +00001962
Guido van Rossume92e6102000-04-24 15:40:53 +00001963 op->ob_type = (PyTypeObject*)_PyTrash_delete_later;
1964 _PyTrash_delete_later = op;
Guido van Rossumd724b232000-03-13 16:01:29 +00001965}
1966
1967void
Fred Drake100814d2000-07-09 15:48:49 +00001968_PyTrash_destroy_chain(void)
Guido van Rossumd724b232000-03-13 16:01:29 +00001969{
1970 while (_PyTrash_delete_later) {
1971 PyObject *shredder = _PyTrash_delete_later;
Guido van Rossume92e6102000-04-24 15:40:53 +00001972 _PyTrash_delete_later = (PyObject*) shredder->ob_type;
1973
1974 switch (shredder->ob_refcnt) {
1975 case Py_TRASHCAN_TUPLE:
1976 shredder->ob_type = &PyTuple_Type;
1977 break;
1978 case Py_TRASHCAN_LIST:
1979 shredder->ob_type = &PyList_Type;
1980 break;
1981 case Py_TRASHCAN_DICT:
1982 shredder->ob_type = &PyDict_Type;
1983 break;
1984 case Py_TRASHCAN_FRAME:
1985 shredder->ob_type = &PyFrame_Type;
1986 break;
1987 case Py_TRASHCAN_TRACEBACK:
1988 shredder->ob_type = &PyTraceBack_Type;
1989 break;
1990 }
1991 _Py_NewReference(shredder);
1992
Guido van Rossumd724b232000-03-13 16:01:29 +00001993 ++_PyTrash_delete_nesting;
1994 Py_DECREF(shredder);
1995 --_PyTrash_delete_nesting;
1996 }
1997}
Neil Schemenauera35c6882001-02-27 04:45:05 +00001998
1999#ifdef WITH_PYMALLOC
2000#include "obmalloc.c"
2001#endif