Skip to content

Commit

Permalink
Read arguments as Unicode on Python 2 on Windows
Browse files Browse the repository at this point in the history
Need it for international domain names, such as 115.бел

See issue #572
  • Loading branch information
techtonik committed Apr 1, 2017
1 parent f1d4861 commit c2b4e62
Showing 1 changed file with 39 additions and 3 deletions.
42 changes: 39 additions & 3 deletions httpie/__main__.py
Expand Up @@ -4,15 +4,51 @@
"""
import sys

if sys.platform.startswith('win') and sys.version_info[0] == 2:
# https://github.com/jakubroztocil/httpie/issues/572

# https://stackoverflow.com/questions/846850/read-unicode-characters-from-command-line-arguments-in-python-2-x-on-windows/846931#846931
def win32_unicode_argv():
"""Use shell32.GetCommandLineArgvW to get sys.argv as a list of
Unicode strings.
def main():
Versions 2.x of Python don't support Unicode in sys.argv on
Windows, with the underlying Windows API instead replacing
multi-byte characters with '?'.
"""

from ctypes import POINTER, byref, cdll, c_int, windll
from ctypes.wintypes import LPCWSTR, LPWSTR

GetCommandLineW = cdll.kernel32.GetCommandLineW
GetCommandLineW.argtypes = []
GetCommandLineW.restype = LPCWSTR

CommandLineToArgvW = windll.shell32.CommandLineToArgvW
CommandLineToArgvW.argtypes = [LPCWSTR, POINTER(c_int)]
CommandLineToArgvW.restype = POINTER(LPWSTR)

cmd = GetCommandLineW()
argc = c_int(0)
argv = CommandLineToArgvW(cmd, byref(argc))
if argc.value > 0:
# Remove Python executable and commands if present
start = argc.value - len(sys.argv)
return [argv[i] for i in xrange(start, argc.value)]

argv = win32_unicode_argv()
else:
argv = sys.argv


def main(argv):
try:
from .core import main
sys.exit(main())
sys.exit(main(argv))
except KeyboardInterrupt:
from . import ExitStatus
sys.exit(ExitStatus.ERROR_CTRL_C)


if __name__ == '__main__':
main()
main(argv)

0 comments on commit c2b4e62

Please sign in to comment.