76
76
import com .oracle .graal .python .builtins .objects .function .PBuiltinFunction ;
77
77
import com .oracle .graal .python .builtins .objects .function .PFunction ;
78
78
import com .oracle .graal .python .builtins .objects .function .PKeyword ;
79
+ import com .oracle .graal .python .builtins .objects .getsetdescriptor .DescriptorDeleteMarker ;
79
80
import com .oracle .graal .python .builtins .objects .getsetdescriptor .HiddenPythonKey ;
80
81
import com .oracle .graal .python .builtins .objects .list .PList ;
81
82
import com .oracle .graal .python .builtins .objects .object .PythonObject ;
96
97
import com .oracle .graal .python .nodes .ErrorMessages ;
97
98
import com .oracle .graal .python .nodes .PGuards ;
98
99
import com .oracle .graal .python .nodes .SpecialAttributeNames ;
100
+ import static com .oracle .graal .python .nodes .SpecialAttributeNames .__DOC__ ;
99
101
import com .oracle .graal .python .nodes .argument .positional .PositionalArgumentsNode ;
100
102
import com .oracle .graal .python .nodes .attributes .GetAttributeNode .GetFixedAttributeNode ;
101
103
import com .oracle .graal .python .nodes .attributes .LookupAttributeInMRONode ;
120
122
import com .oracle .graal .python .nodes .util .CannotCastException ;
121
123
import com .oracle .graal .python .nodes .util .CastToJavaStringNode ;
122
124
import com .oracle .graal .python .nodes .util .SplitArgsNode ;
125
+ import com .oracle .graal .python .runtime .PythonCore ;
123
126
import com .oracle .graal .python .runtime .exception .PException ;
124
127
import com .oracle .graal .python .runtime .exception .PythonErrorType ;
125
128
import com .oracle .truffle .api .CompilerAsserts ;
@@ -152,12 +155,22 @@ public class TypeBuiltins extends PythonBuiltins {
152
155
public static final HiddenPythonKey TYPE_FREE = new HiddenPythonKey ("__free__" );
153
156
public static final HiddenPythonKey TYPE_FLAGS = new HiddenPythonKey (__FLAGS__ );
154
157
public static final HiddenPythonKey TYPE_VECTORCALL_OFFSET = new HiddenPythonKey ("__vectorcall_offset__" );
158
+ private static final HiddenPythonKey TYPE_DOC = new HiddenPythonKey (__DOC__ );
155
159
156
160
@ Override
157
161
protected List <? extends NodeFactory <? extends PythonBuiltinBaseNode >> getNodeFactories () {
158
162
return TypeBuiltinsFactory .getFactories ();
159
163
}
160
164
165
+ @ Override
166
+ public void initialize (PythonCore core ) {
167
+ super .initialize (core );
168
+ builtinConstants .put (TYPE_DOC , //
169
+ "type(object_or_name, bases, dict)\n " + //
170
+ "type(object) -> the object's type\n " + //
171
+ "type(name, bases, dict) -> a new type" );
172
+ }
173
+
161
174
@ Builtin (name = __REPR__ , minNumOfPositionalArgs = 1 )
162
175
@ GenerateNodeFactory
163
176
@ ImportStatic (SpecialAttributeNames .class )
@@ -180,6 +193,73 @@ private static String concat(Object moduleName, Object qualName) {
180
193
}
181
194
}
182
195
196
+ @ Builtin (name = __DOC__ , minNumOfPositionalArgs = 1 , maxNumOfPositionalArgs = 2 , isGetter = true , isSetter = true , allowsDelete = true )
197
+ @ GenerateNodeFactory
198
+ @ ImportStatic (SpecialAttributeNames .class )
199
+ public abstract static class DocNode extends PythonBinaryBuiltinNode {
200
+
201
+ @ Specialization
202
+ Object getDoc (PythonBuiltinClassType self , @ SuppressWarnings ("unused" ) PNone value ) {
203
+ return getDoc (getCore ().lookupType (self ), value );
204
+ }
205
+
206
+ @ Specialization
207
+ static Object getDoc (PythonBuiltinClass self , @ SuppressWarnings ("unused" ) PNone value ) {
208
+ // see type.c#type_get_doc()
209
+ if (IsBuiltinClassProfile .getUncached ().profileClass (self , PythonBuiltinClassType .PythonClass )) {
210
+ return ((PythonObject ) self ).getAttribute (TYPE_DOC );
211
+ } else {
212
+ return ((PythonObject ) self ).getAttribute (__DOC__ );
213
+ }
214
+ }
215
+
216
+ @ Specialization (guards = "!isAnyBuiltinClass(self)" )
217
+ Object getDoc (VirtualFrame frame , PythonClass self , @ SuppressWarnings ("unused" ) PNone value ) {
218
+ // see type.c#type_get_doc()
219
+ Object res = self .getAttribute (__DOC__ );
220
+ Object resClass = PythonObjectLibrary .getUncached ().getLazyPythonClass (res );
221
+ Object get = LookupAttributeInMRONode .Dynamic .getUncached ().execute (resClass , __GET__ );
222
+ if (PGuards .isCallable (get )) {
223
+ return CallTernaryMethodNode .getUncached ().execute (frame , get , res , PNone .NONE , self );
224
+ }
225
+ return res ;
226
+ }
227
+
228
+ protected boolean isAnyBuiltinClass (PythonClass klass ) {
229
+ return IsBuiltinClassProfile .getUncached ().profileIsAnyBuiltinClass (klass );
230
+ }
231
+
232
+ @ Specialization
233
+ static Object getDoc (PythonNativeClass self , @ SuppressWarnings ("unused" ) PNone value ) {
234
+ return ReadAttributeFromObjectNode .getUncached ().execute (self , __DOC__ );
235
+ }
236
+
237
+ @ Specialization (guards = {"!isNoValue(value)" , "!isDeleteMarker(value)" })
238
+ Object setDoc (PythonClass self , Object value ) {
239
+ self .setAttribute (__DOC__ , value );
240
+ return PNone .NO_VALUE ;
241
+ }
242
+
243
+ @ Specialization (guards = {"!isNoValue(value)" , "isBuiltin.profileIsAnyBuiltinClass(self)" })
244
+ Object doc (Object self , @ SuppressWarnings ("unused" ) Object value ,
245
+ @ SuppressWarnings ("unused" ) @ Cached IsBuiltinClassProfile isBuiltin ,
246
+ @ Cached GetNameNode getName ) {
247
+ throw raise (PythonErrorType .TypeError , ErrorMessages .CANT_SET_S_S , getName .execute (self ), __DOC__ );
248
+ }
249
+
250
+ @ Specialization (guards = {"!isNoValue(value)" , "!isDeleteMarker(value)" })
251
+ Object doc (PythonClass self , Object value ) {
252
+ self .setAttribute (__DOC__ , value );
253
+ return PNone .NO_VALUE ;
254
+ }
255
+
256
+ @ Specialization
257
+ Object doc (Object self , @ SuppressWarnings ("unused" ) DescriptorDeleteMarker marker ,
258
+ @ Cached GetNameNode getName ) {
259
+ throw raise (PythonErrorType .TypeError , ErrorMessages .CANNOT_DELETE_ATTRIBUTE , getName .execute (self ), __DOC__ );
260
+ }
261
+ }
262
+
183
263
@ Builtin (name = __MRO__ , minNumOfPositionalArgs = 1 , isGetter = true )
184
264
@ GenerateNodeFactory
185
265
abstract static class MroAttrNode extends PythonBuiltinNode {
0 commit comments