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