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