From 1bf57236945be879e5a307a3b802fe40af7409a7 Mon Sep 17 00:00:00 2001 From: Niels Thykier Date: Sat, 17 Feb 2018 09:15:45 +0000 Subject: [PATCH] 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 --- scour/scour.py | 4 ++-- testscour.py | 8 ++++---- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/scour/scour.py b/scour/scour.py index ef9c7be..b27cce5 100644 --- a/scour/scour.py +++ b/scour/scour.py @@ -56,7 +56,7 @@ import re import sys import time import xml.dom.minidom -from xml.dom import Node +from xml.dom import Node, NotFoundErr from collections import namedtuple from decimal import Context, Decimal, InvalidOperation, getcontext @@ -3619,7 +3619,7 @@ def scourXmlFile(filename, options=None): for node in all_nodes: try: node.setIdAttribute('id') - except: + except NotFoundErr: pass return doc diff --git a/testscour.py b/testscour.py index 50ad4e3..060b095 100755 --- a/testscour.py +++ b/testscour.py @@ -66,7 +66,7 @@ class EmptyOptions(unittest.TestCase): try: scourString(self.MINIMAL_SVG, options) fail = False - except: + except Exception: fail = True self.assertEqual(fail, False, 'Exception when calling "scourString" with empty options object') @@ -76,7 +76,7 @@ class EmptyOptions(unittest.TestCase): try: scourXmlFile('unittests/minimal.svg', options) fail = False - except: + except Exception: fail = True self.assertEqual(fail, False, 'Exception when calling "scourXmlFile" with empty options object') @@ -91,7 +91,7 @@ class EmptyOptions(unittest.TestCase): try: start(options, input, output) fail = False - except: + except Exception: fail = True sys.stdout = stdout_temp @@ -109,7 +109,7 @@ class InvalidOptions(unittest.TestCase): try: scourXmlFile('unittests/ids-to-strip.svg', options) fail = False - except: + except Exception: fail = True self.assertEqual(fail, False, 'Exception when calling Scour with invalid options')