Skip to content
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
unnest posixpath.expanduser
  • Loading branch information
nineteendo committed Mar 28, 2024
commit ca90fde42531efb86ab035ecafd0c0d54ff8cef0
34 changes: 17 additions & 17 deletions Lib/posixpath.py
Original file line number Diff line number Diff line change
Expand Up @@ -255,37 +255,37 @@ def expanduser(path):
i = path.find(sep, 1)
if i < 0:
i = len(path)
if i == 1:
if 'HOME' not in os.environ:
try:
import pwd
except ImportError:
# pwd module unavailable, return path unchanged
return path
try:
userhome = pwd.getpwuid(os.getuid()).pw_dir
except KeyError:
# bpo-10496: if the current user identifier doesn't exist in the
# password database, return the path unchanged
return path
else:
userhome = os.environ['HOME']
else:
if i != 1:
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')
name = name.decode('ascii')
try:
pwent = pwd.getpwnam(name)
except KeyError:
# bpo-10496: if the user name from the path doesn't exist in the
# password database, return the path unchanged
return path
userhome = pwent.pw_dir
elif 'HOME' in os.environ:
userhome = os.environ['HOME']
else:
try:
import pwd
except ImportError:
# pwd module unavailable, return path unchanged
return path
try:
userhome = pwd.getpwuid(os.getuid()).pw_dir
except KeyError:
# bpo-10496: if the current user identifier doesn't exist in the
# password database, return the path unchanged
return path

# if no user home, return the path unchanged on VxWorks
if userhome is None and sys.platform == "vxworks":
return path
Expand Down