Skip to content

gh-69968: Prevent cycles in the __context__ chain. - #20543

Draft
serhiy-storchaka wants to merge 2 commits into
python:mainfrom
serhiy-storchaka:set-context-reordering
Draft

serhiy-storchaka wants to merge 2 commits into
python:mainfrom
serhiy-storchaka:set-context-reordering

Conversation

@serhiy-storchaka

@serhiy-storchaka serhiy-storchaka commented May 30, 2020 •

Copy link
Copy Markdown
Member

Setting __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.

https://bugs.python.org/issue25782

Comment thread Doc/c-api/exceptions.rst
Comment on lines +605 to +606
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.

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.

Comment on lines +71 to +73
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.

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.

@ambv

ambv commented May 17, 2022

Copy link
Copy Markdown
Contributor

This missed the boat for inclusion in Python 3.9 which accepts security fixes only as of today.

@github-actions

github-actions Bot commented Apr 8, 2026

Copy link
Copy Markdown

This PR is stale because it has been open for 30 days with no activity.

@github-actions github-actions Bot added the stale Stale PR or inactive for long period of time. label Apr 8, 2026
@serhiy-storchaka serhiy-storchaka changed the title bpo-25782: Prevent cycles in the __context__ chain. gh-69968: Prevent cycles in the __context__ chain. Aug 12, 2026
@github-actions github-actions Bot removed the stale Stale PR or inactive for long period of time. label Aug 14, 2026
Setting __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.
@ambv
ambv force-pushed the set-context-reordering branch from a87b7ed to 5d32953 Compare October 20, 2026 21:01
@read-the-docs-community

read-the-docs-community Bot commented Oct 20, 2026 •

Copy link
Copy Markdown

Documentation build overview

📚 cpython-previews | 🛠️ Build #35285557 | 📁 Comparing 167dab3 against main (80040b6)

  🔍 Preview build  

4 files changed
± builtins/exceptions.html
± c-api/exceptions.html
± library/ast.html
± whatsnew/changelog.html

@python-cla-bot

python-cla-bot Bot commented Oct 21, 2026 •

Copy link
Copy Markdown

All commit authors signed the Contributor License Agreement.

CLA signed

…thongh-69968)

Py_END_CRITICAL_SECTION() emits a closing brace, so calling it inside the
nested if/while early-return path closed the wrong block and left
'base_self' undeclared after the loop. Restructure to a single exit:
record the cycle with a flag, then release the lock before Py_DECREF.

Also add the missing news entry.

Co-authored-by: Muse Code (Meta Muse Spark)
@ambv
ambv force-pushed the set-context-reordering branch from e0f22af to 167dab3 Compare October 21, 2026 01:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

type-bug An unexpected behavior, bug, or error

Projects

None yet

Development

Successfully merging this pull request may close these issues.

8 participants