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