[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)

<!--

If you know who should review your pull request, please assign it to that
person, otherwise the pull request would get assigned randomly.

If your pull request is for a specific language, please add the appropriate
lang label.

-->

Closes #35810

COPYBARA_INTEGRATE_REVIEW=https://github.com/grpc/grpc/pull/35810 from XuanWang-Amos:fix_import_pkg_resources_2 70fff7a7c3
PiperOrigin-RevId: 604398041
pull/35573/head
Xuan Wang 1 year ago committed by Copybara-Service
parent 3ebccf55cd
commit 5c988a47c4
  1. 6
      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.

Loading…
Cancel
Save