Skip to content

Commit

Permalink
Fix ResourceWarning in sqlite-utils insert, refs #534
Browse files Browse the repository at this point in the history
  • Loading branch information
simonw committed May 8, 2023
1 parent 2376c45 commit 4fc2f12
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions sqlite_utils/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -948,14 +948,15 @@ def insert_upsert_implementation(

# The --sniff option needs us to buffer the file to peek ahead
sniff_buffer = None
decoded_buffer = None
if sniff:
sniff_buffer = io.BufferedReader(file, buffer_size=4096)
decoded = io.TextIOWrapper(sniff_buffer, encoding=encoding)
decoded_buffer = io.TextIOWrapper(sniff_buffer, encoding=encoding)
else:
decoded = io.TextIOWrapper(file, encoding=encoding)
decoded_buffer = io.TextIOWrapper(file, encoding=encoding)

tracker = None
with file_progress(decoded, silent=silent) as decoded:
with file_progress(decoded_buffer, silent=silent) as decoded:
if csv or tsv:
if sniff:
# Read first 2048 bytes and use that to detect
Expand Down Expand Up @@ -1079,6 +1080,12 @@ def insert_upsert_implementation(
if tracker is not None:
db[table].transform(types=tracker.types)

# Clean up open file-like objects
if sniff_buffer:
sniff_buffer.close()
if decoded_buffer:
decoded_buffer.close()


def _find_variables(tb, vars):
to_find = list(vars)
Expand Down

0 comments on commit 4fc2f12

Please sign in to comment.