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