diff --git a/spacy/cli/download.py b/spacy/cli/download.py index 977545dfe..71c1a65dd 100644 --- a/spacy/cli/download.py +++ b/spacy/cli/download.py @@ -9,7 +9,7 @@ import ujson from .link import link from ..util import prints, get_package_path -from ..compat import url_open, url_error +from ..compat import url_read, HTTPError from .. import about @@ -58,13 +58,13 @@ def download(model, direct=False): def get_json(url, desc): try: - r = url_open(url) - except url_error as e: + data = url_read(url) + except HTTPError as e: msg = ("Couldn't fetch %s. Please find a model for your spaCy " "installation (v%s), and download it manually.") prints(msg % (desc, about.__version__), about.__docs_models__, title="Server error (%d: %s)" % (e.code, e.reason), exits=1) - return ujson.load(r) + return ujson.loads(data) def get_compatibility(): diff --git a/spacy/cli/validate.py b/spacy/cli/validate.py index 2b7388988..c140f6bff 100644 --- a/spacy/cli/validate.py +++ b/spacy/cli/validate.py @@ -6,7 +6,7 @@ from pathlib import Path import sys import ujson -from ..compat import path2str, locale_escape, url_open, url_error +from ..compat import path2str, locale_escape, url_read, HTTPError from ..util import prints, get_data_path, read_json from .. import about @@ -16,11 +16,11 @@ def validate(): with the installed models. Should be run after `pip install -U spacy`. """ try: - r = url_open(about.__compatibility__) - except url_error as e: + data = url_read(about.__compatibility__) + except HTTPError as e: prints("Couldn't fetch compatibility table.", title="Server error (%d: %s)" % (e.code, e.reason), exits=1) - compat = ujson.load(r)['spacy'] + compat = ujson.loads(data)['spacy'] current_compat = compat.get(about.__version__) if not current_compat: prints(about.__compatibility__, exits=1, diff --git a/spacy/compat.py b/spacy/compat.py index 5359edefc..d6260a850 100644 --- a/spacy/compat.py +++ b/spacy/compat.py @@ -39,9 +39,9 @@ except ImportError: import urllib2 as urllib try: - from urllib.error import HTTPError as url_error + from urllib.error import HTTPError except ImportError: - from urllib2 import HTTPError as url_error + from urllib2 import HTTPError pickle = pickle copy_reg = copy_reg @@ -49,7 +49,6 @@ CudaStream = CudaStream cupy = cupy copy_array = copy_array urllib = urllib -url_error = url_error izip = getattr(itertools, 'izip', zip) is_windows = sys.platform.startswith('win') @@ -68,7 +67,7 @@ if is_python2: input_ = raw_input # noqa: F821 json_dumps = lambda data: ujson.dumps(data, indent=2, escape_forward_slashes=False).decode('utf8') path2str = lambda path: str(path).decode('utf8') - url_open = lambda url: urllib.urlopen(url) + url_open = urllib.urlopen elif is_python3: bytes_ = bytes @@ -77,7 +76,16 @@ elif is_python3: input_ = input json_dumps = lambda data: ujson.dumps(data, indent=2, escape_forward_slashes=False) path2str = lambda path: str(path) - url_open = lambda url: urllib.request.urlopen(url) + url_open = urllib.request.urlopen + + +def url_read(url): + file_ = url_open(url) + code = file_.getcode() + if code != 200: + raise HTTPError(url, code, "Cannot GET url", [], file_) + data = file_.read() + return data def b_to_str(b_str):