summaryrefslogtreecommitdiffstats
path: root/Objects/accessobject.c
blob: 41790cd30633058ce2188f55b4fcdb0c2acab797 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
/***********************************************************
Copyright 1991, 1992, 1993 by Stichting Mathematisch Centrum,
Amsterdam, The Netherlands.

                        All Rights Reserved

Permission to use, copy, modify, and distribute this software and its 
documentation for any purpose and without fee is hereby granted, 
provided that the above copyright notice appear in all copies and that
both that copyright notice and this permission notice appear in 
supporting documentation, and that the names of Stichting Mathematisch
Centrum or CWI not be used in advertising or publicity pertaining to
distribution of the software without specific, written prior permission.

STICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO
THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE
FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

******************************************************************/

/* Access object implementation */

#include "allobjects.h"

#include "structmember.h"
#include "modsupport.h"		/* For getargs() etc. */

typedef struct {
	OB_HEAD
	object		*ac_value;
	object		*ac_class;
	typeobject	*ac_type;
	int		ac_mode;
} accessobject;

/* Forward */
static int typecheck PROTO((object *, typeobject *));
static int classcheck PROTO((object *, object *, int, int));

object *
newaccessobject(value, class, type, mode)
	object *value;
	object *class;
	typeobject *type;
	int mode;
{
	accessobject *ap;
	if (class != NULL && !is_classobject(class)) {
		err_badcall();
		return NULL;
	}
	if (!typecheck(value, type)) {
		err_setstr(AccessError,
		"access: initial value has inappropriate type");
		return NULL;
	}
	ap = NEWOBJ(accessobject, &Accesstype);
	if (ap == NULL)
		return NULL;
	XINCREF(value);
	ap->ac_value = value;
	XINCREF(class);
	ap->ac_class = class;
	XINCREF(type);
	ap->ac_type = (typeobject *)type;
	ap->ac_mode = mode;
	return (object *)ap;
}

object *
cloneaccessobject(op)
	object *op;
{
	register accessobject *ap;
	if (!is_accessobject(op)) {
		err_badcall();
		return NULL;
	}
	ap = (accessobject *)op;
	return newaccessobject(ap->ac_value, ap->ac_class,
			       ap->ac_type, ap->ac_mode);
}

void
setaccessowner(op, class)
	object *op;
	object *class;
{
	register accessobject *ap;
	if (!is_accessobject(op) || class != NULL && !is_classobject(class))
		return; /* XXX no error */
	ap = (accessobject *)op;
	XDECREF(ap->ac_class);
	XINCREF(class);
	ap->ac_class = class;
}

object *
getaccessvalue(op, class)
	object *op;
	object *class;
{
	register accessobject *ap;
	if (!is_accessobject(op)) {
		err_badcall();
		return NULL;
	}
	ap = (accessobject *)op;
	
	if (!classcheck(class, ap->ac_class, AC_R, ap->ac_mode)) {
		err_setstr(AccessError, "read access denied");
		return NULL;
	}
	
	if (ap->ac_value == NULL) {
		err_setstr(AccessError, "no current value");
		return NULL;
	}
	INCREF(ap->ac_value);
	return ap->ac_value;
}

int
setaccessvalue(op, class, value)
	object *op;
	object *class;
	object *value;
{
	register accessobject *ap;
	if (!is_accessobject(op)) {
		err_badcall();
		return -1;
	}
	ap = (accessobject *)op;
	
	if (!classcheck(class, ap->ac_class, AC_W, ap->ac_mode)) {
		err_setstr(AccessError, "write access denied");
		return -1;
	}
	
	if (!typecheck(value, ap->ac_type)) {
		err_setstr(AccessError, "assign value of inappropriate type");
		return -1;
	}
	
	if (value == NULL) { /* Delete it */
		if (ap->ac_value == NULL) {
			err_setstr(AccessError, "no current value");
			return -1;
		}
		DECREF(ap->ac_value);
		ap->ac_value = NULL;
		return 0;
	}
	XDECREF(ap->ac_value);
	INCREF(value);
	ap->ac_value = value;
	return 0;
}

