Preventing crashing incase of no network by loading from cache (#91569)

Fixes #91568

Pull Request resolved: https://github.com/pytorch/pytorch/pull/91569
Approved by: https://github.com/NicolasHug
This commit is contained in:
MohammedZ666
2023-01-11 11:56:46 +00:00
committed by PyTorch MergeBot
parent 850cf8949a
commit 84b819d083

View File

@ -12,7 +12,7 @@ import warnings
import zipfile
from pathlib import Path
from typing import Dict, Optional, Any
from urllib.error import HTTPError
from urllib.error import HTTPError, URLError
from urllib.request import urlopen, Request
from urllib.parse import urlparse # noqa: F401
from torch.serialization import MAP_LOCATION
@ -146,6 +146,17 @@ def _parse_repo_info(github):
ref = 'master'
else:
raise
except URLError as e:
# No internet connection, need to check for cache as last resort
for possible_ref in ("main", "master"):
if os.path.exists(f"{get_dir()}/{repo_owner}_{repo_name}_{possible_ref}"):
ref = possible_ref
break
if ref is None:
raise RuntimeError(
"It looks like there is no internet connection and the "
f"repo could not be found in the cache ({get_dir()})"
) from e
return repo_owner, repo_name, ref