[Python-Dev] spurious print and faulty return values: Is this a bug...?

Trent Mick trentm@ActiveState.com
Sat, 20 Jan 2001 15:35:56 -0800


... or am I missing something?

With Python 2.0 on Windows 2000, when playing with sys.exit() and sys.argv()
I get some unexpected results.

First here is a simple case that shows what I expect. I run "caller_good.py"
which call "callee_good.py" and prints its return value. "callee_good.py"
returns 42 so "42" is printed:
    ----------------- caller_good.py --------------------
    import os
    retval = os.system("python callee_good.py")
    print "caller: the retval is", retval
    -----------------------------------------------------

    ----------------- callee_good.py --------------------
    import sys
    sys.exit(42)
    -----------------------------------------------------

    D:\trentm\tmp>python caller_good.py
    caller: the retval is 42


Now here is what I didn't expect. I changed "caller_bad.py" to pass, as an
argument, the value that "callee_bad.py" should return.

    ----------------- caller_bad.py ---------------------
    import os
    retval = os.system("python callee_bad.py 42")
    print "caller: the retval is", retval
    -----------------------------------------------------

    ----------------- callee_bad.py ---------------------
    import sys
    firstarg = sys.argv[1]
    print "callee_bad: firstarg is", firstarg
    sys.exit(firstarg)
    -----------------------------------------------------

    D:\trentm\tmp>python caller_bad.py
    callee_bad: firstarg is 42
    42                             # <---- where did *this* print come from?
    caller: the retval is 1        # <---- and this retval is incorrect


Any ideas? I have not tried to track this down yet nor have I tried the
latest Python-CVS state.

Trent

-- 
Trent Mick
TrentM@ActiveState.com