blob: 1ace8f5b26c7b477c963661e39d3b31d896241db [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",
35 tp->tp_name, tp->tp_alloc, tp->tp_free,
36 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) {
56 v = Py_BuildValue("(siii)", tp->tp_name, tp->tp_alloc,
57 tp->tp_free, tp->tp_maxalloc);
58 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{
75 if (tp->tp_alloc == 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 }
82 tp->tp_alloc++;
83 if (tp->tp_alloc - tp->tp_free > tp->tp_maxalloc)
84 tp->tp_maxalloc = tp->tp_alloc - tp->tp_free;
85}
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#ifdef WITH_CYCLE_GC
97 if (PyType_IS_GC(tp))
98 op = (PyObject *) PyObject_FROM_GC(op);
99#endif
Guido van Rossumb18618d2000-05-03 23:44:39 +0000100 /* Any changes should be reflected in PyObject_INIT (objimpl.h) */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000101 op->ob_type = tp;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000102 _Py_NewReference(op);
Fred Drake41deb1e2001-02-01 05:27:45 +0000103 if (PyType_SUPPORTS_WEAKREFS(tp)) {
104 PyObject **weaklist = PyObject_GET_WEAKREFS_LISTPTR(op);
105 *weaklist = NULL;
106 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000107 return op;
108}
109
Guido van Rossumb18618d2000-05-03 23:44:39 +0000110PyVarObject *
Fred Drake100814d2000-07-09 15:48:49 +0000111PyObject_InitVar(PyVarObject *op, PyTypeObject *tp, int size)
Guido van Rossumb18618d2000-05-03 23:44:39 +0000112{
113 if (op == NULL) {
114 PyErr_SetString(PyExc_SystemError,
115 "NULL object passed to PyObject_InitVar");
116 return op;
117 }
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000118#ifdef WITH_CYCLE_GC
119 if (PyType_IS_GC(tp))
120 op = (PyVarObject *) PyObject_FROM_GC(op);
121#endif
Guido van Rossumb18618d2000-05-03 23:44:39 +0000122 /* Any changes should be reflected in PyObject_INIT_VAR */
123 op->ob_size = size;
124 op->ob_type = tp;
125 _Py_NewReference((PyObject *)op);
Fred Drake41deb1e2001-02-01 05:27:45 +0000126 if (PyType_SUPPORTS_WEAKREFS(tp)) {
127 PyObject **weaklist = PyObject_GET_WEAKREFS_LISTPTR(op);
128 *weaklist = NULL;
129 }
Guido van Rossumb18618d2000-05-03 23:44:39 +0000130 return op;
131}
132
133PyObject *
Fred Drake100814d2000-07-09 15:48:49 +0000134_PyObject_New(PyTypeObject *tp)
Guido van Rossumb18618d2000-05-03 23:44:39 +0000135{
136 PyObject *op;
137 op = (PyObject *) PyObject_MALLOC(_PyObject_SIZE(tp));
138 if (op == NULL)
139 return PyErr_NoMemory();
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000140#ifdef WITH_CYCLE_GC
141 if (PyType_IS_GC(tp))
142 op = (PyObject *) PyObject_FROM_GC(op);
143#endif
Guido van Rossumb18618d2000-05-03 23:44:39 +0000144 return PyObject_INIT(op, tp);
145}
146
Guido van Rossumd0c87ee1997-05-15 21:31:03 +0000147PyVarObject *
Fred Drake100814d2000-07-09 15:48:49 +0000148_PyObject_NewVar(PyTypeObject *tp, int size)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000149{
Guido van Rossumb18618d2000-05-03 23:44:39 +0000150 PyVarObject *op;
151 op = (PyVarObject *) PyObject_MALLOC(_PyObject_VAR_SIZE(tp, size));
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000152 if (op == NULL)
Guido van Rossumd0c87ee1997-05-15 21:31:03 +0000153 return (PyVarObject *)PyErr_NoMemory();
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000154#ifdef WITH_CYCLE_GC
155 if (PyType_IS_GC(tp))
156 op = (PyVarObject *) PyObject_FROM_GC(op);
157#endif
Guido van Rossumb18618d2000-05-03 23:44:39 +0000158 return PyObject_INIT_VAR(op, tp, size);
159}
160
161void
Fred Drake100814d2000-07-09 15:48:49 +0000162_PyObject_Del(PyObject *op)
Guido van Rossumb18618d2000-05-03 23:44:39 +0000163{
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000164#ifdef WITH_CYCLE_GC
Guido van Rossum4cc6ac72000-07-01 01:00:38 +0000165 if (op && PyType_IS_GC(op->ob_type)) {
166 op = (PyObject *) PyObject_AS_GC(op);
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000167 }
Guido van Rossum4cc6ac72000-07-01 01:00:38 +0000168#endif
169 PyObject_FREE(op);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000170}
171
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000172#ifndef WITH_CYCLE_GC
173/* extension modules might need these */
174void _PyGC_Insert(PyObject *op) { }
175void _PyGC_Remove(PyObject *op) { }
176#endif
177
Guido van Rossum90933611991-06-07 16:10:43 +0000178int
Fred Drake100814d2000-07-09 15:48:49 +0000179PyObject_Print(PyObject *op, FILE *fp, int flags)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000180{
Guido van Rossum278ef591991-07-27 21:40:24 +0000181 int ret = 0;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000182 if (PyErr_CheckSignals())
Guido van Rossum90933611991-06-07 16:10:43 +0000183 return -1;
Guido van Rossum9b00dfa1998-04-28 16:06:54 +0000184#ifdef USE_STACKCHECK
185 if (PyOS_CheckStack()) {
Fred Drake661ea262000-10-24 19:57:45 +0000186 PyErr_SetString(PyExc_MemoryError, "stack overflow");
Guido van Rossum9b00dfa1998-04-28 16:06:54 +0000187 return -1;
188 }
189#endif
Guido van Rossum687ef6e2000-01-12 16:28:58 +0000190 clearerr(fp); /* Clear any previous error condition */
Guido van Rossum90933611991-06-07 16:10:43 +0000191 if (op == NULL) {
192 fprintf(fp, "<nil>");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000193 }
Guido van Rossum90933611991-06-07 16:10:43 +0000194 else {
195 if (op->ob_refcnt <= 0)
Fred Drakea44d3532000-06-30 15:01:00 +0000196 fprintf(fp, "<refcnt %u at %p>",
197 op->ob_refcnt, op);
Guido van Rossum2e8f6141992-09-03 20:32:55 +0000198 else if (op->ob_type->tp_print == NULL) {
Guido van Rossum3a80c4a2001-04-27 21:35:01 +0000199 if ((flags & Py_PRINT_RAW)
200 ? (op->ob_type->tp_str == NULL)
201 : (op->ob_type->tp_repr == NULL))
202 {
Fred Drakea44d3532000-06-30 15:01:00 +0000203 fprintf(fp, "<%s object at %p>",
204 op->ob_type->tp_name, op);
Guido van Rossum2e8f6141992-09-03 20:32:55 +0000205 }
206 else {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000207 PyObject *s;
208 if (flags & Py_PRINT_RAW)
209 s = PyObject_Str(op);
Guido van Rossumc6004111993-11-05 10:22:19 +0000210 else
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000211 s = PyObject_Repr(op);
Guido van Rossum2e8f6141992-09-03 20:32:55 +0000212 if (s == NULL)
213 ret = -1;
Guido van Rossum2e8f6141992-09-03 20:32:55 +0000214 else {
Guido van Rossum565798d1998-04-21 22:25:01 +0000215 ret = PyObject_Print(s, fp,
216 Py_PRINT_RAW);
Guido van Rossum2e8f6141992-09-03 20:32:55 +0000217 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000218 Py_XDECREF(s);
Guido van Rossum2e8f6141992-09-03 20:32:55 +0000219 }
220 }
Guido van Rossum90933611991-06-07 16:10:43 +0000221 else
Guido van Rossum278ef591991-07-27 21:40:24 +0000222 ret = (*op->ob_type->tp_print)(op, fp, flags);
Guido van Rossum90933611991-06-07 16:10:43 +0000223 }
Guido van Rossum278ef591991-07-27 21:40:24 +0000224 if (ret == 0) {
225 if (ferror(fp)) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000226 PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossum278ef591991-07-27 21:40:24 +0000227 clearerr(fp);
228 ret = -1;
229 }
230 }
231 return ret;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000232}
233
Barry Warsaw9bf16442001-01-23 16:24:35 +0000234/* For debugging convenience. See Misc/gdbinit for some useful gdb hooks */
Barry Warsawbbd89b62001-01-24 04:18:13 +0000235void _PyObject_Dump(PyObject* op)
Barry Warsaw9bf16442001-01-23 16:24:35 +0000236{
Barry Warsaweefb1072001-02-22 22:39:18 +0000237 if (op == NULL)
238 fprintf(stderr, "NULL\n");
239 else {
240 (void)PyObject_Print(op, stderr, 0);
241 fprintf(stderr, "\nrefcounts: %d\n", op->ob_refcnt);
242 fprintf(stderr, "address : %p\n", op);
243 }
Barry Warsaw9bf16442001-01-23 16:24:35 +0000244}
Barry Warsaw903138f2001-01-23 16:33:18 +0000245
246#ifdef WITH_CYCLE_GC
Barry Warsawbbd89b62001-01-24 04:18:13 +0000247void _PyGC_Dump(PyGC_Head* op)
Barry Warsaw9bf16442001-01-23 16:24:35 +0000248{
Barry Warsawbbd89b62001-01-24 04:18:13 +0000249 _PyObject_Dump(PyObject_FROM_GC(op));
Barry Warsaw9bf16442001-01-23 16:24:35 +0000250}
Barry Warsaw903138f2001-01-23 16:33:18 +0000251#endif /* WITH_CYCLE_GC */
Barry Warsaw9bf16442001-01-23 16:24:35 +0000252
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000253PyObject *
Fred Drake100814d2000-07-09 15:48:49 +0000254PyObject_Repr(PyObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000255{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000256 if (PyErr_CheckSignals())
Guido van Rossum90933611991-06-07 16:10:43 +0000257 return NULL;
Guido van Rossum9b00dfa1998-04-28 16:06:54 +0000258#ifdef USE_STACKCHECK
259 if (PyOS_CheckStack()) {
Fred Drake661ea262000-10-24 19:57:45 +0000260 PyErr_SetString(PyExc_MemoryError, "stack overflow");
Guido van Rossum9b00dfa1998-04-28 16:06:54 +0000261 return NULL;
262 }
263#endif
Guido van Rossum90933611991-06-07 16:10:43 +0000264 if (v == NULL)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000265 return PyString_FromString("<NULL>");
Guido van Rossum90933611991-06-07 16:10:43 +0000266 else if (v->ob_type->tp_repr == NULL) {
267 char buf[120];
Fred Drakea44d3532000-06-30 15:01:00 +0000268 sprintf(buf, "<%.80s object at %p>",
269 v->ob_type->tp_name, v);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000270 return PyString_FromString(buf);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000271 }
Guido van Rossum4c08d552000-03-10 22:55:18 +0000272 else {
273 PyObject *res;
274 res = (*v->ob_type->tp_repr)(v);
275 if (res == NULL)
276 return NULL;
Fredrik Lundhefecc7d2000-07-01 14:31:09 +0000277 if (PyUnicode_Check(res)) {
278 PyObject* str;
Fredrik Lundh2a1e0602000-07-08 17:43:32 +0000279 str = PyUnicode_AsUnicodeEscapeString(res);
Marc-André Lemburg891bc652000-07-03 09:57:53 +0000280 Py_DECREF(res);
281 if (str)
Fredrik Lundhefecc7d2000-07-01 14:31:09 +0000282 res = str;
Marc-André Lemburg891bc652000-07-03 09:57:53 +0000283 else
284 return NULL;
Fredrik Lundhefecc7d2000-07-01 14:31:09 +0000285 }
Guido van Rossum4c08d552000-03-10 22:55:18 +0000286 if (!PyString_Check(res)) {
287 PyErr_Format(PyExc_TypeError,
Guido van Rossum5db862d2000-04-10 12:46:51 +0000288 "__repr__ returned non-string (type %.200s)",
Guido van Rossum4c08d552000-03-10 22:55:18 +0000289 res->ob_type->tp_name);
290 Py_DECREF(res);
291 return NULL;
292 }
293 return res;
294 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000295}
296
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000297PyObject *
Fred Drake100814d2000-07-09 15:48:49 +0000298PyObject_Str(PyObject *v)
Guido van Rossumc6004111993-11-05 10:22:19 +0000299{
Guido van Rossum4c08d552000-03-10 22:55:18 +0000300 PyObject *res;
301
Guido van Rossumc6004111993-11-05 10:22:19 +0000302 if (v == NULL)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000303 return PyString_FromString("<NULL>");
304 else if (PyString_Check(v)) {
305 Py_INCREF(v);
Guido van Rossumc6004111993-11-05 10:22:19 +0000306 return v;
307 }
Guido van Rossum32b582b1995-01-17 16:35:13 +0000308 else if (v->ob_type->tp_str != NULL)
Guido van Rossum4c08d552000-03-10 22:55:18 +0000309 res = (*v->ob_type->tp_str)(v);
Guido van Rossumc6004111993-11-05 10:22:19 +0000310 else {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000311 PyObject *func;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000312 if (!PyInstance_Check(v) ||
313 (func = PyObject_GetAttrString(v, "__str__")) == NULL) {
314 PyErr_Clear();
315 return PyObject_Repr(v);
Guido van Rossumc6004111993-11-05 10:22:19 +0000316 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000317 res = PyEval_CallObject(func, (PyObject *)NULL);
318 Py_DECREF(func);
Guido van Rossumc6004111993-11-05 10:22:19 +0000319 }
Guido van Rossum4c08d552000-03-10 22:55:18 +0000320 if (res == NULL)
321 return NULL;
Marc-André Lemburg891bc652000-07-03 09:57:53 +0000322 if (PyUnicode_Check(res)) {
323 PyObject* str;
324 str = PyUnicode_AsEncodedString(res, NULL, NULL);
325 Py_DECREF(res);
326 if (str)
327 res = str;
328 else
329 return NULL;
330 }
Guido van Rossum4c08d552000-03-10 22:55:18 +0000331 if (!PyString_Check(res)) {
332 PyErr_Format(PyExc_TypeError,
Guido van Rossum5db862d2000-04-10 12:46:51 +0000333 "__str__ returned non-string (type %.200s)",
Guido van Rossum4c08d552000-03-10 22:55:18 +0000334 res->ob_type->tp_name);
335 Py_DECREF(res);
336 return NULL;
337 }
338 return res;
Guido van Rossumc6004111993-11-05 10:22:19 +0000339}
340
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000341PyObject *
342PyObject_Unicode(PyObject *v)
343{
344 PyObject *res;
345
346 if (v == NULL)
347 res = PyString_FromString("<NULL>");
348 else if (PyUnicode_Check(v)) {
349 Py_INCREF(v);
350 return v;
351 }
Marc-André Lemburgae605342001-03-25 19:16:13 +0000352 else if (PyString_Check(v)) {
353 Py_INCREF(v);
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000354 res = v;
Marc-André Lemburgae605342001-03-25 19:16:13 +0000355 }
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000356 else if (v->ob_type->tp_str != NULL)
357 res = (*v->ob_type->tp_str)(v);
358 else {
359 PyObject *func;
360 static PyObject *strstr;
361 if (strstr == NULL) {
362 strstr= PyString_InternFromString("__str__");
363 if (strstr == NULL)
364 return NULL;
365 }
366 if (!PyInstance_Check(v) ||
367 (func = PyObject_GetAttr(v, strstr)) == NULL) {
368 PyErr_Clear();
369 res = PyObject_Repr(v);
370 }
371 else {
372 res = PyEval_CallObject(func, (PyObject *)NULL);
373 Py_DECREF(func);
374 }
375 }
376 if (res == NULL)
377 return NULL;
378 if (!PyUnicode_Check(res)) {
379 PyObject* str;
380 str = PyUnicode_FromObject(res);
381 Py_DECREF(res);
382 if (str)
383 res = str;
384 else
385 return NULL;
386 }
387 return res;
388}
389
390
Guido van Rossumd1f06b92001-01-24 22:14:43 +0000391/* Macro to get the tp_richcompare field of a type if defined */
392#define RICHCOMPARE(t) (PyType_HasFeature((t), Py_TPFLAGS_HAVE_RICHCOMPARE) \
393 ? (t)->tp_richcompare : NULL)
394
Guido van Rossume797ec12001-01-17 15:24:28 +0000395/* Map rich comparison operators to their swapped version, e.g. LT --> GT */
396static int swapped_op[] = {Py_GT, Py_GE, Py_EQ, Py_NE, Py_LT, Py_LE};
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +0000397
Guido van Rossume797ec12001-01-17 15:24:28 +0000398/* Try a genuine rich comparison, returning an object. Return:
399 NULL for exception;
400 NotImplemented if this particular rich comparison is not implemented or
401 undefined;
402 some object not equal to NotImplemented if it is implemented
403 (this latter object may not be a Boolean).
404*/
405static PyObject *
406try_rich_compare(PyObject *v, PyObject *w, int op)
407{
408 richcmpfunc f;
409 PyObject *res;
410
Guido van Rossumd1f06b92001-01-24 22:14:43 +0000411 if ((f = RICHCOMPARE(v->ob_type)) != NULL) {
Guido van Rossume797ec12001-01-17 15:24:28 +0000412 res = (*f)(v, w, op);
413 if (res != Py_NotImplemented)
414 return res;
415 Py_DECREF(res);
416 }
Guido van Rossumd1f06b92001-01-24 22:14:43 +0000417 if ((f = RICHCOMPARE(w->ob_type)) != NULL) {
Guido van Rossume797ec12001-01-17 15:24:28 +0000418 return (*f)(w, v, swapped_op[op]);
419 }
420 res = Py_NotImplemented;
421 Py_INCREF(res);
422 return res;
423}
424
425/* Try a genuine rich comparison, returning an int. Return:
426 -1 for exception (including the case where try_rich_compare() returns an
427 object that's not a Boolean);
428 0 if the outcome is false;
429 1 if the outcome is true;
430 2 if this particular rich comparison is not implemented or undefined.
431*/
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +0000432static int
Guido van Rossume797ec12001-01-17 15:24:28 +0000433try_rich_compare_bool(PyObject *v, PyObject *w, int op)
434{
435 PyObject *res;
436 int ok;
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, avoid INCREF+DECREF */
440 res = try_rich_compare(v, w, op);
441 if (res == NULL)
442 return -1;
443 if (res == Py_NotImplemented) {
444 Py_DECREF(res);
445 return 2;
446 }
447 ok = PyObject_IsTrue(res);
448 Py_DECREF(res);
449 return ok;
450}
451
452/* Try rich comparisons to determine a 3-way comparison. Return:
453 -2 for an exception;
454 -1 if v < w;
455 0 if v == w;
456 1 if v > w;
457 2 if this particular rich comparison is not implemented or undefined.
458*/
459static int
460try_rich_to_3way_compare(PyObject *v, PyObject *w)
461{
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000462 static struct { int op; int outcome; } tries[3] = {
463 /* Try this operator, and if it is true, use this outcome: */
464 {Py_EQ, 0},
465 {Py_LT, -1},
466 {Py_GT, 1},
467 };
468 int i;
469
Guido van Rossumd1f06b92001-01-24 22:14:43 +0000470 if (RICHCOMPARE(v->ob_type) == NULL && RICHCOMPARE(w->ob_type) == NULL)
Guido van Rossume797ec12001-01-17 15:24:28 +0000471 return 2; /* Shortcut */
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000472
473 for (i = 0; i < 3; i++) {
474 switch (try_rich_compare_bool(v, w, tries[i].op)) {
475 case -1:
476 return -1;
477 case 1:
478 return tries[i].outcome;
479 }
Guido van Rossume797ec12001-01-17 15:24:28 +0000480 }
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000481
482 return 2;
Guido van Rossume797ec12001-01-17 15:24:28 +0000483}
484
485/* Try a 3-way comparison, returning an int. Return:
486 -2 for an exception;
487 -1 if v < w;
488 0 if v == w;
489 1 if v > w;
490 2 if this particular 3-way comparison is not implemented or undefined.
491*/
492static int
493try_3way_compare(PyObject *v, PyObject *w)
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +0000494{
495 int c;
Guido van Rossume797ec12001-01-17 15:24:28 +0000496 cmpfunc f;
497
498 /* Comparisons involving instances are given to instance_compare,
499 which has the same return conventions as this function. */
500
501 if (PyInstance_Check(v))
502 return (*v->ob_type->tp_compare)(v, w);
503 if (PyInstance_Check(w))
504 return (*w->ob_type->tp_compare)(v, w);
505
506 /* If the types are equal, don't bother with coercions etc. */
507 if (v->ob_type == w->ob_type) {
508 if ((f = v->ob_type->tp_compare) == NULL)
509 return 2;
510 c = (*f)(v, w);
511 if (PyErr_Occurred())
512 return -2;
513 return c < 0 ? -1 : c > 0 ? 1 : 0;
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +0000514 }
Guido van Rossume797ec12001-01-17 15:24:28 +0000515
516 /* Try coercion; if it fails, give up */
517 c = PyNumber_CoerceEx(&v, &w);
518 if (c < 0)
519 return -2;
520 if (c > 0)
521 return 2;
522
523 /* Try v's comparison, if defined */
524 if ((f = v->ob_type->tp_compare) != NULL) {
525 c = (*f)(v, w);
526 Py_DECREF(v);
527 Py_DECREF(w);
528 if (PyErr_Occurred())
529 return -2;
530 return c < 0 ? -1 : c > 0 ? 1 : 0;
531 }
532
533 /* Try w's comparison, if defined */
534 if ((f = w->ob_type->tp_compare) != NULL) {
535 c = (*f)(w, v); /* swapped! */
536 Py_DECREF(v);
537 Py_DECREF(w);
538 if (PyErr_Occurred())
539 return -2;
540 return c < 0 ? 1 : c > 0 ? -1 : 0; /* negated! */
541 }
542
543 /* No comparison defined */
544 Py_DECREF(v);
545 Py_DECREF(w);
546 return 2;
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +0000547}
548
Guido van Rossume797ec12001-01-17 15:24:28 +0000549/* Final fallback 3-way comparison, returning an int. Return:
550 -2 if an error occurred;
551 -1 if v < w;
552 0 if v == w;
553 1 if v > w.
554*/
555static int
556default_3way_compare(PyObject *v, PyObject *w)
557{
558 int c;
Guido van Rossum8f9143d2001-01-22 15:59:32 +0000559 char *vname, *wname;
Guido van Rossume797ec12001-01-17 15:24:28 +0000560
561 if (v->ob_type == w->ob_type) {
Barry Warsawb0e754d2001-01-20 06:24:55 +0000562 /* When comparing these pointers, they must be cast to
563 * integer types (i.e. Py_uintptr_t, our spelling of C9X's
564 * uintptr_t). ANSI specifies that pointer compares other
565 * than == and != to non-related structures are undefined.
566 */
Barry Warsaw71ff8d52001-01-20 06:08:10 +0000567 Py_uintptr_t vv = (Py_uintptr_t)v;
568 Py_uintptr_t ww = (Py_uintptr_t)w;
Guido van Rossume797ec12001-01-17 15:24:28 +0000569 return (vv < ww) ? -1 : (vv > ww) ? 1 : 0;
570 }
571
572 /* Special case for Unicode */
573 if (PyUnicode_Check(v) || PyUnicode_Check(w)) {
574 c = PyUnicode_Compare(v, w);
575 if (!PyErr_Occurred())
576 return c;
577 /* TypeErrors are ignored: if Unicode coercion fails due
578 to one of the arguments not having the right type, we
579 continue as defined by the coercion protocol (see
580 above). Luckily, decoding errors are reported as
581 ValueErrors and are not masked by this technique. */
582 if (!PyErr_ExceptionMatches(PyExc_TypeError))
583 return -2;
584 PyErr_Clear();
585 }
586
Guido van Rossum0871e932001-01-22 19:28:09 +0000587 /* None is smaller than anything */
588 if (v == Py_None)
589 return -1;
590 if (w == Py_None)
591 return 1;
592
Guido van Rossume797ec12001-01-17 15:24:28 +0000593 /* different type: compare type names */
Guido van Rossum8f9143d2001-01-22 15:59:32 +0000594 if (v->ob_type->tp_as_number)
595 vname = "";
596 else
597 vname = v->ob_type->tp_name;
598 if (w->ob_type->tp_as_number)
599 wname = "";
600 else
601 wname = w->ob_type->tp_name;
602 c = strcmp(vname, wname);
603 if (c < 0)
604 return -1;
605 if (c > 0)
606 return 1;
607 /* Same type name, or (more likely) incomparable numeric types */
608 return ((Py_uintptr_t)(v->ob_type) < (
609 Py_uintptr_t)(w->ob_type)) ? -1 : 1;
Guido van Rossume797ec12001-01-17 15:24:28 +0000610}
611
612#define CHECK_TYPES(o) PyType_HasFeature((o)->ob_type, Py_TPFLAGS_CHECKTYPES)
613
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +0000614static int
Fred Drake100814d2000-07-09 15:48:49 +0000615do_cmp(PyObject *v, PyObject *w)
Guido van Rossum20566841995-01-12 11:26:10 +0000616{
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +0000617 int c;
618
Guido van Rossume797ec12001-01-17 15:24:28 +0000619 c = try_rich_to_3way_compare(v, w);
620 if (c < 2)
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +0000621 return c;
Guido van Rossume797ec12001-01-17 15:24:28 +0000622 c = try_3way_compare(v, w);
623 if (c < 2)
624 return c;
625 return default_3way_compare(v, w);
Guido van Rossum20566841995-01-12 11:26:10 +0000626}
627
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000628/* compare_nesting is incremented before calling compare (for
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000629 some types) and decremented on exit. If the count exceeds the
630 nesting limit, enable code to detect circular data structures.
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000631
632 This is a tunable parameter that should only affect the performance
633 of comparisons, nothing else. Setting it high makes comparing deeply
634 nested non-cyclical data structures faster, but makes comparing cyclical
635 data structures slower.
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000636*/
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000637#define NESTING_LIMIT 20
638
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000639static int compare_nesting = 0;
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000640
641static PyObject*
Fred Drake100814d2000-07-09 15:48:49 +0000642get_inprogress_dict(void)
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000643{
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000644 static PyObject *key;
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000645 PyObject *tstate_dict, *inprogress;
646
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000647 if (key == NULL) {
648 key = PyString_InternFromString("cmp_state");
649 if (key == NULL)
650 return NULL;
651 }
652
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000653 tstate_dict = PyThreadState_GetDict();
654 if (tstate_dict == NULL) {
655 PyErr_BadInternalCall();
656 return NULL;
657 }
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000658
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000659 inprogress = PyDict_GetItem(tstate_dict, key);
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000660 if (inprogress == NULL) {
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000661 inprogress = PyDict_New();
662 if (inprogress == NULL)
663 return NULL;
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000664 if (PyDict_SetItem(tstate_dict, key, inprogress) == -1) {
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000665 Py_DECREF(inprogress);
666 return NULL;
667 }
Jeremy Hyltona251ea02000-06-09 16:20:39 +0000668 Py_DECREF(inprogress);
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000669 }
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000670
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000671 return inprogress;
672}
673
674static PyObject *
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000675check_recursion(PyObject *v, PyObject *w, int op)
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000676{
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000677 PyObject *inprogress;
678 PyObject *token;
Barry Warsaw9d23a4e2000-08-18 05:01:19 +0000679 Py_uintptr_t iv = (Py_uintptr_t)v;
680 Py_uintptr_t iw = (Py_uintptr_t)w;
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000681 PyObject *x, *y, *z;
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000682
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000683 inprogress = get_inprogress_dict();
684 if (inprogress == NULL)
685 return NULL;
686
687 token = PyTuple_New(3);
688 if (token == NULL)
689 return NULL;
690
691 if (iv <= iw) {
692 PyTuple_SET_ITEM(token, 0, x = PyLong_FromVoidPtr((void *)v));
693 PyTuple_SET_ITEM(token, 1, y = PyLong_FromVoidPtr((void *)w));
694 if (op >= 0)
695 op = swapped_op[op];
696 } else {
697 PyTuple_SET_ITEM(token, 0, x = PyLong_FromVoidPtr((void *)w));
698 PyTuple_SET_ITEM(token, 1, y = PyLong_FromVoidPtr((void *)v));
699 }
700 PyTuple_SET_ITEM(token, 2, z = PyInt_FromLong((long)op));
701 if (x == NULL || y == NULL || z == NULL) {
702 Py_DECREF(token);
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000703 return NULL;
704 }
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000705
706 if (PyDict_GetItem(inprogress, token) != NULL) {
707 Py_DECREF(token);
708 return Py_None; /* Without INCREF! */
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000709 }
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000710
711 if (PyDict_SetItem(inprogress, token, token) < 0) {
712 Py_DECREF(token);
713 return NULL;
714 }
715
716 return token;
717}
718
719static void
720delete_token(PyObject *token)
721{
722 PyObject *inprogress;
723
724 if (token == NULL || token == Py_None)
725 return;
726 inprogress = get_inprogress_dict();
727 if (inprogress == NULL)
728 PyErr_Clear();
729 else
730 PyDict_DelItem(inprogress, token);
731 Py_DECREF(token);
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000732}
733
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000734int
Fred Drake100814d2000-07-09 15:48:49 +0000735PyObject_Compare(PyObject *v, PyObject *w)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000736{
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000737 PyTypeObject *vtp;
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000738 int result;
739
Jack Jansend49cbe12000-08-22 21:52:51 +0000740#if defined(USE_STACKCHECK)
Guido van Rossum9c0a99e2000-08-30 15:53:50 +0000741 if (PyOS_CheckStack()) {
Jack Jansend49cbe12000-08-22 21:52:51 +0000742 PyErr_SetString(PyExc_MemoryError, "Stack overflow");
743 return -1;
744 }
745#endif
Guido van Rossumc8b6df91997-05-23 00:06:51 +0000746 if (v == NULL || w == NULL) {
747 PyErr_BadInternalCall();
748 return -1;
749 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000750 if (v == w)
751 return 0;
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +0000752 vtp = v->ob_type;
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000753 compare_nesting++;
754 if (compare_nesting > NESTING_LIMIT &&
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +0000755 (vtp->tp_as_mapping
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000756 || (vtp->tp_as_sequence
757 && !PyString_Check(v)
758 && !PyTuple_Check(v)))) {
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +0000759 /* try to detect circular data structures */
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000760 PyObject *token = check_recursion(v, w, -1);
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000761
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000762 if (token == NULL) {
763 result = -1;
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000764 }
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000765 else if (token == Py_None) {
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000766 /* already comparing these objects. assume
767 they're equal until shown otherwise */
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +0000768 result = 0;
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000769 }
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000770 else {
771 result = do_cmp(v, w);
772 delete_token(token);
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000773 }
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000774 }
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +0000775 else {
776 result = do_cmp(v, w);
777 }
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000778 compare_nesting--;
Guido van Rossume797ec12001-01-17 15:24:28 +0000779 return result < 0 ? -1 : result;
780}
781
782static PyObject *
783try_3way_to_rich_compare(PyObject *v, PyObject *w, int op)
784{
785 int c;
786 PyObject *result;
787
788 c = try_3way_compare(v, w);
Guido van Rossume797ec12001-01-17 15:24:28 +0000789 if (c >= 2)
790 c = default_3way_compare(v, w);
Guido van Rossum2da0ea82001-02-22 22:18:04 +0000791 if (c <= -2)
792 return NULL;
Guido van Rossume797ec12001-01-17 15:24:28 +0000793 switch (op) {
794 case Py_LT: c = c < 0; break;
795 case Py_LE: c = c <= 0; break;
796 case Py_EQ: c = c == 0; break;
797 case Py_NE: c = c != 0; break;
798 case Py_GT: c = c > 0; break;
799 case Py_GE: c = c >= 0; break;
800 }
801 result = c ? Py_True : Py_False;
802 Py_INCREF(result);
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000803 return result;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000804}
805
Neil Schemenauerd38855c2001-01-21 16:25:18 +0000806static PyObject *
Guido van Rossume797ec12001-01-17 15:24:28 +0000807do_richcmp(PyObject *v, PyObject *w, int op)
808{
809 PyObject *res;
810
811 res = try_rich_compare(v, w, op);
812 if (res != Py_NotImplemented)
813 return res;
814 Py_DECREF(res);
815
816 return try_3way_to_rich_compare(v, w, op);
817}
818
819PyObject *
820PyObject_RichCompare(PyObject *v, PyObject *w, int op)
821{
822 PyObject *res;
823
824 assert(Py_LT <= op && op <= Py_GE);
825
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000826 compare_nesting++;
827 if (compare_nesting > NESTING_LIMIT &&
828 (v->ob_type->tp_as_mapping
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000829 || (v->ob_type->tp_as_sequence
830 && !PyString_Check(v)
831 && !PyTuple_Check(v)))) {
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000832 /* try to detect circular data structures */
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000833 PyObject *token = check_recursion(v, w, op);
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000834
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000835 if (token == NULL) {
836 res = NULL;
Guido van Rossume797ec12001-01-17 15:24:28 +0000837 }
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000838 else if (token == Py_None) {
839 /* already comparing these objects with this operator.
840 assume they're equal until shown otherwise */
841 if (op == Py_EQ)
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000842 res = Py_True;
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000843 else if (op == Py_NE)
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000844 res = Py_False;
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000845 else {
846 PyErr_SetString(PyExc_ValueError,
847 "can't order recursive values");
848 res = NULL;
849 }
850 Py_XINCREF(res);
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000851 }
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000852 else {
853 res = do_richcmp(v, w, op);
854 delete_token(token);
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000855 }
Guido van Rossume797ec12001-01-17 15:24:28 +0000856 }
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000857 else {
858 res = do_richcmp(v, w, op);
859 }
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000860 compare_nesting--;
Guido van Rossume797ec12001-01-17 15:24:28 +0000861 return res;
862}
863
864int
865PyObject_RichCompareBool(PyObject *v, PyObject *w, int op)
866{
867 PyObject *res = PyObject_RichCompare(v, w, op);
868 int ok;
869
870 if (res == NULL)
871 return -1;
872 ok = PyObject_IsTrue(res);
873 Py_DECREF(res);
874 return ok;
875}
Fred Drake13634cf2000-06-29 19:17:04 +0000876
877/* Set of hash utility functions to help maintaining the invariant that
878 iff a==b then hash(a)==hash(b)
879
880 All the utility functions (_Py_Hash*()) return "-1" to signify an error.
881*/
882
883long
Fred Drake100814d2000-07-09 15:48:49 +0000884_Py_HashDouble(double v)
Fred Drake13634cf2000-06-29 19:17:04 +0000885{
Tim Peters39dce292000-08-15 03:34:48 +0000886 double intpart, fractpart;
887 int expo;
888 long hipart;
889 long x; /* the final hash value */
890 /* This is designed so that Python numbers of different types
891 * that compare equal hash to the same value; otherwise comparisons
892 * of mapping keys will turn out weird.
893 */
894
895#ifdef MPW /* MPW C modf expects pointer to extended as second argument */
896{
897 extended e;
898 fractpart = modf(v, &e);
899 intpart = e;
900}
901#else
902 fractpart = modf(v, &intpart);
903#endif
904 if (fractpart == 0.0) {
905 /* This must return the same hash as an equal int or long. */
906 if (intpart > LONG_MAX || -intpart > LONG_MAX) {
907 /* Convert to long and use its hash. */
908 PyObject *plong; /* converted to Python long */
909 if (Py_IS_INFINITY(intpart))
910 /* can't convert to long int -- arbitrary */
911 v = v < 0 ? -271828.0 : 314159.0;
912 plong = PyLong_FromDouble(v);
913 if (plong == NULL)
914 return -1;
915 x = PyObject_Hash(plong);
916 Py_DECREF(plong);
917 return x;
918 }
919 /* Fits in a C long == a Python int, so is its own hash. */
920 x = (long)intpart;
921 if (x == -1)
922 x = -2;
923 return x;
924 }
925 /* The fractional part is non-zero, so we don't have to worry about
926 * making this match the hash of some other type.
927 * Use frexp to get at the bits in the double.
Fred Drake13634cf2000-06-29 19:17:04 +0000928 * Since the VAX D double format has 56 mantissa bits, which is the
929 * most of any double format in use, each of these parts may have as
930 * many as (but no more than) 56 significant bits.
Tim Peters39dce292000-08-15 03:34:48 +0000931 * So, assuming sizeof(long) >= 4, each part can be broken into two
932 * longs; frexp and multiplication are used to do that.
933 * Also, since the Cray double format has 15 exponent bits, which is
934 * the most of any double format in use, shifting the exponent field
935 * left by 15 won't overflow a long (again assuming sizeof(long) >= 4).
Fred Drake13634cf2000-06-29 19:17:04 +0000936 */
Tim Peters39dce292000-08-15 03:34:48 +0000937 v = frexp(v, &expo);
938 v *= 2147483648.0; /* 2**31 */
939 hipart = (long)v; /* take the top 32 bits */
940 v = (v - (double)hipart) * 2147483648.0; /* get the next 32 bits */
941 x = hipart + (long)v + (expo << 15);
942 if (x == -1)
943 x = -2;
944 return x;
Fred Drake13634cf2000-06-29 19:17:04 +0000945}
946
947long
Fred Drake100814d2000-07-09 15:48:49 +0000948_Py_HashPointer(void *p)
Fred Drake13634cf2000-06-29 19:17:04 +0000949{
950#if SIZEOF_LONG >= SIZEOF_VOID_P
951 return (long)p;
952#else
953 /* convert to a Python long and hash that */
954 PyObject* longobj;
955 long x;
956
957 if ((longobj = PyLong_FromVoidPtr(p)) == NULL) {
958 x = -1;
959 goto finally;
960 }
961 x = PyObject_Hash(longobj);
962
963finally:
964 Py_XDECREF(longobj);
965 return x;
966#endif
967}
968
969
Guido van Rossum9bfef441993-03-29 10:43:31 +0000970long
Fred Drake100814d2000-07-09 15:48:49 +0000971PyObject_Hash(PyObject *v)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000972{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000973 PyTypeObject *tp = v->ob_type;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000974 if (tp->tp_hash != NULL)
975 return (*tp->tp_hash)(v);
Guido van Rossumd1f06b92001-01-24 22:14:43 +0000976 if (tp->tp_compare == NULL && RICHCOMPARE(tp) == NULL) {
Fred Drake13634cf2000-06-29 19:17:04 +0000977 return _Py_HashPointer(v); /* Use address as hash value */
978 }
Guido van Rossum9bfef441993-03-29 10:43:31 +0000979 /* If there's a cmp but no hash defined, the object can't be hashed */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000980 PyErr_SetString(PyExc_TypeError, "unhashable type");
Guido van Rossum9bfef441993-03-29 10:43:31 +0000981 return -1;
982}
983
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000984PyObject *
Fred Drake100814d2000-07-09 15:48:49 +0000985PyObject_GetAttrString(PyObject *v, char *name)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000986{
Guido van Rossumd8eb1b31996-08-09 20:52:03 +0000987 if (v->ob_type->tp_getattro != NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000988 PyObject *w, *res;
Guido van Rossumc6d06701997-01-18 07:57:16 +0000989 w = PyString_InternFromString(name);
Guido van Rossumd8eb1b31996-08-09 20:52:03 +0000990 if (w == NULL)
991 return NULL;
992 res = (*v->ob_type->tp_getattro)(v, w);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000993 Py_XDECREF(w);
Guido van Rossumd8eb1b31996-08-09 20:52:03 +0000994 return res;
995 }
996
Guido van Rossum3f5da241990-12-20 15:06:42 +0000997 if (v->ob_type->tp_getattr == NULL) {
Guido van Rossumdb935161998-01-19 22:16:36 +0000998 PyErr_Format(PyExc_AttributeError,
999 "'%.50s' object has no attribute '%.400s'",
1000 v->ob_type->tp_name,
1001 name);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001002 return NULL;
1003 }
1004 else {
1005 return (*v->ob_type->tp_getattr)(v, name);
1006 }
1007}
1008
1009int
Fred Drake100814d2000-07-09 15:48:49 +00001010PyObject_HasAttrString(PyObject *v, char *name)
Guido van Rossumed18fdc1993-07-11 19:55:34 +00001011{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001012 PyObject *res = PyObject_GetAttrString(v, name);
Guido van Rossumed18fdc1993-07-11 19:55:34 +00001013 if (res != NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001014 Py_DECREF(res);
Guido van Rossumed18fdc1993-07-11 19:55:34 +00001015 return 1;
1016 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001017 PyErr_Clear();
Guido van Rossumed18fdc1993-07-11 19:55:34 +00001018 return 0;
1019}
1020
1021int
Fred Drake100814d2000-07-09 15:48:49 +00001022PyObject_SetAttrString(PyObject *v, char *name, PyObject *w)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001023{
Guido van Rossumd8eb1b31996-08-09 20:52:03 +00001024 if (v->ob_type->tp_setattro != NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001025 PyObject *s;
Guido van Rossumd8eb1b31996-08-09 20:52:03 +00001026 int res;
Guido van Rossumc6d06701997-01-18 07:57:16 +00001027 s = PyString_InternFromString(name);
Guido van Rossumd8eb1b31996-08-09 20:52:03 +00001028 if (s == NULL)
Guido van Rossumb7fc3041996-09-11 22:51:25 +00001029 return -1;
Guido van Rossumd8eb1b31996-08-09 20:52:03 +00001030 res = (*v->ob_type->tp_setattro)(v, s, w);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001031 Py_XDECREF(s);
Guido van Rossumd8eb1b31996-08-09 20:52:03 +00001032 return res;
1033 }
1034
Guido van Rossum3f5da241990-12-20 15:06:42 +00001035 if (v->ob_type->tp_setattr == NULL) {
1036 if (v->ob_type->tp_getattr == NULL)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001037 PyErr_SetString(PyExc_TypeError,
Guido van Rossum3ea74121991-12-24 13:28:03 +00001038 "attribute-less object (assign or del)");
Guido van Rossum3f5da241990-12-20 15:06:42 +00001039 else
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001040 PyErr_SetString(PyExc_TypeError,
Guido van Rossum3ea74121991-12-24 13:28:03 +00001041 "object has read-only attributes");
Guido van Rossum73531a31990-12-20 23:12:40 +00001042 return -1;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001043 }
1044 else {
1045 return (*v->ob_type->tp_setattr)(v, name, w);
1046 }
1047}
1048
Marc-André Lemburge44e5072000-09-18 16:20:57 +00001049/* Internal API needed by PyObject_GetAttr(): */
1050extern
1051PyObject *_PyUnicode_AsDefaultEncodedString(PyObject *unicode,
1052 const char *errors);
1053
Guido van Rossum98ff96a1997-05-20 18:34:44 +00001054PyObject *
Fred Drake100814d2000-07-09 15:48:49 +00001055PyObject_GetAttr(PyObject *v, PyObject *name)
Guido van Rossum98ff96a1997-05-20 18:34:44 +00001056{
Marc-André Lemburge44e5072000-09-18 16:20:57 +00001057 /* The Unicode to string conversion is done here because the
1058 existing tp_getattro slots expect a string object as name
1059 and we wouldn't want to break those. */
1060 if (PyUnicode_Check(name)) {
1061 name = _PyUnicode_AsDefaultEncodedString(name, NULL);
1062 if (name == NULL)
1063 return NULL;
1064 }
Jeremy Hylton99a8f902000-06-23 14:36:32 +00001065
1066 if (!PyString_Check(name)) {
1067 PyErr_SetString(PyExc_TypeError,
1068 "attribute name must be string");
1069 return NULL;
1070 }
Marc-André Lemburge44e5072000-09-18 16:20:57 +00001071 if (v->ob_type->tp_getattro != NULL)
1072 return (*v->ob_type->tp_getattro)(v, name);
1073 else
Jeremy Hylton99a8f902000-06-23 14:36:32 +00001074 return PyObject_GetAttrString(v, PyString_AS_STRING(name));
Guido van Rossum98ff96a1997-05-20 18:34:44 +00001075}
1076
1077int
Fred Drake100814d2000-07-09 15:48:49 +00001078PyObject_HasAttr(PyObject *v, PyObject *name)
Guido van Rossum98ff96a1997-05-20 18:34:44 +00001079{
1080 PyObject *res = PyObject_GetAttr(v, name);
1081 if (res != NULL) {
1082 Py_DECREF(res);
1083 return 1;
1084 }
1085 PyErr_Clear();
1086 return 0;
1087}
1088
1089int
Fred Drake100814d2000-07-09 15:48:49 +00001090PyObject_SetAttr(PyObject *v, PyObject *name, PyObject *value)
Guido van Rossum98ff96a1997-05-20 18:34:44 +00001091{
1092 int err;
Marc-André Lemburge44e5072000-09-18 16:20:57 +00001093
1094 /* The Unicode to string conversion is done here because the
1095 existing tp_setattro slots expect a string object as name
1096 and we wouldn't want to break those. */
1097 if (PyUnicode_Check(name)) {
1098 name = PyUnicode_AsEncodedString(name, NULL, NULL);
1099 if (name == NULL)
1100 return -1;
Jeremy Hylton99a8f902000-06-23 14:36:32 +00001101 }
Marc-André Lemburge44e5072000-09-18 16:20:57 +00001102 else
1103 Py_INCREF(name);
1104
1105 if (!PyString_Check(name)){
Jeremy Hylton99a8f902000-06-23 14:36:32 +00001106 PyErr_SetString(PyExc_TypeError,
1107 "attribute name must be string");
1108 err = -1;
1109 }
Marc-André Lemburge44e5072000-09-18 16:20:57 +00001110 else {
1111 PyString_InternInPlace(&name);
1112 if (v->ob_type->tp_setattro != NULL)
1113 err = (*v->ob_type->tp_setattro)(v, name, value);
1114 else
1115 err = PyObject_SetAttrString(v,
1116 PyString_AS_STRING(name), value);
1117 }
1118
Guido van Rossum98ff96a1997-05-20 18:34:44 +00001119 Py_DECREF(name);
1120 return err;
1121}
1122
Guido van Rossum6ac258d1993-05-12 08:24:20 +00001123/* Test a value used as condition, e.g., in a for or if statement.
1124 Return -1 if an error occurred */
1125
1126int
Fred Drake100814d2000-07-09 15:48:49 +00001127PyObject_IsTrue(PyObject *v)
Guido van Rossum6ac258d1993-05-12 08:24:20 +00001128{
1129 int res;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001130 if (v == Py_None)
Guido van Rossum6ac258d1993-05-12 08:24:20 +00001131 res = 0;
Guido van Rossum1c4f4581998-05-22 00:53:24 +00001132 else if (v->ob_type->tp_as_number != NULL &&
1133 v->ob_type->tp_as_number->nb_nonzero != NULL)
Guido van Rossum6ac258d1993-05-12 08:24:20 +00001134 res = (*v->ob_type->tp_as_number->nb_nonzero)(v);
Guido van Rossum1c4f4581998-05-22 00:53:24 +00001135 else if (v->ob_type->tp_as_mapping != NULL &&
1136 v->ob_type->tp_as_mapping->mp_length != NULL)
Guido van Rossum6ac258d1993-05-12 08:24:20 +00001137 res = (*v->ob_type->tp_as_mapping->mp_length)(v);
Guido van Rossum1c4f4581998-05-22 00:53:24 +00001138 else if (v->ob_type->tp_as_sequence != NULL &&
1139 v->ob_type->tp_as_sequence->sq_length != NULL)
Guido van Rossum6ac258d1993-05-12 08:24:20 +00001140 res = (*v->ob_type->tp_as_sequence->sq_length)(v);
1141 else
1142 res = 1;
1143 if (res > 0)
1144 res = 1;
1145 return res;
1146}
1147
Guido van Rossumc3d3f961998-04-09 17:53:59 +00001148/* equivalent of 'not v'
1149 Return -1 if an error occurred */
1150
1151int
Fred Drake100814d2000-07-09 15:48:49 +00001152PyObject_Not(PyObject *v)
Guido van Rossumc3d3f961998-04-09 17:53:59 +00001153{
1154 int res;
1155 res = PyObject_IsTrue(v);
1156 if (res < 0)
1157 return res;
1158 return res == 0;
1159}
1160
Guido van Rossum5524a591995-01-10 15:26:20 +00001161/* Coerce two numeric types to the "larger" one.
1162 Increment the reference count on each argument.
Guido van Rossume797ec12001-01-17 15:24:28 +00001163 Return value:
1164 -1 if an error occurred;
1165 0 if the coercion succeeded (and then the reference counts are increased);
1166 1 if no coercion is possible (and no error is raised).
Guido van Rossum5524a591995-01-10 15:26:20 +00001167*/
Guido van Rossum5524a591995-01-10 15:26:20 +00001168int
Fred Drake100814d2000-07-09 15:48:49 +00001169PyNumber_CoerceEx(PyObject **pv, PyObject **pw)
Guido van Rossum5524a591995-01-10 15:26:20 +00001170{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001171 register PyObject *v = *pv;
1172 register PyObject *w = *pw;
Guido van Rossum5524a591995-01-10 15:26:20 +00001173 int res;
1174
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001175 if (v->ob_type == w->ob_type && !PyInstance_Check(v)) {
1176 Py_INCREF(v);
1177 Py_INCREF(w);
Guido van Rossum5524a591995-01-10 15:26:20 +00001178 return 0;
1179 }
1180 if (v->ob_type->tp_as_number && v->ob_type->tp_as_number->nb_coerce) {
1181 res = (*v->ob_type->tp_as_number->nb_coerce)(pv, pw);
1182 if (res <= 0)
1183 return res;
1184 }
1185 if (w->ob_type->tp_as_number && w->ob_type->tp_as_number->nb_coerce) {
1186 res = (*w->ob_type->tp_as_number->nb_coerce)(pw, pv);
1187 if (res <= 0)
1188 return res;
1189 }
Guido van Rossum242c6421997-11-19 16:03:17 +00001190 return 1;
1191}
1192
Guido van Rossume797ec12001-01-17 15:24:28 +00001193/* Coerce two numeric types to the "larger" one.
1194 Increment the reference count on each argument.
1195 Return -1 and raise an exception if no coercion is possible
1196 (and then no reference count is incremented).
1197*/
Guido van Rossum242c6421997-11-19 16:03:17 +00001198int
Fred Drake100814d2000-07-09 15:48:49 +00001199PyNumber_Coerce(PyObject **pv, PyObject **pw)
Guido van Rossum242c6421997-11-19 16:03:17 +00001200{
1201 int err = PyNumber_CoerceEx(pv, pw);
1202 if (err <= 0)
1203 return err;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001204 PyErr_SetString(PyExc_TypeError, "number coercion failed");
Guido van Rossum5524a591995-01-10 15:26:20 +00001205 return -1;
1206}
1207
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001208
Guido van Rossum49b11fe1995-01-26 00:38:22 +00001209/* Test whether an object can be called */
1210
1211int
Fred Drake100814d2000-07-09 15:48:49 +00001212PyCallable_Check(PyObject *x)
Guido van Rossum49b11fe1995-01-26 00:38:22 +00001213{
1214 if (x == NULL)
1215 return 0;
1216 if (x->ob_type->tp_call != NULL ||
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001217 PyFunction_Check(x) ||
1218 PyMethod_Check(x) ||
1219 PyCFunction_Check(x) ||
1220 PyClass_Check(x))
Guido van Rossum49b11fe1995-01-26 00:38:22 +00001221 return 1;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001222 if (PyInstance_Check(x)) {
1223 PyObject *call = PyObject_GetAttrString(x, "__call__");
Guido van Rossum49b11fe1995-01-26 00:38:22 +00001224 if (call == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001225 PyErr_Clear();
Guido van Rossum49b11fe1995-01-26 00:38:22 +00001226 return 0;
1227 }
1228 /* Could test recursively but don't, for fear of endless
1229 recursion if some joker sets self.__call__ = self */
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001230 Py_DECREF(call);
Guido van Rossum49b11fe1995-01-26 00:38:22 +00001231 return 1;
1232 }
1233 return 0;
1234}
1235
1236
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001237/*
1238NoObject is usable as a non-NULL undefined value, used by the macro None.
1239There is (and should be!) no way to create other objects of this type,
Guido van Rossum3f5da241990-12-20 15:06:42 +00001240so there is exactly one (which is indestructible, by the way).
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001241*/
1242
Guido van Rossum0c182a11992-03-27 17:26:13 +00001243/* ARGSUSED */
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001244static PyObject *
Fred Drake100814d2000-07-09 15:48:49 +00001245none_repr(PyObject *op)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001246{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001247 return PyString_FromString("None");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001248}
1249
Barry Warsaw9bf16442001-01-23 16:24:35 +00001250/* ARGUSED */
1251static void
1252none_dealloc(PyObject* ignore)
1253{
1254 /* This should never get called, but we also don't want to SEGV if
1255 * we accidently decref None out of existance.
1256 */
1257 abort();
1258}
1259
1260
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001261static PyTypeObject PyNothing_Type = {
1262 PyObject_HEAD_INIT(&PyType_Type)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001263 0,
Guido van Rossum3f5da241990-12-20 15:06:42 +00001264 "None",
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001265 0,
1266 0,
Barry Warsaw9bf16442001-01-23 16:24:35 +00001267 (destructor)none_dealloc, /*tp_dealloc*/ /*never called*/
Guido van Rossum7066dd71992-09-17 17:54:56 +00001268 0, /*tp_print*/
Guido van Rossum3f5da241990-12-20 15:06:42 +00001269 0, /*tp_getattr*/
1270 0, /*tp_setattr*/
1271 0, /*tp_compare*/
Guido van Rossum1d5735e1994-08-30 08:27:36 +00001272 (reprfunc)none_repr, /*tp_repr*/
Guido van Rossum3f5da241990-12-20 15:06:42 +00001273 0, /*tp_as_number*/
1274 0, /*tp_as_sequence*/
1275 0, /*tp_as_mapping*/
Guido van Rossum9bfef441993-03-29 10:43:31 +00001276 0, /*tp_hash */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001277};
1278
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001279PyObject _Py_NoneStruct = {
1280 PyObject_HEAD_INIT(&PyNothing_Type)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001281};
1282
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +00001283/* NotImplemented is an object that can be used to signal that an
1284 operation is not implemented for the given type combination. */
1285
1286static PyObject *
1287NotImplemented_repr(PyObject *op)
1288{
1289 return PyString_FromString("NotImplemented");
1290}
1291
1292static PyTypeObject PyNotImplemented_Type = {
1293 PyObject_HEAD_INIT(&PyType_Type)
1294 0,
1295 "NotImplemented",
1296 0,
1297 0,
Barry Warsaw9bf16442001-01-23 16:24:35 +00001298 (destructor)none_dealloc, /*tp_dealloc*/ /*never called*/
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +00001299 0, /*tp_print*/
1300 0, /*tp_getattr*/
1301 0, /*tp_setattr*/
1302 0, /*tp_compare*/
1303 (reprfunc)NotImplemented_repr, /*tp_repr*/
1304 0, /*tp_as_number*/
1305 0, /*tp_as_sequence*/
1306 0, /*tp_as_mapping*/
1307 0, /*tp_hash */
1308};
1309
1310PyObject _Py_NotImplementedStruct = {
1311 PyObject_HEAD_INIT(&PyNotImplemented_Type)
1312};
1313
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001314
Guido van Rossum84a90321996-05-22 16:34:47 +00001315#ifdef Py_TRACE_REFS
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001316
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001317static PyObject refchain = {&refchain, &refchain};
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001318
Guido van Rossumaacdc9d1996-08-12 21:32:12 +00001319void
Fred Drake100814d2000-07-09 15:48:49 +00001320_Py_ResetReferences(void)
Guido van Rossume09fb551997-08-05 02:04:34 +00001321{
1322 refchain._ob_prev = refchain._ob_next = &refchain;
1323 _Py_RefTotal = 0;
1324}
1325
1326void
Fred Drake100814d2000-07-09 15:48:49 +00001327_Py_NewReference(PyObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001328{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001329 _Py_RefTotal++;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001330 op->ob_refcnt = 1;
1331 op->_ob_next = refchain._ob_next;
1332 op->_ob_prev = &refchain;
1333 refchain._ob_next->_ob_prev = op;
1334 refchain._ob_next = op;
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +00001335#ifdef COUNT_ALLOCS
1336 inc_count(op->ob_type);
1337#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001338}
1339
Guido van Rossumaacdc9d1996-08-12 21:32:12 +00001340void
Fred Drake100814d2000-07-09 15:48:49 +00001341_Py_ForgetReference(register PyObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001342{
Guido van Rossumbffd6832000-01-20 22:32:56 +00001343#ifdef SLOW_UNREF_CHECK
Guido van Rossum4c08d552000-03-10 22:55:18 +00001344 register PyObject *p;
Guido van Rossumbffd6832000-01-20 22:32:56 +00001345#endif
Guido van Rossumd7047b31995-01-02 19:07:15 +00001346 if (op->ob_refcnt < 0)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001347 Py_FatalError("UNREF negative refcnt");
Guido van Rossum2e8f6141992-09-03 20:32:55 +00001348 if (op == &refchain ||
Guido van Rossumd7047b31995-01-02 19:07:15 +00001349 op->_ob_prev->_ob_next != op || op->_ob_next->_ob_prev != op)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001350 Py_FatalError("UNREF invalid object");
Guido van Rossum2e8f6141992-09-03 20:32:55 +00001351#ifdef SLOW_UNREF_CHECK
Guido van Rossum3f5da241990-12-20 15:06:42 +00001352 for (p = refchain._ob_next; p != &refchain; p = p->_ob_next) {
1353 if (p == op)
1354 break;
1355 }
Guido van Rossumd7047b31995-01-02 19:07:15 +00001356 if (p == &refchain) /* Not found */
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001357 Py_FatalError("UNREF unknown object");
Guido van Rossum2e8f6141992-09-03 20:32:55 +00001358#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001359 op->_ob_next->_ob_prev = op->_ob_prev;
1360 op->_ob_prev->_ob_next = op->_ob_next;
Guido van Rossum2e8f6141992-09-03 20:32:55 +00001361 op->_ob_next = op->_ob_prev = NULL;
Guido van Rossumd8953cb1995-04-06 14:46:26 +00001362#ifdef COUNT_ALLOCS
1363 op->ob_type->tp_free++;
1364#endif
Guido van Rossum3f5da241990-12-20 15:06:42 +00001365}
1366
Guido van Rossumaacdc9d1996-08-12 21:32:12 +00001367void
Fred Drake100814d2000-07-09 15:48:49 +00001368_Py_Dealloc(PyObject *op)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001369{
Guido van Rossum9776adf1994-09-07 14:36:45 +00001370 destructor dealloc = op->ob_type->tp_dealloc;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001371 _Py_ForgetReference(op);
Guido van Rossum9776adf1994-09-07 14:36:45 +00001372 (*dealloc)(op);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001373}
1374
Guido van Rossumaacdc9d1996-08-12 21:32:12 +00001375void
Fred Drake100814d2000-07-09 15:48:49 +00001376_Py_PrintReferences(FILE *fp)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001377{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001378 PyObject *op;
Guido van Rossume09fb551997-08-05 02:04:34 +00001379 fprintf(fp, "Remaining objects:\n");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001380 for (op = refchain._ob_next; op != &refchain; op = op->_ob_next) {
1381 fprintf(fp, "[%d] ", op->ob_refcnt);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001382 if (PyObject_Print(op, fp, 0) != 0)
1383 PyErr_Clear();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001384 putc('\n', fp);
1385 }
1386}
1387
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +00001388PyObject *
Fred Drake100814d2000-07-09 15:48:49 +00001389_Py_GetObjects(PyObject *self, PyObject *args)
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +00001390{
1391 int i, n;
1392 PyObject *t = NULL;
1393 PyObject *res, *op;
1394
1395 if (!PyArg_ParseTuple(args, "i|O", &n, &t))
1396 return NULL;
1397 op = refchain._ob_next;
1398 res = PyList_New(0);
1399 if (res == NULL)
1400 return NULL;
1401 for (i = 0; (n == 0 || i < n) && op != &refchain; i++) {
1402 while (op == self || op == args || op == res || op == t ||
1403 t != NULL && op->ob_type != (PyTypeObject *) t) {
1404 op = op->_ob_next;
1405 if (op == &refchain)
1406 return res;
1407 }
1408 if (PyList_Append(res, op) < 0) {
1409 Py_DECREF(res);
1410 return NULL;
1411 }
1412 op = op->_ob_next;
1413 }
1414 return res;
1415}
1416
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001417#endif
Guido van Rossum97ead3f1996-01-12 01:24:09 +00001418
1419
1420/* Hack to force loading of cobject.o */
Guido van Rossumda9c2711996-12-05 21:58:58 +00001421PyTypeObject *_Py_cobject_hack = &PyCObject_Type;
Guido van Rossum84a90321996-05-22 16:34:47 +00001422
1423
1424/* Hack to force loading of abstract.o */
Jeremy Hylton6253f832000-07-12 12:56:19 +00001425int (*_Py_abstract_hack)(PyObject *) = &PyObject_Size;
Guido van Rossume09fb551997-08-05 02:04:34 +00001426
1427
Andrew M. Kuchling1582a3a2000-08-16 12:27:23 +00001428/* Python's malloc wrappers (see pymem.h) */
Guido van Rossume09fb551997-08-05 02:04:34 +00001429
Thomas Wouters334fb892000-07-25 12:56:38 +00001430void *
Fred Drake100814d2000-07-09 15:48:49 +00001431PyMem_Malloc(size_t nbytes)
Guido van Rossume09fb551997-08-05 02:04:34 +00001432{
1433#if _PyMem_EXTRA > 0
1434 if (nbytes == 0)
1435 nbytes = _PyMem_EXTRA;
1436#endif
Guido van Rossumb18618d2000-05-03 23:44:39 +00001437 return PyMem_MALLOC(nbytes);
Guido van Rossume09fb551997-08-05 02:04:34 +00001438}
1439
Thomas Wouters334fb892000-07-25 12:56:38 +00001440void *
1441PyMem_Realloc(void *p, size_t nbytes)
Guido van Rossume09fb551997-08-05 02:04:34 +00001442{
1443#if _PyMem_EXTRA > 0
1444 if (nbytes == 0)
1445 nbytes = _PyMem_EXTRA;
1446#endif
Guido van Rossumb18618d2000-05-03 23:44:39 +00001447 return PyMem_REALLOC(p, nbytes);
Guido van Rossume09fb551997-08-05 02:04:34 +00001448}
1449
1450void
Thomas Wouters334fb892000-07-25 12:56:38 +00001451PyMem_Free(void *p)
Guido van Rossume09fb551997-08-05 02:04:34 +00001452{
Guido van Rossumb18618d2000-05-03 23:44:39 +00001453 PyMem_FREE(p);
1454}
1455
1456
1457/* Python's object malloc wrappers (see objimpl.h) */
1458
Thomas Wouters334fb892000-07-25 12:56:38 +00001459void *
Fred Drake100814d2000-07-09 15:48:49 +00001460PyObject_Malloc(size_t nbytes)
Guido van Rossumb18618d2000-05-03 23:44:39 +00001461{
1462 return PyObject_MALLOC(nbytes);
1463}
1464
Thomas Wouters334fb892000-07-25 12:56:38 +00001465void *
1466PyObject_Realloc(void *p, size_t nbytes)
Guido van Rossumb18618d2000-05-03 23:44:39 +00001467{
1468 return PyObject_REALLOC(p, nbytes);
1469}
1470
1471void
Thomas Wouters334fb892000-07-25 12:56:38 +00001472PyObject_Free(void *p)
Guido van Rossumb18618d2000-05-03 23:44:39 +00001473{
1474 PyObject_FREE(p);
Guido van Rossume09fb551997-08-05 02:04:34 +00001475}
Guido van Rossum86610361998-04-10 22:32:46 +00001476
1477
Fred Drake41deb1e2001-02-01 05:27:45 +00001478/* Hook to clear up weak references only once the _weakref module is
1479 imported. We use a dummy implementation to simplify the code at each
1480 call site instead of requiring a test for NULL.
1481*/
1482
Fred Drakeb60654b2001-02-26 18:56:37 +00001483static void
Fred Drake41deb1e2001-02-01 05:27:45 +00001484empty_clear_weak_refs(PyObject *o)
1485{
Fred Drakeb60654b2001-02-26 18:56:37 +00001486 return;
Fred Drake41deb1e2001-02-01 05:27:45 +00001487}
1488
Fred Drakeb60654b2001-02-26 18:56:37 +00001489void (*PyObject_ClearWeakRefs)(PyObject *) = empty_clear_weak_refs;
Fred Drake41deb1e2001-02-01 05:27:45 +00001490
1491
1492
Guido van Rossum86610361998-04-10 22:32:46 +00001493/* These methods are used to control infinite recursion in repr, str, print,
1494 etc. Container objects that may recursively contain themselves,
1495 e.g. builtin dictionaries and lists, should used Py_ReprEnter() and
1496 Py_ReprLeave() to avoid infinite recursion.
1497
1498 Py_ReprEnter() returns 0 the first time it is called for a particular
1499 object and 1 every time thereafter. It returns -1 if an exception
1500 occurred. Py_ReprLeave() has no return value.
1501
1502 See dictobject.c and listobject.c for examples of use.
1503*/
1504
1505#define KEY "Py_Repr"
1506
1507int
Fred Drake100814d2000-07-09 15:48:49 +00001508Py_ReprEnter(PyObject *obj)
Guido van Rossum86610361998-04-10 22:32:46 +00001509{
1510 PyObject *dict;
1511 PyObject *list;
1512 int i;
1513
1514 dict = PyThreadState_GetDict();
1515 if (dict == NULL)
1516 return -1;
1517 list = PyDict_GetItemString(dict, KEY);
1518 if (list == NULL) {
1519 list = PyList_New(0);
1520 if (list == NULL)
1521 return -1;
1522 if (PyDict_SetItemString(dict, KEY, list) < 0)
1523 return -1;
1524 Py_DECREF(list);
1525 }
1526 i = PyList_GET_SIZE(list);
1527 while (--i >= 0) {
1528 if (PyList_GET_ITEM(list, i) == obj)
1529 return 1;
1530 }
1531 PyList_Append(list, obj);
1532 return 0;
1533}
1534
1535void
Fred Drake100814d2000-07-09 15:48:49 +00001536Py_ReprLeave(PyObject *obj)
Guido van Rossum86610361998-04-10 22:32:46 +00001537{
1538 PyObject *dict;
1539 PyObject *list;
1540 int i;
1541
1542 dict = PyThreadState_GetDict();
Guido van Rossumeb909461998-04-11 15:17:34 +00001543 if (dict == NULL)
1544 return;
Guido van Rossum86610361998-04-10 22:32:46 +00001545 list = PyDict_GetItemString(dict, KEY);
Guido van Rossumeb909461998-04-11 15:17:34 +00001546 if (list == NULL || !PyList_Check(list))
1547 return;
Guido van Rossum86610361998-04-10 22:32:46 +00001548 i = PyList_GET_SIZE(list);
1549 /* Count backwards because we always expect obj to be list[-1] */
1550 while (--i >= 0) {
1551 if (PyList_GET_ITEM(list, i) == obj) {
1552 PyList_SetSlice(list, i, i + 1, NULL);
1553 break;
1554 }
1555 }
1556}
Guido van Rossumd724b232000-03-13 16:01:29 +00001557
1558/*
1559 trashcan
1560 CT 2k0130
1561 non-recursively destroy nested objects
1562
1563 CT 2k0223
1564 everything is now done in a macro.
1565
1566 CT 2k0305
1567 modified to use functions, after Tim Peter's suggestion.
1568
1569 CT 2k0309
1570 modified to restore a possible error.
Guido van Rossum13ff8eb2000-03-25 18:39:19 +00001571
1572 CT 2k0325
1573 added better safe than sorry check for threadstate
Guido van Rossume92e6102000-04-24 15:40:53 +00001574
1575 CT 2k0422
1576 complete rewrite. We now build a chain via ob_type
1577 and save the limited number of types in ob_refcnt.
1578 This is perfect since we don't need any memory.
1579 A patch for free-threading would need just a lock.
Guido van Rossumd724b232000-03-13 16:01:29 +00001580*/
1581
Guido van Rossume92e6102000-04-24 15:40:53 +00001582#define Py_TRASHCAN_TUPLE 1
1583#define Py_TRASHCAN_LIST 2
1584#define Py_TRASHCAN_DICT 3
1585#define Py_TRASHCAN_FRAME 4
1586#define Py_TRASHCAN_TRACEBACK 5
1587/* extend here if other objects want protection */
1588
Guido van Rossumd724b232000-03-13 16:01:29 +00001589int _PyTrash_delete_nesting = 0;
Guido van Rossume92e6102000-04-24 15:40:53 +00001590
Guido van Rossumd724b232000-03-13 16:01:29 +00001591PyObject * _PyTrash_delete_later = NULL;
1592
1593void
Fred Drake100814d2000-07-09 15:48:49 +00001594_PyTrash_deposit_object(PyObject *op)
Guido van Rossumd724b232000-03-13 16:01:29 +00001595{
Guido van Rossume92e6102000-04-24 15:40:53 +00001596 int typecode;
Guido van Rossum13ff8eb2000-03-25 18:39:19 +00001597
Guido van Rossume92e6102000-04-24 15:40:53 +00001598 if (PyTuple_Check(op))
1599 typecode = Py_TRASHCAN_TUPLE;
1600 else if (PyList_Check(op))
1601 typecode = Py_TRASHCAN_LIST;
1602 else if (PyDict_Check(op))
1603 typecode = Py_TRASHCAN_DICT;
1604 else if (PyFrame_Check(op))
1605 typecode = Py_TRASHCAN_FRAME;
1606 else if (PyTraceBack_Check(op))
1607 typecode = Py_TRASHCAN_TRACEBACK;
Moshe Zadkacf703f02000-08-04 15:36:13 +00001608 else /* We have a bug here -- those are the only types in GC */ {
1609 Py_FatalError("Type not supported in GC -- internal bug");
1610 return; /* pacify compiler -- execution never here */
1611 }
Guido van Rossume92e6102000-04-24 15:40:53 +00001612 op->ob_refcnt = typecode;
Guido van Rossumd724b232000-03-13 16:01:29 +00001613
Guido van Rossume92e6102000-04-24 15:40:53 +00001614 op->ob_type = (PyTypeObject*)_PyTrash_delete_later;
1615 _PyTrash_delete_later = op;
Guido van Rossumd724b232000-03-13 16:01:29 +00001616}
1617
1618void
Fred Drake100814d2000-07-09 15:48:49 +00001619_PyTrash_destroy_chain(void)
Guido van Rossumd724b232000-03-13 16:01:29 +00001620{
1621 while (_PyTrash_delete_later) {
1622 PyObject *shredder = _PyTrash_delete_later;
Guido van Rossume92e6102000-04-24 15:40:53 +00001623 _PyTrash_delete_later = (PyObject*) shredder->ob_type;
1624
1625 switch (shredder->ob_refcnt) {
1626 case Py_TRASHCAN_TUPLE:
1627 shredder->ob_type = &PyTuple_Type;
1628 break;
1629 case Py_TRASHCAN_LIST:
1630 shredder->ob_type = &PyList_Type;
1631 break;
1632 case Py_TRASHCAN_DICT:
1633 shredder->ob_type = &PyDict_Type;
1634 break;
1635 case Py_TRASHCAN_FRAME:
1636 shredder->ob_type = &PyFrame_Type;
1637 break;
1638 case Py_TRASHCAN_TRACEBACK:
1639 shredder->ob_type = &PyTraceBack_Type;
1640 break;
1641 }
1642 _Py_NewReference(shredder);
1643
Guido van Rossumd724b232000-03-13 16:01:29 +00001644 ++_PyTrash_delete_nesting;
1645 Py_DECREF(shredder);
1646 --_PyTrash_delete_nesting;
1647 }
1648}
Neil Schemenauera35c6882001-02-27 04:45:05 +00001649
1650#ifdef WITH_PYMALLOC
1651#include "obmalloc.c"
1652#endif