cpython/Objects/moduleobject.c

253 lines
5.9 KiB
C
Raw Normal View History

1991-02-19 08:39:46 -04:00
1990-10-14 09:07:46 -03:00
/* Module object implementation */
1997-05-02 00:12:38 -03:00
#include "Python.h"
1990-10-14 09:07:46 -03:00
typedef struct {
1997-05-02 00:12:38 -03:00
PyObject_HEAD
PyObject *md_dict;
} PyModuleObject;
1990-10-14 09:07:46 -03:00
1997-05-02 00:12:38 -03:00
PyObject *
2000-07-09 03:03:25 -03:00
PyModule_New(char *name)
1990-10-14 09:07:46 -03:00
{
1997-05-02 00:12:38 -03:00
PyModuleObject *m;
PyObject *nameobj;
m = PyObject_NEW(PyModuleObject, &PyModule_Type);
1990-10-14 09:07:46 -03:00
if (m == NULL)
return NULL;
1997-05-02 00:12:38 -03:00
nameobj = PyString_FromString(name);
m->md_dict = PyDict_New();
PyObject_GC_Init(m);
if (m->md_dict == NULL || nameobj == NULL)
goto fail;
1997-05-02 00:12:38 -03:00
if (PyDict_SetItemString(m->md_dict, "__name__", nameobj) != 0)
goto fail;
1997-05-02 00:12:38 -03:00
if (PyDict_SetItemString(m->md_dict, "__doc__", Py_None) != 0)
1995-01-07 07:59:29 -04:00
goto fail;
1997-05-02 00:12:38 -03:00
Py_DECREF(nameobj);
return (PyObject *)m;
fail:
1997-05-02 00:12:38 -03:00
Py_XDECREF(nameobj);
Py_DECREF(m);
return NULL;
1990-10-14 09:07:46 -03:00
}
1997-05-02 00:12:38 -03:00
PyObject *
2000-07-09 03:03:25 -03:00
PyModule_GetDict(PyObject *m)
1990-10-14 09:07:46 -03:00
{
1997-05-02 00:12:38 -03:00
if (!PyModule_Check(m)) {
PyErr_BadInternalCall();
1990-10-14 09:07:46 -03:00
return NULL;
}
1997-05-02 00:12:38 -03:00
return ((PyModuleObject *)m) -> md_dict;
1990-10-14 09:07:46 -03:00
}
char *
2000-07-09 03:03:25 -03:00
PyModule_GetName(PyObject *m)
{
1997-05-02 00:12:38 -03:00
PyObject *nameobj;
if (!PyModule_Check(m)) {
PyErr_BadArgument();
return NULL;
}
1997-05-02 00:12:38 -03:00
nameobj = PyDict_GetItemString(((PyModuleObject *)m)->md_dict,
"__name__");
if (nameobj == NULL || !PyString_Check(nameobj)) {
PyErr_SetString(PyExc_SystemError, "nameless module");
return NULL;
}
1997-05-02 00:12:38 -03:00
return PyString_AsString(nameobj);
}
char *
2000-07-09 03:03:25 -03:00
PyModule_GetFilename(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);
}
void
2000-07-09 03:03:25 -03:00
_PyModule_Clear(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)
PySys_WriteStderr("# clear[1] %s\n", s);
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)
PySys_WriteStderr("# clear[2] %s\n", s);
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'. */
}
1990-10-14 09:07:46 -03:00
/* Methods */
static void
2000-07-09 03:03:25 -03:00
module_dealloc(PyModuleObject *m)
1990-10-14 09:07:46 -03:00
{
PyObject_GC_Fini(m);
if (m->md_dict != NULL) {
_PyModule_Clear((PyObject *)m);
1997-05-02 00:12:38 -03:00
Py_DECREF(m->md_dict);
}
PyObject_DEL(PyObject_AS_GC(m));
1990-10-14 09:07:46 -03:00
}
1997-05-02 00:12:38 -03:00
static PyObject *
2000-07-09 03:03:25 -03:00
module_repr(PyModuleObject *m)
1990-10-14 09:07:46 -03:00
{
char buf[400];
char *name;
char *filename;
name = PyModule_GetName((PyObject *)m);
if (name == NULL) {
1997-05-02 00:12:38 -03:00
PyErr_Clear();
name = "?";
}
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);
}
1997-05-02 00:12:38 -03:00
return PyString_FromString(buf);
1990-10-14 09:07:46 -03:00
}
1997-05-02 00:12:38 -03:00
static PyObject *
module_getattro(PyModuleObject *m, PyObject *name)
1990-10-14 09:07:46 -03:00
{
1997-05-02 00:12:38 -03:00
PyObject *res;
char *sname = PyString_AsString(name);
if (sname[0] == '_' && strcmp(sname, "__dict__") == 0) {
1997-05-02 00:12:38 -03:00
Py_INCREF(m->md_dict);
return m->md_dict;
}
res = PyDict_GetItem(m->md_dict, name);
if (res == NULL) {
char *modname = PyModule_GetName((PyObject *)m);
if (modname == NULL) {
PyErr_Clear();
modname = "?";
}
PyErr_Format(PyExc_AttributeError,
"'%.50s' module has no attribute '%.400s'",
modname, sname);
}
else
Py_INCREF(res);
1990-10-14 09:07:46 -03:00
return res;
}
static int
module_setattro(PyModuleObject *m, PyObject *name, PyObject *v)
1990-10-14 09:07:46 -03:00
{
char *sname = PyString_AsString(name);
if (sname[0] == '_' && strcmp(sname, "__dict__") == 0) {
1997-05-02 00:12:38 -03:00
PyErr_SetString(PyExc_TypeError,
"read-only special attribute");
return -1;
}
if (v == NULL) {
int rv = PyDict_DelItem(m->md_dict, name);
if (rv < 0) {
char *modname = PyModule_GetName((PyObject *)m);
if (modname == NULL) {
PyErr_Clear();
modname = "?";
}
PyErr_Format(PyExc_AttributeError,
"'%.50s' module has no attribute '%.400s'",
modname, sname);
}
return rv;
}
1990-10-14 09:07:46 -03:00
else
return PyDict_SetItem(m->md_dict, name, v);
1990-10-14 09:07:46 -03:00
}
/* We only need a traverse function, no clear function: If the module
is in a cycle, md_dict will be cleared as well, which will break
the cycle. */
static int
module_traverse(PyModuleObject *m, visitproc visit, void *arg)
{
if (m->md_dict != NULL)
return visit(m->md_dict, arg);
return 0;
}
1997-05-02 00:12:38 -03:00
PyTypeObject PyModule_Type = {
PyObject_HEAD_INIT(&PyType_Type)
0, /* ob_size */
"module", /* tp_name */
sizeof(PyModuleObject) + PyGC_HEAD_SIZE,/* tp_size */
0, /* tp_itemsize */
(destructor)module_dealloc, /* tp_dealloc */
0, /* tp_print */
0, /* tp_getattr */
0, /* tp_setattr */
0, /* tp_compare */
(reprfunc)module_repr, /* tp_repr */
0, /* tp_as_number */
0, /* tp_as_sequence */
0, /* tp_as_mapping */
0, /* tp_hash */
0, /* tp_call */
0, /* tp_str */
(getattrofunc)module_getattro, /* tp_getattro */
(setattrofunc)module_setattro, /* tp_setattro */
0, /* tp_as_buffer */
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_GC, /* tp_flags */
0, /* tp_doc */
(traverseproc)module_traverse, /* tp_traverse */
1990-10-14 09:07:46 -03:00
};