Skip to content

gh-144386: Update equivalent code for "with", "async with" and "async for"#144472

Merged
serhiy-storchaka merged 1 commit intopython:mainfrom
serhiy-storchaka:docs-special-methods-lookup
Feb 18, 2026
Merged

gh-144386: Update equivalent code for "with", "async with" and "async for"#144472
serhiy-storchaka merged 1 commit intopython:mainfrom
serhiy-storchaka:docs-special-methods-lookup

Conversation

@serhiy-storchaka
Copy link
Member

@serhiy-storchaka serhiy-storchaka commented Feb 4, 2026

They use special method lookup for special methods.


📚 Documentation preview 📚: https://cpython-previews--144472.org.readthedocs.build/

…"async for"

They use special method lookup for special methods.
@tanloong
Copy link
Contributor

tanloong commented Feb 5, 2026

I happened to notice that there might be a small point to adjust. The updated equivalent code manager.__enter__ could look up __enter__ in the instance attributes, but the with-statement does not.

import sys
class CM:
    def __init__(self):
        def __enter__():
            pass
        def __exit__(*exc_details):
            pass
        self.__enter__ = __enter__
        self.__exit__ = __exit__


# Not works: TypeError: 'CM' object does not support the context manager protocol (missed __exit__ method)
with CM():
    pass

# Works
manager = CM()
enter = manager.__enter__
exit_ = manager.__exit__
value = enter()
hit_except = False
try:
    TARGET = value
except:
    hit_except = True
    if not exit(*sys.exc_info()):
        raise
finally:
    if not hit_except:
        exit_(None, None, None)

@serhiy-storchaka
Copy link
Member Author

Yes, this is what the added note for.

@tanloong
Copy link
Contributor

tanloong commented Feb 5, 2026

Oops, I missed that.

@miss-islington-app
Copy link

Thanks @serhiy-storchaka for the PR 🌮🎉.. I'm working now to backport this PR to: 3.13, 3.14.
🐍🍒⛏🤖

@serhiy-storchaka serhiy-storchaka deleted the docs-special-methods-lookup branch February 18, 2026 11:13
miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Feb 18, 2026
…"async for" (pythonGH-144472)

They use special method lookup for special methods.
(cherry picked from commit 9e8fa2d)

Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Feb 18, 2026
…"async for" (pythonGH-144472)

They use special method lookup for special methods.
(cherry picked from commit 9e8fa2d)

Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
@bedevere-app
Copy link

bedevere-app bot commented Feb 18, 2026

GH-144945 is a backport of this pull request to the 3.14 branch.

@bedevere-app bedevere-app bot removed the needs backport to 3.14 bugs and security fixes label Feb 18, 2026
@bedevere-app
Copy link

bedevere-app bot commented Feb 18, 2026

GH-144946 is a backport of this pull request to the 3.13 branch.

@bedevere-app bedevere-app bot removed the needs backport to 3.13 bugs and security fixes label Feb 18, 2026
serhiy-storchaka added a commit that referenced this pull request Feb 18, 2026
… "async for" (GH-144472) (GH-144945)

They use special method lookup for special methods.
(cherry picked from commit 9e8fa2d)

Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
serhiy-storchaka added a commit that referenced this pull request Feb 18, 2026
… "async for" (GH-144472) (GH-144946)

They use special method lookup for special methods.
(cherry picked from commit 9e8fa2d)

Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

docs Documentation in the Doc dir skip news

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

3 participants

Comments