Skip to content

Commit

Permalink
only convert path to bytes if not on windows
Browse files Browse the repository at this point in the history
  • Loading branch information
willforde committed Aug 14, 2017
1 parent 96e1a16 commit f7144ac
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 18 deletions.
5 changes: 0 additions & 5 deletions .coveragerc
@@ -1,8 +1,3 @@
[run]
source=urlquick
branch = True

[report]
exclude_lines =
# Exclude code that can't be executed.
if sys.platform.startswith("win"):
7 changes: 0 additions & 7 deletions tests/test_urlquick.py
Expand Up @@ -483,13 +483,6 @@ def test_handle_response_404(self):
ret = cache.handle_response("GET", 404, callback)
self.assertIsNone(ret)

def test_save_path_bytes(self):
ret = urlquick.CacheHandler.safe_path(b"testpath")
if sys.platform.startswith("win"):
self.assertIsInstance(ret, unicode)
else:
self.assertIsInstance(ret, bytes)

def test_save_path_uni(self):
ret = urlquick.CacheHandler.safe_path(u"testpath")
if sys.platform.startswith("win"):
Expand Down
13 changes: 7 additions & 6 deletions urlquick.py
Expand Up @@ -364,14 +364,15 @@ def safe_path(path):
Convert path into a encoding that best suits the platform os.
Unicode when on windows and utf8 when on linux/bsd.
:type path: str or bytes
:type path: str
:param path: The path to convert.
:return: Returns the path as unicode or utf8 encoded str.
"""
if sys.platform.startswith("win"):
return path.decode("utf8") if isinstance(path, bytes) else path
else:
return path.encode("utf8") if isinstance(path, unicode) else path
# Notting needs to be down if on windows as windows works well with unicode already
# We only want to convert to bytes when we are on linux.
if not sys.platform.startswith("win"):
path = path.encode("utf8")
return path

@classmethod
def hash_url(cls, url, data=None):
Expand Down Expand Up @@ -418,7 +419,7 @@ def cache_cleanup(cls, max_age=None):
cache_dir = cls.cache_dir()

# Loop over all cache files and remove stale files
filestart = cls.safe_path("cache-")
filestart = cls.safe_path(u"cache-")
for cachefile in os.listdir(cache_dir):
# Check that we actually have a cache file
if cachefile.startswith(filestart):
Expand Down

0 comments on commit f7144ac

Please sign in to comment.