[3.13] gh-132185: Speed up expanduser() test with large password database (GH-132231) (GH-132443)

Use only a limited number of randomly selected entries.
(cherry picked from commit 842ab81517)

Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
This commit is contained in:
Miss Islington (bot) 2025-04-12 18:43:28 +02:00 committed by GitHub
parent 1884a3bbd1
commit f943376ca8
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -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
@ -285,7 +287,7 @@ class PosixPathTest(unittest.TestCase):
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
@ -359,16 +361,23 @@ class PosixPathTest(unittest.TestCase):
"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 = 1000 if support.is_resource_enabled('cpu') else 100
if len(names) > maxusers:
# Select random names, half of them with non-ASCII name,
# if available.
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))