Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix ResourceWarnings in tests #1660

Merged
merged 1 commit into from
Oct 31, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add a test for this?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, ok. But I don't understand what exactly should I check in it ?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I read full method definition, now it's clear for me, thanks.

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):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As I see in usage, this function used as contextmanager, why you remove this (@contextmanager + yield) ?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, it really doesn't work as was expected, nice catch!

Copy link
Contributor Author

@horpto horpto Oct 31, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After meged comment:

Actually, it was already brokenl. Compare with code:

from contextlib import contextmanager

@contextmanager
def g():
    print("g input")
    yield
    print("g exit")

@contextmanager
def f():
    print("f input")
    yield g()
    print("f exit")


with f():
    print("with block")

This code prints:

f input
with block
f exit

But expected something like:

f input
g input
with block
g exit
f exit

Back to the file_or_filename, methods __enter__ and __exit__ of smart_open(input) or input are not called at all. Oops.

"""
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