Menu

#85 [i:j:k] and [:] slices fail

Version 0.5
open
nobody
None
5
2013-11-13
2013-11-13
No

Where 'arr' is a Java array, arr[0:6:2] fails thusly:

Traceback (most recent call last):
File "d:\jpype\test\jpypetest\array.py", line 45, in testThreeArgumentSlice
self.arrayEquals(VALUES[0:6:2], t.i[0:6:2])
File "D:\jpype\src\python\jpype_jarray.py", line 49, in __getitem

return _jpype.getArrayItem(self.javaobject, ndx)
TypeError: an integer is required

This is because three-arg slices are implemented by passing a slice object to getitem - as is regular slicing in modern python if you don't override getslice.

arr[:] also fails:

Traceback (most recent call last):
File "d:\jpype\test\jpypetest\array.py", line 49, in testUnboundedSlice
self.arrayEquals(VALUES, t.i[:])
File "D:\jpype\src\python\jpype_jarray.py", line 55, in __getslice

return _jpype.getArraySlice(self.javaobject, i, j)
OverflowError: Python int too large to convert to C long

I believe this is because, as described at http://docs.python.org/2/reference/datamodel.html#object.__getslice__, "missing i or j in the slice expression are replaced by zero or sys.maxint, respectively".

Removing the override of getslice and updating JPypeJavaArray::getArraySlice to understand slice objects via http://docs.python.org/2/c-api/slice.html#PySlice_GetIndicesEx would be a way to fix both bugs.

I've attached a test case, but no fix.

1 Attachments

Discussion


Log in to post a comment.

Monday.com Logo