diff --git a/fs/_url_tools.py b/fs/_url_tools.py index c9424672..4c6fd73f 100644 --- a/fs/_url_tools.py +++ b/fs/_url_tools.py @@ -2,11 +2,14 @@ import six import platform +if False: # typing.TYPE_CHECKING + from typing import Text, Union, BinaryIO _WINDOWS_PLATFORM = platform.system() == "Windows" def url_quote(path_snippet): + # type: (Text) -> Text """ On Windows, it will separate drive letter and quote windows path alone. No magic on Unix-alie path, just pythonic @@ -15,7 +18,6 @@ def url_quote(path_snippet): Arguments: path_snippet: a file path, relative or absolute. """ - # type: (Text) -> Text if _WINDOWS_PLATFORM and _has_drive_letter(path_snippet): drive_letter, path = path_snippet.split(":", 1) if six.PY2: @@ -30,6 +32,7 @@ def url_quote(path_snippet): def _has_drive_letter(path_snippet): + # type: (Text) -> bool """ The following path will get True D:/Data @@ -42,6 +45,5 @@ def _has_drive_letter(path_snippet): Arguments: path_snippet: a file path, relative or absolute. """ - # type: (Text) -> Text windows_drive_pattern = ".:[/\\\\].*$" return re.match(windows_drive_pattern, path_snippet) is not None diff --git a/fs/tarfs.py b/fs/tarfs.py index 181368db..250291a1 100644 --- a/fs/tarfs.py +++ b/fs/tarfs.py @@ -469,7 +469,7 @@ def isclosed(self): def geturl(self, path, purpose="download"): # type: (Text, Text) -> Text - if purpose == "fs": + if purpose == "fs" and isinstance(self._file, six.string_types): quoted_file = url_quote(self._file) quoted_path = url_quote(path) return "tar://{}!/{}".format(quoted_file, quoted_path) diff --git a/fs/test.py b/fs/test.py index 71c130ce..a39ecc94 100644 --- a/fs/test.py +++ b/fs/test.py @@ -1846,3 +1846,4 @@ def test_hash(self): self.assertEqual( foo_fs.hash("hashme.txt", "md5"), "9fff4bb103ab8ce4619064109c54cb9c" ) + diff --git a/fs/zipfs.py b/fs/zipfs.py index f19c7d18..c347731c 100644 --- a/fs/zipfs.py +++ b/fs/zipfs.py @@ -449,7 +449,7 @@ def readbytes(self, path): def geturl(self, path, purpose="download"): # type: (Text, Text) -> Text - if purpose == "fs": + if purpose == "fs" and isinstance(self._file, six.string_types): quoted_file = url_quote(self._file) quoted_path = url_quote(path) return "zip://{}!/{}".format(quoted_file, quoted_path) diff --git a/tests/test_base.py b/tests/test_base.py index 06a70aad..66708517 100644 --- a/tests/test_base.py +++ b/tests/test_base.py @@ -1,4 +1,3 @@ -# coding: utf-8 """Test (abstract) base FS class.""" from __future__ import unicode_literals diff --git a/tests/test_tarfs.py b/tests/test_tarfs.py index 57c53e9d..c72791c3 100644 --- a/tests/test_tarfs.py +++ b/tests/test_tarfs.py @@ -200,6 +200,10 @@ def test_geturl_for_fs(self): ) self.assertEqual(self.fs.geturl(test_file, purpose="fs"), expected) + def test_geturl_for_fs_but_file_is_binaryio(self): + self.fs._file = six.BytesIO() + self.assertRaises(NoURL, self.fs.geturl, "test", "fs") + def test_geturl_for_download(self): test_file = "foo/bar/egg/foofoo" with self.assertRaises(NoURL): diff --git a/tests/test_url_tools.py b/tests/test_url_tools.py index b09679c9..5b5d4a1d 100644 --- a/tests/test_url_tools.py +++ b/tests/test_url_tools.py @@ -1,3 +1,7 @@ +# coding: utf-8 +"""Test url tools. """ +from __future__ import unicode_literals + import platform import unittest diff --git a/tests/test_zipfs.py b/tests/test_zipfs.py index 6958151a..9b2e82ea 100644 --- a/tests/test_zipfs.py +++ b/tests/test_zipfs.py @@ -176,6 +176,10 @@ def test_geturl_for_fs(self): ) self.assertEqual(self.fs.geturl(test_file, purpose="fs"), expected) + def test_geturl_for_fs_but_file_is_binaryio(self): + self.fs._file = six.BytesIO() + self.assertRaises(NoURL, self.fs.geturl, "test", "fs") + def test_geturl_for_download(self): test_file = "foo/bar/egg/foofoo" with self.assertRaises(NoURL):