gh-104522: Fix OSError raised when run a subprocess (#114195) · python/cpython@e2c097e · GitHub
Skip to content

Commit e2c097e

Browse files
gh-104522: Fix OSError raised when run a subprocess (#114195)
Only set filename to cwd if it was caused by failed chdir(cwd). _fork_exec() now returns "noexec:chdir" for failed chdir(cwd). Co-authored-by: Robert O'Shea <PurityLake@users.noreply.github.com>
1 parent 4c7e09d commit e2c097e

4 files changed

Lines changed: 29 additions & 18 deletions

File tree

Lib/subprocess.py

Lines changed: 8 additions & 3 deletions

Lib/test/test_subprocess.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2017,11 +2017,12 @@ def test_user(self):
20172017
"import os; print(os.getuid())"],
20182018
user=user,
20192019
close_fds=close_fds)
2020-
except PermissionError: # (EACCES, EPERM)
2021-
pass
2020+
except PermissionError as e: # (EACCES, EPERM)
2021+
self.assertIsNone(e.filename)
20222022
except OSError as e:
20232023
if e.errno not in (errno.EACCES, errno.EPERM):
20242024
raise
2025+
self.assertIsNone(e.filename)
20252026
else:
20262027
if isinstance(user, str):
20272028
user_uid = pwd.getpwnam(user).pw_uid
@@ -2065,8 +2066,8 @@ def test_group(self):
20652066
"import os; print(os.getgid())"],
20662067
group=group,
20672068
close_fds=close_fds)
2068-
except PermissionError: # (EACCES, EPERM)
2069-
pass
2069+
except PermissionError as e: # (EACCES, EPERM)
2070+
self.assertIsNone(e.filename)
20702071
else:
20712072
if isinstance(group, str):
20722073
group_gid = grp.getgrnam(group).gr_gid
@@ -2114,7 +2115,8 @@ def _test_extra_groups_impl(self, *, gid, group_list):
21142115
[sys.executable, "-c",
21152116
"import os, sys, json; json.dump(os.getgroups(), sys.stdout)"],
21162117
extra_groups=group_list)
2117-
except PermissionError:
2118+
except PermissionError as e:
2119+
self.assertIsNone(e.filename)
21182120
self.skipTest("setgroup() EPERM; this test may require root.")
21192121
else:
21202122
parent_groups = os.getgroups()
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
:exc:`OSError` raised when run a subprocess now only has *filename*
2+
attribute set to *cwd* if the error was caused by a failed attempt to change
3+
the current directory.

Modules/_posixsubprocess.c

Lines changed: 11 additions & 10 deletions

0 commit comments

Comments
 (0)