Skip to content

Commit 04ea898

Browse files
committed
add test for method type constructor and fix it
1 parent f36eea8 commit 04ea898

File tree

2 files changed

+34
-4
lines changed

2 files changed

+34
-4
lines changed

graalpython/com.oracle.graal.python.test/src/tests/test_methods.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,3 +70,22 @@ def test_call_builtin_method():
7070
def test_call_builtin_unbound_method():
7171
x = {1: 2}
7272
assert dict.__getitem__.__call__(x, 1) == 2
73+
74+
75+
def test_make_method():
76+
method_type = type(X().foo)
77+
78+
class A():
79+
def __init__(self, *args):
80+
pass
81+
82+
def __call__(self, x, y):
83+
assert isinstance(x, str)
84+
assert isinstance(self, A)
85+
return "A" + str(x) + str(y)
86+
87+
method1 = method_type(A, A)
88+
method2 = method_type(A(), " is ")
89+
90+
assert isinstance(method1(), A)
91+
assert method2(1) == "A is 1", method2(1)

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

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@
140140
import com.oracle.graal.python.nodes.classes.IsSubtypeNode;
141141
import com.oracle.graal.python.nodes.control.GetIteratorNode;
142142
import com.oracle.graal.python.nodes.control.GetNextNode;
143+
import com.oracle.graal.python.nodes.datamodel.IsCallableNode;
143144
import com.oracle.graal.python.nodes.datamodel.IsIndexNode;
144145
import com.oracle.graal.python.nodes.datamodel.IsSequenceNode;
145146
import com.oracle.graal.python.nodes.expression.CastToListNode;
@@ -2378,13 +2379,23 @@ public Object generator(Object args, Object kwargs) {
23782379
@GenerateNodeFactory
23792380
public abstract static class MethodTypeNode extends PythonTernaryBuiltinNode {
23802381
@Specialization
2381-
Object method(LazyPythonClass cls, Object self, PFunction func) {
2382+
Object method(LazyPythonClass cls, PFunction func, Object self) {
23822383
return factory().createMethod(cls, self, func);
23832384
}
23842385

2385-
@Specialization(guards = "isPythonBuiltinClass(cls)")
2386-
Object methodGeneric(@SuppressWarnings("unused") LazyPythonClass cls, Object self, PBuiltinFunction func) {
2387-
return factory().createBuiltinMethod(self, func);
2386+
@Specialization
2387+
Object methodBuiltin(@SuppressWarnings("unused") LazyPythonClass cls, PBuiltinFunction func, Object self) {
2388+
return factory().createMethod(self, func);
2389+
}
2390+
2391+
@Specialization
2392+
Object methodGeneric(@SuppressWarnings("unused") LazyPythonClass cls, Object func, Object self,
2393+
@Cached("create()") IsCallableNode isCallable) {
2394+
if (isCallable.execute(func)) {
2395+
return factory().createMethod(self, func);
2396+
} else {
2397+
throw raise(TypeError, "first argument must be callable");
2398+
}
23882399
}
23892400
}
23902401

0 commit comments

Comments
 (0)