[Python-checkins] CVS: python/dist/src/Objects moduleobject.c,2.31,2.32

Tim Peters tim_one@users.sourceforge.net
Fri, 11 May 2001 14:51:50 -0700


Update of /cvsroot/python/python/dist/src/Objects
In directory usw-pr-cvs1:/tmp/cvs-serv21481/python/dist/src/Objects

Modified Files:
	moduleobject.c 
Log Message:
Variant of patch #423262:  Change module attribute get & set
Allow module getattr and setattr to exploit string interning, via the
previously null module object tp_getattro and tp_setattro slots.   Yields
a very nice speedup for things like random.random and os.path etc.


Index: moduleobject.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Objects/moduleobject.c,v
retrieving revision 2.31
retrieving revision 2.32
diff -C2 -r2.31 -r2.32
*** moduleobject.c	2001/01/02 15:58:27	2.31
--- moduleobject.c	2001/05/11 21:51:48	2.32
***************
*** 163,177 ****
  
  static PyObject *
! module_getattr(PyModuleObject *m, char *name)
  {
  	PyObject *res;
! 	char* modname;
! 	if (strcmp(name, "__dict__") == 0) {
  		Py_INCREF(m->md_dict);
  		return m->md_dict;
  	}
! 	res = PyDict_GetItemString(m->md_dict, name);
  	if (res == NULL) {
! 		modname = PyModule_GetName((PyObject *)m);
  		if (modname == NULL) {
  			PyErr_Clear();
--- 163,178 ----
  
  static PyObject *
! module_getattro(PyModuleObject *m, PyObject *name)
  {
  	PyObject *res;
! 	char *sname = PyString_AsString(name);
! 
! 	if (sname[0] == '_' && strcmp(sname, "__dict__") == 0) {
  		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();
***************
*** 188,195 ****
  
  static int
! module_setattr(PyModuleObject *m, char *name, PyObject *v)
  {
! 	char* modname;
! 	if (name[0] == '_' && strcmp(name, "__dict__") == 0) {
  		PyErr_SetString(PyExc_TypeError,
  				"read-only special attribute");
--- 189,196 ----
  
  static int
! module_setattro(PyModuleObject *m, PyObject *name, PyObject *v)
  {
! 	char *sname = PyString_AsString(name);
! 	if (sname[0] == '_' && strcmp(sname, "__dict__") == 0) {
  		PyErr_SetString(PyExc_TypeError,
  				"read-only special attribute");
***************
*** 197,203 ****
  	}
  	if (v == NULL) {
! 		int rv = PyDict_DelItemString(m->md_dict, name);
  		if (rv < 0) {
! 			modname = PyModule_GetName((PyObject *)m);
  			if (modname == NULL) {
  				PyErr_Clear();
--- 198,204 ----
  	}
  	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();
***************
*** 206,215 ****
  			PyErr_Format(PyExc_AttributeError,
  				     "'%.50s' module has no attribute '%.400s'",
! 				     modname, name);
  		}
  		return rv;
  	}
  	else
! 		return PyDict_SetItemString(m->md_dict, name, v);
  }
  
--- 207,216 ----
  			PyErr_Format(PyExc_AttributeError,
  				     "'%.50s' module has no attribute '%.400s'",
! 				     modname, sname);
  		}
  		return rv;
  	}
  	else
! 		return PyDict_SetItem(m->md_dict, name, v);
  }
  
***************
*** 227,251 ****
  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*/
! 	(getattrfunc)module_getattr, /*tp_getattr*/
! 	(setattrfunc)module_setattr, /*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 */
! 	0,		/* tp_getattro */
! 	0,		/* tp_setattro */
! 	0,		/* tp_as_buffer */
! 	Py_TPFLAGS_DEFAULT | Py_TPFLAGS_GC, /*tp_flags*/
! 	0,		/* tp_doc */
! 	(traverseproc)module_traverse,	/* tp_traverse */
  };
--- 228,252 ----
  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 */
  };