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 | 393661d | 2001-08-31 17:40:15 +0000 | [diff] [blame] | 19 | DL_IMPORT(int) Py_DivisionWarningFlag; |
| 20 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 21 | /* Object allocation routines used by NEWOBJ and NEWVAROBJ macros. |
| 22 | These are used by the individual routines for object creation. |
| 23 | Do not call them otherwise, they do not initialize the object! */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 24 | |
Sjoerd Mullender | a9c3c22 | 1993-10-11 12:54:31 +0000 | [diff] [blame] | 25 | #ifdef COUNT_ALLOCS |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 26 | static PyTypeObject *type_list; |
Sjoerd Mullender | 842d2cc | 1993-10-15 16:18:48 +0000 | [diff] [blame] | 27 | extern int tuple_zero_allocs, fast_tuple_allocs; |
| 28 | extern int quick_int_allocs, quick_neg_int_allocs; |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 29 | extern int null_strings, one_strings; |
Sjoerd Mullender | a9c3c22 | 1993-10-11 12:54:31 +0000 | [diff] [blame] | 30 | void |
Fred Drake | 100814d | 2000-07-09 15:48:49 +0000 | [diff] [blame] | 31 | dump_counts(void) |
Sjoerd Mullender | a9c3c22 | 1993-10-11 12:54:31 +0000 | [diff] [blame] | 32 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 33 | PyTypeObject *tp; |
Sjoerd Mullender | a9c3c22 | 1993-10-11 12:54:31 +0000 | [diff] [blame] | 34 | |
| 35 | for (tp = type_list; tp; tp = tp->tp_next) |
Sjoerd Mullender | 52c1f51 | 1993-10-25 08:40:52 +0000 | [diff] [blame] | 36 | fprintf(stderr, "%s alloc'd: %d, freed: %d, max in use: %d\n", |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 37 | tp->tp_name, tp->tp_allocs, tp->tp_frees, |
Sjoerd Mullender | 52c1f51 | 1993-10-25 08:40:52 +0000 | [diff] [blame] | 38 | tp->tp_maxalloc); |
| 39 | fprintf(stderr, "fast tuple allocs: %d, empty: %d\n", |
| 40 | fast_tuple_allocs, tuple_zero_allocs); |
| 41 | fprintf(stderr, "fast int allocs: pos: %d, neg: %d\n", |
| 42 | quick_int_allocs, quick_neg_int_allocs); |
| 43 | fprintf(stderr, "null strings: %d, 1-strings: %d\n", |
| 44 | null_strings, one_strings); |
Sjoerd Mullender | a9c3c22 | 1993-10-11 12:54:31 +0000 | [diff] [blame] | 45 | } |
| 46 | |
Sjoerd Mullender | 6ec3c65 | 1995-08-29 09:18:14 +0000 | [diff] [blame] | 47 | PyObject * |
Fred Drake | 100814d | 2000-07-09 15:48:49 +0000 | [diff] [blame] | 48 | get_counts(void) |
Sjoerd Mullender | 6ec3c65 | 1995-08-29 09:18:14 +0000 | [diff] [blame] | 49 | { |
| 50 | PyTypeObject *tp; |
| 51 | PyObject *result; |
| 52 | PyObject *v; |
| 53 | |
| 54 | result = PyList_New(0); |
| 55 | if (result == NULL) |
| 56 | return NULL; |
| 57 | for (tp = type_list; tp; tp = tp->tp_next) { |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 58 | v = Py_BuildValue("(siii)", tp->tp_name, tp->tp_allocs, |
| 59 | tp->tp_frees, tp->tp_maxalloc); |
Sjoerd Mullender | 6ec3c65 | 1995-08-29 09:18:14 +0000 | [diff] [blame] | 60 | if (v == NULL) { |
| 61 | Py_DECREF(result); |
| 62 | return NULL; |
| 63 | } |
| 64 | if (PyList_Append(result, v) < 0) { |
| 65 | Py_DECREF(v); |
| 66 | Py_DECREF(result); |
| 67 | return NULL; |
| 68 | } |
| 69 | Py_DECREF(v); |
| 70 | } |
| 71 | return result; |
| 72 | } |
| 73 | |
Sjoerd Mullender | a9c3c22 | 1993-10-11 12:54:31 +0000 | [diff] [blame] | 74 | void |
Fred Drake | 100814d | 2000-07-09 15:48:49 +0000 | [diff] [blame] | 75 | inc_count(PyTypeObject *tp) |
Sjoerd Mullender | a9c3c22 | 1993-10-11 12:54:31 +0000 | [diff] [blame] | 76 | { |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 77 | if (tp->tp_allocs == 0) { |
Guido van Rossum | d8953cb | 1995-04-06 14:46:26 +0000 | [diff] [blame] | 78 | /* first time; insert in linked list */ |
Sjoerd Mullender | a9c3c22 | 1993-10-11 12:54:31 +0000 | [diff] [blame] | 79 | if (tp->tp_next != NULL) /* sanity check */ |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 80 | Py_FatalError("XXX inc_count sanity check"); |
Sjoerd Mullender | a9c3c22 | 1993-10-11 12:54:31 +0000 | [diff] [blame] | 81 | tp->tp_next = type_list; |
| 82 | type_list = tp; |
| 83 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 84 | tp->tp_allocs++; |
| 85 | if (tp->tp_allocs - tp->tp_frees > tp->tp_maxalloc) |
| 86 | tp->tp_maxalloc = tp->tp_allocs - tp->tp_frees; |
Sjoerd Mullender | a9c3c22 | 1993-10-11 12:54:31 +0000 | [diff] [blame] | 87 | } |
| 88 | #endif |
| 89 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 90 | PyObject * |
Fred Drake | 100814d | 2000-07-09 15:48:49 +0000 | [diff] [blame] | 91 | PyObject_Init(PyObject *op, PyTypeObject *tp) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 92 | { |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 93 | if (op == NULL) { |
| 94 | PyErr_SetString(PyExc_SystemError, |
| 95 | "NULL object passed to PyObject_Init"); |
| 96 | return op; |
| 97 | } |
| 98 | /* Any changes should be reflected in PyObject_INIT (objimpl.h) */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 99 | op->ob_type = tp; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 100 | _Py_NewReference(op); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 101 | return op; |
| 102 | } |
| 103 | |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 104 | PyVarObject * |
Fred Drake | 100814d | 2000-07-09 15:48:49 +0000 | [diff] [blame] | 105 | PyObject_InitVar(PyVarObject *op, PyTypeObject *tp, int size) |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 106 | { |
| 107 | if (op == NULL) { |
| 108 | PyErr_SetString(PyExc_SystemError, |
| 109 | "NULL object passed to PyObject_InitVar"); |
| 110 | return op; |
| 111 | } |
| 112 | /* Any changes should be reflected in PyObject_INIT_VAR */ |
| 113 | op->ob_size = size; |
| 114 | op->ob_type = tp; |
| 115 | _Py_NewReference((PyObject *)op); |
| 116 | return op; |
| 117 | } |
| 118 | |
| 119 | PyObject * |
Fred Drake | 100814d | 2000-07-09 15:48:49 +0000 | [diff] [blame] | 120 | _PyObject_New(PyTypeObject *tp) |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 121 | { |
| 122 | PyObject *op; |
| 123 | op = (PyObject *) PyObject_MALLOC(_PyObject_SIZE(tp)); |
| 124 | if (op == NULL) |
| 125 | return PyErr_NoMemory(); |
| 126 | return PyObject_INIT(op, tp); |
| 127 | } |
| 128 | |
Guido van Rossum | d0c87ee | 1997-05-15 21:31:03 +0000 | [diff] [blame] | 129 | PyVarObject * |
Tim Peters | 6d483d3 | 2001-10-06 21:27:34 +0000 | [diff] [blame] | 130 | _PyObject_NewVar(PyTypeObject *tp, int nitems) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 131 | { |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 132 | PyVarObject *op; |
Tim Peters | f2a67da | 2001-10-07 03:54:51 +0000 | [diff] [blame] | 133 | const size_t size = _PyObject_VAR_SIZE(tp, nitems); |
Tim Peters | 6d483d3 | 2001-10-06 21:27:34 +0000 | [diff] [blame] | 134 | op = (PyVarObject *) PyObject_MALLOC(size); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 135 | if (op == NULL) |
Guido van Rossum | d0c87ee | 1997-05-15 21:31:03 +0000 | [diff] [blame] | 136 | return (PyVarObject *)PyErr_NoMemory(); |
Tim Peters | 6d483d3 | 2001-10-06 21:27:34 +0000 | [diff] [blame] | 137 | return PyObject_INIT_VAR(op, tp, nitems); |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | void |
Fred Drake | 100814d | 2000-07-09 15:48:49 +0000 | [diff] [blame] | 141 | _PyObject_Del(PyObject *op) |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 142 | { |
Guido van Rossum | 4cc6ac7 | 2000-07-01 01:00:38 +0000 | [diff] [blame] | 143 | PyObject_FREE(op); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 144 | } |
| 145 | |
Guido van Rossum | 9093361 | 1991-06-07 16:10:43 +0000 | [diff] [blame] | 146 | int |
Fred Drake | 100814d | 2000-07-09 15:48:49 +0000 | [diff] [blame] | 147 | PyObject_Print(PyObject *op, FILE *fp, int flags) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 148 | { |
Guido van Rossum | 278ef59 | 1991-07-27 21:40:24 +0000 | [diff] [blame] | 149 | int ret = 0; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 150 | if (PyErr_CheckSignals()) |
Guido van Rossum | 9093361 | 1991-06-07 16:10:43 +0000 | [diff] [blame] | 151 | return -1; |
Guido van Rossum | 9b00dfa | 1998-04-28 16:06:54 +0000 | [diff] [blame] | 152 | #ifdef USE_STACKCHECK |
| 153 | if (PyOS_CheckStack()) { |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 154 | PyErr_SetString(PyExc_MemoryError, "stack overflow"); |
Guido van Rossum | 9b00dfa | 1998-04-28 16:06:54 +0000 | [diff] [blame] | 155 | return -1; |
| 156 | } |
| 157 | #endif |
Guido van Rossum | 687ef6e | 2000-01-12 16:28:58 +0000 | [diff] [blame] | 158 | clearerr(fp); /* Clear any previous error condition */ |
Guido van Rossum | 9093361 | 1991-06-07 16:10:43 +0000 | [diff] [blame] | 159 | if (op == NULL) { |
| 160 | fprintf(fp, "<nil>"); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 161 | } |
Guido van Rossum | 9093361 | 1991-06-07 16:10:43 +0000 | [diff] [blame] | 162 | else { |
| 163 | if (op->ob_refcnt <= 0) |
Fred Drake | a44d353 | 2000-06-30 15:01:00 +0000 | [diff] [blame] | 164 | fprintf(fp, "<refcnt %u at %p>", |
| 165 | op->ob_refcnt, op); |
Guido van Rossum | 2e8f614 | 1992-09-03 20:32:55 +0000 | [diff] [blame] | 166 | else if (op->ob_type->tp_print == NULL) { |
Guido van Rossum | 4f288ab | 2001-05-01 16:53:37 +0000 | [diff] [blame] | 167 | PyObject *s; |
| 168 | if (flags & Py_PRINT_RAW) |
| 169 | s = PyObject_Str(op); |
| 170 | else |
| 171 | s = PyObject_Repr(op); |
| 172 | if (s == NULL) |
| 173 | ret = -1; |
Guido van Rossum | 2e8f614 | 1992-09-03 20:32:55 +0000 | [diff] [blame] | 174 | else { |
Guido van Rossum | 4f288ab | 2001-05-01 16:53:37 +0000 | [diff] [blame] | 175 | ret = PyObject_Print(s, fp, Py_PRINT_RAW); |
Guido van Rossum | 2e8f614 | 1992-09-03 20:32:55 +0000 | [diff] [blame] | 176 | } |
Guido van Rossum | 4f288ab | 2001-05-01 16:53:37 +0000 | [diff] [blame] | 177 | Py_XDECREF(s); |
Guido van Rossum | 2e8f614 | 1992-09-03 20:32:55 +0000 | [diff] [blame] | 178 | } |
Guido van Rossum | 9093361 | 1991-06-07 16:10:43 +0000 | [diff] [blame] | 179 | else |
Guido van Rossum | 278ef59 | 1991-07-27 21:40:24 +0000 | [diff] [blame] | 180 | ret = (*op->ob_type->tp_print)(op, fp, flags); |
Guido van Rossum | 9093361 | 1991-06-07 16:10:43 +0000 | [diff] [blame] | 181 | } |
Guido van Rossum | 278ef59 | 1991-07-27 21:40:24 +0000 | [diff] [blame] | 182 | if (ret == 0) { |
| 183 | if (ferror(fp)) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 184 | PyErr_SetFromErrno(PyExc_IOError); |
Guido van Rossum | 278ef59 | 1991-07-27 21:40:24 +0000 | [diff] [blame] | 185 | clearerr(fp); |
| 186 | ret = -1; |
| 187 | } |
| 188 | } |
| 189 | return ret; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 190 | } |
| 191 | |
Barry Warsaw | 9bf1644 | 2001-01-23 16:24:35 +0000 | [diff] [blame] | 192 | /* For debugging convenience. See Misc/gdbinit for some useful gdb hooks */ |
Barry Warsaw | bbd89b6 | 2001-01-24 04:18:13 +0000 | [diff] [blame] | 193 | void _PyObject_Dump(PyObject* op) |
Barry Warsaw | 9bf1644 | 2001-01-23 16:24:35 +0000 | [diff] [blame] | 194 | { |
Barry Warsaw | eefb107 | 2001-02-22 22:39:18 +0000 | [diff] [blame] | 195 | if (op == NULL) |
| 196 | fprintf(stderr, "NULL\n"); |
| 197 | else { |
Guido van Rossum | 5f5512d | 2001-09-14 15:50:08 +0000 | [diff] [blame] | 198 | fprintf(stderr, "object : "); |
Barry Warsaw | eefb107 | 2001-02-22 22:39:18 +0000 | [diff] [blame] | 199 | (void)PyObject_Print(op, stderr, 0); |
Guido van Rossum | 5f5512d | 2001-09-14 15:50:08 +0000 | [diff] [blame] | 200 | fprintf(stderr, "\n" |
| 201 | "type : %s\n" |
| 202 | "refcount: %d\n" |
| 203 | "address : %p\n", |
| 204 | op->ob_type==NULL ? "NULL" : op->ob_type->tp_name, |
| 205 | op->ob_refcnt, |
| 206 | op); |
Barry Warsaw | eefb107 | 2001-02-22 22:39:18 +0000 | [diff] [blame] | 207 | } |
Barry Warsaw | 9bf1644 | 2001-01-23 16:24:35 +0000 | [diff] [blame] | 208 | } |
Barry Warsaw | 903138f | 2001-01-23 16:33:18 +0000 | [diff] [blame] | 209 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 210 | PyObject * |
Fred Drake | 100814d | 2000-07-09 15:48:49 +0000 | [diff] [blame] | 211 | PyObject_Repr(PyObject *v) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 212 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 213 | if (PyErr_CheckSignals()) |
Guido van Rossum | 9093361 | 1991-06-07 16:10:43 +0000 | [diff] [blame] | 214 | return NULL; |
Guido van Rossum | 9b00dfa | 1998-04-28 16:06:54 +0000 | [diff] [blame] | 215 | #ifdef USE_STACKCHECK |
| 216 | if (PyOS_CheckStack()) { |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 217 | PyErr_SetString(PyExc_MemoryError, "stack overflow"); |
Guido van Rossum | 9b00dfa | 1998-04-28 16:06:54 +0000 | [diff] [blame] | 218 | return NULL; |
| 219 | } |
| 220 | #endif |
Guido van Rossum | 9093361 | 1991-06-07 16:10:43 +0000 | [diff] [blame] | 221 | if (v == NULL) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 222 | return PyString_FromString("<NULL>"); |
Barry Warsaw | 7ce3694 | 2001-08-24 18:34:26 +0000 | [diff] [blame] | 223 | else if (v->ob_type->tp_repr == NULL) |
Guido van Rossum | 21922aa | 2001-08-30 20:26:05 +0000 | [diff] [blame] | 224 | return PyString_FromFormat("<%s object at %p>", |
Barry Warsaw | 7ce3694 | 2001-08-24 18:34:26 +0000 | [diff] [blame] | 225 | v->ob_type->tp_name, v); |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 226 | else { |
| 227 | PyObject *res; |
| 228 | res = (*v->ob_type->tp_repr)(v); |
| 229 | if (res == NULL) |
| 230 | return NULL; |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 231 | #ifdef Py_USING_UNICODE |
Fredrik Lundh | efecc7d | 2000-07-01 14:31:09 +0000 | [diff] [blame] | 232 | if (PyUnicode_Check(res)) { |
| 233 | PyObject* str; |
Fredrik Lundh | 2a1e060 | 2000-07-08 17:43:32 +0000 | [diff] [blame] | 234 | str = PyUnicode_AsUnicodeEscapeString(res); |
Marc-André Lemburg | 891bc65 | 2000-07-03 09:57:53 +0000 | [diff] [blame] | 235 | Py_DECREF(res); |
| 236 | if (str) |
Fredrik Lundh | efecc7d | 2000-07-01 14:31:09 +0000 | [diff] [blame] | 237 | res = str; |
Marc-André Lemburg | 891bc65 | 2000-07-03 09:57:53 +0000 | [diff] [blame] | 238 | else |
| 239 | return NULL; |
Fredrik Lundh | efecc7d | 2000-07-01 14:31:09 +0000 | [diff] [blame] | 240 | } |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 241 | #endif |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 242 | if (!PyString_Check(res)) { |
| 243 | PyErr_Format(PyExc_TypeError, |
Guido van Rossum | 5db862d | 2000-04-10 12:46:51 +0000 | [diff] [blame] | 244 | "__repr__ returned non-string (type %.200s)", |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 245 | res->ob_type->tp_name); |
| 246 | Py_DECREF(res); |
| 247 | return NULL; |
| 248 | } |
| 249 | return res; |
| 250 | } |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 251 | } |
| 252 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 253 | PyObject * |
Fred Drake | 100814d | 2000-07-09 15:48:49 +0000 | [diff] [blame] | 254 | PyObject_Str(PyObject *v) |
Guido van Rossum | c600411 | 1993-11-05 10:22:19 +0000 | [diff] [blame] | 255 | { |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 256 | PyObject *res; |
| 257 | |
Guido van Rossum | c600411 | 1993-11-05 10:22:19 +0000 | [diff] [blame] | 258 | if (v == NULL) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 259 | return PyString_FromString("<NULL>"); |
Tim Peters | 5a49ade | 2001-09-11 01:41:59 +0000 | [diff] [blame] | 260 | if (PyString_CheckExact(v)) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 261 | Py_INCREF(v); |
Guido van Rossum | c600411 | 1993-11-05 10:22:19 +0000 | [diff] [blame] | 262 | return v; |
| 263 | } |
Guido van Rossum | 4f288ab | 2001-05-01 16:53:37 +0000 | [diff] [blame] | 264 | if (v->ob_type->tp_str == NULL) |
| 265 | return PyObject_Repr(v); |
| 266 | |
| 267 | res = (*v->ob_type->tp_str)(v); |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 268 | if (res == NULL) |
| 269 | return NULL; |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 270 | #ifdef Py_USING_UNICODE |
Marc-André Lemburg | 891bc65 | 2000-07-03 09:57:53 +0000 | [diff] [blame] | 271 | if (PyUnicode_Check(res)) { |
| 272 | PyObject* str; |
| 273 | str = PyUnicode_AsEncodedString(res, NULL, NULL); |
| 274 | Py_DECREF(res); |
| 275 | if (str) |
| 276 | res = str; |
| 277 | else |
| 278 | return NULL; |
| 279 | } |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 280 | #endif |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 281 | if (!PyString_Check(res)) { |
| 282 | PyErr_Format(PyExc_TypeError, |
Guido van Rossum | 5db862d | 2000-04-10 12:46:51 +0000 | [diff] [blame] | 283 | "__str__ returned non-string (type %.200s)", |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 284 | res->ob_type->tp_name); |
| 285 | Py_DECREF(res); |
| 286 | return NULL; |
| 287 | } |
| 288 | return res; |
Guido van Rossum | c600411 | 1993-11-05 10:22:19 +0000 | [diff] [blame] | 289 | } |
| 290 | |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 291 | #ifdef Py_USING_UNICODE |
Guido van Rossum | a3af41d | 2001-01-18 22:07:06 +0000 | [diff] [blame] | 292 | PyObject * |
| 293 | PyObject_Unicode(PyObject *v) |
| 294 | { |
| 295 | PyObject *res; |
| 296 | |
| 297 | if (v == NULL) |
| 298 | res = PyString_FromString("<NULL>"); |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 299 | if (PyUnicode_CheckExact(v)) { |
Guido van Rossum | a3af41d | 2001-01-18 22:07:06 +0000 | [diff] [blame] | 300 | Py_INCREF(v); |
| 301 | return v; |
| 302 | } |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 303 | if (PyUnicode_Check(v)) { |
| 304 | /* For a Unicode subtype that's not a Unicode object, |
| 305 | return a true Unicode object with the same data. */ |
| 306 | return PyUnicode_FromUnicode(PyUnicode_AS_UNICODE(v), |
| 307 | PyUnicode_GET_SIZE(v)); |
| 308 | } |
| 309 | if (PyString_Check(v)) { |
Marc-André Lemburg | ae60534 | 2001-03-25 19:16:13 +0000 | [diff] [blame] | 310 | Py_INCREF(v); |
Guido van Rossum | a3af41d | 2001-01-18 22:07:06 +0000 | [diff] [blame] | 311 | res = v; |
Marc-André Lemburg | ae60534 | 2001-03-25 19:16:13 +0000 | [diff] [blame] | 312 | } |
Guido van Rossum | a3af41d | 2001-01-18 22:07:06 +0000 | [diff] [blame] | 313 | else { |
| 314 | PyObject *func; |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 315 | static PyObject *unicodestr; |
| 316 | /* XXX As soon as we have a tp_unicode slot, we should |
| 317 | check this before trying the __unicode__ |
| 318 | method. */ |
| 319 | if (unicodestr == NULL) { |
| 320 | unicodestr= PyString_InternFromString( |
| 321 | "__unicode__"); |
| 322 | if (unicodestr == NULL) |
Guido van Rossum | a3af41d | 2001-01-18 22:07:06 +0000 | [diff] [blame] | 323 | return NULL; |
| 324 | } |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 325 | func = PyObject_GetAttr(v, unicodestr); |
| 326 | if (func != NULL) { |
Guido van Rossum | a3af41d | 2001-01-18 22:07:06 +0000 | [diff] [blame] | 327 | res = PyEval_CallObject(func, (PyObject *)NULL); |
| 328 | Py_DECREF(func); |
| 329 | } |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 330 | else { |
| 331 | PyErr_Clear(); |
| 332 | if (v->ob_type->tp_str != NULL) |
| 333 | res = (*v->ob_type->tp_str)(v); |
| 334 | else |
| 335 | res = PyObject_Repr(v); |
| 336 | } |
Guido van Rossum | a3af41d | 2001-01-18 22:07:06 +0000 | [diff] [blame] | 337 | } |
| 338 | if (res == NULL) |
| 339 | return NULL; |
| 340 | if (!PyUnicode_Check(res)) { |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 341 | PyObject *str; |
| 342 | str = PyUnicode_FromEncodedObject(res, NULL, "strict"); |
Guido van Rossum | a3af41d | 2001-01-18 22:07:06 +0000 | [diff] [blame] | 343 | Py_DECREF(res); |
| 344 | if (str) |
| 345 | res = str; |
| 346 | else |
| 347 | return NULL; |
| 348 | } |
| 349 | return res; |
| 350 | } |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 351 | #endif |
Guido van Rossum | a3af41d | 2001-01-18 22:07:06 +0000 | [diff] [blame] | 352 | |
| 353 | |
Guido van Rossum | d1f06b9 | 2001-01-24 22:14:43 +0000 | [diff] [blame] | 354 | /* Macro to get the tp_richcompare field of a type if defined */ |
| 355 | #define RICHCOMPARE(t) (PyType_HasFeature((t), Py_TPFLAGS_HAVE_RICHCOMPARE) \ |
| 356 | ? (t)->tp_richcompare : NULL) |
| 357 | |
Guido van Rossum | e797ec1 | 2001-01-17 15:24:28 +0000 | [diff] [blame] | 358 | /* Map rich comparison operators to their swapped version, e.g. LT --> GT */ |
| 359 | 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] | 360 | |
Guido van Rossum | e797ec1 | 2001-01-17 15:24:28 +0000 | [diff] [blame] | 361 | /* Try a genuine rich comparison, returning an object. Return: |
| 362 | NULL for exception; |
| 363 | NotImplemented if this particular rich comparison is not implemented or |
| 364 | undefined; |
| 365 | some object not equal to NotImplemented if it is implemented |
| 366 | (this latter object may not be a Boolean). |
| 367 | */ |
| 368 | static PyObject * |
| 369 | try_rich_compare(PyObject *v, PyObject *w, int op) |
| 370 | { |
| 371 | richcmpfunc f; |
| 372 | PyObject *res; |
| 373 | |
Guido van Rossum | 2ed6bf8 | 2001-09-27 20:30:07 +0000 | [diff] [blame] | 374 | if (v->ob_type != w->ob_type && |
| 375 | PyType_IsSubtype(w->ob_type, v->ob_type) && |
| 376 | (f = RICHCOMPARE(w->ob_type)) != NULL) { |
| 377 | res = (*f)(w, v, swapped_op[op]); |
| 378 | if (res != Py_NotImplemented) |
| 379 | return res; |
| 380 | Py_DECREF(res); |
| 381 | } |
Guido van Rossum | d1f06b9 | 2001-01-24 22:14:43 +0000 | [diff] [blame] | 382 | if ((f = RICHCOMPARE(v->ob_type)) != NULL) { |
Guido van Rossum | e797ec1 | 2001-01-17 15:24:28 +0000 | [diff] [blame] | 383 | res = (*f)(v, w, op); |
| 384 | if (res != Py_NotImplemented) |
| 385 | return res; |
| 386 | Py_DECREF(res); |
| 387 | } |
Guido van Rossum | d1f06b9 | 2001-01-24 22:14:43 +0000 | [diff] [blame] | 388 | if ((f = RICHCOMPARE(w->ob_type)) != NULL) { |
Guido van Rossum | e797ec1 | 2001-01-17 15:24:28 +0000 | [diff] [blame] | 389 | return (*f)(w, v, swapped_op[op]); |
| 390 | } |
| 391 | res = Py_NotImplemented; |
| 392 | Py_INCREF(res); |
| 393 | return res; |
| 394 | } |
| 395 | |
| 396 | /* Try a genuine rich comparison, returning an int. Return: |
| 397 | -1 for exception (including the case where try_rich_compare() returns an |
| 398 | object that's not a Boolean); |
Tim Peters | c99213f | 2001-11-04 05:57:16 +0000 | [diff] [blame] | 399 | 0 if the outcome is false; |
| 400 | 1 if the outcome is true; |
| 401 | 2 if this particular rich comparison is not implemented or undefined. |
Guido van Rossum | e797ec1 | 2001-01-17 15:24:28 +0000 | [diff] [blame] | 402 | */ |
Neil Schemenauer | 5ed85ec | 2001-01-04 01:48:10 +0000 | [diff] [blame] | 403 | static int |
Guido van Rossum | e797ec1 | 2001-01-17 15:24:28 +0000 | [diff] [blame] | 404 | try_rich_compare_bool(PyObject *v, PyObject *w, int op) |
| 405 | { |
| 406 | PyObject *res; |
| 407 | int ok; |
| 408 | |
Guido van Rossum | d1f06b9 | 2001-01-24 22:14:43 +0000 | [diff] [blame] | 409 | if (RICHCOMPARE(v->ob_type) == NULL && RICHCOMPARE(w->ob_type) == NULL) |
Guido van Rossum | e797ec1 | 2001-01-17 15:24:28 +0000 | [diff] [blame] | 410 | return 2; /* Shortcut, avoid INCREF+DECREF */ |
| 411 | res = try_rich_compare(v, w, op); |
| 412 | if (res == NULL) |
| 413 | return -1; |
| 414 | if (res == Py_NotImplemented) { |
| 415 | Py_DECREF(res); |
| 416 | return 2; |
| 417 | } |
| 418 | ok = PyObject_IsTrue(res); |
| 419 | Py_DECREF(res); |
| 420 | return ok; |
| 421 | } |
| 422 | |
| 423 | /* Try rich comparisons to determine a 3-way comparison. Return: |
| 424 | -2 for an exception; |
Tim Peters | c99213f | 2001-11-04 05:57:16 +0000 | [diff] [blame] | 425 | -1 if v < w; |
| 426 | 0 if v == w; |
| 427 | 1 if v > w; |
| 428 | 2 if this particular rich comparison is not implemented or undefined. |
Guido van Rossum | e797ec1 | 2001-01-17 15:24:28 +0000 | [diff] [blame] | 429 | */ |
| 430 | static int |
| 431 | try_rich_to_3way_compare(PyObject *v, PyObject *w) |
| 432 | { |
Guido van Rossum | 2ffbf6b | 2001-01-17 21:27:02 +0000 | [diff] [blame] | 433 | static struct { int op; int outcome; } tries[3] = { |
| 434 | /* Try this operator, and if it is true, use this outcome: */ |
| 435 | {Py_EQ, 0}, |
| 436 | {Py_LT, -1}, |
| 437 | {Py_GT, 1}, |
| 438 | }; |
| 439 | int i; |
| 440 | |
Guido van Rossum | d1f06b9 | 2001-01-24 22:14:43 +0000 | [diff] [blame] | 441 | if (RICHCOMPARE(v->ob_type) == NULL && RICHCOMPARE(w->ob_type) == NULL) |
Guido van Rossum | e797ec1 | 2001-01-17 15:24:28 +0000 | [diff] [blame] | 442 | return 2; /* Shortcut */ |
Guido van Rossum | 2ffbf6b | 2001-01-17 21:27:02 +0000 | [diff] [blame] | 443 | |
| 444 | for (i = 0; i < 3; i++) { |
| 445 | switch (try_rich_compare_bool(v, w, tries[i].op)) { |
| 446 | case -1: |
Tim Peters | 6d60b2e | 2001-05-07 20:53:51 +0000 | [diff] [blame] | 447 | return -2; |
Guido van Rossum | 2ffbf6b | 2001-01-17 21:27:02 +0000 | [diff] [blame] | 448 | case 1: |
| 449 | return tries[i].outcome; |
| 450 | } |
Guido van Rossum | e797ec1 | 2001-01-17 15:24:28 +0000 | [diff] [blame] | 451 | } |
Guido van Rossum | 2ffbf6b | 2001-01-17 21:27:02 +0000 | [diff] [blame] | 452 | |
| 453 | return 2; |
Guido van Rossum | e797ec1 | 2001-01-17 15:24:28 +0000 | [diff] [blame] | 454 | } |
| 455 | |
| 456 | /* Try a 3-way comparison, returning an int. Return: |
| 457 | -2 for an exception; |
Tim Peters | c99213f | 2001-11-04 05:57:16 +0000 | [diff] [blame] | 458 | -1 if v < w; |
| 459 | 0 if v == w; |
| 460 | 1 if v > w; |
| 461 | 2 if this particular 3-way comparison is not implemented or undefined. |
Guido van Rossum | e797ec1 | 2001-01-17 15:24:28 +0000 | [diff] [blame] | 462 | */ |
| 463 | static int |
| 464 | try_3way_compare(PyObject *v, PyObject *w) |
Neil Schemenauer | 5ed85ec | 2001-01-04 01:48:10 +0000 | [diff] [blame] | 465 | { |
| 466 | int c; |
Guido van Rossum | e797ec1 | 2001-01-17 15:24:28 +0000 | [diff] [blame] | 467 | cmpfunc f; |
| 468 | |
| 469 | /* Comparisons involving instances are given to instance_compare, |
| 470 | which has the same return conventions as this function. */ |
| 471 | |
Guido van Rossum | ab3b034 | 2001-09-18 20:38:53 +0000 | [diff] [blame] | 472 | f = v->ob_type->tp_compare; |
Guido van Rossum | e797ec1 | 2001-01-17 15:24:28 +0000 | [diff] [blame] | 473 | if (PyInstance_Check(v)) |
Guido van Rossum | ab3b034 | 2001-09-18 20:38:53 +0000 | [diff] [blame] | 474 | return (*f)(v, w); |
Guido van Rossum | e797ec1 | 2001-01-17 15:24:28 +0000 | [diff] [blame] | 475 | if (PyInstance_Check(w)) |
| 476 | return (*w->ob_type->tp_compare)(v, w); |
| 477 | |
Guido van Rossum | ab3b034 | 2001-09-18 20:38:53 +0000 | [diff] [blame] | 478 | /* If both have the same (non-NULL) tp_compare, use it. */ |
| 479 | if (f != NULL && f == w->ob_type->tp_compare) { |
| 480 | c = (*f)(v, w); |
| 481 | if (c < 0 && PyErr_Occurred()) |
| 482 | return -1; |
| 483 | return c < 0 ? -1 : c > 0 ? 1 : 0; |
| 484 | } |
| 485 | |
| 486 | /* If either tp_compare is _PyObject_SlotCompare, that's safe. */ |
| 487 | if (f == _PyObject_SlotCompare || |
| 488 | w->ob_type->tp_compare == _PyObject_SlotCompare) |
| 489 | return _PyObject_SlotCompare(v, w); |
| 490 | |
Guido van Rossum | e797ec1 | 2001-01-17 15:24:28 +0000 | [diff] [blame] | 491 | /* Try coercion; if it fails, give up */ |
| 492 | c = PyNumber_CoerceEx(&v, &w); |
| 493 | if (c < 0) |
| 494 | return -2; |
| 495 | if (c > 0) |
| 496 | return 2; |
| 497 | |
| 498 | /* Try v's comparison, if defined */ |
| 499 | if ((f = v->ob_type->tp_compare) != NULL) { |
| 500 | c = (*f)(v, w); |
| 501 | Py_DECREF(v); |
| 502 | Py_DECREF(w); |
Martin v. Löwis | 0163d6d | 2001-06-09 07:34:05 +0000 | [diff] [blame] | 503 | if (c < 0 && PyErr_Occurred()) |
Guido van Rossum | e797ec1 | 2001-01-17 15:24:28 +0000 | [diff] [blame] | 504 | return -2; |
| 505 | return c < 0 ? -1 : c > 0 ? 1 : 0; |
| 506 | } |
| 507 | |
| 508 | /* Try w's comparison, if defined */ |
| 509 | if ((f = w->ob_type->tp_compare) != NULL) { |
| 510 | c = (*f)(w, v); /* swapped! */ |
| 511 | Py_DECREF(v); |
| 512 | Py_DECREF(w); |
Martin v. Löwis | 0163d6d | 2001-06-09 07:34:05 +0000 | [diff] [blame] | 513 | if (c < 0 && PyErr_Occurred()) |
Guido van Rossum | e797ec1 | 2001-01-17 15:24:28 +0000 | [diff] [blame] | 514 | return -2; |
| 515 | return c < 0 ? 1 : c > 0 ? -1 : 0; /* negated! */ |
| 516 | } |
| 517 | |
| 518 | /* No comparison defined */ |
| 519 | Py_DECREF(v); |
| 520 | Py_DECREF(w); |
| 521 | return 2; |
Neil Schemenauer | 5ed85ec | 2001-01-04 01:48:10 +0000 | [diff] [blame] | 522 | } |
| 523 | |
Guido van Rossum | e797ec1 | 2001-01-17 15:24:28 +0000 | [diff] [blame] | 524 | /* Final fallback 3-way comparison, returning an int. Return: |
| 525 | -2 if an error occurred; |
Tim Peters | c99213f | 2001-11-04 05:57:16 +0000 | [diff] [blame] | 526 | -1 if v < w; |
| 527 | 0 if v == w; |
| 528 | 1 if v > w. |
Guido van Rossum | e797ec1 | 2001-01-17 15:24:28 +0000 | [diff] [blame] | 529 | */ |
| 530 | static int |
| 531 | default_3way_compare(PyObject *v, PyObject *w) |
| 532 | { |
| 533 | int c; |
Guido van Rossum | 8f9143d | 2001-01-22 15:59:32 +0000 | [diff] [blame] | 534 | char *vname, *wname; |
Guido van Rossum | e797ec1 | 2001-01-17 15:24:28 +0000 | [diff] [blame] | 535 | |
| 536 | if (v->ob_type == w->ob_type) { |
Barry Warsaw | b0e754d | 2001-01-20 06:24:55 +0000 | [diff] [blame] | 537 | /* When comparing these pointers, they must be cast to |
| 538 | * integer types (i.e. Py_uintptr_t, our spelling of C9X's |
| 539 | * uintptr_t). ANSI specifies that pointer compares other |
| 540 | * than == and != to non-related structures are undefined. |
| 541 | */ |
Barry Warsaw | 71ff8d5 | 2001-01-20 06:08:10 +0000 | [diff] [blame] | 542 | Py_uintptr_t vv = (Py_uintptr_t)v; |
| 543 | Py_uintptr_t ww = (Py_uintptr_t)w; |
Guido van Rossum | e797ec1 | 2001-01-17 15:24:28 +0000 | [diff] [blame] | 544 | return (vv < ww) ? -1 : (vv > ww) ? 1 : 0; |
| 545 | } |
| 546 | |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 547 | #ifdef Py_USING_UNICODE |
Guido van Rossum | e797ec1 | 2001-01-17 15:24:28 +0000 | [diff] [blame] | 548 | /* Special case for Unicode */ |
| 549 | if (PyUnicode_Check(v) || PyUnicode_Check(w)) { |
| 550 | c = PyUnicode_Compare(v, w); |
| 551 | if (!PyErr_Occurred()) |
| 552 | return c; |
| 553 | /* TypeErrors are ignored: if Unicode coercion fails due |
| 554 | to one of the arguments not having the right type, we |
| 555 | continue as defined by the coercion protocol (see |
| 556 | above). Luckily, decoding errors are reported as |
| 557 | ValueErrors and are not masked by this technique. */ |
| 558 | if (!PyErr_ExceptionMatches(PyExc_TypeError)) |
| 559 | return -2; |
| 560 | PyErr_Clear(); |
| 561 | } |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 562 | #endif |
Guido van Rossum | e797ec1 | 2001-01-17 15:24:28 +0000 | [diff] [blame] | 563 | |
Guido van Rossum | 0871e93 | 2001-01-22 19:28:09 +0000 | [diff] [blame] | 564 | /* None is smaller than anything */ |
| 565 | if (v == Py_None) |
| 566 | return -1; |
| 567 | if (w == Py_None) |
| 568 | return 1; |
| 569 | |
Guido van Rossum | e797ec1 | 2001-01-17 15:24:28 +0000 | [diff] [blame] | 570 | /* different type: compare type names */ |
Guido van Rossum | 8f9143d | 2001-01-22 15:59:32 +0000 | [diff] [blame] | 571 | if (v->ob_type->tp_as_number) |
| 572 | vname = ""; |
| 573 | else |
| 574 | vname = v->ob_type->tp_name; |
| 575 | if (w->ob_type->tp_as_number) |
| 576 | wname = ""; |
| 577 | else |
| 578 | wname = w->ob_type->tp_name; |
| 579 | c = strcmp(vname, wname); |
| 580 | if (c < 0) |
| 581 | return -1; |
| 582 | if (c > 0) |
| 583 | return 1; |
| 584 | /* Same type name, or (more likely) incomparable numeric types */ |
| 585 | return ((Py_uintptr_t)(v->ob_type) < ( |
| 586 | Py_uintptr_t)(w->ob_type)) ? -1 : 1; |
Guido van Rossum | e797ec1 | 2001-01-17 15:24:28 +0000 | [diff] [blame] | 587 | } |
| 588 | |
| 589 | #define CHECK_TYPES(o) PyType_HasFeature((o)->ob_type, Py_TPFLAGS_CHECKTYPES) |
| 590 | |
Tim Peters | 6d60b2e | 2001-05-07 20:53:51 +0000 | [diff] [blame] | 591 | /* Do a 3-way comparison, by hook or by crook. Return: |
| 592 | -2 for an exception; |
Tim Peters | c99213f | 2001-11-04 05:57:16 +0000 | [diff] [blame] | 593 | -1 if v < w; |
Tim Peters | 6d60b2e | 2001-05-07 20:53:51 +0000 | [diff] [blame] | 594 | 0 if v == w; |
Tim Peters | c99213f | 2001-11-04 05:57:16 +0000 | [diff] [blame] | 595 | 1 if v > w; |
Martin v. Löwis | 0163d6d | 2001-06-09 07:34:05 +0000 | [diff] [blame] | 596 | If the object implements a tp_compare function, it returns |
| 597 | whatever this function returns (whether with an exception or not). |
Tim Peters | 6d60b2e | 2001-05-07 20:53:51 +0000 | [diff] [blame] | 598 | */ |
Neil Schemenauer | 5ed85ec | 2001-01-04 01:48:10 +0000 | [diff] [blame] | 599 | static int |
Fred Drake | 100814d | 2000-07-09 15:48:49 +0000 | [diff] [blame] | 600 | do_cmp(PyObject *v, PyObject *w) |
Guido van Rossum | 2056684 | 1995-01-12 11:26:10 +0000 | [diff] [blame] | 601 | { |
Neil Schemenauer | 5ed85ec | 2001-01-04 01:48:10 +0000 | [diff] [blame] | 602 | int c; |
Martin v. Löwis | 0163d6d | 2001-06-09 07:34:05 +0000 | [diff] [blame] | 603 | cmpfunc f; |
Neil Schemenauer | 5ed85ec | 2001-01-04 01:48:10 +0000 | [diff] [blame] | 604 | |
Martin v. Löwis | 0163d6d | 2001-06-09 07:34:05 +0000 | [diff] [blame] | 605 | if (v->ob_type == w->ob_type |
Guido van Rossum | 82fc51c1 | 2001-08-16 08:02:45 +0000 | [diff] [blame] | 606 | && (f = v->ob_type->tp_compare) != NULL) { |
| 607 | c = (*f)(v, w); |
| 608 | if (c != 2 || !PyInstance_Check(v)) |
| 609 | return c; |
| 610 | } |
Guido van Rossum | e797ec1 | 2001-01-17 15:24:28 +0000 | [diff] [blame] | 611 | c = try_rich_to_3way_compare(v, w); |
| 612 | if (c < 2) |
Neil Schemenauer | 5ed85ec | 2001-01-04 01:48:10 +0000 | [diff] [blame] | 613 | return c; |
Guido van Rossum | e797ec1 | 2001-01-17 15:24:28 +0000 | [diff] [blame] | 614 | c = try_3way_compare(v, w); |
| 615 | if (c < 2) |
| 616 | return c; |
| 617 | return default_3way_compare(v, w); |
Guido van Rossum | 2056684 | 1995-01-12 11:26:10 +0000 | [diff] [blame] | 618 | } |
| 619 | |
Guido van Rossum | 2ffbf6b | 2001-01-17 21:27:02 +0000 | [diff] [blame] | 620 | /* compare_nesting is incremented before calling compare (for |
Jeremy Hylton | 4a3dd2d | 2000-04-14 19:13:24 +0000 | [diff] [blame] | 621 | some types) and decremented on exit. If the count exceeds the |
| 622 | nesting limit, enable code to detect circular data structures. |
Guido van Rossum | a3af41d | 2001-01-18 22:07:06 +0000 | [diff] [blame] | 623 | |
| 624 | This is a tunable parameter that should only affect the performance |
| 625 | of comparisons, nothing else. Setting it high makes comparing deeply |
| 626 | nested non-cyclical data structures faster, but makes comparing cyclical |
| 627 | data structures slower. |
Jeremy Hylton | 4a3dd2d | 2000-04-14 19:13:24 +0000 | [diff] [blame] | 628 | */ |
Guido van Rossum | a3af41d | 2001-01-18 22:07:06 +0000 | [diff] [blame] | 629 | #define NESTING_LIMIT 20 |
| 630 | |
Guido van Rossum | 2ffbf6b | 2001-01-17 21:27:02 +0000 | [diff] [blame] | 631 | static int compare_nesting = 0; |
Jeremy Hylton | 4a3dd2d | 2000-04-14 19:13:24 +0000 | [diff] [blame] | 632 | |
| 633 | static PyObject* |
Fred Drake | 100814d | 2000-07-09 15:48:49 +0000 | [diff] [blame] | 634 | get_inprogress_dict(void) |
Jeremy Hylton | 4a3dd2d | 2000-04-14 19:13:24 +0000 | [diff] [blame] | 635 | { |
Guido van Rossum | 2ffbf6b | 2001-01-17 21:27:02 +0000 | [diff] [blame] | 636 | static PyObject *key; |
Jeremy Hylton | 4a3dd2d | 2000-04-14 19:13:24 +0000 | [diff] [blame] | 637 | PyObject *tstate_dict, *inprogress; |
| 638 | |
Guido van Rossum | 2ffbf6b | 2001-01-17 21:27:02 +0000 | [diff] [blame] | 639 | if (key == NULL) { |
| 640 | key = PyString_InternFromString("cmp_state"); |
| 641 | if (key == NULL) |
| 642 | return NULL; |
| 643 | } |
| 644 | |
Jeremy Hylton | 4a3dd2d | 2000-04-14 19:13:24 +0000 | [diff] [blame] | 645 | tstate_dict = PyThreadState_GetDict(); |
| 646 | if (tstate_dict == NULL) { |
| 647 | PyErr_BadInternalCall(); |
| 648 | return NULL; |
| 649 | } |
Guido van Rossum | a3af41d | 2001-01-18 22:07:06 +0000 | [diff] [blame] | 650 | |
Guido van Rossum | 2ffbf6b | 2001-01-17 21:27:02 +0000 | [diff] [blame] | 651 | inprogress = PyDict_GetItem(tstate_dict, key); |
Jeremy Hylton | 4a3dd2d | 2000-04-14 19:13:24 +0000 | [diff] [blame] | 652 | if (inprogress == NULL) { |
Jeremy Hylton | 4a3dd2d | 2000-04-14 19:13:24 +0000 | [diff] [blame] | 653 | inprogress = PyDict_New(); |
| 654 | if (inprogress == NULL) |
| 655 | return NULL; |
Guido van Rossum | 2ffbf6b | 2001-01-17 21:27:02 +0000 | [diff] [blame] | 656 | if (PyDict_SetItem(tstate_dict, key, inprogress) == -1) { |
Jeremy Hylton | 4a3dd2d | 2000-04-14 19:13:24 +0000 | [diff] [blame] | 657 | Py_DECREF(inprogress); |
| 658 | return NULL; |
| 659 | } |
Jeremy Hylton | a251ea0 | 2000-06-09 16:20:39 +0000 | [diff] [blame] | 660 | Py_DECREF(inprogress); |
Jeremy Hylton | 4a3dd2d | 2000-04-14 19:13:24 +0000 | [diff] [blame] | 661 | } |
Guido van Rossum | a3af41d | 2001-01-18 22:07:06 +0000 | [diff] [blame] | 662 | |
Jeremy Hylton | 4a3dd2d | 2000-04-14 19:13:24 +0000 | [diff] [blame] | 663 | return inprogress; |
| 664 | } |
| 665 | |
| 666 | static PyObject * |
Guido van Rossum | a3af41d | 2001-01-18 22:07:06 +0000 | [diff] [blame] | 667 | check_recursion(PyObject *v, PyObject *w, int op) |
Jeremy Hylton | 4a3dd2d | 2000-04-14 19:13:24 +0000 | [diff] [blame] | 668 | { |
Guido van Rossum | a3af41d | 2001-01-18 22:07:06 +0000 | [diff] [blame] | 669 | PyObject *inprogress; |
| 670 | PyObject *token; |
Barry Warsaw | 9d23a4e | 2000-08-18 05:01:19 +0000 | [diff] [blame] | 671 | Py_uintptr_t iv = (Py_uintptr_t)v; |
| 672 | Py_uintptr_t iw = (Py_uintptr_t)w; |
Guido van Rossum | a3af41d | 2001-01-18 22:07:06 +0000 | [diff] [blame] | 673 | PyObject *x, *y, *z; |
Jeremy Hylton | 4a3dd2d | 2000-04-14 19:13:24 +0000 | [diff] [blame] | 674 | |
Guido van Rossum | a3af41d | 2001-01-18 22:07:06 +0000 | [diff] [blame] | 675 | inprogress = get_inprogress_dict(); |
| 676 | if (inprogress == NULL) |
| 677 | return NULL; |
| 678 | |
| 679 | token = PyTuple_New(3); |
| 680 | if (token == NULL) |
| 681 | return NULL; |
| 682 | |
| 683 | if (iv <= iw) { |
| 684 | PyTuple_SET_ITEM(token, 0, x = PyLong_FromVoidPtr((void *)v)); |
| 685 | PyTuple_SET_ITEM(token, 1, y = PyLong_FromVoidPtr((void *)w)); |
| 686 | if (op >= 0) |
| 687 | op = swapped_op[op]; |
| 688 | } else { |
| 689 | PyTuple_SET_ITEM(token, 0, x = PyLong_FromVoidPtr((void *)w)); |
| 690 | PyTuple_SET_ITEM(token, 1, y = PyLong_FromVoidPtr((void *)v)); |
| 691 | } |
| 692 | PyTuple_SET_ITEM(token, 2, z = PyInt_FromLong((long)op)); |
| 693 | if (x == NULL || y == NULL || z == NULL) { |
| 694 | Py_DECREF(token); |
Jeremy Hylton | 4a3dd2d | 2000-04-14 19:13:24 +0000 | [diff] [blame] | 695 | return NULL; |
| 696 | } |
Guido van Rossum | a3af41d | 2001-01-18 22:07:06 +0000 | [diff] [blame] | 697 | |
| 698 | if (PyDict_GetItem(inprogress, token) != NULL) { |
| 699 | Py_DECREF(token); |
| 700 | return Py_None; /* Without INCREF! */ |
Jeremy Hylton | 4a3dd2d | 2000-04-14 19:13:24 +0000 | [diff] [blame] | 701 | } |
Guido van Rossum | a3af41d | 2001-01-18 22:07:06 +0000 | [diff] [blame] | 702 | |
| 703 | if (PyDict_SetItem(inprogress, token, token) < 0) { |
| 704 | Py_DECREF(token); |
| 705 | return NULL; |
| 706 | } |
| 707 | |
| 708 | return token; |
| 709 | } |
| 710 | |
| 711 | static void |
| 712 | delete_token(PyObject *token) |
| 713 | { |
| 714 | PyObject *inprogress; |
| 715 | |
| 716 | if (token == NULL || token == Py_None) |
| 717 | return; |
| 718 | inprogress = get_inprogress_dict(); |
| 719 | if (inprogress == NULL) |
| 720 | PyErr_Clear(); |
| 721 | else |
| 722 | PyDict_DelItem(inprogress, token); |
| 723 | Py_DECREF(token); |
Jeremy Hylton | 4a3dd2d | 2000-04-14 19:13:24 +0000 | [diff] [blame] | 724 | } |
| 725 | |
Tim Peters | c99213f | 2001-11-04 05:57:16 +0000 | [diff] [blame] | 726 | /* Compare v to w. Return |
| 727 | -1 if v < w or exception (PyErr_Occurred() true in latter case). |
| 728 | 0 if v == w. |
| 729 | 1 if v > w. |
| 730 | XXX The docs (C API manual) say the return value is undefined in case |
| 731 | XXX of error. |
| 732 | */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 733 | int |
Fred Drake | 100814d | 2000-07-09 15:48:49 +0000 | [diff] [blame] | 734 | PyObject_Compare(PyObject *v, PyObject *w) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 735 | { |
Guido van Rossum | a3af41d | 2001-01-18 22:07:06 +0000 | [diff] [blame] | 736 | PyTypeObject *vtp; |
Jeremy Hylton | 4a3dd2d | 2000-04-14 19:13:24 +0000 | [diff] [blame] | 737 | int result; |
| 738 | |
Jack Jansen | d49cbe1 | 2000-08-22 21:52:51 +0000 | [diff] [blame] | 739 | #if defined(USE_STACKCHECK) |
Guido van Rossum | 9c0a99e | 2000-08-30 15:53:50 +0000 | [diff] [blame] | 740 | if (PyOS_CheckStack()) { |
Jack Jansen | d49cbe1 | 2000-08-22 21:52:51 +0000 | [diff] [blame] | 741 | PyErr_SetString(PyExc_MemoryError, "Stack overflow"); |
Jeremy Hylton | 39a362d | 2001-10-22 16:30:36 +0000 | [diff] [blame] | 742 | return -1; |
Jack Jansen | d49cbe1 | 2000-08-22 21:52:51 +0000 | [diff] [blame] | 743 | } |
| 744 | #endif |
Guido van Rossum | c8b6df9 | 1997-05-23 00:06:51 +0000 | [diff] [blame] | 745 | if (v == NULL || w == NULL) { |
| 746 | PyErr_BadInternalCall(); |
| 747 | return -1; |
| 748 | } |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 749 | if (v == w) |
| 750 | return 0; |
Neil Schemenauer | 5ed85ec | 2001-01-04 01:48:10 +0000 | [diff] [blame] | 751 | vtp = v->ob_type; |
Guido van Rossum | 2ffbf6b | 2001-01-17 21:27:02 +0000 | [diff] [blame] | 752 | compare_nesting++; |
| 753 | if (compare_nesting > NESTING_LIMIT && |
Neil Schemenauer | 5ed85ec | 2001-01-04 01:48:10 +0000 | [diff] [blame] | 754 | (vtp->tp_as_mapping |
Guido van Rossum | a3af41d | 2001-01-18 22:07:06 +0000 | [diff] [blame] | 755 | || (vtp->tp_as_sequence |
| 756 | && !PyString_Check(v) |
| 757 | && !PyTuple_Check(v)))) { |
Neil Schemenauer | 5ed85ec | 2001-01-04 01:48:10 +0000 | [diff] [blame] | 758 | /* try to detect circular data structures */ |
Guido van Rossum | a3af41d | 2001-01-18 22:07:06 +0000 | [diff] [blame] | 759 | PyObject *token = check_recursion(v, w, -1); |
Jeremy Hylton | 4a3dd2d | 2000-04-14 19:13:24 +0000 | [diff] [blame] | 760 | |
Guido van Rossum | a3af41d | 2001-01-18 22:07:06 +0000 | [diff] [blame] | 761 | if (token == NULL) { |
| 762 | result = -1; |
Jeremy Hylton | 4a3dd2d | 2000-04-14 19:13:24 +0000 | [diff] [blame] | 763 | } |
Guido van Rossum | a3af41d | 2001-01-18 22:07:06 +0000 | [diff] [blame] | 764 | else if (token == Py_None) { |
Jeremy Hylton | 4a3dd2d | 2000-04-14 19:13:24 +0000 | [diff] [blame] | 765 | /* already comparing these objects. assume |
| 766 | they're equal until shown otherwise */ |
Neil Schemenauer | 5ed85ec | 2001-01-04 01:48:10 +0000 | [diff] [blame] | 767 | result = 0; |
Jeremy Hylton | 4a3dd2d | 2000-04-14 19:13:24 +0000 | [diff] [blame] | 768 | } |
Guido van Rossum | a3af41d | 2001-01-18 22:07:06 +0000 | [diff] [blame] | 769 | else { |
| 770 | result = do_cmp(v, w); |
| 771 | delete_token(token); |
Jeremy Hylton | 4a3dd2d | 2000-04-14 19:13:24 +0000 | [diff] [blame] | 772 | } |
Jeremy Hylton | 4a3dd2d | 2000-04-14 19:13:24 +0000 | [diff] [blame] | 773 | } |
Neil Schemenauer | 5ed85ec | 2001-01-04 01:48:10 +0000 | [diff] [blame] | 774 | else { |
| 775 | result = do_cmp(v, w); |
| 776 | } |
Guido van Rossum | 2ffbf6b | 2001-01-17 21:27:02 +0000 | [diff] [blame] | 777 | compare_nesting--; |
Guido van Rossum | e797ec1 | 2001-01-17 15:24:28 +0000 | [diff] [blame] | 778 | return result < 0 ? -1 : result; |
| 779 | } |
| 780 | |
Tim Peters | c99213f | 2001-11-04 05:57:16 +0000 | [diff] [blame] | 781 | /* Return (new reference to) Py_True or Py_False. */ |
Guido van Rossum | e797ec1 | 2001-01-17 15:24:28 +0000 | [diff] [blame] | 782 | static PyObject * |
Martin v. Löwis | 0163d6d | 2001-06-09 07:34:05 +0000 | [diff] [blame] | 783 | convert_3way_to_object(int op, int c) |
Guido van Rossum | e797ec1 | 2001-01-17 15:24:28 +0000 | [diff] [blame] | 784 | { |
Guido van Rossum | e797ec1 | 2001-01-17 15:24:28 +0000 | [diff] [blame] | 785 | PyObject *result; |
Guido van Rossum | e797ec1 | 2001-01-17 15:24:28 +0000 | [diff] [blame] | 786 | switch (op) { |
| 787 | case Py_LT: c = c < 0; break; |
| 788 | case Py_LE: c = c <= 0; break; |
| 789 | case Py_EQ: c = c == 0; break; |
| 790 | case Py_NE: c = c != 0; break; |
| 791 | case Py_GT: c = c > 0; break; |
| 792 | case Py_GE: c = c >= 0; break; |
| 793 | } |
| 794 | result = c ? Py_True : Py_False; |
| 795 | Py_INCREF(result); |
Jeremy Hylton | 4a3dd2d | 2000-04-14 19:13:24 +0000 | [diff] [blame] | 796 | return result; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 797 | } |
Martin v. Löwis | 0163d6d | 2001-06-09 07:34:05 +0000 | [diff] [blame] | 798 | |
Tim Peters | c99213f | 2001-11-04 05:57:16 +0000 | [diff] [blame] | 799 | /* We want a rich comparison but don't have one. Try a 3-way cmp instead. |
| 800 | Return |
| 801 | NULL if error |
| 802 | Py_True if v op w |
| 803 | Py_False if not (v op w) |
| 804 | */ |
Martin v. Löwis | 0163d6d | 2001-06-09 07:34:05 +0000 | [diff] [blame] | 805 | static PyObject * |
| 806 | try_3way_to_rich_compare(PyObject *v, PyObject *w, int op) |
| 807 | { |
| 808 | int c; |
| 809 | |
| 810 | c = try_3way_compare(v, w); |
| 811 | if (c >= 2) |
| 812 | c = default_3way_compare(v, w); |
| 813 | if (c <= -2) |
| 814 | return NULL; |
| 815 | return convert_3way_to_object(op, c); |
| 816 | } |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 817 | |
Tim Peters | c99213f | 2001-11-04 05:57:16 +0000 | [diff] [blame] | 818 | /* Do rich comparison on v and w. Return |
| 819 | NULL if error |
| 820 | Else a new reference to an object other than Py_NotImplemented, usually(?): |
| 821 | Py_True if v op w |
| 822 | Py_False if not (v op w) |
| 823 | */ |
Neil Schemenauer | d38855c | 2001-01-21 16:25:18 +0000 | [diff] [blame] | 824 | static PyObject * |
Guido van Rossum | e797ec1 | 2001-01-17 15:24:28 +0000 | [diff] [blame] | 825 | do_richcmp(PyObject *v, PyObject *w, int op) |
| 826 | { |
| 827 | PyObject *res; |
| 828 | |
| 829 | res = try_rich_compare(v, w, op); |
| 830 | if (res != Py_NotImplemented) |
| 831 | return res; |
| 832 | Py_DECREF(res); |
| 833 | |
| 834 | return try_3way_to_rich_compare(v, w, op); |
| 835 | } |
| 836 | |
Tim Peters | c99213f | 2001-11-04 05:57:16 +0000 | [diff] [blame] | 837 | /* Return: |
| 838 | NULL for exception; |
Tim Peters | c99213f | 2001-11-04 05:57:16 +0000 | [diff] [blame] | 839 | some object not equal to NotImplemented if it is implemented |
| 840 | (this latter object may not be a Boolean). |
| 841 | */ |
Guido van Rossum | e797ec1 | 2001-01-17 15:24:28 +0000 | [diff] [blame] | 842 | PyObject * |
| 843 | PyObject_RichCompare(PyObject *v, PyObject *w, int op) |
| 844 | { |
| 845 | PyObject *res; |
| 846 | |
| 847 | assert(Py_LT <= op && op <= Py_GE); |
| 848 | |
Guido van Rossum | 2ffbf6b | 2001-01-17 21:27:02 +0000 | [diff] [blame] | 849 | compare_nesting++; |
| 850 | if (compare_nesting > NESTING_LIMIT && |
| 851 | (v->ob_type->tp_as_mapping |
Guido van Rossum | a3af41d | 2001-01-18 22:07:06 +0000 | [diff] [blame] | 852 | || (v->ob_type->tp_as_sequence |
| 853 | && !PyString_Check(v) |
| 854 | && !PyTuple_Check(v)))) { |
Tim Peters | 67754e9 | 2001-11-04 07:29:31 +0000 | [diff] [blame] | 855 | |
Guido van Rossum | 2ffbf6b | 2001-01-17 21:27:02 +0000 | [diff] [blame] | 856 | /* try to detect circular data structures */ |
Guido van Rossum | a3af41d | 2001-01-18 22:07:06 +0000 | [diff] [blame] | 857 | PyObject *token = check_recursion(v, w, op); |
Guido van Rossum | a3af41d | 2001-01-18 22:07:06 +0000 | [diff] [blame] | 858 | if (token == NULL) { |
| 859 | res = NULL; |
Tim Peters | 67754e9 | 2001-11-04 07:29:31 +0000 | [diff] [blame] | 860 | goto Done; |
Guido van Rossum | e797ec1 | 2001-01-17 15:24:28 +0000 | [diff] [blame] | 861 | } |
Guido van Rossum | a3af41d | 2001-01-18 22:07:06 +0000 | [diff] [blame] | 862 | else if (token == Py_None) { |
| 863 | /* already comparing these objects with this operator. |
| 864 | assume they're equal until shown otherwise */ |
| 865 | if (op == Py_EQ) |
Guido van Rossum | 2ffbf6b | 2001-01-17 21:27:02 +0000 | [diff] [blame] | 866 | res = Py_True; |
Guido van Rossum | a3af41d | 2001-01-18 22:07:06 +0000 | [diff] [blame] | 867 | else if (op == Py_NE) |
Guido van Rossum | 2ffbf6b | 2001-01-17 21:27:02 +0000 | [diff] [blame] | 868 | res = Py_False; |
Guido van Rossum | a3af41d | 2001-01-18 22:07:06 +0000 | [diff] [blame] | 869 | else { |
| 870 | PyErr_SetString(PyExc_ValueError, |
| 871 | "can't order recursive values"); |
| 872 | res = NULL; |
| 873 | } |
| 874 | Py_XINCREF(res); |
Guido van Rossum | 2ffbf6b | 2001-01-17 21:27:02 +0000 | [diff] [blame] | 875 | } |
Guido van Rossum | a3af41d | 2001-01-18 22:07:06 +0000 | [diff] [blame] | 876 | else { |
| 877 | res = do_richcmp(v, w, op); |
| 878 | delete_token(token); |
Guido van Rossum | 2ffbf6b | 2001-01-17 21:27:02 +0000 | [diff] [blame] | 879 | } |
Tim Peters | 67754e9 | 2001-11-04 07:29:31 +0000 | [diff] [blame] | 880 | goto Done; |
Guido van Rossum | e797ec1 | 2001-01-17 15:24:28 +0000 | [diff] [blame] | 881 | } |
Tim Peters | 67754e9 | 2001-11-04 07:29:31 +0000 | [diff] [blame] | 882 | |
| 883 | /* No nesting extremism. |
| 884 | If the types are equal, and not old-style instances, try to |
| 885 | get out cheap (don't bother with coercions etc.). */ |
| 886 | if (v->ob_type == w->ob_type && !PyInstance_Check(v)) { |
| 887 | cmpfunc fcmp; |
| 888 | richcmpfunc frich = RICHCOMPARE(v->ob_type); |
| 889 | /* If the type has richcmp, try it first. try_rich_compare |
| 890 | tries it two-sided, which is not needed since we've a |
| 891 | single type only. */ |
| 892 | if (frich != NULL) { |
| 893 | res = (*frich)(v, w, op); |
| 894 | if (res != Py_NotImplemented) |
| 895 | goto Done; |
| 896 | Py_DECREF(res); |
| 897 | } |
| 898 | /* No richcmp, or this particular richmp not implemented. |
| 899 | Try 3-way cmp. */ |
| 900 | fcmp = v->ob_type->tp_compare; |
| 901 | if (fcmp != NULL) { |
| 902 | int c = (*fcmp)(v, w); |
| 903 | if (c < 0 && PyErr_Occurred()) { |
| 904 | res = NULL; |
| 905 | goto Done; |
| 906 | } |
| 907 | res = convert_3way_to_object(op, c); |
| 908 | goto Done; |
| 909 | } |
Guido van Rossum | 2ffbf6b | 2001-01-17 21:27:02 +0000 | [diff] [blame] | 910 | } |
Tim Peters | 67754e9 | 2001-11-04 07:29:31 +0000 | [diff] [blame] | 911 | |
| 912 | /* Fast path not taken, or couldn't deliver a useful result. */ |
| 913 | res = do_richcmp(v, w, op); |
| 914 | Done: |
Guido van Rossum | 2ffbf6b | 2001-01-17 21:27:02 +0000 | [diff] [blame] | 915 | compare_nesting--; |
Guido van Rossum | e797ec1 | 2001-01-17 15:24:28 +0000 | [diff] [blame] | 916 | return res; |
| 917 | } |
| 918 | |
Tim Peters | de9725f | 2001-05-05 10:06:17 +0000 | [diff] [blame] | 919 | /* Return -1 if error; 1 if v op w; 0 if not (v op w). */ |
Guido van Rossum | e797ec1 | 2001-01-17 15:24:28 +0000 | [diff] [blame] | 920 | int |
| 921 | PyObject_RichCompareBool(PyObject *v, PyObject *w, int op) |
| 922 | { |
| 923 | PyObject *res = PyObject_RichCompare(v, w, op); |
| 924 | int ok; |
| 925 | |
| 926 | if (res == NULL) |
| 927 | return -1; |
| 928 | ok = PyObject_IsTrue(res); |
| 929 | Py_DECREF(res); |
| 930 | return ok; |
| 931 | } |
Fred Drake | 13634cf | 2000-06-29 19:17:04 +0000 | [diff] [blame] | 932 | |
| 933 | /* Set of hash utility functions to help maintaining the invariant that |
| 934 | iff a==b then hash(a)==hash(b) |
| 935 | |
| 936 | All the utility functions (_Py_Hash*()) return "-1" to signify an error. |
| 937 | */ |
| 938 | |
| 939 | long |
Fred Drake | 100814d | 2000-07-09 15:48:49 +0000 | [diff] [blame] | 940 | _Py_HashDouble(double v) |
Fred Drake | 13634cf | 2000-06-29 19:17:04 +0000 | [diff] [blame] | 941 | { |
Tim Peters | 39dce29 | 2000-08-15 03:34:48 +0000 | [diff] [blame] | 942 | double intpart, fractpart; |
| 943 | int expo; |
| 944 | long hipart; |
| 945 | long x; /* the final hash value */ |
| 946 | /* This is designed so that Python numbers of different types |
| 947 | * that compare equal hash to the same value; otherwise comparisons |
| 948 | * of mapping keys will turn out weird. |
| 949 | */ |
| 950 | |
| 951 | #ifdef MPW /* MPW C modf expects pointer to extended as second argument */ |
| 952 | { |
| 953 | extended e; |
| 954 | fractpart = modf(v, &e); |
| 955 | intpart = e; |
| 956 | } |
| 957 | #else |
| 958 | fractpart = modf(v, &intpart); |
| 959 | #endif |
| 960 | if (fractpart == 0.0) { |
| 961 | /* This must return the same hash as an equal int or long. */ |
| 962 | if (intpart > LONG_MAX || -intpart > LONG_MAX) { |
| 963 | /* Convert to long and use its hash. */ |
| 964 | PyObject *plong; /* converted to Python long */ |
| 965 | if (Py_IS_INFINITY(intpart)) |
| 966 | /* can't convert to long int -- arbitrary */ |
| 967 | v = v < 0 ? -271828.0 : 314159.0; |
| 968 | plong = PyLong_FromDouble(v); |
| 969 | if (plong == NULL) |
| 970 | return -1; |
| 971 | x = PyObject_Hash(plong); |
| 972 | Py_DECREF(plong); |
| 973 | return x; |
| 974 | } |
| 975 | /* Fits in a C long == a Python int, so is its own hash. */ |
| 976 | x = (long)intpart; |
| 977 | if (x == -1) |
| 978 | x = -2; |
| 979 | return x; |
| 980 | } |
| 981 | /* The fractional part is non-zero, so we don't have to worry about |
| 982 | * making this match the hash of some other type. |
| 983 | * Use frexp to get at the bits in the double. |
Fred Drake | 13634cf | 2000-06-29 19:17:04 +0000 | [diff] [blame] | 984 | * Since the VAX D double format has 56 mantissa bits, which is the |
| 985 | * most of any double format in use, each of these parts may have as |
| 986 | * many as (but no more than) 56 significant bits. |
Tim Peters | 39dce29 | 2000-08-15 03:34:48 +0000 | [diff] [blame] | 987 | * So, assuming sizeof(long) >= 4, each part can be broken into two |
| 988 | * longs; frexp and multiplication are used to do that. |
| 989 | * Also, since the Cray double format has 15 exponent bits, which is |
| 990 | * the most of any double format in use, shifting the exponent field |
| 991 | * 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] | 992 | */ |
Tim Peters | 39dce29 | 2000-08-15 03:34:48 +0000 | [diff] [blame] | 993 | v = frexp(v, &expo); |
| 994 | v *= 2147483648.0; /* 2**31 */ |
| 995 | hipart = (long)v; /* take the top 32 bits */ |
| 996 | v = (v - (double)hipart) * 2147483648.0; /* get the next 32 bits */ |
| 997 | x = hipart + (long)v + (expo << 15); |
| 998 | if (x == -1) |
| 999 | x = -2; |
| 1000 | return x; |
Fred Drake | 13634cf | 2000-06-29 19:17:04 +0000 | [diff] [blame] | 1001 | } |
| 1002 | |
| 1003 | long |
Fred Drake | 100814d | 2000-07-09 15:48:49 +0000 | [diff] [blame] | 1004 | _Py_HashPointer(void *p) |
Fred Drake | 13634cf | 2000-06-29 19:17:04 +0000 | [diff] [blame] | 1005 | { |
| 1006 | #if SIZEOF_LONG >= SIZEOF_VOID_P |
| 1007 | return (long)p; |
| 1008 | #else |
| 1009 | /* convert to a Python long and hash that */ |
| 1010 | PyObject* longobj; |
| 1011 | long x; |
| 1012 | |
| 1013 | if ((longobj = PyLong_FromVoidPtr(p)) == NULL) { |
| 1014 | x = -1; |
| 1015 | goto finally; |
| 1016 | } |
| 1017 | x = PyObject_Hash(longobj); |
| 1018 | |
| 1019 | finally: |
| 1020 | Py_XDECREF(longobj); |
| 1021 | return x; |
| 1022 | #endif |
| 1023 | } |
| 1024 | |
| 1025 | |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 1026 | long |
Fred Drake | 100814d | 2000-07-09 15:48:49 +0000 | [diff] [blame] | 1027 | PyObject_Hash(PyObject *v) |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 1028 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1029 | PyTypeObject *tp = v->ob_type; |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 1030 | if (tp->tp_hash != NULL) |
| 1031 | return (*tp->tp_hash)(v); |
Guido van Rossum | d1f06b9 | 2001-01-24 22:14:43 +0000 | [diff] [blame] | 1032 | if (tp->tp_compare == NULL && RICHCOMPARE(tp) == NULL) { |
Fred Drake | 13634cf | 2000-06-29 19:17:04 +0000 | [diff] [blame] | 1033 | return _Py_HashPointer(v); /* Use address as hash value */ |
| 1034 | } |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 1035 | /* 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] | 1036 | PyErr_SetString(PyExc_TypeError, "unhashable type"); |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 1037 | return -1; |
| 1038 | } |
| 1039 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1040 | PyObject * |
Fred Drake | 100814d | 2000-07-09 15:48:49 +0000 | [diff] [blame] | 1041 | PyObject_GetAttrString(PyObject *v, char *name) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1042 | { |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1043 | PyObject *w, *res; |
Guido van Rossum | d8eb1b3 | 1996-08-09 20:52:03 +0000 | [diff] [blame] | 1044 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1045 | if (v->ob_type->tp_getattr != NULL) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1046 | return (*v->ob_type->tp_getattr)(v, name); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1047 | w = PyString_InternFromString(name); |
| 1048 | if (w == NULL) |
| 1049 | return NULL; |
| 1050 | res = PyObject_GetAttr(v, w); |
| 1051 | Py_XDECREF(w); |
| 1052 | return res; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1053 | } |
| 1054 | |
| 1055 | int |
Fred Drake | 100814d | 2000-07-09 15:48:49 +0000 | [diff] [blame] | 1056 | PyObject_HasAttrString(PyObject *v, char *name) |
Guido van Rossum | ed18fdc | 1993-07-11 19:55:34 +0000 | [diff] [blame] | 1057 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1058 | PyObject *res = PyObject_GetAttrString(v, name); |
Guido van Rossum | ed18fdc | 1993-07-11 19:55:34 +0000 | [diff] [blame] | 1059 | if (res != NULL) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1060 | Py_DECREF(res); |
Guido van Rossum | ed18fdc | 1993-07-11 19:55:34 +0000 | [diff] [blame] | 1061 | return 1; |
| 1062 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1063 | PyErr_Clear(); |
Guido van Rossum | ed18fdc | 1993-07-11 19:55:34 +0000 | [diff] [blame] | 1064 | return 0; |
| 1065 | } |
| 1066 | |
| 1067 | int |
Fred Drake | 100814d | 2000-07-09 15:48:49 +0000 | [diff] [blame] | 1068 | PyObject_SetAttrString(PyObject *v, char *name, PyObject *w) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1069 | { |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1070 | PyObject *s; |
| 1071 | int res; |
Guido van Rossum | d8eb1b3 | 1996-08-09 20:52:03 +0000 | [diff] [blame] | 1072 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1073 | if (v->ob_type->tp_setattr != NULL) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1074 | return (*v->ob_type->tp_setattr)(v, name, w); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1075 | s = PyString_InternFromString(name); |
| 1076 | if (s == NULL) |
| 1077 | return -1; |
| 1078 | res = PyObject_SetAttr(v, s, w); |
| 1079 | Py_XDECREF(s); |
| 1080 | return res; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1081 | } |
| 1082 | |
Guido van Rossum | 98ff96a | 1997-05-20 18:34:44 +0000 | [diff] [blame] | 1083 | PyObject * |
Fred Drake | 100814d | 2000-07-09 15:48:49 +0000 | [diff] [blame] | 1084 | PyObject_GetAttr(PyObject *v, PyObject *name) |
Guido van Rossum | 98ff96a | 1997-05-20 18:34:44 +0000 | [diff] [blame] | 1085 | { |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1086 | PyTypeObject *tp = v->ob_type; |
| 1087 | |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 1088 | #ifdef Py_USING_UNICODE |
Marc-André Lemburg | e44e507 | 2000-09-18 16:20:57 +0000 | [diff] [blame] | 1089 | /* The Unicode to string conversion is done here because the |
| 1090 | existing tp_getattro slots expect a string object as name |
| 1091 | and we wouldn't want to break those. */ |
| 1092 | if (PyUnicode_Check(name)) { |
| 1093 | name = _PyUnicode_AsDefaultEncodedString(name, NULL); |
| 1094 | if (name == NULL) |
| 1095 | return NULL; |
| 1096 | } |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 1097 | #endif |
| 1098 | |
Jeremy Hylton | 99a8f90 | 2000-06-23 14:36:32 +0000 | [diff] [blame] | 1099 | if (!PyString_Check(name)) { |
| 1100 | PyErr_SetString(PyExc_TypeError, |
| 1101 | "attribute name must be string"); |
| 1102 | return NULL; |
| 1103 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1104 | if (tp->tp_getattro != NULL) |
| 1105 | return (*tp->tp_getattro)(v, name); |
| 1106 | if (tp->tp_getattr != NULL) |
| 1107 | return (*tp->tp_getattr)(v, PyString_AS_STRING(name)); |
| 1108 | PyErr_Format(PyExc_AttributeError, |
| 1109 | "'%.50s' object has no attribute '%.400s'", |
| 1110 | tp->tp_name, PyString_AS_STRING(name)); |
| 1111 | return NULL; |
Guido van Rossum | 98ff96a | 1997-05-20 18:34:44 +0000 | [diff] [blame] | 1112 | } |
| 1113 | |
| 1114 | int |
Fred Drake | 100814d | 2000-07-09 15:48:49 +0000 | [diff] [blame] | 1115 | PyObject_HasAttr(PyObject *v, PyObject *name) |
Guido van Rossum | 98ff96a | 1997-05-20 18:34:44 +0000 | [diff] [blame] | 1116 | { |
| 1117 | PyObject *res = PyObject_GetAttr(v, name); |
| 1118 | if (res != NULL) { |
| 1119 | Py_DECREF(res); |
| 1120 | return 1; |
| 1121 | } |
| 1122 | PyErr_Clear(); |
| 1123 | return 0; |
| 1124 | } |
| 1125 | |
| 1126 | int |
Fred Drake | 100814d | 2000-07-09 15:48:49 +0000 | [diff] [blame] | 1127 | PyObject_SetAttr(PyObject *v, PyObject *name, PyObject *value) |
Guido van Rossum | 98ff96a | 1997-05-20 18:34:44 +0000 | [diff] [blame] | 1128 | { |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1129 | PyTypeObject *tp = v->ob_type; |
Guido van Rossum | 98ff96a | 1997-05-20 18:34:44 +0000 | [diff] [blame] | 1130 | int err; |
Marc-André Lemburg | e44e507 | 2000-09-18 16:20:57 +0000 | [diff] [blame] | 1131 | |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 1132 | #ifdef Py_USING_UNICODE |
Marc-André Lemburg | e44e507 | 2000-09-18 16:20:57 +0000 | [diff] [blame] | 1133 | /* The Unicode to string conversion is done here because the |
| 1134 | existing tp_setattro slots expect a string object as name |
| 1135 | and we wouldn't want to break those. */ |
| 1136 | if (PyUnicode_Check(name)) { |
| 1137 | name = PyUnicode_AsEncodedString(name, NULL, NULL); |
| 1138 | if (name == NULL) |
| 1139 | return -1; |
Jeremy Hylton | 99a8f90 | 2000-06-23 14:36:32 +0000 | [diff] [blame] | 1140 | } |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 1141 | else |
| 1142 | #endif |
| 1143 | if (!PyString_Check(name)){ |
Jeremy Hylton | 99a8f90 | 2000-06-23 14:36:32 +0000 | [diff] [blame] | 1144 | PyErr_SetString(PyExc_TypeError, |
| 1145 | "attribute name must be string"); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1146 | return -1; |
Jeremy Hylton | 99a8f90 | 2000-06-23 14:36:32 +0000 | [diff] [blame] | 1147 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1148 | else |
| 1149 | Py_INCREF(name); |
| 1150 | |
| 1151 | PyString_InternInPlace(&name); |
| 1152 | if (tp->tp_setattro != NULL) { |
| 1153 | err = (*tp->tp_setattro)(v, name, value); |
| 1154 | Py_DECREF(name); |
| 1155 | return err; |
Marc-André Lemburg | e44e507 | 2000-09-18 16:20:57 +0000 | [diff] [blame] | 1156 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1157 | if (tp->tp_setattr != NULL) { |
| 1158 | err = (*tp->tp_setattr)(v, PyString_AS_STRING(name), value); |
| 1159 | Py_DECREF(name); |
| 1160 | return err; |
| 1161 | } |
Guido van Rossum | 98ff96a | 1997-05-20 18:34:44 +0000 | [diff] [blame] | 1162 | Py_DECREF(name); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1163 | if (tp->tp_getattr == NULL && tp->tp_getattro == NULL) |
| 1164 | PyErr_Format(PyExc_TypeError, |
| 1165 | "'%.100s' object has no attributes " |
| 1166 | "(%s .%.100s)", |
| 1167 | tp->tp_name, |
| 1168 | value==NULL ? "del" : "assign to", |
| 1169 | PyString_AS_STRING(name)); |
| 1170 | else |
| 1171 | PyErr_Format(PyExc_TypeError, |
| 1172 | "'%.100s' object has only read-only attributes " |
| 1173 | "(%s .%.100s)", |
| 1174 | tp->tp_name, |
| 1175 | value==NULL ? "del" : "assign to", |
| 1176 | PyString_AS_STRING(name)); |
| 1177 | return -1; |
| 1178 | } |
| 1179 | |
| 1180 | /* Helper to get a pointer to an object's __dict__ slot, if any */ |
| 1181 | |
| 1182 | PyObject ** |
| 1183 | _PyObject_GetDictPtr(PyObject *obj) |
| 1184 | { |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1185 | long dictoffset; |
| 1186 | PyTypeObject *tp = obj->ob_type; |
| 1187 | |
| 1188 | if (!(tp->tp_flags & Py_TPFLAGS_HAVE_CLASS)) |
| 1189 | return NULL; |
| 1190 | dictoffset = tp->tp_dictoffset; |
| 1191 | if (dictoffset == 0) |
| 1192 | return NULL; |
| 1193 | if (dictoffset < 0) { |
Tim Peters | f2a67da | 2001-10-07 03:54:51 +0000 | [diff] [blame] | 1194 | const size_t size = _PyObject_VAR_SIZE(tp, |
| 1195 | ((PyVarObject *)obj)->ob_size); |
Tim Peters | 6d483d3 | 2001-10-06 21:27:34 +0000 | [diff] [blame] | 1196 | dictoffset += (long)size; |
| 1197 | assert(dictoffset > 0); |
| 1198 | assert(dictoffset % SIZEOF_VOID_P == 0); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1199 | } |
| 1200 | return (PyObject **) ((char *)obj + dictoffset); |
| 1201 | } |
| 1202 | |
| 1203 | /* Generic GetAttr functions - put these in your tp_[gs]etattro slot */ |
| 1204 | |
| 1205 | PyObject * |
| 1206 | PyObject_GenericGetAttr(PyObject *obj, PyObject *name) |
| 1207 | { |
| 1208 | PyTypeObject *tp = obj->ob_type; |
| 1209 | PyObject *descr; |
| 1210 | descrgetfunc f; |
| 1211 | PyObject **dictptr; |
| 1212 | |
| 1213 | if (tp->tp_dict == NULL) { |
Guido van Rossum | 528b7eb | 2001-08-07 17:24:28 +0000 | [diff] [blame] | 1214 | if (PyType_Ready(tp) < 0) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1215 | return NULL; |
| 1216 | } |
| 1217 | |
| 1218 | descr = _PyType_Lookup(tp, name); |
| 1219 | f = NULL; |
| 1220 | if (descr != NULL) { |
| 1221 | f = descr->ob_type->tp_descr_get; |
| 1222 | if (f != NULL && PyDescr_IsData(descr)) |
| 1223 | return f(descr, obj, (PyObject *)obj->ob_type); |
| 1224 | } |
| 1225 | |
| 1226 | dictptr = _PyObject_GetDictPtr(obj); |
| 1227 | if (dictptr != NULL) { |
| 1228 | PyObject *dict = *dictptr; |
| 1229 | if (dict != NULL) { |
| 1230 | PyObject *res = PyDict_GetItem(dict, name); |
| 1231 | if (res != NULL) { |
| 1232 | Py_INCREF(res); |
| 1233 | return res; |
| 1234 | } |
| 1235 | } |
| 1236 | } |
| 1237 | |
| 1238 | if (f != NULL) |
| 1239 | return f(descr, obj, (PyObject *)obj->ob_type); |
| 1240 | |
| 1241 | if (descr != NULL) { |
| 1242 | Py_INCREF(descr); |
| 1243 | return descr; |
| 1244 | } |
| 1245 | |
| 1246 | PyErr_Format(PyExc_AttributeError, |
| 1247 | "'%.50s' object has no attribute '%.400s'", |
| 1248 | tp->tp_name, PyString_AS_STRING(name)); |
| 1249 | return NULL; |
| 1250 | } |
| 1251 | |
| 1252 | int |
| 1253 | PyObject_GenericSetAttr(PyObject *obj, PyObject *name, PyObject *value) |
| 1254 | { |
| 1255 | PyTypeObject *tp = obj->ob_type; |
| 1256 | PyObject *descr; |
| 1257 | descrsetfunc f; |
| 1258 | PyObject **dictptr; |
| 1259 | |
| 1260 | if (tp->tp_dict == NULL) { |
Guido van Rossum | 528b7eb | 2001-08-07 17:24:28 +0000 | [diff] [blame] | 1261 | if (PyType_Ready(tp) < 0) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1262 | return -1; |
| 1263 | } |
| 1264 | |
| 1265 | descr = _PyType_Lookup(tp, name); |
| 1266 | f = NULL; |
| 1267 | if (descr != NULL) { |
| 1268 | f = descr->ob_type->tp_descr_set; |
| 1269 | if (f != NULL && PyDescr_IsData(descr)) |
| 1270 | return f(descr, obj, value); |
| 1271 | } |
| 1272 | |
| 1273 | dictptr = _PyObject_GetDictPtr(obj); |
| 1274 | if (dictptr != NULL) { |
| 1275 | PyObject *dict = *dictptr; |
| 1276 | if (dict == NULL && value != NULL) { |
| 1277 | dict = PyDict_New(); |
| 1278 | if (dict == NULL) |
| 1279 | return -1; |
| 1280 | *dictptr = dict; |
| 1281 | } |
| 1282 | if (dict != NULL) { |
| 1283 | int res; |
| 1284 | if (value == NULL) |
| 1285 | res = PyDict_DelItem(dict, name); |
| 1286 | else |
| 1287 | res = PyDict_SetItem(dict, name, value); |
| 1288 | if (res < 0 && PyErr_ExceptionMatches(PyExc_KeyError)) |
| 1289 | PyErr_SetObject(PyExc_AttributeError, name); |
| 1290 | return res; |
| 1291 | } |
| 1292 | } |
| 1293 | |
| 1294 | if (f != NULL) |
| 1295 | return f(descr, obj, value); |
| 1296 | |
| 1297 | if (descr == NULL) { |
| 1298 | PyErr_Format(PyExc_AttributeError, |
| 1299 | "'%.50s' object has no attribute '%.400s'", |
| 1300 | tp->tp_name, PyString_AS_STRING(name)); |
| 1301 | return -1; |
| 1302 | } |
| 1303 | |
| 1304 | PyErr_Format(PyExc_AttributeError, |
| 1305 | "'%.50s' object attribute '%.400s' is read-only", |
| 1306 | tp->tp_name, PyString_AS_STRING(name)); |
| 1307 | return -1; |
Guido van Rossum | 98ff96a | 1997-05-20 18:34:44 +0000 | [diff] [blame] | 1308 | } |
| 1309 | |
Guido van Rossum | 6ac258d | 1993-05-12 08:24:20 +0000 | [diff] [blame] | 1310 | /* Test a value used as condition, e.g., in a for or if statement. |
| 1311 | Return -1 if an error occurred */ |
| 1312 | |
| 1313 | int |
Fred Drake | 100814d | 2000-07-09 15:48:49 +0000 | [diff] [blame] | 1314 | PyObject_IsTrue(PyObject *v) |
Guido van Rossum | 6ac258d | 1993-05-12 08:24:20 +0000 | [diff] [blame] | 1315 | { |
| 1316 | int res; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1317 | if (v == Py_None) |
Guido van Rossum | 6ac258d | 1993-05-12 08:24:20 +0000 | [diff] [blame] | 1318 | res = 0; |
Guido van Rossum | 1c4f458 | 1998-05-22 00:53:24 +0000 | [diff] [blame] | 1319 | else if (v->ob_type->tp_as_number != NULL && |
| 1320 | v->ob_type->tp_as_number->nb_nonzero != NULL) |
Guido van Rossum | 6ac258d | 1993-05-12 08:24:20 +0000 | [diff] [blame] | 1321 | res = (*v->ob_type->tp_as_number->nb_nonzero)(v); |
Guido van Rossum | 1c4f458 | 1998-05-22 00:53:24 +0000 | [diff] [blame] | 1322 | else if (v->ob_type->tp_as_mapping != NULL && |
| 1323 | v->ob_type->tp_as_mapping->mp_length != NULL) |
Guido van Rossum | 6ac258d | 1993-05-12 08:24:20 +0000 | [diff] [blame] | 1324 | res = (*v->ob_type->tp_as_mapping->mp_length)(v); |
Guido van Rossum | 1c4f458 | 1998-05-22 00:53:24 +0000 | [diff] [blame] | 1325 | else if (v->ob_type->tp_as_sequence != NULL && |
| 1326 | v->ob_type->tp_as_sequence->sq_length != NULL) |
Guido van Rossum | 6ac258d | 1993-05-12 08:24:20 +0000 | [diff] [blame] | 1327 | res = (*v->ob_type->tp_as_sequence->sq_length)(v); |
| 1328 | else |
| 1329 | res = 1; |
| 1330 | if (res > 0) |
| 1331 | res = 1; |
| 1332 | return res; |
| 1333 | } |
| 1334 | |
Guido van Rossum | c3d3f96 | 1998-04-09 17:53:59 +0000 | [diff] [blame] | 1335 | /* equivalent of 'not v' |
| 1336 | Return -1 if an error occurred */ |
| 1337 | |
| 1338 | int |
Fred Drake | 100814d | 2000-07-09 15:48:49 +0000 | [diff] [blame] | 1339 | PyObject_Not(PyObject *v) |
Guido van Rossum | c3d3f96 | 1998-04-09 17:53:59 +0000 | [diff] [blame] | 1340 | { |
| 1341 | int res; |
| 1342 | res = PyObject_IsTrue(v); |
| 1343 | if (res < 0) |
| 1344 | return res; |
| 1345 | return res == 0; |
| 1346 | } |
| 1347 | |
Guido van Rossum | 5524a59 | 1995-01-10 15:26:20 +0000 | [diff] [blame] | 1348 | /* Coerce two numeric types to the "larger" one. |
| 1349 | Increment the reference count on each argument. |
Guido van Rossum | e797ec1 | 2001-01-17 15:24:28 +0000 | [diff] [blame] | 1350 | Return value: |
| 1351 | -1 if an error occurred; |
| 1352 | 0 if the coercion succeeded (and then the reference counts are increased); |
| 1353 | 1 if no coercion is possible (and no error is raised). |
Guido van Rossum | 5524a59 | 1995-01-10 15:26:20 +0000 | [diff] [blame] | 1354 | */ |
Guido van Rossum | 5524a59 | 1995-01-10 15:26:20 +0000 | [diff] [blame] | 1355 | int |
Fred Drake | 100814d | 2000-07-09 15:48:49 +0000 | [diff] [blame] | 1356 | PyNumber_CoerceEx(PyObject **pv, PyObject **pw) |
Guido van Rossum | 5524a59 | 1995-01-10 15:26:20 +0000 | [diff] [blame] | 1357 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1358 | register PyObject *v = *pv; |
| 1359 | register PyObject *w = *pw; |
Guido van Rossum | 5524a59 | 1995-01-10 15:26:20 +0000 | [diff] [blame] | 1360 | int res; |
| 1361 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1362 | if (v->ob_type == w->ob_type && !PyInstance_Check(v)) { |
| 1363 | Py_INCREF(v); |
| 1364 | Py_INCREF(w); |
Guido van Rossum | 5524a59 | 1995-01-10 15:26:20 +0000 | [diff] [blame] | 1365 | return 0; |
| 1366 | } |
| 1367 | if (v->ob_type->tp_as_number && v->ob_type->tp_as_number->nb_coerce) { |
| 1368 | res = (*v->ob_type->tp_as_number->nb_coerce)(pv, pw); |
| 1369 | if (res <= 0) |
| 1370 | return res; |
| 1371 | } |
| 1372 | if (w->ob_type->tp_as_number && w->ob_type->tp_as_number->nb_coerce) { |
| 1373 | res = (*w->ob_type->tp_as_number->nb_coerce)(pw, pv); |
| 1374 | if (res <= 0) |
| 1375 | return res; |
| 1376 | } |
Guido van Rossum | 242c642 | 1997-11-19 16:03:17 +0000 | [diff] [blame] | 1377 | return 1; |
| 1378 | } |
| 1379 | |
Guido van Rossum | e797ec1 | 2001-01-17 15:24:28 +0000 | [diff] [blame] | 1380 | /* Coerce two numeric types to the "larger" one. |
| 1381 | Increment the reference count on each argument. |
| 1382 | Return -1 and raise an exception if no coercion is possible |
| 1383 | (and then no reference count is incremented). |
| 1384 | */ |
Guido van Rossum | 242c642 | 1997-11-19 16:03:17 +0000 | [diff] [blame] | 1385 | int |
Fred Drake | 100814d | 2000-07-09 15:48:49 +0000 | [diff] [blame] | 1386 | PyNumber_Coerce(PyObject **pv, PyObject **pw) |
Guido van Rossum | 242c642 | 1997-11-19 16:03:17 +0000 | [diff] [blame] | 1387 | { |
| 1388 | int err = PyNumber_CoerceEx(pv, pw); |
| 1389 | if (err <= 0) |
| 1390 | return err; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1391 | PyErr_SetString(PyExc_TypeError, "number coercion failed"); |
Guido van Rossum | 5524a59 | 1995-01-10 15:26:20 +0000 | [diff] [blame] | 1392 | return -1; |
| 1393 | } |
| 1394 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1395 | |
Guido van Rossum | 49b11fe | 1995-01-26 00:38:22 +0000 | [diff] [blame] | 1396 | /* Test whether an object can be called */ |
| 1397 | |
| 1398 | int |
Fred Drake | 100814d | 2000-07-09 15:48:49 +0000 | [diff] [blame] | 1399 | PyCallable_Check(PyObject *x) |
Guido van Rossum | 49b11fe | 1995-01-26 00:38:22 +0000 | [diff] [blame] | 1400 | { |
| 1401 | if (x == NULL) |
| 1402 | return 0; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1403 | if (PyInstance_Check(x)) { |
| 1404 | PyObject *call = PyObject_GetAttrString(x, "__call__"); |
Guido van Rossum | 49b11fe | 1995-01-26 00:38:22 +0000 | [diff] [blame] | 1405 | if (call == NULL) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1406 | PyErr_Clear(); |
Guido van Rossum | 49b11fe | 1995-01-26 00:38:22 +0000 | [diff] [blame] | 1407 | return 0; |
| 1408 | } |
| 1409 | /* Could test recursively but don't, for fear of endless |
| 1410 | recursion if some joker sets self.__call__ = self */ |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1411 | Py_DECREF(call); |
Guido van Rossum | 49b11fe | 1995-01-26 00:38:22 +0000 | [diff] [blame] | 1412 | return 1; |
| 1413 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1414 | else { |
| 1415 | return x->ob_type->tp_call != NULL; |
| 1416 | } |
Guido van Rossum | 49b11fe | 1995-01-26 00:38:22 +0000 | [diff] [blame] | 1417 | } |
| 1418 | |
Tim Peters | 7eea37e | 2001-09-04 22:08:56 +0000 | [diff] [blame] | 1419 | /* Helper for PyObject_Dir. |
| 1420 | Merge the __dict__ of aclass into dict, and recursively also all |
| 1421 | the __dict__s of aclass's base classes. The order of merging isn't |
| 1422 | defined, as it's expected that only the final set of dict keys is |
| 1423 | interesting. |
| 1424 | Return 0 on success, -1 on error. |
| 1425 | */ |
| 1426 | |
| 1427 | static int |
| 1428 | merge_class_dict(PyObject* dict, PyObject* aclass) |
| 1429 | { |
| 1430 | PyObject *classdict; |
| 1431 | PyObject *bases; |
| 1432 | |
| 1433 | assert(PyDict_Check(dict)); |
| 1434 | assert(aclass); |
| 1435 | |
| 1436 | /* Merge in the type's dict (if any). */ |
| 1437 | classdict = PyObject_GetAttrString(aclass, "__dict__"); |
| 1438 | if (classdict == NULL) |
| 1439 | PyErr_Clear(); |
| 1440 | else { |
| 1441 | int status = PyDict_Update(dict, classdict); |
| 1442 | Py_DECREF(classdict); |
| 1443 | if (status < 0) |
| 1444 | return -1; |
| 1445 | } |
| 1446 | |
| 1447 | /* Recursively merge in the base types' (if any) dicts. */ |
| 1448 | bases = PyObject_GetAttrString(aclass, "__bases__"); |
Tim Peters | bc7e863 | 2001-09-16 20:33:22 +0000 | [diff] [blame] | 1449 | if (bases == NULL) |
| 1450 | PyErr_Clear(); |
| 1451 | else { |
Tim Peters | 7eea37e | 2001-09-04 22:08:56 +0000 | [diff] [blame] | 1452 | int i, n; |
| 1453 | assert(PyTuple_Check(bases)); |
| 1454 | n = PyTuple_GET_SIZE(bases); |
| 1455 | for (i = 0; i < n; i++) { |
| 1456 | PyObject *base = PyTuple_GET_ITEM(bases, i); |
| 1457 | if (merge_class_dict(dict, base) < 0) { |
| 1458 | Py_DECREF(bases); |
| 1459 | return -1; |
| 1460 | } |
| 1461 | } |
| 1462 | Py_DECREF(bases); |
| 1463 | } |
| 1464 | return 0; |
| 1465 | } |
| 1466 | |
Tim Peters | 305b585 | 2001-09-17 02:38:46 +0000 | [diff] [blame] | 1467 | /* Helper for PyObject_Dir. |
| 1468 | If obj has an attr named attrname that's a list, merge its string |
| 1469 | elements into keys of dict. |
| 1470 | Return 0 on success, -1 on error. Errors due to not finding the attr, |
| 1471 | or the attr not being a list, are suppressed. |
| 1472 | */ |
| 1473 | |
| 1474 | static int |
| 1475 | merge_list_attr(PyObject* dict, PyObject* obj, char *attrname) |
| 1476 | { |
| 1477 | PyObject *list; |
| 1478 | int result = 0; |
| 1479 | |
| 1480 | assert(PyDict_Check(dict)); |
| 1481 | assert(obj); |
| 1482 | assert(attrname); |
| 1483 | |
| 1484 | list = PyObject_GetAttrString(obj, attrname); |
| 1485 | if (list == NULL) |
| 1486 | PyErr_Clear(); |
| 1487 | |
| 1488 | else if (PyList_Check(list)) { |
| 1489 | int i; |
| 1490 | for (i = 0; i < PyList_GET_SIZE(list); ++i) { |
| 1491 | PyObject *item = PyList_GET_ITEM(list, i); |
| 1492 | if (PyString_Check(item)) { |
| 1493 | result = PyDict_SetItem(dict, item, Py_None); |
| 1494 | if (result < 0) |
| 1495 | break; |
| 1496 | } |
| 1497 | } |
| 1498 | } |
| 1499 | |
| 1500 | Py_XDECREF(list); |
| 1501 | return result; |
| 1502 | } |
| 1503 | |
Tim Peters | 7eea37e | 2001-09-04 22:08:56 +0000 | [diff] [blame] | 1504 | /* Like __builtin__.dir(arg). See bltinmodule.c's builtin_dir for the |
| 1505 | docstring, which should be kept in synch with this implementation. */ |
| 1506 | |
| 1507 | PyObject * |
| 1508 | PyObject_Dir(PyObject *arg) |
| 1509 | { |
| 1510 | /* Set exactly one of these non-NULL before the end. */ |
| 1511 | PyObject *result = NULL; /* result list */ |
| 1512 | PyObject *masterdict = NULL; /* result is masterdict.keys() */ |
| 1513 | |
| 1514 | /* If NULL arg, return the locals. */ |
| 1515 | if (arg == NULL) { |
| 1516 | PyObject *locals = PyEval_GetLocals(); |
| 1517 | if (locals == NULL) |
| 1518 | goto error; |
| 1519 | result = PyDict_Keys(locals); |
| 1520 | if (result == NULL) |
| 1521 | goto error; |
| 1522 | } |
| 1523 | |
| 1524 | /* Elif this is some form of module, we only want its dict. */ |
Guido van Rossum | 8dbd3d8 | 2001-09-10 18:27:43 +0000 | [diff] [blame] | 1525 | else if (PyModule_Check(arg)) { |
Tim Peters | 7eea37e | 2001-09-04 22:08:56 +0000 | [diff] [blame] | 1526 | masterdict = PyObject_GetAttrString(arg, "__dict__"); |
| 1527 | if (masterdict == NULL) |
| 1528 | goto error; |
Guido van Rossum | 8dbd3d8 | 2001-09-10 18:27:43 +0000 | [diff] [blame] | 1529 | if (!PyDict_Check(masterdict)) { |
| 1530 | PyErr_SetString(PyExc_TypeError, |
| 1531 | "module.__dict__ is not a dictionary"); |
| 1532 | goto error; |
| 1533 | } |
Tim Peters | 7eea37e | 2001-09-04 22:08:56 +0000 | [diff] [blame] | 1534 | } |
| 1535 | |
| 1536 | /* Elif some form of type or class, grab its dict and its bases. |
| 1537 | We deliberately don't suck up its __class__, as methods belonging |
| 1538 | to the metaclass would probably be more confusing than helpful. */ |
| 1539 | else if (PyType_Check(arg) || PyClass_Check(arg)) { |
| 1540 | masterdict = PyDict_New(); |
| 1541 | if (masterdict == NULL) |
| 1542 | goto error; |
| 1543 | if (merge_class_dict(masterdict, arg) < 0) |
| 1544 | goto error; |
| 1545 | } |
| 1546 | |
| 1547 | /* Else look at its dict, and the attrs reachable from its class. */ |
| 1548 | else { |
| 1549 | PyObject *itsclass; |
| 1550 | /* Create a dict to start with. CAUTION: Not everything |
| 1551 | responding to __dict__ returns a dict! */ |
| 1552 | masterdict = PyObject_GetAttrString(arg, "__dict__"); |
| 1553 | if (masterdict == NULL) { |
| 1554 | PyErr_Clear(); |
| 1555 | masterdict = PyDict_New(); |
| 1556 | } |
| 1557 | else if (!PyDict_Check(masterdict)) { |
| 1558 | Py_DECREF(masterdict); |
| 1559 | masterdict = PyDict_New(); |
| 1560 | } |
| 1561 | else { |
| 1562 | /* The object may have returned a reference to its |
| 1563 | dict, so copy it to avoid mutating it. */ |
| 1564 | PyObject *temp = PyDict_Copy(masterdict); |
| 1565 | Py_DECREF(masterdict); |
| 1566 | masterdict = temp; |
| 1567 | } |
| 1568 | if (masterdict == NULL) |
| 1569 | goto error; |
| 1570 | |
Tim Peters | 305b585 | 2001-09-17 02:38:46 +0000 | [diff] [blame] | 1571 | /* Merge in __members__ and __methods__ (if any). |
| 1572 | XXX Would like this to go away someday; for now, it's |
| 1573 | XXX needed to get at im_self etc of method objects. */ |
| 1574 | if (merge_list_attr(masterdict, arg, "__members__") < 0) |
| 1575 | goto error; |
| 1576 | if (merge_list_attr(masterdict, arg, "__methods__") < 0) |
| 1577 | goto error; |
| 1578 | |
Tim Peters | 7eea37e | 2001-09-04 22:08:56 +0000 | [diff] [blame] | 1579 | /* Merge in attrs reachable from its class. |
| 1580 | CAUTION: Not all objects have a __class__ attr. */ |
| 1581 | itsclass = PyObject_GetAttrString(arg, "__class__"); |
| 1582 | if (itsclass == NULL) |
| 1583 | PyErr_Clear(); |
| 1584 | else { |
| 1585 | int status = merge_class_dict(masterdict, itsclass); |
| 1586 | Py_DECREF(itsclass); |
| 1587 | if (status < 0) |
| 1588 | goto error; |
| 1589 | } |
| 1590 | } |
| 1591 | |
| 1592 | assert((result == NULL) ^ (masterdict == NULL)); |
| 1593 | if (masterdict != NULL) { |
| 1594 | /* The result comes from its keys. */ |
| 1595 | assert(result == NULL); |
| 1596 | result = PyDict_Keys(masterdict); |
| 1597 | if (result == NULL) |
| 1598 | goto error; |
| 1599 | } |
| 1600 | |
| 1601 | assert(result); |
| 1602 | if (PyList_Sort(result) != 0) |
| 1603 | goto error; |
| 1604 | else |
| 1605 | goto normal_return; |
| 1606 | |
| 1607 | error: |
| 1608 | Py_XDECREF(result); |
| 1609 | result = NULL; |
| 1610 | /* fall through */ |
| 1611 | normal_return: |
| 1612 | Py_XDECREF(masterdict); |
| 1613 | return result; |
| 1614 | } |
Guido van Rossum | 49b11fe | 1995-01-26 00:38:22 +0000 | [diff] [blame] | 1615 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1616 | /* |
| 1617 | NoObject is usable as a non-NULL undefined value, used by the macro None. |
| 1618 | 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] | 1619 | so there is exactly one (which is indestructible, by the way). |
Guido van Rossum | ba21a49 | 2001-08-16 08:17:26 +0000 | [diff] [blame] | 1620 | (XXX This type and the type of NotImplemented below should be unified.) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1621 | */ |
| 1622 | |
Guido van Rossum | 0c182a1 | 1992-03-27 17:26:13 +0000 | [diff] [blame] | 1623 | /* ARGSUSED */ |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1624 | static PyObject * |
Fred Drake | 100814d | 2000-07-09 15:48:49 +0000 | [diff] [blame] | 1625 | none_repr(PyObject *op) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1626 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1627 | return PyString_FromString("None"); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1628 | } |
| 1629 | |
Barry Warsaw | 9bf1644 | 2001-01-23 16:24:35 +0000 | [diff] [blame] | 1630 | /* ARGUSED */ |
| 1631 | static void |
| 1632 | none_dealloc(PyObject* ignore) |
| 1633 | { |
| 1634 | /* This should never get called, but we also don't want to SEGV if |
| 1635 | * we accidently decref None out of existance. |
| 1636 | */ |
| 1637 | abort(); |
| 1638 | } |
| 1639 | |
| 1640 | |
Guido van Rossum | ba21a49 | 2001-08-16 08:17:26 +0000 | [diff] [blame] | 1641 | static PyTypeObject PyNone_Type = { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1642 | PyObject_HEAD_INIT(&PyType_Type) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1643 | 0, |
Guido van Rossum | ba21a49 | 2001-08-16 08:17:26 +0000 | [diff] [blame] | 1644 | "NoneType", |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1645 | 0, |
| 1646 | 0, |
Barry Warsaw | 9bf1644 | 2001-01-23 16:24:35 +0000 | [diff] [blame] | 1647 | (destructor)none_dealloc, /*tp_dealloc*/ /*never called*/ |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 1648 | 0, /*tp_print*/ |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1649 | 0, /*tp_getattr*/ |
| 1650 | 0, /*tp_setattr*/ |
| 1651 | 0, /*tp_compare*/ |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 1652 | (reprfunc)none_repr, /*tp_repr*/ |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1653 | 0, /*tp_as_number*/ |
| 1654 | 0, /*tp_as_sequence*/ |
| 1655 | 0, /*tp_as_mapping*/ |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 1656 | 0, /*tp_hash */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1657 | }; |
| 1658 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1659 | PyObject _Py_NoneStruct = { |
Guido van Rossum | ba21a49 | 2001-08-16 08:17:26 +0000 | [diff] [blame] | 1660 | PyObject_HEAD_INIT(&PyNone_Type) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1661 | }; |
| 1662 | |
Neil Schemenauer | 5ed85ec | 2001-01-04 01:48:10 +0000 | [diff] [blame] | 1663 | /* NotImplemented is an object that can be used to signal that an |
| 1664 | operation is not implemented for the given type combination. */ |
| 1665 | |
| 1666 | static PyObject * |
| 1667 | NotImplemented_repr(PyObject *op) |
| 1668 | { |
| 1669 | return PyString_FromString("NotImplemented"); |
| 1670 | } |
| 1671 | |
| 1672 | static PyTypeObject PyNotImplemented_Type = { |
| 1673 | PyObject_HEAD_INIT(&PyType_Type) |
| 1674 | 0, |
Guido van Rossum | ba21a49 | 2001-08-16 08:17:26 +0000 | [diff] [blame] | 1675 | "NotImplementedType", |
Neil Schemenauer | 5ed85ec | 2001-01-04 01:48:10 +0000 | [diff] [blame] | 1676 | 0, |
| 1677 | 0, |
Barry Warsaw | 9bf1644 | 2001-01-23 16:24:35 +0000 | [diff] [blame] | 1678 | (destructor)none_dealloc, /*tp_dealloc*/ /*never called*/ |
Neil Schemenauer | 5ed85ec | 2001-01-04 01:48:10 +0000 | [diff] [blame] | 1679 | 0, /*tp_print*/ |
| 1680 | 0, /*tp_getattr*/ |
| 1681 | 0, /*tp_setattr*/ |
| 1682 | 0, /*tp_compare*/ |
| 1683 | (reprfunc)NotImplemented_repr, /*tp_repr*/ |
| 1684 | 0, /*tp_as_number*/ |
| 1685 | 0, /*tp_as_sequence*/ |
| 1686 | 0, /*tp_as_mapping*/ |
| 1687 | 0, /*tp_hash */ |
| 1688 | }; |
| 1689 | |
| 1690 | PyObject _Py_NotImplementedStruct = { |
| 1691 | PyObject_HEAD_INIT(&PyNotImplemented_Type) |
| 1692 | }; |
| 1693 | |
Guido van Rossum | ba21a49 | 2001-08-16 08:17:26 +0000 | [diff] [blame] | 1694 | void |
| 1695 | _Py_ReadyTypes(void) |
| 1696 | { |
| 1697 | if (PyType_Ready(&PyType_Type) < 0) |
| 1698 | Py_FatalError("Can't initialize 'type'"); |
| 1699 | |
| 1700 | if (PyType_Ready(&PyList_Type) < 0) |
| 1701 | Py_FatalError("Can't initialize 'list'"); |
| 1702 | |
| 1703 | if (PyType_Ready(&PyNone_Type) < 0) |
| 1704 | Py_FatalError("Can't initialize type(None)"); |
| 1705 | |
| 1706 | if (PyType_Ready(&PyNotImplemented_Type) < 0) |
| 1707 | Py_FatalError("Can't initialize type(NotImplemented)"); |
| 1708 | } |
| 1709 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1710 | |
Guido van Rossum | 84a9032 | 1996-05-22 16:34:47 +0000 | [diff] [blame] | 1711 | #ifdef Py_TRACE_REFS |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1712 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1713 | static PyObject refchain = {&refchain, &refchain}; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1714 | |
Guido van Rossum | aacdc9d | 1996-08-12 21:32:12 +0000 | [diff] [blame] | 1715 | void |
Fred Drake | 100814d | 2000-07-09 15:48:49 +0000 | [diff] [blame] | 1716 | _Py_ResetReferences(void) |
Guido van Rossum | e09fb55 | 1997-08-05 02:04:34 +0000 | [diff] [blame] | 1717 | { |
| 1718 | refchain._ob_prev = refchain._ob_next = &refchain; |
| 1719 | _Py_RefTotal = 0; |
| 1720 | } |
| 1721 | |
| 1722 | void |
Fred Drake | 100814d | 2000-07-09 15:48:49 +0000 | [diff] [blame] | 1723 | _Py_NewReference(PyObject *op) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1724 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1725 | _Py_RefTotal++; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1726 | op->ob_refcnt = 1; |
| 1727 | op->_ob_next = refchain._ob_next; |
| 1728 | op->_ob_prev = &refchain; |
| 1729 | refchain._ob_next->_ob_prev = op; |
| 1730 | refchain._ob_next = op; |
Sjoerd Mullender | a9c3c22 | 1993-10-11 12:54:31 +0000 | [diff] [blame] | 1731 | #ifdef COUNT_ALLOCS |
| 1732 | inc_count(op->ob_type); |
| 1733 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1734 | } |
| 1735 | |
Guido van Rossum | aacdc9d | 1996-08-12 21:32:12 +0000 | [diff] [blame] | 1736 | void |
Fred Drake | 100814d | 2000-07-09 15:48:49 +0000 | [diff] [blame] | 1737 | _Py_ForgetReference(register PyObject *op) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1738 | { |
Guido van Rossum | bffd683 | 2000-01-20 22:32:56 +0000 | [diff] [blame] | 1739 | #ifdef SLOW_UNREF_CHECK |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 1740 | register PyObject *p; |
Guido van Rossum | bffd683 | 2000-01-20 22:32:56 +0000 | [diff] [blame] | 1741 | #endif |
Guido van Rossum | d7047b3 | 1995-01-02 19:07:15 +0000 | [diff] [blame] | 1742 | if (op->ob_refcnt < 0) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1743 | Py_FatalError("UNREF negative refcnt"); |
Guido van Rossum | 2e8f614 | 1992-09-03 20:32:55 +0000 | [diff] [blame] | 1744 | if (op == &refchain || |
Guido van Rossum | d7047b3 | 1995-01-02 19:07:15 +0000 | [diff] [blame] | 1745 | 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] | 1746 | Py_FatalError("UNREF invalid object"); |
Guido van Rossum | 2e8f614 | 1992-09-03 20:32:55 +0000 | [diff] [blame] | 1747 | #ifdef SLOW_UNREF_CHECK |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1748 | for (p = refchain._ob_next; p != &refchain; p = p->_ob_next) { |
| 1749 | if (p == op) |
| 1750 | break; |
| 1751 | } |
Guido van Rossum | d7047b3 | 1995-01-02 19:07:15 +0000 | [diff] [blame] | 1752 | if (p == &refchain) /* Not found */ |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1753 | Py_FatalError("UNREF unknown object"); |
Guido van Rossum | 2e8f614 | 1992-09-03 20:32:55 +0000 | [diff] [blame] | 1754 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1755 | op->_ob_next->_ob_prev = op->_ob_prev; |
| 1756 | op->_ob_prev->_ob_next = op->_ob_next; |
Guido van Rossum | 2e8f614 | 1992-09-03 20:32:55 +0000 | [diff] [blame] | 1757 | op->_ob_next = op->_ob_prev = NULL; |
Guido van Rossum | d8953cb | 1995-04-06 14:46:26 +0000 | [diff] [blame] | 1758 | #ifdef COUNT_ALLOCS |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1759 | op->ob_type->tp_frees++; |
Guido van Rossum | d8953cb | 1995-04-06 14:46:26 +0000 | [diff] [blame] | 1760 | #endif |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1761 | } |
| 1762 | |
Guido van Rossum | aacdc9d | 1996-08-12 21:32:12 +0000 | [diff] [blame] | 1763 | void |
Fred Drake | 100814d | 2000-07-09 15:48:49 +0000 | [diff] [blame] | 1764 | _Py_Dealloc(PyObject *op) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1765 | { |
Guido van Rossum | 9776adf | 1994-09-07 14:36:45 +0000 | [diff] [blame] | 1766 | destructor dealloc = op->ob_type->tp_dealloc; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1767 | _Py_ForgetReference(op); |
Guido van Rossum | 9776adf | 1994-09-07 14:36:45 +0000 | [diff] [blame] | 1768 | (*dealloc)(op); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1769 | } |
| 1770 | |
Guido van Rossum | aacdc9d | 1996-08-12 21:32:12 +0000 | [diff] [blame] | 1771 | void |
Fred Drake | 100814d | 2000-07-09 15:48:49 +0000 | [diff] [blame] | 1772 | _Py_PrintReferences(FILE *fp) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1773 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1774 | PyObject *op; |
Guido van Rossum | e09fb55 | 1997-08-05 02:04:34 +0000 | [diff] [blame] | 1775 | fprintf(fp, "Remaining objects:\n"); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1776 | for (op = refchain._ob_next; op != &refchain; op = op->_ob_next) { |
| 1777 | fprintf(fp, "[%d] ", op->ob_refcnt); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1778 | if (PyObject_Print(op, fp, 0) != 0) |
| 1779 | PyErr_Clear(); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1780 | putc('\n', fp); |
| 1781 | } |
| 1782 | } |
| 1783 | |
Sjoerd Mullender | 6ec3c65 | 1995-08-29 09:18:14 +0000 | [diff] [blame] | 1784 | PyObject * |
Fred Drake | 100814d | 2000-07-09 15:48:49 +0000 | [diff] [blame] | 1785 | _Py_GetObjects(PyObject *self, PyObject *args) |
Sjoerd Mullender | 6ec3c65 | 1995-08-29 09:18:14 +0000 | [diff] [blame] | 1786 | { |
| 1787 | int i, n; |
| 1788 | PyObject *t = NULL; |
| 1789 | PyObject *res, *op; |
| 1790 | |
| 1791 | if (!PyArg_ParseTuple(args, "i|O", &n, &t)) |
| 1792 | return NULL; |
| 1793 | op = refchain._ob_next; |
| 1794 | res = PyList_New(0); |
| 1795 | if (res == NULL) |
| 1796 | return NULL; |
| 1797 | for (i = 0; (n == 0 || i < n) && op != &refchain; i++) { |
| 1798 | while (op == self || op == args || op == res || op == t || |
Guido van Rossum | 3c29602 | 2001-07-14 17:58:00 +0000 | [diff] [blame] | 1799 | (t != NULL && op->ob_type != (PyTypeObject *) t)) { |
Sjoerd Mullender | 6ec3c65 | 1995-08-29 09:18:14 +0000 | [diff] [blame] | 1800 | op = op->_ob_next; |
| 1801 | if (op == &refchain) |
| 1802 | return res; |
| 1803 | } |
| 1804 | if (PyList_Append(res, op) < 0) { |
| 1805 | Py_DECREF(res); |
| 1806 | return NULL; |
| 1807 | } |
| 1808 | op = op->_ob_next; |
| 1809 | } |
| 1810 | return res; |
| 1811 | } |
| 1812 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1813 | #endif |
Guido van Rossum | 97ead3f | 1996-01-12 01:24:09 +0000 | [diff] [blame] | 1814 | |
| 1815 | |
| 1816 | /* Hack to force loading of cobject.o */ |
Guido van Rossum | da9c271 | 1996-12-05 21:58:58 +0000 | [diff] [blame] | 1817 | PyTypeObject *_Py_cobject_hack = &PyCObject_Type; |
Guido van Rossum | 84a9032 | 1996-05-22 16:34:47 +0000 | [diff] [blame] | 1818 | |
| 1819 | |
| 1820 | /* Hack to force loading of abstract.o */ |
Jeremy Hylton | 6253f83 | 2000-07-12 12:56:19 +0000 | [diff] [blame] | 1821 | int (*_Py_abstract_hack)(PyObject *) = &PyObject_Size; |
Guido van Rossum | e09fb55 | 1997-08-05 02:04:34 +0000 | [diff] [blame] | 1822 | |
| 1823 | |
Andrew M. Kuchling | 1582a3a | 2000-08-16 12:27:23 +0000 | [diff] [blame] | 1824 | /* Python's malloc wrappers (see pymem.h) */ |
Guido van Rossum | e09fb55 | 1997-08-05 02:04:34 +0000 | [diff] [blame] | 1825 | |
Thomas Wouters | 334fb89 | 2000-07-25 12:56:38 +0000 | [diff] [blame] | 1826 | void * |
Fred Drake | 100814d | 2000-07-09 15:48:49 +0000 | [diff] [blame] | 1827 | PyMem_Malloc(size_t nbytes) |
Guido van Rossum | e09fb55 | 1997-08-05 02:04:34 +0000 | [diff] [blame] | 1828 | { |
| 1829 | #if _PyMem_EXTRA > 0 |
| 1830 | if (nbytes == 0) |
| 1831 | nbytes = _PyMem_EXTRA; |
| 1832 | #endif |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 1833 | return PyMem_MALLOC(nbytes); |
Guido van Rossum | e09fb55 | 1997-08-05 02:04:34 +0000 | [diff] [blame] | 1834 | } |
| 1835 | |
Thomas Wouters | 334fb89 | 2000-07-25 12:56:38 +0000 | [diff] [blame] | 1836 | void * |
| 1837 | PyMem_Realloc(void *p, size_t nbytes) |
Guido van Rossum | e09fb55 | 1997-08-05 02:04:34 +0000 | [diff] [blame] | 1838 | { |
| 1839 | #if _PyMem_EXTRA > 0 |
| 1840 | if (nbytes == 0) |
| 1841 | nbytes = _PyMem_EXTRA; |
| 1842 | #endif |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 1843 | return PyMem_REALLOC(p, nbytes); |
Guido van Rossum | e09fb55 | 1997-08-05 02:04:34 +0000 | [diff] [blame] | 1844 | } |
| 1845 | |
| 1846 | void |
Thomas Wouters | 334fb89 | 2000-07-25 12:56:38 +0000 | [diff] [blame] | 1847 | PyMem_Free(void *p) |
Guido van Rossum | e09fb55 | 1997-08-05 02:04:34 +0000 | [diff] [blame] | 1848 | { |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 1849 | PyMem_FREE(p); |
| 1850 | } |
| 1851 | |
| 1852 | |
| 1853 | /* Python's object malloc wrappers (see objimpl.h) */ |
| 1854 | |
Thomas Wouters | 334fb89 | 2000-07-25 12:56:38 +0000 | [diff] [blame] | 1855 | void * |
Fred Drake | 100814d | 2000-07-09 15:48:49 +0000 | [diff] [blame] | 1856 | PyObject_Malloc(size_t nbytes) |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 1857 | { |
| 1858 | return PyObject_MALLOC(nbytes); |
| 1859 | } |
| 1860 | |
Thomas Wouters | 334fb89 | 2000-07-25 12:56:38 +0000 | [diff] [blame] | 1861 | void * |
| 1862 | PyObject_Realloc(void *p, size_t nbytes) |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 1863 | { |
| 1864 | return PyObject_REALLOC(p, nbytes); |
| 1865 | } |
| 1866 | |
| 1867 | void |
Thomas Wouters | 334fb89 | 2000-07-25 12:56:38 +0000 | [diff] [blame] | 1868 | PyObject_Free(void *p) |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 1869 | { |
| 1870 | PyObject_FREE(p); |
Guido van Rossum | e09fb55 | 1997-08-05 02:04:34 +0000 | [diff] [blame] | 1871 | } |
Guido van Rossum | 8661036 | 1998-04-10 22:32:46 +0000 | [diff] [blame] | 1872 | |
| 1873 | |
| 1874 | /* These methods are used to control infinite recursion in repr, str, print, |
| 1875 | etc. Container objects that may recursively contain themselves, |
| 1876 | e.g. builtin dictionaries and lists, should used Py_ReprEnter() and |
| 1877 | Py_ReprLeave() to avoid infinite recursion. |
| 1878 | |
| 1879 | Py_ReprEnter() returns 0 the first time it is called for a particular |
| 1880 | object and 1 every time thereafter. It returns -1 if an exception |
| 1881 | occurred. Py_ReprLeave() has no return value. |
| 1882 | |
| 1883 | See dictobject.c and listobject.c for examples of use. |
| 1884 | */ |
| 1885 | |
| 1886 | #define KEY "Py_Repr" |
| 1887 | |
| 1888 | int |
Fred Drake | 100814d | 2000-07-09 15:48:49 +0000 | [diff] [blame] | 1889 | Py_ReprEnter(PyObject *obj) |
Guido van Rossum | 8661036 | 1998-04-10 22:32:46 +0000 | [diff] [blame] | 1890 | { |
| 1891 | PyObject *dict; |
| 1892 | PyObject *list; |
| 1893 | int i; |
| 1894 | |
| 1895 | dict = PyThreadState_GetDict(); |
| 1896 | if (dict == NULL) |
| 1897 | return -1; |
| 1898 | list = PyDict_GetItemString(dict, KEY); |
| 1899 | if (list == NULL) { |
| 1900 | list = PyList_New(0); |
| 1901 | if (list == NULL) |
| 1902 | return -1; |
| 1903 | if (PyDict_SetItemString(dict, KEY, list) < 0) |
| 1904 | return -1; |
| 1905 | Py_DECREF(list); |
| 1906 | } |
| 1907 | i = PyList_GET_SIZE(list); |
| 1908 | while (--i >= 0) { |
| 1909 | if (PyList_GET_ITEM(list, i) == obj) |
| 1910 | return 1; |
| 1911 | } |
| 1912 | PyList_Append(list, obj); |
| 1913 | return 0; |
| 1914 | } |
| 1915 | |
| 1916 | void |
Fred Drake | 100814d | 2000-07-09 15:48:49 +0000 | [diff] [blame] | 1917 | Py_ReprLeave(PyObject *obj) |
Guido van Rossum | 8661036 | 1998-04-10 22:32:46 +0000 | [diff] [blame] | 1918 | { |
| 1919 | PyObject *dict; |
| 1920 | PyObject *list; |
| 1921 | int i; |
| 1922 | |
| 1923 | dict = PyThreadState_GetDict(); |
Guido van Rossum | eb90946 | 1998-04-11 15:17:34 +0000 | [diff] [blame] | 1924 | if (dict == NULL) |
| 1925 | return; |
Guido van Rossum | 8661036 | 1998-04-10 22:32:46 +0000 | [diff] [blame] | 1926 | list = PyDict_GetItemString(dict, KEY); |
Guido van Rossum | eb90946 | 1998-04-11 15:17:34 +0000 | [diff] [blame] | 1927 | if (list == NULL || !PyList_Check(list)) |
| 1928 | return; |
Guido van Rossum | 8661036 | 1998-04-10 22:32:46 +0000 | [diff] [blame] | 1929 | i = PyList_GET_SIZE(list); |
| 1930 | /* Count backwards because we always expect obj to be list[-1] */ |
| 1931 | while (--i >= 0) { |
| 1932 | if (PyList_GET_ITEM(list, i) == obj) { |
| 1933 | PyList_SetSlice(list, i, i + 1, NULL); |
| 1934 | break; |
| 1935 | } |
| 1936 | } |
| 1937 | } |
Guido van Rossum | d724b23 | 2000-03-13 16:01:29 +0000 | [diff] [blame] | 1938 | |
| 1939 | /* |
| 1940 | trashcan |
| 1941 | CT 2k0130 |
| 1942 | non-recursively destroy nested objects |
| 1943 | |
| 1944 | CT 2k0223 |
| 1945 | everything is now done in a macro. |
| 1946 | |
| 1947 | CT 2k0305 |
| 1948 | modified to use functions, after Tim Peter's suggestion. |
| 1949 | |
| 1950 | CT 2k0309 |
| 1951 | modified to restore a possible error. |
Guido van Rossum | 13ff8eb | 2000-03-25 18:39:19 +0000 | [diff] [blame] | 1952 | |
| 1953 | CT 2k0325 |
| 1954 | added better safe than sorry check for threadstate |
Guido van Rossum | e92e610 | 2000-04-24 15:40:53 +0000 | [diff] [blame] | 1955 | |
| 1956 | CT 2k0422 |
| 1957 | complete rewrite. We now build a chain via ob_type |
| 1958 | and save the limited number of types in ob_refcnt. |
| 1959 | This is perfect since we don't need any memory. |
| 1960 | A patch for free-threading would need just a lock. |
Guido van Rossum | d724b23 | 2000-03-13 16:01:29 +0000 | [diff] [blame] | 1961 | */ |
| 1962 | |
Guido van Rossum | e92e610 | 2000-04-24 15:40:53 +0000 | [diff] [blame] | 1963 | #define Py_TRASHCAN_TUPLE 1 |
| 1964 | #define Py_TRASHCAN_LIST 2 |
| 1965 | #define Py_TRASHCAN_DICT 3 |
| 1966 | #define Py_TRASHCAN_FRAME 4 |
| 1967 | #define Py_TRASHCAN_TRACEBACK 5 |
| 1968 | /* extend here if other objects want protection */ |
| 1969 | |
Guido van Rossum | d724b23 | 2000-03-13 16:01:29 +0000 | [diff] [blame] | 1970 | int _PyTrash_delete_nesting = 0; |
Guido van Rossum | e92e610 | 2000-04-24 15:40:53 +0000 | [diff] [blame] | 1971 | |
Guido van Rossum | d724b23 | 2000-03-13 16:01:29 +0000 | [diff] [blame] | 1972 | PyObject * _PyTrash_delete_later = NULL; |
| 1973 | |
| 1974 | void |
Fred Drake | 100814d | 2000-07-09 15:48:49 +0000 | [diff] [blame] | 1975 | _PyTrash_deposit_object(PyObject *op) |
Guido van Rossum | d724b23 | 2000-03-13 16:01:29 +0000 | [diff] [blame] | 1976 | { |
Guido van Rossum | e92e610 | 2000-04-24 15:40:53 +0000 | [diff] [blame] | 1977 | int typecode; |
Guido van Rossum | 13ff8eb | 2000-03-25 18:39:19 +0000 | [diff] [blame] | 1978 | |
Guido van Rossum | e92e610 | 2000-04-24 15:40:53 +0000 | [diff] [blame] | 1979 | if (PyTuple_Check(op)) |
| 1980 | typecode = Py_TRASHCAN_TUPLE; |
| 1981 | else if (PyList_Check(op)) |
| 1982 | typecode = Py_TRASHCAN_LIST; |
| 1983 | else if (PyDict_Check(op)) |
| 1984 | typecode = Py_TRASHCAN_DICT; |
| 1985 | else if (PyFrame_Check(op)) |
| 1986 | typecode = Py_TRASHCAN_FRAME; |
| 1987 | else if (PyTraceBack_Check(op)) |
| 1988 | typecode = Py_TRASHCAN_TRACEBACK; |
Moshe Zadka | cf703f0 | 2000-08-04 15:36:13 +0000 | [diff] [blame] | 1989 | else /* We have a bug here -- those are the only types in GC */ { |
| 1990 | Py_FatalError("Type not supported in GC -- internal bug"); |
| 1991 | return; /* pacify compiler -- execution never here */ |
| 1992 | } |
Guido van Rossum | e92e610 | 2000-04-24 15:40:53 +0000 | [diff] [blame] | 1993 | op->ob_refcnt = typecode; |
Guido van Rossum | d724b23 | 2000-03-13 16:01:29 +0000 | [diff] [blame] | 1994 | |
Guido van Rossum | e92e610 | 2000-04-24 15:40:53 +0000 | [diff] [blame] | 1995 | op->ob_type = (PyTypeObject*)_PyTrash_delete_later; |
| 1996 | _PyTrash_delete_later = op; |
Guido van Rossum | d724b23 | 2000-03-13 16:01:29 +0000 | [diff] [blame] | 1997 | } |
| 1998 | |
| 1999 | void |
Fred Drake | 100814d | 2000-07-09 15:48:49 +0000 | [diff] [blame] | 2000 | _PyTrash_destroy_chain(void) |
Guido van Rossum | d724b23 | 2000-03-13 16:01:29 +0000 | [diff] [blame] | 2001 | { |
| 2002 | while (_PyTrash_delete_later) { |
| 2003 | PyObject *shredder = _PyTrash_delete_later; |
Guido van Rossum | e92e610 | 2000-04-24 15:40:53 +0000 | [diff] [blame] | 2004 | _PyTrash_delete_later = (PyObject*) shredder->ob_type; |
| 2005 | |
| 2006 | switch (shredder->ob_refcnt) { |
| 2007 | case Py_TRASHCAN_TUPLE: |
| 2008 | shredder->ob_type = &PyTuple_Type; |
| 2009 | break; |
| 2010 | case Py_TRASHCAN_LIST: |
| 2011 | shredder->ob_type = &PyList_Type; |
| 2012 | break; |
| 2013 | case Py_TRASHCAN_DICT: |
| 2014 | shredder->ob_type = &PyDict_Type; |
| 2015 | break; |
| 2016 | case Py_TRASHCAN_FRAME: |
| 2017 | shredder->ob_type = &PyFrame_Type; |
| 2018 | break; |
| 2019 | case Py_TRASHCAN_TRACEBACK: |
| 2020 | shredder->ob_type = &PyTraceBack_Type; |
| 2021 | break; |
| 2022 | } |
| 2023 | _Py_NewReference(shredder); |
| 2024 | |
Guido van Rossum | d724b23 | 2000-03-13 16:01:29 +0000 | [diff] [blame] | 2025 | ++_PyTrash_delete_nesting; |
| 2026 | Py_DECREF(shredder); |
| 2027 | --_PyTrash_delete_nesting; |
| 2028 | } |
| 2029 | } |
Neil Schemenauer | a35c688 | 2001-02-27 04:45:05 +0000 | [diff] [blame] | 2030 | |
| 2031 | #ifdef WITH_PYMALLOC |
| 2032 | #include "obmalloc.c" |
| 2033 | #endif |