moduleobject.c 6.21 KB
Newer Older
1
/***********************************************************
2 3
Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
The Netherlands.
4 5 6

                        All Rights Reserved

7 8
Permission to use, copy, modify, and distribute this software and its
documentation for any purpose and without fee is hereby granted,
9
provided that the above copyright notice appear in all copies and that
10
both that copyright notice and this permission notice appear in
11
supporting documentation, and that the names of Stichting Mathematisch
12 13 14 15
Centrum or CWI or Corporation for National Research Initiatives or
CNRI not be used in advertising or publicity pertaining to
distribution of the software without specific, written prior
permission.
16

17 18 19 20 21 22 23 24 25 26 27 28
While CWI is the initial source for this software, a modified version
is made available by the Corporation for National Research Initiatives
(CNRI) at the Internet address ftp://ftp.python.org.

STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH
REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH
CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
29 30 31

******************************************************************/

Guido van Rossum's avatar
Guido van Rossum committed
32 33
/* Module object implementation */

34
#include "Python.h"
Guido van Rossum's avatar
Guido van Rossum committed
35 36

typedef struct {
37 38 39
	PyObject_HEAD
	PyObject *md_dict;
} PyModuleObject;
Guido van Rossum's avatar
Guido van Rossum committed
40

41 42
PyObject *
PyModule_New(name)
Guido van Rossum's avatar
Guido van Rossum committed
43 44
	char *name;
{
45 46 47
	PyModuleObject *m;
	PyObject *nameobj;
	m = PyObject_NEW(PyModuleObject, &PyModule_Type);
Guido van Rossum's avatar
Guido van Rossum committed
48 49
	if (m == NULL)
		return NULL;
50 51
	nameobj = PyString_FromString(name);
	m->md_dict = PyDict_New();
52 53
	if (m->md_dict == NULL || nameobj == NULL)
		goto fail;
54
	if (PyDict_SetItemString(m->md_dict, "__name__", nameobj) != 0)
55
		goto fail;
56
	if (PyDict_SetItemString(m->md_dict, "__doc__", Py_None) != 0)
57
		goto fail;
58 59
	Py_DECREF(nameobj);
	return (PyObject *)m;
60 61

 fail:
62 63
	Py_XDECREF(nameobj);
	Py_DECREF(m);
64
	return NULL;
Guido van Rossum's avatar
Guido van Rossum committed
65 66
}

67 68 69
PyObject *
PyModule_GetDict(m)
	PyObject *m;
Guido van Rossum's avatar
Guido van Rossum committed
70
{
71 72
	if (!PyModule_Check(m)) {
		PyErr_BadInternalCall();
Guido van Rossum's avatar
Guido van Rossum committed
73 74
		return NULL;
	}
75
	return ((PyModuleObject *)m) -> md_dict;
Guido van Rossum's avatar
Guido van Rossum committed
76 77
}

78
char *
79 80
PyModule_GetName(m)
	PyObject *m;
81
{
82 83 84
	PyObject *nameobj;
	if (!PyModule_Check(m)) {
		PyErr_BadArgument();
85 86
		return NULL;
	}
87 88 89 90
	nameobj = PyDict_GetItemString(((PyModuleObject *)m)->md_dict,
				       "__name__");
	if (nameobj == NULL || !PyString_Check(nameobj)) {
		PyErr_SetString(PyExc_SystemError, "nameless module");
91 92
		return NULL;
	}
93
	return PyString_AsString(nameobj);
94 95
}

96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
char *
PyModule_GetFilename(m)
        PyObject *m;
{
	PyObject *fileobj;
	if (!PyModule_Check(m)) {
		PyErr_BadArgument();
		return NULL;
	}
	fileobj = PyDict_GetItemString(((PyModuleObject *)m)->md_dict,
				       "__file__");
	if (fileobj == NULL || !PyString_Check(fileobj)) {
		PyErr_SetString(PyExc_SystemError, "module filename missing");
		return NULL;
	}
	return PyString_AsString(fileobj);
}

114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
void
_PyModule_Clear(m)
	PyObject *m;
{
	/* To make the execution order of destructors for global
	   objects a bit more predictable, we first zap all objects
	   whose name starts with a single underscore, before we clear
	   the entire dictionary.  We zap them by replacing them with
	   None, rather than deleting them from the dictionary, to
	   avoid rehashing the dictionary (to some extent). */

	int pos;
	PyObject *key, *value;
	PyObject *d;

	d = ((PyModuleObject *)m)->md_dict;

	/* First, clear only names starting with a single underscore */
	pos = 0;
	while (PyDict_Next(d, &pos, &key, &value)) {
		if (value != Py_None && PyString_Check(key)) {
			char *s = PyString_AsString(key);
			if (s[0] == '_' && s[1] != '_') {
				if (Py_VerboseFlag > 1)
138
				    PySys_WriteStderr("#   clear[1] %s\n", s);
139 140 141 142 143 144 145 146 147 148 149 150
				PyDict_SetItem(d, key, Py_None);
			}
		}
	}

	/* Next, clear all names except for __builtins__ */
	pos = 0;
	while (PyDict_Next(d, &pos, &key, &value)) {
		if (value != Py_None && PyString_Check(key)) {
			char *s = PyString_AsString(key);
			if (s[0] != '_' || strcmp(s, "__builtins__") != 0) {
				if (Py_VerboseFlag > 1)
151
				    PySys_WriteStderr("#   clear[2] %s\n", s);
152 153 154 155 156 157 158 159 160 161 162
				PyDict_SetItem(d, key, Py_None);
			}
		}
	}

	/* Note: we leave __builtins__ in place, so that destructors
	   of non-global objects defined in this module can still use
	   builtins, in particularly 'None'. */

}

