|
10 | 10 | import sys
|
11 | 11 | import copy
|
12 | 12 | import functools
|
| 13 | +import operator |
13 | 14 | import pickle
|
14 | 15 | import tempfile
|
15 | 16 | import textwrap
|
@@ -737,6 +738,37 @@ def check(fmt, vals, result):
|
737 | 738 | check(b'%i%b %*.*b', (10, b'3', 5, 3, b'abc',), b'103 abc')
|
738 | 739 | check(b'%c', b'a', b'a')
|
739 | 740 |
|
| 741 | + class PseudoFloat: |
| 742 | + def __init__(self, value): |
| 743 | + self.value = float(value) |
| 744 | + def __int__(self): |
| 745 | + return int(self.value) |
| 746 | + |
| 747 | + pi = PseudoFloat(3.1415) |
| 748 | + |
| 749 | + exceptions_params = [ |
| 750 | + ('%x format: an integer is required, not float', b'%x', 3.14), |
| 751 | + ('%X format: an integer is required, not float', b'%X', 2.11), |
| 752 | + ('%o format: an integer is required, not float', b'%o', 1.79), |
| 753 | + ('%x format: an integer is required, not PseudoFloat', b'%x', pi), |
| 754 | + ('%x format: an integer is required, not complex', b'%x', 3j), |
| 755 | + ('%X format: an integer is required, not complex', b'%X', 2j), |
| 756 | + ('%o format: an integer is required, not complex', b'%o', 1j), |
| 757 | + ('%u format: a real number is required, not complex', b'%u', 3j), |
| 758 | + # See https://github.com/python/cpython/issues/130928 as for why |
| 759 | + # the exception message contains '%d' instead of '%i'. |
| 760 | + ('%d format: a real number is required, not complex', b'%i', 2j), |
| 761 | + ('%d format: a real number is required, not complex', b'%d', 2j), |
| 762 | + ( |
| 763 | + r'%c requires an integer in range\(256\) or a single byte', |
| 764 | + b'%c', pi |
| 765 | + ), |
| 766 | + ] |
| 767 | + |
| 768 | + for msg, format_bytes, value in exceptions_params: |
| 769 | + with self.assertRaisesRegex(TypeError, msg): |
| 770 | + operator.mod(format_bytes, value) |
| 771 | + |
740 | 772 | def test_imod(self):
|
741 | 773 | b = self.type2test(b'hello, %b!')
|
742 | 774 | orig = b
|
|
0 commit comments