Files
pytorch/torch/_utils_internal.py
T
Akihiro Nitta f17d7a5556 Fix exception chaining in torch/ (#43836)
Summary:
## Motivation
Fixes https://github.com/pytorch/pytorch/issues/43770.

## Description of the change
This PR fixes exception chaining only in files under `torch/` 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.
I subjectively chose which one to use from the above options.

## 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/000739c31ad34909e64124e0d39b2f49249458e9/torch/jit/annotations.py#L35
- [x] https://github.com/pytorch/pytorch/blob/000739c31ad34909e64124e0d39b2f49249458e9/torch/jit/annotations.py#L150
- [x] https://github.com/pytorch/pytorch/blob/000739c31ad34909e64124e0d39b2f49249458e9/torch/jit/annotations.py#L158
- [x] https://github.com/pytorch/pytorch/blob/000739c31ad34909e64124e0d39b2f49249458e9/torch/jit/annotations.py#L231
- [x] https://github.com/pytorch/pytorch/blob/000739c31ad34909e64124e0d39b2f49249458e9/torch/jit/_trace.py#L432
- [x] https://github.com/pytorch/pytorch/blob/000739c31ad34909e64124e0d39b2f49249458e9/torch/nn/utils/prune.py#L192
- [x] https://github.com/pytorch/pytorch/blob/000739c31ad34909e64124e0d39b2f49249458e9/torch/cuda/nvtx.py#L7
- [x] https://github.com/pytorch/pytorch/blob/000739c31ad34909e64124e0d39b2f49249458e9/torch/utils/cpp_extension.py#L1537
- [x] https://github.com/pytorch/pytorch/blob/000739c31ad34909e64124e0d39b2f49249458e9/torch/utils/tensorboard/_pytorch_graph.py#L292
- [x] https://github.com/pytorch/pytorch/blob/000739c31ad34909e64124e0d39b2f49249458e9/torch/utils/data/dataloader.py#L835
- [x] https://github.com/pytorch/pytorch/blob/000739c31ad34909e64124e0d39b2f49249458e9/torch/utils/data/dataloader.py#L849
- [x] https://github.com/pytorch/pytorch/blob/000739c31ad34909e64124e0d39b2f49249458e9/torch/utils/data/dataloader.py#L856
- [x] https://github.com/pytorch/pytorch/blob/000739c31ad34909e64124e0d39b2f49249458e9/torch/testing/_internal/common_utils.py#L186
- [x] https://github.com/pytorch/pytorch/blob/000739c31ad34909e64124e0d39b2f49249458e9/torch/testing/_internal/common_utils.py#L189
- [x] https://github.com/pytorch/pytorch/blob/000739c31ad34909e64124e0d39b2f49249458e9/torch/testing/_internal/common_utils.py#L424
- [x] https://github.com/pytorch/pytorch/blob/000739c31ad34909e64124e0d39b2f49249458e9/torch/testing/_internal/common_utils.py#L1279
- [x] https://github.com/pytorch/pytorch/blob/000739c31ad34909e64124e0d39b2f49249458e9/torch/testing/_internal/common_utils.py#L1283
- [x] https://github.com/pytorch/pytorch/blob/000739c31ad34909e64124e0d39b2f49249458e9/torch/testing/_internal/common_utils.py#L1356
- [x] https://github.com/pytorch/pytorch/blob/000739c31ad34909e64124e0d39b2f49249458e9/torch/testing/_internal/common_utils.py#L1388
- [x] https://github.com/pytorch/pytorch/blob/000739c31ad34909e64124e0d39b2f49249458e9/torch/testing/_internal/common_utils.py#L1391
- [ ] https://github.com/pytorch/pytorch/blob/000739c31ad34909e64124e0d39b2f49249458e9/torch/testing/_internal/common_utils.py#L1412
- [x] https://github.com/pytorch/pytorch/blob/000739c31ad34909e64124e0d39b2f49249458e9/torch/testing/_internal/codegen/random_topo_test.py#L310
- [x] https://github.com/pytorch/pytorch/blob/000739c31ad34909e64124e0d39b2f49249458e9/torch/testing/_internal/codegen/random_topo_test.py#L329
- [x] https://github.com/pytorch/pytorch/blob/000739c31ad34909e64124e0d39b2f49249458e9/torch/testing/_internal/codegen/random_topo_test.py#L332
- [x] https://github.com/pytorch/pytorch/blob/000739c31ad34909e64124e0d39b2f49249458e9/torch/testing/_internal/jit_utils.py#L183
- [x] https://github.com/pytorch/pytorch/blob/000739c31ad34909e64124e0d39b2f49249458e9/torch/testing/_internal/common_nn.py#L4789
- [x] https://github.com/pytorch/pytorch/blob/000739c31ad34909e64124e0d39b2f49249458e9/torch/onnx/utils.py#L367
- [x] https://github.com/pytorch/pytorch/blob/000739c31ad34909e64124e0d39b2f49249458e9/torch/onnx/utils.py#L659
- [x] https://github.com/pytorch/pytorch/blob/000739c31ad34909e64124e0d39b2f49249458e9/torch/onnx/utils.py#L892
- [x] https://github.com/pytorch/pytorch/blob/000739c31ad34909e64124e0d39b2f49249458e9/torch/onnx/utils.py#L897
- [x] https://github.com/pytorch/pytorch/blob/000739c31ad34909e64124e0d39b2f49249458e9/torch/serialization.py#L108
- [x] https://github.com/pytorch/pytorch/blob/000739c31ad34909e64124e0d39b2f49249458e9/torch/serialization.py#L754
- [x] https://github.com/pytorch/pytorch/blob/000739c31ad34909e64124e0d39b2f49249458e9/torch/distributed/rpc/_testing/faulty_agent_backend_registry.py#L76
- [x] https://github.com/pytorch/pytorch/blob/000739c31ad34909e64124e0d39b2f49249458e9/torch/distributed/rpc/backend_registry.py#L260
- [x] https://github.com/pytorch/pytorch/blob/000739c31ad34909e64124e0d39b2f49249458e9/torch/distributed/distributed_c10d.py#L184
- [x] https://github.com/pytorch/pytorch/blob/000739c31ad34909e64124e0d39b2f49249458e9/torch/_utils_internal.py#L57
- [x] https://github.com/pytorch/pytorch/blob/000739c31ad34909e64124e0d39b2f49249458e9/torch/hub.py#L494
- [x] https://github.com/pytorch/pytorch/blob/000739c31ad34909e64124e0d39b2f49249458e9/torch/contrib/_tensorboard_vis.py#L16
- [x] https://github.com/pytorch/pytorch/blob/000739c31ad34909e64124e0d39b2f49249458e9/torch/distributions/lowrank_multivariate_normal.py#L100
- [x] https://github.com/pytorch/pytorch/blob/000739c31ad34909e64124e0d39b2f49249458e9/torch/distributions/constraint_registry.py#L142

Pull Request resolved: https://github.com/pytorch/pytorch/pull/43836

Reviewed By: ailzhang

Differential Revision: D23431212

Pulled By: malfet

fbshipit-source-id: 5f7f41b391164a5ad0efc06e55cd58c23408a921
2020-08-31 20:26:23 -07:00

70 lines
2.0 KiB
Python

from __future__ import absolute_import, division, print_function, unicode_literals
import os
import inspect
import tempfile
# this arbitrary-looking assortment of functionality is provided here
# to have a central place for overrideable behavior. The motivating
# use is the FB build environment, where this source file is replaced
# by an equivalent.
if os.path.basename(os.path.dirname(__file__)) == 'shared':
torch_parent = os.path.dirname(os.path.dirname(os.path.dirname(__file__)))
else:
torch_parent = os.path.dirname(os.path.dirname(__file__))
def get_file_path(*path_components):
return os.path.join(torch_parent, *path_components)
def get_file_path_2(*path_components):
return os.path.join(*path_components)
def get_writable_path(path):
if os.access(path, os.W_OK):
return path
return tempfile.mkdtemp(suffix=os.path.basename(path))
def prepare_multiprocessing_environment(path):
pass
def resolve_library_path(path):
return os.path.realpath(path)
def get_source_lines_and_file(obj, error_msg=None):
"""
Wrapper around inspect.getsourcelines and inspect.getsourcefile.
Returns: (sourcelines, file_lino, filename)
"""
filename = None # in case getsourcefile throws
try:
filename = inspect.getsourcefile(obj)
sourcelines, file_lineno = inspect.getsourcelines(obj)
except OSError as e:
msg = (f"Can't get source for {obj}. TorchScript requires source access in "
"order to carry out compilation, make sure original .py files are "
"available.")
if error_msg:
msg += '\n' + error_msg
raise OSError(msg) from e
return sourcelines, file_lineno, filename
TEST_MASTER_ADDR = '127.0.0.1'
TEST_MASTER_PORT = 29500
# USE_GLOBAL_DEPS controls whether __init__.py tries to load
# libtorch_global_deps, see Note [Global dependencies]
USE_GLOBAL_DEPS = True
# USE_RTLD_GLOBAL_WITH_LIBTORCH controls whether __init__.py tries to load
# _C.so with RTLD_GLOBAL during the call to dlopen.
USE_RTLD_GLOBAL_WITH_LIBTORCH = False