@@ -4039,9 +4039,16 @@ abstract static class PyStructSequenceInitType2 extends NativeBuiltin {
4039
4039
@ Specialization (limit = "1" )
4040
4040
static int doGeneric (Object klass , String typeName , String typeDoc , Object fieldNamesObj , Object fieldDocsObj , int nInSequence ,
4041
4041
@ CachedLanguage PythonLanguage language ,
4042
+ @ Cached AsPythonObjectNode asPythonObjectNode ,
4042
4043
@ CachedLibrary ("fieldNamesObj" ) InteropLibrary lib ,
4043
- @ Cached (parameters = "true" ) WriteAttributeToObjectNode clearNewNode ,
4044
- @ Cached PRaiseNativeNode raiseNode ) {
4044
+ @ Cached (parameters = "true" ) WriteAttributeToObjectNode clearNewNode ) {
4045
+ return initializeStructType (asPythonObjectNode .execute (klass ), typeName , typeDoc , fieldNamesObj , fieldDocsObj , nInSequence , language , lib , clearNewNode );
4046
+ }
4047
+
4048
+ static int initializeStructType (Object klass , String typeName , String typeDoc , Object fieldNamesObj , Object fieldDocsObj , int nInSequence ,
4049
+ PythonLanguage language ,
4050
+ InteropLibrary lib ,
4051
+ WriteAttributeToObjectNode clearNewNode ) {
4045
4052
// 'fieldNames' and 'fieldDocs' must be of same type; they share the interop lib
4046
4053
assert fieldNamesObj .getClass () == fieldDocsObj .getClass ();
4047
4054
@@ -4076,4 +4083,63 @@ private static String cast(Object object) {
4076
4083
throw CompilerDirectives .shouldNotReachHere ("object is expected to be a Java string" );
4077
4084
}
4078
4085
}
4086
+
4087
+ // directly called without landing function
4088
+ @ Builtin (name = "PyStructSequence_NewType" , minNumOfPositionalArgs = 5 )
4089
+ @ GenerateNodeFactory
4090
+ abstract static class PyStructSequenceNewType extends NativeBuiltin {
4091
+
4092
+ @ Specialization (limit = "1" )
4093
+ Object doGeneric (VirtualFrame frame , String typeName , String typeDoc , Object fieldNamesObj , Object fieldDocsObj , int nInSequence ,
4094
+ @ CachedLanguage PythonLanguage language ,
4095
+ @ Cached ReadAttributeFromObjectNode readTypeBuiltinNode ,
4096
+ @ Cached CallNode callTypeNewNode ,
4097
+ @ CachedLibrary ("fieldNamesObj" ) InteropLibrary lib ,
4098
+ @ Cached (parameters = "true" ) WriteAttributeToObjectNode clearNewNode ,
4099
+ @ Cached GetNativeNullNode getNativeNullNode ,
4100
+ @ Cached ToNewRefNode toNewRefNode ) {
4101
+ try {
4102
+ Object typeBuiltin = readTypeBuiltinNode .execute (getCore ().getBuiltins (), BuiltinNames .TYPE );
4103
+ PTuple bases = factory ().createTuple (new Object []{PythonBuiltinClassType .PTuple });
4104
+ PDict namespace = factory ().createDict (new PKeyword []{new PKeyword (SpecialAttributeNames .__DOC__ , typeDoc )});
4105
+ Object cls = callTypeNewNode .execute (typeBuiltin , typeName , bases , namespace );
4106
+ PyStructSequenceInitType2 .initializeStructType (cls , typeName , typeDoc , fieldNamesObj , fieldDocsObj , nInSequence , language , lib , clearNewNode );
4107
+ return toNewRefNode .execute (cls );
4108
+ } catch (PException e ) {
4109
+ transformToNative (frame , e );
4110
+ return getNativeNullNode .execute ();
4111
+ }
4112
+ }
4113
+ }
4114
+
4115
+ // directly called without landing function
4116
+ @ Builtin (name = "PyStructSequence_New" , minNumOfPositionalArgs = 1 )
4117
+ @ GenerateNodeFactory
4118
+ abstract static class PyStructSequenceNew extends PythonUnaryBuiltinNode {
4119
+
4120
+ @ Specialization
4121
+ Object doGeneric (Object clsPtr ,
4122
+ @ Cached AsPythonObjectNode asPythonObjectNode ,
4123
+ @ Cached ("createForceType()" ) ReadAttributeFromObjectNode readRealSizeNode ,
4124
+ @ Cached CastToJavaIntExactNode castToIntNode ,
4125
+ @ Cached TransformExceptionToNativeNode transformExceptionToNativeNode ,
4126
+ @ Cached GetNativeNullNode getNativeNullNode ,
4127
+ @ Cached ToNewRefNode toNewRefNode ) {
4128
+ try {
4129
+ Object cls = asPythonObjectNode .execute (clsPtr );
4130
+ Object realSizeObj = readRealSizeNode .execute (cls , StructSequence .N_FIELDS );
4131
+ Object res ;
4132
+ if (realSizeObj == PNone .NO_VALUE ) {
4133
+ PRaiseNativeNode .raiseNative (null , SystemError , ErrorMessages .BAD_ARG_TO_INTERNAL_FUNC , PythonUtils .EMPTY_OBJECT_ARRAY , getRaiseNode (), transformExceptionToNativeNode );
4134
+ res = getNativeNullNode .execute ();
4135
+ } else {
4136
+ int realSize = castToIntNode .execute (realSizeObj );
4137
+ res = factory ().createTuple (cls , new Object [realSize ]);
4138
+ }
4139
+ return toNewRefNode .execute (res );
4140
+ } catch (CannotCastException e ) {
4141
+ throw CompilerDirectives .shouldNotReachHere ("attribute 'n_fields' is expected to be a Java int" );
4142
+ }
4143
+ }
4144
+ }
4079
4145
}
0 commit comments