Skip to content

Commit b008992

Browse files
committed
always unpack PStrings to Strings when reading and writing attributes
1 parent 5e7ed7f commit b008992

File tree

2 files changed

+34
-12
lines changed

2 files changed

+34
-12
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/attributes/ReadAttributeFromObjectNode.java

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040

4141
import com.oracle.graal.python.builtins.objects.PNone;
4242
import com.oracle.graal.python.builtins.objects.object.PythonObject;
43+
import com.oracle.graal.python.builtins.objects.str.PString;
4344
import com.oracle.graal.python.nodes.PNode;
4445
import com.oracle.truffle.api.Assumption;
4546
import com.oracle.truffle.api.CompilerDirectives;
@@ -85,6 +86,14 @@ protected static boolean checkShape(@SuppressWarnings("unused") PythonObject obj
8586
return cachedObject.getStorage().getShape() == cachedShape;
8687
}
8788

89+
protected Object attrKey(Object key) {
90+
if (key instanceof PString) {
91+
return ((PString) key).getValue();
92+
} else {
93+
return key;
94+
}
95+
}
96+
8897
@SuppressWarnings("unused")
8998
@Specialization(limit = "1", //
9099
guards = {
@@ -101,12 +110,13 @@ protected static boolean checkShape(@SuppressWarnings("unused") PythonObject obj
101110
protected Object readDirectFinal(PythonObject object, Object key,
102111
@Cached("object") PythonObject cachedObject,
103112
@Cached("key") Object cachedKey,
113+
@Cached("attrKey(key)") Object attrKey,
104114
@Cached("object.getStorage().getShape()") Shape cachedShape,
105115
@Cached("cachedShape.getValidAssumption()") Assumption layoutAssumption,
106-
@Cached("getLocationOrNull(cachedShape.getProperty(key))") Location loc,
116+
@Cached("getLocationOrNull(cachedShape.getProperty(attrKey))") Location loc,
107117
@Cached("loc.getFinalAssumption()") Assumption finalAssumption,
108118
@Cached("readFinalValue(object, loc)") Object cachedValue) {
109-
assert assertFinal(object, key, cachedValue);
119+
assert assertFinal(object, attrKey, cachedValue);
110120
return cachedValue;
111121
}
112122

@@ -125,9 +135,10 @@ private static boolean assertFinal(PythonObject object, Object key, Object cache
125135
assumptions = "layoutAssumption")
126136
protected Object readDirect(PythonObject object, Object key,
127137
@Cached("key") Object cachedKey,
138+
@Cached("attrKey(cachedKey)") Object attrKey,
128139
@Cached("object.getStorage().getShape()") Shape cachedShape,
129140
@Cached("cachedShape.getValidAssumption()") Assumption layoutAssumption,
130-
@Cached("getLocationOrNull(cachedShape.getProperty(key))") Location loc) {
141+
@Cached("getLocationOrNull(cachedShape.getProperty(attrKey))") Location loc) {
131142
if (loc == null) {
132143
return PNone.NO_VALUE;
133144
} else {
@@ -151,7 +162,7 @@ protected Object updateShapeAndRead(PythonObject object, Object key,
151162

152163
@Specialization(replaces = "readDirect")
153164
protected Object readIndirect(PythonObject object, Object key) {
154-
Object value = object.getStorage().get(key);
165+
Object value = object.getStorage().get(attrKey(key));
155166
if (value == null) {
156167
return PNone.NO_VALUE;
157168
} else {
@@ -163,7 +174,7 @@ protected Object readIndirect(PythonObject object, Object key) {
163174
protected Object readForeign(TruffleObject object, Object key,
164175
@Cached("createReadNode()") Node readNode) {
165176
try {
166-
return ForeignAccess.sendRead(readNode, object, key);
177+
return ForeignAccess.sendRead(readNode, object, attrKey(key));
167178
} catch (UnknownIdentifierException | UnsupportedMessageException e) {
168179
return PNone.NO_VALUE;
169180
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/attributes/WriteAttributeToObjectNode.java

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
package com.oracle.graal.python.nodes.attributes;
4040

4141
import com.oracle.graal.python.builtins.objects.object.PythonObject;
42+
import com.oracle.graal.python.builtins.objects.str.PString;
4243
import com.oracle.graal.python.nodes.PNode;
4344
import com.oracle.truffle.api.Assumption;
4445
import com.oracle.truffle.api.CompilerDirectives;
@@ -65,6 +66,14 @@ protected Location getLocationOrNull(Property prop) {
6566
return prop == null ? null : prop.getLocation();
6667
}
6768

69+
protected Object attrKey(Object key) {
70+
if (key instanceof PString) {
71+
return ((PString) key).getValue();
72+
} else {
73+
return key;
74+
}
75+
}
76+
6877
@SuppressWarnings("unused")
6978
@Specialization(guards = {
7079
"object.getStorage().getShape() == cachedShape",
@@ -82,7 +91,7 @@ protected Object updateShapeAndWrite(PythonObject object, Object key, Object val
8291
@Specialization(limit = "getIntOption(getContext(), AttributeAccessInlineCacheMaxDepth)", //
8392
guards = {
8493
"object.getStorage().getShape() == cachedShape",
85-
"cachedKey.equals(key)",
94+
"cachedKey == key",
8695
"loc != null",
8796
"loc.canSet(value)"
8897
}, //
@@ -92,9 +101,10 @@ protected Object updateShapeAndWrite(PythonObject object, Object key, Object val
92101
})
93102
protected boolean doDirect(PythonObject object, Object key, Object value,
94103
@Cached("key") Object cachedKey,
104+
@Cached("attrKey(cachedKey)") Object attrKey,
95105
@Cached("object.getStorage().getShape()") Shape cachedShape,
96106
@Cached("cachedShape.getValidAssumption()") Assumption layoutAssumption,
97-
@Cached("getLocationOrNull(cachedShape.getProperty(key))") Location loc) {
107+
@Cached("getLocationOrNull(cachedShape.getProperty(attrKey))") Location loc) {
98108
try {
99109
loc.set(object.getStorage(), value);
100110
} catch (IncompatibleLocationException | FinalLocationException e) {
@@ -109,7 +119,7 @@ protected boolean doDirect(PythonObject object, Object key, Object value,
109119
@Specialization(limit = "getIntOption(getContext(), AttributeAccessInlineCacheMaxDepth)", //
110120
guards = {
111121
"object.getStorage().getShape() == cachedShape",
112-
"cachedKey.equals(key)",
122+
"cachedKey == key",
113123
"loc == null || !loc.canSet(value)",
114124
"newLoc.canSet(value)"
115125
}, //
@@ -119,12 +129,13 @@ protected boolean doDirect(PythonObject object, Object key, Object value,
119129
})
120130
protected boolean defineDirect(PythonObject object, Object key, Object value,
121131
@Cached("key") Object cachedKey,
132+
@Cached("attrKey(key)") Object attrKey,
122133
@Cached("object.getStorage().getShape()") Shape cachedShape,
123134
@Cached("cachedShape.getValidAssumption()") Assumption layoutAssumption,
124-
@Cached("getLocationOrNull(cachedShape.getProperty(key))") Location loc,
125-
@Cached("cachedShape.defineProperty(key, value, 0)") Shape newShape,
135+
@Cached("getLocationOrNull(cachedShape.getProperty(attrKey))") Location loc,
136+
@Cached("cachedShape.defineProperty(attrKey, value, 0)") Shape newShape,
126137
@Cached("newShape.getValidAssumption()") Assumption newLayoutAssumption,
127-
@Cached("getLocationOrNull(newShape.getProperty(key))") Location newLoc) {
138+
@Cached("getLocationOrNull(newShape.getProperty(attrKey))") Location newLoc) {
128139
try {
129140
newLoc.set(object.getStorage(), value, cachedShape, newShape);
130141
} catch (IncompatibleLocationException e) {
@@ -138,7 +149,7 @@ protected boolean defineDirect(PythonObject object, Object key, Object value,
138149
@TruffleBoundary
139150
@Specialization(replaces = {"doDirect", "defineDirect"}, guards = {"object.getStorage().getShape().isValid()"})
140151
protected boolean doIndirect(PythonObject object, Object key, Object value) {
141-
object.setAttribute(key, value);
152+
object.setAttribute(attrKey(key), value);
142153
return true;
143154
}
144155

0 commit comments

Comments
 (0)