Skip to content

Commit

Permalink
Empty storages should now return None and not raise ValueError (see #67)
Browse files Browse the repository at this point in the history
  • Loading branch information
msiemens committed Sep 17, 2015
1 parent bc6ccd0 commit a951300
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 15 deletions.
13 changes: 7 additions & 6 deletions docs/extend.rst
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,11 @@ of course you can add your own. Let's look how you could add a
def read(self):
with open(self.filename) as handle:
data = yaml.safe_load(handle.read()) # (2)
if data is None: # (3)
raise ValueError
try:
data = yaml.safe_load(handle.read()) # (2)
return data
except yaml.YAMLError:
return None # (3)
def write(self, data):
with open(self.filename, 'w') as handle:
Expand All @@ -75,8 +76,8 @@ There are some things we should look closer at:
2. We use ``yaml.safe_load`` as recommended by the
`PyYAML documentation <http://pyyaml.org/wiki/PyYAMLDocumentation#LoadingYAML>`_
when processing data from a potentially untrusted source.
3. If the storage is uninitialized, TinyDB expects the storage to throw a
``ValueError`` so it can do any internal initialization that is necessary.
3. If the storage is uninitialized, TinyDB expects the storage to return
``None`` so it can do any internal initialization that is necessary.
4. If your storage needs any cleanup (like closing file handles) before an
instance is destroyed, you can put it in the ``close()`` method. To run
these, you'll either have to run ``db.close()`` on your ``TinyDB`` instance
Expand Down
5 changes: 1 addition & 4 deletions tinydb/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,10 +99,7 @@ def _read(self):
:rtype: dict
"""

try:
return self._storage.read()
except ValueError:
return {}
return self._storage.read() or {}

def _read_table(self, table):
"""
Expand Down
3 changes: 3 additions & 0 deletions tinydb/middlewares.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,9 @@ def register_serializer(self, serializer, name):
def read(self):
data = self.storage.read()

if data is None:
return None

for serializer_name in self._serializers:
serializer = self._serializers[serializer_name]
tag = '{{{0}}}:'.format(serializer_name) # E.g: '{TinyDate}:'
Expand Down
16 changes: 11 additions & 5 deletions tinydb/storages.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def read(self):
Read the last stored state.
Any kind of deserialization should go here.
Raise ``ValueError`` here to indicate that the storage is empty.
Return ``None`` here to indicate that the storage is empty.
:rtype: dict
"""
Expand Down Expand Up @@ -93,8 +93,16 @@ def __del__(self):
self.close()

def read(self):
self._handle.seek(0)
return json.load(self._handle)
# Get the file size
self._handle.seek(0, 2)
size = self._handle.tell()

if not size:
# File is empty
return None
else:
self._handle.seek(0)
return json.load(self._handle)

def write(self, data):
self._handle.seek(0)
Expand All @@ -118,8 +126,6 @@ def __init__(self):
super(MemoryStorage, self).__init__()

def read(self):
if self.memory is None:
raise ValueError
return self.memory

def write(self, data):
Expand Down

0 comments on commit a951300

Please sign in to comment.