From aff04ac8648b6109a27f9ccda34c0d9ab1026eff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Lozier?= Date: Sat, 20 Mar 2021 11:42:48 -0400 Subject: [PATCH] Enable test_contextlib --- .../PythonExceptions.BaseException.cs | 5 +--- .../Cases/CPythonCasesManifest.ini | 4 --- tests/suite/test_exceptions.py | 29 ++++++++++++++++--- 3 files changed, 26 insertions(+), 12 deletions(-) diff --git a/src/core/IronPython/Runtime/Exceptions/PythonExceptions.BaseException.cs b/src/core/IronPython/Runtime/Exceptions/PythonExceptions.BaseException.cs index 6a7bc59e5..cedf7eb7f 100644 --- a/src/core/IronPython/Runtime/Exceptions/PythonExceptions.BaseException.cs +++ b/src/core/IronPython/Runtime/Exceptions/PythonExceptions.BaseException.cs @@ -278,9 +278,6 @@ public BaseException? __context__ { get { return _context ?? __cause__; } - internal set { - _context = value; - } } public bool __suppress_context__ { get; set; } @@ -363,7 +360,7 @@ protected internal virtual void InitializeFromClr(System.Exception/*!*/ exceptio internal Exception CreateClrExceptionWithCause(BaseException? cause, BaseException? context, bool suppressContext) { _cause = cause; - _context = context; + if (context != this) _context = context; __suppress_context__ = suppressContext; _traceback = null; diff --git a/tests/IronPython.Tests/Cases/CPythonCasesManifest.ini b/tests/IronPython.Tests/Cases/CPythonCasesManifest.ini index b3f7c5f00..5e83abc85 100644 --- a/tests/IronPython.Tests/Cases/CPythonCasesManifest.ini +++ b/tests/IronPython.Tests/Cases/CPythonCasesManifest.ini @@ -275,10 +275,6 @@ Ignore=true Ignore=true Reason=ImportError: Cannot import name SemLock -[CPython.test_contextlib] -Ignore=true -Reason=Hangs - [CPython.test_copy] IsolationLevel=ENGINE MaxRecursion=100 diff --git a/tests/suite/test_exceptions.py b/tests/suite/test_exceptions.py index a2688c595..5e0996a07 100644 --- a/tests/suite/test_exceptions.py +++ b/tests/suite/test_exceptions.py @@ -87,11 +87,8 @@ def test_finally_continue_nested_finally_fails(self): finally: continue ''' - try: + with self.assertRaises(SyntaxError): compile(t, '', 'exec') - self.fail("Should raise SyntaxError") - except SyntaxError: - pass def test_bigint_division(self): def divide(a, b): @@ -1079,4 +1076,28 @@ def f(): self.assertRaises(error, f) + def test_reraise_context(self): + # reraising an exception should preserve the context + ex1 = Exception(1) + try: + try: + raise ex1 + except: + raise ex1 + except Exception as e: + self.assertIsNone(e.__context__) + + ex1 = Exception(1) + ex2 = Exception(2) + try: + try: + try: + raise ex2 + except: + raise ex1 + except: + raise ex1 + except Exception as e: + self.assertIs(e.__context__, ex2) + run_test(__name__)