From a3d3dc9d51a78addc5356922ae707d15857d3661 Mon Sep 17 00:00:00 2001 From: Patrick Steinhardt Date: Wed, 22 May 2019 21:20:52 +0200 Subject: [PATCH] test: urllib compatibility with Python 3 In Python 3, the functions `quote_plus` and `urlretrieve` have been moved from urllib to urllib.parse and urllib.request, respectively. To provide compatibility with the old and new module layouts, use a try-catch block and fall back to the new layout if an ImportError was raised. Furthermore, do not hand-code `urlretrieve` functionality with `urlopen` to avoid additional compatibility issues. --- test/zziptests.py | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/test/zziptests.py b/test/zziptests.py index 627d5ad..49f43f6 100644 --- a/test/zziptests.py +++ b/test/zziptests.py @@ -4,13 +4,18 @@ import logging import inspect import os import collections -import urllib import shutil import random import re from fnmatch import fnmatchcase as matches from cStringIO import StringIO +try: + from urllib import quote_plus, urlretrieve +except ImportError: + from urllib.parse import quote_plus + from urllib.request import urlretrieve + logg = logging.getLogger("test") topsrcdir = "../.." @@ -106,7 +111,7 @@ def download(base_url, filename, into, style = ""): data = "tmp.download" if not os.path.isdir(data): os.makedirs(data) - subname = urllib.quote_plus(base_url) + subname = quote_plus(base_url) subdir = os.path.join(data, subname) if not os.path.isdir(subdir): os.makedirs(subdir) @@ -118,10 +123,13 @@ def download(base_url, filename, into, style = ""): shutil.copy(srcfile, subfile) if not os.path.exists(subfile): logg.info("need %s", subfile) - d = urllib.urlopen(base_url + "/" + filename + style) - f = open(subfile, "w") - f.write(d.read()) - f.close() + try: + urlretrieve(base_url + "/" + filename + style, subfile) + except: + # Ensure zero-length file exists in case we couldn't + # download the file so that we won't try to + # re-download it. + open(subfile, 'a').close() # if not os.path.isdir(into): os.makedirs(into) -- 2.40.0