Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gh-126664: Use else instead of finally in "The with statement" documentation. #126665

Merged
merged 1 commit into from
Nov 11, 2024

Conversation

vivodi
Copy link
Contributor

@vivodi vivodi commented Nov 11, 2024

Documentation

In 8.5 The with statement, we can use else instead of finally.

manager = (EXPRESSION)
enter = type(manager).__enter__
exit = type(manager).__exit__
value = enter(manager)
hit_except = False

try:
    TARGET = value
    SUITE
except:
    hit_except = True
    if not exit(manager, *sys.exc_info()):
        raise
finally:
    if not hit_except:
        exit(manager, None, None, None)

is semantically equivalent to:

  manager = (EXPRESSION)
  enter = type(manager).__enter__
  exit = type(manager).__exit__
  value = enter(manager)

  try:
      TARGET = value
      SUITE
  except:
      if not exit(manager, *sys.exc_info()):
          raise
  else:
      exit(manager, None, None, None)

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

@vivodi vivodi requested a review from willingc as a code owner November 11, 2024 05:22
Copy link

cpython-cla-bot bot commented Nov 11, 2024

All commit authors signed the Contributor License Agreement.
CLA signed

@gpshead gpshead merged commit 25257d6 into python:main Nov 11, 2024
40 checks passed
@gpshead gpshead added needs backport to 3.12 bug and security fixes needs backport to 3.13 bugs and security fixes labels Nov 11, 2024
@miss-islington-app
Copy link

Thanks @vivodi for the PR, and @gpshead for merging it 🌮🎉.. I'm working now to backport this PR to: 3.13.
🐍🍒⛏🤖 I'm not a witch! I'm not a witch!

@miss-islington-app
Copy link

Thanks @vivodi for the PR, and @gpshead for merging it 🌮🎉.. I'm working now to backport this PR to: 3.12.
🐍🍒⛏🤖

miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Nov 11, 2024
…nt" documentation. (pythonGH-126665)

(cherry picked from commit 25257d6)

Co-authored-by: vivodi <[email protected]>
@bedevere-app
Copy link

bedevere-app bot commented Nov 11, 2024

GH-126670 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 Nov 11, 2024
miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Nov 11, 2024
…nt" documentation. (pythonGH-126665)

(cherry picked from commit 25257d6)

Co-authored-by: vivodi <[email protected]>
@bedevere-app
Copy link

bedevere-app bot commented Nov 11, 2024

GH-126671 is a backport of this pull request to the 3.12 branch.

@bedevere-app bedevere-app bot removed the needs backport to 3.12 bug and security fixes label Nov 11, 2024
gpshead pushed a commit that referenced this pull request Nov 11, 2024
…ent" documentation. (GH-126665) (#126670)

gh-126664: Use `else` instead of `finally` in "The with statement" documentation. (GH-126665)
(cherry picked from commit 25257d6)

Co-authored-by: vivodi <[email protected]>
gpshead pushed a commit that referenced this pull request Nov 11, 2024
…ent" documentation. (GH-126665) (#126671)

gh-126664: Use `else` instead of `finally` in "The with statement" documentation. (GH-126665)
(cherry picked from commit 25257d6)

Co-authored-by: vivodi <[email protected]>
picnixz pushed a commit to picnixz/cpython that referenced this pull request Dec 8, 2024
@rrevans
Copy link

rrevans commented Dec 22, 2024

The try statement example is confusing and/or incorrect after this PR, at least when SUITE contains any return, continue, or break statement. In those cases the else clause of the try statement is not executed.

But with statements invoke the __exit__ function in all cases after the __enter__ function returns without raising an exception, so the given example does not match the actual semantics of with statements if SUITE contains such a statement.

I think this should be reverted. The semantically-equivalent sample code is not helpful if it applies only to suites without those kinds of statements (or it would be confusing to qualify when the semantics are correct).

Example:

for _ in range(1):
    try:
        break
    except:
        pass
    else:
        print('exit')

produces no output (as expected for a try-else statement)

but this does (as expected for a with statement)

class Context:
    def __enter__(self):
        pass
    def __exit__(self, *args):
        print('exit')

for _ in range(1):
    with Context():
        break

gpshead added a commit to gpshead/cpython that referenced this pull request Dec 22, 2024
… statement" documentation. (pythonGH-126665)"

This reverts commit 25257d6.
@gpshead
Copy link
Member

gpshead commented Dec 22, 2024

thanks! yes, you are correct. revert PRs in flight.

gpshead added a commit that referenced this pull request Dec 22, 2024
… "with" (#128169)

Revert "gh-126664: Use `else` instead of `finally` in "The with statement" documentation. (GH-126665)"

This reverts commit 25257d6.
miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Dec 22, 2024
…aining "with" (pythonGH-128169)

Revert "pythongh-126664: Use `else` instead of `finally` in "The with statement" documentation. (pythonGH-126665)"

This reverts commit 25257d6.
(cherry picked from commit 228f275)

Co-authored-by: Gregory P. Smith <[email protected]>
miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Dec 22, 2024
…aining "with" (pythonGH-128169)

Revert "pythongh-126664: Use `else` instead of `finally` in "The with statement" documentation. (pythonGH-126665)"

This reverts commit 25257d6.
(cherry picked from commit 228f275)

Co-authored-by: Gregory P. Smith <[email protected]>
srinivasreddy pushed a commit to srinivasreddy/cpython that referenced this pull request Dec 23, 2024
…aining "with" (python#128169)

Revert "pythongh-126664: Use `else` instead of `finally` in "The with statement" documentation. (pythonGH-126665)"

This reverts commit 25257d6.
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.

4 participants