Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 1 | /*********************************************************** |
Guido van Rossum | 6610ad9 | 1995-01-04 19:07:38 +0000 | [diff] [blame] | 2 | Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam, |
| 3 | The Netherlands. |
Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 4 | |
| 5 | All Rights Reserved |
| 6 | |
Guido van Rossum | d266eb4 | 1996-10-25 14:44:06 +0000 | [diff] [blame] | 7 | Permission to use, copy, modify, and distribute this software and its |
| 8 | documentation for any purpose and without fee is hereby granted, |
Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 9 | provided that the above copyright notice appear in all copies and that |
Guido van Rossum | d266eb4 | 1996-10-25 14:44:06 +0000 | [diff] [blame] | 10 | both that copyright notice and this permission notice appear in |
Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 11 | supporting documentation, and that the names of Stichting Mathematisch |
Guido van Rossum | d266eb4 | 1996-10-25 14:44:06 +0000 | [diff] [blame] | 12 | Centrum or CWI or Corporation for National Research Initiatives or |
| 13 | CNRI not be used in advertising or publicity pertaining to |
| 14 | distribution of the software without specific, written prior |
| 15 | permission. |
Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 16 | |
Guido van Rossum | d266eb4 | 1996-10-25 14:44:06 +0000 | [diff] [blame] | 17 | While CWI is the initial source for this software, a modified version |
| 18 | is made available by the Corporation for National Research Initiatives |
| 19 | (CNRI) at the Internet address ftp://ftp.python.org. |
| 20 | |
| 21 | STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH |
| 22 | REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF |
| 23 | MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH |
| 24 | CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL |
| 25 | DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR |
| 26 | PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER |
| 27 | TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR |
| 28 | PERFORMANCE OF THIS SOFTWARE. |
Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 29 | |
| 30 | ******************************************************************/ |
| 31 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 32 | /* Generic object operations; and implementation of None (NoObject) */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 33 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 34 | #include "Python.h" |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 35 | |
Fred Drake | 13634cf | 2000-06-29 19:17:04 +0000 | [diff] [blame] | 36 | #include "mymath.h" |
| 37 | |
Guido van Rossum | e92e610 | 2000-04-24 15:40:53 +0000 | [diff] [blame] | 38 | /* just for trashcan: */ |
| 39 | #include "compile.h" |
| 40 | #include "frameobject.h" |
| 41 | #include "traceback.h" |
| 42 | |
Guido van Rossum | 6f9e433 | 1995-03-29 16:57:48 +0000 | [diff] [blame] | 43 | #if defined( Py_TRACE_REFS ) || defined( Py_REF_DEBUG ) |
Guido van Rossum | bffd683 | 2000-01-20 22:32:56 +0000 | [diff] [blame] | 44 | DL_IMPORT(long) _Py_RefTotal; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 45 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 46 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 47 | /* Object allocation routines used by NEWOBJ and NEWVAROBJ macros. |
| 48 | These are used by the individual routines for object creation. |
| 49 | Do not call them otherwise, they do not initialize the object! */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 50 | |
Sjoerd Mullender | a9c3c22 | 1993-10-11 12:54:31 +0000 | [diff] [blame] | 51 | #ifdef COUNT_ALLOCS |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 52 | static PyTypeObject *type_list; |
Sjoerd Mullender | 842d2cc | 1993-10-15 16:18:48 +0000 | [diff] [blame] | 53 | extern int tuple_zero_allocs, fast_tuple_allocs; |
| 54 | extern int quick_int_allocs, quick_neg_int_allocs; |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 55 | extern int null_strings, one_strings; |
Sjoerd Mullender | a9c3c22 | 1993-10-11 12:54:31 +0000 | [diff] [blame] | 56 | void |
| 57 | dump_counts() |
| 58 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 59 | PyTypeObject *tp; |
Sjoerd Mullender | a9c3c22 | 1993-10-11 12:54:31 +0000 | [diff] [blame] | 60 | |
| 61 | for (tp = type_list; tp; tp = tp->tp_next) |
Sjoerd Mullender | 52c1f51 | 1993-10-25 08:40:52 +0000 | [diff] [blame] | 62 | fprintf(stderr, "%s alloc'd: %d, freed: %d, max in use: %d\n", |
| 63 | tp->tp_name, tp->tp_alloc, tp->tp_free, |
| 64 | tp->tp_maxalloc); |
| 65 | fprintf(stderr, "fast tuple allocs: %d, empty: %d\n", |
| 66 | fast_tuple_allocs, tuple_zero_allocs); |
| 67 | fprintf(stderr, "fast int allocs: pos: %d, neg: %d\n", |
| 68 | quick_int_allocs, quick_neg_int_allocs); |
| 69 | fprintf(stderr, "null strings: %d, 1-strings: %d\n", |
| 70 | null_strings, one_strings); |
Sjoerd Mullender | a9c3c22 | 1993-10-11 12:54:31 +0000 | [diff] [blame] | 71 | } |
| 72 | |
Sjoerd Mullender | 6ec3c65 | 1995-08-29 09:18:14 +0000 | [diff] [blame] | 73 | PyObject * |
| 74 | get_counts() |
| 75 | { |
| 76 | PyTypeObject *tp; |
| 77 | PyObject *result; |
| 78 | PyObject *v; |
| 79 | |
| 80 | result = PyList_New(0); |
| 81 | if (result == NULL) |
| 82 | return NULL; |
| 83 | for (tp = type_list; tp; tp = tp->tp_next) { |
| 84 | v = Py_BuildValue("(siii)", tp->tp_name, tp->tp_alloc, |
| 85 | tp->tp_free, tp->tp_maxalloc); |
| 86 | if (v == NULL) { |
| 87 | Py_DECREF(result); |
| 88 | return NULL; |
| 89 | } |
| 90 | if (PyList_Append(result, v) < 0) { |
| 91 | Py_DECREF(v); |
| 92 | Py_DECREF(result); |
| 93 | return NULL; |
| 94 | } |
| 95 | Py_DECREF(v); |
| 96 | } |
| 97 | return result; |
| 98 | } |
| 99 | |
Sjoerd Mullender | a9c3c22 | 1993-10-11 12:54:31 +0000 | [diff] [blame] | 100 | void |
| 101 | inc_count(tp) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 102 | PyTypeObject *tp; |
Sjoerd Mullender | a9c3c22 | 1993-10-11 12:54:31 +0000 | [diff] [blame] | 103 | { |
| 104 | if (tp->tp_alloc == 0) { |
Guido van Rossum | d8953cb | 1995-04-06 14:46:26 +0000 | [diff] [blame] | 105 | /* first time; insert in linked list */ |
Sjoerd Mullender | a9c3c22 | 1993-10-11 12:54:31 +0000 | [diff] [blame] | 106 | if (tp->tp_next != NULL) /* sanity check */ |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 107 | Py_FatalError("XXX inc_count sanity check"); |
Sjoerd Mullender | a9c3c22 | 1993-10-11 12:54:31 +0000 | [diff] [blame] | 108 | tp->tp_next = type_list; |
| 109 | type_list = tp; |
| 110 | } |
| 111 | tp->tp_alloc++; |
| 112 | if (tp->tp_alloc - tp->tp_free > tp->tp_maxalloc) |
| 113 | tp->tp_maxalloc = tp->tp_alloc - tp->tp_free; |
| 114 | } |
| 115 | #endif |
| 116 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 117 | PyObject * |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 118 | PyObject_Init(op, tp) |
Guido van Rossum | f5030ab | 1996-07-21 02:30:39 +0000 | [diff] [blame] | 119 | PyObject *op; |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 120 | PyTypeObject *tp; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 121 | { |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 122 | if (op == NULL) { |
| 123 | PyErr_SetString(PyExc_SystemError, |
| 124 | "NULL object passed to PyObject_Init"); |
| 125 | return op; |
| 126 | } |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 127 | #ifdef WITH_CYCLE_GC |
| 128 | if (PyType_IS_GC(tp)) |
| 129 | op = (PyObject *) PyObject_FROM_GC(op); |
| 130 | #endif |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 131 | /* Any changes should be reflected in PyObject_INIT (objimpl.h) */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 132 | op->ob_type = tp; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 133 | _Py_NewReference(op); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 134 | return op; |
| 135 | } |
| 136 | |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 137 | PyVarObject * |
| 138 | PyObject_InitVar(op, tp, size) |
| 139 | PyVarObject *op; |
| 140 | PyTypeObject *tp; |
| 141 | int size; |
| 142 | { |
| 143 | if (op == NULL) { |
| 144 | PyErr_SetString(PyExc_SystemError, |
| 145 | "NULL object passed to PyObject_InitVar"); |
| 146 | return op; |
| 147 | } |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 148 | #ifdef WITH_CYCLE_GC |
| 149 | if (PyType_IS_GC(tp)) |
| 150 | op = (PyVarObject *) PyObject_FROM_GC(op); |
| 151 | #endif |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 152 | /* Any changes should be reflected in PyObject_INIT_VAR */ |
| 153 | op->ob_size = size; |
| 154 | op->ob_type = tp; |
| 155 | _Py_NewReference((PyObject *)op); |
| 156 | return op; |
| 157 | } |
| 158 | |
| 159 | PyObject * |
| 160 | _PyObject_New(tp) |
| 161 | PyTypeObject *tp; |
| 162 | { |
| 163 | PyObject *op; |
| 164 | op = (PyObject *) PyObject_MALLOC(_PyObject_SIZE(tp)); |
| 165 | if (op == NULL) |
| 166 | return PyErr_NoMemory(); |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 167 | #ifdef WITH_CYCLE_GC |
| 168 | if (PyType_IS_GC(tp)) |
| 169 | op = (PyObject *) PyObject_FROM_GC(op); |
| 170 | #endif |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 171 | return PyObject_INIT(op, tp); |
| 172 | } |
| 173 | |
Guido van Rossum | d0c87ee | 1997-05-15 21:31:03 +0000 | [diff] [blame] | 174 | PyVarObject * |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 175 | _PyObject_NewVar(tp, size) |
| 176 | PyTypeObject *tp; |
Guido van Rossum | 2497ead | 1995-02-10 17:00:27 +0000 | [diff] [blame] | 177 | int size; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 178 | { |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 179 | PyVarObject *op; |
| 180 | op = (PyVarObject *) PyObject_MALLOC(_PyObject_VAR_SIZE(tp, size)); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 181 | if (op == NULL) |
Guido van Rossum | d0c87ee | 1997-05-15 21:31:03 +0000 | [diff] [blame] | 182 | return (PyVarObject *)PyErr_NoMemory(); |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 183 | #ifdef WITH_CYCLE_GC |
| 184 | if (PyType_IS_GC(tp)) |
| 185 | op = (PyVarObject *) PyObject_FROM_GC(op); |
| 186 | #endif |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 187 | return PyObject_INIT_VAR(op, tp, size); |
| 188 | } |
| 189 | |
| 190 | void |
| 191 | _PyObject_Del(op) |
| 192 | PyObject *op; |
| 193 | { |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 194 | #ifdef WITH_CYCLE_GC |
| 195 | if (PyType_IS_GC(op->ob_type)) { |
| 196 | PyGC_Head *g = PyObject_AS_GC(op); |
| 197 | PyObject_FREE(g); |
| 198 | } else |
| 199 | #endif |
| 200 | { |
| 201 | PyObject_FREE(op); |
| 202 | } |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 203 | } |
| 204 | |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 205 | #ifndef WITH_CYCLE_GC |
| 206 | /* extension modules might need these */ |
| 207 | void _PyGC_Insert(PyObject *op) { } |
| 208 | void _PyGC_Remove(PyObject *op) { } |
| 209 | #endif |
| 210 | |
Guido van Rossum | 9093361 | 1991-06-07 16:10:43 +0000 | [diff] [blame] | 211 | int |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 212 | PyObject_Print(op, fp, flags) |
| 213 | PyObject *op; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 214 | FILE *fp; |
| 215 | int flags; |
| 216 | { |
Guido van Rossum | 278ef59 | 1991-07-27 21:40:24 +0000 | [diff] [blame] | 217 | int ret = 0; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 218 | if (PyErr_CheckSignals()) |
Guido van Rossum | 9093361 | 1991-06-07 16:10:43 +0000 | [diff] [blame] | 219 | return -1; |
Guido van Rossum | 9b00dfa | 1998-04-28 16:06:54 +0000 | [diff] [blame] | 220 | #ifdef USE_STACKCHECK |
| 221 | if (PyOS_CheckStack()) { |
| 222 | PyErr_SetString(PyExc_MemoryError, "Stack overflow"); |
| 223 | return -1; |
| 224 | } |
| 225 | #endif |
Guido van Rossum | 687ef6e | 2000-01-12 16:28:58 +0000 | [diff] [blame] | 226 | clearerr(fp); /* Clear any previous error condition */ |
Guido van Rossum | 9093361 | 1991-06-07 16:10:43 +0000 | [diff] [blame] | 227 | if (op == NULL) { |
| 228 | fprintf(fp, "<nil>"); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 229 | } |
Guido van Rossum | 9093361 | 1991-06-07 16:10:43 +0000 | [diff] [blame] | 230 | else { |
| 231 | if (op->ob_refcnt <= 0) |
Fred Drake | a44d353 | 2000-06-30 15:01:00 +0000 | [diff] [blame] | 232 | fprintf(fp, "<refcnt %u at %p>", |
| 233 | op->ob_refcnt, op); |
Guido van Rossum | 2e8f614 | 1992-09-03 20:32:55 +0000 | [diff] [blame] | 234 | else if (op->ob_type->tp_print == NULL) { |
| 235 | if (op->ob_type->tp_repr == NULL) { |
Fred Drake | a44d353 | 2000-06-30 15:01:00 +0000 | [diff] [blame] | 236 | fprintf(fp, "<%s object at %p>", |
| 237 | op->ob_type->tp_name, op); |
Guido van Rossum | 2e8f614 | 1992-09-03 20:32:55 +0000 | [diff] [blame] | 238 | } |
| 239 | else { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 240 | PyObject *s; |
| 241 | if (flags & Py_PRINT_RAW) |
| 242 | s = PyObject_Str(op); |
Guido van Rossum | c600411 | 1993-11-05 10:22:19 +0000 | [diff] [blame] | 243 | else |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 244 | s = PyObject_Repr(op); |
Guido van Rossum | 2e8f614 | 1992-09-03 20:32:55 +0000 | [diff] [blame] | 245 | if (s == NULL) |
| 246 | ret = -1; |
Guido van Rossum | 2e8f614 | 1992-09-03 20:32:55 +0000 | [diff] [blame] | 247 | else { |
Guido van Rossum | 565798d | 1998-04-21 22:25:01 +0000 | [diff] [blame] | 248 | ret = PyObject_Print(s, fp, |
| 249 | Py_PRINT_RAW); |
Guido van Rossum | 2e8f614 | 1992-09-03 20:32:55 +0000 | [diff] [blame] | 250 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 251 | Py_XDECREF(s); |
Guido van Rossum | 2e8f614 | 1992-09-03 20:32:55 +0000 | [diff] [blame] | 252 | } |
| 253 | } |
Guido van Rossum | 9093361 | 1991-06-07 16:10:43 +0000 | [diff] [blame] | 254 | else |
Guido van Rossum | 278ef59 | 1991-07-27 21:40:24 +0000 | [diff] [blame] | 255 | ret = (*op->ob_type->tp_print)(op, fp, flags); |
Guido van Rossum | 9093361 | 1991-06-07 16:10:43 +0000 | [diff] [blame] | 256 | } |
Guido van Rossum | 278ef59 | 1991-07-27 21:40:24 +0000 | [diff] [blame] | 257 | if (ret == 0) { |
| 258 | if (ferror(fp)) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 259 | PyErr_SetFromErrno(PyExc_IOError); |
Guido van Rossum | 278ef59 | 1991-07-27 21:40:24 +0000 | [diff] [blame] | 260 | clearerr(fp); |
| 261 | ret = -1; |
| 262 | } |
| 263 | } |
| 264 | return ret; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 265 | } |
| 266 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 267 | PyObject * |
| 268 | PyObject_Repr(v) |
| 269 | PyObject *v; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 270 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 271 | if (PyErr_CheckSignals()) |
Guido van Rossum | 9093361 | 1991-06-07 16:10:43 +0000 | [diff] [blame] | 272 | return NULL; |
Guido van Rossum | 9b00dfa | 1998-04-28 16:06:54 +0000 | [diff] [blame] | 273 | #ifdef USE_STACKCHECK |
| 274 | if (PyOS_CheckStack()) { |
| 275 | PyErr_SetString(PyExc_MemoryError, "Stack overflow"); |
| 276 | return NULL; |
| 277 | } |
| 278 | #endif |
Guido van Rossum | 9093361 | 1991-06-07 16:10:43 +0000 | [diff] [blame] | 279 | if (v == NULL) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 280 | return PyString_FromString("<NULL>"); |
Guido van Rossum | 9093361 | 1991-06-07 16:10:43 +0000 | [diff] [blame] | 281 | else if (v->ob_type->tp_repr == NULL) { |
| 282 | char buf[120]; |
Fred Drake | a44d353 | 2000-06-30 15:01:00 +0000 | [diff] [blame] | 283 | sprintf(buf, "<%.80s object at %p>", |
| 284 | v->ob_type->tp_name, v); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 285 | return PyString_FromString(buf); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 286 | } |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 287 | else { |
| 288 | PyObject *res; |
| 289 | res = (*v->ob_type->tp_repr)(v); |
| 290 | if (res == NULL) |
| 291 | return NULL; |
| 292 | if (!PyString_Check(res)) { |
| 293 | PyErr_Format(PyExc_TypeError, |
Guido van Rossum | 5db862d | 2000-04-10 12:46:51 +0000 | [diff] [blame] | 294 | "__repr__ returned non-string (type %.200s)", |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 295 | res->ob_type->tp_name); |
| 296 | Py_DECREF(res); |
| 297 | return NULL; |
| 298 | } |
| 299 | return res; |
| 300 | } |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 301 | } |
| 302 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 303 | PyObject * |
| 304 | PyObject_Str(v) |
| 305 | PyObject *v; |
Guido van Rossum | c600411 | 1993-11-05 10:22:19 +0000 | [diff] [blame] | 306 | { |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 307 | PyObject *res; |
| 308 | |
Guido van Rossum | c600411 | 1993-11-05 10:22:19 +0000 | [diff] [blame] | 309 | if (v == NULL) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 310 | return PyString_FromString("<NULL>"); |
| 311 | else if (PyString_Check(v)) { |
| 312 | Py_INCREF(v); |
Guido van Rossum | c600411 | 1993-11-05 10:22:19 +0000 | [diff] [blame] | 313 | return v; |
| 314 | } |
Guido van Rossum | 32b582b | 1995-01-17 16:35:13 +0000 | [diff] [blame] | 315 | else if (v->ob_type->tp_str != NULL) |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 316 | res = (*v->ob_type->tp_str)(v); |
Guido van Rossum | c600411 | 1993-11-05 10:22:19 +0000 | [diff] [blame] | 317 | else { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 318 | PyObject *func; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 319 | if (!PyInstance_Check(v) || |
| 320 | (func = PyObject_GetAttrString(v, "__str__")) == NULL) { |
| 321 | PyErr_Clear(); |
| 322 | return PyObject_Repr(v); |
Guido van Rossum | c600411 | 1993-11-05 10:22:19 +0000 | [diff] [blame] | 323 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 324 | res = PyEval_CallObject(func, (PyObject *)NULL); |
| 325 | Py_DECREF(func); |
Guido van Rossum | c600411 | 1993-11-05 10:22:19 +0000 | [diff] [blame] | 326 | } |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 327 | if (res == NULL) |
| 328 | return NULL; |
| 329 | if (!PyString_Check(res)) { |
| 330 | PyErr_Format(PyExc_TypeError, |
Guido van Rossum | 5db862d | 2000-04-10 12:46:51 +0000 | [diff] [blame] | 331 | "__str__ returned non-string (type %.200s)", |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 332 | res->ob_type->tp_name); |
| 333 | Py_DECREF(res); |
| 334 | return NULL; |
| 335 | } |
| 336 | return res; |
Guido van Rossum | c600411 | 1993-11-05 10:22:19 +0000 | [diff] [blame] | 337 | } |
| 338 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 339 | static PyObject * |
Guido van Rossum | 2056684 | 1995-01-12 11:26:10 +0000 | [diff] [blame] | 340 | do_cmp(v, w) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 341 | PyObject *v, *w; |
Guido van Rossum | 2056684 | 1995-01-12 11:26:10 +0000 | [diff] [blame] | 342 | { |
Guido van Rossum | c8b6df9 | 1997-05-23 00:06:51 +0000 | [diff] [blame] | 343 | long c; |
Guido van Rossum | 2056684 | 1995-01-12 11:26:10 +0000 | [diff] [blame] | 344 | /* __rcmp__ actually won't be called unless __cmp__ isn't defined, |
| 345 | because the check in cmpobject() reverses the objects first. |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 346 | This is intentional -- it makes no sense to define cmp(x,y) |
| 347 | different than -cmp(y,x). */ |
| 348 | if (PyInstance_Check(v) || PyInstance_Check(w)) |
| 349 | return PyInstance_DoBinOp(v, w, "__cmp__", "__rcmp__", do_cmp); |
Guido van Rossum | c8b6df9 | 1997-05-23 00:06:51 +0000 | [diff] [blame] | 350 | c = PyObject_Compare(v, w); |
| 351 | if (c && PyErr_Occurred()) |
| 352 | return NULL; |
| 353 | return PyInt_FromLong(c); |
Guido van Rossum | 2056684 | 1995-01-12 11:26:10 +0000 | [diff] [blame] | 354 | } |
| 355 | |
Jeremy Hylton | 4a3dd2d | 2000-04-14 19:13:24 +0000 | [diff] [blame] | 356 | PyObject *_PyCompareState_Key; |
| 357 | |
| 358 | /* _PyCompareState_nesting is incremented beforing call compare (for |
| 359 | some types) and decremented on exit. If the count exceeds the |
| 360 | nesting limit, enable code to detect circular data structures. |
| 361 | */ |
| 362 | #define NESTING_LIMIT 500 |
| 363 | int _PyCompareState_nesting = 0; |
| 364 | |
| 365 | static PyObject* |
| 366 | get_inprogress_dict() |
| 367 | { |
| 368 | PyObject *tstate_dict, *inprogress; |
| 369 | |
| 370 | tstate_dict = PyThreadState_GetDict(); |
| 371 | if (tstate_dict == NULL) { |
| 372 | PyErr_BadInternalCall(); |
| 373 | return NULL; |
| 374 | } |
| 375 | inprogress = PyDict_GetItem(tstate_dict, _PyCompareState_Key); |
| 376 | if (inprogress == NULL) { |
| 377 | PyErr_Clear(); |
| 378 | inprogress = PyDict_New(); |
| 379 | if (inprogress == NULL) |
| 380 | return NULL; |
| 381 | if (PyDict_SetItem(tstate_dict, _PyCompareState_Key, |
| 382 | inprogress) == -1) { |
| 383 | Py_DECREF(inprogress); |
| 384 | return NULL; |
| 385 | } |
Jeremy Hylton | a251ea0 | 2000-06-09 16:20:39 +0000 | [diff] [blame] | 386 | Py_DECREF(inprogress); |
Jeremy Hylton | 4a3dd2d | 2000-04-14 19:13:24 +0000 | [diff] [blame] | 387 | } |
| 388 | return inprogress; |
| 389 | } |
| 390 | |
| 391 | static PyObject * |
| 392 | make_pair(v, w) |
| 393 | PyObject *v, *w; |
| 394 | { |
| 395 | PyObject *pair; |
| 396 | |
| 397 | pair = PyTuple_New(2); |
| 398 | if (pair == NULL) { |
| 399 | return NULL; |
| 400 | } |
Guido van Rossum | ad89bbc | 2000-06-28 21:57:18 +0000 | [diff] [blame] | 401 | if (v <= w) { |
Jeremy Hylton | 4a3dd2d | 2000-04-14 19:13:24 +0000 | [diff] [blame] | 402 | PyTuple_SET_ITEM(pair, 0, PyLong_FromVoidPtr((void *)v)); |
| 403 | PyTuple_SET_ITEM(pair, 1, PyLong_FromVoidPtr((void *)w)); |
| 404 | } else { |
| 405 | PyTuple_SET_ITEM(pair, 0, PyLong_FromVoidPtr((void *)w)); |
| 406 | PyTuple_SET_ITEM(pair, 1, PyLong_FromVoidPtr((void *)v)); |
| 407 | } |
| 408 | return pair; |
| 409 | } |
| 410 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 411 | int |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 412 | PyObject_Compare(v, w) |
| 413 | PyObject *v, *w; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 414 | { |
Guido van Rossum | cd5a5f6 | 1998-06-09 18:58:44 +0000 | [diff] [blame] | 415 | PyTypeObject *vtp, *wtp; |
Jeremy Hylton | 4a3dd2d | 2000-04-14 19:13:24 +0000 | [diff] [blame] | 416 | int result; |
| 417 | |
Guido van Rossum | c8b6df9 | 1997-05-23 00:06:51 +0000 | [diff] [blame] | 418 | if (v == NULL || w == NULL) { |
| 419 | PyErr_BadInternalCall(); |
| 420 | return -1; |
| 421 | } |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 422 | if (v == w) |
| 423 | return 0; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 424 | if (PyInstance_Check(v) || PyInstance_Check(w)) { |
| 425 | PyObject *res; |
Guido van Rossum | 2056684 | 1995-01-12 11:26:10 +0000 | [diff] [blame] | 426 | int c; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 427 | if (!PyInstance_Check(v)) |
| 428 | return -PyObject_Compare(w, v); |
Jeremy Hylton | 4a3dd2d | 2000-04-14 19:13:24 +0000 | [diff] [blame] | 429 | if (++_PyCompareState_nesting > NESTING_LIMIT) { |
| 430 | PyObject *inprogress, *pair; |
| 431 | |
| 432 | inprogress = get_inprogress_dict(); |
| 433 | if (inprogress == NULL) { |
| 434 | return -1; |
| 435 | } |
| 436 | pair = make_pair(v, w); |
| 437 | if (PyDict_GetItem(inprogress, pair)) { |
| 438 | /* already comparing these objects. assume |
| 439 | they're equal until shown otherwise */ |
| 440 | Py_DECREF(pair); |
| 441 | --_PyCompareState_nesting; |
| 442 | return 0; |
| 443 | } |
| 444 | if (PyDict_SetItem(inprogress, pair, pair) == -1) { |
| 445 | return -1; |
| 446 | } |
| 447 | res = do_cmp(v, w); |
| 448 | _PyCompareState_nesting--; |
| 449 | /* XXX DelItem shouldn't fail */ |
| 450 | PyDict_DelItem(inprogress, pair); |
| 451 | Py_DECREF(pair); |
| 452 | } else { |
| 453 | res = do_cmp(v, w); |
| 454 | } |
Guido van Rossum | c8b6df9 | 1997-05-23 00:06:51 +0000 | [diff] [blame] | 455 | if (res == NULL) |
| 456 | return -1; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 457 | if (!PyInt_Check(res)) { |
| 458 | Py_DECREF(res); |
Guido van Rossum | c8b6df9 | 1997-05-23 00:06:51 +0000 | [diff] [blame] | 459 | PyErr_SetString(PyExc_TypeError, |
| 460 | "comparison did not return an int"); |
| 461 | return -1; |
Guido van Rossum | 2056684 | 1995-01-12 11:26:10 +0000 | [diff] [blame] | 462 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 463 | c = PyInt_AsLong(res); |
| 464 | Py_DECREF(res); |
Guido van Rossum | 2056684 | 1995-01-12 11:26:10 +0000 | [diff] [blame] | 465 | return (c < 0) ? -1 : (c > 0) ? 1 : 0; |
| 466 | } |
Guido van Rossum | cd5a5f6 | 1998-06-09 18:58:44 +0000 | [diff] [blame] | 467 | if ((vtp = v->ob_type) != (wtp = w->ob_type)) { |
| 468 | char *vname = vtp->tp_name; |
| 469 | char *wname = wtp->tp_name; |
Guido van Rossum | b4db194 | 1998-07-21 21:56:41 +0000 | [diff] [blame] | 470 | if (vtp->tp_as_number != NULL && wtp->tp_as_number != NULL) { |
Guido van Rossum | 242c642 | 1997-11-19 16:03:17 +0000 | [diff] [blame] | 471 | int err; |
| 472 | err = PyNumber_CoerceEx(&v, &w); |
| 473 | if (err < 0) |
Guido van Rossum | c8b6df9 | 1997-05-23 00:06:51 +0000 | [diff] [blame] | 474 | return -1; |
Guido van Rossum | 242c642 | 1997-11-19 16:03:17 +0000 | [diff] [blame] | 475 | else if (err == 0) { |
Guido van Rossum | b4db194 | 1998-07-21 21:56:41 +0000 | [diff] [blame] | 476 | int cmp; |
| 477 | vtp = v->ob_type; |
| 478 | if (vtp->tp_compare == NULL) |
| 479 | cmp = (v < w) ? -1 : 1; |
| 480 | else |
| 481 | cmp = (*vtp->tp_compare)(v, w); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 482 | Py_DECREF(v); |
| 483 | Py_DECREF(w); |
Guido van Rossum | 9fb0368 | 1991-07-01 18:48:04 +0000 | [diff] [blame] | 484 | return cmp; |
| 485 | } |
| 486 | } |
Guido van Rossum | b244f69 | 2000-04-10 13:42:33 +0000 | [diff] [blame] | 487 | else if (PyUnicode_Check(v) || PyUnicode_Check(w)) { |
| 488 | int result = PyUnicode_Compare(v, w); |
| 489 | if (result == -1 && PyErr_Occurred() && |
| 490 | PyErr_ExceptionMatches(PyExc_TypeError)) |
| 491 | /* TypeErrors are ignored: if Unicode coercion |
| 492 | fails due to one of the arguments not |
| 493 | having the right type, we continue as |
| 494 | defined by the coercion protocol (see |
| 495 | above). Luckily, decoding errors are |
| 496 | reported as ValueErrors and are not masked |
| 497 | by this technique. */ |
| 498 | PyErr_Clear(); |
| 499 | else |
| 500 | return result; |
| 501 | } |
Guido van Rossum | cd5a5f6 | 1998-06-09 18:58:44 +0000 | [diff] [blame] | 502 | else if (vtp->tp_as_number != NULL) |
| 503 | vname = ""; |
| 504 | else if (wtp->tp_as_number != NULL) |
| 505 | wname = ""; |
| 506 | /* Numerical types compare smaller than all other types */ |
| 507 | return strcmp(vname, wname); |
Guido van Rossum | 9fb0368 | 1991-07-01 18:48:04 +0000 | [diff] [blame] | 508 | } |
Jeremy Hylton | 4a3dd2d | 2000-04-14 19:13:24 +0000 | [diff] [blame] | 509 | if (vtp->tp_compare == NULL) { |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 510 | return (v < w) ? -1 : 1; |
Jeremy Hylton | 4a3dd2d | 2000-04-14 19:13:24 +0000 | [diff] [blame] | 511 | } |
| 512 | if (++_PyCompareState_nesting > NESTING_LIMIT |
| 513 | && (vtp->tp_as_mapping |
| 514 | || (vtp->tp_as_sequence && !PyString_Check(v)))) { |
| 515 | PyObject *inprogress, *pair; |
| 516 | |
| 517 | inprogress = get_inprogress_dict(); |
| 518 | if (inprogress == NULL) { |
| 519 | return -1; |
| 520 | } |
| 521 | pair = make_pair(v, w); |
| 522 | if (PyDict_GetItem(inprogress, pair)) { |
| 523 | /* already comparing these objects. assume |
| 524 | they're equal until shown otherwise */ |
| 525 | _PyCompareState_nesting--; |
| 526 | Py_DECREF(pair); |
| 527 | return 0; |
| 528 | } |
| 529 | if (PyDict_SetItem(inprogress, pair, pair) == -1) { |
| 530 | return -1; |
| 531 | } |
| 532 | result = (*vtp->tp_compare)(v, w); |
| 533 | _PyCompareState_nesting--; |
| 534 | PyDict_DelItem(inprogress, pair); /* XXX shouldn't fail */ |
| 535 | Py_DECREF(pair); |
| 536 | } else { |
| 537 | result = (*vtp->tp_compare)(v, w); |
| 538 | } |
| 539 | return result; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 540 | } |
| 541 | |
Fred Drake | 13634cf | 2000-06-29 19:17:04 +0000 | [diff] [blame] | 542 | |
| 543 | /* Set of hash utility functions to help maintaining the invariant that |
| 544 | iff a==b then hash(a)==hash(b) |
| 545 | |
| 546 | All the utility functions (_Py_Hash*()) return "-1" to signify an error. |
| 547 | */ |
| 548 | |
| 549 | long |
| 550 | _Py_HashDouble(v) |
| 551 | double v; |
| 552 | { |
| 553 | /* Use frexp to get at the bits in the double. |
| 554 | * Since the VAX D double format has 56 mantissa bits, which is the |
| 555 | * most of any double format in use, each of these parts may have as |
| 556 | * many as (but no more than) 56 significant bits. |
| 557 | * So, assuming sizeof(long) >= 4, each part can be broken into two longs; |
| 558 | * frexp and multiplication are used to do that. |
| 559 | * Also, since the Cray double format has 15 exponent bits, which is the |
| 560 | * most of any double format in use, shifting the exponent field left by |
| 561 | * 15 won't overflow a long (again assuming sizeof(long) >= 4). |
| 562 | */ |
| 563 | int expo; |
| 564 | long hipart; |
| 565 | |
| 566 | v = frexp(v, &expo); |
| 567 | v = v * 2147483648.0; /* 2**31 */ |
| 568 | hipart = (long)v; /* Take the top 32 bits */ |
| 569 | v = (v - (double)hipart) * 2147483648.0; /* Get the next 32 bits */ |
| 570 | |
| 571 | return hipart + (long)v + (expo << 15); /* Combine everything */ |
| 572 | } |
| 573 | |
| 574 | long |
| 575 | _Py_HashPointer(p) |
| 576 | void *p; |
| 577 | { |
| 578 | #if SIZEOF_LONG >= SIZEOF_VOID_P |
| 579 | return (long)p; |
| 580 | #else |
| 581 | /* convert to a Python long and hash that */ |
| 582 | PyObject* longobj; |
| 583 | long x; |
| 584 | |
| 585 | if ((longobj = PyLong_FromVoidPtr(p)) == NULL) { |
| 586 | x = -1; |
| 587 | goto finally; |
| 588 | } |
| 589 | x = PyObject_Hash(longobj); |
| 590 | |
| 591 | finally: |
| 592 | Py_XDECREF(longobj); |
| 593 | return x; |
| 594 | #endif |
| 595 | } |
| 596 | |
| 597 | |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 598 | long |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 599 | PyObject_Hash(v) |
| 600 | PyObject *v; |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 601 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 602 | PyTypeObject *tp = v->ob_type; |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 603 | if (tp->tp_hash != NULL) |
| 604 | return (*tp->tp_hash)(v); |
Fred Drake | 13634cf | 2000-06-29 19:17:04 +0000 | [diff] [blame] | 605 | if (tp->tp_compare == NULL) { |
| 606 | return _Py_HashPointer(v); /* Use address as hash value */ |
| 607 | } |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 608 | /* 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] | 609 | PyErr_SetString(PyExc_TypeError, "unhashable type"); |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 610 | return -1; |
| 611 | } |
| 612 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 613 | PyObject * |
| 614 | PyObject_GetAttrString(v, name) |
| 615 | PyObject *v; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 616 | char *name; |
| 617 | { |
Guido van Rossum | d8eb1b3 | 1996-08-09 20:52:03 +0000 | [diff] [blame] | 618 | if (v->ob_type->tp_getattro != NULL) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 619 | PyObject *w, *res; |
Guido van Rossum | c6d0670 | 1997-01-18 07:57:16 +0000 | [diff] [blame] | 620 | w = PyString_InternFromString(name); |
Guido van Rossum | d8eb1b3 | 1996-08-09 20:52:03 +0000 | [diff] [blame] | 621 | if (w == NULL) |
| 622 | return NULL; |
| 623 | res = (*v->ob_type->tp_getattro)(v, w); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 624 | Py_XDECREF(w); |
Guido van Rossum | d8eb1b3 | 1996-08-09 20:52:03 +0000 | [diff] [blame] | 625 | return res; |
| 626 | } |
| 627 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 628 | if (v->ob_type->tp_getattr == NULL) { |
Guido van Rossum | db93516 | 1998-01-19 22:16:36 +0000 | [diff] [blame] | 629 | PyErr_Format(PyExc_AttributeError, |
| 630 | "'%.50s' object has no attribute '%.400s'", |
| 631 | v->ob_type->tp_name, |
| 632 | name); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 633 | return NULL; |
| 634 | } |
| 635 | else { |
| 636 | return (*v->ob_type->tp_getattr)(v, name); |
| 637 | } |
| 638 | } |
| 639 | |
| 640 | int |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 641 | PyObject_HasAttrString(v, name) |
| 642 | PyObject *v; |
Guido van Rossum | ed18fdc | 1993-07-11 19:55:34 +0000 | [diff] [blame] | 643 | char *name; |
| 644 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 645 | PyObject *res = PyObject_GetAttrString(v, name); |
Guido van Rossum | ed18fdc | 1993-07-11 19:55:34 +0000 | [diff] [blame] | 646 | if (res != NULL) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 647 | Py_DECREF(res); |
Guido van Rossum | ed18fdc | 1993-07-11 19:55:34 +0000 | [diff] [blame] | 648 | return 1; |
| 649 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 650 | PyErr_Clear(); |
Guido van Rossum | ed18fdc | 1993-07-11 19:55:34 +0000 | [diff] [blame] | 651 | return 0; |
| 652 | } |
| 653 | |
| 654 | int |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 655 | PyObject_SetAttrString(v, name, w) |
| 656 | PyObject *v; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 657 | char *name; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 658 | PyObject *w; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 659 | { |
Guido van Rossum | d8eb1b3 | 1996-08-09 20:52:03 +0000 | [diff] [blame] | 660 | if (v->ob_type->tp_setattro != NULL) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 661 | PyObject *s; |
Guido van Rossum | d8eb1b3 | 1996-08-09 20:52:03 +0000 | [diff] [blame] | 662 | int res; |
Guido van Rossum | c6d0670 | 1997-01-18 07:57:16 +0000 | [diff] [blame] | 663 | s = PyString_InternFromString(name); |
Guido van Rossum | d8eb1b3 | 1996-08-09 20:52:03 +0000 | [diff] [blame] | 664 | if (s == NULL) |
Guido van Rossum | b7fc304 | 1996-09-11 22:51:25 +0000 | [diff] [blame] | 665 | return -1; |
Guido van Rossum | d8eb1b3 | 1996-08-09 20:52:03 +0000 | [diff] [blame] | 666 | res = (*v->ob_type->tp_setattro)(v, s, w); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 667 | Py_XDECREF(s); |
Guido van Rossum | d8eb1b3 | 1996-08-09 20:52:03 +0000 | [diff] [blame] | 668 | return res; |
| 669 | } |
| 670 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 671 | if (v->ob_type->tp_setattr == NULL) { |
| 672 | if (v->ob_type->tp_getattr == NULL) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 673 | PyErr_SetString(PyExc_TypeError, |
Guido van Rossum | 3ea7412 | 1991-12-24 13:28:03 +0000 | [diff] [blame] | 674 | "attribute-less object (assign or del)"); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 675 | else |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 676 | PyErr_SetString(PyExc_TypeError, |
Guido van Rossum | 3ea7412 | 1991-12-24 13:28:03 +0000 | [diff] [blame] | 677 | "object has read-only attributes"); |
Guido van Rossum | 73531a3 | 1990-12-20 23:12:40 +0000 | [diff] [blame] | 678 | return -1; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 679 | } |
| 680 | else { |
| 681 | return (*v->ob_type->tp_setattr)(v, name, w); |
| 682 | } |
| 683 | } |
| 684 | |
Guido van Rossum | 98ff96a | 1997-05-20 18:34:44 +0000 | [diff] [blame] | 685 | PyObject * |
| 686 | PyObject_GetAttr(v, name) |
| 687 | PyObject *v; |
| 688 | PyObject *name; |
| 689 | { |
| 690 | if (v->ob_type->tp_getattro != NULL) |
| 691 | return (*v->ob_type->tp_getattro)(v, name); |
Jeremy Hylton | 99a8f90 | 2000-06-23 14:36:32 +0000 | [diff] [blame] | 692 | |
| 693 | if (!PyString_Check(name)) { |
| 694 | PyErr_SetString(PyExc_TypeError, |
| 695 | "attribute name must be string"); |
| 696 | return NULL; |
| 697 | } |
| 698 | return PyObject_GetAttrString(v, PyString_AS_STRING(name)); |
Guido van Rossum | 98ff96a | 1997-05-20 18:34:44 +0000 | [diff] [blame] | 699 | } |
| 700 | |
| 701 | int |
| 702 | PyObject_HasAttr(v, name) |
| 703 | PyObject *v; |
| 704 | PyObject *name; |
| 705 | { |
| 706 | PyObject *res = PyObject_GetAttr(v, name); |
| 707 | if (res != NULL) { |
| 708 | Py_DECREF(res); |
| 709 | return 1; |
| 710 | } |
| 711 | PyErr_Clear(); |
| 712 | return 0; |
| 713 | } |
| 714 | |
| 715 | int |
| 716 | PyObject_SetAttr(v, name, value) |
| 717 | PyObject *v; |
| 718 | PyObject *name; |
| 719 | PyObject *value; |
| 720 | { |
| 721 | int err; |
| 722 | Py_INCREF(name); |
Jeremy Hylton | 99a8f90 | 2000-06-23 14:36:32 +0000 | [diff] [blame] | 723 | if (PyString_Check(name)) |
| 724 | PyString_InternInPlace(&name); |
Guido van Rossum | 98ff96a | 1997-05-20 18:34:44 +0000 | [diff] [blame] | 725 | if (v->ob_type->tp_setattro != NULL) |
| 726 | err = (*v->ob_type->tp_setattro)(v, name, value); |
Jeremy Hylton | 99a8f90 | 2000-06-23 14:36:32 +0000 | [diff] [blame] | 727 | else if (PyString_Check(name)) { |
Guido van Rossum | 98ff96a | 1997-05-20 18:34:44 +0000 | [diff] [blame] | 728 | err = PyObject_SetAttrString( |
Jeremy Hylton | 99a8f90 | 2000-06-23 14:36:32 +0000 | [diff] [blame] | 729 | v, PyString_AS_STRING(name), value); |
| 730 | } |
| 731 | else { |
| 732 | PyErr_SetString(PyExc_TypeError, |
| 733 | "attribute name must be string"); |
| 734 | err = -1; |
| 735 | } |
Guido van Rossum | 98ff96a | 1997-05-20 18:34:44 +0000 | [diff] [blame] | 736 | Py_DECREF(name); |
| 737 | return err; |
| 738 | } |
| 739 | |
Guido van Rossum | 6ac258d | 1993-05-12 08:24:20 +0000 | [diff] [blame] | 740 | /* Test a value used as condition, e.g., in a for or if statement. |
| 741 | Return -1 if an error occurred */ |
| 742 | |
| 743 | int |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 744 | PyObject_IsTrue(v) |
| 745 | PyObject *v; |
Guido van Rossum | 6ac258d | 1993-05-12 08:24:20 +0000 | [diff] [blame] | 746 | { |
| 747 | int res; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 748 | if (v == Py_None) |
Guido van Rossum | 6ac258d | 1993-05-12 08:24:20 +0000 | [diff] [blame] | 749 | res = 0; |
Guido van Rossum | 1c4f458 | 1998-05-22 00:53:24 +0000 | [diff] [blame] | 750 | else if (v->ob_type->tp_as_number != NULL && |
| 751 | v->ob_type->tp_as_number->nb_nonzero != NULL) |
Guido van Rossum | 6ac258d | 1993-05-12 08:24:20 +0000 | [diff] [blame] | 752 | res = (*v->ob_type->tp_as_number->nb_nonzero)(v); |
Guido van Rossum | 1c4f458 | 1998-05-22 00:53:24 +0000 | [diff] [blame] | 753 | else if (v->ob_type->tp_as_mapping != NULL && |
| 754 | v->ob_type->tp_as_mapping->mp_length != NULL) |
Guido van Rossum | 6ac258d | 1993-05-12 08:24:20 +0000 | [diff] [blame] | 755 | res = (*v->ob_type->tp_as_mapping->mp_length)(v); |
Guido van Rossum | 1c4f458 | 1998-05-22 00:53:24 +0000 | [diff] [blame] | 756 | else if (v->ob_type->tp_as_sequence != NULL && |
| 757 | v->ob_type->tp_as_sequence->sq_length != NULL) |
Guido van Rossum | 6ac258d | 1993-05-12 08:24:20 +0000 | [diff] [blame] | 758 | res = (*v->ob_type->tp_as_sequence->sq_length)(v); |
| 759 | else |
| 760 | res = 1; |
| 761 | if (res > 0) |
| 762 | res = 1; |
| 763 | return res; |
| 764 | } |
| 765 | |
Guido van Rossum | c3d3f96 | 1998-04-09 17:53:59 +0000 | [diff] [blame] | 766 | /* equivalent of 'not v' |
| 767 | Return -1 if an error occurred */ |
| 768 | |
| 769 | int |
| 770 | PyObject_Not(v) |
| 771 | PyObject *v; |
| 772 | { |
| 773 | int res; |
| 774 | res = PyObject_IsTrue(v); |
| 775 | if (res < 0) |
| 776 | return res; |
| 777 | return res == 0; |
| 778 | } |
| 779 | |
Guido van Rossum | 5524a59 | 1995-01-10 15:26:20 +0000 | [diff] [blame] | 780 | /* Coerce two numeric types to the "larger" one. |
| 781 | Increment the reference count on each argument. |
| 782 | Return -1 and raise an exception if no coercion is possible |
| 783 | (and then no reference count is incremented). |
| 784 | */ |
| 785 | |
| 786 | int |
Guido van Rossum | 242c642 | 1997-11-19 16:03:17 +0000 | [diff] [blame] | 787 | PyNumber_CoerceEx(pv, pw) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 788 | PyObject **pv, **pw; |
Guido van Rossum | 5524a59 | 1995-01-10 15:26:20 +0000 | [diff] [blame] | 789 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 790 | register PyObject *v = *pv; |
| 791 | register PyObject *w = *pw; |
Guido van Rossum | 5524a59 | 1995-01-10 15:26:20 +0000 | [diff] [blame] | 792 | int res; |
| 793 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 794 | if (v->ob_type == w->ob_type && !PyInstance_Check(v)) { |
| 795 | Py_INCREF(v); |
| 796 | Py_INCREF(w); |
Guido van Rossum | 5524a59 | 1995-01-10 15:26:20 +0000 | [diff] [blame] | 797 | return 0; |
| 798 | } |
| 799 | if (v->ob_type->tp_as_number && v->ob_type->tp_as_number->nb_coerce) { |
| 800 | res = (*v->ob_type->tp_as_number->nb_coerce)(pv, pw); |
| 801 | if (res <= 0) |
| 802 | return res; |
| 803 | } |
| 804 | if (w->ob_type->tp_as_number && w->ob_type->tp_as_number->nb_coerce) { |
| 805 | res = (*w->ob_type->tp_as_number->nb_coerce)(pw, pv); |
| 806 | if (res <= 0) |
| 807 | return res; |
| 808 | } |
Guido van Rossum | 242c642 | 1997-11-19 16:03:17 +0000 | [diff] [blame] | 809 | return 1; |
| 810 | } |
| 811 | |
| 812 | int |
| 813 | PyNumber_Coerce(pv, pw) |
| 814 | PyObject **pv, **pw; |
| 815 | { |
| 816 | int err = PyNumber_CoerceEx(pv, pw); |
| 817 | if (err <= 0) |
| 818 | return err; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 819 | PyErr_SetString(PyExc_TypeError, "number coercion failed"); |
Guido van Rossum | 5524a59 | 1995-01-10 15:26:20 +0000 | [diff] [blame] | 820 | return -1; |
| 821 | } |
| 822 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 823 | |
Guido van Rossum | 49b11fe | 1995-01-26 00:38:22 +0000 | [diff] [blame] | 824 | /* Test whether an object can be called */ |
| 825 | |
| 826 | int |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 827 | PyCallable_Check(x) |
| 828 | PyObject *x; |
Guido van Rossum | 49b11fe | 1995-01-26 00:38:22 +0000 | [diff] [blame] | 829 | { |
| 830 | if (x == NULL) |
| 831 | return 0; |
| 832 | if (x->ob_type->tp_call != NULL || |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 833 | PyFunction_Check(x) || |
| 834 | PyMethod_Check(x) || |
| 835 | PyCFunction_Check(x) || |
| 836 | PyClass_Check(x)) |
Guido van Rossum | 49b11fe | 1995-01-26 00:38:22 +0000 | [diff] [blame] | 837 | return 1; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 838 | if (PyInstance_Check(x)) { |
| 839 | PyObject *call = PyObject_GetAttrString(x, "__call__"); |
Guido van Rossum | 49b11fe | 1995-01-26 00:38:22 +0000 | [diff] [blame] | 840 | if (call == NULL) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 841 | PyErr_Clear(); |
Guido van Rossum | 49b11fe | 1995-01-26 00:38:22 +0000 | [diff] [blame] | 842 | return 0; |
| 843 | } |
| 844 | /* Could test recursively but don't, for fear of endless |
| 845 | recursion if some joker sets self.__call__ = self */ |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 846 | Py_DECREF(call); |
Guido van Rossum | 49b11fe | 1995-01-26 00:38:22 +0000 | [diff] [blame] | 847 | return 1; |
| 848 | } |
| 849 | return 0; |
| 850 | } |
| 851 | |
| 852 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 853 | /* |
| 854 | NoObject is usable as a non-NULL undefined value, used by the macro None. |
| 855 | 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] | 856 | so there is exactly one (which is indestructible, by the way). |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 857 | */ |
| 858 | |
Guido van Rossum | 0c182a1 | 1992-03-27 17:26:13 +0000 | [diff] [blame] | 859 | /* ARGSUSED */ |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 860 | static PyObject * |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 861 | none_repr(op) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 862 | PyObject *op; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 863 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 864 | return PyString_FromString("None"); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 865 | } |
| 866 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 867 | static PyTypeObject PyNothing_Type = { |
| 868 | PyObject_HEAD_INIT(&PyType_Type) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 869 | 0, |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 870 | "None", |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 871 | 0, |
| 872 | 0, |
| 873 | 0, /*tp_dealloc*/ /*never called*/ |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 874 | 0, /*tp_print*/ |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 875 | 0, /*tp_getattr*/ |
| 876 | 0, /*tp_setattr*/ |
| 877 | 0, /*tp_compare*/ |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 878 | (reprfunc)none_repr, /*tp_repr*/ |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 879 | 0, /*tp_as_number*/ |
| 880 | 0, /*tp_as_sequence*/ |
| 881 | 0, /*tp_as_mapping*/ |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 882 | 0, /*tp_hash */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 883 | }; |
| 884 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 885 | PyObject _Py_NoneStruct = { |
| 886 | PyObject_HEAD_INIT(&PyNothing_Type) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 887 | }; |
| 888 | |
| 889 | |
Guido van Rossum | 84a9032 | 1996-05-22 16:34:47 +0000 | [diff] [blame] | 890 | #ifdef Py_TRACE_REFS |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 891 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 892 | static PyObject refchain = {&refchain, &refchain}; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 893 | |
Guido van Rossum | aacdc9d | 1996-08-12 21:32:12 +0000 | [diff] [blame] | 894 | void |
Guido van Rossum | e09fb55 | 1997-08-05 02:04:34 +0000 | [diff] [blame] | 895 | _Py_ResetReferences() |
| 896 | { |
| 897 | refchain._ob_prev = refchain._ob_next = &refchain; |
| 898 | _Py_RefTotal = 0; |
| 899 | } |
| 900 | |
| 901 | void |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 902 | _Py_NewReference(op) |
| 903 | PyObject *op; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 904 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 905 | _Py_RefTotal++; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 906 | op->ob_refcnt = 1; |
| 907 | op->_ob_next = refchain._ob_next; |
| 908 | op->_ob_prev = &refchain; |
| 909 | refchain._ob_next->_ob_prev = op; |
| 910 | refchain._ob_next = op; |
Sjoerd Mullender | a9c3c22 | 1993-10-11 12:54:31 +0000 | [diff] [blame] | 911 | #ifdef COUNT_ALLOCS |
| 912 | inc_count(op->ob_type); |
| 913 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 914 | } |
| 915 | |
Guido van Rossum | aacdc9d | 1996-08-12 21:32:12 +0000 | [diff] [blame] | 916 | void |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 917 | _Py_ForgetReference(op) |
| 918 | register PyObject *op; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 919 | { |
Guido van Rossum | bffd683 | 2000-01-20 22:32:56 +0000 | [diff] [blame] | 920 | #ifdef SLOW_UNREF_CHECK |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 921 | register PyObject *p; |
Guido van Rossum | bffd683 | 2000-01-20 22:32:56 +0000 | [diff] [blame] | 922 | #endif |
Guido van Rossum | d7047b3 | 1995-01-02 19:07:15 +0000 | [diff] [blame] | 923 | if (op->ob_refcnt < 0) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 924 | Py_FatalError("UNREF negative refcnt"); |
Guido van Rossum | 2e8f614 | 1992-09-03 20:32:55 +0000 | [diff] [blame] | 925 | if (op == &refchain || |
Guido van Rossum | d7047b3 | 1995-01-02 19:07:15 +0000 | [diff] [blame] | 926 | 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] | 927 | Py_FatalError("UNREF invalid object"); |
Guido van Rossum | 2e8f614 | 1992-09-03 20:32:55 +0000 | [diff] [blame] | 928 | #ifdef SLOW_UNREF_CHECK |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 929 | for (p = refchain._ob_next; p != &refchain; p = p->_ob_next) { |
| 930 | if (p == op) |
| 931 | break; |
| 932 | } |
Guido van Rossum | d7047b3 | 1995-01-02 19:07:15 +0000 | [diff] [blame] | 933 | if (p == &refchain) /* Not found */ |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 934 | Py_FatalError("UNREF unknown object"); |
Guido van Rossum | 2e8f614 | 1992-09-03 20:32:55 +0000 | [diff] [blame] | 935 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 936 | op->_ob_next->_ob_prev = op->_ob_prev; |
| 937 | op->_ob_prev->_ob_next = op->_ob_next; |
Guido van Rossum | 2e8f614 | 1992-09-03 20:32:55 +0000 | [diff] [blame] | 938 | op->_ob_next = op->_ob_prev = NULL; |
Guido van Rossum | d8953cb | 1995-04-06 14:46:26 +0000 | [diff] [blame] | 939 | #ifdef COUNT_ALLOCS |
| 940 | op->ob_type->tp_free++; |
| 941 | #endif |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 942 | } |
| 943 | |
Guido van Rossum | aacdc9d | 1996-08-12 21:32:12 +0000 | [diff] [blame] | 944 | void |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 945 | _Py_Dealloc(op) |
| 946 | PyObject *op; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 947 | { |
Guido van Rossum | 9776adf | 1994-09-07 14:36:45 +0000 | [diff] [blame] | 948 | destructor dealloc = op->ob_type->tp_dealloc; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 949 | _Py_ForgetReference(op); |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 950 | #ifndef WITH_CYCLE_GC |
Guido van Rossum | e92e610 | 2000-04-24 15:40:53 +0000 | [diff] [blame] | 951 | if (_PyTrash_delete_nesting < PyTrash_UNWIND_LEVEL-1) |
| 952 | op->ob_type = NULL; |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 953 | #endif |
Guido van Rossum | 9776adf | 1994-09-07 14:36:45 +0000 | [diff] [blame] | 954 | (*dealloc)(op); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 955 | } |
| 956 | |
Guido van Rossum | aacdc9d | 1996-08-12 21:32:12 +0000 | [diff] [blame] | 957 | void |
Guido van Rossum | ded690f | 1996-05-24 20:48:31 +0000 | [diff] [blame] | 958 | _Py_PrintReferences(fp) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 959 | FILE *fp; |
| 960 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 961 | PyObject *op; |
Guido van Rossum | e09fb55 | 1997-08-05 02:04:34 +0000 | [diff] [blame] | 962 | fprintf(fp, "Remaining objects:\n"); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 963 | for (op = refchain._ob_next; op != &refchain; op = op->_ob_next) { |
| 964 | fprintf(fp, "[%d] ", op->ob_refcnt); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 965 | if (PyObject_Print(op, fp, 0) != 0) |
| 966 | PyErr_Clear(); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 967 | putc('\n', fp); |
| 968 | } |
| 969 | } |
| 970 | |
Sjoerd Mullender | 6ec3c65 | 1995-08-29 09:18:14 +0000 | [diff] [blame] | 971 | PyObject * |
Guido van Rossum | ded690f | 1996-05-24 20:48:31 +0000 | [diff] [blame] | 972 | _Py_GetObjects(self, args) |
Sjoerd Mullender | 6ec3c65 | 1995-08-29 09:18:14 +0000 | [diff] [blame] | 973 | PyObject *self; |
| 974 | PyObject *args; |
| 975 | { |
| 976 | int i, n; |
| 977 | PyObject *t = NULL; |
| 978 | PyObject *res, *op; |
| 979 | |
| 980 | if (!PyArg_ParseTuple(args, "i|O", &n, &t)) |
| 981 | return NULL; |
| 982 | op = refchain._ob_next; |
| 983 | res = PyList_New(0); |
| 984 | if (res == NULL) |
| 985 | return NULL; |
| 986 | for (i = 0; (n == 0 || i < n) && op != &refchain; i++) { |
| 987 | while (op == self || op == args || op == res || op == t || |
| 988 | t != NULL && op->ob_type != (PyTypeObject *) t) { |
| 989 | op = op->_ob_next; |
| 990 | if (op == &refchain) |
| 991 | return res; |
| 992 | } |
| 993 | if (PyList_Append(res, op) < 0) { |
| 994 | Py_DECREF(res); |
| 995 | return NULL; |
| 996 | } |
| 997 | op = op->_ob_next; |
| 998 | } |
| 999 | return res; |
| 1000 | } |
| 1001 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1002 | #endif |
Guido van Rossum | 97ead3f | 1996-01-12 01:24:09 +0000 | [diff] [blame] | 1003 | |
| 1004 | |
| 1005 | /* Hack to force loading of cobject.o */ |
Guido van Rossum | da9c271 | 1996-12-05 21:58:58 +0000 | [diff] [blame] | 1006 | PyTypeObject *_Py_cobject_hack = &PyCObject_Type; |
Guido van Rossum | 84a9032 | 1996-05-22 16:34:47 +0000 | [diff] [blame] | 1007 | |
| 1008 | |
| 1009 | /* Hack to force loading of abstract.o */ |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1010 | int (*_Py_abstract_hack) Py_FPROTO((PyObject *)) = &PyObject_Length; |
Guido van Rossum | e09fb55 | 1997-08-05 02:04:34 +0000 | [diff] [blame] | 1011 | |
| 1012 | |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 1013 | /* Python's malloc wrappers (see mymalloc.h) */ |
Guido van Rossum | e09fb55 | 1997-08-05 02:04:34 +0000 | [diff] [blame] | 1014 | |
| 1015 | ANY * |
| 1016 | PyMem_Malloc(nbytes) |
| 1017 | size_t nbytes; |
| 1018 | { |
| 1019 | #if _PyMem_EXTRA > 0 |
| 1020 | if (nbytes == 0) |
| 1021 | nbytes = _PyMem_EXTRA; |
| 1022 | #endif |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 1023 | return PyMem_MALLOC(nbytes); |
Guido van Rossum | e09fb55 | 1997-08-05 02:04:34 +0000 | [diff] [blame] | 1024 | } |
| 1025 | |
| 1026 | ANY * |
| 1027 | PyMem_Realloc(p, nbytes) |
| 1028 | ANY *p; |
| 1029 | size_t nbytes; |
| 1030 | { |
| 1031 | #if _PyMem_EXTRA > 0 |
| 1032 | if (nbytes == 0) |
| 1033 | nbytes = _PyMem_EXTRA; |
| 1034 | #endif |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 1035 | return PyMem_REALLOC(p, nbytes); |
Guido van Rossum | e09fb55 | 1997-08-05 02:04:34 +0000 | [diff] [blame] | 1036 | } |
| 1037 | |
| 1038 | void |
| 1039 | PyMem_Free(p) |
| 1040 | ANY *p; |
| 1041 | { |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 1042 | PyMem_FREE(p); |
| 1043 | } |
| 1044 | |
| 1045 | |
| 1046 | /* Python's object malloc wrappers (see objimpl.h) */ |
| 1047 | |
| 1048 | ANY * |
| 1049 | PyObject_Malloc(nbytes) |
| 1050 | size_t nbytes; |
| 1051 | { |
| 1052 | return PyObject_MALLOC(nbytes); |
| 1053 | } |
| 1054 | |
| 1055 | ANY * |
| 1056 | PyObject_Realloc(p, nbytes) |
| 1057 | ANY *p; |
| 1058 | size_t nbytes; |
| 1059 | { |
| 1060 | return PyObject_REALLOC(p, nbytes); |
| 1061 | } |
| 1062 | |
| 1063 | void |
| 1064 | PyObject_Free(p) |
| 1065 | ANY *p; |
| 1066 | { |
| 1067 | PyObject_FREE(p); |
Guido van Rossum | e09fb55 | 1997-08-05 02:04:34 +0000 | [diff] [blame] | 1068 | } |
Guido van Rossum | 8661036 | 1998-04-10 22:32:46 +0000 | [diff] [blame] | 1069 | |
| 1070 | |
| 1071 | /* These methods are used to control infinite recursion in repr, str, print, |
| 1072 | etc. Container objects that may recursively contain themselves, |
| 1073 | e.g. builtin dictionaries and lists, should used Py_ReprEnter() and |
| 1074 | Py_ReprLeave() to avoid infinite recursion. |
| 1075 | |
| 1076 | Py_ReprEnter() returns 0 the first time it is called for a particular |
| 1077 | object and 1 every time thereafter. It returns -1 if an exception |
| 1078 | occurred. Py_ReprLeave() has no return value. |
| 1079 | |
| 1080 | See dictobject.c and listobject.c for examples of use. |
| 1081 | */ |
| 1082 | |
| 1083 | #define KEY "Py_Repr" |
| 1084 | |
| 1085 | int |
| 1086 | Py_ReprEnter(obj) |
| 1087 | PyObject *obj; |
| 1088 | { |
| 1089 | PyObject *dict; |
| 1090 | PyObject *list; |
| 1091 | int i; |
| 1092 | |
| 1093 | dict = PyThreadState_GetDict(); |
| 1094 | if (dict == NULL) |
| 1095 | return -1; |
| 1096 | list = PyDict_GetItemString(dict, KEY); |
| 1097 | if (list == NULL) { |
| 1098 | list = PyList_New(0); |
| 1099 | if (list == NULL) |
| 1100 | return -1; |
| 1101 | if (PyDict_SetItemString(dict, KEY, list) < 0) |
| 1102 | return -1; |
| 1103 | Py_DECREF(list); |
| 1104 | } |
| 1105 | i = PyList_GET_SIZE(list); |
| 1106 | while (--i >= 0) { |
| 1107 | if (PyList_GET_ITEM(list, i) == obj) |
| 1108 | return 1; |
| 1109 | } |
| 1110 | PyList_Append(list, obj); |
| 1111 | return 0; |
| 1112 | } |
| 1113 | |
| 1114 | void |
| 1115 | Py_ReprLeave(obj) |
| 1116 | PyObject *obj; |
| 1117 | { |
| 1118 | PyObject *dict; |
| 1119 | PyObject *list; |
| 1120 | int i; |
| 1121 | |
| 1122 | dict = PyThreadState_GetDict(); |
Guido van Rossum | eb90946 | 1998-04-11 15:17:34 +0000 | [diff] [blame] | 1123 | if (dict == NULL) |
| 1124 | return; |
Guido van Rossum | 8661036 | 1998-04-10 22:32:46 +0000 | [diff] [blame] | 1125 | list = PyDict_GetItemString(dict, KEY); |
Guido van Rossum | eb90946 | 1998-04-11 15:17:34 +0000 | [diff] [blame] | 1126 | if (list == NULL || !PyList_Check(list)) |
| 1127 | return; |
Guido van Rossum | 8661036 | 1998-04-10 22:32:46 +0000 | [diff] [blame] | 1128 | i = PyList_GET_SIZE(list); |
| 1129 | /* Count backwards because we always expect obj to be list[-1] */ |
| 1130 | while (--i >= 0) { |
| 1131 | if (PyList_GET_ITEM(list, i) == obj) { |
| 1132 | PyList_SetSlice(list, i, i + 1, NULL); |
| 1133 | break; |
| 1134 | } |
| 1135 | } |
| 1136 | } |
Guido van Rossum | d724b23 | 2000-03-13 16:01:29 +0000 | [diff] [blame] | 1137 | |
| 1138 | /* |
| 1139 | trashcan |
| 1140 | CT 2k0130 |
| 1141 | non-recursively destroy nested objects |
| 1142 | |
| 1143 | CT 2k0223 |
| 1144 | everything is now done in a macro. |
| 1145 | |
| 1146 | CT 2k0305 |
| 1147 | modified to use functions, after Tim Peter's suggestion. |
| 1148 | |
| 1149 | CT 2k0309 |
| 1150 | modified to restore a possible error. |
Guido van Rossum | 13ff8eb | 2000-03-25 18:39:19 +0000 | [diff] [blame] | 1151 | |
| 1152 | CT 2k0325 |
| 1153 | added better safe than sorry check for threadstate |
Guido van Rossum | e92e610 | 2000-04-24 15:40:53 +0000 | [diff] [blame] | 1154 | |
| 1155 | CT 2k0422 |
| 1156 | complete rewrite. We now build a chain via ob_type |
| 1157 | and save the limited number of types in ob_refcnt. |
| 1158 | This is perfect since we don't need any memory. |
| 1159 | A patch for free-threading would need just a lock. |
Guido van Rossum | d724b23 | 2000-03-13 16:01:29 +0000 | [diff] [blame] | 1160 | */ |
| 1161 | |
Guido van Rossum | e92e610 | 2000-04-24 15:40:53 +0000 | [diff] [blame] | 1162 | #define Py_TRASHCAN_TUPLE 1 |
| 1163 | #define Py_TRASHCAN_LIST 2 |
| 1164 | #define Py_TRASHCAN_DICT 3 |
| 1165 | #define Py_TRASHCAN_FRAME 4 |
| 1166 | #define Py_TRASHCAN_TRACEBACK 5 |
| 1167 | /* extend here if other objects want protection */ |
| 1168 | |
Guido van Rossum | d724b23 | 2000-03-13 16:01:29 +0000 | [diff] [blame] | 1169 | int _PyTrash_delete_nesting = 0; |
Guido van Rossum | e92e610 | 2000-04-24 15:40:53 +0000 | [diff] [blame] | 1170 | |
Guido van Rossum | d724b23 | 2000-03-13 16:01:29 +0000 | [diff] [blame] | 1171 | PyObject * _PyTrash_delete_later = NULL; |
| 1172 | |
| 1173 | void |
| 1174 | _PyTrash_deposit_object(op) |
| 1175 | PyObject *op; |
| 1176 | { |
Guido van Rossum | e92e610 | 2000-04-24 15:40:53 +0000 | [diff] [blame] | 1177 | int typecode; |
| 1178 | PyObject *hold = _PyTrash_delete_later; |
Guido van Rossum | 13ff8eb | 2000-03-25 18:39:19 +0000 | [diff] [blame] | 1179 | |
Guido van Rossum | e92e610 | 2000-04-24 15:40:53 +0000 | [diff] [blame] | 1180 | if (PyTuple_Check(op)) |
| 1181 | typecode = Py_TRASHCAN_TUPLE; |
| 1182 | else if (PyList_Check(op)) |
| 1183 | typecode = Py_TRASHCAN_LIST; |
| 1184 | else if (PyDict_Check(op)) |
| 1185 | typecode = Py_TRASHCAN_DICT; |
| 1186 | else if (PyFrame_Check(op)) |
| 1187 | typecode = Py_TRASHCAN_FRAME; |
| 1188 | else if (PyTraceBack_Check(op)) |
| 1189 | typecode = Py_TRASHCAN_TRACEBACK; |
| 1190 | op->ob_refcnt = typecode; |
Guido van Rossum | d724b23 | 2000-03-13 16:01:29 +0000 | [diff] [blame] | 1191 | |
Guido van Rossum | e92e610 | 2000-04-24 15:40:53 +0000 | [diff] [blame] | 1192 | op->ob_type = (PyTypeObject*)_PyTrash_delete_later; |
| 1193 | _PyTrash_delete_later = op; |
Guido van Rossum | d724b23 | 2000-03-13 16:01:29 +0000 | [diff] [blame] | 1194 | } |
| 1195 | |
| 1196 | void |
Guido van Rossum | e92e610 | 2000-04-24 15:40:53 +0000 | [diff] [blame] | 1197 | _PyTrash_destroy_chain() |
Guido van Rossum | d724b23 | 2000-03-13 16:01:29 +0000 | [diff] [blame] | 1198 | { |
| 1199 | while (_PyTrash_delete_later) { |
| 1200 | PyObject *shredder = _PyTrash_delete_later; |
Guido van Rossum | e92e610 | 2000-04-24 15:40:53 +0000 | [diff] [blame] | 1201 | _PyTrash_delete_later = (PyObject*) shredder->ob_type; |
| 1202 | |
| 1203 | switch (shredder->ob_refcnt) { |
| 1204 | case Py_TRASHCAN_TUPLE: |
| 1205 | shredder->ob_type = &PyTuple_Type; |
| 1206 | break; |
| 1207 | case Py_TRASHCAN_LIST: |
| 1208 | shredder->ob_type = &PyList_Type; |
| 1209 | break; |
| 1210 | case Py_TRASHCAN_DICT: |
| 1211 | shredder->ob_type = &PyDict_Type; |
| 1212 | break; |
| 1213 | case Py_TRASHCAN_FRAME: |
| 1214 | shredder->ob_type = &PyFrame_Type; |
| 1215 | break; |
| 1216 | case Py_TRASHCAN_TRACEBACK: |
| 1217 | shredder->ob_type = &PyTraceBack_Type; |
| 1218 | break; |
| 1219 | } |
| 1220 | _Py_NewReference(shredder); |
| 1221 | |
Guido van Rossum | d724b23 | 2000-03-13 16:01:29 +0000 | [diff] [blame] | 1222 | ++_PyTrash_delete_nesting; |
| 1223 | Py_DECREF(shredder); |
| 1224 | --_PyTrash_delete_nesting; |
| 1225 | } |
| 1226 | } |
Guido van Rossum | e92e610 | 2000-04-24 15:40:53 +0000 | [diff] [blame] | 1227 | |