@@ -1636,7 +1636,12 @@ class SubinterpImportTests(unittest.TestCase):
1636
1636
allow_exec = False ,
1637
1637
allow_threads = True ,
1638
1638
allow_daemon_threads = False ,
1639
+ # Isolation-related config values aren't included here.
1639
1640
)
1641
+ ISOLATED = dict (
1642
+ use_main_obmalloc = False ,
1643
+ )
1644
+ NOT_ISOLATED = {k : not v for k , v in ISOLATED .items ()}
1640
1645
1641
1646
@unittest .skipUnless (hasattr (os , "pipe" ), "requires os.pipe()" )
1642
1647
def pipe (self ):
@@ -1669,6 +1674,7 @@ def import_script(self, name, fd, check_override=None):
1669
1674
def run_here (self , name , * ,
1670
1675
check_singlephase_setting = False ,
1671
1676
check_singlephase_override = None ,
1677
+ isolated = False ,
1672
1678
):
1673
1679
"""
1674
1680
Try importing the named module in a subinterpreter.
@@ -1689,6 +1695,7 @@ def run_here(self, name, *,
1689
1695
1690
1696
kwargs = dict (
1691
1697
** self .RUN_KWARGS ,
1698
+ ** (self .ISOLATED if isolated else self .NOT_ISOLATED ),
1692
1699
check_multi_interp_extensions = check_singlephase_setting ,
1693
1700
)
1694
1701
@@ -1699,33 +1706,36 @@ def run_here(self, name, *,
1699
1706
self .assertEqual (ret , 0 )
1700
1707
return os .read (r , 100 )
1701
1708
1702
- def check_compatible_here (self , name , * , strict = False ):
1709
+ def check_compatible_here (self , name , * , strict = False , isolated = False ):
1703
1710
# Verify that the named module may be imported in a subinterpreter.
1704
1711
# (See run_here() for more info.)
1705
1712
out = self .run_here (name ,
1706
1713
check_singlephase_setting = strict ,
1714
+ isolated = isolated ,
1707
1715
)
1708
1716
self .assertEqual (out , b'okay' )
1709
1717
1710
- def check_incompatible_here (self , name ):
1718
+ def check_incompatible_here (self , name , * , isolated = False ):
1711
1719
# Differences from check_compatible_here():
1712
1720
# * verify that import fails
1713
1721
# * "strict" is always True
1714
1722
out = self .run_here (name ,
1715
1723
check_singlephase_setting = True ,
1724
+ isolated = isolated ,
1716
1725
)
1717
1726
self .assertEqual (
1718
1727
out .decode ('utf-8' ),
1719
1728
f'ImportError: module { name } does not support loading in subinterpreters' ,
1720
1729
)
1721
1730
1722
- def check_compatible_fresh (self , name , * , strict = False ):
1731
+ def check_compatible_fresh (self , name , * , strict = False , isolated = False ):
1723
1732
# Differences from check_compatible_here():
1724
1733
# * subinterpreter in a new process
1725
1734
# * module has never been imported before in that process
1726
1735
# * this tests importing the module for the first time
1727
1736
kwargs = dict (
1728
1737
** self .RUN_KWARGS ,
1738
+ ** (self .ISOLATED if isolated else self .NOT_ISOLATED ),
1729
1739
check_multi_interp_extensions = strict ,
1730
1740
)
1731
1741
_ , out , err = script_helper .assert_python_ok ('-c' , textwrap .dedent (f'''
@@ -1743,12 +1753,13 @@ def check_compatible_fresh(self, name, *, strict=False):
1743
1753
self .assertEqual (err , b'' )
1744
1754
self .assertEqual (out , b'okay' )
1745
1755
1746
- def check_incompatible_fresh (self , name ):
1756
+ def check_incompatible_fresh (self , name , * , isolated = False ):
1747
1757
# Differences from check_compatible_fresh():
1748
1758
# * verify that import fails
1749
1759
# * "strict" is always True
1750
1760
kwargs = dict (
1751
1761
** self .RUN_KWARGS ,
1762
+ ** (self .ISOLATED if isolated else self .NOT_ISOLATED ),
1752
1763
check_multi_interp_extensions = True ,
1753
1764
)
1754
1765
_ , out , err = script_helper .assert_python_ok ('-c' , textwrap .dedent (f'''
@@ -1854,6 +1865,14 @@ def check_incompatible(setting, override):
1854
1865
with self .subTest ('config: check disabled; override: disabled' ):
1855
1866
check_compatible (False , - 1 )
1856
1867
1868
+ def test_isolated_config (self ):
1869
+ module = 'threading'
1870
+ require_pure_python (module )
1871
+ with self .subTest (f'{ module } : strict, not fresh' ):
1872
+ self .check_compatible_here (module , strict = True , isolated = True )
1873
+ with self .subTest (f'{ module } : strict, fresh' ):
1874
+ self .check_compatible_fresh (module , strict = True , isolated = True )
1875
+
1857
1876
1858
1877
class TestSinglePhaseSnapshot (ModuleSnapshot ):
1859
1878
0 commit comments