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