@@ -1477,6 +1477,17 @@ def test_setgroups(self):
1477
1477
1478
1478
@unittest .skipUnless (hasattr (os , 'posix_spawn' ), "test needs os.posix_spawn" )
1479
1479
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
+
1480
1491
def test_returns_pid (self ):
1481
1492
pidfile = support .TESTFN
1482
1493
self .addCleanup (support .unlink , pidfile )
@@ -1485,8 +1496,8 @@ def test_returns_pid(self):
1485
1496
with open({ pidfile !r} , "w") as pidfile:
1486
1497
pidfile.write(str(os.getpid()))
1487
1498
"""
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 ,
1490
1501
os .environ )
1491
1502
self .assertEqual (os .waitpid (pid , 0 ), (pid , 0 ))
1492
1503
with open (pidfile ) as f :
@@ -1513,17 +1524,17 @@ def test_specify_environment(self):
1513
1524
with open(<
10000
span class=pl-kos>{ envfile !r} , "w") as envfile:
1514
1525
envfile.write(os.environ['foo'])
1515
1526
"""
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 ,
1518
1529
{** os .environ , 'foo' : 'bar' })
1519
1530
self .assertEqual (os .waitpid (pid , 0 ), (pid , 0 ))
1520
1531
with open (envfile ) as f :
1521
1532
self .assertEqual (f .read (), 'bar' )
1522
1533
1523
1534
def test_empty_file_actions (self ):
1524
1535
pid = posix .posix_spawn (
1525
- sys . executable ,
1526
- [ sys . executable , '-c' , 'pass' ] ,
1536
+ self . NOOP_PROGRAM [ 0 ] ,
1537
+ self . NOOP_PROGRAM ,
1527
1538
os .environ ,
1528
1539
[]
1529
1540
)
@@ -1535,43 +1546,36 @@ def test_multiple_file_actions(self):
1535
1546
(os .POSIX_SPAWN_CLOSE , 0 ),
1536
1547
(os .POSIX_SPAWN_DUP2 , 1 , 4 ),
1537
1548
]
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 ,
1540
1551
os .environ , file_actions )
1541
1552
self .assertEqual (os .waitpid (pid , 0 ), (pid , 0 ))
1542
1553
1543
1554
def test_bad_file_actions (self ):
1555
+ args = self .NOOP_PROGRAM
1544
1556
with self .assertRaises (TypeError ):
1545
- posix .posix_spawn (sys .executable ,
1546
- [sys .executable , "-c" , "pass" ],
1557
+ posix .posix_spawn (args [0 ], args ,
1547
1558
os .environ , [None ])
1548
1559
with self .assertRaises (TypeError ):
1549
- posix .posix_spawn (sys .executable ,
1550
- [sys .executable , "-c" , "pass" ],
1560
+ posix .posix_spawn (args [0 ], args ,
1551
1561
os .environ , [()])
1552
1562
with self .assertRaises (TypeError ):
1553
- posix .posix_spawn (sys .executable ,
1554
- [sys .executable , "-c" , "pass" ],
1563
+ posix .posix_spawn (args [0 ], args ,
1555
1564
os .environ , [(None ,)])
1556
1565
with self .assertRaises (TypeError ):
1557
- posix .posix_spawn (sys .executable ,
1558
- [sys .executable , "-c" , "pass" ],
1566
+ posix .posix_spawn (args [0 ], args ,
1559
1567
os .environ , [(12345 ,)])
1560
1568
with self .assertRaises (TypeError ):
1561
- posix .posix_spawn (sys .executable ,
1562
- [sys .executable , "-c" , "pass" ],
1569
+ posix .posix_spawn (args [0 ], args ,
1563
1570
os .environ , [(os .POSIX_SPAWN_CLOSE ,)])
1564
1571
with self .assertRaises (TypeError ):
1565
- posix .posix_spawn (sys .executable ,
1566
- [sys .executable , "-c" , "pass" ],
1572
+ posix .posix_spawn (args [0 ], args ,
1567
1573
os .environ , [(os .POSIX_SPAWN_CLOSE , 1 , 2 )])
1568
1574
with self .assertRaises (TypeError ):
1569
- posix .posix_spawn (sys .executable ,
1570
- [sys .executable , "-c" , "pass" ],
1575
+ posix .posix_spawn (args [0 ], args ,
1571
1576
os .environ , [(os .POSIX_SPAWN_CLOSE , None )])
1572
1577
with self .assertRaises (ValueError ):
1573
- posix .posix_spawn (sys .executable ,
1574
- [sys .executable , "-c" , "pass" ],
1578
+ posix .posix_spawn (args [0 ], args ,
1575
1579
os .environ ,
1576
1580
[(os .POSIX_SPAWN_OPEN , 3 , __file__ + '\0 ' ,
1577
1581
os .O_RDONLY , 0 )])
@@ -1588,8 +1592,8 @@ def test_open_file(self):
1588
1592
os .O_WRONLY | os .O_CREAT | os .O_TRUNC ,
1589
1593
stat .S_IRUSR | stat .S_IWUSR ),
1590
1594
]
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 ,
1593
1597
os .environ , file_actions )
1594
1598
self .assertEqual (os .waitpid (pid , 0 ), (pid , 0 ))
1595
1599
with open (outfile ) as f :
@@ -1606,8 +1610,8 @@ def test_close_file(self):
1606
1610
with open({ closefile !r} , 'w') as closefile:
1607
1611
closefile.write('is closed %d' % e.errno)
1608
1612
"""
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 ,
1611
1615
os .environ ,
1612
1616
[(os .POSIX_SPAWN_CLOSE , 0 ),])
1613
1617
self .assertEqual (os .waitpid (pid , 0 ), (pid , 0 ))
@@ -1625,8 +1629,8 @@ def test_dup2(self):
1625
1629
file_actions = [
1626
1630
(os .POSIX_SPAWN_DUP2 , childfile .fileno (), 1 ),
1627
1631
]
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 ,
1630
1634
os .environ , file_actions )
1631
1635
self .assertEqual (os .waitpid (pid , 0 ), (pid , 0 ))
1632
1636
with open (dupfile ) as f :
0 commit comments