From ff53925811e1f0c6b276a4650d0239db4d8353c1 Mon Sep 17 00:00:00 2001 From: SomberNight Date: Fri, 24 Nov 2023 22:36:37 +0000 Subject: [PATCH] qt console: fix tracebacks in windows binaries fixes https://github.com/spesmilo/electrum/issues/3315 The cause was that tracebacks look different whether stack items have source text available. When using the pyinstaller windows binary, there is no source text available. Example when running from source: ``` >>> a Traceback (most recent call last): File "...\electrum\gui\qt\console.py", line 256, in exec_command result = eval(command, self.namespace, self.namespace) File "", line 1, in NameError: name 'a' is not defined ``` Example for pyinstaller windows binary: ``` >>> a Traceback (most recent call last): File "electrum\gui\qt\console.py", line 256, in exec_command File "", line 1, in NameError: name 'a' is not defined ``` --- electrum/gui/qt/console.py | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/electrum/gui/qt/console.py b/electrum/gui/qt/console.py index a3fd20545..c05cc43a0 100644 --- a/electrum/gui/qt/console.py +++ b/electrum/gui/qt/console.py @@ -264,12 +264,18 @@ class Console(QtWidgets.QPlainTextEdit): exec(command, self.namespace, self.namespace) except SystemExit: self.close() - except BaseException: - traceback_lines = traceback.format_exc().split('\n') - # Remove traceback mentioning this file, and a linebreak - for i in (3,2,1,-1): - traceback_lines.pop(i) - self.appendPlainText('\n'.join(traceback_lines)) + except BaseException as e: + te = traceback.TracebackException.from_exception(e) + # rm part of traceback mentioning this file. + # (note: we rm stack items before converting to str, instead of removing lines from the str, + # as this is more reliable. The latter would differ whether the traceback has source text lines, + # which is not always the case.) + te.stack = traceback.StackSummary.from_list(te.stack[1:]) + tb_str = "".join(te.format()) + # rm last linebreak: + if tb_str.endswith("\n"): + tb_str = tb_str[:-1] + self.appendPlainText(tb_str) sys.stdout = tmp_stdout def keyPressEvent(self, event):