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