| 
									
										
										
										
											1991-02-19 12:39:46 +00:00
										 |  |  | /***********************************************************
 | 
					
						
							| 
									
										
										
										
											2000-06-30 23:50:40 +00:00
										 |  |  | Copyright (c) 2000, BeOpen.com. | 
					
						
							|  |  |  | Copyright (c) 1995-2000, Corporation for National Research Initiatives. | 
					
						
							|  |  |  | Copyright (c) 1990-1995, Stichting Mathematisch Centrum. | 
					
						
							|  |  |  | All rights reserved. | 
					
						
							| 
									
										
										
										
											1991-02-19 12:39:46 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2000-06-30 23:50:40 +00:00
										 |  |  | See the file "Misc/COPYRIGHT" for information on usage and | 
					
						
							|  |  |  | redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES. | 
					
						
							| 
									
										
										
										
											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"
 | 
					
						
							| 
									
										
										
										
											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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											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; | 
					
						
							|  |  |  | 	m = PyObject_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); | 
					
						
							|  |  |  | 	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
										 |  |  | { | 
					
						
							| 
									
										
										
										
											1997-05-02 03:12:38 +00:00
										 |  |  | 	if (!PyModule_Check(m)) { | 
					
						
							|  |  |  | 		PyErr_BadInternalCall(); | 
					
						
							| 
									
										
										
										
											1990-10-14 12:07:46 +00:00
										 |  |  | 		return NULL; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											1997-05-02 03:12:38 +00:00
										 |  |  | 	return ((PyModuleObject *)m) -> md_dict; | 
					
						
							| 
									
										
										
										
											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
										 |  |  | { | 
					
						
							| 
									
										
										
										
											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; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											1997-05-02 03:12:38 +00:00
										 |  |  | 	nameobj = PyDict_GetItemString(((PyModuleObject *)m)->md_dict, | 
					
						
							|  |  |  | 				       "__name__"); | 
					
						
							|  |  |  | 	if (nameobj == NULL || !PyString_Check(nameobj)) { | 
					
						
							|  |  |  | 		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
										 |  |  | { | 
					
						
							|  |  |  | 	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); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/* 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 */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static void | 
					
						
							| 
									
										
										
										
											2000-07-09 06:03:25 +00:00
										 |  |  | module_dealloc(PyModuleObject *m) | 
					
						
							| 
									
										
										
										
											1990-10-14 12:07:46 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											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
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2000-05-03 23:44:39 +00:00
										 |  |  | 	PyObject_DEL(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 buf[400]; | 
					
						
							|  |  |  | 	char *name; | 
					
						
							|  |  |  | 	char *filename; | 
					
						
							|  |  |  | 	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(); | 
					
						
							|  |  |  | 		sprintf(buf, "<module '%.80s' (built-in)>", name); | 
					
						
							|  |  |  | 	} else { | 
					
						
							|  |  |  | 		sprintf(buf, "<module '%.80s' from '%.255s'>", name, filename); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1997-05-02 03:12:38 +00:00
										 |  |  | 	return PyString_FromString(buf); | 
					
						
							| 
									
										
										
										
											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_getattr(PyModuleObject *m, char *name) | 
					
						
							| 
									
										
										
										
											1990-10-14 12:07:46 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											1997-05-02 03:12:38 +00:00
										 |  |  | 	PyObject *res; | 
					
						
							| 
									
										
										
										
											1990-12-20 15:06:42 +00:00
										 |  |  | 	if (strcmp(name, "__dict__") == 0) { | 
					
						
							| 
									
										
										
										
											1997-05-02 03:12:38 +00:00
										 |  |  | 		Py_INCREF(m->md_dict); | 
					
						
							| 
									
										
										
										
											1990-10-21 22:12:30 +00:00
										 |  |  | 		return m->md_dict; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											1997-05-02 03:12:38 +00:00
										 |  |  | 	res = PyDict_GetItemString(m->md_dict, name); | 
					
						
							| 
									
										
										
										
											1990-10-14 20:03:32 +00:00
										 |  |  | 	if (res == NULL) | 
					
						
							| 
									
										
										
										
											1997-05-02 03:12:38 +00:00
										 |  |  | 		PyErr_SetString(PyExc_AttributeError, name); | 
					
						
							| 
									
										
										
										
											1997-05-09 01:07:15 +00:00
										 |  |  | 	else | 
					
						
							|  |  |  | 		Py_INCREF(res); | 
					
						
							| 
									
										
										
										
											1990-10-14 12:07:46 +00:00
										 |  |  | 	return res; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static int | 
					
						
							| 
									
										
										
										
											2000-07-09 06:03:25 +00:00
										 |  |  | module_setattr(PyModuleObject *m, char *name, PyObject *v) | 
					
						
							| 
									
										
										
										
											1990-10-14 12:07:46 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											1993-11-17 22:58:56 +00:00
										 |  |  | 	if (name[0] == '_' && strcmp(name, "__dict__") == 0) { | 
					
						
							| 
									
										
										
										
											1997-05-02 03:12:38 +00:00
										 |  |  | 		PyErr_SetString(PyExc_TypeError, | 
					
						
							|  |  |  | 				"read-only special attribute"); | 
					
						
							| 
									
										
										
										
											1993-11-17 22:58:56 +00:00
										 |  |  | 		return -1; | 
					
						
							| 
									
										
										
										
											1990-10-21 22:12:30 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											1992-09-04 09:45:18 +00:00
										 |  |  | 	if (v == NULL) { | 
					
						
							| 
									
										
										
										
											1997-05-02 03:12:38 +00:00
										 |  |  | 		int rv = PyDict_DelItemString(m->md_dict, name); | 
					
						
							| 
									
										
										
										
											1992-09-04 09:45:18 +00:00
										 |  |  | 		if (rv < 0) | 
					
						
							| 
									
										
										
										
											1997-05-02 03:12:38 +00:00
										 |  |  | 			PyErr_SetString(PyExc_AttributeError, | 
					
						
							| 
									
										
										
										
											1992-09-04 09:45:18 +00:00
										 |  |  | 				   "delete non-existing module attribute"); | 
					
						
							|  |  |  | 		return rv; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											1990-10-14 12:07:46 +00:00
										 |  |  | 	else | 
					
						
							| 
									
										
										
										
											1997-05-02 03:12:38 +00:00
										 |  |  | 		return PyDict_SetItemString(m->md_dict, name, v); | 
					
						
							| 
									
										
										
										
											1990-10-14 12:07:46 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1997-05-02 03:12:38 +00:00
										 |  |  | PyTypeObject PyModule_Type = { | 
					
						
							|  |  |  | 	PyObject_HEAD_INIT(&PyType_Type) | 
					
						
							| 
									
										
										
										
											1990-10-14 12:07:46 +00:00
										 |  |  | 	0,			/*ob_size*/ | 
					
						
							|  |  |  | 	"module",		/*tp_name*/ | 
					
						
							| 
									
										
										
										
											1997-05-02 03:12:38 +00:00
										 |  |  | 	sizeof(PyModuleObject),	/*tp_size*/ | 
					
						
							| 
									
										
										
										
											1990-10-14 12:07:46 +00:00
										 |  |  | 	0,			/*tp_itemsize*/ | 
					
						
							| 
									
										
										
										
											1994-08-30 08:27:36 +00:00
										 |  |  | 	(destructor)module_dealloc, /*tp_dealloc*/ | 
					
						
							| 
									
										
										
										
											1992-09-17 17:54:56 +00:00
										 |  |  | 	0,			/*tp_print*/ | 
					
						
							| 
									
										
										
										
											1994-08-30 08:27:36 +00:00
										 |  |  | 	(getattrfunc)module_getattr, /*tp_getattr*/ | 
					
						
							|  |  |  | 	(setattrfunc)module_setattr, /*tp_setattr*/ | 
					
						
							| 
									
										
										
										
											1990-12-20 15:06:42 +00:00
										 |  |  | 	0,			/*tp_compare*/ | 
					
						
							| 
									
										
										
										
											1994-08-30 08:27:36 +00:00
										 |  |  | 	(reprfunc)module_repr, /*tp_repr*/ | 
					
						
							| 
									
										
										
										
											1990-10-14 12:07:46 +00:00
										 |  |  | }; |