1 /***********************************************************
2 Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
7 Permission to use, copy, modify, and distribute this software and its
8 documentation for any purpose and without fee is hereby granted,
9 provided that the above copyright notice appear in all copies and that
10 both that copyright notice and this permission notice appear in
11 supporting documentation, and that the names of Stichting Mathematisch
12 Centrum or CWI not be used in advertising or publicity pertaining to
13 distribution of the software without specific, written prior permission.
15 STICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO
16 THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
17 FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE
18 FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
19 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
20 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
21 OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
23 ******************************************************************/
25 /* Access object implementation */
28 - __init__ and __del__ (and all other similar methods)
29 should be usable even when private, not ignored
32 #include "allobjects.h"
34 #include "structmember.h"
35 #include "modsupport.h" /* For getargs() etc. */
46 static int typecheck
PROTO((object
*, typeobject
*));
47 static int ownercheck
PROTO((object
*, object
*, int, int));
50 newaccessobject(value
, owner
, type
, mode
)
57 if (!typecheck(value
, type
)) {
58 err_setstr(AccessError
,
59 "access: initial value has inappropriate type");
62 ap
= NEWOBJ(accessobject
, &Accesstype
);
70 ap
->ac_type
= (typeobject
*)type
;
79 register accessobject
*ap
;
80 if (!is_accessobject(op
)) {
84 ap
= (accessobject
*)op
;
85 return newaccessobject(ap
->ac_value
, ap
->ac_owner
,
86 ap
->ac_type
, ap
->ac_mode
);
90 setaccessowner(op
, owner
)
94 register accessobject
*ap
;
95 if (!is_accessobject(op
))
96 return; /* XXX no error */
97 ap
= (accessobject
*)op
;
98 XDECREF(ap
->ac_owner
);
100 ap
->ac_owner
= owner
;
107 if (!is_accessobject(op
))
109 return ((accessobject
*)op
)->ac_value
!= NULL
;
113 getaccessvalue(op
, caller
)
117 register accessobject
*ap
;
118 if (!is_accessobject(op
)) {
122 ap
= (accessobject
*)op
;
124 if (!ownercheck(caller
, ap
->ac_owner
, AC_R
, ap
->ac_mode
)) {
125 err_setstr(AccessError
, "read access denied");
129 if (ap
->ac_value
== NULL
) {
130 err_setstr(AccessError
, "no current value");
133 INCREF(ap
->ac_value
);
138 setaccessvalue(op
, caller
, value
)
143 register accessobject
*ap
;
144 if (!is_accessobject(op
)) {
148 ap
= (accessobject
*)op
;
150 if (!ownercheck(caller
, ap
->ac_owner
, AC_W
, ap
->ac_mode
)) {
151 err_setstr(AccessError
, "write access denied");
155 if (!typecheck(value
, ap
->ac_type
)) {
156 err_setstr(AccessError
, "assign value of inappropriate type");
160 if (value
== NULL
) { /* Delete it */
161 if (ap
->ac_value
== NULL
) {
162 err_setstr(AccessError
, "no current value");
165 DECREF(ap
->ac_value
);
169 XDECREF(ap
->ac_value
);
171 ap
->ac_value
= value
;
176 typecheck(value
, type
)
181 if (value
== NULL
|| type
== NULL
)
182 return 1; /* No check */
183 if (value
->ob_type
== type
)
184 return 1; /* Exact match */
185 if (type
== &Anynumbertype
) {
186 if (value
->ob_type
->tp_as_number
== NULL
)
188 if (!is_instanceobject(value
))
190 /* For instances, make sure it really looks like a number */
191 x
= getattr(value
, "__sub__");
199 if (type
== &Anysequencetype
) {
200 if (value
->ob_type
->tp_as_sequence
== NULL
)
202 if (!is_instanceobject(value
))
204 /* For instances, make sure it really looks like a sequence */
205 x
= getattr(value
, "__getslice__");
213 if (type
== &Anymappingtype
) {
214 if (value
->ob_type
->tp_as_mapping
== NULL
)
216 if (!is_instanceobject(value
))
218 /* For instances, make sure it really looks like a mapping */
219 x
= getattr(value
, "__getitem__");
235 static char privileged
[] = "__privileged__";
236 if (caller
!= NULL
&& hasattr(caller
, privileged
))
239 if (g
!= NULL
&& dictlookup(g
, privileged
))
245 ownercheck(caller
, owner
, access
, mode
)
251 int mask
= AC_PUBLIC
;
252 if (caller
== owner
|| isprivileged(caller
))
253 mask
|= AC_PRIVATE
| AC_PROTECTED
;
254 else if (caller
!= NULL
&& owner
!= NULL
&&
255 is_classobject(owner
) && is_classobject(caller
) &&
256 (issubclass(caller
, owner
) ||
257 issubclass(owner
, caller
)))
258 mask
|= AC_PROTECTED
;
259 return access
& mode
& mask
;
268 XDECREF(ap
->ac_value
);
269 XDECREF(ap
->ac_owner
);
270 XDECREF(ap
->ac_type
);
274 #define OFF(x) offsetof(accessobject, x)
276 static struct memberlist access_memberlist
[] = {
277 {"ac_value", T_OBJECT
, OFF(ac_value
)},
278 {"ac_owner", T_OBJECT
, OFF(ac_owner
)},
279 {"ac_type", T_OBJECT
, OFF(ac_type
)},
280 {"ac_mode", T_INT
, OFF(ac_mode
)},
281 {NULL
} /* Sentinel */
285 access_getattr(ap
, name
)
289 return getmember((char *)ap
, access_memberlist
, name
);
299 typeobject
*type
= ap
->ac_type
;
300 if (is_classobject(ap
->ac_owner
)) {
302 getstringvalue(((classobject
*)ap
->ac_owner
)->cl_name
);
305 sprintf(buf2
, "0x%lx", (long)ap
->ac_owner
);
309 "<access object, value 0x%lx, owner %.100s, type %.100s, mode %04o>",
310 (long)(ap
->ac_value
),
312 type
? type
->tp_name
: "-",
314 return newstringobject(buf
);
317 typeobject Accesstype
= {
318 OB_HEAD_INIT(&Typetype
)
320 "access", /*tp_name*/
321 sizeof(accessobject
), /*tp_size*/
324 (destructor
)access_dealloc
, /*tp_dealloc*/
326 (getattrfunc
)access_getattr
, /*tp_getattr*/
329 (reprfunc
)access_repr
, /*tp_repr*/
331 0, /*tp_as_sequence*/
337 /* Pseudo type objects to indicate collections of types */
339 /* XXX This should be replaced by a more general "subclassing"
340 XXX mechanism for type objects... */
342 typeobject Anynumbertype
= {
343 OB_HEAD_INIT(&Typetype
)
345 "*number*", /*tp_name*/
348 /* XXX Should really distinguish mutable and immutable sequences as well */
350 typeobject Anysequencetype
= {
351 OB_HEAD_INIT(&Typetype
)
353 "*sequence*", /*tp_name*/
356 typeobject Anymappingtype
= {
357 OB_HEAD_INIT(&Typetype
)
359 "*mapping*", /*tp_name*/