[issue14167] document return statement in finally blocks

New submission from Yury Selivanov <yselivanov@gmail.com>: I think that the documentation should put more emphasis on the `return` statement in a `finally` block. Example: def test(): try: 1/0 finally: return 10
test() 10
I think we need to add a warning, or at least a note, that 'return' masks exceptions, if any occurred. ---------- assignee: docs@python components: Documentation messages: 154697 nosy: Yury.Selivanov, docs@python, georg.brandl, rhettinger priority: normal severity: normal status: open title: document return statement in finally blocks type: enhancement versions: Python 2.7, Python 3.2 _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue14167> _______________________________________

Éric Araujo <merwok@netwok.org> added the comment: This behavior surprised me for a second, but it makes sense. An example in the docs is certainly appropriate. Would you like to suggest a place and phrasing for it? ---------- keywords: +easy nosy: +eric.araujo stage: -> needs patch versions: +Python 3.3 _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue14167> _______________________________________

Yury Selivanov <yselivanov@gmail.com> added the comment: Well, I guess the best place to put it would be http://docs.python.org/py3k/reference/compound_stmts.html#finally I've attached the patch, please take a look. If it's possible to make it more clear, feel free to rephrase it. But please include the example, this behaviour needs to be emphasized. ---------- keywords: +patch Added file: http://bugs.python.org/file24709/finally_doc.patch _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue14167> _______________________________________

Changes by Andrew Svetlov <andrew.svetlov@gmail.com>: ---------- nosy: +asvetlov _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue14167> _______________________________________

Roundup Robot added the comment: New changeset e0e8e70e4035 by Andrew Svetlov in branch '3.2': Issue #14167: Document return statement in finally blocks. http://hg.python.org/cpython/rev/e0e8e70e4035 New changeset 05714e9811fa by Andrew Svetlov in branch 'default': Issue #14167: Document return statement in finally blocks. http://hg.python.org/cpython/rev/05714e9811fa New changeset bef098bd3fa5 by Andrew Svetlov in branch '2.7': Issue #14167: Document return statement in finally blocks. http://hg.python.org/cpython/rev/bef098bd3fa5 ---------- nosy: +python-dev _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue14167> _______________________________________

Andrew Svetlov added the comment: Fixed. Thanks. ---------- resolution: -> fixed stage: needs patch -> committed/rejected status: open -> closed _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue14167> _______________________________________

Mark Dickinson added the comment: I think something went wrong with this doc change. The docs used to say: """ If the :keyword:`finally` clause raises another exception or executes a :keyword:`return` or :keyword:`break` statement, the saved exception is lost. """ Now we have: """ If there is a saved exception or :keyword:`break` statement, it is re-raised at the end of the :keyword:`finally` clause. """ which doesn't make much sense (what does 'it' refer to in the case of a 'break' statement), and we seem to have lost the explicit statement that a 'break' in a finally cause swallows exceptions. ---------- nosy: +mark.dickinson _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue14167> _______________________________________

Mark Dickinson added the comment: See also issue 16021. Re-opening this issue for reconsideration. ---------- resolution: fixed -> status: closed -> open _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue14167> _______________________________________

Mark Dickinson added the comment: Bah. Issue 16028; sorry. ---------- _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue14167> _______________________________________

Yury Selivanov added the comment: Can you close this one? It's already merged. ---------- nosy: +yselivanov _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue14167> _______________________________________

Yury Selivanov added the comment: I'll open another issue and will attach a patch. ---------- _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue14167> _______________________________________

Mark Dickinson added the comment: Yury: I don't think there's any need for yet another issue; this one will do (or if you prefer, attach the patch to issue 16028.) ---------- _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue14167> _______________________________________

Yury Selivanov added the comment: OK. Attaching the patch. Please review. ---------- Added file: http://bugs.python.org/file27280/finally_doc_patch.patch _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue14167> _______________________________________

Roundup Robot added the comment: New changeset 485902ecf0ee by Mark Dickinson in branch '3.2': Issue #14167: restore statement about breaks in finally clauses. http://hg.python.org/cpython/rev/485902ecf0ee New changeset f5ed3a5440b2 by Mark Dickinson in branch 'default': Issue #14167: merge fix from 3.2 branch. http://hg.python.org/cpython/rev/f5ed3a5440b2 ---------- _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue14167> _______________________________________

Mark Dickinson added the comment: Thanks for the quick fix! Patch applied to 3.2 and 3.3 The 2.7 commit also introduced an incorrect statement about exception chaining; I'll look at fixing that. ---------- _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue14167> _______________________________________

Yury Selivanov added the comment: thanks! ---------- _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue14167> _______________________________________

Roundup Robot added the comment: New changeset f51d11405f1d by Mark Dickinson in branch '2.7': Issue #14167: restore statement about breaks in finally clauses; remove statement about exception chaining. http://hg.python.org/cpython/rev/f51d11405f1d ---------- _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue14167> _______________________________________

Mark Dickinson added the comment: Reclosing. ---------- resolution: -> fixed status: open -> closed _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue14167> _______________________________________
participants (5)
-
Andrew Svetlov
-
Mark Dickinson
-
Roundup Robot
-
Yury Selivanov
-
Éric Araujo