Issue2550

classification
Title: test_list_jy fails on Java 8
Type: behaviour Severity: normal
Components: Versions: Jython 2.7
Milestone: Jython 2.7.1
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: stefan.richthofer Nosy List: stefan.richthofer
Priority: normal Keywords:

Created on 2017-02-08.14:21:17 by stefan.richthofer, last changed 2017-02-27.04:48:58 by zyasoft.

Messages
msg11082 (view) Author: Stefan Richthofer (stefan.richthofer) Date: 2017-02-08.14:21:15
======================================================================
ERROR: test_sort (__main__.JavaListTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/data/workspace/linux/Jython/ssh/jython/dist/Lib/test/list_tests.py", line 442, in test_sort
    self._test_sort()
  File "/data/workspace/linux/Jython/ssh/jython/dist/Lib/test/list_tests.py", line 446, in _test_sort
    u.sort()
TypeError: sort(): expected 1 args; got 0

----------------------------------------------------------------------


I investigated a bit and found that the object in question is of type ArrayList and indeed:

Jython 2.7.1b3 (, Feb 8 2017, 02:12:28) 
[OpenJDK 64-Bit Server VM (Oracle Corporation)] on java1.8.0_121
Type "help", "copyright", "credits" or "license" for more information.
>>> from java.util import ArrayList
>>> lst = ArrayList([1, 0])
>>> lst.sort()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: sort(): expected 1 args; got 0

In Java 7 this passes without complain.
So what shall this arg be that is suddenly required in Java 8? Let's try:

>>> lst.sort(0)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: sort(): 1st arg can't be coerced to java.util.Comparator
>>> 

So, looking into Java 8 ArrayList, etc: Lists have a sort method now and Jython seems to prefer that over the original Jython-style sort method. The Java sort-method however requires a comparator, which can be null to fallback to some default behavior (what is probably what we want).

To fix this in a Java version independent fashion I'd suggest to blacklist methods called 'sort' for JavaTypes that implement java.util.List, when going through methods in PyJavaType.init. This should assure that Jython always chooses the right sort-method.

Maybe we can alternatively implement an overloading logic for these methods. I'd suggest to add this as an enhancement to this solution later on.
msg11083 (view) Author: Stefan Richthofer (stefan.richthofer) Date: 2017-02-08.14:40:32
Okay, after a closer look, solution appears to be much simpler:

In JavaProxyList.java simply move listSortProxy from getProxyMethods to getPostProxyMethods. This solves the issue; running regrtests now...
msg11084 (view) Author: Stefan Richthofer (stefan.richthofer) Date: 2017-02-08.15:16:07
Fixed as of https://github.com/jythontools/jython/commit/3b27d76fb2e25d00c1b40c2d3ca55c81a8ea8e15.
History
Date User Action Args
2017-02-27 04:48:58zyasoftsetstatus: pending -> closed
2017-02-08 15:16:07stefan.richthofersetstatus: open -> pending
assignee: stefan.richthofer
resolution: fixed
messages: + msg11084
milestone: Jython 2.7.1
2017-02-08 14:40:32stefan.richthofersetmessages: + msg11083
2017-02-08 14:21:17stefan.richthofercreate