static int
typecheck(value, type)
	object *value;
	typeobject *type;
{
	object *x;
	if (value == NULL || type == NULL)
		return 1; /* No check */
	if (value->ob_type == type)
		return 1; /* Exact match */
	if (type == &Anynumbertype) {
		if (value->ob_type->tp_as_number == NULL)
			return 0;
		if (!is_instanceobject(value))
			return 1;
		/* For instances, make sure it really looks like a number */
		x = getattr(value, "__sub__");
		if (x == NULL) {
			err_clear();
			return 0;
		}
		DECREF(x);
		return 1;
	}
	if (type == &Anysequencetype) {
		if (value->ob_type->tp_as_sequence == NULL)
			return 0;
		if (!is_instanceobject(value))
			return 1;
		/* For instances, make sure it really looks like a sequence */
		x = getattr(value, "__getslice__");
		if (x == NULL) {
			err_clear();
			return 0;
		}
		DECREF(x);
		return 1;
	}
	if (type == &Anymappingtype) {
		if (value->ob_type->tp_as_mapping == NULL)
			return 0;
		if (!is_instanceobject(value))
			return 1;
		/* For instances, make sure it really looks like a mapping */
		x = getattr(value, "__getitem__");
		if (x == NULL) {
			err_clear();
			return 0;
		}
		DECREF(x);
		return 1;
	}
	return 0;
}

static int
classcheck(caller, owner, access, mode)
	object *caller;
	object *owner;
	int access;
	int mode;
{
	if (caller == owner && owner != NULL)
		return access & mode & (AC_PRIVATE|AC_PROTECTED|AC_PUBLIC);
	if (caller != NULL && owner != NULL && issubclass(caller, owner))
		return access & mode & (AC_PROTECTED|AC_PUBLIC);
	return access & mode & AC_PUBLIC;
}

/* Access methods */

static void
access_dealloc(ap)
	accessobject *ap;
{
	XDECREF(ap->ac_value);
	XDECREF(ap->ac_class);
	XDECREF(ap->ac_type);
	DEL(ap);
}

#define OFF(x) offsetof(accessobject, x)

static struct memberlist access_memberlist[] = {
	{"ac_value",	T_OBJECT,	OFF(ac_value)},
	{"ac_class",	T_OBJECT,	OFF(ac_class)},
	{"ac_type",	T_OBJECT,	OFF(ac_type)},
	{"ac_mode",	T_INT,		OFF(ac_mode)},
	{NULL}	/* Sentinel */
};

static object *
access_getattr(ap, name)
	accessobject *ap;
	char *name;
{
	return getmember((char *)ap, access_memberlist, name);
}

static object *
access_repr(ap)
	accessobject *ap;
{
	char buf[300];
	classobject *class = (classobject *)ap->ac_class;
	typeobject *type = ap->ac_type;
	sprintf(buf, "<access object, class %.100s, type %.100s, mode 0%o>",
		class ? getstringvalue(class->cl_name) : "-",
		type ? type->tp_name : "-",
		ap->ac_mode);
	return newstringobject(buf);
}

typeobject Accesstype = {
	OB_HEAD_INIT(&Typetype)
	0,			/*ob_size*/
	"access",		/*tp_name*/
	sizeof(accessobject),	/*tp_size*/
	0,			/*tp_itemsize*/
	/* methods */
	access_dealloc,		/*tp_dealloc*/
	0,			/*tp_print*/
	access_getattr,		/*tp_getattr*/
	0,			/*tp_setattr*/
	0,			/*tp_compare*/
	access_repr,		/*tp_repr*/
	0,			/*tp_as_number*/
	0,			/*tp_as_sequence*/
	0,			/*tp_as_mapping*/
	0,			/*tp_hash*/
};

/* Dummy type objects to indicate classes of types */

/* XXX This should be replaced by a more general "subclassing"
   XXX mechanism for type objects... */

typeobject Anynumbertype = {
	OB_HEAD_INIT(&Typetype)
	0,			/*ob_size*/
	"*number*",		/*tp_name*/
};

/* XXX Should really distinguish mutable and immutable sequences as well */

typeobject Anysequencetype = {
	OB_HEAD_INIT(&Typetype)
	0,			/*ob_size*/
	"*sequence*",		/*tp_name*/
};

typeobject Anymappingtype = {
	OB_HEAD_INIT(&Typetype)
	0,			/*ob_size*/
	"*mapping*",		/*tp_name*/
};