Opened 12 years ago

Closed 11 years ago

Last modified 11 years ago

#2861 closed defect (fixed)

[with patch; positive review] scripts do not exit with correct exit code when sys.exit() is used

Reported by: ddrake Owned by: was
Priority: major Milestone: sage-3.3
Component: user interface Keywords:
Cc: Merged in:
Authors: Reviewers:
Report Upstream: Work issues:
Branch: Commit:
Dependencies: Stopgaps:

Description

If I call sys.exit() from a Sage script, the script exits but not with the correct exit code. For example, the script

import sys

print 'exiting!'
sys.exit(1)

exits with exit code 0 when run from Sage:

$ sage exitcode.sage 
exiting!
1
$ echo $?
0

(the 1 gets printed because the preparser turns it into a Sage integer, and Python prints out anything except Python integers.) But the same script works properly when run from Python:

$ python exitcode.sage
exiting!
$ echo $?
1

I don't know if this is Sage or IPython behavior, but having this work would be really useful.

Attachments (1)

trac_2861-scripts.patch (931 bytes) - added by was 11 years ago.

Download all attachments as: .zip

Change History (6)

comment:1 Changed 12 years ago by mabshoff

  • Milestone set to sage-3.0

Changed 11 years ago by was

comment:2 Changed 11 years ago by was

  • Summary changed from scripts do not exit with correct exit code when sys.exit() is used to [with patch; needs review] scripts do not exit with correct exit code when sys.exit() is used

comment:3 Changed 11 years ago by rlm

  • Summary changed from [with patch; needs review] scripts do not exit with correct exit code when sys.exit() is used to [with patch; positive review] scripts do not exit with correct exit code when sys.exit() is used

+1

was showed me this patch working in all the permutations of inputs... Looks good to me.

comment:4 Changed 11 years ago by mabshoff

  • Milestone changed from sage-3.4.1 to sage-3.3
  • Resolution set to fixed
  • Status changed from new to closed

Merged in Sage 3.3.alpha2

comment:5 Changed 11 years ago by ddrake

I was going to reopen this ticket, but instead I'll leave this comment as warning to anyone else trying to use this:

If you do sys.exit(0) in a Sage script, because of preparsing, you effectively get sys.exit(Integer(0)), which results in the script exiting with code 1! This is not what anyone would expect! This is because of how sys.exit works when given non-Python-integer arguments. To make sure that you get the desired behavior, use int inside the call to get a Python integer: sys.exit(int(2)) or whatever.

Note: See TracTickets for help on using tickets.