diff --git a/typer/_completion_shared.py b/typer/_completion_shared.py index 10de54420d..6cc3793d91 100644 --- a/typer/_completion_shared.py +++ b/typer/_completion_shared.py @@ -183,15 +183,19 @@ 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 + 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 + pass + if path_obj is None: + 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)