From 4780fbcedfd3ec8e8498eb5dd7a742f4681e8d78 Mon Sep 17 00:00:00 2001 From: n1nj4sec Date: Thu, 28 Apr 2016 17:46:02 +0200 Subject: [PATCH] fix encoding issues in interactive shell --- pupy/modules/interactive_shell.py | 3 +-- pupy/packages/all/interactive_shell.py | 14 +++++++++++--- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/pupy/modules/interactive_shell.py b/pupy/modules/interactive_shell.py index 3a5ea33c..e70a1e09 100644 --- a/pupy/modules/interactive_shell.py +++ b/pupy/modules/interactive_shell.py @@ -54,11 +54,10 @@ class InteractiveShell(PupyModule): program="/system/bin/sh" elif self.client.is_windows(): program="cmd.exe" - encoding="cp437" if args.program: program=args.program with redirected_stdio(self.client.conn): - self.client.conn.modules.interactive_shell.interactive_open(program=program, encoding=encoding) + self.client.conn.modules.interactive_shell.interactive_open(program=program) else: #handling tty self.client.load_package("ptyshell") self.ps=self.client.conn.modules['ptyshell'].PtyShell() diff --git a/pupy/packages/all/interactive_shell.py b/pupy/packages/all/interactive_shell.py index acaf16a5..7e73479b 100644 --- a/pupy/packages/all/interactive_shell.py +++ b/pupy/packages/all/interactive_shell.py @@ -7,6 +7,8 @@ from threading import Thread from Queue import Queue, Empty import time import traceback +import locale +import re import rpyc import os @@ -48,17 +50,23 @@ def flush_loop(queue, encoding): def interactive_open(program=None, encoding=None): try: - if program is None: + if program is None or program=="cmd.exe": if "win" in sys.platform.lower(): program="cmd.exe" - encoding="cp437" + try: + #couldn't find a better way, none of the following methods worked for me : kernel32.SetConsoleOutputCP(), locale.getpreferredencoding(), sys.stdout.encoding + encoding="cp"+str(re.findall(r".*:\s*([0-9]+)",subprocess.check_output("chcp", shell=True))[0]) + except: + pass else: if "SHELL" in os.environ: program=os.environ["SHELL"] else: program="/bin/sh" encoding=None - print "Opening interactive %s ... (encoding : %s)"%(program,encoding) + if encoding is None: + encoding=locale.getpreferredencoding() + print "Opening interactive %s (with encoding %s)..."%(program,encoding) if sys.platform=="win32": startupinfo = subprocess.STARTUPINFO() startupinfo.dwFlags = subprocess.CREATE_NEW_CONSOLE | subprocess.STARTF_USESHOWWINDOW