From 6123d167883733c6e9c197f64522c4aa89a8de9a Mon Sep 17 00:00:00 2001 From: Serhiy Storchaka Date: Mon, 7 Apr 2025 18:58:16 +0300 Subject: [PATCH 1/6] gh-132185: Speed up expanduser() test with large password database Use only random selected entries if the "cpu" resource is not enabled. --- Lib/test/test_posixpath.py | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/Lib/test/test_posixpath.py b/Lib/test/test_posixpath.py index 43e4fbc610e5f7..5d88d436f2eb92 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 @@ 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 @@ -359,16 +361,22 @@ 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()] + if not support.is_resource_enabled('cpu') and len(names) > 100: + # Select random names, half of them with non-ASCII name, + # if evailable. + random.shuffle(names) + names.sort(key=lambda name: name.isascii()) + del names[50:-50] + 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)) From d964e876c7b9d121ccfddf49fc70bc45033f6e83 Mon Sep 17 00:00:00 2001 From: Serhiy Storchaka Date: Mon, 7 Apr 2025 19:24:36 +0300 Subject: [PATCH 2/6] Limit the number even if the "cpu" resource is enabled. --- Lib/test/test_posixpath.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Lib/test/test_posixpath.py b/Lib/test/test_posixpath.py index 5d88d436f2eb92..5abd75a8427c6b 100644 --- a/Lib/test/test_posixpath.py +++ b/Lib/test/test_posixpath.py @@ -363,7 +363,8 @@ def test_expanduser_pwd2(self): pwd = import_helper.import_module('pwd') getpwall = support.get_attribute(pwd, 'getpwall') names = [entry.pw_name for entry in getpwall()] - if not support.is_resource_enabled('cpu') and len(names) > 100: + maxusers = 10000 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) From bea1adad12f8f9aed6f2811c1a3d64cb116eb491 Mon Sep 17 00:00:00 2001 From: Serhiy Storchaka Date: Mon, 7 Apr 2025 19:49:02 +0300 Subject: [PATCH 3/6] Limit the maximal number even more. Fix a slice. --- Lib/test/test_posixpath.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/Lib/test/test_posixpath.py b/Lib/test/test_posixpath.py index 5abd75a8427c6b..776a5a2c33721f 100644 --- a/Lib/test/test_posixpath.py +++ b/Lib/test/test_posixpath.py @@ -363,13 +363,14 @@ def test_expanduser_pwd2(self): pwd = import_helper.import_module('pwd') getpwall = support.get_attribute(pwd, 'getpwall') names = [entry.pw_name for entry in getpwall()] - maxusers = 10000 if support.is_resource_enabled('cpu') else 100 + names = [(n+'x')[:-1] for n in names for i in range(1000)] + 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[50:-50] + 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. From 7a19eb10893b288526ab4fcb2bc7e0d8624a6e3e Mon Sep 17 00:00:00 2001 From: Serhiy Storchaka Date: Mon, 7 Apr 2025 20:19:08 +0300 Subject: [PATCH 4/6] Remove debug code. --- Lib/test/test_posixpath.py | 1 - 1 file changed, 1 deletion(-) diff --git a/Lib/test/test_posixpath.py b/Lib/test/test_posixpath.py index 776a5a2c33721f..03431658b52ff7 100644 --- a/Lib/test/test_posixpath.py +++ b/Lib/test/test_posixpath.py @@ -363,7 +363,6 @@ def test_expanduser_pwd2(self): pwd = import_helper.import_module('pwd') getpwall = support.get_attribute(pwd, 'getpwall') names = [entry.pw_name for entry in getpwall()] - names = [(n+'x')[:-1] for n in names for i in range(1000)] maxusers = 2000 if support.is_resource_enabled('cpu') else 100 if len(names) > maxusers: # Select random names, half of them with non-ASCII name, From a4f6f6017a98021a4de627003644292e843443e6 Mon Sep 17 00:00:00 2001 From: Serhiy Storchaka Date: Mon, 7 Apr 2025 20:56:09 +0300 Subject: [PATCH 5/6] Update Lib/test/test_posixpath.py MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com> --- Lib/test/test_posixpath.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/test/test_posixpath.py b/Lib/test/test_posixpath.py index 03431658b52ff7..87ba766df6358c 100644 --- a/Lib/test/test_posixpath.py +++ b/Lib/test/test_posixpath.py @@ -366,7 +366,7 @@ def test_expanduser_pwd2(self): 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. + # if available. random.shuffle(names) names.sort(key=lambda name: name.isascii()) del names[maxusers//2:-maxusers//2] From 747dd8670192de62bf10bdc99ec19a250557cafc Mon Sep 17 00:00:00 2001 From: Serhiy Storchaka Date: Sat, 12 Apr 2025 18:48:22 +0300 Subject: [PATCH 6/6] Limit the worst time to 5 secs. --- Lib/test/test_posixpath.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/test/test_posixpath.py b/Lib/test/test_posixpath.py index 87ba766df6358c..fa19d549c26cfa 100644 --- a/Lib/test/test_posixpath.py +++ b/Lib/test/test_posixpath.py @@ -363,7 +363,7 @@ def test_expanduser_pwd2(self): pwd = import_helper.import_module('pwd') getpwall = support.get_attribute(pwd, 'getpwall') names = [entry.pw_name for entry in getpwall()] - maxusers = 2000 if support.is_resource_enabled('cpu') else 100 + 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.