Grio43 commited on
Commit
8741801
·
verified ·
1 Parent(s): f7570f9

web_interface: fix run.bat fallback + UTF-8 console handling

Browse files

run.bat:
- Use enabledelayedexpansion so the python/python3 fallback after a missing py launcher actually runs (the previous %ERRORLEVEL% in the parenthesized else block was expanded at parse time, making the fallback unreachable).
- Set chcp 65001, PYTHONUTF8=1, PYTHONIOENCODING=utf-8 so prints with em-dashes do not crash on cp1252/cp932 consoles.

app.py: reconfigure stdout/stderr with errors=replace at startup so non-UTF-8 consoles cannot crash the launcher with UnicodeEncodeError.

Files changed (2) hide show
  1. web_interface/app.py +9 -0
  2. web_interface/run.bat +40 -15
web_interface/app.py CHANGED
@@ -645,6 +645,15 @@ def _run_app() -> None:
645
  # ---------------------------------------------------------------------------
646
 
647
  def main() -> None:
 
 
 
 
 
 
 
 
 
648
  force = "--reinstall" in sys.argv[1:]
649
  if not _in_target_venv():
650
  _bootstrap(force_reinstall=force)
 
645
  # ---------------------------------------------------------------------------
646
 
647
  def main() -> None:
648
+ # On Windows, the console codepage is often cp1252/cp932/etc., not UTF-8.
649
+ # Our messages contain em-dashes and × — `errors="replace"` keeps them from
650
+ # crashing the bootstrap with UnicodeEncodeError on those consoles.
651
+ for stream in (sys.stdout, sys.stderr):
652
+ try:
653
+ stream.reconfigure(errors="replace")
654
+ except (AttributeError, OSError):
655
+ pass
656
+
657
  force = "--reinstall" in sys.argv[1:]
658
  if not _in_target_venv():
659
  _bootstrap(force_reinstall=force)
web_interface/run.bat CHANGED
@@ -1,25 +1,50 @@
1
  @echo off
2
- setlocal
3
  cd /d "%~dp0"
4
 
5
- rem Prefer the per-user py launcher; fall back to python on PATH.
 
 
 
 
 
 
 
 
 
 
6
  where py >nul 2>nul
7
- if %ERRORLEVEL%==0 (
8
- py "%~dp0app.py" %*
9
- ) else (
10
  where python >nul 2>nul
11
- if %ERRORLEVEL%==0 (
12
- python "%~dp0app.py" %*
13
- ) else (
14
- echo [run.bat] No Python found. Install Python 3.10+ from python.org or the Microsoft Store.
15
- pause
16
- exit /b 1
17
- )
18
  )
19
 
20
- if %ERRORLEVEL% NEQ 0 (
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
21
  echo.
22
- echo [run.bat] app.py exited with code %ERRORLEVEL%.
23
  pause
24
  )
25
- endlocal
 
1
  @echo off
2
+ setlocal enabledelayedexpansion
3
  cd /d "%~dp0"
4
 
5
+ rem Force a UTF-8 console + UTF-8 mode in Python so prints with em-dashes and
6
+ rem the like don't crash on machines whose console codepage is cp1252 / cp932.
7
+ chcp 65001 >nul 2>nul
8
+ set "PYTHONUTF8=1"
9
+ set "PYTHONIOENCODING=utf-8"
10
+
11
+ rem Pick a Python launcher. Try py first (works on most python.org installs),
12
+ rem then `python`, then `python3`. enabledelayedexpansion + !ERRORLEVEL! is
13
+ rem required so each check reads the value AFTER the preceding `where` runs.
14
+ set "PYCMD="
15
+
16
  where py >nul 2>nul
17
+ if !ERRORLEVEL! EQU 0 set "PYCMD=py"
18
+
19
+ if not defined PYCMD (
20
  where python >nul 2>nul
21
+ if !ERRORLEVEL! EQU 0 set "PYCMD=python"
22
+ )
23
+
24
+ if not defined PYCMD (
25
+ where python3 >nul 2>nul
26
+ if !ERRORLEVEL! EQU 0 set "PYCMD=python3"
 
27
  )
28
 
29
+ if not defined PYCMD (
30
+ echo [run.bat] No Python found on PATH.
31
+ echo [run.bat] Install Python 3.10+ from https://www.python.org/downloads/
32
+ echo [run.bat] During install, tick "Add Python to PATH".
33
+ pause
34
+ exit /b 1
35
+ )
36
+
37
+ echo [run.bat] Using !PYCMD! to launch app.py.
38
+ echo [run.bat] First run creates .venv and installs ~500 MB of packages.
39
+ echo [run.bat] That can take several minutes; it only happens once.
40
+ echo.
41
+
42
+ !PYCMD! "%~dp0app.py" %*
43
+ set "EXITCODE=!ERRORLEVEL!"
44
+
45
+ if !EXITCODE! NEQ 0 (
46
  echo.
47
+ echo [run.bat] app.py exited with code !EXITCODE!.
48
  pause
49
  )
50
+ endlocal & exit /b %EXITCODE%