diff -r 913268337886 Lib/fractions.py --- a/Lib/fractions.py Sun Aug 21 17:41:56 2016 +1000 +++ b/Lib/fractions.py Sun Aug 21 09:45:43 2016 +0100 @@ -484,10 +484,14 @@ return Fraction(a._numerator ** power, a._denominator ** power, _normalize=False) - else: + elif a._numerator >= 0: return Fraction(a._denominator ** -power, a._numerator ** -power, _normalize=False) + else: + return Fraction((-a._denominator) ** -power, + (-a._numerator) ** -power, + _normalize=False) else: # A fractional power will generally produce an # irrational number. diff -r 913268337886 Lib/test/test_fractions.py --- a/Lib/test/test_fractions.py Sun Aug 21 17:41:56 2016 +1000 +++ b/Lib/test/test_fractions.py Sun Aug 21 09:45:43 2016 +0100 @@ -356,6 +356,19 @@ z = pow(F(-1), F(1, 2)) self.assertAlmostEqual(z.real, 0) self.assertEqual(z.imag, 1) + # Regression test for #27539. + p = F(-1, 2) ** 0 + self.assertEqual(p, F(1, 1)) + self.assertEqual(p.numerator, 1) + self.assertEqual(p.denominator, 1) + p = F(-1, 2) ** -1 + self.assertEqual(p, F(-2, 1)) + self.assertEqual(p.numerator, -2) + self.assertEqual(p.denominator, 1) + p = F(-1, 2) ** -2 + self.assertEqual(p, F(4, 1)) + self.assertEqual(p.numerator, 4) + self.assertEqual(p.denominator, 1) def testMixedArithmetic(self): self.assertTypedEquals(F(11, 10), F(1, 10) + 1)