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