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