Skip to content

Close HDFStore when an AttributeError happens #44800

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pandas/io/pytables.py
Original file line number Diff line number Diff line change
Expand Up @@ -461,7 +461,7 @@ def read_hdf(
chunksize=chunksize,
auto_close=auto_close,
)
except (ValueError, TypeError, KeyError):
except (ValueError, TypeError, KeyError, AttributeError):
if not isinstance(path_or_buf, HDFStore):
# if there is an error, close the store if we opened it.
with suppress(AttributeError):
Expand Down
16 changes: 16 additions & 0 deletions pandas/tests/io/pytables/test_read.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,22 @@ def test_read_missing_key_close_store(setup_path):
df.to_hdf(path, "k2")


def test_read_missing_attribute_close_store(setup_path):

with ensure_clean_path(setup_path) as path:
df = DataFrame()
with HDFStore(path) as s:
s.put("k", df)
s.get_storer("k").attributes.clear()

with pytest.raises(AttributeError, match="object has no attribute"):
read_hdf(path, "k")

# smoke test to test that file is properly closed after
# read with AttributeError before another write
df.to_hdf(path, "k")


def test_read_missing_key_opened_store(setup_path):
# GH 28699
with ensure_clean_path(setup_path) as path:
Expand Down