From 8e58135a263dfacfb44ce1032251df3c12eb6929 Mon Sep 17 00:00:00 2001 From: Sam Gross Date: Mon, 23 Oct 2017 23:03:37 -0400 Subject: [PATCH] Fix E722 ('do not use bare except') (#3239) The new version of flake8 includes a check for not using bare except. We should avoid this since it catches things like KeyboardInterrupt. --- test/common.py | 6 +++--- torch/__init__.py | 2 +- torch/nn/modules/module.py | 2 +- torch/serialization.py | 2 +- torch/utils/data/dataloader.py | 2 +- tox.ini | 2 +- 6 files changed, 8 insertions(+), 8 deletions(-) diff --git a/test/common.py b/test/common.py index 2fe8e12f572..5f784220527 100644 --- a/test/common.py +++ b/test/common.py @@ -116,7 +116,7 @@ def is_iterable(obj): try: iter(obj) return True - except: + except TypeError: return False @@ -210,7 +210,7 @@ class TestCase(unittest.TestCase): try: self.assertLessEqual(abs(x - y), prec, message) return - except: + except (TypeError, AssertionError): pass super(TestCase, self).assertEqual(x, y, message) @@ -242,7 +242,7 @@ class TestCase(unittest.TestCase): try: self.assertGreaterEqual(abs(x - y), prec, message) return - except: + except (TypeError, AssertionError): pass super(TestCase, self).assertNotEqual(x, y, message) diff --git a/torch/__init__.py b/torch/__init__.py index f61b40ae877..51afa5e709d 100644 --- a/torch/__init__.py +++ b/torch/__init__.py @@ -35,7 +35,7 @@ import os as _dl_flags # or there is risk that later c modules will segfault when importing numpy try: import numpy as np -except: +except ImportError: pass # first check if the os package has the required flags diff --git a/torch/nn/modules/module.py b/torch/nn/modules/module.py index a889ef4be77..669f0a639ed 100644 --- a/torch/nn/modules/module.py +++ b/torch/nn/modules/module.py @@ -404,7 +404,7 @@ class Module(object): param = param.data try: own_state[name].copy_(param) - except: + except Exception: raise RuntimeError('While copying the parameter named {}, ' + 'whose dimensions in the model are {} and ' + 'whose dimensions in the checkpoint are {}.' diff --git a/torch/serialization.py b/torch/serialization.py index 68aa6b818de..22b87676382 100644 --- a/torch/serialization.py +++ b/torch/serialization.py @@ -150,7 +150,7 @@ def _save(obj, f, pickle_module, pickle_protocol): try: source_file = inspect.getsourcefile(obj) source = inspect.getsource(obj) - except: # saving the source is optional, so we can ignore any errors + except Exception: # saving the source is optional, so we can ignore any errors warnings.warn("Couldn't retrieve source code for container of " "type " + obj.__name__ + ". It won't be checked " "for correctness upon loading.") diff --git a/torch/utils/data/dataloader.py b/torch/utils/data/dataloader.py index f4946abbdba..54008b759d0 100644 --- a/torch/utils/data/dataloader.py +++ b/torch/utils/data/dataloader.py @@ -49,7 +49,7 @@ def _pin_memory_loop(in_queue, out_queue, done_event): while True: try: r = in_queue.get() - except: + except Exception: if done_event.is_set(): return raise diff --git a/tox.ini b/tox.ini index 252ecdcc22c..793ccc2c3b2 100644 --- a/tox.ini +++ b/tox.ini @@ -1,4 +1,4 @@ [flake8] max-line-length = 120 -ignore = E305,E402,E721,E722,E741,F401,F403,F405,F821,F841,F999 +ignore = E305,E402,E721,E741,F401,F403,F405,F821,F841,F999 exclude = docs/src,venv,torch/lib/gloo,torch/lib/pybind11,torch/lib/nanopb