This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: replace "except: pass" by "except Exception: pass"
Type: enhancement Stage: resolved
Components: Library (Lib) Versions: Python 3.5
process
Status: closed Resolution: duplicate
Dependencies: Superseder: Fix bare excepts in various places in std lib
View: 16261
Assigned To: rhettinger Nosy List: berker.peksag, martin.panter, matrixise, pitrou, rhettinger, serhiy.storchaka, terry.reedy, vstinner
Priority: normal Keywords: patch

Created on 2014-04-16 17:56 by matrixise, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
issue21259-replace_except_pass.patch matrixise, 2014-04-16 17:58 review
issue21259-replace_except_pass-2.patch matrixise, 2014-04-16 18:26 review
Messages (19)
msg216524 - (view) Author: Stéphane Wirtel (matrixise) * (Python committer) Date: 2014-04-16 17:56
Hi guys,

Via this issue, I will attach a patch where I replace all the "except: pass" in the source code by a clear "except Exception: pass".
msg216527 - (view) Author: Stéphane Wirtel (matrixise) * (Python committer) Date: 2014-04-16 17:58
I attach the patch. Need review.

Thanks
msg216528 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2014-04-16 18:01
You shouldn't change the grammar the tests, nor the pybench source.
Also, in some places it would probably be interesting to narrow down the exception type more.
msg216545 - (view) Author: Stéphane Wirtel (matrixise) * (Python committer) Date: 2014-04-16 18:26
I disabled the patch for pybench and the grammar.

Thanks for your review.
msg216570 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2014-04-16 19:52
FYI, the two are not equivalent.   A bare except is equivalent to BaseException.  Even then, the new code would be slower than the original, so I'm not sure this change should be made at all (it makes the code more verbose, slower, and risks introducing a semantic change).

Also, Guido traditionally tries to discourage across the board changes like this.  Instead, we aim for "holistic refactoring" where code improvements are being made one module at a time by someone who deeply groks the code and is thinking through all the changes.  The problem with across-the-board edits is that they tend to be very shallow "change thing x to thing y" without looking at what the surrounding code is doing or whether the code ever made sense in the first place.

Another part of the risk of semantic change is that we likely do not have specific tests for Exception vs BaseException so it would be easy to make a mistake and not have the test suite catch it.
msg216632 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2014-04-17 01:15
After more consideration, I'm going to reject the patch because it doesn't really make the code better and may make it worse (changing semantics, slower, etc).   Any such changes should be done holistically as part of a deeper refactoring on an individual module.  

I appreciate the patch effort but would like to avoid code churn for nearly zero benefit.
msg216636 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2014-04-17 01:37
> FYI, the two are not equivalent.

I don't get your point, the purpose of the change is to get ride of "except: pass" which is *bad*.

> Even then, the new code would be slower than the original,

I don't understand why you are talking about performances here. Ignore SystemExit and KeyboardInterrupt is a huge bug, performances don't matter here.

I don't want to benchmark, but I expect that performances are exactly the same if no exception is raised.

Please don't close the issue, Stéphane is fixing real bug.

I agree that it would be better to split the large patch is shorter parts. Or all changes replacing "except: pass" should be grouped into the same patch.

Replacing "except: <code>; raise" with "except Exception: <code>; raise" is wrong.
msg216638 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2014-04-17 01:53
> Please don't close the issue, Stéphane is fixing real bug.

He appears to be doing a blanket search and replace without adding tests, without evaluating each change to see if makes sense, and without consulting the original author of each affected piece of code.

We have never received a bug report on any one of these.  If you think any one of them is an actually bug, it should be carefully considered one at a time, evaluating why the bare except was put there in the first place.  

Guido pushes for holistic refactoring for a reason.  It is too easy to introduce bugs into stable code by these kind of wholesale changes.

If you (Victor) want to individually study each one to make sure it is the right thing to do, I would place more faith in the patch.  But as it stands now, reviewing the patch for correctness will take substantially more care and thought than it took to create it in the first place.
msg216640 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2014-04-17 02:05
I reviewed  issue21259-replace_except_pass-2.patch. Please split this huge patch into smaller patches:

1) remove dummy "except: raise"
2) use os.unlink()
3) replace "except: pass" with more precise exceptions like "except OSError:" => please write multiple small patches for this part
4) generic change "except: pass" to "except Exception: pass"

You might open a new issue for these 4 kind of patches (4 issues).
msg216641 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2014-04-17 02:09
> 3) replace "except: pass" with more precise exceptions like "except OSError:" => please write multiple small patches for this part

You may even open a new issue just for "except: pass" => "except OSError: pass"
msg216642 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2014-04-17 02:10
@Raymond: To give you more context, Stéphane is sprinting at Pycon on Pycon. I suggested him to fix all bare "except: pass". His first patch is wrong, but ignoring any exception is even worse.
msg216643 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2014-04-17 02:13
> If you (Victor) want to individually study each one to make sure it is the right thing to do, I would place more faith in the patch. But as it stands now, reviewing the patch for correctness will take substantially more care and thought than it took to create it in the first place.

