Skip to content

Commit 0d462a8

Browse files
committed
Fix missing conversion in writeMember/writeArrayElement
1 parent eaa61f0 commit 0d462a8

File tree

6 files changed

+43
-13
lines changed

6 files changed

+43
-13
lines changed

graalpython/com.oracle.graal.python.test.integration/src/com/oracle/graal/python/test/integration/interop/JavaInteropTest.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -595,6 +595,25 @@ public void writableBindings() {
595595
assertEquals(javaObj.asInt(), 42);
596596
}
597597

598+
@Test
599+
public void testDictTypeConversion() {
600+
Value bindings = context.getBindings("python");
601+
bindings.putMember("foo", "32");
602+
bindings.putMember("bar", (short) 32);
603+
Value intValue = context.eval("python", "int(foo) + int(bar)");
604+
assertEquals(intValue.asInt(), 64);
605+
}
606+
607+
@Test
608+
public void testListTypeConversions() {
609+
Value intConversion = context.eval("python", "int");
610+
Value list = context.eval("python", "[1]");
611+
list.setArrayElement(0, "32");
612+
assertEquals(intConversion.execute(list.getArrayElement(0)).asInt(), 32);
613+
list.setArrayElement(0, (short) 3);
614+
assertEquals(intConversion.execute(list.getArrayElement(0)).asInt(), 3);
615+
}
616+
598617
public class UnsupportedProxyHashMap implements ProxyHashMap {
599618
@Override
600619
public long getHashSize() {

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/PythonAbstractObject.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -227,13 +227,14 @@ public final void clearNativeWrapper() {
227227
public void writeMember(String key, Object value,
228228
@Bind("$node") Node inliningTarget,
229229
@Exclusive @Cached TruffleString.FromJavaStringNode fromJavaStringNode,
230+
@Shared @Cached PForeignToPTypeNode convert,
230231
@Exclusive @Cached PyObjectSetAttr setAttributeNode,
231232
// GR-44020: make shared:
232233
@Exclusive @Cached IsBuiltinObjectProfile attrErrorProfile,
233234
@Exclusive @Cached GilNode gil) throws UnsupportedMessageException, UnknownIdentifierException {
234235
boolean mustRelease = gil.acquire();
235236
try {
236-
setAttributeNode.execute(null, inliningTarget, this, fromJavaStringNode.execute(key, TS_ENCODING), value);
237+
setAttributeNode.execute(null, inliningTarget, this, fromJavaStringNode.execute(key, TS_ENCODING), convert.executeConvert(value));
237238
} catch (PException e) {
238239
e.expectAttributeError(inliningTarget, attrErrorProfile);
239240
// TODO(fa) not accurate; distinguish between read-only and non-existing
@@ -325,10 +326,12 @@ public void writeArrayElement(long key, Object value,
325326
@Bind("$node") Node inliningTarget,
326327
@Shared("getBehavior") @Cached GetInteropBehaviorNode getBehavior,
327328
@Shared("getValue") @Cached GetInteropBehaviorValueNode getValue,
329+
@Shared @Cached PForeignToPTypeNode convert,
328330
@Cached PySequenceSetItemNode sequenceSetItemNode,
329331
@Exclusive @Cached GilNode gil) throws UnsupportedMessageException, InvalidArrayIndexException {
330332
boolean mustRelease = gil.acquire();
331333
try {
334+
value = convert.executeConvert(value);
332335
InteropBehaviorMethod method = InteropBehaviorMethod.write_array_element;
333336
InteropBehavior behavior = getBehavior.execute(inliningTarget, this, method);
334337
if (behavior != null) {
@@ -1520,7 +1523,7 @@ public static String objectHashCodeAsHexString(Object value) {
15201523

15211524
@ExportMessage
15221525
public TriState isIdenticalOrUndefined(Object otherInterop,
1523-
@Cached PForeignToPTypeNode convert,
1526+
@Shared @Cached PForeignToPTypeNode convert,
15241527
@Exclusive @CachedLibrary(limit = "3") InteropLibrary otherLib,
15251528
@Cached IsNode isNode,
15261529
@Exclusive @Cached GilNode gil) {

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/array/PArray.java

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
import com.oracle.graal.python.builtins.objects.object.PythonBuiltinObject;
4444
import com.oracle.graal.python.nodes.ErrorMessages;
4545
import com.oracle.graal.python.nodes.PRaiseNode;
46+
import com.oracle.graal.python.nodes.interop.PForeignToPTypeNode;
4647
import com.oracle.graal.python.runtime.GilNode;
4748
import com.oracle.graal.python.runtime.sequence.storage.ByteSequenceStorage;
4849
import com.oracle.graal.python.runtime.sequence.storage.NativeByteSequenceStorage;
@@ -55,6 +56,7 @@
5556
import com.oracle.truffle.api.CompilerDirectives.CompilationFinal;
5657
import com.oracle.truffle.api.dsl.Bind;
5758
import com.oracle.truffle.api.dsl.Cached;
59+
import com.oracle.truffle.api.dsl.Cached.Exclusive;
5860
import com.oracle.truffle.api.dsl.Cached.Shared;
5961
import com.oracle.truffle.api.interop.InteropLibrary;
6062
import com.oracle.truffle.api.interop.InvalidArrayIndexException;
@@ -345,8 +347,8 @@ public long getArraySize() {
345347
@ExportMessage
346348
public Object readArrayElement(long index,
347349
@Bind("$node") Node inliningTarget,
348-
@Cached.Exclusive @Cached SequenceStorageNodes.GetItemScalarNode getItem,
349-
@Cached.Exclusive @Cached GilNode gil) throws InvalidArrayIndexException {
350+
@Exclusive @Cached SequenceStorageNodes.GetItemScalarNode getItem,
351+
@Exclusive @Cached GilNode gil) throws InvalidArrayIndexException {
350352
boolean mustRelease = gil.acquire();
351353
try {
352354
try {
@@ -363,12 +365,13 @@ public Object readArrayElement(long index,
363365
@ExportMessage
364366
public void writeArrayElement(long index, Object value,
365367
@Bind("$node") Node inliningTarget,
366-
@Cached.Exclusive @Cached SequenceStorageNodes.SetItemScalarNode setItem,
367-
@Cached.Exclusive @Cached GilNode gil) throws InvalidArrayIndexException {
368+
@Exclusive @Cached SequenceStorageNodes.SetItemScalarNode setItem,
369+
@Exclusive @Cached PForeignToPTypeNode convert,
370+
@Exclusive @Cached GilNode gil) throws InvalidArrayIndexException {
368371
boolean mustRelease = gil.acquire();
369372
try {
370373
try {
371-
setItem.execute(inliningTarget, storage, PInt.intValueExact(index), value);
374+
setItem.execute(inliningTarget, storage, PInt.intValueExact(index), convert.executeConvert(value));
372375
} catch (OverflowException e) {
373376
CompilerDirectives.transferToInterpreterAndInvalidate();
374377
throw InvalidArrayIndexException.create(index);
@@ -381,8 +384,8 @@ public void writeArrayElement(long index, Object value,
381384
@ExportMessage
382385
public void removeArrayElement(long index,
383386
@Bind("$node") Node inliningTarget,
384-
@Cached.Exclusive @Cached SequenceStorageNodes.DeleteItemNode delItem,
385-
@Cached.Exclusive @Cached GilNode gil) throws InvalidArrayIndexException {
387+
@Exclusive @Cached SequenceStorageNodes.DeleteItemNode delItem,
388+
@Exclusive @Cached GilNode gil) throws InvalidArrayIndexException {
386389
boolean mustRelease = gil.acquire();
387390
try {
388391
try {

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/bytes/PByteArray.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import com.oracle.graal.python.builtins.objects.ints.PInt;
3636
import com.oracle.graal.python.nodes.ErrorMessages;
3737
import com.oracle.graal.python.nodes.PRaiseNode;
38+
import com.oracle.graal.python.nodes.interop.PForeignToPTypeNode;
3839
import com.oracle.graal.python.runtime.GilNode;
3940
import com.oracle.graal.python.runtime.exception.PException;
4041
import com.oracle.graal.python.runtime.sequence.storage.ByteSequenceStorage;
@@ -150,11 +151,12 @@ public boolean isArrayElementRemovable(long index,
150151
public void writeArrayElement(long index, Object value,
151152
@Bind("$node") Node inliningTarget,
152153
@Exclusive @Cached SequenceStorageNodes.SetItemScalarNode setItem,
154+
@Exclusive @Cached PForeignToPTypeNode convert,
153155
@Exclusive @Cached GilNode gil) throws InvalidArrayIndexException {
154156
boolean mustRelease = gil.acquire();
155157
try {
156158
try {
157-
setItem.execute(inliningTarget, store, PInt.intValueExact(index), value);
159+
setItem.execute(inliningTarget, store, PInt.intValueExact(index), convert.executeConvert(value));
158160
} catch (OverflowException e) {
159161
CompilerDirectives.transferToInterpreterAndInvalidate();
160162
throw InvalidArrayIndexException.create(index);

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/runtime/interop/PythonLocalScope.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2021, 2022, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2021, 2024, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* The Universal Permissive License (UPL), Version 1.0
@@ -44,6 +44,7 @@
4444
import java.util.Map;
4545

4646
import com.oracle.graal.python.PythonLanguage;
47+
import com.oracle.graal.python.nodes.interop.PForeignToPTypeNode;
4748
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
4849
import com.oracle.truffle.api.TruffleLanguage;
4950
import com.oracle.truffle.api.frame.Frame;
@@ -157,7 +158,7 @@ void writeMember(String member, Object value) throws UnknownIdentifierException,
157158
if (slot == null) {
158159
throw UnknownIdentifierException.create(member);
159160
} else {
160-
frame.setObject(slot, value);
161+
frame.setObject(slot, PForeignToPTypeNode.getUncached().executeConvert(value));
161162
}
162163
} else {
163164
throw UnsupportedMessageException.create();

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/runtime/sequence/PSequence.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import com.oracle.graal.python.builtins.objects.common.SequenceStorageNodes;
3030
import com.oracle.graal.python.builtins.objects.ints.PInt;
3131
import com.oracle.graal.python.builtins.objects.object.PythonBuiltinObject;
32+
import com.oracle.graal.python.nodes.interop.PForeignToPTypeNode;
3233
import com.oracle.graal.python.runtime.GilNode;
3334
import com.oracle.graal.python.runtime.sequence.storage.SequenceStorage;
3435
import com.oracle.graal.python.util.OverflowException;
@@ -191,12 +192,13 @@ public Object readArrayElement(long index,
191192
public void writeArrayElement(long index, Object value,
192193
@Bind("$node") Node inliningTarget,
193194
@Exclusive @Cached SequenceNodes.GetSequenceStorageNode getSequenceStorageNode,
195+
@Exclusive @Cached PForeignToPTypeNode convert,
194196
@Exclusive @Cached SequenceStorageNodes.SetItemScalarNode setItem,
195197
@Exclusive @Cached GilNode gil) throws InvalidArrayIndexException {
196198
boolean mustRelease = gil.acquire();
197199
try {
198200
try {
199-
setItem.execute(inliningTarget, getSequenceStorageNode.execute(inliningTarget, this), PInt.intValueExact(index), value);
201+
setItem.execute(inliningTarget, getSequenceStorageNode.execute(inliningTarget, this), PInt.intValueExact(index), convert.executeConvert(value));
200202
} catch (OverflowException e) {
201203
CompilerDirectives.transferToInterpreterAndInvalidate();
202204
throw InvalidArrayIndexException.create(index);

0 commit comments

Comments
 (0)