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