8000 TST clean up after ourselves in SVMlight test · Felixhawk/scikit-learn@8a0b317 · GitHub
[go: up one dir, main page]

Skip to content

Commit 8a0b317

Browse files
committed
TST clean up after ourselves in SVMlight test
As spotted by @yarikoptic in scikit-learn#2553; also, the test was previously not loading the bzip'd file.
1 parent 13a8315 commit 8a0b317

File tree

1 file changed

+7
-13
lines changed

1 file changed

+7
-13
lines changed

sklearn/datasets/tests/test_svmlight_format.py

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import numpy as np
55
import os
66
import shutil
7-
import tempfile
7+
from tempfile import NamedTemporaryFile
88

99
from sklearn.externals.six import b
1010
from sklearn.utils.testing import assert_equal
@@ -109,23 +109,17 @@ def test_load_svmlight_file_n_features():
109109
def test_load_compressed():
110110
X, y = load_svmlight_file(datafile)
111111

112-
try:
113-
tempdir = tempfile.mkdtemp(prefix="sklearn-test")
114-
115-
tmpgz = os.path.join(tempdir, "datafile.gz")
116-
shutil.copyfileobj(open(datafile, "rb"), gzip.open(tmpgz, "wb"))
117-
Xgz, ygz = load_svmlight_file(tmpgz)
112+
with NamedTemporaryFile(prefix="sklearn-test", suffix=".gz") as tmp:
113+
shutil.copyfileobj(open(datafile, "rb"), gzip.open(tmp.name, "wb"))
114+
Xgz, ygz = load_svmlight_file(tmp.name)
118115
assert_array_equal(X.toarray(), Xgz.toarray())
119116
assert_array_equal(y, ygz)
120117

121-
tmpbz = os.path.join(tempdir, "datafile.bz2")
122-
shutil.copyfileobj(open(datafile, "rb"), BZ2File(tmpbz, "wb"))
123-
Xbz, ybz = load_svmlight_file(tmpgz)
118+
with NamedTemporaryFile(prefix="sklearn-test", suffix=".bz2") as tmp:
119+
shutil.copyfileobj(open(datafile, "rb"), BZ2File(tmp.name, "wb"))
120+
Xbz, ybz = load_svmlight_file(tmp.name)
124121
assert_array_equal(X.toarray(), Xbz.toarray())
125122
assert_array_equal(y, ybz)
126-
except:
127-
shutil.rmtree(tempdir)
128-
raise
129123

130124

131125
@raises(ValueError)

0 commit comments

Comments
 (0)
0