From: Miss Islington (bot) <31488909+miss-islington@users.noreply.github.com> Date: Sat, 12 Apr 2025 16:43:28 +0000 (+0200) Subject: [3.13] gh-132185: Speed up expanduser() test with large password database (GH-132231... X-Git-Tag: v3.13.4~289 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=f943376ca801392925f1bbe23f2d0ffb22df3ffe;p=thirdparty%2FPython%2Fcpython.git [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 842ab815177549b9d4bec576d8f2c8f240b63506) Co-authored-by: Serhiy Storchaka --- diff --git a/Lib/test/test_posixpath.py b/Lib/test/test_posixpath.py index 43e4fbc610e5..fa19d549c26c 100644 --- a/Lib/test/test_posixpath.py +++ b/Lib/test/test_posixpath.py @@ -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))