Skip to content

Commit ef563d4

Browse files
committed
Intrinsify PyMethod_New
1 parent b4ea386 commit ef563d4

File tree

3 files changed

+30
-9
lines changed

3 files changed

+30
-9
lines changed

graalpython/com.oracle.graal.python.test/src/tests/cpyext/test_method.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,9 @@
3636
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
3737
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
3838
# SOFTWARE.
39+
import types
3940

40-
from . import CPyExtType
41+
from . import CPyExtType, CPyExtTestCase, unhandled_error_compare, CPyExtFunction
4142

4243
__dir__ = __file__.rpartition("/")[0]
4344

@@ -101,3 +102,20 @@ def test_methods(self):
101102
assert obj.meth_static_noargs() == (None, None)
102103
assert obj.meth_static_varargs(1, 2, 3) == (None, (1, 2, 3))
103104
assert obj.meth_static_varargs_keywords(1, 2, 3, a=1, b=2) == (None, (1, 2, 3), {'a': 1, 'b': 2})
105+
106+
107+
class TestPyMethod(CPyExtTestCase):
108+
def compile_module(self, name):
109+
type(self).mro()[1].__dict__["test_%s" % name].create_module(name)
110+
super().compile_module(name)
111+
112+
test_PyMethod_New = CPyExtFunction(
113+
lambda args: types.MethodType(*args),
114+
lambda: (
115+
(list.append, 6),
116+
),
117+
resultspec="O",
118+
argspec='OO',
119+
arguments=["PyObject* func", "PyObject* self"],
120+
cmpfunc=unhandled_error_compare
121+
)

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/cext/PythonCextBuiltins.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3883,4 +3883,15 @@ Object tssDelete(Object key,
38833883
return PNone.NONE;
38843884
}
38853885
}
3886+
3887+
@Builtin(name = "PyMethod_New", minNumOfPositionalArgs = 2)
3888+
@GenerateNodeFactory
3889+
abstract static class PyMethodNew extends PythonBinaryBuiltinNode {
3890+
@Specialization
3891+
Object methodNew(Object func, Object self) {
3892+
// Note: CPython also constructs the object directly, without running the constructor or
3893+
// checking the inputs
3894+
return factory().createMethod(self, func);
3895+
}
3896+
}
38863897
}

graalpython/lib-graalpython/python_cext.py

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -105,14 +105,6 @@ def METH_UNSUPPORTED():
105105
raise NotImplementedError("unsupported message type")
106106

107107

108-
def PyMethod_New(func, self):
109-
# TODO we should use the method constructor
110-
# e.g. methodtype(func, self)
111-
def bound_function(*args, **kwargs):
112-
return func(self, *args, **kwargs)
113-
return bound_function
114-
115-
116108
def PyMethodDescr_Check(func):
117109
return 1 if isinstance(func, type(list.append)) else 0
118110

0 commit comments

Comments
 (0)