]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[3.13] gh-132185: Speed up expanduser() test with large password database (GH-132231...
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Sat, 12 Apr 2025 16:43:28 +0000 (18:43 +0200)
committerGitHub <noreply@github.com>
Sat, 12 Apr 2025 16:43:28 +0000 (16:43 +0000)
Use only a limited number of randomly selected entries.
(cherry picked from commit 842ab815177549b9d4bec576d8f2c8f240b63506)

Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
Lib/test/test_posixpath.py

index 43e4fbc610e5f72f0fc24eafe86f7e8967aec5f7..fa19d549c26cfad240ef9484e11d453cb31439cf 100644 (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))