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