trio icon indicating copy to clipboard operation
trio copied to clipboard

Trio and PyCharm fighting over sys.excepthook

Open ernestum opened this issue 4 years ago • 11 comments

The Apport error reporting mechanism of Ubuntu as well as PyCharm register custom sys.excepthooks. This causes the warning:

trio/_core/_multierror.py:472: RuntimeWarning: You seem to already have a custom sys.excepthook handler installed. I'll skip installing Trio's custom handler, but this means MultiErrors will not show full tracebacks.

and prevents uncaught multierrors from being printed properly.

I can reproduce this problem by simply running the following test.pyt:

import trio

from within PyCharm or on any Ubuntu/Mint machine that has the python3-apport packet installed.

A possible workaround is to run sys.excepthook = sys.__excepthook__ before importing trio but then the functionality of Apport/PyCharm will probably break.

Unfortunately there is no way to register multiple excepthooks in Python. However we might be able to "wrap" existing excepthooks instead of giving up in desparation?

ernestum avatar May 25 '20 14:05 ernestum

Hi!

Thanks for this detailed report. This was recently fixed (original issue #1065, fixed in #1528). This was included in the release 0.15.0 (and subsequent 0.15.1). Are you still seeing this issue even on the latest release?

tjstum avatar May 26 '20 00:05 tjstum

Oh, I guess you also described an issue with PyCharm's excepthook! Maybe we should leave this open but rename/change the summary to narrow the scope.

tjstum avatar May 26 '20 00:05 tjstum

Yeah, can you give more details on the PyCharm issue?

In PyCharm, what do you get if you do:

print(repr(sys.excepthook))
print(repr(sys.__excepthook__))

?

njsmith avatar May 26 '20 01:05 njsmith

<built-in function excepthook>
<built-in function excepthook>

Process finished with exit code 0

I hope it helped.

mozesa avatar May 26 '20 05:05 mozesa

Huh. And what do you get from 'print(sys.excepthook is sys.excepthook)'?

On Mon, May 25, 2020, 22:50 András Mózes [email protected] wrote:

Process finished with exit code 0

I hope it helped.

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/python-trio/trio/issues/1553#issuecomment-633820405, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAEU42BRFGTPG5BFTI3G7H3RTNKB3ANCNFSM4NJR55HA .

njsmith avatar May 26 '20 07:05 njsmith

I made a big misstake :(

import sys

import trio


print(repr(sys.excepthook))
print(repr(sys.__excepthook__))
<function trio_excepthook at 0x0000026AC55BE5E0>
<built-in function excepthook>

and now print(sys.excepthook is sys.__excepthook__) is False

(previously I didn't import trio)

mozesa avatar May 26 '20 07:05 mozesa

I was able to reproduce this using PyCharm. You have to use PyCharm's "debug" feature, not the normal "run" command:

Connected to pydev debugger (build 201.7223.92)
/.../site-packages/trio/_core/_multierror.py:472: RuntimeWarning: You seem to already have a custom sys.excepthook handler installed. I'll skip installing Trio's custom handler, but this means MultiErrors will not show full tracebacks.
  category=RuntimeWarning
<function _fallback_excepthook at 0x7fa29db23840>
<built-in function excepthook>

I think this is their excepthook.

tjstum avatar May 27 '20 17:05 tjstum

Yes it is true, in debug, always there is the warning.

mozesa avatar May 27 '20 18:05 mozesa

Well, adding special monkeypatches for each random library is obviously a terrible long-term approach. But in the short term, that looks like a pretty easy special monkeypatch to do. Something like: check for the PyCharm excepthook, and if it's found then monkeypatch the default_excepthook variable inside that module.

njsmith avatar May 27 '20 23:05 njsmith

I have also noticed this warning in PyCharm but have so far ignored it 😬

dhirschfeld avatar May 28 '20 01:05 dhirschfeld

same warning for WingPro8, just ignore it.

C:\Python310\lib\site-packages\trio\_core\_multierror.py:511: RuntimeWarning: You seem to already have a custom sys.excepthook handler installed. I'll skip installing Trio's custom handler, but this means MultiErrors will not show full tracebacks.
  warnings.warn(
<bound method CWhenPrintExceptionSupport.__ExceptHook of <debug.tserver.dbgutils.CWhenPrintExceptionSupport object at 0x000001D295CE65C0>>
<built-in function excepthook>
False

honglei avatar Jul 09 '22 14:07 honglei

Closing this in favor of https://github.com/agronholm/exceptiongroup/issues/23.

Zac-HD avatar Oct 17 '23 11:10 Zac-HD