Format bools properly in %d.

This commit is contained in:
Martin v. Löwis 2007-08-14 21:57:32 +00:00
parent 3eed765223
commit ff398c6f95
2 changed files with 11 additions and 2 deletions

View File

@ -163,6 +163,12 @@ class BoolTest(unittest.TestCase):
self.assertIs(bool(""), False) self.assertIs(bool(""), False)
self.assertIs(bool(), False) self.assertIs(bool(), False)
def test_format(self):
self.assertEqual("%d" % False, "0")
self.assertEqual("%d" % True, "1")
self.assertEqual("%x" % False, "0")
self.assertEqual("%x" % True, "1")
def test_hasattr(self): def test_hasattr(self):
self.assertIs(hasattr([], "append"), True) self.assertIs(hasattr([], "append"), True)
self.assertIs(hasattr([], "wobble"), False) self.assertIs(hasattr([], "wobble"), False)

View File

@ -4144,11 +4144,14 @@ _PyString_FormatLong(PyObject *val, int flags, int prec, int type,
return NULL; return NULL;
} }
switch (type) { switch (type) {
case 'd': case 'd':
case 'u': case 'u':
result = Py_Type(val)->tp_str(val); /* Special-case boolean: we want 0/1 */
if (PyBool_Check(val))
result = PyNumber_ToBase(val, 10);
else
result = Py_Type(val)->tp_str(val);
break; break;
case 'o': case 'o':
numnondigits = 2; numnondigits = 2;