home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / pyth_os2.zip / python-1.0.2 / Objects / moduleobject.c < prev    next >
C/C++ Source or Header  |  1994-01-04  |  4KB  |  172 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. /* Module object implementation */
  26.  
  27. #include "allobjects.h"
  28. #include "ceval.h"
  29.  
  30. typedef struct {
  31.     OB_HEAD
  32.     object *md_dict;
  33. } moduleobject;
  34.  
  35. object *
  36. newmoduleobject(name)
  37.     char *name;
  38. {
  39.     moduleobject *m;
  40.     object *nameobj;
  41.     m = NEWOBJ(moduleobject, &Moduletype);
  42.     if (m == NULL)
  43.         return NULL;
  44.     nameobj = newstringobject(name);
  45.     m->md_dict = newdictobject();
  46.     if (m->md_dict == NULL || nameobj == NULL)
  47.         goto fail;
  48.     if (dictinsert(m->md_dict, "__name__", nameobj) != 0)
  49.         goto fail;
  50.     DECREF(nameobj);
  51.     return (object *)m;
  52.  
  53.  fail:
  54.     XDECREF(nameobj);
  55.     DECREF(m);
  56.     return NULL;
  57. }
  58.  
  59. object *
  60. getmoduledict(m)
  61.     object *m;
  62. {
  63.     if (!is_moduleobject(m)) {
  64.         err_badcall();
  65.         return NULL;
  66.     }
  67.     return ((moduleobject *)m) -> md_dict;
  68. }
  69.  
  70. char *
  71. getmodulename(m)
  72.     object *m;
  73. {
  74.     object *nameobj;
  75.     if (!is_moduleobject(m)) {
  76.         err_badarg();
  77.         return NULL;
  78.     }
  79.     nameobj = dictlookup(((moduleobject *)m)->md_dict, "__name__");
  80.     if (nameobj == NULL || !is_stringobject(nameobj)) {
  81.         err_setstr(SystemError, "nameless module");
  82.         return NULL;
  83.     }
  84.     return getstringvalue(nameobj);
  85. }
  86.  
  87. /* Methods */
  88.  
  89. static void
  90. module_dealloc(m)
  91.     moduleobject *m;
  92. {
  93.     if (m->md_dict != NULL)
  94.         DECREF(m->md_dict);
  95.     free((char *)m);
  96. }
  97.  
  98. static object *
  99. module_repr(m)
  100.     moduleobject *m;
  101. {
  102.     char buf[100];
  103.     char *name = getmodulename((object *)m);
  104.     if (name == NULL) {
  105.         err_clear();
  106.         name = "?";
  107.     }
  108.     sprintf(buf, "<module '%.80s'>", name);
  109.     return newstringobject(buf);
  110. }
  111.  
  112. static object *
  113. module_getattr(m, name)
  114.     moduleobject *m;
  115.     char *name;
  116. {
  117.     object *res;
  118.     if (strcmp(name, "__dict__") == 0) {
  119.         INCREF(m->md_dict);
  120.         return m->md_dict;
  121.     }
  122.     res = dictlookup(m->md_dict, name);
  123.     if (res == NULL)
  124.         err_setstr(AttributeError, name);
  125.     else {
  126.         if (is_accessobject(res))
  127.             res = getaccessvalue(res, getglobals());
  128.         else
  129.             INCREF(res);
  130.     }
  131.     return res;
  132. }
  133.  
  134. static int
  135. module_setattr(m, name, v)
  136.     moduleobject *m;
  137.     char *name;
  138.     object *v;
  139. {
  140.     object *ac;
  141.     if (name[0] == '_' && strcmp(name, "__dict__") == 0) {
  142.         err_setstr(TypeError, "read-only special attribute");
  143.         return -1;
  144.     }
  145.     ac = dictlookup(m->md_dict, name);
  146.     if (ac != NULL && is_accessobject(ac))
  147.         return setaccessvalue(ac, getglobals(), v);
  148.     if (v == NULL) {
  149.         int rv = dictremove(m->md_dict, name);
  150.         if (rv < 0)
  151.             err_setstr(AttributeError,
  152.                    "delete non-existing module attribute");
  153.         return rv;
  154.     }
  155.     else
  156.         return dictinsert(m->md_dict, name, v);
  157. }
  158.  
  159. typeobject Moduletype = {
  160.     OB_HEAD_INIT(&Typetype)
  161.     0,            /*ob_size*/
  162.     "module",        /*tp_name*/
  163.     sizeof(moduleobject),    /*tp_size*/
  164.     0,            /*tp_itemsize*/
  165.     (destructor)module_dealloc, /*tp_dealloc*/
  166.     0,            /*tp_print*/
  167.     (getattrfunc)module_getattr, /*tp_getattr*/
  168.     (setattrfunc)module_setattr, /*tp_setattr*/
  169.     0,            /*tp_compare*/
  170.     (reprfunc)module_repr, /*tp_repr*/
  171. };
  172.