[Python-checkins] gh-94808: improve test coverage of number formatting (GH-99472)

miss-islington webhook-mailer at python.org
Fri Dec 23 21:14:20 EST 2022


https://github.com/python/cpython/commit/b168b0e013854b9a7d896c72dfc4d220bd036461
commit: b168b0e013854b9a7d896c72dfc4d220bd036461
branch: 3.10
author: Miss Islington (bot) <31488909+miss-islington at users.noreply.github.com>
committer: miss-islington <31488909+miss-islington at users.noreply.github.com>
date: 2022-12-23T18:14:14-08:00
summary:

gh-94808: improve test coverage of number formatting (GH-99472)

(cherry picked from commit 7ca45e5ddd493411e61706d07679ea54b954e41b)

Co-authored-by: Nikita Sobolev <mail at sobolevn.me>

files:
M Lib/test/test_unicode.py

diff --git a/Lib/test/test_unicode.py b/Lib/test/test_unicode.py
index f6a1651e76f7..1b31ae1e0560 100644
--- a/Lib/test/test_unicode.py
+++ b/Lib/test/test_unicode.py
@@ -1266,6 +1266,20 @@ def __repr__(self):
         self.assertRaises(ValueError, ("{" + big + "}").format)
         self.assertRaises(ValueError, ("{[" + big + "]}").format, [0])
 
+        # test number formatter errors:
+        self.assertRaises(ValueError, '{0:x}'.format, 1j)
+        self.assertRaises(ValueError, '{0:x}'.format, 1.0)
+        self.assertRaises(ValueError, '{0:X}'.format, 1j)
+        self.assertRaises(ValueError, '{0:X}'.format, 1.0)
+        self.assertRaises(ValueError, '{0:o}'.format, 1j)
+        self.assertRaises(ValueError, '{0:o}'.format, 1.0)
+        self.assertRaises(ValueError, '{0:u}'.format, 1j)
+        self.assertRaises(ValueError, '{0:u}'.format, 1.0)
+        self.assertRaises(ValueError, '{0:i}'.format, 1j)
+        self.assertRaises(ValueError, '{0:i}'.format, 1.0)
+        self.assertRaises(ValueError, '{0:d}'.format, 1j)
+        self.assertRaises(ValueError, '{0:d}'.format, 1.0)
+
         # issue 6089
         self.assertRaises(ValueError, "{0[0]x}".format, [None])
         self.assertRaises(ValueError, "{0[0](10)}".format, [None])
@@ -1501,11 +1515,31 @@ def __int__(self):
         self.assertEqual('%X' % letter_m, '6D')
         self.assertEqual('%o' % letter_m, '155')
         self.assertEqual('%c' % letter_m, 'm')
-        self.assertRaisesRegex(TypeError, '%x format: an integer is required, not float', operator.mod, '%x', 3.14),
-        self.assertRaisesRegex(TypeError, '%X format: an integer is required, not float', operator.mod, '%X', 2.11),
-        self.assertRaisesRegex(TypeError, '%o format: an integer is required, not float', operator.mod, '%o', 1.79),
-        self.assertRaisesRegex(TypeError, '%x format: an integer is required, not PseudoFloat', operator.mod, '%x', pi),
-        self.assertRaises(TypeError, operator.mod, '%c', pi),
+        self.assertRaisesRegex(TypeError, '%x format: an integer is required, not float', operator.mod, '%x', 3.14)
+        self.assertRaisesRegex(TypeError, '%X format: an integer is required, not float', operator.mod, '%X', 2.11)
+        self.assertRaisesRegex(TypeError, '%o format: an integer is required, not float', operator.mod, '%o', 1.79)
+        self.assertRaisesRegex(TypeError, '%x format: an integer is required, not PseudoFloat', operator.mod, '%x', pi)
+        self.assertRaisesRegex(TypeError, '%x format: an integer is required, not complex', operator.mod, '%x', 3j)
+        self.assertRaisesRegex(TypeError, '%X format: an integer is required, not complex', operator.mod, '%X', 2j)
+        self.assertRaisesRegex(TypeError, '%o format: an integer is required, not complex', operator.mod, '%o', 1j)
+        self.assertRaisesRegex(TypeError, '%u format: a real number is required, not complex', operator.mod, '%u', 3j)
+        self.assertRaisesRegex(TypeError, '%i format: a real number is required, not complex', operator.mod, '%i', 2j)
+        self.assertRaisesRegex(TypeError, '%d format: a real number is required, not complex', operator.mod, '%d', 1j)
+        self.assertRaisesRegex(TypeError, '%c requires int or char', operator.mod, '%c', pi)
+
+        class RaisingNumber:
+            def __int__(self):
+                raise RuntimeError('int')  # should not be `TypeError`
+            def __index__(self):
+                raise RuntimeError('index')  # should not be `TypeError`
+
+        rn = RaisingNumber()
+        self.assertRaisesRegex(RuntimeError, 'int', operator.mod, '%d', rn)
+        self.assertRaisesRegex(RuntimeError, 'int', operator.mod, '%i', rn)
+        self.assertRaisesRegex(RuntimeError, 'int', operator.mod, '%u', rn)
+        self.assertRaisesRegex(RuntimeError, 'index', operator.mod, '%x', rn)
+        self.assertRaisesRegex(RuntimeError, 'index', operator.mod, '%X', rn)
+        self.assertRaisesRegex(RuntimeError, 'index', operator.mod, '%o', rn)
 
     def test_formatting_with_enum(self):
         # issue18780



More information about the Python-checkins mailing list