8000 Fix TestPosixSpawn.test_close_file() (GH-8992) · python/cpython@0382406 · GitHub
[go: up one dir, main page]

Skip to content

Commit 0382406

Browse files
authored
Fix TestPosixSpawn.test_close_file() (GH-8992)
Modify TestPosixSpawn to run Python using -I and -S options. Disable site module to avoid side effects. For example, on Fedora 28, if the HOME environment variable is not set, site._getuserbase() calls pwd.getpwuid() which opens /var/lib/sss/mc/passwd, but then leaves the file open which makes test_close_file() to fail.
1 parent fbca908 commit 0382406

File tree

1 file changed

+34
-30
lines changed

1 file changed

+34
-30
lines changed

Lib/test/test_posix.py

Lines changed: 34 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1477,6 +1477,17 @@ def test_setgroups(self):
14771477

14781478
@unittest.skipUnless(hasattr(os, 'posix_spawn'), "test needs os.posix_spawn")
14791479
class TestPosixSpawn(unittest.TestCase):
1480+
# Program which does nothing and exit with status 0 (success)
1481+
NOOP_PROGRAM = (sys.executable, '-I', '-S', '-c', 'pass')
1482+
1483+
def python_args(self, *args):
1484+
# Disable site module to avoid side effects. For example,
1485+
# on Fedora 28, if the HOME environment variable is not set,
1486+
# site._getuserbase() calls pwd.getpwuid() which opens
1487+
# /var/lib/sss/mc/passwd but then leaves the file open which makes
1488+
# test_close_file() to fail.
1489+
return (sys.executable, '-I', '-S', *args)
1490+
14801491
def test_returns_pid(self):
14811492
pidfile = support.TESTFN
14821493
self.addCleanup(support.unlink, pidfile)
@@ -1485,8 +1496,8 @@ def test_returns_pid(self):
14851496
with open({pidfile!r}, "w") as pidfile:
14861497
pidfile.write(str(os.getpid()))
14871498
"""
1488-
pid = posix.posix_spawn(sys.executable,
1489-
[sys.executable, '-c', script],
1499+
args = self.python_args('-c', script)
1500+
pid = posix.posix_spawn(args[0], args,
14901501
os.environ)
14911502
self.assertEqual(os.waitpid(pid, 0), (pid, 0))
14921503
with open(pidfile) as f:
@@ -1513,17 +1524,17 @@ def test_specify_environment(self):
15131524
with open(< 10000 span class=pl-kos>{envfile!r}, "w") as envfile:
15141525
envfile.write(os.environ['foo'])
15151526
"""
1516-
pid = posix.posix_spawn(sys.executable,
1517-
[sys.executable, '-c', script],
1527+
args = self.python_args('-c', script)
1528+
pid = posix.posix_spawn(args[0], args,
15181529
{**os.environ, 'foo': 'bar'})
15191530
self.assertEqual(os.waitpid(pid, 0), (pid, 0))
15201531
with open(envfile) as f:
15211532
self.assertEqual(f.read(), 'bar')
15221533

15231534
def test_empty_file_actions(self):
15241535
pid = posix.posix_spawn(
1525-
sys.executable,
1526-
[sys.executable, '-c', 'pass'],
1536+
self.NOOP_PROGRAM[0],
1537+
self.NOOP_PROGRAM,
15271538
os.environ,
15281539
[]
15291540
)
@@ -1535,43 +1546,36 @@ def test_multiple_file_actions(self):
15351546
(os.POSIX_SPAWN_CLOSE, 0),
15361547
(os.POSIX_SPAWN_DUP2, 1, 4),
15371548
]
1538-
pid = posix.posix_spawn(sys.executable,
1539-
[sys.executable, "-c", "pass"],
1549+
pid = posix.posix_spawn(self.NOOP_PROGRAM[0],
1550+
self.NOOP_PROGRAM,
15401551
os.environ, file_actions)
15411552
self.assertEqual(os.waitpid(pid, 0), (pid, 0))
15421553

15431554
def test_bad_file_actions(self):
1555+
args = self.NOOP_PROGRAM
15441556
with self.assertRaises(TypeError):
1545-
posix.posix_spawn(sys.executable,
1546-
[sys.executable, "-c", "pass"],
1557+
posix.posix_spawn(args[0], args,
15471558
os.environ, [None])
15481559
with self.assertRaises(TypeError):
1549-
posix.posix_spawn(sys.executable,
1550-
[sys.executable, "-c", "pass"],
1560+
posix.posix_spawn(args[0], args,
15511561
os.environ, [()])
15521562
with self.assertRaises(TypeError):
1553-
posix.posix_spawn(sys.executable,
1554-
[sys.executable, "-c", "pass"],
1563+
posix.posix_spawn(args[0], args,
15551564
os.environ, [(None,)])
15561565
with self.assertRaises(TypeError):
1557-
posix.posix_spawn(sys.executable,
1558-
[sys.executable, "-c", "pass"],
1566+
posix.posix_spawn(args[0], args,
15591567
os.environ, [(12345,)])
15601568
with self.assertRaises(TypeError):
1561-
posix.posix_spawn(sys.executable,
1562-
[sys.executable, "-c", "pass"],
1569+
posix.posix_spawn(args[0], args,
15631570
os.environ, [(os.POSIX_SPAWN_CLOSE,)])
15641571
with self.assertRaises(TypeError):
1565-
posix.posix_spawn(sys.executable,
1566-
[sys.executable, "-c", "pass"],
1572+
posix.posix_spawn(args[0], args,
15671573
os.environ, [(os.POSIX_SPAWN_CLOSE, 1, 2)])
15681574
with self.assertRaises(TypeError):
1569-
posix.posix_spawn(sys.executable,
1570-
[sys.executable, "-c", "pass"],
1575+
posix.posix_spawn(args[0], args,
15711576
os.environ, [(os.POSIX_SPAWN_CLOSE, None)])
15721577
with self.assertRaises(ValueError):
1573-
posix.posix_spawn(sys.executable,
1574-
[sys.executable, "-c", "pass"],
1578+
posix.posix_spawn(args[0], args,
15751579
os.environ,
15761580
[(os.POSIX_SPAWN_OPEN, 3, __file__ + '\0',
15771581
os.O_RDONLY, 0)])
@@ -1588,8 +1592,8 @@ def test_open_file(self):
15881592
os.O_WRONLY | os.O_CREAT | os.O_TRUNC,
15891593
stat.S_IRUSR | stat.S_IWUSR),
15901594
]
1591-
pid = posix.posix_spawn(sys.executable,
1592-
[sys.executable, '-c', script],
1595+
args = self.python_args('-c', script)
1596+
pid = posix.posix_spawn(args[0], args,
15931597
os.environ, file_actions)
15941598
self.assertEqual(os.waitpid(pid, 0), (pid, 0))
15951599
with open(outfile) as f:
@@ -1606,8 +1610,8 @@ def test_close_file(self):
16061610
with open({closefile!r}, 'w') as closefile:
16071611
closefile.write('is closed %d' % e.errno)
16081612
"""
1609-
pid = posix.posix_spawn(sys.executable,
1610-
[sys.executable, '-c', script],
1613+
args = self.python_args('-c', script)
1614+
pid = posix.posix_spawn(args[0], args,
16111615
os.environ,
16121616
[(os.POSIX_SPAWN_CLOSE, 0),])
16131617
self.assertEqual(os.waitpid(pid, 0), (pid, 0))
@@ -1625,8 +1629,8 @@ def test_dup2(self):
16251629
file_actions = [
16261630
(os.POSIX_SPAWN_DUP2, childfile.fileno(), 1),
16271631
]
1628-
pid = posix.posix_spawn(sys.executable,
1629-
[sys.executable, '-c', script],
1632+
args = self.python_args('-c', script)
1633+
pid = posix.posix_spawn(args[0], args,
16301634
os.environ, file_actions)
16311635
self.assertEqual(os.waitpid(pid, 0), (pid, 0))
16321636
with open(dupfile) as f:

0 commit comments

Comments
 (0)
0