Skip to content

Commit b8861aa

Browse files
committed
Fix resource file access when file does not exists
1 parent 57775e1 commit b8861aa

File tree

2 files changed

+13
-8
lines changed

2 files changed

+13
-8
lines changed

src/silx/gui/icons.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# /*##########################################################################
22
#
3-
# Copyright (c) 2016-2023 European Synchrotron Radiation Facility
3+
# Copyright (c) 2016-2025 European Synchrotron Radiation Facility
44
#
55
# Permission is hereby granted, free of charge, to any person obtaining a copy
66
# of this software and associated documentation files (the "Software"), to deal
@@ -397,9 +397,12 @@ def getQFile(name):
397397

398398
for format_ in _supported_formats:
399399
format_ = str(format_)
400-
filename = silx.resources._resource_filename(
401-
f"{name}.{format_}", default_directory="gui/icons"
402-
)
400+
try:
401+
filename = silx.resources._resource_filename(
402+
f"{name}.{format_}", default_directory="gui/icons"
403+
)
404+
except ValueError:
405+
continue
403406
qfile = qt.QFile(filename)
404407
if qfile.exists():
405408
return qfile

src/silx/resources/__init__.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# /*##########################################################################
22
#
3-
# Copyright (c) 2016-2023 European Synchrotron Radiation Facility
3+
# Copyright (c) 2016-2025 European Synchrotron Radiation Facility
44
#
55
# Permission is hereby granted, free of charge, to any person obtaining a copy
66
# of this software and associated documentation files (the "Software"), to deal
@@ -237,9 +237,11 @@ def _get_resource_filename(package: str, resource: str) -> str:
237237
:return: Abolute resource path in the file system
238238
"""
239239
# Caching prevents extracting the resource twice
240-
file_context = importlib_resources.as_file(
241-
importlib_resources.files(package) / resource
242-
)
240+
traversable = importlib_resources.files(package).joinpath(resource)
241+
if not traversable.is_file() and not traversable.is_dir():
242+
raise ValueError(f"Resource {resource} does not exists in {package}")
243+
file_context = importlib_resources.as_file(traversable)
244+
243245
path = _file_manager.enter_context(file_context)
244246
return str(path.absolute())
245247

0 commit comments

Comments
 (0)