hhatto / autopep8

A tool that automatically formats Python code to conform to the PEP 8 style guide.
https://pypi.org/project/autopep8/
MIT License
4.56k stars 288 forks source link

formatter does not work if it contains a specific string #671

Closed LCH-1 closed 1 year ago

LCH-1 commented 1 year ago

Python Code


YOUR CODE

# normal case
def test():
    pass
def test2():
    pass

# issue case
def test():
    # テスト
    pass
def test2():
    pass

Command Line and Configuration

.pep8, setup.cfg, (using default setting)

[pep8]

Command Line(in windows cmd)

type test.py | python -m autopep8 -

Your Environment

An issue occurred while using autopep8 extension in vscode. (https://github.com/microsoft/vscode-autopep8/issues/51)

Normally, it works fine. But it doesn't work if it contains specific characters.

issue

(venv) C:\Users\user\Desktop\test>type test.py | python -m autopep8 -
Traceback (most recent call last):
  File "<frozen runpy>", line 198, in _run_module_as_main
  File "<frozen runpy>", line 88, in _run_code
  File "C:\Users\user\Desktop\test\venv\Lib\site-packages\autopep8.py", line 4560, in <module>
    sys.exit(main())
             ^^^^^^
  File "C:\Users\user\Desktop\test\venv\Lib\site-packages\autopep8.py", line 4506, in main
    wrap_output(sys.stdout, encoding=encoding).write(fixed_stdin)
UnicodeEncodeError: 'cp949' codec can't encode character '\udce3' in position 18: illegal multibyte sequence

(venv) C:\Users\user\Desktop\test>

Thank you

Vitotian commented 1 year ago

Same issue with Chinese characters

UnicodeEncodeError: 'gbk' codec can't encode character '\udcb0' in position 450: illegal multibyte sequence
sunrise2575 commented 1 year ago

Same issue happens in Korean characters

2023-05-21 18:08:06.772 [info] [Trace - 오후 6:08:06] Received notification 'window/logMessage'.
2023-05-21 18:08:06.772 [info] Traceback (most recent call last):
  File "c:\Users\heeyong\miniconda3\lib\runpy.py", line 197, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "c:\Users\heeyong\miniconda3\lib\runpy.py", line 87, in _run_code
    exec(code, run_globals)
  File "c:\Users\heeyong\.vscode\extensions\ms-python.autopep8-2023.4.0\bundled\libs\autopep8.py", line 4570, in <module>
    sys.exit(main())
  File "c:\Users\heeyong\.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: 'cp949' codec can't encode character '\udcec' in position 275: illegal multibyte sequence

2023-05-21 18:08:06.772 [info] [Trace - 오후 6:08:06] Received response 'textDocument/formatting - (1)' in 239ms.
2023-05-21 18:10:14.248 [info] [Trace - 오후 6:10:14] Sending notification 'textDocument/didOpen'.
LCH-1 commented 1 year ago

This issue appears to have been resolved by https://github.com/microsoft/vscode-autopep8/commit/91b79efc0d09cdfd2ed2548be4450e7036ee19e4.