Skip to content

Commit 7a62b64

Browse files
committedJun 11, 2023
replace deprecated importlib.resources API usage
Switch away from the deprecated importlib.resources is_resource() and path() APIs. The importlib_resources package is used to get the new functionality on all supported versions of Python. Also enable annotations future so type names don't have to be quoted.
1 parent d24e043 commit 7a62b64

File tree

2 files changed

+18
-14
lines changed

2 files changed

+18
-14
lines changed
 

‎setup.cfg

+2
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ include_package_data = True
4242
package_dir =
4343
=src
4444
zip_safe = False
45+
install_requires =
46+
importlib_resources
4547

4648
[options.packages.find]
4749
where = src

‎src/libusb_package/__init__.py

+16-14
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright (c) 2021 Chris Reed
1+
# Copyright (c) 2021-2023 Chris Reed
22
#
33
# SPDX-License-Identifier: Apache-2.0
44
#
@@ -14,19 +14,16 @@
1414
# See the License for the specific language governing permissions and
1515
# limitations under the License.
1616

17+
from __future__ import annotations
18+
1719
import atexit
1820
import ctypes.util
1921
import functools
2022
import platform
2123
import sys
2224
from typing import (Any, Optional, TYPE_CHECKING)
2325

24-
# importlib.resources isn't available before Python 3.7, so if importing it
25-
# fails we import the backport.
26-
try:
27-
from importlib import resources
28-
except ImportError:
29-
import importlib_resources as resources # type:ignore
26+
import importlib_resources
3027

3128
from ._version import version as __version__
3229

@@ -46,15 +43,20 @@
4643
_LIBRARY_NAME = 'libusb-1.0' + _LIBRARY_EXT
4744

4845
@functools.lru_cache()
49-
def get_library_path() -> Optional["Path"]:
50-
"""@brief Returns the path to included library, if there is one."""
51-
if resources.is_resource(__name__, _LIBRARY_NAME):
52-
path_resource = resources.path(__name__, _LIBRARY_NAME)
53-
path = path_resource.__enter__()
46+
def get_library_path() -> Optional[Path]:
47+
"""@brief Returns the path to included library, if there is one.
48+
49+
The path is valid until the process exits. If the library was extracted from a zip in order to
50+
be accessible as a file, it will be cleaned up with the process exits.
51+
"""
52+
lib_resource = importlib_resources.files(__name__).joinpath(_LIBRARY_NAME)
53+
if lib_resource.is_file():
54+
path_context = importlib_resources.as_file(lib_resource)
55+
path = path_context.__enter__()
5456

5557
@atexit.register
5658
def cleanup():
57-
path_resource.__exit__(None, None, None)
59+
path_context.__exit__(None, None, None)
5860

5961
return path
6062
else:
@@ -89,7 +91,7 @@ def find_library(candidate: str) -> Optional[str]:
8991
# dependency unless these functions are used.
9092

9193
@functools.lru_cache()
92-
def get_libusb1_backend() -> Optional["IBackend"]:
94+
def get_libusb1_backend() -> Optional[IBackend]:
9395
"""@brief Return a usb backend for pyusb."""
9496
import usb.backend.libusb1
9597
return usb.backend.libusb1.get_backend(find_library=find_library)

0 commit comments

Comments
 (0)