瀏覽代碼

try to get Process working (in progress)

frabbit 11 年之前
父節點
當前提交
ca6bd04adf

+ 3 - 1
std/haxe/io/Input.hx

@@ -129,8 +129,10 @@ class Input {
 		var last : Int;
 		var s;
 		try {
-			while( (last = readByte()) != 10 )
+			while( (last = readByte()) != 10 ) {
+				trace("read" + last);
 				buf.addChar( last );
+			}
 			s = buf.toString();
 			if( s.charCodeAt(s.length-1) == 13 ) s = s.substr(0,-1);
 		} catch( e : Eof ) {

+ 23 - 13
std/python/_std/sys/io/Process.hx

@@ -1,26 +1,36 @@
 
 package sys.io;
 
+import python.lib.Subprocess;
+import python.lib.subprocess.Popen;
+
 class Process {
 
-	var stdout(default,null) : haxe.io.Input;
-	var stderr(default,null) : haxe.io.Input;
-	var stdin(default,null) : haxe.io.Output;
+	public var stdout(default,null) : haxe.io.Input;
+	public var stderr(default,null) : haxe.io.Input;
+	public var stdin(default,null) : haxe.io.Output;
+
+	var p:Popen;
 
-	function new( cmd : String, args : Array<String> ) : Void {
-		throw "not implemented";
+	public function new( cmd : String, args : Array<String> ) : Void {
+
+		p = Popen.create(args, { bufsize : 1, executable : cmd, stdin : Subprocess.PIPE, stdout: Subprocess.PIPE, stderr : Subprocess.PIPE });
+		this.stdout = new FileInput (cast p.stdout);
+		this.stderr = new FileInput (cast p.stderr);
+		this.stdin =  new FileOutput(cast p.stdin);
 	}
-	function getPid() : Int {
-		return throw "not implemented";
+
+	public function getPid() : Int {
+		return p.pid;
 	}
-	function exitCode() : Int {
-		return throw "not implemented";
+	public function exitCode() : Int {
+		return p.returncode;
 	}
-	function close() : Void {
-		throw "not implemented";
+	public function close() : Void {
+		p.terminate();
 	}
-	function kill() : Void {
-		throw "not implemented";
+	public function kill() : Void {
+		p.kill();
 	}
 
 }

+ 6 - 3
std/python/io/NativeInput.hx

@@ -36,9 +36,12 @@ class NativeInput extends Input{
 
 	override public function readByte():Int
 	{
-		var ret = cast stream.read(1);
-		if (ret == null) throw new Eof();
-		return ret;
+
+		var ret = stream.read(1);
+		trace(ret);
+		if (ret.length == 0) throw new Eof();
+
+		return ret.get(0);
 	}
 
 	public function seek( p : Int, pos : sys.io.FileSeek ) : Void

+ 2 - 2
std/python/lib/Subprocess.hx

@@ -23,9 +23,9 @@ extern class Subprocess {
 
 	//public static var CREATE_NEW_PROCESS_GROUP;
 
-	public static var PIPE:Dynamic;
+	public static var PIPE:Int;
 
-	public static var STDOUT:Dynamic;
+	public static var STDOUT:Int;
 
 	public static function call(args:Array<String>):Int;
 

+ 5 - 0
std/python/lib/Types.hx

@@ -31,6 +31,11 @@ extern class ByteArray implements ArrayAccess<Int> {
 	public inline function get_length ():Int {
 		return Builtin.len(this);
 	}
+
+	public inline function get(i:Int):Int {
+		return untyped __python_array_get__(this, i);
+	}
+
 	public function decode(encoding:String="utf-8", errors:String="strict"):String;
 }
 

+ 10 - 0
std/python/lib/io/BufferedReader.hx

@@ -0,0 +1,10 @@
+
+package python.lib.io;
+
+import python.lib.io.BufferedIOBase;
+
+extern class BufferedReader extends BufferedIOBase {
+
+
+
+}

+ 23 - 9
std/python/lib/subprocess/Popen.hx

@@ -1,6 +1,7 @@
 
 package python.lib.subprocess;
 
+import python.lib.io.BufferedReader;
 import python.lib.io.TextIOBase;
 import python.lib.Subprocess.StartupInfo;
 import python.lib.Types;
@@ -8,9 +9,9 @@ import python.lib.Types;
 typedef PopenOptions = {
 	?bufsize : Int,
 	?executable  : String,
-	?stdin  : TextIOBase,
-	?stdout  : TextIOBase,
-	?stderr : TextIOBase,
+	?stdin  : Dynamic,
+	?stdout  : Dynamic,
+	?stderr : Dynamic,
 	?preexec_fn : Void->Void,
 	?close_fds : Bool,
 	?shell : Bool,
@@ -37,28 +38,41 @@ extern class Popen {
 		o.env = if (Reflect.hasField(o, "env")) o.env else null;
 		o.universal_newlines = if (Reflect.hasField(o, "universal_newlines")) o.universal_newlines else null;
 		o.startupinfo = if (Reflect.hasField(o, "startupinfo")) o.startupinfo else null;
+
 		o.creationflags = if (Reflect.hasField(o, "creationflags")) o.creationflags else 0;
 
-		return new Popen(args, o.bufsize, o.executable, o.stdin, o.stdout, o.stderr, o.preexec_fn,
+		if (std.Sys.systemName() == "Windows") {
+			return new Popen(args, o.bufsize, o.executable, o.stdin, o.stdout, o.stderr, o.preexec_fn,
 			o.close_fds, o.shell, o.cwd, o.env, o.universal_newlines, o.startupinfo, o.creationflags);
+		} else {
+			return new Popen(args, o.bufsize, o.executable, o.stdin, o.stdout, o.stderr, o.preexec_fn,
+			o.close_fds, o.shell, o.cwd, o.env, o.universal_newlines, o.startupinfo);
+		}
+
 	}
 
-	public function new (args:Array<String>, bufsize:Int=0, executable:String = null, 
-			stdin:FileObject = null, stdout:FileObject = null, stderr:FileObject=null, preexec_fn:Void->Void=null, 
-			close_fds:Bool=false, shell:Bool=false, cwd:String=null, env:Dict<String,String>=null, 
+	public function new (args:Array<String>, bufsize:Int=0, executable:String = null,
+			stdin:Int = null, stdout:Int = null, stderr:Int=null, preexec_fn:Void->Void=null,
+			close_fds:Bool=false, shell:Bool=false, cwd:String=null, env:Dict<String,String>=null,
 			universal_newlines:Bool=false, startupinfo:StartupInfo=null, creationflags:Int=0):Void;
 
 
-	public var stdin:TextIOBase;
+
 
 	public function kill ():Void;
 	public function wait (?timeout:Null<Int>):Null<Int>;
 	public function poll ():Null<Int>;
 	public function terminate ():Void;
 
+	public var stdout : BufferedReader;
+	public var stderr : BufferedReader;
+	public var stdin : BufferedReader;
+	public var returncode : Int;
+	public var pid:Int;
+
 	public function communicate (input:Bytes = null, timeout:Null<Int> = null):Tup2<Bytes, Bytes>;
 
-	static function __init__ ():Void 
+	static function __init__ ():Void
 	{
 		python.Macros.importFromAs("subprocess", "Popen", "python.lib.subprocess.Popen");
 	}