7

My final purpose is to convert a running Python project to Jython interpreter because some java API's are going to be added.

Details:

  • The latest Jython is 2.7
  • The project I have is runnable with Python 3.5

So I took the following approach:

First thing was to convert my project to Python 2.7 utilizing the future module and pasteurize executable. This step was done successfully.

Second thing is to convert the Python 2.7 project to a Jython 2.7 Project. Switching the interpreter at Eclipse mars, the following error was indicated:

console: Failed to install '': java.nio.charset.UnsupportedCharsetException: cp0.

To workaround it, the solution from this post was utilized by passing the encoding argument -Dpython.console.encoding=UTF-8 to the java VM according to figure below: enter image description here

Thumbs were up when trying to run again. But unfortunately here, the error shown below just appeared. As I don't plan to change any imported module if there's no need to do so, I decided to ask help here.

pydev debugger: starting (pid: 4216)
Traceback (most recent call last):
  File "C:\Users\souzadan\.p2\pool\plugins\org.python.pydev_4.4.0.201510052309\pysrc\pydevd.py", line 2364, in <module>
    globals = debugger.run(setup['file'], None, None, is_module)
  File "C:\Users\souzadan\.p2\pool\plugins\org.python.pydev_4.4.0.201510052309\pysrc\pydevd.py", line 1784, in run
    pydev_imports.execfile(file, globals, locals)  # execute the script
  File "C:\Users\souzadan\FirmwareDevTools\Workspaces\Eclipse\aPythonWorkspace\aPythonProject\aPythonFolder\aPythonFile.py", line 7, in <module>
    standard_library.install_aliases()
  File "C:\Users\souzadan\FirmwareDevTools\Compilers\Jython2.7.0\Lib\site-packages\future-0.15.2-py2.7.egg\future\standard_library\__init__.py", line 465, in install_aliases
    from future.backports.urllib import request
  File "C:\Users\souzadan\FirmwareDevTools\Compilers\Jython2.7.0\Lib\site-packages\future-0.15.2-py2.7.egg\future\backports\urllib\request.py", line 96, in <module>
    from future.backports import email
  File "C:\Users\souzadan\FirmwareDevTools\Compilers\Jython2.7.0\Lib\site-packages\future-0.15.2-py2.7.egg\future\backports\email\__init__.py", line 16, in <module>
    from future.utils import surrogateescape
  File "C:\Users\souzadan\FirmwareDevTools\Compilers\Jython2.7.0\Lib\site-packages\future-0.15.2-py2.7.egg\future\utils\surrogateescape.py", line 167, in <module>
    FS_ENCODING = 'ascii'; fn = b('[abc\xff]'); encoded = u('[abc\udcff]')
  File "C:\Users\souzadan\FirmwareDevTools\Compilers\Jython2.7.0\Lib\site-packages\future-0.15.2-py2.7.egg\future\utils\surrogateescape.py", line 25, in u
    return text.decode('unicode_escape')
UnicodeDecodeError: 'unicodeescape' codec can't decode bytes in position 4-10: illegal Unicode character
Error in atexit._run_exitfuncs:
Traceback (most recent call last):
  File "C:\Users\souzadan\FirmwareDevTools\Compilers\Jython2.7.0\Lib\atexit.py", line 24, in _run_exitfuncs
    func(*targs, **kargs)
  File "C:\Users\souzadan\FirmwareDevTools\Compilers\Jython2.7.0\Lib\threading.py", line 297, in _MainThread__exitfunc
    t.join()
  File "C:\Users\souzadan\FirmwareDevTools\Compilers\Jython2.7.0\Lib\threading.py", line 128, in join
    raise RuntimeError("cannot join current thread")
RuntimeError: cannot join current thread
Error in sys.exitfunc:
Traceback (most recent call last):
  File "C:\Users\souzadan\FirmwareDevTools\Compilers\Jython2.7.0\Lib\atexit.py", line 24, in _run_exitfuncs
    func(*targs, **kargs)
  File "C:\Users\souzadan\FirmwareDevTools\Compilers\Jython2.7.0\Lib\threading.py", line 297, in _MainThread__exitfunc
    t.join()
  File "C:\Users\souzadan\FirmwareDevTools\Compilers\Jython2.7.0\Lib\threading.py", line 128, in join
    raise RuntimeError("cannot join current thread")
RuntimeError: cannot join current thread

Running Jython through the command line results in a shorter error log:

Traceback (most recent call last):
  File "somePythonCode.py", line 7, in <module>
    standard_library.install_aliases()
  File "C:\Users\souzadan\FirmwareDevTools\Compilers\Jython2.7.0\Lib\site-packages\future-0.15.2-py2.7.egg\future\standard_library\__init__.py", line 465, in install_aliases
    from future.backports.urllib import request
  File "C:\Users\souzadan\FirmwareDevTools\Compilers\Jython2.7.0\Lib\site-packages\future-0.15.2-py2.7.egg\future\backports\urllib\request.py", line 96, in <module>
    from future.backports import email
  File "C:\Users\souzadan\FirmwareDevTools\Compilers\Jython2.7.0\Lib\site-packages\future-0.15.2-py2.7.egg\future\backports\email\__init__.py", line 16, in <module>
    from future.utils import surrogateescape
  File "C:\Users\souzadan\FirmwareDevTools\Compilers\Jython2.7.0\Lib\site-packages\future-0.15.2-py2.7.egg\future\utils\surrogateescape.py", line 167, in <module>
    FS_ENCODING = 'ascii'; fn = b('[abc\xff]'); encoded = u('[abc\udcff]')
  File "C:\Users\souzadan\FirmwareDevTools\Compilers\Jython2.7.0\Lib\site-packages\future-0.15.2-py2.7.egg\future\utils\surrogateescape.py", line 25, in u
    return text.decode('unicode_escape')
