33# Picard, the next-generation MusicBrainz tagger
44#
55# Copyright (C) 2006-2008, 2011-2014 Lukáš Lalinský
6- # Copyright (C) 2009, 2018-2023 Philipp Wolfer
6+ # Copyright (C) 2009, 2018-2024 Philipp Wolfer
77# Copyright (C) 2012 Chad Wilson
88# Copyright (C) 2012-2013 Michael Wiencek
99# Copyright (C) 2013-2024 Laurent Monin
3131# along with this program; if not, write to the Free Software
3232# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
3333
34+ import sys
3435
3536from picard .version import Version
3637
6768api_versions_tuple = [Version .from_string (v ) for v in api_versions ]
6869
6970
70- def crash_handler ():
71+ def crash_handler (exc : Exception = None ):
7172 """Implements minimal handling of an exception crashing the application.
7273 This function tries to log the exception to a log file and display
7374 a minimal crash dialog to the user.
7475 This function is supposed to be called from inside an except blog.
7576 """
76- import sys
77-
7877 # Allow disabling the graphical crash handler for debugging and CI purposes.
7978 if set (sys .argv ) & {'--no-crash-dialog' , '-v' , '--version' , '-V' , '--long-version' , '-h' , '--help' }:
8079 return
@@ -83,7 +82,14 @@ def crash_handler():
8382 # with minimum chance to fail.
8483 from tempfile import NamedTemporaryFile
8584 import traceback
86- trace = traceback .format_exc ()
85+ if exc :
86+ if sys .version_info < (3 , 10 ):
87+ trace_list = traceback .format_exception (None , exc , exc .__traceback__ )
88+ else :
89+ trace_list = traceback .format_exception (exc ) # pylint: disable=no-value-for-parameter
90+ trace = "" .join (trace_list )
91+ else :
92+ trace = traceback .format_exc ()
8793 logfile = None
8894 try :
8995 with NamedTemporaryFile (suffix = '.log' , prefix = 'picard-crash-' , delete = False ) as f :
@@ -124,3 +130,12 @@ def crash_handler():
124130 msgbox .setDefaultButton (QMessageBox .StandardButton .Close )
125131 msgbox .exec ()
126132 app .quit ()
133+
134+
135+ def register_excepthook ():
136+ def _global_exception_handler (exctype , value , traceback ):
137+ from picard import crash_handler
138+ crash_handler (exc = value )
139+ sys .__excepthook__ (exctype , value , traceback )
140+
141+ sys .excepthook = _global_exception_handler
0 commit comments