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); |
| 381 | if (res != NULL && !PyUnicode_Check(res)) { |
| 382 | PyErr_Format(PyExc_TypeError, |
| 383 | "__repr__ returned non-string (type %.200s)", |
| 384 | res->ob_type->tp_name); |
| 385 | Py_DECREF(res); |
| 386 | return NULL; |
| 387 | } |
Victor Stinner | db88ae5 | 2011-12-01 02:15:00 +0100 | [diff] [blame] | 388 | #ifndef Py_DEBUG |
| 389 | if (PyUnicode_READY(res) < 0) |
| 390 | return NULL; |
| 391 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 392 | return res; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 393 | } |
| 394 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 395 | PyObject * |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 396 | PyObject_Str(PyObject *v) |
Guido van Rossum | c600411 | 1993-11-05 10:22:19 +0000 | [diff] [blame] | 397 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 398 | PyObject *res; |
| 399 | if (PyErr_CheckSignals()) |
| 400 | return NULL; |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 401 | #ifdef USE_STACKCHECK |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 402 | if (PyOS_CheckStack()) { |
| 403 | PyErr_SetString(PyExc_MemoryError, "stack overflow"); |
| 404 | return NULL; |
| 405 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 406 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 407 | if (v == NULL) |
| 408 | return PyUnicode_FromString("<NULL>"); |
| 409 | if (PyUnicode_CheckExact(v)) { |
Victor Stinner | db88ae5 | 2011-12-01 02:15:00 +0100 | [diff] [blame] | 410 | #ifndef Py_DEBUG |
Victor Stinner | 4ead7c7 | 2011-11-20 19:48:36 +0100 | [diff] [blame] | 411 | if (PyUnicode_READY(v) < 0) |
| 412 | return NULL; |
Victor Stinner | db88ae5 | 2011-12-01 02:15:00 +0100 | [diff] [blame] | 413 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 414 | Py_INCREF(v); |
| 415 | return v; |
| 416 | } |
| 417 | if (Py_TYPE(v)->tp_str == NULL) |
| 418 | return PyObject_Repr(v); |
Guido van Rossum | 4f288ab | 2001-05-01 16:53:37 +0000 | [diff] [blame] | 419 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 420 | /* It is possible for a type to have a tp_str representation that loops |
| 421 | infinitely. */ |
| 422 | if (Py_EnterRecursiveCall(" while getting the str of an object")) |
| 423 | return NULL; |
| 424 | res = (*Py_TYPE(v)->tp_str)(v); |
| 425 | Py_LeaveRecursiveCall(); |
| 426 | if (res == NULL) |
| 427 | return NULL; |
| 428 | if (!PyUnicode_Check(res)) { |
| 429 | PyErr_Format(PyExc_TypeError, |
| 430 | "__str__ returned non-string (type %.200s)", |
| 431 | Py_TYPE(res)->tp_name); |
| 432 | Py_DECREF(res); |
| 433 | return NULL; |
| 434 | } |
Victor Stinner | db88ae5 | 2011-12-01 02:15:00 +0100 | [diff] [blame] | 435 | #ifndef Py_DEBUG |
Victor Stinner | 4ead7c7 | 2011-11-20 19:48:36 +0100 | [diff] [blame] | 436 | if (PyUnicode_READY(res) < 0) |
| 437 | return NULL; |
Victor Stinner | db88ae5 | 2011-12-01 02:15:00 +0100 | [diff] [blame] | 438 | #endif |
Victor Stinner | 4ead7c7 | 2011-11-20 19:48:36 +0100 | [diff] [blame] | 439 | assert(_PyUnicode_CheckConsistency(res, 1)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 440 | return res; |
Neil Schemenauer | cf52c07 | 2005-08-12 17:34:58 +0000 | [diff] [blame] | 441 | } |
| 442 | |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 443 | PyObject * |
| 444 | PyObject_ASCII(PyObject *v) |
| 445 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 446 | PyObject *repr, *ascii, *res; |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 447 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 448 | repr = PyObject_Repr(v); |
| 449 | if (repr == NULL) |
| 450 | return NULL; |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 451 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 452 | /* 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] | 453 | ascii = _PyUnicode_AsASCIIString(repr, "backslashreplace"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 454 | Py_DECREF(repr); |
| 455 | if (ascii == NULL) |
| 456 | return NULL; |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 457 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 458 | res = PyUnicode_DecodeASCII( |
| 459 | PyBytes_AS_STRING(ascii), |
| 460 | PyBytes_GET_SIZE(ascii), |
| 461 | NULL); |
| 462 | |
| 463 | Py_DECREF(ascii); |
| 464 | return res; |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 465 | } |
Guido van Rossum | a3af41d | 2001-01-18 22:07:06 +0000 | [diff] [blame] | 466 | |
Benjamin Peterson | c15a073 | 2008-08-26 16:46:47 +0000 | [diff] [blame] | 467 | PyObject * |
| 468 | PyObject_Bytes(PyObject *v) |
| 469 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 470 | PyObject *result, *func; |
| 471 | static PyObject *bytesstring = NULL; |
Benjamin Peterson | c15a073 | 2008-08-26 16:46:47 +0000 | [diff] [blame] | 472 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 473 | if (v == NULL) |
| 474 | return PyBytes_FromString("<NULL>"); |
Benjamin Peterson | c15a073 | 2008-08-26 16:46:47 +0000 | [diff] [blame] | 475 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 476 | if (PyBytes_CheckExact(v)) { |
| 477 | Py_INCREF(v); |
| 478 | return v; |
| 479 | } |
Benjamin Peterson | c15a073 | 2008-08-26 16:46:47 +0000 | [diff] [blame] | 480 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 481 | func = _PyObject_LookupSpecial(v, "__bytes__", &bytesstring); |
| 482 | if (func != NULL) { |
| 483 | result = PyObject_CallFunctionObjArgs(func, NULL); |
| 484 | Py_DECREF(func); |
| 485 | if (result == NULL) |
Benjamin Peterson | 41ece39 | 2010-09-11 16:39:57 +0000 | [diff] [blame] | 486 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 487 | if (!PyBytes_Check(result)) { |
Benjamin Peterson | 41ece39 | 2010-09-11 16:39:57 +0000 | [diff] [blame] | 488 | PyErr_Format(PyExc_TypeError, |
| 489 | "__bytes__ returned non-bytes (type %.200s)", |
| 490 | Py_TYPE(result)->tp_name); |
| 491 | Py_DECREF(result); |
| 492 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 493 | } |
| 494 | return result; |
| 495 | } |
| 496 | else if (PyErr_Occurred()) |
| 497 | return NULL; |
| 498 | return PyBytes_FromObject(v); |
Benjamin Peterson | c15a073 | 2008-08-26 16:46:47 +0000 | [diff] [blame] | 499 | } |
| 500 | |
Mark Dickinson | c008a17 | 2009-02-01 13:59:22 +0000 | [diff] [blame] | 501 | /* For Python 3.0.1 and later, the old three-way comparison has been |
| 502 | completely removed in favour of rich comparisons. PyObject_Compare() and |
| 503 | PyObject_Cmp() are gone, and the builtin cmp function no longer exists. |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 504 | 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] | 505 | longer be used. Use tp_richcompare instead. |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 506 | |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 507 | See (*) below for practical amendments. |
| 508 | |
Mark Dickinson | c008a17 | 2009-02-01 13:59:22 +0000 | [diff] [blame] | 509 | tp_richcompare gets called with a first argument of the appropriate type |
| 510 | and a second object of an arbitrary type. We never do any kind of |
| 511 | coercion. |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 512 | |
Mark Dickinson | c008a17 | 2009-02-01 13:59:22 +0000 | [diff] [blame] | 513 | The tp_richcompare slot should return an object, as follows: |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 514 | |
| 515 | NULL if an exception occurred |
| 516 | NotImplemented if the requested comparison is not implemented |
| 517 | any other false value if the requested comparison is false |
| 518 | any other true value if the requested comparison is true |
| 519 | |
| 520 | The PyObject_RichCompare[Bool]() wrappers raise TypeError when they get |
| 521 | NotImplemented. |
| 522 | |
| 523 | (*) Practical amendments: |
| 524 | |
| 525 | - If rich comparison returns NotImplemented, == and != are decided by |
| 526 | comparing the object pointer (i.e. falling back to the base object |
| 527 | implementation). |
| 528 | |
Guido van Rossum | a407300 | 2002-05-31 20:03:54 +0000 | [diff] [blame] | 529 | */ |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 530 | |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 531 | /* Map rich comparison operators to their swapped version, e.g. LT <--> GT */ |
Brett Cannon | a5ca2e7 | 2004-09-25 01:37:24 +0000 | [diff] [blame] | 532 | 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] | 533 | |
Guido van Rossum | 9a4e95c | 2006-12-19 21:35:46 +0000 | [diff] [blame] | 534 | static char *opstrings[] = {"<", "<=", "==", "!=", ">", ">="}; |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 535 | |
| 536 | /* Perform a rich comparison, raising TypeError when the requested comparison |
| 537 | operator is not supported. */ |
Guido van Rossum | e797ec1 | 2001-01-17 15:24:28 +0000 | [diff] [blame] | 538 | static PyObject * |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 539 | do_richcompare(PyObject *v, PyObject *w, int op) |
Guido van Rossum | e797ec1 | 2001-01-17 15:24:28 +0000 | [diff] [blame] | 540 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 541 | richcmpfunc f; |
| 542 | PyObject *res; |
| 543 | int checked_reverse_op = 0; |
Guido van Rossum | e797ec1 | 2001-01-17 15:24:28 +0000 | [diff] [blame] | 544 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 545 | if (v->ob_type != w->ob_type && |
| 546 | PyType_IsSubtype(w->ob_type, v->ob_type) && |
| 547 | (f = w->ob_type->tp_richcompare) != NULL) { |
| 548 | checked_reverse_op = 1; |
| 549 | res = (*f)(w, v, _Py_SwappedOp[op]); |
| 550 | if (res != Py_NotImplemented) |
| 551 | return res; |
| 552 | Py_DECREF(res); |
| 553 | } |
| 554 | if ((f = v->ob_type->tp_richcompare) != NULL) { |
| 555 | res = (*f)(v, w, op); |
| 556 | if (res != Py_NotImplemented) |
| 557 | return res; |
| 558 | Py_DECREF(res); |
| 559 | } |
| 560 | if (!checked_reverse_op && (f = w->ob_type->tp_richcompare) != NULL) { |
| 561 | res = (*f)(w, v, _Py_SwappedOp[op]); |
| 562 | if (res != Py_NotImplemented) |
| 563 | return res; |
| 564 | Py_DECREF(res); |
| 565 | } |
| 566 | /* If neither object implements it, provide a sensible default |
| 567 | for == and !=, but raise an exception for ordering. */ |
| 568 | switch (op) { |
| 569 | case Py_EQ: |
| 570 | res = (v == w) ? Py_True : Py_False; |
| 571 | break; |
| 572 | case Py_NE: |
| 573 | res = (v != w) ? Py_True : Py_False; |
| 574 | break; |
| 575 | default: |
| 576 | /* XXX Special-case None so it doesn't show as NoneType() */ |
| 577 | PyErr_Format(PyExc_TypeError, |
| 578 | "unorderable types: %.100s() %s %.100s()", |
| 579 | v->ob_type->tp_name, |
| 580 | opstrings[op], |
| 581 | w->ob_type->tp_name); |
| 582 | return NULL; |
| 583 | } |
| 584 | Py_INCREF(res); |
| 585 | return res; |
Guido van Rossum | e797ec1 | 2001-01-17 15:24:28 +0000 | [diff] [blame] | 586 | } |
| 587 | |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 588 | /* Perform a rich comparison with object result. This wraps do_richcompare() |
| 589 | with a check for NULL arguments and a recursion check. */ |
Guido van Rossum | e797ec1 | 2001-01-17 15:24:28 +0000 | [diff] [blame] | 590 | |
Guido van Rossum | e797ec1 | 2001-01-17 15:24:28 +0000 | [diff] [blame] | 591 | PyObject * |
| 592 | PyObject_RichCompare(PyObject *v, PyObject *w, int op) |
| 593 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 594 | PyObject *res; |
Guido van Rossum | e797ec1 | 2001-01-17 15:24:28 +0000 | [diff] [blame] | 595 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 596 | assert(Py_LT <= op && op <= Py_GE); |
| 597 | if (v == NULL || w == NULL) { |
| 598 | if (!PyErr_Occurred()) |
| 599 | PyErr_BadInternalCall(); |
| 600 | return NULL; |
| 601 | } |
| 602 | if (Py_EnterRecursiveCall(" in comparison")) |
| 603 | return NULL; |
| 604 | res = do_richcompare(v, w, op); |
| 605 | Py_LeaveRecursiveCall(); |
| 606 | return res; |
Guido van Rossum | e797ec1 | 2001-01-17 15:24:28 +0000 | [diff] [blame] | 607 | } |
| 608 | |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 609 | /* Perform a rich comparison with integer result. This wraps |
| 610 | 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] | 611 | int |
| 612 | PyObject_RichCompareBool(PyObject *v, PyObject *w, int op) |
| 613 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 614 | PyObject *res; |
| 615 | int ok; |
Guido van Rossum | e797ec1 | 2001-01-17 15:24:28 +0000 | [diff] [blame] | 616 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 617 | /* Quick result when objects are the same. |
| 618 | Guarantees that identity implies equality. */ |
| 619 | if (v == w) { |
| 620 | if (op == Py_EQ) |
| 621 | return 1; |
| 622 | else if (op == Py_NE) |
| 623 | return 0; |
| 624 | } |
Mark Dickinson | 4a1f593 | 2008-11-12 23:23:36 +0000 | [diff] [blame] | 625 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 626 | res = PyObject_RichCompare(v, w, op); |
| 627 | if (res == NULL) |
| 628 | return -1; |
| 629 | if (PyBool_Check(res)) |
| 630 | ok = (res == Py_True); |
| 631 | else |
| 632 | ok = PyObject_IsTrue(res); |
| 633 | Py_DECREF(res); |
| 634 | return ok; |
Guido van Rossum | e797ec1 | 2001-01-17 15:24:28 +0000 | [diff] [blame] | 635 | } |
Fred Drake | 13634cf | 2000-06-29 19:17:04 +0000 | [diff] [blame] | 636 | |
| 637 | /* Set of hash utility functions to help maintaining the invariant that |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 638 | if a==b then hash(a)==hash(b) |
Fred Drake | 13634cf | 2000-06-29 19:17:04 +0000 | [diff] [blame] | 639 | |
| 640 | All the utility functions (_Py_Hash*()) return "-1" to signify an error. |
| 641 | */ |
| 642 | |
Mark Dickinson | dc787d2 | 2010-05-23 13:33:13 +0000 | [diff] [blame] | 643 | /* For numeric types, the hash of a number x is based on the reduction |
| 644 | of x modulo the prime P = 2**_PyHASH_BITS - 1. It's designed so that |
| 645 | hash(x) == hash(y) whenever x and y are numerically equal, even if |
| 646 | x and y have different types. |
| 647 | |
| 648 | A quick summary of the hashing strategy: |
| 649 | |
| 650 | (1) First define the 'reduction of x modulo P' for any rational |
| 651 | number x; this is a standard extension of the usual notion of |
| 652 | reduction modulo P for integers. If x == p/q (written in lowest |
| 653 | terms), the reduction is interpreted as the reduction of p times |
| 654 | the inverse of the reduction of q, all modulo P; if q is exactly |
| 655 | divisible by P then define the reduction to be infinity. So we've |
| 656 | got a well-defined map |
| 657 | |
| 658 | reduce : { rational numbers } -> { 0, 1, 2, ..., P-1, infinity }. |
| 659 | |
| 660 | (2) Now for a rational number x, define hash(x) by: |
| 661 | |
| 662 | reduce(x) if x >= 0 |
| 663 | -reduce(-x) if x < 0 |
| 664 | |
| 665 | If the result of the reduction is infinity (this is impossible for |
| 666 | integers, floats and Decimals) then use the predefined hash value |
| 667 | _PyHASH_INF for x >= 0, or -_PyHASH_INF for x < 0, instead. |
| 668 | _PyHASH_INF, -_PyHASH_INF and _PyHASH_NAN are also used for the |
| 669 | hashes of float and Decimal infinities and nans. |
| 670 | |
| 671 | A selling point for the above strategy is that it makes it possible |
| 672 | to compute hashes of decimal and binary floating-point numbers |
| 673 | efficiently, even if the exponent of the binary or decimal number |
| 674 | is large. The key point is that |
| 675 | |
| 676 | reduce(x * y) == reduce(x) * reduce(y) (modulo _PyHASH_MODULUS) |
| 677 | |
| 678 | provided that {reduce(x), reduce(y)} != {0, infinity}. The reduction of a |
| 679 | binary or decimal float is never infinity, since the denominator is a power |
| 680 | of 2 (for binary) or a divisor of a power of 10 (for decimal). So we have, |
| 681 | for nonnegative x, |
| 682 | |
| 683 | reduce(x * 2**e) == reduce(x) * reduce(2**e) % _PyHASH_MODULUS |
| 684 | |
| 685 | reduce(x * 10**e) == reduce(x) * reduce(10**e) % _PyHASH_MODULUS |
| 686 | |
| 687 | and reduce(10**e) can be computed efficiently by the usual modular |
| 688 | exponentiation algorithm. For reduce(2**e) it's even better: since |
| 689 | P is of the form 2**n-1, reduce(2**e) is 2**(e mod n), and multiplication |
| 690 | by 2**(e mod n) modulo 2**n-1 just amounts to a rotation of bits. |
| 691 | |
| 692 | */ |
| 693 | |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 694 | Py_hash_t |
Fred Drake | 100814d | 2000-07-09 15:48:49 +0000 | [diff] [blame] | 695 | _Py_HashDouble(double v) |
Fred Drake | 13634cf | 2000-06-29 19:17:04 +0000 | [diff] [blame] | 696 | { |
Mark Dickinson | dc787d2 | 2010-05-23 13:33:13 +0000 | [diff] [blame] | 697 | int e, sign; |
| 698 | double m; |
Benjamin Peterson | 8035bc5 | 2010-10-23 16:20:50 +0000 | [diff] [blame] | 699 | Py_uhash_t x, y; |
Tim Peters | 39dce29 | 2000-08-15 03:34:48 +0000 | [diff] [blame] | 700 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 701 | if (!Py_IS_FINITE(v)) { |
| 702 | if (Py_IS_INFINITY(v)) |
Mark Dickinson | dc787d2 | 2010-05-23 13:33:13 +0000 | [diff] [blame] | 703 | return v > 0 ? _PyHASH_INF : -_PyHASH_INF; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 704 | else |
Mark Dickinson | dc787d2 | 2010-05-23 13:33:13 +0000 | [diff] [blame] | 705 | return _PyHASH_NAN; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 706 | } |
Mark Dickinson | dc787d2 | 2010-05-23 13:33:13 +0000 | [diff] [blame] | 707 | |
| 708 | m = frexp(v, &e); |
| 709 | |
| 710 | sign = 1; |
| 711 | if (m < 0) { |
| 712 | sign = -1; |
| 713 | m = -m; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 714 | } |
Mark Dickinson | dc787d2 | 2010-05-23 13:33:13 +0000 | [diff] [blame] | 715 | |
| 716 | /* process 28 bits at a time; this should work well both for binary |
| 717 | and hexadecimal floating point. */ |
| 718 | x = 0; |
| 719 | while (m) { |
| 720 | x = ((x << 28) & _PyHASH_MODULUS) | x >> (_PyHASH_BITS - 28); |
| 721 | m *= 268435456.0; /* 2**28 */ |
| 722 | e -= 28; |
Benjamin Peterson | 8035bc5 | 2010-10-23 16:20:50 +0000 | [diff] [blame] | 723 | y = (Py_uhash_t)m; /* pull out integer part */ |
Mark Dickinson | dc787d2 | 2010-05-23 13:33:13 +0000 | [diff] [blame] | 724 | m -= y; |
| 725 | x += y; |
| 726 | if (x >= _PyHASH_MODULUS) |
| 727 | x -= _PyHASH_MODULUS; |
| 728 | } |
| 729 | |
| 730 | /* adjust for the exponent; first reduce it modulo _PyHASH_BITS */ |
| 731 | e = e >= 0 ? e % _PyHASH_BITS : _PyHASH_BITS-1-((-1-e) % _PyHASH_BITS); |
| 732 | x = ((x << e) & _PyHASH_MODULUS) | x >> (_PyHASH_BITS - e); |
| 733 | |
| 734 | x = x * sign; |
Benjamin Peterson | 8035bc5 | 2010-10-23 16:20:50 +0000 | [diff] [blame] | 735 | if (x == (Py_uhash_t)-1) |
| 736 | x = (Py_uhash_t)-2; |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 737 | return (Py_hash_t)x; |
Fred Drake | 13634cf | 2000-06-29 19:17:04 +0000 | [diff] [blame] | 738 | } |
| 739 | |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 740 | Py_hash_t |
Fred Drake | 100814d | 2000-07-09 15:48:49 +0000 | [diff] [blame] | 741 | _Py_HashPointer(void *p) |
Fred Drake | 13634cf | 2000-06-29 19:17:04 +0000 | [diff] [blame] | 742 | { |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 743 | Py_hash_t x; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 744 | size_t y = (size_t)p; |
| 745 | /* bottom 3 or 4 bits are likely to be 0; rotate y by 4 to avoid |
| 746 | excessive hash collisions for dicts and sets */ |
| 747 | y = (y >> 4) | (y << (8 * SIZEOF_VOID_P - 4)); |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 748 | x = (Py_hash_t)y; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 749 | if (x == -1) |
| 750 | x = -2; |
| 751 | return x; |
Fred Drake | 13634cf | 2000-06-29 19:17:04 +0000 | [diff] [blame] | 752 | } |
| 753 | |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 754 | Py_hash_t |
Antoine Pitrou | ce4a9da | 2011-11-21 20:46:33 +0100 | [diff] [blame] | 755 | _Py_HashBytes(unsigned char *p, Py_ssize_t len) |
| 756 | { |
| 757 | Py_uhash_t x; |
| 758 | Py_ssize_t i; |
| 759 | |
| 760 | x = (Py_uhash_t) *p << 7; |
| 761 | for (i = 0; i < len; i++) |
| 762 | x = (1000003U * x) ^ (Py_uhash_t) *p++; |
| 763 | x ^= (Py_uhash_t) len; |
| 764 | if (x == -1) |
| 765 | x = -2; |
| 766 | return x; |
| 767 | } |
| 768 | |
| 769 | Py_hash_t |
Nick Coghlan | d1abd25 | 2008-07-15 15:46:38 +0000 | [diff] [blame] | 770 | PyObject_HashNotImplemented(PyObject *v) |
| 771 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 772 | PyErr_Format(PyExc_TypeError, "unhashable type: '%.200s'", |
| 773 | Py_TYPE(v)->tp_name); |
| 774 | return -1; |
Nick Coghlan | d1abd25 | 2008-07-15 15:46:38 +0000 | [diff] [blame] | 775 | } |
Fred Drake | 13634cf | 2000-06-29 19:17:04 +0000 | [diff] [blame] | 776 | |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 777 | Py_hash_t |
Fred Drake | 100814d | 2000-07-09 15:48:49 +0000 | [diff] [blame] | 778 | PyObject_Hash(PyObject *v) |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 779 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 780 | PyTypeObject *tp = Py_TYPE(v); |
| 781 | if (tp->tp_hash != NULL) |
| 782 | return (*tp->tp_hash)(v); |
| 783 | /* To keep to the general practice that inheriting |
| 784 | * solely from object in C code should work without |
| 785 | * an explicit call to PyType_Ready, we implicitly call |
| 786 | * PyType_Ready here and then check the tp_hash slot again |
| 787 | */ |
| 788 | if (tp->tp_dict == NULL) { |
| 789 | if (PyType_Ready(tp) < 0) |
| 790 | return -1; |
| 791 | if (tp->tp_hash != NULL) |
| 792 | return (*tp->tp_hash)(v); |
| 793 | } |
| 794 | /* Otherwise, the object can't be hashed */ |
| 795 | return PyObject_HashNotImplemented(v); |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 796 | } |
| 797 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 798 | PyObject * |
Jeremy Hylton | af68c87 | 2005-12-10 18:50:16 +0000 | [diff] [blame] | 799 | PyObject_GetAttrString(PyObject *v, const char *name) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 800 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 801 | PyObject *w, *res; |
Guido van Rossum | d8eb1b3 | 1996-08-09 20:52:03 +0000 | [diff] [blame] | 802 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 803 | if (Py_TYPE(v)->tp_getattr != NULL) |
| 804 | return (*Py_TYPE(v)->tp_getattr)(v, (char*)name); |
| 805 | w = PyUnicode_InternFromString(name); |
| 806 | if (w == NULL) |
| 807 | return NULL; |
| 808 | res = PyObject_GetAttr(v, w); |
| 809 | Py_XDECREF(w); |
| 810 | return res; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 811 | } |
| 812 | |
| 813 | int |
Jeremy Hylton | af68c87 | 2005-12-10 18:50:16 +0000 | [diff] [blame] | 814 | PyObject_HasAttrString(PyObject *v, const char *name) |
Guido van Rossum | ed18fdc | 1993-07-11 19:55:34 +0000 | [diff] [blame] | 815 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 816 | PyObject *res = PyObject_GetAttrString(v, name); |
| 817 | if (res != NULL) { |
| 818 | Py_DECREF(res); |
| 819 | return 1; |
| 820 | } |
| 821 | PyErr_Clear(); |
| 822 | return 0; |
Guido van Rossum | ed18fdc | 1993-07-11 19:55:34 +0000 | [diff] [blame] | 823 | } |
| 824 | |
| 825 | int |
Jeremy Hylton | af68c87 | 2005-12-10 18:50:16 +0000 | [diff] [blame] | 826 | PyObject_SetAttrString(PyObject *v, const char *name, PyObject *w) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 827 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 828 | PyObject *s; |
| 829 | int res; |
Guido van Rossum | d8eb1b3 | 1996-08-09 20:52:03 +0000 | [diff] [blame] | 830 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 831 | if (Py_TYPE(v)->tp_setattr != NULL) |
| 832 | return (*Py_TYPE(v)->tp_setattr)(v, (char*)name, w); |
| 833 | s = PyUnicode_InternFromString(name); |
| 834 | if (s == NULL) |
| 835 | return -1; |
| 836 | res = PyObject_SetAttr(v, s, w); |
| 837 | Py_XDECREF(s); |
| 838 | return res; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 839 | } |
| 840 | |
Guido van Rossum | 98ff96a | 1997-05-20 18:34:44 +0000 | [diff] [blame] | 841 | PyObject * |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 842 | _PyObject_GetAttrId(PyObject *v, _Py_Identifier *name) |
| 843 | { |
| 844 | PyObject *result; |
Martin v. Löwis | d10759f | 2011-11-07 13:00:05 +0100 | [diff] [blame] | 845 | PyObject *oname = _PyUnicode_FromId(name); /* borrowed */ |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 846 | if (!oname) |
| 847 | return NULL; |
| 848 | result = PyObject_GetAttr(v, oname); |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 849 | return result; |
| 850 | } |
| 851 | |
| 852 | int |
| 853 | _PyObject_HasAttrId(PyObject *v, _Py_Identifier *name) |
| 854 | { |
| 855 | int result; |
Martin v. Löwis | d10759f | 2011-11-07 13:00:05 +0100 | [diff] [blame] | 856 | PyObject *oname = _PyUnicode_FromId(name); /* borrowed */ |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 857 | if (!oname) |
| 858 | return -1; |
| 859 | result = PyObject_HasAttr(v, oname); |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 860 | return result; |
| 861 | } |
| 862 | |
| 863 | int |
| 864 | _PyObject_SetAttrId(PyObject *v, _Py_Identifier *name, PyObject *w) |
| 865 | { |
| 866 | int result; |
Martin v. Löwis | d10759f | 2011-11-07 13:00:05 +0100 | [diff] [blame] | 867 | PyObject *oname = _PyUnicode_FromId(name); /* borrowed */ |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 868 | if (!oname) |
| 869 | return -1; |
| 870 | result = PyObject_SetAttr(v, oname, w); |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 871 | return result; |
| 872 | } |
| 873 | |
| 874 | PyObject * |
Fred Drake | 100814d | 2000-07-09 15:48:49 +0000 | [diff] [blame] | 875 | PyObject_GetAttr(PyObject *v, PyObject *name) |
Guido van Rossum | 98ff96a | 1997-05-20 18:34:44 +0000 | [diff] [blame] | 876 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 877 | PyTypeObject *tp = Py_TYPE(v); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 878 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 879 | if (!PyUnicode_Check(name)) { |
| 880 | PyErr_Format(PyExc_TypeError, |
| 881 | "attribute name must be string, not '%.200s'", |
| 882 | name->ob_type->tp_name); |
| 883 | return NULL; |
| 884 | } |
| 885 | if (tp->tp_getattro != NULL) |
| 886 | return (*tp->tp_getattro)(v, name); |
| 887 | if (tp->tp_getattr != NULL) { |
| 888 | char *name_str = _PyUnicode_AsString(name); |
| 889 | if (name_str == NULL) |
| 890 | return NULL; |
| 891 | return (*tp->tp_getattr)(v, name_str); |
| 892 | } |
| 893 | PyErr_Format(PyExc_AttributeError, |
| 894 | "'%.50s' object has no attribute '%U'", |
| 895 | tp->tp_name, name); |
| 896 | return NULL; |
Guido van Rossum | 98ff96a | 1997-05-20 18:34:44 +0000 | [diff] [blame] | 897 | } |
| 898 | |
| 899 | int |
Fred Drake | 100814d | 2000-07-09 15:48:49 +0000 | [diff] [blame] | 900 | PyObject_HasAttr(PyObject *v, PyObject *name) |
Guido van Rossum | 98ff96a | 1997-05-20 18:34:44 +0000 | [diff] [blame] | 901 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 902 | PyObject *res = PyObject_GetAttr(v, name); |
| 903 | if (res != NULL) { |
| 904 | Py_DECREF(res); |
| 905 | return 1; |
| 906 | } |
| 907 | PyErr_Clear(); |
| 908 | return 0; |
Guido van Rossum | 98ff96a | 1997-05-20 18:34:44 +0000 | [diff] [blame] | 909 | } |
| 910 | |
| 911 | int |
Fred Drake | 100814d | 2000-07-09 15:48:49 +0000 | [diff] [blame] | 912 | PyObject_SetAttr(PyObject *v, PyObject *name, PyObject *value) |
Guido van Rossum | 98ff96a | 1997-05-20 18:34:44 +0000 | [diff] [blame] | 913 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 914 | PyTypeObject *tp = Py_TYPE(v); |
| 915 | int err; |
Marc-André Lemburg | e44e507 | 2000-09-18 16:20:57 +0000 | [diff] [blame] | 916 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 917 | if (!PyUnicode_Check(name)) { |
| 918 | PyErr_Format(PyExc_TypeError, |
| 919 | "attribute name must be string, not '%.200s'", |
| 920 | name->ob_type->tp_name); |
| 921 | return -1; |
| 922 | } |
| 923 | Py_INCREF(name); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 924 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 925 | PyUnicode_InternInPlace(&name); |
| 926 | if (tp->tp_setattro != NULL) { |
| 927 | err = (*tp->tp_setattro)(v, name, value); |
| 928 | Py_DECREF(name); |
| 929 | return err; |
| 930 | } |
| 931 | if (tp->tp_setattr != NULL) { |
| 932 | char *name_str = _PyUnicode_AsString(name); |
| 933 | if (name_str == NULL) |
| 934 | return -1; |
| 935 | err = (*tp->tp_setattr)(v, name_str, value); |
| 936 | Py_DECREF(name); |
| 937 | return err; |
| 938 | } |
| 939 | Py_DECREF(name); |
| 940 | assert(name->ob_refcnt >= 1); |
| 941 | if (tp->tp_getattr == NULL && tp->tp_getattro == NULL) |
| 942 | PyErr_Format(PyExc_TypeError, |
| 943 | "'%.100s' object has no attributes " |
| 944 | "(%s .%U)", |
| 945 | tp->tp_name, |
| 946 | value==NULL ? "del" : "assign to", |
| 947 | name); |
| 948 | else |
| 949 | PyErr_Format(PyExc_TypeError, |
| 950 | "'%.100s' object has only read-only attributes " |
| 951 | "(%s .%U)", |
| 952 | tp->tp_name, |
| 953 | value==NULL ? "del" : "assign to", |
| 954 | name); |
| 955 | return -1; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 956 | } |
| 957 | |
| 958 | /* Helper to get a pointer to an object's __dict__ slot, if any */ |
| 959 | |
| 960 | PyObject ** |
| 961 | _PyObject_GetDictPtr(PyObject *obj) |
| 962 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 963 | Py_ssize_t dictoffset; |
| 964 | PyTypeObject *tp = Py_TYPE(obj); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 965 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 966 | dictoffset = tp->tp_dictoffset; |
| 967 | if (dictoffset == 0) |
| 968 | return NULL; |
| 969 | if (dictoffset < 0) { |
| 970 | Py_ssize_t tsize; |
| 971 | size_t size; |
Guido van Rossum | 2eb0b87 | 2002-03-01 22:24:49 +0000 | [diff] [blame] | 972 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 973 | tsize = ((PyVarObject *)obj)->ob_size; |
| 974 | if (tsize < 0) |
| 975 | tsize = -tsize; |
| 976 | size = _PyObject_VAR_SIZE(tp, tsize); |
Guido van Rossum | 2eb0b87 | 2002-03-01 22:24:49 +0000 | [diff] [blame] | 977 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 978 | dictoffset += (long)size; |
| 979 | assert(dictoffset > 0); |
| 980 | assert(dictoffset % SIZEOF_VOID_P == 0); |
| 981 | } |
| 982 | return (PyObject **) ((char *)obj + dictoffset); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 983 | } |
| 984 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 985 | PyObject * |
Raymond Hettinger | 1da1dbf | 2003-03-17 19:46:11 +0000 | [diff] [blame] | 986 | PyObject_SelfIter(PyObject *obj) |
Raymond Hettinger | 0153826 | 2003-03-17 08:24:35 +0000 | [diff] [blame] | 987 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 988 | Py_INCREF(obj); |
| 989 | return obj; |
Raymond Hettinger | 0153826 | 2003-03-17 08:24:35 +0000 | [diff] [blame] | 990 | } |
| 991 | |
Amaury Forgeot d'Arc | f343e01 | 2009-01-12 23:58:21 +0000 | [diff] [blame] | 992 | /* Helper used when the __next__ method is removed from a type: |
| 993 | tp_iternext is never NULL and can be safely called without checking |
| 994 | on every iteration. |
| 995 | */ |
| 996 | |
| 997 | PyObject * |
| 998 | _PyObject_NextNotImplemented(PyObject *self) |
| 999 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1000 | PyErr_Format(PyExc_TypeError, |
| 1001 | "'%.200s' object is not iterable", |
| 1002 | Py_TYPE(self)->tp_name); |
| 1003 | return NULL; |
Amaury Forgeot d'Arc | f343e01 | 2009-01-12 23:58:21 +0000 | [diff] [blame] | 1004 | } |
| 1005 | |
Michael W. Hudson | 1593f50 | 2004-09-14 17:09:47 +0000 | [diff] [blame] | 1006 | /* Generic GetAttr functions - put these in your tp_[gs]etattro slot */ |
| 1007 | |
Raymond Hettinger | 0153826 | 2003-03-17 08:24:35 +0000 | [diff] [blame] | 1008 | PyObject * |
Antoine Pitrou | 1a9a9d5 | 2010-08-28 18:17:03 +0000 | [diff] [blame] | 1009 | _PyObject_GenericGetAttrWithDict(PyObject *obj, PyObject *name, PyObject *dict) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1010 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1011 | PyTypeObject *tp = Py_TYPE(obj); |
| 1012 | PyObject *descr = NULL; |
| 1013 | PyObject *res = NULL; |
| 1014 | descrgetfunc f; |
| 1015 | Py_ssize_t dictoffset; |
| 1016 | PyObject **dictptr; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1017 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1018 | if (!PyUnicode_Check(name)){ |
| 1019 | PyErr_Format(PyExc_TypeError, |
| 1020 | "attribute name must be string, not '%.200s'", |
| 1021 | name->ob_type->tp_name); |
| 1022 | return NULL; |
| 1023 | } |
| 1024 | else |
| 1025 | Py_INCREF(name); |
Guido van Rossum | ebca9fc | 2001-12-04 15:54:53 +0000 | [diff] [blame] | 1026 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1027 | if (tp->tp_dict == NULL) { |
| 1028 | if (PyType_Ready(tp) < 0) |
| 1029 | goto done; |
| 1030 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1031 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1032 | descr = _PyType_Lookup(tp, name); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1033 | Py_XINCREF(descr); |
Michael W. Hudson | b2c7de4 | 2003-08-15 13:07:47 +0000 | [diff] [blame] | 1034 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1035 | f = NULL; |
| 1036 | if (descr != NULL) { |
| 1037 | f = descr->ob_type->tp_descr_get; |
| 1038 | if (f != NULL && PyDescr_IsData(descr)) { |
| 1039 | res = f(descr, obj, (PyObject *)obj->ob_type); |
| 1040 | Py_DECREF(descr); |
| 1041 | goto done; |
| 1042 | } |
| 1043 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1044 | |
Antoine Pitrou | 1a9a9d5 | 2010-08-28 18:17:03 +0000 | [diff] [blame] | 1045 | if (dict == NULL) { |
| 1046 | /* Inline _PyObject_GetDictPtr */ |
| 1047 | dictoffset = tp->tp_dictoffset; |
| 1048 | if (dictoffset != 0) { |
| 1049 | if (dictoffset < 0) { |
| 1050 | Py_ssize_t tsize; |
| 1051 | size_t size; |
Guido van Rossum | c66ff44 | 2002-08-19 16:50:48 +0000 | [diff] [blame] | 1052 | |
Antoine Pitrou | 1a9a9d5 | 2010-08-28 18:17:03 +0000 | [diff] [blame] | 1053 | tsize = ((PyVarObject *)obj)->ob_size; |
| 1054 | if (tsize < 0) |
| 1055 | tsize = -tsize; |
| 1056 | size = _PyObject_VAR_SIZE(tp, tsize); |
Guido van Rossum | c66ff44 | 2002-08-19 16:50:48 +0000 | [diff] [blame] | 1057 | |
Antoine Pitrou | 1a9a9d5 | 2010-08-28 18:17:03 +0000 | [diff] [blame] | 1058 | dictoffset += (long)size; |
| 1059 | assert(dictoffset > 0); |
| 1060 | assert(dictoffset % SIZEOF_VOID_P == 0); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1061 | } |
Antoine Pitrou | 1a9a9d5 | 2010-08-28 18:17:03 +0000 | [diff] [blame] | 1062 | dictptr = (PyObject **) ((char *)obj + dictoffset); |
| 1063 | dict = *dictptr; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1064 | } |
| 1065 | } |
Antoine Pitrou | 1a9a9d5 | 2010-08-28 18:17:03 +0000 | [diff] [blame] | 1066 | if (dict != NULL) { |
| 1067 | Py_INCREF(dict); |
| 1068 | res = PyDict_GetItem(dict, name); |
| 1069 | if (res != NULL) { |
| 1070 | Py_INCREF(res); |
| 1071 | Py_XDECREF(descr); |
| 1072 | Py_DECREF(dict); |
| 1073 | goto done; |
| 1074 | } |
| 1075 | Py_DECREF(dict); |
| 1076 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1077 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1078 | if (f != NULL) { |
| 1079 | res = f(descr, obj, (PyObject *)Py_TYPE(obj)); |
| 1080 | Py_DECREF(descr); |
| 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 | if (descr != NULL) { |
| 1085 | res = descr; |
| 1086 | /* descr was already increfed above */ |
| 1087 | goto done; |
| 1088 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1089 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1090 | PyErr_Format(PyExc_AttributeError, |
| 1091 | "'%.50s' object has no attribute '%U'", |
| 1092 | tp->tp_name, name); |
Guido van Rossum | ebca9fc | 2001-12-04 15:54:53 +0000 | [diff] [blame] | 1093 | done: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1094 | Py_DECREF(name); |
| 1095 | return res; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1096 | } |
| 1097 | |
Antoine Pitrou | 1a9a9d5 | 2010-08-28 18:17:03 +0000 | [diff] [blame] | 1098 | PyObject * |
| 1099 | PyObject_GenericGetAttr(PyObject *obj, PyObject *name) |
| 1100 | { |
| 1101 | return _PyObject_GenericGetAttrWithDict(obj, name, NULL); |
| 1102 | } |
| 1103 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1104 | int |
Antoine Pitrou | 1a9a9d5 | 2010-08-28 18:17:03 +0000 | [diff] [blame] | 1105 | _PyObject_GenericSetAttrWithDict(PyObject *obj, PyObject *name, |
| 1106 | PyObject *value, PyObject *dict) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1107 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1108 | PyTypeObject *tp = Py_TYPE(obj); |
| 1109 | PyObject *descr; |
| 1110 | descrsetfunc f; |
| 1111 | PyObject **dictptr; |
| 1112 | int res = -1; |
Guido van Rossum | ebca9fc | 2001-12-04 15:54:53 +0000 | [diff] [blame] | 1113 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1114 | if (!PyUnicode_Check(name)){ |
| 1115 | PyErr_Format(PyExc_TypeError, |
| 1116 | "attribute name must be string, not '%.200s'", |
| 1117 | name->ob_type->tp_name); |
| 1118 | return -1; |
| 1119 | } |
| 1120 | else |
| 1121 | Py_INCREF(name); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1122 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1123 | if (tp->tp_dict == NULL) { |
| 1124 | if (PyType_Ready(tp) < 0) |
| 1125 | goto done; |
| 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 | descr = _PyType_Lookup(tp, name); |
| 1129 | f = NULL; |
| 1130 | if (descr != NULL) { |
| 1131 | f = descr->ob_type->tp_descr_set; |
| 1132 | if (f != NULL && PyDescr_IsData(descr)) { |
| 1133 | res = f(descr, obj, value); |
| 1134 | goto done; |
| 1135 | } |
| 1136 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1137 | |
Antoine Pitrou | 1a9a9d5 | 2010-08-28 18:17:03 +0000 | [diff] [blame] | 1138 | if (dict == NULL) { |
| 1139 | dictptr = _PyObject_GetDictPtr(obj); |
| 1140 | if (dictptr != NULL) { |
| 1141 | dict = *dictptr; |
| 1142 | if (dict == NULL && value != NULL) { |
| 1143 | dict = PyDict_New(); |
| 1144 | if (dict == NULL) |
| 1145 | goto done; |
| 1146 | *dictptr = dict; |
| 1147 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1148 | } |
Antoine Pitrou | 1a9a9d5 | 2010-08-28 18:17:03 +0000 | [diff] [blame] | 1149 | } |
| 1150 | if (dict != NULL) { |
| 1151 | Py_INCREF(dict); |
| 1152 | if (value == NULL) |
| 1153 | res = PyDict_DelItem(dict, name); |
| 1154 | else |
| 1155 | res = PyDict_SetItem(dict, name, value); |
| 1156 | if (res < 0 && PyErr_ExceptionMatches(PyExc_KeyError)) |
| 1157 | PyErr_SetObject(PyExc_AttributeError, name); |
| 1158 | Py_DECREF(dict); |
| 1159 | goto done; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1160 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1161 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1162 | if (f != NULL) { |
| 1163 | res = f(descr, obj, value); |
| 1164 | goto done; |
| 1165 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1166 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1167 | if (descr == NULL) { |
| 1168 | PyErr_Format(PyExc_AttributeError, |
| 1169 | "'%.100s' object has no attribute '%U'", |
| 1170 | tp->tp_name, name); |
| 1171 | goto done; |
| 1172 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1173 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1174 | PyErr_Format(PyExc_AttributeError, |
| 1175 | "'%.50s' object attribute '%U' is read-only", |
| 1176 | tp->tp_name, name); |
Guido van Rossum | ebca9fc | 2001-12-04 15:54:53 +0000 | [diff] [blame] | 1177 | done: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1178 | Py_DECREF(name); |
| 1179 | return res; |
Guido van Rossum | 98ff96a | 1997-05-20 18:34:44 +0000 | [diff] [blame] | 1180 | } |
| 1181 | |
Antoine Pitrou | 1a9a9d5 | 2010-08-28 18:17:03 +0000 | [diff] [blame] | 1182 | int |
| 1183 | PyObject_GenericSetAttr(PyObject *obj, PyObject *name, PyObject *value) |
| 1184 | { |
| 1185 | return _PyObject_GenericSetAttrWithDict(obj, name, value, NULL); |
| 1186 | } |
| 1187 | |
| 1188 | |
Guido van Rossum | 6ac258d | 1993-05-12 08:24:20 +0000 | [diff] [blame] | 1189 | /* Test a value used as condition, e.g., in a for or if statement. |
| 1190 | Return -1 if an error occurred */ |
| 1191 | |
| 1192 | int |
Fred Drake | 100814d | 2000-07-09 15:48:49 +0000 | [diff] [blame] | 1193 | PyObject_IsTrue(PyObject *v) |
Guido van Rossum | 6ac258d | 1993-05-12 08:24:20 +0000 | [diff] [blame] | 1194 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1195 | Py_ssize_t res; |
| 1196 | if (v == Py_True) |
| 1197 | return 1; |
| 1198 | if (v == Py_False) |
| 1199 | return 0; |
| 1200 | if (v == Py_None) |
| 1201 | return 0; |
| 1202 | else if (v->ob_type->tp_as_number != NULL && |
| 1203 | v->ob_type->tp_as_number->nb_bool != NULL) |
| 1204 | res = (*v->ob_type->tp_as_number->nb_bool)(v); |
| 1205 | else if (v->ob_type->tp_as_mapping != NULL && |
| 1206 | v->ob_type->tp_as_mapping->mp_length != NULL) |
| 1207 | res = (*v->ob_type->tp_as_mapping->mp_length)(v); |
| 1208 | else if (v->ob_type->tp_as_sequence != NULL && |
| 1209 | v->ob_type->tp_as_sequence->sq_length != NULL) |
| 1210 | res = (*v->ob_type->tp_as_sequence->sq_length)(v); |
| 1211 | else |
| 1212 | return 1; |
| 1213 | /* if it is negative, it should be either -1 or -2 */ |
| 1214 | 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] | 1215 | } |
| 1216 | |
Tim Peters | 803526b | 2002-07-07 05:13:56 +0000 | [diff] [blame] | 1217 | /* equivalent of 'not v' |
Guido van Rossum | c3d3f96 | 1998-04-09 17:53:59 +0000 | [diff] [blame] | 1218 | Return -1 if an error occurred */ |
| 1219 | |
| 1220 | int |
Fred Drake | 100814d | 2000-07-09 15:48:49 +0000 | [diff] [blame] | 1221 | PyObject_Not(PyObject *v) |
Guido van Rossum | c3d3f96 | 1998-04-09 17:53:59 +0000 | [diff] [blame] | 1222 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1223 | int res; |
| 1224 | res = PyObject_IsTrue(v); |
| 1225 | if (res < 0) |
| 1226 | return res; |
| 1227 | return res == 0; |
Guido van Rossum | c3d3f96 | 1998-04-09 17:53:59 +0000 | [diff] [blame] | 1228 | } |
| 1229 | |
Guido van Rossum | 49b11fe | 1995-01-26 00:38:22 +0000 | [diff] [blame] | 1230 | /* Test whether an object can be called */ |
| 1231 | |
| 1232 | int |
Fred Drake | 100814d | 2000-07-09 15:48:49 +0000 | [diff] [blame] | 1233 | PyCallable_Check(PyObject *x) |
Guido van Rossum | 49b11fe | 1995-01-26 00:38:22 +0000 | [diff] [blame] | 1234 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1235 | if (x == NULL) |
| 1236 | return 0; |
| 1237 | return x->ob_type->tp_call != NULL; |
Guido van Rossum | 49b11fe | 1995-01-26 00:38:22 +0000 | [diff] [blame] | 1238 | } |
| 1239 | |
Tim Peters | 7eea37e | 2001-09-04 22:08:56 +0000 | [diff] [blame] | 1240 | |
Georg Brandl | e32b422 | 2007-03-10 22:13:27 +0000 | [diff] [blame] | 1241 | /* Helper for PyObject_Dir without arguments: returns the local scope. */ |
| 1242 | static PyObject * |
Guido van Rossum | ad7d8d1 | 2007-04-13 01:39:34 +0000 | [diff] [blame] | 1243 | _dir_locals(void) |
Tim Peters | 305b585 | 2001-09-17 02:38:46 +0000 | [diff] [blame] | 1244 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1245 | PyObject *names; |
| 1246 | PyObject *locals = PyEval_GetLocals(); |
Tim Peters | 305b585 | 2001-09-17 02:38:46 +0000 | [diff] [blame] | 1247 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1248 | if (locals == NULL) { |
| 1249 | PyErr_SetString(PyExc_SystemError, "frame does not exist"); |
| 1250 | return NULL; |
| 1251 | } |
Tim Peters | 305b585 | 2001-09-17 02:38:46 +0000 | [diff] [blame] | 1252 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1253 | names = PyMapping_Keys(locals); |
| 1254 | if (!names) |
| 1255 | return NULL; |
| 1256 | if (!PyList_Check(names)) { |
| 1257 | PyErr_Format(PyExc_TypeError, |
| 1258 | "dir(): expected keys() of locals to be a list, " |
| 1259 | "not '%.200s'", Py_TYPE(names)->tp_name); |
| 1260 | Py_DECREF(names); |
| 1261 | return NULL; |
| 1262 | } |
Benjamin Peterson | 3bbb722 | 2011-06-11 16:12:08 -0500 | [diff] [blame] | 1263 | if (PyList_Sort(names)) { |
| 1264 | Py_DECREF(names); |
| 1265 | return NULL; |
| 1266 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1267 | /* the locals don't need to be DECREF'd */ |
| 1268 | return names; |
Georg Brandl | e32b422 | 2007-03-10 22:13:27 +0000 | [diff] [blame] | 1269 | } |
| 1270 | |
Benjamin Peterson | 82b00c1 | 2011-05-24 11:09:06 -0500 | [diff] [blame] | 1271 | /* Helper for PyObject_Dir: object introspection. */ |
Georg Brandl | e32b422 | 2007-03-10 22:13:27 +0000 | [diff] [blame] | 1272 | static PyObject * |
| 1273 | _dir_object(PyObject *obj) |
| 1274 | { |
Benjamin Peterson | 3bbb722 | 2011-06-11 16:12:08 -0500 | [diff] [blame] | 1275 | PyObject *result, *sorted; |
Benjamin Peterson | 7963a35 | 2011-05-23 16:11:05 -0500 | [diff] [blame] | 1276 | static PyObject *dir_str = NULL; |
| 1277 | PyObject *dirfunc = _PyObject_LookupSpecial(obj, "__dir__", &dir_str); |
Georg Brandl | e32b422 | 2007-03-10 22:13:27 +0000 | [diff] [blame] | 1278 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1279 | assert(obj); |
| 1280 | if (dirfunc == NULL) { |
Benjamin Peterson | 82b00c1 | 2011-05-24 11:09:06 -0500 | [diff] [blame] | 1281 | if (!PyErr_Occurred()) |
| 1282 | PyErr_SetString(PyExc_TypeError, "object does not provide __dir__"); |
| 1283 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1284 | } |
Benjamin Peterson | 82b00c1 | 2011-05-24 11:09:06 -0500 | [diff] [blame] | 1285 | /* use __dir__ */ |
| 1286 | result = PyObject_CallFunctionObjArgs(dirfunc, NULL); |
| 1287 | Py_DECREF(dirfunc); |
| 1288 | if (result == NULL) |
| 1289 | return NULL; |
Benjamin Peterson | 3bbb722 | 2011-06-11 16:12:08 -0500 | [diff] [blame] | 1290 | /* return sorted(result) */ |
| 1291 | sorted = PySequence_List(result); |
| 1292 | Py_DECREF(result); |
| 1293 | if (sorted == NULL) |
| 1294 | return NULL; |
| 1295 | if (PyList_Sort(sorted)) { |
| 1296 | Py_DECREF(sorted); |
| 1297 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1298 | } |
Benjamin Peterson | 3bbb722 | 2011-06-11 16:12:08 -0500 | [diff] [blame] | 1299 | return sorted; |
Georg Brandl | e32b422 | 2007-03-10 22:13:27 +0000 | [diff] [blame] | 1300 | } |
| 1301 | |
| 1302 | /* Implementation of dir() -- if obj is NULL, returns the names in the current |
| 1303 | (local) scope. Otherwise, performs introspection of the object: returns a |
| 1304 | sorted list of attribute names (supposedly) accessible from the object |
| 1305 | */ |
| 1306 | PyObject * |
| 1307 | PyObject_Dir(PyObject *obj) |
| 1308 | { |
Benjamin Peterson | 3bbb722 | 2011-06-11 16:12:08 -0500 | [diff] [blame] | 1309 | return (obj == NULL) ? _dir_locals() : _dir_object(obj); |
Tim Peters | 7eea37e | 2001-09-04 22:08:56 +0000 | [diff] [blame] | 1310 | } |
Guido van Rossum | 49b11fe | 1995-01-26 00:38:22 +0000 | [diff] [blame] | 1311 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1312 | /* |
Raymond Hettinger | 66d2be8 | 2011-07-28 09:55:13 -0700 | [diff] [blame] | 1313 | None is a non-NULL undefined value. |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1314 | 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] | 1315 | so there is exactly one (which is indestructible, by the way). |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1316 | */ |
| 1317 | |
Guido van Rossum | 0c182a1 | 1992-03-27 17:26:13 +0000 | [diff] [blame] | 1318 | /* ARGSUSED */ |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1319 | static PyObject * |
Fred Drake | 100814d | 2000-07-09 15:48:49 +0000 | [diff] [blame] | 1320 | none_repr(PyObject *op) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1321 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1322 | return PyUnicode_FromString("None"); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1323 | } |
| 1324 | |
Barry Warsaw | 9bf1644 | 2001-01-23 16:24:35 +0000 | [diff] [blame] | 1325 | /* ARGUSED */ |
| 1326 | static void |
Tim Peters | 803526b | 2002-07-07 05:13:56 +0000 | [diff] [blame] | 1327 | none_dealloc(PyObject* ignore) |
Barry Warsaw | 9bf1644 | 2001-01-23 16:24:35 +0000 | [diff] [blame] | 1328 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1329 | /* This should never get called, but we also don't want to SEGV if |
| 1330 | * we accidentally decref None out of existence. |
| 1331 | */ |
| 1332 | Py_FatalError("deallocating None"); |
Barry Warsaw | 9bf1644 | 2001-01-23 16:24:35 +0000 | [diff] [blame] | 1333 | } |
| 1334 | |
Benjamin Peterson | c4607ae | 2011-07-29 18:19:43 -0500 | [diff] [blame] | 1335 | static PyObject * |
| 1336 | none_new(PyTypeObject *type, PyObject *args, PyObject *kwargs) |
| 1337 | { |
| 1338 | if (PyTuple_GET_SIZE(args) || (kwargs && PyDict_Size(kwargs))) { |
| 1339 | PyErr_SetString(PyExc_TypeError, "NoneType takes no arguments"); |
| 1340 | return NULL; |
| 1341 | } |
| 1342 | Py_RETURN_NONE; |
| 1343 | } |
| 1344 | |
Raymond Hettinger | 66d2be8 | 2011-07-28 09:55:13 -0700 | [diff] [blame] | 1345 | static int |
| 1346 | none_bool(PyObject *v) |
| 1347 | { |
| 1348 | return 0; |
| 1349 | } |
| 1350 | |
| 1351 | static PyNumberMethods none_as_number = { |
| 1352 | 0, /* nb_add */ |
| 1353 | 0, /* nb_subtract */ |
| 1354 | 0, /* nb_multiply */ |
| 1355 | 0, /* nb_remainder */ |
| 1356 | 0, /* nb_divmod */ |
| 1357 | 0, /* nb_power */ |
| 1358 | 0, /* nb_negative */ |
| 1359 | 0, /* nb_positive */ |
| 1360 | 0, /* nb_absolute */ |
| 1361 | (inquiry)none_bool, /* nb_bool */ |
| 1362 | 0, /* nb_invert */ |
| 1363 | 0, /* nb_lshift */ |
| 1364 | 0, /* nb_rshift */ |
| 1365 | 0, /* nb_and */ |
| 1366 | 0, /* nb_xor */ |
| 1367 | 0, /* nb_or */ |
| 1368 | 0, /* nb_int */ |
| 1369 | 0, /* nb_reserved */ |
| 1370 | 0, /* nb_float */ |
| 1371 | 0, /* nb_inplace_add */ |
| 1372 | 0, /* nb_inplace_subtract */ |
| 1373 | 0, /* nb_inplace_multiply */ |
| 1374 | 0, /* nb_inplace_remainder */ |
| 1375 | 0, /* nb_inplace_power */ |
| 1376 | 0, /* nb_inplace_lshift */ |
| 1377 | 0, /* nb_inplace_rshift */ |
| 1378 | 0, /* nb_inplace_and */ |
| 1379 | 0, /* nb_inplace_xor */ |
| 1380 | 0, /* nb_inplace_or */ |
| 1381 | 0, /* nb_floor_divide */ |
| 1382 | 0, /* nb_true_divide */ |
| 1383 | 0, /* nb_inplace_floor_divide */ |
| 1384 | 0, /* nb_inplace_true_divide */ |
| 1385 | 0, /* nb_index */ |
| 1386 | }; |
Barry Warsaw | 9bf1644 | 2001-01-23 16:24:35 +0000 | [diff] [blame] | 1387 | |
Guido van Rossum | ba21a49 | 2001-08-16 08:17:26 +0000 | [diff] [blame] | 1388 | static PyTypeObject PyNone_Type = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1389 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 1390 | "NoneType", |
| 1391 | 0, |
| 1392 | 0, |
| 1393 | none_dealloc, /*tp_dealloc*/ /*never called*/ |
| 1394 | 0, /*tp_print*/ |
| 1395 | 0, /*tp_getattr*/ |
| 1396 | 0, /*tp_setattr*/ |
| 1397 | 0, /*tp_reserved*/ |
| 1398 | none_repr, /*tp_repr*/ |
Raymond Hettinger | 66d2be8 | 2011-07-28 09:55:13 -0700 | [diff] [blame] | 1399 | &none_as_number, /*tp_as_number*/ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1400 | 0, /*tp_as_sequence*/ |
| 1401 | 0, /*tp_as_mapping*/ |
| 1402 | 0, /*tp_hash */ |
Benjamin Peterson | c4607ae | 2011-07-29 18:19:43 -0500 | [diff] [blame] | 1403 | 0, /*tp_call */ |
| 1404 | 0, /*tp_str */ |
| 1405 | 0, /*tp_getattro */ |
| 1406 | 0, /*tp_setattro */ |
| 1407 | 0, /*tp_as_buffer */ |
| 1408 | Py_TPFLAGS_DEFAULT, /*tp_flags */ |
| 1409 | 0, /*tp_doc */ |
| 1410 | 0, /*tp_traverse */ |
| 1411 | 0, /*tp_clear */ |
| 1412 | 0, /*tp_richcompare */ |
| 1413 | 0, /*tp_weaklistoffset */ |
| 1414 | 0, /*tp_iter */ |
| 1415 | 0, /*tp_iternext */ |
| 1416 | 0, /*tp_methods */ |
| 1417 | 0, /*tp_members */ |
| 1418 | 0, /*tp_getset */ |
| 1419 | 0, /*tp_base */ |
| 1420 | 0, /*tp_dict */ |
| 1421 | 0, /*tp_descr_get */ |
| 1422 | 0, /*tp_descr_set */ |
| 1423 | 0, /*tp_dictoffset */ |
| 1424 | 0, /*tp_init */ |
| 1425 | 0, /*tp_alloc */ |
| 1426 | none_new, /*tp_new */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1427 | }; |
| 1428 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1429 | PyObject _Py_NoneStruct = { |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 1430 | _PyObject_EXTRA_INIT |
| 1431 | 1, &PyNone_Type |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1432 | }; |
| 1433 | |
Neil Schemenauer | 5ed85ec | 2001-01-04 01:48:10 +0000 | [diff] [blame] | 1434 | /* NotImplemented is an object that can be used to signal that an |
| 1435 | operation is not implemented for the given type combination. */ |
| 1436 | |
| 1437 | static PyObject * |
| 1438 | NotImplemented_repr(PyObject *op) |
| 1439 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1440 | return PyUnicode_FromString("NotImplemented"); |
Neil Schemenauer | 5ed85ec | 2001-01-04 01:48:10 +0000 | [diff] [blame] | 1441 | } |
| 1442 | |
Benjamin Peterson | 18d7d7a | 2011-07-29 18:27:44 -0500 | [diff] [blame] | 1443 | static PyObject * |
| 1444 | notimplemented_new(PyTypeObject *type, PyObject *args, PyObject *kwargs) |
| 1445 | { |
| 1446 | if (PyTuple_GET_SIZE(args) || (kwargs && PyDict_Size(kwargs))) { |
| 1447 | PyErr_SetString(PyExc_TypeError, "NotImplementedType takes no arguments"); |
| 1448 | return NULL; |
| 1449 | } |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 1450 | Py_RETURN_NOTIMPLEMENTED; |
Benjamin Peterson | 18d7d7a | 2011-07-29 18:27:44 -0500 | [diff] [blame] | 1451 | } |
| 1452 | |
Neil Schemenauer | 5ed85ec | 2001-01-04 01:48:10 +0000 | [diff] [blame] | 1453 | static PyTypeObject PyNotImplemented_Type = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1454 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 1455 | "NotImplementedType", |
| 1456 | 0, |
| 1457 | 0, |
| 1458 | none_dealloc, /*tp_dealloc*/ /*never called*/ |
| 1459 | 0, /*tp_print*/ |
| 1460 | 0, /*tp_getattr*/ |
| 1461 | 0, /*tp_setattr*/ |
| 1462 | 0, /*tp_reserved*/ |
| 1463 | NotImplemented_repr, /*tp_repr*/ |
| 1464 | 0, /*tp_as_number*/ |
| 1465 | 0, /*tp_as_sequence*/ |
| 1466 | 0, /*tp_as_mapping*/ |
| 1467 | 0, /*tp_hash */ |
Benjamin Peterson | 18d7d7a | 2011-07-29 18:27:44 -0500 | [diff] [blame] | 1468 | 0, /*tp_call */ |
| 1469 | 0, /*tp_str */ |
| 1470 | 0, /*tp_getattro */ |
| 1471 | 0, /*tp_setattro */ |
| 1472 | 0, /*tp_as_buffer */ |
| 1473 | Py_TPFLAGS_DEFAULT, /*tp_flags */ |
| 1474 | 0, /*tp_doc */ |
| 1475 | 0, /*tp_traverse */ |
| 1476 | 0, /*tp_clear */ |
| 1477 | 0, /*tp_richcompare */ |
| 1478 | 0, /*tp_weaklistoffset */ |
| 1479 | 0, /*tp_iter */ |
| 1480 | 0, /*tp_iternext */ |
| 1481 | 0, /*tp_methods */ |
| 1482 | 0, /*tp_members */ |
| 1483 | 0, /*tp_getset */ |
| 1484 | 0, /*tp_base */ |
| 1485 | 0, /*tp_dict */ |
| 1486 | 0, /*tp_descr_get */ |
| 1487 | 0, /*tp_descr_set */ |
| 1488 | 0, /*tp_dictoffset */ |
| 1489 | 0, /*tp_init */ |
| 1490 | 0, /*tp_alloc */ |
| 1491 | notimplemented_new, /*tp_new */ |
Neil Schemenauer | 5ed85ec | 2001-01-04 01:48:10 +0000 | [diff] [blame] | 1492 | }; |
| 1493 | |
| 1494 | PyObject _Py_NotImplementedStruct = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1495 | _PyObject_EXTRA_INIT |
| 1496 | 1, &PyNotImplemented_Type |
Neil Schemenauer | 5ed85ec | 2001-01-04 01:48:10 +0000 | [diff] [blame] | 1497 | }; |
| 1498 | |
Guido van Rossum | ba21a49 | 2001-08-16 08:17:26 +0000 | [diff] [blame] | 1499 | void |
| 1500 | _Py_ReadyTypes(void) |
| 1501 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1502 | if (PyType_Ready(&PyType_Type) < 0) |
| 1503 | Py_FatalError("Can't initialize type type"); |
Guido van Rossum | ba21a49 | 2001-08-16 08:17:26 +0000 | [diff] [blame] | 1504 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1505 | if (PyType_Ready(&_PyWeakref_RefType) < 0) |
| 1506 | Py_FatalError("Can't initialize weakref type"); |
Fred Drake | 0a4dd39 | 2004-07-02 18:57:45 +0000 | [diff] [blame] | 1507 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1508 | if (PyType_Ready(&_PyWeakref_CallableProxyType) < 0) |
| 1509 | Py_FatalError("Can't initialize callable weakref proxy type"); |
Benjamin Peterson | fd838e6 | 2009-04-20 02:09:13 +0000 | [diff] [blame] | 1510 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1511 | if (PyType_Ready(&_PyWeakref_ProxyType) < 0) |
| 1512 | Py_FatalError("Can't initialize weakref proxy type"); |
Benjamin Peterson | fd838e6 | 2009-04-20 02:09:13 +0000 | [diff] [blame] | 1513 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1514 | if (PyType_Ready(&PyBool_Type) < 0) |
| 1515 | Py_FatalError("Can't initialize bool type"); |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 1516 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1517 | if (PyType_Ready(&PyByteArray_Type) < 0) |
| 1518 | Py_FatalError("Can't initialize bytearray type"); |
Guido van Rossum | 4dfe8a1 | 2006-04-22 23:28:04 +0000 | [diff] [blame] | 1519 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1520 | if (PyType_Ready(&PyBytes_Type) < 0) |
| 1521 | Py_FatalError("Can't initialize 'str'"); |
Guido van Rossum | cacfc07 | 2002-05-24 19:01:59 +0000 | [diff] [blame] | 1522 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1523 | if (PyType_Ready(&PyList_Type) < 0) |
| 1524 | Py_FatalError("Can't initialize list type"); |
Guido van Rossum | ba21a49 | 2001-08-16 08:17:26 +0000 | [diff] [blame] | 1525 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1526 | if (PyType_Ready(&PyNone_Type) < 0) |
| 1527 | Py_FatalError("Can't initialize None type"); |
Guido van Rossum | ba21a49 | 2001-08-16 08:17:26 +0000 | [diff] [blame] | 1528 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1529 | if (PyType_Ready(&PyNotImplemented_Type) < 0) |
| 1530 | Py_FatalError("Can't initialize NotImplemented type"); |
Benjamin Peterson | ae937c0 | 2009-04-18 20:54:08 +0000 | [diff] [blame] | 1531 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1532 | if (PyType_Ready(&PyTraceBack_Type) < 0) |
| 1533 | Py_FatalError("Can't initialize traceback type"); |
Benjamin Peterson | ae937c0 | 2009-04-18 20:54:08 +0000 | [diff] [blame] | 1534 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1535 | if (PyType_Ready(&PySuper_Type) < 0) |
| 1536 | Py_FatalError("Can't initialize super type"); |
Benjamin Peterson | ae937c0 | 2009-04-18 20:54:08 +0000 | [diff] [blame] | 1537 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1538 | if (PyType_Ready(&PyBaseObject_Type) < 0) |
| 1539 | Py_FatalError("Can't initialize object type"); |
Benjamin Peterson | ae937c0 | 2009-04-18 20:54:08 +0000 | [diff] [blame] | 1540 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1541 | if (PyType_Ready(&PyRange_Type) < 0) |
| 1542 | Py_FatalError("Can't initialize range type"); |
Benjamin Peterson | ae937c0 | 2009-04-18 20:54:08 +0000 | [diff] [blame] | 1543 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1544 | if (PyType_Ready(&PyDict_Type) < 0) |
| 1545 | Py_FatalError("Can't initialize dict type"); |
Benjamin Peterson | ae937c0 | 2009-04-18 20:54:08 +0000 | [diff] [blame] | 1546 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1547 | if (PyType_Ready(&PySet_Type) < 0) |
| 1548 | Py_FatalError("Can't initialize set type"); |
Benjamin Peterson | ae937c0 | 2009-04-18 20:54:08 +0000 | [diff] [blame] | 1549 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1550 | if (PyType_Ready(&PyUnicode_Type) < 0) |
| 1551 | Py_FatalError("Can't initialize str type"); |
Benjamin Peterson | ae937c0 | 2009-04-18 20:54:08 +0000 | [diff] [blame] | 1552 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1553 | if (PyType_Ready(&PySlice_Type) < 0) |
| 1554 | Py_FatalError("Can't initialize slice type"); |
Benjamin Peterson | ae937c0 | 2009-04-18 20:54:08 +0000 | [diff] [blame] | 1555 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1556 | if (PyType_Ready(&PyStaticMethod_Type) < 0) |
| 1557 | Py_FatalError("Can't initialize static method type"); |
Benjamin Peterson | ae937c0 | 2009-04-18 20:54:08 +0000 | [diff] [blame] | 1558 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1559 | if (PyType_Ready(&PyComplex_Type) < 0) |
| 1560 | Py_FatalError("Can't initialize complex type"); |
Skip Montanaro | ba1e0f4 | 2009-10-18 14:25:35 +0000 | [diff] [blame] | 1561 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1562 | if (PyType_Ready(&PyFloat_Type) < 0) |
| 1563 | Py_FatalError("Can't initialize float type"); |
Benjamin Peterson | ae937c0 | 2009-04-18 20:54:08 +0000 | [diff] [blame] | 1564 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1565 | if (PyType_Ready(&PyLong_Type) < 0) |
| 1566 | Py_FatalError("Can't initialize int type"); |
Benjamin Peterson | ae937c0 | 2009-04-18 20:54:08 +0000 | [diff] [blame] | 1567 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1568 | if (PyType_Ready(&PyFrozenSet_Type) < 0) |
| 1569 | Py_FatalError("Can't initialize frozenset type"); |
Benjamin Peterson | ae937c0 | 2009-04-18 20:54:08 +0000 | [diff] [blame] | 1570 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1571 | if (PyType_Ready(&PyProperty_Type) < 0) |
| 1572 | Py_FatalError("Can't initialize property type"); |
Benjamin Peterson | ae937c0 | 2009-04-18 20:54:08 +0000 | [diff] [blame] | 1573 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1574 | if (PyType_Ready(&PyMemoryView_Type) < 0) |
| 1575 | Py_FatalError("Can't initialize memoryview type"); |
Benjamin Peterson | ae937c0 | 2009-04-18 20:54:08 +0000 | [diff] [blame] | 1576 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1577 | if (PyType_Ready(&PyTuple_Type) < 0) |
| 1578 | Py_FatalError("Can't initialize tuple type"); |
Benjamin Peterson | ae937c0 | 2009-04-18 20:54:08 +0000 | [diff] [blame] | 1579 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1580 | if (PyType_Ready(&PyEnum_Type) < 0) |
| 1581 | Py_FatalError("Can't initialize enumerate type"); |
Benjamin Peterson | ae937c0 | 2009-04-18 20:54:08 +0000 | [diff] [blame] | 1582 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1583 | if (PyType_Ready(&PyReversed_Type) < 0) |
| 1584 | Py_FatalError("Can't initialize reversed type"); |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 1585 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1586 | if (PyType_Ready(&PyStdPrinter_Type) < 0) |
| 1587 | Py_FatalError("Can't initialize StdPrinter"); |
Benjamin Peterson | fd838e6 | 2009-04-20 02:09:13 +0000 | [diff] [blame] | 1588 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1589 | if (PyType_Ready(&PyCode_Type) < 0) |
| 1590 | Py_FatalError("Can't initialize code type"); |
Benjamin Peterson | fd838e6 | 2009-04-20 02:09:13 +0000 | [diff] [blame] | 1591 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1592 | if (PyType_Ready(&PyFrame_Type) < 0) |
| 1593 | Py_FatalError("Can't initialize frame type"); |
Benjamin Peterson | fd838e6 | 2009-04-20 02:09:13 +0000 | [diff] [blame] | 1594 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1595 | if (PyType_Ready(&PyCFunction_Type) < 0) |
| 1596 | Py_FatalError("Can't initialize builtin function type"); |
Benjamin Peterson | fd838e6 | 2009-04-20 02:09:13 +0000 | [diff] [blame] | 1597 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1598 | if (PyType_Ready(&PyMethod_Type) < 0) |
| 1599 | Py_FatalError("Can't initialize method type"); |
Benjamin Peterson | fd838e6 | 2009-04-20 02:09:13 +0000 | [diff] [blame] | 1600 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1601 | if (PyType_Ready(&PyFunction_Type) < 0) |
| 1602 | Py_FatalError("Can't initialize function type"); |
Benjamin Peterson | fd838e6 | 2009-04-20 02:09:13 +0000 | [diff] [blame] | 1603 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1604 | if (PyType_Ready(&PyDictProxy_Type) < 0) |
| 1605 | Py_FatalError("Can't initialize dict proxy type"); |
Benjamin Peterson | fd838e6 | 2009-04-20 02:09:13 +0000 | [diff] [blame] | 1606 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1607 | if (PyType_Ready(&PyGen_Type) < 0) |
| 1608 | Py_FatalError("Can't initialize generator type"); |
Benjamin Peterson | fd838e6 | 2009-04-20 02:09:13 +0000 | [diff] [blame] | 1609 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1610 | if (PyType_Ready(&PyGetSetDescr_Type) < 0) |
| 1611 | Py_FatalError("Can't initialize get-set descriptor type"); |
Benjamin Peterson | fd838e6 | 2009-04-20 02:09:13 +0000 | [diff] [blame] | 1612 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1613 | if (PyType_Ready(&PyWrapperDescr_Type) < 0) |
| 1614 | Py_FatalError("Can't initialize wrapper type"); |
Benjamin Peterson | fd838e6 | 2009-04-20 02:09:13 +0000 | [diff] [blame] | 1615 | |
Benjamin Peterson | eff61f6 | 2011-09-01 16:32:31 -0400 | [diff] [blame] | 1616 | if (PyType_Ready(&_PyMethodWrapper_Type) < 0) |
| 1617 | Py_FatalError("Can't initialize method wrapper type"); |
| 1618 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1619 | if (PyType_Ready(&PyEllipsis_Type) < 0) |
| 1620 | Py_FatalError("Can't initialize ellipsis type"); |
Benjamin Peterson | fd838e6 | 2009-04-20 02:09:13 +0000 | [diff] [blame] | 1621 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1622 | if (PyType_Ready(&PyMemberDescr_Type) < 0) |
| 1623 | Py_FatalError("Can't initialize member descriptor type"); |
Benjamin Peterson | 8bc5b68 | 2009-05-09 18:10:51 +0000 | [diff] [blame] | 1624 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1625 | if (PyType_Ready(&PyFilter_Type) < 0) |
| 1626 | Py_FatalError("Can't initialize filter type"); |
Benjamin Peterson | 8bc5b68 | 2009-05-09 18:10:51 +0000 | [diff] [blame] | 1627 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1628 | if (PyType_Ready(&PyMap_Type) < 0) |
| 1629 | Py_FatalError("Can't initialize map type"); |
Benjamin Peterson | 8bc5b68 | 2009-05-09 18:10:51 +0000 | [diff] [blame] | 1630 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1631 | if (PyType_Ready(&PyZip_Type) < 0) |
| 1632 | Py_FatalError("Can't initialize zip type"); |
Guido van Rossum | ba21a49 | 2001-08-16 08:17:26 +0000 | [diff] [blame] | 1633 | } |
| 1634 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1635 | |
Guido van Rossum | 84a9032 | 1996-05-22 16:34:47 +0000 | [diff] [blame] | 1636 | #ifdef Py_TRACE_REFS |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1637 | |
Guido van Rossum | aacdc9d | 1996-08-12 21:32:12 +0000 | [diff] [blame] | 1638 | void |
Fred Drake | 100814d | 2000-07-09 15:48:49 +0000 | [diff] [blame] | 1639 | _Py_NewReference(PyObject *op) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1640 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1641 | _Py_INC_REFTOTAL; |
| 1642 | op->ob_refcnt = 1; |
| 1643 | _Py_AddToAllObjects(op, 1); |
| 1644 | _Py_INC_TPALLOCS(op); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1645 | } |
| 1646 | |
Guido van Rossum | aacdc9d | 1996-08-12 21:32:12 +0000 | [diff] [blame] | 1647 | void |
Fred Drake | 100814d | 2000-07-09 15:48:49 +0000 | [diff] [blame] | 1648 | _Py_ForgetReference(register PyObject *op) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1649 | { |
Guido van Rossum | bffd683 | 2000-01-20 22:32:56 +0000 | [diff] [blame] | 1650 | #ifdef SLOW_UNREF_CHECK |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1651 | register PyObject *p; |
Guido van Rossum | bffd683 | 2000-01-20 22:32:56 +0000 | [diff] [blame] | 1652 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1653 | if (op->ob_refcnt < 0) |
| 1654 | Py_FatalError("UNREF negative refcnt"); |
| 1655 | if (op == &refchain || |
| 1656 | op->_ob_prev->_ob_next != op || op->_ob_next->_ob_prev != op) { |
| 1657 | fprintf(stderr, "* ob\n"); |
| 1658 | _PyObject_Dump(op); |
| 1659 | fprintf(stderr, "* op->_ob_prev->_ob_next\n"); |
| 1660 | _PyObject_Dump(op->_ob_prev->_ob_next); |
| 1661 | fprintf(stderr, "* op->_ob_next->_ob_prev\n"); |
| 1662 | _PyObject_Dump(op->_ob_next->_ob_prev); |
| 1663 | Py_FatalError("UNREF invalid object"); |
| 1664 | } |
Guido van Rossum | 2e8f614 | 1992-09-03 20:32:55 +0000 | [diff] [blame] | 1665 | #ifdef SLOW_UNREF_CHECK |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1666 | for (p = refchain._ob_next; p != &refchain; p = p->_ob_next) { |
| 1667 | if (p == op) |
| 1668 | break; |
| 1669 | } |
| 1670 | if (p == &refchain) /* Not found */ |
| 1671 | Py_FatalError("UNREF unknown object"); |
Guido van Rossum | 2e8f614 | 1992-09-03 20:32:55 +0000 | [diff] [blame] | 1672 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1673 | op->_ob_next->_ob_prev = op->_ob_prev; |
| 1674 | op->_ob_prev->_ob_next = op->_ob_next; |
| 1675 | op->_ob_next = op->_ob_prev = NULL; |
| 1676 | _Py_INC_TPFREES(op); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1677 | } |
| 1678 | |
Guido van Rossum | aacdc9d | 1996-08-12 21:32:12 +0000 | [diff] [blame] | 1679 | void |
Fred Drake | 100814d | 2000-07-09 15:48:49 +0000 | [diff] [blame] | 1680 | _Py_Dealloc(PyObject *op) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1681 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1682 | destructor dealloc = Py_TYPE(op)->tp_dealloc; |
| 1683 | _Py_ForgetReference(op); |
| 1684 | (*dealloc)(op); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1685 | } |
| 1686 | |
Tim Peters | 269b2a6 | 2003-04-17 19:52:29 +0000 | [diff] [blame] | 1687 | /* Print all live objects. Because PyObject_Print is called, the |
| 1688 | * interpreter must be in a healthy state. |
| 1689 | */ |
Guido van Rossum | aacdc9d | 1996-08-12 21:32:12 +0000 | [diff] [blame] | 1690 | void |
Fred Drake | 100814d | 2000-07-09 15:48:49 +0000 | [diff] [blame] | 1691 | _Py_PrintReferences(FILE *fp) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1692 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1693 | PyObject *op; |
| 1694 | fprintf(fp, "Remaining objects:\n"); |
| 1695 | for (op = refchain._ob_next; op != &refchain; op = op->_ob_next) { |
| 1696 | fprintf(fp, "%p [%" PY_FORMAT_SIZE_T "d] ", op, op->ob_refcnt); |
| 1697 | if (PyObject_Print(op, fp, 0) != 0) |
| 1698 | PyErr_Clear(); |
| 1699 | putc('\n', fp); |
| 1700 | } |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1701 | } |
| 1702 | |
Tim Peters | 269b2a6 | 2003-04-17 19:52:29 +0000 | [diff] [blame] | 1703 | /* Print the addresses of all live objects. Unlike _Py_PrintReferences, this |
| 1704 | * doesn't make any calls to the Python C API, so is always safe to call. |
| 1705 | */ |
| 1706 | void |
| 1707 | _Py_PrintReferenceAddresses(FILE *fp) |
| 1708 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1709 | PyObject *op; |
| 1710 | fprintf(fp, "Remaining object addresses:\n"); |
| 1711 | for (op = refchain._ob_next; op != &refchain; op = op->_ob_next) |
| 1712 | fprintf(fp, "%p [%" PY_FORMAT_SIZE_T "d] %s\n", op, |
| 1713 | op->ob_refcnt, Py_TYPE(op)->tp_name); |
Tim Peters | 269b2a6 | 2003-04-17 19:52:29 +0000 | [diff] [blame] | 1714 | } |
| 1715 | |
Sjoerd Mullender | 6ec3c65 | 1995-08-29 09:18:14 +0000 | [diff] [blame] | 1716 | PyObject * |
Fred Drake | 100814d | 2000-07-09 15:48:49 +0000 | [diff] [blame] | 1717 | _Py_GetObjects(PyObject *self, PyObject *args) |
Sjoerd Mullender | 6ec3c65 | 1995-08-29 09:18:14 +0000 | [diff] [blame] | 1718 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1719 | int i, n; |
| 1720 | PyObject *t = NULL; |
| 1721 | PyObject *res, *op; |
Sjoerd Mullender | 6ec3c65 | 1995-08-29 09:18:14 +0000 | [diff] [blame] | 1722 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1723 | if (!PyArg_ParseTuple(args, "i|O", &n, &t)) |
| 1724 | return NULL; |
| 1725 | op = refchain._ob_next; |
| 1726 | res = PyList_New(0); |
| 1727 | if (res == NULL) |
| 1728 | return NULL; |
| 1729 | for (i = 0; (n == 0 || i < n) && op != &refchain; i++) { |
| 1730 | while (op == self || op == args || op == res || op == t || |
| 1731 | (t != NULL && Py_TYPE(op) != (PyTypeObject *) t)) { |
| 1732 | op = op->_ob_next; |
| 1733 | if (op == &refchain) |
| 1734 | return res; |
| 1735 | } |
| 1736 | if (PyList_Append(res, op) < 0) { |
| 1737 | Py_DECREF(res); |
| 1738 | return NULL; |
| 1739 | } |
| 1740 | op = op->_ob_next; |
| 1741 | } |
| 1742 | return res; |
Sjoerd Mullender | 6ec3c65 | 1995-08-29 09:18:14 +0000 | [diff] [blame] | 1743 | } |
| 1744 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1745 | #endif |
Guido van Rossum | 97ead3f | 1996-01-12 01:24:09 +0000 | [diff] [blame] | 1746 | |
Benjamin Peterson | b173f78 | 2009-05-05 22:31:58 +0000 | [diff] [blame] | 1747 | /* Hack to force loading of pycapsule.o */ |
| 1748 | PyTypeObject *_PyCapsule_hack = &PyCapsule_Type; |
| 1749 | |
| 1750 | |
Guido van Rossum | 84a9032 | 1996-05-22 16:34:47 +0000 | [diff] [blame] | 1751 | /* Hack to force loading of abstract.o */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1752 | Py_ssize_t (*_Py_abstract_hack)(PyObject *) = PyObject_Size; |
Guido van Rossum | e09fb55 | 1997-08-05 02:04:34 +0000 | [diff] [blame] | 1753 | |
| 1754 | |
Andrew M. Kuchling | 1582a3a | 2000-08-16 12:27:23 +0000 | [diff] [blame] | 1755 | /* Python's malloc wrappers (see pymem.h) */ |
Guido van Rossum | e09fb55 | 1997-08-05 02:04:34 +0000 | [diff] [blame] | 1756 | |
Thomas Wouters | 334fb89 | 2000-07-25 12:56:38 +0000 | [diff] [blame] | 1757 | void * |
Fred Drake | 100814d | 2000-07-09 15:48:49 +0000 | [diff] [blame] | 1758 | PyMem_Malloc(size_t nbytes) |
Guido van Rossum | e09fb55 | 1997-08-05 02:04:34 +0000 | [diff] [blame] | 1759 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1760 | return PyMem_MALLOC(nbytes); |
Guido van Rossum | e09fb55 | 1997-08-05 02:04:34 +0000 | [diff] [blame] | 1761 | } |
| 1762 | |
Thomas Wouters | 334fb89 | 2000-07-25 12:56:38 +0000 | [diff] [blame] | 1763 | void * |
| 1764 | PyMem_Realloc(void *p, size_t nbytes) |
Guido van Rossum | e09fb55 | 1997-08-05 02:04:34 +0000 | [diff] [blame] | 1765 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1766 | return PyMem_REALLOC(p, nbytes); |
Guido van Rossum | e09fb55 | 1997-08-05 02:04:34 +0000 | [diff] [blame] | 1767 | } |
| 1768 | |
| 1769 | void |
Thomas Wouters | 334fb89 | 2000-07-25 12:56:38 +0000 | [diff] [blame] | 1770 | PyMem_Free(void *p) |
Guido van Rossum | e09fb55 | 1997-08-05 02:04:34 +0000 | [diff] [blame] | 1771 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1772 | PyMem_FREE(p); |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 1773 | } |
| 1774 | |
| 1775 | |
Guido van Rossum | 8661036 | 1998-04-10 22:32:46 +0000 | [diff] [blame] | 1776 | /* These methods are used to control infinite recursion in repr, str, print, |
| 1777 | etc. Container objects that may recursively contain themselves, |
| 1778 | e.g. builtin dictionaries and lists, should used Py_ReprEnter() and |
| 1779 | Py_ReprLeave() to avoid infinite recursion. |
| 1780 | |
| 1781 | Py_ReprEnter() returns 0 the first time it is called for a particular |
| 1782 | object and 1 every time thereafter. It returns -1 if an exception |
| 1783 | occurred. Py_ReprLeave() has no return value. |
| 1784 | |
| 1785 | See dictobject.c and listobject.c for examples of use. |
| 1786 | */ |
| 1787 | |
| 1788 | #define KEY "Py_Repr" |
| 1789 | |
| 1790 | int |
Fred Drake | 100814d | 2000-07-09 15:48:49 +0000 | [diff] [blame] | 1791 | Py_ReprEnter(PyObject *obj) |
Guido van Rossum | 8661036 | 1998-04-10 22:32:46 +0000 | [diff] [blame] | 1792 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1793 | PyObject *dict; |
| 1794 | PyObject *list; |
| 1795 | Py_ssize_t i; |
Guido van Rossum | 8661036 | 1998-04-10 22:32:46 +0000 | [diff] [blame] | 1796 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1797 | dict = PyThreadState_GetDict(); |
| 1798 | if (dict == NULL) |
| 1799 | return 0; |
| 1800 | list = PyDict_GetItemString(dict, KEY); |
| 1801 | if (list == NULL) { |
| 1802 | list = PyList_New(0); |
| 1803 | if (list == NULL) |
| 1804 | return -1; |
| 1805 | if (PyDict_SetItemString(dict, KEY, list) < 0) |
| 1806 | return -1; |
| 1807 | Py_DECREF(list); |
| 1808 | } |
| 1809 | i = PyList_GET_SIZE(list); |
| 1810 | while (--i >= 0) { |
| 1811 | if (PyList_GET_ITEM(list, i) == obj) |
| 1812 | return 1; |
| 1813 | } |
| 1814 | PyList_Append(list, obj); |
| 1815 | return 0; |
Guido van Rossum | 8661036 | 1998-04-10 22:32:46 +0000 | [diff] [blame] | 1816 | } |
| 1817 | |
| 1818 | void |
Fred Drake | 100814d | 2000-07-09 15:48:49 +0000 | [diff] [blame] | 1819 | Py_ReprLeave(PyObject *obj) |
Guido van Rossum | 8661036 | 1998-04-10 22:32:46 +0000 | [diff] [blame] | 1820 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1821 | PyObject *dict; |
| 1822 | PyObject *list; |
| 1823 | Py_ssize_t i; |
Guido van Rossum | 8661036 | 1998-04-10 22:32:46 +0000 | [diff] [blame] | 1824 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1825 | dict = PyThreadState_GetDict(); |
| 1826 | if (dict == NULL) |
| 1827 | return; |
| 1828 | list = PyDict_GetItemString(dict, KEY); |
| 1829 | if (list == NULL || !PyList_Check(list)) |
| 1830 | return; |
| 1831 | i = PyList_GET_SIZE(list); |
| 1832 | /* Count backwards because we always expect obj to be list[-1] */ |
| 1833 | while (--i >= 0) { |
| 1834 | if (PyList_GET_ITEM(list, i) == obj) { |
| 1835 | PyList_SetSlice(list, i, i + 1, NULL); |
| 1836 | break; |
| 1837 | } |
| 1838 | } |
Guido van Rossum | 8661036 | 1998-04-10 22:32:46 +0000 | [diff] [blame] | 1839 | } |
Guido van Rossum | d724b23 | 2000-03-13 16:01:29 +0000 | [diff] [blame] | 1840 | |
Tim Peters | 803526b | 2002-07-07 05:13:56 +0000 | [diff] [blame] | 1841 | /* Trashcan support. */ |
Guido van Rossum | d724b23 | 2000-03-13 16:01:29 +0000 | [diff] [blame] | 1842 | |
Tim Peters | 803526b | 2002-07-07 05:13:56 +0000 | [diff] [blame] | 1843 | /* Current call-stack depth of tp_dealloc calls. */ |
Guido van Rossum | d724b23 | 2000-03-13 16:01:29 +0000 | [diff] [blame] | 1844 | int _PyTrash_delete_nesting = 0; |
Guido van Rossum | e92e610 | 2000-04-24 15:40:53 +0000 | [diff] [blame] | 1845 | |
Tim Peters | 803526b | 2002-07-07 05:13:56 +0000 | [diff] [blame] | 1846 | /* List of objects that still need to be cleaned up, singly linked via their |
| 1847 | * gc headers' gc_prev pointers. |
| 1848 | */ |
| 1849 | PyObject *_PyTrash_delete_later = NULL; |
Guido van Rossum | d724b23 | 2000-03-13 16:01:29 +0000 | [diff] [blame] | 1850 | |
Tim Peters | 803526b | 2002-07-07 05:13:56 +0000 | [diff] [blame] | 1851 | /* Add op to the _PyTrash_delete_later list. Called when the current |
| 1852 | * call-stack depth gets large. op must be a currently untracked gc'ed |
| 1853 | * object, with refcount 0. Py_DECREF must already have been called on it. |
| 1854 | */ |
Guido van Rossum | d724b23 | 2000-03-13 16:01:29 +0000 | [diff] [blame] | 1855 | void |
Fred Drake | 100814d | 2000-07-09 15:48:49 +0000 | [diff] [blame] | 1856 | _PyTrash_deposit_object(PyObject *op) |
Guido van Rossum | d724b23 | 2000-03-13 16:01:29 +0000 | [diff] [blame] | 1857 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1858 | assert(PyObject_IS_GC(op)); |
| 1859 | assert(_Py_AS_GC(op)->gc.gc_refs == _PyGC_REFS_UNTRACKED); |
| 1860 | assert(op->ob_refcnt == 0); |
| 1861 | _Py_AS_GC(op)->gc.gc_prev = (PyGC_Head *)_PyTrash_delete_later; |
| 1862 | _PyTrash_delete_later = op; |
Guido van Rossum | d724b23 | 2000-03-13 16:01:29 +0000 | [diff] [blame] | 1863 | } |
| 1864 | |
Tim Peters | 803526b | 2002-07-07 05:13:56 +0000 | [diff] [blame] | 1865 | /* Dealloccate all the objects in the _PyTrash_delete_later list. Called when |
| 1866 | * the call-stack unwinds again. |
| 1867 | */ |
Guido van Rossum | d724b23 | 2000-03-13 16:01:29 +0000 | [diff] [blame] | 1868 | void |
Fred Drake | 100814d | 2000-07-09 15:48:49 +0000 | [diff] [blame] | 1869 | _PyTrash_destroy_chain(void) |
Guido van Rossum | d724b23 | 2000-03-13 16:01:29 +0000 | [diff] [blame] | 1870 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1871 | while (_PyTrash_delete_later) { |
| 1872 | PyObject *op = _PyTrash_delete_later; |
| 1873 | destructor dealloc = Py_TYPE(op)->tp_dealloc; |
Neil Schemenauer | f589c05 | 2002-03-29 03:05:54 +0000 | [diff] [blame] | 1874 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1875 | _PyTrash_delete_later = |
| 1876 | (PyObject*) _Py_AS_GC(op)->gc.gc_prev; |
Neil Schemenauer | f589c05 | 2002-03-29 03:05:54 +0000 | [diff] [blame] | 1877 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1878 | /* Call the deallocator directly. This used to try to |
| 1879 | * fool Py_DECREF into calling it indirectly, but |
| 1880 | * Py_DECREF was already called on this object, and in |
| 1881 | * assorted non-release builds calling Py_DECREF again ends |
| 1882 | * up distorting allocation statistics. |
| 1883 | */ |
| 1884 | assert(op->ob_refcnt == 0); |
| 1885 | ++_PyTrash_delete_nesting; |
| 1886 | (*dealloc)(op); |
| 1887 | --_PyTrash_delete_nesting; |
| 1888 | } |
Guido van Rossum | d724b23 | 2000-03-13 16:01:29 +0000 | [diff] [blame] | 1889 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1890 | |
Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 1891 | #ifndef Py_TRACE_REFS |
| 1892 | /* For Py_LIMITED_API, we need an out-of-line version of _Py_Dealloc. |
| 1893 | Define this here, so we can undefine the macro. */ |
| 1894 | #undef _Py_Dealloc |
| 1895 | PyAPI_FUNC(void) _Py_Dealloc(PyObject *); |
| 1896 | void |
| 1897 | _Py_Dealloc(PyObject *op) |
| 1898 | { |
| 1899 | _Py_INC_TPFREES(op) _Py_COUNT_ALLOCS_COMMA |
| 1900 | (*Py_TYPE(op)->tp_dealloc)(op); |
| 1901 | } |
| 1902 | #endif |
| 1903 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1904 | #ifdef __cplusplus |
| 1905 | } |
| 1906 | #endif |