home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / pyth_os2.zip / python-1.0.2 / Objects / accessobject.c next >
C/C++ Source or Header  |  1994-01-11  |  8KB  |  361 lines

  1. /***********************************************************
  2. Copyright 1991, 1992, 1993, 1994 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.  
  27. /* XXX TO DO LIST
  28.    - __init__ and __del__ (and all other similar methods)
  29.      should be usable even when private, not ignored
  30. */
  31.  
  32. #include "allobjects.h"
  33. #include "ceval.h"
  34. #include "structmember.h"
  35. #include "modsupport.h"        /* For getargs() etc. */
  36.  
  37. typedef struct {
  38.     OB_HEAD
  39.     object        *ac_value;
  40.     object        *ac_owner;
  41.     typeobject    *ac_type;
  42.     int        ac_mode;
  43. } accessobject;
  44.  
  45. /* Forward */
  46. static int typecheck PROTO((object *, typeobject *));
  47. static int ownercheck PROTO((object *, object *, int, int));
  48.  
  49. object *
  50. newaccessobject(value, owner, type, mode)
  51.     object *value;
  52.     object *owner;
  53.     typeobject *type;
  54.     int mode;
  55. {
  56.     accessobject *ap;
  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;
  67.     XINCREF(owner);
  68.     ap->ac_owner = owner;
  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;
  85.     return newaccessobject(ap->ac_value, ap->ac_owner,
  86.                    ap->ac_type, ap->ac_mode);
  87. }
  88.  
  89. void
  90. setaccessowner(op, owner)
  91.     object *op;
  92.     object *owner;
  93. {
  94.     register accessobject *ap;
  95.     if (!is_accessobject(op))
  96.         return; /* XXX no error */
  97.     ap = (accessobject *)op;
  98.     XDECREF(ap->ac_owner);
  99.     XINCREF(owner);
  100.     ap->ac_owner = owner;
  101. }
  102.  
  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.  
  112. object *
  113. getaccessvalue(op, caller)
  114.     object *op;
  115.     object *caller;
  116. {
  117.     register accessobject *ap;
  118.     if (!is_accessobject(op)) {
  119.         err_badcall();
  120.         return NULL;
  121.     }
  122.     ap = (accessobject *)op;
  123.     
  124.     if (!ownercheck(caller, ap->ac_owner, AC_R, ap->ac_mode)) {
  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
  138. setaccessvalue(op, caller, value)
  139.     object *op;
  140.     object *caller;
  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.     
  150.     if (!ownercheck(caller, ap->ac_owner, AC_W, ap->ac_mode)) {
  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
  231. isprivileged(caller)
  232.     object *caller;
  233. {
  234.     object *g;
  235.     static char privileged[] = "__privileged__";
  236.     if (caller != NULL && hasattr(caller, privileged))
  237.         return 1;
  238.     g = getglobals();
  239.     if (g != NULL && dictlookup(g, privileged))
  240.         return 1;
  241.     return 0;
  242. }
  243.  
  244. static int
  245. ownercheck(caller, owner, access, mode)
  246.     object *caller;
  247.     object *owner;
  248.     int access;
  249.     int mode;
  250. {
  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;
  260. }
  261.  
  262. /* Access methods */
  263.  
  264. static void
  265. access_dealloc(ap)
  266.     accessobject *ap;
  267. {
  268.     XDECREF(ap->ac_value);
  269.     XDECREF(ap->ac_owner);
  270.     XDECREF(ap->ac_type);
  271.     DEL(ap);
  272. }
  273.  
  274. #define OFF(x) offsetof(accessobject, x)
  275.  
  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 */
  282. };
  283.  
  284. static object *
  285. access_getattr(ap, name)
  286.     accessobject *ap;
  287.     char *name;
  288. {
  289.     return getmember((char *)ap, access_memberlist, name);
  290. }
  291.  
  292. static object *
  293. access_repr(ap)
  294.     accessobject *ap;
  295. {
  296.     char buf[300];
  297.     char buf2[20];
  298.     char *ownername;
  299.     typeobject *type = ap->ac_type;
  300.     if (is_classobject(ap->ac_owner)) {
  301.         ownername =
  302.             getstringvalue(((classobject *)ap->ac_owner)->cl_name);
  303.     }
  304.     else {
  305.         sprintf(buf2, "0x%lx", (long)ap->ac_owner);
  306.         ownername = buf2;
  307.     }
  308.     sprintf(buf,
  309.     "<access object, value 0x%lx, owner %.100s, type %.100s, mode %04o>",
  310.         (long)(ap->ac_value),
  311.         ownername,
  312.         type ? type->tp_name : "-",
  313.         ap->ac_mode);
  314.     return newstringobject(buf);
  315. }
  316.  
  317. typeobject Accesstype = {
  318.     OB_HEAD_INIT(&Typetype)
  319.     0,            /*ob_size*/
  320.     "access",        /*tp_name*/
  321.     sizeof(accessobject),    /*tp_size*/
  322.     0,            /*tp_itemsize*/
  323.     /* methods */
  324.     (destructor)access_dealloc, /*tp_dealloc*/
  325.     0,            /*tp_print*/
  326.     (getattrfunc)access_getattr, /*tp_getattr*/
  327.     0,            /*tp_setattr*/
  328.     0,            /*tp_compare*/
  329.     (reprfunc)access_repr, /*tp_repr*/
  330.     0,            /*tp_as_number*/
  331.     0,            /*tp_as_sequence*/
  332.     0,            /*tp_as_mapping*/
  333.     0,            /*tp_hash*/
  334. };
  335.  
  336.  
  337. /* Pseudo type objects to indicate collections of types */
  338.  
  339. /* XXX This should be replaced by a more general "subclassing"
  340.    XXX mechanism for type objects... */
  341.  
  342. typeobject Anynumbertype = {
  343.     OB_HEAD_INIT(&Typetype)
  344.     0,            /*ob_size*/
  345.     "*number*",        /*tp_name*/
  346. };
  347.  
  348. /* XXX Should really distinguish mutable and immutable sequences as well */
  349.  
  350. typeobject Anysequencetype = {
  351.     OB_HEAD_INIT(&Typetype)
  352.     0,            /*ob_size*/
  353.     "*sequence*",        /*tp_name*/
  354. };
  355.  
  356. typeobject Anymappingtype = {
  357.     OB_HEAD_INIT(&Typetype)
  358.     0,            /*ob_size*/
  359.     "*mapping*",        /*tp_name*/
  360. };
  361.