Exit status of Sage

74 views
Skip to first unread message

Jernej Azarija

unread,
Jan 17, 2015, 1:28:22 PM1/17/15
to sage-...@googlegroups.com, Nathann Cohen
Hello,

I have to use Sage from an external comand and in order to do so I'll need to rely on the exit status given by Sage. Considering a trivial example

=============
$ cat foo.sage
exit(0)
=============


I get the following behaviour


=============
$ sage la.sage
0
$ echo $?
1
=============

There are two things I am confused with here.

1. Why do we print 0?

2. Why is the exit status 1 - indicating an error by UNIX standards?


Is there any reason behind this? If yes , what would be the best way to force my own exit status so that I can interpret the execution of Sage from an external program?

Best,

Jernej

Jernej Azarija

unread,
Jan 17, 2015, 1:31:50 PM1/17/15
to sage-...@googlegroups.com, nathan...@gmail.com
I suppose there is a bug in the sage bash script?

===============
101
102     # Return $out
103     echo "$out"
104 }
===============

should that be exit actually?

John H Palmieri

unread,
Jan 17, 2015, 5:02:53 PM1/17/15
to sage-...@googlegroups.com, nathan...@gmail.com

The underlying problem is that the "exit" function in Python doesn't accept any arguments, so "exit(0)" raises an error when you run it in Sage. This is why the exit status is nonzero. If your script had the line "exit()", it would run as expected. Given that, I'm not sure why it prints 0. If you do "exit(3)", it will print 3 instead.

--
John

Robert Bradshaw

unread,
Jan 17, 2015, 5:25:30 PM1/17/15
to sage-devel
You probably want sys.exit, which does take an exit status as an integer.

Jernej Azarija

unread,
Jan 18, 2015, 4:03:11 AM1/18/15
to Sage devel
The thing is that sys.exit works pretty much the same way

====
azi@goodegg:~$ cat foo.sage
import sys
sys.exit(42)
azi@goodegg:~$ sage foo.sage
42
azi@goodegg:~$ echo $?
1
====

And the print itself is extremely annoying. I need to call Sage 10^6 times from an external program and get 10^6 lines of non-needed output.



--
You received this message because you are subscribed to a topic in the Google Groups "sage-devel" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/sage-devel/CriQpiwWRLA/unsubscribe.
To unsubscribe from this group and all its topics, send an email to sage-devel+...@googlegroups.com.
To post to this group, send email to sage-...@googlegroups.com.
Visit this group at http://groups.google.com/group/sage-devel.
For more options, visit https://groups.google.com/d/optout.

Dima Pasechnik

unread,
Jan 18, 2015, 11:02:21 AM1/18/15
to sage-...@googlegroups.com
On 2015-01-18, Jernej Azarija <azi.s...@gmail.com> wrote:
> The thing is that sys.exit works pretty much the same way
>
>====
> azi@goodegg:~$ cat foo.sage
> import sys
> sys.exit(42)
> azi@goodegg:~$ sage foo.sage
> 42
> azi@goodegg:~$ echo $?
> 1
>====
>
> And the print itself is extremely annoying. I need to call Sage 10^6 times
> from an external program and get 10^6 lines of non-needed output.

this looks pretty bad, given that Sage's startup+exit time is 1 or 2 seconds
on a reasoably fast computer.
(unless your computations are still much slower, of course).

There should be ways to communicate with Sage without quitting in.

Jernej Azarija

unread,
Jan 18, 2015, 11:09:00 AM1/18/15
to Sage devel
On Sun, Jan 18, 2015 at 5:02 PM, Dima Pasechnik <dim...@gmail.com> wrote:
On 2015-01-18, Jernej Azarija <azi.s...@gmail.com> wrote:
> The thing is that sys.exit works pretty much the same way
>
>====
> azi@goodegg:~$ cat foo.sage
> import sys
> sys.exit(42)
> azi@goodegg:~$ sage foo.sage
> 42
> azi@goodegg:~$ echo $?
> 1
>====
>
> And the print itself is extremely annoying. I need to call Sage 10^6 times
> from an external program and get 10^6 lines of non-needed output.

this looks pretty bad, given that Sage's startup+exit time is 1 or 2 seconds
on a reasoably fast computer.
(unless your computations are still much slower, of course).

There should be ways to communicate with Sage without quitting in.
I agree. I ended up writing the output to a file and parsing it within the caller. Still pretty  ugly and inefficient but it works.

Though this still does not answer the question - why is Sage printing the exit status and not conforming to the standard behaviour of unix programs?

John H Palmieri

unread,
Jan 18, 2015, 11:32:45 AM1/18/15
to sage-...@googlegroups.com


On Sunday, January 18, 2015 at 8:09:00 AM UTC-8, Jernej Azarija wrote:


On Sun, Jan 18, 2015 at 5:02 PM, Dima Pasechnik <dim...@gmail.com> wrote:
On 2015-01-18, Jernej Azarija <azi.s...@gmail.com> wrote:
> The thing is that sys.exit works pretty much the same way
>
>====
> azi@goodegg:~$ cat foo.sage
> import sys
> sys.exit(42)
> azi@goodegg:~$ sage foo.sage
> 42
> azi@goodegg:~$ echo $?
> 1
>====
>
> And the print itself is extremely annoying. I need to call Sage 10^6 times
> from an external program and get 10^6 lines of non-needed output.

this looks pretty bad, given that Sage's startup+exit time is 1 or 2 seconds
on a reasoably fast computer.
(unless your computations are still much slower, of course).

There should be ways to communicate with Sage without quitting in.
I agree. I ended up writing the output to a file and parsing it within the caller. Still pretty  ugly and inefficient but it works.

Though this still does not answer the question - why is Sage printing the exit status and not conforming to the standard behaviour of unix programs?


Oh, I see: it's the preparsing. When you do "sys.exit(42)", the number 42 is a Sage integer, not a Python integer. So sys.exit doesn't know what to do with it. (I still don't know why that causes the number to be printed.)

So use sys.exit(int(42)) instead.

Vincent Delecroix

unread,
Jan 18, 2015, 12:08:16 PM1/18/15
to sage-...@googlegroups.com
2015-01-18 17:32 UTC+01:00, John H Palmieri <jhpalm...@gmail.com>:
> Oh, I see: it's the preparsing. When you do "sys.exit(42)", the number 42
> is a Sage integer, not a Python integer. So sys.exit doesn't know what to
> do with it. (I still don't know why that causes the number to be printed.)
>
> So use sys.exit(int(42)) instead.

Nice catch! Alternatively:

* use a .py extension (that way there will not be any preparsing)
* write sys.exit(int(42r))

Vincent

Samuel Lelievre

unread,
Jan 19, 2015, 2:54:26 AM1/19/15
to sage-...@googlegroups.com

Nice catch! Alternatively:

 * use a .py extension (that way there will not be any preparsing)
 * write sys.exit(int(42r))

You mean

    sys.exit(42r)

(42r is the raw Python integer 42, so you don't need to apply int to it).

: )
Reply all
Reply to author
Forward
0 new messages