From 5c988a47c4285bf8973a96c3a45bc15a7b9b678b Mon Sep 17 00:00:00 2001 From: Xuan Wang Date: Mon, 5 Feb 2024 12:25:40 -0800 Subject: [PATCH] [Python Misc] Fix issue when deprecate pkg_resources.resource_filename (#35810) We changed `pkg_resources.resource_filename` to `importlib.resources.files`, but the return of `resources.files()` API is a traversable object implementing a subset of the [pathlib.Path](https://docs.python.org/3/library/pathlib.html#pathlib.Path) interface instead of string, thus we're seeing errors like `AttributeError: 'PosixPath' object has no attribute 'rstrip'`. This PR converts the result of `files()` to str to prevent those kinds of errors. Test run: * [x] [grpc/core/master/linux/grpc_interop_tocloud](http://sponge/ee5d493b-e23e-4358-8084-3dba1df1d42f) Closes #35810 COPYBARA_INTEGRATE_REVIEW=https://github.com/grpc/grpc/pull/35810 from XuanWang-Amos:fix_import_pkg_resources_2 70fff7a7c3c65fd2389f41aaff27ed583b54c79a PiperOrigin-RevId: 604398041 --- tools/distrib/python/grpcio_tools/grpc_tools/protoc.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/tools/distrib/python/grpcio_tools/grpc_tools/protoc.py b/tools/distrib/python/grpcio_tools/grpc_tools/protoc.py index bc2b8c0276b..2d39d162059 100644 --- a/tools/distrib/python/grpcio_tools/grpc_tools/protoc.py +++ b/tools/distrib/python/grpcio_tools/grpc_tools/protoc.py @@ -45,14 +45,16 @@ def _get_resource_file_name( package_or_requirement: str, resource_name: str ) -> str: """Obtain the filename for a resource on the file system.""" + file_name = None if sys.version_info >= (3, 9, 0): - return ( + file_name = ( resources.files(package_or_requirement) / resource_name ).resolve() else: - return pkg_resources.resource_filename( + file_name = pkg_resources.resource_filename( package_or_requirement, resource_name ) + return str(file_name) # NOTE(rbellevi): importlib.abc is not supported on 3.4.