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