Skip to content

Commit 2f73a88

Browse files
authored
Fix streamlit#1230: OSError not caught when writing cache to disk (streamlit#8)
* Fix streamlit#1230: OSError not caught when writing cache to disk * Don't show hashing warning when user imports a module inside a cached function.
1 parent f5332a1 commit 2f73a88

File tree

2 files changed

+16
-5
lines changed

2 files changed

+16
-5
lines changed

lib/streamlit/caching.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,11 @@ def _read_from_disk_cache(key):
250250
except util.Error as e:
251251
LOGGER.error(e)
252252
raise CacheError('Unable to read from cache: %s' % e)
253-
except FileNotFoundError:
253+
254+
except (
255+
OSError, # Python 2
256+
FileNotFoundError # Python 3
257+
):
254258
raise CacheKeyNotFoundError('Key not found in disk cache')
255259
return value, args_mutated
256260

@@ -266,7 +270,7 @@ def _write_to_disk_cache(key, value, args_mutated):
266270
# In python 3, it's an open error in util.
267271
except (util.Error, struct.error) as e:
268272
LOGGER.debug(e)
269-
# Cleanup file so we don't leave zero byte files.
273+
# Clean up file so we don't leave zero byte files.
270274
try:
271275
os.remove(path)
272276
except (FileNotFoundError, IOError, OSError):

lib/streamlit/hashing.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -265,14 +265,19 @@ def _to_bytes(self, obj, context):
265265
elif inspect.iscode(obj):
266266
return self._code_to_bytes(obj, context)
267267
elif inspect.ismodule(obj):
268+
# TODO: Figure out how to best show this kind of warning to the
269+
# user. In the meantime, show nothing. This scenario is too common,
270+
# so the current warning is quite annoying...
271+
# st.warning(('Streamlit does not support hashing modules. '
272+
# 'We did not hash `%s`.') % obj.__name__)
268273
# TODO: Hash more than just the name for internal modules.
269-
st.warning(('Streamlit does not support hashing modules. '
270-
'We did not hash `%s`.') % obj.__name__)
271274
return self.to_bytes(obj.__name__)
272275
elif inspect.isclass(obj):
273-
# TODO: Hash more than just the name of classes.
276+
# TODO: Figure out how to best show this kind of warning to the
277+
# user.
274278
st.warning(('Streamlit does not support hashing classes. '
275279
'We did not hash `%s`.') % obj.__name__)
280+
# TODO: Hash more than just the name of classes.
276281
return self.to_bytes(obj.__name__)
277282
elif isinstance(obj, functools.partial):
278283
# The return value of functools.partial is not a plain function:
@@ -288,6 +293,8 @@ def _to_bytes(self, obj, context):
288293
# As a last resort, we pickle the object to hash it.
289294
return pickle.dumps(obj, pickle.HIGHEST_PROTOCOL)
290295
except Exception:
296+
# TODO: Figure out how to best show this kind of warning to the
297+
# user.
291298
st.warning('Streamlit cannot hash an object of type %s.' % type(obj))
292299

293300
def _code_to_bytes(self, code, context):

0 commit comments

Comments
 (0)