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