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