microsoft/vscode-autopep8

UnicodeEncodeError when formatting code including Japanese characters on Windows

kzkadc opened this issue · 1 comments

kzkadc commented

When formatting my code using autopep8 in VSCode, I encounter the following error:

2023-06-03 11:50:30.339 [info] c:\Users\xxxxx\AppData\Local\Programs\Python\Python310\python.exe -m autopep8 -
2023-06-03 11:50:30.340 [info] [Trace - 11:50:30] Received notification 'window/logMessage'.
2023-06-03 11:50:30.340 [info] CWD Server: c:\Users\xxxxx\Desktop\test
2023-06-03 11:50:30.554 [info] [Trace - 11:50:30] Received notification 'window/logMessage'.
2023-06-03 11:50:30.554 [info] Traceback (most recent call last):
  File "c:\Users\xxxxx\AppData\Local\Programs\Python\Python310\lib\runpy.py", line 196, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "c:\Users\xxxxx\AppData\Local\Programs\Python\Python310\lib\runpy.py", line 86, in _run_code
    exec(code, run_globals)
  File "c:\Users\xxxxx\.vscode\extensions\ms-python.autopep8-2023.4.0\bundled\libs\autopep8.py", line 4570, in <module>
    sys.exit(main())
  File "c:\Users\xxxxx\.vscode\extensions\ms-python.autopep8-2023.4.0\bundled\libs\autopep8.py", line 4516, in main
    wrap_output(sys.stdout, encoding=encoding).write(fixed_stdin)
UnicodeEncodeError: 'cp932' codec can't encode character '\udc81' in position 948: illegal multibyte sequence

I am writing my code in UTF-8, but I noticed that this error occurs only when the code includes Japanese characters, such as:

print("こんにちは")

I also noticed that running autopep8 like the following way does not raise the error:

autopep8 test.py
# correctly formatted

but passing the code from stdin as is done in the above log results the error:

cat test.py | autopep8 -
# error

I would like to know if there are any solutions or workarounds available to resolve this problem.

Thank you.

This should be filed on autopep8, we don't control this behavior in the extension. Please file this issue here: https://github.com/hhatto/autopep8/issues