https://github.com/python/cpython/commit/842ab815177549b9d4bec576d8f2c8f240b63506
commit: 842ab815177549b9d4bec576d8f2c8f240b63506
branch: main
author: Serhiy Storchaka <[email protected]>
committer: serhiy-storchaka <[email protected]>
date: 2025-04-12T16:17:52Z
summary:
gh-132185: Speed up expanduser() test with large password database (GH-132231)
Use only a limited number of randomly selected entries.
files:
M Lib/test/test_posixpath.py
diff --git a/Lib/test/test_posixpath.py b/Lib/test/test_posixpath.py
index 43e4fbc610e5f7..fa19d549c26cfa 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,23 @@ 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()]
+ 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))
_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-checkins.python.org/
Member address: [email protected]