blob: 1ba1a1b7056fc73826847cd4d2d61145a1938a7c [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001/***********************************************************
Guido van Rossum6610ad91995-01-04 19:07:38 +00002Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
3The Netherlands.
Guido van Rossumf70e43a1991-02-19 12:39:46 +00004
5 All Rights Reserved
6
Guido van Rossumd266eb41996-10-25 14:44:06 +00007Permission to use, copy, modify, and distribute this software and its
8documentation for any purpose and without fee is hereby granted,
Guido van Rossumf70e43a1991-02-19 12:39:46 +00009provided that the above copyright notice appear in all copies and that
Guido van Rossumd266eb41996-10-25 14:44:06 +000010both that copyright notice and this permission notice appear in
Guido van Rossumf70e43a1991-02-19 12:39:46 +000011supporting documentation, and that the names of Stichting Mathematisch
Guido van Rossumd266eb41996-10-25 14:44:06 +000012Centrum or CWI or Corporation for National Research Initiatives or
13CNRI not be used in advertising or publicity pertaining to
14distribution of the software without specific, written prior
15permission.
Guido van Rossumf70e43a1991-02-19 12:39:46 +000016
Guido van Rossumd266eb41996-10-25 14:44:06 +000017While CWI is the initial source for this software, a modified version
18is made available by the Corporation for National Research Initiatives
19(CNRI) at the Internet address ftp://ftp.python.org.
20
21STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH
22REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
23MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH
24CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
25DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
26PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
27TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
28PERFORMANCE OF THIS SOFTWARE.
Guido van Rossumf70e43a1991-02-19 12:39:46 +000029
30******************************************************************/
31
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000032/* Module object implementation */
33
Guido van Rossumc0b618a1997-05-02 03:12:38 +000034#include "Python.h"
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000035
36typedef struct {
Guido van Rossumc0b618a1997-05-02 03:12:38 +000037 PyObject_HEAD
38 PyObject *md_dict;
39} PyModuleObject;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000040
Guido van Rossumc0b618a1997-05-02 03:12:38 +000041PyObject *
42PyModule_New(name)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000043 char *name;
44{
Guido van Rossumc0b618a1997-05-02 03:12:38 +000045 PyModuleObject *m;
46 PyObject *nameobj;
47 m = PyObject_NEW(PyModuleObject, &PyModule_Type);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000048 if (m == NULL)
49 return NULL;
Guido van Rossumc0b618a1997-05-02 03:12:38 +000050 nameobj = PyString_FromString(name);
51 m->md_dict = PyDict_New();
Guido van Rossumc45611d1993-11-17 22:58:56 +000052 if (m->md_dict == NULL || nameobj == NULL)
53 goto fail;
Guido van Rossumc0b618a1997-05-02 03:12:38 +000054 if (PyDict_SetItemString(m->md_dict, "__name__", nameobj) != 0)
Guido van Rossumc45611d1993-11-17 22:58:56 +000055 goto fail;
Guido van Rossumc0b618a1997-05-02 03:12:38 +000056 if (PyDict_SetItemString(m->md_dict, "__doc__", Py_None) != 0)
Guido van Rossum8b14b4c1995-01-07 11:59:29 +000057 goto fail;
Guido van Rossumc0b618a1997-05-02 03:12:38 +000058 Py_DECREF(nameobj);
59 return (PyObject *)m;
Guido van Rossumc45611d1993-11-17 22:58:56 +000060
61 fail:
Guido van Rossumc0b618a1997-05-02 03:12:38 +000062 Py_XDECREF(nameobj);
63 Py_DECREF(m);
Guido van Rossumc45611d1993-11-17 22:58:56 +000064 return NULL;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000065}
66
Guido van Rossumc0b618a1997-05-02 03:12:38 +000067PyObject *
68PyModule_GetDict(m)
69 PyObject *m;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000070{
Guido van Rossumc0b618a1997-05-02 03:12:38 +000071 if (!PyModule_Check(m)) {
72 PyErr_BadInternalCall();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000073 return NULL;
74 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +000075 return ((PyModuleObject *)m) -> md_dict;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000076}
77
Guido van Rossum0558a201990-10-26 15:00:11 +000078char *
Guido van Rossumc0b618a1997-05-02 03:12:38 +000079PyModule_GetName(m)
80 PyObject *m;
Guido van Rossum0558a201990-10-26 15:00:11 +000081{
Guido van Rossumc0b618a1997-05-02 03:12:38 +000082 PyObject *nameobj;
83 if (!PyModule_Check(m)) {
84 PyErr_BadArgument();
Guido van Rossum0558a201990-10-26 15:00:11 +000085 return NULL;
86 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +000087 nameobj = PyDict_GetItemString(((PyModuleObject *)m)->md_dict,
88 "__name__");
89 if (nameobj == NULL || !PyString_Check(nameobj)) {
90 PyErr_SetString(PyExc_SystemError, "nameless module");
Guido van Rossumc45611d1993-11-17 22:58:56 +000091 return NULL;
92 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +000093 return PyString_AsString(nameobj);
Guido van Rossum0558a201990-10-26 15:00:11 +000094}
95
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000096/* Methods */
97
98static void
Guido van Rossum3f5da241990-12-20 15:06:42 +000099module_dealloc(m)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000100 PyModuleObject *m;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000101{
Guido van Rossum27e916f1995-01-26 00:39:00 +0000102 if (m->md_dict != NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000103 PyDict_Clear(m->md_dict);
104 Py_DECREF(m->md_dict);
Guido van Rossum27e916f1995-01-26 00:39:00 +0000105 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000106 free((char *)m);
107}
108
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000109static PyObject *
Guido van Rossum3f5da241990-12-20 15:06:42 +0000110module_repr(m)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000111 PyModuleObject *m;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000112{
113 char buf[100];
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000114 char *name = PyModule_GetName((PyObject *)m);
Guido van Rossumc45611d1993-11-17 22:58:56 +0000115 if (name == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000116 PyErr_Clear();
Guido van Rossumc45611d1993-11-17 22:58:56 +0000117 name = "?";
118 }
119 sprintf(buf, "<module '%.80s'>", name);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000120 return PyString_FromString(buf);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000121}
122
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000123static PyObject *
Guido van Rossum3f5da241990-12-20 15:06:42 +0000124module_getattr(m, name)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000125 PyModuleObject *m;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000126 char *name;
127{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000128 PyObject *res;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000129 if (strcmp(name, "__dict__") == 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000130 Py_INCREF(m->md_dict);
Guido van Rossume87203a1990-10-21 22:12:30 +0000131 return m->md_dict;
132 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000133 res = PyDict_GetItemString(m->md_dict, name);
Guido van Rossum2b654f71990-10-14 20:03:32 +0000134 if (res == NULL)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000135 PyErr_SetString(PyExc_AttributeError, name);
Guido van Rossum0add15f1997-05-09 01:07:15 +0000136 else
137 Py_INCREF(res);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000138 return res;
139}
140
141static int
Guido van Rossum3f5da241990-12-20 15:06:42 +0000142module_setattr(m, name, v)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000143 PyModuleObject *m;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000144 char *name;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000145 PyObject *v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000146{
Guido van Rossumc45611d1993-11-17 22:58:56 +0000147 if (name[0] == '_' && strcmp(name, "__dict__") == 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000148 PyErr_SetString(PyExc_TypeError,
149 "read-only special attribute");
Guido van Rossumc45611d1993-11-17 22:58:56 +0000150 return -1;
Guido van Rossume87203a1990-10-21 22:12:30 +0000151 }
Guido van Rossum94472a01992-09-04 09:45:18 +0000152 if (v == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000153 int rv = PyDict_DelItemString(m->md_dict, name);
Guido van Rossum94472a01992-09-04 09:45:18 +0000154 if (rv < 0)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000155 PyErr_SetString(PyExc_AttributeError,
Guido van Rossum94472a01992-09-04 09:45:18 +0000156 "delete non-existing module attribute");
157 return rv;
158 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000159 else
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000160 return PyDict_SetItemString(m->md_dict, name, v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000161}
162
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000163PyTypeObject PyModule_Type = {
164 PyObject_HEAD_INIT(&PyType_Type)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000165 0, /*ob_size*/
166 "module", /*tp_name*/
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000167 sizeof(PyModuleObject), /*tp_size*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000168 0, /*tp_itemsize*/
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000169 (destructor)module_dealloc, /*tp_dealloc*/
Guido van Rossum7066dd71992-09-17 17:54:56 +0000170 0, /*tp_print*/
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000171 (getattrfunc)module_getattr, /*tp_getattr*/
172 (setattrfunc)module_setattr, /*tp_setattr*/
Guido van Rossum3f5da241990-12-20 15:06:42 +0000173 0, /*tp_compare*/
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000174 (reprfunc)module_repr, /*tp_repr*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000175};