blob: bf19f3c60c4208ec65e0cb5510765023dc026ba8 [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 *
Jeremy Hyltonaf68c872005-12-10 18:50:16 +000018PyModule_New(const 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;
Martin v. Löwis5b222132007-06-10 09:51:05 +000025 nameobj = PyUnicode_FromString(name);
Guido van Rossumc0b618a1997-05-02 03:12:38 +000026 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;
Christian Heimescbf3b5c2007-12-03 21:02:03 +000033 if (PyDict_SetItemString(m->md_dict, "__package__", Py_None) != 0)
34 goto fail;
Guido van Rossumc0b618a1997-05-02 03:12:38 +000035 Py_DECREF(nameobj);
Neil Schemenauere83c00e2001-08-29 23:54:21 +000036 PyObject_GC_Track(m);
Guido van Rossumc0b618a1997-05-02 03:12:38 +000037 return (PyObject *)m;
Guido van Rossumc45611d1993-11-17 22:58:56 +000038
39 fail:
Guido van Rossumc0b618a1997-05-02 03:12:38 +000040 Py_XDECREF(nameobj);
41 Py_DECREF(m);
Guido van Rossumc45611d1993-11-17 22:58:56 +000042 return NULL;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000043}
44
Guido van Rossumc0b618a1997-05-02 03:12:38 +000045PyObject *
Fred Drakeee238b92000-07-09 06:03:25 +000046PyModule_GetDict(PyObject *m)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000047{
Guido van Rossumcd637aa2002-03-12 20:37:02 +000048 PyObject *d;
Guido van Rossumc0b618a1997-05-02 03:12:38 +000049 if (!PyModule_Check(m)) {
50 PyErr_BadInternalCall();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000051 return NULL;
52 }
Guido van Rossumcd637aa2002-03-12 20:37:02 +000053 d = ((PyModuleObject *)m) -> md_dict;
54 if (d == NULL)
55 ((PyModuleObject *)m) -> md_dict = d = PyDict_New();
56 return d;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000057}
58
Neal Norwitz312e10d2007-08-26 02:21:42 +000059const char *
Fred Drakeee238b92000-07-09 06:03:25 +000060PyModule_GetName(PyObject *m)
Guido van Rossum0558a201990-10-26 15:00:11 +000061{
Guido van Rossumcd637aa2002-03-12 20:37:02 +000062 PyObject *d;
Guido van Rossumc0b618a1997-05-02 03:12:38 +000063 PyObject *nameobj;
64 if (!PyModule_Check(m)) {
65 PyErr_BadArgument();
Guido van Rossum0558a201990-10-26 15:00:11 +000066 return NULL;
67 }
Guido van Rossumcd637aa2002-03-12 20:37:02 +000068 d = ((PyModuleObject *)m)->md_dict;
69 if (d == NULL ||
70 (nameobj = PyDict_GetItemString(d, "__name__")) == NULL ||
Neal Norwitzed2b7392007-08-26 04:51:10 +000071 !PyUnicode_Check(nameobj))
Guido van Rossumcd637aa2002-03-12 20:37:02 +000072 {
Guido van Rossumc0b618a1997-05-02 03:12:38 +000073 PyErr_SetString(PyExc_SystemError, "nameless module");
Guido van Rossumc45611d1993-11-17 22:58:56 +000074 return NULL;
75 }
Neal Norwitzed2b7392007-08-26 04:51:10 +000076 return PyUnicode_AsString(nameobj);
Guido van Rossum0558a201990-10-26 15:00:11 +000077}
78
Neal Norwitz312e10d2007-08-26 02:21:42 +000079const char *
Fred Drakeee238b92000-07-09 06:03:25 +000080PyModule_GetFilename(PyObject *m)
Guido van Rossum98cc19f1999-02-15 14:47:16 +000081{
Guido van Rossumcd637aa2002-03-12 20:37:02 +000082 PyObject *d;
Guido van Rossum98cc19f1999-02-15 14:47:16 +000083 PyObject *fileobj;
84 if (!PyModule_Check(m)) {
85 PyErr_BadArgument();
86 return NULL;
87 }
Guido van Rossumcd637aa2002-03-12 20:37:02 +000088 d = ((PyModuleObject *)m)->md_dict;
89 if (d == NULL ||
90 (fileobj = PyDict_GetItemString(d, "__file__")) == NULL ||
Guido van Rossum00bc0e02007-10-15 02:52:41 +000091 !PyUnicode_Check(fileobj))
Guido van Rossumcd637aa2002-03-12 20:37:02 +000092 {
Guido van Rossum98cc19f1999-02-15 14:47:16 +000093 PyErr_SetString(PyExc_SystemError, "module filename missing");
94 return NULL;
95 }
Guido van Rossum00bc0e02007-10-15 02:52:41 +000096 return PyUnicode_AsString(fileobj);
Guido van Rossum98cc19f1999-02-15 14:47:16 +000097}
98
Guido van Rossumf1dc0611998-02-19 20:51:52 +000099void
Fred Drakeee238b92000-07-09 06:03:25 +0000100_PyModule_Clear(PyObject *m)
Guido van Rossumf1dc0611998-02-19 20:51:52 +0000101{
102 /* To make the execution order of destructors for global
103 objects a bit more predictable, we first zap all objects
104 whose name starts with a single underscore, before we clear
105 the entire dictionary. We zap them by replacing them with
106 None, rather than deleting them from the dictionary, to
107 avoid rehashing the dictionary (to some extent). */
108
Martin v. Löwis18e16552006-02-15 17:27:45 +0000109 Py_ssize_t pos;
Guido van Rossumf1dc0611998-02-19 20:51:52 +0000110 PyObject *key, *value;
111 PyObject *d;
112
113 d = ((PyModuleObject *)m)->md_dict;
Guido van Rossumcd637aa2002-03-12 20:37:02 +0000114 if (d == NULL)
115 return;
Guido van Rossumf1dc0611998-02-19 20:51:52 +0000116
117 /* First, clear only names starting with a single underscore */
118 pos = 0;
119 while (PyDict_Next(d, &pos, &key, &value)) {
Neal Norwitz312e10d2007-08-26 02:21:42 +0000120 if (value != Py_None && PyUnicode_Check(key)) {
121 const char *s = PyUnicode_AsString(key);
Guido van Rossumf1dc0611998-02-19 20:51:52 +0000122 if (s[0] == '_' && s[1] != '_') {
123 if (Py_VerboseFlag > 1)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000124 PySys_WriteStderr("# clear[1] %s\n", s);
Guido van Rossumf1dc0611998-02-19 20:51:52 +0000125 PyDict_SetItem(d, key, Py_None);
126 }
127 }
128 }
129
130 /* Next, clear all names except for __builtins__ */
131 pos = 0;
132 while (PyDict_Next(d, &pos, &key, &value)) {
Neal Norwitz312e10d2007-08-26 02:21:42 +0000133 if (value != Py_None && PyUnicode_Check(key)) {
134 const char *s = PyUnicode_AsString(key);
Guido van Rossumf1dc0611998-02-19 20:51:52 +0000135 if (s[0] != '_' || strcmp(s, "__builtins__") != 0) {
136 if (Py_VerboseFlag > 1)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000137 PySys_WriteStderr("# clear[2] %s\n", s);
Guido van Rossumf1dc0611998-02-19 20:51:52 +0000138 PyDict_SetItem(d, key, Py_None);
139 }
140 }
141 }
142
143 /* Note: we leave __builtins__ in place, so that destructors
144 of non-global objects defined in this module can still use
145 builtins, in particularly 'None'. */
146
147}
148
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000149/* Methods */
150
Tim Peters6d6c1a32001-08-02 04:15:00 +0000151static int
Guido van Rossumc3a787e2002-06-04 05:52:47 +0000152module_init(PyModuleObject *m, PyObject *args, PyObject *kwds)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000153{
Martin v. Löwis15e62742006-02-27 16:46:16 +0000154 static char *kwlist[] = {"name", "doc", NULL};
Guido van Rossumc3a787e2002-06-04 05:52:47 +0000155 PyObject *dict, *name = Py_None, *doc = Py_None;
Guido van Rossum98297ee2007-11-06 21:34:58 +0000156 if (!PyArg_ParseTupleAndKeywords(args, kwds, "U|O:module.__init__",
Jeremy Hyltonaf68c872005-12-10 18:50:16 +0000157 kwlist, &name, &doc))
Guido van Rossumc3a787e2002-06-04 05:52:47 +0000158 return -1;
159 dict = m->md_dict;
160 if (dict == NULL) {
161 dict = PyDict_New();
162 if (dict == NULL)
163 return -1;
164 m->md_dict = dict;
165 }
166 if (PyDict_SetItemString(dict, "__name__", name) < 0)
167 return -1;
168 if (PyDict_SetItemString(dict, "__doc__", doc) < 0)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000169 return -1;
170 return 0;
171}
172
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000173static void
Fred Drakeee238b92000-07-09 06:03:25 +0000174module_dealloc(PyModuleObject *m)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000175{
Neil Schemenauere83c00e2001-08-29 23:54:21 +0000176 PyObject_GC_UnTrack(m);
Guido van Rossum27e916f1995-01-26 00:39:00 +0000177 if (m->md_dict != NULL) {
Guido van Rossumf1dc0611998-02-19 20:51:52 +0000178 _PyModule_Clear((PyObject *)m);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000179 Py_DECREF(m->md_dict);
Guido van Rossum27e916f1995-01-26 00:39:00 +0000180 }
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000181 Py_Type(m)->tp_free((PyObject *)m);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000182}
183
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000184static PyObject *
Fred Drakeee238b92000-07-09 06:03:25 +0000185module_repr(PyModuleObject *m)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000186{
Neal Norwitz312e10d2007-08-26 02:21:42 +0000187 const char *name;
188 const char *filename;
Barry Warsaw2907fe62001-08-16 20:39:24 +0000189
Guido van Rossum98cc19f1999-02-15 14:47:16 +0000190 name = PyModule_GetName((PyObject *)m);
Guido van Rossumc45611d1993-11-17 22:58:56 +0000191 if (name == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000192 PyErr_Clear();
Guido van Rossumc45611d1993-11-17 22:58:56 +0000193 name = "?";
194 }
Guido van Rossum98cc19f1999-02-15 14:47:16 +0000195 filename = PyModule_GetFilename((PyObject *)m);
196 if (filename == NULL) {
197 PyErr_Clear();
Walter Dörwald1ab83302007-05-18 17:15:44 +0000198 return PyUnicode_FromFormat("<module '%s' (built-in)>", name);
Guido van Rossum98cc19f1999-02-15 14:47:16 +0000199 }
Walter Dörwald1ab83302007-05-18 17:15:44 +0000200 return PyUnicode_FromFormat("<module '%s' from '%s'>", name, filename);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000201}
202
Neil Schemenauer10e31cf2001-01-02 15:58:27 +0000203/* We only need a traverse function, no clear function: If the module
204 is in a cycle, md_dict will be cleared as well, which will break
205 the cycle. */
206static int
207module_traverse(PyModuleObject *m, visitproc visit, void *arg)
208{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000209 Py_VISIT(m->md_dict);
Neil Schemenauer10e31cf2001-01-02 15:58:27 +0000210 return 0;
211}
212
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000213PyDoc_STRVAR(module_doc,
Guido van Rossumed3e09f2002-06-04 06:02:35 +0000214"module(name[, doc])\n\
215\n\
216Create a module object.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000217The name must be a string; the optional doc argument can have any type.");
Guido van Rossumed3e09f2002-06-04 06:02:35 +0000218
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000219PyTypeObject PyModule_Type = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000220 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Tim Petersd85e1022001-05-11 21:51:48 +0000221 "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 */
Neil Schemenauer75552942003-07-16 22:04:11 +0000237 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};