Guido van Rossum | 81daa32 | 1993-05-20 14:24:46 +0000 | [diff] [blame] | 1 | /*********************************************************** |
| 2 | Copyright 1991, 1992, 1993 by Stichting Mathematisch Centrum, |
| 3 | Amsterdam, The Netherlands. |
| 4 | |
| 5 | All Rights Reserved |
| 6 | |
| 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. |
| 14 | |
| 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. |
| 22 | |
| 23 | ******************************************************************/ |
| 24 | |
| 25 | /* Access object implementation */ |
| 26 | |
Guido van Rossum | 234f942 | 1993-06-17 12:35:49 +0000 | [diff] [blame] | 27 | /* XXX TO DO LIST |
Guido van Rossum | 234f942 | 1993-06-17 12:35:49 +0000 | [diff] [blame] | 28 | - __init__ and __del__ (and all other similar methods) |
Guido van Rossum | ed18fdc | 1993-07-11 19:55:34 +0000 | [diff] [blame] | 29 | should be usable even when private, not ignored (???) |
Guido van Rossum | 234f942 | 1993-06-17 12:35:49 +0000 | [diff] [blame] | 30 | */ |
| 31 | |
Guido van Rossum | 81daa32 | 1993-05-20 14:24:46 +0000 | [diff] [blame] | 32 | #include "allobjects.h" |
Guido van Rossum | ed18fdc | 1993-07-11 19:55:34 +0000 | [diff] [blame] | 33 | #include "ceval.h" |
Guido van Rossum | 81daa32 | 1993-05-20 14:24:46 +0000 | [diff] [blame] | 34 | #include "structmember.h" |
| 35 | #include "modsupport.h" /* For getargs() etc. */ |
| 36 | |
| 37 | typedef struct { |
| 38 | OB_HEAD |
| 39 | object *ac_value; |
Guido van Rossum | eb6b33a | 1993-05-25 09:38:27 +0000 | [diff] [blame] | 40 | object *ac_owner; |
Guido van Rossum | 81daa32 | 1993-05-20 14:24:46 +0000 | [diff] [blame] | 41 | typeobject *ac_type; |
| 42 | int ac_mode; |
| 43 | } accessobject; |
| 44 | |
| 45 | /* Forward */ |
| 46 | static int typecheck PROTO((object *, typeobject *)); |
Guido van Rossum | eb6b33a | 1993-05-25 09:38:27 +0000 | [diff] [blame] | 47 | static int ownercheck PROTO((object *, object *, int, int)); |
Guido van Rossum | 81daa32 | 1993-05-20 14:24:46 +0000 | [diff] [blame] | 48 | |
| 49 | object * |
Guido van Rossum | eb6b33a | 1993-05-25 09:38:27 +0000 | [diff] [blame] | 50 | newaccessobject(value, owner, type, mode) |
Guido van Rossum | 81daa32 | 1993-05-20 14:24:46 +0000 | [diff] [blame] | 51 | object *value; |
Guido van Rossum | eb6b33a | 1993-05-25 09:38:27 +0000 | [diff] [blame] | 52 | object *owner; |
Guido van Rossum | 81daa32 | 1993-05-20 14:24:46 +0000 | [diff] [blame] | 53 | typeobject *type; |
| 54 | int mode; |
| 55 | { |
| 56 | accessobject *ap; |
Guido van Rossum | 81daa32 | 1993-05-20 14:24:46 +0000 | [diff] [blame] | 57 | if (!typecheck(value, type)) { |
| 58 | err_setstr(AccessError, |
| 59 | "access: initial value has inappropriate type"); |
| 60 | return NULL; |
| 61 | } |
| 62 | ap = NEWOBJ(accessobject, &Accesstype); |
| 63 | if (ap == NULL) |
| 64 | return NULL; |
| 65 | XINCREF(value); |
| 66 | ap->ac_value = value; |
Guido van Rossum | eb6b33a | 1993-05-25 09:38:27 +0000 | [diff] [blame] | 67 | XINCREF(owner); |
| 68 | ap->ac_owner = owner; |
Guido van Rossum | 81daa32 | 1993-05-20 14:24:46 +0000 | [diff] [blame] | 69 | XINCREF(type); |
| 70 | ap->ac_type = (typeobject *)type; |
| 71 | ap->ac_mode = mode; |
| 72 | return (object *)ap; |
| 73 | } |
| 74 | |
| 75 | object * |
| 76 | cloneaccessobject(op) |
| 77 | object *op; |
| 78 | { |
| 79 | register accessobject *ap; |
| 80 | if (!is_accessobject(op)) { |
| 81 | err_badcall(); |
| 82 | return NULL; |
| 83 | } |
| 84 | ap = (accessobject *)op; |
Guido van Rossum | eb6b33a | 1993-05-25 09:38:27 +0000 | [diff] [blame] | 85 | return newaccessobject(ap->ac_value, ap->ac_owner, |
Guido van Rossum | 81daa32 | 1993-05-20 14:24:46 +0000 | [diff] [blame] | 86 | ap->ac_type, ap->ac_mode); |
| 87 | } |
| 88 | |
| 89 | void |
Guido van Rossum | eb6b33a | 1993-05-25 09:38:27 +0000 | [diff] [blame] | 90 | setaccessowner(op, owner) |
Guido van Rossum | 81daa32 | 1993-05-20 14:24:46 +0000 | [diff] [blame] | 91 | object *op; |
Guido van Rossum | eb6b33a | 1993-05-25 09:38:27 +0000 | [diff] [blame] | 92 | object *owner; |
Guido van Rossum | 81daa32 | 1993-05-20 14:24:46 +0000 | [diff] [blame] | 93 | { |
| 94 | register accessobject *ap; |
Guido van Rossum | eb6b33a | 1993-05-25 09:38:27 +0000 | [diff] [blame] | 95 | if (!is_accessobject(op)) |
Guido van Rossum | 81daa32 | 1993-05-20 14:24:46 +0000 | [diff] [blame] | 96 | return; /* XXX no error */ |
| 97 | ap = (accessobject *)op; |
Guido van Rossum | eb6b33a | 1993-05-25 09:38:27 +0000 | [diff] [blame] | 98 | XDECREF(ap->ac_owner); |
| 99 | XINCREF(owner); |
| 100 | ap->ac_owner = owner; |
Guido van Rossum | 81daa32 | 1993-05-20 14:24:46 +0000 | [diff] [blame] | 101 | } |
| 102 | |
Guido van Rossum | b3f7258 | 1993-05-21 19:56:10 +0000 | [diff] [blame] | 103 | int |
| 104 | hasaccessvalue(op) |
| 105 | object *op; |
| 106 | { |
| 107 | if (!is_accessobject(op)) |
| 108 | return 0; |
| 109 | return ((accessobject *)op)->ac_value != NULL; |
| 110 | } |
| 111 | |
Guido van Rossum | 81daa32 | 1993-05-20 14:24:46 +0000 | [diff] [blame] | 112 | object * |
Guido van Rossum | ed18fdc | 1993-07-11 19:55:34 +0000 | [diff] [blame] | 113 | getaccessvalue(op, caller) |
Guido van Rossum | 81daa32 | 1993-05-20 14:24:46 +0000 | [diff] [blame] | 114 | object *op; |
Guido van Rossum | ed18fdc | 1993-07-11 19:55:34 +0000 | [diff] [blame] | 115 | object *caller; |
Guido van Rossum | 81daa32 | 1993-05-20 14:24:46 +0000 | [diff] [blame] | 116 | { |
| 117 | register accessobject *ap; |
| 118 | if (!is_accessobject(op)) { |
| 119 | err_badcall(); |
| 120 | return NULL; |
| 121 | } |
| 122 | ap = (accessobject *)op; |
| 123 | |
Guido van Rossum | ed18fdc | 1993-07-11 19:55:34 +0000 | [diff] [blame] | 124 | if (!ownercheck(caller, ap->ac_owner, AC_R, ap->ac_mode)) { |
Guido van Rossum | 81daa32 | 1993-05-20 14:24:46 +0000 | [diff] [blame] | 125 | err_setstr(AccessError, "read access denied"); |
| 126 | return NULL; |
| 127 | } |
| 128 | |
| 129 | if (ap->ac_value == NULL) { |
| 130 | err_setstr(AccessError, "no current value"); |
| 131 | return NULL; |
| 132 | } |
| 133 | INCREF(ap->ac_value); |
| 134 | return ap->ac_value; |
| 135 | } |
| 136 | |
| 137 | int |
Guido van Rossum | ed18fdc | 1993-07-11 19:55:34 +0000 | [diff] [blame] | 138 | setaccessvalue(op, caller, value) |
Guido van Rossum | 81daa32 | 1993-05-20 14:24:46 +0000 | [diff] [blame] | 139 | object *op; |
Guido van Rossum | ed18fdc | 1993-07-11 19:55:34 +0000 | [diff] [blame] | 140 | object *caller; |
Guido van Rossum | 81daa32 | 1993-05-20 14:24:46 +0000 | [diff] [blame] | 141 | object *value; |
| 142 | { |
| 143 | register accessobject *ap; |
| 144 | if (!is_accessobject(op)) { |
| 145 | err_badcall(); |
| 146 | return -1; |
| 147 | } |
| 148 | ap = (accessobject *)op; |
| 149 | |
Guido van Rossum | ed18fdc | 1993-07-11 19:55:34 +0000 | [diff] [blame] | 150 | if (!ownercheck(caller, ap->ac_owner, AC_W, ap->ac_mode)) { |
Guido van Rossum | 81daa32 | 1993-05-20 14:24:46 +0000 | [diff] [blame] | 151 | err_setstr(AccessError, "write access denied"); |
| 152 | return -1; |
| 153 | } |
| 154 | |
| 155 | if (!typecheck(value, ap->ac_type)) { |
| 156 | err_setstr(AccessError, "assign value of inappropriate type"); |
| 157 | return -1; |
| 158 | } |
| 159 | |
| 160 | if (value == NULL) { /* Delete it */ |
| 161 | if (ap->ac_value == NULL) { |
| 162 | err_setstr(AccessError, "no current value"); |
| 163 | return -1; |
| 164 | } |
| 165 | DECREF(ap->ac_value); |
| 166 | ap->ac_value = NULL; |
| 167 | return 0; |
| 168 | } |
| 169 | XDECREF(ap->ac_value); |
| 170 | INCREF(value); |
| 171 | ap->ac_value = value; |
| 172 | return 0; |
| 173 | } |
| 174 | |
| 175 | static int |
| 176 | typecheck(value, type) |
| 177 | object *value; |
| 178 | typeobject *type; |
| 179 | { |
| 180 | object *x; |
| 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) |
| 187 | return 0; |
| 188 | if (!is_instanceobject(value)) |
| 189 | return 1; |
| 190 | /* For instances, make sure it really looks like a number */ |
| 191 | x = getattr(value, "__sub__"); |
| 192 | if (x == NULL) { |
| 193 | err_clear(); |
| 194 | return 0; |
| 195 | } |
| 196 | DECREF(x); |
| 197 | return 1; |
| 198 | } |
| 199 | if (type == &Anysequencetype) { |
| 200 | if (value->ob_type->tp_as_sequence == NULL) |
| 201 | return 0; |
| 202 | if (!is_instanceobject(value)) |
| 203 | return 1; |
| 204 | /* For instances, make sure it really looks like a sequence */ |
| 205 | x = getattr(value, "__getslice__"); |
| 206 | if (x == NULL) { |
| 207 | err_clear(); |
| 208 | return 0; |
| 209 | } |
| 210 | DECREF(x); |
| 211 | return 1; |
| 212 | } |
| 213 | if (type == &Anymappingtype) { |
| 214 | if (value->ob_type->tp_as_mapping == NULL) |
| 215 | return 0; |
| 216 | if (!is_instanceobject(value)) |
| 217 | return 1; |
| 218 | /* For instances, make sure it really looks like a mapping */ |
| 219 | x = getattr(value, "__getitem__"); |
| 220 | if (x == NULL) { |
| 221 | err_clear(); |
| 222 | return 0; |
| 223 | } |
| 224 | DECREF(x); |
| 225 | return 1; |
| 226 | } |
| 227 | return 0; |
| 228 | } |
| 229 | |
| 230 | static int |
Guido van Rossum | ed18fdc | 1993-07-11 19:55:34 +0000 | [diff] [blame] | 231 | isprivileged(caller) |
| 232 | object *caller; |
| 233 | { |
| 234 | object *g; |
| 235 | if (caller != NULL && hasattr(caller, "__privileged__")) |
| 236 | return 1; |
| 237 | g = getglobals(); |
| 238 | if (g != NULL && dictlookup(g, "__privileged__")) |
| 239 | return 1; |
| 240 | return 0; |
| 241 | } |
| 242 | |
| 243 | static int |
Guido van Rossum | eb6b33a | 1993-05-25 09:38:27 +0000 | [diff] [blame] | 244 | ownercheck(caller, owner, access, mode) |
Guido van Rossum | 81daa32 | 1993-05-20 14:24:46 +0000 | [diff] [blame] | 245 | object *caller; |
| 246 | object *owner; |
| 247 | int access; |
| 248 | int mode; |
| 249 | { |
Guido van Rossum | eb6b33a | 1993-05-25 09:38:27 +0000 | [diff] [blame] | 250 | int mask = AC_PUBLIC; |
Guido van Rossum | ed18fdc | 1993-07-11 19:55:34 +0000 | [diff] [blame] | 251 | if (caller == owner || isprivileged(caller)) |
| 252 | mask |= AC_PRIVATE | AC_PROTECTED; |
| 253 | else if (caller != NULL && owner != NULL && |
| 254 | is_classobject(owner) && is_classobject(caller) && |
| 255 | (issubclass(caller, owner) || |
| 256 | issubclass(owner, caller))) |
Guido van Rossum | eb6b33a | 1993-05-25 09:38:27 +0000 | [diff] [blame] | 257 | mask |= AC_PROTECTED; |
Guido van Rossum | eb6b33a | 1993-05-25 09:38:27 +0000 | [diff] [blame] | 258 | return access & mode & mask; |
Guido van Rossum | 81daa32 | 1993-05-20 14:24:46 +0000 | [diff] [blame] | 259 | } |
| 260 | |
| 261 | /* Access methods */ |
| 262 | |
| 263 | static void |
| 264 | access_dealloc(ap) |
| 265 | accessobject *ap; |
| 266 | { |
| 267 | XDECREF(ap->ac_value); |
Guido van Rossum | eb6b33a | 1993-05-25 09:38:27 +0000 | [diff] [blame] | 268 | XDECREF(ap->ac_owner); |
Guido van Rossum | 81daa32 | 1993-05-20 14:24:46 +0000 | [diff] [blame] | 269 | XDECREF(ap->ac_type); |
| 270 | DEL(ap); |
| 271 | } |
| 272 | |
| 273 | #define OFF(x) offsetof(accessobject, x) |
| 274 | |
| 275 | static struct memberlist access_memberlist[] = { |
| 276 | {"ac_value", T_OBJECT, OFF(ac_value)}, |
Guido van Rossum | eb6b33a | 1993-05-25 09:38:27 +0000 | [diff] [blame] | 277 | {"ac_owner", T_OBJECT, OFF(ac_owner)}, |
Guido van Rossum | 81daa32 | 1993-05-20 14:24:46 +0000 | [diff] [blame] | 278 | {"ac_type", T_OBJECT, OFF(ac_type)}, |
| 279 | {"ac_mode", T_INT, OFF(ac_mode)}, |
| 280 | {NULL} /* Sentinel */ |
| 281 | }; |
| 282 | |
| 283 | static object * |
| 284 | access_getattr(ap, name) |
| 285 | accessobject *ap; |
| 286 | char *name; |
| 287 | { |
| 288 | return getmember((char *)ap, access_memberlist, name); |
| 289 | } |
| 290 | |
| 291 | static object * |
| 292 | access_repr(ap) |
| 293 | accessobject *ap; |
| 294 | { |
| 295 | char buf[300]; |
Guido van Rossum | eb6b33a | 1993-05-25 09:38:27 +0000 | [diff] [blame] | 296 | char buf2[20]; |
| 297 | char *ownername; |
Guido van Rossum | 81daa32 | 1993-05-20 14:24:46 +0000 | [diff] [blame] | 298 | typeobject *type = ap->ac_type; |
Guido van Rossum | eb6b33a | 1993-05-25 09:38:27 +0000 | [diff] [blame] | 299 | if (is_classobject(ap->ac_owner)) { |
| 300 | ownername = |
| 301 | getstringvalue(((classobject *)ap->ac_owner)->cl_name); |
| 302 | } |
| 303 | else { |
| 304 | sprintf(buf2, "0x%lx", (long)ap->ac_owner); |
| 305 | ownername = buf2; |
| 306 | } |
Guido van Rossum | b3f7258 | 1993-05-21 19:56:10 +0000 | [diff] [blame] | 307 | sprintf(buf, |
Guido van Rossum | eb6b33a | 1993-05-25 09:38:27 +0000 | [diff] [blame] | 308 | "<access object, value 0x%lx, owner %.100s, type %.100s, mode %04o>", |
Guido van Rossum | b3f7258 | 1993-05-21 19:56:10 +0000 | [diff] [blame] | 309 | (long)(ap->ac_value), |
Guido van Rossum | eb6b33a | 1993-05-25 09:38:27 +0000 | [diff] [blame] | 310 | ownername, |
Guido van Rossum | 81daa32 | 1993-05-20 14:24:46 +0000 | [diff] [blame] | 311 | type ? type->tp_name : "-", |
| 312 | ap->ac_mode); |
| 313 | return newstringobject(buf); |
| 314 | } |
| 315 | |
| 316 | typeobject Accesstype = { |
| 317 | OB_HEAD_INIT(&Typetype) |
| 318 | 0, /*ob_size*/ |
| 319 | "access", /*tp_name*/ |
| 320 | sizeof(accessobject), /*tp_size*/ |
| 321 | 0, /*tp_itemsize*/ |
| 322 | /* methods */ |
| 323 | access_dealloc, /*tp_dealloc*/ |
| 324 | 0, /*tp_print*/ |
| 325 | access_getattr, /*tp_getattr*/ |
| 326 | 0, /*tp_setattr*/ |
| 327 | 0, /*tp_compare*/ |
| 328 | access_repr, /*tp_repr*/ |
| 329 | 0, /*tp_as_number*/ |
| 330 | 0, /*tp_as_sequence*/ |
| 331 | 0, /*tp_as_mapping*/ |
| 332 | 0, /*tp_hash*/ |
| 333 | }; |
| 334 | |
Guido van Rossum | eb6b33a | 1993-05-25 09:38:27 +0000 | [diff] [blame] | 335 | |
| 336 | /* Pseudo type objects to indicate collections of types */ |
Guido van Rossum | 81daa32 | 1993-05-20 14:24:46 +0000 | [diff] [blame] | 337 | |
| 338 | /* XXX This should be replaced by a more general "subclassing" |
| 339 | XXX mechanism for type objects... */ |
| 340 | |
| 341 | typeobject Anynumbertype = { |
| 342 | OB_HEAD_INIT(&Typetype) |
| 343 | 0, /*ob_size*/ |
| 344 | "*number*", /*tp_name*/ |
| 345 | }; |
| 346 | |
| 347 | /* XXX Should really distinguish mutable and immutable sequences as well */ |
| 348 | |
| 349 | typeobject Anysequencetype = { |
| 350 | OB_HEAD_INIT(&Typetype) |
| 351 | 0, /*ob_size*/ |
| 352 | "*sequence*", /*tp_name*/ |
| 353 | }; |
| 354 | |
| 355 | typeobject Anymappingtype = { |
| 356 | OB_HEAD_INIT(&Typetype) |
| 357 | 0, /*ob_size*/ |
| 358 | "*mapping*", /*tp_name*/ |
| 359 | }; |