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

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

Open
frayien opened this issue Jul 8, 2022 · 1 comment
Open

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

frayien opened this issue Jul 8, 2022 · 1 comment

Comments

@frayien
Copy link

frayien commented Jul 8, 2022

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
Copy link
Owner

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants