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