Merge branch '8784-dir-listings'
[arvados.git] / sdk / python / arvados / cache.py
index ac6d18463c540ad2af84d3b76a9db2129cfa333e..85f2b89ea2b7368a2fb509120228538b9e9eb109 100644 (file)
@@ -1,5 +1,10 @@
+# Copyright (C) The Arvados Authors. All rights reserved.
+#
+# SPDX-License-Identifier: Apache-2.0
+
+from builtins import object
 import errno
-import md5
+import hashlib
 import os
 import tempfile
 import time
@@ -32,14 +37,14 @@ class SafeHTTPCache(object):
         return self._dir
 
     def _filename(self, url):
-        return os.path.join(self._dir, md5.new(url).hexdigest()+'.tmp')
+        return os.path.join(self._dir, hashlib.md5(url.encode('utf-8')).hexdigest()+'.tmp')
 
     def get(self, url):
         filename = self._filename(url)
         try:
             with open(filename, 'rb') as f:
                 return f.read()
-        except IOError as OSError:
+        except (IOError, OSError):
             return None
 
     def set(self, url, content):
@@ -49,7 +54,7 @@ class SafeHTTPCache(object):
             return None
         try:
             try:
-                f = os.fdopen(fd, 'w')
+                f = os.fdopen(fd, 'wb')
             except:
                 os.close(fd)
                 raise