def test_streamlit_write(self): """Test streamlit.util.streamlit.write.""" with patch("streamlit.util.open", mock_open()) as p, util.streamlit_write( FILENAME ) as output: output.write("some data") p().write.assert_called_once_with("some data")
def test_streamlit_write(self): """Test streamlit.util.streamlit.write.""" dirname = os.path.dirname(util.get_streamlit_file_path(FILENAME)) with patch("streamlit.util.open", mock_open()) as open, patch( "streamlit.util.os.makedirs" ) as makedirs, util.streamlit_write(FILENAME) as output: output.write("some data") open().write.assert_called_once_with("some data") makedirs.assert_called_once_with(dirname)
def test_streamlit_write_exception(self): """Test streamlit.util.streamlit.write.""" with patch('streamlit.util.open', mock_open()) as p, \ patch('streamlit.util.platform.system') as system: system.return_value = 'Darwin' p.side_effect = OSError(errno.EINVAL, '[Errno 22] Invalid argument') with pytest.raises(util.Error) as e, \ util.streamlit_write(FILENAME) as output: output.write('some data') error_msg = ('Unable to write file: /some/cache/file\n' 'Python is limited to files below 2GB on OSX. ' 'See https://bugs.python.org/issue24658') self.assertEqual(str(e.value), error_msg)
def write_to_disk_cache(key, rv): path = util.get_streamlit_file_path('cache', '%s.pickle' % key) try: with util.streamlit_write(path, binary=True) as output: pickle.dump(rv, output, pickle.HIGHEST_PROTOCOL) # In python 2, it's pickle struct error. # In python 3, it's an open error in util. except (util.Error, struct.error) as e: LOGGER.debug(e) # Cleanup file so we don't leave zero byte files. try: os.remove(path) except (FileNotFoundError, IOError, OSError): pass raise CacheError('Unable to write to cache: %s' % e)
def test_streamlit_write_exception(self): """Test streamlit.util.streamlit.write.""" with patch( "streamlit.util.open", mock_open()) as p, patch("streamlit.util.os.makedirs"), patch( "streamlit.util.platform.system") as system: system.return_value = "Darwin" p.side_effect = OSError(errno.EINVAL, "[Errno 22] Invalid argument") with pytest.raises( util.Error) as e, util.streamlit_write(FILENAME) as output: output.write("some data") error_msg = ("Unable to write file: /some/cache/file\n" "Python is limited to files below 2GB on OSX. " "See https://bugs.python.org/issue24658") self.assertEqual(str(e.value), error_msg)
def _write_to_disk_cache(key, value, args_mutated): path = util.get_streamlit_file_path("cache", "%s.pickle" % key) try: with util.streamlit_write(path, binary=True) as output: entry = DiskCacheEntry(value=value, args_mutated=args_mutated) pickle.dump(entry, output, pickle.HIGHEST_PROTOCOL) # In python 2, it's pickle struct error. # In python 3, it's an open error in util. except (util.Error, struct.error) as e: LOGGER.debug(e) # Clean up file so we don't leave zero byte files. try: os.remove(path) except (FileNotFoundError, IOError, OSError): pass raise CacheError("Unable to write to cache: %s" % e)