R-O-C-K-E-T / Factorio-SAT

Enhancing the Factorio experience with SAT solvers
GNU General Public License v3.0
613 stars 16 forks source link

[Windows 10][Powershell][Python 3.9] UTF-8 BOM #10

Open frayien opened 2 years ago

frayien commented 2 years ago

Hi, im a using Windows 10 with Powershell and python3.9 and I have trouble with file encoding. When saving/printing the app uses the 'utf-8-sig' encoding thus includes 2 bytes to specify the format (UTF-8 BOM). But when reading utf-8 is assumed thus the parser fails. Example with the README example :

python3.9.exe belt_balancer.py --fast --all networks/4x4 10 4 | python3.9.exe render.py

output :

pygame 2.1.2 (SDL 2.0.18, Python 3.9.13)
Hello from the pygame community. https://www.pygame.org/contribute.html
Traceback (most recent call last):
  File "D:\GitHub\Factorio-SAT\render.py", line 547, in <module>
    solution = np.array(json.loads(input()), ndmin=2)
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.9_3.9.3568.0_x64__qbz5n2kfra8p0\lib\json\__init__.py", line 335, in loads
    raise JSONDecodeError("Unexpected UTF-8 BOM (decode using utf-8-sig)",
json.decoder.JSONDecodeError: Unexpected UTF-8 BOM (decode using utf-8-sig): line 1 column 1 (char 0)

I can make it work by replacing line 547

solution = np.array(json.loads(input()), ndmin=2)

with

solution = np.array(json.loads(input().encode().decode('utf-8-sig')), ndmin=2)

(fix found here : https://www.howtosolutions.net/2019/04/python-fixing-unexpected-utf-8-bom-error-when-loading-json-data/)

But this is more like a workaround and dont fix the root issue.

The problem appear every time the app tries to read a file it itself wrote, for example when using the --partial argument, I did not find a workaround for this one.

I tried to launch the app with WSL Debian for comparison but the pip installation fails for reasons I am still investigating.

R-O-C-K-E-T commented 4 months ago

Could try running python with -X to force utf8. e.g. python3.9.exe -X belt_balancer.py --fast --all networks/4x4 10 4 | python3.9.exe -X render.py. If that fixes it, then I think you can do the same thing by pasting sys.stdin.reconfigure(encoding='utf-8'); sys.stdout.reconfigure(encoding='utf-8') at the top of every file?

Either way, probably best to use wsl.