How to silence “sys.excepthook is missing” error?

后端 未结 4 2029
再見小時候
再見小時候 2020-12-02 16:59

NB: I have not attempted to reproduce the problem described below under Windows, or with versions of Python other than 2.7.3.

The most reliable way to elicit the pro

4条回答
  •  旧巷少年郎
    2020-12-02 17:31

    I ran into this sort of issue myself today and went looking for an answer. I think a simple workaround here is to ensure you flush stdio first, so python blocks instead of failing during script shutdown. For example:

    --- a/testscript.py
    +++ b/testscript.py
    @@ -9,5 +9,6 @@ sys.excepthook = excepthook
     try:
         for n in range(20):
             print n
    +    sys.stdout.flush()
     except:
         pass
    

    Then with this script nothing happens, as the exception (IOError: [Errno 32] Broken pipe) is suppressed by the try...except.

    $ python testscript.py  | :
    $
    

提交回复
热议问题