dflook / python-minifier

Transform Python source code into its most compact representation
MIT License
553 stars 40 forks source link

UnicodeEncodeError #57

Closed NadieFiind closed 1 year ago

NadieFiind commented 1 year ago

An issue related to this is already closed. But it seems that it is not yet completely fixed.

The error occurs on emojis.

This is the exact command I used:

pyminify folder --in-place --remove-literal-statements

Versions:

dflook commented 1 year ago

Hello @NadieFiind, can you share the source you are trying to minify and the output from pyminify?

NadieFiind commented 1 year ago

Also, I have only encountered this error on Windows. But it works fine on Linux.

Source: https://github.com/pyfyre/pyfyre/blob/rewrite/pyfyre_cli/user/src/index.py Specifically on line 8 at the emojis. If I remove the emojis, pyminify works fine.

Output:

__temp__\index.py
Traceback (most recent call last):
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.2032.0_x64__qbz5n2kfra8p0\lib\runpy.py", line 196, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.2032.0_x64__qbz5n2kfra8p0\lib\runpy.py", line 86, in _run_code
    exec(code, run_globals)
  File "C:\Users\infan\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\Scripts\pyminify.exe\__main__.py", line 7, in <module>
  File "C:\Users\infan\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\python_minifier\__main__.py", line 64, in main
    f.write(minified)
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.2032.0_x64__qbz5n2kfra8p0\lib\encodings\cp1252.py", line 19, in encode
    return codecs.charmap_encode(input,self.errors,encoding_table)[0]
UnicodeEncodeError: 'charmap' codec can't encode characters in position 177-178: character maps to <undefined>
dflook commented 1 year ago

Thanks. I'm not familiar with windows but it looks like python is using a different encoding instead of utf-8. Could you set the environment variable PYTHONUTF8=1 and see if it works?

NadieFiind commented 1 year ago

@dflook Oh yes, it works now. Thanks!