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