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
6 changes: 6 additions & 0 deletions Doc/builtins/exceptions.rst
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,12 @@
conflicts in the future. Therefore, it's recommended to avoid
subclassing multiple exception types altogether.

.. versionchanged:: 3.5.2

Setting :attr:`__context__` to self has no any effect. If set

Check warning on line 105 in Doc/builtins/exceptions.rst

View workflow job for this annotation

GitHub Actions / Docs / Docs

py:attr reference target not found: __context__ [ref.attr]
``__context__`` to the chain contained original exception, it is moved to
the start of the context chain.
Comment on lines +105 to +107

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
4 changes: 4 additions & 0 deletions Doc/c-api/exceptions.rst
Original file line number Diff line number Diff line change
Expand Up @@ -888,6 +888,10 @@ Exception Objects
it. There is no type check to make sure that *ctx* is an exception instance.
This ":term:`steals <steal>`" 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 +892 to +893

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
20 changes: 20 additions & 0 deletions Lib/test/test_exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -987,6 +987,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
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Setting :attr:`~BaseException.__context__` to the exception itself now has no
effect, and setting it to a chain containing the exception moves the
exception to the start of the chain, preventing cycles.
38 changes: 37 additions & 1 deletion Objects/exceptions.c
Original file line number Diff line number Diff line change
Expand Up @@ -551,9 +551,45 @@ PyException_GetContext(PyObject *self)
void
PyException_SetContext(PyObject *self, PyObject *context)
{
assert(PyExceptionInstance_Check(self));
assert(context == NULL || PyExceptionInstance_Check(context));

int self_in_chain = 0;

Py_BEGIN_CRITICAL_SECTION(self);
Py_XSETREF(PyBaseExceptionObject_CAST(self)->context, context);
PyBaseExceptionObject *base_self = PyBaseExceptionObject_CAST(self);

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_CAST(prev_exc)->context =
base_self->context;
base_self->context = context;
}
self_in_chain = 1;
break;
}
assert(PyExceptionInstance_Check(exc));
prev_exc = exc;
exc = PyBaseExceptionObject_CAST(exc)->context;
}

if (self_in_chain) {
/* Self was already in the chain; drop the lost link reference. */
assert(Py_REFCNT(self) > 1);
}
else {
Py_XSETREF(base_self->context, context);
}
Py_END_CRITICAL_SECTION();

if (self_in_chain) {
Py_DECREF(self);
}
}

PyObject *
Expand Down
Loading