Skip to content

Commit d927c5f

Browse files
authored
Close source file after looping over all trees in copy_root (#112)
* Close source file after looping over all trees * Make sure there's no file handle preventing close prematurely in test_copy_root.
1 parent 561743c commit d927c5f

File tree

2 files changed

+29
-24
lines changed

2 files changed

+29
-24
lines changed

src/hepconvert/copy_root.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -283,4 +283,5 @@ def copy_root(
283283
msg = "Are the branch-names correct?"
284284
if len(trees) > 1 and progress_bar is not False and progress_bar is not None:
285285
progress_bar.update(n=1)
286-
f.close()
286+
f.close()
287+
of.close()

tests/test_copy_root.py

Lines changed: 27 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -115,30 +115,34 @@ def test_keep_tree(tmp_path):
115115
"z1": np.array([3, 77, 3, 4, 5]),
116116
"p1": np.array([44, 55, 66, 7, 8]),
117117
}
118-
with uproot.open(Path(tmp_path) / "two_trees.root") as file:
119-
hepconvert.copy_root(
120-
Path(tmp_path) / "copied.root",
121-
Path(tmp_path) / "two_trees.root",
122-
keep_trees="tree",
123-
force=True,
124-
)
125-
with uproot.open(Path(tmp_path) / "copied.root") as copy:
126-
assert copy.keys(cycle=False) == ["tree"]
127-
for tree in copy.keys(cycle=False):
128-
for key in copy[tree].keys():
129-
assert ak.all(copy[tree][key].array() == file[tree][key].array())
130118

131-
hepconvert.copy_root(
132-
Path(tmp_path) / "copied.root",
133-
Path(tmp_path) / "two_trees.root",
134-
keep_trees=["tree", "tree2", "tree3"],
135-
force=True,
136-
)
137-
with uproot.open(Path(tmp_path) / "copied.root") as copy:
138-
assert copy.keys(cycle=False) == ["tree", "tree2", "tree3"]
139-
for tree in copy.keys(cycle=False):
140-
for key in copy[tree].keys():
141-
assert ak.all(copy[tree][key].array() == file[tree][key].array())
119+
hepconvert.copy_root(
120+
Path(tmp_path) / "copied.root",
121+
Path(tmp_path) / "two_trees.root",
122+
keep_trees="tree",
123+
force=True,
124+
)
125+
with uproot.open(Path(tmp_path) / "copied.root") as copy, uproot.open(
126+
Path(tmp_path) / "two_trees.root"
127+
) as file:
128+
assert copy.keys(cycle=False) == ["tree"]
129+
for tree in copy.keys(cycle=False):
130+
for key in copy[tree].keys():
131+
assert ak.all(copy[tree][key].array() == file[tree][key].array())
132+
133+
hepconvert.copy_root(
134+
Path(tmp_path) / "copied.root",
135+
Path(tmp_path) / "two_trees.root",
136+
keep_trees=["tree", "tree2", "tree3"],
137+
force=True,
138+
)
139+
with uproot.open(Path(tmp_path) / "copied.root") as copy, uproot.open(
140+
Path(tmp_path) / "two_trees.root"
141+
) as file:
142+
assert copy.keys(cycle=False) == ["tree", "tree2", "tree3"]
143+
for tree in copy.keys(cycle=False):
144+
for key in copy[tree].keys():
145+
assert ak.all(copy[tree][key].array() == file[tree][key].array())
142146

143147
with pytest.raises(
144148
ValueError,

0 commit comments

Comments
 (0)