Guido van Rossum | 81daa32 | 1993-05-20 14:24:46 +0000 | [diff] [blame] | 1 | /*********************************************************** |
Guido van Rossum | 6610ad9 | 1995-01-04 19:07:38 +0000 | [diff] [blame] | 2 | Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam, |
| 3 | The Netherlands. |
Guido van Rossum | 81daa32 | 1993-05-20 14:24:46 +0000 | [diff] [blame] | 4 | |
| 5 | All Rights Reserved |
| 6 | |
Guido van Rossum | d266eb4 | 1996-10-25 14:44:06 +0000 | [diff] [blame^] | 7 | Permission to use, copy, modify, and distribute this software and its |
| 8 | documentation for any purpose and without fee is hereby granted, |
Guido van Rossum | 81daa32 | 1993-05-20 14:24:46 +0000 | [diff] [blame] | 9 | provided that the above copyright notice appear in all copies and that |
Guido van Rossum | d266eb4 | 1996-10-25 14:44:06 +0000 | [diff] [blame^] | 10 | both that copyright notice and this permission notice appear in |
Guido van Rossum | 81daa32 | 1993-05-20 14:24:46 +0000 | [diff] [blame] | 11 | supporting documentation, and that the names of Stichting Mathematisch |
Guido van Rossum | d266eb4 | 1996-10-25 14:44:06 +0000 | [diff] [blame^] | 12 | Centrum or CWI or Corporation for National Research Initiatives or |
| 13 | CNRI not be used in advertising or publicity pertaining to |
| 14 | distribution of the software without specific, written prior |
| 15 | permission. |
Guido van Rossum | 81daa32 | 1993-05-20 14:24:46 +0000 | [diff] [blame] | 16 | |
Guido van Rossum | d266eb4 | 1996-10-25 14:44:06 +0000 | [diff] [blame^] | 17 | While CWI is the initial source for this software, a modified version |
| 18 | is made available by the Corporation for National Research Initiatives |
| 19 | (CNRI) at the Internet address ftp://ftp.python.org. |
| 20 | |
| 21 | STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH |
| 22 | REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF |
| 23 | MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH |
| 24 | CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL |
| 25 | DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR |
| 26 | PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER |
| 27 | TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR |
| 28 | PERFORMANCE OF THIS SOFTWARE. |
Guido van Rossum | 81daa32 | 1993-05-20 14:24:46 +0000 | [diff] [blame] | 29 | |
| 30 | ******************************************************************/ |
| 31 | |
Guido van Rossum | 0dfcf75 | 1996-08-12 22:00:53 +0000 | [diff] [blame] | 32 | #ifdef SUPPORT_OBSOLETE_ACCESS /* This object type is no longer supported */ |
| 33 | |
Guido van Rossum | 81daa32 | 1993-05-20 14:24:46 +0000 | [diff] [blame] | 34 | /* Access object implementation */ |
| 35 | |
Guido van Rossum | 234f942 | 1993-06-17 12:35:49 +0000 | [diff] [blame] | 36 | /* XXX TO DO LIST |
Guido van Rossum | 234f942 | 1993-06-17 12:35:49 +0000 | [diff] [blame] | 37 | - __init__ and __del__ (and all other similar methods) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 38 | should be usable even when private, not ignored |
Guido van Rossum | 234f942 | 1993-06-17 12:35:49 +0000 | [diff] [blame] | 39 | */ |
| 40 | |
Guido van Rossum | 81daa32 | 1993-05-20 14:24:46 +0000 | [diff] [blame] | 41 | #include "allobjects.h" |
Guido van Rossum | ed18fdc | 1993-07-11 19:55:34 +0000 | [diff] [blame] | 42 | #include "ceval.h" |
Guido van Rossum | 81daa32 | 1993-05-20 14:24:46 +0000 | [diff] [blame] | 43 | #include "structmember.h" |
| 44 | #include "modsupport.h" /* For getargs() etc. */ |
| 45 | |
| 46 | typedef struct { |
| 47 | OB_HEAD |
| 48 | object *ac_value; |
Guido van Rossum | eb6b33a | 1993-05-25 09:38:27 +0000 | [diff] [blame] | 49 | object *ac_owner; |
Guido van Rossum | 81daa32 | 1993-05-20 14:24:46 +0000 | [diff] [blame] | 50 | typeobject *ac_type; |
| 51 | int ac_mode; |
| 52 | } accessobject; |
| 53 | |
| 54 | /* Forward */ |
| 55 | static int typecheck PROTO((object *, typeobject *)); |
Guido van Rossum | eb6b33a | 1993-05-25 09:38:27 +0000 | [diff] [blame] | 56 | static int ownercheck PROTO((object *, object *, int, int)); |
Guido van Rossum | 81daa32 | 1993-05-20 14:24:46 +0000 | [diff] [blame] | 57 | |
| 58 | object * |
Guido van Rossum | eb6b33a | 1993-05-25 09:38:27 +0000 | [diff] [blame] | 59 | newaccessobject(value, owner, type, mode) |
Guido van Rossum | 81daa32 | 1993-05-20 14:24:46 +0000 | [diff] [blame] | 60 | object *value; |
Guido van Rossum | eb6b33a | 1993-05-25 09:38:27 +0000 | [diff] [blame] | 61 | object *owner; |
Guido van Rossum | 81daa32 | 1993-05-20 14:24:46 +0000 | [diff] [blame] | 62 | typeobject *type; |
| 63 | int mode; |
| 64 | { |
| 65 | accessobject *ap; |
Guido van Rossum | 81daa32 | 1993-05-20 14:24:46 +0000 | [diff] [blame] | 66 | if (!typecheck(value, type)) { |
| 67 | err_setstr(AccessError, |
| 68 | "access: initial value has inappropriate type"); |
| 69 | return NULL; |
| 70 | } |
| 71 | ap = NEWOBJ(accessobject, &Accesstype); |
| 72 | if (ap == NULL) |
| 73 | return NULL; |
| 74 | XINCREF(value); |
| 75 | ap->ac_value = value; |
Guido van Rossum | eb6b33a | 1993-05-25 09:38:27 +0000 | [diff] [blame] | 76 | XINCREF(owner); |
| 77 | ap->ac_owner = owner; |
Guido van Rossum | 81daa32 | 1993-05-20 14:24:46 +0000 | [diff] [blame] | 78 | XINCREF(type); |
| 79 | ap->ac_type = (typeobject *)type; |
| 80 | ap->ac_mode = mode; |
| 81 | return (object *)ap; |
| 82 | } |
| 83 | |
| 84 | object * |
| 85 | cloneaccessobject(op) |
| 86 | object *op; |
| 87 | { |
| 88 | register accessobject *ap; |
| 89 | if (!is_accessobject(op)) { |
| 90 | err_badcall(); |
| 91 | return NULL; |
| 92 | } |
| 93 | ap = (accessobject *)op; |
Guido van Rossum | eb6b33a | 1993-05-25 09:38:27 +0000 | [diff] [blame] | 94 | return newaccessobject(ap->ac_value, ap->ac_owner, |
Guido van Rossum | 81daa32 | 1993-05-20 14:24:46 +0000 | [diff] [blame] | 95 | ap->ac_type, ap->ac_mode); |
| 96 | } |
| 97 | |
| 98 | void |
Guido van Rossum | eb6b33a | 1993-05-25 09:38:27 +0000 | [diff] [blame] | 99 | setaccessowner(op, owner) |
Guido van Rossum | 81daa32 | 1993-05-20 14:24:46 +0000 | [diff] [blame] | 100 | object *op; |
Guido van Rossum | eb6b33a | 1993-05-25 09:38:27 +0000 | [diff] [blame] | 101 | object *owner; |
Guido van Rossum | 81daa32 | 1993-05-20 14:24:46 +0000 | [diff] [blame] | 102 | { |
| 103 | register accessobject *ap; |
Guido van Rossum | eb6b33a | 1993-05-25 09:38:27 +0000 | [diff] [blame] | 104 | if (!is_accessobject(op)) |
Guido van Rossum | 81daa32 | 1993-05-20 14:24:46 +0000 | [diff] [blame] | 105 | return; /* XXX no error */ |
| 106 | ap = (accessobject *)op; |
Guido van Rossum | eb6b33a | 1993-05-25 09:38:27 +0000 | [diff] [blame] | 107 | XDECREF(ap->ac_owner); |
| 108 | XINCREF(owner); |
| 109 | ap->ac_owner = owner; |
Guido van Rossum | 81daa32 | 1993-05-20 14:24:46 +0000 | [diff] [blame] | 110 | } |
| 111 | |
Guido van Rossum | b3f7258 | 1993-05-21 19:56:10 +0000 | [diff] [blame] | 112 | int |
| 113 | hasaccessvalue(op) |
| 114 | object *op; |
| 115 | { |
| 116 | if (!is_accessobject(op)) |
| 117 | return 0; |
| 118 | return ((accessobject *)op)->ac_value != NULL; |
| 119 | } |
| 120 | |
Guido van Rossum | 81daa32 | 1993-05-20 14:24:46 +0000 | [diff] [blame] | 121 | object * |
Guido van Rossum | ed18fdc | 1993-07-11 19:55:34 +0000 | [diff] [blame] | 122 | getaccessvalue(op, caller) |
Guido van Rossum | 81daa32 | 1993-05-20 14:24:46 +0000 | [diff] [blame] | 123 | object *op; |
Guido van Rossum | ed18fdc | 1993-07-11 19:55:34 +0000 | [diff] [blame] | 124 | object *caller; |
Guido van Rossum | 81daa32 | 1993-05-20 14:24:46 +0000 | [diff] [blame] | 125 | { |
| 126 | register accessobject *ap; |
| 127 | if (!is_accessobject(op)) { |
| 128 | err_badcall(); |
| 129 | return NULL; |
| 130 | } |
| 131 | ap = (accessobject *)op; |
| 132 | |
Guido van Rossum | ed18fdc | 1993-07-11 19:55:34 +0000 | [diff] [blame] | 133 | if (!ownercheck(caller, ap->ac_owner, AC_R, ap->ac_mode)) { |
Guido van Rossum | 81daa32 | 1993-05-20 14:24:46 +0000 | [diff] [blame] | 134 | err_setstr(AccessError, "read access denied"); |
| 135 | return NULL; |
| 136 | } |
| 137 | |
| 138 | if (ap->ac_value == NULL) { |
| 139 | err_setstr(AccessError, "no current value"); |
| 140 | return NULL; |
| 141 | } |
| 142 | INCREF(ap->ac_value); |
| 143 | return ap->ac_value; |
| 144 | } |
| 145 | |
| 146 | int |
Guido van Rossum | ed18fdc | 1993-07-11 19:55:34 +0000 | [diff] [blame] | 147 | setaccessvalue(op, caller, value) |
Guido van Rossum | 81daa32 | 1993-05-20 14:24:46 +0000 | [diff] [blame] | 148 | object *op; |
Guido van Rossum | ed18fdc | 1993-07-11 19:55:34 +0000 | [diff] [blame] | 149 | object *caller; |
Guido van Rossum | 81daa32 | 1993-05-20 14:24:46 +0000 | [diff] [blame] | 150 | object *value; |
| 151 | { |
| 152 | register accessobject *ap; |
| 153 | if (!is_accessobject(op)) { |
| 154 | err_badcall(); |
| 155 | return -1; |
| 156 | } |
| 157 | ap = (accessobject *)op; |
| 158 | |
Guido van Rossum | ed18fdc | 1993-07-11 19:55:34 +0000 | [diff] [blame] | 159 | if (!ownercheck(caller, ap->ac_owner, AC_W, ap->ac_mode)) { |
Guido van Rossum | 81daa32 | 1993-05-20 14:24:46 +0000 | [diff] [blame] | 160 | err_setstr(AccessError, "write access denied"); |
| 161 | return -1; |
| 162 | } |
| 163 | |
| 164 | if (!typecheck(value, ap->ac_type)) { |
| 165 | err_setstr(AccessError, "assign value of inappropriate type"); |
| 166 | return -1; |
| 167 | } |
| 168 | |
| 169 | if (value == NULL) { /* Delete it */ |
| 170 | if (ap->ac_value == NULL) { |
| 171 | err_setstr(AccessError, "no current value"); |
| 172 | return -1; |
| 173 | } |
| 174 | DECREF(ap->ac_value); |
| 175 | ap->ac_value = NULL; |
| 176 | return 0; |
| 177 | } |
| 178 | XDECREF(ap->ac_value); |
| 179 | INCREF(value); |
| 180 | ap->ac_value = value; |
| 181 | return 0; |
| 182 | } |
| 183 | |
| 184 | static int |
| 185 | typecheck(value, type) |
| 186 | object *value; |
| 187 | typeobject *type; |
| 188 | { |
| 189 | object *x; |
| 190 | if (value == NULL || type == NULL) |
| 191 | return 1; /* No check */ |
| 192 | if (value->ob_type == type) |
| 193 | return 1; /* Exact match */ |
| 194 | if (type == &Anynumbertype) { |
| 195 | if (value->ob_type->tp_as_number == NULL) |
| 196 | return 0; |
| 197 | if (!is_instanceobject(value)) |
| 198 | return 1; |
| 199 | /* For instances, make sure it really looks like a number */ |
| 200 | x = getattr(value, "__sub__"); |
| 201 | if (x == NULL) { |
| 202 | err_clear(); |
| 203 | return 0; |
| 204 | } |
| 205 | DECREF(x); |
| 206 | return 1; |
| 207 | } |
| 208 | if (type == &Anysequencetype) { |
| 209 | if (value->ob_type->tp_as_sequence == NULL) |
| 210 | return 0; |
| 211 | if (!is_instanceobject(value)) |
| 212 | return 1; |
| 213 | /* For instances, make sure it really looks like a sequence */ |
| 214 | x = getattr(value, "__getslice__"); |
| 215 | if (x == NULL) { |
| 216 | err_clear(); |
| 217 | return 0; |
| 218 | } |
| 219 | DECREF(x); |
| 220 | return 1; |
| 221 | } |
| 222 | if (type == &Anymappingtype) { |
| 223 | if (value->ob_type->tp_as_mapping == NULL) |
| 224 | return 0; |
| 225 | if (!is_instanceobject(value)) |
| 226 | return 1; |
| 227 | /* For instances, make sure it really looks like a mapping */ |
| 228 | x = getattr(value, "__getitem__"); |
| 229 | if (x == NULL) { |
| 230 | err_clear(); |
| 231 | return 0; |
| 232 | } |
| 233 | DECREF(x); |
| 234 | return 1; |
| 235 | } |
| 236 | return 0; |
| 237 | } |
| 238 | |
| 239 | static int |
Guido van Rossum | ed18fdc | 1993-07-11 19:55:34 +0000 | [diff] [blame] | 240 | isprivileged(caller) |
| 241 | object *caller; |
| 242 | { |
| 243 | object *g; |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 244 | static char privileged[] = "__privileged__"; |
| 245 | if (caller != NULL && hasattr(caller, privileged)) |
Guido van Rossum | ed18fdc | 1993-07-11 19:55:34 +0000 | [diff] [blame] | 246 | return 1; |
| 247 | g = getglobals(); |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 248 | if (g != NULL && dictlookup(g, privileged)) |
Guido van Rossum | ed18fdc | 1993-07-11 19:55:34 +0000 | [diff] [blame] | 249 | return 1; |
| 250 | return 0; |
| 251 | } |
| 252 | |
| 253 | static int |
Guido van Rossum | eb6b33a | 1993-05-25 09:38:27 +0000 | [diff] [blame] | 254 | ownercheck(caller, owner, access, mode) |
Guido van Rossum | 81daa32 | 1993-05-20 14:24:46 +0000 | [diff] [blame] | 255 | object *caller; |
| 256 | object *owner; |
| 257 | int access; |
| 258 | int mode; |
| 259 | { |
Guido van Rossum | eb6b33a | 1993-05-25 09:38:27 +0000 | [diff] [blame] | 260 | int mask = AC_PUBLIC; |
Guido van Rossum | ed18fdc | 1993-07-11 19:55:34 +0000 | [diff] [blame] | 261 | if (caller == owner || isprivileged(caller)) |
| 262 | mask |= AC_PRIVATE | AC_PROTECTED; |
| 263 | else if (caller != NULL && owner != NULL && |
| 264 | is_classobject(owner) && is_classobject(caller) && |
| 265 | (issubclass(caller, owner) || |
| 266 | issubclass(owner, caller))) |
Guido van Rossum | eb6b33a | 1993-05-25 09:38:27 +0000 | [diff] [blame] | 267 | mask |= AC_PROTECTED; |
Guido van Rossum | eb6b33a | 1993-05-25 09:38:27 +0000 | [diff] [blame] | 268 | return access & mode & mask; |
Guido van Rossum | 81daa32 | 1993-05-20 14:24:46 +0000 | [diff] [blame] | 269 | } |
| 270 | |
| 271 | /* Access methods */ |
| 272 | |
| 273 | static void |
| 274 | access_dealloc(ap) |
| 275 | accessobject *ap; |
| 276 | { |
| 277 | XDECREF(ap->ac_value); |
Guido van Rossum | eb6b33a | 1993-05-25 09:38:27 +0000 | [diff] [blame] | 278 | XDECREF(ap->ac_owner); |
Guido van Rossum | 81daa32 | 1993-05-20 14:24:46 +0000 | [diff] [blame] | 279 | XDECREF(ap->ac_type); |
| 280 | DEL(ap); |
| 281 | } |
| 282 | |
| 283 | #define OFF(x) offsetof(accessobject, x) |
| 284 | |
| 285 | static struct memberlist access_memberlist[] = { |
| 286 | {"ac_value", T_OBJECT, OFF(ac_value)}, |
Guido van Rossum | eb6b33a | 1993-05-25 09:38:27 +0000 | [diff] [blame] | 287 | {"ac_owner", T_OBJECT, OFF(ac_owner)}, |
Guido van Rossum | 81daa32 | 1993-05-20 14:24:46 +0000 | [diff] [blame] | 288 | {"ac_type", T_OBJECT, OFF(ac_type)}, |
| 289 | {"ac_mode", T_INT, OFF(ac_mode)}, |
| 290 | {NULL} /* Sentinel */ |
| 291 | }; |
| 292 | |
| 293 | static object * |
| 294 | access_getattr(ap, name) |
| 295 | accessobject *ap; |
| 296 | char *name; |
| 297 | { |
| 298 | return getmember((char *)ap, access_memberlist, name); |
| 299 | } |
| 300 | |
| 301 | static object * |
| 302 | access_repr(ap) |
| 303 | accessobject *ap; |
| 304 | { |
| 305 | char buf[300]; |
Guido van Rossum | eb6b33a | 1993-05-25 09:38:27 +0000 | [diff] [blame] | 306 | char buf2[20]; |
| 307 | char *ownername; |
Guido van Rossum | 81daa32 | 1993-05-20 14:24:46 +0000 | [diff] [blame] | 308 | typeobject *type = ap->ac_type; |
Guido van Rossum | eb6b33a | 1993-05-25 09:38:27 +0000 | [diff] [blame] | 309 | if (is_classobject(ap->ac_owner)) { |
| 310 | ownername = |
| 311 | getstringvalue(((classobject *)ap->ac_owner)->cl_name); |
| 312 | } |
| 313 | else { |
| 314 | sprintf(buf2, "0x%lx", (long)ap->ac_owner); |
| 315 | ownername = buf2; |
| 316 | } |
Guido van Rossum | b3f7258 | 1993-05-21 19:56:10 +0000 | [diff] [blame] | 317 | sprintf(buf, |
Guido van Rossum | eb6b33a | 1993-05-25 09:38:27 +0000 | [diff] [blame] | 318 | "<access object, value 0x%lx, owner %.100s, type %.100s, mode %04o>", |
Guido van Rossum | b3f7258 | 1993-05-21 19:56:10 +0000 | [diff] [blame] | 319 | (long)(ap->ac_value), |
Guido van Rossum | eb6b33a | 1993-05-25 09:38:27 +0000 | [diff] [blame] | 320 | ownername, |
Guido van Rossum | 81daa32 | 1993-05-20 14:24:46 +0000 | [diff] [blame] | 321 | type ? type->tp_name : "-", |
| 322 | ap->ac_mode); |
| 323 | return newstringobject(buf); |
| 324 | } |
| 325 | |
| 326 | typeobject Accesstype = { |
| 327 | OB_HEAD_INIT(&Typetype) |
| 328 | 0, /*ob_size*/ |
| 329 | "access", /*tp_name*/ |
| 330 | sizeof(accessobject), /*tp_size*/ |
| 331 | 0, /*tp_itemsize*/ |
| 332 | /* methods */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 333 | (destructor)access_dealloc, /*tp_dealloc*/ |
Guido van Rossum | 81daa32 | 1993-05-20 14:24:46 +0000 | [diff] [blame] | 334 | 0, /*tp_print*/ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 335 | (getattrfunc)access_getattr, /*tp_getattr*/ |
Guido van Rossum | 81daa32 | 1993-05-20 14:24:46 +0000 | [diff] [blame] | 336 | 0, /*tp_setattr*/ |
| 337 | 0, /*tp_compare*/ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 338 | (reprfunc)access_repr, /*tp_repr*/ |
Guido van Rossum | 81daa32 | 1993-05-20 14:24:46 +0000 | [diff] [blame] | 339 | 0, /*tp_as_number*/ |
| 340 | 0, /*tp_as_sequence*/ |
| 341 | 0, /*tp_as_mapping*/ |
| 342 | 0, /*tp_hash*/ |
| 343 | }; |
| 344 | |
Guido van Rossum | eb6b33a | 1993-05-25 09:38:27 +0000 | [diff] [blame] | 345 | |
| 346 | /* Pseudo type objects to indicate collections of types */ |
Guido van Rossum | 81daa32 | 1993-05-20 14:24:46 +0000 | [diff] [blame] | 347 | |
| 348 | /* XXX This should be replaced by a more general "subclassing" |
| 349 | XXX mechanism for type objects... */ |
| 350 | |
| 351 | typeobject Anynumbertype = { |
| 352 | OB_HEAD_INIT(&Typetype) |
| 353 | 0, /*ob_size*/ |
| 354 | "*number*", /*tp_name*/ |
| 355 | }; |
| 356 | |
| 357 | /* XXX Should really distinguish mutable and immutable sequences as well */ |
| 358 | |
| 359 | typeobject Anysequencetype = { |
| 360 | OB_HEAD_INIT(&Typetype) |
| 361 | 0, /*ob_size*/ |
| 362 | "*sequence*", /*tp_name*/ |
| 363 | }; |
| 364 | |
| 365 | typeobject Anymappingtype = { |
| 366 | OB_HEAD_INIT(&Typetype) |
| 367 | 0, /*ob_size*/ |
| 368 | "*mapping*", /*tp_name*/ |
| 369 | }; |
Guido van Rossum | 0dfcf75 | 1996-08-12 22:00:53 +0000 | [diff] [blame] | 370 | |
| 371 | #endif /* SUPPORT_OBSOLETE_ACCESS */ |