UnicodeDecodeError: 'unicodeescape' codec can't decode bytes in position 4-10: illegal Unicode character

Does anybody have a clue of how to solve this error in the most elegant manner?

A bug was created at Jython.org with a critical severity because many people are utilizing already the latest Python modules and interpreter. And they might want to add Java functionality to their code. So they would basically have to take same path as I did. Backporting the project to Python 2.7 then to Jython 2.7 subsequentely.

At python-future a feature request bug was recorded too.

Prophet Daniel
  • 327
  • 2
  • 15
  • The thing is I don't want to change the library future, @kratenko... – Prophet Daniel Nov 10 '15 at 16:10
  • 1
    Jython and CPython have different enough threading models that you shouldn't expect to be able to run nontrivial concurrent code written for one on the other. You should expect to have to modify the code to get this to work. – Daniel Pryden Nov 10 '15 at 16:38
  • what happens if you run your program using jython directly without Eclipse? – jfs Nov 10 '15 at 20:11
  • @J.F.Sebastian, I updated the post with the error log for running Jython directly without Eclipse. – Prophet Daniel Nov 10 '15 at 20:18
  • Do you get the same result with this command: `jython2.7 -c "from future import standard_library; standard_library.install_aliases()"`? – jfs Nov 10 '15 at 20:21
  • Yes, @J.F.Sebastian, same results as last try even with this -c option. – Prophet Daniel Nov 10 '15 at 20:26
  • I get the same error during installation: `jython2.7 -m pip install future`: ` UnicodeDecodeError: ('unicodeescape', "'([^\\ud800-\\udbff]|\\A)[\\udc00-\\udfff]([^\\udc00-\\udfff]|\\Z)'", 4, 11, 'illegal Unicode character')` It seems like [Jython issue](http://bugs.jython.org/issue1707): it can't handle lone surrogates (`b'\udc00'.decode('unicode-escape')`). Python may use lone surrogates for various purposes e.g., [PEP 383](https://www.python.org/dev/peps/pep-0383/). `future` brings `'surrogateescape'` error handler from Python 3 to Python 2. – jfs Nov 10 '15 at 20:42
  • you could report `future` bug: "`future` shouldn't fail unless `'surrogateescape'` is used. Or report it as a feature request so that `future` would support Jython (there are probably other issues e.g., `json.loads('"\\udc00"')`). – jfs Nov 10 '15 at 20:52
  • @J.F.Sebastian try installing future with easy_install. Should do the trick. – Prophet Daniel Nov 10 '15 at 20:56
  • @DanielKaminskideSouza: it won't. the error is unrelated to `pip` or `easy_install`. There is no need: the error is ignored during installation i.e., `jython2.7 -c 'import future'` works. `jython2.7 -c "from future import standard_library; standard_library.install_aliases()"` produces the same `UnicodeDecodeError`. – jfs Nov 10 '15 at 21:05
  • @J.F.Sebastian, Thank you for digging into the problem. It looks we have a library improvement opportunity here. As I didn't find a bug reporting system at python-future, I've sent an email to lead developer Ed Schofield reporting this as a feature request so that future would support Jython just liker your suggestion. – Prophet Daniel Nov 10 '15 at 21:20
  • @DanielKaminskideSouza it looks like you found the bug reporting: system https://github.com/PythonCharmers/python-future/issues/184 . – Philip Durbin Nov 15 '15 at 12:04
  • When you say you went from CPython 3.5 to Jython 2.7 "because some java API's are going to be added"... what do you really mean? I'd advocate not using Jython at all and just creating API as an RPC interface from your process that people wanting to use _any_ language could call them. – gps Nov 16 '15 at 18:40
  • @gps What I have are java modules that I need to embed them inside my Python project. In my understanding Python ecossystem comprises CPython, Jython and IronPython. – Prophet Daniel Nov 17 '15 at 12:37
  • I'm doind this way because Python is a more suitable language for developing the high level functionality I'm looking for where Java provides a lot o libraries not present in python modules. Jython can expand its user database with a java community push. Because it's by far the largest programming community ever existed. So Python can get into advance those libraries available in java. Whenever a python module with a java library already developed would be needed, the work would be just wrap through a python script that library to make a brand new python module. – Prophet Daniel Nov 17 '15 at 12:38
  • You might want to look at [Py4J](https://www.py4j.org/). It is compatible with Python 3, while Jython is stuck on Python 2.7. – Roland Smith Jul 30 '17 at 08:12

1 Answers1

1

Relying on py4j, as Roland Smith suggests, would be a good approach for porting your app to a JVM environment.

J_H
  • 17,926
  • 4
  • 24
  • 44