From fc166dea5622807331cbbca44962112301548959 Mon Sep 17 00:00:00 2001 From: resteve Date: Fri, 13 Feb 2015 08:12:38 +0100 Subject: [PATCH] Remove shell on Popen subprocess --- translate.py | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/translate.py b/translate.py index df716e0..f83ee16 100644 --- a/translate.py +++ b/translate.py @@ -1,13 +1,21 @@ # This file is part of translate_apertium module for Tryton. # The COPYRIGHT file at the top level of this repository contains the full # copyright notices and license terms. -from subprocess import Popen, PIPE +import subprocess from trytond.pool import PoolMeta __all__ = ['TranslateWizardStart', 'TranslateWizardTranslation'] __metaclass__ = PoolMeta +def apertium_output(cmd, stdin=''): + process = subprocess.Popen(cmd, stdout=subprocess.PIPE, + stderr=subprocess.PIPE, stdin=subprocess.PIPE) + stdout, stderr = process.communicate(stdin.encode('utf-8')) + process.wait() + return unicode(stdout, 'utf-8'), stderr + + class TranslateWizardStart: __name__ = 'translate.wizard.start' @@ -35,15 +43,9 @@ class TranslateWizardTranslation: @classmethod def get_translation_from_apertium(cls, text, source_lang, target_lang): - cmd = 'echo "%s" | apertium %s-%s' % ( - text, - source_lang[:2], - target_lang[:2], - ) - # force utf8 - TypeError: execv() arg 2 must contain only strings - cmd = cmd.encode('utf-8') - proccess = Popen(cmd, shell=True, stdout=PIPE, stderr=PIPE) - out, error = proccess.communicate() + lang = '%s-%s' % (source_lang[:2], target_lang[:2]) + cmd = ['apertium', lang] + out, error = apertium_output(cmd, text) if error: cls.raise_user_error('error_translating', error_args=(text,)) return out.strip('\r\n')