Avoid leaking file descriptors in python tests. (#1353)

Summary: Pull Request resolved: https://github.com/facebookresearch/faiss/pull/1353

Test Plan: Imported from OSS

Reviewed By: mdouze

Differential Revision: D23292456

Pulled By: beauby

fbshipit-source-id: 44458eb16d037883ff39827accf5edddb1b1bb89
pull/1356/head
Lucas Hosseini 2020-08-24 06:45:30 -07:00 committed by Facebook GitHub Bot
parent e0423f7fcd
commit 24c4460dd2
4 changed files with 41 additions and 32 deletions

View File

@ -37,7 +37,8 @@ class TestBinaryFlat(unittest.TestCase):
index.add(self.xb)
D, I = index.search(self.xq, 3)
_, tmpnam = tempfile.mkstemp()
fd, tmpnam = tempfile.mkstemp()
os.close(fd)
try:
faiss.write_index_binary(index, tmpnam)
@ -74,8 +75,8 @@ class TestBinaryIVF(unittest.TestCase):
index.add(self.xb)
D, I = index.search(self.xq, 3)
_, tmpnam = tempfile.mkstemp()
fd, tmpnam = tempfile.mkstemp()
os.close(fd)
try:
faiss.write_index_binary(index, tmpnam)
@ -107,7 +108,8 @@ class TestObjectOwnership(unittest.TestCase):
index = faiss.IndexBinaryFlat(d)
index.add(self.xb)
_, tmpnam = tempfile.mkstemp()
fd, tmpnam = tempfile.mkstemp()
os.close(fd)
try:
faiss.write_index_binary(index, tmpnam)
@ -137,8 +139,8 @@ class TestBinaryFromFloat(unittest.TestCase):
index.add(self.xb)
D, I = index.search(self.xq, 3)
_, tmpnam = tempfile.mkstemp()
fd, tmpnam = tempfile.mkstemp()
os.close(fd)
try:
faiss.write_index_binary(index, tmpnam)
@ -171,8 +173,8 @@ class TestBinaryHNSW(unittest.TestCase):
index.add(self.xb)
D, I = index.search(self.xq, 3)
_, tmpnam = tempfile.mkstemp()
fd, tmpnam = tempfile.mkstemp()
os.close(fd)
try:
faiss.write_index_binary(index, tmpnam)
@ -197,8 +199,8 @@ class TestBinaryHNSW(unittest.TestCase):
index.add(self.xb)
D, I = index.search(self.xq, 3)
_, tmpnam = tempfile.mkstemp()
fd, tmpnam = tempfile.mkstemp()
os.close(fd)
try:
faiss.write_index_binary(index, tmpnam)

View File

@ -445,7 +445,8 @@ class TestHNSW(unittest.TestCase):
self.io_and_retest(index, Dhnsw, Ihnsw)
def io_and_retest(self, index, Dhnsw, Ihnsw):
_, tmpfile = tempfile.mkstemp()
fd, tmpfile = tempfile.mkstemp()
os.close(fd)
try:
faiss.write_index(index, tmpfile)
index2 = faiss.read_index(tmpfile)

View File

@ -130,7 +130,8 @@ class TestRemove(unittest.TestCase):
assert False, 'should have raised an exception'
# while we are there, let's test I/O as well...
_, tmpnam = tempfile.mkstemp()
fd, tmpnam = tempfile.mkstemp()
os.close(fd)
try:
faiss.write_index_binary(index, tmpnam)
index = faiss.read_index_binary(tmpnam)
@ -409,7 +410,8 @@ class TestIVFFlatDedup(unittest.TestCase):
assert ref == new
# test I/O
_, tmpfile = tempfile.mkstemp()
fd, tmpfile = tempfile.mkstemp()
os.close(fd)
try:
faiss.write_index(index_new, tmpfile)
index_st = faiss.read_index(tmpfile)

View File

@ -29,10 +29,11 @@ class TestIOVariants(unittest.TestCase):
# should be fine
faiss.read_index(fname)
with open(fname, 'rb') as f:
data = f.read()
# now damage file
data = open(fname, 'rb').read()
data = data[:int(len(data) / 2)]
open(fname, 'wb').write(data)
with open(fname, 'wb') as f:
f.write(data[:int(len(data) / 2)])
# should make a nice readable exception that mentions the filename
try:
@ -88,19 +89,20 @@ class TestCallbacks(unittest.TestCase):
def test_buf_read(self):
x = np.random.uniform(size=20)
_, fname = tempfile.mkstemp()
fd, fname = tempfile.mkstemp()
os.close(fd)
try:
x.tofile(fname)
f = open(fname, 'rb')
reader = faiss.PyCallbackIOReader(f.read, 1234)
with open(fname, 'rb') as f:
reader = faiss.PyCallbackIOReader(f.read, 1234)
bsz = 123
reader = faiss.BufferedIOReader(reader, bsz)
bsz = 123
reader = faiss.BufferedIOReader(reader, bsz)
y = np.zeros_like(x)
print('nbytes=', y.nbytes)
reader(faiss.swig_ptr(y), y.nbytes, 1)
y = np.zeros_like(x)
print('nbytes=', y.nbytes)
reader(faiss.swig_ptr(y), y.nbytes, 1)
np.testing.assert_array_equal(x, y)
finally:
@ -113,18 +115,18 @@ class TestCallbacks(unittest.TestCase):
index = faiss.IndexFlatL2(d)
index.add(x)
_, fname = tempfile.mkstemp()
fd, fname = tempfile.mkstemp()
os.close(fd)
try:
faiss.write_index(index, fname)
f = open(fname, 'rb')
with open(fname, 'rb') as f:
reader = faiss.PyCallbackIOReader(f.read, 1234)
reader = faiss.PyCallbackIOReader(f.read, 1234)
if bsz > 0:
reader = faiss.BufferedIOReader(reader, bsz)
if bsz > 0:
reader = faiss.BufferedIOReader(reader, bsz)
index2 = faiss.read_index(reader)
index2 = faiss.read_index(reader)
self.assertEqual(index.d, index2.d)
np.testing.assert_array_equal(
@ -162,7 +164,8 @@ class TestCallbacks(unittest.TestCase):
index = faiss.IndexFlatL2(d)
index.add(x)
_, fname = tempfile.mkstemp()
fd, fname = tempfile.mkstemp()
os.close(fd)
try:
faiss.write_index(index, fname)
@ -178,6 +181,7 @@ class TestCallbacks(unittest.TestCase):
)
finally:
del reader
if os.path.exists(fname):
os.unlink(fname)