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 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 223 | PyObject * |
Fred Drake | 100814d | 2000-07-09 15:48:49 +0000 | [diff] [blame] | 224 | PyObject_Repr(PyObject *v) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 225 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 226 | if (PyErr_CheckSignals()) |
Guido van Rossum | 9093361 | 1991-06-07 16:10:43 +0000 | [diff] [blame] | 227 | return NULL; |
Guido van Rossum | 9b00dfa | 1998-04-28 16:06:54 +0000 | [diff] [blame] | 228 | #ifdef USE_STACKCHECK |
| 229 | if (PyOS_CheckStack()) { |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 230 | PyErr_SetString(PyExc_MemoryError, "stack overflow"); |
Guido van Rossum | 9b00dfa | 1998-04-28 16:06:54 +0000 | [diff] [blame] | 231 | return NULL; |
| 232 | } |
| 233 | #endif |
Guido van Rossum | 9093361 | 1991-06-07 16:10:43 +0000 | [diff] [blame] | 234 | if (v == NULL) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 235 | return PyString_FromString("<NULL>"); |
Guido van Rossum | 9093361 | 1991-06-07 16:10:43 +0000 | [diff] [blame] | 236 | else if (v->ob_type->tp_repr == NULL) { |
| 237 | char buf[120]; |
Fred Drake | a44d353 | 2000-06-30 15:01:00 +0000 | [diff] [blame] | 238 | sprintf(buf, "<%.80s object at %p>", |
| 239 | v->ob_type->tp_name, v); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 240 | return PyString_FromString(buf); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 241 | } |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 242 | else { |
| 243 | PyObject *res; |
| 244 | res = (*v->ob_type->tp_repr)(v); |
| 245 | if (res == NULL) |
| 246 | return NULL; |
Fredrik Lundh | efecc7d | 2000-07-01 14:31:09 +0000 | [diff] [blame] | 247 | if (PyUnicode_Check(res)) { |
| 248 | PyObject* str; |
Fredrik Lundh | 2a1e060 | 2000-07-08 17:43:32 +0000 | [diff] [blame] | 249 | str = PyUnicode_AsUnicodeEscapeString(res); |
Marc-André Lemburg | 891bc65 | 2000-07-03 09:57:53 +0000 | [diff] [blame] | 250 | Py_DECREF(res); |
| 251 | if (str) |
Fredrik Lundh | efecc7d | 2000-07-01 14:31:09 +0000 | [diff] [blame] | 252 | res = str; |
Marc-André Lemburg | 891bc65 | 2000-07-03 09:57:53 +0000 | [diff] [blame] | 253 | else |
| 254 | return NULL; |
Fredrik Lundh | efecc7d | 2000-07-01 14:31:09 +0000 | [diff] [blame] | 255 | } |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 256 | if (!PyString_Check(res)) { |
| 257 | PyErr_Format(PyExc_TypeError, |
Guido van Rossum | 5db862d | 2000-04-10 12:46:51 +0000 | [diff] [blame] | 258 | "__repr__ returned non-string (type %.200s)", |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 259 | res->ob_type->tp_name); |
| 260 | Py_DECREF(res); |
| 261 | return NULL; |
| 262 | } |
| 263 | return res; |
| 264 | } |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 265 | } |
| 266 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 267 | PyObject * |
Fred Drake | 100814d | 2000-07-09 15:48:49 +0000 | [diff] [blame] | 268 | PyObject_Str(PyObject *v) |
Guido van Rossum | c600411 | 1993-11-05 10:22:19 +0000 | [diff] [blame] | 269 | { |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 270 | PyObject *res; |
| 271 | |
Guido van Rossum | c600411 | 1993-11-05 10:22:19 +0000 | [diff] [blame] | 272 | if (v == NULL) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 273 | return PyString_FromString("<NULL>"); |
| 274 | else if (PyString_Check(v)) { |
| 275 | Py_INCREF(v); |
Guido van Rossum | c600411 | 1993-11-05 10:22:19 +0000 | [diff] [blame] | 276 | return v; |
| 277 | } |
Guido van Rossum | 32b582b | 1995-01-17 16:35:13 +0000 | [diff] [blame] | 278 | else if (v->ob_type->tp_str != NULL) |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 279 | res = (*v->ob_type->tp_str)(v); |
Guido van Rossum | c600411 | 1993-11-05 10:22:19 +0000 | [diff] [blame] | 280 | else { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 281 | PyObject *func; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 282 | if (!PyInstance_Check(v) || |
| 283 | (func = PyObject_GetAttrString(v, "__str__")) == NULL) { |
| 284 | PyErr_Clear(); |
| 285 | return PyObject_Repr(v); |
Guido van Rossum | c600411 | 1993-11-05 10:22:19 +0000 | [diff] [blame] | 286 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 287 | res = PyEval_CallObject(func, (PyObject *)NULL); |
| 288 | Py_DECREF(func); |
Guido van Rossum | c600411 | 1993-11-05 10:22:19 +0000 | [diff] [blame] | 289 | } |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 290 | if (res == NULL) |
| 291 | return NULL; |
Marc-André Lemburg | 891bc65 | 2000-07-03 09:57:53 +0000 | [diff] [blame] | 292 | if (PyUnicode_Check(res)) { |
| 293 | PyObject* str; |
| 294 | str = PyUnicode_AsEncodedString(res, NULL, NULL); |
| 295 | Py_DECREF(res); |
| 296 | if (str) |
| 297 | res = str; |
| 298 | else |
| 299 | return NULL; |
| 300 | } |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 301 | if (!PyString_Check(res)) { |
| 302 | PyErr_Format(PyExc_TypeError, |
Guido van Rossum | 5db862d | 2000-04-10 12:46:51 +0000 | [diff] [blame] | 303 | "__str__ returned non-string (type %.200s)", |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 304 | res->ob_type->tp_name); |
| 305 | Py_DECREF(res); |
| 306 | return NULL; |
| 307 | } |
| 308 | return res; |
Guido van Rossum | c600411 | 1993-11-05 10:22:19 +0000 | [diff] [blame] | 309 | } |
| 310 | |
Neil Schemenauer | 5ed85ec | 2001-01-04 01:48:10 +0000 | [diff] [blame^] | 311 | #define NEW_STYLE_NUMBER(o) PyType_HasFeature((o)->ob_type, \ |
| 312 | Py_TPFLAGS_NEWSTYLENUMBER) |
| 313 | |
| 314 | static int |
| 315 | cmp_to_int(PyObject *result) |
| 316 | { |
| 317 | int c; |
| 318 | if (result == NULL) |
| 319 | return -1; |
| 320 | if (!PyInt_Check(result)) { |
| 321 | PyErr_SetString(PyExc_TypeError, |
| 322 | "comparison did not return an int"); |
| 323 | return -1; |
| 324 | } |
| 325 | c = PyInt_AS_LONG(result); |
| 326 | Py_DECREF(result); |
| 327 | return (c < 0) ? -1 : (c > 0) ? 1 : 0; |
| 328 | } |
| 329 | |
| 330 | static int |
Fred Drake | 100814d | 2000-07-09 15:48:49 +0000 | [diff] [blame] | 331 | do_cmp(PyObject *v, PyObject *w) |
Guido van Rossum | 2056684 | 1995-01-12 11:26:10 +0000 | [diff] [blame] | 332 | { |
Neil Schemenauer | 5ed85ec | 2001-01-04 01:48:10 +0000 | [diff] [blame^] | 333 | PyNumberMethods *mv, *mw; |
| 334 | PyObject *x; |
| 335 | int c; |
| 336 | |
| 337 | /* new style nb_cmp gets priority */ |
| 338 | mv = v->ob_type->tp_as_number; |
| 339 | if (mv != NULL && NEW_STYLE_NUMBER(v) && mv->nb_cmp) { |
| 340 | x = (*mv->nb_cmp)(v, w); |
| 341 | if (x != Py_NotImplemented) |
| 342 | return cmp_to_int(x); |
| 343 | Py_DECREF(x); |
| 344 | } |
| 345 | mw = w->ob_type->tp_as_number; |
| 346 | if (mw != NULL && NEW_STYLE_NUMBER(w) && mw->nb_cmp) { |
| 347 | x = (*mw->nb_cmp)(v, w); |
| 348 | if (x != Py_NotImplemented) |
| 349 | return cmp_to_int(x); |
| 350 | Py_DECREF(x); |
| 351 | } |
| 352 | /* fall back to tp_compare */ |
| 353 | if (v->ob_type == w->ob_type) { |
| 354 | if (v->ob_type->tp_compare != NULL) { |
| 355 | return (*v->ob_type->tp_compare)(v, w); |
| 356 | } |
| 357 | else { |
| 358 | Py_uintptr_t iv = (Py_uintptr_t)v; |
| 359 | Py_uintptr_t iw = (Py_uintptr_t)w; |
| 360 | return (iv < iw) ? -1 : (iv > iw) ? 1 : 0; |
| 361 | } |
| 362 | } |
| 363 | if (PyUnicode_Check(v) || PyUnicode_Check(w)) { |
| 364 | c = PyUnicode_Compare(v, w); |
| 365 | if (c == -1 && |
| 366 | PyErr_Occurred() && |
| 367 | PyErr_ExceptionMatches(PyExc_TypeError)) |
| 368 | /* TypeErrors are ignored: if Unicode coercion |
| 369 | fails due to one of the arguments not having |
| 370 | the right type, we continue as defined by the |
| 371 | coercion protocol (see above). Luckily, |
| 372 | decoding errors are reported as ValueErrors and |
| 373 | are not masked by this technique. */ |
| 374 | PyErr_Clear(); |
| 375 | else |
| 376 | return c; |
| 377 | } |
| 378 | /* fall back to coercion */ |
| 379 | if (mv && mw && (!NEW_STYLE_NUMBER(v) || !NEW_STYLE_NUMBER(w))) { |
| 380 | /* old style operand, both operations numeric, coerce */ |
| 381 | int err = PyNumber_CoerceEx(&v, &w); |
| 382 | if (err < 0) |
| 383 | return -1; |
| 384 | if (err == 0) { |
| 385 | if (v->ob_type->tp_compare) { |
| 386 | c = (*v->ob_type->tp_compare)(v, w); |
| 387 | } |
| 388 | else { |
| 389 | Py_uintptr_t iv = (Py_uintptr_t)v; |
| 390 | Py_uintptr_t iw = (Py_uintptr_t)w; |
| 391 | c = (iv < iw) ? -1 : (iv > iw) ? 1 : 0; |
| 392 | } |
| 393 | Py_DECREF(v); |
| 394 | Py_DECREF(w); |
| 395 | return c; |
| 396 | } |
| 397 | } |
| 398 | /* last resort, use type names */ |
| 399 | c = strcmp(v->ob_type->tp_name, w->ob_type->tp_name); |
| 400 | return (c < 0) ? -1: (c > 0) ? 1 : 0; |
Guido van Rossum | 2056684 | 1995-01-12 11:26:10 +0000 | [diff] [blame] | 401 | } |
| 402 | |
Jeremy Hylton | 4a3dd2d | 2000-04-14 19:13:24 +0000 | [diff] [blame] | 403 | PyObject *_PyCompareState_Key; |
| 404 | |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 405 | /* _PyCompareState_nesting is incremented before calling compare (for |
Jeremy Hylton | 4a3dd2d | 2000-04-14 19:13:24 +0000 | [diff] [blame] | 406 | some types) and decremented on exit. If the count exceeds the |
| 407 | nesting limit, enable code to detect circular data structures. |
| 408 | */ |
Jack Jansen | d49cbe1 | 2000-08-22 21:52:51 +0000 | [diff] [blame] | 409 | #ifdef macintosh |
| 410 | #define NESTING_LIMIT 60 |
| 411 | #else |
Jeremy Hylton | 4a3dd2d | 2000-04-14 19:13:24 +0000 | [diff] [blame] | 412 | #define NESTING_LIMIT 500 |
Jack Jansen | d49cbe1 | 2000-08-22 21:52:51 +0000 | [diff] [blame] | 413 | #endif |
Jeremy Hylton | 4a3dd2d | 2000-04-14 19:13:24 +0000 | [diff] [blame] | 414 | int _PyCompareState_nesting = 0; |
| 415 | |
| 416 | static PyObject* |
Fred Drake | 100814d | 2000-07-09 15:48:49 +0000 | [diff] [blame] | 417 | get_inprogress_dict(void) |
Jeremy Hylton | 4a3dd2d | 2000-04-14 19:13:24 +0000 | [diff] [blame] | 418 | { |
| 419 | PyObject *tstate_dict, *inprogress; |
| 420 | |
| 421 | tstate_dict = PyThreadState_GetDict(); |
| 422 | if (tstate_dict == NULL) { |
| 423 | PyErr_BadInternalCall(); |
| 424 | return NULL; |
| 425 | } |
| 426 | inprogress = PyDict_GetItem(tstate_dict, _PyCompareState_Key); |
| 427 | if (inprogress == NULL) { |
Jeremy Hylton | 4a3dd2d | 2000-04-14 19:13:24 +0000 | [diff] [blame] | 428 | inprogress = PyDict_New(); |
| 429 | if (inprogress == NULL) |
| 430 | return NULL; |
| 431 | if (PyDict_SetItem(tstate_dict, _PyCompareState_Key, |
| 432 | inprogress) == -1) { |
| 433 | Py_DECREF(inprogress); |
| 434 | return NULL; |
| 435 | } |
Jeremy Hylton | a251ea0 | 2000-06-09 16:20:39 +0000 | [diff] [blame] | 436 | Py_DECREF(inprogress); |
Jeremy Hylton | 4a3dd2d | 2000-04-14 19:13:24 +0000 | [diff] [blame] | 437 | } |
| 438 | return inprogress; |
| 439 | } |
| 440 | |
| 441 | static PyObject * |
Fred Drake | 100814d | 2000-07-09 15:48:49 +0000 | [diff] [blame] | 442 | make_pair(PyObject *v, PyObject *w) |
Jeremy Hylton | 4a3dd2d | 2000-04-14 19:13:24 +0000 | [diff] [blame] | 443 | { |
| 444 | PyObject *pair; |
Barry Warsaw | 9d23a4e | 2000-08-18 05:01:19 +0000 | [diff] [blame] | 445 | Py_uintptr_t iv = (Py_uintptr_t)v; |
| 446 | Py_uintptr_t iw = (Py_uintptr_t)w; |
Jeremy Hylton | 4a3dd2d | 2000-04-14 19:13:24 +0000 | [diff] [blame] | 447 | |
| 448 | pair = PyTuple_New(2); |
| 449 | if (pair == NULL) { |
| 450 | return NULL; |
| 451 | } |
Barry Warsaw | 9d23a4e | 2000-08-18 05:01:19 +0000 | [diff] [blame] | 452 | if (iv <= iw) { |
Jeremy Hylton | 4a3dd2d | 2000-04-14 19:13:24 +0000 | [diff] [blame] | 453 | PyTuple_SET_ITEM(pair, 0, PyLong_FromVoidPtr((void *)v)); |
| 454 | PyTuple_SET_ITEM(pair, 1, PyLong_FromVoidPtr((void *)w)); |
| 455 | } else { |
| 456 | PyTuple_SET_ITEM(pair, 0, PyLong_FromVoidPtr((void *)w)); |
| 457 | PyTuple_SET_ITEM(pair, 1, PyLong_FromVoidPtr((void *)v)); |
| 458 | } |
| 459 | return pair; |
| 460 | } |
| 461 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 462 | int |
Fred Drake | 100814d | 2000-07-09 15:48:49 +0000 | [diff] [blame] | 463 | PyObject_Compare(PyObject *v, PyObject *w) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 464 | { |
Guido van Rossum | cd5a5f6 | 1998-06-09 18:58:44 +0000 | [diff] [blame] | 465 | PyTypeObject *vtp, *wtp; |
Jeremy Hylton | 4a3dd2d | 2000-04-14 19:13:24 +0000 | [diff] [blame] | 466 | int result; |
| 467 | |
Jack Jansen | d49cbe1 | 2000-08-22 21:52:51 +0000 | [diff] [blame] | 468 | #if defined(USE_STACKCHECK) |
Guido van Rossum | 9c0a99e | 2000-08-30 15:53:50 +0000 | [diff] [blame] | 469 | if (PyOS_CheckStack()) { |
Jack Jansen | d49cbe1 | 2000-08-22 21:52:51 +0000 | [diff] [blame] | 470 | PyErr_SetString(PyExc_MemoryError, "Stack overflow"); |
| 471 | return -1; |
| 472 | } |
| 473 | #endif |
Guido van Rossum | c8b6df9 | 1997-05-23 00:06:51 +0000 | [diff] [blame] | 474 | if (v == NULL || w == NULL) { |
| 475 | PyErr_BadInternalCall(); |
| 476 | return -1; |
| 477 | } |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 478 | if (v == w) |
| 479 | return 0; |
Neil Schemenauer | 5ed85ec | 2001-01-04 01:48:10 +0000 | [diff] [blame^] | 480 | vtp = v->ob_type; |
| 481 | wtp = w->ob_type; |
Vladimir Marangozov | 1d3e239 | 2000-08-11 00:14:26 +0000 | [diff] [blame] | 482 | _PyCompareState_nesting++; |
Neil Schemenauer | 5ed85ec | 2001-01-04 01:48:10 +0000 | [diff] [blame^] | 483 | if (_PyCompareState_nesting > NESTING_LIMIT && |
| 484 | (vtp->tp_as_mapping |
| 485 | || PyInstance_Check(v) |
| 486 | || (vtp->tp_as_sequence && !PyString_Check(v)))) { |
| 487 | /* try to detect circular data structures */ |
Jeremy Hylton | 4a3dd2d | 2000-04-14 19:13:24 +0000 | [diff] [blame] | 488 | PyObject *inprogress, *pair; |
| 489 | |
| 490 | inprogress = get_inprogress_dict(); |
| 491 | if (inprogress == NULL) { |
Neil Schemenauer | 5ed85ec | 2001-01-04 01:48:10 +0000 | [diff] [blame^] | 492 | result = -1; |
| 493 | goto exit_cmp; |
Jeremy Hylton | 4a3dd2d | 2000-04-14 19:13:24 +0000 | [diff] [blame] | 494 | } |
| 495 | pair = make_pair(v, w); |
| 496 | if (PyDict_GetItem(inprogress, pair)) { |
| 497 | /* already comparing these objects. assume |
| 498 | they're equal until shown otherwise */ |
Jeremy Hylton | 4a3dd2d | 2000-04-14 19:13:24 +0000 | [diff] [blame] | 499 | Py_DECREF(pair); |
Neil Schemenauer | 5ed85ec | 2001-01-04 01:48:10 +0000 | [diff] [blame^] | 500 | result = 0; |
| 501 | goto exit_cmp; |
Jeremy Hylton | 4a3dd2d | 2000-04-14 19:13:24 +0000 | [diff] [blame] | 502 | } |
| 503 | if (PyDict_SetItem(inprogress, pair, pair) == -1) { |
Neil Schemenauer | 5ed85ec | 2001-01-04 01:48:10 +0000 | [diff] [blame^] | 504 | result = -1; |
| 505 | goto exit_cmp; |
Jeremy Hylton | 4a3dd2d | 2000-04-14 19:13:24 +0000 | [diff] [blame] | 506 | } |
Neil Schemenauer | 5ed85ec | 2001-01-04 01:48:10 +0000 | [diff] [blame^] | 507 | result = do_cmp(v, w); |
| 508 | /* XXX DelItem shouldn't fail */ |
| 509 | PyDict_DelItem(inprogress, pair); |
Jeremy Hylton | 4a3dd2d | 2000-04-14 19:13:24 +0000 | [diff] [blame] | 510 | Py_DECREF(pair); |
Jeremy Hylton | 4a3dd2d | 2000-04-14 19:13:24 +0000 | [diff] [blame] | 511 | } |
Neil Schemenauer | 5ed85ec | 2001-01-04 01:48:10 +0000 | [diff] [blame^] | 512 | else { |
| 513 | result = do_cmp(v, w); |
| 514 | } |
| 515 | exit_cmp: |
Vladimir Marangozov | 1d3e239 | 2000-08-11 00:14:26 +0000 | [diff] [blame] | 516 | _PyCompareState_nesting--; |
Jeremy Hylton | 4a3dd2d | 2000-04-14 19:13:24 +0000 | [diff] [blame] | 517 | return result; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 518 | } |
| 519 | |
Fred Drake | 13634cf | 2000-06-29 19:17:04 +0000 | [diff] [blame] | 520 | |
| 521 | /* Set of hash utility functions to help maintaining the invariant that |
| 522 | iff a==b then hash(a)==hash(b) |
| 523 | |
| 524 | All the utility functions (_Py_Hash*()) return "-1" to signify an error. |
| 525 | */ |
| 526 | |
| 527 | long |
Fred Drake | 100814d | 2000-07-09 15:48:49 +0000 | [diff] [blame] | 528 | _Py_HashDouble(double v) |
Fred Drake | 13634cf | 2000-06-29 19:17:04 +0000 | [diff] [blame] | 529 | { |
Tim Peters | 39dce29 | 2000-08-15 03:34:48 +0000 | [diff] [blame] | 530 | double intpart, fractpart; |
| 531 | int expo; |
| 532 | long hipart; |
| 533 | long x; /* the final hash value */ |
| 534 | /* This is designed so that Python numbers of different types |
| 535 | * that compare equal hash to the same value; otherwise comparisons |
| 536 | * of mapping keys will turn out weird. |
| 537 | */ |
| 538 | |
| 539 | #ifdef MPW /* MPW C modf expects pointer to extended as second argument */ |
| 540 | { |
| 541 | extended e; |
| 542 | fractpart = modf(v, &e); |
| 543 | intpart = e; |
| 544 | } |
| 545 | #else |
| 546 | fractpart = modf(v, &intpart); |
| 547 | #endif |
| 548 | if (fractpart == 0.0) { |
| 549 | /* This must return the same hash as an equal int or long. */ |
| 550 | if (intpart > LONG_MAX || -intpart > LONG_MAX) { |
| 551 | /* Convert to long and use its hash. */ |
| 552 | PyObject *plong; /* converted to Python long */ |
| 553 | if (Py_IS_INFINITY(intpart)) |
| 554 | /* can't convert to long int -- arbitrary */ |
| 555 | v = v < 0 ? -271828.0 : 314159.0; |
| 556 | plong = PyLong_FromDouble(v); |
| 557 | if (plong == NULL) |
| 558 | return -1; |
| 559 | x = PyObject_Hash(plong); |
| 560 | Py_DECREF(plong); |
| 561 | return x; |
| 562 | } |
| 563 | /* Fits in a C long == a Python int, so is its own hash. */ |
| 564 | x = (long)intpart; |
| 565 | if (x == -1) |
| 566 | x = -2; |
| 567 | return x; |
| 568 | } |
| 569 | /* The fractional part is non-zero, so we don't have to worry about |
| 570 | * making this match the hash of some other type. |
| 571 | * Use frexp to get at the bits in the double. |
Fred Drake | 13634cf | 2000-06-29 19:17:04 +0000 | [diff] [blame] | 572 | * Since the VAX D double format has 56 mantissa bits, which is the |
| 573 | * most of any double format in use, each of these parts may have as |
| 574 | * many as (but no more than) 56 significant bits. |
Tim Peters | 39dce29 | 2000-08-15 03:34:48 +0000 | [diff] [blame] | 575 | * So, assuming sizeof(long) >= 4, each part can be broken into two |
| 576 | * longs; frexp and multiplication are used to do that. |
| 577 | * Also, since the Cray double format has 15 exponent bits, which is |
| 578 | * the most of any double format in use, shifting the exponent field |
| 579 | * 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] | 580 | */ |
Tim Peters | 39dce29 | 2000-08-15 03:34:48 +0000 | [diff] [blame] | 581 | v = frexp(v, &expo); |
| 582 | v *= 2147483648.0; /* 2**31 */ |
| 583 | hipart = (long)v; /* take the top 32 bits */ |
| 584 | v = (v - (double)hipart) * 2147483648.0; /* get the next 32 bits */ |
| 585 | x = hipart + (long)v + (expo << 15); |
| 586 | if (x == -1) |
| 587 | x = -2; |
| 588 | return x; |
Fred Drake | 13634cf | 2000-06-29 19:17:04 +0000 | [diff] [blame] | 589 | } |
| 590 | |
| 591 | long |
Fred Drake | 100814d | 2000-07-09 15:48:49 +0000 | [diff] [blame] | 592 | _Py_HashPointer(void *p) |
Fred Drake | 13634cf | 2000-06-29 19:17:04 +0000 | [diff] [blame] | 593 | { |
| 594 | #if SIZEOF_LONG >= SIZEOF_VOID_P |
| 595 | return (long)p; |
| 596 | #else |
| 597 | /* convert to a Python long and hash that */ |
| 598 | PyObject* longobj; |
| 599 | long x; |
| 600 | |
| 601 | if ((longobj = PyLong_FromVoidPtr(p)) == NULL) { |
| 602 | x = -1; |
| 603 | goto finally; |
| 604 | } |
| 605 | x = PyObject_Hash(longobj); |
| 606 | |
| 607 | finally: |
| 608 | Py_XDECREF(longobj); |
| 609 | return x; |
| 610 | #endif |
| 611 | } |
| 612 | |
| 613 | |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 614 | long |
Fred Drake | 100814d | 2000-07-09 15:48:49 +0000 | [diff] [blame] | 615 | PyObject_Hash(PyObject *v) |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 616 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 617 | PyTypeObject *tp = v->ob_type; |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 618 | if (tp->tp_hash != NULL) |
| 619 | return (*tp->tp_hash)(v); |
Fred Drake | 13634cf | 2000-06-29 19:17:04 +0000 | [diff] [blame] | 620 | if (tp->tp_compare == NULL) { |
| 621 | return _Py_HashPointer(v); /* Use address as hash value */ |
| 622 | } |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 623 | /* 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] | 624 | PyErr_SetString(PyExc_TypeError, "unhashable type"); |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 625 | return -1; |
| 626 | } |
| 627 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 628 | PyObject * |
Fred Drake | 100814d | 2000-07-09 15:48:49 +0000 | [diff] [blame] | 629 | PyObject_GetAttrString(PyObject *v, char *name) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 630 | { |
Guido van Rossum | d8eb1b3 | 1996-08-09 20:52:03 +0000 | [diff] [blame] | 631 | if (v->ob_type->tp_getattro != NULL) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 632 | PyObject *w, *res; |
Guido van Rossum | c6d0670 | 1997-01-18 07:57:16 +0000 | [diff] [blame] | 633 | w = PyString_InternFromString(name); |
Guido van Rossum | d8eb1b3 | 1996-08-09 20:52:03 +0000 | [diff] [blame] | 634 | if (w == NULL) |
| 635 | return NULL; |
| 636 | res = (*v->ob_type->tp_getattro)(v, w); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 637 | Py_XDECREF(w); |
Guido van Rossum | d8eb1b3 | 1996-08-09 20:52:03 +0000 | [diff] [blame] | 638 | return res; |
| 639 | } |
| 640 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 641 | if (v->ob_type->tp_getattr == NULL) { |
Guido van Rossum | db93516 | 1998-01-19 22:16:36 +0000 | [diff] [blame] | 642 | PyErr_Format(PyExc_AttributeError, |
| 643 | "'%.50s' object has no attribute '%.400s'", |
| 644 | v->ob_type->tp_name, |
| 645 | name); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 646 | return NULL; |
| 647 | } |
| 648 | else { |
| 649 | return (*v->ob_type->tp_getattr)(v, name); |
| 650 | } |
| 651 | } |
| 652 | |
| 653 | int |
Fred Drake | 100814d | 2000-07-09 15:48:49 +0000 | [diff] [blame] | 654 | PyObject_HasAttrString(PyObject *v, char *name) |
Guido van Rossum | ed18fdc | 1993-07-11 19:55:34 +0000 | [diff] [blame] | 655 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 656 | PyObject *res = PyObject_GetAttrString(v, name); |
Guido van Rossum | ed18fdc | 1993-07-11 19:55:34 +0000 | [diff] [blame] | 657 | if (res != NULL) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 658 | Py_DECREF(res); |
Guido van Rossum | ed18fdc | 1993-07-11 19:55:34 +0000 | [diff] [blame] | 659 | return 1; |
| 660 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 661 | PyErr_Clear(); |
Guido van Rossum | ed18fdc | 1993-07-11 19:55:34 +0000 | [diff] [blame] | 662 | return 0; |
| 663 | } |
| 664 | |
| 665 | int |
Fred Drake | 100814d | 2000-07-09 15:48:49 +0000 | [diff] [blame] | 666 | PyObject_SetAttrString(PyObject *v, char *name, PyObject *w) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 667 | { |
Guido van Rossum | d8eb1b3 | 1996-08-09 20:52:03 +0000 | [diff] [blame] | 668 | if (v->ob_type->tp_setattro != NULL) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 669 | PyObject *s; |
Guido van Rossum | d8eb1b3 | 1996-08-09 20:52:03 +0000 | [diff] [blame] | 670 | int res; |
Guido van Rossum | c6d0670 | 1997-01-18 07:57:16 +0000 | [diff] [blame] | 671 | s = PyString_InternFromString(name); |
Guido van Rossum | d8eb1b3 | 1996-08-09 20:52:03 +0000 | [diff] [blame] | 672 | if (s == NULL) |
Guido van Rossum | b7fc304 | 1996-09-11 22:51:25 +0000 | [diff] [blame] | 673 | return -1; |
Guido van Rossum | d8eb1b3 | 1996-08-09 20:52:03 +0000 | [diff] [blame] | 674 | res = (*v->ob_type->tp_setattro)(v, s, w); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 675 | Py_XDECREF(s); |
Guido van Rossum | d8eb1b3 | 1996-08-09 20:52:03 +0000 | [diff] [blame] | 676 | return res; |
| 677 | } |
| 678 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 679 | if (v->ob_type->tp_setattr == NULL) { |
| 680 | if (v->ob_type->tp_getattr == NULL) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 681 | PyErr_SetString(PyExc_TypeError, |
Guido van Rossum | 3ea7412 | 1991-12-24 13:28:03 +0000 | [diff] [blame] | 682 | "attribute-less object (assign or del)"); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 683 | else |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 684 | PyErr_SetString(PyExc_TypeError, |
Guido van Rossum | 3ea7412 | 1991-12-24 13:28:03 +0000 | [diff] [blame] | 685 | "object has read-only attributes"); |
Guido van Rossum | 73531a3 | 1990-12-20 23:12:40 +0000 | [diff] [blame] | 686 | return -1; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 687 | } |
| 688 | else { |
| 689 | return (*v->ob_type->tp_setattr)(v, name, w); |
| 690 | } |
| 691 | } |
| 692 | |
Marc-André Lemburg | e44e507 | 2000-09-18 16:20:57 +0000 | [diff] [blame] | 693 | /* Internal API needed by PyObject_GetAttr(): */ |
| 694 | extern |
| 695 | PyObject *_PyUnicode_AsDefaultEncodedString(PyObject *unicode, |
| 696 | const char *errors); |
| 697 | |
Guido van Rossum | 98ff96a | 1997-05-20 18:34:44 +0000 | [diff] [blame] | 698 | PyObject * |
Fred Drake | 100814d | 2000-07-09 15:48:49 +0000 | [diff] [blame] | 699 | PyObject_GetAttr(PyObject *v, PyObject *name) |
Guido van Rossum | 98ff96a | 1997-05-20 18:34:44 +0000 | [diff] [blame] | 700 | { |
Marc-André Lemburg | e44e507 | 2000-09-18 16:20:57 +0000 | [diff] [blame] | 701 | /* The Unicode to string conversion is done here because the |
| 702 | existing tp_getattro slots expect a string object as name |
| 703 | and we wouldn't want to break those. */ |
| 704 | if (PyUnicode_Check(name)) { |
| 705 | name = _PyUnicode_AsDefaultEncodedString(name, NULL); |
| 706 | if (name == NULL) |
| 707 | return NULL; |
| 708 | } |
Jeremy Hylton | 99a8f90 | 2000-06-23 14:36:32 +0000 | [diff] [blame] | 709 | |
| 710 | if (!PyString_Check(name)) { |
| 711 | PyErr_SetString(PyExc_TypeError, |
| 712 | "attribute name must be string"); |
| 713 | return NULL; |
| 714 | } |
Marc-André Lemburg | e44e507 | 2000-09-18 16:20:57 +0000 | [diff] [blame] | 715 | if (v->ob_type->tp_getattro != NULL) |
| 716 | return (*v->ob_type->tp_getattro)(v, name); |
| 717 | else |
Jeremy Hylton | 99a8f90 | 2000-06-23 14:36:32 +0000 | [diff] [blame] | 718 | return PyObject_GetAttrString(v, PyString_AS_STRING(name)); |
Guido van Rossum | 98ff96a | 1997-05-20 18:34:44 +0000 | [diff] [blame] | 719 | } |
| 720 | |
| 721 | int |
Fred Drake | 100814d | 2000-07-09 15:48:49 +0000 | [diff] [blame] | 722 | PyObject_HasAttr(PyObject *v, PyObject *name) |
Guido van Rossum | 98ff96a | 1997-05-20 18:34:44 +0000 | [diff] [blame] | 723 | { |
| 724 | PyObject *res = PyObject_GetAttr(v, name); |
| 725 | if (res != NULL) { |
| 726 | Py_DECREF(res); |
| 727 | return 1; |
| 728 | } |
| 729 | PyErr_Clear(); |
| 730 | return 0; |
| 731 | } |
| 732 | |
| 733 | int |
Fred Drake | 100814d | 2000-07-09 15:48:49 +0000 | [diff] [blame] | 734 | PyObject_SetAttr(PyObject *v, PyObject *name, PyObject *value) |
Guido van Rossum | 98ff96a | 1997-05-20 18:34:44 +0000 | [diff] [blame] | 735 | { |
| 736 | int err; |
Marc-André Lemburg | e44e507 | 2000-09-18 16:20:57 +0000 | [diff] [blame] | 737 | |
| 738 | /* The Unicode to string conversion is done here because the |
| 739 | existing tp_setattro slots expect a string object as name |
| 740 | and we wouldn't want to break those. */ |
| 741 | if (PyUnicode_Check(name)) { |
| 742 | name = PyUnicode_AsEncodedString(name, NULL, NULL); |
| 743 | if (name == NULL) |
| 744 | return -1; |
Jeremy Hylton | 99a8f90 | 2000-06-23 14:36:32 +0000 | [diff] [blame] | 745 | } |
Marc-André Lemburg | e44e507 | 2000-09-18 16:20:57 +0000 | [diff] [blame] | 746 | else |
| 747 | Py_INCREF(name); |
| 748 | |
| 749 | if (!PyString_Check(name)){ |
Jeremy Hylton | 99a8f90 | 2000-06-23 14:36:32 +0000 | [diff] [blame] | 750 | PyErr_SetString(PyExc_TypeError, |
| 751 | "attribute name must be string"); |
| 752 | err = -1; |
| 753 | } |
Marc-André Lemburg | e44e507 | 2000-09-18 16:20:57 +0000 | [diff] [blame] | 754 | else { |
| 755 | PyString_InternInPlace(&name); |
| 756 | if (v->ob_type->tp_setattro != NULL) |
| 757 | err = (*v->ob_type->tp_setattro)(v, name, value); |
| 758 | else |
| 759 | err = PyObject_SetAttrString(v, |
| 760 | PyString_AS_STRING(name), value); |
| 761 | } |
| 762 | |
Guido van Rossum | 98ff96a | 1997-05-20 18:34:44 +0000 | [diff] [blame] | 763 | Py_DECREF(name); |
| 764 | return err; |
| 765 | } |
| 766 | |
Guido van Rossum | 6ac258d | 1993-05-12 08:24:20 +0000 | [diff] [blame] | 767 | /* Test a value used as condition, e.g., in a for or if statement. |
| 768 | Return -1 if an error occurred */ |
| 769 | |
| 770 | int |
Fred Drake | 100814d | 2000-07-09 15:48:49 +0000 | [diff] [blame] | 771 | PyObject_IsTrue(PyObject *v) |
Guido van Rossum | 6ac258d | 1993-05-12 08:24:20 +0000 | [diff] [blame] | 772 | { |
| 773 | int res; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 774 | if (v == Py_None) |
Guido van Rossum | 6ac258d | 1993-05-12 08:24:20 +0000 | [diff] [blame] | 775 | res = 0; |
Guido van Rossum | 1c4f458 | 1998-05-22 00:53:24 +0000 | [diff] [blame] | 776 | else if (v->ob_type->tp_as_number != NULL && |
| 777 | v->ob_type->tp_as_number->nb_nonzero != NULL) |
Guido van Rossum | 6ac258d | 1993-05-12 08:24:20 +0000 | [diff] [blame] | 778 | res = (*v->ob_type->tp_as_number->nb_nonzero)(v); |
Guido van Rossum | 1c4f458 | 1998-05-22 00:53:24 +0000 | [diff] [blame] | 779 | else if (v->ob_type->tp_as_mapping != NULL && |
| 780 | v->ob_type->tp_as_mapping->mp_length != NULL) |
Guido van Rossum | 6ac258d | 1993-05-12 08:24:20 +0000 | [diff] [blame] | 781 | res = (*v->ob_type->tp_as_mapping->mp_length)(v); |
Guido van Rossum | 1c4f458 | 1998-05-22 00:53:24 +0000 | [diff] [blame] | 782 | else if (v->ob_type->tp_as_sequence != NULL && |
| 783 | v->ob_type->tp_as_sequence->sq_length != NULL) |
Guido van Rossum | 6ac258d | 1993-05-12 08:24:20 +0000 | [diff] [blame] | 784 | res = (*v->ob_type->tp_as_sequence->sq_length)(v); |
| 785 | else |
| 786 | res = 1; |
| 787 | if (res > 0) |
| 788 | res = 1; |
| 789 | return res; |
| 790 | } |
| 791 | |
Guido van Rossum | c3d3f96 | 1998-04-09 17:53:59 +0000 | [diff] [blame] | 792 | /* equivalent of 'not v' |
| 793 | Return -1 if an error occurred */ |
| 794 | |
| 795 | int |
Fred Drake | 100814d | 2000-07-09 15:48:49 +0000 | [diff] [blame] | 796 | PyObject_Not(PyObject *v) |
Guido van Rossum | c3d3f96 | 1998-04-09 17:53:59 +0000 | [diff] [blame] | 797 | { |
| 798 | int res; |
| 799 | res = PyObject_IsTrue(v); |
| 800 | if (res < 0) |
| 801 | return res; |
| 802 | return res == 0; |
| 803 | } |
| 804 | |
Guido van Rossum | 5524a59 | 1995-01-10 15:26:20 +0000 | [diff] [blame] | 805 | /* Coerce two numeric types to the "larger" one. |
| 806 | Increment the reference count on each argument. |
| 807 | Return -1 and raise an exception if no coercion is possible |
| 808 | (and then no reference count is incremented). |
| 809 | */ |
| 810 | |
| 811 | int |
Fred Drake | 100814d | 2000-07-09 15:48:49 +0000 | [diff] [blame] | 812 | PyNumber_CoerceEx(PyObject **pv, PyObject **pw) |
Guido van Rossum | 5524a59 | 1995-01-10 15:26:20 +0000 | [diff] [blame] | 813 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 814 | register PyObject *v = *pv; |
| 815 | register PyObject *w = *pw; |
Guido van Rossum | 5524a59 | 1995-01-10 15:26:20 +0000 | [diff] [blame] | 816 | int res; |
| 817 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 818 | if (v->ob_type == w->ob_type && !PyInstance_Check(v)) { |
| 819 | Py_INCREF(v); |
| 820 | Py_INCREF(w); |
Guido van Rossum | 5524a59 | 1995-01-10 15:26:20 +0000 | [diff] [blame] | 821 | return 0; |
| 822 | } |
| 823 | if (v->ob_type->tp_as_number && v->ob_type->tp_as_number->nb_coerce) { |
| 824 | res = (*v->ob_type->tp_as_number->nb_coerce)(pv, pw); |
| 825 | if (res <= 0) |
| 826 | return res; |
| 827 | } |
| 828 | if (w->ob_type->tp_as_number && w->ob_type->tp_as_number->nb_coerce) { |
| 829 | res = (*w->ob_type->tp_as_number->nb_coerce)(pw, pv); |
| 830 | if (res <= 0) |
| 831 | return res; |
| 832 | } |
Guido van Rossum | 242c642 | 1997-11-19 16:03:17 +0000 | [diff] [blame] | 833 | return 1; |
| 834 | } |
| 835 | |
| 836 | int |
Fred Drake | 100814d | 2000-07-09 15:48:49 +0000 | [diff] [blame] | 837 | PyNumber_Coerce(PyObject **pv, PyObject **pw) |
Guido van Rossum | 242c642 | 1997-11-19 16:03:17 +0000 | [diff] [blame] | 838 | { |
| 839 | int err = PyNumber_CoerceEx(pv, pw); |
| 840 | if (err <= 0) |
| 841 | return err; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 842 | PyErr_SetString(PyExc_TypeError, "number coercion failed"); |
Guido van Rossum | 5524a59 | 1995-01-10 15:26:20 +0000 | [diff] [blame] | 843 | return -1; |
| 844 | } |
| 845 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 846 | |
Guido van Rossum | 49b11fe | 1995-01-26 00:38:22 +0000 | [diff] [blame] | 847 | /* Test whether an object can be called */ |
| 848 | |
| 849 | int |
Fred Drake | 100814d | 2000-07-09 15:48:49 +0000 | [diff] [blame] | 850 | PyCallable_Check(PyObject *x) |
Guido van Rossum | 49b11fe | 1995-01-26 00:38:22 +0000 | [diff] [blame] | 851 | { |
| 852 | if (x == NULL) |
| 853 | return 0; |
| 854 | if (x->ob_type->tp_call != NULL || |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 855 | PyFunction_Check(x) || |
| 856 | PyMethod_Check(x) || |
| 857 | PyCFunction_Check(x) || |
| 858 | PyClass_Check(x)) |
Guido van Rossum | 49b11fe | 1995-01-26 00:38:22 +0000 | [diff] [blame] | 859 | return 1; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 860 | if (PyInstance_Check(x)) { |
| 861 | PyObject *call = PyObject_GetAttrString(x, "__call__"); |
Guido van Rossum | 49b11fe | 1995-01-26 00:38:22 +0000 | [diff] [blame] | 862 | if (call == NULL) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 863 | PyErr_Clear(); |
Guido van Rossum | 49b11fe | 1995-01-26 00:38:22 +0000 | [diff] [blame] | 864 | return 0; |
| 865 | } |
| 866 | /* Could test recursively but don't, for fear of endless |
| 867 | recursion if some joker sets self.__call__ = self */ |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 868 | Py_DECREF(call); |
Guido van Rossum | 49b11fe | 1995-01-26 00:38:22 +0000 | [diff] [blame] | 869 | return 1; |
| 870 | } |
| 871 | return 0; |
| 872 | } |
| 873 | |
| 874 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 875 | /* |
| 876 | NoObject is usable as a non-NULL undefined value, used by the macro None. |
| 877 | 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] | 878 | so there is exactly one (which is indestructible, by the way). |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 879 | */ |
| 880 | |
Guido van Rossum | 0c182a1 | 1992-03-27 17:26:13 +0000 | [diff] [blame] | 881 | /* ARGSUSED */ |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 882 | static PyObject * |
Fred Drake | 100814d | 2000-07-09 15:48:49 +0000 | [diff] [blame] | 883 | none_repr(PyObject *op) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 884 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 885 | return PyString_FromString("None"); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 886 | } |
| 887 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 888 | static PyTypeObject PyNothing_Type = { |
| 889 | PyObject_HEAD_INIT(&PyType_Type) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 890 | 0, |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 891 | "None", |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 892 | 0, |
| 893 | 0, |
| 894 | 0, /*tp_dealloc*/ /*never called*/ |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 895 | 0, /*tp_print*/ |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 896 | 0, /*tp_getattr*/ |
| 897 | 0, /*tp_setattr*/ |
| 898 | 0, /*tp_compare*/ |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 899 | (reprfunc)none_repr, /*tp_repr*/ |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 900 | 0, /*tp_as_number*/ |
| 901 | 0, /*tp_as_sequence*/ |
| 902 | 0, /*tp_as_mapping*/ |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 903 | 0, /*tp_hash */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 904 | }; |
| 905 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 906 | PyObject _Py_NoneStruct = { |
| 907 | PyObject_HEAD_INIT(&PyNothing_Type) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 908 | }; |
| 909 | |
Neil Schemenauer | 5ed85ec | 2001-01-04 01:48:10 +0000 | [diff] [blame^] | 910 | /* NotImplemented is an object that can be used to signal that an |
| 911 | operation is not implemented for the given type combination. */ |
| 912 | |
| 913 | static PyObject * |
| 914 | NotImplemented_repr(PyObject *op) |
| 915 | { |
| 916 | return PyString_FromString("NotImplemented"); |
| 917 | } |
| 918 | |
| 919 | static PyTypeObject PyNotImplemented_Type = { |
| 920 | PyObject_HEAD_INIT(&PyType_Type) |
| 921 | 0, |
| 922 | "NotImplemented", |
| 923 | 0, |
| 924 | 0, |
| 925 | 0, /*tp_dealloc*/ /*never called*/ |
| 926 | 0, /*tp_print*/ |
| 927 | 0, /*tp_getattr*/ |
| 928 | 0, /*tp_setattr*/ |
| 929 | 0, /*tp_compare*/ |
| 930 | (reprfunc)NotImplemented_repr, /*tp_repr*/ |
| 931 | 0, /*tp_as_number*/ |
| 932 | 0, /*tp_as_sequence*/ |
| 933 | 0, /*tp_as_mapping*/ |
| 934 | 0, /*tp_hash */ |
| 935 | }; |
| 936 | |
| 937 | PyObject _Py_NotImplementedStruct = { |
| 938 | PyObject_HEAD_INIT(&PyNotImplemented_Type) |
| 939 | }; |
| 940 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 941 | |
Guido van Rossum | 84a9032 | 1996-05-22 16:34:47 +0000 | [diff] [blame] | 942 | #ifdef Py_TRACE_REFS |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 943 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 944 | static PyObject refchain = {&refchain, &refchain}; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 945 | |
Guido van Rossum | aacdc9d | 1996-08-12 21:32:12 +0000 | [diff] [blame] | 946 | void |
Fred Drake | 100814d | 2000-07-09 15:48:49 +0000 | [diff] [blame] | 947 | _Py_ResetReferences(void) |
Guido van Rossum | e09fb55 | 1997-08-05 02:04:34 +0000 | [diff] [blame] | 948 | { |
| 949 | refchain._ob_prev = refchain._ob_next = &refchain; |
| 950 | _Py_RefTotal = 0; |
| 951 | } |
| 952 | |
| 953 | void |
Fred Drake | 100814d | 2000-07-09 15:48:49 +0000 | [diff] [blame] | 954 | _Py_NewReference(PyObject *op) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 955 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 956 | _Py_RefTotal++; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 957 | op->ob_refcnt = 1; |
| 958 | op->_ob_next = refchain._ob_next; |
| 959 | op->_ob_prev = &refchain; |
| 960 | refchain._ob_next->_ob_prev = op; |
| 961 | refchain._ob_next = op; |
Sjoerd Mullender | a9c3c22 | 1993-10-11 12:54:31 +0000 | [diff] [blame] | 962 | #ifdef COUNT_ALLOCS |
| 963 | inc_count(op->ob_type); |
| 964 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 965 | } |
| 966 | |
Guido van Rossum | aacdc9d | 1996-08-12 21:32:12 +0000 | [diff] [blame] | 967 | void |
Fred Drake | 100814d | 2000-07-09 15:48:49 +0000 | [diff] [blame] | 968 | _Py_ForgetReference(register PyObject *op) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 969 | { |
Guido van Rossum | bffd683 | 2000-01-20 22:32:56 +0000 | [diff] [blame] | 970 | #ifdef SLOW_UNREF_CHECK |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 971 | register PyObject *p; |
Guido van Rossum | bffd683 | 2000-01-20 22:32:56 +0000 | [diff] [blame] | 972 | #endif |
Guido van Rossum | d7047b3 | 1995-01-02 19:07:15 +0000 | [diff] [blame] | 973 | if (op->ob_refcnt < 0) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 974 | Py_FatalError("UNREF negative refcnt"); |
Guido van Rossum | 2e8f614 | 1992-09-03 20:32:55 +0000 | [diff] [blame] | 975 | if (op == &refchain || |
Guido van Rossum | d7047b3 | 1995-01-02 19:07:15 +0000 | [diff] [blame] | 976 | 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] | 977 | Py_FatalError("UNREF invalid object"); |
Guido van Rossum | 2e8f614 | 1992-09-03 20:32:55 +0000 | [diff] [blame] | 978 | #ifdef SLOW_UNREF_CHECK |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 979 | for (p = refchain._ob_next; p != &refchain; p = p->_ob_next) { |
| 980 | if (p == op) |
| 981 | break; |
| 982 | } |
Guido van Rossum | d7047b3 | 1995-01-02 19:07:15 +0000 | [diff] [blame] | 983 | if (p == &refchain) /* Not found */ |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 984 | Py_FatalError("UNREF unknown object"); |
Guido van Rossum | 2e8f614 | 1992-09-03 20:32:55 +0000 | [diff] [blame] | 985 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 986 | op->_ob_next->_ob_prev = op->_ob_prev; |
| 987 | op->_ob_prev->_ob_next = op->_ob_next; |
Guido van Rossum | 2e8f614 | 1992-09-03 20:32:55 +0000 | [diff] [blame] | 988 | op->_ob_next = op->_ob_prev = NULL; |
Guido van Rossum | d8953cb | 1995-04-06 14:46:26 +0000 | [diff] [blame] | 989 | #ifdef COUNT_ALLOCS |
| 990 | op->ob_type->tp_free++; |
| 991 | #endif |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 992 | } |
| 993 | |
Guido van Rossum | aacdc9d | 1996-08-12 21:32:12 +0000 | [diff] [blame] | 994 | void |
Fred Drake | 100814d | 2000-07-09 15:48:49 +0000 | [diff] [blame] | 995 | _Py_Dealloc(PyObject *op) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 996 | { |
Guido van Rossum | 9776adf | 1994-09-07 14:36:45 +0000 | [diff] [blame] | 997 | destructor dealloc = op->ob_type->tp_dealloc; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 998 | _Py_ForgetReference(op); |
Guido van Rossum | 9776adf | 1994-09-07 14:36:45 +0000 | [diff] [blame] | 999 | (*dealloc)(op); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1000 | } |
| 1001 | |
Guido van Rossum | aacdc9d | 1996-08-12 21:32:12 +0000 | [diff] [blame] | 1002 | void |
Fred Drake | 100814d | 2000-07-09 15:48:49 +0000 | [diff] [blame] | 1003 | _Py_PrintReferences(FILE *fp) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1004 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1005 | PyObject *op; |
Guido van Rossum | e09fb55 | 1997-08-05 02:04:34 +0000 | [diff] [blame] | 1006 | fprintf(fp, "Remaining objects:\n"); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1007 | for (op = refchain._ob_next; op != &refchain; op = op->_ob_next) { |
| 1008 | fprintf(fp, "[%d] ", op->ob_refcnt); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1009 | if (PyObject_Print(op, fp, 0) != 0) |
| 1010 | PyErr_Clear(); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1011 | putc('\n', fp); |
| 1012 | } |
| 1013 | } |
| 1014 | |
Sjoerd Mullender | 6ec3c65 | 1995-08-29 09:18:14 +0000 | [diff] [blame] | 1015 | PyObject * |
Fred Drake | 100814d | 2000-07-09 15:48:49 +0000 | [diff] [blame] | 1016 | _Py_GetObjects(PyObject *self, PyObject *args) |
Sjoerd Mullender | 6ec3c65 | 1995-08-29 09:18:14 +0000 | [diff] [blame] | 1017 | { |
| 1018 | int i, n; |
| 1019 | PyObject *t = NULL; |
| 1020 | PyObject *res, *op; |
| 1021 | |
| 1022 | if (!PyArg_ParseTuple(args, "i|O", &n, &t)) |
| 1023 | return NULL; |
| 1024 | op = refchain._ob_next; |
| 1025 | res = PyList_New(0); |
| 1026 | if (res == NULL) |
| 1027 | return NULL; |
| 1028 | for (i = 0; (n == 0 || i < n) && op != &refchain; i++) { |
| 1029 | while (op == self || op == args || op == res || op == t || |
| 1030 | t != NULL && op->ob_type != (PyTypeObject *) t) { |
| 1031 | op = op->_ob_next; |
| 1032 | if (op == &refchain) |
| 1033 | return res; |
| 1034 | } |
| 1035 | if (PyList_Append(res, op) < 0) { |
| 1036 | Py_DECREF(res); |
| 1037 | return NULL; |
| 1038 | } |
| 1039 | op = op->_ob_next; |
| 1040 | } |
| 1041 | return res; |
| 1042 | } |
| 1043 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1044 | #endif |
Guido van Rossum | 97ead3f | 1996-01-12 01:24:09 +0000 | [diff] [blame] | 1045 | |
| 1046 | |
| 1047 | /* Hack to force loading of cobject.o */ |
Guido van Rossum | da9c271 | 1996-12-05 21:58:58 +0000 | [diff] [blame] | 1048 | PyTypeObject *_Py_cobject_hack = &PyCObject_Type; |
Guido van Rossum | 84a9032 | 1996-05-22 16:34:47 +0000 | [diff] [blame] | 1049 | |
| 1050 | |
| 1051 | /* Hack to force loading of abstract.o */ |
Jeremy Hylton | 6253f83 | 2000-07-12 12:56:19 +0000 | [diff] [blame] | 1052 | int (*_Py_abstract_hack)(PyObject *) = &PyObject_Size; |
Guido van Rossum | e09fb55 | 1997-08-05 02:04:34 +0000 | [diff] [blame] | 1053 | |
| 1054 | |
Andrew M. Kuchling | 1582a3a | 2000-08-16 12:27:23 +0000 | [diff] [blame] | 1055 | /* Python's malloc wrappers (see pymem.h) */ |
Guido van Rossum | e09fb55 | 1997-08-05 02:04:34 +0000 | [diff] [blame] | 1056 | |
Thomas Wouters | 334fb89 | 2000-07-25 12:56:38 +0000 | [diff] [blame] | 1057 | void * |
Fred Drake | 100814d | 2000-07-09 15:48:49 +0000 | [diff] [blame] | 1058 | PyMem_Malloc(size_t nbytes) |
Guido van Rossum | e09fb55 | 1997-08-05 02:04:34 +0000 | [diff] [blame] | 1059 | { |
| 1060 | #if _PyMem_EXTRA > 0 |
| 1061 | if (nbytes == 0) |
| 1062 | nbytes = _PyMem_EXTRA; |
| 1063 | #endif |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 1064 | return PyMem_MALLOC(nbytes); |
Guido van Rossum | e09fb55 | 1997-08-05 02:04:34 +0000 | [diff] [blame] | 1065 | } |
| 1066 | |
Thomas Wouters | 334fb89 | 2000-07-25 12:56:38 +0000 | [diff] [blame] | 1067 | void * |
| 1068 | PyMem_Realloc(void *p, size_t nbytes) |
Guido van Rossum | e09fb55 | 1997-08-05 02:04:34 +0000 | [diff] [blame] | 1069 | { |
| 1070 | #if _PyMem_EXTRA > 0 |
| 1071 | if (nbytes == 0) |
| 1072 | nbytes = _PyMem_EXTRA; |
| 1073 | #endif |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 1074 | return PyMem_REALLOC(p, nbytes); |
Guido van Rossum | e09fb55 | 1997-08-05 02:04:34 +0000 | [diff] [blame] | 1075 | } |
| 1076 | |
| 1077 | void |
Thomas Wouters | 334fb89 | 2000-07-25 12:56:38 +0000 | [diff] [blame] | 1078 | PyMem_Free(void *p) |
Guido van Rossum | e09fb55 | 1997-08-05 02:04:34 +0000 | [diff] [blame] | 1079 | { |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 1080 | PyMem_FREE(p); |
| 1081 | } |
| 1082 | |
| 1083 | |
| 1084 | /* Python's object malloc wrappers (see objimpl.h) */ |
| 1085 | |
Thomas Wouters | 334fb89 | 2000-07-25 12:56:38 +0000 | [diff] [blame] | 1086 | void * |
Fred Drake | 100814d | 2000-07-09 15:48:49 +0000 | [diff] [blame] | 1087 | PyObject_Malloc(size_t nbytes) |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 1088 | { |
| 1089 | return PyObject_MALLOC(nbytes); |
| 1090 | } |
| 1091 | |
Thomas Wouters | 334fb89 | 2000-07-25 12:56:38 +0000 | [diff] [blame] | 1092 | void * |
| 1093 | PyObject_Realloc(void *p, size_t nbytes) |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 1094 | { |
| 1095 | return PyObject_REALLOC(p, nbytes); |
| 1096 | } |
| 1097 | |
| 1098 | void |
Thomas Wouters | 334fb89 | 2000-07-25 12:56:38 +0000 | [diff] [blame] | 1099 | PyObject_Free(void *p) |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 1100 | { |
| 1101 | PyObject_FREE(p); |
Guido van Rossum | e09fb55 | 1997-08-05 02:04:34 +0000 | [diff] [blame] | 1102 | } |
Guido van Rossum | 8661036 | 1998-04-10 22:32:46 +0000 | [diff] [blame] | 1103 | |
| 1104 | |
| 1105 | /* These methods are used to control infinite recursion in repr, str, print, |
| 1106 | etc. Container objects that may recursively contain themselves, |
| 1107 | e.g. builtin dictionaries and lists, should used Py_ReprEnter() and |
| 1108 | Py_ReprLeave() to avoid infinite recursion. |
| 1109 | |
| 1110 | Py_ReprEnter() returns 0 the first time it is called for a particular |
| 1111 | object and 1 every time thereafter. It returns -1 if an exception |
| 1112 | occurred. Py_ReprLeave() has no return value. |
| 1113 | |
| 1114 | See dictobject.c and listobject.c for examples of use. |
| 1115 | */ |
| 1116 | |
| 1117 | #define KEY "Py_Repr" |
| 1118 | |
| 1119 | int |
Fred Drake | 100814d | 2000-07-09 15:48:49 +0000 | [diff] [blame] | 1120 | Py_ReprEnter(PyObject *obj) |
Guido van Rossum | 8661036 | 1998-04-10 22:32:46 +0000 | [diff] [blame] | 1121 | { |
| 1122 | PyObject *dict; |
| 1123 | PyObject *list; |
| 1124 | int i; |
| 1125 | |
| 1126 | dict = PyThreadState_GetDict(); |
| 1127 | if (dict == NULL) |
| 1128 | return -1; |
| 1129 | list = PyDict_GetItemString(dict, KEY); |
| 1130 | if (list == NULL) { |
| 1131 | list = PyList_New(0); |
| 1132 | if (list == NULL) |
| 1133 | return -1; |
| 1134 | if (PyDict_SetItemString(dict, KEY, list) < 0) |
| 1135 | return -1; |
| 1136 | Py_DECREF(list); |
| 1137 | } |
| 1138 | i = PyList_GET_SIZE(list); |
| 1139 | while (--i >= 0) { |
| 1140 | if (PyList_GET_ITEM(list, i) == obj) |
| 1141 | return 1; |
| 1142 | } |
| 1143 | PyList_Append(list, obj); |
| 1144 | return 0; |
| 1145 | } |
| 1146 | |
| 1147 | void |
Fred Drake | 100814d | 2000-07-09 15:48:49 +0000 | [diff] [blame] | 1148 | Py_ReprLeave(PyObject *obj) |
Guido van Rossum | 8661036 | 1998-04-10 22:32:46 +0000 | [diff] [blame] | 1149 | { |
| 1150 | PyObject *dict; |
| 1151 | PyObject *list; |
| 1152 | int i; |
| 1153 | |
| 1154 | dict = PyThreadState_GetDict(); |
Guido van Rossum | eb90946 | 1998-04-11 15:17:34 +0000 | [diff] [blame] | 1155 | if (dict == NULL) |
| 1156 | return; |
Guido van Rossum | 8661036 | 1998-04-10 22:32:46 +0000 | [diff] [blame] | 1157 | list = PyDict_GetItemString(dict, KEY); |
Guido van Rossum | eb90946 | 1998-04-11 15:17:34 +0000 | [diff] [blame] | 1158 | if (list == NULL || !PyList_Check(list)) |
| 1159 | return; |
Guido van Rossum | 8661036 | 1998-04-10 22:32:46 +0000 | [diff] [blame] | 1160 | i = PyList_GET_SIZE(list); |
| 1161 | /* Count backwards because we always expect obj to be list[-1] */ |
| 1162 | while (--i >= 0) { |
| 1163 | if (PyList_GET_ITEM(list, i) == obj) { |
| 1164 | PyList_SetSlice(list, i, i + 1, NULL); |
| 1165 | break; |
| 1166 | } |
| 1167 | } |
| 1168 | } |
Guido van Rossum | d724b23 | 2000-03-13 16:01:29 +0000 | [diff] [blame] | 1169 | |
| 1170 | /* |
| 1171 | trashcan |
| 1172 | CT 2k0130 |
| 1173 | non-recursively destroy nested objects |
| 1174 | |
| 1175 | CT 2k0223 |
| 1176 | everything is now done in a macro. |
| 1177 | |
| 1178 | CT 2k0305 |
| 1179 | modified to use functions, after Tim Peter's suggestion. |
| 1180 | |
| 1181 | CT 2k0309 |
| 1182 | modified to restore a possible error. |
Guido van Rossum | 13ff8eb | 2000-03-25 18:39:19 +0000 | [diff] [blame] | 1183 | |
| 1184 | CT 2k0325 |
| 1185 | added better safe than sorry check for threadstate |
Guido van Rossum | e92e610 | 2000-04-24 15:40:53 +0000 | [diff] [blame] | 1186 | |
| 1187 | CT 2k0422 |
| 1188 | complete rewrite. We now build a chain via ob_type |
| 1189 | and save the limited number of types in ob_refcnt. |
| 1190 | This is perfect since we don't need any memory. |
| 1191 | A patch for free-threading would need just a lock. |
Guido van Rossum | d724b23 | 2000-03-13 16:01:29 +0000 | [diff] [blame] | 1192 | */ |
| 1193 | |
Guido van Rossum | e92e610 | 2000-04-24 15:40:53 +0000 | [diff] [blame] | 1194 | #define Py_TRASHCAN_TUPLE 1 |
| 1195 | #define Py_TRASHCAN_LIST 2 |
| 1196 | #define Py_TRASHCAN_DICT 3 |
| 1197 | #define Py_TRASHCAN_FRAME 4 |
| 1198 | #define Py_TRASHCAN_TRACEBACK 5 |
| 1199 | /* extend here if other objects want protection */ |
| 1200 | |
Guido van Rossum | d724b23 | 2000-03-13 16:01:29 +0000 | [diff] [blame] | 1201 | int _PyTrash_delete_nesting = 0; |
Guido van Rossum | e92e610 | 2000-04-24 15:40:53 +0000 | [diff] [blame] | 1202 | |
Guido van Rossum | d724b23 | 2000-03-13 16:01:29 +0000 | [diff] [blame] | 1203 | PyObject * _PyTrash_delete_later = NULL; |
| 1204 | |
| 1205 | void |
Fred Drake | 100814d | 2000-07-09 15:48:49 +0000 | [diff] [blame] | 1206 | _PyTrash_deposit_object(PyObject *op) |
Guido van Rossum | d724b23 | 2000-03-13 16:01:29 +0000 | [diff] [blame] | 1207 | { |
Guido van Rossum | e92e610 | 2000-04-24 15:40:53 +0000 | [diff] [blame] | 1208 | int typecode; |
Guido van Rossum | 13ff8eb | 2000-03-25 18:39:19 +0000 | [diff] [blame] | 1209 | |
Guido van Rossum | e92e610 | 2000-04-24 15:40:53 +0000 | [diff] [blame] | 1210 | if (PyTuple_Check(op)) |
| 1211 | typecode = Py_TRASHCAN_TUPLE; |
| 1212 | else if (PyList_Check(op)) |
| 1213 | typecode = Py_TRASHCAN_LIST; |
| 1214 | else if (PyDict_Check(op)) |
| 1215 | typecode = Py_TRASHCAN_DICT; |
| 1216 | else if (PyFrame_Check(op)) |
| 1217 | typecode = Py_TRASHCAN_FRAME; |
| 1218 | else if (PyTraceBack_Check(op)) |
| 1219 | typecode = Py_TRASHCAN_TRACEBACK; |
Moshe Zadka | cf703f0 | 2000-08-04 15:36:13 +0000 | [diff] [blame] | 1220 | else /* We have a bug here -- those are the only types in GC */ { |
| 1221 | Py_FatalError("Type not supported in GC -- internal bug"); |
| 1222 | return; /* pacify compiler -- execution never here */ |
| 1223 | } |
Guido van Rossum | e92e610 | 2000-04-24 15:40:53 +0000 | [diff] [blame] | 1224 | op->ob_refcnt = typecode; |
Guido van Rossum | d724b23 | 2000-03-13 16:01:29 +0000 | [diff] [blame] | 1225 | |
Guido van Rossum | e92e610 | 2000-04-24 15:40:53 +0000 | [diff] [blame] | 1226 | op->ob_type = (PyTypeObject*)_PyTrash_delete_later; |
| 1227 | _PyTrash_delete_later = op; |
Guido van Rossum | d724b23 | 2000-03-13 16:01:29 +0000 | [diff] [blame] | 1228 | } |
| 1229 | |
| 1230 | void |
Fred Drake | 100814d | 2000-07-09 15:48:49 +0000 | [diff] [blame] | 1231 | _PyTrash_destroy_chain(void) |
Guido van Rossum | d724b23 | 2000-03-13 16:01:29 +0000 | [diff] [blame] | 1232 | { |
| 1233 | while (_PyTrash_delete_later) { |
| 1234 | PyObject *shredder = _PyTrash_delete_later; |
Guido van Rossum | e92e610 | 2000-04-24 15:40:53 +0000 | [diff] [blame] | 1235 | _PyTrash_delete_later = (PyObject*) shredder->ob_type; |
| 1236 | |
| 1237 | switch (shredder->ob_refcnt) { |
| 1238 | case Py_TRASHCAN_TUPLE: |
| 1239 | shredder->ob_type = &PyTuple_Type; |
| 1240 | break; |
| 1241 | case Py_TRASHCAN_LIST: |
| 1242 | shredder->ob_type = &PyList_Type; |
| 1243 | break; |
| 1244 | case Py_TRASHCAN_DICT: |
| 1245 | shredder->ob_type = &PyDict_Type; |
| 1246 | break; |
| 1247 | case Py_TRASHCAN_FRAME: |
| 1248 | shredder->ob_type = &PyFrame_Type; |
| 1249 | break; |
| 1250 | case Py_TRASHCAN_TRACEBACK: |
| 1251 | shredder->ob_type = &PyTraceBack_Type; |
| 1252 | break; |
| 1253 | } |
| 1254 | _Py_NewReference(shredder); |
| 1255 | |
Guido van Rossum | d724b23 | 2000-03-13 16:01:29 +0000 | [diff] [blame] | 1256 | ++_PyTrash_delete_nesting; |
| 1257 | Py_DECREF(shredder); |
| 1258 | --_PyTrash_delete_nesting; |
| 1259 | } |
| 1260 | } |