blob: 72978aef53a3227f58d3188b00452995f52790d8 [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
Guido van Rossumc0b618a1997-05-02 03:12:38 +000029PyObject *
Victor Stinner0639b562011-03-04 12:57:07 +000030PyModule_NewObject(PyObject *name)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000031{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000032 PyModuleObject *m;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000033 m = PyObject_GC_New(PyModuleObject, &PyModule_Type);
34 if (m == NULL)
35 return NULL;
36 m->md_def = NULL;
37 m->md_state = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000038 m->md_dict = PyDict_New();
Victor Stinner0639b562011-03-04 12:57:07 +000039 if (m->md_dict == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000040 goto fail;
Victor Stinner0639b562011-03-04 12:57:07 +000041 if (PyDict_SetItemString(m->md_dict, "__name__", name) != 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000042 goto fail;
43 if (PyDict_SetItemString(m->md_dict, "__doc__", Py_None) != 0)
44 goto fail;
45 if (PyDict_SetItemString(m->md_dict, "__package__", Py_None) != 0)
46 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000047 PyObject_GC_Track(m);
48 return (PyObject *)m;
Guido van Rossumc45611d1993-11-17 22:58:56 +000049
50 fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000051 Py_DECREF(m);
52 return NULL;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000053}
54
Martin v. Löwis1a214512008-06-11 05:26:20 +000055PyObject *
Victor Stinner0639b562011-03-04 12:57:07 +000056PyModule_New(const char *name)
57{
58 PyObject *nameobj, *module;
59 nameobj = PyUnicode_FromString(name);
60 if (nameobj == NULL)
61 return NULL;
62 module = PyModule_NewObject(nameobj);
63 Py_DECREF(nameobj);
64 return module;
65}
66
67
68PyObject *
Martin v. Löwis1a214512008-06-11 05:26:20 +000069PyModule_Create2(struct PyModuleDef* module, int module_api_version)
70{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000071 PyObject *d, *v, *n;
72 PyMethodDef *ml;
73 const char* name;
74 PyModuleObject *m;
R. David Murrayce4b1702010-12-14 23:06:25 +000075 PyInterpreterState *interp = PyThreadState_Get()->interp;
76 if (interp->modules == NULL)
77 Py_FatalError("Python import machinery not initialized");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000078 if (PyType_Ready(&moduledef_type) < 0)
79 return NULL;
80 if (module->m_base.m_index == 0) {
81 max_module_number++;
82 Py_REFCNT(module) = 1;
83 Py_TYPE(module) = &moduledef_type;
84 module->m_base.m_index = max_module_number;
85 }
86 name = module->m_name;
Martin v. Löwis4d0d4712010-12-03 20:14:31 +000087 if (module_api_version != PYTHON_API_VERSION && module_api_version != PYTHON_ABI_VERSION) {
Victor Stinner4a2b7a12010-08-13 14:03:48 +000088 int err;
89 err = PyErr_WarnFormat(PyExc_RuntimeWarning, 1,
90 "Python C API version mismatch for module %.100s: "
91 "This Python has API version %d, module %.100s has version %d.",
92 name,
93 PYTHON_API_VERSION, name, module_api_version);
94 if (err)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000095 return NULL;
96 }
97 /* Make sure name is fully qualified.
Martin v. Löwis1a214512008-06-11 05:26:20 +000098
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000099 This is a bit of a hack: when the shared library is loaded,
100 the module name is "package.module", but the module calls
101 PyModule_Create*() with just "module" for the name. The shared
102 library loader squirrels away the true name of the module in
103 _Py_PackageContext, and PyModule_Create*() will substitute this
104 (if the name actually matches).
105 */
106 if (_Py_PackageContext != NULL) {
107 char *p = strrchr(_Py_PackageContext, '.');
108 if (p != NULL && strcmp(module->m_name, p+1) == 0) {
109 name = _Py_PackageContext;
110 _Py_PackageContext = NULL;
111 }
112 }
113 if ((m = (PyModuleObject*)PyModule_New(name)) == NULL)
114 return NULL;
Martin v. Löwis1a214512008-06-11 05:26:20 +0000115
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000116 if (module->m_size > 0) {
117 m->md_state = PyMem_MALLOC(module->m_size);
118 if (!m->md_state) {
119 PyErr_NoMemory();
120 Py_DECREF(m);
121 return NULL;
122 }
123 memset(m->md_state, 0, module->m_size);
124 }
125
126 d = PyModule_GetDict((PyObject*)m);
127 if (module->m_methods != NULL) {
128 n = PyUnicode_FromString(name);
Meador Inge29e49d62012-07-19 13:45:43 -0500129 if (n == NULL) {
130 Py_DECREF(m);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000131 return NULL;
Meador Inge29e49d62012-07-19 13:45:43 -0500132 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000133 for (ml = module->m_methods; ml->ml_name != NULL; ml++) {
134 if ((ml->ml_flags & METH_CLASS) ||
135 (ml->ml_flags & METH_STATIC)) {
136 PyErr_SetString(PyExc_ValueError,
137 "module functions cannot set"
138 " METH_CLASS or METH_STATIC");
139 Py_DECREF(n);
Meador Inge29e49d62012-07-19 13:45:43 -0500140 Py_DECREF(m);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000141 return NULL;
142 }
143 v = PyCFunction_NewEx(ml, (PyObject*)m, n);
144 if (v == NULL) {
145 Py_DECREF(n);
Meador Inge29e49d62012-07-19 13:45:43 -0500146 Py_DECREF(m);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000147 return NULL;
148 }
149 if (PyDict_SetItemString(d, ml->ml_name, v) != 0) {
150 Py_DECREF(v);
151 Py_DECREF(n);
Meador Inge29e49d62012-07-19 13:45:43 -0500152 Py_DECREF(m);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000153 return NULL;
154 }
155 Py_DECREF(v);
156 }
157 Py_DECREF(n);
158 }
159 if (module->m_doc != NULL) {
160 v = PyUnicode_FromString(module->m_doc);
161 if (v == NULL || PyDict_SetItemString(d, "__doc__", v) != 0) {
162 Py_XDECREF(v);
Meador Inge29e49d62012-07-19 13:45:43 -0500163 Py_DECREF(m);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000164 return NULL;
165 }
166 Py_DECREF(v);
167 }
168 m->md_def = module;
169 return (PyObject*)m;
Martin v. Löwis1a214512008-06-11 05:26:20 +0000170}
171
172
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000173PyObject *
Fred Drakeee238b92000-07-09 06:03:25 +0000174PyModule_GetDict(PyObject *m)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000175{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000176 PyObject *d;
177 if (!PyModule_Check(m)) {
178 PyErr_BadInternalCall();
179 return NULL;
180 }
181 d = ((PyModuleObject *)m) -> md_dict;
182 if (d == NULL)
183 ((PyModuleObject *)m) -> md_dict = d = PyDict_New();
184 return d;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000185}
186
Victor Stinnerbd475112011-02-23 00:21:43 +0000187PyObject*
188PyModule_GetNameObject(PyObject *m)
Guido van Rossum0558a201990-10-26 15:00:11 +0000189{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000190 PyObject *d;
Victor Stinnerbd475112011-02-23 00:21:43 +0000191 PyObject *name;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000192 if (!PyModule_Check(m)) {
193 PyErr_BadArgument();
194 return NULL;
195 }
196 d = ((PyModuleObject *)m)->md_dict;
197 if (d == NULL ||
Victor Stinnerbd475112011-02-23 00:21:43 +0000198 (name = PyDict_GetItemString(d, "__name__")) == NULL ||
199 !PyUnicode_Check(name))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000200 {
201 PyErr_SetString(PyExc_SystemError, "nameless module");
202 return NULL;
203 }
Victor Stinnerbd475112011-02-23 00:21:43 +0000204 Py_INCREF(name);
205 return name;
206}
207
208const char *
209PyModule_GetName(PyObject *m)
210{
211 PyObject *name = PyModule_GetNameObject(m);
212 if (name == NULL)
213 return NULL;
214 Py_DECREF(name); /* module dict has still a reference */
215 return _PyUnicode_AsString(name);
Guido van Rossum0558a201990-10-26 15:00:11 +0000216}
217
Victor Stinner6c00c142010-08-17 23:37:11 +0000218PyObject*
219PyModule_GetFilenameObject(PyObject *m)
Guido van Rossum98cc19f1999-02-15 14:47:16 +0000220{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000221 PyObject *d;
222 PyObject *fileobj;
223 if (!PyModule_Check(m)) {
224 PyErr_BadArgument();
225 return NULL;
226 }
227 d = ((PyModuleObject *)m)->md_dict;
228 if (d == NULL ||
229 (fileobj = PyDict_GetItemString(d, "__file__")) == NULL ||
230 !PyUnicode_Check(fileobj))
231 {
232 PyErr_SetString(PyExc_SystemError, "module filename missing");
233 return NULL;
234 }
Victor Stinner6c00c142010-08-17 23:37:11 +0000235 Py_INCREF(fileobj);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000236 return fileobj;
Victor Stinner8124feb2010-05-07 00:50:12 +0000237}
238
239const char *
240PyModule_GetFilename(PyObject *m)
241{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000242 PyObject *fileobj;
Victor Stinner6c00c142010-08-17 23:37:11 +0000243 char *utf8;
244 fileobj = PyModule_GetFilenameObject(m);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000245 if (fileobj == NULL)
246 return NULL;
Victor Stinner6c00c142010-08-17 23:37:11 +0000247 utf8 = _PyUnicode_AsString(fileobj);
Victor Stinnerbd475112011-02-23 00:21:43 +0000248 Py_DECREF(fileobj); /* module dict has still a reference */
Victor Stinner6c00c142010-08-17 23:37:11 +0000249 return utf8;
Guido van Rossum98cc19f1999-02-15 14:47:16 +0000250}
251
Martin v. Löwis1a214512008-06-11 05:26:20 +0000252PyModuleDef*
253PyModule_GetDef(PyObject* m)
254{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000255 if (!PyModule_Check(m)) {
256 PyErr_BadArgument();
257 return NULL;
258 }
259 return ((PyModuleObject *)m)->md_def;
Martin v. Löwis1a214512008-06-11 05:26:20 +0000260}
261
262void*
263PyModule_GetState(PyObject* m)
264{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000265 if (!PyModule_Check(m)) {
266 PyErr_BadArgument();
267 return NULL;
268 }
269 return ((PyModuleObject *)m)->md_state;
Martin v. Löwis1a214512008-06-11 05:26:20 +0000270}
271
Guido van Rossumf1dc0611998-02-19 20:51:52 +0000272void
Fred Drakeee238b92000-07-09 06:03:25 +0000273_PyModule_Clear(PyObject *m)
Guido van Rossumf1dc0611998-02-19 20:51:52 +0000274{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000275 /* To make the execution order of destructors for global
276 objects a bit more predictable, we first zap all objects
277 whose name starts with a single underscore, before we clear
278 the entire dictionary. We zap them by replacing them with
279 None, rather than deleting them from the dictionary, to
280 avoid rehashing the dictionary (to some extent). */
Guido van Rossumf1dc0611998-02-19 20:51:52 +0000281
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000282 Py_ssize_t pos;
283 PyObject *key, *value;
284 PyObject *d;
Guido van Rossumf1dc0611998-02-19 20:51:52 +0000285
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000286 d = ((PyModuleObject *)m)->md_dict;
287 if (d == NULL)
288 return;
Guido van Rossumf1dc0611998-02-19 20:51:52 +0000289
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000290 /* First, clear only names starting with a single underscore */
291 pos = 0;
292 while (PyDict_Next(d, &pos, &key, &value)) {
293 if (value != Py_None && PyUnicode_Check(key)) {
Brett Cannon62228db2012-04-29 14:38:11 -0400294 if (PyUnicode_READ_CHAR(key, 0) == '_' &&
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200295 PyUnicode_READ_CHAR(key, 1) != '_') {
Victor Stinnerf3f22a22010-05-19 00:03:09 +0000296 if (Py_VerboseFlag > 1) {
297 const char *s = _PyUnicode_AsString(key);
298 if (s != NULL)
299 PySys_WriteStderr("# clear[1] %s\n", s);
300 else
301 PyErr_Clear();
302 }
Serhiy Storchaka1f9d11b2014-02-12 09:55:01 +0200303 if (PyDict_SetItem(d, key, Py_None) != 0)
304 PyErr_Clear();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000305 }
306 }
307 }
Guido van Rossumf1dc0611998-02-19 20:51:52 +0000308
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000309 /* Next, clear all names except for __builtins__ */
310 pos = 0;
311 while (PyDict_Next(d, &pos, &key, &value)) {
312 if (value != Py_None && PyUnicode_Check(key)) {
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200313 if (PyUnicode_READ_CHAR(key, 0) != '_' ||
314 PyUnicode_CompareWithASCIIString(key, "__builtins__") != 0)
Victor Stinnerf3f22a22010-05-19 00:03:09 +0000315 {
316 if (Py_VerboseFlag > 1) {
317 const char *s = _PyUnicode_AsString(key);
318 if (s != NULL)
319 PySys_WriteStderr("# clear[2] %s\n", s);
320 else
321 PyErr_Clear();
322 }
Serhiy Storchaka1f9d11b2014-02-12 09:55:01 +0200323 if (PyDict_SetItem(d, key, Py_None) != 0)
324 PyErr_Clear();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000325 }
326 }
327 }
Guido van Rossumf1dc0611998-02-19 20:51:52 +0000328
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000329 /* Note: we leave __builtins__ in place, so that destructors
330 of non-global objects defined in this module can still use
331 builtins, in particularly 'None'. */
Guido van Rossumf1dc0611998-02-19 20:51:52 +0000332
333}
334
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000335/* Methods */
336
Tim Peters6d6c1a32001-08-02 04:15:00 +0000337static int
Guido van Rossumc3a787e2002-06-04 05:52:47 +0000338module_init(PyModuleObject *m, PyObject *args, PyObject *kwds)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000339{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000340 static char *kwlist[] = {"name", "doc", NULL};
341 PyObject *dict, *name = Py_None, *doc = Py_None;
342 if (!PyArg_ParseTupleAndKeywords(args, kwds, "U|O:module.__init__",
343 kwlist, &name, &doc))
344 return -1;
345 dict = m->md_dict;
346 if (dict == NULL) {
347 dict = PyDict_New();
348 if (dict == NULL)
349 return -1;
350 m->md_dict = dict;
351 }
352 if (PyDict_SetItemString(dict, "__name__", name) < 0)
353 return -1;
354 if (PyDict_SetItemString(dict, "__doc__", doc) < 0)
355 return -1;
356 return 0;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000357}
358
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000359static void
Fred Drakeee238b92000-07-09 06:03:25 +0000360module_dealloc(PyModuleObject *m)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000361{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000362 PyObject_GC_UnTrack(m);
363 if (m->md_def && m->md_def->m_free)
364 m->md_def->m_free(m);
365 if (m->md_dict != NULL) {
Benjamin Peterson5c4bfc42010-10-12 22:57:59 +0000366 _PyModule_Clear((PyObject *)m);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000367 Py_DECREF(m->md_dict);
368 }
369 if (m->md_state != NULL)
370 PyMem_FREE(m->md_state);
371 Py_TYPE(m)->tp_free((PyObject *)m);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000372}
373
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000374static PyObject *
Fred Drakeee238b92000-07-09 06:03:25 +0000375module_repr(PyModuleObject *m)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000376{
Eric V. Smith984b11f2012-05-24 20:21:04 -0400377 PyObject *name, *filename, *repr, *loader = NULL;
Barry Warsaw2907fe62001-08-16 20:39:24 +0000378
Eric V. Smith984b11f2012-05-24 20:21:04 -0400379 /* See if the module has an __loader__. If it does, give the loader the
380 * first shot at producing a repr for the module.
381 */
382 if (m->md_dict != NULL) {
383 loader = PyDict_GetItemString(m->md_dict, "__loader__");
384 }
385 if (loader != NULL) {
386 repr = PyObject_CallMethod(loader, "module_repr", "(O)",
387 (PyObject *)m, NULL);
388 if (repr == NULL) {
389 PyErr_Clear();
390 }
391 else {
392 return repr;
393 }
394 }
395 /* __loader__.module_repr(m) did not provide us with a repr. Next, see if
396 * the module has an __file__. If it doesn't then use repr(__loader__) if
397 * it exists, otherwise, just use module.__name__.
398 */
Victor Stinnerbd475112011-02-23 00:21:43 +0000399 name = PyModule_GetNameObject((PyObject *)m);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000400 if (name == NULL) {
401 PyErr_Clear();
Victor Stinnerbd475112011-02-23 00:21:43 +0000402 name = PyUnicode_FromStringAndSize("?", 1);
403 if (name == NULL)
404 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000405 }
Victor Stinner6c00c142010-08-17 23:37:11 +0000406 filename = PyModule_GetFilenameObject((PyObject *)m);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000407 if (filename == NULL) {
408 PyErr_Clear();
Eric V. Smith984b11f2012-05-24 20:21:04 -0400409 /* There's no m.__file__, so if there was an __loader__, use that in
410 * the repr, otherwise, the only thing you can use is m.__name__
411 */
412 if (loader == NULL) {
413 repr = PyUnicode_FromFormat("<module %R>", name);
414 }
415 else {
416 repr = PyUnicode_FromFormat("<module %R (%R)>", name, loader);
417 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000418 }
Eric V. Smith984b11f2012-05-24 20:21:04 -0400419 /* Finally, use m.__file__ */
Victor Stinnerbd475112011-02-23 00:21:43 +0000420 else {
421 repr = PyUnicode_FromFormat("<module %R from %R>", name, filename);
422 Py_DECREF(filename);
423 }
424 Py_DECREF(name);
Victor Stinner6c00c142010-08-17 23:37:11 +0000425 return repr;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000426}
427
Neil Schemenauer10e31cf2001-01-02 15:58:27 +0000428static int
429module_traverse(PyModuleObject *m, visitproc visit, void *arg)
430{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000431 if (m->md_def && m->md_def->m_traverse) {
432 int res = m->md_def->m_traverse((PyObject*)m, visit, arg);
433 if (res)
434 return res;
435 }
436 Py_VISIT(m->md_dict);
437 return 0;
Neil Schemenauer10e31cf2001-01-02 15:58:27 +0000438}
439
Martin v. Löwis1a214512008-06-11 05:26:20 +0000440static int
441module_clear(PyModuleObject *m)
442{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000443 if (m->md_def && m->md_def->m_clear) {
444 int res = m->md_def->m_clear((PyObject*)m);
445 if (res)
446 return res;
447 }
448 Py_CLEAR(m->md_dict);
449 return 0;
Martin v. Löwis1a214512008-06-11 05:26:20 +0000450}
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000451
Benjamin Peterson82b00c12011-05-24 11:09:06 -0500452static PyObject *
453module_dir(PyObject *self, PyObject *args)
454{
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200455 _Py_IDENTIFIER(__dict__);
Benjamin Peterson82b00c12011-05-24 11:09:06 -0500456 PyObject *result = NULL;
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +0200457 PyObject *dict = _PyObject_GetAttrId(self, &PyId___dict__);
Benjamin Peterson82b00c12011-05-24 11:09:06 -0500458
459 if (dict != NULL) {
460 if (PyDict_Check(dict))
461 result = PyDict_Keys(dict);
462 else {
463 const char *name = PyModule_GetName(self);
464 if (name)
465 PyErr_Format(PyExc_TypeError,
466 "%.200s.__dict__ is not a dictionary",
467 name);
468 }
469 }
470
471 Py_XDECREF(dict);
472 return result;
473}
474
475static PyMethodDef module_methods[] = {
476 {"__dir__", module_dir, METH_NOARGS,
Benjamin Petersonc7284122011-05-24 12:46:15 -0500477 PyDoc_STR("__dir__() -> list\nspecialized dir() implementation")},
Benjamin Peterson82b00c12011-05-24 11:09:06 -0500478 {0}
479};
480
Martin v. Löwis1a214512008-06-11 05:26:20 +0000481
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000482PyDoc_STRVAR(module_doc,
Guido van Rossumed3e09f2002-06-04 06:02:35 +0000483"module(name[, doc])\n\
484\n\
485Create a module object.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000486The name must be a string; the optional doc argument can have any type.");
Guido van Rossumed3e09f2002-06-04 06:02:35 +0000487
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000488PyTypeObject PyModule_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000489 PyVarObject_HEAD_INIT(&PyType_Type, 0)
490 "module", /* tp_name */
491 sizeof(PyModuleObject), /* tp_size */
492 0, /* tp_itemsize */
493 (destructor)module_dealloc, /* tp_dealloc */
494 0, /* tp_print */
495 0, /* tp_getattr */
496 0, /* tp_setattr */
497 0, /* tp_reserved */
498 (reprfunc)module_repr, /* tp_repr */
499 0, /* tp_as_number */
500 0, /* tp_as_sequence */
501 0, /* tp_as_mapping */
502 0, /* tp_hash */
503 0, /* tp_call */
504 0, /* tp_str */
505 PyObject_GenericGetAttr, /* tp_getattro */
506 PyObject_GenericSetAttr, /* tp_setattro */
507 0, /* tp_as_buffer */
508 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
509 Py_TPFLAGS_BASETYPE, /* tp_flags */
510 module_doc, /* tp_doc */
511 (traverseproc)module_traverse, /* tp_traverse */
512 (inquiry)module_clear, /* tp_clear */
513 0, /* tp_richcompare */
514 0, /* tp_weaklistoffset */
515 0, /* tp_iter */
516 0, /* tp_iternext */
Benjamin Peterson82b00c12011-05-24 11:09:06 -0500517 module_methods, /* tp_methods */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000518 module_members, /* tp_members */
519 0, /* tp_getset */
520 0, /* tp_base */
521 0, /* tp_dict */
522 0, /* tp_descr_get */
523 0, /* tp_descr_set */
524 offsetof(PyModuleObject, md_dict), /* tp_dictoffset */
525 (initproc)module_init, /* tp_init */
526 PyType_GenericAlloc, /* tp_alloc */
527 PyType_GenericNew, /* tp_new */
528 PyObject_GC_Del, /* tp_free */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000529};