summaryrefslogtreecommitdiffstats
path: root/Lib/posixpath.py
diff options
context:
space:
mode:
authorChristian Heimes <christian@python.org>2022-03-07 12:36:47 (GMT)
committerGitHub <noreply@github.com>2022-03-07 12:36:47 (GMT)
commitca9689f8dac01d27e041e1dbbdae146746d48ab3 (patch)
treec6dcb89cc77ea1faf55abf4d664349669808eb31 /Lib/posixpath.py
parent3b3be05a164da43f201e35b6dafbc840993a4d18 (diff)
downloadcpython-ca9689f8dac01d27e041e1dbbdae146746d48ab3.zip
cpython-ca9689f8dac01d27e041e1dbbdae146746d48ab3.tar.gz
cpython-ca9689f8dac01d27e041e1dbbdae146746d48ab3.tar.bz2
bpo-46933: Make pwd module optional (GH-31700)
Co-authored-by: Erlend Egeberg Aasland <erlend.aasland@innova.no>
Diffstat (limited to 'Lib/posixpath.py')
-rw-r--r--Lib/posixpath.py12
1 files changed, 10 insertions, 2 deletions
diff --git a/Lib/posixpath.py b/Lib/posixpath.py
index a46c667..a7b2f2d 100644
--- a/Lib/posixpath.py
+++ b/Lib/posixpath.py
@@ -241,7 +241,11 @@ def expanduser(path):
i = len(path)
if i == 1:
if 'HOME' not in os.environ:
- import pwd
+ try:
+ import pwd
+ except ImportError:
+ # pwd module unavailable, return path unchanged
+ return path
try:
userhome = pwd.getpwuid(os.getuid()).pw_dir
except KeyError:
@@ -251,7 +255,11 @@ def expanduser(path):
else:
userhome = os.environ['HOME']
else:
- import pwd
+ try:
+ import pwd
+ except ImportError:
+ # pwd module unavailable, return path unchanged
+ return path
name = path[1:i]
if isinstance(name, bytes):
name = str(name, 'ASCII')