bpo-41718: subprocess imports grp and pwd on demand (GH-24987) · python/cpython@d72e8d4

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