Skip to content
Myr

Added support for dealing with unicode argv on win py2

Before adoption: this commit came in with the history the project adopted, so no landing records it.

This should help with getting data back correctly on Windows when users are running Python2. Unfortunately this also required some changes to compensate for in the Path type.

authored by Armin Ronacher · committed by Armin Ronacher · parent 7951a73 · Browse files · 4 files+59 added−3 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

4 files+59 added−3 removed

  • other:+59 added−3 removed

4 files+59 added−3 removed

modified:click/_compat.py+35 added
549 unmodified lines
550 except Exception:551 return False552553554def get_os_args():555 # On Python 3 or non windows environments, we just return the args556 # as they are.557 if not PY2 or os.name != 'nt':558 return sys.argv[1:]559560 from ctypes import WINFUNCTYPE, windll, POINTER, byref, c_int561 from ctypes.wintypes import LPWSTR, LPCWSTR562563 GetCommandLineW = WINFUNCTYPE(LPWSTR)(564 ('GetCommandLineW', windll.kernel32))565 CommandLineToArgvW = WINFUNCTYPE(566 POINTER(LPWSTR), LPCWSTR, POINTER(c_int))(567 ('CommandLineToArgvW', windll.shell32))568569 argc = c_int(0)570 argv_unicode = CommandLineToArgvW(GetCommandLineW(), byref(argc))571 argv = [argv_unicode[i] for i in range(0, argc.value)]572573 if not hasattr(sys, 'frozen'):574 argv = argv[1:]575 while len(argv) > 0:576 arg = argv[0]577 if not arg.startswith('-') or arg == '-':578 break579 argv = argv[1:]580 if arg == '-m':581 break582 if arg == '-c':583 argv[0] = '-c'584 break585586 return argv587588589def _get_argv_encoding():590 if os.name == 'nt':591 import locale592 return locale.getpreferredencoding()
39 unmodified lines
modified:click/core.py+3 added-2 removed
11 unmodified lines
12from .formatting import HelpFormatter, join_options13from .parser import OptionParser, split_opt14from .globals import push_context, pop_context1516from ._compat import PY2, isidentifier, iteritems16from ._compat import PY2, isidentifier, iteritems, get_os_args17from ._unicodefun import _check_for_unicode_literals, _verify_python3_env181920_missing = object()
648 unmodified lines
669 else:670 _check_for_unicode_literals()671672 if args is None:673 args = sys.argv[1:]673 args = get_os_args()674 else:675 args = list(args)676677 if prog_name is None:678 prog_name = make_str(os.path.basename(679 sys.argv and sys.argv[0] or __file__))680
1034 unmodified lines
modified:click/types.py+14 added-1 removed
352 unmodified lines
353 :param readable: if true, a readable check is performed.354 :param resolve_path: if this is true, then the path is fully resolved355 before the value is passed onwards. This means356 that it's absolute and symlinks are resolved.357 :param type: optionally a string type that should be used to358 represent the path. The default is `None` which359 means the return value will be either bytes or360 unicode depending on what makes most sense given the361 input data Click deals with.362 """363 envvar_list_splitter = os.path.pathsep364365 def __init__(self, exists=False, file_okay=True, dir_okay=True,361 writable=False, readable=True, resolve_path=False):366 writable=False, readable=True, resolve_path=False,367 path_type=None):368 self.exists = exists369 self.file_okay = file_okay370 self.dir_okay = dir_okay371 self.writable = writable372 self.readable = readable373 self.resolve_path = resolve_path374 self.type = path_type375376 if self.file_okay and not self.dir_okay:377 self.name = 'file'378 self.path_type = 'File'
39 unmodified lines
418 self.path_type,419 filename_to_ui(value)420 ), param, ctx)421422 if self.type is not None and not isinstance(rv, self.type):423 if self.type is text_type:424 rv = rv.decode(get_filesystem_encoding())425 else:426 rv = rv.encode(get_filesystem_encoding())427428 return rv429430431class Tuple(CompositeParamType):
105 unmodified lines
modified:CHANGES+7 added
20 unmodified lines
21 available.22- Click used to return byte strings on Python 2 in some unit-testing23 situations. This has been fixed to correctly return unicode strings24 now.25- For Windows users on Python 2, Click will now handle Unicode more26 correctly handle Unicode coming in from the system. This also has27 the disappointing side effect that filenames will now be always28 unicode by default in the `Path` type which means that this can29 introduce small bugs for code not aware of this.30- Added a `type` parameter to `Path` to force a specific string type31 on the value.3233Version 5.134-----------35
251 unmodified lines