blob: 1d626904b189133256ae15a0d67675afffe76b6a [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001
Guido van Rossum3f5da241990-12-20 15:06:42 +00002/* Generic object operations; and implementation of None (NoObject) */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003
Guido van Rossumc0b618a1997-05-02 03:12:38 +00004#include "Python.h"
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00005
Jack Jansen28fc8802000-07-11 21:47:20 +00006#ifdef macintosh
7#include "macglue.h"
8#endif
9
Guido van Rossume92e6102000-04-24 15:40:53 +000010/* just for trashcan: */
11#include "compile.h"
12#include "frameobject.h"
13#include "traceback.h"
14
Guido van Rossum6f9e4331995-03-29 16:57:48 +000015#if defined( Py_TRACE_REFS ) || defined( Py_REF_DEBUG )
Guido van Rossumbffd6832000-01-20 22:32:56 +000016DL_IMPORT(long) _Py_RefTotal;
Guido van Rossum3f5da241990-12-20 15:06:42 +000017#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000018
Guido van Rossum3f5da241990-12-20 15:06:42 +000019/* Object allocation routines used by NEWOBJ and NEWVAROBJ macros.
20 These are used by the individual routines for object creation.
21 Do not call them otherwise, they do not initialize the object! */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000022
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +000023#ifdef COUNT_ALLOCS
Guido van Rossumc0b618a1997-05-02 03:12:38 +000024static PyTypeObject *type_list;
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +000025extern int tuple_zero_allocs, fast_tuple_allocs;
26extern int quick_int_allocs, quick_neg_int_allocs;
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +000027extern int null_strings, one_strings;
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +000028void
Fred Drake100814d2000-07-09 15:48:49 +000029dump_counts(void)
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +000030{
Guido van Rossumc0b618a1997-05-02 03:12:38 +000031 PyTypeObject *tp;
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +000032
33 for (tp = type_list; tp; tp = tp->tp_next)
Sjoerd Mullender52c1f511993-10-25 08:40:52 +000034 fprintf(stderr, "%s alloc'd: %d, freed: %d, max in use: %d\n",
Tim Peters6d6c1a32001-08-02 04:15:00 +000035 tp->tp_name, tp->tp_allocs, tp->tp_frees,
Sjoerd Mullender52c1f511993-10-25 08:40:52 +000036 tp->tp_maxalloc);
37 fprintf(stderr, "fast tuple allocs: %d, empty: %d\n",
38 fast_tuple_allocs, tuple_zero_allocs);
39 fprintf(stderr, "fast int allocs: pos: %d, neg: %d\n",
40 quick_int_allocs, quick_neg_int_allocs);
41 fprintf(stderr, "null strings: %d, 1-strings: %d\n",
42 null_strings, one_strings);
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +000043}
44
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +000045PyObject *
Fred Drake100814d2000-07-09 15:48:49 +000046get_counts(void)
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +000047{
48 PyTypeObject *tp;
49 PyObject *result;
50 PyObject *v;
51
52 result = PyList_New(0);
53 if (result == NULL)
54 return NULL;
55 for (tp = type_list; tp; tp = tp->tp_next) {
Tim Peters6d6c1a32001-08-02 04:15:00 +000056 v = Py_BuildValue("(siii)", tp->tp_name, tp->tp_allocs,
57 tp->tp_frees, tp->tp_maxalloc);
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +000058 if (v == NULL) {
59 Py_DECREF(result);
60 return NULL;
61 }
62 if (PyList_Append(result, v) < 0) {
63 Py_DECREF(v);
64 Py_DECREF(result);
65 return NULL;
66 }
67 Py_DECREF(v);
68 }
69 return result;
70}
71
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +000072void
Fred Drake100814d2000-07-09 15:48:49 +000073inc_count(PyTypeObject *tp)
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +000074{
Tim Peters6d6c1a32001-08-02 04:15:00 +000075 if (tp->tp_allocs == 0) {
Guido van Rossumd8953cb1995-04-06 14:46:26 +000076 /* first time; insert in linked list */
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +000077 if (tp->tp_next != NULL) /* sanity check */
Guido van Rossumc0b618a1997-05-02 03:12:38 +000078 Py_FatalError("XXX inc_count sanity check");
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +000079 tp->tp_next = type_list;
80 type_list = tp;
81 }
Tim Peters6d6c1a32001-08-02 04:15:00 +000082 tp->tp_allocs++;
83 if (tp->tp_allocs - tp->tp_frees > tp->tp_maxalloc)
84 tp->tp_maxalloc = tp->tp_allocs - tp->tp_frees;
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +000085}
86#endif
87
Guido van Rossumc0b618a1997-05-02 03:12:38 +000088PyObject *
Fred Drake100814d2000-07-09 15:48:49 +000089PyObject_Init(PyObject *op, PyTypeObject *tp)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000090{
Guido van Rossumb18618d2000-05-03 23:44:39 +000091 if (op == NULL) {
92 PyErr_SetString(PyExc_SystemError,
93 "NULL object passed to PyObject_Init");
94 return op;
95 }
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +000096 if (PyType_IS_GC(tp))
97 op = (PyObject *) PyObject_FROM_GC(op);
Guido van Rossumb18618d2000-05-03 23:44:39 +000098 /* 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 }
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000112 if (PyType_IS_GC(tp))
113 op = (PyVarObject *) PyObject_FROM_GC(op);
Guido van Rossumb18618d2000-05-03 23:44:39 +0000114 /* Any changes should be reflected in PyObject_INIT_VAR */
115 op->ob_size = size;
116 op->ob_type = tp;
117 _Py_NewReference((PyObject *)op);
118 return op;
119}
120
121PyObject *
Fred Drake100814d2000-07-09 15:48:49 +0000122_PyObject_New(PyTypeObject *tp)
Guido van Rossumb18618d2000-05-03 23:44:39 +0000123{
124 PyObject *op;
125 op = (PyObject *) PyObject_MALLOC(_PyObject_SIZE(tp));
126 if (op == NULL)
127 return PyErr_NoMemory();
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000128 if (PyType_IS_GC(tp))
129 op = (PyObject *) PyObject_FROM_GC(op);
Guido van Rossumb18618d2000-05-03 23:44:39 +0000130 return PyObject_INIT(op, tp);
131}
132
Guido van Rossumd0c87ee1997-05-15 21:31:03 +0000133PyVarObject *
Fred Drake100814d2000-07-09 15:48:49 +0000134_PyObject_NewVar(PyTypeObject *tp, int size)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000135{
Guido van Rossumb18618d2000-05-03 23:44:39 +0000136 PyVarObject *op;
137 op = (PyVarObject *) PyObject_MALLOC(_PyObject_VAR_SIZE(tp, size));
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000138 if (op == NULL)
Guido van Rossumd0c87ee1997-05-15 21:31:03 +0000139 return (PyVarObject *)PyErr_NoMemory();
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000140 if (PyType_IS_GC(tp))
141 op = (PyVarObject *) PyObject_FROM_GC(op);
Guido van Rossumb18618d2000-05-03 23:44:39 +0000142 return PyObject_INIT_VAR(op, tp, size);
143}
144
145void
Fred Drake100814d2000-07-09 15:48:49 +0000146_PyObject_Del(PyObject *op)
Guido van Rossumb18618d2000-05-03 23:44:39 +0000147{
Guido van Rossum4cc6ac72000-07-01 01:00:38 +0000148 if (op && PyType_IS_GC(op->ob_type)) {
149 op = (PyObject *) PyObject_AS_GC(op);
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000150 }
Guido van Rossum4cc6ac72000-07-01 01:00:38 +0000151 PyObject_FREE(op);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000152}
153
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000154#ifndef WITH_CYCLE_GC
155/* extension modules might need these */
156void _PyGC_Insert(PyObject *op) { }
157void _PyGC_Remove(PyObject *op) { }
158#endif
159
Guido van Rossum90933611991-06-07 16:10:43 +0000160int
Fred Drake100814d2000-07-09 15:48:49 +0000161PyObject_Print(PyObject *op, FILE *fp, int flags)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000162{
Guido van Rossum278ef591991-07-27 21:40:24 +0000163 int ret = 0;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000164 if (PyErr_CheckSignals())
Guido van Rossum90933611991-06-07 16:10:43 +0000165 return -1;
Guido van Rossum9b00dfa1998-04-28 16:06:54 +0000166#ifdef USE_STACKCHECK
167 if (PyOS_CheckStack()) {
Fred Drake661ea262000-10-24 19:57:45 +0000168 PyErr_SetString(PyExc_MemoryError, "stack overflow");
Guido van Rossum9b00dfa1998-04-28 16:06:54 +0000169 return -1;
170 }
171#endif
Guido van Rossum687ef6e2000-01-12 16:28:58 +0000172 clearerr(fp); /* Clear any previous error condition */
Guido van Rossum90933611991-06-07 16:10:43 +0000173 if (op == NULL) {
174 fprintf(fp, "<nil>");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000175 }
Guido van Rossum90933611991-06-07 16:10:43 +0000176 else {
177 if (op->ob_refcnt <= 0)
Fred Drakea44d3532000-06-30 15:01:00 +0000178 fprintf(fp, "<refcnt %u at %p>",
179 op->ob_refcnt, op);
Guido van Rossum2e8f6141992-09-03 20:32:55 +0000180 else if (op->ob_type->tp_print == NULL) {
Guido van Rossum4f288ab2001-05-01 16:53:37 +0000181 PyObject *s;
182 if (flags & Py_PRINT_RAW)
183 s = PyObject_Str(op);
184 else
185 s = PyObject_Repr(op);
186 if (s == NULL)
187 ret = -1;
Guido van Rossum2e8f6141992-09-03 20:32:55 +0000188 else {
Guido van Rossum4f288ab2001-05-01 16:53:37 +0000189 ret = PyObject_Print(s, fp, Py_PRINT_RAW);
Guido van Rossum2e8f6141992-09-03 20:32:55 +0000190 }
Guido van Rossum4f288ab2001-05-01 16:53:37 +0000191 Py_XDECREF(s);
Guido van Rossum2e8f6141992-09-03 20:32:55 +0000192 }
Guido van Rossum90933611991-06-07 16:10:43 +0000193 else
Guido van Rossum278ef591991-07-27 21:40:24 +0000194 ret = (*op->ob_type->tp_print)(op, fp, flags);
Guido van Rossum90933611991-06-07 16:10:43 +0000195 }
Guido van Rossum278ef591991-07-27 21:40:24 +0000196 if (ret == 0) {
197 if (ferror(fp)) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000198 PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossum278ef591991-07-27 21:40:24 +0000199 clearerr(fp);
200 ret = -1;
201 }
202 }
203 return ret;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000204}
205
Barry Warsaw9bf16442001-01-23 16:24:35 +0000206/* For debugging convenience. See Misc/gdbinit for some useful gdb hooks */
Barry Warsawbbd89b62001-01-24 04:18:13 +0000207void _PyObject_Dump(PyObject* op)
Barry Warsaw9bf16442001-01-23 16:24:35 +0000208{
Barry Warsaweefb1072001-02-22 22:39:18 +0000209 if (op == NULL)
210 fprintf(stderr, "NULL\n");
211 else {
212 (void)PyObject_Print(op, stderr, 0);
213 fprintf(stderr, "\nrefcounts: %d\n", op->ob_refcnt);
214 fprintf(stderr, "address : %p\n", op);
215 }
Barry Warsaw9bf16442001-01-23 16:24:35 +0000216}
Barry Warsaw903138f2001-01-23 16:33:18 +0000217
218#ifdef WITH_CYCLE_GC
Barry Warsawbbd89b62001-01-24 04:18:13 +0000219void _PyGC_Dump(PyGC_Head* op)
Barry Warsaw9bf16442001-01-23 16:24:35 +0000220{
Barry Warsawbbd89b62001-01-24 04:18:13 +0000221 _PyObject_Dump(PyObject_FROM_GC(op));
Barry Warsaw9bf16442001-01-23 16:24:35 +0000222}
Barry Warsaw903138f2001-01-23 16:33:18 +0000223#endif /* WITH_CYCLE_GC */
Barry Warsaw9bf16442001-01-23 16:24:35 +0000224
Barry Warsaw7ce36942001-08-24 18:34:26 +0000225
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000226PyObject *
Fred Drake100814d2000-07-09 15:48:49 +0000227PyObject_Repr(PyObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000228{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000229 if (PyErr_CheckSignals())
Guido van Rossum90933611991-06-07 16:10:43 +0000230 return NULL;
Guido van Rossum9b00dfa1998-04-28 16:06:54 +0000231#ifdef USE_STACKCHECK
232 if (PyOS_CheckStack()) {
Fred Drake661ea262000-10-24 19:57:45 +0000233 PyErr_SetString(PyExc_MemoryError, "stack overflow");
Guido van Rossum9b00dfa1998-04-28 16:06:54 +0000234 return NULL;
235 }
236#endif
Guido van Rossum90933611991-06-07 16:10:43 +0000237 if (v == NULL)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000238 return PyString_FromString("<NULL>");
Barry Warsaw7ce36942001-08-24 18:34:26 +0000239 else if (v->ob_type->tp_repr == NULL)
240 return PyString_FromFormat("<%s object at %p",
241 v->ob_type->tp_name, v);
Guido van Rossum4c08d552000-03-10 22:55:18 +0000242 else {
243 PyObject *res;
244 res = (*v->ob_type->tp_repr)(v);
245 if (res == NULL)
246 return NULL;
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000247#ifdef Py_USING_UNICODE
Fredrik Lundhefecc7d2000-07-01 14:31:09 +0000248 if (PyUnicode_Check(res)) {
249 PyObject* str;
Fredrik Lundh2a1e0602000-07-08 17:43:32 +0000250 str = PyUnicode_AsUnicodeEscapeString(res);
Marc-André Lemburg891bc652000-07-03 09:57:53 +0000251 Py_DECREF(res);
252 if (str)
Fredrik Lundhefecc7d2000-07-01 14:31:09 +0000253 res = str;
Marc-André Lemburg891bc652000-07-03 09:57:53 +0000254 else
255 return NULL;
Fredrik Lundhefecc7d2000-07-01 14:31:09 +0000256 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000257#endif
Guido van Rossum4c08d552000-03-10 22:55:18 +0000258 if (!PyString_Check(res)) {
259 PyErr_Format(PyExc_TypeError,
Guido van Rossum5db862d2000-04-10 12:46:51 +0000260 "__repr__ returned non-string (type %.200s)",
Guido van Rossum4c08d552000-03-10 22:55:18 +0000261 res->ob_type->tp_name);
262 Py_DECREF(res);
263 return NULL;
264 }
265 return res;
266 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000267}
268
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000269PyObject *
Fred Drake100814d2000-07-09 15:48:49 +0000270PyObject_Str(PyObject *v)
Guido van Rossumc6004111993-11-05 10:22:19 +0000271{
Guido van Rossum4c08d552000-03-10 22:55:18 +0000272 PyObject *res;
273
Guido van Rossumc6004111993-11-05 10:22:19 +0000274 if (v == NULL)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000275 return PyString_FromString("<NULL>");
Guido van Rossum4f288ab2001-05-01 16:53:37 +0000276 if (PyString_Check(v)) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000277 Py_INCREF(v);
Guido van Rossumc6004111993-11-05 10:22:19 +0000278 return v;
279 }
Guido van Rossum4f288ab2001-05-01 16:53:37 +0000280 if (v->ob_type->tp_str == NULL)
281 return PyObject_Repr(v);
282
283 res = (*v->ob_type->tp_str)(v);
Guido van Rossum4c08d552000-03-10 22:55:18 +0000284 if (res == NULL)
285 return NULL;
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000286#ifdef Py_USING_UNICODE
Marc-André Lemburg891bc652000-07-03 09:57:53 +0000287 if (PyUnicode_Check(res)) {
288 PyObject* str;
289 str = PyUnicode_AsEncodedString(res, NULL, NULL);
290 Py_DECREF(res);
291 if (str)
292 res = str;
293 else
294 return NULL;
295 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000296#endif
Guido van Rossum4c08d552000-03-10 22:55:18 +0000297 if (!PyString_Check(res)) {
298 PyErr_Format(PyExc_TypeError,
Guido van Rossum5db862d2000-04-10 12:46:51 +0000299 "__str__ returned non-string (type %.200s)",
Guido van Rossum4c08d552000-03-10 22:55:18 +0000300 res->ob_type->tp_name);
301 Py_DECREF(res);
302 return NULL;
303 }
304 return res;
Guido van Rossumc6004111993-11-05 10:22:19 +0000305}
306
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000307#ifdef Py_USING_UNICODE
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000308PyObject *
309PyObject_Unicode(PyObject *v)
310{
311 PyObject *res;
312
313 if (v == NULL)
314 res = PyString_FromString("<NULL>");
315 else if (PyUnicode_Check(v)) {
316 Py_INCREF(v);
317 return v;
318 }
Marc-André Lemburgae605342001-03-25 19:16:13 +0000319 else if (PyString_Check(v)) {
320 Py_INCREF(v);
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000321 res = v;
Marc-André Lemburgae605342001-03-25 19:16:13 +0000322 }
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000323 else if (v->ob_type->tp_str != NULL)
324 res = (*v->ob_type->tp_str)(v);
325 else {
326 PyObject *func;
327 static PyObject *strstr;
328 if (strstr == NULL) {
329 strstr= PyString_InternFromString("__str__");
330 if (strstr == NULL)
331 return NULL;
332 }
333 if (!PyInstance_Check(v) ||
334 (func = PyObject_GetAttr(v, strstr)) == NULL) {
335 PyErr_Clear();
336 res = PyObject_Repr(v);
337 }
338 else {
339 res = PyEval_CallObject(func, (PyObject *)NULL);
340 Py_DECREF(func);
341 }
342 }
343 if (res == NULL)
344 return NULL;
345 if (!PyUnicode_Check(res)) {
346 PyObject* str;
347 str = PyUnicode_FromObject(res);
348 Py_DECREF(res);
349 if (str)
350 res = str;
351 else
352 return NULL;
353 }
354 return res;
355}
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000356#endif
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000357
358
Guido van Rossumd1f06b92001-01-24 22:14:43 +0000359/* Macro to get the tp_richcompare field of a type if defined */
360#define RICHCOMPARE(t) (PyType_HasFeature((t), Py_TPFLAGS_HAVE_RICHCOMPARE) \
361 ? (t)->tp_richcompare : NULL)
362
Guido van Rossume797ec12001-01-17 15:24:28 +0000363/* Map rich comparison operators to their swapped version, e.g. LT --> GT */
364static int swapped_op[] = {Py_GT, Py_GE, Py_EQ, Py_NE, Py_LT, Py_LE};
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +0000365
Guido van Rossume797ec12001-01-17 15:24:28 +0000366/* Try a genuine rich comparison, returning an object. Return:
367 NULL for exception;
368 NotImplemented if this particular rich comparison is not implemented or
369 undefined;
370 some object not equal to NotImplemented if it is implemented
371 (this latter object may not be a Boolean).
372*/
373static PyObject *
374try_rich_compare(PyObject *v, PyObject *w, int op)
375{
376 richcmpfunc f;
377 PyObject *res;
378
Guido van Rossumd1f06b92001-01-24 22:14:43 +0000379 if ((f = RICHCOMPARE(v->ob_type)) != NULL) {
Guido van Rossume797ec12001-01-17 15:24:28 +0000380 res = (*f)(v, w, op);
381 if (res != Py_NotImplemented)
382 return res;
383 Py_DECREF(res);
384 }
Guido van Rossumd1f06b92001-01-24 22:14:43 +0000385 if ((f = RICHCOMPARE(w->ob_type)) != NULL) {
Guido van Rossume797ec12001-01-17 15:24:28 +0000386 return (*f)(w, v, swapped_op[op]);
387 }
388 res = Py_NotImplemented;
389 Py_INCREF(res);
390 return res;
391}
392
393/* Try a genuine rich comparison, returning an int. Return:
394 -1 for exception (including the case where try_rich_compare() returns an
395 object that's not a Boolean);
396 0 if the outcome is false;
397 1 if the outcome is true;
398 2 if this particular rich comparison is not implemented or undefined.
399*/
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +0000400static int
Guido van Rossume797ec12001-01-17 15:24:28 +0000401try_rich_compare_bool(PyObject *v, PyObject *w, int op)
402{
403 PyObject *res;
404 int ok;
405
Guido van Rossumd1f06b92001-01-24 22:14:43 +0000406 if (RICHCOMPARE(v->ob_type) == NULL && RICHCOMPARE(w->ob_type) == NULL)
Guido van Rossume797ec12001-01-17 15:24:28 +0000407 return 2; /* Shortcut, avoid INCREF+DECREF */
408 res = try_rich_compare(v, w, op);
409 if (res == NULL)
410 return -1;
411 if (res == Py_NotImplemented) {
412 Py_DECREF(res);
413 return 2;
414 }
415 ok = PyObject_IsTrue(res);
416 Py_DECREF(res);
417 return ok;
418}
419
420/* Try rich comparisons to determine a 3-way comparison. Return:
421 -2 for an exception;
422 -1 if v < w;
423 0 if v == w;
424 1 if v > w;
425 2 if this particular rich comparison is not implemented or undefined.
426*/
427static int
428try_rich_to_3way_compare(PyObject *v, PyObject *w)
429{
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000430 static struct { int op; int outcome; } tries[3] = {
431 /* Try this operator, and if it is true, use this outcome: */
432 {Py_EQ, 0},
433 {Py_LT, -1},
434 {Py_GT, 1},
435 };
436 int i;
437
Guido van Rossumd1f06b92001-01-24 22:14:43 +0000438 if (RICHCOMPARE(v->ob_type) == NULL && RICHCOMPARE(w->ob_type) == NULL)
Guido van Rossume797ec12001-01-17 15:24:28 +0000439 return 2; /* Shortcut */
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000440
441 for (i = 0; i < 3; i++) {
442 switch (try_rich_compare_bool(v, w, tries[i].op)) {
443 case -1:
Tim Peters6d60b2e2001-05-07 20:53:51 +0000444 return -2;
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000445 case 1:
446 return tries[i].outcome;
447 }
Guido van Rossume797ec12001-01-17 15:24:28 +0000448 }
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000449
450 return 2;
Guido van Rossume797ec12001-01-17 15:24:28 +0000451}
452
453/* Try a 3-way comparison, returning an int. Return:
454 -2 for an exception;
455 -1 if v < w;
456 0 if v == w;
457 1 if v > w;
458 2 if this particular 3-way comparison is not implemented or undefined.
459*/
460static int
461try_3way_compare(PyObject *v, PyObject *w)
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +0000462{
463 int c;
Guido van Rossume797ec12001-01-17 15:24:28 +0000464 cmpfunc f;
465
466 /* Comparisons involving instances are given to instance_compare,
467 which has the same return conventions as this function. */
468
469 if (PyInstance_Check(v))
470 return (*v->ob_type->tp_compare)(v, w);
471 if (PyInstance_Check(w))
472 return (*w->ob_type->tp_compare)(v, w);
473
Guido van Rossume797ec12001-01-17 15:24:28 +0000474 /* Try coercion; if it fails, give up */
475 c = PyNumber_CoerceEx(&v, &w);
476 if (c < 0)
477 return -2;
478 if (c > 0)
479 return 2;
480
481 /* Try v's comparison, if defined */
482 if ((f = v->ob_type->tp_compare) != NULL) {
483 c = (*f)(v, w);
484 Py_DECREF(v);
485 Py_DECREF(w);
Martin v. Löwis0163d6d2001-06-09 07:34:05 +0000486 if (c < 0 && PyErr_Occurred())
Guido van Rossume797ec12001-01-17 15:24:28 +0000487 return -2;
488 return c < 0 ? -1 : c > 0 ? 1 : 0;
489 }
490
491 /* Try w's comparison, if defined */
492 if ((f = w->ob_type->tp_compare) != NULL) {
493 c = (*f)(w, v); /* swapped! */
494 Py_DECREF(v);
495 Py_DECREF(w);
Martin v. Löwis0163d6d2001-06-09 07:34:05 +0000496 if (c < 0 && PyErr_Occurred())
Guido van Rossume797ec12001-01-17 15:24:28 +0000497 return -2;
498 return c < 0 ? 1 : c > 0 ? -1 : 0; /* negated! */
499 }
500
501 /* No comparison defined */
502 Py_DECREF(v);
503 Py_DECREF(w);
504 return 2;
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +0000505}
506
Guido van Rossume797ec12001-01-17 15:24:28 +0000507/* Final fallback 3-way comparison, returning an int. Return:
508 -2 if an error occurred;
509 -1 if v < w;
510 0 if v == w;
511 1 if v > w.
512*/
513static int
514default_3way_compare(PyObject *v, PyObject *w)
515{
516 int c;
Guido van Rossum8f9143d2001-01-22 15:59:32 +0000517 char *vname, *wname;
Guido van Rossume797ec12001-01-17 15:24:28 +0000518
519 if (v->ob_type == w->ob_type) {
Barry Warsawb0e754d2001-01-20 06:24:55 +0000520 /* When comparing these pointers, they must be cast to
521 * integer types (i.e. Py_uintptr_t, our spelling of C9X's
522 * uintptr_t). ANSI specifies that pointer compares other
523 * than == and != to non-related structures are undefined.
524 */
Barry Warsaw71ff8d52001-01-20 06:08:10 +0000525 Py_uintptr_t vv = (Py_uintptr_t)v;
526 Py_uintptr_t ww = (Py_uintptr_t)w;
Guido van Rossume797ec12001-01-17 15:24:28 +0000527 return (vv < ww) ? -1 : (vv > ww) ? 1 : 0;
528 }
529
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000530#ifdef Py_USING_UNICODE
Guido van Rossume797ec12001-01-17 15:24:28 +0000531 /* Special case for Unicode */
532 if (PyUnicode_Check(v) || PyUnicode_Check(w)) {
533 c = PyUnicode_Compare(v, w);
534 if (!PyErr_Occurred())
535 return c;
536 /* TypeErrors are ignored: if Unicode coercion fails due
537 to one of the arguments not having the right type, we
538 continue as defined by the coercion protocol (see
539 above). Luckily, decoding errors are reported as
540 ValueErrors and are not masked by this technique. */
541 if (!PyErr_ExceptionMatches(PyExc_TypeError))
542 return -2;
543 PyErr_Clear();
544 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000545#endif
Guido van Rossume797ec12001-01-17 15:24:28 +0000546
Guido van Rossum0871e932001-01-22 19:28:09 +0000547 /* None is smaller than anything */
548 if (v == Py_None)
549 return -1;
550 if (w == Py_None)
551 return 1;
552
Guido van Rossume797ec12001-01-17 15:24:28 +0000553 /* different type: compare type names */
Guido van Rossum8f9143d2001-01-22 15:59:32 +0000554 if (v->ob_type->tp_as_number)
555 vname = "";
556 else
557 vname = v->ob_type->tp_name;
558 if (w->ob_type->tp_as_number)
559 wname = "";
560 else
561 wname = w->ob_type->tp_name;
562 c = strcmp(vname, wname);
563 if (c < 0)
564 return -1;
565 if (c > 0)
566 return 1;
567 /* Same type name, or (more likely) incomparable numeric types */
568 return ((Py_uintptr_t)(v->ob_type) < (
569 Py_uintptr_t)(w->ob_type)) ? -1 : 1;
Guido van Rossume797ec12001-01-17 15:24:28 +0000570}
571
572#define CHECK_TYPES(o) PyType_HasFeature((o)->ob_type, Py_TPFLAGS_CHECKTYPES)
573
Tim Peters6d60b2e2001-05-07 20:53:51 +0000574/* Do a 3-way comparison, by hook or by crook. Return:
575 -2 for an exception;
576 -1 if v < w;
577 0 if v == w;
578 1 if v > w;
Martin v. Löwis0163d6d2001-06-09 07:34:05 +0000579 If the object implements a tp_compare function, it returns
580 whatever this function returns (whether with an exception or not).
Tim Peters6d60b2e2001-05-07 20:53:51 +0000581*/
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +0000582static int
Fred Drake100814d2000-07-09 15:48:49 +0000583do_cmp(PyObject *v, PyObject *w)
Guido van Rossum20566841995-01-12 11:26:10 +0000584{
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +0000585 int c;
Martin v. Löwis0163d6d2001-06-09 07:34:05 +0000586 cmpfunc f;
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +0000587
Martin v. Löwis0163d6d2001-06-09 07:34:05 +0000588 if (v->ob_type == w->ob_type
Guido van Rossum82fc51c2001-08-16 08:02:45 +0000589 && (f = v->ob_type->tp_compare) != NULL) {
590 c = (*f)(v, w);
591 if (c != 2 || !PyInstance_Check(v))
592 return c;
593 }
Guido van Rossume797ec12001-01-17 15:24:28 +0000594 c = try_rich_to_3way_compare(v, w);
595 if (c < 2)
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +0000596 return c;
Guido van Rossume797ec12001-01-17 15:24:28 +0000597 c = try_3way_compare(v, w);
598 if (c < 2)
599 return c;
600 return default_3way_compare(v, w);
Guido van Rossum20566841995-01-12 11:26:10 +0000601}
602
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000603/* compare_nesting is incremented before calling compare (for
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000604 some types) and decremented on exit. If the count exceeds the
605 nesting limit, enable code to detect circular data structures.
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000606
607 This is a tunable parameter that should only affect the performance
608 of comparisons, nothing else. Setting it high makes comparing deeply
609 nested non-cyclical data structures faster, but makes comparing cyclical
610 data structures slower.
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000611*/
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000612#define NESTING_LIMIT 20
613
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000614static int compare_nesting = 0;
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000615
616static PyObject*
Fred Drake100814d2000-07-09 15:48:49 +0000617get_inprogress_dict(void)
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000618{
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000619 static PyObject *key;
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000620 PyObject *tstate_dict, *inprogress;
621
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000622 if (key == NULL) {
623 key = PyString_InternFromString("cmp_state");
624 if (key == NULL)
625 return NULL;
626 }
627
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000628 tstate_dict = PyThreadState_GetDict();
629 if (tstate_dict == NULL) {
630 PyErr_BadInternalCall();
631 return NULL;
632 }
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000633
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000634 inprogress = PyDict_GetItem(tstate_dict, key);
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000635 if (inprogress == NULL) {
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000636 inprogress = PyDict_New();
637 if (inprogress == NULL)
638 return NULL;
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000639 if (PyDict_SetItem(tstate_dict, key, inprogress) == -1) {
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000640 Py_DECREF(inprogress);
641 return NULL;
642 }
Jeremy Hyltona251ea02000-06-09 16:20:39 +0000643 Py_DECREF(inprogress);
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000644 }
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000645
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000646 return inprogress;
647}
648
649static PyObject *
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000650check_recursion(PyObject *v, PyObject *w, int op)
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000651{
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000652 PyObject *inprogress;
653 PyObject *token;
Barry Warsaw9d23a4e2000-08-18 05:01:19 +0000654 Py_uintptr_t iv = (Py_uintptr_t)v;
655 Py_uintptr_t iw = (Py_uintptr_t)w;
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000656 PyObject *x, *y, *z;
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000657
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000658 inprogress = get_inprogress_dict();
659 if (inprogress == NULL)
660 return NULL;
661
662 token = PyTuple_New(3);
663 if (token == NULL)
664 return NULL;
665
666 if (iv <= iw) {
667 PyTuple_SET_ITEM(token, 0, x = PyLong_FromVoidPtr((void *)v));
668 PyTuple_SET_ITEM(token, 1, y = PyLong_FromVoidPtr((void *)w));
669 if (op >= 0)
670 op = swapped_op[op];
671 } else {
672 PyTuple_SET_ITEM(token, 0, x = PyLong_FromVoidPtr((void *)w));
673 PyTuple_SET_ITEM(token, 1, y = PyLong_FromVoidPtr((void *)v));
674 }
675 PyTuple_SET_ITEM(token, 2, z = PyInt_FromLong((long)op));
676 if (x == NULL || y == NULL || z == NULL) {
677 Py_DECREF(token);
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000678 return NULL;
679 }
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000680
681 if (PyDict_GetItem(inprogress, token) != NULL) {
682 Py_DECREF(token);
683 return Py_None; /* Without INCREF! */
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000684 }
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000685
686 if (PyDict_SetItem(inprogress, token, token) < 0) {
687 Py_DECREF(token);
688 return NULL;
689 }
690
691 return token;
692}
693
694static void
695delete_token(PyObject *token)
696{
697 PyObject *inprogress;
698
699 if (token == NULL || token == Py_None)
700 return;
701 inprogress = get_inprogress_dict();
702 if (inprogress == NULL)
703 PyErr_Clear();
704 else
705 PyDict_DelItem(inprogress, token);
706 Py_DECREF(token);
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000707}
708
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000709int
Fred Drake100814d2000-07-09 15:48:49 +0000710PyObject_Compare(PyObject *v, PyObject *w)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000711{
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000712 PyTypeObject *vtp;
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000713 int result;
714
Jack Jansend49cbe12000-08-22 21:52:51 +0000715#if defined(USE_STACKCHECK)
Guido van Rossum9c0a99e2000-08-30 15:53:50 +0000716 if (PyOS_CheckStack()) {
Jack Jansend49cbe12000-08-22 21:52:51 +0000717 PyErr_SetString(PyExc_MemoryError, "Stack overflow");
718 return -1;
719 }
720#endif
Guido van Rossumc8b6df91997-05-23 00:06:51 +0000721 if (v == NULL || w == NULL) {
722 PyErr_BadInternalCall();
723 return -1;
724 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000725 if (v == w)
726 return 0;
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +0000727 vtp = v->ob_type;
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000728 compare_nesting++;
729 if (compare_nesting > NESTING_LIMIT &&
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +0000730 (vtp->tp_as_mapping
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000731 || (vtp->tp_as_sequence
732 && !PyString_Check(v)
733 && !PyTuple_Check(v)))) {
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +0000734 /* try to detect circular data structures */
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000735 PyObject *token = check_recursion(v, w, -1);
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000736
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000737 if (token == NULL) {
738 result = -1;
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000739 }
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000740 else if (token == Py_None) {
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000741 /* already comparing these objects. assume
742 they're equal until shown otherwise */
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +0000743 result = 0;
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000744 }
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000745 else {
746 result = do_cmp(v, w);
747 delete_token(token);
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000748 }
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000749 }
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +0000750 else {
751 result = do_cmp(v, w);
752 }
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000753 compare_nesting--;
Guido van Rossume797ec12001-01-17 15:24:28 +0000754 return result < 0 ? -1 : result;
755}
756
757static PyObject *
Martin v. Löwis0163d6d2001-06-09 07:34:05 +0000758convert_3way_to_object(int op, int c)
Guido van Rossume797ec12001-01-17 15:24:28 +0000759{
Guido van Rossume797ec12001-01-17 15:24:28 +0000760 PyObject *result;
Guido van Rossume797ec12001-01-17 15:24:28 +0000761 switch (op) {
762 case Py_LT: c = c < 0; break;
763 case Py_LE: c = c <= 0; break;
764 case Py_EQ: c = c == 0; break;
765 case Py_NE: c = c != 0; break;
766 case Py_GT: c = c > 0; break;
767 case Py_GE: c = c >= 0; break;
768 }
769 result = c ? Py_True : Py_False;
770 Py_INCREF(result);
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000771 return result;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000772}
Martin v. Löwis0163d6d2001-06-09 07:34:05 +0000773
774
775static PyObject *
776try_3way_to_rich_compare(PyObject *v, PyObject *w, int op)
777{
778 int c;
779
780 c = try_3way_compare(v, w);
781 if (c >= 2)
782 c = default_3way_compare(v, w);
783 if (c <= -2)
784 return NULL;
785 return convert_3way_to_object(op, c);
786}
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000787
Neil Schemenauerd38855c2001-01-21 16:25:18 +0000788static PyObject *
Guido van Rossume797ec12001-01-17 15:24:28 +0000789do_richcmp(PyObject *v, PyObject *w, int op)
790{
791 PyObject *res;
Martin v. Löwis0163d6d2001-06-09 07:34:05 +0000792 cmpfunc f;
793
794 /* If the types are equal, don't bother with coercions etc.
795 Instances are special-cased in try_3way_compare, since
796 a result of 2 does *not* mean one value being greater
797 than the other. */
798 if (v->ob_type == w->ob_type
799 && (f = v->ob_type->tp_compare) != NULL
800 && !PyInstance_Check(v)) {
801 int c;
802 richcmpfunc f1;
803 if ((f1 = RICHCOMPARE(v->ob_type)) != NULL) {
804 /* If the type has richcmp, try it first.
805 try_rich_compare would try it two-sided,
806 which is not needed since we've a single
807 type only. */
808 res = (*f1)(v, w, op);
809 if (res != Py_NotImplemented)
810 return res;
811 Py_DECREF(res);
812 }
813 c = (*f)(v, w);
814 if (c < 0 && PyErr_Occurred())
815 return NULL;
816 return convert_3way_to_object(op, c);
817 }
Guido van Rossume797ec12001-01-17 15:24:28 +0000818
819 res = try_rich_compare(v, w, op);
820 if (res != Py_NotImplemented)
821 return res;
822 Py_DECREF(res);
823
824 return try_3way_to_rich_compare(v, w, op);
825}
826
827PyObject *
828PyObject_RichCompare(PyObject *v, PyObject *w, int op)
829{
830 PyObject *res;
831
832 assert(Py_LT <= op && op <= Py_GE);
833
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000834 compare_nesting++;
835 if (compare_nesting > NESTING_LIMIT &&
836 (v->ob_type->tp_as_mapping
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000837 || (v->ob_type->tp_as_sequence
838 && !PyString_Check(v)
839 && !PyTuple_Check(v)))) {
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000840 /* try to detect circular data structures */
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000841 PyObject *token = check_recursion(v, w, op);
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000842
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000843 if (token == NULL) {
844 res = NULL;
Guido van Rossume797ec12001-01-17 15:24:28 +0000845 }
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000846 else if (token == Py_None) {
847 /* already comparing these objects with this operator.
848 assume they're equal until shown otherwise */
849 if (op == Py_EQ)
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000850 res = Py_True;
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000851 else if (op == Py_NE)
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000852 res = Py_False;
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000853 else {
854 PyErr_SetString(PyExc_ValueError,
855 "can't order recursive values");
856 res = NULL;
857 }
858 Py_XINCREF(res);
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000859 }
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000860 else {
861 res = do_richcmp(v, w, op);
862 delete_token(token);
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000863 }
Guido van Rossume797ec12001-01-17 15:24:28 +0000864 }
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000865 else {
866 res = do_richcmp(v, w, op);
867 }
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000868 compare_nesting--;
Guido van Rossume797ec12001-01-17 15:24:28 +0000869 return res;
870}
871
Tim Petersde9725f2001-05-05 10:06:17 +0000872/* Return -1 if error; 1 if v op w; 0 if not (v op w). */
Guido van Rossume797ec12001-01-17 15:24:28 +0000873int
874PyObject_RichCompareBool(PyObject *v, PyObject *w, int op)
875{
876 PyObject *res = PyObject_RichCompare(v, w, op);
877 int ok;
878
879 if (res == NULL)
880 return -1;
881 ok = PyObject_IsTrue(res);
882 Py_DECREF(res);
883 return ok;
884}
Fred Drake13634cf2000-06-29 19:17:04 +0000885
886/* Set of hash utility functions to help maintaining the invariant that
887 iff a==b then hash(a)==hash(b)
888
889 All the utility functions (_Py_Hash*()) return "-1" to signify an error.
890*/
891
892long
Fred Drake100814d2000-07-09 15:48:49 +0000893_Py_HashDouble(double v)
Fred Drake13634cf2000-06-29 19:17:04 +0000894{
Tim Peters39dce292000-08-15 03:34:48 +0000895 double intpart, fractpart;
896 int expo;
897 long hipart;
898 long x; /* the final hash value */
899 /* This is designed so that Python numbers of different types
900 * that compare equal hash to the same value; otherwise comparisons
901 * of mapping keys will turn out weird.
902 */
903
904#ifdef MPW /* MPW C modf expects pointer to extended as second argument */
905{
906 extended e;
907 fractpart = modf(v, &e);
908 intpart = e;
909}
910#else
911 fractpart = modf(v, &intpart);
912#endif
913 if (fractpart == 0.0) {
914 /* This must return the same hash as an equal int or long. */
915 if (intpart > LONG_MAX || -intpart > LONG_MAX) {
916 /* Convert to long and use its hash. */
917 PyObject *plong; /* converted to Python long */
918 if (Py_IS_INFINITY(intpart))
919 /* can't convert to long int -- arbitrary */
920 v = v < 0 ? -271828.0 : 314159.0;
921 plong = PyLong_FromDouble(v);
922 if (plong == NULL)
923 return -1;
924 x = PyObject_Hash(plong);
925 Py_DECREF(plong);
926 return x;
927 }
928 /* Fits in a C long == a Python int, so is its own hash. */
929 x = (long)intpart;
930 if (x == -1)
931 x = -2;
932 return x;
933 }
934 /* The fractional part is non-zero, so we don't have to worry about
935 * making this match the hash of some other type.
936 * Use frexp to get at the bits in the double.
Fred Drake13634cf2000-06-29 19:17:04 +0000937 * Since the VAX D double format has 56 mantissa bits, which is the
938 * most of any double format in use, each of these parts may have as
939 * many as (but no more than) 56 significant bits.
Tim Peters39dce292000-08-15 03:34:48 +0000940 * So, assuming sizeof(long) >= 4, each part can be broken into two
941 * longs; frexp and multiplication are used to do that.
942 * Also, since the Cray double format has 15 exponent bits, which is
943 * the most of any double format in use, shifting the exponent field
944 * left by 15 won't overflow a long (again assuming sizeof(long) >= 4).
Fred Drake13634cf2000-06-29 19:17:04 +0000945 */
Tim Peters39dce292000-08-15 03:34:48 +0000946 v = frexp(v, &expo);
947 v *= 2147483648.0; /* 2**31 */
948 hipart = (long)v; /* take the top 32 bits */
949 v = (v - (double)hipart) * 2147483648.0; /* get the next 32 bits */
950 x = hipart + (long)v + (expo << 15);
951 if (x == -1)
952 x = -2;
953 return x;
Fred Drake13634cf2000-06-29 19:17:04 +0000954}
955
956long
Fred Drake100814d2000-07-09 15:48:49 +0000957_Py_HashPointer(void *p)
Fred Drake13634cf2000-06-29 19:17:04 +0000958{
959#if SIZEOF_LONG >= SIZEOF_VOID_P
960 return (long)p;
961#else
962 /* convert to a Python long and hash that */
963 PyObject* longobj;
964 long x;
965
966 if ((longobj = PyLong_FromVoidPtr(p)) == NULL) {
967 x = -1;
968 goto finally;
969 }
970 x = PyObject_Hash(longobj);
971
972finally:
973 Py_XDECREF(longobj);
974 return x;
975#endif
976}
977
978
Guido van Rossum9bfef441993-03-29 10:43:31 +0000979long
Fred Drake100814d2000-07-09 15:48:49 +0000980PyObject_Hash(PyObject *v)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000981{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000982 PyTypeObject *tp = v->ob_type;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000983 if (tp->tp_hash != NULL)
984 return (*tp->tp_hash)(v);
Guido van Rossumd1f06b92001-01-24 22:14:43 +0000985 if (tp->tp_compare == NULL && RICHCOMPARE(tp) == NULL) {
Fred Drake13634cf2000-06-29 19:17:04 +0000986 return _Py_HashPointer(v); /* Use address as hash value */
987 }
Guido van Rossum9bfef441993-03-29 10:43:31 +0000988 /* If there's a cmp but no hash defined, the object can't be hashed */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000989 PyErr_SetString(PyExc_TypeError, "unhashable type");
Guido van Rossum9bfef441993-03-29 10:43:31 +0000990 return -1;
991}
992
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000993PyObject *
Fred Drake100814d2000-07-09 15:48:49 +0000994PyObject_GetAttrString(PyObject *v, char *name)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000995{
Tim Peters6d6c1a32001-08-02 04:15:00 +0000996 PyObject *w, *res;
Guido van Rossumd8eb1b31996-08-09 20:52:03 +0000997
Tim Peters6d6c1a32001-08-02 04:15:00 +0000998 if (v->ob_type->tp_getattr != NULL)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000999 return (*v->ob_type->tp_getattr)(v, name);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001000 w = PyString_InternFromString(name);
1001 if (w == NULL)
1002 return NULL;
1003 res = PyObject_GetAttr(v, w);
1004 Py_XDECREF(w);
1005 return res;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001006}
1007
1008int
Fred Drake100814d2000-07-09 15:48:49 +00001009PyObject_HasAttrString(PyObject *v, char *name)
Guido van Rossumed18fdc1993-07-11 19:55:34 +00001010{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001011 PyObject *res = PyObject_GetAttrString(v, name);
Guido van Rossumed18fdc1993-07-11 19:55:34 +00001012 if (res != NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001013 Py_DECREF(res);
Guido van Rossumed18fdc1993-07-11 19:55:34 +00001014 return 1;
1015 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001016 PyErr_Clear();
Guido van Rossumed18fdc1993-07-11 19:55:34 +00001017 return 0;
1018}
1019
1020int
Fred Drake100814d2000-07-09 15:48:49 +00001021PyObject_SetAttrString(PyObject *v, char *name, PyObject *w)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001022{
Tim Peters6d6c1a32001-08-02 04:15:00 +00001023 PyObject *s;
1024 int res;
Guido van Rossumd8eb1b31996-08-09 20:52:03 +00001025
Tim Peters6d6c1a32001-08-02 04:15:00 +00001026 if (v->ob_type->tp_setattr != NULL)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001027 return (*v->ob_type->tp_setattr)(v, name, w);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001028 s = PyString_InternFromString(name);
1029 if (s == NULL)
1030 return -1;
1031 res = PyObject_SetAttr(v, s, w);
1032 Py_XDECREF(s);
1033 return res;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001034}
1035
Guido van Rossum98ff96a1997-05-20 18:34:44 +00001036PyObject *
Fred Drake100814d2000-07-09 15:48:49 +00001037PyObject_GetAttr(PyObject *v, PyObject *name)
Guido van Rossum98ff96a1997-05-20 18:34:44 +00001038{
Tim Peters6d6c1a32001-08-02 04:15:00 +00001039 PyTypeObject *tp = v->ob_type;
1040
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001041#ifdef Py_USING_UNICODE
Marc-André Lemburge44e5072000-09-18 16:20:57 +00001042 /* The Unicode to string conversion is done here because the
1043 existing tp_getattro slots expect a string object as name
1044 and we wouldn't want to break those. */
1045 if (PyUnicode_Check(name)) {
1046 name = _PyUnicode_AsDefaultEncodedString(name, NULL);
1047 if (name == NULL)
1048 return NULL;
1049 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001050#endif
1051
Jeremy Hylton99a8f902000-06-23 14:36:32 +00001052 if (!PyString_Check(name)) {
1053 PyErr_SetString(PyExc_TypeError,
1054 "attribute name must be string");
1055 return NULL;
1056 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001057 if (tp->tp_getattro != NULL)
1058 return (*tp->tp_getattro)(v, name);
1059 if (tp->tp_getattr != NULL)
1060 return (*tp->tp_getattr)(v, PyString_AS_STRING(name));
1061 PyErr_Format(PyExc_AttributeError,
1062 "'%.50s' object has no attribute '%.400s'",
1063 tp->tp_name, PyString_AS_STRING(name));
1064 return NULL;
Guido van Rossum98ff96a1997-05-20 18:34:44 +00001065}
1066
1067int
Fred Drake100814d2000-07-09 15:48:49 +00001068PyObject_HasAttr(PyObject *v, PyObject *name)
Guido van Rossum98ff96a1997-05-20 18:34:44 +00001069{
1070 PyObject *res = PyObject_GetAttr(v, name);
1071 if (res != NULL) {
1072 Py_DECREF(res);
1073 return 1;
1074 }
1075 PyErr_Clear();
1076 return 0;
1077}
1078
1079int
Fred Drake100814d2000-07-09 15:48:49 +00001080PyObject_SetAttr(PyObject *v, PyObject *name, PyObject *value)
Guido van Rossum98ff96a1997-05-20 18:34:44 +00001081{
Tim Peters6d6c1a32001-08-02 04:15:00 +00001082 PyTypeObject *tp = v->ob_type;
Guido van Rossum98ff96a1997-05-20 18:34:44 +00001083 int err;
Marc-André Lemburge44e5072000-09-18 16:20:57 +00001084
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001085#ifdef Py_USING_UNICODE
Marc-André Lemburge44e5072000-09-18 16:20:57 +00001086 /* The Unicode to string conversion is done here because the
1087 existing tp_setattro slots expect a string object as name
1088 and we wouldn't want to break those. */
1089 if (PyUnicode_Check(name)) {
1090 name = PyUnicode_AsEncodedString(name, NULL, NULL);
1091 if (name == NULL)
1092 return -1;
Jeremy Hylton99a8f902000-06-23 14:36:32 +00001093 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001094 else
1095#endif
1096 if (!PyString_Check(name)){
Jeremy Hylton99a8f902000-06-23 14:36:32 +00001097 PyErr_SetString(PyExc_TypeError,
1098 "attribute name must be string");
Tim Peters6d6c1a32001-08-02 04:15:00 +00001099 return -1;
Jeremy Hylton99a8f902000-06-23 14:36:32 +00001100 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001101 else
1102 Py_INCREF(name);
1103
1104 PyString_InternInPlace(&name);
1105 if (tp->tp_setattro != NULL) {
1106 err = (*tp->tp_setattro)(v, name, value);
1107 Py_DECREF(name);
1108 return err;
Marc-André Lemburge44e5072000-09-18 16:20:57 +00001109 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001110 if (tp->tp_setattr != NULL) {
1111 err = (*tp->tp_setattr)(v, PyString_AS_STRING(name), value);
1112 Py_DECREF(name);
1113 return err;
1114 }
Guido van Rossum98ff96a1997-05-20 18:34:44 +00001115 Py_DECREF(name);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001116 if (tp->tp_getattr == NULL && tp->tp_getattro == NULL)
1117 PyErr_Format(PyExc_TypeError,
1118 "'%.100s' object has no attributes "
1119 "(%s .%.100s)",
1120 tp->tp_name,
1121 value==NULL ? "del" : "assign to",
1122 PyString_AS_STRING(name));
1123 else
1124 PyErr_Format(PyExc_TypeError,
1125 "'%.100s' object has only read-only attributes "
1126 "(%s .%.100s)",
1127 tp->tp_name,
1128 value==NULL ? "del" : "assign to",
1129 PyString_AS_STRING(name));
1130 return -1;
1131}
1132
1133/* Helper to get a pointer to an object's __dict__ slot, if any */
1134
1135PyObject **
1136_PyObject_GetDictPtr(PyObject *obj)
1137{
1138#define PTRSIZE (sizeof(PyObject *))
1139
1140 long dictoffset;
1141 PyTypeObject *tp = obj->ob_type;
1142
1143 if (!(tp->tp_flags & Py_TPFLAGS_HAVE_CLASS))
1144 return NULL;
1145 dictoffset = tp->tp_dictoffset;
1146 if (dictoffset == 0)
1147 return NULL;
1148 if (dictoffset < 0) {
1149 dictoffset += PyType_BASICSIZE(tp);
1150 assert(dictoffset > 0); /* Sanity check */
1151 if (tp->tp_itemsize > 0) {
1152 int n = ((PyVarObject *)obj)->ob_size;
1153 if (n > 0) {
1154 dictoffset += tp->tp_itemsize * n;
1155 /* Round up, if necessary */
1156 if (tp->tp_itemsize % PTRSIZE != 0) {
1157 dictoffset += PTRSIZE - 1;
1158 dictoffset /= PTRSIZE;
1159 dictoffset *= PTRSIZE;
1160 }
1161 }
1162 }
1163 }
1164 return (PyObject **) ((char *)obj + dictoffset);
1165}
1166
1167/* Generic GetAttr functions - put these in your tp_[gs]etattro slot */
1168
1169PyObject *
1170PyObject_GenericGetAttr(PyObject *obj, PyObject *name)
1171{
1172 PyTypeObject *tp = obj->ob_type;
1173 PyObject *descr;
1174 descrgetfunc f;
1175 PyObject **dictptr;
1176
1177 if (tp->tp_dict == NULL) {
Guido van Rossum528b7eb2001-08-07 17:24:28 +00001178 if (PyType_Ready(tp) < 0)
Tim Peters6d6c1a32001-08-02 04:15:00 +00001179 return NULL;
1180 }
1181
1182 descr = _PyType_Lookup(tp, name);
1183 f = NULL;
1184 if (descr != NULL) {
1185 f = descr->ob_type->tp_descr_get;
1186 if (f != NULL && PyDescr_IsData(descr))
1187 return f(descr, obj, (PyObject *)obj->ob_type);
1188 }
1189
1190 dictptr = _PyObject_GetDictPtr(obj);
1191 if (dictptr != NULL) {
1192 PyObject *dict = *dictptr;
1193 if (dict != NULL) {
1194 PyObject *res = PyDict_GetItem(dict, name);
1195 if (res != NULL) {
1196 Py_INCREF(res);
1197 return res;
1198 }
1199 }
1200 }
1201
1202 if (f != NULL)
1203 return f(descr, obj, (PyObject *)obj->ob_type);
1204
1205 if (descr != NULL) {
1206 Py_INCREF(descr);
1207 return descr;
1208 }
1209
1210 PyErr_Format(PyExc_AttributeError,
1211 "'%.50s' object has no attribute '%.400s'",
1212 tp->tp_name, PyString_AS_STRING(name));
1213 return NULL;
1214}
1215
1216int
1217PyObject_GenericSetAttr(PyObject *obj, PyObject *name, PyObject *value)
1218{
1219 PyTypeObject *tp = obj->ob_type;
1220 PyObject *descr;
1221 descrsetfunc f;
1222 PyObject **dictptr;
1223
1224 if (tp->tp_dict == NULL) {
Guido van Rossum528b7eb2001-08-07 17:24:28 +00001225 if (PyType_Ready(tp) < 0)
Tim Peters6d6c1a32001-08-02 04:15:00 +00001226 return -1;
1227 }
1228
1229 descr = _PyType_Lookup(tp, name);
1230 f = NULL;
1231 if (descr != NULL) {
1232 f = descr->ob_type->tp_descr_set;
1233 if (f != NULL && PyDescr_IsData(descr))
1234 return f(descr, obj, value);
1235 }
1236
1237 dictptr = _PyObject_GetDictPtr(obj);
1238 if (dictptr != NULL) {
1239 PyObject *dict = *dictptr;
1240 if (dict == NULL && value != NULL) {
1241 dict = PyDict_New();
1242 if (dict == NULL)
1243 return -1;
1244 *dictptr = dict;
1245 }
1246 if (dict != NULL) {
1247 int res;
1248 if (value == NULL)
1249 res = PyDict_DelItem(dict, name);
1250 else
1251 res = PyDict_SetItem(dict, name, value);
1252 if (res < 0 && PyErr_ExceptionMatches(PyExc_KeyError))
1253 PyErr_SetObject(PyExc_AttributeError, name);
1254 return res;
1255 }
1256 }
1257
1258 if (f != NULL)
1259 return f(descr, obj, value);
1260
1261 if (descr == NULL) {
1262 PyErr_Format(PyExc_AttributeError,
1263 "'%.50s' object has no attribute '%.400s'",
1264 tp->tp_name, PyString_AS_STRING(name));
1265 return -1;
1266 }
1267
1268 PyErr_Format(PyExc_AttributeError,
1269 "'%.50s' object attribute '%.400s' is read-only",
1270 tp->tp_name, PyString_AS_STRING(name));
1271 return -1;
Guido van Rossum98ff96a1997-05-20 18:34:44 +00001272}
1273
Guido van Rossum6ac258d1993-05-12 08:24:20 +00001274/* Test a value used as condition, e.g., in a for or if statement.
1275 Return -1 if an error occurred */
1276
1277int
Fred Drake100814d2000-07-09 15:48:49 +00001278PyObject_IsTrue(PyObject *v)
Guido van Rossum6ac258d1993-05-12 08:24:20 +00001279{
1280 int res;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001281 if (v == Py_None)
Guido van Rossum6ac258d1993-05-12 08:24:20 +00001282 res = 0;
Guido van Rossum1c4f4581998-05-22 00:53:24 +00001283 else if (v->ob_type->tp_as_number != NULL &&
1284 v->ob_type->tp_as_number->nb_nonzero != NULL)
Guido van Rossum6ac258d1993-05-12 08:24:20 +00001285 res = (*v->ob_type->tp_as_number->nb_nonzero)(v);
Guido van Rossum1c4f4581998-05-22 00:53:24 +00001286 else if (v->ob_type->tp_as_mapping != NULL &&
1287 v->ob_type->tp_as_mapping->mp_length != NULL)
Guido van Rossum6ac258d1993-05-12 08:24:20 +00001288 res = (*v->ob_type->tp_as_mapping->mp_length)(v);
Guido van Rossum1c4f4581998-05-22 00:53:24 +00001289 else if (v->ob_type->tp_as_sequence != NULL &&
1290 v->ob_type->tp_as_sequence->sq_length != NULL)
Guido van Rossum6ac258d1993-05-12 08:24:20 +00001291 res = (*v->ob_type->tp_as_sequence->sq_length)(v);
1292 else
1293 res = 1;
1294 if (res > 0)
1295 res = 1;
1296 return res;
1297}
1298
Guido van Rossumc3d3f961998-04-09 17:53:59 +00001299/* equivalent of 'not v'
1300 Return -1 if an error occurred */
1301
1302int
Fred Drake100814d2000-07-09 15:48:49 +00001303PyObject_Not(PyObject *v)
Guido van Rossumc3d3f961998-04-09 17:53:59 +00001304{
1305 int res;
1306 res = PyObject_IsTrue(v);
1307 if (res < 0)
1308 return res;
1309 return res == 0;
1310}
1311
Guido van Rossum5524a591995-01-10 15:26:20 +00001312/* Coerce two numeric types to the "larger" one.
1313 Increment the reference count on each argument.
Guido van Rossume797ec12001-01-17 15:24:28 +00001314 Return value:
1315 -1 if an error occurred;
1316 0 if the coercion succeeded (and then the reference counts are increased);
1317 1 if no coercion is possible (and no error is raised).
Guido van Rossum5524a591995-01-10 15:26:20 +00001318*/
Guido van Rossum5524a591995-01-10 15:26:20 +00001319int
Fred Drake100814d2000-07-09 15:48:49 +00001320PyNumber_CoerceEx(PyObject **pv, PyObject **pw)
Guido van Rossum5524a591995-01-10 15:26:20 +00001321{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001322 register PyObject *v = *pv;
1323 register PyObject *w = *pw;
Guido van Rossum5524a591995-01-10 15:26:20 +00001324 int res;
1325
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001326 if (v->ob_type == w->ob_type && !PyInstance_Check(v)) {
1327 Py_INCREF(v);
1328 Py_INCREF(w);
Guido van Rossum5524a591995-01-10 15:26:20 +00001329 return 0;
1330 }
1331 if (v->ob_type->tp_as_number && v->ob_type->tp_as_number->nb_coerce) {
1332 res = (*v->ob_type->tp_as_number->nb_coerce)(pv, pw);
1333 if (res <= 0)
1334 return res;
1335 }
1336 if (w->ob_type->tp_as_number && w->ob_type->tp_as_number->nb_coerce) {
1337 res = (*w->ob_type->tp_as_number->nb_coerce)(pw, pv);
1338 if (res <= 0)
1339 return res;
1340 }
Guido van Rossum242c6421997-11-19 16:03:17 +00001341 return 1;
1342}
1343
Guido van Rossume797ec12001-01-17 15:24:28 +00001344/* Coerce two numeric types to the "larger" one.
1345 Increment the reference count on each argument.
1346 Return -1 and raise an exception if no coercion is possible
1347 (and then no reference count is incremented).
1348*/
Guido van Rossum242c6421997-11-19 16:03:17 +00001349int
Fred Drake100814d2000-07-09 15:48:49 +00001350PyNumber_Coerce(PyObject **pv, PyObject **pw)
Guido van Rossum242c6421997-11-19 16:03:17 +00001351{
1352 int err = PyNumber_CoerceEx(pv, pw);
1353 if (err <= 0)
1354 return err;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001355 PyErr_SetString(PyExc_TypeError, "number coercion failed");
Guido van Rossum5524a591995-01-10 15:26:20 +00001356 return -1;
1357}
1358
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001359
Guido van Rossum49b11fe1995-01-26 00:38:22 +00001360/* Test whether an object can be called */
1361
1362int
Fred Drake100814d2000-07-09 15:48:49 +00001363PyCallable_Check(PyObject *x)
Guido van Rossum49b11fe1995-01-26 00:38:22 +00001364{
1365 if (x == NULL)
1366 return 0;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001367 if (PyInstance_Check(x)) {
1368 PyObject *call = PyObject_GetAttrString(x, "__call__");
Guido van Rossum49b11fe1995-01-26 00:38:22 +00001369 if (call == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001370 PyErr_Clear();
Guido van Rossum49b11fe1995-01-26 00:38:22 +00001371 return 0;
1372 }
1373 /* Could test recursively but don't, for fear of endless
1374 recursion if some joker sets self.__call__ = self */
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001375 Py_DECREF(call);
Guido van Rossum49b11fe1995-01-26 00:38:22 +00001376 return 1;
1377 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001378 else {
1379 return x->ob_type->tp_call != NULL;
1380 }
Guido van Rossum49b11fe1995-01-26 00:38:22 +00001381}
1382
1383
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001384/*
1385NoObject is usable as a non-NULL undefined value, used by the macro None.
1386There is (and should be!) no way to create other objects of this type,
Guido van Rossum3f5da241990-12-20 15:06:42 +00001387so there is exactly one (which is indestructible, by the way).
Guido van Rossumba21a492001-08-16 08:17:26 +00001388(XXX This type and the type of NotImplemented below should be unified.)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001389*/
1390
Guido van Rossum0c182a11992-03-27 17:26:13 +00001391/* ARGSUSED */
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001392static PyObject *
Fred Drake100814d2000-07-09 15:48:49 +00001393none_repr(PyObject *op)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001394{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001395 return PyString_FromString("None");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001396}
1397
Barry Warsaw9bf16442001-01-23 16:24:35 +00001398/* ARGUSED */
1399static void
1400none_dealloc(PyObject* ignore)
1401{
1402 /* This should never get called, but we also don't want to SEGV if
1403 * we accidently decref None out of existance.
1404 */
1405 abort();
1406}
1407
1408
Guido van Rossumba21a492001-08-16 08:17:26 +00001409static PyTypeObject PyNone_Type = {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001410 PyObject_HEAD_INIT(&PyType_Type)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001411 0,
Guido van Rossumba21a492001-08-16 08:17:26 +00001412 "NoneType",
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001413 0,
1414 0,
Barry Warsaw9bf16442001-01-23 16:24:35 +00001415 (destructor)none_dealloc, /*tp_dealloc*/ /*never called*/
Guido van Rossum7066dd71992-09-17 17:54:56 +00001416 0, /*tp_print*/
Guido van Rossum3f5da241990-12-20 15:06:42 +00001417 0, /*tp_getattr*/
1418 0, /*tp_setattr*/
1419 0, /*tp_compare*/
Guido van Rossum1d5735e1994-08-30 08:27:36 +00001420 (reprfunc)none_repr, /*tp_repr*/
Guido van Rossum3f5da241990-12-20 15:06:42 +00001421 0, /*tp_as_number*/
1422 0, /*tp_as_sequence*/
1423 0, /*tp_as_mapping*/
Guido van Rossum9bfef441993-03-29 10:43:31 +00001424 0, /*tp_hash */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001425};
1426
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001427PyObject _Py_NoneStruct = {
Guido van Rossumba21a492001-08-16 08:17:26 +00001428 PyObject_HEAD_INIT(&PyNone_Type)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001429};
1430
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +00001431/* NotImplemented is an object that can be used to signal that an
1432 operation is not implemented for the given type combination. */
1433
1434static PyObject *
1435NotImplemented_repr(PyObject *op)
1436{
1437 return PyString_FromString("NotImplemented");
1438}
1439
1440static PyTypeObject PyNotImplemented_Type = {
1441 PyObject_HEAD_INIT(&PyType_Type)
1442 0,
Guido van Rossumba21a492001-08-16 08:17:26 +00001443 "NotImplementedType",
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +00001444 0,
1445 0,
Barry Warsaw9bf16442001-01-23 16:24:35 +00001446 (destructor)none_dealloc, /*tp_dealloc*/ /*never called*/
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +00001447 0, /*tp_print*/
1448 0, /*tp_getattr*/
1449 0, /*tp_setattr*/
1450 0, /*tp_compare*/
1451 (reprfunc)NotImplemented_repr, /*tp_repr*/
1452 0, /*tp_as_number*/
1453 0, /*tp_as_sequence*/
1454 0, /*tp_as_mapping*/
1455 0, /*tp_hash */
1456};
1457
1458PyObject _Py_NotImplementedStruct = {
1459 PyObject_HEAD_INIT(&PyNotImplemented_Type)
1460};
1461
Guido van Rossumba21a492001-08-16 08:17:26 +00001462void
1463_Py_ReadyTypes(void)
1464{
1465 if (PyType_Ready(&PyType_Type) < 0)
1466 Py_FatalError("Can't initialize 'type'");
1467
1468 if (PyType_Ready(&PyList_Type) < 0)
1469 Py_FatalError("Can't initialize 'list'");
1470
1471 if (PyType_Ready(&PyNone_Type) < 0)
1472 Py_FatalError("Can't initialize type(None)");
1473
1474 if (PyType_Ready(&PyNotImplemented_Type) < 0)
1475 Py_FatalError("Can't initialize type(NotImplemented)");
1476}
1477
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001478
Guido van Rossum84a90321996-05-22 16:34:47 +00001479#ifdef Py_TRACE_REFS
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001480
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001481static PyObject refchain = {&refchain, &refchain};
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001482
Guido van Rossumaacdc9d1996-08-12 21:32:12 +00001483void
Fred Drake100814d2000-07-09 15:48:49 +00001484_Py_ResetReferences(void)
Guido van Rossume09fb551997-08-05 02:04:34 +00001485{
1486 refchain._ob_prev = refchain._ob_next = &refchain;
1487 _Py_RefTotal = 0;
1488}
1489
1490void
Fred Drake100814d2000-07-09 15:48:49 +00001491_Py_NewReference(PyObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001492{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001493 _Py_RefTotal++;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001494 op->ob_refcnt = 1;
1495 op->_ob_next = refchain._ob_next;
1496 op->_ob_prev = &refchain;
1497 refchain._ob_next->_ob_prev = op;
1498 refchain._ob_next = op;
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +00001499#ifdef COUNT_ALLOCS
1500 inc_count(op->ob_type);
1501#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001502}
1503
Guido van Rossumaacdc9d1996-08-12 21:32:12 +00001504void
Fred Drake100814d2000-07-09 15:48:49 +00001505_Py_ForgetReference(register PyObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001506{
Guido van Rossumbffd6832000-01-20 22:32:56 +00001507#ifdef SLOW_UNREF_CHECK
Guido van Rossum4c08d552000-03-10 22:55:18 +00001508 register PyObject *p;
Guido van Rossumbffd6832000-01-20 22:32:56 +00001509#endif
Guido van Rossumd7047b31995-01-02 19:07:15 +00001510 if (op->ob_refcnt < 0)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001511 Py_FatalError("UNREF negative refcnt");
Guido van Rossum2e8f6141992-09-03 20:32:55 +00001512 if (op == &refchain ||
Guido van Rossumd7047b31995-01-02 19:07:15 +00001513 op->_ob_prev->_ob_next != op || op->_ob_next->_ob_prev != op)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001514 Py_FatalError("UNREF invalid object");
Guido van Rossum2e8f6141992-09-03 20:32:55 +00001515#ifdef SLOW_UNREF_CHECK
Guido van Rossum3f5da241990-12-20 15:06:42 +00001516 for (p = refchain._ob_next; p != &refchain; p = p->_ob_next) {
1517 if (p == op)
1518 break;
1519 }
Guido van Rossumd7047b31995-01-02 19:07:15 +00001520 if (p == &refchain) /* Not found */
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001521 Py_FatalError("UNREF unknown object");
Guido van Rossum2e8f6141992-09-03 20:32:55 +00001522#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001523 op->_ob_next->_ob_prev = op->_ob_prev;
1524 op->_ob_prev->_ob_next = op->_ob_next;
Guido van Rossum2e8f6141992-09-03 20:32:55 +00001525 op->_ob_next = op->_ob_prev = NULL;
Guido van Rossumd8953cb1995-04-06 14:46:26 +00001526#ifdef COUNT_ALLOCS
Tim Peters6d6c1a32001-08-02 04:15:00 +00001527 op->ob_type->tp_frees++;
Guido van Rossumd8953cb1995-04-06 14:46:26 +00001528#endif
Guido van Rossum3f5da241990-12-20 15:06:42 +00001529}
1530
Guido van Rossumaacdc9d1996-08-12 21:32:12 +00001531void
Fred Drake100814d2000-07-09 15:48:49 +00001532_Py_Dealloc(PyObject *op)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001533{
Guido van Rossum9776adf1994-09-07 14:36:45 +00001534 destructor dealloc = op->ob_type->tp_dealloc;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001535 _Py_ForgetReference(op);
Guido van Rossum9776adf1994-09-07 14:36:45 +00001536 (*dealloc)(op);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001537}
1538
Guido van Rossumaacdc9d1996-08-12 21:32:12 +00001539void
Fred Drake100814d2000-07-09 15:48:49 +00001540_Py_PrintReferences(FILE *fp)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001541{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001542 PyObject *op;
Guido van Rossume09fb551997-08-05 02:04:34 +00001543 fprintf(fp, "Remaining objects:\n");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001544 for (op = refchain._ob_next; op != &refchain; op = op->_ob_next) {
1545 fprintf(fp, "[%d] ", op->ob_refcnt);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001546 if (PyObject_Print(op, fp, 0) != 0)
1547 PyErr_Clear();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001548 putc('\n', fp);
1549 }
1550}
1551
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +00001552PyObject *
Fred Drake100814d2000-07-09 15:48:49 +00001553_Py_GetObjects(PyObject *self, PyObject *args)
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +00001554{
1555 int i, n;
1556 PyObject *t = NULL;
1557 PyObject *res, *op;
1558
1559 if (!PyArg_ParseTuple(args, "i|O", &n, &t))
1560 return NULL;
1561 op = refchain._ob_next;
1562 res = PyList_New(0);
1563 if (res == NULL)
1564 return NULL;
1565 for (i = 0; (n == 0 || i < n) && op != &refchain; i++) {
1566 while (op == self || op == args || op == res || op == t ||
Guido van Rossum3c296022001-07-14 17:58:00 +00001567 (t != NULL && op->ob_type != (PyTypeObject *) t)) {
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +00001568 op = op->_ob_next;
1569 if (op == &refchain)
1570 return res;
1571 }
1572 if (PyList_Append(res, op) < 0) {
1573 Py_DECREF(res);
1574 return NULL;
1575 }
1576 op = op->_ob_next;
1577 }
1578 return res;
1579}
1580
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001581#endif
Guido van Rossum97ead3f1996-01-12 01:24:09 +00001582
1583
1584/* Hack to force loading of cobject.o */
Guido van Rossumda9c2711996-12-05 21:58:58 +00001585PyTypeObject *_Py_cobject_hack = &PyCObject_Type;
Guido van Rossum84a90321996-05-22 16:34:47 +00001586
1587
1588/* Hack to force loading of abstract.o */
Jeremy Hylton6253f832000-07-12 12:56:19 +00001589int (*_Py_abstract_hack)(PyObject *) = &PyObject_Size;
Guido van Rossume09fb551997-08-05 02:04:34 +00001590
1591
Andrew M. Kuchling1582a3a2000-08-16 12:27:23 +00001592/* Python's malloc wrappers (see pymem.h) */
Guido van Rossume09fb551997-08-05 02:04:34 +00001593
Thomas Wouters334fb892000-07-25 12:56:38 +00001594void *
Fred Drake100814d2000-07-09 15:48:49 +00001595PyMem_Malloc(size_t nbytes)
Guido van Rossume09fb551997-08-05 02:04:34 +00001596{
1597#if _PyMem_EXTRA > 0
1598 if (nbytes == 0)
1599 nbytes = _PyMem_EXTRA;
1600#endif
Guido van Rossumb18618d2000-05-03 23:44:39 +00001601 return PyMem_MALLOC(nbytes);
Guido van Rossume09fb551997-08-05 02:04:34 +00001602}
1603
Thomas Wouters334fb892000-07-25 12:56:38 +00001604void *
1605PyMem_Realloc(void *p, size_t nbytes)
Guido van Rossume09fb551997-08-05 02:04:34 +00001606{
1607#if _PyMem_EXTRA > 0
1608 if (nbytes == 0)
1609 nbytes = _PyMem_EXTRA;
1610#endif
Guido van Rossumb18618d2000-05-03 23:44:39 +00001611 return PyMem_REALLOC(p, nbytes);
Guido van Rossume09fb551997-08-05 02:04:34 +00001612}
1613
1614void
Thomas Wouters334fb892000-07-25 12:56:38 +00001615PyMem_Free(void *p)
Guido van Rossume09fb551997-08-05 02:04:34 +00001616{
Guido van Rossumb18618d2000-05-03 23:44:39 +00001617 PyMem_FREE(p);
1618}
1619
1620
1621/* Python's object malloc wrappers (see objimpl.h) */
1622
Thomas Wouters334fb892000-07-25 12:56:38 +00001623void *
Fred Drake100814d2000-07-09 15:48:49 +00001624PyObject_Malloc(size_t nbytes)
Guido van Rossumb18618d2000-05-03 23:44:39 +00001625{
1626 return PyObject_MALLOC(nbytes);
1627}
1628
Thomas Wouters334fb892000-07-25 12:56:38 +00001629void *
1630PyObject_Realloc(void *p, size_t nbytes)
Guido van Rossumb18618d2000-05-03 23:44:39 +00001631{
1632 return PyObject_REALLOC(p, nbytes);
1633}
1634
1635void
Thomas Wouters334fb892000-07-25 12:56:38 +00001636PyObject_Free(void *p)
Guido van Rossumb18618d2000-05-03 23:44:39 +00001637{
1638 PyObject_FREE(p);
Guido van Rossume09fb551997-08-05 02:04:34 +00001639}
Guido van Rossum86610361998-04-10 22:32:46 +00001640
1641
Fred Drake41deb1e2001-02-01 05:27:45 +00001642/* Hook to clear up weak references only once the _weakref module is
1643 imported. We use a dummy implementation to simplify the code at each
1644 call site instead of requiring a test for NULL.
1645*/
1646
Fred Drakeb60654b2001-02-26 18:56:37 +00001647static void
Fred Drake41deb1e2001-02-01 05:27:45 +00001648empty_clear_weak_refs(PyObject *o)
1649{
Fred Drakeb60654b2001-02-26 18:56:37 +00001650 return;
Fred Drake41deb1e2001-02-01 05:27:45 +00001651}
1652
Fred Drakeb60654b2001-02-26 18:56:37 +00001653void (*PyObject_ClearWeakRefs)(PyObject *) = empty_clear_weak_refs;
Fred Drake41deb1e2001-02-01 05:27:45 +00001654
1655
1656
Guido van Rossum86610361998-04-10 22:32:46 +00001657/* These methods are used to control infinite recursion in repr, str, print,
1658 etc. Container objects that may recursively contain themselves,
1659 e.g. builtin dictionaries and lists, should used Py_ReprEnter() and
1660 Py_ReprLeave() to avoid infinite recursion.
1661
1662 Py_ReprEnter() returns 0 the first time it is called for a particular
1663 object and 1 every time thereafter. It returns -1 if an exception
1664 occurred. Py_ReprLeave() has no return value.
1665
1666 See dictobject.c and listobject.c for examples of use.
1667*/
1668
1669#define KEY "Py_Repr"
1670
1671int
Fred Drake100814d2000-07-09 15:48:49 +00001672Py_ReprEnter(PyObject *obj)
Guido van Rossum86610361998-04-10 22:32:46 +00001673{
1674 PyObject *dict;
1675 PyObject *list;
1676 int i;
1677
1678 dict = PyThreadState_GetDict();
1679 if (dict == NULL)
1680 return -1;
1681 list = PyDict_GetItemString(dict, KEY);
1682 if (list == NULL) {
1683 list = PyList_New(0);
1684 if (list == NULL)
1685 return -1;
1686 if (PyDict_SetItemString(dict, KEY, list) < 0)
1687 return -1;
1688 Py_DECREF(list);
1689 }
1690 i = PyList_GET_SIZE(list);
1691 while (--i >= 0) {
1692 if (PyList_GET_ITEM(list, i) == obj)
1693 return 1;
1694 }
1695 PyList_Append(list, obj);
1696 return 0;
1697}
1698
1699void
Fred Drake100814d2000-07-09 15:48:49 +00001700Py_ReprLeave(PyObject *obj)
Guido van Rossum86610361998-04-10 22:32:46 +00001701{
1702 PyObject *dict;
1703 PyObject *list;
1704 int i;
1705
1706 dict = PyThreadState_GetDict();
Guido van Rossumeb909461998-04-11 15:17:34 +00001707 if (dict == NULL)
1708 return;
Guido van Rossum86610361998-04-10 22:32:46 +00001709 list = PyDict_GetItemString(dict, KEY);
Guido van Rossumeb909461998-04-11 15:17:34 +00001710 if (list == NULL || !PyList_Check(list))
1711 return;
Guido van Rossum86610361998-04-10 22:32:46 +00001712 i = PyList_GET_SIZE(list);
1713 /* Count backwards because we always expect obj to be list[-1] */
1714 while (--i >= 0) {
1715 if (PyList_GET_ITEM(list, i) == obj) {
1716 PyList_SetSlice(list, i, i + 1, NULL);
1717 break;
1718 }
1719 }
1720}
Guido van Rossumd724b232000-03-13 16:01:29 +00001721
1722/*
1723 trashcan
1724 CT 2k0130
1725 non-recursively destroy nested objects
1726
1727 CT 2k0223
1728 everything is now done in a macro.
1729
1730 CT 2k0305
1731 modified to use functions, after Tim Peter's suggestion.
1732
1733 CT 2k0309
1734 modified to restore a possible error.
Guido van Rossum13ff8eb2000-03-25 18:39:19 +00001735
1736 CT 2k0325
1737 added better safe than sorry check for threadstate
Guido van Rossume92e6102000-04-24 15:40:53 +00001738
1739 CT 2k0422
1740 complete rewrite. We now build a chain via ob_type
1741 and save the limited number of types in ob_refcnt.
1742 This is perfect since we don't need any memory.
1743 A patch for free-threading would need just a lock.
Guido van Rossumd724b232000-03-13 16:01:29 +00001744*/
1745
Guido van Rossume92e6102000-04-24 15:40:53 +00001746#define Py_TRASHCAN_TUPLE 1
1747#define Py_TRASHCAN_LIST 2
1748#define Py_TRASHCAN_DICT 3
1749#define Py_TRASHCAN_FRAME 4
1750#define Py_TRASHCAN_TRACEBACK 5
1751/* extend here if other objects want protection */
1752
Guido van Rossumd724b232000-03-13 16:01:29 +00001753int _PyTrash_delete_nesting = 0;
Guido van Rossume92e6102000-04-24 15:40:53 +00001754
Guido van Rossumd724b232000-03-13 16:01:29 +00001755PyObject * _PyTrash_delete_later = NULL;
1756
1757void
Fred Drake100814d2000-07-09 15:48:49 +00001758_PyTrash_deposit_object(PyObject *op)
Guido van Rossumd724b232000-03-13 16:01:29 +00001759{
Guido van Rossume92e6102000-04-24 15:40:53 +00001760 int typecode;
Guido van Rossum13ff8eb2000-03-25 18:39:19 +00001761
Guido van Rossume92e6102000-04-24 15:40:53 +00001762 if (PyTuple_Check(op))
1763 typecode = Py_TRASHCAN_TUPLE;
1764 else if (PyList_Check(op))
1765 typecode = Py_TRASHCAN_LIST;
1766 else if (PyDict_Check(op))
1767 typecode = Py_TRASHCAN_DICT;
1768 else if (PyFrame_Check(op))
1769 typecode = Py_TRASHCAN_FRAME;
1770 else if (PyTraceBack_Check(op))
1771 typecode = Py_TRASHCAN_TRACEBACK;
Moshe Zadkacf703f02000-08-04 15:36:13 +00001772 else /* We have a bug here -- those are the only types in GC */ {
1773 Py_FatalError("Type not supported in GC -- internal bug");
1774 return; /* pacify compiler -- execution never here */
1775 }
Guido van Rossume92e6102000-04-24 15:40:53 +00001776 op->ob_refcnt = typecode;
Guido van Rossumd724b232000-03-13 16:01:29 +00001777
Guido van Rossume92e6102000-04-24 15:40:53 +00001778 op->ob_type = (PyTypeObject*)_PyTrash_delete_later;
1779 _PyTrash_delete_later = op;
Guido van Rossumd724b232000-03-13 16:01:29 +00001780}
1781
1782void
Fred Drake100814d2000-07-09 15:48:49 +00001783_PyTrash_destroy_chain(void)
Guido van Rossumd724b232000-03-13 16:01:29 +00001784{
1785 while (_PyTrash_delete_later) {
1786 PyObject *shredder = _PyTrash_delete_later;
Guido van Rossume92e6102000-04-24 15:40:53 +00001787 _PyTrash_delete_later = (PyObject*) shredder->ob_type;
1788
1789 switch (shredder->ob_refcnt) {
1790 case Py_TRASHCAN_TUPLE:
1791 shredder->ob_type = &PyTuple_Type;
1792 break;
1793 case Py_TRASHCAN_LIST:
1794 shredder->ob_type = &PyList_Type;
1795 break;
1796 case Py_TRASHCAN_DICT:
1797 shredder->ob_type = &PyDict_Type;
1798 break;
1799 case Py_TRASHCAN_FRAME:
1800 shredder->ob_type = &PyFrame_Type;
1801 break;
1802 case Py_TRASHCAN_TRACEBACK:
1803 shredder->ob_type = &PyTraceBack_Type;
1804 break;
1805 }
1806 _Py_NewReference(shredder);
1807
Guido van Rossumd724b232000-03-13 16:01:29 +00001808 ++_PyTrash_delete_nesting;
1809 Py_DECREF(shredder);
1810 --_PyTrash_delete_nesting;
1811 }
1812}
Neil Schemenauera35c6882001-02-27 04:45:05 +00001813
1814#ifdef WITH_PYMALLOC
1815#include "obmalloc.c"
1816#endif