Skip to content

Commit 7735acb

Browse files
fangerertimfel
authored andcommitted
Allow any Python string representation in PyCStructTypeBuiltins.SetattrNode
1 parent e2b13b3 commit 7735acb

File tree

1 file changed

+24
-5
lines changed

1 file changed

+24
-5
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/ctypes/PyCStructTypeBuiltins.java

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
*/
4141
package com.oracle.graal.python.builtins.modules.ctypes;
4242

43+
import static com.oracle.graal.python.nodes.ErrorMessages.ATTR_NAME_MUST_BE_STRING;
4344
import static com.oracle.graal.python.nodes.SpecialMethodNames.J___NEW__;
4445
import static com.oracle.graal.python.nodes.SpecialMethodNames.J___SETATTR__;
4546
import static com.oracle.graal.python.util.PythonUtils.TS_ENCODING;
@@ -56,15 +57,18 @@
5657
import com.oracle.graal.python.nodes.attributes.WriteAttributeToObjectNode;
5758
import com.oracle.graal.python.nodes.function.PythonBuiltinBaseNode;
5859
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;
5962
import com.oracle.truffle.api.dsl.Cached;
63+
import com.oracle.truffle.api.dsl.Cached.Shared;
6064
import com.oracle.truffle.api.dsl.GenerateNodeFactory;
6165
import com.oracle.truffle.api.dsl.NodeFactory;
6266
import com.oracle.truffle.api.dsl.Specialization;
6367
import com.oracle.truffle.api.frame.VirtualFrame;
6468
import com.oracle.truffle.api.strings.TruffleString;
6569

6670
@CoreFunctions(extendClasses = PythonBuiltinClassType.PyCStructType)
67-
public class PyCStructTypeBuiltins extends PythonBuiltins {
71+
public final class PyCStructTypeBuiltins extends PythonBuiltins {
6872

6973
@Override
7074
protected List<? extends NodeFactory<? extends PythonBuiltinBaseNode>> getNodeFactories() {
@@ -81,15 +85,30 @@ protected abstract static class NewNode extends StructUnionTypeNewNode {
8185
protected abstract static class SetattrNode extends PythonTernaryBuiltinNode {
8286

8387
@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) {
8892
writeNode.execute(object, key, value);
8993
if (equalNode.execute(key, StructUnionTypeBuiltins.T__fields_, TS_ENCODING)) {
9094
updateStgDict.execute(frame, object, value, true, factory());
9195
}
9296
return PNone.NONE;
9397
}
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+
}
94113
}
95114
}

0 commit comments

Comments
 (0)