Browse Source

Implement haxe.CallStack for Python (closes https://github.com/frabbit/hx2python/issues/28)

Dan Korostelev 11 năm trước cách đây
mục cha
commit
491aaebead
4 tập tin đã thay đổi với 40 bổ sung1 xóa
  1. 19 0
      std/haxe/CallStack.hx
  2. 2 0
      std/python/lib/Sys.hx
  3. 15 0
      std/python/lib/Traceback.hx
  4. 4 1
      std/python/lib/Types.hx

+ 19 - 0
std/haxe/CallStack.hx

@@ -101,6 +101,14 @@ class CallStack {
 			return stack;
 		#elseif cs
 			return makeStack(new cs.system.diagnostics.StackTrace(1, true));
+		#elseif python
+			var stack = [];
+			var infos = python.lib.Traceback.extract_stack();
+			infos.pop();
+			infos.reverse();
+			for (elem in infos)
+				stack.push(FilePos(null, elem._1, elem._2));
+			return stack;
 		#else
 			return []; // Unsupported
 		#end
@@ -159,6 +167,17 @@ class CallStack {
 			return stack;
 		#elseif cs
 			return makeStack(new cs.system.diagnostics.StackTrace(cs.internal.Exceptions.exception, true));
+		#elseif python
+			var stack = [];
+			var exc = python.lib.Sys.exc_info();
+			if (exc._3 != null)
+			{
+				var infos = python.lib.Traceback.extract_tb(exc._3);
+				infos.reverse();
+				for (elem in infos)
+					stack.push(FilePos(null, elem._1, elem._2));
+			}
+			return stack;
 		#else
 			return []; // Unsupported
 		#end

+ 2 - 0
std/python/lib/Sys.hx

@@ -24,6 +24,8 @@ extern class Sys {
 
 	public static var maxsize:Int;
 
+	public static function exc_info<T:BaseException>():Tup3<Class<T>, T, TB>;
+
 	static function __init__ ():Void
 	{
 		python.Syntax.importAs("sys", "python.lib.Sys");

+ 15 - 0
std/python/lib/Traceback.hx

@@ -0,0 +1,15 @@
+package python.lib;
+
+import python.lib.Types;
+
+extern class Traceback {
+
+	public static function extract_stack(?f:Frame, ?limit:Int):Array<StackItem>;
+	public static function extract_tb(tb:TB, ?limit:Int):Array<StackItem>;
+
+	static function __init__ ():Void {
+		python.Syntax.importAs("traceback", "python.lib.Traceback");
+	}
+}
+
+private typedef StackItem = Tup4<String, Int, String, String>;

+ 4 - 1
std/python/lib/Types.hx

@@ -706,4 +706,7 @@ extern class BytesWarning extends Warning
 extern class ResourceWarning extends Warning
 {
 
-}
+}
+
+extern class TB {}
+extern class Frame {}