Skip to content

Commit d0afd15

Browse files
committed
Revert exposing a function that directly manipulates the exception state
This is done as a separate commit to make reviewing the next commit (ie. the actual implementation) easier. If accepted, this commit should be squashed.
1 parent ca21f97 commit d0afd15

9 files changed

Lines changed: 0 additions & 357 deletions

File tree

‎Lib/contextlib.py‎

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,6 @@
2020
"chdir"]
2121

2222

23-
try:
24-
from _contextlib import _setup_genmgr_ctx
25-
except ImportError:
26-
_setup_genmgr_ctx = None
27-
28-
2923
class AbstractContextManager(abc.ABC):
3024

3125
"""An abstract base class for context managers."""
@@ -245,8 +239,6 @@ def __exit__(self, typ, value, traceback):
245239
# context to be what it was just before the generator's yield
246240
# statement before throwing the current exception into it.
247241
# (see gh-111676).
248-
if _setup_genmgr_ctx is not None:
249-
_setup_genmgr_ctx(exc_context)
250242
self.gen.throw(value)
251243
except StopIteration as exc:
252244
# Suppress StopIteration *unless* it's the same exception that

‎Lib/test/test_contextlib.py‎

Lines changed: 0 additions & 133 deletions
Original file line numberDiff line numberDiff line change
@@ -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-
18071674
if __name__ == "__main__":
18081675
unittest.main()

‎Modules/Setup.bootstrap.in‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ _weakref _weakref.c
3131

3232
# commonly used core modules
3333
_abc _abc.c
34-
_contextlib _contextlibmodule.c
3534
_functools _functoolsmodule.c
3635
_locale _localemodule.c
3736
_opcode _opcode.c

‎Modules/_contextlibmodule.c‎

Lines changed: 0 additions & 64 deletions
This file was deleted.

‎Modules/_contextlibmodules.c.h‎

Lines changed: 0 additions & 73 deletions
This file was deleted.

‎Modules/clinic/_contextlibmodule.c.h‎

Lines changed: 0 additions & 73 deletions
This file was deleted.

‎PCbuild/pythoncore.vcxproj‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -444,7 +444,6 @@
444444
</ClCompile>
445445
<ClCompile Include="..\Modules\_codecsmodule.c" />
446446
<ClCompile Include="..\Modules\_collectionsmodule.c" />
447-
<ClCompile Include="..\Modules\_contextlibmodule.c" />
448447
<ClCompile Include="..\Modules\_csv.c" />
449448
<ClCompile Include="..\Modules\_functoolsmodule.c" />
450449
<ClCompile Include="..\Modules\_hacl\Hacl_Hash_MD5.c" />

0 commit comments

Comments
 (0)