Skip to content

Commit 3577830

Browse files
committed
Fix resource file access when file does not exists
1 parent b112001 commit 3577830

File tree

2 files changed

+19
-9
lines changed

2 files changed

+19
-9
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: 12 additions & 5 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
@@ -65,7 +65,7 @@
6565
import logging
6666
import os
6767
import sys
68-
from typing import NamedTuple, Optional
68+
from typing import NamedTuple
6969

7070
import importlib.resources as importlib_resources
7171

@@ -237,9 +237,16 @@ 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+
package_dir_context = importlib_resources.as_file(
243+
importlib_resources.files(package)
244+
)
245+
path = _file_manager.enter_context(package_dir_context)
246+
return str(path.absolute() / resource)
247+
248+
file_context = importlib_resources.as_file(traversable)
249+
243250
path = _file_manager.enter_context(file_context)
244251
return str(path.absolute())
245252

0 commit comments

Comments
 (0)