@@ -273,22 +273,16 @@ public boolean hasArrayElements(
273273
274274 @ ExportMessage
275275 public Object readArrayElement (long key ,
276- @ CachedLibrary ("this" ) PythonObjectLibrary dataModelLibrary ,
276+ @ CachedLibrary ("this" ) InteropLibrary interopLib ,
277277 @ Shared ("getItemNode" ) @ Cached PInteropSubscriptNode getItemNode ,
278278 @ Exclusive @ Cached GilNode gil ) throws UnsupportedMessageException , InvalidArrayIndexException {
279279 boolean mustRelease = gil .acquire ();
280280 try {
281- if (dataModelLibrary . isSequence (this )) {
281+ if (interopLib . hasArrayElements (this )) {
282282 try {
283283 return getItemNode .execute (this , key );
284284 } catch (PException e ) {
285- if (isAbstractMapping (dataModelLibrary )) {
286- throw UnsupportedMessageException .create ();
287- } else {
288- // TODO(fa) refine exception handling
289- // it's a sequence, so we assume the index is wrong
290- throw InvalidArrayIndexException .create (key );
291- }
285+ throw InvalidArrayIndexException .create (key );
292286 }
293287 }
294288 throw UnsupportedMessageException .create ();
@@ -299,38 +293,35 @@ public Object readArrayElement(long key,
299293
300294 @ ExportMessage
301295 public void writeArrayElement (long key , Object value ,
302- @ CachedLibrary ("this" ) PythonObjectLibrary dataModelLibrary ,
296+ @ CachedLibrary ("this" ) InteropLibrary interopLib ,
303297 @ Cached PInteropSubscriptAssignNode setItemNode ,
304298 @ Exclusive @ Cached GilNode gil ) throws UnsupportedMessageException , InvalidArrayIndexException {
305299 boolean mustRelease = gil .acquire ();
306300 try {
307- if (dataModelLibrary . isSequence (this )) {
301+ if (interopLib . hasArrayElements (this )) {
308302 try {
309303 setItemNode .execute (this , key , value );
310304 } catch (PException e ) {
311- // TODO(fa) refine exception handling
312- // it's a sequence, so we assume the index is wrong
313305 throw InvalidArrayIndexException .create (key );
314306 }
315307 }
308+ throw UnsupportedMessageException .create ();
316309 } finally {
317310 gil .release (mustRelease );
318311 }
319312 }
320313
321314 @ ExportMessage
322315 public void removeArrayElement (long key ,
323- @ CachedLibrary ("this" ) PythonObjectLibrary dataModelLibrary ,
316+ @ CachedLibrary ("this" ) InteropLibrary interopLib ,
324317 @ Exclusive @ Cached PInteropDeleteItemNode deleteItemNode ,
325318 @ Exclusive @ Cached GilNode gil ) throws UnsupportedMessageException , InvalidArrayIndexException {
326319 boolean mustRelease = gil .acquire ();
327320 try {
328- if (dataModelLibrary . isSequence (this )) {
321+ if (interopLib . hasArrayElements (this )) {
329322 try {
330323 deleteItemNode .execute (this , key );
331324 } catch (PException e ) {
332- // TODO(fa) refine exception handling
333- // it's a sequence, so we assume the index is wrong
334325 throw InvalidArrayIndexException .create (key );
335326 }
336327 } else {
@@ -343,12 +334,14 @@ public void removeArrayElement(long key,
343334
344335 @ ExportMessage
345336 public long getArraySize (
337+ @ CachedLibrary ("this" ) InteropLibrary interopLib ,
346338 @ Shared ("sizeNode" ) @ Cached PyObjectSizeNode sizeNode ,
347339 @ Exclusive @ Cached GilNode gil ) throws UnsupportedMessageException {
348340 boolean mustRelease = gil .acquire ();
341+ if (!interopLib .hasArrayElements (this )) {
342+ throw UnsupportedMessageException .create ();
343+ }
349344 try {
350- // since a call to this method must be preceded by a call to 'hasArrayElements', we just
351- // assume that a length exists
352345 long len = sizeNode .execute (null , this );
353346 if (len >= 0 ) {
354347 return len ;
@@ -362,10 +355,14 @@ public long getArraySize(
362355
363356 @ ExportMessage
364357 public boolean isArrayElementReadable (@ SuppressWarnings ("unused" ) long idx ,
358+ @ CachedLibrary ("this" ) InteropLibrary interopLib ,
365359 @ Shared ("sizeNode" ) @ Cached PyObjectSizeNode sizeNode ,
366360 @ Shared ("getItemNode" ) @ Cached PInteropSubscriptNode getItemNode ,
367361 @ Exclusive @ Cached GilNode gil ) {
368362 boolean mustRelease = gil .acquire ();
363+ if (!interopLib .hasArrayElements (this )) {
364+ return false ;
365+ }
369366 try {
370367 return isInBounds (sizeNode .execute (null , this ), getItemNode , idx );
371368 } finally {
@@ -375,10 +372,14 @@ public boolean isArrayElementReadable(@SuppressWarnings("unused") long idx,
375372
376373 @ ExportMessage
377374 public boolean isArrayElementModifiable (@ SuppressWarnings ("unused" ) long idx ,
375+ @ CachedLibrary ("this" ) InteropLibrary interopLib ,
378376 @ Shared ("sizeNode" ) @ Cached PyObjectSizeNode sizeNode ,
379377 @ Shared ("getItemNode" ) @ Cached PInteropSubscriptNode getItemNode ,
380378 @ Exclusive @ Cached GilNode gil ) {
381379 boolean mustRelease = gil .acquire ();
380+ if (!interopLib .hasArrayElements (this )) {
381+ return false ;
382+ }
382383 try {
383384 return !(this instanceof PTuple ) && !(this instanceof PBytes ) && isInBounds (sizeNode .execute (null , this ), getItemNode , idx );
384385 } finally {
@@ -388,10 +389,14 @@ public boolean isArrayElementModifiable(@SuppressWarnings("unused") long idx,
388389
389390 @ ExportMessage
390391 public boolean isArrayElementInsertable (@ SuppressWarnings ("unused" ) long idx ,
392+ @ CachedLibrary ("this" ) InteropLibrary interopLib ,
391393 @ Shared ("sizeNode" ) @ Cached PyObjectSizeNode sizeNode ,
392394 @ Shared ("getItemNode" ) @ Cached PInteropSubscriptNode getItemNode ,
393395 @ Exclusive @ Cached GilNode gil ) {
394396 boolean mustRelease = gil .acquire ();
397+ if (!interopLib .hasArrayElements (this )) {
398+ return false ;
399+ }
395400 try {
396401 return !(this instanceof PTuple ) && !(this instanceof PBytes ) && !isInBounds (sizeNode .execute (null , this ), getItemNode , idx );
397402 } finally {
@@ -401,10 +406,14 @@ public boolean isArrayElementInsertable(@SuppressWarnings("unused") long idx,
401406
402407 @ ExportMessage
403408 public boolean isArrayElementRemovable (@ SuppressWarnings ("unused" ) long idx ,
409+ @ CachedLibrary ("this" ) InteropLibrary interopLib ,
404410 @ Shared ("sizeNode" ) @ Cached PyObjectSizeNode sizeNode ,
405411 @ Shared ("getItemNode" ) @ Cached PInteropSubscriptNode getItemNode ,
406412 @ Exclusive @ Cached GilNode gil ) {
407413 boolean mustRelease = gil .acquire ();
414+ if (!interopLib .hasArrayElements (this )) {
415+ return false ;
416+ }
408417 try {
409418 return !(this instanceof PTuple ) && !(this instanceof PBytes ) && isInBounds (sizeNode .execute (null , this ), getItemNode , idx );
410419 } finally {
@@ -606,9 +615,13 @@ public boolean isInstantiable(
606615
607616 @ ExportMessage
608617 public Object instantiate (Object [] arguments ,
618+ @ CachedLibrary ("this" ) InteropLibrary interopLib ,
609619 @ Exclusive @ Cached PExecuteNode executeNode ,
610620 @ Exclusive @ Cached GilNode gil ) throws UnsupportedMessageException {
611621 boolean mustRelease = gil .acquire ();
622+ if (!interopLib .isInstantiable (this )) {
623+ throw UnsupportedMessageException .create ();
624+ }
612625 try {
613626 return executeNode .execute (this , arguments );
614627 } finally {
0 commit comments