Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork33.7k
gh-95975: Move except/*/finally ref labels to more precise locations#95976
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Merged
Uh oh!
There was an error while loading.Please reload this page.
Merged
Changes fromall commits
Commits
Show all changes
7 commits Select commitHold shift + click to select a range
f741f38 gh-95975: Move except/*/finally ref labels to more precise locations
CAM-Gerlach6a0721d Add section headers to fix :keyword: role and aid navigation
CAM-Gerlachb4eca9e Move see also to the introduction rather than a particular subsection
CAM-Gerlach5db46b9 Fix other minor Sphinx syntax issues with except
CAM-Gerlach89c69bb Suppress redundant link to same section for except too
CAM-Gerlach84a0209 Don't link try/except/else/finally keywords if in the same section
CAM-Gerlachcbe74ab Format try/except/finally as keywords in modified sections
CAM-GerlachFile filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -199,10 +199,8 @@ returns the list ``[0, 1, 2]``. | ||
| .. versionchanged:: 3.11 | ||
| Starred elements are now allowed in the expression list. | ||
| .. _try: | ||
| The :keyword:`!try` statement | ||
CAM-Gerlach marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
| ============================= | ||
| @@ -215,7 +213,7 @@ The :keyword:`!try` statement | ||
| keyword: as | ||
| single: : (colon); compound statement | ||
| The :keyword:`!try` statement specifies exception handlers and/or cleanup code | ||
| for a group of statements: | ||
| .. productionlist:: python-grammar | ||
| @@ -231,40 +229,56 @@ for a group of statements: | ||
| try3_stmt: "try" ":" `suite` | ||
| : "finally" ":" `suite` | ||
| Additional information on exceptions can be found in section :ref:`exceptions`, | ||
| and information on using the :keyword:`raise` statement to generate exceptions | ||
| may be found in section :ref:`raise`. | ||
| .. _except: | ||
| :keyword:`!except` clause | ||
| ------------------------- | ||
| The :keyword:`!except` clause(s) specify one or more exception handlers. When no | ||
| exception occurs in the :keyword:`try` clause, no exception handler is executed. | ||
| When an exception occurs in the :keyword:`!try` suite, a search for an exception | ||
| handler is started. This search inspects the :keyword:`!except` clauses in turn | ||
| until one is found that matches the exception. | ||
| An expression-less :keyword:`!except` clause, if present, must be last; | ||
| it matches any exception. | ||
| For an :keyword:`!except` clause with an expression, | ||
| that expression is evaluated, and the clause matches the exception | ||
| if the resulting object is "compatible" with the exception. An object is | ||
| compatible with an exception if the object is the class or a | ||
| :term:`non-virtual base class <abstract base class>` of the exception object, | ||
| or a tuple containing an item that is the class or a non-virtual base class | ||
| of the exception object. | ||
| If no :keyword:`!except` clause matches the exception, | ||
| the search for an exception handler | ||
| continues in the surrounding code and on the invocation stack. [#]_ | ||
| If the evaluation of an expression | ||
| in the header of an :keyword:`!except` clause raises an exception, | ||
| the original search for a handler is canceled and a search starts for | ||
| the new exception in the surrounding code and on the call stack (it is treated | ||
| as if the entire :keyword:`try` statement raised the exception). | ||
| .. index:: single: as; except clause | ||
| When a matching :keyword:`!except` clause is found, | ||
| the exception is assigned to the target | ||
| specified after the :keyword:`!as` keyword in that :keyword:`!except` clause, | ||
| if present, and the :keyword:`!except` clause's suite is executed. | ||
| All :keyword:`!except` clauses must have an executable block. | ||
| When the end of this block is reached, execution continues | ||
| normally after the entire :keyword:`try` statement. | ||
| (This means that if two nested handlers exist for the same exception, | ||
| and the exception occurs in the :keyword:`!try` clause of the inner handler, | ||
| the outer handler will not handle the exception.) | ||
| When an exception has been assigned using ``as target``, it is cleared at the | ||
| end of the:keyword:`!except` clause. This is as if :: | ||
| except E as N: | ||
| foo | ||
| @@ -278,15 +292,17 @@ was translated to :: | ||
| del N | ||
| This means the exception must be assigned to a different name to be able to | ||
| refer to it after the :keyword:`!except` clause. | ||
| Exceptions are cleared because with the | ||
| traceback attached to them, they form a reference cycle with the stack frame, | ||
| keeping all locals in that frame alive until the next garbage collection occurs. | ||
| .. index:: | ||
| module: sys | ||
| object: traceback | ||
| Before an :keyword:`!except` clause's suite is executed, | ||
| details about the exception are | ||
| stored in the :mod:`sys` module and can be accessed via :func:`sys.exc_info`. | ||
| :func:`sys.exc_info` returns a 3-tuple consisting of the exception class, the | ||
| exception instance and a traceback object (see section :ref:`types`) identifying | ||
| @@ -312,17 +328,24 @@ when leaving an exception handler:: | ||
| >>> print(sys.exc_info()) | ||
| (None, None, None) | ||
| .. index:: | ||
| keyword: except_star | ||
| .. _except_star: | ||
| :keyword:`!except*` clause | ||
| -------------------------- | ||
| The :keyword:`!except*` clause(s) are used for handling | ||
| :exc:`ExceptionGroup`\s. The exception type for matching is interpreted as in | ||
| the case of :keyword:`except`, but in the case of exception groups we can have | ||
| partial matches when the type matches some of the exceptions in the group. | ||
| This means that multiple :keyword:`!except*` clauses can execute, | ||
| each handling part of the exception group. | ||
| Each clause executes once and handles an exception group | ||
| of all matching exceptions. Each exception in the group is handled by at most | ||
| one:keyword:`!except*` clause, the first that matches it. :: | ||
| >>> try: | ||
| ... raise ExceptionGroup("eg", | ||
| @@ -342,15 +365,16 @@ one except* clause, the first that matches it. :: | ||
| +------------------------------------ | ||
| >>> | ||
| Any remaining exceptions that were not handled by any:keyword:`!except*` | ||
| clauseare re-raised at the end, combined into an exception group along with | ||
| all exceptions that were raised from within:keyword:`!except*` clauses. | ||
| An :keyword:`!except*` clause must have a matching type, | ||
| and this type cannot be a subclass of :exc:`BaseExceptionGroup`. | ||
| It is not possible to mix :keyword:`except` and :keyword:`!except*` | ||
| in the same :keyword:`try`. | ||
| :keyword:`break`, :keyword:`continue` and :keyword:`return` | ||
| cannot appear in an :keyword:`!except*` clause. | ||
| .. index:: | ||
| @@ -359,17 +383,28 @@ one except* clause, the first that matches it. :: | ||
| statement: break | ||
| statement: continue | ||
| .. _except_else: | ||
| :keyword:`!else` clause | ||
| ----------------------- | ||
| The optional :keyword:`!else` clause is executed if the control flow leaves the | ||
| :keyword:`try` suite, no exception was raised, and no :keyword:`return`, | ||
| :keyword:`continue`, or :keyword:`break` statement was executed. Exceptions in | ||
| the :keyword:`!else` clause are not handled by the preceding :keyword:`except` | ||
| clauses. | ||
| .. index:: keyword: finally | ||
| .. _finally: | ||
| :keyword:`!finally` clause | ||
| -------------------------- | ||
| If :keyword:`!finally` is present, it specifies a 'cleanup' handler. The | ||
| :keyword:`try` clause is executed, including any :keyword:`except` and | ||
| :keyword:`else` clauses. If an exception occurs in any of the clauses and is | ||
| not handled, the exception is temporarily saved. The :keyword:`!finally` clause | ||
| is executed. If there is a saved exception it is re-raised at the end of the | ||
| :keyword:`!finally` clause. If the :keyword:`!finally` clause raises another | ||
| @@ -387,7 +422,7 @@ or :keyword:`continue` statement, the saved exception is discarded:: | ||
| 42 | ||
| The exception information is not available to the program during execution of | ||
| the :keyword:`!finally` clause. | ||
| .. index:: | ||
| statement: return | ||
| @@ -396,10 +431,10 @@ the :keyword:`finally` clause. | ||
| When a :keyword:`return`, :keyword:`break` or :keyword:`continue` statement is | ||
| executed in the :keyword:`try` suite of a :keyword:`!try`...\ :keyword:`!finally` | ||
| statement, the :keyword:`!finally` clause is also executed 'on the way out.' | ||
| The return value of a function is determined by the last :keyword:`return` | ||
| statement executed. Since the :keyword:`!finally` clause always executes, a | ||
| :keyword:`!return` statement executed in the :keyword:`!finally` clause will | ||
| always be the last one executed:: | ||
| @@ -412,13 +447,9 @@ always be the last one executed:: | ||
| >>> foo() | ||
| 'finally' | ||
| .. versionchanged:: 3.8 | ||
| Prior to Python 3.8, a :keyword:`continue` statement was illegal in the | ||
| :keyword:`!finally` clause due to a problem with the implementation. | ||
| .. _with: | ||
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.