Skip to content

Commit 5753d47

Browse files
committed
Use the right node in formatting ints
1 parent e247ebf commit 5753d47

File tree

4 files changed

+19
-16
lines changed

4 files changed

+19
-16
lines changed

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,10 @@
6666
public abstract class PyNumberCheckNode extends PNodeWithContext {
6767
public abstract boolean execute(Node inliningTarget, Object object);
6868

69+
public static boolean executeUncached(Object object) {
70+
return PyNumberCheckNodeGen.getUncached().execute(null, object);
71+
}
72+
6973
@Specialization
7074
static boolean doString(@SuppressWarnings("unused") TruffleString object) {
7175
return false;

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

Lines changed: 5 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
@@ -86,6 +86,10 @@
8686
public abstract class PyNumberIndexNode extends PNodeWithContext {
8787
public abstract Object execute(Frame frame, Node inliningTarget, Object object);
8888

89+
public static Object executeUncached(Object object) {
90+
return PyNumberIndexNodeGen.getUncached().execute(null, null, object);
91+
}
92+
8993
@Specialization
9094
static int doInt(int object) {
9195
return object;

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/call/special/LookupSpecialMethodNode.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2020, 2023, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2020, 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

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/runtime/formatting/FormatProcessor.java

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,6 @@
66
*/
77
package com.oracle.graal.python.runtime.formatting;
88

9-
import static com.oracle.graal.python.nodes.SpecialMethodNames.T___INDEX__;
10-
import static com.oracle.graal.python.nodes.SpecialMethodNames.T___INT__;
11-
import static com.oracle.graal.python.runtime.exception.PythonErrorType.AttributeError;
129
import static com.oracle.graal.python.runtime.exception.PythonErrorType.MemoryError;
1310
import static com.oracle.graal.python.runtime.exception.PythonErrorType.OverflowError;
1411
import static com.oracle.graal.python.runtime.exception.PythonErrorType.TypeError;
@@ -20,12 +17,12 @@
2017

2118
import com.oracle.graal.python.builtins.Python3Core;
2219
import com.oracle.graal.python.builtins.PythonBuiltinClassType;
23-
import com.oracle.graal.python.builtins.objects.PNone;
24-
import com.oracle.graal.python.builtins.objects.PythonAbstractObject;
2520
import com.oracle.graal.python.builtins.objects.function.PKeyword;
2621
import com.oracle.graal.python.builtins.objects.ints.PInt;
2722
import com.oracle.graal.python.builtins.objects.tuple.TupleBuiltins;
2823
import com.oracle.graal.python.lib.PyFloatAsDoubleNode;
24+
import com.oracle.graal.python.lib.PyNumberCheckNode;
25+
import com.oracle.graal.python.lib.PyNumberIndexNode;
2926
import com.oracle.graal.python.lib.PyObjectGetItem;
3027
import com.oracle.graal.python.lib.PyObjectSizeNode;
3128
import com.oracle.graal.python.nodes.ErrorMessages;
@@ -190,20 +187,18 @@ protected final Object asNumber(Object arg, char specType) {
190187
} else if (arg instanceof Boolean) {
191188
// Fast path for simple booleans
192189
return (Boolean) arg ? 1 : 0;
193-
} else if (arg instanceof PythonAbstractObject) {
194-
// Try again with arg.__int__() or __index__() depending on the spec type
190+
} else if (PyNumberCheckNode.executeUncached(arg)) {
195191
try {
196-
TruffleString magicName = useIndexMagicMethod(specType) ? T___INDEX__ : T___INT__;
197-
Object attribute = lookupAttribute(arg, magicName);
198-
if (!(attribute instanceof PNone)) {
199-
return call(attribute, arg);
192+
if (useIndexMagicMethod(specType)) {
193+
return PyNumberIndexNode.executeUncached(arg);
194+
} else {
195+
return CallNode.executeUncached(PythonBuiltinClassType.PInt, arg);
200196
}
201197
} catch (PException e) {
202-
e.expectUncached(AttributeError);
203-
// No __int__/__index__ defined (at Python level)
198+
e.expectUncached(TypeError);
204199
}
205200
}
206-
return arg;
201+
return null;
207202
}
208203

209204
protected double asFloat(Object arg) {

0 commit comments

Comments
 (0)