Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 1 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 2 | /* Generic object operations; and implementation of None (NoObject) */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4 | #include "Python.h" |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 5 | |
Jack Jansen | 28fc880 | 2000-07-11 21:47:20 +0000 | [diff] [blame] | 6 | #ifdef macintosh |
| 7 | #include "macglue.h" |
| 8 | #endif |
| 9 | |
Guido van Rossum | e92e610 | 2000-04-24 15:40:53 +0000 | [diff] [blame] | 10 | /* just for trashcan: */ |
| 11 | #include "compile.h" |
| 12 | #include "frameobject.h" |
| 13 | #include "traceback.h" |
| 14 | |
Guido van Rossum | 6f9e433 | 1995-03-29 16:57:48 +0000 | [diff] [blame] | 15 | #if defined( Py_TRACE_REFS ) || defined( Py_REF_DEBUG ) |
Guido van Rossum | bffd683 | 2000-01-20 22:32:56 +0000 | [diff] [blame] | 16 | DL_IMPORT(long) _Py_RefTotal; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 17 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 18 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 19 | /* 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 Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 22 | |
Sjoerd Mullender | a9c3c22 | 1993-10-11 12:54:31 +0000 | [diff] [blame] | 23 | #ifdef COUNT_ALLOCS |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 24 | static PyTypeObject *type_list; |
Sjoerd Mullender | 842d2cc | 1993-10-15 16:18:48 +0000 | [diff] [blame] | 25 | extern int tuple_zero_allocs, fast_tuple_allocs; |
| 26 | extern int quick_int_allocs, quick_neg_int_allocs; |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 27 | extern int null_strings, one_strings; |
Sjoerd Mullender | a9c3c22 | 1993-10-11 12:54:31 +0000 | [diff] [blame] | 28 | void |
Fred Drake | 100814d | 2000-07-09 15:48:49 +0000 | [diff] [blame] | 29 | dump_counts(void) |
Sjoerd Mullender | a9c3c22 | 1993-10-11 12:54:31 +0000 | [diff] [blame] | 30 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 31 | PyTypeObject *tp; |
Sjoerd Mullender | a9c3c22 | 1993-10-11 12:54:31 +0000 | [diff] [blame] | 32 | |
| 33 | for (tp = type_list; tp; tp = tp->tp_next) |
Sjoerd Mullender | 52c1f51 | 1993-10-25 08:40:52 +0000 | [diff] [blame] | 34 | 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 Mullender | a9c3c22 | 1993-10-11 12:54:31 +0000 | [diff] [blame] | 43 | } |
| 44 | |
Sjoerd Mullender | 6ec3c65 | 1995-08-29 09:18:14 +0000 | [diff] [blame] | 45 | PyObject * |
Fred Drake | 100814d | 2000-07-09 15:48:49 +0000 | [diff] [blame] | 46 | get_counts(void) |
Sjoerd Mullender | 6ec3c65 | 1995-08-29 09:18:14 +0000 | [diff] [blame] | 47 | { |
| 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 Mullender | a9c3c22 | 1993-10-11 12:54:31 +0000 | [diff] [blame] | 72 | void |
Fred Drake | 100814d | 2000-07-09 15:48:49 +0000 | [diff] [blame] | 73 | inc_count(PyTypeObject *tp) |
Sjoerd Mullender | a9c3c22 | 1993-10-11 12:54:31 +0000 | [diff] [blame] | 74 | { |
| 75 | if (tp->tp_alloc == 0) { |
Guido van Rossum | d8953cb | 1995-04-06 14:46:26 +0000 | [diff] [blame] | 76 | /* first time; insert in linked list */ |
Sjoerd Mullender | a9c3c22 | 1993-10-11 12:54:31 +0000 | [diff] [blame] | 77 | if (tp->tp_next != NULL) /* sanity check */ |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 78 | Py_FatalError("XXX inc_count sanity check"); |
Sjoerd Mullender | a9c3c22 | 1993-10-11 12:54:31 +0000 | [diff] [blame] | 79 | 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 Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 88 | PyObject * |
Fred Drake | 100814d | 2000-07-09 15:48:49 +0000 | [diff] [blame] | 89 | PyObject_Init(PyObject *op, PyTypeObject *tp) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 90 | { |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 91 | if (op == NULL) { |
| 92 | PyErr_SetString(PyExc_SystemError, |
| 93 | "NULL object passed to PyObject_Init"); |
| 94 | return op; |
| 95 | } |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 96 | #ifdef WITH_CYCLE_GC |
| 97 | if (PyType_IS_GC(tp)) |
| 98 | op = (PyObject *) PyObject_FROM_GC(op); |
| 99 | #endif |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 100 | /* Any changes should be reflected in PyObject_INIT (objimpl.h) */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 101 | op->ob_type = tp; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 102 | _Py_NewReference(op); |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 103 | if (PyType_SUPPORTS_WEAKREFS(tp)) { |
| 104 | PyObject **weaklist = PyObject_GET_WEAKREFS_LISTPTR(op); |
| 105 | *weaklist = NULL; |
| 106 | } |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 107 | return op; |
| 108 | } |
| 109 | |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 110 | PyVarObject * |
Fred Drake | 100814d | 2000-07-09 15:48:49 +0000 | [diff] [blame] | 111 | PyObject_InitVar(PyVarObject *op, PyTypeObject *tp, int size) |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 112 | { |
| 113 | if (op == NULL) { |
| 114 | PyErr_SetString(PyExc_SystemError, |
| 115 | "NULL object passed to PyObject_InitVar"); |
| 116 | return op; |
| 117 | } |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 118 | #ifdef WITH_CYCLE_GC |
| 119 | if (PyType_IS_GC(tp)) |
| 120 | op = (PyVarObject *) PyObject_FROM_GC(op); |
| 121 | #endif |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 122 | /* 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 Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 126 | if (PyType_SUPPORTS_WEAKREFS(tp)) { |
| 127 | PyObject **weaklist = PyObject_GET_WEAKREFS_LISTPTR(op); |
| 128 | *weaklist = NULL; |
| 129 | } |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 130 | return op; |
| 131 | } |
| 132 | |
| 133 | PyObject * |
Fred Drake | 100814d | 2000-07-09 15:48:49 +0000 | [diff] [blame] | 134 | _PyObject_New(PyTypeObject *tp) |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 135 | { |
| 136 | PyObject *op; |
| 137 | op = (PyObject *) PyObject_MALLOC(_PyObject_SIZE(tp)); |
| 138 | if (op == NULL) |
| 139 | return PyErr_NoMemory(); |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 140 | #ifdef WITH_CYCLE_GC |
| 141 | if (PyType_IS_GC(tp)) |
| 142 | op = (PyObject *) PyObject_FROM_GC(op); |
| 143 | #endif |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 144 | return PyObject_INIT(op, tp); |
| 145 | } |
| 146 | |
Guido van Rossum | d0c87ee | 1997-05-15 21:31:03 +0000 | [diff] [blame] | 147 | PyVarObject * |
Fred Drake | 100814d | 2000-07-09 15:48:49 +0000 | [diff] [blame] | 148 | _PyObject_NewVar(PyTypeObject *tp, int size) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 149 | { |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 150 | PyVarObject *op; |
| 151 | op = (PyVarObject *) PyObject_MALLOC(_PyObject_VAR_SIZE(tp, size)); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 152 | if (op == NULL) |
Guido van Rossum | d0c87ee | 1997-05-15 21:31:03 +0000 | [diff] [blame] | 153 | return (PyVarObject *)PyErr_NoMemory(); |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 154 | #ifdef WITH_CYCLE_GC |
| 155 | if (PyType_IS_GC(tp)) |
| 156 | op = (PyVarObject *) PyObject_FROM_GC(op); |
| 157 | #endif |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 158 | return PyObject_INIT_VAR(op, tp, size); |
| 159 | } |
| 160 | |
| 161 | void |
Fred Drake | 100814d | 2000-07-09 15:48:49 +0000 | [diff] [blame] | 162 | _PyObject_Del(PyObject *op) |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 163 | { |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 164 | #ifdef WITH_CYCLE_GC |
Guido van Rossum | 4cc6ac7 | 2000-07-01 01:00:38 +0000 | [diff] [blame] | 165 | if (op && PyType_IS_GC(op->ob_type)) { |
| 166 | op = (PyObject *) PyObject_AS_GC(op); |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 167 | } |
Guido van Rossum | 4cc6ac7 | 2000-07-01 01:00:38 +0000 | [diff] [blame] | 168 | #endif |
| 169 | PyObject_FREE(op); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 170 | } |
| 171 | |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 172 | #ifndef WITH_CYCLE_GC |
| 173 | /* extension modules might need these */ |
| 174 | void _PyGC_Insert(PyObject *op) { } |
| 175 | void _PyGC_Remove(PyObject *op) { } |
| 176 | #endif |
| 177 | |
Guido van Rossum | 9093361 | 1991-06-07 16:10:43 +0000 | [diff] [blame] | 178 | int |
Fred Drake | 100814d | 2000-07-09 15:48:49 +0000 | [diff] [blame] | 179 | PyObject_Print(PyObject *op, FILE *fp, int flags) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 180 | { |
Guido van Rossum | 278ef59 | 1991-07-27 21:40:24 +0000 | [diff] [blame] | 181 | int ret = 0; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 182 | if (PyErr_CheckSignals()) |
Guido van Rossum | 9093361 | 1991-06-07 16:10:43 +0000 | [diff] [blame] | 183 | return -1; |
Guido van Rossum | 9b00dfa | 1998-04-28 16:06:54 +0000 | [diff] [blame] | 184 | #ifdef USE_STACKCHECK |
| 185 | if (PyOS_CheckStack()) { |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 186 | PyErr_SetString(PyExc_MemoryError, "stack overflow"); |
Guido van Rossum | 9b00dfa | 1998-04-28 16:06:54 +0000 | [diff] [blame] | 187 | return -1; |
| 188 | } |
| 189 | #endif |
Guido van Rossum | 687ef6e | 2000-01-12 16:28:58 +0000 | [diff] [blame] | 190 | clearerr(fp); /* Clear any previous error condition */ |
Guido van Rossum | 9093361 | 1991-06-07 16:10:43 +0000 | [diff] [blame] | 191 | if (op == NULL) { |
| 192 | fprintf(fp, "<nil>"); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 193 | } |
Guido van Rossum | 9093361 | 1991-06-07 16:10:43 +0000 | [diff] [blame] | 194 | else { |
| 195 | if (op->ob_refcnt <= 0) |
Fred Drake | a44d353 | 2000-06-30 15:01:00 +0000 | [diff] [blame] | 196 | fprintf(fp, "<refcnt %u at %p>", |
| 197 | op->ob_refcnt, op); |
Guido van Rossum | 2e8f614 | 1992-09-03 20:32:55 +0000 | [diff] [blame] | 198 | else if (op->ob_type->tp_print == NULL) { |
| 199 | if (op->ob_type->tp_repr == NULL) { |
Fred Drake | a44d353 | 2000-06-30 15:01:00 +0000 | [diff] [blame] | 200 | fprintf(fp, "<%s object at %p>", |
| 201 | op->ob_type->tp_name, op); |
Guido van Rossum | 2e8f614 | 1992-09-03 20:32:55 +0000 | [diff] [blame] | 202 | } |
| 203 | else { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 204 | PyObject *s; |
| 205 | if (flags & Py_PRINT_RAW) |
| 206 | s = PyObject_Str(op); |
Guido van Rossum | c600411 | 1993-11-05 10:22:19 +0000 | [diff] [blame] | 207 | else |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 208 | s = PyObject_Repr(op); |
Guido van Rossum | 2e8f614 | 1992-09-03 20:32:55 +0000 | [diff] [blame] | 209 | if (s == NULL) |
| 210 | ret = -1; |
Guido van Rossum | 2e8f614 | 1992-09-03 20:32:55 +0000 | [diff] [blame] | 211 | else { |
Guido van Rossum | 565798d | 1998-04-21 22:25:01 +0000 | [diff] [blame] | 212 | ret = PyObject_Print(s, fp, |
| 213 | Py_PRINT_RAW); |
Guido van Rossum | 2e8f614 | 1992-09-03 20:32:55 +0000 | [diff] [blame] | 214 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 215 | Py_XDECREF(s); |
Guido van Rossum | 2e8f614 | 1992-09-03 20:32:55 +0000 | [diff] [blame] | 216 | } |
| 217 | } |
Guido van Rossum | 9093361 | 1991-06-07 16:10:43 +0000 | [diff] [blame] | 218 | else |
Guido van Rossum | 278ef59 | 1991-07-27 21:40:24 +0000 | [diff] [blame] | 219 | ret = (*op->ob_type->tp_print)(op, fp, flags); |
Guido van Rossum | 9093361 | 1991-06-07 16:10:43 +0000 | [diff] [blame] | 220 | } |
Guido van Rossum | 278ef59 | 1991-07-27 21:40:24 +0000 | [diff] [blame] | 221 | if (ret == 0) { |
| 222 | if (ferror(fp)) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 223 | PyErr_SetFromErrno(PyExc_IOError); |
Guido van Rossum | 278ef59 | 1991-07-27 21:40:24 +0000 | [diff] [blame] | 224 | clearerr(fp); |
| 225 | ret = -1; |
| 226 | } |
| 227 | } |
| 228 | return ret; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 229 | } |
| 230 | |
Barry Warsaw | 9bf1644 | 2001-01-23 16:24:35 +0000 | [diff] [blame] | 231 | /* For debugging convenience. See Misc/gdbinit for some useful gdb hooks */ |
Barry Warsaw | bbd89b6 | 2001-01-24 04:18:13 +0000 | [diff] [blame] | 232 | void _PyObject_Dump(PyObject* op) |
Barry Warsaw | 9bf1644 | 2001-01-23 16:24:35 +0000 | [diff] [blame] | 233 | { |
Barry Warsaw | eefb107 | 2001-02-22 22:39:18 +0000 | [diff] [blame] | 234 | if (op == NULL) |
| 235 | fprintf(stderr, "NULL\n"); |
| 236 | else { |
| 237 | (void)PyObject_Print(op, stderr, 0); |
| 238 | fprintf(stderr, "\nrefcounts: %d\n", op->ob_refcnt); |
| 239 | fprintf(stderr, "address : %p\n", op); |
| 240 | } |
Barry Warsaw | 9bf1644 | 2001-01-23 16:24:35 +0000 | [diff] [blame] | 241 | } |
Barry Warsaw | 903138f | 2001-01-23 16:33:18 +0000 | [diff] [blame] | 242 | |
| 243 | #ifdef WITH_CYCLE_GC |
Barry Warsaw | bbd89b6 | 2001-01-24 04:18:13 +0000 | [diff] [blame] | 244 | void _PyGC_Dump(PyGC_Head* op) |
Barry Warsaw | 9bf1644 | 2001-01-23 16:24:35 +0000 | [diff] [blame] | 245 | { |
Barry Warsaw | bbd89b6 | 2001-01-24 04:18:13 +0000 | [diff] [blame] | 246 | _PyObject_Dump(PyObject_FROM_GC(op)); |
Barry Warsaw | 9bf1644 | 2001-01-23 16:24:35 +0000 | [diff] [blame] | 247 | } |
Barry Warsaw | 903138f | 2001-01-23 16:33:18 +0000 | [diff] [blame] | 248 | #endif /* WITH_CYCLE_GC */ |
Barry Warsaw | 9bf1644 | 2001-01-23 16:24:35 +0000 | [diff] [blame] | 249 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 250 | PyObject * |
Fred Drake | 100814d | 2000-07-09 15:48:49 +0000 | [diff] [blame] | 251 | PyObject_Repr(PyObject *v) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 252 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 253 | if (PyErr_CheckSignals()) |
Guido van Rossum | 9093361 | 1991-06-07 16:10:43 +0000 | [diff] [blame] | 254 | return NULL; |
Guido van Rossum | 9b00dfa | 1998-04-28 16:06:54 +0000 | [diff] [blame] | 255 | #ifdef USE_STACKCHECK |
| 256 | if (PyOS_CheckStack()) { |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 257 | PyErr_SetString(PyExc_MemoryError, "stack overflow"); |
Guido van Rossum | 9b00dfa | 1998-04-28 16:06:54 +0000 | [diff] [blame] | 258 | return NULL; |
| 259 | } |
| 260 | #endif |
Guido van Rossum | 9093361 | 1991-06-07 16:10:43 +0000 | [diff] [blame] | 261 | if (v == NULL) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 262 | return PyString_FromString("<NULL>"); |
Guido van Rossum | 9093361 | 1991-06-07 16:10:43 +0000 | [diff] [blame] | 263 | else if (v->ob_type->tp_repr == NULL) { |
| 264 | char buf[120]; |
Fred Drake | a44d353 | 2000-06-30 15:01:00 +0000 | [diff] [blame] | 265 | sprintf(buf, "<%.80s object at %p>", |
| 266 | v->ob_type->tp_name, v); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 267 | return PyString_FromString(buf); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 268 | } |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 269 | else { |
| 270 | PyObject *res; |
| 271 | res = (*v->ob_type->tp_repr)(v); |
| 272 | if (res == NULL) |
| 273 | return NULL; |
Fredrik Lundh | efecc7d | 2000-07-01 14:31:09 +0000 | [diff] [blame] | 274 | if (PyUnicode_Check(res)) { |
| 275 | PyObject* str; |
Fredrik Lundh | 2a1e060 | 2000-07-08 17:43:32 +0000 | [diff] [blame] | 276 | str = PyUnicode_AsUnicodeEscapeString(res); |
Marc-André Lemburg | 891bc65 | 2000-07-03 09:57:53 +0000 | [diff] [blame] | 277 | Py_DECREF(res); |
| 278 | if (str) |
Fredrik Lundh | efecc7d | 2000-07-01 14:31:09 +0000 | [diff] [blame] | 279 | res = str; |
Marc-André Lemburg | 891bc65 | 2000-07-03 09:57:53 +0000 | [diff] [blame] | 280 | else |
| 281 | return NULL; |
Fredrik Lundh | efecc7d | 2000-07-01 14:31:09 +0000 | [diff] [blame] | 282 | } |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 283 | if (!PyString_Check(res)) { |
| 284 | PyErr_Format(PyExc_TypeError, |
Guido van Rossum | 5db862d | 2000-04-10 12:46:51 +0000 | [diff] [blame] | 285 | "__repr__ returned non-string (type %.200s)", |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 286 | res->ob_type->tp_name); |
| 287 | Py_DECREF(res); |
| 288 | return NULL; |
| 289 | } |
| 290 | return res; |
| 291 | } |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 292 | } |
| 293 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 294 | PyObject * |
Fred Drake | 100814d | 2000-07-09 15:48:49 +0000 | [diff] [blame] | 295 | PyObject_Str(PyObject *v) |
Guido van Rossum | c600411 | 1993-11-05 10:22:19 +0000 | [diff] [blame] | 296 | { |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 297 | PyObject *res; |
| 298 | |
Guido van Rossum | c600411 | 1993-11-05 10:22:19 +0000 | [diff] [blame] | 299 | if (v == NULL) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 300 | return PyString_FromString("<NULL>"); |
| 301 | else if (PyString_Check(v)) { |
| 302 | Py_INCREF(v); |
Guido van Rossum | c600411 | 1993-11-05 10:22:19 +0000 | [diff] [blame] | 303 | return v; |
| 304 | } |
Guido van Rossum | 32b582b | 1995-01-17 16:35:13 +0000 | [diff] [blame] | 305 | else if (v->ob_type->tp_str != NULL) |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 306 | res = (*v->ob_type->tp_str)(v); |
Guido van Rossum | c600411 | 1993-11-05 10:22:19 +0000 | [diff] [blame] | 307 | else { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 308 | PyObject *func; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 309 | if (!PyInstance_Check(v) || |
| 310 | (func = PyObject_GetAttrString(v, "__str__")) == NULL) { |
| 311 | PyErr_Clear(); |
| 312 | return PyObject_Repr(v); |
Guido van Rossum | c600411 | 1993-11-05 10:22:19 +0000 | [diff] [blame] | 313 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 314 | res = PyEval_CallObject(func, (PyObject *)NULL); |
| 315 | Py_DECREF(func); |
Guido van Rossum | c600411 | 1993-11-05 10:22:19 +0000 | [diff] [blame] | 316 | } |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 317 | if (res == NULL) |
| 318 | return NULL; |
Marc-André Lemburg | 891bc65 | 2000-07-03 09:57:53 +0000 | [diff] [blame] | 319 | if (PyUnicode_Check(res)) { |
| 320 | PyObject* str; |
| 321 | str = PyUnicode_AsEncodedString(res, NULL, NULL); |
| 322 | Py_DECREF(res); |
| 323 | if (str) |
| 324 | res = str; |
| 325 | else |
| 326 | return NULL; |
| 327 | } |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 328 | if (!PyString_Check(res)) { |
| 329 | PyErr_Format(PyExc_TypeError, |
Guido van Rossum | 5db862d | 2000-04-10 12:46:51 +0000 | [diff] [blame] | 330 | "__str__ returned non-string (type %.200s)", |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 331 | res->ob_type->tp_name); |
| 332 | Py_DECREF(res); |
| 333 | return NULL; |
| 334 | } |
| 335 | return res; |
Guido van Rossum | c600411 | 1993-11-05 10:22:19 +0000 | [diff] [blame] | 336 | } |
| 337 | |
Guido van Rossum | a3af41d | 2001-01-18 22:07:06 +0000 | [diff] [blame] | 338 | PyObject * |
| 339 | PyObject_Unicode(PyObject *v) |
| 340 | { |
| 341 | PyObject *res; |
| 342 | |
| 343 | if (v == NULL) |
| 344 | res = PyString_FromString("<NULL>"); |
| 345 | else if (PyUnicode_Check(v)) { |
| 346 | Py_INCREF(v); |
| 347 | return v; |
| 348 | } |
Marc-André Lemburg | ae60534 | 2001-03-25 19:16:13 +0000 | [diff] [blame] | 349 | else if (PyString_Check(v)) { |
| 350 | Py_INCREF(v); |
Guido van Rossum | a3af41d | 2001-01-18 22:07:06 +0000 | [diff] [blame] | 351 | res = v; |
Marc-André Lemburg | ae60534 | 2001-03-25 19:16:13 +0000 | [diff] [blame] | 352 | } |
Guido van Rossum | a3af41d | 2001-01-18 22:07:06 +0000 | [diff] [blame] | 353 | else if (v->ob_type->tp_str != NULL) |
| 354 | res = (*v->ob_type->tp_str)(v); |
| 355 | else { |
| 356 | PyObject *func; |
| 357 | static PyObject *strstr; |
| 358 | if (strstr == NULL) { |
| 359 | strstr= PyString_InternFromString("__str__"); |
| 360 | if (strstr == NULL) |
| 361 | return NULL; |
| 362 | } |
| 363 | if (!PyInstance_Check(v) || |
| 364 | (func = PyObject_GetAttr(v, strstr)) == NULL) { |
| 365 | PyErr_Clear(); |
| 366 | res = PyObject_Repr(v); |
| 367 | } |
| 368 | else { |
| 369 | res = PyEval_CallObject(func, (PyObject *)NULL); |
| 370 | Py_DECREF(func); |
| 371 | } |
| 372 | } |
| 373 | if (res == NULL) |
| 374 | return NULL; |
| 375 | if (!PyUnicode_Check(res)) { |
| 376 | PyObject* str; |
| 377 | str = PyUnicode_FromObject(res); |
| 378 | Py_DECREF(res); |
| 379 | if (str) |
| 380 | res = str; |
| 381 | else |
| 382 | return NULL; |
| 383 | } |
| 384 | return res; |
| 385 | } |
| 386 | |
| 387 | |
Guido van Rossum | d1f06b9 | 2001-01-24 22:14:43 +0000 | [diff] [blame] | 388 | /* Macro to get the tp_richcompare field of a type if defined */ |
| 389 | #define RICHCOMPARE(t) (PyType_HasFeature((t), Py_TPFLAGS_HAVE_RICHCOMPARE) \ |
| 390 | ? (t)->tp_richcompare : NULL) |
| 391 | |
Guido van Rossum | e797ec1 | 2001-01-17 15:24:28 +0000 | [diff] [blame] | 392 | /* Map rich comparison operators to their swapped version, e.g. LT --> GT */ |
| 393 | static int swapped_op[] = {Py_GT, Py_GE, Py_EQ, Py_NE, Py_LT, Py_LE}; |
Neil Schemenauer | 5ed85ec | 2001-01-04 01:48:10 +0000 | [diff] [blame] | 394 | |
Guido van Rossum | e797ec1 | 2001-01-17 15:24:28 +0000 | [diff] [blame] | 395 | /* Try a genuine rich comparison, returning an object. Return: |
| 396 | NULL for exception; |
| 397 | NotImplemented if this particular rich comparison is not implemented or |
| 398 | undefined; |
| 399 | some object not equal to NotImplemented if it is implemented |
| 400 | (this latter object may not be a Boolean). |
| 401 | */ |
| 402 | static PyObject * |
| 403 | try_rich_compare(PyObject *v, PyObject *w, int op) |
| 404 | { |
| 405 | richcmpfunc f; |
| 406 | PyObject *res; |
| 407 | |
Guido van Rossum | d1f06b9 | 2001-01-24 22:14:43 +0000 | [diff] [blame] | 408 | if ((f = RICHCOMPARE(v->ob_type)) != NULL) { |
Guido van Rossum | e797ec1 | 2001-01-17 15:24:28 +0000 | [diff] [blame] | 409 | res = (*f)(v, w, op); |
| 410 | if (res != Py_NotImplemented) |
| 411 | return res; |
| 412 | Py_DECREF(res); |
| 413 | } |
Guido van Rossum | d1f06b9 | 2001-01-24 22:14:43 +0000 | [diff] [blame] | 414 | if ((f = RICHCOMPARE(w->ob_type)) != NULL) { |
Guido van Rossum | e797ec1 | 2001-01-17 15:24:28 +0000 | [diff] [blame] | 415 | return (*f)(w, v, swapped_op[op]); |
| 416 | } |
| 417 | res = Py_NotImplemented; |
| 418 | Py_INCREF(res); |
| 419 | return res; |
| 420 | } |
| 421 | |
| 422 | /* Try a genuine rich comparison, returning an int. Return: |
| 423 | -1 for exception (including the case where try_rich_compare() returns an |
| 424 | object that's not a Boolean); |
| 425 | 0 if the outcome is false; |
| 426 | 1 if the outcome is true; |
| 427 | 2 if this particular rich comparison is not implemented or undefined. |
| 428 | */ |
Neil Schemenauer | 5ed85ec | 2001-01-04 01:48:10 +0000 | [diff] [blame] | 429 | static int |
Guido van Rossum | e797ec1 | 2001-01-17 15:24:28 +0000 | [diff] [blame] | 430 | try_rich_compare_bool(PyObject *v, PyObject *w, int op) |
| 431 | { |
| 432 | PyObject *res; |
| 433 | int ok; |
| 434 | |
Guido van Rossum | d1f06b9 | 2001-01-24 22:14:43 +0000 | [diff] [blame] | 435 | if (RICHCOMPARE(v->ob_type) == NULL && RICHCOMPARE(w->ob_type) == NULL) |
Guido van Rossum | e797ec1 | 2001-01-17 15:24:28 +0000 | [diff] [blame] | 436 | return 2; /* Shortcut, avoid INCREF+DECREF */ |
| 437 | res = try_rich_compare(v, w, op); |
| 438 | if (res == NULL) |
| 439 | return -1; |
| 440 | if (res == Py_NotImplemented) { |
| 441 | Py_DECREF(res); |
| 442 | return 2; |
| 443 | } |
| 444 | ok = PyObject_IsTrue(res); |
| 445 | Py_DECREF(res); |
| 446 | return ok; |
| 447 | } |
| 448 | |
| 449 | /* Try rich comparisons to determine a 3-way comparison. 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 rich comparison is not implemented or undefined. |
| 455 | */ |
| 456 | static int |
| 457 | try_rich_to_3way_compare(PyObject *v, PyObject *w) |
| 458 | { |
Guido van Rossum | 2ffbf6b | 2001-01-17 21:27:02 +0000 | [diff] [blame] | 459 | static struct { int op; int outcome; } tries[3] = { |
| 460 | /* Try this operator, and if it is true, use this outcome: */ |
| 461 | {Py_EQ, 0}, |
| 462 | {Py_LT, -1}, |
| 463 | {Py_GT, 1}, |
| 464 | }; |
| 465 | int i; |
| 466 | |
Guido van Rossum | d1f06b9 | 2001-01-24 22:14:43 +0000 | [diff] [blame] | 467 | if (RICHCOMPARE(v->ob_type) == NULL && RICHCOMPARE(w->ob_type) == NULL) |
Guido van Rossum | e797ec1 | 2001-01-17 15:24:28 +0000 | [diff] [blame] | 468 | return 2; /* Shortcut */ |
Guido van Rossum | 2ffbf6b | 2001-01-17 21:27:02 +0000 | [diff] [blame] | 469 | |
| 470 | for (i = 0; i < 3; i++) { |
| 471 | switch (try_rich_compare_bool(v, w, tries[i].op)) { |
| 472 | case -1: |
| 473 | return -1; |
| 474 | case 1: |
| 475 | return tries[i].outcome; |
| 476 | } |
Guido van Rossum | e797ec1 | 2001-01-17 15:24:28 +0000 | [diff] [blame] | 477 | } |
Guido van Rossum | 2ffbf6b | 2001-01-17 21:27:02 +0000 | [diff] [blame] | 478 | |
| 479 | return 2; |
Guido van Rossum | e797ec1 | 2001-01-17 15:24:28 +0000 | [diff] [blame] | 480 | } |
| 481 | |
| 482 | /* Try a 3-way comparison, returning an int. Return: |
| 483 | -2 for an exception; |
| 484 | -1 if v < w; |
| 485 | 0 if v == w; |
| 486 | 1 if v > w; |
| 487 | 2 if this particular 3-way comparison is not implemented or undefined. |
| 488 | */ |
| 489 | static int |
| 490 | try_3way_compare(PyObject *v, PyObject *w) |
Neil Schemenauer | 5ed85ec | 2001-01-04 01:48:10 +0000 | [diff] [blame] | 491 | { |
| 492 | int c; |
Guido van Rossum | e797ec1 | 2001-01-17 15:24:28 +0000 | [diff] [blame] | 493 | cmpfunc f; |
| 494 | |
| 495 | /* Comparisons involving instances are given to instance_compare, |
| 496 | which has the same return conventions as this function. */ |
| 497 | |
| 498 | if (PyInstance_Check(v)) |
| 499 | return (*v->ob_type->tp_compare)(v, w); |
| 500 | if (PyInstance_Check(w)) |
| 501 | return (*w->ob_type->tp_compare)(v, w); |
| 502 | |
| 503 | /* If the types are equal, don't bother with coercions etc. */ |
| 504 | if (v->ob_type == w->ob_type) { |
| 505 | if ((f = v->ob_type->tp_compare) == NULL) |
| 506 | return 2; |
| 507 | c = (*f)(v, w); |
| 508 | if (PyErr_Occurred()) |
| 509 | return -2; |
| 510 | return c < 0 ? -1 : c > 0 ? 1 : 0; |
Neil Schemenauer | 5ed85ec | 2001-01-04 01:48:10 +0000 | [diff] [blame] | 511 | } |
Guido van Rossum | e797ec1 | 2001-01-17 15:24:28 +0000 | [diff] [blame] | 512 | |
| 513 | /* Try coercion; if it fails, give up */ |
| 514 | c = PyNumber_CoerceEx(&v, &w); |
| 515 | if (c < 0) |
| 516 | return -2; |
| 517 | if (c > 0) |
| 518 | return 2; |
| 519 | |
| 520 | /* Try v's comparison, if defined */ |
| 521 | if ((f = v->ob_type->tp_compare) != NULL) { |
| 522 | c = (*f)(v, w); |
| 523 | Py_DECREF(v); |
| 524 | Py_DECREF(w); |
| 525 | if (PyErr_Occurred()) |
| 526 | return -2; |
| 527 | return c < 0 ? -1 : c > 0 ? 1 : 0; |
| 528 | } |
| 529 | |
| 530 | /* Try w's comparison, if defined */ |
| 531 | if ((f = w->ob_type->tp_compare) != NULL) { |
| 532 | c = (*f)(w, v); /* swapped! */ |
| 533 | Py_DECREF(v); |
| 534 | Py_DECREF(w); |
| 535 | if (PyErr_Occurred()) |
| 536 | return -2; |
| 537 | return c < 0 ? 1 : c > 0 ? -1 : 0; /* negated! */ |
| 538 | } |
| 539 | |
| 540 | /* No comparison defined */ |
| 541 | Py_DECREF(v); |
| 542 | Py_DECREF(w); |
| 543 | return 2; |
Neil Schemenauer | 5ed85ec | 2001-01-04 01:48:10 +0000 | [diff] [blame] | 544 | } |
| 545 | |
Guido van Rossum | e797ec1 | 2001-01-17 15:24:28 +0000 | [diff] [blame] | 546 | /* Final fallback 3-way comparison, returning an int. Return: |
| 547 | -2 if an error occurred; |
| 548 | -1 if v < w; |
| 549 | 0 if v == w; |
| 550 | 1 if v > w. |
| 551 | */ |
| 552 | static int |
| 553 | default_3way_compare(PyObject *v, PyObject *w) |
| 554 | { |
| 555 | int c; |
Guido van Rossum | 8f9143d | 2001-01-22 15:59:32 +0000 | [diff] [blame] | 556 | char *vname, *wname; |
Guido van Rossum | e797ec1 | 2001-01-17 15:24:28 +0000 | [diff] [blame] | 557 | |
| 558 | if (v->ob_type == w->ob_type) { |
Barry Warsaw | b0e754d | 2001-01-20 06:24:55 +0000 | [diff] [blame] | 559 | /* When comparing these pointers, they must be cast to |
| 560 | * integer types (i.e. Py_uintptr_t, our spelling of C9X's |
| 561 | * uintptr_t). ANSI specifies that pointer compares other |
| 562 | * than == and != to non-related structures are undefined. |
| 563 | */ |
Barry Warsaw | 71ff8d5 | 2001-01-20 06:08:10 +0000 | [diff] [blame] | 564 | Py_uintptr_t vv = (Py_uintptr_t)v; |
| 565 | Py_uintptr_t ww = (Py_uintptr_t)w; |
Guido van Rossum | e797ec1 | 2001-01-17 15:24:28 +0000 | [diff] [blame] | 566 | return (vv < ww) ? -1 : (vv > ww) ? 1 : 0; |
| 567 | } |
| 568 | |
| 569 | /* Special case for Unicode */ |
| 570 | if (PyUnicode_Check(v) || PyUnicode_Check(w)) { |
| 571 | c = PyUnicode_Compare(v, w); |
| 572 | if (!PyErr_Occurred()) |
| 573 | return c; |
| 574 | /* TypeErrors are ignored: if Unicode coercion fails due |
| 575 | to one of the arguments not having the right type, we |
| 576 | continue as defined by the coercion protocol (see |
| 577 | above). Luckily, decoding errors are reported as |
| 578 | ValueErrors and are not masked by this technique. */ |
| 579 | if (!PyErr_ExceptionMatches(PyExc_TypeError)) |
| 580 | return -2; |
| 581 | PyErr_Clear(); |
| 582 | } |
| 583 | |
Guido van Rossum | 0871e93 | 2001-01-22 19:28:09 +0000 | [diff] [blame] | 584 | /* None is smaller than anything */ |
| 585 | if (v == Py_None) |
| 586 | return -1; |
| 587 | if (w == Py_None) |
| 588 | return 1; |
| 589 | |
Guido van Rossum | e797ec1 | 2001-01-17 15:24:28 +0000 | [diff] [blame] | 590 | /* different type: compare type names */ |
Guido van Rossum | 8f9143d | 2001-01-22 15:59:32 +0000 | [diff] [blame] | 591 | if (v->ob_type->tp_as_number) |
| 592 | vname = ""; |
| 593 | else |
| 594 | vname = v->ob_type->tp_name; |
| 595 | if (w->ob_type->tp_as_number) |
| 596 | wname = ""; |
| 597 | else |
| 598 | wname = w->ob_type->tp_name; |
| 599 | c = strcmp(vname, wname); |
| 600 | if (c < 0) |
| 601 | return -1; |
| 602 | if (c > 0) |
| 603 | return 1; |
| 604 | /* Same type name, or (more likely) incomparable numeric types */ |
| 605 | return ((Py_uintptr_t)(v->ob_type) < ( |
| 606 | Py_uintptr_t)(w->ob_type)) ? -1 : 1; |
Guido van Rossum | e797ec1 | 2001-01-17 15:24:28 +0000 | [diff] [blame] | 607 | } |
| 608 | |
| 609 | #define CHECK_TYPES(o) PyType_HasFeature((o)->ob_type, Py_TPFLAGS_CHECKTYPES) |
| 610 | |
Neil Schemenauer | 5ed85ec | 2001-01-04 01:48:10 +0000 | [diff] [blame] | 611 | static int |
Fred Drake | 100814d | 2000-07-09 15:48:49 +0000 | [diff] [blame] | 612 | do_cmp(PyObject *v, PyObject *w) |
Guido van Rossum | 2056684 | 1995-01-12 11:26:10 +0000 | [diff] [blame] | 613 | { |
Neil Schemenauer | 5ed85ec | 2001-01-04 01:48:10 +0000 | [diff] [blame] | 614 | int c; |
| 615 | |
Guido van Rossum | e797ec1 | 2001-01-17 15:24:28 +0000 | [diff] [blame] | 616 | c = try_rich_to_3way_compare(v, w); |
| 617 | if (c < 2) |
Neil Schemenauer | 5ed85ec | 2001-01-04 01:48:10 +0000 | [diff] [blame] | 618 | return c; |
Guido van Rossum | e797ec1 | 2001-01-17 15:24:28 +0000 | [diff] [blame] | 619 | c = try_3way_compare(v, w); |
| 620 | if (c < 2) |
| 621 | return c; |
| 622 | return default_3way_compare(v, w); |
Guido van Rossum | 2056684 | 1995-01-12 11:26:10 +0000 | [diff] [blame] | 623 | } |
| 624 | |
Guido van Rossum | 2ffbf6b | 2001-01-17 21:27:02 +0000 | [diff] [blame] | 625 | /* compare_nesting is incremented before calling compare (for |
Jeremy Hylton | 4a3dd2d | 2000-04-14 19:13:24 +0000 | [diff] [blame] | 626 | some types) and decremented on exit. If the count exceeds the |
| 627 | nesting limit, enable code to detect circular data structures. |
Guido van Rossum | a3af41d | 2001-01-18 22:07:06 +0000 | [diff] [blame] | 628 | |
| 629 | This is a tunable parameter that should only affect the performance |
| 630 | of comparisons, nothing else. Setting it high makes comparing deeply |
| 631 | nested non-cyclical data structures faster, but makes comparing cyclical |
| 632 | data structures slower. |
Jeremy Hylton | 4a3dd2d | 2000-04-14 19:13:24 +0000 | [diff] [blame] | 633 | */ |
Guido van Rossum | a3af41d | 2001-01-18 22:07:06 +0000 | [diff] [blame] | 634 | #define NESTING_LIMIT 20 |
| 635 | |
Guido van Rossum | 2ffbf6b | 2001-01-17 21:27:02 +0000 | [diff] [blame] | 636 | static int compare_nesting = 0; |
Jeremy Hylton | 4a3dd2d | 2000-04-14 19:13:24 +0000 | [diff] [blame] | 637 | |
| 638 | static PyObject* |
Fred Drake | 100814d | 2000-07-09 15:48:49 +0000 | [diff] [blame] | 639 | get_inprogress_dict(void) |
Jeremy Hylton | 4a3dd2d | 2000-04-14 19:13:24 +0000 | [diff] [blame] | 640 | { |
Guido van Rossum | 2ffbf6b | 2001-01-17 21:27:02 +0000 | [diff] [blame] | 641 | static PyObject *key; |
Jeremy Hylton | 4a3dd2d | 2000-04-14 19:13:24 +0000 | [diff] [blame] | 642 | PyObject *tstate_dict, *inprogress; |
| 643 | |
Guido van Rossum | 2ffbf6b | 2001-01-17 21:27:02 +0000 | [diff] [blame] | 644 | if (key == NULL) { |
| 645 | key = PyString_InternFromString("cmp_state"); |
| 646 | if (key == NULL) |
| 647 | return NULL; |
| 648 | } |
| 649 | |
Jeremy Hylton | 4a3dd2d | 2000-04-14 19:13:24 +0000 | [diff] [blame] | 650 | tstate_dict = PyThreadState_GetDict(); |
| 651 | if (tstate_dict == NULL) { |
| 652 | PyErr_BadInternalCall(); |
| 653 | return NULL; |
| 654 | } |
Guido van Rossum | a3af41d | 2001-01-18 22:07:06 +0000 | [diff] [blame] | 655 | |
Guido van Rossum | 2ffbf6b | 2001-01-17 21:27:02 +0000 | [diff] [blame] | 656 | inprogress = PyDict_GetItem(tstate_dict, key); |
Jeremy Hylton | 4a3dd2d | 2000-04-14 19:13:24 +0000 | [diff] [blame] | 657 | if (inprogress == NULL) { |
Jeremy Hylton | 4a3dd2d | 2000-04-14 19:13:24 +0000 | [diff] [blame] | 658 | inprogress = PyDict_New(); |
| 659 | if (inprogress == NULL) |
| 660 | return NULL; |
Guido van Rossum | 2ffbf6b | 2001-01-17 21:27:02 +0000 | [diff] [blame] | 661 | if (PyDict_SetItem(tstate_dict, key, inprogress) == -1) { |
Jeremy Hylton | 4a3dd2d | 2000-04-14 19:13:24 +0000 | [diff] [blame] | 662 | Py_DECREF(inprogress); |
| 663 | return NULL; |
| 664 | } |
Jeremy Hylton | a251ea0 | 2000-06-09 16:20:39 +0000 | [diff] [blame] | 665 | Py_DECREF(inprogress); |
Jeremy Hylton | 4a3dd2d | 2000-04-14 19:13:24 +0000 | [diff] [blame] | 666 | } |
Guido van Rossum | a3af41d | 2001-01-18 22:07:06 +0000 | [diff] [blame] | 667 | |
Jeremy Hylton | 4a3dd2d | 2000-04-14 19:13:24 +0000 | [diff] [blame] | 668 | return inprogress; |
| 669 | } |
| 670 | |
| 671 | static PyObject * |
Guido van Rossum | a3af41d | 2001-01-18 22:07:06 +0000 | [diff] [blame] | 672 | check_recursion(PyObject *v, PyObject *w, int op) |
Jeremy Hylton | 4a3dd2d | 2000-04-14 19:13:24 +0000 | [diff] [blame] | 673 | { |
Guido van Rossum | a3af41d | 2001-01-18 22:07:06 +0000 | [diff] [blame] | 674 | PyObject *inprogress; |
| 675 | PyObject *token; |
Barry Warsaw | 9d23a4e | 2000-08-18 05:01:19 +0000 | [diff] [blame] | 676 | Py_uintptr_t iv = (Py_uintptr_t)v; |
| 677 | Py_uintptr_t iw = (Py_uintptr_t)w; |
Guido van Rossum | a3af41d | 2001-01-18 22:07:06 +0000 | [diff] [blame] | 678 | PyObject *x, *y, *z; |
Jeremy Hylton | 4a3dd2d | 2000-04-14 19:13:24 +0000 | [diff] [blame] | 679 | |
Guido van Rossum | a3af41d | 2001-01-18 22:07:06 +0000 | [diff] [blame] | 680 | inprogress = get_inprogress_dict(); |
| 681 | if (inprogress == NULL) |
| 682 | return NULL; |
| 683 | |
| 684 | token = PyTuple_New(3); |
| 685 | if (token == NULL) |
| 686 | return NULL; |
| 687 | |
| 688 | if (iv <= iw) { |
| 689 | PyTuple_SET_ITEM(token, 0, x = PyLong_FromVoidPtr((void *)v)); |
| 690 | PyTuple_SET_ITEM(token, 1, y = PyLong_FromVoidPtr((void *)w)); |
| 691 | if (op >= 0) |
| 692 | op = swapped_op[op]; |
| 693 | } else { |
| 694 | PyTuple_SET_ITEM(token, 0, x = PyLong_FromVoidPtr((void *)w)); |
| 695 | PyTuple_SET_ITEM(token, 1, y = PyLong_FromVoidPtr((void *)v)); |
| 696 | } |
| 697 | PyTuple_SET_ITEM(token, 2, z = PyInt_FromLong((long)op)); |
| 698 | if (x == NULL || y == NULL || z == NULL) { |
| 699 | Py_DECREF(token); |
Jeremy Hylton | 4a3dd2d | 2000-04-14 19:13:24 +0000 | [diff] [blame] | 700 | return NULL; |
| 701 | } |
Guido van Rossum | a3af41d | 2001-01-18 22:07:06 +0000 | [diff] [blame] | 702 | |
| 703 | if (PyDict_GetItem(inprogress, token) != NULL) { |
| 704 | Py_DECREF(token); |
| 705 | return Py_None; /* Without INCREF! */ |
Jeremy Hylton | 4a3dd2d | 2000-04-14 19:13:24 +0000 | [diff] [blame] | 706 | } |
Guido van Rossum | a3af41d | 2001-01-18 22:07:06 +0000 | [diff] [blame] | 707 | |
| 708 | if (PyDict_SetItem(inprogress, token, token) < 0) { |
| 709 | Py_DECREF(token); |
| 710 | return NULL; |
| 711 | } |
| 712 | |
| 713 | return token; |
| 714 | } |
| 715 | |
| 716 | static void |
| 717 | delete_token(PyObject *token) |
| 718 | { |
| 719 | PyObject *inprogress; |
| 720 | |
| 721 | if (token == NULL || token == Py_None) |
| 722 | return; |
| 723 | inprogress = get_inprogress_dict(); |
| 724 | if (inprogress == NULL) |
| 725 | PyErr_Clear(); |
| 726 | else |
| 727 | PyDict_DelItem(inprogress, token); |
| 728 | Py_DECREF(token); |
Jeremy Hylton | 4a3dd2d | 2000-04-14 19:13:24 +0000 | [diff] [blame] | 729 | } |
| 730 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 731 | int |
Fred Drake | 100814d | 2000-07-09 15:48:49 +0000 | [diff] [blame] | 732 | PyObject_Compare(PyObject *v, PyObject *w) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 733 | { |
Guido van Rossum | a3af41d | 2001-01-18 22:07:06 +0000 | [diff] [blame] | 734 | PyTypeObject *vtp; |
Jeremy Hylton | 4a3dd2d | 2000-04-14 19:13:24 +0000 | [diff] [blame] | 735 | int result; |
| 736 | |
Jack Jansen | d49cbe1 | 2000-08-22 21:52:51 +0000 | [diff] [blame] | 737 | #if defined(USE_STACKCHECK) |
Guido van Rossum | 9c0a99e | 2000-08-30 15:53:50 +0000 | [diff] [blame] | 738 | if (PyOS_CheckStack()) { |
Jack Jansen | d49cbe1 | 2000-08-22 21:52:51 +0000 | [diff] [blame] | 739 | PyErr_SetString(PyExc_MemoryError, "Stack overflow"); |
| 740 | return -1; |
| 741 | } |
| 742 | #endif |
Guido van Rossum | c8b6df9 | 1997-05-23 00:06:51 +0000 | [diff] [blame] | 743 | if (v == NULL || w == NULL) { |
| 744 | PyErr_BadInternalCall(); |
| 745 | return -1; |
| 746 | } |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 747 | if (v == w) |
| 748 | return 0; |
Neil Schemenauer | 5ed85ec | 2001-01-04 01:48:10 +0000 | [diff] [blame] | 749 | vtp = v->ob_type; |
Guido van Rossum | 2ffbf6b | 2001-01-17 21:27:02 +0000 | [diff] [blame] | 750 | compare_nesting++; |
| 751 | if (compare_nesting > NESTING_LIMIT && |
Neil Schemenauer | 5ed85ec | 2001-01-04 01:48:10 +0000 | [diff] [blame] | 752 | (vtp->tp_as_mapping |
Guido van Rossum | a3af41d | 2001-01-18 22:07:06 +0000 | [diff] [blame] | 753 | || (vtp->tp_as_sequence |
| 754 | && !PyString_Check(v) |
| 755 | && !PyTuple_Check(v)))) { |
Neil Schemenauer | 5ed85ec | 2001-01-04 01:48:10 +0000 | [diff] [blame] | 756 | /* try to detect circular data structures */ |
Guido van Rossum | a3af41d | 2001-01-18 22:07:06 +0000 | [diff] [blame] | 757 | PyObject *token = check_recursion(v, w, -1); |
Jeremy Hylton | 4a3dd2d | 2000-04-14 19:13:24 +0000 | [diff] [blame] | 758 | |
Guido van Rossum | a3af41d | 2001-01-18 22:07:06 +0000 | [diff] [blame] | 759 | if (token == NULL) { |
| 760 | result = -1; |
Jeremy Hylton | 4a3dd2d | 2000-04-14 19:13:24 +0000 | [diff] [blame] | 761 | } |
Guido van Rossum | a3af41d | 2001-01-18 22:07:06 +0000 | [diff] [blame] | 762 | else if (token == Py_None) { |
Jeremy Hylton | 4a3dd2d | 2000-04-14 19:13:24 +0000 | [diff] [blame] | 763 | /* already comparing these objects. assume |
| 764 | they're equal until shown otherwise */ |
Neil Schemenauer | 5ed85ec | 2001-01-04 01:48:10 +0000 | [diff] [blame] | 765 | result = 0; |
Jeremy Hylton | 4a3dd2d | 2000-04-14 19:13:24 +0000 | [diff] [blame] | 766 | } |
Guido van Rossum | a3af41d | 2001-01-18 22:07:06 +0000 | [diff] [blame] | 767 | else { |
| 768 | result = do_cmp(v, w); |
| 769 | delete_token(token); |
Jeremy Hylton | 4a3dd2d | 2000-04-14 19:13:24 +0000 | [diff] [blame] | 770 | } |
Jeremy Hylton | 4a3dd2d | 2000-04-14 19:13:24 +0000 | [diff] [blame] | 771 | } |
Neil Schemenauer | 5ed85ec | 2001-01-04 01:48:10 +0000 | [diff] [blame] | 772 | else { |
| 773 | result = do_cmp(v, w); |
| 774 | } |
Guido van Rossum | 2ffbf6b | 2001-01-17 21:27:02 +0000 | [diff] [blame] | 775 | compare_nesting--; |
Guido van Rossum | e797ec1 | 2001-01-17 15:24:28 +0000 | [diff] [blame] | 776 | return result < 0 ? -1 : result; |
| 777 | } |
| 778 | |
| 779 | static PyObject * |
| 780 | try_3way_to_rich_compare(PyObject *v, PyObject *w, int op) |
| 781 | { |
| 782 | int c; |
| 783 | PyObject *result; |
| 784 | |
| 785 | c = try_3way_compare(v, w); |
Guido van Rossum | e797ec1 | 2001-01-17 15:24:28 +0000 | [diff] [blame] | 786 | if (c >= 2) |
| 787 | c = default_3way_compare(v, w); |
Guido van Rossum | 2da0ea8 | 2001-02-22 22:18:04 +0000 | [diff] [blame] | 788 | if (c <= -2) |
| 789 | return NULL; |
Guido van Rossum | e797ec1 | 2001-01-17 15:24:28 +0000 | [diff] [blame] | 790 | switch (op) { |
| 791 | case Py_LT: c = c < 0; break; |
| 792 | case Py_LE: c = c <= 0; break; |
| 793 | case Py_EQ: c = c == 0; break; |
| 794 | case Py_NE: c = c != 0; break; |
| 795 | case Py_GT: c = c > 0; break; |
| 796 | case Py_GE: c = c >= 0; break; |
| 797 | } |
| 798 | result = c ? Py_True : Py_False; |
| 799 | Py_INCREF(result); |
Jeremy Hylton | 4a3dd2d | 2000-04-14 19:13:24 +0000 | [diff] [blame] | 800 | return result; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 801 | } |
| 802 | |
Neil Schemenauer | d38855c | 2001-01-21 16:25:18 +0000 | [diff] [blame] | 803 | static PyObject * |
Guido van Rossum | e797ec1 | 2001-01-17 15:24:28 +0000 | [diff] [blame] | 804 | do_richcmp(PyObject *v, PyObject *w, int op) |
| 805 | { |
| 806 | PyObject *res; |
| 807 | |
| 808 | res = try_rich_compare(v, w, op); |
| 809 | if (res != Py_NotImplemented) |
| 810 | return res; |
| 811 | Py_DECREF(res); |
| 812 | |
| 813 | return try_3way_to_rich_compare(v, w, op); |
| 814 | } |
| 815 | |
| 816 | PyObject * |
| 817 | PyObject_RichCompare(PyObject *v, PyObject *w, int op) |
| 818 | { |
| 819 | PyObject *res; |
| 820 | |
| 821 | assert(Py_LT <= op && op <= Py_GE); |
| 822 | |
Guido van Rossum | 2ffbf6b | 2001-01-17 21:27:02 +0000 | [diff] [blame] | 823 | compare_nesting++; |
| 824 | if (compare_nesting > NESTING_LIMIT && |
| 825 | (v->ob_type->tp_as_mapping |
Guido van Rossum | a3af41d | 2001-01-18 22:07:06 +0000 | [diff] [blame] | 826 | || (v->ob_type->tp_as_sequence |
| 827 | && !PyString_Check(v) |
| 828 | && !PyTuple_Check(v)))) { |
Guido van Rossum | 2ffbf6b | 2001-01-17 21:27:02 +0000 | [diff] [blame] | 829 | /* try to detect circular data structures */ |
Guido van Rossum | a3af41d | 2001-01-18 22:07:06 +0000 | [diff] [blame] | 830 | PyObject *token = check_recursion(v, w, op); |
Guido van Rossum | 2ffbf6b | 2001-01-17 21:27:02 +0000 | [diff] [blame] | 831 | |
Guido van Rossum | a3af41d | 2001-01-18 22:07:06 +0000 | [diff] [blame] | 832 | if (token == NULL) { |
| 833 | res = NULL; |
Guido van Rossum | e797ec1 | 2001-01-17 15:24:28 +0000 | [diff] [blame] | 834 | } |
Guido van Rossum | a3af41d | 2001-01-18 22:07:06 +0000 | [diff] [blame] | 835 | else if (token == Py_None) { |
| 836 | /* already comparing these objects with this operator. |
| 837 | assume they're equal until shown otherwise */ |
| 838 | if (op == Py_EQ) |
Guido van Rossum | 2ffbf6b | 2001-01-17 21:27:02 +0000 | [diff] [blame] | 839 | res = Py_True; |
Guido van Rossum | a3af41d | 2001-01-18 22:07:06 +0000 | [diff] [blame] | 840 | else if (op == Py_NE) |
Guido van Rossum | 2ffbf6b | 2001-01-17 21:27:02 +0000 | [diff] [blame] | 841 | res = Py_False; |
Guido van Rossum | a3af41d | 2001-01-18 22:07:06 +0000 | [diff] [blame] | 842 | else { |
| 843 | PyErr_SetString(PyExc_ValueError, |
| 844 | "can't order recursive values"); |
| 845 | res = NULL; |
| 846 | } |
| 847 | Py_XINCREF(res); |
Guido van Rossum | 2ffbf6b | 2001-01-17 21:27:02 +0000 | [diff] [blame] | 848 | } |
Guido van Rossum | a3af41d | 2001-01-18 22:07:06 +0000 | [diff] [blame] | 849 | else { |
| 850 | res = do_richcmp(v, w, op); |
| 851 | delete_token(token); |
Guido van Rossum | 2ffbf6b | 2001-01-17 21:27:02 +0000 | [diff] [blame] | 852 | } |
Guido van Rossum | e797ec1 | 2001-01-17 15:24:28 +0000 | [diff] [blame] | 853 | } |
Guido van Rossum | 2ffbf6b | 2001-01-17 21:27:02 +0000 | [diff] [blame] | 854 | else { |
| 855 | res = do_richcmp(v, w, op); |
| 856 | } |
Guido van Rossum | 2ffbf6b | 2001-01-17 21:27:02 +0000 | [diff] [blame] | 857 | compare_nesting--; |
Guido van Rossum | e797ec1 | 2001-01-17 15:24:28 +0000 | [diff] [blame] | 858 | return res; |
| 859 | } |
| 860 | |
| 861 | int |
| 862 | PyObject_RichCompareBool(PyObject *v, PyObject *w, int op) |
| 863 | { |
| 864 | PyObject *res = PyObject_RichCompare(v, w, op); |
| 865 | int ok; |
| 866 | |
| 867 | if (res == NULL) |
| 868 | return -1; |
| 869 | ok = PyObject_IsTrue(res); |
| 870 | Py_DECREF(res); |
| 871 | return ok; |
| 872 | } |
Fred Drake | 13634cf | 2000-06-29 19:17:04 +0000 | [diff] [blame] | 873 | |
| 874 | /* Set of hash utility functions to help maintaining the invariant that |
| 875 | iff a==b then hash(a)==hash(b) |
| 876 | |
| 877 | All the utility functions (_Py_Hash*()) return "-1" to signify an error. |
| 878 | */ |
| 879 | |
| 880 | long |
Fred Drake | 100814d | 2000-07-09 15:48:49 +0000 | [diff] [blame] | 881 | _Py_HashDouble(double v) |
Fred Drake | 13634cf | 2000-06-29 19:17:04 +0000 | [diff] [blame] | 882 | { |
Tim Peters | 39dce29 | 2000-08-15 03:34:48 +0000 | [diff] [blame] | 883 | double intpart, fractpart; |
| 884 | int expo; |
| 885 | long hipart; |
| 886 | long x; /* the final hash value */ |
| 887 | /* This is designed so that Python numbers of different types |
| 888 | * that compare equal hash to the same value; otherwise comparisons |
| 889 | * of mapping keys will turn out weird. |
| 890 | */ |
| 891 | |
| 892 | #ifdef MPW /* MPW C modf expects pointer to extended as second argument */ |
| 893 | { |
| 894 | extended e; |
| 895 | fractpart = modf(v, &e); |
| 896 | intpart = e; |
| 897 | } |
| 898 | #else |
| 899 | fractpart = modf(v, &intpart); |
| 900 | #endif |
| 901 | if (fractpart == 0.0) { |
| 902 | /* This must return the same hash as an equal int or long. */ |
| 903 | if (intpart > LONG_MAX || -intpart > LONG_MAX) { |
| 904 | /* Convert to long and use its hash. */ |
| 905 | PyObject *plong; /* converted to Python long */ |
| 906 | if (Py_IS_INFINITY(intpart)) |
| 907 | /* can't convert to long int -- arbitrary */ |
| 908 | v = v < 0 ? -271828.0 : 314159.0; |
| 909 | plong = PyLong_FromDouble(v); |
| 910 | if (plong == NULL) |
| 911 | return -1; |
| 912 | x = PyObject_Hash(plong); |
| 913 | Py_DECREF(plong); |
| 914 | return x; |
| 915 | } |
| 916 | /* Fits in a C long == a Python int, so is its own hash. */ |
| 917 | x = (long)intpart; |
| 918 | if (x == -1) |
| 919 | x = -2; |
| 920 | return x; |
| 921 | } |
| 922 | /* The fractional part is non-zero, so we don't have to worry about |
| 923 | * making this match the hash of some other type. |
| 924 | * Use frexp to get at the bits in the double. |
Fred Drake | 13634cf | 2000-06-29 19:17:04 +0000 | [diff] [blame] | 925 | * Since the VAX D double format has 56 mantissa bits, which is the |
| 926 | * most of any double format in use, each of these parts may have as |
| 927 | * many as (but no more than) 56 significant bits. |
Tim Peters | 39dce29 | 2000-08-15 03:34:48 +0000 | [diff] [blame] | 928 | * So, assuming sizeof(long) >= 4, each part can be broken into two |
| 929 | * longs; frexp and multiplication are used to do that. |
| 930 | * Also, since the Cray double format has 15 exponent bits, which is |
| 931 | * the most of any double format in use, shifting the exponent field |
| 932 | * left by 15 won't overflow a long (again assuming sizeof(long) >= 4). |
Fred Drake | 13634cf | 2000-06-29 19:17:04 +0000 | [diff] [blame] | 933 | */ |
Tim Peters | 39dce29 | 2000-08-15 03:34:48 +0000 | [diff] [blame] | 934 | v = frexp(v, &expo); |
| 935 | v *= 2147483648.0; /* 2**31 */ |
| 936 | hipart = (long)v; /* take the top 32 bits */ |
| 937 | v = (v - (double)hipart) * 2147483648.0; /* get the next 32 bits */ |
| 938 | x = hipart + (long)v + (expo << 15); |
| 939 | if (x == -1) |
| 940 | x = -2; |
| 941 | return x; |
Fred Drake | 13634cf | 2000-06-29 19:17:04 +0000 | [diff] [blame] | 942 | } |
| 943 | |
| 944 | long |
Fred Drake | 100814d | 2000-07-09 15:48:49 +0000 | [diff] [blame] | 945 | _Py_HashPointer(void *p) |
Fred Drake | 13634cf | 2000-06-29 19:17:04 +0000 | [diff] [blame] | 946 | { |
| 947 | #if SIZEOF_LONG >= SIZEOF_VOID_P |
| 948 | return (long)p; |
| 949 | #else |
| 950 | /* convert to a Python long and hash that */ |
| 951 | PyObject* longobj; |
| 952 | long x; |
| 953 | |
| 954 | if ((longobj = PyLong_FromVoidPtr(p)) == NULL) { |
| 955 | x = -1; |
| 956 | goto finally; |
| 957 | } |
| 958 | x = PyObject_Hash(longobj); |
| 959 | |
| 960 | finally: |
| 961 | Py_XDECREF(longobj); |
| 962 | return x; |
| 963 | #endif |
| 964 | } |
| 965 | |
| 966 | |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 967 | long |
Fred Drake | 100814d | 2000-07-09 15:48:49 +0000 | [diff] [blame] | 968 | PyObject_Hash(PyObject *v) |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 969 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 970 | PyTypeObject *tp = v->ob_type; |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 971 | if (tp->tp_hash != NULL) |
| 972 | return (*tp->tp_hash)(v); |
Guido van Rossum | d1f06b9 | 2001-01-24 22:14:43 +0000 | [diff] [blame] | 973 | if (tp->tp_compare == NULL && RICHCOMPARE(tp) == NULL) { |
Fred Drake | 13634cf | 2000-06-29 19:17:04 +0000 | [diff] [blame] | 974 | return _Py_HashPointer(v); /* Use address as hash value */ |
| 975 | } |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 976 | /* If there's a cmp but no hash defined, the object can't be hashed */ |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 977 | PyErr_SetString(PyExc_TypeError, "unhashable type"); |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 978 | return -1; |
| 979 | } |
| 980 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 981 | PyObject * |
Fred Drake | 100814d | 2000-07-09 15:48:49 +0000 | [diff] [blame] | 982 | PyObject_GetAttrString(PyObject *v, char *name) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 983 | { |
Guido van Rossum | d8eb1b3 | 1996-08-09 20:52:03 +0000 | [diff] [blame] | 984 | if (v->ob_type->tp_getattro != NULL) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 985 | PyObject *w, *res; |
Guido van Rossum | c6d0670 | 1997-01-18 07:57:16 +0000 | [diff] [blame] | 986 | w = PyString_InternFromString(name); |
Guido van Rossum | d8eb1b3 | 1996-08-09 20:52:03 +0000 | [diff] [blame] | 987 | if (w == NULL) |
| 988 | return NULL; |
| 989 | res = (*v->ob_type->tp_getattro)(v, w); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 990 | Py_XDECREF(w); |
Guido van Rossum | d8eb1b3 | 1996-08-09 20:52:03 +0000 | [diff] [blame] | 991 | return res; |
| 992 | } |
| 993 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 994 | if (v->ob_type->tp_getattr == NULL) { |
Guido van Rossum | db93516 | 1998-01-19 22:16:36 +0000 | [diff] [blame] | 995 | PyErr_Format(PyExc_AttributeError, |
| 996 | "'%.50s' object has no attribute '%.400s'", |
| 997 | v->ob_type->tp_name, |
| 998 | name); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 999 | return NULL; |
| 1000 | } |
| 1001 | else { |
| 1002 | return (*v->ob_type->tp_getattr)(v, name); |
| 1003 | } |
| 1004 | } |
| 1005 | |
| 1006 | int |
Fred Drake | 100814d | 2000-07-09 15:48:49 +0000 | [diff] [blame] | 1007 | PyObject_HasAttrString(PyObject *v, char *name) |
Guido van Rossum | ed18fdc | 1993-07-11 19:55:34 +0000 | [diff] [blame] | 1008 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1009 | PyObject *res = PyObject_GetAttrString(v, name); |
Guido van Rossum | ed18fdc | 1993-07-11 19:55:34 +0000 | [diff] [blame] | 1010 | if (res != NULL) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1011 | Py_DECREF(res); |
Guido van Rossum | ed18fdc | 1993-07-11 19:55:34 +0000 | [diff] [blame] | 1012 | return 1; |
| 1013 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1014 | PyErr_Clear(); |
Guido van Rossum | ed18fdc | 1993-07-11 19:55:34 +0000 | [diff] [blame] | 1015 | return 0; |
| 1016 | } |
| 1017 | |
| 1018 | int |
Fred Drake | 100814d | 2000-07-09 15:48:49 +0000 | [diff] [blame] | 1019 | PyObject_SetAttrString(PyObject *v, char *name, PyObject *w) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1020 | { |
Guido van Rossum | d8eb1b3 | 1996-08-09 20:52:03 +0000 | [diff] [blame] | 1021 | if (v->ob_type->tp_setattro != NULL) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1022 | PyObject *s; |
Guido van Rossum | d8eb1b3 | 1996-08-09 20:52:03 +0000 | [diff] [blame] | 1023 | int res; |
Guido van Rossum | c6d0670 | 1997-01-18 07:57:16 +0000 | [diff] [blame] | 1024 | s = PyString_InternFromString(name); |
Guido van Rossum | d8eb1b3 | 1996-08-09 20:52:03 +0000 | [diff] [blame] | 1025 | if (s == NULL) |
Guido van Rossum | b7fc304 | 1996-09-11 22:51:25 +0000 | [diff] [blame] | 1026 | return -1; |
Guido van Rossum | d8eb1b3 | 1996-08-09 20:52:03 +0000 | [diff] [blame] | 1027 | res = (*v->ob_type->tp_setattro)(v, s, w); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1028 | Py_XDECREF(s); |
Guido van Rossum | d8eb1b3 | 1996-08-09 20:52:03 +0000 | [diff] [blame] | 1029 | return res; |
| 1030 | } |
| 1031 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1032 | if (v->ob_type->tp_setattr == NULL) { |
| 1033 | if (v->ob_type->tp_getattr == NULL) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1034 | PyErr_SetString(PyExc_TypeError, |
Guido van Rossum | 3ea7412 | 1991-12-24 13:28:03 +0000 | [diff] [blame] | 1035 | "attribute-less object (assign or del)"); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1036 | else |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1037 | PyErr_SetString(PyExc_TypeError, |
Guido van Rossum | 3ea7412 | 1991-12-24 13:28:03 +0000 | [diff] [blame] | 1038 | "object has read-only attributes"); |
Guido van Rossum | 73531a3 | 1990-12-20 23:12:40 +0000 | [diff] [blame] | 1039 | return -1; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1040 | } |
| 1041 | else { |
| 1042 | return (*v->ob_type->tp_setattr)(v, name, w); |
| 1043 | } |
| 1044 | } |
| 1045 | |
Marc-André Lemburg | e44e507 | 2000-09-18 16:20:57 +0000 | [diff] [blame] | 1046 | /* Internal API needed by PyObject_GetAttr(): */ |
| 1047 | extern |
| 1048 | PyObject *_PyUnicode_AsDefaultEncodedString(PyObject *unicode, |
| 1049 | const char *errors); |
| 1050 | |
Guido van Rossum | 98ff96a | 1997-05-20 18:34:44 +0000 | [diff] [blame] | 1051 | PyObject * |
Fred Drake | 100814d | 2000-07-09 15:48:49 +0000 | [diff] [blame] | 1052 | PyObject_GetAttr(PyObject *v, PyObject *name) |
Guido van Rossum | 98ff96a | 1997-05-20 18:34:44 +0000 | [diff] [blame] | 1053 | { |
Marc-André Lemburg | e44e507 | 2000-09-18 16:20:57 +0000 | [diff] [blame] | 1054 | /* The Unicode to string conversion is done here because the |
| 1055 | existing tp_getattro slots expect a string object as name |
| 1056 | and we wouldn't want to break those. */ |
| 1057 | if (PyUnicode_Check(name)) { |
| 1058 | name = _PyUnicode_AsDefaultEncodedString(name, NULL); |
| 1059 | if (name == NULL) |
| 1060 | return NULL; |
| 1061 | } |
Jeremy Hylton | 99a8f90 | 2000-06-23 14:36:32 +0000 | [diff] [blame] | 1062 | |
| 1063 | if (!PyString_Check(name)) { |
| 1064 | PyErr_SetString(PyExc_TypeError, |
| 1065 | "attribute name must be string"); |
| 1066 | return NULL; |
| 1067 | } |
Marc-André Lemburg | e44e507 | 2000-09-18 16:20:57 +0000 | [diff] [blame] | 1068 | if (v->ob_type->tp_getattro != NULL) |
| 1069 | return (*v->ob_type->tp_getattro)(v, name); |
| 1070 | else |
Jeremy Hylton | 99a8f90 | 2000-06-23 14:36:32 +0000 | [diff] [blame] | 1071 | return PyObject_GetAttrString(v, PyString_AS_STRING(name)); |
Guido van Rossum | 98ff96a | 1997-05-20 18:34:44 +0000 | [diff] [blame] | 1072 | } |
| 1073 | |
| 1074 | int |
Fred Drake | 100814d | 2000-07-09 15:48:49 +0000 | [diff] [blame] | 1075 | PyObject_HasAttr(PyObject *v, PyObject *name) |
Guido van Rossum | 98ff96a | 1997-05-20 18:34:44 +0000 | [diff] [blame] | 1076 | { |
| 1077 | PyObject *res = PyObject_GetAttr(v, name); |
| 1078 | if (res != NULL) { |
| 1079 | Py_DECREF(res); |
| 1080 | return 1; |
| 1081 | } |
| 1082 | PyErr_Clear(); |
| 1083 | return 0; |
| 1084 | } |
| 1085 | |
| 1086 | int |
Fred Drake | 100814d | 2000-07-09 15:48:49 +0000 | [diff] [blame] | 1087 | PyObject_SetAttr(PyObject *v, PyObject *name, PyObject *value) |
Guido van Rossum | 98ff96a | 1997-05-20 18:34:44 +0000 | [diff] [blame] | 1088 | { |
| 1089 | int err; |
Marc-André Lemburg | e44e507 | 2000-09-18 16:20:57 +0000 | [diff] [blame] | 1090 | |
| 1091 | /* The Unicode to string conversion is done here because the |
| 1092 | existing tp_setattro slots expect a string object as name |
| 1093 | and we wouldn't want to break those. */ |
| 1094 | if (PyUnicode_Check(name)) { |
| 1095 | name = PyUnicode_AsEncodedString(name, NULL, NULL); |
| 1096 | if (name == NULL) |
| 1097 | return -1; |
Jeremy Hylton | 99a8f90 | 2000-06-23 14:36:32 +0000 | [diff] [blame] | 1098 | } |
Marc-André Lemburg | e44e507 | 2000-09-18 16:20:57 +0000 | [diff] [blame] | 1099 | else |
| 1100 | Py_INCREF(name); |
| 1101 | |
| 1102 | if (!PyString_Check(name)){ |
Jeremy Hylton | 99a8f90 | 2000-06-23 14:36:32 +0000 | [diff] [blame] | 1103 | PyErr_SetString(PyExc_TypeError, |
| 1104 | "attribute name must be string"); |
| 1105 | err = -1; |
| 1106 | } |
Marc-André Lemburg | e44e507 | 2000-09-18 16:20:57 +0000 | [diff] [blame] | 1107 | else { |
| 1108 | PyString_InternInPlace(&name); |
| 1109 | if (v->ob_type->tp_setattro != NULL) |
| 1110 | err = (*v->ob_type->tp_setattro)(v, name, value); |
| 1111 | else |
| 1112 | err = PyObject_SetAttrString(v, |
| 1113 | PyString_AS_STRING(name), value); |
| 1114 | } |
| 1115 | |
Guido van Rossum | 98ff96a | 1997-05-20 18:34:44 +0000 | [diff] [blame] | 1116 | Py_DECREF(name); |
| 1117 | return err; |
| 1118 | } |
| 1119 | |
Guido van Rossum | 6ac258d | 1993-05-12 08:24:20 +0000 | [diff] [blame] | 1120 | /* Test a value used as condition, e.g., in a for or if statement. |
| 1121 | Return -1 if an error occurred */ |
| 1122 | |
| 1123 | int |
Fred Drake | 100814d | 2000-07-09 15:48:49 +0000 | [diff] [blame] | 1124 | PyObject_IsTrue(PyObject *v) |
Guido van Rossum | 6ac258d | 1993-05-12 08:24:20 +0000 | [diff] [blame] | 1125 | { |
| 1126 | int res; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1127 | if (v == Py_None) |
Guido van Rossum | 6ac258d | 1993-05-12 08:24:20 +0000 | [diff] [blame] | 1128 | res = 0; |
Guido van Rossum | 1c4f458 | 1998-05-22 00:53:24 +0000 | [diff] [blame] | 1129 | else if (v->ob_type->tp_as_number != NULL && |
| 1130 | v->ob_type->tp_as_number->nb_nonzero != NULL) |
Guido van Rossum | 6ac258d | 1993-05-12 08:24:20 +0000 | [diff] [blame] | 1131 | res = (*v->ob_type->tp_as_number->nb_nonzero)(v); |
Guido van Rossum | 1c4f458 | 1998-05-22 00:53:24 +0000 | [diff] [blame] | 1132 | else if (v->ob_type->tp_as_mapping != NULL && |
| 1133 | v->ob_type->tp_as_mapping->mp_length != NULL) |
Guido van Rossum | 6ac258d | 1993-05-12 08:24:20 +0000 | [diff] [blame] | 1134 | res = (*v->ob_type->tp_as_mapping->mp_length)(v); |
Guido van Rossum | 1c4f458 | 1998-05-22 00:53:24 +0000 | [diff] [blame] | 1135 | else if (v->ob_type->tp_as_sequence != NULL && |
| 1136 | v->ob_type->tp_as_sequence->sq_length != NULL) |
Guido van Rossum | 6ac258d | 1993-05-12 08:24:20 +0000 | [diff] [blame] | 1137 | res = (*v->ob_type->tp_as_sequence->sq_length)(v); |
| 1138 | else |
| 1139 | res = 1; |
| 1140 | if (res > 0) |
| 1141 | res = 1; |
| 1142 | return res; |
| 1143 | } |
| 1144 | |
Guido van Rossum | c3d3f96 | 1998-04-09 17:53:59 +0000 | [diff] [blame] | 1145 | /* equivalent of 'not v' |
| 1146 | Return -1 if an error occurred */ |
| 1147 | |
| 1148 | int |
Fred Drake | 100814d | 2000-07-09 15:48:49 +0000 | [diff] [blame] | 1149 | PyObject_Not(PyObject *v) |
Guido van Rossum | c3d3f96 | 1998-04-09 17:53:59 +0000 | [diff] [blame] | 1150 | { |
| 1151 | int res; |
| 1152 | res = PyObject_IsTrue(v); |
| 1153 | if (res < 0) |
| 1154 | return res; |
| 1155 | return res == 0; |
| 1156 | } |
| 1157 | |
Guido van Rossum | 5524a59 | 1995-01-10 15:26:20 +0000 | [diff] [blame] | 1158 | /* Coerce two numeric types to the "larger" one. |
| 1159 | Increment the reference count on each argument. |
Guido van Rossum | e797ec1 | 2001-01-17 15:24:28 +0000 | [diff] [blame] | 1160 | Return value: |
| 1161 | -1 if an error occurred; |
| 1162 | 0 if the coercion succeeded (and then the reference counts are increased); |
| 1163 | 1 if no coercion is possible (and no error is raised). |
Guido van Rossum | 5524a59 | 1995-01-10 15:26:20 +0000 | [diff] [blame] | 1164 | */ |
Guido van Rossum | 5524a59 | 1995-01-10 15:26:20 +0000 | [diff] [blame] | 1165 | int |
Fred Drake | 100814d | 2000-07-09 15:48:49 +0000 | [diff] [blame] | 1166 | PyNumber_CoerceEx(PyObject **pv, PyObject **pw) |
Guido van Rossum | 5524a59 | 1995-01-10 15:26:20 +0000 | [diff] [blame] | 1167 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1168 | register PyObject *v = *pv; |
| 1169 | register PyObject *w = *pw; |
Guido van Rossum | 5524a59 | 1995-01-10 15:26:20 +0000 | [diff] [blame] | 1170 | int res; |
| 1171 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1172 | if (v->ob_type == w->ob_type && !PyInstance_Check(v)) { |
| 1173 | Py_INCREF(v); |
| 1174 | Py_INCREF(w); |
Guido van Rossum | 5524a59 | 1995-01-10 15:26:20 +0000 | [diff] [blame] | 1175 | return 0; |
| 1176 | } |
| 1177 | if (v->ob_type->tp_as_number && v->ob_type->tp_as_number->nb_coerce) { |
| 1178 | res = (*v->ob_type->tp_as_number->nb_coerce)(pv, pw); |
| 1179 | if (res <= 0) |
| 1180 | return res; |
| 1181 | } |
| 1182 | if (w->ob_type->tp_as_number && w->ob_type->tp_as_number->nb_coerce) { |
| 1183 | res = (*w->ob_type->tp_as_number->nb_coerce)(pw, pv); |
| 1184 | if (res <= 0) |
| 1185 | return res; |
| 1186 | } |
Guido van Rossum | 242c642 | 1997-11-19 16:03:17 +0000 | [diff] [blame] | 1187 | return 1; |
| 1188 | } |
| 1189 | |
Guido van Rossum | e797ec1 | 2001-01-17 15:24:28 +0000 | [diff] [blame] | 1190 | /* Coerce two numeric types to the "larger" one. |
| 1191 | Increment the reference count on each argument. |
| 1192 | Return -1 and raise an exception if no coercion is possible |
| 1193 | (and then no reference count is incremented). |
| 1194 | */ |
Guido van Rossum | 242c642 | 1997-11-19 16:03:17 +0000 | [diff] [blame] | 1195 | int |
Fred Drake | 100814d | 2000-07-09 15:48:49 +0000 | [diff] [blame] | 1196 | PyNumber_Coerce(PyObject **pv, PyObject **pw) |
Guido van Rossum | 242c642 | 1997-11-19 16:03:17 +0000 | [diff] [blame] | 1197 | { |
| 1198 | int err = PyNumber_CoerceEx(pv, pw); |
| 1199 | if (err <= 0) |
| 1200 | return err; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1201 | PyErr_SetString(PyExc_TypeError, "number coercion failed"); |
Guido van Rossum | 5524a59 | 1995-01-10 15:26:20 +0000 | [diff] [blame] | 1202 | return -1; |
| 1203 | } |
| 1204 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1205 | |
Guido van Rossum | 49b11fe | 1995-01-26 00:38:22 +0000 | [diff] [blame] | 1206 | /* Test whether an object can be called */ |
| 1207 | |
| 1208 | int |
Fred Drake | 100814d | 2000-07-09 15:48:49 +0000 | [diff] [blame] | 1209 | PyCallable_Check(PyObject *x) |
Guido van Rossum | 49b11fe | 1995-01-26 00:38:22 +0000 | [diff] [blame] | 1210 | { |
| 1211 | if (x == NULL) |
| 1212 | return 0; |
| 1213 | if (x->ob_type->tp_call != NULL || |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1214 | PyFunction_Check(x) || |
| 1215 | PyMethod_Check(x) || |
| 1216 | PyCFunction_Check(x) || |
| 1217 | PyClass_Check(x)) |
Guido van Rossum | 49b11fe | 1995-01-26 00:38:22 +0000 | [diff] [blame] | 1218 | return 1; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1219 | if (PyInstance_Check(x)) { |
| 1220 | PyObject *call = PyObject_GetAttrString(x, "__call__"); |
Guido van Rossum | 49b11fe | 1995-01-26 00:38:22 +0000 | [diff] [blame] | 1221 | if (call == NULL) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1222 | PyErr_Clear(); |
Guido van Rossum | 49b11fe | 1995-01-26 00:38:22 +0000 | [diff] [blame] | 1223 | return 0; |
| 1224 | } |
| 1225 | /* Could test recursively but don't, for fear of endless |
| 1226 | recursion if some joker sets self.__call__ = self */ |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1227 | Py_DECREF(call); |
Guido van Rossum | 49b11fe | 1995-01-26 00:38:22 +0000 | [diff] [blame] | 1228 | return 1; |
| 1229 | } |
| 1230 | return 0; |
| 1231 | } |
| 1232 | |
| 1233 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1234 | /* |
| 1235 | NoObject is usable as a non-NULL undefined value, used by the macro None. |
| 1236 | There is (and should be!) no way to create other objects of this type, |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1237 | so there is exactly one (which is indestructible, by the way). |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1238 | */ |
| 1239 | |
Guido van Rossum | 0c182a1 | 1992-03-27 17:26:13 +0000 | [diff] [blame] | 1240 | /* ARGSUSED */ |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1241 | static PyObject * |
Fred Drake | 100814d | 2000-07-09 15:48:49 +0000 | [diff] [blame] | 1242 | none_repr(PyObject *op) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1243 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1244 | return PyString_FromString("None"); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1245 | } |
| 1246 | |
Barry Warsaw | 9bf1644 | 2001-01-23 16:24:35 +0000 | [diff] [blame] | 1247 | /* ARGUSED */ |
| 1248 | static void |
| 1249 | none_dealloc(PyObject* ignore) |
| 1250 | { |
| 1251 | /* This should never get called, but we also don't want to SEGV if |
| 1252 | * we accidently decref None out of existance. |
| 1253 | */ |
| 1254 | abort(); |
| 1255 | } |
| 1256 | |
| 1257 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1258 | static PyTypeObject PyNothing_Type = { |
| 1259 | PyObject_HEAD_INIT(&PyType_Type) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1260 | 0, |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1261 | "None", |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1262 | 0, |
| 1263 | 0, |
Barry Warsaw | 9bf1644 | 2001-01-23 16:24:35 +0000 | [diff] [blame] | 1264 | (destructor)none_dealloc, /*tp_dealloc*/ /*never called*/ |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 1265 | 0, /*tp_print*/ |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1266 | 0, /*tp_getattr*/ |
| 1267 | 0, /*tp_setattr*/ |
| 1268 | 0, /*tp_compare*/ |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 1269 | (reprfunc)none_repr, /*tp_repr*/ |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1270 | 0, /*tp_as_number*/ |
| 1271 | 0, /*tp_as_sequence*/ |
| 1272 | 0, /*tp_as_mapping*/ |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 1273 | 0, /*tp_hash */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1274 | }; |
| 1275 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1276 | PyObject _Py_NoneStruct = { |
| 1277 | PyObject_HEAD_INIT(&PyNothing_Type) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1278 | }; |
| 1279 | |
Neil Schemenauer | 5ed85ec | 2001-01-04 01:48:10 +0000 | [diff] [blame] | 1280 | /* NotImplemented is an object that can be used to signal that an |
| 1281 | operation is not implemented for the given type combination. */ |
| 1282 | |
| 1283 | static PyObject * |
| 1284 | NotImplemented_repr(PyObject *op) |
| 1285 | { |
| 1286 | return PyString_FromString("NotImplemented"); |
| 1287 | } |
| 1288 | |
| 1289 | static PyTypeObject PyNotImplemented_Type = { |
| 1290 | PyObject_HEAD_INIT(&PyType_Type) |
| 1291 | 0, |
| 1292 | "NotImplemented", |
| 1293 | 0, |
| 1294 | 0, |
Barry Warsaw | 9bf1644 | 2001-01-23 16:24:35 +0000 | [diff] [blame] | 1295 | (destructor)none_dealloc, /*tp_dealloc*/ /*never called*/ |
Neil Schemenauer | 5ed85ec | 2001-01-04 01:48:10 +0000 | [diff] [blame] | 1296 | 0, /*tp_print*/ |
| 1297 | 0, /*tp_getattr*/ |
| 1298 | 0, /*tp_setattr*/ |
| 1299 | 0, /*tp_compare*/ |
| 1300 | (reprfunc)NotImplemented_repr, /*tp_repr*/ |
| 1301 | 0, /*tp_as_number*/ |
| 1302 | 0, /*tp_as_sequence*/ |
| 1303 | 0, /*tp_as_mapping*/ |
| 1304 | 0, /*tp_hash */ |
| 1305 | }; |
| 1306 | |
| 1307 | PyObject _Py_NotImplementedStruct = { |
| 1308 | PyObject_HEAD_INIT(&PyNotImplemented_Type) |
| 1309 | }; |
| 1310 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1311 | |
Guido van Rossum | 84a9032 | 1996-05-22 16:34:47 +0000 | [diff] [blame] | 1312 | #ifdef Py_TRACE_REFS |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1313 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1314 | static PyObject refchain = {&refchain, &refchain}; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1315 | |
Guido van Rossum | aacdc9d | 1996-08-12 21:32:12 +0000 | [diff] [blame] | 1316 | void |
Fred Drake | 100814d | 2000-07-09 15:48:49 +0000 | [diff] [blame] | 1317 | _Py_ResetReferences(void) |
Guido van Rossum | e09fb55 | 1997-08-05 02:04:34 +0000 | [diff] [blame] | 1318 | { |
| 1319 | refchain._ob_prev = refchain._ob_next = &refchain; |
| 1320 | _Py_RefTotal = 0; |
| 1321 | } |
| 1322 | |
| 1323 | void |
Fred Drake | 100814d | 2000-07-09 15:48:49 +0000 | [diff] [blame] | 1324 | _Py_NewReference(PyObject *op) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1325 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1326 | _Py_RefTotal++; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1327 | op->ob_refcnt = 1; |
| 1328 | op->_ob_next = refchain._ob_next; |
| 1329 | op->_ob_prev = &refchain; |
| 1330 | refchain._ob_next->_ob_prev = op; |
| 1331 | refchain._ob_next = op; |
Sjoerd Mullender | a9c3c22 | 1993-10-11 12:54:31 +0000 | [diff] [blame] | 1332 | #ifdef COUNT_ALLOCS |
| 1333 | inc_count(op->ob_type); |
| 1334 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1335 | } |
| 1336 | |
Guido van Rossum | aacdc9d | 1996-08-12 21:32:12 +0000 | [diff] [blame] | 1337 | void |
Fred Drake | 100814d | 2000-07-09 15:48:49 +0000 | [diff] [blame] | 1338 | _Py_ForgetReference(register PyObject *op) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1339 | { |
Guido van Rossum | bffd683 | 2000-01-20 22:32:56 +0000 | [diff] [blame] | 1340 | #ifdef SLOW_UNREF_CHECK |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 1341 | register PyObject *p; |
Guido van Rossum | bffd683 | 2000-01-20 22:32:56 +0000 | [diff] [blame] | 1342 | #endif |
Guido van Rossum | d7047b3 | 1995-01-02 19:07:15 +0000 | [diff] [blame] | 1343 | if (op->ob_refcnt < 0) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1344 | Py_FatalError("UNREF negative refcnt"); |
Guido van Rossum | 2e8f614 | 1992-09-03 20:32:55 +0000 | [diff] [blame] | 1345 | if (op == &refchain || |
Guido van Rossum | d7047b3 | 1995-01-02 19:07:15 +0000 | [diff] [blame] | 1346 | op->_ob_prev->_ob_next != op || op->_ob_next->_ob_prev != op) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1347 | Py_FatalError("UNREF invalid object"); |
Guido van Rossum | 2e8f614 | 1992-09-03 20:32:55 +0000 | [diff] [blame] | 1348 | #ifdef SLOW_UNREF_CHECK |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1349 | for (p = refchain._ob_next; p != &refchain; p = p->_ob_next) { |
| 1350 | if (p == op) |
| 1351 | break; |
| 1352 | } |
Guido van Rossum | d7047b3 | 1995-01-02 19:07:15 +0000 | [diff] [blame] | 1353 | if (p == &refchain) /* Not found */ |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1354 | Py_FatalError("UNREF unknown object"); |
Guido van Rossum | 2e8f614 | 1992-09-03 20:32:55 +0000 | [diff] [blame] | 1355 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1356 | op->_ob_next->_ob_prev = op->_ob_prev; |
| 1357 | op->_ob_prev->_ob_next = op->_ob_next; |
Guido van Rossum | 2e8f614 | 1992-09-03 20:32:55 +0000 | [diff] [blame] | 1358 | op->_ob_next = op->_ob_prev = NULL; |
Guido van Rossum | d8953cb | 1995-04-06 14:46:26 +0000 | [diff] [blame] | 1359 | #ifdef COUNT_ALLOCS |
| 1360 | op->ob_type->tp_free++; |
| 1361 | #endif |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1362 | } |
| 1363 | |
Guido van Rossum | aacdc9d | 1996-08-12 21:32:12 +0000 | [diff] [blame] | 1364 | void |
Fred Drake | 100814d | 2000-07-09 15:48:49 +0000 | [diff] [blame] | 1365 | _Py_Dealloc(PyObject *op) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1366 | { |
Guido van Rossum | 9776adf | 1994-09-07 14:36:45 +0000 | [diff] [blame] | 1367 | destructor dealloc = op->ob_type->tp_dealloc; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1368 | _Py_ForgetReference(op); |
Guido van Rossum | 9776adf | 1994-09-07 14:36:45 +0000 | [diff] [blame] | 1369 | (*dealloc)(op); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1370 | } |
| 1371 | |
Guido van Rossum | aacdc9d | 1996-08-12 21:32:12 +0000 | [diff] [blame] | 1372 | void |
Fred Drake | 100814d | 2000-07-09 15:48:49 +0000 | [diff] [blame] | 1373 | _Py_PrintReferences(FILE *fp) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1374 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1375 | PyObject *op; |
Guido van Rossum | e09fb55 | 1997-08-05 02:04:34 +0000 | [diff] [blame] | 1376 | fprintf(fp, "Remaining objects:\n"); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1377 | for (op = refchain._ob_next; op != &refchain; op = op->_ob_next) { |
| 1378 | fprintf(fp, "[%d] ", op->ob_refcnt); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1379 | if (PyObject_Print(op, fp, 0) != 0) |
| 1380 | PyErr_Clear(); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1381 | putc('\n', fp); |
| 1382 | } |
| 1383 | } |
| 1384 | |
Sjoerd Mullender | 6ec3c65 | 1995-08-29 09:18:14 +0000 | [diff] [blame] | 1385 | PyObject * |
Fred Drake | 100814d | 2000-07-09 15:48:49 +0000 | [diff] [blame] | 1386 | _Py_GetObjects(PyObject *self, PyObject *args) |
Sjoerd Mullender | 6ec3c65 | 1995-08-29 09:18:14 +0000 | [diff] [blame] | 1387 | { |
| 1388 | int i, n; |
| 1389 | PyObject *t = NULL; |
| 1390 | PyObject *res, *op; |
| 1391 | |
| 1392 | if (!PyArg_ParseTuple(args, "i|O", &n, &t)) |
| 1393 | return NULL; |
| 1394 | op = refchain._ob_next; |
| 1395 | res = PyList_New(0); |
| 1396 | if (res == NULL) |
| 1397 | return NULL; |
| 1398 | for (i = 0; (n == 0 || i < n) && op != &refchain; i++) { |
| 1399 | while (op == self || op == args || op == res || op == t || |
| 1400 | t != NULL && op->ob_type != (PyTypeObject *) t) { |
| 1401 | op = op->_ob_next; |
| 1402 | if (op == &refchain) |
| 1403 | return res; |
| 1404 | } |
| 1405 | if (PyList_Append(res, op) < 0) { |
| 1406 | Py_DECREF(res); |
| 1407 | return NULL; |
| 1408 | } |
| 1409 | op = op->_ob_next; |
| 1410 | } |
| 1411 | return res; |
| 1412 | } |
| 1413 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1414 | #endif |
Guido van Rossum | 97ead3f | 1996-01-12 01:24:09 +0000 | [diff] [blame] | 1415 | |
| 1416 | |
| 1417 | /* Hack to force loading of cobject.o */ |
Guido van Rossum | da9c271 | 1996-12-05 21:58:58 +0000 | [diff] [blame] | 1418 | PyTypeObject *_Py_cobject_hack = &PyCObject_Type; |
Guido van Rossum | 84a9032 | 1996-05-22 16:34:47 +0000 | [diff] [blame] | 1419 | |
| 1420 | |
| 1421 | /* Hack to force loading of abstract.o */ |
Jeremy Hylton | 6253f83 | 2000-07-12 12:56:19 +0000 | [diff] [blame] | 1422 | int (*_Py_abstract_hack)(PyObject *) = &PyObject_Size; |
Guido van Rossum | e09fb55 | 1997-08-05 02:04:34 +0000 | [diff] [blame] | 1423 | |
| 1424 | |
Andrew M. Kuchling | 1582a3a | 2000-08-16 12:27:23 +0000 | [diff] [blame] | 1425 | /* Python's malloc wrappers (see pymem.h) */ |
Guido van Rossum | e09fb55 | 1997-08-05 02:04:34 +0000 | [diff] [blame] | 1426 | |
Thomas Wouters | 334fb89 | 2000-07-25 12:56:38 +0000 | [diff] [blame] | 1427 | void * |
Fred Drake | 100814d | 2000-07-09 15:48:49 +0000 | [diff] [blame] | 1428 | PyMem_Malloc(size_t nbytes) |
Guido van Rossum | e09fb55 | 1997-08-05 02:04:34 +0000 | [diff] [blame] | 1429 | { |
| 1430 | #if _PyMem_EXTRA > 0 |
| 1431 | if (nbytes == 0) |
| 1432 | nbytes = _PyMem_EXTRA; |
| 1433 | #endif |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 1434 | return PyMem_MALLOC(nbytes); |
Guido van Rossum | e09fb55 | 1997-08-05 02:04:34 +0000 | [diff] [blame] | 1435 | } |
| 1436 | |
Thomas Wouters | 334fb89 | 2000-07-25 12:56:38 +0000 | [diff] [blame] | 1437 | void * |
| 1438 | PyMem_Realloc(void *p, size_t nbytes) |
Guido van Rossum | e09fb55 | 1997-08-05 02:04:34 +0000 | [diff] [blame] | 1439 | { |
| 1440 | #if _PyMem_EXTRA > 0 |
| 1441 | if (nbytes == 0) |
| 1442 | nbytes = _PyMem_EXTRA; |
| 1443 | #endif |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 1444 | return PyMem_REALLOC(p, nbytes); |
Guido van Rossum | e09fb55 | 1997-08-05 02:04:34 +0000 | [diff] [blame] | 1445 | } |
| 1446 | |
| 1447 | void |
Thomas Wouters | 334fb89 | 2000-07-25 12:56:38 +0000 | [diff] [blame] | 1448 | PyMem_Free(void *p) |
Guido van Rossum | e09fb55 | 1997-08-05 02:04:34 +0000 | [diff] [blame] | 1449 | { |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 1450 | PyMem_FREE(p); |
| 1451 | } |
| 1452 | |
| 1453 | |
| 1454 | /* Python's object malloc wrappers (see objimpl.h) */ |
| 1455 | |
Thomas Wouters | 334fb89 | 2000-07-25 12:56:38 +0000 | [diff] [blame] | 1456 | void * |
Fred Drake | 100814d | 2000-07-09 15:48:49 +0000 | [diff] [blame] | 1457 | PyObject_Malloc(size_t nbytes) |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 1458 | { |
| 1459 | return PyObject_MALLOC(nbytes); |
| 1460 | } |
| 1461 | |
Thomas Wouters | 334fb89 | 2000-07-25 12:56:38 +0000 | [diff] [blame] | 1462 | void * |
| 1463 | PyObject_Realloc(void *p, size_t nbytes) |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 1464 | { |
| 1465 | return PyObject_REALLOC(p, nbytes); |
| 1466 | } |
| 1467 | |
| 1468 | void |
Thomas Wouters | 334fb89 | 2000-07-25 12:56:38 +0000 | [diff] [blame] | 1469 | PyObject_Free(void *p) |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 1470 | { |
| 1471 | PyObject_FREE(p); |
Guido van Rossum | e09fb55 | 1997-08-05 02:04:34 +0000 | [diff] [blame] | 1472 | } |
Guido van Rossum | 8661036 | 1998-04-10 22:32:46 +0000 | [diff] [blame] | 1473 | |
| 1474 | |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 1475 | /* Hook to clear up weak references only once the _weakref module is |
| 1476 | imported. We use a dummy implementation to simplify the code at each |
| 1477 | call site instead of requiring a test for NULL. |
| 1478 | */ |
| 1479 | |
Fred Drake | b60654b | 2001-02-26 18:56:37 +0000 | [diff] [blame] | 1480 | static void |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 1481 | empty_clear_weak_refs(PyObject *o) |
| 1482 | { |
Fred Drake | b60654b | 2001-02-26 18:56:37 +0000 | [diff] [blame] | 1483 | return; |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 1484 | } |
| 1485 | |
Fred Drake | b60654b | 2001-02-26 18:56:37 +0000 | [diff] [blame] | 1486 | void (*PyObject_ClearWeakRefs)(PyObject *) = empty_clear_weak_refs; |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 1487 | |
| 1488 | |
| 1489 | |
Guido van Rossum | 8661036 | 1998-04-10 22:32:46 +0000 | [diff] [blame] | 1490 | /* These methods are used to control infinite recursion in repr, str, print, |
| 1491 | etc. Container objects that may recursively contain themselves, |
| 1492 | e.g. builtin dictionaries and lists, should used Py_ReprEnter() and |
| 1493 | Py_ReprLeave() to avoid infinite recursion. |
| 1494 | |
| 1495 | Py_ReprEnter() returns 0 the first time it is called for a particular |
| 1496 | object and 1 every time thereafter. It returns -1 if an exception |
| 1497 | occurred. Py_ReprLeave() has no return value. |
| 1498 | |
| 1499 | See dictobject.c and listobject.c for examples of use. |
| 1500 | */ |
| 1501 | |
| 1502 | #define KEY "Py_Repr" |
| 1503 | |
| 1504 | int |
Fred Drake | 100814d | 2000-07-09 15:48:49 +0000 | [diff] [blame] | 1505 | Py_ReprEnter(PyObject *obj) |
Guido van Rossum | 8661036 | 1998-04-10 22:32:46 +0000 | [diff] [blame] | 1506 | { |
| 1507 | PyObject *dict; |
| 1508 | PyObject *list; |
| 1509 | int i; |
| 1510 | |
| 1511 | dict = PyThreadState_GetDict(); |
| 1512 | if (dict == NULL) |
| 1513 | return -1; |
| 1514 | list = PyDict_GetItemString(dict, KEY); |
| 1515 | if (list == NULL) { |
| 1516 | list = PyList_New(0); |
| 1517 | if (list == NULL) |
| 1518 | return -1; |
| 1519 | if (PyDict_SetItemString(dict, KEY, list) < 0) |
| 1520 | return -1; |
| 1521 | Py_DECREF(list); |
| 1522 | } |
| 1523 | i = PyList_GET_SIZE(list); |
| 1524 | while (--i >= 0) { |
| 1525 | if (PyList_GET_ITEM(list, i) == obj) |
| 1526 | return 1; |
| 1527 | } |
| 1528 | PyList_Append(list, obj); |
| 1529 | return 0; |
| 1530 | } |
| 1531 | |
| 1532 | void |
Fred Drake | 100814d | 2000-07-09 15:48:49 +0000 | [diff] [blame] | 1533 | Py_ReprLeave(PyObject *obj) |
Guido van Rossum | 8661036 | 1998-04-10 22:32:46 +0000 | [diff] [blame] | 1534 | { |
| 1535 | PyObject *dict; |
| 1536 | PyObject *list; |
| 1537 | int i; |
| 1538 | |
| 1539 | dict = PyThreadState_GetDict(); |
Guido van Rossum | eb90946 | 1998-04-11 15:17:34 +0000 | [diff] [blame] | 1540 | if (dict == NULL) |
| 1541 | return; |
Guido van Rossum | 8661036 | 1998-04-10 22:32:46 +0000 | [diff] [blame] | 1542 | list = PyDict_GetItemString(dict, KEY); |
Guido van Rossum | eb90946 | 1998-04-11 15:17:34 +0000 | [diff] [blame] | 1543 | if (list == NULL || !PyList_Check(list)) |
| 1544 | return; |
Guido van Rossum | 8661036 | 1998-04-10 22:32:46 +0000 | [diff] [blame] | 1545 | i = PyList_GET_SIZE(list); |
| 1546 | /* Count backwards because we always expect obj to be list[-1] */ |
| 1547 | while (--i >= 0) { |
| 1548 | if (PyList_GET_ITEM(list, i) == obj) { |
| 1549 | PyList_SetSlice(list, i, i + 1, NULL); |
| 1550 | break; |
| 1551 | } |
| 1552 | } |
| 1553 | } |
Guido van Rossum | d724b23 | 2000-03-13 16:01:29 +0000 | [diff] [blame] | 1554 | |
| 1555 | /* |
| 1556 | trashcan |
| 1557 | CT 2k0130 |
| 1558 | non-recursively destroy nested objects |
| 1559 | |
| 1560 | CT 2k0223 |
| 1561 | everything is now done in a macro. |
| 1562 | |
| 1563 | CT 2k0305 |
| 1564 | modified to use functions, after Tim Peter's suggestion. |
| 1565 | |
| 1566 | CT 2k0309 |
| 1567 | modified to restore a possible error. |
Guido van Rossum | 13ff8eb | 2000-03-25 18:39:19 +0000 | [diff] [blame] | 1568 | |
| 1569 | CT 2k0325 |
| 1570 | added better safe than sorry check for threadstate |
Guido van Rossum | e92e610 | 2000-04-24 15:40:53 +0000 | [diff] [blame] | 1571 | |
| 1572 | CT 2k0422 |
| 1573 | complete rewrite. We now build a chain via ob_type |
| 1574 | and save the limited number of types in ob_refcnt. |
| 1575 | This is perfect since we don't need any memory. |
| 1576 | A patch for free-threading would need just a lock. |
Guido van Rossum | d724b23 | 2000-03-13 16:01:29 +0000 | [diff] [blame] | 1577 | */ |
| 1578 | |
Guido van Rossum | e92e610 | 2000-04-24 15:40:53 +0000 | [diff] [blame] | 1579 | #define Py_TRASHCAN_TUPLE 1 |
| 1580 | #define Py_TRASHCAN_LIST 2 |
| 1581 | #define Py_TRASHCAN_DICT 3 |
| 1582 | #define Py_TRASHCAN_FRAME 4 |
| 1583 | #define Py_TRASHCAN_TRACEBACK 5 |
| 1584 | /* extend here if other objects want protection */ |
| 1585 | |
Guido van Rossum | d724b23 | 2000-03-13 16:01:29 +0000 | [diff] [blame] | 1586 | int _PyTrash_delete_nesting = 0; |
Guido van Rossum | e92e610 | 2000-04-24 15:40:53 +0000 | [diff] [blame] | 1587 | |
Guido van Rossum | d724b23 | 2000-03-13 16:01:29 +0000 | [diff] [blame] | 1588 | PyObject * _PyTrash_delete_later = NULL; |
| 1589 | |
| 1590 | void |
Fred Drake | 100814d | 2000-07-09 15:48:49 +0000 | [diff] [blame] | 1591 | _PyTrash_deposit_object(PyObject *op) |
Guido van Rossum | d724b23 | 2000-03-13 16:01:29 +0000 | [diff] [blame] | 1592 | { |
Guido van Rossum | e92e610 | 2000-04-24 15:40:53 +0000 | [diff] [blame] | 1593 | int typecode; |
Guido van Rossum | 13ff8eb | 2000-03-25 18:39:19 +0000 | [diff] [blame] | 1594 | |
Guido van Rossum | e92e610 | 2000-04-24 15:40:53 +0000 | [diff] [blame] | 1595 | if (PyTuple_Check(op)) |
| 1596 | typecode = Py_TRASHCAN_TUPLE; |
| 1597 | else if (PyList_Check(op)) |
| 1598 | typecode = Py_TRASHCAN_LIST; |
| 1599 | else if (PyDict_Check(op)) |
| 1600 | typecode = Py_TRASHCAN_DICT; |
| 1601 | else if (PyFrame_Check(op)) |
| 1602 | typecode = Py_TRASHCAN_FRAME; |
| 1603 | else if (PyTraceBack_Check(op)) |
| 1604 | typecode = Py_TRASHCAN_TRACEBACK; |
Moshe Zadka | cf703f0 | 2000-08-04 15:36:13 +0000 | [diff] [blame] | 1605 | else /* We have a bug here -- those are the only types in GC */ { |
| 1606 | Py_FatalError("Type not supported in GC -- internal bug"); |
| 1607 | return; /* pacify compiler -- execution never here */ |
| 1608 | } |
Guido van Rossum | e92e610 | 2000-04-24 15:40:53 +0000 | [diff] [blame] | 1609 | op->ob_refcnt = typecode; |
Guido van Rossum | d724b23 | 2000-03-13 16:01:29 +0000 | [diff] [blame] | 1610 | |
Guido van Rossum | e92e610 | 2000-04-24 15:40:53 +0000 | [diff] [blame] | 1611 | op->ob_type = (PyTypeObject*)_PyTrash_delete_later; |
| 1612 | _PyTrash_delete_later = op; |
Guido van Rossum | d724b23 | 2000-03-13 16:01:29 +0000 | [diff] [blame] | 1613 | } |
| 1614 | |
| 1615 | void |
Fred Drake | 100814d | 2000-07-09 15:48:49 +0000 | [diff] [blame] | 1616 | _PyTrash_destroy_chain(void) |
Guido van Rossum | d724b23 | 2000-03-13 16:01:29 +0000 | [diff] [blame] | 1617 | { |
| 1618 | while (_PyTrash_delete_later) { |
| 1619 | PyObject *shredder = _PyTrash_delete_later; |
Guido van Rossum | e92e610 | 2000-04-24 15:40:53 +0000 | [diff] [blame] | 1620 | _PyTrash_delete_later = (PyObject*) shredder->ob_type; |
| 1621 | |
| 1622 | switch (shredder->ob_refcnt) { |
| 1623 | case Py_TRASHCAN_TUPLE: |
| 1624 | shredder->ob_type = &PyTuple_Type; |
| 1625 | break; |
| 1626 | case Py_TRASHCAN_LIST: |
| 1627 | shredder->ob_type = &PyList_Type; |
| 1628 | break; |
| 1629 | case Py_TRASHCAN_DICT: |
| 1630 | shredder->ob_type = &PyDict_Type; |
| 1631 | break; |
| 1632 | case Py_TRASHCAN_FRAME: |
| 1633 | shredder->ob_type = &PyFrame_Type; |
| 1634 | break; |
| 1635 | case Py_TRASHCAN_TRACEBACK: |
| 1636 | shredder->ob_type = &PyTraceBack_Type; |
| 1637 | break; |
| 1638 | } |
| 1639 | _Py_NewReference(shredder); |
| 1640 | |
Guido van Rossum | d724b23 | 2000-03-13 16:01:29 +0000 | [diff] [blame] | 1641 | ++_PyTrash_delete_nesting; |
| 1642 | Py_DECREF(shredder); |
| 1643 | --_PyTrash_delete_nesting; |
| 1644 | } |
| 1645 | } |
Neil Schemenauer | a35c688 | 2001-02-27 04:45:05 +0000 | [diff] [blame] | 1646 | |
| 1647 | #ifdef WITH_PYMALLOC |
| 1648 | #include "obmalloc.c" |
| 1649 | #endif |