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