Skip to content

Commit d72e8d4

Browse files
authored
bpo-41718: subprocess imports grp and pwd on demand (GH-24987)
The shutil and subprocess modules now only import the grp and pwd modules when they are needed, which is not the case by default in subprocess.
1 parent 76b5d71 commit d72e8d4

File tree

2 files changed

+23
-24
lines changed

2 files changed

+23
-24
lines changed

Lib/shutil.py

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -32,16 +32,6 @@
3232
except ImportError:
3333
_LZMA_SUPPORTED = False
3434

35-
try:
36-
from pwd import getpwnam
37-
except ImportError:
38-
getpwnam = None
39-
40-
try:
41-
from grp import getgrnam
42-
except ImportError:
43-
getgrnam = None
44-
4535
_WINDOWS = os.name == 'nt'
4636
posix = nt = None
4737
if os.name == 'posix':
@@ -843,8 +833,14 @@ def _is_immutable(src):
843833

844834
def _get_gid(name):
845835
"""Returns a gid, given a group name."""
846-
if getgrnam is None or name is None:
836+
if name is None:
837+
return None
838+
839+
try:
840+
from grp import getgrnam
841+
except ImportError:
847842
return None
843+
848844
try:
849845
result = getgrnam(name)
850846
except KeyError:
@@ -855,8 +851,14 @@ def _get_gid(name):
855851

856852
def _get_uid(name):
857853
"""Returns an uid, given a user name."""
858-
if getpwnam is None or name is None:
854+
if name is None:
859855
return None
856+
857+
try:
858+
from pwd import getpwnam
859+
except ImportError:
860+
return None
861+
860862
try:
861863
result = getpwnam(name)
862864
except KeyError:

Lib/subprocess.py

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -53,14 +53,6 @@
5353
from time import monotonic as _time
5454
import types
5555

56-
try:
57-
import pwd
58-
except ImportError:
59-
pwd = None
60-
try:
61-
import grp
62-
except ImportError:
63-
grp = None
6456
try:
6557
import fcntl
6658
except ImportError:
@@ -875,7 +867,9 @@ def __init__(self, args, bufsize=-1, executable=None,
875867
"current platform")
876868

877869
elif isinstance(group, str):
878-
if grp is None:
870+
try:
871+
import grp
872+
except ImportError:
879873
raise ValueError("The group parameter cannot be a string "
880874
"on systems without the grp module")
881875

@@ -901,7 +895,9 @@ def __init__(self, args, bufsize=-1, executable=None,
901895
gids = []
902896
for extra_group in extra_groups:
903897
if isinstance(extra_group, str):
904-
if grp is None:
898+
try:
899+
import grp
900+
except ImportError:
905901
raise ValueError("Items in extra_groups cannot be "
906902
"strings on systems without the "
907903
"grp module")
@@ -927,10 +923,11 @@ def __init__(self, args, bufsize=-1, executable=None,
927923
"the current platform")
928924

929925
elif isinstance(user, str):
930-
if pwd is None:
926+
try:
927+
import pwd
928+
except ImportError:
931929
raise ValueError("The user parameter cannot be a string "
932930
"on systems without the pwd module")
933-
934931
uid = pwd.getpwnam(user).pw_uid
935932
elif isinstance(user, int):
936933
uid = user

0 commit comments

Comments
 (0)