Skip to content

Commit 0f395dc

Browse files
author
Lee Kamentsky
committed
issue #17 load_image_url raises exception if file url and unicode
1 parent e65c0e4 commit 0f395dc

2 files changed

Lines changed: 39 additions & 2 deletions

File tree

bioformats/formatreader.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -545,7 +545,10 @@ def __init__(self, path=None, url=None, perform_init=True):
545545
self.using_temp_file = False
546546
if url is not None and url.lower().startswith(file_scheme):
547547
utf8_url = urllib.url2pathname(url[len(file_scheme):])
548-
path = unicode(utf8_url, 'utf-8')
548+
if isinstance(utf8_url, str):
549+
path = unicode(utf8_url, 'utf-8')
550+
else:
551+
path = utf8_url
549552
self.path = path
550553
if path is None:
551554
if url.lower().startswith("omero:"):

bioformats/tests/test_load_using_bioformats.py

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,9 @@
1414

1515
import bioformats.formatreader as formatreader
1616
import bioformats.metadatatools as metadatatools
17-
from bioformats import load_image
17+
from bioformats import load_image, load_image_url
1818
from bioformats import log4j
19+
import urllib
1920

2021
class TestLoadUsingBioformats(unittest.TestCase):
2122

@@ -38,4 +39,37 @@ def test_file_not_found(self):
3839
self.assertRaises(exceptions.IOError,
3940
lambda :load_image(path))
4041

42+
class TestLoadUsingBioformatsURL(unittest.TestCase):
43+
def setUp(self):
44+
javabridge.attach()
45+
log4j.basic_config()
46+
47+
def tearDown(self):
48+
javabridge.detach()
4149

50+
def test_01_01_open_file(self):
51+
path = os.path.join(os.path.dirname(__file__), 'Channel1-01-A-01.tif')
52+
url = "file:" + urllib.pathname2url(path).encode("utf-8")
53+
image, scale = load_image_url(
54+
url, rescale=False, wants_max_intensity=True)
55+
self.assertEqual(image.shape[0], 640)
56+
57+
def test_01_02_open_http(self):
58+
url = "https://github.com/CellProfiler/python-bioformats"+\
59+
"/raw/master/bioformats/tests/Channel1-01-A-01.tif"
60+
image, scale = load_image_url(
61+
url, rescale=False, wants_max_intensity=True)
62+
self.assertEqual(image.shape[0], 640)
63+
64+
def test_01_03_unicode_url(self):
65+
#
66+
# Regression test of issue #17: ensure that this does not
67+
# raise an exception when converting URL to string
68+
#
69+
path = os.path.join(os.path.dirname(__file__), 'Channel1-01-A-01.tif')
70+
url = u"file:" + urllib.pathname2url(path).encode("utf-8")
71+
image, scale = load_image_url(
72+
url, rescale=False, wants_max_intensity=True)
73+
self.assertEqual(image.shape[0], 640)
74+
75+

0 commit comments

Comments
 (0)