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