Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
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
4 changes: 4 additions & 0 deletions Doc/c-api/exceptions.rst
Original file line number Diff line number Diff line change
Expand Up @@ -601,6 +601,10 @@ Exception Objects
it. There is no type check to make sure that *ctx* is an exception instance.
This steals a reference to *ctx*.

.. versionchanged:: 3.5.2
If *ctx* is *ex*, the function has no any effect. If *ex* is in the
context chain started from *ctx*, it is moved to the start of the chain.
Comment on lines +605 to +606
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
If *ctx* is *ex*, the function has no any effect. If *ex* is in the
context chain started from *ctx*, it is moved to the start of the chain.
If *ctx* is *ex*, the function has no effect. If *ex* is in the
context chain started from *ctx*, it is moved to the start of the chain.



.. c:function:: PyObject* PyException_GetCause(PyObject *ex)

Expand Down
6 changes: 6 additions & 0 deletions Doc/library/exceptions.rst
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,12 @@ In either case, the exception itself is always shown after any chained
exceptions so that the final line of the traceback always shows the last
exception that was raised.

.. versionchanged:: 3.5.2

Setting :attr:`__context__` to self has no any effect. If set
``__context__`` to the chain contained original exception, it is moved to
the start of the context chain.
Comment on lines +71 to +73
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Setting :attr:`__context__` to self has no any effect. If set
``__context__`` to the chain contained original exception, it is moved to
the start of the context chain.
Setting :attr:`__context__` to self has no effect. If set
``__context__`` to the chain contained an original exception, it is moved to
the start of the context chain.



Base classes
------------
Expand Down
20 changes: 20 additions & 0 deletions Lib/test/test_exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -716,6 +716,26 @@ def __exit__ (self, exc_type, exc_value, exc_tb):
obj = wr()
self.assertIsNone(obj)

def test_context_loop(self):
exc1 = Exception(1)
exc1.__context__ = exc1
self.assertIsNone(exc1.__context__)

exc2 = Exception(2)
exc2.__context__ = exc1
self.assertIs(exc2.__context__, exc1)
exc1.__context__ = exc2
self.assertIs(exc1.__context__, exc2)
self.assertIsNone(exc2.__context__)

exc3 = Exception(3)
exc3.__context__ = exc1
self.assertIs(exc3.__context__, exc1)
exc1.__context__ = exc3
self.assertIs(exc1.__context__, exc3)
self.assertIs(exc3.__context__, exc2)
self.assertIsNone(exc2.__context__)

def test_exception_target_in_nested_scope(self):
# issue 4617: This used to raise a SyntaxError
# "can not delete variable 'e' referenced in nested scope"
Expand Down
26 changes: 25 additions & 1 deletion Objects/exceptions.c
Original file line number Diff line number Diff line change
Expand Up @@ -356,9 +356,33 @@ PyException_GetContext(PyObject *self)
void
PyException_SetContext(PyObject *self, PyObject *context)
{
Py_XSETREF(_PyBaseExceptionObject_cast(self)->context, context);
assert(PyExceptionInstance_Check(self));
assert(context == NULL || PyExceptionInstance_Check(context));

PyObject *exc = context;
PyObject *prev_exc = NULL;

while (exc != NULL) {
if (exc == self) {
if (prev_exc != NULL) {
/* Move self to the start of the chain. */
((PyBaseExceptionObject *)prev_exc)->context =
((PyBaseExceptionObject *)self)->context;
((PyBaseExceptionObject *)self)->context = context;
}
assert(Py_REFCNT(self) > 1);
Py_DECREF(self);
return;
}
assert(PyExceptionInstance_Check(exc));
prev_exc = exc;
exc = ((PyBaseExceptionObject *)exc)->context;
}

Py_XSETREF(((PyBaseExceptionObject *)self)->context, context);
}


#undef PyExceptionClass_Name

const char *
Expand Down