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