From cc8744d6ca81c3b8ee0cd1a99cac607b2cb1f4c1 Mon Sep 17 00:00:00 2001 From: Wu Tingfeng Date: Thu, 5 Jan 2023 14:21:14 +0800 Subject: [PATCH] fix python 2 urllib/urllib2 compatibility --- update_plugins.py | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/update_plugins.py b/update_plugins.py index 47b33c3d..82d23845 100644 --- a/update_plugins.py +++ b/update_plugins.py @@ -2,15 +2,20 @@ try: import concurrent.futures as futures except ImportError: try: - import futures + import futures # type: ignore except ImportError: - futures = None + futures = None # type: ignore + +try: + from urllib.request import urlopen +except ImportError: + from urllib2 import urlopen # type: ignore import re import shutil import tempfile -import urllib.request import zipfile +from contextlib import closing from io import BytesIO from os import listdir, path @@ -73,7 +78,7 @@ SOURCE_DIR = path.join(path.dirname(__file__), "sources_non_forked_cache") def download_extract_replace(plugin_name, zip_path, temp_dir, source_dir): # Download and extract file in temp dir - with urllib.request.urlopen(zip_path) as req: + with closing(urlopen(zip_path)) as req: zip_f = zipfile.ZipFile(BytesIO(req.read())) zip_f.extractall(temp_dir) content_disp = req.headers.get("Content-Disposition")