From ef7d778311f36f3b646d5c388bd929f99a231345 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Szczepanik?= Date: Mon, 13 Nov 2023 19:05:37 +0100 Subject: [PATCH] Narrow down the try/except This narrows down the try/except to wrap the loader only, and not the extend/append. It is clearer what is being tried. --- datalad_tabby/io/load.py | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/datalad_tabby/io/load.py b/datalad_tabby/io/load.py index b454426..69597fe 100644 --- a/datalad_tabby/io/load.py +++ b/datalad_tabby/io/load.py @@ -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) @@ -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(