@@ -1671,138 +1671,5 @@ def test_exception(self):
16711671 self .assertEqual (os .getcwd (), old_cwd )
16721672
16731673
1674- try :
1675- from _contextlib import _setup_genmgr_ctx
1676- except ImportError :
1677- _setup_genmgr_ctx = None
1678-
1679- if _setup_genmgr_ctx :
1680- class SetExceptionTests (unittest .TestCase ):
1681-
1682- def tearDown (self ):
1683- # make sure we don't leave the global exception set
1684- _setup_genmgr_ctx (None );
1685-
1686- def test_set_exc_invalid_values (self ):
1687- for x in (0 , "1" , b"2" ):
1688- with self .assertRaises (TypeError ):
1689- _setup_genmgr_ctx (x );
1690-
1691- def test_clear_exc (self ):
1692- try :
1693- raise ValueError ()
1694- except ValueError :
1695- self .assertIsInstance (sys .exception (), ValueError )
1696- _setup_genmgr_ctx (None )
1697- self .assertIsNone (sys .exception ())
1698-
1699- def test_set_exc (self ):
1700- exc = ValueError ()
1701- self .assertIsNone (sys .exception ())
1702- _setup_genmgr_ctx (exc )
1703- self .assertIs (sys .exception (), exc )
1704-
1705- def test_set_exc_replaced_by_new_exception_and_restored (self ):
1706- exc = ValueError ()
1707- _setup_genmgr_ctx (exc )
1708- self .assertIs (sys .exception (), exc )
1709- try :
1710- raise TypeError ()
1711- except TypeError :
1712- self .assertIsInstance (sys .exception (), TypeError )
1713- self .assertIs (sys .exception ().__context__ , exc )
1714-
1715- self .assertIs (sys .exception (), exc )
1716-
1717- def test_set_exc_popped_on_exit_except (self ):
1718- exc = ValueError ()
1719- try :
1720- raise TypeError ()
1721- except TypeError :
1722- self .assertIsInstance (sys .exception (), TypeError )
1723- _setup_genmgr_ctx (exc )
1724- self .assertIs (sys .exception (), exc )
1725- self .assertIsNone (sys .exception ())
1726-
1727- def test_cleared_exc_overridden_and_restored (self ):
1728- try :
1729- raise ValueError ()
1730- except ValueError :
1731- try :
1732- raise TypeError ()
1733- except TypeError :
1734- self .assertIsInstance (sys .exception (), TypeError )
1735- _setup_genmgr_ctx (None )
1736- self .assertIsNone (sys .exception ())
1737- try :
1738- raise IndexError ()
1739- except IndexError :
1740- self .assertIsInstance (sys .exception (), IndexError )
1741- self .assertIsNone (sys .exception ().__context__ )
1742- self .assertIsNone (sys .exception ())
1743- self .assertIsInstance (sys .exception (), ValueError )
1744- self .assertIsNone (sys .exception ())
1745-
1746- def test_clear_exc_in_generator (self ):
1747- def inner ():
1748- self .assertIsNone (sys .exception ())
1749- yield
1750- self .assertIsInstance (sys .exception (), ValueError )
1751- _setup_genmgr_ctx (None )
1752- self .assertIsNone (sys .exception ())
1753- yield
1754- self .assertIsNone (sys .exception ())
1755-
1756- # with a single exception in exc_info stack
1757- g = inner ()
1758- next (g )
1759- try :
1760- raise ValueError ()
1761- except :
1762- self .assertIsInstance (sys .exception (), ValueError )
1763- next (g )
1764- self .assertIsInstance (sys .exception (), ValueError )
1765- self .assertIsNone (sys .exception ())
1766- with self .assertRaises (StopIteration ):
1767- next (g )
1768- self .assertIsNone (sys .exception ())
1769-
1770- # with multiple exceptions in exc_info stack by chaining generators
1771- def outer ():
1772- g = inner ()
1773- self .assertIsNone (sys .exception ())
1774- yield next (g )
1775- self .assertIsInstance (sys .exception (), TypeError )
1776- try :
1777- raise ValueError ()
1778- except :
1779- self .assertIsInstance (sys .exception (), ValueError )
1780- self .assertIsInstance (sys .exception ().__context__ , TypeError )
1781- yield next (g )
1782- # at this point the TypeError from the caller has been handled
1783- # by the caller's except block. Even still, it should still be
1784- # referenced as the __context__ of the current exception.
1785- self .assertIsInstance (sys .exception (), ValueError )
1786- self .assertIsInstance (sys .exception ().__context__ , TypeError )
1787- # not handling an exception, caller isn't handling one either
1788- self .assertIsNone (sys .exception ())
1789- with self .assertRaises (StopIteration ):
1790- next (g )
1791- self .assertIsNone (sys .exception ())
1792-
1793- g = outer ()
1794- next (g )
1795- try :
1796- raise TypeError ()
1797- except :
1798- self .assertIsInstance (sys .exception (), TypeError )
1799- next (g )
1800- self .assertIsInstance (sys .exception (), TypeError )
1801- self .assertIsNone (sys .exception ())
1802- with self .assertRaises (StopIteration ):
1803- next (g )
1804- self .assertIsNone (sys .exception ())
1805-
1806-
18071674if __name__ == "__main__" :
18081675 unittest .main ()
0 commit comments