Skip to content
Myr

Merge pull request #1846 from pallets/fsdecode

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

use os.fsdecode to display paths

authored by David Lord · committed by GitHub · parents 3f73190, 1167ebe · Browse files · 5 files+11 added−21 removed
This commit

State

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

Diff stats

5 files+11 added−21 removed

  • tests:+1 added−1 removed
  • other:+10 added−20 removed

5 files+11 added−21 removed

modified:src/click/_compat.py-8 removed
331 unmodified lines
332 return rv333 return _force_correct_text_writer(sys.stderr, encoding, errors, force_writable=True)334335336def filename_to_ui(value):337 if isinstance(value, bytes):338 value = value.decode(get_filesystem_encoding(), "replace")339 else:340 value = value.encode("utf-8", "surrogateescape").decode("utf-8", "replace")341 return value342343336def get_strerror(e, default=None):337 if hasattr(e, "strerror"):338 msg = e.strerror339 else:
249 unmodified lines
modified:src/click/exceptions.py+2 added-3 removed
1import os2from gettext import gettext as _3from gettext import ngettext44from ._compat import filename_to_ui5from ._compat import get_text_stderr6from .utils import echo78
216 unmodified lines
225class FileError(ClickException):226 """Raised if a file cannot be opened."""227228 def __init__(self, filename, hint=None):229 ui_filename = filename_to_ui(filename)229 if hint is None:230 hint = _("unknown error")231232 super().__init__(hint)234 self.ui_filename = ui_filename233 self.ui_filename = os.fsdecode(filename)234 self.filename = filename235236 def format_message(self):237 return _("Could not open file {filename!r}: {message}").format(
19 unmodified lines
modified:src/click/types.py+6 added-7 removed
4 unmodified lines
5from gettext import gettext as _6from gettext import ngettext78from ._compat import _get_argv_encoding9from ._compat import filename_to_ui9from ._compat import get_filesystem_encoding10from ._compat import get_strerror11from ._compat import open_stream12from .exceptions import BadParameter
640 unmodified lines
653 else:654 ctx.call_on_close(safecall(f.flush))655 return f656 except OSError as e: # noqa: B014658 self.fail(f"{filename_to_ui(value)!r}: {get_strerror(e)}", param, ctx)657 self.fail(f"{os.fsdecode(value)!r}: {get_strerror(e)}", param, ctx)658659 def shell_complete(self, ctx, param, incomplete):660 """Return a special completion marker that tells the completion661 system to use the shell to provide file path completions.
112 unmodified lines
774 if not self.exists:775 return self.coerce_path_result(rv)776 self.fail(777 _("{name} {filename!r} does not exist.").format(779 name=self.name.title(), filename=filename_to_ui(value)778 name=self.name.title(), filename=os.fsdecode(value)779 ),780 param,781 ctx,782 )783784 if not self.file_okay and stat.S_ISREG(st.st_mode):785 self.fail(786 _("{name} {filename!r} is a file.").format(788 name=self.name.title(), filename=filename_to_ui(value)787 name=self.name.title(), filename=os.fsdecode(value)788 ),789 param,790 ctx,791 )792 if not self.dir_okay and stat.S_ISDIR(st.st_mode):793 self.fail(794 _("{name} {filename!r} is a directory.").format(796 name=self.name.title(), filename=filename_to_ui(value)795 name=self.name.title(), filename=os.fsdecode(value)796 ),797 param,798 ctx,799 )800 if self.writable and not os.access(value, os.W_OK):801 self.fail(802 _("{name} {filename!r} is not writable.").format(804 name=self.name.title(), filename=filename_to_ui(value)803 name=self.name.title(), filename=os.fsdecode(value)804 ),805 param,806 ctx,807 )808 if self.readable and not os.access(value, os.R_OK):809 self.fail(810 _("{name} {filename!r} is not readable.").format(812 name=self.name.title(), filename=filename_to_ui(value)811 name=self.name.title(), filename=os.fsdecode(value)812 ),813 param,814 ctx,815 )
153 unmodified lines
modified:src/click/utils.py+2 added-2 removed
5 unmodified lines
6from ._compat import _default_text_stdout7from ._compat import _find_binary_writer8from ._compat import auto_wrap_for_ansi9from ._compat import binary_streams10from ._compat import filename_to_ui10from ._compat import get_filesystem_encoding11from ._compat import get_strerror12from ._compat import is_bytes13from ._compat import open_stream
339 unmodified lines
353 path that leads up to it.354 """355 if shorten:356 filename = os.path.basename(filename)358 return filename_to_ui(filename)357358 return os.fsdecode(filename)359360361def get_app_dir(app_name, roaming=True, force_posix=False):362 r"""Returns the config folder for the application. The default behavior363 is to return whatever is most appropriate for the operating system.364
166 unmodified lines
modified:tests/test_utils.py+1 added-1 removed
91 unmodified lines
92 assert click.format_filename("/x/foo.txt", shorten=True) == "foo.txt"9394 # filesystem encoding on windows permits this.95 if not WIN:96 assert click.format_filename(b"/x/foo\xff.txt", shorten=True) == "foo\ufffd.txt"96 assert click.format_filename(b"/x/foo\xff.txt", shorten=True) == "foo\udcff.txt"979899def test_prompts(runner):100 @click.command()
346 unmodified lines