blob: 0d9b3fe5db0f7494f99630b18496a8e288b047df [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
583 && (f = v->ob_type->tp_compare) != NULL)
584 return (*f)(v, w);
Guido van Rossume797ec12001-01-17 15:24:28 +0000585 c = try_rich_to_3way_compare(v, w);
586 if (c < 2)
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +0000587 return c;
Guido van Rossume797ec12001-01-17 15:24:28 +0000588 c = try_3way_compare(v, w);
589 if (c < 2)
590 return c;
591 return default_3way_compare(v, w);
Guido van Rossum20566841995-01-12 11:26:10 +0000592}
593
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000594/* compare_nesting is incremented before calling compare (for
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000595 some types) and decremented on exit. If the count exceeds the
596 nesting limit, enable code to detect circular data structures.
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000597
598 This is a tunable parameter that should only affect the performance
599 of comparisons, nothing else. Setting it high makes comparing deeply
600 nested non-cyclical data structures faster, but makes comparing cyclical
601 data structures slower.
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000602*/
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000603#define NESTING_LIMIT 20
604
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000605static int compare_nesting = 0;
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000606
607static PyObject*
Fred Drake100814d2000-07-09 15:48:49 +0000608get_inprogress_dict(void)
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000609{
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000610 static PyObject *key;
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000611 PyObject *tstate_dict, *inprogress;
612
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000613 if (key == NULL) {
614 key = PyString_InternFromString("cmp_state");
615 if (key == NULL)
616 return NULL;
617 }
618
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000619 tstate_dict = PyThreadState_GetDict();
620 if (tstate_dict == NULL) {
621 PyErr_BadInternalCall();
622 return NULL;
623 }
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000624
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000625 inprogress = PyDict_GetItem(tstate_dict, key);
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000626 if (inprogress == NULL) {
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000627 inprogress = PyDict_New();
628 if (inprogress == NULL)
629 return NULL;
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000630 if (PyDict_SetItem(tstate_dict, key, inprogress) == -1) {
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000631 Py_DECREF(inprogress);
632 return NULL;
633 }
Jeremy Hyltona251ea02000-06-09 16:20:39 +0000634 Py_DECREF(inprogress);
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000635 }
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000636
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000637 return inprogress;
638}
639
640static PyObject *
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000641check_recursion(PyObject *v, PyObject *w, int op)
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000642{
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000643 PyObject *inprogress;
644 PyObject *token;
Barry Warsaw9d23a4e2000-08-18 05:01:19 +0000645 Py_uintptr_t iv = (Py_uintptr_t)v;
646 Py_uintptr_t iw = (Py_uintptr_t)w;
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000647 PyObject *x, *y, *z;
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000648
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000649 inprogress = get_inprogress_dict();
650 if (inprogress == NULL)
651 return NULL;
652
653 token = PyTuple_New(3);
654 if (token == NULL)
655 return NULL;
656
657 if (iv <= iw) {
658 PyTuple_SET_ITEM(token, 0, x = PyLong_FromVoidPtr((void *)v));
659 PyTuple_SET_ITEM(token, 1, y = PyLong_FromVoidPtr((void *)w));
660 if (op >= 0)
661 op = swapped_op[op];
662 } else {
663 PyTuple_SET_ITEM(token, 0, x = PyLong_FromVoidPtr((void *)w));
664 PyTuple_SET_ITEM(token, 1, y = PyLong_FromVoidPtr((void *)v));
665 }
666 PyTuple_SET_ITEM(token, 2, z = PyInt_FromLong((long)op));
667 if (x == NULL || y == NULL || z == NULL) {
668 Py_DECREF(token);
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000669 return NULL;
670 }
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000671
672 if (PyDict_GetItem(inprogress, token) != NULL) {
673 Py_DECREF(token);
674 return Py_None; /* Without INCREF! */
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000675 }
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000676
677 if (PyDict_SetItem(inprogress, token, token) < 0) {
678 Py_DECREF(token);
679 return NULL;
680 }
681
682 return token;
683}
684
685static void
686delete_token(PyObject *token)
687{
688 PyObject *inprogress;
689
690 if (token == NULL || token == Py_None)
691 return;
692 inprogress = get_inprogress_dict();
693 if (inprogress == NULL)
694 PyErr_Clear();
695 else
696 PyDict_DelItem(inprogress, token);
697 Py_DECREF(token);
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000698}
699
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000700int
Fred Drake100814d2000-07-09 15:48:49 +0000701PyObject_Compare(PyObject *v, PyObject *w)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000702{
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000703 PyTypeObject *vtp;
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000704 int result;
705
Jack Jansend49cbe12000-08-22 21:52:51 +0000706#if defined(USE_STACKCHECK)
Guido van Rossum9c0a99e2000-08-30 15:53:50 +0000707 if (PyOS_CheckStack()) {
Jack Jansend49cbe12000-08-22 21:52:51 +0000708 PyErr_SetString(PyExc_MemoryError, "Stack overflow");
709 return -1;
710 }
711#endif
Guido van Rossumc8b6df91997-05-23 00:06:51 +0000712 if (v == NULL || w == NULL) {
713 PyErr_BadInternalCall();
714 return -1;
715 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000716 if (v == w)
717 return 0;
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +0000718 vtp = v->ob_type;
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000719 compare_nesting++;
720 if (compare_nesting > NESTING_LIMIT &&
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +0000721 (vtp->tp_as_mapping
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000722 || (vtp->tp_as_sequence
723 && !PyString_Check(v)
724 && !PyTuple_Check(v)))) {
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +0000725 /* try to detect circular data structures */
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000726 PyObject *token = check_recursion(v, w, -1);
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000727
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000728 if (token == NULL) {
729 result = -1;
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000730 }
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000731 else if (token == Py_None) {
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000732 /* already comparing these objects. assume
733 they're equal until shown otherwise */
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +0000734 result = 0;
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000735 }
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000736 else {
737 result = do_cmp(v, w);
738 delete_token(token);
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000739 }
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000740 }
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +0000741 else {
742 result = do_cmp(v, w);
743 }
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000744 compare_nesting--;
Guido van Rossume797ec12001-01-17 15:24:28 +0000745 return result < 0 ? -1 : result;
746}
747
748static PyObject *
Martin v. Löwis0163d6d2001-06-09 07:34:05 +0000749convert_3way_to_object(int op, int c)
Guido van Rossume797ec12001-01-17 15:24:28 +0000750{
Guido van Rossume797ec12001-01-17 15:24:28 +0000751 PyObject *result;
Guido van Rossume797ec12001-01-17 15:24:28 +0000752 switch (op) {
753 case Py_LT: c = c < 0; break;
754 case Py_LE: c = c <= 0; break;
755 case Py_EQ: c = c == 0; break;
756 case Py_NE: c = c != 0; break;
757 case Py_GT: c = c > 0; break;
758 case Py_GE: c = c >= 0; break;
759 }
760 result = c ? Py_True : Py_False;
761 Py_INCREF(result);
Jeremy Hylton4a3dd2d2000-04-14 19:13:24 +0000762 return result;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000763}
Martin v. Löwis0163d6d2001-06-09 07:34:05 +0000764
765
766static PyObject *
767try_3way_to_rich_compare(PyObject *v, PyObject *w, int op)
768{
769 int c;
770
771 c = try_3way_compare(v, w);
772 if (c >= 2)
773 c = default_3way_compare(v, w);
774 if (c <= -2)
775 return NULL;
776 return convert_3way_to_object(op, c);
777}
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000778
Neil Schemenauerd38855c2001-01-21 16:25:18 +0000779static PyObject *
Guido van Rossume797ec12001-01-17 15:24:28 +0000780do_richcmp(PyObject *v, PyObject *w, int op)
781{
782 PyObject *res;
Martin v. Löwis0163d6d2001-06-09 07:34:05 +0000783 cmpfunc f;
784
785 /* If the types are equal, don't bother with coercions etc.
786 Instances are special-cased in try_3way_compare, since
787 a result of 2 does *not* mean one value being greater
788 than the other. */
789 if (v->ob_type == w->ob_type
790 && (f = v->ob_type->tp_compare) != NULL
791 && !PyInstance_Check(v)) {
792 int c;
793 richcmpfunc f1;
794 if ((f1 = RICHCOMPARE(v->ob_type)) != NULL) {
795 /* If the type has richcmp, try it first.
796 try_rich_compare would try it two-sided,
797 which is not needed since we've a single
798 type only. */
799 res = (*f1)(v, w, op);
800 if (res != Py_NotImplemented)
801 return res;
802 Py_DECREF(res);
803 }
804 c = (*f)(v, w);
805 if (c < 0 && PyErr_Occurred())
806 return NULL;
807 return convert_3way_to_object(op, c);
808 }
Guido van Rossume797ec12001-01-17 15:24:28 +0000809
810 res = try_rich_compare(v, w, op);
811 if (res != Py_NotImplemented)
812 return res;
813 Py_DECREF(res);
814
815 return try_3way_to_rich_compare(v, w, op);
816}
817
818PyObject *
819PyObject_RichCompare(PyObject *v, PyObject *w, int op)
820{
821 PyObject *res;
822
823 assert(Py_LT <= op && op <= Py_GE);
824
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000825 compare_nesting++;
826 if (compare_nesting > NESTING_LIMIT &&
827 (v->ob_type->tp_as_mapping
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000828 || (v->ob_type->tp_as_sequence
829 && !PyString_Check(v)
830 && !PyTuple_Check(v)))) {
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000831 /* try to detect circular data structures */
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000832 PyObject *token = check_recursion(v, w, op);
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000833
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000834 if (token == NULL) {
835 res = NULL;
Guido van Rossume797ec12001-01-17 15:24:28 +0000836 }
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000837 else if (token == Py_None) {
838 /* already comparing these objects with this operator.
839 assume they're equal until shown otherwise */
840 if (op == Py_EQ)
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000841 res = Py_True;
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000842 else if (op == Py_NE)
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000843 res = Py_False;
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000844 else {
845 PyErr_SetString(PyExc_ValueError,
846 "can't order recursive values");
847 res = NULL;
848 }
849 Py_XINCREF(res);
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000850 }
Guido van Rossuma3af41d2001-01-18 22:07:06 +0000851 else {
852 res = do_richcmp(v, w, op);
853 delete_token(token);
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000854 }
Guido van Rossume797ec12001-01-17 15:24:28 +0000855 }
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000856 else {
857 res = do_richcmp(v, w, op);
858 }
Guido van Rossum2ffbf6b2001-01-17 21:27:02 +0000859 compare_nesting--;
Guido van Rossume797ec12001-01-17 15:24:28 +0000860 return res;
861}
862
Tim Petersde9725f2001-05-05 10:06:17 +0000863/* Return -1 if error; 1 if v op w; 0 if not (v op w). */
Guido van Rossume797ec12001-01-17 15:24:28 +0000864int
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{
Tim Peters6d6c1a32001-08-02 04:15:00 +0000987 PyObject *w, *res;
Guido van Rossumd8eb1b31996-08-09 20:52:03 +0000988
Tim Peters6d6c1a32001-08-02 04:15:00 +0000989 if (v->ob_type->tp_getattr != NULL)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000990 return (*v->ob_type->tp_getattr)(v, name);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000991 w = PyString_InternFromString(name);
992 if (w == NULL)
993 return NULL;
994 res = PyObject_GetAttr(v, w);
995 Py_XDECREF(w);
996 return res;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000997}
998
999int
Fred Drake100814d2000-07-09 15:48:49 +00001000PyObject_HasAttrString(PyObject *v, char *name)
Guido van Rossumed18fdc1993-07-11 19:55:34 +00001001{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001002 PyObject *res = PyObject_GetAttrString(v, name);
Guido van Rossumed18fdc1993-07-11 19:55:34 +00001003 if (res != NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001004 Py_DECREF(res);
Guido van Rossumed18fdc1993-07-11 19:55:34 +00001005 return 1;
1006 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001007 PyErr_Clear();
Guido van Rossumed18fdc1993-07-11 19:55:34 +00001008 return 0;
1009}
1010
1011int
Fred Drake100814d2000-07-09 15:48:49 +00001012PyObject_SetAttrString(PyObject *v, char *name, PyObject *w)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001013{
Tim Peters6d6c1a32001-08-02 04:15:00 +00001014 PyObject *s;
1015 int res;
Guido van Rossumd8eb1b31996-08-09 20:52:03 +00001016
Tim Peters6d6c1a32001-08-02 04:15:00 +00001017 if (v->ob_type->tp_setattr != NULL)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001018 return (*v->ob_type->tp_setattr)(v, name, w);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001019 s = PyString_InternFromString(name);
1020 if (s == NULL)
1021 return -1;
1022 res = PyObject_SetAttr(v, s, w);
1023 Py_XDECREF(s);
1024 return res;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001025}
1026
Guido van Rossum98ff96a1997-05-20 18:34:44 +00001027PyObject *
Fred Drake100814d2000-07-09 15:48:49 +00001028PyObject_GetAttr(PyObject *v, PyObject *name)
Guido van Rossum98ff96a1997-05-20 18:34:44 +00001029{
Tim Peters6d6c1a32001-08-02 04:15:00 +00001030 PyTypeObject *tp = v->ob_type;
1031
Marc-André Lemburge44e5072000-09-18 16:20:57 +00001032 /* The Unicode to string conversion is done here because the
1033 existing tp_getattro slots expect a string object as name
1034 and we wouldn't want to break those. */
1035 if (PyUnicode_Check(name)) {
1036 name = _PyUnicode_AsDefaultEncodedString(name, NULL);
1037 if (name == NULL)
1038 return NULL;
1039 }
Jeremy Hylton99a8f902000-06-23 14:36:32 +00001040 if (!PyString_Check(name)) {
1041 PyErr_SetString(PyExc_TypeError,
1042 "attribute name must be string");
1043 return NULL;
1044 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001045 if (tp->tp_getattro != NULL)
1046 return (*tp->tp_getattro)(v, name);
1047 if (tp->tp_getattr != NULL)
1048 return (*tp->tp_getattr)(v, PyString_AS_STRING(name));
1049 PyErr_Format(PyExc_AttributeError,
1050 "'%.50s' object has no attribute '%.400s'",
1051 tp->tp_name, PyString_AS_STRING(name));
1052 return NULL;
Guido van Rossum98ff96a1997-05-20 18:34:44 +00001053}
1054
1055int
Fred Drake100814d2000-07-09 15:48:49 +00001056PyObject_HasAttr(PyObject *v, PyObject *name)
Guido van Rossum98ff96a1997-05-20 18:34:44 +00001057{
1058 PyObject *res = PyObject_GetAttr(v, name);
1059 if (res != NULL) {
1060 Py_DECREF(res);
1061 return 1;
1062 }
1063 PyErr_Clear();
1064 return 0;
1065}
1066
1067int
Fred Drake100814d2000-07-09 15:48:49 +00001068PyObject_SetAttr(PyObject *v, PyObject *name, PyObject *value)
Guido van Rossum98ff96a1997-05-20 18:34:44 +00001069{
Tim Peters6d6c1a32001-08-02 04:15:00 +00001070 PyTypeObject *tp = v->ob_type;
Guido van Rossum98ff96a1997-05-20 18:34:44 +00001071 int err;
Marc-André Lemburge44e5072000-09-18 16:20:57 +00001072
1073 /* The Unicode to string conversion is done here because the
1074 existing tp_setattro slots expect a string object as name
1075 and we wouldn't want to break those. */
1076 if (PyUnicode_Check(name)) {
1077 name = PyUnicode_AsEncodedString(name, NULL, NULL);
1078 if (name == NULL)
1079 return -1;
Jeremy Hylton99a8f902000-06-23 14:36:32 +00001080 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001081 else if (!PyString_Check(name)){
Jeremy Hylton99a8f902000-06-23 14:36:32 +00001082 PyErr_SetString(PyExc_TypeError,
1083 "attribute name must be string");
Tim Peters6d6c1a32001-08-02 04:15:00 +00001084 return -1;
Jeremy Hylton99a8f902000-06-23 14:36:32 +00001085 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001086 else
1087 Py_INCREF(name);
1088
1089 PyString_InternInPlace(&name);
1090 if (tp->tp_setattro != NULL) {
1091 err = (*tp->tp_setattro)(v, name, value);
1092 Py_DECREF(name);
1093 return err;
Marc-André Lemburge44e5072000-09-18 16:20:57 +00001094 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001095 if (tp->tp_setattr != NULL) {
1096 err = (*tp->tp_setattr)(v, PyString_AS_STRING(name), value);
1097 Py_DECREF(name);
1098 return err;
1099 }
Guido van Rossum98ff96a1997-05-20 18:34:44 +00001100 Py_DECREF(name);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001101 if (tp->tp_getattr == NULL && tp->tp_getattro == NULL)
1102 PyErr_Format(PyExc_TypeError,
1103 "'%.100s' object has no attributes "
1104 "(%s .%.100s)",
1105 tp->tp_name,
1106 value==NULL ? "del" : "assign to",
1107 PyString_AS_STRING(name));
1108 else
1109 PyErr_Format(PyExc_TypeError,
1110 "'%.100s' object has only read-only attributes "
1111 "(%s .%.100s)",
1112 tp->tp_name,
1113 value==NULL ? "del" : "assign to",
1114 PyString_AS_STRING(name));
1115 return -1;
1116}
1117
1118/* Helper to get a pointer to an object's __dict__ slot, if any */
1119
1120PyObject **
1121_PyObject_GetDictPtr(PyObject *obj)
1122{
1123#define PTRSIZE (sizeof(PyObject *))
1124
1125 long dictoffset;
1126 PyTypeObject *tp = obj->ob_type;
1127
1128 if (!(tp->tp_flags & Py_TPFLAGS_HAVE_CLASS))
1129 return NULL;
1130 dictoffset = tp->tp_dictoffset;
1131 if (dictoffset == 0)
1132 return NULL;
1133 if (dictoffset < 0) {
1134 dictoffset += PyType_BASICSIZE(tp);
1135 assert(dictoffset > 0); /* Sanity check */
1136 if (tp->tp_itemsize > 0) {
1137 int n = ((PyVarObject *)obj)->ob_size;
1138 if (n > 0) {
1139 dictoffset += tp->tp_itemsize * n;
1140 /* Round up, if necessary */
1141 if (tp->tp_itemsize % PTRSIZE != 0) {
1142 dictoffset += PTRSIZE - 1;
1143 dictoffset /= PTRSIZE;
1144 dictoffset *= PTRSIZE;
1145 }
1146 }
1147 }
1148 }
1149 return (PyObject **) ((char *)obj + dictoffset);
1150}
1151
1152/* Generic GetAttr functions - put these in your tp_[gs]etattro slot */
1153
1154PyObject *
1155PyObject_GenericGetAttr(PyObject *obj, PyObject *name)
1156{
1157 PyTypeObject *tp = obj->ob_type;
1158 PyObject *descr;
1159 descrgetfunc f;
1160 PyObject **dictptr;
1161
1162 if (tp->tp_dict == NULL) {
Guido van Rossum528b7eb2001-08-07 17:24:28 +00001163 if (PyType_Ready(tp) < 0)
Tim Peters6d6c1a32001-08-02 04:15:00 +00001164 return NULL;
1165 }
1166
1167 descr = _PyType_Lookup(tp, name);
1168 f = NULL;
1169 if (descr != NULL) {
1170 f = descr->ob_type->tp_descr_get;
1171 if (f != NULL && PyDescr_IsData(descr))
1172 return f(descr, obj, (PyObject *)obj->ob_type);
1173 }
1174
1175 dictptr = _PyObject_GetDictPtr(obj);
1176 if (dictptr != NULL) {
1177 PyObject *dict = *dictptr;
1178 if (dict != NULL) {
1179 PyObject *res = PyDict_GetItem(dict, name);
1180 if (res != NULL) {
1181 Py_INCREF(res);
1182 return res;
1183 }
1184 }
1185 }
1186
1187 if (f != NULL)
1188 return f(descr, obj, (PyObject *)obj->ob_type);
1189
1190 if (descr != NULL) {
1191 Py_INCREF(descr);
1192 return descr;
1193 }
1194
1195 PyErr_Format(PyExc_AttributeError,
1196 "'%.50s' object has no attribute '%.400s'",
1197 tp->tp_name, PyString_AS_STRING(name));
1198 return NULL;
1199}
1200
1201int
1202PyObject_GenericSetAttr(PyObject *obj, PyObject *name, PyObject *value)
1203{
1204 PyTypeObject *tp = obj->ob_type;
1205 PyObject *descr;
1206 descrsetfunc f;
1207 PyObject **dictptr;
1208
1209 if (tp->tp_dict == NULL) {
Guido van Rossum528b7eb2001-08-07 17:24:28 +00001210 if (PyType_Ready(tp) < 0)
Tim Peters6d6c1a32001-08-02 04:15:00 +00001211 return -1;
1212 }
1213
1214 descr = _PyType_Lookup(tp, name);
1215 f = NULL;
1216 if (descr != NULL) {
1217 f = descr->ob_type->tp_descr_set;
1218 if (f != NULL && PyDescr_IsData(descr))
1219 return f(descr, obj, value);
1220 }
1221
1222 dictptr = _PyObject_GetDictPtr(obj);
1223 if (dictptr != NULL) {
1224 PyObject *dict = *dictptr;
1225 if (dict == NULL && value != NULL) {
1226 dict = PyDict_New();
1227 if (dict == NULL)
1228 return -1;
1229 *dictptr = dict;
1230 }
1231 if (dict != NULL) {
1232 int res;
1233 if (value == NULL)
1234 res = PyDict_DelItem(dict, name);
1235 else
1236 res = PyDict_SetItem(dict, name, value);
1237 if (res < 0 && PyErr_ExceptionMatches(PyExc_KeyError))
1238 PyErr_SetObject(PyExc_AttributeError, name);
1239 return res;
1240 }
1241 }
1242
1243 if (f != NULL)
1244 return f(descr, obj, value);
1245
1246 if (descr == NULL) {
1247 PyErr_Format(PyExc_AttributeError,
1248 "'%.50s' object has no attribute '%.400s'",
1249 tp->tp_name, PyString_AS_STRING(name));
1250 return -1;
1251 }
1252
1253 PyErr_Format(PyExc_AttributeError,
1254 "'%.50s' object attribute '%.400s' is read-only",
1255 tp->tp_name, PyString_AS_STRING(name));
1256 return -1;
Guido van Rossum98ff96a1997-05-20 18:34:44 +00001257}
1258
Guido van Rossum6ac258d1993-05-12 08:24:20 +00001259/* Test a value used as condition, e.g., in a for or if statement.
1260 Return -1 if an error occurred */
1261
1262int
Fred Drake100814d2000-07-09 15:48:49 +00001263PyObject_IsTrue(PyObject *v)
Guido van Rossum6ac258d1993-05-12 08:24:20 +00001264{
1265 int res;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001266 if (v == Py_None)
Guido van Rossum6ac258d1993-05-12 08:24:20 +00001267 res = 0;
Guido van Rossum1c4f4581998-05-22 00:53:24 +00001268 else if (v->ob_type->tp_as_number != NULL &&
1269 v->ob_type->tp_as_number->nb_nonzero != NULL)
Guido van Rossum6ac258d1993-05-12 08:24:20 +00001270 res = (*v->ob_type->tp_as_number->nb_nonzero)(v);
Guido van Rossum1c4f4581998-05-22 00:53:24 +00001271 else if (v->ob_type->tp_as_mapping != NULL &&
1272 v->ob_type->tp_as_mapping->mp_length != NULL)
Guido van Rossum6ac258d1993-05-12 08:24:20 +00001273 res = (*v->ob_type->tp_as_mapping->mp_length)(v);
Guido van Rossum1c4f4581998-05-22 00:53:24 +00001274 else if (v->ob_type->tp_as_sequence != NULL &&
1275 v->ob_type->tp_as_sequence->sq_length != NULL)
Guido van Rossum6ac258d1993-05-12 08:24:20 +00001276 res = (*v->ob_type->tp_as_sequence->sq_length)(v);
1277 else
1278 res = 1;
1279 if (res > 0)
1280 res = 1;
1281 return res;
1282}
1283
Guido van Rossumc3d3f961998-04-09 17:53:59 +00001284/* equivalent of 'not v'
1285 Return -1 if an error occurred */
1286
1287int
Fred Drake100814d2000-07-09 15:48:49 +00001288PyObject_Not(PyObject *v)
Guido van Rossumc3d3f961998-04-09 17:53:59 +00001289{
1290 int res;
1291 res = PyObject_IsTrue(v);
1292 if (res < 0)
1293 return res;
1294 return res == 0;
1295}
1296
Guido van Rossum5524a591995-01-10 15:26:20 +00001297/* Coerce two numeric types to the "larger" one.
1298 Increment the reference count on each argument.
Guido van Rossume797ec12001-01-17 15:24:28 +00001299 Return value:
1300 -1 if an error occurred;
1301 0 if the coercion succeeded (and then the reference counts are increased);
1302 1 if no coercion is possible (and no error is raised).
Guido van Rossum5524a591995-01-10 15:26:20 +00001303*/
Guido van Rossum5524a591995-01-10 15:26:20 +00001304int
Fred Drake100814d2000-07-09 15:48:49 +00001305PyNumber_CoerceEx(PyObject **pv, PyObject **pw)
Guido van Rossum5524a591995-01-10 15:26:20 +00001306{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001307 register PyObject *v = *pv;
1308 register PyObject *w = *pw;
Guido van Rossum5524a591995-01-10 15:26:20 +00001309 int res;
1310
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001311 if (v->ob_type == w->ob_type && !PyInstance_Check(v)) {
1312 Py_INCREF(v);
1313 Py_INCREF(w);
Guido van Rossum5524a591995-01-10 15:26:20 +00001314 return 0;
1315 }
1316 if (v->ob_type->tp_as_number && v->ob_type->tp_as_number->nb_coerce) {
1317 res = (*v->ob_type->tp_as_number->nb_coerce)(pv, pw);
1318 if (res <= 0)
1319 return res;
1320 }
1321 if (w->ob_type->tp_as_number && w->ob_type->tp_as_number->nb_coerce) {
1322 res = (*w->ob_type->tp_as_number->nb_coerce)(pw, pv);
1323 if (res <= 0)
1324 return res;
1325 }
Guido van Rossum242c6421997-11-19 16:03:17 +00001326 return 1;
1327}
1328
Guido van Rossume797ec12001-01-17 15:24:28 +00001329/* Coerce two numeric types to the "larger" one.
1330 Increment the reference count on each argument.
1331 Return -1 and raise an exception if no coercion is possible
1332 (and then no reference count is incremented).
1333*/
Guido van Rossum242c6421997-11-19 16:03:17 +00001334int
Fred Drake100814d2000-07-09 15:48:49 +00001335PyNumber_Coerce(PyObject **pv, PyObject **pw)
Guido van Rossum242c6421997-11-19 16:03:17 +00001336{
1337 int err = PyNumber_CoerceEx(pv, pw);
1338 if (err <= 0)
1339 return err;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001340 PyErr_SetString(PyExc_TypeError, "number coercion failed");
Guido van Rossum5524a591995-01-10 15:26:20 +00001341 return -1;
1342}
1343
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001344
Guido van Rossum49b11fe1995-01-26 00:38:22 +00001345/* Test whether an object can be called */
1346
1347int
Fred Drake100814d2000-07-09 15:48:49 +00001348PyCallable_Check(PyObject *x)
Guido van Rossum49b11fe1995-01-26 00:38:22 +00001349{
1350 if (x == NULL)
1351 return 0;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001352 if (PyInstance_Check(x)) {
1353 PyObject *call = PyObject_GetAttrString(x, "__call__");
Guido van Rossum49b11fe1995-01-26 00:38:22 +00001354 if (call == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001355 PyErr_Clear();
Guido van Rossum49b11fe1995-01-26 00:38:22 +00001356 return 0;
1357 }
1358 /* Could test recursively but don't, for fear of endless
1359 recursion if some joker sets self.__call__ = self */
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001360 Py_DECREF(call);
Guido van Rossum49b11fe1995-01-26 00:38:22 +00001361 return 1;
1362 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001363 else {
1364 return x->ob_type->tp_call != NULL;
1365 }
Guido van Rossum49b11fe1995-01-26 00:38:22 +00001366}
1367
1368
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001369/*
1370NoObject is usable as a non-NULL undefined value, used by the macro None.
1371There is (and should be!) no way to create other objects of this type,
Guido van Rossum3f5da241990-12-20 15:06:42 +00001372so there is exactly one (which is indestructible, by the way).
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001373*/
1374
Guido van Rossum0c182a11992-03-27 17:26:13 +00001375/* ARGSUSED */
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001376static PyObject *
Fred Drake100814d2000-07-09 15:48:49 +00001377none_repr(PyObject *op)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001378{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001379 return PyString_FromString("None");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001380}
1381
Barry Warsaw9bf16442001-01-23 16:24:35 +00001382/* ARGUSED */
1383static void
1384none_dealloc(PyObject* ignore)
1385{
1386 /* This should never get called, but we also don't want to SEGV if
1387 * we accidently decref None out of existance.
1388 */
1389 abort();
1390}
1391
1392
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001393static PyTypeObject PyNothing_Type = {
1394 PyObject_HEAD_INIT(&PyType_Type)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001395 0,
Guido van Rossum3f5da241990-12-20 15:06:42 +00001396 "None",
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001397 0,
1398 0,
Barry Warsaw9bf16442001-01-23 16:24:35 +00001399 (destructor)none_dealloc, /*tp_dealloc*/ /*never called*/
Guido van Rossum7066dd71992-09-17 17:54:56 +00001400 0, /*tp_print*/
Guido van Rossum3f5da241990-12-20 15:06:42 +00001401 0, /*tp_getattr*/
1402 0, /*tp_setattr*/
1403 0, /*tp_compare*/
Guido van Rossum1d5735e1994-08-30 08:27:36 +00001404 (reprfunc)none_repr, /*tp_repr*/
Guido van Rossum3f5da241990-12-20 15:06:42 +00001405 0, /*tp_as_number*/
1406 0, /*tp_as_sequence*/
1407 0, /*tp_as_mapping*/
Guido van Rossum9bfef441993-03-29 10:43:31 +00001408 0, /*tp_hash */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001409};
1410
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001411PyObject _Py_NoneStruct = {
1412 PyObject_HEAD_INIT(&PyNothing_Type)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001413};
1414
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +00001415/* NotImplemented is an object that can be used to signal that an
1416 operation is not implemented for the given type combination. */
1417
1418static PyObject *
1419NotImplemented_repr(PyObject *op)
1420{
1421 return PyString_FromString("NotImplemented");
1422}
1423
1424static PyTypeObject PyNotImplemented_Type = {
1425 PyObject_HEAD_INIT(&PyType_Type)
1426 0,
1427 "NotImplemented",
1428 0,
1429 0,
Barry Warsaw9bf16442001-01-23 16:24:35 +00001430 (destructor)none_dealloc, /*tp_dealloc*/ /*never called*/
Neil Schemenauer5ed85ec2001-01-04 01:48:10 +00001431 0, /*tp_print*/
1432 0, /*tp_getattr*/
1433 0, /*tp_setattr*/
1434 0, /*tp_compare*/
1435 (reprfunc)NotImplemented_repr, /*tp_repr*/
1436 0, /*tp_as_number*/
1437 0, /*tp_as_sequence*/
1438 0, /*tp_as_mapping*/
1439 0, /*tp_hash */
1440};
1441
1442PyObject _Py_NotImplementedStruct = {
1443 PyObject_HEAD_INIT(&PyNotImplemented_Type)
1444};
1445
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001446
Guido van Rossum84a90321996-05-22 16:34:47 +00001447#ifdef Py_TRACE_REFS
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001448
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001449static PyObject refchain = {&refchain, &refchain};
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001450
Guido van Rossumaacdc9d1996-08-12 21:32:12 +00001451void
Fred Drake100814d2000-07-09 15:48:49 +00001452_Py_ResetReferences(void)
Guido van Rossume09fb551997-08-05 02:04:34 +00001453{
1454 refchain._ob_prev = refchain._ob_next = &refchain;
1455 _Py_RefTotal = 0;
1456}
1457
1458void
Fred Drake100814d2000-07-09 15:48:49 +00001459_Py_NewReference(PyObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001460{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001461 _Py_RefTotal++;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001462 op->ob_refcnt = 1;
1463 op->_ob_next = refchain._ob_next;
1464 op->_ob_prev = &refchain;
1465 refchain._ob_next->_ob_prev = op;
1466 refchain._ob_next = op;
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +00001467#ifdef COUNT_ALLOCS
1468 inc_count(op->ob_type);
1469#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001470}
1471
Guido van Rossumaacdc9d1996-08-12 21:32:12 +00001472void
Fred Drake100814d2000-07-09 15:48:49 +00001473_Py_ForgetReference(register PyObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001474{
Guido van Rossumbffd6832000-01-20 22:32:56 +00001475#ifdef SLOW_UNREF_CHECK
Guido van Rossum4c08d552000-03-10 22:55:18 +00001476 register PyObject *p;
Guido van Rossumbffd6832000-01-20 22:32:56 +00001477#endif
Guido van Rossumd7047b31995-01-02 19:07:15 +00001478 if (op->ob_refcnt < 0)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001479 Py_FatalError("UNREF negative refcnt");
Guido van Rossum2e8f6141992-09-03 20:32:55 +00001480 if (op == &refchain ||
Guido van Rossumd7047b31995-01-02 19:07:15 +00001481 op->_ob_prev->_ob_next != op || op->_ob_next->_ob_prev != op)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001482 Py_FatalError("UNREF invalid object");
Guido van Rossum2e8f6141992-09-03 20:32:55 +00001483#ifdef SLOW_UNREF_CHECK
Guido van Rossum3f5da241990-12-20 15:06:42 +00001484 for (p = refchain._ob_next; p != &refchain; p = p->_ob_next) {
1485 if (p == op)
1486 break;
1487 }
Guido van Rossumd7047b31995-01-02 19:07:15 +00001488 if (p == &refchain) /* Not found */
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001489 Py_FatalError("UNREF unknown object");
Guido van Rossum2e8f6141992-09-03 20:32:55 +00001490#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001491 op->_ob_next->_ob_prev = op->_ob_prev;
1492 op->_ob_prev->_ob_next = op->_ob_next;
Guido van Rossum2e8f6141992-09-03 20:32:55 +00001493 op->_ob_next = op->_ob_prev = NULL;
Guido van Rossumd8953cb1995-04-06 14:46:26 +00001494#ifdef COUNT_ALLOCS
Tim Peters6d6c1a32001-08-02 04:15:00 +00001495 op->ob_type->tp_frees++;
Guido van Rossumd8953cb1995-04-06 14:46:26 +00001496#endif
Guido van Rossum3f5da241990-12-20 15:06:42 +00001497}
1498
Guido van Rossumaacdc9d1996-08-12 21:32:12 +00001499void
Fred Drake100814d2000-07-09 15:48:49 +00001500_Py_Dealloc(PyObject *op)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001501{
Guido van Rossum9776adf1994-09-07 14:36:45 +00001502 destructor dealloc = op->ob_type->tp_dealloc;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001503 _Py_ForgetReference(op);
Guido van Rossum9776adf1994-09-07 14:36:45 +00001504 (*dealloc)(op);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001505}
1506
Guido van Rossumaacdc9d1996-08-12 21:32:12 +00001507void
Fred Drake100814d2000-07-09 15:48:49 +00001508_Py_PrintReferences(FILE *fp)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001509{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001510 PyObject *op;
Guido van Rossume09fb551997-08-05 02:04:34 +00001511 fprintf(fp, "Remaining objects:\n");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001512 for (op = refchain._ob_next; op != &refchain; op = op->_ob_next) {
1513 fprintf(fp, "[%d] ", op->ob_refcnt);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001514 if (PyObject_Print(op, fp, 0) != 0)
1515 PyErr_Clear();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001516 putc('\n', fp);
1517 }
1518}
1519
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +00001520PyObject *
Fred Drake100814d2000-07-09 15:48:49 +00001521_Py_GetObjects(PyObject *self, PyObject *args)
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +00001522{
1523 int i, n;
1524 PyObject *t = NULL;
1525 PyObject *res, *op;
1526
1527 if (!PyArg_ParseTuple(args, "i|O", &n, &t))
1528 return NULL;
1529 op = refchain._ob_next;
1530 res = PyList_New(0);
1531 if (res == NULL)
1532 return NULL;
1533 for (i = 0; (n == 0 || i < n) && op != &refchain; i++) {
1534 while (op == self || op == args || op == res || op == t ||
Guido van Rossum3c296022001-07-14 17:58:00 +00001535 (t != NULL && op->ob_type != (PyTypeObject *) t)) {
Sjoerd Mullender6ec3c651995-08-29 09:18:14 +00001536 op = op->_ob_next;
1537 if (op == &refchain)
1538 return res;
1539 }
1540 if (PyList_Append(res, op) < 0) {
1541 Py_DECREF(res);
1542 return NULL;
1543 }
1544 op = op->_ob_next;
1545 }
1546 return res;
1547}
1548
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001549#endif
Guido van Rossum97ead3f1996-01-12 01:24:09 +00001550
1551
1552/* Hack to force loading of cobject.o */
Guido van Rossumda9c2711996-12-05 21:58:58 +00001553PyTypeObject *_Py_cobject_hack = &PyCObject_Type;
Guido van Rossum84a90321996-05-22 16:34:47 +00001554
1555
1556/* Hack to force loading of abstract.o */
Jeremy Hylton6253f832000-07-12 12:56:19 +00001557int (*_Py_abstract_hack)(PyObject *) = &PyObject_Size;
Guido van Rossume09fb551997-08-05 02:04:34 +00001558
1559
Andrew M. Kuchling1582a3a2000-08-16 12:27:23 +00001560/* Python's malloc wrappers (see pymem.h) */
Guido van Rossume09fb551997-08-05 02:04:34 +00001561
Thomas Wouters334fb892000-07-25 12:56:38 +00001562void *
Fred Drake100814d2000-07-09 15:48:49 +00001563PyMem_Malloc(size_t nbytes)
Guido van Rossume09fb551997-08-05 02:04:34 +00001564{
1565#if _PyMem_EXTRA > 0
1566 if (nbytes == 0)
1567 nbytes = _PyMem_EXTRA;
1568#endif
Guido van Rossumb18618d2000-05-03 23:44:39 +00001569 return PyMem_MALLOC(nbytes);
Guido van Rossume09fb551997-08-05 02:04:34 +00001570}
1571
Thomas Wouters334fb892000-07-25 12:56:38 +00001572void *
1573PyMem_Realloc(void *p, size_t nbytes)
Guido van Rossume09fb551997-08-05 02:04:34 +00001574{
1575#if _PyMem_EXTRA > 0
1576 if (nbytes == 0)
1577 nbytes = _PyMem_EXTRA;
1578#endif
Guido van Rossumb18618d2000-05-03 23:44:39 +00001579 return PyMem_REALLOC(p, nbytes);
Guido van Rossume09fb551997-08-05 02:04:34 +00001580}
1581
1582void
Thomas Wouters334fb892000-07-25 12:56:38 +00001583PyMem_Free(void *p)
Guido van Rossume09fb551997-08-05 02:04:34 +00001584{
Guido van Rossumb18618d2000-05-03 23:44:39 +00001585 PyMem_FREE(p);
1586}
1587
1588
1589/* Python's object malloc wrappers (see objimpl.h) */
1590
Thomas Wouters334fb892000-07-25 12:56:38 +00001591void *
Fred Drake100814d2000-07-09 15:48:49 +00001592PyObject_Malloc(size_t nbytes)
Guido van Rossumb18618d2000-05-03 23:44:39 +00001593{
1594 return PyObject_MALLOC(nbytes);
1595}
1596
Thomas Wouters334fb892000-07-25 12:56:38 +00001597void *
1598PyObject_Realloc(void *p, size_t nbytes)
Guido van Rossumb18618d2000-05-03 23:44:39 +00001599{
1600 return PyObject_REALLOC(p, nbytes);
1601}
1602
1603void
Thomas Wouters334fb892000-07-25 12:56:38 +00001604PyObject_Free(void *p)
Guido van Rossumb18618d2000-05-03 23:44:39 +00001605{
1606 PyObject_FREE(p);
Guido van Rossume09fb551997-08-05 02:04:34 +00001607}
Guido van Rossum86610361998-04-10 22:32:46 +00001608
1609
Fred Drake41deb1e2001-02-01 05:27:45 +00001610/* Hook to clear up weak references only once the _weakref module is
1611 imported. We use a dummy implementation to simplify the code at each
1612 call site instead of requiring a test for NULL.
1613*/
1614
Fred Drakeb60654b2001-02-26 18:56:37 +00001615static void
Fred Drake41deb1e2001-02-01 05:27:45 +00001616empty_clear_weak_refs(PyObject *o)
1617{
Fred Drakeb60654b2001-02-26 18:56:37 +00001618 return;
Fred Drake41deb1e2001-02-01 05:27:45 +00001619}
1620
Fred Drakeb60654b2001-02-26 18:56:37 +00001621void (*PyObject_ClearWeakRefs)(PyObject *) = empty_clear_weak_refs;
Fred Drake41deb1e2001-02-01 05:27:45 +00001622
1623
1624
Guido van Rossum86610361998-04-10 22:32:46 +00001625/* These methods are used to control infinite recursion in repr, str, print,
1626 etc. Container objects that may recursively contain themselves,
1627 e.g. builtin dictionaries and lists, should used Py_ReprEnter() and
1628 Py_ReprLeave() to avoid infinite recursion.
1629
1630 Py_ReprEnter() returns 0 the first time it is called for a particular
1631 object and 1 every time thereafter. It returns -1 if an exception
1632 occurred. Py_ReprLeave() has no return value.
1633
1634 See dictobject.c and listobject.c for examples of use.
1635*/
1636
1637#define KEY "Py_Repr"
1638
1639int
Fred Drake100814d2000-07-09 15:48:49 +00001640Py_ReprEnter(PyObject *obj)
Guido van Rossum86610361998-04-10 22:32:46 +00001641{
1642 PyObject *dict;
1643 PyObject *list;
1644 int i;
1645
1646 dict = PyThreadState_GetDict();
1647 if (dict == NULL)
1648 return -1;
1649 list = PyDict_GetItemString(dict, KEY);
1650 if (list == NULL) {
1651 list = PyList_New(0);
1652 if (list == NULL)
1653 return -1;
1654 if (PyDict_SetItemString(dict, KEY, list) < 0)
1655 return -1;
1656 Py_DECREF(list);
1657 }
1658 i = PyList_GET_SIZE(list);
1659 while (--i >= 0) {
1660 if (PyList_GET_ITEM(list, i) == obj)
1661 return 1;
1662 }
1663 PyList_Append(list, obj);
1664 return 0;
1665}
1666
1667void
Fred Drake100814d2000-07-09 15:48:49 +00001668Py_ReprLeave(PyObject *obj)
Guido van Rossum86610361998-04-10 22:32:46 +00001669{
1670 PyObject *dict;
1671 PyObject *list;
1672 int i;
1673
1674 dict = PyThreadState_GetDict();
Guido van Rossumeb909461998-04-11 15:17:34 +00001675 if (dict == NULL)
1676 return;
Guido van Rossum86610361998-04-10 22:32:46 +00001677 list = PyDict_GetItemString(dict, KEY);
Guido van Rossumeb909461998-04-11 15:17:34 +00001678 if (list == NULL || !PyList_Check(list))
1679 return;
Guido van Rossum86610361998-04-10 22:32:46 +00001680 i = PyList_GET_SIZE(list);
1681 /* Count backwards because we always expect obj to be list[-1] */
1682 while (--i >= 0) {
1683 if (PyList_GET_ITEM(list, i) == obj) {
1684 PyList_SetSlice(list, i, i + 1, NULL);
1685 break;
1686 }
1687 }
1688}
Guido van Rossumd724b232000-03-13 16:01:29 +00001689
1690/*
1691 trashcan
1692 CT 2k0130
1693 non-recursively destroy nested objects
1694
1695 CT 2k0223
1696 everything is now done in a macro.
1697
1698 CT 2k0305
1699 modified to use functions, after Tim Peter's suggestion.
1700
1701 CT 2k0309
1702 modified to restore a possible error.
Guido van Rossum13ff8eb2000-03-25 18:39:19 +00001703
1704 CT 2k0325
1705 added better safe than sorry check for threadstate
Guido van Rossume92e6102000-04-24 15:40:53 +00001706
1707 CT 2k0422
1708 complete rewrite. We now build a chain via ob_type
1709 and save the limited number of types in ob_refcnt.
1710 This is perfect since we don't need any memory.
1711 A patch for free-threading would need just a lock.
Guido van Rossumd724b232000-03-13 16:01:29 +00001712*/
1713
Guido van Rossume92e6102000-04-24 15:40:53 +00001714#define Py_TRASHCAN_TUPLE 1
1715#define Py_TRASHCAN_LIST 2
1716#define Py_TRASHCAN_DICT 3
1717#define Py_TRASHCAN_FRAME 4
1718#define Py_TRASHCAN_TRACEBACK 5
1719/* extend here if other objects want protection */
1720
Guido van Rossumd724b232000-03-13 16:01:29 +00001721int _PyTrash_delete_nesting = 0;
Guido van Rossume92e6102000-04-24 15:40:53 +00001722
Guido van Rossumd724b232000-03-13 16:01:29 +00001723PyObject * _PyTrash_delete_later = NULL;
1724
1725void
Fred Drake100814d2000-07-09 15:48:49 +00001726_PyTrash_deposit_object(PyObject *op)
Guido van Rossumd724b232000-03-13 16:01:29 +00001727{
Guido van Rossume92e6102000-04-24 15:40:53 +00001728 int typecode;
Guido van Rossum13ff8eb2000-03-25 18:39:19 +00001729
Guido van Rossume92e6102000-04-24 15:40:53 +00001730 if (PyTuple_Check(op))
1731 typecode = Py_TRASHCAN_TUPLE;
1732 else if (PyList_Check(op))
1733 typecode = Py_TRASHCAN_LIST;
1734 else if (PyDict_Check(op))
1735 typecode = Py_TRASHCAN_DICT;
1736 else if (PyFrame_Check(op))
1737 typecode = Py_TRASHCAN_FRAME;
1738 else if (PyTraceBack_Check(op))
1739 typecode = Py_TRASHCAN_TRACEBACK;
Moshe Zadkacf703f02000-08-04 15:36:13 +00001740 else /* We have a bug here -- those are the only types in GC */ {
1741 Py_FatalError("Type not supported in GC -- internal bug");
1742 return; /* pacify compiler -- execution never here */
1743 }
Guido van Rossume92e6102000-04-24 15:40:53 +00001744 op->ob_refcnt = typecode;
Guido van Rossumd724b232000-03-13 16:01:29 +00001745
Guido van Rossume92e6102000-04-24 15:40:53 +00001746 op->ob_type = (PyTypeObject*)_PyTrash_delete_later;
1747 _PyTrash_delete_later = op;
Guido van Rossumd724b232000-03-13 16:01:29 +00001748}
1749
1750void
Fred Drake100814d2000-07-09 15:48:49 +00001751_PyTrash_destroy_chain(void)
Guido van Rossumd724b232000-03-13 16:01:29 +00001752{
1753 while (_PyTrash_delete_later) {
1754 PyObject *shredder = _PyTrash_delete_later;
Guido van Rossume92e6102000-04-24 15:40:53 +00001755 _PyTrash_delete_later = (PyObject*) shredder->ob_type;
1756
1757 switch (shredder->ob_refcnt) {
1758 case Py_TRASHCAN_TUPLE:
1759 shredder->ob_type = &PyTuple_Type;
1760 break;
1761 case Py_TRASHCAN_LIST:
1762 shredder->ob_type = &PyList_Type;
1763 break;
1764 case Py_TRASHCAN_DICT:
1765 shredder->ob_type = &PyDict_Type;
1766 break;
1767 case Py_TRASHCAN_FRAME:
1768 shredder->ob_type = &PyFrame_Type;
1769 break;
1770 case Py_TRASHCAN_TRACEBACK:
1771 shredder->ob_type = &PyTraceBack_Type;
1772 break;
1773 }
1774 _Py_NewReference(shredder);
1775
Guido van Rossumd724b232000-03-13 16:01:29 +00001776 ++_PyTrash_delete_nesting;
1777 Py_DECREF(shredder);
1778 --_PyTrash_delete_nesting;
1779 }
1780}
Neil Schemenauera35c6882001-02-27 04:45:05 +00001781
1782#ifdef WITH_PYMALLOC
1783#include "obmalloc.c"
1784#endif