Skip to content

Commit dfd61e9

Browse files
committed
[GR-24336] Don't report generic iterables as arrays
PullRequest: graalpython/1058
2 parents ac94948 + 648d2ee commit dfd61e9

File tree

2 files changed

+13
-40
lines changed

2 files changed

+13
-40
lines changed

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

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -52,15 +52,6 @@
5252
import java.util.Arrays;
5353
import java.util.List;
5454

55-
import com.oracle.graal.python.runtime.interop.InteropArray;
56-
import com.oracle.graal.python.test.PythonTests;
57-
import com.oracle.truffle.api.interop.ArityException;
58-
import com.oracle.truffle.api.interop.InteropLibrary;
59-
import com.oracle.truffle.api.interop.TruffleObject;
60-
import com.oracle.truffle.api.interop.UnknownIdentifierException;
61-
import com.oracle.truffle.api.library.ExportLibrary;
62-
import com.oracle.truffle.api.library.ExportMessage;
63-
6455
import org.graalvm.polyglot.Context;
6556
import org.graalvm.polyglot.Context.Builder;
6657
import org.graalvm.polyglot.Engine;
@@ -76,6 +67,15 @@
7667
import org.junit.runners.Parameterized.Parameter;
7768
import org.junit.runners.Parameterized.Parameters;
7869

70+
import com.oracle.graal.python.runtime.interop.InteropArray;
71+
import com.oracle.graal.python.test.PythonTests;
72+
import com.oracle.truffle.api.interop.ArityException;
73+
import com.oracle.truffle.api.interop.InteropLibrary;
74+
import com.oracle.truffle.api.interop.TruffleObject;
75+
import com.oracle.truffle.api.interop.UnknownIdentifierException;
76+
import com.oracle.truffle.api.library.ExportLibrary;
77+
import com.oracle.truffle.api.library.ExportMessage;
78+
7979
@RunWith(Enclosed.class)
8080
public class JavaInteropTest {
8181
public static class GeneralInterop extends PythonTests {
@@ -301,13 +301,14 @@ public void accessSuitePy() throws IOException {
301301
"suite.py").build();
302302
Value suite = context.eval(suitePy);
303303

304+
Value listConverter = context.eval("python", "list");
304305
Value libraries = suite.getMember("libraries");
305306
assertNotNull("libraries found", libraries);
306-
final List<Object> suiteKeys = Arrays.asList(suite.invokeMember("keys").as(Object[].class));
307+
final List<Object> suiteKeys = Arrays.asList(listConverter.execute(suite.invokeMember("keys")).as(Object[].class));
307308
assertTrue("Libraries found among keys: " + suiteKeys, suiteKeys.contains("libraries"));
308309

309310
Value dacapo = null;
310-
for (Object k : libraries.invokeMember("keys").as(List.class)) {
311+
for (Object k : listConverter.execute(libraries.invokeMember("keys")).as(List.class)) {
311312
System.err.println("k " + k);
312313
if ("DACAPO".equals(k)) {
313314
dacapo = libraries.getMember((String) k);

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

Lines changed: 1 addition & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -275,17 +275,13 @@ public Object readMember(String key,
275275
@ExportMessage
276276
public boolean hasArrayElements(
277277
@CachedLibrary("this") PythonObjectLibrary dataModelLibrary) {
278-
return (dataModelLibrary.isSequence(this) || dataModelLibrary.isIterable(this)) && !dataModelLibrary.isMapping(this);
278+
return dataModelLibrary.isSequence(this) && !dataModelLibrary.isMapping(this);
279279
}
280280

281281
@ExportMessage
282282
public Object readArrayElement(long key,
283283
@CachedLibrary("this") PythonObjectLibrary dataModelLibrary,
284284
@Shared("getItemNode") @Cached PInteropSubscriptNode getItemNode,
285-
@Exclusive @Cached LookupInheritedAttributeNode.Dynamic lookupIterNode,
286-
@Exclusive @Cached LookupInheritedAttributeNode.Dynamic lookupNextNode,
287-
@Exclusive @Cached CallNode callIterNode,
288-
@Exclusive @Cached CallNode callNextNode,
289285
@Shared("toForeign") @Cached PTypeToForeignNode toForeign) throws UnsupportedMessageException, InvalidArrayIndexException {
290286
if (dataModelLibrary.isSequence(this)) {
291287
try {
@@ -301,21 +297,6 @@ public Object readArrayElement(long key,
301297
}
302298
}
303299

304-
if (dataModelLibrary.isIterable(this)) {
305-
Object attrIter = lookupIterNode.execute(this, SpecialMethodNames.__ITER__);
306-
Object iter = callIterNode.execute(null, attrIter, this);
307-
if (iter != this) {
308-
// there is a separate iterator for this object, should be safe to consume
309-
Object result = iterateToKey(lookupNextNode, callNextNode, iter, key);
310-
if (result != PNone.NO_VALUE) {
311-
return result;
312-
}
313-
// TODO(fa) refine exception handling
314-
// it's an iterable, so we assume the index is wrong
315-
throw InvalidArrayIndexException.create(key);
316-
}
317-
}
318-
319300
throw UnsupportedMessageException.create();
320301
}
321302

@@ -353,15 +334,6 @@ public void removeArrayElement(long key,
353334
}
354335
}
355336

356-
private static Object iterateToKey(LookupInheritedAttributeNode.Dynamic lookupNextNode, CallNode callNextNode, Object iter, long key) {
357-
Object value = PNone.NO_VALUE;
358-
for (long i = 0; i <= key; i++) {
359-
Object attrNext = lookupNextNode.execute(iter, SpecialMethodNames.__NEXT__);
360-
value = callNextNode.execute(null, attrNext, iter);
361-
}
362-
return value;
363-
}
364-
365337
@ExportMessage
366338
public long getArraySize(@CachedLibrary("this") PythonObjectLibrary lib) throws UnsupportedMessageException {
367339
// since a call to this method must be preceded by a call to 'hasArrayElements', we just

0 commit comments

Comments
 (0)