blob: 4267896bf0241cc72cbd34296bb07cecbac726df [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002/* Module object implementation */
3
Guido van Rossumc0b618a1997-05-02 03:12:38 +00004#include "Python.h"
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00005
6typedef struct {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00007 PyObject_HEAD
8 PyObject *md_dict;
9} PyModuleObject;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000010
Guido van Rossumc0b618a1997-05-02 03:12:38 +000011PyObject *
Fred Drakeee238b92000-07-09 06:03:25 +000012PyModule_New(char *name)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000013{
Guido van Rossumc0b618a1997-05-02 03:12:38 +000014 PyModuleObject *m;
15 PyObject *nameobj;
16 m = PyObject_NEW(PyModuleObject, &PyModule_Type);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000017 if (m == NULL)
18 return NULL;
Guido van Rossumc0b618a1997-05-02 03:12:38 +000019 nameobj = PyString_FromString(name);
20 m->md_dict = PyDict_New();
Guido van Rossumc45611d1993-11-17 22:58:56 +000021 if (m->md_dict == NULL || nameobj == NULL)
22 goto fail;
Guido van Rossumc0b618a1997-05-02 03:12:38 +000023 if (PyDict_SetItemString(m->md_dict, "__name__", nameobj) != 0)
Guido van Rossumc45611d1993-11-17 22:58:56 +000024 goto fail;
Guido van Rossumc0b618a1997-05-02 03:12:38 +000025 if (PyDict_SetItemString(m->md_dict, "__doc__", Py_None) != 0)
Guido van Rossum8b14b4c1995-01-07 11:59:29 +000026 goto fail;
Guido van Rossumc0b618a1997-05-02 03:12:38 +000027 Py_DECREF(nameobj);
28 return (PyObject *)m;
Guido van Rossumc45611d1993-11-17 22:58:56 +000029
30 fail:
Guido van Rossumc0b618a1997-05-02 03:12:38 +000031 Py_XDECREF(nameobj);
32 Py_DECREF(m);
Guido van Rossumc45611d1993-11-17 22:58:56 +000033 return NULL;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000034}
35
Guido van Rossumc0b618a1997-05-02 03:12:38 +000036PyObject *
Fred Drakeee238b92000-07-09 06:03:25 +000037PyModule_GetDict(PyObject *m)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000038{
Guido van Rossumc0b618a1997-05-02 03:12:38 +000039 if (!PyModule_Check(m)) {
40 PyErr_BadInternalCall();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000041 return NULL;
42 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +000043 return ((PyModuleObject *)m) -> md_dict;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000044}
45
Guido van Rossum0558a201990-10-26 15:00:11 +000046char *
Fred Drakeee238b92000-07-09 06:03:25 +000047PyModule_GetName(PyObject *m)
Guido van Rossum0558a201990-10-26 15:00:11 +000048{
Guido van Rossumc0b618a1997-05-02 03:12:38 +000049 PyObject *nameobj;
50 if (!PyModule_Check(m)) {
51 PyErr_BadArgument();
Guido van Rossum0558a201990-10-26 15:00:11 +000052 return NULL;
53 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +000054 nameobj = PyDict_GetItemString(((PyModuleObject *)m)->md_dict,
55 "__name__");
56 if (nameobj == NULL || !PyString_Check(nameobj)) {
57 PyErr_SetString(PyExc_SystemError, "nameless module");
Guido van Rossumc45611d1993-11-17 22:58:56 +000058 return NULL;
59 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +000060 return PyString_AsString(nameobj);
Guido van Rossum0558a201990-10-26 15:00:11 +000061}
62
Guido van Rossum98cc19f1999-02-15 14:47:16 +000063char *
Fred Drakeee238b92000-07-09 06:03:25 +000064PyModule_GetFilename(PyObject *m)
Guido van Rossum98cc19f1999-02-15 14:47:16 +000065{
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
Guido van Rossumf1dc0611998-02-19 20:51:52 +000080void
Fred Drakeee238b92000-07-09 06:03:25 +000081_PyModule_Clear(PyObject *m)
Guido van Rossumf1dc0611998-02-19 20:51:52 +000082{
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)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000103 PySys_WriteStderr("# clear[1] %s\n", s);
Guido van Rossumf1dc0611998-02-19 20:51:52 +0000104 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)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000116 PySys_WriteStderr("# clear[2] %s\n", s);
Guido van Rossumf1dc0611998-02-19 20:51:52 +0000117 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
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000128/* Methods */
129
130static void
Fred Drakeee238b92000-07-09 06:03:25 +0000131module_dealloc(PyModuleObject *m)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000132{
Guido van Rossum27e916f1995-01-26 00:39:00 +0000133 if (m->md_dict != NULL) {
Guido van Rossumf1dc0611998-02-19 20:51:52 +0000134 _PyModule_Clear((PyObject *)m);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000135 Py_DECREF(m->md_dict);
Guido van Rossum27e916f1995-01-26 00:39:00 +0000136 }
Guido van Rossumb18618d2000-05-03 23:44:39 +0000137 PyObject_DEL(m);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000138}
139
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000140static PyObject *
Fred Drakeee238b92000-07-09 06:03:25 +0000141module_repr(PyModuleObject *m)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000142{
Guido van Rossum98cc19f1999-02-15 14:47:16 +0000143 char buf[400];
144 char *name;
145 char *filename;
146 name = PyModule_GetName((PyObject *)m);
Guido van Rossumc45611d1993-11-17 22:58:56 +0000147 if (name == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000148 PyErr_Clear();
Guido van Rossumc45611d1993-11-17 22:58:56 +0000149 name = "?";
150 }
Guido van Rossum98cc19f1999-02-15 14:47:16 +0000151 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
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000159 return PyString_FromString(buf);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000160}
161
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000162static PyObject *
Fred Drakeee238b92000-07-09 06:03:25 +0000163module_getattr(PyModuleObject *m, char *name)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000164{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000165 PyObject *res;
Fred Drake661ea262000-10-24 19:57:45 +0000166 char* modname;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000167 if (strcmp(name, "__dict__") == 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000168 Py_INCREF(m->md_dict);
Guido van Rossume87203a1990-10-21 22:12:30 +0000169 return m->md_dict;
170 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000171 res = PyDict_GetItemString(m->md_dict, name);
Fred Drake661ea262000-10-24 19:57:45 +0000172 if (res == NULL) {
173 modname = PyModule_GetName((PyObject *)m);
174 if (modname == NULL) {
175 PyErr_Clear();
176 modname = "?";
177 }
178 PyErr_Format(PyExc_AttributeError,
179 "'%.50s' module has no attribute '%.400s'",
180 modname, name);
181 }
Guido van Rossum0add15f1997-05-09 01:07:15 +0000182 else
183 Py_INCREF(res);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000184 return res;
185}
186
187static int
Fred Drakeee238b92000-07-09 06:03:25 +0000188module_setattr(PyModuleObject *m, char *name, PyObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000189{
Fred Drake661ea262000-10-24 19:57:45 +0000190 char* modname;
Guido van Rossumc45611d1993-11-17 22:58:56 +0000191 if (name[0] == '_' && strcmp(name, "__dict__") == 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000192 PyErr_SetString(PyExc_TypeError,
193 "read-only special attribute");
Guido van Rossumc45611d1993-11-17 22:58:56 +0000194 return -1;
Guido van Rossume87203a1990-10-21 22:12:30 +0000195 }
Guido van Rossum94472a01992-09-04 09:45:18 +0000196 if (v == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000197 int rv = PyDict_DelItemString(m->md_dict, name);
Fred Drake661ea262000-10-24 19:57:45 +0000198 if (rv < 0) {
199 modname = PyModule_GetName((PyObject *)m);
200 if (modname == NULL) {
201 PyErr_Clear();
202 modname = "?";
203 }
204 PyErr_Format(PyExc_AttributeError,
205 "'%.50s' module has no attribute '%.400s'",
206 modname, name);
207 }
Guido van Rossum94472a01992-09-04 09:45:18 +0000208 return rv;
209 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000210 else
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000211 return PyDict_SetItemString(m->md_dict, name, v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000212}
213
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000214PyTypeObject PyModule_Type = {
215 PyObject_HEAD_INIT(&PyType_Type)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000216 0, /*ob_size*/
217 "module", /*tp_name*/
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000218 sizeof(PyModuleObject), /*tp_size*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000219 0, /*tp_itemsize*/
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000220 (destructor)module_dealloc, /*tp_dealloc*/
Guido van Rossum7066dd71992-09-17 17:54:56 +0000221 0, /*tp_print*/
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000222 (getattrfunc)module_getattr, /*tp_getattr*/
223 (setattrfunc)module_setattr, /*tp_setattr*/
Guido van Rossum3f5da241990-12-20 15:06:42 +0000224 0, /*tp_compare*/
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000225 (reprfunc)module_repr, /*tp_repr*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000226};