diff --git a/graalpython/com.oracle.graal.python.test/src/tests/test_math.py b/graalpython/com.oracle.graal.python.test/src/tests/test_math.py index b4cd1564af..6b609b2edc 100644 --- a/graalpython/com.oracle.graal.python.test/src/tests/test_math.py +++ b/graalpython/com.oracle.graal.python.test/src/tests/test_math.py @@ -1778,3 +1778,48 @@ def test_objects(self): self.assertIsClose(MyFloat(), MyFloat(), rel_tol=MyFloat()) self.assertIsNotClose(MyFloat(), 10) + + +class MathLogTests(unittest.TestCase): + + def test_log_base_none(self): + class PositiveFloat: + def __float__(self): + return 10.0 + + self.assertRaises(TypeError, math.log, 10, None) + self.assertRaises(TypeError, math.log, 10.0, None) + self.assertRaises(TypeError, math.log, 2**70, None) + self.assertRaises(TypeError, math.log, PositiveFloat(), None) + self.assertRaises(ValueError, math.log, -1, None) + self.assertEqual(math.log(1), 0.0) + self.assertEqual(math.log(1.0), 0.0) + self.assertTrue(48.52 < math.log(2**70) < 48.53) + + def test_log_value_checked_before_base(self): + class NegativeFloat: + def __float__(self): + return -1.0 + + self.assertRaises(ValueError, math.log, -1, object()) + self.assertRaises(ValueError, math.log, 0, object()) + self.assertRaises(ValueError, math.log, -1.0, object()) + self.assertRaises(ValueError, math.log, -2**70, object()) + self.assertRaises(ValueError, math.log, NegativeFloat(), object()) + self.assertRaises(TypeError, math.log, float('nan'), object()) + + def test_log_zero_with_base(self): + class IntSubclass(int): + pass + + self.assertRaises(ValueError, math.log, 0, 2) + self.assertRaises(ValueError, math.log, 0.0, 2.0) + self.assertRaises(ValueError, math.log, -0.0, 2) + self.assertRaises(ValueError, math.log, False, 2) + self.assertRaises(ValueError, math.log, 0, 1) + self.assertRaises(ValueError, math.log, 0, IntSubclass(2)) + self.assertRaises(ValueError, math.log, 0.0, IntSubclass(2)) + self.assertRaises(ValueError, math.log, IntSubclass(0)) + self.assertRaises(ValueError, math.log, IntSubclass(0), 2.0) + self.assertRaises(ValueError, math.log, IntSubclass(0), 2) + self.assertRaises(ValueError, math.log, IntSubclass(0), IntSubclass(2)) diff --git a/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/MathModuleBuiltins.java b/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/MathModuleBuiltins.java index 029f6ac3cf..0af8758e68 100644 --- a/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/MathModuleBuiltins.java +++ b/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/MathModuleBuiltins.java @@ -1675,7 +1675,7 @@ private static double countBase(BigInteger base, Node inliningTarget, InlinedCon return logBase; } - @Specialization + @Specialization(guards = "isNoValue(novalue)") static double log(long value, PNone novalue, @Bind Node inliningTarget, @Shared @Cached InlinedConditionProfile doNotFit, @@ -1683,7 +1683,7 @@ static double log(long value, PNone novalue, return logDN(value, novalue, inliningTarget, doNotFit, raiseNode); } - @Specialization + @Specialization(guards = "isNoValue(novalue)") static double logDN(double value, @SuppressWarnings("unused") PNone novalue, @Bind Node inliningTarget, @Shared @Cached InlinedConditionProfile doNotFit, @@ -1692,14 +1692,14 @@ static double logDN(double value, @SuppressWarnings("unused") PNone novalue, return Math.log(value); } - @Specialization + @Specialization(guards = "isNoValue(novalue)") @TruffleBoundary static double logPIN(PInt value, @SuppressWarnings("unused") PNone novalue, @Bind Node inliningTarget, @Shared @Cached InlinedConditionProfile doNotFit, @Shared @Cached PRaiseNode raiseNode) { BigInteger bValue = value.getValue(); - raiseMathError(inliningTarget, doNotFit, bValue.compareTo(BigInteger.ZERO) < 0, raiseNode); + raiseMathError(inliningTarget, doNotFit, bValue.compareTo(BigInteger.ZERO) <= 0, raiseNode); return logBigInteger(bValue); } @@ -1736,7 +1736,7 @@ static double logDD(double value, double base, @Shared @Cached InlinedConditionProfile doNotFit, @Shared @Cached InlinedConditionProfile divByZero, @Shared @Cached PRaiseNode raiseNode) { - raiseMathError(inliningTarget, doNotFit, value < 0 || base <= 0, raiseNode); + raiseMathError(inliningTarget, doNotFit, value <= 0 || base <= 0, raiseNode); double logBase = countBase(base, inliningTarget, divByZero, raiseNode); return Math.log(value) / logBase; } @@ -1749,7 +1749,7 @@ static double logDPI(double value, PInt base, @Shared @Cached InlinedConditionProfile divByZero, @Shared @Cached PRaiseNode raiseNode) { BigInteger bBase = base.getValue(); - raiseMathError(inliningTarget, doNotFit, value < 0 || bBase.compareTo(BigInteger.ZERO) <= 0, raiseNode); + raiseMathError(inliningTarget, doNotFit, value <= 0 || bBase.compareTo(BigInteger.ZERO) <= 0, raiseNode); double logBase = countBase(bBase, inliningTarget, divByZero, raiseNode); return Math.log(value) / logBase; } @@ -1771,7 +1771,7 @@ static double logPID(PInt value, double base, @Shared @Cached InlinedConditionProfile divByZero, @Shared @Cached PRaiseNode raiseNode) { BigInteger bValue = value.getValue(); - raiseMathError(inliningTarget, doNotFit, bValue.compareTo(BigInteger.ZERO) < 0 || base <= 0, raiseNode); + raiseMathError(inliningTarget, doNotFit, bValue.compareTo(BigInteger.ZERO) <= 0 || base <= 0, raiseNode); double logBase = countBase(base, inliningTarget, divByZero, raiseNode); return logBigInteger(bValue) / logBase; } @@ -1784,7 +1784,7 @@ static double logLPI(long value, PInt base, @Shared @Cached InlinedConditionProfile divByZero, @Shared @Cached PRaiseNode raiseNode) { BigInteger bBase = base.getValue(); - raiseMathError(inliningTarget, doNotFit, value < 0 || bBase.compareTo(BigInteger.ZERO) <= 0, raiseNode); + raiseMathError(inliningTarget, doNotFit, value <= 0 || bBase.compareTo(BigInteger.ZERO) <= 0, raiseNode); double logBase = countBase(bBase, inliningTarget, divByZero, raiseNode); return Math.log(value) / logBase; } @@ -1798,12 +1798,12 @@ static double logPIPI(PInt value, PInt base, @Shared @Cached PRaiseNode raiseNode) { BigInteger bValue = value.getValue(); BigInteger bBase = base.getValue(); - raiseMathError(inliningTarget, doNotFit, bValue.compareTo(BigInteger.ZERO) < 0 || bBase.compareTo(BigInteger.ZERO) <= 0, raiseNode); + raiseMathError(inliningTarget, doNotFit, bValue.compareTo(BigInteger.ZERO) <= 0 || bBase.compareTo(BigInteger.ZERO) <= 0, raiseNode); double logBase = countBase(bBase, inliningTarget, divByZero, raiseNode); return logBigInteger(bValue) / logBase; } - @Specialization(guards = "!isNumber(value)") + @Specialization(guards = {"!isNumber(value)", "isNoValue(novalue)"}) double logO(VirtualFrame frame, Object value, PNone novalue, @Bind Node inliningTarget, @Shared @Cached PyFloatAsDoubleNode asDoubleNode) { @@ -1813,28 +1813,41 @@ static double logPIPI(PInt value, PInt base, @Specialization(guards = {"!isNumber(value)", "!isNoValue(base)"}) double logOO(VirtualFrame frame, Object value, Object base, @Bind Node inliningTarget, - @Shared @Cached PyFloatAsDoubleNode asDoubleNode) { - return executeRecursiveLogNode(frame, asDoubleNode.execute(frame, inliningTarget, value), asDoubleNode.execute(frame, inliningTarget, base)); + @Shared @Cached PyFloatAsDoubleNode asDoubleNode, + @Shared @Cached InlinedConditionProfile doNotFit, + @Shared @Cached PRaiseNode raiseNode) { + double x = asDoubleNode.execute(frame, inliningTarget, value); + raiseMathError(inliningTarget, doNotFit, x <= 0, raiseNode); + return executeRecursiveLogNode(frame, x, asDoubleNode.execute(frame, inliningTarget, base)); } - @Specialization(guards = {"!isNumber(base)"}) + @Specialization(guards = {"!isNumber(base)", "!isNoValue(base)"}) double logLO(VirtualFrame frame, long value, Object base, @Bind Node inliningTarget, - @Shared @Cached PyFloatAsDoubleNode asDoubleNode) { + @Shared @Cached PyFloatAsDoubleNode asDoubleNode, + @Shared @Cached InlinedConditionProfile doNotFit, + @Shared @Cached PRaiseNode raiseNode) { + raiseMathError(inliningTarget, doNotFit, value <= 0, raiseNode); return executeRecursiveLogNode(frame, value, asDoubleNode.execute(frame, inliningTarget, base)); } - @Specialization(guards = {"!isNumber(base)"}) + @Specialization(guards = {"!isNumber(base)", "!isNoValue(base)"}) double logDO(VirtualFrame frame, double value, Object base, @Bind Node inliningTarget, - @Shared @Cached PyFloatAsDoubleNode asDoubleNode) { + @Shared @Cached PyFloatAsDoubleNode asDoubleNode, + @Shared @Cached InlinedConditionProfile doNotFit, + @Shared @Cached PRaiseNode raiseNode) { + raiseMathError(inliningTarget, doNotFit, value <= 0, raiseNode); return executeRecursiveLogNode(frame, value, asDoubleNode.execute(frame, inliningTarget, base)); } - @Specialization(guards = {"!isNumber(base)"}) + @Specialization(guards = {"!isNumber(base)", "!isNoValue(base)"}) double logPIO(VirtualFrame frame, PInt value, Object base, @Bind Node inliningTarget, - @Shared @Cached PyFloatAsDoubleNode asDoubleNode) { + @Shared @Cached PyFloatAsDoubleNode asDoubleNode, + @Shared @Cached InlinedConditionProfile doNotFit, + @Shared @Cached PRaiseNode raiseNode) { + raiseMathError(inliningTarget, doNotFit, value.isZeroOrNegative(), raiseNode); return executeRecursiveLogNode(frame, value, asDoubleNode.execute(frame, inliningTarget, base)); }