From e48fbea5a592229803120725e016be1006b85380 Mon Sep 17 00:00:00 2001 From: Devin Jeanpierre <@ssbr> Date: Fri, 29 Jan 2016 14:11:25 -0800 Subject: [PATCH] Make all classes new-style in Python 2, allowing them to be used with ExitStack. --- contextlib2.py | 4 ++-- test_contextlib2.py | 10 ++++++++++ 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/contextlib2.py b/contextlib2.py index 2b80384..133b8e4 100644 --- a/contextlib2.py +++ b/contextlib2.py @@ -179,7 +179,7 @@ class closing(object): self.thing.close() -class _RedirectStream: +class _RedirectStream(object): _stream = None @@ -219,7 +219,7 @@ class redirect_stderr(_RedirectStream): _stream = "stderr" -class suppress: +class suppress(object): """Context manager to suppress specified exceptions After the exception is suppressed, execution proceeds with the next diff --git a/test_contextlib2.py b/test_contextlib2.py index 3a12585..478b3aa 100755 --- a/test_contextlib2.py +++ b/test_contextlib2.py @@ -763,6 +763,11 @@ class TestRedirectStream: s = f.getvalue() self.assertEqual(s, "Hello World!\n") + def test_cm_is_exitstack_compatible(self): + with ExitStack() as stack: + # This shouldn't raise an exception. + stack.enter_context(self.redirect_stream(io.StringIO())) + class TestRedirectStdout(TestRedirectStream, unittest.TestCase): @@ -830,6 +835,11 @@ class TestSuppress(unittest.TestCase): 1/0 self.assertTrue(outer_continued) + def test_cm_is_exitstack_compatible(self): + with ExitStack() as stack: + # This shouldn't raise an exception. + stack.enter_context(suppress()) + if __name__ == "__main__": import unittest unittest.main()