blob: fea9ee507c75f97ac9696e1e936eb2393a00c82f [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
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000225PyObject *
Fred Drake100814d2000-07-09 15:48:49 +0000226PyObject_Repr(PyObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000227{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000228 if (PyErr_CheckSignals())
Guido van Rossum90933611991-06-07 16:10:43 +0000229 return NULL;
Guido van Rossum9b00dfa1998-04-28 16:06:54 +0000230#ifdef USE_STACKCHECK
231 if (PyOS_CheckStack()) {
Fred Drake661ea262000-10-24 19:57:45 +0000232 PyErr_SetString(PyExc_MemoryError, "stack overflow");
Guido van Rossum9b00dfa1998-04-28 16:06:54 +0000233 return NULL;
234 }
235#endif
Guido van Rossum90933611991-06-07 16:10:43 +0000236 if (v == NULL)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000237 return PyString_FromString("<NULL>");
Guido van Rossum90933611991-06-07 16:10:43 +0000238 else if (v->ob_type->tp_repr == NULL) {
239 char buf[120];
Fred Drakea44d3532000-06-30 15:01:00 +0000240 sprintf(buf, "<%.80s object at %p>",
241 v->ob_type->tp_name, v);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000242 return PyString_FromString(buf);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000243 }
Guido van Rossum4c08d552000-03-10 22:55:18 +0000244 else {
245 PyObject *res;
246 res = (*v->ob_type->tp_repr)(v);
247 if (res == NULL)
248 return NULL;
Fredrik Lundhefecc7d2000-07-01 14:31:09 +0000249 if (PyUnicode_Check(res)) {
250 PyObject* str;
Fredrik Lundh2a1e0602000-07-08 17:43:32 +0000251 str = PyUnicode_AsUnicodeEscapeString(res);
Marc-André Lemburg891bc652000-07-03 09:57:53 +0000252 Py_DECREF(res);
253 if (str)
Fredrik Lundhefecc7d2000-07-01 14:31:09 +0000254 res = str;
Marc-André Lemburg891bc652000-07-03 09:57:53 +0000255 else
256 return NULL;
Fredrik Lundhefecc7d2000-07-01 14:31:09 +0000257 }
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;
Marc-André Lemburg891bc652000-07-03 09:57:53 +0000286 if (PyUnicode_Check(res)) {
287 PyObject* str;
288 str = PyUnicode_AsEncodedString(res, NULL, NULL);
289 Py_DECREF(res);
290 if (str)
291 res = str;
292 else
293 return NULL;
294 }
Guido van Rossum4c08d552000-03-10 22:55:18 +0000295 if (!PyString_Check(res)) {
296 PyErr_Format(PyExc_TypeError,
Guido van Rossum5db862d2000-04-10 12:46:51 +0000297 "__str__ returned non-string (type %.200s)",
Guido van Rossum4c08d552000-03-10 22:55:18 +0000298 res->ob_type->tp_name);
299 Py_DECREF(res);
300 return NULL;
301 }
302 return res;
Guido van Rossumc6004111993-11-05 10:22:19 +0000303}
304
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000305PyObject *
306PyObject_Unicode(PyObject *v)
307{
308 PyObject *res;
309
310 if (v == NULL)
311 res = PyString_FromString("<NULL>");
312 else if (PyUnicode_Check(v)) {
313 Py_INCREF(v);
314 return v;
315 }
Marc-André Lemburgae605342001-03-25 19:16:13 +0000316 else if (PyString_Check(v)) {
317 Py_INCREF(v);
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000318 res = v;
Marc-André Lemburgae605342001-03-25 19:16:13 +0000319 }
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000320 else if (v->ob_type->tp_str != NULL)
321 res = (*v->ob_type->tp_str)(v);
322 else {
323 PyObject *func;
324 static PyObject *strstr;
325 if (strstr == NULL) {
326 strstr= PyString_InternFromString("__str__");
327 if (strstr == NULL)
328 return NULL;
329 }
330 if (!PyInstance_Check(v) ||
331 (func = PyObject_GetAttr(v, strstr)) == NULL) {
332 PyErr_Clear();
333 res = PyObject_Repr(v);
334 }
335 else {
336 res = PyEval_CallObject(func, (PyObject *)NULL);
337 Py_DECREF(func);
338 }
339 }
340 if (res == NULL)
341 return NULL;
342 if (!PyUnicode_Check(res)) {
343 PyObject* str;
344 str = PyUnicode_FromObject(res);
345 Py_DECREF(res);
346 if (str)
347 res = str;
348 else
349 return NULL;
350 }
351 return res;
352}
353
354
Guido van Rossumd1f06b92001-01-24 22:14:43 +0000355/* Macro to get the tp_richcompare field of a type if defined */
356#define RICHCOMPARE(t) (PyType_HasFeature((t), Py_TPFLAGS_HAVE_RICHCOMPARE) \
357 ? (t)->tp_richcompare : NULL)
358
Guido van Rossume797ec12001-01-17 15:24:28 +0000359/* Map rich comparison operators to their swapped version, e.g. LT --> GT */
360static int swapped_op[] = {Py_GT, Py_GE, Py_EQ, Py_NE, Py_LT, Py_LE};
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +0000361
Guido van Rossume797ec12001-01-17 15:24:28 +0000362/* Try a genuine rich comparison, returning an object. Return:
363 NULL for exception;
364 NotImplemented if this particular rich comparison is not implemented or
365 undefined;
366 some object not equal to NotImplemented if it is implemented
367 (this latter object may not be a Boolean).
368*/
369static PyObject *
370try_rich_compare(PyObject *v, PyObject *w, int op)
371{
372 richcmpfunc f;
373 PyObject *res;
374
Guido van Rossumd1f06b92001-01-24 22:14:43 +0000375 if ((f = RICHCOMPARE(v->ob_type)) != NULL) {
Guido van Rossume797ec12001-01-17 15:24:28 +0000376 res = (*f)(v, w, op);
377 if (res != Py_NotImplemented)
378 return res;
379 Py_DECREF(res);
380 }
Guido van Rossumd1f06b92001-01-24 22:14:43 +0000381 if ((f = RICHCOMPARE(w->ob_type)) != NULL) {
Guido van Rossume797ec12001-01-17 15:24:28 +0000382 return (*f)(w, v, swapped_op[op]);
383 }
384 res = Py_NotImplemented;
385 Py_INCREF(res);
386 return res;
387}
388
389/* Try a genuine rich comparison, returning an int. Return:
390 -1 for exception (including the case where try_rich_compare() returns an
391 object that's not a Boolean);
392 0 if the outcome is false;
393 1 if the outcome is true;
394 2 if this particular rich comparison is not implemented or undefined.
395*/
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +0000396static int
Guido van Rossume797ec12001-01-17 15:24:28 +0000397try_rich_compare_bool(PyObject *v, PyObject *w, int op)
398{
399 PyObject *res;
400 int ok;
401
Guido van Rossumd1f06b92001-01-24 22:14:43 +0000402 if (RICHCOMPARE(v->ob_type) == NULL && RICHCOMPARE(w->ob_type) == NULL)
Guido van Rossume797ec12001-01-17 15:24:28 +0000403 return 2; /* Shortcut, avoid INCREF+DECREF */
404 res = try_rich_compare(v, w, op);
405 if (res == NULL)
406 return -1;
407 if (res == Py_NotImplemented) {
408 Py_DECREF(res);
409 return 2;
410 }
411 ok = PyObject_IsTrue(res);
412 Py_DECREF(res);
413 return ok;
414}
415
416/* Try rich comparisons to determine a 3-way comparison. Return:
417 -2 for an exception;
418 -1 if v < w;
419 0 if v == w;
420 1 if v > w;
421 2 if this particular rich comparison is not implemented or undefined.
422*/
423static int
424try_rich_to_3way_compare(PyObject *v, PyObject *w)
425{
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000426 static struct { int op; int outcome; } tries[3] = {
427 /* Try this operator, and if it is true, use this outcome: */
428 {Py_EQ, 0},
429 {Py_LT, -1},
430 {Py_GT, 1},
431 };
432 int i;
433
Guido van Rossumd1f06b92001-01-24 22:14:43 +0000434 if (RICHCOMPARE(v->ob_type) == NULL && RICHCOMPARE(w->ob_type) == NULL)
Guido van Rossume797ec12001-01-17 15:24:28 +0000435 return 2; /* Shortcut */
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000436
437 for (i = 0; i < 3; i++) {
438 switch (try_rich_compare_bool(v, w, tries[i].op)) {
439 case -1:
Tim Peters6d60b2e2001-05-07 20:53:51 +0000440 return -2;
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000441 case 1:
442 return tries[i].outcome;
443 }
Guido van Rossume797ec12001-01-17 15:24:28 +0000444 }
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000445
446 return 2;
Guido van Rossume797ec12001-01-17 15:24:28 +0000447}
448
449/* Try a 3-way comparison, returning an int. Return:
450 -2 for an exception;
451 -1 if v < w;
452 0 if v == w;
453 1 if v > w;
454 2 if this particular 3-way comparison is not implemented or undefined.
455*/
456static int
457try_3way_compare(PyObject *v, PyObject *w)
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +0000458{
459 int c;
Guido van Rossume797ec12001-01-17 15:24:28 +0000460 cmpfunc f;
461
462 /* Comparisons involving instances are given to instance_compare,
463 which has the same return conventions as this function. */
464
465 if (PyInstance_Check(v))
466 return (*v->ob_type->tp_compare)(v, w);
467 if (PyInstance_Check(w))
468 return (*w->ob_type->tp_compare)(v, w);
469
Guido van Rossume797ec12001-01-17 15:24:28 +0000470 /* Try coercion; if it fails, give up */
471 c = PyNumber_CoerceEx(&v, &w);
472 if (c < 0)
473 return -2;
474 if (c > 0)
475 return 2;
476
477 /* Try v's comparison, if defined */
478 if ((f = v->ob_type->tp_compare) != NULL) {
479 c = (*f)(v, w);
480 Py_DECREF(v);
481 Py_DECREF(w);
Martin v. Löwis0163d6d2001-06-09 07:34:05 +0000482 if (c < 0 && PyErr_Occurred())
Guido van Rossume797ec12001-01-17 15:24:28 +0000483 return -2;
484 return c < 0 ? -1 : c > 0 ? 1 : 0;
485 }
486
487 /* Try w's comparison, if defined */
488 if ((f = w->ob_type->tp_compare) != NULL) {
489 c = (*f)(w, v); /* swapped! */
490 Py_DECREF(v);
491 Py_DECREF(w);
Martin v. Löwis0163d6d2001-06-09 07:34:05 +0000492 if (c < 0 && PyErr_Occurred())
Guido van Rossume797ec12001-01-17 15:24:28 +0000493 return -2;
494 return c < 0 ? 1 : c > 0 ? -1 : 0; /* negated! */
495 }
496
497 /* No comparison defined */
498 Py_DECREF(v);
499 Py_DECREF(w);
500 return 2;
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +0000501}
502
Guido van Rossume797ec12001-01-17 15:24:28 +0000503/* Final fallback 3-way comparison, returning an int. Return:
504 -2 if an error occurred;
505 -1 if v < w;
506 0 if v == w;
507 1 if v > w.
508*/
509static int
510default_3way_compare(PyObject *v, PyObject *w)
511{
512 int c;
Guido van Rossum8f9143d2001-01-22 15:59:32 +0000513 char *vname, *wname;
Guido van Rossume797ec12001-01-17 15:24:28 +0000514
515 if (v->ob_type == w->ob_type) {
Barry Warsawb0e754d2001-01-20 06:24:55 +0000516 /* When comparing these pointers, they must be cast to
517 * integer types (i.e. Py_uintptr_t, our spelling of C9X's
518 * uintptr_t). ANSI specifies that pointer compares other
519 * than == and != to non-related structures are undefined.
520 */
Barry Warsaw71ff8d52001-01-20 06:08:10 +0000521 Py_uintptr_t vv = (Py_uintptr_t)v;
522 Py_uintptr_t ww = (Py_uintptr_t)w;
Guido van Rossume797ec12001-01-17 15:24:28 +0000523 return (vv < ww) ? -1 : (vv > ww) ? 1 : 0;
524 }
525
526 /* Special case for Unicode */
527 if (PyUnicode_Check(v) || PyUnicode_Check(w)) {
528 c = PyUnicode_Compare(v, w);
529 if (!PyErr_Occurred())
530 return c;
531 /* TypeErrors are ignored: if Unicode coercion fails due
532 to one of the arguments not having the right type, we
533 continue as defined by the coercion protocol (see
534 above). Luckily, decoding errors are reported as
535 ValueErrors and are not masked by this technique. */
536 if (!PyErr_ExceptionMatches(PyExc_TypeError))
537 return -2;
538 PyErr_Clear();
539 }
540
Guido van Rossum0871e932001-01-22 19:28:09 +0000541 /* None is smaller than anything */
542 if (v == Py_None)
543 return -1;
544 if (w == Py_None)
545 return 1;
546
Guido van Rossume797ec12001-01-17 15:24:28 +0000547 /* different type: compare type names */
Guido van Rossum8f9143d2001-01-22 15:59:32 +0000548 if (v->ob_type->tp_as_number)
549 vname = "";
550 else
551 vname = v->ob_type->tp_name;
552 if (w->ob_type->tp_as_number)
553 wname = "";
554 else
555 wname = w->ob_type->tp_name;
556 c = strcmp(vname, wname);
557 if (c < 0)
558 return -1;
559 if (c > 0)
560 return 1;
561 /* Same type name, or (more likely) incomparable numeric types */
562 return ((Py_uintptr_t)(v->ob_type) < (
563 Py_uintptr_t)(w->ob_type)) ? -1 : 1;
Guido van Rossume797ec12001-01-17 15:24:28 +0000564}
565
566#define CHECK_TYPES(o) PyType_HasFeature((o)->ob_type, Py_TPFLAGS_CHECKTYPES)
567
Tim Peters6d60b2e2001-05-07 20:53:51 +0000568/* Do a 3-way comparison, by hook or by crook. Return:
569 -2 for an exception;
570 -1 if v < w;
571 0 if v == w;
572 1 if v > w;
Martin v. Löwis0163d6d2001-06-09 07:34:05 +0000573 If the object implements a tp_compare function, it returns
574 whatever this function returns (whether with an exception or not).
Tim Peters6d60b2e2001-05-07 20:53:51 +0000575*/
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +0000576static int
Fred Drake100814d2000-07-09 15:48:49 +0000577do_cmp(PyObject *v, PyObject *w)
Guido van Rossum20566841995-01-12 11:26:10 +0000578{
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +0000579 int c;
Martin v. Löwis0163d6d2001-06-09 07:34:05 +0000580 cmpfunc f;
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +0000581
Martin v. Löwis0163d6d2001-06-09 07:34:05 +0000582 if (v->ob_type == w->ob_type
Guido van Rossum82fc51c2001-08-16 08:02:45 +0000583 && (f = v->ob_type->tp_compare) != NULL) {
584 c = (*f)(v, w);
585 if (c != 2 || !PyInstance_Check(v))
586 return c;
587 }
Guido van Rossume797ec12001-01-17 15:24:28 +0000588 c = try_rich_to_3way_compare(v, w);
589 if (c < 2)
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +0000590 return c;
Guido van Rossume797ec12001-01-17 15:24:28 +0000591 c = try_3way_compare(v, w);
592 if (c < 2)
593 return c;
594 return default_3way_compare(v, w);
Guido van Rossum20566841995-01-12 11:26:10 +0000595}
596
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000597/* compare_nesting is incremented before calling compare (for
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000598 some types) and decremented on exit. If the count exceeds the
599 nesting limit, enable code to detect circular data structures.
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000600
601 This is a tunable parameter that should only affect the performance
602 of comparisons, nothing else. Setting it high makes comparing deeply
603 nested non-cyclical data structures faster, but makes comparing cyclical
604 data structures slower.
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000605*/
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000606#define NESTING_LIMIT 20
607
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000608static int compare_nesting = 0;
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000609
610static PyObject*
Fred Drake100814d2000-07-09 15:48:49 +0000611get_inprogress_dict(void)
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000612{
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000613 static PyObject *key;
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000614 PyObject *tstate_dict, *inprogress;
615
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000616 if (key == NULL) {
617 key = PyString_InternFromString("cmp_state");
618 if (key == NULL)
619 return NULL;
620 }
621
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000622 tstate_dict = PyThreadState_GetDict();
623 if (tstate_dict == NULL) {
624 PyErr_BadInternalCall();
625 return NULL;
626 }
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000627
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000628 inprogress = PyDict_GetItem(tstate_dict, key);
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000629 if (inprogress == NULL) {
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000630 inprogress = PyDict_New();
631 if (inprogress == NULL)
632 return NULL;
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000633 if (PyDict_SetItem(tstate_dict, key, inprogress) == -1) {
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000634 Py_DECREF(inprogress);
635 return NULL;
636 }
Jeremy Hyltona251ea02000-06-09 16:20:39 +0000637 Py_DECREF(inprogress);
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000638 }
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000639
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000640 return inprogress;
641}
642
643static PyObject *
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000644check_recursion(PyObject *v, PyObject *w, int op)
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000645{
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000646 PyObject *inprogress;
647 PyObject *token;
Barry Warsaw9d23a4e2000-08-18 05:01:19 +0000648 Py_uintptr_t iv = (Py_uintptr_t)v;
649 Py_uintptr_t iw = (Py_uintptr_t)w;
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000650 PyObject *x, *y, *z;
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000651
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000652 inprogress = get_inprogress_dict();
653 if (inprogress == NULL)
654 return NULL;
655
656 token = PyTuple_New(3);
657 if (token == NULL)
658 return NULL;
659
660 if (iv <= iw) {
661 PyTuple_SET_ITEM(token, 0, x = PyLong_FromVoidPtr((void *)v));
662 PyTuple_SET_ITEM(token, 1, y = PyLong_FromVoidPtr((void *)w));
663 if (op >= 0)
664 op = swapped_op[op];
665 } else {
666 PyTuple_SET_ITEM(token, 0, x = PyLong_FromVoidPtr((void *)w));
667 PyTuple_SET_ITEM(token, 1, y = PyLong_FromVoidPtr((void *)v));
668 }
669 PyTuple_SET_ITEM(token, 2, z = PyInt_FromLong((long)op));
670 if (x == NULL || y == NULL || z == NULL) {
671 Py_DECREF(token);
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000672 return NULL;
673 }
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000674
675 if (PyDict_GetItem(inprogress, token) != NULL) {
676 Py_DECREF(token);
677 return Py_None; /* Without INCREF! */
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000678 }
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000679
680 if (PyDict_SetItem(inprogress, token, token) < 0) {
681 Py_DECREF(token);
682 return NULL;
683 }
684
685 return token;
686}
687
688static void
689delete_token(PyObject *token)
690{
691 PyObject *inprogress;
692
693 if (token == NULL || token == Py_None)
694 return;
695 inprogress = get_inprogress_dict();
696 if (inprogress == NULL)
697 PyErr_Clear();
698 else
699 PyDict_DelItem(inprogress, token);
700 Py_DECREF(token);
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000701}
702
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000703int
Fred Drake100814d2000-07-09 15:48:49 +0000704PyObject_Compare(PyObject *v, PyObject *w)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000705{
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000706 PyTypeObject *vtp;
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000707 int result;
708
Jack Jansend49cbe12000-08-22 21:52:51 +0000709#if defined(USE_STACKCHECK)
Guido van Rossum9c0a99e2000-08-30 15:53:50 +0000710 if (PyOS_CheckStack()) {
Jack Jansend49cbe12000-08-22 21:52:51 +0000711 PyErr_SetString(PyExc_MemoryError, "Stack overflow");
712 return -1;
713 }
714#endif
Guido van Rossumc8b6df91997-05-23 00:06:51 +0000715 if (v == NULL || w == NULL) {
716 PyErr_BadInternalCall();
717 return -1;
718 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000719 if (v == w)
720 return 0;
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +0000721 vtp = v->ob_type;
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000722 compare_nesting++;
723 if (compare_nesting > NESTING_LIMIT &&
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +0000724 (vtp->tp_as_mapping
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000725 || (vtp->tp_as_sequence
726 && !PyString_Check(v)
727 && !PyTuple_Check(v)))) {
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +0000728 /* try to detect circular data structures */
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000729 PyObject *token = check_recursion(v, w, -1);
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000730
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000731 if (token == NULL) {
732 result = -1;
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000733 }
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000734 else if (token == Py_None) {
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000735 /* already comparing these objects. assume
736 they're equal until shown otherwise */
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +0000737 result = 0;
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000738 }
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000739 else {
740 result = do_cmp(v, w);
741 delete_token(token);
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000742 }
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000743 }
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +0000744 else {
745 result = do_cmp(v, w);
746 }
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000747 compare_nesting--;
Guido van Rossume797ec12001-01-17 15:24:28 +0000748 return result < 0 ? -1 : result;
749}
750
751static PyObject *
Martin v. Löwis0163d6d2001-06-09 07:34:05 +0000752convert_3way_to_object(int op, int c)
Guido van Rossume797ec12001-01-17 15:24:28 +0000753{
Guido van Rossume797ec12001-01-17 15:24:28 +0000754 PyObject *result;
Guido van Rossume797ec12001-01-17 15:24:28 +0000755 switch (op) {
756 case Py_LT: c = c < 0; break;
757 case Py_LE: c = c <= 0; break;
758 case Py_EQ: c = c == 0; break;
759 case Py_NE: c = c != 0; break;
760 case Py_GT: c = c > 0; break;
761 case Py_GE: c = c >= 0; break;
762 }
763 result = c ? Py_True : Py_False;
764 Py_INCREF(result);
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000765 return result;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000766}
Martin v. Löwis0163d6d2001-06-09 07:34:05 +0000767
768
769static PyObject *
770try_3way_to_rich_compare(PyObject *v, PyObject *w, int op)
771{
772 int c;
773
774 c = try_3way_compare(v, w);
775 if (c >= 2)
776 c = default_3way_compare(v, w);
777 if (c <= -2)
778 return NULL;
779 return convert_3way_to_object(op, c);
780}
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000781
Neil Schemenauerd38855c2001-01-21 16:25:18 +0000782static PyObject *
Guido van Rossume797ec12001-01-17 15:24:28 +0000783do_richcmp(PyObject *v, PyObject *w, int op)
784{
785 PyObject *res;
Martin v. Löwis0163d6d2001-06-09 07:34:05 +0000786 cmpfunc f;
787
788 /* If the types are equal, don't bother with coercions etc.
789 Instances are special-cased in try_3way_compare, since
790 a result of 2 does *not* mean one value being greater
791 than the other. */
792 if (v->ob_type == w->ob_type
793 && (f = v->ob_type->tp_compare) != NULL
794 && !PyInstance_Check(v)) {
795 int c;
796 richcmpfunc f1;
797 if ((f1 = RICHCOMPARE(v->ob_type)) != NULL) {
798 /* If the type has richcmp, try it first.
799 try_rich_compare would try it two-sided,
800 which is not needed since we've a single
801 type only. */
802 res = (*f1)(v, w, op);
803 if (res != Py_NotImplemented)
804 return res;
805 Py_DECREF(res);
806 }
807 c = (*f)(v, w);
808 if (c < 0 && PyErr_Occurred())
809 return NULL;
810 return convert_3way_to_object(op, c);
811 }
Guido van Rossume797ec12001-01-17 15:24:28 +0000812
813 res = try_rich_compare(v, w, op);
814 if (res != Py_NotImplemented)
815 return res;
816 Py_DECREF(res);
817
818 return try_3way_to_rich_compare(v, w, op);
819}
820
821PyObject *
822PyObject_RichCompare(PyObject *v, PyObject *w, int op)
823{
824 PyObject *res;
825
826 assert(Py_LT <= op && op <= Py_GE);
827
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000828 compare_nesting++;
829 if (compare_nesting > NESTING_LIMIT &&
830 (v->ob_type->tp_as_mapping
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000831 || (v->ob_type->tp_as_sequence
832 && !PyString_Check(v)
833 && !PyTuple_Check(v)))) {
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000834 /* try to detect circular data structures */
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000835 PyObject *token = check_recursion(v, w, op);
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000836
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000837 if (token == NULL) {
838 res = NULL;
Guido van Rossume797ec12001-01-17 15:24:28 +0000839 }
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000840 else if (token == Py_None) {
841 /* already comparing these objects with this operator.
842 assume they're equal until shown otherwise */
843 if (op == Py_EQ)
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000844 res = Py_True;
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000845 else if (op == Py_NE)
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000846 res = Py_False;
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000847 else {
848 PyErr_SetString(PyExc_ValueError,
849 "can't order recursive values");
850 res = NULL;
851 }
852 Py_XINCREF(res);
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000853 }
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000854 else {
855 res = do_richcmp(v, w, op);
856 delete_token(token);
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000857 }
Guido van Rossume797ec12001-01-17 15:24:28 +0000858 }
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000859 else {
860 res = do_richcmp(v, w, op);
861 }
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000862 compare_nesting--;
Guido van Rossume797ec12001-01-17 15:24:28 +0000863 return res;
864}
865
Tim Petersde9725f2001-05-05 10:06:17 +0000866/* Return -1 if error; 1 if v op w; 0 if not (v op w). */
Guido van Rossume797ec12001-01-17 15:24:28 +0000867int
868PyObject_RichCompareBool(PyObject *v, PyObject *w, int op)
869{
870 PyObject *res = PyObject_RichCompare(v, w, op);
871 int ok;
872
873 if (res == NULL)
874 return -1;
875 ok = PyObject_IsTrue(res);
876 Py_DECREF(res);
877 return ok;
878}
Fred Drake13634cf2000-06-29 19:17:04 +0000879
880/* Set of hash utility functions to help maintaining the invariant that
881 iff a==b then hash(a)==hash(b)
882
883 All the utility functions (_Py_Hash*()) return "-1" to signify an error.
884*/
885
886long
Fred Drake100814d2000-07-09 15:48:49 +0000887_Py_HashDouble(double v)
Fred Drake13634cf2000-06-29 19:17:04 +0000888{
Tim Peters39dce292000-08-15 03:34:48 +0000889 double intpart, fractpart;
890 int expo;
891 long hipart;
892 long x; /* the final hash value */
893 /* This is designed so that Python numbers of different types
894 * that compare equal hash to the same value; otherwise comparisons
895 * of mapping keys will turn out weird.
896 */
897
898#ifdef MPW /* MPW C modf expects pointer to extended as second argument */
899{
900 extended e;
901 fractpart = modf(v, &e);
902 intpart = e;
903}
904#else
905 fractpart = modf(v, &intpart);
906#endif
907 if (fractpart == 0.0) {
908 /* This must return the same hash as an equal int or long. */
909 if (intpart > LONG_MAX || -intpart > LONG_MAX) {
910 /* Convert to long and use its hash. */
911 PyObject *plong; /* converted to Python long */
912 if (Py_IS_INFINITY(intpart))
913 /* can't convert to long int -- arbitrary */
914 v = v < 0 ? -271828.0 : 314159.0;
915 plong = PyLong_FromDouble(v);
916 if (plong == NULL)
917 return -1;
918 x = PyObject_Hash(plong);
919 Py_DECREF(plong);
920 return x;
921 }
922 /* Fits in a C long == a Python int, so is its own hash. */
923 x = (long)intpart;
924 if (x == -1)
925 x = -2;
926 return x;
927 }
928 /* The fractional part is non-zero, so we don't have to worry about
929 * making this match the hash of some other type.
930 * Use frexp to get at the bits in the double.
Fred Drake13634cf2000-06-29 19:17:04 +0000931 * Since the VAX D double format has 56 mantissa bits, which is the
932 * most of any double format in use, each of these parts may have as
933 * many as (but no more than) 56 significant bits.
Tim Peters39dce292000-08-15 03:34:48 +0000934 * So, assuming sizeof(long) >= 4, each part can be broken into two
935 * longs; frexp and multiplication are used to do that.
936 * Also, since the Cray double format has 15 exponent bits, which is
937 * the most of any double format in use, shifting the exponent field
938 * left by 15 won't overflow a long (again assuming sizeof(long) >= 4).
Fred Drake13634cf2000-06-29 19:17:04 +0000939 */
Tim Peters39dce292000-08-15 03:34:48 +0000940 v = frexp(v, &expo);
941 v *= 2147483648.0; /* 2**31 */
942 hipart = (long)v; /* take the top 32 bits */
943 v = (v - (double)hipart) * 2147483648.0; /* get the next 32 bits */
944 x = hipart + (long)v + (expo << 15);
945 if (x == -1)
946 x = -2;
947 return x;
Fred Drake13634cf2000-06-29 19:17:04 +0000948}
949
950long
Fred Drake100814d2000-07-09 15:48:49 +0000951_Py_HashPointer(void *p)
Fred Drake13634cf2000-06-29 19:17:04 +0000952{
953#if SIZEOF_LONG >= SIZEOF_VOID_P
954 return (long)p;
955#else
956 /* convert to a Python long and hash that */
957 PyObject* longobj;
958 long x;
959
960 if ((longobj = PyLong_FromVoidPtr(p)) == NULL) {
961 x = -1;
962 goto finally;
963 }
964 x = PyObject_Hash(longobj);
965
966finally:
967 Py_XDECREF(longobj);
968 return x;
969#endif
970}
971
972
Guido van Rossum9bfef441993-03-29 10:43:31 +0000973long
Fred Drake100814d2000-07-09 15:48:49 +0000974PyObject_Hash(PyObject *v)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000975{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000976 PyTypeObject *tp = v->ob_type;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000977 if (tp->tp_hash != NULL)
978 return (*tp->tp_hash)(v);
Guido van Rossumd1f06b92001-01-24 22:14:43 +0000979 if (tp->tp_compare == NULL && RICHCOMPARE(tp) == NULL) {
Fred Drake13634cf2000-06-29 19:17:04 +0000980 return _Py_HashPointer(v); /* Use address as hash value */
981 }
Guido van Rossum9bfef441993-03-29 10:43:31 +0000982 /* If there's a cmp but no hash defined, the object can't be hashed */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000983 PyErr_SetString(PyExc_TypeError, "unhashable type");
Guido van Rossum9bfef441993-03-29 10:43:31 +0000984 return -1;
985}
986
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000987PyObject *
Fred Drake100814d2000-07-09 15:48:49 +0000988PyObject_GetAttrString(PyObject *v, char *name)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000989{
Tim Peters6d6c1a32001-08-02 04:15:00 +0000990 PyObject *w, *res;
Guido van Rossumd8eb1b31996-08-09 20:52:03 +0000991
Tim Peters6d6c1a32001-08-02 04:15:00 +0000992 if (v->ob_type->tp_getattr != NULL)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000993 return (*v->ob_type->tp_getattr)(v, name);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000994 w = PyString_InternFromString(name);
995 if (w == NULL)
996 return NULL;
997 res = PyObject_GetAttr(v, w);
998 Py_XDECREF(w);
999 return res;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001000}
1001
1002int
Fred Drake100814d2000-07-09 15:48:49 +00001003PyObject_HasAttrString(PyObject *v, char *name)
Guido van Rossumed18fdc1993-07-11 19:55:34 +00001004{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001005 PyObject *res = PyObject_GetAttrString(v, name);
Guido van Rossumed18fdc1993-07-11 19:55:34 +00001006 if (res != NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001007 Py_DECREF(res);
Guido van Rossumed18fdc1993-07-11 19:55:34 +00001008 return 1;
1009 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001010 PyErr_Clear();
Guido van Rossumed18fdc1993-07-11 19:55:34 +00001011 return 0;
1012}
1013
1014int
Fred Drake100814d2000-07-09 15:48:49 +00001015PyObject_SetAttrString(PyObject *v, char *name, PyObject *w)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001016{
Tim Peters6d6c1a32001-08-02 04:15:00 +00001017 PyObject *s;
1018 int res;
Guido van Rossumd8eb1b31996-08-09 20:52:03 +00001019
Tim Peters6d6c1a32001-08-02 04:15:00 +00001020 if (v->ob_type->tp_setattr != NULL)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001021 return (*v->ob_type->tp_setattr)(v, name, w);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001022 s = PyString_InternFromString(name);
1023 if (s == NULL)
1024 return -1;
1025 res = PyObject_SetAttr(v, s, w);
1026 Py_XDECREF(s);
1027 return res;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001028}
1029
Guido van Rossum98ff96a1997-05-20 18:34:44 +00001030PyObject *
Fred Drake100814d2000-07-09 15:48:49 +00001031PyObject_GetAttr(PyObject *v, PyObject *name)
Guido van Rossum98ff96a1997-05-20 18:34:44 +00001032{
Tim Peters6d6c1a32001-08-02 04:15:00 +00001033 PyTypeObject *tp = v->ob_type;
1034
Marc-André Lemburge44e5072000-09-18 16:20:57 +00001035 /* The Unicode to string conversion is done here because the
1036 existing tp_getattro slots expect a string object as name
1037 and we wouldn't want to break those. */
1038 if (PyUnicode_Check(name)) {
1039 name = _PyUnicode_AsDefaultEncodedString(name, NULL);
1040 if (name == NULL)
1041 return NULL;
1042 }
Jeremy Hylton99a8f902000-06-23 14:36:32 +00001043 if (!PyString_Check(name)) {
1044 PyErr_SetString(PyExc_TypeError,
1045 "attribute name must be string");
1046 return NULL;
1047 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001048 if (tp->tp_getattro != NULL)
1049 return (*tp->tp_getattro)(v, name);
1050 if (tp->tp_getattr != NULL)
1051 return (*tp->tp_getattr)(v, PyString_AS_STRING(name));
1052 PyErr_Format(PyExc_AttributeError,
1053 "'%.50s' object has no attribute '%.400s'",
1054 tp->tp_name, PyString_AS_STRING(name));
1055 return NULL;
Guido van Rossum98ff96a1997-05-20 18:34:44 +00001056}
1057
1058int
Fred Drake100814d2000-07-09 15:48:49 +00001059PyObject_HasAttr(PyObject *v, PyObject *name)
Guido van Rossum98ff96a1997-05-20 18:34:44 +00001060{
1061 PyObject *res = PyObject_GetAttr(v, name);
1062 if (res != NULL) {
1063 Py_DECREF(res);
1064 return 1;
1065 }
1066 PyErr_Clear();
1067 return 0;
1068}
1069
1070int
Fred Drake100814d2000-07-09 15:48:49 +00001071PyObject_SetAttr(PyObject *v, PyObject *name, PyObject *value)
Guido van Rossum98ff96a1997-05-20 18:34:44 +00001072{
Tim Peters6d6c1a32001-08-02 04:15:00 +00001073 PyTypeObject *tp = v->ob_type;
Guido van Rossum98ff96a1997-05-20 18:34:44 +00001074 int err;
Marc-André Lemburge44e5072000-09-18 16:20:57 +00001075
1076 /* The Unicode to string conversion is done here because the
1077 existing tp_setattro slots expect a string object as name
1078 and we wouldn't want to break those. */
1079 if (PyUnicode_Check(name)) {
1080 name = PyUnicode_AsEncodedString(name, NULL, NULL);
1081 if (name == NULL)
1082 return -1;
Jeremy Hylton99a8f902000-06-23 14:36:32 +00001083 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001084 else if (!PyString_Check(name)){
Jeremy Hylton99a8f902000-06-23 14:36:32 +00001085 PyErr_SetString(PyExc_TypeError,
1086 "attribute name must be string");
Tim Peters6d6c1a32001-08-02 04:15:00 +00001087 return -1;
Jeremy Hylton99a8f902000-06-23 14:36:32 +00001088 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001089 else
1090 Py_INCREF(name);
1091
1092 PyString_InternInPlace(&name);
1093 if (tp->tp_setattro != NULL) {
1094 err = (*tp->tp_setattro)(v, name, value);
1095 Py_DECREF(name);
1096 return err;
Marc-André Lemburge44e5072000-09-18 16:20:57 +00001097 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001098 if (tp->tp_setattr != NULL) {
1099 err = (*tp->tp_setattr)(v, PyString_AS_STRING(name), value);
1100 Py_DECREF(name);
1101 return err;
1102 }
Guido van Rossum98ff96a1997-05-20 18:34:44 +00001103 Py_DECREF(name);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001104 if (tp->tp_getattr == NULL && tp->tp_getattro == NULL)
1105 PyErr_Format(PyExc_TypeError,
1106 "'%.100s' object has no attributes "
1107 "(%s .%.100s)",
1108 tp->tp_name,
1109 value==NULL ? "del" : "assign to",
1110 PyString_AS_STRING(name));
1111 else
1112 PyErr_Format(PyExc_TypeError,
1113 "'%.100s' object has only read-only attributes "
1114 "(%s .%.100s)",
1115 tp->tp_name,
1116 value==NULL ? "del" : "assign to",
1117 PyString_AS_STRING(name));
1118 return -1;
1119}
1120
1121/* Helper to get a pointer to an object's __dict__ slot, if any */
1122
1123PyObject **
1124_PyObject_GetDictPtr(PyObject *obj)
1125{
1126#define PTRSIZE (sizeof(PyObject *))
1127
1128 long dictoffset;
1129 PyTypeObject *tp = obj->ob_type;
1130
1131 if (!(tp->tp_flags & Py_TPFLAGS_HAVE_CLASS))
1132 return NULL;
1133 dictoffset = tp->tp_dictoffset;
1134 if (dictoffset == 0)
1135 return NULL;
1136 if (dictoffset < 0) {
1137 dictoffset += PyType_BASICSIZE(tp);
1138 assert(dictoffset > 0); /* Sanity check */
1139 if (tp->tp_itemsize > 0) {
1140 int n = ((PyVarObject *)obj)->ob_size;
1141 if (n > 0) {
1142 dictoffset += tp->tp_itemsize * n;
1143 /* Round up, if necessary */
1144 if (tp->tp_itemsize % PTRSIZE != 0) {
1145 dictoffset += PTRSIZE - 1;
1146 dictoffset /= PTRSIZE;
1147 dictoffset *= PTRSIZE;
1148 }
1149 }
1150 }
1151 }
1152 return (PyObject **) ((char *)obj + dictoffset);
1153}
1154
1155/* Generic GetAttr functions - put these in your tp_[gs]etattro slot */
1156
1157PyObject *
1158PyObject_GenericGetAttr(PyObject *obj, PyObject *name)
1159{
1160 PyTypeObject *tp = obj->ob_type;
1161 PyObject *descr;
1162 descrgetfunc f;
1163 PyObject **dictptr;
1164
1165 if (tp->tp_dict == NULL) {
Guido van Rossum528b7eb2001-08-07 17:24:28 +00001166 if (PyType_Ready(tp) < 0)
Tim Peters6d6c1a32001-08-02 04:15:00 +00001167 return NULL;
1168 }
1169
1170 descr = _PyType_Lookup(tp, name);
1171 f = NULL;
1172 if (descr != NULL) {
1173 f = descr->ob_type->tp_descr_get;
1174 if (f != NULL && PyDescr_IsData(descr))
1175 return f(descr, obj, (PyObject *)obj->ob_type);
1176 }
1177
1178 dictptr = _PyObject_GetDictPtr(obj);
1179 if (dictptr != NULL) {
1180 PyObject *dict = *dictptr;
1181 if (dict != NULL) {
1182 PyObject *res = PyDict_GetItem(dict, name);
1183 if (res != NULL) {
1184 Py_INCREF(res);
1185 return res;
1186 }
1187 }
1188 }
1189
1190 if (f != NULL)
1191 return f(descr, obj, (PyObject *)obj->ob_type);
1192
1193 if (descr != NULL) {
1194 Py_INCREF(descr);
1195 return descr;
1196 }
1197
1198 PyErr_Format(PyExc_AttributeError,
1199 "'%.50s' object has no attribute '%.400s'",
1200 tp->tp_name, PyString_AS_STRING(name));
1201 return NULL;
1202}
1203
1204int
1205PyObject_GenericSetAttr(PyObject *obj, PyObject *name, PyObject *value)
1206{
1207 PyTypeObject *tp = obj->ob_type;
1208 PyObject *descr;
1209 descrsetfunc f;
1210 PyObject **dictptr;
1211
1212 if (tp->tp_dict == NULL) {
Guido van Rossum528b7eb2001-08-07 17:24:28 +00001213 if (PyType_Ready(tp) < 0)
Tim Peters6d6c1a32001-08-02 04:15:00 +00001214 return -1;
1215 }
1216
1217 descr = _PyType_Lookup(tp, name);
1218 f = NULL;
1219 if (descr != NULL) {
1220 f = descr->ob_type->tp_descr_set;
1221 if (f != NULL && PyDescr_IsData(descr))
1222 return f(descr, obj, value);
1223 }
1224
1225 dictptr = _PyObject_GetDictPtr(obj);
1226 if (dictptr != NULL) {
1227 PyObject *dict = *dictptr;
1228 if (dict == NULL && value != NULL) {
1229 dict = PyDict_New();
1230 if (dict == NULL)
1231 return -1;
1232 *dictptr = dict;
1233 }
1234 if (dict != NULL) {
1235 int res;
1236 if (value == NULL)
1237 res = PyDict_DelItem(dict, name);
1238 else
1239 res = PyDict_SetItem(dict, name, value);
1240 if (res < 0 && PyErr_ExceptionMatches(PyExc_KeyError))
1241 PyErr_SetObject(PyExc_AttributeError, name);
1242 return res;
1243 }
1244 }
1245
1246 if (f != NULL)
1247 return f(descr, obj, value);
1248
1249 if (descr == NULL) {
1250 PyErr_Format(PyExc_AttributeError,
1251 "'%.50s' object has no attribute '%.400s'",
1252 tp->tp_name, PyString_AS_STRING(name));
1253 return -1;
1254 }
1255
1256 PyErr_Format(PyExc_AttributeError,
1257 "'%.50s' object attribute '%.400s' is read-only",
1258 tp->tp_name, PyString_AS_STRING(name));
1259 return -1;
Guido van Rossum98ff96a1997-05-20 18:34:44 +00001260}
1261
Guido van Rossum6ac258d1993-05-12 08:24:20 +00001262/* Test a value used as condition, e.g., in a for or if statement.
1263 Return -1 if an error occurred */
1264
1265int
Fred Drake100814d2000-07-09 15:48:49 +00001266PyObject_IsTrue(PyObject *v)
Guido van Rossum6ac258d1993-05-12 08:24:20 +00001267{
1268 int res;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001269 if (v == Py_None)
Guido van Rossum6ac258d1993-05-12 08:24:20 +00001270 res = 0;
Guido van Rossum1c4f4581998-05-22 00:53:24 +00001271 else if (v->ob_type->tp_as_number != NULL &&
1272 v->ob_type->tp_as_number->nb_nonzero != NULL)
Guido van Rossum6ac258d1993-05-12 08:24:20 +00001273 res = (*v->ob_type->tp_as_number->nb_nonzero)(v);
Guido van Rossum1c4f4581998-05-22 00:53:24 +00001274 else if (v->ob_type->tp_as_mapping != NULL &&
1275 v->ob_type->tp_as_mapping->mp_length != NULL)
Guido van Rossum6ac258d1993-05-12 08:24:20 +00001276 res = (*v->ob_type->tp_as_mapping->mp_length)(v);
Guido van Rossum1c4f4581998-05-22 00:53:24 +00001277 else if (v->ob_type->tp_as_sequence != NULL &&
1278 v->ob_type->tp_as_sequence->sq_length != NULL)
Guido van Rossum6ac258d1993-05-12 08:24:20 +00001279 res = (*v->ob_type->tp_as_sequence->sq_length)(v);
1280 else
1281 res = 1;
1282 if (res > 0)
1283 res = 1;
1284 return res;
1285}
1286
Guido van Rossumc3d3f961998-04-09 17:53:59 +00001287/* equivalent of 'not v'
1288 Return -1 if an error occurred */
1289
1290int
Fred Drake100814d2000-07-09 15:48:49 +00001291PyObject_Not(PyObject *v)
Guido van Rossumc3d3f961998-04-09 17:53:59 +00001292{
1293 int res;
1294 res = PyObject_IsTrue(v);
1295 if (res < 0)
1296 return res;
1297 return res == 0;
1298}
1299
Guido van Rossum5524a591995-01-10 15:26:20 +00001300/* Coerce two numeric types to the "larger" one.
1301 Increment the reference count on each argument.
Guido van Rossume797ec12001-01-17 15:24:28 +00001302 Return value:
1303 -1 if an error occurred;
1304 0 if the coercion succeeded (and then the reference counts are increased);
1305 1 if no coercion is possible (and no error is raised).
Guido van Rossum5524a591995-01-10 15:26:20 +00001306*/
Guido van Rossum5524a591995-01-10 15:26:20 +00001307int
Fred Drake100814d2000-07-09 15:48:49 +00001308PyNumber_CoerceEx(PyObject **pv, PyObject **pw)
Guido van Rossum5524a591995-01-10 15:26:20 +00001309{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001310 register PyObject *v = *pv;
1311 register PyObject *w = *pw;
Guido van Rossum5524a591995-01-10 15:26:20 +00001312 int res;
1313
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001314 if (v->ob_type == w->ob_type && !PyInstance_Check(v)) {
1315 Py_INCREF(v);
1316 Py_INCREF(w);
Guido van Rossum5524a591995-01-10 15:26:20 +00001317 return 0;
1318 }
1319 if (v->ob_type->tp_as_number && v->ob_type->tp_as_number->nb_coerce) {
1320 res = (*v->ob_type->tp_as_number->nb_coerce)(pv, pw);
1321 if (res <= 0)
1322 return res;
1323 }
1324 if (w->ob_type->tp_as_number && w->ob_type->tp_as_number->nb_coerce) {
1325 res = (*w->ob_type->tp_as_number->nb_coerce)(pw, pv);
1326 if (res <= 0)
1327 return res;
1328 }
Guido van Rossum242c6421997-11-19 16:03:17 +00001329 return 1;
1330}
1331
Guido van Rossume797ec12001-01-17 15:24:28 +00001332/* Coerce two numeric types to the "larger" one.
1333 Increment the reference count on each argument.
1334 Return -1 and raise an exception if no coercion is possible
1335 (and then no reference count is incremented).
1336*/
Guido van Rossum242c6421997-11-19 16:03:17 +00001337int
Fred Drake100814d2000-07-09 15:48:49 +00001338PyNumber_Coerce(PyObject **pv, PyObject **pw)
Guido van Rossum242c6421997-11-19 16:03:17 +00001339{
1340 int err = PyNumber_CoerceEx(pv, pw);
1341 if (err <= 0)
1342 return err;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001343 PyErr_SetString(PyExc_TypeError, "number coercion failed");
Guido van Rossum5524a591995-01-10 15:26:20 +00001344 return -1;
1345}
1346
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001347
Guido van Rossum49b11fe1995-01-26 00:38:22 +00001348/* Test whether an object can be called */
1349
1350int
Fred Drake100814d2000-07-09 15:48:49 +00001351PyCallable_Check(PyObject *x)
Guido van Rossum49b11fe1995-01-26 00:38:22 +00001352{
1353 if (x == NULL)
1354 return 0;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001355 if (PyInstance_Check(x)) {
1356 PyObject *call = PyObject_GetAttrString(x, "__call__");
Guido van Rossum49b11fe1995-01-26 00:38:22 +00001357 if (call == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001358 PyErr_Clear();
Guido van Rossum49b11fe1995-01-26 00:38:22 +00001359 return 0;
1360 }
1361 /* Could test recursively but don't, for fear of endless
1362 recursion if some joker sets self.__call__ = self */
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001363 Py_DECREF(call);
Guido van Rossum49b11fe1995-01-26 00:38:22 +00001364 return 1;
1365 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001366 else {
1367 return x->ob_type->tp_call != NULL;
1368 }
Guido van Rossum49b11fe1995-01-26 00:38:22 +00001369}
1370
1371
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001372/*
1373NoObject is usable as a non-NULL undefined value, used by the macro None.
1374There is (and should be!) no way to create other objects of this type,
Guido van Rossum3f5da241990-12-20 15:06:42 +00001375so there is exactly one (which is indestructible, by the way).
Guido van Rossumba21a492001-08-16 08:17:26 +00001376(XXX This type and the type of NotImplemented below should be unified.)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001377*/
1378
Guido van Rossum0c182a11992-03-27 17:26:13 +00001379/* ARGSUSED */
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001380static PyObject *
Fred Drake100814d2000-07-09 15:48:49 +00001381none_repr(PyObject *op)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001382{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001383 return PyString_FromString("None");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001384}
1385
Barry Warsaw9bf16442001-01-23 16:24:35 +00001386/* ARGUSED */
1387static void
1388none_dealloc(PyObject* ignore)
1389{
1390 /* This should never get called, but we also don't want to SEGV if
1391 * we accidently decref None out of existance.
1392 */
1393 abort();
1394}
1395
1396
Guido van Rossumba21a492001-08-16 08:17:26 +00001397static PyTypeObject PyNone_Type = {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001398 PyObject_HEAD_INIT(&PyType_Type)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001399 0,
Guido van Rossumba21a492001-08-16 08:17:26 +00001400 "NoneType",
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001401 0,
1402 0,
Barry Warsaw9bf16442001-01-23 16:24:35 +00001403 (destructor)none_dealloc, /*tp_dealloc*/ /*never called*/
Guido van Rossum7066dd71992-09-17 17:54:56 +00001404 0, /*tp_print*/
Guido van Rossum3f5da241990-12-20 15:06:42 +00001405 0, /*tp_getattr*/
1406 0, /*tp_setattr*/
1407 0, /*tp_compare*/
Guido van Rossum1d5735e1994-08-30 08:27:36 +00001408 (reprfunc)none_repr, /*tp_repr*/
Guido van Rossum3f5da241990-12-20 15:06:42 +00001409 0, /*tp_as_number*/
1410 0, /*tp_as_sequence*/
1411 0, /*tp_as_mapping*/
Guido van Rossum9bfef441993-03-29 10:43:31 +00001412 0, /*tp_hash */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001413};
1414
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001415PyObject _Py_NoneStruct = {
Guido van Rossumba21a492001-08-16 08:17:26 +00001416 PyObject_HEAD_INIT(&PyNone_Type)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001417};
1418
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +00001419/* NotImplemented is an object that can be used to signal that an
1420 operation is not implemented for the given type combination. */
1421
1422static PyObject *
1423NotImplemented_repr(PyObject *op)
1424{
1425 return PyString_FromString("NotImplemented");
1426}
1427
1428static PyTypeObject PyNotImplemented_Type = {
1429 PyObject_HEAD_INIT(&PyType_Type)
1430 0,
Guido van Rossumba21a492001-08-16 08:17:26 +00001431 "NotImplementedType",
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +00001432 0,
1433 0,
Barry Warsaw9bf16442001-01-23 16:24:35 +00001434 (destructor)none_dealloc, /*tp_dealloc*/ /*never called*/
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +00001435 0, /*tp_print*/
1436 0, /*tp_getattr*/
1437 0, /*tp_setattr*/
1438 0, /*tp_compare*/
1439 (reprfunc)NotImplemented_repr, /*tp_repr*/
1440 0, /*tp_as_number*/
1441 0, /*tp_as_sequence*/
1442 0, /*tp_as_mapping*/
1443 0, /*tp_hash */
1444};
1445
1446PyObject _Py_NotImplementedStruct = {
1447 PyObject_HEAD_INIT(&PyNotImplemented_Type)
1448};
1449
Guido van Rossumba21a492001-08-16 08:17:26 +00001450void
1451_Py_ReadyTypes(void)
1452{
1453 if (PyType_Ready(&PyType_Type) < 0)
1454 Py_FatalError("Can't initialize 'type'");
1455
1456 if (PyType_Ready(&PyList_Type) < 0)
1457 Py_FatalError("Can't initialize 'list'");
1458
1459 if (PyType_Ready(&PyNone_Type) < 0)
1460 Py_FatalError("Can't initialize type(None)");
1461
1462 if (PyType_Ready(&PyNotImplemented_Type) < 0)
1463 Py_FatalError("Can't initialize type(NotImplemented)");
1464}
1465
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001466
Guido van Rossum84a90321996-05-22 16:34:47 +00001467#ifdef Py_TRACE_REFS
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001468
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001469static PyObject refchain = {&refchain, &refchain};
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001470
Guido van Rossumaacdc9d1996-08-12 21:32:12 +00001471void
Fred Drake100814d2000-07-09 15:48:49 +00001472_Py_ResetReferences(void)
Guido van Rossume09fb551997-08-05 02:04:34 +00001473{
1474 refchain._ob_prev = refchain._ob_next = &refchain;
1475 _Py_RefTotal = 0;
1476}
1477
1478void
Fred Drake100814d2000-07-09 15:48:49 +00001479_Py_NewReference(PyObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001480{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001481 _Py_RefTotal++;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001482 op->ob_refcnt = 1;
1483 op->_ob_next = refchain._ob_next;
1484 op->_ob_prev = &refchain;
1485 refchain._ob_next->_ob_prev = op;
1486 refchain._ob_next = op;
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +00001487#ifdef COUNT_ALLOCS
1488 inc_count(op->ob_type);
1489#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001490}
1491
Guido van Rossumaacdc9d1996-08-12 21:32:12 +00001492void
Fred Drake100814d2000-07-09 15:48:49 +00001493_Py_ForgetReference(register PyObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001494{
Guido van Rossumbffd6832000-01-20 22:32:56 +00001495#ifdef SLOW_UNREF_CHECK
Guido van Rossum4c08d552000-03-10 22:55:18 +00001496 register PyObject *p;
Guido van Rossumbffd6832000-01-20 22:32:56 +00001497#endif
Guido van Rossumd7047b31995-01-02 19:07:15 +00001498 if (op->ob_refcnt < 0)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001499 Py_FatalError("UNREF negative refcnt");
Guido van Rossum2e8f6141992-09-03 20:32:55 +00001500 if (op == &refchain ||
Guido van Rossumd7047b31995-01-02 19:07:15 +00001501 op->_ob_prev->_ob_next != op || op->_ob_next->_ob_prev != op)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001502 Py_FatalError("UNREF invalid object");
Guido van Rossum2e8f6141992-09-03 20:32:55 +00001503#ifdef SLOW_UNREF_CHECK
Guido van Rossum3f5da241990-12-20 15:06:42 +00001504 for (p = refchain._ob_next; p != &refchain; p = p->_ob_next) {
1505 if (p == op)
1506 break;
1507 }
Guido van Rossumd7047b31995-01-02 19:07:15 +00001508 if (p == &refchain) /* Not found */
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001509 Py_FatalError("UNREF unknown object");
Guido van Rossum2e8f6141992-09-03 20:32:55 +00001510#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001511 op->_ob_next->_ob_prev = op->_ob_prev;
1512 op->_ob_prev->_ob_next = op->_ob_next;
Guido van Rossum2e8f6141992-09-03 20:32:55 +00001513 op->_ob_next = op->_ob_prev = NULL;
Guido van Rossumd8953cb1995-04-06 14:46:26 +00001514#ifdef COUNT_ALLOCS
Tim Peters6d6c1a32001-08-02 04:15:00 +00001515 op->ob_type->tp_frees++;
Guido van Rossumd8953cb1995-04-06 14:46:26 +00001516#endif
Guido van Rossum3f5da241990-12-20 15:06:42 +00001517}
1518
Guido van Rossumaacdc9d1996-08-12 21:32:12 +00001519void
Fred Drake100814d2000-07-09 15:48:49 +00001520_Py_Dealloc(PyObject *op)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001521{
Guido van Rossum9776adf1994-09-07 14:36:45 +00001522 destructor dealloc = op->ob_type->tp_dealloc;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001523 _Py_ForgetReference(op);
Guido van Rossum9776adf1994-09-07 14:36:45 +00001524 (*dealloc)(op);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001525}
1526
Guido van Rossumaacdc9d1996-08-12 21:32:12 +00001527void
Fred Drake100814d2000-07-09 15:48:49 +00001528_Py_PrintReferences(FILE *fp)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001529{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001530 PyObject *op;
Guido van Rossume09fb551997-08-05 02:04:34 +00001531 fprintf(fp, "Remaining objects:\n");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001532 for (op = refchain._ob_next; op != &refchain; op = op->_ob_next) {
1533 fprintf(fp, "[%d] ", op->ob_refcnt);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001534 if (PyObject_Print(op, fp, 0) != 0)
1535 PyErr_Clear();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001536 putc('\n', fp);
1537 }
1538}
1539
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +00001540PyObject *
Fred Drake100814d2000-07-09 15:48:49 +00001541_Py_GetObjects(PyObject *self, PyObject *args)
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +00001542{
1543 int i, n;
1544 PyObject *t = NULL;
1545 PyObject *res, *op;
1546
1547 if (!PyArg_ParseTuple(args, "i|O", &n, &t))
1548 return NULL;
1549 op = refchain._ob_next;
1550 res = PyList_New(0);
1551 if (res == NULL)
1552 return NULL;
1553 for (i = 0; (n == 0 || i < n) && op != &refchain; i++) {
1554 while (op == self || op == args || op == res || op == t ||
Guido van Rossum3c296022001-07-14 17:58:00 +00001555 (t != NULL && op->ob_type != (PyTypeObject *) t)) {
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +00001556 op = op->_ob_next;
1557 if (op == &refchain)
1558 return res;
1559 }
1560 if (PyList_Append(res, op) < 0) {
1561 Py_DECREF(res);
1562 return NULL;
1563 }
1564 op = op->_ob_next;
1565 }
1566 return res;
1567}
1568
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001569#endif
Guido van Rossum97ead3f1996-01-12 01:24:09 +00001570
1571
1572/* Hack to force loading of cobject.o */
Guido van Rossumda9c2711996-12-05 21:58:58 +00001573PyTypeObject *_Py_cobject_hack = &PyCObject_Type;
Guido van Rossum84a90321996-05-22 16:34:47 +00001574
1575
1576/* Hack to force loading of abstract.o */
Jeremy Hylton6253f832000-07-12 12:56:19 +00001577int (*_Py_abstract_hack)(PyObject *) = &PyObject_Size;
Guido van Rossume09fb551997-08-05 02:04:34 +00001578
1579
Andrew M. Kuchling1582a3a2000-08-16 12:27:23 +00001580/* Python's malloc wrappers (see pymem.h) */
Guido van Rossume09fb551997-08-05 02:04:34 +00001581
Thomas Wouters334fb892000-07-25 12:56:38 +00001582void *
Fred Drake100814d2000-07-09 15:48:49 +00001583PyMem_Malloc(size_t nbytes)
Guido van Rossume09fb551997-08-05 02:04:34 +00001584{
1585#if _PyMem_EXTRA > 0
1586 if (nbytes == 0)
1587 nbytes = _PyMem_EXTRA;
1588#endif
Guido van Rossumb18618d2000-05-03 23:44:39 +00001589 return PyMem_MALLOC(nbytes);
Guido van Rossume09fb551997-08-05 02:04:34 +00001590}
1591
Thomas Wouters334fb892000-07-25 12:56:38 +00001592void *
1593PyMem_Realloc(void *p, size_t nbytes)
Guido van Rossume09fb551997-08-05 02:04:34 +00001594{
1595#if _PyMem_EXTRA > 0
1596 if (nbytes == 0)
1597 nbytes = _PyMem_EXTRA;
1598#endif
Guido van Rossumb18618d2000-05-03 23:44:39 +00001599 return PyMem_REALLOC(p, nbytes);
Guido van Rossume09fb551997-08-05 02:04:34 +00001600}
1601
1602void
Thomas Wouters334fb892000-07-25 12:56:38 +00001603PyMem_Free(void *p)
Guido van Rossume09fb551997-08-05 02:04:34 +00001604{
Guido van Rossumb18618d2000-05-03 23:44:39 +00001605 PyMem_FREE(p);
1606}
1607
1608
1609/* Python's object malloc wrappers (see objimpl.h) */
1610
Thomas Wouters334fb892000-07-25 12:56:38 +00001611void *
Fred Drake100814d2000-07-09 15:48:49 +00001612PyObject_Malloc(size_t nbytes)
Guido van Rossumb18618d2000-05-03 23:44:39 +00001613{
1614 return PyObject_MALLOC(nbytes);
1615}
1616
Thomas Wouters334fb892000-07-25 12:56:38 +00001617void *
1618PyObject_Realloc(void *p, size_t nbytes)
Guido van Rossumb18618d2000-05-03 23:44:39 +00001619{
1620 return PyObject_REALLOC(p, nbytes);
1621}
1622
1623void
Thomas Wouters334fb892000-07-25 12:56:38 +00001624PyObject_Free(void *p)
Guido van Rossumb18618d2000-05-03 23:44:39 +00001625{
1626 PyObject_FREE(p);
Guido van Rossume09fb551997-08-05 02:04:34 +00001627}
Guido van Rossum86610361998-04-10 22:32:46 +00001628
1629
Fred Drake41deb1e2001-02-01 05:27:45 +00001630/* Hook to clear up weak references only once the _weakref module is
1631 imported. We use a dummy implementation to simplify the code at each
1632 call site instead of requiring a test for NULL.
1633*/
1634
Fred Drakeb60654b2001-02-26 18:56:37 +00001635static void
Fred Drake41deb1e2001-02-01 05:27:45 +00001636empty_clear_weak_refs(PyObject *o)
1637{
Fred Drakeb60654b2001-02-26 18:56:37 +00001638 return;
Fred Drake41deb1e2001-02-01 05:27:45 +00001639}
1640
Fred Drakeb60654b2001-02-26 18:56:37 +00001641void (*PyObject_ClearWeakRefs)(PyObject *) = empty_clear_weak_refs;
Fred Drake41deb1e2001-02-01 05:27:45 +00001642
1643
1644
Guido van Rossum86610361998-04-10 22:32:46 +00001645/* These methods are used to control infinite recursion in repr, str, print,
1646 etc. Container objects that may recursively contain themselves,
1647 e.g. builtin dictionaries and lists, should used Py_ReprEnter() and
1648 Py_ReprLeave() to avoid infinite recursion.
1649
1650 Py_ReprEnter() returns 0 the first time it is called for a particular
1651 object and 1 every time thereafter. It returns -1 if an exception
1652 occurred. Py_ReprLeave() has no return value.
1653
1654 See dictobject.c and listobject.c for examples of use.
1655*/
1656
1657#define KEY "Py_Repr"
1658
1659int
Fred Drake100814d2000-07-09 15:48:49 +00001660Py_ReprEnter(PyObject *obj)
Guido van Rossum86610361998-04-10 22:32:46 +00001661{
1662 PyObject *dict;
1663 PyObject *list;
1664 int i;
1665
1666 dict = PyThreadState_GetDict();
1667 if (dict == NULL)
1668 return -1;
1669 list = PyDict_GetItemString(dict, KEY);
1670 if (list == NULL) {
1671 list = PyList_New(0);
1672 if (list == NULL)
1673 return -1;
1674 if (PyDict_SetItemString(dict, KEY, list) < 0)
1675 return -1;
1676 Py_DECREF(list);
1677 }
1678 i = PyList_GET_SIZE(list);
1679 while (--i >= 0) {
1680 if (PyList_GET_ITEM(list, i) == obj)
1681 return 1;
1682 }
1683 PyList_Append(list, obj);
1684 return 0;
1685}
1686
1687void
Fred Drake100814d2000-07-09 15:48:49 +00001688Py_ReprLeave(PyObject *obj)
Guido van Rossum86610361998-04-10 22:32:46 +00001689{
1690 PyObject *dict;
1691 PyObject *list;
1692 int i;
1693
1694 dict = PyThreadState_GetDict();
Guido van Rossumeb909461998-04-11 15:17:34 +00001695 if (dict == NULL)
1696 return;
Guido van Rossum86610361998-04-10 22:32:46 +00001697 list = PyDict_GetItemString(dict, KEY);
Guido van Rossumeb909461998-04-11 15:17:34 +00001698 if (list == NULL || !PyList_Check(list))
1699 return;
Guido van Rossum86610361998-04-10 22:32:46 +00001700 i = PyList_GET_SIZE(list);
1701 /* Count backwards because we always expect obj to be list[-1] */
1702 while (--i >= 0) {
1703 if (PyList_GET_ITEM(list, i) == obj) {
1704 PyList_SetSlice(list, i, i + 1, NULL);
1705 break;
1706 }
1707 }
1708}
Guido van Rossumd724b232000-03-13 16:01:29 +00001709
1710/*
1711 trashcan
1712 CT 2k0130
1713 non-recursively destroy nested objects
1714
1715 CT 2k0223
1716 everything is now done in a macro.
1717
1718 CT 2k0305
1719 modified to use functions, after Tim Peter's suggestion.
1720
1721 CT 2k0309
1722 modified to restore a possible error.
Guido van Rossum13ff8eb2000-03-25 18:39:19 +00001723
1724 CT 2k0325
1725 added better safe than sorry check for threadstate
Guido van Rossume92e6102000-04-24 15:40:53 +00001726
1727 CT 2k0422
1728 complete rewrite. We now build a chain via ob_type
1729 and save the limited number of types in ob_refcnt.
1730 This is perfect since we don't need any memory.
1731 A patch for free-threading would need just a lock.
Guido van Rossumd724b232000-03-13 16:01:29 +00001732*/
1733
Guido van Rossume92e6102000-04-24 15:40:53 +00001734#define Py_TRASHCAN_TUPLE 1
1735#define Py_TRASHCAN_LIST 2
1736#define Py_TRASHCAN_DICT 3
1737#define Py_TRASHCAN_FRAME 4
1738#define Py_TRASHCAN_TRACEBACK 5
1739/* extend here if other objects want protection */
1740
Guido van Rossumd724b232000-03-13 16:01:29 +00001741int _PyTrash_delete_nesting = 0;
Guido van Rossume92e6102000-04-24 15:40:53 +00001742
Guido van Rossumd724b232000-03-13 16:01:29 +00001743PyObject * _PyTrash_delete_later = NULL;
1744
1745void
Fred Drake100814d2000-07-09 15:48:49 +00001746_PyTrash_deposit_object(PyObject *op)
Guido van Rossumd724b232000-03-13 16:01:29 +00001747{
Guido van Rossume92e6102000-04-24 15:40:53 +00001748 int typecode;
Guido van Rossum13ff8eb2000-03-25 18:39:19 +00001749
Guido van Rossume92e6102000-04-24 15:40:53 +00001750 if (PyTuple_Check(op))
1751 typecode = Py_TRASHCAN_TUPLE;
1752 else if (PyList_Check(op))
1753 typecode = Py_TRASHCAN_LIST;
1754 else if (PyDict_Check(op))
1755 typecode = Py_TRASHCAN_DICT;
1756 else if (PyFrame_Check(op))
1757 typecode = Py_TRASHCAN_FRAME;
1758 else if (PyTraceBack_Check(op))
1759 typecode = Py_TRASHCAN_TRACEBACK;
Moshe Zadkacf703f02000-08-04 15:36:13 +00001760 else /* We have a bug here -- those are the only types in GC */ {
1761 Py_FatalError("Type not supported in GC -- internal bug");
1762 return; /* pacify compiler -- execution never here */
1763 }
Guido van Rossume92e6102000-04-24 15:40:53 +00001764 op->ob_refcnt = typecode;
Guido van Rossumd724b232000-03-13 16:01:29 +00001765
Guido van Rossume92e6102000-04-24 15:40:53 +00001766 op->ob_type = (PyTypeObject*)_PyTrash_delete_later;
1767 _PyTrash_delete_later = op;
Guido van Rossumd724b232000-03-13 16:01:29 +00001768}
1769
1770void
Fred Drake100814d2000-07-09 15:48:49 +00001771_PyTrash_destroy_chain(void)
Guido van Rossumd724b232000-03-13 16:01:29 +00001772{
1773 while (_PyTrash_delete_later) {
1774 PyObject *shredder = _PyTrash_delete_later;
Guido van Rossume92e6102000-04-24 15:40:53 +00001775 _PyTrash_delete_later = (PyObject*) shredder->ob_type;
1776
1777 switch (shredder->ob_refcnt) {
1778 case Py_TRASHCAN_TUPLE:
1779 shredder->ob_type = &PyTuple_Type;
1780 break;
1781 case Py_TRASHCAN_LIST:
1782 shredder->ob_type = &PyList_Type;
1783 break;
1784 case Py_TRASHCAN_DICT:
1785 shredder->ob_type = &PyDict_Type;
1786 break;
1787 case Py_TRASHCAN_FRAME:
1788 shredder->ob_type = &PyFrame_Type;
1789 break;
1790 case Py_TRASHCAN_TRACEBACK:
1791 shredder->ob_type = &PyTraceBack_Type;
1792 break;
1793 }
1794 _Py_NewReference(shredder);
1795
Guido van Rossumd724b232000-03-13 16:01:29 +00001796 ++_PyTrash_delete_nesting;
1797 Py_DECREF(shredder);
1798 --_PyTrash_delete_nesting;
1799 }
1800}
Neil Schemenauera35c6882001-02-27 04:45:05 +00001801
1802#ifdef WITH_PYMALLOC
1803#include "obmalloc.c"
1804#endif