Title: -jar option doesn't set __name__ == '__main__' in
Type: behaviour Severity: normal
Components: None Versions: 2.5.0
Status: closed Resolution: wont fix
Dependencies: Superseder:
Assigned To: leosoto Nosy List: dhandy, fwierzbicki, leosoto
Priority: Keywords:

Created on 2009-07-31.19:17:33 by dhandy, last changed 2009-08-07.13:29:16 by fwierzbicki.

msg4973 (view) Author: David Handy (dhandy) Date: 2009-07-31.19:17:32
Test program:

if __name__ == '__main__':
    print "Hello from"
    print "__main__ not defined when is executed"

I put this file into the root of the stand-alone jython.jar

Command line:

java -jar jython.jar -jar jython.jar

Printed result:

__main__ not defined when is executed

This is not what is expected of a main program's startup script, and
has resulted in at least one confused email to the jython mailing
list asking why their startup script wouldn't run.
msg4998 (view) Author: Leonardo Soto (leosoto) Date: 2009-08-06.19:03:32
Well, seems like the __name__ is purposely set after the jar file name
instead of  "__main__" (see org.python.util.jython#runJar). 

So, time to improve docs.
msg4999 (view) Author: Leonardo Soto (leosoto) Date: 2009-08-06.19:10:03
BTW, I'm not implying that the idea of setting __name__ to foo.jar is a
good one, since it's not consistent with the way python modules can be
run from command line (the -m switch).

However, I guess that the backwards compatibility argument wins here.
msg5000 (view) Author: Leonardo Soto (leosoto) Date: 2009-08-06.19:24:54
Docs improved on

Unless someone wants to champion a backwards-incompatible modification
here, this is not going to change. Closing.
msg5001 (view) Author: David Handy (dhandy) Date: 2009-08-06.19:26:07
I doubt that too many applications relied on __name__ being set to the
name of the jar file. Besides, it is redundant.

I changed my example program to these 3 lines:
import sys
print "sys.argv:", sys.argv
print "__name__:", __name__

The result:
sys.argv: ['jython.jar']
__name__: jython.jar

So if they need to get the name of the jar file being executed, they can
always get it out of sys.argv[0]
msg5003 (view) Author: Leonardo Soto (leosoto) Date: 2009-08-06.20:31:39
I agree that the current behavior doesn't make much sense. OTOH,
checking for __name__ == '__main__' on is also a bit redundant.

The other argument here is that Jython 2.5 implemented the -m option,
which overlaps a bit with -jar. If I had to make a decision, I'd
deprecate -jar in favor or -m, and keep -jar unchanged.
msg5005 (view) Author: Frank Wierzbicki (fwierzbicki) Date: 2009-08-07.13:29:16
We can't make a change for 2.5.x like this, but we could talk about it
for 2.6 -- maybe you could bring it up on jython-dev to get wider opinions?
Date User Action Args
2009-08-07 13:29:16fwierzbickisetmessages: + msg5005
2009-08-06 20:31:40leosotosetmessages: + msg5003
2009-08-06 19:26:07dhandysetmessages: + msg5001
2009-08-06 19:24:54leosotosetstatus: open -> closed
assignee: leosoto
resolution: wont fix
messages: + msg5000
2009-08-06 19:10:03leosotosetmessages: + msg4999
2009-08-06 19:03:32leosotosetnosy: + leosoto
messages: + msg4998
2009-07-31 21:12:20fwierzbickisetnosy: + fwierzbicki
2009-07-31 19:17:33dhandycreate