From 7b17c15ab57e0731491b1b6e64aa0eb6a7e71df0 Mon Sep 17 00:00:00 2001 From: Cal Jacobson Date: Sun, 11 Aug 2024 13:16:13 -0500 Subject: [PATCH 1/4] handle failures to find a user home directory In certain environments (notably, the [bazel sandbox](https://bazel.build/docs/sandboxing) on windows), it is possible for `pathlib.Path('~').expanduser()` to fail to find the user home directory and [raise a `RuntimeError`](https://docs.python.org/3/library/pathlib.html#pathlib.Path.expanduser). This causes `distutils` (and ultimately, `setuptools`) to fail. With this patch, we catch and handle the exception by logging a warning and continuing without a user's config file. motivated by https://github.com/bazelbuild/rules_python/pull/1067 --- distutils/dist.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/distutils/dist.py b/distutils/dist.py index 115302b3..9b16058d 100644 --- a/distutils/dist.py +++ b/distutils/dist.py @@ -354,7 +354,12 @@ def _gen_paths(self): prefix = '.' * (os.name == 'posix') filename = prefix + 'pydistutils.cfg' if self.want_user_cfg: - yield pathlib.Path('~').expanduser() / filename + try: + user_home = pathlib.Path('~').expanduser() + except RuntimeError: + self.announce("Failed to locate user home directory. Skipping user config.", logging.WARNING) + else: + yield user_home / filename # All platforms support local setup.cfg yield pathlib.Path('setup.cfg') From 9824bd88e4f9dd6e5ffe21cc8edebab352e6ddc5 Mon Sep 17 00:00:00 2001 From: Cal Jacobson Date: Sun, 25 Aug 2024 23:18:34 -0500 Subject: [PATCH 2/4] Update distutils/dist.py Co-authored-by: Anderson Bravalheri --- distutils/dist.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/distutils/dist.py b/distutils/dist.py index 9b16058d..bc631c42 100644 --- a/distutils/dist.py +++ b/distutils/dist.py @@ -355,11 +355,9 @@ def _gen_paths(self): filename = prefix + 'pydistutils.cfg' if self.want_user_cfg: try: - user_home = pathlib.Path('~').expanduser() + yield pathlib.Path('~').expanduser() / filename except RuntimeError: - self.announce("Failed to locate user home directory. Skipping user config.", logging.WARNING) - else: - yield user_home / filename + warnings.warn("Failed to locate user home directory. Skipping user config.") # All platforms support local setup.cfg yield pathlib.Path('setup.cfg') From d54bfb030bf334bded0ed131922cdb8a4cfd1fb9 Mon Sep 17 00:00:00 2001 From: Cal Jacobson Date: Mon, 26 Aug 2024 00:03:31 -0500 Subject: [PATCH 3/4] format --- distutils/dist.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/distutils/dist.py b/distutils/dist.py index 0d3c4015..19f4cc5c 100644 --- a/distutils/dist.py +++ b/distutils/dist.py @@ -349,7 +349,9 @@ def _gen_paths(self): try: yield pathlib.Path('~').expanduser() / filename except RuntimeError: - warnings.warn("Failed to locate user home directory. Skipping user config.") + warnings.warn( + "Failed to locate user home directory. Skipping user config." + ) # All platforms support local setup.cfg yield pathlib.Path('setup.cfg') From a13507cc6170a63271ac2a79d588c5a962dcf2fb Mon Sep 17 00:00:00 2001 From: "Jason R. Coombs" Date: Mon, 26 Aug 2024 16:05:57 -0400 Subject: [PATCH 4/4] Simply suppress the exception. --- distutils/dist.py | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/distutils/dist.py b/distutils/dist.py index 19f4cc5c..60edc5b5 100644 --- a/distutils/dist.py +++ b/distutils/dist.py @@ -346,12 +346,8 @@ def _gen_paths(self): prefix = '.' * (os.name == 'posix') filename = prefix + 'pydistutils.cfg' if self.want_user_cfg: - try: + with contextlib.suppress(RuntimeError): yield pathlib.Path('~').expanduser() / filename - except RuntimeError: - warnings.warn( - "Failed to locate user home directory. Skipping user config." - ) # All platforms support local setup.cfg yield pathlib.Path('setup.cfg')