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