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