Debug script under pdb, how to avoid a bunch of errors caused by the exit()?

Oscar Benjamin oscar.j.benjamin at gmail.com
Thu Sep 6 12:33:00 EDT 2018


On Thu, 6 Sep 2018 at 10:59, Jach Fong <jfong at ms4.hinet.net> wrote:
>
>      Here the script file, test0.py:
> --------------------------
> password = 'bad'
> if password == 'bad':
>      print('bad password')
>      exit()
> else:
>      print('good password')
>
> print('something else to do')
> --------------------
>
>      When running it under Python3.4 Windows Vista, no problem at all.
>
> D:\Works\Python>py test0.py
> bad password
>
>      But if debug it under pdb:
>
> D:\Works\Python>py -m pdb test0.py
> [...]
> ValueError: I/O operation on closed file.
> Uncaught exception. Entering post mortem debugging
>
>      How to get rid of these?

I haven't noticed this behaviour before but then I normally use
functions rather than writing my code at top level and quitting with
exit(). If I had written your script then it might look something
like:

def main():
    password = 'bad'
    if password == 'bad':
        print('bad password')
        return  # <-- Not exit()
    else:
        print('good password')

    print('something else to do')

if __name__ == "__main__":
    main()

Now since you're no longer using exit() to end the program it doesn't
need to screw up pdb.

--
Oscar



More information about the Python-list mailing list