[Python-checkins] r51246 - python/trunk/Objects/classobject.c

neal.norwitz python-checkins at python.org
Sun Aug 13 20:10:29 CEST 2006


Author: neal.norwitz
Date: Sun Aug 13 20:10:28 2006
New Revision: 51246

Modified:
   python/trunk/Objects/classobject.c
Log:
Handle a whole lot of failures from PyString_FromInternedString().

Should fix most of Klocwork 234-272.



Modified: python/trunk/Objects/classobject.c
==============================================================================
--- python/trunk/Objects/classobject.c	(original)
+++ python/trunk/Objects/classobject.c	Sun Aug 13 20:10:28 2006
@@ -103,8 +103,14 @@
 	op->cl_name = name;
 	if (getattrstr == NULL) {
 		getattrstr = PyString_InternFromString("__getattr__");
+		if (getattrstr == NULL)
+			return NULL;
 		setattrstr = PyString_InternFromString("__setattr__");
+		if (setattrstr == NULL)
+			return NULL;
 		delattrstr = PyString_InternFromString("__delattr__");
+		if (delattrstr == NULL)
+			return NULL;
 	}
 	op->cl_getattr = class_lookup(op, getattrstr, &dummy);
 	op->cl_setattr = class_lookup(op, setattrstr, &dummy);
@@ -522,11 +528,14 @@
 	PyObject *init;
 	static PyObject *initstr;
 
+	if (initstr == NULL) {
+		initstr = PyString_InternFromString("__init__");
+		if (initstr == NULL)
+			return NULL;
+	}
 	inst = (PyInstanceObject *) PyInstance_NewRaw(klass, NULL);
 	if (inst == NULL)
 		return NULL;
