blob: 3ea3be8b828624f2c4cd5051c8a9ad3039c3622d [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 Pitrouf95a1b32010-05-09 15:52:27 +000010 PyObject_HEAD
11 PyObject *md_dict;
12 struct PyModuleDef *md_def;
13 void *md_state;
Antoine Pitroudcedaf62013-07-31 23:14:08 +020014 PyObject *md_weaklist;
15 PyObject *md_name; /* for logging purposes after md_dict is cleared */
Guido van Rossumc0b618a1997-05-02 03:12:38 +000016} PyModuleObject;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000017
Neil Schemenauerf23473f2001-10-21 22:28:58 +000018static PyMemberDef module_members[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000019 {"__dict__", T_OBJECT, offsetof(PyModuleObject, md_dict), READONLY},
20 {0}
Tim Peters6d6c1a32001-08-02 04:15:00 +000021};
22
Martin v. Löwis1a214512008-06-11 05:26:20 +000023static PyTypeObject moduledef_type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000024 PyVarObject_HEAD_INIT(&PyType_Type, 0)
25 "moduledef", /* tp_name */
26 sizeof(struct PyModuleDef), /* tp_size */
27 0, /* tp_itemsize */
Martin v. Löwis1a214512008-06-11 05:26:20 +000028};
29
30
Brett Cannon4c14b5d2013-05-04 13:56:58 -040031static int
Antoine Pitroudcedaf62013-07-31 23:14:08 +020032module_init_dict(PyModuleObject *mod, PyObject *md_dict,
33 PyObject *name, PyObject *doc)
Brett Cannon4c14b5d2013-05-04 13:56:58 -040034{
35 if (md_dict == NULL)
36 return -1;
37 if (doc == NULL)
38 doc = Py_None;
39
40 if (PyDict_SetItemString(md_dict, "__name__", name) != 0)
41 return -1;
42 if (PyDict_SetItemString(md_dict, "__doc__", doc) != 0)
43 return -1;
44 if (PyDict_SetItemString(md_dict, "__package__", Py_None) != 0)
45 return -1;
46 if (PyDict_SetItemString(md_dict, "__loader__", Py_None) != 0)
47 return -1;
Antoine Pitroudcedaf62013-07-31 23:14:08 +020048 if (PyUnicode_CheckExact(name)) {
49 Py_INCREF(name);
50 Py_XDECREF(mod->md_name);
51 mod->md_name = name;
52 }
Brett Cannon4c14b5d2013-05-04 13:56:58 -040053
54 return 0;
55}
56
57
Guido van Rossumc0b618a1997-05-02 03:12:38 +000058PyObject *
Victor Stinner0639b562011-03-04 12:57:07 +000059PyModule_NewObject(PyObject *name)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000060{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000061 PyModuleObject *m;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000062 m = PyObject_GC_New(PyModuleObject, &PyModule_Type);
63 if (m == NULL)
64 return NULL;
65 m->md_def = NULL;
66 m->md_state = NULL;
Antoine Pitroudcedaf62013-07-31 23:14:08 +020067 m->md_weaklist = NULL;
68 m->md_name = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000069 m->md_dict = PyDict_New();
Antoine Pitroudcedaf62013-07-31 23:14:08 +020070 if (module_init_dict(m, m->md_dict, name, NULL) != 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000071 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000072 PyObject_GC_Track(m);
73 return (PyObject *)m;
Guido van Rossumc45611d1993-11-17 22:58:56 +000074
75 fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000076 Py_DECREF(m);
77 return NULL;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000078}
79
Martin v. Löwis1a214512008-06-11 05:26:20 +000080PyObject *
Victor Stinner0639b562011-03-04 12:57:07 +000081PyModule_New(const char *name)
82{
83 PyObject *nameobj, *module;
84 nameobj = PyUnicode_FromString(name);
85 if (nameobj == NULL)
86 return NULL;
87 module = PyModule_NewObject(nameobj);
88 Py_DECREF(nameobj);
89 return module;
90}
91
92
93PyObject *
Martin v. Löwis1a214512008-06-11 05:26:20 +000094PyModule_Create2(struct PyModuleDef* module, int module_api_version)
95{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000096 PyObject *d, *v, *n;
97 PyMethodDef *ml;
98 const char* name;
99 PyModuleObject *m;
R. David Murrayce4b1702010-12-14 23:06:25 +0000100 PyInterpreterState *interp = PyThreadState_Get()->interp;
101 if (interp->modules == NULL)
102 Py_FatalError("Python import machinery not initialized");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000103 if (PyType_Ready(&moduledef_type) < 0)
104 return NULL;
105 if (module->m_base.m_index == 0) {
106 max_module_number++;
107 Py_REFCNT(module) = 1;
108 Py_TYPE(module) = &moduledef_type;
109 module->m_base.m_index = max_module_number;
110 }
111 name = module->m_name;
Martin v. Löwis4d0d4712010-12-03 20:14:31 +0000112 if (module_api_version != PYTHON_API_VERSION && module_api_version != PYTHON_ABI_VERSION) {
Victor Stinner4a2b7a12010-08-13 14:03:48 +0000113 int err;
114 err = PyErr_WarnFormat(PyExc_RuntimeWarning, 1,
115 "Python C API version mismatch for module %.100s: "
116 "This Python has API version %d, module %.100s has version %d.",
117 name,
118 PYTHON_API_VERSION, name, module_api_version);
119 if (err)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000120 return NULL;
121 }
122 /* Make sure name is fully qualified.
Martin v. Löwis1a214512008-06-11 05:26:20 +0000123
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000124 This is a bit of a hack: when the shared library is loaded,
125 the module name is "package.module", but the module calls
126 PyModule_Create*() with just "module" for the name. The shared
127 library loader squirrels away the true name of the module in
128 _Py_PackageContext, and PyModule_Create*() will substitute this
129 (if the name actually matches).
130 */
131 if (_Py_PackageContext != NULL) {
132 char *p = strrchr(_Py_PackageContext, '.');
133 if (p != NULL && strcmp(module->m_name, p+1) == 0) {
134 name = _Py_PackageContext;
135 _Py_PackageContext = NULL;
136 }
137 }
138 if ((m = (PyModuleObject*)PyModule_New(name)) == NULL)
139 return NULL;
Martin v. Löwis1a214512008-06-11 05:26:20 +0000140
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000141 if (module->m_size > 0) {
142 m->md_state = PyMem_MALLOC(module->m_size);
143 if (!m->md_state) {
144 PyErr_NoMemory();
145 Py_DECREF(m);
146 return NULL;
147 }
148 memset(m->md_state, 0, module->m_size);
149 }
150
151 d = PyModule_GetDict((PyObject*)m);
152 if (module->m_methods != NULL) {
153 n = PyUnicode_FromString(name);
Meador Inge29e49d62012-07-19 13:45:43 -0500154 if (n == NULL) {
155 Py_DECREF(m);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000156 return NULL;
Meador Inge29e49d62012-07-19 13:45:43 -0500157 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000158 for (ml = module->m_methods; ml->ml_name != NULL; ml++) {
159 if ((ml->ml_flags & METH_CLASS) ||
160 (ml->ml_flags & METH_STATIC)) {
161 PyErr_SetString(PyExc_ValueError,
162 "module functions cannot set"
163 " METH_CLASS or METH_STATIC");
164 Py_DECREF(n);
Meador Inge29e49d62012-07-19 13:45:43 -0500165 Py_DECREF(m);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000166 return NULL;
167 }
168 v = PyCFunction_NewEx(ml, (PyObject*)m, n);
169 if (v == NULL) {
170 Py_DECREF(n);
Meador Inge29e49d62012-07-19 13:45:43 -0500171 Py_DECREF(m);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000172 return NULL;
173 }
174 if (PyDict_SetItemString(d, ml->ml_name, v) != 0) {
175 Py_DECREF(v);
176 Py_DECREF(n);
Meador Inge29e49d62012-07-19 13:45:43 -0500177 Py_DECREF(m);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000178 return NULL;
179 }
180 Py_DECREF(v);
181 }
182 Py_DECREF(n);
183 }
184 if (module->m_doc != NULL) {
185 v = PyUnicode_FromString(module->m_doc);
186 if (v == NULL || PyDict_SetItemString(d, "__doc__", v) != 0) {
187 Py_XDECREF(v);
Meador Inge29e49d62012-07-19 13:45:43 -0500188 Py_DECREF(m);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000189 return NULL;
190 }
191 Py_DECREF(v);
192 }
193 m->md_def = module;
194 return (PyObject*)m;
Martin v. Löwis1a214512008-06-11 05:26:20 +0000195}
196
197
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000198PyObject *
Fred Drakeee238b92000-07-09 06:03:25 +0000199PyModule_GetDict(PyObject *m)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000200{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000201 PyObject *d;
202 if (!PyModule_Check(m)) {
203 PyErr_BadInternalCall();
204 return NULL;
205 }
206 d = ((PyModuleObject *)m) -> md_dict;
207 if (d == NULL)
208 ((PyModuleObject *)m) -> md_dict = d = PyDict_New();
209 return d;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000210}
211
Victor Stinnerbd475112011-02-23 00:21:43 +0000212PyObject*
213PyModule_GetNameObject(PyObject *m)
Guido van Rossum0558a201990-10-26 15:00:11 +0000214{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000215 PyObject *d;
Victor Stinnerbd475112011-02-23 00:21:43 +0000216 PyObject *name;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000217 if (!PyModule_Check(m)) {
218 PyErr_BadArgument();
219 return NULL;
220 }
221 d = ((PyModuleObject *)m)->md_dict;
222 if (d == NULL ||
Victor Stinnerbd475112011-02-23 00:21:43 +0000223 (name = PyDict_GetItemString(d, "__name__")) == NULL ||
224 !PyUnicode_Check(name))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000225 {
226 PyErr_SetString(PyExc_SystemError, "nameless module");
227 return NULL;
228 }
Victor Stinnerbd475112011-02-23 00:21:43 +0000229 Py_INCREF(name);
230 return name;
231}
232
233const char *
234PyModule_GetName(PyObject *m)
235{
236 PyObject *name = PyModule_GetNameObject(m);
237 if (name == NULL)
238 return NULL;
239 Py_DECREF(name); /* module dict has still a reference */
240 return _PyUnicode_AsString(name);
Guido van Rossum0558a201990-10-26 15:00:11 +0000241}
242
Victor Stinner6c00c142010-08-17 23:37:11 +0000243PyObject*
244PyModule_GetFilenameObject(PyObject *m)
Guido van Rossum98cc19f1999-02-15 14:47:16 +0000245{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000246 PyObject *d;
247 PyObject *fileobj;
248 if (!PyModule_Check(m)) {
249 PyErr_BadArgument();
250 return NULL;
251 }
252 d = ((PyModuleObject *)m)->md_dict;
253 if (d == NULL ||
254 (fileobj = PyDict_GetItemString(d, "__file__")) == NULL ||
255 !PyUnicode_Check(fileobj))
256 {
257 PyErr_SetString(PyExc_SystemError, "module filename missing");
258 return NULL;
259 }
Victor Stinner6c00c142010-08-17 23:37:11 +0000260 Py_INCREF(fileobj);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000261 return fileobj;
Victor Stinner8124feb2010-05-07 00:50:12 +0000262}
263
264const char *
265PyModule_GetFilename(PyObject *m)
266{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000267 PyObject *fileobj;
Victor Stinner6c00c142010-08-17 23:37:11 +0000268 char *utf8;
269 fileobj = PyModule_GetFilenameObject(m);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000270 if (fileobj == NULL)
271 return NULL;
Victor Stinner6c00c142010-08-17 23:37:11 +0000272 utf8 = _PyUnicode_AsString(fileobj);
Victor Stinnerbd475112011-02-23 00:21:43 +0000273 Py_DECREF(fileobj); /* module dict has still a reference */
Victor Stinner6c00c142010-08-17 23:37:11 +0000274 return utf8;
Guido van Rossum98cc19f1999-02-15 14:47:16 +0000275}
276
Martin v. Löwis1a214512008-06-11 05:26:20 +0000277PyModuleDef*
278PyModule_GetDef(PyObject* m)
279{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000280 if (!PyModule_Check(m)) {
281 PyErr_BadArgument();
282 return NULL;
283 }
284 return ((PyModuleObject *)m)->md_def;
Martin v. Löwis1a214512008-06-11 05:26:20 +0000285}
286
287void*
288PyModule_GetState(PyObject* m)
289{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000290 if (!PyModule_Check(m)) {
291 PyErr_BadArgument();
292 return NULL;
293 }
294 return ((PyModuleObject *)m)->md_state;
Martin v. Löwis1a214512008-06-11 05:26:20 +0000295}
296
Guido van Rossumf1dc0611998-02-19 20:51:52 +0000297void
Fred Drakeee238b92000-07-09 06:03:25 +0000298_PyModule_Clear(PyObject *m)
Guido van Rossumf1dc0611998-02-19 20:51:52 +0000299{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000300 /* To make the execution order of destructors for global
301 objects a bit more predictable, we first zap all objects
302 whose name starts with a single underscore, before we clear
303 the entire dictionary. We zap them by replacing them with
304 None, rather than deleting them from the dictionary, to
305 avoid rehashing the dictionary (to some extent). */
Guido van Rossumf1dc0611998-02-19 20:51:52 +0000306
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000307 Py_ssize_t pos;
308 PyObject *key, *value;
309 PyObject *d;
Guido van Rossumf1dc0611998-02-19 20:51:52 +0000310
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000311 d = ((PyModuleObject *)m)->md_dict;
312 if (d == NULL)
313 return;
Guido van Rossumf1dc0611998-02-19 20:51:52 +0000314
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000315 /* First, clear only names starting with a single underscore */
316 pos = 0;
317 while (PyDict_Next(d, &pos, &key, &value)) {
318 if (value != Py_None && PyUnicode_Check(key)) {
Brett Cannon62228db2012-04-29 14:38:11 -0400319 if (PyUnicode_READ_CHAR(key, 0) == '_' &&
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200320 PyUnicode_READ_CHAR(key, 1) != '_') {
Victor Stinnerf3f22a22010-05-19 00:03:09 +0000321 if (Py_VerboseFlag > 1) {
322 const char *s = _PyUnicode_AsString(key);
323 if (s != NULL)
324 PySys_WriteStderr("# clear[1] %s\n", s);
325 else
326 PyErr_Clear();
327 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000328 PyDict_SetItem(d, key, Py_None);
329 }
330 }
331 }
Guido van Rossumf1dc0611998-02-19 20:51:52 +0000332
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000333 /* Next, clear all names except for __builtins__ */
334 pos = 0;
335 while (PyDict_Next(d, &pos, &key, &value)) {
336 if (value != Py_None && PyUnicode_Check(key)) {
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200337 if (PyUnicode_READ_CHAR(key, 0) != '_' ||
338 PyUnicode_CompareWithASCIIString(key, "__builtins__") != 0)
Victor Stinnerf3f22a22010-05-19 00:03:09 +0000339 {
340 if (Py_VerboseFlag > 1) {
341 const char *s = _PyUnicode_AsString(key);
342 if (s != NULL)
343 PySys_WriteStderr("# clear[2] %s\n", s);
344 else
345 PyErr_Clear();
346 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000347 PyDict_SetItem(d, key, Py_None);
348 }
349 }
350 }
Guido van Rossumf1dc0611998-02-19 20:51:52 +0000351
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000352 /* Note: we leave __builtins__ in place, so that destructors
353 of non-global objects defined in this module can still use
354 builtins, in particularly 'None'. */
Guido van Rossumf1dc0611998-02-19 20:51:52 +0000355
356}
357
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000358/* Methods */
359
Tim Peters6d6c1a32001-08-02 04:15:00 +0000360static int
Guido van Rossumc3a787e2002-06-04 05:52:47 +0000361module_init(PyModuleObject *m, PyObject *args, PyObject *kwds)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000362{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000363 static char *kwlist[] = {"name", "doc", NULL};
364 PyObject *dict, *name = Py_None, *doc = Py_None;
365 if (!PyArg_ParseTupleAndKeywords(args, kwds, "U|O:module.__init__",
366 kwlist, &name, &doc))
367 return -1;
368 dict = m->md_dict;
369 if (dict == NULL) {
370 dict = PyDict_New();
371 if (dict == NULL)
372 return -1;
373 m->md_dict = dict;
374 }
Antoine Pitroudcedaf62013-07-31 23:14:08 +0200375 if (module_init_dict(m, dict, name, doc) < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000376 return -1;
377 return 0;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000378}
379
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000380static void
Fred Drakeee238b92000-07-09 06:03:25 +0000381module_dealloc(PyModuleObject *m)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000382{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000383 PyObject_GC_UnTrack(m);
Antoine Pitroudcedaf62013-07-31 23:14:08 +0200384 if (Py_VerboseFlag && m->md_name) {
385 PySys_FormatStderr("# destroy %S\n", m->md_name);
386 }
387 if (m->md_weaklist != NULL)
388 PyObject_ClearWeakRefs((PyObject *) m);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000389 if (m->md_def && m->md_def->m_free)
390 m->md_def->m_free(m);
Antoine Pitroudcedaf62013-07-31 23:14:08 +0200391 Py_XDECREF(m->md_dict);
392 Py_XDECREF(m->md_name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000393 if (m->md_state != NULL)
394 PyMem_FREE(m->md_state);
395 Py_TYPE(m)->tp_free((PyObject *)m);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000396}
397
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000398static PyObject *
Fred Drakeee238b92000-07-09 06:03:25 +0000399module_repr(PyModuleObject *m)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000400{
Eric V. Smith984b11f2012-05-24 20:21:04 -0400401 PyObject *name, *filename, *repr, *loader = NULL;
Barry Warsaw2907fe62001-08-16 20:39:24 +0000402
Eric V. Smith984b11f2012-05-24 20:21:04 -0400403 /* See if the module has an __loader__. If it does, give the loader the
404 * first shot at producing a repr for the module.
405 */
406 if (m->md_dict != NULL) {
407 loader = PyDict_GetItemString(m->md_dict, "__loader__");
408 }
Brett Cannon4c14b5d2013-05-04 13:56:58 -0400409 if (loader != NULL && loader != Py_None) {
Eric V. Smith984b11f2012-05-24 20:21:04 -0400410 repr = PyObject_CallMethod(loader, "module_repr", "(O)",
411 (PyObject *)m, NULL);
412 if (repr == NULL) {
413 PyErr_Clear();
414 }
415 else {
416 return repr;
417 }
418 }
419 /* __loader__.module_repr(m) did not provide us with a repr. Next, see if
420 * the module has an __file__. If it doesn't then use repr(__loader__) if
421 * it exists, otherwise, just use module.__name__.
422 */
Victor Stinnerbd475112011-02-23 00:21:43 +0000423 name = PyModule_GetNameObject((PyObject *)m);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000424 if (name == NULL) {
425 PyErr_Clear();
Victor Stinnerbd475112011-02-23 00:21:43 +0000426 name = PyUnicode_FromStringAndSize("?", 1);
427 if (name == NULL)
428 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000429 }
Victor Stinner6c00c142010-08-17 23:37:11 +0000430 filename = PyModule_GetFilenameObject((PyObject *)m);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000431 if (filename == NULL) {
432 PyErr_Clear();
Brett Cannon4c14b5d2013-05-04 13:56:58 -0400433 /* There's no m.__file__, so if there was a __loader__, use that in
Eric V. Smith984b11f2012-05-24 20:21:04 -0400434 * the repr, otherwise, the only thing you can use is m.__name__
435 */
Brett Cannon4c14b5d2013-05-04 13:56:58 -0400436 if (loader == NULL || loader == Py_None) {
Eric V. Smith984b11f2012-05-24 20:21:04 -0400437 repr = PyUnicode_FromFormat("<module %R>", name);
438 }
439 else {
440 repr = PyUnicode_FromFormat("<module %R (%R)>", name, loader);
441 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000442 }
Eric V. Smith984b11f2012-05-24 20:21:04 -0400443 /* Finally, use m.__file__ */
Victor Stinnerbd475112011-02-23 00:21:43 +0000444 else {
445 repr = PyUnicode_FromFormat("<module %R from %R>", name, filename);
446 Py_DECREF(filename);
447 }
448 Py_DECREF(name);
Victor Stinner6c00c142010-08-17 23:37:11 +0000449 return repr;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000450}
451
Neil Schemenauer10e31cf2001-01-02 15:58:27 +0000452static int
453module_traverse(PyModuleObject *m, visitproc visit, void *arg)
454{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000455 if (m->md_def && m->md_def->m_traverse) {
456 int res = m->md_def->m_traverse((PyObject*)m, visit, arg);
457 if (res)
458 return res;
459 }
460 Py_VISIT(m->md_dict);
461 return 0;
Neil Schemenauer10e31cf2001-01-02 15:58:27 +0000462}
463
Martin v. Löwis1a214512008-06-11 05:26:20 +0000464static int
465module_clear(PyModuleObject *m)
466{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000467 if (m->md_def && m->md_def->m_clear) {
468 int res = m->md_def->m_clear((PyObject*)m);
469 if (res)
470 return res;
471 }
472 Py_CLEAR(m->md_dict);
473 return 0;
Martin v. Löwis1a214512008-06-11 05:26:20 +0000474}
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000475
Benjamin Peterson82b00c12011-05-24 11:09:06 -0500476static PyObject *
477module_dir(PyObject *self, PyObject *args)
478{
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200479 _Py_IDENTIFIER(__dict__);
Benjamin Peterson82b00c12011-05-24 11:09:06 -0500480 PyObject *result = NULL;
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +0200481 PyObject *dict = _PyObject_GetAttrId(self, &PyId___dict__);
Benjamin Peterson82b00c12011-05-24 11:09:06 -0500482
483 if (dict != NULL) {
484 if (PyDict_Check(dict))
485 result = PyDict_Keys(dict);
486 else {
487 const char *name = PyModule_GetName(self);
488 if (name)
489 PyErr_Format(PyExc_TypeError,
490 "%.200s.__dict__ is not a dictionary",
491 name);
492 }
493 }
494
495 Py_XDECREF(dict);
496 return result;
497}
498
499static PyMethodDef module_methods[] = {
500 {"__dir__", module_dir, METH_NOARGS,
Benjamin Petersonc7284122011-05-24 12:46:15 -0500501 PyDoc_STR("__dir__() -> list\nspecialized dir() implementation")},
Benjamin Peterson82b00c12011-05-24 11:09:06 -0500502 {0}
503};
504
Martin v. Löwis1a214512008-06-11 05:26:20 +0000505
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000506PyDoc_STRVAR(module_doc,
Guido van Rossumed3e09f2002-06-04 06:02:35 +0000507"module(name[, doc])\n\
508\n\
509Create a module object.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000510The name must be a string; the optional doc argument can have any type.");
Guido van Rossumed3e09f2002-06-04 06:02:35 +0000511
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000512PyTypeObject PyModule_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000513 PyVarObject_HEAD_INIT(&PyType_Type, 0)
514 "module", /* tp_name */
515 sizeof(PyModuleObject), /* tp_size */
516 0, /* tp_itemsize */
517 (destructor)module_dealloc, /* tp_dealloc */
518 0, /* tp_print */
519 0, /* tp_getattr */
520 0, /* tp_setattr */
521 0, /* tp_reserved */
522 (reprfunc)module_repr, /* tp_repr */
523 0, /* tp_as_number */
524 0, /* tp_as_sequence */
525 0, /* tp_as_mapping */
526 0, /* tp_hash */
527 0, /* tp_call */
528 0, /* tp_str */
529 PyObject_GenericGetAttr, /* tp_getattro */
530 PyObject_GenericSetAttr, /* tp_setattro */
531 0, /* tp_as_buffer */
532 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
533 Py_TPFLAGS_BASETYPE, /* tp_flags */
534 module_doc, /* tp_doc */
535 (traverseproc)module_traverse, /* tp_traverse */
536 (inquiry)module_clear, /* tp_clear */
537 0, /* tp_richcompare */
Antoine Pitroudcedaf62013-07-31 23:14:08 +0200538 offsetof(PyModuleObject, md_weaklist), /* tp_weaklistoffset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000539 0, /* tp_iter */
540 0, /* tp_iternext */
Benjamin Peterson82b00c12011-05-24 11:09:06 -0500541 module_methods, /* tp_methods */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000542 module_members, /* tp_members */
543 0, /* tp_getset */
544 0, /* tp_base */
545 0, /* tp_dict */
546 0, /* tp_descr_get */
547 0, /* tp_descr_set */
548 offsetof(PyModuleObject, md_dict), /* tp_dictoffset */
549 (initproc)module_init, /* tp_init */
550 PyType_GenericAlloc, /* tp_alloc */
551 PyType_GenericNew, /* tp_new */
552 PyObject_GC_Del, /* tp_free */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000553};