Skip to content

Commit c18a80e

Browse files
committed
PyExpatModuleBuiltins: use putUncachedWithJavaEq during initialization
1 parent 6cf3b17 commit c18a80e

File tree

2 files changed

+17
-9
lines changed

2 files changed

+17
-9
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/PyExpatModuleBuiltins.java

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@
4242

4343
import static com.oracle.graal.python.util.PythonUtils.toTruffleStringUncached;
4444

45-
import java.util.LinkedHashMap;
4645
import java.util.List;
4746

4847
import com.oracle.graal.python.builtins.Builtin;
@@ -51,14 +50,18 @@
5150
import com.oracle.graal.python.builtins.PythonBuiltinClassType;
5251
import com.oracle.graal.python.builtins.PythonBuiltins;
5352
import com.oracle.graal.python.builtins.PythonOS;
53+
import com.oracle.graal.python.builtins.objects.common.EconomicMapStorage;
5454
import com.oracle.graal.python.builtins.objects.module.PythonModule;
55+
import com.oracle.graal.python.lib.PyObjectHashNode;
5556
import com.oracle.graal.python.nodes.PRaiseNode;
5657
import com.oracle.graal.python.nodes.function.PythonBuiltinBaseNode;
5758
import com.oracle.graal.python.nodes.function.builtins.PythonTernaryBuiltinNode;
5859
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
5960
import com.oracle.truffle.api.dsl.GenerateNodeFactory;
6061
import com.oracle.truffle.api.dsl.NodeFactory;
6162
import com.oracle.truffle.api.dsl.Specialization;
63+
import com.oracle.truffle.api.strings.TruffleString;
64+
import com.oracle.truffle.api.strings.TruffleString.HashCodeNode;
6265

6366
@CoreFunctions(defineModule = "pyexpat", os = PythonOS.PLATFORM_WIN32)
6467
public final class PyExpatModuleBuiltins extends PythonBuiltins {
@@ -142,15 +145,17 @@ public void initialize(Python3Core core) {
142145
addBuiltinConstant("model", model);
143146

144147
PythonModule errors = core.factory().createPythonModule(toTruffleStringUncached("pyexpat.errors"));
145-
LinkedHashMap<String, Object> codes = new LinkedHashMap<>(ErrorConstant.values().length);
146-
LinkedHashMap<Object, Object> messages = new LinkedHashMap<>(ErrorConstant.values().length);
148+
EconomicMapStorage codes = EconomicMapStorage.create(ErrorConstant.values().length);
149+
EconomicMapStorage messages = EconomicMapStorage.create(ErrorConstant.values().length);
147150
for (ErrorConstant c : ErrorConstant.values()) {
148-
errors.setAttribute(toTruffleStringUncached(c.name()), toTruffleStringUncached(c.message));
149-
codes.put(c.message, c.ordinal() + 1);
150-
messages.put(c.ordinal() + 1, c.message);
151+
TruffleString messageTs = toTruffleStringUncached(c.message);
152+
errors.setAttribute(toTruffleStringUncached(c.name()), messageTs);
153+
int id = c.ordinal() + 1;
154+
codes.putUncachedWithJavaEq(messageTs, PyObjectHashNode.hash(messageTs, HashCodeNode.getUncached()), id);
155+
messages.putUncachedWithJavaEq(id, PyObjectHashNode.hash(id), c.message);
151156
}
152-
errors.setAttribute(toTruffleStringUncached("messages"), core.factory().createDictFromMapGeneric(messages));
153-
errors.setAttribute(toTruffleStringUncached("codes"), core.factory().createDictFromMap(codes));
157+
errors.setAttribute(toTruffleStringUncached("messages"), core.factory().createDict(messages));
158+
errors.setAttribute(toTruffleStringUncached("codes"), core.factory().createDict(codes));
154159
addBuiltinConstant("errors", errors);
155160
}
156161

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/common/ObjectHashMap.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -353,7 +353,8 @@ public static Object doGetWithRestart(Frame frame, Node inliningTarget, ObjectHa
353353
@Cached InlinedCountingConditionProfile collisionFoundNoValue,
354354
@Cached InlinedCountingConditionProfile collisionFoundEqKey,
355355
@Cached PyObjectRichCompareBool.EqNode eqNode) {
356-
// must not call generic __eq__ before this
356+
// Must not call generic __eq__ before builtins are initialized
357+
// If this assert fires: we'll need something like putUncachedWithJavaEq also for get
357358
assert map.size == 0 || SpecialMethodSlot.areBuiltinSlotsInitialized();
358359
while (true) {
359360
try {
@@ -476,6 +477,8 @@ public static void doPutWithRestart(Frame frame, Node inliningTarget, ObjectHash
476477
@Cached InlinedBranchProfile rehash1Profile,
477478
@Cached InlinedBranchProfile rehash2Profile,
478479
@Cached PyObjectRichCompareBool.EqNode eqNode) {
480+
// Must not call generic __eq__ before builtins are initialized
481+
// If this assert fires: make sure to use putUncachedWithJavaEq during initialization
479482
assert map.size == 0 || (SpecialMethodSlot.areBuiltinSlotsInitialized() || eqNode == null);
480483
while (true) {
481484
try {

0 commit comments

Comments
 (0)