diff options
author | nkozlovskiy <nmk@ydb.tech> | 2023-10-02 18:57:38 +0300 |
---|---|---|
committer | nkozlovskiy <nmk@ydb.tech> | 2023-10-02 19:39:06 +0300 |
commit | 6295ef4d23465c11296e898b9dc4524ad9592b5d (patch) | |
tree | fc0c852877b2c52f365a1f6ed0710955844338c2 /contrib/deprecated/python/win-unicode-console/win_unicode_console/unicode_argv.py | |
parent | de63c80b75948ecc13894854514d147840ff8430 (diff) | |
download | ydb-6295ef4d23465c11296e898b9dc4524ad9592b5d.tar.gz |
oss ydb: fix dstool building and test run
Diffstat (limited to 'contrib/deprecated/python/win-unicode-console/win_unicode_console/unicode_argv.py')
-rw-r--r-- | contrib/deprecated/python/win-unicode-console/win_unicode_console/unicode_argv.py | 79 |
1 files changed, 79 insertions, 0 deletions
diff --git a/contrib/deprecated/python/win-unicode-console/win_unicode_console/unicode_argv.py b/contrib/deprecated/python/win-unicode-console/win_unicode_console/unicode_argv.py new file mode 100644 index 0000000000..d23bc05f12 --- /dev/null +++ b/contrib/deprecated/python/win-unicode-console/win_unicode_console/unicode_argv.py @@ -0,0 +1,79 @@ +"""Get Unicode argv strings in Python 2 on Windows + +get_full_unicode_argv based on +http://code.activestate.com/recipes/572200/ + +argv_setter_hook based on +https://mail.python.org/pipermail/python-list/2016-June/710183.html +""" + +import sys +from ctypes import WinDLL, c_int, POINTER, byref +from ctypes.wintypes import LPCWSTR, LPWSTR + +kernel32 = WinDLL("kernel32", use_last_error=True) +shell32 = WinDLL("shell32", use_last_error=True) + +GetCommandLineW = kernel32.GetCommandLineW +GetCommandLineW.argtypes = () +GetCommandLineW.restype = LPCWSTR + +CommandLineToArgvW = shell32.CommandLineToArgvW +CommandLineToArgvW.argtypes = (LPCWSTR, POINTER(c_int)) +CommandLineToArgvW.restype = POINTER(LPWSTR) + +LocalFree = kernel32.LocalFree + + +def get_full_unicode_argv(): + cmd = GetCommandLineW() + argc = c_int(0) + argv = CommandLineToArgvW(cmd, byref(argc)) + py_argv = [arg for i, arg in zip(range(argc.value), argv)] + LocalFree(argv) + return py_argv + +def get_unicode_argv(): + if original_argv == [""]: + return [u""] + + new_argv = get_full_unicode_argv()[-len(original_argv):] + + if original_argv[0] == "-c": + new_argv[0] = u"-c" + + return new_argv + + +original_argv = None + +def argv_setter_hook(path): + global original_argv + + if original_argv is not None: # already got it + raise ImportError + + try: + original_argv = sys.argv + except AttributeError: + pass + else: + enable() + finally: + raise ImportError + +def enable(): + global original_argv + + if original_argv is None: + try: + original_argv = sys.argv + except AttributeError: # in sitecustomize in Python 2 + sys.path_hooks[:0] = [argv_setter_hook] + return + + sys.argv = get_unicode_argv() + +def disable(): + if original_argv is not None: + sys.argv = original_argv |