blob: 5970901558cecc07db3e4ae109e357e0de9e7d10 [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;
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 Pitrouf95a1b32010-05-09 15:52:27 +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 Pitrouf95a1b32010-05-09 15:52:27 +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
Brett Cannon4c14b5d2013-05-04 13:56:58 -040029static int
30module_init_dict(PyObject *md_dict, PyObject *name, PyObject *doc)
31{
32 if (md_dict == NULL)
33 return -1;
34 if (doc == NULL)
35 doc = Py_None;
36
37 if (PyDict_SetItemString(md_dict, "__name__", name) != 0)
38 return -1;
39 if (PyDict_SetItemString(md_dict, "__doc__", doc) != 0)
40 return -1;
41 if (PyDict_SetItemString(md_dict, "__package__", Py_None) != 0)
42 return -1;
43 if (PyDict_SetItemString(md_dict, "__loader__", Py_None) != 0)
44 return -1;
45
46 return 0;
47}
48
49
Guido van Rossumc0b618a1997-05-02 03:12:38 +000050PyObject *
Victor Stinner0639b562011-03-04 12:57:07 +000051PyModule_NewObject(PyObject *name)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000052{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000053 PyModuleObject *m;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000054 m = PyObject_GC_New(PyModuleObject, &PyModule_Type);
55 if (m == NULL)
56 return NULL;
57 m->md_def = NULL;
58 m->md_state = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000059 m->md_dict = PyDict_New();
Brett Cannon4c14b5d2013-05-04 13:56:58 -040060 if (module_init_dict(m->md_dict, name, NULL) != 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000061 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000062 PyObject_GC_Track(m);
63 return (PyObject *)m;
Guido van Rossumc45611d1993-11-17 22:58:56 +000064
65 fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000066 Py_DECREF(m);
67 return NULL;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000068}
69
Martin v. Löwis1a214512008-06-11 05:26:20 +000070PyObject *
Victor Stinner0639b562011-03-04 12:57:07 +000071PyModule_New(const char *name)
72{
73 PyObject *nameobj, *module;
74 nameobj = PyUnicode_FromString(name);
75 if (nameobj == NULL)
76 return NULL;
77 module = PyModule_NewObject(nameobj);
78 Py_DECREF(nameobj);
79 return module;
80}
81
82
83PyObject *
Martin v. Löwis1a214512008-06-11 05:26:20 +000084PyModule_Create2(struct PyModuleDef* module, int module_api_version)
85{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000086 PyObject *d, *v, *n;
87 PyMethodDef *ml;
88 const char* name;
89 PyModuleObject *m;
R. David Murrayce4b1702010-12-14 23:06:25 +000090 PyInterpreterState *interp = PyThreadState_Get()->interp;
91 if (interp->modules == NULL)
92 Py_FatalError("Python import machinery not initialized");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000093 if (PyType_Ready(&moduledef_type) < 0)
94 return NULL;
95 if (module->m_base.m_index == 0) {
96 max_module_number++;
97 Py_REFCNT(module) = 1;
98 Py_TYPE(module) = &moduledef_type;
99 module->m_base.m_index = max_module_number;
100 }
101 name = module->m_name;
Martin v. Löwis4d0d4712010-12-03 20:14:31 +0000102 if (module_api_version != PYTHON_API_VERSION && module_api_version != PYTHON_ABI_VERSION) {
Victor Stinner4a2b7a12010-08-13 14:03:48 +0000103 int err;
104 err = PyErr_WarnFormat(PyExc_RuntimeWarning, 1,
105 "Python C API version mismatch for module %.100s: "
106 "This Python has API version %d, module %.100s has version %d.",
107 name,
108 PYTHON_API_VERSION, name, module_api_version);
109 if (err)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000110 return NULL;
111 }
112 /* Make sure name is fully qualified.
Martin v. Löwis1a214512008-06-11 05:26:20 +0000113
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000114 This is a bit of a hack: when the shared library is loaded,
115 the module name is "package.module", but the module calls
116 PyModule_Create*() with just "module" for the name. The shared
117 library loader squirrels away the true name of the module in
118 _Py_PackageContext, and PyModule_Create*() will substitute this
119 (if the name actually matches).
120 */
121 if (_Py_PackageContext != NULL) {
122 char *p = strrchr(_Py_PackageContext, '.');
123 if (p != NULL && strcmp(module->m_name, p+1) == 0) {
124 name = _Py_PackageContext;
125 _Py_PackageContext = NULL;
126 }
127 }
128 if ((m = (PyModuleObject*)PyModule_New(name)) == NULL)
129 return NULL;
Martin v. Löwis1a214512008-06-11 05:26:20 +0000130
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000131 if (module->m_size > 0) {
132 m->md_state = PyMem_MALLOC(module->m_size);
133 if (!m->md_state) {
134 PyErr_NoMemory();
135 Py_DECREF(m);
136 return NULL;
137 }
138 memset(m->md_state, 0, module->m_size);
139 }
140
141 d = PyModule_GetDict((PyObject*)m);
142 if (module->m_methods != NULL) {
143 n = PyUnicode_FromString(name);
Meador Inge29e49d62012-07-19 13:45:43 -0500144 if (n == NULL) {
145 Py_DECREF(m);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000146 return NULL;
Meador Inge29e49d62012-07-19 13:45:43 -0500147 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000148 for (ml = module->m_methods; ml->ml_name != NULL; ml++) {
149 if ((ml->ml_flags & METH_CLASS) ||
150 (ml->ml_flags & METH_STATIC)) {
151 PyErr_SetString(PyExc_ValueError,
152 "module functions cannot set"
153 " METH_CLASS or METH_STATIC");
154 Py_DECREF(n);
Meador Inge29e49d62012-07-19 13:45:43 -0500155 Py_DECREF(m);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000156 return NULL;
157 }
158 v = PyCFunction_NewEx(ml, (PyObject*)m, n);
159 if (v == NULL) {
160 Py_DECREF(n);
Meador Inge29e49d62012-07-19 13:45:43 -0500161 Py_DECREF(m);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000162 return NULL;
163 }
164 if (PyDict_SetItemString(d, ml->ml_name, v) != 0) {
165 Py_DECREF(v);
166 Py_DECREF(n);
Meador Inge29e49d62012-07-19 13:45:43 -0500167 Py_DECREF(m);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000168 return NULL;
169 }
170 Py_DECREF(v);
171 }
172 Py_DECREF(n);
173 }
174 if (module->m_doc != NULL) {
175 v = PyUnicode_FromString(module->m_doc);
176 if (v == NULL || PyDict_SetItemString(d, "__doc__", v) != 0) {
177 Py_XDECREF(v);
Meador Inge29e49d62012-07-19 13:45:43 -0500178 Py_DECREF(m);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000179 return NULL;
180 }
181 Py_DECREF(v);
182 }
183 m->md_def = module;
184 return (PyObject*)m;
Martin v. Löwis1a214512008-06-11 05:26:20 +0000185}
186
187
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000188PyObject *
Fred Drakeee238b92000-07-09 06:03:25 +0000189PyModule_GetDict(PyObject *m)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000190{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000191 PyObject *d;
192 if (!PyModule_Check(m)) {
193 PyErr_BadInternalCall();
194 return NULL;
195 }
196 d = ((PyModuleObject *)m) -> md_dict;
197 if (d == NULL)
198 ((PyModuleObject *)m) -> md_dict = d = PyDict_New();
199 return d;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000200}
201
Victor Stinnerbd475112011-02-23 00:21:43 +0000202PyObject*
203PyModule_GetNameObject(PyObject *m)
Guido van Rossum0558a201990-10-26 15:00:11 +0000204{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000205 PyObject *d;
Victor Stinnerbd475112011-02-23 00:21:43 +0000206 PyObject *name;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000207 if (!PyModule_Check(m)) {
208 PyErr_BadArgument();
209 return NULL;
210 }
211 d = ((PyModuleObject *)m)->md_dict;
212 if (d == NULL ||
Victor Stinnerbd475112011-02-23 00:21:43 +0000213 (name = PyDict_GetItemString(d, "__name__")) == NULL ||
214 !PyUnicode_Check(name))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000215 {
216 PyErr_SetString(PyExc_SystemError, "nameless module");
217 return NULL;
218 }
Victor Stinnerbd475112011-02-23 00:21:43 +0000219 Py_INCREF(name);
220 return name;
221}
222
223const char *
224PyModule_GetName(PyObject *m)
225{
226 PyObject *name = PyModule_GetNameObject(m);
227 if (name == NULL)
228 return NULL;
229 Py_DECREF(name); /* module dict has still a reference */
230 return _PyUnicode_AsString(name);
Guido van Rossum0558a201990-10-26 15:00:11 +0000231}
232
Victor Stinner6c00c142010-08-17 23:37:11 +0000233PyObject*
234PyModule_GetFilenameObject(PyObject *m)
Guido van Rossum98cc19f1999-02-15 14:47:16 +0000235{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000236 PyObject *d;
237 PyObject *fileobj;
238 if (!PyModule_Check(m)) {
239 PyErr_BadArgument();
240 return NULL;
241 }
242 d = ((PyModuleObject *)m)->md_dict;
243 if (d == NULL ||
244 (fileobj = PyDict_GetItemString(d, "__file__")) == NULL ||
245 !PyUnicode_Check(fileobj))
246 {
247 PyErr_SetString(PyExc_SystemError, "module filename missing");
248 return NULL;
249 }
Victor Stinner6c00c142010-08-17 23:37:11 +0000250 Py_INCREF(fileobj);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000251 return fileobj;
Victor Stinner8124feb2010-05-07 00:50:12 +0000252}
253
254const char *
255PyModule_GetFilename(PyObject *m)
256{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000257 PyObject *fileobj;
Victor Stinner6c00c142010-08-17 23:37:11 +0000258 char *utf8;
259 fileobj = PyModule_GetFilenameObject(m);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000260 if (fileobj == NULL)
261 return NULL;
Victor Stinner6c00c142010-08-17 23:37:11 +0000262 utf8 = _PyUnicode_AsString(fileobj);
Victor Stinnerbd475112011-02-23 00:21:43 +0000263 Py_DECREF(fileobj); /* module dict has still a reference */
Victor Stinner6c00c142010-08-17 23:37:11 +0000264 return utf8;
Guido van Rossum98cc19f1999-02-15 14:47:16 +0000265}
266
Martin v. Löwis1a214512008-06-11 05:26:20 +0000267PyModuleDef*
268PyModule_GetDef(PyObject* m)
269{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000270 if (!PyModule_Check(m)) {
271 PyErr_BadArgument();
272 return NULL;
273 }
274 return ((PyModuleObject *)m)->md_def;
Martin v. Löwis1a214512008-06-11 05:26:20 +0000275}
276
277void*
278PyModule_GetState(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_state;
Martin v. Löwis1a214512008-06-11 05:26:20 +0000285}
286
Guido van Rossumf1dc0611998-02-19 20:51:52 +0000287void
Fred Drakeee238b92000-07-09 06:03:25 +0000288_PyModule_Clear(PyObject *m)
Guido van Rossumf1dc0611998-02-19 20:51:52 +0000289{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000290 /* To make the execution order of destructors for global
291 objects a bit more predictable, we first zap all objects
292 whose name starts with a single underscore, before we clear
293 the entire dictionary. We zap them by replacing them with
294 None, rather than deleting them from the dictionary, to
295 avoid rehashing the dictionary (to some extent). */
Guido van Rossumf1dc0611998-02-19 20:51:52 +0000296
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000297 Py_ssize_t pos;
298 PyObject *key, *value;
299 PyObject *d;
Guido van Rossumf1dc0611998-02-19 20:51:52 +0000300
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000301 d = ((PyModuleObject *)m)->md_dict;
302 if (d == NULL)
303 return;
Guido van Rossumf1dc0611998-02-19 20:51:52 +0000304
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000305 /* First, clear only names starting with a single underscore */
306 pos = 0;
307 while (PyDict_Next(d, &pos, &key, &value)) {
308 if (value != Py_None && PyUnicode_Check(key)) {
Brett Cannon62228db2012-04-29 14:38:11 -0400309 if (PyUnicode_READ_CHAR(key, 0) == '_' &&
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200310 PyUnicode_READ_CHAR(key, 1) != '_') {
Victor Stinnerf3f22a22010-05-19 00:03:09 +0000311 if (Py_VerboseFlag > 1) {
312 const char *s = _PyUnicode_AsString(key);
313 if (s != NULL)
314 PySys_WriteStderr("# clear[1] %s\n", s);
315 else
316 PyErr_Clear();
317 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000318 PyDict_SetItem(d, key, Py_None);
319 }
320 }
321 }
Guido van Rossumf1dc0611998-02-19 20:51:52 +0000322
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000323 /* Next, clear all names except for __builtins__ */
324 pos = 0;
325 while (PyDict_Next(d, &pos, &key, &value)) {
326 if (value != Py_None && PyUnicode_Check(key)) {
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200327 if (PyUnicode_READ_CHAR(key, 0) != '_' ||
328 PyUnicode_CompareWithASCIIString(key, "__builtins__") != 0)
Victor Stinnerf3f22a22010-05-19 00:03:09 +0000329 {
330 if (Py_VerboseFlag > 1) {
331 const char *s = _PyUnicode_AsString(key);
332 if (s != NULL)
333 PySys_WriteStderr("# clear[2] %s\n", s);
334 else
335 PyErr_Clear();
336 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000337 PyDict_SetItem(d, key, Py_None);
338 }
339 }
340 }
Guido van Rossumf1dc0611998-02-19 20:51:52 +0000341
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000342 /* Note: we leave __builtins__ in place, so that destructors
343 of non-global objects defined in this module can still use
344 builtins, in particularly 'None'. */
Guido van Rossumf1dc0611998-02-19 20:51:52 +0000345
346}
347
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000348/* Methods */
349
Tim Peters6d6c1a32001-08-02 04:15:00 +0000350static int
Guido van Rossumc3a787e2002-06-04 05:52:47 +0000351module_init(PyModuleObject *m, PyObject *args, PyObject *kwds)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000352{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000353 static char *kwlist[] = {"name", "doc", NULL};
354 PyObject *dict, *name = Py_None, *doc = Py_None;
355 if (!PyArg_ParseTupleAndKeywords(args, kwds, "U|O:module.__init__",
356 kwlist, &name, &doc))
357 return -1;
358 dict = m->md_dict;
359 if (dict == NULL) {
360 dict = PyDict_New();
361 if (dict == NULL)
362 return -1;
363 m->md_dict = dict;
364 }
Brett Cannon4c14b5d2013-05-04 13:56:58 -0400365 if (module_init_dict(dict, name, doc) < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000366 return -1;
367 return 0;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000368}
369
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000370static void
Fred Drakeee238b92000-07-09 06:03:25 +0000371module_dealloc(PyModuleObject *m)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000372{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000373 PyObject_GC_UnTrack(m);
374 if (m->md_def && m->md_def->m_free)
375 m->md_def->m_free(m);
376 if (m->md_dict != NULL) {
Benjamin Peterson5c4bfc42010-10-12 22:57:59 +0000377 _PyModule_Clear((PyObject *)m);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000378 Py_DECREF(m->md_dict);
379 }
380 if (m->md_state != NULL)
381 PyMem_FREE(m->md_state);
382 Py_TYPE(m)->tp_free((PyObject *)m);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000383}
384
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000385static PyObject *
Fred Drakeee238b92000-07-09 06:03:25 +0000386module_repr(PyModuleObject *m)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000387{
Eric V. Smith984b11f2012-05-24 20:21:04 -0400388 PyObject *name, *filename, *repr, *loader = NULL;
Barry Warsaw2907fe62001-08-16 20:39:24 +0000389
Eric V. Smith984b11f2012-05-24 20:21:04 -0400390 /* See if the module has an __loader__. If it does, give the loader the
391 * first shot at producing a repr for the module.
392 */
393 if (m->md_dict != NULL) {
394 loader = PyDict_GetItemString(m->md_dict, "__loader__");
395 }
Brett Cannon4c14b5d2013-05-04 13:56:58 -0400396 if (loader != NULL && loader != Py_None) {
Eric V. Smith984b11f2012-05-24 20:21:04 -0400397 repr = PyObject_CallMethod(loader, "module_repr", "(O)",
398 (PyObject *)m, NULL);
399 if (repr == NULL) {
400 PyErr_Clear();
401 }
402 else {
403 return repr;
404 }
405 }
406 /* __loader__.module_repr(m) did not provide us with a repr. Next, see if
407 * the module has an __file__. If it doesn't then use repr(__loader__) if
408 * it exists, otherwise, just use module.__name__.
409 */
Victor Stinnerbd475112011-02-23 00:21:43 +0000410 name = PyModule_GetNameObject((PyObject *)m);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000411 if (name == NULL) {
412 PyErr_Clear();
Victor Stinnerbd475112011-02-23 00:21:43 +0000413 name = PyUnicode_FromStringAndSize("?", 1);
414 if (name == NULL)
415 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000416 }
Victor Stinner6c00c142010-08-17 23:37:11 +0000417 filename = PyModule_GetFilenameObject((PyObject *)m);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000418 if (filename == NULL) {
419 PyErr_Clear();
Brett Cannon4c14b5d2013-05-04 13:56:58 -0400420 /* There's no m.__file__, so if there was a __loader__, use that in
Eric V. Smith984b11f2012-05-24 20:21:04 -0400421 * the repr, otherwise, the only thing you can use is m.__name__
422 */
Brett Cannon4c14b5d2013-05-04 13:56:58 -0400423 if (loader == NULL || loader == Py_None) {
Eric V. Smith984b11f2012-05-24 20:21:04 -0400424 repr = PyUnicode_FromFormat("<module %R>", name);
425 }
426 else {
427 repr = PyUnicode_FromFormat("<module %R (%R)>", name, loader);
428 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000429 }
Eric V. Smith984b11f2012-05-24 20:21:04 -0400430 /* Finally, use m.__file__ */
Victor Stinnerbd475112011-02-23 00:21:43 +0000431 else {
432 repr = PyUnicode_FromFormat("<module %R from %R>", name, filename);
433 Py_DECREF(filename);
434 }
435 Py_DECREF(name);
Victor Stinner6c00c142010-08-17 23:37:11 +0000436 return repr;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000437}
438
Neil Schemenauer10e31cf2001-01-02 15:58:27 +0000439static int
440module_traverse(PyModuleObject *m, visitproc visit, void *arg)
441{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000442 if (m->md_def && m->md_def->m_traverse) {
443 int res = m->md_def->m_traverse((PyObject*)m, visit, arg);
444 if (res)
445 return res;
446 }
447 Py_VISIT(m->md_dict);
448 return 0;
Neil Schemenauer10e31cf2001-01-02 15:58:27 +0000449}
450
Martin v. Löwis1a214512008-06-11 05:26:20 +0000451static int
452module_clear(PyModuleObject *m)
453{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000454 if (m->md_def && m->md_def->m_clear) {
455 int res = m->md_def->m_clear((PyObject*)m);
456 if (res)
457 return res;
458 }
459 Py_CLEAR(m->md_dict);
460 return 0;
Martin v. Löwis1a214512008-06-11 05:26:20 +0000461}
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000462
Benjamin Peterson82b00c12011-05-24 11:09:06 -0500463static PyObject *
464module_dir(PyObject *self, PyObject *args)
465{
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200466 _Py_IDENTIFIER(__dict__);
Benjamin Peterson82b00c12011-05-24 11:09:06 -0500467 PyObject *result = NULL;
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +0200468 PyObject *dict = _PyObject_GetAttrId(self, &PyId___dict__);
Benjamin Peterson82b00c12011-05-24 11:09:06 -0500469
470 if (dict != NULL) {
471 if (PyDict_Check(dict))
472 result = PyDict_Keys(dict);
473 else {
474 const char *name = PyModule_GetName(self);
475 if (name)
476 PyErr_Format(PyExc_TypeError,
477 "%.200s.__dict__ is not a dictionary",
478 name);
479 }
480 }
481
482 Py_XDECREF(dict);
483 return result;
484}
485
486static PyMethodDef module_methods[] = {
487 {"__dir__", module_dir, METH_NOARGS,
Benjamin Petersonc7284122011-05-24 12:46:15 -0500488 PyDoc_STR("__dir__() -> list\nspecialized dir() implementation")},
Benjamin Peterson82b00c12011-05-24 11:09:06 -0500489 {0}
490};
491
Martin v. Löwis1a214512008-06-11 05:26:20 +0000492
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000493PyDoc_STRVAR(module_doc,
Guido van Rossumed3e09f2002-06-04 06:02:35 +0000494"module(name[, doc])\n\
495\n\
496Create a module object.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000497The name must be a string; the optional doc argument can have any type.");
Guido van Rossumed3e09f2002-06-04 06:02:35 +0000498
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000499PyTypeObject PyModule_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000500 PyVarObject_HEAD_INIT(&PyType_Type, 0)
501 "module", /* tp_name */
502 sizeof(PyModuleObject), /* tp_size */
503 0, /* tp_itemsize */
504 (destructor)module_dealloc, /* tp_dealloc */
505 0, /* tp_print */
506 0, /* tp_getattr */
507 0, /* tp_setattr */
508 0, /* tp_reserved */
509 (reprfunc)module_repr, /* tp_repr */
510 0, /* tp_as_number */
511 0, /* tp_as_sequence */
512 0, /* tp_as_mapping */
513 0, /* tp_hash */
514 0, /* tp_call */
515 0, /* tp_str */
516 PyObject_GenericGetAttr, /* tp_getattro */
517 PyObject_GenericSetAttr, /* tp_setattro */
518 0, /* tp_as_buffer */
519 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
520 Py_TPFLAGS_BASETYPE, /* tp_flags */
521 module_doc, /* tp_doc */
522 (traverseproc)module_traverse, /* tp_traverse */
523 (inquiry)module_clear, /* tp_clear */
524 0, /* tp_richcompare */
525 0, /* tp_weaklistoffset */
526 0, /* tp_iter */
527 0, /* tp_iternext */
Benjamin Peterson82b00c12011-05-24 11:09:06 -0500528 module_methods, /* tp_methods */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000529 module_members, /* tp_members */
530 0, /* tp_getset */
531 0, /* tp_base */
532 0, /* tp_dict */
533 0, /* tp_descr_get */
534 0, /* tp_descr_set */
535 offsetof(PyModuleObject, md_dict), /* tp_dictoffset */
536 (initproc)module_init, /* tp_init */
537 PyType_GenericAlloc, /* tp_alloc */
538 PyType_GenericNew, /* tp_new */
539 PyObject_GC_Del, /* tp_free */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000540};