Skip to content

Commit 16bb601

Browse files
committed
Implement C API function PyDescr_NewClassMethod
1 parent 143ecef commit 16bb601

File tree

3 files changed

+58
-4
lines changed

3 files changed

+58
-4
lines changed

graalpython/com.oracle.graal.python.cext/src/descrobject.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,3 +65,10 @@ UPCALL_ID(PyMethodDescr_Check);
6565
int PyMethodDescr_Check(PyObject* method) {
6666
return UPCALL_CEXT_I(_jls_PyMethodDescr_Check, native_to_java(method));
6767
}
68+
69+
typedef PyObject* (*new_classmethod_fun_t)(PyTypeObject*, void*, void*);
70+
71+
UPCALL_TYPED_ID(PyDescr_NewClassMethod, new_classmethod_fun_t);
72+
PyObject* PyDescr_NewClassMethod(PyTypeObject *type, PyMethodDef *method) {
73+
return _jls_PyDescr_NewClassMethod(native_type_to_java(type), native_pointer_to_java(method->ml_name), native_pointer_to_java(method->ml_meth));
74+
}

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

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@
8989
import com.oracle.graal.python.builtins.modules.ExternalFunctionNodes.SSizeObjArgProcRootNode;
9090
import com.oracle.graal.python.builtins.modules.ExternalFunctionNodes.SetAttrFuncRootNode;
9191
import com.oracle.graal.python.builtins.modules.PythonCextBuiltinsFactory.CheckIterNextResultNodeGen;
92+
import com.oracle.graal.python.builtins.modules.PythonCextBuiltinsFactory.CreateFunctionNodeFactory;
9293
import com.oracle.graal.python.builtins.modules.PythonCextBuiltinsFactory.DefaultCheckFunctionResultNodeGen;
9394
import com.oracle.graal.python.builtins.modules.PythonCextBuiltinsFactory.GetByteArrayNodeGen;
9495
import com.oracle.graal.python.builtins.objects.PNone;
@@ -118,6 +119,7 @@
118119
import com.oracle.graal.python.builtins.objects.cext.capi.CExtNodes.DirectUpcallNode;
119120
import com.oracle.graal.python.builtins.objects.cext.capi.CExtNodes.FastCallArgsToSulongNode;
120121
import com.oracle.graal.python.builtins.objects.cext.capi.CExtNodes.FastCallWithKeywordsArgsToSulongNode;
122+
import com.oracle.graal.python.builtins.objects.cext.capi.CExtNodes.FromCharPointerNode;
121123
import com.oracle.graal.python.builtins.objects.cext.capi.CExtNodes.GetNativeNullNode;
122124
import com.oracle.graal.python.builtins.objects.cext.capi.CExtNodes.MayRaiseNode;
123125
import com.oracle.graal.python.builtins.objects.cext.capi.CExtNodes.ObjectUpcallNode;
@@ -250,6 +252,7 @@
250252
import com.oracle.truffle.api.RootCallTarget;
251253
import com.oracle.truffle.api.TruffleLanguage.ContextReference;
252254
import com.oracle.truffle.api.TruffleLogger;
255+
import com.oracle.truffle.api.dsl.Bind;
253256
import com.oracle.truffle.api.dsl.Cached;
254257
import com.oracle.truffle.api.dsl.Cached.Exclusive;
255258
import com.oracle.truffle.api.dsl.Cached.Shared;
@@ -475,6 +478,8 @@ Object runWithoutCWrapper(PBuiltinFunction descriptor, Object self) {
475478
@TypeSystemReference(PythonArithmeticTypes.class)
476479
abstract static class CreateFunctionNode extends PythonBuiltinNode {
477480

481+
abstract Object execute(String name, Object callable, Object wrapper, Object type);
482+
478483
@Specialization(guards = {"lib.isLazyPythonClass(type)", "isNoValue(wrapper)"}, limit = "3")
479484
static Object doPythonCallableWithoutWrapper(@SuppressWarnings("unused") String name, PythonNativeWrapper callable, @SuppressWarnings("unused") PNone wrapper,
480485
@SuppressWarnings("unused") Object type,
@@ -572,6 +577,11 @@ static boolean isNativeWrapper(Object obj) {
572577
static boolean isDecoratedManagedFunction(Object obj) {
573578
return obj instanceof PyCFunctionDecorator && CApiGuards.isNativeWrapper(((PyCFunctionDecorator) obj).getNativeFunction());
574579
}
580+
581+
public static CreateFunctionNode create() {
582+
return CreateFunctionNodeFactory.create(null);
583+
}
584+
575585
}
576586

577587
@Builtin(name = "PyErr_Restore", minNumOfPositionalArgs = 3)
@@ -3657,7 +3667,42 @@ static long doIt(
36573667
@CachedContext(PythonLanguage.class) PythonContext context) {
36583668
CApiContext nativeContext = context.getCApiContext();
36593669
return nativeContext.getAndIncMaxModuleNumber();
3670+
}
3671+
}
36603672

3673+
// directly called without landing function
3674+
@Builtin(name = "PyDescr_NewClassMethod", minNumOfPositionalArgs = 3)
3675+
@GenerateNodeFactory
3676+
abstract static class PyDescrNewClassMethod extends PythonBuiltinNode {
3677+
3678+
@Specialization(guards = "meth != null")
3679+
@SuppressWarnings("unused")
3680+
Object doPBuiltinFunction(Object typeObj, Object nameObj, Object methObj,
3681+
@Cached AsPythonObjectNode asPythonObjectNode,
3682+
@Bind("asBuiltinFunction(methObj, asPythonObjectNode)") PBuiltinFunction meth,
3683+
@Cached ToNewRefNode toNewRefNode) {
3684+
return toNewRefNode.execute(factory().createClassmethodFromCallableObj(meth));
3685+
}
3686+
3687+
@Specialization(guards = "meth != null")
3688+
Object doNativeCallable(Object type, Object nameObj, Object methObj,
3689+
@SuppressWarnings("unused") @Cached AsPythonObjectNode asPythonObjectNode,
3690+
@Bind("asBuiltinFunction(methObj, asPythonObjectNode)") @SuppressWarnings("unused") PBuiltinFunction meth,
3691+
@Cached FromCharPointerNode fromCharPointerNode,
3692+
@Cached CastToJavaStringNode castToJavaStringNode,
3693+
@Cached CreateFunctionNode createFunctionNode,
3694+
@Cached ToNewRefNode toNewRefNode) {
3695+
String name = castToJavaStringNode.execute(fromCharPointerNode.execute(nameObj));
3696+
Object callable = createFunctionNode.execute(name, methObj, PNone.NONE, type);
3697+
return toNewRefNode.execute(factory().createClassmethodFromCallableObj(callable));
3698+
}
3699+
3700+
static PBuiltinFunction asBuiltinFunction(Object methObj, AsPythonObjectNode asPythonObjectNode) {
3701+
Object object = asPythonObjectNode.execute(methObj);
3702+
if (object instanceof PBuiltinFunction) {
3703+
return (PBuiltinFunction) object;
3704+
}
3705+
return null;
36613706
}
36623707
}
36633708
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/cext/capi/CArrayWrappers.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -102,10 +102,8 @@ public void toNative(
102102
}
103103

104104
/**
105-
* Unlike a
106-
* {@link DynamicObjectNativeWrapper.PythonObjectNativeWrapper}
107-
* object that wraps a Python unicode object, this wrapper let's a Java String look like a
108-
* {@code char*}.
105+
* Unlike a {@link DynamicObjectNativeWrapper.PythonObjectNativeWrapper} object that wraps a
106+
* Python unicode object, this wrapper let's a Java String look like a {@code char*}.
109107
*/
110108
@ExportLibrary(InteropLibrary.class)
111109
@ExportLibrary(NativeTypeLibrary.class)
@@ -115,6 +113,10 @@ public CStringWrapper(String delegate) {
115113
super(delegate);
116114
}
117115

116+
public final String getString(PythonNativeWrapperLibrary lib) {
117+
return ((String) lib.getDelegate(this));
118+
}
119+
118120
@ExportMessage
119121
final long getArraySize(
120122
@CachedLibrary("this") PythonNativeWrapperLibrary lib) {

0 commit comments

Comments
 (0)