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