]> granicus.if.org Git - python/commitdiff
Close #18780: %-formatting now prints value for int subclasses with %d, %i, and ...
authorEthan Furman <ethan@stoneleaf.us>
Sat, 31 Aug 2013 17:18:55 +0000 (10:18 -0700)
committerEthan Furman <ethan@stoneleaf.us>
Sat, 31 Aug 2013 17:18:55 +0000 (10:18 -0700)
Lib/test/test_unicode.py
Misc/NEWS
Objects/unicodeobject.c

index 9e53213a9204ed267ac3c8f2965e834f658da6be..daab79d742d9e0df0045116c252daeba98a90578 100644 (file)
@@ -1124,6 +1124,53 @@ class UnicodeTest(string_tests.CommonTest,
         self.assertEqual('%.1s' % "a\xe9\u20ac", 'a')
         self.assertEqual('%.2s' % "a\xe9\u20ac", 'a\xe9')
 
+    def test_formatting_with_enum(self):
+        # issue18780
+        import enum
+        class Float(float, enum.Enum):
+            PI = 3.1415926
+        class Int(enum.IntEnum):
+            IDES = 15
+        class Str(str, enum.Enum):
+            ABC = 'abc'
+        # Testing Unicode formatting strings...
+        self.assertEqual(
+                "%s, %s" % (Str.ABC, Str.ABC),
+                'Str.ABC, Str.ABC',
+                )
+        self.assertEqual(
+                "%s, %s, %d, %i, %u, %f, %5.2f" %
+                    (Str.ABC, Str.ABC,
+                     Int.IDES, Int.IDES, Int.IDES,
+                     Float.PI, Float.PI),
+                'Str.ABC, Str.ABC, 15, 15, 15, 3.141593,  3.14')
+
+        # formatting jobs delegated from the string implementation:
+        self.assertEqual(
+                '...%(foo)s...' % {'foo':Str.ABC},
+                '...Str.ABC...',
+                )
+        self.assertEqual(
+                '...%(foo)s...' % {'foo':Int.IDES},
+                '...Int.IDES...',
+                )
+        self.assertEqual(
+                '...%(foo)i...' % {'foo':Int.IDES},
+                '...15...',
+                )
+        self.assertEqual(
+                '...%(foo)d...' % {'foo':Int.IDES},
+                '...15...',
+                )
+        self.assertEqual(
+                '...%(foo)u...' % {'foo':Int.IDES, 'def':Float.PI},
+                '...15...',
+                )
+        self.assertEqual(
+                '...%(foo)f...' % {'foo':Float.PI,'def':123},
+                '...3.141593...',
+                )
+
     @support.cpython_only
     def test_formatting_huge_precision(self):
         from _testcapi import INT_MAX
index 7258ce1755f5ae7066eccbf03d06bb1ed6b65d46..e655fe57b2bec5c943ddf09b2bc5a998d745e2fb 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -48,6 +48,9 @@ Core and Builtins
 - Issue #17934: Add a clear() method to frame objects, to help clean up
   expensive details (local variables) and break reference cycles.
 
+- Issue #18780: %-formatting codes %d, %i, and %u now treat int-subclasses
+  as int (displays value of int-subclass instead of str(int-subclass) ).
+
 Library
 -------
 
index f1d687ae9d6c6034a09489decca6f3f31d4941c9..6dc583517c2acf4f75a26839c5dd94118719ef98 100644 (file)
@@ -13566,11 +13566,9 @@ formatlong(PyObject *val, struct unicode_format_arg_t *arg)
     case 'd':
     case 'i':
     case 'u':
-        /* Special-case boolean: we want 0/1 */
-        if (PyBool_Check(val))
-            result = PyNumber_ToBase(val, 10);
-        else
-            result = Py_TYPE(val)->tp_str(val);
+        /* int and int subclasses should print numerically when a numeric */
+        /* format code is used (see issue18780) */
+        result = PyNumber_ToBase(val, 10);
         break;
     case 'o':
         numnondigits = 2;