Agreed. I would like to help Stéphane to fix these issues. I agree
that the huge patch must be splitted, I just explained how the patch
should be splitted and how Stéphane can provide more useful patches.
msg216646 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2014-04-17 03:02
Several areas for attention:
* Changes to the test suite probably should not be made
  The user doesn't benefit in any way and you risk
  unintentionally breaking the test suite in a way that
  isn't obvious (we have no tests for the tests themselves
  so changing tests is like refactoring without a safety net).
* Some of the exception handling in IDLE needs to catch
  all exceptions (i.e. SystemExit and KeyboardInterrupt
  are supposed to display tracebacks and not terminate
  IDLE itself).
* Changing the exceptions in threading.py is worrisome.
* The Pdb debugger may have legitimate reasons to catch
  all exceptions (like IDLE, we want don't want to 
  terminate the debugger itself).

In other words, many of the proposed changes should not
be made.  

For the rest, be careful to not change semantics
unintentionally and consider adding tests if you think
there is a real bug.   In cases where there is doubt
about the right thing to do, consider assigning the
original author or maintainer of the code.
msg216652 - (view) Author: Martin Panter (martin.panter) * (Python committer) Date: 2014-04-17 04:29
If you do go ahead and add “except Exception” clauses, maybe look around at what other exceptions are being handled. The other day I stumbled across this in “tkinter.font” module:

try:
    ...
except (KeyboardInterrupt, SystemExit):
    raise
except Exception:
    pass

It would have been simpler (and semantically equivalent) to write

try:
    ...
except Exception:
    pass
msg216699 - (view) Author: Berker Peksag (berker.peksag) * (Python committer) Date: 2014-04-17 15:51
This looks like a duplicate of issue 16261.
msg216811 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2014-04-18 22:07
This is definitely a duplicate of #16261 and should be closed at such.

On that issue, in msg202448, I explained that #15313 is about except: in idlelib and idlelib must be patched separately for the reason Raymond repeated (point 2). The other reason (point 1) is to keep different versions the same as much as possible.

I agree that bare excepts should eventually be replaced -- but on a case by case basis to the right specific exception. A bare except is easy to grep for. Once converted to something else, it is no longer visible as needing attention.
msg243652 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2015-05-20 07:39
Just committed smaller patch in issue16261 and closed the issue.

Not all changes in the patch in this issue look innocent and correct.
msg243680 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2015-05-20 16:54
A couple of years ago, when I pushed 'except: pass', I was told in post-review that grandfathered bad code is no excuse for more bad code and that I should be explicit, including if I actually meant "except BaseException:", which in this case I did.  No other developer said otherwise.  I took the above to be the general consensus. I agree with it, one reason being that bare excepts are speed bumps when reading someone else's code.

Victor> generic change "except: pass" to "except Exception: pass"
This is not correct without case-by-case examination.

#16261 had 2 patches.  The patch for doc examples changed 3 'except:'s to 'except Exception:'  I believe these are correct, or correct enough.  They all need to *not* catch KeyboardInterrupt.  The patch for lib code never changed to Exception, but something tighter.

The patch committed for #16261 patched 7 files (down from the original proposed 11).  Even that took a couple of years to get a second review.  I think further followup patches should probably change even fewer files and be attached to new, narrowly focused issues.

Raymond, since you closed this once, and since no new patch has been submitted, I presume you do not mind if I reclose this.
msg251055 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2015-09-19 06:36
> I presume you do not mind if I reclose this.

Thanks, please do reclose this.
History
Date User Action Args
2022-04-11 14:58:02adminsetgithub: 65458
2015-09-19 06:36:24rhettingersetmessages: + msg251055
2015-09-19 06:02:44martin.pantersetsuperseder: Fix bare excepts in various places in std lib
2015-05-20 16:54:48terry.reedysetstatus: open -> closed
type: enhancement
messages: + msg243680

resolution: duplicate
stage: resolved
2015-05-20 07:39:08serhiy.storchakasetnosy: + serhiy.storchaka
messages: + msg243652
2014-04-18 22:07:22terry.reedysetnosy: + terry.reedy
messages: + msg216811
2014-04-17 15:51:29berker.peksagsetnosy: + berker.peksag
messages: + msg216699
2014-04-17 04:29:37martin.pantersetnosy: + martin.panter
messages: + msg216652
2014-04-17 03:02:23rhettingersetmessages: + msg216646
2014-04-17 02:13:13vstinnersetmessages: + msg216643
2014-04-17 02:10:53vstinnersetmessages: + msg216642
2014-04-17 02:09:47vstinnersetmessages: + msg216641
2014-04-17 02:05:52vstinnersetmessages: + msg216640
2014-04-17 01:53:36rhettingersetmessages: + msg216638
2014-04-17 01:37:49vstinnersetstatus: closed -> open
resolution: rejected -> (no value)
messages: + msg216636
2014-04-17 01:15:03rhettingersetstatus: open -> closed
assignee: rhettinger
resolution: rejected
messages: + msg216632
2014-04-16 19:53:00rhettingersetnosy: + rhettinger
messages: + msg216570
2014-04-16 18:26:55matrixisesetfiles: + issue21259-replace_except_pass-2.patch

messages: + msg216545
2014-04-16 18:01:33pitrousetnosy: + pitrou
messages: + msg216528
2014-04-16 17:58:07matrixisesetfiles: + issue21259-replace_except_pass.patch

nosy: + vstinner
messages: + msg216527

keywords: + patch
2014-04-16 17:56:55matrixisecreate