]> granicus.if.org Git - zziplib/commitdiff
test: urllib compatibility with Python 3
authorPatrick Steinhardt <ps@pks.im>
Wed, 22 May 2019 19:20:52 +0000 (21:20 +0200)
committerPatrick Steinhardt <ps@pks.im>
Thu, 1 Aug 2019 06:35:33 +0000 (08:35 +0200)
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

index 627d5ad22aa67b5f1d4124de3f9c9894af6d0fda..49f43f6c3b2c28aa1711be0d6c0316d5b2b82e9c 100644 (file)
@@ -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)