Skip to content

Commit 0044d53

Browse files
committed
TypeBuiltins: setName and setQualName collapse multiple specializations by using the CastToJavaStringNode
1 parent 2058f60 commit 0044d53

File tree

2 files changed

+25
-46
lines changed

2 files changed

+25
-46
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/str/StringUtils.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -206,8 +206,4 @@ public static String strip(String str, String chars, StripKind stripKind) {
206206
public static boolean containsNullCharacter(String value) {
207207
return value.indexOf(0) > 0;
208208
}
209-
210-
public static boolean containsNullCharacter(PString value) {
211-
return containsNullCharacter(value.getValue());
212-
}
213209
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/type/TypeBuiltins.java

Lines changed: 25 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
import static com.oracle.graal.python.nodes.SpecialAttributeNames.__MRO__;
3939
import static com.oracle.graal.python.nodes.SpecialAttributeNames.__NAME__;
4040
import static com.oracle.graal.python.nodes.SpecialAttributeNames.__QUALNAME__;
41+
import static com.oracle.graal.python.nodes.SpecialMethodNames.MRO;
4142
import static com.oracle.graal.python.nodes.SpecialMethodNames.__ALLOC__;
4243
import static com.oracle.graal.python.nodes.SpecialMethodNames.__CALL__;
4344
import static com.oracle.graal.python.nodes.SpecialMethodNames.__DELETE__;
@@ -76,13 +77,16 @@
7677
import com.oracle.graal.python.builtins.objects.function.PBuiltinFunction;
7778
import com.oracle.graal.python.builtins.objects.function.PFunction;
7879
import com.oracle.graal.python.builtins.objects.function.PKeyword;
80+
import com.oracle.graal.python.builtins.objects.getsetdescriptor.HiddenPythonKey;
7981
import com.oracle.graal.python.builtins.objects.list.PList;
8082
import com.oracle.graal.python.builtins.objects.mappingproxy.PMappingproxy;
8183
import com.oracle.graal.python.builtins.objects.object.PythonObject;
8284
import com.oracle.graal.python.builtins.objects.object.PythonObjectLibrary;
83-
import com.oracle.graal.python.builtins.objects.str.PString;
8485
import com.oracle.graal.python.builtins.objects.tuple.PTuple;
8586
import com.oracle.graal.python.builtins.objects.type.TypeBuiltinsFactory.CallNodeFactory;
87+
import com.oracle.graal.python.builtins.objects.type.TypeNodes.CheckCompatibleForAssigmentNode;
88+
import com.oracle.graal.python.builtins.objects.type.TypeNodes.GetBaseClassNode;
89+
import com.oracle.graal.python.builtins.objects.type.TypeNodes.GetBestBaseClassNode;
8690
import com.oracle.graal.python.builtins.objects.type.TypeNodes.GetMroNode;
8791
import com.oracle.graal.python.builtins.objects.type.TypeNodes.GetNameNode;
8892
import com.oracle.graal.python.builtins.objects.type.TypeNodes.GetSubclassesNode;
@@ -112,6 +116,8 @@
112116
import com.oracle.graal.python.nodes.function.builtins.PythonVarargsBuiltinNode;
113117
import com.oracle.graal.python.nodes.object.IsBuiltinClassProfile;
114118
import com.oracle.graal.python.nodes.truffle.PythonTypes;
119+
import com.oracle.graal.python.nodes.util.CannotCastException;
120+
import com.oracle.graal.python.nodes.util.CastToJavaStringNode;
115121
import com.oracle.graal.python.nodes.util.SplitArgsNode;
116122
import com.oracle.graal.python.runtime.exception.PException;
117123
import com.oracle.graal.python.runtime.exception.PythonErrorType;
@@ -131,11 +137,6 @@
131137
import com.oracle.truffle.api.interop.InteropLibrary;
132138
import com.oracle.truffle.api.interop.UnsupportedMessageException;
133139
import com.oracle.truffle.api.library.CachedLibrary;
134-
import com.oracle.graal.python.builtins.objects.getsetdescriptor.HiddenPythonKey;
135-
import com.oracle.graal.python.builtins.objects.type.TypeNodes.CheckCompatibleForAssigmentNode;
136-
import com.oracle.graal.python.builtins.objects.type.TypeNodes.GetBaseClassNode;
137-
import com.oracle.graal.python.builtins.objects.type.TypeNodes.GetBestBaseClassNode;
138-
import static com.oracle.graal.python.nodes.SpecialMethodNames.MRO;
139140
import com.oracle.truffle.api.profiles.BranchProfile;
140141
import com.oracle.truffle.api.profiles.ConditionProfile;
141142

@@ -837,28 +838,18 @@ Object setName(@SuppressWarnings("unused") PythonBuiltinClass cls, @SuppressWarn
837838
}
838839

839840
@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")
859841
Object setName(PythonClass cls, Object 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+
@Cached CastToJavaStringNode castToJavaStringNode) {
843+
try {
844+
String string = castToJavaStringNode.execute(value);
845+
if (containsNullCharacter(string)) {
846+
throw raise(PythonBuiltinClassType.ValueError, "type name must not contain null characters");
847+
}
848+
cls.setName(string);
849+
return PNone.NONE;
850+
} catch (CannotCastException e) {
851+
throw raise(PythonBuiltinClassType.TypeError, "can only assign string to %p.__name__, not '%p'", cls, value);
852+
}
862853
}
863854

864855
@Specialization(guards = "isNoValue(value)")
@@ -962,22 +953,14 @@ Object setName(@SuppressWarnings("unused") PythonBuiltinClass cls, @SuppressWarn
962953
}
963954

964955
@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")
978956
Object setName(PythonClass cls, Object value,
979-
@Cached.Shared("builtinStringProfile") @Cached IsBuiltinClassProfile profile) {
980-
throw raise(PythonBuiltinClassType.TypeError, "can only assign string to %p.__qualname__, not '%p'", cls, value);
957+
@Cached CastToJavaStringNode castToJavaStringNode) {
958+
try {
959+
cls.setQualName(castToJavaStringNode.execute(value));
960+
return PNone.NONE;
961+
} catch (CannotCastException e) {
962+
throw raise(PythonBuiltinClassType.TypeError, "can only assign string to %p.__qualname__, not '%p'", cls, value);
963+
}
981964
}
982965

983966
@Specialization(guards = "isNoValue(value)")

0 commit comments

Comments
 (0)