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