home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / py2s152.zip / Objects / moduleobject.c < prev    next >
C/C++ Source or Header  |  1999-06-27  |  7KB  |  252 lines

  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 "Python.h"
  35.  
  36. typedef struct {
  37.     PyObject_HEAD
  38.     PyObject *md_dict;
  39. } PyModuleObject;
  40.  
  41. PyObject *
  42. PyModule_New(name)
  43.     char *name;
  44. {
  45.     PyModuleObject *m;
  46.     PyObject *nameobj;
  47.     m = PyObject_NEW(PyModuleObject, &PyModule_Type);
  48.     if (m == NULL)
  49.         return NULL;
  50.     nameobj = PyString_FromString(name);
  51.     m->md_dict = PyDict_New();
  52.     if (m->md_dict == NULL || nameobj == NULL)
  53.         goto fail;
  54.     if (PyDict_SetItemString(m->md_dict, "__name__", nameobj) != 0)
  55.         goto fail;
  56.     if (PyDict_SetItemString(m->md_dict, "__doc__", Py_None) != 0)
  57.         goto fail;
  58.     Py_DECREF(nameobj);
  59.     return (PyObject *)m;
  60.  
  61.  fail:
  62.     Py_XDECREF(nameobj);
  63.     Py_DECREF(m);
  64.     return NULL;
  65. }
  66.  
  67. PyObject *
  68. PyModule_GetDict(m)
  69.     PyObject *m;
  70. {
  71.     if (!PyModule_Check(m)) {
  72.         PyErr_BadInternalCall();
  73.         return NULL;
  74.     }
  75.     return ((PyModuleObject *)m) -> md_dict;
  76. }
  77.  
  78. char *
  79. PyModule_GetName(m)
  80.     PyObject *m;
  81. {
  82.     PyObject *nameobj;
  83.     if (!PyModule_Check(m)) {
  84.         PyErr_BadArgument();
  85.         return NULL;
  86.     }
  87.     nameobj = PyDict_GetItemString(((PyModuleObject *)m)->md_dict,
  88.                        "__name__");
  89.     if (nameobj == NULL || !PyString_Check(nameobj)) {
  90.         PyErr_SetString(PyExc_SystemError, "nameless module");
  91.         return NULL;
  92.     }
  93.     return PyString_AsString(nameobj);
  94. }
  95.  
  96. char *
  97. PyModule_GetFilename(m)
  98.         PyObject *m;
  99. {
  100.     PyObject *fileobj;
  101.     if (!PyModule_Check(m)) {
  102.         PyErr_BadArgument();
  103.         return NULL;
  104.     }
  105.     fileobj = PyDict_GetItemString(((PyModuleObject *)m)->md_dict,
  106.                        "__file__");
  107.     if (fileobj == NULL || !PyString_Check(fileobj)) {
  108.         PyErr_SetString(PyExc_SystemError, "module filename missing");
  109.         return NULL;
  110.     }
  111.     return PyString_AsString(fileobj);
  112. }
  113.  
  114. void
  115. _PyModule_Clear(m)
  116.     PyObject *m;
  117. {
  118.     /* To make the execution order of destructors for global
  119.        objects a bit more predictable, we first zap all objects
  120.        whose name starts with a single underscore, before we clear
  121.        the entire dictionary.  We zap them by replacing them with
  122.        None, rather than deleting them from the dictionary, to
  123.        avoid rehashing the dictionary (to some extent). */
  124.  
  125.     int pos;
  126.     PyObject *key, *value;
  127.     PyObject *d;
  128.  
  129.     d = ((PyModuleObject *)m)->md_dict;
  130.  
  131.     /* First, clear only names starting with a single underscore */
  132.     pos = 0;
  133.     while (PyDict_Next(d, &pos, &key, &value)) {
  134.         if (value != Py_None && PyString_Check(key)) {
  135.             char *s = PyString_AsString(key);
  136.             if (s[0] == '_' && s[1] != '_') {
  137.                 if (Py_VerboseFlag > 1)
  138.                     PySys_WriteStderr("#   clear[1] %s\n", s);
  139.                 PyDict_SetItem(d, key, Py_None);
  140.             }
  141.         }
  142.     }
  143.  
  144.     /* Next, clear all names except for __builtins__ */
  145.     pos = 0;
  146.     while (PyDict_Next(d, &pos, &key, &value)) {
  147.         if (value != Py_None && PyString_Check(key)) {
  148.             char *s = PyString_AsString(key);
  149.             if (s[0] != '_' || strcmp(s, "__builtins__") != 0) {
  150.                 if (Py_VerboseFlag > 1)
  151.                     PySys_WriteStderr("#   clear[2] %s\n", s);
  152.                 PyDict_SetItem(d, key, Py_None);
  153.             }
  154.         }
  155.     }
  156.  
  157.     /* Note: we leave __builtins__ in place, so that destructors
  158.        of non-global objects defined in this module can still use
  159.        builtins, in particularly 'None'. */
  160.  
  161. }
  162.  
  163. /* Methods */
  164.  
  165. static void
  166. module_dealloc(m)
  167.     PyModuleObject *m;
  168. {
  169.     if (m->md_dict != NULL) {
  170.         _PyModule_Clear((PyObject *)m);
  171.         Py_DECREF(m->md_dict);
  172.     }
  173.     free((char *)m);
  174. }
  175.  
  176. static PyObject *
  177. module_repr(m)
  178.     PyModuleObject *m;
  179. {
  180.     char buf[400];
  181.     char *name;
  182.     char *filename;
  183.     name = PyModule_GetName((PyObject *)m);
  184.     if (name == NULL) {
  185.         PyErr_Clear();
  186.         name = "?";
  187.     }
  188.     filename = PyModule_GetFilename((PyObject *)m);
  189.     if (filename == NULL) {
  190.         PyErr_Clear();
  191.         sprintf(buf, "<module '%.80s' (built-in)>", name);
  192.     } else {
  193.         sprintf(buf, "<module '%.80s' from '%.255s'>", name, filename);
  194.     }
  195.  
  196.     return PyString_FromString(buf);
  197. }
  198.  
  199. static PyObject *
  200. module_getattr(m, name)
  201.     PyModuleObject *m;
  202.     char *name;
  203. {
  204.     PyObject *res;
  205.     if (strcmp(name, "__dict__") == 0) {
  206.         Py_INCREF(m->md_dict);
  207.         return m->md_dict;
  208.     }
  209.     res = PyDict_GetItemString(m->md_dict, name);
  210.     if (res == NULL)
  211.         PyErr_SetString(PyExc_AttributeError, name);
  212.     else
  213.         Py_INCREF(res);
  214.     return res;
  215. }
  216.  
  217. static int
  218. module_setattr(m, name, v)
  219.     PyModuleObject *m;
  220.     char *name;
  221.     PyObject *v;
  222. {
  223.     if (name[0] == '_' && strcmp(name, "__dict__") == 0) {
  224.         PyErr_SetString(PyExc_TypeError,
  225.                 "read-only special attribute");
  226.         return -1;
  227.     }
  228.     if (v == NULL) {
  229.         int rv = PyDict_DelItemString(m->md_dict, name);
  230.         if (rv < 0)
  231.             PyErr_SetString(PyExc_AttributeError,
  232.                    "delete non-existing module attribute");
  233.         return rv;
  234.     }
  235.     else
  236.         return PyDict_SetItemString(m->md_dict, name, v);
  237. }
  238.  
  239. PyTypeObject PyModule_Type = {
  240.     PyObject_HEAD_INIT(&PyType_Type)
  241.     0,            /*ob_size*/
  242.     "module",        /*tp_name*/
  243.     sizeof(PyModuleObject),    /*tp_size*/
  244.     0,            /*tp_itemsize*/
  245.     (destructor)module_dealloc, /*tp_dealloc*/
  246.     0,            /*tp_print*/
  247.     (getattrfunc)module_getattr, /*tp_getattr*/
  248.     (setattrfunc)module_setattr, /*tp_setattr*/
  249.     0,            /*tp_compare*/
  250.     (reprfunc)module_repr, /*tp_repr*/
  251. };
  252.