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