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

Added function to replace gensim check_output with subprocess.check_output #1182

Closed
wants to merge 17 commits into from
Closed
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
14 changes: 12 additions & 2 deletions gensim/test/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

import logging
import unittest
import subprocess

from gensim import utils
from six import iteritems
Expand Down Expand Up @@ -79,7 +80,7 @@ def test_decode_entities(self):
# create a string that fails to decode with unichr on narrow python builds
body = u'It’s the Year of the Horse. YES VIN DIESEL 🙌 💯'
expected = u'It\x92s the Year of the Horse. YES VIN DIESEL \U0001f64c \U0001f4af'
self.assertEquals(utils.decode_htmlentities(body), expected)
self.assertEqual(utils.decode_htmlentities(body), expected)

class TestSampleDict(unittest.TestCase):
def test_sample_dict(self):
Expand All @@ -90,9 +91,18 @@ def test_sample_dict(self):
self.assertEqual(sampled_dict,expected_dict)
sampled_dict_random = utils.sample_dict(d,2)
if sampled_dict_random in expected_dict_random:
self.assertTrue(True)
self.assertTrue(True)


class TestCheckOutput(unittest.TestCase):
def test_check_output(self):
res = utils.check_output(args=["echo", "hello"])
self.assertEqual(res, b'hello\n')

def test_check_output_exception(self):
self.assertRaises(subprocess.CalledProcessError,
lambda: utils.check_output(args=["ldfs"]))


if __name__ == '__main__':
logging.root.setLevel(logging.WARNING)
Expand Down
43 changes: 23 additions & 20 deletions gensim/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -1154,30 +1154,33 @@ def keep_vocab_item(word, count, min_count, trim_rule=None):
return default_res


def check_output(stdout=subprocess.PIPE, *popenargs, **kwargs):
def check_output(**kwargs):
"""
Run command with arguments and return its output as a byte string.
Backported from Python 2.7 as it's implemented as pure python on stdlib.

>>> check_output(args=['/usr/bin/python', '--version'])
Python 2.6.2
Added extra KeyboardInterrupt handling
subprocess.check_output with the flag set to true will spawn a new
shell process and execute 'args' if there is an error while executing
args, the error message will be logged. This allows the user to
receive an accurate error message if subprocess fails to execute the
specified command.
If flag is set to true, subprocess.check_output takes 'args' as a string
instead of a list. To abstract the user from this,
this function will convert the argument list to a string if needed.
"""
args = kwargs.get("args")
args = " ".join(args)
try:
logger.debug("COMMAND: %s %s", popenargs, kwargs)
process = subprocess.Popen(stdout=stdout, *popenargs, **kwargs)
output, unused_err = process.communicate()
retcode = process.poll()
if retcode:
cmd = kwargs.get("args")
if cmd is None:
cmd = popenargs[0]
error = subprocess.CalledProcessError(retcode, cmd)
error.output = output
raise error
return output
res = subprocess.check_output(args, shell=True)
return res
except subprocess.CalledProcessError as e:
"""
If this error is raised, it is because check_output could not execute
the command. Instead of raising the error, output a more specific error
message
"""
logger.debug("Error cause due to argument passed to check_output - %s",
args)
logger.error(e)
raise
except KeyboardInterrupt:
process.terminate()
raise


Expand Down