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
Provenance
- Commit
269a4ae- Tree
acc9ed8- Author
David Lord <davidism@gmail.com>- Committer
GitHub <noreply@github.com>- Parent 1
3f73190- Parent 2
1167ebe
5 files changed+11 added−21 removed
modified:src/click/_compat.py-8 removed
331 unmodified lines332 return rv333 return _force_correct_text_writer(sys.stderr, encoding, errors, force_writable=True)334 335 336-def 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 value342-343-336 def get_strerror(e, default=None):337 if hasattr(e, "strerror"):338 msg = e.strerror339 else:249 unmodified linesmodified:src/click/exceptions.py+2 added-3 removed
1+import os2 from gettext import gettext as _3 from gettext import ngettext4 4-from ._compat import filename_to_ui5 from ._compat import get_text_stderr6 from .utils import echo7 8 216 unmodified lines225 class FileError(ClickException):226 """Raised if a file cannot be opened."""227 228 def __init__(self, filename, hint=None):229- ui_filename = filename_to_ui(filename)229 if hint is None:230 hint = _("unknown error")231 232 super().__init__(hint)234- self.ui_filename = ui_filename233+ self.ui_filename = os.fsdecode(filename)234 self.filename = filename235 236 def format_message(self):237 return _("Could not open file {filename!r}: {message}").format(19 unmodified linesmodified:src/click/types.py+6 added-7 removed
4 unmodified lines5 from gettext import gettext as _6 from gettext import ngettext7 8 from ._compat import _get_argv_encoding9-from ._compat import filename_to_ui9 from ._compat import get_filesystem_encoding10 from ._compat import get_strerror11 from ._compat import open_stream12 from .exceptions import BadParameter640 unmodified lines653 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)658 659 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 lines774 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 )783 784 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 linesmodified:src/click/utils.py+2 added-2 removed
5 unmodified lines6 from ._compat import _default_text_stdout7 from ._compat import _find_binary_writer8 from ._compat import auto_wrap_for_ansi9 from ._compat import binary_streams10-from ._compat import filename_to_ui10 from ._compat import get_filesystem_encoding11 from ._compat import get_strerror12 from ._compat import is_bytes13 from ._compat import open_stream339 unmodified lines353 path that leads up to it.354 """355 if shorten:356 filename = os.path.basename(filename)358- return filename_to_ui(filename)357 358+ return os.fsdecode(filename)359 360+361 def 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 linesmodified:tests/test_utils.py+1 added-1 removed
91 unmodified lines92 assert click.format_filename("/x/foo.txt", shorten=True) == "foo.txt"93 94 # 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"97 98 99 def test_prompts(runner):100 @click.command()346 unmodified lines