add cmd.run_with_en_env

This commit is contained in:
Prodesire 2017-12-23 10:10:13 +08:00
parent 7f1ec71b66
commit 11e34cc573
2 changed files with 29 additions and 1 deletions

View File

@ -23,6 +23,26 @@ def run(cmd, wait=True, shell=True, timeout=0, timeinterval=1):
return p.poll(), stdout
def run_with_en_env(cmd, wait=True, shell=True, timeout=0, timeinterval=1):
"""
Run cmd with English character sets environment, so that the output will
be in English.
Parameters are same with `run`.
"""
if WINDOWS:
with chcp(437):
return run(cmd, wait=wait, shell=shell,
timeout=timeout, timeinterval=timeinterval)
else:
export_lang = 'export LANG=en_US.UTF-8'
if isinstance(cmd, list):
pre_cmd = [export_lang, ';']
else:
pre_cmd = export_lang + '; '
return run(pre_cmd + cmd, wait=wait, shell=shell,
timeout=timeout, timeinterval=timeinterval)
if PY2 and WINDOWS:
# enable passing unicode arguments from command line in Python 2.x
# https://stackoverflow.com/questions/846850/read-unicode-characters

View File

@ -3,7 +3,7 @@ import pytest
from pydu.platform import WINDOWS
from pydu.compat import string_types
from pydu.string import safeunicode
from pydu.cmd import run, cmdline_argv
from pydu.cmd import run, run_with_en_env, cmdline_argv
def test_run():
@ -20,6 +20,14 @@ def test_run():
assert 'timeout' in output
def test_run_with_en_env():
_, output = run_with_en_env('nocmd')
assert output.decode('ascii')
_, output = run_with_en_env(['nocmd'])
assert output.decode('ascii')
def test_cmdline_argv():
argv = cmdline_argv()
for s in argv[1:]: