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