Skip to content

Commit

Permalink
Fix format_exception call for Python < 3.10
Browse files Browse the repository at this point in the history
  • Loading branch information
caprieldeluca committed Jan 13, 2024
1 parent 00ec054 commit c203c4b
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
3 changes: 2 additions & 1 deletion metadata.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
name=Puentes
description=Run external Python files inside QGIS.

version=1.0.0
version=1.0.1
qgisMinimumVersion=3.0
qgisMaximumVersion=3.99
author=Gabriel De Luca
Expand All @@ -18,6 +18,7 @@ tags=python, script, utilities.
homepage=https://github.com/caprieldeluca/puentes

changelog=
1.0.1 - Fix format_exception call for Python < 3.10.
1.0.0 - First stable release.
0.3.1 - Plog last traceback instead of raise exception on Python errors.
0.3.0 - Move settings to plugin/puentes, only store file_path.
Expand Down
17 changes: 14 additions & 3 deletions plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
import io
from pathlib import Path
import runpy
import traceback
import sys, traceback

from qgis.core import QgsApplication
from qgis.PyQt.QtGui import QIcon
Expand Down Expand Up @@ -94,8 +94,19 @@ def run_command(self):
runpy.run_path(self.file_path, init_globals={'plog': plog})
QSettings().setValue('plugins/puentes/file_path', self.file_path)

except Exception as e:
plog(*traceback.format_exception(e, limit=-1))
except Exception:
# From Python 3.10 only exc_value (the Exception instance) is needed,
# exc_type and exc_traceback are preserved for backwards compatibility
exc_type, exc_value, exc_traceback = sys.exc_info()
# Create a StackSummary object to get its length
stack_length = len(traceback.extract_tb(exc_traceback))
# Define limit as negative index to remove first frame
# (this file exception) from the stacktrace
limit = 1 - stack_length
plog(*traceback.format_exception(exc_type,
exc_value,
exc_traceback,
limit=limit))


#####
Expand Down

0 comments on commit c203c4b

Please sign in to comment.