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