blob: 812cbc4ae893f3317c471e93cc30aa03596ba8b4 [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"
Tim Peters6d6c1a32001-08-02 04:15:00 +00005#include "structmember.h"
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00006
7typedef struct {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00008 PyObject_HEAD
9 PyObject *md_dict;
10} PyModuleObject;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000011
Neil Schemenauerf23473f2001-10-21 22:28:58 +000012static PyMemberDef module_members[] = {
Tim Peters6d6c1a32001-08-02 04:15:00 +000013 {"__dict__", T_OBJECT, offsetof(PyModuleObject, md_dict), READONLY},
14 {0}
15};
16
Guido van Rossumc0b618a1997-05-02 03:12:38 +000017PyObject *
Fred Drakeee238b92000-07-09 06:03:25 +000018PyModule_New(char *name)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000019{
Guido van Rossumc0b618a1997-05-02 03:12:38 +000020 PyModuleObject *m;
21 PyObject *nameobj;
Neil Schemenauere83c00e2001-08-29 23:54:21 +000022 m = PyObject_GC_New(PyModuleObject, &PyModule_Type);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000023 if (m == NULL)
24 return NULL;
Guido van Rossumc0b618a1997-05-02 03:12:38 +000025 nameobj = PyString_FromString(name);
26 m->md_dict = PyDict_New();
Guido van Rossumc45611d1993-11-17 22:58:56 +000027 if (m->md_dict == NULL || nameobj == NULL)
28 goto fail;
Guido van Rossumc0b618a1997-05-02 03:12:38 +000029 if (PyDict_SetItemString(m->md_dict, "__name__", nameobj) != 0)
Guido van Rossumc45611d1993-11-17 22:58:56 +000030 goto fail;
Guido van Rossumc0b618a1997-05-02 03:12:38 +000031 if (PyDict_SetItemString(m->md_dict, "__doc__", Py_None) != 0)
Guido van Rossum8b14b4c1995-01-07 11:59:29 +000032 goto fail;
Guido van Rossumc0b618a1997-05-02 03:12:38 +000033 Py_DECREF(nameobj);
Neil Schemenauere83c00e2001-08-29 23:54:21 +000034 PyObject_GC_Track(m);
Guido van Rossumc0b618a1997-05-02 03:12:38 +000035 return (PyObject *)m;
Guido van Rossumc45611d1993-11-17 22:58:56 +000036
37 fail:
Guido van Rossumc0b618a1997-05-02 03:12:38 +000038 Py_XDECREF(nameobj);
39 Py_DECREF(m);
Guido van Rossumc45611d1993-11-17 22:58:56 +000040 return NULL;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000041}
42
Guido van Rossumc0b618a1997-05-02 03:12:38 +000043PyObject *
Fred Drakeee238b92000-07-09 06:03:25 +000044PyModule_GetDict(PyObject *m)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000045{
Guido van Rossumcd637aa2002-03-12 20:37:02 +000046 PyObject *d;
Guido van Rossumc0b618a1997-05-02 03:12:38 +000047 if (!PyModule_Check(m)) {
48 PyErr_BadInternalCall();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000049 return NULL;
50 }
Guido van Rossumcd637aa2002-03-12 20:37:02 +000051 d = ((PyModuleObject *)m) -> md_dict;
52 if (d == NULL)
53 ((PyModuleObject *)m) -> md_dict = d = PyDict_New();
54 return d;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000055}
56
Guido van Rossum0558a201990-10-26 15:00:11 +000057char *
Fred Drakeee238b92000-07-09 06:03:25 +000058PyModule_GetName(PyObject *m)
Guido van Rossum0558a201990-10-26 15:00:11 +000059{
Guido van Rossumcd637aa2002-03-12 20:37:02 +000060 PyObject *d;
Guido van Rossumc0b618a1997-05-02 03:12:38 +000061 PyObject *nameobj;
62 if (!PyModule_Check(m)) {
63 PyErr_BadArgument();
Guido van Rossum0558a201990-10-26 15:00:11 +000064 return NULL;
65 }
Guido van Rossumcd637aa2002-03-12 20:37:02 +000066 d = ((PyModuleObject *)m)->md_dict;
67 if (d == NULL ||
68 (nameobj = PyDict_GetItemString(d, "__name__")) == NULL ||
69 !PyString_Check(nameobj))
70 {
Guido van Rossumc0b618a1997-05-02 03:12:38 +000071 PyErr_SetString(PyExc_SystemError, "nameless module");
Guido van Rossumc45611d1993-11-17 22:58:56 +000072 return NULL;
73 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +000074 return PyString_AsString(nameobj);
Guido van Rossum0558a201990-10-26 15:00:11 +000075}
76
Guido van Rossum98cc19f1999-02-15 14:47:16 +000077char *
Fred Drakeee238b92000-07-09 06:03:25 +000078PyModule_GetFilename(PyObject *m)
Guido van Rossum98cc19f1999-02-15 14:47:16 +000079{
Guido van Rossumcd637aa2002-03-12 20:37:02 +000080 PyObject *d;
Guido van Rossum98cc19f1999-02-15 14:47:16 +000081 PyObject *fileobj;
82 if (!PyModule_Check(m)) {
83 PyErr_BadArgument();
84 return NULL;
85 }
Guido van Rossumcd637aa2002-03-12 20:37:02 +000086 d = ((PyModuleObject *)m)->md_dict;
87 if (d == NULL ||
88 (fileobj = PyDict_GetItemString(d, "__file__")) == NULL ||
89 !PyString_Check(fileobj))
90 {
Guido van Rossum98cc19f1999-02-15 14:47:16 +000091 PyErr_SetString(PyExc_SystemError, "module filename missing");
92 return NULL;
93 }
94 return PyString_AsString(fileobj);
95}
96
Guido van Rossumf1dc0611998-02-19 20:51:52 +000097void
Fred Drakeee238b92000-07-09 06:03:25 +000098_PyModule_Clear(PyObject *m)
Guido van Rossumf1dc0611998-02-19 20:51:52 +000099{
100 /* To make the execution order of destructors for global
101 objects a bit more predictable, we first zap all objects
102 whose name starts with a single underscore, before we clear
103 the entire dictionary. We zap them by replacing them with
104 None, rather than deleting them from the dictionary, to
105 avoid rehashing the dictionary (to some extent). */
106
107 int pos;
108 PyObject *key, *value;
109 PyObject *d;
110
111 d = ((PyModuleObject *)m)->md_dict;
Guido van Rossumcd637aa2002-03-12 20:37:02 +0000112 if (d == NULL)
113 return;
Guido van Rossumf1dc0611998-02-19 20:51:52 +0000114
115 /* First, clear only names starting with a single underscore */
116 pos = 0;
117 while (PyDict_Next(d, &pos, &key, &value)) {
118 if (value != Py_None && PyString_Check(key)) {
119 char *s = PyString_AsString(key);
120 if (s[0] == '_' && s[1] != '_') {
121 if (Py_VerboseFlag > 1)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000122 PySys_WriteStderr("# clear[1] %s\n", s);
Guido van Rossumf1dc0611998-02-19 20:51:52 +0000123 PyDict_SetItem(d, key, Py_None);
124 }
125 }
126 }
127
128 /* Next, clear all names except for __builtins__ */
129 pos = 0;
130 while (PyDict_Next(d, &pos, &key, &value)) {
131 if (value != Py_None && PyString_Check(key)) {
132 char *s = PyString_AsString(key);
133 if (s[0] != '_' || strcmp(s, "__builtins__") != 0) {
134 if (Py_VerboseFlag > 1)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000135 PySys_WriteStderr("# clear[2] %s\n", s);
Guido van Rossumf1dc0611998-02-19 20:51:52 +0000136 PyDict_SetItem(d, key, Py_None);
137 }
138 }
139 }
140
141 /* Note: we leave __builtins__ in place, so that destructors
142 of non-global objects defined in this module can still use
143 builtins, in particularly 'None'. */
144
145}
146
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000147/* Methods */
148
Tim Peters6d6c1a32001-08-02 04:15:00 +0000149static int
Guido van Rossumc3a787e2002-06-04 05:52:47 +0000150module_init(PyModuleObject *m, PyObject *args, PyObject *kwds)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000151{
Guido van Rossumc3a787e2002-06-04 05:52:47 +0000152 static char *kwlist[] = {"name", "doc", NULL};
153 PyObject *dict, *name = Py_None, *doc = Py_None;
154 if (!PyArg_ParseTupleAndKeywords(args, kwds, "S|O", kwlist,
155 &name, &doc))
156 return -1;
157 dict = m->md_dict;
158 if (dict == NULL) {
159 dict = PyDict_New();
160 if (dict == NULL)
161 return -1;
162 m->md_dict = dict;
163 }
164 if (PyDict_SetItemString(dict, "__name__", name) < 0)
165 return -1;
166 if (PyDict_SetItemString(dict, "__doc__", doc) < 0)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000167 return -1;
168 return 0;
169}
170
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000171static void
Fred Drakeee238b92000-07-09 06:03:25 +0000172module_dealloc(PyModuleObject *m)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000173{
Neil Schemenauere83c00e2001-08-29 23:54:21 +0000174 PyObject_GC_UnTrack(m);
Guido van Rossum27e916f1995-01-26 00:39:00 +0000175 if (m->md_dict != NULL) {
Guido van Rossumf1dc0611998-02-19 20:51:52 +0000176 _PyModule_Clear((PyObject *)m);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000177 Py_DECREF(m->md_dict);
Guido van Rossum27e916f1995-01-26 00:39:00 +0000178 }
Guido van Rossum9475a232001-10-05 20:51:39 +0000179 m->ob_type->tp_free((PyObject *)m);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000180}
181
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000182static PyObject *
Fred Drakeee238b92000-07-09 06:03:25 +0000183module_repr(PyModuleObject *m)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000184{
Guido van Rossum98cc19f1999-02-15 14:47:16 +0000185 char *name;
186 char *filename;
Barry Warsaw2907fe62001-08-16 20:39:24 +0000187
Guido van Rossum98cc19f1999-02-15 14:47:16 +0000188 name = PyModule_GetName((PyObject *)m);
Guido van Rossumc45611d1993-11-17 22:58:56 +0000189 if (name == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000190 PyErr_Clear();
Guido van Rossumc45611d1993-11-17 22:58:56 +0000191 name = "?";
192 }
Guido van Rossum98cc19f1999-02-15 14:47:16 +0000193 filename = PyModule_GetFilename((PyObject *)m);
194 if (filename == NULL) {
195 PyErr_Clear();
Barry Warsaw7ce36942001-08-24 18:34:26 +0000196 return PyString_FromFormat("<module '%s' (built-in)>", name);
Guido van Rossum98cc19f1999-02-15 14:47:16 +0000197 }
Barry Warsaw7ce36942001-08-24 18:34:26 +0000198 return PyString_FromFormat("<module '%s' from '%s'>", name, filename);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000199}
200
Neil Schemenauer10e31cf2001-01-02 15:58:27 +0000201/* We only need a traverse function, no clear function: If the module
202 is in a cycle, md_dict will be cleared as well, which will break
203 the cycle. */
204static int
205module_traverse(PyModuleObject *m, visitproc visit, void *arg)
206{
207 if (m->md_dict != NULL)
208 return visit(m->md_dict, arg);
209 return 0;
210}
211
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000212PyDoc_STRVAR(module_doc,
Guido van Rossumed3e09f2002-06-04 06:02:35 +0000213"module(name[, doc])\n\
214\n\
215Create a module object.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000216The name must be a string; the optional doc argument can have any type.");
Guido van Rossumed3e09f2002-06-04 06:02:35 +0000217
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000218PyTypeObject PyModule_Type = {
219 PyObject_HEAD_INIT(&PyType_Type)
Tim Petersd85e1022001-05-11 21:51:48 +0000220 0, /* ob_size */
221 "module", /* tp_name */
Neil Schemenauere83c00e2001-08-29 23:54:21 +0000222 sizeof(PyModuleObject), /* tp_size */
Tim Petersd85e1022001-05-11 21:51:48 +0000223 0, /* tp_itemsize */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000224 (destructor)module_dealloc, /* tp_dealloc */
Tim Petersd85e1022001-05-11 21:51:48 +0000225 0, /* tp_print */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000226 0, /* tp_getattr */
227 0, /* tp_setattr */
Tim Petersd85e1022001-05-11 21:51:48 +0000228 0, /* tp_compare */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000229 (reprfunc)module_repr, /* tp_repr */
Tim Petersd85e1022001-05-11 21:51:48 +0000230 0, /* tp_as_number */
231 0, /* tp_as_sequence */
232 0, /* tp_as_mapping */
233 0, /* tp_hash */
234 0, /* tp_call */
235 0, /* tp_str */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000236 PyObject_GenericGetAttr, /* tp_getattro */
237 PyObject_GenericSetAttr, /* tp_setattro */
Tim Petersd85e1022001-05-11 21:51:48 +0000238 0, /* tp_as_buffer */
Neil Schemenauere83c00e2001-08-29 23:54:21 +0000239 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
Tim Peters6d6c1a32001-08-02 04:15:00 +0000240 Py_TPFLAGS_BASETYPE, /* tp_flags */
Guido van Rossumed3e09f2002-06-04 06:02:35 +0000241 module_doc, /* tp_doc */
Tim Petersd85e1022001-05-11 21:51:48 +0000242 (traverseproc)module_traverse, /* tp_traverse */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000243 0, /* tp_clear */
244 0, /* tp_richcompare */
245 0, /* tp_weaklistoffset */
246 0, /* tp_iter */
247 0, /* tp_iternext */
248 0, /* tp_methods */
249 module_members, /* tp_members */
250 0, /* tp_getset */
251 0, /* tp_base */
252 0, /* tp_dict */
253 0, /* tp_descr_get */
254 0, /* tp_descr_set */
255 offsetof(PyModuleObject, md_dict), /* tp_dictoffset */
256 (initproc)module_init, /* tp_init */
257 PyType_GenericAlloc, /* tp_alloc */
258 PyType_GenericNew, /* tp_new */
Neil Schemenauer99b5d282002-04-12 02:44:22 +0000259 PyObject_GC_Del, /* tp_free */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000260};