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.
This commit is contained in:
Victor Stinner 2021-03-23 17:42:51 +01:00 committed by GitHub
parent 76b5d714e4
commit d72e8d4875
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 23 additions and 24 deletions

View file

@ -32,16 +32,6 @@ try:
except ImportError: except ImportError:
_LZMA_SUPPORTED = False _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' _WINDOWS = os.name == 'nt'
posix = nt = None posix = nt = None
if os.name == 'posix': if os.name == 'posix':
@ -843,8 +833,14 @@ def _is_immutable(src):
def _get_gid(name): def _get_gid(name):
"""Returns a gid, given a group name.""" """Returns a gid, given a group name."""
if getgrnam is None or name is None: if name is None:
return None return None
try:
from grp import getgrnam
except ImportError:
return None
try: try:
result = getgrnam(name) result = getgrnam(name)
except KeyError: except KeyError:
@ -855,8 +851,14 @@ def _get_gid(name):
def _get_uid(name): def _get_uid(name):
"""Returns an uid, given a user name.""" """Returns an uid, given a user name."""
if getpwnam is None or name is None: if name is None:
return None return None
try:
from pwd import getpwnam
except ImportError:
return None
try: try:
result = getpwnam(name) result = getpwnam(name)
except KeyError: except KeyError:

View file

@ -53,14 +53,6 @@ import contextlib
from time import monotonic as _time from time import monotonic as _time
import types import types
try:
import pwd
except ImportError:
pwd = None
try:
import grp
except ImportError:
grp = None
try: try:
import fcntl import fcntl
except ImportError: except ImportError:
@ -875,7 +867,9 @@ class Popen(object):
"current platform") "current platform")
elif isinstance(group, str): elif isinstance(group, str):
if grp is None: try:
import grp
except ImportError:
raise ValueError("The group parameter cannot be a string " raise ValueError("The group parameter cannot be a string "
"on systems without the grp module") "on systems without the grp module")
@ -901,7 +895,9 @@ class Popen(object):
gids = [] gids = []
for extra_group in extra_groups: for extra_group in extra_groups:
if isinstance(extra_group, str): if isinstance(extra_group, str):
if grp is None: try:
import grp
except ImportError:
raise ValueError("Items in extra_groups cannot be " raise ValueError("Items in extra_groups cannot be "
"strings on systems without the " "strings on systems without the "
"grp module") "grp module")
@ -927,10 +923,11 @@ class Popen(object):
"the current platform") "the current platform")
elif isinstance(user, str): elif isinstance(user, str):
if pwd is None: try:
import pwd
except ImportError:
raise ValueError("The user parameter cannot be a string " raise ValueError("The user parameter cannot be a string "
"on systems without the pwd module") "on systems without the pwd module")
uid = pwd.getpwnam(user).pw_uid uid = pwd.getpwnam(user).pw_uid
elif isinstance(user, int): elif isinstance(user, int):
uid = user uid = user