Selaa lähdekoodia

[python] Allow main() as entry point when imported (#7529)

* [python] execute main() to when imported as a module - rolls back #6865

* [python] add python global `__name__` to python.Lib
George Corney 6 vuotta sitten
vanhempi
commit
9c2aec4793
2 muutettua tiedostoa jossa 6 lisäystä ja 4 poistoa
  1. 2 4
      src/generators/genpy.ml
  2. 4 0
      std/python/Lib.hx

+ 2 - 4
src/generators/genpy.ml

@@ -2463,13 +2463,11 @@ module Generator = struct
 			| Some e ->
 			| Some e ->
 				newline ctx;
 				newline ctx;
 				newline ctx;
 				newline ctx;
-				spr ctx "if __name__ == '__main__':";
-				newline ctx;
 				match e.eexpr with
 				match e.eexpr with
 				| TBlock el ->
 				| TBlock el ->
-					List.iter (fun e -> gen_expr ctx e "" "    "; newline ctx) el;
+					List.iter (fun e -> gen_expr ctx e "" ""; newline ctx) el
 				| _ ->
 				| _ ->
-					gen_expr ctx e "" "    "; newline ctx
+					gen_expr ctx e "" ""; newline ctx
 
 
 	(* Entry point *)
 	(* Entry point *)
 
 

+ 4 - 0
std/python/Lib.hx

@@ -33,6 +33,10 @@ typedef PySys = python.lib.Sys;
 	and vice-versa.
 	and vice-versa.
 **/
 **/
 class Lib {
 class Lib {
+
+	static public var __name__(get, never):String;
+	static inline function get___name__():String return python.Syntax.code('__name__');
+
 	/**
 	/**
 		Print the specified value on the default output.
 		Print the specified value on the default output.
 	**/
 	**/