blob: def37810b8f421d22e953758c18af8689b981eef [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
Martin v. Löwis1a214512008-06-11 05:26:20 +00007static Py_ssize_t max_module_number;
8
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00009typedef struct {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000010 PyObject_HEAD
11 PyObject *md_dict;
12 struct PyModuleDef *md_def;
13 void *md_state;
Guido van Rossumc0b618a1997-05-02 03:12:38 +000014} PyModuleObject;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000015
Neil Schemenauerf23473f2001-10-21 22:28:58 +000016static PyMemberDef module_members[] = {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000017 {"__dict__", T_OBJECT, offsetof(PyModuleObject, md_dict), READONLY},
18 {0}
Tim Peters6d6c1a32001-08-02 04:15:00 +000019};
20
Martin v. Löwis1a214512008-06-11 05:26:20 +000021static PyTypeObject moduledef_type = {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000022 PyVarObject_HEAD_INIT(&PyType_Type, 0)
23 "moduledef", /* tp_name */
24 sizeof(struct PyModuleDef), /* tp_size */
25 0, /* tp_itemsize */
Martin v. Löwis1a214512008-06-11 05:26:20 +000026};
27
28
Guido van Rossumc0b618a1997-05-02 03:12:38 +000029PyObject *
Jeremy Hyltonaf68c872005-12-10 18:50:16 +000030PyModule_New(const char *name)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000031{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000032 PyModuleObject *m;
33 PyObject *nameobj;
34 m = PyObject_GC_New(PyModuleObject, &PyModule_Type);
35 if (m == NULL)
36 return NULL;
37 m->md_def = NULL;
38 m->md_state = NULL;
39 nameobj = PyUnicode_FromString(name);
40 m->md_dict = PyDict_New();
41 if (m->md_dict == NULL || nameobj == NULL)
42 goto fail;
43 if (PyDict_SetItemString(m->md_dict, "__name__", nameobj) != 0)
44 goto fail;
45 if (PyDict_SetItemString(m->md_dict, "__doc__", Py_None) != 0)
46 goto fail;
47 if (PyDict_SetItemString(m->md_dict, "__package__", Py_None) != 0)
48 goto fail;
49 Py_DECREF(nameobj);
50 PyObject_GC_Track(m);
51 return (PyObject *)m;
Guido van Rossumc45611d1993-11-17 22:58:56 +000052
53 fail:
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000054 Py_XDECREF(nameobj);
55 Py_DECREF(m);
56 return NULL;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000057}
58
Martin v. Löwis1a214512008-06-11 05:26:20 +000059static char api_version_warning[] =
60"Python C API version mismatch for module %.100s:\
61 This Python has API version %d, module %.100s has version %d.";
62
63PyObject *
64PyModule_Create2(struct PyModuleDef* module, int module_api_version)
65{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000066 PyObject *d, *v, *n;
67 PyMethodDef *ml;
68 const char* name;
69 PyModuleObject *m;
70 if (!Py_IsInitialized())
71 Py_FatalError("Interpreter not initialized (version mismatch?)");
72 if (PyType_Ready(&moduledef_type) < 0)
73 return NULL;
74 if (module->m_base.m_index == 0) {
75 max_module_number++;
76 Py_REFCNT(module) = 1;
77 Py_TYPE(module) = &moduledef_type;
78 module->m_base.m_index = max_module_number;
79 }
80 name = module->m_name;
81 if (module_api_version != PYTHON_API_VERSION) {
82 char message[512];
83 PyOS_snprintf(message, sizeof(message),
84 api_version_warning, name,
85 PYTHON_API_VERSION, name,
86 module_api_version);
87 if (PyErr_WarnEx(PyExc_RuntimeWarning, message, 1))
88 return NULL;
89 }
90 /* Make sure name is fully qualified.
Martin v. Löwis1a214512008-06-11 05:26:20 +000091
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000092 This is a bit of a hack: when the shared library is loaded,
93 the module name is "package.module", but the module calls
94 PyModule_Create*() with just "module" for the name. The shared
95 library loader squirrels away the true name of the module in
96 _Py_PackageContext, and PyModule_Create*() will substitute this
97 (if the name actually matches).
98 */
99 if (_Py_PackageContext != NULL) {
100 char *p = strrchr(_Py_PackageContext, '.');
101 if (p != NULL && strcmp(module->m_name, p+1) == 0) {
102 name = _Py_PackageContext;
103 _Py_PackageContext = NULL;
104 }
105 }
106 if ((m = (PyModuleObject*)PyModule_New(name)) == NULL)
107 return NULL;
Martin v. Löwis1a214512008-06-11 05:26:20 +0000108
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000109 if (module->m_size > 0) {
110 m->md_state = PyMem_MALLOC(module->m_size);
111 if (!m->md_state) {
112 PyErr_NoMemory();
113 Py_DECREF(m);
114 return NULL;
115 }
116 memset(m->md_state, 0, module->m_size);
117 }
118
119 d = PyModule_GetDict((PyObject*)m);
120 if (module->m_methods != NULL) {
121 n = PyUnicode_FromString(name);
122 if (n == NULL)
123 return NULL;
124 for (ml = module->m_methods; ml->ml_name != NULL; ml++) {
125 if ((ml->ml_flags & METH_CLASS) ||
126 (ml->ml_flags & METH_STATIC)) {
127 PyErr_SetString(PyExc_ValueError,
128 "module functions cannot set"
129 " METH_CLASS or METH_STATIC");
130 Py_DECREF(n);
131 return NULL;
132 }
133 v = PyCFunction_NewEx(ml, (PyObject*)m, n);
134 if (v == NULL) {
135 Py_DECREF(n);
136 return NULL;
137 }
138 if (PyDict_SetItemString(d, ml->ml_name, v) != 0) {
139 Py_DECREF(v);
140 Py_DECREF(n);
141 return NULL;
142 }
143 Py_DECREF(v);
144 }
145 Py_DECREF(n);
146 }
147 if (module->m_doc != NULL) {
148 v = PyUnicode_FromString(module->m_doc);
149 if (v == NULL || PyDict_SetItemString(d, "__doc__", v) != 0) {
150 Py_XDECREF(v);
151 return NULL;
152 }
153 Py_DECREF(v);
154 }
155 m->md_def = module;
156 return (PyObject*)m;
Martin v. Löwis1a214512008-06-11 05:26:20 +0000157}
158
159
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000160PyObject *
Fred Drakeee238b92000-07-09 06:03:25 +0000161PyModule_GetDict(PyObject *m)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000162{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000163 PyObject *d;
164 if (!PyModule_Check(m)) {
165 PyErr_BadInternalCall();
166 return NULL;
167 }
168 d = ((PyModuleObject *)m) -> md_dict;
169 if (d == NULL)
170 ((PyModuleObject *)m) -> md_dict = d = PyDict_New();
171 return d;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000172}
173
Neal Norwitz312e10d2007-08-26 02:21:42 +0000174const char *
Fred Drakeee238b92000-07-09 06:03:25 +0000175PyModule_GetName(PyObject *m)
Guido van Rossum0558a201990-10-26 15:00:11 +0000176{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000177 PyObject *d;
178 PyObject *nameobj;
179 if (!PyModule_Check(m)) {
180 PyErr_BadArgument();
181 return NULL;
182 }
183 d = ((PyModuleObject *)m)->md_dict;
184 if (d == NULL ||
185 (nameobj = PyDict_GetItemString(d, "__name__")) == NULL ||
186 !PyUnicode_Check(nameobj))
187 {
188 PyErr_SetString(PyExc_SystemError, "nameless module");
189 return NULL;
190 }
191 return _PyUnicode_AsString(nameobj);
Guido van Rossum0558a201990-10-26 15:00:11 +0000192}
193
Neal Norwitz312e10d2007-08-26 02:21:42 +0000194const char *
Fred Drakeee238b92000-07-09 06:03:25 +0000195PyModule_GetFilename(PyObject *m)
Guido van Rossum98cc19f1999-02-15 14:47:16 +0000196{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000197 PyObject *d;
198 PyObject *fileobj;
199 if (!PyModule_Check(m)) {
200 PyErr_BadArgument();
201 return NULL;
202 }
203 d = ((PyModuleObject *)m)->md_dict;
204 if (d == NULL ||
205 (fileobj = PyDict_GetItemString(d, "__file__")) == NULL ||
206 !PyUnicode_Check(fileobj))
207 {
208 PyErr_SetString(PyExc_SystemError, "module filename missing");
209 return NULL;
210 }
211 return _PyUnicode_AsString(fileobj);
Guido van Rossum98cc19f1999-02-15 14:47:16 +0000212}
213
Martin v. Löwis1a214512008-06-11 05:26:20 +0000214PyModuleDef*
215PyModule_GetDef(PyObject* m)
216{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000217 if (!PyModule_Check(m)) {
218 PyErr_BadArgument();
219 return NULL;
220 }
221 return ((PyModuleObject *)m)->md_def;
Martin v. Löwis1a214512008-06-11 05:26:20 +0000222}
223
224void*
225PyModule_GetState(PyObject* m)
226{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000227 if (!PyModule_Check(m)) {
228 PyErr_BadArgument();
229 return NULL;
230 }
231 return ((PyModuleObject *)m)->md_state;
Martin v. Löwis1a214512008-06-11 05:26:20 +0000232}
233
Guido van Rossumf1dc0611998-02-19 20:51:52 +0000234void
Fred Drakeee238b92000-07-09 06:03:25 +0000235_PyModule_Clear(PyObject *m)
Guido van Rossumf1dc0611998-02-19 20:51:52 +0000236{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000237 /* To make the execution order of destructors for global
238 objects a bit more predictable, we first zap all objects
239 whose name starts with a single underscore, before we clear
240 the entire dictionary. We zap them by replacing them with
241 None, rather than deleting them from the dictionary, to
242 avoid rehashing the dictionary (to some extent). */
Guido van Rossumf1dc0611998-02-19 20:51:52 +0000243
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000244 Py_ssize_t pos;
245 PyObject *key, *value;
246 PyObject *d;
Guido van Rossumf1dc0611998-02-19 20:51:52 +0000247
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000248 d = ((PyModuleObject *)m)->md_dict;
249 if (d == NULL)
250 return;
Guido van Rossumf1dc0611998-02-19 20:51:52 +0000251
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000252 /* First, clear only names starting with a single underscore */
253 pos = 0;
254 while (PyDict_Next(d, &pos, &key, &value)) {
255 if (value != Py_None && PyUnicode_Check(key)) {
256 const char *s = _PyUnicode_AsString(key);
257 if (s[0] == '_' && s[1] != '_') {
258 if (Py_VerboseFlag > 1)
259 PySys_WriteStderr("# clear[1] %s\n", s);
260 PyDict_SetItem(d, key, Py_None);
261 }
262 }
263 }
Guido van Rossumf1dc0611998-02-19 20:51:52 +0000264
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000265 /* Next, clear all names except for __builtins__ */
266 pos = 0;
267 while (PyDict_Next(d, &pos, &key, &value)) {
268 if (value != Py_None && PyUnicode_Check(key)) {
269 const char *s = _PyUnicode_AsString(key);
270 if (s[0] != '_' || strcmp(s, "__builtins__") != 0) {
271 if (Py_VerboseFlag > 1)
272 PySys_WriteStderr("# clear[2] %s\n", s);
273 PyDict_SetItem(d, key, Py_None);
274 }
275 }
276 }
Guido van Rossumf1dc0611998-02-19 20:51:52 +0000277
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000278 /* Note: we leave __builtins__ in place, so that destructors
279 of non-global objects defined in this module can still use
280 builtins, in particularly 'None'. */
Guido van Rossumf1dc0611998-02-19 20:51:52 +0000281
282}
283
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000284/* Methods */
285
Tim Peters6d6c1a32001-08-02 04:15:00 +0000286static int
Guido van Rossumc3a787e2002-06-04 05:52:47 +0000287module_init(PyModuleObject *m, PyObject *args, PyObject *kwds)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000288{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000289 static char *kwlist[] = {"name", "doc", NULL};
290 PyObject *dict, *name = Py_None, *doc = Py_None;
291 if (!PyArg_ParseTupleAndKeywords(args, kwds, "U|O:module.__init__",
292 kwlist, &name, &doc))
293 return -1;
294 dict = m->md_dict;
295 if (dict == NULL) {
296 dict = PyDict_New();
297 if (dict == NULL)
298 return -1;
299 m->md_dict = dict;
300 }
301 if (PyDict_SetItemString(dict, "__name__", name) < 0)
302 return -1;
303 if (PyDict_SetItemString(dict, "__doc__", doc) < 0)
304 return -1;
305 return 0;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000306}
307
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000308static void
Fred Drakeee238b92000-07-09 06:03:25 +0000309module_dealloc(PyModuleObject *m)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000310{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000311 PyObject_GC_UnTrack(m);
312 if (m->md_def && m->md_def->m_free)
313 m->md_def->m_free(m);
314 if (m->md_dict != NULL) {
315 /* If we are the only ones holding a reference, we can clear
316 the dictionary. */
317 if (Py_REFCNT(m->md_dict) == 1)
318 _PyModule_Clear((PyObject *)m);
319 Py_DECREF(m->md_dict);
320 }
321 if (m->md_state != NULL)
322 PyMem_FREE(m->md_state);
323 Py_TYPE(m)->tp_free((PyObject *)m);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000324}
325
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000326static PyObject *
Fred Drakeee238b92000-07-09 06:03:25 +0000327module_repr(PyModuleObject *m)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000328{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000329 const char *name;
330 const char *filename;
Barry Warsaw2907fe62001-08-16 20:39:24 +0000331
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000332 name = PyModule_GetName((PyObject *)m);
333 if (name == NULL) {
334 PyErr_Clear();
335 name = "?";
336 }
337 filename = PyModule_GetFilename((PyObject *)m);
338 if (filename == NULL) {
339 PyErr_Clear();
340 return PyUnicode_FromFormat("<module '%s' (built-in)>", name);
341 }
342 return PyUnicode_FromFormat("<module '%s' from '%s'>", name, filename);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000343}
344
Neil Schemenauer10e31cf2001-01-02 15:58:27 +0000345static int
346module_traverse(PyModuleObject *m, visitproc visit, void *arg)
347{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000348 if (m->md_def && m->md_def->m_traverse) {
349 int res = m->md_def->m_traverse((PyObject*)m, visit, arg);
350 if (res)
351 return res;
352 }
353 Py_VISIT(m->md_dict);
354 return 0;
Neil Schemenauer10e31cf2001-01-02 15:58:27 +0000355}
356
Martin v. Löwis1a214512008-06-11 05:26:20 +0000357static int
358module_clear(PyModuleObject *m)
359{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000360 if (m->md_def && m->md_def->m_clear) {
361 int res = m->md_def->m_clear((PyObject*)m);
362 if (res)
363 return res;
364 }
365 Py_CLEAR(m->md_dict);
366 return 0;
Martin v. Löwis1a214512008-06-11 05:26:20 +0000367}
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000368
Martin v. Löwis1a214512008-06-11 05:26:20 +0000369
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000370PyDoc_STRVAR(module_doc,
Guido van Rossumed3e09f2002-06-04 06:02:35 +0000371"module(name[, doc])\n\
372\n\
373Create a module object.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000374The name must be a string; the optional doc argument can have any type.");
Guido van Rossumed3e09f2002-06-04 06:02:35 +0000375
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000376PyTypeObject PyModule_Type = {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000377 PyVarObject_HEAD_INIT(&PyType_Type, 0)
378 "module", /* tp_name */
379 sizeof(PyModuleObject), /* tp_size */
380 0, /* tp_itemsize */
381 (destructor)module_dealloc, /* tp_dealloc */
382 0, /* tp_print */
383 0, /* tp_getattr */
384 0, /* tp_setattr */
385 0, /* tp_reserved */
386 (reprfunc)module_repr, /* tp_repr */
387 0, /* tp_as_number */
388 0, /* tp_as_sequence */
389 0, /* tp_as_mapping */
390 0, /* tp_hash */
391 0, /* tp_call */
392 0, /* tp_str */
393 PyObject_GenericGetAttr, /* tp_getattro */
394 PyObject_GenericSetAttr, /* tp_setattro */
395 0, /* tp_as_buffer */
396 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
397 Py_TPFLAGS_BASETYPE, /* tp_flags */
398 module_doc, /* tp_doc */
399 (traverseproc)module_traverse, /* tp_traverse */
400 (inquiry)module_clear, /* tp_clear */
401 0, /* tp_richcompare */
402 0, /* tp_weaklistoffset */
403 0, /* tp_iter */
404 0, /* tp_iternext */
405 0, /* tp_methods */
406 module_members, /* tp_members */
407 0, /* tp_getset */
408 0, /* tp_base */
409 0, /* tp_dict */
410 0, /* tp_descr_get */
411 0, /* tp_descr_set */
412 offsetof(PyModuleObject, md_dict), /* tp_dictoffset */
413 (initproc)module_init, /* tp_init */
414 PyType_GenericAlloc, /* tp_alloc */
415 PyType_GenericNew, /* tp_new */
416 PyObject_GC_Del, /* tp_free */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000417};