[Python-checkins] r70389 - python/trunk/Objects/typeobject.c

georg.brandl python-checkins at python.org
Sun Mar 15 22:43:38 CET 2009


Author: georg.brandl
Date: Sun Mar 15 22:43:38 2009
New Revision: 70389

Log:
Fix a small nit in the error message if bool() falls back on __len__ and it returns the wrong type: it would tell the user that __nonzero__ should return bool or int.

Modified:
   python/trunk/Objects/typeobject.c

Modified: python/trunk/Objects/typeobject.c
==============================================================================
--- python/trunk/Objects/typeobject.c	(original)
+++ python/trunk/Objects/typeobject.c	Sun Mar 15 22:43:38 2009
@@ -5096,6 +5096,7 @@
 	PyObject *func, *args;
 	static PyObject *nonzero_str, *len_str;
 	int result = -1;
+	int using_len = 0;
 
 	func = lookup_maybe(self, "__nonzero__", &nonzero_str);
 	if (func == NULL) {
@@ -5104,6 +5105,7 @@
 		func = lookup_maybe(self, "__len__", &len_str);
 		if (func == NULL)
 			return PyErr_Occurred() ? -1 : 1;
+		using_len = 1;
 	}
 	args = PyTuple_New(0);
 	if (args != NULL) {
@@ -5114,8 +5116,10 @@
 				result = PyObject_IsTrue(temp);
 			else {
 				PyErr_Format(PyExc_TypeError,
-					     "__nonzero__ should return "
+					     "%s should return "
 					     "bool or int, returned %s",
+					     (using_len ? "__len__"
+					                : "__nonzero__"),
 					     temp->ob_type->tp_name);
 				result = -1;
 			}


More information about the Python-checkins mailing list