Skip to content

Commit 6123d16

Browse files
gh-132185: Speed up expanduser() test with large password database
Use only random selected entries if the "cpu" resource is not enabled.
1 parent 895d983 commit 6123d16

File tree

1 file changed

+15
-7
lines changed

1 file changed

+15
-7
lines changed

Lib/test/test_posixpath.py

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
import inspect
22
import os
33
import posixpath
4+
import random
45
import sys
56
import unittest
67
from posixpath import realpath, abspath, dirname, basename
8+
from test import support
79
from test import test_genericpath
8-
from test.support import get_attribute, import_helper
9-
from test.support import cpython_only, os_helper
10+
from test.support import import_helper
11+
from test.support import os_helper
1012
from test.support.os_helper import FakePath
1113
from unittest import mock
1214

@@ -285,7 +287,7 @@ def test_isjunction(self):
285287
self.assertFalse(posixpath.isjunction(ABSTFN))
286288

287289
@unittest.skipIf(sys.platform == 'win32', "Fast paths are not for win32")
288-
@cpython_only
290+
@support.cpython_only
289291
def test_fast_paths_in_use(self):
290292
# There are fast paths of these functions implemented in posixmodule.c.
291293
# Confirm that they are being used, and not the Python fallbacks
@@ -359,16 +361,22 @@ def test_expanduser_pwd(self):
359361
"no home directory on VxWorks")
360362
def test_expanduser_pwd2(self):
361363
pwd = import_helper.import_module('pwd')
362-
for all_entry in get_attribute(pwd, 'getpwall')():
363-
name = all_entry.pw_name
364-
364+
getpwall = support.get_attribute(pwd, 'getpwall')
365+
names = [entry.pw_name for entry in getpwall()]
366+
if not support.is_resource_enabled('cpu') and len(names) > 100:
367+
# Select random names, half of them with non-ASCII name,
368+
# if evailable.
369+
random.shuffle(names)
370+
names.sort(key=lambda name: name.isascii())
371+
del names[50:-50]
372+
for name in names:
365373
# gh-121200: pw_dir can be different between getpwall() and
366374
# getpwnam(), so use getpwnam() pw_dir as expanduser() does.
367375
entry = pwd.getpwnam(name)
368376
home = entry.pw_dir
369377
home = home.rstrip('/') or '/'
370378

371-
with self.subTest(all_entry=all_entry, entry=entry):
379+
with self.subTest(name=name, pw_dir=entry.pw_dir):
372380
self.assertEqual(posixpath.expanduser('~' + name), home)
373381
self.assertEqual(posixpath.expanduser(os.fsencode('~' + name)),
374382
os.fsencode(home))

0 commit comments

Comments
 (0)