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