Disabling built-in ANSI Escape behavior in a Python Windows build

48 views Asked by At

This has been vexing me for a while. I really, REALLY love MobaXterm. I'm an old-line *NIX kinda guy, and I've had Cygwin on every Windows box I've endured, then started using MinGW, then discovered MobaXterm which IS the Swiss Army Knife for *NIX guys in the Windows Wilderness.

I work in a "managed" environment without administrative access to anything. Recently, my IT department has completely re-arranged things and Windows Python under MobaXterm has an idiosyncrasy that I cannot get around. In this version STDOUT is polluted with ANSI escape sequences. Simplest example I can produce:

$ cat stdout-test.py
#
# python stdout-test.py fpout.log > stdout.log
#

import os
import sys

fpout = open(sys.argv[1], "w", encoding="utf-8", newline="\n");

a = "this is a test"

print(a)

print(a, file=fpout)

pass

$ python stdout-test.py fpout.log > stdout.log
$ od -c fpout.log
0000000   t   h   i   s       i   s       a       t   e   s   t  \n
0000017
$ od -c stdout.log
0000000 033   [   0   m 033   [   0   K   t   h   i   s       i   s
0000020   a       t   e   s   t 033   [   0   K  \r  \n 033   [   0   K
0000040

HOWEVER: if I execute the python executable directly, these artifacts are missing:

$ python
Python 3.8.6 (tags/v3.8.6:db45529, Sep 23 2020, 15:52:53) [MSC v.1927 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> dir(sys)
['__breakpointhook__', '__displayhook__', '__doc__', '__excepthook__', '__interactivehook__', '
__loader__', '__name__', '__package__', '__spec__', '__stderr__', '__stdin__', '__stdout__', '_
_unraisablehook__', '_base_executable', '_clear_type_cache', '_current_frames', '_debugmallocst
ats', '_enablelegacywindowsfsencoding', '_framework', '_getframe', '_git', '_home', '_xoptions'
, 'addaudithook', 'api_version', 'argv', 'audit', 'base_exec_prefix', 'base_prefix', 'breakpoin
thook', 'builtin_module_names', 'byteorder', 'call_tracing', 'callstats', 'copyright', 'display
hook', 'dllhandle', 'dont_write_bytecode', 'exc_info', 'excepthook', 'exec_prefix', 'executable
', 'exit', 'flags', 'float_info', 'float_repr_style', 'get_asyncgen_hooks', 'get_coroutine_orig
in_tracking_depth', 'getallocatedblocks', 'getcheckinterval', 'getdefaultencoding', 'getfilesys
temencodeerrors', 'getfilesystemencoding', 'getprofile', 'getrecursionlimit', 'getrefcount', 'g
etsizeof', 'getswitchinterval', 'gettrace', 'getwindowsversion', 'hash_info', 'hexversion', 'im
plementation', 'int_info', 'intern', 'is_finalizing', 'maxsize', 'maxunicode', 'meta_path', 'mo
dules', 'path', 'path_hooks', 'path_importer_cache', 'platform', 'prefix', 'ps1', 'ps2', 'pycac
he_prefix', 'set_asyncgen_hooks', 'set_coroutine_origin_tracking_depth', 'setcheckinterval', 's
etprofile', 'setrecursionlimit', 'setswitchinterval', 'settrace', 'stderr', 'stdin', 'stdout',
'thread_info', 'unraisablehook', 'version', 'version_info', 'warnoptions', 'winver']
>>> sys._base_executable
'C:\\Users\\jsaari\\AppData\\Local\\Programs\\Python\\Python38\\python.exe'
>>> quit()
$ /drives/c/users/jsaari/appdata/local/programs/python/python38/python -i
Python 3.8.6 (tags/v3.8.6:db45529, Sep 23 2020, 15:52:53) [MSC v.1927 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> quit()
$ /drives/c/users/jsaari/appdata/local/programs/python/python38/python stdout-test.py fpout.log > stdout.log
$ od -c fpout.log
0000000   t   h   i   s       i   s       a       t   e   s   t  \n
0000017
$ od -c stdout.log
0000000   t   h   i   s       i   s       a       t   e   s   t  \r  \n
0000020

Y'see, there's this extra step in MobaXterm that might be culpable, but I have not been able to tease out any information on what's actually happening with Python by way of this:

$ alias python
alias python='_exec_cyg_or_win_binary python python'

So what's actually happening here and how can I exploit how to NOT pollute stdout?

Windows 10 MobaXterm v22.0 Build 4858 Python 3.8.6 (tags/v3.8.6:db45529, Sep 23 2020, 15:52:53) [MSC v.1927 64 bit (AMD64)] on win32 Type "help", "copyright", "credits" or "license" for more information.

0

There are 0 answers