Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bpo-46933: Make pwd module optional (GH-31700) #31700

Merged
merged 3 commits into from
Mar 7, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ Mac/pythonw
Misc/python.pc
Misc/python-embed.pc
Misc/python-config.sh
Modules/Setup.bootstrap
Modules/Setup.config
Modules/Setup.local
Modules/Setup.stdlib
Expand Down
5 changes: 4 additions & 1 deletion Lib/distutils/tests/test_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,10 @@ def test_check_environ_getpwuid(self):
util._environ_checked = 0
os.environ.pop('HOME', None)

import pwd
try:
import pwd
except ImportError:
raise unittest.SkipTest("Test requires pwd module.")

# only set pw_dir field, other fields are not used
result = pwd.struct_passwd((None, None, None, None, None,
Expand Down
12 changes: 10 additions & 2 deletions Lib/posixpath.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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')
Expand Down
3 changes: 3 additions & 0 deletions Lib/test/test_pathlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -1486,6 +1486,9 @@ def _test_home(self, p):
self.assertIs(type(p), type(q))
self.assertTrue(p.is_absolute())

@unittest.skipIf(
pwd is None, reason="Test requires pwd module to get homedir."
)
def test_home(self):
with os_helper.EnvironmentVarGuard() as env:
self._test_home(self.cls.home())
Expand Down
7 changes: 6 additions & 1 deletion Lib/test/test_posix.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,18 @@
import time
import os
import platform
import pwd
import stat
import tempfile
import unittest
import warnings
import textwrap
from contextlib import contextmanager

try:
import pwd
except ImportError:
pwd = None

_DUMMY_SYMLINK = os.path.join(tempfile.gettempdir(),
os_helper.TESTFN + '-dummy-symlink')

Expand Down Expand Up @@ -126,6 +130,7 @@ def test_setresgid_exception(self):

@unittest.skipUnless(hasattr(posix, 'initgroups'),
"test needs os.initgroups()")
@unittest.skipUnless(hasattr(pwd, 'getpwuid'), "test needs pwd.getpwuid()")
def test_initgroups(self):
# It takes a string and an integer; check that it raises a TypeError
# for other argument lists.
Expand Down
14 changes: 9 additions & 5 deletions Makefile.pre.in
Original file line number Diff line number Diff line change
Expand Up @@ -917,6 +917,9 @@ Modules/Setup.local:
@# Create empty Setup.local when file was deleted by user
echo "# Edit this file for local setup changes" > $@

Modules/Setup.bootstrap: $(srcdir)/Modules/Setup.bootstrap.in config.status
./config.status $@

Modules/Setup.stdlib: $(srcdir)/Modules/Setup.stdlib.in config.status
./config.status $@

Expand All @@ -925,13 +928,13 @@ Makefile Modules/config.c: Makefile.pre \
$(MAKESETUP) \
$(srcdir)/Modules/Setup \
Modules/Setup.local \
$(srcdir)/Modules/Setup.bootstrap \
Modules/Setup.bootstrap \
Modules/Setup.stdlib
$(SHELL) $(MAKESETUP) -c $(srcdir)/Modules/config.c.in \
-s Modules \
Modules/Setup.local \
@MODULES_SETUP_STDLIB@ \
$(srcdir)/Modules/Setup.bootstrap \
Modules/Setup.bootstrap \
$(srcdir)/Modules/Setup
@mv config.c Modules
@echo "The Makefile was updated, you may need to re-run make."
Expand Down Expand Up @@ -2146,7 +2149,7 @@ libainstall: @DEF_MAKE_RULE@ python-config
$(INSTALL_DATA) $(srcdir)/Modules/config.c.in $(DESTDIR)$(LIBPL)/config.c.in
$(INSTALL_DATA) Makefile $(DESTDIR)$(LIBPL)/Makefile
$(INSTALL_DATA) $(srcdir)/Modules/Setup $(DESTDIR)$(LIBPL)/Setup
$(INSTALL_DATA) $(srcdir)/Modules/Setup.bootstrap $(DESTDIR)$(LIBPL)/Setup.bootstrap
$(INSTALL_DATA) Modules/Setup.bootstrap $(DESTDIR)$(LIBPL)/Setup.bootstrap
$(INSTALL_DATA) Modules/Setup.stdlib $(DESTDIR)$(LIBPL)/Setup.stdlib
$(INSTALL_DATA) Modules/Setup.local $(DESTDIR)$(LIBPL)/Setup.local
$(INSTALL_DATA) Misc/python.pc $(DESTDIR)$(LIBPC)/python-$(VERSION).pc
Expand Down Expand Up @@ -2381,8 +2384,9 @@ distclean: clobber
for file in $(srcdir)/Lib/test/data/* ; do \
if test "$$file" != "$(srcdir)/Lib/test/data/README"; then rm "$$file"; fi; \
done
-rm -f core Makefile Makefile.pre config.status Modules/Setup.local \
Modules/Setup.stdlib Modules/ld_so_aix Modules/python.exp Misc/python.pc \
-rm -f core Makefile Makefile.pre config.status Modules/Setup.local
Modules/Setup.bootstrap Modules/Setup.stdlib \
Modules/ld_so_aix Modules/python.exp Misc/python.pc \
Misc/python-embed.pc Misc/python-config.sh
-rm -f python*-gdb.py
# Issue #28258: set LC_ALL to avoid issues with Estonian locale.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
The :mod:`pwd` module is now optional. :func:`os.path.expanduser` returns the path when the :mod:`pwd` module is not available.
2 changes: 1 addition & 1 deletion Modules/Setup.bootstrap → Modules/Setup.bootstrap.in
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,4 @@ _stat _stat.c
_symtable symtablemodule.c

# for systems without $HOME env, used by site._getuserbase()
pwd pwdmodule.c
@MODULE_PWD_TRUE@pwd pwdmodule.c
Loading