home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 15 / AACD15.ISO / AACD / Programming / Python2 / Python20_source / Objects / moduleobject.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-10-25  |  4.7 KB  |  210 lines

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