Simon Tatham<p>An oddity in <a href="https://hachyderm.io/tags/SageMath" class="mention hashtag" rel="nofollow noopener" target="_blank">#<span>SageMath</span></a> I noticed today:</p><p>If you want a Sage program to return failure to the OS, writing sys.exit(1) like you would in plain Python _works_, but has the side effect of printing a line to standard error containing just the text "1":</p><p>$ cat fail.sage<br>import sys<br>sys.exit(1)<br>$ sage fail.sage<br>1<br>$</p><p>Why? Because in ordinary Python, sys.exit("message") prints the message to stderr and then exits with status 1. So sys.exit looks at its argument to see if it has type 'int', and decides whether to print a message or not.</p><p>And one of the changes that Sage makes to the Python semantics is that integer literals aren't plain 'int' any more – they're autoconverted into some more interesting Sage type, so that writing e.g. '1/3' can give you a rational instead of a float, as computer algebra users expect.</p><p>So we're passing the value 1 to sys.exit as a _Sage_ numeric type, and apparently sys.exit decides that doesn't count as an int, but it's happy to call str() on it to convert it into an error message. Hence printing "1".</p><p>An easy workaround is sys.exit(int(1)).</p>