Skip to content

Commit

Permalink
Narrow down the try/except
Browse files Browse the repository at this point in the history
This narrows down the try/except to wrap the loader only, and not the
extend/append. It is clearer what is being tried.
  • Loading branch information
mslw committed Nov 13, 2023
1 parent 71676da commit ef7d778
Showing 1 changed file with 10 additions and 8 deletions.
18 changes: 10 additions & 8 deletions datalad_tabby/io/load.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,12 +98,14 @@ def _load_single(
)

try:
obj.update(self._parse_tsv_single(src))
tsv_obj = self._parse_tsv_single(src)
except UnicodeDecodeError:
# by default Path.open() uses locale.getencoding()
# that didn't work, try guessing
encoding = cs_from_path(src).best().encoding
obj.update(self._parse_tsv_single(src, encoding=encoding))
tsv_obj = self._parse_tsv_single(src, encoding=encoding)

obj.update(tsv_obj)

return self._postproc_obj(obj, src=src, trace=trace)

Expand Down Expand Up @@ -159,19 +161,19 @@ def _load_many(
# to do with any possibly loaded JSON data

try:
array.extend(
self._parse_tsv_many(src, obj_tmpl, trace=trace, fieldnames=None)
tsv_array = self._parse_tsv_many(
src, obj_tmpl, trace=trace, fieldnames=None
)
except UnicodeDecodeError:
# by default Path.open() uses locale.getencoding()
# that didn't work, try guessing
encoding = cs_from_path(src).best().encoding
array.extend(
self._parse_tsv_many(
src, obj_tmpl, trace=trace, fieldnames=None, encoding=encoding
)
tsv_array = self._parse_tsv_many(
src, obj_tmpl, trace=trace, fieldnames=None, encoding=encoding
)

array.extend(tsv_array)

return array

def _parse_tsv_many(
Expand Down

0 comments on commit ef7d778

Please sign in to comment.