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