Skip to content

Commit 31ef1bf

Browse files
Fix for subprocess.run() encoding on Windows (#2283)
* Switch console to UTF-8 while running subprocess.run on Windows * Fix "charmap" error on Windows * Path to docs
1 parent dcf6f60 commit 31ef1bf

File tree

3 files changed

+32
-23
lines changed

3 files changed

+32
-23
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
# 0.18.0
44

5-
* `flet build` command to package Flet app for any platform ([#2271](https://github.com/flet-dev/flet/issues/2271)).
5+
* `flet build` command to package Flet app for any platform ([docs](https://flet.dev/docs/guides/python/packaging-app-for-distribution)).
66
* Added TextStyle for the Text control ([#2270](https://github.com/flet-dev/flet/issues/2270)).
77
* Refactor code, add Enum deprecation utils ([#2259](https://github.com/flet-dev/flet/issues/2259)).
88
* `CupertinoAppBar` control ([#2278](https://github.com/flet-dev/flet/issues/2278)).

package/CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# 0.18.0
22

3-
* `flet build` command to package Flet app for any platform ([#2271](https://github.com/flet-dev/flet/issues/2271)).
3+
* `flet build` command to package Flet app for any platform ([docs](https://flet.dev/docs/guides/python/packaging-app-for-distribution)).
44
* Added TextStyle for the Text control ([#2270](https://github.com/flet-dev/flet/issues/2270)).
55
* Refactor code, add Enum deprecation utils ([#2259](https://github.com/flet-dev/flet/issues/2259)).
66
* `CupertinoAppBar` control ([#2278](https://github.com/flet-dev/flet/issues/2278)).

sdk/python/packages/flet/src/flet/cli/commands/build.py

Lines changed: 30 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,12 @@
1313
import yaml
1414
from flet.cli.commands.base import BaseCommand
1515
from flet_core.utils import random_string, slugify
16-
from flet_runtime.utils import copy_tree
16+
from flet_runtime.utils import copy_tree, is_windows
1717
from rich import print
1818

19+
if is_windows():
20+
from ctypes import windll
21+
1922
PYODIDE_ROOT_URL = "https://cdn.jsdelivr.net/pyodide/v0.24.1/full"
2023

2124

@@ -509,11 +512,8 @@ def fallback_image(yaml_path: str, images: list):
509512

510513
# generate icons
511514
print("Generating app icons...", end="")
512-
icons_result = subprocess.run(
513-
[dart_exe, "run", "flutter_launcher_icons"],
514-
cwd=str(self.flutter_dir),
515-
capture_output=self.verbose < 2,
516-
text=True,
515+
icons_result = self.run(
516+
[dart_exe, "run", "flutter_launcher_icons"], cwd=str(self.flutter_dir)
517517
)
518518
if icons_result.returncode != 0:
519519
if icons_result.stdout:
@@ -527,11 +527,9 @@ def fallback_image(yaml_path: str, images: list):
527527
# generate splash
528528
if target_platform in ["web", "ipa", "apk", "aab"]:
529529
print("Generating splash screens...", end="")
530-
splash_result = subprocess.run(
530+
splash_result = self.run(
531531
[dart_exe, "run", "flutter_native_splash:create"],
532532
cwd=str(self.flutter_dir),
533-
capture_output=self.verbose < 2,
534-
text=True,
535533
)
536534
if splash_result.returncode != 0:
537535
if splash_result.stdout:
@@ -586,12 +584,7 @@ def fallback_image(yaml_path: str, images: list):
586584
]
587585
)
588586

589-
package_result = subprocess.run(
590-
package_args,
591-
cwd=str(self.flutter_dir),
592-
capture_output=self.verbose < 2,
593-
text=True,
594-
)
587+
package_result = self.run(package_args, cwd=str(self.flutter_dir))
595588

596589
if package_result.returncode != 0:
597590
if package_result.stdout:
@@ -629,12 +622,7 @@ def fallback_image(yaml_path: str, images: list):
629622
if self.verbose > 0:
630623
print(build_args)
631624

632-
build_result = subprocess.run(
633-
build_args,
634-
cwd=str(self.flutter_dir),
635-
capture_output=self.verbose < 2,
636-
text=True,
637-
)
625+
build_result = self.run(build_args, cwd=str(self.flutter_dir))
638626

639627
if build_result.returncode != 0:
640628
if build_result.stdout:
@@ -703,6 +691,27 @@ def copy_icon_image(self, src_path: Path, dest_path: Path, image_name: str):
703691
return Path(images[0]).name
704692
return None
705693

694+
def run(self, args, cwd):
695+
if is_windows():
696+
# Source: https://stackoverflow.com/a/77374899/1435891
697+
# Save the current console output code page and switch to 65001 (UTF-8)
698+
previousCp = windll.kernel32.GetConsoleOutputCP()
699+
windll.kernel32.SetConsoleOutputCP(65001)
700+
701+
r = subprocess.run(
702+
args,
703+
cwd=cwd,
704+
capture_output=self.verbose < 2,
705+
text=True,
706+
encoding="utf8",
707+
)
708+
709+
if is_windows():
710+
# Restore the previous output console code page.
711+
windll.kernel32.SetConsoleOutputCP(previousCp)
712+
713+
return r
714+
706715
def cleanup(self, exit_code: int):
707716
if self.verbose > 0:
708717
print(f"Deleting Flutter bootstrap directory {self.flutter_dir}")

0 commit comments

Comments
 (0)