|
56 | 56 |
|
57 | 57 | nib, _ = optional_import("nibabel") |
58 | 58 | http_error, has_req = optional_import("requests", name="HTTPError") |
| 59 | +file_url_error, has_gdown = optional_import("gdown.exceptions", name="FileURLRetrievalError") |
| 60 | + |
59 | 61 |
|
60 | 62 | quick_test_var = "QUICKTEST" |
61 | 63 | _tf32_enabled = None |
62 | 64 | _test_data_config: dict = {} |
63 | 65 |
|
64 | 66 | MODULE_PATH = Path(__file__).resolve().parents[1] |
65 | 67 |
|
| 68 | +DOWNLOAD_EXCEPTS: tuple[type, ...] = (ContentTooShortError, HTTPError, ConnectionError) |
| 69 | +if has_req: |
| 70 | + DOWNLOAD_EXCEPTS += (http_error,) |
| 71 | +if has_gdown: |
| 72 | + DOWNLOAD_EXCEPTS += (file_url_error,) |
| 73 | + |
| 74 | +DOWNLOAD_FAIL_MSGS = ( |
| 75 | + "unexpected EOF", # incomplete download |
| 76 | + "network issue", |
| 77 | + "gdown dependency", # gdown not installed |
| 78 | + "md5 check", |
| 79 | + "limit", # HTTP Error 503: Egress is over the account limit |
| 80 | + "authenticate", |
| 81 | + "timed out", # urlopen error [Errno 110] Connection timed out |
| 82 | + "HTTPError", # HTTPError: 429 Client Error: Too Many Requests for huggingface hub |
| 83 | +) |
| 84 | + |
66 | 85 |
|
67 | 86 | def testing_data_config(*keys): |
68 | 87 | """get _test_data_config[keys0][keys1]...[keysN]""" |
@@ -142,29 +161,21 @@ def assert_allclose( |
142 | 161 |
|
143 | 162 | @contextmanager |
144 | 163 | def skip_if_downloading_fails(): |
| 164 | + """ |
| 165 | + Skips a test if downloading something raises an exception recognised to indicate a download has failed. |
| 166 | + """ |
| 167 | + |
145 | 168 | try: |
146 | 169 | yield |
147 | | - except (ContentTooShortError, HTTPError, ConnectionError) + (http_error,) if has_req else () as e: # noqa: B030 |
148 | | - raise unittest.SkipTest(f"error while downloading: {e}") from e |
| 170 | + except DOWNLOAD_EXCEPTS as e: |
| 171 | + raise unittest.SkipTest(f"Error while downloading: {e}") from e |
149 | 172 | except ssl.SSLError as ssl_e: |
150 | 173 | if "decryption failed" in str(ssl_e): |
151 | 174 | raise unittest.SkipTest(f"SSL error while downloading: {ssl_e}") from ssl_e |
152 | 175 | except (RuntimeError, OSError) as rt_e: |
153 | 176 | err_str = str(rt_e) |
154 | | - if any( |
155 | | - k in err_str |
156 | | - for k in ( |
157 | | - "unexpected EOF", # incomplete download |
158 | | - "network issue", |
159 | | - "gdown dependency", # gdown not installed |
160 | | - "md5 check", |
161 | | - "limit", # HTTP Error 503: Egress is over the account limit |
162 | | - "authenticate", |
163 | | - "timed out", # urlopen error [Errno 110] Connection timed out |
164 | | - "HTTPError", # HTTPError: 429 Client Error: Too Many Requests for huggingface hub |
165 | | - ) |
166 | | - ): |
167 | | - raise unittest.SkipTest(f"error while downloading: {rt_e}") from rt_e # incomplete download |
| 177 | + if any(k in err_str for k in DOWNLOAD_FAIL_MSGS): |
| 178 | + raise unittest.SkipTest(f"Error while downloading: {rt_e}") from rt_e # incomplete download |
168 | 179 |
|
169 | 180 | raise rt_e |
170 | 181 |
|
|
0 commit comments