blob: 180f7bc3c72e9b36004d05b89a64495e77cc0e4b [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 *
Fred Drakeee238b92000-07-09 06:03:25 +000018PyModule_New(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;
Guido van Rossumc0b618a1997-05-02 03:12:38 +000025 nameobj = PyString_FromString(name);
26 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;
Guido van Rossumc0b618a1997-05-02 03:12:38 +000033 Py_DECREF(nameobj);
Neil Schemenauere83c00e2001-08-29 23:54:21 +000034 PyObject_GC_Track(m);
Guido van Rossumc0b618a1997-05-02 03:12:38 +000035 return (PyObject *)m;
Guido van Rossumc45611d1993-11-17 22:58:56 +000036
37 fail:
Guido van Rossumc0b618a1997-05-02 03:12:38 +000038 Py_XDECREF(nameobj);
39 Py_DECREF(m);
Guido van Rossumc45611d1993-11-17 22:58:56 +000040 return NULL;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000041}
42
Guido van Rossumc0b618a1997-05-02 03:12:38 +000043PyObject *
Fred Drakeee238b92000-07-09 06:03:25 +000044PyModule_GetDict(PyObject *m)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000045{
Guido van Rossumc0b618a1997-05-02 03:12:38 +000046 if (!PyModule_Check(m)) {
47 PyErr_BadInternalCall();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000048 return NULL;
49 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +000050 return ((PyModuleObject *)m) -> md_dict;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000051}
52
Guido van Rossum0558a201990-10-26 15:00:11 +000053char *
Fred Drakeee238b92000-07-09 06:03:25 +000054PyModule_GetName(PyObject *m)
Guido van Rossum0558a201990-10-26 15:00:11 +000055{
Guido van Rossumc0b618a1997-05-02 03:12:38 +000056 PyObject *nameobj;
57 if (!PyModule_Check(m)) {
58 PyErr_BadArgument();
Guido van Rossum0558a201990-10-26 15:00:11 +000059 return NULL;
60 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +000061 nameobj = PyDict_GetItemString(((PyModuleObject *)m)->md_dict,
62 "__name__");
63 if (nameobj == NULL || !PyString_Check(nameobj)) {
64 PyErr_SetString(PyExc_SystemError, "nameless module");
Guido van Rossumc45611d1993-11-17 22:58:56 +000065 return NULL;
66 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +000067 return PyString_AsString(nameobj);
Guido van Rossum0558a201990-10-26 15:00:11 +000068}
69
Guido van Rossum98cc19f1999-02-15 14:47:16 +000070char *
Fred Drakeee238b92000-07-09 06:03:25 +000071PyModule_GetFilename(PyObject *m)
Guido van Rossum98cc19f1999-02-15 14:47:16 +000072{
73 PyObject *fileobj;
74 if (!PyModule_Check(m)) {
75 PyErr_BadArgument();
76 return NULL;
77 }
78 fileobj = PyDict_GetItemString(((PyModuleObject *)m)->md_dict,
79 "__file__");
80 if (fileobj == NULL || !PyString_Check(fileobj)) {
81 PyErr_SetString(PyExc_SystemError, "module filename missing");
82 return NULL;
83 }
84 return PyString_AsString(fileobj);
85}
86
Guido van Rossumf1dc0611998-02-19 20:51:52 +000087void
Fred Drakeee238b92000-07-09 06:03:25 +000088_PyModule_Clear(PyObject *m)
Guido van Rossumf1dc0611998-02-19 20:51:52 +000089{
90 /* To make the execution order of destructors for global
91 objects a bit more predictable, we first zap all objects
92 whose name starts with a single underscore, before we clear
93 the entire dictionary. We zap them by replacing them with
94 None, rather than deleting them from the dictionary, to
95 avoid rehashing the dictionary (to some extent). */
96
97 int pos;
98 PyObject *key, *value;
99 PyObject *d;
100
101 d = ((PyModuleObject *)m)->md_dict;
102
103 /* First, clear only names starting with a single underscore */
104 pos = 0;
105 while (PyDict_Next(d, &pos, &key, &value)) {
106 if (value != Py_None && PyString_Check(key)) {
107 char *s = PyString_AsString(key);
108 if (s[0] == '_' && s[1] != '_') {
109 if (Py_VerboseFlag > 1)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000110 PySys_WriteStderr("# clear[1] %s\n", s);
Guido van Rossumf1dc0611998-02-19 20:51:52 +0000111 PyDict_SetItem(d, key, Py_None);
112 }
113 }
114 }
115
116 /* Next, clear all names except for __builtins__ */
117 pos = 0;
118 while (PyDict_Next(d, &pos, &key, &value)) {
119 if (value != Py_None && PyString_Check(key)) {
120 char *s = PyString_AsString(key);
121 if (s[0] != '_' || strcmp(s, "__builtins__") != 0) {
122 if (Py_VerboseFlag > 1)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000123 PySys_WriteStderr("# clear[2] %s\n", s);
Guido van Rossumf1dc0611998-02-19 20:51:52 +0000124 PyDict_SetItem(d, key, Py_None);
125 }
126 }
127 }
128
129 /* Note: we leave __builtins__ in place, so that destructors
130 of non-global objects defined in this module can still use
131 builtins, in particularly 'None'. */
132
133}
134
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000135/* Methods */
136
Tim Peters6d6c1a32001-08-02 04:15:00 +0000137static int
138module_init(PyModuleObject *m, PyObject *args, PyObject *kw)
139{
140 m->md_dict = PyDict_New();
141 if (m->md_dict == NULL)
142 return -1;
143 return 0;
144}
145
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000146static void
Fred Drakeee238b92000-07-09 06:03:25 +0000147module_dealloc(PyModuleObject *m)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000148{
Neil Schemenauere83c00e2001-08-29 23:54:21 +0000149 PyObject_GC_UnTrack(m);
Guido van Rossum27e916f1995-01-26 00:39:00 +0000150 if (m->md_dict != NULL) {
Guido van Rossumf1dc0611998-02-19 20:51:52 +0000151 _PyModule_Clear((PyObject *)m);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000152 Py_DECREF(m->md_dict);
Guido van Rossum27e916f1995-01-26 00:39:00 +0000153 }
Guido van Rossum9475a232001-10-05 20:51:39 +0000154 m->ob_type->tp_free((PyObject *)m);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000155}
156
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000157static PyObject *
Fred Drakeee238b92000-07-09 06:03:25 +0000158module_repr(PyModuleObject *m)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000159{
Guido van Rossum98cc19f1999-02-15 14:47:16 +0000160 char *name;
161 char *filename;
Barry Warsaw2907fe62001-08-16 20:39:24 +0000162
Guido van Rossum98cc19f1999-02-15 14:47:16 +0000163 name = PyModule_GetName((PyObject *)m);
Guido van Rossumc45611d1993-11-17 22:58:56 +0000164 if (name == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000165 PyErr_Clear();
Guido van Rossumc45611d1993-11-17 22:58:56 +0000166 name = "?";
167 }
Guido van Rossum98cc19f1999-02-15 14:47:16 +0000168 filename = PyModule_GetFilename((PyObject *)m);
169 if (filename == NULL) {
170 PyErr_Clear();
Barry Warsaw7ce36942001-08-24 18:34:26 +0000171 return PyString_FromFormat("<module '%s' (built-in)>", name);
Guido van Rossum98cc19f1999-02-15 14:47:16 +0000172 }
Barry Warsaw7ce36942001-08-24 18:34:26 +0000173 return PyString_FromFormat("<module '%s' from '%s'>", name, filename);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000174}
175
Neil Schemenauer10e31cf2001-01-02 15:58:27 +0000176/* We only need a traverse function, no clear function: If the module
177 is in a cycle, md_dict will be cleared as well, which will break
178 the cycle. */
179static int
180module_traverse(PyModuleObject *m, visitproc visit, void *arg)
181{
182 if (m->md_dict != NULL)
183 return visit(m->md_dict, arg);
184 return 0;
185}
186
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000187PyTypeObject PyModule_Type = {
188 PyObject_HEAD_INIT(&PyType_Type)
Tim Petersd85e1022001-05-11 21:51:48 +0000189 0, /* ob_size */
190 "module", /* tp_name */
Neil Schemenauere83c00e2001-08-29 23:54:21 +0000191 sizeof(PyModuleObject), /* tp_size */
Tim Petersd85e1022001-05-11 21:51:48 +0000192 0, /* tp_itemsize */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000193 (destructor)module_dealloc, /* tp_dealloc */
Tim Petersd85e1022001-05-11 21:51:48 +0000194 0, /* tp_print */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000195 0, /* tp_getattr */
196 0, /* tp_setattr */
Tim Petersd85e1022001-05-11 21:51:48 +0000197 0, /* tp_compare */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000198 (reprfunc)module_repr, /* tp_repr */
Tim Petersd85e1022001-05-11 21:51:48 +0000199 0, /* tp_as_number */
200 0, /* tp_as_sequence */
201 0, /* tp_as_mapping */
202 0, /* tp_hash */
203 0, /* tp_call */
204 0, /* tp_str */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000205 PyObject_GenericGetAttr, /* tp_getattro */
206 PyObject_GenericSetAttr, /* tp_setattro */
Tim Petersd85e1022001-05-11 21:51:48 +0000207 0, /* tp_as_buffer */
Neil Schemenauere83c00e2001-08-29 23:54:21 +0000208 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
Tim Peters6d6c1a32001-08-02 04:15:00 +0000209 Py_TPFLAGS_BASETYPE, /* tp_flags */
Tim Petersd85e1022001-05-11 21:51:48 +0000210 0, /* tp_doc */
211 (traverseproc)module_traverse, /* tp_traverse */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000212 0, /* tp_clear */
213 0, /* tp_richcompare */
214 0, /* tp_weaklistoffset */
215 0, /* tp_iter */
216 0, /* tp_iternext */
217 0, /* tp_methods */
218 module_members, /* tp_members */
219 0, /* tp_getset */
220 0, /* tp_base */
221 0, /* tp_dict */
222 0, /* tp_descr_get */
223 0, /* tp_descr_set */
224 offsetof(PyModuleObject, md_dict), /* tp_dictoffset */
225 (initproc)module_init, /* tp_init */
226 PyType_GenericAlloc, /* tp_alloc */
227 PyType_GenericNew, /* tp_new */
Guido van Rossum9475a232001-10-05 20:51:39 +0000228 _PyObject_GC_Del, /* tp_free */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000229};