[Python-checkins] CVS: python/dist/src/Objects typeobject.c,2.34,2.35

Guido van Rossum gvanrossum@users.sourceforge.net
Thu, 16 Aug 2001 02:18:58 -0700


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

Modified Files:
	typeobject.c 
Log Message:
Fix SF bug #442501: calculate __module__ properly.

- type_module(), type_name(): if tp_name contains one or more period,
  the part before the last period is __module__, the part after that
  is __name__.  Otherwise, for non-heap types, __module__ is
  "__builtin__".  For heap types, __module__ is looked up in
  tp_defined.

- type_new(): heap types have their __module__ set from
  globals().__name__; a pre-existing __module__ in their dict is not
  overridden.  This is not inherited.

- type_repr(): if __module__ exists and is not "__builtin__", it is
  included in the string representation (just as it already is for
  classes).  For example <type '__main__.C'>.


Index: typeobject.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Objects/typeobject.c,v
retrieving revision 2.34
retrieving revision 2.35
diff -C2 -d -r2.34 -r2.35
*** typeobject.c	2001/08/16 08:27:33	2.34
--- typeobject.c	2001/08/16 09:18:56	2.35
***************
*** 6,10 ****
  
  static struct memberlist type_members[] = {
- 	{"__name__", T_STRING, offsetof(PyTypeObject, tp_name), READONLY},
  	{"__basicsize__", T_INT, offsetof(PyTypeObject,tp_basicsize),READONLY},
  	{"__itemsize__", T_INT, offsetof(PyTypeObject, tp_itemsize), READONLY},
--- 6,9 ----
***************
*** 22,28 ****
  
  static PyObject *
  type_module(PyTypeObject *type, void *context)
  {
! 	return PyString_FromString("__builtin__");
  }
  
--- 21,55 ----
  
  static PyObject *
+ type_name(PyTypeObject *type, void *context)
+ {
+ 	char *s;
+ 
+ 	s = strrchr(type->tp_name, '.');
+ 	if (s == NULL)
+ 		s = type->tp_name;
+ 	else
+ 		s++;
+ 	return PyString_FromString(s);
+ }
+ 
+ static PyObject *
  type_module(PyTypeObject *type, void *context)
  {
! 	PyObject *mod;
! 	char *s;
! 
! 	s = strrchr(type->tp_name, '.');
! 	if (s != NULL)
! 		return PyString_FromStringAndSize(type->tp_name,
! 						  (int)(s - type->tp_name));
! 	if (!(type->tp_flags & Py_TPFLAGS_HEAPTYPE))
! 		return PyString_FromString("__builtin__");
! 	mod = PyDict_GetItemString(type->tp_defined, "__module__");
! 	if (mod != NULL && PyString_Check(mod)) {
! 		Py_INCREF(mod);
! 		return mod;
! 	}
! 	PyErr_SetString(PyExc_AttributeError, "__module__");
! 	return NULL;
  }
  
***************
*** 66,69 ****
--- 93,97 ----
  
  struct getsetlist type_getsets[] = {
+ 	{"__name__", (getter)type_name, NULL, NULL},
  	{"__module__", (getter)type_module, NULL, NULL},
  	{"__dict__",  (getter)type_dict,  NULL, NULL},
***************
*** 86,91 ****
  type_repr(PyTypeObject *type)
  {
! 	char buf[100];
! 	sprintf(buf, "<type '%.80s'>", type->tp_name);
  	return PyString_FromString(buf);
  }
--- 114,138 ----
  type_repr(PyTypeObject *type)
  {
! 	PyObject *mod, *name;
! 	char buf[200];
! 
! 	mod = type_module(type, NULL);
! 	if (mod == NULL)
! 		PyErr_Clear();
! 	else if (!PyString_Check(mod)) {
! 		Py_DECREF(mod);
! 		mod = NULL;
! 	}
! 	name = type_name(type, NULL);
! 	if (name == NULL)
! 		return NULL;
! 	if (mod != NULL && strcmp(PyString_AS_STRING(mod), "__builtin__"))
! 		sprintf(buf, "<type '%.80s.%.80s'>",
! 			PyString_AS_STRING(mod),
! 			PyString_AS_STRING(name));
! 	else
! 		sprintf(buf, "<type '%.80s'>", type->tp_name);
! 	Py_XDECREF(mod);
! 	Py_DECREF(name);
  	return PyString_FromString(buf);
  }
***************
*** 612,615 ****
--- 659,675 ----
  	}
  
+ 	/* Set __module__ in the dict */
+ 	if (PyDict_GetItemString(dict, "__module__") == NULL) {
+ 		tmp = PyEval_GetGlobals();
+ 		if (tmp != NULL) {
+ 			tmp = PyDict_GetItemString(tmp, "__name__");
+ 			if (tmp != NULL) {
+ 				if (PyDict_SetItemString(dict, "__module__",
+ 							 tmp) < 0)
+ 					return NULL;
+ 			}
+ 		}
+ 	}
+ 
  	/* Special-case __new__: if it's a plain function,
  	   make it a static function */
***************
*** 2479,2484 ****
  	}
  	getattr = _PyType_Lookup(tp, getattr_str);
! 	if (getattr == NULL)
  		return PyObject_GenericGetAttr(self, name);
  	return PyObject_CallFunction(getattr, "OO", self, name);
  }
--- 2539,2550 ----
  	}
  	getattr = _PyType_Lookup(tp, getattr_str);
! 	if (getattr == NULL) {
! 		/* Avoid further slowdowns */
! 		if (tp->tp_getattro == slot_tp_getattro)
! 			tp->tp_getattro = PyObject_GenericGetAttr;
! 		else
! 			fprintf(stderr, "huh?\n");
  		return PyObject_GenericGetAttr(self, name);
+ 	}
  	return PyObject_CallFunction(getattr, "OO", self, name);
  }