From e9ee0b04f323c6c2a648d1daf224f0ab1cd9213b Mon Sep 17 00:00:00 2001 From: Matej Froebe Date: Fri, 7 May 2021 09:26:46 +0200 Subject: [PATCH 1/4] Windows encoding bugfix Problem: When installing auto-completion in Powershell, the path to the profile file was decoded with windows-1252 encoding - but such path didn't exist in my case. The correct encoding was cp850. Solution: - try to decode with cp850 also - check if the decoded path exists (checking for UnicodeDecodeError isn't enough) --- typer/completion.py | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/typer/completion.py b/typer/completion.py index 4a0b01251d..cc48ee955c 100644 --- a/typer/completion.py +++ b/typer/completion.py @@ -282,16 +282,20 @@ def install_powershell(*, prog_name: str, complete_var: str, shell: str) -> Path if isinstance(result.stdout, str): # pragma: nocover path_str = result.stdout if isinstance(result.stdout, bytes): - try: - # PowerShell would be predominant in Windows - path_str = result.stdout.decode("windows-1252") - except UnicodeDecodeError: # pragma: nocover + path_obj = None + for encoding in ("windows-1252", "cp850", "utf8"): try: - path_str = result.stdout.decode("utf8") + path_str = result.stdout.decode(encoding).strip() + if os.path.exists(path_str): + path_obj = Path(path_str.strip()) + break except UnicodeDecodeError: - click.echo("Couldn't decode the path automatically", err=True) - raise click.exceptions.Exit(1) - path_obj = Path(path_str.strip()) + pass + + if path_obj is None: + click.echo("Couldn't decode the path automatically", err=True) + raise click.exceptions.Exit(1) + parent_dir: Path = path_obj.parent parent_dir.mkdir(parents=True, exist_ok=True) script_content = get_completion_script( From 1453cf360c982c2945ed67b28ec6e1705842329c Mon Sep 17 00:00:00 2001 From: Sofie Van Landeghem Date: Fri, 19 Apr 2024 17:11:17 +0200 Subject: [PATCH 2/4] Update typer/_completion_shared.py --- typer/_completion_shared.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/typer/_completion_shared.py b/typer/_completion_shared.py index 6cc3793d91..c708744b99 100644 --- a/typer/_completion_shared.py +++ b/typer/_completion_shared.py @@ -194,7 +194,7 @@ def install_powershell(*, prog_name: str, complete_var: str, shell: str) -> Path pass if path_obj is None: click.echo("Couldn't decode the path automatically", err=True) - raise click.exceptions.Exit(1) + raise path_obj = Path(path_str.strip()) parent_dir: Path = path_obj.parent From 97194bb763f1c80652aa077022b354da37c7e544 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 19 Apr 2024 15:12:12 +0000 Subject: [PATCH 3/4] =?UTF-8?q?=F0=9F=8E=A8=20[pre-commit.ci]=20Auto=20for?= =?UTF-8?q?mat=20from=20pre-commit.com=20hooks?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- typer/_completion_shared.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/typer/_completion_shared.py b/typer/_completion_shared.py index c708744b99..e091a10293 100644 --- a/typer/_completion_shared.py +++ b/typer/_completion_shared.py @@ -194,7 +194,7 @@ def install_powershell(*, prog_name: str, complete_var: str, shell: str) -> Path pass if path_obj is None: click.echo("Couldn't decode the path automatically", err=True) - raise + raise path_obj = Path(path_str.strip()) parent_dir: Path = path_obj.parent From 4429be5c22eb51b8ced8c4db6d37860dddba79ce Mon Sep 17 00:00:00 2001 From: Sofie Van Landeghem Date: Fri, 19 Apr 2024 17:40:03 +0200 Subject: [PATCH 4/4] update raise statement --- typer/_completion_shared.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/typer/_completion_shared.py b/typer/_completion_shared.py index e091a10293..6cc3793d91 100644 --- a/typer/_completion_shared.py +++ b/typer/_completion_shared.py @@ -194,7 +194,7 @@ def install_powershell(*, prog_name: str, complete_var: str, shell: str) -> Path pass if path_obj is None: click.echo("Couldn't decode the path automatically", err=True) - raise + raise click.exceptions.Exit(1) path_obj = Path(path_str.strip()) parent_dir: Path = path_obj.parent