|  | 
| 1 | 1 | import inspect | 
| 2 | 2 | import os | 
| 3 | 3 | import posixpath | 
|  | 4 | +import random | 
| 4 | 5 | import sys | 
| 5 | 6 | import unittest | 
| 6 | 7 | from posixpath import realpath, abspath, dirname, basename | 
|  | 8 | +from test import support | 
| 7 | 9 | 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 | 
| 10 | 12 | from test.support.os_helper import FakePath | 
| 11 | 13 | from unittest import mock | 
| 12 | 14 | 
 | 
| @@ -285,7 +287,7 @@ def test_isjunction(self): | 
| 285 | 287 |         self.assertFalse(posixpath.isjunction(ABSTFN)) | 
| 286 | 288 | 
 | 
| 287 | 289 |     @unittest.skipIf(sys.platform == 'win32', "Fast paths are not for win32") | 
| 288 |  | -    @cpython_only | 
|  | 290 | +    @support.cpython_only | 
| 289 | 291 |     def test_fast_paths_in_use(self): | 
| 290 | 292 |         # There are fast paths of these functions implemented in posixmodule.c. | 
| 291 | 293 |         # Confirm that they are being used, and not the Python fallbacks | 
| @@ -359,16 +361,23 @@ def test_expanduser_pwd(self): | 
| 359 | 361 |                      "no home directory on VxWorks") | 
| 360 | 362 |     def test_expanduser_pwd2(self): | 
| 361 | 363 |         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 | +        maxusers = 1000 if support.is_resource_enabled('cpu') else 100 | 
|  | 367 | +        if len(names) > maxusers: | 
|  | 368 | +            # Select random names, half of them with non-ASCII name, | 
|  | 369 | +            # if available. | 
|  | 370 | +            random.shuffle(names) | 
|  | 371 | +            names.sort(key=lambda name: name.isascii()) | 
|  | 372 | +            del names[maxusers//2:-maxusers//2] | 
|  | 373 | +        for name in names: | 
| 365 | 374 |             # gh-121200: pw_dir can be different between getpwall() and | 
| 366 | 375 |             # getpwnam(), so use getpwnam() pw_dir as expanduser() does. | 
| 367 | 376 |             entry = pwd.getpwnam(name) | 
| 368 | 377 |             home = entry.pw_dir | 
| 369 | 378 |             home = home.rstrip('/') or '/' | 
| 370 | 379 | 
 | 
| 371 |  | -            with self.subTest(all_entry=all_entry, entry=entry): | 
|  | 380 | +            with self.subTest(name=name, pw_dir=entry.pw_dir): | 
| 372 | 381 |                 self.assertEqual(posixpath.expanduser('~' + name), home) | 
| 373 | 382 |                 self.assertEqual(posixpath.expanduser(os.fsencode('~' + name)), | 
| 374 | 383 |                                  os.fsencode(home)) | 
|  | 
0 commit comments