pytorch
fe80f190 - use context manager for path extension in torch.hub (#75786)

Commit
2 years ago
use context manager for path extension in torch.hub (#75786) We are using the idiom ```py sys.path.insert(0, path) # do something sys.path.remove(path) ``` three times in `torch.hub`. This is a textbook case for using a context manager. In addition, by using `try` / `finally` we can enforce the Python path is back in its original state even if the actual action raises an exception: ```py import sys path = "/tmp" # PR try: sys.path.insert(0, path) try: # Any exception raised while performing the actual functionality raise Exception finally: sys.path.remove(path) except Exception: assert path not in sys.path # main try: sys.path.insert(0, path) # Any exception raised while performing the actual functionality raise Exception sys.path.remove(path) except Exception: assert path in sys.path ``` Pull Request resolved: https://github.com/pytorch/pytorch/pull/75786 Approved by: https://github.com/NicolasHug
Author
Committer
Parents
Loading