blob: b63f0679dbda0b1d4194becf358a31d104c48174 [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001/***********************************************************
2Copyright 1991 by Stichting Mathematisch Centrum, Amsterdam, The
3Netherlands.
4
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 Rossum90933611991-06-07 16:10:43 +000070 if (intrcheck()) {
71 err_set(KeyboardInterrupt);
72 return -1;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000073 }
Guido van Rossum90933611991-06-07 16:10:43 +000074 if (op == NULL) {
75 fprintf(fp, "<nil>");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000076 }
Guido van Rossum90933611991-06-07 16:10:43 +000077 else {
78 if (op->ob_refcnt <= 0)
79 fprintf(fp, "(refcnt %d):", op->ob_refcnt);
80 if (op->ob_type->tp_print == NULL)
81 fprintf(fp, "<%s object at %lx>",
82 op->ob_type->tp_name, (long)op);
83 else
84 return (*op->ob_type->tp_print)(op, fp, flags);
85 }
86 return 0;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000087}
88
89object *
90reprobject(v)
91 object *v;
92{
Guido van Rossum90933611991-06-07 16:10:43 +000093 if (intrcheck()) {
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000094 err_set(KeyboardInterrupt);
Guido van Rossum90933611991-06-07 16:10:43 +000095 return NULL;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000096 }
Guido van Rossum90933611991-06-07 16:10:43 +000097 if (v == NULL)
98 return newstringobject("<NULL>");
99 else if (v->ob_type->tp_repr == NULL) {
100 char buf[120];
101 sprintf(buf, "<%.80s object at %lx>",
102 v->ob_type->tp_name, (long)v);
103 return newstringobject(buf);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000104 }
Guido van Rossum90933611991-06-07 16:10:43 +0000105 else
106 return (*v->ob_type->tp_repr)(v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000107}
108
109int
110cmpobject(v, w)
111 object *v, *w;
112{
113 typeobject *tp;
114 if (v == w)
115 return 0;
116 if (v == NULL)
117 return -1;
118 if (w == NULL)
119 return 1;
120 if ((tp = v->ob_type) != w->ob_type)
121 return strcmp(tp->tp_name, w->ob_type->tp_name);
122 if (tp->tp_compare == NULL)
123 return (v < w) ? -1 : 1;
124 return ((*tp->tp_compare)(v, w));
125}
126
Guido van Rossum3f5da241990-12-20 15:06:42 +0000127object *
128getattr(v, name)
129 object *v;
130 char *name;
131{
132 if (v->ob_type->tp_getattr == NULL) {
133 err_setstr(TypeError, "attribute-less object");
134 return NULL;
135 }
136 else {
137 return (*v->ob_type->tp_getattr)(v, name);
138 }
139}
140
141int
142setattr(v, name, w)
143 object *v;
144 char *name;
145 object *w;
146{
147 if (v->ob_type->tp_setattr == NULL) {
148 if (v->ob_type->tp_getattr == NULL)
149 err_setstr(TypeError, "attribute-less object");
150 else
151 err_setstr(TypeError, "object has read-only attributes");
Guido van Rossum73531a31990-12-20 23:12:40 +0000152 return -1;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000153 }
154 else {
155 return (*v->ob_type->tp_setattr)(v, name, w);
156 }
157}
158
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000159
160/*
161NoObject is usable as a non-NULL undefined value, used by the macro None.
162There is (and should be!) no way to create other objects of this type,
Guido van Rossum3f5da241990-12-20 15:06:42 +0000163so there is exactly one (which is indestructible, by the way).
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000164*/
165
Guido van Rossum90933611991-06-07 16:10:43 +0000166static int
Guido van Rossum3f5da241990-12-20 15:06:42 +0000167none_print(op, fp, flags)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000168 object *op;
169 FILE *fp;
170 int flags;
171{
Guido van Rossum3f5da241990-12-20 15:06:42 +0000172 fprintf(fp, "None");
Guido van Rossum90933611991-06-07 16:10:43 +0000173 return 0;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000174}
175
176static object *
177none_repr(op)
178 object *op;
179{
180 return newstringobject("None");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000181}
182
183static typeobject Notype = {
184 OB_HEAD_INIT(&Typetype)
185 0,
Guido van Rossum3f5da241990-12-20 15:06:42 +0000186 "None",
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000187 0,
188 0,
189 0, /*tp_dealloc*/ /*never called*/
Guido van Rossum3f5da241990-12-20 15:06:42 +0000190 none_print, /*tp_print*/
191 0, /*tp_getattr*/
192 0, /*tp_setattr*/
193 0, /*tp_compare*/
194 none_repr, /*tp_repr*/
195 0, /*tp_as_number*/
196 0, /*tp_as_sequence*/
197 0, /*tp_as_mapping*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000198};
199
200object NoObject = {
201 OB_HEAD_INIT(&Notype)
202};
203
204
205#ifdef TRACE_REFS
206
207static object refchain = {&refchain, &refchain};
208
209NEWREF(op)
210 object *op;
211{
212 ref_total++;
213 op->ob_refcnt = 1;
214 op->_ob_next = refchain._ob_next;
215 op->_ob_prev = &refchain;
216 refchain._ob_next->_ob_prev = op;
217 refchain._ob_next = op;
218}
219
Guido van Rossum3f5da241990-12-20 15:06:42 +0000220UNREF(op)
221 register object *op;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000222{
Guido van Rossum3f5da241990-12-20 15:06:42 +0000223 register object *p;
Guido van Rossumbd3edc81990-11-02 17:49:51 +0000224 if (op->ob_refcnt < 0) {
Guido van Rossum3f5da241990-12-20 15:06:42 +0000225 fprintf(stderr, "UNREF negative refcnt\n");
226 abort();
227 }
228 for (p = refchain._ob_next; p != &refchain; p = p->_ob_next) {
229 if (p == op)
230 break;
231 }
232 if (p == &refchain) { /* Not found */
233 fprintf(stderr, "UNREF unknown object\n");
Guido van Rossumbd3edc81990-11-02 17:49:51 +0000234 abort();
235 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000236 op->_ob_next->_ob_prev = op->_ob_prev;
237 op->_ob_prev->_ob_next = op->_ob_next;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000238}
239
240DELREF(op)
241 object *op;
242{
243 UNREF(op);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000244 (*(op)->ob_type->tp_dealloc)(op);
245}
246
247printrefs(fp)
248 FILE *fp;
249{
250 object *op;
251 fprintf(fp, "Remaining objects:\n");
252 for (op = refchain._ob_next; op != &refchain; op = op->_ob_next) {
253 fprintf(fp, "[%d] ", op->ob_refcnt);
Guido van Rossum90933611991-06-07 16:10:43 +0000254 if (printobject(op, fp, 0) != 0)
255 err_clear();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000256 putc('\n', fp);
257 }
258}
259
260#endif