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