Browse Source

Don't wrap all imports in try/except, instead support ignoreError=true flag in @:import meta.

Dan Korostelev 11 years ago
parent
commit
3577197503
5 changed files with 43 additions and 35 deletions
  1. 31 11
      genpy.ml
  2. 1 7
      std/python/lib/Msvcrt.hx
  3. 1 8
      std/python/lib/Termios.hx
  4. 1 9
      std/python/lib/Tty.hx
  5. 9 0
      tests/unit/TestPython.hx

+ 31 - 11
genpy.ml

@@ -1379,6 +1379,10 @@ module Generator = struct
 		cfd_methods : string list;
 	}
 
+	type import_type =
+		| IModule of string
+		| IObject of string * string
+
 	let mk_context com = {
 		com = com;
 		buf = Buffer.create 16000;
@@ -1704,22 +1708,40 @@ module Generator = struct
 				| path,name -> (ExtString.String.join "_" path) ^ "_" ^ name
 			in
 
-			let import = match args with
+			let import_type,ignore_error = match args with
 				| [(EConst(String(module_name)), _)] ->
+					IModule module_name, false
+				| [(EConst(String(module_name)), _); (EBinop(OpAssign, (EConst(Ident("ignoreError")),_), (EConst(Ident("true")),_)),_)] ->
+					IModule module_name,true
+				| [(EConst(String(module_name)), _); (EConst(String(object_name)), _)] ->
+					IObject (module_name,object_name), false
+				| [(EConst(String(module_name)), _); (EConst(String(object_name)), _); (EBinop(OpAssign, (EConst(Ident("ignoreError")),_), (EConst(Ident("true")),_)),_)] ->
+					IObject (module_name,object_name), true
+				| _ ->
+					error "Unsupported @:import format" mp
+			in
+
+			let import = match import_type with
+				| IModule module_name ->
 					(* importing whole module *)
-					"\timport " ^ module_name ^ " as " ^ class_name
+					"import " ^ module_name ^ " as " ^ class_name
 
-				| [(EConst(String(module_name)), _); (EConst(String(object_name)), _)] ->
+				| IObject (module_name,object_name) ->
 					if String.contains object_name '.' then
 						(* importing nested class *)
-						"\timport " ^ module_name ^ " as _hx_temp_import; " ^ class_name ^ " = _hx_temp_import." ^ object_name ^ "; del _hx_temp_import"
+						"import " ^ module_name ^ " as _hx_temp_import; " ^ class_name ^ " = _hx_temp_import." ^ object_name ^ "; del _hx_temp_import"
 					else
 						(* importing a class from a module *)
-						"\tfrom " ^ module_name ^ " import " ^ object_name ^ " as " ^ class_name
-				| _ ->
-					error "Unsupported @:import format" mp
+						"from " ^ module_name ^ " import " ^ object_name ^ " as " ^ class_name
 			in
-			spr_line ctx import
+
+			if ignore_error then begin
+				spr_line ctx "try:";
+				spr ctx "\t";
+				spr_line ctx import;
+				spr_line ctx "except:\n\tpass"
+			end else
+				spr_line ctx import
 		end
 
 	let gen_class ctx c =
@@ -1877,13 +1899,11 @@ module Generator = struct
 		end
 
 	let gen_imports ctx =
-		spr ctx "try:\n";
 		List.iter (fun mt ->
 			match mt with
 			| TClassDecl c when c.cl_extern -> gen_import ctx c
 			| _ -> ()
-		) ctx.com.types;
-		spr ctx "except:\n\tpass\n"
+		) ctx.com.types
 
 	let gen_types ctx =
 		let used_paths = Hashtbl.create 0 in

+ 1 - 7
std/python/lib/Msvcrt.hx

@@ -1,15 +1,9 @@
 
 package python.lib;
 
+@:import("msvcrt", ignoreError=true)
 extern class Msvcrt {
 
 	public static function getch ():python.lib.Bytes;
 
-	static function __init__ ():Void
-	{
-		try {
-			python.Syntax.importAs("msvcrt", "python.lib.Msvcrt");
-		} catch (e:Dynamic) {}
-	}
-
 }

+ 1 - 8
std/python/lib/Termios.hx

@@ -3,6 +3,7 @@ package python.lib;
 
 abstract TermiosSettings(Dynamic) {}
 
+@:import("termios", ignoreError=true)
 extern class Termios {
 
 	public static var TCSADRAIN : Int;
@@ -12,12 +13,4 @@ extern class Termios {
 
 	public static function tcsetattr (fileNo:Int, when:Int, settings:TermiosSettings):Void;
 
-	static function __init__ ():Void
-	{
-		try {
-			python.Syntax.importAs("termios", "python.lib.Termios");
-		}
-		catch (e:Dynamic) {}
-	}
-
 }

+ 1 - 9
std/python/lib/Tty.hx

@@ -1,15 +1,7 @@
 
 package python.lib;
 
+@:import("tty", ignoreError=true)
 extern class Tty {
 	public static function setraw (fileNo:Int):Void;
-
-
-	static function __init__ ():Void
-	{
-		try {
-			python.Syntax.importAs("tty", "python.lib.Tty");
-		} catch (e:Dynamic) {}
-
-	}
 }

+ 9 - 0
tests/unit/TestPython.hx

@@ -101,6 +101,15 @@ extern class ExternModule {
 	static function f(v:Int):Int;
 }
 
+@:import("inexistant", "AZAZA", ignoreError=true)
+extern class InexistantExtern1 {}
+
+@:import("inexistant", "AZAZA.ZAZA", ignoreError=true)
+extern class InexistantExtern2 {}
+
+@:import("inexistant", ignoreError=true)
+extern class InexistantExtern3 {}
+
 class TestPython extends Test {
 
 	public function testDoWhileAsExpression () {