home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Source Code / C / Applications / Python 1.4 / Python 1.4 source / Objects / moduleobject.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-10-28  |  4.3 KB  |  189 lines  |  [TEXT/CWIE]

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