Skip to content

Commit

Permalink
Recover from getfile TypeError
Browse files Browse the repository at this point in the history
since pytest-xdist adds <module '__main__' (built-in)> into stack
  • Loading branch information
Joonalai committed Nov 26, 2023
1 parent 35546fd commit 6865dbe
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

- Fix: Recover from `TypeError` when getting plugin path (when using pytest-xdist)

## [0.3.1] - 2023-08-08

- Feature: Add `exc_info` and `stack_info` parameters to message bar logger for capturing exception.
Expand Down
14 changes: 12 additions & 2 deletions tools/resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
from qgis.PyQt.QtWidgets import QDialog, QWidget

__copyright__ = (
"Copyright 2019, 3Liz, 2020-2021 Gispo Ltd, 2022 National Land Survey of Finland"
"Copyright 2019, 3Liz, 2020-2021 Gispo Ltd, "
"2022-2023 National Land Survey of Finland"
)
__license__ = "GPL version 3"
__email__ = "[email protected]"
Expand Down Expand Up @@ -56,7 +57,16 @@ def _plugin_path_dependency() -> str:
top_level_name, *_ = module_name.split(".", maxsplit=1)
top_level_module = sys.modules.get(top_level_name)
if top_level_module is not None:
top_level_directory = Path(inspect.getfile(top_level_module)).parent
try:
top_level_directory = Path(inspect.getfile(top_level_module)).parent
except TypeError:
# TypeError gets thrown if
# pytest-xdist is used in tests since it
# adds <module '__main__' (built-in)> into stack
# The 'getfile' function throws a TypeError for built-in modules.
# We are only interested in custom modules, so it is safe to ignore
# the error.
continue
if (top_level_directory / "metadata.txt").exists():
return str(top_level_directory)

Expand Down

0 comments on commit 6865dbe

Please sign in to comment.