11/*
2- * Copyright (c) 2021, 2022, Oracle and/or its affiliates. All rights reserved.
2+ * Copyright (c) 2022, Oracle and/or its affiliates. All rights reserved.
33 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44 *
55 * The Universal Permissive License (UPL), Version 1.0
4040 */
4141package com .oracle .graal .python .builtins .modules .cext ;
4242
43+ import static com .oracle .graal .python .builtins .objects .cext .common .CExtContext .isClassOrStaticMethod ;
4344
45+ import com .oracle .graal .python .annotations .ArgumentClinic ;
4446import java .util .List ;
4547import com .oracle .graal .python .builtins .Builtin ;
4648import com .oracle .graal .python .builtins .CoreFunctions ;
4749import com .oracle .graal .python .builtins .Python3Core ;
4850import com .oracle .graal .python .builtins .PythonBuiltinClassType ;
4951import com .oracle .graal .python .builtins .PythonBuiltins ;
52+ import com .oracle .graal .python .builtins .modules .BuiltinConstructors ;
5053import com .oracle .graal .python .builtins .modules .BuiltinFunctions .IsInstanceNode ;
54+ import com .oracle .graal .python .builtins .modules .cext .PythonCextDescrBuiltinsClinicProviders .PyDescrNewClassMethodClinicProviderGen ;
55+ import com .oracle .graal .python .builtins .modules .cext .PythonCextDescrBuiltinsClinicProviders .PyDescrNewGetSetNodeClinicProviderGen ;
56+ import com .oracle .graal .python .builtins .objects .cext .capi .CExtNodes ;
57+ import com .oracle .graal .python .builtins .objects .getsetdescriptor .GetSetDescriptor ;
5158import com .oracle .graal .python .nodes .function .PythonBuiltinBaseNode ;
59+ import com .oracle .graal .python .nodes .function .builtins .PythonClinicBuiltinNode ;
5260import com .oracle .graal .python .nodes .function .builtins .PythonUnaryBuiltinNode ;
61+ import com .oracle .graal .python .nodes .function .builtins .clinic .ArgumentClinicProvider ;
5362import com .oracle .truffle .api .dsl .Cached ;
5463import com .oracle .truffle .api .dsl .GenerateNodeFactory ;
5564import com .oracle .truffle .api .dsl .NodeFactory ;
@@ -70,20 +79,74 @@ public void initialize(Python3Core core) {
7079 super .initialize (core );
7180 }
7281
73- //def PyMethodDescr_Check(func):
74- // return 1 if isinstance(func, type(list.append)) else 0
75-
82+ @ Builtin (name = "PyDictProxy_New" , minNumOfPositionalArgs = 1 )
83+ @ GenerateNodeFactory
84+ public abstract static class PyDictProxyNewNode extends PythonUnaryBuiltinNode {
85+ @ Specialization
86+ public static Object values (VirtualFrame frame , Object obj ,
87+ @ Cached BuiltinConstructors .MappingproxyNode mappingNode ) {
88+ return mappingNode .execute (frame , PythonBuiltinClassType .PMappingproxy , obj );
89+ }
90+ }
91+
92+ // directly called without landing function
93+ @ Builtin (name = "PyDescr_NewGetSet" , minNumOfPositionalArgs = 6 , parameterNames = {"name" , "cls" , "getter" , "setter" , "doc" , "closure" })
94+ @ ArgumentClinic (name = "name" , conversion = ArgumentClinic .ClinicConversion .String )
95+ @ GenerateNodeFactory
96+ abstract static class PyDescrNewGetSetNode extends PythonClinicBuiltinNode {
97+ @ Override
98+ protected ArgumentClinicProvider getArgumentClinic () {
99+ return PyDescrNewGetSetNodeClinicProviderGen .INSTANCE ;
100+ }
101+
102+ @ Specialization
103+ Object doNativeCallable (String name , Object cls , Object getter , Object setter , Object doc , Object closure ,
104+ @ Cached PythonCextBuiltins .CreateGetSetNode createGetSetNode ,
105+ @ Cached CExtNodes .ToSulongNode toSulongNode ) {
106+ GetSetDescriptor descr = createGetSetNode .execute (name , cls , getter , setter , doc , closure ,
107+ getLanguage (), factory ());
108+ return toSulongNode .execute (descr );
109+ }
110+ }
111+
112+ // directly called without landing function
113+ @ Builtin (name = "PyDescr_NewClassMethod" , minNumOfPositionalArgs = 6 , parameterNames = {"name" , "doc" , "flags" , "wrapper" , "cfunc" , "primary" })
114+ @ ArgumentClinic (name = "name" , conversion = ArgumentClinic .ClinicConversion .String )
115+ @ GenerateNodeFactory
116+ abstract static class PyDescrNewClassMethod extends PythonClinicBuiltinNode {
117+ @ Override
118+ protected ArgumentClinicProvider getArgumentClinic () {
119+ return PyDescrNewClassMethodClinicProviderGen .INSTANCE ;
120+ }
121+
122+ @ Specialization
123+ Object doNativeCallable (String name , Object doc , int flags , Object wrapper , Object methObj , Object primary ,
124+ @ Cached CExtNodes .AsPythonObjectNode asPythonObjectNode ,
125+ @ Cached PythonCextBuiltins .NewClassMethodNode newClassMethodNode ,
126+ @ Cached CExtNodes .ToNewRefNode newRefNode ) {
127+ Object type = asPythonObjectNode .execute (primary );
128+ Object func = newClassMethodNode .execute (name , methObj , flags , wrapper , type , doc , factory ());
129+ if (!isClassOrStaticMethod (flags )) {
130+ /*
131+ * NewClassMethodNode only wraps method with METH_CLASS and METH_STATIC set but we
132+ * need to do so here.
133+ */
134+ func = factory ().createClassmethodFromCallableObj (func );
135+ }
136+ return newRefNode .execute (func );
137+ }
138+ }
139+
76140 @ Builtin (name = "PyMethodDescr_Check" , minNumOfPositionalArgs = 1 )
77141 @ GenerateNodeFactory
78142 public abstract static class PyMethodDescrCheckNode extends PythonUnaryBuiltinNode {
79143
80144 @ SuppressWarnings ("unused" )
81145 @ Specialization
82- int check (VirtualFrame frame , Object func ,
83- @ Cached IsInstanceNode isInstanceNode ) {
146+ static int check (VirtualFrame frame , Object func ,
147+ @ Cached IsInstanceNode isInstanceNode ) {
84148 return isInstanceNode .executeWith (frame , func , PythonBuiltinClassType .PBuiltinFunction ) ? 1 : 0 ;
85149 }
86150 }
87151
88-
89152}
0 commit comments