Skip to content

Commit 8f974cb

Browse files
committed
fix urllib imports
1 parent eac252f commit 8f974cb

File tree

2 files changed

+21
-9
lines changed

2 files changed

+21
-9
lines changed

bioformats/formatreader.py

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,15 @@
3232
import numpy as np
3333
import os
3434
import sys
35-
import urllib
36-
import urllib2
35+
36+
if sys.version_info.major == 3:
37+
from urllib.request import urlopen, urlparse, url2pathname
38+
from urllib.parse import unquote
39+
else:
40+
from urllib import url2pathname
41+
from urllib2 import urlopen, urlparse, unquote
42+
urlparse = urlparse.urlparse
43+
3744
import shutil
3845
import tempfile
3946
import traceback
@@ -546,7 +553,7 @@ def __init__(self, path=None, url=None, perform_init=True):
546553
self.url = url
547554
self.using_temp_file = False
548555
if url is not None and url.lower().startswith(file_scheme):
549-
utf8_url = urllib.url2pathname(url[len(file_scheme):])
556+
utf8_url = url2pathname(url[len(file_scheme):])
550557
if isinstance(utf8_url, str):
551558
path = str(utf8_url, 'utf-8')
552559
else:
@@ -593,7 +600,7 @@ def __init__(self, path=None, url=None, perform_init=True):
593600
# Other URLS, copy them to a tempfile location
594601
#
595602
ext = url[url.rfind("."):]
596-
src = urllib2.urlopen(url)
603+
src = urlopen(url)
597604
dest_fd, self.path = tempfile.mkstemp(suffix=ext)
598605
try:
599606
dest = os.fdopen(dest_fd, 'wb')
@@ -605,8 +612,8 @@ def __init__(self, path=None, url=None, perform_init=True):
605612
self.using_temp_file = True
606613
src.close()
607614
dest.close()
608-
urlpath = urllib2.urlparse.urlparse(url)[2]
609-
filename = urllib2.unquote(urlpath.split("/")[-1])
615+
urlpath = urlparse(url)[2]
616+
filename = unquote(urlpath.split("/")[-1])
610617
else:
611618
if sys.platform.startswith("win"):
612619
self.path = self.path.replace("/", os.path.sep)

bioformats/tests/test_load_using_bioformats.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,12 @@
1616
import bioformats.formatreader as formatreader
1717
import bioformats.metadatatools as metadatatools
1818
from bioformats import load_image, load_image_url
19-
import urllib
19+
20+
import sys
21+
if sys.version_info.major == 3:
22+
from urllib.request import pathname2url
23+
else:
24+
from urllib import pathname2url
2025

2126
class TestLoadUsingBioformats(unittest.TestCase):
2227

@@ -48,7 +53,7 @@ def tearDown(self):
4853

4954
def test_01_01_open_file(self):
5055
path = os.path.join(os.path.dirname(__file__), 'Channel1-01-A-01.tif')
51-
url = "file:" + urllib.pathname2url(path).encode("utf-8")
56+
url = "file:" + pathname2url(path).encode("utf-8")
5257
image, scale = load_image_url(
5358
url, rescale=False, wants_max_intensity=True)
5459
self.assertEqual(image.shape[0], 640)
@@ -66,7 +71,7 @@ def test_01_03_unicode_url(self):
6671
# raise an exception when converting URL to string
6772
#
6873
path = os.path.join(os.path.dirname(__file__), 'Channel1-01-A-01.tif')
69-
url = "file:" + urllib.pathname2url(path).encode("utf-8")
74+
url = "file:" + pathname2url(path).encode("utf-8")
7075
image, scale = load_image_url(
7176
url, rescale=False, wants_max_intensity=True)
7277
self.assertEqual(image.shape[0], 640)

0 commit comments

Comments
 (0)