Skip to content

Commit f2ff599

Browse files
committed
Raise NotImplementedError when trying to cast native int
1 parent f5a0090 commit f2ff599

File tree

5 files changed

+40
-9
lines changed

5 files changed

+40
-9
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/lib/PyLongAsDoubleNode.java

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,17 @@
4040
*/
4141
package com.oracle.graal.python.lib;
4242

43+
import static com.oracle.graal.python.builtins.PythonBuiltinClassType.NotImplementedError;
4344
import static com.oracle.graal.python.builtins.PythonBuiltinClassType.TypeError;
4445

46+
import com.oracle.graal.python.builtins.objects.cext.PythonAbstractNativeObject;
4547
import com.oracle.graal.python.builtins.objects.ints.PInt;
4648
import com.oracle.graal.python.nodes.ErrorMessages;
4749
import com.oracle.graal.python.nodes.PNodeWithContext;
4850
import com.oracle.graal.python.nodes.PRaiseNode;
51+
import com.oracle.truffle.api.CompilerDirectives;
52+
import com.oracle.truffle.api.HostCompilerDirectives.InliningCutoff;
53+
import com.oracle.truffle.api.dsl.Cached;
4954
import com.oracle.truffle.api.dsl.Fallback;
5055
import com.oracle.truffle.api.dsl.GenerateCached;
5156
import com.oracle.truffle.api.dsl.GenerateInline;
@@ -83,12 +88,23 @@ static double doLong(long self) {
8388
}
8489

