Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 1 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 2 | /* Generic object operations; and implementation of None (NoObject) */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4 | #include "Python.h" |
Benjamin Peterson | fd838e6 | 2009-04-20 02:09:13 +0000 | [diff] [blame] | 5 | #include "frameobject.h" |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 6 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7 | #ifdef __cplusplus |
| 8 | extern "C" { |
| 9 | #endif |
| 10 | |
Tim Peters | 3459251 | 2002-07-11 06:23:50 +0000 | [diff] [blame] | 11 | #ifdef Py_REF_DEBUG |
Neal Norwitz | 84632ee | 2006-03-04 20:00:59 +0000 | [diff] [blame] | 12 | Py_ssize_t _Py_RefTotal; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 13 | |
| 14 | Py_ssize_t |
| 15 | _Py_GetRefTotal(void) |
| 16 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 17 | PyObject *o; |
| 18 | Py_ssize_t total = _Py_RefTotal; |
| 19 | /* ignore the references to the dummy object of the dicts and sets |
| 20 | because they are not reliable and not useful (now that the |
| 21 | hash table code is well-tested) */ |
| 22 | o = _PyDict_Dummy(); |
| 23 | if (o != NULL) |
| 24 | total -= o->ob_refcnt; |
| 25 | o = _PySet_Dummy(); |
| 26 | if (o != NULL) |
| 27 | total -= o->ob_refcnt; |
| 28 | return total; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 29 | } |
| 30 | #endif /* Py_REF_DEBUG */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 31 | |
Mark Hammond | a290527 | 2002-07-29 13:42:14 +0000 | [diff] [blame] | 32 | int Py_DivisionWarningFlag; |
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 |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 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 |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +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 | } |
Tim Peters | 7571a0f | 2003-03-23 17:52:28 +0000 | [diff] [blame] | 73 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 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; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 78 | /* All types are added to type_list, at least when |
| 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. */ |
Benjamin Peterson | a4a37fe | 2009-01-11 17:13:55 +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 |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 89 | dump_counts(FILE* f) |
Sjoerd Mullender | a9c3c22 | 1993-10-11 12:54:31 +0000 | [diff] [blame] | 90 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 91 | PyTypeObject *tp; |
Sjoerd Mullender | a9c3c22 | 1993-10-11 12:54:31 +0000 | [diff] [blame] | 92 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 93 | for (tp = type_list; tp; tp = tp->tp_next) |
| 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", |
| 97 | tp->tp_name, tp->tp_allocs, tp->tp_frees, |
| 98 | tp->tp_maxalloc); |
| 99 | fprintf(f, "fast tuple allocs: %" PY_FORMAT_SIZE_T "d, " |
| 100 | "empty: %" PY_FORMAT_SIZE_T "d\n", |
| 101 | fast_tuple_allocs, tuple_zero_allocs); |
| 102 | fprintf(f, "fast int allocs: pos: %" PY_FORMAT_SIZE_T "d, " |
| 103 | "neg: %" PY_FORMAT_SIZE_T "d\n", |
| 104 | quick_int_allocs, quick_neg_int_allocs); |
| 105 | fprintf(f, "null strings: %" PY_FORMAT_SIZE_T "d, " |
| 106 | "1-strings: %" PY_FORMAT_SIZE_T "d\n", |
| 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 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 113 | PyTypeObject *tp; |
| 114 | PyObject *result; |
| 115 | PyObject *v; |
Sjoerd Mullender | 6ec3c65 | 1995-08-29 09:18:14 +0000 | [diff] [blame] | 116 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 117 | result = PyList_New(0); |
| 118 | if (result == NULL) |
| 119 | return NULL; |
| 120 | for (tp = type_list; tp; tp = tp->tp_next) { |
| 121 | v = Py_BuildValue("(snnn)", tp->tp_name, tp->tp_allocs, |
| 122 | tp->tp_frees, tp->tp_maxalloc); |
| 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; |
Sjoerd Mullender | 6ec3c65 | 1995-08-29 09:18:14 +0000 | [diff] [blame] | 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 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 140 | if (tp->tp_next == NULL && tp->tp_prev == NULL) { |
| 141 | /* first time; insert in linked list */ |
| 142 | if (tp->tp_next != NULL) /* sanity check */ |
| 143 | Py_FatalError("XXX inc_count sanity check"); |
| 144 | if (type_list) |
| 145 | type_list->tp_prev = tp; |
| 146 | tp->tp_next = type_list; |
| 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); |
| 156 | type_list = tp; |
Tim Peters | 3e40c7f | 2003-03-23 03:04:32 +0000 | [diff] [blame] | 157 | #ifdef Py_TRACE_REFS |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +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 |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 163 | } |
| 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 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 168 | |
| 169 | void dec_count(PyTypeObject *tp) |
| 170 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 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 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 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 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 193 | char buf[300]; |
Tim Peters | 7c321a8 | 2002-07-09 02:57:01 +0000 | [diff] [blame] | 194 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 195 | PyOS_snprintf(buf, sizeof(buf), |
| 196 | "%s:%i object at %p has negative ref count " |
| 197 | "%" PY_FORMAT_SIZE_T "d", |
| 198 | fname, lineno, op, op->ob_refcnt); |
| 199 | Py_FatalError(buf); |
Tim Peters | 7c321a8 | 2002-07-09 02:57:01 +0000 | [diff] [blame] | 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 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 219 | if (op == NULL) |
| 220 | return PyErr_NoMemory(); |
| 221 | /* Any changes should be reflected in PyObject_INIT (objimpl.h) */ |
| 222 | Py_TYPE(op) = tp; |
| 223 | _Py_NewReference(op); |
| 224 | return op; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 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 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 230 | if (op == NULL) |
| 231 | return (PyVarObject *) PyErr_NoMemory(); |
| 232 | /* Any changes should be reflected in PyObject_INIT_VAR */ |
| 233 | op->ob_size = size; |
| 234 | Py_TYPE(op) = tp; |
| 235 | _Py_NewReference((PyObject *)op); |
| 236 | return op; |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 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 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 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); |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 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 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 252 | PyVarObject *op; |
| 253 | const size_t size = _PyObject_VAR_SIZE(tp, nitems); |
| 254 | op = (PyVarObject *) PyObject_MALLOC(size); |
| 255 | if (op == NULL) |
| 256 | return (PyVarObject *)PyErr_NoMemory(); |
| 257 | return PyObject_INIT_VAR(op, tp, nitems); |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 258 | } |
| 259 | |
Antoine Pitrou | c47bd4a | 2010-07-27 22:08:27 +0000 | [diff] [blame] | 260 | int |
| 261 | PyObject_Print(PyObject *op, FILE *fp, int flags) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 262 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 263 | int ret = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 264 | if (PyErr_CheckSignals()) |
| 265 | return -1; |
Guido van Rossum | 9b00dfa | 1998-04-28 16:06:54 +0000 | [diff] [blame] | 266 | #ifdef USE_STACKCHECK |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 267 | if (PyOS_CheckStack()) { |
| 268 | PyErr_SetString(PyExc_MemoryError, "stack overflow"); |
| 269 | return -1; |
| 270 | } |
Guido van Rossum | 9b00dfa | 1998-04-28 16:06:54 +0000 | [diff] [blame] | 271 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 272 | clearerr(fp); /* Clear any previous error condition */ |
| 273 | if (op == NULL) { |
| 274 | Py_BEGIN_ALLOW_THREADS |
| 275 | fprintf(fp, "<nil>"); |
| 276 | Py_END_ALLOW_THREADS |
| 277 | } |
| 278 | else { |
| 279 | if (op->ob_refcnt <= 0) |
| 280 | /* XXX(twouters) cast refcount to long until %zd is |
| 281 | universally available */ |
| 282 | Py_BEGIN_ALLOW_THREADS |
| 283 | fprintf(fp, "<refcnt %ld at %p>", |
| 284 | (long)op->ob_refcnt, op); |
| 285 | Py_END_ALLOW_THREADS |
| 286 | else { |
| 287 | PyObject *s; |
| 288 | if (flags & Py_PRINT_RAW) |
| 289 | s = PyObject_Str(op); |
| 290 | else |
| 291 | s = PyObject_Repr(op); |
| 292 | if (s == NULL) |
| 293 | ret = -1; |
| 294 | else if (PyBytes_Check(s)) { |
| 295 | fwrite(PyBytes_AS_STRING(s), 1, |
| 296 | PyBytes_GET_SIZE(s), fp); |
| 297 | } |
| 298 | else if (PyUnicode_Check(s)) { |
| 299 | PyObject *t; |
Victor Stinner | 372ac5e | 2010-05-17 01:26:01 +0000 | [diff] [blame] | 300 | t = PyUnicode_EncodeUTF8(PyUnicode_AS_UNICODE(s), |
| 301 | PyUnicode_GET_SIZE(s), |
| 302 | "backslashreplace"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 303 | if (t == NULL) |
| 304 | ret = 0; |
| 305 | else { |
| 306 | fwrite(PyBytes_AS_STRING(t), 1, |
| 307 | PyBytes_GET_SIZE(t), fp); |
Victor Stinner | ba6b430 | 2010-05-17 09:33:42 +0000 | [diff] [blame] | 308 | Py_DECREF(t); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 309 | } |
| 310 | } |
| 311 | else { |
| 312 | PyErr_Format(PyExc_TypeError, |
| 313 | "str() or repr() returned '%.100s'", |
| 314 | s->ob_type->tp_name); |
| 315 | ret = -1; |
| 316 | } |
| 317 | Py_XDECREF(s); |
| 318 | } |
| 319 | } |
| 320 | if (ret == 0) { |
| 321 | if (ferror(fp)) { |
| 322 | PyErr_SetFromErrno(PyExc_IOError); |
| 323 | clearerr(fp); |
| 324 | ret = -1; |
| 325 | } |
| 326 | } |
| 327 | return ret; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 328 | } |
| 329 | |
Guido van Rossum | 3893815 | 2006-08-21 23:36:26 +0000 | [diff] [blame] | 330 | /* For debugging convenience. Set a breakpoint here and call it from your DLL */ |
| 331 | void |
Thomas Wouters | b213704 | 2007-02-01 18:02:27 +0000 | [diff] [blame] | 332 | _Py_BreakPoint(void) |
Guido van Rossum | 3893815 | 2006-08-21 23:36:26 +0000 | [diff] [blame] | 333 | { |
| 334 | } |
| 335 | |
Neal Norwitz | 1a99750 | 2003-01-13 20:13:12 +0000 | [diff] [blame] | 336 | |
Barry Warsaw | 9bf1644 | 2001-01-23 16:24:35 +0000 | [diff] [blame] | 337 | /* For debugging convenience. See Misc/gdbinit for some useful gdb hooks */ |
Guido van Rossum | 3893815 | 2006-08-21 23:36:26 +0000 | [diff] [blame] | 338 | void |
| 339 | _PyObject_Dump(PyObject* op) |
Barry Warsaw | 9bf1644 | 2001-01-23 16:24:35 +0000 | [diff] [blame] | 340 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 341 | if (op == NULL) |
| 342 | fprintf(stderr, "NULL\n"); |
| 343 | else { |
Georg Brandl | dfd7344 | 2009-04-05 11:47:34 +0000 | [diff] [blame] | 344 | #ifdef WITH_THREAD |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 345 | PyGILState_STATE gil; |
Georg Brandl | dfd7344 | 2009-04-05 11:47:34 +0000 | [diff] [blame] | 346 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 347 | fprintf(stderr, "object : "); |
Georg Brandl | dfd7344 | 2009-04-05 11:47:34 +0000 | [diff] [blame] | 348 | #ifdef WITH_THREAD |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 349 | gil = PyGILState_Ensure(); |
Georg Brandl | dfd7344 | 2009-04-05 11:47:34 +0000 | [diff] [blame] | 350 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 351 | (void)PyObject_Print(op, stderr, 0); |
Georg Brandl | dfd7344 | 2009-04-05 11:47:34 +0000 | [diff] [blame] | 352 | #ifdef WITH_THREAD |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 353 | PyGILState_Release(gil); |
Georg Brandl | dfd7344 | 2009-04-05 11:47:34 +0000 | [diff] [blame] | 354 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 355 | /* XXX(twouters) cast refcount to long until %zd is |
| 356 | universally available */ |
| 357 | fprintf(stderr, "\n" |
| 358 | "type : %s\n" |
| 359 | "refcount: %ld\n" |
| 360 | "address : %p\n", |
| 361 | Py_TYPE(op)==NULL ? "NULL" : Py_TYPE(op)->tp_name, |
| 362 | (long)op->ob_refcnt, |
| 363 | op); |
| 364 | } |
Barry Warsaw | 9bf1644 | 2001-01-23 16:24:35 +0000 | [diff] [blame] | 365 | } |
Barry Warsaw | 903138f | 2001-01-23 16:33:18 +0000 | [diff] [blame] | 366 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 367 | PyObject * |
Fred Drake | 100814d | 2000-07-09 15:48:49 +0000 | [diff] [blame] | 368 | PyObject_Repr(PyObject *v) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 369 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 370 | PyObject *res; |
| 371 | if (PyErr_CheckSignals()) |
| 372 | return NULL; |
Guido van Rossum | 9b00dfa | 1998-04-28 16:06:54 +0000 | [diff] [blame] | 373 | #ifdef USE_STACKCHECK |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 374 | if (PyOS_CheckStack()) { |
| 375 | PyErr_SetString(PyExc_MemoryError, "stack overflow"); |
| 376 | return NULL; |
| 377 | } |
Guido van Rossum | 9b00dfa | 1998-04-28 16:06:54 +0000 | [diff] [blame] | 378 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 379 | if (v == NULL) |
| 380 | return PyUnicode_FromString("<NULL>"); |
| 381 | if (Py_TYPE(v)->tp_repr == NULL) |
| 382 | return PyUnicode_FromFormat("<%s object at %p>", |
| 383 | v->ob_type->tp_name, v); |
| 384 | res = (*v->ob_type->tp_repr)(v); |
| 385 | if (res != NULL && !PyUnicode_Check(res)) { |
| 386 | PyErr_Format(PyExc_TypeError, |
| 387 | "__repr__ returned non-string (type %.200s)", |
| 388 | res->ob_type->tp_name); |
| 389 | Py_DECREF(res); |
| 390 | return NULL; |
| 391 | } |
| 392 | return res; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 393 | } |
| 394 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 395 | PyObject * |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 396 | PyObject_Str(PyObject *v) |
Guido van Rossum | c600411 | 1993-11-05 10:22:19 +0000 | [diff] [blame] | 397 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 398 | PyObject *res; |
| 399 | if (PyErr_CheckSignals()) |
| 400 | return NULL; |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 401 | #ifdef USE_STACKCHECK |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 402 | if (PyOS_CheckStack()) { |
| 403 | PyErr_SetString(PyExc_MemoryError, "stack overflow"); |
| 404 | return NULL; |
| 405 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 406 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 407 | if (v == NULL) |
| 408 | return PyUnicode_FromString("<NULL>"); |
| 409 | if (PyUnicode_CheckExact(v)) { |
| 410 | Py_INCREF(v); |
| 411 | return v; |
| 412 | } |
| 413 | if (Py_TYPE(v)->tp_str == NULL) |
| 414 | return PyObject_Repr(v); |
Guido van Rossum | 4f288ab | 2001-05-01 16:53:37 +0000 | [diff] [blame] | 415 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 416 | /* It is possible for a type to have a tp_str representation that loops |
| 417 | infinitely. */ |
| 418 | if (Py_EnterRecursiveCall(" while getting the str of an object")) |
| 419 | return NULL; |
| 420 | res = (*Py_TYPE(v)->tp_str)(v); |
| 421 | Py_LeaveRecursiveCall(); |
| 422 | if (res == NULL) |
| 423 | return NULL; |
| 424 | if (!PyUnicode_Check(res)) { |
| 425 | PyErr_Format(PyExc_TypeError, |
| 426 | "__str__ returned non-string (type %.200s)", |
| 427 | Py_TYPE(res)->tp_name); |
| 428 | Py_DECREF(res); |
| 429 | return NULL; |
| 430 | } |
| 431 | return res; |
Neil Schemenauer | cf52c07 | 2005-08-12 17:34:58 +0000 | [diff] [blame] | 432 | } |
| 433 | |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 434 | PyObject * |
| 435 | PyObject_ASCII(PyObject *v) |
| 436 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 437 | PyObject *repr, *ascii, *res; |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 438 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 439 | repr = PyObject_Repr(v); |
| 440 | if (repr == NULL) |
| 441 | return NULL; |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 442 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 443 | /* repr is guaranteed to be a PyUnicode object by PyObject_Repr */ |
| 444 | ascii = PyUnicode_EncodeASCII( |
| 445 | PyUnicode_AS_UNICODE(repr), |
| 446 | PyUnicode_GET_SIZE(repr), |
| 447 | "backslashreplace"); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 448 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 449 | Py_DECREF(repr); |
| 450 | if (ascii == NULL) |
| 451 | return NULL; |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 452 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 453 | res = PyUnicode_DecodeASCII( |
| 454 | PyBytes_AS_STRING(ascii), |
| 455 | PyBytes_GET_SIZE(ascii), |
| 456 | NULL); |
| 457 | |
| 458 | Py_DECREF(ascii); |
| 459 | return res; |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 460 | } |
Guido van Rossum | a3af41d | 2001-01-18 22:07:06 +0000 | [diff] [blame] | 461 | |
Benjamin Peterson | c15a073 | 2008-08-26 16:46:47 +0000 | [diff] [blame] | 462 | PyObject * |
| 463 | PyObject_Bytes(PyObject *v) |
| 464 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 465 | PyObject *result, *func; |
| 466 | static PyObject *bytesstring = NULL; |
Benjamin Peterson | c15a073 | 2008-08-26 16:46:47 +0000 | [diff] [blame] | 467 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 468 | if (v == NULL) |
| 469 | return PyBytes_FromString("<NULL>"); |
Benjamin Peterson | c15a073 | 2008-08-26 16:46:47 +0000 | [diff] [blame] | 470 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 471 | if (PyBytes_CheckExact(v)) { |
| 472 | Py_INCREF(v); |
| 473 | return v; |
| 474 | } |
Benjamin Peterson | c15a073 | 2008-08-26 16:46:47 +0000 | [diff] [blame] | 475 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 476 | func = _PyObject_LookupSpecial(v, "__bytes__", &bytesstring); |
| 477 | if (func != NULL) { |
| 478 | result = PyObject_CallFunctionObjArgs(func, NULL); |
| 479 | Py_DECREF(func); |
| 480 | if (result == NULL) |
Benjamin Peterson | 41ece39 | 2010-09-11 16:39:57 +0000 | [diff] [blame] | 481 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 482 | if (!PyBytes_Check(result)) { |
Benjamin Peterson | 41ece39 | 2010-09-11 16:39:57 +0000 | [diff] [blame] | 483 | PyErr_Format(PyExc_TypeError, |
| 484 | "__bytes__ returned non-bytes (type %.200s)", |
| 485 | Py_TYPE(result)->tp_name); |
| 486 | Py_DECREF(result); |
| 487 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 488 | } |
| 489 | return result; |
| 490 | } |
| 491 | else if (PyErr_Occurred()) |
| 492 | return NULL; |
| 493 | return PyBytes_FromObject(v); |
Benjamin Peterson | c15a073 | 2008-08-26 16:46:47 +0000 | [diff] [blame] | 494 | } |
| 495 | |
Mark Dickinson | c008a17 | 2009-02-01 13:59:22 +0000 | [diff] [blame] | 496 | /* For Python 3.0.1 and later, the old three-way comparison has been |
| 497 | completely removed in favour of rich comparisons. PyObject_Compare() and |
| 498 | PyObject_Cmp() are gone, and the builtin cmp function no longer exists. |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 499 | The old tp_compare slot has been renamed to tp_reserved, and should no |
Mark Dickinson | c008a17 | 2009-02-01 13:59:22 +0000 | [diff] [blame] | 500 | longer be used. Use tp_richcompare instead. |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 501 | |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 502 | See (*) below for practical amendments. |
| 503 | |
Mark Dickinson | c008a17 | 2009-02-01 13:59:22 +0000 | [diff] [blame] | 504 | tp_richcompare gets called with a first argument of the appropriate type |
| 505 | and a second object of an arbitrary type. We never do any kind of |
| 506 | coercion. |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 507 | |
Mark Dickinson | c008a17 | 2009-02-01 13:59:22 +0000 | [diff] [blame] | 508 | The tp_richcompare slot should return an object, as follows: |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 509 | |
| 510 | NULL if an exception occurred |
| 511 | NotImplemented if the requested comparison is not implemented |
| 512 | any other false value if the requested comparison is false |
| 513 | any other true value if the requested comparison is true |
| 514 | |
| 515 | The PyObject_RichCompare[Bool]() wrappers raise TypeError when they get |
| 516 | NotImplemented. |
| 517 | |
| 518 | (*) Practical amendments: |
| 519 | |
| 520 | - If rich comparison returns NotImplemented, == and != are decided by |
| 521 | comparing the object pointer (i.e. falling back to the base object |
| 522 | implementation). |
| 523 | |
Guido van Rossum | a407300 | 2002-05-31 20:03:54 +0000 | [diff] [blame] | 524 | */ |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 525 | |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 526 | /* Map rich comparison operators to their swapped version, e.g. LT <--> GT */ |
Brett Cannon | a5ca2e7 | 2004-09-25 01:37:24 +0000 | [diff] [blame] | 527 | 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] | 528 | |
Guido van Rossum | 9a4e95c | 2006-12-19 21:35:46 +0000 | [diff] [blame] | 529 | static char *opstrings[] = {"<", "<=", "==", "!=", ">", ">="}; |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 530 | |
| 531 | /* Perform a rich comparison, raising TypeError when the requested comparison |
| 532 | operator is not supported. */ |
Guido van Rossum | e797ec1 | 2001-01-17 15:24:28 +0000 | [diff] [blame] | 533 | static PyObject * |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 534 | do_richcompare(PyObject *v, PyObject *w, int op) |
Guido van Rossum | e797ec1 | 2001-01-17 15:24:28 +0000 | [diff] [blame] | 535 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 536 | richcmpfunc f; |
| 537 | PyObject *res; |
| 538 | int checked_reverse_op = 0; |
Guido van Rossum | e797ec1 | 2001-01-17 15:24:28 +0000 | [diff] [blame] | 539 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 540 | if (v->ob_type != w->ob_type && |
| 541 | PyType_IsSubtype(w->ob_type, v->ob_type) && |
| 542 | (f = w->ob_type->tp_richcompare) != NULL) { |
| 543 | checked_reverse_op = 1; |
| 544 | res = (*f)(w, v, _Py_SwappedOp[op]); |
| 545 | if (res != Py_NotImplemented) |
| 546 | return res; |
| 547 | Py_DECREF(res); |
| 548 | } |
| 549 | if ((f = v->ob_type->tp_richcompare) != NULL) { |
| 550 | res = (*f)(v, w, op); |
| 551 | if (res != Py_NotImplemented) |
| 552 | return res; |
| 553 | Py_DECREF(res); |
| 554 | } |
| 555 | if (!checked_reverse_op && (f = w->ob_type->tp_richcompare) != NULL) { |
| 556 | res = (*f)(w, v, _Py_SwappedOp[op]); |
| 557 | if (res != Py_NotImplemented) |
| 558 | return res; |
| 559 | Py_DECREF(res); |
| 560 | } |
| 561 | /* If neither object implements it, provide a sensible default |
| 562 | for == and !=, but raise an exception for ordering. */ |
| 563 | switch (op) { |
| 564 | case Py_EQ: |
| 565 | res = (v == w) ? Py_True : Py_False; |
| 566 | break; |
| 567 | case Py_NE: |
| 568 | res = (v != w) ? Py_True : Py_False; |
| 569 | break; |
| 570 | default: |
| 571 | /* XXX Special-case None so it doesn't show as NoneType() */ |
| 572 | PyErr_Format(PyExc_TypeError, |
| 573 | "unorderable types: %.100s() %s %.100s()", |
| 574 | v->ob_type->tp_name, |
| 575 | opstrings[op], |
| 576 | w->ob_type->tp_name); |
| 577 | return NULL; |
| 578 | } |
| 579 | Py_INCREF(res); |
| 580 | return res; |
Guido van Rossum | e797ec1 | 2001-01-17 15:24:28 +0000 | [diff] [blame] | 581 | } |
| 582 | |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 583 | /* Perform a rich comparison with object result. This wraps do_richcompare() |
| 584 | with a check for NULL arguments and a recursion check. */ |
Guido van Rossum | e797ec1 | 2001-01-17 15:24:28 +0000 | [diff] [blame] | 585 | |
Guido van Rossum | e797ec1 | 2001-01-17 15:24:28 +0000 | [diff] [blame] | 586 | PyObject * |
| 587 | PyObject_RichCompare(PyObject *v, PyObject *w, int op) |
| 588 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 589 | PyObject *res; |
Guido van Rossum | e797ec1 | 2001-01-17 15:24:28 +0000 | [diff] [blame] | 590 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 591 | assert(Py_LT <= op && op <= Py_GE); |
| 592 | if (v == NULL || w == NULL) { |
| 593 | if (!PyErr_Occurred()) |
| 594 | PyErr_BadInternalCall(); |
| 595 | return NULL; |
| 596 | } |
| 597 | if (Py_EnterRecursiveCall(" in comparison")) |
| 598 | return NULL; |
| 599 | res = do_richcompare(v, w, op); |
| 600 | Py_LeaveRecursiveCall(); |
| 601 | return res; |
Guido van Rossum | e797ec1 | 2001-01-17 15:24:28 +0000 | [diff] [blame] | 602 | } |
| 603 | |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 604 | /* Perform a rich comparison with integer result. This wraps |
| 605 | PyObject_RichCompare(), returning -1 for error, 0 for false, 1 for true. */ |
Guido van Rossum | e797ec1 | 2001-01-17 15:24:28 +0000 | [diff] [blame] | 606 | int |
| 607 | PyObject_RichCompareBool(PyObject *v, PyObject *w, int op) |
| 608 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 609 | PyObject *res; |
| 610 | int ok; |
Guido van Rossum | e797ec1 | 2001-01-17 15:24:28 +0000 | [diff] [blame] | 611 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 612 | /* Quick result when objects are the same. |
| 613 | Guarantees that identity implies equality. */ |
| 614 | if (v == w) { |
| 615 | if (op == Py_EQ) |
| 616 | return 1; |
| 617 | else if (op == Py_NE) |
| 618 | return 0; |
| 619 | } |
Mark Dickinson | 4a1f593 | 2008-11-12 23:23:36 +0000 | [diff] [blame] | 620 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 621 | res = PyObject_RichCompare(v, w, op); |
| 622 | if (res == NULL) |
| 623 | return -1; |
| 624 | if (PyBool_Check(res)) |
| 625 | ok = (res == Py_True); |
| 626 | else |
| 627 | ok = PyObject_IsTrue(res); |
| 628 | Py_DECREF(res); |
| 629 | return ok; |
Guido van Rossum | e797ec1 | 2001-01-17 15:24:28 +0000 | [diff] [blame] | 630 | } |
Fred Drake | 13634cf | 2000-06-29 19:17:04 +0000 | [diff] [blame] | 631 | |
| 632 | /* Set of hash utility functions to help maintaining the invariant that |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 633 | if a==b then hash(a)==hash(b) |
Fred Drake | 13634cf | 2000-06-29 19:17:04 +0000 | [diff] [blame] | 634 | |
| 635 | All the utility functions (_Py_Hash*()) return "-1" to signify an error. |
| 636 | */ |
| 637 | |
Mark Dickinson | dc787d2 | 2010-05-23 13:33:13 +0000 | [diff] [blame] | 638 | /* For numeric types, the hash of a number x is based on the reduction |
| 639 | of x modulo the prime P = 2**_PyHASH_BITS - 1. It's designed so that |
| 640 | hash(x) == hash(y) whenever x and y are numerically equal, even if |
| 641 | x and y have different types. |
| 642 | |
| 643 | A quick summary of the hashing strategy: |
| 644 | |
| 645 | (1) First define the 'reduction of x modulo P' for any rational |
| 646 | number x; this is a standard extension of the usual notion of |
| 647 | reduction modulo P for integers. If x == p/q (written in lowest |
| 648 | terms), the reduction is interpreted as the reduction of p times |
| 649 | the inverse of the reduction of q, all modulo P; if q is exactly |
| 650 | divisible by P then define the reduction to be infinity. So we've |
| 651 | got a well-defined map |
| 652 | |
| 653 | reduce : { rational numbers } -> { 0, 1, 2, ..., P-1, infinity }. |
| 654 | |
| 655 | (2) Now for a rational number x, define hash(x) by: |
| 656 | |
| 657 | reduce(x) if x >= 0 |
| 658 | -reduce(-x) if x < 0 |
| 659 | |
| 660 | If the result of the reduction is infinity (this is impossible for |
| 661 | integers, floats and Decimals) then use the predefined hash value |
| 662 | _PyHASH_INF for x >= 0, or -_PyHASH_INF for x < 0, instead. |
| 663 | _PyHASH_INF, -_PyHASH_INF and _PyHASH_NAN are also used for the |
| 664 | hashes of float and Decimal infinities and nans. |
| 665 | |
| 666 | A selling point for the above strategy is that it makes it possible |
| 667 | to compute hashes of decimal and binary floating-point numbers |
| 668 | efficiently, even if the exponent of the binary or decimal number |
| 669 | is large. The key point is that |
| 670 | |
| 671 | reduce(x * y) == reduce(x) * reduce(y) (modulo _PyHASH_MODULUS) |
| 672 | |
| 673 | provided that {reduce(x), reduce(y)} != {0, infinity}. The reduction of a |
| 674 | binary or decimal float is never infinity, since the denominator is a power |
| 675 | of 2 (for binary) or a divisor of a power of 10 (for decimal). So we have, |
| 676 | for nonnegative x, |
| 677 | |
| 678 | reduce(x * 2**e) == reduce(x) * reduce(2**e) % _PyHASH_MODULUS |
| 679 | |
| 680 | reduce(x * 10**e) == reduce(x) * reduce(10**e) % _PyHASH_MODULUS |
| 681 | |
| 682 | and reduce(10**e) can be computed efficiently by the usual modular |
| 683 | exponentiation algorithm. For reduce(2**e) it's even better: since |
| 684 | P is of the form 2**n-1, reduce(2**e) is 2**(e mod n), and multiplication |
| 685 | by 2**(e mod n) modulo 2**n-1 just amounts to a rotation of bits. |
| 686 | |
| 687 | */ |
| 688 | |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 689 | Py_hash_t |
Fred Drake | 100814d | 2000-07-09 15:48:49 +0000 | [diff] [blame] | 690 | _Py_HashDouble(double v) |
Fred Drake | 13634cf | 2000-06-29 19:17:04 +0000 | [diff] [blame] | 691 | { |
Mark Dickinson | dc787d2 | 2010-05-23 13:33:13 +0000 | [diff] [blame] | 692 | int e, sign; |
| 693 | double m; |
Benjamin Peterson | 8035bc5 | 2010-10-23 16:20:50 +0000 | [diff] [blame] | 694 | Py_uhash_t x, y; |
Tim Peters | 39dce29 | 2000-08-15 03:34:48 +0000 | [diff] [blame] | 695 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 696 | if (!Py_IS_FINITE(v)) { |
| 697 | if (Py_IS_INFINITY(v)) |
Mark Dickinson | dc787d2 | 2010-05-23 13:33:13 +0000 | [diff] [blame] | 698 | return v > 0 ? _PyHASH_INF : -_PyHASH_INF; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 699 | else |
Mark Dickinson | dc787d2 | 2010-05-23 13:33:13 +0000 | [diff] [blame] | 700 | return _PyHASH_NAN; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 701 | } |
Mark Dickinson | dc787d2 | 2010-05-23 13:33:13 +0000 | [diff] [blame] | 702 | |
| 703 | m = frexp(v, &e); |
| 704 | |
| 705 | sign = 1; |
| 706 | if (m < 0) { |
| 707 | sign = -1; |
| 708 | m = -m; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 709 | } |
Mark Dickinson | dc787d2 | 2010-05-23 13:33:13 +0000 | [diff] [blame] | 710 | |
| 711 | /* process 28 bits at a time; this should work well both for binary |
| 712 | and hexadecimal floating point. */ |
| 713 | x = 0; |
| 714 | while (m) { |
| 715 | x = ((x << 28) & _PyHASH_MODULUS) | x >> (_PyHASH_BITS - 28); |
| 716 | m *= 268435456.0; /* 2**28 */ |
| 717 | e -= 28; |
Benjamin Peterson | 8035bc5 | 2010-10-23 16:20:50 +0000 | [diff] [blame] | 718 | y = (Py_uhash_t)m; /* pull out integer part */ |
Mark Dickinson | dc787d2 | 2010-05-23 13:33:13 +0000 | [diff] [blame] | 719 | m -= y; |
| 720 | x += y; |
| 721 | if (x >= _PyHASH_MODULUS) |
| 722 | x -= _PyHASH_MODULUS; |
| 723 | } |
| 724 | |
| 725 | /* adjust for the exponent; first reduce it modulo _PyHASH_BITS */ |
| 726 | e = e >= 0 ? e % _PyHASH_BITS : _PyHASH_BITS-1-((-1-e) % _PyHASH_BITS); |
| 727 | x = ((x << e) & _PyHASH_MODULUS) | x >> (_PyHASH_BITS - e); |
| 728 | |
| 729 | x = x * sign; |
Benjamin Peterson | 8035bc5 | 2010-10-23 16:20:50 +0000 | [diff] [blame] | 730 | if (x == (Py_uhash_t)-1) |
| 731 | x = (Py_uhash_t)-2; |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 732 | return (Py_hash_t)x; |
Fred Drake | 13634cf | 2000-06-29 19:17:04 +0000 | [diff] [blame] | 733 | } |
| 734 | |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 735 | Py_hash_t |
Fred Drake | 100814d | 2000-07-09 15:48:49 +0000 | [diff] [blame] | 736 | _Py_HashPointer(void *p) |
Fred Drake | 13634cf | 2000-06-29 19:17:04 +0000 | [diff] [blame] | 737 | { |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 738 | Py_hash_t x; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 739 | size_t y = (size_t)p; |
| 740 | /* bottom 3 or 4 bits are likely to be 0; rotate y by 4 to avoid |
| 741 | excessive hash collisions for dicts and sets */ |
| 742 | y = (y >> 4) | (y << (8 * SIZEOF_VOID_P - 4)); |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 743 | x = (Py_hash_t)y; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 744 | if (x == -1) |
| 745 | x = -2; |
| 746 | return x; |
Fred Drake | 13634cf | 2000-06-29 19:17:04 +0000 | [diff] [blame] | 747 | } |
| 748 | |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 749 | Py_hash_t |
Nick Coghlan | d1abd25 | 2008-07-15 15:46:38 +0000 | [diff] [blame] | 750 | PyObject_HashNotImplemented(PyObject *v) |
| 751 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 752 | PyErr_Format(PyExc_TypeError, "unhashable type: '%.200s'", |
| 753 | Py_TYPE(v)->tp_name); |
| 754 | return -1; |
Nick Coghlan | d1abd25 | 2008-07-15 15:46:38 +0000 | [diff] [blame] | 755 | } |
Fred Drake | 13634cf | 2000-06-29 19:17:04 +0000 | [diff] [blame] | 756 | |
Georg Brandl | 2daf6ae | 2012-02-20 19:54:16 +0100 | [diff] [blame] | 757 | _Py_HashSecret_t _Py_HashSecret; |
| 758 | |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 759 | Py_hash_t |
Fred Drake | 100814d | 2000-07-09 15:48:49 +0000 | [diff] [blame] | 760 | PyObject_Hash(PyObject *v) |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 761 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 762 | PyTypeObject *tp = Py_TYPE(v); |
| 763 | if (tp->tp_hash != NULL) |
| 764 | return (*tp->tp_hash)(v); |
| 765 | /* To keep to the general practice that inheriting |
| 766 | * solely from object in C code should work without |
| 767 | * an explicit call to PyType_Ready, we implicitly call |
| 768 | * PyType_Ready here and then check the tp_hash slot again |
| 769 | */ |
| 770 | if (tp->tp_dict == NULL) { |
| 771 | if (PyType_Ready(tp) < 0) |
| 772 | return -1; |
| 773 | if (tp->tp_hash != NULL) |
| 774 | return (*tp->tp_hash)(v); |
| 775 | } |
| 776 | /* Otherwise, the object can't be hashed */ |
| 777 | return PyObject_HashNotImplemented(v); |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 778 | } |
| 779 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 780 | PyObject * |
Jeremy Hylton | af68c87 | 2005-12-10 18:50:16 +0000 | [diff] [blame] | 781 | PyObject_GetAttrString(PyObject *v, const char *name) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 782 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 783 | PyObject *w, *res; |
Guido van Rossum | d8eb1b3 | 1996-08-09 20:52:03 +0000 | [diff] [blame] | 784 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 785 | if (Py_TYPE(v)->tp_getattr != NULL) |
| 786 | return (*Py_TYPE(v)->tp_getattr)(v, (char*)name); |
| 787 | w = PyUnicode_InternFromString(name); |
| 788 | if (w == NULL) |
| 789 | return NULL; |
| 790 | res = PyObject_GetAttr(v, w); |
| 791 | Py_XDECREF(w); |
| 792 | return res; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 793 | } |
| 794 | |
| 795 | int |
Jeremy Hylton | af68c87 | 2005-12-10 18:50:16 +0000 | [diff] [blame] | 796 | PyObject_HasAttrString(PyObject *v, const char *name) |
Guido van Rossum | ed18fdc | 1993-07-11 19:55:34 +0000 | [diff] [blame] | 797 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 798 | PyObject *res = PyObject_GetAttrString(v, name); |
| 799 | if (res != NULL) { |
| 800 | Py_DECREF(res); |
| 801 | return 1; |
| 802 | } |
| 803 | PyErr_Clear(); |
| 804 | return 0; |
Guido van Rossum | ed18fdc | 1993-07-11 19:55:34 +0000 | [diff] [blame] | 805 | } |
| 806 | |
| 807 | int |
Jeremy Hylton | af68c87 | 2005-12-10 18:50:16 +0000 | [diff] [blame] | 808 | PyObject_SetAttrString(PyObject *v, const char *name, PyObject *w) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 809 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 810 | PyObject *s; |
| 811 | int res; |
Guido van Rossum | d8eb1b3 | 1996-08-09 20:52:03 +0000 | [diff] [blame] | 812 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 813 | if (Py_TYPE(v)->tp_setattr != NULL) |
| 814 | return (*Py_TYPE(v)->tp_setattr)(v, (char*)name, w); |
| 815 | s = PyUnicode_InternFromString(name); |
| 816 | if (s == NULL) |
| 817 | return -1; |
| 818 | res = PyObject_SetAttr(v, s, w); |
| 819 | Py_XDECREF(s); |
| 820 | return res; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 821 | } |
| 822 | |
Guido van Rossum | 98ff96a | 1997-05-20 18:34:44 +0000 | [diff] [blame] | 823 | PyObject * |
Fred Drake | 100814d | 2000-07-09 15:48:49 +0000 | [diff] [blame] | 824 | PyObject_GetAttr(PyObject *v, PyObject *name) |
Guido van Rossum | 98ff96a | 1997-05-20 18:34:44 +0000 | [diff] [blame] | 825 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 826 | PyTypeObject *tp = Py_TYPE(v); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 827 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 828 | if (!PyUnicode_Check(name)) { |
| 829 | PyErr_Format(PyExc_TypeError, |
| 830 | "attribute name must be string, not '%.200s'", |
| 831 | name->ob_type->tp_name); |
| 832 | return NULL; |
| 833 | } |
| 834 | if (tp->tp_getattro != NULL) |
| 835 | return (*tp->tp_getattro)(v, name); |
| 836 | if (tp->tp_getattr != NULL) { |
| 837 | char *name_str = _PyUnicode_AsString(name); |
| 838 | if (name_str == NULL) |
| 839 | return NULL; |
| 840 | return (*tp->tp_getattr)(v, name_str); |
| 841 | } |
| 842 | PyErr_Format(PyExc_AttributeError, |
| 843 | "'%.50s' object has no attribute '%U'", |
| 844 | tp->tp_name, name); |
| 845 | return NULL; |
Guido van Rossum | 98ff96a | 1997-05-20 18:34:44 +0000 | [diff] [blame] | 846 | } |
| 847 | |
| 848 | int |
Fred Drake | 100814d | 2000-07-09 15:48:49 +0000 | [diff] [blame] | 849 | PyObject_HasAttr(PyObject *v, PyObject *name) |
Guido van Rossum | 98ff96a | 1997-05-20 18:34:44 +0000 | [diff] [blame] | 850 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 851 | PyObject *res = PyObject_GetAttr(v, name); |
| 852 | if (res != NULL) { |
| 853 | Py_DECREF(res); |
| 854 | return 1; |
| 855 | } |
| 856 | PyErr_Clear(); |
| 857 | return 0; |
Guido van Rossum | 98ff96a | 1997-05-20 18:34:44 +0000 | [diff] [blame] | 858 | } |
| 859 | |
| 860 | int |
Fred Drake | 100814d | 2000-07-09 15:48:49 +0000 | [diff] [blame] | 861 | PyObject_SetAttr(PyObject *v, PyObject *name, PyObject *value) |
Guido van Rossum | 98ff96a | 1997-05-20 18:34:44 +0000 | [diff] [blame] | 862 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 863 | PyTypeObject *tp = Py_TYPE(v); |
| 864 | int err; |
Marc-André Lemburg | e44e507 | 2000-09-18 16:20:57 +0000 | [diff] [blame] | 865 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 866 | if (!PyUnicode_Check(name)) { |
| 867 | PyErr_Format(PyExc_TypeError, |
| 868 | "attribute name must be string, not '%.200s'", |
| 869 | name->ob_type->tp_name); |
| 870 | return -1; |
| 871 | } |
| 872 | Py_INCREF(name); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 873 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 874 | PyUnicode_InternInPlace(&name); |
| 875 | if (tp->tp_setattro != NULL) { |
| 876 | err = (*tp->tp_setattro)(v, name, value); |
| 877 | Py_DECREF(name); |
| 878 | return err; |
| 879 | } |
| 880 | if (tp->tp_setattr != NULL) { |
| 881 | char *name_str = _PyUnicode_AsString(name); |
| 882 | if (name_str == NULL) |
| 883 | return -1; |
| 884 | err = (*tp->tp_setattr)(v, name_str, value); |
| 885 | Py_DECREF(name); |
| 886 | return err; |
| 887 | } |
| 888 | Py_DECREF(name); |
| 889 | assert(name->ob_refcnt >= 1); |
| 890 | if (tp->tp_getattr == NULL && tp->tp_getattro == NULL) |
| 891 | PyErr_Format(PyExc_TypeError, |
| 892 | "'%.100s' object has no attributes " |
| 893 | "(%s .%U)", |
| 894 | tp->tp_name, |
| 895 | value==NULL ? "del" : "assign to", |
| 896 | name); |
| 897 | else |
| 898 | PyErr_Format(PyExc_TypeError, |
| 899 | "'%.100s' object has only read-only attributes " |
| 900 | "(%s .%U)", |
| 901 | tp->tp_name, |
| 902 | value==NULL ? "del" : "assign to", |
| 903 | name); |
| 904 | return -1; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 905 | } |
| 906 | |
| 907 | /* Helper to get a pointer to an object's __dict__ slot, if any */ |
| 908 | |
| 909 | PyObject ** |
| 910 | _PyObject_GetDictPtr(PyObject *obj) |
| 911 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 912 | Py_ssize_t dictoffset; |
| 913 | PyTypeObject *tp = Py_TYPE(obj); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 914 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 915 | dictoffset = tp->tp_dictoffset; |
| 916 | if (dictoffset == 0) |
| 917 | return NULL; |
| 918 | if (dictoffset < 0) { |
| 919 | Py_ssize_t tsize; |
| 920 | size_t size; |
Guido van Rossum | 2eb0b87 | 2002-03-01 22:24:49 +0000 | [diff] [blame] | 921 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 922 | tsize = ((PyVarObject *)obj)->ob_size; |
| 923 | if (tsize < 0) |
| 924 | tsize = -tsize; |
| 925 | size = _PyObject_VAR_SIZE(tp, tsize); |
Guido van Rossum | 2eb0b87 | 2002-03-01 22:24:49 +0000 | [diff] [blame] | 926 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 927 | dictoffset += (long)size; |
| 928 | assert(dictoffset > 0); |
| 929 | assert(dictoffset % SIZEOF_VOID_P == 0); |
| 930 | } |
| 931 | return (PyObject **) ((char *)obj + dictoffset); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 932 | } |
| 933 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 934 | PyObject * |
Raymond Hettinger | 1da1dbf | 2003-03-17 19:46:11 +0000 | [diff] [blame] | 935 | PyObject_SelfIter(PyObject *obj) |
Raymond Hettinger | 0153826 | 2003-03-17 08:24:35 +0000 | [diff] [blame] | 936 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 937 | Py_INCREF(obj); |
| 938 | return obj; |
Raymond Hettinger | 0153826 | 2003-03-17 08:24:35 +0000 | [diff] [blame] | 939 | } |
| 940 | |
Amaury Forgeot d'Arc | f343e01 | 2009-01-12 23:58:21 +0000 | [diff] [blame] | 941 | /* Helper used when the __next__ method is removed from a type: |
| 942 | tp_iternext is never NULL and can be safely called without checking |
| 943 | on every iteration. |
| 944 | */ |
| 945 | |
| 946 | PyObject * |
| 947 | _PyObject_NextNotImplemented(PyObject *self) |
| 948 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 949 | PyErr_Format(PyExc_TypeError, |
| 950 | "'%.200s' object is not iterable", |
| 951 | Py_TYPE(self)->tp_name); |
| 952 | return NULL; |
Amaury Forgeot d'Arc | f343e01 | 2009-01-12 23:58:21 +0000 | [diff] [blame] | 953 | } |
| 954 | |
Michael W. Hudson | 1593f50 | 2004-09-14 17:09:47 +0000 | [diff] [blame] | 955 | /* Generic GetAttr functions - put these in your tp_[gs]etattro slot */ |
| 956 | |
Raymond Hettinger | 0153826 | 2003-03-17 08:24:35 +0000 | [diff] [blame] | 957 | PyObject * |
Antoine Pitrou | 1a9a9d5 | 2010-08-28 18:17:03 +0000 | [diff] [blame] | 958 | _PyObject_GenericGetAttrWithDict(PyObject *obj, PyObject *name, PyObject *dict) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 959 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 960 | PyTypeObject *tp = Py_TYPE(obj); |
| 961 | PyObject *descr = NULL; |
| 962 | PyObject *res = NULL; |
| 963 | descrgetfunc f; |
| 964 | Py_ssize_t dictoffset; |
| 965 | PyObject **dictptr; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 966 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 967 | if (!PyUnicode_Check(name)){ |
| 968 | PyErr_Format(PyExc_TypeError, |
| 969 | "attribute name must be string, not '%.200s'", |
| 970 | name->ob_type->tp_name); |
| 971 | return NULL; |
| 972 | } |
| 973 | else |
| 974 | Py_INCREF(name); |
Guido van Rossum | ebca9fc | 2001-12-04 15:54:53 +0000 | [diff] [blame] | 975 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 976 | if (tp->tp_dict == NULL) { |
| 977 | if (PyType_Ready(tp) < 0) |
| 978 | goto done; |
| 979 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 980 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 981 | descr = _PyType_Lookup(tp, name); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 982 | Py_XINCREF(descr); |
Michael W. Hudson | b2c7de4 | 2003-08-15 13:07:47 +0000 | [diff] [blame] | 983 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 984 | f = NULL; |
| 985 | if (descr != NULL) { |
| 986 | f = descr->ob_type->tp_descr_get; |
| 987 | if (f != NULL && PyDescr_IsData(descr)) { |
| 988 | res = f(descr, obj, (PyObject *)obj->ob_type); |
| 989 | Py_DECREF(descr); |
| 990 | goto done; |
| 991 | } |
| 992 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 993 | |
Antoine Pitrou | 1a9a9d5 | 2010-08-28 18:17:03 +0000 | [diff] [blame] | 994 | if (dict == NULL) { |
| 995 | /* Inline _PyObject_GetDictPtr */ |
| 996 | dictoffset = tp->tp_dictoffset; |
| 997 | if (dictoffset != 0) { |
| 998 | if (dictoffset < 0) { |
| 999 | Py_ssize_t tsize; |
| 1000 | size_t size; |
Guido van Rossum | c66ff44 | 2002-08-19 16:50:48 +0000 | [diff] [blame] | 1001 | |
Antoine Pitrou | 1a9a9d5 | 2010-08-28 18:17:03 +0000 | [diff] [blame] | 1002 | tsize = ((PyVarObject *)obj)->ob_size; |
| 1003 | if (tsize < 0) |
| 1004 | tsize = -tsize; |
| 1005 | size = _PyObject_VAR_SIZE(tp, tsize); |
Guido van Rossum | c66ff44 | 2002-08-19 16:50:48 +0000 | [diff] [blame] | 1006 | |
Antoine Pitrou | 1a9a9d5 | 2010-08-28 18:17:03 +0000 | [diff] [blame] | 1007 | dictoffset += (long)size; |
| 1008 | assert(dictoffset > 0); |
| 1009 | assert(dictoffset % SIZEOF_VOID_P == 0); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1010 | } |
Antoine Pitrou | 1a9a9d5 | 2010-08-28 18:17:03 +0000 | [diff] [blame] | 1011 | dictptr = (PyObject **) ((char *)obj + dictoffset); |
| 1012 | dict = *dictptr; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1013 | } |
| 1014 | } |
Antoine Pitrou | 1a9a9d5 | 2010-08-28 18:17:03 +0000 | [diff] [blame] | 1015 | if (dict != NULL) { |
| 1016 | Py_INCREF(dict); |
| 1017 | res = PyDict_GetItem(dict, name); |
| 1018 | if (res != NULL) { |
| 1019 | Py_INCREF(res); |
| 1020 | Py_XDECREF(descr); |
| 1021 | Py_DECREF(dict); |
| 1022 | goto done; |
| 1023 | } |
| 1024 | Py_DECREF(dict); |
| 1025 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1026 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1027 | if (f != NULL) { |
| 1028 | res = f(descr, obj, (PyObject *)Py_TYPE(obj)); |
| 1029 | Py_DECREF(descr); |
| 1030 | goto done; |
| 1031 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1032 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1033 | if (descr != NULL) { |
| 1034 | res = descr; |
| 1035 | /* descr was already increfed above */ |
| 1036 | goto done; |
| 1037 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1038 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1039 | PyErr_Format(PyExc_AttributeError, |
| 1040 | "'%.50s' object has no attribute '%U'", |
| 1041 | tp->tp_name, name); |
Guido van Rossum | ebca9fc | 2001-12-04 15:54:53 +0000 | [diff] [blame] | 1042 | done: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1043 | Py_DECREF(name); |
| 1044 | return res; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1045 | } |
| 1046 | |
Antoine Pitrou | 1a9a9d5 | 2010-08-28 18:17:03 +0000 | [diff] [blame] | 1047 | PyObject * |
| 1048 | PyObject_GenericGetAttr(PyObject *obj, PyObject *name) |
| 1049 | { |
| 1050 | return _PyObject_GenericGetAttrWithDict(obj, name, NULL); |
| 1051 | } |
| 1052 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1053 | int |
Antoine Pitrou | 1a9a9d5 | 2010-08-28 18:17:03 +0000 | [diff] [blame] | 1054 | _PyObject_GenericSetAttrWithDict(PyObject *obj, PyObject *name, |
| 1055 | PyObject *value, PyObject *dict) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1056 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1057 | PyTypeObject *tp = Py_TYPE(obj); |
| 1058 | PyObject *descr; |
| 1059 | descrsetfunc f; |
| 1060 | PyObject **dictptr; |
| 1061 | int res = -1; |
Guido van Rossum | ebca9fc | 2001-12-04 15:54:53 +0000 | [diff] [blame] | 1062 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1063 | if (!PyUnicode_Check(name)){ |
| 1064 | PyErr_Format(PyExc_TypeError, |
| 1065 | "attribute name must be string, not '%.200s'", |
| 1066 | name->ob_type->tp_name); |
| 1067 | return -1; |
| 1068 | } |
| 1069 | else |
| 1070 | Py_INCREF(name); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1071 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1072 | if (tp->tp_dict == NULL) { |
| 1073 | if (PyType_Ready(tp) < 0) |
| 1074 | goto done; |
| 1075 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1076 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1077 | descr = _PyType_Lookup(tp, name); |
| 1078 | f = NULL; |
| 1079 | if (descr != NULL) { |
| 1080 | f = descr->ob_type->tp_descr_set; |
| 1081 | if (f != NULL && PyDescr_IsData(descr)) { |
| 1082 | res = f(descr, obj, value); |
| 1083 | goto done; |
| 1084 | } |
| 1085 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1086 | |
Antoine Pitrou | 1a9a9d5 | 2010-08-28 18:17:03 +0000 | [diff] [blame] | 1087 | if (dict == NULL) { |
| 1088 | dictptr = _PyObject_GetDictPtr(obj); |
| 1089 | if (dictptr != NULL) { |
| 1090 | dict = *dictptr; |
| 1091 | if (dict == NULL && value != NULL) { |
| 1092 | dict = PyDict_New(); |
| 1093 | if (dict == NULL) |
| 1094 | goto done; |
| 1095 | *dictptr = dict; |
| 1096 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1097 | } |
Antoine Pitrou | 1a9a9d5 | 2010-08-28 18:17:03 +0000 | [diff] [blame] | 1098 | } |
| 1099 | if (dict != NULL) { |
| 1100 | Py_INCREF(dict); |
| 1101 | if (value == NULL) |
| 1102 | res = PyDict_DelItem(dict, name); |
| 1103 | else |
| 1104 | res = PyDict_SetItem(dict, name, value); |
| 1105 | if (res < 0 && PyErr_ExceptionMatches(PyExc_KeyError)) |
| 1106 | PyErr_SetObject(PyExc_AttributeError, name); |
| 1107 | Py_DECREF(dict); |
| 1108 | goto done; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1109 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1110 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1111 | if (f != NULL) { |
| 1112 | res = f(descr, obj, value); |
| 1113 | goto done; |
| 1114 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1115 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1116 | if (descr == NULL) { |
| 1117 | PyErr_Format(PyExc_AttributeError, |
| 1118 | "'%.100s' object has no attribute '%U'", |
| 1119 | tp->tp_name, name); |
| 1120 | goto done; |
| 1121 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1122 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1123 | PyErr_Format(PyExc_AttributeError, |
| 1124 | "'%.50s' object attribute '%U' is read-only", |
| 1125 | tp->tp_name, name); |
Guido van Rossum | ebca9fc | 2001-12-04 15:54:53 +0000 | [diff] [blame] | 1126 | done: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1127 | Py_DECREF(name); |
| 1128 | return res; |
Guido van Rossum | 98ff96a | 1997-05-20 18:34:44 +0000 | [diff] [blame] | 1129 | } |
| 1130 | |
Antoine Pitrou | 1a9a9d5 | 2010-08-28 18:17:03 +0000 | [diff] [blame] | 1131 | int |
| 1132 | PyObject_GenericSetAttr(PyObject *obj, PyObject *name, PyObject *value) |
| 1133 | { |
| 1134 | return _PyObject_GenericSetAttrWithDict(obj, name, value, NULL); |
| 1135 | } |
| 1136 | |
| 1137 | |
Guido van Rossum | 6ac258d | 1993-05-12 08:24:20 +0000 | [diff] [blame] | 1138 | /* Test a value used as condition, e.g., in a for or if statement. |
| 1139 | Return -1 if an error occurred */ |
| 1140 | |
| 1141 | int |
Fred Drake | 100814d | 2000-07-09 15:48:49 +0000 | [diff] [blame] | 1142 | PyObject_IsTrue(PyObject *v) |
Guido van Rossum | 6ac258d | 1993-05-12 08:24:20 +0000 | [diff] [blame] | 1143 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1144 | Py_ssize_t res; |
| 1145 | if (v == Py_True) |
| 1146 | return 1; |
| 1147 | if (v == Py_False) |
| 1148 | return 0; |
| 1149 | if (v == Py_None) |
| 1150 | return 0; |
| 1151 | else if (v->ob_type->tp_as_number != NULL && |
| 1152 | v->ob_type->tp_as_number->nb_bool != NULL) |
| 1153 | res = (*v->ob_type->tp_as_number->nb_bool)(v); |
| 1154 | else if (v->ob_type->tp_as_mapping != NULL && |
| 1155 | v->ob_type->tp_as_mapping->mp_length != NULL) |
| 1156 | res = (*v->ob_type->tp_as_mapping->mp_length)(v); |
| 1157 | else if (v->ob_type->tp_as_sequence != NULL && |
| 1158 | v->ob_type->tp_as_sequence->sq_length != NULL) |
| 1159 | res = (*v->ob_type->tp_as_sequence->sq_length)(v); |
| 1160 | else |
| 1161 | return 1; |
| 1162 | /* if it is negative, it should be either -1 or -2 */ |
| 1163 | 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] | 1164 | } |
| 1165 | |
Tim Peters | 803526b | 2002-07-07 05:13:56 +0000 | [diff] [blame] | 1166 | /* equivalent of 'not v' |
Guido van Rossum | c3d3f96 | 1998-04-09 17:53:59 +0000 | [diff] [blame] | 1167 | Return -1 if an error occurred */ |
| 1168 | |
| 1169 | int |
Fred Drake | 100814d | 2000-07-09 15:48:49 +0000 | [diff] [blame] | 1170 | PyObject_Not(PyObject *v) |
Guido van Rossum | c3d3f96 | 1998-04-09 17:53:59 +0000 | [diff] [blame] | 1171 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1172 | int res; |
| 1173 | res = PyObject_IsTrue(v); |
| 1174 | if (res < 0) |
| 1175 | return res; |
| 1176 | return res == 0; |
Guido van Rossum | c3d3f96 | 1998-04-09 17:53:59 +0000 | [diff] [blame] | 1177 | } |
| 1178 | |
Guido van Rossum | 49b11fe | 1995-01-26 00:38:22 +0000 | [diff] [blame] | 1179 | /* Test whether an object can be called */ |
| 1180 | |
| 1181 | int |
Fred Drake | 100814d | 2000-07-09 15:48:49 +0000 | [diff] [blame] | 1182 | PyCallable_Check(PyObject *x) |
Guido van Rossum | 49b11fe | 1995-01-26 00:38:22 +0000 | [diff] [blame] | 1183 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1184 | if (x == NULL) |
| 1185 | return 0; |
| 1186 | return x->ob_type->tp_call != NULL; |
Guido van Rossum | 49b11fe | 1995-01-26 00:38:22 +0000 | [diff] [blame] | 1187 | } |
| 1188 | |
Georg Brandl | e32b422 | 2007-03-10 22:13:27 +0000 | [diff] [blame] | 1189 | /* ------------------------- PyObject_Dir() helpers ------------------------- */ |
| 1190 | |
Tim Peters | 7eea37e | 2001-09-04 22:08:56 +0000 | [diff] [blame] | 1191 | /* Helper for PyObject_Dir. |
| 1192 | Merge the __dict__ of aclass into dict, and recursively also all |
| 1193 | the __dict__s of aclass's base classes. The order of merging isn't |
| 1194 | defined, as it's expected that only the final set of dict keys is |
| 1195 | interesting. |
| 1196 | Return 0 on success, -1 on error. |
| 1197 | */ |
| 1198 | |
| 1199 | static int |
| 1200 | merge_class_dict(PyObject* dict, PyObject* aclass) |
| 1201 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1202 | PyObject *classdict; |
| 1203 | PyObject *bases; |
Tim Peters | 7eea37e | 2001-09-04 22:08:56 +0000 | [diff] [blame] | 1204 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1205 | assert(PyDict_Check(dict)); |
| 1206 | assert(aclass); |
Tim Peters | 7eea37e | 2001-09-04 22:08:56 +0000 | [diff] [blame] | 1207 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1208 | /* Merge in the type's dict (if any). */ |
| 1209 | classdict = PyObject_GetAttrString(aclass, "__dict__"); |
| 1210 | if (classdict == NULL) |
| 1211 | PyErr_Clear(); |
| 1212 | else { |
| 1213 | int status = PyDict_Update(dict, classdict); |
| 1214 | Py_DECREF(classdict); |
| 1215 | if (status < 0) |
| 1216 | return -1; |
| 1217 | } |
Tim Peters | 7eea37e | 2001-09-04 22:08:56 +0000 | [diff] [blame] | 1218 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1219 | /* Recursively merge in the base types' (if any) dicts. */ |
| 1220 | bases = PyObject_GetAttrString(aclass, "__bases__"); |
| 1221 | if (bases == NULL) |
| 1222 | PyErr_Clear(); |
| 1223 | else { |
| 1224 | /* We have no guarantee that bases is a real tuple */ |
| 1225 | Py_ssize_t i, n; |
| 1226 | n = PySequence_Size(bases); /* This better be right */ |
| 1227 | if (n < 0) |
| 1228 | PyErr_Clear(); |
| 1229 | else { |
| 1230 | for (i = 0; i < n; i++) { |
| 1231 | int status; |
| 1232 | PyObject *base = PySequence_GetItem(bases, i); |
| 1233 | if (base == NULL) { |
| 1234 | Py_DECREF(bases); |
| 1235 | return -1; |
| 1236 | } |
| 1237 | status = merge_class_dict(dict, base); |
| 1238 | Py_DECREF(base); |
| 1239 | if (status < 0) { |
| 1240 | Py_DECREF(bases); |
| 1241 | return -1; |
| 1242 | } |
| 1243 | } |
| 1244 | } |
| 1245 | Py_DECREF(bases); |
| 1246 | } |
| 1247 | return 0; |
Tim Peters | 7eea37e | 2001-09-04 22:08:56 +0000 | [diff] [blame] | 1248 | } |
| 1249 | |
Georg Brandl | e32b422 | 2007-03-10 22:13:27 +0000 | [diff] [blame] | 1250 | /* Helper for PyObject_Dir without arguments: returns the local scope. */ |
| 1251 | static PyObject * |
Guido van Rossum | ad7d8d1 | 2007-04-13 01:39:34 +0000 | [diff] [blame] | 1252 | _dir_locals(void) |
Tim Peters | 305b585 | 2001-09-17 02:38:46 +0000 | [diff] [blame] | 1253 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1254 | PyObject *names; |
| 1255 | PyObject *locals = PyEval_GetLocals(); |
Tim Peters | 305b585 | 2001-09-17 02:38:46 +0000 | [diff] [blame] | 1256 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1257 | if (locals == NULL) { |
| 1258 | PyErr_SetString(PyExc_SystemError, "frame does not exist"); |
| 1259 | return NULL; |
| 1260 | } |
Tim Peters | 305b585 | 2001-09-17 02:38:46 +0000 | [diff] [blame] | 1261 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1262 | names = PyMapping_Keys(locals); |
| 1263 | if (!names) |
| 1264 | return NULL; |
| 1265 | if (!PyList_Check(names)) { |
| 1266 | PyErr_Format(PyExc_TypeError, |
| 1267 | "dir(): expected keys() of locals to be a list, " |
| 1268 | "not '%.200s'", Py_TYPE(names)->tp_name); |
| 1269 | Py_DECREF(names); |
| 1270 | return NULL; |
| 1271 | } |
| 1272 | /* the locals don't need to be DECREF'd */ |
| 1273 | return names; |
Georg Brandl | e32b422 | 2007-03-10 22:13:27 +0000 | [diff] [blame] | 1274 | } |
| 1275 | |
| 1276 | /* Helper for PyObject_Dir of type objects: returns __dict__ and __bases__. |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1277 | We deliberately don't suck up its __class__, as methods belonging to the |
| 1278 | metaclass would probably be more confusing than helpful. |
Georg Brandl | e32b422 | 2007-03-10 22:13:27 +0000 | [diff] [blame] | 1279 | */ |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1280 | static PyObject * |
Georg Brandl | e32b422 | 2007-03-10 22:13:27 +0000 | [diff] [blame] | 1281 | _specialized_dir_type(PyObject *obj) |
| 1282 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1283 | PyObject *result = NULL; |
| 1284 | PyObject *dict = PyDict_New(); |
Georg Brandl | e32b422 | 2007-03-10 22:13:27 +0000 | [diff] [blame] | 1285 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1286 | if (dict != NULL && merge_class_dict(dict, obj) == 0) |
| 1287 | result = PyDict_Keys(dict); |
Georg Brandl | e32b422 | 2007-03-10 22:13:27 +0000 | [diff] [blame] | 1288 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1289 | Py_XDECREF(dict); |
| 1290 | return result; |
Tim Peters | 305b585 | 2001-09-17 02:38:46 +0000 | [diff] [blame] | 1291 | } |
| 1292 | |
Georg Brandl | e32b422 | 2007-03-10 22:13:27 +0000 | [diff] [blame] | 1293 | /* Helper for PyObject_Dir of module objects: returns the module's __dict__. */ |
| 1294 | static PyObject * |
| 1295 | _specialized_dir_module(PyObject *obj) |
Tim Peters | 7eea37e | 2001-09-04 22:08:56 +0000 | [diff] [blame] | 1296 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1297 | PyObject *result = NULL; |
| 1298 | PyObject *dict = PyObject_GetAttrString(obj, "__dict__"); |
Tim Peters | 7eea37e | 2001-09-04 22:08:56 +0000 | [diff] [blame] | 1299 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1300 | if (dict != NULL) { |
| 1301 | if (PyDict_Check(dict)) |
| 1302 | result = PyDict_Keys(dict); |
| 1303 | else { |
| 1304 | const char *name = PyModule_GetName(obj); |
| 1305 | if (name) |
| 1306 | PyErr_Format(PyExc_TypeError, |
| 1307 | "%.200s.__dict__ is not a dictionary", |
| 1308 | name); |
| 1309 | } |
| 1310 | } |
Tim Peters | 7eea37e | 2001-09-04 22:08:56 +0000 | [diff] [blame] | 1311 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1312 | Py_XDECREF(dict); |
| 1313 | return result; |
Georg Brandl | e32b422 | 2007-03-10 22:13:27 +0000 | [diff] [blame] | 1314 | } |
Tim Peters | 7eea37e | 2001-09-04 22:08:56 +0000 | [diff] [blame] | 1315 | |
Georg Brandl | e32b422 | 2007-03-10 22:13:27 +0000 | [diff] [blame] | 1316 | /* Helper for PyObject_Dir of generic objects: returns __dict__, __class__, |
| 1317 | and recursively up the __class__.__bases__ chain. |
| 1318 | */ |
| 1319 | static PyObject * |
| 1320 | _generic_dir(PyObject *obj) |
| 1321 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1322 | PyObject *result = NULL; |
| 1323 | PyObject *dict = NULL; |
| 1324 | PyObject *itsclass = NULL; |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1325 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1326 | /* Get __dict__ (which may or may not be a real dict...) */ |
| 1327 | dict = PyObject_GetAttrString(obj, "__dict__"); |
| 1328 | if (dict == NULL) { |
| 1329 | PyErr_Clear(); |
| 1330 | dict = PyDict_New(); |
| 1331 | } |
| 1332 | else if (!PyDict_Check(dict)) { |
| 1333 | Py_DECREF(dict); |
| 1334 | dict = PyDict_New(); |
| 1335 | } |
| 1336 | else { |
| 1337 | /* Copy __dict__ to avoid mutating it. */ |
| 1338 | PyObject *temp = PyDict_Copy(dict); |
| 1339 | Py_DECREF(dict); |
| 1340 | dict = temp; |
| 1341 | } |
Tim Peters | 7eea37e | 2001-09-04 22:08:56 +0000 | [diff] [blame] | 1342 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1343 | if (dict == NULL) |
| 1344 | goto error; |
Tim Peters | 7eea37e | 2001-09-04 22:08:56 +0000 | [diff] [blame] | 1345 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1346 | /* Merge in attrs reachable from its class. */ |
| 1347 | itsclass = PyObject_GetAttrString(obj, "__class__"); |
| 1348 | if (itsclass == NULL) |
| 1349 | /* XXX(tomer): Perhaps fall back to obj->ob_type if no |
| 1350 | __class__ exists? */ |
| 1351 | PyErr_Clear(); |
| 1352 | else { |
| 1353 | if (merge_class_dict(dict, itsclass) != 0) |
| 1354 | goto error; |
| 1355 | } |
Georg Brandl | e32b422 | 2007-03-10 22:13:27 +0000 | [diff] [blame] | 1356 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1357 | result = PyDict_Keys(dict); |
| 1358 | /* fall through */ |
Georg Brandl | e32b422 | 2007-03-10 22:13:27 +0000 | [diff] [blame] | 1359 | error: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1360 | Py_XDECREF(itsclass); |
| 1361 | Py_XDECREF(dict); |
| 1362 | return result; |
Georg Brandl | e32b422 | 2007-03-10 22:13:27 +0000 | [diff] [blame] | 1363 | } |
| 1364 | |
| 1365 | /* Helper for PyObject_Dir: object introspection. |
| 1366 | This calls one of the above specialized versions if no __dir__ method |
| 1367 | exists. */ |
| 1368 | static PyObject * |
| 1369 | _dir_object(PyObject *obj) |
| 1370 | { |
Benjamin Peterson | 7963a35 | 2011-05-23 16:11:05 -0500 | [diff] [blame] | 1371 | PyObject *result = NULL; |
| 1372 | static PyObject *dir_str = NULL; |
| 1373 | PyObject *dirfunc = _PyObject_LookupSpecial(obj, "__dir__", &dir_str); |
Georg Brandl | e32b422 | 2007-03-10 22:13:27 +0000 | [diff] [blame] | 1374 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1375 | assert(obj); |
| 1376 | if (dirfunc == NULL) { |
Benjamin Peterson | 7963a35 | 2011-05-23 16:11:05 -0500 | [diff] [blame] | 1377 | if (PyErr_Occurred()) |
| 1378 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1379 | /* use default implementation */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1380 | if (PyModule_Check(obj)) |
| 1381 | result = _specialized_dir_module(obj); |
| 1382 | else if (PyType_Check(obj)) |
| 1383 | result = _specialized_dir_type(obj); |
| 1384 | else |
| 1385 | result = _generic_dir(obj); |
| 1386 | } |
| 1387 | else { |
| 1388 | /* use __dir__ */ |
Benjamin Peterson | 7963a35 | 2011-05-23 16:11:05 -0500 | [diff] [blame] | 1389 | result = PyObject_CallFunctionObjArgs(dirfunc, NULL); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1390 | Py_DECREF(dirfunc); |
| 1391 | if (result == NULL) |
| 1392 | return NULL; |
Georg Brandl | e32b422 | 2007-03-10 22:13:27 +0000 | [diff] [blame] | 1393 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1394 | /* result must be a list */ |
| 1395 | /* XXX(gbrandl): could also check if all items are strings */ |
| 1396 | if (!PyList_Check(result)) { |
| 1397 | PyErr_Format(PyExc_TypeError, |
| 1398 | "__dir__() must return a list, not %.200s", |
| 1399 | Py_TYPE(result)->tp_name); |
| 1400 | Py_DECREF(result); |
| 1401 | result = NULL; |
| 1402 | } |
| 1403 | } |
Georg Brandl | e32b422 | 2007-03-10 22:13:27 +0000 | [diff] [blame] | 1404 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1405 | return result; |
Georg Brandl | e32b422 | 2007-03-10 22:13:27 +0000 | [diff] [blame] | 1406 | } |
| 1407 | |
| 1408 | /* Implementation of dir() -- if obj is NULL, returns the names in the current |
| 1409 | (local) scope. Otherwise, performs introspection of the object: returns a |
| 1410 | sorted list of attribute names (supposedly) accessible from the object |
| 1411 | */ |
| 1412 | PyObject * |
| 1413 | PyObject_Dir(PyObject *obj) |
| 1414 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1415 | PyObject * result; |
Georg Brandl | e32b422 | 2007-03-10 22:13:27 +0000 | [diff] [blame] | 1416 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1417 | if (obj == NULL) |
| 1418 | /* no object -- introspect the locals */ |
| 1419 | result = _dir_locals(); |
| 1420 | else |
| 1421 | /* object -- introspect the object */ |
| 1422 | result = _dir_object(obj); |
Georg Brandl | e32b422 | 2007-03-10 22:13:27 +0000 | [diff] [blame] | 1423 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1424 | assert(result == NULL || PyList_Check(result)); |
Georg Brandl | e32b422 | 2007-03-10 22:13:27 +0000 | [diff] [blame] | 1425 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1426 | if (result != NULL && PyList_Sort(result) != 0) { |
| 1427 | /* sorting the list failed */ |
| 1428 | Py_DECREF(result); |
| 1429 | result = NULL; |
| 1430 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1431 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1432 | return result; |
Tim Peters | 7eea37e | 2001-09-04 22:08:56 +0000 | [diff] [blame] | 1433 | } |
Guido van Rossum | 49b11fe | 1995-01-26 00:38:22 +0000 | [diff] [blame] | 1434 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1435 | /* |
| 1436 | NoObject is usable as a non-NULL undefined value, used by the macro None. |
| 1437 | 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] | 1438 | so there is exactly one (which is indestructible, by the way). |
Guido van Rossum | ba21a49 | 2001-08-16 08:17:26 +0000 | [diff] [blame] | 1439 | (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] | 1440 | */ |
| 1441 | |
Guido van Rossum | 0c182a1 | 1992-03-27 17:26:13 +0000 | [diff] [blame] | 1442 | /* ARGSUSED */ |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1443 | static PyObject * |
Fred Drake | 100814d | 2000-07-09 15:48:49 +0000 | [diff] [blame] | 1444 | none_repr(PyObject *op) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1445 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1446 | return PyUnicode_FromString("None"); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1447 | } |
| 1448 | |
Barry Warsaw | 9bf1644 | 2001-01-23 16:24:35 +0000 | [diff] [blame] | 1449 | /* ARGUSED */ |
| 1450 | static void |
Tim Peters | 803526b | 2002-07-07 05:13:56 +0000 | [diff] [blame] | 1451 | none_dealloc(PyObject* ignore) |
Barry Warsaw | 9bf1644 | 2001-01-23 16:24:35 +0000 | [diff] [blame] | 1452 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1453 | /* This should never get called, but we also don't want to SEGV if |
| 1454 | * we accidentally decref None out of existence. |
| 1455 | */ |
| 1456 | Py_FatalError("deallocating None"); |
Barry Warsaw | 9bf1644 | 2001-01-23 16:24:35 +0000 | [diff] [blame] | 1457 | } |
| 1458 | |
| 1459 | |
Guido van Rossum | ba21a49 | 2001-08-16 08:17:26 +0000 | [diff] [blame] | 1460 | static PyTypeObject PyNone_Type = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1461 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 1462 | "NoneType", |
| 1463 | 0, |
| 1464 | 0, |
| 1465 | none_dealloc, /*tp_dealloc*/ /*never called*/ |
| 1466 | 0, /*tp_print*/ |
| 1467 | 0, /*tp_getattr*/ |
| 1468 | 0, /*tp_setattr*/ |
| 1469 | 0, /*tp_reserved*/ |
| 1470 | none_repr, /*tp_repr*/ |
| 1471 | 0, /*tp_as_number*/ |
| 1472 | 0, /*tp_as_sequence*/ |
| 1473 | 0, /*tp_as_mapping*/ |
| 1474 | 0, /*tp_hash */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1475 | }; |
| 1476 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1477 | PyObject _Py_NoneStruct = { |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 1478 | _PyObject_EXTRA_INIT |
| 1479 | 1, &PyNone_Type |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1480 | }; |
| 1481 | |
Neil Schemenauer | 5ed85ec | 2001-01-04 01:48:10 +0000 | [diff] [blame] | 1482 | /* NotImplemented is an object that can be used to signal that an |
| 1483 | operation is not implemented for the given type combination. */ |
| 1484 | |
| 1485 | static PyObject * |
| 1486 | NotImplemented_repr(PyObject *op) |
| 1487 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1488 | return PyUnicode_FromString("NotImplemented"); |
Neil Schemenauer | 5ed85ec | 2001-01-04 01:48:10 +0000 | [diff] [blame] | 1489 | } |
| 1490 | |
| 1491 | static PyTypeObject PyNotImplemented_Type = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1492 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 1493 | "NotImplementedType", |
| 1494 | 0, |
| 1495 | 0, |
| 1496 | none_dealloc, /*tp_dealloc*/ /*never called*/ |
| 1497 | 0, /*tp_print*/ |
| 1498 | 0, /*tp_getattr*/ |
| 1499 | 0, /*tp_setattr*/ |
| 1500 | 0, /*tp_reserved*/ |
| 1501 | NotImplemented_repr, /*tp_repr*/ |
| 1502 | 0, /*tp_as_number*/ |
| 1503 | 0, /*tp_as_sequence*/ |
| 1504 | 0, /*tp_as_mapping*/ |
| 1505 | 0, /*tp_hash */ |
Neil Schemenauer | 5ed85ec | 2001-01-04 01:48:10 +0000 | [diff] [blame] | 1506 | }; |
| 1507 | |
| 1508 | PyObject _Py_NotImplementedStruct = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1509 | _PyObject_EXTRA_INIT |
| 1510 | 1, &PyNotImplemented_Type |
Neil Schemenauer | 5ed85ec | 2001-01-04 01:48:10 +0000 | [diff] [blame] | 1511 | }; |
| 1512 | |
Guido van Rossum | ba21a49 | 2001-08-16 08:17:26 +0000 | [diff] [blame] | 1513 | void |
| 1514 | _Py_ReadyTypes(void) |
| 1515 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1516 | if (PyType_Ready(&PyType_Type) < 0) |
| 1517 | Py_FatalError("Can't initialize type type"); |
Guido van Rossum | ba21a49 | 2001-08-16 08:17:26 +0000 | [diff] [blame] | 1518 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1519 | if (PyType_Ready(&_PyWeakref_RefType) < 0) |
| 1520 | Py_FatalError("Can't initialize weakref type"); |
Fred Drake | 0a4dd39 | 2004-07-02 18:57:45 +0000 | [diff] [blame] | 1521 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1522 | if (PyType_Ready(&_PyWeakref_CallableProxyType) < 0) |
| 1523 | Py_FatalError("Can't initialize callable weakref proxy type"); |
Benjamin Peterson | fd838e6 | 2009-04-20 02:09:13 +0000 | [diff] [blame] | 1524 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1525 | if (PyType_Ready(&_PyWeakref_ProxyType) < 0) |
| 1526 | Py_FatalError("Can't initialize weakref proxy type"); |
Benjamin Peterson | fd838e6 | 2009-04-20 02:09:13 +0000 | [diff] [blame] | 1527 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1528 | if (PyType_Ready(&PyBool_Type) < 0) |
| 1529 | Py_FatalError("Can't initialize bool type"); |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 1530 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1531 | if (PyType_Ready(&PyByteArray_Type) < 0) |
| 1532 | Py_FatalError("Can't initialize bytearray type"); |
Guido van Rossum | 4dfe8a1 | 2006-04-22 23:28:04 +0000 | [diff] [blame] | 1533 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1534 | if (PyType_Ready(&PyBytes_Type) < 0) |
| 1535 | Py_FatalError("Can't initialize 'str'"); |
Guido van Rossum | cacfc07 | 2002-05-24 19:01:59 +0000 | [diff] [blame] | 1536 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1537 | if (PyType_Ready(&PyList_Type) < 0) |
| 1538 | Py_FatalError("Can't initialize list type"); |
Guido van Rossum | ba21a49 | 2001-08-16 08:17:26 +0000 | [diff] [blame] | 1539 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1540 | if (PyType_Ready(&PyNone_Type) < 0) |
| 1541 | Py_FatalError("Can't initialize None type"); |
Guido van Rossum | ba21a49 | 2001-08-16 08:17:26 +0000 | [diff] [blame] | 1542 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1543 | if (PyType_Ready(&PyNotImplemented_Type) < 0) |
| 1544 | Py_FatalError("Can't initialize NotImplemented type"); |
Benjamin Peterson | ae937c0 | 2009-04-18 20:54:08 +0000 | [diff] [blame] | 1545 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1546 | if (PyType_Ready(&PyTraceBack_Type) < 0) |
| 1547 | Py_FatalError("Can't initialize traceback type"); |
Benjamin Peterson | ae937c0 | 2009-04-18 20:54:08 +0000 | [diff] [blame] | 1548 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1549 | if (PyType_Ready(&PySuper_Type) < 0) |
| 1550 | Py_FatalError("Can't initialize super type"); |
Benjamin Peterson | ae937c0 | 2009-04-18 20:54:08 +0000 | [diff] [blame] | 1551 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1552 | if (PyType_Ready(&PyBaseObject_Type) < 0) |
| 1553 | Py_FatalError("Can't initialize object type"); |
Benjamin Peterson | ae937c0 | 2009-04-18 20:54:08 +0000 | [diff] [blame] | 1554 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1555 | if (PyType_Ready(&PyRange_Type) < 0) |
| 1556 | Py_FatalError("Can't initialize range type"); |
Benjamin Peterson | ae937c0 | 2009-04-18 20:54:08 +0000 | [diff] [blame] | 1557 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1558 | if (PyType_Ready(&PyDict_Type) < 0) |
| 1559 | Py_FatalError("Can't initialize dict type"); |
Benjamin Peterson | ae937c0 | 2009-04-18 20:54:08 +0000 | [diff] [blame] | 1560 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1561 | if (PyType_Ready(&PySet_Type) < 0) |
| 1562 | Py_FatalError("Can't initialize set type"); |
Benjamin Peterson | ae937c0 | 2009-04-18 20:54:08 +0000 | [diff] [blame] | 1563 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1564 | if (PyType_Ready(&PyUnicode_Type) < 0) |
| 1565 | Py_FatalError("Can't initialize str type"); |
Benjamin Peterson | ae937c0 | 2009-04-18 20:54:08 +0000 | [diff] [blame] | 1566 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1567 | if (PyType_Ready(&PySlice_Type) < 0) |
| 1568 | Py_FatalError("Can't initialize slice type"); |
Benjamin Peterson | ae937c0 | 2009-04-18 20:54:08 +0000 | [diff] [blame] | 1569 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1570 | if (PyType_Ready(&PyStaticMethod_Type) < 0) |
| 1571 | Py_FatalError("Can't initialize static method type"); |
Benjamin Peterson | ae937c0 | 2009-04-18 20:54:08 +0000 | [diff] [blame] | 1572 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1573 | if (PyType_Ready(&PyComplex_Type) < 0) |
| 1574 | Py_FatalError("Can't initialize complex type"); |
Skip Montanaro | ba1e0f4 | 2009-10-18 14:25:35 +0000 | [diff] [blame] | 1575 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1576 | if (PyType_Ready(&PyFloat_Type) < 0) |
| 1577 | Py_FatalError("Can't initialize float type"); |
Benjamin Peterson | ae937c0 | 2009-04-18 20:54:08 +0000 | [diff] [blame] | 1578 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1579 | if (PyType_Ready(&PyLong_Type) < 0) |
| 1580 | Py_FatalError("Can't initialize int type"); |
Benjamin Peterson | ae937c0 | 2009-04-18 20:54:08 +0000 | [diff] [blame] | 1581 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1582 | if (PyType_Ready(&PyFrozenSet_Type) < 0) |
| 1583 | Py_FatalError("Can't initialize frozenset type"); |
Benjamin Peterson | ae937c0 | 2009-04-18 20:54:08 +0000 | [diff] [blame] | 1584 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1585 | if (PyType_Ready(&PyProperty_Type) < 0) |
| 1586 | Py_FatalError("Can't initialize property type"); |
Benjamin Peterson | ae937c0 | 2009-04-18 20:54:08 +0000 | [diff] [blame] | 1587 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1588 | if (PyType_Ready(&PyMemoryView_Type) < 0) |
| 1589 | Py_FatalError("Can't initialize memoryview type"); |
Benjamin Peterson | ae937c0 | 2009-04-18 20:54:08 +0000 | [diff] [blame] | 1590 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1591 | if (PyType_Ready(&PyTuple_Type) < 0) |
| 1592 | Py_FatalError("Can't initialize tuple type"); |
Benjamin Peterson | ae937c0 | 2009-04-18 20:54:08 +0000 | [diff] [blame] | 1593 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1594 | if (PyType_Ready(&PyEnum_Type) < 0) |
| 1595 | Py_FatalError("Can't initialize enumerate type"); |
Benjamin Peterson | ae937c0 | 2009-04-18 20:54:08 +0000 | [diff] [blame] | 1596 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1597 | if (PyType_Ready(&PyReversed_Type) < 0) |
| 1598 | Py_FatalError("Can't initialize reversed type"); |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 1599 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1600 | if (PyType_Ready(&PyStdPrinter_Type) < 0) |
| 1601 | Py_FatalError("Can't initialize StdPrinter"); |
Benjamin Peterson | fd838e6 | 2009-04-20 02:09:13 +0000 | [diff] [blame] | 1602 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1603 | if (PyType_Ready(&PyCode_Type) < 0) |
| 1604 | Py_FatalError("Can't initialize code type"); |
Benjamin Peterson | fd838e6 | 2009-04-20 02:09:13 +0000 | [diff] [blame] | 1605 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1606 | if (PyType_Ready(&PyFrame_Type) < 0) |
| 1607 | Py_FatalError("Can't initialize frame type"); |
Benjamin Peterson | fd838e6 | 2009-04-20 02:09:13 +0000 | [diff] [blame] | 1608 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1609 | if (PyType_Ready(&PyCFunction_Type) < 0) |
| 1610 | Py_FatalError("Can't initialize builtin function type"); |
Benjamin Peterson | fd838e6 | 2009-04-20 02:09:13 +0000 | [diff] [blame] | 1611 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1612 | if (PyType_Ready(&PyMethod_Type) < 0) |
| 1613 | Py_FatalError("Can't initialize method type"); |
Benjamin Peterson | fd838e6 | 2009-04-20 02:09:13 +0000 | [diff] [blame] | 1614 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1615 | if (PyType_Ready(&PyFunction_Type) < 0) |
| 1616 | Py_FatalError("Can't initialize function type"); |
Benjamin Peterson | fd838e6 | 2009-04-20 02:09:13 +0000 | [diff] [blame] | 1617 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1618 | if (PyType_Ready(&PyDictProxy_Type) < 0) |
| 1619 | Py_FatalError("Can't initialize dict proxy type"); |
Benjamin Peterson | fd838e6 | 2009-04-20 02:09:13 +0000 | [diff] [blame] | 1620 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1621 | if (PyType_Ready(&PyGen_Type) < 0) |
| 1622 | Py_FatalError("Can't initialize generator type"); |
Benjamin Peterson | fd838e6 | 2009-04-20 02:09:13 +0000 | [diff] [blame] | 1623 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1624 | if (PyType_Ready(&PyGetSetDescr_Type) < 0) |
| 1625 | Py_FatalError("Can't initialize get-set descriptor type"); |
Benjamin Peterson | fd838e6 | 2009-04-20 02:09:13 +0000 | [diff] [blame] | 1626 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1627 | if (PyType_Ready(&PyWrapperDescr_Type) < 0) |
| 1628 | Py_FatalError("Can't initialize wrapper type"); |
Benjamin Peterson | fd838e6 | 2009-04-20 02:09:13 +0000 | [diff] [blame] | 1629 | |
Benjamin Peterson | eff61f6 | 2011-09-01 16:32:31 -0400 | [diff] [blame] | 1630 | if (PyType_Ready(&_PyMethodWrapper_Type) < 0) |
| 1631 | Py_FatalError("Can't initialize method wrapper type"); |
| 1632 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1633 | if (PyType_Ready(&PyEllipsis_Type) < 0) |
| 1634 | Py_FatalError("Can't initialize ellipsis type"); |
Benjamin Peterson | fd838e6 | 2009-04-20 02:09:13 +0000 | [diff] [blame] | 1635 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1636 | if (PyType_Ready(&PyMemberDescr_Type) < 0) |
| 1637 | Py_FatalError("Can't initialize member descriptor type"); |
Benjamin Peterson | 8bc5b68 | 2009-05-09 18:10:51 +0000 | [diff] [blame] | 1638 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1639 | if (PyType_Ready(&PyFilter_Type) < 0) |
| 1640 | Py_FatalError("Can't initialize filter type"); |
Benjamin Peterson | 8bc5b68 | 2009-05-09 18:10:51 +0000 | [diff] [blame] | 1641 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1642 | if (PyType_Ready(&PyMap_Type) < 0) |
| 1643 | Py_FatalError("Can't initialize map type"); |
Benjamin Peterson | 8bc5b68 | 2009-05-09 18:10:51 +0000 | [diff] [blame] | 1644 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1645 | if (PyType_Ready(&PyZip_Type) < 0) |
| 1646 | Py_FatalError("Can't initialize zip type"); |
Guido van Rossum | ba21a49 | 2001-08-16 08:17:26 +0000 | [diff] [blame] | 1647 | } |
| 1648 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1649 | |
Guido van Rossum | 84a9032 | 1996-05-22 16:34:47 +0000 | [diff] [blame] | 1650 | #ifdef Py_TRACE_REFS |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1651 | |
Guido van Rossum | aacdc9d | 1996-08-12 21:32:12 +0000 | [diff] [blame] | 1652 | void |
Fred Drake | 100814d | 2000-07-09 15:48:49 +0000 | [diff] [blame] | 1653 | _Py_NewReference(PyObject *op) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1654 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1655 | _Py_INC_REFTOTAL; |
| 1656 | op->ob_refcnt = 1; |
| 1657 | _Py_AddToAllObjects(op, 1); |
| 1658 | _Py_INC_TPALLOCS(op); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1659 | } |
| 1660 | |
Guido van Rossum | aacdc9d | 1996-08-12 21:32:12 +0000 | [diff] [blame] | 1661 | void |
Fred Drake | 100814d | 2000-07-09 15:48:49 +0000 | [diff] [blame] | 1662 | _Py_ForgetReference(register PyObject *op) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1663 | { |
Guido van Rossum | bffd683 | 2000-01-20 22:32:56 +0000 | [diff] [blame] | 1664 | #ifdef SLOW_UNREF_CHECK |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1665 | register PyObject *p; |
Guido van Rossum | bffd683 | 2000-01-20 22:32:56 +0000 | [diff] [blame] | 1666 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1667 | if (op->ob_refcnt < 0) |
| 1668 | Py_FatalError("UNREF negative refcnt"); |
| 1669 | if (op == &refchain || |
| 1670 | op->_ob_prev->_ob_next != op || op->_ob_next->_ob_prev != op) { |
| 1671 | fprintf(stderr, "* ob\n"); |
| 1672 | _PyObject_Dump(op); |
| 1673 | fprintf(stderr, "* op->_ob_prev->_ob_next\n"); |
| 1674 | _PyObject_Dump(op->_ob_prev->_ob_next); |
| 1675 | fprintf(stderr, "* op->_ob_next->_ob_prev\n"); |
| 1676 | _PyObject_Dump(op->_ob_next->_ob_prev); |
| 1677 | Py_FatalError("UNREF invalid object"); |
| 1678 | } |
Guido van Rossum | 2e8f614 | 1992-09-03 20:32:55 +0000 | [diff] [blame] | 1679 | #ifdef SLOW_UNREF_CHECK |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1680 | for (p = refchain._ob_next; p != &refchain; p = p->_ob_next) { |
| 1681 | if (p == op) |
| 1682 | break; |
| 1683 | } |
| 1684 | if (p == &refchain) /* Not found */ |
| 1685 | Py_FatalError("UNREF unknown object"); |
Guido van Rossum | 2e8f614 | 1992-09-03 20:32:55 +0000 | [diff] [blame] | 1686 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1687 | op->_ob_next->_ob_prev = op->_ob_prev; |
| 1688 | op->_ob_prev->_ob_next = op->_ob_next; |
| 1689 | op->_ob_next = op->_ob_prev = NULL; |
| 1690 | _Py_INC_TPFREES(op); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1691 | } |
| 1692 | |
Guido van Rossum | aacdc9d | 1996-08-12 21:32:12 +0000 | [diff] [blame] | 1693 | void |
Fred Drake | 100814d | 2000-07-09 15:48:49 +0000 | [diff] [blame] | 1694 | _Py_Dealloc(PyObject *op) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1695 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1696 | destructor dealloc = Py_TYPE(op)->tp_dealloc; |
| 1697 | _Py_ForgetReference(op); |
| 1698 | (*dealloc)(op); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1699 | } |
| 1700 | |
Tim Peters | 269b2a6 | 2003-04-17 19:52:29 +0000 | [diff] [blame] | 1701 | /* Print all live objects. Because PyObject_Print is called, the |
| 1702 | * interpreter must be in a healthy state. |
| 1703 | */ |
Guido van Rossum | aacdc9d | 1996-08-12 21:32:12 +0000 | [diff] [blame] | 1704 | void |
Fred Drake | 100814d | 2000-07-09 15:48:49 +0000 | [diff] [blame] | 1705 | _Py_PrintReferences(FILE *fp) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1706 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1707 | PyObject *op; |
| 1708 | fprintf(fp, "Remaining objects:\n"); |
| 1709 | for (op = refchain._ob_next; op != &refchain; op = op->_ob_next) { |
| 1710 | fprintf(fp, "%p [%" PY_FORMAT_SIZE_T "d] ", op, op->ob_refcnt); |
| 1711 | if (PyObject_Print(op, fp, 0) != 0) |
| 1712 | PyErr_Clear(); |
| 1713 | putc('\n', fp); |
| 1714 | } |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1715 | } |
| 1716 | |
Tim Peters | 269b2a6 | 2003-04-17 19:52:29 +0000 | [diff] [blame] | 1717 | /* Print the addresses of all live objects. Unlike _Py_PrintReferences, this |
| 1718 | * doesn't make any calls to the Python C API, so is always safe to call. |
| 1719 | */ |
| 1720 | void |
| 1721 | _Py_PrintReferenceAddresses(FILE *fp) |
| 1722 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1723 | PyObject *op; |
| 1724 | fprintf(fp, "Remaining object addresses:\n"); |
| 1725 | for (op = refchain._ob_next; op != &refchain; op = op->_ob_next) |
| 1726 | fprintf(fp, "%p [%" PY_FORMAT_SIZE_T "d] %s\n", op, |
| 1727 | op->ob_refcnt, Py_TYPE(op)->tp_name); |
Tim Peters | 269b2a6 | 2003-04-17 19:52:29 +0000 | [diff] [blame] | 1728 | } |
| 1729 | |
Sjoerd Mullender | 6ec3c65 | 1995-08-29 09:18:14 +0000 | [diff] [blame] | 1730 | PyObject * |
Fred Drake | 100814d | 2000-07-09 15:48:49 +0000 | [diff] [blame] | 1731 | _Py_GetObjects(PyObject *self, PyObject *args) |
Sjoerd Mullender | 6ec3c65 | 1995-08-29 09:18:14 +0000 | [diff] [blame] | 1732 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1733 | int i, n; |
| 1734 | PyObject *t = NULL; |
| 1735 | PyObject *res, *op; |
Sjoerd Mullender | 6ec3c65 | 1995-08-29 09:18:14 +0000 | [diff] [blame] | 1736 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1737 | if (!PyArg_ParseTuple(args, "i|O", &n, &t)) |
| 1738 | return NULL; |
| 1739 | op = refchain._ob_next; |
| 1740 | res = PyList_New(0); |
| 1741 | if (res == NULL) |
| 1742 | return NULL; |
| 1743 | for (i = 0; (n == 0 || i < n) && op != &refchain; i++) { |
| 1744 | while (op == self || op == args || op == res || op == t || |
| 1745 | (t != NULL && Py_TYPE(op) != (PyTypeObject *) t)) { |
| 1746 | op = op->_ob_next; |
| 1747 | if (op == &refchain) |
| 1748 | return res; |
| 1749 | } |
| 1750 | if (PyList_Append(res, op) < 0) { |
| 1751 | Py_DECREF(res); |
| 1752 | return NULL; |
| 1753 | } |
| 1754 | op = op->_ob_next; |
| 1755 | } |
| 1756 | return res; |
Sjoerd Mullender | 6ec3c65 | 1995-08-29 09:18:14 +0000 | [diff] [blame] | 1757 | } |
| 1758 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1759 | #endif |
Guido van Rossum | 97ead3f | 1996-01-12 01:24:09 +0000 | [diff] [blame] | 1760 | |
Benjamin Peterson | b173f78 | 2009-05-05 22:31:58 +0000 | [diff] [blame] | 1761 | /* Hack to force loading of pycapsule.o */ |
| 1762 | PyTypeObject *_PyCapsule_hack = &PyCapsule_Type; |
| 1763 | |
| 1764 | |
Guido van Rossum | 84a9032 | 1996-05-22 16:34:47 +0000 | [diff] [blame] | 1765 | /* Hack to force loading of abstract.o */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1766 | Py_ssize_t (*_Py_abstract_hack)(PyObject *) = PyObject_Size; |
Guido van Rossum | e09fb55 | 1997-08-05 02:04:34 +0000 | [diff] [blame] | 1767 | |
| 1768 | |
Andrew M. Kuchling | 1582a3a | 2000-08-16 12:27:23 +0000 | [diff] [blame] | 1769 | /* Python's malloc wrappers (see pymem.h) */ |
Guido van Rossum | e09fb55 | 1997-08-05 02:04:34 +0000 | [diff] [blame] | 1770 | |
Thomas Wouters | 334fb89 | 2000-07-25 12:56:38 +0000 | [diff] [blame] | 1771 | void * |
Fred Drake | 100814d | 2000-07-09 15:48:49 +0000 | [diff] [blame] | 1772 | PyMem_Malloc(size_t nbytes) |
Guido van Rossum | e09fb55 | 1997-08-05 02:04:34 +0000 | [diff] [blame] | 1773 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1774 | return PyMem_MALLOC(nbytes); |
Guido van Rossum | e09fb55 | 1997-08-05 02:04:34 +0000 | [diff] [blame] | 1775 | } |
| 1776 | |
Thomas Wouters | 334fb89 | 2000-07-25 12:56:38 +0000 | [diff] [blame] | 1777 | void * |
| 1778 | PyMem_Realloc(void *p, size_t nbytes) |
Guido van Rossum | e09fb55 | 1997-08-05 02:04:34 +0000 | [diff] [blame] | 1779 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1780 | return PyMem_REALLOC(p, nbytes); |
Guido van Rossum | e09fb55 | 1997-08-05 02:04:34 +0000 | [diff] [blame] | 1781 | } |
| 1782 | |
| 1783 | void |
Thomas Wouters | 334fb89 | 2000-07-25 12:56:38 +0000 | [diff] [blame] | 1784 | PyMem_Free(void *p) |
Guido van Rossum | e09fb55 | 1997-08-05 02:04:34 +0000 | [diff] [blame] | 1785 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1786 | PyMem_FREE(p); |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 1787 | } |
| 1788 | |
| 1789 | |
Guido van Rossum | 8661036 | 1998-04-10 22:32:46 +0000 | [diff] [blame] | 1790 | /* These methods are used to control infinite recursion in repr, str, print, |
| 1791 | etc. Container objects that may recursively contain themselves, |
| 1792 | e.g. builtin dictionaries and lists, should used Py_ReprEnter() and |
| 1793 | Py_ReprLeave() to avoid infinite recursion. |
| 1794 | |
| 1795 | Py_ReprEnter() returns 0 the first time it is called for a particular |
| 1796 | object and 1 every time thereafter. It returns -1 if an exception |
| 1797 | occurred. Py_ReprLeave() has no return value. |
| 1798 | |
| 1799 | See dictobject.c and listobject.c for examples of use. |
| 1800 | */ |
| 1801 | |
| 1802 | #define KEY "Py_Repr" |
| 1803 | |
| 1804 | int |
Fred Drake | 100814d | 2000-07-09 15:48:49 +0000 | [diff] [blame] | 1805 | Py_ReprEnter(PyObject *obj) |
Guido van Rossum | 8661036 | 1998-04-10 22:32:46 +0000 | [diff] [blame] | 1806 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1807 | PyObject *dict; |
| 1808 | PyObject *list; |
| 1809 | Py_ssize_t i; |
Guido van Rossum | 8661036 | 1998-04-10 22:32:46 +0000 | [diff] [blame] | 1810 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1811 | dict = PyThreadState_GetDict(); |
| 1812 | if (dict == NULL) |
| 1813 | return 0; |
| 1814 | list = PyDict_GetItemString(dict, KEY); |
| 1815 | if (list == NULL) { |
| 1816 | list = PyList_New(0); |
| 1817 | if (list == NULL) |
| 1818 | return -1; |
| 1819 | if (PyDict_SetItemString(dict, KEY, list) < 0) |
| 1820 | return -1; |
| 1821 | Py_DECREF(list); |
| 1822 | } |
| 1823 | i = PyList_GET_SIZE(list); |
| 1824 | while (--i >= 0) { |
| 1825 | if (PyList_GET_ITEM(list, i) == obj) |
| 1826 | return 1; |
| 1827 | } |
| 1828 | PyList_Append(list, obj); |
| 1829 | return 0; |
Guido van Rossum | 8661036 | 1998-04-10 22:32:46 +0000 | [diff] [blame] | 1830 | } |
| 1831 | |
| 1832 | void |
Fred Drake | 100814d | 2000-07-09 15:48:49 +0000 | [diff] [blame] | 1833 | Py_ReprLeave(PyObject *obj) |
Guido van Rossum | 8661036 | 1998-04-10 22:32:46 +0000 | [diff] [blame] | 1834 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1835 | PyObject *dict; |
| 1836 | PyObject *list; |
| 1837 | Py_ssize_t i; |
Guido van Rossum | 8661036 | 1998-04-10 22:32:46 +0000 | [diff] [blame] | 1838 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1839 | dict = PyThreadState_GetDict(); |
| 1840 | if (dict == NULL) |
| 1841 | return; |
| 1842 | list = PyDict_GetItemString(dict, KEY); |
| 1843 | if (list == NULL || !PyList_Check(list)) |
| 1844 | return; |
| 1845 | i = PyList_GET_SIZE(list); |
| 1846 | /* Count backwards because we always expect obj to be list[-1] */ |
| 1847 | while (--i >= 0) { |
| 1848 | if (PyList_GET_ITEM(list, i) == obj) { |
| 1849 | PyList_SetSlice(list, i, i + 1, NULL); |
| 1850 | break; |
| 1851 | } |
| 1852 | } |
Guido van Rossum | 8661036 | 1998-04-10 22:32:46 +0000 | [diff] [blame] | 1853 | } |
Guido van Rossum | d724b23 | 2000-03-13 16:01:29 +0000 | [diff] [blame] | 1854 | |
Tim Peters | 803526b | 2002-07-07 05:13:56 +0000 | [diff] [blame] | 1855 | /* Trashcan support. */ |
Guido van Rossum | d724b23 | 2000-03-13 16:01:29 +0000 | [diff] [blame] | 1856 | |
Tim Peters | 803526b | 2002-07-07 05:13:56 +0000 | [diff] [blame] | 1857 | /* Current call-stack depth of tp_dealloc calls. */ |
Guido van Rossum | d724b23 | 2000-03-13 16:01:29 +0000 | [diff] [blame] | 1858 | int _PyTrash_delete_nesting = 0; |
Guido van Rossum | e92e610 | 2000-04-24 15:40:53 +0000 | [diff] [blame] | 1859 | |
Tim Peters | 803526b | 2002-07-07 05:13:56 +0000 | [diff] [blame] | 1860 | /* List of objects that still need to be cleaned up, singly linked via their |
| 1861 | * gc headers' gc_prev pointers. |
| 1862 | */ |
| 1863 | PyObject *_PyTrash_delete_later = NULL; |
Guido van Rossum | d724b23 | 2000-03-13 16:01:29 +0000 | [diff] [blame] | 1864 | |
Tim Peters | 803526b | 2002-07-07 05:13:56 +0000 | [diff] [blame] | 1865 | /* Add op to the _PyTrash_delete_later list. Called when the current |
| 1866 | * call-stack depth gets large. op must be a currently untracked gc'ed |
| 1867 | * object, with refcount 0. Py_DECREF must already have been called on it. |
| 1868 | */ |
Guido van Rossum | d724b23 | 2000-03-13 16:01:29 +0000 | [diff] [blame] | 1869 | void |
Fred Drake | 100814d | 2000-07-09 15:48:49 +0000 | [diff] [blame] | 1870 | _PyTrash_deposit_object(PyObject *op) |
Guido van Rossum | d724b23 | 2000-03-13 16:01:29 +0000 | [diff] [blame] | 1871 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1872 | assert(PyObject_IS_GC(op)); |
| 1873 | assert(_Py_AS_GC(op)->gc.gc_refs == _PyGC_REFS_UNTRACKED); |
| 1874 | assert(op->ob_refcnt == 0); |
| 1875 | _Py_AS_GC(op)->gc.gc_prev = (PyGC_Head *)_PyTrash_delete_later; |
| 1876 | _PyTrash_delete_later = op; |
Guido van Rossum | d724b23 | 2000-03-13 16:01:29 +0000 | [diff] [blame] | 1877 | } |
| 1878 | |
Tim Peters | 803526b | 2002-07-07 05:13:56 +0000 | [diff] [blame] | 1879 | /* Dealloccate all the objects in the _PyTrash_delete_later list. Called when |
| 1880 | * the call-stack unwinds again. |
| 1881 | */ |
Guido van Rossum | d724b23 | 2000-03-13 16:01:29 +0000 | [diff] [blame] | 1882 | void |
Fred Drake | 100814d | 2000-07-09 15:48:49 +0000 | [diff] [blame] | 1883 | _PyTrash_destroy_chain(void) |
Guido van Rossum | d724b23 | 2000-03-13 16:01:29 +0000 | [diff] [blame] | 1884 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1885 | while (_PyTrash_delete_later) { |
| 1886 | PyObject *op = _PyTrash_delete_later; |
| 1887 | destructor dealloc = Py_TYPE(op)->tp_dealloc; |
Neil Schemenauer | f589c05 | 2002-03-29 03:05:54 +0000 | [diff] [blame] | 1888 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1889 | _PyTrash_delete_later = |
| 1890 | (PyObject*) _Py_AS_GC(op)->gc.gc_prev; |
Neil Schemenauer | f589c05 | 2002-03-29 03:05:54 +0000 | [diff] [blame] | 1891 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1892 | /* Call the deallocator directly. This used to try to |
| 1893 | * fool Py_DECREF into calling it indirectly, but |
| 1894 | * Py_DECREF was already called on this object, and in |
| 1895 | * assorted non-release builds calling Py_DECREF again ends |
| 1896 | * up distorting allocation statistics. |
| 1897 | */ |
| 1898 | assert(op->ob_refcnt == 0); |
| 1899 | ++_PyTrash_delete_nesting; |
| 1900 | (*dealloc)(op); |
| 1901 | --_PyTrash_delete_nesting; |
| 1902 | } |
Guido van Rossum | d724b23 | 2000-03-13 16:01:29 +0000 | [diff] [blame] | 1903 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1904 | |
Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 1905 | #ifndef Py_TRACE_REFS |
| 1906 | /* For Py_LIMITED_API, we need an out-of-line version of _Py_Dealloc. |
| 1907 | Define this here, so we can undefine the macro. */ |
| 1908 | #undef _Py_Dealloc |
| 1909 | PyAPI_FUNC(void) _Py_Dealloc(PyObject *); |
| 1910 | void |
| 1911 | _Py_Dealloc(PyObject *op) |
| 1912 | { |
| 1913 | _Py_INC_TPFREES(op) _Py_COUNT_ALLOCS_COMMA |
| 1914 | (*Py_TYPE(op)->tp_dealloc)(op); |
| 1915 | } |
| 1916 | #endif |
| 1917 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1918 | #ifdef __cplusplus |
| 1919 | } |
| 1920 | #endif |