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