Skip to content

Commit

Permalink
Fix ResourceWarnings in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Александр Менщиков authored and horpto committed Oct 28, 2017
1 parent 09cd946 commit e2ff841
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 9 deletions.
Binary file added gensim/.spyderproject
Binary file not shown.
11 changes: 7 additions & 4 deletions gensim/matutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -789,7 +789,7 @@ def __init__(self, input, transposed=True):
to be rows of the matrix (and document features are columns).
`input` is either a string (file path) or a file-like object that supports
`seek()` (e.g. gzip.GzipFile, bz2.BZ2File).
`seek()` (e.g. gzip.GzipFile, bz2.BZ2File). File-like objects are not closed automatically.
"""
logger.info("initializing corpus reader from %s", input)
self.input, self.transposed = input, transposed
Expand Down Expand Up @@ -881,9 +881,9 @@ def docbyoffset(self, offset):
if offset == -1:
return []
if isinstance(self.input, string_types):
fin = utils.smart_open(self.input)
fin, close_fin = utils.smart_open(self.input), True
else:
fin = self.input
fin, close_fin = self.input, False

fin.seek(offset) # works for gzip/bz2 input, too
previd, document = -1, []
Expand All @@ -895,8 +895,11 @@ def docbyoffset(self, offset):
assert previd <= docid, "matrix columns must come in ascending order"
if docid != previd:
if previd >= 0:
return document
break
previd = docid

document.append((termid, val,)) # add another field to the current document

if close_fin:
fin.close()
return document
3 changes: 2 additions & 1 deletion gensim/test/test_glove2word2vec.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import logging
import unittest
import os
import sys
import tempfile

import numpy
Expand All @@ -32,7 +33,7 @@ def setUp(self):

def testConversion(self):
check_output(args=[
'python', '-m', 'gensim.scripts.glove2word2vec',
sys.executable, '-m', 'gensim.scripts.glove2word2vec',
'--input', self.datapath, '--output', self.output_file
])
# test that the converted model loads successfully
Expand Down
6 changes: 2 additions & 4 deletions gensim/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
import multiprocessing
import shutil
import sys
from contextlib import contextmanager
import subprocess

import numpy as np
Expand Down Expand Up @@ -135,7 +134,6 @@ def __exit__(self, type, value, traceback):
nocm = NoCM()


@contextmanager
def file_or_filename(input):
"""
Return a file-like object ready to be read from the beginning. `input` is either
Expand All @@ -144,11 +142,11 @@ def file_or_filename(input):
"""
if isinstance(input, string_types):
# input was a filename: open as file
yield smart_open(input)
return smart_open(input)
else:
# input already a file-like object; just reset to the beginning
input.seek(0)
yield input
return input


def deaccent(text):
Expand Down

0 comments on commit e2ff841

Please sign in to comment.