Issue 9608: Re-phrase best way of using exceptions in doanddont.rst (original) (raw)

Created on 2010-08-15 12:23 by flub, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
doandont.diff flub,2010-08-15 12:23
doanddont-except.patch r.david.murray,2010-09-07 23:15
doanddont-except.patch r.david.murray,2010-09-11 02:43
Messages (9)
msg113949 - (view) Author: Floris Bruynooghe (flub) Date: 2010-08-15 12:23
The description of how to best use exceptions is slightly confusing and led me to believe there was an issue when using open() as a context manager. The main issue is that the wording seems to suggest the example above it is the best and not the very last. Attached is a patch which uses a slightly different wording which IMHO makes it clearer that the with-statement is the preferred method and does not introduce subtle bugs.
msg114433 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2010-08-20 17:38
Doc/howto/doanddont.rst is the source for Python HOWTOs: Idioms and Anti-Idioms in Python Moshe Zadka original author (added as nosy) The gist of the patch is to clarify that using 'with' is best, not the non-with version that is currently called both 'best' and 'not very good'. I agreed that the current version is confusing and the patch or something like it should be applied. Floris: 2.6 is closed to non-critical patches. 3.3 is for patches that cannot be applied sooner.
msg115819 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2010-09-07 23:15
Here is a more extensive rewrite that I think makes things clearer and (I hope) makes the text read better. I also updated the preceding section per the confusion expressed in issue 8518. Note that this patch is somewhat Python3 specific, since it assumes that all non-BaseException exceptions inherit from Exception. If someone wants to backport it to 2.7 a paragraph or footnote should be added about that problem. Opinions welcome.
msg115834 - (view) Author: Éric Araujo (eric.araujo) * (Python committer) Date: 2010-09-08 00:49
Your rewrite makes the text much clearer in my opinion. Here are some nits for you to grind, cheers :) 1) I’d say “using a bare ``except:``” and maybe add an index entry for “bare except” referring to that section, to make indexing and maybe googling find this. 2) To make the bad examples clearer, I would remove the second inline comment (“will be changed as soon as we run it”). The wording is confusing: Does that mean the code will be changed by Python at runtime? The text after this code example is clear enough, this comment doesn’t help. 3) “except catches all errors, including GeneratorExit which is not even an error”: Isn’t this confusing for beginners?
msg115839 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2010-09-08 01:16
(1) and (2) are good ideas. For (3), would it be clear enough if it read "``except:`` catches *all* exceptions, [...] and GeneratorExit (which is not an error and should not normally be caught by user code)."
msg115841 - (view) Author: Éric Araujo (eric.araujo) * (Python committer) Date: 2010-09-08 01:58
It would.
msg116073 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2010-09-11 02:43
Here is another edit pass, incorporating Éric's suggestions and adding some additional tweaks. In particular, I eliminated the anti-pattern of catching (IOError, OSError) in one of the earlier examples in favor of the correct EnvironmentError, and also improved the error message generated in that case :)
msg116114 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2010-09-11 18:59
Committed in r84719 and r84720, backported to 2.7 in r84721 with the addition of a sentence admitting that sometimes you need a bare except to catch third-party exceptions that don't inherit from Exception.
msg116140 - (view) Author: Éric Araujo (eric.araujo) * (Python committer) Date: 2010-09-12 00:27
Nice, thanks!
History
Date User Action Args
2022-04-11 14:57:05 admin set github: 53817
2010-09-12 00:28:01 eric.araujo set status: open -> closedresolution: fixedstage: patch review -> resolved
2010-09-12 00:27:32 eric.araujo set status: closed -> openresolution: fixed -> (no value)messages: + stage: resolved -> patch review
2010-09-11 18:59:11 r.david.murray set status: open -> closedresolution: fixedmessages: + stage: patch review -> resolved
2010-09-11 02:43:56 r.david.murray set files: + doanddont-except.patchmessages: +
2010-09-08 01:58:29 eric.araujo set messages: +
2010-09-08 01:16:40 r.david.murray set messages: +
2010-09-08 00:49:54 eric.araujo set nosy: + eric.araujomessages: +
2010-09-07 23:15:05 r.david.murray set files: + doanddont-except.patchnosy: + r.david.murraymessages: +
2010-09-07 23:06:19 r.david.murray link issue8518 superseder
2010-08-20 17:39:00 terry.reedy set versions: - Python 2.6, Python 3.3nosy: + moshez, terry.reedymessages: + type: enhancement -> behaviorstage: patch review
2010-08-15 12:23:44 flub create