8590
@Specialization
86-
double doPInt(PInt self) {
87-
return self.doubleValueWithOverflow(this);
91+
static double doPInt(Node inliningTarget, PInt self) {
92+
return self.doubleValueWithOverflow(inliningTarget);
93+
}
94+
95+
@Specialization(guards = "check.execute(inliningTarget, self)", limit = "1")
96+
@InliningCutoff
97+
static double doNative(Node inliningTarget, @SuppressWarnings("unused") PythonAbstractNativeObject self,
98+
@SuppressWarnings("unused") @Cached PyLongCheckNode check) {
99+
CompilerDirectives.transferToInterpreterAndInvalidate();
100+
throw PRaiseNode.raiseUncached(inliningTarget, NotImplementedError, ErrorMessages.CASTING_A_NATIVE_INT_OBJECT_IS_NOT_IMPLEMENTED_YET);
88101
}
89102

90103
@Fallback
91-
double fallback(@SuppressWarnings("unused") Object object) {
92-
throw PRaiseNode.getUncached().raise(TypeError, ErrorMessages.INTEGER_REQUIRED);
104+
@InliningCutoff
105+
@SuppressWarnings("unused")
106+
static double fallback(Node inliningTarget, Object object,
107+
@Cached(inline = false) PRaiseNode raiseNode) {
108+
throw raiseNode.raise(TypeError, ErrorMessages.INTEGER_REQUIRED);
93109
}
94110
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/lib/PyLongCheckNode.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2021, 2023, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2021, 2024, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* The Universal Permissive License (UPL), Version 1.0
@@ -46,6 +46,7 @@
4646
import com.oracle.graal.python.nodes.SpecialMethodNames;
4747
import com.oracle.graal.python.nodes.classes.IsSubtypeNode;
4848
import com.oracle.graal.python.nodes.object.GetClassNode;
49+
import com.oracle.truffle.api.HostCompilerDirectives.InliningCutoff;
4950
import com.oracle.truffle.api.dsl.Cached;
5051
import com.oracle.truffle.api.dsl.GenerateCached;
5152
import com.oracle.truffle.api.dsl.GenerateInline;
@@ -91,6 +92,7 @@ static boolean doPInt(@SuppressWarnings("unused") PInt object) {
9192
}
9293

9394
@Specialization
95+
@InliningCutoff
9496
static boolean doGeneric(Node inliningTarget, Object object,
9597
@Cached GetClassNode getClassNode,
9698
@Cached(inline = false) IsSubtypeNode isSubtypeNode,

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/ErrorMessages.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -970,6 +970,7 @@ public abstract class ErrorMessages {
970970
public static final TruffleString SEMAPHORE_RELEASED_TOO_MANY_TIMES = tsLiteral("semaphore or lock released too many times");
971971
public static final TruffleString N_SLOTNAMES_SHOULD_BE_A_LIST_OR_NONE_NOT_P = tsLiteral("%N.__slotnames__ should be a list or None, not %p");
972972
public static final TruffleString COPYREG_SLOTNAMES_DIDN_T_RETURN_A_LIST_OR_NONE = tsLiteral("copyreg._slotnames didn't return a list or None");
973+
public static final TruffleString CASTING_A_NATIVE_INT_OBJECT_IS_NOT_IMPLEMENTED_YET = tsLiteral("casting a native int object is not implemented yet");
973974

974975
// SSL errors
975976
public static final TruffleString SSL_SESSION_CLOSED = tsLiteral("SSL/TLS session closed cleanly.");

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/util/CastToJavaBooleanNode.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2018, 2023, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2018, 2024, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* The Universal Permissive License (UPL), Version 1.0
@@ -40,14 +40,19 @@
4040
*/
4141
package com.oracle.graal.python.nodes.util;
4242

43+
import static com.oracle.graal.python.builtins.PythonBuiltinClassType.NotImplementedError;
44+
4345
import com.oracle.graal.python.builtins.PythonBuiltinClassType;
4446
import com.oracle.graal.python.builtins.objects.cext.PythonNativeObject;
4547
import com.oracle.graal.python.builtins.objects.ints.PInt;
48+
import com.oracle.graal.python.nodes.ErrorMessages;
4649
import com.oracle.graal.python.nodes.PGuards;
4750
import com.oracle.graal.python.nodes.PNodeWithContext;
51+
import com.oracle.graal.python.nodes.PRaiseNode;
4852
import com.oracle.graal.python.nodes.classes.IsSubtypeNode;
4953
import com.oracle.graal.python.nodes.object.GetClassNode;
5054
import com.oracle.truffle.api.CompilerDirectives;
55+
import com.oracle.truffle.api.HostCompilerDirectives;
5156
import com.oracle.truffle.api.dsl.Cached;
5257
import com.oracle.truffle.api.dsl.Cached.Shared;
5358
import com.oracle.truffle.api.dsl.Fallback;
@@ -94,13 +99,14 @@ static boolean doPInt(Node inliningTarget, PInt x,
9499
}
95100

96101
@Specialization
102+
@HostCompilerDirectives.InliningCutoff
97103
static boolean doNativeObject(Node inliningTarget, PythonNativeObject x,
98104
@SuppressWarnings("unused") @Shared("dummy") @Cached InlinedConditionProfile isBoolean,
99105
@Shared @Cached GetClassNode getClassNode,
100106
@Shared @Cached(inline = false) IsSubtypeNode isSubtypeNode) {
101107
if (isSubtypeNode.execute(getClassNode.execute(inliningTarget, x), PythonBuiltinClassType.Boolean)) {
102108
CompilerDirectives.transferToInterpreterAndInvalidate();
103-
throw new RuntimeException("casting a native long object to a Java boolean is not implemented yet");
109+
throw PRaiseNode.raiseUncached(inliningTarget, NotImplementedError, ErrorMessages.CASTING_A_NATIVE_INT_OBJECT_IS_NOT_IMPLEMENTED_YET);
104110
}
105111
// the object's type is not a subclass of 'int'
106112
throw CannotCastException.INSTANCE;

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/util/CastToJavaLongNode.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2018, 2023, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2018, 2024, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* The Universal Permissive License (UPL), Version 1.0
@@ -40,13 +40,18 @@
4040
*/
4141
package com.oracle.graal.python.nodes.util;
4242

43+
import static com.oracle.graal.python.builtins.PythonBuiltinClassType.NotImplementedError;
44+
4345
import com.oracle.graal.python.builtins.PythonBuiltinClassType;
4446
import com.oracle.graal.python.builtins.objects.cext.PythonNativeObject;
47+
import com.oracle.graal.python.nodes.ErrorMessages;
4548
import com.oracle.graal.python.nodes.PGuards;
4649
import com.oracle.graal.python.nodes.PNodeWithContext;
50+
import com.oracle.graal.python.nodes.PRaiseNode;
4751
import com.oracle.graal.python.nodes.classes.IsSubtypeNode;
4852
import com.oracle.graal.python.nodes.object.GetClassNode;
4953
import com.oracle.truffle.api.CompilerDirectives;
54+
import com.oracle.truffle.api.HostCompilerDirectives.InliningCutoff;
5055
import com.oracle.truffle.api.dsl.Cached;
5156
import com.oracle.truffle.api.dsl.Fallback;
5257
import com.oracle.truffle.api.dsl.GenerateCached;
@@ -83,12 +88,13 @@ static long doLong(boolean x) {
8388
}
8489

8590
@Specialization
91+
@InliningCutoff
8692
static long doNativeObject(Node inliningTarget, PythonNativeObject x,
8793
@Cached GetClassNode getClassNode,
8894
@Cached(inline = false) IsSubtypeNode isSubtypeNode) {
8995
if (isSubtypeNode.execute(getClassNode.execute(inliningTarget, x), PythonBuiltinClassType.PInt)) {
9096
CompilerDirectives.transferToInterpreterAndInvalidate();
91-
throw new RuntimeException("casting a native long object to a Java long is not implemented yet");
97+
throw PRaiseNode.raiseUncached(inliningTarget, NotImplementedError, ErrorMessages.CASTING_A_NATIVE_INT_OBJECT_IS_NOT_IMPLEMENTED_YET);
9298
}
9399
// the object's type is not a subclass of 'int'
94100
throw CannotCastException.INSTANCE;

0 commit comments

Comments
 (0)