Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
more test cases. Add whatnew entry
  • Loading branch information
iritkatriel committed Jan 12, 2022
commit e660f0700a8dabab242b796a591d597e75b8a836
3 changes: 3 additions & 0 deletions Doc/whatsnew/3.11.rst
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,9 @@ sys
the results of subsequent calls to :func:`exc_info`.
(Contributed by Irit Katriel in :issue:`45711`.)

* Add :func:`sys.exception` which returns the active exception instance
(equivalent to ``sys.exc_info()[1]``).
(Contributed by Irit Katriel in :issue:`46328`.)

threading
---------
Expand Down
53 changes: 48 additions & 5 deletions Lib/test/test_sys.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,14 @@ def baddisplayhook(obj):
code = compile("42", "<string>", "single")
self.assertRaises(ValueError, eval, code)

class ExcInfoTest(unittest.TestCase):
class ActiveExceptionTests(unittest.TestCase):
def test_exc_info_no_exception(self):
self.assertEqual(sys.exc_info(), (None, None, None))

def test_sys_exception_no_exception(self):
self.assertEqual(sys.exception(), None)

def test_exc_info_with_exception(self):
def test_exc_info_with_exception_instance(self):
def f():
raise ValueError(42)
Comment thread
iritkatriel marked this conversation as resolved.

Expand All @@ -85,13 +87,54 @@ def f():
except Exception as e_:
e = e_
exc_info = sys.exc_info()
exc = sys.exception()

self.assertIs(exc, e)
self.assertIs(exc_info[0], type(e))
self.assertIsInstance(e, ValueError)
self.assertIs(exc_info[0], ValueError)
self.assertIs(exc_info[1], e)
self.assertIs(exc_info[2], e.__traceback__)

def test_exc_info_with_exception_type(self):
def f():
raise ValueError

try:
f()
except Exception as e_:
e = e_
exc_info = sys.exc_info()

self.assertIsInstance(e, ValueError)
self.assertIs(exc_info[0], ValueError)
self.assertIs(exc_info[1], e)
self.assertIs(exc_info[2], e.__traceback__)

def test_sys_exception_with_exception_instance(self):
def f():
raise ValueError(42)

try:
f()
except Exception as e_:
e = e_
exc = sys.exception()

self.assertIsInstance(e, ValueError)
self.assertIs(exc, e)

def test_sys_exception_with_exception_type(self):
def f():
raise ValueError

try:
f()
except Exception as e_:
e = e_
exc = sys.exception()

self.assertIsInstance(e, ValueError)
self.assertIs(exc, e)


class ExceptHookTest(unittest.TestCase):

def test_original_excepthook(self):
Expand Down