diff options
| author | David Lord <davidism@gmail.com> | 2021-04-12 07:36:25 -0700 |
|---|---|---|
| committer | David Lord <davidism@gmail.com> | 2021-04-12 07:36:25 -0700 |
| commit | 1167ebe1f2b678b58394332e82675300d4a0299a (patch) | |
| tree | acc9ed82d0318533bcedbeb43fd9e5757c9cc3b9 /src | |
| parent | 3f7319081113a0bdfa8fa0b8c4ff018a8f5e5278 (diff) | |
| download | click-fsdecode.tar.gz | |
use os.fsdecode to display pathsfsdecode
Works with pathlib, consistent with Python's output.
Diffstat (limited to 'src')
| -rw-r--r-- | src/click/_compat.py | 8 | ||||
| -rw-r--r-- | src/click/exceptions.py | 5 | ||||
| -rw-r--r-- | src/click/types.py | 13 | ||||
| -rw-r--r-- | src/click/utils.py | 4 |
4 files changed, 10 insertions, 20 deletions
diff --git a/src/click/_compat.py b/src/click/_compat.py index a5e4a87..afa1d92 100644 --- a/src/click/_compat.py +++ b/src/click/_compat.py @@ -333,14 +333,6 @@ def get_text_stderr(encoding=None, errors=None): return _force_correct_text_writer(sys.stderr, encoding, errors, force_writable=True) -def filename_to_ui(value): - if isinstance(value, bytes): - value = value.decode(get_filesystem_encoding(), "replace") - else: - value = value.encode("utf-8", "surrogateescape").decode("utf-8", "replace") - return value - - def get_strerror(e, default=None): if hasattr(e, "strerror"): msg = e.strerror diff --git a/src/click/exceptions.py b/src/click/exceptions.py index ab9b312..c7c8b10 100644 --- a/src/click/exceptions.py +++ b/src/click/exceptions.py @@ -1,7 +1,7 @@ +import os from gettext import gettext as _ from gettext import ngettext -from ._compat import filename_to_ui from ._compat import get_text_stderr from .utils import echo @@ -226,12 +226,11 @@ class FileError(ClickException): """Raised if a file cannot be opened.""" def __init__(self, filename, hint=None): - ui_filename = filename_to_ui(filename) if hint is None: hint = _("unknown error") super().__init__(hint) - self.ui_filename = ui_filename + self.ui_filename = os.fsdecode(filename) self.filename = filename def format_message(self): diff --git a/src/click/types.py b/src/click/types.py index 543833a..eebf830 100644 --- a/src/click/types.py +++ b/src/click/types.py @@ -6,7 +6,6 @@ from gettext import gettext as _ from gettext import ngettext from ._compat import _get_argv_encoding -from ._compat import filename_to_ui from ._compat import get_filesystem_encoding from ._compat import get_strerror from ._compat import open_stream @@ -655,7 +654,7 @@ class File(ParamType): ctx.call_on_close(safecall(f.flush)) return f except OSError as e: # noqa: B014 - self.fail(f"{filename_to_ui(value)!r}: {get_strerror(e)}", param, ctx) + self.fail(f"{os.fsdecode(value)!r}: {get_strerror(e)}", param, ctx) def shell_complete(self, ctx, param, incomplete): """Return a special completion marker that tells the completion @@ -776,7 +775,7 @@ class Path(ParamType): return self.coerce_path_result(rv) self.fail( _("{name} {filename!r} does not exist.").format( - name=self.name.title(), filename=filename_to_ui(value) + name=self.name.title(), filename=os.fsdecode(value) ), param, ctx, @@ -785,7 +784,7 @@ class Path(ParamType): if not self.file_okay and stat.S_ISREG(st.st_mode): self.fail( _("{name} {filename!r} is a file.").format( - name=self.name.title(), filename=filename_to_ui(value) + name=self.name.title(), filename=os.fsdecode(value) ), param, ctx, @@ -793,7 +792,7 @@ class Path(ParamType): if not self.dir_okay and stat.S_ISDIR(st.st_mode): self.fail( _("{name} {filename!r} is a directory.").format( - name=self.name.title(), filename=filename_to_ui(value) + name=self.name.title(), filename=os.fsdecode(value) ), param, ctx, @@ -801,7 +800,7 @@ class Path(ParamType): if self.writable and not os.access(value, os.W_OK): self.fail( _("{name} {filename!r} is not writable.").format( - name=self.name.title(), filename=filename_to_ui(value) + name=self.name.title(), filename=os.fsdecode(value) ), param, ctx, @@ -809,7 +808,7 @@ class Path(ParamType): if self.readable and not os.access(value, os.R_OK): self.fail( _("{name} {filename!r} is not readable.").format( - name=self.name.title(), filename=filename_to_ui(value) + name=self.name.title(), filename=os.fsdecode(value) ), param, ctx, diff --git a/src/click/utils.py b/src/click/utils.py index 4a52478..41ce001 100644 --- a/src/click/utils.py +++ b/src/click/utils.py @@ -7,7 +7,6 @@ from ._compat import _default_text_stdout from ._compat import _find_binary_writer from ._compat import auto_wrap_for_ansi from ._compat import binary_streams -from ._compat import filename_to_ui from ._compat import get_filesystem_encoding from ._compat import get_strerror from ._compat import is_bytes @@ -355,7 +354,8 @@ def format_filename(filename, shorten=False): """ if shorten: filename = os.path.basename(filename) - return filename_to_ui(filename) + + return os.fsdecode(filename) def get_app_dir(app_name, roaming=True, force_posix=False): |
