Skip to content

Commit

Permalink
Resolve differences between seek_until docstring and implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
Ed Slavich committed Apr 29, 2021
1 parent 234b91d commit a666707
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 20 deletions.
2 changes: 2 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@
- Add argument to ``asdftool diff`` that ignores tree nodes that match
a JMESPath expression. [#956]

- Fix behavior of ``exception`` argument to ``GenericFile.seek_until``. [#980]

2.7.4 (unreleased)
------------------

Expand Down
2 changes: 1 addition & 1 deletion asdf/asdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -872,7 +872,7 @@ def _open_asdf(cls, self, fd, uri=None, mode='r',
# now, but we don't do anything special with it until
# after the blocks have been read
tree = yamlutil.load_tree(reader)
has_blocks = fd.seek_until(constants.BLOCK_MAGIC, 4, include=True)
has_blocks = fd.seek_until(constants.BLOCK_MAGIC, 4, include=True, exception=False)
elif yaml_token == constants.BLOCK_MAGIC:
has_blocks = True
elif yaml_token != b'':
Expand Down
6 changes: 6 additions & 0 deletions asdf/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,9 @@ class AsdfConversionWarning(AsdfWarning):
"""
Warning class used for failures to convert data into custom types.
"""

class DelimiterNotFoundError(ValueError):
"""
Indicates that a delimiter was not found when reading or
seeking through a file.
"""
36 changes: 19 additions & 17 deletions asdf/generic_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import numpy as np

from . import util
from .exceptions import DelimiterNotFoundError
from .extern import atomicfile
from .util import patched_urllib_parse

Expand Down Expand Up @@ -142,7 +143,7 @@ def read(self, nbytes=None):

if content == b'':
if self._exception:
raise ValueError("{0} not found".format(self._delimiter_name))
raise DelimiterNotFoundError("{0} not found".format(self._delimiter_name))
self._past_end = True
return content

Expand All @@ -156,7 +157,7 @@ def read(self, nbytes=None):
self._past_end = True
elif nbytes is None and self._exception:
# Read the whole file and didn't find the delimiter
raise ValueError("{0} not found".format(self._delimiter_name))
raise DelimiterNotFoundError("{0} not found".format(self._delimiter_name))
else:
if nbytes:
content = content[:nbytes - len(self._initial_content)]
Expand Down Expand Up @@ -442,7 +443,7 @@ def read_until(self, delimiter, readahead_bytes, delimiter_name=None,
Raises
------
ValueError :
DelimiterNotFoundError :
If the delimiter is not found before the end of the file.
"""
buff = io.BytesIO()
Expand Down Expand Up @@ -491,7 +492,7 @@ def reader_until(self, delimiter, readahead_bytes,
Raises
------
ValueError :
DelimiterNotFoundError :
If the delimiter is not found before the end of the file.
"""
raise NotImplementedError()
Expand Down Expand Up @@ -530,27 +531,28 @@ def seek_until(self, delimiter, readahead_bytes, delimiter_name=None,
Returns
-------
content : bytes
The content from the current position in the file, up to
the delimiter. Includes the delimiter if `include` is
``True``.
bool
``True`` if the delimiter was found.
Raises
------
ValueError :
If the delimiter is not found before the end of the file.
DelimiterNotFoundError :
If ``exception`` is enabled and the delimiter is not found
before the end of the file.
"""
reader = self.reader_until(
delimiter, readahead_bytes, delimiter_name=delimiter_name,
include=include, initial_content=initial_content,
exception=exception)
while True:
try:
content = reader.read(self.block_size)
except ValueError:
exception=True)
try:
while reader.read(self.block_size) != b'':
pass
return True
except DelimiterNotFoundError as e:
if exception:
raise e
else:
return False
if content == b'':
return True

def fast_forward(self, size):
"""
Expand Down
5 changes: 3 additions & 2 deletions asdf/tests/test_generic_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import numpy as np

import asdf
from asdf import exceptions
from asdf import util
from asdf import generic_io
from asdf.asdf import is_asdf_file
Expand Down Expand Up @@ -550,14 +551,14 @@ def test_truncated_reader():

# Simple cases where the delimiter is not found at all
tr = generic_io._TruncatedReader(fd, b'x', 1)
with pytest.raises(ValueError):
with pytest.raises(exceptions.DelimiterNotFoundError):
tr.read()

fd.seek(0)
tr = generic_io._TruncatedReader(fd, b'x', 1)
assert tr.read(100) == content[:100]
assert tr.read(1) == content[100:]
with pytest.raises(ValueError):
with pytest.raises(exceptions.DelimiterNotFoundError):
tr.read()

fd.seek(0)
Expand Down

0 comments on commit a666707

Please sign in to comment.