diff --git a/tests/__init__.py b/tests/__init__.py index 9a5866aac..7fb0b3694 100644 --- a/tests/__init__.py +++ b/tests/__init__.py @@ -2,6 +2,7 @@ import ssl import urllib.request from typing import List +from urllib.error import HTTPError from pypdf.generic import DictionaryObject, IndirectObject @@ -30,10 +31,19 @@ def get_pdf_from_url(url: str, name: str) -> bytes: cache_path = os.path.join(cache_dir, name) if not os.path.exists(cache_path): ssl._create_default_https_context = ssl._create_unverified_context - with urllib.request.urlopen(url) as response, open( - cache_path, "wb" - ) as out_file: - out_file.write(response.read()) + cpt = 3 + while cpt > 0: + try: + with urllib.request.urlopen(url) as response, open( + cache_path, "wb" + ) as out_file: + out_file.write(response.read()) + cpt = 0 + except HTTPError as e: + if cpt > 0: + cpt -= 1 + else: + raise e with open(cache_path, "rb") as fp: data = fp.read() return data