FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

spam · Issue #144671 · python/cpython · GitHub

Repository navigation

spam #144671

Closed as not planned
Closed as not planned

Description

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)

Originally posted by @tanloong in #144472 (comment)

Activity

  1. changed the title [-]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.[/-] [+]The updated equivalent code `manager.__enter__` could look up `__enter__` in the instance attributes, but the `with-statement` does not.[/+] on Feb 10, 2026
  2. changed the title [-]The updated equivalent code `manager.__enter__` could look up `__enter__` in the instance attributes, but the `with-statement` does not.[/-] [+]spam[/+] on Feb 10, 2026
  3. quanghuynh10111-png commented on Feb 10, 2026

    Author
  4. locked and limited conversation to collaborators on Feb 10, 2026
  5. locked as spam and limited conversation to collaborators on Feb 10, 2026
  6. hugovk commented on Feb 10, 2026

    Member

    Reported @quanghuynh10111-png to GH.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions


      Back | FazBrowse Home | New Git URL