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