Skip to content

Commit 7cdcbca

Browse files
committed
Use PythonObjectLibrary instead of CoerceToDoubleNode, externalize error messages
1 parent ce4031a commit 7cdcbca

File tree

1 file changed

+9
-8
lines changed

1 file changed

+9
-8
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/CmathModuleBuiltins.java

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,14 @@
1111
import com.oracle.graal.python.builtins.objects.PNone;
1212
import com.oracle.graal.python.builtins.objects.complex.ComplexBuiltins;
1313
import com.oracle.graal.python.builtins.objects.complex.PComplex;
14+
import com.oracle.graal.python.builtins.objects.object.PythonObjectLibrary;
1415
import com.oracle.graal.python.builtins.objects.tuple.PTuple;
1516
import com.oracle.graal.python.nodes.ErrorMessages;
1617
import com.oracle.graal.python.nodes.function.PythonBuiltinBaseNode;
1718
import com.oracle.graal.python.nodes.function.builtins.PythonBinaryBuiltinNode;
1819
import com.oracle.graal.python.nodes.function.builtins.PythonUnaryBuiltinNode;
1920
import com.oracle.graal.python.nodes.truffle.PythonArithmeticTypes;
2021
import com.oracle.graal.python.nodes.util.CoerceToComplexNode;
21-
import com.oracle.graal.python.nodes.util.CoerceToDoubleNode;
2222
import com.oracle.graal.python.runtime.PythonCore;
2323
import com.oracle.graal.python.runtime.object.PythonObjectFactory;
2424
import com.oracle.truffle.api.CompilerDirectives;
@@ -29,6 +29,7 @@
2929
import com.oracle.truffle.api.dsl.Specialization;
3030
import com.oracle.truffle.api.dsl.TypeSystemReference;
3131
import com.oracle.truffle.api.frame.VirtualFrame;
32+
import com.oracle.truffle.api.library.CachedLibrary;
3233

3334
import java.util.List;
3435

@@ -327,19 +328,19 @@ PComplex doDD(double r, double phi) {
327328
return rect(r, phi);
328329
}
329330

330-
@Specialization
331-
PComplex doGeneral(VirtualFrame frame, Object r, Object phi,
332-
@Cached CoerceToDoubleNode coerceRToDouble,
333-
@Cached CoerceToDoubleNode coercePhiToDouble) {
334-
return rect(coerceRToDouble.execute(frame, r), coercePhiToDouble.execute(frame, phi));
331+
@Specialization(limit = "2")
332+
PComplex doGeneral(Object r, Object phi,
333+
@CachedLibrary("r") PythonObjectLibrary rLib,
334+
@CachedLibrary("phi") PythonObjectLibrary phiLib) {
335+
return rect(rLib.asJavaDouble(r), phiLib.asJavaDouble(phi));
335336
}
336337

337338
private PComplex rect(double r, double phi) {
338339
// deal with special values
339340
if (!Double.isFinite(r) || !Double.isFinite(phi)) {
340341
// need to raise an exception if r is a nonzero number and phi is infinite
341342
if (r != 0.0 && !Double.isNaN(r) && Double.isInfinite(phi)) {
342-
throw raise(ValueError, "math domain error");
343+
throw raise(ValueError, ErrorMessages.MATH_DOMAIN_ERROR);
343344
}
344345

345346
// if r is +/-infinity and phi is finite but nonzero then
@@ -434,7 +435,7 @@ private double computeRealPart(double real, double imag) {
434435
final double scaleUp = 0x1.0p53;
435436
return Math.log(Math.hypot(ax * scaleUp, ay * scaleUp)) - 53 * ln2;
436437
}
437-
throw raise(ValueError, "math domain error");
438+
throw raise(ValueError, ErrorMessages.MATH_DOMAIN_ERROR);
438439
}
439440
double h = Math.hypot(ax, ay);
440441
if (0.71 <= h && h <= 1.73) {

0 commit comments

Comments
 (0)