Skip to content

Commit 8494967

Browse files
akihironittafacebook-github-bot
authored andcommittedSep 14, 2020
Fix exception chaining in test/ (pytorch#44193)
Summary: ## Motivation This PR fixes pytorch#43770 and is the continuation of pytorch#43836. ## Description of the change This PR fixes exception chaining only in files under `test/` where appropriate. To fix exception chaining, I used either: 1. `raise new_exception from old_exception` where `new_exception` itself seems not descriptive enough to debug or `old_exception` delivers valuable information. 2. `raise new_exception from None` where raising both of `new_exception` and `old_exception` seems a bit noisy and redundant. ## List of lines containing `raise` in `except` clause: I wrote [this simple script](https://gist.github.com/akihironitta/4223c1b32404b36c1b349d70c4c93b4d) using [ast](https://docs.python.org/3.8/library/ast.html#module-ast) to list lines where `raise`ing in `except` clause. - [x] https://github.com/pytorch/pytorch/blob/f8f35fddd4b2bd788953d6d6ccfadf690b73a3e8/test/test_cpp_extensions_aot.py#L16 - [x] https://github.com/pytorch/pytorch/blob/f8f35fddd4b2bd788953d6d6ccfadf690b73a3e8/test/test_jit.py#L2503 - [x] https://github.com/pytorch/pytorch/blob/f8f35fddd4b2bd788953d6d6ccfadf690b73a3e8/test/onnx/model_defs/word_language_model.py#L22 - [x] https://github.com/pytorch/pytorch/blob/f8f35fddd4b2bd788953d6d6ccfadf690b73a3e8/test/onnx/verify.py#L73 - [x] https://github.com/pytorch/pytorch/blob/f8f35fddd4b2bd788953d6d6ccfadf690b73a3e8/test/onnx/verify.py#L110 - [x] https://github.com/pytorch/pytorch/blob/f8f35fddd4b2bd788953d6d6ccfadf690b73a3e8/test/onnx/test_verify.py#L31 - [x] https://github.com/pytorch/pytorch/blob/f8f35fddd4b2bd788953d6d6ccfadf690b73a3e8/test/distributed/test_c10d.py#L255 - [x] https://github.com/pytorch/pytorch/blob/f8f35fddd4b2bd788953d6d6ccfadf690b73a3e8/test/distributed/test_c10d.py#L2992 - [x] https://github.com/pytorch/pytorch/blob/f8f35fddd4b2bd788953d6d6ccfadf690b73a3e8/test/distributed/test_c10d.py#L3025 - [x] https://github.com/pytorch/pytorch/blob/f8f35fddd4b2bd788953d6d6ccfadf690b73a3e8/test/distributed/test_c10d.py#L3712 - [x] https://github.com/pytorch/pytorch/blob/f8f35fddd4b2bd788953d6d6ccfadf690b73a3e8/test/distributed/test_distributed.py#L3180 - [x] https://github.com/pytorch/pytorch/blob/f8f35fddd4b2bd788953d6d6ccfadf690b73a3e8/test/distributed/test_distributed.py#L3198 - [x] https://github.com/pytorch/pytorch/blob/f8f35fddd4b2bd788953d6d6ccfadf690b73a3e8/test/distributed/test_data_parallel.py#L752 - [x] https://github.com/pytorch/pytorch/blob/f8f35fddd4b2bd788953d6d6ccfadf690b73a3e8/test/distributed/test_data_parallel.py#L776 - [x] https://github.com/pytorch/pytorch/blob/f8f35fddd4b2bd788953d6d6ccfadf690b73a3e8/test/test_type_hints.py#L151 - [x] https://github.com/pytorch/pytorch/blob/f8f35fddd4b2bd788953d6d6ccfadf690b73a3e8/test/test_jit_fuser.py#L771 - [x] https://github.com/pytorch/pytorch/blob/f8f35fddd4b2bd788953d6d6ccfadf690b73a3e8/test/test_jit_fuser.py#L773 - [x] https://github.com/pytorch/pytorch/blob/f8f35fddd4b2bd788953d6d6ccfadf690b73a3e8/test/test_dispatch.py#L105 - [x] https://github.com/pytorch/pytorch/blob/f8f35fddd4b2bd788953d6d6ccfadf690b73a3e8/test/test_distributions.py#L4738 - [x] https://github.com/pytorch/pytorch/blob/f8f35fddd4b2bd788953d6d6ccfadf690b73a3e8/test/test_nn.py#L9824 - [x] https://github.com/pytorch/pytorch/blob/f8f35fddd4b2bd788953d6d6ccfadf690b73a3e8/test/test_namedtensor.py#L843 - [x] https://github.com/pytorch/pytorch/blob/f8f35fddd4b2bd788953d6d6ccfadf690b73a3e8/test/test_jit_fuser_te.py#L875 - [x] https://github.com/pytorch/pytorch/blob/f8f35fddd4b2bd788953d6d6ccfadf690b73a3e8/test/test_jit_fuser_te.py#L877 - [x] https://github.com/pytorch/pytorch/blob/f8f35fddd4b2bd788953d6d6ccfadf690b73a3e8/test/test_dataloader.py#L31 - [x] https://github.com/pytorch/pytorch/blob/f8f35fddd4b2bd788953d6d6ccfadf690b73a3e8/test/test_dataloader.py#L43 - [x] https://github.com/pytorch/pytorch/blob/f8f35fddd4b2bd788953d6d6ccfadf690b73a3e8/test/test_dataloader.py#L365 - [x] https://github.com/pytorch/pytorch/blob/f8f35fddd4b2bd788953d6d6ccfadf690b73a3e8/test/test_dataloader.py#L391 Pull Request resolved: pytorch#44193 Reviewed By: albanD Differential Revision: D23681529 Pulled By: malfet fbshipit-source-id: 7c2256ff17334625081137b35baeb816c1e53e0b
1 parent a188dbd commit 8494967

9 files changed

+12
-12
lines changed
 

‎test/onnx/model_defs/word_language_model.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ def __init__(self, rnn_type, ntoken, ninp, nhid, nlayers,
2020
nonlinearity = {'RNN_TANH': 'tanh', 'RNN_RELU': 'relu'}[rnn_type]
2121
except KeyError:
2222
raise ValueError("""An invalid option for `--model` was supplied,
23-
options are ['LSTM', 'GRU', 'RNN_TANH' or 'RNN_RELU']""")
23+
options are ['LSTM', 'GRU', 'RNN_TANH' or 'RNN_RELU']""") from None
2424
self.rnn = nn.RNN(ninp, nhid, nlayers, nonlinearity=nonlinearity, dropout=dropout)
2525
self.decoder = nn.Linear(nhid, ntoken)
2626

‎test/test_cpp_extensions_aot.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@
1212
import torch_test_cpp_extension.cpp as cpp_extension
1313
import torch_test_cpp_extension.msnpu as msnpu_extension
1414
import torch_test_cpp_extension.rng as rng_extension
15-
except ImportError:
15+
except ImportError as e:
1616
raise RuntimeError(
1717
"test_cpp_extensions_aot.py cannot be invoked directly. Run "
1818
"`python run_test.py -i test_cpp_extensions_aot_ninja` instead."
19-
)
19+
) from e
2020

2121

2222
class TestCppExtensionAOT(common.TestCase):

‎test/test_dataloader.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
err_msg = ("psutil not found. Some critical data loader tests relying on it "
2929
"(e.g., TestDataLoader.test_proper_exit) will not run.")
3030
if IS_PYTORCH_CI:
31-
raise ImportError(err_msg)
31+
raise ImportError(err_msg) from None
3232
else:
3333
warnings.warn(err_msg)
3434

@@ -40,7 +40,7 @@
4040
err_msg = ("faulthandler not found. Some data loader tests use it for error "
4141
"reporting (e.g., TestDataLoader.test_proper_exit).")
4242
if IS_PYTORCH_CI:
43-
raise ImportError(err_msg)
43+
raise ImportError(err_msg) from None
4444
else:
4545
warnings.warn(err_msg)
4646

‎test/test_distributions.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -4733,9 +4733,9 @@ def test_invalid(self):
47334733
try:
47344734
with self.assertRaises(ValueError):
47354735
Dist(validate_args=True, **param)
4736-
except AssertionError:
4736+
except AssertionError as e:
47374737
fail_string = 'ValueError not raised for {} example {}/{}'
4738-
raise AssertionError(fail_string.format(Dist.__name__, i + 1, len(params)))
4738+
raise AssertionError(fail_string.format(Dist.__name__, i + 1, len(params))) from e
47394739

47404740
def tearDown(self):
47414741
super(TestValidation, self).tearDown()

‎test/test_jit.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -2526,7 +2526,7 @@ def test_pytorch_jit_env_off(self):
25262526
try:
25272527
subprocess.check_output([sys.executable, '-c', 'import torch'], env=env)
25282528
except subprocess.CalledProcessError as e:
2529-
raise RuntimeError("Could not 'import torch' with PYTORCH_JIT=0")
2529+
raise RuntimeError("Could not 'import torch' with PYTORCH_JIT=0") from e
25302530

25312531
def test_print_op_module(self):
25322532
# Issue #19351: python2 and python3 go through different paths.

‎test/test_jit_fuser.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -768,7 +768,7 @@ def test_lstm_traced_cpu(self):
768768
warnings.warn('CPU fuser test has failed! This is not a hard failure, '
769769
'because the kernels sometimes trigger bugs in compilers '
770770
'(most notably GCC 7.2).')
771-
raise unittest.SkipTest('Failed to compile')
771+
raise unittest.SkipTest('Failed to compile') from e
772772
else:
773773
raise
774774

‎test/test_jit_fuser_te.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -889,7 +889,7 @@ def test_lstm_traced_cpu(self):
889889
warnings.warn('CPU fuser test has failed! This is not a hard failure, '
890890
'because the kernels sometimes trigger bugs in compilers '
891891
'(most notably GCC 7.2).')
892-
raise unittest.SkipTest('Failed to compile')
892+
raise unittest.SkipTest('Failed to compile') from e
893893
else:
894894
raise
895895

‎test/test_namedtensor.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -840,7 +840,7 @@ def _test(testcase, names=('N', 'D'), device='cpu'):
840840
out = testcase.lambd(tensor)
841841
except RuntimeError as err:
842842
# Get a better error message by catching the error and asserting.
843-
raise RuntimeError('{}: {}'.format(testcase.name, err))
843+
raise RuntimeError('{}: {}'.format(testcase.name, err)) from err
844844
self.assertEqual(out.names, tensor.names,
845845
msg=testcase.name)
846846

‎test/test_type_hints.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ def test_doc_examples(self):
148148
target_is_directory=True
149149
)
150150
except OSError:
151-
raise unittest.SkipTest('cannot symlink')
151+
raise unittest.SkipTest('cannot symlink') from None
152152
(stdout, stderr, result) = mypy.api.run([
153153
'--follow-imports', 'silent',
154154
'--check-untyped-defs',

0 commit comments

Comments
 (0)