瀏覽代碼

-D std-encoding-auto to not force utf8 on stdout/stderr (closes #8586)

Aleksandr Kuzmenko 5 年之前
父節點
當前提交
96425277e3
共有 4 個文件被更改,包括 21 次插入9 次删除
  1. 6 0
      src-json/define.json
  2. 9 7
      src/generators/genpy.ml
  3. 4 2
      std/cs/Boot.hx
  4. 2 0
      std/java/Init.hx

+ 6 - 0
src-json/define.json

@@ -550,6 +550,12 @@
 		"define": "static",
 		"doc": "Defined if the current target is static."
 	},
+	{
+		"name": "StdEncodingAuto",
+		"define": "std-encoding-auto",
+		"doc": "Use default encoding for stdin, stdout and stderr instead of forcing utf-8",
+		"platforms": ["java", "cs", "python"]
+	},
 	{
 		"name": "Swc",
 		"define": "swc",

+ 9 - 7
src/generators/genpy.ml

@@ -2505,13 +2505,15 @@ module Generator = struct
 		if has_feature ctx "closure_Array" || has_feature ctx "closure_String" then
 			spr ctx "from functools import partial as _hx_partial\n";
 		spr ctx "import sys\n";
-		spr ctx "try:\n";
-		spr ctx "    if sys.stdout.encoding != 'utf-8':\n";
-		spr ctx "        sys.stdout = open(sys.stdout.fileno(), mode='w', encoding='utf8', buffering=1)\n";
-		spr ctx "    if sys.stderr.encoding != 'utf-8':\n";
-		spr ctx "        sys.stderr = open(sys.stderr.fileno(), mode='w', encoding='utf8', buffering=1)\n";
-		spr ctx "except:\n";
-		spr ctx "    pass\n";
+		if not (defined com Define.StdEncodingAuto) then begin
+			spr ctx "try:\n";
+			spr ctx "    if sys.stdout.encoding != 'utf-8':\n";
+			spr ctx "        sys.stdout = open(sys.stdout.fileno(), mode='w', encoding='utf8', buffering=1)\n";
+			spr ctx "    if sys.stderr.encoding != 'utf-8':\n";
+			spr ctx "        sys.stderr = open(sys.stderr.fileno(), mode='w', encoding='utf8', buffering=1)\n";
+			spr ctx "except:\n";
+			spr ctx "    pass\n";
+		end;
 		gen_imports ctx;
 		gen_resources ctx;
 		gen_types ctx;

+ 4 - 2
std/cs/Boot.hx

@@ -41,8 +41,10 @@ import Reflect;
 @:dox(hide)
 class Boot {
 	@:keep public static function init():Void {
-		cs.system.Console.InputEncoding = new cs.system.text.UTF8Encoding();
-		cs.system.Console.OutputEncoding = new cs.system.text.UTF8Encoding();
+		#if !std_encoding_auto
+			cs.system.Console.InputEncoding = new cs.system.text.UTF8Encoding();
+			cs.system.Console.OutputEncoding = new cs.system.text.UTF8Encoding();
+		#end
 		cs.Lib.applyCultureChanges();
 	}
 }

+ 2 - 0
std/java/Init.hx

@@ -23,9 +23,11 @@ package java;
 
 @:native("haxe.java.Init") @:keep class Init {
 	public static function init():Void {
+		#if !std_encoding_auto
 		try {
 			java.lang.System.setOut(new java.io.PrintStream(java.lang.System.out, true, "utf-8"));
 			java.lang.System.setErr(new java.io.PrintStream(java.lang.System.err, true, "utf-8"));
 		} catch (e:java.io.UnsupportedEncodingException) {}
+		#end
 	}
 }