Skip to content

Commit

Permalink
Fixes piskvorky#1869 , Mmcorpus file object behaviour fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
sj29-innovate committed Feb 21, 2018
1 parent d996114 commit 4bd295e
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 31 deletions.
38 changes: 9 additions & 29 deletions gensim/test/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,46 +86,26 @@ def test_decode_entities(self):
self.assertEqual(utils.decode_htmlentities(body), expected)

def test_open_file_existent_file(self):
exc = False
try:
with utils.open_file(datapath('testcorpus.mm')):
pass
except Exception:
exc = True

self.assertFalse(exc)
number_of_lines_in_file = 30
with utils.open_file(datapath('testcorpus.mm')) as infile:
self.assertEqual(sum(1 for _ in infile), number_of_lines_in_file)

def test_open_file_non_existent_file(self):
exc = False
try:
with utils.open_file(datapath('non_existent_file.txt')):
with self.assertRaises(Exception):
with utils.open_file('non_existent_file.txt'):
pass
except Exception:
exc = True

self.assertTrue(exc)

def test_open_file_existent_file_object(self):
exc = False
number_of_lines_in_file = 30
file_obj = open(datapath('testcorpus.mm'))
try:
with utils.open_file(file_obj):
pass
except Exception:
exc = True

self.assertFalse(exc)
with utils.open_file(file_obj) as infile:
self.assertEqual(sum(1 for _ in infile), number_of_lines_in_file)

def test_open_file_non_existent_file_object(self):
exc = False
file_obj = None
try:
with self.assertRaises(Exception):
with utils.open_file(file_obj):
pass
except Exception:
exc = True

self.assertTrue(exc)


class TestSampleDict(unittest.TestCase):
Expand Down
4 changes: 2 additions & 2 deletions gensim/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,12 +162,12 @@ def open_file(input):
except Exception:
# Handling any unhandled exceptions from the code nested in 'with' statement.
exc = True
if not type(mgr).__exit__(mgr, *sys.exc_info()):
if not mgr.__exit__(*sys.exc_info()):
raise
# Try to introspect and silence errors.
finally:
if not exc and isinstance(input, string_types):
type(mgr).__exit__(mgr, None, None, None)
mgr.__exit__(None, None, None)


def deaccent(text):
Expand Down

0 comments on commit 4bd295e

Please sign in to comment.