Title: Glob patterns like *.txt processed incorrectly on startup
Type: Severity: normal
Components: Versions:
Status: closed Resolution: duplicate
Dependencies: Superseder:
Assigned To: Nosy List: AndreasEK, pekka.klarck
Priority: Keywords:

Created on 2010-04-12.14:25:49 by pekka.klarck, last changed 2010-04-15.13:28:26 by pekka.klarck.

msg5680 (view) Author: Pekka Klärck (pekka.klarck) Date: 2010-04-12.14:25:48
1) When a glob pattern like *.txt is used on Windows and it matches existing files/dirs, Jython only gets the last matching item:

C:\path>dir /B x
C:\path>python26 -c "import sys; print sys.argv[1:]" x\*.txt
C:\path>jython25 -c "import sys; print sys.argv[1:]" x\*.txt

This problem probably doesn't appear on Unixes because there the shell expands patterns when they match before the command is actually called.

2) If a pattern matches no files, Jython gets nothing:

C:\path>python26 -c "import sys; print sys.argv[1:]" *.none
C:\path>jython25 -c "import sys; print sys.argv[1:]" *.none

I assume this problem appears also on Unixes.
msg5691 (view) Author: Pekka Klärck (pekka.klarck) Date: 2010-04-13.13:49:55
I investigated this problem a bit more and noticed that the bug is in the jython.bat script and thus affects only Windows. An easy way to see that the batch file is to blame is running the original examples with --print option:

C:\path>jython --print  -c "import sys; print sys.argv[1:]" x\*.txt
"C:\Program Files\Java\jre1.6.0_07\bin\java"  -Xmx512m -Xss1152k  -Dpython.home="C:\jython2.5.1" -Dpython.executable="c:\jython2.5.1\jython.bat" -classpath "C:\jython2.5.1\jython.jar" org.python.util.jython   -c "import sys; print sys.
argv[1:]" x\f2.txt

I also noticed that when the pattern matches no file, also the arguments after the pattern disappear:

C:\path>jython -c "import sys; print sys.argv[1:]" *.none these disappear too
msg5692 (view) Author: Pekka Klärck (pekka.klarck) Date: 2010-04-13.14:14:52
I investigated the jython.bat script and it seems that the following helper causes the problem:

rem remove quotes around first arg
for %%i in (%1) do set _CMP=%%~i
set _ARGS=%2
goto :EOF

When this helper gets e.g. *.txt or *.none in as %1, the pattern is expanded when it's used in `for %%i in (%1)`. If the pattern matches something, _CMP will get the last value, and when there's no match _CMP is not set at all. Not setting _CMP leads that later in :procArg arguments are erased.

I don't see any reason for the for loop in the above helper. If it is changed to just `set _CMP=%~1`, patterns aren't expanded but the helper seems to work otherwise as it should. This is what I get when executing a fixed version of the script:

C:\path>jython-fix  -c "import sys; print sys.argv[1:]" x\*.txt
['x\\f1.txt', 'x\\f2.txt']
C:\path>jython-fix  -c "import sys; print sys.argv[1:]" *.none second
['*.none', 'second']

Notice that when the pattern matches the results are different to what you got with CPython where you always get the string as-is. At least I'm fine with it because that's the effective behavior on Unixes where the shell expands patterns and that's also how Jython 2.2 behaves.

DISCLAIMER: My batch file coding skills aren't good enough to tell could the change I propose break something else. It would be great to hear a second opinion from someone who has been unfortunate to play more with batch files, preferably from someone who also knows the jython.bat file well.
msg5702 (view) Author: Pekka Klärck (pekka.klarck) Date: 2010-04-14.14:47:30
This is actually a duplicate of issue #1567. I don't know is the fix proposed by Andreas in that issue or by me here better.

See also issue #1599 about more problems with special characters in arguments to jython.bat.
msg5708 (view) Author: Andreas Ebbert-Karroum (AndreasEK) Date: 2010-04-15.11:32:16
Pekka, are you sure this solution works? According to this document:

%~1 Expands %1 and removes any surrounding quotation marks ("").

So the * would still be expanded.
msg5709 (view) Author: Pekka Klärck (pekka.klarck) Date: 2010-04-15.13:28:26
I don't think expands in that context means expanding patterns. More importantly, the fix actually worked.
Date User Action Args
2010-04-15 13:28:26pekka.klarcksetmessages: + msg5709
2010-04-15 11:32:17AndreasEKsetnosy: + AndreasEK
messages: + msg5708
2010-04-15 01:11:28pjenveysetstatus: open -> closed
resolution: duplicate
2010-04-14 14:47:30pekka.klarcksetmessages: + msg5702
2010-04-13 14:14:53pekka.klarcksetmessages: + msg5692
2010-04-13 13:49:56pekka.klarcksetmessages: + msg5691
2010-04-12 14:25:50pekka.klarckcreate