[Python-checkins] CVS: python/dist/src/Objects object.c,2.161,2.162

Guido van Rossum gvanrossum@users.sourceforge.net
2001年12月04日 07:54:55 -0800


Update of /cvsroot/python/python/dist/src/Objects
In directory usw-pr-cvs1:/tmp/cvs-serv9857
Modified Files:
	object.c 
Log Message:
PyObject_Generic{Get,Set}Attr(): ensure that the attribute name is a
string object (or a Unicode that's trivially converted to ASCII).
PyObject_GetAttr(): add an 'else' to the Unicode test like
PyObject_SetAttr() already has.
Index: object.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Objects/object.c,v
retrieving revision 2.161
retrieving revision 2.162
diff -C2 -d -r2.161 -r2.162
*** object.c	2001年11月04日 07:29:31	2.161
--- object.c	2001年12月04日 15:54:53	2.162
***************
*** 1095,1100 ****
 			return NULL;
 	}
 #endif
- 
 	if (!PyString_Check(name)) {
 		PyErr_SetString(PyExc_TypeError,
--- 1095,1100 ----
 			return NULL;
 	}
+ 	else
 #endif
 	if (!PyString_Check(name)) {
 		PyErr_SetString(PyExc_TypeError,
***************
*** 1208,1217 ****
 	PyTypeObject *tp = obj->ob_type;
 	PyObject *descr;
 	descrgetfunc f;
 	PyObject **dictptr;
 
 	if (tp->tp_dict == NULL) {
 		if (PyType_Ready(tp) < 0)
! 			return NULL;
 	}
 
--- 1208,1237 ----
 	PyTypeObject *tp = obj->ob_type;
 	PyObject *descr;
+ 	PyObject *res = NULL;
 	descrgetfunc f;
 	PyObject **dictptr;
 
+ #ifdef Py_USING_UNICODE
+ 	/* The Unicode to string conversion is done here because the
+ 	 existing tp_setattro slots expect a string object as name
+ 	 and we wouldn't want to break those. */
+ 	if (PyUnicode_Check(name)) {
+ 		name = PyUnicode_AsEncodedString(name, NULL, NULL);
+ 		if (name == NULL)
+ 			return NULL;
+ 	}
+ 	else 
+ #endif
+ 	if (!PyString_Check(name)){
+ 		PyErr_SetString(PyExc_TypeError,
+ 				"attribute name must be string");
+ 		return NULL;
+ 	}
+ 	else
+ 		Py_INCREF(name);
+ 
 	if (tp->tp_dict == NULL) {
 		if (PyType_Ready(tp) < 0)
! 			goto done;
 	}
 
***************
*** 1220,1225 ****
 	if (descr != NULL) {
 		f = descr->ob_type->tp_descr_get;
! 		if (f != NULL && PyDescr_IsData(descr))
! 			return f(descr, obj, (PyObject *)obj->ob_type);
 	}
 
--- 1240,1247 ----
 	if (descr != NULL) {
 		f = descr->ob_type->tp_descr_get;
! 		if (f != NULL && PyDescr_IsData(descr)) {
! 			res = f(descr, obj, (PyObject *)obj->ob_type);
! 			goto done;
! 		}
 	}
 
***************
*** 1228,1245 ****
 		PyObject *dict = *dictptr;
 		if (dict != NULL) {
! 			PyObject *res = PyDict_GetItem(dict, name);
 			if (res != NULL) {
 				Py_INCREF(res);
! 				return res;
 			}
 		}
 	}
 
! 	if (f != NULL)
! 		return f(descr, obj, (PyObject *)obj->ob_type);
 
 	if (descr != NULL) {
 		Py_INCREF(descr);
! 		return descr;
 	}
 
--- 1250,1270 ----
 		PyObject *dict = *dictptr;
 		if (dict != NULL) {
! 			res = PyDict_GetItem(dict, name);
 			if (res != NULL) {
 				Py_INCREF(res);
! 				goto done;
 			}
 		}
 	}
 
! 	if (f != NULL) {
! 		res = f(descr, obj, (PyObject *)obj->ob_type);
! 		goto done;
! 	}
 
 	if (descr != NULL) {
 		Py_INCREF(descr);
! 		res = descr;
! 		goto done;
 	}
 
***************
*** 1247,1251 ****
 		 "'%.50s' object has no attribute '%.400s'",
 		 tp->tp_name, PyString_AS_STRING(name));
