@@ -150,14 +150,38 @@ def test_frozenset(self):
150150 eq (r (frozenset ({1 , 2 , 3 , 4 , 5 , 6 , 7 })), "frozenset({1, 2, 3, 4, 5, 6, ...})" )
151151
152152 def test_numbers (self ):
153- eq = self .assertEqual
154- eq (r (123 ), repr (123 ))
155- eq (r (123 ), repr (123 ))
156- eq (r (1.0 / 3 ), repr (1.0 / 3 ))
157-
158- n = 10 ** 100
159- expected = repr (n )[:18 ] + "..." + repr (n )[- 19 :]
160- eq (r (n ), expected )
153+ for x in [123 , 1.0 / 3 ]:
154+ self .assertEqual (r (x ), repr (x ))
155+
156+ max_digits = sys .get_int_max_str_digits ()
157+ for k in [100 , max_digits - 1 ]:
158+ with self .subTest (f'10 ** { k } ' , k = k ):
159+ n = 10 ** k
160+ expected = repr (n )[:18 ] + "..." + repr (n )[- 19 :]
161+ self .assertEqual (r (n ), expected )
162+
163+ def re_msg (n , d ):
164+ return (rf'<{ n .__class__ .__name__ } instance with roughly { d } '
165+ rf'digits \(limit at { max_digits } \) at 0x[a-f0-9]+>' )
166+
167+ k = max_digits
168+ with self .subTest (f'10 ** { k } ' , k = k ):
169+ n = 10 ** k
170+ self .assertRaises (ValueError , repr , n )
171+ self .assertRegex (r (n ), re_msg (n , k + 1 ))
172+
173+ for k in [max_digits + 1 , 2 * max_digits ]:
174+ self .assertGreater (k , 100 )
175+ with self .subTest (f'10 ** { k } ' , k = k ):
176+ n = 10 ** k
177+ self .assertRaises (ValueError , repr , n )
178+ self .assertRegex (r (n ), re_msg (n , k + 1 ))
179+ with self .subTest (f'10 ** { k } - 1' , k = k ):
180+ n = 10 ** k - 1
181+ # Here, since math.log10(n) == math.log10(n-1),
182+ # the number of digits of n - 1 is overestimated.
183+ self .assertRaises (ValueError , repr , n )
184+ self .assertRegex (r (n ), re_msg (n , k + 1 ))
161185
162186 def test_instance (self ):
163187 eq = self .assertEqual
0 commit comments