blob: 9a8d4b391be430df66a43e93899f3672b305fff5 [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001/***********************************************************
Guido van Rossum9bfef441993-03-29 10:43:31 +00002Copyright 1991, 1992, 1993 by Stichting Mathematisch Centrum,
3Amsterdam, The Netherlands.
Guido van Rossumf70e43a1991-02-19 12:39:46 +00004
5 All Rights Reserved
6
7Permission to use, copy, modify, and distribute this software and its
8documentation for any purpose and without fee is hereby granted,
9provided that the above copyright notice appear in all copies and that
10both that copyright notice and this permission notice appear in
11supporting documentation, and that the names of Stichting Mathematisch
12Centrum or CWI not be used in advertising or publicity pertaining to
13distribution of the software without specific, written prior permission.
14
15STICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO
16THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
17FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE
18FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
19WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
20ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
21OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22
23******************************************************************/
24
Guido van Rossum3f5da241990-12-20 15:06:42 +000025/* Generic object operations; and implementation of None (NoObject) */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000026
Guido van Rossum3f5da241990-12-20 15:06:42 +000027#include "allobjects.h"
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000028
Guido van Rossum3f5da241990-12-20 15:06:42 +000029#ifdef REF_DEBUG
30long ref_total;
31#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000032
Guido van Rossum3f5da241990-12-20 15:06:42 +000033/* Object allocation routines used by NEWOBJ and NEWVAROBJ macros.
34 These are used by the individual routines for object creation.
35 Do not call them otherwise, they do not initialize the object! */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000036
37object *
38newobject(tp)
39 typeobject *tp;
40{
41 object *op = (object *) malloc(tp->tp_basicsize);
42 if (op == NULL)
43 return err_nomem();
44 NEWREF(op);
45 op->ob_type = tp;
46 return op;
47}
48
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000049varobject *
50newvarobject(tp, size)
51 typeobject *tp;
52 unsigned int size;
53{
54 varobject *op = (varobject *)
55 malloc(tp->tp_basicsize + size * tp->tp_itemsize);
56 if (op == NULL)
Guido van Rossum05ab1111991-05-05 20:10:41 +000057 return (varobject *)err_nomem();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000058 NEWREF(op);
59 op->ob_type = tp;
60 op->ob_size = size;
61 return op;
62}
63
Guido van Rossum90933611991-06-07 16:10:43 +000064int
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000065printobject(op, fp, flags)
66 object *op;
67 FILE *fp;
68 int flags;
69{
Guido van Rossum278ef591991-07-27 21:40:24 +000070 int ret = 0;
Guido van Rossum90933611991-06-07 16:10:43 +000071 if (intrcheck()) {
72 err_set(KeyboardInterrupt);
73 return -1;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000074 }
Guido van Rossum90933611991-06-07 16:10:43 +000075 if (op == NULL) {
76 fprintf(fp, "<nil>");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000077 }
Guido van Rossum90933611991-06-07 16:10:43 +000078 else {
79 if (op->ob_refcnt <= 0)
Guido van Rossum2e8f6141992-09-03 20:32:55 +000080 fprintf(fp, "<refcnt %u at %lx>",
81 op->ob_refcnt, (long)op);
82 else if (op->ob_type->tp_print == NULL) {
83 if (op->ob_type->tp_repr == NULL) {
84 fprintf(fp, "<%s object at %lx>",
85 op->ob_type->tp_name, (long)op);
86 }
87 else {
88 object *s = reprobject(op);
89 if (s == NULL)
90 ret = -1;
91 else if (!is_stringobject(s)) {
92 err_setstr(TypeError,
93 "repr not string");
94 ret = -1;
95 }
96 else {
97 fprintf(fp, "%s", getstringvalue(s));
98 }
99 XDECREF(s);
100 }
101 }
Guido van Rossum90933611991-06-07 16:10:43 +0000102 else
Guido van Rossum278ef591991-07-27 21:40:24 +0000103 ret = (*op->ob_type->tp_print)(op, fp, flags);
Guido van Rossum90933611991-06-07 16:10:43 +0000104 }
Guido van Rossum278ef591991-07-27 21:40:24 +0000105 if (ret == 0) {
106 if (ferror(fp)) {
Guido van Rossum2912f221991-12-10 13:59:09 +0000107 err_errno(IOError);
Guido van Rossum278ef591991-07-27 21:40:24 +0000108 clearerr(fp);
109 ret = -1;
110 }
111 }
112 return ret;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000113}
114
115object *
116reprobject(v)
117 object *v;
118{
Guido van Rossum90933611991-06-07 16:10:43 +0000119 if (intrcheck()) {
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000120 err_set(KeyboardInterrupt);
Guido van Rossum90933611991-06-07 16:10:43 +0000121 return NULL;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000122 }
Guido van Rossum90933611991-06-07 16:10:43 +0000123 if (v == NULL)
124 return newstringobject("<NULL>");
125 else if (v->ob_type->tp_repr == NULL) {
126 char buf[120];
127 sprintf(buf, "<%.80s object at %lx>",
128 v->ob_type->tp_name, (long)v);
129 return newstringobject(buf);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000130 }
Guido van Rossum90933611991-06-07 16:10:43 +0000131 else
132 return (*v->ob_type->tp_repr)(v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000133}
134
135int
136cmpobject(v, w)
137 object *v, *w;
138{
139 typeobject *tp;
140 if (v == w)
141 return 0;
142 if (v == NULL)
143 return -1;
144 if (w == NULL)
145 return 1;
Guido van Rossum9fb03681991-07-01 18:48:04 +0000146 if ((tp = v->ob_type) != w->ob_type) {
147 if (tp->tp_as_number != NULL &&
148 w->ob_type->tp_as_number != NULL) {
149 if (coerce(&v, &w) != 0) {
150 err_clear();
151 /* XXX Should report the error,
152 XXX but the interface isn't there... */
153 }
154 else {
155 int cmp = (*v->ob_type->tp_compare)(v, w);
156 DECREF(v);
157 DECREF(w);
158 return cmp;
159 }
160 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000161 return strcmp(tp->tp_name, w->ob_type->tp_name);
Guido van Rossum9fb03681991-07-01 18:48:04 +0000162 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000163 if (tp->tp_compare == NULL)
164 return (v < w) ? -1 : 1;
Guido van Rossum9fb03681991-07-01 18:48:04 +0000165 return (*tp->tp_compare)(v, w);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000166}
167
Guido van Rossum9bfef441993-03-29 10:43:31 +0000168long
169hashobject(v)
170 object *v;
171{
172 typeobject *tp = v->ob_type;
173 if (tp->tp_hash != NULL)
174 return (*tp->tp_hash)(v);
175 if (tp->tp_compare == NULL)
176 return (long) v; /* Use address as hash value */
177 /* If there's a cmp but no hash defined, the object can't be hashed */
178 err_setstr(TypeError, "unhashable type");
179 return -1;
180}
181
Guido van Rossum3f5da241990-12-20 15:06:42 +0000182object *
183getattr(v, name)
184 object *v;
185 char *name;
186{
187 if (v->ob_type->tp_getattr == NULL) {
188 err_setstr(TypeError, "attribute-less object");
189 return NULL;
190 }
191 else {
192 return (*v->ob_type->tp_getattr)(v, name);
193 }
194}
195
196int
197setattr(v, name, w)
198 object *v;
199 char *name;
200 object *w;
201{
202 if (v->ob_type->tp_setattr == NULL) {
203 if (v->ob_type->tp_getattr == NULL)
Guido van Rossum3ea74121991-12-24 13:28:03 +0000204 err_setstr(TypeError,
205 "attribute-less object (assign or del)");
Guido van Rossum3f5da241990-12-20 15:06:42 +0000206 else
Guido van Rossum3ea74121991-12-24 13:28:03 +0000207 err_setstr(TypeError,
208 "object has read-only attributes");
Guido van Rossum73531a31990-12-20 23:12:40 +0000209 return -1;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000210 }
211 else {
212 return (*v->ob_type->tp_setattr)(v, name, w);
213 }
214}
215
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000216
217/*
218NoObject is usable as a non-NULL undefined value, used by the macro None.
219There is (and should be!) no way to create other objects of this type,
Guido van Rossum3f5da241990-12-20 15:06:42 +0000220so there is exactly one (which is indestructible, by the way).
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000221*/
222
Guido van Rossum0c182a11992-03-27 17:26:13 +0000223/* ARGSUSED */
Guido van Rossum3f5da241990-12-20 15:06:42 +0000224static object *
225none_repr(op)
226 object *op;
227{
228 return newstringobject("None");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000229}
230
231static typeobject Notype = {
232 OB_HEAD_INIT(&Typetype)
233 0,
Guido van Rossum3f5da241990-12-20 15:06:42 +0000234 "None",
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000235 0,
236 0,
237 0, /*tp_dealloc*/ /*never called*/
Guido van Rossum7066dd71992-09-17 17:54:56 +0000238 0, /*tp_print*/
Guido van Rossum3f5da241990-12-20 15:06:42 +0000239 0, /*tp_getattr*/
240 0, /*tp_setattr*/
241 0, /*tp_compare*/
242 none_repr, /*tp_repr*/
243 0, /*tp_as_number*/
244 0, /*tp_as_sequence*/
245 0, /*tp_as_mapping*/
Guido van Rossum9bfef441993-03-29 10:43:31 +0000246 0, /*tp_hash */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000247};
248
249object NoObject = {
250 OB_HEAD_INIT(&Notype)
251};
252
253
254#ifdef TRACE_REFS
255
256static object refchain = {&refchain, &refchain};
257
258NEWREF(op)
259 object *op;
260{
261 ref_total++;
262 op->ob_refcnt = 1;
263 op->_ob_next = refchain._ob_next;
264 op->_ob_prev = &refchain;
265 refchain._ob_next->_ob_prev = op;
266 refchain._ob_next = op;
267}
268
Guido van Rossum3f5da241990-12-20 15:06:42 +0000269UNREF(op)
270 register object *op;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000271{
Guido van Rossum3f5da241990-12-20 15:06:42 +0000272 register object *p;
Guido van Rossumbd3edc81990-11-02 17:49:51 +0000273 if (op->ob_refcnt < 0) {
Guido van Rossum3f5da241990-12-20 15:06:42 +0000274 fprintf(stderr, "UNREF negative refcnt\n");
275 abort();
276 }
Guido van Rossum2e8f6141992-09-03 20:32:55 +0000277 if (op == &refchain ||
278 op->_ob_prev->_ob_next != op || op->_ob_next->_ob_prev != op) {
279 fprintf(stderr, "UNREF invalid object\n");
280 abort();
281 }
282#ifdef SLOW_UNREF_CHECK
Guido van Rossum3f5da241990-12-20 15:06:42 +0000283 for (p = refchain._ob_next; p != &refchain; p = p->_ob_next) {
284 if (p == op)
285 break;
286 }
287 if (p == &refchain) { /* Not found */
288 fprintf(stderr, "UNREF unknown object\n");
Guido van Rossumbd3edc81990-11-02 17:49:51 +0000289 abort();
290 }
Guido van Rossum2e8f6141992-09-03 20:32:55 +0000291#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000292 op->_ob_next->_ob_prev = op->_ob_prev;
293 op->_ob_prev->_ob_next = op->_ob_next;
Guido van Rossum2e8f6141992-09-03 20:32:55 +0000294 op->_ob_next = op->_ob_prev = NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000295}
296
297DELREF(op)
298 object *op;
299{
300 UNREF(op);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000301 (*(op)->ob_type->tp_dealloc)(op);
Guido van Rossum2e8f6141992-09-03 20:32:55 +0000302 op->ob_type = NULL;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000303}
304
305printrefs(fp)
306 FILE *fp;
307{
308 object *op;
Guido van Rossum2e8f6141992-09-03 20:32:55 +0000309 fprintf(fp, "Remaining objects (except strings referenced once):\n");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000310 for (op = refchain._ob_next; op != &refchain; op = op->_ob_next) {
Guido van Rossum2e8f6141992-09-03 20:32:55 +0000311 if (op->ob_refcnt == 1 && is_stringobject(op))
312 continue; /* Will be printed elsewhere */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000313 fprintf(fp, "[%d] ", op->ob_refcnt);
Guido van Rossum90933611991-06-07 16:10:43 +0000314 if (printobject(op, fp, 0) != 0)
315 err_clear();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000316 putc('\n', fp);
317 }
318}
319
320#endif