Guido van Rossum's avatar
Guido van Rossum committed
163 164 165
/* Methods */

static void
Guido van Rossum's avatar
Guido van Rossum committed
166
module_dealloc(m)
167
	PyModuleObject *m;
Guido van Rossum's avatar
Guido van Rossum committed
168
{
169
	if (m->md_dict != NULL) {
170
		_PyModule_Clear((PyObject *)m);
171
		Py_DECREF(m->md_dict);
172
	}
Guido van Rossum's avatar
Guido van Rossum committed
173 174 175
	free((char *)m);
}

176
static PyObject *
Guido van Rossum's avatar
Guido van Rossum committed
177
module_repr(m)
178
	PyModuleObject *m;
Guido van Rossum's avatar
Guido van Rossum committed
179
{
180 181 182 183
	char buf[400];
	char *name;
	char *filename;
	name = PyModule_GetName((PyObject *)m);
184
	if (name == NULL) {
185
		PyErr_Clear();
186 187
		name = "?";
	}
188 189 190 191 192 193 194 195
	filename = PyModule_GetFilename((PyObject *)m);
	if (filename == NULL) {
		PyErr_Clear();
		sprintf(buf, "<module '%.80s' (built-in)>", name);
	} else {
		sprintf(buf, "<module '%.80s' from '%.255s'>", name, filename);
	}

196
	return PyString_FromString(buf);
Guido van Rossum's avatar
Guido van Rossum committed
197 198
}

199
static PyObject *
Guido van Rossum's avatar
Guido van Rossum committed
200
module_getattr(m, name)
201
	PyModuleObject *m;
Guido van Rossum's avatar
Guido van Rossum committed
202 203
	char *name;
{
204
	PyObject *res;
Guido van Rossum's avatar
Guido van Rossum committed
205
	if (strcmp(name, "__dict__") == 0) {
206
		Py_INCREF(m->md_dict);
207 208
		return m->md_dict;
	}
209
	res = PyDict_GetItemString(m->md_dict, name);
210
	if (res == NULL)
211
		PyErr_SetString(PyExc_AttributeError, name);
212 213
	else
		Py_INCREF(res);
Guido van Rossum's avatar
Guido van Rossum committed
214 215 216 217
	return res;
}

static int
Guido van Rossum's avatar
Guido van Rossum committed
218
module_setattr(m, name, v)
219
	PyModuleObject *m;
Guido van Rossum's avatar
Guido van Rossum committed
220
	char *name;
221
	PyObject *v;
Guido van Rossum's avatar
Guido van Rossum committed
222
{
223
	if (name[0] == '_' && strcmp(name, "__dict__") == 0) {
224 225
		PyErr_SetString(PyExc_TypeError,
				"read-only special attribute");
226
		return -1;
227
	}
228
	if (v == NULL) {
229
		int rv = PyDict_DelItemString(m->md_dict, name);
230
		if (rv < 0)
231
			PyErr_SetString(PyExc_AttributeError,
232 233 234
				   "delete non-existing module attribute");
		return rv;
	}
Guido van Rossum's avatar
Guido van Rossum committed
235
	else
236
		return PyDict_SetItemString(m->md_dict, name, v);
Guido van Rossum's avatar
Guido van Rossum committed
237 238
}

239 240
PyTypeObject PyModule_Type = {
	PyObject_HEAD_INIT(&PyType_Type)
Guido van Rossum's avatar
Guido van Rossum committed
241 242
	0,			/*ob_size*/
	"module",		/*tp_name*/
243
	sizeof(PyModuleObject),	/*tp_size*/
Guido van Rossum's avatar
Guido van Rossum committed
244
	0,			/*tp_itemsize*/
245
	(destructor)module_dealloc, /*tp_dealloc*/
246
	0,			/*tp_print*/
247 248
	(getattrfunc)module_getattr, /*tp_getattr*/
	(setattrfunc)module_setattr, /*tp_setattr*/
Guido van Rossum's avatar
Guido van Rossum committed
249
	0,			/*tp_compare*/
250
	(reprfunc)module_repr, /*tp_repr*/
Guido van Rossum's avatar
Guido van Rossum committed
251
};