-	if (initstr == NULL)
-		initstr = PyString_InternFromString("__init__");
 	init = instance_getattr2(inst, initstr);
 	if (init == NULL) {
 		if (PyErr_Occurred()) {
@@ -612,8 +621,11 @@
 	/* Save the current exception, if any. */
 	PyErr_Fetch(&error_type, &error_value, &error_traceback);
 	/* Execute __del__ method, if any. */
-	if (delstr == NULL)
+	if (delstr == NULL) {
 		delstr = PyString_InternFromString("__del__");
+		if (delstr == NULL)
+			return NULL;
+	}
 	if ((del = instance_getattr2(inst, delstr)) != NULL) {
 		PyObject *res = PyEval_CallObject(del, (PyObject *)NULL);
 		if (res == NULL)
@@ -840,8 +852,11 @@
 	PyObject *res;
 	static PyObject *reprstr;
 
-	if (reprstr == NULL)
+	if (reprstr == NULL) {
 		reprstr = PyString_InternFromString("__repr__");
+		if (reprstr == NULL)
+			return NULL;
+	}
 	func = instance_getattr(inst, reprstr);
 	if (func == NULL) {
 		PyObject *classname, *mod;
@@ -876,8 +891,11 @@
 	PyObject *res;
 	static PyObject *strstr;
 
-	if (strstr == NULL)
+	if (strstr == NULL) {
 		strstr = PyString_InternFromString("__str__");
+		if (strstr == NULL)
+			return NULL;
+	}
 	func = instance_getattr(inst, strstr);
 	if (func == NULL) {
 		if (!PyErr_ExceptionMatches(PyExc_AttributeError))
@@ -898,8 +916,11 @@
 	long outcome;
 	static PyObject *hashstr, *eqstr, *cmpstr;
 
-	if (hashstr == NULL)
+	if (hashstr == NULL) {
 		hashstr = PyString_InternFromString("__hash__");
+		if (hashstr == NULL)
+			return -1;
+	}
 	func = instance_getattr(inst, hashstr);
 	if (func == NULL) {
 		if (!PyErr_ExceptionMatches(PyExc_AttributeError))
@@ -908,15 +929,21 @@
 		/* If there is no __eq__ and no __cmp__ method, we hash on the
 		   address.  If an __eq__ or __cmp__ method exists, there must
 		   be a __hash__. */
-		if (eqstr == NULL)
+		if (eqstr == NULL) {
 			eqstr = PyString_InternFromString("__eq__");
+			if (eqstr == NULL)
+				return -1;
+		}
 		func = instance_getattr(inst, eqstr);
 		if (func == NULL) {
 			if (!PyErr_ExceptionMatches(PyExc_AttributeError))
 				return -1;
 			PyErr_Clear();
-			if (cmpstr == NULL)
+			if (cmpstr == NULL) {
 				cmpstr = PyString_InternFromString("__cmp__");
+				if (cmpstr == NULL)
+					return -1;
+			}
 			func = instance_getattr(inst, cmpstr);
 			if (func == NULL) {
 				if (!PyErr_ExceptionMatches(
@@ -964,8 +991,11 @@
 	PyObject *res;
 	Py_ssize_t outcome;
 
-	if (lenstr == NULL)
+	if (lenstr == NULL) {
 		lenstr = PyString_InternFromString("__len__");
+		if (lenstr == NULL)
+			return -1;
+	}
 	func = instance_getattr(inst, lenstr);
 	if (func == NULL)
 		return -1;
@@ -1010,8 +1040,11 @@
 	PyObject *arg;
 	PyObject *res;
 
-	if (getitemstr == NULL)
+	if (getitemstr == NULL) {
 		getitemstr = PyString_InternFromString("__getitem__");
+		if (getitemstr == NULL)
+			return NULL;
+	}
 	func = instance_getattr(inst, getitemstr);
 	if (func == NULL)
 		return NULL;
@@ -1034,13 +1067,19 @@
 	PyObject *res;
 
 	if (value == NULL) {
-		if (delitemstr == NULL)
+		if (delitemstr == NULL) {
 			delitemstr = PyString_InternFromString("__delitem__");
+			if (delitemstr == NULL)
+				return -1;
+		}
 		func = instance_getattr(inst, delitemstr);
 	}
 	else {
-		if (setitemstr == NULL)
+		if (setitemstr == NULL) {
 			setitemstr = PyString_InternFromString("__setitem__");
+			if (setitemstr == NULL)
+				return -1;
+		}
 		func = instance_getattr(inst, setitemstr);
 	}
 	if (func == NULL)
@@ -1073,8 +1112,11 @@
 {
 	PyObject *func, *res;
 
-	if (getitemstr == NULL)
+	if (getitemstr == NULL) {
 		getitemstr = PyString_InternFromString("__getitem__");
+		if (getitemstr == NULL)
+			return NULL;
+	}
 	func = instance_getattr(inst, getitemstr);
 	if (func == NULL)
 		return NULL;
@@ -1089,8 +1131,11 @@
 	PyObject *func, *arg, *res;
 	static PyObject *getslicestr;
 
-	if (getslicestr == NULL)
+	if (getslicestr == NULL) {
 		getslicestr = PyString_InternFromString("__getslice__");
+		if (getslicestr == NULL)
+			return NULL;
+	}
 	func = instance_getattr(inst, getslicestr);
 
 	if (func == NULL) {
@@ -1098,8 +1143,11 @@
 			return NULL;
 		PyErr_Clear();
 
-		if (getitemstr == NULL)
+		if (getitemstr == NULL) {
 			getitemstr = PyString_InternFromString("__getitem__");
+			if (getitemstr == NULL)
+				return NULL;
+		}
 		func = instance_getattr(inst, getitemstr);
 		if (func == NULL)
 			return NULL;
@@ -1123,13 +1171,19 @@
 	PyObject *func, *arg, *res;
 
 	if (item == NULL) {
-		if (delitemstr == NULL)
+		if (delitemstr == NULL) {
 			delitemstr = PyString_InternFromString("__delitem__");
+			if (delitemstr == NULL)
+				return -1;
+		}
 		func = instance_getattr(inst, delitemstr);
 	}
 	else {
-		if (setitemstr == NULL)
+		if (setitemstr == NULL) {
 			setitemstr = PyString_InternFromString("__setitem__");
+			if (setitemstr == NULL)
+				return -1;
+		}
 		func = instance_getattr(inst, setitemstr);
 	}
 	if (func == NULL)
@@ -1158,17 +1212,23 @@
 	static PyObject *setslicestr, *delslicestr;
 
 	if (value == NULL) {
-		if (delslicestr == NULL)
+		if (delslicestr == NULL) {
 			delslicestr =
 				PyString_InternFromString("__delslice__");
+			if (delslicestr == NULL)
+				return -1;
+		}
 		func = instance_getattr(inst, delslicestr);
 		if (func == NULL) {
 			if (!PyErr_ExceptionMatches(PyExc_AttributeError))
 				return -1;
 			PyErr_Clear();
-			if (delitemstr == NULL)
+			if (delitemstr == NULL) {
 				delitemstr =
 				    PyString_InternFromString("__delitem__");
+				if (delitemstr == NULL)
+					return -1;
+			}
 			func = instance_getattr(inst, delitemstr);
 			if (func == NULL)
 				return -1;
@@ -1179,17 +1239,23 @@
 			arg = Py_BuildValue("(nn)", i, j);
 	}
 	else {
-		if (setslicestr == NULL)
+		if (setslicestr == NULL) {
 			setslicestr =
 				PyString_InternFromString("__setslice__");
+			if (setslicestr == NULL)
+				return -1;
+		}
 		func = instance_getattr(inst, setslicestr);
 		if (func == NULL) {
 			if (!PyErr_ExceptionMatches(PyExc_AttributeError))
 				return -1;
 			PyErr_Clear();
-			if (setitemstr == NULL)
+			if (setitemstr == NULL) {
 				setitemstr =
 				    PyString_InternFromString("__setitem__");
+				if (setitemstr == NULL)
+					return -1;
+			}
 			func = instance_getattr(inst, setitemstr);
 			if (func == NULL)
 				return -1;
@@ -1462,7 +1528,8 @@
 #define UNARY(funcname, methodname) \
 static PyObject *funcname(PyInstanceObject *self) { \
 	static PyObject *o; \
-	if (o == NULL) o = PyString_InternFromString(methodname); \
+	if (o == NULL) { o = PyString_InternFromString(methodname); \
+			 if (o == NULL) return NULL; } \
 	return generic_unary_op(self, o); \
 }
 
@@ -1634,14 +1701,20 @@
 	long outcome;
 	static PyObject *nonzerostr;
 
-	if (nonzerostr == NULL)
+	if (nonzerostr == NULL) {
 		nonzerostr = PyString_InternFromString("__nonzero__");
+		if (nonzerostr == NULL)
+			return -1;
+	}
 	if ((func = instance_getattr(self, nonzerostr)) == NULL) {
 		if (!PyErr_ExceptionMatches(PyExc_AttributeError))
 			return -1;
 		PyErr_Clear();
-		if (lenstr == NULL)
+		if (lenstr == NULL) {
 			lenstr = PyString_InternFromString("__len__");
+			if (lenstr == NULL)
+				return -1;
+		}
 		if ((func = instance_getattr(self, lenstr)) == NULL) {
 			if (!PyErr_ExceptionMatches(PyExc_AttributeError))
 				return -1;
@@ -1923,8 +1996,11 @@
 {
 	PyObject *func;
 
-	if (nextstr == NULL)
+	if (nextstr == NULL) {
 		nextstr = PyString_InternFromString("next");
+		if (nextstr == NULL)
+			return NULL;
+	}
 
 	if ((func = instance_getattr(self, nextstr)) != NULL) {
 		PyObject *res = PyEval_CallObject(func, (PyObject *)NULL);


More information about the Python-checkins mailing list