فهرست منبع

for historic reasons pythons bool extends int, fixes #6549

frabbit 7 سال پیش
والد
کامیت
131568ed90
3فایلهای تغییر یافته به همراه40 افزوده شده و 3 حذف شده
  1. 6 2
      src/generators/genpy.ml
  2. 2 1
      std/python/Boot.hx
  3. 32 0
      tests/unit/src/unit/issues/Issue6549.hx

+ 6 - 2
src/generators/genpy.ml

@@ -1469,13 +1469,17 @@ module Printer = struct
 			let assign = if is_empty_expr then "" else Printf.sprintf "%s = _hx_e1\n%s" v.v_name indent in
 			let handle_base_type bt =
 				let t = print_base_type bt in
+				let print_custom_check t_str =
+					Printf.sprintf "if %s:\n%s    %s    %s" t_str indent assign (print_expr {pctx with pc_indent = "    " ^ pctx.pc_indent} e)
+				in
 				let print_type_check t_str =
-					Printf.sprintf "if isinstance(_hx_e1, %s):\n%s    %s    %s" t_str indent assign (print_expr {pctx with pc_indent = "    " ^ pctx.pc_indent} e)
+					print_custom_check ("isinstance(_hx_e1, " ^ t_str ^ ")")
+					(*Printf.sprintf "if isinstance(_hx_e1, %s):\n%s    %s    %s" t_str indent assign (print_expr {pctx with pc_indent = "    " ^ pctx.pc_indent} e)*)
 				in
 				let res = match t with
 				| "str" -> print_type_check "str"
 				| "Bool" -> print_type_check "bool"
-				| "Int" -> print_type_check "int"
+				| "Int" -> print_custom_check "(isinstance(_hx_e1, int) and not isinstance(_hx_e1, bool))" (* for historic reasons bool extends int *)
 				| "Float" -> print_type_check "float"
 				| t -> print_type_check t
 				in

+ 2 - 1
std/python/Boot.hx

@@ -60,7 +60,8 @@ class Boot {
 	}
 
 	inline static function isPyInt(o:Dynamic):Bool {
-		return UBuiltins.isinstance(o, UBuiltins.int);
+		// for historic reasons bool extends int
+		return UBuiltins.isinstance(o, UBuiltins.int) && !isPyBool(o);
 	}
 
 	inline static function isPyFloat(o:Dynamic):Bool {

+ 32 - 0
tests/unit/src/unit/issues/Issue6549.hx

@@ -0,0 +1,32 @@
+package unit.issues;
+
+class Issue6549 extends unit.Test {
+
+	function test () {
+		var int = 1;
+		var x = try {
+			throw true;
+		} catch (err:Int) {
+			"int";
+		}
+		catch (err:Dynamic) {
+			"dynamic";
+		}
+
+		eq(x, "dynamic");
+
+		var x = try {
+			throw true;
+		} catch (err:Int) {
+			"int";
+		}
+		catch (err:Bool) {
+			"bool";
+		}
+		catch (err:Dynamic) {
+			"dynamic";
+		}
+
+		eq(x, "bool");
+	}
+}