40
40
*/
41
41
package com .oracle .graal .python .builtins .modules .ctypes ;
42
42
43
+ import static com .oracle .graal .python .nodes .ErrorMessages .ATTR_NAME_MUST_BE_STRING ;
43
44
import static com .oracle .graal .python .nodes .SpecialMethodNames .J___NEW__ ;
44
45
import static com .oracle .graal .python .nodes .SpecialMethodNames .J___SETATTR__ ;
45
46
import static com .oracle .graal .python .util .PythonUtils .TS_ENCODING ;
56
57
import com .oracle .graal .python .nodes .attributes .WriteAttributeToObjectNode ;
57
58
import com .oracle .graal .python .nodes .function .PythonBuiltinBaseNode ;
58
59
import com .oracle .graal .python .nodes .function .builtins .PythonTernaryBuiltinNode ;
60
+ import com .oracle .graal .python .nodes .util .CannotCastException ;
61
+ import com .oracle .graal .python .nodes .util .CastToTruffleStringNode ;
59
62
import com .oracle .truffle .api .dsl .Cached ;
63
+ import com .oracle .truffle .api .dsl .Cached .Shared ;
60
64
import com .oracle .truffle .api .dsl .GenerateNodeFactory ;
61
65
import com .oracle .truffle .api .dsl .NodeFactory ;
62
66
import com .oracle .truffle .api .dsl .Specialization ;
63
67
import com .oracle .truffle .api .frame .VirtualFrame ;
64
68
import com .oracle .truffle .api .strings .TruffleString ;
65
69
66
70
@ CoreFunctions (extendClasses = PythonBuiltinClassType .PyCStructType )
67
- public class PyCStructTypeBuiltins extends PythonBuiltins {
71
+ public final class PyCStructTypeBuiltins extends PythonBuiltins {
68
72
69
73
@ Override
70
74
protected List <? extends NodeFactory <? extends PythonBuiltinBaseNode >> getNodeFactories () {
@@ -81,15 +85,30 @@ protected abstract static class NewNode extends StructUnionTypeNewNode {
81
85
protected abstract static class SetattrNode extends PythonTernaryBuiltinNode {
82
86
83
87
@ Specialization
84
- protected PNone doStringKey (VirtualFrame frame , Object object , TruffleString key , Object value ,
85
- @ Cached TruffleString .EqualNode equalNode ,
86
- @ Cached WriteAttributeToObjectNode writeNode ,
87
- @ Cached PyCStructUnionTypeUpdateStgDict updateStgDict ) {
88
+ PNone doStringKey (VirtualFrame frame , Object object , TruffleString key , Object value ,
89
+ @ Shared @ Cached TruffleString .EqualNode equalNode ,
90
+ @ Shared @ Cached WriteAttributeToObjectNode writeNode ,
91
+ @ Shared @ Cached PyCStructUnionTypeUpdateStgDict updateStgDict ) {
88
92
writeNode .execute (object , key , value );
89
93
if (equalNode .execute (key , StructUnionTypeBuiltins .T__fields_ , TS_ENCODING )) {
90
94
updateStgDict .execute (frame , object , value , true , factory ());
91
95
}
92
96
return PNone .NONE ;
93
97
}
98
+
99
+ @ Specialization (replaces = "doStringKey" )
100
+ PNone doGenericKey (VirtualFrame frame , Object object , Object keyObject , Object value ,
101
+ @ Shared @ Cached TruffleString .EqualNode equalNode ,
102
+ @ Shared @ Cached WriteAttributeToObjectNode writeNode ,
103
+ @ Shared @ Cached PyCStructUnionTypeUpdateStgDict updateStgDict ,
104
+ @ Cached CastToTruffleStringNode castKeyToStringNode ) {
105
+ TruffleString key ;
106
+ try {
107
+ key = castKeyToStringNode .execute (keyObject );
108
+ } catch (CannotCastException e ) {
109
+ throw raise (PythonBuiltinClassType .TypeError , ATTR_NAME_MUST_BE_STRING , keyObject );
110
+ }
111
+ return doStringKey (frame , object , key , value , equalNode , writeNode , updateStgDict );
112
+ }
94
113
}
95
114
}
0 commit comments