mirror of
https://github.com/simonw/sqlite-utils.git
synced 2026-10-31 07:27:50 +01:00
Forward ignore_extras= and extras_key= from the TSV and auto-detect paths
Both branches that recurse into the CSV branch called rows_from_file() without forwarding the two arguments, so the inner call applied its own defaults and raised RowError even when the caller had explicitly asked to ignore or capture the extras. The outer _extra_key_strategy wrapper in each branch was dead code as a result. Forward both arguments and drop the redundant wrapper, so the strategy is applied once on the shared CSV path. With no options passed the behaviour is unchanged. Fixes #892
This commit is contained in:
parent
6bc1d33d58
commit
d5d01b3f9e
2 changed files with 50 additions and 18 deletions
|
|
@ -358,17 +358,18 @@ def rows_from_file(
|
|||
)
|
||||
return _CloseableIterator(iter(rows), decoded_fp), Format.CSV
|
||||
elif format == Format.TSV:
|
||||
# The inner call applies the extra-field strategy, so the
|
||||
# ignore_extras= and extras_key= arguments must be passed to it -
|
||||
# see https://github.com/simonw/sqlite-utils/issues/892
|
||||
rows, _ = rows_from_file(
|
||||
fp, format=Format.CSV, dialect=csv.excel_tab, encoding=encoding
|
||||
)
|
||||
return (
|
||||
_extra_key_strategy(
|
||||
cast(Iterable[dict[str | None, object]], rows),
|
||||
ignore_extras,
|
||||
extras_key,
|
||||
),
|
||||
Format.TSV,
|
||||
fp,
|
||||
format=Format.CSV,
|
||||
dialect=csv.excel_tab,
|
||||
encoding=encoding,
|
||||
ignore_extras=ignore_extras,
|
||||
extras_key=extras_key,
|
||||
)
|
||||
return rows, Format.TSV
|
||||
elif format is None:
|
||||
# Detect the format, then call this recursively
|
||||
buffered = io.BufferedReader(cast(io.RawIOBase, fp), buffer_size=4096)
|
||||
|
|
@ -393,18 +394,16 @@ def rows_from_file(
|
|||
first_bytes.decode(encoding or "utf-8-sig", "ignore")
|
||||
)
|
||||
rows, _ = rows_from_file(
|
||||
buffered, format=Format.CSV, dialect=dialect, encoding=encoding
|
||||
buffered,
|
||||
format=Format.CSV,
|
||||
dialect=dialect,
|
||||
encoding=encoding,
|
||||
ignore_extras=ignore_extras,
|
||||
extras_key=extras_key,
|
||||
)
|
||||
# Make sure we return the format we detected
|
||||
detected_format = Format.TSV if dialect.delimiter == "\t" else Format.CSV
|
||||
return (
|
||||
_extra_key_strategy(
|
||||
cast(Iterable[dict[str | None, object]], rows),
|
||||
ignore_extras,
|
||||
extras_key,
|
||||
),
|
||||
detected_format,
|
||||
)
|
||||
return rows, detected_format
|
||||
else:
|
||||
raise RowsFromFileError("Bad format")
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue