r0x0r / pywebview

Build GUI for your Python program with JavaScript, HTML, and CSS
https://pywebview.flowrl.com
BSD 3-Clause "New" or "Revised" License
4.65k stars 540 forks source link

UnicodeDecodeError when calling `pywebview.api.*` from webview #1470

Open patarapolw opened 2 days ago

patarapolw commented 2 days ago

Specification

Description

Not while developing, but running the program packaged with PyInstaller 6.10.0 --noconsole.

pywebview.api.* doesn't work if Chinese characters are included in the arguments. The error is visible in the JavaScript console, not in the terminal.

image

Practicalities

r0x0r commented 2 days ago

Can you find out where in the pywebview code this error originates from?

patarapolw commented 2 days ago

image

The error occurs on posting to Python, always at VM25:488, which is

image

r0x0r commented 1 day ago

This error occurs on the Python side, when you call you function via JS API. See webview/util.py lines 214-222. I would start by checking code in your function first.

patarapolw commented 1 day ago

For example, both of these functions give the same errors.

    def log(self, obj):
        pprint(obj, indent=1, sort_dicts=False)

    def mark(self, v: str, t: str):
        # v is a Chinese word. t is an English keyword.

While functions that only send Chinese letters to the web are done successful without errors.

    def get_stats(self):
        return self.latest_stats  # dict containing Chinese characters.

Note that the errors only occur after running the app packaged with pyinstaller --noconsole, while packaging with console doesn't bear errors.

r0x0r commented 1 day ago

This is a Pyinstaller issue. Here is for example an old Stack Overflow thread https://stackoverflow.com/questions/47692960/error-when-using-pyinstaller-unicodedecodeerror-utf-8-codec-cant-decode-byt According to this it should have been fixed, but apparently the issue still persists.

patarapolw commented 1 day ago

Not the same issue, at least; as I can compile, with or without --noconsole.

My guess is, on compiling with --noconsole, there is no way to set encoding, and it defaults to Windows's. On with console, some unknown env var might do something. (PYTHONUTF8 and PYTHONIOENCODING aren't set.)