Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 1 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 2 | /* Generic object operations; and implementation of None (NoObject) */ |
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" |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 5 | |
Tim Peters | 3459251 | 2002-07-11 06:23:50 +0000 | [diff] [blame] | 6 | #ifdef Py_REF_DEBUG |
Mark Hammond | a290527 | 2002-07-29 13:42:14 +0000 | [diff] [blame] | 7 | long _Py_RefTotal; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 8 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 9 | |
Mark Hammond | a290527 | 2002-07-29 13:42:14 +0000 | [diff] [blame] | 10 | int Py_DivisionWarningFlag; |
Guido van Rossum | 393661d | 2001-08-31 17:40:15 +0000 | [diff] [blame] | 11 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 12 | /* Object allocation routines used by NEWOBJ and NEWVAROBJ macros. |
| 13 | These are used by the individual routines for object creation. |
| 14 | Do not call them otherwise, they do not initialize the object! */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 15 | |
Tim Peters | 78be799 | 2003-03-23 02:51:01 +0000 | [diff] [blame] | 16 | #ifdef Py_TRACE_REFS |
Tim Peters | 7571a0f | 2003-03-23 17:52:28 +0000 | [diff] [blame] | 17 | /* Head of circular doubly-linked list of all objects. These are linked |
| 18 | * together via the _ob_prev and _ob_next members of a PyObject, which |
| 19 | * exist only in a Py_TRACE_REFS build. |
| 20 | */ |
Tim Peters | 78be799 | 2003-03-23 02:51:01 +0000 | [diff] [blame] | 21 | static PyObject refchain = {&refchain, &refchain}; |
Tim Peters | 36eb4df | 2003-03-23 03:33:13 +0000 | [diff] [blame] | 22 | |
Tim Peters | 7571a0f | 2003-03-23 17:52:28 +0000 | [diff] [blame] | 23 | /* Insert op at the front of the list of all objects. If force is true, |
| 24 | * op is added even if _ob_prev and _ob_next are non-NULL already. If |
| 25 | * force is false amd _ob_prev or _ob_next are non-NULL, do nothing. |
| 26 | * force should be true if and only if op points to freshly allocated, |
| 27 | * uninitialized memory, or you've unlinked op from the list and are |
Tim Peters | 51f8d38 | 2003-03-23 18:06:08 +0000 | [diff] [blame] | 28 | * relinking it into the front. |
Tim Peters | 7571a0f | 2003-03-23 17:52:28 +0000 | [diff] [blame] | 29 | * Note that objects are normally added to the list via _Py_NewReference, |
| 30 | * which is called by PyObject_Init. Not all objects are initialized that |
| 31 | * way, though; exceptions include statically allocated type objects, and |
| 32 | * statically allocated singletons (like Py_True and Py_None). |
| 33 | */ |
Tim Peters | 36eb4df | 2003-03-23 03:33:13 +0000 | [diff] [blame] | 34 | void |
Tim Peters | 7571a0f | 2003-03-23 17:52:28 +0000 | [diff] [blame] | 35 | _Py_AddToAllObjects(PyObject *op, int force) |
Tim Peters | 36eb4df | 2003-03-23 03:33:13 +0000 | [diff] [blame] | 36 | { |
Tim Peters | 7571a0f | 2003-03-23 17:52:28 +0000 | [diff] [blame] | 37 | #ifdef Py_DEBUG |
| 38 | if (!force) { |
| 39 | /* If it's initialized memory, op must be in or out of |
| 40 | * the list unambiguously. |
| 41 | */ |
| 42 | assert((op->_ob_prev == NULL) == (op->_ob_next == NULL)); |
| 43 | } |
Tim Peters | 78be799 | 2003-03-23 02:51:01 +0000 | [diff] [blame] | 44 | #endif |
Tim Peters | 7571a0f | 2003-03-23 17:52:28 +0000 | [diff] [blame] | 45 | if (force || op->_ob_prev == NULL) { |
| 46 | op->_ob_next = refchain._ob_next; |
| 47 | op->_ob_prev = &refchain; |
| 48 | refchain._ob_next->_ob_prev = op; |
| 49 | refchain._ob_next = op; |
| 50 | } |
| 51 | } |
| 52 | #endif /* Py_TRACE_REFS */ |
Tim Peters | 78be799 | 2003-03-23 02:51:01 +0000 | [diff] [blame] | 53 | |
Sjoerd Mullender | a9c3c22 | 1993-10-11 12:54:31 +0000 | [diff] [blame] | 54 | #ifdef COUNT_ALLOCS |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 55 | static PyTypeObject *type_list; |
Sjoerd Mullender | 842d2cc | 1993-10-15 16:18:48 +0000 | [diff] [blame] | 56 | extern int tuple_zero_allocs, fast_tuple_allocs; |
| 57 | extern int quick_int_allocs, quick_neg_int_allocs; |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 58 | extern int null_strings, one_strings; |
Sjoerd Mullender | a9c3c22 | 1993-10-11 12:54:31 +0000 | [diff] [blame] | 59 | void |
Fred Drake | 100814d | 2000-07-09 15:48:49 +0000 | [diff] [blame] | 60 | dump_counts(void) |
Sjoerd Mullender | a9c3c22 | 1993-10-11 12:54:31 +0000 | [diff] [blame] | 61 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 62 | PyTypeObject *tp; |
Sjoerd Mullender | a9c3c22 | 1993-10-11 12:54:31 +0000 | [diff] [blame] | 63 | |
| 64 | for (tp = type_list; tp; tp = tp->tp_next) |
Sjoerd Mullender | 52c1f51 | 1993-10-25 08:40:52 +0000 | [diff] [blame] | 65 | fprintf(stderr, "%s alloc'd: %d, freed: %d, max in use: %d\n", |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 66 | tp->tp_name, tp->tp_allocs, tp->tp_frees, |
Sjoerd Mullender | 52c1f51 | 1993-10-25 08:40:52 +0000 | [diff] [blame] | 67 | tp->tp_maxalloc); |
| 68 | fprintf(stderr, "fast tuple allocs: %d, empty: %d\n", |
| 69 | fast_tuple_allocs, tuple_zero_allocs); |
| 70 | fprintf(stderr, "fast int allocs: pos: %d, neg: %d\n", |
| 71 | quick_int_allocs, quick_neg_int_allocs); |
| 72 | fprintf(stderr, "null strings: %d, 1-strings: %d\n", |
| 73 | null_strings, one_strings); |
Sjoerd Mullender | a9c3c22 | 1993-10-11 12:54:31 +0000 | [diff] [blame] | 74 | } |
| 75 | |
Sjoerd Mullender | 6ec3c65 | 1995-08-29 09:18:14 +0000 | [diff] [blame] | 76 | PyObject * |
Fred Drake | 100814d | 2000-07-09 15:48:49 +0000 | [diff] [blame] | 77 | get_counts(void) |
Sjoerd Mullender | 6ec3c65 | 1995-08-29 09:18:14 +0000 | [diff] [blame] | 78 | { |
| 79 | PyTypeObject *tp; |
| 80 | PyObject *result; |
| 81 | PyObject *v; |
| 82 | |
| 83 | result = PyList_New(0); |
| 84 | if (result == NULL) |
| 85 | return NULL; |
| 86 | for (tp = type_list; tp; tp = tp->tp_next) { |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 87 | v = Py_BuildValue("(siii)", tp->tp_name, tp->tp_allocs, |
| 88 | tp->tp_frees, tp->tp_maxalloc); |
Sjoerd Mullender | 6ec3c65 | 1995-08-29 09:18:14 +0000 | [diff] [blame] | 89 | if (v == NULL) { |
| 90 | Py_DECREF(result); |
| 91 | return NULL; |
| 92 | } |
| 93 | if (PyList_Append(result, v) < 0) { |
| 94 | Py_DECREF(v); |
| 95 | Py_DECREF(result); |
| 96 | return NULL; |
| 97 | } |
| 98 | Py_DECREF(v); |
| 99 | } |
| 100 | return result; |
| 101 | } |
| 102 | |
Sjoerd Mullender | a9c3c22 | 1993-10-11 12:54:31 +0000 | [diff] [blame] | 103 | void |
Fred Drake | 100814d | 2000-07-09 15:48:49 +0000 | [diff] [blame] | 104 | inc_count(PyTypeObject *tp) |
Sjoerd Mullender | a9c3c22 | 1993-10-11 12:54:31 +0000 | [diff] [blame] | 105 | { |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 106 | if (tp->tp_allocs == 0) { |
Guido van Rossum | d8953cb | 1995-04-06 14:46:26 +0000 | [diff] [blame] | 107 | /* first time; insert in linked list */ |
Sjoerd Mullender | a9c3c22 | 1993-10-11 12:54:31 +0000 | [diff] [blame] | 108 | if (tp->tp_next != NULL) /* sanity check */ |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 109 | Py_FatalError("XXX inc_count sanity check"); |
Sjoerd Mullender | a9c3c22 | 1993-10-11 12:54:31 +0000 | [diff] [blame] | 110 | tp->tp_next = type_list; |
Tim Peters | c6a3ff6 | 2002-07-08 22:11:52 +0000 | [diff] [blame] | 111 | /* Note that as of Python 2.2, heap-allocated type objects |
| 112 | * can go away, but this code requires that they stay alive |
| 113 | * until program exit. That's why we're careful with |
| 114 | * refcounts here. type_list gets a new reference to tp, |
| 115 | * while ownership of the reference type_list used to hold |
| 116 | * (if any) was transferred to tp->tp_next in the line above. |
| 117 | * tp is thus effectively immortal after this. |
| 118 | */ |
| 119 | Py_INCREF(tp); |
Sjoerd Mullender | a9c3c22 | 1993-10-11 12:54:31 +0000 | [diff] [blame] | 120 | type_list = tp; |
Tim Peters | 3e40c7f | 2003-03-23 03:04:32 +0000 | [diff] [blame] | 121 | #ifdef Py_TRACE_REFS |
Tim Peters | 7571a0f | 2003-03-23 17:52:28 +0000 | [diff] [blame] | 122 | /* Also insert in the doubly-linked list of all objects, |
| 123 | * if not already there. |
| 124 | */ |
| 125 | _Py_AddToAllObjects((PyObject *)tp, 0); |
Tim Peters | 78be799 | 2003-03-23 02:51:01 +0000 | [diff] [blame] | 126 | #endif |
Sjoerd Mullender | a9c3c22 | 1993-10-11 12:54:31 +0000 | [diff] [blame] | 127 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 128 | tp->tp_allocs++; |
| 129 | if (tp->tp_allocs - tp->tp_frees > tp->tp_maxalloc) |
| 130 | tp->tp_maxalloc = tp->tp_allocs - tp->tp_frees; |
Sjoerd Mullender | a9c3c22 | 1993-10-11 12:54:31 +0000 | [diff] [blame] | 131 | } |
| 132 | #endif |
| 133 | |
Tim Peters | 7c321a8 | 2002-07-09 02:57:01 +0000 | [diff] [blame] | 134 | #ifdef Py_REF_DEBUG |
| 135 | /* Log a fatal error; doesn't return. */ |
| 136 | void |
| 137 | _Py_NegativeRefcount(const char *fname, int lineno, PyObject *op) |
| 138 | { |
| 139 | char buf[300]; |
| 140 | |
| 141 | PyOS_snprintf(buf, sizeof(buf), |
| 142 | "%s:%i object at %p has negative ref count %i", |
| 143 | fname, lineno, op, op->ob_refcnt); |
| 144 | Py_FatalError(buf); |
| 145 | } |
| 146 | |
| 147 | #endif /* Py_REF_DEBUG */ |
| 148 | |
Thomas Heller | 1328b52 | 2004-04-22 17:23:49 +0000 | [diff] [blame] | 149 | void |
| 150 | Py_IncRef(PyObject *o) |
| 151 | { |
| 152 | Py_XINCREF(o); |
| 153 | } |
| 154 | |
| 155 | void |
| 156 | Py_DecRef(PyObject *o) |
| 157 | { |
| 158 | Py_XDECREF(o); |
| 159 | } |
| 160 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 161 | PyObject * |
Fred Drake | 100814d | 2000-07-09 15:48:49 +0000 | [diff] [blame] | 162 | PyObject_Init(PyObject *op, PyTypeObject *tp) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 163 | { |
Guido van Rossum | 6e08c14 | 2002-10-11 20:37:24 +0000 | [diff] [blame] | 164 | if (op == NULL) |
| 165 | return PyErr_NoMemory(); |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 166 | /* Any changes should be reflected in PyObject_INIT (objimpl.h) */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 167 | op->ob_type = tp; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 168 | _Py_NewReference(op); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 169 | return op; |
| 170 | } |
| 171 | |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 172 | PyVarObject * |
Fred Drake | 100814d | 2000-07-09 15:48:49 +0000 | [diff] [blame] | 173 | PyObject_InitVar(PyVarObject *op, PyTypeObject *tp, int size) |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 174 | { |
Guido van Rossum | 6e08c14 | 2002-10-11 20:37:24 +0000 | [diff] [blame] | 175 | if (op == NULL) |
| 176 | return (PyVarObject *) PyErr_NoMemory(); |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 177 | /* Any changes should be reflected in PyObject_INIT_VAR */ |
| 178 | op->ob_size = size; |
| 179 | op->ob_type = tp; |
| 180 | _Py_NewReference((PyObject *)op); |
| 181 | return op; |
| 182 | } |
| 183 | |
| 184 | PyObject * |
Fred Drake | 100814d | 2000-07-09 15:48:49 +0000 | [diff] [blame] | 185 | _PyObject_New(PyTypeObject *tp) |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 186 | { |
| 187 | PyObject *op; |
| 188 | op = (PyObject *) PyObject_MALLOC(_PyObject_SIZE(tp)); |
| 189 | if (op == NULL) |
| 190 | return PyErr_NoMemory(); |
| 191 | return PyObject_INIT(op, tp); |
| 192 | } |
| 193 | |
Guido van Rossum | d0c87ee | 1997-05-15 21:31:03 +0000 | [diff] [blame] | 194 | PyVarObject * |
Tim Peters | 6d483d3 | 2001-10-06 21:27:34 +0000 | [diff] [blame] | 195 | _PyObject_NewVar(PyTypeObject *tp, int nitems) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 196 | { |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 197 | PyVarObject *op; |
Tim Peters | f2a67da | 2001-10-07 03:54:51 +0000 | [diff] [blame] | 198 | const size_t size = _PyObject_VAR_SIZE(tp, nitems); |
Tim Peters | 6d483d3 | 2001-10-06 21:27:34 +0000 | [diff] [blame] | 199 | op = (PyVarObject *) PyObject_MALLOC(size); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 200 | if (op == NULL) |
Guido van Rossum | d0c87ee | 1997-05-15 21:31:03 +0000 | [diff] [blame] | 201 | return (PyVarObject *)PyErr_NoMemory(); |
Tim Peters | 6d483d3 | 2001-10-06 21:27:34 +0000 | [diff] [blame] | 202 | return PyObject_INIT_VAR(op, tp, nitems); |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 203 | } |
| 204 | |
Neil Schemenauer | bdf0eed | 2002-04-12 03:08:42 +0000 | [diff] [blame] | 205 | /* for binary compatibility with 2.2 */ |
| 206 | #undef _PyObject_Del |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 207 | void |
Fred Drake | 100814d | 2000-07-09 15:48:49 +0000 | [diff] [blame] | 208 | _PyObject_Del(PyObject *op) |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 209 | { |
Guido van Rossum | 4cc6ac7 | 2000-07-01 01:00:38 +0000 | [diff] [blame] | 210 | PyObject_FREE(op); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 211 | } |
| 212 | |
Neal Norwitz | 1a99750 | 2003-01-13 20:13:12 +0000 | [diff] [blame] | 213 | /* Implementation of PyObject_Print with recursion checking */ |
| 214 | static int |
| 215 | internal_print(PyObject *op, FILE *fp, int flags, int nesting) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 216 | { |
Guido van Rossum | 278ef59 | 1991-07-27 21:40:24 +0000 | [diff] [blame] | 217 | int ret = 0; |
Neal Norwitz | 1a99750 | 2003-01-13 20:13:12 +0000 | [diff] [blame] | 218 | if (nesting > 10) { |
| 219 | PyErr_SetString(PyExc_RuntimeError, "print recursion"); |
| 220 | return -1; |
| 221 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 222 | if (PyErr_CheckSignals()) |
Guido van Rossum | 9093361 | 1991-06-07 16:10:43 +0000 | [diff] [blame] | 223 | return -1; |
Guido van Rossum | 9b00dfa | 1998-04-28 16:06:54 +0000 | [diff] [blame] | 224 | #ifdef USE_STACKCHECK |
| 225 | if (PyOS_CheckStack()) { |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 226 | PyErr_SetString(PyExc_MemoryError, "stack overflow"); |
Guido van Rossum | 9b00dfa | 1998-04-28 16:06:54 +0000 | [diff] [blame] | 227 | return -1; |
| 228 | } |
| 229 | #endif |
Guido van Rossum | 687ef6e | 2000-01-12 16:28:58 +0000 | [diff] [blame] | 230 | clearerr(fp); /* Clear any previous error condition */ |
Guido van Rossum | 9093361 | 1991-06-07 16:10:43 +0000 | [diff] [blame] | 231 | if (op == NULL) { |
| 232 | fprintf(fp, "<nil>"); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 233 | } |
Guido van Rossum | 9093361 | 1991-06-07 16:10:43 +0000 | [diff] [blame] | 234 | else { |
| 235 | if (op->ob_refcnt <= 0) |
Fred Drake | a44d353 | 2000-06-30 15:01:00 +0000 | [diff] [blame] | 236 | fprintf(fp, "<refcnt %u at %p>", |
| 237 | op->ob_refcnt, op); |
Guido van Rossum | 2e8f614 | 1992-09-03 20:32:55 +0000 | [diff] [blame] | 238 | else if (op->ob_type->tp_print == NULL) { |
Guido van Rossum | 4f288ab | 2001-05-01 16:53:37 +0000 | [diff] [blame] | 239 | PyObject *s; |
| 240 | if (flags & Py_PRINT_RAW) |
| 241 | s = PyObject_Str(op); |
| 242 | else |
| 243 | s = PyObject_Repr(op); |
| 244 | if (s == NULL) |
| 245 | ret = -1; |
Guido van Rossum | 2e8f614 | 1992-09-03 20:32:55 +0000 | [diff] [blame] | 246 | else { |
Neal Norwitz | 1a99750 | 2003-01-13 20:13:12 +0000 | [diff] [blame] | 247 | ret = internal_print(s, fp, Py_PRINT_RAW, |
| 248 | nesting+1); |
Guido van Rossum | 2e8f614 | 1992-09-03 20:32:55 +0000 | [diff] [blame] | 249 | } |
Guido van Rossum | 4f288ab | 2001-05-01 16:53:37 +0000 | [diff] [blame] | 250 | Py_XDECREF(s); |
Guido van Rossum | 2e8f614 | 1992-09-03 20:32:55 +0000 | [diff] [blame] | 251 | } |
Guido van Rossum | 9093361 | 1991-06-07 16:10:43 +0000 | [diff] [blame] | 252 | else |
Guido van Rossum | 278ef59 | 1991-07-27 21:40:24 +0000 | [diff] [blame] | 253 | ret = (*op->ob_type->tp_print)(op, fp, flags); |
Guido van Rossum | 9093361 | 1991-06-07 16:10:43 +0000 | [diff] [blame] | 254 | } |
Guido van Rossum | 278ef59 | 1991-07-27 21:40:24 +0000 | [diff] [blame] | 255 | if (ret == 0) { |
| 256 | if (ferror(fp)) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 257 | PyErr_SetFromErrno(PyExc_IOError); |
Guido van Rossum | 278ef59 | 1991-07-27 21:40:24 +0000 | [diff] [blame] | 258 | clearerr(fp); |
| 259 | ret = -1; |
| 260 | } |
| 261 | } |
| 262 | return ret; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 263 | } |
| 264 | |
Neal Norwitz | 1a99750 | 2003-01-13 20:13:12 +0000 | [diff] [blame] | 265 | int |
| 266 | PyObject_Print(PyObject *op, FILE *fp, int flags) |
| 267 | { |
| 268 | return internal_print(op, fp, flags, 0); |
| 269 | } |
| 270 | |
| 271 | |
Barry Warsaw | 9bf1644 | 2001-01-23 16:24:35 +0000 | [diff] [blame] | 272 | /* For debugging convenience. See Misc/gdbinit for some useful gdb hooks */ |
Tim Peters | 803526b | 2002-07-07 05:13:56 +0000 | [diff] [blame] | 273 | void _PyObject_Dump(PyObject* op) |
Barry Warsaw | 9bf1644 | 2001-01-23 16:24:35 +0000 | [diff] [blame] | 274 | { |
Barry Warsaw | eefb107 | 2001-02-22 22:39:18 +0000 | [diff] [blame] | 275 | if (op == NULL) |
| 276 | fprintf(stderr, "NULL\n"); |
| 277 | else { |
Guido van Rossum | 5f5512d | 2001-09-14 15:50:08 +0000 | [diff] [blame] | 278 | fprintf(stderr, "object : "); |
Barry Warsaw | eefb107 | 2001-02-22 22:39:18 +0000 | [diff] [blame] | 279 | (void)PyObject_Print(op, stderr, 0); |
Guido van Rossum | 5f5512d | 2001-09-14 15:50:08 +0000 | [diff] [blame] | 280 | fprintf(stderr, "\n" |
| 281 | "type : %s\n" |
| 282 | "refcount: %d\n" |
| 283 | "address : %p\n", |
| 284 | op->ob_type==NULL ? "NULL" : op->ob_type->tp_name, |
| 285 | op->ob_refcnt, |
| 286 | op); |
Barry Warsaw | eefb107 | 2001-02-22 22:39:18 +0000 | [diff] [blame] | 287 | } |
Barry Warsaw | 9bf1644 | 2001-01-23 16:24:35 +0000 | [diff] [blame] | 288 | } |
Barry Warsaw | 903138f | 2001-01-23 16:33:18 +0000 | [diff] [blame] | 289 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 290 | PyObject * |
Fred Drake | 100814d | 2000-07-09 15:48:49 +0000 | [diff] [blame] | 291 | PyObject_Repr(PyObject *v) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 292 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 293 | if (PyErr_CheckSignals()) |
Guido van Rossum | 9093361 | 1991-06-07 16:10:43 +0000 | [diff] [blame] | 294 | return NULL; |
Guido van Rossum | 9b00dfa | 1998-04-28 16:06:54 +0000 | [diff] [blame] | 295 | #ifdef USE_STACKCHECK |
| 296 | if (PyOS_CheckStack()) { |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 297 | PyErr_SetString(PyExc_MemoryError, "stack overflow"); |
Guido van Rossum | 9b00dfa | 1998-04-28 16:06:54 +0000 | [diff] [blame] | 298 | return NULL; |
| 299 | } |
| 300 | #endif |
Guido van Rossum | 9093361 | 1991-06-07 16:10:43 +0000 | [diff] [blame] | 301 | if (v == NULL) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 302 | return PyString_FromString("<NULL>"); |
Barry Warsaw | 7ce3694 | 2001-08-24 18:34:26 +0000 | [diff] [blame] | 303 | else if (v->ob_type->tp_repr == NULL) |
Guido van Rossum | 21922aa | 2001-08-30 20:26:05 +0000 | [diff] [blame] | 304 | return PyString_FromFormat("<%s object at %p>", |
Barry Warsaw | 7ce3694 | 2001-08-24 18:34:26 +0000 | [diff] [blame] | 305 | v->ob_type->tp_name, v); |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 306 | else { |
| 307 | PyObject *res; |
| 308 | res = (*v->ob_type->tp_repr)(v); |
| 309 | if (res == NULL) |
| 310 | return NULL; |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 311 | #ifdef Py_USING_UNICODE |
Fredrik Lundh | efecc7d | 2000-07-01 14:31:09 +0000 | [diff] [blame] | 312 | if (PyUnicode_Check(res)) { |
| 313 | PyObject* str; |
Fredrik Lundh | 2a1e060 | 2000-07-08 17:43:32 +0000 | [diff] [blame] | 314 | str = PyUnicode_AsUnicodeEscapeString(res); |
Marc-André Lemburg | 891bc65 | 2000-07-03 09:57:53 +0000 | [diff] [blame] | 315 | Py_DECREF(res); |
| 316 | if (str) |
Fredrik Lundh | efecc7d | 2000-07-01 14:31:09 +0000 | [diff] [blame] | 317 | res = str; |
Marc-André Lemburg | 891bc65 | 2000-07-03 09:57:53 +0000 | [diff] [blame] | 318 | else |
| 319 | return NULL; |
Fredrik Lundh | efecc7d | 2000-07-01 14:31:09 +0000 | [diff] [blame] | 320 | } |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 321 | #endif |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 322 | if (!PyString_Check(res)) { |
| 323 | PyErr_Format(PyExc_TypeError, |
Guido van Rossum | 5db862d | 2000-04-10 12:46:51 +0000 | [diff] [blame] | 324 | "__repr__ returned non-string (type %.200s)", |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 325 | res->ob_type->tp_name); |
| 326 | Py_DECREF(res); |
| 327 | return NULL; |
| 328 | } |
| 329 | return res; |
| 330 | } |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 331 | } |
| 332 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 333 | PyObject * |
Fred Drake | 100814d | 2000-07-09 15:48:49 +0000 | [diff] [blame] | 334 | PyObject_Str(PyObject *v) |
Guido van Rossum | c600411 | 1993-11-05 10:22:19 +0000 | [diff] [blame] | 335 | { |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 336 | PyObject *res; |
Tim Peters | 803526b | 2002-07-07 05:13:56 +0000 | [diff] [blame] | 337 | |
Guido van Rossum | c600411 | 1993-11-05 10:22:19 +0000 | [diff] [blame] | 338 | if (v == NULL) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 339 | return PyString_FromString("<NULL>"); |
Tim Peters | 5a49ade | 2001-09-11 01:41:59 +0000 | [diff] [blame] | 340 | if (PyString_CheckExact(v)) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 341 | Py_INCREF(v); |
Guido van Rossum | c600411 | 1993-11-05 10:22:19 +0000 | [diff] [blame] | 342 | return v; |
| 343 | } |
Guido van Rossum | 4f288ab | 2001-05-01 16:53:37 +0000 | [diff] [blame] | 344 | if (v->ob_type->tp_str == NULL) |
| 345 | return PyObject_Repr(v); |
| 346 | |
| 347 | res = (*v->ob_type->tp_str)(v); |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 348 | if (res == NULL) |
| 349 | return NULL; |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 350 | #ifdef Py_USING_UNICODE |
Marc-André Lemburg | 891bc65 | 2000-07-03 09:57:53 +0000 | [diff] [blame] | 351 | if (PyUnicode_Check(res)) { |
| 352 | PyObject* str; |
| 353 | str = PyUnicode_AsEncodedString(res, NULL, NULL); |
| 354 | Py_DECREF(res); |
| 355 | if (str) |
| 356 | res = str; |
| 357 | else |
| 358 | return NULL; |
| 359 | } |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 360 | #endif |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 361 | if (!PyString_Check(res)) { |
| 362 | PyErr_Format(PyExc_TypeError, |
Guido van Rossum | 5db862d | 2000-04-10 12:46:51 +0000 | [diff] [blame] | 363 | "__str__ returned non-string (type %.200s)", |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 364 | res->ob_type->tp_name); |
| 365 | Py_DECREF(res); |
| 366 | return NULL; |
| 367 | } |
| 368 | return res; |
Guido van Rossum | c600411 | 1993-11-05 10:22:19 +0000 | [diff] [blame] | 369 | } |
| 370 | |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 371 | #ifdef Py_USING_UNICODE |
Guido van Rossum | a3af41d | 2001-01-18 22:07:06 +0000 | [diff] [blame] | 372 | PyObject * |
| 373 | PyObject_Unicode(PyObject *v) |
| 374 | { |
| 375 | PyObject *res; |
Tim Peters | 803526b | 2002-07-07 05:13:56 +0000 | [diff] [blame] | 376 | |
Guido van Rossum | a3af41d | 2001-01-18 22:07:06 +0000 | [diff] [blame] | 377 | if (v == NULL) |
| 378 | res = PyString_FromString("<NULL>"); |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 379 | if (PyUnicode_CheckExact(v)) { |
Guido van Rossum | a3af41d | 2001-01-18 22:07:06 +0000 | [diff] [blame] | 380 | Py_INCREF(v); |
| 381 | return v; |
| 382 | } |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 383 | if (PyUnicode_Check(v)) { |
| 384 | /* For a Unicode subtype that's not a Unicode object, |
| 385 | return a true Unicode object with the same data. */ |
| 386 | return PyUnicode_FromUnicode(PyUnicode_AS_UNICODE(v), |
| 387 | PyUnicode_GET_SIZE(v)); |
| 388 | } |
| 389 | if (PyString_Check(v)) { |
Marc-André Lemburg | ae60534 | 2001-03-25 19:16:13 +0000 | [diff] [blame] | 390 | Py_INCREF(v); |
Guido van Rossum | a3af41d | 2001-01-18 22:07:06 +0000 | [diff] [blame] | 391 | res = v; |
Marc-André Lemburg | ae60534 | 2001-03-25 19:16:13 +0000 | [diff] [blame] | 392 | } |
Guido van Rossum | a3af41d | 2001-01-18 22:07:06 +0000 | [diff] [blame] | 393 | else { |
| 394 | PyObject *func; |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 395 | static PyObject *unicodestr; |
| 396 | /* XXX As soon as we have a tp_unicode slot, we should |
| 397 | check this before trying the __unicode__ |
| 398 | method. */ |
| 399 | if (unicodestr == NULL) { |
| 400 | unicodestr= PyString_InternFromString( |
| 401 | "__unicode__"); |
| 402 | if (unicodestr == NULL) |
Guido van Rossum | a3af41d | 2001-01-18 22:07:06 +0000 | [diff] [blame] | 403 | return NULL; |
| 404 | } |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 405 | func = PyObject_GetAttr(v, unicodestr); |
| 406 | if (func != NULL) { |
Guido van Rossum | a3af41d | 2001-01-18 22:07:06 +0000 | [diff] [blame] | 407 | res = PyEval_CallObject(func, (PyObject *)NULL); |
| 408 | Py_DECREF(func); |
| 409 | } |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 410 | else { |
| 411 | PyErr_Clear(); |
| 412 | if (v->ob_type->tp_str != NULL) |
| 413 | res = (*v->ob_type->tp_str)(v); |
| 414 | else |
| 415 | res = PyObject_Repr(v); |
| 416 | } |
Guido van Rossum | a3af41d | 2001-01-18 22:07:06 +0000 | [diff] [blame] | 417 | } |
| 418 | if (res == NULL) |
| 419 | return NULL; |
| 420 | if (!PyUnicode_Check(res)) { |
Guido van Rossum | b8c65bc | 2001-10-19 02:01:31 +0000 | [diff] [blame] | 421 | PyObject *str; |
| 422 | str = PyUnicode_FromEncodedObject(res, NULL, "strict"); |
Guido van Rossum | a3af41d | 2001-01-18 22:07:06 +0000 | [diff] [blame] | 423 | Py_DECREF(res); |
| 424 | if (str) |
| 425 | res = str; |
| 426 | else |
| 427 | return NULL; |
| 428 | } |
| 429 | return res; |
| 430 | } |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 431 | #endif |
Guido van Rossum | a3af41d | 2001-01-18 22:07:06 +0000 | [diff] [blame] | 432 | |
| 433 | |
Guido van Rossum | a407300 | 2002-05-31 20:03:54 +0000 | [diff] [blame] | 434 | /* Helper to warn about deprecated tp_compare return values. Return: |
| 435 | -2 for an exception; |
| 436 | -1 if v < w; |
| 437 | 0 if v == w; |
| 438 | 1 if v > w. |
| 439 | (This function cannot return 2.) |
| 440 | */ |
| 441 | static int |
| 442 | adjust_tp_compare(int c) |
| 443 | { |
| 444 | if (PyErr_Occurred()) { |
| 445 | if (c != -1 && c != -2) { |
| 446 | PyObject *t, *v, *tb; |
| 447 | PyErr_Fetch(&t, &v, &tb); |
| 448 | if (PyErr_Warn(PyExc_RuntimeWarning, |
| 449 | "tp_compare didn't return -1 or -2 " |
| 450 | "for exception") < 0) { |
| 451 | Py_XDECREF(t); |
| 452 | Py_XDECREF(v); |
| 453 | Py_XDECREF(tb); |
| 454 | } |
| 455 | else |
| 456 | PyErr_Restore(t, v, tb); |
| 457 | } |
| 458 | return -2; |
| 459 | } |
| 460 | else if (c < -1 || c > 1) { |
| 461 | if (PyErr_Warn(PyExc_RuntimeWarning, |
| 462 | "tp_compare didn't return -1, 0 or 1") < 0) |
| 463 | return -2; |
| 464 | else |
| 465 | return c < -1 ? -1 : 1; |
| 466 | } |
| 467 | else { |
| 468 | assert(c >= -1 && c <= 1); |
| 469 | return c; |
| 470 | } |
| 471 | } |
| 472 | |
| 473 | |
Guido van Rossum | d1f06b9 | 2001-01-24 22:14:43 +0000 | [diff] [blame] | 474 | /* Macro to get the tp_richcompare field of a type if defined */ |
| 475 | #define RICHCOMPARE(t) (PyType_HasFeature((t), Py_TPFLAGS_HAVE_RICHCOMPARE) \ |
| 476 | ? (t)->tp_richcompare : NULL) |
| 477 | |
Guido van Rossum | e797ec1 | 2001-01-17 15:24:28 +0000 | [diff] [blame] | 478 | /* Map rich comparison operators to their swapped version, e.g. LT --> GT */ |
Brett Cannon | a5ca2e7 | 2004-09-25 01:37:24 +0000 | [diff] [blame^] | 479 | 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] | 480 | |
Guido van Rossum | e797ec1 | 2001-01-17 15:24:28 +0000 | [diff] [blame] | 481 | /* Try a genuine rich comparison, returning an object. Return: |
| 482 | NULL for exception; |
| 483 | NotImplemented if this particular rich comparison is not implemented or |
| 484 | undefined; |
| 485 | some object not equal to NotImplemented if it is implemented |
| 486 | (this latter object may not be a Boolean). |
| 487 | */ |
| 488 | static PyObject * |
| 489 | try_rich_compare(PyObject *v, PyObject *w, int op) |
| 490 | { |
| 491 | richcmpfunc f; |
| 492 | PyObject *res; |
| 493 | |
Guido van Rossum | 2ed6bf8 | 2001-09-27 20:30:07 +0000 | [diff] [blame] | 494 | if (v->ob_type != w->ob_type && |
| 495 | PyType_IsSubtype(w->ob_type, v->ob_type) && |
| 496 | (f = RICHCOMPARE(w->ob_type)) != NULL) { |
Tim Peters | f4aca75 | 2004-09-23 02:39:37 +0000 | [diff] [blame] | 497 | res = (*f)(w, v, _Py_SwappedOp[op]); |
Guido van Rossum | 2ed6bf8 | 2001-09-27 20:30:07 +0000 | [diff] [blame] | 498 | if (res != Py_NotImplemented) |
| 499 | return res; |
| 500 | Py_DECREF(res); |
| 501 | } |
Guido van Rossum | d1f06b9 | 2001-01-24 22:14:43 +0000 | [diff] [blame] | 502 | if ((f = RICHCOMPARE(v->ob_type)) != NULL) { |
Guido van Rossum | e797ec1 | 2001-01-17 15:24:28 +0000 | [diff] [blame] | 503 | res = (*f)(v, w, op); |
| 504 | if (res != Py_NotImplemented) |
| 505 | return res; |
| 506 | Py_DECREF(res); |
| 507 | } |
Guido van Rossum | d1f06b9 | 2001-01-24 22:14:43 +0000 | [diff] [blame] | 508 | if ((f = RICHCOMPARE(w->ob_type)) != NULL) { |
Tim Peters | f4aca75 | 2004-09-23 02:39:37 +0000 | [diff] [blame] | 509 | return (*f)(w, v, _Py_SwappedOp[op]); |
Guido van Rossum | e797ec1 | 2001-01-17 15:24:28 +0000 | [diff] [blame] | 510 | } |
| 511 | res = Py_NotImplemented; |
| 512 | Py_INCREF(res); |
| 513 | return res; |
| 514 | } |
| 515 | |
| 516 | /* Try a genuine rich comparison, returning an int. Return: |
| 517 | -1 for exception (including the case where try_rich_compare() returns an |
| 518 | object that's not a Boolean); |
Tim Peters | c99213f | 2001-11-04 05:57:16 +0000 | [diff] [blame] | 519 | 0 if the outcome is false; |
| 520 | 1 if the outcome is true; |
| 521 | 2 if this particular rich comparison is not implemented or undefined. |
Guido van Rossum | e797ec1 | 2001-01-17 15:24:28 +0000 | [diff] [blame] | 522 | */ |
Neil Schemenauer | 5ed85ec | 2001-01-04 01:48:10 +0000 | [diff] [blame] | 523 | static int |
Guido van Rossum | e797ec1 | 2001-01-17 15:24:28 +0000 | [diff] [blame] | 524 | try_rich_compare_bool(PyObject *v, PyObject *w, int op) |
| 525 | { |
| 526 | PyObject *res; |
| 527 | int ok; |
| 528 | |
Guido van Rossum | d1f06b9 | 2001-01-24 22:14:43 +0000 | [diff] [blame] | 529 | if (RICHCOMPARE(v->ob_type) == NULL && RICHCOMPARE(w->ob_type) == NULL) |
Guido van Rossum | e797ec1 | 2001-01-17 15:24:28 +0000 | [diff] [blame] | 530 | return 2; /* Shortcut, avoid INCREF+DECREF */ |
| 531 | res = try_rich_compare(v, w, op); |
| 532 | if (res == NULL) |
| 533 | return -1; |
| 534 | if (res == Py_NotImplemented) { |
| 535 | Py_DECREF(res); |
| 536 | return 2; |
| 537 | } |
| 538 | ok = PyObject_IsTrue(res); |
| 539 | Py_DECREF(res); |
| 540 | return ok; |
| 541 | } |
| 542 | |
| 543 | /* Try rich comparisons to determine a 3-way comparison. Return: |
| 544 | -2 for an exception; |
Tim Peters | c99213f | 2001-11-04 05:57:16 +0000 | [diff] [blame] | 545 | -1 if v < w; |
| 546 | 0 if v == w; |
| 547 | 1 if v > w; |
| 548 | 2 if this particular rich comparison is not implemented or undefined. |
Guido van Rossum | e797ec1 | 2001-01-17 15:24:28 +0000 | [diff] [blame] | 549 | */ |
| 550 | static int |
| 551 | try_rich_to_3way_compare(PyObject *v, PyObject *w) |
| 552 | { |
Guido van Rossum | 2ffbf6b | 2001-01-17 21:27:02 +0000 | [diff] [blame] | 553 | static struct { int op; int outcome; } tries[3] = { |
| 554 | /* Try this operator, and if it is true, use this outcome: */ |
| 555 | {Py_EQ, 0}, |
| 556 | {Py_LT, -1}, |
| 557 | {Py_GT, 1}, |
| 558 | }; |
| 559 | int i; |
| 560 | |
Guido van Rossum | d1f06b9 | 2001-01-24 22:14:43 +0000 | [diff] [blame] | 561 | if (RICHCOMPARE(v->ob_type) == NULL && RICHCOMPARE(w->ob_type) == NULL) |
Guido van Rossum | e797ec1 | 2001-01-17 15:24:28 +0000 | [diff] [blame] | 562 | return 2; /* Shortcut */ |
Guido van Rossum | 2ffbf6b | 2001-01-17 21:27:02 +0000 | [diff] [blame] | 563 | |
| 564 | for (i = 0; i < 3; i++) { |
| 565 | switch (try_rich_compare_bool(v, w, tries[i].op)) { |
| 566 | case -1: |
Tim Peters | 6d60b2e | 2001-05-07 20:53:51 +0000 | [diff] [blame] | 567 | return -2; |
Guido van Rossum | 2ffbf6b | 2001-01-17 21:27:02 +0000 | [diff] [blame] | 568 | case 1: |
| 569 | return tries[i].outcome; |
| 570 | } |
Guido van Rossum | e797ec1 | 2001-01-17 15:24:28 +0000 | [diff] [blame] | 571 | } |
Guido van Rossum | 2ffbf6b | 2001-01-17 21:27:02 +0000 | [diff] [blame] | 572 | |
| 573 | return 2; |
Guido van Rossum | e797ec1 | 2001-01-17 15:24:28 +0000 | [diff] [blame] | 574 | } |
| 575 | |
| 576 | /* Try a 3-way comparison, returning an int. Return: |
| 577 | -2 for an exception; |
Tim Peters | c99213f | 2001-11-04 05:57:16 +0000 | [diff] [blame] | 578 | -1 if v < w; |
| 579 | 0 if v == w; |
| 580 | 1 if v > w; |
| 581 | 2 if this particular 3-way comparison is not implemented or undefined. |
Guido van Rossum | e797ec1 | 2001-01-17 15:24:28 +0000 | [diff] [blame] | 582 | */ |
| 583 | static int |
| 584 | try_3way_compare(PyObject *v, PyObject *w) |
Neil Schemenauer | 5ed85ec | 2001-01-04 01:48:10 +0000 | [diff] [blame] | 585 | { |
| 586 | int c; |
Guido van Rossum | e797ec1 | 2001-01-17 15:24:28 +0000 | [diff] [blame] | 587 | cmpfunc f; |
| 588 | |
| 589 | /* Comparisons involving instances are given to instance_compare, |
| 590 | which has the same return conventions as this function. */ |
| 591 | |
Guido van Rossum | ab3b034 | 2001-09-18 20:38:53 +0000 | [diff] [blame] | 592 | f = v->ob_type->tp_compare; |
Guido van Rossum | e797ec1 | 2001-01-17 15:24:28 +0000 | [diff] [blame] | 593 | if (PyInstance_Check(v)) |
Guido van Rossum | ab3b034 | 2001-09-18 20:38:53 +0000 | [diff] [blame] | 594 | return (*f)(v, w); |
Guido van Rossum | e797ec1 | 2001-01-17 15:24:28 +0000 | [diff] [blame] | 595 | if (PyInstance_Check(w)) |
| 596 | return (*w->ob_type->tp_compare)(v, w); |
| 597 | |
Guido van Rossum | ab3b034 | 2001-09-18 20:38:53 +0000 | [diff] [blame] | 598 | /* If both have the same (non-NULL) tp_compare, use it. */ |
| 599 | if (f != NULL && f == w->ob_type->tp_compare) { |
| 600 | c = (*f)(v, w); |
Guido van Rossum | a407300 | 2002-05-31 20:03:54 +0000 | [diff] [blame] | 601 | return adjust_tp_compare(c); |
Guido van Rossum | ab3b034 | 2001-09-18 20:38:53 +0000 | [diff] [blame] | 602 | } |
| 603 | |
| 604 | /* If either tp_compare is _PyObject_SlotCompare, that's safe. */ |
| 605 | if (f == _PyObject_SlotCompare || |
| 606 | w->ob_type->tp_compare == _PyObject_SlotCompare) |
| 607 | return _PyObject_SlotCompare(v, w); |
| 608 | |
Guido van Rossum | e797ec1 | 2001-01-17 15:24:28 +0000 | [diff] [blame] | 609 | /* Try coercion; if it fails, give up */ |
| 610 | c = PyNumber_CoerceEx(&v, &w); |
| 611 | if (c < 0) |
| 612 | return -2; |
| 613 | if (c > 0) |
| 614 | return 2; |
| 615 | |
| 616 | /* Try v's comparison, if defined */ |
| 617 | if ((f = v->ob_type->tp_compare) != NULL) { |
| 618 | c = (*f)(v, w); |
| 619 | Py_DECREF(v); |
| 620 | Py_DECREF(w); |
Guido van Rossum | a407300 | 2002-05-31 20:03:54 +0000 | [diff] [blame] | 621 | return adjust_tp_compare(c); |
Guido van Rossum | e797ec1 | 2001-01-17 15:24:28 +0000 | [diff] [blame] | 622 | } |
| 623 | |
| 624 | /* Try w's comparison, if defined */ |
| 625 | if ((f = w->ob_type->tp_compare) != NULL) { |
| 626 | c = (*f)(w, v); /* swapped! */ |
| 627 | Py_DECREF(v); |
| 628 | Py_DECREF(w); |
Guido van Rossum | a407300 | 2002-05-31 20:03:54 +0000 | [diff] [blame] | 629 | c = adjust_tp_compare(c); |
| 630 | if (c >= -1) |
| 631 | return -c; /* Swapped! */ |
| 632 | else |
| 633 | return c; |
Guido van Rossum | e797ec1 | 2001-01-17 15:24:28 +0000 | [diff] [blame] | 634 | } |
| 635 | |
| 636 | /* No comparison defined */ |
| 637 | Py_DECREF(v); |
| 638 | Py_DECREF(w); |
| 639 | return 2; |
Neil Schemenauer | 5ed85ec | 2001-01-04 01:48:10 +0000 | [diff] [blame] | 640 | } |
| 641 | |
Guido van Rossum | e797ec1 | 2001-01-17 15:24:28 +0000 | [diff] [blame] | 642 | /* Final fallback 3-way comparison, returning an int. Return: |
| 643 | -2 if an error occurred; |
Tim Peters | c99213f | 2001-11-04 05:57:16 +0000 | [diff] [blame] | 644 | -1 if v < w; |
| 645 | 0 if v == w; |
| 646 | 1 if v > w. |
Guido van Rossum | e797ec1 | 2001-01-17 15:24:28 +0000 | [diff] [blame] | 647 | */ |
| 648 | static int |
| 649 | default_3way_compare(PyObject *v, PyObject *w) |
| 650 | { |
| 651 | int c; |
Guido van Rossum | 8f9143d | 2001-01-22 15:59:32 +0000 | [diff] [blame] | 652 | char *vname, *wname; |
Guido van Rossum | e797ec1 | 2001-01-17 15:24:28 +0000 | [diff] [blame] | 653 | |
| 654 | if (v->ob_type == w->ob_type) { |
Barry Warsaw | b0e754d | 2001-01-20 06:24:55 +0000 | [diff] [blame] | 655 | /* When comparing these pointers, they must be cast to |
| 656 | * integer types (i.e. Py_uintptr_t, our spelling of C9X's |
| 657 | * uintptr_t). ANSI specifies that pointer compares other |
| 658 | * than == and != to non-related structures are undefined. |
| 659 | */ |
Barry Warsaw | 71ff8d5 | 2001-01-20 06:08:10 +0000 | [diff] [blame] | 660 | Py_uintptr_t vv = (Py_uintptr_t)v; |
| 661 | Py_uintptr_t ww = (Py_uintptr_t)w; |
Guido van Rossum | e797ec1 | 2001-01-17 15:24:28 +0000 | [diff] [blame] | 662 | return (vv < ww) ? -1 : (vv > ww) ? 1 : 0; |
| 663 | } |
| 664 | |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 665 | #ifdef Py_USING_UNICODE |
Guido van Rossum | e797ec1 | 2001-01-17 15:24:28 +0000 | [diff] [blame] | 666 | /* Special case for Unicode */ |
| 667 | if (PyUnicode_Check(v) || PyUnicode_Check(w)) { |
| 668 | c = PyUnicode_Compare(v, w); |
| 669 | if (!PyErr_Occurred()) |
| 670 | return c; |
| 671 | /* TypeErrors are ignored: if Unicode coercion fails due |
| 672 | to one of the arguments not having the right type, we |
| 673 | continue as defined by the coercion protocol (see |
| 674 | above). Luckily, decoding errors are reported as |
| 675 | ValueErrors and are not masked by this technique. */ |
| 676 | if (!PyErr_ExceptionMatches(PyExc_TypeError)) |
| 677 | return -2; |
| 678 | PyErr_Clear(); |
| 679 | } |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 680 | #endif |
Guido van Rossum | e797ec1 | 2001-01-17 15:24:28 +0000 | [diff] [blame] | 681 | |
Guido van Rossum | 0871e93 | 2001-01-22 19:28:09 +0000 | [diff] [blame] | 682 | /* None is smaller than anything */ |
| 683 | if (v == Py_None) |
| 684 | return -1; |
| 685 | if (w == Py_None) |
| 686 | return 1; |
| 687 | |
Guido van Rossum | fb50d3f | 2003-02-18 16:40:09 +0000 | [diff] [blame] | 688 | /* different type: compare type names; numbers are smaller */ |
| 689 | if (PyNumber_Check(v)) |
Guido van Rossum | 8f9143d | 2001-01-22 15:59:32 +0000 | [diff] [blame] | 690 | vname = ""; |
| 691 | else |
| 692 | vname = v->ob_type->tp_name; |
Guido van Rossum | fb50d3f | 2003-02-18 16:40:09 +0000 | [diff] [blame] | 693 | if (PyNumber_Check(w)) |
Guido van Rossum | 8f9143d | 2001-01-22 15:59:32 +0000 | [diff] [blame] | 694 | wname = ""; |
| 695 | else |
| 696 | wname = w->ob_type->tp_name; |
| 697 | c = strcmp(vname, wname); |
| 698 | if (c < 0) |
| 699 | return -1; |
| 700 | if (c > 0) |
| 701 | return 1; |
| 702 | /* Same type name, or (more likely) incomparable numeric types */ |
| 703 | return ((Py_uintptr_t)(v->ob_type) < ( |
| 704 | Py_uintptr_t)(w->ob_type)) ? -1 : 1; |
Guido van Rossum | e797ec1 | 2001-01-17 15:24:28 +0000 | [diff] [blame] | 705 | } |
| 706 | |
Tim Peters | 6d60b2e | 2001-05-07 20:53:51 +0000 | [diff] [blame] | 707 | /* Do a 3-way comparison, by hook or by crook. Return: |
Guido van Rossum | a407300 | 2002-05-31 20:03:54 +0000 | [diff] [blame] | 708 | -2 for an exception (but see below); |
Tim Peters | c99213f | 2001-11-04 05:57:16 +0000 | [diff] [blame] | 709 | -1 if v < w; |
Tim Peters | 6d60b2e | 2001-05-07 20:53:51 +0000 | [diff] [blame] | 710 | 0 if v == w; |
Tim Peters | c99213f | 2001-11-04 05:57:16 +0000 | [diff] [blame] | 711 | 1 if v > w; |
Guido van Rossum | a407300 | 2002-05-31 20:03:54 +0000 | [diff] [blame] | 712 | BUT: if the object implements a tp_compare function, it returns |
Martin v. Löwis | 0163d6d | 2001-06-09 07:34:05 +0000 | [diff] [blame] | 713 | whatever this function returns (whether with an exception or not). |
Tim Peters | 6d60b2e | 2001-05-07 20:53:51 +0000 | [diff] [blame] | 714 | */ |
Neil Schemenauer | 5ed85ec | 2001-01-04 01:48:10 +0000 | [diff] [blame] | 715 | static int |
Fred Drake | 100814d | 2000-07-09 15:48:49 +0000 | [diff] [blame] | 716 | do_cmp(PyObject *v, PyObject *w) |
Guido van Rossum | 2056684 | 1995-01-12 11:26:10 +0000 | [diff] [blame] | 717 | { |
Neil Schemenauer | 5ed85ec | 2001-01-04 01:48:10 +0000 | [diff] [blame] | 718 | int c; |
Martin v. Löwis | 0163d6d | 2001-06-09 07:34:05 +0000 | [diff] [blame] | 719 | cmpfunc f; |
Neil Schemenauer | 5ed85ec | 2001-01-04 01:48:10 +0000 | [diff] [blame] | 720 | |
Martin v. Löwis | 0163d6d | 2001-06-09 07:34:05 +0000 | [diff] [blame] | 721 | if (v->ob_type == w->ob_type |
Guido van Rossum | 82fc51c | 2001-08-16 08:02:45 +0000 | [diff] [blame] | 722 | && (f = v->ob_type->tp_compare) != NULL) { |
| 723 | c = (*f)(v, w); |
Guido van Rossum | a407300 | 2002-05-31 20:03:54 +0000 | [diff] [blame] | 724 | if (PyInstance_Check(v)) { |
| 725 | /* Instance tp_compare has a different signature. |
| 726 | But if it returns undefined we fall through. */ |
| 727 | if (c != 2) |
| 728 | return c; |
Neal Norwitz | 3f8dae7 | 2002-05-31 20:23:33 +0000 | [diff] [blame] | 729 | /* Else fall through to try_rich_to_3way_compare() */ |
Guido van Rossum | a407300 | 2002-05-31 20:03:54 +0000 | [diff] [blame] | 730 | } |
| 731 | else |
| 732 | return adjust_tp_compare(c); |
Guido van Rossum | 82fc51c | 2001-08-16 08:02:45 +0000 | [diff] [blame] | 733 | } |
Guido van Rossum | a407300 | 2002-05-31 20:03:54 +0000 | [diff] [blame] | 734 | /* We only get here if one of the following is true: |
| 735 | a) v and w have different types |
| 736 | b) v and w have the same type, which doesn't have tp_compare |
| 737 | c) v and w are instances, and either __cmp__ is not defined or |
| 738 | __cmp__ returns NotImplemented |
| 739 | */ |
Guido van Rossum | e797ec1 | 2001-01-17 15:24:28 +0000 | [diff] [blame] | 740 | c = try_rich_to_3way_compare(v, w); |
| 741 | if (c < 2) |
Neil Schemenauer | 5ed85ec | 2001-01-04 01:48:10 +0000 | [diff] [blame] | 742 | return c; |
Guido van Rossum | e797ec1 | 2001-01-17 15:24:28 +0000 | [diff] [blame] | 743 | c = try_3way_compare(v, w); |
| 744 | if (c < 2) |
| 745 | return c; |
| 746 | return default_3way_compare(v, w); |
Guido van Rossum | 2056684 | 1995-01-12 11:26:10 +0000 | [diff] [blame] | 747 | } |
| 748 | |
Tim Peters | c99213f | 2001-11-04 05:57:16 +0000 | [diff] [blame] | 749 | /* Compare v to w. Return |
| 750 | -1 if v < w or exception (PyErr_Occurred() true in latter case). |
| 751 | 0 if v == w. |
| 752 | 1 if v > w. |
| 753 | XXX The docs (C API manual) say the return value is undefined in case |
| 754 | XXX of error. |
| 755 | */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 756 | int |
Fred Drake | 100814d | 2000-07-09 15:48:49 +0000 | [diff] [blame] | 757 | PyObject_Compare(PyObject *v, PyObject *w) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 758 | { |
Jeremy Hylton | 4a3dd2d | 2000-04-14 19:13:24 +0000 | [diff] [blame] | 759 | int result; |
| 760 | |
Guido van Rossum | c8b6df9 | 1997-05-23 00:06:51 +0000 | [diff] [blame] | 761 | if (v == NULL || w == NULL) { |
| 762 | PyErr_BadInternalCall(); |
| 763 | return -1; |
| 764 | } |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 765 | if (v == w) |
| 766 | return 0; |
Armin Rigo | 2b3eb40 | 2003-10-28 12:05:48 +0000 | [diff] [blame] | 767 | if (Py_EnterRecursiveCall(" in cmp")) |
| 768 | return -1; |
| 769 | result = do_cmp(v, w); |
| 770 | Py_LeaveRecursiveCall(); |
Guido van Rossum | e797ec1 | 2001-01-17 15:24:28 +0000 | [diff] [blame] | 771 | return result < 0 ? -1 : result; |
| 772 | } |
| 773 | |
Tim Peters | c99213f | 2001-11-04 05:57:16 +0000 | [diff] [blame] | 774 | /* Return (new reference to) Py_True or Py_False. */ |
Guido van Rossum | e797ec1 | 2001-01-17 15:24:28 +0000 | [diff] [blame] | 775 | static PyObject * |
Martin v. Löwis | 0163d6d | 2001-06-09 07:34:05 +0000 | [diff] [blame] | 776 | convert_3way_to_object(int op, int c) |
Guido van Rossum | e797ec1 | 2001-01-17 15:24:28 +0000 | [diff] [blame] | 777 | { |
Guido van Rossum | e797ec1 | 2001-01-17 15:24:28 +0000 | [diff] [blame] | 778 | PyObject *result; |
Guido van Rossum | e797ec1 | 2001-01-17 15:24:28 +0000 | [diff] [blame] | 779 | switch (op) { |
| 780 | case Py_LT: c = c < 0; break; |
| 781 | case Py_LE: c = c <= 0; break; |
| 782 | case Py_EQ: c = c == 0; break; |
| 783 | case Py_NE: c = c != 0; break; |
| 784 | case Py_GT: c = c > 0; break; |
| 785 | case Py_GE: c = c >= 0; break; |
| 786 | } |
| 787 | result = c ? Py_True : Py_False; |
| 788 | Py_INCREF(result); |
Jeremy Hylton | 4a3dd2d | 2000-04-14 19:13:24 +0000 | [diff] [blame] | 789 | return result; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 790 | } |
Tim Peters | 803526b | 2002-07-07 05:13:56 +0000 | [diff] [blame] | 791 | |
Tim Peters | c99213f | 2001-11-04 05:57:16 +0000 | [diff] [blame] | 792 | /* We want a rich comparison but don't have one. Try a 3-way cmp instead. |
| 793 | Return |
| 794 | NULL if error |
| 795 | Py_True if v op w |
| 796 | Py_False if not (v op w) |
| 797 | */ |
Martin v. Löwis | 0163d6d | 2001-06-09 07:34:05 +0000 | [diff] [blame] | 798 | static PyObject * |
| 799 | try_3way_to_rich_compare(PyObject *v, PyObject *w, int op) |
| 800 | { |
| 801 | int c; |
| 802 | |
| 803 | c = try_3way_compare(v, w); |
| 804 | if (c >= 2) |
| 805 | c = default_3way_compare(v, w); |
| 806 | if (c <= -2) |
| 807 | return NULL; |
| 808 | return convert_3way_to_object(op, c); |
| 809 | } |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 810 | |
Tim Peters | c99213f | 2001-11-04 05:57:16 +0000 | [diff] [blame] | 811 | /* Do rich comparison on v and w. Return |
| 812 | NULL if error |
| 813 | Else a new reference to an object other than Py_NotImplemented, usually(?): |
| 814 | Py_True if v op w |
| 815 | Py_False if not (v op w) |
| 816 | */ |
Neil Schemenauer | d38855c | 2001-01-21 16:25:18 +0000 | [diff] [blame] | 817 | static PyObject * |
Guido van Rossum | e797ec1 | 2001-01-17 15:24:28 +0000 | [diff] [blame] | 818 | do_richcmp(PyObject *v, PyObject *w, int op) |
| 819 | { |
| 820 | PyObject *res; |
| 821 | |
| 822 | res = try_rich_compare(v, w, op); |
| 823 | if (res != Py_NotImplemented) |
| 824 | return res; |
| 825 | Py_DECREF(res); |
| 826 | |
| 827 | return try_3way_to_rich_compare(v, w, op); |
| 828 | } |
| 829 | |
Tim Peters | c99213f | 2001-11-04 05:57:16 +0000 | [diff] [blame] | 830 | /* Return: |
| 831 | NULL for exception; |
Tim Peters | c99213f | 2001-11-04 05:57:16 +0000 | [diff] [blame] | 832 | some object not equal to NotImplemented if it is implemented |
| 833 | (this latter object may not be a Boolean). |
| 834 | */ |
Guido van Rossum | e797ec1 | 2001-01-17 15:24:28 +0000 | [diff] [blame] | 835 | PyObject * |
| 836 | PyObject_RichCompare(PyObject *v, PyObject *w, int op) |
| 837 | { |
| 838 | PyObject *res; |
| 839 | |
| 840 | assert(Py_LT <= op && op <= Py_GE); |
Armin Rigo | 2b3eb40 | 2003-10-28 12:05:48 +0000 | [diff] [blame] | 841 | if (Py_EnterRecursiveCall(" in cmp")) |
| 842 | return NULL; |
Guido van Rossum | e797ec1 | 2001-01-17 15:24:28 +0000 | [diff] [blame] | 843 | |
Armin Rigo | 2b3eb40 | 2003-10-28 12:05:48 +0000 | [diff] [blame] | 844 | /* If the types are equal, and not old-style instances, try to |
Tim Peters | 67754e9 | 2001-11-04 07:29:31 +0000 | [diff] [blame] | 845 | get out cheap (don't bother with coercions etc.). */ |
| 846 | if (v->ob_type == w->ob_type && !PyInstance_Check(v)) { |
| 847 | cmpfunc fcmp; |
| 848 | richcmpfunc frich = RICHCOMPARE(v->ob_type); |
| 849 | /* If the type has richcmp, try it first. try_rich_compare |
| 850 | tries it two-sided, which is not needed since we've a |
| 851 | single type only. */ |
| 852 | if (frich != NULL) { |
| 853 | res = (*frich)(v, w, op); |
| 854 | if (res != Py_NotImplemented) |
| 855 | goto Done; |
| 856 | Py_DECREF(res); |
| 857 | } |
| 858 | /* No richcmp, or this particular richmp not implemented. |
| 859 | Try 3-way cmp. */ |
| 860 | fcmp = v->ob_type->tp_compare; |
| 861 | if (fcmp != NULL) { |
| 862 | int c = (*fcmp)(v, w); |
Guido van Rossum | a407300 | 2002-05-31 20:03:54 +0000 | [diff] [blame] | 863 | c = adjust_tp_compare(c); |
| 864 | if (c == -2) { |
Tim Peters | 67754e9 | 2001-11-04 07:29:31 +0000 | [diff] [blame] | 865 | res = NULL; |
| 866 | goto Done; |
| 867 | } |
| 868 | res = convert_3way_to_object(op, c); |
| 869 | goto Done; |
| 870 | } |
Guido van Rossum | 2ffbf6b | 2001-01-17 21:27:02 +0000 | [diff] [blame] | 871 | } |
Tim Peters | 67754e9 | 2001-11-04 07:29:31 +0000 | [diff] [blame] | 872 | |
| 873 | /* Fast path not taken, or couldn't deliver a useful result. */ |
| 874 | res = do_richcmp(v, w, op); |
| 875 | Done: |
Armin Rigo | 2b3eb40 | 2003-10-28 12:05:48 +0000 | [diff] [blame] | 876 | Py_LeaveRecursiveCall(); |
Guido van Rossum | e797ec1 | 2001-01-17 15:24:28 +0000 | [diff] [blame] | 877 | return res; |
| 878 | } |
| 879 | |
Tim Peters | de9725f | 2001-05-05 10:06:17 +0000 | [diff] [blame] | 880 | /* Return -1 if error; 1 if v op w; 0 if not (v op w). */ |
Guido van Rossum | e797ec1 | 2001-01-17 15:24:28 +0000 | [diff] [blame] | 881 | int |
| 882 | PyObject_RichCompareBool(PyObject *v, PyObject *w, int op) |
| 883 | { |
Raymond Hettinger | 93d4481 | 2004-03-21 17:01:44 +0000 | [diff] [blame] | 884 | PyObject *res; |
Guido van Rossum | e797ec1 | 2001-01-17 15:24:28 +0000 | [diff] [blame] | 885 | int ok; |
| 886 | |
Raymond Hettinger | 93d4481 | 2004-03-21 17:01:44 +0000 | [diff] [blame] | 887 | /* Quick result when objects are the same. |
| 888 | Guarantees that identity implies equality. */ |
| 889 | if (v == w) { |
| 890 | if (op == Py_EQ) |
| 891 | return 1; |
| 892 | else if (op == Py_NE) |
| 893 | return 0; |
| 894 | } |
| 895 | |
| 896 | res = PyObject_RichCompare(v, w, op); |
Guido van Rossum | e797ec1 | 2001-01-17 15:24:28 +0000 | [diff] [blame] | 897 | if (res == NULL) |
| 898 | return -1; |
Guido van Rossum | 81912d4 | 2002-08-24 05:33:28 +0000 | [diff] [blame] | 899 | if (PyBool_Check(res)) |
| 900 | ok = (res == Py_True); |
| 901 | else |
| 902 | ok = PyObject_IsTrue(res); |
Guido van Rossum | e797ec1 | 2001-01-17 15:24:28 +0000 | [diff] [blame] | 903 | Py_DECREF(res); |
| 904 | return ok; |
| 905 | } |
Fred Drake | 13634cf | 2000-06-29 19:17:04 +0000 | [diff] [blame] | 906 | |
| 907 | /* Set of hash utility functions to help maintaining the invariant that |
Raymond Hettinger | 8183fa4 | 2004-03-21 17:35:06 +0000 | [diff] [blame] | 908 | if a==b then hash(a)==hash(b) |
Fred Drake | 13634cf | 2000-06-29 19:17:04 +0000 | [diff] [blame] | 909 | |
| 910 | All the utility functions (_Py_Hash*()) return "-1" to signify an error. |
| 911 | */ |
| 912 | |
| 913 | long |
Fred Drake | 100814d | 2000-07-09 15:48:49 +0000 | [diff] [blame] | 914 | _Py_HashDouble(double v) |
Fred Drake | 13634cf | 2000-06-29 19:17:04 +0000 | [diff] [blame] | 915 | { |
Tim Peters | 39dce29 | 2000-08-15 03:34:48 +0000 | [diff] [blame] | 916 | double intpart, fractpart; |
| 917 | int expo; |
| 918 | long hipart; |
| 919 | long x; /* the final hash value */ |
| 920 | /* This is designed so that Python numbers of different types |
| 921 | * that compare equal hash to the same value; otherwise comparisons |
| 922 | * of mapping keys will turn out weird. |
| 923 | */ |
| 924 | |
Tim Peters | 39dce29 | 2000-08-15 03:34:48 +0000 | [diff] [blame] | 925 | fractpart = modf(v, &intpart); |
Tim Peters | 39dce29 | 2000-08-15 03:34:48 +0000 | [diff] [blame] | 926 | if (fractpart == 0.0) { |
| 927 | /* This must return the same hash as an equal int or long. */ |
| 928 | if (intpart > LONG_MAX || -intpart > LONG_MAX) { |
| 929 | /* Convert to long and use its hash. */ |
| 930 | PyObject *plong; /* converted to Python long */ |
| 931 | if (Py_IS_INFINITY(intpart)) |
| 932 | /* can't convert to long int -- arbitrary */ |
| 933 | v = v < 0 ? -271828.0 : 314159.0; |
| 934 | plong = PyLong_FromDouble(v); |
| 935 | if (plong == NULL) |
| 936 | return -1; |
| 937 | x = PyObject_Hash(plong); |
| 938 | Py_DECREF(plong); |
| 939 | return x; |
| 940 | } |
| 941 | /* Fits in a C long == a Python int, so is its own hash. */ |
| 942 | x = (long)intpart; |
| 943 | if (x == -1) |
| 944 | x = -2; |
| 945 | return x; |
| 946 | } |
| 947 | /* The fractional part is non-zero, so we don't have to worry about |
| 948 | * making this match the hash of some other type. |
| 949 | * Use frexp to get at the bits in the double. |
Fred Drake | 13634cf | 2000-06-29 19:17:04 +0000 | [diff] [blame] | 950 | * Since the VAX D double format has 56 mantissa bits, which is the |
| 951 | * most of any double format in use, each of these parts may have as |
| 952 | * many as (but no more than) 56 significant bits. |
Tim Peters | 39dce29 | 2000-08-15 03:34:48 +0000 | [diff] [blame] | 953 | * So, assuming sizeof(long) >= 4, each part can be broken into two |
| 954 | * longs; frexp and multiplication are used to do that. |
| 955 | * Also, since the Cray double format has 15 exponent bits, which is |
| 956 | * the most of any double format in use, shifting the exponent field |
| 957 | * left by 15 won't overflow a long (again assuming sizeof(long) >= 4). |
Fred Drake | 13634cf | 2000-06-29 19:17:04 +0000 | [diff] [blame] | 958 | */ |
Tim Peters | 39dce29 | 2000-08-15 03:34:48 +0000 | [diff] [blame] | 959 | v = frexp(v, &expo); |
| 960 | v *= 2147483648.0; /* 2**31 */ |
| 961 | hipart = (long)v; /* take the top 32 bits */ |
| 962 | v = (v - (double)hipart) * 2147483648.0; /* get the next 32 bits */ |
| 963 | x = hipart + (long)v + (expo << 15); |
| 964 | if (x == -1) |
| 965 | x = -2; |
| 966 | return x; |
Fred Drake | 13634cf | 2000-06-29 19:17:04 +0000 | [diff] [blame] | 967 | } |
| 968 | |
| 969 | long |
Fred Drake | 100814d | 2000-07-09 15:48:49 +0000 | [diff] [blame] | 970 | _Py_HashPointer(void *p) |
Fred Drake | 13634cf | 2000-06-29 19:17:04 +0000 | [diff] [blame] | 971 | { |
| 972 | #if SIZEOF_LONG >= SIZEOF_VOID_P |
| 973 | return (long)p; |
| 974 | #else |
| 975 | /* convert to a Python long and hash that */ |
| 976 | PyObject* longobj; |
| 977 | long x; |
Tim Peters | 803526b | 2002-07-07 05:13:56 +0000 | [diff] [blame] | 978 | |
Fred Drake | 13634cf | 2000-06-29 19:17:04 +0000 | [diff] [blame] | 979 | if ((longobj = PyLong_FromVoidPtr(p)) == NULL) { |
| 980 | x = -1; |
| 981 | goto finally; |
| 982 | } |
| 983 | x = PyObject_Hash(longobj); |
Tim Peters | 803526b | 2002-07-07 05:13:56 +0000 | [diff] [blame] | 984 | |
Fred Drake | 13634cf | 2000-06-29 19:17:04 +0000 | [diff] [blame] | 985 | finally: |
| 986 | Py_XDECREF(longobj); |
| 987 | return x; |
| 988 | #endif |
| 989 | } |
| 990 | |
| 991 | |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 992 | long |
Fred Drake | 100814d | 2000-07-09 15:48:49 +0000 | [diff] [blame] | 993 | PyObject_Hash(PyObject *v) |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 994 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 995 | PyTypeObject *tp = v->ob_type; |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 996 | if (tp->tp_hash != NULL) |
| 997 | return (*tp->tp_hash)(v); |
Guido van Rossum | d1f06b9 | 2001-01-24 22:14:43 +0000 | [diff] [blame] | 998 | if (tp->tp_compare == NULL && RICHCOMPARE(tp) == NULL) { |
Fred Drake | 13634cf | 2000-06-29 19:17:04 +0000 | [diff] [blame] | 999 | return _Py_HashPointer(v); /* Use address as hash value */ |
| 1000 | } |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 1001 | /* If there's a cmp but no hash defined, the object can't be hashed */ |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1002 | PyErr_SetString(PyExc_TypeError, "unhashable type"); |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 1003 | return -1; |
| 1004 | } |
| 1005 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1006 | PyObject * |
Fred Drake | 100814d | 2000-07-09 15:48:49 +0000 | [diff] [blame] | 1007 | PyObject_GetAttrString(PyObject *v, char *name) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1008 | { |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1009 | PyObject *w, *res; |
Guido van Rossum | d8eb1b3 | 1996-08-09 20:52:03 +0000 | [diff] [blame] | 1010 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1011 | if (v->ob_type->tp_getattr != NULL) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1012 | return (*v->ob_type->tp_getattr)(v, name); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1013 | w = PyString_InternFromString(name); |
| 1014 | if (w == NULL) |
| 1015 | return NULL; |
| 1016 | res = PyObject_GetAttr(v, w); |
| 1017 | Py_XDECREF(w); |
| 1018 | return res; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1019 | } |
| 1020 | |
| 1021 | int |
Fred Drake | 100814d | 2000-07-09 15:48:49 +0000 | [diff] [blame] | 1022 | PyObject_HasAttrString(PyObject *v, char *name) |
Guido van Rossum | ed18fdc | 1993-07-11 19:55:34 +0000 | [diff] [blame] | 1023 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1024 | PyObject *res = PyObject_GetAttrString(v, name); |
Guido van Rossum | ed18fdc | 1993-07-11 19:55:34 +0000 | [diff] [blame] | 1025 | if (res != NULL) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1026 | Py_DECREF(res); |
Guido van Rossum | ed18fdc | 1993-07-11 19:55:34 +0000 | [diff] [blame] | 1027 | return 1; |
| 1028 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1029 | PyErr_Clear(); |
Guido van Rossum | ed18fdc | 1993-07-11 19:55:34 +0000 | [diff] [blame] | 1030 | return 0; |
| 1031 | } |
| 1032 | |
| 1033 | int |
Fred Drake | 100814d | 2000-07-09 15:48:49 +0000 | [diff] [blame] | 1034 | PyObject_SetAttrString(PyObject *v, char *name, PyObject *w) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1035 | { |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1036 | PyObject *s; |
| 1037 | int res; |
Guido van Rossum | d8eb1b3 | 1996-08-09 20:52:03 +0000 | [diff] [blame] | 1038 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1039 | if (v->ob_type->tp_setattr != NULL) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1040 | return (*v->ob_type->tp_setattr)(v, name, w); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1041 | s = PyString_InternFromString(name); |
| 1042 | if (s == NULL) |
| 1043 | return -1; |
| 1044 | res = PyObject_SetAttr(v, s, w); |
| 1045 | Py_XDECREF(s); |
| 1046 | return res; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1047 | } |
| 1048 | |
Guido van Rossum | 98ff96a | 1997-05-20 18:34:44 +0000 | [diff] [blame] | 1049 | PyObject * |
Fred Drake | 100814d | 2000-07-09 15:48:49 +0000 | [diff] [blame] | 1050 | PyObject_GetAttr(PyObject *v, PyObject *name) |
Guido van Rossum | 98ff96a | 1997-05-20 18:34:44 +0000 | [diff] [blame] | 1051 | { |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1052 | PyTypeObject *tp = v->ob_type; |
| 1053 | |
Jeremy Hylton | 99a8f90 | 2000-06-23 14:36:32 +0000 | [diff] [blame] | 1054 | if (!PyString_Check(name)) { |
Martin v. Löwis | 0c160a0 | 2002-03-15 13:40:30 +0000 | [diff] [blame] | 1055 | #ifdef Py_USING_UNICODE |
| 1056 | /* The Unicode to string conversion is done here because the |
| 1057 | existing tp_getattro slots expect a string object as name |
| 1058 | and we wouldn't want to break those. */ |
| 1059 | if (PyUnicode_Check(name)) { |
| 1060 | name = _PyUnicode_AsDefaultEncodedString(name, NULL); |
| 1061 | if (name == NULL) |
| 1062 | return NULL; |
| 1063 | } |
| 1064 | else |
| 1065 | #endif |
| 1066 | { |
| 1067 | PyErr_SetString(PyExc_TypeError, |
| 1068 | "attribute name must be string"); |
| 1069 | return NULL; |
| 1070 | } |
Jeremy Hylton | 99a8f90 | 2000-06-23 14:36:32 +0000 | [diff] [blame] | 1071 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1072 | if (tp->tp_getattro != NULL) |
| 1073 | return (*tp->tp_getattro)(v, name); |
| 1074 | if (tp->tp_getattr != NULL) |
| 1075 | return (*tp->tp_getattr)(v, PyString_AS_STRING(name)); |
| 1076 | PyErr_Format(PyExc_AttributeError, |
| 1077 | "'%.50s' object has no attribute '%.400s'", |
| 1078 | tp->tp_name, PyString_AS_STRING(name)); |
| 1079 | return NULL; |
Guido van Rossum | 98ff96a | 1997-05-20 18:34:44 +0000 | [diff] [blame] | 1080 | } |
| 1081 | |
| 1082 | int |
Fred Drake | 100814d | 2000-07-09 15:48:49 +0000 | [diff] [blame] | 1083 | PyObject_HasAttr(PyObject *v, PyObject *name) |
Guido van Rossum | 98ff96a | 1997-05-20 18:34:44 +0000 | [diff] [blame] | 1084 | { |
| 1085 | PyObject *res = PyObject_GetAttr(v, name); |
| 1086 | if (res != NULL) { |
| 1087 | Py_DECREF(res); |
| 1088 | return 1; |
| 1089 | } |
| 1090 | PyErr_Clear(); |
| 1091 | return 0; |
| 1092 | } |
| 1093 | |
| 1094 | int |
Fred Drake | 100814d | 2000-07-09 15:48:49 +0000 | [diff] [blame] | 1095 | PyObject_SetAttr(PyObject *v, PyObject *name, PyObject *value) |
Guido van Rossum | 98ff96a | 1997-05-20 18:34:44 +0000 | [diff] [blame] | 1096 | { |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1097 | PyTypeObject *tp = v->ob_type; |
Guido van Rossum | 98ff96a | 1997-05-20 18:34:44 +0000 | [diff] [blame] | 1098 | int err; |
Marc-André Lemburg | e44e507 | 2000-09-18 16:20:57 +0000 | [diff] [blame] | 1099 | |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 1100 | if (!PyString_Check(name)){ |
Martin v. Löwis | 0c160a0 | 2002-03-15 13:40:30 +0000 | [diff] [blame] | 1101 | #ifdef Py_USING_UNICODE |
| 1102 | /* The Unicode to string conversion is done here because the |
| 1103 | existing tp_setattro slots expect a string object as name |
| 1104 | and we wouldn't want to break those. */ |
| 1105 | if (PyUnicode_Check(name)) { |
| 1106 | name = PyUnicode_AsEncodedString(name, NULL, NULL); |
| 1107 | if (name == NULL) |
| 1108 | return -1; |
| 1109 | } |
Tim Peters | 803526b | 2002-07-07 05:13:56 +0000 | [diff] [blame] | 1110 | else |
Martin v. Löwis | 0c160a0 | 2002-03-15 13:40:30 +0000 | [diff] [blame] | 1111 | #endif |
| 1112 | { |
| 1113 | PyErr_SetString(PyExc_TypeError, |
| 1114 | "attribute name must be string"); |
| 1115 | return -1; |
| 1116 | } |
Jeremy Hylton | 99a8f90 | 2000-06-23 14:36:32 +0000 | [diff] [blame] | 1117 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1118 | else |
| 1119 | Py_INCREF(name); |
| 1120 | |
| 1121 | PyString_InternInPlace(&name); |
| 1122 | if (tp->tp_setattro != NULL) { |
| 1123 | err = (*tp->tp_setattro)(v, name, value); |
| 1124 | Py_DECREF(name); |
| 1125 | return err; |
Marc-André Lemburg | e44e507 | 2000-09-18 16:20:57 +0000 | [diff] [blame] | 1126 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1127 | if (tp->tp_setattr != NULL) { |
| 1128 | err = (*tp->tp_setattr)(v, PyString_AS_STRING(name), value); |
| 1129 | Py_DECREF(name); |
| 1130 | return err; |
| 1131 | } |
Guido van Rossum | 98ff96a | 1997-05-20 18:34:44 +0000 | [diff] [blame] | 1132 | Py_DECREF(name); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1133 | if (tp->tp_getattr == NULL && tp->tp_getattro == NULL) |
| 1134 | PyErr_Format(PyExc_TypeError, |
| 1135 | "'%.100s' object has no attributes " |
| 1136 | "(%s .%.100s)", |
| 1137 | tp->tp_name, |
| 1138 | value==NULL ? "del" : "assign to", |
| 1139 | PyString_AS_STRING(name)); |
| 1140 | else |
| 1141 | PyErr_Format(PyExc_TypeError, |
| 1142 | "'%.100s' object has only read-only attributes " |
| 1143 | "(%s .%.100s)", |
| 1144 | tp->tp_name, |
| 1145 | value==NULL ? "del" : "assign to", |
| 1146 | PyString_AS_STRING(name)); |
| 1147 | return -1; |
| 1148 | } |
| 1149 | |
| 1150 | /* Helper to get a pointer to an object's __dict__ slot, if any */ |
| 1151 | |
| 1152 | PyObject ** |
| 1153 | _PyObject_GetDictPtr(PyObject *obj) |
| 1154 | { |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1155 | long dictoffset; |
| 1156 | PyTypeObject *tp = obj->ob_type; |
| 1157 | |
| 1158 | if (!(tp->tp_flags & Py_TPFLAGS_HAVE_CLASS)) |
| 1159 | return NULL; |
| 1160 | dictoffset = tp->tp_dictoffset; |
| 1161 | if (dictoffset == 0) |
| 1162 | return NULL; |
| 1163 | if (dictoffset < 0) { |
Guido van Rossum | 2eb0b87 | 2002-03-01 22:24:49 +0000 | [diff] [blame] | 1164 | int tsize; |
| 1165 | size_t size; |
| 1166 | |
| 1167 | tsize = ((PyVarObject *)obj)->ob_size; |
| 1168 | if (tsize < 0) |
| 1169 | tsize = -tsize; |
| 1170 | size = _PyObject_VAR_SIZE(tp, tsize); |
| 1171 | |
Tim Peters | 6d483d3 | 2001-10-06 21:27:34 +0000 | [diff] [blame] | 1172 | dictoffset += (long)size; |
| 1173 | assert(dictoffset > 0); |
| 1174 | assert(dictoffset % SIZEOF_VOID_P == 0); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1175 | } |
| 1176 | return (PyObject **) ((char *)obj + dictoffset); |
| 1177 | } |
| 1178 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1179 | PyObject * |
Raymond Hettinger | 1da1dbf | 2003-03-17 19:46:11 +0000 | [diff] [blame] | 1180 | PyObject_SelfIter(PyObject *obj) |
Raymond Hettinger | 0153826 | 2003-03-17 08:24:35 +0000 | [diff] [blame] | 1181 | { |
| 1182 | Py_INCREF(obj); |
| 1183 | return obj; |
| 1184 | } |
| 1185 | |
Michael W. Hudson | 1593f50 | 2004-09-14 17:09:47 +0000 | [diff] [blame] | 1186 | /* Generic GetAttr functions - put these in your tp_[gs]etattro slot */ |
| 1187 | |
Raymond Hettinger | 0153826 | 2003-03-17 08:24:35 +0000 | [diff] [blame] | 1188 | PyObject * |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1189 | PyObject_GenericGetAttr(PyObject *obj, PyObject *name) |
| 1190 | { |
| 1191 | PyTypeObject *tp = obj->ob_type; |
Guido van Rossum | 056fbf4 | 2002-08-19 19:22:50 +0000 | [diff] [blame] | 1192 | PyObject *descr = NULL; |
Guido van Rossum | ebca9fc | 2001-12-04 15:54:53 +0000 | [diff] [blame] | 1193 | PyObject *res = NULL; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1194 | descrgetfunc f; |
Guido van Rossum | c66ff44 | 2002-08-19 16:50:48 +0000 | [diff] [blame] | 1195 | long dictoffset; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1196 | PyObject **dictptr; |
| 1197 | |
Guido van Rossum | ebca9fc | 2001-12-04 15:54:53 +0000 | [diff] [blame] | 1198 | if (!PyString_Check(name)){ |
Martin v. Löwis | 0c160a0 | 2002-03-15 13:40:30 +0000 | [diff] [blame] | 1199 | #ifdef Py_USING_UNICODE |
| 1200 | /* The Unicode to string conversion is done here because the |
| 1201 | existing tp_setattro slots expect a string object as name |
| 1202 | and we wouldn't want to break those. */ |
| 1203 | if (PyUnicode_Check(name)) { |
| 1204 | name = PyUnicode_AsEncodedString(name, NULL, NULL); |
| 1205 | if (name == NULL) |
| 1206 | return NULL; |
| 1207 | } |
Tim Peters | 803526b | 2002-07-07 05:13:56 +0000 | [diff] [blame] | 1208 | else |
Martin v. Löwis | 0c160a0 | 2002-03-15 13:40:30 +0000 | [diff] [blame] | 1209 | #endif |
| 1210 | { |
| 1211 | PyErr_SetString(PyExc_TypeError, |
| 1212 | "attribute name must be string"); |
| 1213 | return NULL; |
| 1214 | } |
Guido van Rossum | ebca9fc | 2001-12-04 15:54:53 +0000 | [diff] [blame] | 1215 | } |
| 1216 | else |
| 1217 | Py_INCREF(name); |
| 1218 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1219 | if (tp->tp_dict == NULL) { |
Guido van Rossum | 528b7eb | 2001-08-07 17:24:28 +0000 | [diff] [blame] | 1220 | if (PyType_Ready(tp) < 0) |
Guido van Rossum | ebca9fc | 2001-12-04 15:54:53 +0000 | [diff] [blame] | 1221 | goto done; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1222 | } |
| 1223 | |
Guido van Rossum | 056fbf4 | 2002-08-19 19:22:50 +0000 | [diff] [blame] | 1224 | /* Inline _PyType_Lookup */ |
| 1225 | { |
| 1226 | int i, n; |
| 1227 | PyObject *mro, *base, *dict; |
| 1228 | |
| 1229 | /* Look in tp_dict of types in MRO */ |
| 1230 | mro = tp->tp_mro; |
| 1231 | assert(mro != NULL); |
| 1232 | assert(PyTuple_Check(mro)); |
| 1233 | n = PyTuple_GET_SIZE(mro); |
| 1234 | for (i = 0; i < n; i++) { |
| 1235 | base = PyTuple_GET_ITEM(mro, i); |
| 1236 | if (PyClass_Check(base)) |
| 1237 | dict = ((PyClassObject *)base)->cl_dict; |
| 1238 | else { |
| 1239 | assert(PyType_Check(base)); |
| 1240 | dict = ((PyTypeObject *)base)->tp_dict; |
| 1241 | } |
| 1242 | assert(dict && PyDict_Check(dict)); |
| 1243 | descr = PyDict_GetItem(dict, name); |
| 1244 | if (descr != NULL) |
| 1245 | break; |
| 1246 | } |
| 1247 | } |
| 1248 | |
Michael W. Hudson | b2c7de4 | 2003-08-15 13:07:47 +0000 | [diff] [blame] | 1249 | Py_XINCREF(descr); |
| 1250 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1251 | f = NULL; |
Guido van Rossum | 90195e2 | 2003-02-19 03:19:29 +0000 | [diff] [blame] | 1252 | if (descr != NULL && |
| 1253 | PyType_HasFeature(descr->ob_type, Py_TPFLAGS_HAVE_CLASS)) { |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1254 | f = descr->ob_type->tp_descr_get; |
Guido van Rossum | ebca9fc | 2001-12-04 15:54:53 +0000 | [diff] [blame] | 1255 | if (f != NULL && PyDescr_IsData(descr)) { |
| 1256 | res = f(descr, obj, (PyObject *)obj->ob_type); |
Michael W. Hudson | b2c7de4 | 2003-08-15 13:07:47 +0000 | [diff] [blame] | 1257 | Py_DECREF(descr); |
Guido van Rossum | ebca9fc | 2001-12-04 15:54:53 +0000 | [diff] [blame] | 1258 | goto done; |
| 1259 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1260 | } |
| 1261 | |
Guido van Rossum | c66ff44 | 2002-08-19 16:50:48 +0000 | [diff] [blame] | 1262 | /* Inline _PyObject_GetDictPtr */ |
| 1263 | dictoffset = tp->tp_dictoffset; |
| 1264 | if (dictoffset != 0) { |
| 1265 | PyObject *dict; |
| 1266 | if (dictoffset < 0) { |
| 1267 | int tsize; |
| 1268 | size_t size; |
| 1269 | |
| 1270 | tsize = ((PyVarObject *)obj)->ob_size; |
| 1271 | if (tsize < 0) |
| 1272 | tsize = -tsize; |
| 1273 | size = _PyObject_VAR_SIZE(tp, tsize); |
| 1274 | |
| 1275 | dictoffset += (long)size; |
| 1276 | assert(dictoffset > 0); |
| 1277 | assert(dictoffset % SIZEOF_VOID_P == 0); |
| 1278 | } |
| 1279 | dictptr = (PyObject **) ((char *)obj + dictoffset); |
| 1280 | dict = *dictptr; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1281 | if (dict != NULL) { |
Guido van Rossum | ebca9fc | 2001-12-04 15:54:53 +0000 | [diff] [blame] | 1282 | res = PyDict_GetItem(dict, name); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1283 | if (res != NULL) { |
| 1284 | Py_INCREF(res); |
Michael W. Hudson | b2c7de4 | 2003-08-15 13:07:47 +0000 | [diff] [blame] | 1285 | Py_XDECREF(descr); |
Guido van Rossum | ebca9fc | 2001-12-04 15:54:53 +0000 | [diff] [blame] | 1286 | goto done; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1287 | } |
| 1288 | } |
| 1289 | } |
| 1290 | |
Guido van Rossum | ebca9fc | 2001-12-04 15:54:53 +0000 | [diff] [blame] | 1291 | if (f != NULL) { |
| 1292 | res = f(descr, obj, (PyObject *)obj->ob_type); |
Michael W. Hudson | b2c7de4 | 2003-08-15 13:07:47 +0000 | [diff] [blame] | 1293 | Py_DECREF(descr); |
Guido van Rossum | ebca9fc | 2001-12-04 15:54:53 +0000 | [diff] [blame] | 1294 | goto done; |
| 1295 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1296 | |
| 1297 | if (descr != NULL) { |
Guido van Rossum | ebca9fc | 2001-12-04 15:54:53 +0000 | [diff] [blame] | 1298 | res = descr; |
Michael W. Hudson | b2c7de4 | 2003-08-15 13:07:47 +0000 | [diff] [blame] | 1299 | /* descr was already increfed above */ |
Guido van Rossum | ebca9fc | 2001-12-04 15:54:53 +0000 | [diff] [blame] | 1300 | goto done; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1301 | } |
| 1302 | |
| 1303 | PyErr_Format(PyExc_AttributeError, |
| 1304 | "'%.50s' object has no attribute '%.400s'", |
| 1305 | tp->tp_name, PyString_AS_STRING(name)); |
Guido van Rossum | ebca9fc | 2001-12-04 15:54:53 +0000 | [diff] [blame] | 1306 | done: |
| 1307 | Py_DECREF(name); |
| 1308 | return res; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1309 | } |
| 1310 | |
| 1311 | int |
| 1312 | PyObject_GenericSetAttr(PyObject *obj, PyObject *name, PyObject *value) |
| 1313 | { |
| 1314 | PyTypeObject *tp = obj->ob_type; |
| 1315 | PyObject *descr; |
| 1316 | descrsetfunc f; |
| 1317 | PyObject **dictptr; |
Guido van Rossum | ebca9fc | 2001-12-04 15:54:53 +0000 | [diff] [blame] | 1318 | int res = -1; |
| 1319 | |
Guido van Rossum | ebca9fc | 2001-12-04 15:54:53 +0000 | [diff] [blame] | 1320 | if (!PyString_Check(name)){ |
Martin v. Löwis | 0c160a0 | 2002-03-15 13:40:30 +0000 | [diff] [blame] | 1321 | #ifdef Py_USING_UNICODE |
| 1322 | /* The Unicode to string conversion is done here because the |
| 1323 | existing tp_setattro slots expect a string object as name |
| 1324 | and we wouldn't want to break those. */ |
| 1325 | if (PyUnicode_Check(name)) { |
| 1326 | name = PyUnicode_AsEncodedString(name, NULL, NULL); |
| 1327 | if (name == NULL) |
| 1328 | return -1; |
| 1329 | } |
Tim Peters | 803526b | 2002-07-07 05:13:56 +0000 | [diff] [blame] | 1330 | else |
Martin v. Löwis | 0c160a0 | 2002-03-15 13:40:30 +0000 | [diff] [blame] | 1331 | #endif |
| 1332 | { |
| 1333 | PyErr_SetString(PyExc_TypeError, |
| 1334 | "attribute name must be string"); |
| 1335 | return -1; |
| 1336 | } |
Guido van Rossum | ebca9fc | 2001-12-04 15:54:53 +0000 | [diff] [blame] | 1337 | } |
| 1338 | else |
| 1339 | Py_INCREF(name); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1340 | |
| 1341 | if (tp->tp_dict == NULL) { |
Guido van Rossum | 528b7eb | 2001-08-07 17:24:28 +0000 | [diff] [blame] | 1342 | if (PyType_Ready(tp) < 0) |
Guido van Rossum | ebca9fc | 2001-12-04 15:54:53 +0000 | [diff] [blame] | 1343 | goto done; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1344 | } |
| 1345 | |
| 1346 | descr = _PyType_Lookup(tp, name); |
| 1347 | f = NULL; |
Guido van Rossum | 90195e2 | 2003-02-19 03:19:29 +0000 | [diff] [blame] | 1348 | if (descr != NULL && |
| 1349 | PyType_HasFeature(descr->ob_type, Py_TPFLAGS_HAVE_CLASS)) { |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1350 | f = descr->ob_type->tp_descr_set; |
Guido van Rossum | ebca9fc | 2001-12-04 15:54:53 +0000 | [diff] [blame] | 1351 | if (f != NULL && PyDescr_IsData(descr)) { |
| 1352 | res = f(descr, obj, value); |
| 1353 | goto done; |
| 1354 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1355 | } |
| 1356 | |
| 1357 | dictptr = _PyObject_GetDictPtr(obj); |
| 1358 | if (dictptr != NULL) { |
| 1359 | PyObject *dict = *dictptr; |
| 1360 | if (dict == NULL && value != NULL) { |
| 1361 | dict = PyDict_New(); |
| 1362 | if (dict == NULL) |
Guido van Rossum | ebca9fc | 2001-12-04 15:54:53 +0000 | [diff] [blame] | 1363 | goto done; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1364 | *dictptr = dict; |
| 1365 | } |
| 1366 | if (dict != NULL) { |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1367 | if (value == NULL) |
| 1368 | res = PyDict_DelItem(dict, name); |
| 1369 | else |
| 1370 | res = PyDict_SetItem(dict, name, value); |
| 1371 | if (res < 0 && PyErr_ExceptionMatches(PyExc_KeyError)) |
| 1372 | PyErr_SetObject(PyExc_AttributeError, name); |
Guido van Rossum | ebca9fc | 2001-12-04 15:54:53 +0000 | [diff] [blame] | 1373 | goto done; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1374 | } |
| 1375 | } |
| 1376 | |
Guido van Rossum | ebca9fc | 2001-12-04 15:54:53 +0000 | [diff] [blame] | 1377 | if (f != NULL) { |
| 1378 | res = f(descr, obj, value); |
| 1379 | goto done; |
| 1380 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1381 | |
| 1382 | if (descr == NULL) { |
| 1383 | PyErr_Format(PyExc_AttributeError, |
| 1384 | "'%.50s' object has no attribute '%.400s'", |
| 1385 | tp->tp_name, PyString_AS_STRING(name)); |
Guido van Rossum | ebca9fc | 2001-12-04 15:54:53 +0000 | [diff] [blame] | 1386 | goto done; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1387 | } |
| 1388 | |
| 1389 | PyErr_Format(PyExc_AttributeError, |
| 1390 | "'%.50s' object attribute '%.400s' is read-only", |
| 1391 | tp->tp_name, PyString_AS_STRING(name)); |
Guido van Rossum | ebca9fc | 2001-12-04 15:54:53 +0000 | [diff] [blame] | 1392 | done: |
| 1393 | Py_DECREF(name); |
| 1394 | return res; |
Guido van Rossum | 98ff96a | 1997-05-20 18:34:44 +0000 | [diff] [blame] | 1395 | } |
| 1396 | |
Guido van Rossum | 6ac258d | 1993-05-12 08:24:20 +0000 | [diff] [blame] | 1397 | /* Test a value used as condition, e.g., in a for or if statement. |
| 1398 | Return -1 if an error occurred */ |
| 1399 | |
| 1400 | int |
Fred Drake | 100814d | 2000-07-09 15:48:49 +0000 | [diff] [blame] | 1401 | PyObject_IsTrue(PyObject *v) |
Guido van Rossum | 6ac258d | 1993-05-12 08:24:20 +0000 | [diff] [blame] | 1402 | { |
| 1403 | int res; |
Guido van Rossum | 6248f44 | 2002-08-24 06:31:34 +0000 | [diff] [blame] | 1404 | if (v == Py_True) |
| 1405 | return 1; |
| 1406 | if (v == Py_False) |
| 1407 | return 0; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1408 | if (v == Py_None) |
Neal Norwitz | 51290d3 | 2002-06-13 21:32:44 +0000 | [diff] [blame] | 1409 | return 0; |
Guido van Rossum | 1c4f458 | 1998-05-22 00:53:24 +0000 | [diff] [blame] | 1410 | else if (v->ob_type->tp_as_number != NULL && |
| 1411 | v->ob_type->tp_as_number->nb_nonzero != NULL) |
Guido van Rossum | 6ac258d | 1993-05-12 08:24:20 +0000 | [diff] [blame] | 1412 | res = (*v->ob_type->tp_as_number->nb_nonzero)(v); |
Guido van Rossum | 1c4f458 | 1998-05-22 00:53:24 +0000 | [diff] [blame] | 1413 | else if (v->ob_type->tp_as_mapping != NULL && |
| 1414 | v->ob_type->tp_as_mapping->mp_length != NULL) |
Guido van Rossum | 6ac258d | 1993-05-12 08:24:20 +0000 | [diff] [blame] | 1415 | res = (*v->ob_type->tp_as_mapping->mp_length)(v); |
Guido van Rossum | 1c4f458 | 1998-05-22 00:53:24 +0000 | [diff] [blame] | 1416 | else if (v->ob_type->tp_as_sequence != NULL && |
| 1417 | v->ob_type->tp_as_sequence->sq_length != NULL) |
Guido van Rossum | 6ac258d | 1993-05-12 08:24:20 +0000 | [diff] [blame] | 1418 | res = (*v->ob_type->tp_as_sequence->sq_length)(v); |
| 1419 | else |
Neal Norwitz | 51290d3 | 2002-06-13 21:32:44 +0000 | [diff] [blame] | 1420 | return 1; |
| 1421 | return (res > 0) ? 1 : res; |
Guido van Rossum | 6ac258d | 1993-05-12 08:24:20 +0000 | [diff] [blame] | 1422 | } |
| 1423 | |
Tim Peters | 803526b | 2002-07-07 05:13:56 +0000 | [diff] [blame] | 1424 | /* equivalent of 'not v' |
Guido van Rossum | c3d3f96 | 1998-04-09 17:53:59 +0000 | [diff] [blame] | 1425 | Return -1 if an error occurred */ |
| 1426 | |
| 1427 | int |
Fred Drake | 100814d | 2000-07-09 15:48:49 +0000 | [diff] [blame] | 1428 | PyObject_Not(PyObject *v) |
Guido van Rossum | c3d3f96 | 1998-04-09 17:53:59 +0000 | [diff] [blame] | 1429 | { |
| 1430 | int res; |
| 1431 | res = PyObject_IsTrue(v); |
| 1432 | if (res < 0) |
| 1433 | return res; |
| 1434 | return res == 0; |
| 1435 | } |
| 1436 | |
Guido van Rossum | 5524a59 | 1995-01-10 15:26:20 +0000 | [diff] [blame] | 1437 | /* Coerce two numeric types to the "larger" one. |
| 1438 | Increment the reference count on each argument. |
Guido van Rossum | e797ec1 | 2001-01-17 15:24:28 +0000 | [diff] [blame] | 1439 | Return value: |
| 1440 | -1 if an error occurred; |
| 1441 | 0 if the coercion succeeded (and then the reference counts are increased); |
| 1442 | 1 if no coercion is possible (and no error is raised). |
Guido van Rossum | 5524a59 | 1995-01-10 15:26:20 +0000 | [diff] [blame] | 1443 | */ |
Guido van Rossum | 5524a59 | 1995-01-10 15:26:20 +0000 | [diff] [blame] | 1444 | int |
Fred Drake | 100814d | 2000-07-09 15:48:49 +0000 | [diff] [blame] | 1445 | PyNumber_CoerceEx(PyObject **pv, PyObject **pw) |
Guido van Rossum | 5524a59 | 1995-01-10 15:26:20 +0000 | [diff] [blame] | 1446 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1447 | register PyObject *v = *pv; |
| 1448 | register PyObject *w = *pw; |
Guido van Rossum | 5524a59 | 1995-01-10 15:26:20 +0000 | [diff] [blame] | 1449 | int res; |
| 1450 | |
Guido van Rossum | 517c7d4 | 2002-04-26 02:49:14 +0000 | [diff] [blame] | 1451 | /* Shortcut only for old-style types */ |
| 1452 | if (v->ob_type == w->ob_type && |
| 1453 | !PyType_HasFeature(v->ob_type, Py_TPFLAGS_CHECKTYPES)) |
| 1454 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1455 | Py_INCREF(v); |
| 1456 | Py_INCREF(w); |
Guido van Rossum | 5524a59 | 1995-01-10 15:26:20 +0000 | [diff] [blame] | 1457 | return 0; |
| 1458 | } |
| 1459 | if (v->ob_type->tp_as_number && v->ob_type->tp_as_number->nb_coerce) { |
| 1460 | res = (*v->ob_type->tp_as_number->nb_coerce)(pv, pw); |
| 1461 | if (res <= 0) |
| 1462 | return res; |
| 1463 | } |
| 1464 | if (w->ob_type->tp_as_number && w->ob_type->tp_as_number->nb_coerce) { |
| 1465 | res = (*w->ob_type->tp_as_number->nb_coerce)(pw, pv); |
| 1466 | if (res <= 0) |
| 1467 | return res; |
| 1468 | } |
Guido van Rossum | 242c642 | 1997-11-19 16:03:17 +0000 | [diff] [blame] | 1469 | return 1; |
| 1470 | } |
| 1471 | |
Guido van Rossum | e797ec1 | 2001-01-17 15:24:28 +0000 | [diff] [blame] | 1472 | /* Coerce two numeric types to the "larger" one. |
| 1473 | Increment the reference count on each argument. |
| 1474 | Return -1 and raise an exception if no coercion is possible |
| 1475 | (and then no reference count is incremented). |
| 1476 | */ |
Guido van Rossum | 242c642 | 1997-11-19 16:03:17 +0000 | [diff] [blame] | 1477 | int |
Fred Drake | 100814d | 2000-07-09 15:48:49 +0000 | [diff] [blame] | 1478 | PyNumber_Coerce(PyObject **pv, PyObject **pw) |
Guido van Rossum | 242c642 | 1997-11-19 16:03:17 +0000 | [diff] [blame] | 1479 | { |
| 1480 | int err = PyNumber_CoerceEx(pv, pw); |
| 1481 | if (err <= 0) |
| 1482 | return err; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1483 | PyErr_SetString(PyExc_TypeError, "number coercion failed"); |
Guido van Rossum | 5524a59 | 1995-01-10 15:26:20 +0000 | [diff] [blame] | 1484 | return -1; |
| 1485 | } |
| 1486 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1487 | |
Guido van Rossum | 49b11fe | 1995-01-26 00:38:22 +0000 | [diff] [blame] | 1488 | /* Test whether an object can be called */ |
| 1489 | |
| 1490 | int |
Fred Drake | 100814d | 2000-07-09 15:48:49 +0000 | [diff] [blame] | 1491 | PyCallable_Check(PyObject *x) |
Guido van Rossum | 49b11fe | 1995-01-26 00:38:22 +0000 | [diff] [blame] | 1492 | { |
| 1493 | if (x == NULL) |
| 1494 | return 0; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1495 | if (PyInstance_Check(x)) { |
| 1496 | PyObject *call = PyObject_GetAttrString(x, "__call__"); |
Guido van Rossum | 49b11fe | 1995-01-26 00:38:22 +0000 | [diff] [blame] | 1497 | if (call == NULL) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1498 | PyErr_Clear(); |
Guido van Rossum | 49b11fe | 1995-01-26 00:38:22 +0000 | [diff] [blame] | 1499 | return 0; |
| 1500 | } |
| 1501 | /* Could test recursively but don't, for fear of endless |
| 1502 | recursion if some joker sets self.__call__ = self */ |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1503 | Py_DECREF(call); |
Guido van Rossum | 49b11fe | 1995-01-26 00:38:22 +0000 | [diff] [blame] | 1504 | return 1; |
| 1505 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1506 | else { |
| 1507 | return x->ob_type->tp_call != NULL; |
| 1508 | } |
Guido van Rossum | 49b11fe | 1995-01-26 00:38:22 +0000 | [diff] [blame] | 1509 | } |
| 1510 | |
Tim Peters | 7eea37e | 2001-09-04 22:08:56 +0000 | [diff] [blame] | 1511 | /* Helper for PyObject_Dir. |
| 1512 | Merge the __dict__ of aclass into dict, and recursively also all |
| 1513 | the __dict__s of aclass's base classes. The order of merging isn't |
| 1514 | defined, as it's expected that only the final set of dict keys is |
| 1515 | interesting. |
| 1516 | Return 0 on success, -1 on error. |
| 1517 | */ |
| 1518 | |
| 1519 | static int |
| 1520 | merge_class_dict(PyObject* dict, PyObject* aclass) |
| 1521 | { |
| 1522 | PyObject *classdict; |
| 1523 | PyObject *bases; |
| 1524 | |
| 1525 | assert(PyDict_Check(dict)); |
| 1526 | assert(aclass); |
| 1527 | |
| 1528 | /* Merge in the type's dict (if any). */ |
| 1529 | classdict = PyObject_GetAttrString(aclass, "__dict__"); |
| 1530 | if (classdict == NULL) |
| 1531 | PyErr_Clear(); |
| 1532 | else { |
| 1533 | int status = PyDict_Update(dict, classdict); |
| 1534 | Py_DECREF(classdict); |
| 1535 | if (status < 0) |
| 1536 | return -1; |
| 1537 | } |
| 1538 | |
| 1539 | /* Recursively merge in the base types' (if any) dicts. */ |
| 1540 | bases = PyObject_GetAttrString(aclass, "__bases__"); |
Tim Peters | bc7e863 | 2001-09-16 20:33:22 +0000 | [diff] [blame] | 1541 | if (bases == NULL) |
| 1542 | PyErr_Clear(); |
| 1543 | else { |
Guido van Rossum | 4402241 | 2002-05-13 18:29:46 +0000 | [diff] [blame] | 1544 | /* We have no guarantee that bases is a real tuple */ |
Tim Peters | 7eea37e | 2001-09-04 22:08:56 +0000 | [diff] [blame] | 1545 | int i, n; |
Guido van Rossum | 4402241 | 2002-05-13 18:29:46 +0000 | [diff] [blame] | 1546 | n = PySequence_Size(bases); /* This better be right */ |
| 1547 | if (n < 0) |
| 1548 | PyErr_Clear(); |
| 1549 | else { |
| 1550 | for (i = 0; i < n; i++) { |
Tim Peters | 18e7083 | 2003-02-05 19:35:19 +0000 | [diff] [blame] | 1551 | int status; |
Guido van Rossum | 4402241 | 2002-05-13 18:29:46 +0000 | [diff] [blame] | 1552 | PyObject *base = PySequence_GetItem(bases, i); |
| 1553 | if (base == NULL) { |
| 1554 | Py_DECREF(bases); |
| 1555 | return -1; |
| 1556 | } |
Tim Peters | 18e7083 | 2003-02-05 19:35:19 +0000 | [diff] [blame] | 1557 | status = merge_class_dict(dict, base); |
| 1558 | Py_DECREF(base); |
| 1559 | if (status < 0) { |
Guido van Rossum | 4402241 | 2002-05-13 18:29:46 +0000 | [diff] [blame] | 1560 | Py_DECREF(bases); |
| 1561 | return -1; |
| 1562 | } |
Tim Peters | 7eea37e | 2001-09-04 22:08:56 +0000 | [diff] [blame] | 1563 | } |
| 1564 | } |
| 1565 | Py_DECREF(bases); |
| 1566 | } |
| 1567 | return 0; |
| 1568 | } |
| 1569 | |
Tim Peters | 305b585 | 2001-09-17 02:38:46 +0000 | [diff] [blame] | 1570 | /* Helper for PyObject_Dir. |
| 1571 | If obj has an attr named attrname that's a list, merge its string |
| 1572 | elements into keys of dict. |
| 1573 | Return 0 on success, -1 on error. Errors due to not finding the attr, |
| 1574 | or the attr not being a list, are suppressed. |
| 1575 | */ |
| 1576 | |
| 1577 | static int |
| 1578 | merge_list_attr(PyObject* dict, PyObject* obj, char *attrname) |
| 1579 | { |
| 1580 | PyObject *list; |
| 1581 | int result = 0; |
| 1582 | |
| 1583 | assert(PyDict_Check(dict)); |
| 1584 | assert(obj); |
| 1585 | assert(attrname); |
| 1586 | |
| 1587 | list = PyObject_GetAttrString(obj, attrname); |
| 1588 | if (list == NULL) |
| 1589 | PyErr_Clear(); |
| 1590 | |
| 1591 | else if (PyList_Check(list)) { |
| 1592 | int i; |
| 1593 | for (i = 0; i < PyList_GET_SIZE(list); ++i) { |
| 1594 | PyObject *item = PyList_GET_ITEM(list, i); |
| 1595 | if (PyString_Check(item)) { |
| 1596 | result = PyDict_SetItem(dict, item, Py_None); |
| 1597 | if (result < 0) |
| 1598 | break; |
| 1599 | } |
| 1600 | } |
| 1601 | } |
| 1602 | |
| 1603 | Py_XDECREF(list); |
| 1604 | return result; |
| 1605 | } |
| 1606 | |
Tim Peters | 7eea37e | 2001-09-04 22:08:56 +0000 | [diff] [blame] | 1607 | /* Like __builtin__.dir(arg). See bltinmodule.c's builtin_dir for the |
| 1608 | docstring, which should be kept in synch with this implementation. */ |
| 1609 | |
| 1610 | PyObject * |
| 1611 | PyObject_Dir(PyObject *arg) |
| 1612 | { |
| 1613 | /* Set exactly one of these non-NULL before the end. */ |
| 1614 | PyObject *result = NULL; /* result list */ |
| 1615 | PyObject *masterdict = NULL; /* result is masterdict.keys() */ |
| 1616 | |
| 1617 | /* If NULL arg, return the locals. */ |
| 1618 | if (arg == NULL) { |
| 1619 | PyObject *locals = PyEval_GetLocals(); |
| 1620 | if (locals == NULL) |
| 1621 | goto error; |
Raymond Hettinger | 214b1c3 | 2004-07-02 06:41:07 +0000 | [diff] [blame] | 1622 | result = PyMapping_Keys(locals); |
Tim Peters | 7eea37e | 2001-09-04 22:08:56 +0000 | [diff] [blame] | 1623 | if (result == NULL) |
| 1624 | goto error; |
| 1625 | } |
| 1626 | |
| 1627 | /* Elif this is some form of module, we only want its dict. */ |
Guido van Rossum | 8dbd3d8 | 2001-09-10 18:27:43 +0000 | [diff] [blame] | 1628 | else if (PyModule_Check(arg)) { |
Tim Peters | 7eea37e | 2001-09-04 22:08:56 +0000 | [diff] [blame] | 1629 | masterdict = PyObject_GetAttrString(arg, "__dict__"); |
| 1630 | if (masterdict == NULL) |
| 1631 | goto error; |
Guido van Rossum | 8dbd3d8 | 2001-09-10 18:27:43 +0000 | [diff] [blame] | 1632 | if (!PyDict_Check(masterdict)) { |
| 1633 | PyErr_SetString(PyExc_TypeError, |
| 1634 | "module.__dict__ is not a dictionary"); |
| 1635 | goto error; |
| 1636 | } |
Tim Peters | 7eea37e | 2001-09-04 22:08:56 +0000 | [diff] [blame] | 1637 | } |
| 1638 | |
| 1639 | /* Elif some form of type or class, grab its dict and its bases. |
| 1640 | We deliberately don't suck up its __class__, as methods belonging |
| 1641 | to the metaclass would probably be more confusing than helpful. */ |
| 1642 | else if (PyType_Check(arg) || PyClass_Check(arg)) { |
| 1643 | masterdict = PyDict_New(); |
| 1644 | if (masterdict == NULL) |
| 1645 | goto error; |
| 1646 | if (merge_class_dict(masterdict, arg) < 0) |
| 1647 | goto error; |
| 1648 | } |
| 1649 | |
| 1650 | /* Else look at its dict, and the attrs reachable from its class. */ |
| 1651 | else { |
| 1652 | PyObject *itsclass; |
| 1653 | /* Create a dict to start with. CAUTION: Not everything |
| 1654 | responding to __dict__ returns a dict! */ |
| 1655 | masterdict = PyObject_GetAttrString(arg, "__dict__"); |
| 1656 | if (masterdict == NULL) { |
| 1657 | PyErr_Clear(); |
| 1658 | masterdict = PyDict_New(); |
| 1659 | } |
| 1660 | else if (!PyDict_Check(masterdict)) { |
| 1661 | Py_DECREF(masterdict); |
| 1662 | masterdict = PyDict_New(); |
| 1663 | } |
| 1664 | else { |
| 1665 | /* The object may have returned a reference to its |
| 1666 | dict, so copy it to avoid mutating it. */ |
| 1667 | PyObject *temp = PyDict_Copy(masterdict); |
| 1668 | Py_DECREF(masterdict); |
| 1669 | masterdict = temp; |
| 1670 | } |
| 1671 | if (masterdict == NULL) |
| 1672 | goto error; |
| 1673 | |
Tim Peters | 305b585 | 2001-09-17 02:38:46 +0000 | [diff] [blame] | 1674 | /* Merge in __members__ and __methods__ (if any). |
| 1675 | XXX Would like this to go away someday; for now, it's |
| 1676 | XXX needed to get at im_self etc of method objects. */ |
| 1677 | if (merge_list_attr(masterdict, arg, "__members__") < 0) |
| 1678 | goto error; |
| 1679 | if (merge_list_attr(masterdict, arg, "__methods__") < 0) |
| 1680 | goto error; |
| 1681 | |
Tim Peters | 7eea37e | 2001-09-04 22:08:56 +0000 | [diff] [blame] | 1682 | /* Merge in attrs reachable from its class. |
| 1683 | CAUTION: Not all objects have a __class__ attr. */ |
| 1684 | itsclass = PyObject_GetAttrString(arg, "__class__"); |
| 1685 | if (itsclass == NULL) |
| 1686 | PyErr_Clear(); |
| 1687 | else { |
| 1688 | int status = merge_class_dict(masterdict, itsclass); |
| 1689 | Py_DECREF(itsclass); |
| 1690 | if (status < 0) |
| 1691 | goto error; |
| 1692 | } |
| 1693 | } |
| 1694 | |
| 1695 | assert((result == NULL) ^ (masterdict == NULL)); |
| 1696 | if (masterdict != NULL) { |
| 1697 | /* The result comes from its keys. */ |
| 1698 | assert(result == NULL); |
| 1699 | result = PyDict_Keys(masterdict); |
| 1700 | if (result == NULL) |
| 1701 | goto error; |
| 1702 | } |
| 1703 | |
| 1704 | assert(result); |
Raymond Hettinger | 2a7dede | 2004-08-07 04:55:30 +0000 | [diff] [blame] | 1705 | if (!PyList_Check(result)) { |
Tim Peters | f4aca75 | 2004-09-23 02:39:37 +0000 | [diff] [blame] | 1706 | PyErr_SetString(PyExc_TypeError, |
Raymond Hettinger | 2a7dede | 2004-08-07 04:55:30 +0000 | [diff] [blame] | 1707 | "Expected keys() to be a list."); |
| 1708 | goto error; |
| 1709 | } |
Tim Peters | 7eea37e | 2001-09-04 22:08:56 +0000 | [diff] [blame] | 1710 | if (PyList_Sort(result) != 0) |
| 1711 | goto error; |
| 1712 | else |
| 1713 | goto normal_return; |
| 1714 | |
| 1715 | error: |
| 1716 | Py_XDECREF(result); |
| 1717 | result = NULL; |
| 1718 | /* fall through */ |
| 1719 | normal_return: |
| 1720 | Py_XDECREF(masterdict); |
| 1721 | return result; |
| 1722 | } |
Guido van Rossum | 49b11fe | 1995-01-26 00:38:22 +0000 | [diff] [blame] | 1723 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1724 | /* |
| 1725 | NoObject is usable as a non-NULL undefined value, used by the macro None. |
| 1726 | 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] | 1727 | so there is exactly one (which is indestructible, by the way). |
Guido van Rossum | ba21a49 | 2001-08-16 08:17:26 +0000 | [diff] [blame] | 1728 | (XXX This type and the type of NotImplemented below should be unified.) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1729 | */ |
| 1730 | |
Guido van Rossum | 0c182a1 | 1992-03-27 17:26:13 +0000 | [diff] [blame] | 1731 | /* ARGSUSED */ |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1732 | static PyObject * |
Fred Drake | 100814d | 2000-07-09 15:48:49 +0000 | [diff] [blame] | 1733 | none_repr(PyObject *op) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1734 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1735 | return PyString_FromString("None"); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1736 | } |
| 1737 | |
Barry Warsaw | 9bf1644 | 2001-01-23 16:24:35 +0000 | [diff] [blame] | 1738 | /* ARGUSED */ |
| 1739 | static void |
Tim Peters | 803526b | 2002-07-07 05:13:56 +0000 | [diff] [blame] | 1740 | none_dealloc(PyObject* ignore) |
Barry Warsaw | 9bf1644 | 2001-01-23 16:24:35 +0000 | [diff] [blame] | 1741 | { |
| 1742 | /* This should never get called, but we also don't want to SEGV if |
| 1743 | * we accidently decref None out of existance. |
| 1744 | */ |
Martin v. Löwis | 3f19b10 | 2002-08-07 16:21:51 +0000 | [diff] [blame] | 1745 | Py_FatalError("deallocating None"); |
Barry Warsaw | 9bf1644 | 2001-01-23 16:24:35 +0000 | [diff] [blame] | 1746 | } |
| 1747 | |
| 1748 | |
Guido van Rossum | ba21a49 | 2001-08-16 08:17:26 +0000 | [diff] [blame] | 1749 | static PyTypeObject PyNone_Type = { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1750 | PyObject_HEAD_INIT(&PyType_Type) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1751 | 0, |
Guido van Rossum | ba21a49 | 2001-08-16 08:17:26 +0000 | [diff] [blame] | 1752 | "NoneType", |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1753 | 0, |
| 1754 | 0, |
Barry Warsaw | 9bf1644 | 2001-01-23 16:24:35 +0000 | [diff] [blame] | 1755 | (destructor)none_dealloc, /*tp_dealloc*/ /*never called*/ |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 1756 | 0, /*tp_print*/ |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1757 | 0, /*tp_getattr*/ |
| 1758 | 0, /*tp_setattr*/ |
| 1759 | 0, /*tp_compare*/ |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 1760 | (reprfunc)none_repr, /*tp_repr*/ |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1761 | 0, /*tp_as_number*/ |
| 1762 | 0, /*tp_as_sequence*/ |
| 1763 | 0, /*tp_as_mapping*/ |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 1764 | 0, /*tp_hash */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1765 | }; |
| 1766 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1767 | PyObject _Py_NoneStruct = { |
Guido van Rossum | ba21a49 | 2001-08-16 08:17:26 +0000 | [diff] [blame] | 1768 | PyObject_HEAD_INIT(&PyNone_Type) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1769 | }; |
| 1770 | |
Neil Schemenauer | 5ed85ec | 2001-01-04 01:48:10 +0000 | [diff] [blame] | 1771 | /* NotImplemented is an object that can be used to signal that an |
| 1772 | operation is not implemented for the given type combination. */ |
| 1773 | |
| 1774 | static PyObject * |
| 1775 | NotImplemented_repr(PyObject *op) |
| 1776 | { |
| 1777 | return PyString_FromString("NotImplemented"); |
| 1778 | } |
| 1779 | |
| 1780 | static PyTypeObject PyNotImplemented_Type = { |
| 1781 | PyObject_HEAD_INIT(&PyType_Type) |
| 1782 | 0, |
Guido van Rossum | ba21a49 | 2001-08-16 08:17:26 +0000 | [diff] [blame] | 1783 | "NotImplementedType", |
Neil Schemenauer | 5ed85ec | 2001-01-04 01:48:10 +0000 | [diff] [blame] | 1784 | 0, |
| 1785 | 0, |
Barry Warsaw | 9bf1644 | 2001-01-23 16:24:35 +0000 | [diff] [blame] | 1786 | (destructor)none_dealloc, /*tp_dealloc*/ /*never called*/ |
Neil Schemenauer | 5ed85ec | 2001-01-04 01:48:10 +0000 | [diff] [blame] | 1787 | 0, /*tp_print*/ |
| 1788 | 0, /*tp_getattr*/ |
| 1789 | 0, /*tp_setattr*/ |
| 1790 | 0, /*tp_compare*/ |
| 1791 | (reprfunc)NotImplemented_repr, /*tp_repr*/ |
| 1792 | 0, /*tp_as_number*/ |
| 1793 | 0, /*tp_as_sequence*/ |
| 1794 | 0, /*tp_as_mapping*/ |
| 1795 | 0, /*tp_hash */ |
| 1796 | }; |
| 1797 | |
| 1798 | PyObject _Py_NotImplementedStruct = { |
| 1799 | PyObject_HEAD_INIT(&PyNotImplemented_Type) |
| 1800 | }; |
| 1801 | |
Guido van Rossum | ba21a49 | 2001-08-16 08:17:26 +0000 | [diff] [blame] | 1802 | void |
| 1803 | _Py_ReadyTypes(void) |
| 1804 | { |
| 1805 | if (PyType_Ready(&PyType_Type) < 0) |
| 1806 | Py_FatalError("Can't initialize 'type'"); |
| 1807 | |
Fred Drake | 0a4dd39 | 2004-07-02 18:57:45 +0000 | [diff] [blame] | 1808 | if (PyType_Ready(&_PyWeakref_RefType) < 0) |
| 1809 | Py_FatalError("Can't initialize 'weakref'"); |
| 1810 | |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 1811 | if (PyType_Ready(&PyBool_Type) < 0) |
| 1812 | Py_FatalError("Can't initialize 'bool'"); |
| 1813 | |
Guido van Rossum | cacfc07 | 2002-05-24 19:01:59 +0000 | [diff] [blame] | 1814 | if (PyType_Ready(&PyString_Type) < 0) |
| 1815 | Py_FatalError("Can't initialize 'str'"); |
| 1816 | |
Guido van Rossum | ba21a49 | 2001-08-16 08:17:26 +0000 | [diff] [blame] | 1817 | if (PyType_Ready(&PyList_Type) < 0) |
| 1818 | Py_FatalError("Can't initialize 'list'"); |
| 1819 | |
| 1820 | if (PyType_Ready(&PyNone_Type) < 0) |
| 1821 | Py_FatalError("Can't initialize type(None)"); |
| 1822 | |
| 1823 | if (PyType_Ready(&PyNotImplemented_Type) < 0) |
| 1824 | Py_FatalError("Can't initialize type(NotImplemented)"); |
| 1825 | } |
| 1826 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1827 | |
Guido van Rossum | 84a9032 | 1996-05-22 16:34:47 +0000 | [diff] [blame] | 1828 | #ifdef Py_TRACE_REFS |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1829 | |
Guido van Rossum | aacdc9d | 1996-08-12 21:32:12 +0000 | [diff] [blame] | 1830 | void |
Fred Drake | 100814d | 2000-07-09 15:48:49 +0000 | [diff] [blame] | 1831 | _Py_NewReference(PyObject *op) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1832 | { |
Tim Peters | 3459251 | 2002-07-11 06:23:50 +0000 | [diff] [blame] | 1833 | _Py_INC_REFTOTAL; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1834 | op->ob_refcnt = 1; |
Tim Peters | 7571a0f | 2003-03-23 17:52:28 +0000 | [diff] [blame] | 1835 | _Py_AddToAllObjects(op, 1); |
Tim Peters | 3459251 | 2002-07-11 06:23:50 +0000 | [diff] [blame] | 1836 | _Py_INC_TPALLOCS(op); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1837 | } |
| 1838 | |
Guido van Rossum | aacdc9d | 1996-08-12 21:32:12 +0000 | [diff] [blame] | 1839 | void |
Fred Drake | 100814d | 2000-07-09 15:48:49 +0000 | [diff] [blame] | 1840 | _Py_ForgetReference(register PyObject *op) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1841 | { |
Guido van Rossum | bffd683 | 2000-01-20 22:32:56 +0000 | [diff] [blame] | 1842 | #ifdef SLOW_UNREF_CHECK |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 1843 | register PyObject *p; |
Guido van Rossum | bffd683 | 2000-01-20 22:32:56 +0000 | [diff] [blame] | 1844 | #endif |
Guido van Rossum | d7047b3 | 1995-01-02 19:07:15 +0000 | [diff] [blame] | 1845 | if (op->ob_refcnt < 0) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1846 | Py_FatalError("UNREF negative refcnt"); |
Guido van Rossum | 2e8f614 | 1992-09-03 20:32:55 +0000 | [diff] [blame] | 1847 | if (op == &refchain || |
Guido van Rossum | d7047b3 | 1995-01-02 19:07:15 +0000 | [diff] [blame] | 1848 | op->_ob_prev->_ob_next != op || op->_ob_next->_ob_prev != op) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1849 | Py_FatalError("UNREF invalid object"); |
Guido van Rossum | 2e8f614 | 1992-09-03 20:32:55 +0000 | [diff] [blame] | 1850 | #ifdef SLOW_UNREF_CHECK |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1851 | for (p = refchain._ob_next; p != &refchain; p = p->_ob_next) { |
| 1852 | if (p == op) |
| 1853 | break; |
| 1854 | } |
Guido van Rossum | d7047b3 | 1995-01-02 19:07:15 +0000 | [diff] [blame] | 1855 | if (p == &refchain) /* Not found */ |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1856 | Py_FatalError("UNREF unknown object"); |
Guido van Rossum | 2e8f614 | 1992-09-03 20:32:55 +0000 | [diff] [blame] | 1857 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1858 | op->_ob_next->_ob_prev = op->_ob_prev; |
| 1859 | op->_ob_prev->_ob_next = op->_ob_next; |
Guido van Rossum | 2e8f614 | 1992-09-03 20:32:55 +0000 | [diff] [blame] | 1860 | op->_ob_next = op->_ob_prev = NULL; |
Tim Peters | 3459251 | 2002-07-11 06:23:50 +0000 | [diff] [blame] | 1861 | _Py_INC_TPFREES(op); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1862 | } |
| 1863 | |
Guido van Rossum | aacdc9d | 1996-08-12 21:32:12 +0000 | [diff] [blame] | 1864 | void |
Fred Drake | 100814d | 2000-07-09 15:48:49 +0000 | [diff] [blame] | 1865 | _Py_Dealloc(PyObject *op) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1866 | { |
Guido van Rossum | 9776adf | 1994-09-07 14:36:45 +0000 | [diff] [blame] | 1867 | destructor dealloc = op->ob_type->tp_dealloc; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1868 | _Py_ForgetReference(op); |
Guido van Rossum | 9776adf | 1994-09-07 14:36:45 +0000 | [diff] [blame] | 1869 | (*dealloc)(op); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1870 | } |
| 1871 | |
Tim Peters | 269b2a6 | 2003-04-17 19:52:29 +0000 | [diff] [blame] | 1872 | /* Print all live objects. Because PyObject_Print is called, the |
| 1873 | * interpreter must be in a healthy state. |
| 1874 | */ |
Guido van Rossum | aacdc9d | 1996-08-12 21:32:12 +0000 | [diff] [blame] | 1875 | void |
Fred Drake | 100814d | 2000-07-09 15:48:49 +0000 | [diff] [blame] | 1876 | _Py_PrintReferences(FILE *fp) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1877 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1878 | PyObject *op; |
Guido van Rossum | e09fb55 | 1997-08-05 02:04:34 +0000 | [diff] [blame] | 1879 | fprintf(fp, "Remaining objects:\n"); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1880 | for (op = refchain._ob_next; op != &refchain; op = op->_ob_next) { |
Tim Peters | 269b2a6 | 2003-04-17 19:52:29 +0000 | [diff] [blame] | 1881 | fprintf(fp, "%p [%d] ", op, op->ob_refcnt); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1882 | if (PyObject_Print(op, fp, 0) != 0) |
| 1883 | PyErr_Clear(); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1884 | putc('\n', fp); |
| 1885 | } |
| 1886 | } |
| 1887 | |
Tim Peters | 269b2a6 | 2003-04-17 19:52:29 +0000 | [diff] [blame] | 1888 | /* Print the addresses of all live objects. Unlike _Py_PrintReferences, this |
| 1889 | * doesn't make any calls to the Python C API, so is always safe to call. |
| 1890 | */ |
| 1891 | void |
| 1892 | _Py_PrintReferenceAddresses(FILE *fp) |
| 1893 | { |
| 1894 | PyObject *op; |
| 1895 | fprintf(fp, "Remaining object addresses:\n"); |
| 1896 | for (op = refchain._ob_next; op != &refchain; op = op->_ob_next) |
Tim Peters | 21d7d4d | 2003-04-18 00:45:59 +0000 | [diff] [blame] | 1897 | fprintf(fp, "%p [%d] %s\n", op, op->ob_refcnt, |
| 1898 | op->ob_type->tp_name); |
Tim Peters | 269b2a6 | 2003-04-17 19:52:29 +0000 | [diff] [blame] | 1899 | } |
| 1900 | |
Sjoerd Mullender | 6ec3c65 | 1995-08-29 09:18:14 +0000 | [diff] [blame] | 1901 | PyObject * |
Fred Drake | 100814d | 2000-07-09 15:48:49 +0000 | [diff] [blame] | 1902 | _Py_GetObjects(PyObject *self, PyObject *args) |
Sjoerd Mullender | 6ec3c65 | 1995-08-29 09:18:14 +0000 | [diff] [blame] | 1903 | { |
| 1904 | int i, n; |
| 1905 | PyObject *t = NULL; |
| 1906 | PyObject *res, *op; |
| 1907 | |
| 1908 | if (!PyArg_ParseTuple(args, "i|O", &n, &t)) |
| 1909 | return NULL; |
| 1910 | op = refchain._ob_next; |
| 1911 | res = PyList_New(0); |
| 1912 | if (res == NULL) |
| 1913 | return NULL; |
| 1914 | for (i = 0; (n == 0 || i < n) && op != &refchain; i++) { |
| 1915 | while (op == self || op == args || op == res || op == t || |
Guido van Rossum | 3c29602 | 2001-07-14 17:58:00 +0000 | [diff] [blame] | 1916 | (t != NULL && op->ob_type != (PyTypeObject *) t)) { |
Sjoerd Mullender | 6ec3c65 | 1995-08-29 09:18:14 +0000 | [diff] [blame] | 1917 | op = op->_ob_next; |
| 1918 | if (op == &refchain) |
| 1919 | return res; |
| 1920 | } |
| 1921 | if (PyList_Append(res, op) < 0) { |
| 1922 | Py_DECREF(res); |
| 1923 | return NULL; |
| 1924 | } |
| 1925 | op = op->_ob_next; |
| 1926 | } |
| 1927 | return res; |
| 1928 | } |
| 1929 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1930 | #endif |
Guido van Rossum | 97ead3f | 1996-01-12 01:24:09 +0000 | [diff] [blame] | 1931 | |
| 1932 | |
| 1933 | /* Hack to force loading of cobject.o */ |
Guido van Rossum | da9c271 | 1996-12-05 21:58:58 +0000 | [diff] [blame] | 1934 | PyTypeObject *_Py_cobject_hack = &PyCObject_Type; |
Guido van Rossum | 84a9032 | 1996-05-22 16:34:47 +0000 | [diff] [blame] | 1935 | |
| 1936 | |
| 1937 | /* Hack to force loading of abstract.o */ |
Neal Norwitz | 4178515 | 2002-06-13 21:42:51 +0000 | [diff] [blame] | 1938 | int (*_Py_abstract_hack)(PyObject *) = PyObject_Size; |
Guido van Rossum | e09fb55 | 1997-08-05 02:04:34 +0000 | [diff] [blame] | 1939 | |
| 1940 | |
Andrew M. Kuchling | 1582a3a | 2000-08-16 12:27:23 +0000 | [diff] [blame] | 1941 | /* Python's malloc wrappers (see pymem.h) */ |
Guido van Rossum | e09fb55 | 1997-08-05 02:04:34 +0000 | [diff] [blame] | 1942 | |
Thomas Wouters | 334fb89 | 2000-07-25 12:56:38 +0000 | [diff] [blame] | 1943 | void * |
Fred Drake | 100814d | 2000-07-09 15:48:49 +0000 | [diff] [blame] | 1944 | PyMem_Malloc(size_t nbytes) |
Guido van Rossum | e09fb55 | 1997-08-05 02:04:34 +0000 | [diff] [blame] | 1945 | { |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 1946 | return PyMem_MALLOC(nbytes); |
Guido van Rossum | e09fb55 | 1997-08-05 02:04:34 +0000 | [diff] [blame] | 1947 | } |
| 1948 | |
Thomas Wouters | 334fb89 | 2000-07-25 12:56:38 +0000 | [diff] [blame] | 1949 | void * |
| 1950 | PyMem_Realloc(void *p, size_t nbytes) |
Guido van Rossum | e09fb55 | 1997-08-05 02:04:34 +0000 | [diff] [blame] | 1951 | { |
Tim Peters | af3e8de | 2002-04-12 07:22:56 +0000 | [diff] [blame] | 1952 | return PyMem_REALLOC(p, nbytes); |
Guido van Rossum | e09fb55 | 1997-08-05 02:04:34 +0000 | [diff] [blame] | 1953 | } |
| 1954 | |
| 1955 | void |
Thomas Wouters | 334fb89 | 2000-07-25 12:56:38 +0000 | [diff] [blame] | 1956 | PyMem_Free(void *p) |
Guido van Rossum | e09fb55 | 1997-08-05 02:04:34 +0000 | [diff] [blame] | 1957 | { |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 1958 | PyMem_FREE(p); |
| 1959 | } |
| 1960 | |
| 1961 | |
Guido van Rossum | 8661036 | 1998-04-10 22:32:46 +0000 | [diff] [blame] | 1962 | /* These methods are used to control infinite recursion in repr, str, print, |
| 1963 | etc. Container objects that may recursively contain themselves, |
| 1964 | e.g. builtin dictionaries and lists, should used Py_ReprEnter() and |
| 1965 | Py_ReprLeave() to avoid infinite recursion. |
| 1966 | |
| 1967 | Py_ReprEnter() returns 0 the first time it is called for a particular |
| 1968 | object and 1 every time thereafter. It returns -1 if an exception |
| 1969 | occurred. Py_ReprLeave() has no return value. |
| 1970 | |
| 1971 | See dictobject.c and listobject.c for examples of use. |
| 1972 | */ |
| 1973 | |
| 1974 | #define KEY "Py_Repr" |
| 1975 | |
| 1976 | int |
Fred Drake | 100814d | 2000-07-09 15:48:49 +0000 | [diff] [blame] | 1977 | Py_ReprEnter(PyObject *obj) |
Guido van Rossum | 8661036 | 1998-04-10 22:32:46 +0000 | [diff] [blame] | 1978 | { |
| 1979 | PyObject *dict; |
| 1980 | PyObject *list; |
| 1981 | int i; |
| 1982 | |
| 1983 | dict = PyThreadState_GetDict(); |
| 1984 | if (dict == NULL) |
Guido van Rossum | 0fc8f00 | 2003-04-15 15:12:39 +0000 | [diff] [blame] | 1985 | return 0; |
Guido van Rossum | 8661036 | 1998-04-10 22:32:46 +0000 | [diff] [blame] | 1986 | list = PyDict_GetItemString(dict, KEY); |
| 1987 | if (list == NULL) { |
| 1988 | list = PyList_New(0); |
| 1989 | if (list == NULL) |
| 1990 | return -1; |
| 1991 | if (PyDict_SetItemString(dict, KEY, list) < 0) |
| 1992 | return -1; |
| 1993 | Py_DECREF(list); |
| 1994 | } |
| 1995 | i = PyList_GET_SIZE(list); |
| 1996 | while (--i >= 0) { |
| 1997 | if (PyList_GET_ITEM(list, i) == obj) |
| 1998 | return 1; |
| 1999 | } |
| 2000 | PyList_Append(list, obj); |
| 2001 | return 0; |
| 2002 | } |
| 2003 | |
| 2004 | void |
Fred Drake | 100814d | 2000-07-09 15:48:49 +0000 | [diff] [blame] | 2005 | Py_ReprLeave(PyObject *obj) |
Guido van Rossum | 8661036 | 1998-04-10 22:32:46 +0000 | [diff] [blame] | 2006 | { |
| 2007 | PyObject *dict; |
| 2008 | PyObject *list; |
| 2009 | int i; |
| 2010 | |
| 2011 | dict = PyThreadState_GetDict(); |
Guido van Rossum | eb90946 | 1998-04-11 15:17:34 +0000 | [diff] [blame] | 2012 | if (dict == NULL) |
| 2013 | return; |
Guido van Rossum | 8661036 | 1998-04-10 22:32:46 +0000 | [diff] [blame] | 2014 | list = PyDict_GetItemString(dict, KEY); |
Guido van Rossum | eb90946 | 1998-04-11 15:17:34 +0000 | [diff] [blame] | 2015 | if (list == NULL || !PyList_Check(list)) |
| 2016 | return; |
Guido van Rossum | 8661036 | 1998-04-10 22:32:46 +0000 | [diff] [blame] | 2017 | i = PyList_GET_SIZE(list); |
| 2018 | /* Count backwards because we always expect obj to be list[-1] */ |
| 2019 | while (--i >= 0) { |
| 2020 | if (PyList_GET_ITEM(list, i) == obj) { |
| 2021 | PyList_SetSlice(list, i, i + 1, NULL); |
| 2022 | break; |
| 2023 | } |
| 2024 | } |
| 2025 | } |
Guido van Rossum | d724b23 | 2000-03-13 16:01:29 +0000 | [diff] [blame] | 2026 | |
Tim Peters | 803526b | 2002-07-07 05:13:56 +0000 | [diff] [blame] | 2027 | /* Trashcan support. */ |
Guido van Rossum | d724b23 | 2000-03-13 16:01:29 +0000 | [diff] [blame] | 2028 | |
Tim Peters | 803526b | 2002-07-07 05:13:56 +0000 | [diff] [blame] | 2029 | /* Current call-stack depth of tp_dealloc calls. */ |
Guido van Rossum | d724b23 | 2000-03-13 16:01:29 +0000 | [diff] [blame] | 2030 | int _PyTrash_delete_nesting = 0; |
Guido van Rossum | e92e610 | 2000-04-24 15:40:53 +0000 | [diff] [blame] | 2031 | |
Tim Peters | 803526b | 2002-07-07 05:13:56 +0000 | [diff] [blame] | 2032 | /* List of objects that still need to be cleaned up, singly linked via their |
| 2033 | * gc headers' gc_prev pointers. |
| 2034 | */ |
| 2035 | PyObject *_PyTrash_delete_later = NULL; |
Guido van Rossum | d724b23 | 2000-03-13 16:01:29 +0000 | [diff] [blame] | 2036 | |
Tim Peters | 803526b | 2002-07-07 05:13:56 +0000 | [diff] [blame] | 2037 | /* Add op to the _PyTrash_delete_later list. Called when the current |
| 2038 | * call-stack depth gets large. op must be a currently untracked gc'ed |
| 2039 | * object, with refcount 0. Py_DECREF must already have been called on it. |
| 2040 | */ |
Guido van Rossum | d724b23 | 2000-03-13 16:01:29 +0000 | [diff] [blame] | 2041 | void |
Fred Drake | 100814d | 2000-07-09 15:48:49 +0000 | [diff] [blame] | 2042 | _PyTrash_deposit_object(PyObject *op) |
Guido van Rossum | d724b23 | 2000-03-13 16:01:29 +0000 | [diff] [blame] | 2043 | { |
Tim Peters | 803526b | 2002-07-07 05:13:56 +0000 | [diff] [blame] | 2044 | assert(PyObject_IS_GC(op)); |
| 2045 | assert(_Py_AS_GC(op)->gc.gc_refs == _PyGC_REFS_UNTRACKED); |
| 2046 | assert(op->ob_refcnt == 0); |
Neil Schemenauer | f589c05 | 2002-03-29 03:05:54 +0000 | [diff] [blame] | 2047 | _Py_AS_GC(op)->gc.gc_prev = (PyGC_Head *)_PyTrash_delete_later; |
Guido van Rossum | e92e610 | 2000-04-24 15:40:53 +0000 | [diff] [blame] | 2048 | _PyTrash_delete_later = op; |
Guido van Rossum | d724b23 | 2000-03-13 16:01:29 +0000 | [diff] [blame] | 2049 | } |
| 2050 | |
Tim Peters | 803526b | 2002-07-07 05:13:56 +0000 | [diff] [blame] | 2051 | /* Dealloccate all the objects in the _PyTrash_delete_later list. Called when |
| 2052 | * the call-stack unwinds again. |
| 2053 | */ |
Guido van Rossum | d724b23 | 2000-03-13 16:01:29 +0000 | [diff] [blame] | 2054 | void |
Fred Drake | 100814d | 2000-07-09 15:48:49 +0000 | [diff] [blame] | 2055 | _PyTrash_destroy_chain(void) |
Guido van Rossum | d724b23 | 2000-03-13 16:01:29 +0000 | [diff] [blame] | 2056 | { |
| 2057 | while (_PyTrash_delete_later) { |
Tim Peters | 803526b | 2002-07-07 05:13:56 +0000 | [diff] [blame] | 2058 | PyObject *op = _PyTrash_delete_later; |
| 2059 | destructor dealloc = op->ob_type->tp_dealloc; |
Neil Schemenauer | f589c05 | 2002-03-29 03:05:54 +0000 | [diff] [blame] | 2060 | |
Neil Schemenauer | f589c05 | 2002-03-29 03:05:54 +0000 | [diff] [blame] | 2061 | _PyTrash_delete_later = |
Tim Peters | 803526b | 2002-07-07 05:13:56 +0000 | [diff] [blame] | 2062 | (PyObject*) _Py_AS_GC(op)->gc.gc_prev; |
Neil Schemenauer | f589c05 | 2002-03-29 03:05:54 +0000 | [diff] [blame] | 2063 | |
Tim Peters | 803526b | 2002-07-07 05:13:56 +0000 | [diff] [blame] | 2064 | /* Call the deallocator directly. This used to try to |
| 2065 | * fool Py_DECREF into calling it indirectly, but |
| 2066 | * Py_DECREF was already called on this object, and in |
| 2067 | * assorted non-release builds calling Py_DECREF again ends |
| 2068 | * up distorting allocation statistics. |
| 2069 | */ |
| 2070 | assert(op->ob_refcnt == 0); |
Guido van Rossum | d724b23 | 2000-03-13 16:01:29 +0000 | [diff] [blame] | 2071 | ++_PyTrash_delete_nesting; |
Tim Peters | 803526b | 2002-07-07 05:13:56 +0000 | [diff] [blame] | 2072 | (*dealloc)(op); |
Guido van Rossum | d724b23 | 2000-03-13 16:01:29 +0000 | [diff] [blame] | 2073 | --_PyTrash_delete_nesting; |
| 2074 | } |
| 2075 | } |