Skip to content

Commit 4bf3e32

Browse files
authored
TST : Add multiple retry on get_url (#1626)
This reduces CI fails due to network issues
1 parent 7a807d2 commit 4bf3e32

File tree

1 file changed

+14
-4
lines changed

1 file changed

+14
-4
lines changed

tests/__init__.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import ssl
33
import urllib.request
44
from typing import List
5+
from urllib.error import HTTPError
56

67
from pypdf.generic import DictionaryObject, IndirectObject
78

@@ -30,10 +31,19 @@ def get_pdf_from_url(url: str, name: str) -> bytes:
3031
cache_path = os.path.join(cache_dir, name)
3132
if not os.path.exists(cache_path):
3233
ssl._create_default_https_context = ssl._create_unverified_context
33-
with urllib.request.urlopen(url) as response, open(
34-
cache_path, "wb"
35-
) as out_file:
36-
out_file.write(response.read())
34+
cpt = 3
35+
while cpt > 0:
36+
try:
37+
with urllib.request.urlopen(url) as response, open(
38+
cache_path, "wb"
39+
) as out_file:
40+
out_file.write(response.read())
41+
cpt = 0
42+
except HTTPError as e:
43+
if cpt > 0:
44+
cpt -= 1
45+
else:
46+
raise e
3747
with open(cache_path, "rb") as fp:
3848
data = fp.read()
3949
return data

0 commit comments

Comments
 (0)