Skip to content

Commit 24f1dba

Browse files
timfelfangerer
authored andcommitted
hasArrayElements is now strictly only for indexed based access. for now, we try to read to determine if an index is readable
1 parent 393eb36 commit 24f1dba

File tree

1 file changed

+19
-6
lines changed

1 file changed

+19
-6
lines changed

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

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -303,31 +303,44 @@ public long getArraySize(
303303

304304
@ExportMessage
305305
public boolean isArrayElementReadable(@SuppressWarnings("unused") long idx,
306+
@Shared("getItemNode") @Cached PInteropSubscriptNode getItemNode,
306307
@Shared("callLenNode") @Cached LookupAndCallUnaryDynamicNode callLenNode) {
307-
return isInBounds(callLenNode, idx);
308+
return isInBounds(callLenNode, getItemNode, idx);
308309
}
309310

310311
@ExportMessage
311312
public boolean isArrayElementModifiable(@SuppressWarnings("unused") long idx,
313+
@Shared("getItemNode") @Cached PInteropSubscriptNode getItemNode,
312314
@Shared("callLenNode") @Cached LookupAndCallUnaryDynamicNode callLenNode) {
313-
return !(this instanceof PTuple) && !(this instanceof PBytes) && isInBounds(callLenNode, idx);
315+
return !(this instanceof PTuple) && !(this instanceof PBytes) && isInBounds(callLenNode, getItemNode, idx);
314316
}
315317

316318
@ExportMessage
317319
public boolean isArrayElementInsertable(@SuppressWarnings("unused") long idx,
320+
@Shared("getItemNode") @Cached PInteropSubscriptNode getItemNode,
318321
@Shared("callLenNode") @Cached LookupAndCallUnaryDynamicNode callLenNode) {
319-
return !(this instanceof PTuple) && !(this instanceof PBytes) && !isInBounds(callLenNode, idx);
322+
return !(this instanceof PTuple) && !(this instanceof PBytes) && !isInBounds(callLenNode, getItemNode, idx);
320323
}
321324

322325
@ExportMessage
323326
public boolean isArrayElementRemovable(@SuppressWarnings("unused") long idx,
327+
@Shared("getItemNode") @Cached PInteropSubscriptNode getItemNode,
324328
@Shared("callLenNode") @Cached LookupAndCallUnaryDynamicNode callLenNode) {
325-
return !(this instanceof PTuple) && !(this instanceof PBytes) && isInBounds(callLenNode, idx);
329+
return !(this instanceof PTuple) && !(this instanceof PBytes) && isInBounds(callLenNode, getItemNode, idx);
326330
}
327331

328-
private boolean isInBounds(LookupAndCallUnaryDynamicNode callLenNode, long idx) {
332+
private boolean isInBounds(LookupAndCallUnaryDynamicNode callLenNode, PInteropSubscriptNode getItemNode, long idx) {
329333
long len = getArraySizeSafe(callLenNode);
330-
return 0 <= idx && idx < len;
334+
if (0 <= idx && idx < len) {
335+
try {
336+
getItemNode.execute(this, idx);
337+
return true;
338+
} catch (PException e) {
339+
return false;
340+
}
341+
} else {
342+
return false;
343+
}
331344
}
332345

333346
private long getArraySizeSafe(LookupAndCallUnaryDynamicNode callLenNode) {

0 commit comments

Comments
 (0)