! 	return NULL;
 }
 
--- 1272,1278 ----
 		 "'%.50s' object has no attribute '%.400s'",
 		 tp->tp_name, PyString_AS_STRING(name));
! done:
! 	Py_DECREF(name);
! 	return res;
 }
 
***************
*** 1257,1264 ****
 	descrsetfunc f;
 	PyObject **dictptr;
 
 	if (tp->tp_dict == NULL) {
 		if (PyType_Ready(tp) < 0)
! 			return -1;
 	}
 
--- 1284,1311 ----
 	descrsetfunc f;
 	PyObject **dictptr;
+ 	int res = -1;
 
+ #ifdef Py_USING_UNICODE
+ 	/* The Unicode to string conversion is done here because the
+ 	 existing tp_setattro slots expect a string object as name
+ 	 and we wouldn't want to break those. */
+ 	if (PyUnicode_Check(name)) {
+ 		name = PyUnicode_AsEncodedString(name, NULL, NULL);
+ 		if (name == NULL)
+ 			return -1;
+ 	}
+ 	else 
+ #endif
+ 	if (!PyString_Check(name)){
+ 		PyErr_SetString(PyExc_TypeError,
+ 				"attribute name must be string");
+ 		return -1;
+ 	}
+ 	else
+ 		Py_INCREF(name);
+ 
 	if (tp->tp_dict == NULL) {
 		if (PyType_Ready(tp) < 0)
! 			goto done;
 	}
 
***************
*** 1267,1272 ****
 	if (descr != NULL) {
 		f = descr->ob_type->tp_descr_set;
! 		if (f != NULL && PyDescr_IsData(descr))
! 			return f(descr, obj, value);
 	}
 
--- 1314,1321 ----
 	if (descr != NULL) {
 		f = descr->ob_type->tp_descr_set;
! 		if (f != NULL && PyDescr_IsData(descr)) {
! 			res = f(descr, obj, value);
! 			goto done;
! 		}
 	}
 
***************
*** 1277,1285 ****
 			dict = PyDict_New();
 			if (dict == NULL)
! 				return -1;
 			*dictptr = dict;
 		}
 		if (dict != NULL) {
- 			int res;
 			if (value == NULL)
 				res = PyDict_DelItem(dict, name);
--- 1326,1333 ----
 			dict = PyDict_New();
 			if (dict == NULL)
! 				goto done;
 			*dictptr = dict;
 		}
 		if (dict != NULL) {
 			if (value == NULL)
 				res = PyDict_DelItem(dict, name);
***************
*** 1288,1297 ****
 			if (res < 0 && PyErr_ExceptionMatches(PyExc_KeyError))
 				PyErr_SetObject(PyExc_AttributeError, name);
! 			return res;
 		}
 	}
 
! 	if (f != NULL)
! 		return f(descr, obj, value);
 
 	if (descr == NULL) {
--- 1336,1347 ----
 			if (res < 0 && PyErr_ExceptionMatches(PyExc_KeyError))
 				PyErr_SetObject(PyExc_AttributeError, name);
! 			goto done;
 		}
 	}
 
! 	if (f != NULL) {
! 		res = f(descr, obj, value);
! 		goto done;
! 	}
 
 	if (descr == NULL) {
***************
*** 1299,1303 ****
 			 "'%.50s' object has no attribute '%.400s'",
 			 tp->tp_name, PyString_AS_STRING(name));
! 		return -1;
 	}
 
--- 1349,1353 ----
 			 "'%.50s' object has no attribute '%.400s'",
 			 tp->tp_name, PyString_AS_STRING(name));
! 		goto done;
 	}
 
***************
*** 1305,1309 ****
 		 "'%.50s' object attribute '%.400s' is read-only",
 		 tp->tp_name, PyString_AS_STRING(name));
! 	return -1;
 }
 
--- 1355,1361 ----
 		 "'%.50s' object attribute '%.400s' is read-only",
 		 tp->tp_name, PyString_AS_STRING(name));
! done:
! 	Py_DECREF(name);
! 	return res;
 }
 

AltStyle によって変換されたページ (->オリジナル) /