Skip to content

bump csv field size limit in load_csv so long rows parse#50

Open
HrachShah wants to merge 1 commit into
simonw:mainfrom
HrachShah:fix/csv-field-size-limit-41
Open

bump csv field size limit in load_csv so long rows parse#50
HrachShah wants to merge 1 commit into
simonw:mainfrom
HrachShah:fix/csv-field-size-limit-41

Conversation

@HrachShah

Copy link
Copy Markdown

Closes #41.

The csv module's C parser caps each field at 131072 bytes by default.
`load_csv` had no protection against that, so a row with a long string
(e.g. a nucleotide sequence, a large JSON blob, an embedded log line)
raised an uncaught `_csv.Error: field larger than field limit (131072)`
instead of being returned to the caller.

Set `csv.field_size_limit(sys.maxsize)` at the top of `load_csv` so the
parser is allowed to handle the longest field the caller can possibly
give it. `sys.maxsize` is the platform's `PY_SSIZE_T_MAX` and matches
the recommendation in the Python csv docs
for "set the limit as high as possible" without overflowing the parser.

Tests: `tests/test_csv_diff.py` now has `test_load_csv_handles_fields_larger_than_default_limit`
(200_000-byte field) and `test_load_csv_handles_tsv_with_long_fields` (same
size, tab-separated). Both fail on master with the original `_csv.Error`
and pass with the bump. The full `tests/` suite still passes 26/26.

The csv module's underlying C parser caps each field at 131072 bytes by
default. load_csv had no protection against that, so a row with a long
string (e.g. a nucleotide sequence, a large JSON blob, an embedded log
line) raised an uncaught _csv.Error instead of being returned to the
caller.

Set csv.field_size_limit(sys.maxsize) at the top of load_csv so the
parser is allowed to handle the longest field the caller can possibly
give it. sys.maxsize is the platform's PY_SSIZE_T_MAX and matches the
recommendation in the Python csv docs for "set the limit as high as
possible" without overflowing the parser.

Closes simonw#41.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug: Uncaught exception "_csv.Error: field larger than field limit (131072)"

1 participant