[Patches] PyObject_GetAttr/PyObject_SetAttr core dumps if given non-string names

Gisle Aas gisle@ActiveState.com
22 Jun 2000 19:36:10 -0000


While developing a perl interface to python I discovered that
PyObject_(Has|Get|Set)Attr core dumps if given non-string names.

This patch ought to fix it.  If you actually wanted the object passed
to v->ob_type->tp_setattro to be interned if it was a string, then a
little additional tweak is needed for the PyObject_SetAttr().

Regards,
Gisle Aas


Index: Objects/object.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Objects/object.c,v
retrieving revision 2.71
diff -u -p -u -r2.71 object.c
--- Objects/object.c	2000/06/09 16:20:39	2.71
+++ Objects/object.c	2000/06/22 19:27:51
@@ -600,8 +600,12 @@ PyObject_GetAttr(v, name)
 {
 	if (v->ob_type->tp_getattro != NULL)
 		return (*v->ob_type->tp_getattro)(v, name);
-	else
+	else if (PyString_Check(name))
 		return PyObject_GetAttrString(v, PyString_AsString(name));
+	else {
+		PyErr_SetString(PyExc_TypeError,
+				"attribute name must be string");
+	}
 }
 
 int
@@ -626,12 +630,18 @@ PyObject_SetAttr(v, name, value)
 {
 	int err;
 	Py_INCREF(name);
-	PyString_InternInPlace(&name);
 	if (v->ob_type->tp_setattro != NULL)
 		err = (*v->ob_type->tp_setattro)(v, name, value);
-	else
+	else if (PyString_Check(name)) {
+		PyString_InternInPlace(&name);
 		err = PyObject_SetAttrString(
 			v, PyString_AsString(name), value);
+	}
+	else {
+		PyErr_SetString(PyExc_TypeError,
+				"attribute name must be string");
+		err = -1;
+	}
 	Py_DECREF(name);
 	return err;
 }