Skip to content

bpo-41718: subprocess imports grp and pwd on demand #24987

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 23, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 14 additions & 12 deletions Lib/shutil.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,6 @@
except ImportError:
_LZMA_SUPPORTED = False

try:
from pwd import getpwnam
except ImportError:
getpwnam = None

try:
from grp import getgrnam
except ImportError:
getgrnam = None

_WINDOWS = os.name == 'nt'
posix = nt = None
if os.name == 'posix':
Expand Down Expand Up @@ -843,8 +833,14 @@ def _is_immutable(src):

def _get_gid(name):
"""Returns a gid, given a group name."""
if getgrnam is None or name is None:
if name is None:
return None

try:
from grp import getgrnam
except ImportError:
return None

try:
result = getgrnam(name)
except KeyError:
Expand All @@ -855,8 +851,14 @@ def _get_gid(name):

def _get_uid(name):
"""Returns an uid, given a user name."""
if getpwnam is None or name is None:
if name is None:
return None

try:
from pwd import getpwnam
except ImportError:
return None

try:
result = getpwnam(name)
except KeyError:
Expand Down
21 changes: 9 additions & 12 deletions Lib/subprocess.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,6 @@
from time import monotonic as _time
import types

try:
import pwd
except ImportError:
pwd = None
try:
import grp
except ImportError:
grp = None
try:
import fcntl
except ImportError:
Expand Down Expand Up @@ -875,7 +867,9 @@ def __init__(self, args, bufsize=-1, executable=None,
"current platform")

elif isinstance(group, str):
if grp is None:
try:
import grp
except ImportError:
raise ValueError("The group parameter cannot be a string "
"on systems without the grp module")

Expand All @@ -901,7 +895,9 @@ def __init__(self, args, bufsize=-1, executable=None,
gids = []
for extra_group in extra_groups:
if isinstance(extra_group, str):
if grp is None:
try:
import grp
except ImportError:
raise ValueError("Items in extra_groups cannot be "
"strings on systems without the "
"grp module")
Expand All @@ -927,10 +923,11 @@ def __init__(self, args, bufsize=-1, executable=None,
"the current platform")

elif isinstance(user, str):
if pwd is None:
try:
import pwd
except ImportError:
raise ValueError("The user parameter cannot be a string "
"on systems without the pwd module")

uid = pwd.getpwnam(user).pw_uid
elif isinstance(user, int):
uid = user
Expand Down