mirror of https://github.com/flaggo/pydu.git
add cmd.chcp for Windows
This commit is contained in:
parent
fcc01429cf
commit
0b0a0ba2f7
19
pydu/cmd.py
19
pydu/cmd.py
|
@ -61,3 +61,22 @@ if PY2 and WINDOWS:
|
||||||
else:
|
else:
|
||||||
def cmdline_argv():
|
def cmdline_argv():
|
||||||
return sys.argv
|
return sys.argv
|
||||||
|
|
||||||
|
|
||||||
|
if WINDOWS:
|
||||||
|
from ctypes import windll
|
||||||
|
|
||||||
|
class chcp(object):
|
||||||
|
"""
|
||||||
|
Context manager which sets the active code page number.
|
||||||
|
It could also be used as function.
|
||||||
|
"""
|
||||||
|
def __init__(self, code):
|
||||||
|
self.origin_code = windll.kernel32.GetConsoleOutputCP()
|
||||||
|
windll.kernel32.SetConsoleOutputCP(code)
|
||||||
|
|
||||||
|
def __enter__(self):
|
||||||
|
pass
|
||||||
|
|
||||||
|
def __exit__(self, exc_type, exc_val, exc_tb):
|
||||||
|
windll.kernel32.SetConsoleOutputCP(self.origin_code)
|
||||||
|
|
|
@ -1,4 +1,6 @@
|
||||||
import sys
|
import sys
|
||||||
|
import pytest
|
||||||
|
from pydu.platform import WINDOWS
|
||||||
from pydu.compat import string_types
|
from pydu.compat import string_types
|
||||||
from pydu.string import safeunicode
|
from pydu.string import safeunicode
|
||||||
from pydu.cmd import run, cmdline_argv
|
from pydu.cmd import run, cmdline_argv
|
||||||
|
@ -22,3 +24,20 @@ def test_cmdline_argv():
|
||||||
argv = cmdline_argv()
|
argv = cmdline_argv()
|
||||||
for s in argv[1:]:
|
for s in argv[1:]:
|
||||||
assert isinstance(s, string_types)
|
assert isinstance(s, string_types)
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.skipif(not WINDOWS, reason='Not support non windows')
|
||||||
|
def test_chcp():
|
||||||
|
from pydu.cmd import chcp
|
||||||
|
from ctypes import windll
|
||||||
|
|
||||||
|
origin_code = windll.kernel32.GetConsoleOutputCP()
|
||||||
|
with chcp(437):
|
||||||
|
assert windll.kernel32.GetConsoleOutputCP() == 437
|
||||||
|
assert windll.kernel32.GetConsoleOutputCP() == origin_code
|
||||||
|
|
||||||
|
try:
|
||||||
|
chcp(437)
|
||||||
|
assert windll.kernel32.GetConsoleOutputCP() == 437
|
||||||
|
finally:
|
||||||
|
windll.kernel32.SetConsoleOutputCP(origin_code)
|
||||||
|
|
Loading…
Reference in New Issue