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