Skip to content

Commit 88685eb

Browse files
committed
[GR-24730] Library message for method calls
PullRequest: graalpython/1122
2 parents 85bfd22 + 8bca256 commit 88685eb

32 files changed

+949
-499
lines changed

graalpython/com.oracle.graal.python.test/src/com/oracle/graal/python/test/objects/PythonObjectLibraryTests.java

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -40,24 +40,26 @@
4040
*/
4141
package com.oracle.graal.python.test.objects;
4242

43-
import com.oracle.graal.python.builtins.objects.PNone;
44-
import com.oracle.graal.python.builtins.objects.object.PythonObjectLibrary;
45-
import com.oracle.graal.python.runtime.object.PythonObjectFactory;
43+
import static org.junit.Assert.assertNotNull;
44+
import static org.junit.Assert.assertNotSame;
45+
import static org.junit.Assert.assertSame;
46+
import static org.junit.Assert.fail;
4647

47-
import com.oracle.graal.python.test.PythonTests;
4848
import java.util.concurrent.Callable;
4949

5050
import org.graalvm.polyglot.Context;
5151
import org.graalvm.polyglot.Value;
5252
import org.graalvm.polyglot.proxy.ProxyExecutable;
5353
import org.junit.After;
54-
import static org.junit.Assert.assertNotNull;
55-
import static org.junit.Assert.assertNotSame;
56-
import static org.junit.Assert.assertSame;
57-
import static org.junit.Assert.fail;
5854
import org.junit.Before;
5955
import org.junit.Test;
6056

