Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 1 | /*********************************************************** |
Guido van Rossum | 6610ad9 | 1995-01-04 19:07:38 +0000 | [diff] [blame] | 2 | Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam, |
| 3 | The Netherlands. |
Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 4 | |
| 5 | All Rights Reserved |
| 6 | |
Guido van Rossum | d266eb4 | 1996-10-25 14:44:06 +0000 | [diff] [blame] | 7 | Permission to use, copy, modify, and distribute this software and its |
| 8 | documentation for any purpose and without fee is hereby granted, |
Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 9 | provided that the above copyright notice appear in all copies and that |
Guido van Rossum | d266eb4 | 1996-10-25 14:44:06 +0000 | [diff] [blame] | 10 | both that copyright notice and this permission notice appear in |
Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 11 | supporting documentation, and that the names of Stichting Mathematisch |
Guido van Rossum | d266eb4 | 1996-10-25 14:44:06 +0000 | [diff] [blame] | 12 | Centrum or CWI or Corporation for National Research Initiatives or |
| 13 | CNRI not be used in advertising or publicity pertaining to |
| 14 | distribution of the software without specific, written prior |
| 15 | permission. |
Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 16 | |
Guido van Rossum | d266eb4 | 1996-10-25 14:44:06 +0000 | [diff] [blame] | 17 | While CWI is the initial source for this software, a modified version |
| 18 | is made available by the Corporation for National Research Initiatives |
| 19 | (CNRI) at the Internet address ftp://ftp.python.org. |
| 20 | |
| 21 | STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH |
| 22 | REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF |
| 23 | MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH |
| 24 | CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL |
| 25 | DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR |
| 26 | PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER |
| 27 | TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR |
| 28 | PERFORMANCE OF THIS SOFTWARE. |
Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 29 | |
| 30 | ******************************************************************/ |
| 31 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 32 | /* Generic object operations; and implementation of None (NoObject) */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 33 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 34 | #include "Python.h" |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 35 | |
Guido van Rossum | 6f9e433 | 1995-03-29 16:57:48 +0000 | [diff] [blame] | 36 | #if defined( Py_TRACE_REFS ) || defined( Py_REF_DEBUG ) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 37 | long _Py_RefTotal; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 38 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 39 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 40 | /* Object allocation routines used by NEWOBJ and NEWVAROBJ macros. |
| 41 | These are used by the individual routines for object creation. |
| 42 | Do not call them otherwise, they do not initialize the object! */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 43 | |
Sjoerd Mullender | a9c3c22 | 1993-10-11 12:54:31 +0000 | [diff] [blame] | 44 | #ifdef COUNT_ALLOCS |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 45 | static PyTypeObject *type_list; |
Sjoerd Mullender | 842d2cc | 1993-10-15 16:18:48 +0000 | [diff] [blame] | 46 | extern int tuple_zero_allocs, fast_tuple_allocs; |
| 47 | extern int quick_int_allocs, quick_neg_int_allocs; |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 48 | extern int null_strings, one_strings; |
Sjoerd Mullender | a9c3c22 | 1993-10-11 12:54:31 +0000 | [diff] [blame] | 49 | void |
| 50 | dump_counts() |
| 51 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 52 | PyTypeObject *tp; |
Sjoerd Mullender | a9c3c22 | 1993-10-11 12:54:31 +0000 | [diff] [blame] | 53 | |
| 54 | for (tp = type_list; tp; tp = tp->tp_next) |
Sjoerd Mullender | 52c1f51 | 1993-10-25 08:40:52 +0000 | [diff] [blame] | 55 | fprintf(stderr, "%s alloc'd: %d, freed: %d, max in use: %d\n", |
| 56 | tp->tp_name, tp->tp_alloc, tp->tp_free, |
| 57 | tp->tp_maxalloc); |
| 58 | fprintf(stderr, "fast tuple allocs: %d, empty: %d\n", |
| 59 | fast_tuple_allocs, tuple_zero_allocs); |
| 60 | fprintf(stderr, "fast int allocs: pos: %d, neg: %d\n", |
| 61 | quick_int_allocs, quick_neg_int_allocs); |
| 62 | fprintf(stderr, "null strings: %d, 1-strings: %d\n", |
| 63 | null_strings, one_strings); |
Sjoerd Mullender | a9c3c22 | 1993-10-11 12:54:31 +0000 | [diff] [blame] | 64 | } |
| 65 | |
Sjoerd Mullender | 6ec3c65 | 1995-08-29 09:18:14 +0000 | [diff] [blame] | 66 | PyObject * |
| 67 | get_counts() |
| 68 | { |
| 69 | PyTypeObject *tp; |
| 70 | PyObject *result; |
| 71 | PyObject *v; |
| 72 | |
| 73 | result = PyList_New(0); |
| 74 | if (result == NULL) |
| 75 | return NULL; |
| 76 | for (tp = type_list; tp; tp = tp->tp_next) { |
| 77 | v = Py_BuildValue("(siii)", tp->tp_name, tp->tp_alloc, |
| 78 | tp->tp_free, tp->tp_maxalloc); |
| 79 | if (v == NULL) { |
| 80 | Py_DECREF(result); |
| 81 | return NULL; |
| 82 | } |
| 83 | if (PyList_Append(result, v) < 0) { |
| 84 | Py_DECREF(v); |
| 85 | Py_DECREF(result); |
| 86 | return NULL; |
| 87 | } |
| 88 | Py_DECREF(v); |
| 89 | } |
| 90 | return result; |
| 91 | } |
| 92 | |
Sjoerd Mullender | a9c3c22 | 1993-10-11 12:54:31 +0000 | [diff] [blame] | 93 | void |
| 94 | inc_count(tp) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 95 | PyTypeObject *tp; |
Sjoerd Mullender | a9c3c22 | 1993-10-11 12:54:31 +0000 | [diff] [blame] | 96 | { |
| 97 | if (tp->tp_alloc == 0) { |
Guido van Rossum | d8953cb | 1995-04-06 14:46:26 +0000 | [diff] [blame] | 98 | /* first time; insert in linked list */ |
Sjoerd Mullender | a9c3c22 | 1993-10-11 12:54:31 +0000 | [diff] [blame] | 99 | if (tp->tp_next != NULL) /* sanity check */ |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 100 | Py_FatalError("XXX inc_count sanity check"); |
Sjoerd Mullender | a9c3c22 | 1993-10-11 12:54:31 +0000 | [diff] [blame] | 101 | tp->tp_next = type_list; |
| 102 | type_list = tp; |
| 103 | } |
| 104 | tp->tp_alloc++; |
| 105 | if (tp->tp_alloc - tp->tp_free > tp->tp_maxalloc) |
| 106 | tp->tp_maxalloc = tp->tp_alloc - tp->tp_free; |
| 107 | } |
| 108 | #endif |
| 109 | |
Guido van Rossum | f5030ab | 1996-07-21 02:30:39 +0000 | [diff] [blame] | 110 | #ifndef MS_COREDLL |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 111 | PyObject * |
| 112 | _PyObject_New(tp) |
| 113 | PyTypeObject *tp; |
Guido van Rossum | f5030ab | 1996-07-21 02:30:39 +0000 | [diff] [blame] | 114 | #else |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 115 | PyObject * |
| 116 | _PyObject_New(tp,op) |
| 117 | PyTypeObject *tp; |
Guido van Rossum | f5030ab | 1996-07-21 02:30:39 +0000 | [diff] [blame] | 118 | PyObject *op; |
| 119 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 120 | { |
Guido van Rossum | f5030ab | 1996-07-21 02:30:39 +0000 | [diff] [blame] | 121 | #ifndef MS_COREDLL |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 122 | PyObject *op = (PyObject *) malloc(tp->tp_basicsize); |
Guido van Rossum | f5030ab | 1996-07-21 02:30:39 +0000 | [diff] [blame] | 123 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 124 | if (op == NULL) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 125 | return PyErr_NoMemory(); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 126 | op->ob_type = tp; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 127 | _Py_NewReference(op); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 128 | return op; |
| 129 | } |
| 130 | |
Guido van Rossum | f5030ab | 1996-07-21 02:30:39 +0000 | [diff] [blame] | 131 | #ifndef MS_COREDLL |
Guido van Rossum | d0c87ee | 1997-05-15 21:31:03 +0000 | [diff] [blame] | 132 | PyVarObject * |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 133 | _PyObject_NewVar(tp, size) |
| 134 | PyTypeObject *tp; |
Guido van Rossum | 2497ead | 1995-02-10 17:00:27 +0000 | [diff] [blame] | 135 | int size; |
Guido van Rossum | f5030ab | 1996-07-21 02:30:39 +0000 | [diff] [blame] | 136 | #else |
Guido van Rossum | d0c87ee | 1997-05-15 21:31:03 +0000 | [diff] [blame] | 137 | PyVarObject * |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 138 | _PyObject_NewVar(tp, size, op) |
| 139 | PyTypeObject *tp; |
Guido van Rossum | f5030ab | 1996-07-21 02:30:39 +0000 | [diff] [blame] | 140 | int size; |
Guido van Rossum | d0c87ee | 1997-05-15 21:31:03 +0000 | [diff] [blame] | 141 | PyVarObject *op; |
Guido van Rossum | f5030ab | 1996-07-21 02:30:39 +0000 | [diff] [blame] | 142 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 143 | { |
Guido van Rossum | f5030ab | 1996-07-21 02:30:39 +0000 | [diff] [blame] | 144 | #ifndef MS_COREDLL |
Guido van Rossum | d0c87ee | 1997-05-15 21:31:03 +0000 | [diff] [blame] | 145 | PyVarObject *op = (PyVarObject *) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 146 | malloc(tp->tp_basicsize + size * tp->tp_itemsize); |
Guido van Rossum | f5030ab | 1996-07-21 02:30:39 +0000 | [diff] [blame] | 147 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 148 | if (op == NULL) |
Guido van Rossum | d0c87ee | 1997-05-15 21:31:03 +0000 | [diff] [blame] | 149 | return (PyVarObject *)PyErr_NoMemory(); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 150 | op->ob_type = tp; |
| 151 | op->ob_size = size; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 152 | _Py_NewReference(op); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 153 | return op; |
| 154 | } |
| 155 | |
Guido van Rossum | 9093361 | 1991-06-07 16:10:43 +0000 | [diff] [blame] | 156 | int |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 157 | PyObject_Print(op, fp, flags) |
| 158 | PyObject *op; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 159 | FILE *fp; |
| 160 | int flags; |
| 161 | { |
Guido van Rossum | 278ef59 | 1991-07-27 21:40:24 +0000 | [diff] [blame] | 162 | int ret = 0; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 163 | if (PyErr_CheckSignals()) |
Guido van Rossum | 9093361 | 1991-06-07 16:10:43 +0000 | [diff] [blame] | 164 | return -1; |
Guido van Rossum | 9b00dfa | 1998-04-28 16:06:54 +0000 | [diff] [blame] | 165 | #ifdef USE_STACKCHECK |
| 166 | if (PyOS_CheckStack()) { |
| 167 | PyErr_SetString(PyExc_MemoryError, "Stack overflow"); |
| 168 | return -1; |
| 169 | } |
| 170 | #endif |
Guido van Rossum | 9093361 | 1991-06-07 16:10:43 +0000 | [diff] [blame] | 171 | if (op == NULL) { |
| 172 | fprintf(fp, "<nil>"); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 173 | } |
Guido van Rossum | 9093361 | 1991-06-07 16:10:43 +0000 | [diff] [blame] | 174 | else { |
| 175 | if (op->ob_refcnt <= 0) |
Guido van Rossum | 2e8f614 | 1992-09-03 20:32:55 +0000 | [diff] [blame] | 176 | fprintf(fp, "<refcnt %u at %lx>", |
| 177 | op->ob_refcnt, (long)op); |
| 178 | else if (op->ob_type->tp_print == NULL) { |
| 179 | if (op->ob_type->tp_repr == NULL) { |
| 180 | fprintf(fp, "<%s object at %lx>", |
| 181 | op->ob_type->tp_name, (long)op); |
| 182 | } |
| 183 | else { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 184 | PyObject *s; |
| 185 | if (flags & Py_PRINT_RAW) |
| 186 | s = PyObject_Str(op); |
Guido van Rossum | c600411 | 1993-11-05 10:22:19 +0000 | [diff] [blame] | 187 | else |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 188 | s = PyObject_Repr(op); |
Guido van Rossum | 2e8f614 | 1992-09-03 20:32:55 +0000 | [diff] [blame] | 189 | if (s == NULL) |
| 190 | ret = -1; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 191 | else if (!PyString_Check(s)) { |
| 192 | PyErr_SetString(PyExc_TypeError, |
Guido van Rossum | 2e8f614 | 1992-09-03 20:32:55 +0000 | [diff] [blame] | 193 | "repr not string"); |
| 194 | ret = -1; |
| 195 | } |
| 196 | else { |
Guido van Rossum | 565798d | 1998-04-21 22:25:01 +0000 | [diff] [blame] | 197 | ret = PyObject_Print(s, fp, |
| 198 | Py_PRINT_RAW); |
Guido van Rossum | 2e8f614 | 1992-09-03 20:32:55 +0000 | [diff] [blame] | 199 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 200 | Py_XDECREF(s); |
Guido van Rossum | 2e8f614 | 1992-09-03 20:32:55 +0000 | [diff] [blame] | 201 | } |
| 202 | } |
Guido van Rossum | 9093361 | 1991-06-07 16:10:43 +0000 | [diff] [blame] | 203 | else |
Guido van Rossum | 278ef59 | 1991-07-27 21:40:24 +0000 | [diff] [blame] | 204 | ret = (*op->ob_type->tp_print)(op, fp, flags); |
Guido van Rossum | 9093361 | 1991-06-07 16:10:43 +0000 | [diff] [blame] | 205 | } |
Guido van Rossum | 278ef59 | 1991-07-27 21:40:24 +0000 | [diff] [blame] | 206 | if (ret == 0) { |
| 207 | if (ferror(fp)) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 208 | PyErr_SetFromErrno(PyExc_IOError); |
Guido van Rossum | 278ef59 | 1991-07-27 21:40:24 +0000 | [diff] [blame] | 209 | clearerr(fp); |
| 210 | ret = -1; |
| 211 | } |
| 212 | } |
| 213 | return ret; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 214 | } |
| 215 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 216 | PyObject * |
| 217 | PyObject_Repr(v) |
| 218 | PyObject *v; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 219 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 220 | if (PyErr_CheckSignals()) |
Guido van Rossum | 9093361 | 1991-06-07 16:10:43 +0000 | [diff] [blame] | 221 | return NULL; |
Guido van Rossum | 9b00dfa | 1998-04-28 16:06:54 +0000 | [diff] [blame] | 222 | #ifdef USE_STACKCHECK |
| 223 | if (PyOS_CheckStack()) { |
| 224 | PyErr_SetString(PyExc_MemoryError, "Stack overflow"); |
| 225 | return NULL; |
| 226 | } |
| 227 | #endif |
Guido van Rossum | 9093361 | 1991-06-07 16:10:43 +0000 | [diff] [blame] | 228 | if (v == NULL) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 229 | return PyString_FromString("<NULL>"); |
Guido van Rossum | 9093361 | 1991-06-07 16:10:43 +0000 | [diff] [blame] | 230 | else if (v->ob_type->tp_repr == NULL) { |
| 231 | char buf[120]; |
| 232 | sprintf(buf, "<%.80s object at %lx>", |
| 233 | v->ob_type->tp_name, (long)v); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 234 | return PyString_FromString(buf); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 235 | } |
Guido van Rossum | 9093361 | 1991-06-07 16:10:43 +0000 | [diff] [blame] | 236 | else |
| 237 | return (*v->ob_type->tp_repr)(v); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 238 | } |
| 239 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 240 | PyObject * |
| 241 | PyObject_Str(v) |
| 242 | PyObject *v; |
Guido van Rossum | c600411 | 1993-11-05 10:22:19 +0000 | [diff] [blame] | 243 | { |
| 244 | if (v == NULL) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 245 | return PyString_FromString("<NULL>"); |
| 246 | else if (PyString_Check(v)) { |
| 247 | Py_INCREF(v); |
Guido van Rossum | c600411 | 1993-11-05 10:22:19 +0000 | [diff] [blame] | 248 | return v; |
| 249 | } |
Guido van Rossum | 32b582b | 1995-01-17 16:35:13 +0000 | [diff] [blame] | 250 | else if (v->ob_type->tp_str != NULL) |
| 251 | return (*v->ob_type->tp_str)(v); |
Guido van Rossum | c600411 | 1993-11-05 10:22:19 +0000 | [diff] [blame] | 252 | else { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 253 | PyObject *func; |
| 254 | PyObject *res; |
| 255 | if (!PyInstance_Check(v) || |
| 256 | (func = PyObject_GetAttrString(v, "__str__")) == NULL) { |
| 257 | PyErr_Clear(); |
| 258 | return PyObject_Repr(v); |
Guido van Rossum | c600411 | 1993-11-05 10:22:19 +0000 | [diff] [blame] | 259 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 260 | res = PyEval_CallObject(func, (PyObject *)NULL); |
| 261 | Py_DECREF(func); |
Guido van Rossum | c600411 | 1993-11-05 10:22:19 +0000 | [diff] [blame] | 262 | return res; |
| 263 | } |
| 264 | } |
| 265 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 266 | static PyObject * |
Guido van Rossum | 2056684 | 1995-01-12 11:26:10 +0000 | [diff] [blame] | 267 | do_cmp(v, w) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 268 | PyObject *v, *w; |
Guido van Rossum | 2056684 | 1995-01-12 11:26:10 +0000 | [diff] [blame] | 269 | { |
Guido van Rossum | c8b6df9 | 1997-05-23 00:06:51 +0000 | [diff] [blame] | 270 | long c; |
Guido van Rossum | 2056684 | 1995-01-12 11:26:10 +0000 | [diff] [blame] | 271 | /* __rcmp__ actually won't be called unless __cmp__ isn't defined, |
| 272 | because the check in cmpobject() reverses the objects first. |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 273 | This is intentional -- it makes no sense to define cmp(x,y) |
| 274 | different than -cmp(y,x). */ |
| 275 | if (PyInstance_Check(v) || PyInstance_Check(w)) |
| 276 | return PyInstance_DoBinOp(v, w, "__cmp__", "__rcmp__", do_cmp); |
Guido van Rossum | c8b6df9 | 1997-05-23 00:06:51 +0000 | [diff] [blame] | 277 | c = PyObject_Compare(v, w); |
| 278 | if (c && PyErr_Occurred()) |
| 279 | return NULL; |
| 280 | return PyInt_FromLong(c); |
Guido van Rossum | 2056684 | 1995-01-12 11:26:10 +0000 | [diff] [blame] | 281 | } |
| 282 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 283 | int |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 284 | PyObject_Compare(v, w) |
| 285 | PyObject *v, *w; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 286 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 287 | PyTypeObject *tp; |
Guido van Rossum | c8b6df9 | 1997-05-23 00:06:51 +0000 | [diff] [blame] | 288 | if (v == NULL || w == NULL) { |
| 289 | PyErr_BadInternalCall(); |
| 290 | return -1; |
| 291 | } |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 292 | if (v == w) |
| 293 | return 0; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 294 | if (PyInstance_Check(v) || PyInstance_Check(w)) { |
| 295 | PyObject *res; |
Guido van Rossum | 2056684 | 1995-01-12 11:26:10 +0000 | [diff] [blame] | 296 | int c; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 297 | if (!PyInstance_Check(v)) |
| 298 | return -PyObject_Compare(w, v); |
Guido van Rossum | 2056684 | 1995-01-12 11:26:10 +0000 | [diff] [blame] | 299 | res = do_cmp(v, w); |
Guido van Rossum | c8b6df9 | 1997-05-23 00:06:51 +0000 | [diff] [blame] | 300 | if (res == NULL) |
| 301 | return -1; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 302 | if (!PyInt_Check(res)) { |
| 303 | Py_DECREF(res); |
Guido van Rossum | c8b6df9 | 1997-05-23 00:06:51 +0000 | [diff] [blame] | 304 | PyErr_SetString(PyExc_TypeError, |
| 305 | "comparison did not return an int"); |
| 306 | return -1; |
Guido van Rossum | 2056684 | 1995-01-12 11:26:10 +0000 | [diff] [blame] | 307 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 308 | c = PyInt_AsLong(res); |
| 309 | Py_DECREF(res); |
Guido van Rossum | 2056684 | 1995-01-12 11:26:10 +0000 | [diff] [blame] | 310 | return (c < 0) ? -1 : (c > 0) ? 1 : 0; |
| 311 | } |
Guido van Rossum | 9fb0368 | 1991-07-01 18:48:04 +0000 | [diff] [blame] | 312 | if ((tp = v->ob_type) != w->ob_type) { |
| 313 | if (tp->tp_as_number != NULL && |
| 314 | w->ob_type->tp_as_number != NULL) { |
Guido van Rossum | 242c642 | 1997-11-19 16:03:17 +0000 | [diff] [blame] | 315 | int err; |
| 316 | err = PyNumber_CoerceEx(&v, &w); |
| 317 | if (err < 0) |
Guido van Rossum | c8b6df9 | 1997-05-23 00:06:51 +0000 | [diff] [blame] | 318 | return -1; |
Guido van Rossum | 242c642 | 1997-11-19 16:03:17 +0000 | [diff] [blame] | 319 | else if (err == 0) { |
Guido van Rossum | 9fb0368 | 1991-07-01 18:48:04 +0000 | [diff] [blame] | 320 | int cmp = (*v->ob_type->tp_compare)(v, w); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 321 | Py_DECREF(v); |
| 322 | Py_DECREF(w); |
Guido van Rossum | 9fb0368 | 1991-07-01 18:48:04 +0000 | [diff] [blame] | 323 | return cmp; |
| 324 | } |
| 325 | } |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 326 | return strcmp(tp->tp_name, w->ob_type->tp_name); |
Guido van Rossum | 9fb0368 | 1991-07-01 18:48:04 +0000 | [diff] [blame] | 327 | } |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 328 | if (tp->tp_compare == NULL) |
| 329 | return (v < w) ? -1 : 1; |
Guido van Rossum | 9fb0368 | 1991-07-01 18:48:04 +0000 | [diff] [blame] | 330 | return (*tp->tp_compare)(v, w); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 331 | } |
| 332 | |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 333 | long |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 334 | PyObject_Hash(v) |
| 335 | PyObject *v; |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 336 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 337 | PyTypeObject *tp = v->ob_type; |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 338 | if (tp->tp_hash != NULL) |
| 339 | return (*tp->tp_hash)(v); |
| 340 | if (tp->tp_compare == NULL) |
| 341 | return (long) v; /* Use address as hash value */ |
| 342 | /* 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] | 343 | PyErr_SetString(PyExc_TypeError, "unhashable type"); |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 344 | return -1; |
| 345 | } |
| 346 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 347 | PyObject * |
| 348 | PyObject_GetAttrString(v, name) |
| 349 | PyObject *v; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 350 | char *name; |
| 351 | { |
Guido van Rossum | d8eb1b3 | 1996-08-09 20:52:03 +0000 | [diff] [blame] | 352 | if (v->ob_type->tp_getattro != NULL) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 353 | PyObject *w, *res; |
Guido van Rossum | c6d0670 | 1997-01-18 07:57:16 +0000 | [diff] [blame] | 354 | w = PyString_InternFromString(name); |
Guido van Rossum | d8eb1b3 | 1996-08-09 20:52:03 +0000 | [diff] [blame] | 355 | if (w == NULL) |
| 356 | return NULL; |
| 357 | res = (*v->ob_type->tp_getattro)(v, w); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 358 | Py_XDECREF(w); |
Guido van Rossum | d8eb1b3 | 1996-08-09 20:52:03 +0000 | [diff] [blame] | 359 | return res; |
| 360 | } |
| 361 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 362 | if (v->ob_type->tp_getattr == NULL) { |
Guido van Rossum | db93516 | 1998-01-19 22:16:36 +0000 | [diff] [blame] | 363 | PyErr_Format(PyExc_AttributeError, |
| 364 | "'%.50s' object has no attribute '%.400s'", |
| 365 | v->ob_type->tp_name, |
| 366 | name); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 367 | return NULL; |
| 368 | } |
| 369 | else { |
| 370 | return (*v->ob_type->tp_getattr)(v, name); |
| 371 | } |
| 372 | } |
| 373 | |
| 374 | int |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 375 | PyObject_HasAttrString(v, name) |
| 376 | PyObject *v; |
Guido van Rossum | ed18fdc | 1993-07-11 19:55:34 +0000 | [diff] [blame] | 377 | char *name; |
| 378 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 379 | PyObject *res = PyObject_GetAttrString(v, name); |
Guido van Rossum | ed18fdc | 1993-07-11 19:55:34 +0000 | [diff] [blame] | 380 | if (res != NULL) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 381 | Py_DECREF(res); |
Guido van Rossum | ed18fdc | 1993-07-11 19:55:34 +0000 | [diff] [blame] | 382 | return 1; |
| 383 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 384 | PyErr_Clear(); |
Guido van Rossum | ed18fdc | 1993-07-11 19:55:34 +0000 | [diff] [blame] | 385 | return 0; |
| 386 | } |
| 387 | |
| 388 | int |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 389 | PyObject_SetAttrString(v, name, w) |
| 390 | PyObject *v; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 391 | char *name; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 392 | PyObject *w; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 393 | { |
Guido van Rossum | d8eb1b3 | 1996-08-09 20:52:03 +0000 | [diff] [blame] | 394 | if (v->ob_type->tp_setattro != NULL) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 395 | PyObject *s; |
Guido van Rossum | d8eb1b3 | 1996-08-09 20:52:03 +0000 | [diff] [blame] | 396 | int res; |
Guido van Rossum | c6d0670 | 1997-01-18 07:57:16 +0000 | [diff] [blame] | 397 | s = PyString_InternFromString(name); |
Guido van Rossum | d8eb1b3 | 1996-08-09 20:52:03 +0000 | [diff] [blame] | 398 | if (s == NULL) |
Guido van Rossum | b7fc304 | 1996-09-11 22:51:25 +0000 | [diff] [blame] | 399 | return -1; |
Guido van Rossum | d8eb1b3 | 1996-08-09 20:52:03 +0000 | [diff] [blame] | 400 | res = (*v->ob_type->tp_setattro)(v, s, w); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 401 | Py_XDECREF(s); |
Guido van Rossum | d8eb1b3 | 1996-08-09 20:52:03 +0000 | [diff] [blame] | 402 | return res; |
| 403 | } |
| 404 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 405 | if (v->ob_type->tp_setattr == NULL) { |
| 406 | if (v->ob_type->tp_getattr == NULL) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 407 | PyErr_SetString(PyExc_TypeError, |
Guido van Rossum | 3ea7412 | 1991-12-24 13:28:03 +0000 | [diff] [blame] | 408 | "attribute-less object (assign or del)"); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 409 | else |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 410 | PyErr_SetString(PyExc_TypeError, |
Guido van Rossum | 3ea7412 | 1991-12-24 13:28:03 +0000 | [diff] [blame] | 411 | "object has read-only attributes"); |
Guido van Rossum | 73531a3 | 1990-12-20 23:12:40 +0000 | [diff] [blame] | 412 | return -1; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 413 | } |
| 414 | else { |
| 415 | return (*v->ob_type->tp_setattr)(v, name, w); |
| 416 | } |
| 417 | } |
| 418 | |
Guido van Rossum | 98ff96a | 1997-05-20 18:34:44 +0000 | [diff] [blame] | 419 | PyObject * |
| 420 | PyObject_GetAttr(v, name) |
| 421 | PyObject *v; |
| 422 | PyObject *name; |
| 423 | { |
| 424 | if (v->ob_type->tp_getattro != NULL) |
| 425 | return (*v->ob_type->tp_getattro)(v, name); |
| 426 | else |
| 427 | return PyObject_GetAttrString(v, PyString_AsString(name)); |
| 428 | } |
| 429 | |
| 430 | int |
| 431 | PyObject_HasAttr(v, name) |
| 432 | PyObject *v; |
| 433 | PyObject *name; |
| 434 | { |
| 435 | PyObject *res = PyObject_GetAttr(v, name); |
| 436 | if (res != NULL) { |
| 437 | Py_DECREF(res); |
| 438 | return 1; |
| 439 | } |
| 440 | PyErr_Clear(); |
| 441 | return 0; |
| 442 | } |
| 443 | |
| 444 | int |
| 445 | PyObject_SetAttr(v, name, value) |
| 446 | PyObject *v; |
| 447 | PyObject *name; |
| 448 | PyObject *value; |
| 449 | { |
| 450 | int err; |
| 451 | Py_INCREF(name); |
| 452 | PyString_InternInPlace(&name); |
| 453 | if (v->ob_type->tp_setattro != NULL) |
| 454 | err = (*v->ob_type->tp_setattro)(v, name, value); |
| 455 | else |
| 456 | err = PyObject_SetAttrString( |
| 457 | v, PyString_AsString(name), value); |
| 458 | Py_DECREF(name); |
| 459 | return err; |
| 460 | } |
| 461 | |
Guido van Rossum | 6ac258d | 1993-05-12 08:24:20 +0000 | [diff] [blame] | 462 | /* Test a value used as condition, e.g., in a for or if statement. |
| 463 | Return -1 if an error occurred */ |
| 464 | |
| 465 | int |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 466 | PyObject_IsTrue(v) |
| 467 | PyObject *v; |
Guido van Rossum | 6ac258d | 1993-05-12 08:24:20 +0000 | [diff] [blame] | 468 | { |
| 469 | int res; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 470 | if (v == Py_None) |
Guido van Rossum | 6ac258d | 1993-05-12 08:24:20 +0000 | [diff] [blame] | 471 | res = 0; |
Guido van Rossum | 1c4f458 | 1998-05-22 00:53:24 +0000 | [diff] [blame] | 472 | else if (v->ob_type->tp_as_number != NULL && |
| 473 | v->ob_type->tp_as_number->nb_nonzero != NULL) |
Guido van Rossum | 6ac258d | 1993-05-12 08:24:20 +0000 | [diff] [blame] | 474 | res = (*v->ob_type->tp_as_number->nb_nonzero)(v); |
Guido van Rossum | 1c4f458 | 1998-05-22 00:53:24 +0000 | [diff] [blame] | 475 | else if (v->ob_type->tp_as_mapping != NULL && |
| 476 | v->ob_type->tp_as_mapping->mp_length != NULL) |
Guido van Rossum | 6ac258d | 1993-05-12 08:24:20 +0000 | [diff] [blame] | 477 | res = (*v->ob_type->tp_as_mapping->mp_length)(v); |
Guido van Rossum | 1c4f458 | 1998-05-22 00:53:24 +0000 | [diff] [blame] | 478 | else if (v->ob_type->tp_as_sequence != NULL && |
| 479 | v->ob_type->tp_as_sequence->sq_length != NULL) |
Guido van Rossum | 6ac258d | 1993-05-12 08:24:20 +0000 | [diff] [blame] | 480 | res = (*v->ob_type->tp_as_sequence->sq_length)(v); |
| 481 | else |
| 482 | res = 1; |
| 483 | if (res > 0) |
| 484 | res = 1; |
| 485 | return res; |
| 486 | } |
| 487 | |
Guido van Rossum | c3d3f96 | 1998-04-09 17:53:59 +0000 | [diff] [blame] | 488 | /* equivalent of 'not v' |
| 489 | Return -1 if an error occurred */ |
| 490 | |
| 491 | int |
| 492 | PyObject_Not(v) |
| 493 | PyObject *v; |
| 494 | { |
| 495 | int res; |
| 496 | res = PyObject_IsTrue(v); |
| 497 | if (res < 0) |
| 498 | return res; |
| 499 | return res == 0; |
| 500 | } |
| 501 | |
Guido van Rossum | 5524a59 | 1995-01-10 15:26:20 +0000 | [diff] [blame] | 502 | /* Coerce two numeric types to the "larger" one. |
| 503 | Increment the reference count on each argument. |
| 504 | Return -1 and raise an exception if no coercion is possible |
| 505 | (and then no reference count is incremented). |
| 506 | */ |
| 507 | |
| 508 | int |
Guido van Rossum | 242c642 | 1997-11-19 16:03:17 +0000 | [diff] [blame] | 509 | PyNumber_CoerceEx(pv, pw) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 510 | PyObject **pv, **pw; |
Guido van Rossum | 5524a59 | 1995-01-10 15:26:20 +0000 | [diff] [blame] | 511 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 512 | register PyObject *v = *pv; |
| 513 | register PyObject *w = *pw; |
Guido van Rossum | 5524a59 | 1995-01-10 15:26:20 +0000 | [diff] [blame] | 514 | int res; |
| 515 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 516 | if (v->ob_type == w->ob_type && !PyInstance_Check(v)) { |
| 517 | Py_INCREF(v); |
| 518 | Py_INCREF(w); |
Guido van Rossum | 5524a59 | 1995-01-10 15:26:20 +0000 | [diff] [blame] | 519 | return 0; |
| 520 | } |
| 521 | if (v->ob_type->tp_as_number && v->ob_type->tp_as_number->nb_coerce) { |
| 522 | res = (*v->ob_type->tp_as_number->nb_coerce)(pv, pw); |
| 523 | if (res <= 0) |
| 524 | return res; |
| 525 | } |
| 526 | if (w->ob_type->tp_as_number && w->ob_type->tp_as_number->nb_coerce) { |
| 527 | res = (*w->ob_type->tp_as_number->nb_coerce)(pw, pv); |
| 528 | if (res <= 0) |
| 529 | return res; |
| 530 | } |
Guido van Rossum | 242c642 | 1997-11-19 16:03:17 +0000 | [diff] [blame] | 531 | return 1; |
| 532 | } |
| 533 | |
| 534 | int |
| 535 | PyNumber_Coerce(pv, pw) |
| 536 | PyObject **pv, **pw; |
| 537 | { |
| 538 | int err = PyNumber_CoerceEx(pv, pw); |
| 539 | if (err <= 0) |
| 540 | return err; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 541 | PyErr_SetString(PyExc_TypeError, "number coercion failed"); |
Guido van Rossum | 5524a59 | 1995-01-10 15:26:20 +0000 | [diff] [blame] | 542 | return -1; |
| 543 | } |
| 544 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 545 | |
Guido van Rossum | 49b11fe | 1995-01-26 00:38:22 +0000 | [diff] [blame] | 546 | /* Test whether an object can be called */ |
| 547 | |
| 548 | int |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 549 | PyCallable_Check(x) |
| 550 | PyObject *x; |
Guido van Rossum | 49b11fe | 1995-01-26 00:38:22 +0000 | [diff] [blame] | 551 | { |
| 552 | if (x == NULL) |
| 553 | return 0; |
| 554 | if (x->ob_type->tp_call != NULL || |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 555 | PyFunction_Check(x) || |
| 556 | PyMethod_Check(x) || |
| 557 | PyCFunction_Check(x) || |
| 558 | PyClass_Check(x)) |
Guido van Rossum | 49b11fe | 1995-01-26 00:38:22 +0000 | [diff] [blame] | 559 | return 1; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 560 | if (PyInstance_Check(x)) { |
| 561 | PyObject *call = PyObject_GetAttrString(x, "__call__"); |
Guido van Rossum | 49b11fe | 1995-01-26 00:38:22 +0000 | [diff] [blame] | 562 | if (call == NULL) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 563 | PyErr_Clear(); |
Guido van Rossum | 49b11fe | 1995-01-26 00:38:22 +0000 | [diff] [blame] | 564 | return 0; |
| 565 | } |
| 566 | /* Could test recursively but don't, for fear of endless |
| 567 | recursion if some joker sets self.__call__ = self */ |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 568 | Py_DECREF(call); |
Guido van Rossum | 49b11fe | 1995-01-26 00:38:22 +0000 | [diff] [blame] | 569 | return 1; |
| 570 | } |
| 571 | return 0; |
| 572 | } |
| 573 | |
| 574 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 575 | /* |
| 576 | NoObject is usable as a non-NULL undefined value, used by the macro None. |
| 577 | 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] | 578 | so there is exactly one (which is indestructible, by the way). |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 579 | */ |
| 580 | |
Guido van Rossum | 0c182a1 | 1992-03-27 17:26:13 +0000 | [diff] [blame] | 581 | /* ARGSUSED */ |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 582 | static PyObject * |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 583 | none_repr(op) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 584 | PyObject *op; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 585 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 586 | return PyString_FromString("None"); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 587 | } |
| 588 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 589 | static PyTypeObject PyNothing_Type = { |
| 590 | PyObject_HEAD_INIT(&PyType_Type) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 591 | 0, |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 592 | "None", |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 593 | 0, |
| 594 | 0, |
| 595 | 0, /*tp_dealloc*/ /*never called*/ |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 596 | 0, /*tp_print*/ |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 597 | 0, /*tp_getattr*/ |
| 598 | 0, /*tp_setattr*/ |
| 599 | 0, /*tp_compare*/ |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 600 | (reprfunc)none_repr, /*tp_repr*/ |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 601 | 0, /*tp_as_number*/ |
| 602 | 0, /*tp_as_sequence*/ |
| 603 | 0, /*tp_as_mapping*/ |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 604 | 0, /*tp_hash */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 605 | }; |
| 606 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 607 | PyObject _Py_NoneStruct = { |
| 608 | PyObject_HEAD_INIT(&PyNothing_Type) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 609 | }; |
| 610 | |
| 611 | |
Guido van Rossum | 84a9032 | 1996-05-22 16:34:47 +0000 | [diff] [blame] | 612 | #ifdef Py_TRACE_REFS |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 613 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 614 | static PyObject refchain = {&refchain, &refchain}; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 615 | |
Guido van Rossum | aacdc9d | 1996-08-12 21:32:12 +0000 | [diff] [blame] | 616 | void |
Guido van Rossum | e09fb55 | 1997-08-05 02:04:34 +0000 | [diff] [blame] | 617 | _Py_ResetReferences() |
| 618 | { |
| 619 | refchain._ob_prev = refchain._ob_next = &refchain; |
| 620 | _Py_RefTotal = 0; |
| 621 | } |
| 622 | |
| 623 | void |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 624 | _Py_NewReference(op) |
| 625 | PyObject *op; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 626 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 627 | _Py_RefTotal++; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 628 | op->ob_refcnt = 1; |
| 629 | op->_ob_next = refchain._ob_next; |
| 630 | op->_ob_prev = &refchain; |
| 631 | refchain._ob_next->_ob_prev = op; |
| 632 | refchain._ob_next = op; |
Sjoerd Mullender | a9c3c22 | 1993-10-11 12:54:31 +0000 | [diff] [blame] | 633 | #ifdef COUNT_ALLOCS |
| 634 | inc_count(op->ob_type); |
| 635 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 636 | } |
| 637 | |
Guido van Rossum | aacdc9d | 1996-08-12 21:32:12 +0000 | [diff] [blame] | 638 | void |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 639 | _Py_ForgetReference(op) |
| 640 | register PyObject *op; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 641 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 642 | register PyObject *p; |
Guido van Rossum | d7047b3 | 1995-01-02 19:07:15 +0000 | [diff] [blame] | 643 | if (op->ob_refcnt < 0) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 644 | Py_FatalError("UNREF negative refcnt"); |
Guido van Rossum | 2e8f614 | 1992-09-03 20:32:55 +0000 | [diff] [blame] | 645 | if (op == &refchain || |
Guido van Rossum | d7047b3 | 1995-01-02 19:07:15 +0000 | [diff] [blame] | 646 | 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] | 647 | Py_FatalError("UNREF invalid object"); |
Guido van Rossum | 2e8f614 | 1992-09-03 20:32:55 +0000 | [diff] [blame] | 648 | #ifdef SLOW_UNREF_CHECK |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 649 | for (p = refchain._ob_next; p != &refchain; p = p->_ob_next) { |
| 650 | if (p == op) |
| 651 | break; |
| 652 | } |
Guido van Rossum | d7047b3 | 1995-01-02 19:07:15 +0000 | [diff] [blame] | 653 | if (p == &refchain) /* Not found */ |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 654 | Py_FatalError("UNREF unknown object"); |
Guido van Rossum | 2e8f614 | 1992-09-03 20:32:55 +0000 | [diff] [blame] | 655 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 656 | op->_ob_next->_ob_prev = op->_ob_prev; |
| 657 | op->_ob_prev->_ob_next = op->_ob_next; |
Guido van Rossum | 2e8f614 | 1992-09-03 20:32:55 +0000 | [diff] [blame] | 658 | op->_ob_next = op->_ob_prev = NULL; |
Guido van Rossum | d8953cb | 1995-04-06 14:46:26 +0000 | [diff] [blame] | 659 | #ifdef COUNT_ALLOCS |
| 660 | op->ob_type->tp_free++; |
| 661 | #endif |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 662 | } |
| 663 | |
Guido van Rossum | aacdc9d | 1996-08-12 21:32:12 +0000 | [diff] [blame] | 664 | void |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 665 | _Py_Dealloc(op) |
| 666 | PyObject *op; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 667 | { |
Guido van Rossum | 9776adf | 1994-09-07 14:36:45 +0000 | [diff] [blame] | 668 | destructor dealloc = op->ob_type->tp_dealloc; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 669 | _Py_ForgetReference(op); |
Guido van Rossum | 2e8f614 | 1992-09-03 20:32:55 +0000 | [diff] [blame] | 670 | op->ob_type = NULL; |
Guido van Rossum | 9776adf | 1994-09-07 14:36:45 +0000 | [diff] [blame] | 671 | (*dealloc)(op); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 672 | } |
| 673 | |
Guido van Rossum | aacdc9d | 1996-08-12 21:32:12 +0000 | [diff] [blame] | 674 | void |
Guido van Rossum | ded690f | 1996-05-24 20:48:31 +0000 | [diff] [blame] | 675 | _Py_PrintReferences(fp) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 676 | FILE *fp; |
| 677 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 678 | PyObject *op; |
Guido van Rossum | e09fb55 | 1997-08-05 02:04:34 +0000 | [diff] [blame] | 679 | fprintf(fp, "Remaining objects:\n"); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 680 | for (op = refchain._ob_next; op != &refchain; op = op->_ob_next) { |
| 681 | fprintf(fp, "[%d] ", op->ob_refcnt); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 682 | if (PyObject_Print(op, fp, 0) != 0) |
| 683 | PyErr_Clear(); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 684 | putc('\n', fp); |
| 685 | } |
| 686 | } |
| 687 | |
Sjoerd Mullender | 6ec3c65 | 1995-08-29 09:18:14 +0000 | [diff] [blame] | 688 | PyObject * |
Guido van Rossum | ded690f | 1996-05-24 20:48:31 +0000 | [diff] [blame] | 689 | _Py_GetObjects(self, args) |
Sjoerd Mullender | 6ec3c65 | 1995-08-29 09:18:14 +0000 | [diff] [blame] | 690 | PyObject *self; |
| 691 | PyObject *args; |
| 692 | { |
| 693 | int i, n; |
| 694 | PyObject *t = NULL; |
| 695 | PyObject *res, *op; |
| 696 | |
| 697 | if (!PyArg_ParseTuple(args, "i|O", &n, &t)) |
| 698 | return NULL; |
| 699 | op = refchain._ob_next; |
| 700 | res = PyList_New(0); |
| 701 | if (res == NULL) |
| 702 | return NULL; |
| 703 | for (i = 0; (n == 0 || i < n) && op != &refchain; i++) { |
| 704 | while (op == self || op == args || op == res || op == t || |
| 705 | t != NULL && op->ob_type != (PyTypeObject *) t) { |
| 706 | op = op->_ob_next; |
| 707 | if (op == &refchain) |
| 708 | return res; |
| 709 | } |
| 710 | if (PyList_Append(res, op) < 0) { |
| 711 | Py_DECREF(res); |
| 712 | return NULL; |
| 713 | } |
| 714 | op = op->_ob_next; |
| 715 | } |
| 716 | return res; |
| 717 | } |
| 718 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 719 | #endif |
Guido van Rossum | 97ead3f | 1996-01-12 01:24:09 +0000 | [diff] [blame] | 720 | |
| 721 | |
| 722 | /* Hack to force loading of cobject.o */ |
Guido van Rossum | da9c271 | 1996-12-05 21:58:58 +0000 | [diff] [blame] | 723 | PyTypeObject *_Py_cobject_hack = &PyCObject_Type; |
Guido van Rossum | 84a9032 | 1996-05-22 16:34:47 +0000 | [diff] [blame] | 724 | |
| 725 | |
| 726 | /* Hack to force loading of abstract.o */ |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 727 | int (*_Py_abstract_hack) Py_FPROTO((PyObject *)) = &PyObject_Length; |
Guido van Rossum | e09fb55 | 1997-08-05 02:04:34 +0000 | [diff] [blame] | 728 | |
| 729 | |
| 730 | /* Malloc wrappers (see mymalloc.h) */ |
| 731 | |
| 732 | /* The Py_{Malloc,Realloc} wrappers call PyErr_NoMemory() on failure */ |
| 733 | |
| 734 | ANY * |
| 735 | Py_Malloc(nbytes) |
| 736 | size_t nbytes; |
| 737 | { |
| 738 | ANY *p; |
| 739 | #if _PyMem_EXTRA > 0 |
| 740 | if (nbytes == 0) |
| 741 | nbytes = _PyMem_EXTRA; |
| 742 | #endif |
| 743 | p = malloc(nbytes); |
| 744 | if (p != NULL) |
| 745 | return p; |
Guido van Rossum | ea46e4d | 1997-08-12 14:54:54 +0000 | [diff] [blame] | 746 | else { |
| 747 | PyErr_NoMemory(); |
| 748 | return NULL; |
| 749 | } |
Guido van Rossum | e09fb55 | 1997-08-05 02:04:34 +0000 | [diff] [blame] | 750 | } |
| 751 | |
| 752 | ANY * |
| 753 | Py_Realloc(p, nbytes) |
| 754 | ANY *p; |
| 755 | size_t nbytes; |
| 756 | { |
| 757 | #if _PyMem_EXTRA > 0 |
| 758 | if (nbytes == 0) |
| 759 | nbytes = _PyMem_EXTRA; |
| 760 | #endif |
| 761 | p = realloc(p, nbytes); |
| 762 | if (p != NULL) |
| 763 | return p; |
Guido van Rossum | ea46e4d | 1997-08-12 14:54:54 +0000 | [diff] [blame] | 764 | else { |
| 765 | PyErr_NoMemory(); |
| 766 | return NULL; |
| 767 | } |
Guido van Rossum | e09fb55 | 1997-08-05 02:04:34 +0000 | [diff] [blame] | 768 | } |
| 769 | |
| 770 | void |
| 771 | Py_Free(p) |
| 772 | ANY *p; |
| 773 | { |
| 774 | free(p); |
| 775 | } |
| 776 | |
| 777 | /* The PyMem_{Malloc,Realloc} wrappers don't call anything on failure */ |
| 778 | |
| 779 | ANY * |
| 780 | PyMem_Malloc(nbytes) |
| 781 | size_t nbytes; |
| 782 | { |
| 783 | #if _PyMem_EXTRA > 0 |
| 784 | if (nbytes == 0) |
| 785 | nbytes = _PyMem_EXTRA; |
| 786 | #endif |
| 787 | return malloc(nbytes); |
| 788 | } |
| 789 | |
| 790 | ANY * |
| 791 | PyMem_Realloc(p, nbytes) |
| 792 | ANY *p; |
| 793 | size_t nbytes; |
| 794 | { |
| 795 | #if _PyMem_EXTRA > 0 |
| 796 | if (nbytes == 0) |
| 797 | nbytes = _PyMem_EXTRA; |
| 798 | #endif |
| 799 | return realloc(p, nbytes); |
| 800 | } |
| 801 | |
| 802 | void |
| 803 | PyMem_Free(p) |
| 804 | ANY *p; |
| 805 | { |
| 806 | free(p); |
| 807 | } |
Guido van Rossum | 8661036 | 1998-04-10 22:32:46 +0000 | [diff] [blame] | 808 | |
| 809 | |
| 810 | /* These methods are used to control infinite recursion in repr, str, print, |
| 811 | etc. Container objects that may recursively contain themselves, |
| 812 | e.g. builtin dictionaries and lists, should used Py_ReprEnter() and |
| 813 | Py_ReprLeave() to avoid infinite recursion. |
| 814 | |
| 815 | Py_ReprEnter() returns 0 the first time it is called for a particular |
| 816 | object and 1 every time thereafter. It returns -1 if an exception |
| 817 | occurred. Py_ReprLeave() has no return value. |
| 818 | |
| 819 | See dictobject.c and listobject.c for examples of use. |
| 820 | */ |
| 821 | |
| 822 | #define KEY "Py_Repr" |
| 823 | |
| 824 | int |
| 825 | Py_ReprEnter(obj) |
| 826 | PyObject *obj; |
| 827 | { |
| 828 | PyObject *dict; |
| 829 | PyObject *list; |
| 830 | int i; |
| 831 | |
| 832 | dict = PyThreadState_GetDict(); |
| 833 | if (dict == NULL) |
| 834 | return -1; |
| 835 | list = PyDict_GetItemString(dict, KEY); |
| 836 | if (list == NULL) { |
| 837 | list = PyList_New(0); |
| 838 | if (list == NULL) |
| 839 | return -1; |
| 840 | if (PyDict_SetItemString(dict, KEY, list) < 0) |
| 841 | return -1; |
| 842 | Py_DECREF(list); |
| 843 | } |
| 844 | i = PyList_GET_SIZE(list); |
| 845 | while (--i >= 0) { |
| 846 | if (PyList_GET_ITEM(list, i) == obj) |
| 847 | return 1; |
| 848 | } |
| 849 | PyList_Append(list, obj); |
| 850 | return 0; |
| 851 | } |
| 852 | |
| 853 | void |
| 854 | Py_ReprLeave(obj) |
| 855 | PyObject *obj; |
| 856 | { |
| 857 | PyObject *dict; |
| 858 | PyObject *list; |
| 859 | int i; |
| 860 | |
| 861 | dict = PyThreadState_GetDict(); |
Guido van Rossum | eb90946 | 1998-04-11 15:17:34 +0000 | [diff] [blame] | 862 | if (dict == NULL) |
| 863 | return; |
Guido van Rossum | 8661036 | 1998-04-10 22:32:46 +0000 | [diff] [blame] | 864 | list = PyDict_GetItemString(dict, KEY); |
Guido van Rossum | eb90946 | 1998-04-11 15:17:34 +0000 | [diff] [blame] | 865 | if (list == NULL || !PyList_Check(list)) |
| 866 | return; |
Guido van Rossum | 8661036 | 1998-04-10 22:32:46 +0000 | [diff] [blame] | 867 | i = PyList_GET_SIZE(list); |
| 868 | /* Count backwards because we always expect obj to be list[-1] */ |
| 869 | while (--i >= 0) { |
| 870 | if (PyList_GET_ITEM(list, i) == obj) { |
| 871 | PyList_SetSlice(list, i, i + 1, NULL); |
| 872 | break; |
| 873 | } |
| 874 | } |
| 875 | } |