Skip to content

gh-132185: Speed up expanduser() test with large password database #132231

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

Merged
merged 6 commits into from
Apr 12, 2025
Merged
Changes from 4 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
23 changes: 16 additions & 7 deletions Lib/test/test_posixpath.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import inspect
import os
import posixpath
import random
import sys
import unittest
from posixpath import realpath, abspath, dirname, basename
from test import support
from test import test_genericpath
from test.support import get_attribute, import_helper
from test.support import cpython_only, os_helper
from test.support import import_helper
from test.support import os_helper
from test.support.os_helper import FakePath
from unittest import mock

Expand Down Expand Up @@ -285,7 +287,7 @@ def test_isjunction(self):
self.assertFalse(posixpath.isjunction(ABSTFN))

@unittest.skipIf(sys.platform == 'win32', "Fast paths are not for win32")
@cpython_only
@support.cpython_only
def test_fast_paths_in_use(self):
# There are fast paths of these functions implemented in posixmodule.c.
# Confirm that they are being used, and not the Python fallbacks
Expand Down Expand Up @@ -359,16 +361,23 @@ def test_expanduser_pwd(self):
"no home directory on VxWorks")
def test_expanduser_pwd2(self):
pwd = import_helper.import_module('pwd')
for all_entry in get_attribute(pwd, 'getpwall')():
name = all_entry.pw_name

getpwall = support.get_attribute(pwd, 'getpwall')
names = [entry.pw_name for entry in getpwall()]
maxusers = 2000 if support.is_resource_enabled('cpu') else 100
if len(names) > maxusers:
# Select random names, half of them with non-ASCII name,
# if evailable.
random.shuffle(names)
names.sort(key=lambda name: name.isascii())
del names[maxusers//2:-maxusers//2]
for name in names:
# gh-121200: pw_dir can be different between getpwall() and
# getpwnam(), so use getpwnam() pw_dir as expanduser() does.
entry = pwd.getpwnam(name)
home = entry.pw_dir
home = home.rstrip('/') or '/'

with self.subTest(all_entry=all_entry, entry=entry):
with self.subTest(name=name, pw_dir=entry.pw_dir):
self.assertEqual(posixpath.expanduser('~' + name), home)
self.assertEqual(posixpath.expanduser(os.fsencode('~' + name)),
os.fsencode(home))
Expand Down
Loading