57+
import com.oracle.graal.python.builtins.objects.PNone;
58+
import com.oracle.graal.python.builtins.objects.object.PythonObjectLibrary;
59+
import com.oracle.graal.python.runtime.object.PythonObjectFactory;
60+
import com.oracle.graal.python.test.PythonTests;
61+
import com.oracle.truffle.api.interop.TruffleObject;
62+
6163
public class PythonObjectLibraryTests extends PythonTests {
6264

6365
private Context context;
@@ -82,8 +84,8 @@ public void testLookupAttribute() {
8284
lookupAttr(() -> 1, "__str__", false);
8385
lookupAttr(() -> (long) 1, "__str__", false);
8486
lookupAttr(() -> "abc", "__str__", false);
85-
86-
lookupAttr(() -> new Object(), "__str__", true);
87+
lookupAttr(() -> new TruffleObject() {
88+
}, "__str__", false);
8789

8890
lookupAttr(() -> PythonObjectFactory.getUncached().createInt(1), "__str__", false);
8991

@@ -99,10 +101,10 @@ private void lookupAttr(Callable<Object> createValue, String attrName, boolean e
99101
PythonObjectLibrary lib = PythonObjectLibrary.getFactory().getUncached();
100102
execInContext(() -> {
101103
Object value = createValue.call();
102-
Object attr = lib.lookupAttribute(value, attrName, false);
104+
Object attr = lib.lookupAttribute(value, null, attrName);
103105
assertAttr(attr, expectNoValue);
104106

105-
attr = lib.lookupAttribute(value, attrName, true);
107+
attr = lib.lookupAttributeOnType(value, attrName);
106108
assertAttr(attr, expectNoValue);
107109
return null;
108110
});

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

Lines changed: 25 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@
6262
import static com.oracle.graal.python.nodes.BuiltinNames.__BUILTINS__;
6363
import static com.oracle.graal.python.nodes.BuiltinNames.__DEBUG__;
6464
import static com.oracle.graal.python.nodes.BuiltinNames.__GRAALPYTHON__;
65+
import static com.oracle.graal.python.nodes.SpecialMethodNames.__ABS__;
6566
import static com.oracle.graal.python.nodes.SpecialMethodNames.__INSTANCECHECK__;
6667
import static com.oracle.graal.python.nodes.SpecialMethodNames.__NEXT__;
6768
import static com.oracle.graal.python.nodes.SpecialMethodNames.__SUBCLASSCHECK__;
@@ -120,7 +121,6 @@
120121
import com.oracle.graal.python.nodes.SpecialMethodNames;
121122
import com.oracle.graal.python.nodes.argument.ReadArgumentNode;
122123
import com.oracle.graal.python.nodes.attributes.DeleteAttributeNode;
123-
import com.oracle.graal.python.nodes.attributes.GetAttributeNode;
124124
import com.oracle.graal.python.nodes.attributes.GetAttributeNode.GetAnyAttributeNode;
125125
import com.oracle.graal.python.nodes.attributes.GetAttributeNode.GetFixedAttributeNode;
126126
import com.oracle.graal.python.nodes.attributes.HasInheritedAttributeNode;
@@ -241,14 +241,15 @@ public double absDouble(double arg) {
241241
return Math.abs(arg);
242242
}
243243

244-
@Specialization
244+
@Specialization(limit = "2")
245245
public Object absObject(VirtualFrame frame, Object object,
246-
@Cached("create(__ABS__)") LookupAndCallUnaryNode callAbsNode) {
247-
Object result = callAbsNode.executeObject(frame, object);
248-
if (result == NO_VALUE) {
246+
@CachedLibrary("object") PythonObjectLibrary lib,
247+
@CachedLibrary(limit = "2") PythonObjectLibrary methodLib) {
248+
Object method = lib.lookupAttributeOnType(object, __ABS__);
249+
if (method == NO_VALUE) {
249250
throw raise(TypeError, ErrorMessages.BAD_OPERAND_FOR, "", "abs()", object);
250251
}
251-
return result;
252+
return methodLib.callUnboundMethod(method, frame, object);
252253
}
253254
}
254255

@@ -311,14 +312,13 @@ String doPI(PInt x) {
311312

312313
@Specialization(replaces = {"doL", "doD", "doPI"})
313314
String doO(VirtualFrame frame, Object x,
314-
@Cached ConditionProfile hasFrame,
315315
@Cached ConditionProfile isMinLong,
316316
@Cached IsSubtypeNode isSubtype,
317317
@CachedLibrary(limit = "getCallSiteInlineCacheMaxDepth()") PythonObjectLibrary lib,
318318
@Cached BranchProfile isInt,
319319
@Cached BranchProfile isLong,
320320
@Cached BranchProfile isPInt) {
321-
Object index = lib.asIndexWithFrame(x, hasFrame, frame);
321+
Object index = lib.asIndexWithFrame(x, frame);
322322
if (isSubtype.execute(lib.getLazyPythonClass(index), PythonBuiltinClassType.PInt)) {
323323
if (index instanceof Boolean || index instanceof Integer) {
324324
isInt.enter();
@@ -467,9 +467,8 @@ public char charFromObject(Object arg) {
467467
public abstract static class HashNode extends PythonUnaryBuiltinNode {
468468
@Specialization(limit = "getCallSiteInlineCacheMaxDepth()")
469469
long hash(VirtualFrame frame, Object object,
470-
@Cached("createBinaryProfile()") ConditionProfile profile,
471470
@CachedLibrary("object") PythonObjectLibrary lib) {
472-
return lib.hashWithFrame(object, profile, frame);
471+
return lib.hashWithFrame(object, frame);
473472
}
474473
}
475474

@@ -1314,9 +1313,8 @@ public Object iter(Object callable, Object sentinel) {
13141313
public abstract static class LenNode extends PythonUnaryBuiltinNode {
13151314
@Specialization(limit = "getCallSiteInlineCacheMaxDepth()")
13161315
public int len(VirtualFrame frame, Object obj,
1317-
@Cached("createBinaryProfile()") ConditionProfile hasFrame,
13181316
@CachedLibrary("obj") PythonObjectLibrary lib) {
1319-
return lib.lengthWithFrame(obj, hasFrame, frame);
1317+
return lib.lengthWithFrame(obj, frame);
13201318
}
13211319
}
13221320

@@ -1572,39 +1570,35 @@ public abstract static class PrintNode extends PythonBuiltinNode {
15721570
private static final String DEFAULT_END = "\n";
15731571
private static final String DEFAULT_SEPARATOR = " ";
15741572
@Child private ReadAttributeFromObjectNode readStdout;
1575-
@Child private GetAttributeNode getWrite = GetAttributeNode.create("write", null);
1576-
@Child private CallNode callWrite = CallNode.create();
1577-
@Child private LookupAndCallUnaryNode callFlushNode;
15781573
@CompilationFinal private Assumption singleContextAssumption;
15791574
@CompilationFinal private PythonModule cachedSys;
15801575

15811576
@Specialization
15821577
PNone printNoKeywords(VirtualFrame frame, Object[] values, @SuppressWarnings("unused") PNone sep, @SuppressWarnings("unused") PNone end, @SuppressWarnings("unused") PNone file,
15831578
@SuppressWarnings("unused") PNone flush,
1584-
@CachedLibrary(limit = "3") PythonObjectLibrary lib) {
1579+
@CachedLibrary(limit = "3") PythonObjectLibrary lib,
1580+
@CachedLibrary(limit = "3") PythonObjectLibrary valueLib) {
15851581
Object stdout = getStdout();
1586-
return printAllGiven(frame, values, DEFAULT_SEPARATOR, DEFAULT_END, stdout, false, lib);
1582+
return printAllGiven(frame, values, DEFAULT_SEPARATOR, DEFAULT_END, stdout, false, lib, valueLib);
15871583
}
15881584

15891585
@Specialization(guards = {"!isNone(file)", "!isNoValue(file)"})
15901586
PNone printAllGiven(VirtualFrame frame, Object[] values, String sep, String end, Object file, boolean flush,
1591-
@CachedLibrary(limit = "3") PythonObjectLibrary lib) {
1587+
@CachedLibrary(limit = "3") PythonObjectLibrary lib,
1588+
@CachedLibrary(limit = "3") PythonObjectLibrary valueLib) {
15921589
int lastValue = values.length - 1;
1593-
Object write = getWrite.executeObject(frame, file);
1590+
Object writeMethod = lib.lookupAttributeStrict(file, frame, "write");
15941591
for (int i = 0; i < lastValue; i++) {
1595-
callWrite.execute(frame, write, lib.asPString(values[i]));
1596-
callWrite.execute(frame, write, sep);
1592+
lib.callObject(writeMethod, frame, valueLib.asPString(values[i]));
1593+
lib.callObject(writeMethod, frame, sep);
15971594
}
15981595
if (lastValue >= 0) {
1599-
callWrite.execute(frame, write, lib.asPString(values[lastValue]));
1596+
lib.callObject(writeMethod, frame, valueLib.asPString(values[lastValue]));
16001597
}
1601-
callWrite.execute(frame, write, end);
1598+
lib.callObject(writeMethod, frame, end);
16021599
if (flush) {
1603-
if (callFlushNode == null) {
1604-
CompilerDirectives.transferToInterpreterAndInvalidate();
1605-
callFlushNode = insert(LookupAndCallUnaryNode.create("flush"));
1606-
}
1607-
callFlushNode.executeObject(frame, file);
1600+
Object flushMethod = lib.lookupAttributeStrict(file, frame, "flush");
1601+
lib.callObject(flushMethod, frame);
16081602
}
16091603
return PNone.NONE;
16101604
}
@@ -1615,7 +1609,8 @@ PNone printGeneric(VirtualFrame frame, Object[] values, Object sepIn, Object end
16151609
@Cached CastToJavaStringNode castEnd,
16161610
@Cached("createIfTrueNode()") CoerceToBooleanNode castFlush,
16171611
@Cached PRaiseNode raiseNode,
1618-
@CachedLibrary(limit = "3") PythonObjectLibrary lib) {
1612+
@CachedLibrary(limit = "4") PythonObjectLibrary lib,
1613+
@CachedLibrary(limit = "3") PythonObjectLibrary valueLib) {
16191614
String sep;
16201615
try {
16211616
sep = sepIn instanceof PNone ? DEFAULT_SEPARATOR : castSep.execute(sepIn);
@@ -1642,7 +1637,7 @@ PNone printGeneric(VirtualFrame frame, Object[] values, Object sepIn, Object end
16421637
} else {
16431638
flush = castFlush.executeBoolean(frame, flushIn);
16441639
}
1645-
return printAllGiven(frame, values, sep, end, file, flush, lib);
1640+
return printAllGiven(frame, values, sep, end, file, flush, lib, valueLib);
16461641
}
16471642

16481643
private Object getStdout() {

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

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -589,11 +589,10 @@ PInt comb(PInt n, PInt k) {
589589

590590
@Specialization
591591
Object comb(VirtualFrame frame, Object n, Object k,
592-
@Cached("createBinaryProfile()") ConditionProfile hasFrame,
593592
@CachedLibrary(limit = "2") PythonObjectLibrary lib,
594593
@Cached CombNode recursiveNode) {
595-
Object nValue = lib.asIndexWithFrame(n, hasFrame, frame);
596-
Object kValue = lib.asIndexWithFrame(k, hasFrame, frame);
594+
Object nValue = lib.asIndexWithFrame(n, frame);
595+
Object kValue = lib.asIndexWithFrame(k, frame);
597596
return recursiveNode.execute(frame, nValue, kValue);
598597
}
599598

@@ -665,11 +664,10 @@ Object perm(VirtualFrame frame, Object n, @SuppressWarnings("unused") PNone k,
665664

666665
@Specialization(guards = "!isPNone(k)")
667666
Object perm(VirtualFrame frame, Object n, Object k,
668-
@Cached("createBinaryProfile()") ConditionProfile hasFrame,
669667
@CachedLibrary(limit = "2") PythonObjectLibrary lib,
670668
@Cached PermNode recursiveNode) {
671-
Object nValue = lib.asIndexWithFrame(n, hasFrame, frame);
672-
Object kValue = lib.asIndexWithFrame(k, hasFrame, frame);
669+
Object nValue = lib.asIndexWithFrame(n, frame);
670+
Object kValue = lib.asIndexWithFrame(k, frame);
673671
return recursiveNode.execute(frame, nValue, kValue);
674672
}
675673

@@ -1433,11 +1431,10 @@ int gcd(@SuppressWarnings("unused") PInt x, @SuppressWarnings("unused") double y
14331431

14341432
@Specialization(guards = "!isNumber(x) || !isNumber(y)")
14351433
Object gcd(VirtualFrame frame, Object x, Object y,
1436-
@Cached("createBinaryProfile()") ConditionProfile hasFrame,
14371434
@CachedLibrary(limit = "2") PythonObjectLibrary lib,
14381435
@Cached("create()") GcdNode recursiveNode) {
1439-
Object xValue = lib.asIndexWithFrame(x, hasFrame, frame);
1440-
Object yValue = lib.asIndexWithFrame(y, hasFrame, frame);
1436+
Object xValue = lib.asIndexWithFrame(x, frame);
1437+
Object yValue = lib.asIndexWithFrame(y, frame);
14411438
return recursiveNode.execute(frame, xValue, yValue);
14421439
}
14431440

@@ -2615,10 +2612,9 @@ Object isqrtPInt(PInt x,
26152612

26162613
@Specialization(guards = "!isInteger(x)")
26172614
Object doGeneral(VirtualFrame frame, Object x,
2618-
@Cached("createBinaryProfile()") ConditionProfile hasFrame,
26192615
@CachedLibrary(limit = "1") PythonObjectLibrary lib,
26202616
@Cached IsqrtNode recursiveNode) {
2621-
return recursiveNode.execute(frame, lib.asIndexWithFrame(x, hasFrame, frame));
2617+
return recursiveNode.execute(frame, lib.asIndexWithFrame(x, frame));
26222618
}
26232619

26242620
@TruffleBoundary

0 commit comments

Comments
 (0)