Search code examples
pythonbatch-filejythonwlstcommand-line-arguments

Jython vs CPython - sys module argument parsing


While writing a deployment script for use with the WebLogic Scripting Tool (12.1.3), I came across this inconsistency between Python 2.2.1 and Jython 2.2.1. If you pass command line arguments to each, they are parsed differently, as noted by this test program:

$ cat pytest.py
import sys
print sys.argv

When run, here are the results with each interpreter.

CPython 2.2.1:

$ /cygdrive/c/Python22/python.exe pytest.py a b 'c,d,e'
['pytest.py', 'a', 'b', 'c,d,e']

Jython 2.2.1:

$ /cygdrive/c/jython2.2.1/jython.bat pytest.py a b 'c,d,e'
['pytest.py', 'a', 'b', 'c', 'd', 'e']

The reason I am using Jython 2.2.1 is because it is the Python implementation used by WLST, so I don't believe I can upgrade to a later version or use the CPython interpreter to circumvent the issue in my use case.

Is this a bug? Jython's parse seems counterintuitive. Is there a way to parse arguments the CPython way in Jython? Thanks in advance.

Contents of jython.bat (included with Jython install):

$ cat jython.bat
@echo off
rem This file was generated by the Jython installer
rem Created on Mon Oct 31 13:19:59 PDT 2016 by smcgloth

set ARGS=

:loop
if [%1] == [] goto end
    set ARGS=%ARGS% %1
    shift
    goto loop
:end

"C:\Program Files\Java\jre1.8.0_101\bin\java.exe" -Dpython.home="C:\jython2.2.1" -classpath "C:\jython2.2.1\jython.jar;%CLASSPATH%" org.python.util.jython %ARGS%

Solution

  • I talked this over with my team this morning and we've figured out why I was experiencing this in a Linux environment, so I thought I'd post an answer in case it helps anyone else. One of my teammates wrote a startup script for the WLST that I had overlooked:

    $ cat wlst
    #!/bin/sh
    wlst.sh -skipWLSModuleScanning $@
    

    The culprit in my case was the -skipWLSModuleScanning flag. From the documentation:

    Use this option to reduce startup time by skipping package scanning and caching for WebLogic Server modules.

    It appears that skipping the package scanning must skip something crucial that affects the way Jython parses command line arguments.

    Here are the results when skipping WLS module scanning:

    $ wlst ~/pytest.py a b 'c,d,e'
    
    Initializing WebLogic Scripting Tool (WLST) ...
    
    Welcome to WebLogic Server Administration Scripting Shell
    
    Type help() for help on available commands
    
    ['pytest.py', 'a', 'b', 'c', 'd', 'e']
    

    And here are the results with the standard wlst.sh call, which is the result I would expect:

    $ wlst.sh ~/pytest.py a b 'c,d,e'
    
    Initializing WebLogic Scripting Tool (WLST) ...
    
    Welcome to WebLogic Server Administration Scripting Shell
    
    Type help() for help on available commands
    
    ['/home/tdmsadm/pytest.py', 'a', 'b', 'c,d,e']