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