1991-02-19 12:39:46 +00:00
|
|
|
|
1990-10-14 12:07:46 +00:00
|
|
|
/* Module object implementation */
|
|
|
|
|
1997-05-02 03:12:38 +00:00
|
|
|
#include "Python.h"
|
2001-08-02 04:15:00 +00:00
|
|
|
#include "structmember.h"
|
1990-10-14 12:07:46 +00:00
|
|
|
|
|
|
|
typedef struct {
|
1997-05-02 03:12:38 +00:00
|
|
|
PyObject_HEAD
|
|
|
|
PyObject *md_dict;
|
|
|
|
} PyModuleObject;
|
1990-10-14 12:07:46 +00:00
|
|
|
|
2001-10-21 22:28:58 +00:00
|
|
|
static PyMemberDef module_members[] = {
|
2001-08-02 04:15:00 +00:00
|
|
|
{"__dict__", T_OBJECT, offsetof(PyModuleObject, md_dict), READONLY},
|
|
|
|
{0}
|
|
|
|
};
|
|
|
|
|
1997-05-02 03:12:38 +00:00
|
|
|
PyObject *
|
2000-07-09 06:03:25 +00:00
|
|
|
PyModule_New(char *name)
|
1990-10-14 12:07:46 +00:00
|
|
|
{
|
1997-05-02 03:12:38 +00:00
|
|
|
PyModuleObject *m;
|
|
|
|
PyObject *nameobj;
|
2001-08-29 23:54:21 +00:00
|
|
|
m = PyObject_GC_New(PyModuleObject, &PyModule_Type);
|
1990-10-14 12:07:46 +00:00
|
|
|
if (m == NULL)
|
|
|
|
return NULL;
|
1997-05-02 03:12:38 +00:00
|
|
|
nameobj = PyString_FromString(name);
|
|
|
|
m->md_dict = PyDict_New();
|
1993-11-17 22:58:56 +00:00
|
|
|
if (m->md_dict == NULL || nameobj == NULL)
|
|
|
|
goto fail;
|
1997-05-02 03:12:38 +00:00
|
|
|
if (PyDict_SetItemString(m->md_dict, "__name__", nameobj) != 0)
|
1993-11-17 22:58:56 +00:00
|
|
|
goto fail;
|
1997-05-02 03:12:38 +00:00
|
|
|
if (PyDict_SetItemString(m->md_dict, "__doc__", Py_None) != 0)
|
1995-01-07 11:59:29 +00:00
|
|
|
goto fail;
|
1997-05-02 03:12:38 +00:00
|
|
|
Py_DECREF(nameobj);
|
2001-08-29 23:54:21 +00:00
|
|
|
PyObject_GC_Track(m);
|
1997-05-02 03:12:38 +00:00
|
|
|
return (PyObject *)m;
|
1993-11-17 22:58:56 +00:00
|
|
|
|
|
|
|
fail:
|
1997-05-02 03:12:38 +00:00
|
|
|
Py_XDECREF(nameobj);
|
|
|
|
Py_DECREF(m);
|
1993-11-17 22:58:56 +00:00
|
|
|
return NULL;
|
1990-10-14 12:07:46 +00:00
|
|
|
}
|
|
|
|
|
1997-05-02 03:12:38 +00:00
|
|
|
PyObject *
|
2000-07-09 06:03:25 +00:00
|
|
|
PyModule_GetDict(PyObject *m)
|
1990-10-14 12:07:46 +00:00
|
|
|
{
|
2002-03-12 20:37:02 +00:00
|
|
|
PyObject *d;
|
1997-05-02 03:12:38 +00:00
|
|
|
if (!PyModule_Check(m)) {
|
|
|
|
PyErr_BadInternalCall();
|
1990-10-14 12:07:46 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
2002-03-12 20:37:02 +00:00
|
|
|
d = ((PyModuleObject *)m) -> md_dict;
|
|
|
|
if (d == NULL)
|
|
|
|
((PyModuleObject *)m) -> md_dict = d = PyDict_New();
|
|
|
|
return d;
|
1990-10-14 12:07:46 +00:00
|
|
|
}
|
|
|
|
|
1990-10-26 15:00:11 +00:00
|
|
|
char *
|
2000-07-09 06:03:25 +00:00
|
|
|
PyModule_GetName(PyObject *m)
|
1990-10-26 15:00:11 +00:00
|
|
|
{
|
2002-03-12 20:37:02 +00:00
|
|
|
PyObject *d;
|
1997-05-02 03:12:38 +00:00
|
|
|
PyObject *nameobj;
|
|
|
|
if (!PyModule_Check(m)) {
|
|
|
|
PyErr_BadArgument();
|
1990-10-26 15:00:11 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
2002-03-12 20:37:02 +00:00
|
|
|
d = ((PyModuleObject *)m)->md_dict;
|
|
|
|
if (d == NULL ||
|
|
|
|
(nameobj = PyDict_GetItemString(d, "__name__")) == NULL ||
|
|
|
|
!PyString_Check(nameobj))
|
|
|
|
{
|
1997-05-02 03:12:38 +00:00
|
|
|
PyErr_SetString(PyExc_SystemError, "nameless module");
|
1993-11-17 22:58:56 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
1997-05-02 03:12:38 +00:00
|
|
|
return PyString_AsString(nameobj);
|
1990-10-26 15:00:11 +00:00
|
|
|
}
|
|
|
|
|
1999-02-15 14:47:16 +00:00
|
|
|
char *
|
2000-07-09 06:03:25 +00:00
|
|
|
PyModule_GetFilename(PyObject *m)
|
1999-02-15 14:47:16 +00:00
|
|
|
{
|
2002-03-12 20:37:02 +00:00
|
|
|
PyObject *d;
|
1999-02-15 14:47:16 +00:00
|
|
|
PyObject *fileobj;
|
|
|
|
if (!PyModule_Check(m)) {
|
|
|
|
PyErr_BadArgument();
|
|
|
|
return NULL;
|
|
|
|
}
|
2002-03-12 20:37:02 +00:00
|
|
|
d = ((PyModuleObject *)m)->md_dict;
|
|
|
|
if (d == NULL ||
|
|
|
|
(fileobj = PyDict_GetItemString(d, "__file__")) == NULL ||
|
|
|
|
!PyString_Check(fileobj))
|
|
|
|
{
|
1999-02-15 14:47:16 +00:00
|
|
|
PyErr_SetString(PyExc_SystemError, "module filename missing");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return PyString_AsString(fileobj);
|
|
|
|
}
|
|
|
|
|
1998-02-19 20:51:52 +00:00
|
|
|
void
|
2000-07-09 06:03:25 +00:00
|
|
|
_PyModule_Clear(PyObject *m)
|
1998-02-19 20:51:52 +00:00
|
|
|
{
|
|
|
|
/* 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;
|
2002-03-12 20:37:02 +00:00
|
|
|
if (d == NULL)
|
|
|
|
return;
|
1998-02-19 20:51:52 +00:00
|
|
|
|
|
|
|
/* 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)
|
1998-10-12 18:23:55 +00:00
|
|
|
PySys_WriteStderr("# clear[1] %s\n", s);
|
1998-02-19 20:51:52 +00:00
|
|
|
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)
|
1998-10-12 18:23:55 +00:00
|
|
|
PySys_WriteStderr("# clear[2] %s\n", s);
|
1998-02-19 20:51:52 +00:00
|
|
|
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 12:07:46 +00:00
|
|
|
/* Methods */
|
|
|
|
|
2001-08-02 04:15:00 +00:00
|
|
|
static int
|
|
|
|
module_init(PyModuleObject *m, PyObject *args, PyObject *kw)
|
|
|
|
{
|
|
|
|
m->md_dict = PyDict_New();
|
|
|
|
if (m->md_dict == NULL)
|
|
|
|
return -1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
1990-10-14 12:07:46 +00:00
|
|
|
static void
|
2000-07-09 06:03:25 +00:00
|
|
|
module_dealloc(PyModuleObject *m)
|
1990-10-14 12:07:46 +00:00
|
|
|
{
|
2001-08-29 23:54:21 +00:00
|
|
|
PyObject_GC_UnTrack(m);
|
1995-01-26 00:39:00 +00:00
|
|
|
if (m->md_dict != NULL) {
|
1998-02-19 20:51:52 +00:00
|
|
|
_PyModule_Clear((PyObject *)m);
|
1997-05-02 03:12:38 +00:00
|
|
|
Py_DECREF(m->md_dict);
|
1995-01-26 00:39:00 +00:00
|
|
|
}
|
2001-10-05 20:51:39 +00:00
|
|
|
m->ob_type->tp_free((PyObject *)m);
|
1990-10-14 12:07:46 +00:00
|
|
|
}
|
|
|
|
|
1997-05-02 03:12:38 +00:00
|
|
|
static PyObject *
|
2000-07-09 06:03:25 +00:00
|
|
|
module_repr(PyModuleObject *m)
|
1990-10-14 12:07:46 +00:00
|
|
|
{
|
1999-02-15 14:47:16 +00:00
|
|
|
char *name;
|
|
|
|
char *filename;
|
2001-08-16 20:39:24 +00:00
|
|
|
|
1999-02-15 14:47:16 +00:00
|
|
|
name = PyModule_GetName((PyObject *)m);
|
1993-11-17 22:58:56 +00:00
|
|
|
if (name == NULL) {
|
1997-05-02 03:12:38 +00:00
|
|
|
PyErr_Clear();
|
1993-11-17 22:58:56 +00:00
|
|
|
name = "?";
|
|
|
|
}
|
1999-02-15 14:47:16 +00:00
|
|
|
filename = PyModule_GetFilename((PyObject *)m);
|
|
|
|
if (filename == NULL) {
|
|
|
|
PyErr_Clear();
|
2001-08-24 18:34:26 +00:00
|
|
|
return PyString_FromFormat("<module '%s' (built-in)>", name);
|
2001-08-16 20:39:24 +00:00
|
|
|
}
|
2001-08-24 18:34:26 +00:00
|
|
|
return PyString_FromFormat("<module '%s' from '%s'>", name, filename);
|
1990-10-14 12:07:46 +00:00
|
|
|
}
|
|
|
|
|
2001-01-02 15:58:27 +00: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 03:12:38 +00:00
|
|
|
PyTypeObject PyModule_Type = {
|
|
|
|
PyObject_HEAD_INIT(&PyType_Type)
|
2001-05-11 21:51:48 +00:00
|
|
|
0, /* ob_size */
|
|
|
|
"module", /* tp_name */
|
2001-08-29 23:54:21 +00:00
|
|
|
sizeof(PyModuleObject), /* tp_size */
|
2001-05-11 21:51:48 +00:00
|
|
|
0, /* tp_itemsize */
|
2001-08-02 04:15:00 +00:00
|
|
|
(destructor)module_dealloc, /* tp_dealloc */
|
2001-05-11 21:51:48 +00:00
|
|
|
0, /* tp_print */
|
2001-08-02 04:15:00 +00:00
|
|
|
0, /* tp_getattr */
|
|
|
|
0, /* tp_setattr */
|
2001-05-11 21:51:48 +00:00
|
|
|
0, /* tp_compare */
|
2001-08-02 04:15:00 +00:00
|
|
|
(reprfunc)module_repr, /* tp_repr */
|
2001-05-11 21:51:48 +00:00
|
|
|
0, /* tp_as_number */
|
|
|
|
0, /* tp_as_sequence */
|
|
|
|
0, /* tp_as_mapping */
|
|
|
|
0, /* tp_hash */
|
|
|
|
0, /* tp_call */
|
|
|
|
0, /* tp_str */
|
2001-08-02 04:15:00 +00:00
|
|
|
PyObject_GenericGetAttr, /* tp_getattro */
|
|
|
|
PyObject_GenericSetAttr, /* tp_setattro */
|
2001-05-11 21:51:48 +00:00
|
|
|
0, /* tp_as_buffer */
|
2001-08-29 23:54:21 +00:00
|
|
|
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
|
2001-08-02 04:15:00 +00:00
|
|
|
Py_TPFLAGS_BASETYPE, /* tp_flags */
|
2001-05-11 21:51:48 +00:00
|
|
|
0, /* tp_doc */
|
|
|
|
(traverseproc)module_traverse, /* tp_traverse */
|
2001-08-02 04:15:00 +00:00
|
|
|
0, /* tp_clear */
|
|
|
|
0, /* tp_richcompare */
|
|
|
|
0, /* tp_weaklistoffset */
|
|
|
|
0, /* tp_iter */
|
|
|
|
0, /* tp_iternext */
|
|
|
|
0, /* tp_methods */
|
|
|
|
module_members, /* tp_members */
|
|
|
|
0, /* tp_getset */
|
|
|
|
0, /* tp_base */
|
|
|
|
0, /* tp_dict */
|
|
|
|
0, /* tp_descr_get */
|
|
|
|
0, /* tp_descr_set */
|
|
|
|
offsetof(PyModuleObject, md_dict), /* tp_dictoffset */
|
|
|
|
(initproc)module_init, /* tp_init */
|
|
|
|
PyType_GenericAlloc, /* tp_alloc */
|
|
|
|
PyType_GenericNew, /* tp_new */
|
2001-10-05 20:51:39 +00:00
|
|
|
_PyObject_GC_Del, /* tp_free */
|
1990-10-14 12:07:46 +00:00
|
|
|
};
|