From: Martin v. Löwis Date: Tue, 14 Aug 2007 21:57:32 +0000 (+0000) Subject: Format bools properly in %d. X-Git-Tag: v3.0a1~402 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=ff398c6f957fcd0e55aa57c0eaa5c1d24c5bc2f1;p=python Format bools properly in %d. --- diff --git a/Lib/test/test_bool.py b/Lib/test/test_bool.py index 7a9022e811..bbaea85c63 100644 --- a/Lib/test/test_bool.py +++ b/Lib/test/test_bool.py @@ -163,6 +163,12 @@ class BoolTest(unittest.TestCase): 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): self.assertIs(hasattr([], "append"), True) self.assertIs(hasattr([], "wobble"), False) diff --git a/Objects/stringobject.c b/Objects/stringobject.c index 7a35974bc2..b1d711d3f7 100644 --- a/Objects/stringobject.c +++ b/Objects/stringobject.c @@ -4144,11 +4144,14 @@ _PyString_FormatLong(PyObject *val, int flags, int prec, int type, return NULL; } - switch (type) { case 'd': 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; case 'o': numnondigits = 2;