|
8 | 8 |
|
9 | 9 | import errno
|
10 | 10 | import sys
|
| 11 | +import signal |
11 | 12 | import time
|
12 | 13 | import os
|
13 | 14 | import platform
|
|
16 | 17 | import tempfile
|
17 | 18 | import unittest
|
18 | 19 | import warnings
|
| 20 | +import textwrap |
19 | 21 |
|
20 | 22 | _DUMMY_SYMLINK = os.path.join(tempfile.gettempdir(),
|
21 | 23 | support.TESTFN + '-dummy-symlink')
|
@@ -1540,6 +1542,147 @@ def test_empty_file_actions(self):
|
1540 | 1542 | )
|
1541 | 1543 | self.assertEqual(os.waitpid(pid, 0), (pid, 0))
|
1542 | 1544 |
|
| 1545 | + def test_resetids_explicit_default(self): |
| 1546 | + pid = posix.posix_spawn( |
| 1547 | + sys.executable, |
| 1548 | + [sys.executable, '-c', 'pass'], |
| 1549 | + os.environ, |
| 1550 | + resetids=False |
| 1551 | + ) |
| 1552 | + self.assertEqual(os.waitpid(pid, 0), (pid, 0)) |
| 1553 | + |
| 1554 | + def test_resetids(self): |
| 1555 | + pid = posix.posix_spawn( |
| 1556 | + sys.executable, |
| 1557 | + [sys.executable, '-c', 'pass'], |
| 1558 | + os.environ, |
| 1559 | + resetids=True |
| 1560 | + ) |
| 1561 | + self.assertEqual(os.waitpid(pid, 0), (pid, 0)) |
| 1562 | + |
| 1563 | + def test_resetids_wrong_type(self): |
| 1564 | + with self.assertRaises(TypeError): |
| 1565 | + posix.posix_spawn(sys.executable, |
| 1566 | + [sys.executable, "-c", "pass"], |
| 1567 | + os.environ, resetids=None) |
| 1568 | + |
| 1569 | + def test_setpgroup(self): |
| 1570 | + pid = posix.posix_spawn( |
| 1571 | + sys.executable, |
| 1572 | + [sys.executable, '-c', 'pass'], |
| 1573 | + os.environ, |
| 1574 | + setpgroup=os.getpgrp() |
| 1575 | + ) |
| 1576 | + self.assertEqual(os.waitpid(pid, 0), (pid, 0)) |
| 1577 | + |
| 1578 | + def test_setpgroup_wrong_type(self): |
| 1579 | + with self.assertRaises(TypeError): |
| 1580 | + posix.posix_spawn(sys.executable, |
| 1581 | + [sys.executable, "-c", "pass"], |
| 1582 | + os.environ, setpgroup="023") |
| 1583 | + |
| 1584 | + @unittest.skipUnless(hasattr(signal, 'pthread_sigmask'), |
| 1585 | + 'need signal.pthread_sigmask()') |
| 1586 | + def test_setsigmask(self): |
| 1587 | + code = textwrap.dedent("""\ |
| 1588 | + import _testcapi, signal |
| 1589 | + _testcapi.raise_signal(signal.SIGUSR1)""") |
| 1590 | + |
| 1591 | + pid = posix.posix_spawn( |
| 1592 | + sys.executable, |
| 1593 | + [sys.executable, '-c', code], |
| 1594 | + os.environ, |
| 1595 | + setsigmask=[signal.SIGUSR1] |
| 1596 | + ) |
| 1597 | + self.assertEqual(os.waitpid(pid, 0), (pid, 0)) |
| 1598 | + |
| 1599 | + def test_setsigmask_wrong_type(self): |
| 1600 | + with self.assertRaises(TypeError): |
| 1601 | + posix.posix_spawn(sys.executable, |
| 1602 | + [sys.executable, "-c", "pass"], |
| 1603 | + os.environ, setsigmask=34) |
| 1604 | + with self.assertRaises(TypeError): |
| 1605 | + posix.posix_spawn(sys.executable, |
| 1606 | + [sys.executable, "-c", "pass"], |
| 1607 | + os.environ, setsigmask=["j"]) |
| 1608 | + with self.assertRaises(ValueError): |
| 1609 | + posix.posix_spawn(sys.executable, |
| 1610 | + [sys.executable, "-c", "pass"], |
| 1611 | + os.environ, setsigmask=[signal.NSIG, |
| 1612 | + signal.NSIG+1]) |
| 1613 | + |
| 1614 | + @unittest.skipUnless(hasattr(signal, 'pthread_sigmask'), |
| 1615 | + 'need signal.pthread_sigmask()') |
| 1616 | + def test_setsigdef(self): |
| 1617 | + original_handler = signal.signal(signal.SIGUSR1, signal.SIG_IGN) |
| 1618 | + code = textwrap.dedent("""\ |
| 1619 | + import _testcapi, signal |
| 1620 | + _testcapi.raise_signal(signal.SIGUSR1)""") |
| 1621 | + try: |
| 1622 | + pid = posix.posix_spawn( |
| 1623 | + sys.executable, |
| 1624 | + [sys.executable, '-c', code], |
| 1625 | + os.environ, |
| 1626 | + setsigdef=[signal.SIGUSR1] |
| 1627 | + ) |
| 1628 | + finally: |
| 1629 | + signal.signal(signal.SIGUSR1, original_handler) |
| 1630 | + |
| 1631 | + pid2, status = os.waitpid(pid, 0) |
| 1632 | + self.assertEqual(pid2, pid) |
| 1633 | + self.assertTrue(os.WIFSIGNALED(status), status) |
| 1634 | + self.assertEqual(os.WTERMSIG(status), signal.SIGUSR1) |
| 1635 | + |
| 1636 | + def test_setsigdef_wrong_type(self): |
| 1637 | + with self.assertRaises(TypeError): |
| 1638 | + posix.posix_spawn(sys.executable, |
| 1639 | + [sys.executable, "-c", "pass"], |
| 1640 | + os.environ, setsigdef=34) |
| 1641 | + with self.assertRaises(TypeError): |
| 1642 | + posix.posix_spawn(sys.executable, |
| 1643 | + [sys.executable, "-c", "pass"], |
| 1644 | + os.environ, setsigdef=["j"]) |
| 1645 | + with self.assertRaises(ValueError): |
| 1646 | + posix.posix_spawn(sys.executable, |
| 1647 | + [sys.executable, "-c", "pass"], |
| 1648 | + os.environ, setsigdef=[signal.NSIG, signal.NSIG+1]) |
| 1649 | + |
| 1650 | + @unittest.skipUnless(hasattr(posix, 'sched_setscheduler'), "can't change scheduler") |
| 1651 | + def test_setscheduler_only_param(self): |
| 1652 | + policy = os.sched_getscheduler(0) |
| 1653 | + priority = os.sched_get_priority_min(policy) |
| 1654 | + code = textwrap.dedent(f"""\ |
| 1655 | + import os |
| 1656 | + if os.sched_getscheduler(0) != {policy}: |
| 1657 | + os.exit(101) |
| 1658 | + if os.sched_getparam(0).sched_priority != {priority}: |
| 1659 | + os.exit(102)""") |
| 1660 | + pid = posix.posix_spawn( |
| 1661 | + sys.executable, |
| 1662 | + [sys.executable, '-c', code], |
| 1663 | + os.environ, |
| 1664 | + scheduler=(None, os.sched_param(priority)) |
| 1665 | + ) |
| 1666 | + self.assertEqual(os.waitpid(pid, 0), (pid, 0)) |
| 1667 | + |
| 1668 | + @unittest.skipUnless(hasattr(posix, 'sched_setscheduler'), "can't change scheduler") |
| 1669 | + def test_setscheduler_with_policy(self): |
| 1670 | + policy = os.sched_getscheduler(0) |
| 1671 | + priority = os.sched_get_priority_min(policy) |
| 1672 | + code = textwrap.dedent(f"""\ |
| 1673 | + import os |
| 1674 | + if os.sched_getscheduler(0) != {policy}: |
| 1675 | + os.exit(101) |
| 1676 | + if os.sched_getparam(0).sched_priority != {priority}: |
| 1677 | + os.exit(102)""") |
| 1678 | + pid = posix.posix_spawn( |
| 1679 | + sys.executable, |
| 1680 | + [sys.executable, '-c', code], |
| 1681 | + os.environ, |
| 1682 | + scheduler=(policy, os.sched_param(priority)) |
| 1683 | + ) |
| 1684 | + self.assertEqual(os.waitpid(pid, 0), (pid, 0)) |
| 1685 | + |
1543 | 1686 | def test_multiple_file_actions(self):
|
1544 | 1687 | file_actions = [
|
1545 | 1688 | (os.POSIX_SPAWN_OPEN, 3, os.path.realpath(__file__), os.O_RDONLY, 0),
|
|
0 commit comments