26
26
27
27
package com .oracle .graal .python .builtins .objects .type ;
28
28
29
+ import static com .oracle .graal .python .builtins .objects .str .StringUtils .containsNullCharacter ;
29
30
import static com .oracle .graal .python .nodes .SpecialAttributeNames .__BASES__ ;
30
31
import static com .oracle .graal .python .nodes .SpecialAttributeNames .__BASE__ ;
31
32
import static com .oracle .graal .python .nodes .SpecialAttributeNames .__BASICSIZE__ ;
79
80
import com .oracle .graal .python .builtins .objects .mappingproxy .PMappingproxy ;
80
81
import com .oracle .graal .python .builtins .objects .object .PythonObject ;
81
82
import com .oracle .graal .python .builtins .objects .object .PythonObjectLibrary ;
83
+ import com .oracle .graal .python .builtins .objects .str .PString ;
82
84
import com .oracle .graal .python .builtins .objects .tuple .PTuple ;
83
85
import com .oracle .graal .python .builtins .objects .type .TypeBuiltinsFactory .CallNodeFactory ;
84
86
import com .oracle .graal .python .builtins .objects .type .TypeNodes .GetMroNode ;
@@ -820,9 +822,8 @@ String getNameBuiltin(PythonBuiltinClass cls, @SuppressWarnings("unused") PNone
820
822
}
821
823
822
824
@ Specialization (guards = {"isNoValue(value)" , "!isPythonBuiltinClass(cls)" })
823
- Object getName (PythonClass cls , @ SuppressWarnings ("unused" ) PNone value ,
824
- @ Cached ("create()" ) ReadAttributeFromObjectNode getName ) {
825
- return getName .execute (cls , __NAME__ );
825
+ Object getName (PythonClass cls , @ SuppressWarnings ("unused" ) PNone value ) {
826
+ return cls .getName ();
826
827
}
827
828
828
829
@ Specialization (guards = "!isNoValue(value)" )
@@ -836,9 +837,28 @@ Object setName(@SuppressWarnings("unused") PythonBuiltinClass cls, @SuppressWarn
836
837
}
837
838
838
839
@ Specialization (guards = {"!isNoValue(value)" , "!isPythonBuiltinClass(cls)" })
840
+ Object setName (PythonClass cls , String value ) {
841
+ if (containsNullCharacter (value )) {
842
+ throw raise (PythonBuiltinClassType .ValueError , "type name must not contain null characters" );
843
+ }
844
+ cls .setName (value );
845
+ return PNone .NONE ;
846
+ }
847
+
848
+ @ Specialization (guards = {"!isNoValue(value)" , "!isPythonBuiltinClass(cls)" , "isBuiltinString(value, profile)" }, limit = "1" )
849
+ Object setName (PythonClass cls , PString value ,
850
+ @ Cached .Shared ("builtinStringProfile" ) @ Cached IsBuiltinClassProfile profile ) {
851
+ if (containsNullCharacter (value )) {
852
+ throw raise (PythonBuiltinClassType .ValueError , "type name must not contain null characters" );
853
+ }
854
+ cls .setName (value .getValue ());
855
+ return PNone .NONE ;
856
+ }
857
+
858
+ @ Specialization (guards = {"!isNoValue(value)" , "!isPythonBuiltinClass(cls)" , "!isBuiltinString(value, profile)" }, limit = "1" )
839
859
Object setName (PythonClass cls , Object value ,
840
- @ Cached ( "create() " ) WriteAttributeToObjectNode setName ) {
841
- return setName . execute ( cls , __NAME__ , value );
860
+ @ Cached . Shared ( "builtinStringProfile " ) @ Cached IsBuiltinClassProfile profile ) {
861
+ throw raise ( PythonBuiltinClassType . TypeError , "can only assign string to %p.__name__, not '%p'" , cls , value );
842
862
}
843
863
844
864
@ Specialization (guards = "isNoValue(value)" )
@@ -928,13 +948,12 @@ String getName(PythonBuiltinClassType cls, @SuppressWarnings("unused") PNone val
928
948
929
949
@ Specialization (guards = "isNoValue(value)" )
930
950
String getName (PythonBuiltinClass cls , @ SuppressWarnings ("unused" ) PNone value ) {
931
- return cls .getName ();
951
+ return cls .getQualName ();
932
952
}
933
953
934
954
@ Specialization (guards = {"isNoValue(value)" , "!isPythonBuiltinClass(cls)" })
935
- Object getName (PythonClass cls , @ SuppressWarnings ("unused" ) PNone value ,
936
- @ Cached ("create()" ) ReadAttributeFromObjectNode getName ) {
937
- return getName .execute (cls , __QUALNAME__ );
955
+ Object getName (PythonClass cls , @ SuppressWarnings ("unused" ) PNone value ) {
956
+ return cls .getQualName ();
938
957
}
939
958
940
959
@ Specialization (guards = "!isNoValue(value)" )
@@ -943,9 +962,22 @@ Object setName(@SuppressWarnings("unused") PythonBuiltinClass cls, @SuppressWarn
943
962
}
944
963
945
964
@ Specialization (guards = {"!isNoValue(value)" , "!isPythonBuiltinClass(cls)" })
965
+ Object setName (PythonClass cls , String value ) {
966
+ cls .setQualName (value );
967
+ return PNone .NONE ;
968
+ }
969
+
970
+ @ Specialization (guards = {"!isNoValue(value)" , "!isPythonBuiltinClass(cls)" , "isBuiltinString(value, profile)" }, limit = "1" )
971
+ Object setName (PythonClass cls , PString value ,
972
+ @ Cached .Shared ("builtinStringProfile" ) @ Cached IsBuiltinClassProfile profile ) {
973
+ cls .setQualName (value .getValue ());
974
+ return PNone .NONE ;
975
+ }
976
+
977
+ @ Specialization (guards = {"!isNoValue(value)" , "!isPythonBuiltinClass(cls)" , "!isBuiltinString(value, profile)" }, limit = "1" )
946
978
Object setName (PythonClass cls , Object value ,
947
- @ Cached ( "create() " ) WriteAttributeToObjectNode setName ) {
948
- return setName . execute ( cls , __QUALNAME__ , value );
979
+ @ Cached . Shared ( "builtinStringProfile " ) @ Cached IsBuiltinClassProfile profile ) {
980
+ throw raise ( PythonBuiltinClassType . TypeError , "can only assign string to %p.__qualname__, not '%p'" , cls , value );
949
981
}
950
982
951
983
@ Specialization (guards = "isNoValue(value)" )
0 commit comments