PicoQuant / snAPI

snAPI is a Python wrapper which enables seamless communication and configuration with PicoQuant TCSPC devices.
https://picoquant.github.io/snAPI/
Other
25 stars 4 forks source link

modification of sys.excepthook leads to hidden tracebacks in Pycharm #5

Closed ChrisOKay closed 1 year ago

ChrisOKay commented 1 year ago

Describe the bug When importing snAPI, detailed traceback information is not shown in Pycharm.

To Reproduce Steps to reproduce the behavior:

  1. import snAPI
  2. raise any Exception, e.g. raise TypeError()
  3. Pycharm only shows "Process finished with exit code 1", but does not show the full traceback

Expected behavior Traceback should be shown.

The root cause of this behaviour seems to be the "system-wide" modification of sys.excepthook in Main.py (sys.excepthook = logException). Disabling this statement re-enables the tracebacks.

tpoint75 commented 1 year ago

Yes, the exceptions are directeded together with the snAPI messages to the stdout. We need them in the logfile too. Isn't there a console window in pycharm? I would also prefer to use sn.logprint instead of print in python to have all messages in the snAPI log!

ChrisOKay commented 1 year ago

Yes, Pycharm has a console. Nothing is shown here. I have tried it with a standard windows console, which shows the same behaviour:

snapi_exception.py with raise TypeError():

>python snapi_exception.py Traceback (most recent call last): File "C:\Users\dekarcch\Documents\Python\Temp\Neuer Ordner (2)\snapi_exception.py", line 2, in raise TypeError() TypeError >

snapi_exception.py with import Main; raise TypeError():

>python snapi_exception.py

>

A quick fix would be to "give the exception back" to the original exception hook at the end of the logException function: sys.__excepthook__(exception_type, exception, eTraceback).

---edit--- I am not sure how "stable" this excepthook-rerouting is. After importing snapi, someone else can easily modify the excepthook again, which disables the sn.logprint

import Main; import sys; sys.excepthook = sys.__excepthook__; raise TypeError()

So I would look for alternatives here, e.g. standard try/except constructs.

tpoint75 commented 1 year ago

Hello Chris, thank you for "give the exception back". I will add this to the next release!

I tried PyCharm and I see the console output and the exception!

PyCharm

tpoint75 commented 1 year ago

The problem with this hook is to get the exceptions from elsewhere, where I can't set a try/except manually and I need the exception in the log to give goo support. If you have an better solution, just tell me.

ChrisOKay commented 1 year ago

Thanks for your feedback. Regarding the console output: Yes, once sn is running, one can see this output. But in our usecase, we have a general "driver"-package were everything gets imported and only instantiated on demand. So any import side-effects such as hidden exceptions are not desired.

Regarding the exceptions: A highly philosophical question. IMHO it is not the API's job to log exceptions from external code, but only log/handle "internal" errors (with try/catch -> log constructs in the respective methods). External errors should be raised normally and be handled by the person who wrote the code. If logging is desired for this external code, the person will take care of this logging themselves.