Skip to content

Commit bd86342

Browse files
committed
throw TypeError instead of AttributeError if obj has no __round__ method
1 parent 3524552 commit bd86342

File tree

1 file changed

+18
-4
lines changed

1 file changed

+18
-4
lines changed

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

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1803,14 +1803,28 @@ public Object asciiGeneric(VirtualFrame frame, Object obj,
18031803
public abstract static class RoundNode extends PythonBuiltinNode {
18041804
@Specialization(limit = "1")
18051805
Object round(VirtualFrame frame, Object x, @SuppressWarnings("unused") PNone n,
1806-
@SuppressWarnings("unused") @CachedLibrary("x") PythonObjectLibrary lib) {
1807-
return lib.lookupAndCallSpecialMethod(x, frame, __ROUND__);
1806+
@CachedLibrary("x") PythonObjectLibrary lib,
1807+
@CachedLibrary(limit = "1") PythonObjectLibrary methodLib,
1808+
@Cached BranchProfile noRound) {
1809+
Object method = lib.lookupAttributeOnType(x, __ROUND__);
1810+
if (method == PNone.NO_VALUE) {
1811+
noRound.enter();
1812+
throw raise(TypeError, ErrorMessages.TYPE_DOESNT_DEFINE_METHOD, x, __ROUND__);
1813+
}
1814+
return methodLib.callUnboundMethod(method, frame, x);
18081815
}
18091816

18101817
@Specialization(guards = "!isNoValue(n)", limit = "1")
18111818
Object round(VirtualFrame frame, Object x, Object n,
1812-
@SuppressWarnings("unused") @CachedLibrary("x") PythonObjectLibrary lib) {
1813-
return lib.lookupAndCallSpecialMethod(x, frame, __ROUND__, n);
1819+
@CachedLibrary("x") PythonObjectLibrary lib,
1820+
@CachedLibrary(limit = "1") PythonObjectLibrary methodLib,
1821+
@Cached BranchProfile noRound) {
1822+
Object method = lib.lookupAttributeOnType(x, __ROUND__);
1823+
if (method == PNone.NO_VALUE) {
1824+
noRound.enter();
1825+
throw raise(TypeError, ErrorMessages.TYPE_DOESNT_DEFINE_METHOD, x, __ROUND__);
1826+
}
1827+
return methodLib.callUnboundMethod(method, frame, x, n);
18141828
}
18151829
}
18161830

0 commit comments

Comments
 (0)