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 | 9093361 | 1991-06-07 16:10:43 +0000 | [diff] [blame] | 165 | if (op == NULL) { |
| 166 | fprintf(fp, "<nil>"); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 167 | } |
Guido van Rossum | 9093361 | 1991-06-07 16:10:43 +0000 | [diff] [blame] | 168 | else { |
| 169 | if (op->ob_refcnt <= 0) |
Guido van Rossum | 2e8f614 | 1992-09-03 20:32:55 +0000 | [diff] [blame] | 170 | fprintf(fp, "<refcnt %u at %lx>", |
| 171 | op->ob_refcnt, (long)op); |
| 172 | else if (op->ob_type->tp_print == NULL) { |
| 173 | if (op->ob_type->tp_repr == NULL) { |
| 174 | fprintf(fp, "<%s object at %lx>", |
| 175 | op->ob_type->tp_name, (long)op); |
| 176 | } |
| 177 | else { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 178 | PyObject *s; |
| 179 | if (flags & Py_PRINT_RAW) |
| 180 | s = PyObject_Str(op); |
Guido van Rossum | c600411 | 1993-11-05 10:22:19 +0000 | [diff] [blame] | 181 | else |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 182 | s = PyObject_Repr(op); |
Guido van Rossum | 2e8f614 | 1992-09-03 20:32:55 +0000 | [diff] [blame] | 183 | if (s == NULL) |
| 184 | ret = -1; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 185 | else if (!PyString_Check(s)) { |
| 186 | PyErr_SetString(PyExc_TypeError, |
Guido van Rossum | 2e8f614 | 1992-09-03 20:32:55 +0000 | [diff] [blame] | 187 | "repr not string"); |
| 188 | ret = -1; |
| 189 | } |
| 190 | else { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 191 | fprintf(fp, "%s", |
| 192 | PyString_AsString(s)); |
Guido van Rossum | 2e8f614 | 1992-09-03 20:32:55 +0000 | [diff] [blame] | 193 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 194 | Py_XDECREF(s); |
Guido van Rossum | 2e8f614 | 1992-09-03 20:32:55 +0000 | [diff] [blame] | 195 | } |
| 196 | } |
Guido van Rossum | 9093361 | 1991-06-07 16:10:43 +0000 | [diff] [blame] | 197 | else |
Guido van Rossum | 278ef59 | 1991-07-27 21:40:24 +0000 | [diff] [blame] | 198 | ret = (*op->ob_type->tp_print)(op, fp, flags); |
Guido van Rossum | 9093361 | 1991-06-07 16:10:43 +0000 | [diff] [blame] | 199 | } |
Guido van Rossum | 278ef59 | 1991-07-27 21:40:24 +0000 | [diff] [blame] | 200 | if (ret == 0) { |
| 201 | if (ferror(fp)) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 202 | PyErr_SetFromErrno(PyExc_IOError); |
Guido van Rossum | 278ef59 | 1991-07-27 21:40:24 +0000 | [diff] [blame] | 203 | clearerr(fp); |
| 204 | ret = -1; |
| 205 | } |
| 206 | } |
| 207 | return ret; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 208 | } |
| 209 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 210 | PyObject * |
| 211 | PyObject_Repr(v) |
| 212 | PyObject *v; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 213 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 214 | if (PyErr_CheckSignals()) |
Guido van Rossum | 9093361 | 1991-06-07 16:10:43 +0000 | [diff] [blame] | 215 | return NULL; |
Guido van Rossum | 9093361 | 1991-06-07 16:10:43 +0000 | [diff] [blame] | 216 | if (v == NULL) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 217 | return PyString_FromString("<NULL>"); |
Guido van Rossum | 9093361 | 1991-06-07 16:10:43 +0000 | [diff] [blame] | 218 | else if (v->ob_type->tp_repr == NULL) { |
| 219 | char buf[120]; |
| 220 | sprintf(buf, "<%.80s object at %lx>", |
| 221 | v->ob_type->tp_name, (long)v); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 222 | return PyString_FromString(buf); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 223 | } |
Guido van Rossum | 9093361 | 1991-06-07 16:10:43 +0000 | [diff] [blame] | 224 | else |
| 225 | return (*v->ob_type->tp_repr)(v); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 226 | } |
| 227 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 228 | PyObject * |
| 229 | PyObject_Str(v) |
| 230 | PyObject *v; |
Guido van Rossum | c600411 | 1993-11-05 10:22:19 +0000 | [diff] [blame] | 231 | { |
| 232 | if (v == NULL) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 233 | return PyString_FromString("<NULL>"); |
| 234 | else if (PyString_Check(v)) { |
| 235 | Py_INCREF(v); |
Guido van Rossum | c600411 | 1993-11-05 10:22:19 +0000 | [diff] [blame] | 236 | return v; |
| 237 | } |
Guido van Rossum | 32b582b | 1995-01-17 16:35:13 +0000 | [diff] [blame] | 238 | else if (v->ob_type->tp_str != NULL) |
| 239 | return (*v->ob_type->tp_str)(v); |
Guido van Rossum | c600411 | 1993-11-05 10:22:19 +0000 | [diff] [blame] | 240 | else { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 241 | PyObject *func; |
| 242 | PyObject *res; |
| 243 | if (!PyInstance_Check(v) || |
| 244 | (func = PyObject_GetAttrString(v, "__str__")) == NULL) { |
| 245 | PyErr_Clear(); |
| 246 | return PyObject_Repr(v); |
Guido van Rossum | c600411 | 1993-11-05 10:22:19 +0000 | [diff] [blame] | 247 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 248 | res = PyEval_CallObject(func, (PyObject *)NULL); |
| 249 | Py_DECREF(func); |
Guido van Rossum | c600411 | 1993-11-05 10:22:19 +0000 | [diff] [blame] | 250 | return res; |
| 251 | } |
| 252 | } |
| 253 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 254 | static PyObject * |
Guido van Rossum | 2056684 | 1995-01-12 11:26:10 +0000 | [diff] [blame] | 255 | do_cmp(v, w) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 256 | PyObject *v, *w; |
Guido van Rossum | 2056684 | 1995-01-12 11:26:10 +0000 | [diff] [blame] | 257 | { |
Guido van Rossum | c8b6df9 | 1997-05-23 00:06:51 +0000 | [diff] [blame] | 258 | long c; |
Guido van Rossum | 2056684 | 1995-01-12 11:26:10 +0000 | [diff] [blame] | 259 | /* __rcmp__ actually won't be called unless __cmp__ isn't defined, |
| 260 | because the check in cmpobject() reverses the objects first. |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 261 | This is intentional -- it makes no sense to define cmp(x,y) |
| 262 | different than -cmp(y,x). */ |
| 263 | if (PyInstance_Check(v) || PyInstance_Check(w)) |
| 264 | return PyInstance_DoBinOp(v, w, "__cmp__", "__rcmp__", do_cmp); |
Guido van Rossum | c8b6df9 | 1997-05-23 00:06:51 +0000 | [diff] [blame] | 265 | c = PyObject_Compare(v, w); |
| 266 | if (c && PyErr_Occurred()) |
| 267 | return NULL; |
| 268 | return PyInt_FromLong(c); |
Guido van Rossum | 2056684 | 1995-01-12 11:26:10 +0000 | [diff] [blame] | 269 | } |
| 270 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 271 | int |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 272 | PyObject_Compare(v, w) |
| 273 | PyObject *v, *w; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 274 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 275 | PyTypeObject *tp; |
Guido van Rossum | c8b6df9 | 1997-05-23 00:06:51 +0000 | [diff] [blame] | 276 | if (v == NULL || w == NULL) { |
| 277 | PyErr_BadInternalCall(); |
| 278 | return -1; |
| 279 | } |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 280 | if (v == w) |
| 281 | return 0; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 282 | if (PyInstance_Check(v) || PyInstance_Check(w)) { |
| 283 | PyObject *res; |
Guido van Rossum | 2056684 | 1995-01-12 11:26:10 +0000 | [diff] [blame] | 284 | int c; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 285 | if (!PyInstance_Check(v)) |
| 286 | return -PyObject_Compare(w, v); |
Guido van Rossum | 2056684 | 1995-01-12 11:26:10 +0000 | [diff] [blame] | 287 | res = do_cmp(v, w); |
Guido van Rossum | c8b6df9 | 1997-05-23 00:06:51 +0000 | [diff] [blame] | 288 | if (res == NULL) |
| 289 | return -1; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 290 | if (!PyInt_Check(res)) { |
| 291 | Py_DECREF(res); |
Guido van Rossum | c8b6df9 | 1997-05-23 00:06:51 +0000 | [diff] [blame] | 292 | PyErr_SetString(PyExc_TypeError, |
| 293 | "comparison did not return an int"); |
| 294 | return -1; |
Guido van Rossum | 2056684 | 1995-01-12 11:26:10 +0000 | [diff] [blame] | 295 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 296 | c = PyInt_AsLong(res); |
| 297 | Py_DECREF(res); |
Guido van Rossum | 2056684 | 1995-01-12 11:26:10 +0000 | [diff] [blame] | 298 | return (c < 0) ? -1 : (c > 0) ? 1 : 0; |
| 299 | } |
Guido van Rossum | 9fb0368 | 1991-07-01 18:48:04 +0000 | [diff] [blame] | 300 | if ((tp = v->ob_type) != w->ob_type) { |
| 301 | if (tp->tp_as_number != NULL && |
| 302 | w->ob_type->tp_as_number != NULL) { |
Guido van Rossum | c8b6df9 | 1997-05-23 00:06:51 +0000 | [diff] [blame] | 303 | if (PyNumber_Coerce(&v, &w) != 0) |
| 304 | return -1; |
Guido van Rossum | 9fb0368 | 1991-07-01 18:48:04 +0000 | [diff] [blame] | 305 | else { |
| 306 | int cmp = (*v->ob_type->tp_compare)(v, w); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 307 | Py_DECREF(v); |
| 308 | Py_DECREF(w); |
Guido van Rossum | 9fb0368 | 1991-07-01 18:48:04 +0000 | [diff] [blame] | 309 | return cmp; |
| 310 | } |
| 311 | } |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 312 | return strcmp(tp->tp_name, w->ob_type->tp_name); |
Guido van Rossum | 9fb0368 | 1991-07-01 18:48:04 +0000 | [diff] [blame] | 313 | } |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 314 | if (tp->tp_compare == NULL) |
| 315 | return (v < w) ? -1 : 1; |
Guido van Rossum | 9fb0368 | 1991-07-01 18:48:04 +0000 | [diff] [blame] | 316 | return (*tp->tp_compare)(v, w); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 317 | } |
| 318 | |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 319 | long |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 320 | PyObject_Hash(v) |
| 321 | PyObject *v; |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 322 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 323 | PyTypeObject *tp = v->ob_type; |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 324 | if (tp->tp_hash != NULL) |
| 325 | return (*tp->tp_hash)(v); |
| 326 | if (tp->tp_compare == NULL) |
| 327 | return (long) v; /* Use address as hash value */ |
| 328 | /* 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] | 329 | PyErr_SetString(PyExc_TypeError, "unhashable type"); |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 330 | return -1; |
| 331 | } |
| 332 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 333 | PyObject * |
| 334 | PyObject_GetAttrString(v, name) |
| 335 | PyObject *v; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 336 | char *name; |
| 337 | { |
Guido van Rossum | d8eb1b3 | 1996-08-09 20:52:03 +0000 | [diff] [blame] | 338 | if (v->ob_type->tp_getattro != NULL) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 339 | PyObject *w, *res; |
Guido van Rossum | c6d0670 | 1997-01-18 07:57:16 +0000 | [diff] [blame] | 340 | w = PyString_InternFromString(name); |
Guido van Rossum | d8eb1b3 | 1996-08-09 20:52:03 +0000 | [diff] [blame] | 341 | if (w == NULL) |
| 342 | return NULL; |
| 343 | res = (*v->ob_type->tp_getattro)(v, w); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 344 | Py_XDECREF(w); |
Guido van Rossum | d8eb1b3 | 1996-08-09 20:52:03 +0000 | [diff] [blame] | 345 | return res; |
| 346 | } |
| 347 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 348 | if (v->ob_type->tp_getattr == NULL) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 349 | PyErr_SetString(PyExc_AttributeError, "attribute-less object"); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 350 | return NULL; |
| 351 | } |
| 352 | else { |
| 353 | return (*v->ob_type->tp_getattr)(v, name); |
| 354 | } |
| 355 | } |
| 356 | |
| 357 | int |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 358 | PyObject_HasAttrString(v, name) |
| 359 | PyObject *v; |
Guido van Rossum | ed18fdc | 1993-07-11 19:55:34 +0000 | [diff] [blame] | 360 | char *name; |
| 361 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 362 | PyObject *res = PyObject_GetAttrString(v, name); |
Guido van Rossum | ed18fdc | 1993-07-11 19:55:34 +0000 | [diff] [blame] | 363 | if (res != NULL) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 364 | Py_DECREF(res); |
Guido van Rossum | ed18fdc | 1993-07-11 19:55:34 +0000 | [diff] [blame] | 365 | return 1; |
| 366 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 367 | PyErr_Clear(); |
Guido van Rossum | ed18fdc | 1993-07-11 19:55:34 +0000 | [diff] [blame] | 368 | return 0; |
| 369 | } |
| 370 | |
| 371 | int |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 372 | PyObject_SetAttrString(v, name, w) |
| 373 | PyObject *v; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 374 | char *name; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 375 | PyObject *w; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 376 | { |
Guido van Rossum | d8eb1b3 | 1996-08-09 20:52:03 +0000 | [diff] [blame] | 377 | if (v->ob_type->tp_setattro != NULL) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 378 | PyObject *s; |
Guido van Rossum | d8eb1b3 | 1996-08-09 20:52:03 +0000 | [diff] [blame] | 379 | int res; |
Guido van Rossum | c6d0670 | 1997-01-18 07:57:16 +0000 | [diff] [blame] | 380 | s = PyString_InternFromString(name); |
Guido van Rossum | d8eb1b3 | 1996-08-09 20:52:03 +0000 | [diff] [blame] | 381 | if (s == NULL) |
Guido van Rossum | b7fc304 | 1996-09-11 22:51:25 +0000 | [diff] [blame] | 382 | return -1; |
Guido van Rossum | d8eb1b3 | 1996-08-09 20:52:03 +0000 | [diff] [blame] | 383 | res = (*v->ob_type->tp_setattro)(v, s, w); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 384 | Py_XDECREF(s); |
Guido van Rossum | d8eb1b3 | 1996-08-09 20:52:03 +0000 | [diff] [blame] | 385 | return res; |
| 386 | } |
| 387 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 388 | if (v->ob_type->tp_setattr == NULL) { |
| 389 | if (v->ob_type->tp_getattr == NULL) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 390 | PyErr_SetString(PyExc_TypeError, |
Guido van Rossum | 3ea7412 | 1991-12-24 13:28:03 +0000 | [diff] [blame] | 391 | "attribute-less object (assign or del)"); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 392 | else |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 393 | PyErr_SetString(PyExc_TypeError, |
Guido van Rossum | 3ea7412 | 1991-12-24 13:28:03 +0000 | [diff] [blame] | 394 | "object has read-only attributes"); |
Guido van Rossum | 73531a3 | 1990-12-20 23:12:40 +0000 | [diff] [blame] | 395 | return -1; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 396 | } |
| 397 | else { |
| 398 | return (*v->ob_type->tp_setattr)(v, name, w); |
| 399 | } |
| 400 | } |
| 401 | |
Guido van Rossum | 98ff96a | 1997-05-20 18:34:44 +0000 | [diff] [blame] | 402 | PyObject * |
| 403 | PyObject_GetAttr(v, name) |
| 404 | PyObject *v; |
| 405 | PyObject *name; |
| 406 | { |
| 407 | if (v->ob_type->tp_getattro != NULL) |
| 408 | return (*v->ob_type->tp_getattro)(v, name); |
| 409 | else |
| 410 | return PyObject_GetAttrString(v, PyString_AsString(name)); |
| 411 | } |
| 412 | |
| 413 | int |
| 414 | PyObject_HasAttr(v, name) |
| 415 | PyObject *v; |
| 416 | PyObject *name; |
| 417 | { |
| 418 | PyObject *res = PyObject_GetAttr(v, name); |
| 419 | if (res != NULL) { |
| 420 | Py_DECREF(res); |
| 421 | return 1; |
| 422 | } |
| 423 | PyErr_Clear(); |
| 424 | return 0; |
| 425 | } |
| 426 | |
| 427 | int |
| 428 | PyObject_SetAttr(v, name, value) |
| 429 | PyObject *v; |
| 430 | PyObject *name; |
| 431 | PyObject *value; |
| 432 | { |
| 433 | int err; |
| 434 | Py_INCREF(name); |
| 435 | PyString_InternInPlace(&name); |
| 436 | if (v->ob_type->tp_setattro != NULL) |
| 437 | err = (*v->ob_type->tp_setattro)(v, name, value); |
| 438 | else |
| 439 | err = PyObject_SetAttrString( |
| 440 | v, PyString_AsString(name), value); |
| 441 | Py_DECREF(name); |
| 442 | return err; |
| 443 | } |
| 444 | |
Guido van Rossum | 6ac258d | 1993-05-12 08:24:20 +0000 | [diff] [blame] | 445 | /* Test a value used as condition, e.g., in a for or if statement. |
| 446 | Return -1 if an error occurred */ |
| 447 | |
| 448 | int |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 449 | PyObject_IsTrue(v) |
| 450 | PyObject *v; |
Guido van Rossum | 6ac258d | 1993-05-12 08:24:20 +0000 | [diff] [blame] | 451 | { |
| 452 | int res; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 453 | if (v == Py_None) |
Guido van Rossum | 6ac258d | 1993-05-12 08:24:20 +0000 | [diff] [blame] | 454 | res = 0; |
| 455 | else if (v->ob_type->tp_as_number != NULL) |
| 456 | res = (*v->ob_type->tp_as_number->nb_nonzero)(v); |
| 457 | else if (v->ob_type->tp_as_mapping != NULL) |
| 458 | res = (*v->ob_type->tp_as_mapping->mp_length)(v); |
| 459 | else if (v->ob_type->tp_as_sequence != NULL) |
| 460 | res = (*v->ob_type->tp_as_sequence->sq_length)(v); |
| 461 | else |
| 462 | res = 1; |
| 463 | if (res > 0) |
| 464 | res = 1; |
| 465 | return res; |
| 466 | } |
| 467 | |
Guido van Rossum | 5524a59 | 1995-01-10 15:26:20 +0000 | [diff] [blame] | 468 | /* Coerce two numeric types to the "larger" one. |
| 469 | Increment the reference count on each argument. |
| 470 | Return -1 and raise an exception if no coercion is possible |
| 471 | (and then no reference count is incremented). |
| 472 | */ |
| 473 | |
| 474 | int |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 475 | PyNumber_Coerce(pv, pw) |
| 476 | PyObject **pv, **pw; |
Guido van Rossum | 5524a59 | 1995-01-10 15:26:20 +0000 | [diff] [blame] | 477 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 478 | register PyObject *v = *pv; |
| 479 | register PyObject *w = *pw; |
Guido van Rossum | 5524a59 | 1995-01-10 15:26:20 +0000 | [diff] [blame] | 480 | int res; |
| 481 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 482 | if (v->ob_type == w->ob_type && !PyInstance_Check(v)) { |
| 483 | Py_INCREF(v); |
| 484 | Py_INCREF(w); |
Guido van Rossum | 5524a59 | 1995-01-10 15:26:20 +0000 | [diff] [blame] | 485 | return 0; |
| 486 | } |
| 487 | if (v->ob_type->tp_as_number && v->ob_type->tp_as_number->nb_coerce) { |
| 488 | res = (*v->ob_type->tp_as_number->nb_coerce)(pv, pw); |
| 489 | if (res <= 0) |
| 490 | return res; |
| 491 | } |
| 492 | if (w->ob_type->tp_as_number && w->ob_type->tp_as_number->nb_coerce) { |
| 493 | res = (*w->ob_type->tp_as_number->nb_coerce)(pw, pv); |
| 494 | if (res <= 0) |
| 495 | return res; |
| 496 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 497 | PyErr_SetString(PyExc_TypeError, "number coercion failed"); |
Guido van Rossum | 5524a59 | 1995-01-10 15:26:20 +0000 | [diff] [blame] | 498 | return -1; |
| 499 | } |
| 500 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 501 | |
Guido van Rossum | 49b11fe | 1995-01-26 00:38:22 +0000 | [diff] [blame] | 502 | /* Test whether an object can be called */ |
| 503 | |
| 504 | int |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 505 | PyCallable_Check(x) |
| 506 | PyObject *x; |
Guido van Rossum | 49b11fe | 1995-01-26 00:38:22 +0000 | [diff] [blame] | 507 | { |
| 508 | if (x == NULL) |
| 509 | return 0; |
| 510 | if (x->ob_type->tp_call != NULL || |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 511 | PyFunction_Check(x) || |
| 512 | PyMethod_Check(x) || |
| 513 | PyCFunction_Check(x) || |
| 514 | PyClass_Check(x)) |
Guido van Rossum | 49b11fe | 1995-01-26 00:38:22 +0000 | [diff] [blame] | 515 | return 1; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 516 | if (PyInstance_Check(x)) { |
| 517 | PyObject *call = PyObject_GetAttrString(x, "__call__"); |
Guido van Rossum | 49b11fe | 1995-01-26 00:38:22 +0000 | [diff] [blame] | 518 | if (call == NULL) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 519 | PyErr_Clear(); |
Guido van Rossum | 49b11fe | 1995-01-26 00:38:22 +0000 | [diff] [blame] | 520 | return 0; |
| 521 | } |
| 522 | /* Could test recursively but don't, for fear of endless |
| 523 | recursion if some joker sets self.__call__ = self */ |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 524 | Py_DECREF(call); |
Guido van Rossum | 49b11fe | 1995-01-26 00:38:22 +0000 | [diff] [blame] | 525 | return 1; |
| 526 | } |
| 527 | return 0; |
| 528 | } |
| 529 | |
| 530 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 531 | /* |
| 532 | NoObject is usable as a non-NULL undefined value, used by the macro None. |
| 533 | 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] | 534 | so there is exactly one (which is indestructible, by the way). |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 535 | */ |
| 536 | |
Guido van Rossum | 0c182a1 | 1992-03-27 17:26:13 +0000 | [diff] [blame] | 537 | /* ARGSUSED */ |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 538 | static PyObject * |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 539 | none_repr(op) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 540 | PyObject *op; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 541 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 542 | return PyString_FromString("None"); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 543 | } |
| 544 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 545 | static PyTypeObject PyNothing_Type = { |
| 546 | PyObject_HEAD_INIT(&PyType_Type) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 547 | 0, |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 548 | "None", |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 549 | 0, |
| 550 | 0, |
| 551 | 0, /*tp_dealloc*/ /*never called*/ |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 552 | 0, /*tp_print*/ |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 553 | 0, /*tp_getattr*/ |
| 554 | 0, /*tp_setattr*/ |
| 555 | 0, /*tp_compare*/ |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 556 | (reprfunc)none_repr, /*tp_repr*/ |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 557 | 0, /*tp_as_number*/ |
| 558 | 0, /*tp_as_sequence*/ |
| 559 | 0, /*tp_as_mapping*/ |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 560 | 0, /*tp_hash */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 561 | }; |
| 562 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 563 | PyObject _Py_NoneStruct = { |
| 564 | PyObject_HEAD_INIT(&PyNothing_Type) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 565 | }; |
| 566 | |
| 567 | |
Guido van Rossum | 84a9032 | 1996-05-22 16:34:47 +0000 | [diff] [blame] | 568 | #ifdef Py_TRACE_REFS |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 569 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 570 | static PyObject refchain = {&refchain, &refchain}; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 571 | |
Guido van Rossum | aacdc9d | 1996-08-12 21:32:12 +0000 | [diff] [blame] | 572 | void |
Guido van Rossum | e09fb55 | 1997-08-05 02:04:34 +0000 | [diff] [blame] | 573 | _Py_ResetReferences() |
| 574 | { |
| 575 | refchain._ob_prev = refchain._ob_next = &refchain; |
| 576 | _Py_RefTotal = 0; |
| 577 | } |
| 578 | |
| 579 | void |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 580 | _Py_NewReference(op) |
| 581 | PyObject *op; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 582 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 583 | _Py_RefTotal++; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 584 | op->ob_refcnt = 1; |
| 585 | op->_ob_next = refchain._ob_next; |
| 586 | op->_ob_prev = &refchain; |
| 587 | refchain._ob_next->_ob_prev = op; |
| 588 | refchain._ob_next = op; |
Sjoerd Mullender | a9c3c22 | 1993-10-11 12:54:31 +0000 | [diff] [blame] | 589 | #ifdef COUNT_ALLOCS |
| 590 | inc_count(op->ob_type); |
| 591 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 592 | } |
| 593 | |
Guido van Rossum | aacdc9d | 1996-08-12 21:32:12 +0000 | [diff] [blame] | 594 | void |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 595 | _Py_ForgetReference(op) |
| 596 | register PyObject *op; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 597 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 598 | register PyObject *p; |
Guido van Rossum | d7047b3 | 1995-01-02 19:07:15 +0000 | [diff] [blame] | 599 | if (op->ob_refcnt < 0) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 600 | Py_FatalError("UNREF negative refcnt"); |
Guido van Rossum | 2e8f614 | 1992-09-03 20:32:55 +0000 | [diff] [blame] | 601 | if (op == &refchain || |
Guido van Rossum | d7047b3 | 1995-01-02 19:07:15 +0000 | [diff] [blame] | 602 | 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] | 603 | Py_FatalError("UNREF invalid object"); |
Guido van Rossum | 2e8f614 | 1992-09-03 20:32:55 +0000 | [diff] [blame] | 604 | #ifdef SLOW_UNREF_CHECK |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 605 | for (p = refchain._ob_next; p != &refchain; p = p->_ob_next) { |
| 606 | if (p == op) |
| 607 | break; |
| 608 | } |
Guido van Rossum | d7047b3 | 1995-01-02 19:07:15 +0000 | [diff] [blame] | 609 | if (p == &refchain) /* Not found */ |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 610 | Py_FatalError("UNREF unknown object"); |
Guido van Rossum | 2e8f614 | 1992-09-03 20:32:55 +0000 | [diff] [blame] | 611 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 612 | op->_ob_next->_ob_prev = op->_ob_prev; |
| 613 | op->_ob_prev->_ob_next = op->_ob_next; |
Guido van Rossum | 2e8f614 | 1992-09-03 20:32:55 +0000 | [diff] [blame] | 614 | op->_ob_next = op->_ob_prev = NULL; |
Guido van Rossum | d8953cb | 1995-04-06 14:46:26 +0000 | [diff] [blame] | 615 | #ifdef COUNT_ALLOCS |
| 616 | op->ob_type->tp_free++; |
| 617 | #endif |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 618 | } |
| 619 | |
Guido van Rossum | aacdc9d | 1996-08-12 21:32:12 +0000 | [diff] [blame] | 620 | void |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 621 | _Py_Dealloc(op) |
| 622 | PyObject *op; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 623 | { |
Guido van Rossum | 9776adf | 1994-09-07 14:36:45 +0000 | [diff] [blame] | 624 | destructor dealloc = op->ob_type->tp_dealloc; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 625 | _Py_ForgetReference(op); |
Guido van Rossum | 2e8f614 | 1992-09-03 20:32:55 +0000 | [diff] [blame] | 626 | op->ob_type = NULL; |
Guido van Rossum | 9776adf | 1994-09-07 14:36:45 +0000 | [diff] [blame] | 627 | (*dealloc)(op); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 628 | } |
| 629 | |
Guido van Rossum | aacdc9d | 1996-08-12 21:32:12 +0000 | [diff] [blame] | 630 | void |
Guido van Rossum | ded690f | 1996-05-24 20:48:31 +0000 | [diff] [blame] | 631 | _Py_PrintReferences(fp) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 632 | FILE *fp; |
| 633 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 634 | PyObject *op; |
Guido van Rossum | e09fb55 | 1997-08-05 02:04:34 +0000 | [diff] [blame] | 635 | fprintf(fp, "Remaining objects:\n"); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 636 | for (op = refchain._ob_next; op != &refchain; op = op->_ob_next) { |
| 637 | fprintf(fp, "[%d] ", op->ob_refcnt); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 638 | if (PyObject_Print(op, fp, 0) != 0) |
| 639 | PyErr_Clear(); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 640 | putc('\n', fp); |
| 641 | } |
| 642 | } |
| 643 | |
Sjoerd Mullender | 6ec3c65 | 1995-08-29 09:18:14 +0000 | [diff] [blame] | 644 | PyObject * |
Guido van Rossum | ded690f | 1996-05-24 20:48:31 +0000 | [diff] [blame] | 645 | _Py_GetObjects(self, args) |
Sjoerd Mullender | 6ec3c65 | 1995-08-29 09:18:14 +0000 | [diff] [blame] | 646 | PyObject *self; |
| 647 | PyObject *args; |
| 648 | { |
| 649 | int i, n; |
| 650 | PyObject *t = NULL; |
| 651 | PyObject *res, *op; |
| 652 | |
| 653 | if (!PyArg_ParseTuple(args, "i|O", &n, &t)) |
| 654 | return NULL; |
| 655 | op = refchain._ob_next; |
| 656 | res = PyList_New(0); |
| 657 | if (res == NULL) |
| 658 | return NULL; |
| 659 | for (i = 0; (n == 0 || i < n) && op != &refchain; i++) { |
| 660 | while (op == self || op == args || op == res || op == t || |
| 661 | t != NULL && op->ob_type != (PyTypeObject *) t) { |
| 662 | op = op->_ob_next; |
| 663 | if (op == &refchain) |
| 664 | return res; |
| 665 | } |
| 666 | if (PyList_Append(res, op) < 0) { |
| 667 | Py_DECREF(res); |
| 668 | return NULL; |
| 669 | } |
| 670 | op = op->_ob_next; |
| 671 | } |
| 672 | return res; |
| 673 | } |
| 674 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 675 | #endif |
Guido van Rossum | 97ead3f | 1996-01-12 01:24:09 +0000 | [diff] [blame] | 676 | |
| 677 | |
| 678 | /* Hack to force loading of cobject.o */ |
Guido van Rossum | da9c271 | 1996-12-05 21:58:58 +0000 | [diff] [blame] | 679 | PyTypeObject *_Py_cobject_hack = &PyCObject_Type; |
Guido van Rossum | 84a9032 | 1996-05-22 16:34:47 +0000 | [diff] [blame] | 680 | |
| 681 | |
| 682 | /* Hack to force loading of abstract.o */ |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 683 | int (*_Py_abstract_hack) Py_FPROTO((PyObject *)) = &PyObject_Length; |
Guido van Rossum | e09fb55 | 1997-08-05 02:04:34 +0000 | [diff] [blame] | 684 | |
| 685 | |
| 686 | /* Malloc wrappers (see mymalloc.h) */ |
| 687 | |
| 688 | /* The Py_{Malloc,Realloc} wrappers call PyErr_NoMemory() on failure */ |
| 689 | |
| 690 | ANY * |
| 691 | Py_Malloc(nbytes) |
| 692 | size_t nbytes; |
| 693 | { |
| 694 | ANY *p; |
| 695 | #if _PyMem_EXTRA > 0 |
| 696 | if (nbytes == 0) |
| 697 | nbytes = _PyMem_EXTRA; |
| 698 | #endif |
| 699 | p = malloc(nbytes); |
| 700 | if (p != NULL) |
| 701 | return p; |
Guido van Rossum | ea46e4d | 1997-08-12 14:54:54 +0000 | [diff] [blame] | 702 | else { |
| 703 | PyErr_NoMemory(); |
| 704 | return NULL; |
| 705 | } |
Guido van Rossum | e09fb55 | 1997-08-05 02:04:34 +0000 | [diff] [blame] | 706 | } |
| 707 | |
| 708 | ANY * |
| 709 | Py_Realloc(p, nbytes) |
| 710 | ANY *p; |
| 711 | size_t nbytes; |
| 712 | { |
| 713 | #if _PyMem_EXTRA > 0 |
| 714 | if (nbytes == 0) |
| 715 | nbytes = _PyMem_EXTRA; |
| 716 | #endif |
| 717 | p = realloc(p, nbytes); |
| 718 | if (p != NULL) |
| 719 | return p; |
Guido van Rossum | ea46e4d | 1997-08-12 14:54:54 +0000 | [diff] [blame] | 720 | else { |
| 721 | PyErr_NoMemory(); |
| 722 | return NULL; |
| 723 | } |
Guido van Rossum | e09fb55 | 1997-08-05 02:04:34 +0000 | [diff] [blame] | 724 | } |
| 725 | |
| 726 | void |
| 727 | Py_Free(p) |
| 728 | ANY *p; |
| 729 | { |
| 730 | free(p); |
| 731 | } |
| 732 | |
| 733 | /* The PyMem_{Malloc,Realloc} wrappers don't call anything on failure */ |
| 734 | |
| 735 | ANY * |
| 736 | PyMem_Malloc(nbytes) |
| 737 | size_t nbytes; |
| 738 | { |
| 739 | #if _PyMem_EXTRA > 0 |
| 740 | if (nbytes == 0) |
| 741 | nbytes = _PyMem_EXTRA; |
| 742 | #endif |
| 743 | return malloc(nbytes); |
| 744 | } |
| 745 | |
| 746 | ANY * |
| 747 | PyMem_Realloc(p, nbytes) |
| 748 | ANY *p; |
| 749 | size_t nbytes; |
| 750 | { |
| 751 | #if _PyMem_EXTRA > 0 |
| 752 | if (nbytes == 0) |
| 753 | nbytes = _PyMem_EXTRA; |
| 754 | #endif |
| 755 | return realloc(p, nbytes); |
| 756 | } |
| 757 | |
| 758 | void |
| 759 | PyMem_Free(p) |
| 760 | ANY *p; |
| 761 | { |
| 762 | free(p); |
| 763 | } |