From ae9d2e50ec19c25e3dc5a9feec114b09ad9f5f19 Mon Sep 17 00:00:00 2001 From: svlandeg Date: Thu, 25 Apr 2024 13:04:39 +0200 Subject: [PATCH 1/4] loop through different possible encodings --- typer/_completion_shared.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/typer/_completion_shared.py b/typer/_completion_shared.py index 10de54420d..f8d79ba7f2 100644 --- a/typer/_completion_shared.py +++ b/typer/_completion_shared.py @@ -183,15 +183,15 @@ def install_powershell(*, prog_name: str, complete_var: str, shell: str) -> Path if isinstance(result.stdout, str): # pragma: no cover 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: no cover + for encoding in ["windows-1252", "utf8", "cp850"]: try: - path_str = result.stdout.decode("utf8") - except UnicodeDecodeError: - click.echo("Couldn't decode the path automatically", err=True) - raise + path_str = result.stdout.decode(encoding) + break + except UnicodeDecodeError: # pragma: no cover + pass + if not path_str: + click.echo("Couldn't decode the path automatically", err=True) + raise click.exceptions.Exit(1) path_obj = Path(path_str.strip()) parent_dir: Path = path_obj.parent parent_dir.mkdir(parents=True, exist_ok=True) From 0326567cfde287584cf79db10f1031ed6c48b815 Mon Sep 17 00:00:00 2001 From: svlandeg Date: Thu, 25 Apr 2024 13:17:38 +0200 Subject: [PATCH 2/4] add no cover --- 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 f8d79ba7f2..0f52f6a82c 100644 --- a/typer/_completion_shared.py +++ b/typer/_completion_shared.py @@ -189,7 +189,7 @@ def install_powershell(*, prog_name: str, complete_var: str, shell: str) -> Path break except UnicodeDecodeError: # pragma: no cover pass - if not path_str: + if not path_str: # pragma: no cover click.echo("Couldn't decode the path automatically", err=True) raise click.exceptions.Exit(1) path_obj = Path(path_str.strip()) From 9de881991a8394575beb226b1ec0d5a14dcb6eb8 Mon Sep 17 00:00:00 2001 From: svlandeg Date: Thu, 25 Apr 2024 16:25:07 +0200 Subject: [PATCH 3/4] add the path.exists check --- typer/_completion_shared.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/typer/_completion_shared.py b/typer/_completion_shared.py index 0f52f6a82c..e55badd989 100644 --- a/typer/_completion_shared.py +++ b/typer/_completion_shared.py @@ -183,13 +183,16 @@ def install_powershell(*, prog_name: str, complete_var: str, shell: str) -> Path if isinstance(result.stdout, str): # pragma: no cover path_str = result.stdout if isinstance(result.stdout, bytes): + path_obj = None for encoding in ["windows-1252", "utf8", "cp850"]: try: - path_str = result.stdout.decode(encoding) - break + path_str = result.stdout.decode(encoding).strip() + if os.path.exists(path_str): + path_obj = Path(path_str) + break except UnicodeDecodeError: # pragma: no cover pass - if not path_str: # pragma: no cover + if not path_obj: # pragma: no cover click.echo("Couldn't decode the path automatically", err=True) raise click.exceptions.Exit(1) path_obj = Path(path_str.strip()) From a115c8dc414c999ba706ba3e65b23acdcaae0671 Mon Sep 17 00:00:00 2001 From: svlandeg Date: Thu, 25 Apr 2024 16:31:16 +0200 Subject: [PATCH 4/4] revert the path.exists check (for now) --- typer/_completion_shared.py | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/typer/_completion_shared.py b/typer/_completion_shared.py index e55badd989..0f52f6a82c 100644 --- a/typer/_completion_shared.py +++ b/typer/_completion_shared.py @@ -183,16 +183,13 @@ def install_powershell(*, prog_name: str, complete_var: str, shell: str) -> Path if isinstance(result.stdout, str): # pragma: no cover path_str = result.stdout if isinstance(result.stdout, bytes): - path_obj = None for encoding in ["windows-1252", "utf8", "cp850"]: try: - path_str = result.stdout.decode(encoding).strip() - if os.path.exists(path_str): - path_obj = Path(path_str) - break + path_str = result.stdout.decode(encoding) + break except UnicodeDecodeError: # pragma: no cover pass - if not path_obj: # pragma: no cover + if not path_str: # pragma: no cover click.echo("Couldn't decode the path automatically", err=True) raise click.exceptions.Exit(1) path_obj = Path(path_str.strip())