Skip to content

Commit cfb5ae2

Browse files
committed
Close all the handles
1 parent 61587ec commit cfb5ae2

File tree

5 files changed

+23
-32
lines changed

5 files changed

+23
-32
lines changed

pandas/conftest.py

-10
Original file line numberDiff line numberDiff line change
@@ -31,16 +31,6 @@ def pytest_addoption(parser):
3131
help="Fail if a test is skipped for missing data file.")
3232

3333

34-
def pytest_collection_modifyitems(items):
35-
# Make unhandled ResourceWarnings fail early to track down
36-
# https://github.com/pandas-dev/pandas/issues/22675
37-
if PY3:
38-
for item in items:
39-
item.add_marker(
40-
pytest.mark.filterwarnings("error::ResourceWarning")
41-
)
42-
43-
4434
def pytest_runtest_setup(item):
4535
if 'slow' in item.keywords and item.config.getoption("--skip-slow"):
4636
pytest.skip("skipping due to --skip-slow")

pandas/io/common.py

+2
Original file line numberDiff line numberDiff line change
@@ -386,6 +386,8 @@ def _get_handle(path_or_buf, mode, encoding=None, compression=None,
386386
# ZIP Compression
387387
elif compression == 'zip':
388388
zf = BytesZipFile(path_or_buf, mode)
389+
# Ensure the container is closed as well.
390+
handles.append(zf)
389391
if zf.mode == 'w':
390392
f = zf
391393
elif zf.mode == 'r':

pandas/tests/io/parser/compression.py

+11-15
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,8 @@ def test_zip(self):
3030
expected = self.read_csv(self.csv1)
3131

3232
with tm.ensure_clean('test_file.zip') as path:
33-
tmp = zipfile.ZipFile(path, mode='w')
34-
tmp.writestr('test_file', data)
35-
tmp.close()
33+
with zipfile.ZipFile(path, mode='w') as tmp:
34+
tmp.writestr('test_file', data)
3635

3736
result = self.read_csv(path, compression='zip')
3837
tm.assert_frame_equal(result, expected)
@@ -47,10 +46,9 @@ def test_zip(self):
4746

4847
with tm.ensure_clean('combined_zip.zip') as path:
4948
inner_file_names = ['test_file', 'second_file']
50-
tmp = zipfile.ZipFile(path, mode='w')
51-
for file_name in inner_file_names:
52-
tmp.writestr(file_name, data)
53-
tmp.close()
49+
with zipfile.ZipFile(path, mode='w') as tmp:
50+
for file_name in inner_file_names:
51+
tmp.writestr(file_name, data)
5452

5553
tm.assert_raises_regex(ValueError, 'Multiple files',
5654
self.read_csv, path, compression='zip')
@@ -60,8 +58,8 @@ def test_zip(self):
6058
compression='infer')
6159

6260
with tm.ensure_clean() as path:
63-
tmp = zipfile.ZipFile(path, mode='w')
64-
tmp.close()
61+
with zipfile.ZipFile(path, mode='w') as tmp:
62+
pass
6563

6664
tm.assert_raises_regex(ValueError, 'Zero files',
6765
self.read_csv, path, compression='zip')
@@ -84,9 +82,8 @@ def test_other_compression(self, compress_type, compress_method, ext):
8482
expected = self.read_csv(self.csv1)
8583

8684
with tm.ensure_clean() as path:
87-
tmp = compress_method(path, mode='wb')
88-
tmp.write(data)
89-
tmp.close()
85+
with compress_method(path, mode='wb') as tmp:
86+
tmp.write(data)
9087

9188
result = self.read_csv(path, compression=compress_type)
9289
tm.assert_frame_equal(result, expected)
@@ -100,9 +97,8 @@ def test_other_compression(self, compress_type, compress_method, ext):
10097
tm.assert_frame_equal(result, expected)
10198

10299
with tm.ensure_clean('test.{}'.format(ext)) as path:
103-
tmp = compress_method(path, mode='wb')
104-
tmp.write(data)
105-
tmp.close()
100+
with compress_method(path, mode='wb') as tmp:
101+
tmp.write(data)
106102
result = self.read_csv(path, compression='infer')
107103
tm.assert_frame_equal(result, expected)
108104

pandas/tests/io/test_pickle.py

+4-5
Original file line numberDiff line numberDiff line change
@@ -333,9 +333,9 @@ def compress_file(self, src_path, dest_path, compression):
333333
f = bz2.BZ2File(dest_path, "w")
334334
elif compression == 'zip':
335335
import zipfile
336-
f = zipfile.ZipFile(dest_path, "w",
337-
compression=zipfile.ZIP_DEFLATED)
338-
f.write(src_path, os.path.basename(src_path))
336+
with zipfile.ZipFile(dest_path, "w",
337+
compression=zipfile.ZIP_DEFLATED) as f:
338+
f.write(src_path, os.path.basename(src_path))
339339
elif compression == 'xz':
340340
lzma = pandas.compat.import_lzma()
341341
f = lzma.LZMAFile(dest_path, "w")
@@ -344,9 +344,8 @@ def compress_file(self, src_path, dest_path, compression):
344344
raise ValueError(msg)
345345

346346
if compression != "zip":
347-
with open(src_path, "rb") as fh:
347+
with open(src_path, "rb") as fh, f:
348348
f.write(fh.read())
349-
f.close()
350349

351350
def test_write_explicit(self, compression, get_random_path):
352351
base = get_random_path

pandas/util/testing.py

+6-2
Original file line numberDiff line numberDiff line change
@@ -205,8 +205,12 @@ def decompress_file(path, compression):
205205
msg = 'Unrecognized compression type: {}'.format(compression)
206206
raise ValueError(msg)
207207

208-
yield f
209-
f.close()
208+
try:
209+
yield f
210+
finally:
211+
f.close()
212+
if compression == "zip":
213+
zip_file.close()
210214

211215

212216
def assert_almost_equal(left, right, check_dtype="equiv",

0 commit comments

Comments
 (0)