Issue2599

classification
Title: Cannot handle network paths under Windows
Type: behaviour Severity: major
Components: Core Versions: Jython 2.7
Milestone: Jython 2.7.2
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: stefan.richthofer Nosy List: stefan.richthofer, tkohn, zyasoft
Priority: high Keywords:

Created on 2017-06-25.15:53:37 by tkohn, last changed 2017-09-05.20:53:10 by zyasoft.

Messages
msg11447 (view) Author: Tobias Kohn (tkohn) Date: 2017-06-25.15:53:36
When Jython 2.7.1-rc2 is executed from a network location under Windows, it fails to access any file from within the jar itself.

Reproducing the error with "Jython-standalone-2.7.1-rc2" is a little bit tricky. If we just open a command line and execute Jython, Windows automatically assigns a drive letter to the network location and Jython works just fine. The problem occurs, however, if we use "PowerShell" and carefully avoid assigning a letter to the network path.

Using standard java to start Jython results in the following output:
#############################################################################
$> java -jar jython-standalone-2.7.1-rc2.jar
*sys-package-mgr*: can't create package cache dir, '\\MY-PC\Shared\Jython\MY-PC\Shared\Jython\cachedir\packages'
Exception in thread "main" ImportError: Cannot import site module and its dependencies: No module named site
Determine if the following attributes are correct:
  *sys.path: [\\\\MY-PC\\Shared\\Jython\\MY-PC\\Shared\\Jython\\Lib,__classpath__, __pyclasspath__/]
   This attribute might be including the wrong directories, such as from CPython
  *sys.prefix: \\\\MY-PC\\Shared\\Jython\\MY-PC\\Shared\\Jython
   This attribute is set by the system property python.home, although it can
   be often automatically determined by the location of the Jython jar file
   
You can use the -S option or python.import.site=false to not import the site module
#############################################################################

So, we try the -S option, yielding the following scenario. At first, everything seems fine, but then, the "random"-random cannot be found.
#############################################################################$> java -jar jython-standalone-2.7.1-rc2.jar -S
*sys-package-mgr*: can't create package cache dir, '\\MY-PC\Shared\Jython\MY-PC\Shared\Jython\cachedir\packages'
Jython 2.7.1rc2 (default:f66327aa5de9, May 29 2017, 17:56:49)
[Java HotSpot(TM) 64-Bit Server VM (Oracle Corporation)] on java1.8.0_131
>>> from random import randint
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named random
#############################################################################

Note how the path to jython.jar is obviously doubled:
\\\\MY-PC\\Shared\\Jython\\MY-PC\\Shared\\Jython
It seems that the path "MY-PC/Shared/Jython" lacks the proper reference to the "root", resulting in Java interpreting the path relative to the current directory instead.

The problem did not occur with Jython 2.7.0
msg11448 (view) Author: Tobias Kohn (tkohn) Date: 2017-06-25.21:46:41
I think I found the problem.

In the source file "org/python/core/Py.java" you find the method "public static String getJarFileNameFromURL(URL url)" at line 2633. Inside this method, the offending code is at line 2647:
###################################################################################
int start = JAR_URL_PREFIX.length();
if (Platform.IS_WINDOWS) {
    start++;
}
jarFileName = urlString.substring(start, jarSeparatorIndex);
###################################################################################
On Windows, a network path has an URL like "jar:file://MY-PC/Shared/Jython/jython.jar". However, the instruction "start++" clips one of the slashes away, resulting in "/MY-PC/Shared" instead of "//MY-PC/Shared".

I do not know why this "start++"-instruction is there in the first place. It was obviously not there in Jython 2.7.0. After eliminating it, Jython can be run from a network path without any problems again. Yet, I might be wrong and the actual problem might be found elsewhere.
msg11453 (view) Author: Jim Baker (zyasoft) Date: 2017-06-30.18:04:44
The change mentioned was part of
https://github.com/jythontools/jython/commit/a9d533d5b8869ab050320feccaae17d3509ca86f

Adding Stefan as the originator of that commit, since I don't have enough background as why this change was made.
msg11454 (view) Author: Tobias Kohn (tkohn) Date: 2017-07-01.07:09:37
I just wanted to add that I usually do not use Jython from inside the PowerShell. I only used it so as to reproduce the bug.

Actually, we created a Python IDE that uses Jython from a graphical user interface. This means that Jython is started as part of GUI and not via a terminal/console at all. It is in this setting that we got feedback from users who could not run the scripts anymore. Further testing then showed that the network paths seemed to be the problem.

When we tried to reproduce the problem with Jython from Windows' "cmd", we found that the problem does not occur because "cmd" assigns a drive letter for network drives. This is the only case known to us so far where the problem does not show. I. e. it is not a bug related to the PowerShell but to Windows in general.
msg11456 (view) Author: Stefan Richthofer (stefan.richthofer) Date: 2017-07-02.15:25:53
Worth noting that this is (partly) a follow-up issue of the solution to http://bugs.jython.org/issue2386.

if (Platform.IS_WINDOWS) {
    start++;
}
was used, because Py.getJarFileNameFromURL prepends a slash before the Windows drive letter.

Maybe we can guard the networkpath case via

if (Platform.IS_WINDOWS && !urlString.charAt(start+1) == '/') {
    start++;
}

Unless you see some flaw with this, I'd experimentally check it in.
msg11458 (view) Author: Tobias Kohn (tkohn) Date: 2017-07-02.16:25:05
I actually solved the problem the same way and it worked fine in all my tests so far, particularly in connection with network paths.

Thank you!
msg11474 (view) Author: Stefan Richthofer (stefan.richthofer) Date: 2017-07-10.23:11:01
Fixed as of https://hg.python.org/jython/rev/7f32334eaec6.
@Tobias Kohn Would be good if you could confirm this.
History
Date User Action Args
2017-09-05 20:53:10zyasoftsetstatus: pending -> closed
2017-07-10 23:11:01stefan.richthofersetpriority: high
assignee: stefan.richthofer
status: open -> pending
messages: + msg11474
resolution: fixed
2017-07-02 16:25:05tkohnsetmessages: + msg11458
2017-07-02 15:25:54stefan.richthofersetmessages: + msg11456
2017-07-01 07:09:38tkohnsetmessages: + msg11454
2017-06-30 19:12:03zyasoftsetnosy: + stefan.richthofer
2017-06-30 18:04:44zyasoftsetnosy: + zyasoft
messages: + msg11453
milestone: Jython 2.7.1 -> Jython 2.7.2
2017-06-25 21:46:41tkohnsetmessages: + msg11448
2017-06-25 15:53:37tkohncreate