[Python-checkins] bpo-41718: subprocess imports grp and pwd on demand (GH-24987)

vstinner webhook-mailer at python.org
Tue Mar 23 12:43:01 EDT 2021


https://github.com/python/cpython/commit/d72e8d487553c103bf2742e229f8266b515fd951
commit: d72e8d487553c103bf2742e229f8266b515fd951
branch: master
author: Victor Stinner <vstinner at python.org>
committer: vstinner <vstinner at python.org>
date: 2021-03-23T17:42:51+01:00
summary:

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.

files:
M Lib/shutil.py
M Lib/subprocess.py

diff --git a/Lib/shutil.py b/Lib/shutil.py
index 89d924dec8aa4..e29fe4d83e927 100644
--- a/Lib/shutil.py
+++ b/Lib/shutil.py
@@ -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':
@@ -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:
@@ -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:
diff --git a/Lib/subprocess.py b/Lib/subprocess.py
index d375514b2dd0a..4b011e4ce5579 100644
--- a/Lib/subprocess.py
+++ b/Lib/subprocess.py
@@ -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:
@@ -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")
 
@@ -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")
@@ -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



More information about the Python-checkins mailing list