Catch specific exception rather than anything

The bare "except" also catches exceptions like "NameError" and
"SystemExit", which we really should not catch.  In scour.py, use the
most specific exception (NotFoundErr) and in the tests just catch any
"regular" exception.

Reported by flake8.

Signed-off-by: Niels Thykier <niels@thykier.net>
This commit is contained in:
Niels Thykier 2018-02-17 09:15:45 +00:00
parent 57a6da7774
commit 1bf5723694
2 changed files with 6 additions and 6 deletions

View file

@ -56,7 +56,7 @@ import re
import sys import sys
import time import time
import xml.dom.minidom import xml.dom.minidom
from xml.dom import Node from xml.dom import Node, NotFoundErr
from collections import namedtuple from collections import namedtuple
from decimal import Context, Decimal, InvalidOperation, getcontext from decimal import Context, Decimal, InvalidOperation, getcontext
@ -3619,7 +3619,7 @@ def scourXmlFile(filename, options=None):
for node in all_nodes: for node in all_nodes:
try: try:
node.setIdAttribute('id') node.setIdAttribute('id')
except: except NotFoundErr:
pass pass
return doc return doc

View file

@ -66,7 +66,7 @@ class EmptyOptions(unittest.TestCase):
try: try:
scourString(self.MINIMAL_SVG, options) scourString(self.MINIMAL_SVG, options)
fail = False fail = False
except: except Exception:
fail = True fail = True
self.assertEqual(fail, False, self.assertEqual(fail, False,
'Exception when calling "scourString" with empty options object') 'Exception when calling "scourString" with empty options object')
@ -76,7 +76,7 @@ class EmptyOptions(unittest.TestCase):
try: try:
scourXmlFile('unittests/minimal.svg', options) scourXmlFile('unittests/minimal.svg', options)
fail = False fail = False
except: except Exception:
fail = True fail = True
self.assertEqual(fail, False, self.assertEqual(fail, False,
'Exception when calling "scourXmlFile" with empty options object') 'Exception when calling "scourXmlFile" with empty options object')
@ -91,7 +91,7 @@ class EmptyOptions(unittest.TestCase):
try: try:
start(options, input, output) start(options, input, output)
fail = False fail = False
except: except Exception:
fail = True fail = True
sys.stdout = stdout_temp sys.stdout = stdout_temp
@ -109,7 +109,7 @@ class InvalidOptions(unittest.TestCase):
try: try:
scourXmlFile('unittests/ids-to-strip.svg', options) scourXmlFile('unittests/ids-to-strip.svg', options)
fail = False fail = False
except: except Exception:
fail = True fail = True
self.assertEqual(fail, False, self.assertEqual(fail, False,
'Exception when calling Scour with invalid options') 'Exception when calling Scour with invalid options')