Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot decode output when exec is not found on Windows #7

Closed
pawamoy opened this issue Oct 6, 2020 · 1 comment
Closed

Cannot decode output when exec is not found on Windows #7

pawamoy opened this issue Oct 6, 2020 · 1 comment
Labels
bug Something isn't working windows Windows OS

Comments

@pawamoy
Copy link
Owner

pawamoy commented Oct 6, 2020

Describe the bug
It seems CMD is not using UTF-8 by default. When a command fail (for example when an exec is not found, see #6), and failprint tries to decode the output, it fails.

To Reproduce
Same command as in #6.

Expected behavior
The error message is at least properly decoded and printed by failprint.

System (please complete the following information):

  • failprint version: 0.4.1
  • Python version: 3.6
  • OS: Windows

Additional context
The encoding used by the console can be obtained with the command chcp. It prints a sentence in the current locale with, I hope always in this format, the "code page" at the end, something like Page de codes active : 850. This code page can then be used to decode the output. We could do something like this:

try:
    _, chcp_output = _run_process(["chcp"])
    os_encoding = re.search(r"(\d+)").group(1)  # code page does not lose characters
except UnicodeDecodeError:
    import locale  # locale sometimes lose characters (é -> ,)
    os_encoding = locale.getpreferredencoding()

try:
    stdout = stdout.decode("utf8")
except UnicodeDecodeError:
    if not os_encoding:
        raise
    stdout = stdout.decode(os_encoding)
@pawamoy
Copy link
Owner Author

pawamoy commented Oct 11, 2020

Should be fixed in version 0.6.0.

@pawamoy pawamoy closed this as completed Oct 11, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working windows Windows OS
Projects
None yet
Development

No branches or pull requests

1 participant