#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: | N/A | 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)
Change History (6)
comment:1 Changed 15 years ago by
Milestone: | → sage-3.0 |
---|
Changed 14 years ago by
Attachment: | trac_2861-scripts.patch added |
---|
comment:2 Changed 14 years ago by
Summary: | scripts do not exit with correct exit code when sys.exit() is used → [with patch; needs review] scripts do not exit with correct exit code when sys.exit() is used |
---|
comment:3 Changed 14 years ago by
Summary: | [with patch; needs review] scripts do not exit with correct exit code when sys.exit() is used → [with patch; positive review] scripts do not exit with correct exit code when sys.exit() is used |
---|
comment:4 Changed 14 years ago by
Milestone: | sage-3.4.1 → sage-3.3 |
---|---|
Resolution: | → fixed |
Status: | new → closed |
Merged in Sage 3.3.alpha2
comment:5 Changed 14 years ago by
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.
+1
was showed me this patch working in all the permutations of inputs... Looks good to me.