Skip to content

Commit 807bcc6

Browse files
committed
Address review: subTest and asserts
1 parent 9f24ad1 commit 807bcc6

2 files changed

Lines changed: 34 additions & 32 deletions

File tree

Lib/test/test_complex.py

Lines changed: 32 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -362,37 +362,38 @@ def test_pow_with_small_integer_exponents(self):
362362
exponents = [0, 1, 2, 3, 4, 5, 6, 19]
363363
for z in values:
364364
for e in exponents:
365-
try:
366-
r_pow = z**e
367-
except OverflowError:
368-
continue
369-
r_pro = reduce(lambda x, y: x*y, [z]*e) if e else 1+0j
370-
if str(r_pow) == str(r_pro):
371-
continue
372-
373-
self.assertNotIn(z.real, {0.0, -0.0, INF, -INF, NAN})
374-
self.assertNotIn(z.imag, {0.0, -0.0, INF, -INF, NAN})
375-
376-
# We might fail here, because associativity of multiplication
377-
# is broken already for floats.
378-
# Consider z = 1-1j. Then z*z*z*z = ((z*z)*z)*z = -4+0j,
379-
# while in the algorithm for pow() a diffenent grouping
380-
# of operations is used: z**4 = (z*z)*(z*z) = -4-0j.
381-
#
382-
# Fallback to the generic complex power algorithm.
383-
r_pro, r_pro_errno = _testcapi._py_c_pow(z, e)
384-
self.assertEqual(r_pro_errno, 0)
385-
self.assertClose(r_pow, r_pro)
386-
if isnan(r_pow.real):
387-
self.assertTrue(isnan(r_pro.real))
388-
else:
389-
self.assertEqual(copysign(1, r_pow.real),
390-
copysign(1, r_pro.real))
391-
if isnan(r_pow.imag):
392-
self.assertTrue(isnan(r_pro.imag))
393-
else:
394-
self.assertEqual(copysign(1, r_pow.imag),
395-
copysign(1, r_pro.imag))
365+
with self.subTest(value=z, exponent=e):
366+
try:
367+
r_pow = z**e
368+
except OverflowError:
369+
continue
370+
r_pro = reduce(lambda x, y: x*y, [z]*e) if e else 1+0j
371+
if str(r_pow) == str(r_pro):
372+
continue
373+
374+
self.assertNotIn(z.real, {0.0, -0.0, INF, -INF, NAN})
375+
self.assertNotIn(z.imag, {0.0, -0.0, INF, -INF, NAN})
376+
377+
# We might fail here, because associativity of multiplication
378+
# is broken already for floats.
379+
# Consider z = 1-1j. Then z*z*z*z = ((z*z)*z)*z = -4+0j,
380+
# while in the algorithm for pow() a diffenent grouping
381+
# of operations is used: z**4 = (z*z)*(z*z) = -4-0j.
382+
#
383+
# Fallback to the generic complex power algorithm.
384+
r_pro, r_pro_errno = _testcapi._py_c_pow(z, e)
385+
self.assertEqual(r_pro_errno, 0)
386+
self.assertClose(r_pow, r_pro)
387+
if isnan(r_pow.real):
388+
self.assertTrue(isnan(r_pro.real))
389+
else:
390+
self.assertEqual(copysign(1, r_pow.real),
391+
copysign(1, r_pro.real))
392+
if isnan(r_pow.imag):
393+
self.assertTrue(isnan(r_pro.imag))
394+
else:
395+
self.assertEqual(copysign(1, r_pow.imag),
396+
copysign(1, r_pro.imag))
396397

397398
def test_boolcontext(self):
398399
for i in range(100):

Objects/complexobject.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,8 @@ c_powu(Py_complex x, long n)
163163
{
164164
Py_complex p = x, r = n-- ? p : c_1;
165165
long mask = 1;
166-
assert(-1 <= n && n < INT_EXP_CUTOFF);
166+
assert(-1 <= n);
167+
assert(n < INT_EXP_CUTOFF);
167168
while (n >= mask) {
168169
assert(mask>0);
169170
if (n & mask)

0 commit comments

Comments
 (0)